| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * dir.c - Operations for sysfs directories. | 
|  | 3 | */ | 
|  | 4 |  | 
|  | 5 | #undef DEBUG | 
|  | 6 |  | 
|  | 7 | #include <linux/fs.h> | 
|  | 8 | #include <linux/mount.h> | 
|  | 9 | #include <linux/module.h> | 
|  | 10 | #include <linux/kobject.h> | 
| Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 11 | #include <linux/namei.h> | 
| Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 12 | #include <linux/idr.h> | 
| Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 13 | #include <asm/semaphore.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "sysfs.h" | 
|  | 15 |  | 
|  | 16 | DECLARE_RWSEM(sysfs_rename_sem); | 
| Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 17 | spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  | 
| Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 19 | static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED; | 
|  | 20 | static DEFINE_IDA(sysfs_ino_ida); | 
|  | 21 |  | 
|  | 22 | int sysfs_alloc_ino(ino_t *pino) | 
|  | 23 | { | 
|  | 24 | int ino, rc; | 
|  | 25 |  | 
|  | 26 | retry: | 
|  | 27 | spin_lock(&sysfs_ino_lock); | 
|  | 28 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); | 
|  | 29 | spin_unlock(&sysfs_ino_lock); | 
|  | 30 |  | 
|  | 31 | if (rc == -EAGAIN) { | 
|  | 32 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) | 
|  | 33 | goto retry; | 
|  | 34 | rc = -ENOMEM; | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | *pino = ino; | 
|  | 38 | return rc; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | static void sysfs_free_ino(ino_t ino) | 
|  | 42 | { | 
|  | 43 | spin_lock(&sysfs_ino_lock); | 
|  | 44 | ida_remove(&sysfs_ino_ida, ino); | 
|  | 45 | spin_unlock(&sysfs_ino_lock); | 
|  | 46 | } | 
|  | 47 |  | 
| Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 48 | void release_sysfs_dirent(struct sysfs_dirent * sd) | 
|  | 49 | { | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 50 | struct sysfs_dirent *parent_sd; | 
|  | 51 |  | 
|  | 52 | repeat: | 
|  | 53 | parent_sd = sd->s_parent; | 
|  | 54 |  | 
| Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 55 | if (sd->s_type & SYSFS_KOBJ_LINK) { | 
|  | 56 | struct sysfs_symlink * sl = sd->s_element; | 
|  | 57 | kfree(sl->link_name); | 
|  | 58 | kobject_put(sl->target_kobj); | 
|  | 59 | kfree(sl); | 
|  | 60 | } | 
|  | 61 | kfree(sd->s_iattr); | 
| Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 62 | sysfs_free_ino(sd->s_ino); | 
| Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 63 | kmem_cache_free(sysfs_dir_cachep, sd); | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 64 |  | 
|  | 65 | sd = parent_sd; | 
|  | 66 | if (sd && atomic_dec_and_test(&sd->s_count)) | 
|  | 67 | goto repeat; | 
| Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) | 
|  | 71 | { | 
|  | 72 | struct sysfs_dirent * sd = dentry->d_fsdata; | 
|  | 73 |  | 
|  | 74 | if (sd) { | 
| Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 75 | /* sd->s_dentry is protected with sysfs_lock.  This | 
|  | 76 | * allows sysfs_drop_dentry() to dereference it. | 
|  | 77 | */ | 
|  | 78 | spin_lock(&sysfs_lock); | 
|  | 79 |  | 
|  | 80 | /* The dentry might have been deleted or another | 
|  | 81 | * lookup could have happened updating sd->s_dentry to | 
|  | 82 | * point the new dentry.  Ignore if it isn't pointing | 
|  | 83 | * to this dentry. | 
|  | 84 | */ | 
|  | 85 | if (sd->s_dentry == dentry) | 
|  | 86 | sd->s_dentry = NULL; | 
|  | 87 | spin_unlock(&sysfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | sysfs_put(sd); | 
|  | 89 | } | 
|  | 90 | iput(inode); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | static struct dentry_operations sysfs_dentry_ops = { | 
|  | 94 | .d_iput		= sysfs_d_iput, | 
|  | 95 | }; | 
|  | 96 |  | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 97 | struct sysfs_dirent *sysfs_new_dirent(void *element, umode_t mode, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { | 
|  | 99 | struct sysfs_dirent * sd; | 
|  | 100 |  | 
| Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 101 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (!sd) | 
|  | 103 | return NULL; | 
|  | 104 |  | 
| Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 105 | if (sysfs_alloc_ino(&sd->s_ino)) { | 
|  | 106 | kmem_cache_free(sysfs_dir_cachep, sd); | 
|  | 107 | return NULL; | 
|  | 108 | } | 
|  | 109 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | atomic_set(&sd->s_count, 1); | 
| Juha Yrjölä | eea3f89 | 2006-08-03 19:06:25 +0300 | [diff] [blame] | 111 | atomic_set(&sd->s_event, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | INIT_LIST_HEAD(&sd->s_children); | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 113 | INIT_LIST_HEAD(&sd->s_sibling); | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 114 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | sd->s_element = element; | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 116 | sd->s_mode = mode; | 
|  | 117 | sd->s_type = type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | return sd; | 
|  | 120 | } | 
|  | 121 |  | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 122 | void sysfs_attach_dirent(struct sysfs_dirent *sd, | 
|  | 123 | struct sysfs_dirent *parent_sd, struct dentry *dentry) | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 124 | { | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 125 | if (dentry) { | 
|  | 126 | sd->s_dentry = dentry; | 
|  | 127 | dentry->d_fsdata = sysfs_get(sd); | 
|  | 128 | dentry->d_op = &sysfs_dentry_ops; | 
|  | 129 | } | 
|  | 130 |  | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 131 | if (parent_sd) { | 
|  | 132 | sd->s_parent = sysfs_get(parent_sd); | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 133 | list_add(&sd->s_sibling, &parent_sd->s_children); | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 134 | } | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Martin Waitz | a580290 | 2006-04-02 13:59:55 +0200 | [diff] [blame] | 137 | /* | 
| Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 138 | * | 
|  | 139 | * Return -EEXIST if there is already a sysfs element with the same name for | 
|  | 140 | * the same parent. | 
|  | 141 | * | 
|  | 142 | * called with parent inode's i_mutex held | 
|  | 143 | */ | 
|  | 144 | int sysfs_dirent_exist(struct sysfs_dirent *parent_sd, | 
|  | 145 | const unsigned char *new) | 
|  | 146 | { | 
|  | 147 | struct sysfs_dirent * sd; | 
|  | 148 |  | 
|  | 149 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { | 
|  | 150 | if (sd->s_element) { | 
|  | 151 | const unsigned char *existing = sysfs_get_name(sd); | 
|  | 152 | if (strcmp(existing, new)) | 
|  | 153 | continue; | 
|  | 154 | else | 
|  | 155 | return -EEXIST; | 
|  | 156 | } | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | return 0; | 
|  | 160 | } | 
|  | 161 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | static int init_dir(struct inode * inode) | 
|  | 163 | { | 
|  | 164 | inode->i_op = &sysfs_dir_inode_operations; | 
|  | 165 | inode->i_fop = &sysfs_dir_operations; | 
|  | 166 |  | 
|  | 167 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ | 
| Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 168 | inc_nlink(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return 0; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | static int init_file(struct inode * inode) | 
|  | 173 | { | 
|  | 174 | inode->i_size = PAGE_SIZE; | 
|  | 175 | inode->i_fop = &sysfs_file_operations; | 
|  | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | static int init_symlink(struct inode * inode) | 
|  | 180 | { | 
|  | 181 | inode->i_op = &sysfs_symlink_inode_operations; | 
|  | 182 | return 0; | 
|  | 183 | } | 
|  | 184 |  | 
| Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 185 | static int create_dir(struct kobject *kobj, struct dentry *parent, | 
|  | 186 | const char *name, struct dentry **p_dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { | 
|  | 188 | int error; | 
|  | 189 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; | 
| Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 190 | struct dentry *dentry; | 
|  | 191 | struct sysfs_dirent *sd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 |  | 
| Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 193 | mutex_lock(&parent->d_inode->i_mutex); | 
|  | 194 |  | 
|  | 195 | dentry = lookup_one_len(name, parent, strlen(name)); | 
|  | 196 | if (IS_ERR(dentry)) { | 
|  | 197 | error = PTR_ERR(dentry); | 
|  | 198 | goto out_unlock; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | error = -EEXIST; | 
|  | 202 | if (sysfs_dirent_exist(parent->d_fsdata, name)) | 
|  | 203 | goto out_dput; | 
|  | 204 |  | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 205 | error = -ENOMEM; | 
|  | 206 | sd = sysfs_new_dirent(kobj, mode, SYSFS_DIR); | 
|  | 207 | if (!sd) | 
| Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 208 | goto out_drop; | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 209 | sysfs_attach_dirent(sd, parent->d_fsdata, dentry); | 
| Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 210 |  | 
|  | 211 | error = sysfs_create(dentry, mode, init_dir); | 
|  | 212 | if (error) | 
|  | 213 | goto out_sput; | 
|  | 214 |  | 
|  | 215 | inc_nlink(parent->d_inode); | 
|  | 216 | dentry->d_op = &sysfs_dentry_ops; | 
|  | 217 | d_rehash(dentry); | 
|  | 218 |  | 
|  | 219 | *p_dentry = dentry; | 
|  | 220 | error = 0; | 
|  | 221 | goto out_dput; | 
|  | 222 |  | 
|  | 223 | out_sput: | 
| Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 224 | list_del_init(&sd->s_sibling); | 
|  | 225 | sysfs_put(sd); | 
|  | 226 | out_drop: | 
|  | 227 | d_drop(dentry); | 
|  | 228 | out_dput: | 
|  | 229 | dput(dentry); | 
|  | 230 | out_unlock: | 
|  | 231 | mutex_unlock(&parent->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return error; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 |  | 
|  | 236 | int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d) | 
|  | 237 | { | 
|  | 238 | return create_dir(k,k->dentry,n,d); | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | /** | 
|  | 242 | *	sysfs_create_dir - create a directory for an object. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | *	@kobj:		object we're creating directory for. | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 244 | *	@shadow_parent:	parent parent object. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | */ | 
|  | 246 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 247 | int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { | 
|  | 249 | struct dentry * dentry = NULL; | 
|  | 250 | struct dentry * parent; | 
|  | 251 | int error = 0; | 
|  | 252 |  | 
|  | 253 | BUG_ON(!kobj); | 
|  | 254 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 255 | if (shadow_parent) | 
|  | 256 | parent = shadow_parent; | 
|  | 257 | else if (kobj->parent) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | parent = kobj->parent->dentry; | 
|  | 259 | else if (sysfs_mount && sysfs_mount->mnt_sb) | 
|  | 260 | parent = sysfs_mount->mnt_sb->s_root; | 
|  | 261 | else | 
|  | 262 | return -EFAULT; | 
|  | 263 |  | 
|  | 264 | error = create_dir(kobj,parent,kobject_name(kobj),&dentry); | 
|  | 265 | if (!error) | 
|  | 266 | kobj->dentry = dentry; | 
|  | 267 | return error; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | /* attaches attribute's sysfs_dirent to the dentry corresponding to the | 
|  | 271 | * attribute file | 
|  | 272 | */ | 
|  | 273 | static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry) | 
|  | 274 | { | 
|  | 275 | struct attribute * attr = NULL; | 
|  | 276 | struct bin_attribute * bin_attr = NULL; | 
|  | 277 | int (* init) (struct inode *) = NULL; | 
|  | 278 | int error = 0; | 
|  | 279 |  | 
|  | 280 | if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) { | 
|  | 281 | bin_attr = sd->s_element; | 
|  | 282 | attr = &bin_attr->attr; | 
|  | 283 | } else { | 
|  | 284 | attr = sd->s_element; | 
|  | 285 | init = init_file; | 
|  | 286 | } | 
|  | 287 |  | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 288 | dentry->d_fsdata = sysfs_get(sd); | 
| Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 289 | /* protect sd->s_dentry against sysfs_d_iput */ | 
|  | 290 | spin_lock(&sysfs_lock); | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 291 | sd->s_dentry = dentry; | 
| Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 292 | spin_unlock(&sysfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init); | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 294 | if (error) { | 
|  | 295 | sysfs_put(sd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return error; | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 297 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 |  | 
|  | 299 | if (bin_attr) { | 
|  | 300 | dentry->d_inode->i_size = bin_attr->size; | 
|  | 301 | dentry->d_inode->i_fop = &bin_fops; | 
|  | 302 | } | 
|  | 303 | dentry->d_op = &sysfs_dentry_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | d_rehash(dentry); | 
|  | 305 |  | 
|  | 306 | return 0; | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry) | 
|  | 310 | { | 
|  | 311 | int err = 0; | 
|  | 312 |  | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 313 | dentry->d_fsdata = sysfs_get(sd); | 
| Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 314 | /* protect sd->s_dentry against sysfs_d_iput */ | 
|  | 315 | spin_lock(&sysfs_lock); | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 316 | sd->s_dentry = dentry; | 
| Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 317 | spin_unlock(&sysfs_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink); | 
|  | 319 | if (!err) { | 
|  | 320 | dentry->d_op = &sysfs_dentry_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | d_rehash(dentry); | 
| Maneesh Soni | 6fa5c82 | 2005-05-31 10:38:12 +0530 | [diff] [blame] | 322 | } else | 
|  | 323 | sysfs_put(sd); | 
|  | 324 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return err; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, | 
|  | 329 | struct nameidata *nd) | 
|  | 330 | { | 
|  | 331 | struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; | 
|  | 332 | struct sysfs_dirent * sd; | 
|  | 333 | int err = 0; | 
|  | 334 |  | 
|  | 335 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { | 
|  | 336 | if (sd->s_type & SYSFS_NOT_PINNED) { | 
|  | 337 | const unsigned char * name = sysfs_get_name(sd); | 
|  | 338 |  | 
|  | 339 | if (strcmp(name, dentry->d_name.name)) | 
|  | 340 | continue; | 
|  | 341 |  | 
|  | 342 | if (sd->s_type & SYSFS_KOBJ_LINK) | 
|  | 343 | err = sysfs_attach_link(sd, dentry); | 
|  | 344 | else | 
|  | 345 | err = sysfs_attach_attr(sd, dentry); | 
|  | 346 | break; | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | return ERR_PTR(err); | 
|  | 351 | } | 
|  | 352 |  | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 353 | const struct inode_operations sysfs_dir_inode_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | .lookup		= sysfs_lookup, | 
| Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 355 | .setattr	= sysfs_setattr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | }; | 
|  | 357 |  | 
|  | 358 | static void remove_dir(struct dentry * d) | 
|  | 359 | { | 
|  | 360 | struct dentry * parent = dget(d->d_parent); | 
|  | 361 | struct sysfs_dirent * sd; | 
|  | 362 |  | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 363 | mutex_lock(&parent->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | d_delete(d); | 
|  | 365 | sd = d->d_fsdata; | 
|  | 366 | list_del_init(&sd->s_sibling); | 
|  | 367 | sysfs_put(sd); | 
|  | 368 | if (d->d_inode) | 
|  | 369 | simple_rmdir(parent->d_inode,d); | 
|  | 370 |  | 
|  | 371 | pr_debug(" o %s removing done (%d)\n",d->d_name.name, | 
|  | 372 | atomic_read(&d->d_count)); | 
|  | 373 |  | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 374 | mutex_unlock(&parent->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | dput(parent); | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | void sysfs_remove_subdir(struct dentry * d) | 
|  | 379 | { | 
|  | 380 | remove_dir(d); | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 384 | static void __sysfs_remove_dir(struct dentry *dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | struct sysfs_dirent * parent_sd; | 
|  | 387 | struct sysfs_dirent * sd, * tmp; | 
|  | 388 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 389 | dget(dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (!dentry) | 
|  | 391 | return; | 
|  | 392 |  | 
|  | 393 | pr_debug("sysfs %s: removing dir\n",dentry->d_name.name); | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 394 | mutex_lock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | parent_sd = dentry->d_fsdata; | 
|  | 396 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { | 
|  | 397 | if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED)) | 
|  | 398 | continue; | 
|  | 399 | list_del_init(&sd->s_sibling); | 
|  | 400 | sysfs_drop_dentry(sd, dentry); | 
|  | 401 | sysfs_put(sd); | 
|  | 402 | } | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 403 | mutex_unlock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 |  | 
|  | 405 | remove_dir(dentry); | 
|  | 406 | /** | 
|  | 407 | * Drop reference from dget() on entrance. | 
|  | 408 | */ | 
|  | 409 | dput(dentry); | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 410 | } | 
|  | 411 |  | 
|  | 412 | /** | 
|  | 413 | *	sysfs_remove_dir - remove an object's directory. | 
|  | 414 | *	@kobj:	object. | 
|  | 415 | * | 
|  | 416 | *	The only thing special about this is that we remove any files in | 
|  | 417 | *	the directory before we remove the directory, and we've inlined | 
|  | 418 | *	what used to be sysfs_rmdir() below, instead of calling separately. | 
|  | 419 | */ | 
|  | 420 |  | 
|  | 421 | void sysfs_remove_dir(struct kobject * kobj) | 
|  | 422 | { | 
|  | 423 | __sysfs_remove_dir(kobj->dentry); | 
| Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 424 | kobj->dentry = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } | 
|  | 426 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 427 | int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent, | 
|  | 428 | const char *new_name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { | 
| Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 430 | int error; | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 431 | struct dentry * new_dentry; | 
| Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 432 | struct sysfs_dirent *sd, *parent_sd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 434 | if (!new_parent) | 
|  | 435 | return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 |  | 
|  | 437 | down_write(&sysfs_rename_sem); | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 438 | mutex_lock(&new_parent->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 440 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); | 
| Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 441 | if (IS_ERR(new_dentry)) { | 
|  | 442 | error = PTR_ERR(new_dentry); | 
|  | 443 | goto out_unlock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } | 
| Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 445 |  | 
|  | 446 | /* By allowing two different directories with the same | 
|  | 447 | * d_parent we allow this routine to move between different | 
|  | 448 | * shadows of the same directory | 
|  | 449 | */ | 
|  | 450 | error = -EINVAL; | 
|  | 451 | if (kobj->dentry->d_parent->d_inode != new_parent->d_inode || | 
|  | 452 | new_dentry->d_parent->d_inode != new_parent->d_inode || | 
|  | 453 | new_dentry == kobj->dentry) | 
|  | 454 | goto out_dput; | 
|  | 455 |  | 
|  | 456 | error = -EEXIST; | 
|  | 457 | if (new_dentry->d_inode) | 
|  | 458 | goto out_dput; | 
|  | 459 |  | 
|  | 460 | error = kobject_set_name(kobj, "%s", new_name); | 
|  | 461 | if (error) | 
|  | 462 | goto out_drop; | 
|  | 463 |  | 
|  | 464 | d_add(new_dentry, NULL); | 
|  | 465 | d_move(kobj->dentry, new_dentry); | 
|  | 466 |  | 
|  | 467 | sd = kobj->dentry->d_fsdata; | 
|  | 468 | parent_sd = new_parent->d_fsdata; | 
|  | 469 |  | 
|  | 470 | list_del_init(&sd->s_sibling); | 
|  | 471 | list_add(&sd->s_sibling, &parent_sd->s_children); | 
|  | 472 |  | 
|  | 473 | error = 0; | 
|  | 474 | goto out_unlock; | 
|  | 475 |  | 
|  | 476 | out_drop: | 
|  | 477 | d_drop(new_dentry); | 
|  | 478 | out_dput: | 
|  | 479 | dput(new_dentry); | 
|  | 480 | out_unlock: | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 481 | mutex_unlock(&new_parent->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | up_write(&sysfs_rename_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return error; | 
|  | 484 | } | 
|  | 485 |  | 
| Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 486 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) | 
|  | 487 | { | 
|  | 488 | struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; | 
|  | 489 | struct sysfs_dirent *new_parent_sd, *sd; | 
|  | 490 | int error; | 
|  | 491 |  | 
| Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 492 | old_parent_dentry = kobj->parent ? | 
|  | 493 | kobj->parent->dentry : sysfs_mount->mnt_sb->s_root; | 
| Cornelia Huck | c744aea | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 494 | new_parent_dentry = new_parent ? | 
|  | 495 | new_parent->dentry : sysfs_mount->mnt_sb->s_root; | 
| Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 496 |  | 
| Mark Lord | 0de1517 | 2007-03-06 01:42:03 -0800 | [diff] [blame] | 497 | if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) | 
|  | 498 | return 0;	/* nothing to move */ | 
| Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 499 | again: | 
|  | 500 | mutex_lock(&old_parent_dentry->d_inode->i_mutex); | 
|  | 501 | if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { | 
|  | 502 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | 
|  | 503 | goto again; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | new_parent_sd = new_parent_dentry->d_fsdata; | 
|  | 507 | sd = kobj->dentry->d_fsdata; | 
|  | 508 |  | 
|  | 509 | new_dentry = lookup_one_len(kobj->name, new_parent_dentry, | 
|  | 510 | strlen(kobj->name)); | 
|  | 511 | if (IS_ERR(new_dentry)) { | 
|  | 512 | error = PTR_ERR(new_dentry); | 
|  | 513 | goto out; | 
|  | 514 | } else | 
|  | 515 | error = 0; | 
|  | 516 | d_add(new_dentry, NULL); | 
|  | 517 | d_move(kobj->dentry, new_dentry); | 
|  | 518 | dput(new_dentry); | 
|  | 519 |  | 
|  | 520 | /* Remove from old parent's list and insert into new parent's list. */ | 
|  | 521 | list_del_init(&sd->s_sibling); | 
|  | 522 | list_add(&sd->s_sibling, &new_parent_sd->s_children); | 
|  | 523 |  | 
|  | 524 | out: | 
|  | 525 | mutex_unlock(&new_parent_dentry->d_inode->i_mutex); | 
|  | 526 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | 
|  | 527 |  | 
|  | 528 | return error; | 
|  | 529 | } | 
|  | 530 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | static int sysfs_dir_open(struct inode *inode, struct file *file) | 
|  | 532 | { | 
| Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 533 | struct dentry * dentry = file->f_path.dentry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 535 | struct sysfs_dirent * sd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 |  | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 537 | mutex_lock(&dentry->d_inode->i_mutex); | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 538 | sd = sysfs_new_dirent(NULL, 0, 0); | 
|  | 539 | if (sd) | 
|  | 540 | sysfs_attach_dirent(sd, parent_sd, NULL); | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 541 | mutex_unlock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 |  | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 543 | file->private_data = sd; | 
|  | 544 | return sd ? 0 : -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } | 
|  | 546 |  | 
|  | 547 | static int sysfs_dir_close(struct inode *inode, struct file *file) | 
|  | 548 | { | 
| Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 549 | struct dentry * dentry = file->f_path.dentry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | struct sysfs_dirent * cursor = file->private_data; | 
|  | 551 |  | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 552 | mutex_lock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | list_del_init(&cursor->s_sibling); | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 554 | mutex_unlock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 |  | 
|  | 556 | release_sysfs_dirent(cursor); | 
|  | 557 |  | 
|  | 558 | return 0; | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | /* Relationship between s_mode and the DT_xxx types */ | 
|  | 562 | static inline unsigned char dt_type(struct sysfs_dirent *sd) | 
|  | 563 | { | 
|  | 564 | return (sd->s_mode >> 12) & 15; | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) | 
|  | 568 | { | 
| Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 569 | struct dentry *dentry = filp->f_path.dentry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; | 
|  | 571 | struct sysfs_dirent *cursor = filp->private_data; | 
|  | 572 | struct list_head *p, *q = &cursor->s_sibling; | 
|  | 573 | ino_t ino; | 
|  | 574 | int i = filp->f_pos; | 
|  | 575 |  | 
|  | 576 | switch (i) { | 
|  | 577 | case 0: | 
| Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 578 | ino = parent_sd->s_ino; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) | 
|  | 580 | break; | 
|  | 581 | filp->f_pos++; | 
|  | 582 | i++; | 
|  | 583 | /* fallthrough */ | 
|  | 584 | case 1: | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 585 | if (parent_sd->s_parent) | 
|  | 586 | ino = parent_sd->s_parent->s_ino; | 
|  | 587 | else | 
|  | 588 | ino = parent_sd->s_ino; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) | 
|  | 590 | break; | 
|  | 591 | filp->f_pos++; | 
|  | 592 | i++; | 
|  | 593 | /* fallthrough */ | 
|  | 594 | default: | 
| Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 595 | if (filp->f_pos == 2) | 
|  | 596 | list_move(q, &parent_sd->s_children); | 
|  | 597 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | for (p=q->next; p!= &parent_sd->s_children; p=p->next) { | 
|  | 599 | struct sysfs_dirent *next; | 
|  | 600 | const char * name; | 
|  | 601 | int len; | 
|  | 602 |  | 
|  | 603 | next = list_entry(p, struct sysfs_dirent, | 
|  | 604 | s_sibling); | 
|  | 605 | if (!next->s_element) | 
|  | 606 | continue; | 
|  | 607 |  | 
|  | 608 | name = sysfs_get_name(next); | 
|  | 609 | len = strlen(name); | 
| Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 610 | ino = next->s_ino; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 |  | 
|  | 612 | if (filldir(dirent, name, len, filp->f_pos, ino, | 
|  | 613 | dt_type(next)) < 0) | 
|  | 614 | return 0; | 
|  | 615 |  | 
| Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 616 | list_move(q, p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | p = q; | 
|  | 618 | filp->f_pos++; | 
|  | 619 | } | 
|  | 620 | } | 
|  | 621 | return 0; | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) | 
|  | 625 | { | 
| Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 626 | struct dentry * dentry = file->f_path.dentry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 |  | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 628 | mutex_lock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | switch (origin) { | 
|  | 630 | case 1: | 
|  | 631 | offset += file->f_pos; | 
|  | 632 | case 0: | 
|  | 633 | if (offset >= 0) | 
|  | 634 | break; | 
|  | 635 | default: | 
| Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 636 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | return -EINVAL; | 
|  | 638 | } | 
|  | 639 | if (offset != file->f_pos) { | 
|  | 640 | file->f_pos = offset; | 
|  | 641 | if (file->f_pos >= 2) { | 
|  | 642 | struct sysfs_dirent *sd = dentry->d_fsdata; | 
|  | 643 | struct sysfs_dirent *cursor = file->private_data; | 
|  | 644 | struct list_head *p; | 
|  | 645 | loff_t n = file->f_pos - 2; | 
|  | 646 |  | 
|  | 647 | list_del(&cursor->s_sibling); | 
|  | 648 | p = sd->s_children.next; | 
|  | 649 | while (n && p != &sd->s_children) { | 
|  | 650 | struct sysfs_dirent *next; | 
|  | 651 | next = list_entry(p, struct sysfs_dirent, | 
|  | 652 | s_sibling); | 
|  | 653 | if (next->s_element) | 
|  | 654 | n--; | 
|  | 655 | p = p->next; | 
|  | 656 | } | 
|  | 657 | list_add_tail(&cursor->s_sibling, p); | 
|  | 658 | } | 
|  | 659 | } | 
| Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 660 | mutex_unlock(&dentry->d_inode->i_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return offset; | 
|  | 662 | } | 
|  | 663 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 664 |  | 
|  | 665 | /** | 
|  | 666 | *	sysfs_make_shadowed_dir - Setup so a directory can be shadowed | 
|  | 667 | *	@kobj:	object we're creating shadow of. | 
|  | 668 | */ | 
|  | 669 |  | 
|  | 670 | int sysfs_make_shadowed_dir(struct kobject *kobj, | 
|  | 671 | void * (*follow_link)(struct dentry *, struct nameidata *)) | 
|  | 672 | { | 
|  | 673 | struct inode *inode; | 
|  | 674 | struct inode_operations *i_op; | 
|  | 675 |  | 
|  | 676 | inode = kobj->dentry->d_inode; | 
|  | 677 | if (inode->i_op != &sysfs_dir_inode_operations) | 
|  | 678 | return -EINVAL; | 
|  | 679 |  | 
|  | 680 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); | 
|  | 681 | if (!i_op) | 
|  | 682 | return -ENOMEM; | 
|  | 683 |  | 
|  | 684 | memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); | 
|  | 685 | i_op->follow_link = follow_link; | 
|  | 686 |  | 
|  | 687 | /* Locking of inode->i_op? | 
|  | 688 | * Since setting i_op is a single word write and they | 
|  | 689 | * are atomic we should be ok here. | 
|  | 690 | */ | 
|  | 691 | inode->i_op = i_op; | 
|  | 692 | return 0; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | /** | 
|  | 696 | *	sysfs_create_shadow_dir - create a shadow directory for an object. | 
|  | 697 | *	@kobj:	object we're creating directory for. | 
|  | 698 | * | 
|  | 699 | *	sysfs_make_shadowed_dir must already have been called on this | 
|  | 700 | *	directory. | 
|  | 701 | */ | 
|  | 702 |  | 
|  | 703 | struct dentry *sysfs_create_shadow_dir(struct kobject *kobj) | 
|  | 704 | { | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 705 | struct dentry *dir = kobj->dentry; | 
|  | 706 | struct inode *inode = dir->d_inode; | 
|  | 707 | struct dentry *parent = dir->d_parent; | 
|  | 708 | struct sysfs_dirent *parent_sd = parent->d_fsdata; | 
|  | 709 | struct dentry *shadow; | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 710 | struct sysfs_dirent *sd; | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 711 |  | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 712 | shadow = ERR_PTR(-EINVAL); | 
|  | 713 | if (!sysfs_is_shadowed_inode(inode)) | 
|  | 714 | goto out; | 
|  | 715 |  | 
|  | 716 | shadow = d_alloc(parent, &dir->d_name); | 
|  | 717 | if (!shadow) | 
|  | 718 | goto nomem; | 
|  | 719 |  | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 720 | sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR); | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 721 | if (!sd) | 
|  | 722 | goto nomem; | 
| Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame^] | 723 | /* point to parent_sd but don't attach to it */ | 
|  | 724 | sd->s_parent = sysfs_get(parent_sd); | 
| Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 725 | sysfs_attach_dirent(sd, NULL, shadow); | 
| Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 726 |  | 
|  | 727 | d_instantiate(shadow, igrab(inode)); | 
|  | 728 | inc_nlink(inode); | 
|  | 729 | inc_nlink(parent->d_inode); | 
|  | 730 | shadow->d_op = &sysfs_dentry_ops; | 
|  | 731 |  | 
|  | 732 | dget(shadow);		/* Extra count - pin the dentry in core */ | 
|  | 733 |  | 
|  | 734 | out: | 
|  | 735 | return shadow; | 
|  | 736 | nomem: | 
|  | 737 | dput(shadow); | 
|  | 738 | shadow = ERR_PTR(-ENOMEM); | 
|  | 739 | goto out; | 
|  | 740 | } | 
|  | 741 |  | 
|  | 742 | /** | 
|  | 743 | *	sysfs_remove_shadow_dir - remove an object's directory. | 
|  | 744 | *	@shadow: dentry of shadow directory | 
|  | 745 | * | 
|  | 746 | *	The only thing special about this is that we remove any files in | 
|  | 747 | *	the directory before we remove the directory, and we've inlined | 
|  | 748 | *	what used to be sysfs_rmdir() below, instead of calling separately. | 
|  | 749 | */ | 
|  | 750 |  | 
|  | 751 | void sysfs_remove_shadow_dir(struct dentry *shadow) | 
|  | 752 | { | 
|  | 753 | __sysfs_remove_dir(shadow); | 
|  | 754 | } | 
|  | 755 |  | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 756 | const struct file_operations sysfs_dir_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | .open		= sysfs_dir_open, | 
|  | 758 | .release	= sysfs_dir_close, | 
|  | 759 | .llseek		= sysfs_dir_lseek, | 
|  | 760 | .read		= generic_read_dir, | 
|  | 761 | .readdir	= sysfs_readdir, | 
|  | 762 | }; |