blob: 2ce562cb5c78e0413f568472e610cb295ebc0d86 [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/*
157 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
158 */
159STATIC void
160xfs_qm_dqinit_core(
161 xfs_dqid_t id,
162 uint type,
163 xfs_dqblk_t *d)
164{
165 /*
166 * Caller has zero'd the entire dquot 'chunk' already.
167 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100168 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
169 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
170 d->dd_diskdq.d_id = cpu_to_be32(id);
171 d->dd_diskdq.d_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175 * If default limits are in force, push them into the dquot now.
176 * We overwrite the dquot limits only if they are zero and this
177 * is not the root dquot.
178 */
179void
180xfs_qm_adjust_dqlimits(
181 xfs_mount_t *mp,
182 xfs_disk_dquot_t *d)
183{
184 xfs_quotainfo_t *q = mp->m_quotainfo;
185
186 ASSERT(d->d_id);
187
188 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100189 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100191 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (q->qi_isoftlimit && !d->d_ino_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100193 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100195 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100197 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100199 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/*
203 * Check the limits and timers of a dquot and start or reset timers
204 * if necessary.
205 * This gets called even when quota enforcement is OFF, which makes our
206 * life a little less complicated. (We just don't reject any quota
207 * reservations in that case, when enforcement is off).
208 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
209 * enforcement's off.
210 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000211 * 'automatically' get started when limits get exceeded. They do
212 * get reset to zero, however, when we find the count to be under
213 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215void
216xfs_qm_adjust_dqtimers(
217 xfs_mount_t *mp,
218 xfs_disk_dquot_t *d)
219{
220 ASSERT(d->d_id);
221
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200222#ifdef DEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100223 if (d->d_blk_hardlimit)
224 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
225 be64_to_cpu(d->d_blk_hardlimit));
226 if (d->d_ino_hardlimit)
227 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
228 be64_to_cpu(d->d_ino_hardlimit));
229 if (d->d_rtb_hardlimit)
230 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
231 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100235 if ((d->d_blk_softlimit &&
236 (be64_to_cpu(d->d_bcount) >=
237 be64_to_cpu(d->d_blk_softlimit))) ||
238 (d->d_blk_hardlimit &&
239 (be64_to_cpu(d->d_bcount) >=
240 be64_to_cpu(d->d_blk_hardlimit)))) {
241 d->d_btimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000242 mp->m_quotainfo->qi_btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000243 } else {
244 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 } else {
247 if ((!d->d_blk_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100248 (be64_to_cpu(d->d_bcount) <
249 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 (!d->d_blk_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100251 (be64_to_cpu(d->d_bcount) <
252 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 d->d_btimer = 0;
254 }
255 }
256
257 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100258 if ((d->d_ino_softlimit &&
259 (be64_to_cpu(d->d_icount) >=
260 be64_to_cpu(d->d_ino_softlimit))) ||
261 (d->d_ino_hardlimit &&
262 (be64_to_cpu(d->d_icount) >=
263 be64_to_cpu(d->d_ino_hardlimit)))) {
264 d->d_itimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000265 mp->m_quotainfo->qi_itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000266 } else {
267 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 } else {
270 if ((!d->d_ino_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100271 (be64_to_cpu(d->d_icount) <
272 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 (!d->d_ino_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100274 (be64_to_cpu(d->d_icount) <
275 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 d->d_itimer = 0;
277 }
278 }
279
280 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100281 if ((d->d_rtb_softlimit &&
282 (be64_to_cpu(d->d_rtbcount) >=
283 be64_to_cpu(d->d_rtb_softlimit))) ||
284 (d->d_rtb_hardlimit &&
285 (be64_to_cpu(d->d_rtbcount) >=
286 be64_to_cpu(d->d_rtb_hardlimit)))) {
287 d->d_rtbtimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000288 mp->m_quotainfo->qi_rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000289 } else {
290 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 } else {
293 if ((!d->d_rtb_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100294 (be64_to_cpu(d->d_rtbcount) <
295 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 (!d->d_rtb_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100297 (be64_to_cpu(d->d_rtbcount) <
298 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 d->d_rtbtimer = 0;
300 }
301 }
302}
303
304/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * initialize a buffer full of dquots and log the whole thing
306 */
307STATIC void
308xfs_qm_init_dquot_blk(
309 xfs_trans_t *tp,
310 xfs_mount_t *mp,
311 xfs_dqid_t id,
312 uint type,
313 xfs_buf_t *bp)
314{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000315 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_dqblk_t *d;
317 int curid, i;
318
319 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200320 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Chandra Seetharaman62926042011-07-22 23:40:15 +0000322 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /*
325 * ID of the first dquot in the block - id's are zero based.
326 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000327 curid = id - (id % q->qi_dqperchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 ASSERT(curid >= 0);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000329 memset(d, 0, BBTOB(q->qi_dqchunklen));
330 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 xfs_qm_dqinit_core(curid, type, d);
332 xfs_trans_dquot_buf(tp, bp,
Dave Chinnerc1155412010-05-07 11:05:19 +1000333 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
334 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
335 XFS_BLF_GDQUOT_BUF)));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000336 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339
340
341/*
342 * Allocate a block and fill it with dquots.
343 * This is called when the bmapi finds a hole.
344 */
345STATIC int
346xfs_qm_dqalloc(
Tim Shimminefa092f2005-09-05 08:29:01 +1000347 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 xfs_mount_t *mp,
349 xfs_dquot_t *dqp,
350 xfs_inode_t *quotip,
351 xfs_fileoff_t offset_fsb,
352 xfs_buf_t **O_bpp)
353{
354 xfs_fsblock_t firstblock;
355 xfs_bmap_free_t flist;
356 xfs_bmbt_irec_t map;
357 int nmaps, error, committed;
358 xfs_buf_t *bp;
Tim Shimminefa092f2005-09-05 08:29:01 +1000359 xfs_trans_t *tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 ASSERT(tp != NULL);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000362
363 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /*
366 * Initialize the bmap freelist prior to calling bmapi code.
367 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600368 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 xfs_ilock(quotip, XFS_ILOCK_EXCL);
370 /*
371 * Return if this type of quotas is turned off while we didn't
372 * have an inode lock
373 */
374 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
375 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
376 return (ESRCH);
377 }
378
Christoph Hellwigddc34152011-09-19 15:00:54 +0000379 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 nmaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000381 error = xfs_bmapi_write(tp, quotip, offset_fsb,
382 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
383 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
384 &map, &nmaps, &flist);
385 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
388 ASSERT(nmaps == 1);
389 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
390 (map.br_startblock != HOLESTARTBLOCK));
391
392 /*
393 * Keep track of the blkno to save a lookup later
394 */
395 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
396
397 /* now we can just get the buffer (there's nothing to read yet) */
398 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
399 dqp->q_blkno,
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000400 mp->m_quotainfo->qi_dqchunklen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000402
403 error = xfs_buf_geterror(bp);
404 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto error1;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
408 * Make a chunk of dquots out of this buffer and log
409 * the entire thing.
410 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100411 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000412 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Tim Shimminefa092f2005-09-05 08:29:01 +1000414 /*
415 * xfs_bmap_finish() may commit the current transaction and
416 * start a second transaction if the freelist is not empty.
417 *
418 * Since we still want to modify this buffer, we need to
419 * ensure that the buffer is not released on commit of
420 * the first transaction and ensure the buffer is added to the
421 * second transaction.
422 *
423 * If there is only one transaction then don't stop the buffer
424 * from being released when it commits later on.
425 */
426
427 xfs_trans_bhold(tp, bp);
428
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100429 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto error1;
431 }
432
Tim Shimminefa092f2005-09-05 08:29:01 +1000433 if (committed) {
434 tp = *tpp;
435 xfs_trans_bjoin(tp, bp);
436 } else {
437 xfs_trans_bhold_release(tp, bp);
438 }
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *O_bpp = bp;
441 return 0;
442
443 error1:
444 xfs_bmap_cancel(&flist);
445 error0:
446 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
447
448 return (error);
449}
450
451/*
452 * Maps a dquot to the buffer containing its on-disk version.
453 * This returns a ptr to the buffer containing the on-disk dquot
454 * in the bpp param, and a ptr to the on-disk dquot within that buffer
455 */
456STATIC int
457xfs_qm_dqtobp(
Tim Shimminefa092f2005-09-05 08:29:01 +1000458 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 xfs_dquot_t *dqp,
460 xfs_disk_dquot_t **O_ddpp,
461 xfs_buf_t **O_bpp,
462 uint flags)
463{
464 xfs_bmbt_irec_t map;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000465 int nmaps = 1, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 xfs_buf_t *bp;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000467 xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
468 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_disk_dquot_t *ddq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000470 xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
Tim Shimminefa092f2005-09-05 08:29:01 +1000471 xfs_trans_t *tp = (tpp ? *tpp : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000473 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000475 xfs_ilock(quotip, XFS_ILOCK_SHARED);
476 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000478 * Return if this type of quotas is turned off while we
479 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000482 return ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000486 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000488 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
489 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000490
491 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
492 if (error)
493 return error;
494
495 ASSERT(nmaps == 1);
496 ASSERT(map.br_blockcount == 1);
497
498 /*
499 * Offset of dquot in the (fixed sized) dquot chunk.
500 */
501 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
502 sizeof(xfs_dqblk_t);
503
504 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
505 if (map.br_startblock == HOLESTARTBLOCK) {
506 /*
507 * We don't allocate unless we're asked to
508 */
509 if (!(flags & XFS_QMOPT_DQALLOC))
510 return ENOENT;
511
512 ASSERT(tp);
513 error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
514 dqp->q_fileoffset, &bp);
515 if (error)
516 return error;
517 tp = *tpp;
518 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000519 trace_xfs_dqtobp_read(dqp);
520
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000521 /*
522 * store the blkno etc so that we don't have to do the
523 * mapping all the time
524 */
525 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
526
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000527 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
528 dqp->q_blkno,
529 mp->m_quotainfo->qi_dqchunklen,
530 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (error || !bp)
532 return XFS_ERROR(error);
533 }
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000534
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200535 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /*
538 * calculate the location of the dquot inside the buffer.
539 */
Chandra Seetharaman62926042011-07-22 23:40:15 +0000540 ddq = bp->b_addr + dqp->q_bufoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * A simple sanity check in case we got a corrupted dquot...
544 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100545 error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100547 "dqtobp");
548 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!(flags & XFS_QMOPT_DQREPAIR)) {
550 xfs_trans_brelse(tp, bp);
551 return XFS_ERROR(EIO);
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
555 *O_bpp = bp;
556 *O_ddpp = ddq;
557
558 return (0);
559}
560
561
562/*
563 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
564 * and release the buffer immediately.
565 *
566 */
567/* ARGSUSED */
568STATIC int
569xfs_qm_dqread(
Tim Shimminefa092f2005-09-05 08:29:01 +1000570 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 xfs_dqid_t id,
572 xfs_dquot_t *dqp, /* dquot to get filled in */
573 uint flags)
574{
575 xfs_disk_dquot_t *ddqp;
576 xfs_buf_t *bp;
577 int error;
Tim Shimminefa092f2005-09-05 08:29:01 +1000578 xfs_trans_t *tp;
579
580 ASSERT(tpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000582 trace_xfs_dqread(dqp);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * get a pointer to the on-disk dquot and the buffer containing it
586 * dqp already knows its own type (GROUP/USER).
587 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000588 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return (error);
590 }
Tim Shimminefa092f2005-09-05 08:29:01 +1000591 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* copy everything from disk dquot to the incore dquot */
594 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
Christoph Hellwig1149d962005-11-02 15:01:12 +1100595 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 xfs_qm_dquot_logitem_init(dqp);
597
598 /*
599 * Reservation counters are defined as reservation plus current usage
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300600 * to avoid having to add every time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100602 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
603 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
604 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Mark the buf so that this will stay incore a little longer */
Christoph Hellwig38f23232011-10-10 16:52:45 +0000607 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
611 * So we need to release with xfs_trans_brelse().
612 * The strategy here is identical to that of inodes; we lock
613 * the dquot in xfs_qm_dqget() before making it accessible to
614 * others. This is because dquots, like inodes, need a good level of
615 * concurrency, and we don't want to take locks on the entire buffers
616 * for dquot accesses.
617 * Note also that the dquot buffer may even be dirty at this point, if
618 * this particular dquot was repaired. We still aren't afraid to
619 * brelse it because we have the changes incore.
620 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200621 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xfs_trans_brelse(tp, bp);
623
624 return (error);
625}
626
627
628/*
629 * allocate an incore dquot from the kernel heap,
630 * and fill its core with quota information kept on disk.
631 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
632 * if it wasn't already allocated.
633 */
634STATIC int
635xfs_qm_idtodq(
636 xfs_mount_t *mp,
637 xfs_dqid_t id, /* gid or uid, depending on type */
638 uint type, /* UDQUOT or GDQUOT */
639 uint flags, /* DQALLOC, DQREPAIR */
640 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
641{
642 xfs_dquot_t *dqp;
643 int error;
644 xfs_trans_t *tp;
645 int cancelflags=0;
646
647 dqp = xfs_qm_dqinit(mp, id, type);
648 tp = NULL;
649 if (flags & XFS_QMOPT_DQALLOC) {
650 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000651 error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
652 XFS_WRITE_LOG_RES(mp) +
653 BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
654 128,
655 0,
656 XFS_TRANS_PERM_LOG_RES,
657 XFS_WRITE_LOG_COUNT);
658 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 cancelflags = 0;
660 goto error0;
661 }
662 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
663 }
664
665 /*
666 * Read it from disk; xfs_dqread() takes care of
667 * all the necessary initialization of dquot's fields (locks, etc)
668 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000669 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * This can happen if quotas got turned off (ESRCH),
672 * or if the dquot didn't exist on disk and we ask to
673 * allocate (ENOENT).
674 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000675 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 cancelflags |= XFS_TRANS_ABORT;
677 goto error0;
678 }
679 if (tp) {
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000680 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto error1;
682 }
683
684 *O_dqpp = dqp;
685 return (0);
686
687 error0:
688 ASSERT(error);
689 if (tp)
690 xfs_trans_cancel(tp, cancelflags);
691 error1:
692 xfs_qm_dqdestroy(dqp);
693 *O_dqpp = NULL;
694 return (error);
695}
696
697/*
698 * Lookup a dquot in the incore dquot hashtable. We keep two separate
699 * hashtables for user and group dquots; and, these are global tables
700 * inside the XQM, not per-filesystem tables.
701 * The hash chain must be locked by caller, and it is left locked
702 * on return. Returning dquot is locked.
703 */
704STATIC int
705xfs_qm_dqlookup(
706 xfs_mount_t *mp,
707 xfs_dqid_t id,
708 xfs_dqhash_t *qh,
709 xfs_dquot_t **O_dqpp)
710{
711 xfs_dquot_t *dqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100713 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /*
716 * Traverse the hashchain looking for a match
717 */
Dave Chinnere6a81f12010-04-13 15:06:51 +1000718 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
720 * We already have the hashlock. We don't need the
721 * dqlock to look at the id field of the dquot, since the
722 * id can't be modified without the hashlock anyway.
723 */
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000724 if (be32_to_cpu(dqp->q_core.d_id) != id || dqp->q_mount != mp)
725 continue;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000726
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000727 trace_xfs_dqlookup_found(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000729 xfs_dqlock(dqp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000730 if (dqp->dq_flags & XFS_DQ_FREEING) {
731 *O_dqpp = NULL;
732 xfs_dqunlock(dqp);
733 return -1;
734 }
735
Christoph Hellwig78e55892011-12-06 21:58:22 +0000736 dqp->q_nrefs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000738 /*
739 * move the dquot to the front of the hashchain
740 */
741 list_move(&dqp->q_hashlist, &qh->qh_list);
742 trace_xfs_dqlookup_done(dqp);
743 *O_dqpp = dqp;
744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 *O_dqpp = NULL;
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000748 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
753 * a locked dquot, doing an allocation (if requested) as needed.
754 * When both an inode and an id are given, the inode's id takes precedence.
755 * That is, if the id changes while we don't hold the ilock inside this
756 * function, the new dquot is returned, not necessarily the one requested
757 * in the id argument.
758 */
759int
760xfs_qm_dqget(
761 xfs_mount_t *mp,
762 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000763 xfs_dqid_t id, /* uid/projid/gid depending on type */
764 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
766 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
767{
768 xfs_dquot_t *dqp;
769 xfs_dqhash_t *h;
770 uint version;
771 int error;
772
773 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
774 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000775 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
777 return (ESRCH);
778 }
779 h = XFS_DQ_HASH(mp, id, type);
780
781#ifdef DEBUG
782 if (xfs_do_dqerror) {
783 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
784 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100785 xfs_debug(mp, "Returning error in dqget");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return (EIO);
787 }
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000790 ASSERT(type == XFS_DQ_USER ||
791 type == XFS_DQ_PROJ ||
792 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (ip) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000794 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (type == XFS_DQ_USER)
796 ASSERT(ip->i_udquot == NULL);
797 else
798 ASSERT(ip->i_gdquot == NULL);
799 }
800#endif
Christoph Hellwig92678552011-12-06 21:58:18 +0000801
802restart:
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100803 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /*
806 * Look in the cache (hashtable).
807 * The chain is kept locked during lookup.
808 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000809 switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) {
810 case -1:
811 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
812 mutex_unlock(&h->qh_lock);
813 delay(1);
814 goto restart;
815 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
817 /*
818 * The dquot was found, moved to the front of the chain,
819 * taken off the freelist if it was on it, and locked
820 * at this point. Just unlock the hashchain and return.
821 */
822 ASSERT(*O_dqpp);
823 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100824 mutex_unlock(&h->qh_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000825 trace_xfs_dqget_hit(*O_dqpp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000826 return 0; /* success */
827 default:
828 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
833 * Dquot cache miss. We don't want to keep the inode lock across
834 * a (potential) disk read. Also we don't want to deal with the lock
835 * ordering between quotainode and this inode. OTOH, dropping the inode
836 * lock here means dealing with a chown that can happen before
837 * we re-acquire the lock.
838 */
839 if (ip)
840 xfs_iunlock(ip, XFS_ILOCK_EXCL);
841 /*
842 * Save the hashchain version stamp, and unlock the chain, so that
843 * we don't keep the lock across a disk read
844 */
845 version = h->qh_version;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100846 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /*
849 * Allocate the dquot on the kernel heap, and read the ondisk
850 * portion off the disk. Also, do all the necessary initialization
851 * This can return ENOENT if dquot didn't exist on disk and we didn't
852 * ask it to allocate; ESRCH if quotas got turned off suddenly.
853 */
854 if ((error = xfs_qm_idtodq(mp, id, type,
855 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
856 XFS_QMOPT_DOWARN),
857 &dqp))) {
858 if (ip)
859 xfs_ilock(ip, XFS_ILOCK_EXCL);
860 return (error);
861 }
862
863 /*
864 * See if this is mount code calling to look at the overall quota limits
865 * which are stored in the id == 0 user or group's dquot.
866 * Since we may not have done a quotacheck by this point, just return
867 * the dquot without attaching it to any hashtables, lists, etc, or even
868 * taking a reference.
869 * The caller must dqdestroy this once done.
870 */
871 if (flags & XFS_QMOPT_DQSUSER) {
872 ASSERT(id == 0);
873 ASSERT(! ip);
874 goto dqret;
875 }
876
877 /*
878 * Dquot lock comes after hashlock in the lock ordering
879 */
880 if (ip) {
881 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig191f8482010-04-20 17:01:53 +1000882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /*
884 * A dquot could be attached to this inode by now, since
885 * we had dropped the ilock.
886 */
887 if (type == XFS_DQ_USER) {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000888 if (!XFS_IS_UQUOTA_ON(mp)) {
889 /* inode stays locked on return */
890 xfs_qm_dqdestroy(dqp);
891 return XFS_ERROR(ESRCH);
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (ip->i_udquot) {
894 xfs_qm_dqdestroy(dqp);
895 dqp = ip->i_udquot;
896 xfs_dqlock(dqp);
897 goto dqret;
898 }
899 } else {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000900 if (!XFS_IS_OQUOTA_ON(mp)) {
901 /* inode stays locked on return */
902 xfs_qm_dqdestroy(dqp);
903 return XFS_ERROR(ESRCH);
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (ip->i_gdquot) {
906 xfs_qm_dqdestroy(dqp);
907 dqp = ip->i_gdquot;
908 xfs_dqlock(dqp);
909 goto dqret;
910 }
911 }
912 }
913
914 /*
915 * Hashlock comes after ilock in lock order
916 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100917 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (version != h->qh_version) {
919 xfs_dquot_t *tmpdqp;
920 /*
921 * Now, see if somebody else put the dquot in the
922 * hashtable before us. This can happen because we didn't
923 * keep the hashchain lock. We don't have to worry about
924 * lock order between the two dquots here since dqp isn't
925 * on any findable lists yet.
926 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000927 switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) {
928 case 0:
929 case -1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
Christoph Hellwig92678552011-12-06 21:58:18 +0000931 * Duplicate found, either in cache or on its way out.
932 * Just throw away the new dquot and start over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000934 if (tmpdqp)
935 xfs_qm_dqput(tmpdqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100936 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 xfs_qm_dqdestroy(dqp);
938 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
Christoph Hellwig92678552011-12-06 21:58:18 +0000939 goto restart;
940 default:
941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 }
944
945 /*
946 * Put the dquot at the beginning of the hash-chain and mp's list
947 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
948 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100949 ASSERT(mutex_is_locked(&h->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 dqp->q_hash = h;
Dave Chinnere6a81f12010-04-13 15:06:51 +1000951 list_add(&dqp->q_hashlist, &h->qh_list);
952 h->qh_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /*
955 * Attach this dquot to this filesystem's list of all dquots,
956 * kept inside the mount structure in m_quotainfo field
957 */
Dave Chinner3a254042010-04-13 15:06:48 +1000958 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
961 * We return a locked dquot to the caller, with a reference taken
962 */
963 xfs_dqlock(dqp);
964 dqp->q_nrefs = 1;
965
Dave Chinner3a254042010-04-13 15:06:48 +1000966 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
967 mp->m_quotainfo->qi_dquots++;
968 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100969 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 dqret:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000971 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000972 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *O_dqpp = dqp;
974 return (0);
975}
976
977
978/*
979 * Release a reference to the dquot (decrement ref-count)
980 * and unlock it. If there is a group quota attached to this
981 * dquot, carefully release that too without tripping over
982 * deadlocks'n'stuff.
983 */
984void
985xfs_qm_dqput(
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000986 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000988 struct xfs_dquot *gdqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 ASSERT(dqp->q_nrefs > 0);
991 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000992
993 trace_xfs_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000995recurse:
996 if (--dqp->q_nrefs > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 xfs_dqunlock(dqp);
998 return;
999 }
1000
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001001 trace_xfs_dqput_free(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001003 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
1004 if (list_empty(&dqp->q_freelist)) {
1005 list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
1006 xfs_Gqm->qm_dqfrlist_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Dave Chinner3a8406f2010-04-13 15:06:52 +10001008 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Christoph Hellwigbf72de32011-12-06 21:58:19 +00001009
1010 /*
1011 * If we just added a udquot to the freelist, then we want to release
1012 * the gdquot reference that it (probably) has. Otherwise it'll keep
1013 * the gdquot from getting reclaimed.
1014 */
1015 gdqp = dqp->q_gdquot;
1016 if (gdqp) {
1017 xfs_dqlock(gdqp);
1018 dqp->q_gdquot = NULL;
1019 }
1020 xfs_dqunlock(dqp);
1021
1022 /*
1023 * If we had a group quota hint, release it now.
1024 */
1025 if (gdqp) {
1026 dqp = gdqp;
1027 goto recurse;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/*
1032 * Release a dquot. Flush it if dirty, then dqput() it.
1033 * dquot must not be locked.
1034 */
1035void
1036xfs_qm_dqrele(
1037 xfs_dquot_t *dqp)
1038{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001039 if (!dqp)
1040 return;
1041
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001042 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 xfs_dqlock(dqp);
1045 /*
1046 * We don't care to flush it if the dquot is dirty here.
1047 * That will create stutters that we want to avoid.
1048 * Instead we do a delayed write when we try to reclaim
1049 * a dirty dquot. Also xfs_sync will take part of the burden...
1050 */
1051 xfs_qm_dqput(dqp);
1052}
1053
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001054/*
1055 * This is the dquot flushing I/O completion routine. It is called
1056 * from interrupt level when the buffer containing the dquot is
1057 * flushed to disk. It is responsible for removing the dquot logitem
1058 * from the AIL if it has not been re-logged, and unlocking the dquot's
1059 * flush lock. This behavior is very similar to that of inodes..
1060 */
1061STATIC void
1062xfs_qm_dqflush_done(
1063 struct xfs_buf *bp,
1064 struct xfs_log_item *lip)
1065{
1066 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
1067 xfs_dquot_t *dqp = qip->qli_dquot;
1068 struct xfs_ail *ailp = lip->li_ailp;
1069
1070 /*
1071 * We only want to pull the item from the AIL if its
1072 * location in the log has not changed since we started the flush.
1073 * Thus, we only bother if the dquot's lsn has
1074 * not changed. First we check the lsn outside the lock
1075 * since it's cheaper, and then we recheck while
1076 * holding the lock before removing the dquot from the AIL.
1077 */
1078 if ((lip->li_flags & XFS_LI_IN_AIL) &&
1079 lip->li_lsn == qip->qli_flush_lsn) {
1080
1081 /* xfs_trans_ail_delete() drops the AIL lock. */
1082 spin_lock(&ailp->xa_lock);
1083 if (lip->li_lsn == qip->qli_flush_lsn)
1084 xfs_trans_ail_delete(ailp, lip);
1085 else
1086 spin_unlock(&ailp->xa_lock);
1087 }
1088
1089 /*
1090 * Release the dq's flush lock since we're done with it.
1091 */
1092 xfs_dqfunlock(dqp);
1093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095/*
1096 * Write a modified dquot to disk.
1097 * The dquot must be locked and the flush lock too taken by caller.
1098 * The flush lock will not be unlocked until the dquot reaches the disk,
1099 * but the dquot is free to be unlocked and modified by the caller
1100 * in the interim. Dquot is still locked on return. This behavior is
1101 * identical to that of inodes.
1102 */
1103int
1104xfs_qm_dqflush(
1105 xfs_dquot_t *dqp,
1106 uint flags)
1107{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001108 struct xfs_mount *mp = dqp->q_mount;
1109 struct xfs_buf *bp;
1110 struct xfs_disk_dquot *ddqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001114 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001115
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001116 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001119 * If not dirty, or it's pinned and we are not supposed to block, nada.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
David Chinner2f8a3ce2008-10-30 17:07:20 +11001121 if (!XFS_DQ_IS_DIRTY(dqp) ||
Christoph Hellwigfdedf282011-12-06 21:58:10 +00001122 ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 xfs_dqfunlock(dqp);
David Chinner2f8a3ce2008-10-30 17:07:20 +11001124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 xfs_qm_dqunpin_wait(dqp);
1127
1128 /*
1129 * This may have been unpinned because the filesystem is shutting
1130 * down forcibly. If that's the case we must not write this dquot
1131 * to disk, because the log record didn't make it to disk!
1132 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001133 if (XFS_FORCED_SHUTDOWN(mp)) {
1134 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 xfs_dqfunlock(dqp);
1136 return XFS_ERROR(EIO);
1137 }
1138
1139 /*
1140 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001142 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1143 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
1144 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ASSERT(error != ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 xfs_dqfunlock(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001147 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001150 /*
1151 * Calculate the location of the dquot inside the buffer.
1152 */
Chandra Seetharaman62926042011-07-22 23:40:15 +00001153 ddqp = bp->b_addr + dqp->q_bufoffset;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001154
1155 /*
1156 * A simple sanity check in case we got a corrupted dquot..
1157 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001158 error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
1159 XFS_QMOPT_DOWARN, "dqflush (incore copy)");
1160 if (error) {
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001161 xfs_buf_relse(bp);
1162 xfs_dqfunlock(dqp);
1163 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return XFS_ERROR(EIO);
1165 }
1166
1167 /* This is the only portion of data that needs to persist */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001168 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * Clear the dirty field and remember the flush lsn for later use.
1172 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001173 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
David Chinner7b2e2a32008-10-30 17:39:12 +11001175 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1176 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /*
1179 * Attach an iodone routine so that we can remove this dquot from the
1180 * AIL and release the flush lock once the dquot is synced to disk.
1181 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001182 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1183 &dqp->q_logitem.qli_item);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * If the buffer is pinned then push on the log so we won't
1187 * get stuck waiting in the write for too long.
1188 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001189 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001190 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001191 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Dave Chinner20026d92010-02-04 09:48:58 +11001194 if (flags & SYNC_WAIT)
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001195 error = xfs_bwrite(bp);
1196 else
Christoph Hellwig61551f12011-08-23 08:28:06 +00001197 xfs_buf_delwri_queue(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001198
1199 xfs_buf_relse(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001200
1201 trace_xfs_dqflush_done(dqp);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
1204 * dqp is still locked, but caller is free to unlock it now.
1205 */
Dave Chinner20026d92010-02-04 09:48:58 +11001206 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210void
1211xfs_dqunlock(
1212 xfs_dquot_t *dqp)
1213{
Christoph Hellwig800b4842011-12-06 21:58:14 +00001214 xfs_dqunlock_nonotify(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (dqp->q_logitem.qli_dquot == dqp) {
David Chinner783a2f62008-10-30 17:39:58 +11001216 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
Christoph Hellwig800b4842011-12-06 21:58:14 +00001217 &dqp->q_logitem.qli_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219}
1220
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001221/*
1222 * Lock two xfs_dquot structures.
1223 *
1224 * To avoid deadlocks we always lock the quota structure with
1225 * the lowerd id first.
1226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227void
1228xfs_dqlock2(
1229 xfs_dquot_t *d1,
1230 xfs_dquot_t *d2)
1231{
1232 if (d1 && d2) {
1233 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001234 if (be32_to_cpu(d1->q_core.d_id) >
1235 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001236 mutex_lock(&d2->q_qlock);
1237 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001239 mutex_lock(&d1->q_qlock);
1240 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001242 } else if (d1) {
1243 mutex_lock(&d1->q_qlock);
1244 } else if (d2) {
1245 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
Christoph Hellwig92678552011-12-06 21:58:18 +00001250 * Take a dquot out of the mount's dqlist as well as the hashlist. This is
1251 * called via unmount as well as quotaoff, and the purge will always succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Christoph Hellwig92678552011-12-06 21:58:18 +00001253void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254xfs_qm_dqpurge(
Christoph Hellwig92678552011-12-06 21:58:18 +00001255 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Christoph Hellwig92678552011-12-06 21:58:18 +00001257 struct xfs_mount *mp = dqp->q_mount;
1258 struct xfs_dqhash *qh = dqp->q_hash;
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 xfs_dqlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /*
1263 * If we're turning off quotas, we have to make sure that, for
1264 * example, we don't delete quota disk blocks while dquots are
1265 * in the process of getting written to those disk blocks.
1266 * This dquot might well be on AIL, and we can't leave it there
1267 * if we're turning off quotas. Basically, we need this flush
1268 * lock, and are willing to block on it.
1269 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001270 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Block on the flush lock after nudging dquot buffer,
1273 * if it is incore.
1274 */
Christoph Hellwig800b4842011-12-06 21:58:14 +00001275 xfs_dqflock_pushbuf_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 /*
Christoph Hellwig92678552011-12-06 21:58:18 +00001279 * If we are turning this type of quotas off, we don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * about the dirty metadata sitting in this dquot. OTOH, if
1281 * we're unmounting, we do care, so we flush it and wait.
1282 */
1283 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10001284 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * We don't care about getting disk errors here. We need
1288 * to purge this dquot anyway, so we go ahead regardless.
1289 */
Dave Chinner20026d92010-02-04 09:48:58 +11001290 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
David Chinner3c568362008-04-10 12:20:24 +10001291 if (error)
Dave Chinner53487782011-03-07 10:05:35 +11001292 xfs_warn(mp, "%s: dquot %p flush failed",
1293 __func__, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 xfs_dqflock(dqp);
1295 }
Christoph Hellwig92678552011-12-06 21:58:18 +00001296
Peter Leckiebc3048e2008-10-30 17:05:04 +11001297 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1299 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 xfs_dqfunlock(dqp);
1302 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Christoph Hellwig92678552011-12-06 21:58:18 +00001304 mutex_lock(&qh->qh_lock);
1305 list_del_init(&dqp->q_hashlist);
1306 qh->qh_version++;
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001307 mutex_unlock(&qh->qh_lock);
1308
Christoph Hellwig92678552011-12-06 21:58:18 +00001309 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
1310 list_del_init(&dqp->q_mplist);
1311 mp->m_quotainfo->qi_dqreclaims++;
1312 mp->m_quotainfo->qi_dquots--;
1313 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
1314
1315 /*
1316 * We move dquots to the freelist as soon as their reference count
1317 * hits zero, so it really should be on the freelist here.
1318 */
1319 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
1320 ASSERT(!list_empty(&dqp->q_freelist));
1321 list_del_init(&dqp->q_freelist);
1322 xfs_Gqm->qm_dqfrlist_cnt--;
1323 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
1324
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001325 xfs_qm_dqdestroy(dqp);
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
1329 * Give the buffer a little push if it is incore and
1330 * wait on the flush lock.
1331 */
1332void
Christoph Hellwig800b4842011-12-06 21:58:14 +00001333xfs_dqflock_pushbuf_wait(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 xfs_dquot_t *dqp)
1335{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001336 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 xfs_buf_t *bp;
1338
1339 /*
1340 * Check to see if the dquot has been flushed delayed
1341 * write. If so, grab its buffer and send it
1342 * out immediately. We'll be able to acquire
1343 * the flush lock when the I/O completes.
1344 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001345 bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
1346 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001347 if (!bp)
1348 goto out_lock;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001349
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001350 if (XFS_BUF_ISDELAYWRITE(bp)) {
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001351 if (xfs_buf_ispinned(bp))
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001352 xfs_log_force(mp, 0);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001353 xfs_buf_delwri_promote(bp);
1354 wake_up_process(bp->b_target->bt_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001356 xfs_buf_relse(bp);
1357out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 xfs_dqflock(dqp);
1359}