blob: 85fb3a161b2137e4d2a8dd558a14f22656a8225b [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);
32
33 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
34 return NULL;
35
36 return kobj->ktype->namespace(kobj);
37}
38
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080039/*
40 * populate_dir - populate directory with attributes.
41 * @kobj: object we're working on.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080043 * Most subsystems have a set of default attributes that are associated
44 * with an object that registers with them. This is a helper called during
45 * object registration that loops through the default attributes of the
46 * subsystem and creates attributes files for them in sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080048static int populate_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080050 struct kobj_type *t = get_ktype(kobj);
51 struct attribute *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int error = 0;
53 int i;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (t && t->default_attrs) {
56 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080057 error = sysfs_create_file(kobj, attr);
58 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
60 }
61 }
62 return error;
63}
64
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080065static int create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Tejun Heoe34ff492013-09-11 22:29:05 -040067 int error;
68
69 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
yan6b960612012-04-20 21:25:53 +080070 if (!error) {
71 error = populate_dir(kobj);
72 if (error)
73 sysfs_remove_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 return error;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int get_kobj_path_length(struct kobject *kobj)
79{
80 int length = 1;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080081 struct kobject *parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080083 /* walk up the ancestors until we hit the one pointing to the
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * root.
85 * Add 1 to strlen for leading '/' of each level.
86 */
87 do {
Chuck Ebbertb365b3d2006-01-12 20:02:00 -050088 if (kobject_name(parent) == NULL)
89 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 length += strlen(kobject_name(parent)) + 1;
91 parent = parent->parent;
92 } while (parent);
93 return length;
94}
95
96static void fill_kobj_path(struct kobject *kobj, char *path, int length)
97{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080098 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 --length;
101 for (parent = kobj; parent; parent = parent->parent) {
102 int cur = strlen(kobject_name(parent));
103 /* back up enough to print this name with '/' */
104 length -= cur;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800105 strncpy(path + length, kobject_name(parent), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *(path + --length) = '/';
107 }
108
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800109 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
Harvey Harrison810304d2008-04-30 00:55:08 -0700110 kobj, __func__, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800114 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 * @kobj: kobject in question, with which to build the path
117 * @gfp_mask: the allocation type used to allocate the path
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800118 *
119 * The result must be freed by the caller with kfree().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Al Virofd4f2df2005-10-21 03:18:50 -0400121char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 char *path;
124 int len;
125
126 len = get_kobj_path_length(kobj);
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500127 if (len == 0)
128 return NULL;
Burman Yan4668edc2006-12-06 20:38:51 -0800129 path = kzalloc(len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (!path)
131 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 fill_kobj_path(kobj, path, len);
133
134 return path;
135}
Dmitry Torokhov80fc9f52006-10-11 01:43:58 -0400136EXPORT_SYMBOL_GPL(kobject_get_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100138/* add the kobject to its kset's list */
139static void kobj_kset_join(struct kobject *kobj)
140{
141 if (!kobj->kset)
142 return;
143
144 kset_get(kobj->kset);
145 spin_lock(&kobj->kset->list_lock);
146 list_add_tail(&kobj->entry, &kobj->kset->list);
147 spin_unlock(&kobj->kset->list_lock);
148}
149
150/* remove the kobject from its kset's list */
151static void kobj_kset_leave(struct kobject *kobj)
152{
153 if (!kobj->kset)
154 return;
155
156 spin_lock(&kobj->kset->list_lock);
157 list_del_init(&kobj->entry);
158 spin_unlock(&kobj->kset->list_lock);
159 kset_put(kobj->kset);
160}
161
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800162static void kobject_init_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800164 if (!kobj)
165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kref_init(&kobj->kref);
167 INIT_LIST_HEAD(&kobj->entry);
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800168 kobj->state_in_sysfs = 0;
169 kobj->state_add_uevent_sent = 0;
170 kobj->state_remove_uevent_sent = 0;
171 kobj->state_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700175static int kobject_add_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int error = 0;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800178 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100180 if (!kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return -ENOENT;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100182
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100183 if (!kobj->name || !kobj->name[0]) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700184 WARN(1, "kobject: (%p): attempted to be registered with empty "
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800185 "name!\n", kobj);
Greg Kroah-Hartmanc171fef2006-01-20 14:08:59 -0800186 return -EINVAL;
187 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 parent = kobject_get(kobj->parent);
190
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100191 /* join kset if set, use it as parent if we do not already have one */
192 if (kobj->kset) {
193 if (!parent)
194 parent = kobject_get(&kobj->kset->kobj);
195 kobj_kset_join(kobj);
196 kobj->parent = parent;
197 }
198
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800199 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700200 kobject_name(kobj), kobj, __func__,
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800201 parent ? kobject_name(parent) : "<NULL>",
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800202 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900204 error = create_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (error) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100206 kobj_kset_leave(kobj);
207 kobject_put(parent);
208 kobj->parent = NULL;
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800209
210 /* be noisy on error issues */
211 if (error == -EEXIST)
Dan Williams282029c2012-04-06 13:41:15 -0700212 WARN(1, "%s failed for %s with "
213 "-EEXIST, don't try to register things with "
214 "the same name in the same directory.\n",
215 __func__, kobject_name(kobj));
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800216 else
Dan Williams282029c2012-04-06 13:41:15 -0700217 WARN(1, "%s failed for %s (error: %d parent: %s)\n",
218 __func__, kobject_name(kobj), error,
219 parent ? kobject_name(parent) : "'none'");
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100220 } else
221 kobj->state_in_sysfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 return error;
224}
225
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700226/**
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500227 * kobject_set_name_vargs - Set the name of an kobject
228 * @kobj: struct kobject to set the name of
Greg Kroah-Hartman8c4606b2007-12-04 14:45:47 +0800229 * @fmt: format string used to build the name
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500230 * @vargs: vargs to format the string.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100232int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500233 va_list vargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Kay Sievers9f255652008-05-06 22:24:04 +0200235 const char *old_name = kobj->name;
236 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Kay Sievers8a577ff2009-04-18 15:05:45 -0700238 if (kobj->name && !fmt)
239 return 0;
240
Kay Sieversa4ca6612008-04-30 02:06:29 +0200241 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
242 if (!kobj->name)
243 return -ENOMEM;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500244
Kay Sievers9f255652008-05-06 22:24:04 +0200245 /* ewww... some of these buggers have '/' in the name ... */
Ingo Oeser25fdeb32008-07-23 01:25:01 +0200246 while ((s = strchr(kobj->name, '/')))
Kay Sievers9f255652008-05-06 22:24:04 +0200247 s[0] = '!';
248
249 kfree(old_name);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500250 return 0;
251}
252
253/**
254 * kobject_set_name - Set the name of a kobject
255 * @kobj: struct kobject to set the name of
256 * @fmt: format string used to build the name
257 *
258 * This sets the name of the kobject. If you have already added the
259 * kobject to the system, you must call kobject_rename() in order to
260 * change the name of the kobject.
261 */
262int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
263{
Kay Sieversa4ca6612008-04-30 02:06:29 +0200264 va_list vargs;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500265 int retval;
266
Kay Sieversa4ca6612008-04-30 02:06:29 +0200267 va_start(vargs, fmt);
268 retval = kobject_set_name_vargs(kobj, fmt, vargs);
269 va_end(vargs);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500270
271 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273EXPORT_SYMBOL(kobject_set_name);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/**
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700276 * kobject_init - initialize a kobject structure
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800277 * @kobj: pointer to the kobject to initialize
278 * @ktype: pointer to the ktype for this kobject.
279 *
280 * This function will properly initialize a kobject such that it can then
281 * be passed to the kobject_add() call.
282 *
283 * After this function is called, the kobject MUST be cleaned up by a call
284 * to kobject_put(), not by a call to kfree directly to ensure that all of
285 * the memory is cleaned up properly.
286 */
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700287void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800288{
289 char *err_str;
290
291 if (!kobj) {
292 err_str = "invalid kobject pointer!";
293 goto error;
294 }
295 if (!ktype) {
296 err_str = "must have a ktype to be initialized properly!\n";
297 goto error;
298 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100299 if (kobj->state_initialized) {
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800300 /* do not error out as sometimes we can recover */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100301 printk(KERN_ERR "kobject (%p): tried to init an initialized "
302 "object, something is seriously wrong.\n", kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800303 dump_stack();
304 }
305
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800306 kobject_init_internal(kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800307 kobj->ktype = ktype;
308 return;
309
310error:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100311 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800312 dump_stack();
313}
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700314EXPORT_SYMBOL(kobject_init);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800315
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800316static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
317 const char *fmt, va_list vargs)
318{
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800319 int retval;
320
Kay Sieversa4ca6612008-04-30 02:06:29 +0200321 retval = kobject_set_name_vargs(kobj, fmt, vargs);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800322 if (retval) {
323 printk(KERN_ERR "kobject: can not set name properly!\n");
324 return retval;
325 }
326 kobj->parent = parent;
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700327 return kobject_add_internal(kobj);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800328}
329
330/**
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700331 * kobject_add - the main kobject add function
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800332 * @kobj: the kobject to add
333 * @parent: pointer to the parent of the kobject.
334 * @fmt: format to name the kobject with.
335 *
336 * The kobject name is set and added to the kobject hierarchy in this
337 * function.
338 *
339 * If @parent is set, then the parent of the @kobj will be set to it.
340 * If @parent is NULL, then the parent of the @kobj will be set to the
341 * kobject associted with the kset assigned to this kobject. If no kset
342 * is assigned to the kobject, then the kobject will be located in the
343 * root of the sysfs tree.
344 *
345 * If this function returns an error, kobject_put() must be called to
346 * properly clean up the memory associated with the object.
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800347 * Under no instance should the kobject that is passed to this function
348 * be directly freed with a call to kfree(), that can leak memory.
349 *
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100350 * Note, no "add" uevent will be created with this call, the caller should set
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800351 * up all of the necessary sysfs files for the object and then call
352 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
353 * userspace is properly notified of this kobject's creation.
354 */
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700355int kobject_add(struct kobject *kobj, struct kobject *parent,
356 const char *fmt, ...)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800357{
358 va_list args;
359 int retval;
360
361 if (!kobj)
362 return -EINVAL;
363
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100364 if (!kobj->state_initialized) {
365 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
366 "uninitialized object, something is seriously wrong.\n",
367 kobject_name(kobj), kobj);
368 dump_stack();
369 return -EINVAL;
370 }
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800371 va_start(args, fmt);
372 retval = kobject_add_varg(kobj, parent, fmt, args);
373 va_end(args);
374
375 return retval;
376}
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700377EXPORT_SYMBOL(kobject_add);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800378
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800379/**
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800380 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
381 * @kobj: pointer to the kobject to initialize
382 * @ktype: pointer to the ktype for this kobject.
383 * @parent: pointer to the parent of this kobject.
384 * @fmt: the name of the kobject.
385 *
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700386 * This function combines the call to kobject_init() and
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700387 * kobject_add(). The same type of error handling after a call to
388 * kobject_add() and kobject lifetime rules are the same here.
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800389 */
390int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
391 struct kobject *parent, const char *fmt, ...)
392{
393 va_list args;
394 int retval;
395
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700396 kobject_init(kobj, ktype);
Greg Kroah-Hartmanc11c41542007-12-03 21:31:08 -0800397
398 va_start(args, fmt);
399 retval = kobject_add_varg(kobj, parent, fmt, args);
400 va_end(args);
401
402 return retval;
403}
404EXPORT_SYMBOL_GPL(kobject_init_and_add);
405
406/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800407 * kobject_rename - change the name of an object
408 * @kobj: object in question.
409 * @new_name: object's new name
Eric W. Biederman030c1d22008-05-08 14:41:00 -0700410 *
411 * It is the responsibility of the caller to provide mutual
412 * exclusion between two different calls of kobject_rename
413 * on the same kobject and to ensure that new_name is valid and
414 * won't conflict with other kobjects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800416int kobject_rename(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 int error = 0;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800419 const char *devpath = NULL;
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700420 const char *dup_name = NULL, *name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800421 char *devpath_string = NULL;
422 char *envp[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 kobj = kobject_get(kobj);
425 if (!kobj)
426 return -EINVAL;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700427 if (!kobj->parent)
428 return -EINVAL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800429
430 devpath = kobject_get_path(kobj, GFP_KERNEL);
431 if (!devpath) {
432 error = -ENOMEM;
433 goto out;
434 }
435 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
436 if (!devpath_string) {
437 error = -ENOMEM;
438 goto out;
439 }
440 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
441 envp[0] = devpath_string;
442 envp[1] = NULL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800443
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700444 name = dup_name = kstrdup(new_name, GFP_KERNEL);
445 if (!name) {
446 error = -ENOMEM;
447 goto out;
448 }
449
Tejun Heoe34ff492013-09-11 22:29:05 -0400450 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700451 if (error)
452 goto out;
453
454 /* Install the new kobject name */
455 dup_name = kobj->name;
456 kobj->name = name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800457
458 /* This function is mostly/only used for network interface.
459 * Some hotplug package track interfaces by their name and
460 * therefore want to know when the name is changed by the user. */
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700461 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800462
463out:
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700464 kfree(dup_name);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800465 kfree(devpath_string);
466 kfree(devpath);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700467 kobject_put(kobj);
468
469 return error;
470}
Alex Chiang8344b562008-06-10 15:30:42 -0600471EXPORT_SYMBOL_GPL(kobject_rename);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700472
473/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800474 * kobject_move - move object to another parent
475 * @kobj: object in question.
476 * @new_parent: object's new parent (can be NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +0100477 */
Cornelia Huck8a824722006-11-20 17:07:51 +0100478int kobject_move(struct kobject *kobj, struct kobject *new_parent)
479{
480 int error;
481 struct kobject *old_parent;
482 const char *devpath = NULL;
483 char *devpath_string = NULL;
484 char *envp[2];
485
486 kobj = kobject_get(kobj);
487 if (!kobj)
488 return -EINVAL;
489 new_parent = kobject_get(new_parent);
490 if (!new_parent) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100491 if (kobj->kset)
492 new_parent = kobject_get(&kobj->kset->kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +0100493 }
Tejun Heoe34ff492013-09-11 22:29:05 -0400494
Cornelia Huck8a824722006-11-20 17:07:51 +0100495 /* old object path */
496 devpath = kobject_get_path(kobj, GFP_KERNEL);
497 if (!devpath) {
498 error = -ENOMEM;
499 goto out;
500 }
501 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
502 if (!devpath_string) {
503 error = -ENOMEM;
504 goto out;
505 }
506 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
507 envp[0] = devpath_string;
508 envp[1] = NULL;
Tejun Heoe34ff492013-09-11 22:29:05 -0400509 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
Cornelia Huck8a824722006-11-20 17:07:51 +0100510 if (error)
511 goto out;
512 old_parent = kobj->parent;
513 kobj->parent = new_parent;
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300514 new_parent = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100515 kobject_put(old_parent);
516 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
517out:
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300518 kobject_put(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +0100519 kobject_put(kobj);
520 kfree(devpath_string);
521 kfree(devpath);
522 return error;
523}
524
525/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800526 * kobject_del - unlink kobject from hierarchy.
527 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800529void kobject_del(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800531 if (!kobj)
532 return;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 sysfs_remove_dir(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100535 kobj->state_in_sysfs = 0;
536 kobj_kset_leave(kobj);
537 kobject_put(kobj->parent);
538 kobj->parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800542 * kobject_get - increment refcount for object.
543 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800545struct kobject *kobject_get(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 if (kobj)
548 kref_get(&kobj->kref);
549 return kobj;
550}
551
Anatol Pomozov2d864e42013-05-07 15:37:48 -0700552static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700553{
554 if (!kref_get_unless_zero(&kobj->kref))
555 kobj = NULL;
556 return kobj;
557}
558
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800559/*
560 * kobject_cleanup - free kobject resources.
561 * @kobj: object to cleanup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Greg Kroah-Hartman18041f472007-12-03 21:31:08 -0800563static void kobject_cleanup(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100565 struct kobj_type *t = get_ktype(kobj);
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100566 const char *name = kobj->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Russell Kingc817a672013-06-27 15:06:14 +0100568 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
569 kobject_name(kobj), kobj, __func__, kobj->parent);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100570
571 if (t && !t->release)
572 pr_debug("kobject: '%s' (%p): does not have a release() "
573 "function, it is broken and must be fixed.\n",
574 kobject_name(kobj), kobj);
575
576 /* send "remove" if the caller did not do it but sent "add" */
577 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
578 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
579 kobject_name(kobj), kobj);
580 kobject_uevent(kobj, KOBJ_REMOVE);
581 }
582
583 /* remove from sysfs if the caller did not do it */
584 if (kobj->state_in_sysfs) {
585 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
586 kobject_name(kobj), kobj);
587 kobject_del(kobj);
588 }
589
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700590 if (t && t->release) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100591 pr_debug("kobject: '%s' (%p): calling ktype release\n",
592 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 t->release(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100594 }
595
596 /* free name if we allocated it */
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100597 if (name) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100598 pr_debug("kobject: '%s': free name\n", name);
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700599 kfree(name);
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Russell Kingc817a672013-06-27 15:06:14 +0100603#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
604static void kobject_delayed_cleanup(struct work_struct *work)
605{
606 kobject_cleanup(container_of(to_delayed_work(work),
607 struct kobject, release));
608}
609#endif
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611static void kobject_release(struct kref *kref)
612{
Russell Kingc817a672013-06-27 15:06:14 +0100613 struct kobject *kobj = container_of(kref, struct kobject, kref);
614#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
615 pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n",
616 kobject_name(kobj), kobj, __func__, kobj->parent);
617 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
618 schedule_delayed_work(&kobj->release, HZ);
619#else
620 kobject_cleanup(kobj);
621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800625 * kobject_put - decrement refcount for object.
626 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800628 * Decrement the refcount, and if 0, call kobject_cleanup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800630void kobject_put(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800632 if (kobj) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700633 if (!kobj->state_initialized)
634 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800635 "initialized, yet kobject_put() is being "
636 "called.\n", kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 kref_put(&kobj->kref, kobject_release);
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800641static void dynamic_kobj_release(struct kobject *kobj)
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500642{
Harvey Harrison810304d2008-04-30 00:55:08 -0700643 pr_debug("kobject: (%p): %s\n", kobj, __func__);
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500644 kfree(kobj);
645}
646
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800647static struct kobj_type dynamic_kobj_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100648 .release = dynamic_kobj_release,
649 .sysfs_ops = &kobj_sysfs_ops,
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500650};
651
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800652/**
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800653 * kobject_create - create a struct kobject dynamically
654 *
655 * This function creates a kobject structure dynamically and sets it up
656 * to be a "dynamic" kobject with a default release function set up.
657 *
658 * If the kobject was not able to be created, NULL will be returned.
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800659 * The kobject structure returned from here must be cleaned up with a
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700660 * call to kobject_put() and not kfree(), as kobject_init() has
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800661 * already been called on this structure.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800662 */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800663struct kobject *kobject_create(void)
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800664{
665 struct kobject *kobj;
666
667 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
668 if (!kobj)
669 return NULL;
670
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700671 kobject_init(kobj, &dynamic_kobj_ktype);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800672 return kobj;
673}
674
675/**
676 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
677 *
Zhi Yong Wu9ff1f832012-05-07 10:48:25 +0800678 * @name: the name for the kobject
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800679 * @parent: the parent kobject of this kobject, if any.
680 *
Dave Youngf70701a2008-01-28 16:58:00 +0800681 * This function creates a kobject structure dynamically and registers it
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800682 * with sysfs. When you are finished with this structure, call
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800683 * kobject_put() and the structure will be dynamically freed when
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800684 * it is no longer being used.
685 *
686 * If the kobject was not able to be created, NULL will be returned.
687 */
688struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
689{
690 struct kobject *kobj;
691 int retval;
692
693 kobj = kobject_create();
694 if (!kobj)
695 return NULL;
696
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700697 retval = kobject_add(kobj, parent, "%s", name);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800698 if (retval) {
699 printk(KERN_WARNING "%s: kobject_add error: %d\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700700 __func__, retval);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800701 kobject_put(kobj);
702 kobj = NULL;
703 }
704 return kobj;
705}
706EXPORT_SYMBOL_GPL(kobject_create_and_add);
707
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500708/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800709 * kset_init - initialize a kset for use
710 * @k: kset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800712void kset_init(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Greg Kroah-Hartmane1543dd2007-12-17 23:05:35 -0700714 kobject_init_internal(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 INIT_LIST_HEAD(&k->list);
716 spin_lock_init(&k->list_lock);
717}
718
Kay Sievers23b52122007-11-02 13:47:53 +0100719/* default kobject attribute operations */
720static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
721 char *buf)
722{
723 struct kobj_attribute *kattr;
724 ssize_t ret = -EIO;
725
726 kattr = container_of(attr, struct kobj_attribute, attr);
727 if (kattr->show)
728 ret = kattr->show(kobj, kattr, buf);
729 return ret;
730}
731
732static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
733 const char *buf, size_t count)
734{
735 struct kobj_attribute *kattr;
736 ssize_t ret = -EIO;
737
738 kattr = container_of(attr, struct kobj_attribute, attr);
739 if (kattr->store)
740 ret = kattr->store(kobj, kattr, buf, count);
741 return ret;
742}
743
Emese Revfy52cf25d2010-01-19 02:58:23 +0100744const struct sysfs_ops kobj_sysfs_ops = {
Kay Sievers23b52122007-11-02 13:47:53 +0100745 .show = kobj_attr_show,
746 .store = kobj_attr_store,
747};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800750 * kset_register - initialize and add a kset.
751 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800753int kset_register(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Kay Sievers80f03e32007-05-26 11:21:36 +0200755 int err;
756
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800757 if (!k)
758 return -EINVAL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kset_init(k);
Greg Kroah-Hartman12e339a2002-04-09 12:14:34 -0700761 err = kobject_add_internal(&k->kobj);
Kay Sievers80f03e32007-05-26 11:21:36 +0200762 if (err)
763 return err;
764 kobject_uevent(&k->kobj, KOBJ_ADD);
765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800769 * kset_unregister - remove a kset.
770 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800772void kset_unregister(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800774 if (!k)
775 return;
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800776 kobject_put(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800780 * kset_find_obj - search for object in kset.
781 * @kset: kset we're looking in.
782 * @name: object's name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800784 * Lock kset via @kset->subsys, and iterate over @kset->list,
785 * looking for a matching kobject. If matching object is found
786 * take a reference and return the object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800788struct kobject *kset_find_obj(struct kset *kset, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400790 struct kobject *k;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800791 struct kobject *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 spin_lock(&kset->list_lock);
Robin Holtc25d1df2010-09-29 14:00:54 -0500794
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400795 list_for_each_entry(k, &kset->list, entry) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800796 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700797 ret = kobject_get_unless_zero(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799 }
800 }
Robin Holtc25d1df2010-09-29 14:00:54 -0500801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 spin_unlock(&kset->list_lock);
803 return ret;
804}
805
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700806static void kset_release(struct kobject *kobj)
807{
808 struct kset *kset = container_of(kobj, struct kset, kobj);
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800809 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700810 kobject_name(kobj), kobj, __func__);
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700811 kfree(kset);
812}
813
Kay Sievers386f2752007-11-02 13:47:53 +0100814static struct kobj_type kset_ktype = {
815 .sysfs_ops = &kobj_sysfs_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700816 .release = kset_release,
817};
818
819/**
820 * kset_create - create a struct kset dynamically
821 *
822 * @name: the name for the kset
823 * @uevent_ops: a struct kset_uevent_ops for the kset
824 * @parent_kobj: the parent kobject of this kset, if any.
825 *
826 * This function creates a kset structure dynamically. This structure can
827 * then be registered with the system and show up in sysfs with a call to
828 * kset_register(). When you are finished with this structure, if
829 * kset_register() has been called, call kset_unregister() and the
830 * structure will be dynamically freed when it is no longer being used.
831 *
832 * If the kset was not able to be created, NULL will be returned.
833 */
834static struct kset *kset_create(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100835 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700836 struct kobject *parent_kobj)
837{
838 struct kset *kset;
Dave Youngd9cd8f32009-05-11 14:17:45 +0800839 int retval;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700840
841 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
842 if (!kset)
843 return NULL;
Kees Cookb7165eb2013-06-06 13:52:19 -0700844 retval = kobject_set_name(&kset->kobj, "%s", name);
Dave Youngd9cd8f32009-05-11 14:17:45 +0800845 if (retval) {
846 kfree(kset);
847 return NULL;
848 }
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700849 kset->uevent_ops = uevent_ops;
850 kset->kobj.parent = parent_kobj;
851
852 /*
Kay Sievers386f2752007-11-02 13:47:53 +0100853 * The kobject of this kset will have a type of kset_ktype and belong to
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700854 * no kset itself. That way we can properly free it when it is
855 * finished being used.
856 */
Kay Sievers386f2752007-11-02 13:47:53 +0100857 kset->kobj.ktype = &kset_ktype;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700858 kset->kobj.kset = NULL;
859
860 return kset;
861}
862
863/**
864 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
865 *
866 * @name: the name for the kset
867 * @uevent_ops: a struct kset_uevent_ops for the kset
868 * @parent_kobj: the parent kobject of this kset, if any.
869 *
870 * This function creates a kset structure dynamically and registers it
871 * with sysfs. When you are finished with this structure, call
872 * kset_unregister() and the structure will be dynamically freed when it
873 * is no longer being used.
874 *
875 * If the kset was not able to be created, NULL will be returned.
876 */
877struct kset *kset_create_and_add(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100878 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700879 struct kobject *parent_kobj)
880{
881 struct kset *kset;
882 int error;
883
884 kset = kset_create(name, uevent_ops, parent_kobj);
885 if (!kset)
886 return NULL;
887 error = kset_register(kset);
888 if (error) {
889 kfree(kset);
890 return NULL;
891 }
892 return kset;
893}
894EXPORT_SYMBOL_GPL(kset_create_and_add);
895
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700896
897static DEFINE_SPINLOCK(kobj_ns_type_lock);
898static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
899
900int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
901{
902 enum kobj_ns_type type = ops->type;
903 int error;
904
905 spin_lock(&kobj_ns_type_lock);
906
907 error = -EINVAL;
908 if (type >= KOBJ_NS_TYPES)
909 goto out;
910
911 error = -EINVAL;
912 if (type <= KOBJ_NS_TYPE_NONE)
913 goto out;
914
915 error = -EBUSY;
916 if (kobj_ns_ops_tbl[type])
917 goto out;
918
919 error = 0;
920 kobj_ns_ops_tbl[type] = ops;
921
922out:
923 spin_unlock(&kobj_ns_type_lock);
924 return error;
925}
926
927int kobj_ns_type_registered(enum kobj_ns_type type)
928{
929 int registered = 0;
930
931 spin_lock(&kobj_ns_type_lock);
932 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
933 registered = kobj_ns_ops_tbl[type] != NULL;
934 spin_unlock(&kobj_ns_type_lock);
935
936 return registered;
937}
938
939const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
940{
941 const struct kobj_ns_type_operations *ops = NULL;
942
943 if (parent && parent->ktype->child_ns_type)
944 ops = parent->ktype->child_ns_type(parent);
945
946 return ops;
947}
948
949const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
950{
951 return kobj_child_ns_ops(kobj->parent);
952}
953
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -0700954bool kobj_ns_current_may_mount(enum kobj_ns_type type)
955{
956 bool may_mount = false;
957
958 if (type == KOBJ_NS_TYPE_NONE)
959 return true;
960
961 spin_lock(&kobj_ns_type_lock);
962 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
963 kobj_ns_ops_tbl[type])
964 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
965 spin_unlock(&kobj_ns_type_lock);
966
967 return may_mount;
968}
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700969
Al Viroa685e082011-06-08 21:13:01 -0400970void *kobj_ns_grab_current(enum kobj_ns_type type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700971{
Al Viroa685e082011-06-08 21:13:01 -0400972 void *ns = NULL;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700973
974 spin_lock(&kobj_ns_type_lock);
975 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
976 kobj_ns_ops_tbl[type])
Al Viroa685e082011-06-08 21:13:01 -0400977 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700978 spin_unlock(&kobj_ns_type_lock);
979
980 return ns;
981}
982
983const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
984{
985 const void *ns = NULL;
986
987 spin_lock(&kobj_ns_type_lock);
988 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
989 kobj_ns_ops_tbl[type])
990 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
991 spin_unlock(&kobj_ns_type_lock);
992
993 return ns;
994}
995
996const void *kobj_ns_initial(enum kobj_ns_type type)
997{
998 const void *ns = NULL;
999
1000 spin_lock(&kobj_ns_type_lock);
1001 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1002 kobj_ns_ops_tbl[type])
1003 ns = kobj_ns_ops_tbl[type]->initial_ns();
1004 spin_unlock(&kobj_ns_type_lock);
1005
1006 return ns;
1007}
1008
Al Viroa685e082011-06-08 21:13:01 -04001009void kobj_ns_drop(enum kobj_ns_type type, void *ns)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001010{
Al Viroa685e082011-06-08 21:13:01 -04001011 spin_lock(&kobj_ns_type_lock);
1012 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1013 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1014 kobj_ns_ops_tbl[type]->drop_ns(ns);
1015 spin_unlock(&kobj_ns_type_lock);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018EXPORT_SYMBOL(kobject_get);
1019EXPORT_SYMBOL(kobject_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020EXPORT_SYMBOL(kobject_del);
1021
1022EXPORT_SYMBOL(kset_register);
1023EXPORT_SYMBOL(kset_unregister);