blob: e769ee3c2fb91ce4391b4bf7e9c99c8caa551909 [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>
16#include <linux/string.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050017#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/stat.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Tejun Heoe34ff492013-09-11 22:29:05 -040021/**
22 * kobject_namespace - return @kobj's namespace tag
23 * @kobj: kobject in question
24 *
25 * Returns namespace tag of @kobj if its parent has namespace ops enabled
26 * and thus @kobj should have a namespace tag associated with it. Returns
27 * %NULL otherwise.
28 */
29const void *kobject_namespace(struct kobject *kobj)
30{
31 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
Tejun Heocb26a312013-09-11 22:29:07 -040032 const void *ns;
Tejun Heoe34ff492013-09-11 22:29:05 -040033
34 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
35 return NULL;
36
Tejun Heocb26a312013-09-11 22:29:07 -040037 ns = kobj->ktype->namespace(kobj);
38 WARN_ON(!ns); /* @kobj in a namespace is required to have !NULL tag */
39 return ns;
Tejun Heoe34ff492013-09-11 22:29:05 -040040}
41
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080042/*
43 * populate_dir - populate directory with attributes.
44 * @kobj: object we're working on.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080046 * Most subsystems have a set of default attributes that are associated
47 * with an object that registers with them. This is a helper called during
48 * object registration that loops through the default attributes of the
49 * subsystem and creates attributes files for them in sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080051static int populate_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080053 struct kobj_type *t = get_ktype(kobj);
54 struct attribute *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int error = 0;
56 int i;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (t && t->default_attrs) {
59 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080060 error = sysfs_create_file(kobj, attr);
61 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
63 }
64 }
65 return error;
66}
67
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080068static int create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Tejun Heoe34ff492013-09-11 22:29:05 -040070 int error;
71
72 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
yan6b960612012-04-20 21:25:53 +080073 if (!error) {
74 error = populate_dir(kobj);
75 if (error)
76 sysfs_remove_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78 return error;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int get_kobj_path_length(struct kobject *kobj)
82{
83 int length = 1;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080084 struct kobject *parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080086 /* walk up the ancestors until we hit the one pointing to the
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * root.
88 * Add 1 to strlen for leading '/' of each level.
89 */
90 do {
Chuck Ebbertb365b3d2006-01-12 20:02:00 -050091 if (kobject_name(parent) == NULL)
92 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 length += strlen(kobject_name(parent)) + 1;
94 parent = parent->parent;
95 } while (parent);
96 return length;
97}
98
99static void fill_kobj_path(struct kobject *kobj, char *path, int length)
100{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800101 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 --length;
104 for (parent = kobj; parent; parent = parent->parent) {
105 int cur = strlen(kobject_name(parent));
106 /* back up enough to print this name with '/' */
107 length -= cur;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800108 strncpy(path + length, kobject_name(parent), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *(path + --length) = '/';
110 }
111
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800112 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
Harvey Harrison810304d2008-04-30 00:55:08 -0700113 kobj, __func__, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800117 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *
119 * @kobj: kobject in question, with which to build the path
120 * @gfp_mask: the allocation type used to allocate the path
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800121 *
122 * The result must be freed by the caller with kfree().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
Al Virofd4f2df2005-10-21 03:18:50 -0400124char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 char *path;
127 int len;
128
129 len = get_kobj_path_length(kobj);
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500130 if (len == 0)
131 return NULL;
Burman Yan4668edc2006-12-06 20:38:51 -0800132 path = kzalloc(len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!path)
134 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 fill_kobj_path(kobj, path, len);
136
137 return path;
138}
Dmitry Torokhov80fc9f52006-10-11 01:43:58 -0400139EXPORT_SYMBOL_GPL(kobject_get_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100141/* add the kobject to its kset's list */
142static void kobj_kset_join(struct kobject *kobj)
143{
144 if (!kobj->kset)
145 return;
146
147 kset_get(kobj->kset);
148 spin_lock(&kobj->kset->list_lock);
149 list_add_tail(&kobj->entry, &kobj->kset->list);
150 spin_unlock(&kobj->kset->list_lock);
151}
152
153/* remove the kobject from its kset's list */
154static void kobj_kset_leave(struct kobject *kobj)
155{
156 if (!kobj->kset)
157 return;
158
159 spin_lock(&kobj->kset->list_lock);
160 list_del_init(&kobj->entry);
161 spin_unlock(&kobj->kset->list_lock);
162 kset_put(kobj->kset);
163}
164
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800165static void kobject_init_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800167 if (!kobj)
168 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kref_init(&kobj->kref);
170 INIT_LIST_HEAD(&kobj->entry);
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800171 kobj->state_in_sysfs = 0;
172 kobj->state_add_uevent_sent = 0;
173 kobj->state_remove_uevent_sent = 0;
174 kobj->state_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700178static int kobject_add_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int error = 0;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800181 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100183 if (!kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -ENOENT;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100185
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100186 if (!kobj->name || !kobj->name[0]) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700187 WARN(1, "kobject: (%p): attempted to be registered with empty "
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800188 "name!\n", kobj);
Greg Kroah-Hartmanc171fef2006-01-20 14:08:59 -0800189 return -EINVAL;
190 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 parent = kobject_get(kobj->parent);
193
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100194 /* join kset if set, use it as parent if we do not already have one */
195 if (kobj->kset) {
196 if (!parent)
197 parent = kobject_get(&kobj->kset->kobj);
198 kobj_kset_join(kobj);
199 kobj->parent = parent;
200 }
201
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800202 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700203 kobject_name(kobj), kobj, __func__,
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800204 parent ? kobject_name(parent) : "<NULL>",
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800205 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900207 error = create_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (error) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100209 kobj_kset_leave(kobj);
210 kobject_put(parent);
211 kobj->parent = NULL;
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800212
213 /* be noisy on error issues */
214 if (error == -EEXIST)
Dan Williams282029c2012-04-06 13:41:15 -0700215 WARN(1, "%s failed for %s with "
216 "-EEXIST, don't try to register things with "
217 "the same name in the same directory.\n",
218 __func__, kobject_name(kobj));
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800219 else
Dan Williams282029c2012-04-06 13:41:15 -0700220 WARN(1, "%s failed for %s (error: %d parent: %s)\n",
221 __func__, kobject_name(kobj), error,
222 parent ? kobject_name(parent) : "'none'");
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100223 } else
224 kobj->state_in_sysfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return error;
227}
228
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700229/**
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500230 * kobject_set_name_vargs - Set the name of an kobject
231 * @kobj: struct kobject to set the name of
Greg Kroah-Hartman8c4606b2007-12-04 14:45:47 +0800232 * @fmt: format string used to build the name
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500233 * @vargs: vargs to format the string.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100235int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500236 va_list vargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Kay Sievers9f2556512008-05-06 22:24:04 +0200238 const char *old_name = kobj->name;
239 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Kay Sievers8a577ff2009-04-18 15:05:45 -0700241 if (kobj->name && !fmt)
242 return 0;
243
Kay Sieversa4ca6612008-04-30 02:06:29 +0200244 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
245 if (!kobj->name)
246 return -ENOMEM;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500247
Kay Sievers9f2556512008-05-06 22:24:04 +0200248 /* ewww... some of these buggers have '/' in the name ... */
Ingo Oeser25fdeb32008-07-23 01:25:01 +0200249 while ((s = strchr(kobj->name, '/')))
Kay Sievers9f2556512008-05-06 22:24:04 +0200250 s[0] = '!';
251
252 kfree(old_name);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500253 return 0;
254}
255
256/**
257 * kobject_set_name - Set the name of a kobject
258 * @kobj: struct kobject to set the name of
259 * @fmt: format string used to build the name
260 *
261 * This sets the name of the kobject. If you have already added the
262 * kobject to the system, you must call kobject_rename() in order to
263 * change the name of the kobject.
264 */
265int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
266{
Kay Sieversa4ca6612008-04-30 02:06:29 +0200267 va_list vargs;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500268 int retval;
269
Kay Sieversa4ca6612008-04-30 02:06:29 +0200270 va_start(vargs, fmt);
271 retval = kobject_set_name_vargs(kobj, fmt, vargs);
272 va_end(vargs);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500273
274 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276EXPORT_SYMBOL(kobject_set_name);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/**
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700279 * kobject_init - initialize a kobject structure
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800280 * @kobj: pointer to the kobject to initialize
281 * @ktype: pointer to the ktype for this kobject.
282 *
283 * This function will properly initialize a kobject such that it can then
284 * be passed to the kobject_add() call.
285 *
286 * After this function is called, the kobject MUST be cleaned up by a call
287 * to kobject_put(), not by a call to kfree directly to ensure that all of
288 * the memory is cleaned up properly.
289 */
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700290void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800291{
292 char *err_str;
293
294 if (!kobj) {
295 err_str = "invalid kobject pointer!";
296 goto error;
297 }
298 if (!ktype) {
299 err_str = "must have a ktype to be initialized properly!\n";
300 goto error;
301 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100302 if (kobj->state_initialized) {
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800303 /* do not error out as sometimes we can recover */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100304 printk(KERN_ERR "kobject (%p): tried to init an initialized "
305 "object, something is seriously wrong.\n", kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800306 dump_stack();
307 }
308
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800309 kobject_init_internal(kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800310 kobj->ktype = ktype;
311 return;
312
313error:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100314 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800315 dump_stack();
316}
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700317EXPORT_SYMBOL(kobject_init);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800318
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800319static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
320 const char *fmt, va_list vargs)
321{
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800322 int retval;
323
Kay Sieversa4ca6612008-04-30 02:06:29 +0200324 retval = kobject_set_name_vargs(kobj, fmt, vargs);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800325 if (retval) {
326 printk(KERN_ERR "kobject: can not set name properly!\n");
327 return retval;
328 }
329 kobj->parent = parent;
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700330 return kobject_add_internal(kobj);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800331}
332
333/**
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700334 * kobject_add - the main kobject add function
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800335 * @kobj: the kobject to add
336 * @parent: pointer to the parent of the kobject.
337 * @fmt: format to name the kobject with.
338 *
339 * The kobject name is set and added to the kobject hierarchy in this
340 * function.
341 *
342 * If @parent is set, then the parent of the @kobj will be set to it.
343 * If @parent is NULL, then the parent of the @kobj will be set to the
344 * kobject associted with the kset assigned to this kobject. If no kset
345 * is assigned to the kobject, then the kobject will be located in the
346 * root of the sysfs tree.
347 *
348 * If this function returns an error, kobject_put() must be called to
349 * properly clean up the memory associated with the object.
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800350 * Under no instance should the kobject that is passed to this function
351 * be directly freed with a call to kfree(), that can leak memory.
352 *
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100353 * Note, no "add" uevent will be created with this call, the caller should set
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800354 * up all of the necessary sysfs files for the object and then call
355 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
356 * userspace is properly notified of this kobject's creation.
357 */
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700358int kobject_add(struct kobject *kobj, struct kobject *parent,
359 const char *fmt, ...)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800360{
361 va_list args;
362 int retval;
363
364 if (!kobj)
365 return -EINVAL;
366
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100367 if (!kobj->state_initialized) {
368 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
369 "uninitialized object, something is seriously wrong.\n",
370 kobject_name(kobj), kobj);
371 dump_stack();
372 return -EINVAL;
373 }
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800374 va_start(args, fmt);
375 retval = kobject_add_varg(kobj, parent, fmt, args);
376 va_end(args);
377
378 return retval;
379}
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700380EXPORT_SYMBOL(kobject_add);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800381
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800382/**
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800383 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
384 * @kobj: pointer to the kobject to initialize
385 * @ktype: pointer to the ktype for this kobject.
386 * @parent: pointer to the parent of this kobject.
387 * @fmt: the name of the kobject.
388 *
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700389 * This function combines the call to kobject_init() and
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700390 * kobject_add(). The same type of error handling after a call to
391 * kobject_add() and kobject lifetime rules are the same here.
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800392 */
393int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
394 struct kobject *parent, const char *fmt, ...)
395{
396 va_list args;
397 int retval;
398
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700399 kobject_init(kobj, ktype);
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800400
401 va_start(args, fmt);
402 retval = kobject_add_varg(kobj, parent, fmt, args);
403 va_end(args);
404
405 return retval;
406}
407EXPORT_SYMBOL_GPL(kobject_init_and_add);
408
409/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800410 * kobject_rename - change the name of an object
411 * @kobj: object in question.
412 * @new_name: object's new name
Eric W. Biederman030c1d22008-05-08 14:41:00 -0700413 *
414 * It is the responsibility of the caller to provide mutual
415 * exclusion between two different calls of kobject_rename
416 * on the same kobject and to ensure that new_name is valid and
417 * won't conflict with other kobjects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800419int kobject_rename(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int error = 0;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800422 const char *devpath = NULL;
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700423 const char *dup_name = NULL, *name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800424 char *devpath_string = NULL;
425 char *envp[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 kobj = kobject_get(kobj);
428 if (!kobj)
429 return -EINVAL;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700430 if (!kobj->parent)
431 return -EINVAL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800432
433 devpath = kobject_get_path(kobj, GFP_KERNEL);
434 if (!devpath) {
435 error = -ENOMEM;
436 goto out;
437 }
438 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
439 if (!devpath_string) {
440 error = -ENOMEM;
441 goto out;
442 }
443 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
444 envp[0] = devpath_string;
445 envp[1] = NULL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800446
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700447 name = dup_name = kstrdup(new_name, GFP_KERNEL);
448 if (!name) {
449 error = -ENOMEM;
450 goto out;
451 }
452
Tejun Heoe34ff492013-09-11 22:29:05 -0400453 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700454 if (error)
455 goto out;
456
457 /* Install the new kobject name */
458 dup_name = kobj->name;
459 kobj->name = name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800460
461 /* This function is mostly/only used for network interface.
462 * Some hotplug package track interfaces by their name and
463 * therefore want to know when the name is changed by the user. */
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700464 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800465
466out:
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700467 kfree(dup_name);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800468 kfree(devpath_string);
469 kfree(devpath);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700470 kobject_put(kobj);
471
472 return error;
473}
Alex Chiang8344b562008-06-10 15:30:42 -0600474EXPORT_SYMBOL_GPL(kobject_rename);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700475
476/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800477 * kobject_move - move object to another parent
478 * @kobj: object in question.
479 * @new_parent: object's new parent (can be NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +0100480 */
Cornelia Huck8a824722006-11-20 17:07:51 +0100481int kobject_move(struct kobject *kobj, struct kobject *new_parent)
482{
483 int error;
484 struct kobject *old_parent;
485 const char *devpath = NULL;
486 char *devpath_string = NULL;
487 char *envp[2];
488
489 kobj = kobject_get(kobj);
490 if (!kobj)
491 return -EINVAL;
492 new_parent = kobject_get(new_parent);
493 if (!new_parent) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100494 if (kobj->kset)
495 new_parent = kobject_get(&kobj->kset->kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +0100496 }
Tejun Heoe34ff492013-09-11 22:29:05 -0400497
Cornelia Huck8a824722006-11-20 17:07:51 +0100498 /* old object path */
499 devpath = kobject_get_path(kobj, GFP_KERNEL);
500 if (!devpath) {
501 error = -ENOMEM;
502 goto out;
503 }
504 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
505 if (!devpath_string) {
506 error = -ENOMEM;
507 goto out;
508 }
509 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
510 envp[0] = devpath_string;
511 envp[1] = NULL;
Tejun Heoe34ff492013-09-11 22:29:05 -0400512 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
Cornelia Huck8a824722006-11-20 17:07:51 +0100513 if (error)
514 goto out;
515 old_parent = kobj->parent;
516 kobj->parent = new_parent;
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300517 new_parent = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100518 kobject_put(old_parent);
519 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
520out:
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300521 kobject_put(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +0100522 kobject_put(kobj);
523 kfree(devpath_string);
524 kfree(devpath);
525 return error;
526}
527
528/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800529 * kobject_del - unlink kobject from hierarchy.
530 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800532void kobject_del(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800534 if (!kobj)
535 return;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sysfs_remove_dir(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100538 kobj->state_in_sysfs = 0;
539 kobj_kset_leave(kobj);
540 kobject_put(kobj->parent);
541 kobj->parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800545 * kobject_get - increment refcount for object.
546 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800548struct kobject *kobject_get(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 if (kobj)
551 kref_get(&kobj->kref);
552 return kobj;
553}
554
Anatol Pomozov2d864e42013-05-07 15:37:48 -0700555static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700556{
557 if (!kref_get_unless_zero(&kobj->kref))
558 kobj = NULL;
559 return kobj;
560}
561
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800562/*
563 * kobject_cleanup - free kobject resources.
564 * @kobj: object to cleanup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800566static void kobject_cleanup(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100568 struct kobj_type *t = get_ktype(kobj);
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100569 const char *name = kobj->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Russell Kingc817a672013-06-27 15:06:14 +0100571 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
572 kobject_name(kobj), kobj, __func__, kobj->parent);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100573
574 if (t && !t->release)
575 pr_debug("kobject: '%s' (%p): does not have a release() "
576 "function, it is broken and must be fixed.\n",
577 kobject_name(kobj), kobj);
578
579 /* send "remove" if the caller did not do it but sent "add" */
580 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
581 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
582 kobject_name(kobj), kobj);
583 kobject_uevent(kobj, KOBJ_REMOVE);
584 }
585
586 /* remove from sysfs if the caller did not do it */
587 if (kobj->state_in_sysfs) {
588 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
589 kobject_name(kobj), kobj);
590 kobject_del(kobj);
591 }
592
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700593 if (t && t->release) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100594 pr_debug("kobject: '%s' (%p): calling ktype release\n",
595 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 t->release(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100597 }
598
599 /* free name if we allocated it */
Kay Sieversaf5ca3f42007-12-20 02:09:39 +0100600 if (name) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100601 pr_debug("kobject: '%s': free name\n", name);
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700602 kfree(name);
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Russell Kingc817a672013-06-27 15:06:14 +0100606#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
607static void kobject_delayed_cleanup(struct work_struct *work)
608{
609 kobject_cleanup(container_of(to_delayed_work(work),
610 struct kobject, release));
611}
612#endif
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static void kobject_release(struct kref *kref)
615{
Russell Kingc817a672013-06-27 15:06:14 +0100616 struct kobject *kobj = container_of(kref, struct kobject, kref);
617#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
618 pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n",
619 kobject_name(kobj), kobj, __func__, kobj->parent);
620 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
621 schedule_delayed_work(&kobj->release, HZ);
622#else
623 kobject_cleanup(kobj);
624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800628 * kobject_put - decrement refcount for object.
629 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800631 * Decrement the refcount, and if 0, call kobject_cleanup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800633void kobject_put(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800635 if (kobj) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700636 if (!kobj->state_initialized)
637 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800638 "initialized, yet kobject_put() is being "
639 "called.\n", kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kref_put(&kobj->kref, kobject_release);
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800644static void dynamic_kobj_release(struct kobject *kobj)
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500645{
Harvey Harrison810304d2008-04-30 00:55:08 -0700646 pr_debug("kobject: (%p): %s\n", kobj, __func__);
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500647 kfree(kobj);
648}
649
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800650static struct kobj_type dynamic_kobj_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100651 .release = dynamic_kobj_release,
652 .sysfs_ops = &kobj_sysfs_ops,
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500653};
654
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800655/**
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800656 * kobject_create - create a struct kobject dynamically
657 *
658 * This function creates a kobject structure dynamically and sets it up
659 * to be a "dynamic" kobject with a default release function set up.
660 *
661 * If the kobject was not able to be created, NULL will be returned.
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800662 * The kobject structure returned from here must be cleaned up with a
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700663 * call to kobject_put() and not kfree(), as kobject_init() has
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800664 * already been called on this structure.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800665 */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800666struct kobject *kobject_create(void)
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800667{
668 struct kobject *kobj;
669
670 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
671 if (!kobj)
672 return NULL;
673
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700674 kobject_init(kobj, &dynamic_kobj_ktype);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800675 return kobj;
676}
677
678/**
679 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
680 *
Zhi Yong Wu9ff1f832012-05-07 10:48:25 +0800681 * @name: the name for the kobject
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800682 * @parent: the parent kobject of this kobject, if any.
683 *
Dave Youngf70701a2008-01-28 16:58:00 +0800684 * This function creates a kobject structure dynamically and registers it
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800685 * with sysfs. When you are finished with this structure, call
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800686 * kobject_put() and the structure will be dynamically freed when
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800687 * it is no longer being used.
688 *
689 * If the kobject was not able to be created, NULL will be returned.
690 */
691struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
692{
693 struct kobject *kobj;
694 int retval;
695
696 kobj = kobject_create();
697 if (!kobj)
698 return NULL;
699
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700700 retval = kobject_add(kobj, parent, "%s", name);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800701 if (retval) {
702 printk(KERN_WARNING "%s: kobject_add error: %d\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700703 __func__, retval);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800704 kobject_put(kobj);
705 kobj = NULL;
706 }
707 return kobj;
708}
709EXPORT_SYMBOL_GPL(kobject_create_and_add);
710
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500711/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800712 * kset_init - initialize a kset for use
713 * @k: kset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800715void kset_init(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Greg Kroah-Hartmane1543dd2007-12-17 23:05:35 -0700717 kobject_init_internal(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 INIT_LIST_HEAD(&k->list);
719 spin_lock_init(&k->list_lock);
720}
721
Kay Sievers23b52122007-11-02 13:47:53 +0100722/* default kobject attribute operations */
723static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
724 char *buf)
725{
726 struct kobj_attribute *kattr;
727 ssize_t ret = -EIO;
728
729 kattr = container_of(attr, struct kobj_attribute, attr);
730 if (kattr->show)
731 ret = kattr->show(kobj, kattr, buf);
732 return ret;
733}
734
735static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
736 const char *buf, size_t count)
737{
738 struct kobj_attribute *kattr;
739 ssize_t ret = -EIO;
740
741 kattr = container_of(attr, struct kobj_attribute, attr);
742 if (kattr->store)
743 ret = kattr->store(kobj, kattr, buf, count);
744 return ret;
745}
746
Emese Revfy52cf25d2010-01-19 02:58:23 +0100747const struct sysfs_ops kobj_sysfs_ops = {
Kay Sievers23b52122007-11-02 13:47:53 +0100748 .show = kobj_attr_show,
749 .store = kobj_attr_store,
750};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800753 * kset_register - initialize and add a kset.
754 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800756int kset_register(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Kay Sievers80f03e32007-05-26 11:21:36 +0200758 int err;
759
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800760 if (!k)
761 return -EINVAL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 kset_init(k);
Greg Kroah-Hartman12e339a2002-04-09 12:14:34 -0700764 err = kobject_add_internal(&k->kobj);
Kay Sievers80f03e32007-05-26 11:21:36 +0200765 if (err)
766 return err;
767 kobject_uevent(&k->kobj, KOBJ_ADD);
768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800772 * kset_unregister - remove a kset.
773 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800775void kset_unregister(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800777 if (!k)
778 return;
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800779 kobject_put(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800783 * kset_find_obj - search for object in kset.
784 * @kset: kset we're looking in.
785 * @name: object's name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800787 * Lock kset via @kset->subsys, and iterate over @kset->list,
788 * looking for a matching kobject. If matching object is found
789 * take a reference and return the object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800791struct kobject *kset_find_obj(struct kset *kset, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400793 struct kobject *k;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800794 struct kobject *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 spin_lock(&kset->list_lock);
Robin Holtc25d1df2010-09-29 14:00:54 -0500797
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400798 list_for_each_entry(k, &kset->list, entry) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800799 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700800 ret = kobject_get_unless_zero(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 break;
802 }
803 }
Robin Holtc25d1df2010-09-29 14:00:54 -0500804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 spin_unlock(&kset->list_lock);
806 return ret;
807}
808
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700809static void kset_release(struct kobject *kobj)
810{
811 struct kset *kset = container_of(kobj, struct kset, kobj);
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800812 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700813 kobject_name(kobj), kobj, __func__);
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700814 kfree(kset);
815}
816
Kay Sievers386f2752007-11-02 13:47:53 +0100817static struct kobj_type kset_ktype = {
818 .sysfs_ops = &kobj_sysfs_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700819 .release = kset_release,
820};
821
822/**
823 * kset_create - create a struct kset dynamically
824 *
825 * @name: the name for the kset
826 * @uevent_ops: a struct kset_uevent_ops for the kset
827 * @parent_kobj: the parent kobject of this kset, if any.
828 *
829 * This function creates a kset structure dynamically. This structure can
830 * then be registered with the system and show up in sysfs with a call to
831 * kset_register(). When you are finished with this structure, if
832 * kset_register() has been called, call kset_unregister() and the
833 * structure will be dynamically freed when it is no longer being used.
834 *
835 * If the kset was not able to be created, NULL will be returned.
836 */
837static struct kset *kset_create(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100838 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700839 struct kobject *parent_kobj)
840{
841 struct kset *kset;
Dave Youngd9cd8f32009-05-11 14:17:45 +0800842 int retval;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700843
844 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
845 if (!kset)
846 return NULL;
Kees Cookb7165eb2013-06-06 13:52:19 -0700847 retval = kobject_set_name(&kset->kobj, "%s", name);
Dave Youngd9cd8f32009-05-11 14:17:45 +0800848 if (retval) {
849 kfree(kset);
850 return NULL;
851 }
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700852 kset->uevent_ops = uevent_ops;
853 kset->kobj.parent = parent_kobj;
854
855 /*
Kay Sievers386f2752007-11-02 13:47:53 +0100856 * The kobject of this kset will have a type of kset_ktype and belong to
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700857 * no kset itself. That way we can properly free it when it is
858 * finished being used.
859 */
Kay Sievers386f2752007-11-02 13:47:53 +0100860 kset->kobj.ktype = &kset_ktype;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700861 kset->kobj.kset = NULL;
862
863 return kset;
864}
865
866/**
867 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
868 *
869 * @name: the name for the kset
870 * @uevent_ops: a struct kset_uevent_ops for the kset
871 * @parent_kobj: the parent kobject of this kset, if any.
872 *
873 * This function creates a kset structure dynamically and registers it
874 * with sysfs. When you are finished with this structure, call
875 * kset_unregister() and the structure will be dynamically freed when it
876 * is no longer being used.
877 *
878 * If the kset was not able to be created, NULL will be returned.
879 */
880struct kset *kset_create_and_add(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100881 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700882 struct kobject *parent_kobj)
883{
884 struct kset *kset;
885 int error;
886
887 kset = kset_create(name, uevent_ops, parent_kobj);
888 if (!kset)
889 return NULL;
890 error = kset_register(kset);
891 if (error) {
892 kfree(kset);
893 return NULL;
894 }
895 return kset;
896}
897EXPORT_SYMBOL_GPL(kset_create_and_add);
898
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700899
900static DEFINE_SPINLOCK(kobj_ns_type_lock);
901static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
902
903int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
904{
905 enum kobj_ns_type type = ops->type;
906 int error;
907
908 spin_lock(&kobj_ns_type_lock);
909
910 error = -EINVAL;
911 if (type >= KOBJ_NS_TYPES)
912 goto out;
913
914 error = -EINVAL;
915 if (type <= KOBJ_NS_TYPE_NONE)
916 goto out;
917
918 error = -EBUSY;
919 if (kobj_ns_ops_tbl[type])
920 goto out;
921
922 error = 0;
923 kobj_ns_ops_tbl[type] = ops;
924
925out:
926 spin_unlock(&kobj_ns_type_lock);
927 return error;
928}
929
930int kobj_ns_type_registered(enum kobj_ns_type type)
931{
932 int registered = 0;
933
934 spin_lock(&kobj_ns_type_lock);
935 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
936 registered = kobj_ns_ops_tbl[type] != NULL;
937 spin_unlock(&kobj_ns_type_lock);
938
939 return registered;
940}
941
942const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
943{
944 const struct kobj_ns_type_operations *ops = NULL;
945
946 if (parent && parent->ktype->child_ns_type)
947 ops = parent->ktype->child_ns_type(parent);
948
949 return ops;
950}
951
952const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
953{
954 return kobj_child_ns_ops(kobj->parent);
955}
956
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -0700957bool kobj_ns_current_may_mount(enum kobj_ns_type type)
958{
959 bool may_mount = false;
960
961 if (type == KOBJ_NS_TYPE_NONE)
962 return true;
963
964 spin_lock(&kobj_ns_type_lock);
965 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
966 kobj_ns_ops_tbl[type])
967 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
968 spin_unlock(&kobj_ns_type_lock);
969
970 return may_mount;
971}
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700972
Al Viroa685e082011-06-08 21:13:01 -0400973void *kobj_ns_grab_current(enum kobj_ns_type type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700974{
Al Viroa685e082011-06-08 21:13:01 -0400975 void *ns = NULL;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700976
977 spin_lock(&kobj_ns_type_lock);
978 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
979 kobj_ns_ops_tbl[type])
Al Viroa685e082011-06-08 21:13:01 -0400980 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700981 spin_unlock(&kobj_ns_type_lock);
982
983 return ns;
984}
985
986const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
987{
988 const void *ns = NULL;
989
990 spin_lock(&kobj_ns_type_lock);
991 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
992 kobj_ns_ops_tbl[type])
993 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
994 spin_unlock(&kobj_ns_type_lock);
995
996 return ns;
997}
998
999const void *kobj_ns_initial(enum kobj_ns_type type)
1000{
1001 const void *ns = NULL;
1002
1003 spin_lock(&kobj_ns_type_lock);
1004 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1005 kobj_ns_ops_tbl[type])
1006 ns = kobj_ns_ops_tbl[type]->initial_ns();
1007 spin_unlock(&kobj_ns_type_lock);
1008
1009 return ns;
1010}
1011
Al Viroa685e082011-06-08 21:13:01 -04001012void kobj_ns_drop(enum kobj_ns_type type, void *ns)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001013{
Al Viroa685e082011-06-08 21:13:01 -04001014 spin_lock(&kobj_ns_type_lock);
1015 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1016 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1017 kobj_ns_ops_tbl[type]->drop_ns(ns);
1018 spin_unlock(&kobj_ns_type_lock);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021EXPORT_SYMBOL(kobject_get);
1022EXPORT_SYMBOL(kobject_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023EXPORT_SYMBOL(kobject_del);
1024
1025EXPORT_SYMBOL(kset_register);
1026EXPORT_SYMBOL(kset_unregister);