xref: /linux/fs/xfs/xfs_qm_bhv.c (revision df2e3152f1cb798ed8ffa7e488c50261e6dc50e3)
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_mount.h"
13 #include "xfs_quota.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_qm.h"
17 
18 
19 STATIC void
20 xfs_fill_statvfs_from_dquot(
21 	struct kstatfs		*statp,
22 	struct xfs_inode	*ip,
23 	struct xfs_dquot	*dqp)
24 {
25 	struct xfs_dquot_res	*blkres = &dqp->q_blk;
26 	uint64_t		limit;
27 
28 	if (XFS_IS_REALTIME_MOUNT(ip->i_mount) &&
29 	    (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME)))
30 		blkres = &dqp->q_rtb;
31 
32 	limit = blkres->softlimit ?
33 		blkres->softlimit :
34 		blkres->hardlimit;
35 	if (limit) {
36 		uint64_t	remaining = 0;
37 
38 		if (limit > blkres->reserved)
39 			remaining = limit - blkres->reserved;
40 
41 		statp->f_blocks = min(statp->f_blocks, limit);
42 		statp->f_bfree = min(statp->f_bfree, remaining);
43 	}
44 
45 	limit = dqp->q_ino.softlimit ?
46 		dqp->q_ino.softlimit :
47 		dqp->q_ino.hardlimit;
48 	if (limit) {
49 		uint64_t	remaining = 0;
50 
51 		if (limit > dqp->q_ino.reserved)
52 			remaining = limit - dqp->q_ino.reserved;
53 
54 		statp->f_files = min(statp->f_files, limit);
55 		statp->f_ffree = min(statp->f_ffree, remaining);
56 	}
57 }
58 
59 
60 /*
61  * Directory tree accounting is implemented using project quotas, where
62  * the project identifier is inherited from parent directories.
63  * A statvfs (df, etc.) of a directory that is using project quota should
64  * return a statvfs of the project, not the entire filesystem.
65  * This makes such trees appear as if they are filesystems in themselves.
66  */
67 void
68 xfs_qm_statvfs(
69 	struct xfs_inode	*ip,
70 	struct kstatfs		*statp)
71 {
72 	struct xfs_mount	*mp = ip->i_mount;
73 	struct xfs_dquot	*dqp;
74 
75 	if (!xfs_qm_dqget(mp, ip->i_projid, XFS_DQTYPE_PROJ, false, &dqp)) {
76 		xfs_fill_statvfs_from_dquot(statp, ip, dqp);
77 		xfs_qm_dqput(dqp);
78 	}
79 }
80 
81 int
82 xfs_qm_newmount(
83 	xfs_mount_t	*mp,
84 	uint		*needquotamount,
85 	uint		*quotaflags)
86 {
87 	uint		quotaondisk;
88 	uint		uquotaondisk = 0, gquotaondisk = 0, pquotaondisk = 0;
89 
90 	quotaondisk = xfs_has_quota(mp) &&
91 				(mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT);
92 
93 	if (quotaondisk) {
94 		uquotaondisk = mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT;
95 		pquotaondisk = mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT;
96 		gquotaondisk = mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT;
97 	}
98 
99 	/*
100 	 * If the device itself is read-only, we can't allow
101 	 * the user to change the state of quota on the mount -
102 	 * this would generate a transaction on the ro device,
103 	 * which would lead to an I/O error and shutdown
104 	 */
105 
106 	if (((uquotaondisk && !XFS_IS_UQUOTA_ON(mp)) ||
107 	    (!uquotaondisk &&  XFS_IS_UQUOTA_ON(mp)) ||
108 	     (gquotaondisk && !XFS_IS_GQUOTA_ON(mp)) ||
109 	    (!gquotaondisk &&  XFS_IS_GQUOTA_ON(mp)) ||
110 	     (pquotaondisk && !XFS_IS_PQUOTA_ON(mp)) ||
111 	    (!pquotaondisk &&  XFS_IS_PQUOTA_ON(mp)))  &&
112 	    xfs_dev_is_read_only(mp, "changing quota state")) {
113 		xfs_warn(mp, "please mount with%s%s%s%s.",
114 			(!quotaondisk ? "out quota" : ""),
115 			(uquotaondisk ? " usrquota" : ""),
116 			(gquotaondisk ? " grpquota" : ""),
117 			(pquotaondisk ? " prjquota" : ""));
118 		return -EPERM;
119 	}
120 
121 	if (XFS_IS_QUOTA_ON(mp) || quotaondisk) {
122 		/*
123 		 * Call mount_quotas at this point only if we won't have to do
124 		 * a quotacheck.
125 		 */
126 		if (quotaondisk && !XFS_QM_NEED_QUOTACHECK(mp)) {
127 			/*
128 			 * If an error occurred, qm_mount_quotas code
129 			 * has already disabled quotas. So, just finish
130 			 * mounting, and get on with the boring life
131 			 * without disk quotas.
132 			 */
133 			xfs_qm_mount_quotas(mp);
134 		} else {
135 			/*
136 			 * Clear the quota flags, but remember them. This
137 			 * is so that the quota code doesn't get invoked
138 			 * before we're ready. This can happen when an
139 			 * inode goes inactive and wants to free blocks,
140 			 * or via xfs_log_mount_finish.
141 			 */
142 			*needquotamount = true;
143 			*quotaflags = mp->m_qflags;
144 			mp->m_qflags = 0;
145 		}
146 	}
147 
148 	return 0;
149 }
150 
151 /*
152  * If the sysadmin didn't provide any quota mount options, restore the quota
153  * accounting and enforcement state from the ondisk superblock.  Only do this
154  * for metadir filesystems because this is a behavior change.
155  */
156 void
157 xfs_qm_resume_quotaon(
158 	struct xfs_mount	*mp)
159 {
160 	if (!xfs_has_metadir(mp))
161 		return;
162 	if (xfs_has_norecovery(mp))
163 		return;
164 
165 	mp->m_qflags = mp->m_sb.sb_qflags & (XFS_ALL_QUOTA_ACCT |
166 					     XFS_ALL_QUOTA_ENFD);
167 }
168