xfs_dquot.c 31.1 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "xfs_log.h"
#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"
39
#include "xfs_cksum.h"
C
Christoph Hellwig 已提交
40
#include "xfs_trace.h"
L
Linus Torvalds 已提交
41 42

/*
43 44 45
 * Lock order:
 *
 * ip->i_lock
46
 *   qi->qi_tree_lock
47 48 49
 *     dquot->q_qlock (xfs_dqlock() and friends)
 *       dquot->q_flush (xfs_dqflock() and friends)
 *       qi->qi_lru_lock
50 51 52 53
 *
 * If two dquots need to be locked the order is user before group/project,
 * otherwise by the lowest id first, see xfs_dqlock2.
 */
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61

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

62 63 64
struct kmem_zone		*xfs_qm_dqtrxzone;
static struct kmem_zone		*xfs_qm_dqzone;

65 66
static struct lock_class_key xfs_dquot_other_class;

L
Linus Torvalds 已提交
67 68 69 70 71 72 73
/*
 * This is called to free all the memory associated with a dquot
 */
void
xfs_qm_dqdestroy(
	xfs_dquot_t	*dqp)
{
74
	ASSERT(list_empty(&dqp->q_lru));
L
Linus Torvalds 已提交
75 76

	mutex_destroy(&dqp->q_qlock);
77
	kmem_zone_free(xfs_qm_dqzone, dqp);
C
Christoph Hellwig 已提交
78

79
	XFS_STATS_DEC(xs_qm_dquot);
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88
}

/*
 * 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(
89 90
	struct xfs_mount	*mp,
	struct xfs_dquot	*dq)
L
Linus Torvalds 已提交
91
{
92 93
	struct xfs_quotainfo	*q = mp->m_quotainfo;
	struct xfs_disk_dquot	*d = &dq->q_core;
94
	int			prealloc = 0;
L
Linus Torvalds 已提交
95 96 97

	ASSERT(d->d_id);

98
	if (q->qi_bsoftlimit && !d->d_blk_softlimit) {
99
		d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
100 101 102
		prealloc = 1;
	}
	if (q->qi_bhardlimit && !d->d_blk_hardlimit) {
103
		d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
104 105
		prealloc = 1;
	}
L
Linus Torvalds 已提交
106
	if (q->qi_isoftlimit && !d->d_ino_softlimit)
107
		d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
L
Linus Torvalds 已提交
108
	if (q->qi_ihardlimit && !d->d_ino_hardlimit)
109
		d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
L
Linus Torvalds 已提交
110
	if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
111
		d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
L
Linus Torvalds 已提交
112
	if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
113
		d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
114 115 116

	if (prealloc)
		xfs_dquot_set_prealloc_limits(dq);
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127
}

/*
 * 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
128 129 130
 * '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 已提交
131 132 133 134 135 136 137 138
 */
