Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * item.c - library routines for handling generic config items
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on kobject:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	kobject is Copyright (c) 2002-2003 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Please see the file Documentation/filesystems/configfs.rst for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * critical information about using the config_item interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static inline struct config_item *to_item(struct list_head *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	return container_of(entry, struct config_item, ci_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* Evil kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static void config_item_release(struct kref *kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *	config_item_init - initialize item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *	@item:	item in question.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void config_item_init(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	kref_init(&item->ci_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	INIT_LIST_HEAD(&item->ci_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *	config_item_set_name - Set the name of an item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *	@item:	item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *	@fmt:  The vsnprintf()'s format string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *	If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *	dynamically allocated string that @item->ci_name points to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *	Otherwise, use the static @item->ci_namebuf array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) int config_item_set_name(struct config_item *item, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int limit = CONFIGFS_ITEM_NAME_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int need;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 * First, try the static array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	need = vsnprintf(item->ci_namebuf, limit, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (need < limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		name = item->ci_namebuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		name = kvasprintf(GFP_KERNEL, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/* Free the old name, if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (item->ci_name && item->ci_name != item->ci_namebuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		kfree(item->ci_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	/* Now, set the new name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	item->ci_name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) EXPORT_SYMBOL(config_item_set_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) void config_item_init_type_name(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				const struct config_item_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	config_item_set_name(item, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	item->ci_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	config_item_init(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) EXPORT_SYMBOL(config_item_init_type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) void config_group_init_type_name(struct config_group *group, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			 const struct config_item_type *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	config_item_set_name(&group->cg_item, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	group->cg_item.ci_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	config_group_init(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) EXPORT_SYMBOL(config_group_init_type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct config_item *config_item_get(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		kref_get(&item->ci_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) EXPORT_SYMBOL(config_item_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct config_item *config_item_get_unless_zero(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (item && kref_get_unless_zero(&item->ci_kref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) EXPORT_SYMBOL(config_item_get_unless_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void config_item_cleanup(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	const struct config_item_type *t = item->ci_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct config_group *s = item->ci_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct config_item *parent = item->ci_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	pr_debug("config_item %s: cleaning up\n", config_item_name(item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (item->ci_name != item->ci_namebuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		kfree(item->ci_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	item->ci_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (t && t->ct_item_ops && t->ct_item_ops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		t->ct_item_ops->release(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		config_group_put(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		config_item_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void config_item_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	config_item_cleanup(container_of(kref, struct config_item, ci_kref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  *	config_item_put - decrement refcount for item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  *	@item:	item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  *	Decrement the refcount, and if 0, call config_item_cleanup().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void config_item_put(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		kref_put(&item->ci_kref, config_item_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) EXPORT_SYMBOL(config_item_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  *	config_group_init - initialize a group for use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  *	@group:	config_group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) void config_group_init(struct config_group *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	config_item_init(&group->cg_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	INIT_LIST_HEAD(&group->cg_children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	INIT_LIST_HEAD(&group->default_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) EXPORT_SYMBOL(config_group_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *	config_group_find_item - search for item in group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *	@group:	group we're looking in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  *	@name:	item's name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  *	Iterate over @group->cg_list, looking for a matching config_item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  *	If matching item is found take a reference and return the item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  *	Caller must have locked group via @group->cg_subsys->su_mtx.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct config_item *config_group_find_item(struct config_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 					   const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct list_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct config_item *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	list_for_each(entry, &group->cg_children) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		struct config_item *item = to_item(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (config_item_name(item) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		    !strcmp(config_item_name(item), name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			ret = config_item_get(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) EXPORT_SYMBOL(config_group_find_item);