blob: a5a9b13b064860052d48fe640d019a69dc4c9d50 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Tejun Heoe34ff492013-09-11 22:29:05 -040022/**
23 * kobject_namespace - return @kobj's namespace tag
24 * @kobj: kobject in question
25 *
26 * Returns namespace tag of @kobj if its parent has namespace ops enabled
27 * and thus @kobj should have a namespace tag associated with it. Returns
28 * %NULL otherwise.
29 */
30const void *kobject_namespace(struct kobject *kobj)
31{
32 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
Tejun Heocb26a312013-09-11 22:29:07 -040033 const void *ns;
Tejun Heoe34ff492013-09-11 22:29:05 -040034
35 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
36 return NULL;
37
Tejun Heocb26a312013-09-11 22:29:07 -040038 ns = kobj->ktype->namespace(kobj);
39 WARN_ON(!ns); /* @kobj in a namespace is required to have !NULL tag */
40 return ns;
Tejun Heoe34ff492013-09-11 22:29:05 -040041}
42
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080043/*
44 * populate_dir - populate directory with attributes.
45 * @kobj: object we're working on.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080047 * Most subsystems have a set of default attributes that are associated
48 * with an object that registers with them. This is a helper called during
49 * object registration that loops through the default attributes of the
50 * subsystem and creates attributes files for them in sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080052static int populate_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080054 struct kobj_type *t = get_ktype(kobj);
55 struct attribute *attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int error = 0;
57 int i;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (t && t->default_attrs) {
60 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080061 error = sysfs_create_file(kobj, attr);
62 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
64 }
65 }
66 return error;
67}
68
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080069static int create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Tejun Heoe34ff492013-09-11 22:29:05 -040071 int error;
72
73 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
yan6b960612012-04-20 21:25:53 +080074 if (!error) {
75 error = populate_dir(kobj);
76 if (error)
77 sysfs_remove_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 return error;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static int get_kobj_path_length(struct kobject *kobj)
83{
84 int length = 1;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080085 struct kobject *parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -080087 /* walk up the ancestors until we hit the one pointing to the
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * root.
89 * Add 1 to strlen for leading '/' of each level.
90 */
91 do {
Chuck Ebbertb365b3d2006-01-12 20:02:00 -050092 if (kobject_name(parent) == NULL)
93 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 length += strlen(kobject_name(parent)) + 1;
95 parent = parent->parent;
96 } while (parent);
97 return length;
98}
99
100static void fill_kobj_path(struct kobject *kobj, char *path, int length)
101{
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800102 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 --length;
105 for (parent = kobj; parent; parent = parent->parent) {
106 int cur = strlen(kobject_name(parent));
107 /* back up enough to print this name with '/' */
108 length -= cur;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800109 strncpy(path + length, kobject_name(parent), cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *(path + --length) = '/';
111 }
112
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800113 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
Harvey Harrison810304d2008-04-30 00:55:08 -0700114 kobj, __func__, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800118 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
120 * @kobj: kobject in question, with which to build the path
121 * @gfp_mask: the allocation type used to allocate the path
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800122 *
123 * The result must be freed by the caller with kfree().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Al Virofd4f2df2005-10-21 03:18:50 -0400125char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 char *path;
128 int len;
129
130 len = get_kobj_path_length(kobj);
Chuck Ebbertb365b3d2006-01-12 20:02:00 -0500131 if (len == 0)
132 return NULL;
Burman Yan4668edc2006-12-06 20:38:51 -0800133 path = kzalloc(len, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!path)
135 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 fill_kobj_path(kobj, path, len);
137
138 return path;
139}
Dmitry Torokhov80fc9f52006-10-11 01:43:58 -0400140EXPORT_SYMBOL_GPL(kobject_get_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100142/* add the kobject to its kset's list */
143static void kobj_kset_join(struct kobject *kobj)
144{
145 if (!kobj->kset)
146 return;
147
148 kset_get(kobj->kset);
149 spin_lock(&kobj->kset->list_lock);
150 list_add_tail(&kobj->entry, &kobj->kset->list);
151 spin_unlock(&kobj->kset->list_lock);
152}
153
154/* remove the kobject from its kset's list */
155static void kobj_kset_leave(struct kobject *kobj)
156{
157 if (!kobj->kset)
158 return;
159
160 spin_lock(&kobj->kset->list_lock);
161 list_del_init(&kobj->entry);
162 spin_unlock(&kobj->kset->list_lock);
163 kset_put(kobj->kset);
164}
165
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800166static void kobject_init_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800168 if (!kobj)
169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 kref_init(&kobj->kref);
171 INIT_LIST_HEAD(&kobj->entry);
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800172 kobj->state_in_sysfs = 0;
173 kobj->state_add_uevent_sent = 0;
174 kobj->state_remove_uevent_sent = 0;
175 kobj->state_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700179static int kobject_add_internal(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int error = 0;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800182 struct kobject *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100184 if (!kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -ENOENT;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100186
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100187 if (!kobj->name || !kobj->name[0]) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700188 WARN(1, "kobject: (%p): attempted to be registered with empty "
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800189 "name!\n", kobj);
Greg Kroah-Hartmanc171fef2006-01-20 14:08:59 -0800190 return -EINVAL;
191 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 parent = kobject_get(kobj->parent);
194
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100195 /* join kset if set, use it as parent if we do not already have one */
196 if (kobj->kset) {
197 if (!parent)
198 parent = kobject_get(&kobj->kset->kobj);
199 kobj_kset_join(kobj);
200 kobj->parent = parent;
201 }
202
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800203 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700204 kobject_name(kobj), kobj, __func__,
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800205 parent ? kobject_name(parent) : "<NULL>",
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800206 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Eric W. Biederman90bc6132007-07-31 19:15:08 +0900208 error = create_dir(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (error) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100210 kobj_kset_leave(kobj);
211 kobject_put(parent);
212 kobj->parent = NULL;
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800213
214 /* be noisy on error issues */
215 if (error == -EEXIST)
Dan Williams282029c2012-04-06 13:41:15 -0700216 WARN(1, "%s failed for %s with "
217 "-EEXIST, don't try to register things with "
218 "the same name in the same directory.\n",
219 __func__, kobject_name(kobj));
Greg Kroah-Hartmandcd0da02006-03-20 13:17:13 -0800220 else
Dan Williams282029c2012-04-06 13:41:15 -0700221 WARN(1, "%s failed for %s (error: %d parent: %s)\n",
222 __func__, kobject_name(kobj), error,
223 parent ? kobject_name(parent) : "'none'");
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100224 } else
225 kobj->state_in_sysfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 return error;
228}
229
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700230/**
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500231 * kobject_set_name_vargs - Set the name of an kobject
232 * @kobj: struct kobject to set the name of
Greg Kroah-Hartman8c4606b2007-12-04 14:45:47 +0800233 * @fmt: format string used to build the name
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500234 * @vargs: vargs to format the string.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Kay Sievers1fa5ae82009-01-25 15:17:37 +0100236int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500237 va_list vargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Kay Sievers9f255652008-05-06 22:24:04 +0200239 const char *old_name = kobj->name;
240 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Kay Sievers8a577ff2009-04-18 15:05:45 -0700242 if (kobj->name && !fmt)
243 return 0;
244
Kay Sieversa4ca6612008-04-30 02:06:29 +0200245 kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
246 if (!kobj->name)
247 return -ENOMEM;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500248
Kay Sievers9f255652008-05-06 22:24:04 +0200249 /* ewww... some of these buggers have '/' in the name ... */
Ingo Oeser25fdeb32008-07-23 01:25:01 +0200250 while ((s = strchr(kobj->name, '/')))
Kay Sievers9f255652008-05-06 22:24:04 +0200251 s[0] = '!';
252
253 kfree(old_name);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500254 return 0;
255}
256
257/**
258 * kobject_set_name - Set the name of a kobject
259 * @kobj: struct kobject to set the name of
260 * @fmt: format string used to build the name
261 *
262 * This sets the name of the kobject. If you have already added the
263 * kobject to the system, you must call kobject_rename() in order to
264 * change the name of the kobject.
265 */
266int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
267{
Kay Sieversa4ca6612008-04-30 02:06:29 +0200268 va_list vargs;
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500269 int retval;
270
Kay Sieversa4ca6612008-04-30 02:06:29 +0200271 va_start(vargs, fmt);
272 retval = kobject_set_name_vargs(kobj, fmt, vargs);
273 va_end(vargs);
Greg Kroah-Hartman663a4742007-11-29 18:32:47 -0500274
275 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277EXPORT_SYMBOL(kobject_set_name);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/**
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700280 * kobject_init - initialize a kobject structure
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800281 * @kobj: pointer to the kobject to initialize
282 * @ktype: pointer to the ktype for this kobject.
283 *
284 * This function will properly initialize a kobject such that it can then
285 * be passed to the kobject_add() call.
286 *
287 * After this function is called, the kobject MUST be cleaned up by a call
288 * to kobject_put(), not by a call to kfree directly to ensure that all of
289 * the memory is cleaned up properly.
290 */
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700291void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800292{
293 char *err_str;
294
295 if (!kobj) {
296 err_str = "invalid kobject pointer!";
297 goto error;
298 }
299 if (!ktype) {
300 err_str = "must have a ktype to be initialized properly!\n";
301 goto error;
302 }
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100303 if (kobj->state_initialized) {
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800304 /* do not error out as sometimes we can recover */
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100305 printk(KERN_ERR "kobject (%p): tried to init an initialized "
306 "object, something is seriously wrong.\n", kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800307 dump_stack();
308 }
309
Greg Kroah-Hartmana4573c42008-02-26 09:36:38 -0800310 kobject_init_internal(kobj);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800311 kobj->ktype = ktype;
312 return;
313
314error:
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100315 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800316 dump_stack();
317}
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700318EXPORT_SYMBOL(kobject_init);
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800319
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800320static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
321 const char *fmt, va_list vargs)
322{
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800323 int retval;
324
Kay Sieversa4ca6612008-04-30 02:06:29 +0200325 retval = kobject_set_name_vargs(kobj, fmt, vargs);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800326 if (retval) {
327 printk(KERN_ERR "kobject: can not set name properly!\n");
328 return retval;
329 }
330 kobj->parent = parent;
Greg Kroah-Hartman9e7bbcc2007-12-17 23:05:35 -0700331 return kobject_add_internal(kobj);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800332}
333
334/**
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700335 * kobject_add - the main kobject add function
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800336 * @kobj: the kobject to add
337 * @parent: pointer to the parent of the kobject.
338 * @fmt: format to name the kobject with.
339 *
340 * The kobject name is set and added to the kobject hierarchy in this
341 * function.
342 *
343 * If @parent is set, then the parent of the @kobj will be set to it.
344 * If @parent is NULL, then the parent of the @kobj will be set to the
345 * kobject associted with the kset assigned to this kobject. If no kset
346 * is assigned to the kobject, then the kobject will be located in the
347 * root of the sysfs tree.
348 *
349 * If this function returns an error, kobject_put() must be called to
350 * properly clean up the memory associated with the object.
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800351 * Under no instance should the kobject that is passed to this function
352 * be directly freed with a call to kfree(), that can leak memory.
353 *
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100354 * Note, no "add" uevent will be created with this call, the caller should set
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800355 * up all of the necessary sysfs files for the object and then call
356 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
357 * userspace is properly notified of this kobject's creation.
358 */
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700359int kobject_add(struct kobject *kobj, struct kobject *parent,
360 const char *fmt, ...)
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800361{
362 va_list args;
363 int retval;
364
365 if (!kobj)
366 return -EINVAL;
367
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100368 if (!kobj->state_initialized) {
369 printk(KERN_ERR "kobject '%s' (%p): tried to add an "
370 "uninitialized object, something is seriously wrong.\n",
371 kobject_name(kobj), kobj);
372 dump_stack();
373 return -EINVAL;
374 }
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800375 va_start(args, fmt);
376 retval = kobject_add_varg(kobj, parent, fmt, args);
377 va_end(args);
378
379 return retval;
380}
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700381EXPORT_SYMBOL(kobject_add);
Greg Kroah-Hartman244f6ce2007-12-03 21:31:08 -0800382
Greg Kroah-Hartmane86000d2007-12-03 21:31:08 -0800383/**
Greg Kroah-Hartmanc11c4152007-12-03 21:31:08 -0800384 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
385 * @kobj: pointer to the kobject to initialize
386 * @ktype: pointer to the ktype for this kobject.
387 * @parent: pointer to the parent of this kobject.
388 * @fmt: the name of the kobject.
389 *
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700390 * This function combines the call to kobject_init() and
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700391 * kobject_add(). The same type of error handling after a call to
392 * kobject_add() and kobject lifetime rules are the same here.
Greg Kroah-Hartmanc11c4152007-12-03 21:31:08 -0800393 */
394int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
395 struct kobject *parent, const char *fmt, ...)
396{
397 va_list args;
398 int retval;
399
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700400 kobject_init(kobj, ktype);
Greg Kroah-Hartmanc11c4152007-12-03 21:31:08 -0800401
402 va_start(args, fmt);
403 retval = kobject_add_varg(kobj, parent, fmt, args);
404 va_end(args);
405
406 return retval;
407}
408EXPORT_SYMBOL_GPL(kobject_init_and_add);
409
410/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800411 * kobject_rename - change the name of an object
412 * @kobj: object in question.
413 * @new_name: object's new name
Eric W. Biederman030c1d22008-05-08 14:41:00 -0700414 *
415 * It is the responsibility of the caller to provide mutual
416 * exclusion between two different calls of kobject_rename
417 * on the same kobject and to ensure that new_name is valid and
418 * won't conflict with other kobjects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800420int kobject_rename(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int error = 0;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800423 const char *devpath = NULL;
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700424 const char *dup_name = NULL, *name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800425 char *devpath_string = NULL;
426 char *envp[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 kobj = kobject_get(kobj);
429 if (!kobj)
430 return -EINVAL;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700431 if (!kobj->parent)
432 return -EINVAL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800433
434 devpath = kobject_get_path(kobj, GFP_KERNEL);
435 if (!devpath) {
436 error = -ENOMEM;
437 goto out;
438 }
439 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
440 if (!devpath_string) {
441 error = -ENOMEM;
442 goto out;
443 }
444 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
445 envp[0] = devpath_string;
446 envp[1] = NULL;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800447
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700448 name = dup_name = kstrdup(new_name, GFP_KERNEL);
449 if (!name) {
450 error = -ENOMEM;
451 goto out;
452 }
453
Tejun Heoe34ff492013-09-11 22:29:05 -0400454 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700455 if (error)
456 goto out;
457
458 /* Install the new kobject name */
459 dup_name = kobj->name;
460 kobj->name = name;
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800461
462 /* This function is mostly/only used for network interface.
463 * Some hotplug package track interfaces by their name and
464 * therefore want to know when the name is changed by the user. */
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700465 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800466
467out:
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700468 kfree(dup_name);
Jean Tourrilhesca2f37d2007-03-07 10:49:30 -0800469 kfree(devpath_string);
470 kfree(devpath);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700471 kobject_put(kobj);
472
473 return error;
474}
Alex Chiang8344b562008-06-10 15:30:42 -0600475EXPORT_SYMBOL_GPL(kobject_rename);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700476
477/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800478 * kobject_move - move object to another parent
479 * @kobj: object in question.
480 * @new_parent: object's new parent (can be NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +0100481 */
Cornelia Huck8a824722006-11-20 17:07:51 +0100482int kobject_move(struct kobject *kobj, struct kobject *new_parent)
483{
484 int error;
485 struct kobject *old_parent;
486 const char *devpath = NULL;
487 char *devpath_string = NULL;
488 char *envp[2];
489
490 kobj = kobject_get(kobj);
491 if (!kobj)
492 return -EINVAL;
493 new_parent = kobject_get(new_parent);
494 if (!new_parent) {
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100495 if (kobj->kset)
496 new_parent = kobject_get(&kobj->kset->kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +0100497 }
Tejun Heoe34ff492013-09-11 22:29:05 -0400498
Cornelia Huck8a824722006-11-20 17:07:51 +0100499 /* old object path */
500 devpath = kobject_get_path(kobj, GFP_KERNEL);
501 if (!devpath) {
502 error = -ENOMEM;
503 goto out;
504 }
505 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
506 if (!devpath_string) {
507 error = -ENOMEM;
508 goto out;
509 }
510 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
511 envp[0] = devpath_string;
512 envp[1] = NULL;
Tejun Heoe34ff492013-09-11 22:29:05 -0400513 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
Cornelia Huck8a824722006-11-20 17:07:51 +0100514 if (error)
515 goto out;
516 old_parent = kobj->parent;
517 kobj->parent = new_parent;
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300518 new_parent = NULL;
Cornelia Huck8a824722006-11-20 17:07:51 +0100519 kobject_put(old_parent);
520 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
521out:
Dmitriy Monakhov9e993ef2007-03-03 16:11:21 +0300522 kobject_put(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +0100523 kobject_put(kobj);
524 kfree(devpath_string);
525 kfree(devpath);
526 return error;
527}
528
529/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800530 * kobject_del - unlink kobject from hierarchy.
531 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800533void kobject_del(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800535 if (!kobj)
536 return;
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 sysfs_remove_dir(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100539 kobj->state_in_sysfs = 0;
540 kobj_kset_leave(kobj);
541 kobject_put(kobj->parent);
542 kobj->parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800546 * kobject_get - increment refcount for object.
547 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800549struct kobject *kobject_get(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 if (kobj)
552 kref_get(&kobj->kref);
553 return kobj;
554}
555
Anatol Pomozov2d864e42013-05-07 15:37:48 -0700556static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700557{
558 if (!kref_get_unless_zero(&kobj->kref))
559 kobj = NULL;
560 return kobj;
561}
562
Greg Kroah-Hartman18041f42007-12-03 21:31:08 -0800563/*
564 * kobject_cleanup - free kobject resources.
565 * @kobj: object to cleanup
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Greg Kroah-Hartman18041f42007-12-03 21:31:08 -0800567static void kobject_cleanup(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100569 struct kobj_type *t = get_ktype(kobj);
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100570 const char *name = kobj->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Russell Kingc817a672013-06-27 15:06:14 +0100572 pr_debug("kobject: '%s' (%p): %s, parent %p\n",
573 kobject_name(kobj), kobj, __func__, kobj->parent);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100574
575 if (t && !t->release)
576 pr_debug("kobject: '%s' (%p): does not have a release() "
577 "function, it is broken and must be fixed.\n",
578 kobject_name(kobj), kobj);
579
580 /* send "remove" if the caller did not do it but sent "add" */
581 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
582 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
583 kobject_name(kobj), kobj);
584 kobject_uevent(kobj, KOBJ_REMOVE);
585 }
586
587 /* remove from sysfs if the caller did not do it */
588 if (kobj->state_in_sysfs) {
589 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
590 kobject_name(kobj), kobj);
591 kobject_del(kobj);
592 }
593
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700594 if (t && t->release) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100595 pr_debug("kobject: '%s' (%p): calling ktype release\n",
596 kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 t->release(kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100598 }
599
600 /* free name if we allocated it */
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100601 if (name) {
Kay Sievers0f4dafc2007-12-19 01:40:42 +0100602 pr_debug("kobject: '%s': free name\n", name);
Greg Kroah-Hartmance2c9cb2007-09-12 15:06:57 -0700603 kfree(name);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Russell Kingc817a672013-06-27 15:06:14 +0100607#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
608static void kobject_delayed_cleanup(struct work_struct *work)
609{
610 kobject_cleanup(container_of(to_delayed_work(work),
611 struct kobject, release));
612}
613#endif
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static void kobject_release(struct kref *kref)
616{
Russell Kingc817a672013-06-27 15:06:14 +0100617 struct kobject *kobj = container_of(kref, struct kobject, kref);
618#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
619 pr_debug("kobject: '%s' (%p): %s, parent %p (delayed)\n",
620 kobject_name(kobj), kobj, __func__, kobj->parent);
621 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
622 schedule_delayed_work(&kobj->release, HZ);
623#else
624 kobject_cleanup(kobj);
625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800629 * kobject_put - decrement refcount for object.
630 * @kobj: object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800632 * Decrement the refcount, and if 0, call kobject_cleanup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800634void kobject_put(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800636 if (kobj) {
Arjan van de Vend955c782008-07-25 01:45:55 -0700637 if (!kobj->state_initialized)
638 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800639 "initialized, yet kobject_put() is being "
640 "called.\n", kobject_name(kobj), kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kref_put(&kobj->kref, kobject_release);
Greg Kroah-Hartmanc1ebdae2008-02-26 09:36:38 -0800642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800645static void dynamic_kobj_release(struct kobject *kobj)
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500646{
Harvey Harrison810304d2008-04-30 00:55:08 -0700647 pr_debug("kobject: (%p): %s\n", kobj, __func__);
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500648 kfree(kobj);
649}
650
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800651static struct kobj_type dynamic_kobj_ktype = {
Kay Sievers386f2752007-11-02 13:47:53 +0100652 .release = dynamic_kobj_release,
653 .sysfs_ops = &kobj_sysfs_ops,
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500654};
655
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800656/**
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800657 * kobject_create - create a struct kobject dynamically
658 *
659 * This function creates a kobject structure dynamically and sets it up
660 * to be a "dynamic" kobject with a default release function set up.
661 *
662 * If the kobject was not able to be created, NULL will be returned.
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800663 * The kobject structure returned from here must be cleaned up with a
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700664 * call to kobject_put() and not kfree(), as kobject_init() has
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800665 * already been called on this structure.
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800666 */
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -0800667struct kobject *kobject_create(void)
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800668{
669 struct kobject *kobj;
670
671 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
672 if (!kobj)
673 return NULL;
674
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700675 kobject_init(kobj, &dynamic_kobj_ktype);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800676 return kobj;
677}
678
679/**
680 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
681 *
Zhi Yong Wu9ff1f832012-05-07 10:48:25 +0800682 * @name: the name for the kobject
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800683 * @parent: the parent kobject of this kobject, if any.
684 *
Dave Youngf70701a2008-01-28 16:58:00 +0800685 * This function creates a kobject structure dynamically and registers it
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800686 * with sysfs. When you are finished with this structure, call
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800687 * kobject_put() and the structure will be dynamically freed when
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800688 * it is no longer being used.
689 *
690 * If the kobject was not able to be created, NULL will be returned.
691 */
692struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
693{
694 struct kobject *kobj;
695 int retval;
696
697 kobj = kobject_create();
698 if (!kobj)
699 return NULL;
700
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -0700701 retval = kobject_add(kobj, parent, "%s", name);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800702 if (retval) {
703 printk(KERN_WARNING "%s: kobject_add error: %d\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700704 __func__, retval);
Greg Kroah-Hartman3f9e3ee2007-11-05 13:16:15 -0800705 kobject_put(kobj);
706 kobj = NULL;
707 }
708 return kobj;
709}
710EXPORT_SYMBOL_GPL(kobject_create_and_add);
711
Jun'ichi Nomura74231722006-03-13 17:14:25 -0500712/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800713 * kset_init - initialize a kset for use
714 * @k: kset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800716void kset_init(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Greg Kroah-Hartmane1543dd2007-12-17 23:05:35 -0700718 kobject_init_internal(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 INIT_LIST_HEAD(&k->list);
720 spin_lock_init(&k->list_lock);
721}
722
Kay Sievers23b52122007-11-02 13:47:53 +0100723/* default kobject attribute operations */
724static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
725 char *buf)
726{
727 struct kobj_attribute *kattr;
728 ssize_t ret = -EIO;
729
730 kattr = container_of(attr, struct kobj_attribute, attr);
731 if (kattr->show)
732 ret = kattr->show(kobj, kattr, buf);
733 return ret;
734}
735
736static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
737 const char *buf, size_t count)
738{
739 struct kobj_attribute *kattr;
740 ssize_t ret = -EIO;
741
742 kattr = container_of(attr, struct kobj_attribute, attr);
743 if (kattr->store)
744 ret = kattr->store(kobj, kattr, buf, count);
745 return ret;
746}
747
Emese Revfy52cf25d2010-01-19 02:58:23 +0100748const struct sysfs_ops kobj_sysfs_ops = {
Kay Sievers23b52122007-11-02 13:47:53 +0100749 .show = kobj_attr_show,
750 .store = kobj_attr_store,
751};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753/**
Jeff Mahoneyeee03162013-09-11 13:00:30 -0400754 * kobj_completion_init - initialize a kobj_completion object.
755 * @kc: kobj_completion
756 * @ktype: type of kobject to initialize
757 *
758 * kobj_completion structures can be embedded within structures with different
759 * lifetime rules. During the release of the enclosing object, we can
760 * wait on the release of the kobject so that we don't free it while it's
761 * still busy.
762 */
763void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype)
764{
765 init_completion(&kc->kc_unregister);
766 kobject_init(&kc->kc_kobj, ktype);
767}
768EXPORT_SYMBOL_GPL(kobj_completion_init);
769
770/**
771 * kobj_completion_release - release a kobj_completion object
772 * @kobj: kobject embedded in kobj_completion
773 *
774 * Used with kobject_release to notify waiters that the kobject has been
775 * released.
776 */
777void kobj_completion_release(struct kobject *kobj)
778{
779 struct kobj_completion *kc = kobj_to_kobj_completion(kobj);
780 complete(&kc->kc_unregister);
781}
782EXPORT_SYMBOL_GPL(kobj_completion_release);
783
784/**
785 * kobj_completion_del_and_wait - release the kobject and wait for it
786 * @kc: kobj_completion object to release
787 *
788 * Delete the kobject from sysfs and drop the reference count. Then wait
789 * until any other outstanding references are also dropped. This routine
790 * is only necessary once other references may have been taken on the
791 * kobject. Typically this happens when the kobject has been published
792 * to sysfs via kobject_add.
793 */
794void kobj_completion_del_and_wait(struct kobj_completion *kc)
795{
796 kobject_del(&kc->kc_kobj);
797 kobject_put(&kc->kc_kobj);
798 wait_for_completion(&kc->kc_unregister);
799}
800EXPORT_SYMBOL_GPL(kobj_completion_del_and_wait);
801
802/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800803 * kset_register - initialize and add a kset.
804 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800806int kset_register(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Kay Sievers80f03e32007-05-26 11:21:36 +0200808 int err;
809
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800810 if (!k)
811 return -EINVAL;
Kay Sievers80f03e32007-05-26 11:21:36 +0200812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 kset_init(k);
Greg Kroah-Hartman12e339a2002-04-09 12:14:34 -0700814 err = kobject_add_internal(&k->kobj);
Kay Sievers80f03e32007-05-26 11:21:36 +0200815 if (err)
816 return err;
817 kobject_uevent(&k->kobj, KOBJ_ADD);
818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800822 * kset_unregister - remove a kset.
823 * @k: kset.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800825void kset_unregister(struct kset *k)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Greg Kroah-Hartman31b9025a2007-01-18 12:23:51 -0800827 if (!k)
828 return;
Greg Kroah-Hartman78a2d902007-12-20 08:13:05 -0800829 kobject_put(&k->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832/**
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800833 * kset_find_obj - search for object in kset.
834 * @kset: kset we're looking in.
835 * @name: object's name.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800837 * Lock kset via @kset->subsys, and iterate over @kset->list,
838 * looking for a matching kobject. If matching object is found
839 * take a reference and return the object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800841struct kobject *kset_find_obj(struct kset *kset, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400843 struct kobject *k;
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800844 struct kobject *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 spin_lock(&kset->list_lock);
Robin Holtc25d1df2010-09-29 14:00:54 -0500847
Robert P. J. Dayc6a2a3d2008-03-27 01:13:34 -0400848 list_for_each_entry(k, &kset->list, entry) {
Greg Kroah-Hartmane374a2b2008-01-24 21:59:04 -0800849 if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
Linus Torvaldsa49b7e82013-04-13 15:15:30 -0700850 ret = kobject_get_unless_zero(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852 }
853 }
Robin Holtc25d1df2010-09-29 14:00:54 -0500854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_unlock(&kset->list_lock);
856 return ret;
857}
858
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700859static void kset_release(struct kobject *kobj)
860{
861 struct kset *kset = container_of(kobj, struct kset, kobj);
Greg Kroah-Hartman9f66fa22007-11-28 23:49:41 -0800862 pr_debug("kobject: '%s' (%p): %s\n",
Harvey Harrison810304d2008-04-30 00:55:08 -0700863 kobject_name(kobj), kobj, __func__);
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700864 kfree(kset);
865}
866
Kay Sievers386f2752007-11-02 13:47:53 +0100867static struct kobj_type kset_ktype = {
868 .sysfs_ops = &kobj_sysfs_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700869 .release = kset_release,
870};
871
872/**
873 * kset_create - create a struct kset dynamically
874 *
875 * @name: the name for the kset
876 * @uevent_ops: a struct kset_uevent_ops for the kset
877 * @parent_kobj: the parent kobject of this kset, if any.
878 *
879 * This function creates a kset structure dynamically. This structure can
880 * then be registered with the system and show up in sysfs with a call to
881 * kset_register(). When you are finished with this structure, if
882 * kset_register() has been called, call kset_unregister() and the
883 * structure will be dynamically freed when it is no longer being used.
884 *
885 * If the kset was not able to be created, NULL will be returned.
886 */
887static struct kset *kset_create(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100888 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700889 struct kobject *parent_kobj)
890{
891 struct kset *kset;
Dave Youngd9cd8f32009-05-11 14:17:45 +0800892 int retval;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700893
894 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
895 if (!kset)
896 return NULL;
Kees Cookb7165eb2013-06-06 13:52:19 -0700897 retval = kobject_set_name(&kset->kobj, "%s", name);
Dave Youngd9cd8f32009-05-11 14:17:45 +0800898 if (retval) {
899 kfree(kset);
900 return NULL;
901 }
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700902 kset->uevent_ops = uevent_ops;
903 kset->kobj.parent = parent_kobj;
904
905 /*
Kay Sievers386f2752007-11-02 13:47:53 +0100906 * The kobject of this kset will have a type of kset_ktype and belong to
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700907 * no kset itself. That way we can properly free it when it is
908 * finished being used.
909 */
Kay Sievers386f2752007-11-02 13:47:53 +0100910 kset->kobj.ktype = &kset_ktype;
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700911 kset->kobj.kset = NULL;
912
913 return kset;
914}
915
916/**
917 * kset_create_and_add - create a struct kset dynamically and add it to sysfs
918 *
919 * @name: the name for the kset
920 * @uevent_ops: a struct kset_uevent_ops for the kset
921 * @parent_kobj: the parent kobject of this kset, if any.
922 *
923 * This function creates a kset structure dynamically and registers it
924 * with sysfs. When you are finished with this structure, call
925 * kset_unregister() and the structure will be dynamically freed when it
926 * is no longer being used.
927 *
928 * If the kset was not able to be created, NULL will be returned.
929 */
930struct kset *kset_create_and_add(const char *name,
Emese Revfy9cd43612009-12-31 14:52:51 +0100931 const struct kset_uevent_ops *uevent_ops,
Greg Kroah-Hartmanb727c702007-09-27 14:48:53 -0700932 struct kobject *parent_kobj)
933{
934 struct kset *kset;
935 int error;
936
937 kset = kset_create(name, uevent_ops, parent_kobj);
938 if (!kset)
939 return NULL;
940 error = kset_register(kset);
941 if (error) {
942 kfree(kset);
943 return NULL;
944 }
945 return kset;
946}
947EXPORT_SYMBOL_GPL(kset_create_and_add);
948
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700949
950static DEFINE_SPINLOCK(kobj_ns_type_lock);
951static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
952
953int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
954{
955 enum kobj_ns_type type = ops->type;
956 int error;
957
958 spin_lock(&kobj_ns_type_lock);
959
960 error = -EINVAL;
961 if (type >= KOBJ_NS_TYPES)
962 goto out;
963
964 error = -EINVAL;
965 if (type <= KOBJ_NS_TYPE_NONE)
966 goto out;
967
968 error = -EBUSY;
969 if (kobj_ns_ops_tbl[type])
970 goto out;
971
972 error = 0;
973 kobj_ns_ops_tbl[type] = ops;
974
975out:
976 spin_unlock(&kobj_ns_type_lock);
977 return error;
978}
979
980int kobj_ns_type_registered(enum kobj_ns_type type)
981{
982 int registered = 0;
983
984 spin_lock(&kobj_ns_type_lock);
985 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
986 registered = kobj_ns_ops_tbl[type] != NULL;
987 spin_unlock(&kobj_ns_type_lock);
988
989 return registered;
990}
991
992const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
993{
994 const struct kobj_ns_type_operations *ops = NULL;
995
996 if (parent && parent->ktype->child_ns_type)
997 ops = parent->ktype->child_ns_type(parent);
998
999 return ops;
1000}
1001
1002const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
1003{
1004 return kobj_child_ns_ops(kobj->parent);
1005}
1006
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -07001007bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1008{
1009 bool may_mount = false;
1010
1011 if (type == KOBJ_NS_TYPE_NONE)
1012 return true;
1013
1014 spin_lock(&kobj_ns_type_lock);
1015 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1016 kobj_ns_ops_tbl[type])
1017 may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
1018 spin_unlock(&kobj_ns_type_lock);
1019
1020 return may_mount;
1021}
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001022
Al Viroa685e082011-06-08 21:13:01 -04001023void *kobj_ns_grab_current(enum kobj_ns_type type)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001024{
Al Viroa685e082011-06-08 21:13:01 -04001025 void *ns = NULL;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001026
1027 spin_lock(&kobj_ns_type_lock);
1028 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1029 kobj_ns_ops_tbl[type])
Al Viroa685e082011-06-08 21:13:01 -04001030 ns = kobj_ns_ops_tbl[type]->grab_current_ns();
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001031 spin_unlock(&kobj_ns_type_lock);
1032
1033 return ns;
1034}
1035
1036const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
1037{
1038 const void *ns = NULL;
1039
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 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
1044 spin_unlock(&kobj_ns_type_lock);
1045
1046 return ns;
1047}
1048
1049const void *kobj_ns_initial(enum kobj_ns_type type)
1050{
1051 const void *ns = NULL;
1052
1053 spin_lock(&kobj_ns_type_lock);
1054 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1055 kobj_ns_ops_tbl[type])
1056 ns = kobj_ns_ops_tbl[type]->initial_ns();
1057 spin_unlock(&kobj_ns_type_lock);
1058
1059 return ns;
1060}
1061
Al Viroa685e082011-06-08 21:13:01 -04001062void kobj_ns_drop(enum kobj_ns_type type, void *ns)
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001063{
Al Viroa685e082011-06-08 21:13:01 -04001064 spin_lock(&kobj_ns_type_lock);
1065 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1066 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1067 kobj_ns_ops_tbl[type]->drop_ns(ns);
1068 spin_unlock(&kobj_ns_type_lock);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071EXPORT_SYMBOL(kobject_get);
1072EXPORT_SYMBOL(kobject_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073EXPORT_SYMBOL(kobject_del);
1074
1075EXPORT_SYMBOL(kset_register);
1076EXPORT_SYMBOL(kset_unregister);