xfs_dquot.c 25.6 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
 * 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
static struct lock_class_key xfs_dquot_other_class;

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

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

76
	XFS_STATS_DEC(xs_qm_dquot);
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

/*
 * 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)
94
		d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
L
Linus Torvalds 已提交
95
	if (q->qi_bhardlimit && !d->d_blk_hardlimit)
96
		d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
L
Linus Torvalds 已提交
97
	if (q->qi_isoftlimit && !d->d_ino_softlimit)
98
		d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
L
Linus Torvalds 已提交
99
	if (q->qi_ihardlimit && !d->d_ino_hardlimit)
100
		d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
L
Linus Torvalds 已提交
101
	if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
102
		d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
L
Linus Torvalds 已提交
103
	if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
104
		d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115
}

/*
 * 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
116 117 118
 * '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 已提交
119 120 121 122 123 124 125 126
 */
void
xfs_qm_adjust_dqtimers(
	xfs_mount_t		*mp,
	xfs_disk_dquot_t	*d)
{
	ASSERT(d->d_id);

127
#ifdef DEBUG
128 129 130 131 132 133 134 135 136
	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 已提交
137
#endif
138

L
Linus Torvalds 已提交
139
	if (!d->d_btimer) {
140
		if ((d->d_blk_softlimit &&
141
		     (be64_to_cpu(d->d_bcount) >
142 143
		      be64_to_cpu(d->d_blk_softlimit))) ||
		    (d->d_blk_hardlimit &&
144
		     (be64_to_cpu(d->d_bcount) >
145 146
		      be64_to_cpu(d->d_blk_hardlimit)))) {
			d->d_btimer = cpu_to_be32(get_seconds() +
147
					mp->m_quotainfo->qi_btimelimit);
148 149
		} else {
			d->d_bwarns = 0;
L
Linus Torvalds 已提交
150 151 152
		}
	} else {
		if ((!d->d_blk_softlimit ||
153
		     (be64_to_cpu(d->d_bcount) <=
154
		      be64_to_cpu(d->d_blk_softlimit))) &&
L
Linus Torvalds 已提交
155
		    (!d->d_blk_hardlimit ||
156
		    (be64_to_cpu(d->d_bcount) <=
157
		     be64_to_cpu(d->d_blk_hardlimit)))) {
L
Linus Torvalds 已提交
158 159 160 161 162
			d->d_btimer = 0;
		}
	}

	if (!d->d_itimer) {
163
		if ((d->d_ino_softlimit &&
164
		     (be64_to_cpu(d->d_icount) >
165 166
		      be64_to_cpu(d->d_ino_softlimit))) ||
		    (d->d_ino_hardlimit &&
167
		     (be64_to_cpu(d->d_icount) >
168 169
		      be64_to_cpu(d->d_ino_hardlimit)))) {
			d->d_itimer = cpu_to_be32(get_seconds() +
170
					mp->m_quotainfo->qi_itimelimit);
171 172
		} else {
			d->d_iwarns = 0;
L
Linus Torvalds 已提交
173 174 175
		}
	} else {
		if ((!d->d_ino_softlimit ||
176
		     (be64_to_cpu(d->d_icount) <=
177
		      be64_to_cpu(d->d_ino_softlimit)))  &&
L
Linus Torvalds 已提交
178
		    (!d->d_ino_hardlimit ||
179
		     (be64_to_cpu(d->d_icount) <=
180
		      be64_to_cpu(d->d_ino_hardlimit)))) {
L
Linus Torvalds 已提交
181 182 183 184 185
			d->d_itimer = 0;
		}
	}

	if (!d->d_rtbtimer) {
186
		if ((d->d_rtb_softlimit &&
187
		     (be64_to_cpu(d->d_rtbcount) >
188 189
		      be64_to_cpu(d->d_rtb_softlimit))) ||
		    (d->d_rtb_hardlimit &&
190
		     (be64_to_cpu(d->d_rtbcount) >
191 192
		      be64_to_cpu(d->d_rtb_hardlimit)))) {
			d->d_rtbtimer = cpu_to_be32(get_seconds() +
193
					mp->m_quotainfo->qi_rtbtimelimit);
194 195
		} else {
			d->d_rtbwarns = 0;
L
Linus Torvalds 已提交
196 197 198
		}
	} else {
		if ((!d->d_rtb_softlimit ||
199
		     (be64_to_cpu(d->d_rtbcount) <=
200
		      be64_to_cpu(d->d_rtb_softlimit))) &&
L
Linus Torvalds 已提交
201
		    (!d->d_rtb_hardlimit ||
202
		     (be64_to_cpu(d->d_rtbcount) <=
203
		      be64_to_cpu(d->d_rtb_hardlimit)))) {
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
			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)
{
220
	struct xfs_quotainfo	*q = mp->m_quotainfo;
L
Linus Torvalds 已提交
221 222 223 224
	xfs_dqblk_t	*d;
	int		curid, i;

	ASSERT(tp);
225
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
226

227
	d = bp->b_addr;
L
Linus Torvalds 已提交
228 229 230 231

	/*
	 * ID of the first dquot in the block - id's are zero based.
	 */
232
	curid = id - (id % q->qi_dqperchunk);
L
Linus Torvalds 已提交
233
	ASSERT(curid >= 0);
234
	memset(d, 0, BBTOB(q->qi_dqchunklen));
235 236 237 238 239 240 241
	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;
	}

L
Linus Torvalds 已提交
242
	xfs_trans_dquot_buf(tp, bp,
243 244 245
			    (type & XFS_DQ_USER ? XFS_BLF_UDQUOT_BUF :
			    ((type & XFS_DQ_PROJ) ? XFS_BLF_PDQUOT_BUF :
			     XFS_BLF_GDQUOT_BUF)));
246
	xfs_trans_log_buf(tp, bp, 0, BBTOB(q->qi_dqchunklen) - 1);
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256
}



/*
 * Allocate a block and fill it with dquots.
 * This is called when the bmapi finds a hole.
 */
STATIC int
xfs_qm_dqalloc(
257
	xfs_trans_t	**tpp,
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268
	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;
269
	xfs_trans_t	*tp = *tpp;
L
Linus Torvalds 已提交
270 271

	ASSERT(tp != NULL);
C
Christoph Hellwig 已提交
272 273

	trace_xfs_dqalloc(dqp);
L
Linus Torvalds 已提交
274 275 276 277

	/*
	 * Initialize the bmap freelist prior to calling bmapi code.
	 */
278
	xfs_bmap_init(&flist, &firstblock);
L
Linus Torvalds 已提交
279 280 281 282 283
	xfs_ilock(quotip, XFS_ILOCK_EXCL);
	/*
	 * Return if this type of quotas is turned off while we didn't
	 * have an inode lock
	 */
284
	if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
L
Linus Torvalds 已提交
285 286 287 288
		xfs_iunlock(quotip, XFS_ILOCK_EXCL);
		return (ESRCH);
	}

289
	xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
L
Linus Torvalds 已提交
290
	nmaps = 1;
291 292 293 294 295
	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 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309
		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,
310
			       mp->m_quotainfo->qi_dqchunklen,
L
Linus Torvalds 已提交
311
			       0);
