blob: 1d110dc95db544e8757d610faf3f5bbad7f9af20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kobject.c - library routines for handling generic kernel objects
3 *
4 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
Greg Kroah-Hartmanf0e7e1b2007-09-27 14:48:53 -07005 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (c) 2006-2007 Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This file is released under the GPLv2.
9 *
10 *
11 * Please see the file Documentation/kobject.txt for critical information
12 * about using the kobject interface.
13 */
14
15#include <linux/kobject.h>
Jeff Mahoneyeee03162013-09-11 13:00:30 -040016#include <linux/kobj_completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Heo93b2b8e2013-11-28 14:54:15 -050098 kernfs_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 Sieversaf5ca3f2007-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{
Kay Sievers9f255652008-05-06 22:24:04 +0200261 const char *old_name = kobj->name;
262 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Kay Sievers8a577ff2009-04-18 15:05:45 -0700264 if (kobj->name && !fmt)
265 return 0;
266
Kay Sieversa4ca6612008-04-30 02:06:29 +0200267 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
268 if (!kobj->name)
269 return -ENOMEM;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500270
Kay Sievers9f255652008-05-06 22:24:04 +0200271 /* ewww... some of these buggers have '/' in the name ... */
Ingo Oeser25fdeb32008-07-23 01:25:01 +0200272 while ((s = strchr(kobj->name, '/')))
Kay Sievers9f255652008-05-06 22:24:04 +0200273 s[0] = '!';
274
275 kfree(old_name);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500276 return 0;
277}
278
279/**
280 * kobject_set_name - Set the name of a kobject
281 * @kobj: struct kobject to set the name of
282 * @fmt: format string used to build the name
283 *
284 * This sets the name of the kobject. If you have already added the
285 * kobject to the system, you must call kobject_rename() in order to
286 * change the name of the kobject.
287 */
288int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
289{
Kay Sieversa4ca6612008-04-30 02:06:29 +0200290 va_list vargs;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500291 int retval;
292
Kay Sieversa4ca6612008-04-30 02:06:29 +0200293 va_start(vargs, fmt);
294 retval = kobject_set_name_vargs(kobj, fmt, vargs);
295 va_end(vargs);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500296
297 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299EXPORT_SYMBOL(kobject_set_name);
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/**
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700302 * kobject_init - initialize a kobject structure
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800303 * @kobj: pointer to the kobject to initialize
304 * @ktype: pointer to the ktype for this kobject.
305 *
306 * This function will properly initialize a kobject such that it can then
307 * be passed to the kobject_add() call.
308 *
309 * After this function is called, the kobject MUST be cleaned up by a call
310 * to kobject_put(), not by a call to kfree directly to ensure that all of
311 * the memory is cleaned up properly.
312 */
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700313void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800314{
315 char *err_str;
316
317 if (!kobj) {
318 err_str = "invalid kobject pointer!";
319 goto error;
320 }
321 if (!ktype) {
322 err_str = "must have a ktype to be initialized properly!\n";
323 goto error;
324 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100325 if (kobj->state_initialized) {
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800326 /* do not error out as sometimes we can recover */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100327 printk(KERN_ERR "kobject (%p): tried to init an initialized "
328 "object, something is seriously wrong.\n", kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800329 dump_stack();
330 }
331
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800332 kobject_init_internal(kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800333 kobj->ktype = ktype;
334 return;
335
336error:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100337 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800338 dump_stack();
339}
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700340EXPORT_SYMBOL(kobject_init);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800341
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800342static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
343 const char *fmt, va_list vargs)
344{
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800345 int retval;
346
Kay Sieversa4ca6612008-04-30 02:06:29 +0200347 retval = kobject_set_name_vargs(kobj, fmt, vargs);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800348 if (retval) {
349 printk(KERN_ERR "kobject: can not set name properly!\n");
350 return retval;
351 }
352 kobj->parent = parent;
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700353 return kobject_add_internal(kobj);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800354}
355
356/**
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700357 * kobject_add - the main kobject add function
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800358 * @kobj: the kobject to add
359 * @parent: pointer to the parent of the kobject.
360 * @fmt: format to name the kobject with.
361 *
362 * The kobject name is set and added to the kobject hierarchy in this
363 * function.
364 *
365 * If @parent is set, then the parent of the @kobj will be set to it.
366 * If @parent is NULL, then the parent of the @kobj will be set to the
367 * kobject associted with the kset assigned to this kobject. If no kset
368 * is assigned to the kobject, then the kobject will be located in the
369 * root of the sysfs tree.
370 *
371 * If this function returns an error, kobject_put() must be called to
372 * properly clean up the memory associated with the object.
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800373 * Under no instance should the kobject that is passed to this function
374 * be directly freed with a call to kfree(), that can leak memory.
375 *
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100376 * Note, no "add" uevent will be created with this call, the caller should set
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800377 * up all of the necessary sysfs files for the object and then call
378 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
379 * userspace is properly notified of this kobject's creation.
380 */
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700381int kobject_add(struct kobject *kobj, struct kobject *parent,
382 const char *fmt, ...)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800383{
384 va_list args;
385 int retval;
386
387 if (!kobj)
388 return -EINVAL;
389
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100390 if (!kobj->state_initialized) {
391 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
392 "uninitialized object, something is seriously wrong.\n",
393 kobject_name(kobj), kobj);
394 dump_stack();
395 return -EINVAL;
396 }
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800397 va_start(args, fmt);
398 retval = kobject_add_varg(kobj, parent, fmt, args);
399 va_end(args);
400
401 return retval;
402}
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700403EXPORT_SYMBOL(kobject_add);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800404
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800405/**
Greg Kroah-Hartmanc11c4152007-12-03 21:31:08 -0800406 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
407 * @kobj: pointer to the kobject to initialize
408 * @ktype: pointer to the ktype for this kobject.
409 * @parent: pointer to the parent of this kobject.
410 * @fmt: the name of the kobject.
411 *
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700412 * This function combines the call to kobject_init() and
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700413 * kobject_add(). The same type of error handling after a call to
414 * kobject_add() and kobject lifetime rules are the same here.
Greg Kroah-Hartmanc11c4152007-12-03 21:31:08 -0800415 */
416int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
417 struct kobject *parent, const char *fmt, ...)
418{
419 va_list args;
420 int retval;
421
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700422 kobject_init(kobj, ktype);
Greg Kroah-Hartmanc11c4152007-12-03 21:31:08 -0800423
424 va_start(args, fmt);
425 retval = kobject_add_varg(kobj, parent, fmt, args);
426 va_end(args);
427
428 return retval;
429}
430EXPORT_SYMBOL_GPL(kobject_init_and_add);
431
432/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800433 * kobject_rename - change the name of an object
434 * @kobj: object in question.
435 * @new_name: object's new name
Eric W. Biederman030c1d22008-05-08 14:41:00 -0700436 *
437 * It is the responsibility of the caller to provide mutual
438 * exclusion between two different calls of kobject_rename
439 * on the same kobject and to ensure that new_name is valid and
440 * won't conflict with other kobjects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800442int kobject_rename(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 int error = 0;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800445 const char *devpath = NULL;
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700446 const char *dup_name = NULL, *name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800447 char *devpath_string = NULL;
448 char *envp[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 kobj = kobject_get(kobj);
451 if (!kobj)
452 return -EINVAL;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700453 if (!kobj->parent)
454 return -EINVAL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800455
456 devpath = kobject_get_path(kobj, GFP_KERNEL);
457 if (!devpath) {
458 error = -ENOMEM;
459 goto out;
460 }
461 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
462 if (!devpath_string) {
463 error = -ENOMEM;
464 goto out;
465 }
466 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
467 envp[0] = devpath_string;
468 envp[1] = NULL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800469
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700470 name = dup_name = kstrdup(new_name, GFP_KERNEL);
471 if (!name) {
472 error = -ENOMEM;
473 goto out;
474 }
475
Tejun Heoe34ff492013-09-11 22:29:05 -0400476 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700477 if (error)
478 goto out;
479
480 /* Install the new kobject name */
481 dup_name = kobj->name;
482 kobj->name = name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800483
484 /* This function is mostly/only used for network interface.
485 * Some hotplug package track interfaces by their name and
486 * therefore want to know when the name is changed by the user. */
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700487 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800488
489out:
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700490 kfree(dup_name);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800491 kfree(devpath_string);
492 kfree(devpath);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700493 kobject_put(kobj);
494
495 return error;
496}
Alex Chiang8344b562008-06-10 15:30:42 -0600497EXPORT_SYMBOL_GPL(kobject_rename);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700498
499/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800500 * kobject_move - move object to another parent
501 * @kobj: object in question.
502 * @new_parent: object's new parent (can be NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +0100503 */
Cornelia Huck8a824722006-11-20 17:07:51 +0100504int kobject_move(struct kobject *kobj, struct kobject *new_parent)
505{
506 int error;
507 struct kobject *old_parent;
508 const char *devpath = NULL;
509 char *devpath_string = NULL;
510 char *envp[2];
511
512 kobj = kobject_get(kobj);
513 if (!kobj)
514 return -EINVAL;
515 new_parent = kobject_get(new_parent);
516 if (!new_parent) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100517 if (kobj->kset)
518 new_parent = kobject_get(&kobj->kset->kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +0100519 }
Tejun Heoe34ff492013-09-11 22:29:05 -0400520
Cornelia Huck8a824722006-11-20 17:07:51 +0100521 /* old object path */
522 devpath = kobject_get_path(kobj, GFP_KERNEL);
523 if (!devpath) {
524 error = -ENOMEM;
525 goto out;
526 }
527 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
528 if (!devpath_string) {
529 error = -ENOMEM;
530 goto out;
531 }
532 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
533 envp[0] = devpath_string;
534 envp[1] = NULL;
Tejun Heoe34ff492013-09-11 22:29:05 -0400535 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
Cornelia Huck8a824722006-11-20 17:07:51 +0100536 if (error)
537 goto out;
538 old_parent = kobj->parent;
539 kobj->parent = new_parent;
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300540 new_parent = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100541 kobject_put(old_parent);
542 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
543out:
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300544 kobject_put(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +0100545 kobject_put(kobj);
546 kfree(devpath_string);
547 kfree(devpath);
548 return error;
549}
550
551/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800552 * kobject_del - unlink kobject from hierarchy.
553 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800555void kobject_del(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Tejun Heo26ea12d2013-09-18 17:15:36 -0400557 struct sysfs_dirent *sd;
558
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800559 if (!kobj)
560 return;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100561
Tejun Heo26ea12d2013-09-18 17:15:36 -0400562 sd = kobj->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 sysfs_remove_dir(kobj);
Tejun Heo26ea12d2013-09-18 17:15:36 -0400564 sysfs_put(sd);
565
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100566 kobj->state_in_sysfs = 0;
567 kobj_kset_leave(kobj);
568 kobject_put(kobj->parent);
569 kobj->parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800573 * kobject_get - increment refcount for object.
574 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800576struct kobject *kobject_get(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 if (kobj)
579 kref_get(&kobj->kref);
580 return kobj;
581}
582
Anatol Pomozov2d864e42013-05-07 15:37:48 -0700583static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700584{
585 if (!kref_get_unless_zero(&kobj->kref))
586 kobj = NULL;
587 return kobj;
588}
589
Greg Kroah-Hartman18041f42007-12-03 21:31:08 -0800590/*
591 * kobject_cleanup - free kobject resources.
592 * @kobj: object to cleanup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Greg Kroah-Hartman18041f42007-12-03 21:31:08 -0800594static void kobject_cleanup(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100596 struct kobj_type *t = get_ktype(kobj);
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100597 const char *name = kobj->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Russell Kingc817a672013-06-27 15:06:14 +0100599 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
600 kobject_name(kobj), kobj, __func__, kobj->parent);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100601
602 if (t && !t->release)
603 pr_debug("kobject: '%s' (%p): does not have a release() "
604 "function, it is broken and must be fixed.\n",
605 kobject_name(kobj), kobj);
606
607 /* send "remove" if the caller did not do it but sent "add" */
608 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
609 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
610 kobject_name(kobj), kobj);
611 kobject_uevent(kobj, KOBJ_REMOVE);
612 }
613
614 /* remove from sysfs if the caller did not do it */
615 if (kobj->state_in_sysfs) {
616 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
617 kobject_name(kobj), kobj);
618 kobject_del(kobj);
619 }
620
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700621 if (t && t->release) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100622 pr_debug("kobject: '%s' (%p): calling ktype release\n",
623 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 t->release(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100625 }
626
627 /* free name if we allocated it */
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100628 if (name) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100629 pr_debug("kobject: '%s': free name\n", name);
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700630 kfree(name);
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Russell Kingc817a672013-06-27 15:06:14 +0100634#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
635static void kobject_delayed_cleanup(struct work_struct *work)
636{
637 kobject_cleanup(container_of(to_delayed_work(work),
638 struct kobject, release));
639}
640#endif
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static void kobject_release(struct kref *kref)
643{
Russell Kingc817a672013-06-27 15:06:14 +0100644 struct kobject *kobj = container_of(kref, struct kobject, kref);
645#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
Bjorn Helgaas89c86a62013-12-05 17:37:51 -0700646 unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
647 pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
648 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
Russell Kingc817a672013-06-27 15:06:14 +0100649 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
Bjorn Helgaas89c86a62013-12-05 17:37:51 -0700650
651 schedule_delayed_work(&kobj->release, delay);
Russell Kingc817a672013-06-27 15:06:14 +0100652#else
653 kobject_cleanup(kobj);
654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800658 * kobject_put - decrement refcount for object.
659 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800661 * Decrement the refcount, and if 0, call kobject_cleanup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800663void kobject_put(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800665 if (kobj) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700666 if (!kobj->state_initialized)
667 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800668 "initialized, yet kobject_put() is being "
669 "called.\n", kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 kref_put(&kobj->kref, kobject_release);
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800674static void dynamic_kobj_release(struct kobject *kobj)
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500675{
Harvey Harrison810304d2008-04-30 00:55:08 -0700676 pr_debug("kobject: (%p): %s\n", kobj, __func__);
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500677 kfree(kobj);
678}
679
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800680static struct kobj_type dynamic_kobj_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100681 .release = dynamic_kobj_release,
682 .sysfs_ops = &kobj_sysfs_ops,
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500683};
684
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800685/**
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800686 * kobject_create - create a struct kobject dynamically
687 *
688 * This function creates a kobject structure dynamically and sets it up
689 * to be a "dynamic" kobject with a default release function set up.
690 *
691 * If the kobject was not able to be created, NULL will be returned.
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800692 * The kobject structure returned from here must be cleaned up with a
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700693 * call to kobject_put() and not kfree(), as kobject_init() has
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800694 * already been called on this structure.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800695 */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800696struct kobject *kobject_create(void)
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800697{
698 struct kobject *kobj;
699
700 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
701 if (!kobj)
702 return NULL;
703
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700704 kobject_init(kobj, &dynamic_kobj_ktype);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800705 return kobj;
706}
707
708/**
709 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
710 *
Zhi Yong Wu9ff1f832012-05-07 10:48:25 +0800711 * @name: the name for the kobject
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800712 * @parent: the parent kobject of this kobject, if any.
713 *
Dave Youngf70701a2008-01-28 16:58:00 +0800714 * This function creates a kobject structure dynamically and registers it
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800715 * with sysfs. When you are finished with this structure, call
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800716 * kobject_put() and the structure will be dynamically freed when
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800717 * it is no longer being used.
718 *
719 * If the kobject was not able to be created, NULL will be returned.
720 */
721struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
722{
723 struct kobject *kobj;
724 int retval;
725
726 kobj = kobject_create();
727 if (!kobj)
728 return NULL;
729
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700730 retval = kobject_add(kobj, parent, "%s", name);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800731 if (retval) {
732 printk(KERN_WARNING "%s: kobject_add error: %d\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700733 __func__, retval);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800734 kobject_put(kobj);
735 kobj = NULL;
736 }
737 return kobj;
738}
739EXPORT_SYMBOL_GPL(kobject_create_and_add);
740
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500741/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800742 * kset_init - initialize a kset for use
743 * @k: kset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800745void kset_init(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Greg Kroah-Hartmane1543dd2007-12-17 23:05:35 -0700747 kobject_init_internal(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 INIT_LIST_HEAD(&k->list);
749 spin_lock_init(&k->list_lock);
750}
751
Kay Sievers23b52122007-11-02 13:47:53 +0100752/* default kobject attribute operations */
753static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
754 char *buf)
755{
756 struct kobj_attribute *kattr;
757 ssize_t ret = -EIO;
758
759 kattr = container_of(attr, struct kobj_attribute, attr);
760 if (kattr->show)
761 ret = kattr->show(kobj, kattr, buf);
762 return ret;
763}
764
765static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
766 const char *buf, size_t count)
767{
768 struct kobj_attribute *kattr;
769 ssize_t ret = -EIO;
770
771 kattr = container_of(attr, struct kobj_attribute, attr);
772 if (kattr->store)
773 ret = kattr->store(kobj, kattr, buf, count);
774 return ret;
775}
776
Emese Revfy52cf25d2010-01-19 02:58:23 +0100777const struct sysfs_ops kobj_sysfs_ops = {
Kay Sievers23b52122007-11-02 13:47:53 +0100778 .show = kobj_attr_show,
779 .store = kobj_attr_store,
780};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/**
Jeff Mahoneyeee03162013-09-11 13:00:30 -0400783 * kobj_completion_init - initialize a kobj_completion object.
784 * @kc: kobj_completion
785 * @ktype: type of kobject to initialize
786 *
787 * kobj_completion structures can be embedded within structures with different
788 * lifetime rules. During the release of the enclosing object, we can
789 * wait on the release of the kobject so that we don't free it while it's
790 * still busy.
791 */
792void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype)
793{
794 init_completion(&kc->kc_unregister);
795 kobject_init(&kc->kc_kobj, ktype);
796}
797EXPORT_SYMBOL_GPL(kobj_completion_init);
798
799/**
800 * kobj_completion_release - release a kobj_completion object
801 * @kobj: kobject embedded in kobj_completion
802 *
803 * Used with kobject_release to notify waiters that the kobject has been
804 * released.
805 */
806void kobj_completion_release(struct kobject *kobj)
807{
808 struct kobj_completion *kc = kobj_to_kobj_completion(kobj);
809 complete(&kc->kc_unregister);
810}
811EXPORT_SYMBOL_GPL(kobj_completion_release);
812
813/**
814 * kobj_completion_del_and_wait - release the kobject and wait for it
815 * @kc: kobj_completion object to release
816 *
817 * Delete the kobject from sysfs and drop the reference count. Then wait
818 * until any other outstanding references are also dropped. This routine
819 * is only necessary once other references may have been taken on the
820 * kobject. Typically this happens when the kobject has been published
821 * to sysfs via kobject_add.
822 */
823void kobj_completion_del_and_wait(struct kobj_completion *kc)
824{
825 kobject_del(&kc->kc_kobj);
826 kobject_put(&kc->kc_kobj);
827 wait_for_completion(&kc->kc_unregister);
828}
829EXPORT_SYMBOL_GPL(kobj_completion_del_and_wait);
830
831/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800832 * kset_register - initialize and add a kset.
833 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800835int kset_register(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Kay Sievers80f03e32007-05-26 11:21:36 +0200837 int err;
838
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800839 if (!k)
840 return -EINVAL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 kset_init(k);
Greg Kroah-Hartman12e339a2002-04-09 12:14:34 -0700843 err = kobject_add_internal(&k->kobj);
Kay Sievers80f03e32007-05-26 11:21:36 +0200844 if (err)
845 return err;
846 kobject_uevent(&k->kobj, KOBJ_ADD);
847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800851 * kset_unregister - remove a kset.
852 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800854void kset_unregister(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800856 if (!k)
857 return;
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800858 kobject_put(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800862 * kset_find_obj - search for object in kset.
863 * @kset: kset we're looking in.
864 * @name: object's name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800866 * Lock kset via @kset->subsys, and iterate over @kset->list,
867 * looking for a matching kobject. If matching object is found
868 * take a reference and return the object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800870struct kobject *kset_find_obj(struct kset *kset, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400872 struct kobject *k;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800873 struct kobject *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 spin_lock(&kset->list_lock);
Robin Holtc25d1df2010-09-29 14:00:54 -0500876
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400877 list_for_each_entry(k, &kset->list, entry) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800878 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700879 ret = kobject_get_unless_zero(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
881 }
882 }
Robin Holtc25d1df2010-09-29 14:00:54 -0500883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 spin_unlock(&kset->list_lock);
885 return ret;
886}
887
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700888static void kset_release(struct kobject *kobj)
889{
890 struct kset *kset = container_of(kobj, struct kset, kobj);
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800891 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700892 kobject_name(kobj), kobj, __func__);
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700893 kfree(kset);
894}
895
Kay Sievers386f2752007-11-02 13:47:53 +0100896static struct kobj_type kset_ktype = {
897 .sysfs_ops = &kobj_sysfs_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700898 .release = kset_release,
899};
900
901/**
902 * kset_create - create a struct kset dynamically
903 *
904 * @name: the name for the kset
905 * @uevent_ops: a struct kset_uevent_ops for the kset
906 * @parent_kobj: the parent kobject of this kset, if any.
907 *
908 * This function creates a kset structure dynamically. This structure can
909 * then be registered with the system and show up in sysfs with a call to
910 * kset_register(). When you are finished with this structure, if
911 * kset_register() has been called, call kset_unregister() and the
912 * structure will be dynamically freed when it is no longer being used.
913 *
914 * If the kset was not able to be created, NULL will be returned.
915 */
916static struct kset *kset_create(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100917 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700918 struct kobject *parent_kobj)
919{
920 struct kset *kset;
Dave Youngd9cd8f32009-05-11 14:17:45 +0800921 int retval;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700922
923 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
924 if (!kset)
925 return NULL;
Kees Cookb7165eb2013-06-06 13:52:19 -0700926 retval = kobject_set_name(&kset->kobj, "%s", name);
Dave Youngd9cd8f32009-05-11 14:17:45 +0800927 if (retval) {
928 kfree(kset);
929 return NULL;
930 }
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700931 kset->uevent_ops = uevent_ops;
932 kset->kobj.parent = parent_kobj;
933
934 /*
Kay Sievers386f2752007-11-02 13:47:53 +0100935 * The kobject of this kset will have a type of kset_ktype and belong to
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700936 * no kset itself. That way we can properly free it when it is
937 * finished being used.
938 */
Kay Sievers386f2752007-11-02 13:47:53 +0100939 kset->kobj.ktype = &kset_ktype;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700940 kset->kobj.kset = NULL;
941
942 return kset;
943}
944
945/**
946 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
947 *
948 * @name: the name for the kset
949 * @uevent_ops: a struct kset_uevent_ops for the kset
950 * @parent_kobj: the parent kobject of this kset, if any.
951 *
952 * This function creates a kset structure dynamically and registers it
953 * with sysfs. When you are finished with this structure, call
954 * kset_unregister() and the structure will be dynamically freed when it
955 * is no longer being used.
956 *
957 * If the kset was not able to be created, NULL will be returned.
958 */
959struct kset *kset_create_and_add(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100960 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700961 struct kobject *parent_kobj)
962{
963 struct kset *kset;
964 int error;
965
966 kset = kset_create(name, uevent_ops, parent_kobj);
967 if (!kset)
968 return NULL;
969 error = kset_register(kset);
970 if (error) {
971 kfree(kset);
972 return NULL;
973 }
974 return kset;
975}
976EXPORT_SYMBOL_GPL(kset_create_and_add);
977
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700978
979static DEFINE_SPINLOCK(kobj_ns_type_lock);
980static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
981
982int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
983{
984 enum kobj_ns_type type = ops->type;
985 int error;
986
987 spin_lock(&kobj_ns_type_lock);
988
989 error = -EINVAL;
990 if (type >= KOBJ_NS_TYPES)
991 goto out;
992
993 error = -EINVAL;
994 if (type <= KOBJ_NS_TYPE_NONE)
995 goto out;
996
997 error = -EBUSY;
998 if (kobj_ns_ops_tbl[type])
999 goto out;
1000
1001 error = 0;
1002 kobj_ns_ops_tbl[type] = ops;
1003
1004out:
1005 spin_unlock(&kobj_ns_type_lock);
1006 return error;
1007}
1008
1009int kobj_ns_type_registered(enum kobj_ns_type type)
1010{
1011 int registered = 0;
1012
1013 spin_lock(&kobj_ns_type_lock);
1014 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
1015 registered = kobj_ns_ops_tbl[type] != NULL;
1016 spin_unlock(&kobj_ns_type_lock);
1017
1018 return registered;
1019}
1020
1021const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
1022{
1023 const struct kobj_ns_type_operations *ops = NULL;
1024
1025 if (parent && parent->ktype->child_ns_type)
1026 ops = parent->ktype->child_ns_type(parent);
1027
1028 return ops;
1029}
1030
1031const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
1032{
1033 return kobj_child_ns_ops(kobj->parent);
1034}
1035
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001036bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1037{
Eric W. Biederman730d7d32013-09-23 14:41:17 -07001038 bool may_mount = true;
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001039
1040 spin_lock(&kobj_ns_type_lock);
1041 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1042 kobj_ns_ops_tbl[type])
1043 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
1044 spin_unlock(&kobj_ns_type_lock);
1045
1046 return may_mount;
1047}
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001048
Al Viroa685e082011-06-08 21:13:01 -04001049void *kobj_ns_grab_current(enum kobj_ns_type type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001050{
Al Viroa685e082011-06-08 21:13:01 -04001051 void *ns = NULL;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001052
1053 spin_lock(&kobj_ns_type_lock);
1054 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1055 kobj_ns_ops_tbl[type])
Al Viroa685e082011-06-08 21:13:01 -04001056 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001057 spin_unlock(&kobj_ns_type_lock);
1058
1059 return ns;
1060}
1061
1062const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
1063{
1064 const void *ns = NULL;
1065
1066 spin_lock(&kobj_ns_type_lock);
1067 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1068 kobj_ns_ops_tbl[type])
1069 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
1070 spin_unlock(&kobj_ns_type_lock);
1071
1072 return ns;
1073}
1074
1075const void *kobj_ns_initial(enum kobj_ns_type type)
1076{
1077 const void *ns = NULL;
1078
1079 spin_lock(&kobj_ns_type_lock);
1080 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1081 kobj_ns_ops_tbl[type])
1082 ns = kobj_ns_ops_tbl[type]->initial_ns();
1083 spin_unlock(&kobj_ns_type_lock);
1084
1085 return ns;
1086}
1087
Al Viroa685e082011-06-08 21:13:01 -04001088void kobj_ns_drop(enum kobj_ns_type type, void *ns)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001089{
Al Viroa685e082011-06-08 21:13:01 -04001090 spin_lock(&kobj_ns_type_lock);
1091 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1092 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1093 kobj_ns_ops_tbl[type]->drop_ns(ns);
1094 spin_unlock(&kobj_ns_type_lock);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097EXPORT_SYMBOL(kobject_get);
1098EXPORT_SYMBOL(kobject_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099EXPORT_SYMBOL(kobject_del);
1100
1101EXPORT_SYMBOL(kset_register);
1102EXPORT_SYMBOL(kset_unregister);