^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * kobject.c - library routines for handling generic kernel objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2006-2007 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Please see the file Documentation/core-api/kobject.rst for critical information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * about using the kobject interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * kobject_namespace() - Return @kobj's namespace tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @kobj: kobject in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Returns namespace tag of @kobj if its parent has namespace ops enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * and thus @kobj should have a namespace tag associated with it. Returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * %NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const void *kobject_namespace(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return kobj->ktype->namespace(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * kobject_get_ownership() - Get sysfs ownership data for @kobj.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @kobj: kobject in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @uid: kernel user ID for sysfs objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @gid: kernel group ID for sysfs objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Returns initial uid/gid pair that should be used when creating sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * representation of given kobject. Normally used to adjust ownership of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * objects in a container.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void kobject_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *uid = GLOBAL_ROOT_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *gid = GLOBAL_ROOT_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (kobj->ktype->get_ownership)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) kobj->ktype->get_ownership(kobj, uid, gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * populate_dir - populate directory with attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @kobj: object we're working on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Most subsystems have a set of default attributes that are associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * with an object that registers with them. This is a helper called during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * object registration that loops through the default attributes of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * subsystem and creates attributes files for them in sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static int populate_dir(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct kobj_type *t = get_ktype(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct attribute *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (t && t->default_attrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) error = sysfs_create_file(kobj, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static int create_dir(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) const struct kobj_type *ktype = get_ktype(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const struct kobj_ns_type_operations *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) error = populate_dir(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) sysfs_remove_dir(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ktype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) error = sysfs_create_groups(kobj, ktype->default_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) sysfs_remove_dir(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @kobj->sd may be deleted by an ancestor going away. Hold an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * extra reference so that it stays until @kobj is gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) sysfs_get(kobj->sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * If @kobj has ns_ops, its children need to be filtered based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * their namespace tags. Enable namespace support on @kobj->sd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ops = kobj_child_ns_ops(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) BUG_ON(ops->type >= KOBJ_NS_TYPES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) BUG_ON(!kobj_ns_type_registered(ops->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) sysfs_enable_ns(kobj->sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static int get_kobj_path_length(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int length = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct kobject *parent = kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* walk up the ancestors until we hit the one pointing to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * Add 1 to strlen for leading '/' of each level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (kobject_name(parent) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) length += strlen(kobject_name(parent)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) parent = parent->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } while (parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void fill_kobj_path(struct kobject *kobj, char *path, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct kobject *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) --length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) for (parent = kobj; parent; parent = parent->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int cur = strlen(kobject_name(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* back up enough to print this name with '/' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) length -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) memcpy(path + length, kobject_name(parent), cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *(path + --length) = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) kobj, __func__, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * kobject_get_path() - Allocate memory and fill in the path for @kobj.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * @kobj: kobject in question, with which to build the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * @gfp_mask: the allocation type used to allocate the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Return: The newly allocated memory, caller must free with kfree().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) len = get_kobj_path_length(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) path = kzalloc(len, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) fill_kobj_path(kobj, path, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) EXPORT_SYMBOL_GPL(kobject_get_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* add the kobject to its kset's list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void kobj_kset_join(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!kobj->kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) kset_get(kobj->kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) spin_lock(&kobj->kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) list_add_tail(&kobj->entry, &kobj->kset->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) spin_unlock(&kobj->kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* remove the kobject from its kset's list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void kobj_kset_leave(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!kobj->kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) spin_lock(&kobj->kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) list_del_init(&kobj->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) spin_unlock(&kobj->kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) kset_put(kobj->kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static void kobject_init_internal(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) kref_init(&kobj->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) INIT_LIST_HEAD(&kobj->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) kobj->state_in_sysfs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) kobj->state_add_uevent_sent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) kobj->state_remove_uevent_sent = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kobj->state_initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int kobject_add_internal(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct kobject *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!kobj->name || !kobj->name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) WARN(1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) "kobject: (%p): attempted to be registered with empty name!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) parent = kobject_get(kobj->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* join kset if set, use it as parent if we do not already have one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (kobj->kset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) parent = kobject_get(&kobj->kset->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) kobj_kset_join(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) kobj->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) kobject_name(kobj), kobj, __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) parent ? kobject_name(parent) : "<NULL>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) error = create_dir(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) kobj_kset_leave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) kobject_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) kobj->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* be noisy on error issues */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (error == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pr_err("%s failed for %s with -EEXIST, don't try to register things with the same name in the same directory.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) __func__, kobject_name(kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) pr_err("%s failed for %s (error: %d parent: %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) __func__, kobject_name(kobj), error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) parent ? kobject_name(parent) : "'none'");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) kobj->state_in_sysfs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * kobject_set_name_vargs() - Set the name of a kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @kobj: struct kobject to set the name of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @fmt: format string used to build the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @vargs: vargs to format the string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) va_list vargs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) const char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (kobj->name && !fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * ewww... some of these buggers have '/' in the name ... If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * that's the case, we need to make sure we have an actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * allocated copy to modify, since kvasprintf_const may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * returned something from .rodata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (strchr(s, '/')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) char *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) t = kstrdup(s, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kfree_const(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) strreplace(t, '/', '!');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) s = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) kfree_const(kobj->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) kobj->name = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * kobject_set_name() - Set the name of a kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * @kobj: struct kobject to set the name of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * @fmt: format string used to build the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * This sets the name of the kobject. If you have already added the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * kobject to the system, you must call kobject_rename() in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * change the name of the kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) va_list vargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) va_start(vargs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) retval = kobject_set_name_vargs(kobj, fmt, vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) va_end(vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) EXPORT_SYMBOL(kobject_set_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * kobject_init() - Initialize a kobject structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @kobj: pointer to the kobject to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * @ktype: pointer to the ktype for this kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * This function will properly initialize a kobject such that it can then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * be passed to the kobject_add() call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * After this function is called, the kobject MUST be cleaned up by a call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * to kobject_put(), not by a call to kfree directly to ensure that all of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * the memory is cleaned up properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) char *err_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) err_str = "invalid kobject pointer!";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!ktype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) err_str = "must have a ktype to be initialized properly!\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (kobj->state_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* do not error out as sometimes we can recover */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) kobject_init_internal(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) kobj->ktype = ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) pr_err("kobject (%p): %s\n", kobj, err_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) EXPORT_SYMBOL(kobject_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct kobject *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) const char *fmt, va_list vargs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) retval = kobject_set_name_vargs(kobj, fmt, vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) pr_err("kobject: can not set name properly!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) kobj->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return kobject_add_internal(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * kobject_add() - The main kobject add function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * @kobj: the kobject to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @parent: pointer to the parent of the kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * @fmt: format to name the kobject with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * The kobject name is set and added to the kobject hierarchy in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * If @parent is set, then the parent of the @kobj will be set to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * If @parent is NULL, then the parent of the @kobj will be set to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * kobject associated with the kset assigned to this kobject. If no kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * is assigned to the kobject, then the kobject will be located in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * root of the sysfs tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * Note, no "add" uevent will be created with this call, the caller should set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * up all of the necessary sysfs files for the object and then call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * kobject_uevent() with the UEVENT_ADD parameter to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * userspace is properly notified of this kobject's creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * Return: If this function returns an error, kobject_put() must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * called to properly clean up the memory associated with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * object. Under no instance should the kobject that is passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * to this function be directly freed with a call to kfree(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * that can leak memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * If this function returns success, kobject_put() must also be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * in order to properly clean up the memory associated with the object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * In short, once this function is called, kobject_put() MUST be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * when the use of the object is finished in order to properly free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * everything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) int kobject_add(struct kobject *kobj, struct kobject *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (!kobj->state_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) pr_err("kobject '%s' (%p): tried to add an uninitialized object, something is seriously wrong.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) retval = kobject_add_varg(kobj, parent, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) EXPORT_SYMBOL(kobject_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * kobject_init_and_add() - Initialize a kobject structure and add it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * the kobject hierarchy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * @kobj: pointer to the kobject to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * @ktype: pointer to the ktype for this kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * @parent: pointer to the parent of this kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * @fmt: the name of the kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * This function combines the call to kobject_init() and kobject_add().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * If this function returns an error, kobject_put() must be called to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * properly clean up the memory associated with the object. This is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * same type of error handling after a call to kobject_add() and kobject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * lifetime rules are the same here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct kobject *parent, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) kobject_init(kobj, ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) retval = kobject_add_varg(kobj, parent, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) EXPORT_SYMBOL_GPL(kobject_init_and_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * kobject_rename() - Change the name of an object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * @kobj: object in question.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * @new_name: object's new name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * It is the responsibility of the caller to provide mutual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * exclusion between two different calls of kobject_rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * on the same kobject and to ensure that new_name is valid and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * won't conflict with other kobjects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) int kobject_rename(struct kobject *kobj, const char *new_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) const char *devpath = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) const char *dup_name = NULL, *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) char *devpath_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) char *envp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) kobj = kobject_get(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (!kobj->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) devpath = kobject_get_path(kobj, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (!devpath) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (!devpath_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) envp[0] = devpath_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) envp[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* Install the new kobject name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) dup_name = kobj->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) kobj->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /* This function is mostly/only used for network interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * Some hotplug package track interfaces by their name and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * therefore want to know when the name is changed by the user. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) kobject_uevent_env(kobj, KOBJ_MOVE, envp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) kfree_const(dup_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) kfree(devpath_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) kfree(devpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) EXPORT_SYMBOL_GPL(kobject_rename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * kobject_move() - Move object to another parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * @kobj: object in question.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * @new_parent: object's new parent (can be NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) int kobject_move(struct kobject *kobj, struct kobject *new_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct kobject *old_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) const char *devpath = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) char *devpath_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) char *envp[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) kobj = kobject_get(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) new_parent = kobject_get(new_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (!new_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (kobj->kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) new_parent = kobject_get(&kobj->kset->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /* old object path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) devpath = kobject_get_path(kobj, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (!devpath) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (!devpath_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) envp[0] = devpath_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) envp[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) old_parent = kobj->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) kobj->parent = new_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) new_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) kobject_put(old_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) kobject_uevent_env(kobj, KOBJ_MOVE, envp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) kobject_put(new_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) kfree(devpath_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) kfree(devpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) EXPORT_SYMBOL_GPL(kobject_move);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static void __kobject_del(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) struct kernfs_node *sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) const struct kobj_type *ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) sd = kobj->sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ktype = get_ktype(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (ktype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) sysfs_remove_groups(kobj, ktype->default_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /* send "remove" if the caller did not do it but sent "add" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) kobject_uevent(kobj, KOBJ_REMOVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) sysfs_remove_dir(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) sysfs_put(sd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) kobj->state_in_sysfs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) kobj_kset_leave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) kobj->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * kobject_del() - Unlink kobject from hierarchy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * @kobj: object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * This is the function that should be called to delete an object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * successfully added via kobject_add().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) void kobject_del(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct kobject *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) parent = kobj->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) __kobject_del(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) kobject_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) EXPORT_SYMBOL(kobject_del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * kobject_get() - Increment refcount for object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * @kobj: object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) struct kobject *kobject_get(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (!kobj->state_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) WARN(1, KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) "kobject: '%s' (%p): is not initialized, yet kobject_get() is being called.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) kref_get(&kobj->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) return kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) EXPORT_SYMBOL(kobject_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) if (!kref_get_unless_zero(&kobj->kref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) kobj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) return kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) EXPORT_SYMBOL(kobject_get_unless_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * kobject_cleanup - free kobject resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * @kobj: object to cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) static void kobject_cleanup(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct kobject *parent = kobj->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) struct kobj_type *t = get_ktype(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) const char *name = kobj->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) pr_debug("kobject: '%s' (%p): %s, parent %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) kobject_name(kobj), kobj, __func__, kobj->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (t && !t->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) pr_debug("kobject: '%s' (%p): does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) /* remove from sysfs if the caller did not do it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (kobj->state_in_sysfs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) __kobject_del(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) /* avoid dropping the parent reference unnecessarily */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (t && t->release) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) pr_debug("kobject: '%s' (%p): calling ktype release\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) t->release(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* free name if we allocated it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) if (name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) pr_debug("kobject: '%s': free name\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) kfree_const(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) kobject_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) static void kobject_delayed_cleanup(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) kobject_cleanup(container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct kobject, release));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) static void kobject_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct kobject *kobj = container_of(kref, struct kobject, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) kobject_name(kobj), kobj, __func__, kobj->parent, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) schedule_delayed_work(&kobj->release, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) kobject_cleanup(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * kobject_put() - Decrement refcount for object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * @kobj: object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * Decrement the refcount, and if 0, call kobject_cleanup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) void kobject_put(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (!kobj->state_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) WARN(1, KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) "kobject: '%s' (%p): is not initialized, yet kobject_put() is being called.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) kobject_name(kobj), kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) kref_put(&kobj->kref, kobject_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) EXPORT_SYMBOL(kobject_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) static void dynamic_kobj_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) pr_debug("kobject: (%p): %s\n", kobj, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) kfree(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static struct kobj_type dynamic_kobj_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) .release = dynamic_kobj_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) .sysfs_ops = &kobj_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * kobject_create() - Create a struct kobject dynamically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * This function creates a kobject structure dynamically and sets it up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * to be a "dynamic" kobject with a default release function set up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * If the kobject was not able to be created, NULL will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * The kobject structure returned from here must be cleaned up with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * call to kobject_put() and not kfree(), as kobject_init() has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * already been called on this structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) struct kobject *kobject_create(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) kobject_init(kobj, &dynamic_kobj_ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) return kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * kobject_create_and_add() - Create a struct kobject dynamically and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) * register it with sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * @name: the name for the kobject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * @parent: the parent kobject of this kobject, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * This function creates a kobject structure dynamically and registers it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * with sysfs. When you are finished with this structure, call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * kobject_put() and the structure will be dynamically freed when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * it is no longer being used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * If the kobject was not able to be created, NULL will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) kobj = kobject_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) retval = kobject_add(kobj, parent, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) pr_warn("%s: kobject_add error: %d\n", __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) kobj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) return kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) EXPORT_SYMBOL_GPL(kobject_create_and_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * kset_init() - Initialize a kset for use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * @k: kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) void kset_init(struct kset *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) kobject_init_internal(&k->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) INIT_LIST_HEAD(&k->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) spin_lock_init(&k->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) /* default kobject attribute operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) struct kobj_attribute *kattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) kattr = container_of(attr, struct kobj_attribute, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (kattr->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) ret = kattr->show(kobj, kattr, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct kobj_attribute *kattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) ssize_t ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) kattr = container_of(attr, struct kobj_attribute, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (kattr->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) ret = kattr->store(kobj, kattr, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) const struct sysfs_ops kobj_sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) .show = kobj_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) .store = kobj_attr_store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * kset_register() - Initialize and add a kset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * @k: kset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) int kset_register(struct kset *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) if (!k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) kset_init(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) err = kobject_add_internal(&k->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) kobject_uevent(&k->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) EXPORT_SYMBOL(kset_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * kset_unregister() - Remove a kset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * @k: kset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) void kset_unregister(struct kset *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (!k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) kobject_del(&k->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) kobject_put(&k->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) EXPORT_SYMBOL(kset_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) * kset_find_obj() - Search for object in kset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) * @kset: kset we're looking in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * @name: object's name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * Lock kset via @kset->subsys, and iterate over @kset->list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) * looking for a matching kobject. If matching object is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) * take a reference and return the object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct kobject *kset_find_obj(struct kset *kset, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) struct kobject *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) struct kobject *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) spin_lock(&kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) list_for_each_entry(k, &kset->list, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) ret = kobject_get_unless_zero(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) spin_unlock(&kset->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) EXPORT_SYMBOL_GPL(kset_find_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) static void kset_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) struct kset *kset = container_of(kobj, struct kset, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) pr_debug("kobject: '%s' (%p): %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) kobject_name(kobj), kobj, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) kfree(kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) static void kset_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) if (kobj->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) kobject_get_ownership(kobj->parent, uid, gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) static struct kobj_type kset_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) .sysfs_ops = &kobj_sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) .release = kset_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) .get_ownership = kset_get_ownership,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * kset_create() - Create a struct kset dynamically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * @name: the name for the kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * @uevent_ops: a struct kset_uevent_ops for the kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * @parent_kobj: the parent kobject of this kset, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) * This function creates a kset structure dynamically. This structure can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) * then be registered with the system and show up in sysfs with a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) * kset_register(). When you are finished with this structure, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) * kset_register() has been called, call kset_unregister() and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) * structure will be dynamically freed when it is no longer being used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) * If the kset was not able to be created, NULL will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) static struct kset *kset_create(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) const struct kset_uevent_ops *uevent_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) struct kobject *parent_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) struct kset *kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) kset = kzalloc(sizeof(*kset), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (!kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) retval = kobject_set_name(&kset->kobj, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) kfree(kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) kset->uevent_ops = uevent_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) kset->kobj.parent = parent_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) * The kobject of this kset will have a type of kset_ktype and belong to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * no kset itself. That way we can properly free it when it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * finished being used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) kset->kobj.ktype = &kset_ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) kset->kobj.kset = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) return kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * @name: the name for the kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * @uevent_ops: a struct kset_uevent_ops for the kset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * @parent_kobj: the parent kobject of this kset, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * This function creates a kset structure dynamically and registers it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) * with sysfs. When you are finished with this structure, call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) * kset_unregister() and the structure will be dynamically freed when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * is no longer being used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * If the kset was not able to be created, NULL will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) struct kset *kset_create_and_add(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) const struct kset_uevent_ops *uevent_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) struct kobject *parent_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) struct kset *kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) kset = kset_create(name, uevent_ops, parent_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (!kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) error = kset_register(kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) kfree(kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) return kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) EXPORT_SYMBOL_GPL(kset_create_and_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static DEFINE_SPINLOCK(kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) enum kobj_ns_type type = ops->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (type >= KOBJ_NS_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (type <= KOBJ_NS_TYPE_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) error = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (kobj_ns_ops_tbl[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) kobj_ns_ops_tbl[type] = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) int kobj_ns_type_registered(enum kobj_ns_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) int registered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) registered = kobj_ns_ops_tbl[type] != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) return registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) const struct kobj_ns_type_operations *ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) if (parent && parent->ktype && parent->ktype->child_ns_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) ops = parent->ktype->child_ns_type(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) return ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) return kobj_child_ns_ops(kobj->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) bool kobj_ns_current_may_mount(enum kobj_ns_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) bool may_mount = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) kobj_ns_ops_tbl[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) return may_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) void *kobj_ns_grab_current(enum kobj_ns_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) void *ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) kobj_ns_ops_tbl[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) ns = kobj_ns_ops_tbl[type]->grab_current_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) return ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) EXPORT_SYMBOL_GPL(kobj_ns_grab_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) const void *ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) kobj_ns_ops_tbl[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) return ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) const void *kobj_ns_initial(enum kobj_ns_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) const void *ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) kobj_ns_ops_tbl[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) ns = kobj_ns_ops_tbl[type]->initial_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) return ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) void kobj_ns_drop(enum kobj_ns_type type, void *ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) spin_lock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) kobj_ns_ops_tbl[type]->drop_ns(ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) spin_unlock(&kobj_ns_type_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) EXPORT_SYMBOL_GPL(kobj_ns_drop);