Back to home page

LXR

 
 

    


0001 /*
0002  * kobject.c - library routines for handling generic kernel objects
0003  *
0004  * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
0005  * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
0006  * Copyright (c) 2006-2007 Novell Inc.
0007  *
0008  * This file is released under the GPLv2.
0009  *
0010  *
0011  * Please see the file Documentation/kobject.txt for critical information
0012  * about using the kobject interface.
0013  */
0014 
0015 #include <linux/kobject.h>
0016 #include <linux/string.h>
0017 #include <linux/export.h>
0018 #include <linux/stat.h>
0019 #include <linux/slab.h>
0020 #include <linux/random.h>
0021 
0022 /**
0023  * kobject_namespace - return @kobj's namespace tag
0024  * @kobj: kobject in question
0025  *
0026  * Returns namespace tag of @kobj if its parent has namespace ops enabled
0027  * and thus @kobj should have a namespace tag associated with it.  Returns
0028  * %NULL otherwise.
0029  */
0030 const void *kobject_namespace(struct kobject *kobj)
0031 {
0032     const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
0033 
0034     if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
0035         return NULL;
0036 
0037     return kobj->ktype->namespace(kobj);
0038 }
0039 
0040 /*
0041  * populate_dir - populate directory with attributes.
0042  * @kobj: object we're working on.
0043  *
0044  * Most subsystems have a set of default attributes that are associated
0045  * with an object that registers with them.  This is a helper called during
0046  * object registration that loops through the default attributes of the
0047  * subsystem and creates attributes files for them in sysfs.
0048  */
0049 static int populate_dir(struct kobject *kobj)
0050 {
0051     struct kobj_type *t = get_ktype(kobj);
0052     struct attribute *attr;
0053     int error = 0;
0054     int i;
0055 
0056     if (t && t->default_attrs) {
0057         for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
0058             error = sysfs_create_file(kobj, attr);
0059             if (error)
0060                 break;
0061         }
0062     }
0063     return error;
0064 }
0065 
0066 static int create_dir(struct kobject *kobj)
0067 {
0068     const struct kobj_ns_type_operations *ops;
0069     int error;
0070 
0071     error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
0072     if (error)
0073         return error;
0074 
0075     error = populate_dir(kobj);
0076     if (error) {
0077         sysfs_remove_dir(kobj);
0078         return error;
0079     }
0080 
0081     /*
0082      * @kobj->sd may be deleted by an ancestor going away.  Hold an
0083      * extra reference so that it stays until @kobj is gone.
0084      */
0085     sysfs_get(kobj->sd);
0086 
0087     /*
0088      * If @kobj has ns_ops, its children need to be filtered based on
0089      * their namespace tags.  Enable namespace support on @kobj->sd.
0090      */
0091     ops = kobj_child_ns_ops(kobj);
0092     if (ops) {
0093         BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
0094         BUG_ON(ops->type >= KOBJ_NS_TYPES);
0095         BUG_ON(!kobj_ns_type_registered(ops->type));
0096 
0097         sysfs_enable_ns(kobj->sd);
0098     }
0099 
0100     return 0;
0101 }
0102 
0103 static int get_kobj_path_length(struct kobject *kobj)
0104 {
0105     int length = 1;
0106     struct kobject *parent = kobj;
0107 
0108     /* walk up the ancestors until we hit the one pointing to the
0109      * root.
0110      * Add 1 to strlen for leading '/' of each level.
0111      */
0112     do {
0113         if (kobject_name(parent) == NULL)
0114             return 0;
0115         length += strlen(kobject_name(parent)) + 1;
0116         parent = parent->parent;
0117     } while (parent);
0118     return length;
0119 }
0120 
0121 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
0122 {
0123     struct kobject *parent;
0124 
0125     --length;
0126     for (parent = kobj; parent; parent = parent->parent) {
0127         int cur = strlen(kobject_name(parent));
0128         /* back up enough to print this name with '/' */
0129         length -= cur;
0130         strncpy(path + length, kobject_name(parent), cur);
0131         *(path + --length) = '/';
0132     }
0133 
0134     pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
0135          kobj, __func__, path);
0136 }
0137 
0138 /**
0139  * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
0140  *
0141  * @kobj:   kobject in question, with which to build the path
0142  * @gfp_mask:   the allocation type used to allocate the path
0143  *
0144  * The result must be freed by the caller with kfree().
0145  */
0146 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
0147 {
0148     char *path;
0149     int len;
0150 
0151     len = get_kobj_path_length(kobj);
0152     if (len == 0)
0153         return NULL;
0154     path = kzalloc(len, gfp_mask);
0155     if (!path)
0156         return NULL;
0157     fill_kobj_path(kobj, path, len);
0158 
0159     return path;
0160 }
0161 EXPORT_SYMBOL_GPL(kobject_get_path);
0162 
0163 /* add the kobject to its kset's list */
0164 static void kobj_kset_join(struct kobject *kobj)
0165 {
0166     if (!kobj->kset)
0167         return;
0168 
0169     kset_get(kobj->kset);
0170     spin_lock(&kobj->kset->list_lock);
0171     list_add_tail(&kobj->entry, &kobj->kset->list);
0172     spin_unlock(&kobj->kset->list_lock);
0173 }
0174 
0175 /* remove the kobject from its kset's list */
0176 static void kobj_kset_leave(struct kobject *kobj)
0177 {
0178     if (!kobj->kset)
0179         return;
0180 
0181     spin_lock(&kobj->kset->list_lock);
0182     list_del_init(&kobj->entry);
0183     spin_unlock(&kobj->kset->list_lock);
0184     kset_put(kobj->kset);
0185 }
0186 
0187 static void kobject_init_internal(struct kobject *kobj)
0188 {
0189     if (!kobj)
0190         return;
0191     kref_init(&kobj->kref);
0192     INIT_LIST_HEAD(&kobj->entry);
0193     kobj->state_in_sysfs = 0;
0194     kobj->state_add_uevent_sent = 0;
0195     kobj->state_remove_uevent_sent = 0;
0196     kobj->state_initialized = 1;
0197 }
0198 
0199 
0200 static int kobject_add_internal(struct kobject *kobj)
0201 {
0202     int error = 0;
0203     struct kobject *parent;
0204 
0205     if (!kobj)
0206         return -ENOENT;
0207 
0208     if (!kobj->name || !kobj->name[0]) {
0209         WARN(1, "kobject: (%p): attempted to be registered with empty "
0210              "name!\n", kobj);
0211         return -EINVAL;
0212     }
0213 
0214     parent = kobject_get(kobj->parent);
0215 
0216     /* join kset if set, use it as parent if we do not already have one */
0217     if (kobj->kset) {
0218         if (!parent)
0219             parent = kobject_get(&kobj->kset->kobj);
0220         kobj_kset_join(kobj);
0221         kobj->parent = parent;
0222     }
0223 
0224     pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
0225          kobject_name(kobj), kobj, __func__,
0226          parent ? kobject_name(parent) : "<NULL>",
0227          kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
0228 
0229     error = create_dir(kobj);
0230     if (error) {
0231         kobj_kset_leave(kobj);
0232         kobject_put(parent);
0233         kobj->parent = NULL;
0234 
0235         /* be noisy on error issues */
0236         if (error == -EEXIST)
0237             WARN(1, "%s failed for %s with "
0238                  "-EEXIST, don't try to register things with "
0239                  "the same name in the same directory.\n",
0240                  __func__, kobject_name(kobj));
0241         else
0242             WARN(1, "%s failed for %s (error: %d parent: %s)\n",
0243                  __func__, kobject_name(kobj), error,
0244                  parent ? kobject_name(parent) : "'none'");
0245     } else
0246         kobj->state_in_sysfs = 1;
0247 
0248     return error;
0249 }
0250 
0251 /**
0252  * kobject_set_name_vargs - Set the name of an kobject
0253  * @kobj: struct kobject to set the name of
0254  * @fmt: format string used to build the name
0255  * @vargs: vargs to format the string.
0256  */
0257 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
0258                   va_list vargs)
0259 {
0260     const char *s;
0261 
0262     if (kobj->name && !fmt)
0263         return 0;
0264 
0265     s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
0266     if (!s)
0267         return -ENOMEM;
0268 
0269     /*
0270      * ewww... some of these buggers have '/' in the name ... If
0271      * that's the case, we need to make sure we have an actual
0272      * allocated copy to modify, since kvasprintf_const may have
0273      * returned something from .rodata.
0274      */
0275     if (strchr(s, '/')) {
0276         char *t;
0277 
0278         t = kstrdup(s, GFP_KERNEL);
0279         kfree_const(s);
0280         if (!t)
0281             return -ENOMEM;
0282         strreplace(t, '/', '!');
0283         s = t;
0284     }
0285     kfree_const(kobj->name);
0286     kobj->name = s;
0287 
0288     return 0;
0289 }
0290 
0291 /**
0292  * kobject_set_name - Set the name of a kobject
0293  * @kobj: struct kobject to set the name of
0294  * @fmt: format string used to build the name
0295  *
0296  * This sets the name of the kobject.  If you have already added the
0297  * kobject to the system, you must call kobject_rename() in order to
0298  * change the name of the kobject.
0299  */
0300 int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
0301 {
0302     va_list vargs;
0303     int retval;
0304 
0305     va_start(vargs, fmt);
0306     retval = kobject_set_name_vargs(kobj, fmt, vargs);
0307     va_end(vargs);
0308 
0309     return retval;
0310 }
0311 EXPORT_SYMBOL(kobject_set_name);
0312 
0313 /**
0314  * kobject_init - initialize a kobject structure
0315  * @kobj: pointer to the kobject to initialize
0316  * @ktype: pointer to the ktype for this kobject.
0317  *
0318  * This function will properly initialize a kobject such that it can then
0319  * be passed to the kobject_add() call.
0320  *
0321  * After this function is called, the kobject MUST be cleaned up by a call
0322  * to kobject_put(), not by a call to kfree directly to ensure that all of
0323  * the memory is cleaned up properly.
0324  */
0325 void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
0326 {
0327     char *err_str;
0328 
0329     if (!kobj) {
0330         err_str = "invalid kobject pointer!";
0331         goto error;
0332     }
0333     if (!ktype) {
0334         err_str = "must have a ktype to be initialized properly!\n";
0335         goto error;
0336     }
0337     if (kobj->state_initialized) {
0338         /* do not error out as sometimes we can recover */
0339         printk(KERN_ERR "kobject (%p): tried to init an initialized "
0340                "object, something is seriously wrong.\n", kobj);
0341         dump_stack();
0342     }
0343 
0344     kobject_init_internal(kobj);
0345     kobj->ktype = ktype;
0346     return;
0347 
0348 error:
0349     printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
0350     dump_stack();
0351 }
0352 EXPORT_SYMBOL(kobject_init);
0353 
0354 static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
0355                        struct kobject *parent,
0356                        const char *fmt, va_list vargs)
0357 {
0358     int retval;
0359 
0360     retval = kobject_set_name_vargs(kobj, fmt, vargs);
0361     if (retval) {
0362         printk(KERN_ERR "kobject: can not set name properly!\n");
0363         return retval;
0364     }
0365     kobj->parent = parent;
0366     return kobject_add_internal(kobj);
0367 }
0368 
0369 /**
0370  * kobject_add - the main kobject add function
0371  * @kobj: the kobject to add
0372  * @parent: pointer to the parent of the kobject.
0373  * @fmt: format to name the kobject with.
0374  *
0375  * The kobject name is set and added to the kobject hierarchy in this
0376  * function.
0377  *
0378  * If @parent is set, then the parent of the @kobj will be set to it.
0379  * If @parent is NULL, then the parent of the @kobj will be set to the
0380  * kobject associated with the kset assigned to this kobject.  If no kset
0381  * is assigned to the kobject, then the kobject will be located in the
0382  * root of the sysfs tree.
0383  *
0384  * If this function returns an error, kobject_put() must be called to
0385  * properly clean up the memory associated with the object.
0386  * Under no instance should the kobject that is passed to this function
0387  * be directly freed with a call to kfree(), that can leak memory.
0388  *
0389  * Note, no "add" uevent will be created with this call, the caller should set
0390  * up all of the necessary sysfs files for the object and then call
0391  * kobject_uevent() with the UEVENT_ADD parameter to ensure that
0392  * userspace is properly notified of this kobject's creation.
0393  */
0394 int kobject_add(struct kobject *kobj, struct kobject *parent,
0395         const char *fmt, ...)
0396 {
0397     va_list args;
0398     int retval;
0399 
0400     if (!kobj)
0401         return -EINVAL;
0402 
0403     if (!kobj->state_initialized) {
0404         printk(KERN_ERR "kobject '%s' (%p): tried to add an "
0405                "uninitialized object, something is seriously wrong.\n",
0406                kobject_name(kobj), kobj);
0407         dump_stack();
0408         return -EINVAL;
0409     }
0410     va_start(args, fmt);
0411     retval = kobject_add_varg(kobj, parent, fmt, args);
0412     va_end(args);
0413 
0414     return retval;
0415 }
0416 EXPORT_SYMBOL(kobject_add);
0417 
0418 /**
0419  * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
0420  * @kobj: pointer to the kobject to initialize
0421  * @ktype: pointer to the ktype for this kobject.
0422  * @parent: pointer to the parent of this kobject.
0423  * @fmt: the name of the kobject.
0424  *
0425  * This function combines the call to kobject_init() and
0426  * kobject_add().  The same type of error handling after a call to
0427  * kobject_add() and kobject lifetime rules are the same here.
0428  */
0429 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
0430              struct kobject *parent, const char *fmt, ...)
0431 {
0432     va_list args;
0433     int retval;
0434 
0435     kobject_init(kobj, ktype);
0436 
0437     va_start(args, fmt);
0438     retval = kobject_add_varg(kobj, parent, fmt, args);
0439     va_end(args);
0440 
0441     return retval;
0442 }
0443 EXPORT_SYMBOL_GPL(kobject_init_and_add);
0444 
0445 /**
0446  * kobject_rename - change the name of an object
0447  * @kobj: object in question.
0448  * @new_name: object's new name
0449  *
0450  * It is the responsibility of the caller to provide mutual
0451  * exclusion between two different calls of kobject_rename
0452  * on the same kobject and to ensure that new_name is valid and
0453  * won't conflict with other kobjects.
0454  */
0455 int kobject_rename(struct kobject *kobj, const char *new_name)
0456 {
0457     int error = 0;
0458     const char *devpath = NULL;
0459     const char *dup_name = NULL, *name;
0460     char *devpath_string = NULL;
0461     char *envp[2];
0462 
0463     kobj = kobject_get(kobj);
0464     if (!kobj)
0465         return -EINVAL;
0466     if (!kobj->parent)
0467         return -EINVAL;
0468 
0469     devpath = kobject_get_path(kobj, GFP_KERNEL);
0470     if (!devpath) {
0471         error = -ENOMEM;
0472         goto out;
0473     }
0474     devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
0475     if (!devpath_string) {
0476         error = -ENOMEM;
0477         goto out;
0478     }
0479     sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
0480     envp[0] = devpath_string;
0481     envp[1] = NULL;
0482 
0483     name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
0484     if (!name) {
0485         error = -ENOMEM;
0486         goto out;
0487     }
0488 
0489     error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
0490     if (error)
0491         goto out;
0492 
0493     /* Install the new kobject name */
0494     dup_name = kobj->name;
0495     kobj->name = name;
0496 
0497     /* This function is mostly/only used for network interface.
0498      * Some hotplug package track interfaces by their name and
0499      * therefore want to know when the name is changed by the user. */
0500     kobject_uevent_env(kobj, KOBJ_MOVE, envp);
0501 
0502 out:
0503     kfree_const(dup_name);
0504     kfree(devpath_string);
0505     kfree(devpath);
0506     kobject_put(kobj);
0507 
0508     return error;
0509 }
0510 EXPORT_SYMBOL_GPL(kobject_rename);
0511 
0512 /**
0513  * kobject_move - move object to another parent
0514  * @kobj: object in question.
0515  * @new_parent: object's new parent (can be NULL)
0516  */
0517 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
0518 {
0519     int error;
0520     struct kobject *old_parent;
0521     const char *devpath = NULL;
0522     char *devpath_string = NULL;
0523     char *envp[2];
0524 
0525     kobj = kobject_get(kobj);
0526     if (!kobj)
0527         return -EINVAL;
0528     new_parent = kobject_get(new_parent);
0529     if (!new_parent) {
0530         if (kobj->kset)
0531             new_parent = kobject_get(&kobj->kset->kobj);
0532     }
0533 
0534     /* old object path */
0535     devpath = kobject_get_path(kobj, GFP_KERNEL);
0536     if (!devpath) {
0537         error = -ENOMEM;
0538         goto out;
0539     }
0540     devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
0541     if (!devpath_string) {
0542         error = -ENOMEM;
0543         goto out;
0544     }
0545     sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
0546     envp[0] = devpath_string;
0547     envp[1] = NULL;
0548     error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
0549     if (error)
0550         goto out;
0551     old_parent = kobj->parent;
0552     kobj->parent = new_parent;
0553     new_parent = NULL;
0554     kobject_put(old_parent);
0555     kobject_uevent_env(kobj, KOBJ_MOVE, envp);
0556 out:
0557     kobject_put(new_parent);
0558     kobject_put(kobj);
0559     kfree(devpath_string);
0560     kfree(devpath);
0561     return error;
0562 }
0563 EXPORT_SYMBOL_GPL(kobject_move);
0564 
0565 /**
0566  * kobject_del - unlink kobject from hierarchy.
0567  * @kobj: object.
0568  */
0569 void kobject_del(struct kobject *kobj)
0570 {
0571     struct kernfs_node *sd;
0572 
0573     if (!kobj)
0574         return;
0575 
0576     sd = kobj->sd;
0577     sysfs_remove_dir(kobj);
0578     sysfs_put(sd);
0579 
0580     kobj->state_in_sysfs = 0;
0581     kobj_kset_leave(kobj);
0582     kobject_put(kobj->parent);
0583     kobj->parent = NULL;
0584 }
0585 EXPORT_SYMBOL(kobject_del);
0586 
0587 /**
0588  * kobject_get - increment refcount for object.
0589  * @kobj: object.
0590  */
0591 struct kobject *kobject_get(struct kobject *kobj)
0592 {
0593     if (kobj) {
0594         if (!kobj->state_initialized)
0595             WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
0596                    "initialized, yet kobject_get() is being "
0597                    "called.\n", kobject_name(kobj), kobj);
0598         kref_get(&kobj->kref);
0599     }
0600     return kobj;
0601 }
0602 EXPORT_SYMBOL(kobject_get);
0603 
0604 static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
0605 {
0606     if (!kref_get_unless_zero(&kobj->kref))
0607         kobj = NULL;
0608     return kobj;
0609 }
0610 
0611 /*
0612  * kobject_cleanup - free kobject resources.
0613  * @kobj: object to cleanup
0614  */
0615 static void kobject_cleanup(struct kobject *kobj)
0616 {
0617     struct kobj_type *t = get_ktype(kobj);
0618     const char *name = kobj->name;
0619 
0620     pr_debug("kobject: '%s' (%p): %s, parent %p\n",
0621          kobject_name(kobj), kobj, __func__, kobj->parent);
0622 
0623     if (t && !t->release)
0624         pr_debug("kobject: '%s' (%p): does not have a release() "
0625              "function, it is broken and must be fixed.\n",
0626              kobject_name(kobj), kobj);
0627 
0628     /* send "remove" if the caller did not do it but sent "add" */
0629     if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
0630         pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
0631              kobject_name(kobj), kobj);
0632         kobject_uevent(kobj, KOBJ_REMOVE);
0633     }
0634 
0635     /* remove from sysfs if the caller did not do it */
0636     if (kobj->state_in_sysfs) {
0637         pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
0638              kobject_name(kobj), kobj);
0639         kobject_del(kobj);
0640     }
0641 
0642     if (t && t->release) {
0643         pr_debug("kobject: '%s' (%p): calling ktype release\n",
0644              kobject_name(kobj), kobj);
0645         t->release(kobj);
0646     }
0647 
0648     /* free name if we allocated it */
0649     if (name) {
0650         pr_debug("kobject: '%s': free name\n", name);
0651         kfree_const(name);
0652     }
0653 }
0654 
0655 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
0656 static void kobject_delayed_cleanup(struct work_struct *work)
0657 {
0658     kobject_cleanup(container_of(to_delayed_work(work),
0659                      struct kobject, release));
0660 }
0661 #endif
0662 
0663 static void kobject_release(struct kref *kref)
0664 {
0665     struct kobject *kobj = container_of(kref, struct kobject, kref);
0666 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
0667     unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
0668     pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
0669          kobject_name(kobj), kobj, __func__, kobj->parent, delay);
0670     INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
0671 
0672     schedule_delayed_work(&kobj->release, delay);
0673 #else
0674     kobject_cleanup(kobj);
0675 #endif
0676 }
0677 
0678 /**
0679  * kobject_put - decrement refcount for object.
0680  * @kobj: object.
0681  *
0682  * Decrement the refcount, and if 0, call kobject_cleanup().
0683  */
0684 void kobject_put(struct kobject *kobj)
0685 {
0686     if (kobj) {
0687         if (!kobj->state_initialized)
0688             WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
0689                    "initialized, yet kobject_put() is being "
0690                    "called.\n", kobject_name(kobj), kobj);
0691         kref_put(&kobj->kref, kobject_release);
0692     }
0693 }
0694 EXPORT_SYMBOL(kobject_put);
0695 
0696 static void dynamic_kobj_release(struct kobject *kobj)
0697 {
0698     pr_debug("kobject: (%p): %s\n", kobj, __func__);
0699     kfree(kobj);
0700 }
0701 
0702 static struct kobj_type dynamic_kobj_ktype = {
0703     .release    = dynamic_kobj_release,
0704     .sysfs_ops  = &kobj_sysfs_ops,
0705 };
0706 
0707 /**
0708  * kobject_create - create a struct kobject dynamically
0709  *
0710  * This function creates a kobject structure dynamically and sets it up
0711  * to be a "dynamic" kobject with a default release function set up.
0712  *
0713  * If the kobject was not able to be created, NULL will be returned.
0714  * The kobject structure returned from here must be cleaned up with a
0715  * call to kobject_put() and not kfree(), as kobject_init() has
0716  * already been called on this structure.
0717  */
0718 struct kobject *kobject_create(void)
0719 {
0720     struct kobject *kobj;
0721 
0722     kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
0723     if (!kobj)
0724         return NULL;
0725 
0726     kobject_init(kobj, &dynamic_kobj_ktype);
0727     return kobj;
0728 }
0729 
0730 /**
0731  * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
0732  *
0733  * @name: the name for the kobject
0734  * @parent: the parent kobject of this kobject, if any.
0735  *
0736  * This function creates a kobject structure dynamically and registers it
0737  * with sysfs.  When you are finished with this structure, call
0738  * kobject_put() and the structure will be dynamically freed when
0739  * it is no longer being used.
0740  *
0741  * If the kobject was not able to be created, NULL will be returned.
0742  */
0743 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
0744 {
0745     struct kobject *kobj;
0746     int retval;
0747 
0748     kobj = kobject_create();
0749     if (!kobj)
0750         return NULL;
0751 
0752     retval = kobject_add(kobj, parent, "%s", name);
0753     if (retval) {
0754         printk(KERN_WARNING "%s: kobject_add error: %d\n",
0755                __func__, retval);
0756         kobject_put(kobj);
0757         kobj = NULL;
0758     }
0759     return kobj;
0760 }
0761 EXPORT_SYMBOL_GPL(kobject_create_and_add);
0762 
0763 /**
0764  * kset_init - initialize a kset for use
0765  * @k: kset
0766  */
0767 void kset_init(struct kset *k)
0768 {
0769     kobject_init_internal(&k->kobj);
0770     INIT_LIST_HEAD(&k->list);
0771     spin_lock_init(&k->list_lock);
0772 }
0773 
0774 /* default kobject attribute operations */
0775 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
0776                   char *buf)
0777 {
0778     struct kobj_attribute *kattr;
0779     ssize_t ret = -EIO;
0780 
0781     kattr = container_of(attr, struct kobj_attribute, attr);
0782     if (kattr->show)
0783         ret = kattr->show(kobj, kattr, buf);
0784     return ret;
0785 }
0786 
0787 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
0788                    const char *buf, size_t count)
0789 {
0790     struct kobj_attribute *kattr;
0791     ssize_t ret = -EIO;
0792 
0793     kattr = container_of(attr, struct kobj_attribute, attr);
0794     if (kattr->store)
0795         ret = kattr->store(kobj, kattr, buf, count);
0796     return ret;
0797 }
0798 
0799 const struct sysfs_ops kobj_sysfs_ops = {
0800     .show   = kobj_attr_show,
0801     .store  = kobj_attr_store,
0802 };
0803 EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
0804 
0805 /**
0806  * kset_register - initialize and add a kset.
0807  * @k: kset.
0808  */
0809 int kset_register(struct kset *k)
0810 {
0811     int err;
0812 
0813     if (!k)
0814         return -EINVAL;
0815 
0816     kset_init(k);
0817     err = kobject_add_internal(&k->kobj);
0818     if (err)
0819         return err;
0820     kobject_uevent(&k->kobj, KOBJ_ADD);
0821     return 0;
0822 }
0823 EXPORT_SYMBOL(kset_register);
0824 
0825 /**
0826  * kset_unregister - remove a kset.
0827  * @k: kset.
0828  */
0829 void kset_unregister(struct kset *k)
0830 {
0831     if (!k)
0832         return;
0833     kobject_del(&k->kobj);
0834     kobject_put(&k->kobj);
0835 }
0836 EXPORT_SYMBOL(kset_unregister);
0837 
0838 /**
0839  * kset_find_obj - search for object in kset.
0840  * @kset: kset we're looking in.
0841  * @name: object's name.
0842  *
0843  * Lock kset via @kset->subsys, and iterate over @kset->list,
0844  * looking for a matching kobject. If matching object is found
0845  * take a reference and return the object.
0846  */
0847 struct kobject *kset_find_obj(struct kset *kset, const char *name)
0848 {
0849     struct kobject *k;
0850     struct kobject *ret = NULL;
0851 
0852     spin_lock(&kset->list_lock);
0853 
0854     list_for_each_entry(k, &kset->list, entry) {
0855         if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
0856             ret = kobject_get_unless_zero(k);
0857             break;
0858         }
0859     }
0860 
0861     spin_unlock(&kset->list_lock);
0862     return ret;
0863 }
0864 EXPORT_SYMBOL_GPL(kset_find_obj);
0865 
0866 static void kset_release(struct kobject *kobj)
0867 {
0868     struct kset *kset = container_of(kobj, struct kset, kobj);
0869     pr_debug("kobject: '%s' (%p): %s\n",
0870          kobject_name(kobj), kobj, __func__);
0871     kfree(kset);
0872 }
0873 
0874 static struct kobj_type kset_ktype = {
0875     .sysfs_ops  = &kobj_sysfs_ops,
0876     .release = kset_release,
0877 };
0878 
0879 /**
0880  * kset_create - create a struct kset dynamically
0881  *
0882  * @name: the name for the kset
0883  * @uevent_ops: a struct kset_uevent_ops for the kset
0884  * @parent_kobj: the parent kobject of this kset, if any.
0885  *
0886  * This function creates a kset structure dynamically.  This structure can
0887  * then be registered with the system and show up in sysfs with a call to
0888  * kset_register().  When you are finished with this structure, if
0889  * kset_register() has been called, call kset_unregister() and the
0890  * structure will be dynamically freed when it is no longer being used.
0891  *
0892  * If the kset was not able to be created, NULL will be returned.
0893  */
0894 static struct kset *kset_create(const char *name,
0895                 const struct kset_uevent_ops *uevent_ops,
0896                 struct kobject *parent_kobj)
0897 {
0898     struct kset *kset;
0899     int retval;
0900 
0901     kset = kzalloc(sizeof(*kset), GFP_KERNEL);
0902     if (!kset)
0903         return NULL;
0904     retval = kobject_set_name(&kset->kobj, "%s", name);
0905     if (retval) {
0906         kfree(kset);
0907         return NULL;
0908     }
0909     kset->uevent_ops = uevent_ops;
0910     kset->kobj.parent = parent_kobj;
0911 
0912     /*
0913      * The kobject of this kset will have a type of kset_ktype and belong to
0914      * no kset itself.  That way we can properly free it when it is
0915      * finished being used.
0916      */
0917     kset->kobj.ktype = &kset_ktype;
0918     kset->kobj.kset = NULL;
0919 
0920     return kset;
0921 }
0922 
0923 /**
0924  * kset_create_and_add - create a struct kset dynamically and add it to sysfs
0925  *
0926  * @name: the name for the kset
0927  * @uevent_ops: a struct kset_uevent_ops for the kset
0928  * @parent_kobj: the parent kobject of this kset, if any.
0929  *
0930  * This function creates a kset structure dynamically and registers it
0931  * with sysfs.  When you are finished with this structure, call
0932  * kset_unregister() and the structure will be dynamically freed when it
0933  * is no longer being used.
0934  *
0935  * If the kset was not able to be created, NULL will be returned.
0936  */
0937 struct kset *kset_create_and_add(const char *name,
0938                  const struct kset_uevent_ops *uevent_ops,
0939                  struct kobject *parent_kobj)
0940 {
0941     struct kset *kset;
0942     int error;
0943 
0944     kset = kset_create(name, uevent_ops, parent_kobj);
0945     if (!kset)
0946         return NULL;
0947     error = kset_register(kset);
0948     if (error) {
0949         kfree(kset);
0950         return NULL;
0951     }
0952     return kset;
0953 }
0954 EXPORT_SYMBOL_GPL(kset_create_and_add);
0955 
0956 
0957 static DEFINE_SPINLOCK(kobj_ns_type_lock);
0958 static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
0959 
0960 int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
0961 {
0962     enum kobj_ns_type type = ops->type;
0963     int error;
0964 
0965     spin_lock(&kobj_ns_type_lock);
0966 
0967     error = -EINVAL;
0968     if (type >= KOBJ_NS_TYPES)
0969         goto out;
0970 
0971     error = -EINVAL;
0972     if (type <= KOBJ_NS_TYPE_NONE)
0973         goto out;
0974 
0975     error = -EBUSY;
0976     if (kobj_ns_ops_tbl[type])
0977         goto out;
0978 
0979     error = 0;
0980     kobj_ns_ops_tbl[type] = ops;
0981 
0982 out:
0983     spin_unlock(&kobj_ns_type_lock);
0984     return error;
0985 }
0986 
0987 int kobj_ns_type_registered(enum kobj_ns_type type)
0988 {
0989     int registered = 0;
0990 
0991     spin_lock(&kobj_ns_type_lock);
0992     if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
0993         registered = kobj_ns_ops_tbl[type] != NULL;
0994     spin_unlock(&kobj_ns_type_lock);
0995 
0996     return registered;
0997 }
0998 
0999 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
1000 {
1001     const struct kobj_ns_type_operations *ops = NULL;
1002 
1003     if (parent && parent->ktype && parent->ktype->child_ns_type)
1004         ops = parent->ktype->child_ns_type(parent);
1005 
1006     return ops;
1007 }
1008 
1009 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
1010 {
1011     return kobj_child_ns_ops(kobj->parent);
1012 }
1013 
1014 bool kobj_ns_current_may_mount(enum kobj_ns_type type)
1015 {
1016     bool may_mount = true;
1017 
1018     spin_lock(&kobj_ns_type_lock);
1019     if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1020         kobj_ns_ops_tbl[type])
1021         may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
1022     spin_unlock(&kobj_ns_type_lock);
1023 
1024     return may_mount;
1025 }
1026 
1027 void *kobj_ns_grab_current(enum kobj_ns_type type)
1028 {
1029     void *ns = NULL;
1030 
1031     spin_lock(&kobj_ns_type_lock);
1032     if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1033         kobj_ns_ops_tbl[type])
1034         ns = kobj_ns_ops_tbl[type]->grab_current_ns();
1035     spin_unlock(&kobj_ns_type_lock);
1036 
1037     return ns;
1038 }
1039 
1040 const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
1041 {
1042     const void *ns = NULL;
1043 
1044     spin_lock(&kobj_ns_type_lock);
1045     if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1046         kobj_ns_ops_tbl[type])
1047         ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
1048     spin_unlock(&kobj_ns_type_lock);
1049 
1050     return ns;
1051 }
1052 
1053 const void *kobj_ns_initial(enum kobj_ns_type type)
1054 {
1055     const void *ns = NULL;
1056 
1057     spin_lock(&kobj_ns_type_lock);
1058     if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1059         kobj_ns_ops_tbl[type])
1060         ns = kobj_ns_ops_tbl[type]->initial_ns();
1061     spin_unlock(&kobj_ns_type_lock);
1062 
1063     return ns;
1064 }
1065 
1066 void kobj_ns_drop(enum kobj_ns_type type, void *ns)
1067 {
1068     spin_lock(&kobj_ns_type_lock);
1069     if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
1070         kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
1071         kobj_ns_ops_tbl[type]->drop_ns(ns);
1072     spin_unlock(&kobj_ns_type_lock);
1073 }