void
xfs_qm_adjust_dqtimers(
	xfs_mount_t		*mp,
	xfs_disk_dquot_t	*d)
{
	ASSERT(d->d_id);

139
#ifdef DEBUG
140 141 142 143 144 145 146 147 148
	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 已提交
149
#endif
150

L
Linus Torvalds 已提交
151
	if (!d->d_btimer) {
152
		if ((d->d_blk_softlimit &&
153
		     (be64_to_cpu(d->d_bcount) >
154 155
		      be64_to_cpu(d->d_blk_softlimit))) ||
		    (d->d_blk_hardlimit &&
156
		     (be64_to_cpu(d->d_bcount) >
157 158
		      be64_to_cpu(d->d_blk_hardlimit)))) {
			d->d_btimer = cpu_to_be32(get_seconds() +
159
					mp->m_quotainfo->qi_btimelimit);
160 161
		} else {
			d->d_bwarns = 0;
L
Linus Torvalds 已提交
162 163 164
		}
	} else {
		if ((!d->d_blk_softlimit ||
165
		     (be64_to_cpu(d->d_bcount) <=
166
		      be64_to_cpu(d->d_blk_softlimit))) &&
L
Linus Torvalds 已提交
167
		    (!d->d_blk_hardlimit ||
168
		    (be64_to_cpu(d->d_bcount) <=
169
		     be64_to_cpu(d->d_blk_hardlimit)))) {
L
Linus Torvalds 已提交
170 171 172 173 174
			d->d_btimer = 0;
		}
	}

	if (!d->d_itimer) {
175
		if ((d->d_ino_softlimit &&
176
		     (be64_to_cpu(d->d_icount) >
177 178
		      be64_to_cpu(d->d_ino_softlimit))) ||
		    (d->d_ino_hardlimit &&
179
		     (be64_to_cpu(d->d_icount) >
180 181
		      be64_to_cpu(d->d_ino_hardlimit)))) {
			d->d_itimer = cpu_to_be32(get_seconds() +
182
					mp->m_quotainfo->qi_itimelimit);
183 184
		} else {
			d->d_iwarns = 0;
L
Linus Torvalds 已提交
185 186 187
		}
	} else {
		if ((!d->d_ino_softlimit ||
188
		     (be64_to_cpu(d->d_icount) <=
189
		      be64_to_cpu(d->d_ino_softlimit)))  &&
L
Linus Torvalds 已提交
190
		    (!d->d_ino_hardlimit ||
191
		     (be64_to_cpu(d->d_icount) <=
192
		      be64_to_cpu(d->d_ino_hardlimit)))) {
L
Linus Torvalds 已提交
193 194 195 196 197
			d->d_itimer = 0;
		}
	}

	if (!d->d_rtbtimer) {
198
		if ((d->d_rtb_softlimit &&
199
		     (be64_to_cpu(d->d_rtbcount) >
200 201
		      be64_to_cpu(d->d_rtb_softlimit))) ||
		    (d->d_rtb_hardlimit &&
202
		     (be64_to_cpu(d->d_rtbcount) >
203 204
		      be64_to_cpu(d->d_rtb_hardlimit)))) {
			d->d_rtbtimer = cpu_to_be32(get_seconds() +
205
					mp->m_quotainfo->qi_rtbtimelimit);
206 207
		} else {
			d->d_rtbwarns = 0;
L
Linus Torvalds 已提交
208 209 210
		}
	} else {
		if ((!d->d_rtb_softlimit ||
211
		     (be64_to_cpu(d->d_rtbcount) <=
212
		      be64_to_cpu(d->d_rtb_softlimit))) &&
L
Linus Torvalds 已提交
213
		    (!d->d_rtb_hardlimit ||
214
		     (be64_to_cpu(d->d_rtbcount) <=
215
		      be64_to_cpu(d->d_rtb_hardlimit)))) {
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
			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)
{
232
	struct xfs_quotainfo	*q = mp->m_quotainfo;
L
Linus Torvalds 已提交
233 234 235 236
	xfs_dqblk_t	*d;
	int		curid, i;

	ASSERT(tp);
237
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
238

239
	d = bp->b_addr;
L
Linus Torvalds 已提交
240 241 242 243

	/*
	 * ID of the first dquot in the block - id's are zero based.
	 */
244
	curid = id - (id % q->qi_dqperchunk);
L
Linus Torvalds 已提交
245
	ASSERT(curid >= 0);
246
	memset(d, 0, BBTOB(q->qi_dqchunklen));
247 248 249 250 251
	for (i = 0; i < q->qi_dqperchunk; i++, d++, curid++) {
		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(curid);
		d->dd_diskdq.d_flags = type;
D
Dave Chinner 已提交
252
		if (xfs_sb_version_hascrc(&mp->m_sb)) {
253
			uuid_copy(&d->dd_uuid, &mp->m_sb.sb_uuid);
D
Dave Chinner 已提交
254 255 256
			xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
					 XFS_DQUOT_CRC_OFF);
		}
257 258
	}

L
Linus Torvalds 已提交
259
	xfs_trans_dquot_buf(tp, bp,
260 261 262
			    (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
			    ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
			     XFS_BLF_GDQUOT_BUF)));
263
	xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
L
Linus Torvalds 已提交
264 265
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
/*
 * Initialize the dynamic speculative preallocation thresholds. The lo/hi
 * watermarks correspond to the soft and hard limits by default. If a soft limit
 * is not specified, we use 95% of the hard limit.
 */
void
xfs_dquot_set_prealloc_limits(struct xfs_dquot *dqp)
{
	__uint64_t space;

	dqp->q_prealloc_hi_wmark = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
	dqp->q_prealloc_lo_wmark = be64_to_cpu(dqp->q_core.d_blk_softlimit);
	if (!dqp->q_prealloc_lo_wmark) {
		dqp->q_prealloc_lo_wmark = dqp->q_prealloc_hi_wmark;
		do_div(dqp->q_prealloc_lo_wmark, 100);
		dqp->q_prealloc_lo_wmark *= 95;
	}

	space = dqp->q_prealloc_hi_wmark;

	do_div(space, 100);
	dqp->q_low_space[XFS_QLOWSP_1_PCNT] = space;
	dqp->q_low_space[XFS_QLOWSP_3_PCNT] = space * 3;
	dqp->q_low_space[XFS_QLOWSP_5_PCNT] = space * 5;
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
STATIC bool
xfs_dquot_buf_verify_crc(
	struct xfs_mount	*mp,
	struct xfs_buf		*bp)
{
	struct xfs_dqblk	*d = (struct xfs_dqblk *)bp->b_addr;
	int			ndquots;
	int			i;

	if (!xfs_sb_version_hascrc(&mp->m_sb))
		return true;

	/*
	 * if we are in log recovery, the quota subsystem has not been
	 * initialised so we have no quotainfo structure. In that case, we need
	 * to manually calculate the number of dquots in the buffer.
	 */
	if (mp->m_quotainfo)
		ndquots = mp->m_quotainfo->qi_dqperchunk;
	else
		ndquots = xfs_qm_calc_dquots_per_chunk(mp,
					XFS_BB_TO_FSB(mp, bp->b_length));

	for (i = 0; i < ndquots; i++, d++) {
		if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
D
Dave Chinner 已提交
317
				 XFS_DQUOT_CRC_OFF))
318 319 320 321 322 323 324 325
			return false;
		if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_uuid))
			return false;
	}
	return true;
}

