xfs_dquot.c 30.0 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_format.h"
21
#include "xfs_log_format.h"
22
#include "xfs_shared.h"
23
#include "xfs_trans_resv.h"
24
#include "xfs_bit.h"
L
Linus Torvalds 已提交
25
#include "xfs_mount.h"
26
#include "xfs_defer.h"
L
Linus Torvalds 已提交
27 28
#include "xfs_inode.h"
#include "xfs_bmap.h"
D
Dave Chinner 已提交
29
#include "xfs_bmap_util.h"
30 31
#include "xfs_alloc.h"
#include "xfs_quota.h"
L
Linus Torvalds 已提交
32
#include "xfs_error.h"
33
#include "xfs_trans.h"
L
Linus Torvalds 已提交
34 35 36 37
#include "xfs_buf_item.h"
#include "xfs_trans_space.h"
#include "xfs_trans_priv.h"
#include "xfs_qm.h"
38
#include "xfs_cksum.h"
C
Christoph Hellwig 已提交
39
#include "xfs_trace.h"
40
#include "xfs_log.h"
41
#include "xfs_bmap_btree.h"
L
Linus Torvalds 已提交
42 43

/*
44 45 46
 * Lock order:
 *
 * ip->i_lock
47
 *   qi->qi_tree_lock
48 49 50
 *     dquot->q_qlock (xfs_dqlock() and friends)
 *       dquot->q_flush (xfs_dqflock() and friends)
 *       qi->qi_lru_lock
51 52 53 54
 *
 * 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 已提交
55 56 57 58 59 60 61 62

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

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

66 67
static struct lock_class_key xfs_dquot_group_class;
static struct lock_class_key xfs_dquot_project_class;
68

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

78
	kmem_free(dqp->q_logitem.qli_item.li_lv_shadow);
L
Linus Torvalds 已提交
79
	mutex_destroy(&dqp->q_qlock);
C
Christoph Hellwig 已提交
80

81 82
	XFS_STATS_DEC(dqp->q_mount, xs_qm_dquot);
	kmem_zone_free(xfs_qm_dqzone, dqp);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91
}

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

	ASSERT(d->d_id);
101
	defq = xfs_get_defquota(dq, q);
L
Linus Torvalds 已提交
102

103 104
	if (defq->bsoftlimit && !d->d_blk_softlimit) {
		d->d_blk_softlimit = cpu_to_be64(defq->bsoftlimit);
105 106
		prealloc = 1;
	}
107 108
	if (defq->bhardlimit && !d->d_blk_hardlimit) {
		d->d_blk_hardlimit = cpu_to_be64(defq->bhardlimit);
109 110
		prealloc = 1;
	}
111 112 113 114 115 116 117 118
	if (defq->isoftlimit && !d->d_ino_softlimit)
		d->d_ino_softlimit = cpu_to_be64(defq->isoftlimit);
	if (defq->ihardlimit && !d->d_ino_hardlimit)
		d->d_ino_hardlimit = cpu_to_be64(defq->ihardlimit);
	if (defq->rtbsoftlimit && !d->d_rtb_softlimit)
		d->d_rtb_softlimit = cpu_to_be64(defq->rtbsoftlimit);
	if (defq->rtbhardlimit && !d->d_rtb_hardlimit)
		d->d_rtb_hardlimit = cpu_to_be64(defq->rtbhardlimit);
119 120 121

	if (prealloc)
		xfs_dquot_set_prealloc_limits(dq);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132
}

/*
 * 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
133 134 135
 * '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 已提交
136 137 138 139 140 141 142 143
 */
void
xfs_qm_adjust_dqtimers(
	xfs_mount_t		*mp,
	xfs_disk_dquot_t	*d)
{
	ASSERT(d->d_id);

144
#ifdef DEBUG
145 146 147 148 149 150 151 152 153
	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 已提交
154
#endif
155

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

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

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

	ASSERT(tp);
243
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
244

245
	d = bp->b_addr;
L
Linus Torvalds 已提交
246 247 248 249

	/*
	 * ID of the first dquot in the block - id's are zero based.
	 */
250 251
	curid = id - (id % q->qi_dqperchunk);
	memset(d, 0, BBTOB(q->qi_dqchunklen));
252 253 254 255 256
	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 已提交
257
		if (xfs_sb_version_hascrc(&mp->m_sb)) {
258
			uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
D
Dave Chinner 已提交
259 260 261
			xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
					 XFS_DQUOT_CRC_OFF);
		}
