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