STATIC bool
326
xfs_dquot_buf_verify(
327
	struct xfs_mount	*mp,
328 329 330 331
	struct xfs_buf		*bp)
{
	struct xfs_dqblk	*d = (struct xfs_dqblk *)bp->b_addr;
	xfs_dqid_t		id = 0;
332
	int			ndquots;
333 334
	int			i;

335 336 337 338 339 340 341 342 343 344
	/*
	 * if we are in log recovery, the quota subsystem has not been
	 * initialised so we have no quotainfo structure. In that case, we need
	 * to manually calculate the number of dquots in the buffer.
	 */
	if (mp->m_quotainfo)
		ndquots = mp->m_quotainfo->qi_dqperchunk;
	else
		ndquots = xfs_qm_calc_dquots_per_chunk(mp, bp->b_length);

345 346 347 348 349 350 351
	/*
	 * On the first read of the buffer, verify that each dquot is valid.
	 * We don't know what the id of the dquot is supposed to be, just that
	 * they should be increasing monotonically within the buffer. If the
	 * first id is corrupt, then it will fail on the second dquot in the
	 * buffer so corruptions could point to the wrong dquot in this case.
	 */
352 353 354
	for (i = 0; i < ndquots; i++) {
		struct xfs_disk_dquot	*ddq;
		int			error;
355 356 357 358 359 360 361

		ddq = &d[i].dd_diskdq;

		if (i == 0)
			id = be32_to_cpu(ddq->d_id);

		error = xfs_qm_dqcheck(mp, ddq, id + i, 0, XFS_QMOPT_DOWARN,
362 363 364
				       "xfs_dquot_buf_verify");
		if (error)
			return false;
365
	}
366
	return true;
367 368 369
}

static void
370
xfs_dquot_buf_read_verify(
371 372
	struct xfs_buf	*bp)
{
373 374 375 376 377 378
	struct xfs_mount	*mp = bp->b_target->bt_mount;

	if (!xfs_dquot_buf_verify_crc(mp, bp) || !xfs_dquot_buf_verify(mp, bp)) {
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
	}
379
}
L
Linus Torvalds 已提交
380

D
Dave Chinner 已提交
381 382 383 384 385
/*
 * we don't calculate the CRC here as that is done when the dquot is flushed to
 * the buffer after the update is done. This ensures that the dquot in the
 * buffer always has an up-to-date CRC value.
 */
386
void
387
xfs_dquot_buf_write_verify(
388 389
	struct xfs_buf	*bp)
{
390 391 392 393 394 395 396
	struct xfs_mount	*mp = bp->b_target->bt_mount;

	if (!xfs_dquot_buf_verify(mp, bp)) {
		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
		xfs_buf_ioerror(bp, EFSCORRUPTED);
		return;
	}
397
}
L
Linus Torvalds 已提交
398

399 400 401 402 403
const struct xfs_buf_ops xfs_dquot_buf_ops = {
	.verify_read = xfs_dquot_buf_read_verify,
	.verify_write = xfs_dquot_buf_write_verify,
};

L
Linus Torvalds 已提交
404 405 406 407 408 409
/*
 * Allocate a block and fill it with dquots.
 * This is called when the bmapi finds a hole.
 */
STATIC int
xfs_qm_dqalloc(
410
	xfs_trans_t	**tpp,
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418 419 420 421
	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;
422
	xfs_trans_t	*tp = *tpp;
L
Linus Torvalds 已提交
423 424

	ASSERT(tp != NULL);
C
Christoph Hellwig 已提交
425 426

	trace_xfs_dqalloc(dqp);
L
Linus Torvalds 已提交
427 428 429 430

	/*
	 * Initialize the bmap freelist prior to calling bmapi code.
	 */
431
	xfs_bmap_init(&flist, &firstblock);
L
Linus Torvalds 已提交
432 433 434 435 436
	xfs_ilock(quotip, XFS_ILOCK_EXCL);
	/*
	 * Return if this type of quotas is turned off while we didn't
	 * have an inode lock
	 */
437
	if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
L
Linus Torvalds 已提交
438 439 440 441
		xfs_iunlock(quotip, XFS_ILOCK_EXCL);
		return (ESRCH);
	}

442
	xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
443
	nmaps = 1;
444 445 446 447 448
	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 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462
		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,
463
			       mp->m_quotainfo->qi_dqchunklen,
L
Linus Torvalds 已提交
464
			       0);
