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