312 313 314

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

L
Linus Torvalds 已提交
317 318 319 320
	/*
	 * Make a chunk of dquots out of this buffer and log
	 * the entire thing.
	 */
321
	xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
322
			      dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
L
Linus Torvalds 已提交
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	/*
	 * 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);

339
	if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
L
Linus Torvalds 已提交
340 341 342
		goto error1;
	}

343 344 345 346 347 348 349
	if (committed) {
		tp = *tpp;
		xfs_trans_bjoin(tp, bp);
	} else {
		xfs_trans_bhold_release(tp, bp);
	}

L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	*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(
368
	xfs_trans_t		**tpp,
L
Linus Torvalds 已提交
369 370 371 372 373 374
	xfs_dquot_t		*dqp,
	xfs_disk_dquot_t	**O_ddpp,
	xfs_buf_t		**O_bpp,
	uint			flags)
{
	xfs_bmbt_irec_t map;
375
	int		nmaps = 1, error;
L
Linus Torvalds 已提交
376
	xfs_buf_t	*bp;
377 378
	xfs_inode_t	*quotip = XFS_DQ_TO_QIP(dqp);
	xfs_mount_t	*mp = dqp->q_mount;
L
Linus Torvalds 已提交
379
	xfs_disk_dquot_t *ddq;
380
	xfs_dqid_t	id = be32_to_cpu(dqp->q_core.d_id);
381
	xfs_trans_t	*tp = (tpp ? *tpp : NULL);
L
Linus Torvalds 已提交
382

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

385
	xfs_ilock(quotip, XFS_ILOCK_SHARED);
386
	if (!xfs_this_quota_on(dqp->q_mount, dqp->dq_flags)) {
L
Linus Torvalds 已提交
387
		/*
388 389
		 * Return if this type of quotas is turned off while we
		 * didn't have the quota inode lock.
L
Linus Torvalds 已提交
390
		 */
