blob: b7a8f5665d7b2d9ce22313c1492d23869ae5e955 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
19#include <linux/capability.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs.h"
22#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110023#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110024#include "xfs_format.h"
25#include "xfs_log_format.h"
26#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110033#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000035#include "xfs_trace.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110036#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
39STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
40 uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041STATIC uint xfs_qm_export_flags(uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042STATIC uint xfs_qm_export_qtype_flags(uint);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Turn off quota accounting and/or enforcement for all udquots and/or
46 * gdquots. Called only at unmount time.
47 *
48 * This assumes that there are no dquots of this file system cached
49 * incore, and modifies the ondisk dquot directly. Therefore, for example,
50 * it is an error to call this twice, without purging the cache.
51 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +010052int
Linus Torvalds1da177e2005-04-16 15:20:36 -070053xfs_qm_scall_quotaoff(
54 xfs_mount_t *mp,
Christoph Hellwigfcafb712009-02-09 08:47:34 +010055 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +100057 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 uint dqtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int error;
60 uint inactivate_flags;
61 xfs_qoff_logitem_t *qoffstart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /*
64 * No file system can have quotas enabled on disk but not in core.
65 * Note that quota utilities (like quotaoff) _expect_
Dave Chinner24513372014-06-25 14:58:08 +100066 * errno == -EEXIST here.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68 if ((mp->m_qflags & flags) == 0)
Dave Chinner24513372014-06-25 14:58:08 +100069 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 error = 0;
71
72 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
73
74 /*
75 * We don't want to deal with two quotaoffs messing up each other,
76 * so we're going to serialize it. quotaoff isn't exactly a performance
77 * critical thing.
78 * If quotaoff, then we must be dealing with the root filesystem.
79 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +100080 ASSERT(q);
81 mutex_lock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /*
84 * If we're just turning off quota enforcement, change mp and go.
85 */
86 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
87 mp->m_qflags &= ~(flags);
88
Eric Sandeen3685c2a2007-10-11 17:42:32 +100089 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 mp->m_sb.sb_qflags = mp->m_qflags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +100091 spin_unlock(&mp->m_sb_lock);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +100092 mutex_unlock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* XXX what to do if error ? Revert back to old vals incore ? */
95 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
Eric Sandeend99831f2014-06-22 15:03:54 +100096 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
99 dqtype = 0;
100 inactivate_flags = 0;
101 /*
102 * If accounting is off, we must turn enforcement off, clear the
103 * quota 'CHKD' certificate to make it known that we have to
104 * do a quotacheck the next time this quota is turned on.
105 */
106 if (flags & XFS_UQUOTA_ACCT) {
107 dqtype |= XFS_QMOPT_UQUOTA;
108 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
109 inactivate_flags |= XFS_UQUOTA_ACTIVE;
110 }
111 if (flags & XFS_GQUOTA_ACCT) {
112 dqtype |= XFS_QMOPT_GQUOTA;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500113 flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 inactivate_flags |= XFS_GQUOTA_ACTIVE;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500115 }
116 if (flags & XFS_PQUOTA_ACCT) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000117 dqtype |= XFS_QMOPT_PQUOTA;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500118 flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000119 inactivate_flags |= XFS_PQUOTA_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
122 /*
123 * Nothing to do? Don't complain. This happens when we're just
124 * turning off quota enforcement.
125 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000126 if ((mp->m_qflags & flags) == 0)
127 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /*
130 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
David Chinnercb6edc22008-04-10 12:20:45 +1000131 * and synchronously. If we fail to write, we should abort the
132 * operation as it cannot be recovered safely if we crash.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
David Chinnercb6edc22008-04-10 12:20:45 +1000134 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
135 if (error)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000136 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /*
139 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
140 * to take care of the race between dqget and quotaoff. We don't take
141 * any special locks to reset these bits. All processes need to check
142 * these bits *after* taking inode lock(s) to see if the particular
143 * quota type is in the process of being turned off. If *ACTIVE, it is
144 * guaranteed that all dquot structures and all quotainode ptrs will all
145 * stay valid as long as that inode is kept locked.
146 *
147 * There is no turning back after this.
148 */
149 mp->m_qflags &= ~inactivate_flags;
150
151 /*
152 * Give back all the dquot reference(s) held by inodes.
153 * Here we go thru every single incore inode in this file system, and
154 * do a dqrele on the i_udquot/i_gdquot that it may have.
155 * Essentially, as long as somebody has an inode locked, this guarantees
156 * that quotas will not be turned off. This is handy because in a
157 * transaction once we lock the inode(s) and check for quotaon, we can
158 * depend on the quota inodes (and other things) being valid as long as
159 * we keep the lock(s).
160 */
161 xfs_qm_dqrele_all_inodes(mp, flags);
162
163 /*
164 * Next we make the changes in the quota flag in the mount struct.
165 * This isn't protected by a particular lock directly, because we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166 * don't want to take a mrlock every time we depend on quotas being on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500168 mp->m_qflags &= ~flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
171 * Go through all the dquots of this file system and purge them,
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500172 * according to what was turned off.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Christoph Hellwigb84a3a92012-03-14 11:53:34 -0500174 xfs_qm_dqpurge_all(mp, dqtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /*
177 * Transactions that had started before ACTIVE state bit was cleared
178 * could have logged many dquots, so they'd have higher LSNs than
179 * the first QUOTAOFF log record does. If we happen to crash when
180 * the tail of the log has gone past the QUOTAOFF record, but
181 * before the last dquot modification, those dquots __will__
182 * recover, and that's not good.
183 *
184 * So, we have QUOTAOFF start and end logitems; the start
185 * logitem won't get overwritten until the end logitem appears...
186 */
David Chinnercb6edc22008-04-10 12:20:45 +1000187 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
188 if (error) {
189 /* We're screwed now. Shutdown is the only option. */
190 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000191 goto out_unlock;
David Chinnercb6edc22008-04-10 12:20:45 +1000192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
Chandra Seetharamanc31ad432013-07-10 18:00:36 -0500195 * If all quotas are completely turned off, close shop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Chandra Seetharamanc31ad432013-07-10 18:00:36 -0500197 if (mp->m_qflags == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000198 mutex_unlock(&q->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 xfs_qm_destroy_quotainfo(mp);
Eric Sandeend99831f2014-06-22 15:03:54 +1000200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 /*
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000204 * Release our quotainode references if we don't need them anymore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000206 if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
207 IRELE(q->qi_uquotaip);
208 q->qi_uquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500210 if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000211 IRELE(q->qi_gquotaip);
212 q->qi_gquotaip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500214 if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
215 IRELE(q->qi_pquotaip);
216 q->qi_pquotaip = NULL;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000219out_unlock:
220 mutex_unlock(&q->qi_quotaofflock);
221 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Christoph Hellwig5d188982010-07-20 17:51:31 +1000224STATIC int
225xfs_qm_scall_trunc_qfile(
226 struct xfs_mount *mp,
227 xfs_ino_t ino)
228{
229 struct xfs_inode *ip;
230 struct xfs_trans *tp;
231 int error;
232
233 if (ino == NULLFSINO)
234 return 0;
235
236 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
237 if (error)
238 return error;
239
240 xfs_ilock(ip, XFS_IOLOCK_EXCL);
241
242 tp = xfs_trans_alloc(mp, XFS_TRANS_TRUNCATE_FILE);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000243 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000244 if (error) {
245 xfs_trans_cancel(tp, 0);
246 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
247 goto out_put;
248 }
249
250 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000251 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000252
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000253 ip->i_d.di_size = 0;
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000254 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
255
256 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000257 if (error) {
258 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
259 XFS_TRANS_ABORT);
260 goto out_unlock;
261 }
262
Christoph Hellwig673e8e52011-12-18 20:00:04 +0000263 ASSERT(ip->i_d.di_nextents == 0);
264
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000265 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Christoph Hellwig5d188982010-07-20 17:51:31 +1000266 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
267
268out_unlock:
269 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
270out_put:
271 IRELE(ip);
272 return error;
273}
274
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100275int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276xfs_qm_scall_trunc_qfiles(
277 xfs_mount_t *mp,
278 uint flags)
279{
Dave Chinner24513372014-06-25 14:58:08 +1000280 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Eric Sandeenf58522c2014-05-05 17:27:06 +1000282 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
283 (flags & ~XFS_DQ_ALLTYPES)) {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500284 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100285 __func__, flags, mp->m_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Jie Liuc61a9e32013-11-22 14:04:00 +0800289 if (flags & XFS_DQ_USER) {
Christoph Hellwig5d188982010-07-20 17:51:31 +1000290 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
Jie Liuc61a9e32013-11-22 14:04:00 +0800291 if (error)
292 return error;
293 }
294 if (flags & XFS_DQ_GROUP) {
295 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
296 if (error)
297 return error;
298 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500299 if (flags & XFS_DQ_PROJ)
Jie Liuc61a9e32013-11-22 14:04:00 +0800300 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jie Liuc61a9e32013-11-22 14:04:00 +0800302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
306 * Switch on (a given) quota enforcement for a filesystem. This takes
307 * effect immediately.
308 * (Switching on quota accounting must be done at mount time.)
309 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100310int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311xfs_qm_scall_quotaon(
312 xfs_mount_t *mp,
313 uint flags)
314{
315 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 uint qf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 __int64_t sbflags;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
320 /*
321 * Switching on quota accounting must be done at mount time.
322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 flags &= ~(XFS_ALL_QUOTA_ACCT);
324
325 sbflags = 0;
326
327 if (flags == 0) {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500328 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100329 __func__, mp->m_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /*
334 * Can't enforce without accounting. We check the superblock
335 * qflags here instead of m_qflags because rootfs can have
336 * quota acct on ondisk without m_qflags' knowing.
337 */
Jan Karafbf64b32014-10-08 11:52:52 +0200338 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500339 (flags & XFS_UQUOTA_ENFD)) ||
Jan Karafbf64b32014-10-08 11:52:52 +0200340 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500341 (flags & XFS_GQUOTA_ENFD)) ||
Jan Karafbf64b32014-10-08 11:52:52 +0200342 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500343 (flags & XFS_PQUOTA_ENFD))) {
Dave Chinner82211122011-03-07 10:07:35 +1100344 xfs_debug(mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -0500345 "%s: Can't enforce without acct, flags=%x sbflags=%x",
Dave Chinner82211122011-03-07 10:07:35 +1100346 __func__, flags, mp->m_sb.sb_qflags);
Dave Chinner24513372014-06-25 14:58:08 +1000347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300350 * If everything's up to-date incore, then don't waste time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352 if ((mp->m_qflags & flags) == flags)
Dave Chinner24513372014-06-25 14:58:08 +1000353 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /*
356 * Change sb_qflags on disk but not incore mp->qflags
357 * if this is the root filesystem.
358 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000359 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 qf = mp->m_sb.sb_qflags;
361 mp->m_sb.sb_qflags = qf | flags;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000362 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /*
365 * There's nothing to change if it's the same.
366 */
367 if ((qf & flags) == flags && sbflags == 0)
Dave Chinner24513372014-06-25 14:58:08 +1000368 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 sbflags |= XFS_SB_QFLAGS;
370
371 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
Eric Sandeend99831f2014-06-22 15:03:54 +1000372 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * If we aren't trying to switch on quota enforcement, we are done.
375 */
376 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
377 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000378 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
379 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
380 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
Jan Karafbf64b32014-10-08 11:52:52 +0200381 (mp->m_qflags & XFS_GQUOTA_ACCT)))
Eric Sandeend99831f2014-06-22 15:03:54 +1000382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (! XFS_IS_QUOTA_RUNNING(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000385 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * Switch on quota enforcement in core.
389 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000390 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000392 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Eric Sandeend99831f2014-06-22 15:03:54 +1000394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
399 * Return quota status information, such as uquota-off, enforcements, etc.
Chandra Seetharaman5d5e3d52013-08-06 17:27:08 -0500400 * for Q_XGETQSTAT command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100402int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403xfs_qm_scall_getqstat(
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000404 struct xfs_mount *mp,
405 struct fs_quota_stat *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000407 struct xfs_quotainfo *q = mp->m_quotainfo;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500408 struct xfs_inode *uip = NULL;
409 struct xfs_inode *gip = NULL;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500410 struct xfs_inode *pip = NULL;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500411 bool tempuqip = false;
412 bool tempgqip = false;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500413 bool temppqip = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 memset(out, 0, sizeof(fs_quota_stat_t));
416
417 out->qs_version = FS_QSTAT_VERSION;
Eric Sandeen62118702008-03-06 13:44:28 +1100418 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 out->qs_uquota.qfs_ino = NULLFSINO;
420 out->qs_gquota.qfs_ino = NULLFSINO;
Eric Sandeend99831f2014-06-22 15:03:54 +1000421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
425 (XFS_ALL_QUOTA_ACCT|
426 XFS_ALL_QUOTA_ENFD));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000427 if (q) {
428 uip = q->qi_uquotaip;
429 gip = q->qi_gquotaip;
Chandra Seetharamand892d582013-07-19 17:36:02 -0500430 pip = q->qi_pquotaip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
433 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000434 0, 0, &uip) == 0)
Thiago Farina667a9292012-11-12 21:32:59 -0200435 tempuqip = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
438 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000439 0, 0, &gip) == 0)
Thiago Farina667a9292012-11-12 21:32:59 -0200440 tempgqip = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500442 /*
443 * Q_XGETQSTAT doesn't have room for both group and project quotas.
444 * So, allow the project quota values to be copied out only if
445 * there is no group quota information available.
446 */
447 if (!gip) {
448 if (!pip && mp->m_sb.sb_pquotino != NULLFSINO) {
449 if (xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
450 0, 0, &pip) == 0)
451 temppqip = true;
452 }
453 } else
454 pip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (uip) {
Chandra Seetharamand892d582013-07-19 17:36:02 -0500456 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
458 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
459 if (tempuqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100460 IRELE(uip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (gip) {
Chandra Seetharamand892d582013-07-19 17:36:02 -0500464 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
466 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
467 if (tempgqip)
Christoph Hellwig43355092008-03-27 18:01:08 +1100468 IRELE(gip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500470 if (pip) {
471 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
472 out->qs_gquota.qfs_nblks = pip->i_d.di_nblocks;
473 out->qs_gquota.qfs_nextents = pip->i_d.di_nextents;
474 if (temppqip)
475 IRELE(pip);
476 }
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000477 if (q) {
478 out->qs_incoredqs = q->qi_dquots;
479 out->qs_btimelimit = q->qi_btimelimit;
480 out->qs_itimelimit = q->qi_itimelimit;
481 out->qs_rtbtimelimit = q->qi_rtbtimelimit;
482 out->qs_bwarnlimit = q->qi_bwarnlimit;
483 out->qs_iwarnlimit = q->qi_iwarnlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Chandra Seetharaman5d5e3d52013-08-06 17:27:08 -0500488/*
489 * Return quota status information, such as uquota-off, enforcements, etc.
490 * for Q_XGETQSTATV command, to support separate project quota field.
491 */
492int
493xfs_qm_scall_getqstatv(
494 struct xfs_mount *mp,
495 struct fs_quota_statv *out)
496{
497 struct xfs_quotainfo *q = mp->m_quotainfo;
498 struct xfs_inode *uip = NULL;
499 struct xfs_inode *gip = NULL;
500 struct xfs_inode *pip = NULL;
501 bool tempuqip = false;
502 bool tempgqip = false;
503 bool temppqip = false;
504
505 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
506 out->qs_uquota.qfs_ino = NULLFSINO;
507 out->qs_gquota.qfs_ino = NULLFSINO;
508 out->qs_pquota.qfs_ino = NULLFSINO;
Eric Sandeend99831f2014-06-22 15:03:54 +1000509 return 0;
Chandra Seetharaman5d5e3d52013-08-06 17:27:08 -0500510 }
511
512 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
513 (XFS_ALL_QUOTA_ACCT|
514 XFS_ALL_QUOTA_ENFD));
515 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
516 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
517 out->qs_pquota.qfs_ino = mp->m_sb.sb_pquotino;
518
519 if (q) {
520 uip = q->qi_uquotaip;
521 gip = q->qi_gquotaip;
522 pip = q->qi_pquotaip;
523 }
524 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
525 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
526 0, 0, &uip) == 0)
527 tempuqip = true;
528 }
529 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
530 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
531 0, 0, &gip) == 0)
532 tempgqip = true;
533 }
534 if (!pip && mp->m_sb.sb_pquotino != NULLFSINO) {
535 if (xfs_iget(mp, NULL, mp->m_sb.sb_pquotino,
536 0, 0, &pip) == 0)
537 temppqip = true;
538 }
539 if (uip) {
540 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
541 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
542 if (tempuqip)
543 IRELE(uip);
544 }
545
546 if (gip) {
547 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
548 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
549 if (tempgqip)
550 IRELE(gip);
551 }
552 if (pip) {
553 out->qs_pquota.qfs_nblks = pip->i_d.di_nblocks;
554 out->qs_pquota.qfs_nextents = pip->i_d.di_nextents;
555 if (temppqip)
556 IRELE(pip);
557 }
558 if (q) {
559 out->qs_incoredqs = q->qi_dquots;
560 out->qs_btimelimit = q->qi_btimelimit;
561 out->qs_itimelimit = q->qi_itimelimit;
562 out->qs_rtbtimelimit = q->qi_rtbtimelimit;
563 out->qs_bwarnlimit = q->qi_bwarnlimit;
564 out->qs_iwarnlimit = q->qi_iwarnlimit;
565 }
566 return 0;
567}
568
Christoph Hellwigc472b432010-05-06 17:05:17 -0400569#define XFS_DQ_MASK \
570 (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_WARNS_MASK)
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/*
573 * Adjust quota limits, and start/stop timers accordingly.
574 */
Christoph Hellwigfcafb712009-02-09 08:47:34 +0100575int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576xfs_qm_scall_setqlim(
Brian Fosterb1366452013-03-18 10:51:46 -0400577 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xfs_dqid_t id,
579 uint type,
580 fs_disk_quota_t *newlim)
581{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000582 struct xfs_quotainfo *q = mp->m_quotainfo;
Brian Fosterb1366452013-03-18 10:51:46 -0400583 struct xfs_disk_dquot *ddq;
584 struct xfs_dquot *dqp;
585 struct xfs_trans *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int error;
587 xfs_qcnt_t hard, soft;
588
Christoph Hellwigc472b432010-05-06 17:05:17 -0400589 if (newlim->d_fieldmask & ~XFS_DQ_MASK)
Dave Chinner24513372014-06-25 14:58:08 +1000590 return -EINVAL;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400591 if ((newlim->d_fieldmask & XFS_DQ_MASK) == 0)
592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Chinnerf6481672013-05-21 18:02:00 +1000594 /*
595 * We don't want to race with a quotaoff so take the quotaoff lock.
596 * We don't hold an inode lock, so there's nothing else to stop
597 * a quotaoff from happening.
598 */
599 mutex_lock(&q->qi_quotaofflock);
600
601 /*
602 * Get the dquot (locked) before we start, as we need to do a
603 * transaction to allocate it if it doesn't exist. Once we have the
604 * dquot, unlock it so we can start the next transaction safely. We hold
605 * a reference to the dquot, so it's safe to do this unlock/lock without
606 * it being reclaimed in the mean time.
607 */
608 error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp);
609 if (error) {
Dave Chinner24513372014-06-25 14:58:08 +1000610 ASSERT(error != -ENOENT);
Dave Chinnerf6481672013-05-21 18:02:00 +1000611 goto out_unlock;
612 }
613 xfs_dqunlock(dqp);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000616 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_setqlim, 0, 0);
Jeff Liuf0f2df92013-01-28 21:26:49 +0800617 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 xfs_trans_cancel(tp, 0);
Dave Chinnerf6481672013-05-21 18:02:00 +1000619 goto out_rele;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Dave Chinnerf6481672013-05-21 18:02:00 +1000622 xfs_dqlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 xfs_trans_dqjoin(tp, dqp);
624 ddq = &dqp->q_core;
625
626 /*
627 * Make sure that hardlimits are >= soft limits before changing.
628 */
629 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
630 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100631 be64_to_cpu(ddq->d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
633 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100634 be64_to_cpu(ddq->d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100636 ddq->d_blk_hardlimit = cpu_to_be64(hard);
637 ddq->d_blk_softlimit = cpu_to_be64(soft);
Brian Fosterb1366452013-03-18 10:51:46 -0400638 xfs_dquot_set_prealloc_limits(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (id == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000640 q->qi_bhardlimit = hard;
641 q->qi_bsoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 } else {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500644 xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
647 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100648 be64_to_cpu(ddq->d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
650 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100651 be64_to_cpu(ddq->d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100653 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
654 ddq->d_rtb_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (id == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000656 q->qi_rtbhardlimit = hard;
657 q->qi_rtbsoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 } else {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500660 xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
664 (xfs_qcnt_t) newlim->d_ino_hardlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100665 be64_to_cpu(ddq->d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
667 (xfs_qcnt_t) newlim->d_ino_softlimit :
Christoph Hellwig1149d962005-11-02 15:01:12 +1100668 be64_to_cpu(ddq->d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (hard == 0 || hard >= soft) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100670 ddq->d_ino_hardlimit = cpu_to_be64(hard);
671 ddq->d_ino_softlimit = cpu_to_be64(soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (id == 0) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000673 q->qi_ihardlimit = hard;
674 q->qi_isoftlimit = soft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 } else {
Eric Sandeen08e96e12013-10-11 20:59:05 -0500677 xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Nathan Scott754002b2005-06-21 15:49:06 +1000680 /*
681 * Update warnings counter(s) if requested
682 */
683 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100684 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000685 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100686 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000687 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100688 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
Nathan Scott754002b2005-06-21 15:49:06 +1000689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (id == 0) {
691 /*
692 * Timelimits for the super user set the relative time
693 * the other users can be over quota for this file system.
694 * If it is zero a default is used. Ditto for the default
Nathan Scott754002b2005-06-21 15:49:06 +1000695 * soft and hard limit values (already done, above), and
696 * for warnings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
698 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000699 q->qi_btimelimit = newlim->d_btimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100700 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000703 q->qi_itimelimit = newlim->d_itimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100704 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000707 q->qi_rtbtimelimit = newlim->d_rtbtimer;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100708 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Nathan Scott754002b2005-06-21 15:49:06 +1000710 if (newlim->d_fieldmask & FS_DQ_BWARNS)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000711 q->qi_bwarnlimit = newlim->d_bwarns;
Nathan Scott754002b2005-06-21 15:49:06 +1000712 if (newlim->d_fieldmask & FS_DQ_IWARNS)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000713 q->qi_iwarnlimit = newlim->d_iwarns;
Nathan Scott754002b2005-06-21 15:49:06 +1000714 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000715 q->qi_rtbwarnlimit = newlim->d_rtbwarns;
Nathan Scott754002b2005-06-21 15:49:06 +1000716 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * If the user is now over quota, start the timelimit.
719 * The user will not be 'warned'.
720 * Note that we keep the timers ticking, whether enforcement
721 * is on or off. We don't really want to bother with iterating
722 * over all ondisk dquots and turning the timers on/off.
723 */
724 xfs_qm_adjust_dqtimers(mp, ddq);
725 }
726 dqp->dq_flags |= XFS_DQ_DIRTY;
727 xfs_trans_log_dquot(tp, dqp);
728
David Chinnere5720ee2008-04-10 12:21:18 +1000729 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Chinnerf6481672013-05-21 18:02:00 +1000731out_rele:
732 xfs_qm_dqrele(dqp);
733out_unlock:
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000734 mutex_unlock(&q->qi_quotaofflock);
David Chinnere5720ee2008-04-10 12:21:18 +1000735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738STATIC int
739xfs_qm_log_quotaoff_end(
740 xfs_mount_t *mp,
741 xfs_qoff_logitem_t *startqoff,
742 uint flags)
743{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000744 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000746 xfs_qoff_logitem_t *qoffi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
749
Jie Liu3d3c8b52013-08-12 20:49:59 +1000750 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_equotaoff, 0, 0);
Jeff Liu762d7ba2013-01-28 21:27:21 +0800751 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 xfs_trans_cancel(tp, 0);
Eric Sandeend99831f2014-06-22 15:03:54 +1000753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
757 flags & XFS_ALL_QUOTA_ACCT);
758 xfs_trans_log_quotaoff_item(tp, qoffi);
759
760 /*
761 * We have to make sure that the transaction is secure on disk before we
762 * return and actually stop quota accounting. So, make it synchronous.
763 * We don't care about quotoff's performance.
764 */
765 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000766 error = xfs_trans_commit(tp, 0);
Eric Sandeend99831f2014-06-22 15:03:54 +1000767 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770
771STATIC int
772xfs_qm_log_quotaoff(
773 xfs_mount_t *mp,
774 xfs_qoff_logitem_t **qoffstartp,
775 uint flags)
776{
777 xfs_trans_t *tp;
778 int error;
Brian Foster5d45ee12014-11-28 14:00:53 +1100779 xfs_qoff_logitem_t *qoffi;
780
781 *qoffstartp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000784 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_quotaoff, 0, 0);
Brian Foster5d45ee12014-11-28 14:00:53 +1100785 if (error) {
786 xfs_trans_cancel(tp, 0);
787 goto out;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
791 xfs_trans_log_quotaoff_item(tp, qoffi);
792
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000793 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000795 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 xfs_mod_sb(tp, XFS_SB_QFLAGS);
798
799 /*
800 * We have to make sure that the transaction is secure on disk before we
801 * return and actually stop quota accounting. So, make it synchronous.
802 * We don't care about quotoff's performance.
803 */
804 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000805 error = xfs_trans_commit(tp, 0);
Brian Foster5d45ee12014-11-28 14:00:53 +1100806 if (error)
807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *qoffstartp = qoffi;
Brian Foster5d45ee12014-11-28 14:00:53 +1100810out:
Eric Sandeend99831f2014-06-22 15:03:54 +1000811 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814
Christoph Hellwig18535a72012-02-20 02:28:16 +0000815int
816xfs_qm_scall_getquota(
817 struct xfs_mount *mp,
818 xfs_dqid_t id,
819 uint type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct fs_disk_quota *dst)
821{
Christoph Hellwig18535a72012-02-20 02:28:16 +0000822 struct xfs_dquot *dqp;
823 int error;
824
825 /*
826 * Try to get the dquot. We don't want it allocated on disk, so
827 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
828 * exist, we'll get ENOENT back.
829 */
830 error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp);
831 if (error)
832 return error;
833
834 /*
835 * If everything's NULL, this dquot doesn't quite exist as far as
836 * our utility programs are concerned.
837 */
838 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000839 error = -ENOENT;
Christoph Hellwig18535a72012-02-20 02:28:16 +0000840 goto out_put;
841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 memset(dst, 0, sizeof(*dst));
Christoph Hellwig18535a72012-02-20 02:28:16 +0000844 dst->d_version = FS_DQUOT_VERSION;
845 dst->d_flags = xfs_qm_export_qtype_flags(dqp->q_core.d_flags);
846 dst->d_id = be32_to_cpu(dqp->q_core.d_id);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100847 dst->d_blk_hardlimit =
Christoph Hellwig18535a72012-02-20 02:28:16 +0000848 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
Christoph Hellwig1149d962005-11-02 15:01:12 +1100849 dst->d_blk_softlimit =
Christoph Hellwig18535a72012-02-20 02:28:16 +0000850 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
851 dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
852 dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
Christoph Hellwig89605012012-02-20 02:28:17 +0000853 dst->d_bcount = XFS_FSB_TO_BB(mp, dqp->q_res_bcount);
854 dst->d_icount = dqp->q_res_icount;
Christoph Hellwig18535a72012-02-20 02:28:16 +0000855 dst->d_btimer = be32_to_cpu(dqp->q_core.d_btimer);
856 dst->d_itimer = be32_to_cpu(dqp->q_core.d_itimer);
857 dst->d_iwarns = be16_to_cpu(dqp->q_core.d_iwarns);
858 dst->d_bwarns = be16_to_cpu(dqp->q_core.d_bwarns);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100859 dst->d_rtb_hardlimit =
Christoph Hellwig18535a72012-02-20 02:28:16 +0000860 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
Christoph Hellwig1149d962005-11-02 15:01:12 +1100861 dst->d_rtb_softlimit =
Christoph Hellwig18535a72012-02-20 02:28:16 +0000862 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
Christoph Hellwig89605012012-02-20 02:28:17 +0000863 dst->d_rtbcount = XFS_FSB_TO_BB(mp, dqp->q_res_rtbcount);
Christoph Hellwig18535a72012-02-20 02:28:16 +0000864 dst->d_rtbtimer = be32_to_cpu(dqp->q_core.d_rtbtimer);
865 dst->d_rtbwarns = be16_to_cpu(dqp->q_core.d_rtbwarns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
868 * Internally, we don't reset all the timers when quota enforcement
Nathan Scottc41564b2006-03-29 08:55:14 +1000869 * gets turned off. No need to confuse the user level code,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 * so return zeroes in that case.
871 */
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500872 if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
873 dqp->q_core.d_flags == XFS_DQ_USER) ||
874 (!XFS_IS_GQUOTA_ENFORCED(mp) &&
875 dqp->q_core.d_flags == XFS_DQ_GROUP) ||
876 (!XFS_IS_PQUOTA_ENFORCED(mp) &&
877 dqp->q_core.d_flags == XFS_DQ_PROJ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 dst->d_btimer = 0;
879 dst->d_itimer = 0;
880 dst->d_rtbtimer = 0;
881 }
882
883#ifdef DEBUG
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200884 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500885 (XFS_IS_GQUOTA_ENFORCED(mp) && dst->d_flags == FS_GROUP_QUOTA) ||
886 (XFS_IS_PQUOTA_ENFORCED(mp) && dst->d_flags == FS_PROJ_QUOTA)) &&
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000887 dst->d_id != 0) {
Brian Fosterf7555032012-12-21 10:45:17 -0500888 if ((dst->d_bcount > dst->d_blk_softlimit) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 (dst->d_blk_softlimit > 0)) {
890 ASSERT(dst->d_btimer != 0);
891 }
Brian Fosterf7555032012-12-21 10:45:17 -0500892 if ((dst->d_icount > dst->d_ino_softlimit) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 (dst->d_ino_softlimit > 0)) {
894 ASSERT(dst->d_itimer != 0);
895 }
896 }
897#endif
Christoph Hellwig18535a72012-02-20 02:28:16 +0000898out_put:
899 xfs_qm_dqput(dqp);
900 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904xfs_qm_export_qtype_flags(
905 uint flags)
906{
907 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000908 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200910 ASSERT((flags & (FS_PROJ_QUOTA | FS_USER_QUOTA)) !=
911 (FS_PROJ_QUOTA | FS_USER_QUOTA));
912 ASSERT((flags & (FS_PROJ_QUOTA | FS_GROUP_QUOTA)) !=
913 (FS_PROJ_QUOTA | FS_GROUP_QUOTA));
914 ASSERT((flags & (FS_USER_QUOTA | FS_GROUP_QUOTA)) !=
915 (FS_USER_QUOTA | FS_GROUP_QUOTA));
916 ASSERT((flags & (FS_PROJ_QUOTA|FS_USER_QUOTA|FS_GROUP_QUOTA)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 return (flags & XFS_DQ_USER) ?
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200919 FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
920 FS_PROJ_QUOTA : FS_GROUP_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923STATIC uint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924xfs_qm_export_flags(
925 uint flags)
926{
927 uint uflags;
928
929 uflags = 0;
930 if (flags & XFS_UQUOTA_ACCT)
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200931 uflags |= FS_QUOTA_UDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (flags & XFS_GQUOTA_ACCT)
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200933 uflags |= FS_QUOTA_GDQ_ACCT;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500934 if (flags & XFS_PQUOTA_ACCT)
935 uflags |= FS_QUOTA_PDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (flags & XFS_UQUOTA_ENFD)
Christoph Hellwigade7ce32010-06-04 10:56:01 +0200937 uflags |= FS_QUOTA_UDQ_ENFD;
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500938 if (flags & XFS_GQUOTA_ENFD)
939 uflags |= FS_QUOTA_GDQ_ENFD;
940 if (flags & XFS_PQUOTA_ENFD)
941 uflags |= FS_QUOTA_PDQ_ENFD;
Eric Sandeend99831f2014-06-22 15:03:54 +1000942 return uflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200946STATIC int
947xfs_dqrele_inode(
948 struct xfs_inode *ip,
Brian Fostera454f742012-11-06 09:50:39 -0500949 int flags,
950 void *args)
David Chinner5b4d89a2008-10-30 17:08:03 +1100951{
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200952 /* skip quota inodes */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000953 if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500954 ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
955 ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200956 ASSERT(ip->i_udquot == NULL);
957 ASSERT(ip->i_gdquot == NULL);
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500958 ASSERT(ip->i_pdquot == NULL);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200959 return 0;
960 }
Dave Chinnercb4f0d12008-11-10 17:11:18 +1100961
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200962 xfs_ilock(ip, XFS_ILOCK_EXCL);
963 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
964 xfs_qm_dqrele(ip->i_udquot);
965 ip->i_udquot = NULL;
966 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500967 if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200968 xfs_qm_dqrele(ip->i_gdquot);
969 ip->i_gdquot = NULL;
970 }
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500971 if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
972 xfs_qm_dqrele(ip->i_pdquot);
973 ip->i_pdquot = NULL;
974 }
Christoph Hellwigf2d67612010-06-24 11:52:50 +1000975 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200976 return 0;
David Chinner5b4d89a2008-10-30 17:08:03 +1100977}
978
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200979
David Chinner5b4d89a2008-10-30 17:08:03 +1100980/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * Go thru all the inodes in the file system, releasing their dquots.
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200982 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * Note that the mount structure gets modified to indicate that quotas are off
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200984 * AFTER this, in the case of quotaoff.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
986void
987xfs_qm_dqrele_all_inodes(
988 struct xfs_mount *mp,
989 uint flags)
990{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ASSERT(mp->m_quotainfo);
Brian Fostera454f742012-11-06 09:50:39 -0500992 xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}