465 466 467

	error = xfs_buf_geterror(bp);
	if (error)
L
Linus Torvalds 已提交
468
		goto error1;
469
	bp->b_ops = &xfs_dquot_buf_ops;
470

L
Linus Torvalds 已提交
471 472 473 474
	/*
	 * Make a chunk of dquots out of this buffer and log
	 * the entire thing.
	 */
475
	xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
476
			      dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
L
Linus Torvalds 已提交
477

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	/*
	 * 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);

493
	if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
L
Linus Torvalds 已提交
494 495 496
		goto error1;
	}

497 498 499 500 501 502 503
	if (committed) {
		tp = *tpp;
		xfs_trans_bjoin(tp, bp);
	} else {
		xfs_trans_bhold_release(tp, bp);
	}

L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511 512 513
	*O_bpp = bp;
	return 0;

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

	return (error);
}
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
STATIC int
xfs_qm_dqrepair(
	struct xfs_mount	*mp,
	struct xfs_trans	*tp,
	struct xfs_dquot	*dqp,
	xfs_dqid_t		firstid,
	struct xfs_buf		**bpp)
{
	int			error;
	struct xfs_disk_dquot	*ddq;
	struct xfs_dqblk	*d;
	int			i;

	/*
	 * Read the buffer without verification so we get the corrupted
529
	 * buffer returned to us. make sure we verify it on write, though.
530 531 532 533 534 535 536 537 538
	 */
	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, dqp->q_blkno,
				   mp->m_quotainfo->qi_dqchunklen,
				   0, bpp, NULL);

	if (error) {
		ASSERT(*bpp == NULL);
		return XFS_ERROR(error);
	}
539
	(*bpp)->b_ops = &xfs_dquot_buf_ops;
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

	ASSERT(xfs_buf_islocked(*bpp));
	d = (struct xfs_dqblk *)(*bpp)->b_addr;

	/* Do the actual repair of dquots in this buffer */
	for (i = 0; i < mp->m_quotainfo->qi_dqperchunk; i++) {
		ddq = &d[i].dd_diskdq;
		error = xfs_qm_dqcheck(mp, ddq, firstid + i,
				       dqp->dq_flags & XFS_DQ_ALLTYPES,
				       XFS_QMOPT_DQREPAIR, "xfs_qm_dqrepair");
		if (error) {
			/* repair failed, we're screwed */
			xfs_trans_brelse(tp, *bpp);
			return XFS_ERROR(EIO);
		}
	}

	return 0;
}

L
Linus Torvalds 已提交
560 561 562 563 564 565 566
/*
 * 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(
567
	xfs_trans_t		**tpp,
L
Linus Torvalds 已提交
568 569 570 571 572
	xfs_dquot_t		*dqp,
	xfs_disk_dquot_t	**O_ddpp,
	xfs_buf_t		**O_bpp,
	uint			flags)
{
573 574 575
	struct xfs_bmbt_irec	map;
	int			nmaps = 1, error;
	struct xfs_buf		*bp;
576
	struct xfs_inode	*quotip = xfs_dq_to_quota_inode(dqp);
577 578 579
	struct xfs_mount	*mp = dqp->q_mount;
	xfs_dqid_t		id = be32_to_cpu(dqp->q_core.d_id);
	struct xfs_trans	*tp = (tpp ? *tpp : NULL);
L
Linus Torvalds 已提交
580

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

583
	xfs_ilock(quotip, XFS_ILOCK_SHARED);
584
	if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
L
Linus Torvalds 已提交
585
		/*
586 587
		 * Return if this type of quotas is turned off while we
		 * didn't have the quota inode lock.
L
Linus Torvalds 已提交
588
		 */
589 590 591 592 593 594 595
		xfs_iunlock(quotip, XFS_ILOCK_SHARED);
		return ESRCH;
	}

	/*
	 * Find the block map; no allocations yet
	 */
D
Dave Chinner 已提交
596 597
	error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
			       XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

	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 已提交
614
		/*
615
		 * We don't allocate unless we're asked to
L
Linus Torvalds 已提交
616
		 */
617 618
		if (!(flags & XFS_QMOPT_DQALLOC))
			return ENOENT;
L
Linus Torvalds 已提交
619

620 621 622
		ASSERT(tp);
		error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
					dqp->q_fileoffset, &bp);
L
Linus Torvalds 已提交
623
		if (error)
624 625 626 627
			return error;
		tp = *tpp;
	} else {
		trace_xfs_dqtobp_read(dqp);
L
Linus Torvalds 已提交
628 629

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

635 636 637
		error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
					   dqp->q_blkno,
					   mp->m_quotainfo->qi_dqchunklen,
638
					   0, &bp, &xfs_dquot_buf_ops);
639

640 641 642 643 644 645
		if (error == EFSCORRUPTED && (flags & XFS_QMOPT_DQREPAIR)) {
			xfs_dqid_t firstid = (xfs_dqid_t)map.br_startoff *
						mp->m_quotainfo->qi_dqperchunk;
			ASSERT(bp == NULL);
			error = xfs_qm_dqrepair(mp, tp, dqp, firstid, &bp);
		}
