blob: 254170a82aa337d95cbfbdf1a2db1023db3a2907 [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masahiro Yamadafa60ce22021-05-06 18:06:44 -07002/*
Joel Becker7063fbf2005-12-15 14:29:43 -08003 * item.c - library routines for handling generic config items
4 *
Joel Becker7063fbf2005-12-15 14:29:43 -08005 * Based on kobject:
Fabian Frederickf6b1fe72014-06-04 16:05:57 -07006 * kobject is Copyright (c) 2002-2003 Patrick Mochel
Joel Becker7063fbf2005-12-15 14:29:43 -08007 *
8 * configfs Copyright (C) 2005 Oracle. All rights reserved.
9 *
Mauro Carvalho Chehab98264992020-04-27 23:17:21 +020010 * Please see the file Documentation/filesystems/configfs.rst for
Joel Becker7063fbf2005-12-15 14:29:43 -080011 * critical information about using the config_item interface.
12 */
13
14#include <linux/string.h>
15#include <linux/module.h>
16#include <linux/stat.h>
17#include <linux/slab.h>
18
19#include <linux/configfs.h>
20
21
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070022static inline struct config_item *to_item(struct list_head *entry)
Joel Becker7063fbf2005-12-15 14:29:43 -080023{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070024 return container_of(entry, struct config_item, ci_entry);
Joel Becker7063fbf2005-12-15 14:29:43 -080025}
26
27/* Evil kernel */
28static void config_item_release(struct kref *kref);
29
30/**
31 * config_item_init - initialize item.
32 * @item: item in question.
33 */
Fabian Frederick5286d202015-06-24 16:54:51 -070034static void config_item_init(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -080035{
36 kref_init(&item->ci_kref);
37 INIT_LIST_HEAD(&item->ci_entry);
38}
39
40/**
41 * config_item_set_name - Set the name of an item
42 * @item: item.
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070043 * @fmt: The vsnprintf()'s format string.
Joel Becker7063fbf2005-12-15 14:29:43 -080044 *
45 * If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
46 * dynamically allocated string that @item->ci_name points to.
47 * Otherwise, use the static @item->ci_namebuf array.
48 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070049int config_item_set_name(struct config_item *item, const char *fmt, ...)
Joel Becker7063fbf2005-12-15 14:29:43 -080050{
Joel Becker7063fbf2005-12-15 14:29:43 -080051 int limit = CONFIGFS_ITEM_NAME_LEN;
52 int need;
53 va_list args;
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070054 char *name;
Joel Becker7063fbf2005-12-15 14:29:43 -080055
56 /*
57 * First, try the static array
58 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070059 va_start(args, fmt);
60 need = vsnprintf(item->ci_namebuf, limit, fmt, args);
Joel Becker7063fbf2005-12-15 14:29:43 -080061 va_end(args);
62 if (need < limit)
63 name = item->ci_namebuf;
64 else {
Fabian Frederickf6b1fe72014-06-04 16:05:57 -070065 va_start(args, fmt);
Bart Van Assche707c6232018-06-15 15:20:42 -070066 name = kvasprintf(GFP_KERNEL, fmt, args);
Joel Becker7063fbf2005-12-15 14:29:43 -080067 va_end(args);
Bart Van Assche707c6232018-06-15 15:20:42 -070068 if (!name)
69 return -EFAULT;
Joel Becker7063fbf2005-12-15 14:29:43 -080070 }
71
72 /* Free the old name, if necessary. */
73 if (item->ci_name && item->ci_name != item->ci_namebuf)
74 kfree(item->ci_name);
75
76 /* Now, set the new name */
77 item->ci_name = name;
Bart Van Assche707c6232018-06-15 15:20:42 -070078 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -080079}
Joel Becker7063fbf2005-12-15 14:29:43 -080080EXPORT_SYMBOL(config_item_set_name);
81
82void config_item_init_type_name(struct config_item *item,
83 const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +020084 const struct config_item_type *type)
Joel Becker7063fbf2005-12-15 14:29:43 -080085{
Nicolas Iooss3958b792015-07-17 16:23:45 -070086 config_item_set_name(item, "%s", name);
Joel Becker7063fbf2005-12-15 14:29:43 -080087 item->ci_type = type;
88 config_item_init(item);
89}
90EXPORT_SYMBOL(config_item_init_type_name);
91
92void config_group_init_type_name(struct config_group *group, const char *name,
Bhumika Goyalaa293582017-10-16 17:18:40 +020093 const struct config_item_type *type)
Joel Becker7063fbf2005-12-15 14:29:43 -080094{
Nicolas Iooss3958b792015-07-17 16:23:45 -070095 config_item_set_name(&group->cg_item, "%s", name);
Joel Becker7063fbf2005-12-15 14:29:43 -080096 group->cg_item.ci_type = type;
97 config_group_init(group);
98}
99EXPORT_SYMBOL(config_group_init_type_name);
100
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700101struct config_item *config_item_get(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800102{
103 if (item)
104 kref_get(&item->ci_kref);
105 return item;
106}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700107EXPORT_SYMBOL(config_item_get);
Joel Becker7063fbf2005-12-15 14:29:43 -0800108
Bart Van Assche19e72d32017-02-09 17:28:50 -0800109struct config_item *config_item_get_unless_zero(struct config_item *item)
110{
111 if (item && kref_get_unless_zero(&item->ci_kref))
112 return item;
113 return NULL;
114}
115EXPORT_SYMBOL(config_item_get_unless_zero);
116
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700117static void config_item_cleanup(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800118{
Bhumika Goyalaa293582017-10-16 17:18:40 +0200119 const struct config_item_type *t = item->ci_type;
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700120 struct config_group *s = item->ci_group;
121 struct config_item *parent = item->ci_parent;
Joel Becker7063fbf2005-12-15 14:29:43 -0800122
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700123 pr_debug("config_item %s: cleaning up\n", config_item_name(item));
Joel Becker7063fbf2005-12-15 14:29:43 -0800124 if (item->ci_name != item->ci_namebuf)
125 kfree(item->ci_name);
126 item->ci_name = NULL;
127 if (t && t->ct_item_ops && t->ct_item_ops->release)
128 t->ct_item_ops->release(item);
129 if (s)
130 config_group_put(s);
131 if (parent)
132 config_item_put(parent);
133}
134
135static void config_item_release(struct kref *kref)
136{
137 config_item_cleanup(container_of(kref, struct config_item, ci_kref));
138}
139
140/**
141 * config_item_put - decrement refcount for item.
142 * @item: item.
143 *
144 * Decrement the refcount, and if 0, call config_item_cleanup().
145 */
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700146void config_item_put(struct config_item *item)
Joel Becker7063fbf2005-12-15 14:29:43 -0800147{
148 if (item)
149 kref_put(&item->ci_kref, config_item_release);
150}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700151EXPORT_SYMBOL(config_item_put);
Joel Becker7063fbf2005-12-15 14:29:43 -0800152
Joel Becker7063fbf2005-12-15 14:29:43 -0800153/**
154 * config_group_init - initialize a group for use
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700155 * @group: config_group
Joel Becker7063fbf2005-12-15 14:29:43 -0800156 */
Joel Becker7063fbf2005-12-15 14:29:43 -0800157void config_group_init(struct config_group *group)
158{
159 config_item_init(&group->cg_item);
160 INIT_LIST_HEAD(&group->cg_children);
Christoph Hellwig1ae16022016-02-26 11:02:14 +0100161 INIT_LIST_HEAD(&group->default_groups);
Joel Becker7063fbf2005-12-15 14:29:43 -0800162}
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700163EXPORT_SYMBOL(config_group_init);
Joel Becker7063fbf2005-12-15 14:29:43 -0800164
Joel Becker7063fbf2005-12-15 14:29:43 -0800165/**
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530166 * config_group_find_item - search for item in group.
Joel Becker7063fbf2005-12-15 14:29:43 -0800167 * @group: group we're looking in.
168 * @name: item's name.
169 *
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530170 * Iterate over @group->cg_list, looking for a matching config_item.
171 * If matching item is found take a reference and return the item.
172 * Caller must have locked group via @group->cg_subsys->su_mtx.
Joel Becker7063fbf2005-12-15 14:29:43 -0800173 */
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530174struct config_item *config_group_find_item(struct config_group *group,
175 const char *name)
Joel Becker7063fbf2005-12-15 14:29:43 -0800176{
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700177 struct list_head *entry;
178 struct config_item *ret = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800179
Fabian Frederickf6b1fe72014-06-04 16:05:57 -0700180 list_for_each(entry, &group->cg_children) {
181 struct config_item *item = to_item(entry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800182 if (config_item_name(item) &&
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530183 !strcmp(config_item_name(item), name)) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800184 ret = config_item_get(item);
185 break;
186 }
187 }
188 return ret;
189}
Satyam Sharma3fe6c5c2007-07-04 16:37:16 +0530190EXPORT_SYMBOL(config_group_find_item);