262 263
	}

L
Linus Torvalds 已提交
264
	xfs_trans_dquot_buf(tp, bp,
265 266 267
			    (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
			    ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
			     XFS_BLF_GDQUOT_BUF)));
268
	xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
L
Linus Torvalds 已提交
269 270
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/*
 * 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;
}

L
Linus Torvalds 已提交
297 298 299 300 301 302
/*
 * Allocate a block and fill it with dquots.
 * This is called when the bmapi finds a hole.
 */
STATIC int
xfs_qm_dqalloc(
303
	xfs_trans_t	**tpp,
L
Linus Torvalds 已提交
304 305 306 307 308 309 310
	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;
311
	struct xfs_defer_ops dfops;
L
Linus Torvalds 已提交
312
	xfs_bmbt_irec_t map;
313
	int		nmaps, error;
L
Linus Torvalds 已提交
314
	xfs_buf_t	*bp;
315
	xfs_trans_t	*tp = *tpp;
L
Linus Torvalds 已提交
316 317

	ASSERT(tp != NULL);
C
Christoph Hellwig 已提交
318 319

	trace_xfs_dqalloc(dqp);
L
Linus Torvalds 已提交
320 321 322 323

	/*
	 * Initialize the bmap freelist prior to calling bmapi code.
	 */
324
	xfs_defer_init(&dfops, &firstblock);
L
Linus Torvalds 已提交
325 326 327 328 329
	xfs_ilock(quotip, XFS_ILOCK_EXCL);
	/*
	 * Return if this type of quotas is turned off while we didn't
	 * have an inode lock
	 */
330
	if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
L
Linus Torvalds 已提交
331
		xfs_iunlock(quotip, XFS_ILOCK_EXCL);
D
Dave Chinner 已提交
332
		return -ESRCH;
L
Linus Torvalds 已提交
333 334
	}

335
	xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
336
	nmaps = 1;
337 338 339
	error = xfs_bmapi_write(tp, quotip, offset_fsb,
				XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA,
				&firstblock, XFS_QM_DQALLOC_SPACE_RES(mp),
340
				&map, &nmaps, &dfops);
341
	if (error)
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355
		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,
356
			       mp->m_quotainfo->qi_dqchunklen,
L
Linus Torvalds 已提交
357
			       0);
D
Dave Chinner 已提交
358
	if (!bp) {
D
Dave Chinner 已提交
359
		error = -ENOMEM;
L
Linus Torvalds 已提交
360
		goto error1;
D
Dave Chinner 已提交
361
	}
362
	bp->b_ops = &xfs_dquot_buf_ops;
363

L
Linus Torvalds 已提交
364 365 366 367
	/*
	 * Make a chunk of dquots out of this buffer and log
	 * the entire thing.
	 */
368
	xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
369
			      dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
L
Linus Torvalds 已提交
370

371
	/*
372
	 * xfs_defer_finish() may commit the current transaction and
373 374 375 376 377 378 379 380 381 382 383 384 385
	 * 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);

386
	error = xfs_defer_finish(tpp, &dfops, NULL);
387
	if (error)
L
Linus Torvalds 已提交
388 389
		goto error1;

390 391
	/* Transaction was committed? */
	if (*tpp != tp) {
392 393 394 395 396 397
		tp = *tpp;
		xfs_trans_bjoin(tp, bp);
	} else {
		xfs_trans_bhold_release(tp, bp);
	}

L
Linus Torvalds 已提交
398 399 400
	*O_bpp = bp;
	return 0;

401
error1:
402
	xfs_defer_cancel(&dfops);
403
error0:
L
Linus Torvalds 已提交
404 405
	xfs_iunlock(quotip, XFS_ILOCK_EXCL);

E
Eric Sandeen 已提交
406
	return error;
L
Linus Torvalds 已提交
407
}
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
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
424
	 * buffer returned to us. make sure we verify it on write, though.
425 426 427 428 429 430 431
	 */
	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);
E
Eric Sandeen 已提交
432
		return error;
433
	}
434
	(*bpp)->b_ops = &xfs_dquot_buf_ops;
435 436 437 438 439 440 441

	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;