L
Linus Torvalds 已提交
646

647 648 649
		if (error) {
			ASSERT(bp == NULL);
			return XFS_ERROR(error);
L
Linus Torvalds 已提交
650 651 652
		}
	}

653
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
654
	*O_bpp = bp;
655
	*O_ddpp = bp->b_addr + dqp->q_bufoffset;
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664

	return (0);
}


/*
 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
 * and release the buffer immediately.
 *
C
Christoph Hellwig 已提交
665
 * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
L
Linus Torvalds 已提交
666
 */
C
Christoph Hellwig 已提交
667
int
L
Linus Torvalds 已提交
668
xfs_qm_dqread(
C
Christoph Hellwig 已提交
669 670 671 672 673
	struct xfs_mount	*mp,
	xfs_dqid_t		id,
	uint			type,
	uint			flags,
	struct xfs_dquot	**O_dqpp)
L
Linus Torvalds 已提交
674
{
C
Christoph Hellwig 已提交
675 676 677 678 679 680
	struct xfs_dquot	*dqp;
	struct xfs_disk_dquot	*ddqp;
	struct xfs_buf		*bp;
	struct xfs_trans	*tp = NULL;
	int			error;
	int			cancelflags = 0;
681

682

683
	dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
684 685 686 687

	dqp->dq_flags = type;
	dqp->q_core.d_id = cpu_to_be32(id);
	dqp->q_mount = mp;
688
	INIT_LIST_HEAD(&dqp->q_lru);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	mutex_init(&dqp->q_qlock);
	init_waitqueue_head(&dqp->q_pinwait);

	/*
	 * 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);

	/*
	 * Make sure group quotas have a different lock class than user
	 * quotas.
	 */
	if (!(type & XFS_DQ_USER))
		lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);

707
	XFS_STATS_INC(xs_qm_dquot);
L
Linus Torvalds 已提交
708

C
Christoph Hellwig 已提交
709 710
	trace_xfs_dqread(dqp);

C
Christoph Hellwig 已提交
711 712 713
	if (flags & XFS_QMOPT_DQALLOC) {
		tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
		error = xfs_trans_reserve(tp, XFS_QM_DQALLOC_SPACE_RES(mp),
714 715 716
					  XFS_QM_DQALLOC_LOG_RES(mp), 0,
					  XFS_TRANS_PERM_LOG_RES,
					  XFS_WRITE_LOG_COUNT);
C
Christoph Hellwig 已提交
717 718 719 720 721
		if (error)
			goto error1;
		cancelflags = XFS_TRANS_RELEASE_LOG_RES;
	}

L
Linus Torvalds 已提交
722 723 724 725
	/*
	 * get a pointer to the on-disk dquot and the buffer containing it
	 * dqp already knows its own type (GROUP/USER).
	 */
C
Christoph Hellwig 已提交
726 727 728 729 730 731 732 733 734 735
	error = xfs_qm_dqtobp(&tp, dqp, &ddqp, &bp, flags);
	if (error) {
		/*
		 * This can happen if quotas got turned off (ESRCH),
		 * or if the dquot didn't exist on disk and we ask to
		 * allocate (ENOENT).
		 */
		trace_xfs_dqread_fail(dqp);
		cancelflags |= XFS_TRANS_ABORT;
		goto error1;
L
Linus Torvalds 已提交
736 737 738 739 740 741 742 743
	}

	/* copy everything from disk dquot to the incore dquot */
	memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
	xfs_qm_dquot_logitem_init(dqp);

	/*
	 * Reservation counters are defined as reservation plus current usage
L
Lucas De Marchi 已提交
744
	 * to avoid having to add every time.
L
Linus Torvalds 已提交
745
	 */
746 747 748
	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 已提交
749

750 751 752
	/* initialize the dquot speculative prealloc thresholds */
	xfs_dquot_set_prealloc_limits(dqp);

L
Linus Torvalds 已提交
753
	/* Mark the buf so that this will stay incore a little longer */
754
	xfs_buf_set_ref(bp, XFS_DQUOT_REF);
L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767

	/*
	 * 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.
	 */
768
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
769 770 771
	xfs_trans_brelse(tp, bp);

	if (tp) {
C
Christoph Hellwig 已提交
772 773 774
		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
		if (error)
			goto error0;
L
Linus Torvalds 已提交
775 776 777
	}

	*O_dqpp = dqp;
C
Christoph Hellwig 已提交
778
	return error;
L
Linus Torvalds 已提交
779

C
Christoph Hellwig 已提交
780
error1:
L
Linus Torvalds 已提交
781 782
	if (tp)
		xfs_trans_cancel(tp, cancelflags);