391 392 393 394 395 396 397
		xfs_iunlock(quotip, XFS_ILOCK_SHARED);
		return ESRCH;
	}

	/*
	 * Find the block map; no allocations yet
	 */
D
Dave Chinner 已提交
398 399
	error = xfs_bmapi_read(quotip, dqp->q_fileoffset,
			       XFS_DQUOT_CLUSTER_SIZE_FSB, &map, &nmaps, 0);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

	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 已提交
416
		/*
417
		 * We don't allocate unless we're asked to
L
Linus Torvalds 已提交
418
		 */
419 420
		if (!(flags & XFS_QMOPT_DQALLOC))
			return ENOENT;
L
Linus Torvalds 已提交
421

422 423 424
		ASSERT(tp);
		error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
					dqp->q_fileoffset, &bp);
L
Linus Torvalds 已提交
425
		if (error)
426 427 428 429
			return error;
		tp = *tpp;
	} else {
		trace_xfs_dqtobp_read(dqp);
L
Linus Torvalds 已提交
430 431

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

437 438 439 440
		error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
					   dqp->q_blkno,
					   mp->m_quotainfo->qi_dqchunklen,
					   0, &bp);
L
Linus Torvalds 已提交
441 442 443
		if (error || !bp)
			return XFS_ERROR(error);
	}
444

445
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
446 447 448 449

	/*
	 * calculate the location of the dquot inside the buffer.
	 */
450
	ddq = bp->b_addr + dqp->q_bufoffset;
L
Linus Torvalds 已提交
451 452 453 454

	/*
	 * A simple sanity check in case we got a corrupted dquot...
	 */
455
	error = xfs_qm_dqcheck(mp, ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
L
Linus Torvalds 已提交
456
			   flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
457 458
			   "dqtobp");
	if (error) {
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
		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.
 *
C
Christoph Hellwig 已提交
476
 * If XFS_QMOPT_DQALLOC is set, allocate a dquot on disk if it needed.
L
Linus Torvalds 已提交
477
 */
C
Christoph Hellwig 已提交
478
int
L
Linus Torvalds 已提交
479
xfs_qm_dqread(
C
Christoph Hellwig 已提交
480 481 482 483 484
	struct xfs_mount	*mp,
	xfs_dqid_t		id,
	uint			type,
	uint			flags,
	struct xfs_dquot	**O_dqpp)
L
Linus Torvalds 已提交
485
{
C
Christoph Hellwig 已提交
486 487 488 489 490 491
	struct xfs_dquot	*dqp;
	struct xfs_disk_dquot	*ddqp;
	struct xfs_buf		*bp;
	struct xfs_trans	*tp = NULL;
	int			error;
	int			cancelflags = 0;
492

493 494 495 496 497 498

	dqp = kmem_zone_zalloc(xfs_Gqm->qm_dqzone, KM_SLEEP);

	dqp->dq_flags = type;
	dqp->q_core.d_id = cpu_to_be32(id);
	dqp->q_mount = mp;
499
	INIT_LIST_HEAD(&dqp->q_lru);
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	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);

518
	XFS_STATS_INC(xs_qm_dquot);
L
Linus Torvalds 已提交
519

C
Christoph Hellwig 已提交
520 521
	trace_xfs_dqread(dqp);

C
Christoph Hellwig 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	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),
				XFS_WRITE_LOG_RES(mp) +
				/*
				 * Round the chunklen up to the next multiple
				 * of 128 (buf log item chunk size)).
				 */
				BBTOB(mp->m_quotainfo->qi_dqchunklen) - 1 + 128,
				0,
				XFS_TRANS_PERM_LOG_RES,
				XFS_WRITE_LOG_COUNT);
		if (error)
			goto error1;
		cancelflags = XFS_TRANS_RELEASE_LOG_RES;
	}

