]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/sysfs/dir.c
Merge remote-tracking branch 'pwm/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 out_valid:
319         return 1;
320 out_bad:
321         /* Remove the dentry from the dcache hashes.
322          * If this is a deleted dentry we use d_drop instead of d_delete
323          * so sysfs doesn't need to cope with negative dentries.
324          *
325          * If this is a dentry that has simply been renamed we
326          * use d_drop to remove it from the dcache lookup on its
327          * old parent.  If this dentry persists later when a lookup
328          * is performed at its new name the dentry will be readded
329          * to the dcache hashes.
330          */
331         mutex_unlock(&sysfs_mutex);
332
333         /* If we have submounts we must allow the vfs caches
334          * to lie about the state of the filesystem to prevent
335          * leaks and other nasty things.
336          */
337         if (check_submounts_and_drop(dentry) != 0)
338                 goto out_valid;
339
340         return 0;
341 }
342
343 static void sysfs_dentry_release(struct dentry *dentry)
344 {
345         sysfs_put(dentry->d_fsdata);
346 }
347
348 const struct dentry_operations sysfs_dentry_ops = {
349         .d_revalidate   = sysfs_dentry_revalidate,
350         .d_delete       = sysfs_dentry_delete,
351         .d_release      = sysfs_dentry_release,
352 };
353
354 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
355 {
356         char *dup_name = NULL;
357         struct sysfs_dirent *sd;
358
359         if (type & SYSFS_COPY_NAME) {
360                 name = dup_name = kstrdup(name, GFP_KERNEL);
361                 if (!name)
362                         return NULL;
363         }
364
365         sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
366         if (!sd)
367                 goto err_out1;
368
369         if (sysfs_alloc_ino(&sd->s_ino))
370                 goto err_out2;
371
372         atomic_set(&sd->s_count, 1);
373         atomic_set(&sd->s_active, 0);
374
375         sd->s_name = name;
376         sd->s_mode = mode;
377         sd->s_flags = type | SYSFS_FLAG_REMOVED;
378
379         return sd;
380
381  err_out2:
382         kmem_cache_free(sysfs_dir_cachep, sd);
383  err_out1:
384         kfree(dup_name);
385         return NULL;
386 }
387
388 /**
389  *      sysfs_addrm_start - prepare for sysfs_dirent add/remove
390  *      @acxt: pointer to sysfs_addrm_cxt to be used
391  *
392  *      This function is called when the caller is about to add or remove
393  *      sysfs_dirent.  This function acquires sysfs_mutex.  @acxt is used
394  *      to keep and pass context to other addrm functions.
395  *
396  *      LOCKING:
397  *      Kernel thread context (may sleep).  sysfs_mutex is locked on
398  *      return.
399  */
400 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt)
401         __acquires(sysfs_mutex)
402 {
403         memset(acxt, 0, sizeof(*acxt));
404
405         mutex_lock(&sysfs_mutex);
406 }
407
408 /**
409  *      __sysfs_add_one - add sysfs_dirent to parent without warning
410  *      @acxt: addrm context to use
411  *      @sd: sysfs_dirent to be added
412  *      @parent_sd: the parent sysfs_dirent to add @sd to
413  *
414  *      Get @parent_sd and set @sd->s_parent to it and increment nlink of
415  *      the parent inode if @sd is a directory and link into the children
416  *      list of the parent.
417  *
418  *      This function should be called between calls to
419  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
420  *      passed the same @acxt as passed to sysfs_addrm_start().
421  *
422  *      LOCKING:
423  *      Determined by sysfs_addrm_start().
424  *
425  *      RETURNS:
426  *      0 on success, -EEXIST if entry with the given name already
427  *      exists.
428  */
429 int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
430                     struct sysfs_dirent *parent_sd)
431 {
432         struct sysfs_inode_attrs *ps_iattr;
433         int ret;
434
435         sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
436         sd->s_parent = sysfs_get(parent_sd);
437
438         ret = sysfs_link_sibling(sd);
439         if (ret)
440                 return ret;
441
442         /* Update timestamps on the parent */
443         ps_iattr = parent_sd->s_iattr;
444         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
449         /* Mark the entry added into directory tree */
450         sd->s_flags &= ~SYSFS_FLAG_REMOVED;
451
452         return 0;
453 }
454
455 /**
456  *      sysfs_pathname - return full path to sysfs dirent
457  *      @sd: sysfs_dirent whose path we want
458  *      @path: caller allocated buffer of size PATH_MAX
459  *
460  *      Gives the name "/" to the sysfs_root entry; any path returned
461  *      is relative to wherever sysfs is mounted.
462  */
463 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path)
464 {
465         if (sd->s_parent) {
466                 sysfs_pathname(sd->s_parent, path);
467                 strlcat(path, "/", PATH_MAX);
468         }
469         strlcat(path, sd->s_name, PATH_MAX);
470         return path;
471 }
472
473 /**
474  *      sysfs_add_one - add sysfs_dirent to parent
475  *      @acxt: addrm context to use
476  *      @sd: sysfs_dirent to be added
477  *      @parent_sd: the parent sysfs_dirent to add @sd to
478  *
479  *      Get @parent_sd and set @sd->s_parent to it and increment nlink of
480  *      the parent inode if @sd is a directory and link into the children
481  *      list of the parent.
482  *
483  *      This function should be called between calls to
484  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
485  *      passed the same @acxt as passed to sysfs_addrm_start().
486  *
487  *      LOCKING:
488  *      Determined by sysfs_addrm_start().
489  *
490  *      RETURNS:
491  *      0 on success, -EEXIST if entry with the given name already
492  *      exists.
493  */
494 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
495                   struct sysfs_dirent *parent_sd)
496 {
497         int ret;
498
499         ret = __sysfs_add_one(acxt, sd, parent_sd);
500         if (ret == -EEXIST) {
501                 char *path = kzalloc(PATH_MAX, GFP_KERNEL);
502                 WARN(1, KERN_WARNING
503                      "sysfs: cannot create duplicate filename '%s'\n",
504                      (path == NULL) ? sd->s_name
505                                     : (sysfs_pathname(parent_sd, path),
506                                        strlcat(path, "/", PATH_MAX),
507                                        strlcat(path, sd->s_name, PATH_MAX),
508                                        path));
509                 kfree(path);
510         }
511
512         return ret;
513 }
514
515 /**
516  *      sysfs_remove_one - remove sysfs_dirent from parent
517  *      @acxt: addrm context to use
518  *      @sd: sysfs_dirent to be removed
519  *
520  *      Mark @sd removed and drop nlink of parent inode if @sd is a
521  *      directory.  @sd is unlinked from the children list.
522  *
523  *      This function should be called between calls to
524  *      sysfs_addrm_start() and sysfs_addrm_finish() and should be
525  *      passed the same @acxt as passed to sysfs_addrm_start().
526  *
527  *      LOCKING:
528  *      Determined by sysfs_addrm_start().
529  */
530 static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
531                              struct sysfs_dirent *sd)
532 {
533         struct sysfs_inode_attrs *ps_iattr;
534
535         /*
536          * Removal can be called multiple times on the same node.  Only the
537          * first invocation is effective and puts the base ref.
538          */
539         if (sd->s_flags & SYSFS_FLAG_REMOVED)
540                 return;
541
542         sysfs_unlink_sibling(sd);
543
544         /* Update timestamps on the parent */
545         ps_iattr = sd->s_parent->s_iattr;
546         if (ps_iattr) {
547                 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
548                 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
549         }
550
551         sd->s_flags |= SYSFS_FLAG_REMOVED;
552         sd->u.removed_list = acxt->removed;
553         acxt->removed = sd;
554 }
555
556 /**
557  *      sysfs_addrm_finish - finish up sysfs_dirent add/remove
558  *      @acxt: addrm context to finish up
559  *
560  *      Finish up sysfs_dirent add/remove.  Resources acquired by
561  *      sysfs_addrm_start() are released and removed sysfs_dirents are
562  *      cleaned up.
563  *
564  *      LOCKING:
565  *      sysfs_mutex is released.
566  */
567 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
568         __releases(sysfs_mutex)
569 {
570         /* release resources acquired by sysfs_addrm_start() */
571         mutex_unlock(&sysfs_mutex);
572
573         /* kill removed sysfs_dirents */
574         while (acxt->removed) {
575                 struct sysfs_dirent *sd = acxt->removed;
576
577                 acxt->removed = sd->u.removed_list;
578
579                 sysfs_deactivate(sd);
580                 sysfs_unmap_bin_file(sd);
581                 sysfs_put(sd);
582         }
583 }
584
585 /**
586  *      sysfs_find_dirent - find sysfs_dirent with the given name
587  *      @parent_sd: sysfs_dirent to search under
588  *      @name: name to look for
589  *      @ns: the namespace tag to use
590  *
591  *      Look for sysfs_dirent with name @name under @parent_sd.
592  *
593  *      LOCKING:
594  *      mutex_lock(sysfs_mutex)
595  *
596  *      RETURNS:
597  *      Pointer to sysfs_dirent if found, NULL if not.
598  */
599 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
600                                        const unsigned char *name,
601                                        const void *ns)
602 {
603         struct rb_node *node = parent_sd->s_dir.children.rb_node;
604         unsigned int hash;
605
606         hash = sysfs_name_hash(name, ns);
607         while (node) {
608                 struct sysfs_dirent *sd;
609                 int result;
610
611                 sd = to_sysfs_dirent(node);
612                 result = sysfs_name_compare(hash, name, ns, sd);
613                 if (result < 0)
614                         node = node->rb_left;
615                 else if (result > 0)
616                         node = node->rb_right;
617                 else
618                         return sd;
619         }
620         return NULL;
621 }
622
623 /**
624  *      sysfs_get_dirent_ns - find and get sysfs_dirent with the given name
625  *      @parent_sd: sysfs_dirent to search under
626  *      @name: name to look for
627  *      @ns: the namespace tag to use
628  *
629  *      Look for sysfs_dirent with name @name under @parent_sd and get
630  *      it if found.
631  *
632  *      LOCKING:
633  *      Kernel thread context (may sleep).  Grabs sysfs_mutex.
634  *
635  *      RETURNS:
636  *      Pointer to sysfs_dirent if found, NULL if not.
637  */
638 struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
639                                          const unsigned char *name,
640                                          const void *ns)
641 {
642         struct sysfs_dirent *sd;
643
644         mutex_lock(&sysfs_mutex);
645         sd = sysfs_find_dirent(parent_sd, name, ns);
646         sysfs_get(sd);
647         mutex_unlock(&sysfs_mutex);
648
649         return sd;
650 }
651 EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
652
653 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
654                       const char *name, const void *ns,
655                       struct sysfs_dirent **p_sd)
656 {
657         umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
658         struct sysfs_addrm_cxt acxt;
659         struct sysfs_dirent *sd;
660         int rc;
661
662         /* allocate */
663         sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
664         if (!sd)
665                 return -ENOMEM;
666
667         sd->s_ns = ns;
668         sd->s_dir.kobj = kobj;
669
670         /* link in */
671         sysfs_addrm_start(&acxt);
672         rc = sysfs_add_one(&acxt, sd, parent_sd);
673         sysfs_addrm_finish(&acxt);
674
675         if (rc == 0)
676                 *p_sd = sd;
677         else
678                 sysfs_put(sd);
679
680         return rc;
681 }
682
683 int sysfs_create_subdir(struct kobject *kobj, const char *name,
684                         struct sysfs_dirent **p_sd)
685 {
686         return create_dir(kobj, kobj->sd, name, NULL, p_sd);
687 }
688
689 /**
690  * sysfs_create_dir_ns - create a directory for an object with a namespace tag
691  * @kobj: object we're creating directory for
692  * @ns: the namespace tag to use
693  */
694 int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
695 {
696         struct sysfs_dirent *parent_sd, *sd;
697         int error = 0;
698
699         BUG_ON(!kobj);
700
701         if (kobj->parent)
702                 parent_sd = kobj->parent->sd;
703         else
704                 parent_sd = &sysfs_root;
705
706         if (!parent_sd)
707                 return -ENOENT;
708
709         error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
710         if (!error)
711                 kobj->sd = sd;
712         return error;
713 }
714
715 static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
716                                    unsigned int flags)
717 {
718         struct dentry *ret = NULL;
719         struct dentry *parent = dentry->d_parent;
720         struct sysfs_dirent *parent_sd = parent->d_fsdata;
721         struct sysfs_dirent *sd;
722         struct inode *inode;
723         const void *ns = NULL;
724
725         mutex_lock(&sysfs_mutex);
726
727         if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
728                 ns = sysfs_info(dir->i_sb)->ns;
729
730         sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns);
731
732         /* no such entry */
733         if (!sd) {
734                 ret = ERR_PTR(-ENOENT);
735                 goto out_unlock;
736         }
737         dentry->d_fsdata = sysfs_get(sd);
738
739         /* attach dentry and inode */
740         inode = sysfs_get_inode(dir->i_sb, sd);
741         if (!inode) {
742                 ret = ERR_PTR(-ENOMEM);
743                 goto out_unlock;
744         }
745
746         /* instantiate and hash dentry */
747         ret = d_materialise_unique(dentry, inode);
748  out_unlock:
749         mutex_unlock(&sysfs_mutex);
750         return ret;
751 }
752
753 const struct inode_operations sysfs_dir_inode_operations = {
754         .lookup         = sysfs_lookup,
755         .permission     = sysfs_permission,
756         .setattr        = sysfs_setattr,
757         .getattr        = sysfs_getattr,
758         .setxattr       = sysfs_setxattr,
759 };
760
761 static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos)
762 {
763         struct sysfs_dirent *last;
764
765         while (true) {
766                 struct rb_node *rbn;
767
768                 last = pos;
769
770                 if (sysfs_type(pos) != SYSFS_DIR)
771                         break;
772
773                 rbn = rb_first(&pos->s_dir.children);
774                 if (!rbn)
775                         break;
776
777                 pos = to_sysfs_dirent(rbn);
778         }
779
780         return last;
781 }
782
783 /**
784  * sysfs_next_descendant_post - find the next descendant for post-order walk
785  * @pos: the current position (%NULL to initiate traversal)
786  * @root: sysfs_dirent whose descendants to walk
787  *
788  * Find the next descendant to visit for post-order traversal of @root's
789  * descendants.  @root is included in the iteration and the last node to be
790  * visited.
791  */
792 static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos,
793                                                        struct sysfs_dirent *root)
794 {
795         struct rb_node *rbn;
796
797         lockdep_assert_held(&sysfs_mutex);
798
799         /* if first iteration, visit leftmost descendant which may be root */
800         if (!pos)
801                 return sysfs_leftmost_descendant(root);
802
803         /* if we visited @root, we're done */
804         if (pos == root)
805                 return NULL;
806
807         /* if there's an unvisited sibling, visit its leftmost descendant */
808         rbn = rb_next(&pos->s_rb);
809         if (rbn)
810                 return sysfs_leftmost_descendant(to_sysfs_dirent(rbn));
811
812         /* no sibling left, visit parent */
813         return pos->s_parent;
814 }
815
816 void __sysfs_remove(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
817 {
818         struct sysfs_dirent *pos, *next;
819
820         if (!sd)
821                 return;
822
823         pr_debug("sysfs %s: removing\n", sd->s_name);
824
825         next = NULL;
826         do {
827                 pos = next;
828                 next = sysfs_next_descendant_post(pos, sd);
829                 if (pos)
830                         sysfs_remove_one(acxt, pos);
831         } while (next);
832 }
833
834 /**
835  * sysfs_remove - remove a sysfs_dirent recursively
836  * @sd: the sysfs_dirent to remove
837  *
838  * Remove @sd along with all its subdirectories and files.
839  */
840 void sysfs_remove(struct sysfs_dirent *sd)
841 {
842         struct sysfs_addrm_cxt acxt;
843
844         sysfs_addrm_start(&acxt);
845         __sysfs_remove(&acxt, sd);
846         sysfs_addrm_finish(&acxt);
847 }
848
849 /**
850  *      sysfs_remove_dir - remove an object's directory.
851  *      @kobj:  object.
852  *
853  *      The only thing special about this is that we remove any files in
854  *      the directory before we remove the directory, and we've inlined
855  *      what used to be sysfs_rmdir() below, instead of calling separately.
856  */
857 void sysfs_remove_dir(struct kobject *kobj)
858 {
859         struct sysfs_dirent *sd = kobj->sd;
860
861         spin_lock(&sysfs_assoc_lock);
862         kobj->sd = NULL;
863         spin_unlock(&sysfs_assoc_lock);
864
865         if (sd) {
866                 WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
867                 sysfs_remove(sd);
868         }
869 }
870
871 int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
872                  const char *new_name, const void *new_ns)
873 {
874         int error;
875
876         mutex_lock(&sysfs_mutex);
877
878         error = 0;
879         if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) &&
880             (strcmp(sd->s_name, new_name) == 0))
881                 goto out;       /* nothing to rename */
882
883         error = -EEXIST;
884         if (sysfs_find_dirent(new_parent_sd, new_name, new_ns))
885                 goto out;
886
887         /* rename sysfs_dirent */
888         if (strcmp(sd->s_name, new_name) != 0) {
889                 error = -ENOMEM;
890                 new_name = kstrdup(new_name, GFP_KERNEL);
891                 if (!new_name)
892                         goto out;
893
894                 kfree(sd->s_name);
895                 sd->s_name = new_name;
896         }
897
898         /*
899          * Move to the appropriate place in the appropriate directories rbtree.
900          */
901         sysfs_unlink_sibling(sd);
902         sysfs_get(new_parent_sd);
903         sysfs_put(sd->s_parent);
904         sd->s_ns = new_ns;
905         sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns);
906         sd->s_parent = new_parent_sd;
907         sysfs_link_sibling(sd);
908
909         error = 0;
910  out:
911         mutex_unlock(&sysfs_mutex);
912         return error;
913 }
914
915 int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
916                         const void *new_ns)
917 {
918         struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
919
920         return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns);
921 }
922
923 int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
924                       const void *new_ns)
925 {
926         struct sysfs_dirent *sd = kobj->sd;
927         struct sysfs_dirent *new_parent_sd;
928
929         BUG_ON(!sd->s_parent);
930         new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
931                 new_parent_kobj->sd : &sysfs_root;
932
933         return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns);
934 }
935
936 /* Relationship between s_mode and the DT_xxx types */
937 static inline unsigned char dt_type(struct sysfs_dirent *sd)
938 {
939         return (sd->s_mode >> 12) & 15;
940 }
941
942 static int sysfs_dir_release(struct inode *inode, struct file *filp)
943 {
944         sysfs_put(filp->private_data);
945         return 0;
946 }
947
948 static struct sysfs_dirent *sysfs_dir_pos(const void *ns,
949         struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos)
950 {
951         if (pos) {
952                 int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) &&
953                         pos->s_parent == parent_sd &&
954                         hash == pos->s_hash;
955                 sysfs_put(pos);
956                 if (!valid)
957                         pos = NULL;
958         }
959         if (!pos && (hash > 1) && (hash < INT_MAX)) {
960                 struct rb_node *node = parent_sd->s_dir.children.rb_node;
961                 while (node) {
962                         pos = to_sysfs_dirent(node);
963
964                         if (hash < pos->s_hash)
965                                 node = node->rb_left;
966                         else if (hash > pos->s_hash)
967                                 node = node->rb_right;
968                         else
969                                 break;
970                 }
971         }
972         /* Skip over entries in the wrong namespace */
973         while (pos && pos->s_ns != ns) {
974                 struct rb_node *node = rb_next(&pos->s_rb);
975                 if (!node)
976                         pos = NULL;
977                 else
978                         pos = to_sysfs_dirent(node);
979         }
980         return pos;
981 }
982
983 static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns,
984         struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos)
985 {
986         pos = sysfs_dir_pos(ns, parent_sd, ino, pos);
987         if (pos)
988                 do {
989                         struct rb_node *node = rb_next(&pos->s_rb);
990                         if (!node)
991                                 pos = NULL;
992                         else
993                                 pos = to_sysfs_dirent(node);
994                 } while (pos && pos->s_ns != ns);
995         return pos;
996 }
997
998 static int sysfs_readdir(struct file *file, struct dir_context *ctx)
999 {
1000         struct dentry *dentry = file->f_path.dentry;
1001         struct sysfs_dirent *parent_sd = dentry->d_fsdata;
1002         struct sysfs_dirent *pos = file->private_data;
1003         const void *ns = NULL;
1004
1005         if (!dir_emit_dots(file, ctx))
1006                 return 0;
1007         mutex_lock(&sysfs_mutex);
1008
1009         if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS)
1010                 ns = sysfs_info(dentry->d_sb)->ns;
1011
1012         for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos);
1013              pos;
1014              pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) {
1015                 const char *name = pos->s_name;
1016                 unsigned int type = dt_type(pos);
1017                 int len = strlen(name);
1018                 ino_t ino = pos->s_ino;
1019                 ctx->pos = pos->s_hash;
1020                 file->private_data = sysfs_get(pos);
1021
1022                 mutex_unlock(&sysfs_mutex);
1023                 if (!dir_emit(ctx, name, len, ino, type))
1024                         return 0;
1025                 mutex_lock(&sysfs_mutex);
1026         }
1027         mutex_unlock(&sysfs_mutex);
1028         file->private_data = NULL;
1029         ctx->pos = INT_MAX;
1030         return 0;
1031 }
1032
1033 static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1034 {
1035         struct inode *inode = file_inode(file);
1036         loff_t ret;
1037
1038         mutex_lock(&inode->i_mutex);
1039         ret = generic_file_llseek(file, offset, whence);
1040         mutex_unlock(&inode->i_mutex);
1041
1042         return ret;
1043 }
1044
1045 const struct file_operations sysfs_dir_operations = {
1046         .read           = generic_read_dir,
1047         .iterate        = sysfs_readdir,
1048         .release        = sysfs_dir_release,
1049         .llseek         = sysfs_dir_llseek,
1050 };