blob: 71e615fef174336fe92814bd74db7627a966c3fc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
31#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_rtalloc.h"
33#include "xfs_error.h"
34#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_attr.h"
36#include "xfs_buf_item.h"
37#include "xfs_trans_space.h"
38#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_qm.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
Christoph Hellwigbf72de32011-12-06 21:58:19 +000043 * Lock order:
44 *
45 * ip->i_lock
46 * qh->qh_lock
47 * qi->qi_dqlist_lock
48 * dquot->q_qlock (xfs_dqlock() and friends)
49 * dquot->q_flush (xfs_dqflock() and friends)
50 * xfs_Gqm->qm_dqfrlist_lock
51 *
52 * If two dquots need to be locked the order is user before group/project,
53 * otherwise by the lowest id first, see xfs_dqlock2.
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef DEBUG
57xfs_buftarg_t *xfs_dqerror_target;
58int xfs_do_dqerror;
59int xfs_dqreq_num;
60int xfs_dqerror_mod = 33;
61#endif
62
Christoph Hellwig98b8c7a2009-01-19 02:03:25 +010063static struct lock_class_key xfs_dquot_other_class;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * This is called to free all the memory associated with a dquot
67 */
68void
69xfs_qm_dqdestroy(
70 xfs_dquot_t *dqp)
71{
Dave Chinner3a8406f2010-04-13 15:06:52 +100072 ASSERT(list_empty(&dqp->q_freelist));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 mutex_destroy(&dqp->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 atomic_dec(&xfs_Gqm->qm_totaldquots);
78}
79
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * If default limits are in force, push them into the dquot now.
82 * We overwrite the dquot limits only if they are zero and this
83 * is not the root dquot.
84 */
85void
86xfs_qm_adjust_dqlimits(
87 xfs_mount_t *mp,
88 xfs_disk_dquot_t *d)
89{
90 xfs_quotainfo_t *q = mp->m_quotainfo;
91
92 ASSERT(d->d_id);
93
94 if (q->qi_bsoftlimit && !d->d_blk_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +110095 d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (q->qi_bhardlimit && !d->d_blk_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +110097 d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (q->qi_isoftlimit && !d->d_ino_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +110099 d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (q->qi_ihardlimit && !d->d_ino_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100101 d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100103 d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100105 d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
109 * Check the limits and timers of a dquot and start or reset timers
110 * if necessary.
111 * This gets called even when quota enforcement is OFF, which makes our
112 * life a little less complicated. (We just don't reject any quota
113 * reservations in that case, when enforcement is off).
114 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
115 * enforcement's off.
116 * In contrast, warnings are a little different in that they don't
Nathan Scott754002b2005-06-21 15:49:06 +1000117 * 'automatically' get started when limits get exceeded. They do
118 * get reset to zero, however, when we find the count to be under
119 * the soft limit (they are only ever set non-zero via userspace).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121void
122xfs_qm_adjust_dqtimers(
123 xfs_mount_t *mp,
124 xfs_disk_dquot_t *d)
125{
126 ASSERT(d->d_id);
127
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200128#ifdef DEBUG
Christoph Hellwig1149d962005-11-02 15:01:12 +1100129 if (d->d_blk_hardlimit)
130 ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
131 be64_to_cpu(d->d_blk_hardlimit));
132 if (d->d_ino_hardlimit)
133 ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
134 be64_to_cpu(d->d_ino_hardlimit));
135 if (d->d_rtb_hardlimit)
136 ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
137 be64_to_cpu(d->d_rtb_hardlimit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!d->d_btimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100141 if ((d->d_blk_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000142 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100143 be64_to_cpu(d->d_blk_softlimit))) ||
144 (d->d_blk_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000145 (be64_to_cpu(d->d_bcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100146 be64_to_cpu(d->d_blk_hardlimit)))) {
147 d->d_btimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000148 mp->m_quotainfo->qi_btimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000149 } else {
150 d->d_bwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 } else {
153 if ((!d->d_blk_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000154 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100155 be64_to_cpu(d->d_blk_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 (!d->d_blk_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000157 (be64_to_cpu(d->d_bcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100158 be64_to_cpu(d->d_blk_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 d->d_btimer = 0;
160 }
161 }
162
163 if (!d->d_itimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100164 if ((d->d_ino_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000165 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100166 be64_to_cpu(d->d_ino_softlimit))) ||
167 (d->d_ino_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000168 (be64_to_cpu(d->d_icount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100169 be64_to_cpu(d->d_ino_hardlimit)))) {
170 d->d_itimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000171 mp->m_quotainfo->qi_itimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000172 } else {
173 d->d_iwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 } else {
176 if ((!d->d_ino_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000177 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100178 be64_to_cpu(d->d_ino_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 (!d->d_ino_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000180 (be64_to_cpu(d->d_icount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100181 be64_to_cpu(d->d_ino_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 d->d_itimer = 0;
183 }
184 }
185
186 if (!d->d_rtbtimer) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100187 if ((d->d_rtb_softlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000188 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100189 be64_to_cpu(d->d_rtb_softlimit))) ||
190 (d->d_rtb_hardlimit &&
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000191 (be64_to_cpu(d->d_rtbcount) >
Christoph Hellwig1149d962005-11-02 15:01:12 +1100192 be64_to_cpu(d->d_rtb_hardlimit)))) {
193 d->d_rtbtimer = cpu_to_be32(get_seconds() +
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000194 mp->m_quotainfo->qi_rtbtimelimit);
Nathan Scott754002b2005-06-21 15:49:06 +1000195 } else {
196 d->d_rtbwarns = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 } else {
199 if ((!d->d_rtb_softlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000200 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100201 be64_to_cpu(d->d_rtb_softlimit))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 (!d->d_rtb_hardlimit ||
Mitsuo Hayasakad0a3fe62012-02-06 12:50:07 +0000203 (be64_to_cpu(d->d_rtbcount) <=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100204 be64_to_cpu(d->d_rtb_hardlimit)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 d->d_rtbtimer = 0;
206 }
207 }
208}
209
210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * initialize a buffer full of dquots and log the whole thing
212 */
213STATIC void
214xfs_qm_init_dquot_blk(
215 xfs_trans_t *tp,
216 xfs_mount_t *mp,
217 xfs_dqid_t id,
218 uint type,
219 xfs_buf_t *bp)
220{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000221 struct xfs_quotainfo *q = mp->m_quotainfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 xfs_dqblk_t *d;
223 int curid, i;
224
225 ASSERT(tp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200226 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Chandra Seetharaman62926042011-07-22 23:40:15 +0000228 d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * ID of the first dquot in the block - id's are zero based.
232 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000233 curid = id - (id % q->qi_dqperchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ASSERT(curid >= 0);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000235 memset(d, 0, BBTOB(q->qi_dqchunklen));
Christoph Hellwig49d35a52011-12-06 21:58:23 +0000236 for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
237 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
238 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
239 d->dd_diskdq.d_id = cpu_to_be32(curid);
240 d->dd_diskdq.d_flags = type;
241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xfs_trans_dquot_buf(tp, bp,
Dave Chinnerc1155412010-05-07 11:05:19 +1000244 (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
245 ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
246 XFS_BLF_GDQUOT_BUF)));
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000247 xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250
251
252/*
253 * Allocate a block and fill it with dquots.
254 * This is called when the bmapi finds a hole.
255 */
256STATIC int
257xfs_qm_dqalloc(
Tim Shimminefa092f2005-09-05 08:29:01 +1000258 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 xfs_mount_t *mp,
260 xfs_dquot_t *dqp,
261 xfs_inode_t *quotip,
262 xfs_fileoff_t offset_fsb,
263 xfs_buf_t **O_bpp)
264{
265 xfs_fsblock_t firstblock;
266 xfs_bmap_free_t flist;
267 xfs_bmbt_irec_t map;
268 int nmaps, error, committed;
269 xfs_buf_t *bp;
Tim Shimminefa092f2005-09-05 08:29:01 +1000270 xfs_trans_t *tp = *tpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 ASSERT(tp != NULL);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000273
274 trace_xfs_dqalloc(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * Initialize the bmap freelist prior to calling bmapi code.
278 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600279 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 xfs_ilock(quotip, XFS_ILOCK_EXCL);
281 /*
282 * Return if this type of quotas is turned off while we didn't
283 * have an inode lock
284 */
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000285 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
287 return (ESRCH);
288 }
289
Christoph Hellwigddc34152011-09-19 15:00:54 +0000290 xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 nmaps = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +0000292 error = xfs_bmapi_write(tp, quotip, offset_fsb,
293 XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
294 &firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
295 &map, &nmaps, &flist);
296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
299 ASSERT(nmaps == 1);
300 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
301 (map.br_startblock != HOLESTARTBLOCK));
302
303 /*
304 * Keep track of the blkno to save a lookup later
305 */
306 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
307
308 /* now we can just get the buffer (there's nothing to read yet) */
309 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
310 dqp->q_blkno,
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000311 mp->m_quotainfo->qi_dqchunklen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000313
314 error = xfs_buf_geterror(bp);
315 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto error1;
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +0000317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Make a chunk of dquots out of this buffer and log
320 * the entire thing.
321 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100322 xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000323 dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Tim Shimminefa092f2005-09-05 08:29:01 +1000325 /*
326 * xfs_bmap_finish() may commit the current transaction and
327 * start a second transaction if the freelist is not empty.
328 *
329 * Since we still want to modify this buffer, we need to
330 * ensure that the buffer is not released on commit of
331 * the first transaction and ensure the buffer is added to the
332 * second transaction.
333 *
334 * If there is only one transaction then don't stop the buffer
335 * from being released when it commits later on.
336 */
337
338 xfs_trans_bhold(tp, bp);
339
Eric Sandeenf7c99b62007-02-10 18:37:16 +1100340 if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto error1;
342 }
343
Tim Shimminefa092f2005-09-05 08:29:01 +1000344 if (committed) {
345 tp = *tpp;
346 xfs_trans_bjoin(tp, bp);
347 } else {
348 xfs_trans_bhold_release(tp, bp);
349 }
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *O_bpp = bp;
352 return 0;
353
354 error1:
355 xfs_bmap_cancel(&flist);
356 error0:
357 xfs_iunlock(quotip, XFS_ILOCK_EXCL);
358
359 return (error);
360}
361
362/*
363 * Maps a dquot to the buffer containing its on-disk version.
364 * This returns a ptr to the buffer containing the on-disk dquot
365 * in the bpp param, and a ptr to the on-disk dquot within that buffer
366 */
367STATIC int
368xfs_qm_dqtobp(
Tim Shimminefa092f2005-09-05 08:29:01 +1000369 xfs_trans_t **tpp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 xfs_dquot_t *dqp,
371 xfs_disk_dquot_t **O_ddpp,
372 xfs_buf_t **O_bpp,
373 uint flags)
374{
375 xfs_bmbt_irec_t map;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000376 int nmaps = 1, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfs_buf_t *bp;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000378 xfs_inode_t *quotip = XFS_DQ_TO_QIP(dqp);
379 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfs_disk_dquot_t *ddq;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000381 xfs_dqid_t id = be32_to_cpu(dqp->q_core.d_id);
Tim Shimminefa092f2005-09-05 08:29:01 +1000382 xfs_trans_t *tp = (tpp ? *tpp : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000384 dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000386 xfs_ilock(quotip, XFS_ILOCK_SHARED);
Chandra Seetharaman6967b962012-01-23 17:31:25 +0000387 if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000389 * Return if this type of quotas is turned off while we
390 * didn't have the quota inode lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000393 return ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000397 * Find the block map; no allocations yet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Dave Chinner5c8ed202011-09-18 20:40:45 +0000399 error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
400 XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000401
402 xfs_iunlock(quotip, XFS_ILOCK_SHARED);
403 if (error)
404 return error;
405
406 ASSERT(nmaps == 1);
407 ASSERT(map.br_blockcount == 1);
408
409 /*
410 * Offset of dquot in the (fixed sized) dquot chunk.
411 */
412 dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
413 sizeof(xfs_dqblk_t);
414
415 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
416 if (map.br_startblock == HOLESTARTBLOCK) {
417 /*
418 * We don't allocate unless we're asked to
419 */
420 if (!(flags & XFS_QMOPT_DQALLOC))
421 return ENOENT;
422
423 ASSERT(tp);
424 error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
425 dqp->q_fileoffset, &bp);
426 if (error)
427 return error;
428 tp = *tpp;
429 } else {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000430 trace_xfs_dqtobp_read(dqp);
431
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000432 /*
433 * store the blkno etc so that we don't have to do the
434 * mapping all the time
435 */
436 dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
437
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000438 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
439 dqp->q_blkno,
440 mp->m_quotainfo->qi_dqchunklen,
441 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (error || !bp)
443 return XFS_ERROR(error);
444 }
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000445
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200446 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /*
449 * calculate the location of the dquot inside the buffer.
450 */
Chandra Seetharaman62926042011-07-22 23:40:15 +0000451 ddq = bp->b_addr + dqp->q_bufoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /*
454 * A simple sanity check in case we got a corrupted dquot...
455 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100456 error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100458 "dqtobp");
459 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!(flags & XFS_QMOPT_DQREPAIR)) {
461 xfs_trans_brelse(tp, bp);
462 return XFS_ERROR(EIO);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 *O_bpp = bp;
467 *O_ddpp = ddq;
468
469 return (0);
470}
471
472
473/*
474 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
475 * and release the buffer immediately.
476 *
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000477 * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000479int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480xfs_qm_dqread(
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000481 struct xfs_mount *mp,
482 xfs_dqid_t id,
483 uint type,
484 uint flags,
485 struct xfs_dquot **O_dqpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000487 struct xfs_dquot *dqp;
488 struct xfs_disk_dquot *ddqp;
489 struct xfs_buf *bp;
490 struct xfs_trans *tp = NULL;
491 int error;
492 int cancelflags = 0;
Tim Shimminefa092f2005-09-05 08:29:01 +1000493
Christoph Hellwig92b2e5b32012-02-01 13:57:20 +0000494
495 dqp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);
496
497 dqp->dq_flags = type;
498 dqp->q_core.d_id = cpu_to_be32(id);
499 dqp->q_mount = mp;
500 INIT_LIST_HEAD(&dqp->q_freelist);
501 mutex_init(&dqp->q_qlock);
502 init_waitqueue_head(&dqp->q_pinwait);
503
504 /*
505 * Because we want to use a counting completion, complete
506 * the flush completion once to allow a single access to
507 * the flush completion without blocking.
508 */
509 init_completion(&dqp->q_flush);
510 complete(&dqp->q_flush);
511
512 /*
513 * Make sure group quotas have a different lock class than user
514 * quotas.
515 */
516 if (!(type & XFS_DQ_USER))
517 lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
518
519 atomic_inc(&xfs_Gqm->qm_totaldquots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000521 trace_xfs_dqread(dqp);
522
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000523 if (flags & XFS_QMOPT_DQALLOC) {
524 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
525 error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
526 XFS_WRITE_LOG_RES(mp) +
527 /*
528 * Round the chunklen up to the next multiple
529 * of 128 (buf log item chunk size)).
530 */
531 BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 + 128,
532 0,
533 XFS_TRANS_PERM_LOG_RES,
534 XFS_WRITE_LOG_COUNT);
535 if (error)
536 goto error1;
537 cancelflags = XFS_TRANS_RELEASE_LOG_RES;
538 }
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * get a pointer to the on-disk dquot and the buffer containing it
542 * dqp already knows its own type (GROUP/USER).
543 */
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000544 error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
545 if (error) {
546 /*
547 * This can happen if quotas got turned off (ESRCH),
548 * or if the dquot didn't exist on disk and we ask to
549 * allocate (ENOENT).
550 */
551 trace_xfs_dqread_fail(dqp);
552 cancelflags |= XFS_TRANS_ABORT;
553 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
556 /* copy everything from disk dquot to the incore dquot */
557 memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 xfs_qm_dquot_logitem_init(dqp);
559
560 /*
561 * Reservation counters are defined as reservation plus current usage
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300562 * to avoid having to add every time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Christoph Hellwig1149d962005-11-02 15:01:12 +1100564 dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
565 dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
566 dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* Mark the buf so that this will stay incore a little longer */
Christoph Hellwig38f23232011-10-10 16:52:45 +0000569 xfs_buf_set_ref(bp, XFS_DQUOT_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /*
572 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
573 * So we need to release with xfs_trans_brelse().
574 * The strategy here is identical to that of inodes; we lock
575 * the dquot in xfs_qm_dqget() before making it accessible to
576 * others. This is because dquots, like inodes, need a good level of
577 * concurrency, and we don't want to take locks on the entire buffers
578 * for dquot accesses.
579 * Note also that the dquot buffer may even be dirty at this point, if
580 * this particular dquot was repaired. We still aren't afraid to
581 * brelse it because we have the changes incore.
582 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200583 ASSERT(xfs_buf_islocked(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 xfs_trans_brelse(tp, bp);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (tp) {
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000587 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
588 if (error)
589 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 *O_dqpp = dqp;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000595error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (tp)
597 xfs_trans_cancel(tp, cancelflags);
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000598error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 xfs_qm_dqdestroy(dqp);
600 *O_dqpp = NULL;
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000601 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/*
605 * Lookup a dquot in the incore dquot hashtable. We keep two separate
606 * hashtables for user and group dquots; and, these are global tables
607 * inside the XQM, not per-filesystem tables.
608 * The hash chain must be locked by caller, and it is left locked
609 * on return. Returning dquot is locked.
610 */
611STATIC int
612xfs_qm_dqlookup(
613 xfs_mount_t *mp,
614 xfs_dqid_t id,
615 xfs_dqhash_t *qh,
616 xfs_dquot_t **O_dqpp)
617{
618 xfs_dquot_t *dqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100620 ASSERT(mutex_is_locked(&qh->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Traverse the hashchain looking for a match
624 */
Dave Chinnere6a81f12010-04-13 15:06:51 +1000625 list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * We already have the hashlock. We don't need the
628 * dqlock to look at the id field of the dquot, since the
629 * id can't be modified without the hashlock anyway.
630 */
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000631 if (be32_to_cpu(dqp->q_core.d_id) != id || dqp->q_mount != mp)
632 continue;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000633
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000634 trace_xfs_dqlookup_found(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000636 xfs_dqlock(dqp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000637 if (dqp->dq_flags & XFS_DQ_FREEING) {
638 *O_dqpp = NULL;
639 xfs_dqunlock(dqp);
640 return -1;
641 }
642
Christoph Hellwig78e55892011-12-06 21:58:22 +0000643 dqp->q_nrefs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000645 /*
646 * move the dquot to the front of the hashchain
647 */
648 list_move(&dqp->q_hashlist, &qh->qh_list);
649 trace_xfs_dqlookup_done(dqp);
650 *O_dqpp = dqp;
651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 *O_dqpp = NULL;
Christoph Hellwigbe7ffc32011-12-06 21:58:17 +0000655 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
659 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
660 * a locked dquot, doing an allocation (if requested) as needed.
661 * When both an inode and an id are given, the inode's id takes precedence.
662 * That is, if the id changes while we don't hold the ilock inside this
663 * function, the new dquot is returned, not necessarily the one requested
664 * in the id argument.
665 */
666int
667xfs_qm_dqget(
668 xfs_mount_t *mp,
669 xfs_inode_t *ip, /* locked inode (optional) */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000670 xfs_dqid_t id, /* uid/projid/gid depending on type */
671 uint type, /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 uint flags, /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
673 xfs_dquot_t **O_dqpp) /* OUT : locked incore dquot */
674{
Chandra Seetharaman36731412012-01-23 17:31:30 +0000675 xfs_dquot_t *dqp, *dqp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfs_dqhash_t *h;
677 uint version;
678 int error;
679
680 ASSERT(XFS_IS_QUOTA_RUNNING(mp));
681 if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000682 (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
684 return (ESRCH);
685 }
686 h = XFS_DQ_HASH(mp, id, type);
687
688#ifdef DEBUG
689 if (xfs_do_dqerror) {
690 if ((xfs_dqerror_target == mp->m_ddev_targp) &&
691 (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100692 xfs_debug(mp, "Returning error in dqget");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return (EIO);
694 }
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000697 ASSERT(type == XFS_DQ_USER ||
698 type == XFS_DQ_PROJ ||
699 type == XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (ip) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000701 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Chandra Seetharaman36731412012-01-23 17:31:30 +0000702 ASSERT(xfs_inode_dquot(ip, type) == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704#endif
Christoph Hellwig92678552011-12-06 21:58:18 +0000705
706restart:
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100707 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * Look in the cache (hashtable).
711 * The chain is kept locked during lookup.
712 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000713 switch (xfs_qm_dqlookup(mp, id, h, O_dqpp)) {
714 case -1:
715 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
716 mutex_unlock(&h->qh_lock);
717 delay(1);
718 goto restart;
719 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
721 /*
722 * The dquot was found, moved to the front of the chain,
723 * taken off the freelist if it was on it, and locked
724 * at this point. Just unlock the hashchain and return.
725 */
726 ASSERT(*O_dqpp);
727 ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100728 mutex_unlock(&h->qh_lock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000729 trace_xfs_dqget_hit(*O_dqpp);
Christoph Hellwig92678552011-12-06 21:58:18 +0000730 return 0; /* success */
731 default:
732 XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Dquot cache miss. We don't want to keep the inode lock across
738 * a (potential) disk read. Also we don't want to deal with the lock
739 * ordering between quotainode and this inode. OTOH, dropping the inode
740 * lock here means dealing with a chown that can happen before
741 * we re-acquire the lock.
742 */
743 if (ip)
744 xfs_iunlock(ip, XFS_ILOCK_EXCL);
745 /*
746 * Save the hashchain version stamp, and unlock the chain, so that
747 * we don't keep the lock across a disk read
748 */
749 version = h->qh_version;
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100750 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Christoph Hellwig97e7ade2011-12-06 21:58:24 +0000752 error = xfs_qm_dqread(mp, id, type, flags, &dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Christoph Hellwig7ae44402011-12-06 21:58:25 +0000754 if (ip)
755 xfs_ilock(ip, XFS_ILOCK_EXCL);
756
757 if (error)
758 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * Dquot lock comes after hashlock in the lock ordering
762 */
763 if (ip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * A dquot could be attached to this inode by now, since
766 * we had dropped the ilock.
767 */
Chandra Seetharaman36731412012-01-23 17:31:30 +0000768 if (xfs_this_quota_on(mp, type)) {
769 dqp1 = xfs_inode_dquot(ip, type);
770 if (dqp1) {
Christoph Hellwig191f8482010-04-20 17:01:53 +1000771 xfs_qm_dqdestroy(dqp);
Chandra Seetharaman36731412012-01-23 17:31:30 +0000772 dqp = dqp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 xfs_dqlock(dqp);
774 goto dqret;
775 }
776 } else {
Chandra Seetharaman36731412012-01-23 17:31:30 +0000777 /* inode stays locked on return */
778 xfs_qm_dqdestroy(dqp);
779 return XFS_ERROR(ESRCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781 }
782
783 /*
784 * Hashlock comes after ilock in lock order
785 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100786 mutex_lock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (version != h->qh_version) {
788 xfs_dquot_t *tmpdqp;
789 /*
790 * Now, see if somebody else put the dquot in the
791 * hashtable before us. This can happen because we didn't
792 * keep the hashchain lock. We don't have to worry about
793 * lock order between the two dquots here since dqp isn't
794 * on any findable lists yet.
795 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000796 switch (xfs_qm_dqlookup(mp, id, h, &tmpdqp)) {
797 case 0:
798 case -1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /*
Christoph Hellwig92678552011-12-06 21:58:18 +0000800 * Duplicate found, either in cache or on its way out.
801 * Just throw away the new dquot and start over.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Christoph Hellwig92678552011-12-06 21:58:18 +0000803 if (tmpdqp)
804 xfs_qm_dqput(tmpdqp);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100805 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfs_qm_dqdestroy(dqp);
807 XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
Christoph Hellwig92678552011-12-06 21:58:18 +0000808 goto restart;
809 default:
810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
813
814 /*
815 * Put the dquot at the beginning of the hash-chain and mp's list
816 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
817 */
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100818 ASSERT(mutex_is_locked(&h->qh_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 dqp->q_hash = h;
Dave Chinnere6a81f12010-04-13 15:06:51 +1000820 list_add(&dqp->q_hashlist, &h->qh_list);
821 h->qh_version++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * Attach this dquot to this filesystem's list of all dquots,
825 * kept inside the mount structure in m_quotainfo field
826 */
Dave Chinner3a254042010-04-13 15:06:48 +1000827 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /*
830 * We return a locked dquot to the caller, with a reference taken
831 */
832 xfs_dqlock(dqp);
833 dqp->q_nrefs = 1;
834
Dave Chinner3a254042010-04-13 15:06:48 +1000835 list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
836 mp->m_quotainfo->qi_dquots++;
837 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
Christoph Hellwigc9a192d2009-02-09 08:47:22 +0100838 mutex_unlock(&h->qh_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dqret:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000840 ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000841 trace_xfs_dqget_miss(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *O_dqpp = dqp;
843 return (0);
844}
845
846
847/*
848 * Release a reference to the dquot (decrement ref-count)
849 * and unlock it. If there is a group quota attached to this
850 * dquot, carefully release that too without tripping over
851 * deadlocks'n'stuff.
852 */
853void
854xfs_qm_dqput(
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000855 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000857 struct xfs_dquot *gdqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 ASSERT(dqp->q_nrefs > 0);
860 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000861
862 trace_xfs_dqput(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000864recurse:
865 if (--dqp->q_nrefs > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xfs_dqunlock(dqp);
867 return;
868 }
869
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000870 trace_xfs_dqput_free(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000872 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
873 if (list_empty(&dqp->q_freelist)) {
874 list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
875 xfs_Gqm->qm_dqfrlist_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Dave Chinner3a8406f2010-04-13 15:06:52 +1000877 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Christoph Hellwigbf72de32011-12-06 21:58:19 +0000878
879 /*
880 * If we just added a udquot to the freelist, then we want to release
881 * the gdquot reference that it (probably) has. Otherwise it'll keep
882 * the gdquot from getting reclaimed.
883 */
884 gdqp = dqp->q_gdquot;
885 if (gdqp) {
886 xfs_dqlock(gdqp);
887 dqp->q_gdquot = NULL;
888 }
889 xfs_dqunlock(dqp);
890
891 /*
892 * If we had a group quota hint, release it now.
893 */
894 if (gdqp) {
895 dqp = gdqp;
896 goto recurse;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
901 * Release a dquot. Flush it if dirty, then dqput() it.
902 * dquot must not be locked.
903 */
904void
905xfs_qm_dqrele(
906 xfs_dquot_t *dqp)
907{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200908 if (!dqp)
909 return;
910
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000911 trace_xfs_dqrele(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 xfs_dqlock(dqp);
914 /*
915 * We don't care to flush it if the dquot is dirty here.
916 * That will create stutters that we want to avoid.
917 * Instead we do a delayed write when we try to reclaim
918 * a dirty dquot. Also xfs_sync will take part of the burden...
919 */
920 xfs_qm_dqput(dqp);
921}
922
Christoph Hellwigca30b2a2010-06-23 18:11:15 +1000923/*
924 * This is the dquot flushing I/O completion routine. It is called
925 * from interrupt level when the buffer containing the dquot is
926 * flushed to disk. It is responsible for removing the dquot logitem
927 * from the AIL if it has not been re-logged, and unlocking the dquot's
928 * flush lock. This behavior is very similar to that of inodes..
929 */
930STATIC void
931xfs_qm_dqflush_done(
932 struct xfs_buf *bp,
933 struct xfs_log_item *lip)
934{
935 xfs_dq_logitem_t *qip = (struct xfs_dq_logitem *)lip;
936 xfs_dquot_t *dqp = qip->qli_dquot;
937 struct xfs_ail *ailp = lip->li_ailp;
938
939 /*
940 * We only want to pull the item from the AIL if its
941 * location in the log has not changed since we started the flush.
942 * Thus, we only bother if the dquot's lsn has
943 * not changed. First we check the lsn outside the lock
944 * since it's cheaper, and then we recheck while
945 * holding the lock before removing the dquot from the AIL.
946 */
947 if ((lip->li_flags & XFS_LI_IN_AIL) &&
948 lip->li_lsn == qip->qli_flush_lsn) {
949
950 /* xfs_trans_ail_delete() drops the AIL lock. */
951 spin_lock(&ailp->xa_lock);
952 if (lip->li_lsn == qip->qli_flush_lsn)
953 xfs_trans_ail_delete(ailp, lip);
954 else
955 spin_unlock(&ailp->xa_lock);
956 }
957
958 /*
959 * Release the dq's flush lock since we're done with it.
960 */
961 xfs_dqfunlock(dqp);
962}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964/*
965 * Write a modified dquot to disk.
966 * The dquot must be locked and the flush lock too taken by caller.
967 * The flush lock will not be unlocked until the dquot reaches the disk,
968 * but the dquot is free to be unlocked and modified by the caller
969 * in the interim. Dquot is still locked on return. This behavior is
970 * identical to that of inodes.
971 */
972int
973xfs_qm_dqflush(
974 xfs_dquot_t *dqp,
975 uint flags)
976{
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000977 struct xfs_mount *mp = dqp->q_mount;
978 struct xfs_buf *bp;
979 struct xfs_disk_dquot *ddqp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 ASSERT(XFS_DQ_IS_LOCKED(dqp));
David Chinnere1f49cf2008-08-13 16:41:43 +1000983 ASSERT(!completion_done(&dqp->q_flush));
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000984
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000985 trace_xfs_dqflush(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /*
Christoph Hellwigacecf1b2010-09-06 01:44:45 +0000988 * If not dirty, or it's pinned and we are not supposed to block, nada.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
David Chinner2f8a3ce2008-10-30 17:07:20 +1100990 if (!XFS_DQ_IS_DIRTY(dqp) ||
Christoph Hellwigfdedf282011-12-06 21:58:10 +0000991 ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 xfs_dqfunlock(dqp);
David Chinner2f8a3ce2008-10-30 17:07:20 +1100993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 xfs_qm_dqunpin_wait(dqp);
996
997 /*
998 * This may have been unpinned because the filesystem is shutting
999 * down forcibly. If that's the case we must not write this dquot
1000 * to disk, because the log record didn't make it to disk!
1001 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001002 if (XFS_FORCED_SHUTDOWN(mp)) {
1003 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 xfs_dqfunlock(dqp);
1005 return XFS_ERROR(EIO);
1006 }
1007
1008 /*
1009 * Get the buffer containing the on-disk dquot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001011 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1012 mp->m_quotainfo->qi_dqchunklen, 0, &bp);
1013 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ASSERT(error != ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 xfs_dqfunlock(dqp);
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001019 /*
1020 * Calculate the location of the dquot inside the buffer.
1021 */
Chandra Seetharaman62926042011-07-22 23:40:15 +00001022 ddqp = bp->b_addr + dqp->q_bufoffset;
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001023
1024 /*
1025 * A simple sanity check in case we got a corrupted dquot..
1026 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001027 error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
1028 XFS_QMOPT_DOWARN, "dqflush (incore copy)");
1029 if (error) {
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001030 xfs_buf_relse(bp);
1031 xfs_dqfunlock(dqp);
1032 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return XFS_ERROR(EIO);
1034 }
1035
1036 /* This is the only portion of data that needs to persist */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001037 memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /*
1040 * Clear the dirty field and remember the flush lsn for later use.
1041 */
Christoph Hellwigacecf1b2010-09-06 01:44:45 +00001042 dqp->dq_flags &= ~XFS_DQ_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
David Chinner7b2e2a32008-10-30 17:39:12 +11001044 xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1045 &dqp->q_logitem.qli_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /*
1048 * Attach an iodone routine so that we can remove this dquot from the
1049 * AIL and release the flush lock once the dquot is synced to disk.
1050 */
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10001051 xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
1052 &dqp->q_logitem.qli_item);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /*
1055 * If the buffer is pinned then push on the log so we won't
1056 * get stuck waiting in the write for too long.
1057 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001058 if (xfs_buf_ispinned(bp)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001059 trace_xfs_dqflush_force(dqp);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001060 xfs_log_force(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
Dave Chinner20026d92010-02-04 09:48:58 +11001063 if (flags & SYNC_WAIT)
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001064 error = xfs_bwrite(bp);
1065 else
Christoph Hellwig61551f12011-08-23 08:28:06 +00001066 xfs_buf_delwri_queue(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00001067
1068 xfs_buf_relse(bp);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001069
1070 trace_xfs_dqflush_done(dqp);
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * dqp is still locked, but caller is free to unlock it now.
1074 */
Dave Chinner20026d92010-02-04 09:48:58 +11001075 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077}
1078
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001079/*
1080 * Lock two xfs_dquot structures.
1081 *
1082 * To avoid deadlocks we always lock the quota structure with
1083 * the lowerd id first.
1084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085void
1086xfs_dqlock2(
1087 xfs_dquot_t *d1,
1088 xfs_dquot_t *d2)
1089{
1090 if (d1 && d2) {
1091 ASSERT(d1 != d2);
Christoph Hellwig1149d962005-11-02 15:01:12 +11001092 if (be32_to_cpu(d1->q_core.d_id) >
1093 be32_to_cpu(d2->q_core.d_id)) {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001094 mutex_lock(&d2->q_qlock);
1095 mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else {
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001097 mutex_lock(&d1->q_qlock);
1098 mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Christoph Hellwig5bb87a32009-01-19 02:03:19 +01001100 } else if (d1) {
1101 mutex_lock(&d1->q_qlock);
1102 } else if (d2) {
1103 mutex_lock(&d2->q_qlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
Christoph Hellwig92678552011-12-06 21:58:18 +00001108 * Take a dquot out of the mount's dqlist as well as the hashlist. This is
1109 * called via unmount as well as quotaoff, and the purge will always succeed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Christoph Hellwig92678552011-12-06 21:58:18 +00001111void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112xfs_qm_dqpurge(
Christoph Hellwig92678552011-12-06 21:58:18 +00001113 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Christoph Hellwig92678552011-12-06 21:58:18 +00001115 struct xfs_mount *mp = dqp->q_mount;
1116 struct xfs_dqhash *qh = dqp->q_hash;
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 xfs_dqlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /*
1121 * If we're turning off quotas, we have to make sure that, for
1122 * example, we don't delete quota disk blocks while dquots are
1123 * in the process of getting written to those disk blocks.
1124 * This dquot might well be on AIL, and we can't leave it there
1125 * if we're turning off quotas. Basically, we need this flush
1126 * lock, and are willing to block on it.
1127 */
David Chinnere1f49cf2008-08-13 16:41:43 +10001128 if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /*
1130 * Block on the flush lock after nudging dquot buffer,
1131 * if it is incore.
1132 */
Christoph Hellwig800b4842011-12-06 21:58:14 +00001133 xfs_dqflock_pushbuf_wait(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 /*
Christoph Hellwig92678552011-12-06 21:58:18 +00001137 * If we are turning this type of quotas off, we don't care
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * about the dirty metadata sitting in this dquot. OTOH, if
1139 * we're unmounting, we do care, so we flush it and wait.
1140 */
1141 if (XFS_DQ_IS_DIRTY(dqp)) {
David Chinner3c568362008-04-10 12:20:24 +10001142 int error;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * We don't care about getting disk errors here. We need
1146 * to purge this dquot anyway, so we go ahead regardless.
1147 */
Dave Chinner20026d92010-02-04 09:48:58 +11001148 error = xfs_qm_dqflush(dqp, SYNC_WAIT);
David Chinner3c568362008-04-10 12:20:24 +10001149 if (error)
Dave Chinner53487782011-03-07 10:05:35 +11001150 xfs_warn(mp, "%s: dquot %p flush failed",
1151 __func__, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 xfs_dqflock(dqp);
1153 }
Christoph Hellwig92678552011-12-06 21:58:18 +00001154
Peter Leckiebc3048e2008-10-30 17:05:04 +11001155 ASSERT(atomic_read(&dqp->q_pincount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1157 !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfs_dqfunlock(dqp);
1160 xfs_dqunlock(dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Christoph Hellwig92678552011-12-06 21:58:18 +00001162 mutex_lock(&qh->qh_lock);
1163 list_del_init(&dqp->q_hashlist);
1164 qh->qh_version++;
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001165 mutex_unlock(&qh->qh_lock);
1166
Christoph Hellwig92678552011-12-06 21:58:18 +00001167 mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
1168 list_del_init(&dqp->q_mplist);
1169 mp->m_quotainfo->qi_dqreclaims++;
1170 mp->m_quotainfo->qi_dquots--;
1171 mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
1172
1173 /*
1174 * We move dquots to the freelist as soon as their reference count
1175 * hits zero, so it really should be on the freelist here.
1176 */
1177 mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
1178 ASSERT(!list_empty(&dqp->q_freelist));
1179 list_del_init(&dqp->q_freelist);
1180 xfs_Gqm->qm_dqfrlist_cnt--;
1181 mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
1182
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001183 xfs_qm_dqdestroy(dqp);
Christoph Hellwig80a376b2011-12-06 21:58:16 +00001184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/*
1187 * Give the buffer a little push if it is incore and
1188 * wait on the flush lock.
1189 */
1190void
Christoph Hellwig800b4842011-12-06 21:58:14 +00001191xfs_dqflock_pushbuf_wait(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 xfs_dquot_t *dqp)
1193{
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001194 xfs_mount_t *mp = dqp->q_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 xfs_buf_t *bp;
1196
1197 /*
1198 * Check to see if the dquot has been flushed delayed
1199 * write. If so, grab its buffer and send it
1200 * out immediately. We'll be able to acquire
1201 * the flush lock when the I/O completes.
1202 */
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001203 bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
1204 mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001205 if (!bp)
1206 goto out_lock;
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001207
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001208 if (XFS_BUF_ISDELAYWRITE(bp)) {
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00001209 if (xfs_buf_ispinned(bp))
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +10001210 xfs_log_force(mp, 0);
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001211 xfs_buf_delwri_promote(bp);
1212 wake_up_process(bp->b_target->bt_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Dave Chinner7d6a7bd2010-01-26 15:13:41 +11001214 xfs_buf_relse(bp);
1215out_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 xfs_dqflock(dqp);
1217}