xfs_dquot.c 35.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18 19
 */
#include "xfs.h"
#include "xfs_fs.h"
20
#include "xfs_bit.h"
L
Linus Torvalds 已提交
21
#include "xfs_log.h"
22
#include "xfs_inum.h"
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_alloc.h"
#include "xfs_quota.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
#include "xfs_inode.h"
#include "xfs_bmap.h"
#include "xfs_rtalloc.h"
#include "xfs_error.h"
#include "xfs_itable.h"
#include "xfs_attr.h"
#include "xfs_buf_item.h"
#include "xfs_trans_space.h"
#include "xfs_trans_priv.h"
#include "xfs_qm.h"
C
Christoph Hellwig 已提交
40
#include "xfs_trace.h"
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63


/*
   LOCK ORDER

   inode lock		    (ilock)
   dquot hash-chain lock    (hashlock)
   xqm dquot freelist lock  (freelistlock
   mount's dquot list lock  (mplistlock)
   user dquot lock - lock ordering among dquots is based on the uid or gid
   group dquot lock - similar to udquots. Between the two dquots, the udquot
		      has to be locked first.
   pin lock - the dquot lock must be held to take this lock.
   flush lock - ditto.
*/

#ifdef DEBUG
xfs_buftarg_t *xfs_dqerror_target;
int xfs_do_dqerror;
int xfs_dqreq_num;
int xfs_dqerror_mod = 33;
#endif

64 65
static struct lock_class_key xfs_dquot_other_class;

L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73
/*
 * Allocate and initialize a dquot. We don't always allocate fresh memory;
 * we try to reclaim a free dquot if the number of incore dquots are above
 * a threshold.
 * The only field inside the core that gets initialized at this point
 * is the d_id field. The idea is to fill in the entire q_core
 * when we read in the on disk dquot.
 */
74
STATIC xfs_dquot_t *
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84
xfs_qm_dqinit(
	xfs_mount_t  *mp,
	xfs_dqid_t   id,
	uint	     type)
{
	xfs_dquot_t	*dqp;
	boolean_t	brandnewdquot;

	brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
	dqp->dq_flags = type;
85
	dqp->q_core.d_id = cpu_to_be32(id);
L
Linus Torvalds 已提交
86 87 88 89 90 91
	dqp->q_mount = mp;

	/*
	 * No need to re-initialize these if this is a reclaimed dquot.
	 */
	if (brandnewdquot) {
92
		INIT_LIST_HEAD(&dqp->q_freelist);
93
		mutex_init(&dqp->q_qlock);
P
Peter Leckie 已提交
94
		init_waitqueue_head(&dqp->q_pinwait);
L
Linus Torvalds 已提交
95

96 97 98 99 100 101 102 103
		/*
		 * Because we want to use a counting completion, complete
		 * the flush completion once to allow a single access to
		 * the flush completion without blocking.
		 */
		init_completion(&dqp->q_flush);
		complete(&dqp->q_flush);

C
Christoph Hellwig 已提交
104
		trace_xfs_dqinit(dqp);
L
Linus Torvalds 已提交
105 106 107 108 109
	} else {
		/*
		 * Only the q_core portion was zeroed in dqreclaim_one().
		 * So, we need to reset others.
		 */
110 111 112 113 114 115 116 117 118 119 120 121 122 123
		dqp->q_nrefs = 0;
		dqp->q_blkno = 0;
		INIT_LIST_HEAD(&dqp->q_mplist);
		INIT_LIST_HEAD(&dqp->q_hashlist);
		dqp->q_bufoffset = 0;
		dqp->q_fileoffset = 0;
		dqp->q_transp = NULL;
		dqp->q_gdquot = NULL;
		dqp->q_res_bcount = 0;
		dqp->q_res_icount = 0;
		dqp->q_res_rtbcount = 0;
		atomic_set(&dqp->q_pincount, 0);
		dqp->q_hash = NULL;
		ASSERT(list_empty(&dqp->q_freelist));
L
Linus Torvalds 已提交
124

C
Christoph Hellwig 已提交
125
		trace_xfs_dqreuse(dqp);
126 127 128 129 130 131 132 133 134
	}

	/*
	 * In either case we need to make sure group quotas have a different
	 * lock class than user quotas, to make sure lockdep knows we can
	 * locks of one of each at the same time.
	 */
	if (!(type & XFS_DQ_USER))
		lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148

	/*
	 * log item gets initialized later
	 */
	return (dqp);
}

/*
 * This is called to free all the memory associated with a dquot
 */
void
xfs_qm_dqdestroy(
	xfs_dquot_t	*dqp)
{
149
	ASSERT(list_empty(&dqp->q_freelist));
L
Linus Torvalds 已提交
150 151 152

	mutex_destroy(&dqp->q_qlock);
	kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
C
Christoph Hellwig 已提交
153

L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	atomic_dec(&xfs_Gqm->qm_totaldquots);
}

/*
 * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
 */
STATIC void
xfs_qm_dqinit_core(
	xfs_dqid_t	id,
	uint		type,
	xfs_dqblk_t	*d)
{
	/*
	 * Caller has zero'd the entire dquot 'chunk' already.
	 */
169 170 171 172
	d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
	d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
	d->dd_diskdq.d_id = cpu_to_be32(id);
	d->dd_diskdq.d_flags = type;
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

/*
 * If default limits are in force, push them into the dquot now.
 * We overwrite the dquot limits only if they are zero and this
 * is not the root dquot.
 */
void
xfs_qm_adjust_dqlimits(
	xfs_mount_t		*mp,
	xfs_disk_dquot_t	*d)
{
	xfs_quotainfo_t		*q = mp->m_quotainfo;

	ASSERT(d->d_id);

	if (q->qi_bsoftlimit && !d->d_blk_softlimit)
190
		d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
L
Linus Torvalds 已提交
191
	if (q->qi_bhardlimit && !d->d_blk_hardlimit)
192
		d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
L
Linus Torvalds 已提交
193
	if (q->qi_isoftlimit && !d->d_ino_softlimit)
194
		d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
L
Linus Torvalds 已提交
195
	if (q->qi_ihardlimit && !d->d_ino_hardlimit)
196
		d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
L
Linus Torvalds 已提交
197
	if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
198
		d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
L
Linus Torvalds 已提交
199
	if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
200
		d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211
}

/*
 * Check the limits and timers of a dquot and start or reset timers
 * if necessary.
 * This gets called even when quota enforcement is OFF, which makes our
 * life a little less complicated. (We just don't reject any quota
 * reservations in that case, when enforcement is off).
 * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
 * enforcement's off.
 * In contrast, warnings are a little different in that they don't
212 213 214
 * 'automatically' get started when limits get exceeded.  They do
 * get reset to zero, however, when we find the count to be under
 * the soft limit (they are only ever set non-zero via userspace).
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222
 */
void
xfs_qm_adjust_dqtimers(
	xfs_mount_t		*mp,
	xfs_disk_dquot_t	*d)
{
	ASSERT(d->d_id);

223
#ifdef DEBUG
224 225 226 227 228 229 230 231 232
	if (d->d_blk_hardlimit)
		ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
		       be64_to_cpu(d->d_blk_hardlimit));
	if (d->d_ino_hardlimit)
		ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
		       be64_to_cpu(d->d_ino_hardlimit));
	if (d->d_rtb_hardlimit)
		ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
		       be64_to_cpu(d->d_rtb_hardlimit));