C
Christoph Hellwig 已提交
783
error0:
L
Linus Torvalds 已提交
784 785
	xfs_qm_dqdestroy(dqp);
	*O_dqpp = NULL;
C
Christoph Hellwig 已提交
786
	return error;
L
Linus Torvalds 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800
}

/*
 * 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) */
801 802
	xfs_dqid_t	id,	  /* uid/projid/gid depending on type */
	uint		type,	  /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
L
Linus Torvalds 已提交
803 804 805
	uint		flags,	  /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
	xfs_dquot_t	**O_dqpp) /* OUT : locked incore dquot */
{
806
	struct xfs_quotainfo	*qi = mp->m_quotainfo;
807
	struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
808 809
	struct xfs_dquot	*dqp;
	int			error;
L
Linus Torvalds 已提交
810 811 812

	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
	if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
813
	    (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821
	    (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
		return (ESRCH);
	}

#ifdef DEBUG
	if (xfs_do_dqerror) {
		if ((xfs_dqerror_target == mp->m_ddev_targp) &&
		    (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
822
			xfs_debug(mp, "Returning error in dqget");
L
Linus Torvalds 已提交
823 824 825 826
			return (EIO);
		}
	}

827 828 829
	ASSERT(type == XFS_DQ_USER ||
	       type == XFS_DQ_PROJ ||
	       type == XFS_DQ_GROUP);
L
Linus Torvalds 已提交
830
	if (ip) {
C
Christoph Hellwig 已提交
831
		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
832
		ASSERT(xfs_inode_dquot(ip, type) == NULL);
L
Linus Torvalds 已提交
833 834
	}
#endif
835 836

restart:
837 838 839 840 841 842 843 844 845 846 847
	mutex_lock(&qi->qi_tree_lock);
	dqp = radix_tree_lookup(tree, id);
	if (dqp) {
		xfs_dqlock(dqp);
		if (dqp->dq_flags & XFS_DQ_FREEING) {
			xfs_dqunlock(dqp);
			mutex_unlock(&qi->qi_tree_lock);
			trace_xfs_dqget_freeing(dqp);
			delay(1);
			goto restart;
		}
L
Linus Torvalds 已提交
848

849 850 851 852
		dqp->q_nrefs++;
		mutex_unlock(&qi->qi_tree_lock);

		trace_xfs_dqget_hit(dqp);
853
		XFS_STATS_INC(xs_qm_dqcachehits);
854 855
		*O_dqpp = dqp;
		return 0;
L
Linus Torvalds 已提交
856
	}
857 858
	mutex_unlock(&qi->qi_tree_lock);
	XFS_STATS_INC(xs_qm_dqcachemisses);
L
Linus Torvalds 已提交
859 860 861 862 863 864 865 866 867 868 869

	/*
	 * 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);

C
Christoph Hellwig 已提交
870
	error = xfs_qm_dqread(mp, id, type, flags, &dqp);
L
Linus Torvalds 已提交
871

C
Christoph Hellwig 已提交
872 873 874 875 876
	if (ip)
		xfs_ilock(ip, XFS_ILOCK_EXCL);

	if (error)
		return error;
L
Linus Torvalds 已提交
877 878 879 880 881 882

	if (ip) {
		/*
		 * A dquot could be attached to this inode by now, since
		 * we had dropped the ilock.
		 */
883
		if (xfs_this_quota_on(mp, type)) {
884 885
			struct xfs_dquot	*dqp1;

886 887
			dqp1 = xfs_inode_dquot(ip, type);
			if (dqp1) {
L
Linus Torvalds 已提交
888
				xfs_qm_dqdestroy(dqp);
889
				dqp = dqp1;
L
Linus Torvalds 已提交
890 891 892 893
				xfs_dqlock(dqp);
				goto dqret;
			}
		} else {
894 895 896
			/* inode stays locked on return */
			xfs_qm_dqdestroy(dqp);
			return XFS_ERROR(ESRCH);
L
Linus Torvalds 已提交
897 898 899
		}
	}

900 901 902 903 904
	mutex_lock(&qi->qi_tree_lock);
	error = -radix_tree_insert(tree, id, dqp);
	if (unlikely(error)) {
		WARN_ON(error != EEXIST);

L
Linus Torvalds 已提交
905
		/*
906 907
		 * Duplicate found. Just throw away the new dquot and start
		 * over.
L
Linus Torvalds 已提交
908
		 */
909 910 911 912 913
		mutex_unlock(&qi->qi_tree_lock);
		trace_xfs_dqget_dup(dqp);
		xfs_qm_dqdestroy(dqp);
		XFS_STATS_INC(xs_qm_dquot_dups);
		goto restart;
L
Linus Torvalds 已提交
914 915 916 917 918 919 920 921
	}

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

922 923 924
	qi->qi_dquots++;
	mutex_unlock(&qi->qi_tree_lock);

L
Linus Torvalds 已提交
925
 dqret:
C
Christoph Hellwig 已提交
926
	ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
C
Christoph Hellwig 已提交
927
	trace_xfs_dqget_miss(dqp);
L
Linus Torvalds 已提交
928 929 930 931 932
	*O_dqpp = dqp;
	return (0);
}


933 934
STATIC void
xfs_qm_dqput_final(
935
	struct xfs_dquot	*dqp)
L
Linus Torvalds 已提交
936
{
937
	struct xfs_quotainfo	*qi = dqp->q_mount->m_quotainfo;
938
	struct xfs_dquot	*gdqp;
939
	struct xfs_dquot	*pdqp;
L
Linus Torvalds 已提交
940

941 942
	trace_xfs_dqput_free(dqp);

943 944 945 946
	mutex_lock(&qi->qi_lru_lock);
	if (list_empty(&dqp->q_lru)) {
		list_add_tail(&dqp->q_lru, &qi->qi_lru_list);
		qi->qi_lru_count++;
947
		XFS_STATS_INC(xs_qm_dquot_unused);
948
	}
949
	mutex_unlock(&qi->qi_lru_lock);
950

L
Linus Torvalds 已提交
951
	/*
952
	 * If we just added a udquot to the freelist, then we want to release
953 954
	 * the gdquot/pdquot reference that it (probably) has. Otherwise it'll
	 * keep the gdquot/pdquot from getting reclaimed.
L
Linus Torvalds 已提交
955
	 */
956 957 958 959
	gdqp = dqp->q_gdquot;
	if (gdqp) {
		xfs_dqlock(gdqp);
		dqp->q_gdquot = NULL;
L
Linus Torvalds 已提交
960
	}
961 962 963 964 965 966

	pdqp = dqp->q_pdquot;
	if (pdqp) {
		xfs_dqlock(pdqp);
		dqp->q_pdquot = NULL;
	}
967
	xfs_dqunlock(dqp);
L
Linus Torvalds 已提交
968

969
	/*
970
	 * If we had a group/project quota hint, release it now.
971
	 */
972 973
	if (gdqp)
		xfs_qm_dqput(gdqp);
974 975
	if (pdqp)
		xfs_qm_dqput(pdqp);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
}

/*
 * 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(
	struct xfs_dquot	*dqp)
{
	ASSERT(dqp->q_nrefs > 0);
	ASSERT(XFS_DQ_IS_LOCKED(dqp));

	trace_xfs_dqput(dqp);

	if (--dqp->q_nrefs > 0)
		xfs_dqunlock(dqp);
	else
		xfs_qm_dqput_final(dqp);
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006
}

/*
 * 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 已提交
1007 1008 1009
	if (!dqp)
		return;

C
Christoph Hellwig 已提交
1010
	trace_xfs_dqrele(dqp);
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	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);
}

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
/*
 * 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)
1052
			xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
1053 1054 1055 1056 1057 1058 1059 1060 1061
		else
			spin_unlock(&ailp->xa_lock);
	}

	/*
	 * Release the dq's flush lock since we're done with it.
	 */
	xfs_dqfunlock(dqp);
}
L
Linus Torvalds 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

