Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 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 | */ |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 10 | |
Tejun Heo | abd54f0 | 2014-02-03 14:02:55 -0500 | [diff] [blame] | 11 | #include <linux/sched.h> |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 12 | #include <linux/fs.h> |
| 13 | #include <linux/namei.h> |
| 14 | #include <linux/idr.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/security.h> |
| 17 | #include <linux/hash.h> |
| 18 | |
| 19 | #include "kernfs-internal.h" |
| 20 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 21 | DEFINE_MUTEX(kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 22 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 23 | #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 24 | |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 25 | static bool kernfs_active(struct kernfs_node *kn) |
| 26 | { |
| 27 | lockdep_assert_held(&kernfs_mutex); |
| 28 | return atomic_read(&kn->active) >= 0; |
| 29 | } |
| 30 | |
Tejun Heo | 182fd64 | 2014-02-03 14:02:59 -0500 | [diff] [blame] | 31 | static bool kernfs_lockdep(struct kernfs_node *kn) |
| 32 | { |
| 33 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 34 | return kn->flags & KERNFS_LOCKDEP; |
| 35 | #else |
| 36 | return false; |
| 37 | #endif |
| 38 | } |
| 39 | |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 40 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 41 | * kernfs_name_hash |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 42 | * @name: Null terminated string to hash |
| 43 | * @ns: Namespace tag to hash |
| 44 | * |
| 45 | * Returns 31 bit hash of ns + name (so it fits in an off_t ) |
| 46 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 47 | static unsigned int kernfs_name_hash(const char *name, const void *ns) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 48 | { |
| 49 | unsigned long hash = init_name_hash(); |
| 50 | unsigned int len = strlen(name); |
| 51 | while (len--) |
| 52 | hash = partial_name_hash(*name++, hash); |
| 53 | hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); |
| 54 | hash &= 0x7fffffffU; |
| 55 | /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ |
| 56 | if (hash < 1) |
| 57 | hash += 2; |
| 58 | if (hash >= INT_MAX) |
| 59 | hash = INT_MAX - 1; |
| 60 | return hash; |
| 61 | } |
| 62 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 63 | static int kernfs_name_compare(unsigned int hash, const char *name, |
| 64 | const void *ns, const struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 65 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 66 | if (hash != kn->hash) |
| 67 | return hash - kn->hash; |
| 68 | if (ns != kn->ns) |
| 69 | return ns - kn->ns; |
| 70 | return strcmp(name, kn->name); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 73 | static int kernfs_sd_compare(const struct kernfs_node *left, |
| 74 | const struct kernfs_node *right) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 75 | { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 76 | return kernfs_name_compare(left->hash, left->name, left->ns, right); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 80 | * kernfs_link_sibling - link kernfs_node into sibling rbtree |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 81 | * @kn: kernfs_node of interest |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 82 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 83 | * Link @kn into its sibling rbtree which starts from |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 84 | * @kn->parent->dir.children. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 85 | * |
| 86 | * Locking: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 87 | * mutex_lock(kernfs_mutex) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 88 | * |
| 89 | * RETURNS: |
| 90 | * 0 on susccess -EEXIST on failure. |
| 91 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 92 | static int kernfs_link_sibling(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 93 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 94 | struct rb_node **node = &kn->parent->dir.children.rb_node; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 95 | struct rb_node *parent = NULL; |
| 96 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 97 | if (kernfs_type(kn) == KERNFS_DIR) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 98 | kn->parent->dir.subdirs++; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 99 | |
| 100 | while (*node) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 101 | struct kernfs_node *pos; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 102 | int result; |
| 103 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 104 | pos = rb_to_kn(*node); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 105 | parent = *node; |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 106 | result = kernfs_sd_compare(kn, pos); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 107 | if (result < 0) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 108 | node = &pos->rb.rb_left; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 109 | else if (result > 0) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 110 | node = &pos->rb.rb_right; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 111 | else |
| 112 | return -EEXIST; |
| 113 | } |
| 114 | /* add new node and rebalance the tree */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 115 | rb_link_node(&kn->rb, parent, node); |
| 116 | rb_insert_color(&kn->rb, &kn->parent->dir.children); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 121 | * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 122 | * @kn: kernfs_node of interest |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 123 | * |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 124 | * Try to unlink @kn from its sibling rbtree which starts from |
| 125 | * kn->parent->dir.children. Returns %true if @kn was actually |
| 126 | * removed, %false if @kn wasn't on the rbtree. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 127 | * |
| 128 | * Locking: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 129 | * mutex_lock(kernfs_mutex) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 130 | */ |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 131 | static bool kernfs_unlink_sibling(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 132 | { |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 133 | if (RB_EMPTY_NODE(&kn->rb)) |
| 134 | return false; |
| 135 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 136 | if (kernfs_type(kn) == KERNFS_DIR) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 137 | kn->parent->dir.subdirs--; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 138 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 139 | rb_erase(&kn->rb, &kn->parent->dir.children); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 140 | RB_CLEAR_NODE(&kn->rb); |
| 141 | return true; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 145 | * kernfs_get_active - get an active reference to kernfs_node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 146 | * @kn: kernfs_node to get an active reference to |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 147 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 148 | * Get an active reference of @kn. This function is noop if @kn |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 149 | * is NULL. |
| 150 | * |
| 151 | * RETURNS: |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 152 | * Pointer to @kn on success, NULL on failure. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 153 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 154 | struct kernfs_node *kernfs_get_active(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 155 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 156 | if (unlikely(!kn)) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 157 | return NULL; |
| 158 | |
Greg Kroah-Hartman | f4b3e63 | 2014-01-13 14:13:39 -0800 | [diff] [blame] | 159 | if (!atomic_inc_unless_negative(&kn->active)) |
| 160 | return NULL; |
| 161 | |
Tejun Heo | 182fd64 | 2014-02-03 14:02:59 -0500 | [diff] [blame] | 162 | if (kernfs_lockdep(kn)) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 163 | rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); |
Greg Kroah-Hartman | f4b3e63 | 2014-01-13 14:13:39 -0800 | [diff] [blame] | 164 | return kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 168 | * kernfs_put_active - put an active reference to kernfs_node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 169 | * @kn: kernfs_node to put an active reference to |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 170 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 171 | * Put an active reference to @kn. This function is noop if @kn |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 172 | * is NULL. |
| 173 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 174 | void kernfs_put_active(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 175 | { |
Tejun Heo | abd54f0 | 2014-02-03 14:02:55 -0500 | [diff] [blame] | 176 | struct kernfs_root *root = kernfs_root(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 177 | int v; |
| 178 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 179 | if (unlikely(!kn)) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 180 | return; |
| 181 | |
Tejun Heo | 182fd64 | 2014-02-03 14:02:59 -0500 | [diff] [blame] | 182 | if (kernfs_lockdep(kn)) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 183 | rwsem_release(&kn->dep_map, 1, _RET_IP_); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 184 | v = atomic_dec_return(&kn->active); |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 185 | if (likely(v != KN_DEACTIVATED_BIAS)) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 186 | return; |
| 187 | |
Tejun Heo | abd54f0 | 2014-02-03 14:02:55 -0500 | [diff] [blame] | 188 | wake_up_all(&root->deactivate_waitq); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 192 | * kernfs_drain - drain kernfs_node |
| 193 | * @kn: kernfs_node to drain |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 194 | * |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 195 | * Drain existing usages and nuke all existing mmaps of @kn. Mutiple |
| 196 | * removers may invoke this function concurrently on @kn and all will |
| 197 | * return after draining is complete. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 198 | */ |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 199 | static void kernfs_drain(struct kernfs_node *kn) |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 200 | __releases(&kernfs_mutex) __acquires(&kernfs_mutex) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 201 | { |
Tejun Heo | abd54f0 | 2014-02-03 14:02:55 -0500 | [diff] [blame] | 202 | struct kernfs_root *root = kernfs_root(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 203 | |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 204 | lockdep_assert_held(&kernfs_mutex); |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 205 | WARN_ON_ONCE(kernfs_active(kn)); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 206 | |
| 207 | mutex_unlock(&kernfs_mutex); |
| 208 | |
Tejun Heo | 182fd64 | 2014-02-03 14:02:59 -0500 | [diff] [blame] | 209 | if (kernfs_lockdep(kn)) { |
Tejun Heo | a660793 | 2014-02-03 14:02:54 -0500 | [diff] [blame] | 210 | rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 211 | if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS) |
| 212 | lock_contended(&kn->dep_map, _RET_IP_); |
| 213 | } |
Greg Kroah-Hartman | 0890147 | 2014-01-13 14:39:52 -0800 | [diff] [blame] | 214 | |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 215 | /* but everyone should wait for draining */ |
Tejun Heo | abd54f0 | 2014-02-03 14:02:55 -0500 | [diff] [blame] | 216 | wait_event(root->deactivate_waitq, |
| 217 | atomic_read(&kn->active) == KN_DEACTIVATED_BIAS); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 218 | |
Tejun Heo | 182fd64 | 2014-02-03 14:02:59 -0500 | [diff] [blame] | 219 | if (kernfs_lockdep(kn)) { |
Tejun Heo | a660793 | 2014-02-03 14:02:54 -0500 | [diff] [blame] | 220 | lock_acquired(&kn->dep_map, _RET_IP_); |
| 221 | rwsem_release(&kn->dep_map, 1, _RET_IP_); |
| 222 | } |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 223 | |
Tejun Heo | ccf02aa | 2014-02-03 14:02:57 -0500 | [diff] [blame] | 224 | kernfs_unmap_bin_file(kn); |
| 225 | |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 226 | mutex_lock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 229 | /** |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 230 | * kernfs_get - get a reference count on a kernfs_node |
| 231 | * @kn: the target kernfs_node |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 232 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 233 | void kernfs_get(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 234 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 235 | if (kn) { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 236 | WARN_ON(!atomic_read(&kn->count)); |
| 237 | atomic_inc(&kn->count); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | EXPORT_SYMBOL_GPL(kernfs_get); |
| 241 | |
| 242 | /** |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 243 | * kernfs_put - put a reference count on a kernfs_node |
| 244 | * @kn: the target kernfs_node |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 245 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 246 | * Put a reference count of @kn and destroy it if it reached zero. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 247 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 248 | void kernfs_put(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 249 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 250 | struct kernfs_node *parent; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 251 | struct kernfs_root *root; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 252 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 253 | if (!kn || !atomic_dec_and_test(&kn->count)) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 254 | return; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 255 | root = kernfs_root(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 256 | repeat: |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 257 | /* |
| 258 | * Moving/renaming is always done while holding reference. |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 259 | * kn->parent won't change beneath us. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 260 | */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 261 | parent = kn->parent; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 262 | |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 263 | WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS, |
| 264 | "kernfs_put: %s/%s: released with incorrect active_ref %d\n", |
| 265 | parent ? parent->name : "", kn->name, atomic_read(&kn->active)); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 266 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 267 | if (kernfs_type(kn) == KERNFS_LINK) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 268 | kernfs_put(kn->symlink.target_kn); |
Tejun Heo | 2063d60 | 2013-12-11 16:02:57 -0500 | [diff] [blame] | 269 | if (!(kn->flags & KERNFS_STATIC_NAME)) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 270 | kfree(kn->name); |
| 271 | if (kn->iattr) { |
| 272 | if (kn->iattr->ia_secdata) |
| 273 | security_release_secctx(kn->iattr->ia_secdata, |
| 274 | kn->iattr->ia_secdata_len); |
| 275 | simple_xattrs_free(&kn->iattr->xattrs); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 276 | } |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 277 | kfree(kn->iattr); |
| 278 | ida_simple_remove(&root->ino_ida, kn->ino); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 279 | kmem_cache_free(kernfs_node_cache, kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 280 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 281 | kn = parent; |
| 282 | if (kn) { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 283 | if (atomic_dec_and_test(&kn->count)) |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 284 | goto repeat; |
| 285 | } else { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 286 | /* just released the root kn, free @root too */ |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 287 | ida_destroy(&root->ino_ida); |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 288 | kfree(root); |
| 289 | } |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 290 | } |
| 291 | EXPORT_SYMBOL_GPL(kernfs_put); |
| 292 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 293 | static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 294 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 295 | struct kernfs_node *kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 296 | |
| 297 | if (flags & LOOKUP_RCU) |
| 298 | return -ECHILD; |
| 299 | |
Tejun Heo | 19bbb92 | 2013-12-11 16:02:59 -0500 | [diff] [blame] | 300 | /* Always perform fresh lookup for negatives */ |
| 301 | if (!dentry->d_inode) |
| 302 | goto out_bad_unlocked; |
| 303 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 304 | kn = dentry->d_fsdata; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 305 | mutex_lock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 306 | |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 307 | /* The kernfs node has been deactivated */ |
| 308 | if (!kernfs_active(kn)) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 309 | goto out_bad; |
| 310 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 311 | /* The kernfs node has been moved? */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 312 | if (dentry->d_parent->d_fsdata != kn->parent) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 313 | goto out_bad; |
| 314 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 315 | /* The kernfs node has been renamed */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 316 | if (strcmp(dentry->d_name.name, kn->name) != 0) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 317 | goto out_bad; |
| 318 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 319 | /* The kernfs node has been moved to a different namespace */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 320 | if (kn->parent && kernfs_ns_enabled(kn->parent) && |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 321 | kernfs_info(dentry->d_sb)->ns != kn->ns) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 322 | goto out_bad; |
| 323 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 324 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 325 | out_valid: |
| 326 | return 1; |
| 327 | out_bad: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 328 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | 19bbb92 | 2013-12-11 16:02:59 -0500 | [diff] [blame] | 329 | out_bad_unlocked: |
| 330 | /* |
| 331 | * @dentry doesn't match the underlying kernfs node, drop the |
| 332 | * dentry and force lookup. If we have submounts we must allow the |
| 333 | * vfs caches to lie about the state of the filesystem to prevent |
| 334 | * leaks and other nasty things, so use check_submounts_and_drop() |
| 335 | * instead of d_drop(). |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 336 | */ |
| 337 | if (check_submounts_and_drop(dentry) != 0) |
| 338 | goto out_valid; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 343 | static void kernfs_dop_release(struct dentry *dentry) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 344 | { |
| 345 | kernfs_put(dentry->d_fsdata); |
| 346 | } |
| 347 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 348 | const struct dentry_operations kernfs_dops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 349 | .d_revalidate = kernfs_dop_revalidate, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 350 | .d_release = kernfs_dop_release, |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 351 | }; |
| 352 | |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 353 | static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, |
| 354 | const char *name, umode_t mode, |
| 355 | unsigned flags) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 356 | { |
| 357 | char *dup_name = NULL; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 358 | struct kernfs_node *kn; |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 359 | int ret; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 360 | |
Tejun Heo | 2063d60 | 2013-12-11 16:02:57 -0500 | [diff] [blame] | 361 | if (!(flags & KERNFS_STATIC_NAME)) { |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 362 | name = dup_name = kstrdup(name, GFP_KERNEL); |
| 363 | if (!name) |
| 364 | return NULL; |
| 365 | } |
| 366 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 367 | kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 368 | if (!kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 369 | goto err_out1; |
| 370 | |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 371 | ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL); |
| 372 | if (ret < 0) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 373 | goto err_out2; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 374 | kn->ino = ret; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 375 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 376 | atomic_set(&kn->count, 1); |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 377 | atomic_set(&kn->active, KN_DEACTIVATED_BIAS); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 378 | RB_CLEAR_NODE(&kn->rb); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 379 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 380 | kn->name = name; |
| 381 | kn->mode = mode; |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 382 | kn->flags = flags; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 383 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 384 | return kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 385 | |
| 386 | err_out2: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 387 | kmem_cache_free(kernfs_node_cache, kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 388 | err_out1: |
| 389 | kfree(dup_name); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 393 | struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, |
| 394 | const char *name, umode_t mode, |
| 395 | unsigned flags) |
| 396 | { |
| 397 | struct kernfs_node *kn; |
| 398 | |
| 399 | kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags); |
| 400 | if (kn) { |
| 401 | kernfs_get(parent); |
| 402 | kn->parent = parent; |
| 403 | } |
| 404 | return kn; |
| 405 | } |
| 406 | |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 407 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 408 | * kernfs_add_one - add kernfs_node to parent without warning |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 409 | * @kn: kernfs_node to be added |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 410 | * |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 411 | * The caller must already have initialized @kn->parent. This |
| 412 | * function increments nlink of the parent's inode if @kn is a |
| 413 | * directory and link into the children list of the parent. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 414 | * |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 415 | * RETURNS: |
| 416 | * 0 on success, -EEXIST if entry with the given name already |
| 417 | * exists. |
| 418 | */ |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 419 | int kernfs_add_one(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 420 | { |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 421 | struct kernfs_node *parent = kn->parent; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 422 | struct kernfs_iattrs *ps_iattr; |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 423 | bool has_ns; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 424 | int ret; |
| 425 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 426 | mutex_lock(&kernfs_mutex); |
| 427 | |
| 428 | ret = -EINVAL; |
| 429 | has_ns = kernfs_ns_enabled(parent); |
| 430 | if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", |
| 431 | has_ns ? "required" : "invalid", parent->name, kn->name)) |
| 432 | goto out_unlock; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 433 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 434 | if (kernfs_type(parent) != KERNFS_DIR) |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 435 | goto out_unlock; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 436 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 437 | ret = -ENOENT; |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 438 | if (!kernfs_active(parent)) |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 439 | goto out_unlock; |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 440 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 441 | kn->hash = kernfs_name_hash(kn->name, kn->ns); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 442 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 443 | ret = kernfs_link_sibling(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 444 | if (ret) |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 445 | goto out_unlock; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 446 | |
| 447 | /* Update timestamps on the parent */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 448 | ps_iattr = parent->iattr; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 449 | if (ps_iattr) { |
| 450 | struct iattr *ps_iattrs = &ps_iattr->ia_iattr; |
| 451 | ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; |
| 452 | } |
| 453 | |
| 454 | /* Mark the entry added into directory tree */ |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 455 | atomic_sub(KN_DEACTIVATED_BIAS, &kn->active); |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 456 | ret = 0; |
| 457 | out_unlock: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 458 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 459 | return ret; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 463 | * kernfs_find_ns - find kernfs_node with the given name |
| 464 | * @parent: kernfs_node to search under |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 465 | * @name: name to look for |
| 466 | * @ns: the namespace tag to use |
| 467 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 468 | * Look for kernfs_node with name @name under @parent. Returns pointer to |
| 469 | * the found kernfs_node on success, %NULL on failure. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 470 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 471 | static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent, |
| 472 | const unsigned char *name, |
| 473 | const void *ns) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 474 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 475 | struct rb_node *node = parent->dir.children.rb_node; |
Tejun Heo | ac9bba0 | 2013-11-29 17:19:09 -0500 | [diff] [blame] | 476 | bool has_ns = kernfs_ns_enabled(parent); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 477 | unsigned int hash; |
| 478 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 479 | lockdep_assert_held(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 480 | |
| 481 | if (has_ns != (bool)ns) { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 482 | WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 483 | has_ns ? "required" : "invalid", parent->name, name); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 487 | hash = kernfs_name_hash(name, ns); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 488 | while (node) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 489 | struct kernfs_node *kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 490 | int result; |
| 491 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 492 | kn = rb_to_kn(node); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 493 | result = kernfs_name_compare(hash, name, ns, kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 494 | if (result < 0) |
| 495 | node = node->rb_left; |
| 496 | else if (result > 0) |
| 497 | node = node->rb_right; |
| 498 | else |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 499 | return kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 500 | } |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | /** |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 505 | * kernfs_find_and_get_ns - find and get kernfs_node with the given name |
| 506 | * @parent: kernfs_node to search under |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 507 | * @name: name to look for |
| 508 | * @ns: the namespace tag to use |
| 509 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 510 | * Look for kernfs_node with name @name under @parent and get a reference |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 511 | * if found. This function may sleep and returns pointer to the found |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 512 | * kernfs_node on success, %NULL on failure. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 513 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 514 | struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, |
| 515 | const char *name, const void *ns) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 516 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 517 | struct kernfs_node *kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 518 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 519 | mutex_lock(&kernfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 520 | kn = kernfs_find_ns(parent, name, ns); |
| 521 | kernfs_get(kn); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 522 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 523 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 524 | return kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 525 | } |
| 526 | EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns); |
| 527 | |
| 528 | /** |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 529 | * kernfs_create_root - create a new kernfs hierarchy |
Tejun Heo | 80b9bbe | 2013-12-11 16:03:00 -0500 | [diff] [blame] | 530 | * @kdops: optional directory syscall operations for the hierarchy |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 531 | * @priv: opaque data associated with the new directory |
| 532 | * |
| 533 | * Returns the root of the new hierarchy on success, ERR_PTR() value on |
| 534 | * failure. |
| 535 | */ |
Tejun Heo | 80b9bbe | 2013-12-11 16:03:00 -0500 | [diff] [blame] | 536 | struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv) |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 537 | { |
| 538 | struct kernfs_root *root; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 539 | struct kernfs_node *kn; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 540 | |
| 541 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 542 | if (!root) |
| 543 | return ERR_PTR(-ENOMEM); |
| 544 | |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 545 | ida_init(&root->ino_ida); |
| 546 | |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 547 | kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, |
| 548 | KERNFS_DIR); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 549 | if (!kn) { |
Tejun Heo | bc75555 | 2013-11-28 14:54:41 -0500 | [diff] [blame] | 550 | ida_destroy(&root->ino_ida); |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 551 | kfree(root); |
| 552 | return ERR_PTR(-ENOMEM); |
| 553 | } |
| 554 | |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 555 | atomic_sub(KN_DEACTIVATED_BIAS, &kn->active); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 556 | kn->priv = priv; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 557 | kn->dir.root = root; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 558 | |
Tejun Heo | 80b9bbe | 2013-12-11 16:03:00 -0500 | [diff] [blame] | 559 | root->dir_ops = kdops; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 560 | root->kn = kn; |
Tejun Heo | abd54f0 | 2014-02-03 14:02:55 -0500 | [diff] [blame] | 561 | init_waitqueue_head(&root->deactivate_waitq); |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 562 | |
| 563 | return root; |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * kernfs_destroy_root - destroy a kernfs hierarchy |
| 568 | * @root: root of the hierarchy to destroy |
| 569 | * |
| 570 | * Destroy the hierarchy anchored at @root by removing all existing |
| 571 | * directories and destroying @root. |
| 572 | */ |
| 573 | void kernfs_destroy_root(struct kernfs_root *root) |
| 574 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 575 | kernfs_remove(root->kn); /* will also free @root */ |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 579 | * kernfs_create_dir_ns - create a directory |
| 580 | * @parent: parent in which to create a new directory |
| 581 | * @name: name of the new directory |
Tejun Heo | bb8b9d0 | 2013-12-11 16:02:55 -0500 | [diff] [blame] | 582 | * @mode: mode of the new directory |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 583 | * @priv: opaque data associated with the new directory |
| 584 | * @ns: optional namespace tag of the directory |
| 585 | * |
| 586 | * Returns the created node on success, ERR_PTR() value on failure. |
| 587 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 588 | struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, |
Tejun Heo | bb8b9d0 | 2013-12-11 16:02:55 -0500 | [diff] [blame] | 589 | const char *name, umode_t mode, |
| 590 | void *priv, const void *ns) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 591 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 592 | struct kernfs_node *kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 593 | int rc; |
| 594 | |
| 595 | /* allocate */ |
Tejun Heo | db4aad2 | 2014-01-17 09:58:25 -0500 | [diff] [blame] | 596 | kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 597 | if (!kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 598 | return ERR_PTR(-ENOMEM); |
| 599 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 600 | kn->dir.root = parent->dir.root; |
| 601 | kn->ns = ns; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 602 | kn->priv = priv; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 603 | |
| 604 | /* link in */ |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 605 | rc = kernfs_add_one(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 606 | if (!rc) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 607 | return kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 608 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 609 | kernfs_put(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 610 | return ERR_PTR(rc); |
| 611 | } |
| 612 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 613 | static struct dentry *kernfs_iop_lookup(struct inode *dir, |
| 614 | struct dentry *dentry, |
| 615 | unsigned int flags) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 616 | { |
Tejun Heo | 19bbb92 | 2013-12-11 16:02:59 -0500 | [diff] [blame] | 617 | struct dentry *ret; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 618 | struct kernfs_node *parent = dentry->d_parent->d_fsdata; |
| 619 | struct kernfs_node *kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 620 | struct inode *inode; |
| 621 | const void *ns = NULL; |
| 622 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 623 | mutex_lock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 624 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 625 | if (kernfs_ns_enabled(parent)) |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 626 | ns = kernfs_info(dir->i_sb)->ns; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 627 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 628 | kn = kernfs_find_ns(parent, dentry->d_name.name, ns); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 629 | |
| 630 | /* no such entry */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 631 | if (!kn) { |
Tejun Heo | 19bbb92 | 2013-12-11 16:02:59 -0500 | [diff] [blame] | 632 | ret = NULL; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 633 | goto out_unlock; |
| 634 | } |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 635 | kernfs_get(kn); |
| 636 | dentry->d_fsdata = kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 637 | |
| 638 | /* attach dentry and inode */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 639 | inode = kernfs_get_inode(dir->i_sb, kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 640 | if (!inode) { |
| 641 | ret = ERR_PTR(-ENOMEM); |
| 642 | goto out_unlock; |
| 643 | } |
| 644 | |
| 645 | /* instantiate and hash dentry */ |
| 646 | ret = d_materialise_unique(dentry, inode); |
| 647 | out_unlock: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 648 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 649 | return ret; |
| 650 | } |
| 651 | |
Tejun Heo | 80b9bbe | 2013-12-11 16:03:00 -0500 | [diff] [blame] | 652 | static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry, |
| 653 | umode_t mode) |
| 654 | { |
| 655 | struct kernfs_node *parent = dir->i_private; |
| 656 | struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops; |
| 657 | |
| 658 | if (!kdops || !kdops->mkdir) |
| 659 | return -EPERM; |
| 660 | |
| 661 | return kdops->mkdir(parent, dentry->d_name.name, mode); |
| 662 | } |
| 663 | |
| 664 | static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry) |
| 665 | { |
| 666 | struct kernfs_node *kn = dentry->d_fsdata; |
| 667 | struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops; |
| 668 | |
| 669 | if (!kdops || !kdops->rmdir) |
| 670 | return -EPERM; |
| 671 | |
| 672 | return kdops->rmdir(kn); |
| 673 | } |
| 674 | |
| 675 | static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 676 | struct inode *new_dir, struct dentry *new_dentry) |
| 677 | { |
| 678 | struct kernfs_node *kn = old_dentry->d_fsdata; |
| 679 | struct kernfs_node *new_parent = new_dir->i_private; |
| 680 | struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops; |
| 681 | |
| 682 | if (!kdops || !kdops->rename) |
| 683 | return -EPERM; |
| 684 | |
| 685 | return kdops->rename(kn, new_parent, new_dentry->d_name.name); |
| 686 | } |
| 687 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 688 | const struct inode_operations kernfs_dir_iops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 689 | .lookup = kernfs_iop_lookup, |
| 690 | .permission = kernfs_iop_permission, |
| 691 | .setattr = kernfs_iop_setattr, |
| 692 | .getattr = kernfs_iop_getattr, |
| 693 | .setxattr = kernfs_iop_setxattr, |
| 694 | .removexattr = kernfs_iop_removexattr, |
| 695 | .getxattr = kernfs_iop_getxattr, |
| 696 | .listxattr = kernfs_iop_listxattr, |
Tejun Heo | 80b9bbe | 2013-12-11 16:03:00 -0500 | [diff] [blame] | 697 | |
| 698 | .mkdir = kernfs_iop_mkdir, |
| 699 | .rmdir = kernfs_iop_rmdir, |
| 700 | .rename = kernfs_iop_rename, |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 701 | }; |
| 702 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 703 | static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 704 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 705 | struct kernfs_node *last; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 706 | |
| 707 | while (true) { |
| 708 | struct rb_node *rbn; |
| 709 | |
| 710 | last = pos; |
| 711 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 712 | if (kernfs_type(pos) != KERNFS_DIR) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 713 | break; |
| 714 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 715 | rbn = rb_first(&pos->dir.children); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 716 | if (!rbn) |
| 717 | break; |
| 718 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 719 | pos = rb_to_kn(rbn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | return last; |
| 723 | } |
| 724 | |
| 725 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 726 | * kernfs_next_descendant_post - find the next descendant for post-order walk |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 727 | * @pos: the current position (%NULL to initiate traversal) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 728 | * @root: kernfs_node whose descendants to walk |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 729 | * |
| 730 | * Find the next descendant to visit for post-order traversal of @root's |
| 731 | * descendants. @root is included in the iteration and the last node to be |
| 732 | * visited. |
| 733 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 734 | static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, |
| 735 | struct kernfs_node *root) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 736 | { |
| 737 | struct rb_node *rbn; |
| 738 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 739 | lockdep_assert_held(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 740 | |
| 741 | /* if first iteration, visit leftmost descendant which may be root */ |
| 742 | if (!pos) |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 743 | return kernfs_leftmost_descendant(root); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 744 | |
| 745 | /* if we visited @root, we're done */ |
| 746 | if (pos == root) |
| 747 | return NULL; |
| 748 | |
| 749 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 750 | rbn = rb_next(&pos->rb); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 751 | if (rbn) |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 752 | return kernfs_leftmost_descendant(rb_to_kn(rbn)); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 753 | |
| 754 | /* no sibling left, visit parent */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 755 | return pos->parent; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 756 | } |
| 757 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 758 | static void __kernfs_remove(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 759 | { |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 760 | struct kernfs_node *pos; |
| 761 | |
| 762 | lockdep_assert_held(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 763 | |
Tejun Heo | 6b0afc2 | 2014-02-03 14:03:01 -0500 | [diff] [blame^] | 764 | /* |
| 765 | * Short-circuit if non-root @kn has already finished removal. |
| 766 | * This is for kernfs_remove_self() which plays with active ref |
| 767 | * after removal. |
| 768 | */ |
| 769 | if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb))) |
Greg Kroah-Hartman | ce9b499 | 2014-01-13 13:50:31 -0800 | [diff] [blame] | 770 | return; |
| 771 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 772 | pr_debug("kernfs %s: removing\n", kn->name); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 773 | |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 774 | /* prevent any new usage under @kn by deactivating all nodes */ |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 775 | pos = NULL; |
| 776 | while ((pos = kernfs_next_descendant_post(pos, kn))) |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 777 | if (kernfs_active(pos)) |
| 778 | atomic_add(KN_DEACTIVATED_BIAS, &pos->active); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 779 | |
| 780 | /* deactivate and unlink the subtree node-by-node */ |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 781 | do { |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 782 | pos = kernfs_leftmost_descendant(kn); |
| 783 | |
| 784 | /* |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 785 | * kernfs_drain() drops kernfs_mutex temporarily and @pos's |
| 786 | * base ref could have been put by someone else by the time |
| 787 | * the function returns. Make sure it doesn't go away |
| 788 | * underneath us. |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 789 | */ |
| 790 | kernfs_get(pos); |
| 791 | |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 792 | kernfs_drain(pos); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 793 | |
| 794 | /* |
| 795 | * kernfs_unlink_sibling() succeeds once per node. Use it |
| 796 | * to decide who's responsible for cleanups. |
| 797 | */ |
| 798 | if (!pos->parent || kernfs_unlink_sibling(pos)) { |
| 799 | struct kernfs_iattrs *ps_iattr = |
| 800 | pos->parent ? pos->parent->iattr : NULL; |
| 801 | |
| 802 | /* update timestamps on the parent */ |
| 803 | if (ps_iattr) { |
| 804 | ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME; |
| 805 | ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME; |
| 806 | } |
| 807 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 808 | kernfs_put(pos); |
Tejun Heo | 35beab0 | 2014-02-03 14:02:56 -0500 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | kernfs_put(pos); |
| 812 | } while (pos != kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /** |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 816 | * kernfs_remove - remove a kernfs_node recursively |
| 817 | * @kn: the kernfs_node to remove |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 818 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 819 | * Remove @kn along with all its subdirectories and files. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 820 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 821 | void kernfs_remove(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 822 | { |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 823 | mutex_lock(&kernfs_mutex); |
| 824 | __kernfs_remove(kn); |
| 825 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** |
Tejun Heo | 6b0afc2 | 2014-02-03 14:03:01 -0500 | [diff] [blame^] | 829 | * kernfs_break_active_protection - break out of active protection |
| 830 | * @kn: the self kernfs_node |
| 831 | * |
| 832 | * The caller must be running off of a kernfs operation which is invoked |
| 833 | * with an active reference - e.g. one of kernfs_ops. Each invocation of |
| 834 | * this function must also be matched with an invocation of |
| 835 | * kernfs_unbreak_active_protection(). |
| 836 | * |
| 837 | * This function releases the active reference of @kn the caller is |
| 838 | * holding. Once this function is called, @kn may be removed at any point |
| 839 | * and the caller is solely responsible for ensuring that the objects it |
| 840 | * dereferences are accessible. |
| 841 | */ |
| 842 | void kernfs_break_active_protection(struct kernfs_node *kn) |
| 843 | { |
| 844 | /* |
| 845 | * Take out ourself out of the active ref dependency chain. If |
| 846 | * we're called without an active ref, lockdep will complain. |
| 847 | */ |
| 848 | kernfs_put_active(kn); |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * kernfs_unbreak_active_protection - undo kernfs_break_active_protection() |
| 853 | * @kn: the self kernfs_node |
| 854 | * |
| 855 | * If kernfs_break_active_protection() was called, this function must be |
| 856 | * invoked before finishing the kernfs operation. Note that while this |
| 857 | * function restores the active reference, it doesn't and can't actually |
| 858 | * restore the active protection - @kn may already or be in the process of |
| 859 | * being removed. Once kernfs_break_active_protection() is invoked, that |
| 860 | * protection is irreversibly gone for the kernfs operation instance. |
| 861 | * |
| 862 | * While this function may be called at any point after |
| 863 | * kernfs_break_active_protection() is invoked, its most useful location |
| 864 | * would be right before the enclosing kernfs operation returns. |
| 865 | */ |
| 866 | void kernfs_unbreak_active_protection(struct kernfs_node *kn) |
| 867 | { |
| 868 | /* |
| 869 | * @kn->active could be in any state; however, the increment we do |
| 870 | * here will be undone as soon as the enclosing kernfs operation |
| 871 | * finishes and this temporary bump can't break anything. If @kn |
| 872 | * is alive, nothing changes. If @kn is being deactivated, the |
| 873 | * soon-to-follow put will either finish deactivation or restore |
| 874 | * deactivated state. If @kn is already removed, the temporary |
| 875 | * bump is guaranteed to be gone before @kn is released. |
| 876 | */ |
| 877 | atomic_inc(&kn->active); |
| 878 | if (kernfs_lockdep(kn)) |
| 879 | rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_); |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * kernfs_remove_self - remove a kernfs_node from its own method |
| 884 | * @kn: the self kernfs_node to remove |
| 885 | * |
| 886 | * The caller must be running off of a kernfs operation which is invoked |
| 887 | * with an active reference - e.g. one of kernfs_ops. This can be used to |
| 888 | * implement a file operation which deletes itself. |
| 889 | * |
| 890 | * For example, the "delete" file for a sysfs device directory can be |
| 891 | * implemented by invoking kernfs_remove_self() on the "delete" file |
| 892 | * itself. This function breaks the circular dependency of trying to |
| 893 | * deactivate self while holding an active ref itself. It isn't necessary |
| 894 | * to modify the usual removal path to use kernfs_remove_self(). The |
| 895 | * "delete" implementation can simply invoke kernfs_remove_self() on self |
| 896 | * before proceeding with the usual removal path. kernfs will ignore later |
| 897 | * kernfs_remove() on self. |
| 898 | * |
| 899 | * kernfs_remove_self() can be called multiple times concurrently on the |
| 900 | * same kernfs_node. Only the first one actually performs removal and |
| 901 | * returns %true. All others will wait until the kernfs operation which |
| 902 | * won self-removal finishes and return %false. Note that the losers wait |
| 903 | * for the completion of not only the winning kernfs_remove_self() but also |
| 904 | * the whole kernfs_ops which won the arbitration. This can be used to |
| 905 | * guarantee, for example, all concurrent writes to a "delete" file to |
| 906 | * finish only after the whole operation is complete. |
| 907 | */ |
| 908 | bool kernfs_remove_self(struct kernfs_node *kn) |
| 909 | { |
| 910 | bool ret; |
| 911 | |
| 912 | mutex_lock(&kernfs_mutex); |
| 913 | kernfs_break_active_protection(kn); |
| 914 | |
| 915 | /* |
| 916 | * SUICIDAL is used to arbitrate among competing invocations. Only |
| 917 | * the first one will actually perform removal. When the removal |
| 918 | * is complete, SUICIDED is set and the active ref is restored |
| 919 | * while holding kernfs_mutex. The ones which lost arbitration |
| 920 | * waits for SUICDED && drained which can happen only after the |
| 921 | * enclosing kernfs operation which executed the winning instance |
| 922 | * of kernfs_remove_self() finished. |
| 923 | */ |
| 924 | if (!(kn->flags & KERNFS_SUICIDAL)) { |
| 925 | kn->flags |= KERNFS_SUICIDAL; |
| 926 | __kernfs_remove(kn); |
| 927 | kn->flags |= KERNFS_SUICIDED; |
| 928 | ret = true; |
| 929 | } else { |
| 930 | wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq; |
| 931 | DEFINE_WAIT(wait); |
| 932 | |
| 933 | while (true) { |
| 934 | prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE); |
| 935 | |
| 936 | if ((kn->flags & KERNFS_SUICIDED) && |
| 937 | atomic_read(&kn->active) == KN_DEACTIVATED_BIAS) |
| 938 | break; |
| 939 | |
| 940 | mutex_unlock(&kernfs_mutex); |
| 941 | schedule(); |
| 942 | mutex_lock(&kernfs_mutex); |
| 943 | } |
| 944 | finish_wait(waitq, &wait); |
| 945 | WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); |
| 946 | ret = false; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * This must be done while holding kernfs_mutex; otherwise, waiting |
| 951 | * for SUICIDED && deactivated could finish prematurely. |
| 952 | */ |
| 953 | kernfs_unbreak_active_protection(kn); |
| 954 | |
| 955 | mutex_unlock(&kernfs_mutex); |
| 956 | return ret; |
| 957 | } |
| 958 | |
| 959 | /** |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 960 | * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it |
| 961 | * @parent: parent of the target |
| 962 | * @name: name of the kernfs_node to remove |
| 963 | * @ns: namespace tag of the kernfs_node to remove |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 964 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 965 | * Look for the kernfs_node with @name and @ns under @parent and remove it. |
| 966 | * Returns 0 on success, -ENOENT if such entry doesn't exist. |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 967 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 968 | int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 969 | const void *ns) |
| 970 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 971 | struct kernfs_node *kn; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 972 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 973 | if (!parent) { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 974 | WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 975 | name); |
| 976 | return -ENOENT; |
| 977 | } |
| 978 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 979 | mutex_lock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 980 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 981 | kn = kernfs_find_ns(parent, name, ns); |
| 982 | if (kn) |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 983 | __kernfs_remove(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 984 | |
Tejun Heo | 988cd7a | 2014-02-03 14:02:58 -0500 | [diff] [blame] | 985 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 986 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 987 | if (kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 988 | return 0; |
| 989 | else |
| 990 | return -ENOENT; |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * kernfs_rename_ns - move and rename a kernfs_node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 995 | * @kn: target node |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 996 | * @new_parent: new parent to put @sd under |
| 997 | * @new_name: new name |
| 998 | * @new_ns: new namespace tag |
| 999 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1000 | int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1001 | const char *new_name, const void *new_ns) |
| 1002 | { |
| 1003 | int error; |
| 1004 | |
Tejun Heo | ae34372 | 2014-01-10 08:57:21 -0500 | [diff] [blame] | 1005 | mutex_lock(&kernfs_mutex); |
Tejun Heo | d0ae3d4 | 2013-12-11 16:02:56 -0500 | [diff] [blame] | 1006 | |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1007 | error = -ENOENT; |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 1008 | if (!kernfs_active(kn) || !kernfs_active(new_parent)) |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1009 | goto out; |
| 1010 | |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1011 | error = 0; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1012 | if ((kn->parent == new_parent) && (kn->ns == new_ns) && |
| 1013 | (strcmp(kn->name, new_name) == 0)) |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1014 | goto out; /* nothing to rename */ |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1015 | |
| 1016 | error = -EEXIST; |
| 1017 | if (kernfs_find_ns(new_parent, new_name, new_ns)) |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1018 | goto out; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1019 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1020 | /* rename kernfs_node */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1021 | if (strcmp(kn->name, new_name) != 0) { |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1022 | error = -ENOMEM; |
| 1023 | new_name = kstrdup(new_name, GFP_KERNEL); |
| 1024 | if (!new_name) |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1025 | goto out; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1026 | |
Tejun Heo | 47a52e9 | 2013-12-11 16:02:58 -0500 | [diff] [blame] | 1027 | if (kn->flags & KERNFS_STATIC_NAME) |
| 1028 | kn->flags &= ~KERNFS_STATIC_NAME; |
| 1029 | else |
| 1030 | kfree(kn->name); |
| 1031 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1032 | kn->name = new_name; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Move to the appropriate place in the appropriate directories rbtree. |
| 1037 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1038 | kernfs_unlink_sibling(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1039 | kernfs_get(new_parent); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1040 | kernfs_put(kn->parent); |
| 1041 | kn->ns = new_ns; |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1042 | kn->hash = kernfs_name_hash(kn->name, kn->ns); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1043 | kn->parent = new_parent; |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1044 | kernfs_link_sibling(kn); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1045 | |
| 1046 | error = 0; |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1047 | out: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 1048 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1049 | return error; |
| 1050 | } |
| 1051 | |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1052 | /* Relationship between s_mode and the DT_xxx types */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1053 | static inline unsigned char dt_type(struct kernfs_node *kn) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1054 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1055 | return (kn->mode >> 12) & 15; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1056 | } |
| 1057 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1058 | static int kernfs_dir_fop_release(struct inode *inode, struct file *filp) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1059 | { |
| 1060 | kernfs_put(filp->private_data); |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1064 | static struct kernfs_node *kernfs_dir_pos(const void *ns, |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1065 | struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1066 | { |
| 1067 | if (pos) { |
Tejun Heo | 81c173c | 2014-02-03 14:03:00 -0500 | [diff] [blame] | 1068 | int valid = kernfs_active(pos) && |
Greg Kroah-Hartman | 798c75a | 2014-01-13 14:36:03 -0800 | [diff] [blame] | 1069 | pos->parent == parent && hash == pos->hash; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1070 | kernfs_put(pos); |
| 1071 | if (!valid) |
| 1072 | pos = NULL; |
| 1073 | } |
| 1074 | if (!pos && (hash > 1) && (hash < INT_MAX)) { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1075 | struct rb_node *node = parent->dir.children.rb_node; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1076 | while (node) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1077 | pos = rb_to_kn(node); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1078 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1079 | if (hash < pos->hash) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1080 | node = node->rb_left; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1081 | else if (hash > pos->hash) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1082 | node = node->rb_right; |
| 1083 | else |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | /* Skip over entries in the wrong namespace */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1088 | while (pos && pos->ns != ns) { |
| 1089 | struct rb_node *node = rb_next(&pos->rb); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1090 | if (!node) |
| 1091 | pos = NULL; |
| 1092 | else |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1093 | pos = rb_to_kn(node); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1094 | } |
| 1095 | return pos; |
| 1096 | } |
| 1097 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1098 | static struct kernfs_node *kernfs_dir_next_pos(const void *ns, |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1099 | struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1100 | { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1101 | pos = kernfs_dir_pos(ns, parent, ino, pos); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1102 | if (pos) |
| 1103 | do { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1104 | struct rb_node *node = rb_next(&pos->rb); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1105 | if (!node) |
| 1106 | pos = NULL; |
| 1107 | else |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1108 | pos = rb_to_kn(node); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1109 | } while (pos && pos->ns != ns); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1110 | return pos; |
| 1111 | } |
| 1112 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1113 | static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1114 | { |
| 1115 | struct dentry *dentry = file->f_path.dentry; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1116 | struct kernfs_node *parent = dentry->d_fsdata; |
| 1117 | struct kernfs_node *pos = file->private_data; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1118 | const void *ns = NULL; |
| 1119 | |
| 1120 | if (!dir_emit_dots(file, ctx)) |
| 1121 | return 0; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 1122 | mutex_lock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1123 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 1124 | if (kernfs_ns_enabled(parent)) |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 1125 | ns = kernfs_info(dentry->d_sb)->ns; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1126 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1127 | for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1128 | pos; |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1129 | pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1130 | const char *name = pos->name; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1131 | unsigned int type = dt_type(pos); |
| 1132 | int len = strlen(name); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1133 | ino_t ino = pos->ino; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1134 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 1135 | ctx->pos = pos->hash; |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1136 | file->private_data = pos; |
| 1137 | kernfs_get(pos); |
| 1138 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 1139 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1140 | if (!dir_emit(ctx, name, len, ino, type)) |
| 1141 | return 0; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 1142 | mutex_lock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1143 | } |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 1144 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1145 | file->private_data = NULL; |
| 1146 | ctx->pos = INT_MAX; |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1150 | static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset, |
| 1151 | int whence) |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1152 | { |
| 1153 | struct inode *inode = file_inode(file); |
| 1154 | loff_t ret; |
| 1155 | |
| 1156 | mutex_lock(&inode->i_mutex); |
| 1157 | ret = generic_file_llseek(file, offset, whence); |
| 1158 | mutex_unlock(&inode->i_mutex); |
| 1159 | |
| 1160 | return ret; |
| 1161 | } |
| 1162 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 1163 | const struct file_operations kernfs_dir_fops = { |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1164 | .read = generic_read_dir, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 1165 | .iterate = kernfs_fop_readdir, |
| 1166 | .release = kernfs_dir_fop_release, |
| 1167 | .llseek = kernfs_dir_fop_llseek, |
Tejun Heo | fd7b9f7 | 2013-11-28 14:54:33 -0500 | [diff] [blame] | 1168 | }; |