]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_qm_syscalls.c
xfs: Remove some useless flags tests
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_qm_syscalls.c
CommitLineData
1da177e4 1/*
4ce3121f
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
4ce3121f
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
4ce3121f
NS
9 * 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.
1da177e4 13 *
4ce3121f
NS
14 * 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
1da177e4 17 */
16f7e0fe
RD
18
19#include <linux/capability.h>
20
1da177e4
LT
21#include "xfs.h"
22#include "xfs_fs.h"
70a9883c 23#include "xfs_shared.h"
239880ef
DC
24#include "xfs_format.h"
25#include "xfs_log_format.h"
26#include "xfs_trans_resv.h"
a844f451 27#include "xfs_bit.h"
1da177e4 28#include "xfs_sb.h"
1da177e4 29#include "xfs_mount.h"
1da177e4 30#include "xfs_inode.h"
239880ef 31#include "xfs_trans.h"
1da177e4 32#include "xfs_error.h"
a4fbe6ab 33#include "xfs_quota.h"
1da177e4 34#include "xfs_qm.h"
0b1b213f 35#include "xfs_trace.h"
6d8b79cf 36#include "xfs_icache.h"
1da177e4 37
1da177e4
LT
38STATIC 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);
1da177e4 41STATIC uint xfs_qm_export_flags(uint);
1da177e4 42STATIC uint xfs_qm_export_qtype_flags(uint);
1da177e4 43
1da177e4
LT
44/*
45 * 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 */
fcafb71b 52int
1da177e4
LT
53xfs_qm_scall_quotaoff(
54 xfs_mount_t *mp,
fcafb71b 55 uint flags)
1da177e4 56{
8a7b8a89 57 struct xfs_quotainfo *q = mp->m_quotainfo;
1da177e4 58 uint dqtype;
1da177e4
LT
59 int error;
60 uint inactivate_flags;
61 xfs_qoff_logitem_t *qoffstart;
1da177e4 62
1da177e4
LT
63 /*
64 * No file system can have quotas enabled on disk but not in core.
65 * Note that quota utilities (like quotaoff) _expect_
2451337d 66 * errno == -EEXIST here.
1da177e4
LT
67 */
68 if ((mp->m_qflags & flags) == 0)
2451337d 69 return -EEXIST;
1da177e4
LT
70 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 */
8a7b8a89
CH
80 ASSERT(q);
81 mutex_lock(&q->qi_quotaofflock);
1da177e4
LT
82
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
3685c2a1 89 spin_lock(&mp->m_sb_lock);
1da177e4 90 mp->m_sb.sb_qflags = mp->m_qflags;
3685c2a1 91 spin_unlock(&mp->m_sb_lock);
8a7b8a89 92 mutex_unlock(&q->qi_quotaofflock);
1da177e4
LT
93
94 /* XXX what to do if error ? Revert back to old vals incore ? */
95 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
d99831ff 96 return error;
1da177e4
LT
97 }
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;
83e782e1 113 flags |= (XFS_GQUOTA_CHKD | XFS_GQUOTA_ENFD);
1da177e4 114 inactivate_flags |= XFS_GQUOTA_ACTIVE;
92f8ff73
CS
115 }
116 if (flags & XFS_PQUOTA_ACCT) {
c8ad20ff 117 dqtype |= XFS_QMOPT_PQUOTA;
83e782e1 118 flags |= (XFS_PQUOTA_CHKD | XFS_PQUOTA_ENFD);
c8ad20ff 119 inactivate_flags |= XFS_PQUOTA_ACTIVE;
1da177e4
LT
120 }
121
122 /*
123 * Nothing to do? Don't complain. This happens when we're just
124 * turning off quota enforcement.
125 */
8a7b8a89
CH
126 if ((mp->m_qflags & flags) == 0)
127 goto out_unlock;
1da177e4
LT
128
129 /*
130 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
cb6edc26
DC
131 * and synchronously. If we fail to write, we should abort the
132 * operation as it cannot be recovered safely if we crash.
1da177e4 133 */
cb6edc26
DC
134 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
135 if (error)
8a7b8a89 136 goto out_unlock;
1da177e4
LT
137
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
25985edc 166 * don't want to take a mrlock every time we depend on quotas being on.
1da177e4 167 */
b84a3a96 168 mp->m_qflags &= ~flags;
1da177e4
LT
169
170 /*
171 * Go through all the dquots of this file system and purge them,
b84a3a96 172 * according to what was turned off.
1da177e4 173 */
b84a3a96 174 xfs_qm_dqpurge_all(mp, dqtype);
1da177e4
LT
175
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 */
cb6edc26
DC
187 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);
8a7b8a89 191 goto out_unlock;
cb6edc26 192 }
1da177e4
LT
193
194 /*
c31ad439 195 * If all quotas are completely turned off, close shop.
1da177e4 196 */
c31ad439 197 if (mp->m_qflags == 0) {
8a7b8a89 198 mutex_unlock(&q->qi_quotaofflock);
1da177e4 199 xfs_qm_destroy_quotainfo(mp);
d99831ff 200 return 0;
1da177e4
LT
201 }
202
203 /*
8a7b8a89 204 * Release our quotainode references if we don't need them anymore.
1da177e4 205 */
8a7b8a89
CH
206 if ((dqtype & XFS_QMOPT_UQUOTA) && q->qi_uquotaip) {
207 IRELE(q->qi_uquotaip);
208 q->qi_uquotaip = NULL;
1da177e4 209 }
92f8ff73 210 if ((dqtype & XFS_QMOPT_GQUOTA) && q->qi_gquotaip) {
8a7b8a89
CH
211 IRELE(q->qi_gquotaip);
212 q->qi_gquotaip = NULL;
1da177e4 213 }
92f8ff73
CS
214 if ((dqtype & XFS_QMOPT_PQUOTA) && q->qi_pquotaip) {
215 IRELE(q->qi_pquotaip);
216 q->qi_pquotaip = NULL;
217 }
1da177e4 218
8a7b8a89
CH
219out_unlock:
220 mutex_unlock(&q->qi_quotaofflock);
221 return error;
1da177e4
LT
222}
223
5d18898b
CH
224STATIC 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);
3d3c8b52 243 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
5d18898b
CH
244 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);
ddc3415a 251 xfs_trans_ijoin(tp, ip, 0);
5d18898b 252
673e8e59 253 ip->i_d.di_size = 0;
673e8e59
CH
254 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
255
256 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
5d18898b
CH
257 if (error) {
258 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES |
259 XFS_TRANS_ABORT);
260 goto out_unlock;
261 }
262
673e8e59
CH
263 ASSERT(ip->i_d.di_nextents == 0);
264
dcd79a14 265 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
5d18898b
CH
266 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
fcafb71b 275int
1da177e4
LT
276xfs_qm_scall_trunc_qfiles(
277 xfs_mount_t *mp,
278 uint flags)
279{
2451337d 280 int error = -EINVAL;
1da177e4 281
f58522c5
ES
282 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0 ||
283 (flags & ~XFS_DQ_ALLTYPES)) {
08e96e1a 284 xfs_debug(mp, "%s: flags=%x m_qflags=%x",
8221112b 285 __func__, flags, mp->m_qflags);
2451337d 286 return -EINVAL;
1da177e4
LT
287 }
288
c61a9e39 289 if (flags & XFS_DQ_USER) {
5d18898b 290 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_uquotino);
c61a9e39
JL
291 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 }
d892d586 299 if (flags & XFS_DQ_PROJ)
c61a9e39 300 error = xfs_qm_scall_trunc_qfile(mp, mp->m_sb.sb_pquotino);
1da177e4 301
c61a9e39 302 return error;
1da177e4
LT
303}
304
1da177e4
LT
305/*
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 */
fcafb71b 310int
1da177e4
LT
311xfs_qm_scall_quotaon(
312 xfs_mount_t *mp,
313 uint flags)
314{
315 int error;
1da177e4 316 uint qf;
1da177e4
LT
317 __int64_t sbflags;
318
1da177e4
LT
319 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
320 /*
321 * Switching on quota accounting must be done at mount time.
322 */
1da177e4
LT
323 flags &= ~(XFS_ALL_QUOTA_ACCT);
324
325 sbflags = 0;
326
327 if (flags == 0) {
08e96e1a 328 xfs_debug(mp, "%s: zero flags, m_qflags=%x",
8221112b 329 __func__, mp->m_qflags);
2451337d 330 return -EINVAL;
1da177e4
LT
331 }
332
1da177e4
LT
333 /*
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 */
fbf64b3d 338 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
83e782e1 339 (flags & XFS_UQUOTA_ENFD)) ||
fbf64b3d 340 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
83e782e1 341 (flags & XFS_GQUOTA_ENFD)) ||
fbf64b3d 342 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
83e782e1 343 (flags & XFS_PQUOTA_ENFD))) {
8221112b 344 xfs_debug(mp,
08e96e1a 345 "%s: Can't enforce without acct, flags=%x sbflags=%x",
8221112b 346 __func__, flags, mp->m_sb.sb_qflags);
2451337d 347 return -EINVAL;
1da177e4
LT
348 }
349 /*
25985edc 350 * If everything's up to-date incore, then don't waste time.
1da177e4
LT
351 */
352 if ((mp->m_qflags & flags) == flags)
2451337d 353 return -EEXIST;
1da177e4
LT
354
355 /*
356 * Change sb_qflags on disk but not incore mp->qflags
357 * if this is the root filesystem.
358 */
3685c2a1 359 spin_lock(&mp->m_sb_lock);
1da177e4
LT
360 qf = mp->m_sb.sb_qflags;
361 mp->m_sb.sb_qflags = qf | flags;
3685c2a1 362 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
363
364 /*
365 * There's nothing to change if it's the same.
366 */
367 if ((qf & flags) == flags && sbflags == 0)
2451337d 368 return -EEXIST;
1da177e4
LT
369 sbflags |= XFS_SB_QFLAGS;
370
371 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
d99831ff 372 return error;
1da177e4
LT
373 /*
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)) ||
c8ad20ff
NS
378 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
379 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
380 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
fbf64b3d 381 (mp->m_qflags & XFS_GQUOTA_ACCT)))
d99831ff 382 return 0;
1da177e4
LT
383
384 if (! XFS_IS_QUOTA_RUNNING(mp))
2451337d 385 return -ESRCH;
1da177e4
LT
386
387 /*
388 * Switch on quota enforcement in core.
389 */
8a7b8a89 390 mutex_lock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 391 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
8a7b8a89 392 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock);
1da177e4 393
d99831ff 394 return 0;
1da177e4
LT
395}
396
397
1da177e4
LT
398/*
399 * Return quota status information, such as uquota-off, enforcements, etc.
5d5e3d57 400 * for Q_XGETQSTAT command.
1da177e4 401 */
fcafb71b 402int
1da177e4 403xfs_qm_scall_getqstat(
8a7b8a89
CH
404 struct xfs_mount *mp,
405 struct fs_quota_stat *out)
1da177e4 406{
8a7b8a89 407 struct xfs_quotainfo *q = mp->m_quotainfo;
113a5683
CS
408 struct xfs_inode *uip = NULL;
409 struct xfs_inode *gip = NULL;
d892d586 410 struct xfs_inode *pip = NULL;
113a5683
CS
411 bool tempuqip = false;
412 bool tempgqip = false;
d892d586 413 bool temppqip = false;
1da177e4 414
1da177e4
LT
415 memset(out, 0, sizeof(fs_quota_stat_t));
416
417 out->qs_version = FS_QSTAT_VERSION;
62118709 418 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
1da177e4
LT
419 out->qs_uquota.qfs_ino = NULLFSINO;
420 out->qs_gquota.qfs_ino = NULLFSINO;
d99831ff 421 return 0;
1da177e4 422 }
d892d586 423
1da177e4
LT
424 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
425 (XFS_ALL_QUOTA_ACCT|
426 XFS_ALL_QUOTA_ENFD));
8a7b8a89
CH
427 if (q) {
428 uip = q->qi_uquotaip;
429 gip = q->qi_gquotaip;
d892d586 430 pip = q->qi_pquotaip;
1da177e4
LT
431 }
432 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
433 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
7b6259e7 434 0, 0, &uip) == 0)
667a9291 435 tempuqip = true;
1da177e4
LT
436 }
437 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
438 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
7b6259e7 439 0, 0, &gip) == 0)
667a9291 440 tempgqip = true;
1da177e4 441 }
d892d586
CS
442 /*
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;
1da177e4 455 if (uip) {
d892d586 456 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
1da177e4
LT
457 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
458 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
459 if (tempuqip)
43355099 460 IRELE(uip);
1da177e4 461 }
d892d586 462
1da177e4 463 if (gip) {
d892d586 464 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
1da177e4
LT
465 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
466 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
467 if (tempgqip)
43355099 468 IRELE(gip);
1da177e4 469 }
d892d586
CS
470 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)
5d5e3d57
CS
475 IRELE(pip);
476 }
477 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;
484 }
485 return 0;
486}
487
488/*
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;
d99831ff 509 return 0;
5d5e3d57
CS
510 }
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)
d892d586
CS
556 IRELE(pip);
557 }
8a7b8a89
CH
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;
1da177e4 565 }
8a7b8a89 566 return 0;
1da177e4
LT
567}
568
c472b432
CH
569#define XFS_DQ_MASK \
570 (FS_DQ_LIMIT_MASK | FS_DQ_TIMER_MASK | FS_DQ_WARNS_MASK)
571
1da177e4
LT
572/*
573 * Adjust quota limits, and start/stop timers accordingly.
574 */
fcafb71b 575int
1da177e4 576xfs_qm_scall_setqlim(
b1366451 577 struct xfs_mount *mp,
1da177e4
LT
578 xfs_dqid_t id,
579 uint type,
580 fs_disk_quota_t *newlim)
581{
8a7b8a89 582 struct xfs_quotainfo *q = mp->m_quotainfo;
b1366451
BF
583 struct xfs_disk_dquot *ddq;
584 struct xfs_dquot *dqp;
585 struct xfs_trans *tp;
1da177e4
LT
586 int error;
587 xfs_qcnt_t hard, soft;
588
c472b432 589 if (newlim->d_fieldmask & ~XFS_DQ_MASK)
2451337d 590 return -EINVAL;
c472b432
CH
591 if ((newlim->d_fieldmask & XFS_DQ_MASK) == 0)
592 return 0;
1da177e4 593
1da177e4
LT
594 /*
595 * We don't want to race with a quotaoff so take the quotaoff lock.
f648167f
DC
596 * We don't hold an inode lock, so there's nothing else to stop
597 * a quotaoff from happening.
1da177e4 598 */
8a7b8a89 599 mutex_lock(&q->qi_quotaofflock);
1da177e4
LT
600
601 /*
f648167f
DC
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.
1da177e4 607 */
f648167f
DC
608 error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp);
609 if (error) {
2451337d 610 ASSERT(error != -ENOENT);
8a7b8a89 611 goto out_unlock;
1da177e4 612 }
f648167f
DC
613 xfs_dqunlock(dqp);
614
615 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
3d3c8b52 616 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_setqlim, 0, 0);
f648167f
DC
617 if (error) {
618 xfs_trans_cancel(tp, 0);
619 goto out_rele;
620 }
621
622 xfs_dqlock(dqp);
1da177e4
LT
623 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) :
1149d96a 631 be64_to_cpu(ddq->d_blk_hardlimit);
1da177e4
LT
632 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
633 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
1149d96a 634 be64_to_cpu(ddq->d_blk_softlimit);
1da177e4 635 if (hard == 0 || hard >= soft) {
1149d96a
CH
636 ddq->d_blk_hardlimit = cpu_to_be64(hard);
637 ddq->d_blk_softlimit = cpu_to_be64(soft);
b1366451 638 xfs_dquot_set_prealloc_limits(dqp);
1da177e4 639 if (id == 0) {
8a7b8a89
CH
640 q->qi_bhardlimit = hard;
641 q->qi_bsoftlimit = soft;
1da177e4
LT
642 }
643 } else {
08e96e1a 644 xfs_debug(mp, "blkhard %Ld < blksoft %Ld", hard, soft);
1da177e4
LT
645 }
646 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
647 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
1149d96a 648 be64_to_cpu(ddq->d_rtb_hardlimit);
1da177e4
LT
649 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
650 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
1149d96a 651 be64_to_cpu(ddq->d_rtb_softlimit);
1da177e4 652 if (hard == 0 || hard >= soft) {
1149d96a
CH
653 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
654 ddq->d_rtb_softlimit = cpu_to_be64(soft);
1da177e4 655 if (id == 0) {
8a7b8a89
CH
656 q->qi_rtbhardlimit = hard;
657 q->qi_rtbsoftlimit = soft;
1da177e4
LT
658 }
659 } else {
08e96e1a 660 xfs_debug(mp, "rtbhard %Ld < rtbsoft %Ld", hard, soft);
1da177e4
LT
661 }
662
663 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
664 (xfs_qcnt_t) newlim->d_ino_hardlimit :
1149d96a 665 be64_to_cpu(ddq->d_ino_hardlimit);
1da177e4
LT
666 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
667 (xfs_qcnt_t) newlim->d_ino_softlimit :
1149d96a 668 be64_to_cpu(ddq->d_ino_softlimit);
1da177e4 669 if (hard == 0 || hard >= soft) {
1149d96a
CH
670 ddq->d_ino_hardlimit = cpu_to_be64(hard);
671 ddq->d_ino_softlimit = cpu_to_be64(soft);
1da177e4 672 if (id == 0) {
8a7b8a89
CH
673 q->qi_ihardlimit = hard;
674 q->qi_isoftlimit = soft;
1da177e4
LT
675 }
676 } else {
08e96e1a 677 xfs_debug(mp, "ihard %Ld < isoft %Ld", hard, soft);
1da177e4
LT
678 }
679
754002b4
NS
680 /*
681 * Update warnings counter(s) if requested
682 */
683 if (newlim->d_fieldmask & FS_DQ_BWARNS)
1149d96a 684 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
754002b4 685 if (newlim->d_fieldmask & FS_DQ_IWARNS)
1149d96a 686 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
754002b4 687 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
1149d96a 688 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
754002b4 689
1da177e4
LT
690 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
754002b4
NS
695 * soft and hard limit values (already done, above), and
696 * for warnings.
1da177e4
LT
697 */
698 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
8a7b8a89 699 q->qi_btimelimit = newlim->d_btimer;
1149d96a 700 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
1da177e4
LT
701 }
702 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
8a7b8a89 703 q->qi_itimelimit = newlim->d_itimer;
1149d96a 704 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
1da177e4
LT
705 }
706 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
8a7b8a89 707 q->qi_rtbtimelimit = newlim->d_rtbtimer;
1149d96a 708 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
1da177e4 709 }
754002b4 710 if (newlim->d_fieldmask & FS_DQ_BWARNS)
8a7b8a89 711 q->qi_bwarnlimit = newlim->d_bwarns;
754002b4 712 if (newlim->d_fieldmask & FS_DQ_IWARNS)
8a7b8a89 713 q->qi_iwarnlimit = newlim->d_iwarns;
754002b4 714 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
8a7b8a89 715 q->qi_rtbwarnlimit = newlim->d_rtbwarns;
754002b4 716 } else {
1da177e4
LT
717 /*
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
e5720eec 729 error = xfs_trans_commit(tp, 0);
1da177e4 730
f648167f
DC
731out_rele:
732 xfs_qm_dqrele(dqp);
733out_unlock:
8a7b8a89 734 mutex_unlock(&q->qi_quotaofflock);
e5720eec 735 return error;
1da177e4
LT
736}
737
1da177e4
LT
738STATIC int
739xfs_qm_log_quotaoff_end(
740 xfs_mount_t *mp,
741 xfs_qoff_logitem_t *startqoff,
742 uint flags)
743{
c8ad20ff 744 xfs_trans_t *tp;
1da177e4 745 int error;
c8ad20ff 746 xfs_qoff_logitem_t *qoffi;
1da177e4
LT
747
748 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
749
3d3c8b52 750 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_equotaoff, 0, 0);
762d7ba6 751 if (error) {
1da177e4 752 xfs_trans_cancel(tp, 0);
d99831ff 753 return error;
1da177e4
LT
754 }
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);
1c72bf90 766 error = xfs_trans_commit(tp, 0);
d99831ff 767 return error;
1da177e4
LT
768}
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;
5d45ee1b
BF
779 xfs_qoff_logitem_t *qoffi;
780
781 *qoffstartp = NULL;
1da177e4
LT
782
783 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
3d3c8b52 784 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_quotaoff, 0, 0);
5d45ee1b
BF
785 if (error) {
786 xfs_trans_cancel(tp, 0);
787 goto out;
788 }
1da177e4
LT
789
790 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
791 xfs_trans_log_quotaoff_item(tp, qoffi);
792
3685c2a1 793 spin_lock(&mp->m_sb_lock);
1da177e4 794 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
3685c2a1 795 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
796
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);
1c72bf90 805 error = xfs_trans_commit(tp, 0);
5d45ee1b
BF
806 if (error)
807 goto out;
1da177e4 808
1da177e4 809 *qoffstartp = qoffi;
5d45ee1b 810out:
d99831ff 811 return error;
1da177e4
LT
812}
813
814
18535a7e
CH
815int
816xfs_qm_scall_getquota(
817 struct xfs_mount *mp,
818 xfs_dqid_t id,
819 uint type,
1da177e4
LT
820 struct fs_disk_quota *dst)
821{
18535a7e
CH
822 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)) {
2451337d 839 error = -ENOENT;
18535a7e
CH
840 goto out_put;
841 }
842
1da177e4 843 memset(dst, 0, sizeof(*dst));
18535a7e
CH
844 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);
1149d96a 847 dst->d_blk_hardlimit =
18535a7e 848 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_blk_hardlimit));
1149d96a 849 dst->d_blk_softlimit =
18535a7e
CH
850 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);
89605011
CH
853 dst->d_bcount = XFS_FSB_TO_BB(mp, dqp->q_res_bcount);
854 dst->d_icount = dqp->q_res_icount;
18535a7e
CH
855 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);
1149d96a 859 dst->d_rtb_hardlimit =
18535a7e 860 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_hardlimit));
1149d96a 861 dst->d_rtb_softlimit =
18535a7e 862 XFS_FSB_TO_BB(mp, be64_to_cpu(dqp->q_core.d_rtb_softlimit));
89605011 863 dst->d_rtbcount = XFS_FSB_TO_BB(mp, dqp->q_res_rtbcount);
18535a7e
CH
864 dst->d_rtbtimer = be32_to_cpu(dqp->q_core.d_rtbtimer);
865 dst->d_rtbwarns = be16_to_cpu(dqp->q_core.d_rtbwarns);
1da177e4
LT
866
867 /*
868 * Internally, we don't reset all the timers when quota enforcement
c41564b5 869 * gets turned off. No need to confuse the user level code,
1da177e4
LT
870 * so return zeroes in that case.
871 */
83e782e1
CS
872 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)) {
1da177e4
LT
878 dst->d_btimer = 0;
879 dst->d_itimer = 0;
880 dst->d_rtbtimer = 0;
881 }
882
883#ifdef DEBUG
ade7ce31 884 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == FS_USER_QUOTA) ||
83e782e1
CS
885 (XFS_IS_GQUOTA_ENFORCED(mp) && dst->d_flags == FS_GROUP_QUOTA) ||
886 (XFS_IS_PQUOTA_ENFORCED(mp) && dst->d_flags == FS_PROJ_QUOTA)) &&
e6d29426 887 dst->d_id != 0) {
f7555032 888 if ((dst->d_bcount > dst->d_blk_softlimit) &&
1da177e4
LT
889 (dst->d_blk_softlimit > 0)) {
890 ASSERT(dst->d_btimer != 0);
891 }
f7555032 892 if ((dst->d_icount > dst->d_ino_softlimit) &&
1da177e4
LT
893 (dst->d_ino_softlimit > 0)) {
894 ASSERT(dst->d_itimer != 0);
895 }
896 }
897#endif
18535a7e
CH
898out_put:
899 xfs_qm_dqput(dqp);
900 return error;
1da177e4
LT
901}
902
1da177e4
LT
903STATIC uint
904xfs_qm_export_qtype_flags(
905 uint flags)
906{
907 /*
c8ad20ff 908 * Can't be more than one, or none.
1da177e4 909 */
ade7ce31
CH
910 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);
1da177e4
LT
917
918 return (flags & XFS_DQ_USER) ?
ade7ce31
CH
919 FS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
920 FS_PROJ_QUOTA : FS_GROUP_QUOTA;
1da177e4
LT
921}
922
1da177e4
LT
923STATIC uint
924xfs_qm_export_flags(
925 uint flags)
926{
927 uint uflags;
928
929 uflags = 0;
930 if (flags & XFS_UQUOTA_ACCT)
ade7ce31 931 uflags |= FS_QUOTA_UDQ_ACCT;
1da177e4 932 if (flags & XFS_GQUOTA_ACCT)
ade7ce31 933 uflags |= FS_QUOTA_GDQ_ACCT;
83e782e1
CS
934 if (flags & XFS_PQUOTA_ACCT)
935 uflags |= FS_QUOTA_PDQ_ACCT;
1da177e4 936 if (flags & XFS_UQUOTA_ENFD)
ade7ce31 937 uflags |= FS_QUOTA_UDQ_ENFD;
83e782e1
CS
938 if (flags & XFS_GQUOTA_ENFD)
939 uflags |= FS_QUOTA_GDQ_ENFD;
940 if (flags & XFS_PQUOTA_ENFD)
941 uflags |= FS_QUOTA_PDQ_ENFD;
d99831ff 942 return uflags;
1da177e4
LT
943}
944
945
fe588ed3
CH
946STATIC int
947xfs_dqrele_inode(
948 struct xfs_inode *ip,
a454f742
BF
949 int flags,
950 void *args)
1da177e4 951{
fe588ed3 952 /* skip quota inodes */
8a7b8a89 953 if (ip == ip->i_mount->m_quotainfo->qi_uquotaip ||
92f8ff73
CS
954 ip == ip->i_mount->m_quotainfo->qi_gquotaip ||
955 ip == ip->i_mount->m_quotainfo->qi_pquotaip) {
fe588ed3
CH
956 ASSERT(ip->i_udquot == NULL);
957 ASSERT(ip->i_gdquot == NULL);
92f8ff73 958 ASSERT(ip->i_pdquot == NULL);
fe588ed3
CH
959 return 0;
960 }
cb4f0d1d 961
fe588ed3
CH
962 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 }
92f8ff73 967 if ((flags & XFS_GQUOTA_ACCT) && ip->i_gdquot) {
fe588ed3
CH
968 xfs_qm_dqrele(ip->i_gdquot);
969 ip->i_gdquot = NULL;
970 }
92f8ff73
CS
971 if ((flags & XFS_PQUOTA_ACCT) && ip->i_pdquot) {
972 xfs_qm_dqrele(ip->i_pdquot);
973 ip->i_pdquot = NULL;
974 }
f2d67614 975 xfs_iunlock(ip, XFS_ILOCK_EXCL);
fe588ed3 976 return 0;
5b4d89ae
DC
977}
978
fe588ed3 979
5b4d89ae
DC
980/*
981 * Go thru all the inodes in the file system, releasing their dquots.
fe588ed3 982 *
5b4d89ae 983 * Note that the mount structure gets modified to indicate that quotas are off
fe588ed3 984 * AFTER this, in the case of quotaoff.
5b4d89ae
DC
985 */
986void
987xfs_qm_dqrele_all_inodes(
988 struct xfs_mount *mp,
989 uint flags)
990{
5b4d89ae 991 ASSERT(mp->m_quotainfo);
a454f742 992 xfs_inode_ag_iterator(mp, xfs_dqrele_inode, flags, NULL);
1da177e4 993}