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