2 * linux/kernel/vserver/inode.c
4 * Virtual Server: File System Support
6 * Copyright (C) 2004-2005 Herbert Pötzl
8 * V0.01 separated from vcontext V0.05
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>
24 #include <asm/errno.h>
25 #include <asm/uaccess.h>
28 static int __vc_get_iattr(struct inode *in, uint32_t *xid, uint32_t *flags, uint32_t *mask)
30 struct proc_dir_entry *entry;
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;
41 if (S_ISDIR(in->i_mode))
42 *mask |= IATTR_BARRIER;
49 switch (in->i_sb->s_magic) {
50 case PROC_SUPER_MAGIC:
51 entry = PROC_I(in)->pde;
53 /* check for specific inodes? */
57 *flags |= (entry->vx_flags & IATTR_FLAGS);
59 *flags |= (PROC_I(in)->vx_flags & IATTR_FLAGS);
62 case DEVPTS_SUPER_MAGIC:
73 int vc_get_iattr(uint32_t id, void __user *data)
76 struct vcmd_ctx_iattr_v1 vc_data = { .xid = -1 };
79 if (!vx_check(0, VX_ADMIN))
81 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
84 ret = user_path_walk_link(vc_data.name, &nd);
86 ret = __vc_get_iattr(nd.dentry->d_inode,
87 &vc_data.xid, &vc_data.flags, &vc_data.mask);
93 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
100 int vc_get_iattr_x32(uint32_t id, void __user *data)
103 struct vcmd_ctx_iattr_v1_x32 vc_data = { .xid = -1 };
106 if (!vx_check(0, VX_ADMIN))
108 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
111 ret = user_path_walk_link(compat_ptr(vc_data.name_ptr), &nd);
113 ret = __vc_get_iattr(nd.dentry->d_inode,
114 &vc_data.xid, &vc_data.flags, &vc_data.mask);
120 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
125 #endif /* CONFIG_COMPAT */
128 static int __vc_set_iattr(struct dentry *de, uint32_t *xid, uint32_t *flags, uint32_t *mask)
130 struct inode *in = de->d_inode;
131 int error = 0, is_proc = 0, has_xid = 0;
132 struct iattr attr = { 0 };
134 if (!in || !in->i_sb)
137 is_proc = (in->i_sb->s_magic == PROC_SUPER_MAGIC);
138 if ((*mask & IATTR_FLAGS) && !is_proc)
141 has_xid = IS_TAGXID(in) ||
142 (in->i_sb->s_magic == DEVPTS_SUPER_MAGIC);
143 if ((*mask & IATTR_XID) && !has_xid)
146 mutex_lock(&in->i_mutex);
147 if (*mask & IATTR_XID) {
149 attr.ia_valid |= ATTR_XID;
152 if (*mask & IATTR_FLAGS) {
153 struct proc_dir_entry *entry = PROC_I(in)->pde;
154 unsigned int iflags = PROC_I(in)->vx_flags;
156 iflags = (iflags & ~(*mask & IATTR_FLAGS))
157 | (*flags & IATTR_FLAGS);
158 PROC_I(in)->vx_flags = iflags;
160 entry->vx_flags = iflags;
163 if (*mask & (IATTR_BARRIER | IATTR_IUNLINK | IATTR_IMMUTABLE)) {
164 if (*mask & IATTR_IMMUTABLE) {
165 if (*flags & IATTR_IMMUTABLE)
166 in->i_flags |= S_IMMUTABLE;
168 in->i_flags &= ~S_IMMUTABLE;
170 if (*mask & IATTR_IUNLINK) {
171 if (*flags & IATTR_IUNLINK)
172 in->i_flags |= S_IUNLINK;
174 in->i_flags &= ~S_IUNLINK;
176 if (S_ISDIR(in->i_mode) && (*mask & IATTR_BARRIER)) {
177 if (*flags & IATTR_BARRIER)
178 in->i_flags |= S_BARRIER;
180 in->i_flags &= ~S_BARRIER;
182 if (in->i_op && in->i_op->sync_flags) {
183 error = in->i_op->sync_flags(in);
190 if (in->i_op && in->i_op->setattr)
191 error = in->i_op->setattr(de, &attr);
193 error = inode_change_ok(in, &attr);
195 error = inode_setattr(in, &attr);
200 mutex_unlock(&in->i_mutex);
204 int vc_set_iattr(uint32_t id, void __user *data)
207 struct vcmd_ctx_iattr_v1 vc_data;
210 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_LINUX_IMMUTABLE))
212 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
215 ret = user_path_walk_link(vc_data.name, &nd);
217 ret = __vc_set_iattr(nd.dentry,
218 &vc_data.xid, &vc_data.flags, &vc_data.mask);
222 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
229 int vc_set_iattr_x32(uint32_t id, void __user *data)
232 struct vcmd_ctx_iattr_v1_x32 vc_data;
235 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_LINUX_IMMUTABLE))
237 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
240 ret = user_path_walk_link(compat_ptr(vc_data.name_ptr), &nd);
242 ret = __vc_set_iattr(nd.dentry,
243 &vc_data.xid, &vc_data.flags, &vc_data.mask);
247 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
252 #endif /* CONFIG_COMPAT */
254 #ifdef CONFIG_VSERVER_LEGACY
256 #define PROC_DYNAMIC_FIRST 0xF0000000UL
258 int vx_proc_ioctl(struct inode * inode, struct file * filp,
259 unsigned int cmd, unsigned long arg)
261 struct proc_dir_entry *entry;
265 if (inode->i_ino < PROC_DYNAMIC_FIRST)
268 entry = PROC_I(inode)->pde;
274 /* fixme: if stealth, return -ENOTTY */
276 flags = entry->vx_flags;
277 if (capable(CAP_CONTEXT))
278 error = put_user(flags, (int __user *) arg);
282 /* fixme: if stealth, return -ENOTTY */
284 if (!capable(CAP_CONTEXT))
287 if (IS_RDONLY(inode))
290 if (get_user(flags, (int __user *) arg))
293 entry->vx_flags = flags;
304 int vx_parse_xid(char *string, xid_t *xid, int remove)
306 static match_table_t tokens = {
310 substring_t args[MAX_OPT_ARGS];
311 int token, option = 0;
316 token = match_token(string, tokens, args);
317 if (token && xid && !match_int(args, &option))
320 vxdprintk(VXD_CBIT(xid, 7),
321 "vx_parse_xid(»%s«): %d:#%d",
322 string, token, option);
324 if (token && remove) {
325 char *p = strstr(string, "xid=");
329 while (*q != '\0' && *q != ',')
340 void vx_propagate_xid(struct nameidata *nd, struct inode *inode)
343 struct vfsmount *mnt;
352 propagate = (mnt->mnt_flags & MNT_XID);
354 new_xid = mnt->mnt_xid;
356 vxdprintk(VXD_CBIT(xid, 7),
357 "vx_propagate_xid(%p[#%lu.%d]): %d,%d",
358 inode, inode->i_ino, inode->i_xid,
359 new_xid, (propagate)?1:0);
362 inode->i_xid = new_xid;
365 #include <linux/module.h>
367 EXPORT_SYMBOL_GPL(vx_propagate_xid);