xfs_qm_bhv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_quota.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_qm.h"
  17. STATIC void
  18. xfs_fill_statvfs_from_dquot(
  19. struct kstatfs *statp,
  20. struct xfs_dquot *dqp)
  21. {
  22. uint64_t limit;
  23. limit = dqp->q_blk.softlimit ?
  24. dqp->q_blk.softlimit :
  25. dqp->q_blk.hardlimit;
  26. if (limit && statp->f_blocks > limit) {
  27. statp->f_blocks = limit;
  28. statp->f_bfree = statp->f_bavail =
  29. (statp->f_blocks > dqp->q_blk.reserved) ?
  30. (statp->f_blocks - dqp->q_blk.reserved) : 0;
  31. }
  32. limit = dqp->q_ino.softlimit ?
  33. dqp->q_ino.softlimit :
  34. dqp->q_ino.hardlimit;
  35. if (limit && statp->f_files > limit) {
  36. statp->f_files = limit;
  37. statp->f_ffree =
  38. (statp->f_files > dqp->q_ino.reserved) ?
  39. (statp->f_files - dqp->q_ino.reserved) : 0;
  40. }
  41. }
  42. /*
  43. * Directory tree accounting is implemented using project quotas, where
  44. * the project identifier is inherited from parent directories.
  45. * A statvfs (df, etc.) of a directory that is using project quota should
  46. * return a statvfs of the project, not the entire filesystem.
  47. * This makes such trees appear as if they are filesystems in themselves.
  48. */
  49. void
  50. xfs_qm_statvfs(
  51. struct xfs_inode *ip,
  52. struct kstatfs *statp)
  53. {
  54. struct xfs_mount *mp = ip->i_mount;
  55. struct xfs_dquot *dqp;
  56. if (!xfs_qm_dqget(mp, ip->i_d.di_projid, XFS_DQTYPE_PROJ, false, &dqp)) {
  57. xfs_fill_statvfs_from_dquot(statp, dqp);
  58. xfs_qm_dqput(dqp);
  59. }
  60. }
  61. int
  62. xfs_qm_newmount(
  63. xfs_mount_t *mp,
  64. uint *needquotamount,
  65. uint *quotaflags)
  66. {
  67. uint quotaondisk;
  68. uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
  69. quotaondisk = xfs_sb_version_hasquota(&mp->m_sb) &&
  70. (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
  71. if (quotaondisk) {
  72. uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
  73. pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
  74. gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
  75. }
  76. /*
  77. * If the device itself is read-only, we can't allow
  78. * the user to change the state of quota on the mount -
  79. * this would generate a transaction on the ro device,
  80. * which would lead to an I/O error and shutdown
  81. */
  82. if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
  83. (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) ||
  84. (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
  85. (!gquotaondisk && XFS_IS_GQUOTA_ON(mp)) ||
  86. (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
  87. (!pquotaondisk && XFS_IS_PQUOTA_ON(mp))) &&
  88. xfs_dev_is_read_only(mp, "changing quota state")) {
  89. xfs_warn(mp, "please mount with%s%s%s%s.",
  90. (!quotaondisk ? "out quota" : ""),
  91. (uquotaondisk ? " usrquota" : ""),
  92. (gquotaondisk ? " grpquota" : ""),
  93. (pquotaondisk ? " prjquota" : ""));
  94. return -EPERM;
  95. }
  96. if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
  97. /*
  98. * Call mount_quotas at this point only if we won't have to do
  99. * a quotacheck.
  100. */
  101. if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
  102. /*
  103. * If an error occurred, qm_mount_quotas code
  104. * has already disabled quotas. So, just finish
  105. * mounting, and get on with the boring life
  106. * without disk quotas.
  107. */
  108. xfs_qm_mount_quotas(mp);
  109. } else {
  110. /*
  111. * Clear the quota flags, but remember them. This
  112. * is so that the quota code doesn't get invoked
  113. * before we're ready. This can happen when an
  114. * inode goes inactive and wants to free blocks,
  115. * or via xfs_log_mount_finish.
  116. */
  117. *needquotamount = true;
  118. *quotaflags = mp->m_qflags;
  119. mp->m_qflags = 0;
  120. }
  121. }
  122. return 0;
  123. }