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