/*
 * 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(
1073 1074
	struct xfs_dquot	*dqp,
	struct xfs_buf		**bpp)
L
Linus Torvalds 已提交
1075
{
1076 1077 1078
	struct xfs_mount	*mp = dqp->q_mount;
	struct xfs_buf		*bp;
	struct xfs_disk_dquot	*ddqp;
L
Linus Torvalds 已提交
1079 1080 1081
	int			error;

	ASSERT(XFS_DQ_IS_LOCKED(dqp));
1082
	ASSERT(!completion_done(&dqp->q_flush));
1083

C
Christoph Hellwig 已提交
1084
	trace_xfs_dqflush(dqp);
L
Linus Torvalds 已提交
1085

1086 1087
	*bpp = NULL;

L
Linus Torvalds 已提交
1088 1089 1090 1091 1092
	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
1093 1094 1095 1096
	 * to disk, because the log record didn't make it to disk.
	 *
	 * We also have to remove the log item from the AIL in this case,
	 * as we wait for an emptry AIL as part of the unmount process.
L
Linus Torvalds 已提交
1097
	 */
1098
	if (XFS_FORCED_SHUTDOWN(mp)) {
1099
		struct xfs_log_item	*lip = &dqp->q_logitem.qli_item;
1100
		dqp->dq_flags &= ~XFS_DQ_DIRTY;
1101 1102 1103

		spin_lock(&mp->m_ail->xa_lock);
		if (lip->li_flags & XFS_LI_IN_AIL)
1104 1105
			xfs_trans_ail_delete(mp->m_ail, lip,
					     SHUTDOWN_CORRUPT_INCORE);
1106 1107
		else
			spin_unlock(&mp->m_ail->xa_lock);
1108 1109
		error = XFS_ERROR(EIO);
		goto out_unlock;
L
Linus Torvalds 已提交
1110 1111 1112 1113 1114
	}

	/*
	 * Get the buffer containing the on-disk dquot
	 */
