Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/export.h>
0003 #include <linux/fs.h>
0004 #include <linux/fs_stack.h>
0005 
0006 /* does _NOT_ require i_mutex to be held.
0007  *
0008  * This function cannot be inlined since i_size_{read,write} is rather
0009  * heavy-weight on 32-bit systems
0010  */
0011 void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
0012 {
0013     loff_t i_size;
0014     blkcnt_t i_blocks;
0015 
0016     /*
0017      * i_size_read() includes its own seqlocking and protection from
0018      * preemption (see include/linux/fs.h): we need nothing extra for
0019      * that here, and prefer to avoid nesting locks than attempt to keep
0020      * i_size and i_blocks in sync together.
0021      */
0022     i_size = i_size_read(src);
0023 
0024     /*
0025      * But on 32-bit, we ought to make an effort to keep the two halves of
0026      * i_blocks in sync despite SMP or PREEMPTION - though stat's
0027      * generic_fillattr() doesn't bother, and we won't be applying quotas
0028      * (where i_blocks does become important) at the upper level.
0029      *
0030      * We don't actually know what locking is used at the lower level;
0031      * but if it's a filesystem that supports quotas, it will be using
0032      * i_lock as in inode_add_bytes().
0033      */
0034     if (sizeof(i_blocks) > sizeof(long))
0035         spin_lock(&src->i_lock);
0036     i_blocks = src->i_blocks;
0037     if (sizeof(i_blocks) > sizeof(long))
0038         spin_unlock(&src->i_lock);
0039 
0040     /*
0041      * If CONFIG_SMP or CONFIG_PREEMPTION on 32-bit, it's vital for
0042      * fsstack_copy_inode_size() to hold some lock around
0043      * i_size_write(), otherwise i_size_read() may spin forever (see
0044      * include/linux/fs.h).  We don't necessarily hold i_mutex when this
0045      * is called, so take i_lock for that case.
0046      *
0047      * And if on 32-bit, continue our effort to keep the two halves of
0048      * i_blocks in sync despite SMP or PREEMPTION: use i_lock for that case
0049      * too, and do both at once by combining the tests.
0050      *
0051      * There is none of this locking overhead in the 64-bit case.
0052      */
0053     if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
0054         spin_lock(&dst->i_lock);
0055     i_size_write(dst, i_size);
0056     dst->i_blocks = i_blocks;
0057     if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
0058         spin_unlock(&dst->i_lock);
0059 }
0060 EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
0061 
0062 /* copy all attributes */
0063 void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
0064 {
0065     dest->i_mode = src->i_mode;
0066     dest->i_uid = src->i_uid;
0067     dest->i_gid = src->i_gid;
0068     dest->i_rdev = src->i_rdev;
0069     dest->i_atime = src->i_atime;
0070     dest->i_mtime = src->i_mtime;
0071     dest->i_ctime = src->i_ctime;
0072     dest->i_blkbits = src->i_blkbits;
0073     dest->i_flags = src->i_flags;
0074     set_nlink(dest, src->i_nlink);
0075 }
0076 EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);