L
Linus Torvalds 已提交
539 540 541 542
	/*
	 * get a pointer to the on-disk dquot and the buffer containing it
	 * dqp already knows its own type (GROUP/USER).
	 */
C
Christoph Hellwig 已提交
543 544 545 546 547 548 549 550 551 552
	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 已提交
553 554 555 556 557 558 559 560
	}

	/* 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 已提交
561
	 * to avoid having to add every time.
L
Linus Torvalds 已提交
562
	 */
563 564 565
	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 已提交
566 567

	/* Mark the buf so that this will stay incore a little longer */
568
	xfs_buf_set_ref(bp, XFS_DQUOT_REF);
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581

	/*
	 * 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.
	 */
582
	ASSERT(xfs_buf_islocked(bp));
L
Linus Torvalds 已提交
583 584 585
	xfs_trans_brelse(tp, bp);

	if (tp) {
C
Christoph Hellwig 已提交
586 587 588
		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
		if (error)
			goto error0;
L
Linus Torvalds 已提交
589 590 591
	}

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

C
Christoph Hellwig 已提交
594
error1:
L
Linus Torvalds 已提交
595 596
	if (tp)
		xfs_trans_cancel(tp, cancelflags);
C
Christoph Hellwig 已提交
597
error0:
L
Linus Torvalds 已提交
598 599
	xfs_qm_dqdestroy(dqp);
	*O_dqpp = NULL;
C
Christoph Hellwig 已提交
600
	return error;
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614
}

/*
 * 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) */
615 616
	xfs_dqid_t	id,	  /* uid/projid/gid depending on type */
	uint		type,	  /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
L
Linus Torvalds 已提交
617 618 619
	uint		flags,	  /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
	xfs_dquot_t	**O_dqpp) /* OUT : locked incore dquot */
{
620 621 622 623
	struct xfs_quotainfo	*qi = mp->m_quotainfo;
	struct radix_tree_root *tree = XFS_DQUOT_TREE(qi, type);
	struct xfs_dquot	*dqp;
	int			error;
L
Linus Torvalds 已提交
624 625 626

	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
	if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
627
	    (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635
	    (! 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) {
636
			xfs_debug(mp, "Returning error in dqget");
L
Linus Torvalds 已提交
637 638 639 640
			return (EIO);
		}
	}

641 642 643
	ASSERT(type == XFS_DQ_USER ||
	       type == XFS_DQ_PROJ ||
	       type == XFS_DQ_GROUP);
L
Linus Torvalds 已提交
644
	if (ip) {
C
Christoph Hellwig 已提交
645
		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
646
		ASSERT(xfs_inode_dquot(ip, type) == NULL);
L
Linus Torvalds 已提交
647 648
	}
#endif
649 650

restart:
651 652 653 654 655 656 657 658 659 660 661
	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 已提交
662

663 664 665 666
		dqp->q_nrefs++;
		mutex_unlock(&qi->qi_tree_lock);

		trace_xfs_dqget_hit(dqp);
667
		XFS_STATS_INC(xs_qm_dqcachehits);
668 669
		*O_dqpp = dqp;
		return 0;
L
Linus Torvalds 已提交
670
	}
671 672
	mutex_unlock(&qi->qi_tree_lock);
	XFS_STATS_INC(xs_qm_dqcachemisses);
L
Linus Torvalds 已提交
673 674 675 676 677 678 679 680 681 682 683

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

C
Christoph Hellwig 已提交
686 687 688 689 690
	if (ip)
		xfs_ilock(ip, XFS_ILOCK_EXCL);

	if (error)
		return error;
L
Linus Torvalds 已提交
691 692 693 694 695 696

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

700 701
			dqp1 = xfs_inode_dquot(ip, type);
			if (dqp1) {
L
Linus Torvalds 已提交
702
				xfs_qm_dqdestroy(dqp);
703
				dqp = dqp1;
L
Linus Torvalds 已提交
704 705 706 707
				xfs_dqlock(dqp);
				goto dqret;
			}
		} else {
708 709 710
			/* inode stays locked on return */
			xfs_qm_dqdestroy(dqp);
			return XFS_ERROR(ESRCH);
L
Linus Torvalds 已提交
711 712 713
		}
	}

