blob: 68089f56d5cd1943b1afaaeca8af815150cdae11 [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_ag.h"
40#include "xfs_dir.h"
41#include "xfs_dir2.h"
42#include "xfs_alloc.h"
43#include "xfs_dmapi.h"
44#include "xfs_quota.h"
45#include "xfs_mount.h"
46#include "xfs_alloc_btree.h"
47#include "xfs_bmap_btree.h"
48#include "xfs_ialloc_btree.h"
49#include "xfs_btree.h"
50#include "xfs_ialloc.h"
51#include "xfs_attr_sf.h"
52#include "xfs_dir_sf.h"
53#include "xfs_dir2_sf.h"
54#include "xfs_dinode.h"
55#include "xfs_inode.h"
56#include "xfs_bmap.h"
57#include "xfs_bit.h"
58#include "xfs_rtalloc.h"
59#include "xfs_error.h"
60#include "xfs_itable.h"
61#include "xfs_rw.h"
62#include "xfs_acl.h"
63#include "xfs_cap.h"
64#include "xfs_mac.h"
65#include "xfs_attr.h"
66#include "xfs_buf_item.h"
67#include "xfs_trans_space.h"
68#include "xfs_trans_priv.h"
69
70#include "xfs_qm.h"
71
72
73/*
74 LOCK ORDER
75
76 inode lock (ilock)
77 dquot hash-chain lock (hashlock)
78 xqm dquot freelist lock (freelistlock
79 mount's dquot list lock (mplistlock)
80 user dquot lock - lock ordering among dquots is based on the uid or gid
81 group dquot lock - similar to udquots. Between the two dquots, the udquot
82 has to be locked first.
83 pin lock - the dquot lock must be held to take this lock.
84 flush lock - ditto.
85*/
86
87STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
88
89#ifdef DEBUG
90xfs_buftarg_t *xfs_dqerror_target;
91int xfs_do_dqerror;
92int xfs_dqreq_num;
93int xfs_dqerror_mod = 33;
94#endif
95
96/*
97 * Allocate and initialize a dquot. We don't always allocate fresh memory;
98 * we try to reclaim a free dquot if the number of incore dquots are above
99 * a threshold.
100 * The only field inside the core that gets initialized at this point
101 * is the d_id field. The idea is to fill in the entire q_core
102 * when we read in the on disk dquot.
103 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000104STATIC xfs_dquot_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105xfs_qm_dqinit(
106 xfs_mount_t *mp,
107 xfs_dqid_t id,
108 uint type)
109{
110 xfs_dquot_t *dqp;
111 boolean_t brandnewdquot;
112
113 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
114 dqp->dq_flags = type;
115 INT_SET(dqp->q_core.d_id, ARCH_CONVERT, id);
116 dqp->q_mount = mp;
117
118 /*
119 * No need to re-initialize these if this is a reclaimed dquot.
120 */
121 if (brandnewdquot) {
122 dqp->dq_flnext = dqp->dq_flprev = dqp;
123 mutex_init(&dqp->q_qlock, MUTEX_DEFAULT, "xdq");
124 initnsema(&dqp->q_flock, 1, "fdq");
125 sv_init(&dqp->q_pinwait, SV_DEFAULT, "pdq");
126
127#ifdef XFS_DQUOT_TRACE
128 dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_SLEEP);
129 xfs_dqtrace_entry(dqp, "DQINIT");
130#endif
131 } else {
132 /*
133 * Only the q_core portion was zeroed in dqreclaim_one().
134 * So, we need to reset others.
135 */
136 dqp->q_nrefs = 0;
137 dqp->q_blkno = 0;
138 dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
139 dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
140 dqp->q_bufoffset = 0;
141 dqp->q_fileoffset = 0;
142 dqp->q_transp = NULL;
143 dqp->q_gdquot = NULL;
144 dqp->q_res_bcount = 0;
145 dqp->q_res_icount = 0;
146 dqp->q_res_rtbcount = 0;
147 dqp->q_pincount = 0;
148 dqp->q_hash = NULL;
149 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
150
151#ifdef XFS_DQUOT_TRACE
152 ASSERT(dqp->q_trace);
153 xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
154#endif
155 }
156
157 /*
158 * log item gets initialized later
159 */
160 return (dqp);
161}
162
163/*
164 * This is called to free all the memory associated with a dquot
165 */
166void
167xfs_qm_dqdestroy(
168 xfs_dquot_t *dqp)
169{
170 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
171
172 mutex_destroy(&dqp->q_qlock);
173 freesema(&dqp->q_flock);
174 sv_destroy(&dqp->q_pinwait);
175
176#ifdef XFS_DQUOT_TRACE
177 if (dqp->q_trace)
178 ktrace_free(dqp->q_trace);
179 dqp->q_trace = NULL;
180#endif
181 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
182 atomic_dec(&xfs_Gqm->qm_totaldquots);
183}
184
185/*
186 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
187 */
188STATIC void
189xfs_qm_dqinit_core(
190 xfs_dqid_t id,
191 uint type,
192 xfs_dqblk_t *d)
193{
194 /*
195 * Caller has zero'd the entire dquot 'chunk' already.
196 */
197 INT_SET(d->dd_diskdq.d_magic, ARCH_CONVERT, XFS_DQUOT_MAGIC);
198 INT_SET(d->dd_diskdq.d_version, ARCH_CONVERT, XFS_DQUOT_VERSION);
199 INT_SET(d->dd_diskdq.d_id, ARCH_CONVERT, id);
200 INT_SET(d->dd_diskdq.d_flags, ARCH_CONVERT, type);
201}
202
203
204#ifdef XFS_DQUOT_TRACE
205/*
206 * Dquot tracing for debugging.
207 */
208/* ARGSUSED */
209void
210__xfs_dqtrace_entry(
211 xfs_dquot_t *dqp,
212 char *func,
213 void *retaddr,
214 xfs_inode_t *ip)
215{
216 xfs_dquot_t *udqp = NULL;
217 xfs_ino_t ino = 0;
218
219 ASSERT(dqp->q_trace);
220 if (ip) {
221 ino = ip->i_ino;
222 udqp = ip->i_udquot;
223 }
224 ktrace_enter(dqp->q_trace,
225 (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
226 (void *)func,
227 (void *)(__psint_t)dqp->q_nrefs,
228 (void *)(__psint_t)dqp->dq_flags,
229 (void *)(__psint_t)dqp->q_res_bcount,
230 (void *)(__psint_t)INT_GET(dqp->q_core.d_bcount,
231 ARCH_CONVERT),
232 (void *)(__psint_t)INT_GET(dqp->q_core.d_icount,
233 ARCH_CONVERT),
234 (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_hardlimit,
235 ARCH_CONVERT),
236 (void *)(__psint_t)INT_GET(dqp->q_core.d_blk_softlimit,
237 ARCH_CONVERT),
238 (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_hardlimit,
239 ARCH_CONVERT),
240 (void *)(__psint_t)INT_GET(dqp->q_core.d_ino_softlimit,
241 ARCH_CONVERT),
242 (void *)(__psint_t)INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
243 (void *)(__psint_t)current_pid(),
244 (void *)(__psint_t)ino,
245 (void *)(__psint_t)retaddr,
246 (void *)(__psint_t)udqp);
247 return;
248}
249#endif
250
251
252/*
253 * If default limits are in force, push them into the dquot now.
254 * We overwrite the dquot limits only if they are zero and this
255 * is not the root dquot.
256 */
257void
258xfs_qm_adjust_dqlimits(
259 xfs_mount_t *mp,
260 xfs_disk_dquot_t *d)
261{
262 xfs_quotainfo_t *q = mp->m_quotainfo;
263
264 ASSERT(d->d_id);
265
266 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
267 INT_SET(d->d_blk_softlimit, ARCH_CONVERT, q->qi_bsoftlimit);
268 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
269 INT_SET(d->d_blk_hardlimit, ARCH_CONVERT, q->qi_bhardlimit);
270 if (q->qi_isoftlimit && !d->d_ino_softlimit)
271 INT_SET(d->d_ino_softlimit, ARCH_CONVERT, q->qi_isoftlimit);
272 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
273 INT_SET(d->d_ino_hardlimit, ARCH_CONVERT, q->qi_ihardlimit);
274 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
275 INT_SET(d->d_rtb_softlimit, ARCH_CONVERT, q->qi_rtbsoftlimit);
276 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
277 INT_SET(d->d_rtb_hardlimit, ARCH_CONVERT, q->qi_rtbhardlimit);
278}
279
280/*
281 * Check the limits and timers of a dquot and start or reset timers
282 * if necessary.
283 * This gets called even when quota enforcement is OFF, which makes our
284 * life a little less complicated. (We just don't reject any quota
285 * reservations in that case, when enforcement is off).
286 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
287 * enforcement's off.
288 * In contrast, warnings are a little different in that they don't
289 * 'automatically' get started when limits get exceeded.
290 */
291void
292xfs_qm_adjust_dqtimers(
293 xfs_mount_t *mp,
294 xfs_disk_dquot_t *d)
295{
296 ASSERT(d->d_id);
297
298#ifdef QUOTADEBUG
299 if (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT))
300 ASSERT(INT_GET(d->d_blk_softlimit, ARCH_CONVERT) <=
301 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT));
302 if (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT))
303 ASSERT(INT_GET(d->d_ino_softlimit, ARCH_CONVERT) <=
304 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT));
305 if (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT))
306 ASSERT(INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) <=
307 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT));
308#endif
309 if (!d->d_btimer) {
310 if ((INT_GET(d->d_blk_softlimit, ARCH_CONVERT) &&
311 (INT_GET(d->d_bcount, ARCH_CONVERT) >=
312 INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) ||
313 (INT_GET(d->d_blk_hardlimit, ARCH_CONVERT) &&
314 (INT_GET(d->d_bcount, ARCH_CONVERT) >=
315 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
316 INT_SET(d->d_btimer, ARCH_CONVERT,
317 get_seconds() + XFS_QI_BTIMELIMIT(mp));
318 }
319 } else {
320 if ((!d->d_blk_softlimit ||
321 (INT_GET(d->d_bcount, ARCH_CONVERT) <
322 INT_GET(d->d_blk_softlimit, ARCH_CONVERT))) &&
323 (!d->d_blk_hardlimit ||
324 (INT_GET(d->d_bcount, ARCH_CONVERT) <
325 INT_GET(d->d_blk_hardlimit, ARCH_CONVERT)))) {
326 d->d_btimer = 0;
327 }
328 }
329
330 if (!d->d_itimer) {
331 if ((INT_GET(d->d_ino_softlimit, ARCH_CONVERT) &&
332 (INT_GET(d->d_icount, ARCH_CONVERT) >=
333 INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) ||
334 (INT_GET(d->d_ino_hardlimit, ARCH_CONVERT) &&
335 (INT_GET(d->d_icount, ARCH_CONVERT) >=
336 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
337 INT_SET(d->d_itimer, ARCH_CONVERT,
338 get_seconds() + XFS_QI_ITIMELIMIT(mp));
339 }
340 } else {
341 if ((!d->d_ino_softlimit ||
342 (INT_GET(d->d_icount, ARCH_CONVERT) <
343 INT_GET(d->d_ino_softlimit, ARCH_CONVERT))) &&
344 (!d->d_ino_hardlimit ||
345 (INT_GET(d->d_icount, ARCH_CONVERT) <
346 INT_GET(d->d_ino_hardlimit, ARCH_CONVERT)))) {
347 d->d_itimer = 0;
348 }
349 }
350
351 if (!d->d_rtbtimer) {
352 if ((INT_GET(d->d_rtb_softlimit, ARCH_CONVERT) &&
353 (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
354 INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) ||
355 (INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT) &&
356 (INT_GET(d->d_rtbcount, ARCH_CONVERT) >=
357 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
358 INT_SET(d->d_rtbtimer, ARCH_CONVERT,
359 get_seconds() + XFS_QI_RTBTIMELIMIT(mp));
360 }
361 } else {
362 if ((!d->d_rtb_softlimit ||
363 (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
364 INT_GET(d->d_rtb_softlimit, ARCH_CONVERT))) &&
365 (!d->d_rtb_hardlimit ||
366 (INT_GET(d->d_rtbcount, ARCH_CONVERT) <
367 INT_GET(d->d_rtb_hardlimit, ARCH_CONVERT)))) {
368 d->d_rtbtimer = 0;
369 }
370 }
371}
372
373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * initialize a buffer full of dquots and log the whole thing
375 */
376STATIC void
377xfs_qm_init_dquot_blk(
378 xfs_trans_t *tp,
379 xfs_mount_t *mp,
380 xfs_dqid_t id,
381 uint type,
382 xfs_buf_t *bp)
383{
384 xfs_dqblk_t *d;
385 int curid, i;
386
387 ASSERT(tp);
388 ASSERT(XFS_BUF_ISBUSY(bp));
389 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
390
391 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
392
393 /*
394 * ID of the first dquot in the block - id's are zero based.
395 */
396 curid = id - (id % XFS_QM_DQPERBLK(mp));
397 ASSERT(curid >= 0);
398 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
399 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
400 xfs_qm_dqinit_core(curid, type, d);
401 xfs_trans_dquot_buf(tp, bp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000402 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
403 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
404 XFS_BLI_GDQUOT_BUF)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
406}
407
408
409
410/*
411 * Allocate a block and fill it with dquots.
412 * This is called when the bmapi finds a hole.
413 */
414STATIC int
415xfs_qm_dqalloc(
416 xfs_trans_t *tp,
417 xfs_mount_t *mp,
418 xfs_dquot_t *dqp,
419 xfs_inode_t *quotip,
420 xfs_fileoff_t offset_fsb,
421 xfs_buf_t **O_bpp)
422{
423 xfs_fsblock_t firstblock;
424 xfs_bmap_free_t flist;
425 xfs_bmbt_irec_t map;
426 int nmaps, error, committed;
427 xfs_buf_t *bp;
428
429 ASSERT(tp != NULL);
430 xfs_dqtrace_entry(dqp, "DQALLOC");
431
432 /*
433 * Initialize the bmap freelist prior to calling bmapi code.
434 */
435 XFS_BMAP_INIT(&flist, &firstblock);
436 xfs_ilock(quotip, XFS_ILOCK_EXCL);
437 /*
438 * Return if this type of quotas is turned off while we didn't
439 * have an inode lock
440 */
441 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
442 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
443 return (ESRCH);
444 }
445
446 /*
447 * xfs_trans_commit normally decrements the vnode ref count
448 * when it unlocks the inode. Since we want to keep the quota
449 * inode around, we bump the vnode ref count now.
450 */
451 VN_HOLD(XFS_ITOV(quotip));
452
453 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
454 nmaps = 1;
455 if ((error = xfs_bmapi(tp, quotip,
456 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
457 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
458 &firstblock,
459 XFS_QM_DQALLOC_SPACE_RES(mp),
460 &map, &nmaps, &flist))) {
461 goto error0;
462 }
463 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
464 ASSERT(nmaps == 1);
465 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
466 (map.br_startblock != HOLESTARTBLOCK));
467
468 /*
469 * Keep track of the blkno to save a lookup later
470 */
471 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
472
473 /* now we can just get the buffer (there's nothing to read yet) */
474 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
475 dqp->q_blkno,
476 XFS_QI_DQCHUNKLEN(mp),
477 0);
478 if (!bp || (error = XFS_BUF_GETERROR(bp)))
479 goto error1;
480 /*
481 * Make a chunk of dquots out of this buffer and log
482 * the entire thing.
483 */
484 xfs_qm_init_dquot_blk(tp, mp, INT_GET(dqp->q_core.d_id, ARCH_CONVERT),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000485 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed))) {
488 goto error1;
489 }
490
491 *O_bpp = bp;
492 return 0;
493
494 error1:
495 xfs_bmap_cancel(&flist);
496 error0:
497 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
498
499 return (error);
500}
501
502/*
503 * Maps a dquot to the buffer containing its on-disk version.
504 * This returns a ptr to the buffer containing the on-disk dquot
505 * in the bpp param, and a ptr to the on-disk dquot within that buffer
506 */
507STATIC int
508xfs_qm_dqtobp(
509 xfs_trans_t *tp,
510 xfs_dquot_t *dqp,
511 xfs_disk_dquot_t **O_ddpp,
512 xfs_buf_t **O_bpp,
513 uint flags)
514{
515 xfs_bmbt_irec_t map;
516 int nmaps, error;
517 xfs_buf_t *bp;
518 xfs_inode_t *quotip;
519 xfs_mount_t *mp;
520 xfs_disk_dquot_t *ddq;
521 xfs_dqid_t id;
522 boolean_t newdquot;
523
524 mp = dqp->q_mount;
525 id = INT_GET(dqp->q_core.d_id, ARCH_CONVERT);
526 nmaps = 1;
527 newdquot = B_FALSE;
528
529 /*
530 * If we don't know where the dquot lives, find out.
531 */
532 if (dqp->q_blkno == (xfs_daddr_t) 0) {
533 /* We use the id as an index */
534 dqp->q_fileoffset = (xfs_fileoff_t) ((uint)id /
535 XFS_QM_DQPERBLK(mp));
536 nmaps = 1;
537 quotip = XFS_DQ_TO_QIP(dqp);
538 xfs_ilock(quotip, XFS_ILOCK_SHARED);
539 /*
540 * Return if this type of quotas is turned off while we didn't
541 * have an inode lock
542 */
543 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
544 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
545 return (ESRCH);
546 }
547 /*
548 * Find the block map; no allocations yet
549 */
550 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
551 XFS_DQUOT_CLUSTER_SIZE_FSB,
552 XFS_BMAPI_METADATA,
553 NULL, 0, &map, &nmaps, NULL);
554
555 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
556 if (error)
557 return (error);
558 ASSERT(nmaps == 1);
559 ASSERT(map.br_blockcount == 1);
560
561 /*
562 * offset of dquot in the (fixed sized) dquot chunk.
563 */
564 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
565 sizeof(xfs_dqblk_t);
566 if (map.br_startblock == HOLESTARTBLOCK) {
567 /*
568 * We don't allocate unless we're asked to
569 */
570 if (!(flags & XFS_QMOPT_DQALLOC))
571 return (ENOENT);
572
573 ASSERT(tp);
574 if ((error = xfs_qm_dqalloc(tp, mp, dqp, quotip,
575 dqp->q_fileoffset, &bp)))
576 return (error);
577 newdquot = B_TRUE;
578 } else {
579 /*
580 * store the blkno etc so that we don't have to do the
581 * mapping all the time
582 */
583 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
584 }
585 }
586 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
587 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
588
589 /*
590 * Read in the buffer, unless we've just done the allocation
591 * (in which case we already have the buf).
592 */
593 if (! newdquot) {
594 xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
595 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
596 dqp->q_blkno,
597 XFS_QI_DQCHUNKLEN(mp),
598 0, &bp))) {
599 return (error);
600 }
601 if (error || !bp)
602 return XFS_ERROR(error);
603 }
604 ASSERT(XFS_BUF_ISBUSY(bp));
605 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
606
607 /*
608 * calculate the location of the dquot inside the buffer.
609 */
610 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
611
612 /*
613 * A simple sanity check in case we got a corrupted dquot...
614 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000615 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
617 "dqtobp")) {
618 if (!(flags & XFS_QMOPT_DQREPAIR)) {
619 xfs_trans_brelse(tp, bp);
620 return XFS_ERROR(EIO);
621 }
622 XFS_BUF_BUSY(bp); /* We dirtied this */
623 }
624
625 *O_bpp = bp;
626 *O_ddpp = ddq;
627
628 return (0);
629}
630
631
632/*
633 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
634 * and release the buffer immediately.
635 *
636 */
637/* ARGSUSED */
638STATIC int
639xfs_qm_dqread(
640 xfs_trans_t *tp,
641 xfs_dqid_t id,
642 xfs_dquot_t *dqp, /* dquot to get filled in */
643 uint flags)
644{
645 xfs_disk_dquot_t *ddqp;
646 xfs_buf_t *bp;
647 int error;
648
649 /*
650 * get a pointer to the on-disk dquot and the buffer containing it
651 * dqp already knows its own type (GROUP/USER).
652 */
653 xfs_dqtrace_entry(dqp, "DQREAD");
654 if ((error = xfs_qm_dqtobp(tp, dqp, &ddqp, &bp, flags))) {
655 return (error);
656 }
657
658 /* copy everything from disk dquot to the incore dquot */
659 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
660 ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
661 xfs_qm_dquot_logitem_init(dqp);
662
663 /*
664 * Reservation counters are defined as reservation plus current usage
665 * to avoid having to add everytime.
666 */
667 dqp->q_res_bcount = INT_GET(ddqp->d_bcount, ARCH_CONVERT);
668 dqp->q_res_icount = INT_GET(ddqp->d_icount, ARCH_CONVERT);
669 dqp->q_res_rtbcount = INT_GET(ddqp->d_rtbcount, ARCH_CONVERT);
670
671 /* Mark the buf so that this will stay incore a little longer */
672 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
673
674 /*
675 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
676 * So we need to release with xfs_trans_brelse().
677 * The strategy here is identical to that of inodes; we lock
678 * the dquot in xfs_qm_dqget() before making it accessible to
679 * others. This is because dquots, like inodes, need a good level of
680 * concurrency, and we don't want to take locks on the entire buffers
681 * for dquot accesses.
682 * Note also that the dquot buffer may even be dirty at this point, if
683 * this particular dquot was repaired. We still aren't afraid to
684 * brelse it because we have the changes incore.
685 */
686 ASSERT(XFS_BUF_ISBUSY(bp));
687 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
688 xfs_trans_brelse(tp, bp);
689
690 return (error);
691}
692
693
694/*
695 * allocate an incore dquot from the kernel heap,
696 * and fill its core with quota information kept on disk.
697 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
698 * if it wasn't already allocated.
699 */
700STATIC int
701xfs_qm_idtodq(
702 xfs_mount_t *mp,
703 xfs_dqid_t id, /* gid or uid, depending on type */
704 uint type, /* UDQUOT or GDQUOT */
705 uint flags, /* DQALLOC, DQREPAIR */
706 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
707{
708 xfs_dquot_t *dqp;
709 int error;
710 xfs_trans_t *tp;
711 int cancelflags=0;
712
713 dqp = xfs_qm_dqinit(mp, id, type);
714 tp = NULL;
715 if (flags & XFS_QMOPT_DQALLOC) {
716 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
717 if ((error = xfs_trans_reserve(tp,
718 XFS_QM_DQALLOC_SPACE_RES(mp),
719 XFS_WRITE_LOG_RES(mp) +
720 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
721 128,
722 0,
723 XFS_TRANS_PERM_LOG_RES,
724 XFS_WRITE_LOG_COUNT))) {
725 cancelflags = 0;
726 goto error0;
727 }
728 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
729 }
730
731 /*
732 * Read it from disk; xfs_dqread() takes care of
733 * all the necessary initialization of dquot's fields (locks, etc)
734 */
735 if ((error = xfs_qm_dqread(tp, id, dqp, flags))) {
736 /*
737 * This can happen if quotas got turned off (ESRCH),
738 * or if the dquot didn't exist on disk and we ask to
739 * allocate (ENOENT).
740 */
741 xfs_dqtrace_entry(dqp, "DQREAD FAIL");
742 cancelflags |= XFS_TRANS_ABORT;
743 goto error0;
744 }
745 if (tp) {
746 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES,
747 NULL)))
748 goto error1;
749 }
750
751 *O_dqpp = dqp;
752 return (0);
753
754 error0:
755 ASSERT(error);
756 if (tp)
757 xfs_trans_cancel(tp, cancelflags);
758 error1:
759 xfs_qm_dqdestroy(dqp);
760 *O_dqpp = NULL;
761 return (error);
762}
763
764/*
765 * Lookup a dquot in the incore dquot hashtable. We keep two separate
766 * hashtables for user and group dquots; and, these are global tables
767 * inside the XQM, not per-filesystem tables.
768 * The hash chain must be locked by caller, and it is left locked
769 * on return. Returning dquot is locked.
770 */
771STATIC int
772xfs_qm_dqlookup(
773 xfs_mount_t *mp,
774 xfs_dqid_t id,
775 xfs_dqhash_t *qh,
776 xfs_dquot_t **O_dqpp)
777{
778 xfs_dquot_t *dqp;
779 uint flist_locked;
780 xfs_dquot_t *d;
781
782 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
783
784 flist_locked = B_FALSE;
785
786 /*
787 * Traverse the hashchain looking for a match
788 */
789 for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
790 /*
791 * We already have the hashlock. We don't need the
792 * dqlock to look at the id field of the dquot, since the
793 * id can't be modified without the hashlock anyway.
794 */
795 if (INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id && dqp->q_mount == mp) {
796 xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
797 /*
798 * All in core dquots must be on the dqlist of mp
799 */
800 ASSERT(dqp->MPL_PREVP != NULL);
801
802 xfs_dqlock(dqp);
803 if (dqp->q_nrefs == 0) {
804 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
805 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
806 xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
807
808 /*
809 * We may have raced with dqreclaim_one()
810 * (and lost). So, flag that we don't
811 * want the dquot to be reclaimed.
812 */
813 dqp->dq_flags |= XFS_DQ_WANT;
814 xfs_dqunlock(dqp);
815 xfs_qm_freelist_lock(xfs_Gqm);
816 xfs_dqlock(dqp);
817 dqp->dq_flags &= ~(XFS_DQ_WANT);
818 }
819 flist_locked = B_TRUE;
820 }
821
822 /*
823 * id couldn't have changed; we had the hashlock all
824 * along
825 */
826 ASSERT(INT_GET(dqp->q_core.d_id, ARCH_CONVERT) == id);
827
828 if (flist_locked) {
829 if (dqp->q_nrefs != 0) {
830 xfs_qm_freelist_unlock(xfs_Gqm);
831 flist_locked = B_FALSE;
832 } else {
833 /*
834 * take it off the freelist
835 */
836 xfs_dqtrace_entry(dqp,
837 "DQLOOKUP: TAKEOFF FL");
838 XQM_FREELIST_REMOVE(dqp);
839 /* xfs_qm_freelist_print(&(xfs_Gqm->
840 qm_dqfreelist),
841 "after removal"); */
842 }
843 }
844
845 /*
846 * grab a reference
847 */
848 XFS_DQHOLD(dqp);
849
850 if (flist_locked)
851 xfs_qm_freelist_unlock(xfs_Gqm);
852 /*
853 * move the dquot to the front of the hashchain
854 */
855 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
856 if (dqp->HL_PREVP != &qh->qh_next) {
857 xfs_dqtrace_entry(dqp,
858 "DQLOOKUP: HASH MOVETOFRONT");
859 if ((d = dqp->HL_NEXT))
860 d->HL_PREVP = dqp->HL_PREVP;
861 *(dqp->HL_PREVP) = d;
862 d = qh->qh_next;
863 d->HL_PREVP = &dqp->HL_NEXT;
864 dqp->HL_NEXT = d;
865 dqp->HL_PREVP = &qh->qh_next;
866 qh->qh_next = dqp;
867 }
868 xfs_dqtrace_entry(dqp, "LOOKUP END");
869 *O_dqpp = dqp;
870 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
871 return (0);
872 }
873 }
874
875 *O_dqpp = NULL;
876 ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
877 return (1);
878}
879
880/*
881 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
882 * a locked dquot, doing an allocation (if requested) as needed.
883 * When both an inode and an id are given, the inode's id takes precedence.
884 * That is, if the id changes while we don't hold the ilock inside this
885 * function, the new dquot is returned, not necessarily the one requested
886 * in the id argument.
887 */
888int
889xfs_qm_dqget(
890 xfs_mount_t *mp,
891 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000892 xfs_dqid_t id, /* uid/projid/gid depending on type */
893 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
895 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
896{
897 xfs_dquot_t *dqp;
898 xfs_dqhash_t *h;
899 uint version;
900 int error;
901
902 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
903 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000904 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000906printk("XQM: ESRCH1\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return (ESRCH);
908 }
909 h = XFS_DQ_HASH(mp, id, type);
910
911#ifdef DEBUG
912 if (xfs_do_dqerror) {
913 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
914 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
915 cmn_err(CE_DEBUG, "Returning error in dqget");
916 return (EIO);
917 }
918 }
919#endif
920
921 again:
922
923#ifdef DEBUG
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000924 ASSERT(type == XFS_DQ_USER ||
925 type == XFS_DQ_PROJ ||
926 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (ip) {
928 ASSERT(XFS_ISLOCKED_INODE_EXCL(ip));
929 if (type == XFS_DQ_USER)
930 ASSERT(ip->i_udquot == NULL);
931 else
932 ASSERT(ip->i_gdquot == NULL);
933 }
934#endif
935 XFS_DQ_HASH_LOCK(h);
936
937 /*
938 * Look in the cache (hashtable).
939 * The chain is kept locked during lookup.
940 */
941 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
942 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
943 /*
944 * The dquot was found, moved to the front of the chain,
945 * taken off the freelist if it was on it, and locked
946 * at this point. Just unlock the hashchain and return.
947 */
948 ASSERT(*O_dqpp);
949 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
950 XFS_DQ_HASH_UNLOCK(h);
951 xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
952 return (0); /* success */
953 }
954 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
955
956 /*
957 * Dquot cache miss. We don't want to keep the inode lock across
958 * a (potential) disk read. Also we don't want to deal with the lock
959 * ordering between quotainode and this inode. OTOH, dropping the inode
960 * lock here means dealing with a chown that can happen before
961 * we re-acquire the lock.
962 */
963 if (ip)
964 xfs_iunlock(ip, XFS_ILOCK_EXCL);
965 /*
966 * Save the hashchain version stamp, and unlock the chain, so that
967 * we don't keep the lock across a disk read
968 */
969 version = h->qh_version;
970 XFS_DQ_HASH_UNLOCK(h);
971
972 /*
973 * Allocate the dquot on the kernel heap, and read the ondisk
974 * portion off the disk. Also, do all the necessary initialization
975 * This can return ENOENT if dquot didn't exist on disk and we didn't
976 * ask it to allocate; ESRCH if quotas got turned off suddenly.
977 */
978 if ((error = xfs_qm_idtodq(mp, id, type,
979 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
980 XFS_QMOPT_DOWARN),
981 &dqp))) {
982 if (ip)
983 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000984if (error == ESRCH) printk("XQM: ESRCH2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return (error);
986 }
987
988 /*
989 * See if this is mount code calling to look at the overall quota limits
990 * which are stored in the id == 0 user or group's dquot.
991 * Since we may not have done a quotacheck by this point, just return
992 * the dquot without attaching it to any hashtables, lists, etc, or even
993 * taking a reference.
994 * The caller must dqdestroy this once done.
995 */
996 if (flags & XFS_QMOPT_DQSUSER) {
997 ASSERT(id == 0);
998 ASSERT(! ip);
999 goto dqret;
1000 }
1001
1002 /*
1003 * Dquot lock comes after hashlock in the lock ordering
1004 */
1005 if (ip) {
1006 xfs_ilock(ip, XFS_ILOCK_EXCL);
1007 if (! XFS_IS_DQTYPE_ON(mp, type)) {
1008 /* inode stays locked on return */
1009 xfs_qm_dqdestroy(dqp);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001010printk("XQM: ESRCH3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return XFS_ERROR(ESRCH);
1012 }
1013 /*
1014 * A dquot could be attached to this inode by now, since
1015 * we had dropped the ilock.
1016 */
1017 if (type == XFS_DQ_USER) {
1018 if (ip->i_udquot) {
1019 xfs_qm_dqdestroy(dqp);
1020 dqp = ip->i_udquot;
1021 xfs_dqlock(dqp);
1022 goto dqret;
1023 }
1024 } else {
1025 if (ip->i_gdquot) {
1026 xfs_qm_dqdestroy(dqp);
1027 dqp = ip->i_gdquot;
1028 xfs_dqlock(dqp);
1029 goto dqret;
1030 }
1031 }
1032 }
1033
1034 /*
1035 * Hashlock comes after ilock in lock order
1036 */
1037 XFS_DQ_HASH_LOCK(h);
1038 if (version != h->qh_version) {
1039 xfs_dquot_t *tmpdqp;
1040 /*
1041 * Now, see if somebody else put the dquot in the
1042 * hashtable before us. This can happen because we didn't
1043 * keep the hashchain lock. We don't have to worry about
1044 * lock order between the two dquots here since dqp isn't
1045 * on any findable lists yet.
1046 */
1047 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1048 /*
1049 * Duplicate found. Just throw away the new dquot
1050 * and start over.
1051 */
1052 xfs_qm_dqput(tmpdqp);
1053 XFS_DQ_HASH_UNLOCK(h);
1054 xfs_qm_dqdestroy(dqp);
1055 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1056 goto again;
1057 }
1058 }
1059
1060 /*
1061 * Put the dquot at the beginning of the hash-chain and mp's list
1062 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1063 */
1064 ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
1065 dqp->q_hash = h;
1066 XQM_HASHLIST_INSERT(h, dqp);
1067
1068 /*
1069 * Attach this dquot to this filesystem's list of all dquots,
1070 * kept inside the mount structure in m_quotainfo field
1071 */
1072 xfs_qm_mplist_lock(mp);
1073
1074 /*
1075 * We return a locked dquot to the caller, with a reference taken
1076 */
1077 xfs_dqlock(dqp);
1078 dqp->q_nrefs = 1;
1079
1080 XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
1081
1082 xfs_qm_mplist_unlock(mp);
1083 XFS_DQ_HASH_UNLOCK(h);
1084 dqret:
1085 ASSERT((ip == NULL) || XFS_ISLOCKED_INODE_EXCL(ip));
1086 xfs_dqtrace_entry(dqp, "DQGET DONE");
1087 *O_dqpp = dqp;
1088 return (0);
1089}
1090
1091
1092/*
1093 * Release a reference to the dquot (decrement ref-count)
1094 * and unlock it. If there is a group quota attached to this
1095 * dquot, carefully release that too without tripping over
1096 * deadlocks'n'stuff.
1097 */
1098void
1099xfs_qm_dqput(
1100 xfs_dquot_t *dqp)
1101{
1102 xfs_dquot_t *gdqp;
1103
1104 ASSERT(dqp->q_nrefs > 0);
1105 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1106 xfs_dqtrace_entry(dqp, "DQPUT");
1107
1108 if (dqp->q_nrefs != 1) {
1109 dqp->q_nrefs--;
1110 xfs_dqunlock(dqp);
1111 return;
1112 }
1113
1114 /*
1115 * drop the dqlock and acquire the freelist and dqlock
1116 * in the right order; but try to get it out-of-order first
1117 */
1118 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
1119 xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
1120 xfs_dqunlock(dqp);
1121 xfs_qm_freelist_lock(xfs_Gqm);
1122 xfs_dqlock(dqp);
1123 }
1124
1125 while (1) {
1126 gdqp = NULL;
1127
1128 /* We can't depend on nrefs being == 1 here */
1129 if (--dqp->q_nrefs == 0) {
1130 xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
1131 /*
1132 * insert at end of the freelist.
1133 */
1134 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1135
1136 /*
1137 * If we just added a udquot to the freelist, then
1138 * we want to release the gdquot reference that
1139 * it (probably) has. Otherwise it'll keep the
1140 * gdquot from getting reclaimed.
1141 */
1142 if ((gdqp = dqp->q_gdquot)) {
1143 /*
1144 * Avoid a recursive dqput call
1145 */
1146 xfs_dqlock(gdqp);
1147 dqp->q_gdquot = NULL;
1148 }
1149
1150 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1151 "@@@@@++ Free list (after append) @@@@@+");
1152 */
1153 }
1154 xfs_dqunlock(dqp);
1155
1156 /*
1157 * If we had a group quota inside the user quota as a hint,
1158 * release it now.
1159 */
1160 if (! gdqp)
1161 break;
1162 dqp = gdqp;
1163 }
1164 xfs_qm_freelist_unlock(xfs_Gqm);
1165}
1166
1167/*
1168 * Release a dquot. Flush it if dirty, then dqput() it.
1169 * dquot must not be locked.
1170 */
1171void
1172xfs_qm_dqrele(
1173 xfs_dquot_t *dqp)
1174{
1175 ASSERT(dqp);
1176 xfs_dqtrace_entry(dqp, "DQRELE");
1177
1178 xfs_dqlock(dqp);
1179 /*
1180 * We don't care to flush it if the dquot is dirty here.
1181 * That will create stutters that we want to avoid.
1182 * Instead we do a delayed write when we try to reclaim
1183 * a dirty dquot. Also xfs_sync will take part of the burden...
1184 */
1185 xfs_qm_dqput(dqp);
1186}
1187
1188
1189/*
1190 * Write a modified dquot to disk.
1191 * The dquot must be locked and the flush lock too taken by caller.
1192 * The flush lock will not be unlocked until the dquot reaches the disk,
1193 * but the dquot is free to be unlocked and modified by the caller
1194 * in the interim. Dquot is still locked on return. This behavior is
1195 * identical to that of inodes.
1196 */
1197int
1198xfs_qm_dqflush(
1199 xfs_dquot_t *dqp,
1200 uint flags)
1201{
1202 xfs_mount_t *mp;
1203 xfs_buf_t *bp;
1204 xfs_disk_dquot_t *ddqp;
1205 int error;
1206 SPLDECL(s);
1207
1208 ASSERT(XFS_DQ_IS_LOCKED(dqp));
1209 ASSERT(XFS_DQ_IS_FLUSH_LOCKED(dqp));
1210 xfs_dqtrace_entry(dqp, "DQFLUSH");
1211
1212 /*
1213 * If not dirty, nada.
1214 */
1215 if (!XFS_DQ_IS_DIRTY(dqp)) {
1216 xfs_dqfunlock(dqp);
1217 return (0);
1218 }
1219
1220 /*
1221 * Cant flush a pinned dquot. Wait for it.
1222 */
1223 xfs_qm_dqunpin_wait(dqp);
1224
1225 /*
1226 * This may have been unpinned because the filesystem is shutting
1227 * down forcibly. If that's the case we must not write this dquot
1228 * to disk, because the log record didn't make it to disk!
1229 */
1230 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1231 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1232 xfs_dqfunlock(dqp);
1233 return XFS_ERROR(EIO);
1234 }
1235
1236 /*
1237 * Get the buffer containing the on-disk dquot
1238 * We don't need a transaction envelope because we know that the
1239 * the ondisk-dquot has already been allocated for.
1240 */
1241 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1242 xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
1243 ASSERT(error != ENOENT);
1244 /*
1245 * Quotas could have gotten turned off (ESRCH)
1246 */
1247 xfs_dqfunlock(dqp);
1248 return (error);
1249 }
1250
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001251 if (xfs_qm_dqcheck(&dqp->q_core, INT_GET(ddqp->d_id, ARCH_CONVERT),
1252 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 xfs_force_shutdown(dqp->q_mount, XFS_CORRUPT_INCORE);
1254 return XFS_ERROR(EIO);
1255 }
1256
1257 /* This is the only portion of data that needs to persist */
1258 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1259
1260 /*
1261 * Clear the dirty field and remember the flush lsn for later use.
1262 */
1263 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1264 mp = dqp->q_mount;
1265
1266 /* lsn is 64 bits */
1267 AIL_LOCK(mp, s);
1268 dqp->q_logitem.qli_flush_lsn = dqp->q_logitem.qli_item.li_lsn;
1269 AIL_UNLOCK(mp, s);
1270
1271 /*
1272 * Attach an iodone routine so that we can remove this dquot from the
1273 * AIL and release the flush lock once the dquot is synced to disk.
1274 */
1275 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1276 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1277 /*
1278 * If the buffer is pinned then push on the log so we won't
1279 * get stuck waiting in the write for too long.
1280 */
1281 if (XFS_BUF_ISPINNED(bp)) {
1282 xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
1283 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1284 }
1285
1286 if (flags & XFS_QMOPT_DELWRI) {
1287 xfs_bdwrite(mp, bp);
1288 } else if (flags & XFS_QMOPT_ASYNC) {
1289 xfs_bawrite(mp, bp);
1290 } else {
1291 error = xfs_bwrite(mp, bp);
1292 }
1293 xfs_dqtrace_entry(dqp, "DQFLUSH END");
1294 /*
1295 * dqp is still locked, but caller is free to unlock it now.
1296 */
1297 return (error);
1298
1299}
1300
1301/*
1302 * This is the dquot flushing I/O completion routine. It is called
1303 * from interrupt level when the buffer containing the dquot is
1304 * flushed to disk. It is responsible for removing the dquot logitem
1305 * from the AIL if it has not been re-logged, and unlocking the dquot's
1306 * flush lock. This behavior is very similar to that of inodes..
1307 */
1308/*ARGSUSED*/
1309STATIC void
1310xfs_qm_dqflush_done(
1311 xfs_buf_t *bp,
1312 xfs_dq_logitem_t *qip)
1313{
1314 xfs_dquot_t *dqp;
1315 SPLDECL(s);
1316
1317 dqp = qip->qli_dquot;
1318
1319 /*
1320 * We only want to pull the item from the AIL if its
1321 * location in the log has not changed since we started the flush.
1322 * Thus, we only bother if the dquot's lsn has
1323 * not changed. First we check the lsn outside the lock
1324 * since it's cheaper, and then we recheck while
1325 * holding the lock before removing the dquot from the AIL.
1326 */
1327 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1328 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1329
1330 AIL_LOCK(dqp->q_mount, s);
1331 /*
1332 * xfs_trans_delete_ail() drops the AIL lock.
1333 */
1334 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
1335 xfs_trans_delete_ail(dqp->q_mount,
1336 (xfs_log_item_t*)qip, s);
1337 else
1338 AIL_UNLOCK(dqp->q_mount, s);
1339 }
1340
1341 /*
1342 * Release the dq's flush lock since we're done with it.
1343 */
1344 xfs_dqfunlock(dqp);
1345}
1346
1347
1348int
1349xfs_qm_dqflock_nowait(
1350 xfs_dquot_t *dqp)
1351{
1352 int locked;
1353
1354 locked = cpsema(&((dqp)->q_flock));
1355
1356 /* XXX ifdef these out */
1357 if (locked)
1358 (dqp)->dq_flags |= XFS_DQ_FLOCKED;
1359 return (locked);
1360}
1361
1362
1363int
1364xfs_qm_dqlock_nowait(
1365 xfs_dquot_t *dqp)
1366{
1367 return (mutex_trylock(&((dqp)->q_qlock)));
1368}
1369
1370void
1371xfs_dqlock(
1372 xfs_dquot_t *dqp)
1373{
1374 mutex_lock(&(dqp->q_qlock), PINOD);
1375}
1376
1377void
1378xfs_dqunlock(
1379 xfs_dquot_t *dqp)
1380{
1381 mutex_unlock(&(dqp->q_qlock));
1382 if (dqp->q_logitem.qli_dquot == dqp) {
1383 /* Once was dqp->q_mount, but might just have been cleared */
1384 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_mountp,
1385 (xfs_log_item_t*)&(dqp->q_logitem));
1386 }
1387}
1388
1389
1390void
1391xfs_dqunlock_nonotify(
1392 xfs_dquot_t *dqp)
1393{
1394 mutex_unlock(&(dqp->q_qlock));
1395}
1396
1397void
1398xfs_dqlock2(
1399 xfs_dquot_t *d1,
1400 xfs_dquot_t *d2)
1401{
1402 if (d1 && d2) {
1403 ASSERT(d1 != d2);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001404 if (INT_GET(d1->q_core.d_id, ARCH_CONVERT) >
1405 INT_GET(d2->q_core.d_id, ARCH_CONVERT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 xfs_dqlock(d2);
1407 xfs_dqlock(d1);
1408 } else {
1409 xfs_dqlock(d1);
1410 xfs_dqlock(d2);
1411 }
1412 } else {
1413 if (d1) {
1414 xfs_dqlock(d1);
1415 } else if (d2) {
1416 xfs_dqlock(d2);
1417 }
1418 }
1419}
1420
1421
1422/*
1423 * Take a dquot out of the mount's dqlist as well as the hashlist.
1424 * This is called via unmount as well as quotaoff, and the purge
1425 * will always succeed unless there are soft (temp) references
1426 * outstanding.
1427 *
1428 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1429 * that we're returning! XXXsup - not cool.
1430 */
1431/* ARGSUSED */
1432int
1433xfs_qm_dqpurge(
1434 xfs_dquot_t *dqp,
1435 uint flags)
1436{
1437 xfs_dqhash_t *thishash;
1438 xfs_mount_t *mp;
1439
1440 mp = dqp->q_mount;
1441
1442 ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
1443 ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
1444
1445 xfs_dqlock(dqp);
1446 /*
1447 * We really can't afford to purge a dquot that is
1448 * referenced, because these are hard refs.
1449 * It shouldn't happen in general because we went thru _all_ inodes in
1450 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1451 * However it is possible that we have dquots with temporary
1452 * references that are not attached to an inode. e.g. see xfs_setattr().
1453 */
1454 if (dqp->q_nrefs != 0) {
1455 xfs_dqunlock(dqp);
1456 XFS_DQ_HASH_UNLOCK(dqp->q_hash);
1457 return (1);
1458 }
1459
1460 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1461
1462 /*
1463 * If we're turning off quotas, we have to make sure that, for
1464 * example, we don't delete quota disk blocks while dquots are
1465 * in the process of getting written to those disk blocks.
1466 * This dquot might well be on AIL, and we can't leave it there
1467 * if we're turning off quotas. Basically, we need this flush
1468 * lock, and are willing to block on it.
1469 */
1470 if (! xfs_qm_dqflock_nowait(dqp)) {
1471 /*
1472 * Block on the flush lock after nudging dquot buffer,
1473 * if it is incore.
1474 */
1475 xfs_qm_dqflock_pushbuf_wait(dqp);
1476 }
1477
1478 /*
1479 * XXXIf we're turning this type of quotas off, we don't care
1480 * about the dirty metadata sitting in this dquot. OTOH, if
1481 * we're unmounting, we do care, so we flush it and wait.
1482 */
1483 if (XFS_DQ_IS_DIRTY(dqp)) {
1484 xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
1485 /* dqflush unlocks dqflock */
1486 /*
1487 * Given that dqpurge is a very rare occurrence, it is OK
1488 * that we're holding the hashlist and mplist locks
1489 * across the disk write. But, ... XXXsup
1490 *
1491 * We don't care about getting disk errors here. We need
1492 * to purge this dquot anyway, so we go ahead regardless.
1493 */
1494 (void) xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
1495 xfs_dqflock(dqp);
1496 }
1497 ASSERT(dqp->q_pincount == 0);
1498 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1499 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1500
1501 thishash = dqp->q_hash;
1502 XQM_HASHLIST_REMOVE(thishash, dqp);
1503 XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
1504 /*
1505 * XXX Move this to the front of the freelist, if we can get the
1506 * freelist lock.
1507 */
1508 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1509
1510 dqp->q_mount = NULL;
1511 dqp->q_hash = NULL;
1512 dqp->dq_flags = XFS_DQ_INACTIVE;
1513 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1514 xfs_dqfunlock(dqp);
1515 xfs_dqunlock(dqp);
1516 XFS_DQ_HASH_UNLOCK(thishash);
1517 return (0);
1518}
1519
1520
1521#ifdef QUOTADEBUG
1522void
1523xfs_qm_dqprint(xfs_dquot_t *dqp)
1524{
1525 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1526 cmn_err(CE_DEBUG, "---- dquotID = %d",
1527 (int)INT_GET(dqp->q_core.d_id, ARCH_CONVERT));
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001528 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1530 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1531 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1532 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
1533 INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT),
1534 (int) INT_GET(dqp->q_core.d_blk_hardlimit, ARCH_CONVERT));
1535 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
1536 INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT),
1537 (int)INT_GET(dqp->q_core.d_blk_softlimit, ARCH_CONVERT));
1538 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
1539 INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT),
1540 (int)INT_GET(dqp->q_core.d_ino_hardlimit, ARCH_CONVERT));
1541 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
1542 INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT),
1543 (int)INT_GET(dqp->q_core.d_ino_softlimit, ARCH_CONVERT));
1544 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
1545 INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT),
1546 (int)INT_GET(dqp->q_core.d_bcount, ARCH_CONVERT));
1547 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
1548 INT_GET(dqp->q_core.d_icount, ARCH_CONVERT),
1549 (int)INT_GET(dqp->q_core.d_icount, ARCH_CONVERT));
1550 cmn_err(CE_DEBUG, "---- btimer = %d",
1551 (int)INT_GET(dqp->q_core.d_btimer, ARCH_CONVERT));
1552 cmn_err(CE_DEBUG, "---- itimer = %d",
1553 (int)INT_GET(dqp->q_core.d_itimer, ARCH_CONVERT));
1554 cmn_err(CE_DEBUG, "---------------------------");
1555}
1556#endif
1557
1558/*
1559 * Give the buffer a little push if it is incore and
1560 * wait on the flush lock.
1561 */
1562void
1563xfs_qm_dqflock_pushbuf_wait(
1564 xfs_dquot_t *dqp)
1565{
1566 xfs_buf_t *bp;
1567
1568 /*
1569 * Check to see if the dquot has been flushed delayed
1570 * write. If so, grab its buffer and send it
1571 * out immediately. We'll be able to acquire
1572 * the flush lock when the I/O completes.
1573 */
1574 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
1575 XFS_QI_DQCHUNKLEN(dqp->q_mount),
1576 XFS_INCORE_TRYLOCK);
1577 if (bp != NULL) {
1578 if (XFS_BUF_ISDELAYWRITE(bp)) {
1579 if (XFS_BUF_ISPINNED(bp)) {
1580 xfs_log_force(dqp->q_mount,
1581 (xfs_lsn_t)0,
1582 XFS_LOG_FORCE);
1583 }
1584 xfs_bawrite(dqp->q_mount, bp);
1585 } else {
1586 xfs_buf_relse(bp);
1587 }
1588 }
1589 xfs_dqflock(dqp);
1590}