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