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