stack.c 2.5 KB

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