^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/fs_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /* does _NOT_ require i_mutex to be held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This function cannot be inlined since i_size_{read,write} is rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * heavy-weight on 32-bit systems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) loff_t i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) blkcnt_t i_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * i_size_read() includes its own seqlocking and protection from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * preemption (see include/linux/fs.h): we need nothing extra for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * that here, and prefer to avoid nesting locks than attempt to keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * i_size and i_blocks in sync together.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) i_size = i_size_read(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * But on 32-bit, we ought to make an effort to keep the two halves of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * i_blocks in sync despite SMP or PREEMPTION - though stat's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * generic_fillattr() doesn't bother, and we won't be applying quotas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * (where i_blocks does become important) at the upper level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * We don't actually know what locking is used at the lower level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * but if it's a filesystem that supports quotas, it will be using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * i_lock as in inode_add_bytes().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (sizeof(i_blocks) > sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spin_lock(&src->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) i_blocks = src->i_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (sizeof(i_blocks) > sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) spin_unlock(&src->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * If CONFIG_SMP or CONFIG_PREEMPTION on 32-bit, it's vital for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * fsstack_copy_inode_size() to hold some lock around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * i_size_write(), otherwise i_size_read() may spin forever (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * include/linux/fs.h). We don't necessarily hold i_mutex when this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * is called, so take i_lock for that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * And if on 32-bit, continue our effort to keep the two halves of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * i_blocks in sync despite SMP or PREEMPTION: use i_lock for that case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * too, and do both at once by combining the tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * There is none of this locking overhead in the 64-bit case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) spin_lock(&dst->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) i_size_write(dst, i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dst->i_blocks = i_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) spin_unlock(&dst->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* copy all attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) dest->i_mode = src->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) dest->i_uid = src->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) dest->i_gid = src->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) dest->i_rdev = src->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dest->i_atime = src->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dest->i_mtime = src->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dest->i_ctime = src->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dest->i_blkbits = src->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dest->i_flags = src->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) set_nlink(dest, src->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);