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