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