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