442
		error = xfs_dqcheck(mp, ddq, firstid + i,
443 444 445 446 447
				       dqp->dq_flags & XFS_DQ_ALLTYPES,
				       XFS_QMOPT_DQREPAIR, "xfs_qm_dqrepair");
		if (error) {
			/* repair failed, we're screwed */
			xfs_trans_brelse(tp, *bpp);
D
Dave Chinner 已提交
448
			return -EIO;
449 450 451 452 453 454
		}
	}

	return 0;
}

L
Linus Torvalds 已提交
455 456 457 458 459 460 461
/*
 * 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(
462
	xfs_trans_t		**tpp,
L
Linus Torvalds 已提交
463 464 465 466 467
	xfs_dquot_t		*dqp,
	xfs_disk_dquot_t	**O_ddpp,
	xfs_buf_t		**O_bpp,
	uint			flags)
{
468 469 470
	struct xfs_bmbt_irec	map;
	int			nmaps = 1, error;
	struct xfs_buf		*bp;
471
	struct xfs_inode	*quotip;
472 473 474
	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);
475
	uint			lock_mode;
L
Linus Torvalds 已提交
476

477
	quotip = xfs_quota_inode(dqp->q_mount, dqp->dq_flags);
478
	dqp->q_fileoffset = (xfs_fileoff_t)id / mp->m_quotainfo->qi_dqperchunk;
L
Linus Torvalds 已提交
479

480
	lock_mode = xfs_ilock_data_map_shared(quotip);
481
	if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
L
Linus Torvalds 已提交
482
		/*
483 484
		 * Return if this type of quotas is turned off while we
		 * didn't have the quota inode lock.
L
Linus Torvalds 已提交
485
		 */
486
		xfs_iunlock(quotip, lock_mode);
D
Dave Chinner 已提交
487
		return -ESRCH;
488 489 490 491 492
	}

	/*
	 * Find the block map; no allocations yet
	 */
D
Dave Chinner 已提交
493 494
	error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
			       XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
495

496
	xfs_iunlock(quotip, lock_mode);
497 498 499 500 501 502 503 504 505 506 507 508 509 510
	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 已提交
511
		/*
512
		 * We don't allocate unless we're asked to
L
Linus Torvalds 已提交
513
		 */
514
		if (!(flags & XFS_QMOPT_DQALLOC))
D
Dave Chinner 已提交
515
			return -ENOENT;
L
Linus Torvalds 已提交
516

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

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

532 533 534
		error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
					   dqp->q_blkno,
					   mp->m_quotainfo->qi_dqchunklen,
535
					   0, &bp, &xfs_dquot_buf_ops);
536

D
Dave Chinner 已提交
537
		if (error == -EFSCORRUPTED && (flags & XFS_QMOPT_DQREPAIR)) {
538 539 540 541 542
			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 已提交
543

544 545
		if (error) {
			ASSERT(bp == NULL);
E
Eric Sandeen 已提交
546
			return error;
L
Linus Torvalds 已提交
547 548 549
		}
	}

550
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
551
	*O_bpp = bp;
552
	*O_ddpp = bp->b_addr + dqp->q_bufoffset;
L
Linus Torvalds 已提交
553

E
Eric Sandeen 已提交
554
	return 0;
L
Linus Torvalds 已提交
555 556 557 558 559 560 561
}


/*
 * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
 * and release the buffer immediately.
 *
C
Christoph Hellwig 已提交
562
 * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
L
Linus Torvalds 已提交
563
 */
C
Christoph Hellwig 已提交
564
int
L
Linus Torvalds 已提交
565
xfs_qm_dqread(
C
Christoph Hellwig 已提交
566 567 568 569 570
	struct xfs_mount	*mp,
	xfs_dqid_t		id,
	uint			type,
	uint			flags,
	struct xfs_dquot	**O_dqpp)
