• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_bmap.h"
41 #include "xfs_rtalloc.h"
42 #include "xfs_error.h"
43 #include "xfs_itable.h"
44 #include "xfs_rw.h"
45 #include "xfs_acl.h"
46 #include "xfs_attr.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_trans_space.h"
49 #include "xfs_trans_priv.h"
50 #include "xfs_qm.h"
51 
52 
53 /*
54    LOCK ORDER
55 
56    inode lock		    (ilock)
57    dquot hash-chain lock    (hashlock)
58    xqm dquot freelist lock  (freelistlock
59    mount's dquot list lock  (mplistlock)
60    user dquot lock - lock ordering among dquots is based on the uid or gid
61    group dquot lock - similar to udquots. Between the two dquots, the udquot
62 		      has to be locked first.
63    pin lock - the dquot lock must be held to take this lock.
64    flush lock - ditto.
65 */
66 
67 STATIC void		xfs_qm_dqflush_done(xfs_buf_t *, xfs_dq_logitem_t *);
68 
69 #ifdef DEBUG
70 xfs_buftarg_t *xfs_dqerror_target;
71 int xfs_do_dqerror;
72 int xfs_dqreq_num;
73 int xfs_dqerror_mod = 33;
74 #endif
75 
76 static struct lock_class_key xfs_dquot_other_class;
77 
78 /*
79  * Allocate and initialize a dquot. We don't always allocate fresh memory;
80  * we try to reclaim a free dquot if the number of incore dquots are above
81  * a threshold.
82  * The only field inside the core that gets initialized at this point
83  * is the d_id field. The idea is to fill in the entire q_core
84  * when we read in the on disk dquot.
85  */
86 STATIC xfs_dquot_t *
xfs_qm_dqinit(xfs_mount_t * mp,xfs_dqid_t id,uint type)87 xfs_qm_dqinit(
88 	xfs_mount_t  *mp,
89 	xfs_dqid_t   id,
90 	uint	     type)
91 {
92 	xfs_dquot_t	*dqp;
93 	boolean_t	brandnewdquot;
94 
95 	brandnewdquot = xfs_qm_dqalloc_incore(&dqp);
96 	dqp->dq_flags = type;
97 	dqp->q_core.d_id = cpu_to_be32(id);
98 	dqp->q_mount = mp;
99 
100 	/*
101 	 * No need to re-initialize these if this is a reclaimed dquot.
102 	 */
103 	if (brandnewdquot) {
104 		dqp->dq_flnext = dqp->dq_flprev = dqp;
105 		mutex_init(&dqp->q_qlock);
106 		init_waitqueue_head(&dqp->q_pinwait);
107 
108 		/*
109 		 * Because we want to use a counting completion, complete
110 		 * the flush completion once to allow a single access to
111 		 * the flush completion without blocking.
112 		 */
113 		init_completion(&dqp->q_flush);
114 		complete(&dqp->q_flush);
115 
116 #ifdef XFS_DQUOT_TRACE
117 		dqp->q_trace = ktrace_alloc(DQUOT_TRACE_SIZE, KM_NOFS);
118 		xfs_dqtrace_entry(dqp, "DQINIT");
119 #endif
120 	} else {
121 		/*
122 		 * Only the q_core portion was zeroed in dqreclaim_one().
123 		 * So, we need to reset others.
124 		 */
125 		 dqp->q_nrefs = 0;
126 		 dqp->q_blkno = 0;
127 		 dqp->MPL_NEXT = dqp->HL_NEXT = NULL;
128 		 dqp->HL_PREVP = dqp->MPL_PREVP = NULL;
129 		 dqp->q_bufoffset = 0;
130 		 dqp->q_fileoffset = 0;
131 		 dqp->q_transp = NULL;
132 		 dqp->q_gdquot = NULL;
133 		 dqp->q_res_bcount = 0;
134 		 dqp->q_res_icount = 0;
135 		 dqp->q_res_rtbcount = 0;
136 		 atomic_set(&dqp->q_pincount, 0);
137 		 dqp->q_hash = NULL;
138 		 ASSERT(dqp->dq_flnext == dqp->dq_flprev);
139 
140 #ifdef XFS_DQUOT_TRACE
141 		 ASSERT(dqp->q_trace);
142 		 xfs_dqtrace_entry(dqp, "DQRECLAIMED_INIT");
143 #endif
144 	}
145 
146 	/*
147 	 * In either case we need to make sure group quotas have a different
148 	 * lock class than user quotas, to make sure lockdep knows we can
149 	 * locks of one of each at the same time.
150 	 */
151 	if (!(type & XFS_DQ_USER))
152 		lockdep_set_class(&dqp->q_qlock, &xfs_dquot_other_class);
153 
154 	/*
155 	 * log item gets initialized later
156 	 */
157 	return (dqp);
158 }
159 
160 /*
161  * This is called to free all the memory associated with a dquot
162  */
163 void
xfs_qm_dqdestroy(xfs_dquot_t * dqp)164 xfs_qm_dqdestroy(
165 	xfs_dquot_t	*dqp)
166 {
167 	ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));
168 
169 	mutex_destroy(&dqp->q_qlock);
170 	sv_destroy(&dqp->q_pinwait);
171 
172 #ifdef XFS_DQUOT_TRACE
173 	if (dqp->q_trace)
174 	     ktrace_free(dqp->q_trace);
175 	dqp->q_trace = NULL;
176 #endif
177 	kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
178 	atomic_dec(&xfs_Gqm->qm_totaldquots);
179 }
180 
181 /*
182  * This is what a 'fresh' dquot inside a dquot chunk looks like on disk.
183  */
184 STATIC void
xfs_qm_dqinit_core(xfs_dqid_t id,uint type,xfs_dqblk_t * d)185 xfs_qm_dqinit_core(
186 	xfs_dqid_t	id,
187 	uint		type,
188 	xfs_dqblk_t	*d)
189 {
190 	/*
191 	 * Caller has zero'd the entire dquot 'chunk' already.
192 	 */
193 	d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
194 	d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
195 	d->dd_diskdq.d_id = cpu_to_be32(id);
196 	d->dd_diskdq.d_flags = type;
197 }
198 
199 
200 #ifdef XFS_DQUOT_TRACE
201 /*
202  * Dquot tracing for debugging.
203  */
204 /* ARGSUSED */
205 void
__xfs_dqtrace_entry(xfs_dquot_t * dqp,char * func,void * retaddr,xfs_inode_t * ip)206 __xfs_dqtrace_entry(
207 	xfs_dquot_t	*dqp,
208 	char		*func,
209 	void		*retaddr,
210 	xfs_inode_t	*ip)
211 {
212 	xfs_dquot_t	*udqp = NULL;
213 	xfs_ino_t	ino = 0;
214 
215 	ASSERT(dqp->q_trace);
216 	if (ip) {
217 		ino = ip->i_ino;
218 		udqp = ip->i_udquot;
219 	}
220 	ktrace_enter(dqp->q_trace,
221 		     (void *)(__psint_t)DQUOT_KTRACE_ENTRY,
222 		     (void *)func,
223 		     (void *)(__psint_t)dqp->q_nrefs,
224 		     (void *)(__psint_t)dqp->dq_flags,
225 		     (void *)(__psint_t)dqp->q_res_bcount,
226 		     (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_bcount),
227 		     (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_icount),
228 		     (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_blk_hardlimit),
229 		     (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_blk_softlimit),
230 		     (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_ino_hardlimit),
231 		     (void *)(__psint_t)be64_to_cpu(dqp->q_core.d_ino_softlimit),
232 		     (void *)(__psint_t)be32_to_cpu(dqp->q_core.d_id),
233 		     (void *)(__psint_t)current_pid(),
234 		     (void *)(__psint_t)ino,
235 		     (void *)(__psint_t)retaddr,
236 		     (void *)(__psint_t)udqp);
237 	return;
238 }
239 #endif
240 
241 
242 /*
243  * If default limits are in force, push them into the dquot now.
244  * We overwrite the dquot limits only if they are zero and this
245  * is not the root dquot.
246  */
247 void
xfs_qm_adjust_dqlimits(xfs_mount_t * mp,xfs_disk_dquot_t * d)248 xfs_qm_adjust_dqlimits(
249 	xfs_mount_t		*mp,
250 	xfs_disk_dquot_t	*d)
251 {
252 	xfs_quotainfo_t		*q = mp->m_quotainfo;
253 
254 	ASSERT(d->d_id);
255 
256 	if (q->qi_bsoftlimit && !d->d_blk_softlimit)
257 		d->d_blk_softlimit = cpu_to_be64(q->qi_bsoftlimit);
258 	if (q->qi_bhardlimit && !d->d_blk_hardlimit)
259 		d->d_blk_hardlimit = cpu_to_be64(q->qi_bhardlimit);
260 	if (q->qi_isoftlimit && !d->d_ino_softlimit)
261 		d->d_ino_softlimit = cpu_to_be64(q->qi_isoftlimit);
262 	if (q->qi_ihardlimit && !d->d_ino_hardlimit)
263 		d->d_ino_hardlimit = cpu_to_be64(q->qi_ihardlimit);
264 	if (q->qi_rtbsoftlimit && !d->d_rtb_softlimit)
265 		d->d_rtb_softlimit = cpu_to_be64(q->qi_rtbsoftlimit);
266 	if (q->qi_rtbhardlimit && !d->d_rtb_hardlimit)
267 		d->d_rtb_hardlimit = cpu_to_be64(q->qi_rtbhardlimit);
268 }
269 
270 /*
271  * Check the limits and timers of a dquot and start or reset timers
272  * if necessary.
273  * This gets called even when quota enforcement is OFF, which makes our
274  * life a little less complicated. (We just don't reject any quota
275  * reservations in that case, when enforcement is off).
276  * We also return 0 as the values of the timers in Q_GETQUOTA calls, when
277  * enforcement's off.
278  * In contrast, warnings are a little different in that they don't
279  * 'automatically' get started when limits get exceeded.  They do
280  * get reset to zero, however, when we find the count to be under
281  * the soft limit (they are only ever set non-zero via userspace).
282  */
283 void
xfs_qm_adjust_dqtimers(xfs_mount_t * mp,xfs_disk_dquot_t * d)284 xfs_qm_adjust_dqtimers(
285 	xfs_mount_t		*mp,
286 	xfs_disk_dquot_t	*d)
287 {
288 	ASSERT(d->d_id);
289 
290 #ifdef QUOTADEBUG
291 	if (d->d_blk_hardlimit)
292 		ASSERT(be64_to_cpu(d->d_blk_softlimit) <=
293 		       be64_to_cpu(d->d_blk_hardlimit));
294 	if (d->d_ino_hardlimit)
295 		ASSERT(be64_to_cpu(d->d_ino_softlimit) <=
296 		       be64_to_cpu(d->d_ino_hardlimit));
297 	if (d->d_rtb_hardlimit)
298 		ASSERT(be64_to_cpu(d->d_rtb_softlimit) <=
299 		       be64_to_cpu(d->d_rtb_hardlimit));
300 #endif
301 	if (!d->d_btimer) {
302 		if ((d->d_blk_softlimit &&
303 		     (be64_to_cpu(d->d_bcount) >=
304 		      be64_to_cpu(d->d_blk_softlimit))) ||
305 		    (d->d_blk_hardlimit &&
306 		     (be64_to_cpu(d->d_bcount) >=
307 		      be64_to_cpu(d->d_blk_hardlimit)))) {
308 			d->d_btimer = cpu_to_be32(get_seconds() +
309 					XFS_QI_BTIMELIMIT(mp));
310 		} else {
311 			d->d_bwarns = 0;
312 		}
313 	} else {
314 		if ((!d->d_blk_softlimit ||
315 		     (be64_to_cpu(d->d_bcount) <
316 		      be64_to_cpu(d->d_blk_softlimit))) &&
317 		    (!d->d_blk_hardlimit ||
318 		    (be64_to_cpu(d->d_bcount) <
319 		     be64_to_cpu(d->d_blk_hardlimit)))) {
320 			d->d_btimer = 0;
321 		}
322 	}
323 
324 	if (!d->d_itimer) {
325 		if ((d->d_ino_softlimit &&
326 		     (be64_to_cpu(d->d_icount) >=
327 		      be64_to_cpu(d->d_ino_softlimit))) ||
328 		    (d->d_ino_hardlimit &&
329 		     (be64_to_cpu(d->d_icount) >=
330 		      be64_to_cpu(d->d_ino_hardlimit)))) {
331 			d->d_itimer = cpu_to_be32(get_seconds() +
332 					XFS_QI_ITIMELIMIT(mp));
333 		} else {
334 			d->d_iwarns = 0;
335 		}
336 	} else {
337 		if ((!d->d_ino_softlimit ||
338 		     (be64_to_cpu(d->d_icount) <
339 		      be64_to_cpu(d->d_ino_softlimit)))  &&
340 		    (!d->d_ino_hardlimit ||
341 		     (be64_to_cpu(d->d_icount) <
342 		      be64_to_cpu(d->d_ino_hardlimit)))) {
343 			d->d_itimer = 0;
344 		}
345 	}
346 
347 	if (!d->d_rtbtimer) {
348 		if ((d->d_rtb_softlimit &&
349 		     (be64_to_cpu(d->d_rtbcount) >=
350 		      be64_to_cpu(d->d_rtb_softlimit))) ||
351 		    (d->d_rtb_hardlimit &&
352 		     (be64_to_cpu(d->d_rtbcount) >=
353 		      be64_to_cpu(d->d_rtb_hardlimit)))) {
354 			d->d_rtbtimer = cpu_to_be32(get_seconds() +
355 					XFS_QI_RTBTIMELIMIT(mp));
356 		} else {
357 			d->d_rtbwarns = 0;
358 		}
359 	} else {
360 		if ((!d->d_rtb_softlimit ||
361 		     (be64_to_cpu(d->d_rtbcount) <
362 		      be64_to_cpu(d->d_rtb_softlimit))) &&
363 		    (!d->d_rtb_hardlimit ||
364 		     (be64_to_cpu(d->d_rtbcount) <
365 		      be64_to_cpu(d->d_rtb_hardlimit)))) {
366 			d->d_rtbtimer = 0;
367 		}
368 	}
369 }
370 
371 /*
372  * initialize a buffer full of dquots and log the whole thing
373  */
374 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)375 xfs_qm_init_dquot_blk(
376 	xfs_trans_t	*tp,
377 	xfs_mount_t	*mp,
378 	xfs_dqid_t	id,
379 	uint		type,
380 	xfs_buf_t	*bp)
381 {
382 	xfs_dqblk_t	*d;
383 	int		curid, i;
384 
385 	ASSERT(tp);
386 	ASSERT(XFS_BUF_ISBUSY(bp));
387 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
388 
389 	d = (xfs_dqblk_t *)XFS_BUF_PTR(bp);
390 
391 	/*
392 	 * ID of the first dquot in the block - id's are zero based.
393 	 */
394 	curid = id - (id % XFS_QM_DQPERBLK(mp));
395 	ASSERT(curid >= 0);
396 	memset(d, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)));
397 	for (i = 0; i < XFS_QM_DQPERBLK(mp); i++, d++, curid++)
398 		xfs_qm_dqinit_core(curid, type, d);
399 	xfs_trans_dquot_buf(tp, bp,
400 			    (type & XFS_DQ_USER ? XFS_BLI_UDQUOT_BUF :
401 			    ((type & XFS_DQ_PROJ) ? XFS_BLI_PDQUOT_BUF :
402 			     XFS_BLI_GDQUOT_BUF)));
403 	xfs_trans_log_buf(tp, bp, 0, BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1);
404 }
405 
406 
407 
408 /*
409  * Allocate a block and fill it with dquots.
410  * This is called when the bmapi finds a hole.
411  */
412 STATIC int
xfs_qm_dqalloc(xfs_trans_t ** tpp,xfs_mount_t * mp,xfs_dquot_t * dqp,xfs_inode_t * quotip,xfs_fileoff_t offset_fsb,xfs_buf_t ** O_bpp)413 xfs_qm_dqalloc(
414 	xfs_trans_t	**tpp,
415 	xfs_mount_t	*mp,
416 	xfs_dquot_t	*dqp,
417 	xfs_inode_t	*quotip,
418 	xfs_fileoff_t	offset_fsb,
419 	xfs_buf_t	**O_bpp)
420 {
421 	xfs_fsblock_t	firstblock;
422 	xfs_bmap_free_t flist;
423 	xfs_bmbt_irec_t map;
424 	int		nmaps, error, committed;
425 	xfs_buf_t	*bp;
426 	xfs_trans_t	*tp = *tpp;
427 
428 	ASSERT(tp != NULL);
429 	xfs_dqtrace_entry(dqp, "DQALLOC");
430 
431 	/*
432 	 * Initialize the bmap freelist prior to calling bmapi code.
433 	 */
434 	xfs_bmap_init(&flist, &firstblock);
435 	xfs_ilock(quotip, XFS_ILOCK_EXCL);
436 	/*
437 	 * Return if this type of quotas is turned off while we didn't
438 	 * have an inode lock
439 	 */
440 	if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
441 		xfs_iunlock(quotip, XFS_ILOCK_EXCL);
442 		return (ESRCH);
443 	}
444 
445 	/*
446 	 * xfs_trans_commit normally decrements the vnode ref count
447 	 * when it unlocks the inode. Since we want to keep the quota
448 	 * inode around, we bump the vnode ref count now.
449 	 */
450 	IHOLD(quotip);
451 
452 	xfs_trans_ijoin(tp, quotip, XFS_ILOCK_EXCL);
453 	nmaps = 1;
454 	if ((error = xfs_bmapi(tp, quotip,
455 			      offset_fsb, XFS_DQUOT_CLUSTER_SIZE_FSB,
456 			      XFS_BMAPI_METADATA | XFS_BMAPI_WRITE,
457 			      &firstblock,
458 			      XFS_QM_DQALLOC_SPACE_RES(mp),
459 			      &map, &nmaps, &flist, NULL))) {
460 		goto error0;
461 	}
462 	ASSERT(map.br_blockcount == XFS_DQUOT_CLUSTER_SIZE_FSB);
463 	ASSERT(nmaps == 1);
464 	ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
465 	       (map.br_startblock != HOLESTARTBLOCK));
466 
467 	/*
468 	 * Keep track of the blkno to save a lookup later
469 	 */
470 	dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
471 
472 	/* now we can just get the buffer (there's nothing to read yet) */
473 	bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
474 			       dqp->q_blkno,
475 			       XFS_QI_DQCHUNKLEN(mp),
476 			       0);
477 	if (!bp || (error = XFS_BUF_GETERROR(bp)))
478 		goto error1;
479 	/*
480 	 * Make a chunk of dquots out of this buffer and log
481 	 * the entire thing.
482 	 */
483 	xfs_qm_init_dquot_blk(tp, mp, be32_to_cpu(dqp->q_core.d_id),
484 			      dqp->dq_flags & XFS_DQ_ALLTYPES, bp);
485 
486 	/*
487 	 * xfs_bmap_finish() may commit the current transaction and
488 	 * start a second transaction if the freelist is not empty.
489 	 *
490 	 * Since we still want to modify this buffer, we need to
491 	 * ensure that the buffer is not released on commit of
492 	 * the first transaction and ensure the buffer is added to the
493 	 * second transaction.
494 	 *
495 	 * If there is only one transaction then don't stop the buffer
496 	 * from being released when it commits later on.
497 	 */
498 
499 	xfs_trans_bhold(tp, bp);
500 
501 	if ((error = xfs_bmap_finish(tpp, &flist, &committed))) {
502 		goto error1;
503 	}
504 
505 	if (committed) {
506 		tp = *tpp;
507 		xfs_trans_bjoin(tp, bp);
508 	} else {
509 		xfs_trans_bhold_release(tp, bp);
510 	}
511 
512 	*O_bpp = bp;
513 	return 0;
514 
515       error1:
516 	xfs_bmap_cancel(&flist);
517       error0:
518 	xfs_iunlock(quotip, XFS_ILOCK_EXCL);
519 
520 	return (error);
521 }
522 
523 /*
524  * Maps a dquot to the buffer containing its on-disk version.
525  * This returns a ptr to the buffer containing the on-disk dquot
526  * in the bpp param, and a ptr to the on-disk dquot within that buffer
527  */
528 STATIC int
xfs_qm_dqtobp(xfs_trans_t ** tpp,xfs_dquot_t * dqp,xfs_disk_dquot_t ** O_ddpp,xfs_buf_t ** O_bpp,uint flags)529 xfs_qm_dqtobp(
530 	xfs_trans_t		**tpp,
531 	xfs_dquot_t		*dqp,
532 	xfs_disk_dquot_t	**O_ddpp,
533 	xfs_buf_t		**O_bpp,
534 	uint			flags)
535 {
536 	xfs_bmbt_irec_t map;
537 	int		nmaps, error;
538 	xfs_buf_t	*bp;
539 	xfs_inode_t	*quotip;
540 	xfs_mount_t	*mp;
541 	xfs_disk_dquot_t *ddq;
542 	xfs_dqid_t	id;
543 	boolean_t	newdquot;
544 	xfs_trans_t	*tp = (tpp ? *tpp : NULL);
545 
546 	mp = dqp->q_mount;
547 	id = be32_to_cpu(dqp->q_core.d_id);
548 	nmaps = 1;
549 	newdquot = B_FALSE;
550 
551 	/*
552 	 * If we don't know where the dquot lives, find out.
553 	 */
554 	if (dqp->q_blkno == (xfs_daddr_t) 0) {
555 		/* We use the id as an index */
556 		dqp->q_fileoffset = (xfs_fileoff_t)id / XFS_QM_DQPERBLK(mp);
557 		nmaps = 1;
558 		quotip = XFS_DQ_TO_QIP(dqp);
559 		xfs_ilock(quotip, XFS_ILOCK_SHARED);
560 		/*
561 		 * Return if this type of quotas is turned off while we didn't
562 		 * have an inode lock
563 		 */
564 		if (XFS_IS_THIS_QUOTA_OFF(dqp)) {
565 			xfs_iunlock(quotip, XFS_ILOCK_SHARED);
566 			return (ESRCH);
567 		}
568 		/*
569 		 * Find the block map; no allocations yet
570 		 */
571 		error = xfs_bmapi(NULL, quotip, dqp->q_fileoffset,
572 				  XFS_DQUOT_CLUSTER_SIZE_FSB,
573 				  XFS_BMAPI_METADATA,
574 				  NULL, 0, &map, &nmaps, NULL, NULL);
575 
576 		xfs_iunlock(quotip, XFS_ILOCK_SHARED);
577 		if (error)
578 			return (error);
579 		ASSERT(nmaps == 1);
580 		ASSERT(map.br_blockcount == 1);
581 
582 		/*
583 		 * offset of dquot in the (fixed sized) dquot chunk.
584 		 */
585 		dqp->q_bufoffset = (id % XFS_QM_DQPERBLK(mp)) *
586 			sizeof(xfs_dqblk_t);
587 		if (map.br_startblock == HOLESTARTBLOCK) {
588 			/*
589 			 * We don't allocate unless we're asked to
590 			 */
591 			if (!(flags & XFS_QMOPT_DQALLOC))
592 				return (ENOENT);
593 
594 			ASSERT(tp);
595 			if ((error = xfs_qm_dqalloc(tpp, mp, dqp, quotip,
596 						dqp->q_fileoffset, &bp)))
597 				return (error);
598 			tp = *tpp;
599 			newdquot = B_TRUE;
600 		} else {
601 			/*
602 			 * store the blkno etc so that we don't have to do the
603 			 * mapping all the time
604 			 */
605 			dqp->q_blkno = XFS_FSB_TO_DADDR(mp, map.br_startblock);
606 		}
607 	}
608 	ASSERT(dqp->q_blkno != DELAYSTARTBLOCK);
609 	ASSERT(dqp->q_blkno != HOLESTARTBLOCK);
610 
611 	/*
612 	 * Read in the buffer, unless we've just done the allocation
613 	 * (in which case we already have the buf).
614 	 */
615 	if (! newdquot) {
616 		xfs_dqtrace_entry(dqp, "DQTOBP READBUF");
617 		if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
618 					       dqp->q_blkno,
619 					       XFS_QI_DQCHUNKLEN(mp),
620 					       0, &bp))) {
621 			return (error);
622 		}
623 		if (error || !bp)
624 			return XFS_ERROR(error);
625 	}
626 	ASSERT(XFS_BUF_ISBUSY(bp));
627 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
628 
629 	/*
630 	 * calculate the location of the dquot inside the buffer.
631 	 */
632 	ddq = (xfs_disk_dquot_t *)((char *)XFS_BUF_PTR(bp) + dqp->q_bufoffset);
633 
634 	/*
635 	 * A simple sanity check in case we got a corrupted dquot...
636 	 */
637 	if (xfs_qm_dqcheck(ddq, id, dqp->dq_flags & XFS_DQ_ALLTYPES,
638 			   flags & (XFS_QMOPT_DQREPAIR|XFS_QMOPT_DOWARN),
639 			   "dqtobp")) {
640 		if (!(flags & XFS_QMOPT_DQREPAIR)) {
641 			xfs_trans_brelse(tp, bp);
642 			return XFS_ERROR(EIO);
643 		}
644 		XFS_BUF_BUSY(bp); /* We dirtied this */
645 	}
646 
647 	*O_bpp = bp;
648 	*O_ddpp = ddq;
649 
650 	return (0);
651 }
652 
653 
654 /*
655  * Read in the ondisk dquot using dqtobp() then copy it to an incore version,
656  * and release the buffer immediately.
657  *
658  */
659 /* ARGSUSED */
660 STATIC int
xfs_qm_dqread(xfs_trans_t ** tpp,xfs_dqid_t id,xfs_dquot_t * dqp,uint flags)661 xfs_qm_dqread(
662 	xfs_trans_t	**tpp,
663 	xfs_dqid_t	id,
664 	xfs_dquot_t	*dqp,	/* dquot to get filled in */
665 	uint		flags)
666 {
667 	xfs_disk_dquot_t *ddqp;
668 	xfs_buf_t	 *bp;
669 	int		 error;
670 	xfs_trans_t	 *tp;
671 
672 	ASSERT(tpp);
673 
674 	/*
675 	 * get a pointer to the on-disk dquot and the buffer containing it
676 	 * dqp already knows its own type (GROUP/USER).
677 	 */
678 	xfs_dqtrace_entry(dqp, "DQREAD");
679 	if ((error = xfs_qm_dqtobp(tpp, dqp, &ddqp, &bp, flags))) {
680 		return (error);
681 	}
682 	tp = *tpp;
683 
684 	/* copy everything from disk dquot to the incore dquot */
685 	memcpy(&dqp->q_core, ddqp, sizeof(xfs_disk_dquot_t));
686 	ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
687 	xfs_qm_dquot_logitem_init(dqp);
688 
689 	/*
690 	 * Reservation counters are defined as reservation plus current usage
691 	 * to avoid having to add everytime.
692 	 */
693 	dqp->q_res_bcount = be64_to_cpu(ddqp->d_bcount);
694 	dqp->q_res_icount = be64_to_cpu(ddqp->d_icount);
695 	dqp->q_res_rtbcount = be64_to_cpu(ddqp->d_rtbcount);
696 
697 	/* Mark the buf so that this will stay incore a little longer */
698 	XFS_BUF_SET_VTYPE_REF(bp, B_FS_DQUOT, XFS_DQUOT_REF);
699 
700 	/*
701 	 * We got the buffer with a xfs_trans_read_buf() (in dqtobp())
702 	 * So we need to release with xfs_trans_brelse().
703 	 * The strategy here is identical to that of inodes; we lock
704 	 * the dquot in xfs_qm_dqget() before making it accessible to
705 	 * others. This is because dquots, like inodes, need a good level of
706 	 * concurrency, and we don't want to take locks on the entire buffers
707 	 * for dquot accesses.
708 	 * Note also that the dquot buffer may even be dirty at this point, if
709 	 * this particular dquot was repaired. We still aren't afraid to
710 	 * brelse it because we have the changes incore.
711 	 */
712 	ASSERT(XFS_BUF_ISBUSY(bp));
713 	ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
714 	xfs_trans_brelse(tp, bp);
715 
716 	return (error);
717 }
718 
719 
720 /*
721  * allocate an incore dquot from the kernel heap,
722  * and fill its core with quota information kept on disk.
723  * If XFS_QMOPT_DQALLOC is set, it'll allocate a dquot on disk
724  * if it wasn't already allocated.
725  */
726 STATIC int
xfs_qm_idtodq(xfs_mount_t * mp,xfs_dqid_t id,uint type,uint flags,xfs_dquot_t ** O_dqpp)727 xfs_qm_idtodq(
728 	xfs_mount_t	*mp,
729 	xfs_dqid_t	id,	 /* gid or uid, depending on type */
730 	uint		type,	 /* UDQUOT or GDQUOT */
731 	uint		flags,	 /* DQALLOC, DQREPAIR */
732 	xfs_dquot_t	**O_dqpp)/* OUT : incore dquot, not locked */
733 {
734 	xfs_dquot_t	*dqp;
735 	int		error;
736 	xfs_trans_t	*tp;
737 	int		cancelflags=0;
738 
739 	dqp = xfs_qm_dqinit(mp, id, type);
740 	tp = NULL;
741 	if (flags & XFS_QMOPT_DQALLOC) {
742 		tp = xfs_trans_alloc(mp, XFS_TRANS_QM_DQALLOC);
743 		if ((error = xfs_trans_reserve(tp,
744 				       XFS_QM_DQALLOC_SPACE_RES(mp),
745 				       XFS_WRITE_LOG_RES(mp) +
746 					      BBTOB(XFS_QI_DQCHUNKLEN(mp)) - 1 +
747 					      128,
748 				       0,
749 				       XFS_TRANS_PERM_LOG_RES,
750 				       XFS_WRITE_LOG_COUNT))) {
751 			cancelflags = 0;
752 			goto error0;
753 		}
754 		cancelflags = XFS_TRANS_RELEASE_LOG_RES;
755 	}
756 
757 	/*
758 	 * Read it from disk; xfs_dqread() takes care of
759 	 * all the necessary initialization of dquot's fields (locks, etc)
760 	 */
761 	if ((error = xfs_qm_dqread(&tp, id, dqp, flags))) {
762 		/*
763 		 * This can happen if quotas got turned off (ESRCH),
764 		 * or if the dquot didn't exist on disk and we ask to
765 		 * allocate (ENOENT).
766 		 */
767 		xfs_dqtrace_entry(dqp, "DQREAD FAIL");
768 		cancelflags |= XFS_TRANS_ABORT;
769 		goto error0;
770 	}
771 	if (tp) {
772 		if ((error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES)))
773 			goto error1;
774 	}
775 
776 	*O_dqpp = dqp;
777 	return (0);
778 
779  error0:
780 	ASSERT(error);
781 	if (tp)
782 		xfs_trans_cancel(tp, cancelflags);
783  error1:
784 	xfs_qm_dqdestroy(dqp);
785 	*O_dqpp = NULL;
786 	return (error);
787 }
788 
789 /*
790  * Lookup a dquot in the incore dquot hashtable. We keep two separate
791  * hashtables for user and group dquots; and, these are global tables
792  * inside the XQM, not per-filesystem tables.
793  * The hash chain must be locked by caller, and it is left locked
794  * on return. Returning dquot is locked.
795  */
796 STATIC int
xfs_qm_dqlookup(xfs_mount_t * mp,xfs_dqid_t id,xfs_dqhash_t * qh,xfs_dquot_t ** O_dqpp)797 xfs_qm_dqlookup(
798 	xfs_mount_t		*mp,
799 	xfs_dqid_t		id,
800 	xfs_dqhash_t		*qh,
801 	xfs_dquot_t		**O_dqpp)
802 {
803 	xfs_dquot_t		*dqp;
804 	uint			flist_locked;
805 	xfs_dquot_t		*d;
806 
807 	ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
808 
809 	flist_locked = B_FALSE;
810 
811 	/*
812 	 * Traverse the hashchain looking for a match
813 	 */
814 	for (dqp = qh->qh_next; dqp != NULL; dqp = dqp->HL_NEXT) {
815 		/*
816 		 * We already have the hashlock. We don't need the
817 		 * dqlock to look at the id field of the dquot, since the
818 		 * id can't be modified without the hashlock anyway.
819 		 */
820 		if (be32_to_cpu(dqp->q_core.d_id) == id && dqp->q_mount == mp) {
821 			xfs_dqtrace_entry(dqp, "DQFOUND BY LOOKUP");
822 			/*
823 			 * All in core dquots must be on the dqlist of mp
824 			 */
825 			ASSERT(dqp->MPL_PREVP != NULL);
826 
827 			xfs_dqlock(dqp);
828 			if (dqp->q_nrefs == 0) {
829 				ASSERT (XFS_DQ_IS_ON_FREELIST(dqp));
830 				if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
831 					xfs_dqtrace_entry(dqp, "DQLOOKUP: WANT");
832 
833 					/*
834 					 * We may have raced with dqreclaim_one()
835 					 * (and lost). So, flag that we don't
836 					 * want the dquot to be reclaimed.
837 					 */
838 					dqp->dq_flags |= XFS_DQ_WANT;
839 					xfs_dqunlock(dqp);
840 					xfs_qm_freelist_lock(xfs_Gqm);
841 					xfs_dqlock(dqp);
842 					dqp->dq_flags &= ~(XFS_DQ_WANT);
843 				}
844 				flist_locked = B_TRUE;
845 			}
846 
847 			/*
848 			 * id couldn't have changed; we had the hashlock all
849 			 * along
850 			 */
851 			ASSERT(be32_to_cpu(dqp->q_core.d_id) == id);
852 
853 			if (flist_locked) {
854 				if (dqp->q_nrefs != 0) {
855 					xfs_qm_freelist_unlock(xfs_Gqm);
856 					flist_locked = B_FALSE;
857 				} else {
858 					/*
859 					 * take it off the freelist
860 					 */
861 					xfs_dqtrace_entry(dqp,
862 							"DQLOOKUP: TAKEOFF FL");
863 					XQM_FREELIST_REMOVE(dqp);
864 					/* xfs_qm_freelist_print(&(xfs_Gqm->
865 							qm_dqfreelist),
866 							"after removal"); */
867 				}
868 			}
869 
870 			/*
871 			 * grab a reference
872 			 */
873 			XFS_DQHOLD(dqp);
874 
875 			if (flist_locked)
876 				xfs_qm_freelist_unlock(xfs_Gqm);
877 			/*
878 			 * move the dquot to the front of the hashchain
879 			 */
880 			ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
881 			if (dqp->HL_PREVP != &qh->qh_next) {
882 				xfs_dqtrace_entry(dqp,
883 						  "DQLOOKUP: HASH MOVETOFRONT");
884 				if ((d = dqp->HL_NEXT))
885 					d->HL_PREVP = dqp->HL_PREVP;
886 				*(dqp->HL_PREVP) = d;
887 				d = qh->qh_next;
888 				d->HL_PREVP = &dqp->HL_NEXT;
889 				dqp->HL_NEXT = d;
890 				dqp->HL_PREVP = &qh->qh_next;
891 				qh->qh_next = dqp;
892 			}
893 			xfs_dqtrace_entry(dqp, "LOOKUP END");
894 			*O_dqpp = dqp;
895 			ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
896 			return (0);
897 		}
898 	}
899 
900 	*O_dqpp = NULL;
901 	ASSERT(XFS_DQ_IS_HASH_LOCKED(qh));
902 	return (1);
903 }
904 
905 /*
906  * Given the file system, inode OR id, and type (UDQUOT/GDQUOT), return a
907  * a locked dquot, doing an allocation (if requested) as needed.
908  * When both an inode and an id are given, the inode's id takes precedence.
909  * That is, if the id changes while we don't hold the ilock inside this
910  * function, the new dquot is returned, not necessarily the one requested
911  * in the id argument.
912  */
913 int
xfs_qm_dqget(xfs_mount_t * mp,xfs_inode_t * ip,xfs_dqid_t id,uint type,uint flags,xfs_dquot_t ** O_dqpp)914 xfs_qm_dqget(
915 	xfs_mount_t	*mp,
916 	xfs_inode_t	*ip,	  /* locked inode (optional) */
917 	xfs_dqid_t	id,	  /* uid/projid/gid depending on type */
918 	uint		type,	  /* XFS_DQ_USER/XFS_DQ_PROJ/XFS_DQ_GROUP */
919 	uint		flags,	  /* DQALLOC, DQSUSER, DQREPAIR, DOWARN */
920 	xfs_dquot_t	**O_dqpp) /* OUT : locked incore dquot */
921 {
922 	xfs_dquot_t	*dqp;
923 	xfs_dqhash_t	*h;
924 	uint		version;
925 	int		error;
926 
927 	ASSERT(XFS_IS_QUOTA_RUNNING(mp));
928 	if ((! XFS_IS_UQUOTA_ON(mp) && type == XFS_DQ_USER) ||
929 	    (! XFS_IS_PQUOTA_ON(mp) && type == XFS_DQ_PROJ) ||
930 	    (! XFS_IS_GQUOTA_ON(mp) && type == XFS_DQ_GROUP)) {
931 		return (ESRCH);
932 	}
933 	h = XFS_DQ_HASH(mp, id, type);
934 
935 #ifdef DEBUG
936 	if (xfs_do_dqerror) {
937 		if ((xfs_dqerror_target == mp->m_ddev_targp) &&
938 		    (xfs_dqreq_num++ % xfs_dqerror_mod) == 0) {
939 			cmn_err(CE_DEBUG, "Returning error in dqget");
940 			return (EIO);
941 		}
942 	}
943 #endif
944 
945  again:
946 
947 #ifdef DEBUG
948 	ASSERT(type == XFS_DQ_USER ||
949 	       type == XFS_DQ_PROJ ||
950 	       type == XFS_DQ_GROUP);
951 	if (ip) {
952 		ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
953 		if (type == XFS_DQ_USER)
954 			ASSERT(ip->i_udquot == NULL);
955 		else
956 			ASSERT(ip->i_gdquot == NULL);
957 	}
958 #endif
959 	XFS_DQ_HASH_LOCK(h);
960 
961 	/*
962 	 * Look in the cache (hashtable).
963 	 * The chain is kept locked during lookup.
964 	 */
965 	if (xfs_qm_dqlookup(mp, id, h, O_dqpp) == 0) {
966 		XQM_STATS_INC(xqmstats.xs_qm_dqcachehits);
967 		/*
968 		 * The dquot was found, moved to the front of the chain,
969 		 * taken off the freelist if it was on it, and locked
970 		 * at this point. Just unlock the hashchain and return.
971 		 */
972 		ASSERT(*O_dqpp);
973 		ASSERT(XFS_DQ_IS_LOCKED(*O_dqpp));
974 		XFS_DQ_HASH_UNLOCK(h);
975 		xfs_dqtrace_entry(*O_dqpp, "DQGET DONE (FROM CACHE)");
976 		return (0);	/* success */
977 	}
978 	XQM_STATS_INC(xqmstats.xs_qm_dqcachemisses);
979 
980 	/*
981 	 * Dquot cache miss. We don't want to keep the inode lock across
982 	 * a (potential) disk read. Also we don't want to deal with the lock
983 	 * ordering between quotainode and this inode. OTOH, dropping the inode
984 	 * lock here means dealing with a chown that can happen before
985 	 * we re-acquire the lock.
986 	 */
987 	if (ip)
988 		xfs_iunlock(ip, XFS_ILOCK_EXCL);
989 	/*
990 	 * Save the hashchain version stamp, and unlock the chain, so that
991 	 * we don't keep the lock across a disk read
992 	 */
993 	version = h->qh_version;
994 	XFS_DQ_HASH_UNLOCK(h);
995 
996 	/*
997 	 * Allocate the dquot on the kernel heap, and read the ondisk
998 	 * portion off the disk. Also, do all the necessary initialization
999 	 * This can return ENOENT if dquot didn't exist on disk and we didn't
1000 	 * ask it to allocate; ESRCH if quotas got turned off suddenly.
1001 	 */
1002 	if ((error = xfs_qm_idtodq(mp, id, type,
1003 				  flags & (XFS_QMOPT_DQALLOC|XFS_QMOPT_DQREPAIR|
1004 					   XFS_QMOPT_DOWARN),
1005 				  &dqp))) {
1006 		if (ip)
1007 			xfs_ilock(ip, XFS_ILOCK_EXCL);
1008 		return (error);
1009 	}
1010 
1011 	/*
1012 	 * See if this is mount code calling to look at the overall quota limits
1013 	 * which are stored in the id == 0 user or group's dquot.
1014 	 * Since we may not have done a quotacheck by this point, just return
1015 	 * the dquot without attaching it to any hashtables, lists, etc, or even
1016 	 * taking a reference.
1017 	 * The caller must dqdestroy this once done.
1018 	 */
1019 	if (flags & XFS_QMOPT_DQSUSER) {
1020 		ASSERT(id == 0);
1021 		ASSERT(! ip);
1022 		goto dqret;
1023 	}
1024 
1025 	/*
1026 	 * Dquot lock comes after hashlock in the lock ordering
1027 	 */
1028 	if (ip) {
1029 		xfs_ilock(ip, XFS_ILOCK_EXCL);
1030 		if (! XFS_IS_DQTYPE_ON(mp, type)) {
1031 			/* inode stays locked on return */
1032 			xfs_qm_dqdestroy(dqp);
1033 			return XFS_ERROR(ESRCH);
1034 		}
1035 		/*
1036 		 * A dquot could be attached to this inode by now, since
1037 		 * we had dropped the ilock.
1038 		 */
1039 		if (type == XFS_DQ_USER) {
1040 			if (ip->i_udquot) {
1041 				xfs_qm_dqdestroy(dqp);
1042 				dqp = ip->i_udquot;
1043 				xfs_dqlock(dqp);
1044 				goto dqret;
1045 			}
1046 		} else {
1047 			if (ip->i_gdquot) {
1048 				xfs_qm_dqdestroy(dqp);
1049 				dqp = ip->i_gdquot;
1050 				xfs_dqlock(dqp);
1051 				goto dqret;
1052 			}
1053 		}
1054 	}
1055 
1056 	/*
1057 	 * Hashlock comes after ilock in lock order
1058 	 */
1059 	XFS_DQ_HASH_LOCK(h);
1060 	if (version != h->qh_version) {
1061 		xfs_dquot_t *tmpdqp;
1062 		/*
1063 		 * Now, see if somebody else put the dquot in the
1064 		 * hashtable before us. This can happen because we didn't
1065 		 * keep the hashchain lock. We don't have to worry about
1066 		 * lock order between the two dquots here since dqp isn't
1067 		 * on any findable lists yet.
1068 		 */
1069 		if (xfs_qm_dqlookup(mp, id, h, &tmpdqp) == 0) {
1070 			/*
1071 			 * Duplicate found. Just throw away the new dquot
1072 			 * and start over.
1073 			 */
1074 			xfs_qm_dqput(tmpdqp);
1075 			XFS_DQ_HASH_UNLOCK(h);
1076 			xfs_qm_dqdestroy(dqp);
1077 			XQM_STATS_INC(xqmstats.xs_qm_dquot_dups);
1078 			goto again;
1079 		}
1080 	}
1081 
1082 	/*
1083 	 * Put the dquot at the beginning of the hash-chain and mp's list
1084 	 * LOCK ORDER: hashlock, freelistlock, mplistlock, udqlock, gdqlock ..
1085 	 */
1086 	ASSERT(XFS_DQ_IS_HASH_LOCKED(h));
1087 	dqp->q_hash = h;
1088 	XQM_HASHLIST_INSERT(h, dqp);
1089 
1090 	/*
1091 	 * Attach this dquot to this filesystem's list of all dquots,
1092 	 * kept inside the mount structure in m_quotainfo field
1093 	 */
1094 	xfs_qm_mplist_lock(mp);
1095 
1096 	/*
1097 	 * We return a locked dquot to the caller, with a reference taken
1098 	 */
1099 	xfs_dqlock(dqp);
1100 	dqp->q_nrefs = 1;
1101 
1102 	XQM_MPLIST_INSERT(&(XFS_QI_MPL_LIST(mp)), dqp);
1103 
1104 	xfs_qm_mplist_unlock(mp);
1105 	XFS_DQ_HASH_UNLOCK(h);
1106  dqret:
1107 	ASSERT((ip == NULL) || xfs_isilocked(ip, XFS_ILOCK_EXCL));
1108 	xfs_dqtrace_entry(dqp, "DQGET DONE");
1109 	*O_dqpp = dqp;
1110 	return (0);
1111 }
1112 
1113 
1114 /*
1115  * Release a reference to the dquot (decrement ref-count)
1116  * and unlock it. If there is a group quota attached to this
1117  * dquot, carefully release that too without tripping over
1118  * deadlocks'n'stuff.
1119  */
1120 void
xfs_qm_dqput(xfs_dquot_t * dqp)1121 xfs_qm_dqput(
1122 	xfs_dquot_t	*dqp)
1123 {
1124 	xfs_dquot_t	*gdqp;
1125 
1126 	ASSERT(dqp->q_nrefs > 0);
1127 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
1128 	xfs_dqtrace_entry(dqp, "DQPUT");
1129 
1130 	if (dqp->q_nrefs != 1) {
1131 		dqp->q_nrefs--;
1132 		xfs_dqunlock(dqp);
1133 		return;
1134 	}
1135 
1136 	/*
1137 	 * drop the dqlock and acquire the freelist and dqlock
1138 	 * in the right order; but try to get it out-of-order first
1139 	 */
1140 	if (! xfs_qm_freelist_lock_nowait(xfs_Gqm)) {
1141 		xfs_dqtrace_entry(dqp, "DQPUT: FLLOCK-WAIT");
1142 		xfs_dqunlock(dqp);
1143 		xfs_qm_freelist_lock(xfs_Gqm);
1144 		xfs_dqlock(dqp);
1145 	}
1146 
1147 	while (1) {
1148 		gdqp = NULL;
1149 
1150 		/* We can't depend on nrefs being == 1 here */
1151 		if (--dqp->q_nrefs == 0) {
1152 			xfs_dqtrace_entry(dqp, "DQPUT: ON FREELIST");
1153 			/*
1154 			 * insert at end of the freelist.
1155 			 */
1156 			XQM_FREELIST_INSERT(&(xfs_Gqm->qm_dqfreelist), dqp);
1157 
1158 			/*
1159 			 * If we just added a udquot to the freelist, then
1160 			 * we want to release the gdquot reference that
1161 			 * it (probably) has. Otherwise it'll keep the
1162 			 * gdquot from getting reclaimed.
1163 			 */
1164 			if ((gdqp = dqp->q_gdquot)) {
1165 				/*
1166 				 * Avoid a recursive dqput call
1167 				 */
1168 				xfs_dqlock(gdqp);
1169 				dqp->q_gdquot = NULL;
1170 			}
1171 
1172 			/* xfs_qm_freelist_print(&(xfs_Gqm->qm_dqfreelist),
1173 			   "@@@@@++ Free list (after append) @@@@@+");
1174 			   */
1175 		}
1176 		xfs_dqunlock(dqp);
1177 
1178 		/*
1179 		 * If we had a group quota inside the user quota as a hint,
1180 		 * release it now.
1181 		 */
1182 		if (! gdqp)
1183 			break;
1184 		dqp = gdqp;
1185 	}
1186 	xfs_qm_freelist_unlock(xfs_Gqm);
1187 }
1188 
1189 /*
1190  * Release a dquot. Flush it if dirty, then dqput() it.
1191  * dquot must not be locked.
1192  */
1193 void
xfs_qm_dqrele(xfs_dquot_t * dqp)1194 xfs_qm_dqrele(
1195 	xfs_dquot_t	*dqp)
1196 {
1197 	ASSERT(dqp);
1198 	xfs_dqtrace_entry(dqp, "DQRELE");
1199 
1200 	xfs_dqlock(dqp);
1201 	/*
1202 	 * We don't care to flush it if the dquot is dirty here.
1203 	 * That will create stutters that we want to avoid.
1204 	 * Instead we do a delayed write when we try to reclaim
1205 	 * a dirty dquot. Also xfs_sync will take part of the burden...
1206 	 */
1207 	xfs_qm_dqput(dqp);
1208 }
1209 
1210 
1211 /*
1212  * Write a modified dquot to disk.
1213  * The dquot must be locked and the flush lock too taken by caller.
1214  * The flush lock will not be unlocked until the dquot reaches the disk,
1215  * but the dquot is free to be unlocked and modified by the caller
1216  * in the interim. Dquot is still locked on return. This behavior is
1217  * identical to that of inodes.
1218  */
1219 int
xfs_qm_dqflush(xfs_dquot_t * dqp,uint flags)1220 xfs_qm_dqflush(
1221 	xfs_dquot_t		*dqp,
1222 	uint			flags)
1223 {
1224 	xfs_mount_t		*mp;
1225 	xfs_buf_t		*bp;
1226 	xfs_disk_dquot_t	*ddqp;
1227 	int			error;
1228 
1229 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
1230 	ASSERT(!completion_done(&dqp->q_flush));
1231 	xfs_dqtrace_entry(dqp, "DQFLUSH");
1232 
1233 	/*
1234 	 * If not dirty, or it's pinned and we are not supposed to
1235 	 * block, nada.
1236 	 */
1237 	if (!XFS_DQ_IS_DIRTY(dqp) ||
1238 	    (!(flags & XFS_QMOPT_SYNC) && atomic_read(&dqp->q_pincount) > 0)) {
1239 		xfs_dqfunlock(dqp);
1240 		return 0;
1241 	}
1242 	xfs_qm_dqunpin_wait(dqp);
1243 
1244 	/*
1245 	 * This may have been unpinned because the filesystem is shutting
1246 	 * down forcibly. If that's the case we must not write this dquot
1247 	 * to disk, because the log record didn't make it to disk!
1248 	 */
1249 	if (XFS_FORCED_SHUTDOWN(dqp->q_mount)) {
1250 		dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1251 		xfs_dqfunlock(dqp);
1252 		return XFS_ERROR(EIO);
1253 	}
1254 
1255 	/*
1256 	 * Get the buffer containing the on-disk dquot
1257 	 * We don't need a transaction envelope because we know that the
1258 	 * the ondisk-dquot has already been allocated for.
1259 	 */
1260 	if ((error = xfs_qm_dqtobp(NULL, dqp, &ddqp, &bp, XFS_QMOPT_DOWARN))) {
1261 		xfs_dqtrace_entry(dqp, "DQTOBP FAIL");
1262 		ASSERT(error != ENOENT);
1263 		/*
1264 		 * Quotas could have gotten turned off (ESRCH)
1265 		 */
1266 		xfs_dqfunlock(dqp);
1267 		return (error);
1268 	}
1269 
1270 	if (xfs_qm_dqcheck(&dqp->q_core, be32_to_cpu(ddqp->d_id),
1271 			   0, XFS_QMOPT_DOWARN, "dqflush (incore copy)")) {
1272 		xfs_force_shutdown(dqp->q_mount, SHUTDOWN_CORRUPT_INCORE);
1273 		return XFS_ERROR(EIO);
1274 	}
1275 
1276 	/* This is the only portion of data that needs to persist */
1277 	memcpy(ddqp, &(dqp->q_core), sizeof(xfs_disk_dquot_t));
1278 
1279 	/*
1280 	 * Clear the dirty field and remember the flush lsn for later use.
1281 	 */
1282 	dqp->dq_flags &= ~(XFS_DQ_DIRTY);
1283 	mp = dqp->q_mount;
1284 
1285 	xfs_trans_ail_copy_lsn(mp->m_ail, &dqp->q_logitem.qli_flush_lsn,
1286 					&dqp->q_logitem.qli_item.li_lsn);
1287 
1288 	/*
1289 	 * Attach an iodone routine so that we can remove this dquot from the
1290 	 * AIL and release the flush lock once the dquot is synced to disk.
1291 	 */
1292 	xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t *, xfs_log_item_t *))
1293 			      xfs_qm_dqflush_done, &(dqp->q_logitem.qli_item));
1294 	/*
1295 	 * If the buffer is pinned then push on the log so we won't
1296 	 * get stuck waiting in the write for too long.
1297 	 */
1298 	if (XFS_BUF_ISPINNED(bp)) {
1299 		xfs_dqtrace_entry(dqp, "DQFLUSH LOG FORCE");
1300 		xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1301 	}
1302 
1303 	if (flags & XFS_QMOPT_DELWRI) {
1304 		xfs_bdwrite(mp, bp);
1305 	} else if (flags & XFS_QMOPT_ASYNC) {
1306 		error = xfs_bawrite(mp, bp);
1307 	} else {
1308 		error = xfs_bwrite(mp, bp);
1309 	}
1310 	xfs_dqtrace_entry(dqp, "DQFLUSH END");
1311 	/*
1312 	 * dqp is still locked, but caller is free to unlock it now.
1313 	 */
1314 	return (error);
1315 
1316 }
1317 
1318 /*
1319  * This is the dquot flushing I/O completion routine.  It is called
1320  * from interrupt level when the buffer containing the dquot is
1321  * flushed to disk.  It is responsible for removing the dquot logitem
1322  * from the AIL if it has not been re-logged, and unlocking the dquot's
1323  * flush lock. This behavior is very similar to that of inodes..
1324  */
1325 /*ARGSUSED*/
1326 STATIC void
xfs_qm_dqflush_done(xfs_buf_t * bp,xfs_dq_logitem_t * qip)1327 xfs_qm_dqflush_done(
1328 	xfs_buf_t		*bp,
1329 	xfs_dq_logitem_t	*qip)
1330 {
1331 	xfs_dquot_t		*dqp;
1332 	struct xfs_ail		*ailp;
1333 
1334 	dqp = qip->qli_dquot;
1335 	ailp = qip->qli_item.li_ailp;
1336 
1337 	/*
1338 	 * We only want to pull the item from the AIL if its
1339 	 * location in the log has not changed since we started the flush.
1340 	 * Thus, we only bother if the dquot's lsn has
1341 	 * not changed. First we check the lsn outside the lock
1342 	 * since it's cheaper, and then we recheck while
1343 	 * holding the lock before removing the dquot from the AIL.
1344 	 */
1345 	if ((qip->qli_item.li_flags & XFS_LI_IN_AIL) &&
1346 	    qip->qli_item.li_lsn == qip->qli_flush_lsn) {
1347 
1348 		/* xfs_trans_ail_delete() drops the AIL lock. */
1349 		spin_lock(&ailp->xa_lock);
1350 		if (qip->qli_item.li_lsn == qip->qli_flush_lsn)
1351 			xfs_trans_ail_delete(ailp, (xfs_log_item_t*)qip);
1352 		else
1353 			spin_unlock(&ailp->xa_lock);
1354 	}
1355 
1356 	/*
1357 	 * Release the dq's flush lock since we're done with it.
1358 	 */
1359 	xfs_dqfunlock(dqp);
1360 }
1361 
1362 int
xfs_qm_dqlock_nowait(xfs_dquot_t * dqp)1363 xfs_qm_dqlock_nowait(
1364 	xfs_dquot_t *dqp)
1365 {
1366 	return mutex_trylock(&dqp->q_qlock);
1367 }
1368 
1369 void
xfs_dqlock(xfs_dquot_t * dqp)1370 xfs_dqlock(
1371 	xfs_dquot_t *dqp)
1372 {
1373 	mutex_lock(&dqp->q_qlock);
1374 }
1375 
1376 void
xfs_dqunlock(xfs_dquot_t * dqp)1377 xfs_dqunlock(
1378 	xfs_dquot_t *dqp)
1379 {
1380 	mutex_unlock(&(dqp->q_qlock));
1381 	if (dqp->q_logitem.qli_dquot == dqp) {
1382 		/* Once was dqp->q_mount, but might just have been cleared */
1383 		xfs_trans_unlocked_item(dqp->q_logitem.qli_item.li_ailp,
1384 					(xfs_log_item_t*)&(dqp->q_logitem));
1385 	}
1386 }
1387 
1388 
1389 void
xfs_dqunlock_nonotify(xfs_dquot_t * dqp)1390 xfs_dqunlock_nonotify(
1391 	xfs_dquot_t *dqp)
1392 {
1393 	mutex_unlock(&(dqp->q_qlock));
1394 }
1395 
1396 /*
1397  * Lock two xfs_dquot structures.
1398  *
1399  * To avoid deadlocks we always lock the quota structure with
1400  * the lowerd id first.
1401  */
1402 void
xfs_dqlock2(xfs_dquot_t * d1,xfs_dquot_t * d2)1403 xfs_dqlock2(
1404 	xfs_dquot_t	*d1,
1405 	xfs_dquot_t	*d2)
1406 {
1407 	if (d1 && d2) {
1408 		ASSERT(d1 != d2);
1409 		if (be32_to_cpu(d1->q_core.d_id) >
1410 		    be32_to_cpu(d2->q_core.d_id)) {
1411 			mutex_lock(&d2->q_qlock);
1412 			mutex_lock_nested(&d1->q_qlock, XFS_QLOCK_NESTED);
1413 		} else {
1414 			mutex_lock(&d1->q_qlock);
1415 			mutex_lock_nested(&d2->q_qlock, XFS_QLOCK_NESTED);
1416 		}
1417 	} else if (d1) {
1418 		mutex_lock(&d1->q_qlock);
1419 	} else if (d2) {
1420 		mutex_lock(&d2->q_qlock);
1421 	}
1422 }
1423 
1424 
1425 /*
1426  * Take a dquot out of the mount's dqlist as well as the hashlist.
1427  * This is called via unmount as well as quotaoff, and the purge
1428  * will always succeed unless there are soft (temp) references
1429  * outstanding.
1430  *
1431  * This returns 0 if it was purged, 1 if it wasn't. It's not an error code
1432  * that we're returning! XXXsup - not cool.
1433  */
1434 /* ARGSUSED */
1435 int
xfs_qm_dqpurge(xfs_dquot_t * dqp)1436 xfs_qm_dqpurge(
1437 	xfs_dquot_t	*dqp)
1438 {
1439 	xfs_dqhash_t	*thishash;
1440 	xfs_mount_t	*mp = dqp->q_mount;
1441 
1442 	ASSERT(XFS_QM_IS_MPLIST_LOCKED(mp));
1443 	ASSERT(XFS_DQ_IS_HASH_LOCKED(dqp->q_hash));
1444 
1445 	xfs_dqlock(dqp);
1446 	/*
1447 	 * We really can't afford to purge a dquot that is
1448 	 * referenced, because these are hard refs.
1449 	 * It shouldn't happen in general because we went thru _all_ inodes in
1450 	 * dqrele_all_inodes before calling this and didn't let the mountlock go.
1451 	 * However it is possible that we have dquots with temporary
1452 	 * references that are not attached to an inode. e.g. see xfs_setattr().
1453 	 */
1454 	if (dqp->q_nrefs != 0) {
1455 		xfs_dqunlock(dqp);
1456 		XFS_DQ_HASH_UNLOCK(dqp->q_hash);
1457 		return (1);
1458 	}
1459 
1460 	ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1461 
1462 	/*
1463 	 * If we're turning off quotas, we have to make sure that, for
1464 	 * example, we don't delete quota disk blocks while dquots are
1465 	 * in the process of getting written to those disk blocks.
1466 	 * This dquot might well be on AIL, and we can't leave it there
1467 	 * if we're turning off quotas. Basically, we need this flush
1468 	 * lock, and are willing to block on it.
1469 	 */
1470 	if (!xfs_dqflock_nowait(dqp)) {
1471 		/*
1472 		 * Block on the flush lock after nudging dquot buffer,
1473 		 * if it is incore.
1474 		 */
1475 		xfs_qm_dqflock_pushbuf_wait(dqp);
1476 	}
1477 
1478 	/*
1479 	 * XXXIf we're turning this type of quotas off, we don't care
1480 	 * about the dirty metadata sitting in this dquot. OTOH, if
1481 	 * we're unmounting, we do care, so we flush it and wait.
1482 	 */
1483 	if (XFS_DQ_IS_DIRTY(dqp)) {
1484 		int	error;
1485 		xfs_dqtrace_entry(dqp, "DQPURGE ->DQFLUSH: DQDIRTY");
1486 		/* dqflush unlocks dqflock */
1487 		/*
1488 		 * Given that dqpurge is a very rare occurrence, it is OK
1489 		 * that we're holding the hashlist and mplist locks
1490 		 * across the disk write. But, ... XXXsup
1491 		 *
1492 		 * We don't care about getting disk errors here. We need
1493 		 * to purge this dquot anyway, so we go ahead regardless.
1494 		 */
1495 		error = xfs_qm_dqflush(dqp, XFS_QMOPT_SYNC);
1496 		if (error)
1497 			xfs_fs_cmn_err(CE_WARN, mp,
1498 				"xfs_qm_dqpurge: dquot %p flush failed", dqp);
1499 		xfs_dqflock(dqp);
1500 	}
1501 	ASSERT(atomic_read(&dqp->q_pincount) == 0);
1502 	ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1503 	       !(dqp->q_logitem.qli_item.li_flags & XFS_LI_IN_AIL));
1504 
1505 	thishash = dqp->q_hash;
1506 	XQM_HASHLIST_REMOVE(thishash, dqp);
1507 	XQM_MPLIST_REMOVE(&(XFS_QI_MPL_LIST(mp)), dqp);
1508 	/*
1509 	 * XXX Move this to the front of the freelist, if we can get the
1510 	 * freelist lock.
1511 	 */
1512 	ASSERT(XFS_DQ_IS_ON_FREELIST(dqp));
1513 
1514 	dqp->q_mount = NULL;
1515 	dqp->q_hash = NULL;
1516 	dqp->dq_flags = XFS_DQ_INACTIVE;
1517 	memset(&dqp->q_core, 0, sizeof(dqp->q_core));
1518 	xfs_dqfunlock(dqp);
1519 	xfs_dqunlock(dqp);
1520 	XFS_DQ_HASH_UNLOCK(thishash);
1521 	return (0);
1522 }
1523 
1524 
1525 #ifdef QUOTADEBUG
1526 void
xfs_qm_dqprint(xfs_dquot_t * dqp)1527 xfs_qm_dqprint(xfs_dquot_t *dqp)
1528 {
1529 	cmn_err(CE_DEBUG, "-----------KERNEL DQUOT----------------");
1530 	cmn_err(CE_DEBUG, "---- dquotID =  %d",
1531 		(int)be32_to_cpu(dqp->q_core.d_id));
1532 	cmn_err(CE_DEBUG, "---- type    =  %s", DQFLAGTO_TYPESTR(dqp));
1533 	cmn_err(CE_DEBUG, "---- fs      =  0x%p", dqp->q_mount);
1534 	cmn_err(CE_DEBUG, "---- blkno   =  0x%x", (int) dqp->q_blkno);
1535 	cmn_err(CE_DEBUG, "---- boffset =  0x%x", (int) dqp->q_bufoffset);
1536 	cmn_err(CE_DEBUG, "---- blkhlimit =  %Lu (0x%x)",
1537 		be64_to_cpu(dqp->q_core.d_blk_hardlimit),
1538 		(int)be64_to_cpu(dqp->q_core.d_blk_hardlimit));
1539 	cmn_err(CE_DEBUG, "---- blkslimit =  %Lu (0x%x)",
1540 		be64_to_cpu(dqp->q_core.d_blk_softlimit),
1541 		(int)be64_to_cpu(dqp->q_core.d_blk_softlimit));
1542 	cmn_err(CE_DEBUG, "---- inohlimit =  %Lu (0x%x)",
1543 		be64_to_cpu(dqp->q_core.d_ino_hardlimit),
1544 		(int)be64_to_cpu(dqp->q_core.d_ino_hardlimit));
1545 	cmn_err(CE_DEBUG, "---- inoslimit =  %Lu (0x%x)",
1546 		be64_to_cpu(dqp->q_core.d_ino_softlimit),
1547 		(int)be64_to_cpu(dqp->q_core.d_ino_softlimit));
1548 	cmn_err(CE_DEBUG, "---- bcount  =  %Lu (0x%x)",
1549 		be64_to_cpu(dqp->q_core.d_bcount),
1550 		(int)be64_to_cpu(dqp->q_core.d_bcount));
1551 	cmn_err(CE_DEBUG, "---- icount  =  %Lu (0x%x)",
1552 		be64_to_cpu(dqp->q_core.d_icount),
1553 		(int)be64_to_cpu(dqp->q_core.d_icount));
1554 	cmn_err(CE_DEBUG, "---- btimer  =  %d",
1555 		(int)be32_to_cpu(dqp->q_core.d_btimer));
1556 	cmn_err(CE_DEBUG, "---- itimer  =  %d",
1557 		(int)be32_to_cpu(dqp->q_core.d_itimer));
1558 	cmn_err(CE_DEBUG, "---------------------------");
1559 }
1560 #endif
1561 
1562 /*
1563  * Give the buffer a little push if it is incore and
1564  * wait on the flush lock.
1565  */
1566 void
xfs_qm_dqflock_pushbuf_wait(xfs_dquot_t * dqp)1567 xfs_qm_dqflock_pushbuf_wait(
1568 	xfs_dquot_t	*dqp)
1569 {
1570 	xfs_buf_t	*bp;
1571 
1572 	/*
1573 	 * Check to see if the dquot has been flushed delayed
1574 	 * write.  If so, grab its buffer and send it
1575 	 * out immediately.  We'll be able to acquire
1576 	 * the flush lock when the I/O completes.
1577 	 */
1578 	bp = xfs_incore(dqp->q_mount->m_ddev_targp, dqp->q_blkno,
1579 		    XFS_QI_DQCHUNKLEN(dqp->q_mount),
1580 		    XFS_INCORE_TRYLOCK);
1581 	if (bp != NULL) {
1582 		if (XFS_BUF_ISDELAYWRITE(bp)) {
1583 			int	error;
1584 			if (XFS_BUF_ISPINNED(bp)) {
1585 				xfs_log_force(dqp->q_mount,
1586 					      (xfs_lsn_t)0,
1587 					      XFS_LOG_FORCE);
1588 			}
1589 			error = xfs_bawrite(dqp->q_mount, bp);
1590 			if (error)
1591 				xfs_fs_cmn_err(CE_WARN, dqp->q_mount,
1592 					"xfs_qm_dqflock_pushbuf_wait: "
1593 					"pushbuf error %d on dqp %p, bp %p",
1594 					error, dqp, bp);
1595 		} else {
1596 			xfs_buf_relse(bp);
1597 		}
1598 	}
1599 	xfs_dqflock(dqp);
1600 }
1601