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