L
Linus Torvalds 已提交
233
#endif
234

L
Linus Torvalds 已提交
235
	if (!d->d_btimer) {
236 237 238 239 240 241 242
		if ((d->d_blk_softlimit &&
		     (be64_to_cpu(d->d_bcount) >=
		      be64_to_cpu(d->d_blk_softlimit))) ||
		    (d->d_blk_hardlimit &&
		     (be64_to_cpu(d->d_bcount) >=
		      be64_to_cpu(d->d_blk_hardlimit)))) {
			d->d_btimer = cpu_to_be32(get_seconds() +
243
					mp->m_quotainfo->qi_btimelimit);
244 245
		} else {
			d->d_bwarns = 0;
L
Linus Torvalds 已提交
246 247 248
		}
	} else {
		if ((!d->d_blk_softlimit ||
249 250
		     (be64_to_cpu(d->d_bcount) <
		      be64_to_cpu(d->d_blk_softlimit))) &&
L
Linus Torvalds 已提交
251
		    (!d->d_blk_hardlimit ||
252 253
		    (be64_to_cpu(d->d_bcount) <
		     be64_to_cpu(d->d_blk_hardlimit)))) {
L
Linus Torvalds 已提交
254 255 256 257 258
			d->d_btimer = 0;
		}
	}

	if (!d->d_itimer) {
259 260 261 262 263 264 265
		if ((d->d_ino_softlimit &&
		     (be64_to_cpu(d->d_icount) >=
		      be64_to_cpu(d->d_ino_softlimit))) ||
		    (d->d_ino_hardlimit &&
		     (be64_to_cpu(d->d_icount) >=
		      be64_to_cpu(d->d_ino_hardlimit)))) {
			d->d_itimer = cpu_to_be32(get_seconds() +
266
					mp->m_quotainfo->qi_itimelimit);
267 268
		} else {
			d->d_iwarns = 0;
L
Linus Torvalds 已提交
269 270 271
		}
	} else {
		if ((!d->d_ino_softlimit ||
272 273
		     (be64_to_cpu(d->d_icount) <
		      be64_to_cpu(d->d_ino_softlimit)))  &&
L
Linus Torvalds 已提交
274
		    (!d->d_ino_hardlimit ||
275 276
		     (be64_to_cpu(d->d_icount) <
		      be64_to_cpu(d->d_ino_hardlimit)))) {
L
Linus Torvalds 已提交
277 278 279 280 281
			d->d_itimer = 0;
		}
	}

	if (!d->d_rtbtimer) {
282 283 284 285 286 287 288
		if ((d->d_rtb_softlimit &&
		     (be64_to_cpu(d->d_rtbcount) >=
		      be64_to_cpu(d->d_rtb_softlimit))) ||
		    (d->d_rtb_hardlimit &&
		     (be64_to_cpu(d->d_rtbcount) >=
		      be64_to_cpu(d->d_rtb_hardlimit)))) {
			d->d_rtbtimer = cpu_to_be32(get_seconds() +
289
					mp->m_quotainfo->qi_rtbtimelimit);
290 291
		} else {
			d->d_rtbwarns = 0;
L
Linus Torvalds 已提交
292 293 294
		}
	} else {
		if ((!d->d_rtb_softlimit ||
295 296
		     (be64_to_cpu(d->d_rtbcount) <
		      be64_to_cpu(d->d_rtb_softlimit))) &&
L
Linus Torvalds 已提交
297
		    (!d->d_rtb_hardlimit ||
298 299
		     (be64_to_cpu(d->d_rtbcount) <
		      be64_to_cpu(d->d_rtb_hardlimit)))) {
L
Linus Torvalds 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
			d->d_rtbtimer = 0;
		}
	}
}

/*
 * initialize a buffer full of dquots and log the whole thing
 */
STATIC void
xfs_qm_init_dquot_blk(
	xfs_trans_t	*tp,
	xfs_mount_t	*mp,
	xfs_dqid_t	id,
	uint		type,
	xfs_buf_t	*bp)
{
316
	struct xfs_quotainfo	*q = mp->m_quotainfo;
L
Linus Torvalds 已提交
317 318 319 320
	xfs_dqblk_t	*d;
	int		curid, i;

	ASSERT(tp);
321
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
322

323
	d = bp->b_addr;
L
Linus Torvalds 已提交
324 325 326 327

	/*
	 * ID of the first dquot in the block - id's are zero based.
	 */
328
	curid = id - (id % q->qi_dqperchunk);
L
Linus Torvalds 已提交
329
	ASSERT(curid >= 0);
330 331
	memset(d, 0, BBTOB(q->qi_dqchunklen));
	for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++)
L
Linus Torvalds 已提交
332 333
		xfs_qm_dqinit_core(curid, type, d);
	xfs_trans_dquot_buf(tp, bp,
334 335 336
			    (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
			    ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
			     XFS_BLF_GDQUOT_BUF)));
337
	xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347
}



/*
 * Allocate a block and fill it with dquots.
 * This is called when the bmapi finds a hole.
 */