714 715 716 717 718
	mutex_lock(&qi->qi_tree_lock);
	error = -radix_tree_insert(tree, id, dqp);
	if (unlikely(error)) {
		WARN_ON(error != EEXIST);

L
Linus Torvalds 已提交
719
		/*
720 721
		 * Duplicate found. Just throw away the new dquot and start
		 * over.
L
Linus Torvalds 已提交
722
		 */
723 724 725 726 727
		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 已提交
728 729 730 731 732 733 734 735
	}

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

736 737 738
	qi->qi_dquots++;
	mutex_unlock(&qi->qi_tree_lock);

L
Linus Torvalds 已提交
739
 dqret:
C
Christoph Hellwig 已提交
740
	ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
C
Christoph Hellwig 已提交
741
	trace_xfs_dqget_miss(dqp);
L
Linus Torvalds 已提交
742 743 744 745 746
	*O_dqpp = dqp;
	return (0);
}


747 748
STATIC void
xfs_qm_dqput_final(
749
	struct xfs_dquot	*dqp)
L
Linus Torvalds 已提交
750
{
751
	struct xfs_quotainfo	*qi = dqp->q_mount->m_quotainfo;
752
	struct xfs_dquot	*gdqp;
L
Linus Torvalds 已提交
753

754 755
	trace_xfs_dqput_free(dqp);

756 757 758 759
	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++;
760
		XFS_STATS_INC(xs_qm_dquot_unused);
761
	}
762
	mutex_unlock(&qi->qi_lru_lock);
763

L
Linus Torvalds 已提交
764
	/*
765 766 767
	 * 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.
L
Linus Torvalds 已提交
768
	 */
769 770 771 772
	gdqp = dqp->q_gdquot;
	if (gdqp) {
		xfs_dqlock(gdqp);
		dqp->q_gdquot = NULL;
L
Linus Torvalds 已提交
773
	}
774
	xfs_dqunlock(dqp);
L
Linus Torvalds 已提交
775

776 777 778
	/*
	 * If we had a group quota hint, release it now.
	 */
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	if (gdqp)
		xfs_qm_dqput(gdqp);
}

/*
 * 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 已提交
802 803 804 805 806 807 808 809 810 811
}

/*
 * 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 已提交
812 813 814
	if (!dqp)
		return;

C
Christoph Hellwig 已提交
815
	trace_xfs_dqrele(dqp);
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826

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

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
/*
 * 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 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880

/*
 * 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)
{
881 882 883
	struct xfs_mount	*mp = dqp->q_mount;
	struct xfs_buf		*bp;
	struct xfs_disk_dquot	*ddqp;
L
Linus Torvalds 已提交
884 885 886
	int			error;

	ASSERT(XFS_DQ_IS_LOCKED(dqp));
887
	ASSERT(!completion_done(&dqp->q_flush));
888

C
Christoph Hellwig 已提交
889
	trace_xfs_dqflush(dqp);
L
Linus Torvalds 已提交
890 891

	/*
892
	 * If not dirty, or it's pinned and we are not supposed to block, nada.
L
Linus Torvalds 已提交
893
	 */
894
	if (!XFS_DQ_IS_DIRTY(dqp) ||
895
	    ((flags & SYNC_TRYLOCK) && atomic_read(&dqp->q_pincount) > 0)) {
L
Linus Torvalds 已提交
896
		xfs_dqfunlock(dqp);
897
		return 0;
L
Linus Torvalds 已提交
898 899 900 901 902 903 904 905
	}
	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!
	 */
906 907
	if (XFS_FORCED_SHUTDOWN(mp)) {
		dqp->dq_flags &= ~XFS_DQ_DIRTY;
L
Linus Torvalds 已提交
908 909 910 911 912 913 914
		xfs_dqfunlock(dqp);
		return XFS_ERROR(EIO);
	}

	/*
	 * Get the buffer containing the on-disk dquot
	 */
915 916 917
	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 已提交
918 919
		ASSERT(error != ENOENT);
		xfs_dqfunlock(dqp);
920
		return error;
L
Linus Torvalds 已提交
921 922
	}

923 924 925
	/*
	 * Calculate the location of the dquot inside the buffer.
	 */
