blob: 450e771d0ac78e16c6f4310fbdcda779c199f639 [file] [log] [blame]
Greg Kroah-Hartmand9d16e12017-11-07 17:30:05 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * kobject.c - library routines for handling generic kernel objects
4 *
5 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
Greg Kroah-Hartmanf0e7e1b2007-09-27 14:48:53 -07006 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (c) 2006-2007 Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Gortmaker8bc3bcc2011-11-16 21:29:17 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stat.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/slab.h>
Bjorn Helgaas89c86a62013-12-05 17:37:51 -070021#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Tejun Heoe34ff492013-09-11 22:29:05 -040023/**
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 */
31const 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 Torvaldsa1212d22013-11-07 20:47:28 +090038 return kobj->ktype->namespace(kobj);
Tejun Heoe34ff492013-09-11 22:29:05 -040039}
40
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080041/*
42 * populate_dir - populate directory with attributes.
43 * @kobj: object we're working on.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080045 * 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 Torvalds1da177e2005-04-16 15:20:36 -070049 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080050static int populate_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080052 struct kobj_type *t = get_ktype(kobj);
53 struct attribute *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int error = 0;
55 int i;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (t && t->default_attrs) {
58 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080059 error = sysfs_create_file(kobj, attr);
60 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 break;
62 }
63 }
64 return error;
65}
66
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080067static int create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Tejun Heoc84a3b22013-11-23 18:01:46 -050069 const struct kobj_ns_type_operations *ops;
Tejun Heoe34ff492013-09-11 22:29:05 -040070 int error;
71
72 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
Tejun Heoc84a3b22013-11-23 18:01:46 -050073 if (error)
74 return error;
75
76 error = populate_dir(kobj);
77 if (error) {
78 sysfs_remove_dir(kobj);
79 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Tejun Heo26ea12d2013-09-18 17:15:36 -040081
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 Heoc84a3b22013-11-23 18:01:46 -050088 /*
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 Heofa4cd452014-02-07 13:32:07 -050098 sysfs_enable_ns(kobj->sd);
Tejun Heoc84a3b22013-11-23 18:01:46 -050099 }
100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int get_kobj_path_length(struct kobject *kobj)
105{
106 int length = 1;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800107 struct kobject *parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800109 /* walk up the ancestors until we hit the one pointing to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * root.
111 * Add 1 to strlen for leading '/' of each level.
112 */
113 do {
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500114 if (kobject_name(parent) == NULL)
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 length += strlen(kobject_name(parent)) + 1;
117 parent = parent->parent;
118 } while (parent);
119 return length;
120}
121
122static void fill_kobj_path(struct kobject *kobj, char *path, int length)
123{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800124 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
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-Hartmane374a2b2008-01-24 21:59:04 -0800131 strncpy(path + length, kobject_name(parent), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *(path + --length) = '/';
133 }
134
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800135 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
Harvey Harrison810304d2008-04-30 00:55:08 -0700136 kobj, __func__, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800140 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
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. Day72fd4a32007-02-10 01:45:59 -0800144 *
145 * The result must be freed by the caller with kfree().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
Al Virofd4f2df2005-10-21 03:18:50 -0400147char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 char *path;
150 int len;
151
152 len = get_kobj_path_length(kobj);
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500153 if (len == 0)
154 return NULL;
Burman Yan4668edc2006-12-06 20:38:51 -0800155 path = kzalloc(len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!path)
157 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 fill_kobj_path(kobj, path, len);
159
160 return path;
161}
Dmitry Torokhov80fc9f52006-10-11 01:43:58 -0400162EXPORT_SYMBOL_GPL(kobject_get_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100164/* add the kobject to its kset's list */
165static 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 */
177static 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-Hartmane374a2b2008-01-24 21:59:04 -0800188static void kobject_init_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800190 if (!kobj)
191 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 kref_init(&kobj->kref);
193 INIT_LIST_HEAD(&kobj->entry);
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800194 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 Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700201static int kobject_add_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int error = 0;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800204 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100206 if (!kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -ENOENT;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100208
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100209 if (!kobj->name || !kobj->name[0]) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700210 WARN(1, "kobject: (%p): attempted to be registered with empty "
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800211 "name!\n", kobj);
Greg Kroah-Hartmanc171fef2006-01-20 14:08:59 -0800212 return -EINVAL;
213 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 parent = kobject_get(kobj->parent);
216
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100217 /* 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-Hartman9f66fa22007-11-28 23:49:41 -0800225 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700226 kobject_name(kobj), kobj, __func__,
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800227 parent ? kobject_name(parent) : "<NULL>",
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800228 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900230 error = create_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (error) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100232 kobj_kset_leave(kobj);
233 kobject_put(parent);
234 kobj->parent = NULL;
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800235
236 /* be noisy on error issues */
237 if (error == -EEXIST)
Dan Williams282029c2012-04-06 13:41:15 -0700238 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-Hartmandcd0da02006-03-20 13:17:13 -0800242 else
Dan Williams282029c2012-04-06 13:41:15 -0700243 WARN(1, "%s failed for %s (error: %d parent: %s)\n",
244 __func__, kobject_name(kobj), error,
245 parent ? kobject_name(parent) : "'none'");
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100246 } else
247 kobj->state_in_sysfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return error;
250}
251
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700252/**
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500253 * kobject_set_name_vargs - Set the name of an kobject
254 * @kobj: struct kobject to set the name of
Greg Kroah-Hartman8c4606b2007-12-04 14:45:47 +0800255 * @fmt: format string used to build the name
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500256 * @vargs: vargs to format the string.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100258int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500259 va_list vargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800261 const char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Kay Sievers8a577ff2009-04-18 15:05:45 -0700263 if (kobj->name && !fmt)
264 return 0;
265
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800266 s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
Rasmus Villemoes2abf1142015-06-25 15:02:30 -0700267 if (!s)
Kay Sieversa4ca6612008-04-30 02:06:29 +0200268 return -ENOMEM;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500269
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800270 /*
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 Villemoes2abf1142015-06-25 15:02:30 -0700287 kobj->name = s;
Kay Sievers9f2556512008-05-06 22:24:04 +0200288
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500289 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 */
301int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
302{
Kay Sieversa4ca6612008-04-30 02:06:29 +0200303 va_list vargs;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500304 int retval;
305
Kay Sieversa4ca6612008-04-30 02:06:29 +0200306 va_start(vargs, fmt);
307 retval = kobject_set_name_vargs(kobj, fmt, vargs);
308 va_end(vargs);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500309
310 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312EXPORT_SYMBOL(kobject_set_name);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/**
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700315 * kobject_init - initialize a kobject structure
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800316 * @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-Hartmanf9cb0742007-12-17 23:05:35 -0700326void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800327{
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 Sievers0f4dafc2007-12-19 01:40:42 +0100338 if (kobj->state_initialized) {
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800339 /* do not error out as sometimes we can recover */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100340 printk(KERN_ERR "kobject (%p): tried to init an initialized "
341 "object, something is seriously wrong.\n", kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800342 dump_stack();
343 }
344
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800345 kobject_init_internal(kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800346 kobj->ktype = ktype;
347 return;
348
349error:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100350 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800351 dump_stack();
352}
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700353EXPORT_SYMBOL(kobject_init);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800354
Nicolas Iooss8db14862015-07-17 16:23:42 -0700355static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
356 struct kobject *parent,
357 const char *fmt, va_list vargs)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800358{
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800359 int retval;
360
Kay Sieversa4ca6612008-04-30 02:06:29 +0200361 retval = kobject_set_name_vargs(kobj, fmt, vargs);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800362 if (retval) {
363 printk(KERN_ERR "kobject: can not set name properly!\n");
364 return retval;
365 }
366 kobj->parent = parent;
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700367 return kobject_add_internal(kobj);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800368}
369
370/**
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700371 * kobject_add - the main kobject add function
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800372 * @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 Assche97057102014-01-04 14:20:04 +0100381 * kobject associated with the kset assigned to this kobject. If no kset
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800382 * 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-Hartman244f6ce2007-12-03 21:31:08 -0800387 * 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 Sievers0f4dafc2007-12-19 01:40:42 +0100390 * Note, no "add" uevent will be created with this call, the caller should set
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800391 * 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-Hartmanb2d6db52007-12-17 23:05:35 -0700395int kobject_add(struct kobject *kobj, struct kobject *parent,
396 const char *fmt, ...)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800397{
398 va_list args;
399 int retval;
400
401 if (!kobj)
402 return -EINVAL;
403
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100404 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-Hartman244f6ce2007-12-03 21:31:08 -0800411 va_start(args, fmt);
412 retval = kobject_add_varg(kobj, parent, fmt, args);
413 va_end(args);
414
415 return retval;
416}
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700417EXPORT_SYMBOL(kobject_add);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800418
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800419/**
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800420 * 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-Hartmanf9cb0742007-12-17 23:05:35 -0700426 * This function combines the call to kobject_init() and
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700427 * 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-Hartmanc11c41542007-12-03 21:31:08 -0800429 */
430int 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-Hartmanf9cb0742007-12-17 23:05:35 -0700436 kobject_init(kobj, ktype);
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800437
438 va_start(args, fmt);
439 retval = kobject_add_varg(kobj, parent, fmt, args);
440 va_end(args);
441
442 return retval;
443}
444EXPORT_SYMBOL_GPL(kobject_init_and_add);
445
446/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800447 * kobject_rename - change the name of an object
448 * @kobj: object in question.
449 * @new_name: object's new name
Eric W. Biederman030c1d22008-05-08 14:41:00 -0700450 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800456int kobject_rename(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int error = 0;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800459 const char *devpath = NULL;
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700460 const char *dup_name = NULL, *name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800461 char *devpath_string = NULL;
462 char *envp[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 kobj = kobject_get(kobj);
465 if (!kobj)
466 return -EINVAL;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700467 if (!kobj->parent)
468 return -EINVAL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800469
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 Tourrilhesca2f37d2007-03-07 10:49:30 -0800483
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800484 name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700485 if (!name) {
486 error = -ENOMEM;
487 goto out;
488 }
489
Tejun Heoe34ff492013-09-11 22:29:05 -0400490 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700491 if (error)
492 goto out;
493
494 /* Install the new kobject name */
495 dup_name = kobj->name;
496 kobj->name = name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800497
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. Biederman0b4a4fe2008-07-03 18:05:28 -0700501 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800502
503out:
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800504 kfree_const(dup_name);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800505 kfree(devpath_string);
506 kfree(devpath);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700507 kobject_put(kobj);
508
509 return error;
510}
Alex Chiang8344b562008-06-10 15:30:42 -0600511EXPORT_SYMBOL_GPL(kobject_rename);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700512
513/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800514 * kobject_move - move object to another parent
515 * @kobj: object in question.
516 * @new_parent: object's new parent (can be NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +0100517 */
Cornelia Huck8a824722006-11-20 17:07:51 +0100518int 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 Huckc744aeae2007-01-08 20:16:44 +0100531 if (kobj->kset)
532 new_parent = kobject_get(&kobj->kset->kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +0100533 }
Tejun Heoe34ff492013-09-11 22:29:05 -0400534
Cornelia Huck8a824722006-11-20 17:07:51 +0100535 /* 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 Heoe34ff492013-09-11 22:29:05 -0400549 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
Cornelia Huck8a824722006-11-20 17:07:51 +0100550 if (error)
551 goto out;
552 old_parent = kobj->parent;
553 kobj->parent = new_parent;
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300554 new_parent = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100555 kobject_put(old_parent);
556 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
557out:
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300558 kobject_put(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +0100559 kobject_put(kobj);
560 kfree(devpath_string);
561 kfree(devpath);
562 return error;
563}
Anand Jain24199d22015-02-12 07:03:37 +0800564EXPORT_SYMBOL_GPL(kobject_move);
Cornelia Huck8a824722006-11-20 17:07:51 +0100565
566/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800567 * kobject_del - unlink kobject from hierarchy.
568 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800570void kobject_del(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Tejun Heo324a56e2013-12-11 14:11:53 -0500572 struct kernfs_node *sd;
Tejun Heo26ea12d2013-09-18 17:15:36 -0400573
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800574 if (!kobj)
575 return;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100576
Tejun Heo26ea12d2013-09-18 17:15:36 -0400577 sd = kobj->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 sysfs_remove_dir(kobj);
Tejun Heo26ea12d2013-09-18 17:15:36 -0400579 sysfs_put(sd);
580
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100581 kobj->state_in_sysfs = 0;
582 kobj_kset_leave(kobj);
583 kobject_put(kobj->parent);
584 kobj->parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400586EXPORT_SYMBOL(kobject_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800589 * kobject_get - increment refcount for object.
590 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800592struct kobject *kobject_get(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Ethan Zhaod82d54a2015-03-12 13:04:16 +0900594 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 Torvalds1da177e2005-04-16 15:20:36 -0700599 kref_get(&kobj->kref);
Ethan Zhaod82d54a2015-03-12 13:04:16 +0900600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return kobj;
602}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400603EXPORT_SYMBOL(kobject_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Jan Karac70c1762017-03-23 01:37:01 +0100605struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700606{
Jan Karac70c1762017-03-23 01:37:01 +0100607 if (!kobj)
608 return NULL;
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700609 if (!kref_get_unless_zero(&kobj->kref))
610 kobj = NULL;
611 return kobj;
612}
Jan Karac70c1762017-03-23 01:37:01 +0100613EXPORT_SYMBOL(kobject_get_unless_zero);
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700614
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800615/*
616 * kobject_cleanup - free kobject resources.
617 * @kobj: object to cleanup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800619static void kobject_cleanup(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100621 struct kobj_type *t = get_ktype(kobj);
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100622 const char *name = kobj->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Russell Kingc817a672013-06-27 15:06:14 +0100624 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
625 kobject_name(kobj), kobj, __func__, kobj->parent);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100626
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-Hartmance2c9cb2007-09-12 15:06:57 -0700646 if (t && t->release) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100647 pr_debug("kobject: '%s' (%p): calling ktype release\n",
648 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 t->release(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100650 }
651
652 /* free name if we allocated it */
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100653 if (name) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100654 pr_debug("kobject: '%s': free name\n", name);
Rasmus Villemoesf773f322015-11-06 16:31:23 -0800655 kfree_const(name);
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Russell Kingc817a672013-06-27 15:06:14 +0100659#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
660static 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 Torvalds1da177e2005-04-16 15:20:36 -0700667static void kobject_release(struct kref *kref)
668{
Russell Kingc817a672013-06-27 15:06:14 +0100669 struct kobject *kobj = container_of(kref, struct kobject, kref);
670#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
Bjorn Helgaas89c86a62013-12-05 17:37:51 -0700671 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 Kingc817a672013-06-27 15:06:14 +0100674 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
Bjorn Helgaas89c86a62013-12-05 17:37:51 -0700675
676 schedule_delayed_work(&kobj->release, delay);
Russell Kingc817a672013-06-27 15:06:14 +0100677#else
678 kobject_cleanup(kobj);
679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800683 * kobject_put - decrement refcount for object.
684 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800686 * Decrement the refcount, and if 0, call kobject_cleanup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800688void kobject_put(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800690 if (kobj) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700691 if (!kobj->state_initialized)
692 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800693 "initialized, yet kobject_put() is being "
694 "called.\n", kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 kref_put(&kobj->kref, kobject_release);
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400698EXPORT_SYMBOL(kobject_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800700static void dynamic_kobj_release(struct kobject *kobj)
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500701{
Harvey Harrison810304d2008-04-30 00:55:08 -0700702 pr_debug("kobject: (%p): %s\n", kobj, __func__);
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500703 kfree(kobj);
704}
705
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800706static struct kobj_type dynamic_kobj_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100707 .release = dynamic_kobj_release,
708 .sysfs_ops = &kobj_sysfs_ops,
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500709};
710
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800711/**
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800712 * 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-Hartman43968d22007-11-05 22:24:43 -0800718 * The kobject structure returned from here must be cleaned up with a
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700719 * call to kobject_put() and not kfree(), as kobject_init() has
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800720 * already been called on this structure.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800721 */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800722struct kobject *kobject_create(void)
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800723{
724 struct kobject *kobj;
725
726 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
727 if (!kobj)
728 return NULL;
729
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700730 kobject_init(kobj, &dynamic_kobj_ktype);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800731 return kobj;
732}
733
734/**
735 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
736 *
Zhi Yong Wu9ff1f832012-05-07 10:48:25 +0800737 * @name: the name for the kobject
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800738 * @parent: the parent kobject of this kobject, if any.
739 *
Dave Youngf70701a2008-01-28 16:58:00 +0800740 * This function creates a kobject structure dynamically and registers it
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800741 * with sysfs. When you are finished with this structure, call
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800742 * kobject_put() and the structure will be dynamically freed when
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800743 * it is no longer being used.
744 *
745 * If the kobject was not able to be created, NULL will be returned.
746 */
747struct 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-Hartmanb2d6db52007-12-17 23:05:35 -0700756 retval = kobject_add(kobj, parent, "%s", name);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800757 if (retval) {
758 printk(KERN_WARNING "%s: kobject_add error: %d\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700759 __func__, retval);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800760 kobject_put(kobj);
761 kobj = NULL;
762 }
763 return kobj;
764}
765EXPORT_SYMBOL_GPL(kobject_create_and_add);
766
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500767/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800768 * kset_init - initialize a kset for use
769 * @k: kset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800771void kset_init(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Greg Kroah-Hartmane1543dd2007-12-17 23:05:35 -0700773 kobject_init_internal(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 INIT_LIST_HEAD(&k->list);
775 spin_lock_init(&k->list_lock);
776}
777
Kay Sievers23b52122007-11-02 13:47:53 +0100778/* default kobject attribute operations */
779static 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
791static 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 Revfy52cf25d2010-01-19 02:58:23 +0100803const struct sysfs_ops kobj_sysfs_ops = {
Kay Sievers23b52122007-11-02 13:47:53 +0100804 .show = kobj_attr_show,
805 .store = kobj_attr_store,
806};
Jeff Mahoney29dfe2d2013-11-01 13:06:56 -0400807EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800810 * kset_register - initialize and add a kset.
811 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800813int kset_register(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Kay Sievers80f03e32007-05-26 11:21:36 +0200815 int err;
816
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800817 if (!k)
818 return -EINVAL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 kset_init(k);
Greg Kroah-Hartman12e339a2002-04-09 12:14:34 -0700821 err = kobject_add_internal(&k->kobj);
Kay Sievers80f03e32007-05-26 11:21:36 +0200822 if (err)
823 return err;
824 kobject_uevent(&k->kobj, KOBJ_ADD);
825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400827EXPORT_SYMBOL(kset_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800830 * kset_unregister - remove a kset.
831 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800833void kset_unregister(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800835 if (!k)
836 return;
Bjorn Helgaas35a5fe62013-12-05 17:38:00 -0700837 kobject_del(&k->kobj);
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800838 kobject_put(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
Gabriel Somlofa40ae32015-08-10 15:51:43 -0400840EXPORT_SYMBOL(kset_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800843 * kset_find_obj - search for object in kset.
844 * @kset: kset we're looking in.
845 * @name: object's name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800847 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700850 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800851struct kobject *kset_find_obj(struct kset *kset, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400853 struct kobject *k;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800854 struct kobject *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 spin_lock(&kset->list_lock);
Robin Holtc25d1df2010-09-29 14:00:54 -0500857
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400858 list_for_each_entry(k, &kset->list, entry) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800859 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700860 ret = kobject_get_unless_zero(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 }
863 }
Robin Holtc25d1df2010-09-29 14:00:54 -0500864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 spin_unlock(&kset->list_lock);
866 return ret;
867}
Gabriel Somlo2fe829a2016-01-28 09:23:12 -0500868EXPORT_SYMBOL_GPL(kset_find_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700870static void kset_release(struct kobject *kobj)
871{
872 struct kset *kset = container_of(kobj, struct kset, kobj);
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800873 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700874 kobject_name(kobj), kobj, __func__);
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700875 kfree(kset);
876}
877
Kay Sievers386f2752007-11-02 13:47:53 +0100878static struct kobj_type kset_ktype = {
879 .sysfs_ops = &kobj_sysfs_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700880 .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 */
898static struct kset *kset_create(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100899 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700900 struct kobject *parent_kobj)
901{
902 struct kset *kset;
Dave Youngd9cd8f32009-05-11 14:17:45 +0800903 int retval;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700904
905 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
906 if (!kset)
907 return NULL;
Kees Cookb7165eb2013-06-06 13:52:19 -0700908 retval = kobject_set_name(&kset->kobj, "%s", name);
Dave Youngd9cd8f32009-05-11 14:17:45 +0800909 if (retval) {
910 kfree(kset);
911 return NULL;
912 }
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700913 kset->uevent_ops = uevent_ops;
914 kset->kobj.parent = parent_kobj;
915
916 /*
Kay Sievers386f2752007-11-02 13:47:53 +0100917 * The kobject of this kset will have a type of kset_ktype and belong to
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700918 * no kset itself. That way we can properly free it when it is
919 * finished being used.
920 */
Kay Sievers386f2752007-11-02 13:47:53 +0100921 kset->kobj.ktype = &kset_ktype;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700922 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 */
941struct kset *kset_create_and_add(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100942 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700943 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}
958EXPORT_SYMBOL_GPL(kset_create_and_add);
959
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700960
961static DEFINE_SPINLOCK(kobj_ns_type_lock);
962static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
963
964int 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
986out:
987 spin_unlock(&kobj_ns_type_lock);
988 return error;
989}
990
991int 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
1003const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
1004{
1005 const struct kobj_ns_type_operations *ops = NULL;
1006
Pankaj Dubey41fb96a2014-09-24 16:25:54 +05301007 if (parent && parent->ktype && parent->ktype->child_ns_type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001008 ops = parent->ktype->child_ns_type(parent);
1009
1010 return ops;
1011}
1012
1013const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
1014{
1015 return kobj_child_ns_ops(kobj->parent);
1016}
1017
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001018bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1019{
Eric W. Biederman730d7d32013-09-23 14:41:17 -07001020 bool may_mount = true;
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001021
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. Biedermanbc451f22010-03-30 11:31:25 -07001030
Al Viroa685e082011-06-08 21:13:01 -04001031void *kobj_ns_grab_current(enum kobj_ns_type type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001032{
Al Viroa685e082011-06-08 21:13:01 -04001033 void *ns = NULL;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001034
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 Viroa685e082011-06-08 21:13:01 -04001038 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001039 spin_unlock(&kobj_ns_type_lock);
1040
1041 return ns;
1042}
1043
1044const 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
1057const 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 Viroa685e082011-06-08 21:13:01 -04001070void kobj_ns_drop(enum kobj_ns_type type, void *ns)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001071{
Al Viroa685e082011-06-08 21:13:01 -04001072 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. Biedermanbc451f22010-03-30 11:31:25 -07001077}