STATIC int
xfs_qm_dqalloc(
348
	xfs_trans_t	**tpp,
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359
	xfs_mount_t	*mp,
	xfs_dquot_t	*dqp,
	xfs_inode_t	*quotip,
	xfs_fileoff_t	offset_fsb,
	xfs_buf_t	**O_bpp)
{
	xfs_fsblock_t	firstblock;
	xfs_bmap_free_t flist;
	xfs_bmbt_irec_t map;
	int		nmaps, error, committed;
	xfs_buf_t	*bp;
360
	xfs_trans_t	*tp = *tpp;
L
Linus Torvalds 已提交
361 362

	ASSERT(tp != NULL);
C
Christoph Hellwig 已提交
363 364

	trace_xfs_dqalloc(dqp);
L
Linus Torvalds 已提交
365 366 367 368

	/*
	 * Initialize the bmap freelist prior to calling bmapi code.
	 */
369
	xfs_bmap_init(&flist, &firstblock);
L
Linus Torvalds 已提交
370 371 372 373 374 375 376 377 378 379
	xfs_ilock(quotip, XFS_ILOCK_EXCL);
	/*
	 * Return if this type of quotas is turned off while we didn't
	 * have an inode lock
	 */
	if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
		xfs_iunlock(quotip, XFS_ILOCK_EXCL);
		return (ESRCH);
	}

380
	xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
381
	nmaps = 1;
382 383 384 385 386
	error = xfs_bmapi_write(tp, quotip, offset_fsb,
				XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
				&firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
				&map, &nmaps, &flist);
	if (error)
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400
		goto error0;
	ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
	ASSERT(nmaps == 1);
	ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
	       (map.br_startblock != HOLESTARTBLOCK));

	/*
	 * Keep track of the blkno to save a lookup later
	 */
	dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);

	/* now we can just get the buffer (there's nothing to read yet) */
	bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
			       dqp->q_blkno,
401
			       mp->m_quotainfo->qi_dqchunklen,
L
Linus Torvalds 已提交
402
			       0);
403 404 405

	error = xfs_buf_geterror(bp);
	if (error)
L
Linus Torvalds 已提交
406
		goto error1;
407

L
Linus Torvalds 已提交
408 409 410 411
	/*
	 * Make a chunk of dquots out of this buffer and log
	 * the entire thing.
	 */
412
	xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
413
			      dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
L
Linus Torvalds 已提交
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	/*
	 * xfs_bmap_finish() may commit the current transaction and
	 * start a second transaction if the freelist is not empty.
	 *
	 * Since we still want to modify this buffer, we need to
	 * ensure that the buffer is not released on commit of
	 * the first transaction and ensure the buffer is added to the
	 * second transaction.
	 *
	 * If there is only one transaction then don't stop the buffer
	 * from being released when it commits later on.
	 */

	xfs_trans_bhold(tp, bp);

430
	if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
L
Linus Torvalds 已提交
431 432 433
		goto error1;
	}

434 435 436 437 438 439 440
	if (committed) {
		tp = *tpp;
		xfs_trans_bjoin(tp, bp);
	} else {
		xfs_trans_bhold_release(tp, bp);
	}

L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	*O_bpp = bp;
	return 0;

      error1:
	xfs_bmap_cancel(&flist);
      error0:
	xfs_iunlock(quotip, XFS_ILOCK_EXCL);

	return (error);
}

/*
 * Maps a dquot to the buffer containing its on-disk version.
 * This returns a ptr to the buffer containing the on-disk dquot
 * in the bpp param, and a ptr to the on-disk dquot within that buffer
 */
STATIC int
xfs_qm_dqtobp(
459
	xfs_trans_t		**tpp,
L
Linus Torvalds 已提交
460 461 462 463 464 465
	xfs_dquot_t		*dqp,
	xfs_disk_dquot_t	**O_ddpp,
	xfs_buf_t		**O_bpp,
	uint			flags)
{
	xfs_bmbt_irec_t map;
466
	int		nmaps = 1, error;
L
Linus Torvalds 已提交
467
	xfs_buf_t	*bp;
468 469
	xfs_inode_t	*quotip = XFS_DQ_TO_QIP(dqp);
	xfs_mount_t	*mp = dqp->q_mount;
L
Linus Torvalds 已提交
470
	xfs_disk_dquot_t *ddq;
471
	xfs_dqid_t	id = be32_to_cpu(dqp->q_core.d_id);
472
	xfs_trans_t	*tp = (tpp ? *tpp : NULL);
L
Linus Torvalds 已提交
473

474
	dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
L
Linus Torvalds 已提交
475

476 477
	xfs_ilock(quotip, XFS_ILOCK_SHARED);
	if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
L
Linus Torvalds 已提交
478
		/*
479 480
		 * Return if this type of quotas is turned off while we
		 * didn't have the quota inode lock.
L
Linus Torvalds 已提交
481
		 */
482 483 484 485 486 487 488
		xfs_iunlock(quotip, XFS_ILOCK_SHARED);
		return ESRCH;
	}

	/*
	 * Find the block map; no allocations yet
	 */
D
Dave Chinner 已提交
489 490
	error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
			       XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

	xfs_iunlock(quotip, XFS_ILOCK_SHARED);
	if (error)
		return error;

	ASSERT(nmaps == 1);
	ASSERT(map.br_blockcount == 1);

	/*
	 * Offset of dquot in the (fixed sized) dquot chunk.
	 */
	dqp->q_bufoffset = (id % mp->m_quotainfo->qi_dqperchunk) *
		sizeof(xfs_dqblk_t);

	ASSERT(map.br_startblock != DELAYSTARTBLOCK);
	if (map.br_startblock == HOLESTARTBLOCK) {
L
Linus Torvalds 已提交
507
		/*
508
		 * We don't allocate unless we're asked to
L
Linus Torvalds 已提交
509
		 */
510 511
		if (!(flags & XFS_QMOPT_DQALLOC))
			return ENOENT;
L
Linus Torvalds 已提交
512

513 514 515
		ASSERT(tp);
		error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
					dqp->q_fileoffset, &bp);
L
Linus Torvalds 已提交
516
		if (error)
517 518 519 520
			return error;
		tp = *tpp;
	} else {
		trace_xfs_dqtobp_read(dqp);
L
Linus Torvalds 已提交
521 522

		/*
523 524
		 * store the blkno etc so that we don't have to do the
		 * mapping all the time
L
Linus Torvalds 已提交
525
		 */
526
		dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
C
Christoph Hellwig 已提交
527

528 529 530 531
		error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
					   dqp->q_blkno,
					   mp->m_quotainfo->qi_dqchunklen,
					   0, &bp);
L
Linus Torvalds 已提交
532 533 534
		if (error || !bp)
			return XFS_ERROR(error);
	}
535

536
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
537 538 539 540

	/*
	 * calculate the location of the dquot inside the buffer.
	 */
541
	ddq = bp->b_addr + dqp->q_bufoffset;
L
Linus Torvalds 已提交
542 543 544 545

	/*
	 * A simple sanity check in case we got a corrupted dquot...
	 */
546
	error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
L
Linus Torvalds 已提交
547
			   flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
