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> |
Dave Young | 869512a | 2007-07-26 14:53:53 +0000 | [diff] [blame] | 14 | #include <linux/mutex.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; |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 364 | struct sysfs_dirent *sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 365 | |
| 366 | if (type & SYSFS_COPY_NAME) { |
| 367 | name = dup_name = kstrdup(name, GFP_KERNEL); |
| 368 | if (!name) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 369 | return NULL; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 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) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 374 | goto err_out1; |
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)) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 377 | goto err_out2; |
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 | |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 389 | err_out2: |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 390 | kmem_cache_free(sysfs_dir_cachep, sd); |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 391 | err_out1: |
| 392 | kfree(dup_name); |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 393 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 396 | /** |
| 397 | * sysfs_attach_dentry - associate sysfs_dirent with dentry |
| 398 | * @sd: target sysfs_dirent |
| 399 | * @dentry: dentry to associate |
| 400 | * |
| 401 | * Associate @sd with @dentry. This is protected by |
| 402 | * sysfs_assoc_lock to avoid race with sysfs_d_iput(). |
| 403 | * |
| 404 | * LOCKING: |
| 405 | * mutex_lock(sysfs_mutex) |
| 406 | */ |
Tejun Heo | 198a2a8 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 407 | static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry) |
| 408 | { |
| 409 | dentry->d_op = &sysfs_dentry_ops; |
| 410 | dentry->d_fsdata = sysfs_get(sd); |
| 411 | |
| 412 | /* protect sd->s_dentry against sysfs_d_iput */ |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 413 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 198a2a8 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 414 | sd->s_dentry = dentry; |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 415 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | 198a2a8 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 416 | |
| 417 | d_rehash(dentry); |
| 418 | } |
| 419 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 420 | static int sysfs_ilookup_test(struct inode *inode, void *arg) |
| 421 | { |
| 422 | struct sysfs_dirent *sd = arg; |
| 423 | return inode->i_ino == sd->s_ino; |
| 424 | } |
| 425 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 426 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 427 | * sysfs_addrm_start - prepare for sysfs_dirent add/remove |
| 428 | * @acxt: pointer to sysfs_addrm_cxt to be used |
| 429 | * @parent_sd: parent sysfs_dirent |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 430 | * |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 431 | * This function is called when the caller is about to add or |
| 432 | * remove sysfs_dirent under @parent_sd. This function acquires |
| 433 | * sysfs_mutex, grabs inode for @parent_sd if available and lock |
| 434 | * i_mutex of it. @acxt is used to keep and pass context to |
| 435 | * other addrm functions. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 436 | * |
| 437 | * LOCKING: |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 438 | * Kernel thread context (may sleep). sysfs_mutex is locked on |
| 439 | * return. i_mutex of parent inode is locked on return if |
| 440 | * available. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 441 | */ |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 442 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, |
| 443 | struct sysfs_dirent *parent_sd) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 444 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 445 | struct inode *inode; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 446 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 447 | memset(acxt, 0, sizeof(*acxt)); |
| 448 | acxt->parent_sd = parent_sd; |
| 449 | |
| 450 | /* Lookup parent inode. inode initialization and I_NEW |
| 451 | * clearing are protected by sysfs_mutex. By grabbing it and |
| 452 | * looking up with _nowait variant, inode state can be |
| 453 | * determined reliably. |
| 454 | */ |
| 455 | mutex_lock(&sysfs_mutex); |
| 456 | |
| 457 | inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test, |
| 458 | parent_sd); |
| 459 | |
| 460 | if (inode && !(inode->i_state & I_NEW)) { |
| 461 | /* parent inode available */ |
| 462 | acxt->parent_inode = inode; |
| 463 | |
| 464 | /* sysfs_mutex is below i_mutex in lock hierarchy. |
| 465 | * First, trylock i_mutex. If fails, unlock |
| 466 | * sysfs_mutex and lock them in order. |
| 467 | */ |
| 468 | if (!mutex_trylock(&inode->i_mutex)) { |
| 469 | mutex_unlock(&sysfs_mutex); |
| 470 | mutex_lock(&inode->i_mutex); |
| 471 | mutex_lock(&sysfs_mutex); |
| 472 | } |
| 473 | } else |
| 474 | iput(inode); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * sysfs_add_one - add sysfs_dirent to parent |
| 479 | * @acxt: addrm context to use |
| 480 | * @sd: sysfs_dirent to be added |
| 481 | * |
| 482 | * Get @acxt->parent_sd and set sd->s_parent to it and increment |
| 483 | * nlink of parent inode if @sd is a directory. @sd is NOT |
| 484 | * linked into the children list of the parent. The caller |
| 485 | * should invoke sysfs_link_sibling() after this function |
| 486 | * completes if @sd needs to be on the children list. |
| 487 | * |
| 488 | * This function should be called between calls to |
| 489 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 490 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 491 | * |
| 492 | * LOCKING: |
| 493 | * Determined by sysfs_addrm_start(). |
| 494 | */ |
| 495 | void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) |
| 496 | { |
| 497 | sd->s_parent = sysfs_get(acxt->parent_sd); |
| 498 | |
| 499 | if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) |
| 500 | inc_nlink(acxt->parent_inode); |
| 501 | |
| 502 | acxt->cnt++; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * sysfs_remove_one - remove sysfs_dirent from parent |
| 507 | * @acxt: addrm context to use |
| 508 | * @sd: sysfs_dirent to be added |
| 509 | * |
| 510 | * Mark @sd removed and drop nlink of parent inode if @sd is a |
| 511 | * directory. @sd is NOT unlinked from the children list of the |
| 512 | * parent. The caller is repsonsible for removing @sd from the |
| 513 | * children list before calling this function. |
| 514 | * |
| 515 | * This function should be called between calls to |
| 516 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 517 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 518 | * |
| 519 | * LOCKING: |
| 520 | * Determined by sysfs_addrm_start(). |
| 521 | */ |
| 522 | void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) |
| 523 | { |
| 524 | BUG_ON(sd->s_sibling || (sd->s_flags & SYSFS_FLAG_REMOVED)); |
| 525 | |
| 526 | sd->s_flags |= SYSFS_FLAG_REMOVED; |
| 527 | sd->s_sibling = acxt->removed; |
| 528 | acxt->removed = sd; |
| 529 | |
| 530 | if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) |
| 531 | drop_nlink(acxt->parent_inode); |
| 532 | |
| 533 | acxt->cnt++; |
| 534 | } |
| 535 | |
| 536 | /** |
Tejun Heo | a0edd7c | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 537 | * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent |
| 538 | * @sd: target sysfs_dirent |
| 539 | * |
| 540 | * Drop dentry for @sd. @sd must have been unlinked from its |
| 541 | * parent on entry to this function such that it can't be looked |
| 542 | * up anymore. |
| 543 | * |
| 544 | * @sd->s_dentry which is protected with sysfs_assoc_lock points |
| 545 | * to the currently associated dentry but we're not holding a |
| 546 | * reference to it and racing with dput(). Grab dcache_lock and |
| 547 | * verify dentry before dropping it. If @sd->s_dentry is NULL or |
| 548 | * dput() beats us, no need to bother. |
| 549 | */ |
| 550 | static void sysfs_drop_dentry(struct sysfs_dirent *sd) |
| 551 | { |
| 552 | struct dentry *dentry = NULL; |
| 553 | struct inode *inode; |
| 554 | |
| 555 | /* We're not holding a reference to ->s_dentry dentry but the |
| 556 | * field will stay valid as long as sysfs_assoc_lock is held. |
| 557 | */ |
| 558 | spin_lock(&sysfs_assoc_lock); |
| 559 | spin_lock(&dcache_lock); |
| 560 | |
| 561 | /* drop dentry if it's there and dput() didn't kill it yet */ |
| 562 | if (sd->s_dentry && sd->s_dentry->d_inode) { |
| 563 | dentry = dget_locked(sd->s_dentry); |
| 564 | spin_lock(&dentry->d_lock); |
| 565 | __d_drop(dentry); |
| 566 | spin_unlock(&dentry->d_lock); |
| 567 | } |
| 568 | |
| 569 | spin_unlock(&dcache_lock); |
| 570 | spin_unlock(&sysfs_assoc_lock); |
| 571 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 572 | dput(dentry); |
Tejun Heo | a0edd7c | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 573 | |
| 574 | /* adjust nlink and update timestamp */ |
| 575 | inode = ilookup(sysfs_sb, sd->s_ino); |
| 576 | if (inode) { |
| 577 | mutex_lock(&inode->i_mutex); |
| 578 | |
| 579 | inode->i_ctime = CURRENT_TIME; |
| 580 | drop_nlink(inode); |
| 581 | if (sysfs_type(sd) == SYSFS_DIR) |
| 582 | drop_nlink(inode); |
| 583 | |
| 584 | mutex_unlock(&inode->i_mutex); |
| 585 | iput(inode); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 590 | * sysfs_addrm_finish - finish up sysfs_dirent add/remove |
| 591 | * @acxt: addrm context to finish up |
| 592 | * |
| 593 | * Finish up sysfs_dirent add/remove. Resources acquired by |
| 594 | * sysfs_addrm_start() are released and removed sysfs_dirents are |
| 595 | * cleaned up. Timestamps on the parent inode are updated. |
| 596 | * |
| 597 | * LOCKING: |
| 598 | * All mutexes acquired by sysfs_addrm_start() are released. |
| 599 | * |
| 600 | * RETURNS: |
| 601 | * Number of added/removed sysfs_dirents since sysfs_addrm_start(). |
| 602 | */ |
| 603 | int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) |
| 604 | { |
| 605 | /* release resources acquired by sysfs_addrm_start() */ |
| 606 | mutex_unlock(&sysfs_mutex); |
| 607 | if (acxt->parent_inode) { |
| 608 | struct inode *inode = acxt->parent_inode; |
| 609 | |
| 610 | /* if added/removed, update timestamps on the parent */ |
| 611 | if (acxt->cnt) |
| 612 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 613 | |
| 614 | mutex_unlock(&inode->i_mutex); |
| 615 | iput(inode); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 616 | } |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 617 | |
| 618 | /* kill removed sysfs_dirents */ |
| 619 | while (acxt->removed) { |
| 620 | struct sysfs_dirent *sd = acxt->removed; |
| 621 | |
| 622 | acxt->removed = sd->s_sibling; |
| 623 | sd->s_sibling = NULL; |
| 624 | |
| 625 | sysfs_drop_dentry(sd); |
| 626 | sysfs_deactivate(sd); |
| 627 | sysfs_put(sd); |
| 628 | } |
| 629 | |
| 630 | return acxt->cnt; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 633 | /** |
| 634 | * sysfs_find_dirent - find sysfs_dirent with the given name |
| 635 | * @parent_sd: sysfs_dirent to search under |
| 636 | * @name: name to look for |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 637 | * |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 638 | * Look for sysfs_dirent with name @name under @parent_sd. |
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 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 641 | * mutex_lock(sysfs_mutex) |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 642 | * |
| 643 | * RETURNS: |
| 644 | * Pointer to sysfs_dirent if found, NULL if not. |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 645 | */ |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 646 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, |
| 647 | const unsigned char *name) |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 648 | { |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 649 | struct sysfs_dirent *sd; |
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 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) |
| 652 | if (sysfs_type(sd) && !strcmp(sd->s_name, name)) |
| 653 | return sd; |
| 654 | return NULL; |
| 655 | } |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 656 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 657 | /** |
| 658 | * sysfs_get_dirent - find and get sysfs_dirent with the given name |
| 659 | * @parent_sd: sysfs_dirent to search under |
| 660 | * @name: name to look for |
| 661 | * |
| 662 | * Look for sysfs_dirent with name @name under @parent_sd and get |
| 663 | * it if found. |
| 664 | * |
| 665 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 666 | * Kernel thread context (may sleep). Grabs sysfs_mutex. |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 667 | * |
| 668 | * RETURNS: |
| 669 | * Pointer to sysfs_dirent if found, NULL if not. |
| 670 | */ |
| 671 | struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, |
| 672 | const unsigned char *name) |
| 673 | { |
| 674 | struct sysfs_dirent *sd; |
| 675 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 676 | mutex_lock(&sysfs_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 677 | sd = sysfs_find_dirent(parent_sd, name); |
| 678 | sysfs_get(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 679 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 680 | |
| 681 | return sd; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 682 | } |
| 683 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 684 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
| 685 | const char *name, struct sysfs_dirent **p_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 688 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 689 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 691 | /* allocate */ |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 692 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 693 | if (!sd) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 694 | return -ENOMEM; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 695 | sd->s_elem.dir.kobj = kobj; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 696 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 697 | /* link in */ |
| 698 | sysfs_addrm_start(&acxt, parent_sd); |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 699 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 700 | if (!sysfs_find_dirent(parent_sd, name)) { |
| 701 | sysfs_add_one(&acxt, sd); |
| 702 | sysfs_link_sibling(sd); |
| 703 | } |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 704 | |
| 705 | if (!sysfs_addrm_finish(&acxt)) { |
| 706 | sysfs_put(sd); |
| 707 | return -EEXIST; |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 708 | } |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 709 | |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 710 | *p_sd = sd; |
| 711 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 714 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
| 715 | struct sysfs_dirent **p_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 717 | return create_dir(kobj, kobj->sd, name, p_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /** |
| 721 | * sysfs_create_dir - create a directory for an object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | * @kobj: object we're creating directory for. |
| 723 | */ |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 724 | int sysfs_create_dir(struct kobject * kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 726 | struct sysfs_dirent *parent_sd, *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | int error = 0; |
| 728 | |
| 729 | BUG_ON(!kobj); |
| 730 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 731 | if (kobj->parent) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 732 | parent_sd = kobj->parent->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | else if (sysfs_mount && sysfs_mount->mnt_sb) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 734 | parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | else |
| 736 | return -EFAULT; |
| 737 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 738 | error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | if (!error) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 740 | kobj->sd = sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | return error; |
| 742 | } |
| 743 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 744 | static int sysfs_count_nlink(struct sysfs_dirent *sd) |
| 745 | { |
| 746 | struct sysfs_dirent *child; |
| 747 | int nr = 0; |
| 748 | |
| 749 | for (child = sd->s_children; child; child = child->s_sibling) |
| 750 | if (sysfs_type(child) == SYSFS_DIR) |
| 751 | nr++; |
| 752 | return nr + 2; |
| 753 | } |
| 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, |
| 756 | struct nameidata *nd) |
| 757 | { |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 758 | struct dentry *ret = NULL; |
Tejun Heo | a7a0475 | 2007-08-02 21:38:02 +0900 | [diff] [blame^] | 759 | struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata; |
| 760 | struct sysfs_dirent *sd; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 761 | struct bin_attribute *bin_attr; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 762 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 764 | mutex_lock(&sysfs_mutex); |
| 765 | |
Tejun Heo | a7a0475 | 2007-08-02 21:38:02 +0900 | [diff] [blame^] | 766 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) |
| 767 | if (sysfs_type(sd) && !strcmp(sd->s_name, dentry->d_name.name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 770 | /* no such entry */ |
Tejun Heo | a7a0475 | 2007-08-02 21:38:02 +0900 | [diff] [blame^] | 771 | if (!sd) |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 772 | goto out_unlock; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 773 | |
| 774 | /* attach dentry and inode */ |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 775 | inode = sysfs_get_inode(sd); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 776 | if (!inode) { |
| 777 | ret = ERR_PTR(-ENOMEM); |
| 778 | goto out_unlock; |
| 779 | } |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 780 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 781 | if (inode->i_state & I_NEW) { |
| 782 | /* initialize inode according to type */ |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 783 | switch (sysfs_type(sd)) { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 784 | case SYSFS_DIR: |
| 785 | inode->i_op = &sysfs_dir_inode_operations; |
| 786 | inode->i_fop = &sysfs_dir_operations; |
| 787 | inode->i_nlink = sysfs_count_nlink(sd); |
| 788 | break; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 789 | case SYSFS_KOBJ_ATTR: |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 790 | inode->i_size = PAGE_SIZE; |
| 791 | inode->i_fop = &sysfs_file_operations; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 792 | break; |
| 793 | case SYSFS_KOBJ_BIN_ATTR: |
| 794 | bin_attr = sd->s_elem.bin_attr.bin_attr; |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 795 | inode->i_size = bin_attr->size; |
| 796 | inode->i_fop = &bin_fops; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 797 | break; |
| 798 | case SYSFS_KOBJ_LINK: |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 799 | inode->i_op = &sysfs_symlink_inode_operations; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 800 | break; |
| 801 | default: |
| 802 | BUG(); |
| 803 | } |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 804 | } |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 805 | |
| 806 | sysfs_instantiate(dentry, inode); |
| 807 | sysfs_attach_dentry(sd, dentry); |
| 808 | |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 809 | out_unlock: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 810 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 811 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 814 | const struct inode_operations sysfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | .lookup = sysfs_lookup, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 816 | .setattr = sysfs_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | }; |
| 818 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 819 | static void remove_dir(struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 821 | struct sysfs_addrm_cxt acxt; |
| 822 | |
| 823 | sysfs_addrm_start(&acxt, sd->s_parent); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 824 | sysfs_unlink_sibling(sd); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 825 | sysfs_remove_one(&acxt, sd); |
| 826 | sysfs_addrm_finish(&acxt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 829 | void sysfs_remove_subdir(struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 831 | remove_dir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 835 | static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 837 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 838 | struct sysfs_dirent **pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 840 | if (!dir_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | return; |
| 842 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 843 | pr_debug("sysfs %s: removing dir\n", dir_sd->s_name); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 844 | sysfs_addrm_start(&acxt, dir_sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 845 | pos = &dir_sd->s_children; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 846 | while (*pos) { |
| 847 | struct sysfs_dirent *sd = *pos; |
| 848 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 849 | if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR) { |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 850 | *pos = sd->s_sibling; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 851 | sd->s_sibling = NULL; |
| 852 | sysfs_remove_one(&acxt, sd); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 853 | } else |
| 854 | pos = &(*pos)->s_sibling; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 856 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 857 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 858 | remove_dir(dir_sd); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
| 862 | * sysfs_remove_dir - remove an object's directory. |
| 863 | * @kobj: object. |
| 864 | * |
| 865 | * The only thing special about this is that we remove any files in |
| 866 | * the directory before we remove the directory, and we've inlined |
| 867 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 868 | */ |
| 869 | |
| 870 | void sysfs_remove_dir(struct kobject * kobj) |
| 871 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 872 | struct sysfs_dirent *sd = kobj->sd; |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 873 | |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 874 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 875 | kobj->sd = NULL; |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 876 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 877 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 878 | __sysfs_remove_dir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 881 | int sysfs_rename_dir(struct kobject * kobj, const char *new_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | { |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 883 | struct sysfs_dirent *sd; |
| 884 | struct dentry *parent = NULL; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 885 | struct dentry *old_dentry = NULL, *new_dentry = NULL; |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 886 | struct sysfs_dirent *parent_sd; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 887 | const char *dup_name = NULL; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 888 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 890 | if (!kobj->parent) |
| 891 | return -EINVAL; |
| 892 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 893 | /* get dentries */ |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 894 | sd = kobj->sd; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 895 | old_dentry = sysfs_get_dentry(sd); |
| 896 | if (IS_ERR(old_dentry)) { |
| 897 | error = PTR_ERR(old_dentry); |
| 898 | goto out_dput; |
| 899 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 901 | parent_sd = kobj->parent->sd; |
| 902 | parent = sysfs_get_dentry(parent_sd); |
| 903 | if (IS_ERR(parent)) { |
| 904 | error = PTR_ERR(parent); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 905 | goto out_dput; |
| 906 | } |
| 907 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 908 | /* lock parent and get dentry for new name */ |
| 909 | mutex_lock(&parent->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 911 | new_dentry = lookup_one_len(new_name, parent, strlen(new_name)); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 912 | if (IS_ERR(new_dentry)) { |
| 913 | error = PTR_ERR(new_dentry); |
| 914 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 916 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 917 | error = -EINVAL; |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 918 | if (old_dentry == new_dentry) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 919 | goto out_unlock; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 920 | |
| 921 | error = -EEXIST; |
| 922 | if (new_dentry->d_inode) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 923 | goto out_unlock; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 924 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 925 | /* rename kobject and sysfs_dirent */ |
| 926 | error = -ENOMEM; |
| 927 | new_name = dup_name = kstrdup(new_name, GFP_KERNEL); |
| 928 | if (!new_name) |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 929 | goto out_drop; |
| 930 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 931 | error = kobject_set_name(kobj, "%s", new_name); |
| 932 | if (error) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 933 | goto out_drop; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 934 | |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 935 | mutex_lock(&sysfs_mutex); |
| 936 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 937 | dup_name = sd->s_name; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 938 | sd->s_name = new_name; |
| 939 | |
| 940 | /* move under the new parent */ |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 941 | d_add(new_dentry, NULL); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 942 | d_move(sd->s_dentry, new_dentry); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 943 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 944 | sysfs_unlink_sibling(sd); |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 945 | sysfs_get(parent_sd); |
Tejun Heo | 7f7cfff | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 946 | sysfs_put(sd->s_parent); |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 947 | sd->s_parent = parent_sd; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 948 | sysfs_link_sibling(sd); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 949 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 950 | mutex_unlock(&sysfs_mutex); |
| 951 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 952 | error = 0; |
| 953 | goto out_unlock; |
| 954 | |
| 955 | out_drop: |
| 956 | d_drop(new_dentry); |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 957 | out_unlock: |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 958 | mutex_unlock(&parent->d_inode->i_mutex); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 959 | out_dput: |
| 960 | kfree(dup_name); |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 961 | dput(parent); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 962 | dput(old_dentry); |
| 963 | dput(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | return error; |
| 965 | } |
| 966 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 967 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 968 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 969 | struct sysfs_dirent *sd = kobj->sd; |
| 970 | struct sysfs_dirent *new_parent_sd; |
| 971 | struct dentry *old_parent, *new_parent = NULL; |
| 972 | struct dentry *old_dentry = NULL, *new_dentry = NULL; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 973 | int error; |
| 974 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 975 | BUG_ON(!sd->s_parent); |
| 976 | new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 977 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 978 | /* get dentries */ |
| 979 | old_dentry = sysfs_get_dentry(sd); |
| 980 | if (IS_ERR(old_dentry)) { |
| 981 | error = PTR_ERR(old_dentry); |
| 982 | goto out_dput; |
| 983 | } |
| 984 | old_parent = sd->s_parent->s_dentry; |
| 985 | |
| 986 | new_parent = sysfs_get_dentry(new_parent_sd); |
| 987 | if (IS_ERR(new_parent)) { |
| 988 | error = PTR_ERR(new_parent); |
| 989 | goto out_dput; |
| 990 | } |
| 991 | |
| 992 | if (old_parent->d_inode == new_parent->d_inode) { |
| 993 | error = 0; |
| 994 | goto out_dput; /* nothing to move */ |
| 995 | } |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 996 | again: |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 997 | mutex_lock(&old_parent->d_inode->i_mutex); |
| 998 | if (!mutex_trylock(&new_parent->d_inode->i_mutex)) { |
| 999 | mutex_unlock(&old_parent->d_inode->i_mutex); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1000 | goto again; |
| 1001 | } |
| 1002 | |
Greg Kroah-Hartman | 19c38de | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 1003 | new_dentry = lookup_one_len(kobject_name(kobj), new_parent, strlen(kobject_name(kobj))); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1004 | if (IS_ERR(new_dentry)) { |
| 1005 | error = PTR_ERR(new_dentry); |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 1006 | goto out_unlock; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1007 | } else |
| 1008 | error = 0; |
| 1009 | d_add(new_dentry, NULL); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 1010 | d_move(sd->s_dentry, new_dentry); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1011 | dput(new_dentry); |
| 1012 | |
| 1013 | /* 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] | 1014 | mutex_lock(&sysfs_mutex); |
| 1015 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1016 | sysfs_unlink_sibling(sd); |
Tejun Heo | 7f7cfff | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 1017 | sysfs_get(new_parent_sd); |
| 1018 | sysfs_put(sd->s_parent); |
| 1019 | sd->s_parent = new_parent_sd; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1020 | sysfs_link_sibling(sd); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1021 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1022 | mutex_unlock(&sysfs_mutex); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1023 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 1024 | out_unlock: |
| 1025 | mutex_unlock(&new_parent->d_inode->i_mutex); |
| 1026 | mutex_unlock(&old_parent->d_inode->i_mutex); |
| 1027 | out_dput: |
| 1028 | dput(new_parent); |
| 1029 | dput(old_dentry); |
| 1030 | dput(new_dentry); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1031 | return error; |
| 1032 | } |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | static int sysfs_dir_open(struct inode *inode, struct file *file) |
| 1035 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 1036 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1038 | struct sysfs_dirent * sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 1040 | sd = sysfs_new_dirent("_DIR_", 0, 0); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1041 | if (sd) { |
| 1042 | mutex_lock(&sysfs_mutex); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 1043 | sd->s_parent = sysfs_get(parent_sd); |
| 1044 | sysfs_link_sibling(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1045 | mutex_unlock(&sysfs_mutex); |
| 1046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1048 | file->private_data = sd; |
| 1049 | return sd ? 0 : -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static int sysfs_dir_close(struct inode *inode, struct file *file) |
| 1053 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | struct sysfs_dirent * cursor = file->private_data; |
| 1055 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1056 | mutex_lock(&sysfs_mutex); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1057 | sysfs_unlink_sibling(cursor); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1058 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
| 1060 | release_sysfs_dirent(cursor); |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | /* Relationship between s_mode and the DT_xxx types */ |
| 1066 | static inline unsigned char dt_type(struct sysfs_dirent *sd) |
| 1067 | { |
| 1068 | return (sd->s_mode >> 12) & 15; |
| 1069 | } |
| 1070 | |
| 1071 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 1072 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 1073 | struct dentry *dentry = filp->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
| 1075 | struct sysfs_dirent *cursor = filp->private_data; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1076 | struct sysfs_dirent **pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | ino_t ino; |
| 1078 | int i = filp->f_pos; |
| 1079 | |
| 1080 | switch (i) { |
| 1081 | case 0: |
Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 1082 | ino = parent_sd->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 1084 | break; |
| 1085 | filp->f_pos++; |
| 1086 | i++; |
| 1087 | /* fallthrough */ |
| 1088 | case 1: |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 1089 | if (parent_sd->s_parent) |
| 1090 | ino = parent_sd->s_parent->s_ino; |
| 1091 | else |
| 1092 | ino = parent_sd->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 1094 | break; |
| 1095 | filp->f_pos++; |
| 1096 | i++; |
| 1097 | /* fallthrough */ |
| 1098 | default: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1099 | mutex_lock(&sysfs_mutex); |
| 1100 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1101 | pos = &parent_sd->s_children; |
| 1102 | while (*pos != cursor) |
| 1103 | pos = &(*pos)->s_sibling; |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 1104 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1105 | /* unlink cursor */ |
| 1106 | *pos = cursor->s_sibling; |
| 1107 | |
| 1108 | if (filp->f_pos == 2) |
| 1109 | pos = &parent_sd->s_children; |
| 1110 | |
| 1111 | for ( ; *pos; pos = &(*pos)->s_sibling) { |
| 1112 | struct sysfs_dirent *next = *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | const char * name; |
| 1114 | int len; |
| 1115 | |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 1116 | if (!sysfs_type(next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | continue; |
| 1118 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 1119 | name = next->s_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | len = strlen(name); |
Eric Sandeen | dc35125 | 2007-06-11 14:02:45 +0900 | [diff] [blame] | 1121 | ino = next->s_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
| 1123 | if (filldir(dirent, name, len, filp->f_pos, ino, |
| 1124 | dt_type(next)) < 0) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1125 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | filp->f_pos++; |
| 1128 | } |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1129 | |
| 1130 | /* put cursor back in */ |
| 1131 | cursor->s_sibling = *pos; |
| 1132 | *pos = cursor; |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1133 | |
| 1134 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) |
| 1140 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 1141 | struct dentry * dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | switch (origin) { |
| 1144 | case 1: |
| 1145 | offset += file->f_pos; |
| 1146 | case 0: |
| 1147 | if (offset >= 0) |
| 1148 | break; |
| 1149 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | return -EINVAL; |
| 1151 | } |
| 1152 | if (offset != file->f_pos) { |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1153 | mutex_lock(&sysfs_mutex); |
| 1154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | file->f_pos = offset; |
| 1156 | if (file->f_pos >= 2) { |
| 1157 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 1158 | struct sysfs_dirent *cursor = file->private_data; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1159 | struct sysfs_dirent **pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | loff_t n = file->f_pos - 2; |
| 1161 | |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1162 | sysfs_unlink_sibling(cursor); |
| 1163 | |
| 1164 | pos = &sd->s_children; |
| 1165 | while (n && *pos) { |
| 1166 | struct sysfs_dirent *next = *pos; |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 1167 | if (sysfs_type(next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | n--; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1169 | pos = &(*pos)->s_sibling; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 1171 | |
| 1172 | cursor->s_sibling = *pos; |
| 1173 | *pos = cursor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 1175 | |
| 1176 | mutex_unlock(&sysfs_mutex); |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | return offset; |
| 1180 | } |
| 1181 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1182 | const struct file_operations sysfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | .open = sysfs_dir_open, |
| 1184 | .release = sysfs_dir_close, |
| 1185 | .llseek = sysfs_dir_lseek, |
| 1186 | .read = generic_read_dir, |
| 1187 | .readdir = sysfs_readdir, |
| 1188 | }; |