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