L
Linus Torvalds 已提交
571
{
C
Christoph Hellwig 已提交
572 573 574 575 576
	struct xfs_dquot	*dqp;
	struct xfs_disk_dquot	*ddqp;
	struct xfs_buf		*bp;
	struct xfs_trans	*tp = NULL;
	int			error;
577

578
	dqp = kmem_zone_zalloc(xfs_qm_dqzone, KM_SLEEP);
579 580 581 582

	dqp->dq_flags = type;
	dqp->q_core.d_id = cpu_to_be32(id);
	dqp->q_mount = mp;
583
	INIT_LIST_HEAD(&dqp->q_lru);
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	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.
	 */
599 600 601 602 603 604 605 606 607 608 609 610 611 612
	switch (type) {
	case XFS_DQ_USER:
		/* uses the default lock class */
		break;
	case XFS_DQ_GROUP:
		lockdep_set_class(&dqp->q_qlock, &xfs_dquot_group_class);
		break;
	case XFS_DQ_PROJ:
		lockdep_set_class(&dqp->q_qlock, &xfs_dquot_project_class);
		break;
	default:
		ASSERT(0);
		break;
	}
613

614
	XFS_STATS_INC(mp, xs_qm_dquot);
L
Linus Torvalds 已提交
615

C
Christoph Hellwig 已提交
616 617
	trace_xfs_dqread(dqp);

C
Christoph Hellwig 已提交
618
	if (flags & XFS_QMOPT_DQALLOC) {
619 620
		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_qm_dqalloc,
				XFS_QM_DQALLOC_SPACE_RES(mp), 0, 0, &tp);
C
Christoph Hellwig 已提交
621
		if (error)
622
			goto error0;
C
Christoph Hellwig 已提交
623 624
	}

L
Linus Torvalds 已提交
625 626 627 628
	/*
	 * get a pointer to the on-disk dquot and the buffer containing it
	 * dqp already knows its own type (GROUP/USER).
	 */
C
Christoph Hellwig 已提交
629 630 631 632 633 634 635 636 637
	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);
		goto error1;
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645
	}

	/* 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 已提交
646
	 * to avoid having to add every time.
L
Linus Torvalds 已提交
647
	 */
648 649 650
	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 已提交
651

652 653 654
	/* initialize the dquot speculative prealloc thresholds */
	xfs_dquot_set_prealloc_limits(dqp);

L
Linus Torvalds 已提交
655
	/* Mark the buf so that this will stay incore a little longer */
656
	xfs_buf_set_ref(bp, XFS_DQUOT_REF);
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669

	/*
	 * 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.
	 */
670
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
671 672 673
	xfs_trans_brelse(tp, bp);

	if (tp) {
674
		error = xfs_trans_commit(tp);
C
Christoph Hellwig 已提交
675 676
		if (error)
			goto error0;
L
Linus Torvalds 已提交
677 678 679
	}

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

C
Christoph Hellwig 已提交
682
error1:
L
Linus Torvalds 已提交
683
	if (tp)
684
		xfs_trans_cancel(tp);
C
Christoph Hellwig 已提交
685
error0:
L
Linus Torvalds 已提交
686 687
	xfs_qm_dqdestroy(dqp);
	*O_dqpp = NULL;
C
Christoph Hellwig 已提交
688
	return error;
L
Linus Torvalds 已提交
689 690
}

691 692 693 694 695
/*
 * Advance to the next id in the current chunk, or if at the
 * end of the chunk, skip ahead to first id in next allocated chunk
 * using the SEEK_DATA interface.
 */
E
Eryu Guan 已提交
696
static int
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
xfs_dq_get_next_id(
	xfs_mount_t		*mp,
	uint			type,
	xfs_dqid_t		*id,
	loff_t			eof)
{
	struct xfs_inode	*quotip;
	xfs_fsblock_t		start;
	loff_t			offset;
	uint			lock;
	xfs_dqid_t		next_id;
	int			error = 0;

	/* Simple advance */
	next_id = *id + 1;

713 714 715 716
	/* If we'd wrap past the max ID, stop */
	if (next_id < *id)
		return -ENOENT;

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	/* If new ID is within the current chunk, advancing it sufficed */
	if (next_id % mp->m_quotainfo->qi_dqperchunk) {
		*id = next_id;
		return 0;
	}

	/* Nope, next_id is now past the current chunk, so find the next one */
	start = (xfs_fsblock_t)next_id / mp->m_quotainfo->qi_dqperchunk;

	quotip = xfs_quota_inode(mp, type);
	lock = xfs_ilock_data_map_shared(quotip);

	offset = __xfs_seek_hole_data(VFS_I(quotip), XFS_FSB_TO_B(mp, start),
				      eof, SEEK_DATA);
	if (offset < 0)
		error = offset;

	xfs_iunlock(quotip, lock);

	/* -ENXIO is essentially "no more data" */
	if (error)
		return (error == -ENXIO ? -ENOENT: error);

	/* Convert next data offset back to a quota id */
	*id = XFS_B_TO_FSB(mp, offset) * mp->m_quotainfo->qi_dqperchunk;
	return 0;
}

