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