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