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); |
Roel Kluin | f7a75f0 | 2007-10-16 23:30:25 -0700 | [diff] [blame] | 29 | DEFINE_SPINLOCK(sysfs_assoc_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); |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 114 | |
| 115 | /* if @sd has ns tag, mark the parent to enable ns filtering */ |
| 116 | if (sd->s_ns) |
| 117 | sd->s_parent->s_flags |= SYSFS_FLAG_HAS_NS; |
| 118 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 119 | return 0; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 123 | * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 124 | * @sd: sysfs_dirent of interest |
| 125 | * |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 126 | * Unlink @sd from its sibling rbtree which starts from |
Tejun Heo | bc747f3 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 127 | * sd->s_parent->s_dir.children. |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 128 | * |
| 129 | * Locking: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 130 | * mutex_lock(sysfs_mutex) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 131 | */ |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 132 | static void sysfs_unlink_sibling(struct sysfs_dirent *sd) |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 133 | { |
Greg Kroah-Hartman | 54d20f0 | 2012-03-08 13:03:10 -0800 | [diff] [blame] | 134 | if (sysfs_type(sd) == SYSFS_DIR) |
| 135 | sd->s_parent->s_dir.subdirs--; |
| 136 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 137 | rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children); |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * Either all or none of the children have tags. Clearing HAS_NS |
| 141 | * when there's no child left is enough to keep the flag synced. |
| 142 | */ |
| 143 | if (RB_EMPTY_ROOT(&sd->s_parent->s_dir.children)) |
| 144 | sd->s_parent->s_flags &= ~SYSFS_FLAG_HAS_NS; |
Tejun Heo | 0c73f18 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 148 | * sysfs_get_active - get an active reference to sysfs_dirent |
| 149 | * @sd: sysfs_dirent to get an active reference to |
| 150 | * |
| 151 | * Get an active reference of @sd. This function is noop if @sd |
| 152 | * is NULL. |
| 153 | * |
| 154 | * RETURNS: |
| 155 | * Pointer to @sd on success, NULL on failure. |
| 156 | */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 157 | struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 158 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 159 | if (unlikely(!sd)) |
| 160 | return NULL; |
| 161 | |
Maarten Lankhorst | 3db3c62 | 2013-03-08 16:07:27 +0100 | [diff] [blame] | 162 | if (!atomic_inc_unless_negative(&sd->s_active)) |
| 163 | return NULL; |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 164 | |
Tejun Heo | 785a162 | 2013-10-14 09:27:11 -0400 | [diff] [blame] | 165 | if (likely(!sysfs_ignore_lockdep(sd))) |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 166 | rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_); |
| 167 | return sd; |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * sysfs_put_active - put an active reference to sysfs_dirent |
| 172 | * @sd: sysfs_dirent to put an active reference to |
| 173 | * |
| 174 | * Put an active reference to @sd. This function is noop if @sd |
| 175 | * is NULL. |
| 176 | */ |
Eric W. Biederman | e72ceb8 | 2010-02-11 15:18:38 -0800 | [diff] [blame] | 177 | void sysfs_put_active(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 178 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 179 | int v; |
| 180 | |
| 181 | if (unlikely(!sd)) |
| 182 | return; |
| 183 | |
Tejun Heo | 785a162 | 2013-10-14 09:27:11 -0400 | [diff] [blame] | 184 | if (likely(!sysfs_ignore_lockdep(sd))) |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 185 | rwsem_release(&sd->dep_map, 1, _RET_IP_); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 186 | v = atomic_dec_return(&sd->s_active); |
| 187 | if (likely(v != SD_DEACTIVATED_BIAS)) |
| 188 | return; |
| 189 | |
| 190 | /* atomic_dec_return() is a mb(), we'll always see the updated |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 191 | * sd->u.completion. |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 192 | */ |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 193 | complete(sd->u.completion); |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 197 | * sysfs_deactivate - deactivate sysfs_dirent |
| 198 | * @sd: sysfs_dirent to deactivate |
| 199 | * |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 200 | * Deny new active references and drain existing ones. |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 201 | */ |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 202 | static void sysfs_deactivate(struct sysfs_dirent *sd) |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 203 | { |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 204 | DECLARE_COMPLETION_ONSTACK(wait); |
| 205 | int v; |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 206 | |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 207 | BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED)); |
Eric W. Biederman | a2db684 | 2010-02-11 15:20:00 -0800 | [diff] [blame] | 208 | |
| 209 | if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF)) |
| 210 | return; |
| 211 | |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 212 | sd->u.completion = (void *)&wait; |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 213 | |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 214 | rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 215 | /* atomic_add_return() is a mb(), put_active() will always see |
Mikulas Patocka | 58f2a4c | 2011-07-21 20:01:12 -0400 | [diff] [blame] | 216 | * the updated sd->u.completion. |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 217 | */ |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 218 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); |
| 219 | |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 220 | if (v != SD_DEACTIVATED_BIAS) { |
| 221 | lock_contended(&sd->dep_map, _RET_IP_); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 222 | wait_for_completion(&wait); |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 223 | } |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 224 | |
Eric W. Biederman | 846f997 | 2010-01-02 13:37:12 -0800 | [diff] [blame] | 225 | lock_acquired(&sd->dep_map, _RET_IP_); |
| 226 | rwsem_release(&sd->dep_map, 1, _RET_IP_); |
Tejun Heo | b6b4a43 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Eric W. Biederman | cafa6b5 | 2011-12-18 20:08:16 -0800 | [diff] [blame] | 229 | static int sysfs_alloc_ino(unsigned int *pino) |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 230 | { |
| 231 | int ino, rc; |
| 232 | |
| 233 | retry: |
| 234 | spin_lock(&sysfs_ino_lock); |
| 235 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); |
| 236 | spin_unlock(&sysfs_ino_lock); |
| 237 | |
| 238 | if (rc == -EAGAIN) { |
| 239 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) |
| 240 | goto retry; |
| 241 | rc = -ENOMEM; |
| 242 | } |
| 243 | |
| 244 | *pino = ino; |
| 245 | return rc; |
| 246 | } |
| 247 | |
Eric W. Biederman | cafa6b5 | 2011-12-18 20:08:16 -0800 | [diff] [blame] | 248 | static void sysfs_free_ino(unsigned int ino) |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 249 | { |
| 250 | spin_lock(&sysfs_ino_lock); |
| 251 | ida_remove(&sysfs_ino_ida, ino); |
| 252 | spin_unlock(&sysfs_ino_lock); |
| 253 | } |
| 254 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 255 | void release_sysfs_dirent(struct sysfs_dirent *sd) |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 256 | { |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 257 | struct sysfs_dirent *parent_sd; |
| 258 | |
| 259 | repeat: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 260 | /* Moving/renaming is always done while holding reference. |
| 261 | * sd->s_parent won't change beneath us. |
| 262 | */ |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 263 | parent_sd = sd->s_parent; |
| 264 | |
Ming Lei | bb2b005 | 2013-04-04 22:22:37 +0800 | [diff] [blame] | 265 | WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED), |
| 266 | "sysfs: free using entry: %s/%s\n", |
| 267 | parent_sd ? parent_sd->s_name : "", sd->s_name); |
| 268 | |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 269 | if (sysfs_type(sd) == SYSFS_KOBJ_LINK) |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 270 | sysfs_put(sd->s_symlink.target_sd); |
Tejun Heo | b402d72 | 2007-06-14 04:27:21 +0900 | [diff] [blame] | 271 | if (sysfs_type(sd) & SYSFS_COPY_NAME) |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 272 | kfree(sd->s_name); |
Eric W. Biederman | 4c3da22 | 2009-11-04 02:50:06 -0800 | [diff] [blame] | 273 | if (sd->s_iattr && sd->s_iattr->ia_secdata) |
| 274 | security_release_secctx(sd->s_iattr->ia_secdata, |
| 275 | sd->s_iattr->ia_secdata_len); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 276 | kfree(sd->s_iattr); |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 277 | sysfs_free_ino(sd->s_ino); |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 278 | kmem_cache_free(sysfs_dir_cachep, sd); |
Tejun Heo | 13b3086 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 279 | |
| 280 | sd = parent_sd; |
| 281 | if (sd && atomic_dec_and_test(&sd->s_count)) |
| 282 | goto repeat; |
Tejun Heo | fa7f912 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 285 | static int sysfs_dentry_delete(const struct dentry *dentry) |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 286 | { |
| 287 | struct sysfs_dirent *sd = dentry->d_fsdata; |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 288 | return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED)); |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 291 | static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 292 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 293 | struct sysfs_dirent *sd; |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 294 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 295 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 296 | return -ECHILD; |
| 297 | |
| 298 | sd = dentry->d_fsdata; |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 299 | mutex_lock(&sysfs_mutex); |
| 300 | |
| 301 | /* The sysfs dirent has been deleted */ |
| 302 | if (sd->s_flags & SYSFS_FLAG_REMOVED) |
| 303 | goto out_bad; |
| 304 | |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 305 | /* The sysfs dirent has been moved? */ |
| 306 | if (dentry->d_parent->d_fsdata != sd->s_parent) |
| 307 | goto out_bad; |
| 308 | |
| 309 | /* The sysfs dirent has been renamed */ |
| 310 | if (strcmp(dentry->d_name.name, sd->s_name) != 0) |
| 311 | goto out_bad; |
| 312 | |
Glauber Costa | e5bcac6 | 2012-07-06 13:09:07 +0400 | [diff] [blame] | 313 | /* The sysfs dirent has been moved to a different namespace */ |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 314 | if (sd->s_ns && sd->s_ns != sysfs_info(dentry->d_sb)->ns) |
| 315 | goto out_bad; |
Glauber Costa | e5bcac6 | 2012-07-06 13:09:07 +0400 | [diff] [blame] | 316 | |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 317 | mutex_unlock(&sysfs_mutex); |
| 318 | out_valid: |
| 319 | return 1; |
| 320 | out_bad: |
| 321 | /* Remove the dentry from the dcache hashes. |
| 322 | * If this is a deleted dentry we use d_drop instead of d_delete |
| 323 | * so sysfs doesn't need to cope with negative dentries. |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 324 | * |
| 325 | * If this is a dentry that has simply been renamed we |
| 326 | * use d_drop to remove it from the dcache lookup on its |
| 327 | * old parent. If this dentry persists later when a lookup |
| 328 | * is performed at its new name the dentry will be readded |
| 329 | * to the dcache hashes. |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 330 | */ |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 331 | mutex_unlock(&sysfs_mutex); |
Miklos Szeredi | 6497d16 | 2013-09-05 11:44:41 +0200 | [diff] [blame] | 332 | |
| 333 | /* If we have submounts we must allow the vfs caches |
| 334 | * to lie about the state of the filesystem to prevent |
| 335 | * leaks and other nasty things. |
| 336 | */ |
| 337 | if (check_submounts_and_drop(dentry) != 0) |
| 338 | goto out_valid; |
| 339 | |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 343 | static void sysfs_dentry_release(struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 345 | sysfs_put(dentry->d_fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 348 | const struct dentry_operations sysfs_dentry_ops = { |
Eric W. Biederman | e8f077c | 2009-11-07 23:27:01 -0800 | [diff] [blame] | 349 | .d_revalidate = sysfs_dentry_revalidate, |
| 350 | .d_delete = sysfs_dentry_delete, |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 351 | .d_release = sysfs_dentry_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | }; |
| 353 | |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 354 | 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] | 355 | { |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 356 | char *dup_name = NULL; |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 357 | struct sysfs_dirent *sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 358 | |
| 359 | if (type & SYSFS_COPY_NAME) { |
| 360 | name = dup_name = kstrdup(name, GFP_KERNEL); |
| 361 | if (!name) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 362 | return NULL; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 365 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (!sd) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 367 | goto err_out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 369 | if (sysfs_alloc_ino(&sd->s_ino)) |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 370 | goto err_out2; |
Tejun Heo | 2b611bb | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | atomic_set(&sd->s_count, 1); |
Tejun Heo | 8619f97 | 2007-06-14 03:45:18 +0900 | [diff] [blame] | 373 | atomic_set(&sd->s_active, 0); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 374 | |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 375 | sd->s_name = name; |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 376 | sd->s_mode = mode; |
Ming Lei | bb2b005 | 2013-04-04 22:22:37 +0800 | [diff] [blame] | 377 | sd->s_flags = type | SYSFS_FLAG_REMOVED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | return sd; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 380 | |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 381 | err_out2: |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 382 | kmem_cache_free(sysfs_dir_cachep, sd); |
Akinobu Mita | 01da242 | 2007-07-14 11:03:35 +0900 | [diff] [blame] | 383 | err_out1: |
| 384 | kfree(dup_name); |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 385 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 388 | /** |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 389 | * sysfs_addrm_start - prepare for sysfs_dirent add/remove |
| 390 | * @acxt: pointer to sysfs_addrm_cxt to be used |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 391 | * |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 392 | * This function is called when the caller is about to add or remove |
| 393 | * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used |
| 394 | * to keep and pass context to other addrm functions. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 395 | * |
| 396 | * LOCKING: |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 397 | * Kernel thread context (may sleep). sysfs_mutex is locked on |
Eric W. Biederman | a16bbc3 | 2009-11-20 16:08:55 -0800 | [diff] [blame] | 398 | * return. |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 399 | */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 400 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt) |
| 401 | __acquires(sysfs_mutex) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 402 | { |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 403 | memset(acxt, 0, sizeof(*acxt)); |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 404 | |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 405 | mutex_lock(&sysfs_mutex); |
Tejun Heo | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 409 | * __sysfs_add_one - add sysfs_dirent to parent without warning |
| 410 | * @acxt: addrm context to use |
| 411 | * @sd: sysfs_dirent to be added |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 412 | * @parent_sd: the parent sysfs_dirent to add @sd to |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 413 | * |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 414 | * Get @parent_sd and set @sd->s_parent to it and increment nlink of |
| 415 | * the parent inode if @sd is a directory and link into the children |
| 416 | * list of the parent. |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 417 | * |
| 418 | * This function should be called between calls to |
| 419 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be |
| 420 | * passed the same @acxt as passed to sysfs_addrm_start(). |
| 421 | * |
| 422 | * LOCKING: |
| 423 | * Determined by sysfs_addrm_start(). |
| 424 | * |
| 425 | * RETURNS: |
| 426 | * 0 on success, -EEXIST if entry with the given name already |
| 427 | * exists. |
| 428 | */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 429 | int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, |
| 430 | struct sysfs_dirent *parent_sd) |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 431 | { |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 432 | struct sysfs_inode_attrs *ps_iattr; |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 433 | int ret; |
Eric W. Biederman | 6b0bfe9 | 2009-11-20 16:08:51 -0800 | [diff] [blame] | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 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 | fb6896da | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 586 | |
Tejun Heo | fb6896da | 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 | fb6896da | 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; |
| 612 | unsigned int hash; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 613 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 614 | hash = sysfs_name_hash(name, ns); |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 615 | while (node) { |
| 616 | struct sysfs_dirent *sd; |
| 617 | int result; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 618 | |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 619 | sd = to_sysfs_dirent(node); |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 620 | result = sysfs_name_compare(hash, name, ns, sd); |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 621 | if (result < 0) |
| 622 | node = node->rb_left; |
| 623 | else if (result > 0) |
| 624 | node = node->rb_right; |
| 625 | else |
| 626 | return sd; |
Mikulas Patocka | 4f72c0c | 2011-07-25 17:55:57 -0400 | [diff] [blame] | 627 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 628 | return NULL; |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 629 | } |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 630 | |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 631 | /** |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 632 | * 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] | 633 | * @parent_sd: sysfs_dirent to search under |
| 634 | * @name: name to look for |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 635 | * @ns: the namespace tag to use |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 636 | * |
| 637 | * Look for sysfs_dirent with name @name under @parent_sd and get |
| 638 | * it if found. |
| 639 | * |
| 640 | * LOCKING: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 641 | * Kernel thread context (may sleep). Grabs sysfs_mutex. |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 642 | * |
| 643 | * RETURNS: |
| 644 | * Pointer to sysfs_dirent if found, NULL if not. |
| 645 | */ |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 646 | struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, |
| 647 | const unsigned char *name, |
| 648 | const void *ns) |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 649 | { |
| 650 | struct sysfs_dirent *sd; |
| 651 | |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 652 | mutex_lock(&sysfs_mutex); |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 653 | sd = sysfs_find_dirent(parent_sd, name, ns); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 654 | sysfs_get(sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 655 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | f0b0af4 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 656 | |
| 657 | return sd; |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 658 | } |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns); |
Maneesh Soni | c516865 | 2006-03-09 19:40:14 +0530 | [diff] [blame] | 660 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 661 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 662 | const char *name, const void *ns, |
| 663 | struct sysfs_dirent **p_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 665 | umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 666 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 667 | struct sysfs_dirent *sd; |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 668 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 670 | /* allocate */ |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 671 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
Tejun Heo | a26cd72 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 672 | if (!sd) |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 673 | return -ENOMEM; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 674 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 675 | sd->s_ns = ns; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 676 | sd->s_dir.kobj = kobj; |
Tejun Heo | dfeb9fb | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 677 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 678 | /* link in */ |
Tejun Heo | d69ac5a | 2013-09-18 17:15:35 -0400 | [diff] [blame] | 679 | sysfs_addrm_start(&acxt); |
| 680 | rc = sysfs_add_one(&acxt, sd, parent_sd); |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 681 | sysfs_addrm_finish(&acxt); |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 682 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 683 | if (rc == 0) |
| 684 | *p_sd = sd; |
| 685 | else |
Tejun Heo | 967e35d | 2007-07-18 16:38:11 +0900 | [diff] [blame] | 686 | sysfs_put(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 687 | |
Tejun Heo | 23dc279 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 688 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 691 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
| 692 | struct sysfs_dirent **p_sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 694 | return create_dir(kobj, kobj->sd, name, NULL, p_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 698 | * sysfs_create_dir_ns - create a directory for an object with a namespace tag |
| 699 | * @kobj: object we're creating directory for |
| 700 | * @ns: the namespace tag to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | */ |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 702 | int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 704 | struct sysfs_dirent *parent_sd, *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | int error = 0; |
| 706 | |
| 707 | BUG_ON(!kobj); |
| 708 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 709 | if (kobj->parent) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 710 | parent_sd = kobj->parent->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | else |
Eric W. Biederman | 7d0c7d6 | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 712 | parent_sd = &sysfs_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Dan Williams | 3a19888 | 2012-04-06 13:41:06 -0700 | [diff] [blame] | 714 | if (!parent_sd) |
| 715 | return -ENOENT; |
| 716 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 717 | error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | if (!error) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 719 | kobj->sd = sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | return error; |
| 721 | } |
| 722 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 723 | static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry, |
| 724 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 726 | struct dentry *ret = NULL; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 727 | struct dentry *parent = dentry->d_parent; |
| 728 | struct sysfs_dirent *parent_sd = parent->d_fsdata; |
Tejun Heo | a7a0475 | 2007-08-02 21:38:02 +0900 | [diff] [blame] | 729 | struct sysfs_dirent *sd; |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 730 | struct inode *inode; |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 731 | const void *ns = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 733 | mutex_lock(&sysfs_mutex); |
| 734 | |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 735 | if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS) |
| 736 | ns = sysfs_info(dir->i_sb)->ns; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 737 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 738 | sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 740 | /* no such entry */ |
Tejun Heo | e49452c | 2008-01-16 12:06:14 +0900 | [diff] [blame] | 741 | if (!sd) { |
| 742 | ret = ERR_PTR(-ENOENT); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 743 | goto out_unlock; |
Tejun Heo | e49452c | 2008-01-16 12:06:14 +0900 | [diff] [blame] | 744 | } |
Al Viro | 469796d | 2012-06-07 20:51:39 -0400 | [diff] [blame] | 745 | dentry->d_fsdata = sysfs_get(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 746 | |
| 747 | /* attach dentry and inode */ |
Eric W. Biederman | fac2622 | 2010-02-12 19:22:27 -0800 | [diff] [blame] | 748 | inode = sysfs_get_inode(dir->i_sb, sd); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 749 | if (!inode) { |
| 750 | ret = ERR_PTR(-ENOMEM); |
| 751 | goto out_unlock; |
| 752 | } |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 753 | |
Tejun Heo | d6b4fd2 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 754 | /* instantiate and hash dentry */ |
Al Viro | e77fb7c | 2012-06-07 20:56:54 -0400 | [diff] [blame] | 755 | ret = d_materialise_unique(dentry, inode); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 756 | out_unlock: |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 757 | mutex_unlock(&sysfs_mutex); |
Tejun Heo | 6cb5214 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 758 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 761 | const struct inode_operations sysfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | .lookup = sysfs_lookup, |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 763 | .permission = sysfs_permission, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 764 | .setattr = sysfs_setattr, |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 765 | .getattr = sysfs_getattr, |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 766 | .setxattr = sysfs_setxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 769 | static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos) |
| 770 | { |
| 771 | struct sysfs_dirent *last; |
| 772 | |
| 773 | while (true) { |
| 774 | struct rb_node *rbn; |
| 775 | |
| 776 | last = pos; |
| 777 | |
| 778 | if (sysfs_type(pos) != SYSFS_DIR) |
| 779 | break; |
| 780 | |
| 781 | rbn = rb_first(&pos->s_dir.children); |
| 782 | if (!rbn) |
| 783 | break; |
| 784 | |
| 785 | pos = to_sysfs_dirent(rbn); |
| 786 | } |
| 787 | |
| 788 | return last; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * sysfs_next_descendant_post - find the next descendant for post-order walk |
| 793 | * @pos: the current position (%NULL to initiate traversal) |
| 794 | * @root: sysfs_dirent whose descendants to walk |
| 795 | * |
| 796 | * Find the next descendant to visit for post-order traversal of @root's |
| 797 | * descendants. @root is included in the iteration and the last node to be |
| 798 | * visited. |
| 799 | */ |
| 800 | static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos, |
| 801 | struct sysfs_dirent *root) |
| 802 | { |
| 803 | struct rb_node *rbn; |
| 804 | |
| 805 | lockdep_assert_held(&sysfs_mutex); |
| 806 | |
| 807 | /* if first iteration, visit leftmost descendant which may be root */ |
| 808 | if (!pos) |
| 809 | return sysfs_leftmost_descendant(root); |
| 810 | |
| 811 | /* if we visited @root, we're done */ |
| 812 | if (pos == root) |
| 813 | return NULL; |
| 814 | |
| 815 | /* if there's an unvisited sibling, visit its leftmost descendant */ |
| 816 | rbn = rb_next(&pos->s_rb); |
| 817 | if (rbn) |
| 818 | return sysfs_leftmost_descendant(to_sysfs_dirent(rbn)); |
| 819 | |
| 820 | /* no sibling left, visit parent */ |
| 821 | return pos->s_parent; |
| 822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 824 | static void __sysfs_remove(struct sysfs_addrm_cxt *acxt, |
| 825 | struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 827 | struct sysfs_dirent *pos, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 829 | if (!sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return; |
| 831 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 832 | pr_debug("sysfs %s: removing\n", sd->s_name); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 833 | |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 834 | next = NULL; |
| 835 | do { |
| 836 | pos = next; |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 837 | next = sysfs_next_descendant_post(pos, sd); |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 838 | if (pos) |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 839 | sysfs_remove_one(acxt, pos); |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 840 | } while (next); |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 841 | } |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 842 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 843 | /** |
| 844 | * sysfs_remove - remove a sysfs_dirent recursively |
| 845 | * @sd: the sysfs_dirent to remove |
| 846 | * |
| 847 | * Remove @sd along with all its subdirectories and files. |
| 848 | */ |
| 849 | void sysfs_remove(struct sysfs_dirent *sd) |
| 850 | { |
| 851 | struct sysfs_addrm_cxt acxt; |
| 852 | |
| 853 | sysfs_addrm_start(&acxt); |
| 854 | __sysfs_remove(&acxt, sd); |
Tejun Heo | bcdde7e | 2013-09-18 17:15:37 -0400 | [diff] [blame] | 855 | sysfs_addrm_finish(&acxt); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /** |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 859 | * sysfs_hash_and_remove - find a sysfs_dirent by name and remove it |
| 860 | * @dir_sd: parent of the target |
| 861 | * @name: name of the sysfs_dirent to remove |
| 862 | * @ns: namespace tag of the sysfs_dirent to remove |
| 863 | * |
| 864 | * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove |
| 865 | * it. Returns 0 on success, -ENOENT if such entry doesn't exist. |
| 866 | */ |
| 867 | int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name, |
| 868 | const void *ns) |
| 869 | { |
| 870 | struct sysfs_addrm_cxt acxt; |
| 871 | struct sysfs_dirent *sd; |
| 872 | |
| 873 | if (!dir_sd) { |
| 874 | WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n", |
| 875 | name); |
| 876 | return -ENOENT; |
| 877 | } |
| 878 | |
| 879 | sysfs_addrm_start(&acxt); |
| 880 | |
| 881 | sd = sysfs_find_dirent(dir_sd, name, ns); |
| 882 | if (sd) |
| 883 | __sysfs_remove(&acxt, sd); |
| 884 | |
| 885 | sysfs_addrm_finish(&acxt); |
| 886 | |
| 887 | if (sd) |
| 888 | return 0; |
| 889 | else |
| 890 | return -ENOENT; |
| 891 | } |
| 892 | |
| 893 | /** |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 894 | * sysfs_remove_dir - remove an object's directory. |
| 895 | * @kobj: object. |
| 896 | * |
| 897 | * The only thing special about this is that we remove any files in |
| 898 | * the directory before we remove the directory, and we've inlined |
| 899 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 900 | */ |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 901 | void sysfs_remove_dir(struct kobject *kobj) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 902 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 903 | struct sysfs_dirent *sd = kobj->sd; |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 904 | |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 905 | spin_lock(&sysfs_assoc_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 906 | kobj->sd = NULL; |
Tejun Heo | 5f99532 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 907 | spin_unlock(&sysfs_assoc_lock); |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 908 | |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 909 | if (sd) { |
| 910 | WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); |
| 911 | sysfs_remove(sd); |
| 912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 915 | int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd, |
| 916 | const char *new_name, const void *new_ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 918 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 920 | mutex_lock(&sysfs_mutex); |
Eric W. Biederman | 932ea2e | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 921 | |
Eric W. Biederman | 9918f9a | 2007-08-20 21:36:31 +0900 | [diff] [blame] | 922 | error = 0; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 923 | if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) && |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 924 | (strcmp(sd->s_name, new_name) == 0)) |
Eric W. Biederman | 9918f9a | 2007-08-20 21:36:31 +0900 | [diff] [blame] | 925 | goto out; /* nothing to rename */ |
| 926 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 927 | error = -EEXIST; |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 928 | if (sysfs_find_dirent(new_parent_sd, new_name, new_ns)) |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 929 | goto out; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 930 | |
Eric W. Biederman | 0b4a4fe | 2008-07-03 18:05:28 -0700 | [diff] [blame] | 931 | /* rename sysfs_dirent */ |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 932 | if (strcmp(sd->s_name, new_name) != 0) { |
| 933 | error = -ENOMEM; |
Sasikantha babu | b4eafca | 2012-05-03 02:26:14 +0530 | [diff] [blame] | 934 | new_name = kstrdup(new_name, GFP_KERNEL); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 935 | if (!new_name) |
| 936 | goto out; |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 937 | |
Sasikantha babu | b4eafca | 2012-05-03 02:26:14 +0530 | [diff] [blame] | 938 | kfree(sd->s_name); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 939 | sd->s_name = new_name; |
| 940 | } |
| 941 | |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 942 | /* |
| 943 | * Move to the appropriate place in the appropriate directories rbtree. |
| 944 | */ |
Eric W. Biederman | f6d90b4 | 2011-11-01 07:06:17 -0700 | [diff] [blame] | 945 | sysfs_unlink_sibling(sd); |
| 946 | sysfs_get(new_parent_sd); |
| 947 | sysfs_put(sd->s_parent); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 948 | sd->s_ns = new_ns; |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 949 | sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); |
Eric W. Biederman | f6d90b4 | 2011-11-01 07:06:17 -0700 | [diff] [blame] | 950 | sd->s_parent = new_parent_sd; |
| 951 | sysfs_link_sibling(sd); |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 952 | |
Tejun Heo | 996b737 | 2007-06-14 03:45:14 +0900 | [diff] [blame] | 953 | error = 0; |
Eric W. Biederman | 9918f9a | 2007-08-20 21:36:31 +0900 | [diff] [blame] | 954 | out: |
Eric W. Biederman | 832b6af | 2009-11-20 16:08:56 -0800 | [diff] [blame] | 955 | mutex_unlock(&sysfs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | return error; |
| 957 | } |
| 958 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 959 | int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, |
| 960 | const void *new_ns) |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 961 | { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 962 | struct sysfs_dirent *parent_sd = kobj->sd->s_parent; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 963 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 964 | return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 967 | int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, |
| 968 | const void *new_ns) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 969 | { |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 970 | struct sysfs_dirent *sd = kobj->sd; |
| 971 | struct sysfs_dirent *new_parent_sd; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 972 | |
Tejun Heo | 5122503 | 2007-06-14 04:27:25 +0900 | [diff] [blame] | 973 | BUG_ON(!sd->s_parent); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 974 | new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? |
Cornelia Huck | a6a8357 | 2009-10-06 15:33:35 +0200 | [diff] [blame] | 975 | new_parent_kobj->sd : &sysfs_root; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 976 | |
Tejun Heo | cfec0bc | 2013-09-11 22:29:09 -0400 | [diff] [blame] | 977 | return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | /* Relationship between s_mode and the DT_xxx types */ |
| 981 | static inline unsigned char dt_type(struct sysfs_dirent *sd) |
| 982 | { |
| 983 | return (sd->s_mode >> 12) & 15; |
| 984 | } |
| 985 | |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 986 | static int sysfs_dir_release(struct inode *inode, struct file *filp) |
| 987 | { |
| 988 | sysfs_put(filp->private_data); |
| 989 | return 0; |
| 990 | } |
| 991 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 992 | static struct sysfs_dirent *sysfs_dir_pos(const void *ns, |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 993 | 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] | 994 | { |
| 995 | if (pos) { |
| 996 | int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && |
| 997 | pos->s_parent == parent_sd && |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 998 | hash == pos->s_hash; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 999 | sysfs_put(pos); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1000 | if (!valid) |
| 1001 | pos = NULL; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1002 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1003 | if (!pos && (hash > 1) && (hash < INT_MAX)) { |
| 1004 | struct rb_node *node = parent_sd->s_dir.children.rb_node; |
| 1005 | while (node) { |
| 1006 | pos = to_sysfs_dirent(node); |
| 1007 | |
| 1008 | if (hash < pos->s_hash) |
| 1009 | node = node->rb_left; |
| 1010 | else if (hash > pos->s_hash) |
| 1011 | node = node->rb_right; |
| 1012 | else |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1013 | break; |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1014 | } |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1015 | } |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1016 | /* Skip over entries in the wrong namespace */ |
Eric W. Biederman | b9e2780d | 2011-10-25 05:38:41 -0700 | [diff] [blame] | 1017 | while (pos && pos->s_ns != ns) { |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1018 | struct rb_node *node = rb_next(&pos->s_rb); |
| 1019 | if (!node) |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1020 | pos = NULL; |
| 1021 | else |
Eric W. Biederman | 4e4d6d8 | 2011-12-18 20:05:43 -0800 | [diff] [blame] | 1022 | pos = to_sysfs_dirent(node); |
Mikulas Patocka | a406f75 | 2011-07-25 17:57:03 -0400 | [diff] [blame] | 1023 | } |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1024 | return pos; |
| 1025 | } |
| 1026 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1027 | static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns, |
| 1028 | 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] | 1029 | { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1030 | pos = sysfs_dir_pos(ns, parent_sd, ino, pos); |
Greg Kroah-Hartman | 37814ee | 2013-08-21 16:36:02 -0700 | [diff] [blame] | 1031 | if (pos) |
| 1032 | do { |
| 1033 | struct rb_node *node = rb_next(&pos->s_rb); |
| 1034 | if (!node) |
| 1035 | pos = NULL; |
| 1036 | else |
| 1037 | pos = to_sysfs_dirent(node); |
| 1038 | } while (pos && pos->s_ns != ns); |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1039 | return pos; |
| 1040 | } |
| 1041 | |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1042 | static int sysfs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1044 | struct dentry *dentry = file->f_path.dentry; |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 1045 | struct sysfs_dirent *parent_sd = dentry->d_fsdata; |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1046 | struct sysfs_dirent *pos = file->private_data; |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 1047 | const void *ns = NULL; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1048 | |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1049 | if (!dir_emit_dots(file, ctx)) |
| 1050 | return 0; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1051 | mutex_lock(&sysfs_mutex); |
Tejun Heo | cb26a31 | 2013-09-11 22:29:07 -0400 | [diff] [blame] | 1052 | |
| 1053 | if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS) |
| 1054 | ns = sysfs_info(dentry->d_sb)->ns; |
| 1055 | |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1056 | for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos); |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1057 | pos; |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1058 | pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) { |
| 1059 | const char *name = pos->s_name; |
| 1060 | unsigned int type = dt_type(pos); |
| 1061 | int len = strlen(name); |
| 1062 | ino_t ino = pos->s_ino; |
| 1063 | ctx->pos = pos->s_hash; |
| 1064 | file->private_data = sysfs_get(pos); |
Eric W. Biederman | 3efa65b | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 1065 | |
Eric W. Biederman | 3efa65b | 2007-08-20 21:36:30 +0900 | [diff] [blame] | 1066 | mutex_unlock(&sysfs_mutex); |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1067 | if (!dir_emit(ctx, name, len, ino, type)) |
| 1068 | return 0; |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1069 | mutex_lock(&sysfs_mutex); |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1070 | } |
| 1071 | mutex_unlock(&sysfs_mutex); |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1072 | file->private_data = NULL; |
| 1073 | ctx->pos = INT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
Ming Lei | 991f76f | 2013-03-20 23:25:24 +0800 | [diff] [blame] | 1077 | static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence) |
| 1078 | { |
| 1079 | struct inode *inode = file_inode(file); |
| 1080 | loff_t ret; |
| 1081 | |
| 1082 | mutex_lock(&inode->i_mutex); |
| 1083 | ret = generic_file_llseek(file, offset, whence); |
| 1084 | mutex_unlock(&inode->i_mutex); |
| 1085 | |
| 1086 | return ret; |
| 1087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1089 | const struct file_operations sysfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | .read = generic_read_dir, |
Al Viro | d55fea8 | 2013-05-16 14:31:02 -0400 | [diff] [blame] | 1091 | .iterate = sysfs_readdir, |
Eric W. Biederman | 1e5289c | 2010-01-01 14:43:53 -0800 | [diff] [blame] | 1092 | .release = sysfs_dir_release, |
Ming Lei | 991f76f | 2013-03-20 23:25:24 +0800 | [diff] [blame] | 1093 | .llseek = sysfs_dir_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | }; |