blob: f152af8cfab04175499bae571a43c836e6662f15 [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
42
43/*
44 LOCK ORDER
45
46 inode lock (ilock)
47 dquot hash-chain lock (hashlock)
48 xqm dquot freelist lock (freelistlock
49 mount's dquot list lock (mplistlock)
50 user dquot lock - lock ordering among dquots is based on the uid or gid
51 group dquot lock - similar to udquots. Between the two dquots, the udquot
52 has to be locked first.
53 pin lock - the dquot lock must be held to take this lock.
54 flush lock - ditto.
55*/
56
57STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
58
59#ifdef DEBUG
60xfs_buftarg_t *xfs_dqerror_target;
61int xfs_do_dqerror;
62int xfs_dqreq_num;
63int xfs_dqerror_mod = 33;
64#endif
65
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010066static struct lock_class_key xfs_dquot_other_class;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Allocate and initialize a dquot. We don't always allocate fresh memory;
70 * we try to reclaim a free dquot if the number of incore dquots are above
71 * a threshold.
72 * The only field inside the core that gets initialized at this point
73 * is the d_id field. The idea is to fill in the entire q_core
74 * when we read in the on disk dquot.
75 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100076STATIC xfs_dquot_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070077xfs_qm_dqinit(
78 xfs_mount_t *mp,
79 xfs_dqid_t id,
80 uint type)
81{
82 xfs_dquot_t *dqp;
83 boolean_t brandnewdquot;
84
85 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
86 dqp->dq_flags = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +110087 dqp->q_core.d_id = cpu_to_be32(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dqp->q_mount = mp;
89
90 /*
91 * No need to re-initialize these if this is a reclaimed dquot.
92 */
93 if (brandnewdquot) {
Dave Chinner3a8406f2010-04-13 15:06:52 +100094 INIT_LIST_HEAD(&dqp->q_freelist);
Jes Sorensen794ee1b2006-01-09 15:59:21 -080095 mutex_init(&dqp->q_qlock);
Peter Leckiebc3048e2008-10-30 17:05:04 +110096 init_waitqueue_head(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David Chinnere1f49cf2008-08-13 16:41:43 +100098 /*
99 * Because we want to use a counting completion, complete
100 * the flush completion once to allow a single access to
101 * the flush completion without blocking.
102 */
103 init_completion(&dqp->q_flush);
104 complete(&dqp->q_flush);
105
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000106 trace_xfs_dqinit(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 } else {
108 /*
109 * Only the q_core portion was zeroed in dqreclaim_one().
110 * So, we need to reset others.
111 */
Dave Chinner3a8406f2010-04-13 15:06:52 +1000112 dqp->q_nrefs = 0;
113 dqp->q_blkno = 0;
114 INIT_LIST_HEAD(&dqp->q_mplist);
115 INIT_LIST_HEAD(&dqp->q_hashlist);
116 dqp->q_bufoffset = 0;
117 dqp->q_fileoffset = 0;
118 dqp->q_transp = NULL;
119 dqp->q_gdquot = NULL;
120 dqp->q_res_bcount = 0;
121 dqp->q_res_icount = 0;
122 dqp->q_res_rtbcount = 0;
123 atomic_set(&dqp->q_pincount, 0);
124 dqp->q_hash = NULL;
125 ASSERT(list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000127 trace_xfs_dqreuse(dqp);
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +0100128 }
129
130 /*
131 * In either case we need to make sure group quotas have a different
132 * lock class than user quotas, to make sure lockdep knows we can
133 * locks of one of each at the same time.
134 */
135 if (!(type & XFS_DQ_USER))
136 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /*
139 * log item gets initialized later
140 */
141 return (dqp);
142}
143
144/*
145 * This is called to free all the memory associated with a dquot
146 */
147void
148xfs_qm_dqdestroy(
149 xfs_dquot_t *dqp)
150{
Dave Chinner3a8406f2010-04-13 15:06:52 +1000151 ASSERT(list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 mutex_destroy(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 sv_destroy(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 atomic_dec(&xfs_Gqm->qm_totaldquots);
158}
159
160/*
161 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
162 */
163STATIC void
164xfs_qm_dqinit_core(
165 xfs_dqid_t id,
166 uint type,
167 xfs_dqblk_t *d)
168{
169 /*
170 * Caller has zero'd the entire dquot 'chunk' already.
171 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100172 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
173 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
174 d->dd_diskdq.d_id = cpu_to_be32(id);
175 d->dd_diskdq.d_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * If default limits are in force, push them into the dquot now.
180 * We overwrite the dquot limits only if they are zero and this
181 * is not the root dquot.
182 */
183void
184xfs_qm_adjust_dqlimits(
185 xfs_mount_t *mp,
186 xfs_disk_dquot_t *d)
187{
188 xfs_quotainfo_t *q = mp->m_quotainfo;
189
190 ASSERT(d->d_id);
191
192 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100193 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100195 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (q->qi_isoftlimit && !d->d_ino_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100197 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100199 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100201 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100203 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/*
207 * Check the limits and timers of a dquot and start or reset timers
208 * if necessary.
209 * This gets called even when quota enforcement is OFF, which makes our
210 * life a little less complicated. (We just don't reject any quota
211 * reservations in that case, when enforcement is off).
212 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
213 * enforcement's off.
214 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000215 * 'automatically' get started when limits get exceeded. They do
216 * get reset to zero, however, when we find the count to be under
217 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
219void
220xfs_qm_adjust_dqtimers(
221 xfs_mount_t *mp,
222 xfs_disk_dquot_t *d)
223{
224 ASSERT(d->d_id);
225
226#ifdef QUOTADEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100227 if (d->d_blk_hardlimit)
228 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
229 be64_to_cpu(d->d_blk_hardlimit));
230 if (d->d_ino_hardlimit)
231 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
232 be64_to_cpu(d->d_ino_hardlimit));
233 if (d->d_rtb_hardlimit)
234 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
235 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#endif
237 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100238 if ((d->d_blk_softlimit &&
239 (be64_to_cpu(d->d_bcount) >=
240 be64_to_cpu(d->d_blk_softlimit))) ||
241 (d->d_blk_hardlimit &&
242 (be64_to_cpu(d->d_bcount) >=
243 be64_to_cpu(d->d_blk_hardlimit)))) {
244 d->d_btimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000245 mp->m_quotainfo->qi_btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000246 } else {
247 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 } else {
250 if ((!d->d_blk_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100251 (be64_to_cpu(d->d_bcount) <
252 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 (!d->d_blk_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100254 (be64_to_cpu(d->d_bcount) <
255 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 d->d_btimer = 0;
257 }
258 }
259
260 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100261 if ((d->d_ino_softlimit &&
262 (be64_to_cpu(d->d_icount) >=
263 be64_to_cpu(d->d_ino_softlimit))) ||
264 (d->d_ino_hardlimit &&
265 (be64_to_cpu(d->d_icount) >=
266 be64_to_cpu(d->d_ino_hardlimit)))) {
267 d->d_itimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000268 mp->m_quotainfo->qi_itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000269 } else {
270 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 } else {
273 if ((!d->d_ino_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100274 (be64_to_cpu(d->d_icount) <
275 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 (!d->d_ino_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100277 (be64_to_cpu(d->d_icount) <
278 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 d->d_itimer = 0;
280 }
281 }
282
283 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100284 if ((d->d_rtb_softlimit &&
285 (be64_to_cpu(d->d_rtbcount) >=
286 be64_to_cpu(d->d_rtb_softlimit))) ||
287 (d->d_rtb_hardlimit &&
288 (be64_to_cpu(d->d_rtbcount) >=
289 be64_to_cpu(d->d_rtb_hardlimit)))) {
290 d->d_rtbtimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000291 mp->m_quotainfo->qi_rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000292 } else {
293 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 } else {
296 if ((!d->d_rtb_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100297 (be64_to_cpu(d->d_rtbcount) <
298 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 (!d->d_rtb_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100300 (be64_to_cpu(d->d_rtbcount) <
301 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 d->d_rtbtimer = 0;
303 }
304 }
305}
306
307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * initialize a buffer full of dquots and log the whole thing
309 */
310STATIC void
311xfs_qm_init_dquot_blk(
312 xfs_trans_t *tp,
313 xfs_mount_t *mp,
314 xfs_dqid_t id,
315 uint type,
316 xfs_buf_t *bp)
317{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000318 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 xfs_dqblk_t *d;
320 int curid, i;
321
322 ASSERT(tp);
323 ASSERT(XFS_BUF_ISBUSY(bp));
324 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
325
326 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
327
328 /*
329 * ID of the first dquot in the block - id's are zero based.
330 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000331 curid = id - (id % q->qi_dqperchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ASSERT(curid >= 0);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000333 memset(d, 0, BBTOB(q->qi_dqchunklen));
334 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 xfs_qm_dqinit_core(curid, type, d);
336 xfs_trans_dquot_buf(tp, bp,
Dave Chinnerc1155412010-05-07 11:05:19 +1000337 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
338 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
339 XFS_BLF_GDQUOT_BUF)));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000340 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343
344
345/*
346 * Allocate a block and fill it with dquots.
347 * This is called when the bmapi finds a hole.
348 */
349STATIC int
350xfs_qm_dqalloc(
Tim Shimminefa092f2005-09-05 08:29:01 +1000351 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_mount_t *mp,
353 xfs_dquot_t *dqp,
354 xfs_inode_t *quotip,
355 xfs_fileoff_t offset_fsb,
356 xfs_buf_t **O_bpp)
357{
358 xfs_fsblock_t firstblock;
359 xfs_bmap_free_t flist;
360 xfs_bmbt_irec_t map;
361 int nmaps, error, committed;
362 xfs_buf_t *bp;
Tim Shimminefa092f2005-09-05 08:29:01 +1000363 xfs_trans_t *tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 ASSERT(tp != NULL);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000366
367 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /*
370 * Initialize the bmap freelist prior to calling bmapi code.
371 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600372 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 xfs_ilock(quotip, XFS_ILOCK_EXCL);
374 /*
375 * Return if this type of quotas is turned off while we didn't
376 * have an inode lock
377 */
378 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
379 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
380 return (ESRCH);
381 }
382
383 /*
384 * xfs_trans_commit normally decrements the vnode ref count
385 * when it unlocks the inode. Since we want to keep the quota
386 * inode around, we bump the vnode ref count now.
387 */
Christoph Hellwig0b1f91772008-08-13 16:13:09 +1000388 IHOLD(quotip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
391 nmaps = 1;
392 if ((error = xfs_bmapi(tp, quotip,
393 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
394 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
395 &firstblock,
396 XFS_QM_DQALLOC_SPACE_RES(mp),
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000397 &map, &nmaps, &flist, NULL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto error0;
399 }
400 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
401 ASSERT(nmaps == 1);
402 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
403 (map.br_startblock != HOLESTARTBLOCK));
404
405 /*
406 * Keep track of the blkno to save a lookup later
407 */
408 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
409
410 /* now we can just get the buffer (there's nothing to read yet) */
411 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
412 dqp->q_blkno,
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000413 mp->m_quotainfo->qi_dqchunklen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 0);
415 if (!bp || (error = XFS_BUF_GETERROR(bp)))
416 goto error1;
417 /*
418 * Make a chunk of dquots out of this buffer and log
419 * the entire thing.
420 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100421 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000422 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Tim Shimminefa092f2005-09-05 08:29:01 +1000424 /*
425 * xfs_bmap_finish() may commit the current transaction and
426 * start a second transaction if the freelist is not empty.
427 *
428 * Since we still want to modify this buffer, we need to
429 * ensure that the buffer is not released on commit of
430 * the first transaction and ensure the buffer is added to the
431 * second transaction.
432 *
433 * If there is only one transaction then don't stop the buffer
434 * from being released when it commits later on.
435 */
436
437 xfs_trans_bhold(tp, bp);
438
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100439 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 goto error1;
441 }
442
Tim Shimminefa092f2005-09-05 08:29:01 +1000443 if (committed) {
444 tp = *tpp;
445 xfs_trans_bjoin(tp, bp);
446 } else {
447 xfs_trans_bhold_release(tp, bp);
448 }
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *O_bpp = bp;
451 return 0;
452
453 error1:
454 xfs_bmap_cancel(&flist);
455 error0:
456 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
457
458 return (error);
459}
460
461/*
462 * Maps a dquot to the buffer containing its on-disk version.
463 * This returns a ptr to the buffer containing the on-disk dquot
464 * in the bpp param, and a ptr to the on-disk dquot within that buffer
465 */
466STATIC int
467xfs_qm_dqtobp(
Tim Shimminefa092f2005-09-05 08:29:01 +1000468 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_dquot_t *dqp,
470 xfs_disk_dquot_t **O_ddpp,
471 xfs_buf_t **O_bpp,
472 uint flags)
473{
474 xfs_bmbt_irec_t map;
475 int nmaps, error;
476 xfs_buf_t *bp;
477 xfs_inode_t *quotip;
478 xfs_mount_t *mp;
479 xfs_disk_dquot_t *ddq;
480 xfs_dqid_t id;
481 boolean_t newdquot;
Tim Shimminefa092f2005-09-05 08:29:01 +1000482 xfs_trans_t *tp = (tpp ? *tpp : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 mp = dqp->q_mount;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100485 id = be32_to_cpu(dqp->q_core.d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 nmaps = 1;
487 newdquot = B_FALSE;
488
489 /*
490 * If we don't know where the dquot lives, find out.
491 */
492 if (dqp->q_blkno == (xfs_daddr_t) 0) {
493 /* We use the id as an index */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000494 dqp->q_fileoffset = (xfs_fileoff_t)id /
495 mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 nmaps = 1;
497 quotip = XFS_DQ_TO_QIP(dqp);
498 xfs_ilock(quotip, XFS_ILOCK_SHARED);
499 /*
500 * Return if this type of quotas is turned off while we didn't
501 * have an inode lock
502 */
503 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
504 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
505 return (ESRCH);
506 }
507 /*
508 * Find the block map; no allocations yet
509 */
510 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
511 XFS_DQUOT_CLUSTER_SIZE_FSB,
512 XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000513 NULL, 0, &map, &nmaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
516 if (error)
517 return (error);
518 ASSERT(nmaps == 1);
519 ASSERT(map.br_blockcount == 1);
520
521 /*
522 * offset of dquot in the (fixed sized) dquot chunk.
523 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000524 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sizeof(xfs_dqblk_t);
526 if (map.br_startblock == HOLESTARTBLOCK) {
527 /*
528 * We don't allocate unless we're asked to
529 */
530 if (!(flags & XFS_QMOPT_DQALLOC))
531 return (ENOENT);
532
533 ASSERT(tp);
Tim Shimminefa092f2005-09-05 08:29:01 +1000534 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dqp->q_fileoffset, &bp)))
536 return (error);
Tim Shimminefa092f2005-09-05 08:29:01 +1000537 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 newdquot = B_TRUE;
539 } else {
540 /*
541 * store the blkno etc so that we don't have to do the
542 * mapping all the time
543 */
544 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
545 }
546 }
547 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
548 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
549
550 /*
551 * Read in the buffer, unless we've just done the allocation
552 * (in which case we already have the buf).
553 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000554 if (!newdquot) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000555 trace_xfs_dqtobp_read(dqp);
556
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000557 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
558 dqp->q_blkno,
559 mp->m_quotainfo->qi_dqchunklen,
560 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (error || !bp)
562 return XFS_ERROR(error);
563 }
564 ASSERT(XFS_BUF_ISBUSY(bp));
565 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
566
567 /*
568 * calculate the location of the dquot inside the buffer.
569 */
570 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
571
572 /*
573 * A simple sanity check in case we got a corrupted dquot...
574 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000575 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
577 "dqtobp")) {
578 if (!(flags & XFS_QMOPT_DQREPAIR)) {
579 xfs_trans_brelse(tp, bp);
580 return XFS_ERROR(EIO);
581 }
582 XFS_BUF_BUSY(bp); /* We dirtied this */
583 }
584
585 *O_bpp = bp;
586 *O_ddpp = ddq;
587
588 return (0);
589}
590
591
592/*
593 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
594 * and release the buffer immediately.
595 *
596 */
597/* ARGSUSED */
598STATIC int
599xfs_qm_dqread(
Tim Shimminefa092f2005-09-05 08:29:01 +1000600 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 xfs_dqid_t id,
602 xfs_dquot_t *dqp, /* dquot to get filled in */
603 uint flags)
604{
605 xfs_disk_dquot_t *ddqp;
606 xfs_buf_t *bp;
607 int error;
Tim Shimminefa092f2005-09-05 08:29:01 +1000608 xfs_trans_t *tp;
609
610 ASSERT(tpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000612 trace_xfs_dqread(dqp);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * get a pointer to the on-disk dquot and the buffer containing it
616 * dqp already knows its own type (GROUP/USER).
617 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000618 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return (error);
620 }
Tim Shimminefa092f2005-09-05 08:29:01 +1000621 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* copy everything from disk dquot to the incore dquot */
624 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
Christoph Hellwig1149d962005-11-02 15:01:12 +1100625 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 xfs_qm_dquot_logitem_init(dqp);
627
628 /*
629 * Reservation counters are defined as reservation plus current usage
630 * to avoid having to add everytime.
631 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100632 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
633 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
634 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 /* Mark the buf so that this will stay incore a little longer */
637 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
638
639 /*
640 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
641 * So we need to release with xfs_trans_brelse().
642 * The strategy here is identical to that of inodes; we lock
643 * the dquot in xfs_qm_dqget() before making it accessible to
644 * others. This is because dquots, like inodes, need a good level of
645 * concurrency, and we don't want to take locks on the entire buffers
646 * for dquot accesses.
647 * Note also that the dquot buffer may even be dirty at this point, if
648 * this particular dquot was repaired. We still aren't afraid to
649 * brelse it because we have the changes incore.
650 */
651 ASSERT(XFS_BUF_ISBUSY(bp));
652 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
653 xfs_trans_brelse(tp, bp);
654
655 return (error);
656}
657
658
659/*
660 * allocate an incore dquot from the kernel heap,
661 * and fill its core with quota information kept on disk.
662 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
663 * if it wasn't already allocated.
664 */
665STATIC int
666xfs_qm_idtodq(
667 xfs_mount_t *mp,
668 xfs_dqid_t id, /* gid or uid, depending on type */
669 uint type, /* UDQUOT or GDQUOT */
670 uint flags, /* DQALLOC, DQREPAIR */
671 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
672{
673 xfs_dquot_t *dqp;
674 int error;
675 xfs_trans_t *tp;
676 int cancelflags=0;
677
678 dqp = xfs_qm_dqinit(mp, id, type);
679 tp = NULL;
680 if (flags & XFS_QMOPT_DQALLOC) {
681 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000682 error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
683 XFS_WRITE_LOG_RES(mp) +
684 BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
685 128,
686 0,
687 XFS_TRANS_PERM_LOG_RES,
688 XFS_WRITE_LOG_COUNT);
689 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 cancelflags = 0;
691 goto error0;
692 }
693 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
694 }
695
696 /*
697 * Read it from disk; xfs_dqread() takes care of
698 * all the necessary initialization of dquot's fields (locks, etc)
699 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000700 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * This can happen if quotas got turned off (ESRCH),
703 * or if the dquot didn't exist on disk and we ask to
704 * allocate (ENOENT).
705 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000706 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 cancelflags |= XFS_TRANS_ABORT;
708 goto error0;
709 }
710 if (tp) {
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000711 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto error1;
713 }
714
715 *O_dqpp = dqp;
716 return (0);
717
718 error0:
719 ASSERT(error);
720 if (tp)
721 xfs_trans_cancel(tp, cancelflags);
722 error1:
723 xfs_qm_dqdestroy(dqp);
724 *O_dqpp = NULL;
725 return (error);
726}
727
728/*
729 * Lookup a dquot in the incore dquot hashtable. We keep two separate
730 * hashtables for user and group dquots; and, these are global tables
731 * inside the XQM, not per-filesystem tables.
732 * The hash chain must be locked by caller, and it is left locked
733 * on return. Returning dquot is locked.
734 */
735STATIC int
736xfs_qm_dqlookup(
737 xfs_mount_t *mp,
738 xfs_dqid_t id,
739 xfs_dqhash_t *qh,
740 xfs_dquot_t **O_dqpp)
741{
742 xfs_dquot_t *dqp;
743 uint flist_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100745 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 flist_locked = B_FALSE;
748
749 /*
750 * Traverse the hashchain looking for a match
751 */
Dave Chinnere6a81f12010-04-13 15:06:51 +1000752 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * We already have the hashlock. We don't need the
755 * dqlock to look at the id field of the dquot, since the
756 * id can't be modified without the hashlock anyway.
757 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100758 if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000759 trace_xfs_dqlookup_found(dqp);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /*
762 * All in core dquots must be on the dqlist of mp
763 */
Dave Chinner3a254042010-04-13 15:06:48 +1000764 ASSERT(!list_empty(&dqp->q_mplist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 xfs_dqlock(dqp);
767 if (dqp->q_nrefs == 0) {
Dave Chinner3a8406f2010-04-13 15:06:52 +1000768 ASSERT(!list_empty(&dqp->q_freelist));
769 if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000770 trace_xfs_dqlookup_want(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /*
773 * We may have raced with dqreclaim_one()
774 * (and lost). So, flag that we don't
775 * want the dquot to be reclaimed.
776 */
777 dqp->dq_flags |= XFS_DQ_WANT;
778 xfs_dqunlock(dqp);
Dave Chinner3a8406f2010-04-13 15:06:52 +1000779 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 xfs_dqlock(dqp);
781 dqp->dq_flags &= ~(XFS_DQ_WANT);
782 }
783 flist_locked = B_TRUE;
784 }
785
786 /*
787 * id couldn't have changed; we had the hashlock all
788 * along
789 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100790 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (flist_locked) {
793 if (dqp->q_nrefs != 0) {
Dave Chinner3a8406f2010-04-13 15:06:52 +1000794 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 flist_locked = B_FALSE;
796 } else {
Dave Chinner3a8406f2010-04-13 15:06:52 +1000797 /* take it off the freelist */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000798 trace_xfs_dqlookup_freelist(dqp);
Dave Chinner3a8406f2010-04-13 15:06:52 +1000799 list_del_init(&dqp->q_freelist);
800 xfs_Gqm->qm_dqfrlist_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 XFS_DQHOLD(dqp);
805
806 if (flist_locked)
Dave Chinner3a8406f2010-04-13 15:06:52 +1000807 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * move the dquot to the front of the hashchain
810 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100811 ASSERT(mutex_is_locked(&qh->qh_lock));
Dave Chinnere6a81f12010-04-13 15:06:51 +1000812 list_move(&dqp->q_hashlist, &qh->qh_list);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000813 trace_xfs_dqlookup_done(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *O_dqpp = dqp;
Dave Chinnere6a81f12010-04-13 15:06:51 +1000815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 }
818
819 *O_dqpp = NULL;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100820 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return (1);
822}
823
824/*
825 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
826 * a locked dquot, doing an allocation (if requested) as needed.
827 * When both an inode and an id are given, the inode's id takes precedence.
828 * That is, if the id changes while we don't hold the ilock inside this
829 * function, the new dquot is returned, not necessarily the one requested
830 * in the id argument.
831 */
832int
833xfs_qm_dqget(
834 xfs_mount_t *mp,
835 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000836 xfs_dqid_t id, /* uid/projid/gid depending on type */
837 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
839 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
840{
841 xfs_dquot_t *dqp;
842 xfs_dqhash_t *h;
843 uint version;
844 int error;
845
846 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
847 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000848 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
850 return (ESRCH);
851 }
852 h = XFS_DQ_HASH(mp, id, type);
853
854#ifdef DEBUG
855 if (xfs_do_dqerror) {
856 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
857 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
858 cmn_err(CE_DEBUG, "Returning error in dqget");
859 return (EIO);
860 }
861 }
862#endif
863
864 again:
865
866#ifdef DEBUG
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000867 ASSERT(type == XFS_DQ_USER ||
868 type == XFS_DQ_PROJ ||
869 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (ip) {
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000871 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (type == XFS_DQ_USER)
873 ASSERT(ip->i_udquot == NULL);
874 else
875 ASSERT(ip->i_gdquot == NULL);
876 }
877#endif
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100878 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 /*
881 * Look in the cache (hashtable).
882 * The chain is kept locked during lookup.
883 */
884 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
885 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
886 /*
887 * The dquot was found, moved to the front of the chain,
888 * taken off the freelist if it was on it, and locked
889 * at this point. Just unlock the hashchain and return.
890 */
891 ASSERT(*O_dqpp);
892 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100893 mutex_unlock(&h->qh_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000894 trace_xfs_dqget_hit(*O_dqpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return (0); /* success */
896 }
897 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
898
899 /*
900 * Dquot cache miss. We don't want to keep the inode lock across
901 * a (potential) disk read. Also we don't want to deal with the lock
902 * ordering between quotainode and this inode. OTOH, dropping the inode
903 * lock here means dealing with a chown that can happen before
904 * we re-acquire the lock.
905 */
906 if (ip)
907 xfs_iunlock(ip, XFS_ILOCK_EXCL);
908 /*
909 * Save the hashchain version stamp, and unlock the chain, so that
910 * we don't keep the lock across a disk read
911 */
912 version = h->qh_version;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100913 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /*
916 * Allocate the dquot on the kernel heap, and read the ondisk
917 * portion off the disk. Also, do all the necessary initialization
918 * This can return ENOENT if dquot didn't exist on disk and we didn't
919 * ask it to allocate; ESRCH if quotas got turned off suddenly.
920 */
921 if ((error = xfs_qm_idtodq(mp, id, type,
922 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
923 XFS_QMOPT_DOWARN),
924 &dqp))) {
925 if (ip)
926 xfs_ilock(ip, XFS_ILOCK_EXCL);
927 return (error);
928 }
929
930 /*
931 * See if this is mount code calling to look at the overall quota limits
932 * which are stored in the id == 0 user or group's dquot.
933 * Since we may not have done a quotacheck by this point, just return
934 * the dquot without attaching it to any hashtables, lists, etc, or even
935 * taking a reference.
936 * The caller must dqdestroy this once done.
937 */
938 if (flags & XFS_QMOPT_DQSUSER) {
939 ASSERT(id == 0);
940 ASSERT(! ip);
941 goto dqret;
942 }
943
944 /*
945 * Dquot lock comes after hashlock in the lock ordering
946 */
947 if (ip) {
948 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig191f8482010-04-20 17:01:53 +1000949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /*
951 * A dquot could be attached to this inode by now, since
952 * we had dropped the ilock.
953 */
954 if (type == XFS_DQ_USER) {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000955 if (!XFS_IS_UQUOTA_ON(mp)) {
956 /* inode stays locked on return */
957 xfs_qm_dqdestroy(dqp);
958 return XFS_ERROR(ESRCH);
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (ip->i_udquot) {
961 xfs_qm_dqdestroy(dqp);
962 dqp = ip->i_udquot;
963 xfs_dqlock(dqp);
964 goto dqret;
965 }
966 } else {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000967 if (!XFS_IS_OQUOTA_ON(mp)) {
968 /* inode stays locked on return */
969 xfs_qm_dqdestroy(dqp);
970 return XFS_ERROR(ESRCH);
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ip->i_gdquot) {
973 xfs_qm_dqdestroy(dqp);
974 dqp = ip->i_gdquot;
975 xfs_dqlock(dqp);
976 goto dqret;
977 }
978 }
979 }
980
981 /*
982 * Hashlock comes after ilock in lock order
983 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100984 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (version != h->qh_version) {
986 xfs_dquot_t *tmpdqp;
987 /*
988 * Now, see if somebody else put the dquot in the
989 * hashtable before us. This can happen because we didn't
990 * keep the hashchain lock. We don't have to worry about
991 * lock order between the two dquots here since dqp isn't
992 * on any findable lists yet.
993 */
994 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
995 /*
996 * Duplicate found. Just throw away the new dquot
997 * and start over.
998 */
999 xfs_qm_dqput(tmpdqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001000 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 xfs_qm_dqdestroy(dqp);
1002 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1003 goto again;
1004 }
1005 }
1006
1007 /*
1008 * Put the dquot at the beginning of the hash-chain and mp's list
1009 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1010 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001011 ASSERT(mutex_is_locked(&h->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dqp->q_hash = h;
Dave Chinnere6a81f12010-04-13 15:06:51 +10001013 list_add(&dqp->q_hashlist, &h->qh_list);
1014 h->qh_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /*
1017 * Attach this dquot to this filesystem's list of all dquots,
1018 * kept inside the mount structure in m_quotainfo field
1019 */
Dave Chinner3a254042010-04-13 15:06:48 +10001020 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /*
1023 * We return a locked dquot to the caller, with a reference taken
1024 */
1025 xfs_dqlock(dqp);
1026 dqp->q_nrefs = 1;
1027
Dave Chinner3a254042010-04-13 15:06:48 +10001028 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
1029 mp->m_quotainfo->qi_dquots++;
1030 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001031 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dqret:
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001033 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001034 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 *O_dqpp = dqp;
1036 return (0);
1037}
1038
1039
1040/*
1041 * Release a reference to the dquot (decrement ref-count)
1042 * and unlock it. If there is a group quota attached to this
1043 * dquot, carefully release that too without tripping over
1044 * deadlocks'n'stuff.
1045 */
1046void
1047xfs_qm_dqput(
1048 xfs_dquot_t *dqp)
1049{
1050 xfs_dquot_t *gdqp;
1051
1052 ASSERT(dqp->q_nrefs > 0);
1053 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001054
1055 trace_xfs_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (dqp->q_nrefs != 1) {
1058 dqp->q_nrefs--;
1059 xfs_dqunlock(dqp);
1060 return;
1061 }
1062
1063 /*
1064 * drop the dqlock and acquire the freelist and dqlock
1065 * in the right order; but try to get it out-of-order first
1066 */
Dave Chinner3a8406f2010-04-13 15:06:52 +10001067 if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001068 trace_xfs_dqput_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 xfs_dqunlock(dqp);
Dave Chinner3a8406f2010-04-13 15:06:52 +10001070 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 xfs_dqlock(dqp);
1072 }
1073
1074 while (1) {
1075 gdqp = NULL;
1076
1077 /* We can't depend on nrefs being == 1 here */
1078 if (--dqp->q_nrefs == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001079 trace_xfs_dqput_free(dqp);
1080
Dave Chinner3a8406f2010-04-13 15:06:52 +10001081 list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
1082 xfs_Gqm->qm_dqfrlist_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
1085 * If we just added a udquot to the freelist, then
1086 * we want to release the gdquot reference that
1087 * it (probably) has. Otherwise it'll keep the
1088 * gdquot from getting reclaimed.
1089 */
1090 if ((gdqp = dqp->q_gdquot)) {
1091 /*
1092 * Avoid a recursive dqput call
1093 */
1094 xfs_dqlock(gdqp);
1095 dqp->q_gdquot = NULL;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 xfs_dqunlock(dqp);
1099
1100 /*
1101 * If we had a group quota inside the user quota as a hint,
1102 * release it now.
1103 */
1104 if (! gdqp)
1105 break;
1106 dqp = gdqp;
1107 }
Dave Chinner3a8406f2010-04-13 15:06:52 +10001108 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111/*
1112 * Release a dquot. Flush it if dirty, then dqput() it.
1113 * dquot must not be locked.
1114 */
1115void
1116xfs_qm_dqrele(
1117 xfs_dquot_t *dqp)
1118{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001119 if (!dqp)
1120 return;
1121
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001122 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 xfs_dqlock(dqp);
1125 /*
1126 * We don't care to flush it if the dquot is dirty here.
1127 * That will create stutters that we want to avoid.
1128 * Instead we do a delayed write when we try to reclaim
1129 * a dirty dquot. Also xfs_sync will take part of the burden...
1130 */
1131 xfs_qm_dqput(dqp);
1132}
1133
1134
1135/*
1136 * Write a modified dquot to disk.
1137 * The dquot must be locked and the flush lock too taken by caller.
1138 * The flush lock will not be unlocked until the dquot reaches the disk,
1139 * but the dquot is free to be unlocked and modified by the caller
1140 * in the interim. Dquot is still locked on return. This behavior is
1141 * identical to that of inodes.
1142 */
1143int
1144xfs_qm_dqflush(
1145 xfs_dquot_t *dqp,
1146 uint flags)
1147{
1148 xfs_mount_t *mp;
1149 xfs_buf_t *bp;
1150 xfs_disk_dquot_t *ddqp;
1151 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001154 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001155 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /*
David Chinner2f8a3ce2008-10-30 17:07:20 +11001158 * If not dirty, or it's pinned and we are not supposed to
1159 * block, nada.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
David Chinner2f8a3ce2008-10-30 17:07:20 +11001161 if (!XFS_DQ_IS_DIRTY(dqp) ||
Dave Chinner20026d92010-02-04 09:48:58 +11001162 (!(flags & SYNC_WAIT) && atomic_read(&dqp->q_pincount) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfs_dqfunlock(dqp);
David Chinner2f8a3ce2008-10-30 17:07:20 +11001164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 xfs_qm_dqunpin_wait(dqp);
1167
1168 /*
1169 * This may have been unpinned because the filesystem is shutting
1170 * down forcibly. If that's the case we must not write this dquot
1171 * to disk, because the log record didn't make it to disk!
1172 */
1173 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1174 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1175 xfs_dqfunlock(dqp);
1176 return XFS_ERROR(EIO);
1177 }
1178
1179 /*
1180 * Get the buffer containing the on-disk dquot
1181 * We don't need a transaction envelope because we know that the
1182 * the ondisk-dquot has already been allocated for.
1183 */
1184 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ASSERT(error != ENOENT);
1186 /*
1187 * Quotas could have gotten turned off (ESRCH)
1188 */
1189 xfs_dqfunlock(dqp);
1190 return (error);
1191 }
1192
Christoph Hellwig1149d962005-11-02 15:01:12 +11001193 if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001194 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001195 xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return XFS_ERROR(EIO);
1197 }
1198
1199 /* This is the only portion of data that needs to persist */
1200 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1201
1202 /*
1203 * Clear the dirty field and remember the flush lsn for later use.
1204 */
1205 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1206 mp = dqp->q_mount;
1207
David Chinner7b2e2a32008-10-30 17:39:12 +11001208 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1209 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /*
1212 * Attach an iodone routine so that we can remove this dquot from the
1213 * AIL and release the flush lock once the dquot is synced to disk.
1214 */
1215 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1216 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1217 /*
1218 * If the buffer is pinned then push on the log so we won't
1219 * get stuck waiting in the write for too long.
1220 */
1221 if (XFS_BUF_ISPINNED(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001222 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001223 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
Dave Chinner20026d92010-02-04 09:48:58 +11001226 if (flags & SYNC_WAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 error = xfs_bwrite(mp, bp);
Dave Chinner20026d92010-02-04 09:48:58 +11001228 else
1229 xfs_bdwrite(mp, bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001230
1231 trace_xfs_dqflush_done(dqp);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * dqp is still locked, but caller is free to unlock it now.
1235 */
Dave Chinner20026d92010-02-04 09:48:58 +11001236 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238}
1239
1240/*
1241 * This is the dquot flushing I/O completion routine. It is called
1242 * from interrupt level when the buffer containing the dquot is
1243 * flushed to disk. It is responsible for removing the dquot logitem
1244 * from the AIL if it has not been re-logged, and unlocking the dquot's
1245 * flush lock. This behavior is very similar to that of inodes..
1246 */
1247/*ARGSUSED*/
1248STATIC void
1249xfs_qm_dqflush_done(
1250 xfs_buf_t *bp,
1251 xfs_dq_logitem_t *qip)
1252{
1253 xfs_dquot_t *dqp;
David Chinner783a2f62008-10-30 17:39:58 +11001254 struct xfs_ail *ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 dqp = qip->qli_dquot;
David Chinner783a2f62008-10-30 17:39:58 +11001257 ailp = qip->qli_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
1260 * We only want to pull the item from the AIL if its
1261 * location in the log has not changed since we started the flush.
1262 * Thus, we only bother if the dquot's lsn has
1263 * not changed. First we check the lsn outside the lock
1264 * since it's cheaper, and then we recheck while
1265 * holding the lock before removing the dquot from the AIL.
1266 */
1267 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1268 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1269
David Chinner783a2f62008-10-30 17:39:58 +11001270 /* xfs_trans_ail_delete() drops the AIL lock. */
1271 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
David Chinner783a2f62008-10-30 17:39:58 +11001273 xfs_trans_ail_delete(ailp, (xfs_log_item_t*)qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 else
David Chinner783a2f62008-10-30 17:39:58 +11001275 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 /*
1279 * Release the dq's flush lock since we're done with it.
1280 */
1281 xfs_dqfunlock(dqp);
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284int
1285xfs_qm_dqlock_nowait(
1286 xfs_dquot_t *dqp)
1287{
David Chinnere1f49cf2008-08-13 16:41:43 +10001288 return mutex_trylock(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291void
1292xfs_dqlock(
1293 xfs_dquot_t *dqp)
1294{
David Chinnere1f49cf2008-08-13 16:41:43 +10001295 mutex_lock(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298void
1299xfs_dqunlock(
1300 xfs_dquot_t *dqp)
1301{
1302 mutex_unlock(&(dqp->q_qlock));
1303 if (dqp->q_logitem.qli_dquot == dqp) {
1304 /* Once was dqp->q_mount, but might just have been cleared */
David Chinner783a2f62008-10-30 17:39:58 +11001305 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 (xfs_log_item_t*)&(dqp->q_logitem));
1307 }
1308}
1309
1310
1311void
1312xfs_dqunlock_nonotify(
1313 xfs_dquot_t *dqp)
1314{
1315 mutex_unlock(&(dqp->q_qlock));
1316}
1317
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001318/*
1319 * Lock two xfs_dquot structures.
1320 *
1321 * To avoid deadlocks we always lock the quota structure with
1322 * the lowerd id first.
1323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324void
1325xfs_dqlock2(
1326 xfs_dquot_t *d1,
1327 xfs_dquot_t *d2)
1328{
1329 if (d1 && d2) {
1330 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001331 if (be32_to_cpu(d1->q_core.d_id) >
1332 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001333 mutex_lock(&d2->q_qlock);
1334 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001336 mutex_lock(&d1->q_qlock);
1337 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001339 } else if (d1) {
1340 mutex_lock(&d1->q_qlock);
1341 } else if (d2) {
1342 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344}
1345
1346
1347/*
1348 * Take a dquot out of the mount's dqlist as well as the hashlist.
1349 * This is called via unmount as well as quotaoff, and the purge
1350 * will always succeed unless there are soft (temp) references
1351 * outstanding.
1352 *
1353 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1354 * that we're returning! XXXsup - not cool.
1355 */
1356/* ARGSUSED */
1357int
1358xfs_qm_dqpurge(
Denys Vlasenko4f0e8a92008-05-19 16:34:04 +10001359 xfs_dquot_t *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Dave Chinnere6a81f12010-04-13 15:06:51 +10001361 xfs_dqhash_t *qh = dqp->q_hash;
David Chinner3c568362008-04-10 12:20:24 +10001362 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Dave Chinner3a254042010-04-13 15:06:48 +10001364 ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001365 ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 xfs_dqlock(dqp);
1368 /*
1369 * We really can't afford to purge a dquot that is
1370 * referenced, because these are hard refs.
1371 * It shouldn't happen in general because we went thru _all_ inodes in
1372 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1373 * However it is possible that we have dquots with temporary
1374 * references that are not attached to an inode. e.g. see xfs_setattr().
1375 */
1376 if (dqp->q_nrefs != 0) {
1377 xfs_dqunlock(dqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001378 mutex_unlock(&dqp->q_hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return (1);
1380 }
1381
Dave Chinner3a8406f2010-04-13 15:06:52 +10001382 ASSERT(!list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
1385 * If we're turning off quotas, we have to make sure that, for
1386 * example, we don't delete quota disk blocks while dquots are
1387 * in the process of getting written to those disk blocks.
1388 * This dquot might well be on AIL, and we can't leave it there
1389 * if we're turning off quotas. Basically, we need this flush
1390 * lock, and are willing to block on it.
1391 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001392 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /*
1394 * Block on the flush lock after nudging dquot buffer,
1395 * if it is incore.
1396 */
1397 xfs_qm_dqflock_pushbuf_wait(dqp);
1398 }
1399
1400 /*
1401 * XXXIf we're turning this type of quotas off, we don't care
1402 * about the dirty metadata sitting in this dquot. OTOH, if
1403 * we're unmounting, we do care, so we flush it and wait.
1404 */
1405 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10001406 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* dqflush unlocks dqflock */
1409 /*
1410 * Given that dqpurge is a very rare occurrence, it is OK
1411 * that we're holding the hashlist and mplist locks
1412 * across the disk write. But, ... XXXsup
1413 *
1414 * We don't care about getting disk errors here. We need
1415 * to purge this dquot anyway, so we go ahead regardless.
1416 */
Dave Chinner20026d92010-02-04 09:48:58 +11001417 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
David Chinner3c568362008-04-10 12:20:24 +10001418 if (error)
1419 xfs_fs_cmn_err(CE_WARN, mp,
1420 "xfs_qm_dqpurge: dquot %p flush failed", dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 xfs_dqflock(dqp);
1422 }
Peter Leckiebc3048e2008-10-30 17:05:04 +11001423 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1425 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1426
Dave Chinnere6a81f12010-04-13 15:06:51 +10001427 list_del_init(&dqp->q_hashlist);
1428 qh->qh_version++;
Dave Chinner3a254042010-04-13 15:06:48 +10001429 list_del_init(&dqp->q_mplist);
1430 mp->m_quotainfo->qi_dqreclaims++;
1431 mp->m_quotainfo->qi_dquots--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * XXX Move this to the front of the freelist, if we can get the
1434 * freelist lock.
1435 */
Dave Chinner3a8406f2010-04-13 15:06:52 +10001436 ASSERT(!list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 dqp->q_mount = NULL;
1439 dqp->q_hash = NULL;
1440 dqp->dq_flags = XFS_DQ_INACTIVE;
1441 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1442 xfs_dqfunlock(dqp);
1443 xfs_dqunlock(dqp);
Dave Chinnere6a81f12010-04-13 15:06:51 +10001444 mutex_unlock(&qh->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return (0);
1446}
1447
1448
1449#ifdef QUOTADEBUG
1450void
1451xfs_qm_dqprint(xfs_dquot_t *dqp)
1452{
1453 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1454 cmn_err(CE_DEBUG, "---- dquotID = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001455 (int)be32_to_cpu(dqp->q_core.d_id));
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001456 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1458 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1459 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1460 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001461 be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1462 (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001464 be64_to_cpu(dqp->q_core.d_blk_softlimit),
1465 (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001467 be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1468 (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001470 be64_to_cpu(dqp->q_core.d_ino_softlimit),
1471 (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001473 be64_to_cpu(dqp->q_core.d_bcount),
1474 (int)be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001476 be64_to_cpu(dqp->q_core.d_icount),
1477 (int)be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 cmn_err(CE_DEBUG, "---- btimer = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001479 (int)be32_to_cpu(dqp->q_core.d_btimer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 cmn_err(CE_DEBUG, "---- itimer = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001481 (int)be32_to_cpu(dqp->q_core.d_itimer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 cmn_err(CE_DEBUG, "---------------------------");
1483}
1484#endif
1485
1486/*
1487 * Give the buffer a little push if it is incore and
1488 * wait on the flush lock.
1489 */
1490void
1491xfs_qm_dqflock_pushbuf_wait(
1492 xfs_dquot_t *dqp)
1493{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001494 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 xfs_buf_t *bp;
1496
1497 /*
1498 * Check to see if the dquot has been flushed delayed
1499 * write. If so, grab its buffer and send it
1500 * out immediately. We'll be able to acquire
1501 * the flush lock when the I/O completes.
1502 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001503 bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
1504 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001505 if (!bp)
1506 goto out_lock;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001507
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001508 if (XFS_BUF_ISDELAYWRITE(bp)) {
1509 if (XFS_BUF_ISPINNED(bp))
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001510 xfs_log_force(mp, 0);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001511 xfs_buf_delwri_promote(bp);
1512 wake_up_process(bp->b_target->bt_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001514 xfs_buf_relse(bp);
1515out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 xfs_dqflock(dqp);
1517}