blob: 4ad9422566a84698af49b70a1aa4ea6c9eb75f6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hellwig5f45f1a2005-06-23 00:09:12 -070011#include <linux/namei.h>
Tejun Heo2b611bb2007-06-14 03:45:13 +090012#include <linux/idr.h>
Tejun Heo8619f972007-06-14 03:45:18 +090013#include <linux/completion.h>
Dave Young869512a2007-07-26 14:53:53 +000014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "sysfs.h"
16
Tejun Heo3007e992007-06-14 04:27:23 +090017DEFINE_MUTEX(sysfs_mutex);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +090018DEFINE_MUTEX(sysfs_rename_mutex);
Tejun Heo5f995322007-06-14 04:27:23 +090019spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Tejun Heo2b611bb2007-06-14 03:45:13 +090021static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22static DEFINE_IDA(sysfs_ino_ida);
23
Tejun Heob6b4a432007-06-14 03:45:18 +090024/**
Tejun Heo0c73f182007-06-14 03:45:18 +090025 * sysfs_link_sibling - link sysfs_dirent into sibling list
26 * @sd: sysfs_dirent of interest
27 *
28 * Link @sd into its sibling list which starts from
Tejun Heobc747f32007-09-20 16:05:12 +090029 * sd->s_parent->s_dir.children.
Tejun Heo0c73f182007-06-14 03:45:18 +090030 *
31 * Locking:
Tejun Heo3007e992007-06-14 04:27:23 +090032 * mutex_lock(sysfs_mutex)
Tejun Heo0c73f182007-06-14 03:45:18 +090033 */
Tejun Heo41fc1c22007-08-02 21:38:03 +090034static void sysfs_link_sibling(struct sysfs_dirent *sd)
Tejun Heo0c73f182007-06-14 03:45:18 +090035{
36 struct sysfs_dirent *parent_sd = sd->s_parent;
Eric W. Biederman3efa65b2007-08-20 21:36:30 +090037 struct sysfs_dirent **pos;
Tejun Heo0c73f182007-06-14 03:45:18 +090038
39 BUG_ON(sd->s_sibling);
Eric W. Biederman3efa65b2007-08-20 21:36:30 +090040
41 /* Store directory entries in order by ino. This allows
42 * readdir to properly restart without having to add a
Tejun Heobc747f32007-09-20 16:05:12 +090043 * cursor into the s_dir.children list.
Eric W. Biederman3efa65b2007-08-20 21:36:30 +090044 */
Tejun Heobc747f32007-09-20 16:05:12 +090045 for (pos = &parent_sd->s_dir.children; *pos; pos = &(*pos)->s_sibling) {
Eric W. Biederman3efa65b2007-08-20 21:36:30 +090046 if (sd->s_ino < (*pos)->s_ino)
47 break;
48 }
49 sd->s_sibling = *pos;
50 *pos = sd;
Tejun Heo0c73f182007-06-14 03:45:18 +090051}
52
53/**
54 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
55 * @sd: sysfs_dirent of interest
56 *
57 * Unlink @sd from its sibling list which starts from
Tejun Heobc747f32007-09-20 16:05:12 +090058 * sd->s_parent->s_dir.children.
Tejun Heo0c73f182007-06-14 03:45:18 +090059 *
60 * Locking:
Tejun Heo3007e992007-06-14 04:27:23 +090061 * mutex_lock(sysfs_mutex)
Tejun Heo0c73f182007-06-14 03:45:18 +090062 */
Tejun Heo41fc1c22007-08-02 21:38:03 +090063static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
Tejun Heo0c73f182007-06-14 03:45:18 +090064{
65 struct sysfs_dirent **pos;
66
Tejun Heobc747f32007-09-20 16:05:12 +090067 for (pos = &sd->s_parent->s_dir.children; *pos;
68 pos = &(*pos)->s_sibling) {
Tejun Heo0c73f182007-06-14 03:45:18 +090069 if (*pos == sd) {
70 *pos = sd->s_sibling;
71 sd->s_sibling = NULL;
72 break;
73 }
74 }
75}
76
77/**
Tejun Heo53e0ae92007-06-14 04:27:25 +090078 * sysfs_get_dentry - get dentry for the given sysfs_dirent
79 * @sd: sysfs_dirent of interest
80 *
81 * Get dentry for @sd. Dentry is looked up if currently not
Tejun Heoe0712bb2007-08-20 21:36:30 +090082 * present. This function descends from the root looking up
83 * dentry for each step.
Tejun Heo53e0ae92007-06-14 04:27:25 +090084 *
85 * LOCKING:
Eric W. Biederman932ea2e2007-08-20 21:36:30 +090086 * mutex_lock(sysfs_rename_mutex)
Tejun Heo53e0ae92007-06-14 04:27:25 +090087 *
88 * RETURNS:
89 * Pointer to found dentry on success, ERR_PTR() value on error.
90 */
91struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
92{
Tejun Heoe0712bb2007-08-20 21:36:30 +090093 struct dentry *dentry = dget(sysfs_sb->s_root);
Tejun Heo53e0ae92007-06-14 04:27:25 +090094
Tejun Heoe0712bb2007-08-20 21:36:30 +090095 while (dentry->d_fsdata != sd) {
96 struct sysfs_dirent *cur;
97 struct dentry *parent;
Tejun Heo53e0ae92007-06-14 04:27:25 +090098
Tejun Heoe0712bb2007-08-20 21:36:30 +090099 /* find the first ancestor which hasn't been looked up */
100 cur = sd;
101 while (cur->s_parent != dentry->d_fsdata)
Tejun Heo53e0ae92007-06-14 04:27:25 +0900102 cur = cur->s_parent;
103
Tejun Heo53e0ae92007-06-14 04:27:25 +0900104 /* look it up */
Tejun Heoe0712bb2007-08-20 21:36:30 +0900105 parent = dentry;
106 mutex_lock(&parent->d_inode->i_mutex);
107 dentry = lookup_one_len_kern(cur->s_name, parent,
Tejun Heo53e0ae92007-06-14 04:27:25 +0900108 strlen(cur->s_name));
Tejun Heoe0712bb2007-08-20 21:36:30 +0900109 mutex_unlock(&parent->d_inode->i_mutex);
110 dput(parent);
Tejun Heo53e0ae92007-06-14 04:27:25 +0900111
Tejun Heoe0712bb2007-08-20 21:36:30 +0900112 if (IS_ERR(dentry))
113 break;
Tejun Heo53e0ae92007-06-14 04:27:25 +0900114 }
Tejun Heo53e0ae92007-06-14 04:27:25 +0900115 return dentry;
116}
117
118/**
Tejun Heob6b4a432007-06-14 03:45:18 +0900119 * sysfs_get_active - get an active reference to sysfs_dirent
120 * @sd: sysfs_dirent to get an active reference to
121 *
122 * Get an active reference of @sd. This function is noop if @sd
123 * is NULL.
124 *
125 * RETURNS:
126 * Pointer to @sd on success, NULL on failure.
127 */
128struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
129{
Tejun Heo8619f972007-06-14 03:45:18 +0900130 if (unlikely(!sd))
131 return NULL;
132
133 while (1) {
134 int v, t;
135
136 v = atomic_read(&sd->s_active);
137 if (unlikely(v < 0))
138 return NULL;
139
140 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
141 if (likely(t == v))
142 return sd;
143 if (t < 0)
144 return NULL;
145
146 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +0900147 }
Tejun Heob6b4a432007-06-14 03:45:18 +0900148}
149
150/**
151 * sysfs_put_active - put an active reference to sysfs_dirent
152 * @sd: sysfs_dirent to put an active reference to
153 *
154 * Put an active reference to @sd. This function is noop if @sd
155 * is NULL.
156 */
157void sysfs_put_active(struct sysfs_dirent *sd)
158{
Tejun Heo8619f972007-06-14 03:45:18 +0900159 struct completion *cmpl;
160 int v;
161
162 if (unlikely(!sd))
163 return;
164
165 v = atomic_dec_return(&sd->s_active);
166 if (likely(v != SD_DEACTIVATED_BIAS))
167 return;
168
169 /* atomic_dec_return() is a mb(), we'll always see the updated
Tejun Heo0c73f182007-06-14 03:45:18 +0900170 * sd->s_sibling.
Tejun Heo8619f972007-06-14 03:45:18 +0900171 */
Tejun Heo0c73f182007-06-14 03:45:18 +0900172 cmpl = (void *)sd->s_sibling;
Tejun Heo8619f972007-06-14 03:45:18 +0900173 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +0900174}
175
176/**
177 * sysfs_get_active_two - get active references to sysfs_dirent and parent
178 * @sd: sysfs_dirent of interest
179 *
180 * Get active reference to @sd and its parent. Parent's active
181 * reference is grabbed first. This function is noop if @sd is
182 * NULL.
183 *
184 * RETURNS:
185 * Pointer to @sd on success, NULL on failure.
186 */
187struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
188{
189 if (sd) {
190 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
191 return NULL;
192 if (unlikely(!sysfs_get_active(sd))) {
193 sysfs_put_active(sd->s_parent);
194 return NULL;
195 }
196 }
197 return sd;
198}
199
200/**
201 * sysfs_put_active_two - put active references to sysfs_dirent and parent
202 * @sd: sysfs_dirent of interest
203 *
204 * Put active references to @sd and its parent. This function is
205 * noop if @sd is NULL.
206 */
207void sysfs_put_active_two(struct sysfs_dirent *sd)
208{
209 if (sd) {
210 sysfs_put_active(sd);
211 sysfs_put_active(sd->s_parent);
212 }
213}
214
215/**
216 * sysfs_deactivate - deactivate sysfs_dirent
217 * @sd: sysfs_dirent to deactivate
218 *
Tejun Heo8619f972007-06-14 03:45:18 +0900219 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900220 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900221static void sysfs_deactivate(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900222{
Tejun Heo8619f972007-06-14 03:45:18 +0900223 DECLARE_COMPLETION_ONSTACK(wait);
224 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900225
Tejun Heo380e6fb2007-06-14 04:27:22 +0900226 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
Tejun Heo0c73f182007-06-14 03:45:18 +0900227 sd->s_sibling = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900228
229 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo0c73f182007-06-14 03:45:18 +0900230 * the updated sd->s_sibling.
Tejun Heob6b4a432007-06-14 03:45:18 +0900231 */
Tejun Heo8619f972007-06-14 03:45:18 +0900232 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
233
234 if (v != SD_DEACTIVATED_BIAS)
235 wait_for_completion(&wait);
236
Tejun Heo0c73f182007-06-14 03:45:18 +0900237 sd->s_sibling = NULL;
Tejun Heob6b4a432007-06-14 03:45:18 +0900238}
239
Tejun Heo42b37df2007-06-14 03:45:17 +0900240static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900241{
242 int ino, rc;
243
244 retry:
245 spin_lock(&sysfs_ino_lock);
246 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
247 spin_unlock(&sysfs_ino_lock);
248
249 if (rc == -EAGAIN) {
250 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
251 goto retry;
252 rc = -ENOMEM;
253 }
254
255 *pino = ino;
256 return rc;
257}
258
259static void sysfs_free_ino(ino_t ino)
260{
261 spin_lock(&sysfs_ino_lock);
262 ida_remove(&sysfs_ino_ida, ino);
263 spin_unlock(&sysfs_ino_lock);
264}
265
Tejun Heofa7f9122007-06-14 03:45:13 +0900266void release_sysfs_dirent(struct sysfs_dirent * sd)
267{
Tejun Heo13b30862007-06-14 03:45:14 +0900268 struct sysfs_dirent *parent_sd;
269
270 repeat:
Tejun Heo3007e992007-06-14 04:27:23 +0900271 /* Moving/renaming is always done while holding reference.
272 * sd->s_parent won't change beneath us.
273 */
Tejun Heo13b30862007-06-14 03:45:14 +0900274 parent_sd = sd->s_parent;
275
Tejun Heob402d722007-06-14 04:27:21 +0900276 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heob1fc3d62007-09-20 16:05:11 +0900277 sysfs_put(sd->s_symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900278 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900279 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900280 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900281 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900282 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900283
284 sd = parent_sd;
285 if (sd && atomic_dec_and_test(&sd->s_count))
286 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
290{
291 struct sysfs_dirent * sd = dentry->d_fsdata;
292
Eric W. Biederman5a26b792007-08-20 21:36:30 +0900293 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 iput(inode);
295}
296
297static struct dentry_operations sysfs_dentry_ops = {
298 .d_iput = sysfs_d_iput,
299};
300
Tejun Heo3e519032007-06-14 03:45:15 +0900301struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Tejun Heo0c096b52007-06-14 03:45:15 +0900303 char *dup_name = NULL;
Akinobu Mita01da2422007-07-14 11:03:35 +0900304 struct sysfs_dirent *sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900305
306 if (type & SYSFS_COPY_NAME) {
307 name = dup_name = kstrdup(name, GFP_KERNEL);
308 if (!name)
Akinobu Mita01da2422007-07-14 11:03:35 +0900309 return NULL;
Tejun Heo0c096b52007-06-14 03:45:15 +0900310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800312 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!sd)
Akinobu Mita01da2422007-07-14 11:03:35 +0900314 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Tejun Heo0c096b52007-06-14 03:45:15 +0900316 if (sysfs_alloc_ino(&sd->s_ino))
Akinobu Mita01da2422007-07-14 11:03:35 +0900317 goto err_out2;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900320 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300321 atomic_set(&sd->s_event, 1);
Tejun Heoa26cd722007-06-14 03:45:14 +0900322
Tejun Heo0c096b52007-06-14 03:45:15 +0900323 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900324 sd->s_mode = mode;
Tejun Heob402d722007-06-14 04:27:21 +0900325 sd->s_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900328
Akinobu Mita01da2422007-07-14 11:03:35 +0900329 err_out2:
Tejun Heo0c096b52007-06-14 03:45:15 +0900330 kmem_cache_free(sysfs_dir_cachep, sd);
Akinobu Mita01da2422007-07-14 11:03:35 +0900331 err_out1:
332 kfree(dup_name);
Tejun Heo0c096b52007-06-14 03:45:15 +0900333 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Tejun Heofb6896d2007-06-14 04:27:24 +0900336static int sysfs_ilookup_test(struct inode *inode, void *arg)
337{
338 struct sysfs_dirent *sd = arg;
339 return inode->i_ino == sd->s_ino;
340}
341
Tejun Heo3007e992007-06-14 04:27:23 +0900342/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900343 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
344 * @acxt: pointer to sysfs_addrm_cxt to be used
345 * @parent_sd: parent sysfs_dirent
Tejun Heo3007e992007-06-14 04:27:23 +0900346 *
Tejun Heofb6896d2007-06-14 04:27:24 +0900347 * This function is called when the caller is about to add or
348 * remove sysfs_dirent under @parent_sd. This function acquires
349 * sysfs_mutex, grabs inode for @parent_sd if available and lock
350 * i_mutex of it. @acxt is used to keep and pass context to
351 * other addrm functions.
Tejun Heo3007e992007-06-14 04:27:23 +0900352 *
353 * LOCKING:
Tejun Heofb6896d2007-06-14 04:27:24 +0900354 * Kernel thread context (may sleep). sysfs_mutex is locked on
355 * return. i_mutex of parent inode is locked on return if
356 * available.
Tejun Heo3007e992007-06-14 04:27:23 +0900357 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900358void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
359 struct sysfs_dirent *parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700360{
Tejun Heofb6896d2007-06-14 04:27:24 +0900361 struct inode *inode;
Tejun Heoa26cd722007-06-14 03:45:14 +0900362
Tejun Heofb6896d2007-06-14 04:27:24 +0900363 memset(acxt, 0, sizeof(*acxt));
364 acxt->parent_sd = parent_sd;
365
366 /* Lookup parent inode. inode initialization and I_NEW
367 * clearing are protected by sysfs_mutex. By grabbing it and
368 * looking up with _nowait variant, inode state can be
369 * determined reliably.
370 */
371 mutex_lock(&sysfs_mutex);
372
373 inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test,
374 parent_sd);
375
376 if (inode && !(inode->i_state & I_NEW)) {
377 /* parent inode available */
378 acxt->parent_inode = inode;
379
380 /* sysfs_mutex is below i_mutex in lock hierarchy.
381 * First, trylock i_mutex. If fails, unlock
382 * sysfs_mutex and lock them in order.
383 */
384 if (!mutex_trylock(&inode->i_mutex)) {
385 mutex_unlock(&sysfs_mutex);
386 mutex_lock(&inode->i_mutex);
387 mutex_lock(&sysfs_mutex);
388 }
389 } else
390 iput(inode);
391}
392
393/**
394 * sysfs_add_one - add sysfs_dirent to parent
395 * @acxt: addrm context to use
396 * @sd: sysfs_dirent to be added
397 *
398 * Get @acxt->parent_sd and set sd->s_parent to it and increment
Tejun Heo181b2e42007-09-20 16:05:09 +0900399 * nlink of parent inode if @sd is a directory and link into the
400 * children list of the parent.
Tejun Heofb6896d2007-06-14 04:27:24 +0900401 *
402 * This function should be called between calls to
403 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
404 * passed the same @acxt as passed to sysfs_addrm_start().
405 *
406 * LOCKING:
407 * Determined by sysfs_addrm_start().
Tejun Heo23dc2792007-08-02 21:38:03 +0900408 *
409 * RETURNS:
410 * 0 on success, -EEXIST if entry with the given name already
411 * exists.
Tejun Heofb6896d2007-06-14 04:27:24 +0900412 */
Tejun Heo23dc2792007-08-02 21:38:03 +0900413int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900414{
Greg Kroah-Hartman5c3e8962007-09-13 02:53:13 -0700415 if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) {
416 printk(KERN_WARNING "sysfs: duplicate filename '%s' "
417 "can not be created\n", sd->s_name);
418 WARN_ON(1);
Tejun Heo23dc2792007-08-02 21:38:03 +0900419 return -EEXIST;
Greg Kroah-Hartman5c3e8962007-09-13 02:53:13 -0700420 }
Tejun Heo23dc2792007-08-02 21:38:03 +0900421
Tejun Heofb6896d2007-06-14 04:27:24 +0900422 sd->s_parent = sysfs_get(acxt->parent_sd);
423
424 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
425 inc_nlink(acxt->parent_inode);
426
427 acxt->cnt++;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900428
429 sysfs_link_sibling(sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900430
431 return 0;
Tejun Heofb6896d2007-06-14 04:27:24 +0900432}
433
434/**
435 * sysfs_remove_one - remove sysfs_dirent from parent
436 * @acxt: addrm context to use
437 * @sd: sysfs_dirent to be added
438 *
439 * Mark @sd removed and drop nlink of parent inode if @sd is a
Tejun Heo181b2e42007-09-20 16:05:09 +0900440 * directory. @sd is unlinked from the children list.
Tejun Heofb6896d2007-06-14 04:27:24 +0900441 *
442 * This function should be called between calls to
443 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
444 * passed the same @acxt as passed to sysfs_addrm_start().
445 *
446 * LOCKING:
447 * Determined by sysfs_addrm_start().
448 */
449void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
450{
Tejun Heo41fc1c22007-08-02 21:38:03 +0900451 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED);
452
453 sysfs_unlink_sibling(sd);
Tejun Heofb6896d2007-06-14 04:27:24 +0900454
455 sd->s_flags |= SYSFS_FLAG_REMOVED;
456 sd->s_sibling = acxt->removed;
457 acxt->removed = sd;
458
459 if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
460 drop_nlink(acxt->parent_inode);
461
462 acxt->cnt++;
463}
464
465/**
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900466 * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
467 * @sd: target sysfs_dirent
468 *
469 * Drop dentry for @sd. @sd must have been unlinked from its
470 * parent on entry to this function such that it can't be looked
471 * up anymore.
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900472 */
473static void sysfs_drop_dentry(struct sysfs_dirent *sd)
474{
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900475 struct inode *inode;
Eric W. Biederman89bec092007-08-20 21:36:30 +0900476 struct dentry *dentry;
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900477
Eric W. Biederman89bec092007-08-20 21:36:30 +0900478 inode = ilookup(sysfs_sb, sd->s_ino);
479 if (!inode)
480 return;
481
482 /* Drop any existing dentries associated with sd.
483 *
484 * For the dentry to be properly freed we need to grab a
485 * reference to the dentry under the dcache lock, unhash it,
486 * and then put it. The playing with the dentry count allows
487 * dput to immediately free the dentry if it is not in use.
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900488 */
Eric W. Biederman89bec092007-08-20 21:36:30 +0900489repeat:
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900490 spin_lock(&dcache_lock);
Eric W. Biederman89bec092007-08-20 21:36:30 +0900491 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
492 if (d_unhashed(dentry))
493 continue;
494 dget_locked(dentry);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900495 spin_lock(&dentry->d_lock);
496 __d_drop(dentry);
497 spin_unlock(&dentry->d_lock);
Eric W. Biederman89bec092007-08-20 21:36:30 +0900498 spin_unlock(&dcache_lock);
499 dput(dentry);
500 goto repeat;
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900501 }
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900502 spin_unlock(&dcache_lock);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900503
504 /* adjust nlink and update timestamp */
Eric W. Biederman89bec092007-08-20 21:36:30 +0900505 mutex_lock(&inode->i_mutex);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900506
Eric W. Biederman89bec092007-08-20 21:36:30 +0900507 inode->i_ctime = CURRENT_TIME;
508 drop_nlink(inode);
509 if (sysfs_type(sd) == SYSFS_DIR)
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900510 drop_nlink(inode);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900511
Eric W. Biederman89bec092007-08-20 21:36:30 +0900512 mutex_unlock(&inode->i_mutex);
513
514 iput(inode);
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900515}
516
517/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900518 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
519 * @acxt: addrm context to finish up
520 *
521 * Finish up sysfs_dirent add/remove. Resources acquired by
522 * sysfs_addrm_start() are released and removed sysfs_dirents are
523 * cleaned up. Timestamps on the parent inode are updated.
524 *
525 * LOCKING:
526 * All mutexes acquired by sysfs_addrm_start() are released.
Tejun Heofb6896d2007-06-14 04:27:24 +0900527 */
Tejun Heo990e53f2007-08-02 21:38:03 +0900528void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
Tejun Heofb6896d2007-06-14 04:27:24 +0900529{
530 /* release resources acquired by sysfs_addrm_start() */
531 mutex_unlock(&sysfs_mutex);
532 if (acxt->parent_inode) {
533 struct inode *inode = acxt->parent_inode;
534
535 /* if added/removed, update timestamps on the parent */
536 if (acxt->cnt)
537 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
538
539 mutex_unlock(&inode->i_mutex);
540 iput(inode);
Tejun Heo13b30862007-06-14 03:45:14 +0900541 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900542
543 /* kill removed sysfs_dirents */
544 while (acxt->removed) {
545 struct sysfs_dirent *sd = acxt->removed;
546
547 acxt->removed = sd->s_sibling;
548 sd->s_sibling = NULL;
549
550 sysfs_drop_dentry(sd);
551 sysfs_deactivate(sd);
552 sysfs_put(sd);
553 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700554}
555
Tejun Heof0b0af42007-06-14 04:27:22 +0900556/**
557 * sysfs_find_dirent - find sysfs_dirent with the given name
558 * @parent_sd: sysfs_dirent to search under
559 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530560 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900561 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530562 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900563 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900564 * mutex_lock(sysfs_mutex)
Tejun Heof0b0af42007-06-14 04:27:22 +0900565 *
566 * RETURNS:
567 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530568 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900569struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
570 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530571{
Tejun Heof0b0af42007-06-14 04:27:22 +0900572 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530573
Tejun Heobc747f32007-09-20 16:05:12 +0900574 for (sd = parent_sd->s_dir.children; sd; sd = sd->s_sibling)
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900575 if (!strcmp(sd->s_name, name))
Tejun Heof0b0af42007-06-14 04:27:22 +0900576 return sd;
577 return NULL;
578}
Maneesh Sonic5168652006-03-09 19:40:14 +0530579
Tejun Heof0b0af42007-06-14 04:27:22 +0900580/**
581 * sysfs_get_dirent - find and get sysfs_dirent with the given name
582 * @parent_sd: sysfs_dirent to search under
583 * @name: name to look for
584 *
585 * Look for sysfs_dirent with name @name under @parent_sd and get
586 * it if found.
587 *
588 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900589 * Kernel thread context (may sleep). Grabs sysfs_mutex.
Tejun Heof0b0af42007-06-14 04:27:22 +0900590 *
591 * RETURNS:
592 * Pointer to sysfs_dirent if found, NULL if not.
593 */
594struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
595 const unsigned char *name)
596{
597 struct sysfs_dirent *sd;
598
Tejun Heo3007e992007-06-14 04:27:23 +0900599 mutex_lock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900600 sd = sysfs_find_dirent(parent_sd, name);
601 sysfs_get(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900602 mutex_unlock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900603
604 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530605}
606
Tejun Heo608e2662007-06-14 04:27:22 +0900607static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
608 const char *name, struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heo51225032007-06-14 04:27:25 +0900611 struct sysfs_addrm_cxt acxt;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900612 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900613 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Tejun Heofc9f54b2007-06-14 03:45:17 +0900615 /* allocate */
Tejun Heo3e519032007-06-14 03:45:15 +0900616 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900617 if (!sd)
Tejun Heo51225032007-06-14 04:27:25 +0900618 return -ENOMEM;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900619 sd->s_dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900620
Tejun Heo51225032007-06-14 04:27:25 +0900621 /* link in */
622 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +0900623 rc = sysfs_add_one(&acxt, sd);
624 sysfs_addrm_finish(&acxt);
Tejun Heo967e35d2007-07-18 16:38:11 +0900625
Tejun Heo23dc2792007-08-02 21:38:03 +0900626 if (rc == 0)
627 *p_sd = sd;
628 else
Tejun Heo967e35d2007-07-18 16:38:11 +0900629 sysfs_put(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900630
Tejun Heo23dc2792007-08-02 21:38:03 +0900631 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Tejun Heo608e2662007-06-14 04:27:22 +0900634int sysfs_create_subdir(struct kobject *kobj, const char *name,
635 struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Tejun Heo608e2662007-06-14 04:27:22 +0900637 return create_dir(kobj, kobj->sd, name, p_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/**
641 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * @kobj: object we're creating directory for.
643 */
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900644int sysfs_create_dir(struct kobject * kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Tejun Heo608e2662007-06-14 04:27:22 +0900646 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int error = 0;
648
649 BUG_ON(!kobj);
650
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900651 if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900652 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 else
Eric W. Biederman7d0c7d62007-08-20 21:36:30 +0900654 parent_sd = &sysfs_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Tejun Heo608e2662007-06-14 04:27:22 +0900656 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!error)
Tejun Heo608e2662007-06-14 04:27:22 +0900658 kobj->sd = sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return error;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
663 struct nameidata *nd)
664{
Tejun Heo6cb52142007-07-31 19:15:08 +0900665 struct dentry *ret = NULL;
Tejun Heoa7a04752007-08-02 21:38:02 +0900666 struct sysfs_dirent *parent_sd = dentry->d_parent->d_fsdata;
667 struct sysfs_dirent *sd;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900668 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Tejun Heo6cb52142007-07-31 19:15:08 +0900670 mutex_lock(&sysfs_mutex);
671
Eric W. Biederman94777e02007-08-20 21:36:30 +0900672 sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Tejun Heofc9f54b2007-06-14 03:45:17 +0900674 /* no such entry */
Tejun Heoa7a04752007-08-02 21:38:02 +0900675 if (!sd)
Tejun Heo6cb52142007-07-31 19:15:08 +0900676 goto out_unlock;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900677
678 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900679 inode = sysfs_get_inode(sd);
Tejun Heo6cb52142007-07-31 19:15:08 +0900680 if (!inode) {
681 ret = ERR_PTR(-ENOMEM);
682 goto out_unlock;
683 }
Tejun Heo3007e992007-06-14 04:27:23 +0900684
Tejun Heod6b4fd22007-09-20 16:05:11 +0900685 /* instantiate and hash dentry */
686 dentry->d_op = &sysfs_dentry_ops;
687 dentry->d_fsdata = sysfs_get(sd);
Eric W. Biederman119dd522007-08-20 21:36:29 +0900688 d_instantiate(dentry, inode);
Tejun Heod6b4fd22007-09-20 16:05:11 +0900689 d_rehash(dentry);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900690
Tejun Heo6cb52142007-07-31 19:15:08 +0900691 out_unlock:
Tejun Heo3007e992007-06-14 04:27:23 +0900692 mutex_unlock(&sysfs_mutex);
Tejun Heo6cb52142007-07-31 19:15:08 +0900693 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800696const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530698 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
Tejun Heo608e2662007-06-14 04:27:22 +0900701static void remove_dir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Tejun Heofb6896d2007-06-14 04:27:24 +0900703 struct sysfs_addrm_cxt acxt;
704
705 sysfs_addrm_start(&acxt, sd->s_parent);
Tejun Heofb6896d2007-06-14 04:27:24 +0900706 sysfs_remove_one(&acxt, sd);
707 sysfs_addrm_finish(&acxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Tejun Heo608e2662007-06-14 04:27:22 +0900710void sysfs_remove_subdir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Tejun Heo608e2662007-06-14 04:27:22 +0900712 remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715
Tejun Heo608e2662007-06-14 04:27:22 +0900716static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Tejun Heofb6896d2007-06-14 04:27:24 +0900718 struct sysfs_addrm_cxt acxt;
Tejun Heo0c73f182007-06-14 03:45:18 +0900719 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Tejun Heo608e2662007-06-14 04:27:22 +0900721 if (!dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return;
723
Tejun Heo608e2662007-06-14 04:27:22 +0900724 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
Tejun Heofb6896d2007-06-14 04:27:24 +0900725 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heobc747f32007-09-20 16:05:12 +0900726 pos = &dir_sd->s_dir.children;
Tejun Heo0c73f182007-06-14 03:45:18 +0900727 while (*pos) {
728 struct sysfs_dirent *sd = *pos;
729
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900730 if (sysfs_type(sd) != SYSFS_DIR)
Tejun Heofb6896d2007-06-14 04:27:24 +0900731 sysfs_remove_one(&acxt, sd);
Tejun Heo41fc1c22007-08-02 21:38:03 +0900732 else
Tejun Heo0c73f182007-06-14 03:45:18 +0900733 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Tejun Heofb6896d2007-06-14 04:27:24 +0900735 sysfs_addrm_finish(&acxt);
Tejun Heo0ab66082007-06-14 03:45:16 +0900736
Tejun Heo608e2662007-06-14 04:27:22 +0900737 remove_dir(dir_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700738}
739
740/**
741 * sysfs_remove_dir - remove an object's directory.
742 * @kobj: object.
743 *
744 * The only thing special about this is that we remove any files in
745 * the directory before we remove the directory, and we've inlined
746 * what used to be sysfs_rmdir() below, instead of calling separately.
747 */
748
749void sysfs_remove_dir(struct kobject * kobj)
750{
Tejun Heo608e2662007-06-14 04:27:22 +0900751 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900752
Tejun Heo5f995322007-06-14 04:27:23 +0900753 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900754 kobj->sd = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900755 spin_unlock(&sysfs_assoc_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900756
Tejun Heo608e2662007-06-14 04:27:22 +0900757 __sysfs_remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900760int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900762 struct sysfs_dirent *sd = kobj->sd;
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900763 struct dentry *parent = NULL;
Tejun Heo51225032007-06-14 04:27:25 +0900764 struct dentry *old_dentry = NULL, *new_dentry = NULL;
765 const char *dup_name = NULL;
Tejun Heo996b7372007-06-14 03:45:14 +0900766 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900768 mutex_lock(&sysfs_rename_mutex);
769
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900770 error = 0;
771 if (strcmp(sd->s_name, new_name) == 0)
772 goto out; /* nothing to rename */
773
Tejun Heoff869de2007-08-02 21:38:02 +0900774 /* get the original dentry */
Tejun Heo51225032007-06-14 04:27:25 +0900775 old_dentry = sysfs_get_dentry(sd);
776 if (IS_ERR(old_dentry)) {
777 error = PTR_ERR(old_dentry);
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900778 goto out;
Tejun Heo51225032007-06-14 04:27:25 +0900779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Tejun Heoff869de2007-08-02 21:38:02 +0900781 parent = old_dentry->d_parent;
Tejun Heo51225032007-06-14 04:27:25 +0900782
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900783 /* lock parent and get dentry for new name */
784 mutex_lock(&parent->d_inode->i_mutex);
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900785 mutex_lock(&sysfs_mutex);
Tejun Heo996b7372007-06-14 03:45:14 +0900786
787 error = -EEXIST;
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900788 if (sysfs_find_dirent(sd->s_parent, new_name))
789 goto out_unlock;
790
791 error = -ENOMEM;
792 new_dentry = d_alloc_name(parent, new_name);
793 if (!new_dentry)
Tejun Heo51225032007-06-14 04:27:25 +0900794 goto out_unlock;
Tejun Heo996b7372007-06-14 03:45:14 +0900795
Tejun Heo0c096b52007-06-14 03:45:15 +0900796 /* rename kobject and sysfs_dirent */
797 error = -ENOMEM;
798 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
799 if (!new_name)
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900800 goto out_unlock;
Tejun Heo996b7372007-06-14 03:45:14 +0900801
Tejun Heo0c096b52007-06-14 03:45:15 +0900802 error = kobject_set_name(kobj, "%s", new_name);
803 if (error)
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900804 goto out_unlock;
Tejun Heo0c096b52007-06-14 03:45:15 +0900805
Tejun Heo51225032007-06-14 04:27:25 +0900806 dup_name = sd->s_name;
Tejun Heo0c096b52007-06-14 03:45:15 +0900807 sd->s_name = new_name;
808
Tejun Heoff869de2007-08-02 21:38:02 +0900809 /* rename */
Tejun Heo996b7372007-06-14 03:45:14 +0900810 d_add(new_dentry, NULL);
Eric W. Biederman5a26b792007-08-20 21:36:30 +0900811 d_move(old_dentry, new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900812
Tejun Heo996b7372007-06-14 03:45:14 +0900813 error = 0;
Tejun Heo996b7372007-06-14 03:45:14 +0900814 out_unlock:
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900815 mutex_unlock(&sysfs_mutex);
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900816 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900817 kfree(dup_name);
Tejun Heo51225032007-06-14 04:27:25 +0900818 dput(old_dentry);
819 dput(new_dentry);
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900820 out:
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900821 mutex_unlock(&sysfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return error;
823}
824
Tejun Heo51225032007-06-14 04:27:25 +0900825int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
Cornelia Huck8a824722006-11-20 17:07:51 +0100826{
Tejun Heo51225032007-06-14 04:27:25 +0900827 struct sysfs_dirent *sd = kobj->sd;
828 struct sysfs_dirent *new_parent_sd;
829 struct dentry *old_parent, *new_parent = NULL;
830 struct dentry *old_dentry = NULL, *new_dentry = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100831 int error;
832
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900833 mutex_lock(&sysfs_rename_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900834 BUG_ON(!sd->s_parent);
835 new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100836
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900837 error = 0;
838 if (sd->s_parent == new_parent_sd)
839 goto out; /* nothing to move */
840
Tejun Heo51225032007-06-14 04:27:25 +0900841 /* get dentries */
842 old_dentry = sysfs_get_dentry(sd);
843 if (IS_ERR(old_dentry)) {
844 error = PTR_ERR(old_dentry);
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900845 goto out;
Tejun Heo51225032007-06-14 04:27:25 +0900846 }
Eric W. Biederman5a26b792007-08-20 21:36:30 +0900847 old_parent = old_dentry->d_parent;
Tejun Heo51225032007-06-14 04:27:25 +0900848
849 new_parent = sysfs_get_dentry(new_parent_sd);
850 if (IS_ERR(new_parent)) {
851 error = PTR_ERR(new_parent);
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900852 goto out;
Tejun Heo51225032007-06-14 04:27:25 +0900853 }
854
Cornelia Huck8a824722006-11-20 17:07:51 +0100855again:
Tejun Heo51225032007-06-14 04:27:25 +0900856 mutex_lock(&old_parent->d_inode->i_mutex);
857 if (!mutex_trylock(&new_parent->d_inode->i_mutex)) {
858 mutex_unlock(&old_parent->d_inode->i_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100859 goto again;
860 }
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900861 mutex_lock(&sysfs_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100862
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900863 error = -EEXIST;
864 if (sysfs_find_dirent(new_parent_sd, sd->s_name))
Tejun Heo51225032007-06-14 04:27:25 +0900865 goto out_unlock;
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900866
867 error = -ENOMEM;
868 new_dentry = d_alloc_name(new_parent, sd->s_name);
869 if (!new_dentry)
870 goto out_unlock;
871
872 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +0100873 d_add(new_dentry, NULL);
Eric W. Biederman5a26b792007-08-20 21:36:30 +0900874 d_move(old_dentry, new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100875 dput(new_dentry);
876
877 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo0c73f182007-06-14 03:45:18 +0900878 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900879 sysfs_get(new_parent_sd);
880 sysfs_put(sd->s_parent);
881 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900882 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +0100883
Tejun Heo51225032007-06-14 04:27:25 +0900884 out_unlock:
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900885 mutex_unlock(&sysfs_mutex);
Tejun Heo51225032007-06-14 04:27:25 +0900886 mutex_unlock(&new_parent->d_inode->i_mutex);
887 mutex_unlock(&old_parent->d_inode->i_mutex);
Eric W. Biederman45aaae92007-08-20 21:36:31 +0900888 out:
Tejun Heo51225032007-06-14 04:27:25 +0900889 dput(new_parent);
890 dput(old_dentry);
891 dput(new_dentry);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900892 mutex_unlock(&sysfs_rename_mutex);
Cornelia Huck8a824722006-11-20 17:07:51 +0100893 return error;
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896/* Relationship between s_mode and the DT_xxx types */
897static inline unsigned char dt_type(struct sysfs_dirent *sd)
898{
899 return (sd->s_mode >> 12) & 15;
900}
901
902static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
903{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800904 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900906 struct sysfs_dirent *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900909 if (filp->f_pos == 0) {
910 ino = parent_sd->s_ino;
911 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
912 filp->f_pos++;
913 }
914 if (filp->f_pos == 1) {
915 if (parent_sd->s_parent)
916 ino = parent_sd->s_parent->s_ino;
917 else
Eric Sandeendc351252007-06-11 14:02:45 +0900918 ino = parent_sd->s_ino;
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900919 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 filp->f_pos++;
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900921 }
922 if ((filp->f_pos > 1) && (filp->f_pos < INT_MAX)) {
923 mutex_lock(&sysfs_mutex);
924
925 /* Skip the dentries we have already reported */
Tejun Heobc747f32007-09-20 16:05:12 +0900926 pos = parent_sd->s_dir.children;
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900927 while (pos && (filp->f_pos > pos->s_ino))
928 pos = pos->s_sibling;
929
930 for ( ; pos; pos = pos->s_sibling) {
931 const char * name;
932 int len;
933
934 name = pos->s_name;
935 len = strlen(name);
936 filp->f_pos = ino = pos->s_ino;
937
938 if (filldir(dirent, name, len, filp->f_pos, ino,
939 dt_type(pos)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
Eric W. Biederman3efa65b2007-08-20 21:36:30 +0900941 }
942 if (!pos)
943 filp->f_pos = INT_MAX;
944 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 return 0;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800950const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 .read = generic_read_dir,
952 .readdir = sysfs_readdir,
953};