stack.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/fs_stack.h>
  4. /* does _NOT_ require i_mutex to be held.
  5. *
  6. * This function cannot be inlined since i_size_{read,write} is rather
  7. * heavy-weight on 32-bit systems
  8. */
  9. void fsstack_copy_inode_size(struct inode *dst, struct inode *src)
  10. {
  11. loff_t i_size;
  12. blkcnt_t i_blocks;
  13. /*
  14. * i_size_read() includes its own seqlocking and protection from
  15. * preemption (see include/linux/fs.h): we need nothing extra for
  16. * that here, and prefer to avoid nesting locks than attempt to keep
  17. * i_size and i_blocks in sync together.
  18. */
  19. i_size = i_size_read(src);
  20. /*
  21. * But if CONFIG_LBDAF (on 32-bit), we ought to make an effort to
  22. * keep the two halves of i_blocks in sync despite SMP or PREEMPT -
  23. * though stat's generic_fillattr() doesn't bother, and we won't be
  24. * applying quotas (where i_blocks does become important) at the
  25. * 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(). tmpfs uses other locking, and
  30. * its 32-bit is (just) able to exceed 2TB i_size with the aid of
  31. * holes; but its i_blocks cannot carry into the upper long without
  32. * almost 2TB swap - let's ignore that case.
  33. */
  34. if (sizeof(i_blocks) > sizeof(long))
  35. spin_lock(&src->i_lock);
  36. i_blocks = src->i_blocks;
  37. if (sizeof(i_blocks) > sizeof(long))
  38. spin_unlock(&src->i_lock);
  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. *
  46. * And if CONFIG_LBADF (on 32-bit), continue our effort to keep the
  47. * two halves of i_blocks in sync despite SMP or PREEMPT: use i_lock
  48. * for that case too, and do both at once by combining the tests.
  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);
  58. }
  59. EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
  60. /* copy all attributes */
  61. void fsstack_copy_attr_all(struct inode *dest, const struct inode *src)
  62. {
  63. dest->i_mode = src->i_mode;
  64. dest->i_uid = src->i_uid;
  65. dest->i_gid = src->i_gid;
  66. dest->i_rdev = src->i_rdev;
  67. dest->i_atime = src->i_atime;
  68. dest->i_mtime = src->i_mtime;
  69. dest->i_ctime = src->i_ctime;
  70. dest->i_blkbits = src->i_blkbits;
  71. dest->i_flags = src->i_flags;
  72. dest->i_nlink = src->i_nlink;
  73. }
  74. EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);