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