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