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