Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 2 | * fs/sysfs/dir.c - sysfs core and dir operation implementation |
| 3 | * |
| 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | * |
| 10 | * Please see Documentation/filesystems/sysfs.txt for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #undef DEBUG |
| 14 | |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/mount.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/kobject.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 19 | #include <linux/namei.h> |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 20 | #include <linux/idr.h> |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 21 | #include <linux/completion.h> |
Dave Young | 869512a | 2007-07-26 14:53:53 +0000 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Robert P. J. Day | c6f8773 | 2008-03-13 22:41:52 -0400 | [diff] [blame] | 23 | #include <linux/slab.h> |
Eric W. Biederman | 4c3da22 | 2009-11-04 02:50:06 -0800 | [diff] [blame] | 24 | #include <linux/security.h> |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 25 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "sysfs.h" |
| 27 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 28 | DEFINE_MUTEX(sysfs_mutex); |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 29 | DEFINE_SPINLOCK(sysfs_symlink_target_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Tejun Heo | bcac376 | 2013-09-11 22:29:03 -0400 | [diff] [blame] | 31 | #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb) |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 32 | |
Roel Kluin | f7a75f0 | 2007-10-16 23:30:25 -0700 | [diff] [blame] | 33 | static DEFINE_SPINLOCK(sysfs_ino_lock); |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 34 | static DEFINE_IDA(sysfs_ino_ida); |
| 35 | |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 36 | /** |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 37 | * sysfs_name_hash |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 38 | * @name: Null terminated string to hash |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 39 | * @ns: Namespace tag to hash |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 40 | * |
| 41 | * Returns 31 bit hash of ns + name (so it fits in an off_t ) |
| 42 | */ |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 43 | static unsigned int sysfs_name_hash(const char *name, const void *ns) |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 44 | { |
| 45 | unsigned long hash = init_name_hash(); |
| 46 | unsigned int len = strlen(name); |
| 47 | while (len--) |
| 48 | hash = partial_name_hash(*name++, hash); |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 49 | hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 50 | hash &= 0x7fffffffU; |
| 51 | /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ |
| 52 | if (hash < 1) |
| 53 | hash += 2; |
| 54 | if (hash >= INT_MAX) |
| 55 | hash = INT_MAX - 1; |
| 56 | return hash; |
| 57 | } |
| 58 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 59 | static int sysfs_name_compare(unsigned int hash, const char *name, |
| 60 | const void *ns, const struct sysfs_dirent *sd) |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 61 | { |
| 62 | if (hash != sd->s_hash) |
| 63 | return hash - sd->s_hash; |
| 64 | if (ns != sd->s_ns) |
| 65 | return ns - sd->s_ns; |
| 66 | return strcmp(name, sd->s_name); |
| 67 | } |
| 68 | |
| 69 | static int sysfs_sd_compare(const struct sysfs_dirent *left, |
| 70 | const struct sysfs_dirent *right) |
| 71 | { |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 72 | return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns, |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 73 | right); |
| 74 | } |
| 75 | |
| 76 | /** |
Warner Wang | 4347491 | 2013-05-13 11:11:05 +0800 | [diff] [blame] | 77 | * sysfs_link_sibling - link sysfs_dirent into sibling rbtree |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 78 | * @sd: sysfs_dirent of interest |
| 79 | * |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 80 | * Link @sd into its sibling rbtree which starts from |
Tejun Heo | bc747f3 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 81 | * sd->s_parent->s_dir.children. |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 82 | * |
| 83 | * Locking: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 84 | * mutex_lock(sysfs_mutex) |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 85 | * |
| 86 | * RETURNS: |
| 87 | * 0 on susccess -EEXIST on failure. |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 88 | */ |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 89 | static int sysfs_link_sibling(struct sysfs_dirent *sd) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 90 | { |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 91 | struct rb_node **node = &sd->s_parent->s_dir.children.rb_node; |
| 92 | struct rb_node *parent = NULL; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 93 | |
Greg Kroah-Hartman | 54d20f0 | 2012-03-08 13:03:10 -0800 | [diff] [blame] | 94 | if (sysfs_type(sd) == SYSFS_DIR) |
| 95 | sd->s_parent->s_dir.subdirs++; |
| 96 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 97 | while (*node) { |
| 98 | struct sysfs_dirent *pos; |
| 99 | int result; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 100 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 101 | pos = to_sysfs_dirent(*node); |
| 102 | parent = *node; |
| 103 | result = sysfs_sd_compare(sd, pos); |
| 104 | if (result < 0) |
| 105 | node = &pos->s_rb.rb_left; |
| 106 | else if (result > 0) |
| 107 | node = &pos->s_rb.rb_right; |
| 108 | else |
| 109 | return -EEXIST; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 110 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 111 | /* add new node and rebalance the tree */ |
| 112 | rb_link_node(&sd->s_rb, parent, node); |
| 113 | rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children); |
| 114 | return 0; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 118 | * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 119 | * @sd: sysfs_dirent of interest |
| 120 | * |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 121 | * Unlink @sd from its sibling rbtree which starts from |
Tejun Heo | bc747f3 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 122 | * sd->s_parent->s_dir.children. |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 123 | * |
| 124 | * Locking: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 125 | * mutex_lock(sysfs_mutex) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 126 | */ |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 127 | static void sysfs_unlink_sibling(struct sysfs_dirent *sd) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 128 | { |
Greg Kroah-Hartman | 54d20f0 | 2012-03-08 13:03:10 -0800 | [diff] [blame] | 129 | if (sysfs_type(sd) == SYSFS_DIR) |
| 130 | sd->s_parent->s_dir.subdirs--; |
| 131 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 132 | rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children); |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 136 | * sysfs_get_active - get an active reference to sysfs_dirent |
| 137 | * @sd: sysfs_dirent to get an active reference to |
| 138 | * |
| 139 | * Get an active reference of @sd. This function is noop if @sd |
| 140 | * is NULL. |
| 141 | * |
| 142 | * RETURNS: |
| 143 | * Pointer to @sd on success, NULL on failure. |
| 144 | */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 145 | struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 146 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 147 | if (unlikely(!sd)) |
| 148 | return NULL; |
| 149 | |
Maarten Lankhorst | 3db3c62 | 2013-03-08 16:07:27 +0100 | [diff] [blame] | 150 | if (!atomic_inc_unless_negative(&sd->s_active)) |
| 151 | return NULL; |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 152 | |
Tejun Heo | 785a162 | 2013-10-14 09:27:11 -0400 | [diff] [blame] | 153 | if (likely(!sysfs_ignore_lockdep(sd))) |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 154 | rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_); |
| 155 | return sd; |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * sysfs_put_active - put an active reference to sysfs_dirent |
| 160 | * @sd: sysfs_dirent to put an active reference to |
| 161 | * |
| 162 | * Put an active reference to @sd. This function is noop if @sd |
| 163 | * is NULL. |
| 164 | */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 165 | void sysfs_put_active(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 166 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 167 | int v; |
| 168 | |
| 169 | if (unlikely(!sd)) |
| 170 | return; |
| 171 | |
Tejun Heo | 785a162 | 2013-10-14 09:27:11 -0400 | [diff] [blame] | 172 | if (likely(!sysfs_ignore_lockdep(sd))) |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 173 | rwsem_release(&sd->dep_map, 1, _RET_IP_); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 174 | v = atomic_dec_return(&sd->s_active); |
| 175 | if (likely(v != SD_DEACTIVATED_BIAS)) |
| 176 | return; |
| 177 | |
| 178 | /* atomic_dec_return() is a mb(), we'll always see the updated |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 179 | * sd->u.completion. |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 180 | */ |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 181 | complete(sd->u.completion); |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 185 | * sysfs_deactivate - deactivate sysfs_dirent |
| 186 | * @sd: sysfs_dirent to deactivate |
| 187 | * |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 188 | * Deny new active references and drain existing ones. |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 189 | */ |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 190 | static void sysfs_deactivate(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 191 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 192 | DECLARE_COMPLETION_ONSTACK(wait); |
| 193 | int v; |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 194 | |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 195 | BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED)); |
Eric W. Biederman | a2db684 | 2010-02-11 15:20:00 -0800 | [diff] [blame] | 196 | |
| 197 | if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF)) |
| 198 | return; |
| 199 | |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 200 | sd->u.completion = (void *)&wait; |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 201 | |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 202 | rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 203 | /* atomic_add_return() is a mb(), put_active() will always see |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 204 | * the updated sd->u.completion. |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 205 | */ |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 206 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); |
| 207 | |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 208 | if (v != SD_DEACTIVATED_BIAS) { |
| 209 | lock_contended(&sd->dep_map, _RET_IP_); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 210 | wait_for_completion(&wait); |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 211 | } |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 212 | |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 213 | lock_acquired(&sd->dep_map, _RET_IP_); |
| 214 | rwsem_release(&sd->dep_map, 1, _RET_IP_); |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Eric W. Biederman | cafa6b5 | 2011-12-18 20:08:16 -0800 | [diff] [blame] | 217 | static int sysfs_alloc_ino(unsigned int *pino) |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 218 | { |
| 219 | int ino, rc; |
| 220 | |
| 221 | retry: |
| 222 | spin_lock(&sysfs_ino_lock); |
| 223 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); |
| 224 | spin_unlock(&sysfs_ino_lock); |
| 225 | |
| 226 | if (rc == -EAGAIN) { |
| 227 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) |
| 228 | goto retry; |
| 229 | rc = -ENOMEM; |
| 230 | } |
| 231 | |
| 232 | *pino = ino; |
| 233 | return rc; |
| 234 | } |
| 235 | |
Eric W. Biederman | cafa6b5 | 2011-12-18 20:08:16 -0800 | [diff] [blame] | 236 | static void sysfs_free_ino(unsigned int ino) |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 237 | { |
| 238 | spin_lock(&sysfs_ino_lock); |
| 239 | ida_remove(&sysfs_ino_ida, ino); |
| 240 | spin_unlock(&sysfs_ino_lock); |
| 241 | } |
| 242 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 243 | void release_sysfs_dirent(struct sysfs_dirent *sd) |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 244 | { |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 245 | struct sysfs_dirent *parent_sd; |
| 246 | |
| 247 | repeat: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 248 | /* Moving/renaming is always done while holding reference. |
| 249 | * sd->s_parent won't change beneath us. |
| 250 | */ |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 251 | parent_sd = sd->s_parent; |
| 252 | |
Ming Lei | bb2b005 | 2013-04-04 22:22:37 +0800 | [diff] [blame] | 253 | WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED), |
| 254 | "sysfs: free using entry: %s/%s\n", |
| 255 | parent_sd ? parent_sd->s_name : "", sd->s_name); |
| 256 | |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 257 | if (sysfs_type(sd) == SYSFS_KOBJ_LINK) |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 258 | sysfs_put(sd->s_symlink.target_sd); |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 259 | if (sysfs_type(sd) & SYSFS_COPY_NAME) |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 260 | kfree(sd->s_name); |
Eric W. Biederman | 4c3da22 | 2009-11-04 02:50:06 -0800 | [diff] [blame] | 261 | if (sd->s_iattr && sd->s_iattr->ia_secdata) |
| 262 | security_release_secctx(sd->s_iattr->ia_secdata, |
| 263 | sd->s_iattr->ia_secdata_len); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 264 | kfree(sd->s_iattr); |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 265 | sysfs_free_ino(sd->s_ino); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 266 | kmem_cache_free(sysfs_dir_cachep, sd); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 267 | |
| 268 | sd = parent_sd; |
| 269 | if (sd && atomic_dec_and_test(&sd->s_count)) |
| 270 | goto repeat; |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 271 | } |
| 272 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 273 | static int sysfs_dentry_delete(const struct dentry *dentry) |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 274 | { |
| 275 | struct sysfs_dirent *sd = dentry->d_fsdata; |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 276 | return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED)); |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 279 | static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 280 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 281 | struct sysfs_dirent *sd; |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 282 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 283 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 284 | return -ECHILD; |
| 285 | |
| 286 | sd = dentry->d_fsdata; |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 287 | mutex_lock(&sysfs_mutex); |
| 288 | |
| 289 | /* The sysfs dirent has been deleted */ |
| 290 | if (sd->s_flags & SYSFS_FLAG_REMOVED) |
| 291 | goto out_bad; |
| 292 | |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 293 | /* The sysfs dirent has been moved? */ |
| 294 | if (dentry->d_parent->d_fsdata != sd->s_parent) |
| 295 | goto out_bad; |
| 296 | |
| 297 | /* The sysfs dirent has been renamed */ |
| 298 | if (strcmp(dentry->d_name.name, sd->s_name) != 0) |
| 299 | goto out_bad; |
| 300 | |
Glauber Costa | e5bcac6 | 2012-07-06 13:09:07 +0400 | [diff] [blame] | 301 | /* The sysfs dirent has been moved to a different namespace */ |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 302 | if (sd->s_parent && (sd->s_parent->s_flags & SYSFS_FLAG_NS) && |
| 303 | sysfs_info(dentry->d_sb)->ns != sd->s_ns) |
| 304 | goto out_bad; |
Glauber Costa | e5bcac6 | 2012-07-06 13:09:07 +0400 | [diff] [blame] | 305 | |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 306 | mutex_unlock(&sysfs_mutex); |
| 307 | out_valid: |
| 308 | return 1; |
| 309 | out_bad: |
| 310 | /* Remove the dentry from the dcache hashes. |
| 311 | * If this is a deleted dentry we use d_drop instead of d_delete |
| 312 | * so sysfs doesn't need to cope with negative dentries. |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 313 | * |
| 314 | * If this is a dentry that has simply been renamed we |
| 315 | * use d_drop to remove it from the dcache lookup on its |
| 316 | * old parent. If this dentry persists later when a lookup |
| 317 | * is performed at its new name the dentry will be readded |
| 318 | * to the dcache hashes. |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 319 | */ |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 320 | mutex_unlock(&sysfs_mutex); |
Miklos Szeredi | 6497d16 | 2013-09-05 11:44:41 +0200 | [diff] [blame] | 321 | |
| 322 | /* If we have submounts we must allow the vfs caches |
| 323 | * to lie about the state of the filesystem to prevent |
| 324 | * leaks and other nasty things. |
| 325 | */ |
| 326 | if (check_submounts_and_drop(dentry) != 0) |
| 327 | goto out_valid; |
| 328 | |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 332 | static void sysfs_dentry_release(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 334 | sysfs_put(dentry->d_fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 337 | const struct dentry_operations sysfs_dentry_ops = { |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 338 | .d_revalidate = sysfs_dentry_revalidate, |
| 339 | .d_delete = sysfs_dentry_delete, |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 340 | .d_release = sysfs_dentry_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | }; |
| 342 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 343 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 345 | char *dup_name = NULL; |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 346 | struct sysfs_dirent *sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 347 | |
| 348 | if (type & SYSFS_COPY_NAME) { |
| 349 | name = dup_name = kstrdup(name, GFP_KERNEL); |
| 350 | if (!name) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 351 | return NULL; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 354 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (!sd) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 356 | goto err_out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 358 | if (sysfs_alloc_ino(&sd->s_ino)) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 359 | goto err_out2; |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | atomic_set(&sd->s_count, 1); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 362 | atomic_set(&sd->s_active, 0); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 363 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 364 | sd->s_name = name; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 365 | sd->s_mode = mode; |
Ming Lei | bb2b005 | 2013-04-04 22:22:37 +0800 | [diff] [blame] | 366 | sd->s_flags = type | SYSFS_FLAG_REMOVED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | return sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 369 | |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 370 | err_out2: |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 371 | kmem_cache_free(sysfs_dir_cachep, sd); |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 372 | err_out1: |
| 373 | kfree(dup_name); |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 374 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 377 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 378 | * sysfs_addrm_start - prepare for sysfs_dirent add/remove |
| 379 | * @acxt: pointer to sysfs_addrm_cxt to be used |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 380 | * |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 381 | * This function is called when the caller is about to add or remove |
| 382 | * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used |
| 383 | * to keep and pass context to other addrm functions. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 384 | * |
| 385 | * LOCKING: |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 386 | * Kernel thread context (may sleep). sysfs_mutex is locked on |
Eric W. Biederman | a16bbc3 | 2009-11-20 16:08:55 -0800 | [diff] [blame] | 387 | * return. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 388 | */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 389 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt) |
| 390 | __acquires(sysfs_mutex) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 391 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 392 | memset(acxt, 0, sizeof(*acxt)); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 393 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 394 | mutex_lock(&sysfs_mutex); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 398 | * __sysfs_add_one - add sysfs_dirent to parent without warning |
| 399 | * @acxt: addrm context to use |
| 400 | * @sd: sysfs_dirent to be added |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 401 | * @parent_sd: the parent sysfs_dirent to add @sd to |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 402 | * |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 403 | * Get @parent_sd and set @sd->s_parent to it and increment nlink of |
| 404 | * the parent inode if @sd is a directory and link into the children |
| 405 | * list of the parent. |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 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 | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 418 | int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, |
| 419 | struct sysfs_dirent *parent_sd) |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 420 | { |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 421 | bool has_ns = parent_sd->s_flags & SYSFS_FLAG_NS; |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 422 | struct sysfs_inode_attrs *ps_iattr; |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 423 | int ret; |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 424 | |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 425 | if (has_ns != (bool)sd->s_ns) { |
Linus Torvalds | a1212d2 | 2013-11-07 20:47:28 +0900 | [diff] [blame] | 426 | WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 427 | has_ns ? "required" : "invalid", |
| 428 | parent_sd->s_name, sd->s_name); |
Linus Torvalds | a1212d2 | 2013-11-07 20:47:28 +0900 | [diff] [blame] | 429 | return -EINVAL; |
| 430 | } |
| 431 | |
Tejun Heo | ae2108a | 2013-11-23 17:21:47 -0500 | [diff] [blame] | 432 | if (sysfs_type(parent_sd) != SYSFS_DIR) |
| 433 | return -EINVAL; |
| 434 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 435 | sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 436 | sd->s_parent = sysfs_get(parent_sd); |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 437 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 438 | ret = sysfs_link_sibling(sd); |
| 439 | if (ret) |
| 440 | return ret; |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 441 | |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 442 | /* Update timestamps on the parent */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 443 | ps_iattr = parent_sd->s_iattr; |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [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 | |
Ming Lei | bb2b005 | 2013-04-04 22:22:37 +0800 | [diff] [blame] | 449 | /* Mark the entry added into directory tree */ |
| 450 | sd->s_flags &= ~SYSFS_FLAG_REMOVED; |
| 451 | |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | /** |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 456 | * sysfs_pathname - return full path to sysfs dirent |
| 457 | * @sd: sysfs_dirent whose path we want |
Geert Uytterhoeven | 66081a7 | 2012-09-29 22:23:19 +0200 | [diff] [blame] | 458 | * @path: caller allocated buffer of size PATH_MAX |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 459 | * |
| 460 | * Gives the name "/" to the sysfs_root entry; any path returned |
| 461 | * is relative to wherever sysfs is mounted. |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 462 | */ |
| 463 | static char *sysfs_pathname(struct sysfs_dirent *sd, char *path) |
| 464 | { |
| 465 | if (sd->s_parent) { |
| 466 | sysfs_pathname(sd->s_parent, path); |
Geert Uytterhoeven | 66081a7 | 2012-09-29 22:23:19 +0200 | [diff] [blame] | 467 | strlcat(path, "/", PATH_MAX); |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 468 | } |
Geert Uytterhoeven | 66081a7 | 2012-09-29 22:23:19 +0200 | [diff] [blame] | 469 | strlcat(path, sd->s_name, PATH_MAX); |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 470 | return path; |
| 471 | } |
| 472 | |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 473 | void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name) |
| 474 | { |
| 475 | char *path; |
| 476 | |
| 477 | path = kzalloc(PATH_MAX, GFP_KERNEL); |
| 478 | if (path) { |
| 479 | sysfs_pathname(parent, path); |
| 480 | strlcat(path, "/", PATH_MAX); |
| 481 | strlcat(path, name, PATH_MAX); |
| 482 | } |
| 483 | |
| 484 | WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n", |
| 485 | path ? path : name); |
| 486 | |
| 487 | kfree(path); |
| 488 | } |
| 489 | |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 490 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 491 | * sysfs_add_one - add sysfs_dirent to parent |
| 492 | * @acxt: addrm context to use |
| 493 | * @sd: sysfs_dirent to be added |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 494 | * @parent_sd: the parent sysfs_dirent to add @sd to |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 495 | * |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 496 | * Get @parent_sd and set @sd->s_parent to it and increment nlink of |
| 497 | * the parent inode if @sd is a directory and link into the children |
| 498 | * list of the parent. |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 499 | * |
| 500 | * This function should be called between calls to |
| 501 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 502 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 503 | * |
| 504 | * LOCKING: |
| 505 | * Determined by sysfs_addrm_start(). |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 506 | * |
| 507 | * RETURNS: |
| 508 | * 0 on success, -EEXIST if entry with the given name already |
| 509 | * exists. |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 510 | */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 511 | int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, |
| 512 | struct sysfs_dirent *parent_sd) |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 513 | { |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 514 | int ret; |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 515 | |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 516 | ret = __sysfs_add_one(acxt, sd, parent_sd); |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 517 | |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 518 | if (ret == -EEXIST) |
| 519 | sysfs_warn_dup(parent_sd, sd->s_name); |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 520 | return ret; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * sysfs_remove_one - remove sysfs_dirent from parent |
| 525 | * @acxt: addrm context to use |
Jean Delvare | 9fd5b1c | 2008-01-08 18:11:24 +0100 | [diff] [blame] | 526 | * @sd: sysfs_dirent to be removed |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 527 | * |
| 528 | * Mark @sd removed and drop nlink of parent inode if @sd is a |
Tejun Heo | 181b2e4 | 2007-09-20 16:05:09 +0900 | [diff] [blame] | 529 | * directory. @sd is unlinked from the children list. |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 530 | * |
| 531 | * This function should be called between calls to |
| 532 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 533 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 534 | * |
| 535 | * LOCKING: |
| 536 | * Determined by sysfs_addrm_start(). |
| 537 | */ |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 538 | static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, |
| 539 | struct sysfs_dirent *sd) |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 540 | { |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 541 | struct sysfs_inode_attrs *ps_iattr; |
| 542 | |
Tejun Heo | 26ea12d | 2013-09-18 17:15:36 -0400 | [diff] [blame] | 543 | /* |
| 544 | * Removal can be called multiple times on the same node. Only the |
| 545 | * first invocation is effective and puts the base ref. |
| 546 | */ |
| 547 | if (sd->s_flags & SYSFS_FLAG_REMOVED) |
| 548 | return; |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 549 | |
| 550 | sysfs_unlink_sibling(sd); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 551 | |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 552 | /* Update timestamps on the parent */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 553 | ps_iattr = sd->s_parent->s_iattr; |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 554 | if (ps_iattr) { |
| 555 | struct iattr *ps_iattrs = &ps_iattr->ia_iattr; |
| 556 | ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; |
| 557 | } |
| 558 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 559 | sd->s_flags |= SYSFS_FLAG_REMOVED; |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 560 | sd->u.removed_list = acxt->removed; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 561 | acxt->removed = sd; |
Tejun Heo | a0edd7c | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /** |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 565 | * sysfs_addrm_finish - finish up sysfs_dirent add/remove |
| 566 | * @acxt: addrm context to finish up |
| 567 | * |
| 568 | * Finish up sysfs_dirent add/remove. Resources acquired by |
| 569 | * sysfs_addrm_start() are released and removed sysfs_dirents are |
Eric W. Biederman | a16bbc3 | 2009-11-20 16:08:55 -0800 | [diff] [blame] | 570 | * cleaned up. |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 571 | * |
| 572 | * LOCKING: |
Eric W. Biederman | a16bbc3 | 2009-11-20 16:08:55 -0800 | [diff] [blame] | 573 | * sysfs_mutex is released. |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 574 | */ |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 575 | void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 576 | __releases(sysfs_mutex) |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 577 | { |
| 578 | /* release resources acquired by sysfs_addrm_start() */ |
| 579 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 580 | |
| 581 | /* kill removed sysfs_dirents */ |
| 582 | while (acxt->removed) { |
| 583 | struct sysfs_dirent *sd = acxt->removed; |
| 584 | |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 585 | acxt->removed = sd->u.removed_list; |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 586 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 587 | sysfs_deactivate(sd); |
Tejun Heo | 73d9714 | 2013-10-01 17:42:07 -0400 | [diff] [blame] | 588 | sysfs_unmap_bin_file(sd); |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 589 | sysfs_put(sd); |
| 590 | } |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 593 | /** |
| 594 | * sysfs_find_dirent - find sysfs_dirent with the given name |
| 595 | * @parent_sd: sysfs_dirent to search under |
| 596 | * @name: name to look for |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 597 | * @ns: the namespace tag to use |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 598 | * |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 599 | * Look for sysfs_dirent with name @name under @parent_sd. |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 600 | * |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 601 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 602 | * mutex_lock(sysfs_mutex) |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 603 | * |
| 604 | * RETURNS: |
| 605 | * Pointer to sysfs_dirent if found, NULL if not. |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 606 | */ |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 607 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 608 | const unsigned char *name, |
| 609 | const void *ns) |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 610 | { |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 611 | struct rb_node *node = parent_sd->s_dir.children.rb_node; |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 612 | bool has_ns = parent_sd->s_flags & SYSFS_FLAG_NS; |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 613 | unsigned int hash; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 614 | |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 615 | if (has_ns != (bool)ns) { |
Linus Torvalds | a1212d2 | 2013-11-07 20:47:28 +0900 | [diff] [blame] | 616 | WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 617 | has_ns ? "required" : "invalid", |
| 618 | parent_sd->s_name, name); |
Linus Torvalds | a1212d2 | 2013-11-07 20:47:28 +0900 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
| 621 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 622 | hash = sysfs_name_hash(name, ns); |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 623 | while (node) { |
| 624 | struct sysfs_dirent *sd; |
| 625 | int result; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 626 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 627 | sd = to_sysfs_dirent(node); |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 628 | result = sysfs_name_compare(hash, name, ns, sd); |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 629 | if (result < 0) |
| 630 | node = node->rb_left; |
| 631 | else if (result > 0) |
| 632 | node = node->rb_right; |
| 633 | else |
| 634 | return sd; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 635 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 636 | return NULL; |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 637 | } |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 638 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 639 | /** |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 640 | * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 641 | * @parent_sd: sysfs_dirent to search under |
| 642 | * @name: name to look for |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 643 | * @ns: the namespace tag to use |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 644 | * |
| 645 | * Look for sysfs_dirent with name @name under @parent_sd and get |
| 646 | * it if found. |
| 647 | * |
| 648 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 649 | * Kernel thread context (may sleep). Grabs sysfs_mutex. |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 650 | * |
| 651 | * RETURNS: |
| 652 | * Pointer to sysfs_dirent if found, NULL if not. |
| 653 | */ |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 654 | struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, |
| 655 | const unsigned char *name, |
| 656 | const void *ns) |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 657 | { |
| 658 | struct sysfs_dirent *sd; |
| 659 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 660 | mutex_lock(&sysfs_mutex); |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 661 | sd = sysfs_find_dirent(parent_sd, name, ns); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 662 | sysfs_get(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 663 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 664 | |
| 665 | return sd; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 666 | } |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 667 | EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns); |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 668 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 669 | /** |
| 670 | * kernfs_create_dir_ns - create a directory |
| 671 | * @parent: parent in which to create a new directory |
| 672 | * @name: name of the new directory |
| 673 | * @priv: opaque data associated with the new directory |
| 674 | * @ns: optional namespace tag of the directory |
| 675 | * |
| 676 | * Returns the created node on success, ERR_PTR() value on failure. |
| 677 | */ |
| 678 | struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent, |
| 679 | const char *name, void *priv, |
| 680 | const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 682 | umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 683 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 684 | struct sysfs_dirent *sd; |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 685 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 687 | /* allocate */ |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 688 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 689 | if (!sd) |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 690 | return ERR_PTR(-ENOMEM); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 691 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 692 | sd->s_ns = ns; |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 693 | sd->priv = priv; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 694 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 695 | /* link in */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 696 | sysfs_addrm_start(&acxt); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 697 | rc = __sysfs_add_one(&acxt, sd, parent); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 698 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 699 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 700 | if (!rc) |
| 701 | return sd; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 702 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 703 | sysfs_put(sd); |
| 704 | return ERR_PTR(rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 708 | * sysfs_create_dir_ns - create a directory for an object with a namespace tag |
| 709 | * @kobj: object we're creating directory for |
| 710 | * @ns: the namespace tag to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | */ |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 712 | int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 714 | struct sysfs_dirent *parent_sd, *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
| 716 | BUG_ON(!kobj); |
| 717 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 718 | if (kobj->parent) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 719 | parent_sd = kobj->parent->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | else |
Eric W. Biederman | 7d0c7d6 | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 721 | parent_sd = &sysfs_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Dan Williams | 3a19888 | 2012-04-06 13:41:06 -0700 | [diff] [blame] | 723 | if (!parent_sd) |
| 724 | return -ENOENT; |
| 725 | |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 726 | sd = kernfs_create_dir_ns(parent_sd, kobject_name(kobj), kobj, ns); |
| 727 | if (IS_ERR(sd)) { |
| 728 | if (PTR_ERR(sd) == -EEXIST) |
| 729 | sysfs_warn_dup(parent_sd, kobject_name(kobj)); |
| 730 | return PTR_ERR(sd); |
| 731 | } |
| 732 | |
| 733 | kobj->sd = sd; |
| 734 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 737 | static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry, |
| 738 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 740 | struct dentry *ret = NULL; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 741 | struct dentry *parent = dentry->d_parent; |
| 742 | struct sysfs_dirent *parent_sd = parent->d_fsdata; |
Tejun Heo | a7a0475 | 2007-08-02 21:38:02 +0900 | [diff] [blame] | 743 | struct sysfs_dirent *sd; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 744 | struct inode *inode; |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 745 | const void *ns = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 747 | mutex_lock(&sysfs_mutex); |
| 748 | |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 749 | if (parent_sd->s_flags & SYSFS_FLAG_NS) |
| 750 | ns = sysfs_info(dir->i_sb)->ns; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 751 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 752 | sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 754 | /* no such entry */ |
Tejun Heo | e49452c | 2008-01-16 12:06:14 +0900 | [diff] [blame] | 755 | if (!sd) { |
| 756 | ret = ERR_PTR(-ENOENT); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 757 | goto out_unlock; |
Tejun Heo | e49452c | 2008-01-16 12:06:14 +0900 | [diff] [blame] | 758 | } |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 759 | dentry->d_fsdata = sysfs_get(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 760 | |
| 761 | /* attach dentry and inode */ |
Eric W. Biederman | fac2622 | 2010-02-12 19:22:27 -0800 | [diff] [blame] | 762 | inode = sysfs_get_inode(dir->i_sb, sd); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 763 | if (!inode) { |
| 764 | ret = ERR_PTR(-ENOMEM); |
| 765 | goto out_unlock; |
| 766 | } |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 767 | |
Tejun Heo | d6b4fd2 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 768 | /* instantiate and hash dentry */ |
Al Viro | e77fb7c | 2012-06-07 20:56:54 -0400 | [diff] [blame] | 769 | ret = d_materialise_unique(dentry, inode); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 770 | out_unlock: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 771 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 772 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 775 | const struct inode_operations sysfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | .lookup = sysfs_lookup, |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 777 | .permission = sysfs_permission, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 778 | .setattr = sysfs_setattr, |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 779 | .getattr = sysfs_getattr, |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 780 | .setxattr = sysfs_setxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | }; |
| 782 | |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 783 | static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos) |
| 784 | { |
| 785 | struct sysfs_dirent *last; |
| 786 | |
| 787 | while (true) { |
| 788 | struct rb_node *rbn; |
| 789 | |
| 790 | last = pos; |
| 791 | |
| 792 | if (sysfs_type(pos) != SYSFS_DIR) |
| 793 | break; |
| 794 | |
| 795 | rbn = rb_first(&pos->s_dir.children); |
| 796 | if (!rbn) |
| 797 | break; |
| 798 | |
| 799 | pos = to_sysfs_dirent(rbn); |
| 800 | } |
| 801 | |
| 802 | return last; |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * sysfs_next_descendant_post - find the next descendant for post-order walk |
| 807 | * @pos: the current position (%NULL to initiate traversal) |
| 808 | * @root: sysfs_dirent whose descendants to walk |
| 809 | * |
| 810 | * Find the next descendant to visit for post-order traversal of @root's |
| 811 | * descendants. @root is included in the iteration and the last node to be |
| 812 | * visited. |
| 813 | */ |
| 814 | static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos, |
| 815 | struct sysfs_dirent *root) |
| 816 | { |
| 817 | struct rb_node *rbn; |
| 818 | |
| 819 | lockdep_assert_held(&sysfs_mutex); |
| 820 | |
| 821 | /* if first iteration, visit leftmost descendant which may be root */ |
| 822 | if (!pos) |
| 823 | return sysfs_leftmost_descendant(root); |
| 824 | |
| 825 | /* if we visited @root, we're done */ |
| 826 | if (pos == root) |
| 827 | return NULL; |
| 828 | |
| 829 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
| 830 | rbn = rb_next(&pos->s_rb); |
| 831 | if (rbn) |
| 832 | return sysfs_leftmost_descendant(to_sysfs_dirent(rbn)); |
| 833 | |
| 834 | /* no sibling left, visit parent */ |
| 835 | return pos->s_parent; |
| 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 838 | static void __kernfs_remove(struct sysfs_addrm_cxt *acxt, |
| 839 | struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 841 | struct sysfs_dirent *pos, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 843 | if (!sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | return; |
| 845 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 846 | pr_debug("sysfs %s: removing\n", sd->s_name); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 847 | |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 848 | next = NULL; |
| 849 | do { |
| 850 | pos = next; |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 851 | next = sysfs_next_descendant_post(pos, sd); |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 852 | if (pos) |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 853 | sysfs_remove_one(acxt, pos); |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 854 | } while (next); |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 855 | } |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 856 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 857 | /** |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 858 | * kernfs_remove - remove a sysfs_dirent recursively |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 859 | * @sd: the sysfs_dirent to remove |
| 860 | * |
| 861 | * Remove @sd along with all its subdirectories and files. |
| 862 | */ |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 863 | void kernfs_remove(struct sysfs_dirent *sd) |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 864 | { |
| 865 | struct sysfs_addrm_cxt acxt; |
| 866 | |
| 867 | sysfs_addrm_start(&acxt); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 868 | __kernfs_remove(&acxt, sd); |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 869 | sysfs_addrm_finish(&acxt); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /** |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 873 | * kernfs_remove_by_name_ns - find a sysfs_dirent by name and remove it |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 874 | * @dir_sd: parent of the target |
| 875 | * @name: name of the sysfs_dirent to remove |
| 876 | * @ns: namespace tag of the sysfs_dirent to remove |
| 877 | * |
| 878 | * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove |
| 879 | * it. Returns 0 on success, -ENOENT if such entry doesn't exist. |
| 880 | */ |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 881 | int kernfs_remove_by_name_ns(struct sysfs_dirent *dir_sd, const char *name, |
| 882 | const void *ns) |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 883 | { |
| 884 | struct sysfs_addrm_cxt acxt; |
| 885 | struct sysfs_dirent *sd; |
| 886 | |
| 887 | if (!dir_sd) { |
| 888 | WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n", |
| 889 | name); |
| 890 | return -ENOENT; |
| 891 | } |
| 892 | |
| 893 | sysfs_addrm_start(&acxt); |
| 894 | |
| 895 | sd = sysfs_find_dirent(dir_sd, name, ns); |
| 896 | if (sd) |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 897 | __kernfs_remove(&acxt, sd); |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 898 | |
| 899 | sysfs_addrm_finish(&acxt); |
| 900 | |
| 901 | if (sd) |
| 902 | return 0; |
| 903 | else |
| 904 | return -ENOENT; |
| 905 | } |
| 906 | |
| 907 | /** |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 908 | * sysfs_remove_dir - remove an object's directory. |
| 909 | * @kobj: object. |
| 910 | * |
| 911 | * The only thing special about this is that we remove any files in |
| 912 | * the directory before we remove the directory, and we've inlined |
| 913 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 914 | */ |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 915 | void sysfs_remove_dir(struct kobject *kobj) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 916 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 917 | struct sysfs_dirent *sd = kobj->sd; |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 918 | |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 919 | /* |
| 920 | * In general, kboject owner is responsible for ensuring removal |
| 921 | * doesn't race with other operations and sysfs doesn't provide any |
| 922 | * protection; however, when @kobj is used as a symlink target, the |
| 923 | * symlinking entity usually doesn't own @kobj and thus has no |
| 924 | * control over removal. @kobj->sd may be removed anytime and |
| 925 | * symlink code may end up dereferencing an already freed sd. |
| 926 | * |
| 927 | * sysfs_symlink_target_lock synchronizes @kobj->sd disassociation |
| 928 | * against symlink operations so that symlink code can safely |
| 929 | * dereference @kobj->sd. |
| 930 | */ |
| 931 | spin_lock(&sysfs_symlink_target_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 932 | kobj->sd = NULL; |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 933 | spin_unlock(&sysfs_symlink_target_lock); |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 934 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 935 | if (sd) { |
| 936 | WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); |
Tejun Heo | 879f40d | 2013-11-23 17:21:49 -0500 | [diff] [blame] | 937 | kernfs_remove(sd); |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 941 | /** |
| 942 | * kernfs_rename_ns - move and rename a kernfs_node |
| 943 | * @sd: target node |
| 944 | * @new_parent: new parent to put @sd under |
| 945 | * @new_name: new name |
| 946 | * @new_ns: new namespace tag |
| 947 | */ |
| 948 | int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, |
| 949 | const char *new_name, const void *new_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 951 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 953 | mutex_lock(&sysfs_mutex); |
Eric W. Biederman | 932ea2e | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 954 | |
Eric W. Biederman | 9918f9a | 2007-08-20 21:36:31 +0900 | [diff] [blame] | 955 | error = 0; |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 956 | if ((sd->s_parent == new_parent) && (sd->s_ns == new_ns) && |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 957 | (strcmp(sd->s_name, new_name) == 0)) |
Eric W. Biederman | 9918f9a | 2007-08-20 21:36:31 +0900 | [diff] [blame] | 958 | goto out; /* nothing to rename */ |
| 959 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 960 | error = -EEXIST; |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 961 | if (sysfs_find_dirent(new_parent, new_name, new_ns)) |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 962 | goto out; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 963 | |
Eric W. Biederman | 0b4a4fe | 2008-07-03 18:05:28 -0700 | [diff] [blame] | 964 | /* rename sysfs_dirent */ |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 965 | if (strcmp(sd->s_name, new_name) != 0) { |
| 966 | error = -ENOMEM; |
Sasikantha babu | b4eafca | 2012-05-03 02:26:14 +0530 | [diff] [blame] | 967 | new_name = kstrdup(new_name, GFP_KERNEL); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 968 | if (!new_name) |
| 969 | goto out; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 970 | |
Sasikantha babu | b4eafca | 2012-05-03 02:26:14 +0530 | [diff] [blame] | 971 | kfree(sd->s_name); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 972 | sd->s_name = new_name; |
| 973 | } |
| 974 | |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 975 | /* |
| 976 | * Move to the appropriate place in the appropriate directories rbtree. |
| 977 | */ |
Eric W. Biederman | f6d90b4 | 2011-11-01 07:06:17 -0700 | [diff] [blame] | 978 | sysfs_unlink_sibling(sd); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 979 | sysfs_get(new_parent); |
Eric W. Biederman | f6d90b4 | 2011-11-01 07:06:17 -0700 | [diff] [blame] | 980 | sysfs_put(sd->s_parent); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 981 | sd->s_ns = new_ns; |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 982 | sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 983 | sd->s_parent = new_parent; |
Eric W. Biederman | f6d90b4 | 2011-11-01 07:06:17 -0700 | [diff] [blame] | 984 | sysfs_link_sibling(sd); |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 985 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 986 | error = 0; |
Eric W. Biederman | 9918f9a | 2007-08-20 21:36:31 +0900 | [diff] [blame] | 987 | out: |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 988 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | return error; |
| 990 | } |
| 991 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 992 | int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, |
| 993 | const void *new_ns) |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 994 | { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 995 | struct sysfs_dirent *parent_sd = kobj->sd->s_parent; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 996 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 997 | return kernfs_rename_ns(kobj->sd, parent_sd, new_name, new_ns); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 1000 | int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, |
| 1001 | const void *new_ns) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1002 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 1003 | struct sysfs_dirent *sd = kobj->sd; |
| 1004 | struct sysfs_dirent *new_parent_sd; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1005 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 1006 | BUG_ON(!sd->s_parent); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 1007 | new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? |
Cornelia Huck | a6a8357 | 2009-10-06 15:33:35 +0200 | [diff] [blame] | 1008 | new_parent_kobj->sd : &sysfs_root; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1009 | |
Tejun Heo | 890ece1 | 2013-11-23 17:21:51 -0500 | [diff] [blame] | 1010 | return kernfs_rename_ns(sd, new_parent_sd, sd->s_name, new_ns); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 1013 | /** |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 1014 | * kernfs_enable_ns - enable namespace under a directory |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 1015 | * @sd: directory of interest, should be empty |
| 1016 | * |
| 1017 | * This is to be called right after @sd is created to enable namespace |
| 1018 | * under it. All children of @sd must have non-NULL namespace tags and |
| 1019 | * only the ones which match the super_block's tag will be visible. |
| 1020 | */ |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame^] | 1021 | void kernfs_enable_ns(struct sysfs_dirent *sd) |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 1022 | { |
| 1023 | WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); |
| 1024 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd->s_dir.children)); |
| 1025 | sd->s_flags |= SYSFS_FLAG_NS; |
| 1026 | } |
| 1027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | /* Relationship between s_mode and the DT_xxx types */ |
| 1029 | static inline unsigned char dt_type(struct sysfs_dirent *sd) |
| 1030 | { |
| 1031 | return (sd->s_mode >> 12) & 15; |
| 1032 | } |
| 1033 | |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1034 | static int sysfs_dir_release(struct inode *inode, struct file *filp) |
| 1035 | { |
| 1036 | sysfs_put(filp->private_data); |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1040 | static struct sysfs_dirent *sysfs_dir_pos(const void *ns, |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1041 | struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos) |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1042 | { |
| 1043 | if (pos) { |
| 1044 | int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && |
| 1045 | pos->s_parent == parent_sd && |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1046 | hash == pos->s_hash; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1047 | sysfs_put(pos); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1048 | if (!valid) |
| 1049 | pos = NULL; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1050 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1051 | if (!pos && (hash > 1) && (hash < INT_MAX)) { |
| 1052 | struct rb_node *node = parent_sd->s_dir.children.rb_node; |
| 1053 | while (node) { |
| 1054 | pos = to_sysfs_dirent(node); |
| 1055 | |
| 1056 | if (hash < pos->s_hash) |
| 1057 | node = node->rb_left; |
| 1058 | else if (hash > pos->s_hash) |
| 1059 | node = node->rb_right; |
| 1060 | else |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1061 | break; |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1062 | } |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1063 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1064 | /* Skip over entries in the wrong namespace */ |
Eric W. Biederman | b9e2780d | 2011-10-25 05:38:41 -0700 | [diff] [blame] | 1065 | while (pos && pos->s_ns != ns) { |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1066 | struct rb_node *node = rb_next(&pos->s_rb); |
| 1067 | if (!node) |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1068 | pos = NULL; |
| 1069 | else |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1070 | pos = to_sysfs_dirent(node); |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1071 | } |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1072 | return pos; |
| 1073 | } |
| 1074 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1075 | static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns, |
| 1076 | struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos) |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1077 | { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1078 | pos = sysfs_dir_pos(ns, parent_sd, ino, pos); |
Greg Kroah-Hartman | 37814ee | 2013-08-21 16:36:02 -0700 | [diff] [blame] | 1079 | if (pos) |
| 1080 | do { |
| 1081 | struct rb_node *node = rb_next(&pos->s_rb); |
| 1082 | if (!node) |
| 1083 | pos = NULL; |
| 1084 | else |
| 1085 | pos = to_sysfs_dirent(node); |
| 1086 | } while (pos && pos->s_ns != ns); |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1087 | return pos; |
| 1088 | } |
| 1089 | |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1090 | static int sysfs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1092 | struct dentry *dentry = file->f_path.dentry; |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 1093 | struct sysfs_dirent *parent_sd = dentry->d_fsdata; |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1094 | struct sysfs_dirent *pos = file->private_data; |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 1095 | const void *ns = NULL; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1096 | |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1097 | if (!dir_emit_dots(file, ctx)) |
| 1098 | return 0; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1099 | mutex_lock(&sysfs_mutex); |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 1100 | |
| 1101 | if (parent_sd->s_flags & SYSFS_FLAG_NS) |
| 1102 | ns = sysfs_info(dentry->d_sb)->ns; |
| 1103 | |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1104 | for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos); |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1105 | pos; |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1106 | pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) { |
| 1107 | const char *name = pos->s_name; |
| 1108 | unsigned int type = dt_type(pos); |
| 1109 | int len = strlen(name); |
| 1110 | ino_t ino = pos->s_ino; |
| 1111 | ctx->pos = pos->s_hash; |
| 1112 | file->private_data = sysfs_get(pos); |
Eric W. Biederman | 3efa65b | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 1113 | |
Eric W. Biederman | 3efa65b | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 1114 | mutex_unlock(&sysfs_mutex); |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1115 | if (!dir_emit(ctx, name, len, ino, type)) |
| 1116 | return 0; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1117 | mutex_lock(&sysfs_mutex); |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1118 | } |
| 1119 | mutex_unlock(&sysfs_mutex); |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1120 | file->private_data = NULL; |
| 1121 | ctx->pos = INT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
Ming Lei | 991f76f | 2013-03-20 23:25:24 +0800 | [diff] [blame] | 1125 | static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence) |
| 1126 | { |
| 1127 | struct inode *inode = file_inode(file); |
| 1128 | loff_t ret; |
| 1129 | |
| 1130 | mutex_lock(&inode->i_mutex); |
| 1131 | ret = generic_file_llseek(file, offset, whence); |
| 1132 | mutex_unlock(&inode->i_mutex); |
| 1133 | |
| 1134 | return ret; |
| 1135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1137 | const struct file_operations sysfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | .read = generic_read_dir, |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1139 | .iterate = sysfs_readdir, |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1140 | .release = sysfs_dir_release, |
Ming Lei | 991f76f | 2013-03-20 23:25:24 +0800 | [diff] [blame] | 1141 | .llseek = sysfs_dir_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | }; |