548 549
			   "dqtobp");
	if (error) {
L
Linus Torvalds 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
		if (!(flags & XFS_QMOPT_DQREPAIR)) {
			xfs_trans_brelse(tp, bp);
			return XFS_ERROR(EIO);
		}
	}

	*O_bpp = bp;
	*O_ddpp = ddq;

	return (0);
}


/*
 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
 * and release the buffer immediately.
 *
 */
/* ARGSUSED */
STATIC int
xfs_qm_dqread(
571
	xfs_trans_t	**tpp,
L
Linus Torvalds 已提交
572 573 574 575 576 577 578
	xfs_dqid_t	id,
	xfs_dquot_t	*dqp,	/* dquot to get filled in */
	uint		flags)
{
	xfs_disk_dquot_t *ddqp;
	xfs_buf_t	 *bp;
	int		 error;
579 580 581
	xfs_trans_t	 *tp;

	ASSERT(tpp);
L
Linus Torvalds 已提交
582

C
Christoph Hellwig 已提交
583 584
	trace_xfs_dqread(dqp);

L
Linus Torvalds 已提交
585 586 587 588
	/*
	 * get a pointer to the on-disk dquot and the buffer containing it
	 * dqp already knows its own type (GROUP/USER).
	 */
589
	if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
L
Linus Torvalds 已提交
590 591
		return (error);
	}
592
	tp = *tpp;
L
Linus Torvalds 已提交
593 594 595

	/* copy everything from disk dquot to the incore dquot */
	memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
596
	ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
L
Linus Torvalds 已提交
597 598 599 600
	xfs_qm_dquot_logitem_init(dqp);

	/*
	 * Reservation counters are defined as reservation plus current usage
L
Lucas De Marchi 已提交
601
	 * to avoid having to add every time.
L
Linus Torvalds 已提交
602
	 */
603 604 605
	dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
	dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
	dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
L
Linus Torvalds 已提交
606 607

	/* Mark the buf so that this will stay incore a little longer */
608
	xfs_buf_set_ref(bp, XFS_DQUOT_REF);
L
Linus Torvalds 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621

	/*
	 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
	 * So we need to release with xfs_trans_brelse().
	 * The strategy here is identical to that of inodes; we lock
	 * the dquot in xfs_qm_dqget() before making it accessible to
	 * others. This is because dquots, like inodes, need a good level of
	 * concurrency, and we don't want to take locks on the entire buffers
	 * for dquot accesses.
	 * Note also that the dquot buffer may even be dirty at this point, if
	 * this particular dquot was repaired. We still aren't afraid to
	 * brelse it because we have the changes incore.
	 */
622
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	xfs_trans_brelse(tp, bp);

	return (error);
}


/*
 * allocate an incore dquot from the kernel heap,
 * and fill its core with quota information kept on disk.
 * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
 * if it wasn't already allocated.
 */
STATIC int
xfs_qm_idtodq(
	xfs_mount_t	*mp,
	xfs_dqid_t	id,	 /* gid or uid, depending on type */
	uint		type,	 /* UDQUOT or GDQUOT */
	uint		flags,	 /* DQALLOC, DQREPAIR */
	xfs_dquot_t	**O_dqpp)/* OUT : incore dquot, not locked */
{
	xfs_dquot_t	*dqp;
	int		error;
	xfs_trans_t	*tp;
	int		cancelflags=0;

	dqp = xfs_qm_dqinit(mp, id, type);
	tp = NULL;
	if (flags & XFS_QMOPT_DQALLOC) {
		tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
652 653 654 655 656 657 658 659
		error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
				XFS_WRITE_LOG_RES(mp) +
				BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 +
				128,
				0,
				XFS_TRANS_PERM_LOG_RES,
				XFS_WRITE_LOG_COUNT);
		if (error) {
L
Linus Torvalds 已提交
660 661 662 663 664 665 666 667 668 669
			cancelflags = 0;
			goto error0;
		}
		cancelflags = XFS_TRANS_RELEASE_LOG_RES;
	}

	/*
	 * Read it from disk; xfs_dqread() takes care of
	 * all the necessary initialization of dquot's fields (locks, etc)
	 */
670
	if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
L
Linus Torvalds 已提交
671 672 673 674 675
		/*
		 * This can happen if quotas got turned off (ESRCH),
		 * or if the dquot didn't exist on disk and we ask to
		 * allocate (ENOENT).
		 */
C
Christoph Hellwig 已提交
676
		trace_xfs_dqread_fail(dqp);
L
Linus Torvalds 已提交
677 678 679 680
		cancelflags |= XFS_TRANS_ABORT;
		goto error0;
	}
	if (tp) {
681
		if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
			goto error1;
	}

	*O_dqpp = dqp;
	return (0);

 error0:
	ASSERT(error);
	if (tp)
		xfs_trans_cancel(tp, cancelflags);
 error1:
	xfs_qm_dqdestroy(dqp);
	*O_dqpp = NULL;
	return (error);
}

/*
 * Lookup a dquot in the incore dquot hashtable. We keep two separate
 * hashtables for user and group dquots; and, these are global tables
 * inside the XQM, not per-filesystem tables.
 * The hash chain must be locked by caller, and it is left locked
 * on return. Returning dquot is locked.
 */
