[XFS] Catch errors when turning off quotas.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / xfs / quota / 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"
a844f451 23#include "xfs_bit.h"
1da177e4 24#include "xfs_log.h"
a844f451 25#include "xfs_inum.h"
1da177e4
LT
26#include "xfs_trans.h"
27#include "xfs_sb.h"
a844f451 28#include "xfs_ag.h"
1da177e4
LT
29#include "xfs_dir2.h"
30#include "xfs_alloc.h"
31#include "xfs_dmapi.h"
32#include "xfs_quota.h"
33#include "xfs_mount.h"
1da177e4 34#include "xfs_bmap_btree.h"
a844f451 35#include "xfs_alloc_btree.h"
1da177e4 36#include "xfs_ialloc_btree.h"
1da177e4 37#include "xfs_dir2_sf.h"
a844f451 38#include "xfs_attr_sf.h"
1da177e4
LT
39#include "xfs_dinode.h"
40#include "xfs_inode.h"
a844f451
NS
41#include "xfs_ialloc.h"
42#include "xfs_itable.h"
1da177e4 43#include "xfs_bmap.h"
a844f451 44#include "xfs_btree.h"
1da177e4
LT
45#include "xfs_rtalloc.h"
46#include "xfs_error.h"
1da177e4
LT
47#include "xfs_rw.h"
48#include "xfs_acl.h"
1da177e4
LT
49#include "xfs_attr.h"
50#include "xfs_buf_item.h"
51#include "xfs_utils.h"
1da177e4
LT
52#include "xfs_qm.h"
53
54#ifdef DEBUG
55# define qdprintk(s, args...) cmn_err(CE_DEBUG, s, ## args)
56#else
57# define qdprintk(s, args...) do { } while (0)
58#endif
59
60STATIC int xfs_qm_scall_trunc_qfiles(xfs_mount_t *, uint);
61STATIC int xfs_qm_scall_getquota(xfs_mount_t *, xfs_dqid_t, uint,
62 fs_disk_quota_t *);
63STATIC int xfs_qm_scall_getqstat(xfs_mount_t *, fs_quota_stat_t *);
64STATIC int xfs_qm_scall_setqlim(xfs_mount_t *, xfs_dqid_t, uint,
65 fs_disk_quota_t *);
66STATIC int xfs_qm_scall_quotaon(xfs_mount_t *, uint);
67STATIC int xfs_qm_scall_quotaoff(xfs_mount_t *, uint, boolean_t);
68STATIC int xfs_qm_log_quotaoff(xfs_mount_t *, xfs_qoff_logitem_t **, uint);
69STATIC int xfs_qm_log_quotaoff_end(xfs_mount_t *, xfs_qoff_logitem_t *,
70 uint);
71STATIC uint xfs_qm_import_flags(uint);
72STATIC uint xfs_qm_export_flags(uint);
73STATIC uint xfs_qm_import_qtype_flags(uint);
74STATIC uint xfs_qm_export_qtype_flags(uint);
75STATIC void xfs_qm_export_dquot(xfs_mount_t *, xfs_disk_dquot_t *,
76 fs_disk_quota_t *);
77
78
79/*
80 * The main distribution switch of all XFS quotactl system calls.
81 */
82int
83xfs_qm_quotactl(
b09cc771 84 xfs_mount_t *mp,
1da177e4
LT
85 int cmd,
86 int id,
87 xfs_caddr_t addr)
88{
1da177e4 89 int error;
1da177e4 90
ee34807a 91 ASSERT(addr != NULL || cmd == Q_XQUOTASYNC);
1da177e4
LT
92
93 /*
94 * The following commands are valid even when quotaoff.
95 */
96 switch (cmd) {
c8ad20ff 97 case Q_XQUOTARM:
1da177e4 98 /*
c8ad20ff 99 * Truncate quota files. quota must be off.
1da177e4 100 */
c310ab6c 101 if (XFS_IS_QUOTA_ON(mp))
1da177e4 102 return XFS_ERROR(EINVAL);
bd186aa9 103 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
104 return XFS_ERROR(EROFS);
105 return (xfs_qm_scall_trunc_qfiles(mp,
106 xfs_qm_import_qtype_flags(*(uint *)addr)));
c8ad20ff
NS
107
108 case Q_XGETQSTAT:
1da177e4
LT
109 /*
110 * Get quota status information.
111 */
1da177e4
LT
112 return (xfs_qm_scall_getqstat(mp, (fs_quota_stat_t *)addr));
113
c8ad20ff 114 case Q_XQUOTAON:
1da177e4 115 /*
c8ad20ff
NS
116 * QUOTAON - enabling quota enforcement.
117 * Quota accounting must be turned on at mount time.
1da177e4 118 */
bd186aa9 119 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
120 return XFS_ERROR(EROFS);
121 return (xfs_qm_scall_quotaon(mp,
122 xfs_qm_import_flags(*(uint *)addr)));
c8ad20ff
NS
123
124 case Q_XQUOTAOFF:
bd186aa9 125 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
126 return XFS_ERROR(EROFS);
127 break;
128
ee34807a 129 case Q_XQUOTASYNC:
1f9b3b64 130 return (xfs_sync_inodes(mp, SYNC_DELWRI, NULL));
ee34807a 131
c8ad20ff 132 default:
1da177e4
LT
133 break;
134 }
135
136 if (! XFS_IS_QUOTA_ON(mp))
137 return XFS_ERROR(ESRCH);
138
139 switch (cmd) {
c8ad20ff 140 case Q_XQUOTAOFF:
bd186aa9 141 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
142 return XFS_ERROR(EROFS);
143 error = xfs_qm_scall_quotaoff(mp,
144 xfs_qm_import_flags(*(uint *)addr),
145 B_FALSE);
146 break;
147
c8ad20ff 148 case Q_XGETQUOTA:
1da177e4
LT
149 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_USER,
150 (fs_disk_quota_t *)addr);
151 break;
c8ad20ff
NS
152 case Q_XGETGQUOTA:
153 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
154 (fs_disk_quota_t *)addr);
155 break;
156 case Q_XGETPQUOTA:
157 error = xfs_qm_scall_getquota(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
158 (fs_disk_quota_t *)addr);
159 break;
160
161 case Q_XSETQLIM:
bd186aa9 162 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
163 return XFS_ERROR(EROFS);
164 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_USER,
165 (fs_disk_quota_t *)addr);
166 break;
c8ad20ff 167 case Q_XSETGQLIM:
bd186aa9 168 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
169 return XFS_ERROR(EROFS);
170 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_GROUP,
171 (fs_disk_quota_t *)addr);
172 break;
c8ad20ff 173 case Q_XSETPQLIM:
bd186aa9 174 if (mp->m_flags & XFS_MOUNT_RDONLY)
c8ad20ff
NS
175 return XFS_ERROR(EROFS);
176 error = xfs_qm_scall_setqlim(mp, (xfs_dqid_t)id, XFS_DQ_PROJ,
177 (fs_disk_quota_t *)addr);
1da177e4
LT
178 break;
179
c8ad20ff 180 default:
1da177e4
LT
181 error = XFS_ERROR(EINVAL);
182 break;
183 }
184
185 return (error);
186}
187
188/*
189 * Turn off quota accounting and/or enforcement for all udquots and/or
190 * gdquots. Called only at unmount time.
191 *
192 * This assumes that there are no dquots of this file system cached
193 * incore, and modifies the ondisk dquot directly. Therefore, for example,
194 * it is an error to call this twice, without purging the cache.
195 */
196STATIC int
197xfs_qm_scall_quotaoff(
198 xfs_mount_t *mp,
199 uint flags,
200 boolean_t force)
201{
202 uint dqtype;
1da177e4
LT
203 int error;
204 uint inactivate_flags;
205 xfs_qoff_logitem_t *qoffstart;
206 int nculprits;
207
208 if (!force && !capable(CAP_SYS_ADMIN))
209 return XFS_ERROR(EPERM);
210 /*
211 * No file system can have quotas enabled on disk but not in core.
212 * Note that quota utilities (like quotaoff) _expect_
213 * errno == EEXIST here.
214 */
215 if ((mp->m_qflags & flags) == 0)
216 return XFS_ERROR(EEXIST);
217 error = 0;
218
219 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
220
221 /*
222 * We don't want to deal with two quotaoffs messing up each other,
223 * so we're going to serialize it. quotaoff isn't exactly a performance
224 * critical thing.
225 * If quotaoff, then we must be dealing with the root filesystem.
226 */
227 ASSERT(mp->m_quotainfo);
228 if (mp->m_quotainfo)
794ee1ba 229 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
1da177e4
LT
230
231 ASSERT(mp->m_quotainfo);
232
233 /*
234 * If we're just turning off quota enforcement, change mp and go.
235 */
236 if ((flags & XFS_ALL_QUOTA_ACCT) == 0) {
237 mp->m_qflags &= ~(flags);
238
3685c2a1 239 spin_lock(&mp->m_sb_lock);
1da177e4 240 mp->m_sb.sb_qflags = mp->m_qflags;
3685c2a1 241 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
242 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
243
244 /* XXX what to do if error ? Revert back to old vals incore ? */
245 error = xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS);
246 return (error);
247 }
248
249 dqtype = 0;
250 inactivate_flags = 0;
251 /*
252 * If accounting is off, we must turn enforcement off, clear the
253 * quota 'CHKD' certificate to make it known that we have to
254 * do a quotacheck the next time this quota is turned on.
255 */
256 if (flags & XFS_UQUOTA_ACCT) {
257 dqtype |= XFS_QMOPT_UQUOTA;
258 flags |= (XFS_UQUOTA_CHKD | XFS_UQUOTA_ENFD);
259 inactivate_flags |= XFS_UQUOTA_ACTIVE;
260 }
261 if (flags & XFS_GQUOTA_ACCT) {
262 dqtype |= XFS_QMOPT_GQUOTA;
c8ad20ff 263 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
1da177e4 264 inactivate_flags |= XFS_GQUOTA_ACTIVE;
c8ad20ff
NS
265 } else if (flags & XFS_PQUOTA_ACCT) {
266 dqtype |= XFS_QMOPT_PQUOTA;
267 flags |= (XFS_OQUOTA_CHKD | XFS_OQUOTA_ENFD);
268 inactivate_flags |= XFS_PQUOTA_ACTIVE;
1da177e4
LT
269 }
270
271 /*
272 * Nothing to do? Don't complain. This happens when we're just
273 * turning off quota enforcement.
274 */
275 if ((mp->m_qflags & flags) == 0) {
276 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
277 return (0);
278 }
279
280 /*
281 * Write the LI_QUOTAOFF log record, and do SB changes atomically,
cb6edc26
DC
282 * and synchronously. If we fail to write, we should abort the
283 * operation as it cannot be recovered safely if we crash.
1da177e4 284 */
cb6edc26
DC
285 error = xfs_qm_log_quotaoff(mp, &qoffstart, flags);
286 if (error)
287 goto out_error;
1da177e4
LT
288
289 /*
290 * Next we clear the XFS_MOUNT_*DQ_ACTIVE bit(s) in the mount struct
291 * to take care of the race between dqget and quotaoff. We don't take
292 * any special locks to reset these bits. All processes need to check
293 * these bits *after* taking inode lock(s) to see if the particular
294 * quota type is in the process of being turned off. If *ACTIVE, it is
295 * guaranteed that all dquot structures and all quotainode ptrs will all
296 * stay valid as long as that inode is kept locked.
297 *
298 * There is no turning back after this.
299 */
300 mp->m_qflags &= ~inactivate_flags;
301
302 /*
303 * Give back all the dquot reference(s) held by inodes.
304 * Here we go thru every single incore inode in this file system, and
305 * do a dqrele on the i_udquot/i_gdquot that it may have.
306 * Essentially, as long as somebody has an inode locked, this guarantees
307 * that quotas will not be turned off. This is handy because in a
308 * transaction once we lock the inode(s) and check for quotaon, we can
309 * depend on the quota inodes (and other things) being valid as long as
310 * we keep the lock(s).
311 */
312 xfs_qm_dqrele_all_inodes(mp, flags);
313
314 /*
315 * Next we make the changes in the quota flag in the mount struct.
316 * This isn't protected by a particular lock directly, because we
317 * don't want to take a mrlock everytime we depend on quotas being on.
318 */
319 mp->m_qflags &= ~(flags);
320
321 /*
322 * Go through all the dquots of this file system and purge them,
323 * according to what was turned off. We may not be able to get rid
324 * of all dquots, because dquots can have temporary references that
325 * are not attached to inodes. eg. xfs_setattr, xfs_create.
326 * So, if we couldn't purge all the dquots from the filesystem,
327 * we can't get rid of the incore data structures.
328 */
329 while ((nculprits = xfs_qm_dqpurge_all(mp, dqtype|XFS_QMOPT_QUOTAOFF)))
330 delay(10 * nculprits);
331
332 /*
333 * Transactions that had started before ACTIVE state bit was cleared
334 * could have logged many dquots, so they'd have higher LSNs than
335 * the first QUOTAOFF log record does. If we happen to crash when
336 * the tail of the log has gone past the QUOTAOFF record, but
337 * before the last dquot modification, those dquots __will__
338 * recover, and that's not good.
339 *
340 * So, we have QUOTAOFF start and end logitems; the start
341 * logitem won't get overwritten until the end logitem appears...
342 */
cb6edc26
DC
343 error = xfs_qm_log_quotaoff_end(mp, qoffstart, flags);
344 if (error) {
345 /* We're screwed now. Shutdown is the only option. */
346 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
347 goto out_error;
348 }
1da177e4
LT
349
350 /*
351 * If quotas is completely disabled, close shop.
352 */
c8ad20ff
NS
353 if (((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET1) ||
354 ((flags & XFS_MOUNT_QUOTA_ALL) == XFS_MOUNT_QUOTA_SET2)) {
1da177e4
LT
355 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
356 xfs_qm_destroy_quotainfo(mp);
357 return (0);
358 }
359
360 /*
361 * Release our quotainode references, and vn_purge them,
362 * if we don't need them anymore.
363 */
364 if ((dqtype & XFS_QMOPT_UQUOTA) && XFS_QI_UQIP(mp)) {
365 XFS_PURGE_INODE(XFS_QI_UQIP(mp));
366 XFS_QI_UQIP(mp) = NULL;
367 }
c8ad20ff 368 if ((dqtype & (XFS_QMOPT_GQUOTA|XFS_QMOPT_PQUOTA)) && XFS_QI_GQIP(mp)) {
1da177e4
LT
369 XFS_PURGE_INODE(XFS_QI_GQIP(mp));
370 XFS_QI_GQIP(mp) = NULL;
371 }
cb6edc26 372out_error:
1da177e4
LT
373 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
374
375 return (error);
376}
377
378STATIC int
379xfs_qm_scall_trunc_qfiles(
380 xfs_mount_t *mp,
381 uint flags)
382{
383 int error;
384 xfs_inode_t *qip;
385
386 if (!capable(CAP_SYS_ADMIN))
387 return XFS_ERROR(EPERM);
388 error = 0;
62118709 389 if (!xfs_sb_version_hasquota(&mp->m_sb) || flags == 0) {
1da177e4
LT
390 qdprintk("qtrunc flags=%x m_qflags=%x\n", flags, mp->m_qflags);
391 return XFS_ERROR(EINVAL);
392 }
393
394 if ((flags & XFS_DQ_USER) && mp->m_sb.sb_uquotino != NULLFSINO) {
395 error = xfs_iget(mp, NULL, mp->m_sb.sb_uquotino, 0, 0, &qip, 0);
396 if (! error) {
397 (void) xfs_truncate_file(mp, qip);
43355099 398 IRELE(qip);
1da177e4
LT
399 }
400 }
401
c8ad20ff
NS
402 if ((flags & (XFS_DQ_GROUP|XFS_DQ_PROJ)) &&
403 mp->m_sb.sb_gquotino != NULLFSINO) {
1da177e4
LT
404 error = xfs_iget(mp, NULL, mp->m_sb.sb_gquotino, 0, 0, &qip, 0);
405 if (! error) {
406 (void) xfs_truncate_file(mp, qip);
43355099 407 IRELE(qip);
1da177e4
LT
408 }
409 }
410
411 return (error);
412}
413
414
415/*
416 * Switch on (a given) quota enforcement for a filesystem. This takes
417 * effect immediately.
418 * (Switching on quota accounting must be done at mount time.)
419 */
420STATIC int
421xfs_qm_scall_quotaon(
422 xfs_mount_t *mp,
423 uint flags)
424{
425 int error;
1da177e4
LT
426 uint qf;
427 uint accflags;
428 __int64_t sbflags;
429
430 if (!capable(CAP_SYS_ADMIN))
431 return XFS_ERROR(EPERM);
432
433 flags &= (XFS_ALL_QUOTA_ACCT | XFS_ALL_QUOTA_ENFD);
434 /*
435 * Switching on quota accounting must be done at mount time.
436 */
437 accflags = flags & XFS_ALL_QUOTA_ACCT;
438 flags &= ~(XFS_ALL_QUOTA_ACCT);
439
440 sbflags = 0;
441
442 if (flags == 0) {
443 qdprintk("quotaon: zero flags, m_qflags=%x\n", mp->m_qflags);
444 return XFS_ERROR(EINVAL);
445 }
446
447 /* No fs can turn on quotas with a delayed effect */
448 ASSERT((flags & XFS_ALL_QUOTA_ACCT) == 0);
449
450 /*
451 * Can't enforce without accounting. We check the superblock
452 * qflags here instead of m_qflags because rootfs can have
453 * quota acct on ondisk without m_qflags' knowing.
454 */
455 if (((flags & XFS_UQUOTA_ACCT) == 0 &&
456 (mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) == 0 &&
457 (flags & XFS_UQUOTA_ENFD))
458 ||
c8ad20ff
NS
459 ((flags & XFS_PQUOTA_ACCT) == 0 &&
460 (mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) == 0 &&
424ea91b 461 (flags & XFS_GQUOTA_ACCT) == 0 &&
1da177e4 462 (mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) == 0 &&
c8ad20ff 463 (flags & XFS_OQUOTA_ENFD))) {
1da177e4
LT
464 qdprintk("Can't enforce without acct, flags=%x sbflags=%x\n",
465 flags, mp->m_sb.sb_qflags);
466 return XFS_ERROR(EINVAL);
467 }
468 /*
469 * If everything's upto-date incore, then don't waste time.
470 */
471 if ((mp->m_qflags & flags) == flags)
472 return XFS_ERROR(EEXIST);
473
474 /*
475 * Change sb_qflags on disk but not incore mp->qflags
476 * if this is the root filesystem.
477 */
3685c2a1 478 spin_lock(&mp->m_sb_lock);
1da177e4
LT
479 qf = mp->m_sb.sb_qflags;
480 mp->m_sb.sb_qflags = qf | flags;
3685c2a1 481 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
482
483 /*
484 * There's nothing to change if it's the same.
485 */
486 if ((qf & flags) == flags && sbflags == 0)
487 return XFS_ERROR(EEXIST);
488 sbflags |= XFS_SB_QFLAGS;
489
490 if ((error = xfs_qm_write_sb_changes(mp, sbflags)))
491 return (error);
492 /*
493 * If we aren't trying to switch on quota enforcement, we are done.
494 */
495 if (((mp->m_sb.sb_qflags & XFS_UQUOTA_ACCT) !=
496 (mp->m_qflags & XFS_UQUOTA_ACCT)) ||
c8ad20ff
NS
497 ((mp->m_sb.sb_qflags & XFS_PQUOTA_ACCT) !=
498 (mp->m_qflags & XFS_PQUOTA_ACCT)) ||
499 ((mp->m_sb.sb_qflags & XFS_GQUOTA_ACCT) !=
500 (mp->m_qflags & XFS_GQUOTA_ACCT)) ||
1da177e4
LT
501 (flags & XFS_ALL_QUOTA_ENFD) == 0)
502 return (0);
503
504 if (! XFS_IS_QUOTA_RUNNING(mp))
505 return XFS_ERROR(ESRCH);
506
507 /*
508 * Switch on quota enforcement in core.
509 */
794ee1ba 510 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
1da177e4
LT
511 mp->m_qflags |= (flags & XFS_ALL_QUOTA_ENFD);
512 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
513
514 return (0);
515}
516
517
1da177e4
LT
518/*
519 * Return quota status information, such as uquota-off, enforcements, etc.
520 */
521STATIC int
522xfs_qm_scall_getqstat(
523 xfs_mount_t *mp,
524 fs_quota_stat_t *out)
525{
526 xfs_inode_t *uip, *gip;
527 boolean_t tempuqip, tempgqip;
528
529 uip = gip = NULL;
530 tempuqip = tempgqip = B_FALSE;
531 memset(out, 0, sizeof(fs_quota_stat_t));
532
533 out->qs_version = FS_QSTAT_VERSION;
62118709 534 if (!xfs_sb_version_hasquota(&mp->m_sb)) {
1da177e4
LT
535 out->qs_uquota.qfs_ino = NULLFSINO;
536 out->qs_gquota.qfs_ino = NULLFSINO;
537 return (0);
538 }
539 out->qs_flags = (__uint16_t) xfs_qm_export_flags(mp->m_qflags &
540 (XFS_ALL_QUOTA_ACCT|
541 XFS_ALL_QUOTA_ENFD));
542 out->qs_pad = 0;
543 out->qs_uquota.qfs_ino = mp->m_sb.sb_uquotino;
544 out->qs_gquota.qfs_ino = mp->m_sb.sb_gquotino;
545
546 if (mp->m_quotainfo) {
547 uip = mp->m_quotainfo->qi_uquotaip;
548 gip = mp->m_quotainfo->qi_gquotaip;
549 }
550 if (!uip && mp->m_sb.sb_uquotino != NULLFSINO) {
551 if (xfs_iget(mp, NULL, mp->m_sb.sb_uquotino,
552 0, 0, &uip, 0) == 0)
553 tempuqip = B_TRUE;
554 }
555 if (!gip && mp->m_sb.sb_gquotino != NULLFSINO) {
556 if (xfs_iget(mp, NULL, mp->m_sb.sb_gquotino,
557 0, 0, &gip, 0) == 0)
558 tempgqip = B_TRUE;
559 }
560 if (uip) {
561 out->qs_uquota.qfs_nblks = uip->i_d.di_nblocks;
562 out->qs_uquota.qfs_nextents = uip->i_d.di_nextents;
563 if (tempuqip)
43355099 564 IRELE(uip);
1da177e4
LT
565 }
566 if (gip) {
567 out->qs_gquota.qfs_nblks = gip->i_d.di_nblocks;
568 out->qs_gquota.qfs_nextents = gip->i_d.di_nextents;
569 if (tempgqip)
43355099 570 IRELE(gip);
1da177e4
LT
571 }
572 if (mp->m_quotainfo) {
573 out->qs_incoredqs = XFS_QI_MPLNDQUOTS(mp);
574 out->qs_btimelimit = XFS_QI_BTIMELIMIT(mp);
575 out->qs_itimelimit = XFS_QI_ITIMELIMIT(mp);
576 out->qs_rtbtimelimit = XFS_QI_RTBTIMELIMIT(mp);
577 out->qs_bwarnlimit = XFS_QI_BWARNLIMIT(mp);
578 out->qs_iwarnlimit = XFS_QI_IWARNLIMIT(mp);
579 }
580 return (0);
581}
582
583/*
584 * Adjust quota limits, and start/stop timers accordingly.
585 */
586STATIC int
587xfs_qm_scall_setqlim(
588 xfs_mount_t *mp,
589 xfs_dqid_t id,
590 uint type,
591 fs_disk_quota_t *newlim)
592{
593 xfs_disk_dquot_t *ddq;
594 xfs_dquot_t *dqp;
595 xfs_trans_t *tp;
596 int error;
597 xfs_qcnt_t hard, soft;
598
599 if (!capable(CAP_SYS_ADMIN))
600 return XFS_ERROR(EPERM);
601
754002b4
NS
602 if ((newlim->d_fieldmask &
603 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
1da177e4
LT
604 return (0);
605
606 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
607 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
608 0, 0, XFS_DEFAULT_LOG_COUNT))) {
609 xfs_trans_cancel(tp, 0);
610 return (error);
611 }
612
613 /*
614 * We don't want to race with a quotaoff so take the quotaoff lock.
615 * (We don't hold an inode lock, so there's nothing else to stop
616 * a quotaoff from happening). (XXXThis doesn't currently happen
617 * because we take the vfslock before calling xfs_qm_sysent).
618 */
794ee1ba 619 mutex_lock(&(XFS_QI_QOFFLOCK(mp)));
1da177e4
LT
620
621 /*
622 * Get the dquot (locked), and join it to the transaction.
623 * Allocate the dquot if this doesn't exist.
624 */
625 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
626 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
627 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
628 ASSERT(error != ENOENT);
629 return (error);
630 }
631 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
632 xfs_trans_dqjoin(tp, dqp);
633 ddq = &dqp->q_core;
634
635 /*
636 * Make sure that hardlimits are >= soft limits before changing.
637 */
638 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
639 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
1149d96a 640 be64_to_cpu(ddq->d_blk_hardlimit);
1da177e4
LT
641 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
642 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
1149d96a 643 be64_to_cpu(ddq->d_blk_softlimit);
1da177e4 644 if (hard == 0 || hard >= soft) {
1149d96a
CH
645 ddq->d_blk_hardlimit = cpu_to_be64(hard);
646 ddq->d_blk_softlimit = cpu_to_be64(soft);
1da177e4
LT
647 if (id == 0) {
648 mp->m_quotainfo->qi_bhardlimit = hard;
649 mp->m_quotainfo->qi_bsoftlimit = soft;
650 }
651 } else {
652 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
653 }
654 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
655 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
1149d96a 656 be64_to_cpu(ddq->d_rtb_hardlimit);
1da177e4
LT
657 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
658 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
1149d96a 659 be64_to_cpu(ddq->d_rtb_softlimit);
1da177e4 660 if (hard == 0 || hard >= soft) {
1149d96a
CH
661 ddq->d_rtb_hardlimit = cpu_to_be64(hard);
662 ddq->d_rtb_softlimit = cpu_to_be64(soft);
1da177e4
LT
663 if (id == 0) {
664 mp->m_quotainfo->qi_rtbhardlimit = hard;
665 mp->m_quotainfo->qi_rtbsoftlimit = soft;
666 }
667 } else {
668 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
669 }
670
671 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
672 (xfs_qcnt_t) newlim->d_ino_hardlimit :
1149d96a 673 be64_to_cpu(ddq->d_ino_hardlimit);
1da177e4
LT
674 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
675 (xfs_qcnt_t) newlim->d_ino_softlimit :
1149d96a 676 be64_to_cpu(ddq->d_ino_softlimit);
1da177e4 677 if (hard == 0 || hard >= soft) {
1149d96a
CH
678 ddq->d_ino_hardlimit = cpu_to_be64(hard);
679 ddq->d_ino_softlimit = cpu_to_be64(soft);
1da177e4
LT
680 if (id == 0) {
681 mp->m_quotainfo->qi_ihardlimit = hard;
682 mp->m_quotainfo->qi_isoftlimit = soft;
683 }
684 } else {
685 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
686 }
687
754002b4
NS
688 /*
689 * Update warnings counter(s) if requested
690 */
691 if (newlim->d_fieldmask & FS_DQ_BWARNS)
1149d96a 692 ddq->d_bwarns = cpu_to_be16(newlim->d_bwarns);
754002b4 693 if (newlim->d_fieldmask & FS_DQ_IWARNS)
1149d96a 694 ddq->d_iwarns = cpu_to_be16(newlim->d_iwarns);
754002b4 695 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
1149d96a 696 ddq->d_rtbwarns = cpu_to_be16(newlim->d_rtbwarns);
754002b4 697
1da177e4
LT
698 if (id == 0) {
699 /*
700 * Timelimits for the super user set the relative time
701 * the other users can be over quota for this file system.
702 * If it is zero a default is used. Ditto for the default
754002b4
NS
703 * soft and hard limit values (already done, above), and
704 * for warnings.
1da177e4
LT
705 */
706 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
707 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
1149d96a 708 ddq->d_btimer = cpu_to_be32(newlim->d_btimer);
1da177e4
LT
709 }
710 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
711 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
1149d96a 712 ddq->d_itimer = cpu_to_be32(newlim->d_itimer);
1da177e4
LT
713 }
714 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
715 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
1149d96a 716 ddq->d_rtbtimer = cpu_to_be32(newlim->d_rtbtimer);
1da177e4 717 }
754002b4
NS
718 if (newlim->d_fieldmask & FS_DQ_BWARNS)
719 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
720 if (newlim->d_fieldmask & FS_DQ_IWARNS)
721 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
722 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
723 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
724 } else {
1da177e4
LT
725 /*
726 * If the user is now over quota, start the timelimit.
727 * The user will not be 'warned'.
728 * Note that we keep the timers ticking, whether enforcement
729 * is on or off. We don't really want to bother with iterating
730 * over all ondisk dquots and turning the timers on/off.
731 */
732 xfs_qm_adjust_dqtimers(mp, ddq);
733 }
734 dqp->dq_flags |= XFS_DQ_DIRTY;
735 xfs_trans_log_dquot(tp, dqp);
736
737 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
1c72bf90 738 xfs_trans_commit(tp, 0);
1da177e4
LT
739 xfs_qm_dqprint(dqp);
740 xfs_qm_dqrele(dqp);
741 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
742
743 return (0);
744}
745
746STATIC int
747xfs_qm_scall_getquota(
748 xfs_mount_t *mp,
749 xfs_dqid_t id,
750 uint type,
751 fs_disk_quota_t *out)
752{
753 xfs_dquot_t *dqp;
754 int error;
755
756 /*
757 * Try to get the dquot. We don't want it allocated on disk, so
758 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
759 * exist, we'll get ENOENT back.
760 */
761 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
762 return (error);
763 }
764
765 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
766 /*
767 * If everything's NULL, this dquot doesn't quite exist as far as
768 * our utility programs are concerned.
769 */
770 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
771 xfs_qm_dqput(dqp);
772 return XFS_ERROR(ENOENT);
773 }
774 /* xfs_qm_dqprint(dqp); */
775 /*
776 * Convert the disk dquot to the exportable format
777 */
778 xfs_qm_export_dquot(mp, &dqp->q_core, out);
779 xfs_qm_dqput(dqp);
780 return (error ? XFS_ERROR(EFAULT) : 0);
781}
782
783
784STATIC int
785xfs_qm_log_quotaoff_end(
786 xfs_mount_t *mp,
787 xfs_qoff_logitem_t *startqoff,
788 uint flags)
789{
c8ad20ff 790 xfs_trans_t *tp;
1da177e4 791 int error;
c8ad20ff 792 xfs_qoff_logitem_t *qoffi;
1da177e4
LT
793
794 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
795
796 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
797 0, 0, XFS_DEFAULT_LOG_COUNT))) {
798 xfs_trans_cancel(tp, 0);
799 return (error);
800 }
801
802 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
803 flags & XFS_ALL_QUOTA_ACCT);
804 xfs_trans_log_quotaoff_item(tp, qoffi);
805
806 /*
807 * We have to make sure that the transaction is secure on disk before we
808 * return and actually stop quota accounting. So, make it synchronous.
809 * We don't care about quotoff's performance.
810 */
811 xfs_trans_set_sync(tp);
1c72bf90 812 error = xfs_trans_commit(tp, 0);
1da177e4
LT
813 return (error);
814}
815
816
817STATIC int
818xfs_qm_log_quotaoff(
819 xfs_mount_t *mp,
820 xfs_qoff_logitem_t **qoffstartp,
821 uint flags)
822{
823 xfs_trans_t *tp;
824 int error;
1da177e4
LT
825 xfs_qoff_logitem_t *qoffi=NULL;
826 uint oldsbqflag=0;
827
828 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
829 if ((error = xfs_trans_reserve(tp, 0,
830 sizeof(xfs_qoff_logitem_t) * 2 +
831 mp->m_sb.sb_sectsize + 128,
832 0,
833 0,
834 XFS_DEFAULT_LOG_COUNT))) {
835 goto error0;
836 }
837
838 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
839 xfs_trans_log_quotaoff_item(tp, qoffi);
840
3685c2a1 841 spin_lock(&mp->m_sb_lock);
1da177e4
LT
842 oldsbqflag = mp->m_sb.sb_qflags;
843 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
3685c2a1 844 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
845
846 xfs_mod_sb(tp, XFS_SB_QFLAGS);
847
848 /*
849 * We have to make sure that the transaction is secure on disk before we
850 * return and actually stop quota accounting. So, make it synchronous.
851 * We don't care about quotoff's performance.
852 */
853 xfs_trans_set_sync(tp);
1c72bf90 854 error = xfs_trans_commit(tp, 0);
1da177e4
LT
855
856error0:
857 if (error) {
858 xfs_trans_cancel(tp, 0);
859 /*
860 * No one else is modifying sb_qflags, so this is OK.
861 * We still hold the quotaofflock.
862 */
3685c2a1 863 spin_lock(&mp->m_sb_lock);
1da177e4 864 mp->m_sb.sb_qflags = oldsbqflag;
3685c2a1 865 spin_unlock(&mp->m_sb_lock);
1da177e4
LT
866 }
867 *qoffstartp = qoffi;
868 return (error);
869}
870
871
872/*
873 * Translate an internal style on-disk-dquot to the exportable format.
874 * The main differences are that the counters/limits are all in Basic
875 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
876 * to be converted to the native endianness.
877 */
878STATIC void
879xfs_qm_export_dquot(
880 xfs_mount_t *mp,
881 xfs_disk_dquot_t *src,
882 struct fs_disk_quota *dst)
883{
884 memset(dst, 0, sizeof(*dst));
885 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
1149d96a
CH
886 dst->d_flags = xfs_qm_export_qtype_flags(src->d_flags);
887 dst->d_id = be32_to_cpu(src->d_id);
888 dst->d_blk_hardlimit =
889 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_hardlimit));
890 dst->d_blk_softlimit =
891 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_blk_softlimit));
892 dst->d_ino_hardlimit = be64_to_cpu(src->d_ino_hardlimit);
893 dst->d_ino_softlimit = be64_to_cpu(src->d_ino_softlimit);
894 dst->d_bcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_bcount));
895 dst->d_icount = be64_to_cpu(src->d_icount);
896 dst->d_btimer = be32_to_cpu(src->d_btimer);
897 dst->d_itimer = be32_to_cpu(src->d_itimer);
898 dst->d_iwarns = be16_to_cpu(src->d_iwarns);
899 dst->d_bwarns = be16_to_cpu(src->d_bwarns);
900 dst->d_rtb_hardlimit =
901 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_hardlimit));
902 dst->d_rtb_softlimit =
903 XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtb_softlimit));
904 dst->d_rtbcount = XFS_FSB_TO_BB(mp, be64_to_cpu(src->d_rtbcount));
905 dst->d_rtbtimer = be32_to_cpu(src->d_rtbtimer);
906 dst->d_rtbwarns = be16_to_cpu(src->d_rtbwarns);
1da177e4
LT
907
908 /*
909 * Internally, we don't reset all the timers when quota enforcement
c41564b5 910 * gets turned off. No need to confuse the user level code,
1da177e4
LT
911 * so return zeroes in that case.
912 */
e6d29426
KO
913 if ((!XFS_IS_UQUOTA_ENFORCED(mp) && src->d_flags == XFS_DQ_USER) ||
914 (!XFS_IS_OQUOTA_ENFORCED(mp) &&
915 (src->d_flags & (XFS_DQ_PROJ | XFS_DQ_GROUP)))) {
1da177e4
LT
916 dst->d_btimer = 0;
917 dst->d_itimer = 0;
918 dst->d_rtbtimer = 0;
919 }
920
921#ifdef DEBUG
e6d29426
KO
922 if (((XFS_IS_UQUOTA_ENFORCED(mp) && dst->d_flags == XFS_USER_QUOTA) ||
923 (XFS_IS_OQUOTA_ENFORCED(mp) &&
924 (dst->d_flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)))) &&
925 dst->d_id != 0) {
1da177e4
LT
926 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
927 (dst->d_blk_softlimit > 0)) {
928 ASSERT(dst->d_btimer != 0);
929 }
930 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
931 (dst->d_ino_softlimit > 0)) {
932 ASSERT(dst->d_itimer != 0);
933 }
934 }
935#endif
936}
937
938STATIC uint
939xfs_qm_import_qtype_flags(
c8ad20ff 940 uint uflags)
1da177e4 941{
c8ad20ff
NS
942 uint oflags = 0;
943
1da177e4 944 /*
c8ad20ff 945 * Can't be more than one, or none.
1da177e4
LT
946 */
947 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
c8ad20ff
NS
948 (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
949 ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
950 (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
951 ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
952 (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
953 ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
1da177e4
LT
954 return (0);
955
c8ad20ff
NS
956 oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
957 oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
958 oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
959 return oflags;
1da177e4
LT
960}
961
962STATIC uint
963xfs_qm_export_qtype_flags(
964 uint flags)
965{
966 /*
c8ad20ff 967 * Can't be more than one, or none.
1da177e4 968 */
c8ad20ff
NS
969 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
970 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
971 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
972 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
973 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
974 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
975 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
1da177e4
LT
976
977 return (flags & XFS_DQ_USER) ?
c8ad20ff
NS
978 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
979 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
1da177e4
LT
980}
981
982STATIC uint
983xfs_qm_import_flags(
984 uint uflags)
985{
986 uint flags = 0;
987
988 if (uflags & XFS_QUOTA_UDQ_ACCT)
989 flags |= XFS_UQUOTA_ACCT;
c8ad20ff
NS
990 if (uflags & XFS_QUOTA_PDQ_ACCT)
991 flags |= XFS_PQUOTA_ACCT;
1da177e4
LT
992 if (uflags & XFS_QUOTA_GDQ_ACCT)
993 flags |= XFS_GQUOTA_ACCT;
994 if (uflags & XFS_QUOTA_UDQ_ENFD)
995 flags |= XFS_UQUOTA_ENFD;
c8ad20ff
NS
996 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
997 flags |= XFS_OQUOTA_ENFD;
1da177e4
LT
998 return (flags);
999}
1000
1001
1002STATIC uint
1003xfs_qm_export_flags(
1004 uint flags)
1005{
1006 uint uflags;
1007
1008 uflags = 0;
1009 if (flags & XFS_UQUOTA_ACCT)
1010 uflags |= XFS_QUOTA_UDQ_ACCT;
c8ad20ff
NS
1011 if (flags & XFS_PQUOTA_ACCT)
1012 uflags |= XFS_QUOTA_PDQ_ACCT;
1da177e4
LT
1013 if (flags & XFS_GQUOTA_ACCT)
1014 uflags |= XFS_QUOTA_GDQ_ACCT;
1015 if (flags & XFS_UQUOTA_ENFD)
1016 uflags |= XFS_QUOTA_UDQ_ENFD;
c8ad20ff
NS
1017 if (flags & (XFS_OQUOTA_ENFD)) {
1018 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1019 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1020 }
1da177e4
LT
1021 return (uflags);
1022}
1023
1024
1025/*
1026 * Go thru all the inodes in the file system, releasing their dquots.
1027 * Note that the mount structure gets modified to indicate that quotas are off
1028 * AFTER this, in the case of quotaoff. This also gets called from
1029 * xfs_rootumount.
1030 */
1031void
1032xfs_qm_dqrele_all_inodes(
1033 struct xfs_mount *mp,
1034 uint flags)
1035{
1da177e4
LT
1036 xfs_inode_t *ip, *topino;
1037 uint ireclaims;
67fcaa73 1038 bhv_vnode_t *vp;
1da177e4
LT
1039 boolean_t vnode_refd;
1040
1041 ASSERT(mp->m_quotainfo);
1042
1da177e4 1043 XFS_MOUNT_ILOCK(mp);
cdb62687 1044again:
1da177e4
LT
1045 ip = mp->m_inodes;
1046 if (ip == NULL) {
1047 XFS_MOUNT_IUNLOCK(mp);
1048 return;
1049 }
1050 do {
1051 /* Skip markers inserted by xfs_sync */
1052 if (ip->i_mount == NULL) {
1053 ip = ip->i_mnext;
1054 continue;
1055 }
1056 /* Root inode, rbmip and rsumip have associated blocks */
1057 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
1058 ASSERT(ip->i_udquot == NULL);
1059 ASSERT(ip->i_gdquot == NULL);
1060 ip = ip->i_mnext;
1061 continue;
1062 }
1063 vp = XFS_ITOV_NULL(ip);
1064 if (!vp) {
1065 ASSERT(ip->i_udquot == NULL);
1066 ASSERT(ip->i_gdquot == NULL);
1067 ip = ip->i_mnext;
1068 continue;
1069 }
1070 vnode_refd = B_FALSE;
1071 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1da177e4
LT
1072 ireclaims = mp->m_ireclaims;
1073 topino = mp->m_inodes;
cdb62687
CH
1074 vp = vn_grab(vp);
1075 if (!vp)
1076 goto again;
1da177e4 1077
cdb62687 1078 XFS_MOUNT_IUNLOCK(mp);
1da177e4 1079 /* XXX restart limit ? */
1da177e4
LT
1080 xfs_ilock(ip, XFS_ILOCK_EXCL);
1081 vnode_refd = B_TRUE;
1082 } else {
1083 ireclaims = mp->m_ireclaims;
1084 topino = mp->m_inodes;
1085 XFS_MOUNT_IUNLOCK(mp);
1086 }
1087
1088 /*
1089 * We don't keep the mountlock across the dqrele() call,
1090 * since it can take a while..
1091 */
1092 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1093 xfs_qm_dqrele(ip->i_udquot);
1094 ip->i_udquot = NULL;
1095 }
c8ad20ff 1096 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
1da177e4
LT
1097 xfs_qm_dqrele(ip->i_gdquot);
1098 ip->i_gdquot = NULL;
1099 }
1100 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1101 /*
1102 * Wait until we've dropped the ilock and mountlock to
1103 * do the vn_rele. Or be condemned to an eternity in the
1104 * inactive code in hell.
1105 */
1106 if (vnode_refd)
43355099 1107 IRELE(ip);
1da177e4
LT
1108 XFS_MOUNT_ILOCK(mp);
1109 /*
1110 * If an inode was inserted or removed, we gotta
1111 * start over again.
1112 */
1113 if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
1114 /* XXX use a sentinel */
1da177e4
LT
1115 goto again;
1116 }
1117 ip = ip->i_mnext;
1118 } while (ip != mp->m_inodes);
1119
1120 XFS_MOUNT_IUNLOCK(mp);
1121}
1122
1123/*------------------------------------------------------------------------*/
1124#ifdef DEBUG
1125/*
1126 * This contains all the test functions for XFS disk quotas.
1127 * Currently it does a quota accounting check. ie. it walks through
1128 * all inodes in the file system, calculating the dquot accounting fields,
1129 * and prints out any inconsistencies.
1130 */
1131xfs_dqhash_t *qmtest_udqtab;
1132xfs_dqhash_t *qmtest_gdqtab;
1133int qmtest_hashmask;
1134int qmtest_nfails;
1135mutex_t qcheck_lock;
1136
1137#define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1138 (__psunsigned_t)(id)) & \
1139 (qmtest_hashmask - 1))
1140
1141#define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
1142 (qmtest_udqtab + \
1143 DQTEST_HASHVAL(mp, id)) : \
1144 (qmtest_gdqtab + \
1145 DQTEST_HASHVAL(mp, id)))
1146
1147#define DQTEST_LIST_PRINT(l, NXT, title) \
1148{ \
1149 xfs_dqtest_t *dqp; int i = 0;\
1150 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
1151 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
1152 dqp = (xfs_dqtest_t *)dqp->NXT) { \
1153 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
1154 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
1155 dqp->d_bcount, dqp->d_icount); } \
1156}
1157
1158typedef struct dqtest {
1159 xfs_dqmarker_t q_lists;
1160 xfs_dqhash_t *q_hash; /* the hashchain header */
1161 xfs_mount_t *q_mount; /* filesystem this relates to */
1162 xfs_dqid_t d_id; /* user id or group id */
1163 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
1164 xfs_qcnt_t d_icount; /* # inodes owned by the user */
1165} xfs_dqtest_t;
1166
1167STATIC void
1168xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
1169{
1170 xfs_dquot_t *d;
1171 if (((d) = (h)->qh_next))
1172 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1173 (dqp)->HL_NEXT = d;
1174 (dqp)->HL_PREVP = &((h)->qh_next);
1175 (h)->qh_next = (xfs_dquot_t *)dqp;
1176 (h)->qh_version++;
1177 (h)->qh_nelems++;
1178}
1179STATIC void
1180xfs_qm_dqtest_print(
1181 xfs_dqtest_t *d)
1182{
1183 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1184 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
1da177e4
LT
1185 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1186 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1187 d->d_bcount, (int)d->d_bcount);
1188 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1189 d->d_icount, (int)d->d_icount);
1190 cmn_err(CE_DEBUG, "---------------------------");
1191}
1192
1193STATIC void
1194xfs_qm_dqtest_failed(
1195 xfs_dqtest_t *d,
1196 xfs_dquot_t *dqp,
1197 char *reason,
1198 xfs_qcnt_t a,
1199 xfs_qcnt_t b,
1200 int error)
1201{
1202 qmtest_nfails++;
1203 if (error)
1204 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
1149d96a 1205 d->d_id, error, reason);
1da177e4
LT
1206 else
1207 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
1149d96a 1208 d->d_id, reason, (int)a, (int)b);
1da177e4
LT
1209 xfs_qm_dqtest_print(d);
1210 if (dqp)
1211 xfs_qm_dqprint(dqp);
1212}
1213
1214STATIC int
1215xfs_dqtest_cmp2(
1216 xfs_dqtest_t *d,
1217 xfs_dquot_t *dqp)
1218{
1219 int err = 0;
1149d96a 1220 if (be64_to_cpu(dqp->q_core.d_icount) != d->d_icount) {
1da177e4 1221 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
1149d96a 1222 be64_to_cpu(dqp->q_core.d_icount),
1da177e4
LT
1223 d->d_icount, 0);
1224 err++;
1225 }
1149d96a 1226 if (be64_to_cpu(dqp->q_core.d_bcount) != d->d_bcount) {
1da177e4 1227 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1149d96a 1228 be64_to_cpu(dqp->q_core.d_bcount),
1da177e4
LT
1229 d->d_bcount, 0);
1230 err++;
1231 }
1149d96a
CH
1232 if (dqp->q_core.d_blk_softlimit &&
1233 be64_to_cpu(dqp->q_core.d_bcount) >=
1234 be64_to_cpu(dqp->q_core.d_blk_softlimit)) {
1da177e4
LT
1235 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1236 cmn_err(CE_DEBUG,
1237 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1238 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1239 err++;
1240 }
1241 }
1149d96a
CH
1242 if (dqp->q_core.d_ino_softlimit &&
1243 be64_to_cpu(dqp->q_core.d_icount) >=
1244 be64_to_cpu(dqp->q_core.d_ino_softlimit)) {
1da177e4
LT
1245 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1246 cmn_err(CE_DEBUG,
1247 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1248 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1249 err++;
1250 }
1251 }
1252#ifdef QUOTADEBUG
1253 if (!err) {
1254 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
c8ad20ff 1255 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1da177e4
LT
1256 }
1257#endif
1258 return (err);
1259}
1260
1261STATIC void
1262xfs_dqtest_cmp(
1263 xfs_dqtest_t *d)
1264{
1265 xfs_dquot_t *dqp;
1266 int error;
1267
1268 /* xfs_qm_dqtest_print(d); */
1269 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1270 &dqp))) {
1271 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1272 return;
1273 }
1274 xfs_dqtest_cmp2(d, dqp);
1275 xfs_qm_dqput(dqp);
1276}
1277
1278STATIC int
1279xfs_qm_internalqcheck_dqget(
1280 xfs_mount_t *mp,
1281 xfs_dqid_t id,
1282 uint type,
1283 xfs_dqtest_t **O_dq)
1284{
1285 xfs_dqtest_t *d;
1286 xfs_dqhash_t *h;
1287
1288 h = DQTEST_HASH(mp, id, type);
1289 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1290 d = (xfs_dqtest_t *) d->HL_NEXT) {
1291 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1292 if (d->d_id == id && mp == d->q_mount) {
1293 *O_dq = d;
1294 return (0);
1295 }
1296 }
1297 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1298 d->dq_flags = type;
1299 d->d_id = id;
1300 d->q_mount = mp;
1301 d->q_hash = h;
1302 xfs_qm_hashinsert(h, d);
1303 *O_dq = d;
1304 return (0);
1305}
1306
1307STATIC void
1308xfs_qm_internalqcheck_get_dquots(
1309 xfs_mount_t *mp,
1310 xfs_dqid_t uid,
c8ad20ff 1311 xfs_dqid_t projid,
1da177e4
LT
1312 xfs_dqid_t gid,
1313 xfs_dqtest_t **ud,
1314 xfs_dqtest_t **gd)
1315{
1316 if (XFS_IS_UQUOTA_ON(mp))
1317 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1318 if (XFS_IS_GQUOTA_ON(mp))
1319 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
c8ad20ff
NS
1320 else if (XFS_IS_PQUOTA_ON(mp))
1321 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
1da177e4
LT
1322}
1323
1324
1325STATIC void
1326xfs_qm_internalqcheck_dqadjust(
1327 xfs_inode_t *ip,
1328 xfs_dqtest_t *d)
1329{
1330 d->d_icount++;
1331 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1332}
1333
1334STATIC int
1335xfs_qm_internalqcheck_adjust(
1336 xfs_mount_t *mp, /* mount point for filesystem */
1337 xfs_ino_t ino, /* inode number to get data for */
1338 void __user *buffer, /* not used */
1339 int ubsize, /* not used */
1340 void *private_data, /* not used */
1341 xfs_daddr_t bno, /* starting block of inode cluster */
1342 int *ubused, /* not used */
1343 void *dip, /* not used */
1344 int *res) /* bulkstat result code */
1345{
1346 xfs_inode_t *ip;
1347 xfs_dqtest_t *ud, *gd;
1348 uint lock_flags;
1349 boolean_t ipreleased;
1350 int error;
1351
1352 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1353
1354 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1355 *res = BULKSTAT_RV_NOTHING;
1356 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1357 (unsigned long long) ino,
1358 (unsigned long long) mp->m_sb.sb_uquotino,
1359 (unsigned long long) mp->m_sb.sb_gquotino);
1360 return XFS_ERROR(EINVAL);
1361 }
1362 ipreleased = B_FALSE;
1363 again:
1364 lock_flags = XFS_ILOCK_SHARED;
1365 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1366 *res = BULKSTAT_RV_NOTHING;
1367 return (error);
1368 }
1369
1370 if (ip->i_d.di_mode == 0) {
1371 xfs_iput_new(ip, lock_flags);
1372 *res = BULKSTAT_RV_NOTHING;
1373 return XFS_ERROR(ENOENT);
1374 }
1375
1376 /*
1377 * This inode can have blocks after eof which can get released
1378 * when we send it to inactive. Since we don't check the dquot
1379 * until the after all our calculations are done, we must get rid
1380 * of those now.
1381 */
1382 if (! ipreleased) {
1383 xfs_iput(ip, lock_flags);
1384 ipreleased = B_TRUE;
1385 goto again;
1386 }
1387 xfs_qm_internalqcheck_get_dquots(mp,
1388 (xfs_dqid_t) ip->i_d.di_uid,
c8ad20ff 1389 (xfs_dqid_t) ip->i_d.di_projid,
1da177e4
LT
1390 (xfs_dqid_t) ip->i_d.di_gid,
1391 &ud, &gd);
1392 if (XFS_IS_UQUOTA_ON(mp)) {
1393 ASSERT(ud);
1394 xfs_qm_internalqcheck_dqadjust(ip, ud);
1395 }
c8ad20ff 1396 if (XFS_IS_OQUOTA_ON(mp)) {
1da177e4
LT
1397 ASSERT(gd);
1398 xfs_qm_internalqcheck_dqadjust(ip, gd);
1399 }
1400 xfs_iput(ip, lock_flags);
1401 *res = BULKSTAT_RV_DIDONE;
1402 return (0);
1403}
1404
1405
1406/* PRIVATE, debugging */
1407int
1408xfs_qm_internalqcheck(
1409 xfs_mount_t *mp)
1410{
1411 xfs_ino_t lastino;
1412 int done, count;
1413 int i;
1414 xfs_dqtest_t *d, *e;
1415 xfs_dqhash_t *h1;
1416 int error;
1417
1418 lastino = 0;
1419 qmtest_hashmask = 32;
1420 count = 5;
1421 done = 0;
1422 qmtest_nfails = 0;
1423
1424 if (! XFS_IS_QUOTA_ON(mp))
1425 return XFS_ERROR(ESRCH);
1426
1427 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1428 XFS_bflush(mp->m_ddev_targp);
1429 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1430 XFS_bflush(mp->m_ddev_targp);
1431
794ee1ba 1432 mutex_lock(&qcheck_lock);
1da177e4
LT
1433 /* There should be absolutely no quota activity while this
1434 is going on. */
1435 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1436 sizeof(xfs_dqhash_t), KM_SLEEP);
1437 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1438 sizeof(xfs_dqhash_t), KM_SLEEP);
1439 do {
1440 /*
1441 * Iterate thru all the inodes in the file system,
1442 * adjusting the corresponding dquot counters
1443 */
1444 if ((error = xfs_bulkstat(mp, &lastino, &count,
1445 xfs_qm_internalqcheck_adjust, NULL,
1446 0, NULL, BULKSTAT_FG_IGET, &done))) {
1447 break;
1448 }
1449 } while (! done);
1450 if (error) {
1451 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1452 }
1453 cmn_err(CE_DEBUG, "Checking results against system dquots");
1454 for (i = 0; i < qmtest_hashmask; i++) {
1455 h1 = &qmtest_udqtab[i];
1456 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1457 xfs_dqtest_cmp(d);
1458 e = (xfs_dqtest_t *) d->HL_NEXT;
1459 kmem_free(d, sizeof(xfs_dqtest_t));
1460 d = e;
1461 }
1462 h1 = &qmtest_gdqtab[i];
1463 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1464 xfs_dqtest_cmp(d);
1465 e = (xfs_dqtest_t *) d->HL_NEXT;
1466 kmem_free(d, sizeof(xfs_dqtest_t));
1467 d = e;
1468 }
1469 }
1470
1471 if (qmtest_nfails) {
1472 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1473 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1474 } else {
1475 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1476 }
1477 kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1478 kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1479 mutex_unlock(&qcheck_lock);
1480 return (qmtest_nfails);
1481}
1482
1483#endif /* DEBUG */