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