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