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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kobject.h> |
Robert P. J. Day | c6f8773 | 2008-03-13 22:41:52 -0400 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "sysfs.h" |
| 19 | |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 20 | DEFINE_SPINLOCK(sysfs_symlink_target_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Cornelia Huck | 36ce6da | 2008-06-10 11:09:08 +0200 | [diff] [blame] | 22 | /** |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 23 | * sysfs_pathname - return full path to sysfs dirent |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 24 | * @kn: kernfs_node whose path we want |
Geert Uytterhoeven | 66081a7 | 2012-09-29 22:23:19 +0200 | [diff] [blame] | 25 | * @path: caller allocated buffer of size PATH_MAX |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 26 | * |
| 27 | * Gives the name "/" to the sysfs_root entry; any path returned |
| 28 | * is relative to wherever sysfs is mounted. |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 29 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 30 | static char *sysfs_pathname(struct kernfs_node *kn, char *path) |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 31 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 32 | if (kn->parent) { |
| 33 | sysfs_pathname(kn->parent, path); |
Geert Uytterhoeven | 66081a7 | 2012-09-29 22:23:19 +0200 | [diff] [blame] | 34 | strlcat(path, "/", PATH_MAX); |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 35 | } |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 36 | strlcat(path, kn->name, PATH_MAX); |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 37 | return path; |
| 38 | } |
| 39 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 40 | void sysfs_warn_dup(struct kernfs_node *parent, const char *name) |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 41 | { |
| 42 | char *path; |
| 43 | |
| 44 | path = kzalloc(PATH_MAX, GFP_KERNEL); |
| 45 | if (path) { |
| 46 | sysfs_pathname(parent, path); |
| 47 | strlcat(path, "/", PATH_MAX); |
| 48 | strlcat(path, name, PATH_MAX); |
| 49 | } |
| 50 | |
| 51 | WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n", |
| 52 | path ? path : name); |
| 53 | |
| 54 | kfree(path); |
| 55 | } |
| 56 | |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 57 | /** |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 58 | * sysfs_create_dir_ns - create a directory for an object with a namespace tag |
| 59 | * @kobj: object we're creating directory for |
| 60 | * @ns: the namespace tag to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | */ |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 62 | int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 64 | struct kernfs_node *parent, *kn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | BUG_ON(!kobj); |
| 67 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 68 | if (kobj->parent) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 69 | parent = kobj->parent->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | else |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 71 | parent = sysfs_root_kn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 73 | if (!parent) |
Dan Williams | 3a19888 | 2012-04-06 13:41:06 -0700 | [diff] [blame] | 74 | return -ENOENT; |
| 75 | |
Tejun Heo | bb8b9d09 | 2013-12-11 16:02:55 -0500 | [diff] [blame] | 76 | kn = kernfs_create_dir_ns(parent, kobject_name(kobj), |
| 77 | S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 78 | if (IS_ERR(kn)) { |
| 79 | if (PTR_ERR(kn) == -EEXIST) |
| 80 | sysfs_warn_dup(parent, kobject_name(kobj)); |
| 81 | return PTR_ERR(kn); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 84 | kobj->sd = kn; |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 85 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 88 | /** |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 89 | * sysfs_remove_dir - remove an object's directory. |
| 90 | * @kobj: object. |
| 91 | * |
| 92 | * The only thing special about this is that we remove any files in |
| 93 | * the directory before we remove the directory, and we've inlined |
| 94 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 95 | */ |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 96 | void sysfs_remove_dir(struct kobject *kobj) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 97 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 98 | struct kernfs_node *kn = kobj->sd; |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 99 | |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 100 | /* |
| 101 | * In general, kboject owner is responsible for ensuring removal |
| 102 | * doesn't race with other operations and sysfs doesn't provide any |
| 103 | * protection; however, when @kobj is used as a symlink target, the |
| 104 | * symlinking entity usually doesn't own @kobj and thus has no |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 105 | * control over removal. @kobj->sd may be removed anytime |
| 106 | * and symlink code may end up dereferencing an already freed node. |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 107 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 108 | * sysfs_symlink_target_lock synchronizes @kobj->sd |
| 109 | * disassociation against symlink operations so that symlink code |
| 110 | * can safely dereference @kobj->sd. |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 111 | */ |
| 112 | spin_lock(&sysfs_symlink_target_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 113 | kobj->sd = NULL; |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 114 | spin_unlock(&sysfs_symlink_target_lock); |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 115 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 116 | if (kn) { |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 117 | WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 118 | kernfs_remove(kn); |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 122 | int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, |
| 123 | const void *new_ns) |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 124 | { |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 125 | struct kernfs_node *parent = kobj->sd->parent; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 126 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 127 | return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns); |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 130 | int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, |
| 131 | const void *new_ns) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 132 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 133 | struct kernfs_node *kn = kobj->sd; |
| 134 | struct kernfs_node *new_parent; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 135 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 136 | BUG_ON(!kn->parent); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 137 | new_parent = new_parent_kobj && new_parent_kobj->sd ? |
| 138 | new_parent_kobj->sd : sysfs_root_kn; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 139 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 140 | return kernfs_rename_ns(kn, new_parent, kn->name, new_ns); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 141 | } |