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