18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/export.h>
38c2ecf20Sopenharmony_ci#include <linux/fs.h>
48c2ecf20Sopenharmony_ci#include <linux/fs_stack.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/* does _NOT_ require i_mutex to be held.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This function cannot be inlined since i_size_{read,write} is rather
98c2ecf20Sopenharmony_ci * heavy-weight on 32-bit systems
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_civoid fsstack_copy_inode_size(struct inode *dst, struct inode *src)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	loff_t i_size;
148c2ecf20Sopenharmony_ci	blkcnt_t i_blocks;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	/*
178c2ecf20Sopenharmony_ci	 * i_size_read() includes its own seqlocking and protection from
188c2ecf20Sopenharmony_ci	 * preemption (see include/linux/fs.h): we need nothing extra for
198c2ecf20Sopenharmony_ci	 * that here, and prefer to avoid nesting locks than attempt to keep
208c2ecf20Sopenharmony_ci	 * i_size and i_blocks in sync together.
218c2ecf20Sopenharmony_ci	 */
228c2ecf20Sopenharmony_ci	i_size = i_size_read(src);
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	/*
258c2ecf20Sopenharmony_ci	 * But on 32-bit, we ought to make an effort to keep the two halves of
268c2ecf20Sopenharmony_ci	 * i_blocks in sync despite SMP or PREEMPTION - though stat's
278c2ecf20Sopenharmony_ci	 * generic_fillattr() doesn't bother, and we won't be applying quotas
288c2ecf20Sopenharmony_ci	 * (where i_blocks does become important) at the upper level.
298c2ecf20Sopenharmony_ci	 *
308c2ecf20Sopenharmony_ci	 * We don't actually know what locking is used at the lower level;
318c2ecf20Sopenharmony_ci	 * but if it's a filesystem that supports quotas, it will be using
328c2ecf20Sopenharmony_ci	 * i_lock as in inode_add_bytes().
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci	if (sizeof(i_blocks) > sizeof(long))
358c2ecf20Sopenharmony_ci		spin_lock(&src->i_lock);
368c2ecf20Sopenharmony_ci	i_blocks = src->i_blocks;
378c2ecf20Sopenharmony_ci	if (sizeof(i_blocks) > sizeof(long))
388c2ecf20Sopenharmony_ci		spin_unlock(&src->i_lock);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	/*
418c2ecf20Sopenharmony_ci	 * If CONFIG_SMP or CONFIG_PREEMPTION on 32-bit, it's vital for
428c2ecf20Sopenharmony_ci	 * fsstack_copy_inode_size() to hold some lock around
438c2ecf20Sopenharmony_ci	 * i_size_write(), otherwise i_size_read() may spin forever (see
448c2ecf20Sopenharmony_ci	 * include/linux/fs.h).  We don't necessarily hold i_mutex when this
458c2ecf20Sopenharmony_ci	 * is called, so take i_lock for that case.
468c2ecf20Sopenharmony_ci	 *
478c2ecf20Sopenharmony_ci	 * And if on 32-bit, continue our effort to keep the two halves of
488c2ecf20Sopenharmony_ci	 * i_blocks in sync despite SMP or PREEMPTION: use i_lock for that case
498c2ecf20Sopenharmony_ci	 * too, and do both at once by combining the tests.
508c2ecf20Sopenharmony_ci	 *
518c2ecf20Sopenharmony_ci	 * There is none of this locking overhead in the 64-bit case.
528c2ecf20Sopenharmony_ci	 */
538c2ecf20Sopenharmony_ci	if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
548c2ecf20Sopenharmony_ci		spin_lock(&dst->i_lock);
558c2ecf20Sopenharmony_ci	i_size_write(dst, i_size);
568c2ecf20Sopenharmony_ci	dst->i_blocks = i_blocks;
578c2ecf20Sopenharmony_ci	if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
588c2ecf20Sopenharmony_ci		spin_unlock(&dst->i_lock);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* copy all attributes */
638c2ecf20Sopenharmony_civoid fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	dest->i_mode = src->i_mode;
668c2ecf20Sopenharmony_ci	dest->i_uid = src->i_uid;
678c2ecf20Sopenharmony_ci	dest->i_gid = src->i_gid;
688c2ecf20Sopenharmony_ci	dest->i_rdev = src->i_rdev;
698c2ecf20Sopenharmony_ci	dest->i_atime = src->i_atime;
708c2ecf20Sopenharmony_ci	dest->i_mtime = src->i_mtime;
718c2ecf20Sopenharmony_ci	dest->i_ctime = src->i_ctime;
728c2ecf20Sopenharmony_ci	dest->i_blkbits = src->i_blkbits;
738c2ecf20Sopenharmony_ci	dest->i_flags = src->i_flags;
748c2ecf20Sopenharmony_ci	set_nlink(dest, src->i_nlink);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
77