STATIC int
xfs_qm_dqlookup(
	xfs_mount_t		*mp,
	xfs_dqid_t		id,
	xfs_dqhash_t		*qh,
	xfs_dquot_t		**O_dqpp)
{
	xfs_dquot_t		*dqp;

714
	ASSERT(mutex_is_locked(&qh->qh_lock));
L
Linus Torvalds 已提交
715 716 717 718

	/*
	 * Traverse the hashchain looking for a match
	 */
719
	list_for_each_entry(dqp, &qh->qh_list, q_hashlist) {
L
Linus Torvalds 已提交
720 721 722 723 724
		/*
		 * We already have the hashlock. We don't need the
		 * dqlock to look at the id field of the dquot, since the
		 * id can't be modified without the hashlock anyway.
		 */
725
		if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
C
Christoph Hellwig 已提交
726 727
			trace_xfs_dqlookup_found(dqp);

L
Linus Torvalds 已提交
728 729 730
			/*
			 * All in core dquots must be on the dqlist of mp
			 */
731
			ASSERT(!list_empty(&dqp->q_mplist));
L
Linus Torvalds 已提交
732 733 734

			xfs_dqlock(dqp);
			if (dqp->q_nrefs == 0) {
735 736
				ASSERT(!list_empty(&dqp->q_freelist));
				if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
C
Christoph Hellwig 已提交
737
					trace_xfs_dqlookup_want(dqp);
L
Linus Torvalds 已提交
738 739 740 741 742 743 744 745

					/*
					 * We may have raced with dqreclaim_one()
					 * (and lost). So, flag that we don't
					 * want the dquot to be reclaimed.
					 */
					dqp->dq_flags |= XFS_DQ_WANT;
					xfs_dqunlock(dqp);
746
					mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
L
Linus Torvalds 已提交
747 748 749 750
					xfs_dqlock(dqp);
					dqp->dq_flags &= ~(XFS_DQ_WANT);
				}

C
Christoph Hellwig 已提交
751
				if (dqp->q_nrefs == 0) {
752
					/* take it off the freelist */
C
Christoph Hellwig 已提交
753
					trace_xfs_dqlookup_freelist(dqp);
754 755
					list_del_init(&dqp->q_freelist);
					xfs_Gqm->qm_dqfrlist_cnt--;
L
Linus Torvalds 已提交
756
				}
C
Christoph Hellwig 已提交
757 758 759 760
				XFS_DQHOLD(dqp);
				mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
			} else {
				XFS_DQHOLD(dqp);
L
Linus Torvalds 已提交
761 762 763 764 765
			}

			/*
			 * move the dquot to the front of the hashchain
			 */
766
			ASSERT(mutex_is_locked(&qh->qh_lock));
767
			list_move(&dqp->q_hashlist, &qh->qh_list);
C
Christoph Hellwig 已提交
768
			trace_xfs_dqlookup_done(dqp);
L
Linus Torvalds 已提交
769
			*O_dqpp = dqp;
770
			return 0;
L
Linus Torvalds 已提交
771 772 773 774
		}
	}

	*O_dqpp = NULL;
775
	ASSERT(mutex_is_locked(&qh->qh_lock));
L
Linus Torvalds 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	return (1);
}

/*
 * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
 * a locked dquot, doing an allocation (if requested) as needed.
 * When both an inode and an id are given, the inode's id takes precedence.
 * That is, if the id changes while we don't hold the ilock inside this
 * function, the new dquot is returned, not necessarily the one requested
 * in the id argument.
 */
int
xfs_qm_dqget(
	xfs_mount_t	*mp,
	xfs_inode_t	*ip,	  /* locked inode (optional) */
791 792
	xfs_dqid_t	id,	  /* uid/projid/gid depending on type */
	uint		type,	  /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
L
Linus Torvalds 已提交
793 794 795 796 797 798 799 800 801 802
	uint		flags,	  /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
	xfs_dquot_t	**O_dqpp) /* OUT : locked incore dquot */
{
	xfs_dquot_t	*dqp;
	xfs_dqhash_t	*h;
	uint		version;
	int		error;

	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
	if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
803
	    (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
L
Linus Torvalds 已提交
804 805 806 807 808 809 810 811 812
	    (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
		return (ESRCH);
	}
	h = XFS_DQ_HASH(mp, id, type);

#ifdef DEBUG
	if (xfs_do_dqerror) {
		if ((xfs_dqerror_target == mp->m_ddev_targp) &&
		    (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
813
			xfs_debug(mp, "Returning error in dqget");
L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821
			return (EIO);
		}
	}
#endif

 again:

#ifdef DEBUG
822 823 824
	ASSERT(type == XFS_DQ_USER ||
	       type == XFS_DQ_PROJ ||
	       type == XFS_DQ_GROUP);
L
Linus Torvalds 已提交
825
	if (ip) {
C
Christoph Hellwig 已提交
826
		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
L
Linus Torvalds 已提交
827 828 829 830 831 832
		if (type == XFS_DQ_USER)
			ASSERT(ip->i_udquot == NULL);
		else
			ASSERT(ip->i_gdquot == NULL);
	}
#endif
833
	mutex_lock(&h->qh_lock);
L
Linus Torvalds 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847

	/*
	 * Look in the cache (hashtable).
	 * The chain is kept locked during lookup.
	 */
	if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
		XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
		/*
		 * The dquot was found, moved to the front of the chain,
		 * taken off the freelist if it was on it, and locked
		 * at this point. Just unlock the hashchain and return.
		 */
		ASSERT(*O_dqpp);
		ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
848
		mutex_unlock(&h->qh_lock);
C
Christoph Hellwig 已提交
849
		trace_xfs_dqget_hit(*O_dqpp);
L
Linus Torvalds 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
		return (0);	/* success */
	}
	XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);

	/*
	 * Dquot cache miss. We don't want to keep the inode lock across
	 * a (potential) disk read. Also we don't want to deal with the lock
	 * ordering between quotainode and this inode. OTOH, dropping the inode
	 * lock here means dealing with a chown that can happen before
	 * we re-acquire the lock.
	 */
	if (ip)
		xfs_iunlock(ip, XFS_ILOCK_EXCL);
	/*
	 * Save the hashchain version stamp, and unlock the chain, so that
	 * we don't keep the lock across a disk read
	 */
	version = h->qh_version;
868
	mutex_unlock(&h->qh_lock);
L
Linus Torvalds 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

	/*
	 * Allocate the dquot on the kernel heap, and read the ondisk
	 * portion off the disk. Also, do all the necessary initialization
	 * This can return ENOENT if dquot didn't exist on disk and we didn't
	 * ask it to allocate; ESRCH if quotas got turned off suddenly.
	 */
	if ((error = xfs_qm_idtodq(mp, id, type,
				  flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
					   XFS_QMOPT_DOWARN),
				  &dqp))) {
		if (ip)
			xfs_ilock(ip, XFS_ILOCK_EXCL);
		return (error);
	}

	/*
	 * See if this is mount code calling to look at the overall quota limits
	 * which are stored in the id == 0 user or group's dquot.
	 * Since we may not have done a quotacheck by this point, just return
	 * the dquot without attaching it to any hashtables, lists, etc, or even
	 * taking a reference.
	 * The caller must dqdestroy this once done.
	 */
	if (flags & XFS_QMOPT_DQSUSER) {
		ASSERT(id == 0);
		ASSERT(! ip);
		goto dqret;
	}

	/*
	 * Dquot lock comes after hashlock in the lock ordering
	 */
	if (ip) {
		xfs_ilock(ip, XFS_ILOCK_EXCL);
904

L
Linus Torvalds 已提交
905 906 907 908 909
		/*
		 * A dquot could be attached to this inode by now, since
		 * we had dropped the ilock.
		 */
		if (type == XFS_DQ_USER) {
910 911 912 913 914
			if (!XFS_IS_UQUOTA_ON(mp)) {
				/* inode stays locked on return */
				xfs_qm_dqdestroy(dqp);
				return XFS_ERROR(ESRCH);
			}
L
Linus Torvalds 已提交
915 916 917 918 919 920 921
			if (ip->i_udquot) {
				xfs_qm_dqdestroy(dqp);
				dqp = ip->i_udquot;
				xfs_dqlock(dqp);
				goto dqret;
			}
		} else {
922 923 924 925 926
			if (!XFS_IS_OQUOTA_ON(mp)) {
				/* inode stays locked on return */
				xfs_qm_dqdestroy(dqp);
				return XFS_ERROR(ESRCH);
			}
L
Linus Torvalds 已提交
927 928 929 930 931 932 933 934 935 936 937 938
			if (ip->i_gdquot) {
				xfs_qm_dqdestroy(dqp);
				dqp = ip->i_gdquot;
				xfs_dqlock(dqp);
				goto dqret;
			}
		}
	}

	/*
	 * Hashlock comes after ilock in lock order
	 */
