Greg Kroah-Hartman | d9d16e1 | 2017-11-07 17:30:05 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * kobject.c - library routines for handling generic kernel objects |
| 4 | * |
| 5 | * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org> |
Greg Kroah-Hartman | f0e7e1b | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 6 | * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com> |
| 7 | * Copyright (c) 2006-2007 Novell Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This file is released under the GPLv2. |
| 10 | * |
| 11 | * |
| 12 | * Please see the file Documentation/kobject.txt for critical information |
| 13 | * about using the kobject interface. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kobject.h> |
| 17 | #include <linux/string.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 18 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/stat.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 20 | #include <linux/slab.h> |
Bjorn Helgaas | 89c86a6 | 2013-12-05 17:37:51 -0700 | [diff] [blame] | 21 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 23 | /** |
| 24 | * kobject_namespace - return @kobj's namespace tag |
| 25 | * @kobj: kobject in question |
| 26 | * |
| 27 | * Returns namespace tag of @kobj if its parent has namespace ops enabled |
| 28 | * and thus @kobj should have a namespace tag associated with it. Returns |
| 29 | * %NULL otherwise. |
| 30 | */ |
| 31 | const void *kobject_namespace(struct kobject *kobj) |
| 32 | { |
| 33 | const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj); |
| 34 | |
| 35 | if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE) |
| 36 | return NULL; |
| 37 | |
Linus Torvalds | a1212d2 | 2013-11-07 20:47:28 +0900 | [diff] [blame] | 38 | return kobj->ktype->namespace(kobj); |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 41 | /* |
| 42 | * populate_dir - populate directory with attributes. |
| 43 | * @kobj: object we're working on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 45 | * Most subsystems have a set of default attributes that are associated |
| 46 | * with an object that registers with them. This is a helper called during |
| 47 | * object registration that loops through the default attributes of the |
| 48 | * subsystem and creates attributes files for them in sysfs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 50 | static int populate_dir(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 52 | struct kobj_type *t = get_ktype(kobj); |
| 53 | struct attribute *attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int error = 0; |
| 55 | int i; |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (t && t->default_attrs) { |
| 58 | for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 59 | error = sysfs_create_file(kobj, attr); |
| 60 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | break; |
| 62 | } |
| 63 | } |
| 64 | return error; |
| 65 | } |
| 66 | |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 67 | static int create_dir(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 69 | const struct kobj_ns_type_operations *ops; |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 70 | int error; |
| 71 | |
| 72 | error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj)); |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 73 | if (error) |
| 74 | return error; |
| 75 | |
| 76 | error = populate_dir(kobj); |
| 77 | if (error) { |
| 78 | sysfs_remove_dir(kobj); |
| 79 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Tejun Heo | 26ea12d | 2013-09-18 17:15:36 -0400 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * @kobj->sd may be deleted by an ancestor going away. Hold an |
| 84 | * extra reference so that it stays until @kobj is gone. |
| 85 | */ |
| 86 | sysfs_get(kobj->sd); |
| 87 | |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 88 | /* |
| 89 | * If @kobj has ns_ops, its children need to be filtered based on |
| 90 | * their namespace tags. Enable namespace support on @kobj->sd. |
| 91 | */ |
| 92 | ops = kobj_child_ns_ops(kobj); |
| 93 | if (ops) { |
| 94 | BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE); |
| 95 | BUG_ON(ops->type >= KOBJ_NS_TYPES); |
| 96 | BUG_ON(!kobj_ns_type_registered(ops->type)); |
| 97 | |
Tejun Heo | fa4cd45 | 2014-02-07 13:32:07 -0500 | [diff] [blame] | 98 | sysfs_enable_ns(kobj->sd); |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static int get_kobj_path_length(struct kobject *kobj) |
| 105 | { |
| 106 | int length = 1; |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 107 | struct kobject *parent = kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 109 | /* walk up the ancestors until we hit the one pointing to the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | * root. |
| 111 | * Add 1 to strlen for leading '/' of each level. |
| 112 | */ |
| 113 | do { |
Chuck Ebbert | b365b3d | 2006-01-12 20:02:00 -0500 | [diff] [blame] | 114 | if (kobject_name(parent) == NULL) |
| 115 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | length += strlen(kobject_name(parent)) + 1; |
| 117 | parent = parent->parent; |
| 118 | } while (parent); |
| 119 | return length; |
| 120 | } |
| 121 | |
| 122 | static void fill_kobj_path(struct kobject *kobj, char *path, int length) |
| 123 | { |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 124 | struct kobject *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | --length; |
| 127 | for (parent = kobj; parent; parent = parent->parent) { |
| 128 | int cur = strlen(kobject_name(parent)); |
| 129 | /* back up enough to print this name with '/' */ |
| 130 | length -= cur; |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 131 | strncpy(path + length, kobject_name(parent), cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | *(path + --length) = '/'; |
| 133 | } |
| 134 | |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 135 | pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj), |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 136 | kobj, __func__, path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 140 | * kobject_get_path - generate and return the path associated with a given kobj and kset pair. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * |
| 142 | * @kobj: kobject in question, with which to build the path |
| 143 | * @gfp_mask: the allocation type used to allocate the path |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 144 | * |
| 145 | * The result must be freed by the caller with kfree(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | */ |
Al Viro | fd4f2df | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 147 | char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
| 149 | char *path; |
| 150 | int len; |
| 151 | |
| 152 | len = get_kobj_path_length(kobj); |
Chuck Ebbert | b365b3d | 2006-01-12 20:02:00 -0500 | [diff] [blame] | 153 | if (len == 0) |
| 154 | return NULL; |
Burman Yan | 4668edc | 2006-12-06 20:38:51 -0800 | [diff] [blame] | 155 | path = kzalloc(len, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (!path) |
| 157 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | fill_kobj_path(kobj, path, len); |
| 159 | |
| 160 | return path; |
| 161 | } |
Dmitry Torokhov | 80fc9f5 | 2006-10-11 01:43:58 -0400 | [diff] [blame] | 162 | EXPORT_SYMBOL_GPL(kobject_get_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 164 | /* add the kobject to its kset's list */ |
| 165 | static void kobj_kset_join(struct kobject *kobj) |
| 166 | { |
| 167 | if (!kobj->kset) |
| 168 | return; |
| 169 | |
| 170 | kset_get(kobj->kset); |
| 171 | spin_lock(&kobj->kset->list_lock); |
| 172 | list_add_tail(&kobj->entry, &kobj->kset->list); |
| 173 | spin_unlock(&kobj->kset->list_lock); |
| 174 | } |
| 175 | |
| 176 | /* remove the kobject from its kset's list */ |
| 177 | static void kobj_kset_leave(struct kobject *kobj) |
| 178 | { |
| 179 | if (!kobj->kset) |
| 180 | return; |
| 181 | |
| 182 | spin_lock(&kobj->kset->list_lock); |
| 183 | list_del_init(&kobj->entry); |
| 184 | spin_unlock(&kobj->kset->list_lock); |
| 185 | kset_put(kobj->kset); |
| 186 | } |
| 187 | |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 188 | static void kobject_init_internal(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Greg Kroah-Hartman | 31b9025a | 2007-01-18 12:23:51 -0800 | [diff] [blame] | 190 | if (!kobj) |
| 191 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | kref_init(&kobj->kref); |
| 193 | INIT_LIST_HEAD(&kobj->entry); |
Greg Kroah-Hartman | a4573c4 | 2008-02-26 09:36:38 -0800 | [diff] [blame] | 194 | kobj->state_in_sysfs = 0; |
| 195 | kobj->state_add_uevent_sent = 0; |
| 196 | kobj->state_remove_uevent_sent = 0; |
| 197 | kobj->state_initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Greg Kroah-Hartman | 9e7bbcc | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 201 | static int kobject_add_internal(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | int error = 0; |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 204 | struct kobject *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 206 | if (!kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return -ENOENT; |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 208 | |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 209 | if (!kobj->name || !kobj->name[0]) { |
Arjan van de Ven | d955c78 | 2008-07-25 01:45:55 -0700 | [diff] [blame] | 210 | WARN(1, "kobject: (%p): attempted to be registered with empty " |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 211 | "name!\n", kobj); |
Greg Kroah-Hartman | c171fef | 2006-01-20 14:08:59 -0800 | [diff] [blame] | 212 | return -EINVAL; |
| 213 | } |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | parent = kobject_get(kobj->parent); |
| 216 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 217 | /* join kset if set, use it as parent if we do not already have one */ |
| 218 | if (kobj->kset) { |
| 219 | if (!parent) |
| 220 | parent = kobject_get(&kobj->kset->kobj); |
| 221 | kobj_kset_join(kobj); |
| 222 | kobj->parent = parent; |
| 223 | } |
| 224 | |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 225 | pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n", |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 226 | kobject_name(kobj), kobj, __func__, |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 227 | parent ? kobject_name(parent) : "<NULL>", |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 228 | kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 230 | error = create_dir(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (error) { |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 232 | kobj_kset_leave(kobj); |
| 233 | kobject_put(parent); |
| 234 | kobj->parent = NULL; |
Greg Kroah-Hartman | dcd0da0 | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 235 | |
| 236 | /* be noisy on error issues */ |
| 237 | if (error == -EEXIST) |
Dan Williams | 282029c | 2012-04-06 13:41:15 -0700 | [diff] [blame] | 238 | WARN(1, "%s failed for %s with " |
| 239 | "-EEXIST, don't try to register things with " |
| 240 | "the same name in the same directory.\n", |
| 241 | __func__, kobject_name(kobj)); |
Greg Kroah-Hartman | dcd0da0 | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 242 | else |
Dan Williams | 282029c | 2012-04-06 13:41:15 -0700 | [diff] [blame] | 243 | WARN(1, "%s failed for %s (error: %d parent: %s)\n", |
| 244 | __func__, kobject_name(kobj), error, |
| 245 | parent ? kobject_name(parent) : "'none'"); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 246 | } else |
| 247 | kobj->state_in_sysfs = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | return error; |
| 250 | } |
| 251 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 252 | /** |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 253 | * kobject_set_name_vargs - Set the name of an kobject |
| 254 | * @kobj: struct kobject to set the name of |
Greg Kroah-Hartman | 8c4606b | 2007-12-04 14:45:47 +0800 | [diff] [blame] | 255 | * @fmt: format string used to build the name |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 256 | * @vargs: vargs to format the string. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | */ |
Kay Sievers | 1fa5ae8 | 2009-01-25 15:17:37 +0100 | [diff] [blame] | 258 | int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 259 | va_list vargs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Rasmus Villemoes | f773f32 | 2015-11-06 16:31:23 -0800 | [diff] [blame] | 261 | const char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Kay Sievers | 8a577ff | 2009-04-18 15:05:45 -0700 | [diff] [blame] | 263 | if (kobj->name && !fmt) |
| 264 | return 0; |
| 265 | |
Rasmus Villemoes | f773f32 | 2015-11-06 16:31:23 -0800 | [diff] [blame] | 266 | s = kvasprintf_const(GFP_KERNEL, fmt, vargs); |
Rasmus Villemoes | 2abf114 | 2015-06-25 15:02:30 -0700 | [diff] [blame] | 267 | if (!s) |
Kay Sievers | a4ca661 | 2008-04-30 02:06:29 +0200 | [diff] [blame] | 268 | return -ENOMEM; |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 269 | |
Rasmus Villemoes | f773f32 | 2015-11-06 16:31:23 -0800 | [diff] [blame] | 270 | /* |
| 271 | * ewww... some of these buggers have '/' in the name ... If |
| 272 | * that's the case, we need to make sure we have an actual |
| 273 | * allocated copy to modify, since kvasprintf_const may have |
| 274 | * returned something from .rodata. |
| 275 | */ |
| 276 | if (strchr(s, '/')) { |
| 277 | char *t; |
| 278 | |
| 279 | t = kstrdup(s, GFP_KERNEL); |
| 280 | kfree_const(s); |
| 281 | if (!t) |
| 282 | return -ENOMEM; |
| 283 | strreplace(t, '/', '!'); |
| 284 | s = t; |
| 285 | } |
| 286 | kfree_const(kobj->name); |
Rasmus Villemoes | 2abf114 | 2015-06-25 15:02:30 -0700 | [diff] [blame] | 287 | kobj->name = s; |
Kay Sievers | 9f255651 | 2008-05-06 22:24:04 +0200 | [diff] [blame] | 288 | |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * kobject_set_name - Set the name of a kobject |
| 294 | * @kobj: struct kobject to set the name of |
| 295 | * @fmt: format string used to build the name |
| 296 | * |
| 297 | * This sets the name of the kobject. If you have already added the |
| 298 | * kobject to the system, you must call kobject_rename() in order to |
| 299 | * change the name of the kobject. |
| 300 | */ |
| 301 | int kobject_set_name(struct kobject *kobj, const char *fmt, ...) |
| 302 | { |
Kay Sievers | a4ca661 | 2008-04-30 02:06:29 +0200 | [diff] [blame] | 303 | va_list vargs; |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 304 | int retval; |
| 305 | |
Kay Sievers | a4ca661 | 2008-04-30 02:06:29 +0200 | [diff] [blame] | 306 | va_start(vargs, fmt); |
| 307 | retval = kobject_set_name_vargs(kobj, fmt, vargs); |
| 308 | va_end(vargs); |
Greg Kroah-Hartman | 663a474 | 2007-11-29 18:32:47 -0500 | [diff] [blame] | 309 | |
| 310 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | EXPORT_SYMBOL(kobject_set_name); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | /** |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 315 | * kobject_init - initialize a kobject structure |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 316 | * @kobj: pointer to the kobject to initialize |
| 317 | * @ktype: pointer to the ktype for this kobject. |
| 318 | * |
| 319 | * This function will properly initialize a kobject such that it can then |
| 320 | * be passed to the kobject_add() call. |
| 321 | * |
| 322 | * After this function is called, the kobject MUST be cleaned up by a call |
| 323 | * to kobject_put(), not by a call to kfree directly to ensure that all of |
| 324 | * the memory is cleaned up properly. |
| 325 | */ |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 326 | void kobject_init(struct kobject *kobj, struct kobj_type *ktype) |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 327 | { |
| 328 | char *err_str; |
| 329 | |
| 330 | if (!kobj) { |
| 331 | err_str = "invalid kobject pointer!"; |
| 332 | goto error; |
| 333 | } |
| 334 | if (!ktype) { |
| 335 | err_str = "must have a ktype to be initialized properly!\n"; |
| 336 | goto error; |
| 337 | } |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 338 | if (kobj->state_initialized) { |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 339 | /* do not error out as sometimes we can recover */ |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 340 | printk(KERN_ERR "kobject (%p): tried to init an initialized " |
| 341 | "object, something is seriously wrong.\n", kobj); |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 342 | dump_stack(); |
| 343 | } |
| 344 | |
Greg Kroah-Hartman | a4573c4 | 2008-02-26 09:36:38 -0800 | [diff] [blame] | 345 | kobject_init_internal(kobj); |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 346 | kobj->ktype = ktype; |
| 347 | return; |
| 348 | |
| 349 | error: |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 350 | printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str); |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 351 | dump_stack(); |
| 352 | } |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 353 | EXPORT_SYMBOL(kobject_init); |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 354 | |
Nicolas Iooss | 8db1486 | 2015-07-17 16:23:42 -0700 | [diff] [blame] | 355 | static __printf(3, 0) int kobject_add_varg(struct kobject *kobj, |
| 356 | struct kobject *parent, |
| 357 | const char *fmt, va_list vargs) |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 358 | { |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 359 | int retval; |
| 360 | |
Kay Sievers | a4ca661 | 2008-04-30 02:06:29 +0200 | [diff] [blame] | 361 | retval = kobject_set_name_vargs(kobj, fmt, vargs); |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 362 | if (retval) { |
| 363 | printk(KERN_ERR "kobject: can not set name properly!\n"); |
| 364 | return retval; |
| 365 | } |
| 366 | kobj->parent = parent; |
Greg Kroah-Hartman | 9e7bbcc | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 367 | return kobject_add_internal(kobj); |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 371 | * kobject_add - the main kobject add function |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 372 | * @kobj: the kobject to add |
| 373 | * @parent: pointer to the parent of the kobject. |
| 374 | * @fmt: format to name the kobject with. |
| 375 | * |
| 376 | * The kobject name is set and added to the kobject hierarchy in this |
| 377 | * function. |
| 378 | * |
| 379 | * If @parent is set, then the parent of the @kobj will be set to it. |
| 380 | * If @parent is NULL, then the parent of the @kobj will be set to the |
Bart Van Assche | 9705710 | 2014-01-04 14:20:04 +0100 | [diff] [blame] | 381 | * kobject associated with the kset assigned to this kobject. If no kset |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 382 | * is assigned to the kobject, then the kobject will be located in the |
| 383 | * root of the sysfs tree. |
| 384 | * |
| 385 | * If this function returns an error, kobject_put() must be called to |
| 386 | * properly clean up the memory associated with the object. |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 387 | * Under no instance should the kobject that is passed to this function |
| 388 | * be directly freed with a call to kfree(), that can leak memory. |
| 389 | * |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 390 | * Note, no "add" uevent will be created with this call, the caller should set |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 391 | * up all of the necessary sysfs files for the object and then call |
| 392 | * kobject_uevent() with the UEVENT_ADD parameter to ensure that |
| 393 | * userspace is properly notified of this kobject's creation. |
| 394 | */ |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 395 | int kobject_add(struct kobject *kobj, struct kobject *parent, |
| 396 | const char *fmt, ...) |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 397 | { |
| 398 | va_list args; |
| 399 | int retval; |
| 400 | |
| 401 | if (!kobj) |
| 402 | return -EINVAL; |
| 403 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 404 | if (!kobj->state_initialized) { |
| 405 | printk(KERN_ERR "kobject '%s' (%p): tried to add an " |
| 406 | "uninitialized object, something is seriously wrong.\n", |
| 407 | kobject_name(kobj), kobj); |
| 408 | dump_stack(); |
| 409 | return -EINVAL; |
| 410 | } |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 411 | va_start(args, fmt); |
| 412 | retval = kobject_add_varg(kobj, parent, fmt, args); |
| 413 | va_end(args); |
| 414 | |
| 415 | return retval; |
| 416 | } |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 417 | EXPORT_SYMBOL(kobject_add); |
Greg Kroah-Hartman | 244f6ce | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 418 | |
Greg Kroah-Hartman | e86000d | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 419 | /** |
Greg Kroah-Hartman | c11c4154 | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 420 | * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy |
| 421 | * @kobj: pointer to the kobject to initialize |
| 422 | * @ktype: pointer to the ktype for this kobject. |
| 423 | * @parent: pointer to the parent of this kobject. |
| 424 | * @fmt: the name of the kobject. |
| 425 | * |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 426 | * This function combines the call to kobject_init() and |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 427 | * kobject_add(). The same type of error handling after a call to |
| 428 | * kobject_add() and kobject lifetime rules are the same here. |
Greg Kroah-Hartman | c11c4154 | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 429 | */ |
| 430 | int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, |
| 431 | struct kobject *parent, const char *fmt, ...) |
| 432 | { |
| 433 | va_list args; |
| 434 | int retval; |
| 435 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 436 | kobject_init(kobj, ktype); |
Greg Kroah-Hartman | c11c4154 | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 437 | |
| 438 | va_start(args, fmt); |
| 439 | retval = kobject_add_varg(kobj, parent, fmt, args); |
| 440 | va_end(args); |
| 441 | |
| 442 | return retval; |
| 443 | } |
| 444 | EXPORT_SYMBOL_GPL(kobject_init_and_add); |
| 445 | |
| 446 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 447 | * kobject_rename - change the name of an object |
| 448 | * @kobj: object in question. |
| 449 | * @new_name: object's new name |
Eric W. Biederman | 030c1d2 | 2008-05-08 14:41:00 -0700 | [diff] [blame] | 450 | * |
| 451 | * It is the responsibility of the caller to provide mutual |
| 452 | * exclusion between two different calls of kobject_rename |
| 453 | * on the same kobject and to ensure that new_name is valid and |
| 454 | * won't conflict with other kobjects. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 456 | int kobject_rename(struct kobject *kobj, const char *new_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
| 458 | int error = 0; |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 459 | const char *devpath = NULL; |
Eric W. Biederman | 0b4a4fe | 2008-07-03 18:05:28 -0700 | [diff] [blame] | 460 | const char *dup_name = NULL, *name; |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 461 | char *devpath_string = NULL; |
| 462 | char *envp[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | kobj = kobject_get(kobj); |
| 465 | if (!kobj) |
| 466 | return -EINVAL; |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 467 | if (!kobj->parent) |
| 468 | return -EINVAL; |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 469 | |
| 470 | devpath = kobject_get_path(kobj, GFP_KERNEL); |
| 471 | if (!devpath) { |
| 472 | error = -ENOMEM; |
| 473 | goto out; |
| 474 | } |
| 475 | devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); |
| 476 | if (!devpath_string) { |
| 477 | error = -ENOMEM; |
| 478 | goto out; |
| 479 | } |
| 480 | sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); |
| 481 | envp[0] = devpath_string; |
| 482 | envp[1] = NULL; |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 483 | |
Rasmus Villemoes | f773f32 | 2015-11-06 16:31:23 -0800 | [diff] [blame] | 484 | name = dup_name = kstrdup_const(new_name, GFP_KERNEL); |
Eric W. Biederman | 0b4a4fe | 2008-07-03 18:05:28 -0700 | [diff] [blame] | 485 | if (!name) { |
| 486 | error = -ENOMEM; |
| 487 | goto out; |
| 488 | } |
| 489 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 490 | error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj)); |
Eric W. Biederman | 0b4a4fe | 2008-07-03 18:05:28 -0700 | [diff] [blame] | 491 | if (error) |
| 492 | goto out; |
| 493 | |
| 494 | /* Install the new kobject name */ |
| 495 | dup_name = kobj->name; |
| 496 | kobj->name = name; |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 497 | |
| 498 | /* This function is mostly/only used for network interface. |
| 499 | * Some hotplug package track interfaces by their name and |
| 500 | * therefore want to know when the name is changed by the user. */ |
Eric W. Biederman | 0b4a4fe | 2008-07-03 18:05:28 -0700 | [diff] [blame] | 501 | kobject_uevent_env(kobj, KOBJ_MOVE, envp); |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 502 | |
| 503 | out: |
Rasmus Villemoes | f773f32 | 2015-11-06 16:31:23 -0800 | [diff] [blame] | 504 | kfree_const(dup_name); |
Jean Tourrilhes | ca2f37d | 2007-03-07 10:49:30 -0800 | [diff] [blame] | 505 | kfree(devpath_string); |
| 506 | kfree(devpath); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 507 | kobject_put(kobj); |
| 508 | |
| 509 | return error; |
| 510 | } |
Alex Chiang | 8344b56 | 2008-06-10 15:30:42 -0600 | [diff] [blame] | 511 | EXPORT_SYMBOL_GPL(kobject_rename); |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 512 | |
| 513 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 514 | * kobject_move - move object to another parent |
| 515 | * @kobj: object in question. |
| 516 | * @new_parent: object's new parent (can be NULL) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 517 | */ |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 518 | int kobject_move(struct kobject *kobj, struct kobject *new_parent) |
| 519 | { |
| 520 | int error; |
| 521 | struct kobject *old_parent; |
| 522 | const char *devpath = NULL; |
| 523 | char *devpath_string = NULL; |
| 524 | char *envp[2]; |
| 525 | |
| 526 | kobj = kobject_get(kobj); |
| 527 | if (!kobj) |
| 528 | return -EINVAL; |
| 529 | new_parent = kobject_get(new_parent); |
| 530 | if (!new_parent) { |
Cornelia Huck | c744aeae | 2007-01-08 20:16:44 +0100 | [diff] [blame] | 531 | if (kobj->kset) |
| 532 | new_parent = kobject_get(&kobj->kset->kobj); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 533 | } |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 534 | |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 535 | /* old object path */ |
| 536 | devpath = kobject_get_path(kobj, GFP_KERNEL); |
| 537 | if (!devpath) { |
| 538 | error = -ENOMEM; |
| 539 | goto out; |
| 540 | } |
| 541 | devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); |
| 542 | if (!devpath_string) { |
| 543 | error = -ENOMEM; |
| 544 | goto out; |
| 545 | } |
| 546 | sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); |
| 547 | envp[0] = devpath_string; |
| 548 | envp[1] = NULL; |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 549 | error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj)); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 550 | if (error) |
| 551 | goto out; |
| 552 | old_parent = kobj->parent; |
| 553 | kobj->parent = new_parent; |
Dmitriy Monakhov | 9e993ef | 2007-03-03 16:11:21 +0300 | [diff] [blame] | 554 | new_parent = NULL; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 555 | kobject_put(old_parent); |
| 556 | kobject_uevent_env(kobj, KOBJ_MOVE, envp); |
| 557 | out: |
Dmitriy Monakhov | 9e993ef | 2007-03-03 16:11:21 +0300 | [diff] [blame] | 558 | kobject_put(new_parent); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 559 | kobject_put(kobj); |
| 560 | kfree(devpath_string); |
| 561 | kfree(devpath); |
| 562 | return error; |
| 563 | } |
Anand Jain | 24199d2 | 2015-02-12 07:03:37 +0800 | [diff] [blame] | 564 | EXPORT_SYMBOL_GPL(kobject_move); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 565 | |
| 566 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 567 | * kobject_del - unlink kobject from hierarchy. |
| 568 | * @kobj: object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 570 | void kobject_del(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 572 | struct kernfs_node *sd; |
Tejun Heo | 26ea12d | 2013-09-18 17:15:36 -0400 | [diff] [blame] | 573 | |
Greg Kroah-Hartman | 31b9025a | 2007-01-18 12:23:51 -0800 | [diff] [blame] | 574 | if (!kobj) |
| 575 | return; |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 576 | |
Tejun Heo | 26ea12d | 2013-09-18 17:15:36 -0400 | [diff] [blame] | 577 | sd = kobj->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | sysfs_remove_dir(kobj); |
Tejun Heo | 26ea12d | 2013-09-18 17:15:36 -0400 | [diff] [blame] | 579 | sysfs_put(sd); |
| 580 | |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 581 | kobj->state_in_sysfs = 0; |
| 582 | kobj_kset_leave(kobj); |
| 583 | kobject_put(kobj->parent); |
| 584 | kobj->parent = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
Gabriel Somlo | fa40ae3 | 2015-08-10 15:51:43 -0400 | [diff] [blame] | 586 | EXPORT_SYMBOL(kobject_del); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 589 | * kobject_get - increment refcount for object. |
| 590 | * @kobj: object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 592 | struct kobject *kobject_get(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Ethan Zhao | d82d54a | 2015-03-12 13:04:16 +0900 | [diff] [blame] | 594 | if (kobj) { |
| 595 | if (!kobj->state_initialized) |
| 596 | WARN(1, KERN_WARNING "kobject: '%s' (%p): is not " |
| 597 | "initialized, yet kobject_get() is being " |
| 598 | "called.\n", kobject_name(kobj), kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | kref_get(&kobj->kref); |
Ethan Zhao | d82d54a | 2015-03-12 13:04:16 +0900 | [diff] [blame] | 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return kobj; |
| 602 | } |
Gabriel Somlo | fa40ae3 | 2015-08-10 15:51:43 -0400 | [diff] [blame] | 603 | EXPORT_SYMBOL(kobject_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Jan Kara | c70c176 | 2017-03-23 01:37:01 +0100 | [diff] [blame] | 605 | struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj) |
Linus Torvalds | a49b7e8 | 2013-04-13 15:15:30 -0700 | [diff] [blame] | 606 | { |
Jan Kara | c70c176 | 2017-03-23 01:37:01 +0100 | [diff] [blame] | 607 | if (!kobj) |
| 608 | return NULL; |
Linus Torvalds | a49b7e8 | 2013-04-13 15:15:30 -0700 | [diff] [blame] | 609 | if (!kref_get_unless_zero(&kobj->kref)) |
| 610 | kobj = NULL; |
| 611 | return kobj; |
| 612 | } |
Jan Kara | c70c176 | 2017-03-23 01:37:01 +0100 | [diff] [blame] | 613 | EXPORT_SYMBOL(kobject_get_unless_zero); |
Linus Torvalds | a49b7e8 | 2013-04-13 15:15:30 -0700 | [diff] [blame] | 614 | |
Greg Kroah-Hartman | 18041f47 | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 615 | /* |
| 616 | * kobject_cleanup - free kobject resources. |
| 617 | * @kobj: object to cleanup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | */ |
Greg Kroah-Hartman | 18041f47 | 2007-12-03 21:31:08 -0800 | [diff] [blame] | 619 | static void kobject_cleanup(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 621 | struct kobj_type *t = get_ktype(kobj); |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 622 | const char *name = kobj->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Russell King | c817a67 | 2013-06-27 15:06:14 +0100 | [diff] [blame] | 624 | pr_debug("kobject: '%s' (%p): %s, parent %p\n", |
| 625 | kobject_name(kobj), kobj, __func__, kobj->parent); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 626 | |
| 627 | if (t && !t->release) |
| 628 | pr_debug("kobject: '%s' (%p): does not have a release() " |
| 629 | "function, it is broken and must be fixed.\n", |
| 630 | kobject_name(kobj), kobj); |
| 631 | |
| 632 | /* send "remove" if the caller did not do it but sent "add" */ |
| 633 | if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) { |
| 634 | pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n", |
| 635 | kobject_name(kobj), kobj); |
| 636 | kobject_uevent(kobj, KOBJ_REMOVE); |
| 637 | } |
| 638 | |
| 639 | /* remove from sysfs if the caller did not do it */ |
| 640 | if (kobj->state_in_sysfs) { |
| 641 | pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", |
| 642 | kobject_name(kobj), kobj); |
| 643 | kobject_del(kobj); |
| 644 | } |
| 645 | |
Greg Kroah-Hartman | ce2c9cb | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 646 | if (t && t->release) { |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 647 | pr_debug("kobject: '%s' (%p): calling ktype release\n", |
| 648 | kobject_name(kobj), kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | t->release(kobj); |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* free name if we allocated it */ |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 653 | if (name) { |
Kay Sievers | 0f4dafc | 2007-12-19 01:40:42 +0100 | [diff] [blame] | 654 | pr_debug("kobject: '%s': free name\n", name); |
Rasmus Villemoes | f773f32 | 2015-11-06 16:31:23 -0800 | [diff] [blame] | 655 | kfree_const(name); |
Greg Kroah-Hartman | ce2c9cb | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 656 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Russell King | c817a67 | 2013-06-27 15:06:14 +0100 | [diff] [blame] | 659 | #ifdef CONFIG_DEBUG_KOBJECT_RELEASE |
| 660 | static void kobject_delayed_cleanup(struct work_struct *work) |
| 661 | { |
| 662 | kobject_cleanup(container_of(to_delayed_work(work), |
| 663 | struct kobject, release)); |
| 664 | } |
| 665 | #endif |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | static void kobject_release(struct kref *kref) |
| 668 | { |
Russell King | c817a67 | 2013-06-27 15:06:14 +0100 | [diff] [blame] | 669 | struct kobject *kobj = container_of(kref, struct kobject, kref); |
| 670 | #ifdef CONFIG_DEBUG_KOBJECT_RELEASE |
Bjorn Helgaas | 89c86a6 | 2013-12-05 17:37:51 -0700 | [diff] [blame] | 671 | unsigned long delay = HZ + HZ * (get_random_int() & 0x3); |
| 672 | pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n", |
| 673 | kobject_name(kobj), kobj, __func__, kobj->parent, delay); |
Russell King | c817a67 | 2013-06-27 15:06:14 +0100 | [diff] [blame] | 674 | INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup); |
Bjorn Helgaas | 89c86a6 | 2013-12-05 17:37:51 -0700 | [diff] [blame] | 675 | |
| 676 | schedule_delayed_work(&kobj->release, delay); |
Russell King | c817a67 | 2013-06-27 15:06:14 +0100 | [diff] [blame] | 677 | #else |
| 678 | kobject_cleanup(kobj); |
| 679 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 683 | * kobject_put - decrement refcount for object. |
| 684 | * @kobj: object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | * |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 686 | * Decrement the refcount, and if 0, call kobject_cleanup(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 688 | void kobject_put(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Greg Kroah-Hartman | c1ebdae | 2008-02-26 09:36:38 -0800 | [diff] [blame] | 690 | if (kobj) { |
Arjan van de Ven | d955c78 | 2008-07-25 01:45:55 -0700 | [diff] [blame] | 691 | if (!kobj->state_initialized) |
| 692 | WARN(1, KERN_WARNING "kobject: '%s' (%p): is not " |
Greg Kroah-Hartman | c1ebdae | 2008-02-26 09:36:38 -0800 | [diff] [blame] | 693 | "initialized, yet kobject_put() is being " |
| 694 | "called.\n", kobject_name(kobj), kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | kref_put(&kobj->kref, kobject_release); |
Greg Kroah-Hartman | c1ebdae | 2008-02-26 09:36:38 -0800 | [diff] [blame] | 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
Gabriel Somlo | fa40ae3 | 2015-08-10 15:51:43 -0400 | [diff] [blame] | 698 | EXPORT_SYMBOL(kobject_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 700 | static void dynamic_kobj_release(struct kobject *kobj) |
Jun'ichi Nomura | 7423172 | 2006-03-13 17:14:25 -0500 | [diff] [blame] | 701 | { |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 702 | pr_debug("kobject: (%p): %s\n", kobj, __func__); |
Jun'ichi Nomura | 7423172 | 2006-03-13 17:14:25 -0500 | [diff] [blame] | 703 | kfree(kobj); |
| 704 | } |
| 705 | |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 706 | static struct kobj_type dynamic_kobj_ktype = { |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 707 | .release = dynamic_kobj_release, |
| 708 | .sysfs_ops = &kobj_sysfs_ops, |
Jun'ichi Nomura | 7423172 | 2006-03-13 17:14:25 -0500 | [diff] [blame] | 709 | }; |
| 710 | |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 711 | /** |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 712 | * kobject_create - create a struct kobject dynamically |
| 713 | * |
| 714 | * This function creates a kobject structure dynamically and sets it up |
| 715 | * to be a "dynamic" kobject with a default release function set up. |
| 716 | * |
| 717 | * If the kobject was not able to be created, NULL will be returned. |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 718 | * The kobject structure returned from here must be cleaned up with a |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 719 | * call to kobject_put() and not kfree(), as kobject_init() has |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 720 | * already been called on this structure. |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 721 | */ |
Greg Kroah-Hartman | 43968d2 | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 722 | struct kobject *kobject_create(void) |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 723 | { |
| 724 | struct kobject *kobj; |
| 725 | |
| 726 | kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); |
| 727 | if (!kobj) |
| 728 | return NULL; |
| 729 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 730 | kobject_init(kobj, &dynamic_kobj_ktype); |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 731 | return kobj; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs |
| 736 | * |
Zhi Yong Wu | 9ff1f83 | 2012-05-07 10:48:25 +0800 | [diff] [blame] | 737 | * @name: the name for the kobject |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 738 | * @parent: the parent kobject of this kobject, if any. |
| 739 | * |
Dave Young | f70701a | 2008-01-28 16:58:00 +0800 | [diff] [blame] | 740 | * This function creates a kobject structure dynamically and registers it |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 741 | * with sysfs. When you are finished with this structure, call |
Greg Kroah-Hartman | 78a2d90 | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 742 | * kobject_put() and the structure will be dynamically freed when |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 743 | * it is no longer being used. |
| 744 | * |
| 745 | * If the kobject was not able to be created, NULL will be returned. |
| 746 | */ |
| 747 | struct kobject *kobject_create_and_add(const char *name, struct kobject *parent) |
| 748 | { |
| 749 | struct kobject *kobj; |
| 750 | int retval; |
| 751 | |
| 752 | kobj = kobject_create(); |
| 753 | if (!kobj) |
| 754 | return NULL; |
| 755 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 756 | retval = kobject_add(kobj, parent, "%s", name); |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 757 | if (retval) { |
| 758 | printk(KERN_WARNING "%s: kobject_add error: %d\n", |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 759 | __func__, retval); |
Greg Kroah-Hartman | 3f9e3ee | 2007-11-05 13:16:15 -0800 | [diff] [blame] | 760 | kobject_put(kobj); |
| 761 | kobj = NULL; |
| 762 | } |
| 763 | return kobj; |
| 764 | } |
| 765 | EXPORT_SYMBOL_GPL(kobject_create_and_add); |
| 766 | |
Jun'ichi Nomura | 7423172 | 2006-03-13 17:14:25 -0500 | [diff] [blame] | 767 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 768 | * kset_init - initialize a kset for use |
| 769 | * @k: kset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 771 | void kset_init(struct kset *k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | { |
Greg Kroah-Hartman | e1543dd | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 773 | kobject_init_internal(&k->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | INIT_LIST_HEAD(&k->list); |
| 775 | spin_lock_init(&k->list_lock); |
| 776 | } |
| 777 | |
Kay Sievers | 23b5212 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 778 | /* default kobject attribute operations */ |
| 779 | static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr, |
| 780 | char *buf) |
| 781 | { |
| 782 | struct kobj_attribute *kattr; |
| 783 | ssize_t ret = -EIO; |
| 784 | |
| 785 | kattr = container_of(attr, struct kobj_attribute, attr); |
| 786 | if (kattr->show) |
| 787 | ret = kattr->show(kobj, kattr, buf); |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr, |
| 792 | const char *buf, size_t count) |
| 793 | { |
| 794 | struct kobj_attribute *kattr; |
| 795 | ssize_t ret = -EIO; |
| 796 | |
| 797 | kattr = container_of(attr, struct kobj_attribute, attr); |
| 798 | if (kattr->store) |
| 799 | ret = kattr->store(kobj, kattr, buf, count); |
| 800 | return ret; |
| 801 | } |
| 802 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 803 | const struct sysfs_ops kobj_sysfs_ops = { |
Kay Sievers | 23b5212 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 804 | .show = kobj_attr_show, |
| 805 | .store = kobj_attr_store, |
| 806 | }; |
Jeff Mahoney | 29dfe2d | 2013-11-01 13:06:56 -0400 | [diff] [blame] | 807 | EXPORT_SYMBOL_GPL(kobj_sysfs_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 810 | * kset_register - initialize and add a kset. |
| 811 | * @k: kset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 813 | int kset_register(struct kset *k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
Kay Sievers | 80f03e3 | 2007-05-26 11:21:36 +0200 | [diff] [blame] | 815 | int err; |
| 816 | |
Greg Kroah-Hartman | 31b9025a | 2007-01-18 12:23:51 -0800 | [diff] [blame] | 817 | if (!k) |
| 818 | return -EINVAL; |
Kay Sievers | 80f03e3 | 2007-05-26 11:21:36 +0200 | [diff] [blame] | 819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | kset_init(k); |
Greg Kroah-Hartman | 12e339a | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 821 | err = kobject_add_internal(&k->kobj); |
Kay Sievers | 80f03e3 | 2007-05-26 11:21:36 +0200 | [diff] [blame] | 822 | if (err) |
| 823 | return err; |
| 824 | kobject_uevent(&k->kobj, KOBJ_ADD); |
| 825 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
Gabriel Somlo | fa40ae3 | 2015-08-10 15:51:43 -0400 | [diff] [blame] | 827 | EXPORT_SYMBOL(kset_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 830 | * kset_unregister - remove a kset. |
| 831 | * @k: kset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 833 | void kset_unregister(struct kset *k) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Greg Kroah-Hartman | 31b9025a | 2007-01-18 12:23:51 -0800 | [diff] [blame] | 835 | if (!k) |
| 836 | return; |
Bjorn Helgaas | 35a5fe6 | 2013-12-05 17:38:00 -0700 | [diff] [blame] | 837 | kobject_del(&k->kobj); |
Greg Kroah-Hartman | 78a2d90 | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 838 | kobject_put(&k->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
Gabriel Somlo | fa40ae3 | 2015-08-10 15:51:43 -0400 | [diff] [blame] | 840 | EXPORT_SYMBOL(kset_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | /** |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 843 | * kset_find_obj - search for object in kset. |
| 844 | * @kset: kset we're looking in. |
| 845 | * @name: object's name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | * |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 847 | * Lock kset via @kset->subsys, and iterate over @kset->list, |
| 848 | * looking for a matching kobject. If matching object is found |
| 849 | * take a reference and return the object. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | */ |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 851 | struct kobject *kset_find_obj(struct kset *kset, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
Robert P. J. Day | c6a2a3d | 2008-03-27 01:13:34 -0400 | [diff] [blame] | 853 | struct kobject *k; |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 854 | struct kobject *ret = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
| 856 | spin_lock(&kset->list_lock); |
Robin Holt | c25d1df | 2010-09-29 14:00:54 -0500 | [diff] [blame] | 857 | |
Robert P. J. Day | c6a2a3d | 2008-03-27 01:13:34 -0400 | [diff] [blame] | 858 | list_for_each_entry(k, &kset->list, entry) { |
Greg Kroah-Hartman | e374a2b | 2008-01-24 21:59:04 -0800 | [diff] [blame] | 859 | if (kobject_name(k) && !strcmp(kobject_name(k), name)) { |
Linus Torvalds | a49b7e8 | 2013-04-13 15:15:30 -0700 | [diff] [blame] | 860 | ret = kobject_get_unless_zero(k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | break; |
| 862 | } |
| 863 | } |
Robin Holt | c25d1df | 2010-09-29 14:00:54 -0500 | [diff] [blame] | 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | spin_unlock(&kset->list_lock); |
| 866 | return ret; |
| 867 | } |
Gabriel Somlo | 2fe829a | 2016-01-28 09:23:12 -0500 | [diff] [blame] | 868 | EXPORT_SYMBOL_GPL(kset_find_obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 870 | static void kset_release(struct kobject *kobj) |
| 871 | { |
| 872 | struct kset *kset = container_of(kobj, struct kset, kobj); |
Greg Kroah-Hartman | 9f66fa2 | 2007-11-28 23:49:41 -0800 | [diff] [blame] | 873 | pr_debug("kobject: '%s' (%p): %s\n", |
Harvey Harrison | 810304d | 2008-04-30 00:55:08 -0700 | [diff] [blame] | 874 | kobject_name(kobj), kobj, __func__); |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 875 | kfree(kset); |
| 876 | } |
| 877 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 878 | static struct kobj_type kset_ktype = { |
| 879 | .sysfs_ops = &kobj_sysfs_ops, |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 880 | .release = kset_release, |
| 881 | }; |
| 882 | |
| 883 | /** |
| 884 | * kset_create - create a struct kset dynamically |
| 885 | * |
| 886 | * @name: the name for the kset |
| 887 | * @uevent_ops: a struct kset_uevent_ops for the kset |
| 888 | * @parent_kobj: the parent kobject of this kset, if any. |
| 889 | * |
| 890 | * This function creates a kset structure dynamically. This structure can |
| 891 | * then be registered with the system and show up in sysfs with a call to |
| 892 | * kset_register(). When you are finished with this structure, if |
| 893 | * kset_register() has been called, call kset_unregister() and the |
| 894 | * structure will be dynamically freed when it is no longer being used. |
| 895 | * |
| 896 | * If the kset was not able to be created, NULL will be returned. |
| 897 | */ |
| 898 | static struct kset *kset_create(const char *name, |
Emese Revfy | 9cd4361 | 2009-12-31 14:52:51 +0100 | [diff] [blame] | 899 | const struct kset_uevent_ops *uevent_ops, |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 900 | struct kobject *parent_kobj) |
| 901 | { |
| 902 | struct kset *kset; |
Dave Young | d9cd8f3 | 2009-05-11 14:17:45 +0800 | [diff] [blame] | 903 | int retval; |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 904 | |
| 905 | kset = kzalloc(sizeof(*kset), GFP_KERNEL); |
| 906 | if (!kset) |
| 907 | return NULL; |
Kees Cook | b7165eb | 2013-06-06 13:52:19 -0700 | [diff] [blame] | 908 | retval = kobject_set_name(&kset->kobj, "%s", name); |
Dave Young | d9cd8f3 | 2009-05-11 14:17:45 +0800 | [diff] [blame] | 909 | if (retval) { |
| 910 | kfree(kset); |
| 911 | return NULL; |
| 912 | } |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 913 | kset->uevent_ops = uevent_ops; |
| 914 | kset->kobj.parent = parent_kobj; |
| 915 | |
| 916 | /* |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 917 | * The kobject of this kset will have a type of kset_ktype and belong to |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 918 | * no kset itself. That way we can properly free it when it is |
| 919 | * finished being used. |
| 920 | */ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 921 | kset->kobj.ktype = &kset_ktype; |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 922 | kset->kobj.kset = NULL; |
| 923 | |
| 924 | return kset; |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * kset_create_and_add - create a struct kset dynamically and add it to sysfs |
| 929 | * |
| 930 | * @name: the name for the kset |
| 931 | * @uevent_ops: a struct kset_uevent_ops for the kset |
| 932 | * @parent_kobj: the parent kobject of this kset, if any. |
| 933 | * |
| 934 | * This function creates a kset structure dynamically and registers it |
| 935 | * with sysfs. When you are finished with this structure, call |
| 936 | * kset_unregister() and the structure will be dynamically freed when it |
| 937 | * is no longer being used. |
| 938 | * |
| 939 | * If the kset was not able to be created, NULL will be returned. |
| 940 | */ |
| 941 | struct kset *kset_create_and_add(const char *name, |
Emese Revfy | 9cd4361 | 2009-12-31 14:52:51 +0100 | [diff] [blame] | 942 | const struct kset_uevent_ops *uevent_ops, |
Greg Kroah-Hartman | b727c70 | 2007-09-27 14:48:53 -0700 | [diff] [blame] | 943 | struct kobject *parent_kobj) |
| 944 | { |
| 945 | struct kset *kset; |
| 946 | int error; |
| 947 | |
| 948 | kset = kset_create(name, uevent_ops, parent_kobj); |
| 949 | if (!kset) |
| 950 | return NULL; |
| 951 | error = kset_register(kset); |
| 952 | if (error) { |
| 953 | kfree(kset); |
| 954 | return NULL; |
| 955 | } |
| 956 | return kset; |
| 957 | } |
| 958 | EXPORT_SYMBOL_GPL(kset_create_and_add); |
| 959 | |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 960 | |
| 961 | static DEFINE_SPINLOCK(kobj_ns_type_lock); |
| 962 | static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES]; |
| 963 | |
| 964 | int kobj_ns_type_register(const struct kobj_ns_type_operations *ops) |
| 965 | { |
| 966 | enum kobj_ns_type type = ops->type; |
| 967 | int error; |
| 968 | |
| 969 | spin_lock(&kobj_ns_type_lock); |
| 970 | |
| 971 | error = -EINVAL; |
| 972 | if (type >= KOBJ_NS_TYPES) |
| 973 | goto out; |
| 974 | |
| 975 | error = -EINVAL; |
| 976 | if (type <= KOBJ_NS_TYPE_NONE) |
| 977 | goto out; |
| 978 | |
| 979 | error = -EBUSY; |
| 980 | if (kobj_ns_ops_tbl[type]) |
| 981 | goto out; |
| 982 | |
| 983 | error = 0; |
| 984 | kobj_ns_ops_tbl[type] = ops; |
| 985 | |
| 986 | out: |
| 987 | spin_unlock(&kobj_ns_type_lock); |
| 988 | return error; |
| 989 | } |
| 990 | |
| 991 | int kobj_ns_type_registered(enum kobj_ns_type type) |
| 992 | { |
| 993 | int registered = 0; |
| 994 | |
| 995 | spin_lock(&kobj_ns_type_lock); |
| 996 | if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES)) |
| 997 | registered = kobj_ns_ops_tbl[type] != NULL; |
| 998 | spin_unlock(&kobj_ns_type_lock); |
| 999 | |
| 1000 | return registered; |
| 1001 | } |
| 1002 | |
| 1003 | const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent) |
| 1004 | { |
| 1005 | const struct kobj_ns_type_operations *ops = NULL; |
| 1006 | |
Pankaj Dubey | 41fb96a | 2014-09-24 16:25:54 +0530 | [diff] [blame] | 1007 | if (parent && parent->ktype && parent->ktype->child_ns_type) |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1008 | ops = parent->ktype->child_ns_type(parent); |
| 1009 | |
| 1010 | return ops; |
| 1011 | } |
| 1012 | |
| 1013 | const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj) |
| 1014 | { |
| 1015 | return kobj_child_ns_ops(kobj->parent); |
| 1016 | } |
| 1017 | |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 1018 | bool kobj_ns_current_may_mount(enum kobj_ns_type type) |
| 1019 | { |
Eric W. Biederman | 730d7d3 | 2013-09-23 14:41:17 -0700 | [diff] [blame] | 1020 | bool may_mount = true; |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 1021 | |
| 1022 | spin_lock(&kobj_ns_type_lock); |
| 1023 | if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && |
| 1024 | kobj_ns_ops_tbl[type]) |
| 1025 | may_mount = kobj_ns_ops_tbl[type]->current_may_mount(); |
| 1026 | spin_unlock(&kobj_ns_type_lock); |
| 1027 | |
| 1028 | return may_mount; |
| 1029 | } |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1030 | |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1031 | void *kobj_ns_grab_current(enum kobj_ns_type type) |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1032 | { |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1033 | void *ns = NULL; |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1034 | |
| 1035 | spin_lock(&kobj_ns_type_lock); |
| 1036 | if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && |
| 1037 | kobj_ns_ops_tbl[type]) |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1038 | ns = kobj_ns_ops_tbl[type]->grab_current_ns(); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1039 | spin_unlock(&kobj_ns_type_lock); |
| 1040 | |
| 1041 | return ns; |
| 1042 | } |
| 1043 | |
| 1044 | const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk) |
| 1045 | { |
| 1046 | const void *ns = NULL; |
| 1047 | |
| 1048 | spin_lock(&kobj_ns_type_lock); |
| 1049 | if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && |
| 1050 | kobj_ns_ops_tbl[type]) |
| 1051 | ns = kobj_ns_ops_tbl[type]->netlink_ns(sk); |
| 1052 | spin_unlock(&kobj_ns_type_lock); |
| 1053 | |
| 1054 | return ns; |
| 1055 | } |
| 1056 | |
| 1057 | const void *kobj_ns_initial(enum kobj_ns_type type) |
| 1058 | { |
| 1059 | const void *ns = NULL; |
| 1060 | |
| 1061 | spin_lock(&kobj_ns_type_lock); |
| 1062 | if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && |
| 1063 | kobj_ns_ops_tbl[type]) |
| 1064 | ns = kobj_ns_ops_tbl[type]->initial_ns(); |
| 1065 | spin_unlock(&kobj_ns_type_lock); |
| 1066 | |
| 1067 | return ns; |
| 1068 | } |
| 1069 | |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1070 | void kobj_ns_drop(enum kobj_ns_type type, void *ns) |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1071 | { |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 1072 | spin_lock(&kobj_ns_type_lock); |
| 1073 | if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && |
| 1074 | kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns) |
| 1075 | kobj_ns_ops_tbl[type]->drop_ns(ns); |
| 1076 | spin_unlock(&kobj_ns_type_lock); |
Eric W. Biederman | bc451f2 | 2010-03-30 11:31:25 -0700 | [diff] [blame] | 1077 | } |