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