939
	mutex_lock(&h->qh_lock);
L
Linus Torvalds 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	if (version != h->qh_version) {
		xfs_dquot_t *tmpdqp;
		/*
		 * Now, see if somebody else put the dquot in the
		 * hashtable before us. This can happen because we didn't
		 * keep the hashchain lock. We don't have to worry about
		 * lock order between the two dquots here since dqp isn't
		 * on any findable lists yet.
		 */
		if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
			/*
			 * Duplicate found. Just throw away the new dquot
			 * and start over.
			 */
			xfs_qm_dqput(tmpdqp);
955
			mutex_unlock(&h->qh_lock);
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963 964 965
			xfs_qm_dqdestroy(dqp);
			XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
			goto again;
		}
	}

	/*
	 * Put the dquot at the beginning of the hash-chain and mp's list
	 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
	 */
966
	ASSERT(mutex_is_locked(&h->qh_lock));
L
Linus Torvalds 已提交
967
	dqp->q_hash = h;
968 969
	list_add(&dqp->q_hashlist, &h->qh_list);
	h->qh_version++;
L
Linus Torvalds 已提交
970 971 972 973 974

	/*
	 * Attach this dquot to this filesystem's list of all dquots,
	 * kept inside the mount structure in m_quotainfo field
	 */
975
	mutex_lock(&mp->m_quotainfo->qi_dqlist_lock);
L
Linus Torvalds 已提交
976 977 978 979 980 981 982

	/*
	 * We return a locked dquot to the caller, with a reference taken
	 */
	xfs_dqlock(dqp);
	dqp->q_nrefs = 1;

983 984 985
	list_add(&dqp->q_mplist, &mp->m_quotainfo->qi_dqlist);
	mp->m_quotainfo->qi_dquots++;
	mutex_unlock(&mp->m_quotainfo->qi_dqlist_lock);
986
	mutex_unlock(&h->qh_lock);
L
Linus Torvalds 已提交
987
 dqret:
C
Christoph Hellwig 已提交
988
	ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
C
Christoph Hellwig 已提交
989
	trace_xfs_dqget_miss(dqp);
L
Linus Torvalds 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	*O_dqpp = dqp;
	return (0);
}


/*
 * Release a reference to the dquot (decrement ref-count)
 * and unlock it. If there is a group quota attached to this
 * dquot, carefully release that too without tripping over
 * deadlocks'n'stuff.
 */
void
xfs_qm_dqput(
	xfs_dquot_t	*dqp)
{
	xfs_dquot_t	*gdqp;

	ASSERT(dqp->q_nrefs > 0);
	ASSERT(XFS_DQ_IS_LOCKED(dqp));
C
Christoph Hellwig 已提交
1009 1010

	trace_xfs_dqput(dqp);
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	if (dqp->q_nrefs != 1) {
		dqp->q_nrefs--;
		xfs_dqunlock(dqp);
		return;
	}

	/*
	 * drop the dqlock and acquire the freelist and dqlock
	 * in the right order; but try to get it out-of-order first
	 */
1022
	if (!mutex_trylock(&xfs_Gqm->qm_dqfrlist_lock)) {
C
Christoph Hellwig 已提交
1023
		trace_xfs_dqput_wait(dqp);
L
Linus Torvalds 已提交
1024
		xfs_dqunlock(dqp);
1025
		mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031 1032 1033
		xfs_dqlock(dqp);
	}

	while (1) {
		gdqp = NULL;

		/* We can't depend on nrefs being == 1 here */
		if (--dqp->q_nrefs == 0) {
C
Christoph Hellwig 已提交
1034 1035
			trace_xfs_dqput_free(dqp);

1036 1037
			list_add_tail(&dqp->q_freelist, &xfs_Gqm->qm_dqfrlist);
			xfs_Gqm->qm_dqfrlist_cnt++;
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

			/*
			 * If we just added a udquot to the freelist, then
			 * we want to release the gdquot reference that
			 * it (probably) has. Otherwise it'll keep the
			 * gdquot from getting reclaimed.
			 */
			if ((gdqp = dqp->q_gdquot)) {
				/*
				 * Avoid a recursive dqput call
				 */
				xfs_dqlock(gdqp);
				dqp->q_gdquot = NULL;
			}
		}
		xfs_dqunlock(dqp);

		/*
		 * If we had a group quota inside the user quota as a hint,
		 * release it now.
		 */
		if (! gdqp)
			break;
		dqp = gdqp;
	}
1063
	mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
}

/*
 * Release a dquot. Flush it if dirty, then dqput() it.
 * dquot must not be locked.
 */
