Lines Matching refs:kobj

23  * kobject_namespace() - Return @kobj's namespace tag.
24 * @kobj: kobject in question
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
30 const void *kobject_namespace(const struct kobject *kobj)
32 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
37 return kobj->ktype->namespace(kobj);
41 * kobject_get_ownership() - Get sysfs ownership data for @kobj.
42 * @kobj: kobject in question
50 void kobject_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
55 if (kobj->ktype->get_ownership)
56 kobj->ktype->get_ownership(kobj, uid, gid);
67 static int create_dir(struct kobject *kobj)
69 const struct kobj_type *ktype = get_ktype(kobj);
73 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
78 error = sysfs_create_groups(kobj, ktype->default_groups);
80 sysfs_remove_dir(kobj);
86 * @kobj->sd may be deleted by an ancestor going away. Hold an
87 * extra reference so that it stays until @kobj is gone.
89 sysfs_get(kobj->sd);
92 * If @kobj has ns_ops, its children need to be filtered based on
93 * their namespace tags. Enable namespace support on @kobj->sd.
95 ops = kobj_child_ns_ops(kobj);
100 sysfs_enable_ns(kobj->sd);
106 static int get_kobj_path_length(const struct kobject *kobj)
109 const struct kobject *parent = kobj;
124 static int fill_kobj_path(const struct kobject *kobj, char *path, int length)
129 for (parent = kobj; parent; parent = parent->parent) {
139 pr_debug("'%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
140 kobj, __func__, path);
146 * kobject_get_path() - Allocate memory and fill in the path for @kobj.
147 * @kobj: kobject in question, with which to build the path
152 char *kobject_get_path(const struct kobject *kobj, gfp_t gfp_mask)
158 len = get_kobj_path_length(kobj);
164 if (fill_kobj_path(kobj, path, len)) {
174 static void kobj_kset_join(struct kobject *kobj)
176 if (!kobj->kset)
179 kset_get(kobj->kset);
180 spin_lock(&kobj->kset->list_lock);
181 list_add_tail(&kobj->entry, &kobj->kset->list);
182 spin_unlock(&kobj->kset->list_lock);
186 static void kobj_kset_leave(struct kobject *kobj)
188 if (!kobj->kset)
191 spin_lock(&kobj->kset->list_lock);
192 list_del_init(&kobj->entry);
193 spin_unlock(&kobj->kset->list_lock);
194 kset_put(kobj->kset);
197 static void kobject_init_internal(struct kobject *kobj)
199 if (!kobj)
201 kref_init(&kobj->kref);
202 INIT_LIST_HEAD(&kobj->entry);
203 kobj->state_in_sysfs = 0;
204 kobj->state_add_uevent_sent = 0;
205 kobj->state_remove_uevent_sent = 0;
206 kobj->state_initialized = 1;
210 static int kobject_add_internal(struct kobject *kobj)
215 if (!kobj)
218 if (!kobj->name || !kobj->name[0]) {
221 kobj);
225 parent = kobject_get(kobj->parent);
228 if (kobj->kset) {
230 parent = kobject_get(&kobj->kset->kobj);
231 kobj_kset_join(kobj);
232 kobj->parent = parent;
236 kobject_name(kobj), kobj, __func__,
238 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
240 error = create_dir(kobj);
242 kobj_kset_leave(kobj);
244 kobj->parent = NULL;
249 __func__, kobject_name(kobj));
252 __func__, kobject_name(kobj), error,
255 kobj->state_in_sysfs = 1;
262 * @kobj: struct kobject to set the name of
266 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
271 if (kobj->name && !fmt)
293 kfree_const(kobj->name);
294 kobj->name = s;
301 * @kobj: struct kobject to set the name of
308 int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
314 retval = kobject_set_name_vargs(kobj, fmt, vargs);
323 * @kobj: pointer to the kobject to initialize
333 void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
337 if (!kobj) {
345 if (kobj->state_initialized) {
348 kobj);
352 kobject_init_internal(kobj);
353 kobj->ktype = ktype;
357 pr_err("kobject (%p): %s\n", kobj, err_str);
362 static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
368 retval = kobject_set_name_vargs(kobj, fmt, vargs);
373 kobj->parent = parent;
374 return kobject_add_internal(kobj);
379 * @kobj: the kobject to add
386 * If @parent is set, then the parent of the @kobj will be set to it.
387 * If @parent is NULL, then the parent of the @kobj will be set to the
410 int kobject_add(struct kobject *kobj, struct kobject *parent,
416 if (!kobj)
419 if (!kobj->state_initialized) {
421 kobject_name(kobj), kobj);
426 retval = kobject_add_varg(kobj, parent, fmt, args);
436 * @kobj: pointer to the kobject to initialize
448 int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
454 kobject_init(kobj, ktype);
457 retval = kobject_add_varg(kobj, parent, fmt, args);
466 * @kobj: object in question.
474 int kobject_rename(struct kobject *kobj, const char *new_name)
482 kobj = kobject_get(kobj);
483 if (!kobj)
485 if (!kobj->parent) {
486 kobject_put(kobj);
490 devpath = kobject_get_path(kobj, GFP_KERNEL);
510 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
515 dup_name = kobj->name;
516 kobj->name = name;
521 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
527 kobject_put(kobj);
535 * @kobj: object in question.
538 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
546 kobj = kobject_get(kobj);
547 if (!kobj)
551 if (kobj->kset)
552 new_parent = kobject_get(&kobj->kset->kobj);
556 devpath = kobject_get_path(kobj, GFP_KERNEL);
569 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
572 old_parent = kobj->parent;
573 kobj->parent = new_parent;
576 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
579 kobject_put(kobj);
586 static void __kobject_del(struct kobject *kobj)
591 sd = kobj->sd;
592 ktype = get_ktype(kobj);
595 sysfs_remove_groups(kobj, ktype->default_groups);
598 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
600 kobject_name(kobj), kobj);
601 kobject_uevent(kobj, KOBJ_REMOVE);
604 sysfs_remove_dir(kobj);
607 kobj->state_in_sysfs = 0;
608 kobj_kset_leave(kobj);
609 kobj->parent = NULL;
614 * @kobj: object.
619 void kobject_del(struct kobject *kobj)
623 if (!kobj)
626 parent = kobj->parent;
627 __kobject_del(kobj);
634 * @kobj: object.
636 struct kobject *kobject_get(struct kobject *kobj)
638 if (kobj) {
639 if (!kobj->state_initialized)
642 kobject_name(kobj), kobj);
643 kref_get(&kobj->kref);
645 return kobj;
649 struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
651 if (!kobj)
653 if (!kref_get_unless_zero(&kobj->kref))
654 kobj = NULL;
655 return kobj;
661 * @kobj: object to cleanup
663 static void kobject_cleanup(struct kobject *kobj)
665 struct kobject *parent = kobj->parent;
666 const struct kobj_type *t = get_ktype(kobj);
667 const char *name = kobj->name;
670 kobject_name(kobj), kobj, __func__, kobj->parent);
674 kobject_name(kobj), kobj);
677 if (kobj->state_in_sysfs) {
679 kobject_name(kobj), kobj);
680 __kobject_del(kobj);
688 kobject_name(kobj), kobj);
689 t->release(kobj);
711 struct kobject *kobj = container_of(kref, struct kobject, kref);
715 kobject_name(kobj), kobj, __func__, kobj->parent, delay);
716 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
718 schedule_delayed_work(&kobj->release, delay);
720 kobject_cleanup(kobj);
726 * @kobj: object.
730 void kobject_put(struct kobject *kobj)
732 if (kobj) {
733 if (!kobj->state_initialized)
736 kobject_name(kobj), kobj);
737 kref_put(&kobj->kref, kobject_release);
742 static void dynamic_kobj_release(struct kobject *kobj)
744 pr_debug("(%p): %s\n", kobj, __func__);
745 kfree(kobj);
766 struct kobject *kobj;
768 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
769 if (!kobj)
772 kobject_init(kobj, &dynamic_kobj_ktype);
773 return kobj;
791 struct kobject *kobj;
794 kobj = kobject_create();
795 if (!kobj)
798 retval = kobject_add(kobj, parent, "%s", name);
801 kobject_put(kobj);
802 kobj = NULL;
804 return kobj;
814 kobject_init_internal(&k->kobj);
820 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
828 ret = kattr->show(kobj, kattr, buf);
832 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
840 ret = kattr->store(kobj, kattr, buf, count);
854 * NOTE: On error, the kset.kobj.name allocated by() kobj_set_name()
864 if (!k->kobj.ktype) {
870 err = kobject_add_internal(&k->kobj);
872 kfree_const(k->kobj.name);
874 k->kobj.name = NULL;
877 kobject_uevent(&k->kobj, KOBJ_ADD);
890 kobject_del(&k->kobj);
891 kobject_put(&k->kobj);
923 static void kset_release(struct kobject *kobj)
925 struct kset *kset = container_of(kobj, struct kset, kobj);
927 kobject_name(kobj), kobj, __func__);
931 static void kset_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
933 if (kobj->parent)
934 kobject_get_ownership(kobj->parent, uid, gid);
968 retval = kobject_set_name(&kset->kobj, "%s", name);
974 kset->kobj.parent = parent_kobj;
981 kset->kobj.ktype = &kset_ktype;
982 kset->kobj.kset = NULL;
1069 const struct kobj_ns_type_operations *kobj_ns_ops(const struct kobject *kobj)
1071 return kobj_child_ns_ops(kobj->parent);