L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754 755 756
/*
 * 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) */
757 758
	xfs_dqid_t	id,	  /* uid/projid/gid depending on type */
	uint		type,	  /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
L
Linus Torvalds 已提交
759 760 761
	uint		flags,	  /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
	xfs_dquot_t	**O_dqpp) /* OUT : locked incore dquot */
{
762
	struct xfs_quotainfo	*qi = mp->m_quotainfo;
763
	struct radix_tree_root *tree = xfs_dquot_tree(qi, type);
764
	struct xfs_dquot	*dqp;
765
	loff_t			eof = 0;
766
	int			error;
L
Linus Torvalds 已提交
767 768 769

	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
	if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
770
	    (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
L
Linus Torvalds 已提交
771
	    (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
D
Dave Chinner 已提交
772
		return -ESRCH;
L
Linus Torvalds 已提交
773 774 775 776 777 778
	}

#ifdef DEBUG
	if (xfs_do_dqerror) {
		if ((xfs_dqerror_target == mp->m_ddev_targp) &&
		    (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
779
			xfs_debug(mp, "Returning error in dqget");
D
Dave Chinner 已提交
780
			return -EIO;
L
Linus Torvalds 已提交
781 782 783
		}
	}

784 785 786
	ASSERT(type == XFS_DQ_USER ||
	       type == XFS_DQ_PROJ ||
	       type == XFS_DQ_GROUP);
L
Linus Torvalds 已提交
787
	if (ip) {
C
Christoph Hellwig 已提交
788
		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
789
		ASSERT(xfs_inode_dquot(ip, type) == NULL);
L
Linus Torvalds 已提交
790 791
	}
#endif
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	/* Get the end of the quota file if we need it */
	if (flags & XFS_QMOPT_DQNEXT) {
		struct xfs_inode	*quotip;
		xfs_fileoff_t		last;
		uint			lock_mode;

		quotip = xfs_quota_inode(mp, type);
		lock_mode = xfs_ilock_data_map_shared(quotip);
		error = xfs_bmap_last_offset(quotip, &last, XFS_DATA_FORK);
		xfs_iunlock(quotip, lock_mode);
		if (error)
			return error;
		eof = XFS_FSB_TO_B(mp, last);
	}

808
restart:
809 810 811 812 813 814 815 816 817 818 819
	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 已提交
820

821 822 823 824 825 826 827 828 829 830 831 832
		/* uninit / unused quota found in radix tree, keep looking  */
		if (flags & XFS_QMOPT_DQNEXT) {
			if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
				xfs_dqunlock(dqp);
				mutex_unlock(&qi->qi_tree_lock);
				error = xfs_dq_get_next_id(mp, type, &id, eof);
				if (error)
					return error;
				goto restart;
			}
		}

833 834 835 836
		dqp->q_nrefs++;
		mutex_unlock(&qi->qi_tree_lock);

		trace_xfs_dqget_hit(dqp);
837
		XFS_STATS_INC(mp, xs_qm_dqcachehits);
838 839
		*O_dqpp = dqp;
		return 0;
L
Linus Torvalds 已提交
840
	}
841
	mutex_unlock(&qi->qi_tree_lock);
842
	XFS_STATS_INC(mp, xs_qm_dqcachemisses);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853

	/*
	 * 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 已提交
854
	error = xfs_qm_dqread(mp, id, type, flags, &dqp);
L
Linus Torvalds 已提交
855

C
Christoph Hellwig 已提交
856 857 858
	if (ip)
		xfs_ilock(ip, XFS_ILOCK_EXCL);

859 860 861 862 863 864 865
	/* If we are asked to find next active id, keep looking */
	if (error == -ENOENT && (flags & XFS_QMOPT_DQNEXT)) {
		error = xfs_dq_get_next_id(mp, type, &id, eof);
		if (!error)
			goto restart;
	}

C
Christoph Hellwig 已提交
866 867
	if (error)
		return error;
L
Linus Torvalds 已提交
868 869 870 871 872 873

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

877 878
			dqp1 = xfs_inode_dquot(ip, type);
			if (dqp1) {
L
Linus Torvalds 已提交
879
				xfs_qm_dqdestroy(dqp);
880
				dqp = dqp1;
L
Linus Torvalds 已提交
881 882 883 884
				xfs_dqlock(dqp);
				goto dqret;
			}
		} else {
885 886
			/* inode stays locked on return */
			xfs_qm_dqdestroy(dqp);
D
Dave Chinner 已提交
887
			return -ESRCH;
L
Linus Torvalds 已提交
888 889 890
		}
	}

