1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2630d9c47SPaul Gortmaker #include <linux/export.h>
342cf1193SJosef "Jeff" Sipek #include <linux/fs.h>
442cf1193SJosef "Jeff" Sipek #include <linux/fs_stack.h>
542cf1193SJosef "Jeff" Sipek
642cf1193SJosef "Jeff" Sipek /* does _NOT_ require i_mutex to be held.
742cf1193SJosef "Jeff" Sipek *
842cf1193SJosef "Jeff" Sipek * This function cannot be inlined since i_size_{read,write} is rather
942cf1193SJosef "Jeff" Sipek * heavy-weight on 32-bit systems
1042cf1193SJosef "Jeff" Sipek */
fsstack_copy_inode_size(struct inode * dst,struct inode * src)111b8ab815SErez Zadok void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
1242cf1193SJosef "Jeff" Sipek {
131b8ab815SErez Zadok loff_t i_size;
141b8ab815SErez Zadok blkcnt_t i_blocks;
151b8ab815SErez Zadok
161b8ab815SErez Zadok /*
171b8ab815SErez Zadok * i_size_read() includes its own seqlocking and protection from
181b8ab815SErez Zadok * preemption (see include/linux/fs.h): we need nothing extra for
191b8ab815SErez Zadok * that here, and prefer to avoid nesting locks than attempt to keep
201b8ab815SErez Zadok * i_size and i_blocks in sync together.
211b8ab815SErez Zadok */
221b8ab815SErez Zadok i_size = i_size_read(src);
231b8ab815SErez Zadok
241b8ab815SErez Zadok /*
2572deb455SChristoph Hellwig * But on 32-bit, we ought to make an effort to keep the two halves of
262496396fSThomas Gleixner * i_blocks in sync despite SMP or PREEMPTION - though stat's
2772deb455SChristoph Hellwig * generic_fillattr() doesn't bother, and we won't be applying quotas
2872deb455SChristoph Hellwig * (where i_blocks does become important) at the upper level.
291b8ab815SErez Zadok *
301b8ab815SErez Zadok * We don't actually know what locking is used at the lower level;
311b8ab815SErez Zadok * but if it's a filesystem that supports quotas, it will be using
3231475dd6SHugh Dickins * i_lock as in inode_add_bytes().
331b8ab815SErez Zadok */
341b8ab815SErez Zadok if (sizeof(i_blocks) > sizeof(long))
351b8ab815SErez Zadok spin_lock(&src->i_lock);
361b8ab815SErez Zadok i_blocks = src->i_blocks;
371b8ab815SErez Zadok if (sizeof(i_blocks) > sizeof(long))
381b8ab815SErez Zadok spin_unlock(&src->i_lock);
391b8ab815SErez Zadok
401b8ab815SErez Zadok /*
412496396fSThomas Gleixner * If CONFIG_SMP or CONFIG_PREEMPTION on 32-bit, it's vital for
421b8ab815SErez Zadok * fsstack_copy_inode_size() to hold some lock around
431b8ab815SErez Zadok * i_size_write(), otherwise i_size_read() may spin forever (see
441b8ab815SErez Zadok * include/linux/fs.h). We don't necessarily hold i_mutex when this
451b8ab815SErez Zadok * is called, so take i_lock for that case.
461b8ab815SErez Zadok *
4772deb455SChristoph Hellwig * And if on 32-bit, continue our effort to keep the two halves of
482496396fSThomas Gleixner * i_blocks in sync despite SMP or PREEMPTION: use i_lock for that case
4972deb455SChristoph Hellwig * too, and do both at once by combining the tests.
501b8ab815SErez Zadok *
511b8ab815SErez Zadok * There is none of this locking overhead in the 64-bit case.
521b8ab815SErez Zadok */
531b8ab815SErez Zadok if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
541b8ab815SErez Zadok spin_lock(&dst->i_lock);
551b8ab815SErez Zadok i_size_write(dst, i_size);
561b8ab815SErez Zadok dst->i_blocks = i_blocks;
571b8ab815SErez Zadok if (sizeof(i_size) > sizeof(long) || sizeof(i_blocks) > sizeof(long))
581b8ab815SErez Zadok spin_unlock(&dst->i_lock);
5942cf1193SJosef "Jeff" Sipek }
6042cf1193SJosef "Jeff" Sipek EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
6142cf1193SJosef "Jeff" Sipek
629afa2fb6SErez Zadok /* copy all attributes */
fsstack_copy_attr_all(struct inode * dest,const struct inode * src)639afa2fb6SErez Zadok void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
6442cf1193SJosef "Jeff" Sipek {
6542cf1193SJosef "Jeff" Sipek dest->i_mode = src->i_mode;
6642cf1193SJosef "Jeff" Sipek dest->i_uid = src->i_uid;
6742cf1193SJosef "Jeff" Sipek dest->i_gid = src->i_gid;
6842cf1193SJosef "Jeff" Sipek dest->i_rdev = src->i_rdev;
6942cf1193SJosef "Jeff" Sipek dest->i_atime = src->i_atime;
7042cf1193SJosef "Jeff" Sipek dest->i_mtime = src->i_mtime;
71*2276e5baSJeff Layton inode_set_ctime_to_ts(dest, inode_get_ctime(src));
7242cf1193SJosef "Jeff" Sipek dest->i_blkbits = src->i_blkbits;
7342cf1193SJosef "Jeff" Sipek dest->i_flags = src->i_flags;
74bfe86848SMiklos Szeredi set_nlink(dest, src->i_nlink);
7542cf1193SJosef "Jeff" Sipek }
7642cf1193SJosef "Jeff" Sipek EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
77