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