void
xfs_qm_dqrele(
	xfs_dquot_t	*dqp)
{
C
Christoph Hellwig 已提交
1074 1075 1076
	if (!dqp)
		return;

C
Christoph Hellwig 已提交
1077
	trace_xfs_dqrele(dqp);
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

	xfs_dqlock(dqp);
	/*
	 * We don't care to flush it if the dquot is dirty here.
	 * That will create stutters that we want to avoid.
	 * Instead we do a delayed write when we try to reclaim
	 * a dirty dquot. Also xfs_sync will take part of the burden...
	 */
	xfs_qm_dqput(dqp);
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
/*
 * This is the dquot flushing I/O completion routine.  It is called
 * from interrupt level when the buffer containing the dquot is
 * flushed to disk.  It is responsible for removing the dquot logitem
 * from the AIL if it has not been re-logged, and unlocking the dquot's
 * flush lock. This behavior is very similar to that of inodes..
 */
STATIC void
xfs_qm_dqflush_done(
	struct xfs_buf		*bp,
	struct xfs_log_item	*lip)
{
	xfs_dq_logitem_t	*qip = (struct xfs_dq_logitem *)lip;
	xfs_dquot_t		*dqp = qip->qli_dquot;
	struct xfs_ail		*ailp = lip->li_ailp;

	/*
	 * We only want to pull the item from the AIL if its
	 * location in the log has not changed since we started the flush.
	 * Thus, we only bother if the dquot's lsn has
	 * not changed. First we check the lsn outside the lock
	 * since it's cheaper, and then we recheck while
	 * holding the lock before removing the dquot from the AIL.
	 */
	if ((lip->li_flags & XFS_LI_IN_AIL) &&
	    lip->li_lsn == qip->qli_flush_lsn) {

		/* xfs_trans_ail_delete() drops the AIL lock. */
		spin_lock(&ailp->xa_lock);
		if (lip->li_lsn == qip->qli_flush_lsn)
			xfs_trans_ail_delete(ailp, lip);
		else
			spin_unlock(&ailp->xa_lock);
	}

	/*
	 * Release the dq's flush lock since we're done with it.
	 */
	xfs_dqfunlock(dqp);
}
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

/*
 * Write a modified dquot to disk.
 * The dquot must be locked and the flush lock too taken by caller.
 * The flush lock will not be unlocked until the dquot reaches the disk,
 * but the dquot is free to be unlocked and modified by the caller
 * in the interim. Dquot is still locked on return. This behavior is
 * identical to that of inodes.
 */
int
xfs_qm_dqflush(
	xfs_dquot_t		*dqp,
	uint			flags)
{
1143 1144 1145
	struct xfs_mount	*mp = dqp->q_mount;
	struct xfs_buf		*bp;
	struct xfs_disk_dquot	*ddqp;
L
Linus Torvalds 已提交
1146 1147 1148
	int			error;

	ASSERT(XFS_DQ_IS_LOCKED(dqp));
1149
	ASSERT(!completion_done(&dqp->q_flush));
1150

C
Christoph Hellwig 已提交
1151
	trace_xfs_dqflush(dqp);
L
Linus Torvalds 已提交
1152 1153

	/*
1154
	 * If not dirty, or it's pinned and we are not supposed to block, nada.
L
Linus Torvalds 已提交
1155
	 */
1156
	if (!XFS_DQ_IS_DIRTY(dqp) ||
1157
	    ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
L
Linus Torvalds 已提交
1158
		xfs_dqfunlock(dqp);
1159
		return 0;
L
Linus Torvalds 已提交
1160 1161 1162 1163 1164 1165 1166 1167
	}
	xfs_qm_dqunpin_wait(dqp);

	/*
	 * This may have been unpinned because the filesystem is shutting
	 * down forcibly. If that's the case we must not write this dquot
	 * to disk, because the log record didn't make it to disk!
	 */
1168 1169
	if (XFS_FORCED_SHUTDOWN(mp)) {
		dqp->dq_flags &= ~XFS_DQ_DIRTY;
L
Linus Torvalds 已提交
1170 1171 1172 1173 1174 1175 1176
		xfs_dqfunlock(dqp);
		return XFS_ERROR(EIO);
	}

	/*
	 * Get the buffer containing the on-disk dquot
	 */
1177 1178 1179
	error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
				   mp->m_quotainfo->qi_dqchunklen, 0, &bp);
	if (error) {
L
Linus Torvalds 已提交
1180 1181
		ASSERT(error != ENOENT);
		xfs_dqfunlock(dqp);
1182
		return error;
L
Linus Torvalds 已提交
1183 1184
	}

1185 1186 1187
	/*
	 * Calculate the location of the dquot inside the buffer.
	 */
1188
	ddqp = bp->b_addr + dqp->q_bufoffset;
1189 1190 1191 1192

	/*
	 * A simple sanity check in case we got a corrupted dquot..
	 */
1193 1194 1195
	error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
			   XFS_QMOPT_DOWARN, "dqflush (incore copy)");
	if (error) {
1196 1197 1198
		xfs_buf_relse(bp);
		xfs_dqfunlock(dqp);
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
L
Linus Torvalds 已提交
1199 1200 1201 1202
		return XFS_ERROR(EIO);
	}

	/* This is the only portion of data that needs to persist */
1203
	memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
L
Linus Torvalds 已提交
1204 1205 1206 1207

	/*
	 * Clear the dirty field and remember the flush lsn for later use.
	 */
1208
	dqp->dq_flags &= ~XFS_DQ_DIRTY;
L
Linus Torvalds 已提交
1209

1210 1211
	xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
					&dqp->q_logitem.qli_item.li_lsn);
L
Linus Torvalds 已提交
1212 1213 1214 1215 1216

	/*
	 * Attach an iodone routine so that we can remove this dquot from the
	 * AIL and release the flush lock once the dquot is synced to disk.
	 */
1217 1218 1219
	xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
				  &dqp->q_logitem.qli_item);

L
Linus Torvalds 已提交
1220 1221 1222 1223
	/*
	 * If the buffer is pinned then push on the log so we won't
	 * get stuck waiting in the write for too long.
	 */
1224
	if (xfs_buf_ispinned(bp)) {
C
Christoph Hellwig 已提交
1225
		trace_xfs_dqflush_force(dqp);
1226
		xfs_log_force(mp, 0);
L
Linus Torvalds 已提交
1227 1228
	}

1229
	if (flags & SYNC_WAIT)
1230 1231
		error = xfs_bwrite(bp);
	else
1232
		xfs_buf_delwri_queue(bp);
1233 1234

	xfs_buf_relse(bp);
C
Christoph Hellwig 已提交
1235 1236 1237

	trace_xfs_dqflush_done(dqp);

L
Linus Torvalds 已提交
1238 1239 1240
	/*
	 * dqp is still locked, but caller is free to unlock it now.
	 */
1241
	return error;
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246 1247 1248

}

void
xfs_dqunlock(
	xfs_dquot_t *dqp)
{
1249
	xfs_dqunlock_nonotify(dqp);
L
Linus Torvalds 已提交
1250
	if (dqp->q_logitem.qli_dquot == dqp) {
1251
		xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
1252
					&dqp->q_logitem.qli_item);
L
Linus Torvalds 已提交
1253 1254 1255
	}
}

