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