blob: ad64ab62d9c566804a844d416af0160b7b5cacfb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_btree.h"
39#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_buf_item.h"
47#include "xfs_trans_space.h"
48#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
53/*
54 LOCK ORDER
55
56 inode lock (ilock)
57 dquot hash-chain lock (hashlock)
58 xqm dquot freelist lock (freelistlock
59 mount's dquot list lock (mplistlock)
60 user dquot lock - lock ordering among dquots is based on the uid or gid
61 group dquot lock - similar to udquots. Between the two dquots, the udquot
62 has to be locked first.
63 pin lock - the dquot lock must be held to take this lock.
64 flush lock - ditto.
65*/
66
67STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
68
69#ifdef DEBUG
70xfs_buftarg_t *xfs_dqerror_target;
71int xfs_do_dqerror;
72int xfs_dqreq_num;
73int xfs_dqerror_mod = 33;
74#endif
75
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010076static struct lock_class_key xfs_dquot_other_class;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * Allocate and initialize a dquot. We don't always allocate fresh memory;
80 * we try to reclaim a free dquot if the number of incore dquots are above
81 * a threshold.
82 * The only field inside the core that gets initialized at this point
83 * is the d_id field. The idea is to fill in the entire q_core
84 * when we read in the on disk dquot.
85 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100086STATIC xfs_dquot_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087xfs_qm_dqinit(
88 xfs_mount_t *mp,
89 xfs_dqid_t id,
90 uint type)
91{
92 xfs_dquot_t *dqp;
93 boolean_t brandnewdquot;
94
95 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
96 dqp->dq_flags = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +110097 dqp->q_core.d_id = cpu_to_be32(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dqp->q_mount = mp;
99
100 /*
101 * No need to re-initialize these if this is a reclaimed dquot.
102 */
103 if (brandnewdquot) {
104 dqp->dq_flnext = dqp->dq_flprev = dqp;
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800105 mutex_init(&dqp->q_qlock);
Peter Leckiebc3048e2008-10-30 17:05:04 +1100106 init_waitqueue_head(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
David Chinnere1f49cf2008-08-13 16:41:43 +1000108 /*
109 * Because we want to use a counting completion, complete
110 * the flush completion once to allow a single access to
111 * the flush completion without blocking.
112 */
113 init_completion(&dqp->q_flush);
114 complete(&dqp->q_flush);
115
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000116 trace_xfs_dqinit(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 } else {
118 /*
119 * Only the q_core portion was zeroed in dqreclaim_one().
120 * So, we need to reset others.
121 */
122 dqp->q_nrefs = 0;
123 dqp->q_blkno = 0;
Dave Chinner3a254042010-04-13 15:06:48 +1000124 INIT_LIST_HEAD(&dqp->q_mplist);
Dave Chinnere6a81f12010-04-13 15:06:51 +1000125 INIT_LIST_HEAD(&dqp->q_hashlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 dqp->q_bufoffset = 0;
127 dqp->q_fileoffset = 0;
128 dqp->q_transp = NULL;
129 dqp->q_gdquot = NULL;
130 dqp->q_res_bcount = 0;
131 dqp->q_res_icount = 0;
132 dqp->q_res_rtbcount = 0;
Peter Leckiebc3048e2008-10-30 17:05:04 +1100133 atomic_set(&dqp->q_pincount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dqp->q_hash = NULL;
135 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
136
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000137 trace_xfs_dqreuse(dqp);
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +0100138 }
139
140 /*
141 * In either case we need to make sure group quotas have a different
142 * lock class than user quotas, to make sure lockdep knows we can
143 * locks of one of each at the same time.
144 */
145 if (!(type & XFS_DQ_USER))
146 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /*
149 * log item gets initialized later
150 */
151 return (dqp);
152}
153
154/*
155 * This is called to free all the memory associated with a dquot
156 */
157void
158xfs_qm_dqdestroy(
159 xfs_dquot_t *dqp)
160{
161 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
162
163 mutex_destroy(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 sv_destroy(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 atomic_dec(&xfs_Gqm->qm_totaldquots);
168}
169
170/*
171 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
172 */
173STATIC void
174xfs_qm_dqinit_core(
175 xfs_dqid_t id,
176 uint type,
177 xfs_dqblk_t *d)
178{
179 /*
180 * Caller has zero'd the entire dquot 'chunk' already.
181 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100182 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
183 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
184 d->dd_diskdq.d_id = cpu_to_be32(id);
185 d->dd_diskdq.d_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * If default limits are in force, push them into the dquot now.
190 * We overwrite the dquot limits only if they are zero and this
191 * is not the root dquot.
192 */
193void
194xfs_qm_adjust_dqlimits(
195 xfs_mount_t *mp,
196 xfs_disk_dquot_t *d)
197{
198 xfs_quotainfo_t *q = mp->m_quotainfo;
199
200 ASSERT(d->d_id);
201
202 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100203 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100205 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (q->qi_isoftlimit && !d->d_ino_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100207 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100209 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100211 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100213 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
217 * Check the limits and timers of a dquot and start or reset timers
218 * if necessary.
219 * This gets called even when quota enforcement is OFF, which makes our
220 * life a little less complicated. (We just don't reject any quota
221 * reservations in that case, when enforcement is off).
222 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
223 * enforcement's off.
224 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000225 * 'automatically' get started when limits get exceeded. They do
226 * get reset to zero, however, when we find the count to be under
227 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229void
230xfs_qm_adjust_dqtimers(
231 xfs_mount_t *mp,
232 xfs_disk_dquot_t *d)
233{
234 ASSERT(d->d_id);
235
236#ifdef QUOTADEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100237 if (d->d_blk_hardlimit)
238 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
239 be64_to_cpu(d->d_blk_hardlimit));
240 if (d->d_ino_hardlimit)
241 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
242 be64_to_cpu(d->d_ino_hardlimit));
243 if (d->d_rtb_hardlimit)
244 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
245 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100248 if ((d->d_blk_softlimit &&
249 (be64_to_cpu(d->d_bcount) >=
250 be64_to_cpu(d->d_blk_softlimit))) ||
251 (d->d_blk_hardlimit &&
252 (be64_to_cpu(d->d_bcount) >=
253 be64_to_cpu(d->d_blk_hardlimit)))) {
254 d->d_btimer = cpu_to_be32(get_seconds() +
255 XFS_QI_BTIMELIMIT(mp));
Nathan Scott754002b2005-06-21 15:49:06 +1000256 } else {
257 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 } else {
260 if ((!d->d_blk_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100261 (be64_to_cpu(d->d_bcount) <
262 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 (!d->d_blk_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100264 (be64_to_cpu(d->d_bcount) <
265 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 d->d_btimer = 0;
267 }
268 }
269
270 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100271 if ((d->d_ino_softlimit &&
272 (be64_to_cpu(d->d_icount) >=
273 be64_to_cpu(d->d_ino_softlimit))) ||
274 (d->d_ino_hardlimit &&
275 (be64_to_cpu(d->d_icount) >=
276 be64_to_cpu(d->d_ino_hardlimit)))) {
277 d->d_itimer = cpu_to_be32(get_seconds() +
278 XFS_QI_ITIMELIMIT(mp));
Nathan Scott754002b2005-06-21 15:49:06 +1000279 } else {
280 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 } else {
283 if ((!d->d_ino_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100284 (be64_to_cpu(d->d_icount) <
285 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 (!d->d_ino_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100287 (be64_to_cpu(d->d_icount) <
288 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 d->d_itimer = 0;
290 }
291 }
292
293 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100294 if ((d->d_rtb_softlimit &&
295 (be64_to_cpu(d->d_rtbcount) >=
296 be64_to_cpu(d->d_rtb_softlimit))) ||
297 (d->d_rtb_hardlimit &&
298 (be64_to_cpu(d->d_rtbcount) >=
299 be64_to_cpu(d->d_rtb_hardlimit)))) {
300 d->d_rtbtimer = cpu_to_be32(get_seconds() +
301 XFS_QI_RTBTIMELIMIT(mp));
Nathan Scott754002b2005-06-21 15:49:06 +1000302 } else {
303 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 } else {
306 if ((!d->d_rtb_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100307 (be64_to_cpu(d->d_rtbcount) <
308 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 (!d->d_rtb_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100310 (be64_to_cpu(d->d_rtbcount) <
311 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 d->d_rtbtimer = 0;
313 }
314 }
315}
316
317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * initialize a buffer full of dquots and log the whole thing
319 */
320STATIC void
321xfs_qm_init_dquot_blk(
322 xfs_trans_t *tp,
323 xfs_mount_t *mp,
324 xfs_dqid_t id,
325 uint type,
326 xfs_buf_t *bp)
327{
328 xfs_dqblk_t *d;
329 int curid, i;
330
331 ASSERT(tp);
332 ASSERT(XFS_BUF_ISBUSY(bp));
333 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
334
335 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
336
337 /*
338 * ID of the first dquot in the block - id's are zero based.
339 */
340 curid = id - (id % XFS_QM_DQPERBLK(mp));
341 ASSERT(curid >= 0);
342 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
343 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
344 xfs_qm_dqinit_core(curid, type, d);
345 xfs_trans_dquot_buf(tp, bp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000346 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
347 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
348 XFS_BLI_GDQUOT_BUF)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
350}
351
352
353
354/*
355 * Allocate a block and fill it with dquots.
356 * This is called when the bmapi finds a hole.
357 */
358STATIC int
359xfs_qm_dqalloc(
Tim Shimminefa092f2005-09-05 08:29:01 +1000360 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xfs_mount_t *mp,
362 xfs_dquot_t *dqp,
363 xfs_inode_t *quotip,
364 xfs_fileoff_t offset_fsb,
365 xfs_buf_t **O_bpp)
366{
367 xfs_fsblock_t firstblock;
368 xfs_bmap_free_t flist;
369 xfs_bmbt_irec_t map;
370 int nmaps, error, committed;
371 xfs_buf_t *bp;
Tim Shimminefa092f2005-09-05 08:29:01 +1000372 xfs_trans_t *tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 ASSERT(tp != NULL);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000375
376 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /*
379 * Initialize the bmap freelist prior to calling bmapi code.
380 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600381 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 xfs_ilock(quotip, XFS_ILOCK_EXCL);
383 /*
384 * Return if this type of quotas is turned off while we didn't
385 * have an inode lock
386 */
387 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
388 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
389 return (ESRCH);
390 }
391
392 /*
393 * xfs_trans_commit normally decrements the vnode ref count
394 * when it unlocks the inode. Since we want to keep the quota
395 * inode around, we bump the vnode ref count now.
396 */
Christoph Hellwig0b1f91772008-08-13 16:13:09 +1000397 IHOLD(quotip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
400 nmaps = 1;
401 if ((error = xfs_bmapi(tp, quotip,
402 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
403 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
404 &firstblock,
405 XFS_QM_DQALLOC_SPACE_RES(mp),
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000406 &map, &nmaps, &flist, NULL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto error0;
408 }
409 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
410 ASSERT(nmaps == 1);
411 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
412 (map.br_startblock != HOLESTARTBLOCK));
413
414 /*
415 * Keep track of the blkno to save a lookup later
416 */
417 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
418
419 /* now we can just get the buffer (there's nothing to read yet) */
420 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
421 dqp->q_blkno,
422 XFS_QI_DQCHUNKLEN(mp),
423 0);
424 if (!bp || (error = XFS_BUF_GETERROR(bp)))
425 goto error1;
426 /*
427 * Make a chunk of dquots out of this buffer and log
428 * the entire thing.
429 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100430 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000431 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Tim Shimminefa092f2005-09-05 08:29:01 +1000433 /*
434 * xfs_bmap_finish() may commit the current transaction and
435 * start a second transaction if the freelist is not empty.
436 *
437 * Since we still want to modify this buffer, we need to
438 * ensure that the buffer is not released on commit of
439 * the first transaction and ensure the buffer is added to the
440 * second transaction.
441 *
442 * If there is only one transaction then don't stop the buffer
443 * from being released when it commits later on.
444 */
445
446 xfs_trans_bhold(tp, bp);
447
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100448 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto error1;
450 }
451
Tim Shimminefa092f2005-09-05 08:29:01 +1000452 if (committed) {
453 tp = *tpp;
454 xfs_trans_bjoin(tp, bp);
455 } else {
456 xfs_trans_bhold_release(tp, bp);
457 }
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *O_bpp = bp;
460 return 0;
461
462 error1:
463 xfs_bmap_cancel(&flist);
464 error0:
465 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
466
467 return (error);
468}
469
470/*
471 * Maps a dquot to the buffer containing its on-disk version.
472 * This returns a ptr to the buffer containing the on-disk dquot
473 * in the bpp param, and a ptr to the on-disk dquot within that buffer
474 */
475STATIC int
476xfs_qm_dqtobp(
Tim Shimminefa092f2005-09-05 08:29:01 +1000477 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 xfs_dquot_t *dqp,
479 xfs_disk_dquot_t **O_ddpp,
480 xfs_buf_t **O_bpp,
481 uint flags)
482{
483 xfs_bmbt_irec_t map;
484 int nmaps, error;
485 xfs_buf_t *bp;
486 xfs_inode_t *quotip;
487 xfs_mount_t *mp;
488 xfs_disk_dquot_t *ddq;
489 xfs_dqid_t id;
490 boolean_t newdquot;
Tim Shimminefa092f2005-09-05 08:29:01 +1000491 xfs_trans_t *tp = (tpp ? *tpp : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 mp = dqp->q_mount;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100494 id = be32_to_cpu(dqp->q_core.d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 nmaps = 1;
496 newdquot = B_FALSE;
497
498 /*
499 * If we don't know where the dquot lives, find out.
500 */
501 if (dqp->q_blkno == (xfs_daddr_t) 0) {
502 /* We use the id as an index */
Nathan Scottc310ab62005-11-02 10:31:41 +1100503 dqp->q_fileoffset = (xfs_fileoff_t)id / XFS_QM_DQPERBLK(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 nmaps = 1;
505 quotip = XFS_DQ_TO_QIP(dqp);
506 xfs_ilock(quotip, XFS_ILOCK_SHARED);
507 /*
508 * Return if this type of quotas is turned off while we didn't
509 * have an inode lock
510 */
511 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
512 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
513 return (ESRCH);
514 }
515 /*
516 * Find the block map; no allocations yet
517 */
518 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
519 XFS_DQUOT_CLUSTER_SIZE_FSB,
520 XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000521 NULL, 0, &map, &nmaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
524 if (error)
525 return (error);
526 ASSERT(nmaps == 1);
527 ASSERT(map.br_blockcount == 1);
528
529 /*
530 * offset of dquot in the (fixed sized) dquot chunk.
531 */
532 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
533 sizeof(xfs_dqblk_t);
534 if (map.br_startblock == HOLESTARTBLOCK) {
535 /*
536 * We don't allocate unless we're asked to
537 */
538 if (!(flags & XFS_QMOPT_DQALLOC))
539 return (ENOENT);
540
541 ASSERT(tp);
Tim Shimminefa092f2005-09-05 08:29:01 +1000542 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dqp->q_fileoffset, &bp)))
544 return (error);
Tim Shimminefa092f2005-09-05 08:29:01 +1000545 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 newdquot = B_TRUE;
547 } else {
548 /*
549 * store the blkno etc so that we don't have to do the
550 * mapping all the time
551 */
552 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
553 }
554 }
555 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
556 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
557
558 /*
559 * Read in the buffer, unless we've just done the allocation
560 * (in which case we already have the buf).
561 */
562 if (! newdquot) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000563 trace_xfs_dqtobp_read(dqp);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
566 dqp->q_blkno,
567 XFS_QI_DQCHUNKLEN(mp),
568 0, &bp))) {
569 return (error);
570 }
571 if (error || !bp)
572 return XFS_ERROR(error);
573 }
574 ASSERT(XFS_BUF_ISBUSY(bp));
575 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
576
577 /*
578 * calculate the location of the dquot inside the buffer.
579 */
580 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
581
582 /*
583 * A simple sanity check in case we got a corrupted dquot...
584 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000585 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
587 "dqtobp")) {
588 if (!(flags & XFS_QMOPT_DQREPAIR)) {
589 xfs_trans_brelse(tp, bp);
590 return XFS_ERROR(EIO);
591 }
592 XFS_BUF_BUSY(bp); /* We dirtied this */
593 }
594
595 *O_bpp = bp;
596 *O_ddpp = ddq;
597
598 return (0);
599}
600
601
602/*
603 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
604 * and release the buffer immediately.
605 *
606 */
607/* ARGSUSED */
608STATIC int
609xfs_qm_dqread(
Tim Shimminefa092f2005-09-05 08:29:01 +1000610 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfs_dqid_t id,
612 xfs_dquot_t *dqp, /* dquot to get filled in */
613 uint flags)
614{
615 xfs_disk_dquot_t *ddqp;
616 xfs_buf_t *bp;
617 int error;
Tim Shimminefa092f2005-09-05 08:29:01 +1000618 xfs_trans_t *tp;
619
620 ASSERT(tpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000622 trace_xfs_dqread(dqp);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * get a pointer to the on-disk dquot and the buffer containing it
626 * dqp already knows its own type (GROUP/USER).
627 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000628 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return (error);
630 }
Tim Shimminefa092f2005-09-05 08:29:01 +1000631 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* copy everything from disk dquot to the incore dquot */
634 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
Christoph Hellwig1149d962005-11-02 15:01:12 +1100635 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 xfs_qm_dquot_logitem_init(dqp);
637
638 /*
639 * Reservation counters are defined as reservation plus current usage
640 * to avoid having to add everytime.
641 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100642 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
643 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
644 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* Mark the buf so that this will stay incore a little longer */
647 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
648
649 /*
650 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
651 * So we need to release with xfs_trans_brelse().
652 * The strategy here is identical to that of inodes; we lock
653 * the dquot in xfs_qm_dqget() before making it accessible to
654 * others. This is because dquots, like inodes, need a good level of
655 * concurrency, and we don't want to take locks on the entire buffers
656 * for dquot accesses.
657 * Note also that the dquot buffer may even be dirty at this point, if
658 * this particular dquot was repaired. We still aren't afraid to
659 * brelse it because we have the changes incore.
660 */
661 ASSERT(XFS_BUF_ISBUSY(bp));
662 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
663 xfs_trans_brelse(tp, bp);
664
665 return (error);
666}
667
668
669/*
670 * allocate an incore dquot from the kernel heap,
671 * and fill its core with quota information kept on disk.
672 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
673 * if it wasn't already allocated.
674 */
675STATIC int
676xfs_qm_idtodq(
677 xfs_mount_t *mp,
678 xfs_dqid_t id, /* gid or uid, depending on type */
679 uint type, /* UDQUOT or GDQUOT */
680 uint flags, /* DQALLOC, DQREPAIR */
681 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
682{
683 xfs_dquot_t *dqp;
684 int error;
685 xfs_trans_t *tp;
686 int cancelflags=0;
687
688 dqp = xfs_qm_dqinit(mp, id, type);
689 tp = NULL;
690 if (flags & XFS_QMOPT_DQALLOC) {
691 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
692 if ((error = xfs_trans_reserve(tp,
693 XFS_QM_DQALLOC_SPACE_RES(mp),
694 XFS_WRITE_LOG_RES(mp) +
695 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
696 128,
697 0,
698 XFS_TRANS_PERM_LOG_RES,
699 XFS_WRITE_LOG_COUNT))) {
700 cancelflags = 0;
701 goto error0;
702 }
703 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
704 }
705
706 /*
707 * Read it from disk; xfs_dqread() takes care of
708 * all the necessary initialization of dquot's fields (locks, etc)
709 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000710 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * This can happen if quotas got turned off (ESRCH),
713 * or if the dquot didn't exist on disk and we ask to
714 * allocate (ENOENT).
715 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000716 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cancelflags |= XFS_TRANS_ABORT;
718 goto error0;
719 }
720 if (tp) {
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000721 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto error1;
723 }
724
725 *O_dqpp = dqp;
726 return (0);
727
728 error0:
729 ASSERT(error);
730 if (tp)
731 xfs_trans_cancel(tp, cancelflags);
732 error1:
733 xfs_qm_dqdestroy(dqp);
734 *O_dqpp = NULL;
735 return (error);
736}
737
738/*
739 * Lookup a dquot in the incore dquot hashtable. We keep two separate
740 * hashtables for user and group dquots; and, these are global tables
741 * inside the XQM, not per-filesystem tables.
742 * The hash chain must be locked by caller, and it is left locked
743 * on return. Returning dquot is locked.
744 */
745STATIC int
746xfs_qm_dqlookup(
747 xfs_mount_t *mp,
748 xfs_dqid_t id,
749 xfs_dqhash_t *qh,
750 xfs_dquot_t **O_dqpp)
751{
752 xfs_dquot_t *dqp;
753 uint flist_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100755 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 flist_locked = B_FALSE;
758
759 /*
760 * Traverse the hashchain looking for a match
761 */
Dave Chinnere6a81f12010-04-13 15:06:51 +1000762 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * We already have the hashlock. We don't need the
765 * dqlock to look at the id field of the dquot, since the
766 * id can't be modified without the hashlock anyway.
767 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100768 if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000769 trace_xfs_dqlookup_found(dqp);
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
772 * All in core dquots must be on the dqlist of mp
773 */
Dave Chinner3a254042010-04-13 15:06:48 +1000774 ASSERT(!list_empty(&dqp->q_mplist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 xfs_dqlock(dqp);
777 if (dqp->q_nrefs == 0) {
778 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
779 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000780 trace_xfs_dqlookup_want(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
783 * We may have raced with dqreclaim_one()
784 * (and lost). So, flag that we don't
785 * want the dquot to be reclaimed.
786 */
787 dqp->dq_flags |= XFS_DQ_WANT;
788 xfs_dqunlock(dqp);
789 xfs_qm_freelist_lock(xfs_Gqm);
790 xfs_dqlock(dqp);
791 dqp->dq_flags &= ~(XFS_DQ_WANT);
792 }
793 flist_locked = B_TRUE;
794 }
795
796 /*
797 * id couldn't have changed; we had the hashlock all
798 * along
799 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100800 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (flist_locked) {
803 if (dqp->q_nrefs != 0) {
804 xfs_qm_freelist_unlock(xfs_Gqm);
805 flist_locked = B_FALSE;
806 } else {
807 /*
808 * take it off the freelist
809 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000810 trace_xfs_dqlookup_freelist(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 XQM_FREELIST_REMOVE(dqp);
812 /* xfs_qm_freelist_print(&(xfs_Gqm->
813 qm_dqfreelist),
814 "after removal"); */
815 }
816 }
817
818 /*
819 * grab a reference
820 */
821 XFS_DQHOLD(dqp);
822
823 if (flist_locked)
824 xfs_qm_freelist_unlock(xfs_Gqm);
825 /*
826 * move the dquot to the front of the hashchain
827 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100828 ASSERT(mutex_is_locked(&qh->qh_lock));
Dave Chinnere6a81f12010-04-13 15:06:51 +1000829 list_move(&dqp->q_hashlist, &qh->qh_list);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000830 trace_xfs_dqlookup_done(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *O_dqpp = dqp;
Dave Chinnere6a81f12010-04-13 15:06:51 +1000832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 }
835
836 *O_dqpp = NULL;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100837 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return (1);
839}
840
841/*
842 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
843 * a locked dquot, doing an allocation (if requested) as needed.
844 * When both an inode and an id are given, the inode's id takes precedence.
845 * That is, if the id changes while we don't hold the ilock inside this
846 * function, the new dquot is returned, not necessarily the one requested
847 * in the id argument.
848 */
849int
850xfs_qm_dqget(
851 xfs_mount_t *mp,
852 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000853 xfs_dqid_t id, /* uid/projid/gid depending on type */
854 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
856 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
857{
858 xfs_dquot_t *dqp;
859 xfs_dqhash_t *h;
860 uint version;
861 int error;
862
863 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
864 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000865 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
867 return (ESRCH);
868 }
869 h = XFS_DQ_HASH(mp, id, type);
870
871#ifdef DEBUG
872 if (xfs_do_dqerror) {
873 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
874 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
875 cmn_err(CE_DEBUG, "Returning error in dqget");
876 return (EIO);
877 }
878 }
879#endif
880
881 again:
882
883#ifdef DEBUG
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000884 ASSERT(type == XFS_DQ_USER ||
885 type == XFS_DQ_PROJ ||
886 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (ip) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000888 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (type == XFS_DQ_USER)
890 ASSERT(ip->i_udquot == NULL);
891 else
892 ASSERT(ip->i_gdquot == NULL);
893 }
894#endif
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100895 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /*
898 * Look in the cache (hashtable).
899 * The chain is kept locked during lookup.
900 */
901 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
902 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
903 /*
904 * The dquot was found, moved to the front of the chain,
905 * taken off the freelist if it was on it, and locked
906 * at this point. Just unlock the hashchain and return.
907 */
908 ASSERT(*O_dqpp);
909 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100910 mutex_unlock(&h->qh_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000911 trace_xfs_dqget_hit(*O_dqpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return (0); /* success */
913 }
914 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
915
916 /*
917 * Dquot cache miss. We don't want to keep the inode lock across
918 * a (potential) disk read. Also we don't want to deal with the lock
919 * ordering between quotainode and this inode. OTOH, dropping the inode
920 * lock here means dealing with a chown that can happen before
921 * we re-acquire the lock.
922 */
923 if (ip)
924 xfs_iunlock(ip, XFS_ILOCK_EXCL);
925 /*
926 * Save the hashchain version stamp, and unlock the chain, so that
927 * we don't keep the lock across a disk read
928 */
929 version = h->qh_version;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100930 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /*
933 * Allocate the dquot on the kernel heap, and read the ondisk
934 * portion off the disk. Also, do all the necessary initialization
935 * This can return ENOENT if dquot didn't exist on disk and we didn't
936 * ask it to allocate; ESRCH if quotas got turned off suddenly.
937 */
938 if ((error = xfs_qm_idtodq(mp, id, type,
939 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
940 XFS_QMOPT_DOWARN),
941 &dqp))) {
942 if (ip)
943 xfs_ilock(ip, XFS_ILOCK_EXCL);
944 return (error);
945 }
946
947 /*
948 * See if this is mount code calling to look at the overall quota limits
949 * which are stored in the id == 0 user or group's dquot.
950 * Since we may not have done a quotacheck by this point, just return
951 * the dquot without attaching it to any hashtables, lists, etc, or even
952 * taking a reference.
953 * The caller must dqdestroy this once done.
954 */
955 if (flags & XFS_QMOPT_DQSUSER) {
956 ASSERT(id == 0);
957 ASSERT(! ip);
958 goto dqret;
959 }
960
961 /*
962 * Dquot lock comes after hashlock in the lock ordering
963 */
964 if (ip) {
965 xfs_ilock(ip, XFS_ILOCK_EXCL);
966 if (! XFS_IS_DQTYPE_ON(mp, type)) {
967 /* inode stays locked on return */
968 xfs_qm_dqdestroy(dqp);
969 return XFS_ERROR(ESRCH);
970 }
971 /*
972 * A dquot could be attached to this inode by now, since
973 * we had dropped the ilock.
974 */
975 if (type == XFS_DQ_USER) {
976 if (ip->i_udquot) {
977 xfs_qm_dqdestroy(dqp);
978 dqp = ip->i_udquot;
979 xfs_dqlock(dqp);
980 goto dqret;
981 }
982 } else {
983 if (ip->i_gdquot) {
984 xfs_qm_dqdestroy(dqp);
985 dqp = ip->i_gdquot;
986 xfs_dqlock(dqp);
987 goto dqret;
988 }
989 }
990 }
991
992 /*
993 * Hashlock comes after ilock in lock order
994 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100995 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (version != h->qh_version) {
997 xfs_dquot_t *tmpdqp;
998 /*
999 * Now, see if somebody else put the dquot in the
1000 * hashtable before us. This can happen because we didn't
1001 * keep the hashchain lock. We don't have to worry about
1002 * lock order between the two dquots here since dqp isn't
1003 * on any findable lists yet.
1004 */
1005 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1006 /*
1007 * Duplicate found. Just throw away the new dquot
1008 * and start over.
1009 */
1010 xfs_qm_dqput(tmpdqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001011 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 xfs_qm_dqdestroy(dqp);
1013 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1014 goto again;
1015 }
1016 }
1017
1018 /*
1019 * Put the dquot at the beginning of the hash-chain and mp's list
1020 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1021 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001022 ASSERT(mutex_is_locked(&h->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 dqp->q_hash = h;
Dave Chinnere6a81f12010-04-13 15:06:51 +10001024 list_add(&dqp->q_hashlist, &h->qh_list);
1025 h->qh_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /*
1028 * Attach this dquot to this filesystem's list of all dquots,
1029 * kept inside the mount structure in m_quotainfo field
1030 */
Dave Chinner3a254042010-04-13 15:06:48 +10001031 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /*
1034 * We return a locked dquot to the caller, with a reference taken
1035 */
1036 xfs_dqlock(dqp);
1037 dqp->q_nrefs = 1;
1038
Dave Chinner3a254042010-04-13 15:06:48 +10001039 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
1040 mp->m_quotainfo->qi_dquots++;
1041 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001042 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 dqret:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001044 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001045 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *O_dqpp = dqp;
1047 return (0);
1048}
1049
1050
1051/*
1052 * Release a reference to the dquot (decrement ref-count)
1053 * and unlock it. If there is a group quota attached to this
1054 * dquot, carefully release that too without tripping over
1055 * deadlocks'n'stuff.
1056 */
1057void
1058xfs_qm_dqput(
1059 xfs_dquot_t *dqp)
1060{
1061 xfs_dquot_t *gdqp;
1062
1063 ASSERT(dqp->q_nrefs > 0);
1064 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001065
1066 trace_xfs_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (dqp->q_nrefs != 1) {
1069 dqp->q_nrefs--;
1070 xfs_dqunlock(dqp);
1071 return;
1072 }
1073
1074 /*
1075 * drop the dqlock and acquire the freelist and dqlock
1076 * in the right order; but try to get it out-of-order first
1077 */
1078 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001079 trace_xfs_dqput_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 xfs_dqunlock(dqp);
1081 xfs_qm_freelist_lock(xfs_Gqm);
1082 xfs_dqlock(dqp);
1083 }
1084
1085 while (1) {
1086 gdqp = NULL;
1087
1088 /* We can't depend on nrefs being == 1 here */
1089 if (--dqp->q_nrefs == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001090 trace_xfs_dqput_free(dqp);
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * insert at end of the freelist.
1094 */
1095 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1096
1097 /*
1098 * If we just added a udquot to the freelist, then
1099 * we want to release the gdquot reference that
1100 * it (probably) has. Otherwise it'll keep the
1101 * gdquot from getting reclaimed.
1102 */
1103 if ((gdqp = dqp->q_gdquot)) {
1104 /*
1105 * Avoid a recursive dqput call
1106 */
1107 xfs_dqlock(gdqp);
1108 dqp->q_gdquot = NULL;
1109 }
1110
1111 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1112 "@@@@@++ Free list (after append) @@@@@+");
1113 */
1114 }
1115 xfs_dqunlock(dqp);
1116
1117 /*
1118 * If we had a group quota inside the user quota as a hint,
1119 * release it now.
1120 */
1121 if (! gdqp)
1122 break;
1123 dqp = gdqp;
1124 }
1125 xfs_qm_freelist_unlock(xfs_Gqm);
1126}
1127
1128/*
1129 * Release a dquot. Flush it if dirty, then dqput() it.
1130 * dquot must not be locked.
1131 */
1132void
1133xfs_qm_dqrele(
1134 xfs_dquot_t *dqp)
1135{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001136 if (!dqp)
1137 return;
1138
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001139 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 xfs_dqlock(dqp);
1142 /*
1143 * We don't care to flush it if the dquot is dirty here.
1144 * That will create stutters that we want to avoid.
1145 * Instead we do a delayed write when we try to reclaim
1146 * a dirty dquot. Also xfs_sync will take part of the burden...
1147 */
1148 xfs_qm_dqput(dqp);
1149}
1150
1151
1152/*
1153 * Write a modified dquot to disk.
1154 * The dquot must be locked and the flush lock too taken by caller.
1155 * The flush lock will not be unlocked until the dquot reaches the disk,
1156 * but the dquot is free to be unlocked and modified by the caller
1157 * in the interim. Dquot is still locked on return. This behavior is
1158 * identical to that of inodes.
1159 */
1160int
1161xfs_qm_dqflush(
1162 xfs_dquot_t *dqp,
1163 uint flags)
1164{
1165 xfs_mount_t *mp;
1166 xfs_buf_t *bp;
1167 xfs_disk_dquot_t *ddqp;
1168 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001171 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001172 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
David Chinner2f8a3ce2008-10-30 17:07:20 +11001175 * If not dirty, or it's pinned and we are not supposed to
1176 * block, nada.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
David Chinner2f8a3ce2008-10-30 17:07:20 +11001178 if (!XFS_DQ_IS_DIRTY(dqp) ||
Dave Chinner20026d92010-02-04 09:48:58 +11001179 (!(flags & SYNC_WAIT) && atomic_read(&dqp->q_pincount) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 xfs_dqfunlock(dqp);
David Chinner2f8a3ce2008-10-30 17:07:20 +11001181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 xfs_qm_dqunpin_wait(dqp);
1184
1185 /*
1186 * This may have been unpinned because the filesystem is shutting
1187 * down forcibly. If that's the case we must not write this dquot
1188 * to disk, because the log record didn't make it to disk!
1189 */
1190 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1191 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1192 xfs_dqfunlock(dqp);
1193 return XFS_ERROR(EIO);
1194 }
1195
1196 /*
1197 * Get the buffer containing the on-disk dquot
1198 * We don't need a transaction envelope because we know that the
1199 * the ondisk-dquot has already been allocated for.
1200 */
1201 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ASSERT(error != ENOENT);
1203 /*
1204 * Quotas could have gotten turned off (ESRCH)
1205 */
1206 xfs_dqfunlock(dqp);
1207 return (error);
1208 }
1209
Christoph Hellwig1149d962005-11-02 15:01:12 +11001210 if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001211 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001212 xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return XFS_ERROR(EIO);
1214 }
1215
1216 /* This is the only portion of data that needs to persist */
1217 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1218
1219 /*
1220 * Clear the dirty field and remember the flush lsn for later use.
1221 */
1222 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1223 mp = dqp->q_mount;
1224
David Chinner7b2e2a32008-10-30 17:39:12 +11001225 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1226 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Attach an iodone routine so that we can remove this dquot from the
1230 * AIL and release the flush lock once the dquot is synced to disk.
1231 */
1232 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1233 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1234 /*
1235 * If the buffer is pinned then push on the log so we won't
1236 * get stuck waiting in the write for too long.
1237 */
1238 if (XFS_BUF_ISPINNED(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001239 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001240 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Dave Chinner20026d92010-02-04 09:48:58 +11001243 if (flags & SYNC_WAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 error = xfs_bwrite(mp, bp);
Dave Chinner20026d92010-02-04 09:48:58 +11001245 else
1246 xfs_bdwrite(mp, bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001247
1248 trace_xfs_dqflush_done(dqp);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * dqp is still locked, but caller is free to unlock it now.
1252 */
Dave Chinner20026d92010-02-04 09:48:58 +11001253 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255}
1256
1257/*
1258 * This is the dquot flushing I/O completion routine. It is called
1259 * from interrupt level when the buffer containing the dquot is
1260 * flushed to disk. It is responsible for removing the dquot logitem
1261 * from the AIL if it has not been re-logged, and unlocking the dquot's
1262 * flush lock. This behavior is very similar to that of inodes..
1263 */
1264/*ARGSUSED*/
1265STATIC void
1266xfs_qm_dqflush_done(
1267 xfs_buf_t *bp,
1268 xfs_dq_logitem_t *qip)
1269{
1270 xfs_dquot_t *dqp;
David Chinner783a2f62008-10-30 17:39:58 +11001271 struct xfs_ail *ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 dqp = qip->qli_dquot;
David Chinner783a2f62008-10-30 17:39:58 +11001274 ailp = qip->qli_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /*
1277 * We only want to pull the item from the AIL if its
1278 * location in the log has not changed since we started the flush.
1279 * Thus, we only bother if the dquot's lsn has
1280 * not changed. First we check the lsn outside the lock
1281 * since it's cheaper, and then we recheck while
1282 * holding the lock before removing the dquot from the AIL.
1283 */
1284 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1285 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1286
David Chinner783a2f62008-10-30 17:39:58 +11001287 /* xfs_trans_ail_delete() drops the AIL lock. */
1288 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
David Chinner783a2f62008-10-30 17:39:58 +11001290 xfs_trans_ail_delete(ailp, (xfs_log_item_t*)qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 else
David Chinner783a2f62008-10-30 17:39:58 +11001292 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295 /*
1296 * Release the dq's flush lock since we're done with it.
1297 */
1298 xfs_dqfunlock(dqp);
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301int
1302xfs_qm_dqlock_nowait(
1303 xfs_dquot_t *dqp)
1304{
David Chinnere1f49cf2008-08-13 16:41:43 +10001305 return mutex_trylock(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308void
1309xfs_dqlock(
1310 xfs_dquot_t *dqp)
1311{
David Chinnere1f49cf2008-08-13 16:41:43 +10001312 mutex_lock(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
1315void
1316xfs_dqunlock(
1317 xfs_dquot_t *dqp)
1318{
1319 mutex_unlock(&(dqp->q_qlock));
1320 if (dqp->q_logitem.qli_dquot == dqp) {
1321 /* Once was dqp->q_mount, but might just have been cleared */
David Chinner783a2f62008-10-30 17:39:58 +11001322 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 (xfs_log_item_t*)&(dqp->q_logitem));
1324 }
1325}
1326
1327
1328void
1329xfs_dqunlock_nonotify(
1330 xfs_dquot_t *dqp)
1331{
1332 mutex_unlock(&(dqp->q_qlock));
1333}
1334
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001335/*
1336 * Lock two xfs_dquot structures.
1337 *
1338 * To avoid deadlocks we always lock the quota structure with
1339 * the lowerd id first.
1340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341void
1342xfs_dqlock2(
1343 xfs_dquot_t *d1,
1344 xfs_dquot_t *d2)
1345{
1346 if (d1 && d2) {
1347 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001348 if (be32_to_cpu(d1->q_core.d_id) >
1349 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001350 mutex_lock(&d2->q_qlock);
1351 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001353 mutex_lock(&d1->q_qlock);
1354 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001356 } else if (d1) {
1357 mutex_lock(&d1->q_qlock);
1358 } else if (d2) {
1359 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361}
1362
1363
1364/*
1365 * Take a dquot out of the mount's dqlist as well as the hashlist.
1366 * This is called via unmount as well as quotaoff, and the purge
1367 * will always succeed unless there are soft (temp) references
1368 * outstanding.
1369 *
1370 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1371 * that we're returning! XXXsup - not cool.
1372 */
1373/* ARGSUSED */
1374int
1375xfs_qm_dqpurge(
Denys Vlasenko4f0e8a92008-05-19 16:34:04 +10001376 xfs_dquot_t *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Dave Chinnere6a81f12010-04-13 15:06:51 +10001378 xfs_dqhash_t *qh = dqp->q_hash;
David Chinner3c568362008-04-10 12:20:24 +10001379 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dave Chinner3a254042010-04-13 15:06:48 +10001381 ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001382 ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 xfs_dqlock(dqp);
1385 /*
1386 * We really can't afford to purge a dquot that is
1387 * referenced, because these are hard refs.
1388 * It shouldn't happen in general because we went thru _all_ inodes in
1389 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1390 * However it is possible that we have dquots with temporary
1391 * references that are not attached to an inode. e.g. see xfs_setattr().
1392 */
1393 if (dqp->q_nrefs != 0) {
1394 xfs_dqunlock(dqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001395 mutex_unlock(&dqp->q_hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return (1);
1397 }
1398
1399 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1400
1401 /*
1402 * If we're turning off quotas, we have to make sure that, for
1403 * example, we don't delete quota disk blocks while dquots are
1404 * in the process of getting written to those disk blocks.
1405 * This dquot might well be on AIL, and we can't leave it there
1406 * if we're turning off quotas. Basically, we need this flush
1407 * lock, and are willing to block on it.
1408 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001409 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * Block on the flush lock after nudging dquot buffer,
1412 * if it is incore.
1413 */
1414 xfs_qm_dqflock_pushbuf_wait(dqp);
1415 }
1416
1417 /*
1418 * XXXIf we're turning this type of quotas off, we don't care
1419 * about the dirty metadata sitting in this dquot. OTOH, if
1420 * we're unmounting, we do care, so we flush it and wait.
1421 */
1422 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10001423 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* dqflush unlocks dqflock */
1426 /*
1427 * Given that dqpurge is a very rare occurrence, it is OK
1428 * that we're holding the hashlist and mplist locks
1429 * across the disk write. But, ... XXXsup
1430 *
1431 * We don't care about getting disk errors here. We need
1432 * to purge this dquot anyway, so we go ahead regardless.
1433 */
Dave Chinner20026d92010-02-04 09:48:58 +11001434 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
David Chinner3c568362008-04-10 12:20:24 +10001435 if (error)
1436 xfs_fs_cmn_err(CE_WARN, mp,
1437 "xfs_qm_dqpurge: dquot %p flush failed", dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 xfs_dqflock(dqp);
1439 }
Peter Leckiebc3048e2008-10-30 17:05:04 +11001440 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1442 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1443
Dave Chinnere6a81f12010-04-13 15:06:51 +10001444 list_del_init(&dqp->q_hashlist);
1445 qh->qh_version++;
Dave Chinner3a254042010-04-13 15:06:48 +10001446 list_del_init(&dqp->q_mplist);
1447 mp->m_quotainfo->qi_dqreclaims++;
1448 mp->m_quotainfo->qi_dquots--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /*
1450 * XXX Move this to the front of the freelist, if we can get the
1451 * freelist lock.
1452 */
1453 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1454
1455 dqp->q_mount = NULL;
1456 dqp->q_hash = NULL;
1457 dqp->dq_flags = XFS_DQ_INACTIVE;
1458 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1459 xfs_dqfunlock(dqp);
1460 xfs_dqunlock(dqp);
Dave Chinnere6a81f12010-04-13 15:06:51 +10001461 mutex_unlock(&qh->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return (0);
1463}
1464
1465
1466#ifdef QUOTADEBUG
1467void
1468xfs_qm_dqprint(xfs_dquot_t *dqp)
1469{
1470 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1471 cmn_err(CE_DEBUG, "---- dquotID = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001472 (int)be32_to_cpu(dqp->q_core.d_id));
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001473 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1475 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1476 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1477 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001478 be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1479 (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001481 be64_to_cpu(dqp->q_core.d_blk_softlimit),
1482 (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001484 be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1485 (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001487 be64_to_cpu(dqp->q_core.d_ino_softlimit),
1488 (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001490 be64_to_cpu(dqp->q_core.d_bcount),
1491 (int)be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001493 be64_to_cpu(dqp->q_core.d_icount),
1494 (int)be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 cmn_err(CE_DEBUG, "---- btimer = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001496 (int)be32_to_cpu(dqp->q_core.d_btimer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 cmn_err(CE_DEBUG, "---- itimer = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001498 (int)be32_to_cpu(dqp->q_core.d_itimer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 cmn_err(CE_DEBUG, "---------------------------");
1500}
1501#endif
1502
1503/*
1504 * Give the buffer a little push if it is incore and
1505 * wait on the flush lock.
1506 */
1507void
1508xfs_qm_dqflock_pushbuf_wait(
1509 xfs_dquot_t *dqp)
1510{
1511 xfs_buf_t *bp;
1512
1513 /*
1514 * Check to see if the dquot has been flushed delayed
1515 * write. If so, grab its buffer and send it
1516 * out immediately. We'll be able to acquire
1517 * the flush lock when the I/O completes.
1518 */
1519 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001520 XFS_QI_DQCHUNKLEN(dqp->q_mount), XBF_TRYLOCK);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001521 if (!bp)
1522 goto out_lock;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001523
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001524 if (XFS_BUF_ISDELAYWRITE(bp)) {
1525 if (XFS_BUF_ISPINNED(bp))
1526 xfs_log_force(dqp->q_mount, 0);
1527 xfs_buf_delwri_promote(bp);
1528 wake_up_process(bp->b_target->bt_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001530 xfs_buf_relse(bp);
1531out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 xfs_dqflock(dqp);
1533}