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