blob: b77673dd05581eaf36db6f8723b3eb87c1016230 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott932f2c32006-06-09 15:29:58 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott4ce31212005-11-02 14:59:41 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
7#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07008#include "xfs_shared.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +10009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_quota.h"
13#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_qm.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Christoph Hellwigb09cc772007-08-30 17:19:57 +100019STATIC void
20xfs_fill_statvfs_from_dquot(
Christoph Hellwig00dd4022008-11-28 14:23:36 +110021 struct kstatfs *statp,
Christoph Hellwig89605012012-02-20 02:28:17 +000022 struct xfs_dquot *dqp)
Nathan Scott932f2c32006-06-09 15:29:58 +100023{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070024 uint64_t limit;
Nathan Scott932f2c32006-06-09 15:29:58 +100025
Darrick J. Wongd3537cf2020-07-14 10:37:31 -070026 limit = dqp->q_blk.softlimit ?
27 dqp->q_blk.softlimit :
28 dqp->q_blk.hardlimit;
Nathan Scott932f2c32006-06-09 15:29:58 +100029 if (limit && statp->f_blocks > limit) {
30 statp->f_blocks = limit;
Christoph Hellwig9b00f302010-01-21 11:17:20 +000031 statp->f_bfree = statp->f_bavail =
Darrick J. Wong784e80f2020-07-14 10:37:30 -070032 (statp->f_blocks > dqp->q_blk.reserved) ?
33 (statp->f_blocks - dqp->q_blk.reserved) : 0;
Nathan Scott932f2c32006-06-09 15:29:58 +100034 }
Christoph Hellwig2a293b72006-07-28 17:04:26 +100035
Darrick J. Wongd3537cf2020-07-14 10:37:31 -070036 limit = dqp->q_ino.softlimit ?
37 dqp->q_ino.softlimit :
38 dqp->q_ino.hardlimit;
Nathan Scott932f2c32006-06-09 15:29:58 +100039 if (limit && statp->f_files > limit) {
40 statp->f_files = limit;
Christoph Hellwig2a293b72006-07-28 17:04:26 +100041 statp->f_ffree =
Darrick J. Wong784e80f2020-07-14 10:37:30 -070042 (statp->f_files > dqp->q_ino.reserved) ?
43 (statp->f_files - dqp->q_ino.reserved) : 0;
Nathan Scott932f2c32006-06-09 15:29:58 +100044 }
Nathan Scott932f2c32006-06-09 15:29:58 +100045}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Christoph Hellwigb09cc772007-08-30 17:19:57 +100048/*
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 Hellwig7d095252009-06-08 15:33:32 +020055void
Christoph Hellwigb09cc772007-08-30 17:19:57 +100056xfs_qm_statvfs(
Pavel Reichlaefe69a2019-11-12 17:04:02 -080057 struct xfs_inode *ip,
Christoph Hellwig00dd4022008-11-28 14:23:36 +110058 struct kstatfs *statp)
Christoph Hellwigb09cc772007-08-30 17:19:57 +100059{
Pavel Reichlaefe69a2019-11-12 17:04:02 -080060 struct xfs_mount *mp = ip->i_mount;
61 struct xfs_dquot *dqp;
Christoph Hellwigb09cc772007-08-30 17:19:57 +100062
Christoph Hellwigceaf6032021-03-29 11:11:39 -070063 if (!xfs_qm_dqget(mp, ip->i_projid, XFS_DQTYPE_PROJ, false, &dqp)) {
Christoph Hellwig89605012012-02-20 02:28:17 +000064 xfs_fill_statvfs_from_dquot(statp, dqp);
Christoph Hellwigb09cc772007-08-30 17:19:57 +100065 xfs_qm_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Christoph Hellwig7d095252009-06-08 15:33:32 +020069int
Linus Torvalds1da177e2005-04-16 15:20:36 -070070xfs_qm_newmount(
71 xfs_mount_t *mp,
72 uint *needquotamount,
73 uint *quotaflags)
74{
75 uint quotaondisk;
Nathan Scottc8ad20f2005-06-21 15:38:48 +100076 uint uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Dave Chinner38c26bf2021-08-18 18:46:37 -070078 quotaondisk = xfs_has_quota(mp) &&
Nathan Scottc8ad20f2005-06-21 15:38:48 +100079 (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 if (quotaondisk) {
82 uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +100083 pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 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 Seetharaman92f8ff72013-07-11 00:00:40 -050097 (!gquotaondisk && XFS_IS_GQUOTA_ON(mp)) ||
98 (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
99 (!pquotaondisk && XFS_IS_PQUOTA_ON(mp))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 xfs_dev_is_read_only(mp, "changing quota state")) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100101 xfs_warn(mp, "please mount with%s%s%s%s.",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 (!quotaondisk ? "out quota" : ""),
103 (uquotaondisk ? " usrquota" : ""),
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500104 (gquotaondisk ? " grpquota" : ""),
105 (pquotaondisk ? " prjquota" : ""));
Dave Chinner24513372014-06-25 14:58:08 +1000106 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
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 Marchi25985ed2011-03-30 22:57:33 -0300116 * If an error occurred, qm_mount_quotas code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * has already disabled quotas. So, just finish
118 * mounting, and get on with the boring life
119 * without disk quotas.
120 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000121 xfs_qm_mount_quotas(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 } 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 Farina667a9292012-11-12 21:32:59 -0200130 *needquotamount = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *quotaflags = mp->m_qflags;
132 mp->m_qflags = 0;
133 }
134 }
135
136 return 0;
137}