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