Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * inode.c - basic inode and dentry operations.
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  * Please see Documentation/filesystems/configfs.rst for more
0011  * information.
0012  */
0013 
0014 #undef DEBUG
0015 
0016 #include <linux/pagemap.h>
0017 #include <linux/namei.h>
0018 #include <linux/backing-dev.h>
0019 #include <linux/capability.h>
0020 #include <linux/sched.h>
0021 #include <linux/lockdep.h>
0022 #include <linux/slab.h>
0023 
0024 #include <linux/configfs.h>
0025 #include "configfs_internal.h"
0026 
0027 #ifdef CONFIG_LOCKDEP
0028 static struct lock_class_key default_group_class[MAX_LOCK_DEPTH];
0029 #endif
0030 
0031 static const struct inode_operations configfs_inode_operations ={
0032     .setattr    = configfs_setattr,
0033 };
0034 
0035 int configfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0036              struct iattr *iattr)
0037 {
0038     struct inode * inode = d_inode(dentry);
0039     struct configfs_dirent * sd = dentry->d_fsdata;
0040     struct iattr * sd_iattr;
0041     unsigned int ia_valid = iattr->ia_valid;
0042     int error;
0043 
0044     if (!sd)
0045         return -EINVAL;
0046 
0047     sd_iattr = sd->s_iattr;
0048     if (!sd_iattr) {
0049         /* setting attributes for the first time, allocate now */
0050         sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
0051         if (!sd_iattr)
0052             return -ENOMEM;
0053         /* assign default attributes */
0054         sd_iattr->ia_mode = sd->s_mode;
0055         sd_iattr->ia_uid = GLOBAL_ROOT_UID;
0056         sd_iattr->ia_gid = GLOBAL_ROOT_GID;
0057         sd_iattr->ia_atime = sd_iattr->ia_mtime =
0058             sd_iattr->ia_ctime = current_time(inode);
0059         sd->s_iattr = sd_iattr;
0060     }
0061     /* attributes were changed atleast once in past */
0062 
0063     error = simple_setattr(mnt_userns, dentry, iattr);
0064     if (error)
0065         return error;
0066 
0067     if (ia_valid & ATTR_UID)
0068         sd_iattr->ia_uid = iattr->ia_uid;
0069     if (ia_valid & ATTR_GID)
0070         sd_iattr->ia_gid = iattr->ia_gid;
0071     if (ia_valid & ATTR_ATIME)
0072         sd_iattr->ia_atime = iattr->ia_atime;
0073     if (ia_valid & ATTR_MTIME)
0074         sd_iattr->ia_mtime = iattr->ia_mtime;
0075     if (ia_valid & ATTR_CTIME)
0076         sd_iattr->ia_ctime = iattr->ia_ctime;
0077     if (ia_valid & ATTR_MODE) {
0078         umode_t mode = iattr->ia_mode;
0079 
0080         if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
0081             mode &= ~S_ISGID;
0082         sd_iattr->ia_mode = sd->s_mode = mode;
0083     }
0084 
0085     return error;
0086 }
0087 
0088 static inline void set_default_inode_attr(struct inode * inode, umode_t mode)
0089 {
0090     inode->i_mode = mode;
0091     inode->i_atime = inode->i_mtime =
0092         inode->i_ctime = current_time(inode);
0093 }
0094 
0095 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
0096 {
0097     inode->i_mode = iattr->ia_mode;
0098     inode->i_uid = iattr->ia_uid;
0099     inode->i_gid = iattr->ia_gid;
0100     inode->i_atime = iattr->ia_atime;
0101     inode->i_mtime = iattr->ia_mtime;
0102     inode->i_ctime = iattr->ia_ctime;
0103 }
0104 
0105 struct inode *configfs_new_inode(umode_t mode, struct configfs_dirent *sd,
0106                  struct super_block *s)
0107 {
0108     struct inode * inode = new_inode(s);
0109     if (inode) {
0110         inode->i_ino = get_next_ino();
0111         inode->i_mapping->a_ops = &ram_aops;
0112         inode->i_op = &configfs_inode_operations;
0113 
0114         if (sd->s_iattr) {
0115             /* sysfs_dirent has non-default attributes
0116              * get them for the new inode from persistent copy
0117              * in sysfs_dirent
0118              */
0119             set_inode_attr(inode, sd->s_iattr);
0120         } else
0121             set_default_inode_attr(inode, mode);
0122     }
0123     return inode;
0124 }
0125 
0126 #ifdef CONFIG_LOCKDEP
0127 
0128 static void configfs_set_inode_lock_class(struct configfs_dirent *sd,
0129                       struct inode *inode)
0130 {
0131     int depth = sd->s_depth;
0132 
0133     if (depth > 0) {
0134         if (depth <= ARRAY_SIZE(default_group_class)) {
0135             lockdep_set_class(&inode->i_rwsem,
0136                       &default_group_class[depth - 1]);
0137         } else {
0138             /*
0139              * In practice the maximum level of locking depth is
0140              * already reached. Just inform about possible reasons.
0141              */
0142             pr_info("Too many levels of inodes for the locking correctness validator.\n");
0143             pr_info("Spurious warnings may appear.\n");
0144         }
0145     }
0146 }
0147 
0148 #else /* CONFIG_LOCKDEP */
0149 
0150 static void configfs_set_inode_lock_class(struct configfs_dirent *sd,
0151                       struct inode *inode)
0152 {
0153 }
0154 
0155 #endif /* CONFIG_LOCKDEP */
0156 
0157 struct inode *configfs_create(struct dentry *dentry, umode_t mode)
0158 {
0159     struct inode *inode = NULL;
0160     struct configfs_dirent *sd;
0161     struct inode *p_inode;
0162 
0163     if (!dentry)
0164         return ERR_PTR(-ENOENT);
0165 
0166     if (d_really_is_positive(dentry))
0167         return ERR_PTR(-EEXIST);
0168 
0169     sd = dentry->d_fsdata;
0170     inode = configfs_new_inode(mode, sd, dentry->d_sb);
0171     if (!inode)
0172         return ERR_PTR(-ENOMEM);
0173 
0174     p_inode = d_inode(dentry->d_parent);
0175     p_inode->i_mtime = p_inode->i_ctime = current_time(p_inode);
0176     configfs_set_inode_lock_class(sd, inode);
0177     return inode;
0178 }
0179 
0180 /*
0181  * Get the name for corresponding element represented by the given configfs_dirent
0182  */
0183 const unsigned char * configfs_get_name(struct configfs_dirent *sd)
0184 {
0185     struct configfs_attribute *attr;
0186 
0187     BUG_ON(!sd || !sd->s_element);
0188 
0189     /* These always have a dentry, so use that */
0190     if (sd->s_type & (CONFIGFS_DIR | CONFIGFS_ITEM_LINK))
0191         return sd->s_dentry->d_name.name;
0192 
0193     if (sd->s_type & (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)) {
0194         attr = sd->s_element;
0195         return attr->ca_name;
0196     }
0197     return NULL;
0198 }
0199 
0200 
0201 /*
0202  * Unhashes the dentry corresponding to given configfs_dirent
0203  * Called with parent inode's i_mutex held.
0204  */
0205 void configfs_drop_dentry(struct configfs_dirent * sd, struct dentry * parent)
0206 {
0207     struct dentry * dentry = sd->s_dentry;
0208 
0209     if (dentry) {
0210         spin_lock(&dentry->d_lock);
0211         if (simple_positive(dentry)) {
0212             dget_dlock(dentry);
0213             __d_drop(dentry);
0214             spin_unlock(&dentry->d_lock);
0215             simple_unlink(d_inode(parent), dentry);
0216         } else
0217             spin_unlock(&dentry->d_lock);
0218     }
0219 }
0220 
0221 void configfs_hash_and_remove(struct dentry * dir, const char * name)
0222 {
0223     struct configfs_dirent * sd;
0224     struct configfs_dirent * parent_sd = dir->d_fsdata;
0225 
0226     if (d_really_is_negative(dir))
0227         /* no inode means this hasn't been made visible yet */
0228         return;
0229 
0230     inode_lock(d_inode(dir));
0231     list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
0232         if (!sd->s_element)
0233             continue;
0234         if (!strcmp(configfs_get_name(sd), name)) {
0235             spin_lock(&configfs_dirent_lock);
0236             list_del_init(&sd->s_sibling);
0237             spin_unlock(&configfs_dirent_lock);
0238             configfs_drop_dentry(sd, dir);
0239             configfs_put(sd);
0240             break;
0241         }
0242     }
0243     inode_unlock(d_inode(dir));
0244 }