xref: /openbmc/linux/fs/xfs/xfs_trans_dquot.c (revision b296a6d5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_quota.h"
17 #include "xfs_qm.h"
18 #include "xfs_trace.h"
19 
20 STATIC void	xfs_trans_alloc_dqinfo(xfs_trans_t *);
21 
22 /*
23  * Add the locked dquot to the transaction.
24  * The dquot must be locked, and it cannot be associated with any
25  * transaction.
26  */
27 void
28 xfs_trans_dqjoin(
29 	struct xfs_trans	*tp,
30 	struct xfs_dquot	*dqp)
31 {
32 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
33 	ASSERT(dqp->q_logitem.qli_dquot == dqp);
34 
35 	/*
36 	 * Get a log_item_desc to point at the new item.
37 	 */
38 	xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
39 }
40 
41 /*
42  * This is called to mark the dquot as needing
43  * to be logged when the transaction is committed.  The dquot must
44  * already be associated with the given transaction.
45  * Note that it marks the entire transaction as dirty. In the ordinary
46  * case, this gets called via xfs_trans_commit, after the transaction
47  * is already dirty. However, there's nothing stop this from getting
48  * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
49  * flag.
50  */
51 void
52 xfs_trans_log_dquot(
53 	struct xfs_trans	*tp,
54 	struct xfs_dquot	*dqp)
55 {
56 	ASSERT(XFS_DQ_IS_LOCKED(dqp));
57 
58 	/* Upgrade the dquot to bigtime format if possible. */
59 	if (dqp->q_id != 0 &&
60 	    xfs_sb_version_hasbigtime(&tp->t_mountp->m_sb) &&
61 	    !(dqp->q_type & XFS_DQTYPE_BIGTIME))
62 		dqp->q_type |= XFS_DQTYPE_BIGTIME;
63 
64 	tp->t_flags |= XFS_TRANS_DIRTY;
65 	set_bit(XFS_LI_DIRTY, &dqp->q_logitem.qli_item.li_flags);
66 }
67 
68 /*
69  * Carry forward whatever is left of the quota blk reservation to
70  * the spanky new transaction
71  */
72 void
73 xfs_trans_dup_dqinfo(
74 	struct xfs_trans	*otp,
75 	struct xfs_trans	*ntp)
76 {
77 	struct xfs_dqtrx	*oq, *nq;
78 	int			i, j;
79 	struct xfs_dqtrx	*oqa, *nqa;
80 	uint64_t		blk_res_used;
81 
82 	if (!otp->t_dqinfo)
83 		return;
84 
85 	xfs_trans_alloc_dqinfo(ntp);
86 
87 	/*
88 	 * Because the quota blk reservation is carried forward,
89 	 * it is also necessary to carry forward the DQ_DIRTY flag.
90 	 */
91 	if (otp->t_flags & XFS_TRANS_DQ_DIRTY)
92 		ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
93 
94 	for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
95 		oqa = otp->t_dqinfo->dqs[j];
96 		nqa = ntp->t_dqinfo->dqs[j];
97 		for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
98 			blk_res_used = 0;
99 
100 			if (oqa[i].qt_dquot == NULL)
101 				break;
102 			oq = &oqa[i];
103 			nq = &nqa[i];
104 
105 			if (oq->qt_blk_res && oq->qt_bcount_delta > 0)
106 				blk_res_used = oq->qt_bcount_delta;
107 
108 			nq->qt_dquot = oq->qt_dquot;
109 			nq->qt_bcount_delta = nq->qt_icount_delta = 0;
110 			nq->qt_rtbcount_delta = 0;
111 
112 			/*
113 			 * Transfer whatever is left of the reservations.
114 			 */
115 			nq->qt_blk_res = oq->qt_blk_res - blk_res_used;
116 			oq->qt_blk_res = blk_res_used;
117 
118 			nq->qt_rtblk_res = oq->qt_rtblk_res -
119 				oq->qt_rtblk_res_used;
120 			oq->qt_rtblk_res = oq->qt_rtblk_res_used;
121 
122 			nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
123 			oq->qt_ino_res = oq->qt_ino_res_used;
124 
125 		}
126 	}
127 }
128 
129 /*
130  * Wrap around mod_dquot to account for both user and group quotas.
131  */
132 void
133 xfs_trans_mod_dquot_byino(
134 	xfs_trans_t	*tp,
135 	xfs_inode_t	*ip,
136 	uint		field,
137 	int64_t		delta)
138 {
139 	xfs_mount_t	*mp = tp->t_mountp;
140 
141 	if (!XFS_IS_QUOTA_RUNNING(mp) ||
142 	    !XFS_IS_QUOTA_ON(mp) ||
143 	    xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
144 		return;
145 
146 	if (tp->t_dqinfo == NULL)
147 		xfs_trans_alloc_dqinfo(tp);
148 
149 	if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
150 		(void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
151 	if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
152 		(void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
153 	if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
154 		(void) xfs_trans_mod_dquot(tp, ip->i_pdquot, field, delta);
155 }
156 
157 STATIC struct xfs_dqtrx *
158 xfs_trans_get_dqtrx(
159 	struct xfs_trans	*tp,
160 	struct xfs_dquot	*dqp)
161 {
162 	int			i;
163 	struct xfs_dqtrx	*qa;
164 
165 	switch (xfs_dquot_type(dqp)) {
166 	case XFS_DQTYPE_USER:
167 		qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
168 		break;
169 	case XFS_DQTYPE_GROUP:
170 		qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
171 		break;
172 	case XFS_DQTYPE_PROJ:
173 		qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
174 		break;
175 	default:
176 		return NULL;
177 	}
178 
179 	for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
180 		if (qa[i].qt_dquot == NULL ||
181 		    qa[i].qt_dquot == dqp)
182 			return &qa[i];
183 	}
184 
185 	return NULL;
186 }
187 
188 /*
189  * Make the changes in the transaction structure.
190  * The moral equivalent to xfs_trans_mod_sb().
191  * We don't touch any fields in the dquot, so we don't care
192  * if it's locked or not (most of the time it won't be).
193  */
194 void
195 xfs_trans_mod_dquot(
196 	struct xfs_trans	*tp,
197 	struct xfs_dquot	*dqp,
198 	uint			field,
199 	int64_t			delta)
200 {
201 	struct xfs_dqtrx	*qtrx;
202 
203 	ASSERT(tp);
204 	ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
205 	qtrx = NULL;
206 
207 	if (tp->t_dqinfo == NULL)
208 		xfs_trans_alloc_dqinfo(tp);
209 	/*
210 	 * Find either the first free slot or the slot that belongs
211 	 * to this dquot.
212 	 */
213 	qtrx = xfs_trans_get_dqtrx(tp, dqp);
214 	ASSERT(qtrx);
215 	if (qtrx->qt_dquot == NULL)
216 		qtrx->qt_dquot = dqp;
217 
218 	if (delta) {
219 		trace_xfs_trans_mod_dquot_before(qtrx);
220 		trace_xfs_trans_mod_dquot(tp, dqp, field, delta);
221 	}
222 
223 	switch (field) {
224 
225 		/*
226 		 * regular disk blk reservation
227 		 */
228 	      case XFS_TRANS_DQ_RES_BLKS:
229 		qtrx->qt_blk_res += delta;
230 		break;
231 
232 		/*
233 		 * inode reservation
234 		 */
235 	      case XFS_TRANS_DQ_RES_INOS:
236 		qtrx->qt_ino_res += delta;
237 		break;
238 
239 		/*
240 		 * disk blocks used.
241 		 */
242 	      case XFS_TRANS_DQ_BCOUNT:
243 		qtrx->qt_bcount_delta += delta;
244 		break;
245 
246 	      case XFS_TRANS_DQ_DELBCOUNT:
247 		qtrx->qt_delbcnt_delta += delta;
248 		break;
249 
250 		/*
251 		 * Inode Count
252 		 */
253 	      case XFS_TRANS_DQ_ICOUNT:
254 		if (qtrx->qt_ino_res && delta > 0) {
255 			qtrx->qt_ino_res_used += delta;
256 			ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
257 		}
258 		qtrx->qt_icount_delta += delta;
259 		break;
260 
261 		/*
262 		 * rtblk reservation
263 		 */
264 	      case XFS_TRANS_DQ_RES_RTBLKS:
265 		qtrx->qt_rtblk_res += delta;
266 		break;
267 
268 		/*
269 		 * rtblk count
270 		 */
271 	      case XFS_TRANS_DQ_RTBCOUNT:
272 		if (qtrx->qt_rtblk_res && delta > 0) {
273 			qtrx->qt_rtblk_res_used += delta;
274 			ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
275 		}
276 		qtrx->qt_rtbcount_delta += delta;
277 		break;
278 
279 	      case XFS_TRANS_DQ_DELRTBCOUNT:
280 		qtrx->qt_delrtb_delta += delta;
281 		break;
282 
283 	      default:
284 		ASSERT(0);
285 	}
286 
287 	if (delta)
288 		trace_xfs_trans_mod_dquot_after(qtrx);
289 
290 	tp->t_flags |= XFS_TRANS_DQ_DIRTY;
291 }
292 
293 
294 /*
295  * Given an array of dqtrx structures, lock all the dquots associated and join
296  * them to the transaction, provided they have been modified.  We know that the
297  * highest number of dquots of one type - usr, grp and prj - involved in a
298  * transaction is 3 so we don't need to make this very generic.
299  */
300 STATIC void
301 xfs_trans_dqlockedjoin(
302 	struct xfs_trans	*tp,
303 	struct xfs_dqtrx	*q)
304 {
305 	ASSERT(q[0].qt_dquot != NULL);
306 	if (q[1].qt_dquot == NULL) {
307 		xfs_dqlock(q[0].qt_dquot);
308 		xfs_trans_dqjoin(tp, q[0].qt_dquot);
309 	} else {
310 		ASSERT(XFS_QM_TRANS_MAXDQS == 2);
311 		xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
312 		xfs_trans_dqjoin(tp, q[0].qt_dquot);
313 		xfs_trans_dqjoin(tp, q[1].qt_dquot);
314 	}
315 }
316 
317 /* Apply dqtrx changes to the quota reservation counters. */
318 static inline void
319 xfs_apply_quota_reservation_deltas(
320 	struct xfs_dquot_res	*res,
321 	uint64_t		reserved,
322 	int64_t			res_used,
323 	int64_t			count_delta)
324 {
325 	if (reserved != 0) {
326 		/*
327 		 * Subtle math here: If reserved > res_used (the normal case),
328 		 * we're simply subtracting the unused transaction quota
329 		 * reservation from the dquot reservation.
330 		 *
331 		 * If, however, res_used > reserved, then we have allocated
332 		 * more quota blocks than were reserved for the transaction.
333 		 * We must add that excess to the dquot reservation since it
334 		 * tracks (usage + resv) and by definition we didn't reserve
335 		 * that excess.
336 		 */
337 		res->reserved -= abs(reserved - res_used);
338 	} else if (count_delta != 0) {
339 		/*
340 		 * These blks were never reserved, either inside a transaction
341 		 * or outside one (in a delayed allocation). Also, this isn't
342 		 * always a negative number since we sometimes deliberately
343 		 * skip quota reservations.
344 		 */
345 		res->reserved += count_delta;
346 	}
347 }
348 
349 /*
350  * Called by xfs_trans_commit() and similar in spirit to
351  * xfs_trans_apply_sb_deltas().
352  * Go thru all the dquots belonging to this transaction and modify the
353  * INCORE dquot to reflect the actual usages.
354  * Unreserve just the reservations done by this transaction.
355  * dquot is still left locked at exit.
356  */
357 void
358 xfs_trans_apply_dquot_deltas(
359 	struct xfs_trans	*tp)
360 {
361 	int			i, j;
362 	struct xfs_dquot	*dqp;
363 	struct xfs_dqtrx	*qtrx, *qa;
364 	int64_t			totalbdelta;
365 	int64_t			totalrtbdelta;
366 
367 	if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
368 		return;
369 
370 	ASSERT(tp->t_dqinfo);
371 	for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
372 		qa = tp->t_dqinfo->dqs[j];
373 		if (qa[0].qt_dquot == NULL)
374 			continue;
375 
376 		/*
377 		 * Lock all of the dquots and join them to the transaction.
378 		 */
379 		xfs_trans_dqlockedjoin(tp, qa);
380 
381 		for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
382 			uint64_t	blk_res_used;
383 
384 			qtrx = &qa[i];
385 			/*
386 			 * The array of dquots is filled
387 			 * sequentially, not sparsely.
388 			 */
389 			if ((dqp = qtrx->qt_dquot) == NULL)
390 				break;
391 
392 			ASSERT(XFS_DQ_IS_LOCKED(dqp));
393 
394 			/*
395 			 * adjust the actual number of blocks used
396 			 */
397 
398 			/*
399 			 * The issue here is - sometimes we don't make a blkquota
400 			 * reservation intentionally to be fair to users
401 			 * (when the amount is small). On the other hand,
402 			 * delayed allocs do make reservations, but that's
403 			 * outside of a transaction, so we have no
404 			 * idea how much was really reserved.
405 			 * So, here we've accumulated delayed allocation blks and
406 			 * non-delay blks. The assumption is that the
407 			 * delayed ones are always reserved (outside of a
408 			 * transaction), and the others may or may not have
409 			 * quota reservations.
410 			 */
411 			totalbdelta = qtrx->qt_bcount_delta +
412 				qtrx->qt_delbcnt_delta;
413 			totalrtbdelta = qtrx->qt_rtbcount_delta +
414 				qtrx->qt_delrtb_delta;
415 
416 			if (totalbdelta != 0 || totalrtbdelta != 0 ||
417 			    qtrx->qt_icount_delta != 0) {
418 				trace_xfs_trans_apply_dquot_deltas_before(dqp);
419 				trace_xfs_trans_apply_dquot_deltas(qtrx);
420 			}
421 
422 #ifdef DEBUG
423 			if (totalbdelta < 0)
424 				ASSERT(dqp->q_blk.count >= -totalbdelta);
425 
426 			if (totalrtbdelta < 0)
427 				ASSERT(dqp->q_rtb.count >= -totalrtbdelta);
428 
429 			if (qtrx->qt_icount_delta < 0)
430 				ASSERT(dqp->q_ino.count >= -qtrx->qt_icount_delta);
431 #endif
432 			if (totalbdelta)
433 				dqp->q_blk.count += totalbdelta;
434 
435 			if (qtrx->qt_icount_delta)
436 				dqp->q_ino.count += qtrx->qt_icount_delta;
437 
438 			if (totalrtbdelta)
439 				dqp->q_rtb.count += totalrtbdelta;
440 
441 			if (totalbdelta != 0 || totalrtbdelta != 0 ||
442 			    qtrx->qt_icount_delta != 0)
443 				trace_xfs_trans_apply_dquot_deltas_after(dqp);
444 
445 			/*
446 			 * Get any default limits in use.
447 			 * Start/reset the timer(s) if needed.
448 			 */
449 			if (dqp->q_id) {
450 				xfs_qm_adjust_dqlimits(dqp);
451 				xfs_qm_adjust_dqtimers(dqp);
452 			}
453 
454 			dqp->q_flags |= XFS_DQFLAG_DIRTY;
455 			/*
456 			 * add this to the list of items to get logged
457 			 */
458 			xfs_trans_log_dquot(tp, dqp);
459 			/*
460 			 * Take off what's left of the original reservation.
461 			 * In case of delayed allocations, there's no
462 			 * reservation that a transaction structure knows of.
463 			 */
464 			blk_res_used = max_t(int64_t, 0, qtrx->qt_bcount_delta);
465 			xfs_apply_quota_reservation_deltas(&dqp->q_blk,
466 					qtrx->qt_blk_res, blk_res_used,
467 					qtrx->qt_bcount_delta);
468 
469 			/*
470 			 * Adjust the RT reservation.
471 			 */
472 			xfs_apply_quota_reservation_deltas(&dqp->q_rtb,
473 					qtrx->qt_rtblk_res,
474 					qtrx->qt_rtblk_res_used,
475 					qtrx->qt_rtbcount_delta);
476 
477 			/*
478 			 * Adjust the inode reservation.
479 			 */
480 			ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
481 			xfs_apply_quota_reservation_deltas(&dqp->q_ino,
482 					qtrx->qt_ino_res,
483 					qtrx->qt_ino_res_used,
484 					qtrx->qt_icount_delta);
485 
486 			ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
487 			ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
488 			ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count);
489 		}
490 	}
491 }
492 
493 /*
494  * Release the reservations, and adjust the dquots accordingly.
495  * This is called only when the transaction is being aborted. If by
496  * any chance we have done dquot modifications incore (ie. deltas) already,
497  * we simply throw those away, since that's the expected behavior
498  * when a transaction is curtailed without a commit.
499  */
500 void
501 xfs_trans_unreserve_and_mod_dquots(
502 	struct xfs_trans	*tp)
503 {
504 	int			i, j;
505 	struct xfs_dquot	*dqp;
506 	struct xfs_dqtrx	*qtrx, *qa;
507 	bool			locked;
508 
509 	if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
510 		return;
511 
512 	for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
513 		qa = tp->t_dqinfo->dqs[j];
514 
515 		for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
516 			qtrx = &qa[i];
517 			/*
518 			 * We assume that the array of dquots is filled
519 			 * sequentially, not sparsely.
520 			 */
521 			if ((dqp = qtrx->qt_dquot) == NULL)
522 				break;
523 			/*
524 			 * Unreserve the original reservation. We don't care
525 			 * about the number of blocks used field, or deltas.
526 			 * Also we don't bother to zero the fields.
527 			 */
528 			locked = false;
529 			if (qtrx->qt_blk_res) {
530 				xfs_dqlock(dqp);
531 				locked = true;
532 				dqp->q_blk.reserved -=
533 					(xfs_qcnt_t)qtrx->qt_blk_res;
534 			}
535 			if (qtrx->qt_ino_res) {
536 				if (!locked) {
537 					xfs_dqlock(dqp);
538 					locked = true;
539 				}
540 				dqp->q_ino.reserved -=
541 					(xfs_qcnt_t)qtrx->qt_ino_res;
542 			}
543 
544 			if (qtrx->qt_rtblk_res) {
545 				if (!locked) {
546 					xfs_dqlock(dqp);
547 					locked = true;
548 				}
549 				dqp->q_rtb.reserved -=
550 					(xfs_qcnt_t)qtrx->qt_rtblk_res;
551 			}
552 			if (locked)
553 				xfs_dqunlock(dqp);
554 
555 		}
556 	}
557 }
558 
559 STATIC void
560 xfs_quota_warn(
561 	struct xfs_mount	*mp,
562 	struct xfs_dquot	*dqp,
563 	int			type)
564 {
565 	enum quota_type		qtype;
566 
567 	switch (xfs_dquot_type(dqp)) {
568 	case XFS_DQTYPE_PROJ:
569 		qtype = PRJQUOTA;
570 		break;
571 	case XFS_DQTYPE_USER:
572 		qtype = USRQUOTA;
573 		break;
574 	case XFS_DQTYPE_GROUP:
575 		qtype = GRPQUOTA;
576 		break;
577 	default:
578 		return;
579 	}
580 
581 	quota_send_warning(make_kqid(&init_user_ns, qtype, dqp->q_id),
582 			   mp->m_super->s_dev, type);
583 }
584 
585 /*
586  * Decide if we can make an additional reservation against a quota resource.
587  * Returns an inode QUOTA_NL_ warning code and whether or not it's fatal.
588  *
589  * Note that we assume that the numeric difference between the inode and block
590  * warning codes will always be 3 since it's userspace ABI now, and will never
591  * decrease the quota reservation, so the *BELOW messages are irrelevant.
592  */
593 static inline int
594 xfs_dqresv_check(
595 	struct xfs_dquot_res	*res,
596 	struct xfs_quota_limits	*qlim,
597 	int64_t			delta,
598 	bool			*fatal)
599 {
600 	xfs_qcnt_t		hardlimit = res->hardlimit;
601 	xfs_qcnt_t		softlimit = res->softlimit;
602 	xfs_qcnt_t		total_count = res->reserved + delta;
603 
604 	BUILD_BUG_ON(QUOTA_NL_BHARDWARN     != QUOTA_NL_IHARDWARN + 3);
605 	BUILD_BUG_ON(QUOTA_NL_BSOFTLONGWARN != QUOTA_NL_ISOFTLONGWARN + 3);
606 	BUILD_BUG_ON(QUOTA_NL_BSOFTWARN     != QUOTA_NL_ISOFTWARN + 3);
607 
608 	*fatal = false;
609 	if (delta <= 0)
610 		return QUOTA_NL_NOWARN;
611 
612 	if (!hardlimit)
613 		hardlimit = qlim->hard;
614 	if (!softlimit)
615 		softlimit = qlim->soft;
616 
617 	if (hardlimit && total_count > hardlimit) {
618 		*fatal = true;
619 		return QUOTA_NL_IHARDWARN;
620 	}
621 
622 	if (softlimit && total_count > softlimit) {
623 		time64_t	now = ktime_get_real_seconds();
624 
625 		if ((res->timer != 0 && now > res->timer) ||
626 		    (res->warnings != 0 && res->warnings >= qlim->warn)) {
627 			*fatal = true;
628 			return QUOTA_NL_ISOFTLONGWARN;
629 		}
630 
631 		res->warnings++;
632 		return QUOTA_NL_ISOFTWARN;
633 	}
634 
635 	return QUOTA_NL_NOWARN;
636 }
637 
638 /*
639  * This reserves disk blocks and inodes against a dquot.
640  * Flags indicate if the dquot is to be locked here and also
641  * if the blk reservation is for RT or regular blocks.
642  * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
643  */
644 STATIC int
645 xfs_trans_dqresv(
646 	struct xfs_trans	*tp,
647 	struct xfs_mount	*mp,
648 	struct xfs_dquot	*dqp,
649 	int64_t			nblks,
650 	long			ninos,
651 	uint			flags)
652 {
653 	struct xfs_quotainfo	*q = mp->m_quotainfo;
654 	struct xfs_def_quota	*defq;
655 	struct xfs_dquot_res	*blkres;
656 	struct xfs_quota_limits	*qlim;
657 
658 	xfs_dqlock(dqp);
659 
660 	defq = xfs_get_defquota(q, xfs_dquot_type(dqp));
661 
662 	if (flags & XFS_TRANS_DQ_RES_BLKS) {
663 		blkres = &dqp->q_blk;
664 		qlim = &defq->blk;
665 	} else {
666 		blkres = &dqp->q_rtb;
667 		qlim = &defq->rtb;
668 	}
669 
670 	if ((flags & XFS_QMOPT_FORCE_RES) == 0 && dqp->q_id &&
671 	    xfs_dquot_is_enforced(dqp)) {
672 		int		quota_nl;
673 		bool		fatal;
674 
675 		/*
676 		 * dquot is locked already. See if we'd go over the hardlimit
677 		 * or exceed the timelimit if we'd reserve resources.
678 		 */
679 		quota_nl = xfs_dqresv_check(blkres, qlim, nblks, &fatal);
680 		if (quota_nl != QUOTA_NL_NOWARN) {
681 			/*
682 			 * Quota block warning codes are 3 more than the inode
683 			 * codes, which we check above.
684 			 */
685 			xfs_quota_warn(mp, dqp, quota_nl + 3);
686 			if (fatal)
687 				goto error_return;
688 		}
689 
690 		quota_nl = xfs_dqresv_check(&dqp->q_ino, &defq->ino, ninos,
691 				&fatal);
692 		if (quota_nl != QUOTA_NL_NOWARN) {
693 			xfs_quota_warn(mp, dqp, quota_nl);
694 			if (fatal)
695 				goto error_return;
696 		}
697 	}
698 
699 	/*
700 	 * Change the reservation, but not the actual usage.
701 	 * Note that q_blk.reserved = q_blk.count + resv
702 	 */
703 	blkres->reserved += (xfs_qcnt_t)nblks;
704 	dqp->q_ino.reserved += (xfs_qcnt_t)ninos;
705 
706 	/*
707 	 * note the reservation amt in the trans struct too,
708 	 * so that the transaction knows how much was reserved by
709 	 * it against this particular dquot.
710 	 * We don't do this when we are reserving for a delayed allocation,
711 	 * because we don't have the luxury of a transaction envelope then.
712 	 */
713 	if (tp) {
714 		ASSERT(tp->t_dqinfo);
715 		ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
716 		if (nblks != 0)
717 			xfs_trans_mod_dquot(tp, dqp,
718 					    flags & XFS_QMOPT_RESBLK_MASK,
719 					    nblks);
720 		if (ninos != 0)
721 			xfs_trans_mod_dquot(tp, dqp,
722 					    XFS_TRANS_DQ_RES_INOS,
723 					    ninos);
724 	}
725 	ASSERT(dqp->q_blk.reserved >= dqp->q_blk.count);
726 	ASSERT(dqp->q_rtb.reserved >= dqp->q_rtb.count);
727 	ASSERT(dqp->q_ino.reserved >= dqp->q_ino.count);
728 
729 	xfs_dqunlock(dqp);
730 	return 0;
731 
732 error_return:
733 	xfs_dqunlock(dqp);
734 	if (xfs_dquot_type(dqp) == XFS_DQTYPE_PROJ)
735 		return -ENOSPC;
736 	return -EDQUOT;
737 }
738 
739 
740 /*
741  * Given dquot(s), make disk block and/or inode reservations against them.
742  * The fact that this does the reservation against user, group and
743  * project quotas is important, because this follows a all-or-nothing
744  * approach.
745  *
746  * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
747  *	   XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT.  Used by pquota.
748  *	   XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
749  *	   XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
750  * dquots are unlocked on return, if they were not locked by caller.
751  */
752 int
753 xfs_trans_reserve_quota_bydquots(
754 	struct xfs_trans	*tp,
755 	struct xfs_mount	*mp,
756 	struct xfs_dquot	*udqp,
757 	struct xfs_dquot	*gdqp,
758 	struct xfs_dquot	*pdqp,
759 	int64_t			nblks,
760 	long			ninos,
761 	uint			flags)
762 {
763 	int		error;
764 
765 	if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
766 		return 0;
767 
768 	if (tp && tp->t_dqinfo == NULL)
769 		xfs_trans_alloc_dqinfo(tp);
770 
771 	ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
772 
773 	if (udqp) {
774 		error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos, flags);
775 		if (error)
776 			return error;
777 	}
778 
779 	if (gdqp) {
780 		error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
781 		if (error)
782 			goto unwind_usr;
783 	}
784 
785 	if (pdqp) {
786 		error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
787 		if (error)
788 			goto unwind_grp;
789 	}
790 
791 	/*
792 	 * Didn't change anything critical, so, no need to log
793 	 */
794 	return 0;
795 
796 unwind_grp:
797 	flags |= XFS_QMOPT_FORCE_RES;
798 	if (gdqp)
799 		xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
800 unwind_usr:
801 	flags |= XFS_QMOPT_FORCE_RES;
802 	if (udqp)
803 		xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
804 	return error;
805 }
806 
807 
808 /*
809  * Lock the dquot and change the reservation if we can.
810  * This doesn't change the actual usage, just the reservation.
811  * The inode sent in is locked.
812  */
813 int
814 xfs_trans_reserve_quota_nblks(
815 	struct xfs_trans	*tp,
816 	struct xfs_inode	*ip,
817 	int64_t			nblks,
818 	long			ninos,
819 	uint			flags)
820 {
821 	struct xfs_mount	*mp = ip->i_mount;
822 
823 	if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
824 		return 0;
825 
826 	ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
827 
828 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
829 	ASSERT((flags & ~(XFS_QMOPT_FORCE_RES)) == XFS_TRANS_DQ_RES_RTBLKS ||
830 	       (flags & ~(XFS_QMOPT_FORCE_RES)) == XFS_TRANS_DQ_RES_BLKS);
831 
832 	/*
833 	 * Reserve nblks against these dquots, with trans as the mediator.
834 	 */
835 	return xfs_trans_reserve_quota_bydquots(tp, mp,
836 						ip->i_udquot, ip->i_gdquot,
837 						ip->i_pdquot,
838 						nblks, ninos, flags);
839 }
840 
841 /*
842  * This routine is called to allocate a quotaoff log item.
843  */
844 struct xfs_qoff_logitem *
845 xfs_trans_get_qoff_item(
846 	struct xfs_trans	*tp,
847 	struct xfs_qoff_logitem	*startqoff,
848 	uint			flags)
849 {
850 	struct xfs_qoff_logitem	*q;
851 
852 	ASSERT(tp != NULL);
853 
854 	q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
855 	ASSERT(q != NULL);
856 
857 	/*
858 	 * Get a log_item_desc to point at the new item.
859 	 */
860 	xfs_trans_add_item(tp, &q->qql_item);
861 	return q;
862 }
863 
864 
865 /*
866  * This is called to mark the quotaoff logitem as needing
867  * to be logged when the transaction is committed.  The logitem must
868  * already be associated with the given transaction.
869  */
870 void
871 xfs_trans_log_quotaoff_item(
872 	struct xfs_trans	*tp,
873 	struct xfs_qoff_logitem	*qlp)
874 {
875 	tp->t_flags |= XFS_TRANS_DIRTY;
876 	set_bit(XFS_LI_DIRTY, &qlp->qql_item.li_flags);
877 }
878 
879 STATIC void
880 xfs_trans_alloc_dqinfo(
881 	xfs_trans_t	*tp)
882 {
883 	tp->t_dqinfo = kmem_cache_zalloc(xfs_qm_dqtrxzone,
884 					 GFP_KERNEL | __GFP_NOFAIL);
885 }
886 
887 void
888 xfs_trans_free_dqinfo(
889 	xfs_trans_t	*tp)
890 {
891 	if (!tp->t_dqinfo)
892 		return;
893 	kmem_cache_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
894 	tp->t_dqinfo = NULL;
895 }
896