1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset:8; -*-
3*4882a593Smuzhiyun * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * configfs_internal.h - Internal stuff for configfs
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Based on sysfs:
8*4882a593Smuzhiyun * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * configfs Copyright (C) 2005 Oracle. All rights reserved.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #ifdef pr_fmt
14*4882a593Smuzhiyun #undef pr_fmt
15*4882a593Smuzhiyun #endif
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/list.h>
21*4882a593Smuzhiyun #include <linux/spinlock.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct configfs_fragment {
24*4882a593Smuzhiyun atomic_t frag_count;
25*4882a593Smuzhiyun struct rw_semaphore frag_sem;
26*4882a593Smuzhiyun bool frag_dead;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun void put_fragment(struct configfs_fragment *);
30*4882a593Smuzhiyun struct configfs_fragment *get_fragment(struct configfs_fragment *);
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun struct configfs_dirent {
33*4882a593Smuzhiyun atomic_t s_count;
34*4882a593Smuzhiyun int s_dependent_count;
35*4882a593Smuzhiyun struct list_head s_sibling;
36*4882a593Smuzhiyun struct list_head s_children;
37*4882a593Smuzhiyun int s_links;
38*4882a593Smuzhiyun void * s_element;
39*4882a593Smuzhiyun int s_type;
40*4882a593Smuzhiyun umode_t s_mode;
41*4882a593Smuzhiyun struct dentry * s_dentry;
42*4882a593Smuzhiyun struct iattr * s_iattr;
43*4882a593Smuzhiyun #ifdef CONFIG_LOCKDEP
44*4882a593Smuzhiyun int s_depth;
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun struct configfs_fragment *s_frag;
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #define CONFIGFS_ROOT 0x0001
50*4882a593Smuzhiyun #define CONFIGFS_DIR 0x0002
51*4882a593Smuzhiyun #define CONFIGFS_ITEM_ATTR 0x0004
52*4882a593Smuzhiyun #define CONFIGFS_ITEM_BIN_ATTR 0x0008
53*4882a593Smuzhiyun #define CONFIGFS_ITEM_LINK 0x0020
54*4882a593Smuzhiyun #define CONFIGFS_USET_DIR 0x0040
55*4882a593Smuzhiyun #define CONFIGFS_USET_DEFAULT 0x0080
56*4882a593Smuzhiyun #define CONFIGFS_USET_DROPPING 0x0100
57*4882a593Smuzhiyun #define CONFIGFS_USET_IN_MKDIR 0x0200
58*4882a593Smuzhiyun #define CONFIGFS_USET_CREATING 0x0400
59*4882a593Smuzhiyun #define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun extern struct mutex configfs_symlink_mutex;
62*4882a593Smuzhiyun extern spinlock_t configfs_dirent_lock;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun extern struct kmem_cache *configfs_dir_cachep;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun extern int configfs_is_root(struct config_item *item);
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun extern struct inode * configfs_new_inode(umode_t mode, struct configfs_dirent *, struct super_block *);
69*4882a593Smuzhiyun extern struct inode *configfs_create(struct dentry *, umode_t mode);
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
72*4882a593Smuzhiyun extern int configfs_create_bin_file(struct config_item *,
73*4882a593Smuzhiyun const struct configfs_bin_attribute *);
74*4882a593Smuzhiyun extern int configfs_make_dirent(struct configfs_dirent *, struct dentry *,
75*4882a593Smuzhiyun void *, umode_t, int, struct configfs_fragment *);
76*4882a593Smuzhiyun extern int configfs_dirent_is_ready(struct configfs_dirent *);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun extern void configfs_hash_and_remove(struct dentry * dir, const char * name);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun extern const unsigned char * configfs_get_name(struct configfs_dirent *sd);
81*4882a593Smuzhiyun extern void configfs_drop_dentry(struct configfs_dirent *sd, struct dentry *parent);
82*4882a593Smuzhiyun extern int configfs_setattr(struct dentry *dentry, struct iattr *iattr);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun extern struct dentry *configfs_pin_fs(void);
85*4882a593Smuzhiyun extern void configfs_release_fs(void);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun extern const struct file_operations configfs_dir_operations;
88*4882a593Smuzhiyun extern const struct file_operations configfs_file_operations;
89*4882a593Smuzhiyun extern const struct file_operations configfs_bin_file_operations;
90*4882a593Smuzhiyun extern const struct inode_operations configfs_dir_inode_operations;
91*4882a593Smuzhiyun extern const struct inode_operations configfs_root_inode_operations;
92*4882a593Smuzhiyun extern const struct inode_operations configfs_symlink_inode_operations;
93*4882a593Smuzhiyun extern const struct dentry_operations configfs_dentry_ops;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun extern int configfs_symlink(struct inode *dir, struct dentry *dentry,
96*4882a593Smuzhiyun const char *symname);
97*4882a593Smuzhiyun extern int configfs_unlink(struct inode *dir, struct dentry *dentry);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
100*4882a593Smuzhiyun struct dentry *dentry, char *body);
101*4882a593Smuzhiyun
to_item(struct dentry * dentry)102*4882a593Smuzhiyun static inline struct config_item * to_item(struct dentry * dentry)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun struct configfs_dirent * sd = dentry->d_fsdata;
105*4882a593Smuzhiyun return ((struct config_item *) sd->s_element);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
to_attr(struct dentry * dentry)108*4882a593Smuzhiyun static inline struct configfs_attribute * to_attr(struct dentry * dentry)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun struct configfs_dirent * sd = dentry->d_fsdata;
111*4882a593Smuzhiyun return ((struct configfs_attribute *) sd->s_element);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
to_bin_attr(struct dentry * dentry)114*4882a593Smuzhiyun static inline struct configfs_bin_attribute *to_bin_attr(struct dentry *dentry)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun struct configfs_attribute *attr = to_attr(dentry);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun return container_of(attr, struct configfs_bin_attribute, cb_attr);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
configfs_get_config_item(struct dentry * dentry)121*4882a593Smuzhiyun static inline struct config_item *configfs_get_config_item(struct dentry *dentry)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun struct config_item * item = NULL;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
126*4882a593Smuzhiyun if (!d_unhashed(dentry)) {
127*4882a593Smuzhiyun struct configfs_dirent * sd = dentry->d_fsdata;
128*4882a593Smuzhiyun item = config_item_get(sd->s_element);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun return item;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
release_configfs_dirent(struct configfs_dirent * sd)135*4882a593Smuzhiyun static inline void release_configfs_dirent(struct configfs_dirent * sd)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun if (!(sd->s_type & CONFIGFS_ROOT)) {
138*4882a593Smuzhiyun kfree(sd->s_iattr);
139*4882a593Smuzhiyun put_fragment(sd->s_frag);
140*4882a593Smuzhiyun kmem_cache_free(configfs_dir_cachep, sd);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
configfs_get(struct configfs_dirent * sd)144*4882a593Smuzhiyun static inline struct configfs_dirent * configfs_get(struct configfs_dirent * sd)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun if (sd) {
147*4882a593Smuzhiyun WARN_ON(!atomic_read(&sd->s_count));
148*4882a593Smuzhiyun atomic_inc(&sd->s_count);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun return sd;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
configfs_put(struct configfs_dirent * sd)153*4882a593Smuzhiyun static inline void configfs_put(struct configfs_dirent * sd)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun WARN_ON(!atomic_read(&sd->s_count));
156*4882a593Smuzhiyun if (atomic_dec_and_test(&sd->s_count))
157*4882a593Smuzhiyun release_configfs_dirent(sd);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160