xfs_qm.c 62.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2 3
 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
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.
Linus Torvalds's avatar
Linus Torvalds committed
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
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19
 */
#include "xfs.h"
#include "xfs_fs.h"
20
#include "xfs_bit.h"
Linus Torvalds's avatar
Linus Torvalds committed
21
#include "xfs_log.h"
22
#include "xfs_inum.h"
Linus Torvalds's avatar
Linus Torvalds committed
23 24 25 26 27 28 29 30 31
#include "xfs_trans.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "xfs_dir2.h"
#include "xfs_alloc.h"
#include "xfs_dmapi.h"
#include "xfs_quota.h"
#include "xfs_mount.h"
#include "xfs_bmap_btree.h"
32
#include "xfs_alloc_btree.h"
Linus Torvalds's avatar
Linus Torvalds committed
33 34
#include "xfs_ialloc_btree.h"
#include "xfs_dir2_sf.h"
35
#include "xfs_attr_sf.h"
Linus Torvalds's avatar
Linus Torvalds committed
36 37
#include "xfs_dinode.h"
#include "xfs_inode.h"
38 39 40
#include "xfs_btree.h"
#include "xfs_ialloc.h"
#include "xfs_itable.h"
Linus Torvalds's avatar
Linus Torvalds committed
41 42
#include "xfs_rtalloc.h"
#include "xfs_error.h"
43
#include "xfs_bmap.h"
Linus Torvalds's avatar
Linus Torvalds committed
44 45 46 47 48 49
#include "xfs_rw.h"
#include "xfs_attr.h"
#include "xfs_buf_item.h"
#include "xfs_trans_space.h"
#include "xfs_utils.h"
#include "xfs_qm.h"
Christoph Hellwig's avatar
Christoph Hellwig committed
50
#include "xfs_trace.h"
Linus Torvalds's avatar
Linus Torvalds committed
51 52 53 54 55 56 57

/*
 * The global quota manager. There is only one of these for the entire
 * system, _not_ one per file system. XQM keeps track of the overall
 * quota functionality, including maintaining the freelist and hash
 * tables of dquots.
 */
Christoph Hellwig's avatar
Christoph Hellwig committed
58
struct mutex	xfs_Gqm_lock;
Linus Torvalds's avatar
Linus Torvalds committed
59
struct xfs_qm	*xfs_Gqm;
60
uint		ndquot;
Linus Torvalds's avatar
Linus Torvalds committed
61 62 63 64

kmem_zone_t	*qm_dqzone;
kmem_zone_t	*qm_dqtrxzone;

65
static cred_t	xfs_zerocr;
66

Linus Torvalds's avatar
Linus Torvalds committed
67 68 69 70
STATIC void	xfs_qm_list_init(xfs_dqlist_t *, char *, int);
STATIC void	xfs_qm_list_destroy(xfs_dqlist_t *);

STATIC int	xfs_qm_init_quotainos(xfs_mount_t *);
71
STATIC int	xfs_qm_init_quotainfo(xfs_mount_t *);
72
STATIC int	xfs_qm_shake(struct shrinker *, int, gfp_t);
Linus Torvalds's avatar
Linus Torvalds committed
73

74 75 76 77 78
static struct shrinker xfs_qm_shaker = {
	.shrink = xfs_qm_shake,
	.seeks = DEFAULT_SEEKS,
};

Linus Torvalds's avatar
Linus Torvalds committed
79
#ifdef DEBUG
Christoph Hellwig's avatar
Christoph Hellwig committed
80
extern struct mutex	qcheck_lock;
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83
#endif

#ifdef QUOTADEBUG
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static void
xfs_qm_dquot_list_print(
	struct xfs_mount *mp)
{
	xfs_dquot_t	*dqp;
	int		i = 0;

	list_for_each_entry(dqp, &mp->m_quotainfo->qi_dqlist_lock, qi_mplist) {
		cmn_err(CE_DEBUG, "   %d. \"%d (%s)\"   "
				  "bcnt = %lld, icnt = %lld, refs = %d",
			i++, be32_to_cpu(dqp->q_core.d_id),
			DQFLAGTO_TYPESTR(dqp),
			(long long)be64_to_cpu(dqp->q_core.d_bcount),
			(long long)be64_to_cpu(dqp->q_core.d_icount),
			dqp->q_nrefs);
	}
Linus Torvalds's avatar
Linus Torvalds committed
100 101
}
#else
102
static void xfs_qm_dquot_list_print(struct xfs_mount *mp) { }
Linus Torvalds's avatar
Linus Torvalds committed
103 104 105 106 107 108 109 110 111
#endif

/*
 * Initialize the XQM structure.
 * Note that there is not one quota manager per file system.
 */
