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