blob: 1a2aa173ef21357fc935f268d303f8508fbf72e5 [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_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
31#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_rtalloc.h"
33#include "xfs_error.h"
34#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_attr.h"
36#include "xfs_buf_item.h"
37#include "xfs_trans_space.h"
38#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Christoph Hellwigbf72de32011-12-06 21:58:19 +000043 * Lock order:
44 *
45 * ip->i_lock
46 * qh->qh_lock
47 * qi->qi_dqlist_lock
48 * dquot->q_qlock (xfs_dqlock() and friends)
49 * dquot->q_flush (xfs_dqflock() and friends)
50 * xfs_Gqm->qm_dqfrlist_lock
51 *
52 * If two dquots need to be locked the order is user before group/project,
53 * otherwise by the lowest id first, see xfs_dqlock2.
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef DEBUG
57xfs_buftarg_t *xfs_dqerror_target;
58int xfs_do_dqerror;
59int xfs_dqreq_num;
60int xfs_dqerror_mod = 33;
61#endif
62
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010063static struct lock_class_key xfs_dquot_other_class;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
66 * Allocate and initialize a dquot. We don't always allocate fresh memory;
67 * we try to reclaim a free dquot if the number of incore dquots are above
68 * a threshold.
69 * The only field inside the core that gets initialized at this point
70 * is the d_id field. The idea is to fill in the entire q_core
71 * when we read in the on disk dquot.
72 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100073STATIC xfs_dquot_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070074xfs_qm_dqinit(
75 xfs_mount_t *mp,
76 xfs_dqid_t id,
77 uint type)
78{
79 xfs_dquot_t *dqp;
80 boolean_t brandnewdquot;
81
82 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
83 dqp->dq_flags = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +110084 dqp->q_core.d_id = cpu_to_be32(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 dqp->q_mount = mp;
86
87 /*
88 * No need to re-initialize these if this is a reclaimed dquot.
89 */
90 if (brandnewdquot) {
Dave Chinner3a8406f2010-04-13 15:06:52 +100091 INIT_LIST_HEAD(&dqp->q_freelist);
Jes Sorensen794ee1b2006-01-09 15:59:21 -080092 mutex_init(&dqp->q_qlock);
Peter Leckiebc3048e2008-10-30 17:05:04 +110093 init_waitqueue_head(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
David Chinnere1f49cf2008-08-13 16:41:43 +100095 /*
96 * Because we want to use a counting completion, complete
97 * the flush completion once to allow a single access to
98 * the flush completion without blocking.
99 */
100 init_completion(&dqp->q_flush);
101 complete(&dqp->q_flush);
102
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000103 trace_xfs_dqinit(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else {
105 /*
106 * Only the q_core portion was zeroed in dqreclaim_one().
107 * So, we need to reset others.
108 */
Dave Chinner3a8406f2010-04-13 15:06:52 +1000109 dqp->q_nrefs = 0;
110 dqp->q_blkno = 0;
111 INIT_LIST_HEAD(&dqp->q_mplist);
112 INIT_LIST_HEAD(&dqp->q_hashlist);
113 dqp->q_bufoffset = 0;
114 dqp->q_fileoffset = 0;
115 dqp->q_transp = NULL;
116 dqp->q_gdquot = NULL;
117 dqp->q_res_bcount = 0;
118 dqp->q_res_icount = 0;
119 dqp->q_res_rtbcount = 0;
120 atomic_set(&dqp->q_pincount, 0);
121 dqp->q_hash = NULL;
122 ASSERT(list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000124 trace_xfs_dqreuse(dqp);
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +0100125 }
126
127 /*
128 * In either case we need to make sure group quotas have a different
129 * lock class than user quotas, to make sure lockdep knows we can
130 * locks of one of each at the same time.
131 */
132 if (!(type & XFS_DQ_USER))
133 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /*
136 * log item gets initialized later
137 */
138 return (dqp);
139}
140
141/*
142 * This is called to free all the memory associated with a dquot
143 */
144void
145xfs_qm_dqdestroy(
146 xfs_dquot_t *dqp)
147{
Dave Chinner3a8406f2010-04-13 15:06:52 +1000148 ASSERT(list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 mutex_destroy(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 atomic_dec(&xfs_Gqm->qm_totaldquots);
154}
155
156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * If default limits are in force, push them into the dquot now.
158 * We overwrite the dquot limits only if they are zero and this
159 * is not the root dquot.
160 */
161void
162xfs_qm_adjust_dqlimits(
163 xfs_mount_t *mp,
164 xfs_disk_dquot_t *d)
165{
166 xfs_quotainfo_t *q = mp->m_quotainfo;
167
168 ASSERT(d->d_id);
169
170 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100171 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100173 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (q->qi_isoftlimit && !d->d_ino_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100175 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100177 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100179 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100181 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/*
185 * Check the limits and timers of a dquot and start or reset timers
186 * if necessary.
187 * This gets called even when quota enforcement is OFF, which makes our
188 * life a little less complicated. (We just don't reject any quota
189 * reservations in that case, when enforcement is off).
190 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
191 * enforcement's off.
192 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000193 * 'automatically' get started when limits get exceeded. They do
194 * get reset to zero, however, when we find the count to be under
195 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
197void
198xfs_qm_adjust_dqtimers(
199 xfs_mount_t *mp,
200 xfs_disk_dquot_t *d)
201{
202 ASSERT(d->d_id);
203
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200204#ifdef DEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100205 if (d->d_blk_hardlimit)
206 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
207 be64_to_cpu(d->d_blk_hardlimit));
208 if (d->d_ino_hardlimit)
209 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
210 be64_to_cpu(d->d_ino_hardlimit));
211 if (d->d_rtb_hardlimit)
212 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
213 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100217 if ((d->d_blk_softlimit &&
218 (be64_to_cpu(d->d_bcount) >=
219 be64_to_cpu(d->d_blk_softlimit))) ||
220 (d->d_blk_hardlimit &&
221 (be64_to_cpu(d->d_bcount) >=
222 be64_to_cpu(d->d_blk_hardlimit)))) {
223 d->d_btimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000224 mp->m_quotainfo->qi_btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000225 } else {
226 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 } else {
229 if ((!d->d_blk_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100230 (be64_to_cpu(d->d_bcount) <
231 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 (!d->d_blk_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100233 (be64_to_cpu(d->d_bcount) <
234 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 d->d_btimer = 0;
236 }
237 }
238
239 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100240 if ((d->d_ino_softlimit &&
241 (be64_to_cpu(d->d_icount) >=
242 be64_to_cpu(d->d_ino_softlimit))) ||
243 (d->d_ino_hardlimit &&
244 (be64_to_cpu(d->d_icount) >=
245 be64_to_cpu(d->d_ino_hardlimit)))) {
246 d->d_itimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000247 mp->m_quotainfo->qi_itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000248 } else {
249 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 } else {
252 if ((!d->d_ino_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100253 (be64_to_cpu(d->d_icount) <
254 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 (!d->d_ino_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100256 (be64_to_cpu(d->d_icount) <
257 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 d->d_itimer = 0;
259 }
260 }
261
262 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100263 if ((d->d_rtb_softlimit &&
264 (be64_to_cpu(d->d_rtbcount) >=
265 be64_to_cpu(d->d_rtb_softlimit))) ||
266 (d->d_rtb_hardlimit &&
267 (be64_to_cpu(d->d_rtbcount) >=
268 be64_to_cpu(d->d_rtb_hardlimit)))) {
269 d->d_rtbtimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000270 mp->m_quotainfo->qi_rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000271 } else {
272 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 } else {
275 if ((!d->d_rtb_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100276 (be64_to_cpu(d->d_rtbcount) <
277 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 (!d->d_rtb_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100279 (be64_to_cpu(d->d_rtbcount) <
280 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 d->d_rtbtimer = 0;
282 }
283 }
284}
285
286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * initialize a buffer full of dquots and log the whole thing
288 */
289STATIC void
290xfs_qm_init_dquot_blk(
291 xfs_trans_t *tp,
292 xfs_mount_t *mp,
293 xfs_dqid_t id,
294 uint type,
295 xfs_buf_t *bp)
296{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000297 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfs_dqblk_t *d;
299 int curid, i;
300
301 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200302 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Chandra Seetharaman62926042011-07-22 23:40:15 +0000304 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * ID of the first dquot in the block - id's are zero based.
308 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000309 curid = id - (id % q->qi_dqperchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 ASSERT(curid >= 0);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000311 memset(d, 0, BBTOB(q->qi_dqchunklen));
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000312 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
313 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
314 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
315 d->dd_diskdq.d_id = cpu_to_be32(curid);
316 d->dd_diskdq.d_flags = type;
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 xfs_trans_dquot_buf(tp, bp,
Dave Chinnerc1155412010-05-07 11:05:19 +1000320 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
321 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
322 XFS_BLF_GDQUOT_BUF)));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000323 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326
327
328/*
329 * Allocate a block and fill it with dquots.
330 * This is called when the bmapi finds a hole.
331 */
332STATIC int
333xfs_qm_dqalloc(
Tim Shimminefa092f2005-09-05 08:29:01 +1000334 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 xfs_mount_t *mp,
336 xfs_dquot_t *dqp,
337 xfs_inode_t *quotip,
338 xfs_fileoff_t offset_fsb,
339 xfs_buf_t **O_bpp)
340{
341 xfs_fsblock_t firstblock;
342 xfs_bmap_free_t flist;
343 xfs_bmbt_irec_t map;
344 int nmaps, error, committed;
345 xfs_buf_t *bp;
Tim Shimminefa092f2005-09-05 08:29:01 +1000346 xfs_trans_t *tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 ASSERT(tp != NULL);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000349
350 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /*
353 * Initialize the bmap freelist prior to calling bmapi code.
354 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600355 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 xfs_ilock(quotip, XFS_ILOCK_EXCL);
357 /*
358 * Return if this type of quotas is turned off while we didn't
359 * have an inode lock
360 */
361 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
362 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
363 return (ESRCH);
364 }
365
Christoph Hellwigddc34152011-09-19 15:00:54 +0000366 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 nmaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000368 error = xfs_bmapi_write(tp, quotip, offset_fsb,
369 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
370 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
371 &map, &nmaps, &flist);
372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
375 ASSERT(nmaps == 1);
376 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
377 (map.br_startblock != HOLESTARTBLOCK));
378
379 /*
380 * Keep track of the blkno to save a lookup later
381 */
382 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
383
384 /* now we can just get the buffer (there's nothing to read yet) */
385 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
386 dqp->q_blkno,
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000387 mp->m_quotainfo->qi_dqchunklen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000389
390 error = xfs_buf_geterror(bp);
391 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto error1;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * Make a chunk of dquots out of this buffer and log
396 * the entire thing.
397 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100398 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000399 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Tim Shimminefa092f2005-09-05 08:29:01 +1000401 /*
402 * xfs_bmap_finish() may commit the current transaction and
403 * start a second transaction if the freelist is not empty.
404 *
405 * Since we still want to modify this buffer, we need to
406 * ensure that the buffer is not released on commit of
407 * the first transaction and ensure the buffer is added to the
408 * second transaction.
409 *
410 * If there is only one transaction then don't stop the buffer
411 * from being released when it commits later on.
412 */
413
414 xfs_trans_bhold(tp, bp);
415
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100416 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 goto error1;
418 }
419
Tim Shimminefa092f2005-09-05 08:29:01 +1000420 if (committed) {
421 tp = *tpp;
422 xfs_trans_bjoin(tp, bp);
423 } else {
424 xfs_trans_bhold_release(tp, bp);
425 }
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *O_bpp = bp;
428 return 0;
429
430 error1:
431 xfs_bmap_cancel(&flist);
432 error0:
433 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
434
435 return (error);
436}
437
438/*
439 * Maps a dquot to the buffer containing its on-disk version.
440 * This returns a ptr to the buffer containing the on-disk dquot
441 * in the bpp param, and a ptr to the on-disk dquot within that buffer
442 */
443STATIC int
444xfs_qm_dqtobp(
Tim Shimminefa092f2005-09-05 08:29:01 +1000445 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 xfs_dquot_t *dqp,
447 xfs_disk_dquot_t **O_ddpp,
448 xfs_buf_t **O_bpp,
449 uint flags)
450{
451 xfs_bmbt_irec_t map;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000452 int nmaps = 1, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xfs_buf_t *bp;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000454 xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
455 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_disk_dquot_t *ddq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000457 xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
Tim Shimminefa092f2005-09-05 08:29:01 +1000458 xfs_trans_t *tp = (tpp ? *tpp : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000460 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000462 xfs_ilock(quotip, XFS_ILOCK_SHARED);
463 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000465 * Return if this type of quotas is turned off while we
466 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000469 return ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000473 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000475 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
476 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000477
478 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
479 if (error)
480 return error;
481
482 ASSERT(nmaps == 1);
483 ASSERT(map.br_blockcount == 1);
484
485 /*
486 * Offset of dquot in the (fixed sized) dquot chunk.
487 */
488 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
489 sizeof(xfs_dqblk_t);
490
491 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
492 if (map.br_startblock == HOLESTARTBLOCK) {
493 /*
494 * We don't allocate unless we're asked to
495 */
496 if (!(flags & XFS_QMOPT_DQALLOC))
497 return ENOENT;
498
499 ASSERT(tp);
500 error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
501 dqp->q_fileoffset, &bp);
502 if (error)
503 return error;
504 tp = *tpp;
505 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000506 trace_xfs_dqtobp_read(dqp);
507
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000508 /*
509 * store the blkno etc so that we don't have to do the
510 * mapping all the time
511 */
512 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
513
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000514 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
515 dqp->q_blkno,
516 mp->m_quotainfo->qi_dqchunklen,
517 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (error || !bp)
519 return XFS_ERROR(error);
520 }
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000521
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200522 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /*
525 * calculate the location of the dquot inside the buffer.
526 */
Chandra Seetharaman62926042011-07-22 23:40:15 +0000527 ddq = bp->b_addr + dqp->q_bufoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /*
530 * A simple sanity check in case we got a corrupted dquot...
531 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100532 error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100534 "dqtobp");
535 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!(flags & XFS_QMOPT_DQREPAIR)) {
537 xfs_trans_brelse(tp, bp);
538 return XFS_ERROR(EIO);
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
542 *O_bpp = bp;
543 *O_ddpp = ddq;
544
545 return (0);
546}
547
548
549/*
550 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
551 * and release the buffer immediately.
552 *
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000553 * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555STATIC int
556xfs_qm_dqread(
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000557 struct xfs_mount *mp,
558 xfs_dqid_t id,
559 uint type,
560 uint flags,
561 struct xfs_dquot **O_dqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000563 struct xfs_dquot *dqp;
564 struct xfs_disk_dquot *ddqp;
565 struct xfs_buf *bp;
566 struct xfs_trans *tp = NULL;
567 int error;
568 int cancelflags = 0;
Tim Shimminefa092f2005-09-05 08:29:01 +1000569
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000570 dqp = xfs_qm_dqinit(mp, id, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000572 trace_xfs_dqread(dqp);
573
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000574 if (flags & XFS_QMOPT_DQALLOC) {
575 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
576 error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
577 XFS_WRITE_LOG_RES(mp) +
578 /*
579 * Round the chunklen up to the next multiple
580 * of 128 (buf log item chunk size)).
581 */
582 BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 + 128,
583 0,
584 XFS_TRANS_PERM_LOG_RES,
585 XFS_WRITE_LOG_COUNT);
586 if (error)
587 goto error1;
588 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
589 }
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * get a pointer to the on-disk dquot and the buffer containing it
593 * dqp already knows its own type (GROUP/USER).
594 */
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000595 error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
596 if (error) {
597 /*
598 * This can happen if quotas got turned off (ESRCH),
599 * or if the dquot didn't exist on disk and we ask to
600 * allocate (ENOENT).
601 */
602 trace_xfs_dqread_fail(dqp);
603 cancelflags |= XFS_TRANS_ABORT;
604 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 /* copy everything from disk dquot to the incore dquot */
608 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 xfs_qm_dquot_logitem_init(dqp);
610
611 /*
612 * Reservation counters are defined as reservation plus current usage
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300613 * to avoid having to add every time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100615 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
616 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
617 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* Mark the buf so that this will stay incore a little longer */
Christoph Hellwig38f23232011-10-10 16:52:45 +0000620 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
623 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
624 * So we need to release with xfs_trans_brelse().
625 * The strategy here is identical to that of inodes; we lock
626 * the dquot in xfs_qm_dqget() before making it accessible to
627 * others. This is because dquots, like inodes, need a good level of
628 * concurrency, and we don't want to take locks on the entire buffers
629 * for dquot accesses.
630 * Note also that the dquot buffer may even be dirty at this point, if
631 * this particular dquot was repaired. We still aren't afraid to
632 * brelse it because we have the changes incore.
633 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200634 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 xfs_trans_brelse(tp, bp);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (tp) {
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000638 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
639 if (error)
640 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 *O_dqpp = dqp;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000644 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000646error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (tp)
648 xfs_trans_cancel(tp, cancelflags);
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000649error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 xfs_qm_dqdestroy(dqp);
651 *O_dqpp = NULL;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
656 * Lookup a dquot in the incore dquot hashtable. We keep two separate
657 * hashtables for user and group dquots; and, these are global tables
658 * inside the XQM, not per-filesystem tables.
659 * The hash chain must be locked by caller, and it is left locked
660 * on return. Returning dquot is locked.
661 */
662STATIC int
663xfs_qm_dqlookup(
664 xfs_mount_t *mp,
665 xfs_dqid_t id,
666 xfs_dqhash_t *qh,
667 xfs_dquot_t **O_dqpp)
668{
669 xfs_dquot_t *dqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100671 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Traverse the hashchain looking for a match
675 */
Dave Chinnere6a81f12010-04-13 15:06:51 +1000676 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * We already have the hashlock. We don't need the
679 * dqlock to look at the id field of the dquot, since the
680 * id can't be modified without the hashlock anyway.
681 */
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000682 if (be32_to_cpu(dqp->q_core.d_id) != id || dqp->q_mount != mp)
683 continue;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000684
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000685 trace_xfs_dqlookup_found(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000687 xfs_dqlock(dqp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000688 if (dqp->dq_flags & XFS_DQ_FREEING) {
689 *O_dqpp = NULL;
690 xfs_dqunlock(dqp);
691 return -1;
692 }
693
Christoph Hellwig78e55892011-12-06 21:58:22 +0000694 dqp->q_nrefs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000696 /*
697 * move the dquot to the front of the hashchain
698 */
699 list_move(&dqp->q_hashlist, &qh->qh_list);
700 trace_xfs_dqlookup_done(dqp);
701 *O_dqpp = dqp;
702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 *O_dqpp = NULL;
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000706 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
710 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
711 * a locked dquot, doing an allocation (if requested) as needed.
712 * When both an inode and an id are given, the inode's id takes precedence.
713 * That is, if the id changes while we don't hold the ilock inside this
714 * function, the new dquot is returned, not necessarily the one requested
715 * in the id argument.
716 */
717int
718xfs_qm_dqget(
719 xfs_mount_t *mp,
720 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000721 xfs_dqid_t id, /* uid/projid/gid depending on type */
722 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
724 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
725{
726 xfs_dquot_t *dqp;
727 xfs_dqhash_t *h;
728 uint version;
729 int error;
730
731 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
732 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000733 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
735 return (ESRCH);
736 }
737 h = XFS_DQ_HASH(mp, id, type);
738
739#ifdef DEBUG
740 if (xfs_do_dqerror) {
741 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
742 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100743 xfs_debug(mp, "Returning error in dqget");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return (EIO);
745 }
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000748 ASSERT(type == XFS_DQ_USER ||
749 type == XFS_DQ_PROJ ||
750 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (ip) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000752 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (type == XFS_DQ_USER)
754 ASSERT(ip->i_udquot == NULL);
755 else
756 ASSERT(ip->i_gdquot == NULL);
757 }
758#endif
Christoph Hellwig92678552011-12-06 21:58:18 +0000759
760restart:
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100761 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /*
764 * Look in the cache (hashtable).
765 * The chain is kept locked during lookup.
766 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000767 switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) {
768 case -1:
769 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
770 mutex_unlock(&h->qh_lock);
771 delay(1);
772 goto restart;
773 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
775 /*
776 * The dquot was found, moved to the front of the chain,
777 * taken off the freelist if it was on it, and locked
778 * at this point. Just unlock the hashchain and return.
779 */
780 ASSERT(*O_dqpp);
781 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100782 mutex_unlock(&h->qh_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000783 trace_xfs_dqget_hit(*O_dqpp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000784 return 0; /* success */
785 default:
786 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /*
791 * Dquot cache miss. We don't want to keep the inode lock across
792 * a (potential) disk read. Also we don't want to deal with the lock
793 * ordering between quotainode and this inode. OTOH, dropping the inode
794 * lock here means dealing with a chown that can happen before
795 * we re-acquire the lock.
796 */
797 if (ip)
798 xfs_iunlock(ip, XFS_ILOCK_EXCL);
799 /*
800 * Save the hashchain version stamp, and unlock the chain, so that
801 * we don't keep the lock across a disk read
802 */
803 version = h->qh_version;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100804 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000806 error = xfs_qm_dqread(mp, id, type, flags, &dqp);
807 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ip)
809 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 /*
814 * See if this is mount code calling to look at the overall quota limits
815 * which are stored in the id == 0 user or group's dquot.
816 * Since we may not have done a quotacheck by this point, just return
817 * the dquot without attaching it to any hashtables, lists, etc, or even
818 * taking a reference.
819 * The caller must dqdestroy this once done.
820 */
821 if (flags & XFS_QMOPT_DQSUSER) {
822 ASSERT(id == 0);
823 ASSERT(! ip);
824 goto dqret;
825 }
826
827 /*
828 * Dquot lock comes after hashlock in the lock ordering
829 */
830 if (ip) {
831 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig191f8482010-04-20 17:01:53 +1000832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * A dquot could be attached to this inode by now, since
835 * we had dropped the ilock.
836 */
837 if (type == XFS_DQ_USER) {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000838 if (!XFS_IS_UQUOTA_ON(mp)) {
839 /* inode stays locked on return */
840 xfs_qm_dqdestroy(dqp);
841 return XFS_ERROR(ESRCH);
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (ip->i_udquot) {
844 xfs_qm_dqdestroy(dqp);
845 dqp = ip->i_udquot;
846 xfs_dqlock(dqp);
847 goto dqret;
848 }
849 } else {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000850 if (!XFS_IS_OQUOTA_ON(mp)) {
851 /* inode stays locked on return */
852 xfs_qm_dqdestroy(dqp);
853 return XFS_ERROR(ESRCH);
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (ip->i_gdquot) {
856 xfs_qm_dqdestroy(dqp);
857 dqp = ip->i_gdquot;
858 xfs_dqlock(dqp);
859 goto dqret;
860 }
861 }
862 }
863
864 /*
865 * Hashlock comes after ilock in lock order
866 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100867 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (version != h->qh_version) {
869 xfs_dquot_t *tmpdqp;
870 /*
871 * Now, see if somebody else put the dquot in the
872 * hashtable before us. This can happen because we didn't
873 * keep the hashchain lock. We don't have to worry about
874 * lock order between the two dquots here since dqp isn't
875 * on any findable lists yet.
876 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000877 switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) {
878 case 0:
879 case -1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /*
Christoph Hellwig92678552011-12-06 21:58:18 +0000881 * Duplicate found, either in cache or on its way out.
882 * Just throw away the new dquot and start over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000884 if (tmpdqp)
885 xfs_qm_dqput(tmpdqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100886 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 xfs_qm_dqdestroy(dqp);
888 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
Christoph Hellwig92678552011-12-06 21:58:18 +0000889 goto restart;
890 default:
891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 }
894
895 /*
896 * Put the dquot at the beginning of the hash-chain and mp's list
897 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
898 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100899 ASSERT(mutex_is_locked(&h->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 dqp->q_hash = h;
Dave Chinnere6a81f12010-04-13 15:06:51 +1000901 list_add(&dqp->q_hashlist, &h->qh_list);
902 h->qh_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /*
905 * Attach this dquot to this filesystem's list of all dquots,
906 * kept inside the mount structure in m_quotainfo field
907 */
Dave Chinner3a254042010-04-13 15:06:48 +1000908 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /*
911 * We return a locked dquot to the caller, with a reference taken
912 */
913 xfs_dqlock(dqp);
914 dqp->q_nrefs = 1;
915
Dave Chinner3a254042010-04-13 15:06:48 +1000916 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
917 mp->m_quotainfo->qi_dquots++;
918 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100919 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 dqret:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000921 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000922 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *O_dqpp = dqp;
924 return (0);
925}
926
927
928/*
929 * Release a reference to the dquot (decrement ref-count)
930 * and unlock it. If there is a group quota attached to this
931 * dquot, carefully release that too without tripping over
932 * deadlocks'n'stuff.
933 */
934void
935xfs_qm_dqput(
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000936 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000938 struct xfs_dquot *gdqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 ASSERT(dqp->q_nrefs > 0);
941 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000942
943 trace_xfs_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000945recurse:
946 if (--dqp->q_nrefs > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 xfs_dqunlock(dqp);
948 return;
949 }
950
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000951 trace_xfs_dqput_free(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000953 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
954 if (list_empty(&dqp->q_freelist)) {
955 list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
956 xfs_Gqm->qm_dqfrlist_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
Dave Chinner3a8406f2010-04-13 15:06:52 +1000958 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000959
960 /*
961 * If we just added a udquot to the freelist, then we want to release
962 * the gdquot reference that it (probably) has. Otherwise it'll keep
963 * the gdquot from getting reclaimed.
964 */
965 gdqp = dqp->q_gdquot;
966 if (gdqp) {
967 xfs_dqlock(gdqp);
968 dqp->q_gdquot = NULL;
969 }
970 xfs_dqunlock(dqp);
971
972 /*
973 * If we had a group quota hint, release it now.
974 */
975 if (gdqp) {
976 dqp = gdqp;
977 goto recurse;
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981/*
982 * Release a dquot. Flush it if dirty, then dqput() it.
983 * dquot must not be locked.
984 */
985void
986xfs_qm_dqrele(
987 xfs_dquot_t *dqp)
988{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200989 if (!dqp)
990 return;
991
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000992 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 xfs_dqlock(dqp);
995 /*
996 * We don't care to flush it if the dquot is dirty here.
997 * That will create stutters that we want to avoid.
998 * Instead we do a delayed write when we try to reclaim
999 * a dirty dquot. Also xfs_sync will take part of the burden...
1000 */
1001 xfs_qm_dqput(dqp);
1002}
1003
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001004/*
1005 * This is the dquot flushing I/O completion routine. It is called
1006 * from interrupt level when the buffer containing the dquot is
1007 * flushed to disk. It is responsible for removing the dquot logitem
1008 * from the AIL if it has not been re-logged, and unlocking the dquot's
1009 * flush lock. This behavior is very similar to that of inodes..
1010 */
1011STATIC void
1012xfs_qm_dqflush_done(
1013 struct xfs_buf *bp,
1014 struct xfs_log_item *lip)
1015{
1016 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
1017 xfs_dquot_t *dqp = qip->qli_dquot;
1018 struct xfs_ail *ailp = lip->li_ailp;
1019
1020 /*
1021 * We only want to pull the item from the AIL if its
1022 * location in the log has not changed since we started the flush.
1023 * Thus, we only bother if the dquot's lsn has
1024 * not changed. First we check the lsn outside the lock
1025 * since it's cheaper, and then we recheck while
1026 * holding the lock before removing the dquot from the AIL.
1027 */
1028 if ((lip->li_flags & XFS_LI_IN_AIL) &&
1029 lip->li_lsn == qip->qli_flush_lsn) {
1030
1031 /* xfs_trans_ail_delete() drops the AIL lock. */
1032 spin_lock(&ailp->xa_lock);
1033 if (lip->li_lsn == qip->qli_flush_lsn)
1034 xfs_trans_ail_delete(ailp, lip);
1035 else
1036 spin_unlock(&ailp->xa_lock);
1037 }
1038
1039 /*
1040 * Release the dq's flush lock since we're done with it.
1041 */
1042 xfs_dqfunlock(dqp);
1043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045/*
1046 * Write a modified dquot to disk.
1047 * The dquot must be locked and the flush lock too taken by caller.
1048 * The flush lock will not be unlocked until the dquot reaches the disk,
1049 * but the dquot is free to be unlocked and modified by the caller
1050 * in the interim. Dquot is still locked on return. This behavior is
1051 * identical to that of inodes.
1052 */
1053int
1054xfs_qm_dqflush(
1055 xfs_dquot_t *dqp,
1056 uint flags)
1057{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001058 struct xfs_mount *mp = dqp->q_mount;
1059 struct xfs_buf *bp;
1060 struct xfs_disk_dquot *ddqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001064 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001065
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001066 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001069 * If not dirty, or it's pinned and we are not supposed to block, nada.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
David Chinner2f8a3ce2008-10-30 17:07:20 +11001071 if (!XFS_DQ_IS_DIRTY(dqp) ||
Christoph Hellwigfdedf282011-12-06 21:58:10 +00001072 ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 xfs_dqfunlock(dqp);
David Chinner2f8a3ce2008-10-30 17:07:20 +11001074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 xfs_qm_dqunpin_wait(dqp);
1077
1078 /*
1079 * This may have been unpinned because the filesystem is shutting
1080 * down forcibly. If that's the case we must not write this dquot
1081 * to disk, because the log record didn't make it to disk!
1082 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001083 if (XFS_FORCED_SHUTDOWN(mp)) {
1084 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 xfs_dqfunlock(dqp);
1086 return XFS_ERROR(EIO);
1087 }
1088
1089 /*
1090 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001092 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1093 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
1094 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ASSERT(error != ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 xfs_dqfunlock(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001097 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001100 /*
1101 * Calculate the location of the dquot inside the buffer.
1102 */
Chandra Seetharaman62926042011-07-22 23:40:15 +00001103 ddqp = bp->b_addr + dqp->q_bufoffset;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001104
1105 /*
1106 * A simple sanity check in case we got a corrupted dquot..
1107 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001108 error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
1109 XFS_QMOPT_DOWARN, "dqflush (incore copy)");
1110 if (error) {
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001111 xfs_buf_relse(bp);
1112 xfs_dqfunlock(dqp);
1113 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return XFS_ERROR(EIO);
1115 }
1116
1117 /* This is the only portion of data that needs to persist */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001118 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /*
1121 * Clear the dirty field and remember the flush lsn for later use.
1122 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001123 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
David Chinner7b2e2a32008-10-30 17:39:12 +11001125 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1126 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * Attach an iodone routine so that we can remove this dquot from the
1130 * AIL and release the flush lock once the dquot is synced to disk.
1131 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001132 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1133 &dqp->q_logitem.qli_item);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 /*
1136 * If the buffer is pinned then push on the log so we won't
1137 * get stuck waiting in the write for too long.
1138 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001139 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001140 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001141 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Dave Chinner20026d92010-02-04 09:48:58 +11001144 if (flags & SYNC_WAIT)
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001145 error = xfs_bwrite(bp);
1146 else
Christoph Hellwig61551f12011-08-23 08:28:06 +00001147 xfs_buf_delwri_queue(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001148
1149 xfs_buf_relse(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001150
1151 trace_xfs_dqflush_done(dqp);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * dqp is still locked, but caller is free to unlock it now.
1155 */
Dave Chinner20026d92010-02-04 09:48:58 +11001156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160void
1161xfs_dqunlock(
1162 xfs_dquot_t *dqp)
1163{
Christoph Hellwig800b4842011-12-06 21:58:14 +00001164 xfs_dqunlock_nonotify(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (dqp->q_logitem.qli_dquot == dqp) {
David Chinner783a2f62008-10-30 17:39:58 +11001166 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
Christoph Hellwig800b4842011-12-06 21:58:14 +00001167 &dqp->q_logitem.qli_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
1169}
1170
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001171/*
1172 * Lock two xfs_dquot structures.
1173 *
1174 * To avoid deadlocks we always lock the quota structure with
1175 * the lowerd id first.
1176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177void
1178xfs_dqlock2(
1179 xfs_dquot_t *d1,
1180 xfs_dquot_t *d2)
1181{
1182 if (d1 && d2) {
1183 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001184 if (be32_to_cpu(d1->q_core.d_id) >
1185 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001186 mutex_lock(&d2->q_qlock);
1187 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001189 mutex_lock(&d1->q_qlock);
1190 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001192 } else if (d1) {
1193 mutex_lock(&d1->q_qlock);
1194 } else if (d2) {
1195 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199/*
Christoph Hellwig92678552011-12-06 21:58:18 +00001200 * Take a dquot out of the mount's dqlist as well as the hashlist. This is
1201 * called via unmount as well as quotaoff, and the purge will always succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
Christoph Hellwig92678552011-12-06 21:58:18 +00001203void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204xfs_qm_dqpurge(
Christoph Hellwig92678552011-12-06 21:58:18 +00001205 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Christoph Hellwig92678552011-12-06 21:58:18 +00001207 struct xfs_mount *mp = dqp->q_mount;
1208 struct xfs_dqhash *qh = dqp->q_hash;
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 xfs_dqlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /*
1213 * If we're turning off quotas, we have to make sure that, for
1214 * example, we don't delete quota disk blocks while dquots are
1215 * in the process of getting written to those disk blocks.
1216 * This dquot might well be on AIL, and we can't leave it there
1217 * if we're turning off quotas. Basically, we need this flush
1218 * lock, and are willing to block on it.
1219 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001220 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /*
1222 * Block on the flush lock after nudging dquot buffer,
1223 * if it is incore.
1224 */
Christoph Hellwig800b4842011-12-06 21:58:14 +00001225 xfs_dqflock_pushbuf_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
1228 /*
Christoph Hellwig92678552011-12-06 21:58:18 +00001229 * If we are turning this type of quotas off, we don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 * about the dirty metadata sitting in this dquot. OTOH, if
1231 * we're unmounting, we do care, so we flush it and wait.
1232 */
1233 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10001234 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * We don't care about getting disk errors here. We need
1238 * to purge this dquot anyway, so we go ahead regardless.
1239 */
Dave Chinner20026d92010-02-04 09:48:58 +11001240 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
David Chinner3c568362008-04-10 12:20:24 +10001241 if (error)
Dave Chinner53487782011-03-07 10:05:35 +11001242 xfs_warn(mp, "%s: dquot %p flush failed",
1243 __func__, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 xfs_dqflock(dqp);
1245 }
Christoph Hellwig92678552011-12-06 21:58:18 +00001246
Peter Leckiebc3048e2008-10-30 17:05:04 +11001247 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1249 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 xfs_dqfunlock(dqp);
1252 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Christoph Hellwig92678552011-12-06 21:58:18 +00001254 mutex_lock(&qh->qh_lock);
1255 list_del_init(&dqp->q_hashlist);
1256 qh->qh_version++;
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001257 mutex_unlock(&qh->qh_lock);
1258
Christoph Hellwig92678552011-12-06 21:58:18 +00001259 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
1260 list_del_init(&dqp->q_mplist);
1261 mp->m_quotainfo->qi_dqreclaims++;
1262 mp->m_quotainfo->qi_dquots--;
1263 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
1264
1265 /*
1266 * We move dquots to the freelist as soon as their reference count
1267 * hits zero, so it really should be on the freelist here.
1268 */
1269 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
1270 ASSERT(!list_empty(&dqp->q_freelist));
1271 list_del_init(&dqp->q_freelist);
1272 xfs_Gqm->qm_dqfrlist_cnt--;
1273 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
1274
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001275 xfs_qm_dqdestroy(dqp);
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/*
1279 * Give the buffer a little push if it is incore and
1280 * wait on the flush lock.
1281 */
1282void
Christoph Hellwig800b4842011-12-06 21:58:14 +00001283xfs_dqflock_pushbuf_wait(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 xfs_dquot_t *dqp)
1285{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001286 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 xfs_buf_t *bp;
1288
1289 /*
1290 * Check to see if the dquot has been flushed delayed
1291 * write. If so, grab its buffer and send it
1292 * out immediately. We'll be able to acquire
1293 * the flush lock when the I/O completes.
1294 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001295 bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
1296 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001297 if (!bp)
1298 goto out_lock;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001299
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001300 if (XFS_BUF_ISDELAYWRITE(bp)) {
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001301 if (xfs_buf_ispinned(bp))
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001302 xfs_log_force(mp, 0);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001303 xfs_buf_delwri_promote(bp);
1304 wake_up_process(bp->b_target->bt_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001306 xfs_buf_relse(bp);
1307out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 xfs_dqflock(dqp);
1309}