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