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