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