Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 3 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 4ce3121 | 2005-11-02 14:59:41 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 9 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_quota.h" |
| 13 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xfs_qm.h" |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 19 | STATIC void |
| 20 | xfs_fill_statvfs_from_dquot( |
Christoph Hellwig | 00dd402 | 2008-11-28 14:23:36 +1100 | [diff] [blame] | 21 | struct kstatfs *statp, |
Christoph Hellwig | 8960501 | 2012-02-20 02:28:17 +0000 | [diff] [blame] | 22 | struct xfs_dquot *dqp) |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 23 | { |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 24 | uint64_t limit; |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 25 | |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 26 | limit = dqp->q_blk.softlimit ? |
| 27 | dqp->q_blk.softlimit : |
| 28 | dqp->q_blk.hardlimit; |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 29 | if (limit && statp->f_blocks > limit) { |
| 30 | statp->f_blocks = limit; |
Christoph Hellwig | 9b00f30 | 2010-01-21 11:17:20 +0000 | [diff] [blame] | 31 | statp->f_bfree = statp->f_bavail = |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 32 | (statp->f_blocks > dqp->q_blk.reserved) ? |
| 33 | (statp->f_blocks - dqp->q_blk.reserved) : 0; |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 34 | } |
Christoph Hellwig | 2a293b7 | 2006-07-28 17:04:26 +1000 | [diff] [blame] | 35 | |
Darrick J. Wong | d3537cf | 2020-07-14 10:37:31 -0700 | [diff] [blame] | 36 | limit = dqp->q_ino.softlimit ? |
| 37 | dqp->q_ino.softlimit : |
| 38 | dqp->q_ino.hardlimit; |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 39 | if (limit && statp->f_files > limit) { |
| 40 | statp->f_files = limit; |
Christoph Hellwig | 2a293b7 | 2006-07-28 17:04:26 +1000 | [diff] [blame] | 41 | statp->f_ffree = |
Darrick J. Wong | 784e80f | 2020-07-14 10:37:30 -0700 | [diff] [blame] | 42 | (statp->f_files > dqp->q_ino.reserved) ? |
| 43 | (statp->f_files - dqp->q_ino.reserved) : 0; |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 44 | } |
Nathan Scott | 932f2c3 | 2006-06-09 15:29:58 +1000 | [diff] [blame] | 45 | } |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 48 | /* |
| 49 | * Directory tree accounting is implemented using project quotas, where |
| 50 | * the project identifier is inherited from parent directories. |
| 51 | * A statvfs (df, etc.) of a directory that is using project quota should |
| 52 | * return a statvfs of the project, not the entire filesystem. |
| 53 | * This makes such trees appear as if they are filesystems in themselves. |
| 54 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 55 | void |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 56 | xfs_qm_statvfs( |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 57 | struct xfs_inode *ip, |
Christoph Hellwig | 00dd402 | 2008-11-28 14:23:36 +1100 | [diff] [blame] | 58 | struct kstatfs *statp) |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 59 | { |
Pavel Reichl | aefe69a | 2019-11-12 17:04:02 -0800 | [diff] [blame] | 60 | struct xfs_mount *mp = ip->i_mount; |
| 61 | struct xfs_dquot *dqp; |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 62 | |
Christoph Hellwig | ceaf603 | 2021-03-29 11:11:39 -0700 | [diff] [blame] | 63 | if (!xfs_qm_dqget(mp, ip->i_projid, XFS_DQTYPE_PROJ, false, &dqp)) { |
Christoph Hellwig | 8960501 | 2012-02-20 02:28:17 +0000 | [diff] [blame] | 64 | xfs_fill_statvfs_from_dquot(statp, dqp); |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 65 | xfs_qm_dqput(dqp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 69 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | xfs_qm_newmount( |
| 71 | xfs_mount_t *mp, |
| 72 | uint *needquotamount, |
| 73 | uint *quotaflags) |
| 74 | { |
| 75 | uint quotaondisk; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 76 | uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Dave Chinner | 38c26bf | 2021-08-18 18:46:37 -0700 | [diff] [blame] | 78 | quotaondisk = xfs_has_quota(mp) && |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 79 | (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | if (quotaondisk) { |
| 82 | uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT; |
Nathan Scott | c8ad20f | 2005-06-21 15:38:48 +1000 | [diff] [blame] | 83 | pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * If the device itself is read-only, we can't allow |
| 89 | * the user to change the state of quota on the mount - |
| 90 | * this would generate a transaction on the ro device, |
| 91 | * which would lead to an I/O error and shutdown |
| 92 | */ |
| 93 | |
| 94 | if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) || |
| 95 | (!uquotaondisk && XFS_IS_UQUOTA_ON(mp)) || |
| 96 | (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) || |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 97 | (!gquotaondisk && XFS_IS_GQUOTA_ON(mp)) || |
| 98 | (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) || |
| 99 | (!pquotaondisk && XFS_IS_PQUOTA_ON(mp))) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | xfs_dev_is_read_only(mp, "changing quota state")) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 101 | xfs_warn(mp, "please mount with%s%s%s%s.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | (!quotaondisk ? "out quota" : ""), |
| 103 | (uquotaondisk ? " usrquota" : ""), |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 104 | (gquotaondisk ? " grpquota" : ""), |
| 105 | (pquotaondisk ? " prjquota" : "")); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 106 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (XFS_IS_QUOTA_ON(mp) || quotaondisk) { |
| 110 | /* |
| 111 | * Call mount_quotas at this point only if we won't have to do |
| 112 | * a quotacheck. |
| 113 | */ |
| 114 | if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) { |
| 115 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 116 | * If an error occurred, qm_mount_quotas code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | * has already disabled quotas. So, just finish |
| 118 | * mounting, and get on with the boring life |
| 119 | * without disk quotas. |
| 120 | */ |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 121 | xfs_qm_mount_quotas(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } else { |
| 123 | /* |
| 124 | * Clear the quota flags, but remember them. This |
| 125 | * is so that the quota code doesn't get invoked |
| 126 | * before we're ready. This can happen when an |
| 127 | * inode goes inactive and wants to free blocks, |
| 128 | * or via xfs_log_mount_finish. |
| 129 | */ |
Thiago Farina | 667a929 | 2012-11-12 21:32:59 -0200 | [diff] [blame] | 130 | *needquotamount = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | *quotaflags = mp->m_qflags; |
| 132 | mp->m_qflags = 0; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |