blob: cfbf4091fe5c5ca0ae9b45376e574b2fe01715f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/dir.c - sysfs core and dir operation implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#undef DEBUG
14
15#include <linux/fs.h>
16#include <linux/mount.h>
17#include <linux/module.h>
18#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070019#include <linux/namei.h>
Tejun Heo2b611bb2007-06-14 03:45:13 +090020#include <linux/idr.h>
Tejun Heo8619f972007-06-14 03:45:18 +090021#include <linux/completion.h>
Dave Young869512a2007-07-26 14:53:53 +000022#include <linux/mutex.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040023#include <linux/slab.h>
Eric W. Biederman4c3da222009-11-04 02:50:06 -080024#include <linux/security.h>
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080025#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "sysfs.h"
27
Tejun Heo3007e992007-06-14 04:27:23 +090028DEFINE_MUTEX(sysfs_mutex);
Tejun Heo0cae60f2013-10-30 10:28:36 -040029DEFINE_SPINLOCK(sysfs_symlink_target_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Tejun Heobcac3762013-09-11 22:29:03 -040031#define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080032
Roel Kluinf7a75f02007-10-16 23:30:25 -070033static DEFINE_SPINLOCK(sysfs_ino_lock);
Tejun Heo2b611bb2007-06-14 03:45:13 +090034static DEFINE_IDA(sysfs_ino_ida);
35
Tejun Heob6b4a432007-06-14 03:45:18 +090036/**
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080037 * sysfs_name_hash
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080038 * @name: Null terminated string to hash
Tejun Heocfec0bc2013-09-11 22:29:09 -040039 * @ns: Namespace tag to hash
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080040 *
41 * Returns 31 bit hash of ns + name (so it fits in an off_t )
42 */
Tejun Heocfec0bc2013-09-11 22:29:09 -040043static unsigned int sysfs_name_hash(const char *name, const void *ns)
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080044{
45 unsigned long hash = init_name_hash();
46 unsigned int len = strlen(name);
47 while (len--)
48 hash = partial_name_hash(*name++, hash);
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070049 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080050 hash &= 0x7fffffffU;
51 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
52 if (hash < 1)
53 hash += 2;
54 if (hash >= INT_MAX)
55 hash = INT_MAX - 1;
56 return hash;
57}
58
Tejun Heocfec0bc2013-09-11 22:29:09 -040059static int sysfs_name_compare(unsigned int hash, const char *name,
60 const void *ns, const struct sysfs_dirent *sd)
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080061{
62 if (hash != sd->s_hash)
63 return hash - sd->s_hash;
64 if (ns != sd->s_ns)
65 return ns - sd->s_ns;
66 return strcmp(name, sd->s_name);
67}
68
69static int sysfs_sd_compare(const struct sysfs_dirent *left,
70 const struct sysfs_dirent *right)
71{
Tejun Heocfec0bc2013-09-11 22:29:09 -040072 return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns,
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080073 right);
74}
75
76/**
Warner Wang43474912013-05-13 11:11:05 +080077 * sysfs_link_sibling - link sysfs_dirent into sibling rbtree
Tejun Heo0c73f182007-06-14 03:45:18 +090078 * @sd: sysfs_dirent of interest
79 *
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080080 * Link @sd into its sibling rbtree which starts from
Tejun Heobc747f32007-09-20 16:05:12 +090081 * sd->s_parent->s_dir.children.
Tejun Heo0c73f182007-06-14 03:45:18 +090082 *
83 * Locking:
Tejun Heo3007e992007-06-14 04:27:23 +090084 * mutex_lock(sysfs_mutex)
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080085 *
86 * RETURNS:
87 * 0 on susccess -EEXIST on failure.
Tejun Heo0c73f182007-06-14 03:45:18 +090088 */
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080089static int sysfs_link_sibling(struct sysfs_dirent *sd)
Tejun Heo0c73f182007-06-14 03:45:18 +090090{
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080091 struct rb_node **node = &sd->s_parent->s_dir.children.rb_node;
92 struct rb_node *parent = NULL;
Mikulas Patocka4f72c0c2011-07-25 17:55:57 -040093
Greg Kroah-Hartman54d20f02012-03-08 13:03:10 -080094 if (sysfs_type(sd) == SYSFS_DIR)
95 sd->s_parent->s_dir.subdirs++;
96
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -080097 while (*node) {
98 struct sysfs_dirent *pos;
99 int result;
Mikulas Patocka4f72c0c2011-07-25 17:55:57 -0400100
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800101 pos = to_sysfs_dirent(*node);
102 parent = *node;
103 result = sysfs_sd_compare(sd, pos);
104 if (result < 0)
105 node = &pos->s_rb.rb_left;
106 else if (result > 0)
107 node = &pos->s_rb.rb_right;
108 else
109 return -EEXIST;
Mikulas Patocka4f72c0c2011-07-25 17:55:57 -0400110 }
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800111 /* add new node and rebalance the tree */
112 rb_link_node(&sd->s_rb, parent, node);
113 rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children);
114 return 0;
Tejun Heo0c73f182007-06-14 03:45:18 +0900115}
116
117/**
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800118 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
Tejun Heo0c73f182007-06-14 03:45:18 +0900119 * @sd: sysfs_dirent of interest
120 *
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800121 * Unlink @sd from its sibling rbtree which starts from
Tejun Heobc747f32007-09-20 16:05:12 +0900122 * sd->s_parent->s_dir.children.
Tejun Heo0c73f182007-06-14 03:45:18 +0900123 *
124 * Locking:
Tejun Heo3007e992007-06-14 04:27:23 +0900125 * mutex_lock(sysfs_mutex)
Tejun Heo0c73f182007-06-14 03:45:18 +0900126 */
Tejun Heo41fc1c22007-08-02 21:38:03 +0900127static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
Tejun Heo0c73f182007-06-14 03:45:18 +0900128{
Greg Kroah-Hartman54d20f02012-03-08 13:03:10 -0800129 if (sysfs_type(sd) == SYSFS_DIR)
130 sd->s_parent->s_dir.subdirs--;
131
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800132 rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
Tejun Heo0c73f182007-06-14 03:45:18 +0900133}
134
135/**
Tejun Heob6b4a432007-06-14 03:45:18 +0900136 * sysfs_get_active - get an active reference to sysfs_dirent
137 * @sd: sysfs_dirent to get an active reference to
138 *
139 * Get an active reference of @sd. This function is noop if @sd
140 * is NULL.
141 *
142 * RETURNS:
143 * Pointer to @sd on success, NULL on failure.
144 */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800145struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900146{
Tejun Heo8619f972007-06-14 03:45:18 +0900147 if (unlikely(!sd))
148 return NULL;
149
Maarten Lankhorst3db3c622013-03-08 16:07:27 +0100150 if (!atomic_inc_unless_negative(&sd->s_active))
151 return NULL;
Alan Stern356c05d2012-05-14 13:30:03 -0400152
Tejun Heo785a1622013-10-14 09:27:11 -0400153 if (likely(!sysfs_ignore_lockdep(sd)))
Alan Stern356c05d2012-05-14 13:30:03 -0400154 rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
155 return sd;
Tejun Heob6b4a432007-06-14 03:45:18 +0900156}
157
158/**
159 * sysfs_put_active - put an active reference to sysfs_dirent
160 * @sd: sysfs_dirent to put an active reference to
161 *
162 * Put an active reference to @sd. This function is noop if @sd
163 * is NULL.
164 */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800165void sysfs_put_active(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900166{
Tejun Heo8619f972007-06-14 03:45:18 +0900167 int v;
168
169 if (unlikely(!sd))
170 return;
171
Tejun Heo785a1622013-10-14 09:27:11 -0400172 if (likely(!sysfs_ignore_lockdep(sd)))
Alan Stern356c05d2012-05-14 13:30:03 -0400173 rwsem_release(&sd->dep_map, 1, _RET_IP_);
Tejun Heo8619f972007-06-14 03:45:18 +0900174 v = atomic_dec_return(&sd->s_active);
175 if (likely(v != SD_DEACTIVATED_BIAS))
176 return;
177
178 /* atomic_dec_return() is a mb(), we'll always see the updated
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400179 * sd->u.completion.
Tejun Heo8619f972007-06-14 03:45:18 +0900180 */
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400181 complete(sd->u.completion);
Tejun Heob6b4a432007-06-14 03:45:18 +0900182}
183
184/**
Tejun Heob6b4a432007-06-14 03:45:18 +0900185 * sysfs_deactivate - deactivate sysfs_dirent
186 * @sd: sysfs_dirent to deactivate
187 *
Tejun Heo8619f972007-06-14 03:45:18 +0900188 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900189 */
Tejun Heofb6896d2007-06-14 04:27:24 +0900190static void sysfs_deactivate(struct sysfs_dirent *sd)
Tejun Heob6b4a432007-06-14 03:45:18 +0900191{
Tejun Heo8619f972007-06-14 03:45:18 +0900192 DECLARE_COMPLETION_ONSTACK(wait);
193 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900194
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400195 BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
Eric W. Biedermana2db6842010-02-11 15:20:00 -0800196
197 if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
198 return;
199
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400200 sd->u.completion = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900201
Eric W. Biederman846f9972010-01-02 13:37:12 -0800202 rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
Tejun Heo8619f972007-06-14 03:45:18 +0900203 /* atomic_add_return() is a mb(), put_active() will always see
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400204 * the updated sd->u.completion.
Tejun Heob6b4a432007-06-14 03:45:18 +0900205 */
Tejun Heo8619f972007-06-14 03:45:18 +0900206 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
207
Eric W. Biederman846f9972010-01-02 13:37:12 -0800208 if (v != SD_DEACTIVATED_BIAS) {
209 lock_contended(&sd->dep_map, _RET_IP_);
Tejun Heo8619f972007-06-14 03:45:18 +0900210 wait_for_completion(&wait);
Eric W. Biederman846f9972010-01-02 13:37:12 -0800211 }
Tejun Heo8619f972007-06-14 03:45:18 +0900212
Eric W. Biederman846f9972010-01-02 13:37:12 -0800213 lock_acquired(&sd->dep_map, _RET_IP_);
214 rwsem_release(&sd->dep_map, 1, _RET_IP_);
Tejun Heob6b4a432007-06-14 03:45:18 +0900215}
216
Eric W. Biedermancafa6b52011-12-18 20:08:16 -0800217static int sysfs_alloc_ino(unsigned int *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900218{
219 int ino, rc;
220
221 retry:
222 spin_lock(&sysfs_ino_lock);
223 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
224 spin_unlock(&sysfs_ino_lock);
225
226 if (rc == -EAGAIN) {
227 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
228 goto retry;
229 rc = -ENOMEM;
230 }
231
232 *pino = ino;
233 return rc;
234}
235
Eric W. Biedermancafa6b52011-12-18 20:08:16 -0800236static void sysfs_free_ino(unsigned int ino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900237{
238 spin_lock(&sysfs_ino_lock);
239 ida_remove(&sysfs_ino_ida, ino);
240 spin_unlock(&sysfs_ino_lock);
241}
242
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700243void release_sysfs_dirent(struct sysfs_dirent *sd)
Tejun Heofa7f9122007-06-14 03:45:13 +0900244{
Tejun Heo13b30862007-06-14 03:45:14 +0900245 struct sysfs_dirent *parent_sd;
246
247 repeat:
Tejun Heo3007e992007-06-14 04:27:23 +0900248 /* Moving/renaming is always done while holding reference.
249 * sd->s_parent won't change beneath us.
250 */
Tejun Heo13b30862007-06-14 03:45:14 +0900251 parent_sd = sd->s_parent;
252
Ming Leibb2b0052013-04-04 22:22:37 +0800253 WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
254 "sysfs: free using entry: %s/%s\n",
255 parent_sd ? parent_sd->s_name : "", sd->s_name);
256
Tejun Heob402d722007-06-14 04:27:21 +0900257 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heob1fc3d62007-09-20 16:05:11 +0900258 sysfs_put(sd->s_symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900259 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900260 kfree(sd->s_name);
Eric W. Biederman4c3da222009-11-04 02:50:06 -0800261 if (sd->s_iattr && sd->s_iattr->ia_secdata)
262 security_release_secctx(sd->s_iattr->ia_secdata,
263 sd->s_iattr->ia_secdata_len);
Tejun Heofa7f9122007-06-14 03:45:13 +0900264 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900265 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900266 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900267
268 sd = parent_sd;
269 if (sd && atomic_dec_and_test(&sd->s_count))
270 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900271}
272
Nick Pigginfe15ce42011-01-07 17:49:23 +1100273static int sysfs_dentry_delete(const struct dentry *dentry)
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800274{
275 struct sysfs_dirent *sd = dentry->d_fsdata;
Al Viro469796d2012-06-07 20:51:39 -0400276 return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED));
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800277}
278
Al Viro0b728e12012-06-10 16:03:43 -0400279static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800280{
Nick Piggin34286d62011-01-07 17:49:57 +1100281 struct sysfs_dirent *sd;
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800282
Al Viro0b728e12012-06-10 16:03:43 -0400283 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100284 return -ECHILD;
285
286 sd = dentry->d_fsdata;
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800287 mutex_lock(&sysfs_mutex);
288
289 /* The sysfs dirent has been deleted */
290 if (sd->s_flags & SYSFS_FLAG_REMOVED)
291 goto out_bad;
292
Eric W. Biederman832b6af2009-11-20 16:08:56 -0800293 /* The sysfs dirent has been moved? */
294 if (dentry->d_parent->d_fsdata != sd->s_parent)
295 goto out_bad;
296
297 /* The sysfs dirent has been renamed */
298 if (strcmp(dentry->d_name.name, sd->s_name) != 0)
299 goto out_bad;
300
Glauber Costae5bcac62012-07-06 13:09:07 +0400301 /* The sysfs dirent has been moved to a different namespace */
Tejun Heoc84a3b22013-11-23 18:01:46 -0500302 if (sd->s_parent && (sd->s_parent->s_flags & SYSFS_FLAG_NS) &&
303 sysfs_info(dentry->d_sb)->ns != sd->s_ns)
304 goto out_bad;
Glauber Costae5bcac62012-07-06 13:09:07 +0400305
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800306 mutex_unlock(&sysfs_mutex);
307out_valid:
308 return 1;
309out_bad:
310 /* Remove the dentry from the dcache hashes.
311 * If this is a deleted dentry we use d_drop instead of d_delete
312 * so sysfs doesn't need to cope with negative dentries.
Eric W. Biederman832b6af2009-11-20 16:08:56 -0800313 *
314 * If this is a dentry that has simply been renamed we
315 * use d_drop to remove it from the dcache lookup on its
316 * old parent. If this dentry persists later when a lookup
317 * is performed at its new name the dentry will be readded
318 * to the dcache hashes.
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800319 */
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800320 mutex_unlock(&sysfs_mutex);
Miklos Szeredi6497d162013-09-05 11:44:41 +0200321
322 /* If we have submounts we must allow the vfs caches
323 * to lie about the state of the filesystem to prevent
324 * leaks and other nasty things.
325 */
326 if (check_submounts_and_drop(dentry) != 0)
327 goto out_valid;
328
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800329 return 0;
330}
331
Al Viro469796d2012-06-07 20:51:39 -0400332static void sysfs_dentry_release(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Al Viro469796d2012-06-07 20:51:39 -0400334 sysfs_put(dentry->d_fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Al Viro469796d2012-06-07 20:51:39 -0400337const struct dentry_operations sysfs_dentry_ops = {
Eric W. Biedermane8f077c2009-11-07 23:27:01 -0800338 .d_revalidate = sysfs_dentry_revalidate,
339 .d_delete = sysfs_dentry_delete,
Al Viro469796d2012-06-07 20:51:39 -0400340 .d_release = sysfs_dentry_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341};
342
Tejun Heo3e519032007-06-14 03:45:15 +0900343struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Tejun Heo0c096b52007-06-14 03:45:15 +0900345 char *dup_name = NULL;
Akinobu Mita01da2422007-07-14 11:03:35 +0900346 struct sysfs_dirent *sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900347
348 if (type & SYSFS_COPY_NAME) {
349 name = dup_name = kstrdup(name, GFP_KERNEL);
350 if (!name)
Akinobu Mita01da2422007-07-14 11:03:35 +0900351 return NULL;
Tejun Heo0c096b52007-06-14 03:45:15 +0900352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800354 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!sd)
Akinobu Mita01da2422007-07-14 11:03:35 +0900356 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Tejun Heo0c096b52007-06-14 03:45:15 +0900358 if (sysfs_alloc_ino(&sd->s_ino))
Akinobu Mita01da2422007-07-14 11:03:35 +0900359 goto err_out2;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900362 atomic_set(&sd->s_active, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900363
Tejun Heo0c096b52007-06-14 03:45:15 +0900364 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900365 sd->s_mode = mode;
Ming Leibb2b0052013-04-04 22:22:37 +0800366 sd->s_flags = type | SYSFS_FLAG_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900369
Akinobu Mita01da2422007-07-14 11:03:35 +0900370 err_out2:
Tejun Heo0c096b52007-06-14 03:45:15 +0900371 kmem_cache_free(sysfs_dir_cachep, sd);
Akinobu Mita01da2422007-07-14 11:03:35 +0900372 err_out1:
373 kfree(dup_name);
Tejun Heo0c096b52007-06-14 03:45:15 +0900374 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Tejun Heo3007e992007-06-14 04:27:23 +0900377/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900378 * sysfs_addrm_start - prepare for sysfs_dirent add/remove
379 * @acxt: pointer to sysfs_addrm_cxt to be used
Tejun Heo3007e992007-06-14 04:27:23 +0900380 *
Tejun Heod69ac5a2013-09-18 17:15:35 -0400381 * This function is called when the caller is about to add or remove
382 * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used
383 * to keep and pass context to other addrm functions.
Tejun Heo3007e992007-06-14 04:27:23 +0900384 *
385 * LOCKING:
Tejun Heofb6896d2007-06-14 04:27:24 +0900386 * Kernel thread context (may sleep). sysfs_mutex is locked on
Eric W. Biedermana16bbc32009-11-20 16:08:55 -0800387 * return.
Tejun Heo3007e992007-06-14 04:27:23 +0900388 */
Tejun Heod69ac5a2013-09-18 17:15:35 -0400389void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
390 __acquires(sysfs_mutex)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700391{
Tejun Heofb6896d2007-06-14 04:27:24 +0900392 memset(acxt, 0, sizeof(*acxt));
Tejun Heofb6896d2007-06-14 04:27:24 +0900393
Tejun Heofb6896d2007-06-14 04:27:24 +0900394 mutex_lock(&sysfs_mutex);
Tejun Heofb6896d2007-06-14 04:27:24 +0900395}
396
397/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200398 * __sysfs_add_one - add sysfs_dirent to parent without warning
399 * @acxt: addrm context to use
400 * @sd: sysfs_dirent to be added
Tejun Heod69ac5a2013-09-18 17:15:35 -0400401 * @parent_sd: the parent sysfs_dirent to add @sd to
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200402 *
Tejun Heod69ac5a2013-09-18 17:15:35 -0400403 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
404 * the parent inode if @sd is a directory and link into the children
405 * list of the parent.
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200406 *
407 * This function should be called between calls to
408 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
409 * passed the same @acxt as passed to sysfs_addrm_start().
410 *
411 * LOCKING:
412 * Determined by sysfs_addrm_start().
413 *
414 * RETURNS:
415 * 0 on success, -EEXIST if entry with the given name already
416 * exists.
417 */
Tejun Heod69ac5a2013-09-18 17:15:35 -0400418int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
419 struct sysfs_dirent *parent_sd)
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200420{
Tejun Heoc84a3b22013-11-23 18:01:46 -0500421 bool has_ns = parent_sd->s_flags & SYSFS_FLAG_NS;
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800422 struct sysfs_inode_attrs *ps_iattr;
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800423 int ret;
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800424
Tejun Heoc84a3b22013-11-23 18:01:46 -0500425 if (has_ns != (bool)sd->s_ns) {
Linus Torvaldsa1212d22013-11-07 20:47:28 +0900426 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
Tejun Heoc84a3b22013-11-23 18:01:46 -0500427 has_ns ? "required" : "invalid",
428 parent_sd->s_name, sd->s_name);
Linus Torvaldsa1212d22013-11-07 20:47:28 +0900429 return -EINVAL;
430 }
431
Tejun Heoae2108a2013-11-23 17:21:47 -0500432 if (sysfs_type(parent_sd) != SYSFS_DIR)
433 return -EINVAL;
434
Tejun Heocfec0bc2013-09-11 22:29:09 -0400435 sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
Tejun Heod69ac5a2013-09-18 17:15:35 -0400436 sd->s_parent = sysfs_get(parent_sd);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200437
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800438 ret = sysfs_link_sibling(sd);
439 if (ret)
440 return ret;
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200441
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800442 /* Update timestamps on the parent */
Tejun Heod69ac5a2013-09-18 17:15:35 -0400443 ps_iattr = parent_sd->s_iattr;
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800444 if (ps_iattr) {
445 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
446 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
447 }
448
Ming Leibb2b0052013-04-04 22:22:37 +0800449 /* Mark the entry added into directory tree */
450 sd->s_flags &= ~SYSFS_FLAG_REMOVED;
451
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200452 return 0;
453}
454
455/**
Alex Chiang425cb022009-02-12 10:56:59 -0700456 * sysfs_pathname - return full path to sysfs dirent
457 * @sd: sysfs_dirent whose path we want
Geert Uytterhoeven66081a72012-09-29 22:23:19 +0200458 * @path: caller allocated buffer of size PATH_MAX
Alex Chiang425cb022009-02-12 10:56:59 -0700459 *
460 * Gives the name "/" to the sysfs_root entry; any path returned
461 * is relative to wherever sysfs is mounted.
Alex Chiang425cb022009-02-12 10:56:59 -0700462 */
463static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
464{
465 if (sd->s_parent) {
466 sysfs_pathname(sd->s_parent, path);
Geert Uytterhoeven66081a72012-09-29 22:23:19 +0200467 strlcat(path, "/", PATH_MAX);
Alex Chiang425cb022009-02-12 10:56:59 -0700468 }
Geert Uytterhoeven66081a72012-09-29 22:23:19 +0200469 strlcat(path, sd->s_name, PATH_MAX);
Alex Chiang425cb022009-02-12 10:56:59 -0700470 return path;
471}
472
Tejun Heod1c14592013-10-24 11:49:11 -0400473void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name)
474{
475 char *path;
476
477 path = kzalloc(PATH_MAX, GFP_KERNEL);
478 if (path) {
479 sysfs_pathname(parent, path);
480 strlcat(path, "/", PATH_MAX);
481 strlcat(path, name, PATH_MAX);
482 }
483
484 WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
485 path ? path : name);
486
487 kfree(path);
488}
489
Alex Chiang425cb022009-02-12 10:56:59 -0700490/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900491 * sysfs_add_one - add sysfs_dirent to parent
492 * @acxt: addrm context to use
493 * @sd: sysfs_dirent to be added
Tejun Heod69ac5a2013-09-18 17:15:35 -0400494 * @parent_sd: the parent sysfs_dirent to add @sd to
Tejun Heofb6896d2007-06-14 04:27:24 +0900495 *
Tejun Heod69ac5a2013-09-18 17:15:35 -0400496 * Get @parent_sd and set @sd->s_parent to it and increment nlink of
497 * the parent inode if @sd is a directory and link into the children
498 * list of the parent.
Tejun Heofb6896d2007-06-14 04:27:24 +0900499 *
500 * This function should be called between calls to
501 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
502 * passed the same @acxt as passed to sysfs_addrm_start().
503 *
504 * LOCKING:
505 * Determined by sysfs_addrm_start().
Tejun Heo23dc2792007-08-02 21:38:03 +0900506 *
507 * RETURNS:
508 * 0 on success, -EEXIST if entry with the given name already
509 * exists.
Tejun Heofb6896d2007-06-14 04:27:24 +0900510 */
Tejun Heod69ac5a2013-09-18 17:15:35 -0400511int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
512 struct sysfs_dirent *parent_sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900513{
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200514 int ret;
Tejun Heo23dc2792007-08-02 21:38:03 +0900515
Tejun Heod69ac5a2013-09-18 17:15:35 -0400516 ret = __sysfs_add_one(acxt, sd, parent_sd);
Alex Chiang425cb022009-02-12 10:56:59 -0700517
Tejun Heod1c14592013-10-24 11:49:11 -0400518 if (ret == -EEXIST)
519 sysfs_warn_dup(parent_sd, sd->s_name);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200520 return ret;
Tejun Heofb6896d2007-06-14 04:27:24 +0900521}
522
523/**
524 * sysfs_remove_one - remove sysfs_dirent from parent
525 * @acxt: addrm context to use
Jean Delvare9fd5b1c2008-01-08 18:11:24 +0100526 * @sd: sysfs_dirent to be removed
Tejun Heofb6896d2007-06-14 04:27:24 +0900527 *
528 * Mark @sd removed and drop nlink of parent inode if @sd is a
Tejun Heo181b2e42007-09-20 16:05:09 +0900529 * directory. @sd is unlinked from the children list.
Tejun Heofb6896d2007-06-14 04:27:24 +0900530 *
531 * This function should be called between calls to
532 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
533 * passed the same @acxt as passed to sysfs_addrm_start().
534 *
535 * LOCKING:
536 * Determined by sysfs_addrm_start().
537 */
Tejun Heo250f7c32013-09-18 17:15:38 -0400538static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
539 struct sysfs_dirent *sd)
Tejun Heofb6896d2007-06-14 04:27:24 +0900540{
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800541 struct sysfs_inode_attrs *ps_iattr;
542
Tejun Heo26ea12d2013-09-18 17:15:36 -0400543 /*
544 * Removal can be called multiple times on the same node. Only the
545 * first invocation is effective and puts the base ref.
546 */
547 if (sd->s_flags & SYSFS_FLAG_REMOVED)
548 return;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900549
550 sysfs_unlink_sibling(sd);
Tejun Heofb6896d2007-06-14 04:27:24 +0900551
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800552 /* Update timestamps on the parent */
Tejun Heod69ac5a2013-09-18 17:15:35 -0400553 ps_iattr = sd->s_parent->s_iattr;
Eric W. Biederman6b0bfe92009-11-20 16:08:51 -0800554 if (ps_iattr) {
555 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
556 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
557 }
558
Tejun Heofb6896d2007-06-14 04:27:24 +0900559 sd->s_flags |= SYSFS_FLAG_REMOVED;
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400560 sd->u.removed_list = acxt->removed;
Tejun Heofb6896d2007-06-14 04:27:24 +0900561 acxt->removed = sd;
Tejun Heoa0edd7c2007-06-14 04:27:24 +0900562}
563
564/**
Tejun Heofb6896d2007-06-14 04:27:24 +0900565 * sysfs_addrm_finish - finish up sysfs_dirent add/remove
566 * @acxt: addrm context to finish up
567 *
568 * Finish up sysfs_dirent add/remove. Resources acquired by
569 * sysfs_addrm_start() are released and removed sysfs_dirents are
Eric W. Biedermana16bbc32009-11-20 16:08:55 -0800570 * cleaned up.
Tejun Heofb6896d2007-06-14 04:27:24 +0900571 *
572 * LOCKING:
Eric W. Biedermana16bbc32009-11-20 16:08:55 -0800573 * sysfs_mutex is released.
Tejun Heofb6896d2007-06-14 04:27:24 +0900574 */
Tejun Heo990e53f2007-08-02 21:38:03 +0900575void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
Tejun Heod69ac5a2013-09-18 17:15:35 -0400576 __releases(sysfs_mutex)
Tejun Heofb6896d2007-06-14 04:27:24 +0900577{
578 /* release resources acquired by sysfs_addrm_start() */
579 mutex_unlock(&sysfs_mutex);
Tejun Heofb6896d2007-06-14 04:27:24 +0900580
581 /* kill removed sysfs_dirents */
582 while (acxt->removed) {
583 struct sysfs_dirent *sd = acxt->removed;
584
Mikulas Patocka58f2a4c2011-07-21 20:01:12 -0400585 acxt->removed = sd->u.removed_list;
Tejun Heofb6896d2007-06-14 04:27:24 +0900586
Tejun Heofb6896d2007-06-14 04:27:24 +0900587 sysfs_deactivate(sd);
Tejun Heo73d97142013-10-01 17:42:07 -0400588 sysfs_unmap_bin_file(sd);
Tejun Heofb6896d2007-06-14 04:27:24 +0900589 sysfs_put(sd);
590 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700591}
592
Tejun Heof0b0af42007-06-14 04:27:22 +0900593/**
594 * sysfs_find_dirent - find sysfs_dirent with the given name
595 * @parent_sd: sysfs_dirent to search under
596 * @name: name to look for
Tejun Heocfec0bc2013-09-11 22:29:09 -0400597 * @ns: the namespace tag to use
Maneesh Sonic5168652006-03-09 19:40:14 +0530598 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900599 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530600 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900601 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900602 * mutex_lock(sysfs_mutex)
Tejun Heof0b0af42007-06-14 04:27:22 +0900603 *
604 * RETURNS:
605 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530606 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900607struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
Tejun Heocfec0bc2013-09-11 22:29:09 -0400608 const unsigned char *name,
609 const void *ns)
Maneesh Sonic5168652006-03-09 19:40:14 +0530610{
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800611 struct rb_node *node = parent_sd->s_dir.children.rb_node;
Tejun Heoc84a3b22013-11-23 18:01:46 -0500612 bool has_ns = parent_sd->s_flags & SYSFS_FLAG_NS;
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800613 unsigned int hash;
Maneesh Sonic5168652006-03-09 19:40:14 +0530614
Tejun Heoc84a3b22013-11-23 18:01:46 -0500615 if (has_ns != (bool)ns) {
Linus Torvaldsa1212d22013-11-07 20:47:28 +0900616 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
Tejun Heoc84a3b22013-11-23 18:01:46 -0500617 has_ns ? "required" : "invalid",
618 parent_sd->s_name, name);
Linus Torvaldsa1212d22013-11-07 20:47:28 +0900619 return NULL;
620 }
621
Tejun Heocfec0bc2013-09-11 22:29:09 -0400622 hash = sysfs_name_hash(name, ns);
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800623 while (node) {
624 struct sysfs_dirent *sd;
625 int result;
Mikulas Patocka4f72c0c2011-07-25 17:55:57 -0400626
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800627 sd = to_sysfs_dirent(node);
Tejun Heocfec0bc2013-09-11 22:29:09 -0400628 result = sysfs_name_compare(hash, name, ns, sd);
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800629 if (result < 0)
630 node = node->rb_left;
631 else if (result > 0)
632 node = node->rb_right;
633 else
634 return sd;
Mikulas Patocka4f72c0c2011-07-25 17:55:57 -0400635 }
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -0800636 return NULL;
Tejun Heof0b0af42007-06-14 04:27:22 +0900637}
Maneesh Sonic5168652006-03-09 19:40:14 +0530638
Tejun Heof0b0af42007-06-14 04:27:22 +0900639/**
Tejun Heo388975c2013-09-11 23:19:13 -0400640 * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
Tejun Heof0b0af42007-06-14 04:27:22 +0900641 * @parent_sd: sysfs_dirent to search under
642 * @name: name to look for
Tejun Heo388975c2013-09-11 23:19:13 -0400643 * @ns: the namespace tag to use
Tejun Heof0b0af42007-06-14 04:27:22 +0900644 *
645 * Look for sysfs_dirent with name @name under @parent_sd and get
646 * it if found.
647 *
648 * LOCKING:
Tejun Heo3007e992007-06-14 04:27:23 +0900649 * Kernel thread context (may sleep). Grabs sysfs_mutex.
Tejun Heof0b0af42007-06-14 04:27:22 +0900650 *
651 * RETURNS:
652 * Pointer to sysfs_dirent if found, NULL if not.
653 */
Tejun Heo388975c2013-09-11 23:19:13 -0400654struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
655 const unsigned char *name,
656 const void *ns)
Tejun Heof0b0af42007-06-14 04:27:22 +0900657{
658 struct sysfs_dirent *sd;
659
Tejun Heo3007e992007-06-14 04:27:23 +0900660 mutex_lock(&sysfs_mutex);
Tejun Heocfec0bc2013-09-11 22:29:09 -0400661 sd = sysfs_find_dirent(parent_sd, name, ns);
Tejun Heof0b0af42007-06-14 04:27:22 +0900662 sysfs_get(sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900663 mutex_unlock(&sysfs_mutex);
Tejun Heof0b0af42007-06-14 04:27:22 +0900664
665 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530666}
Tejun Heo388975c2013-09-11 23:19:13 -0400667EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
Maneesh Sonic5168652006-03-09 19:40:14 +0530668
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500669/**
670 * kernfs_create_dir_ns - create a directory
671 * @parent: parent in which to create a new directory
672 * @name: name of the new directory
673 * @priv: opaque data associated with the new directory
674 * @ns: optional namespace tag of the directory
675 *
676 * Returns the created node on success, ERR_PTR() value on failure.
677 */
678struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
679 const char *name, void *priv,
680 const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700682 umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heo51225032007-06-14 04:27:25 +0900683 struct sysfs_addrm_cxt acxt;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900684 struct sysfs_dirent *sd;
Tejun Heo23dc2792007-08-02 21:38:03 +0900685 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Tejun Heofc9f54b2007-06-14 03:45:17 +0900687 /* allocate */
Tejun Heo3e519032007-06-14 03:45:15 +0900688 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900689 if (!sd)
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500690 return ERR_PTR(-ENOMEM);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700691
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700692 sd->s_ns = ns;
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500693 sd->priv = priv;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900694
Tejun Heo51225032007-06-14 04:27:25 +0900695 /* link in */
Tejun Heod69ac5a2013-09-18 17:15:35 -0400696 sysfs_addrm_start(&acxt);
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500697 rc = __sysfs_add_one(&acxt, sd, parent);
Tejun Heo23dc2792007-08-02 21:38:03 +0900698 sysfs_addrm_finish(&acxt);
Tejun Heo967e35d2007-07-18 16:38:11 +0900699
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500700 if (!rc)
701 return sd;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900702
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500703 sysfs_put(sd);
704 return ERR_PTR(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/**
Tejun Heoe34ff492013-09-11 22:29:05 -0400708 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
709 * @kobj: object we're creating directory for
710 * @ns: the namespace tag to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Tejun Heoe34ff492013-09-11 22:29:05 -0400712int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Tejun Heo608e2662007-06-14 04:27:22 +0900714 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 BUG_ON(!kobj);
717
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900718 if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900719 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 else
Eric W. Biederman7d0c7d62007-08-20 21:36:30 +0900721 parent_sd = &sysfs_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Dan Williams3a198882012-04-06 13:41:06 -0700723 if (!parent_sd)
724 return -ENOENT;
725
Tejun Heo93b2b8e2013-11-28 14:54:15 -0500726 sd = kernfs_create_dir_ns(parent_sd, kobject_name(kobj), kobj, ns);
727 if (IS_ERR(sd)) {
728 if (PTR_ERR(sd) == -EEXIST)
729 sysfs_warn_dup(parent_sd, kobject_name(kobj));
730 return PTR_ERR(sd);
731 }
732
733 kobj->sd = sd;
734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700737static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
738 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Tejun Heo6cb52142007-07-31 19:15:08 +0900740 struct dentry *ret = NULL;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700741 struct dentry *parent = dentry->d_parent;
742 struct sysfs_dirent *parent_sd = parent->d_fsdata;
Tejun Heoa7a04752007-08-02 21:38:02 +0900743 struct sysfs_dirent *sd;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900744 struct inode *inode;
Tejun Heoc84a3b22013-11-23 18:01:46 -0500745 const void *ns = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Tejun Heo6cb52142007-07-31 19:15:08 +0900747 mutex_lock(&sysfs_mutex);
748
Tejun Heoc84a3b22013-11-23 18:01:46 -0500749 if (parent_sd->s_flags & SYSFS_FLAG_NS)
750 ns = sysfs_info(dir->i_sb)->ns;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700751
Tejun Heocfec0bc2013-09-11 22:29:09 -0400752 sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Tejun Heofc9f54b2007-06-14 03:45:17 +0900754 /* no such entry */
Tejun Heoe49452c2008-01-16 12:06:14 +0900755 if (!sd) {
756 ret = ERR_PTR(-ENOENT);
Tejun Heo6cb52142007-07-31 19:15:08 +0900757 goto out_unlock;
Tejun Heoe49452c2008-01-16 12:06:14 +0900758 }
Al Viro469796d2012-06-07 20:51:39 -0400759 dentry->d_fsdata = sysfs_get(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900760
761 /* attach dentry and inode */
Eric W. Biedermanfac26222010-02-12 19:22:27 -0800762 inode = sysfs_get_inode(dir->i_sb, sd);
Tejun Heo6cb52142007-07-31 19:15:08 +0900763 if (!inode) {
764 ret = ERR_PTR(-ENOMEM);
765 goto out_unlock;
766 }
Tejun Heo3007e992007-06-14 04:27:23 +0900767
Tejun Heod6b4fd22007-09-20 16:05:11 +0900768 /* instantiate and hash dentry */
Al Viroe77fb7c2012-06-07 20:56:54 -0400769 ret = d_materialise_unique(dentry, inode);
Tejun Heo6cb52142007-07-31 19:15:08 +0900770 out_unlock:
Tejun Heo3007e992007-06-14 04:27:23 +0900771 mutex_unlock(&sysfs_mutex);
Tejun Heo6cb52142007-07-31 19:15:08 +0900772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800775const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .lookup = sysfs_lookup,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800777 .permission = sysfs_permission,
Maneesh Soni988d1862005-05-31 10:39:14 +0530778 .setattr = sysfs_setattr,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800779 .getattr = sysfs_getattr,
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400780 .setxattr = sysfs_setxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781};
782
Tejun Heobcdde7e2013-09-18 17:15:37 -0400783static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
784{
785 struct sysfs_dirent *last;
786
787 while (true) {
788 struct rb_node *rbn;
789
790 last = pos;
791
792 if (sysfs_type(pos) != SYSFS_DIR)
793 break;
794
795 rbn = rb_first(&pos->s_dir.children);
796 if (!rbn)
797 break;
798
799 pos = to_sysfs_dirent(rbn);
800 }
801
802 return last;
803}
804
805/**
806 * sysfs_next_descendant_post - find the next descendant for post-order walk
807 * @pos: the current position (%NULL to initiate traversal)
808 * @root: sysfs_dirent whose descendants to walk
809 *
810 * Find the next descendant to visit for post-order traversal of @root's
811 * descendants. @root is included in the iteration and the last node to be
812 * visited.
813 */
814static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
815 struct sysfs_dirent *root)
816{
817 struct rb_node *rbn;
818
819 lockdep_assert_held(&sysfs_mutex);
820
821 /* if first iteration, visit leftmost descendant which may be root */
822 if (!pos)
823 return sysfs_leftmost_descendant(root);
824
825 /* if we visited @root, we're done */
826 if (pos == root)
827 return NULL;
828
829 /* if there's an unvisited sibling, visit its leftmost descendant */
830 rbn = rb_next(&pos->s_rb);
831 if (rbn)
832 return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
833
834 /* no sibling left, visit parent */
835 return pos->s_parent;
836}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Tejun Heo879f40d2013-11-23 17:21:49 -0500838static void __kernfs_remove(struct sysfs_addrm_cxt *acxt,
839 struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Tejun Heobcdde7e2013-09-18 17:15:37 -0400841 struct sysfs_dirent *pos, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Tejun Heo250f7c32013-09-18 17:15:38 -0400843 if (!sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return;
845
Tejun Heo250f7c32013-09-18 17:15:38 -0400846 pr_debug("sysfs %s: removing\n", sd->s_name);
Tejun Heo0ab66082007-06-14 03:45:16 +0900847
Tejun Heobcdde7e2013-09-18 17:15:37 -0400848 next = NULL;
849 do {
850 pos = next;
Tejun Heo250f7c32013-09-18 17:15:38 -0400851 next = sysfs_next_descendant_post(pos, sd);
Tejun Heobcdde7e2013-09-18 17:15:37 -0400852 if (pos)
Tejun Heo250f7c32013-09-18 17:15:38 -0400853 sysfs_remove_one(acxt, pos);
Tejun Heobcdde7e2013-09-18 17:15:37 -0400854 } while (next);
Tejun Heo250f7c32013-09-18 17:15:38 -0400855}
Tejun Heobcdde7e2013-09-18 17:15:37 -0400856
Tejun Heo250f7c32013-09-18 17:15:38 -0400857/**
Tejun Heo879f40d2013-11-23 17:21:49 -0500858 * kernfs_remove - remove a sysfs_dirent recursively
Tejun Heo250f7c32013-09-18 17:15:38 -0400859 * @sd: the sysfs_dirent to remove
860 *
861 * Remove @sd along with all its subdirectories and files.
862 */
Tejun Heo879f40d2013-11-23 17:21:49 -0500863void kernfs_remove(struct sysfs_dirent *sd)
Tejun Heo250f7c32013-09-18 17:15:38 -0400864{
865 struct sysfs_addrm_cxt acxt;
866
867 sysfs_addrm_start(&acxt);
Tejun Heo879f40d2013-11-23 17:21:49 -0500868 __kernfs_remove(&acxt, sd);
Tejun Heobcdde7e2013-09-18 17:15:37 -0400869 sysfs_addrm_finish(&acxt);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700870}
871
872/**
Tejun Heo879f40d2013-11-23 17:21:49 -0500873 * kernfs_remove_by_name_ns - find a sysfs_dirent by name and remove it
Tejun Heo7eed6ec2013-10-24 11:49:10 -0400874 * @dir_sd: parent of the target
875 * @name: name of the sysfs_dirent to remove
876 * @ns: namespace tag of the sysfs_dirent to remove
877 *
878 * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove
879 * it. Returns 0 on success, -ENOENT if such entry doesn't exist.
880 */
Tejun Heo879f40d2013-11-23 17:21:49 -0500881int kernfs_remove_by_name_ns(struct sysfs_dirent *dir_sd, const char *name,
882 const void *ns)
Tejun Heo7eed6ec2013-10-24 11:49:10 -0400883{
884 struct sysfs_addrm_cxt acxt;
885 struct sysfs_dirent *sd;
886
887 if (!dir_sd) {
888 WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
889 name);
890 return -ENOENT;
891 }
892
893 sysfs_addrm_start(&acxt);
894
895 sd = sysfs_find_dirent(dir_sd, name, ns);
896 if (sd)
Tejun Heo879f40d2013-11-23 17:21:49 -0500897 __kernfs_remove(&acxt, sd);
Tejun Heo7eed6ec2013-10-24 11:49:10 -0400898
899 sysfs_addrm_finish(&acxt);
900
901 if (sd)
902 return 0;
903 else
904 return -ENOENT;
905}
906
907/**
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700908 * sysfs_remove_dir - remove an object's directory.
909 * @kobj: object.
910 *
911 * The only thing special about this is that we remove any files in
912 * the directory before we remove the directory, and we've inlined
913 * what used to be sysfs_rmdir() below, instead of calling separately.
914 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700915void sysfs_remove_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700916{
Tejun Heo608e2662007-06-14 04:27:22 +0900917 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900918
Tejun Heo0cae60f2013-10-30 10:28:36 -0400919 /*
920 * In general, kboject owner is responsible for ensuring removal
921 * doesn't race with other operations and sysfs doesn't provide any
922 * protection; however, when @kobj is used as a symlink target, the
923 * symlinking entity usually doesn't own @kobj and thus has no
924 * control over removal. @kobj->sd may be removed anytime and
925 * symlink code may end up dereferencing an already freed sd.
926 *
927 * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation
928 * against symlink operations so that symlink code can safely
929 * dereference @kobj->sd.
930 */
931 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900932 kobj->sd = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400933 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900934
Tejun Heo250f7c32013-09-18 17:15:38 -0400935 if (sd) {
936 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
Tejun Heo879f40d2013-11-23 17:21:49 -0500937 kernfs_remove(sd);
Tejun Heo250f7c32013-09-18 17:15:38 -0400938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Tejun Heo890ece12013-11-23 17:21:51 -0500941/**
942 * kernfs_rename_ns - move and rename a kernfs_node
943 * @sd: target node
944 * @new_parent: new parent to put @sd under
945 * @new_name: new name
946 * @new_ns: new namespace tag
947 */
948int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
949 const char *new_name, const void *new_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Tejun Heo996b7372007-06-14 03:45:14 +0900951 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Eric W. Biederman832b6af2009-11-20 16:08:56 -0800953 mutex_lock(&sysfs_mutex);
Eric W. Biederman932ea2e2007-08-20 21:36:30 +0900954
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900955 error = 0;
Tejun Heo890ece12013-11-23 17:21:51 -0500956 if ((sd->s_parent == new_parent) && (sd->s_ns == new_ns) &&
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800957 (strcmp(sd->s_name, new_name) == 0))
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900958 goto out; /* nothing to rename */
959
Tejun Heo996b7372007-06-14 03:45:14 +0900960 error = -EEXIST;
Tejun Heo890ece12013-11-23 17:21:51 -0500961 if (sysfs_find_dirent(new_parent, new_name, new_ns))
Eric W. Biederman832b6af2009-11-20 16:08:56 -0800962 goto out;
Tejun Heo996b7372007-06-14 03:45:14 +0900963
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700964 /* rename sysfs_dirent */
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800965 if (strcmp(sd->s_name, new_name) != 0) {
966 error = -ENOMEM;
Sasikantha babub4eafca2012-05-03 02:26:14 +0530967 new_name = kstrdup(new_name, GFP_KERNEL);
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800968 if (!new_name)
969 goto out;
Tejun Heo996b7372007-06-14 03:45:14 +0900970
Sasikantha babub4eafca2012-05-03 02:26:14 +0530971 kfree(sd->s_name);
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800972 sd->s_name = new_name;
973 }
974
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700975 /*
976 * Move to the appropriate place in the appropriate directories rbtree.
977 */
Eric W. Biedermanf6d90b42011-11-01 07:06:17 -0700978 sysfs_unlink_sibling(sd);
Tejun Heo890ece12013-11-23 17:21:51 -0500979 sysfs_get(new_parent);
Eric W. Biedermanf6d90b42011-11-01 07:06:17 -0700980 sysfs_put(sd->s_parent);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700981 sd->s_ns = new_ns;
Tejun Heocfec0bc2013-09-11 22:29:09 -0400982 sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
Tejun Heo890ece12013-11-23 17:21:51 -0500983 sd->s_parent = new_parent;
Eric W. Biedermanf6d90b42011-11-01 07:06:17 -0700984 sysfs_link_sibling(sd);
Tejun Heo0c096b52007-06-14 03:45:15 +0900985
Tejun Heo996b7372007-06-14 03:45:14 +0900986 error = 0;
Eric W. Biederman9918f9a2007-08-20 21:36:31 +0900987 out:
Eric W. Biederman832b6af2009-11-20 16:08:56 -0800988 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return error;
990}
991
Tejun Heoe34ff492013-09-11 22:29:05 -0400992int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
993 const void *new_ns)
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800994{
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700995 struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700996
Tejun Heo890ece12013-11-23 17:21:51 -0500997 return kernfs_rename_ns(kobj->sd, parent_sd, new_name, new_ns);
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800998}
999
Tejun Heoe34ff492013-09-11 22:29:05 -04001000int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
1001 const void *new_ns)
Cornelia Huck8a824722006-11-20 17:07:51 +01001002{
Tejun Heo51225032007-06-14 04:27:25 +09001003 struct sysfs_dirent *sd = kobj->sd;
1004 struct sysfs_dirent *new_parent_sd;
Cornelia Huck8a824722006-11-20 17:07:51 +01001005
Tejun Heo51225032007-06-14 04:27:25 +09001006 BUG_ON(!sd->s_parent);
Eric W. Biedermanca1bab32009-11-20 16:08:57 -08001007 new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
Cornelia Hucka6a83572009-10-06 15:33:35 +02001008 new_parent_kobj->sd : &sysfs_root;
Cornelia Huck8a824722006-11-20 17:07:51 +01001009
Tejun Heo890ece12013-11-23 17:21:51 -05001010 return kernfs_rename_ns(sd, new_parent_sd, sd->s_name, new_ns);
Cornelia Huck8a824722006-11-20 17:07:51 +01001011}
1012
Tejun Heoc84a3b22013-11-23 18:01:46 -05001013/**
Tejun Heo93b2b8e2013-11-28 14:54:15 -05001014 * kernfs_enable_ns - enable namespace under a directory
Tejun Heoc84a3b22013-11-23 18:01:46 -05001015 * @sd: directory of interest, should be empty
1016 *
1017 * This is to be called right after @sd is created to enable namespace
1018 * under it. All children of @sd must have non-NULL namespace tags and
1019 * only the ones which match the super_block's tag will be visible.
1020 */
Tejun Heo93b2b8e2013-11-28 14:54:15 -05001021void kernfs_enable_ns(struct sysfs_dirent *sd)
Tejun Heoc84a3b22013-11-23 18:01:46 -05001022{
1023 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
1024 WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd->s_dir.children));
1025 sd->s_flags |= SYSFS_FLAG_NS;
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/* Relationship between s_mode and the DT_xxx types */
1029static inline unsigned char dt_type(struct sysfs_dirent *sd)
1030{
1031 return (sd->s_mode >> 12) & 15;
1032}
1033
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001034static int sysfs_dir_release(struct inode *inode, struct file *filp)
1035{
1036 sysfs_put(filp->private_data);
1037 return 0;
1038}
1039
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001040static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -08001041 struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001042{
1043 if (pos) {
1044 int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
1045 pos->s_parent == parent_sd &&
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -08001046 hash == pos->s_hash;
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001047 sysfs_put(pos);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001048 if (!valid)
1049 pos = NULL;
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001050 }
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -08001051 if (!pos && (hash > 1) && (hash < INT_MAX)) {
1052 struct rb_node *node = parent_sd->s_dir.children.rb_node;
1053 while (node) {
1054 pos = to_sysfs_dirent(node);
1055
1056 if (hash < pos->s_hash)
1057 node = node->rb_left;
1058 else if (hash > pos->s_hash)
1059 node = node->rb_right;
1060 else
Mikulas Patockaa406f752011-07-25 17:57:03 -04001061 break;
Mikulas Patockaa406f752011-07-25 17:57:03 -04001062 }
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001063 }
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -08001064 /* Skip over entries in the wrong namespace */
Eric W. Biedermanb9e2780d2011-10-25 05:38:41 -07001065 while (pos && pos->s_ns != ns) {
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -08001066 struct rb_node *node = rb_next(&pos->s_rb);
1067 if (!node)
Mikulas Patockaa406f752011-07-25 17:57:03 -04001068 pos = NULL;
1069 else
Eric W. Biederman4e4d6d82011-12-18 20:05:43 -08001070 pos = to_sysfs_dirent(node);
Mikulas Patockaa406f752011-07-25 17:57:03 -04001071 }
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001072 return pos;
1073}
1074
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001075static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
1076 struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001077{
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001078 pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
Greg Kroah-Hartman37814ee2013-08-21 16:36:02 -07001079 if (pos)
1080 do {
1081 struct rb_node *node = rb_next(&pos->s_rb);
1082 if (!node)
1083 pos = NULL;
1084 else
1085 pos = to_sysfs_dirent(node);
1086 } while (pos && pos->s_ns != ns);
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001087 return pos;
1088}
1089
Al Virod55fea82013-05-16 14:31:02 -04001090static int sysfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Al Virod55fea82013-05-16 14:31:02 -04001092 struct dentry *dentry = file->f_path.dentry;
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -07001093 struct sysfs_dirent *parent_sd = dentry->d_fsdata;
Al Virod55fea82013-05-16 14:31:02 -04001094 struct sysfs_dirent *pos = file->private_data;
Tejun Heoc84a3b22013-11-23 18:01:46 -05001095 const void *ns = NULL;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001096
Al Virod55fea82013-05-16 14:31:02 -04001097 if (!dir_emit_dots(file, ctx))
1098 return 0;
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001099 mutex_lock(&sysfs_mutex);
Tejun Heoc84a3b22013-11-23 18:01:46 -05001100
1101 if (parent_sd->s_flags & SYSFS_FLAG_NS)
1102 ns = sysfs_info(dentry->d_sb)->ns;
1103
Al Virod55fea82013-05-16 14:31:02 -04001104 for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001105 pos;
Al Virod55fea82013-05-16 14:31:02 -04001106 pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
1107 const char *name = pos->s_name;
1108 unsigned int type = dt_type(pos);
1109 int len = strlen(name);
1110 ino_t ino = pos->s_ino;
1111 ctx->pos = pos->s_hash;
1112 file->private_data = sysfs_get(pos);
Eric W. Biederman3efa65b2007-08-20 21:36:30 +09001113
Eric W. Biederman3efa65b2007-08-20 21:36:30 +09001114 mutex_unlock(&sysfs_mutex);
Al Virod55fea82013-05-16 14:31:02 -04001115 if (!dir_emit(ctx, name, len, ino, type))
1116 return 0;
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001117 mutex_lock(&sysfs_mutex);
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001118 }
1119 mutex_unlock(&sysfs_mutex);
Al Virod55fea82013-05-16 14:31:02 -04001120 file->private_data = NULL;
1121 ctx->pos = INT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return 0;
1123}
1124
Ming Lei991f76f2013-03-20 23:25:24 +08001125static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1126{
1127 struct inode *inode = file_inode(file);
1128 loff_t ret;
1129
1130 mutex_lock(&inode->i_mutex);
1131 ret = generic_file_llseek(file, offset, whence);
1132 mutex_unlock(&inode->i_mutex);
1133
1134 return ret;
1135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001137const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 .read = generic_read_dir,
Al Virod55fea82013-05-16 14:31:02 -04001139 .iterate = sysfs_readdir,
Eric W. Biederman1e5289c2010-01-01 14:43:53 -08001140 .release = sysfs_dir_release,
Ming Lei991f76f2013-03-20 23:25:24 +08001141 .llseek = sysfs_dir_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};