891
	mutex_lock(&qi->qi_tree_lock);
D
Dave Chinner 已提交
892
	error = radix_tree_insert(tree, id, dqp);
893
	if (unlikely(error)) {
D
Dave Chinner 已提交
894
		WARN_ON(error != -EEXIST);
895

L
Linus Torvalds 已提交
896
		/*
897 898
		 * Duplicate found. Just throw away the new dquot and start
		 * over.
L
Linus Torvalds 已提交
899
		 */
900 901 902
		mutex_unlock(&qi->qi_tree_lock);
		trace_xfs_dqget_dup(dqp);
		xfs_qm_dqdestroy(dqp);
903
		XFS_STATS_INC(mp, xs_qm_dquot_dups);
904
		goto restart;
L
Linus Torvalds 已提交
905 906 907 908 909 910 911 912
	}

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

913 914 915
	qi->qi_dquots++;
	mutex_unlock(&qi->qi_tree_lock);

916 917 918 919 920 921 922 923 924 925 926
	/* If we are asked to find next active id, keep looking */
	if (flags & XFS_QMOPT_DQNEXT) {
		if (XFS_IS_DQUOT_UNINITIALIZED(dqp)) {
			xfs_qm_dqput(dqp);
			error = xfs_dq_get_next_id(mp, type, &id, eof);
			if (error)
				return error;
			goto restart;
		}
	}

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

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
/*
 * 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);

D
Dave Chinner 已提交
949 950 951 952 953
	if (--dqp->q_nrefs == 0) {
		struct xfs_quotainfo	*qi = dqp->q_mount->m_quotainfo;
		trace_xfs_dqput_free(dqp);

		if (list_lru_add(&qi->qi_lru, &dqp->q_lru))
954
			XFS_STATS_INC(dqp->q_mount, xs_qm_dquot_unused);
D
Dave Chinner 已提交
955 956
	}
	xfs_dqunlock(dqp);
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965 966
}

/*
 * 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 已提交
967 968 969
	if (!dqp)
		return;

C
Christoph Hellwig 已提交
970
	trace_xfs_dqrele(dqp);
L
Linus Torvalds 已提交
971 972 973 974 975 976 977 978 979 980 981

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

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
/*
 * 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)
1012
			xfs_trans_ail_delete(ailp, lip, SHUTDOWN_CORRUPT_INCORE);
1013 1014 1015 1016 1017 1018 1019 1020 1021
		else
			spin_unlock(&ailp->xa_lock);
	}

	/*
	 * Release the dq's flush lock since we're done with it.
	 */
	xfs_dqfunlock(dqp);
}
L
Linus Torvalds 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

/*
 * 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(
1033 1034
	struct xfs_dquot	*dqp,
	struct xfs_buf		**bpp)
L
Linus Torvalds 已提交
1035
{
1036 1037 1038
	struct xfs_mount	*mp = dqp->q_mount;
	struct xfs_buf		*bp;
	struct xfs_disk_dquot	*ddqp;
L
Linus Torvalds 已提交
1039 1040 1041
	int			error;

	ASSERT(XFS_DQ_IS_LOCKED(dqp));
1042
	ASSERT(!completion_done(&dqp->q_flush));
1043

C
Christoph Hellwig 已提交
1044
	trace_xfs_dqflush(dqp);
L
Linus Torvalds 已提交
1045

1046 1047
	*bpp = NULL;

L
Linus Torvalds 已提交
1048 1049 1050 1051 1052
	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
1053 1054 1055 1056
	 * 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 已提交
1057
	 */
