]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/configfs/item.c
timekeeping: Repair ktime_get_coarse*() granularity
[mirror_ubuntu-jammy-kernel.git] / fs / configfs / item.c
CommitLineData
7063fbf2
JB
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * item.c - library routines for handling generic config items
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
20 *
21 * Based on kobject:
f6b1fe7c 22 * kobject is Copyright (c) 2002-2003 Patrick Mochel
7063fbf2
JB
23 *
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
25 *
395cf969 26 * Please see the file Documentation/filesystems/configfs/configfs.txt for
7063fbf2
JB
27 * critical information about using the config_item interface.
28 */
29
30#include <linux/string.h>
31#include <linux/module.h>
32#include <linux/stat.h>
33#include <linux/slab.h>
34
35#include <linux/configfs.h>
36
37
f6b1fe7c 38static inline struct config_item *to_item(struct list_head *entry)
7063fbf2 39{
f6b1fe7c 40 return container_of(entry, struct config_item, ci_entry);
7063fbf2
JB
41}
42
43/* Evil kernel */
44static void config_item_release(struct kref *kref);
45
46/**
47 * config_item_init - initialize item.
48 * @item: item in question.
49 */
5286d20c 50static void config_item_init(struct config_item *item)
7063fbf2
JB
51{
52 kref_init(&item->ci_kref);
53 INIT_LIST_HEAD(&item->ci_entry);
54}
55
56/**
57 * config_item_set_name - Set the name of an item
58 * @item: item.
f6b1fe7c 59 * @fmt: The vsnprintf()'s format string.
7063fbf2
JB
60 *
61 * If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
62 * dynamically allocated string that @item->ci_name points to.
63 * Otherwise, use the static @item->ci_namebuf array.
64 */
f6b1fe7c 65int config_item_set_name(struct config_item *item, const char *fmt, ...)
7063fbf2 66{
7063fbf2
JB
67 int limit = CONFIGFS_ITEM_NAME_LEN;
68 int need;
69 va_list args;
f6b1fe7c 70 char *name;
7063fbf2
JB
71
72 /*
73 * First, try the static array
74 */
f6b1fe7c
FF
75 va_start(args, fmt);
76 need = vsnprintf(item->ci_namebuf, limit, fmt, args);
7063fbf2
JB
77 va_end(args);
78 if (need < limit)
79 name = item->ci_namebuf;
80 else {
f6b1fe7c 81 va_start(args, fmt);
707c6235 82 name = kvasprintf(GFP_KERNEL, fmt, args);
7063fbf2 83 va_end(args);
707c6235
BVA
84 if (!name)
85 return -EFAULT;
7063fbf2
JB
86 }
87
88 /* Free the old name, if necessary. */
89 if (item->ci_name && item->ci_name != item->ci_namebuf)
90 kfree(item->ci_name);
91
92 /* Now, set the new name */
93 item->ci_name = name;
707c6235 94 return 0;
7063fbf2 95}
7063fbf2
JB
96EXPORT_SYMBOL(config_item_set_name);
97
98void config_item_init_type_name(struct config_item *item,
99 const char *name,
aa293583 100 const struct config_item_type *type)
7063fbf2 101{
3958b792 102 config_item_set_name(item, "%s", name);
7063fbf2
JB
103 item->ci_type = type;
104 config_item_init(item);
105}
106EXPORT_SYMBOL(config_item_init_type_name);
107
108void config_group_init_type_name(struct config_group *group, const char *name,
aa293583 109 const struct config_item_type *type)
7063fbf2 110{
3958b792 111 config_item_set_name(&group->cg_item, "%s", name);
7063fbf2
JB
112 group->cg_item.ci_type = type;
113 config_group_init(group);
114}
115EXPORT_SYMBOL(config_group_init_type_name);
116
f6b1fe7c 117struct config_item *config_item_get(struct config_item *item)
7063fbf2
JB
118{
119 if (item)
120 kref_get(&item->ci_kref);
121 return item;
122}
f6b1fe7c 123EXPORT_SYMBOL(config_item_get);
7063fbf2 124
19e72d3a
BVA
125struct config_item *config_item_get_unless_zero(struct config_item *item)
126{
127 if (item && kref_get_unless_zero(&item->ci_kref))
128 return item;
129 return NULL;
130}
131EXPORT_SYMBOL(config_item_get_unless_zero);
132
f6b1fe7c 133static void config_item_cleanup(struct config_item *item)
7063fbf2 134{
aa293583 135 const struct config_item_type *t = item->ci_type;
f6b1fe7c
FF
136 struct config_group *s = item->ci_group;
137 struct config_item *parent = item->ci_parent;
7063fbf2 138
f6b1fe7c 139 pr_debug("config_item %s: cleaning up\n", config_item_name(item));
7063fbf2
JB
140 if (item->ci_name != item->ci_namebuf)
141 kfree(item->ci_name);
142 item->ci_name = NULL;
143 if (t && t->ct_item_ops && t->ct_item_ops->release)
144 t->ct_item_ops->release(item);
145 if (s)
146 config_group_put(s);
147 if (parent)
148 config_item_put(parent);
149}
150
151static void config_item_release(struct kref *kref)
152{
153 config_item_cleanup(container_of(kref, struct config_item, ci_kref));
154}
155
156/**
157 * config_item_put - decrement refcount for item.
158 * @item: item.
159 *
160 * Decrement the refcount, and if 0, call config_item_cleanup().
161 */
f6b1fe7c 162void config_item_put(struct config_item *item)
7063fbf2
JB
163{
164 if (item)
165 kref_put(&item->ci_kref, config_item_release);
166}
f6b1fe7c 167EXPORT_SYMBOL(config_item_put);
7063fbf2 168
7063fbf2
JB
169/**
170 * config_group_init - initialize a group for use
f6b1fe7c 171 * @group: config_group
7063fbf2 172 */
7063fbf2
JB
173void config_group_init(struct config_group *group)
174{
175 config_item_init(&group->cg_item);
176 INIT_LIST_HEAD(&group->cg_children);
1ae1602d 177 INIT_LIST_HEAD(&group->default_groups);
7063fbf2 178}
f6b1fe7c 179EXPORT_SYMBOL(config_group_init);
7063fbf2 180
7063fbf2 181/**
3fe6c5ce 182 * config_group_find_item - search for item in group.
7063fbf2
JB
183 * @group: group we're looking in.
184 * @name: item's name.
185 *
3fe6c5ce
SS
186 * Iterate over @group->cg_list, looking for a matching config_item.
187 * If matching item is found take a reference and return the item.
188 * Caller must have locked group via @group->cg_subsys->su_mtx.
7063fbf2 189 */
3fe6c5ce
SS
190struct config_item *config_group_find_item(struct config_group *group,
191 const char *name)
7063fbf2 192{
f6b1fe7c
FF
193 struct list_head *entry;
194 struct config_item *ret = NULL;
7063fbf2 195
f6b1fe7c
FF
196 list_for_each(entry, &group->cg_children) {
197 struct config_item *item = to_item(entry);
7063fbf2 198 if (config_item_name(item) &&
3fe6c5ce 199 !strcmp(config_item_name(item), name)) {
7063fbf2
JB
200 ret = config_item_get(item);
201 break;
202 }
203 }
204 return ret;
205}
3fe6c5ce 206EXPORT_SYMBOL(config_group_find_item);