This is the big piece. Benjamin
I looked and the VFS actually allows that. All that is needed is for /sys/class/net to implement a follow link method to redirect lookups to the real directory you want. I am calling the concept of multiple directories all at the same path in the filesystem shadow directories, the directory entry that implements the follow_link method the shadow master, and the directories that are the target of the follow link method shadow directories. It turns out that just implementing a follow_link method is not quite enough. The existince of directories of the form /sys/ ... /net/ can depend on the presence or absence of hotplug hardware, which means I need a simple race free way to create and destroy these directories. To achieve a race free design all shadow directories are created and managed by sysfs itself. The upper level code that knows what shadow directories we need provides just two methods that enable this: current_tag() - that returns a "void *" tag that identifies the context of the current process. kobject_tag(kobj) - that returns a "void *" tag that identifies the context a kobject should be in. Everything else is left up to sysfs. For the network namespace current_tag and kobject_tag are essentially one line functions, and look to remain that. The work needed in sysfs is more extensive. At each directory or symlink creation I need to check if the shadow directory it belongs in exists and if it does not create it. Likewise at each symlink or directory removal I need to check if sysfs directory it is being removed from is a shadow directory and if this is the last object in the shadow directory and if so to remove the shadow directory as well. I also need a bunch of boiler plate that properly finds, creates, and removes/frees the shadow directories. Doing all of that in sysfs isn't bad it is just a bit tedious. Being race free is just a manner of ensure we have the directory inode mutex when we add or remove a shadow directory. Trying to do this race free anywhere besides in sysfs is very nasty, and requires unhealthy amounts of information about how sysfs is implemented. Currently only directories which hold kobjects, and symlinks are supported. There is not enough information in the current file attribute interfaces to give us anything to discriminate on which makes it useless, and there are not potential users which makes it an uniteresting problem to solve. Changelog: - Port to 2.6.21-mm2 on top of GregKH's sysfs patches - Benjamin Thery <benjamin.thery@xxxxxxxx> Signed-off-by: Eric W. Biederman <ebiederm@xxxxxxxxxxxx> =================================================================== Index: linux-2.6.21-mm2-netns2/fs/sysfs/bin.c =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/bin.c +++ linux-2.6.21-mm2-netns2/fs/sysfs/bin.c @@ -248,7 +248,7 @@ int sysfs_create_bin_file(struct kobject void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) { - if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) { + if (sysfs_hash_and_remove(kobj, kobj->dentry, attr->attr.name) < 0) { printk(KERN_ERR "%s: " "bad dentry or inode or no such file: \"%s\"\n", __FUNCTION__, attr->attr.name); Index: linux-2.6.21-mm2-netns2/fs/sysfs/file.c =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/file.c +++ linux-2.6.21-mm2-netns2/fs/sysfs/file.c @@ -560,7 +560,7 @@ EXPORT_SYMBOL_GPL(sysfs_chmod_file); void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr) { - sysfs_hash_and_remove(kobj->dentry, attr->name); + sysfs_hash_and_remove(kobj, kobj->dentry, attr->name); } @@ -577,7 +577,7 @@ void sysfs_remove_file_from_group(struct dir = lookup_one_len(group, kobj->dentry, strlen(group)); if (!IS_ERR(dir)) { - sysfs_hash_and_remove(dir, attr->name); + sysfs_hash_and_remove(kobj, dir, attr->name); dput(dir); } } Index: linux-2.6.21-mm2-netns2/fs/sysfs/group.c =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/group.c +++ linux-2.6.21-mm2-netns2/fs/sysfs/group.c @@ -17,16 +17,16 @@ #include "sysfs.h" -static void remove_files(struct dentry * dir, +static void remove_files(struct kobject *kobj, struct dentry * dir, const struct attribute_group * grp) { struct attribute *const* attr; for (attr = grp->attrs; *attr; attr++) - sysfs_hash_and_remove(dir,(*attr)->name); + sysfs_hash_and_remove(kobj, dir,(*attr)->name); } -static int create_files(struct dentry * dir, +static int create_files(struct kobject *kobj, struct dentry * dir, const struct attribute_group * grp) { struct attribute *const* attr; @@ -36,7 +36,7 @@ static int create_files(struct dentry * error = sysfs_add_file(dir, *attr, SYSFS_KOBJ_ATTR); } if (error) - remove_files(dir,grp); + remove_files(kobj, dir, grp); return error; } @@ -56,7 +56,7 @@ int sysfs_create_group(struct kobject * } else dir = kobj->dentry; dir = dget(dir); - if ((error = create_files(dir,grp))) { + if ((error = create_files(kobj, dir, grp))) { if (grp->name) sysfs_remove_subdir(dir); } @@ -77,7 +77,7 @@ void sysfs_remove_group(struct kobject * else dir = dget(kobj->dentry); - remove_files(dir,grp); + remove_files(kobj, dir, grp); if (grp->name) sysfs_remove_subdir(dir); /* release the ref. taken in this routine */ Index: linux-2.6.21-mm2-netns2/fs/sysfs/inode.c =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/inode.c +++ linux-2.6.21-mm2-netns2/fs/sysfs/inode.c @@ -259,8 +259,9 @@ void sysfs_drop_dentry(struct sysfs_dire dput(parent); } -int sysfs_hash_and_remove(struct dentry * dir, const char * name) +int sysfs_hash_and_remove(struct kobject *kobj, struct dentry * dir, const char * name) { + struct inode * inode; struct sysfs_dirent * sd; struct sysfs_dirent * parent_sd; int found = 0; @@ -272,8 +273,13 @@ int sysfs_hash_and_remove(struct dentry /* no inode means this hasn't been made visible yet */ return -ENOENT; + inode = dir->d_inode; + mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); + dir = sysfs_resolve_for_remove(kobj, dir); + if (IS_ERR(dir)) + goto out_unlock; + dget(dir); parent_sd = dir->d_fsdata; - mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { if (!sd->s_type) continue; @@ -283,7 +289,8 @@ int sysfs_hash_and_remove(struct dentry break; } } - mutex_unlock(&dir->d_inode->i_mutex); +out_unlock: + mutex_unlock(&inode->i_mutex); if (!found) return -ENOENT; @@ -292,5 +299,10 @@ int sysfs_hash_and_remove(struct dentry sysfs_deactivate(sd); sysfs_put(sd); + if (!IS_ERR(dir) && dir) { + sysfs_prune_shadow(dir); + dput(dir); + } + return 0; } Index: linux-2.6.21-mm2-netns2/fs/sysfs/symlink.c =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/symlink.c +++ linux-2.6.21-mm2-netns2/fs/sysfs/symlink.c @@ -66,7 +66,7 @@ static int sysfs_add_link(struct sysfs_d */ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name) { - struct dentry *dentry = NULL; + struct dentry *dir, *dentry = NULL; struct sysfs_dirent *parent_sd = NULL; struct sysfs_dirent *target_sd = NULL; int error = -EEXIST; @@ -95,8 +95,11 @@ int sysfs_create_link(struct kobject * k return -ENOENT; mutex_lock(&dentry->d_inode->i_mutex); - if (!sysfs_dirent_exist(dentry->d_fsdata, name)) - error = sysfs_add_link(parent_sd, name, target_sd); + dir = sysfs_resolve_for_create(target, dentry); + if (IS_ERR(dir)) + error = PTR_ERR(dir); + else if (!sysfs_dirent_exist(dir->d_fsdata, name)) + error = sysfs_add_link(dir->d_fsdata, name, target_sd); mutex_unlock(&dentry->d_inode->i_mutex); if (error) @@ -114,7 +117,7 @@ int sysfs_create_link(struct kobject * k void sysfs_remove_link(struct kobject * kobj, const char * name) { - sysfs_hash_and_remove(kobj->dentry,name); + sysfs_hash_and_remove(kobj, kobj->dentry,name); } static int sysfs_get_target_path(struct sysfs_dirent * parent_sd, Index: linux-2.6.21-mm2-netns2/fs/sysfs/sysfs.h =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/sysfs.h +++ linux-2.6.21-mm2-netns2/fs/sysfs/sysfs.h @@ -14,6 +14,11 @@ struct sysfs_elem_bin_attr { struct bin_attribute * bin_attr; }; +struct sysfs_elem_shadow_dir { + struct kobject * kobj; + void * tag; +}; + /* * As long as s_count reference is held, the sysfs_dirent itself is * accessible. Dereferencing s_elem or any other outer entity @@ -32,6 +37,7 @@ struct sysfs_dirent { struct sysfs_elem_symlink symlink; struct sysfs_elem_attr attr; struct sysfs_elem_bin_attr bin_attr; + struct sysfs_elem_shadow_dir shadow_dir; } s_elem; int s_type; @@ -56,6 +62,10 @@ enum sysfs_s_active_class extern struct vfsmount * sysfs_mount; extern struct kmem_cache *sysfs_dir_cachep; +extern void sysfs_prune_shadow(struct dentry *shadow); +struct dentry *sysfs_resolve_for_create(struct kobject *, struct dentry *); +struct dentry *sysfs_resolve_for_remove(struct kobject *, struct dentry *); + extern struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent *); extern int sysfs_create(struct sysfs_dirent *sd, struct dentry *dentry, int mode, int (*init)(struct inode *)); @@ -69,7 +79,7 @@ extern void sysfs_attach_dirent(struct s struct dentry *dentry); extern int sysfs_add_file(struct dentry *, const struct attribute *, int); -extern int sysfs_hash_and_remove(struct dentry * dir, const char * name); +extern int sysfs_hash_and_remove(struct kobject *, struct dentry *, const char *); extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name); extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **); Index: linux-2.6.21-mm2-netns2/include/linux/sysfs.h =================================================================== --- linux-2.6.21-mm2-netns2.orig/include/linux/sysfs.h +++ linux-2.6.21-mm2-netns2/include/linux/sysfs.h @@ -72,13 +72,19 @@ struct sysfs_ops { ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t); }; +struct shadow_dir_operations { + void *(*current_tag)(void); + void *(*kobject_tag)(struct kobject *kobj); +}; + #define SYSFS_ROOT 0x0001 #define SYSFS_DIR 0x0002 #define SYSFS_KOBJ_ATTR 0x0004 #define SYSFS_KOBJ_BIN_ATTR 0x0008 #define SYSFS_KOBJ_LINK 0x0020 +#define SYSFS_SHADOW 0x0040 #define SYSFS_NOT_PINNED (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR | SYSFS_KOBJ_LINK) -#define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK) +#define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK | SYSFS_SHADOW) #ifdef CONFIG_SYSFS @@ -129,6 +135,8 @@ void sysfs_remove_file_from_group(struct void sysfs_notify(struct kobject * k, char *dir, char *attr); +int sysfs_enable_shadowing(struct kobject *, const struct shadow_dir_operations *); + extern int __must_check sysfs_init(void); #else /* CONFIG_SYSFS */ @@ -224,6 +232,12 @@ static inline void sysfs_notify(struct k { } +static inline int sysfs_enable_shadowing(struct kobject *kobj, + const struct shadow_dir_operations *shadow_ops) +{ + return 0; +} + static inline int __must_check sysfs_init(void) { return 0; Index: linux-2.6.21-mm2-netns2/fs/sysfs/dir.c =================================================================== --- linux-2.6.21-mm2-netns2.orig/fs/sysfs/dir.c +++ linux-2.6.21-mm2-netns2/fs/sysfs/dir.c @@ -217,21 +217,25 @@ static int init_symlink(struct inode * i static int create_dir(struct kobject *kobj, struct dentry *parent, const char *name, struct dentry **p_dentry) { + struct dentry *par; int error; umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; struct dentry *dentry; struct sysfs_dirent *sd; mutex_lock(&parent->d_inode->i_mutex); - - dentry = lookup_one_len(name, parent, strlen(name)); + par = sysfs_resolve_for_create(kobj, parent); + if (IS_ERR(parent)) + dentry = par; + else + dentry = lookup_one_len(name, par, strlen(name)); if (IS_ERR(dentry)) { error = PTR_ERR(dentry); goto out_unlock; } error = -EEXIST; - if (sysfs_dirent_exist(parent->d_fsdata, name)) + if (sysfs_dirent_exist(par->d_fsdata, name)) goto out_dput; error = -ENOMEM; @@ -244,8 +248,8 @@ static int create_dir(struct kobject *ko if (error) goto out_sput; - inc_nlink(parent->d_inode); - sysfs_attach_dirent(sd, parent->d_fsdata, dentry); + inc_nlink(par->d_inode); + sysfs_attach_dirent(sd, par->d_fsdata, dentry); *p_dentry = dentry; error = 0; @@ -263,6 +267,57 @@ static int create_dir(struct kobject *ko return error; } +static void sysfs_empty_dir(struct dentry *dentry) +{ + struct sysfs_dirent *parent_sd, *sd, *tmp; + + /* [BT] This code may be wrong: have a look at __sysfs_remove_dir() */ + parent_sd = dentry->d_fsdata; + list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { + if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED)) + continue; + list_del_init(&sd->s_sibling); + sysfs_drop_dentry(sd); + sysfs_deactivate(sd); + sysfs_put(sd); + } +} + +static void __sysfs_remove_empty_shadow(struct dentry *shadow) +{ + struct sysfs_dirent *sd; + + if (d_unhashed(shadow)) + return; + + sd = shadow->d_fsdata; + d_delete(shadow); + list_del_init(&sd->s_sibling); + simple_rmdir(shadow->d_inode, shadow); + sysfs_put(sd); +} + +static void sysfs_remove_empty_shadow(struct dentry *shadow) +{ + mutex_lock(&shadow->d_inode->i_mutex); + __sysfs_remove_empty_shadow(shadow); + mutex_unlock(&shadow->d_inode->i_mutex); +} + +static void sysfs_remove_shadows(struct dentry *dentry) +{ + struct sysfs_dirent *parent_sd, *sd, *tmp; + + parent_sd = dentry->d_fsdata; + list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { + struct dentry *shadow; + + shadow = dget(sd->s_dentry); + sysfs_empty_dir(shadow); + __sysfs_remove_empty_shadow(shadow); + dput(shadow); + } +} int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d) { @@ -381,6 +436,7 @@ static void remove_dir(struct dentry * d mutex_unlock(&parent->d_inode->i_mutex); + sysfs_prune_shadow(parent); sysfs_drop_dentry(sd); sysfs_deactivate(sd); sysfs_put(sd); @@ -403,19 +459,28 @@ static void __sysfs_remove_dir(struct de pr_debug("sysfs %s: removing dir\n",dentry->d_name.name); mutex_lock(&dentry->d_inode->i_mutex); - parent_sd = dentry->d_fsdata; - list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { - if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED)) + + if (dentry->d_inode->i_op == &sysfs_dir_inode_operations) { + parent_sd = dentry->d_fsdata; + list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, + s_sibling) { + if (!sd->s_type || + !(sd->s_type & SYSFS_NOT_PINNED)) continue; - list_move(&sd->s_sibling, &removed); - } + list_move(&sd->s_sibling, &removed); + } + } else + sysfs_remove_shadows(dentry); + mutex_unlock(&dentry->d_inode->i_mutex); - list_for_each_entry_safe(sd, tmp, &removed, s_sibling) { - list_del_init(&sd->s_sibling); - sysfs_drop_dentry(sd); - sysfs_deactivate(sd); - sysfs_put(sd); + if (dentry->d_inode->i_op == &sysfs_dir_inode_operations) { + list_for_each_entry_safe(sd, tmp, &removed, s_sibling) { + list_del_init(&sd->s_sibling); + sysfs_drop_dentry(sd); + sysfs_deactivate(sd); + sysfs_put(sd); + } } remove_dir(dentry); @@ -459,7 +524,7 @@ int sysfs_rename_dir(struct kobject * ko down_write(&sysfs_rename_sem); mutex_lock(&inode->i_mutex); - new_parent = kobj->dentry->d_parent; + new_parent = sysfs_resolve_for_create(kobj, kobj->dentry->d_parent); parent_sd = new_parent->d_fsdata; new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); if (IS_ERR(new_dentry)) { @@ -507,29 +572,41 @@ int sysfs_rename_dir(struct kobject * ko out_unlock: mutex_unlock(&inode->i_mutex); up_write(&sysfs_rename_sem); + sysfs_prune_shadow(old_parent); return error; } int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) { + struct inode *old_parent_inode, *new_parent_inode; struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; struct sysfs_dirent *new_parent_sd, *sd; int error; - old_parent_dentry = kobj->parent ? - kobj->parent->dentry : sysfs_mount->mnt_sb->s_root; + old_parent_dentry = kobj->dentry->d_parent; new_parent_dentry = new_parent ? new_parent->dentry : sysfs_mount->mnt_sb->s_root; - if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) + old_parent_inode = old_parent_dentry->d_inode; + new_parent_inode = new_parent_dentry->d_inode; + + if (old_parent_inode == new_parent_inode) return 0; /* nothing to move */ + + dget(old_parent_dentry); again: - mutex_lock(&old_parent_dentry->d_inode->i_mutex); - if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { - mutex_unlock(&old_parent_dentry->d_inode->i_mutex); + mutex_lock(&old_parent_inode->i_mutex); + if (!mutex_trylock(&new_parent_inode->i_mutex)) { + mutex_unlock(&old_parent_inode->i_mutex); goto again; } + new_parent_dentry = sysfs_resolve_for_create(kobj, new_parent_dentry); + if (IS_ERR(new_parent_dentry)) { + error = PTR_ERR(new_parent_dentry); + goto out; + } + new_parent_sd = new_parent_dentry->d_fsdata; sd = kobj->dentry->d_fsdata; @@ -549,8 +626,11 @@ again: list_add(&sd->s_sibling, &new_parent_sd->s_children); out: - mutex_unlock(&new_parent_dentry->d_inode->i_mutex); - mutex_unlock(&old_parent_dentry->d_inode->i_mutex); + mutex_unlock(&new_parent_inode->i_mutex); + mutex_unlock(&old_parent_inode->i_mutex); + + sysfs_prune_shadow(old_parent_dentry); + dput(old_parent_dentry); return error; } @@ -619,6 +699,11 @@ static int sysfs_readdir(struct file * f i++; /* fallthrough */ default: + /* If I am the shadow master return nothing. */ + if ((parent_sd->s_type == SYSFS_DIR) && + (dentry->d_inode->i_op != &sysfs_dir_inode_operations)) + return 0; + if (filp->f_pos == 2) list_move(q, &parent_sd->s_children); @@ -695,3 +780,249 @@ const struct file_operations sysfs_dir_o .read = generic_read_dir, .readdir = sysfs_readdir, }; + +static struct dentry *find_shadow_dir(struct dentry *parent, void *target) +{ + /* Find the shadow directory for the specified tag */ + struct sysfs_dirent *parent_sd, *sd; + struct dentry *result; + + result = NULL; + parent_sd = parent->d_fsdata; + list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { + if (sd->s_elem.shadow_dir.tag != target) + continue; + result = sd->s_dentry; + break; + } + return result; +} + +static void *find_shadow_tag(struct kobject *kobj) +{ + /* Find the tag the current kobj is cached with */ + struct dentry *dir; + struct sysfs_dirent *parent_sd, *sd; + void *result; + + result = ERR_PTR(-ENOENT); + dir = kobj->dentry; + sd = dir->d_parent->d_fsdata; + if (sd->s_type != SYSFS_SHADOW) + goto out; + parent_sd = sd->s_elem.shadow_dir.kobj->dentry->d_fsdata; + + list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { + struct dentry *dentry; + int found; + + dentry = lookup_one_len(dir->d_name.name, sd->s_dentry, + dir->d_name.len); + found = (dentry == dir); + dput(dentry); + + if (!found) + continue; + + result = sd->s_elem.shadow_dir.tag; + break; + } +out: + return result; +} + +static struct dentry *add_shadow_dir(struct dentry *dir, void *tag) +{ + struct sysfs_dirent *parent_sd, *sd; + struct dentry *shadow; + struct inode *inode; + int err; + + parent_sd = dir->d_fsdata; + inode = dir->d_inode; + + err = -ENOMEM; + /* [BT] To have working shadow dir in /sys/class/net, I had to change + * d_alloc(dir->d_parent ...) to d_alloc(dir ...) + */ + shadow = d_alloc(dir, &dir->d_name); + if (!shadow) + goto error; + + sd = sysfs_new_dirent(dir->d_name.name, inode->i_mode, SYSFS_SHADOW); + if (!sd) { + err = -ENOMEM; + goto error; + } + sd->s_elem.shadow_dir.tag = tag; + sd->s_elem.shadow_dir.kobj = parent_sd->s_elem.dir.kobj; + + sysfs_attach_dirent(sd, parent_sd, shadow); + + d_instantiate(shadow, igrab(inode)); + + /* Since the shadow directory is reachable make it look + * like it is actually hashed. + */ + shadow->d_hash.pprev = &shadow->d_hash.next; + shadow->d_hash.next = NULL; + shadow->d_flags &= ~DCACHE_UNHASHED; + + inc_nlink(inode); + inc_nlink(dir->d_parent->d_inode); + shadow->d_op = &sysfs_dentry_ops; + +out: + return shadow; +error: + dput(shadow); + shadow = ERR_PTR(err); + goto out; +} + +void sysfs_prune_shadow(struct dentry *shadow) +{ + struct sysfs_dirent *sd; + + if (!shadow) + return; + sd = shadow->d_fsdata; + if (sd->s_type != SYSFS_SHADOW) + return; + + if (!list_empty(&sd->s_children)) + return; + + sysfs_remove_empty_shadow(shadow); +} + +struct dentry *sysfs_resolve_for_create(struct kobject *kobj, struct dentry *dentry) +{ + const struct shadow_dir_operations *shadow_ops; + struct dentry *dir; + struct inode *inode; + + inode = dentry->d_inode; + shadow_ops = inode->i_private; + if (!shadow_ops) + dir = dentry; + else { + struct sysfs_dirent *sd; + void *tag; + sd = dentry->d_fsdata; + if (sd->s_type == SYSFS_SHADOW) + dentry = sd->s_elem.shadow_dir.kobj->dentry; + tag = shadow_ops->kobject_tag(kobj); + dir = find_shadow_dir(dentry, tag); + if (!dir) + dir = add_shadow_dir(dentry, tag); + } + return dir; +} + +struct dentry *sysfs_resolve_for_remove(struct kobject *kobj, struct dentry *dentry) +{ + const struct shadow_dir_operations *shadow_ops; + struct dentry *dir; + struct inode *inode; + + inode = dentry->d_inode; + shadow_ops = inode->i_private; + if (!shadow_ops) + dir = dentry; + else { + struct sysfs_dirent *sd; + void *tag; + sd = dentry->d_fsdata; + if (sd->s_type == SYSFS_SHADOW) + dentry = sd->s_elem.shadow_dir.kobj->dentry; + tag = find_shadow_tag(kobj); + if (IS_ERR(tag)) + dir = tag; + else + dir = find_shadow_dir(dentry, tag); + if (!dir) + dir = ERR_PTR(-ENOENT); + } + return dir; +} + +static void *sysfs_shadow_follow_link(struct dentry *dentry, struct nameidata *nd) +{ + struct sysfs_dirent *sd; + struct dentry *dest; + + sd = dentry->d_fsdata; + dest = NULL; + if (sd->s_type == SYSFS_DIR) { + const struct shadow_dir_operations *shadow_ops; + struct inode *inode; + inode = dentry->d_inode; + shadow_ops = inode->i_private; + mutex_lock(&inode->i_mutex); + dest = find_shadow_dir(dentry, shadow_ops->current_tag()); + dget(dest); + mutex_unlock(&inode->i_mutex); + } + if (!dest) + dest = dget(dentry); + dput(nd->dentry); + nd->dentry = dest; + + return NULL; +} + +static struct inode_operations sysfs_shadow_inode_operations = { + .lookup = sysfs_lookup, + .setattr = sysfs_setattr, + .follow_link = sysfs_shadow_follow_link, +}; + + +/** + * sysfs_enable_shadowing - Automatically create shadows of a directory + * @kobj: object to automatically shadow + * + * Once shadowing has been enabled on a directory the contents + * of the directory become dependent upon context. + * + * shadow_ops->current_tag() returns the context for the current + * process. + * + * shadow_ops->kobject_tag() returns the context that a given kobj + * resides in. + * + * Using those methods the sysfs code on shadowed directories + * carefully stores the files so that when we lookup files + * we get the proper answer for our context. + * + * If the context of a kobject is changed it is expected that + * the kobject will be renamed so the appopriate sysfs data structures + * can be updated. + */ +int sysfs_enable_shadowing(struct kobject *kobj, + const struct shadow_dir_operations *shadow_ops) +{ + struct sysfs_dirent *sd; + struct dentry *dentry; + struct inode *inode; + int err; + + dentry = kobj->dentry; + inode = dentry->d_inode; + + mutex_lock(&inode->i_mutex); + /* We can only enable shadowing on empty directories + * where shadowing is not already enabled. + */ + sd = dentry->d_fsdata; + err = -EINVAL; + if (!inode->i_private && list_empty(&sd->s_children)) { + inode->i_private = (void *)shadow_ops; + inode->i_op = &sysfs_shadow_inode_operations; + err = 0; + } + mutex_unlock(&inode->i_mutex); + return err; +} +
_______________________________________________ Containers mailing list Containers@xxxxxxxxxxxxxxxxxxxxxxxxxx https://lists.linux-foundation.org/mailman/listinfo/containers