1115
	error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1116
				   mp->m_quotainfo->qi_dqchunklen, 0, &bp, NULL);
1117 1118
	if (error)
		goto out_unlock;
L
Linus Torvalds 已提交
1119

1120 1121 1122
	/*
	 * Calculate the location of the dquot inside the buffer.
	 */
1123
	ddqp = bp->b_addr + dqp->q_bufoffset;
1124 1125 1126 1127

	/*
	 * A simple sanity check in case we got a corrupted dquot..
	 */
1128 1129 1130
	error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
			   XFS_QMOPT_DOWARN, "dqflush (incore copy)");
	if (error) {
1131 1132 1133
		xfs_buf_relse(bp);
		xfs_dqfunlock(dqp);
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
L
Linus Torvalds 已提交
1134 1135 1136 1137
		return XFS_ERROR(EIO);
	}

	/* This is the only portion of data that needs to persist */
1138
	memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
L
Linus Torvalds 已提交
1139 1140 1141 1142

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

1145 1146
	xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
					&dqp->q_logitem.qli_item.li_lsn);
L
Linus Torvalds 已提交
1147

1148 1149 1150 1151
	/*
	 * copy the lsn into the on-disk dquot now while we have the in memory
	 * dquot here. This can't be done later in the write verifier as we
	 * can't get access to the log item at that point in time.
D
Dave Chinner 已提交
1152 1153 1154 1155
	 *
	 * We also calculate the CRC here so that the on-disk dquot in the
	 * buffer always has a valid CRC. This ensures there is no possibility
	 * of a dquot without an up-to-date CRC getting to disk.
1156 1157 1158 1159 1160
	 */
	if (xfs_sb_version_hascrc(&mp->m_sb)) {
		struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddqp;

		dqb->dd_lsn = cpu_to_be64(dqp->q_logitem.qli_item.li_lsn);
D
Dave Chinner 已提交
1161 1162
		xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
				 XFS_DQUOT_CRC_OFF);
1163 1164
	}

L
Linus Torvalds 已提交
1165 1166 1167 1168
	/*
	 * 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.
	 */
1169 1170 1171
	xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
				  &dqp->q_logitem.qli_item);

L
Linus Torvalds 已提交
1172 1173 1174 1175
	/*
	 * If the buffer is pinned then push on the log so we won't
	 * get stuck waiting in the write for too long.
	 */
1176
	if (xfs_buf_ispinned(bp)) {
C
Christoph Hellwig 已提交
1177
		trace_xfs_dqflush_force(dqp);
1178
		xfs_log_force(mp, 0);
L
Linus Torvalds 已提交
1179 1180
	}

C
Christoph Hellwig 已提交
1181
	trace_xfs_dqflush_done(dqp);
1182 1183
	*bpp = bp;
	return 0;
C
Christoph Hellwig 已提交
1184

1185 1186 1187
out_unlock:
	xfs_dqfunlock(dqp);
	return XFS_ERROR(EIO);
L
Linus Torvalds 已提交
1188 1189
}

1190 1191 1192 1193 1194 1195
/*
 * Lock two xfs_dquot structures.
 *
 * To avoid deadlocks we always lock the quota structure with
 * the lowerd id first.
 */
L
Linus Torvalds 已提交
1196 1197 1198 1199 1200 1201 1202
void
xfs_dqlock2(
	xfs_dquot_t	*d1,
	xfs_dquot_t	*d2)
{
	if (d1 && d2) {
		ASSERT(d1 != d2);
1203 1204
		if (be32_to_cpu(d1->q_core.d_id) >
		    be32_to_cpu(d2->q_core.d_id)) {
1205 1206
			mutex_lock(&d2->q_qlock);
			mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1207
		} else {
1208 1209
			mutex_lock(&d1->q_qlock);
			mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1210
		}
1211 1212 1213 1214
	} else if (d1) {
		mutex_lock(&d1->q_qlock);
	} else if (d2) {
		mutex_lock(&d2->q_qlock);
L
Linus Torvalds 已提交
1215 1216 1217
	}
}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
int __init
xfs_qm_init(void)
{
	xfs_qm_dqzone =
		kmem_zone_init(sizeof(struct xfs_dquot), "xfs_dquot");
	if (!xfs_qm_dqzone)
		goto out;

	xfs_qm_dqtrxzone =
		kmem_zone_init(sizeof(struct xfs_dquot_acct), "xfs_dqtrx");
	if (!xfs_qm_dqtrxzone)
		goto out_free_dqzone;

	return 0;

out_free_dqzone:
	kmem_zone_destroy(xfs_qm_dqzone);
out:
	return -ENOMEM;
}

1239
void
1240 1241 1242 1243 1244
xfs_qm_exit(void)
{
	kmem_zone_destroy(xfs_qm_dqtrxzone);
	kmem_zone_destroy(xfs_qm_dqzone);
}