]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/sysfs/dir.c
Merge remote-tracking branch 'userns/for-next'
[karo-tx-linux.git] / fs / sysfs / dir.c
1 /*
2  * 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.
11  */
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>
19 #include <linux/namei.h>
20 #include <linux/idr.h>
21 #include <linux/completion.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/security.h>
25 #include <linux/hash.h>
26 #include "sysfs.h"
27
28 DEFINE_MUTEX(sysfs_mutex);
29 DEFINE_SPINLOCK(sysfs_assoc_lock);
30
31 #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb)
32
33 static DEFINE_SPINLOCK(sysfs_ino_lock);
34 static DEFINE_IDA(sysfs_ino_ida);
35
36 /**
37  *      sysfs_name_hash
38  *      @name: Null terminated string to hash
39  *      @ns:   Namespace tag to hash
40  *
41  *      Returns 31 bit hash of ns + name (so it fits in an off_t )
42  */
43 static unsigned int sysfs_name_hash(const char *name, const void *ns)
44 {
45         unsigned long hash = init_name_hash();
46         unsigned int len = strlen(name);
47         while (len--)
48                 hash = partial_name_hash(*name++, hash);
49         hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
50         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
59 static int sysfs_name_compare(unsigned int hash, const char *name,
60                               const void *ns, const struct sysfs_dirent *sd)
61 {
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
69 static int sysfs_sd_compare(const struct sysfs_dirent *left,
70                             const struct sysfs_dirent *right)
71 {
72         return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns,
73                                   right);
74 }
75
76 /**
77  *      sysfs_link_sibling - link sysfs_dirent into sibling rbtree
78  *      @sd: sysfs_dirent of interest
79  *
80  *      Link @sd into its sibling rbtree which starts from
81  *      sd->s_parent->s_dir.children.
82  *
83  *      Locking:
84  *      mutex_lock(sysfs_mutex)
85  *
86  *      RETURNS:
87  *      0 on susccess -EEXIST on failure.
88  */
89 static int sysfs_link_sibling(struct sysfs_dirent *sd)
90 {
91         struct rb_node **node = &sd->s_parent->s_dir.children.rb_node;
92         struct rb_node *parent = NULL;
93
94         if (sysfs_type(sd) == SYSFS_DIR)
95                 sd->s_parent->s_dir.subdirs++;
96
97         while (*node) {
98                 struct sysfs_dirent *pos;
99                 int result;
100
101                 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;
110         }
111         /* 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
115         /* if @sd has ns tag, mark the parent to enable ns filtering */
116         if (sd->s_ns)
117                 sd->s_parent->s_flags |= SYSFS_FLAG_HAS_NS;
118
119         return 0;
120 }
121
122 /**
123  *      sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree
124  *      @sd: sysfs_dirent of interest
125  *
126  *      Unlink @sd from its sibling rbtree which starts from
127  *      sd->s_parent->s_dir.children.
128  *
129  *      Locking:
130  *      mutex_lock(sysfs_mutex)
131  */
132 static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
133 {
134         if (sysfs_type(sd) == SYSFS_DIR)
135                 sd->s_parent->s_dir.subdirs--;
136
137         rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children);
138
139         /*
140          * Either all or none of the children have tags.  Clearing HAS_NS
141          * when there's no child left is enough to keep the flag synced.
142          */
143         if (RB_EMPTY_ROOT(&sd->s_parent->s_dir.children))
144                 sd->s_parent->s_flags &= ~SYSFS_FLAG_HAS_NS;
145 }
146
147 /**
148  *      sysfs_get_active - get an active reference to sysfs_dirent
149  *      @sd: sysfs_dirent to get an active reference to
150  *
151  *      Get an active reference of @sd.  This function is noop if @sd
152  *      is NULL.
153  *
154  *      RETURNS:
155  *      Pointer to @sd on success, NULL on failure.
156  */
157 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
158 {
159         if (unlikely(!sd))
160                 return NULL;
161
162         if (!atomic_inc_unless_negative(&sd->s_active))
163                 return NULL;
164
165         if (likely(!sysfs_ignore_lockdep(sd)))
166                 rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_);
167         return sd;
168 }
169
170 /**
171  *      sysfs_put_active - put an active reference to sysfs_dirent
172  *      @sd: sysfs_dirent to put an active reference to
173  *
174  *      Put an active reference to @sd.  This function is noop if @sd
175  *      is NULL.
176  */
177 void sysfs_put_active(struct sysfs_dirent *sd)
178 {
179         int v;
180
181         if (unlikely(!sd))
182                 return;
183
184         if (likely(!sysfs_ignore_lockdep(sd)))
185                 rwsem_release(&sd->dep_map, 1, _RET_IP_);
186         v = atomic_dec_return(&sd->s_active);
187         if (likely(v != SD_DEACTIVATED_BIAS))
188                 return;
189
190         /* atomic_dec_return() is a mb(), we'll always see the updated
191          * sd->u.completion.
192          */
193         complete(sd->u.completion);
194 }
195
196 /**
197  *      sysfs_deactivate - deactivate sysfs_dirent
198  *      @sd: sysfs_dirent to deactivate
199  *
200  *      Deny new active references and drain existing ones.
201  */
202 static void sysfs_deactivate(struct sysfs_dirent *sd)
203 {
204         DECLARE_COMPLETION_ONSTACK(wait);
205         int v;
206
207         BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED));
208
209         if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF))
210                 return;
211
212         sd->u.completion = (void *)&wait;
213
214         rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_);
215         /* atomic_add_return() is a mb(), put_active() will always see
216          * the updated sd->u.completion.
217          */
218         v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
219
220         if (v != SD_DEACTIVATED_BIAS) {
221                 lock_contended(&sd->dep_map, _RET_IP_);
222                 wait_for_completion(&wait);
223         }
224
225         lock_acquired(&sd->dep_map, _RET_IP_);
226         rwsem_release(&sd->dep_map, 1, _RET_IP_);
227 }
228
229 static int sysfs_alloc_ino(unsigned int *pino)
230 {
231         int ino, rc;
232
233  retry:
234         spin_lock(&sysfs_ino_lock);
235         rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
236         spin_unlock(&sysfs_ino_lock);
237
238         if (rc == -EAGAIN) {
239                 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
240                         goto retry;
241                 rc = -ENOMEM;
242         }
243
244         *pino = ino;
245         return rc;
246 }
247
248 static void sysfs_free_ino(unsigned int ino)
249 {
250         spin_lock(&sysfs_ino_lock);
251         ida_remove(&sysfs_ino_ida, ino);
252         spin_unlock(&sysfs_ino_lock);
253 }
254
255 void release_sysfs_dirent(struct sysfs_dirent *sd)
256 {
257         struct sysfs_dirent *parent_sd;
258
259  repeat:
260         /* Moving/renaming is always done while holding reference.
261          * sd->s_parent won't change beneath us.
262          */
263         parent_sd = sd->s_parent;
264
265         WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
266                 "sysfs: free using entry: %s/%s\n",
267                 parent_sd ? parent_sd->s_name : "", sd->s_name);
268
269         if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
270                 sysfs_put(sd->s_symlink.target_sd);
271         if (sysfs_type(sd) & SYSFS_COPY_NAME)
272                 kfree(sd->s_name);
273         if (sd->s_iattr && sd->s_iattr->ia_secdata)
274                 security_release_secctx(sd->s_iattr->ia_secdata,
275                                         sd->s_iattr->ia_secdata_len);
276         kfree(sd->s_iattr);
277         sysfs_free_ino(sd->s_ino);
278         kmem_cache_free(sysfs_dir_cachep, sd);
279
280         sd = parent_sd;
281         if (sd && atomic_dec_and_test(&sd->s_count))
282                 goto repeat;
283 }
284
285 static int sysfs_dentry_delete(const struct dentry *dentry)
286 {
287         struct sysfs_dirent *sd = dentry->d_fsdata;
288         return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED));
289 }
290
291 static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
292 {
293         struct sysfs_dirent *sd;
294
295         if (flags & LOOKUP_RCU)
296                 return -ECHILD;
297
298         sd = dentry->d_fsdata;
299         mutex_lock(&sysfs_mutex);
300
301         /* The sysfs dirent has been deleted */
302         if (sd->s_flags & SYSFS_FLAG_REMOVED)
303                 goto out_bad;
304
305         /* The sysfs dirent has been moved? */
306         if (dentry->d_parent->d_fsdata != sd->s_parent)
307                 goto out_bad;
308
309         /* The sysfs dirent has been renamed */
310         if (strcmp(dentry->d_name.name, sd->s_name) != 0)
311                 goto out_bad;
312
313         /* The sysfs dirent has been moved to a different namespace */
314         if (sd->s_ns && sd->s_ns != sysfs_info(dentry->d_sb)->ns)
315                 goto out_bad;
316
317         mutex_unlock(&sysfs_mutex);
318         return 1;
319 out_bad:
320         /* Remove the dentry from the dcache hashes.
321          * If this is a deleted dentry we use d_drop instead of d_delete
322          * so sysfs doesn't need to cope with negative dentries.
323          *
324          * If this is a dentry that has simply been renamed we
325          * use d_drop to remove it from the dcache lookup on its
326          * old parent.  If this dentry persists later when a lookup
327          * is performed at its new name the dentry will be readded
328          * to the dcache hashes.
329          */
330         mutex_unlock(&sysfs_mutex);
331         shrink_submounts_and_drop(dentry);
332
333         return 0;
334 }
335
336 static void sysfs_dentry_release(struct dentry *dentry)
337 {
338         sysfs_put(dentry->d_fsdata);
339 }
340
341 const struct dentry_operations sysfs_dentry_ops = {
342         .d_revalidate   = sysfs_dentry_revalidate,
343         .d_delete       = sysfs_dentry_delete,
344         .d_release      = sysfs_dentry_release,
345 };
346
347 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
348 {
349         char *dup_name = NULL;
350         struct sysfs_dirent *sd;
351
352         if (type & SYSFS_COPY_NAME) {
353                 name = dup_name = kstrdup(name, GFP_KERNEL);
354                 if (!name)
355                         return NULL;
356         }
357
358         sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
359         if (!sd)
360                 goto err_out1;
361
362         if (sysfs_alloc_ino(&sd->s_ino))
363                 goto err_out2;
364
365         atomic_set(&sd->s_count, 1);
366         atomic_set(&sd->s_active, 0);
367
368         sd->s_name = name;
369         sd->s_mode = mode;
370         sd->s_flags = type | SYSFS_FLAG_REMOVED;
371
372         return sd;
373
374  err_out2:
375         kmem_cache_free(sysfs_dir_cachep, sd);
376  err_out1:
377         kfree(dup_name);
378         return NULL;
379 }
380
381 /**
382  *      sysfs_addrm_start - prepare for sysfs_dirent add/remove
383  *      @acxt: pointer to sysfs_addrm_cxt to be used
384  *
385  *      This function is called when the caller is about to add or remove
386  *      sysfs_dirent.  This function acquires sysfs_mutex.  @acxt is used
387  *      to keep and pass context to other addrm functions.
388  *
389  *      LOCKING:
390  *      Kernel thread context (may sleep).  sysfs_mutex is locked on
391  *      return.
392  */
393 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
394         __acquires(sysfs_mutex)
395 {
396         memset(acxt, 0, sizeof(*acxt));
397
398         mutex_lock(&sysfs_mutex);
399 }
400
401 /**
402  *      __sysfs_add_one - add sysfs_dirent to parent without warning
403  *      @acxt: addrm context to use
404  *      @sd: sysfs_dirent to be added
405  *      @parent_sd: the parent sysfs_dirent to add @sd to
406  *
407  *      Get @parent_sd and set @sd->s_parent to it and increment nlink of
408  *      the parent inode if @sd is a directory and link into the children
409  *      list of the parent.
410  *
411  *      This function should be called between calls to
412  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
413  *      passed the same @acxt as passed to sysfs_addrm_start().
414  *
415  *      LOCKING:
416  *      Determined by sysfs_addrm_start().
417  *
418  *      RETURNS:
419  *      0 on success, -EEXIST if entry with the given name already
420  *      exists.
421  */
422 int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
423                     struct sysfs_dirent *parent_sd)
424 {
425         struct sysfs_inode_attrs *ps_iattr;
426         int ret;
427
428         sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
429         sd->s_parent = sysfs_get(parent_sd);
430
431         ret = sysfs_link_sibling(sd);
432         if (ret)
433                 return ret;
434
435         /* Update timestamps on the parent */
436         ps_iattr = parent_sd->s_iattr;
437         if (ps_iattr) {
438                 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
439                 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
440         }
441
442         /* Mark the entry added into directory tree */
443         sd->s_flags &= ~SYSFS_FLAG_REMOVED;
444
445         return 0;
446 }
447
448 /**
449  *      sysfs_pathname - return full path to sysfs dirent
450  *      @sd: sysfs_dirent whose path we want
451  *      @path: caller allocated buffer of size PATH_MAX
452  *
453  *      Gives the name "/" to the sysfs_root entry; any path returned
454  *      is relative to wherever sysfs is mounted.
455  */
456 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
457 {
458         if (sd->s_parent) {
459                 sysfs_pathname(sd->s_parent, path);
460                 strlcat(path, "/", PATH_MAX);
461         }
462         strlcat(path, sd->s_name, PATH_MAX);
463         return path;
464 }
465
466 /**
467  *      sysfs_add_one - add sysfs_dirent to parent
468  *      @acxt: addrm context to use
469  *      @sd: sysfs_dirent to be added
470  *      @parent_sd: the parent sysfs_dirent to add @sd to
471  *
472  *      Get @parent_sd and set @sd->s_parent to it and increment nlink of
473  *      the parent inode if @sd is a directory and link into the children
474  *      list of the parent.
475  *
476  *      This function should be called between calls to
477  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
478  *      passed the same @acxt as passed to sysfs_addrm_start().
479  *
480  *      LOCKING:
481  *      Determined by sysfs_addrm_start().
482  *
483  *      RETURNS:
484  *      0 on success, -EEXIST if entry with the given name already
485  *      exists.
486  */
487 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
488                   struct sysfs_dirent *parent_sd)
489 {
490         int ret;
491
492         ret = __sysfs_add_one(acxt, sd, parent_sd);
493         if (ret == -EEXIST) {
494                 char *path = kzalloc(PATH_MAX, GFP_KERNEL);
495                 WARN(1, KERN_WARNING
496                      "sysfs: cannot create duplicate filename '%s'\n",
497                      (path == NULL) ? sd->s_name
498                                     : (sysfs_pathname(parent_sd, path),
499                                        strlcat(path, "/", PATH_MAX),
500                                        strlcat(path, sd->s_name, PATH_MAX),
501                                        path));
502                 kfree(path);
503         }
504
505         return ret;
506 }
507
508 /**
509  *      sysfs_remove_one - remove sysfs_dirent from parent
510  *      @acxt: addrm context to use
511  *      @sd: sysfs_dirent to be removed
512  *
513  *      Mark @sd removed and drop nlink of parent inode if @sd is a
514  *      directory.  @sd is unlinked from the children list.
515  *
516  *      This function should be called between calls to
517  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
518  *      passed the same @acxt as passed to sysfs_addrm_start().
519  *
520  *      LOCKING:
521  *      Determined by sysfs_addrm_start().
522  */
523 static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
524                              struct sysfs_dirent *sd)
525 {
526         struct sysfs_inode_attrs *ps_iattr;
527
528         /*
529          * Removal can be called multiple times on the same node.  Only the
530          * first invocation is effective and puts the base ref.
531          */
532         if (sd->s_flags & SYSFS_FLAG_REMOVED)
533                 return;
534
535         sysfs_unlink_sibling(sd);
536
537         /* Update timestamps on the parent */
538         ps_iattr = sd->s_parent->s_iattr;
539         if (ps_iattr) {
540                 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
541                 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
542         }
543
544         sd->s_flags |= SYSFS_FLAG_REMOVED;
545         sd->u.removed_list = acxt->removed;
546         acxt->removed = sd;
547 }
548
549 /**
550  *      sysfs_addrm_finish - finish up sysfs_dirent add/remove
551  *      @acxt: addrm context to finish up
552  *
553  *      Finish up sysfs_dirent add/remove.  Resources acquired by
554  *      sysfs_addrm_start() are released and removed sysfs_dirents are
555  *      cleaned up.
556  *
557  *      LOCKING:
558  *      sysfs_mutex is released.
559  */
560 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
561         __releases(sysfs_mutex)
562 {
563         /* release resources acquired by sysfs_addrm_start() */
564         mutex_unlock(&sysfs_mutex);
565
566         /* kill removed sysfs_dirents */
567         while (acxt->removed) {
568                 struct sysfs_dirent *sd = acxt->removed;
569
570                 acxt->removed = sd->u.removed_list;
571
572                 sysfs_deactivate(sd);
573                 sysfs_unmap_bin_file(sd);
574                 sysfs_put(sd);
575         }
576 }
577
578 /**
579  *      sysfs_find_dirent - find sysfs_dirent with the given name
580  *      @parent_sd: sysfs_dirent to search under
581  *      @name: name to look for
582  *      @ns: the namespace tag to use
583  *
584  *      Look for sysfs_dirent with name @name under @parent_sd.
585  *
586  *      LOCKING:
587  *      mutex_lock(sysfs_mutex)
588  *
589  *      RETURNS:
590  *      Pointer to sysfs_dirent if found, NULL if not.
591  */
592 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
593                                        const unsigned char *name,
594                                        const void *ns)
595 {
596         struct rb_node *node = parent_sd->s_dir.children.rb_node;
597         unsigned int hash;
598
599         hash = sysfs_name_hash(name, ns);
600         while (node) {
601                 struct sysfs_dirent *sd;
602                 int result;
603
604                 sd = to_sysfs_dirent(node);
605                 result = sysfs_name_compare(hash, name, ns, sd);
606                 if (result < 0)
607                         node = node->rb_left;
608                 else if (result > 0)
609                         node = node->rb_right;
610                 else
611                         return sd;
612         }
613         return NULL;
614 }
615
616 /**
617  *      sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
618  *      @parent_sd: sysfs_dirent to search under
619  *      @name: name to look for
620  *      @ns: the namespace tag to use
621  *
622  *      Look for sysfs_dirent with name @name under @parent_sd and get
623  *      it if found.
624  *
625  *      LOCKING:
626  *      Kernel thread context (may sleep).  Grabs sysfs_mutex.
627  *
628  *      RETURNS:
629  *      Pointer to sysfs_dirent if found, NULL if not.
630  */
631 struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
632                                          const unsigned char *name,
633                                          const void *ns)
634 {
635         struct sysfs_dirent *sd;
636
637         mutex_lock(&sysfs_mutex);
638         sd = sysfs_find_dirent(parent_sd, name, ns);
639         sysfs_get(sd);
640         mutex_unlock(&sysfs_mutex);
641
642         return sd;
643 }
644 EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
645
646 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
647                       const char *name, const void *ns,
648                       struct sysfs_dirent **p_sd)
649 {
650         umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
651         struct sysfs_addrm_cxt acxt;
652         struct sysfs_dirent *sd;
653         int rc;
654
655         /* allocate */
656         sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
657         if (!sd)
658                 return -ENOMEM;
659
660         sd->s_ns = ns;
661         sd->s_dir.kobj = kobj;
662
663         /* link in */
664         sysfs_addrm_start(&acxt);
665         rc = sysfs_add_one(&acxt, sd, parent_sd);
666         sysfs_addrm_finish(&acxt);
667
668         if (rc == 0)
669                 *p_sd = sd;
670         else
671                 sysfs_put(sd);
672
673         return rc;
674 }
675
676 int sysfs_create_subdir(struct kobject *kobj, const char *name,
677                         struct sysfs_dirent **p_sd)
678 {
679         return create_dir(kobj, kobj->sd, name, NULL, p_sd);
680 }
681
682 /**
683  * sysfs_create_dir_ns - create a directory for an object with a namespace tag
684  * @kobj: object we're creating directory for
685  * @ns: the namespace tag to use
686  */
687 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
688 {
689         struct sysfs_dirent *parent_sd, *sd;
690         int error = 0;
691
692         BUG_ON(!kobj);
693
694         if (kobj->parent)
695                 parent_sd = kobj->parent->sd;
696         else
697                 parent_sd = &sysfs_root;
698
699         if (!parent_sd)
700                 return -ENOENT;
701
702         error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
703         if (!error)
704                 kobj->sd = sd;
705         return error;
706 }
707
708 static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
709                                    unsigned int flags)
710 {
711         struct dentry *ret = NULL;
712         struct dentry *parent = dentry->d_parent;
713         struct sysfs_dirent *parent_sd = parent->d_fsdata;
714         struct sysfs_dirent *sd;
715         struct inode *inode;
716         const void *ns = NULL;
717
718         mutex_lock(&sysfs_mutex);
719
720         if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
721                 ns = sysfs_info(dir->i_sb)->ns;
722
723         sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
724
725         /* no such entry */
726         if (!sd) {
727                 ret = ERR_PTR(-ENOENT);
728                 goto out_unlock;
729         }
730         dentry->d_fsdata = sysfs_get(sd);
731
732         /* attach dentry and inode */
733         inode = sysfs_get_inode(dir->i_sb, sd);
734         if (!inode) {
735                 ret = ERR_PTR(-ENOMEM);
736                 goto out_unlock;
737         }
738
739         /* instantiate and hash dentry */
740         ret = d_materialise_unique(dentry, inode);
741  out_unlock:
742         mutex_unlock(&sysfs_mutex);
743         return ret;
744 }
745
746 const struct inode_operations sysfs_dir_inode_operations = {
747         .lookup         = sysfs_lookup,
748         .permission     = sysfs_permission,
749         .setattr        = sysfs_setattr,
750         .getattr        = sysfs_getattr,
751         .setxattr       = sysfs_setxattr,
752 };
753
754 static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
755 {
756         struct sysfs_dirent *last;
757
758         while (true) {
759                 struct rb_node *rbn;
760
761                 last = pos;
762
763                 if (sysfs_type(pos) != SYSFS_DIR)
764                         break;
765
766                 rbn = rb_first(&pos->s_dir.children);
767                 if (!rbn)
768                         break;
769
770                 pos = to_sysfs_dirent(rbn);
771         }
772
773         return last;
774 }
775
776 /**
777  * sysfs_next_descendant_post - find the next descendant for post-order walk
778  * @pos: the current position (%NULL to initiate traversal)
779  * @root: sysfs_dirent whose descendants to walk
780  *
781  * Find the next descendant to visit for post-order traversal of @root's
782  * descendants.  @root is included in the iteration and the last node to be
783  * visited.
784  */
785 static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
786                                                        struct sysfs_dirent *root)
787 {
788         struct rb_node *rbn;
789
790         lockdep_assert_held(&sysfs_mutex);
791
792         /* if first iteration, visit leftmost descendant which may be root */
793         if (!pos)
794                 return sysfs_leftmost_descendant(root);
795
796         /* if we visited @root, we're done */
797         if (pos == root)
798                 return NULL;
799
800         /* if there's an unvisited sibling, visit its leftmost descendant */
801         rbn = rb_next(&pos->s_rb);
802         if (rbn)
803                 return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
804
805         /* no sibling left, visit parent */
806         return pos->s_parent;
807 }
808
809 void __sysfs_remove(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
810 {
811         struct sysfs_dirent *pos, *next;
812
813         if (!sd)
814                 return;
815
816         pr_debug("sysfs %s: removing\n", sd->s_name);
817
818         next = NULL;
819         do {
820                 pos = next;
821                 next = sysfs_next_descendant_post(pos, sd);
822                 if (pos)
823                         sysfs_remove_one(acxt, pos);
824         } while (next);
825 }
826
827 /**
828  * sysfs_remove - remove a sysfs_dirent recursively
829  * @sd: the sysfs_dirent to remove
830  *
831  * Remove @sd along with all its subdirectories and files.
832  */
833 void sysfs_remove(struct sysfs_dirent *sd)
834 {
835         struct sysfs_addrm_cxt acxt;
836
837         sysfs_addrm_start(&acxt);
838         __sysfs_remove(&acxt, sd);
839         sysfs_addrm_finish(&acxt);
840 }
841
842 /**
843  *      sysfs_remove_dir - remove an object's directory.
844  *      @kobj:  object.
845  *
846  *      The only thing special about this is that we remove any files in
847  *      the directory before we remove the directory, and we've inlined
848  *      what used to be sysfs_rmdir() below, instead of calling separately.
849  */
850 void sysfs_remove_dir(struct kobject *kobj)
851 {
852         struct sysfs_dirent *sd = kobj->sd;
853
854         spin_lock(&sysfs_assoc_lock);
855         kobj->sd = NULL;
856         spin_unlock(&sysfs_assoc_lock);
857
858         if (sd) {
859                 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
860                 sysfs_remove(sd);
861         }
862 }
863
864 int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
865                  const char *new_name, const void *new_ns)
866 {
867         int error;
868
869         mutex_lock(&sysfs_mutex);
870
871         error = 0;
872         if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
873             (strcmp(sd->s_name, new_name) == 0))
874                 goto out;       /* nothing to rename */
875
876         error = -EEXIST;
877         if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
878                 goto out;
879
880         /* rename sysfs_dirent */
881         if (strcmp(sd->s_name, new_name) != 0) {
882                 error = -ENOMEM;
883                 new_name = kstrdup(new_name, GFP_KERNEL);
884                 if (!new_name)
885                         goto out;
886
887                 kfree(sd->s_name);
888                 sd->s_name = new_name;
889         }
890
891         /*
892          * Move to the appropriate place in the appropriate directories rbtree.
893          */
894         sysfs_unlink_sibling(sd);
895         sysfs_get(new_parent_sd);
896         sysfs_put(sd->s_parent);
897         sd->s_ns = new_ns;
898         sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
899         sd->s_parent = new_parent_sd;
900         sysfs_link_sibling(sd);
901
902         error = 0;
903  out:
904         mutex_unlock(&sysfs_mutex);
905         return error;
906 }
907
908 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
909                         const void *new_ns)
910 {
911         struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
912
913         return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
914 }
915
916 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
917                       const void *new_ns)
918 {
919         struct sysfs_dirent *sd = kobj->sd;
920         struct sysfs_dirent *new_parent_sd;
921
922         BUG_ON(!sd->s_parent);
923         new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
924                 new_parent_kobj->sd : &sysfs_root;
925
926         return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
927 }
928
929 /* Relationship between s_mode and the DT_xxx types */
930 static inline unsigned char dt_type(struct sysfs_dirent *sd)
931 {
932         return (sd->s_mode >> 12) & 15;
933 }
934
935 static int sysfs_dir_release(struct inode *inode, struct file *filp)
936 {
937         sysfs_put(filp->private_data);
938         return 0;
939 }
940
941 static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
942         struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
943 {
944         if (pos) {
945                 int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
946                         pos->s_parent == parent_sd &&
947                         hash == pos->s_hash;
948                 sysfs_put(pos);
949                 if (!valid)
950                         pos = NULL;
951         }
952         if (!pos && (hash > 1) && (hash < INT_MAX)) {
953                 struct rb_node *node = parent_sd->s_dir.children.rb_node;
954                 while (node) {
955                         pos = to_sysfs_dirent(node);
956
957                         if (hash < pos->s_hash)
958                                 node = node->rb_left;
959                         else if (hash > pos->s_hash)
960                                 node = node->rb_right;
961                         else
962                                 break;
963                 }
964         }
965         /* Skip over entries in the wrong namespace */
966         while (pos && pos->s_ns != ns) {
967                 struct rb_node *node = rb_next(&pos->s_rb);
968                 if (!node)
969                         pos = NULL;
970                 else
971                         pos = to_sysfs_dirent(node);
972         }
973         return pos;
974 }
975
976 static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
977         struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
978 {
979         pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
980         if (pos)
981                 do {
982                         struct rb_node *node = rb_next(&pos->s_rb);
983                         if (!node)
984                                 pos = NULL;
985                         else
986                                 pos = to_sysfs_dirent(node);
987                 } while (pos && pos->s_ns != ns);
988         return pos;
989 }
990
991 static int sysfs_readdir(struct file *file, struct dir_context *ctx)
992 {
993         struct dentry *dentry = file->f_path.dentry;
994         struct sysfs_dirent *parent_sd = dentry->d_fsdata;
995         struct sysfs_dirent *pos = file->private_data;
996         const void *ns = NULL;
997
998         if (!dir_emit_dots(file, ctx))
999                 return 0;
1000         mutex_lock(&sysfs_mutex);
1001
1002         if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
1003                 ns = sysfs_info(dentry->d_sb)->ns;
1004
1005         for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
1006              pos;
1007              pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
1008                 const char *name = pos->s_name;
1009                 unsigned int type = dt_type(pos);
1010                 int len = strlen(name);
1011                 ino_t ino = pos->s_ino;
1012                 ctx->pos = pos->s_hash;
1013                 file->private_data = sysfs_get(pos);
1014
1015                 mutex_unlock(&sysfs_mutex);
1016                 if (!dir_emit(ctx, name, len, ino, type))
1017                         return 0;
1018                 mutex_lock(&sysfs_mutex);
1019         }
1020         mutex_unlock(&sysfs_mutex);
1021         file->private_data = NULL;
1022         ctx->pos = INT_MAX;
1023         return 0;
1024 }
1025
1026 static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1027 {
1028         struct inode *inode = file_inode(file);
1029         loff_t ret;
1030
1031         mutex_lock(&inode->i_mutex);
1032         ret = generic_file_llseek(file, offset, whence);
1033         mutex_unlock(&inode->i_mutex);
1034
1035         return ret;
1036 }
1037
1038 const struct file_operations sysfs_dir_operations = {
1039         .read           = generic_read_dir,
1040         .iterate        = sysfs_readdir,
1041         .release        = sysfs_dir_release,
1042         .llseek         = sysfs_dir_llseek,
1043 };