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 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 17 | DEFINE_MUTEX(sysfs_mutex); |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 18 | spinlock_t 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 | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 23 | /** |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 24 | * sysfs_link_sibling - link sysfs_dirent into sibling list |
| 25 | * @sd: sysfs_dirent of interest |
| 26 | * |
| 27 | * Link @sd into its sibling list which starts from |
| 28 | * sd->s_parent->s_children. |
| 29 | * |
| 30 | * Locking: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 31 | * mutex_lock(sysfs_mutex) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 32 | */ |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 33 | void sysfs_link_sibling(struct sysfs_dirent *sd) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 34 | { |
| 35 | struct sysfs_dirent *parent_sd = sd->s_parent; |
| 36 | |
| 37 | BUG_ON(sd->s_sibling); |
| 38 | sd->s_sibling = parent_sd->s_children; |
| 39 | parent_sd->s_children = sd; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list |
| 44 | * @sd: sysfs_dirent of interest |
| 45 | * |
| 46 | * Unlink @sd from its sibling list which starts from |
| 47 | * sd->s_parent->s_children. |
| 48 | * |
| 49 | * Locking: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 50 | * mutex_lock(sysfs_mutex) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 51 | */ |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 52 | void sysfs_unlink_sibling(struct sysfs_dirent *sd) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 53 | { |
| 54 | struct sysfs_dirent **pos; |
| 55 | |
| 56 | for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) { |
| 57 | if (*pos == sd) { |
| 58 | *pos = sd->s_sibling; |
| 59 | sd->s_sibling = NULL; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
Tejun Heo | 53e0ae9 | 2007-06-14 04:27:25 +0900 | [diff] [blame^] | 66 | * sysfs_get_dentry - get dentry for the given sysfs_dirent |
| 67 | * @sd: sysfs_dirent of interest |
| 68 | * |
| 69 | * Get dentry for @sd. Dentry is looked up if currently not |
| 70 | * present. This function climbs sysfs_dirent tree till it |
| 71 | * reaches a sysfs_dirent with valid dentry attached and descends |
| 72 | * down from there looking up dentry for each step. |
| 73 | * |
| 74 | * LOCKING: |
| 75 | * Kernel thread context (may sleep) |
| 76 | * |
| 77 | * RETURNS: |
| 78 | * Pointer to found dentry on success, ERR_PTR() value on error. |
| 79 | */ |
| 80 | struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd) |
| 81 | { |
| 82 | struct sysfs_dirent *cur; |
| 83 | struct dentry *parent_dentry, *dentry; |
| 84 | int i, depth; |
| 85 | |
| 86 | /* Find the first parent which has valid s_dentry and get the |
| 87 | * dentry. |
| 88 | */ |
| 89 | mutex_lock(&sysfs_mutex); |
| 90 | restart0: |
| 91 | spin_lock(&sysfs_assoc_lock); |
| 92 | restart1: |
| 93 | spin_lock(&dcache_lock); |
| 94 | |
| 95 | dentry = NULL; |
| 96 | depth = 0; |
| 97 | cur = sd; |
| 98 | while (!cur->s_dentry || !cur->s_dentry->d_inode) { |
| 99 | if (cur->s_flags & SYSFS_FLAG_REMOVED) { |
| 100 | dentry = ERR_PTR(-ENOENT); |
| 101 | depth = 0; |
| 102 | break; |
| 103 | } |
| 104 | cur = cur->s_parent; |
| 105 | depth++; |
| 106 | } |
| 107 | if (!IS_ERR(dentry)) |
| 108 | dentry = dget_locked(cur->s_dentry); |
| 109 | |
| 110 | spin_unlock(&dcache_lock); |
| 111 | spin_unlock(&sysfs_assoc_lock); |
| 112 | |
| 113 | /* from the found dentry, look up depth times */ |
| 114 | while (depth--) { |
| 115 | /* find and get depth'th ancestor */ |
| 116 | for (cur = sd, i = 0; cur && i < depth; i++) |
| 117 | cur = cur->s_parent; |
| 118 | |
| 119 | /* This can happen if tree structure was modified due |
| 120 | * to move/rename. Restart. |
| 121 | */ |
| 122 | if (i != depth) { |
| 123 | dput(dentry); |
| 124 | goto restart0; |
| 125 | } |
| 126 | |
| 127 | sysfs_get(cur); |
| 128 | |
| 129 | mutex_unlock(&sysfs_mutex); |
| 130 | |
| 131 | /* look it up */ |
| 132 | parent_dentry = dentry; |
| 133 | dentry = lookup_one_len_kern(cur->s_name, parent_dentry, |
| 134 | strlen(cur->s_name)); |
| 135 | dput(parent_dentry); |
| 136 | |
| 137 | if (IS_ERR(dentry)) { |
| 138 | sysfs_put(cur); |
| 139 | return dentry; |
| 140 | } |
| 141 | |
| 142 | mutex_lock(&sysfs_mutex); |
| 143 | spin_lock(&sysfs_assoc_lock); |
| 144 | |
| 145 | /* This, again, can happen if tree structure has |
| 146 | * changed and we looked up the wrong thing. Restart. |
| 147 | */ |
| 148 | if (cur->s_dentry != dentry) { |
| 149 | dput(dentry); |
| 150 | sysfs_put(cur); |
| 151 | goto restart1; |
| 152 | } |
| 153 | |
| 154 | spin_unlock(&sysfs_assoc_lock); |
| 155 | |
| 156 | sysfs_put(cur); |
| 157 | } |
| 158 | |
| 159 | mutex_unlock(&sysfs_mutex); |
| 160 | return dentry; |
| 161 | } |
| 162 | |
| 163 | /** |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 164 | * sysfs_get_active - get an active reference to sysfs_dirent |
| 165 | * @sd: sysfs_dirent to get an active reference to |
| 166 | * |
| 167 | * Get an active reference of @sd. This function is noop if @sd |
| 168 | * is NULL. |
| 169 | * |
| 170 | * RETURNS: |
| 171 | * Pointer to @sd on success, NULL on failure. |
| 172 | */ |
| 173 | struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) |
| 174 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 175 | if (unlikely(!sd)) |
| 176 | return NULL; |
| 177 | |
| 178 | while (1) { |
| 179 | int v, t; |
| 180 | |
| 181 | v = atomic_read(&sd->s_active); |
| 182 | if (unlikely(v < 0)) |
| 183 | return NULL; |
| 184 | |
| 185 | t = atomic_cmpxchg(&sd->s_active, v, v + 1); |
| 186 | if (likely(t == v)) |
| 187 | return sd; |
| 188 | if (t < 0) |
| 189 | return NULL; |
| 190 | |
| 191 | cpu_relax(); |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 192 | } |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * sysfs_put_active - put an active reference to sysfs_dirent |
| 197 | * @sd: sysfs_dirent to put an active reference to |
| 198 | * |
| 199 | * Put an active reference to @sd. This function is noop if @sd |
| 200 | * is NULL. |
| 201 | */ |
| 202 | void sysfs_put_active(struct sysfs_dirent *sd) |
| 203 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 204 | struct completion *cmpl; |
| 205 | int v; |
| 206 | |
| 207 | if (unlikely(!sd)) |
| 208 | return; |
| 209 | |
| 210 | v = atomic_dec_return(&sd->s_active); |
| 211 | if (likely(v != SD_DEACTIVATED_BIAS)) |
| 212 | return; |
| 213 | |
| 214 | /* atomic_dec_return() is a mb(), we'll always see the updated |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 215 | * sd->s_sibling. |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 216 | */ |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 217 | cmpl = (void *)sd->s_sibling; |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 218 | complete(cmpl); |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
| 222 | * sysfs_get_active_two - get active references to sysfs_dirent and parent |
| 223 | * @sd: sysfs_dirent of interest |
| 224 | * |
| 225 | * Get active reference to @sd and its parent. Parent's active |
| 226 | * reference is grabbed first. This function is noop if @sd is |
| 227 | * NULL. |
| 228 | * |
| 229 | * RETURNS: |
| 230 | * Pointer to @sd on success, NULL on failure. |
| 231 | */ |
| 232 | struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd) |
| 233 | { |
| 234 | if (sd) { |
| 235 | if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent))) |
| 236 | return NULL; |
| 237 | if (unlikely(!sysfs_get_active(sd))) { |
| 238 | sysfs_put_active(sd->s_parent); |
| 239 | return NULL; |
| 240 | } |
| 241 | } |
| 242 | return sd; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * sysfs_put_active_two - put active references to sysfs_dirent and parent |
| 247 | * @sd: sysfs_dirent of interest |
| 248 | * |
| 249 | * Put active references to @sd and its parent. This function is |
| 250 | * noop if @sd is NULL. |
| 251 | */ |
| 252 | void sysfs_put_active_two(struct sysfs_dirent *sd) |
| 253 | { |
| 254 | if (sd) { |
| 255 | sysfs_put_active(sd); |
| 256 | sysfs_put_active(sd->s_parent); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * sysfs_deactivate - deactivate sysfs_dirent |
| 262 | * @sd: sysfs_dirent to deactivate |
| 263 | * |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 264 | * Deny new active references and drain existing ones. |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 265 | */ |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 266 | static void sysfs_deactivate(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 267 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 268 | DECLARE_COMPLETION_ONSTACK(wait); |
| 269 | int v; |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 270 | |
Tejun Heo | 380e6fb | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 271 | BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED)); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 272 | sd->s_sibling = (void *)&wait; |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 273 | |
| 274 | /* atomic_add_return() is a mb(), put_active() will always see |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 275 | * the updated sd->s_sibling. |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 276 | */ |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 277 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); |
| 278 | |
| 279 | if (v != SD_DEACTIVATED_BIAS) |
| 280 | wait_for_completion(&wait); |
| 281 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 282 | sd->s_sibling = NULL; |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Tejun Heo | 42b37df | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 285 | static int sysfs_alloc_ino(ino_t *pino) |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 286 | { |
| 287 | int ino, rc; |
| 288 | |
| 289 | retry: |
| 290 | spin_lock(&sysfs_ino_lock); |
| 291 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); |
| 292 | spin_unlock(&sysfs_ino_lock); |
| 293 | |
| 294 | if (rc == -EAGAIN) { |
| 295 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) |
| 296 | goto retry; |
| 297 | rc = -ENOMEM; |
| 298 | } |
| 299 | |
| 300 | *pino = ino; |
| 301 | return rc; |
| 302 | } |
| 303 | |
| 304 | static void sysfs_free_ino(ino_t ino) |
| 305 | { |
| 306 | spin_lock(&sysfs_ino_lock); |
| 307 | ida_remove(&sysfs_ino_ida, ino); |
| 308 | spin_unlock(&sysfs_ino_lock); |
| 309 | } |
| 310 | |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 311 | void release_sysfs_dirent(struct sysfs_dirent * sd) |
| 312 | { |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 313 | struct sysfs_dirent *parent_sd; |
| 314 | |
| 315 | repeat: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 316 | /* Moving/renaming is always done while holding reference. |
| 317 | * sd->s_parent won't change beneath us. |
| 318 | */ |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 319 | parent_sd = sd->s_parent; |
| 320 | |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 321 | if (sysfs_type(sd) == SYSFS_KOBJ_LINK) |
Tejun Heo | 2b29ac2 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 322 | sysfs_put(sd->s_elem.symlink.target_sd); |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 323 | if (sysfs_type(sd) & SYSFS_COPY_NAME) |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 324 | kfree(sd->s_name); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 325 | kfree(sd->s_iattr); |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 326 | sysfs_free_ino(sd->s_ino); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 327 | kmem_cache_free(sysfs_dir_cachep, sd); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 328 | |
| 329 | sd = parent_sd; |
| 330 | if (sd && atomic_dec_and_test(&sd->s_count)) |
| 331 | goto repeat; |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) |
| 335 | { |
| 336 | struct sysfs_dirent * sd = dentry->d_fsdata; |
| 337 | |
| 338 | if (sd) { |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 339 | /* sd->s_dentry is protected with sysfs_assoc_lock. |
| 340 | * This allows sysfs_drop_dentry() to dereference it. |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 341 | */ |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 342 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 343 | |
| 344 | /* The dentry might have been deleted or another |
| 345 | * lookup could have happened updating sd->s_dentry to |
| 346 | * point the new dentry. Ignore if it isn't pointing |
| 347 | * to this dentry. |
| 348 | */ |
| 349 | if (sd->s_dentry == dentry) |
| 350 | sd->s_dentry = NULL; |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 351 | spin_unlock(&sysfs_assoc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | sysfs_put(sd); |
| 353 | } |
| 354 | iput(inode); |
| 355 | } |
| 356 | |
| 357 | static struct dentry_operations sysfs_dentry_ops = { |
| 358 | .d_iput = sysfs_d_iput, |
| 359 | }; |
| 360 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 361 | 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] | 362 | { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 363 | char *dup_name = NULL; |
| 364 | struct sysfs_dirent *sd = NULL; |
| 365 | |
| 366 | if (type & SYSFS_COPY_NAME) { |
| 367 | name = dup_name = kstrdup(name, GFP_KERNEL); |
| 368 | if (!name) |
| 369 | goto err_out; |
| 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 372 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (!sd) |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 374 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 376 | if (sysfs_alloc_ino(&sd->s_ino)) |
| 377 | goto err_out; |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | atomic_set(&sd->s_count, 1); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 380 | atomic_set(&sd->s_active, 0); |
Juha Yrjölä | eea3f89 | 2006-08-03 19:06:25 +0300 | [diff] [blame] | 381 | atomic_set(&sd->s_event, 1); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 382 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 383 | sd->s_name = name; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 384 | sd->s_mode = mode; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 385 | sd->s_flags = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | return sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 388 | |
| 389 | err_out: |
| 390 | kfree(dup_name); |
| 391 | kmem_cache_free(sysfs_dir_cachep, sd); |
| 392 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 395 | /** |
| 396 | * sysfs_attach_dentry - associate sysfs_dirent with dentry |
| 397 | * @sd: target sysfs_dirent |
| 398 | * @dentry: dentry to associate |
| 399 | * |
| 400 | * Associate @sd with @dentry. This is protected by |
| 401 | * sysfs_assoc_lock to avoid race with sysfs_d_iput(). |
| 402 | * |
| 403 | * LOCKING: |
| 404 | * mutex_lock(sysfs_mutex) |
| 405 | */ |
Tejun Heo | 198a2a8 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 406 | static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry) |
| 407 | { |
| 408 | dentry->d_op = &sysfs_dentry_ops; |
| 409 | dentry->d_fsdata = sysfs_get(sd); |
| 410 | |
| 411 | /* protect sd->s_dentry against sysfs_d_iput */ |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 412 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 198a2a8 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 413 | sd->s_dentry = dentry; |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 414 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | 198a2a8 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 415 | |
| 416 | d_rehash(dentry); |
| 417 | } |
| 418 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 419 | static int sysfs_ilookup_test(struct inode *inode, void *arg) |
| 420 | { |
| 421 | struct sysfs_dirent *sd = arg; |
| 422 | return inode->i_ino == sd->s_ino; |
| 423 | } |
| 424 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 425 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 426 | * sysfs_addrm_start - prepare for sysfs_dirent add/remove |
| 427 | * @acxt: pointer to sysfs_addrm_cxt to be used |
| 428 | * @parent_sd: parent sysfs_dirent |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 429 | * |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 430 | * This function is called when the caller is about to add or |
| 431 | * remove sysfs_dirent under @parent_sd. This function acquires |
| 432 | * sysfs_mutex, grabs inode for @parent_sd if available and lock |
| 433 | * i_mutex of it. @acxt is used to keep and pass context to |
| 434 | * other addrm functions. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 435 | * |
| 436 | * LOCKING: |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 437 | * Kernel thread context (may sleep). sysfs_mutex is locked on |
| 438 | * return. i_mutex of parent inode is locked on return if |
| 439 | * available. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 440 | */ |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 441 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, |
| 442 | struct sysfs_dirent *parent_sd) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 443 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 444 | struct inode *inode; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 445 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 446 | memset(acxt, 0, sizeof(*acxt)); |
| 447 | acxt->parent_sd = parent_sd; |
| 448 | |
| 449 | /* Lookup parent inode. inode initialization and I_NEW |
| 450 | * clearing are protected by sysfs_mutex. By grabbing it and |
| 451 | * looking up with _nowait variant, inode state can be |
| 452 | * determined reliably. |
| 453 | */ |
| 454 | mutex_lock(&sysfs_mutex); |
| 455 | |
| 456 | inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test, |
| 457 | parent_sd); |
| 458 | |
| 459 | if (inode && !(inode->i_state & I_NEW)) { |
| 460 | /* parent inode available */ |
| 461 | acxt->parent_inode = inode; |
| 462 | |
| 463 | /* sysfs_mutex is below i_mutex in lock hierarchy. |
| 464 | * First, trylock i_mutex. If fails, unlock |
| 465 | * sysfs_mutex and lock them in order. |
| 466 | */ |
| 467 | if (!mutex_trylock(&inode->i_mutex)) { |
| 468 | mutex_unlock(&sysfs_mutex); |
| 469 | mutex_lock(&inode->i_mutex); |
| 470 | mutex_lock(&sysfs_mutex); |
| 471 | } |
| 472 | } else |
| 473 | iput(inode); |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * sysfs_add_one - add sysfs_dirent to parent |
| 478 | * @acxt: addrm context to use |
| 479 | * @sd: sysfs_dirent to be added |
| 480 | * |
| 481 | * Get @acxt->parent_sd and set sd->s_parent to it and increment |
| 482 | * nlink of parent inode if @sd is a directory. @sd is NOT |
| 483 | * linked into the children list of the parent. The caller |
| 484 | * should invoke sysfs_link_sibling() after this function |
| 485 | * completes if @sd needs to be on the children list. |
| 486 | * |
| 487 | * This function should be called between calls to |
| 488 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 489 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 490 | * |
| 491 | * LOCKING: |
| 492 | * Determined by sysfs_addrm_start(). |
| 493 | */ |
| 494 | void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) |
| 495 | { |
| 496 | sd->s_parent = sysfs_get(acxt->parent_sd); |
| 497 | |
| 498 | if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) |
| 499 | inc_nlink(acxt->parent_inode); |
| 500 | |
| 501 | acxt->cnt++; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * sysfs_remove_one - remove sysfs_dirent from parent |
| 506 | * @acxt: addrm context to use |
| 507 | * @sd: sysfs_dirent to be added |
| 508 | * |
| 509 | * Mark @sd removed and drop nlink of parent inode if @sd is a |
| 510 | * directory. @sd is NOT unlinked from the children list of the |
| 511 | * parent. The caller is repsonsible for removing @sd from the |
| 512 | * children list before calling this function. |
| 513 | * |
| 514 | * This function should be called between calls to |
| 515 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 516 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 517 | * |
| 518 | * LOCKING: |
| 519 | * Determined by sysfs_addrm_start(). |
| 520 | */ |
| 521 | void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) |
| 522 | { |
| 523 | BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED)); |
| 524 | |
| 525 | sd->s_flags |= SYSFS_FLAG_REMOVED; |
| 526 | sd->s_sibling = acxt->removed; |
| 527 | acxt->removed = sd; |
| 528 | |
| 529 | if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) |
| 530 | drop_nlink(acxt->parent_inode); |
| 531 | |
| 532 | acxt->cnt++; |
| 533 | } |
| 534 | |
| 535 | /** |
Tejun Heo | a0edd7c | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 536 | * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent |
| 537 | * @sd: target sysfs_dirent |
| 538 | * |
| 539 | * Drop dentry for @sd. @sd must have been unlinked from its |
| 540 | * parent on entry to this function such that it can't be looked |
| 541 | * up anymore. |
| 542 | * |
| 543 | * @sd->s_dentry which is protected with sysfs_assoc_lock points |
| 544 | * to the currently associated dentry but we're not holding a |
| 545 | * reference to it and racing with dput(). Grab dcache_lock and |
| 546 | * verify dentry before dropping it. If @sd->s_dentry is NULL or |
| 547 | * dput() beats us, no need to bother. |
| 548 | */ |
| 549 | static void sysfs_drop_dentry(struct sysfs_dirent *sd) |
| 550 | { |
| 551 | struct dentry *dentry = NULL; |
| 552 | struct inode *inode; |
| 553 | |
| 554 | /* We're not holding a reference to ->s_dentry dentry but the |
| 555 | * field will stay valid as long as sysfs_assoc_lock is held. |
| 556 | */ |
| 557 | spin_lock(&sysfs_assoc_lock); |
| 558 | spin_lock(&dcache_lock); |
| 559 | |
| 560 | /* drop dentry if it's there and dput() didn't kill it yet */ |
| 561 | if (sd->s_dentry && sd->s_dentry->d_inode) { |
| 562 | dentry = dget_locked(sd->s_dentry); |
| 563 | spin_lock(&dentry->d_lock); |
| 564 | __d_drop(dentry); |
| 565 | spin_unlock(&dentry->d_lock); |
| 566 | } |
| 567 | |
| 568 | spin_unlock(&dcache_lock); |
| 569 | spin_unlock(&sysfs_assoc_lock); |
| 570 | |
| 571 | dput(dentry); |
| 572 | /* XXX: unpin if directory, this will go away soon */ |
| 573 | if (sysfs_type(sd) == SYSFS_DIR) |
| 574 | dput(dentry); |
| 575 | |
| 576 | /* adjust nlink and update timestamp */ |
| 577 | inode = ilookup(sysfs_sb, sd->s_ino); |
| 578 | if (inode) { |
| 579 | mutex_lock(&inode->i_mutex); |
| 580 | |
| 581 | inode->i_ctime = CURRENT_TIME; |
| 582 | drop_nlink(inode); |
| 583 | if (sysfs_type(sd) == SYSFS_DIR) |
| 584 | drop_nlink(inode); |
| 585 | |
| 586 | mutex_unlock(&inode->i_mutex); |
| 587 | iput(inode); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 592 | * sysfs_addrm_finish - finish up sysfs_dirent add/remove |
| 593 | * @acxt: addrm context to finish up |
| 594 | * |
| 595 | * Finish up sysfs_dirent add/remove. Resources acquired by |
| 596 | * sysfs_addrm_start() are released and removed sysfs_dirents are |
| 597 | * cleaned up. Timestamps on the parent inode are updated. |
| 598 | * |
| 599 | * LOCKING: |
| 600 | * All mutexes acquired by sysfs_addrm_start() are released. |
| 601 | * |
| 602 | * RETURNS: |
| 603 | * Number of added/removed sysfs_dirents since sysfs_addrm_start(). |
| 604 | */ |
| 605 | int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) |
| 606 | { |
| 607 | /* release resources acquired by sysfs_addrm_start() */ |
| 608 | mutex_unlock(&sysfs_mutex); |
| 609 | if (acxt->parent_inode) { |
| 610 | struct inode *inode = acxt->parent_inode; |
| 611 | |
| 612 | /* if added/removed, update timestamps on the parent */ |
| 613 | if (acxt->cnt) |
| 614 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 615 | |
| 616 | mutex_unlock(&inode->i_mutex); |
| 617 | iput(inode); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 618 | } |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 619 | |
| 620 | /* kill removed sysfs_dirents */ |
| 621 | while (acxt->removed) { |
| 622 | struct sysfs_dirent *sd = acxt->removed; |
| 623 | |
| 624 | acxt->removed = sd->s_sibling; |
| 625 | sd->s_sibling = NULL; |
| 626 | |
| 627 | sysfs_drop_dentry(sd); |
| 628 | sysfs_deactivate(sd); |
| 629 | sysfs_put(sd); |
| 630 | } |
| 631 | |
| 632 | return acxt->cnt; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 635 | /** |
| 636 | * sysfs_find_dirent - find sysfs_dirent with the given name |
| 637 | * @parent_sd: sysfs_dirent to search under |
| 638 | * @name: name to look for |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 639 | * |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 640 | * Look for sysfs_dirent with name @name under @parent_sd. |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 641 | * |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 642 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 643 | * mutex_lock(sysfs_mutex) |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 644 | * |
| 645 | * RETURNS: |
| 646 | * Pointer to sysfs_dirent if found, NULL if not. |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 647 | */ |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 648 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, |
| 649 | const unsigned char *name) |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 650 | { |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 651 | struct sysfs_dirent *sd; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 652 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 653 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) |
| 654 | if (sysfs_type(sd) && !strcmp(sd->s_name, name)) |
| 655 | return sd; |
| 656 | return NULL; |
| 657 | } |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 658 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 659 | /** |
| 660 | * sysfs_get_dirent - find and get sysfs_dirent with the given name |
| 661 | * @parent_sd: sysfs_dirent to search under |
| 662 | * @name: name to look for |
| 663 | * |
| 664 | * Look for sysfs_dirent with name @name under @parent_sd and get |
| 665 | * it if found. |
| 666 | * |
| 667 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 668 | * Kernel thread context (may sleep). Grabs sysfs_mutex. |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 669 | * |
| 670 | * RETURNS: |
| 671 | * Pointer to sysfs_dirent if found, NULL if not. |
| 672 | */ |
| 673 | struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, |
| 674 | const unsigned char *name) |
| 675 | { |
| 676 | struct sysfs_dirent *sd; |
| 677 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 678 | mutex_lock(&sysfs_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 679 | sd = sysfs_find_dirent(parent_sd, name); |
| 680 | sysfs_get(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 681 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 682 | |
| 683 | return sd; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 684 | } |
| 685 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 686 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
| 687 | const char *name, struct sysfs_dirent **p_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 689 | struct dentry *parent = parent_sd->s_dentry; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 690 | struct sysfs_addrm_cxt acxt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | int error; |
| 692 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 693 | struct dentry *dentry; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 694 | struct inode *inode; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 695 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 697 | sysfs_addrm_start(&acxt, parent_sd); |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 698 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 699 | /* allocate */ |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 700 | dentry = lookup_one_len(name, parent, strlen(name)); |
| 701 | if (IS_ERR(dentry)) { |
| 702 | error = PTR_ERR(dentry); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 703 | goto out_finish; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | error = -EEXIST; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 707 | if (dentry->d_inode) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 708 | goto out_dput; |
| 709 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 710 | error = -ENOMEM; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 711 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 712 | if (!sd) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 713 | goto out_drop; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 714 | sd->s_elem.dir.kobj = kobj; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 715 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 716 | inode = sysfs_get_inode(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 717 | if (!inode) |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 718 | goto out_sput; |
| 719 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 720 | if (inode->i_state & I_NEW) { |
| 721 | inode->i_op = &sysfs_dir_inode_operations; |
| 722 | inode->i_fop = &sysfs_dir_operations; |
| 723 | /* directory inodes start off with i_nlink == 2 (for ".") */ |
| 724 | inc_nlink(inode); |
| 725 | } |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 726 | |
| 727 | /* link in */ |
| 728 | error = -EEXIST; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 729 | if (sysfs_find_dirent(parent_sd, name)) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 730 | goto out_iput; |
| 731 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 732 | sysfs_add_one(&acxt, sd); |
| 733 | sysfs_link_sibling(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 734 | sysfs_instantiate(dentry, inode); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 735 | sysfs_attach_dentry(sd, dentry); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 736 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 737 | *p_sd = sd; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 738 | error = 0; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 739 | goto out_finish; /* pin directory dentry in core */ |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 740 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 741 | out_iput: |
| 742 | iput(inode); |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 743 | out_sput: |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 744 | sysfs_put(sd); |
| 745 | out_drop: |
| 746 | d_drop(dentry); |
| 747 | out_dput: |
| 748 | dput(dentry); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 749 | out_finish: |
| 750 | sysfs_addrm_finish(&acxt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return error; |
| 752 | } |
| 753 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 754 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
| 755 | struct sysfs_dirent **p_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 757 | return create_dir(kobj, kobj->sd, name, p_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
| 761 | * sysfs_create_dir - create a directory for an object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | * @kobj: object we're creating directory for. |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 763 | * @shadow_parent: parent object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | */ |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 765 | int sysfs_create_dir(struct kobject *kobj, |
| 766 | struct sysfs_dirent *shadow_parent_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 768 | struct sysfs_dirent *parent_sd, *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | int error = 0; |
| 770 | |
| 771 | BUG_ON(!kobj); |
| 772 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 773 | if (shadow_parent_sd) |
| 774 | parent_sd = shadow_parent_sd; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 775 | else if (kobj->parent) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 776 | parent_sd = kobj->parent->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | else if (sysfs_mount && sysfs_mount->mnt_sb) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 778 | parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | else |
| 780 | return -EFAULT; |
| 781 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 782 | error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | if (!error) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 784 | kobj->sd = sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | return error; |
| 786 | } |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, |
| 789 | struct nameidata *nd) |
| 790 | { |
| 791 | struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; |
| 792 | struct sysfs_dirent * sd; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 793 | struct bin_attribute *bin_attr; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 794 | struct inode *inode; |
| 795 | int found = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 797 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) { |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 798 | if ((sysfs_type(sd) & SYSFS_NOT_PINNED) && |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 799 | !strcmp(sd->s_name, dentry->d_name.name)) { |
| 800 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | break; |
| 802 | } |
| 803 | } |
| 804 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 805 | /* no such entry */ |
| 806 | if (!found) |
| 807 | return NULL; |
| 808 | |
| 809 | /* attach dentry and inode */ |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 810 | inode = sysfs_get_inode(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 811 | if (!inode) |
| 812 | return ERR_PTR(-ENOMEM); |
| 813 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 814 | mutex_lock(&sysfs_mutex); |
| 815 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 816 | if (inode->i_state & I_NEW) { |
| 817 | /* initialize inode according to type */ |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 818 | switch (sysfs_type(sd)) { |
| 819 | case SYSFS_KOBJ_ATTR: |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 820 | inode->i_size = PAGE_SIZE; |
| 821 | inode->i_fop = &sysfs_file_operations; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 822 | break; |
| 823 | case SYSFS_KOBJ_BIN_ATTR: |
| 824 | bin_attr = sd->s_elem.bin_attr.bin_attr; |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 825 | inode->i_size = bin_attr->size; |
| 826 | inode->i_fop = &bin_fops; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 827 | break; |
| 828 | case SYSFS_KOBJ_LINK: |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 829 | inode->i_op = &sysfs_symlink_inode_operations; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 830 | break; |
| 831 | default: |
| 832 | BUG(); |
| 833 | } |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 834 | } |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 835 | |
| 836 | sysfs_instantiate(dentry, inode); |
| 837 | sysfs_attach_dentry(sd, dentry); |
| 838 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 839 | mutex_unlock(&sysfs_mutex); |
| 840 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 841 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 844 | const struct inode_operations sysfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | .lookup = sysfs_lookup, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 846 | .setattr = sysfs_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | }; |
| 848 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 849 | static void remove_dir(struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 851 | struct sysfs_addrm_cxt acxt; |
| 852 | |
| 853 | sysfs_addrm_start(&acxt, sd->s_parent); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 854 | sysfs_unlink_sibling(sd); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 855 | sysfs_remove_one(&acxt, sd); |
| 856 | sysfs_addrm_finish(&acxt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 859 | void sysfs_remove_subdir(struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 861 | remove_dir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 865 | static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 867 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 868 | struct sysfs_dirent **pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 870 | if (!dir_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | return; |
| 872 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 873 | pr_debug("sysfs %s: removing dir\n", dir_sd->s_name); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 874 | sysfs_addrm_start(&acxt, dir_sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 875 | pos = &dir_sd->s_children; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 876 | while (*pos) { |
| 877 | struct sysfs_dirent *sd = *pos; |
| 878 | |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 879 | if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) { |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 880 | *pos = sd->s_sibling; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 881 | sd->s_sibling = NULL; |
| 882 | sysfs_remove_one(&acxt, sd); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 883 | } else |
| 884 | pos = &(*pos)->s_sibling; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 886 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 887 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 888 | remove_dir(dir_sd); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /** |
| 892 | * sysfs_remove_dir - remove an object's directory. |
| 893 | * @kobj: object. |
| 894 | * |
| 895 | * The only thing special about this is that we remove any files in |
| 896 | * the directory before we remove the directory, and we've inlined |
| 897 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 898 | */ |
| 899 | |
| 900 | void sysfs_remove_dir(struct kobject * kobj) |
| 901 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 902 | struct sysfs_dirent *sd = kobj->sd; |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 903 | |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 904 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 905 | kobj->sd = NULL; |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 906 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 907 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 908 | __sysfs_remove_dir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 911 | int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 912 | const char *new_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 914 | struct sysfs_dirent *sd = kobj->sd; |
| 915 | struct dentry *new_parent = new_parent_sd->s_dentry; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 916 | struct dentry *new_dentry; |
| 917 | char *dup_name; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 918 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 920 | if (!new_parent_sd) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 921 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 923 | mutex_lock(&new_parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 925 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 926 | if (IS_ERR(new_dentry)) { |
| 927 | error = PTR_ERR(new_dentry); |
| 928 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 930 | |
| 931 | /* By allowing two different directories with the same |
| 932 | * d_parent we allow this routine to move between different |
| 933 | * shadows of the same directory |
| 934 | */ |
| 935 | error = -EINVAL; |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 936 | if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode || |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 937 | new_dentry->d_parent->d_inode != new_parent->d_inode || |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 938 | new_dentry == sd->s_dentry) |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 939 | goto out_dput; |
| 940 | |
| 941 | error = -EEXIST; |
| 942 | if (new_dentry->d_inode) |
| 943 | goto out_dput; |
| 944 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 945 | /* rename kobject and sysfs_dirent */ |
| 946 | error = -ENOMEM; |
| 947 | new_name = dup_name = kstrdup(new_name, GFP_KERNEL); |
| 948 | if (!new_name) |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 949 | goto out_drop; |
| 950 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 951 | error = kobject_set_name(kobj, "%s", new_name); |
| 952 | if (error) |
| 953 | goto out_free; |
| 954 | |
| 955 | kfree(sd->s_name); |
| 956 | sd->s_name = new_name; |
| 957 | |
| 958 | /* move under the new parent */ |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 959 | d_add(new_dentry, NULL); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 960 | d_move(sd->s_dentry, new_dentry); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 961 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 962 | mutex_lock(&sysfs_mutex); |
| 963 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 964 | sysfs_unlink_sibling(sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 965 | sysfs_get(new_parent_sd); |
Tejun Heo | 7f7cfff | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 966 | sysfs_put(sd->s_parent); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 967 | sd->s_parent = new_parent_sd; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 968 | sysfs_link_sibling(sd); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 969 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 970 | mutex_unlock(&sysfs_mutex); |
| 971 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 972 | error = 0; |
| 973 | goto out_unlock; |
| 974 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 975 | out_free: |
| 976 | kfree(dup_name); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 977 | out_drop: |
| 978 | d_drop(new_dentry); |
| 979 | out_dput: |
| 980 | dput(new_dentry); |
| 981 | out_unlock: |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 982 | mutex_unlock(&new_parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return error; |
| 984 | } |
| 985 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 986 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) |
| 987 | { |
| 988 | struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; |
| 989 | struct sysfs_dirent *new_parent_sd, *sd; |
| 990 | int error; |
| 991 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 992 | old_parent_dentry = kobj->parent ? |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 993 | kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root; |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 994 | new_parent_dentry = new_parent ? |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 995 | new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 996 | |
Mark Lord | 0de1517 | 2007-03-06 01:42:03 -0800 | [diff] [blame] | 997 | if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) |
| 998 | return 0; /* nothing to move */ |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 999 | again: |
| 1000 | mutex_lock(&old_parent_dentry->d_inode->i_mutex); |
| 1001 | if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { |
| 1002 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); |
| 1003 | goto again; |
| 1004 | } |
| 1005 | |
| 1006 | new_parent_sd = new_parent_dentry->d_fsdata; |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1007 | sd = kobj->sd; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1008 | |
| 1009 | new_dentry = lookup_one_len(kobj->name, new_parent_dentry, |
| 1010 | strlen(kobj->name)); |
| 1011 | if (IS_ERR(new_dentry)) { |
| 1012 | error = PTR_ERR(new_dentry); |
| 1013 | goto out; |
| 1014 | } else |
| 1015 | error = 0; |
| 1016 | d_add(new_dentry, NULL); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1017 | d_move(sd->s_dentry, new_dentry); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1018 | dput(new_dentry); |
| 1019 | |
| 1020 | /* Remove from old parent's list and insert into new parent's list. */ |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1021 | mutex_lock(&sysfs_mutex); |
| 1022 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1023 | sysfs_unlink_sibling(sd); |
Tejun Heo | 7f7cfff | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1024 | sysfs_get(new_parent_sd); |
| 1025 | sysfs_put(sd->s_parent); |
| 1026 | sd->s_parent = new_parent_sd; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1027 | sysfs_link_sibling(sd); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1028 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1029 | mutex_unlock(&sysfs_mutex); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1030 | out: |
| 1031 | mutex_unlock(&new_parent_dentry->d_inode->i_mutex); |
| 1032 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); |
| 1033 | |
| 1034 | return error; |
| 1035 | } |
| 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | static int sysfs_dir_open(struct inode *inode, struct file *file) |
| 1038 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 1039 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1041 | struct sysfs_dirent * sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 1043 | sd = sysfs_new_dirent("_DIR_", 0, 0); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1044 | if (sd) { |
| 1045 | mutex_lock(&sysfs_mutex); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 1046 | sd->s_parent = sysfs_get(parent_sd); |
| 1047 | sysfs_link_sibling(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1048 | mutex_unlock(&sysfs_mutex); |
| 1049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1051 | file->private_data = sd; |
| 1052 | return sd ? 0 : -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static int sysfs_dir_close(struct inode *inode, struct file *file) |
| 1056 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | struct sysfs_dirent * cursor = file->private_data; |
| 1058 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1059 | mutex_lock(&sysfs_mutex); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1060 | sysfs_unlink_sibling(cursor); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1061 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
| 1063 | release_sysfs_dirent(cursor); |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | /* Relationship between s_mode and the DT_xxx types */ |
| 1069 | static inline unsigned char dt_type(struct sysfs_dirent *sd) |
| 1070 | { |
| 1071 | return (sd->s_mode >> 12) & 15; |
| 1072 | } |
| 1073 | |
| 1074 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 1075 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 1076 | struct dentry *dentry = filp->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
| 1078 | struct sysfs_dirent *cursor = filp->private_data; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1079 | struct sysfs_dirent **pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | ino_t ino; |
| 1081 | int i = filp->f_pos; |
| 1082 | |
| 1083 | switch (i) { |
| 1084 | case 0: |
Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 1085 | ino = parent_sd->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 1087 | break; |
| 1088 | filp->f_pos++; |
| 1089 | i++; |
| 1090 | /* fallthrough */ |
| 1091 | case 1: |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1092 | if (parent_sd->s_parent) |
| 1093 | ino = parent_sd->s_parent->s_ino; |
| 1094 | else |
| 1095 | ino = parent_sd->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 1097 | break; |
| 1098 | filp->f_pos++; |
| 1099 | i++; |
| 1100 | /* fallthrough */ |
| 1101 | default: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1102 | mutex_lock(&sysfs_mutex); |
| 1103 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1104 | pos = &parent_sd->s_children; |
| 1105 | while (*pos != cursor) |
| 1106 | pos = &(*pos)->s_sibling; |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 1107 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1108 | /* unlink cursor */ |
| 1109 | *pos = cursor->s_sibling; |
| 1110 | |
| 1111 | if (filp->f_pos == 2) |
| 1112 | pos = &parent_sd->s_children; |
| 1113 | |
| 1114 | for ( ; *pos; pos = &(*pos)->s_sibling) { |
| 1115 | struct sysfs_dirent *next = *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | const char * name; |
| 1117 | int len; |
| 1118 | |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 1119 | if (!sysfs_type(next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | continue; |
| 1121 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 1122 | name = next->s_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | len = strlen(name); |
Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 1124 | ino = next->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
| 1126 | if (filldir(dirent, name, len, filp->f_pos, ino, |
| 1127 | dt_type(next)) < 0) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1128 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | filp->f_pos++; |
| 1131 | } |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1132 | |
| 1133 | /* put cursor back in */ |
| 1134 | cursor->s_sibling = *pos; |
| 1135 | *pos = cursor; |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1136 | |
| 1137 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) |
| 1143 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 1144 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | switch (origin) { |
| 1147 | case 1: |
| 1148 | offset += file->f_pos; |
| 1149 | case 0: |
| 1150 | if (offset >= 0) |
| 1151 | break; |
| 1152 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | return -EINVAL; |
| 1154 | } |
| 1155 | if (offset != file->f_pos) { |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1156 | mutex_lock(&sysfs_mutex); |
| 1157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | file->f_pos = offset; |
| 1159 | if (file->f_pos >= 2) { |
| 1160 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 1161 | struct sysfs_dirent *cursor = file->private_data; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1162 | struct sysfs_dirent **pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | loff_t n = file->f_pos - 2; |
| 1164 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1165 | sysfs_unlink_sibling(cursor); |
| 1166 | |
| 1167 | pos = &sd->s_children; |
| 1168 | while (n && *pos) { |
| 1169 | struct sysfs_dirent *next = *pos; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 1170 | if (sysfs_type(next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | n--; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1172 | pos = &(*pos)->s_sibling; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | } |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1174 | |
| 1175 | cursor->s_sibling = *pos; |
| 1176 | *pos = cursor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1178 | |
| 1179 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | } |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | return offset; |
| 1183 | } |
| 1184 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1185 | |
| 1186 | /** |
| 1187 | * sysfs_make_shadowed_dir - Setup so a directory can be shadowed |
| 1188 | * @kobj: object we're creating shadow of. |
| 1189 | */ |
| 1190 | |
| 1191 | int sysfs_make_shadowed_dir(struct kobject *kobj, |
| 1192 | void * (*follow_link)(struct dentry *, struct nameidata *)) |
| 1193 | { |
| 1194 | struct inode *inode; |
| 1195 | struct inode_operations *i_op; |
| 1196 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1197 | inode = kobj->sd->s_dentry->d_inode; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1198 | if (inode->i_op != &sysfs_dir_inode_operations) |
| 1199 | return -EINVAL; |
| 1200 | |
| 1201 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); |
| 1202 | if (!i_op) |
| 1203 | return -ENOMEM; |
| 1204 | |
| 1205 | memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); |
| 1206 | i_op->follow_link = follow_link; |
| 1207 | |
| 1208 | /* Locking of inode->i_op? |
| 1209 | * Since setting i_op is a single word write and they |
| 1210 | * are atomic we should be ok here. |
| 1211 | */ |
| 1212 | inode->i_op = i_op; |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * sysfs_create_shadow_dir - create a shadow directory for an object. |
| 1218 | * @kobj: object we're creating directory for. |
| 1219 | * |
| 1220 | * sysfs_make_shadowed_dir must already have been called on this |
| 1221 | * directory. |
| 1222 | */ |
| 1223 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1224 | struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1225 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1226 | struct dentry *dir = kobj->sd->s_dentry; |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1227 | struct inode *inode = dir->d_inode; |
| 1228 | struct dentry *parent = dir->d_parent; |
| 1229 | struct sysfs_dirent *parent_sd = parent->d_fsdata; |
| 1230 | struct dentry *shadow; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1231 | struct sysfs_dirent *sd; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 1232 | struct sysfs_addrm_cxt acxt; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1233 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1234 | sd = ERR_PTR(-EINVAL); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1235 | if (!sysfs_is_shadowed_inode(inode)) |
| 1236 | goto out; |
| 1237 | |
| 1238 | shadow = d_alloc(parent, &dir->d_name); |
| 1239 | if (!shadow) |
| 1240 | goto nomem; |
| 1241 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 1242 | sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1243 | if (!sd) |
| 1244 | goto nomem; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 1245 | sd->s_elem.dir.kobj = kobj; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1246 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 1247 | sysfs_addrm_start(&acxt, parent_sd); |
| 1248 | |
| 1249 | /* add but don't link into children list */ |
| 1250 | sysfs_add_one(&acxt, sd); |
| 1251 | |
| 1252 | /* attach and instantiate dentry */ |
| 1253 | sysfs_attach_dentry(sd, shadow); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1254 | d_instantiate(shadow, igrab(inode)); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 1255 | inc_nlink(inode); /* tj: synchronization? */ |
| 1256 | |
| 1257 | sysfs_addrm_finish(&acxt); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1258 | |
| 1259 | dget(shadow); /* Extra count - pin the dentry in core */ |
| 1260 | |
| 1261 | out: |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1262 | return sd; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1263 | nomem: |
| 1264 | dput(shadow); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1265 | sd = ERR_PTR(-ENOMEM); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1266 | goto out; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * sysfs_remove_shadow_dir - remove an object's directory. |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1271 | * @shadow_sd: sysfs_dirent of shadow directory |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1272 | * |
| 1273 | * The only thing special about this is that we remove any files in |
| 1274 | * the directory before we remove the directory, and we've inlined |
| 1275 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 1276 | */ |
| 1277 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1278 | void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1279 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1280 | __sysfs_remove_dir(shadow_sd); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1283 | const struct file_operations sysfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | .open = sysfs_dir_open, |
| 1285 | .release = sysfs_dir_close, |
| 1286 | .llseek = sysfs_dir_lseek, |
| 1287 | .read = generic_read_dir, |
| 1288 | .readdir = sysfs_readdir, |
| 1289 | }; |