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