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