blob: 838289b92bb9f97d6f753e1ecace647e1135adec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_btree.h"
39#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_buf_item.h"
47#include "xfs_trans_space.h"
48#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
53/*
54 LOCK ORDER
55
56 inode lock (ilock)
57 dquot hash-chain lock (hashlock)
58 xqm dquot freelist lock (freelistlock
59 mount's dquot list lock (mplistlock)
60 user dquot lock - lock ordering among dquots is based on the uid or gid
61 group dquot lock - similar to udquots. Between the two dquots, the udquot
62 has to be locked first.
63 pin lock - the dquot lock must be held to take this lock.
64 flush lock - ditto.
65*/
66
67STATIC void xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
68
69#ifdef DEBUG
70xfs_buftarg_t *xfs_dqerror_target;
71int xfs_do_dqerror;
72int xfs_dqreq_num;
73int xfs_dqerror_mod = 33;
74#endif
75
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010076static struct lock_class_key xfs_dquot_other_class;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * Allocate and initialize a dquot. We don't always allocate fresh memory;
80 * we try to reclaim a free dquot if the number of incore dquots are above
81 * a threshold.
82 * The only field inside the core that gets initialized at this point
83 * is the d_id field. The idea is to fill in the entire q_core
84 * when we read in the on disk dquot.
85 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100086STATIC xfs_dquot_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070087xfs_qm_dqinit(
88 xfs_mount_t *mp,
89 xfs_dqid_t id,
90 uint type)
91{
92 xfs_dquot_t *dqp;
93 boolean_t brandnewdquot;
94
95 brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
96 dqp->dq_flags = type;
Christoph Hellwig1149d962005-11-02 15:01:12 +110097 dqp->q_core.d_id = cpu_to_be32(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dqp->q_mount = mp;
99
100 /*
101 * No need to re-initialize these if this is a reclaimed dquot.
102 */
103 if (brandnewdquot) {
104 dqp->dq_flnext = dqp->dq_flprev = dqp;
Jes Sorensen794ee1b2006-01-09 15:59:21 -0800105 mutex_init(&dqp->q_qlock);
Peter Leckiebc3048e2008-10-30 17:05:04 +1100106 init_waitqueue_head(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
David Chinnere1f49cf2008-08-13 16:41:43 +1000108 /*
109 * Because we want to use a counting completion, complete
110 * the flush completion once to allow a single access to
111 * the flush completion without blocking.
112 */
113 init_completion(&dqp->q_flush);
114 complete(&dqp->q_flush);
115
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000116 trace_xfs_dqinit(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 } else {
118 /*
119 * Only the q_core portion was zeroed in dqreclaim_one().
120 * So, we need to reset others.
121 */
122 dqp->q_nrefs = 0;
123 dqp->q_blkno = 0;
Dave Chinner3a254042010-04-13 15:06:48 +1000124 INIT_LIST_HEAD(&dqp->q_mplist);
125 dqp->HL_NEXT = NULL;
126 dqp->HL_PREVP = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 dqp->q_bufoffset = 0;
128 dqp->q_fileoffset = 0;
129 dqp->q_transp = NULL;
130 dqp->q_gdquot = NULL;
131 dqp->q_res_bcount = 0;
132 dqp->q_res_icount = 0;
133 dqp->q_res_rtbcount = 0;
Peter Leckiebc3048e2008-10-30 17:05:04 +1100134 atomic_set(&dqp->q_pincount, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 dqp->q_hash = NULL;
136 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
137
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000138 trace_xfs_dqreuse(dqp);
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +0100139 }
140
141 /*
142 * In either case we need to make sure group quotas have a different
143 * lock class than user quotas, to make sure lockdep knows we can
144 * locks of one of each at the same time.
145 */
146 if (!(type & XFS_DQ_USER))
147 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /*
150 * log item gets initialized later
151 */
152 return (dqp);
153}
154
155/*
156 * This is called to free all the memory associated with a dquot
157 */
158void
159xfs_qm_dqdestroy(
160 xfs_dquot_t *dqp)
161{
162 ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
163
164 mutex_destroy(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 sv_destroy(&dqp->q_pinwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 atomic_dec(&xfs_Gqm->qm_totaldquots);
169}
170
171/*
172 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
173 */
174STATIC void
175xfs_qm_dqinit_core(
176 xfs_dqid_t id,
177 uint type,
178 xfs_dqblk_t *d)
179{
180 /*
181 * Caller has zero'd the entire dquot 'chunk' already.
182 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100183 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
184 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
185 d->dd_diskdq.d_id = cpu_to_be32(id);
186 d->dd_diskdq.d_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * If default limits are in force, push them into the dquot now.
191 * We overwrite the dquot limits only if they are zero and this
192 * is not the root dquot.
193 */
194void
195xfs_qm_adjust_dqlimits(
196 xfs_mount_t *mp,
197 xfs_disk_dquot_t *d)
198{
199 xfs_quotainfo_t *q = mp->m_quotainfo;
200
201 ASSERT(d->d_id);
202
203 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100204 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100206 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (q->qi_isoftlimit && !d->d_ino_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100208 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100210 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100212 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100214 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * Check the limits and timers of a dquot and start or reset timers
219 * if necessary.
220 * This gets called even when quota enforcement is OFF, which makes our
221 * life a little less complicated. (We just don't reject any quota
222 * reservations in that case, when enforcement is off).
223 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
224 * enforcement's off.
225 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000226 * 'automatically' get started when limits get exceeded. They do
227 * get reset to zero, however, when we find the count to be under
228 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
230void
231xfs_qm_adjust_dqtimers(
232 xfs_mount_t *mp,
233 xfs_disk_dquot_t *d)
234{
235 ASSERT(d->d_id);
236
237#ifdef QUOTADEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100238 if (d->d_blk_hardlimit)
239 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
240 be64_to_cpu(d->d_blk_hardlimit));
241 if (d->d_ino_hardlimit)
242 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
243 be64_to_cpu(d->d_ino_hardlimit));
244 if (d->d_rtb_hardlimit)
245 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
246 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
248 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100249 if ((d->d_blk_softlimit &&
250 (be64_to_cpu(d->d_bcount) >=
251 be64_to_cpu(d->d_blk_softlimit))) ||
252 (d->d_blk_hardlimit &&
253 (be64_to_cpu(d->d_bcount) >=
254 be64_to_cpu(d->d_blk_hardlimit)))) {
255 d->d_btimer = cpu_to_be32(get_seconds() +
256 XFS_QI_BTIMELIMIT(mp));
Nathan Scott754002b2005-06-21 15:49:06 +1000257 } else {
258 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 } else {
261 if ((!d->d_blk_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100262 (be64_to_cpu(d->d_bcount) <
263 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 (!d->d_blk_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100265 (be64_to_cpu(d->d_bcount) <
266 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 d->d_btimer = 0;
268 }
269 }
270
271 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100272 if ((d->d_ino_softlimit &&
273 (be64_to_cpu(d->d_icount) >=
274 be64_to_cpu(d->d_ino_softlimit))) ||
275 (d->d_ino_hardlimit &&
276 (be64_to_cpu(d->d_icount) >=
277 be64_to_cpu(d->d_ino_hardlimit)))) {
278 d->d_itimer = cpu_to_be32(get_seconds() +
279 XFS_QI_ITIMELIMIT(mp));
Nathan Scott754002b2005-06-21 15:49:06 +1000280 } else {
281 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 } else {
284 if ((!d->d_ino_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100285 (be64_to_cpu(d->d_icount) <
286 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 (!d->d_ino_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100288 (be64_to_cpu(d->d_icount) <
289 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 d->d_itimer = 0;
291 }
292 }
293
294 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100295 if ((d->d_rtb_softlimit &&
296 (be64_to_cpu(d->d_rtbcount) >=
297 be64_to_cpu(d->d_rtb_softlimit))) ||
298 (d->d_rtb_hardlimit &&
299 (be64_to_cpu(d->d_rtbcount) >=
300 be64_to_cpu(d->d_rtb_hardlimit)))) {
301 d->d_rtbtimer = cpu_to_be32(get_seconds() +
302 XFS_QI_RTBTIMELIMIT(mp));
Nathan Scott754002b2005-06-21 15:49:06 +1000303 } else {
304 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 } else {
307 if ((!d->d_rtb_softlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100308 (be64_to_cpu(d->d_rtbcount) <
309 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 (!d->d_rtb_hardlimit ||
Christoph Hellwig1149d962005-11-02 15:01:12 +1100311 (be64_to_cpu(d->d_rtbcount) <
312 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 d->d_rtbtimer = 0;
314 }
315 }
316}
317
318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * initialize a buffer full of dquots and log the whole thing
320 */
321STATIC void
322xfs_qm_init_dquot_blk(
323 xfs_trans_t *tp,
324 xfs_mount_t *mp,
325 xfs_dqid_t id,
326 uint type,
327 xfs_buf_t *bp)
328{
329 xfs_dqblk_t *d;
330 int curid, i;
331
332 ASSERT(tp);
333 ASSERT(XFS_BUF_ISBUSY(bp));
334 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
335
336 d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
337
338 /*
339 * ID of the first dquot in the block - id's are zero based.
340 */
341 curid = id - (id % XFS_QM_DQPERBLK(mp));
342 ASSERT(curid >= 0);
343 memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
344 for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
345 xfs_qm_dqinit_core(curid, type, d);
346 xfs_trans_dquot_buf(tp, bp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000347 (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
348 ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
349 XFS_BLI_GDQUOT_BUF)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
351}
352
353
354
355/*
356 * Allocate a block and fill it with dquots.
357 * This is called when the bmapi finds a hole.
358 */
359STATIC int
360xfs_qm_dqalloc(
Tim Shimminefa092f2005-09-05 08:29:01 +1000361 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xfs_mount_t *mp,
363 xfs_dquot_t *dqp,
364 xfs_inode_t *quotip,
365 xfs_fileoff_t offset_fsb,
366 xfs_buf_t **O_bpp)
367{
368 xfs_fsblock_t firstblock;
369 xfs_bmap_free_t flist;
370 xfs_bmbt_irec_t map;
371 int nmaps, error, committed;
372 xfs_buf_t *bp;
Tim Shimminefa092f2005-09-05 08:29:01 +1000373 xfs_trans_t *tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 ASSERT(tp != NULL);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000376
377 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /*
380 * Initialize the bmap freelist prior to calling bmapi code.
381 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600382 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 xfs_ilock(quotip, XFS_ILOCK_EXCL);
384 /*
385 * Return if this type of quotas is turned off while we didn't
386 * have an inode lock
387 */
388 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
389 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
390 return (ESRCH);
391 }
392
393 /*
394 * xfs_trans_commit normally decrements the vnode ref count
395 * when it unlocks the inode. Since we want to keep the quota
396 * inode around, we bump the vnode ref count now.
397 */
Christoph Hellwig0b1f91772008-08-13 16:13:09 +1000398 IHOLD(quotip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
401 nmaps = 1;
402 if ((error = xfs_bmapi(tp, quotip,
403 offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
404 XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
405 &firstblock,
406 XFS_QM_DQALLOC_SPACE_RES(mp),
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000407 &map, &nmaps, &flist, NULL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto error0;
409 }
410 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
411 ASSERT(nmaps == 1);
412 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
413 (map.br_startblock != HOLESTARTBLOCK));
414
415 /*
416 * Keep track of the blkno to save a lookup later
417 */
418 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
419
420 /* now we can just get the buffer (there's nothing to read yet) */
421 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
422 dqp->q_blkno,
423 XFS_QI_DQCHUNKLEN(mp),
424 0);
425 if (!bp || (error = XFS_BUF_GETERROR(bp)))
426 goto error1;
427 /*
428 * Make a chunk of dquots out of this buffer and log
429 * the entire thing.
430 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100431 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000432 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Tim Shimminefa092f2005-09-05 08:29:01 +1000434 /*
435 * xfs_bmap_finish() may commit the current transaction and
436 * start a second transaction if the freelist is not empty.
437 *
438 * Since we still want to modify this buffer, we need to
439 * ensure that the buffer is not released on commit of
440 * the first transaction and ensure the buffer is added to the
441 * second transaction.
442 *
443 * If there is only one transaction then don't stop the buffer
444 * from being released when it commits later on.
445 */
446
447 xfs_trans_bhold(tp, bp);
448
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100449 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto error1;
451 }
452
Tim Shimminefa092f2005-09-05 08:29:01 +1000453 if (committed) {
454 tp = *tpp;
455 xfs_trans_bjoin(tp, bp);
456 } else {
457 xfs_trans_bhold_release(tp, bp);
458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *O_bpp = bp;
461 return 0;
462
463 error1:
464 xfs_bmap_cancel(&flist);
465 error0:
466 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
467
468 return (error);
469}
470
471/*
472 * Maps a dquot to the buffer containing its on-disk version.
473 * This returns a ptr to the buffer containing the on-disk dquot
474 * in the bpp param, and a ptr to the on-disk dquot within that buffer
475 */
476STATIC int
477xfs_qm_dqtobp(
Tim Shimminefa092f2005-09-05 08:29:01 +1000478 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 xfs_dquot_t *dqp,
480 xfs_disk_dquot_t **O_ddpp,
481 xfs_buf_t **O_bpp,
482 uint flags)
483{
484 xfs_bmbt_irec_t map;
485 int nmaps, error;
486 xfs_buf_t *bp;
487 xfs_inode_t *quotip;
488 xfs_mount_t *mp;
489 xfs_disk_dquot_t *ddq;
490 xfs_dqid_t id;
491 boolean_t newdquot;
Tim Shimminefa092f2005-09-05 08:29:01 +1000492 xfs_trans_t *tp = (tpp ? *tpp : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 mp = dqp->q_mount;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100495 id = be32_to_cpu(dqp->q_core.d_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 nmaps = 1;
497 newdquot = B_FALSE;
498
499 /*
500 * If we don't know where the dquot lives, find out.
501 */
502 if (dqp->q_blkno == (xfs_daddr_t) 0) {
503 /* We use the id as an index */
Nathan Scottc310ab62005-11-02 10:31:41 +1100504 dqp->q_fileoffset = (xfs_fileoff_t)id / XFS_QM_DQPERBLK(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 nmaps = 1;
506 quotip = XFS_DQ_TO_QIP(dqp);
507 xfs_ilock(quotip, XFS_ILOCK_SHARED);
508 /*
509 * Return if this type of quotas is turned off while we didn't
510 * have an inode lock
511 */
512 if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
513 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
514 return (ESRCH);
515 }
516 /*
517 * Find the block map; no allocations yet
518 */
519 error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
520 XFS_DQUOT_CLUSTER_SIZE_FSB,
521 XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000522 NULL, 0, &map, &nmaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
525 if (error)
526 return (error);
527 ASSERT(nmaps == 1);
528 ASSERT(map.br_blockcount == 1);
529
530 /*
531 * offset of dquot in the (fixed sized) dquot chunk.
532 */
533 dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
534 sizeof(xfs_dqblk_t);
535 if (map.br_startblock == HOLESTARTBLOCK) {
536 /*
537 * We don't allocate unless we're asked to
538 */
539 if (!(flags & XFS_QMOPT_DQALLOC))
540 return (ENOENT);
541
542 ASSERT(tp);
Tim Shimminefa092f2005-09-05 08:29:01 +1000543 if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 dqp->q_fileoffset, &bp)))
545 return (error);
Tim Shimminefa092f2005-09-05 08:29:01 +1000546 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 newdquot = B_TRUE;
548 } else {
549 /*
550 * store the blkno etc so that we don't have to do the
551 * mapping all the time
552 */
553 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
554 }
555 }
556 ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
557 ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
558
559 /*
560 * Read in the buffer, unless we've just done the allocation
561 * (in which case we already have the buf).
562 */
563 if (! newdquot) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000564 trace_xfs_dqtobp_read(dqp);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
567 dqp->q_blkno,
568 XFS_QI_DQCHUNKLEN(mp),
569 0, &bp))) {
570 return (error);
571 }
572 if (error || !bp)
573 return XFS_ERROR(error);
574 }
575 ASSERT(XFS_BUF_ISBUSY(bp));
576 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
577
578 /*
579 * calculate the location of the dquot inside the buffer.
580 */
581 ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
582
583 /*
584 * A simple sanity check in case we got a corrupted dquot...
585 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000586 if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
588 "dqtobp")) {
589 if (!(flags & XFS_QMOPT_DQREPAIR)) {
590 xfs_trans_brelse(tp, bp);
591 return XFS_ERROR(EIO);
592 }
593 XFS_BUF_BUSY(bp); /* We dirtied this */
594 }
595
596 *O_bpp = bp;
597 *O_ddpp = ddq;
598
599 return (0);
600}
601
602
603/*
604 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
605 * and release the buffer immediately.
606 *
607 */
608/* ARGSUSED */
609STATIC int
610xfs_qm_dqread(
Tim Shimminefa092f2005-09-05 08:29:01 +1000611 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_dqid_t id,
613 xfs_dquot_t *dqp, /* dquot to get filled in */
614 uint flags)
615{
616 xfs_disk_dquot_t *ddqp;
617 xfs_buf_t *bp;
618 int error;
Tim Shimminefa092f2005-09-05 08:29:01 +1000619 xfs_trans_t *tp;
620
621 ASSERT(tpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000623 trace_xfs_dqread(dqp);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /*
626 * get a pointer to the on-disk dquot and the buffer containing it
627 * dqp already knows its own type (GROUP/USER).
628 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000629 if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return (error);
631 }
Tim Shimminefa092f2005-09-05 08:29:01 +1000632 tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* copy everything from disk dquot to the incore dquot */
635 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
Christoph Hellwig1149d962005-11-02 15:01:12 +1100636 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 xfs_qm_dquot_logitem_init(dqp);
638
639 /*
640 * Reservation counters are defined as reservation plus current usage
641 * to avoid having to add everytime.
642 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100643 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
644 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
645 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* Mark the buf so that this will stay incore a little longer */
648 XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
649
650 /*
651 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
652 * So we need to release with xfs_trans_brelse().
653 * The strategy here is identical to that of inodes; we lock
654 * the dquot in xfs_qm_dqget() before making it accessible to
655 * others. This is because dquots, like inodes, need a good level of
656 * concurrency, and we don't want to take locks on the entire buffers
657 * for dquot accesses.
658 * Note also that the dquot buffer may even be dirty at this point, if
659 * this particular dquot was repaired. We still aren't afraid to
660 * brelse it because we have the changes incore.
661 */
662 ASSERT(XFS_BUF_ISBUSY(bp));
663 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
664 xfs_trans_brelse(tp, bp);
665
666 return (error);
667}
668
669
670/*
671 * allocate an incore dquot from the kernel heap,
672 * and fill its core with quota information kept on disk.
673 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
674 * if it wasn't already allocated.
675 */
676STATIC int
677xfs_qm_idtodq(
678 xfs_mount_t *mp,
679 xfs_dqid_t id, /* gid or uid, depending on type */
680 uint type, /* UDQUOT or GDQUOT */
681 uint flags, /* DQALLOC, DQREPAIR */
682 xfs_dquot_t **O_dqpp)/* OUT : incore dquot, not locked */
683{
684 xfs_dquot_t *dqp;
685 int error;
686 xfs_trans_t *tp;
687 int cancelflags=0;
688
689 dqp = xfs_qm_dqinit(mp, id, type);
690 tp = NULL;
691 if (flags & XFS_QMOPT_DQALLOC) {
692 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
693 if ((error = xfs_trans_reserve(tp,
694 XFS_QM_DQALLOC_SPACE_RES(mp),
695 XFS_WRITE_LOG_RES(mp) +
696 BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
697 128,
698 0,
699 XFS_TRANS_PERM_LOG_RES,
700 XFS_WRITE_LOG_COUNT))) {
701 cancelflags = 0;
702 goto error0;
703 }
704 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
705 }
706
707 /*
708 * Read it from disk; xfs_dqread() takes care of
709 * all the necessary initialization of dquot's fields (locks, etc)
710 */
Tim Shimminefa092f2005-09-05 08:29:01 +1000711 if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * This can happen if quotas got turned off (ESRCH),
714 * or if the dquot didn't exist on disk and we ask to
715 * allocate (ENOENT).
716 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000717 trace_xfs_dqread_fail(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cancelflags |= XFS_TRANS_ABORT;
719 goto error0;
720 }
721 if (tp) {
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000722 if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto error1;
724 }
725
726 *O_dqpp = dqp;
727 return (0);
728
729 error0:
730 ASSERT(error);
731 if (tp)
732 xfs_trans_cancel(tp, cancelflags);
733 error1:
734 xfs_qm_dqdestroy(dqp);
735 *O_dqpp = NULL;
736 return (error);
737}
738
739/*
740 * Lookup a dquot in the incore dquot hashtable. We keep two separate
741 * hashtables for user and group dquots; and, these are global tables
742 * inside the XQM, not per-filesystem tables.
743 * The hash chain must be locked by caller, and it is left locked
744 * on return. Returning dquot is locked.
745 */
746STATIC int
747xfs_qm_dqlookup(
748 xfs_mount_t *mp,
749 xfs_dqid_t id,
750 xfs_dqhash_t *qh,
751 xfs_dquot_t **O_dqpp)
752{
753 xfs_dquot_t *dqp;
754 uint flist_locked;
755 xfs_dquot_t *d;
756
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100757 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 flist_locked = B_FALSE;
760
761 /*
762 * Traverse the hashchain looking for a match
763 */
764 for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
765 /*
766 * We already have the hashlock. We don't need the
767 * dqlock to look at the id field of the dquot, since the
768 * id can't be modified without the hashlock anyway.
769 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100770 if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000771 trace_xfs_dqlookup_found(dqp);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
774 * All in core dquots must be on the dqlist of mp
775 */
Dave Chinner3a254042010-04-13 15:06:48 +1000776 ASSERT(!list_empty(&dqp->q_mplist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 xfs_dqlock(dqp);
779 if (dqp->q_nrefs == 0) {
780 ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
781 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000782 trace_xfs_dqlookup_want(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /*
785 * We may have raced with dqreclaim_one()
786 * (and lost). So, flag that we don't
787 * want the dquot to be reclaimed.
788 */
789 dqp->dq_flags |= XFS_DQ_WANT;
790 xfs_dqunlock(dqp);
791 xfs_qm_freelist_lock(xfs_Gqm);
792 xfs_dqlock(dqp);
793 dqp->dq_flags &= ~(XFS_DQ_WANT);
794 }
795 flist_locked = B_TRUE;
796 }
797
798 /*
799 * id couldn't have changed; we had the hashlock all
800 * along
801 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100802 ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (flist_locked) {
805 if (dqp->q_nrefs != 0) {
806 xfs_qm_freelist_unlock(xfs_Gqm);
807 flist_locked = B_FALSE;
808 } else {
809 /*
810 * take it off the freelist
811 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000812 trace_xfs_dqlookup_freelist(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 XQM_FREELIST_REMOVE(dqp);
814 /* xfs_qm_freelist_print(&(xfs_Gqm->
815 qm_dqfreelist),
816 "after removal"); */
817 }
818 }
819
820 /*
821 * grab a reference
822 */
823 XFS_DQHOLD(dqp);
824
825 if (flist_locked)
826 xfs_qm_freelist_unlock(xfs_Gqm);
827 /*
828 * move the dquot to the front of the hashchain
829 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100830 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (dqp->HL_PREVP != &qh->qh_next) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000832 trace_xfs_dqlookup_move(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if ((d = dqp->HL_NEXT))
834 d->HL_PREVP = dqp->HL_PREVP;
835 *(dqp->HL_PREVP) = d;
836 d = qh->qh_next;
837 d->HL_PREVP = &dqp->HL_NEXT;
838 dqp->HL_NEXT = d;
839 dqp->HL_PREVP = &qh->qh_next;
840 qh->qh_next = dqp;
841 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000842 trace_xfs_dqlookup_done(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 *O_dqpp = dqp;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100844 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return (0);
846 }
847 }
848
849 *O_dqpp = NULL;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100850 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return (1);
852}
853
854/*
855 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
856 * a locked dquot, doing an allocation (if requested) as needed.
857 * When both an inode and an id are given, the inode's id takes precedence.
858 * That is, if the id changes while we don't hold the ilock inside this
859 * function, the new dquot is returned, not necessarily the one requested
860 * in the id argument.
861 */
862int
863xfs_qm_dqget(
864 xfs_mount_t *mp,
865 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000866 xfs_dqid_t id, /* uid/projid/gid depending on type */
867 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
869 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
870{
871 xfs_dquot_t *dqp;
872 xfs_dqhash_t *h;
873 uint version;
874 int error;
875
876 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
877 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000878 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
880 return (ESRCH);
881 }
882 h = XFS_DQ_HASH(mp, id, type);
883
884#ifdef DEBUG
885 if (xfs_do_dqerror) {
886 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
887 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
888 cmn_err(CE_DEBUG, "Returning error in dqget");
889 return (EIO);
890 }
891 }
892#endif
893
894 again:
895
896#ifdef DEBUG
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000897 ASSERT(type == XFS_DQ_USER ||
898 type == XFS_DQ_PROJ ||
899 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (ip) {
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000901 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (type == XFS_DQ_USER)
903 ASSERT(ip->i_udquot == NULL);
904 else
905 ASSERT(ip->i_gdquot == NULL);
906 }
907#endif
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100908 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /*
911 * Look in the cache (hashtable).
912 * The chain is kept locked during lookup.
913 */
914 if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
915 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
916 /*
917 * The dquot was found, moved to the front of the chain,
918 * taken off the freelist if it was on it, and locked
919 * at this point. Just unlock the hashchain and return.
920 */
921 ASSERT(*O_dqpp);
922 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100923 mutex_unlock(&h->qh_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000924 trace_xfs_dqget_hit(*O_dqpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return (0); /* success */
926 }
927 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
928
929 /*
930 * Dquot cache miss. We don't want to keep the inode lock across
931 * a (potential) disk read. Also we don't want to deal with the lock
932 * ordering between quotainode and this inode. OTOH, dropping the inode
933 * lock here means dealing with a chown that can happen before
934 * we re-acquire the lock.
935 */
936 if (ip)
937 xfs_iunlock(ip, XFS_ILOCK_EXCL);
938 /*
939 * Save the hashchain version stamp, and unlock the chain, so that
940 * we don't keep the lock across a disk read
941 */
942 version = h->qh_version;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100943 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /*
946 * Allocate the dquot on the kernel heap, and read the ondisk
947 * portion off the disk. Also, do all the necessary initialization
948 * This can return ENOENT if dquot didn't exist on disk and we didn't
949 * ask it to allocate; ESRCH if quotas got turned off suddenly.
950 */
951 if ((error = xfs_qm_idtodq(mp, id, type,
952 flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
953 XFS_QMOPT_DOWARN),
954 &dqp))) {
955 if (ip)
956 xfs_ilock(ip, XFS_ILOCK_EXCL);
957 return (error);
958 }
959
960 /*
961 * See if this is mount code calling to look at the overall quota limits
962 * which are stored in the id == 0 user or group's dquot.
963 * Since we may not have done a quotacheck by this point, just return
964 * the dquot without attaching it to any hashtables, lists, etc, or even
965 * taking a reference.
966 * The caller must dqdestroy this once done.
967 */
968 if (flags & XFS_QMOPT_DQSUSER) {
969 ASSERT(id == 0);
970 ASSERT(! ip);
971 goto dqret;
972 }
973
974 /*
975 * Dquot lock comes after hashlock in the lock ordering
976 */
977 if (ip) {
978 xfs_ilock(ip, XFS_ILOCK_EXCL);
979 if (! XFS_IS_DQTYPE_ON(mp, type)) {
980 /* inode stays locked on return */
981 xfs_qm_dqdestroy(dqp);
982 return XFS_ERROR(ESRCH);
983 }
984 /*
985 * A dquot could be attached to this inode by now, since
986 * we had dropped the ilock.
987 */
988 if (type == XFS_DQ_USER) {
989 if (ip->i_udquot) {
990 xfs_qm_dqdestroy(dqp);
991 dqp = ip->i_udquot;
992 xfs_dqlock(dqp);
993 goto dqret;
994 }
995 } else {
996 if (ip->i_gdquot) {
997 xfs_qm_dqdestroy(dqp);
998 dqp = ip->i_gdquot;
999 xfs_dqlock(dqp);
1000 goto dqret;
1001 }
1002 }
1003 }
1004
1005 /*
1006 * Hashlock comes after ilock in lock order
1007 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001008 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (version != h->qh_version) {
1010 xfs_dquot_t *tmpdqp;
1011 /*
1012 * Now, see if somebody else put the dquot in the
1013 * hashtable before us. This can happen because we didn't
1014 * keep the hashchain lock. We don't have to worry about
1015 * lock order between the two dquots here since dqp isn't
1016 * on any findable lists yet.
1017 */
1018 if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1019 /*
1020 * Duplicate found. Just throw away the new dquot
1021 * and start over.
1022 */
1023 xfs_qm_dqput(tmpdqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001024 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 xfs_qm_dqdestroy(dqp);
1026 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1027 goto again;
1028 }
1029 }
1030
1031 /*
1032 * Put the dquot at the beginning of the hash-chain and mp's list
1033 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1034 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001035 ASSERT(mutex_is_locked(&h->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 dqp->q_hash = h;
1037 XQM_HASHLIST_INSERT(h, dqp);
1038
1039 /*
1040 * Attach this dquot to this filesystem's list of all dquots,
1041 * kept inside the mount structure in m_quotainfo field
1042 */
Dave Chinner3a254042010-04-13 15:06:48 +10001043 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
1046 * We return a locked dquot to the caller, with a reference taken
1047 */
1048 xfs_dqlock(dqp);
1049 dqp->q_nrefs = 1;
1050
Dave Chinner3a254042010-04-13 15:06:48 +10001051 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
1052 mp->m_quotainfo->qi_dquots++;
1053 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001054 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 dqret:
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +10001056 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001057 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 *O_dqpp = dqp;
1059 return (0);
1060}
1061
1062
1063/*
1064 * Release a reference to the dquot (decrement ref-count)
1065 * and unlock it. If there is a group quota attached to this
1066 * dquot, carefully release that too without tripping over
1067 * deadlocks'n'stuff.
1068 */
1069void
1070xfs_qm_dqput(
1071 xfs_dquot_t *dqp)
1072{
1073 xfs_dquot_t *gdqp;
1074
1075 ASSERT(dqp->q_nrefs > 0);
1076 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001077
1078 trace_xfs_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (dqp->q_nrefs != 1) {
1081 dqp->q_nrefs--;
1082 xfs_dqunlock(dqp);
1083 return;
1084 }
1085
1086 /*
1087 * drop the dqlock and acquire the freelist and dqlock
1088 * in the right order; but try to get it out-of-order first
1089 */
1090 if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001091 trace_xfs_dqput_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 xfs_dqunlock(dqp);
1093 xfs_qm_freelist_lock(xfs_Gqm);
1094 xfs_dqlock(dqp);
1095 }
1096
1097 while (1) {
1098 gdqp = NULL;
1099
1100 /* We can't depend on nrefs being == 1 here */
1101 if (--dqp->q_nrefs == 0) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001102 trace_xfs_dqput_free(dqp);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * insert at end of the freelist.
1106 */
1107 XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1108
1109 /*
1110 * If we just added a udquot to the freelist, then
1111 * we want to release the gdquot reference that
1112 * it (probably) has. Otherwise it'll keep the
1113 * gdquot from getting reclaimed.
1114 */
1115 if ((gdqp = dqp->q_gdquot)) {
1116 /*
1117 * Avoid a recursive dqput call
1118 */
1119 xfs_dqlock(gdqp);
1120 dqp->q_gdquot = NULL;
1121 }
1122
1123 /* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1124 "@@@@@++ Free list (after append) @@@@@+");
1125 */
1126 }
1127 xfs_dqunlock(dqp);
1128
1129 /*
1130 * If we had a group quota inside the user quota as a hint,
1131 * release it now.
1132 */
1133 if (! gdqp)
1134 break;
1135 dqp = gdqp;
1136 }
1137 xfs_qm_freelist_unlock(xfs_Gqm);
1138}
1139
1140/*
1141 * Release a dquot. Flush it if dirty, then dqput() it.
1142 * dquot must not be locked.
1143 */
1144void
1145xfs_qm_dqrele(
1146 xfs_dquot_t *dqp)
1147{
Christoph Hellwig7d095252009-06-08 15:33:32 +02001148 if (!dqp)
1149 return;
1150
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001151 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 xfs_dqlock(dqp);
1154 /*
1155 * We don't care to flush it if the dquot is dirty here.
1156 * That will create stutters that we want to avoid.
1157 * Instead we do a delayed write when we try to reclaim
1158 * a dirty dquot. Also xfs_sync will take part of the burden...
1159 */
1160 xfs_qm_dqput(dqp);
1161}
1162
1163
1164/*
1165 * Write a modified dquot to disk.
1166 * The dquot must be locked and the flush lock too taken by caller.
1167 * The flush lock will not be unlocked until the dquot reaches the disk,
1168 * but the dquot is free to be unlocked and modified by the caller
1169 * in the interim. Dquot is still locked on return. This behavior is
1170 * identical to that of inodes.
1171 */
1172int
1173xfs_qm_dqflush(
1174 xfs_dquot_t *dqp,
1175 uint flags)
1176{
1177 xfs_mount_t *mp;
1178 xfs_buf_t *bp;
1179 xfs_disk_dquot_t *ddqp;
1180 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +10001183 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001184 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /*
David Chinner2f8a3ce2008-10-30 17:07:20 +11001187 * If not dirty, or it's pinned and we are not supposed to
1188 * block, nada.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
David Chinner2f8a3ce2008-10-30 17:07:20 +11001190 if (!XFS_DQ_IS_DIRTY(dqp) ||
Dave Chinner20026d92010-02-04 09:48:58 +11001191 (!(flags & SYNC_WAIT) && atomic_read(&dqp->q_pincount) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 xfs_dqfunlock(dqp);
David Chinner2f8a3ce2008-10-30 17:07:20 +11001193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 xfs_qm_dqunpin_wait(dqp);
1196
1197 /*
1198 * This may have been unpinned because the filesystem is shutting
1199 * down forcibly. If that's the case we must not write this dquot
1200 * to disk, because the log record didn't make it to disk!
1201 */
1202 if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1203 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1204 xfs_dqfunlock(dqp);
1205 return XFS_ERROR(EIO);
1206 }
1207
1208 /*
1209 * Get the buffer containing the on-disk dquot
1210 * We don't need a transaction envelope because we know that the
1211 * the ondisk-dquot has already been allocated for.
1212 */
1213 if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ASSERT(error != ENOENT);
1215 /*
1216 * Quotas could have gotten turned off (ESRCH)
1217 */
1218 xfs_dqfunlock(dqp);
1219 return (error);
1220 }
1221
Christoph Hellwig1149d962005-11-02 15:01:12 +11001222 if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001223 0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
Nathan Scott7d04a332006-06-09 14:58:38 +10001224 xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return XFS_ERROR(EIO);
1226 }
1227
1228 /* This is the only portion of data that needs to persist */
1229 memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1230
1231 /*
1232 * Clear the dirty field and remember the flush lsn for later use.
1233 */
1234 dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1235 mp = dqp->q_mount;
1236
David Chinner7b2e2a32008-10-30 17:39:12 +11001237 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1238 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * Attach an iodone routine so that we can remove this dquot from the
1242 * AIL and release the flush lock once the dquot is synced to disk.
1243 */
1244 xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1245 xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1246 /*
1247 * If the buffer is pinned then push on the log so we won't
1248 * get stuck waiting in the write for too long.
1249 */
1250 if (XFS_BUF_ISPINNED(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001251 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001252 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Dave Chinner20026d92010-02-04 09:48:58 +11001255 if (flags & SYNC_WAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 error = xfs_bwrite(mp, bp);
Dave Chinner20026d92010-02-04 09:48:58 +11001257 else
1258 xfs_bdwrite(mp, bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001259
1260 trace_xfs_dqflush_done(dqp);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /*
1263 * dqp is still locked, but caller is free to unlock it now.
1264 */
Dave Chinner20026d92010-02-04 09:48:58 +11001265 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267}
1268
1269/*
1270 * This is the dquot flushing I/O completion routine. It is called
1271 * from interrupt level when the buffer containing the dquot is
1272 * flushed to disk. It is responsible for removing the dquot logitem
1273 * from the AIL if it has not been re-logged, and unlocking the dquot's
1274 * flush lock. This behavior is very similar to that of inodes..
1275 */
1276/*ARGSUSED*/
1277STATIC void
1278xfs_qm_dqflush_done(
1279 xfs_buf_t *bp,
1280 xfs_dq_logitem_t *qip)
1281{
1282 xfs_dquot_t *dqp;
David Chinner783a2f62008-10-30 17:39:58 +11001283 struct xfs_ail *ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 dqp = qip->qli_dquot;
David Chinner783a2f62008-10-30 17:39:58 +11001286 ailp = qip->qli_item.li_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /*
1289 * We only want to pull the item from the AIL if its
1290 * location in the log has not changed since we started the flush.
1291 * Thus, we only bother if the dquot's lsn has
1292 * not changed. First we check the lsn outside the lock
1293 * since it's cheaper, and then we recheck while
1294 * holding the lock before removing the dquot from the AIL.
1295 */
1296 if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1297 qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1298
David Chinner783a2f62008-10-30 17:39:58 +11001299 /* xfs_trans_ail_delete() drops the AIL lock. */
1300 spin_lock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
David Chinner783a2f62008-10-30 17:39:58 +11001302 xfs_trans_ail_delete(ailp, (xfs_log_item_t*)qip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 else
David Chinner783a2f62008-10-30 17:39:58 +11001304 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
1307 /*
1308 * Release the dq's flush lock since we're done with it.
1309 */
1310 xfs_dqfunlock(dqp);
1311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313int
1314xfs_qm_dqlock_nowait(
1315 xfs_dquot_t *dqp)
1316{
David Chinnere1f49cf2008-08-13 16:41:43 +10001317 return mutex_trylock(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320void
1321xfs_dqlock(
1322 xfs_dquot_t *dqp)
1323{
David Chinnere1f49cf2008-08-13 16:41:43 +10001324 mutex_lock(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327void
1328xfs_dqunlock(
1329 xfs_dquot_t *dqp)
1330{
1331 mutex_unlock(&(dqp->q_qlock));
1332 if (dqp->q_logitem.qli_dquot == dqp) {
1333 /* Once was dqp->q_mount, but might just have been cleared */
David Chinner783a2f62008-10-30 17:39:58 +11001334 xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 (xfs_log_item_t*)&(dqp->q_logitem));
1336 }
1337}
1338
1339
1340void
1341xfs_dqunlock_nonotify(
1342 xfs_dquot_t *dqp)
1343{
1344 mutex_unlock(&(dqp->q_qlock));
1345}
1346
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001347/*
1348 * Lock two xfs_dquot structures.
1349 *
1350 * To avoid deadlocks we always lock the quota structure with
1351 * the lowerd id first.
1352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353void
1354xfs_dqlock2(
1355 xfs_dquot_t *d1,
1356 xfs_dquot_t *d2)
1357{
1358 if (d1 && d2) {
1359 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001360 if (be32_to_cpu(d1->q_core.d_id) >
1361 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001362 mutex_lock(&d2->q_qlock);
1363 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001365 mutex_lock(&d1->q_qlock);
1366 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001368 } else if (d1) {
1369 mutex_lock(&d1->q_qlock);
1370 } else if (d2) {
1371 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373}
1374
1375
1376/*
1377 * Take a dquot out of the mount's dqlist as well as the hashlist.
1378 * This is called via unmount as well as quotaoff, and the purge
1379 * will always succeed unless there are soft (temp) references
1380 * outstanding.
1381 *
1382 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1383 * that we're returning! XXXsup - not cool.
1384 */
1385/* ARGSUSED */
1386int
1387xfs_qm_dqpurge(
Denys Vlasenko4f0e8a92008-05-19 16:34:04 +10001388 xfs_dquot_t *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 xfs_dqhash_t *thishash;
David Chinner3c568362008-04-10 12:20:24 +10001391 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Dave Chinner3a254042010-04-13 15:06:48 +10001393 ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001394 ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 xfs_dqlock(dqp);
1397 /*
1398 * We really can't afford to purge a dquot that is
1399 * referenced, because these are hard refs.
1400 * It shouldn't happen in general because we went thru _all_ inodes in
1401 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1402 * However it is possible that we have dquots with temporary
1403 * references that are not attached to an inode. e.g. see xfs_setattr().
1404 */
1405 if (dqp->q_nrefs != 0) {
1406 xfs_dqunlock(dqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001407 mutex_unlock(&dqp->q_hash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return (1);
1409 }
1410
1411 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1412
1413 /*
1414 * If we're turning off quotas, we have to make sure that, for
1415 * example, we don't delete quota disk blocks while dquots are
1416 * in the process of getting written to those disk blocks.
1417 * This dquot might well be on AIL, and we can't leave it there
1418 * if we're turning off quotas. Basically, we need this flush
1419 * lock, and are willing to block on it.
1420 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001421 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
1423 * Block on the flush lock after nudging dquot buffer,
1424 * if it is incore.
1425 */
1426 xfs_qm_dqflock_pushbuf_wait(dqp);
1427 }
1428
1429 /*
1430 * XXXIf we're turning this type of quotas off, we don't care
1431 * about the dirty metadata sitting in this dquot. OTOH, if
1432 * we're unmounting, we do care, so we flush it and wait.
1433 */
1434 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10001435 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* dqflush unlocks dqflock */
1438 /*
1439 * Given that dqpurge is a very rare occurrence, it is OK
1440 * that we're holding the hashlist and mplist locks
1441 * across the disk write. But, ... XXXsup
1442 *
1443 * We don't care about getting disk errors here. We need
1444 * to purge this dquot anyway, so we go ahead regardless.
1445 */
Dave Chinner20026d92010-02-04 09:48:58 +11001446 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
David Chinner3c568362008-04-10 12:20:24 +10001447 if (error)
1448 xfs_fs_cmn_err(CE_WARN, mp,
1449 "xfs_qm_dqpurge: dquot %p flush failed", dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 xfs_dqflock(dqp);
1451 }
Peter Leckiebc3048e2008-10-30 17:05:04 +11001452 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1454 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1455
1456 thishash = dqp->q_hash;
1457 XQM_HASHLIST_REMOVE(thishash, dqp);
Dave Chinner3a254042010-04-13 15:06:48 +10001458 list_del_init(&dqp->q_mplist);
1459 mp->m_quotainfo->qi_dqreclaims++;
1460 mp->m_quotainfo->qi_dquots--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /*
1462 * XXX Move this to the front of the freelist, if we can get the
1463 * freelist lock.
1464 */
1465 ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1466
1467 dqp->q_mount = NULL;
1468 dqp->q_hash = NULL;
1469 dqp->dq_flags = XFS_DQ_INACTIVE;
1470 memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1471 xfs_dqfunlock(dqp);
1472 xfs_dqunlock(dqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +01001473 mutex_unlock(&thishash->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return (0);
1475}
1476
1477
1478#ifdef QUOTADEBUG
1479void
1480xfs_qm_dqprint(xfs_dquot_t *dqp)
1481{
1482 cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1483 cmn_err(CE_DEBUG, "---- dquotID = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001484 (int)be32_to_cpu(dqp->q_core.d_id));
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001485 cmn_err(CE_DEBUG, "---- type = %s", DQFLAGTO_TYPESTR(dqp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 cmn_err(CE_DEBUG, "---- fs = 0x%p", dqp->q_mount);
1487 cmn_err(CE_DEBUG, "---- blkno = 0x%x", (int) dqp->q_blkno);
1488 cmn_err(CE_DEBUG, "---- boffset = 0x%x", (int) dqp->q_bufoffset);
1489 cmn_err(CE_DEBUG, "---- blkhlimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001490 be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1491 (int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 cmn_err(CE_DEBUG, "---- blkslimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001493 be64_to_cpu(dqp->q_core.d_blk_softlimit),
1494 (int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 cmn_err(CE_DEBUG, "---- inohlimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001496 be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1497 (int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 cmn_err(CE_DEBUG, "---- inoslimit = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001499 be64_to_cpu(dqp->q_core.d_ino_softlimit),
1500 (int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 cmn_err(CE_DEBUG, "---- bcount = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001502 be64_to_cpu(dqp->q_core.d_bcount),
1503 (int)be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 cmn_err(CE_DEBUG, "---- icount = %Lu (0x%x)",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001505 be64_to_cpu(dqp->q_core.d_icount),
1506 (int)be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 cmn_err(CE_DEBUG, "---- btimer = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001508 (int)be32_to_cpu(dqp->q_core.d_btimer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 cmn_err(CE_DEBUG, "---- itimer = %d",
Christoph Hellwig1149d962005-11-02 15:01:12 +11001510 (int)be32_to_cpu(dqp->q_core.d_itimer));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 cmn_err(CE_DEBUG, "---------------------------");
1512}
1513#endif
1514
1515/*
1516 * Give the buffer a little push if it is incore and
1517 * wait on the flush lock.
1518 */
1519void
1520xfs_qm_dqflock_pushbuf_wait(
1521 xfs_dquot_t *dqp)
1522{
1523 xfs_buf_t *bp;
1524
1525 /*
1526 * Check to see if the dquot has been flushed delayed
1527 * write. If so, grab its buffer and send it
1528 * out immediately. We'll be able to acquire
1529 * the flush lock when the I/O completes.
1530 */
1531 bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001532 XFS_QI_DQCHUNKLEN(dqp->q_mount), XBF_TRYLOCK);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001533 if (!bp)
1534 goto out_lock;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001535
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001536 if (XFS_BUF_ISDELAYWRITE(bp)) {
1537 if (XFS_BUF_ISPINNED(bp))
1538 xfs_log_force(dqp->q_mount, 0);
1539 xfs_buf_delwri_promote(bp);
1540 wake_up_process(bp->b_target->bt_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001542 xfs_buf_relse(bp);
1543out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 xfs_dqflock(dqp);
1545}