vserver 2.0-pre4
[linux-2.6.git] / kernel / vserver / inode.c
1 /*
2  *  linux/kernel/vserver/inode.c
3  *
4  *  Virtual Server: File System Support
5  *
6  *  Copyright (C) 2004-2005  Herbert Pötzl
7  *
8  *  V0.01  separated from vcontext V0.05
9  *
10  */
11
12 #include <linux/sched.h>
13 #include <linux/vs_context.h>
14 #include <linux/proc_fs.h>
15 #include <linux/devpts_fs.h>
16 #include <linux/namei.h>
17 #include <linux/mount.h>
18 #include <linux/parser.h>
19 #include <linux/compat.h>
20 #include <linux/vserver/inode.h>
21 #include <linux/vserver/inode_cmd.h>
22 #include <linux/vserver/xid.h>
23
24 #include <asm/errno.h>
25 #include <asm/uaccess.h>
26
27
28 static int __vc_get_iattr(struct inode *in, uint32_t *xid, uint32_t *flags, uint32_t *mask)
29 {
30         struct proc_dir_entry *entry;
31
32         if (!in || !in->i_sb)
33                 return -ESRCH;
34
35         *flags = IATTR_XID
36                 | (IS_BARRIER(in) ? IATTR_BARRIER : 0)
37                 | (IS_IUNLINK(in) ? IATTR_IUNLINK : 0)
38                 | (IS_IMMUTABLE(in) ? IATTR_IMMUTABLE : 0);
39         *mask = IATTR_IUNLINK | IATTR_IMMUTABLE;
40
41         if (S_ISDIR(in->i_mode))
42                 *mask |= IATTR_BARRIER;
43
44         if (in->i_sb->s_flags & MS_TAGXID) {
45                 *xid = in->i_xid;
46                 *mask |= IATTR_XID;
47         }
48
49         switch (in->i_sb->s_magic) {
50         case PROC_SUPER_MAGIC:
51                 entry = PROC_I(in)->pde;
52
53                 // check for specific inodes ?
54                 if (entry)
55                         *mask |= IATTR_FLAGS;
56                 if (entry)
57                         *flags |= (entry->vx_flags & IATTR_FLAGS);
58                 else
59                         *flags |= (PROC_I(in)->vx_flags & IATTR_FLAGS);
60                 break;
61
62         case DEVPTS_SUPER_MAGIC:
63                 *xid = in->i_xid;
64                 *mask |= IATTR_XID;
65                 break;
66
67         default:
68                 break;
69         }
70         return 0;
71 }
72
73 int vc_get_iattr(uint32_t id, void __user *data)
74 {
75         struct nameidata nd;
76         struct vcmd_ctx_iattr_v1 vc_data = { .xid = -1 };
77         int ret;
78
79         if (!vx_check(0, VX_ADMIN))
80                 return -ENOSYS;
81         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
82                 return -EFAULT;
83
84         ret = user_path_walk_link(vc_data.name, &nd);
85         if (!ret) {
86                 ret = __vc_get_iattr(nd.dentry->d_inode,
87                         &vc_data.xid, &vc_data.flags, &vc_data.mask);
88                 path_release(&nd);
89         }
90         if (ret)
91                 return ret;
92
93         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
94                 ret = -EFAULT;
95         return ret;
96 }
97
98 #ifdef  CONFIG_COMPAT
99
100 int vc_get_iattr_x32(uint32_t id, void __user *data)
101 {
102         struct nameidata nd;
103         struct vcmd_ctx_iattr_v1_x32 vc_data = { .xid = -1 };
104         int ret;
105
106         if (!vx_check(0, VX_ADMIN))
107                 return -ENOSYS;
108         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
109                 return -EFAULT;
110
111         ret = user_path_walk_link(compat_ptr(vc_data.name_ptr), &nd);
112         if (!ret) {
113                 ret = __vc_get_iattr(nd.dentry->d_inode,
114                         &vc_data.xid, &vc_data.flags, &vc_data.mask);
115                 path_release(&nd);
116         }
117         if (ret)
118                 return ret;
119
120         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
121                 ret = -EFAULT;
122         return ret;
123 }
124
125 #endif  /* CONFIG_COMPAT */
126
127
128 static int __vc_set_iattr(struct dentry *de, uint32_t *xid, uint32_t *flags, uint32_t *mask)
129 {
130         struct inode *in = de->d_inode;
131         int error = 0, is_proc = 0, has_xid = 0;
132
133         if (!in || !in->i_sb)
134                 return -ESRCH;
135
136         is_proc = (in->i_sb->s_magic == PROC_SUPER_MAGIC);
137         if ((*mask & IATTR_FLAGS) && !is_proc)
138                 return -EINVAL;
139
140         has_xid = (in->i_sb->s_flags & MS_TAGXID) ||
141                 (in->i_sb->s_magic == DEVPTS_SUPER_MAGIC);
142         if ((*mask & IATTR_XID) && !has_xid)
143                 return -EINVAL;
144
145         down(&in->i_sem);
146         if (*mask & IATTR_XID)
147                 in->i_xid = *xid;
148
149         if (*mask & IATTR_FLAGS) {
150                 struct proc_dir_entry *entry = PROC_I(in)->pde;
151                 unsigned int iflags = PROC_I(in)->vx_flags;
152
153                 iflags = (iflags & ~(*mask & IATTR_FLAGS))
154                         | (*flags & IATTR_FLAGS);
155                 PROC_I(in)->vx_flags = iflags;
156                 if (entry)
157                         entry->vx_flags = iflags;
158         }
159
160         if (*mask & (IATTR_BARRIER | IATTR_IUNLINK | IATTR_IMMUTABLE)) {
161                 struct iattr attr;
162
163                 attr.ia_valid = ATTR_ATTR_FLAG;
164                 attr.ia_attr_flags =
165                         (IS_IMMUTABLE(in) ? ATTR_FLAG_IMMUTABLE : 0) |
166                         (IS_IUNLINK(in) ? ATTR_FLAG_IUNLINK : 0) |
167                         (IS_BARRIER(in) ? ATTR_FLAG_BARRIER : 0);
168
169                 if (*mask & IATTR_IMMUTABLE) {
170                         if (*flags & IATTR_IMMUTABLE)
171                                 attr.ia_attr_flags |= ATTR_FLAG_IMMUTABLE;
172                         else
173                                 attr.ia_attr_flags &= ~ATTR_FLAG_IMMUTABLE;
174                 }
175                 if (*mask & IATTR_IUNLINK) {
176                         if (*flags & IATTR_IUNLINK)
177                                 attr.ia_attr_flags |= ATTR_FLAG_IUNLINK;
178                         else
179                                 attr.ia_attr_flags &= ~ATTR_FLAG_IUNLINK;
180                 }
181                 if (S_ISDIR(in->i_mode) && (*mask & IATTR_BARRIER)) {
182                         if (*flags & IATTR_BARRIER)
183                                 attr.ia_attr_flags |= ATTR_FLAG_BARRIER;
184                         else
185                                 attr.ia_attr_flags &= ~ATTR_FLAG_BARRIER;
186                 }
187                 if (in->i_op && in->i_op->setattr)
188                         error = in->i_op->setattr(de, &attr);
189                 else {
190                         error = inode_change_ok(in, &attr);
191                         if (!error)
192                                 error = inode_setattr(in, &attr);
193                 }
194         }
195
196         mark_inode_dirty(in);
197         up(&in->i_sem);
198         return 0;
199 }
200
201 int vc_set_iattr(uint32_t id, void __user *data)
202 {
203         struct nameidata nd;
204         struct vcmd_ctx_iattr_v1 vc_data;
205         int ret;
206
207         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_LINUX_IMMUTABLE))
208                 return -EPERM;
209         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
210                 return -EFAULT;
211
212         ret = user_path_walk_link(vc_data.name, &nd);
213         if (!ret) {
214                 ret = __vc_set_iattr(nd.dentry,
215                         &vc_data.xid, &vc_data.flags, &vc_data.mask);
216                 path_release(&nd);
217         }
218
219         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
220                 ret = -EFAULT;
221         return ret;
222 }
223
224 #ifdef  CONFIG_COMPAT
225
226 int vc_set_iattr_x32(uint32_t id, void __user *data)
227 {
228         struct nameidata nd;
229         struct vcmd_ctx_iattr_v1_x32 vc_data;
230         int ret;
231
232         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_LINUX_IMMUTABLE))
233                 return -EPERM;
234         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
235                 return -EFAULT;
236
237         ret = user_path_walk_link(compat_ptr(vc_data.name_ptr), &nd);
238         if (!ret) {
239                 ret = __vc_set_iattr(nd.dentry,
240                         &vc_data.xid, &vc_data.flags, &vc_data.mask);
241                 path_release(&nd);
242         }
243
244         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
245                 ret = -EFAULT;
246         return ret;
247 }
248
249 #endif  /* CONFIG_COMPAT */
250
251 #ifdef  CONFIG_VSERVER_LEGACY
252
253 #define PROC_DYNAMIC_FIRST 0xF0000000UL
254
255 int vx_proc_ioctl(struct inode * inode, struct file * filp,
256         unsigned int cmd, unsigned long arg)
257 {
258         struct proc_dir_entry *entry;
259         int error = 0;
260         int flags;
261
262         if (inode->i_ino < PROC_DYNAMIC_FIRST)
263                 return -ENOTTY;
264
265         entry = PROC_I(inode)->pde;
266         if (!entry)
267                 return -ENOTTY;
268
269         switch(cmd) {
270         case FIOC_GETXFLG: {
271                 /* fixme: if stealth, return -ENOTTY */
272                 error = -EPERM;
273                 flags = entry->vx_flags;
274                 if (capable(CAP_CONTEXT))
275                         error = put_user(flags, (int *) arg);
276                 break;
277         }
278         case FIOC_SETXFLG: {
279                 /* fixme: if stealth, return -ENOTTY */
280                 error = -EPERM;
281                 if (!capable(CAP_CONTEXT))
282                         break;
283                 error = -EROFS;
284                 if (IS_RDONLY(inode))
285                         break;
286                 error = -EFAULT;
287                 if (get_user(flags, (int *) arg))
288                         break;
289                 error = 0;
290                 entry->vx_flags = flags;
291                 break;
292         }
293         default:
294                 return -ENOTTY;
295         }
296         return error;
297 }
298 #endif
299
300
301 int vx_parse_xid(char *string, xid_t *xid, int remove)
302 {
303         static match_table_t tokens = {
304                 {1, "xid=%u"},
305                 {0, NULL}
306         };
307         substring_t args[MAX_OPT_ARGS];
308         int token, option = 0;
309
310         if (!string)
311                 return 0;
312
313         token = match_token(string, tokens, args);
314         if (token && xid && !match_int(args, &option))
315                 *xid = option;
316
317         vxdprintk(VXD_CBIT(xid, 7),
318                 "vx_parse_xid(»%s«): %d:#%d",
319                 string, token, option);
320
321         if (token && remove) {
322                 char *p = strstr(string, "xid=");
323                 char *q = p;
324
325                 if (p) {
326                         while (*q != '\0' && *q != ',')
327                                 q++;
328                         while (*q)
329                                 *p++ = *q++;
330                         while (*p)
331                                 *p++ = '\0';
332                 }
333         }
334         return token;
335 }
336
337 void vx_propagate_xid(struct nameidata *nd, struct inode *inode)
338 {
339         xid_t new_xid = 0;
340         struct vfsmount *mnt;
341         int propagate;
342
343         if (!nd)
344                 return;
345         mnt = nd->mnt;
346         if (!mnt)
347                 return;
348
349         propagate = (mnt->mnt_flags & MNT_XID);
350         if (propagate)
351                 new_xid = mnt->mnt_xid;
352
353         vxdprintk(VXD_CBIT(xid, 7),
354                 "vx_propagate_xid(%p[#%lu.%d]): %d,%d",
355                 inode, inode->i_ino, inode->i_xid,
356                 new_xid, (propagate)?1:0);
357
358         if (propagate)
359                 inode->i_xid = new_xid;
360 }
361
362 #include <linux/module.h>
363
364 EXPORT_SYMBOL_GPL(vx_propagate_xid);
365