Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * configfs_internal.h - Internal stuff for configfs
0004  *
0005  * Based on sysfs:
0006  *  sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
0007  *
0008  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
0009  */
0010 
0011 #ifdef pr_fmt
0012 #undef pr_fmt
0013 #endif
0014 
0015 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0016 
0017 #include <linux/slab.h>
0018 #include <linux/list.h>
0019 #include <linux/spinlock.h>
0020 
0021 struct configfs_fragment {
0022     atomic_t frag_count;
0023     struct rw_semaphore frag_sem;
0024     bool frag_dead;
0025 };
0026 
0027 void put_fragment(struct configfs_fragment *);
0028 struct configfs_fragment *get_fragment(struct configfs_fragment *);
0029 
0030 struct configfs_dirent {
0031     atomic_t        s_count;
0032     int         s_dependent_count;
0033     struct list_head    s_sibling;
0034     struct list_head    s_children;
0035     int         s_links;
0036     void            * s_element;
0037     int         s_type;
0038     umode_t         s_mode;
0039     struct dentry       * s_dentry;
0040     struct iattr        * s_iattr;
0041 #ifdef CONFIG_LOCKDEP
0042     int         s_depth;
0043 #endif
0044     struct configfs_fragment *s_frag;
0045 };
0046 
0047 #define CONFIGFS_ROOT       0x0001
0048 #define CONFIGFS_DIR        0x0002
0049 #define CONFIGFS_ITEM_ATTR  0x0004
0050 #define CONFIGFS_ITEM_BIN_ATTR  0x0008
0051 #define CONFIGFS_ITEM_LINK  0x0020
0052 #define CONFIGFS_USET_DIR   0x0040
0053 #define CONFIGFS_USET_DEFAULT   0x0080
0054 #define CONFIGFS_USET_DROPPING  0x0100
0055 #define CONFIGFS_USET_IN_MKDIR  0x0200
0056 #define CONFIGFS_USET_CREATING  0x0400
0057 #define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)
0058 
0059 extern struct mutex configfs_symlink_mutex;
0060 extern spinlock_t configfs_dirent_lock;
0061 
0062 extern struct kmem_cache *configfs_dir_cachep;
0063 
0064 extern int configfs_is_root(struct config_item *item);
0065 
0066 extern struct inode * configfs_new_inode(umode_t mode, struct configfs_dirent *, struct super_block *);
0067 extern struct inode *configfs_create(struct dentry *, umode_t mode);
0068 
0069 extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
0070 extern int configfs_create_bin_file(struct config_item *,
0071                     const struct configfs_bin_attribute *);
0072 extern int configfs_make_dirent(struct configfs_dirent *, struct dentry *,
0073                 void *, umode_t, int, struct configfs_fragment *);
0074 extern int configfs_dirent_is_ready(struct configfs_dirent *);
0075 
0076 extern void configfs_hash_and_remove(struct dentry * dir, const char * name);
0077 
0078 extern const unsigned char * configfs_get_name(struct configfs_dirent *sd);
0079 extern void configfs_drop_dentry(struct configfs_dirent *sd, struct dentry *parent);
0080 extern int configfs_setattr(struct user_namespace *mnt_userns,
0081                 struct dentry *dentry, struct iattr *iattr);
0082 
0083 extern struct dentry *configfs_pin_fs(void);
0084 extern void configfs_release_fs(void);
0085 
0086 extern const struct file_operations configfs_dir_operations;
0087 extern const struct file_operations configfs_file_operations;
0088 extern const struct file_operations configfs_bin_file_operations;
0089 extern const struct inode_operations configfs_dir_inode_operations;
0090 extern const struct inode_operations configfs_root_inode_operations;
0091 extern const struct inode_operations configfs_symlink_inode_operations;
0092 extern const struct dentry_operations configfs_dentry_ops;
0093 
0094 extern int configfs_symlink(struct user_namespace *mnt_userns,
0095                 struct inode *dir, struct dentry *dentry,
0096                 const char *symname);
0097 extern int configfs_unlink(struct inode *dir, struct dentry *dentry);
0098 
0099 int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
0100         struct dentry *dentry, char *body);
0101 
0102 static inline struct config_item * to_item(struct dentry * dentry)
0103 {
0104     struct configfs_dirent * sd = dentry->d_fsdata;
0105     return ((struct config_item *) sd->s_element);
0106 }
0107 
0108 static inline struct configfs_attribute * to_attr(struct dentry * dentry)
0109 {
0110     struct configfs_dirent * sd = dentry->d_fsdata;
0111     return ((struct configfs_attribute *) sd->s_element);
0112 }
0113 
0114 static inline struct configfs_bin_attribute *to_bin_attr(struct dentry *dentry)
0115 {
0116     struct configfs_attribute *attr = to_attr(dentry);
0117 
0118     return container_of(attr, struct configfs_bin_attribute, cb_attr);
0119 }
0120 
0121 static inline struct config_item *configfs_get_config_item(struct dentry *dentry)
0122 {
0123     struct config_item * item = NULL;
0124 
0125     spin_lock(&dentry->d_lock);
0126     if (!d_unhashed(dentry)) {
0127         struct configfs_dirent * sd = dentry->d_fsdata;
0128         item = config_item_get(sd->s_element);
0129     }
0130     spin_unlock(&dentry->d_lock);
0131 
0132     return item;
0133 }
0134 
0135 static inline void release_configfs_dirent(struct configfs_dirent * sd)
0136 {
0137     if (!(sd->s_type & CONFIGFS_ROOT)) {
0138         kfree(sd->s_iattr);
0139         put_fragment(sd->s_frag);
0140         kmem_cache_free(configfs_dir_cachep, sd);
0141     }
0142 }
0143 
0144 static inline struct configfs_dirent * configfs_get(struct configfs_dirent * sd)
0145 {
0146     if (sd) {
0147         WARN_ON(!atomic_read(&sd->s_count));
0148         atomic_inc(&sd->s_count);
0149     }
0150     return sd;
0151 }
0152 
0153 static inline void configfs_put(struct configfs_dirent * sd)
0154 {
0155     WARN_ON(!atomic_read(&sd->s_count));
0156     if (atomic_dec_and_test(&sd->s_count))
0157         release_configfs_dirent(sd);
0158 }
0159