LCOV - code coverage report
Current view: top level - fs/kernfs - inode.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 163 0.0 %
Date: 2023-07-19 18:55:55 Functions: 0 17 0.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  * fs/kernfs/inode.c - kernfs inode implementation
       4             :  *
       5             :  * Copyright (c) 2001-3 Patrick Mochel
       6             :  * Copyright (c) 2007 SUSE Linux Products GmbH
       7             :  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
       8             :  */
       9             : 
      10             : #include <linux/pagemap.h>
      11             : #include <linux/backing-dev.h>
      12             : #include <linux/capability.h>
      13             : #include <linux/errno.h>
      14             : #include <linux/slab.h>
      15             : #include <linux/xattr.h>
      16             : #include <linux/security.h>
      17             : 
      18             : #include "kernfs-internal.h"
      19             : 
      20             : static const struct inode_operations kernfs_iops = {
      21             :         .permission     = kernfs_iop_permission,
      22             :         .setattr        = kernfs_iop_setattr,
      23             :         .getattr        = kernfs_iop_getattr,
      24             :         .listxattr      = kernfs_iop_listxattr,
      25             : };
      26             : 
      27           0 : static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
      28             : {
      29             :         static DEFINE_MUTEX(iattr_mutex);
      30             :         struct kernfs_iattrs *ret;
      31             : 
      32           0 :         mutex_lock(&iattr_mutex);
      33             : 
      34           0 :         if (kn->iattr || !alloc)
      35             :                 goto out_unlock;
      36             : 
      37           0 :         kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
      38           0 :         if (!kn->iattr)
      39             :                 goto out_unlock;
      40             : 
      41             :         /* assign default attributes */
      42           0 :         kn->iattr->ia_uid = GLOBAL_ROOT_UID;
      43           0 :         kn->iattr->ia_gid = GLOBAL_ROOT_GID;
      44             : 
      45           0 :         ktime_get_real_ts64(&kn->iattr->ia_atime);
      46           0 :         kn->iattr->ia_mtime = kn->iattr->ia_atime;
      47           0 :         kn->iattr->ia_ctime = kn->iattr->ia_atime;
      48             : 
      49           0 :         simple_xattrs_init(&kn->iattr->xattrs);
      50           0 :         atomic_set(&kn->iattr->nr_user_xattrs, 0);
      51           0 :         atomic_set(&kn->iattr->user_xattr_size, 0);
      52             : out_unlock:
      53           0 :         ret = kn->iattr;
      54           0 :         mutex_unlock(&iattr_mutex);
      55           0 :         return ret;
      56             : }
      57             : 
      58             : static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
      59             : {
      60           0 :         return __kernfs_iattrs(kn, 1);
      61             : }
      62             : 
      63             : static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
      64             : {
      65           0 :         return __kernfs_iattrs(kn, 0);
      66             : }
      67             : 
      68           0 : int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
      69             : {
      70             :         struct kernfs_iattrs *attrs;
      71           0 :         unsigned int ia_valid = iattr->ia_valid;
      72             : 
      73           0 :         attrs = kernfs_iattrs(kn);
      74           0 :         if (!attrs)
      75             :                 return -ENOMEM;
      76             : 
      77           0 :         if (ia_valid & ATTR_UID)
      78           0 :                 attrs->ia_uid = iattr->ia_uid;
      79           0 :         if (ia_valid & ATTR_GID)
      80           0 :                 attrs->ia_gid = iattr->ia_gid;
      81           0 :         if (ia_valid & ATTR_ATIME)
      82           0 :                 attrs->ia_atime = iattr->ia_atime;
      83           0 :         if (ia_valid & ATTR_MTIME)
      84           0 :                 attrs->ia_mtime = iattr->ia_mtime;
      85           0 :         if (ia_valid & ATTR_CTIME)
      86           0 :                 attrs->ia_ctime = iattr->ia_ctime;
      87           0 :         if (ia_valid & ATTR_MODE)
      88           0 :                 kn->mode = iattr->ia_mode;
      89             :         return 0;
      90             : }
      91             : 
      92             : /**
      93             :  * kernfs_setattr - set iattr on a node
      94             :  * @kn: target node
      95             :  * @iattr: iattr to set
      96             :  *
      97             :  * Return: %0 on success, -errno on failure.
      98             :  */
      99           0 : int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
     100             : {
     101             :         int ret;
     102           0 :         struct kernfs_root *root = kernfs_root(kn);
     103             : 
     104           0 :         down_write(&root->kernfs_iattr_rwsem);
     105           0 :         ret = __kernfs_setattr(kn, iattr);
     106           0 :         up_write(&root->kernfs_iattr_rwsem);
     107           0 :         return ret;
     108             : }
     109             : 
     110           0 : int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
     111             :                        struct iattr *iattr)
     112             : {
     113           0 :         struct inode *inode = d_inode(dentry);
     114           0 :         struct kernfs_node *kn = inode->i_private;
     115             :         struct kernfs_root *root;
     116             :         int error;
     117             : 
     118           0 :         if (!kn)
     119             :                 return -EINVAL;
     120             : 
     121           0 :         root = kernfs_root(kn);
     122           0 :         down_write(&root->kernfs_iattr_rwsem);
     123           0 :         error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
     124           0 :         if (error)
     125             :                 goto out;
     126             : 
     127           0 :         error = __kernfs_setattr(kn, iattr);
     128           0 :         if (error)
     129             :                 goto out;
     130             : 
     131             :         /* this ignores size changes */
     132           0 :         setattr_copy(&nop_mnt_idmap, inode, iattr);
     133             : 
     134             : out:
     135           0 :         up_write(&root->kernfs_iattr_rwsem);
     136           0 :         return error;
     137             : }
     138             : 
     139           0 : ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
     140             : {
     141           0 :         struct kernfs_node *kn = kernfs_dentry_node(dentry);
     142             :         struct kernfs_iattrs *attrs;
     143             : 
     144           0 :         attrs = kernfs_iattrs(kn);
     145           0 :         if (!attrs)
     146             :                 return -ENOMEM;
     147             : 
     148           0 :         return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
     149             : }
     150             : 
     151             : static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
     152             : {
     153           0 :         inode->i_mode = mode;
     154           0 :         inode->i_atime = inode->i_mtime =
     155             :                 inode->i_ctime = current_time(inode);
     156             : }
     157             : 
     158             : static inline void set_inode_attr(struct inode *inode,
     159             :                                   struct kernfs_iattrs *attrs)
     160             : {
     161           0 :         inode->i_uid = attrs->ia_uid;
     162           0 :         inode->i_gid = attrs->ia_gid;
     163           0 :         inode->i_atime = attrs->ia_atime;
     164           0 :         inode->i_mtime = attrs->ia_mtime;
     165           0 :         inode->i_ctime = attrs->ia_ctime;
     166             : }
     167             : 
     168           0 : static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
     169             : {
     170           0 :         struct kernfs_iattrs *attrs = kn->iattr;
     171             : 
     172           0 :         inode->i_mode = kn->mode;
     173           0 :         if (attrs)
     174             :                 /*
     175             :                  * kernfs_node has non-default attributes get them from
     176             :                  * persistent copy in kernfs_node.
     177             :                  */
     178             :                 set_inode_attr(inode, attrs);
     179             : 
     180           0 :         if (kernfs_type(kn) == KERNFS_DIR)
     181           0 :                 set_nlink(inode, kn->dir.subdirs + 2);
     182           0 : }
     183             : 
     184           0 : int kernfs_iop_getattr(struct mnt_idmap *idmap,
     185             :                        const struct path *path, struct kstat *stat,
     186             :                        u32 request_mask, unsigned int query_flags)
     187             : {
     188           0 :         struct inode *inode = d_inode(path->dentry);
     189           0 :         struct kernfs_node *kn = inode->i_private;
     190           0 :         struct kernfs_root *root = kernfs_root(kn);
     191             : 
     192           0 :         down_read(&root->kernfs_iattr_rwsem);
     193           0 :         kernfs_refresh_inode(kn, inode);
     194           0 :         generic_fillattr(&nop_mnt_idmap, inode, stat);
     195           0 :         up_read(&root->kernfs_iattr_rwsem);
     196             : 
     197           0 :         return 0;
     198             : }
     199             : 
     200           0 : static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
     201             : {
     202           0 :         kernfs_get(kn);
     203           0 :         inode->i_private = kn;
     204           0 :         inode->i_mapping->a_ops = &ram_aops;
     205           0 :         inode->i_op = &kernfs_iops;
     206           0 :         inode->i_generation = kernfs_gen(kn);
     207             : 
     208           0 :         set_default_inode_attr(inode, kn->mode);
     209           0 :         kernfs_refresh_inode(kn, inode);
     210             : 
     211             :         /* initialize inode according to type */
     212           0 :         switch (kernfs_type(kn)) {
     213             :         case KERNFS_DIR:
     214           0 :                 inode->i_op = &kernfs_dir_iops;
     215           0 :                 inode->i_fop = &kernfs_dir_fops;
     216           0 :                 if (kn->flags & KERNFS_EMPTY_DIR)
     217           0 :                         make_empty_dir_inode(inode);
     218             :                 break;
     219             :         case KERNFS_FILE:
     220           0 :                 inode->i_size = kn->attr.size;
     221           0 :                 inode->i_fop = &kernfs_file_fops;
     222           0 :                 break;
     223             :         case KERNFS_LINK:
     224           0 :                 inode->i_op = &kernfs_symlink_iops;
     225           0 :                 break;
     226             :         default:
     227           0 :                 BUG();
     228             :         }
     229             : 
     230           0 :         unlock_new_inode(inode);
     231           0 : }
     232             : 
     233             : /**
     234             :  *      kernfs_get_inode - get inode for kernfs_node
     235             :  *      @sb: super block
     236             :  *      @kn: kernfs_node to allocate inode for
     237             :  *
     238             :  *      Get inode for @kn.  If such inode doesn't exist, a new inode is
     239             :  *      allocated and basics are initialized.  New inode is returned
     240             :  *      locked.
     241             :  *
     242             :  *      Locking:
     243             :  *      Kernel thread context (may sleep).
     244             :  *
     245             :  *      Return:
     246             :  *      Pointer to allocated inode on success, %NULL on failure.
     247             :  */
     248           0 : struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
     249             : {
     250             :         struct inode *inode;
     251             : 
     252           0 :         inode = iget_locked(sb, kernfs_ino(kn));
     253           0 :         if (inode && (inode->i_state & I_NEW))
     254           0 :                 kernfs_init_inode(kn, inode);
     255             : 
     256           0 :         return inode;
     257             : }
     258             : 
     259             : /*
     260             :  * The kernfs_node serves as both an inode and a directory entry for
     261             :  * kernfs.  To prevent the kernfs inode numbers from being freed
     262             :  * prematurely we take a reference to kernfs_node from the kernfs inode.  A
     263             :  * super_operations.evict_inode() implementation is needed to drop that
     264             :  * reference upon inode destruction.
     265             :  */
     266           0 : void kernfs_evict_inode(struct inode *inode)
     267             : {
     268           0 :         struct kernfs_node *kn = inode->i_private;
     269             : 
     270           0 :         truncate_inode_pages_final(&inode->i_data);
     271           0 :         clear_inode(inode);
     272           0 :         kernfs_put(kn);
     273           0 : }
     274             : 
     275           0 : int kernfs_iop_permission(struct mnt_idmap *idmap,
     276             :                           struct inode *inode, int mask)
     277             : {
     278             :         struct kernfs_node *kn;
     279             :         struct kernfs_root *root;
     280             :         int ret;
     281             : 
     282           0 :         if (mask & MAY_NOT_BLOCK)
     283             :                 return -ECHILD;
     284             : 
     285           0 :         kn = inode->i_private;
     286           0 :         root = kernfs_root(kn);
     287             : 
     288           0 :         down_read(&root->kernfs_iattr_rwsem);
     289           0 :         kernfs_refresh_inode(kn, inode);
     290           0 :         ret = generic_permission(&nop_mnt_idmap, inode, mask);
     291           0 :         up_read(&root->kernfs_iattr_rwsem);
     292             : 
     293           0 :         return ret;
     294             : }
     295             : 
     296           0 : int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
     297             :                      void *value, size_t size)
     298             : {
     299           0 :         struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
     300           0 :         if (!attrs)
     301             :                 return -ENODATA;
     302             : 
     303           0 :         return simple_xattr_get(&attrs->xattrs, name, value, size);
     304             : }
     305             : 
     306           0 : int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
     307             :                      const void *value, size_t size, int flags)
     308             : {
     309           0 :         struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
     310           0 :         if (!attrs)
     311             :                 return -ENOMEM;
     312             : 
     313           0 :         return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL);
     314             : }
     315             : 
     316           0 : static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
     317             :                                 struct dentry *unused, struct inode *inode,
     318             :                                 const char *suffix, void *value, size_t size)
     319             : {
     320           0 :         const char *name = xattr_full_name(handler, suffix);
     321           0 :         struct kernfs_node *kn = inode->i_private;
     322             : 
     323           0 :         return kernfs_xattr_get(kn, name, value, size);
     324             : }
     325             : 
     326           0 : static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
     327             :                                 struct mnt_idmap *idmap,
     328             :                                 struct dentry *unused, struct inode *inode,
     329             :                                 const char *suffix, const void *value,
     330             :                                 size_t size, int flags)
     331             : {
     332           0 :         const char *name = xattr_full_name(handler, suffix);
     333           0 :         struct kernfs_node *kn = inode->i_private;
     334             : 
     335           0 :         return kernfs_xattr_set(kn, name, value, size, flags);
     336             : }
     337             : 
     338           0 : static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
     339             :                                      const char *full_name,
     340             :                                      struct simple_xattrs *xattrs,
     341             :                                      const void *value, size_t size, int flags)
     342             : {
     343           0 :         atomic_t *sz = &kn->iattr->user_xattr_size;
     344           0 :         atomic_t *nr = &kn->iattr->nr_user_xattrs;
     345             :         ssize_t removed_size;
     346             :         int ret;
     347             : 
     348           0 :         if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
     349             :                 ret = -ENOSPC;
     350             :                 goto dec_count_out;
     351             :         }
     352             : 
     353           0 :         if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
     354             :                 ret = -ENOSPC;
     355             :                 goto dec_size_out;
     356             :         }
     357             : 
     358           0 :         ret = simple_xattr_set(xattrs, full_name, value, size, flags,
     359             :                                &removed_size);
     360             : 
     361           0 :         if (!ret && removed_size >= 0)
     362           0 :                 size = removed_size;
     363           0 :         else if (!ret)
     364             :                 return 0;
     365             : dec_size_out:
     366           0 :         atomic_sub(size, sz);
     367             : dec_count_out:
     368           0 :         atomic_dec(nr);
     369             :         return ret;
     370             : }
     371             : 
     372             : static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
     373             :                                     const char *full_name,
     374             :                                     struct simple_xattrs *xattrs,
     375             :                                     const void *value, size_t size, int flags)
     376             : {
     377           0 :         atomic_t *sz = &kn->iattr->user_xattr_size;
     378           0 :         atomic_t *nr = &kn->iattr->nr_user_xattrs;
     379             :         ssize_t removed_size;
     380             :         int ret;
     381             : 
     382           0 :         ret = simple_xattr_set(xattrs, full_name, value, size, flags,
     383             :                                &removed_size);
     384             : 
     385           0 :         if (removed_size >= 0) {
     386           0 :                 atomic_sub(removed_size, sz);
     387             :                 atomic_dec(nr);
     388             :         }
     389             : 
     390             :         return ret;
     391             : }
     392             : 
     393           0 : static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
     394             :                                      struct mnt_idmap *idmap,
     395             :                                      struct dentry *unused, struct inode *inode,
     396             :                                      const char *suffix, const void *value,
     397             :                                      size_t size, int flags)
     398             : {
     399           0 :         const char *full_name = xattr_full_name(handler, suffix);
     400           0 :         struct kernfs_node *kn = inode->i_private;
     401             :         struct kernfs_iattrs *attrs;
     402             : 
     403           0 :         if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
     404             :                 return -EOPNOTSUPP;
     405             : 
     406           0 :         attrs = kernfs_iattrs(kn);
     407           0 :         if (!attrs)
     408             :                 return -ENOMEM;
     409             : 
     410           0 :         if (value)
     411           0 :                 return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
     412             :                                                  value, size, flags);
     413             :         else
     414           0 :                 return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
     415             :                                                 value, size, flags);
     416             : 
     417             : }
     418             : 
     419             : static const struct xattr_handler kernfs_trusted_xattr_handler = {
     420             :         .prefix = XATTR_TRUSTED_PREFIX,
     421             :         .get = kernfs_vfs_xattr_get,
     422             :         .set = kernfs_vfs_xattr_set,
     423             : };
     424             : 
     425             : static const struct xattr_handler kernfs_security_xattr_handler = {
     426             :         .prefix = XATTR_SECURITY_PREFIX,
     427             :         .get = kernfs_vfs_xattr_get,
     428             :         .set = kernfs_vfs_xattr_set,
     429             : };
     430             : 
     431             : static const struct xattr_handler kernfs_user_xattr_handler = {
     432             :         .prefix = XATTR_USER_PREFIX,
     433             :         .get = kernfs_vfs_xattr_get,
     434             :         .set = kernfs_vfs_user_xattr_set,
     435             : };
     436             : 
     437             : const struct xattr_handler *kernfs_xattr_handlers[] = {
     438             :         &kernfs_trusted_xattr_handler,
     439             :         &kernfs_security_xattr_handler,
     440             :         &kernfs_user_xattr_handler,
     441             :         NULL
     442             : };

Generated by: LCOV version 1.14