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