Merge tag 'docs-5.7-2' of git://git.lwn.net/linux
[linux-block.git] / fs / xfs / xfs_qm_syscalls.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
4ce3121f
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
1da177e4 5 */
16f7e0fe 6
16f7e0fe 7
1da177e4
LT
8#include "xfs.h"
9#include "xfs_fs.h"
70a9883c 10#include "xfs_shared.h"
239880ef
DC
11#include "xfs_format.h"
12#include "xfs_log_format.h"
13#include "xfs_trans_resv.h"
1da177e4 14#include "xfs_sb.h"
1da177e4 15#include "xfs_mount.h"
1da177e4 16#include "xfs_inode.h"
239880ef 17#include "xfs_trans.h"
a4fbe6ab 18#include "xfs_quota.h"
1da177e4 19#include "xfs_qm.h"
6d8b79cf 20#include "xfs_icache.h"
1da177e4 21
1cc95e6f
PR
22STATIC int
23xfs_qm_log_quotaoff(
24 struct xfs_mount *mp,
25 struct xfs_qoff_logitem **qoffstartp,
26 uint flags)
27{
28 struct xfs_trans *tp;
29 int error;
30 struct xfs_qoff_logitem *qoffi;
31
1cc95e6f
PR
32 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_quotaoff, 0, 0, 0, &tp);
33 if (error)
34 goto out;
35
36 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
37 xfs_trans_log_quotaoff_item(tp, qoffi);
38
39 spin_lock(&mp->m_sb_lock);
40 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
41 spin_unlock(&mp->m_sb_lock);
42
43 xfs_log_sb(tp);
44
45 /*
46 * We have to make sure that the transaction is secure on disk before we
47 * return and actually stop quota accounting. So, make it synchronous.
48 * We don't care about quotoff's performance.
49 */
50 xfs_trans_set_sync(tp);
51 error = xfs_trans_commit(tp);
52 if (error)
53 goto out;
54
55 *qoffstartp = qoffi;
56out:
57 return error;
58}
59
60STATIC int
61xfs_qm_log_quotaoff_end(
62 struct xfs_mount *mp,
8a627143 63 struct xfs_qoff_logitem **startqoff,
1cc95e6f
PR
64 uint flags)
65{
66 struct xfs_trans *tp;
67 int error;
68 struct xfs_qoff_logitem *qoffi;
69
70 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_equotaoff, 0, 0, 0, &tp);
71 if (error)
72 return error;
73
8a627143 74 qoffi = xfs_trans_get_qoff_item(tp, *startqoff,
1cc95e6f
PR
75 flags & XFS_ALL_QUOTA_ACCT);
76 xfs_trans_log_quotaoff_item(tp, qoffi);
8a627143 77 *startqoff = NULL;
1cc95e6f
PR
78
79 /*
80 * We have to make sure that the transaction is secure on disk before we
81 * return and actually stop quota accounting. So, make it synchronous.
82 * We don't care about quotoff's performance.
83 */
84 xfs_trans_set_sync(tp);
85 return xfs_trans_commit(tp);
86}
1da177e4 87
1da177e4
LT
88/*
89 * Turn off quota accounting and/or enforcement for all udquots and/or
90 * gdquots. Called only at unmount time.
91 *
92 * This assumes that there are no dquots of this file system cached
93 * incore, and modifies the ondisk dquot directly. Therefore, for example,
94 * it is an error to call this twice, without purging the cache.
95 */
fcafb71b 96int
1da177e4
LT
97xfs_qm_scall_quotaoff(
98 xfs_mount_t *mp,
fcafb71b 99 uint flags)
1da177e4 100{
8a7b8a89 101 struct xfs_quotainfo *q = mp->m_quotainfo;
1da177e4 102 uint dqtype;
1da177e4
LT
103 int error;
104 uint inactivate_flags;
8a627143 105 struct xfs_qoff_logitem *qoffstart = NULL;
1da177e4 106
1da177e4
LT
107 /*
108 * No file system can have quotas enabled on disk but not in core.
109 * Note that quota utilities (like quotaoff) _expect_
2451337d 110 * errno == -EEXIST here.
1da177e4
LT
111 */
112 if ((mp->m_qflags & flags) == 0)
2451337d 113 return -EEXIST;
1da177e4
LT
114 error = 0;
115
116 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
117
118 /*
119 * We don't want to deal with two quotaoffs messing up each other,
120 * so we're going to serialize it. quotaoff isn't exactly a performance
121 * critical thing.
122 * If quotaoff, then we must be dealing with the root filesystem.
123 */
8a7b8a89
CH
124 ASSERT(q);
125 mutex_lock(&q->qi_quotaofflock);
1da177e4
LT
126
127 /*
128 * If we're just turning off quota enforcement, change mp and go.
129 */
130 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
131 mp->m_qflags &= ~(flags);
132
3685c2a1 133 spin_lock(&mp->m_sb_lock);
1da177e4 134 mp->m_sb.sb_qflags = mp->m_qflags;
3685c2a1 135 spin_unlock(&mp->m_sb_lock);
8a7b8a89 136 mutex_unlock(&q->qi_quotaofflock);
1da177e4
LT
137
138 /* XXX what to do if error ? Revert back to old vals incore ? */
61e63ecb 139 return xfs_sync_sb(mp, false);
1da177e4
LT
140 }
141
142 dqtype = 0;
143 inactivate_flags = 0;
144 /*
145 * If accounting is off, we must turn enforcement off, clear the
146 * quota 'CHKD' certificate to make it known that we have to
147 * do a quotacheck the next time this quota is turned on.
148 */
149 if (flags & XFS_UQUOTA_ACCT) {
150 dqtype |= XFS_QMOPT_UQUOTA;
151 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
152 inactivate_flags |= XFS_UQUOTA_ACTIVE;
153 }
154 if (flags & XFS_GQUOTA_ACCT) {
155 dqtype |= XFS_QMOPT_GQUOTA;
83e782e1 156 flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
1da177e4 157 inactivate_flags |= XFS_GQUOTA_ACTIVE;
92f8ff73
CS
158 }
159 if (flags & XFS_PQUOTA_ACCT) {
c8ad20ff 160 dqtype |= XFS_QMOPT_PQUOTA;
83e782e1 161 flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
c8ad20ff 162 inactivate_flags |= XFS_PQUOTA_ACTIVE;
1da177e4
LT
163 }
164
165 /*
166 * Nothing to do? Don't complain. This happens when we're just
167 * turning off quota enforcement.
168 */
8a7b8a89
CH
169 if ((mp->m_qflags & flags) == 0)
170 goto out_unlock;
1da177e4
LT
171
172 /*
173 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
cb6edc26
DC
174 * and synchronously. If we fail to write, we should abort the
175 * operation as it cannot be recovered safely if we crash.
1da177e4 176 */
cb6edc26
DC
177 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
178 if (error)
8a7b8a89 179 goto out_unlock;
1da177e4
LT
180
181 /*
182 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
183 * to take care of the race between dqget and quotaoff. We don't take
184 * any special locks to reset these bits. All processes need to check
185 * these bits *after* taking inode lock(s) to see if the particular
186 * quota type is in the process of being turned off. If *ACTIVE, it is
187 * guaranteed that all dquot structures and all quotainode ptrs will all
188 * stay valid as long as that inode is kept locked.
189 *
190 * There is no turning back after this.
191 */
192 mp->m_qflags &= ~inactivate_flags;
193
194 /*
195 * Give back all the dquot reference(s) held by inodes.
196 * Here we go thru every single incore inode in this file system, and
197 * do a dqrele on the i_udquot/i_gdquot that it may have.
198 * Essentially, as long as somebody has an inode locked, this guarantees
199 * that quotas will not be turned off. This is handy because in a
200 * transaction once we lock the inode(s) and check for quotaon, we can
201 * depend on the quota inodes (and other things) being valid as long as
202 * we keep the lock(s).
203 */
204 xfs_qm_dqrele_all_inodes(mp, flags);
205
206 /*
207 * Next we make the changes in the quota flag in the mount struct.
208 * This isn't protected by a particular lock directly, because we
25985edc 209 * don't want to take a mrlock every time we depend on quotas being on.
1da177e4 210 */
b84a3a96 211 mp->m_qflags &= ~flags;
1da177e4
LT
212
213 /*
214 * Go through all the dquots of this file system and purge them,
b84a3a96 215 * according to what was turned off.
1da177e4 216 */
b84a3a96 217 xfs_qm_dqpurge_all(mp, dqtype);
1da177e4
LT
218
219 /*
220 * Transactions that had started before ACTIVE state bit was cleared
221 * could have logged many dquots, so they'd have higher LSNs than
222 * the first QUOTAOFF log record does. If we happen to crash when
223 * the tail of the log has gone past the QUOTAOFF record, but
224 * before the last dquot modification, those dquots __will__
225 * recover, and that's not good.
226 *
227 * So, we have QUOTAOFF start and end logitems; the start
228 * logitem won't get overwritten until the end logitem appears...
229 */
8a627143 230 error = xfs_qm_log_quotaoff_end(mp, &qoffstart, flags);
cb6edc26
DC
231 if (error) {
232 /* We're screwed now. Shutdown is the only option. */
233 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
8a7b8a89 234 goto out_unlock;
cb6edc26 235 }
1da177e4
LT
236
237 /*
c31ad439 238 * If all quotas are completely turned off, close shop.
1da177e4 239 */
c31ad439 240 if (mp->m_qflags == 0) {
8a7b8a89 241 mutex_unlock(&q->qi_quotaofflock);
1da177e4 242 xfs_qm_destroy_quotainfo(mp);
d99831ff 243 return 0;
1da177e4
LT
244 }
245
246 /*
8a7b8a89 247 * Release our quotainode references if we don't need them anymore.
1da177e4 248 */
8a7b8a89 249 if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
44a8736b 250 xfs_irele(q->qi_uquotaip);
8a7b8a89 251 q->qi_uquotaip = NULL;
1da177e4 252 }
92f8ff73 253 if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
44a8736b 254 xfs_irele(q->qi_gquotaip);
8a7b8a89 255 q->qi_gquotaip = NULL;
1da177e4 256 }
92f8ff73 257 if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
44a8736b 258 xfs_irele(q->qi_pquotaip);
92f8ff73
CS
259 q->qi_pquotaip = NULL;
260 }
1da177e4 261
8a7b8a89 262out_unlock:
8a627143
BF
263 if (error && qoffstart)
264 xfs_qm_qoff_logitem_relse(qoffstart);
8a7b8a89
CH
265 mutex_unlock(&q->qi_quotaofflock);
266 return error;
1da177e4
LT
267}
268
5d18898b
CH
269STATIC int
270xfs_qm_scall_trunc_qfile(
271 struct xfs_mount *mp,
272 xfs_ino_t ino)
273{
274 struct xfs_inode *ip;
275 struct xfs_trans *tp;
276 int error;
277
278 if (ino == NULLFSINO)
279 return 0;
280
281 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
282 if (error)
283 return error;
284
285 xfs_ilock(ip, XFS_IOLOCK_EXCL);
286
253f4911 287 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
5d18898b 288 if (error) {
5d18898b
CH
289 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
290 goto out_put;
291 }
292
293 xfs_ilock(ip, XFS_ILOCK_EXCL);
ddc3415a 294 xfs_trans_ijoin(tp, ip, 0);
5d18898b 295
673e8e59 296 ip->i_d.di_size = 0;
673e8e59
CH
297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
298
299 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
5d18898b 300 if (error) {
4906e215 301 xfs_trans_cancel(tp);
5d18898b
CH
302 goto out_unlock;
303 }
304
673e8e59
CH
305 ASSERT(ip->i_d.di_nextents == 0);
306
dcd79a14 307 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
70393313 308 error = xfs_trans_commit(tp);
5d18898b
CH
309
310out_unlock:
311 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
312out_put:
44a8736b 313 xfs_irele(ip);
5d18898b
CH
314 return error;
315}
316
fcafb71b 317int
1da177e4
LT
318xfs_qm_scall_trunc_qfiles(
319 xfs_mount_t *mp,
320 uint flags)
321{
2451337d 322 int error = -EINVAL;
1da177e4 323
f58522c5
ES
324 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
325 (flags & ~XFS_DQ_ALLTYPES)) {
08e96e1a 326 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
8221112b 327 __func__, flags, mp->m_qflags);
2451337d 328 return -EINVAL;
1da177e4
LT
329 }
330
c61a9e39 331 if (flags & XFS_DQ_USER) {
5d18898b 332 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
c61a9e39
JL
333 if (error)
334 return error;
335 }
336 if (flags & XFS_DQ_GROUP) {
337 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_gquotino);
338 if (error)
339 return error;
340 }
d892d586 341 if (flags & XFS_DQ_PROJ)
c61a9e39 342 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
1da177e4 343
c61a9e39 344 return error;
1da177e4
LT
345}
346
1da177e4
LT
347/*
348 * Switch on (a given) quota enforcement for a filesystem. This takes
349 * effect immediately.
350 * (Switching on quota accounting must be done at mount time.)
351 */
fcafb71b 352int
1da177e4
LT
353xfs_qm_scall_quotaon(
354 xfs_mount_t *mp,
355 uint flags)
356{
357 int error;
1da177e4 358 uint qf;
1da177e4 359
1da177e4
LT
360 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
361 /*
362 * Switching on quota accounting must be done at mount time.
363 */
1da177e4
LT
364 flags &= ~(XFS_ALL_QUOTA_ACCT);
365
1da177e4 366 if (flags == 0) {
08e96e1a 367 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
8221112b 368 __func__, mp->m_qflags);
2451337d 369 return -EINVAL;
1da177e4
LT
370 }
371
1da177e4
LT
372 /*
373 * Can't enforce without accounting. We check the superblock
374 * qflags here instead of m_qflags because rootfs can have
375 * quota acct on ondisk without m_qflags' knowing.
376 */
fbf64b3d 377 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
83e782e1 378 (flags & XFS_UQUOTA_ENFD)) ||
fbf64b3d 379 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
83e782e1 380 (flags & XFS_GQUOTA_ENFD)) ||
fbf64b3d 381 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
83e782e1 382 (flags & XFS_PQUOTA_ENFD))) {
8221112b 383 xfs_debug(mp,
08e96e1a 384 "%s: Can't enforce without acct, flags=%x sbflags=%x",
8221112b 385 __func__, flags, mp->m_sb.sb_qflags);
2451337d 386 return -EINVAL;
1da177e4
LT
387 }
388 /*
25985edc 389 * If everything's up to-date incore, then don't waste time.
1da177e4
LT
390 */
391 if ((mp->m_qflags & flags) == flags)
2451337d 392 return -EEXIST;
1da177e4
LT
393
394 /*
395 * Change sb_qflags on disk but not incore mp->qflags
396 * if this is the root filesystem.
397 */
3685c2a1 398 spin_lock(&mp->m_sb_lock);
1da177e4
LT
399 qf = mp->m_sb.sb_qflags;
400 mp->m_sb.sb_qflags = qf | flags;
3685c2a1 401 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
402
403 /*
404 * There's nothing to change if it's the same.
405 */
4d11a402 406 if ((qf & flags) == flags)
2451337d 407 return -EEXIST;
1da177e4 408
61e63ecb
DC
409 error = xfs_sync_sb(mp, false);
410 if (error)
d99831ff 411 return error;
1da177e4
LT
412 /*
413 * If we aren't trying to switch on quota enforcement, we are done.
414 */
415 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
416 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
c8ad20ff
NS
417 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
418 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
419 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
fbf64b3d 420 (mp->m_qflags & XFS_GQUOTA_ACCT)))
d99831ff 421 return 0;
1da177e4
LT
422
423 if (! XFS_IS_QUOTA_RUNNING(mp))
2451337d 424 return -ESRCH;
1da177e4
LT
425
426 /*
427 * Switch on quota enforcement in core.
428 */
8a7b8a89 429 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 430 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
8a7b8a89 431 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 432
d99831ff 433 return 0;
1da177e4
LT
434}
435
14bf61ff
JK
436#define XFS_QC_MASK \
437 (QC_LIMIT_MASK | QC_TIMER_MASK | QC_WARNS_MASK)
c472b432 438
1da177e4
LT
439/*
440 * Adjust quota limits, and start/stop timers accordingly.
441 */
fcafb71b 442int
1da177e4 443xfs_qm_scall_setqlim(
b1366451 444 struct xfs_mount *mp,
1da177e4
LT
445 xfs_dqid_t id,
446 uint type,
14bf61ff 447 struct qc_dqblk *newlim)
1da177e4 448{
8a7b8a89 449 struct xfs_quotainfo *q = mp->m_quotainfo;
b1366451
BF
450 struct xfs_disk_dquot *ddq;
451 struct xfs_dquot *dqp;
452 struct xfs_trans *tp;
be607946 453 struct xfs_def_quota *defq;
1da177e4
LT
454 int error;
455 xfs_qcnt_t hard, soft;
456
14bf61ff 457 if (newlim->d_fieldmask & ~XFS_QC_MASK)
2451337d 458 return -EINVAL;
14bf61ff 459 if ((newlim->d_fieldmask & XFS_QC_MASK) == 0)
c472b432 460 return 0;
1da177e4 461
1da177e4
LT
462 /*
463 * We don't want to race with a quotaoff so take the quotaoff lock.
f648167f
DC
464 * We don't hold an inode lock, so there's nothing else to stop
465 * a quotaoff from happening.
1da177e4 466 */
8a7b8a89 467 mutex_lock(&q->qi_quotaofflock);
1da177e4
LT
468
469 /*
f648167f
DC
470 * Get the dquot (locked) before we start, as we need to do a
471 * transaction to allocate it if it doesn't exist. Once we have the
472 * dquot, unlock it so we can start the next transaction safely. We hold
473 * a reference to the dquot, so it's safe to do this unlock/lock without
474 * it being reclaimed in the mean time.
1da177e4 475 */
30ab2dcf 476 error = xfs_qm_dqget(mp, id, type, true, &dqp);
f648167f 477 if (error) {
2451337d 478 ASSERT(error != -ENOENT);
8a7b8a89 479 goto out_unlock;
1da177e4 480 }
be607946
CM
481
482 defq = xfs_get_defquota(dqp, q);
f648167f
DC
483 xfs_dqunlock(dqp);
484
253f4911
CH
485 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_setqlim, 0, 0, 0, &tp);
486 if (error)
f648167f 487 goto out_rele;
f648167f
DC
488
489 xfs_dqlock(dqp);
1da177e4
LT
490 xfs_trans_dqjoin(tp, dqp);
491 ddq = &dqp->q_core;
492
493 /*
494 * Make sure that hardlimits are >= soft limits before changing.
495 */
14bf61ff
JK
496 hard = (newlim->d_fieldmask & QC_SPC_HARD) ?
497 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_hardlimit) :
1149d96a 498 be64_to_cpu(ddq->d_blk_hardlimit);
14bf61ff
JK
499 soft = (newlim->d_fieldmask & QC_SPC_SOFT) ?
500 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_spc_softlimit) :
1149d96a 501 be64_to_cpu(ddq->d_blk_softlimit);
1da177e4 502 if (hard == 0 || hard >= soft) {
1149d96a
CH
503 ddq->d_blk_hardlimit = cpu_to_be64(hard);
504 ddq->d_blk_softlimit = cpu_to_be64(soft);
b1366451 505 xfs_dquot_set_prealloc_limits(dqp);
1da177e4 506 if (id == 0) {
be607946
CM
507 defq->bhardlimit = hard;
508 defq->bsoftlimit = soft;
1da177e4
LT
509 }
510 } else {
08e96e1a 511 xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
1da177e4 512 }
14bf61ff
JK
513 hard = (newlim->d_fieldmask & QC_RT_SPC_HARD) ?
514 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_hardlimit) :
1149d96a 515 be64_to_cpu(ddq->d_rtb_hardlimit);
14bf61ff
JK
516 soft = (newlim->d_fieldmask & QC_RT_SPC_SOFT) ?
517 (xfs_qcnt_t) XFS_B_TO_FSB(mp, newlim->d_rt_spc_softlimit) :
1149d96a 518 be64_to_cpu(ddq->d_rtb_softlimit);
1da177e4 519 if (hard == 0 || hard >= soft) {
1149d96a
CH
520 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
521 ddq->d_rtb_softlimit = cpu_to_be64(soft);
1da177e4 522 if (id == 0) {
be607946
CM
523 defq->rtbhardlimit = hard;
524 defq->rtbsoftlimit = soft;
1da177e4
LT
525 }
526 } else {
08e96e1a 527 xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
1da177e4
LT
528 }
529
14bf61ff 530 hard = (newlim->d_fieldmask & QC_INO_HARD) ?
1da177e4 531 (xfs_qcnt_t) newlim->d_ino_hardlimit :
1149d96a 532 be64_to_cpu(ddq->d_ino_hardlimit);
14bf61ff 533 soft = (newlim->d_fieldmask & QC_INO_SOFT) ?
1da177e4 534 (xfs_qcnt_t) newlim->d_ino_softlimit :
1149d96a 535 be64_to_cpu(ddq->d_ino_softlimit);
1da177e4 536 if (hard == 0 || hard >= soft) {
1149d96a
CH
537 ddq->d_ino_hardlimit = cpu_to_be64(hard);
538 ddq->d_ino_softlimit = cpu_to_be64(soft);
1da177e4 539 if (id == 0) {
be607946
CM
540 defq->ihardlimit = hard;
541 defq->isoftlimit = soft;
1da177e4
LT
542 }
543 } else {
08e96e1a 544 xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
1da177e4
LT
545 }
546
754002b4
NS
547 /*
548 * Update warnings counter(s) if requested
549 */
14bf61ff
JK
550 if (newlim->d_fieldmask & QC_SPC_WARNS)
551 ddq->d_bwarns = cpu_to_be16(newlim->d_spc_warns);
552 if (newlim->d_fieldmask & QC_INO_WARNS)
553 ddq->d_iwarns = cpu_to_be16(newlim->d_ino_warns);
554 if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
555 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rt_spc_warns);
754002b4 556
1da177e4
LT
557 if (id == 0) {
558 /*
559 * Timelimits for the super user set the relative time
560 * the other users can be over quota for this file system.
561 * If it is zero a default is used. Ditto for the default
754002b4
NS
562 * soft and hard limit values (already done, above), and
563 * for warnings.
1da177e4 564 */
14bf61ff
JK
565 if (newlim->d_fieldmask & QC_SPC_TIMER) {
566 q->qi_btimelimit = newlim->d_spc_timer;
567 ddq->d_btimer = cpu_to_be32(newlim->d_spc_timer);
1da177e4 568 }
14bf61ff
JK
569 if (newlim->d_fieldmask & QC_INO_TIMER) {
570 q->qi_itimelimit = newlim->d_ino_timer;
571 ddq->d_itimer = cpu_to_be32(newlim->d_ino_timer);
1da177e4 572 }
14bf61ff
JK
573 if (newlim->d_fieldmask & QC_RT_SPC_TIMER) {
574 q->qi_rtbtimelimit = newlim->d_rt_spc_timer;
575 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rt_spc_timer);
1da177e4 576 }
14bf61ff
JK
577 if (newlim->d_fieldmask & QC_SPC_WARNS)
578 q->qi_bwarnlimit = newlim->d_spc_warns;
579 if (newlim->d_fieldmask & QC_INO_WARNS)
580 q->qi_iwarnlimit = newlim->d_ino_warns;
581 if (newlim->d_fieldmask & QC_RT_SPC_WARNS)
582 q->qi_rtbwarnlimit = newlim->d_rt_spc_warns;
754002b4 583 } else {
1da177e4
LT
584 /*
585 * If the user is now over quota, start the timelimit.
586 * The user will not be 'warned'.
587 * Note that we keep the timers ticking, whether enforcement
588 * is on or off. We don't really want to bother with iterating
589 * over all ondisk dquots and turning the timers on/off.
590 */
591 xfs_qm_adjust_dqtimers(mp, ddq);
592 }
593 dqp->dq_flags |= XFS_DQ_DIRTY;
594 xfs_trans_log_dquot(tp, dqp);
595
70393313 596 error = xfs_trans_commit(tp);
1da177e4 597
f648167f
DC
598out_rele:
599 xfs_qm_dqrele(dqp);
600out_unlock:
8a7b8a89 601 mutex_unlock(&q->qi_quotaofflock);
e5720eec 602 return error;
1da177e4
LT
603}
604
2e330e76
DW
605/* Fill out the quota context. */
606static void
607xfs_qm_scall_getquota_fill_qc(
18535a7e 608 struct xfs_mount *mp,
18535a7e 609 uint type,
2e330e76
DW
610 const struct xfs_dquot *dqp,
611 struct qc_dqblk *dst)
1da177e4
LT
612{
613 memset(dst, 0, sizeof(*dst));
14bf61ff
JK
614 dst->d_spc_hardlimit =
615 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
616 dst->d_spc_softlimit =
617 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_blk_softlimit));
18535a7e
CH
618 dst->d_ino_hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
619 dst->d_ino_softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
14bf61ff
JK
620 dst->d_space = XFS_FSB_TO_B(mp, dqp->q_res_bcount);
621 dst->d_ino_count = dqp->q_res_icount;
622 dst->d_spc_timer = be32_to_cpu(dqp->q_core.d_btimer);
623 dst->d_ino_timer = be32_to_cpu(dqp->q_core.d_itimer);
624 dst->d_ino_warns = be16_to_cpu(dqp->q_core.d_iwarns);
625 dst->d_spc_warns = be16_to_cpu(dqp->q_core.d_bwarns);
626 dst->d_rt_spc_hardlimit =
627 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
628 dst->d_rt_spc_softlimit =
629 XFS_FSB_TO_B(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
630 dst->d_rt_space = XFS_FSB_TO_B(mp, dqp->q_res_rtbcount);
631 dst->d_rt_spc_timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
632 dst->d_rt_spc_warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
1da177e4
LT
633
634 /*
635 * Internally, we don't reset all the timers when quota enforcement
c41564b5 636 * gets turned off. No need to confuse the user level code,
1da177e4
LT
637 * so return zeroes in that case.
638 */
83e782e1
CS
639 if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
640 dqp->q_core.d_flags == XFS_DQ_USER) ||
641 (!XFS_IS_GQUOTA_ENFORCED(mp) &&
642 dqp->q_core.d_flags == XFS_DQ_GROUP) ||
643 (!XFS_IS_PQUOTA_ENFORCED(mp) &&
644 dqp->q_core.d_flags == XFS_DQ_PROJ)) {
14bf61ff
JK
645 dst->d_spc_timer = 0;
646 dst->d_ino_timer = 0;
647 dst->d_rt_spc_timer = 0;
1da177e4
LT
648 }
649
650#ifdef DEBUG
14bf61ff
JK
651 if (((XFS_IS_UQUOTA_ENFORCED(mp) && type == XFS_DQ_USER) ||
652 (XFS_IS_GQUOTA_ENFORCED(mp) && type == XFS_DQ_GROUP) ||
653 (XFS_IS_PQUOTA_ENFORCED(mp) && type == XFS_DQ_PROJ)) &&
2e330e76 654 dqp->q_core.d_id != 0) {
14bf61ff
JK
655 if ((dst->d_space > dst->d_spc_softlimit) &&
656 (dst->d_spc_softlimit > 0)) {
657 ASSERT(dst->d_spc_timer != 0);
1da177e4 658 }
14bf61ff 659 if ((dst->d_ino_count > dst->d_ino_softlimit) &&
1da177e4 660 (dst->d_ino_softlimit > 0)) {
14bf61ff 661 ASSERT(dst->d_ino_timer != 0);
1da177e4
LT
662 }
663 }
664#endif
2e330e76
DW
665}
666
667/* Return the quota information for the dquot matching id. */
668int
669xfs_qm_scall_getquota(
670 struct xfs_mount *mp,
671 xfs_dqid_t id,
672 uint type,
673 struct qc_dqblk *dst)
674{
675 struct xfs_dquot *dqp;
676 int error;
677
678 /*
30ab2dcf
DW
679 * Try to get the dquot. We don't want it allocated on disk, so don't
680 * set doalloc. If it doesn't exist, we'll get ENOENT back.
2e330e76 681 */
30ab2dcf 682 error = xfs_qm_dqget(mp, id, type, false, &dqp);
2e330e76
DW
683 if (error)
684 return error;
685
686 /*
687 * If everything's NULL, this dquot doesn't quite exist as far as
688 * our utility programs are concerned.
689 */
690 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
691 error = -ENOENT;
692 goto out_put;
693 }
694
695 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
696
18535a7e
CH
697out_put:
698 xfs_qm_dqput(dqp);
699 return error;
1da177e4
LT
700}
701
2e330e76
DW
702/*
703 * Return the quota information for the first initialized dquot whose id
704 * is at least as high as id.
705 */
706int
707xfs_qm_scall_getquota_next(
708 struct xfs_mount *mp,
709 xfs_dqid_t *id,
710 uint type,
711 struct qc_dqblk *dst)
712{
713 struct xfs_dquot *dqp;
714 int error;
715
716 error = xfs_qm_dqget_next(mp, *id, type, &dqp);
717 if (error)
718 return error;
719
720 /* Fill in the ID we actually read from disk */
721 *id = be32_to_cpu(dqp->q_core.d_id);
722
723 xfs_qm_scall_getquota_fill_qc(mp, type, dqp, dst);
724
725 xfs_qm_dqput(dqp);
726 return error;
727}
1da177e4 728
fe588ed3
CH
729STATIC int
730xfs_dqrele_inode(
731 struct xfs_inode *ip,
a454f742
BF
732 int flags,
733 void *args)
1da177e4 734{
fe588ed3 735 /* skip quota inodes */
8a7b8a89 736 if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
92f8ff73
CS
737 ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
738 ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
fe588ed3
CH
739 ASSERT(ip->i_udquot == NULL);
740 ASSERT(ip->i_gdquot == NULL);
92f8ff73 741 ASSERT(ip->i_pdquot == NULL);
fe588ed3
CH
742 return 0;
743 }
cb4f0d1d 744
fe588ed3
CH
745 xfs_ilock(ip, XFS_ILOCK_EXCL);
746 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
747 xfs_qm_dqrele(ip->i_udquot);
748 ip->i_udquot = NULL;
749 }
92f8ff73 750 if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
fe588ed3
CH
751 xfs_qm_dqrele(ip->i_gdquot);
752 ip->i_gdquot = NULL;
753 }
92f8ff73
CS
754 if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
755 xfs_qm_dqrele(ip->i_pdquot);
756 ip->i_pdquot = NULL;
757 }
f2d67614 758 xfs_iunlock(ip, XFS_ILOCK_EXCL);
fe588ed3 759 return 0;
5b4d89ae
DC
760}
761
fe588ed3 762
5b4d89ae
DC
763/*
764 * Go thru all the inodes in the file system, releasing their dquots.
fe588ed3 765 *
5b4d89ae 766 * Note that the mount structure gets modified to indicate that quotas are off
fe588ed3 767 * AFTER this, in the case of quotaoff.
5b4d89ae
DC
768 */
769void
770xfs_qm_dqrele_all_inodes(
771 struct xfs_mount *mp,
772 uint flags)
773{
5b4d89ae 774 ASSERT(mp->m_quotainfo);
e20c8a51
BF
775 xfs_inode_ag_iterator_flags(mp, xfs_dqrele_inode, flags, NULL,
776 XFS_AGITER_INEW_WAIT);
1da177e4 777}