1058
	if (XFS_FORCED_SHUTDOWN(mp)) {
1059
		struct xfs_log_item	*lip = &dqp->q_logitem.qli_item;
1060
		dqp->dq_flags &= ~XFS_DQ_DIRTY;
1061

1062 1063
		xfs_trans_ail_remove(lip, SHUTDOWN_CORRUPT_INCORE);

D
Dave Chinner 已提交
1064
		error = -EIO;
1065
		goto out_unlock;
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070
	}

	/*
	 * Get the buffer containing the on-disk dquot
	 */
1071
	error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dqp->q_blkno,
1072 1073
				   mp->m_quotainfo->qi_dqchunklen, 0, &bp,
				   &xfs_dquot_buf_ops);
1074 1075
	if (error)
		goto out_unlock;
L
Linus Torvalds 已提交
1076

1077 1078 1079
	/*
	 * Calculate the location of the dquot inside the buffer.
	 */
1080
	ddqp = bp->b_addr + dqp->q_bufoffset;
1081 1082 1083 1084

	/*
	 * A simple sanity check in case we got a corrupted dquot..
	 */
1085
	error = xfs_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
1086 1087
			   XFS_QMOPT_DOWARN, "dqflush (incore copy)");
	if (error) {
1088 1089 1090
		xfs_buf_relse(bp);
		xfs_dqfunlock(dqp);
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
D
Dave Chinner 已提交
1091
		return -EIO;
L
Linus Torvalds 已提交
1092 1093 1094
	}

	/* This is the only portion of data that needs to persist */
1095
	memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
L
Linus Torvalds 已提交
1096 1097 1098 1099

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

1102 1103
	xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
					&dqp->q_logitem.qli_item.li_lsn);
L
Linus Torvalds 已提交
1104

1105 1106 1107 1108
	/*
	 * 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 已提交
1109 1110 1111 1112
	 *
	 * 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.
1113 1114 1115 1116 1117
	 */
	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 已提交
1118 1119
		xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
				 XFS_DQUOT_CRC_OFF);
1120 1121
	}

L
Linus Torvalds 已提交
1122 1123 1124 1125
	/*
	 * 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.
	 */
1126 1127 1128
	xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
				  &dqp->q_logitem.qli_item);

L
Linus Torvalds 已提交
1129 1130 1131 1132
	/*
	 * If the buffer is pinned then push on the log so we won't
	 * get stuck waiting in the write for too long.
	 */
1133
	if (xfs_buf_ispinned(bp)) {
C
Christoph Hellwig 已提交
1134
		trace_xfs_dqflush_force(dqp);
1135
		xfs_log_force(mp, 0);
L
Linus Torvalds 已提交
1136 1137
	}

C
Christoph Hellwig 已提交
1138
	trace_xfs_dqflush_done(dqp);
1139 1140
	*bpp = bp;
	return 0;
C
Christoph Hellwig 已提交
1141

1142 1143
out_unlock:
	xfs_dqfunlock(dqp);
D
Dave Chinner 已提交
1144
	return -EIO;
L
Linus Torvalds 已提交
1145 1146
}

1147 1148 1149 1150 1151 1152
/*
 * Lock two xfs_dquot structures.
 *
 * To avoid deadlocks we always lock the quota structure with
 * the lowerd id first.
 */
L
Linus Torvalds 已提交
1153 1154 1155 1156 1157 1158 1159
void
xfs_dqlock2(
	xfs_dquot_t	*d1,
	xfs_dquot_t	*d2)
{
	if (d1 && d2) {
		ASSERT(d1 != d2);
1160 1161
		if (be32_to_cpu(d1->q_core.d_id) >
		    be32_to_cpu(d2->q_core.d_id)) {
1162 1163
			mutex_lock(&d2->q_qlock);
			mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1164
		} else {
1165 1166
			mutex_lock(&d1->q_qlock);
			mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1167
		}
1168 1169 1170 1171
	} else if (d1) {
		mutex_lock(&d1->q_qlock);
	} else if (d2) {
		mutex_lock(&d2->q_qlock);
L
Linus Torvalds 已提交
1172 1173 1174
	}
}

1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
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;
}

1196
void
1197 1198 1199 1200 1201
xfs_qm_exit(void)
{
	kmem_zone_destroy(xfs_qm_dqtrxzone);
	kmem_zone_destroy(xfs_qm_dqzone);
}