LCOV - code coverage report
Current view: top level - fs/overlayfs - inode.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 153 456 33.6 %
Date: 2021-04-22 12:43:58 Functions: 14 32 43.8 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  *
       4             :  * Copyright (C) 2011 Novell Inc.
       5             :  */
       6             : 
       7             : #include <linux/fs.h>
       8             : #include <linux/slab.h>
       9             : #include <linux/cred.h>
      10             : #include <linux/xattr.h>
      11             : #include <linux/posix_acl.h>
      12             : #include <linux/ratelimit.h>
      13             : #include <linux/fiemap.h>
      14             : #include "overlayfs.h"
      15             : 
      16             : 
      17           0 : int ovl_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
      18             :                 struct iattr *attr)
      19             : {
      20           0 :         int err;
      21           0 :         bool full_copy_up = false;
      22           0 :         struct dentry *upperdentry;
      23           0 :         const struct cred *old_cred;
      24             : 
      25           0 :         err = setattr_prepare(&init_user_ns, dentry, attr);
      26           0 :         if (err)
      27             :                 return err;
      28             : 
      29           0 :         err = ovl_want_write(dentry);
      30           0 :         if (err)
      31           0 :                 goto out;
      32             : 
      33           0 :         if (attr->ia_valid & ATTR_SIZE) {
      34           0 :                 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
      35             : 
      36           0 :                 err = -ETXTBSY;
      37           0 :                 if (atomic_read(&realinode->i_writecount) < 0)
      38           0 :                         goto out_drop_write;
      39             : 
      40             :                 /* Truncate should trigger data copy up as well */
      41             :                 full_copy_up = true;
      42             :         }
      43             : 
      44           0 :         if (!full_copy_up)
      45           0 :                 err = ovl_copy_up(dentry);
      46             :         else
      47           0 :                 err = ovl_copy_up_with_data(dentry);
      48           0 :         if (!err) {
      49           0 :                 struct inode *winode = NULL;
      50             : 
      51           0 :                 upperdentry = ovl_dentry_upper(dentry);
      52             : 
      53           0 :                 if (attr->ia_valid & ATTR_SIZE) {
      54           0 :                         winode = d_inode(upperdentry);
      55           0 :                         err = get_write_access(winode);
      56           0 :                         if (err)
      57           0 :                                 goto out_drop_write;
      58             :                 }
      59             : 
      60           0 :                 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
      61           0 :                         attr->ia_valid &= ~ATTR_MODE;
      62             : 
      63             :                 /*
      64             :                  * We might have to translate ovl file into real file object
      65             :                  * once use cases emerge.  For now, simply don't let underlying
      66             :                  * filesystem rely on attr->ia_file
      67             :                  */
      68           0 :                 attr->ia_valid &= ~ATTR_FILE;
      69             : 
      70             :                 /*
      71             :                  * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
      72             :                  * set.  Overlayfs does not pass O_TRUNC flag to underlying
      73             :                  * filesystem during open -> do not pass ATTR_OPEN.  This
      74             :                  * disables optimization in fuse which assumes open(O_TRUNC)
      75             :                  * already set file size to 0.  But we never passed O_TRUNC to
      76             :                  * fuse.  So by clearing ATTR_OPEN, fuse will be forced to send
      77             :                  * setattr request to server.
      78             :                  */
      79           0 :                 attr->ia_valid &= ~ATTR_OPEN;
      80             : 
      81           0 :                 inode_lock(upperdentry->d_inode);
      82           0 :                 old_cred = ovl_override_creds(dentry->d_sb);
      83           0 :                 err = notify_change(&init_user_ns, upperdentry, attr, NULL);
      84           0 :                 revert_creds(old_cred);
      85           0 :                 if (!err)
      86           0 :                         ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
      87           0 :                 inode_unlock(upperdentry->d_inode);
      88             : 
      89           0 :                 if (winode)
      90           0 :                         put_write_access(winode);
      91             :         }
      92           0 : out_drop_write:
      93           0 :         ovl_drop_write(dentry);
      94             : out:
      95             :         return err;
      96             : }
      97             : 
      98           0 : static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
      99             : {
     100           0 :         bool samefs = ovl_same_fs(dentry->d_sb);
     101           0 :         unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
     102           0 :         unsigned int xinoshift = 64 - xinobits;
     103             : 
     104           0 :         if (samefs) {
     105             :                 /*
     106             :                  * When all layers are on the same fs, all real inode
     107             :                  * number are unique, so we use the overlay st_dev,
     108             :                  * which is friendly to du -x.
     109             :                  */
     110           0 :                 stat->dev = dentry->d_sb->s_dev;
     111           0 :                 return 0;
     112           0 :         } else if (xinobits) {
     113             :                 /*
     114             :                  * All inode numbers of underlying fs should not be using the
     115             :                  * high xinobits, so we use high xinobits to partition the
     116             :                  * overlay st_ino address space. The high bits holds the fsid
     117             :                  * (upper fsid is 0). The lowest xinobit is reserved for mapping
     118             :                  * the non-peresistent inode numbers range in case of overflow.
     119             :                  * This way all overlay inode numbers are unique and use the
     120             :                  * overlay st_dev.
     121             :                  */
     122           0 :                 if (likely(!(stat->ino >> xinoshift))) {
     123           0 :                         stat->ino |= ((u64)fsid) << (xinoshift + 1);
     124           0 :                         stat->dev = dentry->d_sb->s_dev;
     125           0 :                         return 0;
     126           0 :                 } else if (ovl_xino_warn(dentry->d_sb)) {
     127           0 :                         pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
     128             :                                             dentry, stat->ino, xinobits);
     129             :                 }
     130             :         }
     131             : 
     132             :         /* The inode could not be mapped to a unified st_ino address space */
     133           0 :         if (S_ISDIR(dentry->d_inode->i_mode)) {
     134             :                 /*
     135             :                  * Always use the overlay st_dev for directories, so 'find
     136             :                  * -xdev' will scan the entire overlay mount and won't cross the
     137             :                  * overlay mount boundaries.
     138             :                  *
     139             :                  * If not all layers are on the same fs the pair {real st_ino;
     140             :                  * overlay st_dev} is not unique, so use the non persistent
     141             :                  * overlay st_ino for directories.
     142             :                  */
     143           0 :                 stat->dev = dentry->d_sb->s_dev;
     144           0 :                 stat->ino = dentry->d_inode->i_ino;
     145             :         } else {
     146             :                 /*
     147             :                  * For non-samefs setup, if we cannot map all layers st_ino
     148             :                  * to a unified address space, we need to make sure that st_dev
     149             :                  * is unique per underlying fs, so we use the unique anonymous
     150             :                  * bdev assigned to the underlying fs.
     151             :                  */
     152           0 :                 stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev;
     153             :         }
     154             : 
     155             :         return 0;
     156             : }
     157             : 
     158           0 : int ovl_getattr(struct user_namespace *mnt_userns, const struct path *path,
     159             :                 struct kstat *stat, u32 request_mask, unsigned int flags)
     160             : {
     161           0 :         struct dentry *dentry = path->dentry;
     162           0 :         enum ovl_path_type type;
     163           0 :         struct path realpath;
     164           0 :         const struct cred *old_cred;
     165           0 :         bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
     166           0 :         int fsid = 0;
     167           0 :         int err;
     168           0 :         bool metacopy_blocks = false;
     169             : 
     170           0 :         metacopy_blocks = ovl_is_metacopy_dentry(dentry);
     171             : 
     172           0 :         type = ovl_path_real(dentry, &realpath);
     173           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     174           0 :         err = vfs_getattr(&realpath, stat, request_mask, flags);
     175           0 :         if (err)
     176           0 :                 goto out;
     177             : 
     178             :         /*
     179             :          * For non-dir or same fs, we use st_ino of the copy up origin.
     180             :          * This guaranties constant st_dev/st_ino across copy up.
     181             :          * With xino feature and non-samefs, we use st_ino of the copy up
     182             :          * origin masked with high bits that represent the layer id.
     183             :          *
     184             :          * If lower filesystem supports NFS file handles, this also guaranties
     185             :          * persistent st_ino across mount cycle.
     186             :          */
     187           0 :         if (!is_dir || ovl_same_dev(dentry->d_sb)) {
     188           0 :                 if (!OVL_TYPE_UPPER(type)) {
     189           0 :                         fsid = ovl_layer_lower(dentry)->fsid;
     190           0 :                 } else if (OVL_TYPE_ORIGIN(type)) {
     191           0 :                         struct kstat lowerstat;
     192           0 :                         u32 lowermask = STATX_INO | STATX_BLOCKS |
     193             :                                         (!is_dir ? STATX_NLINK : 0);
     194             : 
     195           0 :                         ovl_path_lower(dentry, &realpath);
     196           0 :                         err = vfs_getattr(&realpath, &lowerstat,
     197             :                                           lowermask, flags);
     198           0 :                         if (err)
     199           0 :                                 goto out;
     200             : 
     201             :                         /*
     202             :                          * Lower hardlinks may be broken on copy up to different
     203             :                          * upper files, so we cannot use the lower origin st_ino
     204             :                          * for those different files, even for the same fs case.
     205             :                          *
     206             :                          * Similarly, several redirected dirs can point to the
     207             :                          * same dir on a lower layer. With the "verify_lower"
     208             :                          * feature, we do not use the lower origin st_ino, if
     209             :                          * we haven't verified that this redirect is unique.
     210             :                          *
     211             :                          * With inodes index enabled, it is safe to use st_ino
     212             :                          * of an indexed origin. The index validates that the
     213             :                          * upper hardlink is not broken and that a redirected
     214             :                          * dir is the only redirect to that origin.
     215             :                          */
     216           0 :                         if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
     217           0 :                             (!ovl_verify_lower(dentry->d_sb) &&
     218           0 :                              (is_dir || lowerstat.nlink == 1))) {
     219           0 :                                 fsid = ovl_layer_lower(dentry)->fsid;
     220           0 :                                 stat->ino = lowerstat.ino;
     221             :                         }
     222             : 
     223             :                         /*
     224             :                          * If we are querying a metacopy dentry and lower
     225             :                          * dentry is data dentry, then use the blocks we
     226             :                          * queried just now. We don't have to do additional
     227             :                          * vfs_getattr(). If lower itself is metacopy, then
     228             :                          * additional vfs_getattr() is unavoidable.
     229             :                          */
     230           0 :                         if (metacopy_blocks &&
     231           0 :                             realpath.dentry == ovl_dentry_lowerdata(dentry)) {
     232           0 :                                 stat->blocks = lowerstat.blocks;
     233           0 :                                 metacopy_blocks = false;
     234             :                         }
     235             :                 }
     236             : 
     237           0 :                 if (metacopy_blocks) {
     238             :                         /*
     239             :                          * If lower is not same as lowerdata or if there was
     240             :                          * no origin on upper, we can end up here.
     241             :                          */
     242           0 :                         struct kstat lowerdatastat;
     243           0 :                         u32 lowermask = STATX_BLOCKS;
     244             : 
     245           0 :                         ovl_path_lowerdata(dentry, &realpath);
     246           0 :                         err = vfs_getattr(&realpath, &lowerdatastat,
     247             :                                           lowermask, flags);
     248           0 :                         if (err)
     249           0 :                                 goto out;
     250           0 :                         stat->blocks = lowerdatastat.blocks;
     251             :                 }
     252             :         }
     253             : 
     254           0 :         err = ovl_map_dev_ino(dentry, stat, fsid);
     255           0 :         if (err)
     256           0 :                 goto out;
     257             : 
     258             :         /*
     259             :          * It's probably not worth it to count subdirs to get the
     260             :          * correct link count.  nlink=1 seems to pacify 'find' and
     261             :          * other utilities.
     262             :          */
     263           0 :         if (is_dir && OVL_TYPE_MERGE(type))
     264           0 :                 stat->nlink = 1;
     265             : 
     266             :         /*
     267             :          * Return the overlay inode nlinks for indexed upper inodes.
     268             :          * Overlay inode nlink counts the union of the upper hardlinks
     269             :          * and non-covered lower hardlinks. It does not include the upper
     270             :          * index hardlink.
     271             :          */
     272           0 :         if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
     273           0 :                 stat->nlink = dentry->d_inode->i_nlink;
     274             : 
     275           0 : out:
     276           0 :         revert_creds(old_cred);
     277             : 
     278           0 :         return err;
     279             : }
     280             : 
     281         175 : int ovl_permission(struct user_namespace *mnt_userns,
     282             :                    struct inode *inode, int mask)
     283             : {
     284         175 :         struct inode *upperinode = ovl_inode_upper(inode);
     285         175 :         struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
     286         175 :         const struct cred *old_cred;
     287         175 :         int err;
     288             : 
     289             :         /* Careful in RCU walk mode */
     290         175 :         if (!realinode) {
     291           0 :                 WARN_ON(!(mask & MAY_NOT_BLOCK));
     292             :                 return -ECHILD;
     293             :         }
     294             : 
     295             :         /*
     296             :          * Check overlay inode with the creds of task and underlying inode
     297             :          * with creds of mounter
     298             :          */
     299         175 :         err = generic_permission(&init_user_ns, inode, mask);
     300         175 :         if (err)
     301             :                 return err;
     302             : 
     303         175 :         old_cred = ovl_override_creds(inode->i_sb);
     304         175 :         if (!upperinode &&
     305          10 :             !special_file(realinode->i_mode) && mask & MAY_WRITE) {
     306           3 :                 mask &= ~(MAY_WRITE | MAY_APPEND);
     307             :                 /* Make sure mounter can read file for copy up later */
     308           3 :                 mask |= MAY_READ;
     309             :         }
     310         175 :         err = inode_permission(&init_user_ns, realinode, mask);
     311         175 :         revert_creds(old_cred);
     312             : 
     313         175 :         return err;
     314             : }
     315             : 
     316           0 : static const char *ovl_get_link(struct dentry *dentry,
     317             :                                 struct inode *inode,
     318             :                                 struct delayed_call *done)
     319             : {
     320           0 :         const struct cred *old_cred;
     321           0 :         const char *p;
     322             : 
     323           0 :         if (!dentry)
     324           0 :                 return ERR_PTR(-ECHILD);
     325             : 
     326           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     327           0 :         p = vfs_get_link(ovl_dentry_real(dentry), done);
     328           0 :         revert_creds(old_cred);
     329           0 :         return p;
     330             : }
     331             : 
     332           0 : bool ovl_is_private_xattr(struct super_block *sb, const char *name)
     333             : {
     334           0 :         struct ovl_fs *ofs = sb->s_fs_info;
     335             : 
     336           0 :         if (ofs->config.userxattr)
     337           0 :                 return strncmp(name, OVL_XATTR_USER_PREFIX,
     338           0 :                                sizeof(OVL_XATTR_USER_PREFIX) - 1) == 0;
     339             :         else
     340           0 :                 return strncmp(name, OVL_XATTR_TRUSTED_PREFIX,
     341           0 :                                sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1) == 0;
     342             : }
     343             : 
     344           0 : int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
     345             :                   const void *value, size_t size, int flags)
     346             : {
     347           0 :         int err;
     348           0 :         struct dentry *upperdentry = ovl_i_dentry_upper(inode);
     349           0 :         struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
     350           0 :         const struct cred *old_cred;
     351             : 
     352           0 :         err = ovl_want_write(dentry);
     353           0 :         if (err)
     354           0 :                 goto out;
     355             : 
     356           0 :         if (!value && !upperdentry) {
     357           0 :                 old_cred = ovl_override_creds(dentry->d_sb);
     358           0 :                 err = vfs_getxattr(&init_user_ns, realdentry, name, NULL, 0);
     359           0 :                 revert_creds(old_cred);
     360           0 :                 if (err < 0)
     361           0 :                         goto out_drop_write;
     362             :         }
     363             : 
     364           0 :         if (!upperdentry) {
     365           0 :                 err = ovl_copy_up(dentry);
     366           0 :                 if (err)
     367           0 :                         goto out_drop_write;
     368             : 
     369           0 :                 realdentry = ovl_dentry_upper(dentry);
     370             :         }
     371             : 
     372           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     373           0 :         if (value)
     374           0 :                 err = vfs_setxattr(&init_user_ns, realdentry, name, value, size,
     375             :                                    flags);
     376             :         else {
     377           0 :                 WARN_ON(flags != XATTR_REPLACE);
     378           0 :                 err = vfs_removexattr(&init_user_ns, realdentry, name);
     379             :         }
     380           0 :         revert_creds(old_cred);
     381             : 
     382             :         /* copy c/mtime */
     383           0 :         ovl_copyattr(d_inode(realdentry), inode);
     384             : 
     385           0 : out_drop_write:
     386           0 :         ovl_drop_write(dentry);
     387           0 : out:
     388           0 :         return err;
     389             : }
     390             : 
     391           0 : int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
     392             :                   void *value, size_t size)
     393             : {
     394           0 :         ssize_t res;
     395           0 :         const struct cred *old_cred;
     396           0 :         struct dentry *realdentry =
     397           0 :                 ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
     398             : 
     399           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     400           0 :         res = vfs_getxattr(&init_user_ns, realdentry, name, value, size);
     401           0 :         revert_creds(old_cred);
     402           0 :         return res;
     403             : }
     404             : 
     405           0 : static bool ovl_can_list(struct super_block *sb, const char *s)
     406             : {
     407             :         /* Never list private (.overlay) */
     408           0 :         if (ovl_is_private_xattr(sb, s))
     409             :                 return false;
     410             : 
     411             :         /* List all non-trusted xatts */
     412           0 :         if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
     413             :                 return true;
     414             : 
     415             :         /* list other trusted for superuser only */
     416           0 :         return ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
     417             : }
     418             : 
     419           0 : ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
     420             : {
     421           0 :         struct dentry *realdentry = ovl_dentry_real(dentry);
     422           0 :         ssize_t res;
     423           0 :         size_t len;
     424           0 :         char *s;
     425           0 :         const struct cred *old_cred;
     426             : 
     427           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     428           0 :         res = vfs_listxattr(realdentry, list, size);
     429           0 :         revert_creds(old_cred);
     430           0 :         if (res <= 0 || size == 0)
     431             :                 return res;
     432             : 
     433             :         /* filter out private xattrs */
     434           0 :         for (s = list, len = res; len;) {
     435           0 :                 size_t slen = strnlen(s, len) + 1;
     436             : 
     437             :                 /* underlying fs providing us with an broken xattr list? */
     438           0 :                 if (WARN_ON(slen > len))
     439             :                         return -EIO;
     440             : 
     441           0 :                 len -= slen;
     442           0 :                 if (!ovl_can_list(dentry->d_sb, s)) {
     443           0 :                         res -= slen;
     444           0 :                         memmove(s, s + slen, len);
     445             :                 } else {
     446           0 :                         s += slen;
     447             :                 }
     448             :         }
     449             : 
     450             :         return res;
     451             : }
     452             : 
     453           0 : struct posix_acl *ovl_get_acl(struct inode *inode, int type)
     454             : {
     455           0 :         struct inode *realinode = ovl_inode_real(inode);
     456           0 :         const struct cred *old_cred;
     457           0 :         struct posix_acl *acl;
     458             : 
     459           0 :         if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
     460           0 :                 return NULL;
     461             : 
     462             :         old_cred = ovl_override_creds(inode->i_sb);
     463             :         acl = get_acl(realinode, type);
     464             :         revert_creds(old_cred);
     465             : 
     466             :         return acl;
     467             : }
     468             : 
     469           0 : int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
     470             : {
     471           0 :         if (flags & S_ATIME) {
     472           0 :                 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
     473           0 :                 struct path upperpath = {
     474           0 :                         .mnt = ovl_upper_mnt(ofs),
     475           0 :                         .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
     476             :                 };
     477             : 
     478           0 :                 if (upperpath.dentry) {
     479           0 :                         touch_atime(&upperpath);
     480           0 :                         inode->i_atime = d_inode(upperpath.dentry)->i_atime;
     481             :                 }
     482             :         }
     483           0 :         return 0;
     484             : }
     485             : 
     486           0 : static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
     487             :                       u64 start, u64 len)
     488             : {
     489           0 :         int err;
     490           0 :         struct inode *realinode = ovl_inode_realdata(inode);
     491           0 :         const struct cred *old_cred;
     492             : 
     493           0 :         if (!realinode->i_op->fiemap)
     494             :                 return -EOPNOTSUPP;
     495             : 
     496           0 :         old_cred = ovl_override_creds(inode->i_sb);
     497           0 :         err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
     498           0 :         revert_creds(old_cred);
     499             : 
     500           0 :         return err;
     501             : }
     502             : 
     503             : static const struct inode_operations ovl_file_inode_operations = {
     504             :         .setattr        = ovl_setattr,
     505             :         .permission     = ovl_permission,
     506             :         .getattr        = ovl_getattr,
     507             :         .listxattr      = ovl_listxattr,
     508             :         .get_acl        = ovl_get_acl,
     509             :         .update_time    = ovl_update_time,
     510             :         .fiemap         = ovl_fiemap,
     511             : };
     512             : 
     513             : static const struct inode_operations ovl_symlink_inode_operations = {
     514             :         .setattr        = ovl_setattr,
     515             :         .get_link       = ovl_get_link,
     516             :         .getattr        = ovl_getattr,
     517             :         .listxattr      = ovl_listxattr,
     518             :         .update_time    = ovl_update_time,
     519             : };
     520             : 
     521             : static const struct inode_operations ovl_special_inode_operations = {
     522             :         .setattr        = ovl_setattr,
     523             :         .permission     = ovl_permission,
     524             :         .getattr        = ovl_getattr,
     525             :         .listxattr      = ovl_listxattr,
     526             :         .get_acl        = ovl_get_acl,
     527             :         .update_time    = ovl_update_time,
     528             : };
     529             : 
     530             : static const struct address_space_operations ovl_aops = {
     531             :         /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
     532             :         .direct_IO              = noop_direct_IO,
     533             : };
     534             : 
     535             : /*
     536             :  * It is possible to stack overlayfs instance on top of another
     537             :  * overlayfs instance as lower layer. We need to annotate the
     538             :  * stackable i_mutex locks according to stack level of the super
     539             :  * block instance. An overlayfs instance can never be in stack
     540             :  * depth 0 (there is always a real fs below it).  An overlayfs
     541             :  * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
     542             :  *
     543             :  * For example, here is a snip from /proc/lockdep_chains after
     544             :  * dir_iterate of nested overlayfs:
     545             :  *
     546             :  * [...] &ovl_i_mutex_dir_key[depth]   (stack_depth=2)
     547             :  * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
     548             :  * [...] &type->i_mutex_dir_key        (stack_depth=0)
     549             :  *
     550             :  * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
     551             :  *
     552             :  * This chain is valid:
     553             :  * - inode->i_rwsem                  (inode_lock[2])
     554             :  * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
     555             :  * - OVL_I(inode)->lock                      (ovl_inode_lock[2])
     556             :  * - OVL_I(lowerinode)->lock         (ovl_inode_lock[1])
     557             :  *
     558             :  * And this chain is valid:
     559             :  * - inode->i_rwsem                  (inode_lock[2])
     560             :  * - OVL_I(inode)->lock                      (ovl_inode_lock[2])
     561             :  * - lowerinode->i_rwsem             (inode_lock[1])
     562             :  * - OVL_I(lowerinode)->lock         (ovl_inode_lock[1])
     563             :  *
     564             :  * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
     565             :  * held, because it is in reverse order of the non-nested case using the same
     566             :  * upper fs:
     567             :  * - inode->i_rwsem                  (inode_lock[1])
     568             :  * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
     569             :  * - OVL_I(inode)->lock                      (ovl_inode_lock[1])
     570             :  */
     571             : #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
     572             : 
     573          24 : static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
     574             : {
     575             : #ifdef CONFIG_LOCKDEP
     576          24 :         static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
     577          24 :         static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
     578          24 :         static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
     579             : 
     580          24 :         int depth = inode->i_sb->s_stack_depth - 1;
     581             : 
     582          24 :         if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
     583           0 :                 depth = 0;
     584             : 
     585          24 :         if (S_ISDIR(inode->i_mode))
     586           8 :                 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
     587             :         else
     588          16 :                 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
     589             : 
     590          24 :         lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
     591             : #endif
     592          24 : }
     593             : 
     594           8 : static void ovl_next_ino(struct inode *inode)
     595             : {
     596           8 :         struct ovl_fs *ofs = inode->i_sb->s_fs_info;
     597             : 
     598           8 :         inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
     599           8 :         if (unlikely(!inode->i_ino))
     600           0 :                 inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
     601           8 : }
     602             : 
     603          24 : static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid)
     604             : {
     605          24 :         int xinobits = ovl_xino_bits(inode->i_sb);
     606          24 :         unsigned int xinoshift = 64 - xinobits;
     607             : 
     608             :         /*
     609             :          * When d_ino is consistent with st_ino (samefs or i_ino has enough
     610             :          * bits to encode layer), set the same value used for st_ino to i_ino,
     611             :          * so inode number exposed via /proc/locks and a like will be
     612             :          * consistent with d_ino and st_ino values. An i_ino value inconsistent
     613             :          * with d_ino also causes nfsd readdirplus to fail.
     614             :          */
     615          24 :         inode->i_ino = ino;
     616          24 :         if (ovl_same_fs(inode->i_sb)) {
     617             :                 return;
     618          24 :         } else if (xinobits && likely(!(ino >> xinoshift))) {
     619           0 :                 inode->i_ino |= (unsigned long)fsid << (xinoshift + 1);
     620           0 :                 return;
     621             :         }
     622             : 
     623             :         /*
     624             :          * For directory inodes on non-samefs with xino disabled or xino
     625             :          * overflow, we allocate a non-persistent inode number, to be used for
     626             :          * resolving st_ino collisions in ovl_map_dev_ino().
     627             :          *
     628             :          * To avoid ino collision with legitimate xino values from upper
     629             :          * layer (fsid 0), use the lowest xinobit to map the non
     630             :          * persistent inode numbers to the unified st_ino address space.
     631             :          */
     632          24 :         if (S_ISDIR(inode->i_mode)) {
     633           8 :                 ovl_next_ino(inode);
     634           8 :                 if (xinobits) {
     635           0 :                         inode->i_ino &= ~0UL >> xinobits;
     636           0 :                         inode->i_ino |= 1UL << xinoshift;
     637             :                 }
     638             :         }
     639             : }
     640             : 
     641          24 : void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
     642             :                     unsigned long ino, int fsid)
     643             : {
     644          24 :         struct inode *realinode;
     645             : 
     646          24 :         if (oip->upperdentry)
     647          16 :                 OVL_I(inode)->__upperdentry = oip->upperdentry;
     648          24 :         if (oip->lowerpath && oip->lowerpath->dentry)
     649          12 :                 OVL_I(inode)->lower = igrab(d_inode(oip->lowerpath->dentry));
     650          24 :         if (oip->lowerdata)
     651           0 :                 OVL_I(inode)->lowerdata = igrab(d_inode(oip->lowerdata));
     652             : 
     653          24 :         realinode = ovl_inode_real(inode);
     654          24 :         ovl_copyattr(realinode, inode);
     655          24 :         ovl_copyflags(realinode, inode);
     656          24 :         ovl_map_ino(inode, ino, fsid);
     657          24 : }
     658             : 
     659          24 : static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
     660             : {
     661          24 :         inode->i_mode = mode;
     662          24 :         inode->i_flags |= S_NOCMTIME;
     663             : #ifdef CONFIG_FS_POSIX_ACL
     664             :         inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
     665             : #endif
     666             : 
     667          24 :         ovl_lockdep_annotate_inode_mutex_key(inode);
     668             : 
     669          24 :         switch (mode & S_IFMT) {
     670          16 :         case S_IFREG:
     671          16 :                 inode->i_op = &ovl_file_inode_operations;
     672          16 :                 inode->i_fop = &ovl_file_operations;
     673          16 :                 inode->i_mapping->a_ops = &ovl_aops;
     674          16 :                 break;
     675             : 
     676           8 :         case S_IFDIR:
     677           8 :                 inode->i_op = &ovl_dir_inode_operations;
     678           8 :                 inode->i_fop = &ovl_dir_operations;
     679           8 :                 break;
     680             : 
     681           0 :         case S_IFLNK:
     682           0 :                 inode->i_op = &ovl_symlink_inode_operations;
     683           0 :                 break;
     684             : 
     685           0 :         default:
     686           0 :                 inode->i_op = &ovl_special_inode_operations;
     687           0 :                 init_special_inode(inode, mode, rdev);
     688           0 :                 break;
     689             :         }
     690          24 : }
     691             : 
     692             : /*
     693             :  * With inodes index enabled, an overlay inode nlink counts the union of upper
     694             :  * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
     695             :  * upper inode, the following nlink modifying operations can happen:
     696             :  *
     697             :  * 1. Lower hardlink copy up
     698             :  * 2. Upper hardlink created, unlinked or renamed over
     699             :  * 3. Lower hardlink whiteout or renamed over
     700             :  *
     701             :  * For the first, copy up case, the union nlink does not change, whether the
     702             :  * operation succeeds or fails, but the upper inode nlink may change.
     703             :  * Therefore, before copy up, we store the union nlink value relative to the
     704             :  * lower inode nlink in the index inode xattr .overlay.nlink.
     705             :  *
     706             :  * For the second, upper hardlink case, the union nlink should be incremented
     707             :  * or decremented IFF the operation succeeds, aligned with nlink change of the
     708             :  * upper inode. Therefore, before link/unlink/rename, we store the union nlink
     709             :  * value relative to the upper inode nlink in the index inode.
     710             :  *
     711             :  * For the last, lower cover up case, we simplify things by preceding the
     712             :  * whiteout or cover up with copy up. This makes sure that there is an index
     713             :  * upper inode where the nlink xattr can be stored before the copied up upper
     714             :  * entry is unlink.
     715             :  */
     716             : #define OVL_NLINK_ADD_UPPER     (1 << 0)
     717             : 
     718             : /*
     719             :  * On-disk format for indexed nlink:
     720             :  *
     721             :  * nlink relative to the upper inode - "U[+-]NUM"
     722             :  * nlink relative to the lower inode - "L[+-]NUM"
     723             :  */
     724             : 
     725           0 : static int ovl_set_nlink_common(struct dentry *dentry,
     726             :                                 struct dentry *realdentry, const char *format)
     727             : {
     728           0 :         struct inode *inode = d_inode(dentry);
     729           0 :         struct inode *realinode = d_inode(realdentry);
     730           0 :         char buf[13];
     731           0 :         int len;
     732             : 
     733           0 :         len = snprintf(buf, sizeof(buf), format,
     734           0 :                        (int) (inode->i_nlink - realinode->i_nlink));
     735             : 
     736           0 :         if (WARN_ON(len >= sizeof(buf)))
     737             :                 return -EIO;
     738             : 
     739           0 :         return ovl_do_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry),
     740             :                                OVL_XATTR_NLINK, buf, len);
     741             : }
     742             : 
     743           0 : int ovl_set_nlink_upper(struct dentry *dentry)
     744             : {
     745           0 :         return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
     746             : }
     747             : 
     748           0 : int ovl_set_nlink_lower(struct dentry *dentry)
     749             : {
     750           0 :         return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
     751             : }
     752             : 
     753          16 : unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
     754             :                            struct dentry *upperdentry,
     755             :                            unsigned int fallback)
     756             : {
     757          16 :         int nlink_diff;
     758          16 :         int nlink;
     759          16 :         char buf[13];
     760          16 :         int err;
     761             : 
     762          16 :         if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
     763             :                 return fallback;
     764             : 
     765           0 :         err = ovl_do_getxattr(ofs, upperdentry, OVL_XATTR_NLINK,
     766             :                               &buf, sizeof(buf) - 1);
     767           0 :         if (err < 0)
     768           0 :                 goto fail;
     769             : 
     770           0 :         buf[err] = '\0';
     771           0 :         if ((buf[0] != 'L' && buf[0] != 'U') ||
     772           0 :             (buf[1] != '+' && buf[1] != '-'))
     773           0 :                 goto fail;
     774             : 
     775           0 :         err = kstrtoint(buf + 1, 10, &nlink_diff);
     776           0 :         if (err < 0)
     777           0 :                 goto fail;
     778             : 
     779           0 :         nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
     780           0 :         nlink += nlink_diff;
     781             : 
     782           0 :         if (nlink <= 0)
     783           0 :                 goto fail;
     784             : 
     785           0 :         return nlink;
     786             : 
     787           0 : fail:
     788           0 :         pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
     789             :                             upperdentry, err);
     790             :         return fallback;
     791             : }
     792             : 
     793           2 : struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
     794             : {
     795           2 :         struct inode *inode;
     796             : 
     797           2 :         inode = new_inode(sb);
     798           2 :         if (inode)
     799           2 :                 ovl_fill_inode(inode, mode, rdev);
     800             : 
     801           2 :         return inode;
     802             : }
     803             : 
     804           0 : static int ovl_inode_test(struct inode *inode, void *data)
     805             : {
     806           0 :         return inode->i_private == data;
     807             : }
     808             : 
     809          30 : static int ovl_inode_set(struct inode *inode, void *data)
     810             : {
     811          30 :         inode->i_private = data;
     812          30 :         return 0;
     813             : }
     814             : 
     815           0 : static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
     816             :                              struct dentry *upperdentry, bool strict)
     817             : {
     818             :         /*
     819             :          * For directories, @strict verify from lookup path performs consistency
     820             :          * checks, so NULL lower/upper in dentry must match NULL lower/upper in
     821             :          * inode. Non @strict verify from NFS handle decode path passes NULL for
     822             :          * 'unknown' lower/upper.
     823             :          */
     824           0 :         if (S_ISDIR(inode->i_mode) && strict) {
     825             :                 /* Real lower dir moved to upper layer under us? */
     826           0 :                 if (!lowerdentry && ovl_inode_lower(inode))
     827             :                         return false;
     828             : 
     829             :                 /* Lookup of an uncovered redirect origin? */
     830           0 :                 if (!upperdentry && ovl_inode_upper(inode))
     831             :                         return false;
     832             :         }
     833             : 
     834             :         /*
     835             :          * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
     836             :          * This happens when finding a copied up overlay inode for a renamed
     837             :          * or hardlinked overlay dentry and lower dentry cannot be followed
     838             :          * by origin because lower fs does not support file handles.
     839             :          */
     840           0 :         if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
     841             :                 return false;
     842             : 
     843             :         /*
     844             :          * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
     845             :          * This happens when finding a lower alias for a copied up hard link.
     846             :          */
     847           0 :         if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
     848           0 :                 return false;
     849             : 
     850             :         return true;
     851             : }
     852             : 
     853           0 : struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
     854             :                                bool is_upper)
     855             : {
     856           0 :         struct inode *inode, *key = d_inode(real);
     857             : 
     858           0 :         inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
     859           0 :         if (!inode)
     860             :                 return NULL;
     861             : 
     862           0 :         if (!ovl_verify_inode(inode, is_upper ? NULL : real,
     863             :                               is_upper ? real : NULL, false)) {
     864           0 :                 iput(inode);
     865           0 :                 return ERR_PTR(-ESTALE);
     866             :         }
     867             : 
     868             :         return inode;
     869             : }
     870             : 
     871          14 : bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
     872             : {
     873          14 :         struct inode *key = d_inode(dir);
     874          14 :         struct inode *trap;
     875          14 :         bool res;
     876             : 
     877          14 :         trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
     878          14 :         if (!trap)
     879             :                 return false;
     880             : 
     881           0 :         res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
     882           0 :                                   !ovl_inode_lower(trap);
     883             : 
     884           0 :         iput(trap);
     885           0 :         return res;
     886             : }
     887             : 
     888             : /*
     889             :  * Create an inode cache entry for layer root dir, that will intentionally
     890             :  * fail ovl_verify_inode(), so any lookup that will find some layer root
     891             :  * will fail.
     892             :  */
     893           8 : struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
     894             : {
     895           8 :         struct inode *key = d_inode(dir);
     896           8 :         struct inode *trap;
     897             : 
     898           8 :         if (!d_is_dir(dir))
     899           8 :                 return ERR_PTR(-ENOTDIR);
     900             : 
     901           8 :         trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
     902             :                             ovl_inode_set, key);
     903           8 :         if (!trap)
     904           8 :                 return ERR_PTR(-ENOMEM);
     905             : 
     906           8 :         if (!(trap->i_state & I_NEW)) {
     907             :                 /* Conflicting layer roots? */
     908           0 :                 iput(trap);
     909           0 :                 return ERR_PTR(-ELOOP);
     910             :         }
     911             : 
     912           8 :         trap->i_mode = S_IFDIR;
     913           8 :         trap->i_flags = S_DEAD;
     914           8 :         unlock_new_inode(trap);
     915             : 
     916           8 :         return trap;
     917             : }
     918             : 
     919             : /*
     920             :  * Does overlay inode need to be hashed by lower inode?
     921             :  */
     922          22 : static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
     923             :                              struct dentry *lower, bool index)
     924             : {
     925          22 :         struct ovl_fs *ofs = sb->s_fs_info;
     926             : 
     927             :         /* No, if pure upper */
     928          22 :         if (!lower)
     929             :                 return false;
     930             : 
     931             :         /* Yes, if already indexed */
     932          10 :         if (index)
     933             :                 return true;
     934             : 
     935             :         /* Yes, if won't be copied up */
     936          10 :         if (!ovl_upper_mnt(ofs))
     937             :                 return true;
     938             : 
     939             :         /* No, if lower hardlink is or will be broken on copy up */
     940          10 :         if ((upper || !ovl_indexdir(sb)) &&
     941          10 :             !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
     942             :                 return false;
     943             : 
     944             :         /* No, if non-indexed upper with NFS export */
     945          10 :         if (sb->s_export_op && upper)
     946           0 :                 return false;
     947             : 
     948             :         /* Otherwise, hash by lower inode for fsnotify */
     949             :         return true;
     950             : }
     951             : 
     952          22 : static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
     953             :                                struct inode *key)
     954             : {
     955           0 :         return newinode ? inode_insert5(newinode, (unsigned long) key,
     956          22 :                                          ovl_inode_test, ovl_inode_set, key) :
     957          22 :                           iget5_locked(sb, (unsigned long) key,
     958             :                                        ovl_inode_test, ovl_inode_set, key);
     959             : }
     960             : 
     961          22 : struct inode *ovl_get_inode(struct super_block *sb,
     962             :                             struct ovl_inode_params *oip)
     963             : {
     964          22 :         struct ovl_fs *ofs = OVL_FS(sb);
     965          22 :         struct dentry *upperdentry = oip->upperdentry;
     966          22 :         struct ovl_path *lowerpath = oip->lowerpath;
     967          22 :         struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
     968          22 :         struct inode *inode;
     969          22 :         struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
     970          44 :         bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
     971          22 :                                         oip->index);
     972          22 :         int fsid = bylower ? lowerpath->layer->fsid : 0;
     973          22 :         bool is_dir;
     974          22 :         unsigned long ino = 0;
     975          22 :         int err = oip->newinode ? -EEXIST : -ENOMEM;
     976             : 
     977          22 :         if (!realinode)
     978           8 :                 realinode = d_inode(lowerdentry);
     979             : 
     980             :         /*
     981             :          * Copy up origin (lower) may exist for non-indexed upper, but we must
     982             :          * not use lower as hash key if this is a broken hardlink.
     983             :          */
     984          22 :         is_dir = S_ISDIR(realinode->i_mode);
     985          22 :         if (upperdentry || bylower) {
     986          22 :                 struct inode *key = d_inode(bylower ? lowerdentry :
     987             :                                                       upperdentry);
     988          22 :                 unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
     989             : 
     990          22 :                 inode = ovl_iget5(sb, oip->newinode, key);
     991          22 :                 if (!inode)
     992           0 :                         goto out_err;
     993          22 :                 if (!(inode->i_state & I_NEW)) {
     994             :                         /*
     995             :                          * Verify that the underlying files stored in the inode
     996             :                          * match those in the dentry.
     997             :                          */
     998           0 :                         if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
     999             :                                               true)) {
    1000           0 :                                 iput(inode);
    1001           0 :                                 err = -ESTALE;
    1002           0 :                                 goto out_err;
    1003             :                         }
    1004             : 
    1005           0 :                         dput(upperdentry);
    1006           0 :                         kfree(oip->redirect);
    1007           0 :                         goto out;
    1008             :                 }
    1009             : 
    1010             :                 /* Recalculate nlink for non-dir due to indexing */
    1011          22 :                 if (!is_dir)
    1012          16 :                         nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry,
    1013             :                                               nlink);
    1014          22 :                 set_nlink(inode, nlink);
    1015          22 :                 ino = key->i_ino;
    1016             :         } else {
    1017             :                 /* Lower hardlink that will be broken on copy up */
    1018           0 :                 inode = new_inode(sb);
    1019           0 :                 if (!inode) {
    1020           0 :                         err = -ENOMEM;
    1021           0 :                         goto out_err;
    1022             :                 }
    1023           0 :                 ino = realinode->i_ino;
    1024           0 :                 fsid = lowerpath->layer->fsid;
    1025             :         }
    1026          22 :         ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
    1027          22 :         ovl_inode_init(inode, oip, ino, fsid);
    1028             : 
    1029          36 :         if (upperdentry && ovl_is_impuredir(sb, upperdentry))
    1030           0 :                 ovl_set_flag(OVL_IMPURE, inode);
    1031             : 
    1032          22 :         if (oip->index)
    1033           0 :                 ovl_set_flag(OVL_INDEX, inode);
    1034             : 
    1035          22 :         OVL_I(inode)->redirect = oip->redirect;
    1036             : 
    1037          22 :         if (bylower)
    1038          10 :                 ovl_set_flag(OVL_CONST_INO, inode);
    1039             : 
    1040             :         /* Check for non-merge dir that may have whiteouts */
    1041          22 :         if (is_dir) {
    1042          10 :                 if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
    1043           6 :                     ovl_check_origin_xattr(ofs, upperdentry ?: lowerdentry)) {
    1044           2 :                         ovl_set_flag(OVL_WHITEOUTS, inode);
    1045             :                 }
    1046             :         }
    1047             : 
    1048          22 :         if (inode->i_state & I_NEW)
    1049          22 :                 unlock_new_inode(inode);
    1050           0 : out:
    1051          22 :         return inode;
    1052             : 
    1053           0 : out_err:
    1054           0 :         pr_warn_ratelimited("failed to get inode (%i)\n", err);
    1055           0 :         inode = ERR_PTR(err);
    1056           0 :         goto out;
    1057             : }

Generated by: LCOV version 1.14