blob: 68e98962dbefe0db3f303204c97c53b82b6da975 [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
Nathan Scott754002b2005-06-21 15:49:06 +1000620 if ((newlim->d_fieldmask &
621 (FS_DQ_LIMIT_MASK|FS_DQ_TIMER_MASK|FS_DQ_WARNS_MASK)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return (0);
623
624 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SETQLIM);
625 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_disk_dquot_t) + 128,
626 0, 0, XFS_DEFAULT_LOG_COUNT))) {
627 xfs_trans_cancel(tp, 0);
628 return (error);
629 }
630
631 /*
632 * We don't want to race with a quotaoff so take the quotaoff lock.
633 * (We don't hold an inode lock, so there's nothing else to stop
634 * a quotaoff from happening). (XXXThis doesn't currently happen
635 * because we take the vfslock before calling xfs_qm_sysent).
636 */
637 mutex_lock(&(XFS_QI_QOFFLOCK(mp)), PINOD);
638
639 /*
640 * Get the dquot (locked), and join it to the transaction.
641 * Allocate the dquot if this doesn't exist.
642 */
643 if ((error = xfs_qm_dqget(mp, NULL, id, type, XFS_QMOPT_DQALLOC, &dqp))) {
644 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
645 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
646 ASSERT(error != ENOENT);
647 return (error);
648 }
649 xfs_dqtrace_entry(dqp, "Q_SETQLIM: AFT DQGET");
650 xfs_trans_dqjoin(tp, dqp);
651 ddq = &dqp->q_core;
652
653 /*
654 * Make sure that hardlimits are >= soft limits before changing.
655 */
656 hard = (newlim->d_fieldmask & FS_DQ_BHARD) ?
657 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_hardlimit) :
658 INT_GET(ddq->d_blk_hardlimit, ARCH_CONVERT);
659 soft = (newlim->d_fieldmask & FS_DQ_BSOFT) ?
660 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_blk_softlimit) :
661 INT_GET(ddq->d_blk_softlimit, ARCH_CONVERT);
662 if (hard == 0 || hard >= soft) {
663 INT_SET(ddq->d_blk_hardlimit, ARCH_CONVERT, hard);
664 INT_SET(ddq->d_blk_softlimit, ARCH_CONVERT, soft);
665 if (id == 0) {
666 mp->m_quotainfo->qi_bhardlimit = hard;
667 mp->m_quotainfo->qi_bsoftlimit = soft;
668 }
669 } else {
670 qdprintk("blkhard %Ld < blksoft %Ld\n", hard, soft);
671 }
672 hard = (newlim->d_fieldmask & FS_DQ_RTBHARD) ?
673 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_hardlimit) :
674 INT_GET(ddq->d_rtb_hardlimit, ARCH_CONVERT);
675 soft = (newlim->d_fieldmask & FS_DQ_RTBSOFT) ?
676 (xfs_qcnt_t) XFS_BB_TO_FSB(mp, newlim->d_rtb_softlimit) :
677 INT_GET(ddq->d_rtb_softlimit, ARCH_CONVERT);
678 if (hard == 0 || hard >= soft) {
679 INT_SET(ddq->d_rtb_hardlimit, ARCH_CONVERT, hard);
680 INT_SET(ddq->d_rtb_softlimit, ARCH_CONVERT, soft);
681 if (id == 0) {
682 mp->m_quotainfo->qi_rtbhardlimit = hard;
683 mp->m_quotainfo->qi_rtbsoftlimit = soft;
684 }
685 } else {
686 qdprintk("rtbhard %Ld < rtbsoft %Ld\n", hard, soft);
687 }
688
689 hard = (newlim->d_fieldmask & FS_DQ_IHARD) ?
690 (xfs_qcnt_t) newlim->d_ino_hardlimit :
691 INT_GET(ddq->d_ino_hardlimit, ARCH_CONVERT);
692 soft = (newlim->d_fieldmask & FS_DQ_ISOFT) ?
693 (xfs_qcnt_t) newlim->d_ino_softlimit :
694 INT_GET(ddq->d_ino_softlimit, ARCH_CONVERT);
695 if (hard == 0 || hard >= soft) {
696 INT_SET(ddq->d_ino_hardlimit, ARCH_CONVERT, hard);
697 INT_SET(ddq->d_ino_softlimit, ARCH_CONVERT, soft);
698 if (id == 0) {
699 mp->m_quotainfo->qi_ihardlimit = hard;
700 mp->m_quotainfo->qi_isoftlimit = soft;
701 }
702 } else {
703 qdprintk("ihard %Ld < isoft %Ld\n", hard, soft);
704 }
705
Nathan Scott754002b2005-06-21 15:49:06 +1000706 /*
707 * Update warnings counter(s) if requested
708 */
709 if (newlim->d_fieldmask & FS_DQ_BWARNS)
710 INT_SET(ddq->d_bwarns, ARCH_CONVERT, newlim->d_bwarns);
711 if (newlim->d_fieldmask & FS_DQ_IWARNS)
712 INT_SET(ddq->d_iwarns, ARCH_CONVERT, newlim->d_iwarns);
713 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
714 INT_SET(ddq->d_rtbwarns, ARCH_CONVERT, newlim->d_rtbwarns);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (id == 0) {
717 /*
718 * Timelimits for the super user set the relative time
719 * the other users can be over quota for this file system.
720 * If it is zero a default is used. Ditto for the default
Nathan Scott754002b2005-06-21 15:49:06 +1000721 * soft and hard limit values (already done, above), and
722 * for warnings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
724 if (newlim->d_fieldmask & FS_DQ_BTIMER) {
725 mp->m_quotainfo->qi_btimelimit = newlim->d_btimer;
726 INT_SET(ddq->d_btimer, ARCH_CONVERT, newlim->d_btimer);
727 }
728 if (newlim->d_fieldmask & FS_DQ_ITIMER) {
729 mp->m_quotainfo->qi_itimelimit = newlim->d_itimer;
730 INT_SET(ddq->d_itimer, ARCH_CONVERT, newlim->d_itimer);
731 }
732 if (newlim->d_fieldmask & FS_DQ_RTBTIMER) {
733 mp->m_quotainfo->qi_rtbtimelimit = newlim->d_rtbtimer;
734 INT_SET(ddq->d_rtbtimer, ARCH_CONVERT, newlim->d_rtbtimer);
735 }
Nathan Scott754002b2005-06-21 15:49:06 +1000736 if (newlim->d_fieldmask & FS_DQ_BWARNS)
737 mp->m_quotainfo->qi_bwarnlimit = newlim->d_bwarns;
738 if (newlim->d_fieldmask & FS_DQ_IWARNS)
739 mp->m_quotainfo->qi_iwarnlimit = newlim->d_iwarns;
740 if (newlim->d_fieldmask & FS_DQ_RTBWARNS)
741 mp->m_quotainfo->qi_rtbwarnlimit = newlim->d_rtbwarns;
742 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /*
744 * If the user is now over quota, start the timelimit.
745 * The user will not be 'warned'.
746 * Note that we keep the timers ticking, whether enforcement
747 * is on or off. We don't really want to bother with iterating
748 * over all ondisk dquots and turning the timers on/off.
749 */
750 xfs_qm_adjust_dqtimers(mp, ddq);
751 }
752 dqp->dq_flags |= XFS_DQ_DIRTY;
753 xfs_trans_log_dquot(tp, dqp);
754
755 xfs_dqtrace_entry(dqp, "Q_SETQLIM: COMMIT");
756 xfs_trans_commit(tp, 0, NULL);
757 xfs_qm_dqprint(dqp);
758 xfs_qm_dqrele(dqp);
759 mutex_unlock(&(XFS_QI_QOFFLOCK(mp)));
760
761 return (0);
762}
763
764STATIC int
765xfs_qm_scall_getquota(
766 xfs_mount_t *mp,
767 xfs_dqid_t id,
768 uint type,
769 fs_disk_quota_t *out)
770{
771 xfs_dquot_t *dqp;
772 int error;
773
774 /*
775 * Try to get the dquot. We don't want it allocated on disk, so
776 * we aren't passing the XFS_QMOPT_DOALLOC flag. If it doesn't
777 * exist, we'll get ENOENT back.
778 */
779 if ((error = xfs_qm_dqget(mp, NULL, id, type, 0, &dqp))) {
780 return (error);
781 }
782
783 xfs_dqtrace_entry(dqp, "Q_GETQUOTA SUCCESS");
784 /*
785 * If everything's NULL, this dquot doesn't quite exist as far as
786 * our utility programs are concerned.
787 */
788 if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
789 xfs_qm_dqput(dqp);
790 return XFS_ERROR(ENOENT);
791 }
792 /* xfs_qm_dqprint(dqp); */
793 /*
794 * Convert the disk dquot to the exportable format
795 */
796 xfs_qm_export_dquot(mp, &dqp->q_core, out);
797 xfs_qm_dqput(dqp);
798 return (error ? XFS_ERROR(EFAULT) : 0);
799}
800
801
802STATIC int
803xfs_qm_log_quotaoff_end(
804 xfs_mount_t *mp,
805 xfs_qoff_logitem_t *startqoff,
806 uint flags)
807{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000808 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int error;
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000810 xfs_qoff_logitem_t *qoffi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF_END);
813
814 if ((error = xfs_trans_reserve(tp, 0, sizeof(xfs_qoff_logitem_t) * 2,
815 0, 0, XFS_DEFAULT_LOG_COUNT))) {
816 xfs_trans_cancel(tp, 0);
817 return (error);
818 }
819
820 qoffi = xfs_trans_get_qoff_item(tp, startqoff,
821 flags & XFS_ALL_QUOTA_ACCT);
822 xfs_trans_log_quotaoff_item(tp, qoffi);
823
824 /*
825 * We have to make sure that the transaction is secure on disk before we
826 * return and actually stop quota accounting. So, make it synchronous.
827 * We don't care about quotoff's performance.
828 */
829 xfs_trans_set_sync(tp);
830 error = xfs_trans_commit(tp, 0, NULL);
831 return (error);
832}
833
834
835STATIC int
836xfs_qm_log_quotaoff(
837 xfs_mount_t *mp,
838 xfs_qoff_logitem_t **qoffstartp,
839 uint flags)
840{
841 xfs_trans_t *tp;
842 int error;
843 unsigned long s;
844 xfs_qoff_logitem_t *qoffi=NULL;
845 uint oldsbqflag=0;
846
847 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_QUOTAOFF);
848 if ((error = xfs_trans_reserve(tp, 0,
849 sizeof(xfs_qoff_logitem_t) * 2 +
850 mp->m_sb.sb_sectsize + 128,
851 0,
852 0,
853 XFS_DEFAULT_LOG_COUNT))) {
854 goto error0;
855 }
856
857 qoffi = xfs_trans_get_qoff_item(tp, NULL, flags & XFS_ALL_QUOTA_ACCT);
858 xfs_trans_log_quotaoff_item(tp, qoffi);
859
860 s = XFS_SB_LOCK(mp);
861 oldsbqflag = mp->m_sb.sb_qflags;
862 mp->m_sb.sb_qflags = (mp->m_qflags & ~(flags)) & XFS_MOUNT_QUOTA_ALL;
863 XFS_SB_UNLOCK(mp, s);
864
865 xfs_mod_sb(tp, XFS_SB_QFLAGS);
866
867 /*
868 * We have to make sure that the transaction is secure on disk before we
869 * return and actually stop quota accounting. So, make it synchronous.
870 * We don't care about quotoff's performance.
871 */
872 xfs_trans_set_sync(tp);
873 error = xfs_trans_commit(tp, 0, NULL);
874
875error0:
876 if (error) {
877 xfs_trans_cancel(tp, 0);
878 /*
879 * No one else is modifying sb_qflags, so this is OK.
880 * We still hold the quotaofflock.
881 */
882 s = XFS_SB_LOCK(mp);
883 mp->m_sb.sb_qflags = oldsbqflag;
884 XFS_SB_UNLOCK(mp, s);
885 }
886 *qoffstartp = qoffi;
887 return (error);
888}
889
890
891/*
892 * Translate an internal style on-disk-dquot to the exportable format.
893 * The main differences are that the counters/limits are all in Basic
894 * Blocks (BBs) instead of the internal FSBs, and all on-disk data has
895 * to be converted to the native endianness.
896 */
897STATIC void
898xfs_qm_export_dquot(
899 xfs_mount_t *mp,
900 xfs_disk_dquot_t *src,
901 struct fs_disk_quota *dst)
902{
903 memset(dst, 0, sizeof(*dst));
904 dst->d_version = FS_DQUOT_VERSION; /* different from src->d_version */
905 dst->d_flags =
906 xfs_qm_export_qtype_flags(INT_GET(src->d_flags, ARCH_CONVERT));
907 dst->d_id = INT_GET(src->d_id, ARCH_CONVERT);
908 dst->d_blk_hardlimit = (__uint64_t)
909 XFS_FSB_TO_BB(mp, INT_GET(src->d_blk_hardlimit, ARCH_CONVERT));
910 dst->d_blk_softlimit = (__uint64_t)
911 XFS_FSB_TO_BB(mp, INT_GET(src->d_blk_softlimit, ARCH_CONVERT));
912 dst->d_ino_hardlimit = (__uint64_t)
913 INT_GET(src->d_ino_hardlimit, ARCH_CONVERT);
914 dst->d_ino_softlimit = (__uint64_t)
915 INT_GET(src->d_ino_softlimit, ARCH_CONVERT);
916 dst->d_bcount = (__uint64_t)
917 XFS_FSB_TO_BB(mp, INT_GET(src->d_bcount, ARCH_CONVERT));
918 dst->d_icount = (__uint64_t) INT_GET(src->d_icount, ARCH_CONVERT);
919 dst->d_btimer = (__uint32_t) INT_GET(src->d_btimer, ARCH_CONVERT);
920 dst->d_itimer = (__uint32_t) INT_GET(src->d_itimer, ARCH_CONVERT);
921 dst->d_iwarns = INT_GET(src->d_iwarns, ARCH_CONVERT);
922 dst->d_bwarns = INT_GET(src->d_bwarns, ARCH_CONVERT);
923
924 dst->d_rtb_hardlimit = (__uint64_t)
925 XFS_FSB_TO_BB(mp, INT_GET(src->d_rtb_hardlimit, ARCH_CONVERT));
926 dst->d_rtb_softlimit = (__uint64_t)
927 XFS_FSB_TO_BB(mp, INT_GET(src->d_rtb_softlimit, ARCH_CONVERT));
928 dst->d_rtbcount = (__uint64_t)
929 XFS_FSB_TO_BB(mp, INT_GET(src->d_rtbcount, ARCH_CONVERT));
930 dst->d_rtbtimer = (__uint32_t) INT_GET(src->d_rtbtimer, ARCH_CONVERT);
931 dst->d_rtbwarns = INT_GET(src->d_rtbwarns, ARCH_CONVERT);
932
933 /*
934 * Internally, we don't reset all the timers when quota enforcement
935 * gets turned off. No need to confuse the userlevel code,
936 * so return zeroes in that case.
937 */
938 if (! XFS_IS_QUOTA_ENFORCED(mp)) {
939 dst->d_btimer = 0;
940 dst->d_itimer = 0;
941 dst->d_rtbtimer = 0;
942 }
943
944#ifdef DEBUG
945 if (XFS_IS_QUOTA_ENFORCED(mp) && dst->d_id != 0) {
946 if (((int) dst->d_bcount >= (int) dst->d_blk_softlimit) &&
947 (dst->d_blk_softlimit > 0)) {
948 ASSERT(dst->d_btimer != 0);
949 }
950 if (((int) dst->d_icount >= (int) dst->d_ino_softlimit) &&
951 (dst->d_ino_softlimit > 0)) {
952 ASSERT(dst->d_itimer != 0);
953 }
954 }
955#endif
956}
957
958STATIC uint
959xfs_qm_import_qtype_flags(
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000960 uint uflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000962 uint oflags = 0;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000965 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
967 if (((uflags & (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ==
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000968 (XFS_GROUP_QUOTA | XFS_USER_QUOTA)) ||
969 ((uflags & (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ==
970 (XFS_GROUP_QUOTA | XFS_PROJ_QUOTA)) ||
971 ((uflags & (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ==
972 (XFS_USER_QUOTA | XFS_PROJ_QUOTA)) ||
973 ((uflags & (XFS_GROUP_QUOTA|XFS_USER_QUOTA|XFS_PROJ_QUOTA)) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return (0);
975
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000976 oflags |= (uflags & XFS_USER_QUOTA) ? XFS_DQ_USER : 0;
977 oflags |= (uflags & XFS_PROJ_QUOTA) ? XFS_DQ_PROJ : 0;
978 oflags |= (uflags & XFS_GROUP_QUOTA) ? XFS_DQ_GROUP: 0;
979 return oflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982STATIC uint
983xfs_qm_export_qtype_flags(
984 uint flags)
985{
986 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000987 * Can't be more than one, or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000989 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_USER_QUOTA)) !=
990 (XFS_PROJ_QUOTA | XFS_USER_QUOTA));
991 ASSERT((flags & (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA)) !=
992 (XFS_PROJ_QUOTA | XFS_GROUP_QUOTA));
993 ASSERT((flags & (XFS_USER_QUOTA | XFS_GROUP_QUOTA)) !=
994 (XFS_USER_QUOTA | XFS_GROUP_QUOTA));
995 ASSERT((flags & (XFS_PROJ_QUOTA|XFS_USER_QUOTA|XFS_GROUP_QUOTA)) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 return (flags & XFS_DQ_USER) ?
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000998 XFS_USER_QUOTA : (flags & XFS_DQ_PROJ) ?
999 XFS_PROJ_QUOTA : XFS_GROUP_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002STATIC uint
1003xfs_qm_import_flags(
1004 uint uflags)
1005{
1006 uint flags = 0;
1007
1008 if (uflags & XFS_QUOTA_UDQ_ACCT)
1009 flags |= XFS_UQUOTA_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001010 if (uflags & XFS_QUOTA_PDQ_ACCT)
1011 flags |= XFS_PQUOTA_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (uflags & XFS_QUOTA_GDQ_ACCT)
1013 flags |= XFS_GQUOTA_ACCT;
1014 if (uflags & XFS_QUOTA_UDQ_ENFD)
1015 flags |= XFS_UQUOTA_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001016 if (uflags & (XFS_QUOTA_PDQ_ENFD|XFS_QUOTA_GDQ_ENFD))
1017 flags |= XFS_OQUOTA_ENFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return (flags);
1019}
1020
1021
1022STATIC uint
1023xfs_qm_export_flags(
1024 uint flags)
1025{
1026 uint uflags;
1027
1028 uflags = 0;
1029 if (flags & XFS_UQUOTA_ACCT)
1030 uflags |= XFS_QUOTA_UDQ_ACCT;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001031 if (flags & XFS_PQUOTA_ACCT)
1032 uflags |= XFS_QUOTA_PDQ_ACCT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (flags & XFS_GQUOTA_ACCT)
1034 uflags |= XFS_QUOTA_GDQ_ACCT;
1035 if (flags & XFS_UQUOTA_ENFD)
1036 uflags |= XFS_QUOTA_UDQ_ENFD;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001037 if (flags & (XFS_OQUOTA_ENFD)) {
1038 uflags |= (flags & XFS_GQUOTA_ACCT) ?
1039 XFS_QUOTA_GDQ_ENFD : XFS_QUOTA_PDQ_ENFD;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return (uflags);
1042}
1043
1044
1045/*
1046 * Go thru all the inodes in the file system, releasing their dquots.
1047 * Note that the mount structure gets modified to indicate that quotas are off
1048 * AFTER this, in the case of quotaoff. This also gets called from
1049 * xfs_rootumount.
1050 */
1051void
1052xfs_qm_dqrele_all_inodes(
1053 struct xfs_mount *mp,
1054 uint flags)
1055{
1056 vmap_t vmap;
1057 xfs_inode_t *ip, *topino;
1058 uint ireclaims;
1059 vnode_t *vp;
1060 boolean_t vnode_refd;
1061
1062 ASSERT(mp->m_quotainfo);
1063
1064again:
1065 XFS_MOUNT_ILOCK(mp);
1066 ip = mp->m_inodes;
1067 if (ip == NULL) {
1068 XFS_MOUNT_IUNLOCK(mp);
1069 return;
1070 }
1071 do {
1072 /* Skip markers inserted by xfs_sync */
1073 if (ip->i_mount == NULL) {
1074 ip = ip->i_mnext;
1075 continue;
1076 }
1077 /* Root inode, rbmip and rsumip have associated blocks */
1078 if (ip == XFS_QI_UQIP(mp) || ip == XFS_QI_GQIP(mp)) {
1079 ASSERT(ip->i_udquot == NULL);
1080 ASSERT(ip->i_gdquot == NULL);
1081 ip = ip->i_mnext;
1082 continue;
1083 }
1084 vp = XFS_ITOV_NULL(ip);
1085 if (!vp) {
1086 ASSERT(ip->i_udquot == NULL);
1087 ASSERT(ip->i_gdquot == NULL);
1088 ip = ip->i_mnext;
1089 continue;
1090 }
1091 vnode_refd = B_FALSE;
1092 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1093 /*
1094 * Sample vp mapping while holding the mplock, lest
1095 * we come across a non-existent vnode.
1096 */
1097 VMAP(vp, vmap);
1098 ireclaims = mp->m_ireclaims;
1099 topino = mp->m_inodes;
1100 XFS_MOUNT_IUNLOCK(mp);
1101
1102 /* XXX restart limit ? */
1103 if ( ! (vp = vn_get(vp, &vmap)))
1104 goto again;
1105 xfs_ilock(ip, XFS_ILOCK_EXCL);
1106 vnode_refd = B_TRUE;
1107 } else {
1108 ireclaims = mp->m_ireclaims;
1109 topino = mp->m_inodes;
1110 XFS_MOUNT_IUNLOCK(mp);
1111 }
1112
1113 /*
1114 * We don't keep the mountlock across the dqrele() call,
1115 * since it can take a while..
1116 */
1117 if ((flags & XFS_UQUOTA_ACCT) && ip->i_udquot) {
1118 xfs_qm_dqrele(ip->i_udquot);
1119 ip->i_udquot = NULL;
1120 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001121 if (flags & (XFS_PQUOTA_ACCT|XFS_GQUOTA_ACCT) && ip->i_gdquot) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 xfs_qm_dqrele(ip->i_gdquot);
1123 ip->i_gdquot = NULL;
1124 }
1125 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1126 /*
1127 * Wait until we've dropped the ilock and mountlock to
1128 * do the vn_rele. Or be condemned to an eternity in the
1129 * inactive code in hell.
1130 */
1131 if (vnode_refd)
1132 VN_RELE(vp);
1133 XFS_MOUNT_ILOCK(mp);
1134 /*
1135 * If an inode was inserted or removed, we gotta
1136 * start over again.
1137 */
1138 if (topino != mp->m_inodes || mp->m_ireclaims != ireclaims) {
1139 /* XXX use a sentinel */
1140 XFS_MOUNT_IUNLOCK(mp);
1141 goto again;
1142 }
1143 ip = ip->i_mnext;
1144 } while (ip != mp->m_inodes);
1145
1146 XFS_MOUNT_IUNLOCK(mp);
1147}
1148
1149/*------------------------------------------------------------------------*/
1150#ifdef DEBUG
1151/*
1152 * This contains all the test functions for XFS disk quotas.
1153 * Currently it does a quota accounting check. ie. it walks through
1154 * all inodes in the file system, calculating the dquot accounting fields,
1155 * and prints out any inconsistencies.
1156 */
1157xfs_dqhash_t *qmtest_udqtab;
1158xfs_dqhash_t *qmtest_gdqtab;
1159int qmtest_hashmask;
1160int qmtest_nfails;
1161mutex_t qcheck_lock;
1162
1163#define DQTEST_HASHVAL(mp, id) (((__psunsigned_t)(mp) + \
1164 (__psunsigned_t)(id)) & \
1165 (qmtest_hashmask - 1))
1166
1167#define DQTEST_HASH(mp, id, type) ((type & XFS_DQ_USER) ? \
1168 (qmtest_udqtab + \
1169 DQTEST_HASHVAL(mp, id)) : \
1170 (qmtest_gdqtab + \
1171 DQTEST_HASHVAL(mp, id)))
1172
1173#define DQTEST_LIST_PRINT(l, NXT, title) \
1174{ \
1175 xfs_dqtest_t *dqp; int i = 0;\
1176 cmn_err(CE_DEBUG, "%s (#%d)", title, (int) (l)->qh_nelems); \
1177 for (dqp = (xfs_dqtest_t *)(l)->qh_next; dqp != NULL; \
1178 dqp = (xfs_dqtest_t *)dqp->NXT) { \
1179 cmn_err(CE_DEBUG, " %d. \"%d (%s)\" bcnt = %d, icnt = %d", \
1180 ++i, dqp->d_id, DQFLAGTO_TYPESTR(dqp), \
1181 dqp->d_bcount, dqp->d_icount); } \
1182}
1183
1184typedef struct dqtest {
1185 xfs_dqmarker_t q_lists;
1186 xfs_dqhash_t *q_hash; /* the hashchain header */
1187 xfs_mount_t *q_mount; /* filesystem this relates to */
1188 xfs_dqid_t d_id; /* user id or group id */
1189 xfs_qcnt_t d_bcount; /* # disk blocks owned by the user */
1190 xfs_qcnt_t d_icount; /* # inodes owned by the user */
1191} xfs_dqtest_t;
1192
1193STATIC void
1194xfs_qm_hashinsert(xfs_dqhash_t *h, xfs_dqtest_t *dqp)
1195{
1196 xfs_dquot_t *d;
1197 if (((d) = (h)->qh_next))
1198 (d)->HL_PREVP = &((dqp)->HL_NEXT);
1199 (dqp)->HL_NEXT = d;
1200 (dqp)->HL_PREVP = &((h)->qh_next);
1201 (h)->qh_next = (xfs_dquot_t *)dqp;
1202 (h)->qh_version++;
1203 (h)->qh_nelems++;
1204}
1205STATIC void
1206xfs_qm_dqtest_print(
1207 xfs_dqtest_t *d)
1208{
1209 cmn_err(CE_DEBUG, "-----------DQTEST DQUOT----------------");
1210 cmn_err(CE_DEBUG, "---- dquot ID = %d", d->d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 cmn_err(CE_DEBUG, "---- fs = 0x%p", d->q_mount);
1212 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1213 d->d_bcount, (int)d->d_bcount);
1214 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1215 d->d_icount, (int)d->d_icount);
1216 cmn_err(CE_DEBUG, "---------------------------");
1217}
1218
1219STATIC void
1220xfs_qm_dqtest_failed(
1221 xfs_dqtest_t *d,
1222 xfs_dquot_t *dqp,
1223 char *reason,
1224 xfs_qcnt_t a,
1225 xfs_qcnt_t b,
1226 int error)
1227{
1228 qmtest_nfails++;
1229 if (error)
1230 cmn_err(CE_DEBUG, "quotacheck failed id=%d, err=%d\nreason: %s",
1231 INT_GET(d->d_id, ARCH_CONVERT), error, reason);
1232 else
1233 cmn_err(CE_DEBUG, "quotacheck failed id=%d (%s) [%d != %d]",
1234 INT_GET(d->d_id, ARCH_CONVERT), reason, (int)a, (int)b);
1235 xfs_qm_dqtest_print(d);
1236 if (dqp)
1237 xfs_qm_dqprint(dqp);
1238}
1239
1240STATIC int
1241xfs_dqtest_cmp2(
1242 xfs_dqtest_t *d,
1243 xfs_dquot_t *dqp)
1244{
1245 int err = 0;
1246 if (INT_GET(dqp->q_core.d_icount, ARCH_CONVERT) != d->d_icount) {
1247 xfs_qm_dqtest_failed(d, dqp, "icount mismatch",
1248 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
1249 d->d_icount, 0);
1250 err++;
1251 }
1252 if (INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT) != d->d_bcount) {
1253 xfs_qm_dqtest_failed(d, dqp, "bcount mismatch",
1254 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
1255 d->d_bcount, 0);
1256 err++;
1257 }
1258 if (INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT) &&
1259 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT) >=
1260 INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT)) {
1261 if (!dqp->q_core.d_btimer && dqp->q_core.d_id) {
1262 cmn_err(CE_DEBUG,
1263 "%d [%s] [0x%p] BLK TIMER NOT STARTED",
1264 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1265 err++;
1266 }
1267 }
1268 if (INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT) &&
1269 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT) >=
1270 INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT)) {
1271 if (!dqp->q_core.d_itimer && dqp->q_core.d_id) {
1272 cmn_err(CE_DEBUG,
1273 "%d [%s] [0x%p] INO TIMER NOT STARTED",
1274 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
1275 err++;
1276 }
1277 }
1278#ifdef QUOTADEBUG
1279 if (!err) {
1280 cmn_err(CE_DEBUG, "%d [%s] [0x%p] qchecked",
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001281 d->d_id, DQFLAGTO_TYPESTR(d), d->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283#endif
1284 return (err);
1285}
1286
1287STATIC void
1288xfs_dqtest_cmp(
1289 xfs_dqtest_t *d)
1290{
1291 xfs_dquot_t *dqp;
1292 int error;
1293
1294 /* xfs_qm_dqtest_print(d); */
1295 if ((error = xfs_qm_dqget(d->q_mount, NULL, d->d_id, d->dq_flags, 0,
1296 &dqp))) {
1297 xfs_qm_dqtest_failed(d, NULL, "dqget failed", 0, 0, error);
1298 return;
1299 }
1300 xfs_dqtest_cmp2(d, dqp);
1301 xfs_qm_dqput(dqp);
1302}
1303
1304STATIC int
1305xfs_qm_internalqcheck_dqget(
1306 xfs_mount_t *mp,
1307 xfs_dqid_t id,
1308 uint type,
1309 xfs_dqtest_t **O_dq)
1310{
1311 xfs_dqtest_t *d;
1312 xfs_dqhash_t *h;
1313
1314 h = DQTEST_HASH(mp, id, type);
1315 for (d = (xfs_dqtest_t *) h->qh_next; d != NULL;
1316 d = (xfs_dqtest_t *) d->HL_NEXT) {
1317 /* DQTEST_LIST_PRINT(h, HL_NEXT, "@@@@@ dqtestlist @@@@@"); */
1318 if (d->d_id == id && mp == d->q_mount) {
1319 *O_dq = d;
1320 return (0);
1321 }
1322 }
1323 d = kmem_zalloc(sizeof(xfs_dqtest_t), KM_SLEEP);
1324 d->dq_flags = type;
1325 d->d_id = id;
1326 d->q_mount = mp;
1327 d->q_hash = h;
1328 xfs_qm_hashinsert(h, d);
1329 *O_dq = d;
1330 return (0);
1331}
1332
1333STATIC void
1334xfs_qm_internalqcheck_get_dquots(
1335 xfs_mount_t *mp,
1336 xfs_dqid_t uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001337 xfs_dqid_t projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 xfs_dqid_t gid,
1339 xfs_dqtest_t **ud,
1340 xfs_dqtest_t **gd)
1341{
1342 if (XFS_IS_UQUOTA_ON(mp))
1343 xfs_qm_internalqcheck_dqget(mp, uid, XFS_DQ_USER, ud);
1344 if (XFS_IS_GQUOTA_ON(mp))
1345 xfs_qm_internalqcheck_dqget(mp, gid, XFS_DQ_GROUP, gd);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001346 else if (XFS_IS_PQUOTA_ON(mp))
1347 xfs_qm_internalqcheck_dqget(mp, projid, XFS_DQ_PROJ, gd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350
1351STATIC void
1352xfs_qm_internalqcheck_dqadjust(
1353 xfs_inode_t *ip,
1354 xfs_dqtest_t *d)
1355{
1356 d->d_icount++;
1357 d->d_bcount += (xfs_qcnt_t)ip->i_d.di_nblocks;
1358}
1359
1360STATIC int
1361xfs_qm_internalqcheck_adjust(
1362 xfs_mount_t *mp, /* mount point for filesystem */
1363 xfs_ino_t ino, /* inode number to get data for */
1364 void __user *buffer, /* not used */
1365 int ubsize, /* not used */
1366 void *private_data, /* not used */
1367 xfs_daddr_t bno, /* starting block of inode cluster */
1368 int *ubused, /* not used */
1369 void *dip, /* not used */
1370 int *res) /* bulkstat result code */
1371{
1372 xfs_inode_t *ip;
1373 xfs_dqtest_t *ud, *gd;
1374 uint lock_flags;
1375 boolean_t ipreleased;
1376 int error;
1377
1378 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
1379
1380 if (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino) {
1381 *res = BULKSTAT_RV_NOTHING;
1382 qdprintk("internalqcheck: ino=%llu, uqino=%llu, gqino=%llu\n",
1383 (unsigned long long) ino,
1384 (unsigned long long) mp->m_sb.sb_uquotino,
1385 (unsigned long long) mp->m_sb.sb_gquotino);
1386 return XFS_ERROR(EINVAL);
1387 }
1388 ipreleased = B_FALSE;
1389 again:
1390 lock_flags = XFS_ILOCK_SHARED;
1391 if ((error = xfs_iget(mp, NULL, ino, 0, lock_flags, &ip, bno))) {
1392 *res = BULKSTAT_RV_NOTHING;
1393 return (error);
1394 }
1395
1396 if (ip->i_d.di_mode == 0) {
1397 xfs_iput_new(ip, lock_flags);
1398 *res = BULKSTAT_RV_NOTHING;
1399 return XFS_ERROR(ENOENT);
1400 }
1401
1402 /*
1403 * This inode can have blocks after eof which can get released
1404 * when we send it to inactive. Since we don't check the dquot
1405 * until the after all our calculations are done, we must get rid
1406 * of those now.
1407 */
1408 if (! ipreleased) {
1409 xfs_iput(ip, lock_flags);
1410 ipreleased = B_TRUE;
1411 goto again;
1412 }
1413 xfs_qm_internalqcheck_get_dquots(mp,
1414 (xfs_dqid_t) ip->i_d.di_uid,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001415 (xfs_dqid_t) ip->i_d.di_projid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 (xfs_dqid_t) ip->i_d.di_gid,
1417 &ud, &gd);
1418 if (XFS_IS_UQUOTA_ON(mp)) {
1419 ASSERT(ud);
1420 xfs_qm_internalqcheck_dqadjust(ip, ud);
1421 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001422 if (XFS_IS_OQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ASSERT(gd);
1424 xfs_qm_internalqcheck_dqadjust(ip, gd);
1425 }
1426 xfs_iput(ip, lock_flags);
1427 *res = BULKSTAT_RV_DIDONE;
1428 return (0);
1429}
1430
1431
1432/* PRIVATE, debugging */
1433int
1434xfs_qm_internalqcheck(
1435 xfs_mount_t *mp)
1436{
1437 xfs_ino_t lastino;
1438 int done, count;
1439 int i;
1440 xfs_dqtest_t *d, *e;
1441 xfs_dqhash_t *h1;
1442 int error;
1443
1444 lastino = 0;
1445 qmtest_hashmask = 32;
1446 count = 5;
1447 done = 0;
1448 qmtest_nfails = 0;
1449
1450 if (! XFS_IS_QUOTA_ON(mp))
1451 return XFS_ERROR(ESRCH);
1452
1453 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1454 XFS_bflush(mp->m_ddev_targp);
1455 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1456 XFS_bflush(mp->m_ddev_targp);
1457
1458 mutex_lock(&qcheck_lock, PINOD);
1459 /* There should be absolutely no quota activity while this
1460 is going on. */
1461 qmtest_udqtab = kmem_zalloc(qmtest_hashmask *
1462 sizeof(xfs_dqhash_t), KM_SLEEP);
1463 qmtest_gdqtab = kmem_zalloc(qmtest_hashmask *
1464 sizeof(xfs_dqhash_t), KM_SLEEP);
1465 do {
1466 /*
1467 * Iterate thru all the inodes in the file system,
1468 * adjusting the corresponding dquot counters
1469 */
1470 if ((error = xfs_bulkstat(mp, &lastino, &count,
1471 xfs_qm_internalqcheck_adjust, NULL,
1472 0, NULL, BULKSTAT_FG_IGET, &done))) {
1473 break;
1474 }
1475 } while (! done);
1476 if (error) {
1477 cmn_err(CE_DEBUG, "Bulkstat returned error 0x%x", error);
1478 }
1479 cmn_err(CE_DEBUG, "Checking results against system dquots");
1480 for (i = 0; i < qmtest_hashmask; i++) {
1481 h1 = &qmtest_udqtab[i];
1482 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1483 xfs_dqtest_cmp(d);
1484 e = (xfs_dqtest_t *) d->HL_NEXT;
1485 kmem_free(d, sizeof(xfs_dqtest_t));
1486 d = e;
1487 }
1488 h1 = &qmtest_gdqtab[i];
1489 for (d = (xfs_dqtest_t *) h1->qh_next; d != NULL; ) {
1490 xfs_dqtest_cmp(d);
1491 e = (xfs_dqtest_t *) d->HL_NEXT;
1492 kmem_free(d, sizeof(xfs_dqtest_t));
1493 d = e;
1494 }
1495 }
1496
1497 if (qmtest_nfails) {
1498 cmn_err(CE_DEBUG, "******** quotacheck failed ********");
1499 cmn_err(CE_DEBUG, "failures = %d", qmtest_nfails);
1500 } else {
1501 cmn_err(CE_DEBUG, "******** quotacheck successful! ********");
1502 }
1503 kmem_free(qmtest_udqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1504 kmem_free(qmtest_gdqtab, qmtest_hashmask * sizeof(xfs_dqhash_t));
1505 mutex_unlock(&qcheck_lock);
1506 return (qmtest_nfails);
1507}
1508
1509#endif /* DEBUG */