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