926
	ddqp = bp->b_addr + dqp->q_bufoffset;
927 928 929 930

	/*
	 * A simple sanity check in case we got a corrupted dquot..
	 */
931 932 933
	error = xfs_qm_dqcheck(mp, &dqp->q_core, be32_to_cpu(ddqp->d_id), 0,
			   XFS_QMOPT_DOWARN, "dqflush (incore copy)");
	if (error) {
934 935 936
		xfs_buf_relse(bp);
		xfs_dqfunlock(dqp);
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
L
Linus Torvalds 已提交
937 938 939 940
		return XFS_ERROR(EIO);
	}

	/* This is the only portion of data that needs to persist */
941
	memcpy(ddqp, &dqp->q_core, sizeof(xfs_disk_dquot_t));
L
Linus Torvalds 已提交
942 943 944 945

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

948 949
	xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
					&dqp->q_logitem.qli_item.li_lsn);
L
Linus Torvalds 已提交
950 951 952 953 954

	/*
	 * 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.
	 */
955 956 957
	xfs_buf_attach_iodone(bp, xfs_qm_dqflush_done,
				  &dqp->q_logitem.qli_item);

L
Linus Torvalds 已提交
958 959 960 961
	/*
	 * If the buffer is pinned then push on the log so we won't
	 * get stuck waiting in the write for too long.
	 */
962
	if (xfs_buf_ispinned(bp)) {
C
Christoph Hellwig 已提交
963
		trace_xfs_dqflush_force(dqp);
964
		xfs_log_force(mp, 0);
L
Linus Torvalds 已提交
965 966
	}

967
	if (flags & SYNC_WAIT)
968 969
		error = xfs_bwrite(bp);
	else
970
		xfs_buf_delwri_queue(bp);
971 972

	xfs_buf_relse(bp);
C
Christoph Hellwig 已提交
973 974 975

	trace_xfs_dqflush_done(dqp);

L
Linus Torvalds 已提交
976 977 978
	/*
	 * dqp is still locked, but caller is free to unlock it now.
	 */
979
	return error;
L
Linus Torvalds 已提交
980 981 982

}

983 984 985 986 987 988
/*
 * Lock two xfs_dquot structures.
 *
 * To avoid deadlocks we always lock the quota structure with
 * the lowerd id first.
 */
L
Linus Torvalds 已提交
989 990 991 992 993 994 995
void
xfs_dqlock2(
	xfs_dquot_t	*d1,
	xfs_dquot_t	*d2)
{
	if (d1 && d2) {
		ASSERT(d1 != d2);
996 997
		if (be32_to_cpu(d1->q_core.d_id) >
		    be32_to_cpu(d2->q_core.d_id)) {
998 999
			mutex_lock(&d2->q_qlock);
			mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1000
		} else {
1001 1002
			mutex_lock(&d1->q_qlock);
			mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
L
Linus Torvalds 已提交
1003
		}
1004 1005 1006 1007
	} else if (d1) {
		mutex_lock(&d1->q_qlock);
	} else if (d2) {
		mutex_lock(&d2->q_qlock);
L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013 1014 1015
	}
}

/*
 * Give the buffer a little push if it is incore and
 * wait on the flush lock.
 */
void
1016
xfs_dqflock_pushbuf_wait(
L
Linus Torvalds 已提交
1017 1018
	xfs_dquot_t	*dqp)
{
1019
	xfs_mount_t	*mp = dqp->q_mount;
L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027
	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.
	 */
1028 1029
	bp = xfs_incore(mp->m_ddev_targp, dqp->q_blkno,
			mp->m_quotainfo->qi_dqchunklen, XBF_TRYLOCK);
1030 1031 1032 1033
	if (!bp)
		goto out_lock;

	if (XFS_BUF_ISDELAYWRITE(bp)) {
1034
		if (xfs_buf_ispinned(bp))
1035
			xfs_log_force(mp, 0);
1036 1037
		xfs_buf_delwri_promote(bp);
		wake_up_process(bp->b_target->bt_task);
L
Linus Torvalds 已提交
1038
	}
1039 1040
	xfs_buf_relse(bp);
out_lock:
L
Linus Torvalds 已提交
1041 1042
	xfs_dqflock(dqp);
}