1256 1257 1258 1259 1260 1261
/*
 * Lock two xfs_dquot structures.
 *
 * To avoid deadlocks we always lock the quota structure with
 * the lowerd id first.
 */
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266 1267 1268
void
xfs_dqlock2(
	xfs_dquot_t	*d1,
	xfs_dquot_t	*d2)
{
	if (d1 && d2) {
		ASSERT(d1 != d2);
1269 1270
		if (be32_to_cpu(d1->q_core.d_id) >
		    be32_to_cpu(d2->q_core.d_id)) {
1271 1272
			mutex_lock(&d2->q_qlock);
			mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1273
		} else {
1274 1275
			mutex_lock(&d1->q_qlock);
			mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1276
		}
1277 1278 1279 1280
	} else if (d1) {
		mutex_lock(&d1->q_qlock);
	} else if (d2) {
		mutex_lock(&d2->q_qlock);
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	}
}


/*
 * Take a dquot out of the mount's dqlist as well as the hashlist.
 * This is called via unmount as well as quotaoff, and the purge
 * will always succeed unless there are soft (temp) references
 * outstanding.
 *
 * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
 * that we're returning! XXXsup - not cool.
 */
/* ARGSUSED */
int
xfs_qm_dqpurge(
1297
	xfs_dquot_t	*dqp)
L
Linus Torvalds 已提交
1298
{
1299
	xfs_dqhash_t	*qh = dqp->q_hash;
1300
	xfs_mount_t	*mp = dqp->q_mount;
L
Linus Torvalds 已提交
1301

1302
	ASSERT(mutex_is_locked(&mp->m_quotainfo->qi_dqlist_lock));
1303
	ASSERT(mutex_is_locked(&dqp->q_hash->qh_lock));
L
Linus Torvalds 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

	xfs_dqlock(dqp);
	/*
	 * We really can't afford to purge a dquot that is
	 * referenced, because these are hard refs.
	 * It shouldn't happen in general because we went thru _all_ inodes in
	 * dqrele_all_inodes before calling this and didn't let the mountlock go.
	 * However it is possible that we have dquots with temporary
	 * references that are not attached to an inode. e.g. see xfs_setattr().
	 */
	if (dqp->q_nrefs != 0) {
		xfs_dqunlock(dqp);
1316
		mutex_unlock(&dqp->q_hash->qh_lock);
L
Linus Torvalds 已提交
1317 1318 1319
		return (1);
	}

1320
	ASSERT(!list_empty(&dqp->q_freelist));
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329

	/*
	 * If we're turning off quotas, we have to make sure that, for
	 * example, we don't delete quota disk blocks while dquots are
	 * in the process of getting written to those disk blocks.
	 * This dquot might well be on AIL, and we can't leave it there
	 * if we're turning off quotas. Basically, we need this flush
	 * lock, and are willing to block on it.
	 */
1330
	if (!xfs_dqflock_nowait(dqp)) {
L
Linus Torvalds 已提交
1331 1332 1333 1334
		/*
		 * Block on the flush lock after nudging dquot buffer,
		 * if it is incore.
		 */
1335
		xfs_dqflock_pushbuf_wait(dqp);
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342 1343
	}

	/*
	 * XXXIf we're turning this type of quotas off, we don't care
	 * about the dirty metadata sitting in this dquot. OTOH, if
	 * we're unmounting, we do care, so we flush it and wait.
	 */
	if (XFS_DQ_IS_DIRTY(dqp)) {
1344
		int	error;
C
Christoph Hellwig 已提交
1345

L
Linus Torvalds 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354
		/* dqflush unlocks dqflock */
		/*
		 * Given that dqpurge is a very rare occurrence, it is OK
		 * that we're holding the hashlist and mplist locks
		 * across the disk write. But, ... XXXsup
		 *
		 * We don't care about getting disk errors here. We need
		 * to purge this dquot anyway, so we go ahead regardless.
		 */
1355
		error = xfs_qm_dqflush(dqp, SYNC_WAIT);
1356
		if (error)
1357 1358
			xfs_warn(mp, "%s: dquot %p flush failed",
				__func__, dqp);
L
Linus Torvalds 已提交
1359 1360
		xfs_dqflock(dqp);
	}
P
Peter Leckie 已提交
1361
	ASSERT(atomic_read(&dqp->q_pincount) == 0);
L
Linus Torvalds 已提交
1362 1363 1364
	ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
	       !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));

1365 1366
	list_del_init(&dqp->q_hashlist);
	qh->qh_version++;
1367 1368 1369
	list_del_init(&dqp->q_mplist);
	mp->m_quotainfo->qi_dqreclaims++;
	mp->m_quotainfo->qi_dquots--;
L
Linus Torvalds 已提交
1370 1371 1372 1373
	/*
	 * XXX Move this to the front of the freelist, if we can get the
	 * freelist lock.
	 */
1374
	ASSERT(!list_empty(&dqp->q_freelist));
L
Linus Torvalds 已提交
1375 1376 1377 1378 1379 1380 1381

	dqp->q_mount = NULL;
	dqp->q_hash = NULL;
	dqp->dq_flags = XFS_DQ_INACTIVE;
	memset(&dqp->q_core, 0, sizeof(dqp->q_core));
	xfs_dqfunlock(dqp);
	xfs_dqunlock(dqp);
1382
	mutex_unlock(&qh->qh_lock);
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391
	return (0);
}


/*
 * Give the buffer a little push if it is incore and
 * wait on the flush lock.
 */
void
1392
xfs_dqflock_pushbuf_wait(
L
Linus Torvalds 已提交
1393 1394
	xfs_dquot_t	*dqp)
{
1395
	xfs_mount_t	*mp = dqp->q_mount;
L
Linus Torvalds 已提交
1396 1397 1398 1399 1400 1401 1402 1403
	xfs_buf_t	*bp;

	/*
	 * Check to see if the dquot has been flushed delayed
	 * write.  If so, grab its buffer and send it
	 * out immediately.  We'll be able to acquire
	 * the flush lock when the I/O completes.
	 */
1404 1405
	bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
			mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
1406 1407 1408 1409
	if (!bp)
		goto out_lock;

	if (XFS_BUF_ISDELAYWRITE(bp)) {
1410
		if (xfs_buf_ispinned(bp))
1411
			xfs_log_force(mp, 0);
1412 1413
		xfs_buf_delwri_promote(bp);
		wake_up_process(bp->b_target->bt_task);
L
Linus Torvalds 已提交
1414
	}
1415 1416
	xfs_buf_relse(bp);
out_lock:
L
Linus Torvalds 已提交
1417 1418
	xfs_dqflock(dqp);
}