aboutsummaryrefslogtreecommitdiffstats
path: root/fs/kernfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/kernfs')
-rw-r--r--fs/kernfs/Makefile5
-rw-r--r--fs/kernfs/dir.c1077
-rw-r--r--fs/kernfs/file.c867
-rw-r--r--fs/kernfs/inode.c377
-rw-r--r--fs/kernfs/kernfs-internal.h122
-rw-r--r--fs/kernfs/mount.c171
-rw-r--r--fs/kernfs/symlink.c151
7 files changed, 2770 insertions, 0 deletions
diff --git a/fs/kernfs/Makefile b/fs/kernfs/Makefile
new file mode 100644
index 000000000000..674337c76673
--- /dev/null
+++ b/fs/kernfs/Makefile
@@ -0,0 +1,5 @@
1#
2# Makefile for the kernfs pseudo filesystem
3#
4
5obj-y := mount.o inode.o dir.o file.o symlink.o
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
new file mode 100644
index 000000000000..bd6e18be6e1a
--- /dev/null
+++ b/fs/kernfs/dir.c
@@ -0,0 +1,1077 @@
1/*
2 * fs/kernfs/dir.c - kernfs directory implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/idr.h>
14#include <linux/slab.h>
15#include <linux/security.h>
16#include <linux/hash.h>
17
18#include "kernfs-internal.h"
19
20DEFINE_MUTEX(kernfs_mutex);
21
22#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
23
24/**
25 * kernfs_name_hash
26 * @name: Null terminated string to hash
27 * @ns: Namespace tag to hash
28 *
29 * Returns 31 bit hash of ns + name (so it fits in an off_t )
30 */
31static unsigned int kernfs_name_hash(const char *name, const void *ns)
32{
33 unsigned long hash = init_name_hash();
34 unsigned int len = strlen(name);
35 while (len--)
36 hash = partial_name_hash(*name++, hash);
37 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
38 hash &= 0x7fffffffU;
39 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
40 if (hash < 1)
41 hash += 2;
42 if (hash >= INT_MAX)
43 hash = INT_MAX - 1;
44 return hash;
45}
46
47static int kernfs_name_compare(unsigned int hash, const char *name,
48 const void *ns, const struct kernfs_node *kn)
49{
50 if (hash != kn->hash)
51 return hash - kn->hash;
52 if (ns != kn->ns)
53 return ns - kn->ns;
54 return strcmp(name, kn->name);
55}
56
57static int kernfs_sd_compare(const struct kernfs_node *left,
58 const struct kernfs_node *right)
59{
60 return kernfs_name_compare(left->hash, left->name, left->ns, right);
61}
62
63/**
64 * kernfs_link_sibling - link kernfs_node into sibling rbtree
65 * @kn: kernfs_node of interest
66 *
67 * Link @kn into its sibling rbtree which starts from
68 * @kn->parent->dir.children.
69 *
70 * Locking:
71 * mutex_lock(kernfs_mutex)
72 *
73 * RETURNS:
74 * 0 on susccess -EEXIST on failure.
75 */
76static int kernfs_link_sibling(struct kernfs_node *kn)
77{
78 struct rb_node **node = &kn->parent->dir.children.rb_node;
79 struct rb_node *parent = NULL;
80
81 if (kernfs_type(kn) == KERNFS_DIR)
82 kn->parent->dir.subdirs++;
83
84 while (*node) {
85 struct kernfs_node *pos;
86 int result;
87
88 pos = rb_to_kn(*node);
89 parent = *node;
90 result = kernfs_sd_compare(kn, pos);
91 if (result < 0)
92 node = &pos->rb.rb_left;
93 else if (result > 0)
94 node = &pos->rb.rb_right;
95 else
96 return -EEXIST;
97 }
98 /* add new node and rebalance the tree */
99 rb_link_node(&kn->rb, parent, node);
100 rb_insert_color(&kn->rb, &kn->parent->dir.children);
101 return 0;
102}
103
104/**
105 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
106 * @kn: kernfs_node of interest
107 *
108 * Unlink @kn from its sibling rbtree which starts from
109 * kn->parent->dir.children.
110 *
111 * Locking:
112 * mutex_lock(kernfs_mutex)
113 */
114static void kernfs_unlink_sibling(struct kernfs_node *kn)
115{
116 if (kernfs_type(kn) == KERNFS_DIR)
117 kn->parent->dir.subdirs--;
118
119 rb_erase(&kn->rb, &kn->parent->dir.children);
120}
121
122/**
123 * kernfs_get_active - get an active reference to kernfs_node
124 * @kn: kernfs_node to get an active reference to
125 *
126 * Get an active reference of @kn. This function is noop if @kn
127 * is NULL.
128 *
129 * RETURNS:
130 * Pointer to @kn on success, NULL on failure.
131 */
132struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
133{
134 if (unlikely(!kn))
135 return NULL;
136
137 if (!atomic_inc_unless_negative(&kn->active))
138 return NULL;
139
140 if (kn->flags & KERNFS_LOCKDEP)
141 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
142 return kn;
143}
144
145/**
146 * kernfs_put_active - put an active reference to kernfs_node
147 * @kn: kernfs_node to put an active reference to
148 *
149 * Put an active reference to @kn. This function is noop if @kn
150 * is NULL.
151 */
152void kernfs_put_active(struct kernfs_node *kn)
153{
154 int v;
155
156 if (unlikely(!kn))
157 return;
158
159 if (kn->flags & KERNFS_LOCKDEP)
160 rwsem_release(&kn->dep_map, 1, _RET_IP_);
161 v = atomic_dec_return(&kn->active);
162 if (likely(v != KN_DEACTIVATED_BIAS))
163 return;
164
165 /*
166 * atomic_dec_return() is a mb(), we'll always see the updated
167 * kn->u.completion.
168 */
169 complete(kn->u.completion);
170}
171
172/**
173 * kernfs_deactivate - deactivate kernfs_node
174 * @kn: kernfs_node to deactivate
175 *
176 * Deny new active references and drain existing ones.
177 */
178static void kernfs_deactivate(struct kernfs_node *kn)
179{
180 DECLARE_COMPLETION_ONSTACK(wait);
181 int v;
182
183 BUG_ON(!(kn->flags & KERNFS_REMOVED));
184
185 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
186 return;
187
188 kn->u.completion = (void *)&wait;
189
190 if (kn->flags & KERNFS_LOCKDEP)
191 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
192 /* atomic_add_return() is a mb(), put_active() will always see
193 * the updated kn->u.completion.
194 */
195 v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
196
197 if (v != KN_DEACTIVATED_BIAS) {
198 if (kn->flags & KERNFS_LOCKDEP)
199 lock_contended(&kn->dep_map, _RET_IP_);
200 wait_for_completion(&wait);
201 }
202
203 if (kn->flags & KERNFS_LOCKDEP) {
204 lock_acquired(&kn->dep_map, _RET_IP_);
205 rwsem_release(&kn->dep_map, 1, _RET_IP_);
206 }
207}
208
209/**
210 * kernfs_get - get a reference count on a kernfs_node
211 * @kn: the target kernfs_node
212 */
213void kernfs_get(struct kernfs_node *kn)
214{
215 if (kn) {
216 WARN_ON(!atomic_read(&kn->count));
217 atomic_inc(&kn->count);
218 }
219}
220EXPORT_SYMBOL_GPL(kernfs_get);
221
222/**
223 * kernfs_put - put a reference count on a kernfs_node
224 * @kn: the target kernfs_node
225 *
226 * Put a reference count of @kn and destroy it if it reached zero.
227 */
228void kernfs_put(struct kernfs_node *kn)
229{
230 struct kernfs_node *parent;
231 struct kernfs_root *root;
232
233 if (!kn || !atomic_dec_and_test(&kn->count))
234 return;
235 root = kernfs_root(kn);
236 repeat:
237 /* Moving/renaming is always done while holding reference.
238 * kn->parent won't change beneath us.
239 */
240 parent = kn->parent;
241
242 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
243 parent ? parent->name : "", kn->name);
244
245 if (kernfs_type(kn) == KERNFS_LINK)
246 kernfs_put(kn->symlink.target_kn);
247 if (!(kn->flags & KERNFS_STATIC_NAME))
248 kfree(kn->name);
249 if (kn->iattr) {
250 if (kn->iattr->ia_secdata)
251 security_release_secctx(kn->iattr->ia_secdata,
252 kn->iattr->ia_secdata_len);
253 simple_xattrs_free(&kn->iattr->xattrs);
254 }
255 kfree(kn->iattr);
256 ida_simple_remove(&root->ino_ida, kn->ino);
257 kmem_cache_free(kernfs_node_cache, kn);
258
259 kn = parent;
260 if (kn) {
261 if (atomic_dec_and_test(&kn->count))
262 goto repeat;
263 } else {
264 /* just released the root kn, free @root too */
265 ida_destroy(&root->ino_ida);
266 kfree(root);
267 }
268}
269EXPORT_SYMBOL_GPL(kernfs_put);
270
271static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
272{
273 struct kernfs_node *kn;
274
275 if (flags & LOOKUP_RCU)
276 return -ECHILD;
277
278 /* Always perform fresh lookup for negatives */
279 if (!dentry->d_inode)
280 goto out_bad_unlocked;
281
282 kn = dentry->d_fsdata;
283 mutex_lock(&kernfs_mutex);
284
285 /* The kernfs node has been deleted */
286 if (kn->flags & KERNFS_REMOVED)
287 goto out_bad;
288
289 /* The kernfs node has been moved? */
290 if (dentry->d_parent->d_fsdata != kn->parent)
291 goto out_bad;
292
293 /* The kernfs node has been renamed */
294 if (strcmp(dentry->d_name.name, kn->name) != 0)
295 goto out_bad;
296
297 /* The kernfs node has been moved to a different namespace */
298 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
299 kernfs_info(dentry->d_sb)->ns != kn->ns)
300 goto out_bad;
301
302 mutex_unlock(&kernfs_mutex);
303out_valid:
304 return 1;
305out_bad:
306 mutex_unlock(&kernfs_mutex);
307out_bad_unlocked:
308 /*
309 * @dentry doesn't match the underlying kernfs node, drop the
310 * dentry and force lookup. If we have submounts we must allow the
311 * vfs caches to lie about the state of the filesystem to prevent
312 * leaks and other nasty things, so use check_submounts_and_drop()
313 * instead of d_drop().
314 */
315 if (check_submounts_and_drop(dentry) != 0)
316 goto out_valid;
317
318 return 0;
319}
320
321static void kernfs_dop_release(struct dentry *dentry)
322{
323 kernfs_put(dentry->d_fsdata);
324}
325
326const struct dentry_operations kernfs_dops = {
327 .d_revalidate = kernfs_dop_revalidate,
328 .d_release = kernfs_dop_release,
329};
330
331static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
332 const char *name, umode_t mode,
333 unsigned flags)
334{
335 char *dup_name = NULL;
336 struct kernfs_node *kn;
337 int ret;
338
339 if (!(flags & KERNFS_STATIC_NAME)) {
340 name = dup_name = kstrdup(name, GFP_KERNEL);
341 if (!name)
342 return NULL;
343 }
344
345 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
346 if (!kn)
347 goto err_out1;
348
349 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
350 if (ret < 0)
351 goto err_out2;
352 kn->ino = ret;
353
354 atomic_set(&kn->count, 1);
355 atomic_set(&kn->active, 0);
356
357 kn->name = name;
358 kn->mode = mode;
359 kn->flags = flags | KERNFS_REMOVED;
360
361 return kn;
362
363 err_out2:
364 kmem_cache_free(kernfs_node_cache, kn);
365 err_out1:
366 kfree(dup_name);
367 return NULL;
368}
369
370struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
371 const char *name, umode_t mode,
372 unsigned flags)
373{
374 struct kernfs_node *kn;
375
376 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
377 if (kn) {
378 kernfs_get(parent);
379 kn->parent = parent;
380 }
381 return kn;
382}
383
384/**
385 * kernfs_addrm_start - prepare for kernfs_node add/remove
386 * @acxt: pointer to kernfs_addrm_cxt to be used
387 *
388 * This function is called when the caller is about to add or remove
389 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
390 * to keep and pass context to other addrm functions.
391 *
392 * LOCKING:
393 * Kernel thread context (may sleep). kernfs_mutex is locked on
394 * return.
395 */
396void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
397 __acquires(kernfs_mutex)
398{
399 memset(acxt, 0, sizeof(*acxt));
400
401 mutex_lock(&kernfs_mutex);
402}
403
404/**
405 * kernfs_add_one - add kernfs_node to parent without warning
406 * @acxt: addrm context to use
407 * @kn: kernfs_node to be added
408 *
409 * The caller must already have initialized @kn->parent. This
410 * function increments nlink of the parent's inode if @kn is a
411 * directory and link into the children list of the parent.
412 *
413 * This function should be called between calls to
414 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
415 * the same @acxt as passed to kernfs_addrm_start().
416 *
417 * LOCKING:
418 * Determined by kernfs_addrm_start().
419 *
420 * RETURNS:
421 * 0 on success, -EEXIST if entry with the given name already
422 * exists.
423 */
424int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
425{
426 struct kernfs_node *parent = kn->parent;
427 bool has_ns = kernfs_ns_enabled(parent);
428 struct kernfs_iattrs *ps_iattr;
429 int ret;
430
431 if (has_ns != (bool)kn->ns) {
432 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
433 has_ns ? "required" : "invalid", parent->name, kn->name);
434 return -EINVAL;
435 }
436
437 if (kernfs_type(parent) != KERNFS_DIR)
438 return -EINVAL;
439
440 if (parent->flags & KERNFS_REMOVED)
441 return -ENOENT;
442
443 kn->hash = kernfs_name_hash(kn->name, kn->ns);
444
445 ret = kernfs_link_sibling(kn);
446 if (ret)
447 return ret;
448
449 /* Update timestamps on the parent */
450 ps_iattr = parent->iattr;
451 if (ps_iattr) {
452 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
453 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
454 }
455
456 /* Mark the entry added into directory tree */
457 kn->flags &= ~KERNFS_REMOVED;
458
459 return 0;
460}
461
462/**
463 * kernfs_remove_one - remove kernfs_node from parent
464 * @acxt: addrm context to use
465 * @kn: kernfs_node to be removed
466 *
467 * Mark @kn removed and drop nlink of parent inode if @kn is a
468 * directory. @kn is unlinked from the children list.
469 *
470 * This function should be called between calls to
471 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
472 * passed the same @acxt as passed to kernfs_addrm_start().
473 *
474 * LOCKING:
475 * Determined by kernfs_addrm_start().
476 */
477static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
478 struct kernfs_node *kn)
479{
480 struct kernfs_iattrs *ps_iattr;
481
482 /*
483 * Removal can be called multiple times on the same node. Only the
484 * first invocation is effective and puts the base ref.
485 */
486 if (kn->flags & KERNFS_REMOVED)
487 return;
488
489 if (kn->parent) {
490 kernfs_unlink_sibling(kn);
491
492 /* Update timestamps on the parent */
493 ps_iattr = kn->parent->iattr;
494 if (ps_iattr) {
495 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
496 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
497 }
498 }
499
500 kn->flags |= KERNFS_REMOVED;
501 kn->u.removed_list = acxt->removed;
502 acxt->removed = kn;
503}
504
505/**
506 * kernfs_addrm_finish - finish up kernfs_node add/remove
507 * @acxt: addrm context to finish up
508 *
509 * Finish up kernfs_node add/remove. Resources acquired by
510 * kernfs_addrm_start() are released and removed kernfs_nodes are
511 * cleaned up.
512 *
513 * LOCKING:
514 * kernfs_mutex is released.
515 */
516void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
517 __releases(kernfs_mutex)
518{
519 /* release resources acquired by kernfs_addrm_start() */
520 mutex_unlock(&kernfs_mutex);
521
522 /* kill removed kernfs_nodes */
523 while (acxt->removed) {
524 struct kernfs_node *kn = acxt->removed;
525
526 acxt->removed = kn->u.removed_list;
527
528 kernfs_deactivate(kn);
529 kernfs_unmap_bin_file(kn);
530 kernfs_put(kn);
531 }
532}
533
534/**
535 * kernfs_find_ns - find kernfs_node with the given name
536 * @parent: kernfs_node to search under
537 * @name: name to look for
538 * @ns: the namespace tag to use
539 *
540 * Look for kernfs_node with name @name under @parent. Returns pointer to
541 * the found kernfs_node on success, %NULL on failure.
542 */
543static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
544 const unsigned char *name,
545 const void *ns)
546{
547 struct rb_node *node = parent->dir.children.rb_node;
548 bool has_ns = kernfs_ns_enabled(parent);
549 unsigned int hash;
550
551 lockdep_assert_held(&kernfs_mutex);
552
553 if (has_ns != (bool)ns) {
554 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
555 has_ns ? "required" : "invalid", parent->name, name);
556 return NULL;
557 }
558
559 hash = kernfs_name_hash(name, ns);
560 while (node) {
561 struct kernfs_node *kn;
562 int result;
563
564 kn = rb_to_kn(node);
565 result = kernfs_name_compare(hash, name, ns, kn);
566 if (result < 0)
567 node = node->rb_left;
568 else if (result > 0)
569 node = node->rb_right;
570 else
571 return kn;
572 }
573 return NULL;
574}
575
576/**
577 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
578 * @parent: kernfs_node to search under
579 * @name: name to look for
580 * @ns: the namespace tag to use
581 *
582 * Look for kernfs_node with name @name under @parent and get a reference
583 * if found. This function may sleep and returns pointer to the found
584 * kernfs_node on success, %NULL on failure.
585 */
586struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
587 const char *name, const void *ns)
588{
589 struct kernfs_node *kn;
590
591 mutex_lock(&kernfs_mutex);
592 kn = kernfs_find_ns(parent, name, ns);
593 kernfs_get(kn);
594 mutex_unlock(&kernfs_mutex);
595
596 return kn;
597}
598EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
599
600/**
601 * kernfs_create_root - create a new kernfs hierarchy
602 * @kdops: optional directory syscall operations for the hierarchy
603 * @priv: opaque data associated with the new directory
604 *
605 * Returns the root of the new hierarchy on success, ERR_PTR() value on
606 * failure.
607 */
608struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
609{
610 struct kernfs_root *root;
611 struct kernfs_node *kn;
612
613 root = kzalloc(sizeof(*root), GFP_KERNEL);
614 if (!root)
615 return ERR_PTR(-ENOMEM);
616
617 ida_init(&root->ino_ida);
618
619 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
620 KERNFS_DIR);
621 if (!kn) {
622 ida_destroy(&root->ino_ida);
623 kfree(root);
624 return ERR_PTR(-ENOMEM);
625 }
626
627 kn->flags &= ~KERNFS_REMOVED;
628 kn->priv = priv;
629 kn->dir.root = root;
630
631 root->dir_ops = kdops;
632 root->kn = kn;
633
634 return root;
635}
636
637/**
638 * kernfs_destroy_root - destroy a kernfs hierarchy
639 * @root: root of the hierarchy to destroy
640 *
641 * Destroy the hierarchy anchored at @root by removing all existing
642 * directories and destroying @root.
643 */
644void kernfs_destroy_root(struct kernfs_root *root)
645{
646 kernfs_remove(root->kn); /* will also free @root */
647}
648
649/**
650 * kernfs_create_dir_ns - create a directory
651 * @parent: parent in which to create a new directory
652 * @name: name of the new directory
653 * @mode: mode of the new directory
654 * @priv: opaque data associated with the new directory
655 * @ns: optional namespace tag of the directory
656 *
657 * Returns the created node on success, ERR_PTR() value on failure.
658 */
659struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
660 const char *name, umode_t mode,
661 void *priv, const void *ns)
662{
663 struct kernfs_addrm_cxt acxt;
664 struct kernfs_node *kn;
665 int rc;
666
667 /* allocate */
668 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
669 if (!kn)
670 return ERR_PTR(-ENOMEM);
671
672 kn->dir.root = parent->dir.root;
673 kn->ns = ns;
674 kn->priv = priv;
675
676 /* link in */
677 kernfs_addrm_start(&acxt);
678 rc = kernfs_add_one(&acxt, kn);
679 kernfs_addrm_finish(&acxt);
680
681 if (!rc)
682 return kn;
683
684 kernfs_put(kn);
685 return ERR_PTR(rc);
686}
687
688static struct dentry *kernfs_iop_lookup(struct inode *dir,
689 struct dentry *dentry,
690 unsigned int flags)
691{
692 struct dentry *ret;
693 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
694 struct kernfs_node *kn;
695 struct inode *inode;
696 const void *ns = NULL;
697
698 mutex_lock(&kernfs_mutex);
699
700 if (kernfs_ns_enabled(parent))
701 ns = kernfs_info(dir->i_sb)->ns;
702
703 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
704
705 /* no such entry */
706 if (!kn) {
707 ret = NULL;
708 goto out_unlock;
709 }
710 kernfs_get(kn);
711 dentry->d_fsdata = kn;
712
713 /* attach dentry and inode */
714 inode = kernfs_get_inode(dir->i_sb, kn);
715 if (!inode) {
716 ret = ERR_PTR(-ENOMEM);
717 goto out_unlock;
718 }
719
720 /* instantiate and hash dentry */
721 ret = d_materialise_unique(dentry, inode);
722 out_unlock:
723 mutex_unlock(&kernfs_mutex);
724 return ret;
725}
726
727static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
728 umode_t mode)
729{
730 struct kernfs_node *parent = dir->i_private;
731 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
732
733 if (!kdops || !kdops->mkdir)
734 return -EPERM;
735
736 return kdops->mkdir(parent, dentry->d_name.name, mode);
737}
738
739static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
740{
741 struct kernfs_node *kn = dentry->d_fsdata;
742 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
743
744 if (!kdops || !kdops->rmdir)
745 return -EPERM;
746
747 return kdops->rmdir(kn);
748}
749
750static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
751 struct inode *new_dir, struct dentry *new_dentry)
752{
753 struct kernfs_node *kn = old_dentry->d_fsdata;
754 struct kernfs_node *new_parent = new_dir->i_private;
755 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
756
757 if (!kdops || !kdops->rename)
758 return -EPERM;
759
760 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
761}
762
763const struct inode_operations kernfs_dir_iops = {
764 .lookup = kernfs_iop_lookup,
765 .permission = kernfs_iop_permission,
766 .setattr = kernfs_iop_setattr,
767 .getattr = kernfs_iop_getattr,
768 .setxattr = kernfs_iop_setxattr,
769 .removexattr = kernfs_iop_removexattr,
770 .getxattr = kernfs_iop_getxattr,
771 .listxattr = kernfs_iop_listxattr,
772
773 .mkdir = kernfs_iop_mkdir,
774 .rmdir = kernfs_iop_rmdir,
775 .rename = kernfs_iop_rename,
776};
777
778static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
779{
780 struct kernfs_node *last;
781
782 while (true) {
783 struct rb_node *rbn;
784
785 last = pos;
786
787 if (kernfs_type(pos) != KERNFS_DIR)
788 break;
789
790 rbn = rb_first(&pos->dir.children);
791 if (!rbn)
792 break;
793
794 pos = rb_to_kn(rbn);
795 }
796
797 return last;
798}
799
800/**
801 * kernfs_next_descendant_post - find the next descendant for post-order walk
802 * @pos: the current position (%NULL to initiate traversal)
803 * @root: kernfs_node whose descendants to walk
804 *
805 * Find the next descendant to visit for post-order traversal of @root's
806 * descendants. @root is included in the iteration and the last node to be
807 * visited.
808 */
809static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
810 struct kernfs_node *root)
811{
812 struct rb_node *rbn;
813
814 lockdep_assert_held(&kernfs_mutex);
815
816 /* if first iteration, visit leftmost descendant which may be root */
817 if (!pos)
818 return kernfs_leftmost_descendant(root);
819
820 /* if we visited @root, we're done */
821 if (pos == root)
822 return NULL;
823
824 /* if there's an unvisited sibling, visit its leftmost descendant */
825 rbn = rb_next(&pos->rb);
826 if (rbn)
827 return kernfs_leftmost_descendant(rb_to_kn(rbn));
828
829 /* no sibling left, visit parent */
830 return pos->parent;
831}
832
833static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
834 struct kernfs_node *kn)
835{
836 struct kernfs_node *pos, *next;
837
838 if (!kn)
839 return;
840
841 pr_debug("kernfs %s: removing\n", kn->name);
842
843 next = NULL;
844 do {
845 pos = next;
846 next = kernfs_next_descendant_post(pos, kn);
847 if (pos)
848 kernfs_remove_one(acxt, pos);
849 } while (next);
850}
851
852/**
853 * kernfs_remove - remove a kernfs_node recursively
854 * @kn: the kernfs_node to remove
855 *
856 * Remove @kn along with all its subdirectories and files.
857 */
858void kernfs_remove(struct kernfs_node *kn)
859{
860 struct kernfs_addrm_cxt acxt;
861
862 kernfs_addrm_start(&acxt);
863 __kernfs_remove(&acxt, kn);
864 kernfs_addrm_finish(&acxt);
865}
866
867/**
868 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
869 * @parent: parent of the target
870 * @name: name of the kernfs_node to remove
871 * @ns: namespace tag of the kernfs_node to remove
872 *
873 * Look for the kernfs_node with @name and @ns under @parent and remove it.
874 * Returns 0 on success, -ENOENT if such entry doesn't exist.
875 */
876int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
877 const void *ns)
878{
879 struct kernfs_addrm_cxt acxt;
880 struct kernfs_node *kn;
881
882 if (!parent) {
883 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
884 name);
885 return -ENOENT;
886 }
887
888 kernfs_addrm_start(&acxt);
889
890 kn = kernfs_find_ns(parent, name, ns);
891 if (kn)
892 __kernfs_remove(&acxt, kn);
893
894 kernfs_addrm_finish(&acxt);
895
896 if (kn)
897 return 0;
898 else
899 return -ENOENT;
900}
901
902/**
903 * kernfs_rename_ns - move and rename a kernfs_node
904 * @kn: target node
905 * @new_parent: new parent to put @sd under
906 * @new_name: new name
907 * @new_ns: new namespace tag
908 */
909int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
910 const char *new_name, const void *new_ns)
911{
912 int error;
913
914 mutex_lock(&kernfs_mutex);
915
916 error = -ENOENT;
917 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
918 goto out;
919
920 error = 0;
921 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
922 (strcmp(kn->name, new_name) == 0))
923 goto out; /* nothing to rename */
924
925 error = -EEXIST;
926 if (kernfs_find_ns(new_parent, new_name, new_ns))
927 goto out;
928
929 /* rename kernfs_node */
930 if (strcmp(kn->name, new_name) != 0) {
931 error = -ENOMEM;
932 new_name = kstrdup(new_name, GFP_KERNEL);
933 if (!new_name)
934 goto out;
935
936 if (kn->flags & KERNFS_STATIC_NAME)
937 kn->flags &= ~KERNFS_STATIC_NAME;
938 else
939 kfree(kn->name);
940
941 kn->name = new_name;
942 }
943
944 /*
945 * Move to the appropriate place in the appropriate directories rbtree.
946 */
947 kernfs_unlink_sibling(kn);
948 kernfs_get(new_parent);
949 kernfs_put(kn->parent);
950 kn->ns = new_ns;
951 kn->hash = kernfs_name_hash(kn->name, kn->ns);
952 kn->parent = new_parent;
953 kernfs_link_sibling(kn);
954
955 error = 0;
956 out:
957 mutex_unlock(&kernfs_mutex);
958 return error;
959}
960
961/* Relationship between s_mode and the DT_xxx types */
962static inline unsigned char dt_type(struct kernfs_node *kn)
963{
964 return (kn->mode >> 12) & 15;
965}
966
967static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
968{
969 kernfs_put(filp->private_data);
970 return 0;
971}
972
973static struct kernfs_node *kernfs_dir_pos(const void *ns,
974 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
975{
976 if (pos) {
977 int valid = !(pos->flags & KERNFS_REMOVED) &&
978 pos->parent == parent && hash == pos->hash;
979 kernfs_put(pos);
980 if (!valid)
981 pos = NULL;
982 }
983 if (!pos && (hash > 1) && (hash < INT_MAX)) {
984 struct rb_node *node = parent->dir.children.rb_node;
985 while (node) {
986 pos = rb_to_kn(node);
987
988 if (hash < pos->hash)
989 node = node->rb_left;
990 else if (hash > pos->hash)
991 node = node->rb_right;
992 else
993 break;
994 }
995 }
996 /* Skip over entries in the wrong namespace */
997 while (pos && pos->ns != ns) {
998 struct rb_node *node = rb_next(&pos->rb);
999 if (!node)
1000 pos = NULL;
1001 else
1002 pos = rb_to_kn(node);
1003 }
1004 return pos;
1005}
1006
1007static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1008 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1009{
1010 pos = kernfs_dir_pos(ns, parent, ino, pos);
1011 if (pos)
1012 do {
1013 struct rb_node *node = rb_next(&pos->rb);
1014 if (!node)
1015 pos = NULL;
1016 else
1017 pos = rb_to_kn(node);
1018 } while (pos && pos->ns != ns);
1019 return pos;
1020}
1021
1022static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1023{
1024 struct dentry *dentry = file->f_path.dentry;
1025 struct kernfs_node *parent = dentry->d_fsdata;
1026 struct kernfs_node *pos = file->private_data;
1027 const void *ns = NULL;
1028
1029 if (!dir_emit_dots(file, ctx))
1030 return 0;
1031 mutex_lock(&kernfs_mutex);
1032
1033 if (kernfs_ns_enabled(parent))
1034 ns = kernfs_info(dentry->d_sb)->ns;
1035
1036 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1037 pos;
1038 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1039 const char *name = pos->name;
1040 unsigned int type = dt_type(pos);
1041 int len = strlen(name);
1042 ino_t ino = pos->ino;
1043
1044 ctx->pos = pos->hash;
1045 file->private_data = pos;
1046 kernfs_get(pos);
1047
1048 mutex_unlock(&kernfs_mutex);
1049 if (!dir_emit(ctx, name, len, ino, type))
1050 return 0;
1051 mutex_lock(&kernfs_mutex);
1052 }
1053 mutex_unlock(&kernfs_mutex);
1054 file->private_data = NULL;
1055 ctx->pos = INT_MAX;
1056 return 0;
1057}
1058
1059static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1060 int whence)
1061{
1062 struct inode *inode = file_inode(file);
1063 loff_t ret;
1064
1065 mutex_lock(&inode->i_mutex);
1066 ret = generic_file_llseek(file, offset, whence);
1067 mutex_unlock(&inode->i_mutex);
1068
1069 return ret;
1070}
1071
1072const struct file_operations kernfs_dir_fops = {
1073 .read = generic_read_dir,
1074 .iterate = kernfs_fop_readdir,
1075 .release = kernfs_dir_fop_release,
1076 .llseek = kernfs_dir_fop_llseek,
1077};
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
new file mode 100644
index 000000000000..dbf397bfdff2
--- /dev/null
+++ b/fs/kernfs/file.c
@@ -0,0 +1,867 @@
1/*
2 * fs/kernfs/file.c - kernfs file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#include <linux/fs.h>
12#include <linux/seq_file.h>
13#include <linux/slab.h>
14#include <linux/poll.h>
15#include <linux/pagemap.h>
16#include <linux/sched.h>
17
18#include "kernfs-internal.h"
19
20/*
21 * There's one kernfs_open_file for each open file and one kernfs_open_node
22 * for each kernfs_node with one or more open files.
23 *
24 * kernfs_node->attr.open points to kernfs_open_node. attr.open is
25 * protected by kernfs_open_node_lock.
26 *
27 * filp->private_data points to seq_file whose ->private points to
28 * kernfs_open_file. kernfs_open_files are chained at
29 * kernfs_open_node->files, which is protected by kernfs_open_file_mutex.
30 */
31static DEFINE_SPINLOCK(kernfs_open_node_lock);
32static DEFINE_MUTEX(kernfs_open_file_mutex);
33
34struct kernfs_open_node {
35 atomic_t refcnt;
36 atomic_t event;
37 wait_queue_head_t poll;
38 struct list_head files; /* goes through kernfs_open_file.list */
39};
40
41static struct kernfs_open_file *kernfs_of(struct file *file)
42{
43 return ((struct seq_file *)file->private_data)->private;
44}
45
46/*
47 * Determine the kernfs_ops for the given kernfs_node. This function must
48 * be called while holding an active reference.
49 */
50static const struct kernfs_ops *kernfs_ops(struct kernfs_node *kn)
51{
52 if (kn->flags & KERNFS_LOCKDEP)
53 lockdep_assert_held(kn);
54 return kn->attr.ops;
55}
56
57/*
58 * As kernfs_seq_stop() is also called after kernfs_seq_start() or
59 * kernfs_seq_next() failure, it needs to distinguish whether it's stopping
60 * a seq_file iteration which is fully initialized with an active reference
61 * or an aborted kernfs_seq_start() due to get_active failure. The
62 * position pointer is the only context for each seq_file iteration and
63 * thus the stop condition should be encoded in it. As the return value is
64 * directly visible to userland, ERR_PTR(-ENODEV) is the only acceptable
65 * choice to indicate get_active failure.
66 *
67 * Unfortunately, this is complicated due to the optional custom seq_file
68 * operations which may return ERR_PTR(-ENODEV) too. kernfs_seq_stop()
69 * can't distinguish whether ERR_PTR(-ENODEV) is from get_active failure or
70 * custom seq_file operations and thus can't decide whether put_active
71 * should be performed or not only on ERR_PTR(-ENODEV).
72 *
73 * This is worked around by factoring out the custom seq_stop() and
74 * put_active part into kernfs_seq_stop_active(), skipping it from
75 * kernfs_seq_stop() if ERR_PTR(-ENODEV) while invoking it directly after
76 * custom seq_file operations fail with ERR_PTR(-ENODEV) - this ensures
77 * that kernfs_seq_stop_active() is skipped only after get_active failure.
78 */
79static void kernfs_seq_stop_active(struct seq_file *sf, void *v)
80{
81 struct kernfs_open_file *of = sf->private;
82 const struct kernfs_ops *ops = kernfs_ops(of->kn);
83
84 if (ops->seq_stop)
85 ops->seq_stop(sf, v);
86 kernfs_put_active(of->kn);
87}
88
89static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
90{
91 struct kernfs_open_file *of = sf->private;
92 const struct kernfs_ops *ops;
93
94 /*
95 * @of->mutex nests outside active ref and is just to ensure that
96 * the ops aren't called concurrently for the same open file.
97 */
98 mutex_lock(&of->mutex);
99 if (!kernfs_get_active(of->kn))
100 return ERR_PTR(-ENODEV);
101
102 ops = kernfs_ops(of->kn);
103 if (ops->seq_start) {
104 void *next = ops->seq_start(sf, ppos);
105 /* see the comment above kernfs_seq_stop_active() */
106 if (next == ERR_PTR(-ENODEV))
107 kernfs_seq_stop_active(sf, next);
108 return next;
109 } else {
110 /*
111 * The same behavior and code as single_open(). Returns
112 * !NULL if pos is at the beginning; otherwise, NULL.
113 */
114 return NULL + !*ppos;
115 }
116}
117
118static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
119{
120 struct kernfs_open_file *of = sf->private;
121 const struct kernfs_ops *ops = kernfs_ops(of->kn);
122
123 if (ops->seq_next) {
124 void *next = ops->seq_next(sf, v, ppos);
125 /* see the comment above kernfs_seq_stop_active() */
126 if (next == ERR_PTR(-ENODEV))
127 kernfs_seq_stop_active(sf, next);
128 return next;
129 } else {
130 /*
131 * The same behavior and code as single_open(), always
132 * terminate after the initial read.
133 */
134 ++*ppos;
135 return NULL;
136 }
137}
138
139static void kernfs_seq_stop(struct seq_file *sf, void *v)
140{
141 struct kernfs_open_file *of = sf->private;
142
143 if (v != ERR_PTR(-ENODEV))
144 kernfs_seq_stop_active(sf, v);
145 mutex_unlock(&of->mutex);
146}
147
148static int kernfs_seq_show(struct seq_file *sf, void *v)
149{
150 struct kernfs_open_file *of = sf->private;
151
152 of->event = atomic_read(&of->kn->attr.open->event);
153
154 return of->kn->attr.ops->seq_show(sf, v);
155}
156
157static const struct seq_operations kernfs_seq_ops = {
158 .start = kernfs_seq_start,
159 .next = kernfs_seq_next,
160 .stop = kernfs_seq_stop,
161 .show = kernfs_seq_show,
162};
163
164/*
165 * As reading a bin file can have side-effects, the exact offset and bytes
166 * specified in read(2) call should be passed to the read callback making
167 * it difficult to use seq_file. Implement simplistic custom buffering for
168 * bin files.
169 */
170static ssize_t kernfs_file_direct_read(struct kernfs_open_file *of,
171 char __user *user_buf, size_t count,
172 loff_t *ppos)
173{
174 ssize_t len = min_t(size_t, count, PAGE_SIZE);
175 const struct kernfs_ops *ops;
176 char *buf;
177
178 buf = kmalloc(len, GFP_KERNEL);
179 if (!buf)
180 return -ENOMEM;
181
182 /*
183 * @of->mutex nests outside active ref and is just to ensure that
184 * the ops aren't called concurrently for the same open file.
185 */
186 mutex_lock(&of->mutex);
187 if (!kernfs_get_active(of->kn)) {
188 len = -ENODEV;
189 mutex_unlock(&of->mutex);
190 goto out_free;
191 }
192
193 ops = kernfs_ops(of->kn);
194 if (ops->read)
195 len = ops->read(of, buf, len, *ppos);
196 else
197 len = -EINVAL;
198
199 kernfs_put_active(of->kn);
200 mutex_unlock(&of->mutex);
201
202 if (len < 0)
203 goto out_free;
204
205 if (copy_to_user(user_buf, buf, len)) {
206 len = -EFAULT;
207 goto out_free;
208 }
209
210 *ppos += len;
211
212 out_free:
213 kfree(buf);
214 return len;
215}
216
217/**
218 * kernfs_fop_read - kernfs vfs read callback
219 * @file: file pointer
220 * @user_buf: data to write
221 * @count: number of bytes
222 * @ppos: starting offset
223 */
224static ssize_t kernfs_fop_read(struct file *file, char __user *user_buf,
225 size_t count, loff_t *ppos)
226{
227 struct kernfs_open_file *of = kernfs_of(file);
228
229 if (of->kn->flags & KERNFS_HAS_SEQ_SHOW)
230 return seq_read(file, user_buf, count, ppos);
231 else
232 return kernfs_file_direct_read(of, user_buf, count, ppos);
233}
234
235/**
236 * kernfs_fop_write - kernfs vfs write callback
237 * @file: file pointer
238 * @user_buf: data to write
239 * @count: number of bytes
240 * @ppos: starting offset
241 *
242 * Copy data in from userland and pass it to the matching kernfs write
243 * operation.
244 *
245 * There is no easy way for us to know if userspace is only doing a partial
246 * write, so we don't support them. We expect the entire buffer to come on
247 * the first write. Hint: if you're writing a value, first read the file,
248 * modify only the the value you're changing, then write entire buffer
249 * back.
250 */
251static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf,
252 size_t count, loff_t *ppos)
253{
254 struct kernfs_open_file *of = kernfs_of(file);
255 ssize_t len = min_t(size_t, count, PAGE_SIZE);
256 const struct kernfs_ops *ops;
257 char *buf;
258
259 buf = kmalloc(len + 1, GFP_KERNEL);
260 if (!buf)
261 return -ENOMEM;
262
263 if (copy_from_user(buf, user_buf, len)) {
264 len = -EFAULT;
265 goto out_free;
266 }
267 buf[len] = '\0'; /* guarantee string termination */
268
269 /*
270 * @of->mutex nests outside active ref and is just to ensure that
271 * the ops aren't called concurrently for the same open file.
272 */
273 mutex_lock(&of->mutex);
274 if (!kernfs_get_active(of->kn)) {
275 mutex_unlock(&of->mutex);
276 len = -ENODEV;
277 goto out_free;
278 }
279
280 ops = kernfs_ops(of->kn);
281 if (ops->write)
282 len = ops->write(of, buf, len, *ppos);
283 else
284 len = -EINVAL;
285
286 kernfs_put_active(of->kn);
287 mutex_unlock(&of->mutex);
288
289 if (len > 0)
290 *ppos += len;
291out_free:
292 kfree(buf);
293 return len;
294}
295
296static void kernfs_vma_open(struct vm_area_struct *vma)
297{
298 struct file *file = vma->vm_file;
299 struct kernfs_open_file *of = kernfs_of(file);
300
301 if (!of->vm_ops)
302 return;
303
304 if (!kernfs_get_active(of->kn))
305 return;
306
307 if (of->vm_ops->open)
308 of->vm_ops->open(vma);
309
310 kernfs_put_active(of->kn);
311}
312
313static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
314{
315 struct file *file = vma->vm_file;
316 struct kernfs_open_file *of = kernfs_of(file);
317 int ret;
318
319 if (!of->vm_ops)
320 return VM_FAULT_SIGBUS;
321
322 if (!kernfs_get_active(of->kn))
323 return VM_FAULT_SIGBUS;
324
325 ret = VM_FAULT_SIGBUS;
326 if (of->vm_ops->fault)
327 ret = of->vm_ops->fault(vma, vmf);
328
329 kernfs_put_active(of->kn);
330 return ret;
331}
332
333static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
334 struct vm_fault *vmf)
335{
336 struct file *file = vma->vm_file;
337 struct kernfs_open_file *of = kernfs_of(file);
338 int ret;
339
340 if (!of->vm_ops)
341 return VM_FAULT_SIGBUS;
342
343 if (!kernfs_get_active(of->kn))
344 return VM_FAULT_SIGBUS;
345
346 ret = 0;
347 if (of->vm_ops->page_mkwrite)
348 ret = of->vm_ops->page_mkwrite(vma, vmf);
349 else
350 file_update_time(file);
351
352 kernfs_put_active(of->kn);
353 return ret;
354}
355
356static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
357 void *buf, int len, int write)
358{
359 struct file *file = vma->vm_file;
360 struct kernfs_open_file *of = kernfs_of(file);
361 int ret;
362
363 if (!of->vm_ops)
364 return -EINVAL;
365
366 if (!kernfs_get_active(of->kn))
367 return -EINVAL;
368
369 ret = -EINVAL;
370 if (of->vm_ops->access)
371 ret = of->vm_ops->access(vma, addr, buf, len, write);
372
373 kernfs_put_active(of->kn);
374 return ret;
375}
376
377#ifdef CONFIG_NUMA
378static int kernfs_vma_set_policy(struct vm_area_struct *vma,
379 struct mempolicy *new)
380{
381 struct file *file = vma->vm_file;
382 struct kernfs_open_file *of = kernfs_of(file);
383 int ret;
384
385 if (!of->vm_ops)
386 return 0;
387
388 if (!kernfs_get_active(of->kn))
389 return -EINVAL;
390
391 ret = 0;
392 if (of->vm_ops->set_policy)
393 ret = of->vm_ops->set_policy(vma, new);
394
395 kernfs_put_active(of->kn);
396 return ret;
397}
398
399static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
400 unsigned long addr)
401{
402 struct file *file = vma->vm_file;
403 struct kernfs_open_file *of = kernfs_of(file);
404 struct mempolicy *pol;
405
406 if (!of->vm_ops)
407 return vma->vm_policy;
408
409 if (!kernfs_get_active(of->kn))
410 return vma->vm_policy;
411
412 pol = vma->vm_policy;
413 if (of->vm_ops->get_policy)
414 pol = of->vm_ops->get_policy(vma, addr);
415
416 kernfs_put_active(of->kn);
417 return pol;
418}
419
420static int kernfs_vma_migrate(struct vm_area_struct *vma,
421 const nodemask_t *from, const nodemask_t *to,
422 unsigned long flags)
423{
424 struct file *file = vma->vm_file;
425 struct kernfs_open_file *of = kernfs_of(file);
426 int ret;
427
428 if (!of->vm_ops)
429 return 0;
430
431 if (!kernfs_get_active(of->kn))
432 return 0;
433
434 ret = 0;
435 if (of->vm_ops->migrate)
436 ret = of->vm_ops->migrate(vma, from, to, flags);
437
438 kernfs_put_active(of->kn);
439 return ret;
440}
441#endif
442
443static const struct vm_operations_struct kernfs_vm_ops = {
444 .open = kernfs_vma_open,
445 .fault = kernfs_vma_fault,
446 .page_mkwrite = kernfs_vma_page_mkwrite,
447 .access = kernfs_vma_access,
448#ifdef CONFIG_NUMA
449 .set_policy = kernfs_vma_set_policy,
450 .get_policy = kernfs_vma_get_policy,
451 .migrate = kernfs_vma_migrate,
452#endif
453};
454
455static int kernfs_fop_mmap(struct file *file, struct vm_area_struct *vma)
456{
457 struct kernfs_open_file *of = kernfs_of(file);
458 const struct kernfs_ops *ops;
459 int rc;
460
461 /*
462 * mmap path and of->mutex are prone to triggering spurious lockdep
463 * warnings and we don't want to add spurious locking dependency
464 * between the two. Check whether mmap is actually implemented
465 * without grabbing @of->mutex by testing HAS_MMAP flag. See the
466 * comment in kernfs_file_open() for more details.
467 */
468 if (!(of->kn->flags & KERNFS_HAS_MMAP))
469 return -ENODEV;
470
471 mutex_lock(&of->mutex);
472
473 rc = -ENODEV;
474 if (!kernfs_get_active(of->kn))
475 goto out_unlock;
476
477 ops = kernfs_ops(of->kn);
478 rc = ops->mmap(of, vma);
479
480 /*
481 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
482 * to satisfy versions of X which crash if the mmap fails: that
483 * substitutes a new vm_file, and we don't then want bin_vm_ops.
484 */
485 if (vma->vm_file != file)
486 goto out_put;
487
488 rc = -EINVAL;
489 if (of->mmapped && of->vm_ops != vma->vm_ops)
490 goto out_put;
491
492 /*
493 * It is not possible to successfully wrap close.
494 * So error if someone is trying to use close.
495 */
496 rc = -EINVAL;
497 if (vma->vm_ops && vma->vm_ops->close)
498 goto out_put;
499
500 rc = 0;
501 of->mmapped = 1;
502 of->vm_ops = vma->vm_ops;
503 vma->vm_ops = &kernfs_vm_ops;
504out_put:
505 kernfs_put_active(of->kn);
506out_unlock:
507 mutex_unlock(&of->mutex);
508
509 return rc;
510}
511
512/**
513 * kernfs_get_open_node - get or create kernfs_open_node
514 * @kn: target kernfs_node
515 * @of: kernfs_open_file for this instance of open
516 *
517 * If @kn->attr.open exists, increment its reference count; otherwise,
518 * create one. @of is chained to the files list.
519 *
520 * LOCKING:
521 * Kernel thread context (may sleep).
522 *
523 * RETURNS:
524 * 0 on success, -errno on failure.
525 */
526static int kernfs_get_open_node(struct kernfs_node *kn,
527 struct kernfs_open_file *of)
528{
529 struct kernfs_open_node *on, *new_on = NULL;
530
531 retry:
532 mutex_lock(&kernfs_open_file_mutex);
533 spin_lock_irq(&kernfs_open_node_lock);
534
535 if (!kn->attr.open && new_on) {
536 kn->attr.open = new_on;
537 new_on = NULL;
538 }
539
540 on = kn->attr.open;
541 if (on) {
542 atomic_inc(&on->refcnt);
543 list_add_tail(&of->list, &on->files);
544 }
545
546 spin_unlock_irq(&kernfs_open_node_lock);
547 mutex_unlock(&kernfs_open_file_mutex);
548
549 if (on) {
550 kfree(new_on);
551 return 0;
552 }
553
554 /* not there, initialize a new one and retry */
555 new_on = kmalloc(sizeof(*new_on), GFP_KERNEL);
556 if (!new_on)
557 return -ENOMEM;
558
559 atomic_set(&new_on->refcnt, 0);
560 atomic_set(&new_on->event, 1);
561 init_waitqueue_head(&new_on->poll);
562 INIT_LIST_HEAD(&new_on->files);
563 goto retry;
564}
565
566/**
567 * kernfs_put_open_node - put kernfs_open_node
568 * @kn: target kernfs_nodet
569 * @of: associated kernfs_open_file
570 *
571 * Put @kn->attr.open and unlink @of from the files list. If
572 * reference count reaches zero, disassociate and free it.
573 *
574 * LOCKING:
575 * None.
576 */
577static void kernfs_put_open_node(struct kernfs_node *kn,
578 struct kernfs_open_file *of)
579{
580 struct kernfs_open_node *on = kn->attr.open;
581 unsigned long flags;
582
583 mutex_lock(&kernfs_open_file_mutex);
584 spin_lock_irqsave(&kernfs_open_node_lock, flags);
585
586 if (of)
587 list_del(&of->list);
588
589 if (atomic_dec_and_test(&on->refcnt))
590 kn->attr.open = NULL;
591 else
592 on = NULL;
593
594 spin_unlock_irqrestore(&kernfs_open_node_lock, flags);
595 mutex_unlock(&kernfs_open_file_mutex);
596
597 kfree(on);
598}
599
600static int kernfs_fop_open(struct inode *inode, struct file *file)
601{
602 struct kernfs_node *kn = file->f_path.dentry->d_fsdata;
603 const struct kernfs_ops *ops;
604 struct kernfs_open_file *of;
605 bool has_read, has_write, has_mmap;
606 int error = -EACCES;
607
608 if (!kernfs_get_active(kn))
609 return -ENODEV;
610
611 ops = kernfs_ops(kn);
612
613 has_read = ops->seq_show || ops->read || ops->mmap;
614 has_write = ops->write || ops->mmap;
615 has_mmap = ops->mmap;
616
617 /* check perms and supported operations */
618 if ((file->f_mode & FMODE_WRITE) &&
619 (!(inode->i_mode & S_IWUGO) || !has_write))
620 goto err_out;
621
622 if ((file->f_mode & FMODE_READ) &&
623 (!(inode->i_mode & S_IRUGO) || !has_read))
624 goto err_out;
625
626 /* allocate a kernfs_open_file for the file */
627 error = -ENOMEM;
628 of = kzalloc(sizeof(struct kernfs_open_file), GFP_KERNEL);
629 if (!of)
630 goto err_out;
631
632 /*
633 * The following is done to give a different lockdep key to
634 * @of->mutex for files which implement mmap. This is a rather
635 * crude way to avoid false positive lockdep warning around
636 * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
637 * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
638 * which mm->mmap_sem nests, while holding @of->mutex. As each
639 * open file has a separate mutex, it's okay as long as those don't
640 * happen on the same file. At this point, we can't easily give
641 * each file a separate locking class. Let's differentiate on
642 * whether the file has mmap or not for now.
643 *
644 * Both paths of the branch look the same. They're supposed to
645 * look that way and give @of->mutex different static lockdep keys.
646 */
647 if (has_mmap)
648 mutex_init(&of->mutex);
649 else
650 mutex_init(&of->mutex);
651
652 of->kn = kn;
653 of->file = file;
654
655 /*
656 * Always instantiate seq_file even if read access doesn't use
657 * seq_file or is not requested. This unifies private data access
658 * and readable regular files are the vast majority anyway.
659 */
660 if (ops->seq_show)
661 error = seq_open(file, &kernfs_seq_ops);
662 else
663 error = seq_open(file, NULL);
664 if (error)
665 goto err_free;
666
667 ((struct seq_file *)file->private_data)->private = of;
668
669 /* seq_file clears PWRITE unconditionally, restore it if WRITE */
670 if (file->f_mode & FMODE_WRITE)
671 file->f_mode |= FMODE_PWRITE;
672
673 /* make sure we have open node struct */
674 error = kernfs_get_open_node(kn, of);
675 if (error)
676 goto err_close;
677
678 /* open succeeded, put active references */
679 kernfs_put_active(kn);
680 return 0;
681
682err_close:
683 seq_release(inode, file);
684err_free:
685 kfree(of);
686err_out:
687 kernfs_put_active(kn);
688 return error;
689}
690
691static int kernfs_fop_release(struct inode *inode, struct file *filp)
692{
693 struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
694 struct kernfs_open_file *of = kernfs_of(filp);
695
696 kernfs_put_open_node(kn, of);
697 seq_release(inode, filp);
698 kfree(of);
699
700 return 0;
701}
702
703void kernfs_unmap_bin_file(struct kernfs_node *kn)
704{
705 struct kernfs_open_node *on;
706 struct kernfs_open_file *of;
707
708 if (!(kn->flags & KERNFS_HAS_MMAP))
709 return;
710
711 spin_lock_irq(&kernfs_open_node_lock);
712 on = kn->attr.open;
713 if (on)
714 atomic_inc(&on->refcnt);
715 spin_unlock_irq(&kernfs_open_node_lock);
716 if (!on)
717 return;
718
719 mutex_lock(&kernfs_open_file_mutex);
720 list_for_each_entry(of, &on->files, list) {
721 struct inode *inode = file_inode(of->file);
722 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
723 }
724 mutex_unlock(&kernfs_open_file_mutex);
725
726 kernfs_put_open_node(kn, NULL);
727}
728
729/*
730 * Kernfs attribute files are pollable. The idea is that you read
731 * the content and then you use 'poll' or 'select' to wait for
732 * the content to change. When the content changes (assuming the
733 * manager for the kobject supports notification), poll will
734 * return POLLERR|POLLPRI, and select will return the fd whether
735 * it is waiting for read, write, or exceptions.
736 * Once poll/select indicates that the value has changed, you
737 * need to close and re-open the file, or seek to 0 and read again.
738 * Reminder: this only works for attributes which actively support
739 * it, and it is not possible to test an attribute from userspace
740 * to see if it supports poll (Neither 'poll' nor 'select' return
741 * an appropriate error code). When in doubt, set a suitable timeout value.
742 */
743static unsigned int kernfs_fop_poll(struct file *filp, poll_table *wait)
744{
745 struct kernfs_open_file *of = kernfs_of(filp);
746 struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
747 struct kernfs_open_node *on = kn->attr.open;
748
749 /* need parent for the kobj, grab both */
750 if (!kernfs_get_active(kn))
751 goto trigger;
752
753 poll_wait(filp, &on->poll, wait);
754
755 kernfs_put_active(kn);
756
757 if (of->event != atomic_read(&on->event))
758 goto trigger;
759
760 return DEFAULT_POLLMASK;
761
762 trigger:
763 return DEFAULT_POLLMASK|POLLERR|POLLPRI;
764}
765
766/**
767 * kernfs_notify - notify a kernfs file
768 * @kn: file to notify
769 *
770 * Notify @kn such that poll(2) on @kn wakes up.
771 */
772void kernfs_notify(struct kernfs_node *kn)
773{
774 struct kernfs_open_node *on;
775 unsigned long flags;
776
777 spin_lock_irqsave(&kernfs_open_node_lock, flags);
778
779 if (!WARN_ON(kernfs_type(kn) != KERNFS_FILE)) {
780 on = kn->attr.open;
781 if (on) {
782 atomic_inc(&on->event);
783 wake_up_interruptible(&on->poll);
784 }
785 }
786
787 spin_unlock_irqrestore(&kernfs_open_node_lock, flags);
788}
789EXPORT_SYMBOL_GPL(kernfs_notify);
790
791const struct file_operations kernfs_file_fops = {
792 .read = kernfs_fop_read,
793 .write = kernfs_fop_write,
794 .llseek = generic_file_llseek,
795 .mmap = kernfs_fop_mmap,
796 .open = kernfs_fop_open,
797 .release = kernfs_fop_release,
798 .poll = kernfs_fop_poll,
799};
800
801/**
802 * __kernfs_create_file - kernfs internal function to create a file
803 * @parent: directory to create the file in
804 * @name: name of the file
805 * @mode: mode of the file
806 * @size: size of the file
807 * @ops: kernfs operations for the file
808 * @priv: private data for the file
809 * @ns: optional namespace tag of the file
810 * @static_name: don't copy file name
811 * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
812 *
813 * Returns the created node on success, ERR_PTR() value on error.
814 */
815struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
816 const char *name,
817 umode_t mode, loff_t size,
818 const struct kernfs_ops *ops,
819 void *priv, const void *ns,
820 bool name_is_static,
821 struct lock_class_key *key)
822{
823 struct kernfs_addrm_cxt acxt;
824 struct kernfs_node *kn;
825 unsigned flags;
826 int rc;
827
828 flags = KERNFS_FILE;
829 if (name_is_static)
830 flags |= KERNFS_STATIC_NAME;
831
832 kn = kernfs_new_node(parent, name, (mode & S_IALLUGO) | S_IFREG, flags);
833 if (!kn)
834 return ERR_PTR(-ENOMEM);
835
836 kn->attr.ops = ops;
837 kn->attr.size = size;
838 kn->ns = ns;
839 kn->priv = priv;
840
841#ifdef CONFIG_DEBUG_LOCK_ALLOC
842 if (key) {
843 lockdep_init_map(&kn->dep_map, "s_active", key, 0);
844 kn->flags |= KERNFS_LOCKDEP;
845 }
846#endif
847
848 /*
849 * kn->attr.ops is accesible only while holding active ref. We
850 * need to know whether some ops are implemented outside active
851 * ref. Cache their existence in flags.
852 */
853 if (ops->seq_show)
854 kn->flags |= KERNFS_HAS_SEQ_SHOW;
855 if (ops->mmap)
856 kn->flags |= KERNFS_HAS_MMAP;
857
858 kernfs_addrm_start(&acxt);
859 rc = kernfs_add_one(&acxt, kn);
860 kernfs_addrm_finish(&acxt);
861
862 if (rc) {
863 kernfs_put(kn);
864 return ERR_PTR(rc);
865 }
866 return kn;
867}
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
new file mode 100644
index 000000000000..e55126f85bd2
--- /dev/null
+++ b/fs/kernfs/inode.c
@@ -0,0 +1,377 @@
1/*
2 * fs/kernfs/inode.c - kernfs inode implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#include <linux/pagemap.h>
12#include <linux/backing-dev.h>
13#include <linux/capability.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/xattr.h>
17#include <linux/security.h>
18
19#include "kernfs-internal.h"
20
21static const struct address_space_operations kernfs_aops = {
22 .readpage = simple_readpage,
23 .write_begin = simple_write_begin,
24 .write_end = simple_write_end,
25};
26
27static struct backing_dev_info kernfs_bdi = {
28 .name = "kernfs",
29 .ra_pages = 0, /* No readahead */
30 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
31};
32
33static const struct inode_operations kernfs_iops = {
34 .permission = kernfs_iop_permission,
35 .setattr = kernfs_iop_setattr,
36 .getattr = kernfs_iop_getattr,
37 .setxattr = kernfs_iop_setxattr,
38 .removexattr = kernfs_iop_removexattr,
39 .getxattr = kernfs_iop_getxattr,
40 .listxattr = kernfs_iop_listxattr,
41};
42
43void __init kernfs_inode_init(void)
44{
45 if (bdi_init(&kernfs_bdi))
46 panic("failed to init kernfs_bdi");
47}
48
49static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
50{
51 struct iattr *iattrs;
52
53 if (kn->iattr)
54 return kn->iattr;
55
56 kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
57 if (!kn->iattr)
58 return NULL;
59 iattrs = &kn->iattr->ia_iattr;
60
61 /* assign default attributes */
62 iattrs->ia_mode = kn->mode;
63 iattrs->ia_uid = GLOBAL_ROOT_UID;
64 iattrs->ia_gid = GLOBAL_ROOT_GID;
65 iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;
66
67 simple_xattrs_init(&kn->iattr->xattrs);
68
69 return kn->iattr;
70}
71
72static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
73{
74 struct kernfs_iattrs *attrs;
75 struct iattr *iattrs;
76 unsigned int ia_valid = iattr->ia_valid;
77
78 attrs = kernfs_iattrs(kn);
79 if (!attrs)
80 return -ENOMEM;
81
82 iattrs = &attrs->ia_iattr;
83
84 if (ia_valid & ATTR_UID)
85 iattrs->ia_uid = iattr->ia_uid;
86 if (ia_valid & ATTR_GID)
87 iattrs->ia_gid = iattr->ia_gid;
88 if (ia_valid & ATTR_ATIME)
89 iattrs->ia_atime = iattr->ia_atime;
90 if (ia_valid & ATTR_MTIME)
91 iattrs->ia_mtime = iattr->ia_mtime;
92 if (ia_valid & ATTR_CTIME)
93 iattrs->ia_ctime = iattr->ia_ctime;
94 if (ia_valid & ATTR_MODE) {
95 umode_t mode = iattr->ia_mode;
96 iattrs->ia_mode = kn->mode = mode;
97 }
98 return 0;
99}
100
101/**
102 * kernfs_setattr - set iattr on a node
103 * @kn: target node
104 * @iattr: iattr to set
105 *
106 * Returns 0 on success, -errno on failure.
107 */
108int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
109{
110 int ret;
111
112 mutex_lock(&kernfs_mutex);
113 ret = __kernfs_setattr(kn, iattr);
114 mutex_unlock(&kernfs_mutex);
115 return ret;
116}
117
118int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
119{
120 struct inode *inode = dentry->d_inode;
121 struct kernfs_node *kn = dentry->d_fsdata;
122 int error;
123
124 if (!kn)
125 return -EINVAL;
126
127 mutex_lock(&kernfs_mutex);
128 error = inode_change_ok(inode, iattr);
129 if (error)
130 goto out;
131
132 error = __kernfs_setattr(kn, iattr);
133 if (error)
134 goto out;
135
136 /* this ignores size changes */
137 setattr_copy(inode, iattr);
138
139out:
140 mutex_unlock(&kernfs_mutex);
141 return error;
142}
143
144static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
145 u32 *secdata_len)
146{
147 struct kernfs_iattrs *attrs;
148 void *old_secdata;
149 size_t old_secdata_len;
150
151 attrs = kernfs_iattrs(kn);
152 if (!attrs)
153 return -ENOMEM;
154
155 old_secdata = attrs->ia_secdata;
156 old_secdata_len = attrs->ia_secdata_len;
157
158 attrs->ia_secdata = *secdata;
159 attrs->ia_secdata_len = *secdata_len;
160
161 *secdata = old_secdata;
162 *secdata_len = old_secdata_len;
163 return 0;
164}
165
166int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
167 const void *value, size_t size, int flags)
168{
169 struct kernfs_node *kn = dentry->d_fsdata;
170 struct kernfs_iattrs *attrs;
171 void *secdata;
172 int error;
173 u32 secdata_len = 0;
174
175 attrs = kernfs_iattrs(kn);
176 if (!attrs)
177 return -ENOMEM;
178
179 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
180 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
181 error = security_inode_setsecurity(dentry->d_inode, suffix,
182 value, size, flags);
183 if (error)
184 return error;
185 error = security_inode_getsecctx(dentry->d_inode,
186 &secdata, &secdata_len);
187 if (error)
188 return error;
189
190 mutex_lock(&kernfs_mutex);
191 error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
192 mutex_unlock(&kernfs_mutex);
193
194 if (secdata)
195 security_release_secctx(secdata, secdata_len);
196 return error;
197 } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
198 return simple_xattr_set(&attrs->xattrs, name, value, size,
199 flags);
200 }
201
202 return -EINVAL;
203}
204
205int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
206{
207 struct kernfs_node *kn = dentry->d_fsdata;
208 struct kernfs_iattrs *attrs;
209
210 attrs = kernfs_iattrs(kn);
211 if (!attrs)
212 return -ENOMEM;
213
214 return simple_xattr_remove(&attrs->xattrs, name);
215}
216
217ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
218 size_t size)
219{
220 struct kernfs_node *kn = dentry->d_fsdata;
221 struct kernfs_iattrs *attrs;
222
223 attrs = kernfs_iattrs(kn);
224 if (!attrs)
225 return -ENOMEM;
226
227 return simple_xattr_get(&attrs->xattrs, name, buf, size);
228}
229
230ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
231{
232 struct kernfs_node *kn = dentry->d_fsdata;
233 struct kernfs_iattrs *attrs;
234
235 attrs = kernfs_iattrs(kn);
236 if (!attrs)
237 return -ENOMEM;
238
239 return simple_xattr_list(&attrs->xattrs, buf, size);
240}
241
242static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
243{
244 inode->i_mode = mode;
245 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
246}
247
248static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
249{
250 inode->i_uid = iattr->ia_uid;
251 inode->i_gid = iattr->ia_gid;
252 inode->i_atime = iattr->ia_atime;
253 inode->i_mtime = iattr->ia_mtime;
254 inode->i_ctime = iattr->ia_ctime;
255}
256
257static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
258{
259 struct kernfs_iattrs *attrs = kn->iattr;
260
261 inode->i_mode = kn->mode;
262 if (attrs) {
263 /*
264 * kernfs_node has non-default attributes get them from
265 * persistent copy in kernfs_node.
266 */
267 set_inode_attr(inode, &attrs->ia_iattr);
268 security_inode_notifysecctx(inode, attrs->ia_secdata,
269 attrs->ia_secdata_len);
270 }
271
272 if (kernfs_type(kn) == KERNFS_DIR)
273 set_nlink(inode, kn->dir.subdirs + 2);
274}
275
276int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
277 struct kstat *stat)
278{
279 struct kernfs_node *kn = dentry->d_fsdata;
280 struct inode *inode = dentry->d_inode;
281
282 mutex_lock(&kernfs_mutex);
283 kernfs_refresh_inode(kn, inode);
284 mutex_unlock(&kernfs_mutex);
285
286 generic_fillattr(inode, stat);
287 return 0;
288}
289
290static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
291{
292 kernfs_get(kn);
293 inode->i_private = kn;
294 inode->i_mapping->a_ops = &kernfs_aops;
295 inode->i_mapping->backing_dev_info = &kernfs_bdi;
296 inode->i_op = &kernfs_iops;
297
298 set_default_inode_attr(inode, kn->mode);
299 kernfs_refresh_inode(kn, inode);
300
301 /* initialize inode according to type */
302 switch (kernfs_type(kn)) {
303 case KERNFS_DIR:
304 inode->i_op = &kernfs_dir_iops;
305 inode->i_fop = &kernfs_dir_fops;
306 break;
307 case KERNFS_FILE:
308 inode->i_size = kn->attr.size;
309 inode->i_fop = &kernfs_file_fops;
310 break;
311 case KERNFS_LINK:
312 inode->i_op = &kernfs_symlink_iops;
313 break;
314 default:
315 BUG();
316 }
317
318 unlock_new_inode(inode);
319}
320
321/**
322 * kernfs_get_inode - get inode for kernfs_node
323 * @sb: super block
324 * @kn: kernfs_node to allocate inode for
325 *
326 * Get inode for @kn. If such inode doesn't exist, a new inode is
327 * allocated and basics are initialized. New inode is returned
328 * locked.
329 *
330 * LOCKING:
331 * Kernel thread context (may sleep).
332 *
333 * RETURNS:
334 * Pointer to allocated inode on success, NULL on failure.
335 */
336struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
337{
338 struct inode *inode;
339
340 inode = iget_locked(sb, kn->ino);
341 if (inode && (inode->i_state & I_NEW))
342 kernfs_init_inode(kn, inode);
343
344 return inode;
345}
346
347/*
348 * The kernfs_node serves as both an inode and a directory entry for
349 * kernfs. To prevent the kernfs inode numbers from being freed
350 * prematurely we take a reference to kernfs_node from the kernfs inode. A
351 * super_operations.evict_inode() implementation is needed to drop that
352 * reference upon inode destruction.
353 */
354void kernfs_evict_inode(struct inode *inode)
355{
356 struct kernfs_node *kn = inode->i_private;
357
358 truncate_inode_pages(&inode->i_data, 0);
359 clear_inode(inode);
360 kernfs_put(kn);
361}
362
363int kernfs_iop_permission(struct inode *inode, int mask)
364{
365 struct kernfs_node *kn;
366
367 if (mask & MAY_NOT_BLOCK)
368 return -ECHILD;
369
370 kn = inode->i_private;
371
372 mutex_lock(&kernfs_mutex);
373 kernfs_refresh_inode(kn, inode);
374 mutex_unlock(&kernfs_mutex);
375
376 return generic_permission(inode, mask);
377}
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
new file mode 100644
index 000000000000..eb536b76374a
--- /dev/null
+++ b/fs/kernfs/kernfs-internal.h
@@ -0,0 +1,122 @@
1/*
2 * fs/kernfs/kernfs-internal.h - kernfs internal header file
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#ifndef __KERNFS_INTERNAL_H
12#define __KERNFS_INTERNAL_H
13
14#include <linux/lockdep.h>
15#include <linux/fs.h>
16#include <linux/mutex.h>
17#include <linux/xattr.h>
18
19#include <linux/kernfs.h>
20
21struct kernfs_iattrs {
22 struct iattr ia_iattr;
23 void *ia_secdata;
24 u32 ia_secdata_len;
25
26 struct simple_xattrs xattrs;
27};
28
29#define KN_DEACTIVATED_BIAS INT_MIN
30
31/* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */
32
33/**
34 * kernfs_root - find out the kernfs_root a kernfs_node belongs to
35 * @kn: kernfs_node of interest
36 *
37 * Return the kernfs_root @kn belongs to.
38 */
39static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn)
40{
41 /* if parent exists, it's always a dir; otherwise, @sd is a dir */
42 if (kn->parent)
43 kn = kn->parent;
44 return kn->dir.root;
45}
46
47/*
48 * Context structure to be used while adding/removing nodes.
49 */
50struct kernfs_addrm_cxt {
51 struct kernfs_node *removed;
52};
53
54/*
55 * mount.c
56 */
57struct kernfs_super_info {
58 /*
59 * The root associated with this super_block. Each super_block is
60 * identified by the root and ns it's associated with.
61 */
62 struct kernfs_root *root;
63
64 /*
65 * Each sb is associated with one namespace tag, currently the
66 * network namespace of the task which mounted this kernfs
67 * instance. If multiple tags become necessary, make the following
68 * an array and compare kernfs_node tag against every entry.
69 */
70 const void *ns;
71};
72#define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
73
74extern struct kmem_cache *kernfs_node_cache;
75
76/*
77 * inode.c
78 */
79struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn);
80void kernfs_evict_inode(struct inode *inode);
81int kernfs_iop_permission(struct inode *inode, int mask);
82int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr);
83int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
84 struct kstat *stat);
85int kernfs_iop_setxattr(struct dentry *dentry, const char *name, const void *value,
86 size_t size, int flags);
87int kernfs_iop_removexattr(struct dentry *dentry, const char *name);
88ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
89 size_t size);
90ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size);
91void kernfs_inode_init(void);
92
93/*
94 * dir.c
95 */
96extern struct mutex kernfs_mutex;
97extern const struct dentry_operations kernfs_dops;
98extern const struct file_operations kernfs_dir_fops;
99extern const struct inode_operations kernfs_dir_iops;
100
101struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
102void kernfs_put_active(struct kernfs_node *kn);
103void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt);
104int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn);
105void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt);
106struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
107 const char *name, umode_t mode,
108 unsigned flags);
109
110/*
111 * file.c
112 */
113extern const struct file_operations kernfs_file_fops;
114
115void kernfs_unmap_bin_file(struct kernfs_node *kn);
116
117/*
118 * symlink.c
119 */
120extern const struct inode_operations kernfs_symlink_iops;
121
122#endif /* __KERNFS_INTERNAL_H */
diff --git a/fs/kernfs/mount.c b/fs/kernfs/mount.c
new file mode 100644
index 000000000000..0f4152defe7b
--- /dev/null
+++ b/fs/kernfs/mount.c
@@ -0,0 +1,171 @@
1/*
2 * fs/kernfs/mount.c - kernfs mount implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#include <linux/fs.h>
12#include <linux/mount.h>
13#include <linux/init.h>
14#include <linux/magic.h>
15#include <linux/slab.h>
16#include <linux/pagemap.h>
17
18#include "kernfs-internal.h"
19
20struct kmem_cache *kernfs_node_cache;
21
22static const struct super_operations kernfs_sops = {
23 .statfs = simple_statfs,
24 .drop_inode = generic_delete_inode,
25 .evict_inode = kernfs_evict_inode,
26};
27
28static int kernfs_fill_super(struct super_block *sb)
29{
30 struct kernfs_super_info *info = kernfs_info(sb);
31 struct inode *inode;
32 struct dentry *root;
33
34 sb->s_blocksize = PAGE_CACHE_SIZE;
35 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
36 sb->s_magic = SYSFS_MAGIC;
37 sb->s_op = &kernfs_sops;
38 sb->s_time_gran = 1;
39
40 /* get root inode, initialize and unlock it */
41 mutex_lock(&kernfs_mutex);
42 inode = kernfs_get_inode(sb, info->root->kn);
43 mutex_unlock(&kernfs_mutex);
44 if (!inode) {
45 pr_debug("kernfs: could not get root inode\n");
46 return -ENOMEM;
47 }
48
49 /* instantiate and link root dentry */
50 root = d_make_root(inode);
51 if (!root) {
52 pr_debug("%s: could not get root dentry!\n", __func__);
53 return -ENOMEM;
54 }
55 kernfs_get(info->root->kn);
56 root->d_fsdata = info->root->kn;
57 sb->s_root = root;
58 sb->s_d_op = &kernfs_dops;
59 return 0;
60}
61
62static int kernfs_test_super(struct super_block *sb, void *data)
63{
64 struct kernfs_super_info *sb_info = kernfs_info(sb);
65 struct kernfs_super_info *info = data;
66
67 return sb_info->root == info->root && sb_info->ns == info->ns;
68}
69
70static int kernfs_set_super(struct super_block *sb, void *data)
71{
72 int error;
73 error = set_anon_super(sb, data);
74 if (!error)
75 sb->s_fs_info = data;
76 return error;
77}
78
79/**
80 * kernfs_super_ns - determine the namespace tag of a kernfs super_block
81 * @sb: super_block of interest
82 *
83 * Return the namespace tag associated with kernfs super_block @sb.
84 */
85const void *kernfs_super_ns(struct super_block *sb)
86{
87 struct kernfs_super_info *info = kernfs_info(sb);
88
89 return info->ns;
90}
91
92/**
93 * kernfs_mount_ns - kernfs mount helper
94 * @fs_type: file_system_type of the fs being mounted
95 * @flags: mount flags specified for the mount
96 * @root: kernfs_root of the hierarchy being mounted
97 * @new_sb_created: tell the caller if we allocated a new superblock
98 * @ns: optional namespace tag of the mount
99 *
100 * This is to be called from each kernfs user's file_system_type->mount()
101 * implementation, which should pass through the specified @fs_type and
102 * @flags, and specify the hierarchy and namespace tag to mount via @root
103 * and @ns, respectively.
104 *
105 * The return value can be passed to the vfs layer verbatim.
106 */
107struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
108 struct kernfs_root *root, bool *new_sb_created,
109 const void *ns)
110{
111 struct super_block *sb;
112 struct kernfs_super_info *info;
113 int error;
114
115 info = kzalloc(sizeof(*info), GFP_KERNEL);
116 if (!info)
117 return ERR_PTR(-ENOMEM);
118
119 info->root = root;
120 info->ns = ns;
121
122 sb = sget(fs_type, kernfs_test_super, kernfs_set_super, flags, info);
123 if (IS_ERR(sb) || sb->s_fs_info != info)
124 kfree(info);
125 if (IS_ERR(sb))
126 return ERR_CAST(sb);
127
128 if (new_sb_created)
129 *new_sb_created = !sb->s_root;
130
131 if (!sb->s_root) {
132 error = kernfs_fill_super(sb);
133 if (error) {
134 deactivate_locked_super(sb);
135 return ERR_PTR(error);
136 }
137 sb->s_flags |= MS_ACTIVE;
138 }
139
140 return dget(sb->s_root);
141}
142
143/**
144 * kernfs_kill_sb - kill_sb for kernfs
145 * @sb: super_block being killed
146 *
147 * This can be used directly for file_system_type->kill_sb(). If a kernfs
148 * user needs extra cleanup, it can implement its own kill_sb() and call
149 * this function at the end.
150 */
151void kernfs_kill_sb(struct super_block *sb)
152{
153 struct kernfs_super_info *info = kernfs_info(sb);
154 struct kernfs_node *root_kn = sb->s_root->d_fsdata;
155
156 /*
157 * Remove the superblock from fs_supers/s_instances
158 * so we can't find it, before freeing kernfs_super_info.
159 */
160 kill_anon_super(sb);
161 kfree(info);
162 kernfs_put(root_kn);
163}
164
165void __init kernfs_init(void)
166{
167 kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
168 sizeof(struct kernfs_node),
169 0, SLAB_PANIC, NULL);
170 kernfs_inode_init();
171}
diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c
new file mode 100644
index 000000000000..4d457055acb9
--- /dev/null
+++ b/fs/kernfs/symlink.c
@@ -0,0 +1,151 @@
1/*
2 * fs/kernfs/symlink.c - kernfs symlink implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
10
11#include <linux/fs.h>
12#include <linux/gfp.h>
13#include <linux/namei.h>
14
15#include "kernfs-internal.h"
16
17/**
18 * kernfs_create_link - create a symlink
19 * @parent: directory to create the symlink in
20 * @name: name of the symlink
21 * @target: target node for the symlink to point to
22 *
23 * Returns the created node on success, ERR_PTR() value on error.
24 */
25struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
26 const char *name,
27 struct kernfs_node *target)
28{
29 struct kernfs_node *kn;
30 struct kernfs_addrm_cxt acxt;
31 int error;
32
33 kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, KERNFS_LINK);
34 if (!kn)
35 return ERR_PTR(-ENOMEM);
36
37 if (kernfs_ns_enabled(parent))
38 kn->ns = target->ns;
39 kn->symlink.target_kn = target;
40 kernfs_get(target); /* ref owned by symlink */
41
42 kernfs_addrm_start(&acxt);
43 error = kernfs_add_one(&acxt, kn);
44 kernfs_addrm_finish(&acxt);
45
46 if (!error)
47 return kn;
48
49 kernfs_put(kn);
50 return ERR_PTR(error);
51}
52
53static int kernfs_get_target_path(struct kernfs_node *parent,
54 struct kernfs_node *target, char *path)
55{
56 struct kernfs_node *base, *kn;
57 char *s = path;
58 int len = 0;
59
60 /* go up to the root, stop at the base */
61 base = parent;
62 while (base->parent) {
63 kn = target->parent;
64 while (kn->parent && base != kn)
65 kn = kn->parent;
66
67 if (base == kn)
68 break;
69
70 strcpy(s, "../");
71 s += 3;
72 base = base->parent;
73 }
74
75 /* determine end of target string for reverse fillup */
76 kn = target;
77 while (kn->parent && kn != base) {
78 len += strlen(kn->name) + 1;
79 kn = kn->parent;
80 }
81
82 /* check limits */
83 if (len < 2)
84 return -EINVAL;
85 len--;
86 if ((s - path) + len > PATH_MAX)
87 return -ENAMETOOLONG;
88
89 /* reverse fillup of target string from target to base */
90 kn = target;
91 while (kn->parent && kn != base) {
92 int slen = strlen(kn->name);
93
94 len -= slen;
95 strncpy(s + len, kn->name, slen);
96 if (len)
97 s[--len] = '/';
98
99 kn = kn->parent;
100 }
101
102 return 0;
103}
104
105static int kernfs_getlink(struct dentry *dentry, char *path)
106{
107 struct kernfs_node *kn = dentry->d_fsdata;
108 struct kernfs_node *parent = kn->parent;
109 struct kernfs_node *target = kn->symlink.target_kn;
110 int error;
111
112 mutex_lock(&kernfs_mutex);
113 error = kernfs_get_target_path(parent, target, path);
114 mutex_unlock(&kernfs_mutex);
115
116 return error;
117}
118
119static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd)
120{
121 int error = -ENOMEM;
122 unsigned long page = get_zeroed_page(GFP_KERNEL);
123 if (page) {
124 error = kernfs_getlink(dentry, (char *) page);
125 if (error < 0)
126 free_page((unsigned long)page);
127 }
128 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
129 return NULL;
130}
131
132static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd,
133 void *cookie)
134{
135 char *page = nd_get_link(nd);
136 if (!IS_ERR(page))
137 free_page((unsigned long)page);
138}
139
140const struct inode_operations kernfs_symlink_iops = {
141 .setxattr = kernfs_iop_setxattr,
142 .removexattr = kernfs_iop_removexattr,
143 .getxattr = kernfs_iop_getxattr,
144 .listxattr = kernfs_iop_listxattr,
145 .readlink = generic_readlink,
146 .follow_link = kernfs_iop_follow_link,
147 .put_link = kernfs_iop_put_link,
148 .setattr = kernfs_iop_setattr,
149 .getattr = kernfs_iop_getattr,
150 .permission = kernfs_iop_permission,
151};