LCOV - code coverage report
Current view: top level - fs/overlayfs - util.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 235 430 54.7 %
Date: 2021-04-22 12:43:58 Functions: 50 74 67.6 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-only
       2             : /*
       3             :  * Copyright (C) 2011 Novell Inc.
       4             :  * Copyright (C) 2016 Red Hat, Inc.
       5             :  */
       6             : 
       7             : #include <linux/fs.h>
       8             : #include <linux/mount.h>
       9             : #include <linux/slab.h>
      10             : #include <linux/cred.h>
      11             : #include <linux/xattr.h>
      12             : #include <linux/exportfs.h>
      13             : #include <linux/uuid.h>
      14             : #include <linux/namei.h>
      15             : #include <linux/ratelimit.h>
      16             : #include "overlayfs.h"
      17             : 
      18           5 : int ovl_want_write(struct dentry *dentry)
      19             : {
      20           5 :         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
      21           5 :         return mnt_want_write(ovl_upper_mnt(ofs));
      22             : }
      23             : 
      24           5 : void ovl_drop_write(struct dentry *dentry)
      25             : {
      26           5 :         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
      27           5 :         mnt_drop_write(ovl_upper_mnt(ofs));
      28           5 : }
      29             : 
      30           4 : struct dentry *ovl_workdir(struct dentry *dentry)
      31             : {
      32           4 :         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
      33           4 :         return ofs->workdir;
      34             : }
      35             : 
      36         251 : const struct cred *ovl_override_creds(struct super_block *sb)
      37             : {
      38         251 :         struct ovl_fs *ofs = sb->s_fs_info;
      39             : 
      40         251 :         return override_creds(ofs->creator_cred);
      41             : }
      42             : 
      43             : /*
      44             :  * Check if underlying fs supports file handles and try to determine encoding
      45             :  * type, in order to deduce maximum inode number used by fs.
      46             :  *
      47             :  * Return 0 if file handles are not supported.
      48             :  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
      49             :  * Return -1 if fs uses a non default encoding with unknown inode size.
      50             :  */
      51          10 : int ovl_can_decode_fh(struct super_block *sb)
      52             : {
      53          10 :         if (!capable(CAP_DAC_READ_SEARCH))
      54             :                 return 0;
      55             : 
      56           0 :         if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
      57             :                 return 0;
      58             : 
      59           0 :         return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
      60             : }
      61             : 
      62          20 : struct dentry *ovl_indexdir(struct super_block *sb)
      63             : {
      64          20 :         struct ovl_fs *ofs = sb->s_fs_info;
      65             : 
      66          20 :         return ofs->indexdir;
      67             : }
      68             : 
      69             : /* Index all files on copy up. For now only enabled for NFS export */
      70           0 : bool ovl_index_all(struct super_block *sb)
      71             : {
      72           0 :         struct ovl_fs *ofs = sb->s_fs_info;
      73             : 
      74           0 :         return ofs->config.nfs_export && ofs->config.index;
      75             : }
      76             : 
      77             : /* Verify lower origin on lookup. For now only enabled for NFS export */
      78           2 : bool ovl_verify_lower(struct super_block *sb)
      79             : {
      80           2 :         struct ovl_fs *ofs = sb->s_fs_info;
      81             : 
      82           2 :         return ofs->config.nfs_export && ofs->config.index;
      83             : }
      84             : 
      85          24 : struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
      86             : {
      87          24 :         size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
      88          24 :         struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
      89             : 
      90          24 :         if (oe)
      91          24 :                 oe->numlower = numlower;
      92             : 
      93          24 :         return oe;
      94             : }
      95             : 
      96           2 : bool ovl_dentry_remote(struct dentry *dentry)
      97             : {
      98           2 :         return dentry->d_flags &
      99             :                 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
     100             : }
     101             : 
     102          24 : void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
     103             :                              unsigned int mask)
     104             : {
     105          24 :         struct ovl_entry *oe = OVL_E(dentry);
     106          24 :         unsigned int i, flags = 0;
     107             : 
     108          24 :         if (upperdentry)
     109          16 :                 flags |= upperdentry->d_flags;
     110          36 :         for (i = 0; i < oe->numlower; i++)
     111          12 :                 flags |= oe->lowerstack[i].dentry->d_flags;
     112             : 
     113          24 :         spin_lock(&dentry->d_lock);
     114          24 :         dentry->d_flags &= ~mask;
     115          24 :         dentry->d_flags |= flags & mask;
     116          24 :         spin_unlock(&dentry->d_lock);
     117          24 : }
     118             : 
     119          30 : bool ovl_dentry_weird(struct dentry *dentry)
     120             : {
     121          30 :         return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
     122             :                                   DCACHE_MANAGE_TRANSIT |
     123             :                                   DCACHE_OP_HASH |
     124             :                                   DCACHE_OP_COMPARE);
     125             : }
     126             : 
     127          12 : enum ovl_path_type ovl_path_type(struct dentry *dentry)
     128             : {
     129          12 :         struct ovl_entry *oe = dentry->d_fsdata;
     130          12 :         enum ovl_path_type type = 0;
     131             : 
     132          12 :         if (ovl_dentry_upper(dentry)) {
     133          10 :                 type = __OVL_PATH_UPPER;
     134             : 
     135             :                 /*
     136             :                  * Non-dir dentry can hold lower dentry of its copy up origin.
     137             :                  */
     138          10 :                 if (oe->numlower) {
     139           6 :                         if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
     140           6 :                                 type |= __OVL_PATH_ORIGIN;
     141           6 :                         if (d_is_dir(dentry) ||
     142           0 :                             !ovl_has_upperdata(d_inode(dentry)))
     143           6 :                                 type |= __OVL_PATH_MERGE;
     144             :                 }
     145             :         } else {
     146           2 :                 if (oe->numlower > 1)
     147           0 :                         type |= __OVL_PATH_MERGE;
     148             :         }
     149          12 :         return type;
     150             : }
     151             : 
     152          17 : void ovl_path_upper(struct dentry *dentry, struct path *path)
     153             : {
     154          17 :         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
     155             : 
     156          17 :         path->mnt = ovl_upper_mnt(ofs);
     157          17 :         path->dentry = ovl_dentry_upper(dentry);
     158          10 : }
     159             : 
     160           6 : void ovl_path_lower(struct dentry *dentry, struct path *path)
     161             : {
     162           6 :         struct ovl_entry *oe = dentry->d_fsdata;
     163             : 
     164           6 :         if (oe->numlower) {
     165           6 :                 path->mnt = oe->lowerstack[0].layer->mnt;
     166           6 :                 path->dentry = oe->lowerstack[0].dentry;
     167             :         } else {
     168           0 :                 *path = (struct path) { };
     169             :         }
     170           6 : }
     171             : 
     172           3 : void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
     173             : {
     174           3 :         struct ovl_entry *oe = dentry->d_fsdata;
     175             : 
     176           3 :         if (oe->numlower) {
     177           3 :                 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
     178           3 :                 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
     179             :         } else {
     180           0 :                 *path = (struct path) { };
     181             :         }
     182           3 : }
     183             : 
     184          12 : enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
     185             : {
     186          12 :         enum ovl_path_type type = ovl_path_type(dentry);
     187             : 
     188          12 :         if (!OVL_TYPE_UPPER(type))
     189           2 :                 ovl_path_lower(dentry, path);
     190             :         else
     191          10 :                 ovl_path_upper(dentry, path);
     192             : 
     193          12 :         return type;
     194             : }
     195             : 
     196         110 : struct dentry *ovl_dentry_upper(struct dentry *dentry)
     197             : {
     198          22 :         return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
     199             : }
     200             : 
     201           4 : struct dentry *ovl_dentry_lower(struct dentry *dentry)
     202             : {
     203           4 :         struct ovl_entry *oe = dentry->d_fsdata;
     204             : 
     205           0 :         return oe->numlower ? oe->lowerstack[0].dentry : NULL;
     206             : }
     207             : 
     208           0 : const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
     209             : {
     210           0 :         struct ovl_entry *oe = dentry->d_fsdata;
     211             : 
     212           0 :         return oe->numlower ? oe->lowerstack[0].layer : NULL;
     213             : }
     214             : 
     215             : /*
     216             :  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
     217             :  * dependig on what is stored in lowerstack[0]. At times we need to find
     218             :  * lower dentry which has data (and not metacopy dentry). This helper
     219             :  * returns the lower data dentry.
     220             :  */
     221           0 : struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
     222             : {
     223           0 :         struct ovl_entry *oe = dentry->d_fsdata;
     224             : 
     225           0 :         return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
     226             : }
     227             : 
     228           0 : struct dentry *ovl_dentry_real(struct dentry *dentry)
     229             : {
     230           0 :         return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
     231             : }
     232             : 
     233         238 : struct dentry *ovl_i_dentry_upper(struct inode *inode)
     234             : {
     235         238 :         return ovl_upperdentry_dereference(OVL_I(inode));
     236             : }
     237             : 
     238         238 : struct inode *ovl_inode_upper(struct inode *inode)
     239             : {
     240         175 :         struct dentry *upperdentry = ovl_i_dentry_upper(inode);
     241             : 
     242         238 :         return upperdentry ? d_inode(upperdentry) : NULL;
     243             : }
     244             : 
     245          88 : struct inode *ovl_inode_lower(struct inode *inode)
     246             : {
     247           3 :         return OVL_I(inode)->lower;
     248             : }
     249             : 
     250          24 : struct inode *ovl_inode_real(struct inode *inode)
     251             : {
     252          24 :         return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
     253             : }
     254             : 
     255             : /* Return inode which contains lower data. Do not return metacopy */
     256           3 : struct inode *ovl_inode_lowerdata(struct inode *inode)
     257             : {
     258           3 :         if (WARN_ON(!S_ISREG(inode->i_mode)))
     259             :                 return NULL;
     260             : 
     261           3 :         return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
     262             : }
     263             : 
     264             : /* Return real inode which contains data. Does not return metacopy inode */
     265          39 : struct inode *ovl_inode_realdata(struct inode *inode)
     266             : {
     267          39 :         struct inode *upperinode;
     268             : 
     269          39 :         upperinode = ovl_inode_upper(inode);
     270          36 :         if (upperinode && ovl_has_upperdata(inode))
     271             :                 return upperinode;
     272             : 
     273           3 :         return ovl_inode_lowerdata(inode);
     274             : }
     275             : 
     276          16 : struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
     277             : {
     278          16 :         return OVL_I(inode)->cache;
     279             : }
     280             : 
     281           0 : void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
     282             : {
     283           0 :         OVL_I(inode)->cache = cache;
     284           0 : }
     285             : 
     286          22 : void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
     287             : {
     288           2 :         set_bit(flag, &OVL_E(dentry)->flags);
     289           2 : }
     290             : 
     291           0 : void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
     292             : {
     293           0 :         clear_bit(flag, &OVL_E(dentry)->flags);
     294           0 : }
     295             : 
     296          40 : bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
     297             : {
     298           0 :         return test_bit(flag, &OVL_E(dentry)->flags);
     299             : }
     300             : 
     301           0 : bool ovl_dentry_is_opaque(struct dentry *dentry)
     302             : {
     303           0 :         return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
     304             : }
     305             : 
     306           0 : bool ovl_dentry_is_whiteout(struct dentry *dentry)
     307             : {
     308           0 :         return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
     309             : }
     310             : 
     311           0 : void ovl_dentry_set_opaque(struct dentry *dentry)
     312             : {
     313           0 :         ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
     314           0 : }
     315             : 
     316             : /*
     317             :  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
     318             :  * to return positive, while there's no actual upper alias for the inode.
     319             :  * Copy up code needs to know about the existence of the upper alias, so it
     320             :  * can't use ovl_dentry_upper().
     321             :  */
     322          40 : bool ovl_dentry_has_upper_alias(struct dentry *dentry)
     323             : {
     324          40 :         return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
     325             : }
     326             : 
     327          20 : void ovl_dentry_set_upper_alias(struct dentry *dentry)
     328             : {
     329          20 :         ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
     330          20 : }
     331             : 
     332          67 : static bool ovl_should_check_upperdata(struct inode *inode)
     333             : {
     334          67 :         if (!S_ISREG(inode->i_mode))
     335             :                 return false;
     336             : 
     337          67 :         if (!ovl_inode_lower(inode))
     338             :                 return false;
     339             : 
     340             :         return true;
     341             : }
     342             : 
     343          67 : bool ovl_has_upperdata(struct inode *inode)
     344             : {
     345          67 :         if (!ovl_should_check_upperdata(inode))
     346             :                 return true;
     347             : 
     348          24 :         if (!ovl_test_flag(OVL_UPPERDATA, inode))
     349             :                 return false;
     350             :         /*
     351             :          * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
     352             :          * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
     353             :          * if setting of OVL_UPPERDATA is visible, then effects of writes
     354             :          * before that are visible too.
     355             :          */
     356          24 :         smp_rmb();
     357          24 :         return true;
     358             : }
     359             : 
     360           6 : void ovl_set_upperdata(struct inode *inode)
     361             : {
     362             :         /*
     363             :          * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
     364             :          * if OVL_UPPERDATA flag is visible, then effects of write operations
     365             :          * before it are visible as well.
     366             :          */
     367           6 :         smp_wmb();
     368          12 :         ovl_set_flag(OVL_UPPERDATA, inode);
     369           6 : }
     370             : 
     371             : /* Caller should hold ovl_inode->lock */
     372           4 : bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
     373             : {
     374           4 :         if (!ovl_open_flags_need_copy_up(flags))
     375             :                 return false;
     376             : 
     377           4 :         return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
     378             : }
     379             : 
     380          36 : bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
     381             : {
     382          36 :         if (!ovl_open_flags_need_copy_up(flags))
     383             :                 return false;
     384             : 
     385          31 :         return !ovl_has_upperdata(d_inode(dentry));
     386             : }
     387             : 
     388           0 : bool ovl_redirect_dir(struct super_block *sb)
     389             : {
     390           0 :         struct ovl_fs *ofs = sb->s_fs_info;
     391             : 
     392           0 :         return ofs->config.redirect_dir && !ofs->noxattr;
     393             : }
     394             : 
     395           0 : const char *ovl_dentry_get_redirect(struct dentry *dentry)
     396             : {
     397           0 :         return OVL_I(d_inode(dentry))->redirect;
     398             : }
     399             : 
     400           0 : void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
     401             : {
     402           0 :         struct ovl_inode *oi = OVL_I(d_inode(dentry));
     403             : 
     404           0 :         kfree(oi->redirect);
     405           0 :         oi->redirect = redirect;
     406           0 : }
     407             : 
     408           4 : void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
     409             : {
     410           4 :         struct inode *upperinode = d_inode(upperdentry);
     411             : 
     412           4 :         WARN_ON(OVL_I(inode)->__upperdentry);
     413             : 
     414             :         /*
     415             :          * Make sure upperdentry is consistent before making it visible
     416             :          */
     417           4 :         smp_wmb();
     418           4 :         OVL_I(inode)->__upperdentry = upperdentry;
     419           4 :         if (inode_unhashed(inode)) {
     420           0 :                 inode->i_private = upperinode;
     421           0 :                 __insert_inode_hash(inode, (unsigned long) upperinode);
     422             :         }
     423           4 : }
     424             : 
     425           0 : static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
     426             : {
     427           0 :         struct inode *inode = d_inode(dentry);
     428             : 
     429           0 :         WARN_ON(!inode_is_locked(inode));
     430             :         /*
     431             :          * Version is used by readdir code to keep cache consistent.  For merge
     432             :          * dirs all changes need to be noted.  For non-merge dirs, cache only
     433             :          * contains impure (ones which have been copied up and have origins)
     434             :          * entries, so only need to note changes to impure entries.
     435             :          */
     436           0 :         if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
     437           0 :                 OVL_I(inode)->version++;
     438           0 : }
     439             : 
     440           0 : void ovl_dir_modified(struct dentry *dentry, bool impurity)
     441             : {
     442             :         /* Copy mtime/ctime */
     443           0 :         ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
     444             : 
     445           0 :         ovl_dentry_version_inc(dentry, impurity);
     446           0 : }
     447             : 
     448           0 : u64 ovl_dentry_version_get(struct dentry *dentry)
     449             : {
     450           0 :         struct inode *inode = d_inode(dentry);
     451             : 
     452           0 :         WARN_ON(!inode_is_locked(inode));
     453           0 :         return OVL_I(inode)->version;
     454             : }
     455             : 
     456          26 : bool ovl_is_whiteout(struct dentry *dentry)
     457             : {
     458          26 :         struct inode *inode = dentry->d_inode;
     459             : 
     460          26 :         return inode && IS_WHITEOUT(inode);
     461             : }
     462             : 
     463          14 : struct file *ovl_path_open(struct path *path, int flags)
     464             : {
     465          14 :         struct inode *inode = d_inode(path->dentry);
     466          14 :         int err, acc_mode;
     467             : 
     468          14 :         if (flags & ~(O_ACCMODE | O_LARGEFILE))
     469           0 :                 BUG();
     470             : 
     471          14 :         switch (flags & O_ACCMODE) {
     472             :         case O_RDONLY:
     473             :                 acc_mode = MAY_READ;
     474             :                 break;
     475           0 :         case O_WRONLY:
     476           0 :                 acc_mode = MAY_WRITE;
     477           0 :                 break;
     478           0 :         default:
     479           0 :                 BUG();
     480             :         }
     481             : 
     482          14 :         err = inode_permission(&init_user_ns, inode, acc_mode | MAY_OPEN);
     483          14 :         if (err)
     484           0 :                 return ERR_PTR(err);
     485             : 
     486             :         /* O_NOATIME is an optimization, don't fail if not permitted */
     487          14 :         if (inode_owner_or_capable(&init_user_ns, inode))
     488          14 :                 flags |= O_NOATIME;
     489             : 
     490          14 :         return dentry_open(path, flags, current_cred());
     491             : }
     492             : 
     493             : /* Caller should hold ovl_inode->lock */
     494           4 : static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
     495             : {
     496           4 :         bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
     497             : 
     498           4 :         if (ovl_dentry_upper(dentry) &&
     499           0 :             (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
     500           0 :             !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
     501           0 :                 return true;
     502             : 
     503             :         return false;
     504             : }
     505             : 
     506          46 : bool ovl_already_copied_up(struct dentry *dentry, int flags)
     507             : {
     508          46 :         bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
     509             : 
     510             :         /*
     511             :          * Check if copy-up has happened as well as for upper alias (in
     512             :          * case of hard links) is there.
     513             :          *
     514             :          * Both checks are lockless:
     515             :          *  - false negatives: will recheck under oi->lock
     516             :          *  - false positives:
     517             :          *    + ovl_dentry_upper() uses memory barriers to ensure the
     518             :          *      upper dentry is up-to-date
     519             :          *    + ovl_dentry_has_upper_alias() relies on locking of
     520             :          *      upper parent i_rwsem to prevent reordering copy-up
     521             :          *      with rename.
     522             :          */
     523          46 :         if (ovl_dentry_upper(dentry) &&
     524          72 :             (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
     525          36 :             !ovl_dentry_needs_data_copy_up(dentry, flags))
     526          36 :                 return true;
     527             : 
     528             :         return false;
     529             : }
     530             : 
     531           4 : int ovl_copy_up_start(struct dentry *dentry, int flags)
     532             : {
     533           4 :         struct inode *inode = d_inode(dentry);
     534           4 :         int err;
     535             : 
     536           4 :         err = ovl_inode_lock_interruptible(inode);
     537           4 :         if (!err && ovl_already_copied_up_locked(dentry, flags)) {
     538           0 :                 err = 1; /* Already copied up */
     539           0 :                 ovl_inode_unlock(inode);
     540             :         }
     541             : 
     542           4 :         return err;
     543             : }
     544             : 
     545           4 : void ovl_copy_up_end(struct dentry *dentry)
     546             : {
     547           4 :         ovl_inode_unlock(d_inode(dentry));
     548           4 : }
     549             : 
     550           6 : bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
     551             : {
     552           6 :         int res;
     553             : 
     554           6 :         res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
     555             : 
     556             :         /* Zero size value means "copied up but origin unknown" */
     557           6 :         if (res >= 0)
     558           0 :                 return true;
     559             : 
     560             :         return false;
     561             : }
     562             : 
     563          20 : bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
     564             :                          enum ovl_xattr ox)
     565             : {
     566          20 :         int res;
     567          20 :         char val;
     568             : 
     569          30 :         if (!d_is_dir(dentry))
     570             :                 return false;
     571             : 
     572          10 :         res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
     573          10 :         if (res == 1 && val == 'y')
     574           0 :                 return true;
     575             : 
     576             :         return false;
     577             : }
     578             : 
     579             : #define OVL_XATTR_OPAQUE_POSTFIX        "opaque"
     580             : #define OVL_XATTR_REDIRECT_POSTFIX      "redirect"
     581             : #define OVL_XATTR_ORIGIN_POSTFIX        "origin"
     582             : #define OVL_XATTR_IMPURE_POSTFIX        "impure"
     583             : #define OVL_XATTR_NLINK_POSTFIX         "nlink"
     584             : #define OVL_XATTR_UPPER_POSTFIX         "upper"
     585             : #define OVL_XATTR_METACOPY_POSTFIX      "metacopy"
     586             : 
     587             : #define OVL_XATTR_TAB_ENTRY(x) \
     588             :         [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
     589             :                 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
     590             : 
     591             : const char *const ovl_xattr_table[][2] = {
     592             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
     593             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
     594             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
     595             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
     596             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
     597             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
     598             :         OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
     599             : };
     600             : 
     601          10 : int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
     602             :                        enum ovl_xattr ox, const void *value, size_t size,
     603             :                        int xerr)
     604             : {
     605          10 :         int err;
     606          10 :         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
     607             : 
     608          10 :         if (ofs->noxattr)
     609             :                 return xerr;
     610             : 
     611          10 :         err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
     612             : 
     613          10 :         if (err == -EOPNOTSUPP) {
     614           0 :                 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
     615           0 :                 ofs->noxattr = true;
     616           0 :                 return xerr;
     617             :         }
     618             : 
     619             :         return err;
     620             : }
     621             : 
     622           6 : int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
     623             : {
     624           6 :         int err;
     625             : 
     626           6 :         if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
     627             :                 return 0;
     628             : 
     629             :         /*
     630             :          * Do not fail when upper doesn't support xattrs.
     631             :          * Upper inodes won't have origin nor redirect xattr anyway.
     632             :          */
     633           4 :         err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
     634             :                                  "y", 1, 0);
     635           4 :         if (!err)
     636           4 :                 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
     637             : 
     638             :         return err;
     639             : }
     640             : 
     641          39 : void ovl_set_flag(unsigned long flag, struct inode *inode)
     642             : {
     643          39 :         set_bit(flag, &OVL_I(inode)->flags);
     644           4 : }
     645             : 
     646           0 : void ovl_clear_flag(unsigned long flag, struct inode *inode)
     647             : {
     648           0 :         clear_bit(flag, &OVL_I(inode)->flags);
     649           0 : }
     650             : 
     651          52 : bool ovl_test_flag(unsigned long flag, struct inode *inode)
     652             : {
     653          52 :         return test_bit(flag, &OVL_I(inode)->flags);
     654             : }
     655             : 
     656             : /**
     657             :  * Caller must hold a reference to inode to prevent it from being freed while
     658             :  * it is marked inuse.
     659             :  */
     660           4 : bool ovl_inuse_trylock(struct dentry *dentry)
     661             : {
     662           4 :         struct inode *inode = d_inode(dentry);
     663           4 :         bool locked = false;
     664             : 
     665           4 :         spin_lock(&inode->i_lock);
     666           4 :         if (!(inode->i_state & I_OVL_INUSE)) {
     667           4 :                 inode->i_state |= I_OVL_INUSE;
     668           4 :                 locked = true;
     669             :         }
     670           4 :         spin_unlock(&inode->i_lock);
     671             : 
     672           4 :         return locked;
     673             : }
     674             : 
     675           4 : void ovl_inuse_unlock(struct dentry *dentry)
     676             : {
     677           4 :         if (dentry) {
     678           4 :                 struct inode *inode = d_inode(dentry);
     679             : 
     680           4 :                 spin_lock(&inode->i_lock);
     681           4 :                 WARN_ON(!(inode->i_state & I_OVL_INUSE));
     682           4 :                 inode->i_state &= ~I_OVL_INUSE;
     683           4 :                 spin_unlock(&inode->i_lock);
     684             :         }
     685           4 : }
     686             : 
     687           8 : bool ovl_is_inuse(struct dentry *dentry)
     688             : {
     689           8 :         struct inode *inode = d_inode(dentry);
     690           8 :         bool inuse;
     691             : 
     692           8 :         spin_lock(&inode->i_lock);
     693           8 :         inuse = (inode->i_state & I_OVL_INUSE);
     694           8 :         spin_unlock(&inode->i_lock);
     695             : 
     696           8 :         return inuse;
     697             : }
     698             : 
     699             : /*
     700             :  * Does this overlay dentry need to be indexed on copy up?
     701             :  */
     702           4 : bool ovl_need_index(struct dentry *dentry)
     703             : {
     704           4 :         struct dentry *lower = ovl_dentry_lower(dentry);
     705             : 
     706           4 :         if (!lower || !ovl_indexdir(dentry->d_sb))
     707             :                 return false;
     708             : 
     709             :         /* Index all files for NFS export and consistency verification */
     710           0 :         if (ovl_index_all(dentry->d_sb))
     711             :                 return true;
     712             : 
     713             :         /* Index only lower hardlinks on copy up */
     714           0 :         if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
     715           0 :                 return true;
     716             : 
     717             :         return false;
     718             : }
     719             : 
     720             : /* Caller must hold OVL_I(inode)->lock */
     721           0 : static void ovl_cleanup_index(struct dentry *dentry)
     722             : {
     723           0 :         struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
     724           0 :         struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
     725           0 :         struct inode *dir = indexdir->d_inode;
     726           0 :         struct dentry *lowerdentry = ovl_dentry_lower(dentry);
     727           0 :         struct dentry *upperdentry = ovl_dentry_upper(dentry);
     728           0 :         struct dentry *index = NULL;
     729           0 :         struct inode *inode;
     730           0 :         struct qstr name = { };
     731           0 :         int err;
     732             : 
     733           0 :         err = ovl_get_index_name(ofs, lowerdentry, &name);
     734           0 :         if (err)
     735           0 :                 goto fail;
     736             : 
     737           0 :         inode = d_inode(upperdentry);
     738           0 :         if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
     739           0 :                 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
     740             :                                     upperdentry, inode->i_ino, inode->i_nlink);
     741             :                 /*
     742             :                  * We either have a bug with persistent union nlink or a lower
     743             :                  * hardlink was added while overlay is mounted. Adding a lower
     744             :                  * hardlink and then unlinking all overlay hardlinks would drop
     745             :                  * overlay nlink to zero before all upper inodes are unlinked.
     746             :                  * As a safety measure, when that situation is detected, set
     747             :                  * the overlay nlink to the index inode nlink minus one for the
     748             :                  * index entry itself.
     749             :                  */
     750           0 :                 set_nlink(d_inode(dentry), inode->i_nlink - 1);
     751           0 :                 ovl_set_nlink_upper(dentry);
     752           0 :                 goto out;
     753             :         }
     754             : 
     755           0 :         inode_lock_nested(dir, I_MUTEX_PARENT);
     756           0 :         index = lookup_one_len(name.name, indexdir, name.len);
     757           0 :         err = PTR_ERR(index);
     758           0 :         if (IS_ERR(index)) {
     759             :                 index = NULL;
     760           0 :         } else if (ovl_index_all(dentry->d_sb)) {
     761             :                 /* Whiteout orphan index to block future open by handle */
     762           0 :                 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
     763             :                                                dir, index);
     764             :         } else {
     765             :                 /* Cleanup orphan index entries */
     766           0 :                 err = ovl_cleanup(dir, index);
     767             :         }
     768             : 
     769           0 :         inode_unlock(dir);
     770           0 :         if (err)
     771           0 :                 goto fail;
     772             : 
     773           0 : out:
     774           0 :         kfree(name.name);
     775           0 :         dput(index);
     776           0 :         return;
     777             : 
     778           0 : fail:
     779           0 :         pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
     780           0 :         goto out;
     781             : }
     782             : 
     783             : /*
     784             :  * Operations that change overlay inode and upper inode nlink need to be
     785             :  * synchronized with copy up for persistent nlink accounting.
     786             :  */
     787           0 : int ovl_nlink_start(struct dentry *dentry)
     788             : {
     789           0 :         struct inode *inode = d_inode(dentry);
     790           0 :         const struct cred *old_cred;
     791           0 :         int err;
     792             : 
     793           0 :         if (WARN_ON(!inode))
     794             :                 return -ENOENT;
     795             : 
     796             :         /*
     797             :          * With inodes index is enabled, we store the union overlay nlink
     798             :          * in an xattr on the index inode. When whiting out an indexed lower,
     799             :          * we need to decrement the overlay persistent nlink, but before the
     800             :          * first copy up, we have no upper index inode to store the xattr.
     801             :          *
     802             :          * As a workaround, before whiteout/rename over an indexed lower,
     803             :          * copy up to create the upper index. Creating the upper index will
     804             :          * initialize the overlay nlink, so it could be dropped if unlink
     805             :          * or rename succeeds.
     806             :          *
     807             :          * TODO: implement metadata only index copy up when called with
     808             :          *       ovl_copy_up_flags(dentry, O_PATH).
     809             :          */
     810           0 :         if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
     811           0 :                 err = ovl_copy_up(dentry);
     812           0 :                 if (err)
     813             :                         return err;
     814             :         }
     815             : 
     816           0 :         err = ovl_inode_lock_interruptible(inode);
     817           0 :         if (err)
     818             :                 return err;
     819             : 
     820           0 :         if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
     821           0 :                 goto out;
     822             : 
     823           0 :         old_cred = ovl_override_creds(dentry->d_sb);
     824             :         /*
     825             :          * The overlay inode nlink should be incremented/decremented IFF the
     826             :          * upper operation succeeds, along with nlink change of upper inode.
     827             :          * Therefore, before link/unlink/rename, we store the union nlink
     828             :          * value relative to the upper inode nlink in an upper inode xattr.
     829             :          */
     830           0 :         err = ovl_set_nlink_upper(dentry);
     831           0 :         revert_creds(old_cred);
     832             : 
     833           0 : out:
     834           0 :         if (err)
     835           0 :                 ovl_inode_unlock(inode);
     836             : 
     837             :         return err;
     838             : }
     839             : 
     840           0 : void ovl_nlink_end(struct dentry *dentry)
     841             : {
     842           0 :         struct inode *inode = d_inode(dentry);
     843             : 
     844           0 :         if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
     845           0 :                 const struct cred *old_cred;
     846             : 
     847           0 :                 old_cred = ovl_override_creds(dentry->d_sb);
     848           0 :                 ovl_cleanup_index(dentry);
     849           0 :                 revert_creds(old_cred);
     850             :         }
     851             : 
     852           0 :         ovl_inode_unlock(inode);
     853           0 : }
     854             : 
     855           0 : int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
     856             : {
     857             :         /* Workdir should not be the same as upperdir */
     858           0 :         if (workdir == upperdir)
     859           0 :                 goto err;
     860             : 
     861             :         /* Workdir should not be subdir of upperdir and vice versa */
     862           0 :         if (lock_rename(workdir, upperdir) != NULL)
     863           0 :                 goto err_unlock;
     864             : 
     865             :         return 0;
     866             : 
     867           0 : err_unlock:
     868           0 :         unlock_rename(workdir, upperdir);
     869           0 : err:
     870           0 :         pr_err("failed to lock workdir+upperdir\n");
     871           0 :         return -EIO;
     872             : }
     873             : 
     874             : /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
     875          16 : int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
     876             : {
     877          16 :         int res;
     878             : 
     879             :         /* Only regular files can have metacopy xattr */
     880          16 :         if (!S_ISREG(d_inode(dentry)->i_mode))
     881             :                 return 0;
     882             : 
     883          16 :         res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
     884          16 :         if (res < 0) {
     885          16 :                 if (res == -ENODATA || res == -EOPNOTSUPP)
     886             :                         return 0;
     887             :                 /*
     888             :                  * getxattr on user.* may fail with EACCES in case there's no
     889             :                  * read permission on the inode.  Not much we can do, other than
     890             :                  * tell the caller that this is not a metacopy inode.
     891             :                  */
     892           0 :                 if (ofs->config.userxattr && res == -EACCES)
     893             :                         return 0;
     894           0 :                 goto out;
     895             :         }
     896             : 
     897             :         return 1;
     898           0 : out:
     899           0 :         pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
     900             :         return res;
     901             : }
     902             : 
     903           0 : bool ovl_is_metacopy_dentry(struct dentry *dentry)
     904             : {
     905           0 :         struct ovl_entry *oe = dentry->d_fsdata;
     906             : 
     907           0 :         if (!d_is_reg(dentry))
     908             :                 return false;
     909             : 
     910           0 :         if (ovl_dentry_upper(dentry)) {
     911           0 :                 if (!ovl_has_upperdata(d_inode(dentry)))
     912             :                         return true;
     913           0 :                 return false;
     914             :         }
     915             : 
     916           0 :         return (oe->numlower > 1);
     917             : }
     918             : 
     919           4 : char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
     920             :                              int padding)
     921             : {
     922           4 :         int res;
     923           4 :         char *s, *next, *buf = NULL;
     924             : 
     925           4 :         res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
     926           4 :         if (res == -ENODATA || res == -EOPNOTSUPP)
     927             :                 return NULL;
     928           0 :         if (res < 0)
     929           0 :                 goto fail;
     930           0 :         if (res == 0)
     931           0 :                 goto invalid;
     932             : 
     933           0 :         buf = kzalloc(res + padding + 1, GFP_KERNEL);
     934           0 :         if (!buf)
     935           4 :                 return ERR_PTR(-ENOMEM);
     936             : 
     937           0 :         res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
     938           0 :         if (res < 0)
     939           0 :                 goto fail;
     940           0 :         if (res == 0)
     941           0 :                 goto invalid;
     942             : 
     943           0 :         if (buf[0] == '/') {
     944           0 :                 for (s = buf; *s++ == '/'; s = next) {
     945           0 :                         next = strchrnul(s, '/');
     946           0 :                         if (s == next)
     947           0 :                                 goto invalid;
     948             :                 }
     949             :         } else {
     950           0 :                 if (strchr(buf, '/') != NULL)
     951           0 :                         goto invalid;
     952             :         }
     953             : 
     954             :         return buf;
     955           0 : invalid:
     956           0 :         pr_warn_ratelimited("invalid redirect (%s)\n", buf);
     957           0 :         res = -EINVAL;
     958           0 :         goto err_free;
     959           0 : fail:
     960           0 :         pr_warn_ratelimited("failed to get redirect (%i)\n", res);
     961           0 : err_free:
     962           0 :         kfree(buf);
     963           0 :         return ERR_PTR(res);
     964             : }
     965             : 
     966             : /*
     967             :  * ovl_sync_status() - Check fs sync status for volatile mounts
     968             :  *
     969             :  * Returns 1 if this is not a volatile mount and a real sync is required.
     970             :  *
     971             :  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
     972             :  * have occurred on the upperdir since the mount.
     973             :  *
     974             :  * Returns -errno if it is a volatile mount, and the error that occurred since
     975             :  * the last mount. If the error code changes, it'll return the latest error
     976             :  * code.
     977             :  */
     978             : 
     979           4 : int ovl_sync_status(struct ovl_fs *ofs)
     980             : {
     981           4 :         struct vfsmount *mnt;
     982             : 
     983           4 :         if (ovl_should_sync(ofs))
     984             :                 return 1;
     985             : 
     986           0 :         mnt = ovl_upper_mnt(ofs);
     987           0 :         if (!mnt)
     988             :                 return 0;
     989             : 
     990           0 :         return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
     991             : }

Generated by: LCOV version 1.14