STATIC struct xfs_qm *
xfs_Gqm_init(void)
{
112 113
	xfs_dqhash_t	*udqhash, *gdqhash;
	xfs_qm_t	*xqm;
114 115
	size_t		hsize;
	uint		i;
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119

	/*
	 * Initialize the dquot hash tables.
	 */
120
	udqhash = kmem_zalloc_greedy(&hsize,
121
				     XFS_QM_HASHSIZE_LOW * sizeof(xfs_dqhash_t),
122 123 124 125 126
				     XFS_QM_HASHSIZE_HIGH * sizeof(xfs_dqhash_t));
	if (!udqhash)
		goto out;

	gdqhash = kmem_zalloc_large(hsize);
Julia Lawall's avatar
Julia Lawall committed
127
	if (!gdqhash)
128 129
		goto out_free_udqhash;

130
	hsize /= sizeof(xfs_dqhash_t);
131
	ndquot = hsize << 8;
Linus Torvalds's avatar
Linus Torvalds committed
132

133 134 135 136
	xqm = kmem_zalloc(sizeof(xfs_qm_t), KM_SLEEP);
	xqm->qm_dqhashmask = hsize - 1;
	xqm->qm_usr_dqhtable = udqhash;
	xqm->qm_grp_dqhtable = gdqhash;
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145 146 147
	ASSERT(xqm->qm_usr_dqhtable != NULL);
	ASSERT(xqm->qm_grp_dqhtable != NULL);

	for (i = 0; i < hsize; i++) {
		xfs_qm_list_init(&(xqm->qm_usr_dqhtable[i]), "uxdqh", i);
		xfs_qm_list_init(&(xqm->qm_grp_dqhtable[i]), "gxdqh", i);
	}

	/*
	 * Freelist of all dquots of all file systems
	 */
148 149 150
	INIT_LIST_HEAD(&xqm->qm_dqfrlist);
	xqm->qm_dqfrlist_cnt = 0;
	mutex_init(&xqm->qm_dqfrlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157 158 159 160 161

	/*
	 * dquot zone. we register our own low-memory callback.
	 */
	if (!qm_dqzone) {
		xqm->qm_dqzone = kmem_zone_init(sizeof(xfs_dquot_t),
						"xfs_dquots");
		qm_dqzone = xqm->qm_dqzone;
	} else
		xqm->qm_dqzone = qm_dqzone;

162
	register_shrinker(&xfs_qm_shaker);
Linus Torvalds's avatar
Linus Torvalds committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

	/*
	 * The t_dqinfo portion of transactions.
	 */
	if (!qm_dqtrxzone) {
		xqm->qm_dqtrxzone = kmem_zone_init(sizeof(xfs_dquot_acct_t),
						   "xfs_dqtrx");
		qm_dqtrxzone = xqm->qm_dqtrxzone;
	} else
		xqm->qm_dqtrxzone = qm_dqtrxzone;

	atomic_set(&xqm->qm_totaldquots, 0);
	xqm->qm_dqfree_ratio = XFS_QM_DQFREE_RATIO;
	xqm->qm_nrefs = 0;
#ifdef DEBUG
178
	mutex_init(&qcheck_lock);
Linus Torvalds's avatar
Linus Torvalds committed
179 180
#endif
	return xqm;
181 182 183 184 185

 out_free_udqhash:
	kmem_free_large(udqhash);
 out:
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
186 187 188 189 190
}

/*
 * Destroy the global quota manager when its reference count goes to zero.
 */
191
STATIC void
Linus Torvalds's avatar
Linus Torvalds committed
192 193 194
xfs_qm_destroy(
	struct xfs_qm	*xqm)
{
195
	struct xfs_dquot *dqp, *n;
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199
	int		hsize, i;

	ASSERT(xqm != NULL);
	ASSERT(xqm->qm_nrefs == 0);
200
	unregister_shrinker(&xfs_qm_shaker);
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205
	hsize = xqm->qm_dqhashmask + 1;
	for (i = 0; i < hsize; i++) {
		xfs_qm_list_destroy(&(xqm->qm_usr_dqhtable[i]));
		xfs_qm_list_destroy(&(xqm->qm_grp_dqhtable[i]));
	}
206 207
	kmem_free_large(xqm->qm_usr_dqhtable);
	kmem_free_large(xqm->qm_grp_dqhtable);
Linus Torvalds's avatar
Linus Torvalds committed
208 209 210
	xqm->qm_usr_dqhtable = NULL;
	xqm->qm_grp_dqhtable = NULL;
	xqm->qm_dqhashmask = 0;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

	/* frlist cleanup */
	mutex_lock(&xqm->qm_dqfrlist_lock);
	list_for_each_entry_safe(dqp, n, &xqm->qm_dqfrlist, q_freelist) {
		xfs_dqlock(dqp);
#ifdef QUOTADEBUG
		cmn_err(CE_DEBUG, "FREELIST destroy 0x%p", dqp);
#endif
		list_del_init(&dqp->q_freelist);
		xfs_Gqm->qm_dqfrlist_cnt--;
		xfs_dqunlock(dqp);
		xfs_qm_dqdestroy(dqp);
	}
	mutex_unlock(&xqm->qm_dqfrlist_lock);
	mutex_destroy(&xqm->qm_dqfrlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
226 227 228
#ifdef DEBUG
	mutex_destroy(&qcheck_lock);
#endif
229
	kmem_free(xqm);
Linus Torvalds's avatar
Linus Torvalds committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

/*
 * Called at mount time to let XQM know that another file system is
 * starting quotas. This isn't crucial information as the individual mount
 * structures are pretty independent, but it helps the XQM keep a
 * global view of what's going on.
 */
/* ARGSUSED */
STATIC int
xfs_qm_hold_quotafs_ref(
	struct xfs_mount *mp)
{
	/*
	 * Need to lock the xfs_Gqm structure for things like this. For example,
	 * the structure could disappear between the entry to this routine and
	 * a HOLD operation if not locked.
	 */
248
	mutex_lock(&xfs_Gqm_lock);
Linus Torvalds's avatar
Linus Torvalds committed
249

250
	if (!xfs_Gqm) {
Linus Torvalds's avatar
Linus Torvalds committed
251
		xfs_Gqm = xfs_Gqm_init();
252 253
		if (!xfs_Gqm) {
			mutex_unlock(&xfs_Gqm_lock);
254
			return ENOMEM;
255
		}
256 257
	}

Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261 262
	/*
	 * We can keep a list of all filesystems with quotas mounted for
	 * debugging and statistical purposes, but ...
	 * Just take a reference and get out.
	 */
263 264
	xfs_Gqm->qm_nrefs++;
	mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds's avatar
Linus Torvalds committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278

	return 0;
}


/*
 * Release the reference that a filesystem took at mount time,
 * so that we know when we need to destroy the entire quota manager.
 */
/* ARGSUSED */
STATIC void
xfs_qm_rele_quotafs_ref(
	struct xfs_mount *mp)
{
279
	xfs_dquot_t	*dqp, *n;
Linus Torvalds's avatar
Linus Torvalds committed
280 281 282 283 284 285 286

	ASSERT(xfs_Gqm);
	ASSERT(xfs_Gqm->qm_nrefs > 0);

	/*
	 * Go thru the freelist and destroy all inactive dquots.
	 */
287
	mutex_lock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
288

289
	list_for_each_entry_safe(dqp, n, &xfs_Gqm->qm_dqfrlist, q_freelist) {
Linus Torvalds's avatar
Linus Torvalds committed
290 291 292 293
		xfs_dqlock(dqp);
		if (dqp->dq_flags & XFS_DQ_INACTIVE) {
			ASSERT(dqp->q_mount == NULL);
			ASSERT(! XFS_DQ_IS_DIRTY(dqp));
294
			ASSERT(list_empty(&dqp->q_hashlist));
295
			ASSERT(list_empty(&dqp->q_mplist));
296 297
			list_del_init(&dqp->q_freelist);
			xfs_Gqm->qm_dqfrlist_cnt--;
Linus Torvalds's avatar
Linus Torvalds committed
298 299 300 301 302 303
			xfs_dqunlock(dqp);
			xfs_qm_dqdestroy(dqp);
		} else {
			xfs_dqunlock(dqp);
		}
	}
304
	mutex_unlock(&xfs_Gqm->qm_dqfrlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307 308 309

	/*
	 * Destroy the entire XQM. If somebody mounts with quotaon, this'll
	 * be restarted.
	 */
310 311
	mutex_lock(&xfs_Gqm_lock);
	if (--xfs_Gqm->qm_nrefs == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314
		xfs_qm_destroy(xfs_Gqm);
		xfs_Gqm = NULL;
	}
315
	mutex_unlock(&xfs_Gqm_lock);
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319 320 321
}

/*
 * Just destroy the quotainfo structure.
 */
void
Christoph Hellwig's avatar
Christoph Hellwig committed
322 323
xfs_qm_unmount(
	struct xfs_mount	*mp)
Linus Torvalds's avatar
Linus Torvalds committed
324
{
Christoph Hellwig's avatar
Christoph Hellwig committed
325
	if (mp->m_quotainfo) {
326
		xfs_qm_dqpurge_all(mp, XFS_QMOPT_QUOTALL);
Linus Torvalds's avatar
Linus Torvalds committed
327
		xfs_qm_destroy_quotainfo(mp);
Christoph Hellwig's avatar
Christoph Hellwig committed
328
	}
Linus Torvalds's avatar
Linus Torvalds committed
329 330 331 332 333 334 335 336
}


/*
 * This is called from xfs_mountfs to start quotas and initialize all
 * necessary data structures like quotainfo.  This is also responsible for
 * running a quotacheck as necessary.  We are guaranteed that the superblock
 * is consistently read in at this point.
337 338 339
 *
 * If we fail here, the mount will continue with quota turned off. We don't
 * need to inidicate success or failure at all.
Linus Torvalds's avatar
Linus Torvalds committed
340
 */
341
void
Linus Torvalds's avatar
Linus Torvalds committed
342
xfs_qm_mount_quotas(
Christoph Hellwig's avatar
Christoph Hellwig committed
343
	xfs_mount_t	*mp)
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
	int		error = 0;
	uint		sbf;

	/*
	 * If quotas on realtime volumes is not supported, we disable
	 * quotas immediately.
	 */
	if (mp->m_sb.sb_rextents) {
		cmn_err(CE_NOTE,
			"Cannot turn on quotas for realtime filesystem %s",
			mp->m_fsname);
		mp->m_qflags = 0;
		goto write_changes;
	}

	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
361

Linus Torvalds's avatar
Linus Torvalds committed
362 363 364 365
	/*
	 * Allocate the quotainfo structure inside the mount struct, and
	 * create quotainode(s), and change/rev superblock if necessary.
	 */
366 367
	error = xfs_qm_init_quotainfo(mp);
	if (error) {
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370 371 372 373 374 375 376 377
		/*
		 * We must turn off quotas.
		 */
		ASSERT(mp->m_quotainfo == NULL);
		mp->m_qflags = 0;
		goto write_changes;
	}
	/*
	 * If any of the quotas are not consistent, do a quotacheck.
	 */
Christoph Hellwig's avatar
Christoph Hellwig committed
378
	if (XFS_QM_NEED_QUOTACHECK(mp)) {
379 380 381 382
		error = xfs_qm_quotacheck(mp);
		if (error) {
			/* Quotacheck failed and disabled quotas. */
			return;
Linus Torvalds's avatar
Linus Torvalds committed
383 384
		}
	}
385 386 387 388 389
	/* 
	 * If one type of quotas is off, then it will lose its
	 * quotachecked status, since we won't be doing accounting for
	 * that type anymore.
	 */
390
	if (!XFS_IS_UQUOTA_ON(mp))
391
		mp->m_qflags &= ~XFS_UQUOTA_CHKD;
392
	if (!(XFS_IS_GQUOTA_ON(mp) || XFS_IS_PQUOTA_ON(mp)))
393
		mp->m_qflags &= ~XFS_OQUOTA_CHKD;
394

Linus Torvalds's avatar
Linus Torvalds committed
395 396
 write_changes:
	/*
Eric Sandeen's avatar
Eric Sandeen committed
397
	 * We actually don't have to acquire the m_sb_lock at all.
Linus Torvalds's avatar
Linus Torvalds committed
398 399
	 * This can only be called from mount, and that's single threaded. XXX
	 */
Eric Sandeen's avatar
Eric Sandeen committed
400
	spin_lock(&mp->m_sb_lock);
Linus Torvalds's avatar
Linus Torvalds committed
401 402
	sbf = mp->m_sb.sb_qflags;
	mp->m_sb.sb_qflags = mp->m_qflags & XFS_MOUNT_QUOTA_ALL;
Eric Sandeen's avatar
Eric Sandeen committed
403
	spin_unlock(&mp->m_sb_lock);
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

	if (sbf != (mp->m_qflags & XFS_MOUNT_QUOTA_ALL)) {
		if (xfs_qm_write_sb_changes(mp, XFS_SB_QFLAGS)) {
			/*
			 * We could only have been turning quotas off.
			 * We aren't in very good shape actually because
			 * the incore structures are convinced that quotas are
			 * off, but the on disk superblock doesn't know that !
			 */
			ASSERT(!(XFS_IS_QUOTA_RUNNING(mp)));
			xfs_fs_cmn_err(CE_ALERT, mp,
				"XFS mount_quotas: Superblock update failed!");
		}
	}

	if (error) {
		xfs_fs_cmn_err(CE_WARN, mp,
			"Failed to initialize disk quotas.");
Christoph Hellwig's avatar
Christoph Hellwig committed
422
		return;
Linus Torvalds's avatar
Linus Torvalds committed
423
	}
Christoph Hellwig's avatar
Christoph Hellwig committed
424 425 426 427 428

#ifdef QUOTADEBUG
	if (XFS_IS_QUOTA_ON(mp))
		xfs_qm_internalqcheck(mp);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431 432 433
}

/*
 * Called from the vfsops layer.
 */
434
void
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
xfs_qm_unmount_quotas(
	xfs_mount_t	*mp)
{
	/*
	 * Release the dquots that root inode, et al might be holding,
	 * before we flush quotas and blow away the quotainfo structure.
	 */
	ASSERT(mp->m_rootip);
	xfs_qm_dqdetach(mp->m_rootip);
	if (mp->m_rbmip)
		xfs_qm_dqdetach(mp->m_rbmip);
	if (mp->m_rsumip)
		xfs_qm_dqdetach(mp->m_rsumip);

	/*
450
	 * Release the quota inodes.
Linus Torvalds's avatar
Linus Torvalds committed
451 452
	 */
	if (mp->m_quotainfo) {
453 454 455
		if (mp->m_quotainfo->qi_uquotaip) {
			IRELE(mp->m_quotainfo->qi_uquotaip);
			mp->m_quotainfo->qi_uquotaip = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
456
		}
457 458 459
		if (mp->m_quotainfo->qi_gquotaip) {
			IRELE(mp->m_quotainfo->qi_gquotaip);
			mp->m_quotainfo->qi_gquotaip = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464 465 466 467
		}
	}
}

/*
 * Flush all dquots of the given file system to disk. The dquots are
 * _not_ purged from memory here, just their data written to disk.
 */
468
STATIC int
Linus Torvalds's avatar
Linus Torvalds committed
469
xfs_qm_dqflush_all(
470 471
	struct xfs_mount	*mp,
	int			sync_mode)
Linus Torvalds's avatar
Linus Torvalds committed
472
{
473 474 475 476 477
	struct xfs_quotainfo	*q = mp->m_quotainfo;
	int			recl;
	struct xfs_dquot	*dqp;
	int			niters;
	int			error;
Linus Torvalds's avatar
Linus Torvalds committed
478

479
	if (!q)
480
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
481 482
	niters = 0;
again:
483 484
	mutex_lock(&q->qi_dqlist_lock);
	list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489
		xfs_dqlock(dqp);
		if (! XFS_DQ_IS_DIRTY(dqp)) {
			xfs_dqunlock(dqp);
			continue;
		}
Christoph Hellwig's avatar
Christoph Hellwig committed
490

Linus Torvalds's avatar
Linus Torvalds committed
491
		/* XXX a sentinel would be better */
492
		recl = q->qi_dqreclaims;
493
		if (!xfs_dqflock_nowait(dqp)) {
Linus Torvalds's avatar
Linus Torvalds committed
494 495 496 497 498 499 500 501 502 503 504 505 506
			/*
			 * If we can't grab the flush lock then 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.
			 */
			xfs_qm_dqflock_pushbuf_wait(dqp);
		}
		/*
		 * Let go of the mplist lock. We don't want to hold it
		 * across a disk write.
		 */
507
		mutex_unlock(&q->qi_dqlist_lock);
508
		error = xfs_qm_dqflush(dqp, sync_mode);
Linus Torvalds's avatar
Linus Torvalds committed
509 510
		xfs_dqunlock(dqp);
		if (error)
511
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
512

513 514 515
		mutex_lock(&q->qi_dqlist_lock);
		if (recl != q->qi_dqreclaims) {
			mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
516 517 518 519 520
			/* XXX restart limit */
			goto again;
		}
	}

521
	mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
522
	/* return ! busy */
523
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527 528 529 530
}
/*
 * Release the group dquot pointers the user dquots may be
 * carrying around as a hint. mplist is locked on entry and exit.
 */
STATIC void
xfs_qm_detach_gdquots(
531
	struct xfs_mount	*mp)
Linus Torvalds's avatar
Linus Torvalds committed
532
{
533 534 535
	struct xfs_quotainfo	*q = mp->m_quotainfo;
	struct xfs_dquot	*dqp, *gdqp;
	int			nrecl;
Linus Torvalds's avatar
Linus Torvalds committed
536 537

 again:
538 539
	ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
	list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544 545 546 547 548 549 550 551
		xfs_dqlock(dqp);
		if ((gdqp = dqp->q_gdquot)) {
			xfs_dqlock(gdqp);
			dqp->q_gdquot = NULL;
		}
		xfs_dqunlock(dqp);

		if (gdqp) {
			/*
			 * Can't hold the mplist lock across a dqput.
			 * XXXmust convert to marker based iterations here.
			 */
552 553
			nrecl = q->qi_dqreclaims;
			mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
554 555
			xfs_qm_dqput(gdqp);

556 557
			mutex_lock(&q->qi_dqlist_lock);
			if (nrecl != q->qi_dqreclaims)
Linus Torvalds's avatar
Linus Torvalds committed
558 559 560 561 562 563 564 565 566 567 568 569 570
				goto again;
		}
	}
}

/*
 * Go through all the incore dquots of this file system and take them
 * off the mplist and hashlist, if the dquot type matches the dqtype
 * parameter. This is used when turning off quota accounting for
 * users and/or groups, as well as when the filesystem is unmounting.
 */
STATIC int
xfs_qm_dqpurge_int(
571 572
	struct xfs_mount	*mp,
	uint			flags)
Linus Torvalds's avatar
Linus Torvalds committed
573
{
574 575 576 577 578
	struct xfs_quotainfo	*q = mp->m_quotainfo;
	struct xfs_dquot	*dqp, *n;
	uint			dqtype;
	int			nrecl;
	int			nmisses;
Linus Torvalds's avatar
Linus Torvalds committed
579

580
	if (!q)
581
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
582 583

	dqtype = (flags & XFS_QMOPT_UQUOTA) ? XFS_DQ_USER : 0;
584
	dqtype |= (flags & XFS_QMOPT_PQUOTA) ? XFS_DQ_PROJ : 0;
Linus Torvalds's avatar
Linus Torvalds committed
585 586
	dqtype |= (flags & XFS_QMOPT_GQUOTA) ? XFS_DQ_GROUP : 0;

587
	mutex_lock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
588 589 590 591 592 593 594 595 596 597 598

	/*
	 * In the first pass through all incore dquots of this filesystem,
	 * we release the group dquot pointers the user dquots may be
	 * carrying around as a hint. We need to do this irrespective of
	 * what's being turned off.
	 */
	xfs_qm_detach_gdquots(mp);

      again:
	nmisses = 0;
599
	ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
Linus Torvalds's avatar
Linus Torvalds committed
600 601 602 603
	/*
	 * Try to get rid of all of the unwanted dquots. The idea is to
	 * get them off mplist and hashlist, but leave them on freelist.
	 */
604
	list_for_each_entry_safe(dqp, n, &q->qi_dqlist, q_mplist) {
Linus Torvalds's avatar
Linus Torvalds committed
605 606 607 608 609
		/*
		 * It's OK to look at the type without taking dqlock here.
		 * We're holding the mplist lock here, and that's needed for
		 * a dqreclaim.
		 */
610
		if ((dqp->dq_flags & dqtype) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
611 612
			continue;

613
		if (!mutex_trylock(&dqp->q_hash->qh_lock)) {
614 615
			nrecl = q->qi_dqreclaims;
			mutex_unlock(&q->qi_dqlist_lock);
616
			mutex_lock(&dqp->q_hash->qh_lock);
617
			mutex_lock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
618 619 620 621 622 623 624

			/*
			 * XXXTheoretically, we can get into a very long
			 * ping pong game here.
			 * No one can be adding dquots to the mplist at
			 * this point, but somebody might be taking things off.
			 */
625
			if (nrecl != q->qi_dqreclaims) {
626
				mutex_unlock(&dqp->q_hash->qh_lock);
Linus Torvalds's avatar
Linus Torvalds committed
627 628 629 630 631 632 633 634
				goto again;
			}
		}

		/*
		 * Take the dquot off the mplist and hashlist. It may remain on
		 * freelist in INACTIVE state.
		 */
635
		nmisses += xfs_qm_dqpurge(dqp);
Linus Torvalds's avatar
Linus Torvalds committed
636
	}
637
	mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
	return nmisses;
}

int
xfs_qm_dqpurge_all(
	xfs_mount_t	*mp,
	uint		flags)
{
	int		ndquots;

	/*
	 * Purge the dquot cache.
	 * None of the dquots should really be busy at this point.
	 */
	if (mp->m_quotainfo) {
		while ((ndquots = xfs_qm_dqpurge_int(mp, flags))) {
			delay(ndquots * 10);
		}
	}
	return 0;
}

STATIC int
xfs_qm_dqattach_one(
	xfs_inode_t	*ip,
	xfs_dqid_t	id,
	uint		type,
	uint		doalloc,
	xfs_dquot_t	*udqhint, /* hint */
	xfs_dquot_t	**IO_idqpp)
{
	xfs_dquot_t	*dqp;
	int		error;

Christoph Hellwig's avatar
Christoph Hellwig committed
672
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds's avatar
Linus Torvalds committed
673
	error = 0;
674

Linus Torvalds's avatar
Linus Torvalds committed
675 676 677 678 679
	/*
	 * See if we already have it in the inode itself. IO_idqpp is
	 * &i_udquot or &i_gdquot. This made the code look weird, but
	 * made the logic a lot simpler.
	 */
680 681
	dqp = *IO_idqpp;
	if (dqp) {
Christoph Hellwig's avatar
Christoph Hellwig committed
682
		trace_xfs_dqattach_found(dqp);
683
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687
	}

	/*
	 * udqhint is the i_udquot field in inode, and is non-NULL only
688
	 * when the type arg is group/project. Its purpose is to save a
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691
	 * lookup by dqid (xfs_qm_dqget) by caching a group dquot inside
	 * the user dquot.
	 */
692 693
	if (udqhint) {
		ASSERT(type == XFS_DQ_GROUP || type == XFS_DQ_PROJ);
Linus Torvalds's avatar
Linus Torvalds committed
694 695
		xfs_dqlock(udqhint);

696 697 698 699 700 701 702 703 704 705 706 707 708 709
		/*
		 * No need to take dqlock to look at the id.
		 *
		 * The ID can't change until it gets reclaimed, and it won't
		 * be reclaimed as long as we have a ref from inode and we
		 * hold the ilock.
		 */
		dqp = udqhint->q_gdquot;
		if (dqp && be32_to_cpu(dqp->q_core.d_id) == id) {
			xfs_dqlock(dqp);
			XFS_DQHOLD(dqp);
			ASSERT(*IO_idqpp == NULL);
			*IO_idqpp = dqp;

Linus Torvalds's avatar
Linus Torvalds committed
710 711
			xfs_dqunlock(dqp);
			xfs_dqunlock(udqhint);
712
			return 0;
Linus Torvalds's avatar
Linus Torvalds committed
713
		}
714 715 716 717 718 719 720

		/*
		 * We can't hold a dquot lock when we call the dqget code.
		 * We'll deadlock in no time, because of (not conforming to)
		 * lock ordering - the inodelock comes before any dquot lock,
		 * and we may drop and reacquire the ilock in xfs_qm_dqget().
		 */
Linus Torvalds's avatar
Linus Torvalds committed
721
		xfs_dqunlock(udqhint);
722 723
	}

Linus Torvalds's avatar
Linus Torvalds committed
724 725 726 727 728 729 730
	/*
	 * Find the dquot from somewhere. This bumps the
	 * reference count of dquot and returns it locked.
	 * This can return ENOENT if dquot didn't exist on
	 * disk and we didn't ask it to allocate;
	 * ESRCH if quotas got turned off suddenly.
	 */
731 732 733
	error = xfs_qm_dqget(ip->i_mount, ip, id, type, XFS_QMOPT_DOWARN, &dqp);
	if (error)
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
734

Christoph Hellwig's avatar
Christoph Hellwig committed
735
	trace_xfs_dqattach_get(dqp);
736

Linus Torvalds's avatar
Linus Torvalds committed
737 738 739 740 741
	/*
	 * dqget may have dropped and re-acquired the ilock, but it guarantees
	 * that the dquot returned is the one that should go in the inode.
	 */
	*IO_idqpp = dqp;
742 743
	xfs_dqunlock(dqp);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749 750
}


/*
 * Given a udquot and gdquot, attach a ptr to the group dquot in the
 * udquot as a hint for future lookups. The idea sounds simple, but the
 * execution isn't, because the udquot might have a group dquot attached
751
 * already and getting rid of that gets us into lock ordering constraints.
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754 755 756 757
 * The process is complicated more by the fact that the dquots may or may not
 * be locked on entry.
 */
STATIC void
xfs_qm_dqattach_grouphint(
	xfs_dquot_t	*udq,
758
	xfs_dquot_t	*gdq)
Linus Torvalds's avatar
Linus Torvalds committed
759 760 761
{
	xfs_dquot_t	*tmp;

762
	xfs_dqlock(udq);
Linus Torvalds's avatar
Linus Torvalds committed
763 764 765

	if ((tmp = udq->q_gdquot)) {
		if (tmp == gdq) {
766
			xfs_dqunlock(udq);
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
			return;
		}

		udq->q_gdquot = NULL;
		/*
		 * We can't keep any dqlocks when calling dqrele,
		 * because the freelist lock comes before dqlocks.
		 */
		xfs_dqunlock(udq);
		/*
		 * we took a hard reference once upon a time in dqget,
		 * so give it back when the udquot no longer points at it
		 * dqput() does the unlocking of the dquot.
		 */
		xfs_qm_dqrele(tmp);

		xfs_dqlock(udq);
		xfs_dqlock(gdq);

	} else {
		ASSERT(XFS_DQ_IS_LOCKED(udq));
788
		xfs_dqlock(gdq);
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791 792 793 794 795 796 797 798 799 800
	}

	ASSERT(XFS_DQ_IS_LOCKED(udq));
	ASSERT(XFS_DQ_IS_LOCKED(gdq));
	/*
	 * Somebody could have attached a gdquot here,
	 * when we dropped the uqlock. If so, just do nothing.
	 */
	if (udq->q_gdquot == NULL) {
		XFS_DQHOLD(gdq);
		udq->q_gdquot = gdq;
	}
801 802 803

	xfs_dqunlock(gdq);
	xfs_dqunlock(udq);
Linus Torvalds's avatar
Linus Torvalds committed
804 805 806 807
}


/*
808 809
 * Given a locked inode, attach dquot(s) to it, taking U/G/P-QUOTAON
 * into account.
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813 814
 * If XFS_QMOPT_DQALLOC, the dquot(s) will be allocated if needed.
 * Inode may get unlocked and relocked in here, and the caller must deal with
 * the consequences.
 */
int
Christoph Hellwig's avatar
Christoph Hellwig committed
815
xfs_qm_dqattach_locked(
Linus Torvalds's avatar
Linus Torvalds committed
816 817 818 819 820 821 822
	xfs_inode_t	*ip,
	uint		flags)
{
	xfs_mount_t	*mp = ip->i_mount;
	uint		nquotas = 0;
	int		error = 0;

Christoph Hellwig's avatar
Christoph Hellwig committed
823 824 825 826 827
	if (!XFS_IS_QUOTA_RUNNING(mp) ||
	    !XFS_IS_QUOTA_ON(mp) ||
	    !XFS_NOT_DQATTACHED(mp, ip) ||
	    ip->i_ino == mp->m_sb.sb_uquotino ||
	    ip->i_ino == mp->m_sb.sb_gquotino)
828
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
829

Christoph Hellwig's avatar
Christoph Hellwig committed
830
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835 836 837 838 839

	if (XFS_IS_UQUOTA_ON(mp)) {
		error = xfs_qm_dqattach_one(ip, ip->i_d.di_uid, XFS_DQ_USER,
						flags & XFS_QMOPT_DQALLOC,
						NULL, &ip->i_udquot);
		if (error)
			goto done;
		nquotas++;
	}
Christoph Hellwig's avatar
Christoph Hellwig committed
840 841

	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
842 843 844 845 846 847
	if (XFS_IS_OQUOTA_ON(mp)) {
		error = XFS_IS_GQUOTA_ON(mp) ?
			xfs_qm_dqattach_one(ip, ip->i_d.di_gid, XFS_DQ_GROUP,
						flags & XFS_QMOPT_DQALLOC,
						ip->i_udquot, &ip->i_gdquot) :
			xfs_qm_dqattach_one(ip, ip->i_d.di_projid, XFS_DQ_PROJ,
Linus Torvalds's avatar
Linus Torvalds committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
						flags & XFS_QMOPT_DQALLOC,
						ip->i_udquot, &ip->i_gdquot);
		/*
		 * Don't worry about the udquot that we may have
		 * attached above. It'll get detached, if not already.
		 */
		if (error)
			goto done;
		nquotas++;
	}

	/*
	 * Attach this group quota to the user quota as a hint.
	 * This WON'T, in general, result in a thrash.
	 */
	if (nquotas == 2) {
Christoph Hellwig's avatar
Christoph Hellwig committed
864
		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds's avatar
Linus Torvalds committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878
		ASSERT(ip->i_udquot);
		ASSERT(ip->i_gdquot);

		/*
		 * We may or may not have the i_udquot locked at this point,
		 * but this check is OK since we don't depend on the i_gdquot to
		 * be accurate 100% all the time. It is just a hint, and this
		 * will succeed in general.
		 */
		if (ip->i_udquot->q_gdquot == ip->i_gdquot)
			goto done;
		/*
		 * Attach i_gdquot to the gdquot hint inside the i_udquot.
		 */
879
		xfs_qm_dqattach_grouphint(ip->i_udquot, ip->i_gdquot);
Linus Torvalds's avatar
Linus Torvalds committed
880 881
	}

Christoph Hellwig's avatar
Christoph Hellwig committed
882
 done:
Linus Torvalds's avatar
Linus Torvalds committed
883 884 885 886
#ifdef QUOTADEBUG
	if (! error) {
		if (XFS_IS_UQUOTA_ON(mp))
			ASSERT(ip->i_udquot);
887
		if (XFS_IS_OQUOTA_ON(mp))
Linus Torvalds's avatar
Linus Torvalds committed
888 889
			ASSERT(ip->i_gdquot);
	}
Christoph Hellwig's avatar
Christoph Hellwig committed
890
	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds's avatar
Linus Torvalds committed
891
#endif
Christoph Hellwig's avatar
Christoph Hellwig committed
892 893
	return error;
}
Linus Torvalds's avatar
Linus Torvalds committed
894

Christoph Hellwig's avatar
Christoph Hellwig committed
895 896 897 898 899 900 901 902 903 904
int
xfs_qm_dqattach(
	struct xfs_inode	*ip,
	uint			flags)
{
	int			error;

	xfs_ilock(ip, XFS_ILOCK_EXCL);
	error = xfs_qm_dqattach_locked(ip, flags);
	xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds's avatar
Linus Torvalds committed
905

906
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920
}

/*
 * Release dquots (and their references) if any.
 * The inode should be locked EXCL except when this's called by
 * xfs_ireclaim.
 */
void
xfs_qm_dqdetach(
	xfs_inode_t	*ip)
{
	if (!(ip->i_udquot || ip->i_gdquot))
		return;

Christoph Hellwig's avatar
Christoph Hellwig committed
921 922
	trace_xfs_dquot_dqdetach(ip);

Linus Torvalds's avatar
Linus Torvalds committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936
	ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_uquotino);
	ASSERT(ip->i_ino != ip->i_mount->m_sb.sb_gquotino);
	if (ip->i_udquot) {
		xfs_qm_dqrele(ip->i_udquot);
		ip->i_udquot = NULL;
	}
	if (ip->i_gdquot) {
		xfs_qm_dqrele(ip->i_gdquot);
		ip->i_gdquot = NULL;
	}
}

int
xfs_qm_sync(
937 938
	struct xfs_mount	*mp,
	int			flags)
Linus Torvalds's avatar
Linus Torvalds committed
939
{
940 941 942 943
	struct xfs_quotainfo	*q = mp->m_quotainfo;
	int			recl, restarts;
	struct xfs_dquot	*dqp;
	int			error;
Linus Torvalds's avatar
Linus Torvalds committed
944

Christoph Hellwig's avatar
Christoph Hellwig committed
945
	if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
946 947
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
948 949 950
	restarts = 0;

  again:
951
	mutex_lock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956 957
	/*
	 * dqpurge_all() also takes the mplist lock and iterate thru all dquots
	 * in quotaoff. However, if the QUOTA_ACTIVE bits are not cleared
	 * when we have the mplist lock, we know that dquots will be consistent
	 * as long as we have it locked.
	 */
958
	if (!XFS_IS_QUOTA_ON(mp)) {
959
		mutex_unlock(&q->qi_dqlist_lock);
960
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
961
	}
962 963
	ASSERT(mutex_is_locked(&q->qi_dqlist_lock));
	list_for_each_entry(dqp, &q->qi_dqlist, q_mplist) {
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966 967 968
		/*
		 * If this is vfs_sync calling, then skip the dquots that
		 * don't 'seem' to be dirty. ie. don't acquire dqlock.
		 * This is very similar to what xfs_sync does with inodes.
		 */
Christoph Hellwig's avatar
Christoph Hellwig committed
969 970
		if (flags & SYNC_TRYLOCK) {
			if (!XFS_DQ_IS_DIRTY(dqp))
Linus Torvalds's avatar
Linus Torvalds committed
971
				continue;
Christoph Hellwig's avatar
Christoph Hellwig committed
972
			if (!xfs_qm_dqlock_nowait(dqp))
Linus Torvalds's avatar
Linus Torvalds committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986
				continue;
		} else {
			xfs_dqlock(dqp);
		}

		/*
		 * Now, find out for sure if this dquot is dirty or not.
		 */
		if (! XFS_DQ_IS_DIRTY(dqp)) {
			xfs_dqunlock(dqp);
			continue;
		}

		/* XXX a sentinel would be better */
987
		recl = q->qi_dqreclaims;
988
		if (!xfs_dqflock_nowait(dqp)) {
Christoph Hellwig's avatar
Christoph Hellwig committed
989
			if (flags & SYNC_TRYLOCK) {
Linus Torvalds's avatar
Linus Torvalds committed
990 991 992 993 994
				xfs_dqunlock(dqp);
				continue;
			}
			/*
			 * If we can't grab the flush lock then if the caller
995
			 * really wanted us to give this our best shot, so
Linus Torvalds's avatar
Linus Torvalds committed
996 997
			 * see if we can give a push to the buffer before we wait
			 * on the flush lock. At this point, we know that
998
			 * even though the dquot is being flushed,
Linus Torvalds's avatar
Linus Torvalds committed
999 1000 1001 1002 1003 1004 1005 1006
			 * it has (new) dirty data.
			 */
			xfs_qm_dqflock_pushbuf_wait(dqp);
		}
		/*
		 * Let go of the mplist lock. We don't want to hold it
		 * across a disk write
		 */
1007
		mutex_unlock(&q->qi_dqlist_lock);
1008
		error = xfs_qm_dqflush(dqp, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010
		xfs_dqunlock(dqp);
		if (error && XFS_FORCED_SHUTDOWN(mp))
1011
			return 0;	/* Need to prevent umount failure */
Linus Torvalds's avatar
Linus Torvalds committed
1012
		else if (error)
1013
			return error;
Linus Torvalds's avatar
Linus Torvalds committed
1014

1015 1016
		mutex_lock(&q->qi_dqlist_lock);
		if (recl != q->qi_dqreclaims) {
Linus Torvalds's avatar
Linus Torvalds committed
1017 1018 1019
			if (++restarts >= XFS_QM_SYNC_MAX_RESTARTS)
				break;

1020
			mutex_unlock(&q->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022 1023 1024
			goto again;
		}
	}

1025
	mutex_unlock(&q->qi_dqlist_lock);
1026
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028
}

1029 1030 1031 1032 1033 1034 1035
/*
 * The hash chains and the mplist use the same xfs_dqhash structure as
 * their list head, but we can take the mplist qh_lock and one of the
 * hash qh_locks at the same time without any problem as they aren't
 * related.
 */
static struct lock_class_key xfs_quota_mplist_class;
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038 1039 1040

/*
 * This initializes all the quota information that's kept in the
 * mount structure
 */
1041
STATIC int
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
xfs_qm_init_quotainfo(
	xfs_mount_t	*mp)
{
	xfs_quotainfo_t *qinf;
	int		error;
	xfs_dquot_t	*dqp;

	ASSERT(XFS_IS_QUOTA_RUNNING(mp));

	/*
	 * Tell XQM that we exist as soon as possible.
	 */
	if ((error = xfs_qm_hold_quotafs_ref(mp))) {
1055
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
1056 1057 1058 1059 1060 1061 1062 1063 1064
	}

	qinf = mp->m_quotainfo = kmem_zalloc(sizeof(xfs_quotainfo_t), KM_SLEEP);

	/*
	 * See if quotainodes are setup, and if not, allocate them,
	 * and change the superblock accordingly.
	 */
	if ((error = xfs_qm_init_quotainos(mp))) {
1065
		kmem_free(qinf);
Linus Torvalds's avatar
Linus Torvalds committed
1066
		mp->m_quotainfo = NULL;
1067
		return error;
Linus Torvalds's avatar
Linus Torvalds committed
1068 1069
	}

1070 1071 1072
	INIT_LIST_HEAD(&qinf->qi_dqlist);
	mutex_init(&qinf->qi_dqlist_lock);
	lockdep_set_class(&qinf->qi_dqlist_lock, &xfs_quota_mplist_class);
1073

Linus Torvalds's avatar
Linus Torvalds committed
1074 1075 1076
	qinf->qi_dqreclaims = 0;

	/* mutex used to serialize quotaoffs */
1077
	mutex_init(&qinf->qi_quotaofflock);
Linus Torvalds's avatar
Linus Torvalds committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

	/* Precalc some constants */
	qinf->qi_dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
	ASSERT(qinf->qi_dqchunklen);
	qinf->qi_dqperchunk = BBTOB(qinf->qi_dqchunklen);
	do_div(qinf->qi_dqperchunk, sizeof(xfs_dqblk_t));

	mp->m_qflags |= (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_CHKD);

	/*
	 * We try to get the limits from the superuser's limits fields.
	 * This is quite hacky, but it is standard quota practice.
	 * We look at the USR dquot with id == 0 first, but if user quotas
	 * are not enabled we goto the GRP dquot with id == 0.
	 * We don't really care to keep separate default limits for user
	 * and group quotas, at least not at this point.
	 */
	error = xfs_qm_dqget(mp, NULL, (xfs_dqid_t)0,
1096 1097 1098
			     XFS_IS_UQUOTA_RUNNING(mp) ? XFS_DQ_USER : 
			     (XFS_IS_GQUOTA_RUNNING(mp) ? XFS_DQ_GROUP :
				XFS_DQ_PROJ),
Linus Torvalds's avatar
Linus Torvalds committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
			     XFS_QMOPT_DQSUSER|XFS_QMOPT_DOWARN,
			     &dqp);
	if (! error) {
		xfs_disk_dquot_t	*ddqp = &dqp->q_core;

		/*
		 * The warnings and timers set the grace period given to
		 * a user or group before he or she can not perform any
		 * more writing. If it is zero, a default is used.
		 */
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
		qinf->qi_btimelimit = ddqp->d_btimer ?
			be32_to_cpu(ddqp->d_btimer) : XFS_QM_BTIMELIMIT;
		qinf->qi_itimelimit = ddqp->d_itimer ?
			be32_to_cpu(ddqp->d_itimer) : XFS_QM_ITIMELIMIT;
		qinf->qi_rtbtimelimit = ddqp->d_rtbtimer ?
			be32_to_cpu(ddqp->d_rtbtimer) : XFS_QM_RTBTIMELIMIT;
		qinf->qi_bwarnlimit = ddqp->d_bwarns ?
			be16_to_cpu(ddqp->d_bwarns) : XFS_QM_BWARNLIMIT;
		qinf->qi_iwarnlimit = ddqp->d_iwarns ?
			be16_to_cpu(ddqp->d_iwarns) : XFS_QM_IWARNLIMIT;
		qinf->qi_rtbwarnlimit = ddqp->d_rtbwarns ?
			be16_to_cpu(ddqp->d_rtbwarns) : XFS_QM_RTBWARNLIMIT;
		qinf->qi_bhardlimit = be64_to_cpu(ddqp->d_blk_hardlimit);
		qinf->qi_bsoftlimit = be64_to_cpu(ddqp->d_blk_softlimit);
		qinf->qi_ihardlimit = be64_to_cpu(ddqp->d_ino_hardlimit);
		qinf->qi_isoftlimit = be64_to_cpu(ddqp->d_ino_softlimit);
		qinf->qi_rtbhardlimit = be64_to_cpu(ddqp->d_rtb_hardlimit);
		qinf->qi_rtbsoftlimit = be64_to_cpu(ddqp->d_rtb_softlimit);
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
 
		/*
		 * We sent the XFS_QMOPT_DQSUSER flag to dqget because
		 * we don't want this dquot cached. We haven't done a
		 * quotacheck yet, and quotacheck doesn't like incore dquots.
		 */
		xfs_qm_dqdestroy(dqp);
	} else {
		qinf->qi_btimelimit = XFS_QM_BTIMELIMIT;
		qinf->qi_itimelimit = XFS_QM_ITIMELIMIT;
		qinf->qi_rtbtimelimit = XFS_QM_RTBTIMELIMIT;
		qinf->qi_bwarnlimit = XFS_QM_BWARNLIMIT;
		qinf->qi_iwarnlimit = XFS_QM_IWARNLIMIT;
1140
		qinf->qi_rtbwarnlimit = XFS_QM_RTBWARNLIMIT;
Linus Torvalds's avatar
Linus Torvalds committed
1141 1142
	}

1143
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
}


/*
 * Gets called when unmounting a filesystem or when all quotas get
 * turned off.
 * This purges the quota inodes, destroys locks and frees itself.
 */
void
xfs_qm_destroy_quotainfo(
	xfs_mount_t	*mp)
{
	xfs_quotainfo_t *qi;

	qi = mp->m_quotainfo;
	ASSERT(qi != NULL);
	ASSERT(xfs_Gqm != NULL);

	/*
	 * Release the reference that XQM kept, so that we know
	 * when the XQM structure should be freed. We cannot assume
	 * that xfs_Gqm is non-null after this point.
	 */
	xfs_qm_rele_quotafs_ref(mp);

1169 1170
	ASSERT(list_empty(&qi->qi_dqlist));
	mutex_destroy(&qi->qi_dqlist_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1171 1172

	if (qi->qi_uquotaip) {
1173
		IRELE(qi->qi_uquotaip);
Linus Torvalds's avatar
Linus Torvalds committed
1174 1175 1176
		qi->qi_uquotaip = NULL; /* paranoia */
	}
	if (qi->qi_gquotaip) {
1177
		IRELE(qi->qi_gquotaip);
Linus Torvalds's avatar
Linus Torvalds committed
1178 1179 1180
		qi->qi_gquotaip = NULL;
	}
	mutex_destroy(&qi->qi_quotaofflock);
1181
	kmem_free(qi);
Linus Torvalds's avatar
Linus Torvalds committed
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	mp->m_quotainfo = NULL;
}



/* ------------------- PRIVATE STATIC FUNCTIONS ----------------------- */

/* ARGSUSED */
STATIC void
xfs_qm_list_init(
	xfs_dqlist_t	*list,
	char		*str,
	int		n)
{
1196
	mutex_init(&list->qh_lock);
1197
	INIT_LIST_HEAD(&list->qh_list);
Linus Torvalds's avatar
Linus Torvalds committed
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
	list->qh_version = 0;
	list->qh_nelems = 0;
}

STATIC void
xfs_qm_list_destroy(
	xfs_dqlist_t	*list)
{
	mutex_destroy(&(list->qh_lock));
}


/*
 * Stripped down version of dqattach. This doesn't attach, or even look at the
 * dquots attached to the inode. The rationale