xfs: decouple log and transaction headers
[deliverable/linux.git] / fs / xfs / xfs_trans_dquot.c
1 /*
2 * Copyright (c) 2000-2002 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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_alloc.h"
27 #include "xfs_quota.h"
28 #include "xfs_mount.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_inode.h"
31 #include "xfs_itable.h"
32 #include "xfs_bmap.h"
33 #include "xfs_rtalloc.h"
34 #include "xfs_error.h"
35 #include "xfs_attr.h"
36 #include "xfs_trans.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_trans_priv.h"
39 #include "xfs_qm.h"
40
41 STATIC 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 */
48 void
49 xfs_trans_dqjoin(
50 xfs_trans_t *tp,
51 xfs_dquot_t *dqp)
52 {
53 ASSERT(dqp->q_transp != tp);
54 ASSERT(XFS_DQ_IS_LOCKED(dqp));
55 ASSERT(dqp->q_logitem.qli_dquot == dqp);
56
57 /*
58 * Get a log_item_desc to point at the new item.
59 */
60 xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
61
62 /*
63 * Initialize d_transp so we can later determine if this dquot is
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 */
80 void
81 xfs_trans_log_dquot(
82 xfs_trans_t *tp,
83 xfs_dquot_t *dqp)
84 {
85 ASSERT(dqp->q_transp == tp);
86 ASSERT(XFS_DQ_IS_LOCKED(dqp));
87
88 tp->t_flags |= XFS_TRANS_DIRTY;
89 dqp->q_logitem.qli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
90 }
91
92 /*
93 * Carry forward whatever is left of the quota blk reservation to
94 * the spanky new transaction
95 */
96 void
97 xfs_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);
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
117 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
118 oqa = otp->t_dqinfo->dqs[j];
119 nqa = ntp->t_dqinfo->dqs[j];
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 }
144 }
145 }
146
147 /*
148 * Wrap around mod_dquot to account for both user and group quotas.
149 */
150 void
151 xfs_trans_mod_dquot_byino(
152 xfs_trans_t *tp,
153 xfs_inode_t *ip,
154 uint field,
155 long delta)
156 {
157 xfs_mount_t *mp = tp->t_mountp;
158
159 if (!XFS_IS_QUOTA_RUNNING(mp) ||
160 !XFS_IS_QUOTA_ON(mp) ||
161 xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
162 return;
163
164 if (tp->t_dqinfo == NULL)
165 xfs_trans_alloc_dqinfo(tp);
166
167 if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
168 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
169 if (XFS_IS_GQUOTA_ON(mp) && ip->i_gdquot)
170 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
171 if (XFS_IS_PQUOTA_ON(mp) && ip->i_pdquot)
172 (void) xfs_trans_mod_dquot(tp, ip->i_pdquot, field, delta);
173 }
174
175 STATIC struct xfs_dqtrx *
176 xfs_trans_get_dqtrx(
177 struct xfs_trans *tp,
178 struct xfs_dquot *dqp)
179 {
180 int i;
181 struct xfs_dqtrx *qa;
182
183 if (XFS_QM_ISUDQ(dqp))
184 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
185 else if (XFS_QM_ISGDQ(dqp))
186 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
187 else if (XFS_QM_ISPDQ(dqp))
188 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_PRJ];
189 else
190 return NULL;
191
192 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
193 if (qa[i].qt_dquot == NULL ||
194 qa[i].qt_dquot == dqp)
195 return &qa[i];
196 }
197
198 return NULL;
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 */
207 void
208 xfs_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);
217 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
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 /*
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.
307 */
308 STATIC void
309 xfs_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 */
334 void
335 xfs_trans_apply_dquot_deltas(
336 struct xfs_trans *tp)
337 {
338 int i, j;
339 struct xfs_dquot *dqp;
340 struct xfs_dqtrx *qtrx, *qa;
341 struct xfs_disk_dquot *d;
342 long totalbdelta;
343 long totalrtbdelta;
344
345 if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
346 return;
347
348 ASSERT(tp->t_dqinfo);
349 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
350 qa = tp->t_dqinfo->dqs[j];
351 if (qa[0].qt_dquot == NULL)
352 continue;
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));
369 ASSERT(dqp->q_transp == tp);
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;
393 #ifdef DEBUG
394 if (totalbdelta < 0)
395 ASSERT(be64_to_cpu(d->d_bcount) >=
396 -totalbdelta);
397
398 if (totalrtbdelta < 0)
399 ASSERT(be64_to_cpu(d->d_rtbcount) >=
400 -totalrtbdelta);
401
402 if (qtrx->qt_icount_delta < 0)
403 ASSERT(be64_to_cpu(d->d_icount) >=
404 -qtrx->qt_icount_delta);
405 #endif
406 if (totalbdelta)
407 be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
408
409 if (qtrx->qt_icount_delta)
410 be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
411
412 if (totalrtbdelta)
413 be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
414
415 /*
416 * Get any default limits in use.
417 * Start/reset the timer(s) if needed.
418 */
419 if (d->d_id) {
420 xfs_qm_adjust_dqlimits(tp->t_mountp, dqp);
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) {
463 if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
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
496 ASSERT(dqp->q_res_bcount >=
497 be64_to_cpu(dqp->q_core.d_bcount));
498 ASSERT(dqp->q_res_icount >=
499 be64_to_cpu(dqp->q_core.d_icount));
500 ASSERT(dqp->q_res_rtbcount >=
501 be64_to_cpu(dqp->q_core.d_rtbcount));
502 }
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 */
513 void
514 xfs_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;
520 bool locked;
521
522 if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
523 return;
524
525 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
526 qa = tp->t_dqinfo->dqs[j];
527
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 */
541 locked = false;
542 if (qtrx->qt_blk_res) {
543 xfs_dqlock(dqp);
544 locked = true;
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);
551 locked = true;
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);
560 locked = true;
561 }
562 dqp->q_res_rtbcount -=
563 (xfs_qcnt_t)qtrx->qt_rtblk_res;
564 }
565 if (locked)
566 xfs_dqunlock(dqp);
567
568 }
569 }
570 }
571
572 STATIC void
573 xfs_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;
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);
586 }
587
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.
593 */
594 STATIC int
595 xfs_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 {
603 xfs_qcnt_t hardlimit;
604 xfs_qcnt_t softlimit;
605 time_t timer;
606 xfs_qwarncnt_t warns;
607 xfs_qwarncnt_t warnlimit;
608 xfs_qcnt_t total_count;
609 xfs_qcnt_t *resbcountp;
610 xfs_quotainfo_t *q = mp->m_quotainfo;
611
612
613 xfs_dqlock(dqp);
614
615 if (flags & XFS_TRANS_DQ_RES_BLKS) {
616 hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
617 if (!hardlimit)
618 hardlimit = q->qi_bhardlimit;
619 softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
620 if (!softlimit)
621 softlimit = q->qi_bsoftlimit;
622 timer = be32_to_cpu(dqp->q_core.d_btimer);
623 warns = be16_to_cpu(dqp->q_core.d_bwarns);
624 warnlimit = dqp->q_mount->m_quotainfo->qi_bwarnlimit;
625 resbcountp = &dqp->q_res_bcount;
626 } else {
627 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
628 hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
629 if (!hardlimit)
630 hardlimit = q->qi_rtbhardlimit;
631 softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
632 if (!softlimit)
633 softlimit = q->qi_rtbsoftlimit;
634 timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
635 warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
636 warnlimit = dqp->q_mount->m_quotainfo->qi_rtbwarnlimit;
637 resbcountp = &dqp->q_res_rtbcount;
638 }
639
640 if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
641 dqp->q_core.d_id &&
642 ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
643 (XFS_IS_GQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISGDQ(dqp)) ||
644 (XFS_IS_PQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISPDQ(dqp)))) {
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 */
651 total_count = *resbcountp + nblks;
652 if (hardlimit && total_count > hardlimit) {
653 xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
654 goto error_return;
655 }
656 if (softlimit && total_count > softlimit) {
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 }
666 }
667 if (ninos > 0) {
668 total_count = be64_to_cpu(dqp->q_core.d_icount) + ninos;
669 timer = be32_to_cpu(dqp->q_core.d_itimer);
670 warns = be16_to_cpu(dqp->q_core.d_iwarns);
671 warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
672 hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
673 if (!hardlimit)
674 hardlimit = q->qi_ihardlimit;
675 softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
676 if (!softlimit)
677 softlimit = q->qi_isoftlimit;
678
679 if (hardlimit && total_count > hardlimit) {
680 xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
681 goto error_return;
682 }
683 if (softlimit && total_count > softlimit) {
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 }
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 }
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));
725
726 xfs_dqunlock(dqp);
727 return 0;
728
729 error_return:
730 xfs_dqunlock(dqp);
731 if (flags & XFS_QMOPT_ENOSPC)
732 return ENOSPC;
733 return EDQUOT;
734 }
735
736
737 /*
738 * Given dquot(s), make disk block and/or inode reservations against them.
739 * The fact that this does the reservation against user, group and
740 * project quotas is important, because this follows a all-or-nothing
741 * approach.
742 *
743 * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
744 * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
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 */
749 int
750 xfs_trans_reserve_quota_bydquots(
751 struct xfs_trans *tp,
752 struct xfs_mount *mp,
753 struct xfs_dquot *udqp,
754 struct xfs_dquot *gdqp,
755 struct xfs_dquot *pdqp,
756 long nblks,
757 long ninos,
758 uint flags)
759 {
760 int error;
761
762 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
763 return 0;
764
765 if (tp && tp->t_dqinfo == NULL)
766 xfs_trans_alloc_dqinfo(tp);
767
768 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
769
770 if (udqp) {
771 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
772 (flags & ~XFS_QMOPT_ENOSPC));
773 if (error)
774 return error;
775 }
776
777 if (gdqp) {
778 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
779 if (error)
780 goto unwind_usr;
781 }
782
783 if (pdqp) {
784 error = xfs_trans_dqresv(tp, mp, pdqp, nblks, ninos, flags);
785 if (error)
786 goto unwind_grp;
787 }
788
789 /*
790 * Didn't change anything critical, so, no need to log
791 */
792 return 0;
793
794 unwind_grp:
795 flags |= XFS_QMOPT_FORCE_RES;
796 if (gdqp)
797 xfs_trans_dqresv(tp, mp, gdqp, -nblks, -ninos, flags);
798 unwind_usr:
799 flags |= XFS_QMOPT_FORCE_RES;
800 if (udqp)
801 xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
802 return error;
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.
810 */
811 int
812 xfs_trans_reserve_quota_nblks(
813 struct xfs_trans *tp,
814 struct xfs_inode *ip,
815 long nblks,
816 long ninos,
817 uint flags)
818 {
819 struct xfs_mount *mp = ip->i_mount;
820
821 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
822 return 0;
823 if (XFS_IS_PQUOTA_ON(mp))
824 flags |= XFS_QMOPT_ENOSPC;
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_QMOPT_ENOSPC)) ==
830 XFS_TRANS_DQ_RES_RTBLKS ||
831 (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
832 XFS_TRANS_DQ_RES_BLKS);
833
834 /*
835 * Reserve nblks against these dquots, with trans as the mediator.
836 */
837 return xfs_trans_reserve_quota_bydquots(tp, mp,
838 ip->i_udquot, ip->i_gdquot,
839 ip->i_pdquot,
840 nblks, ninos, flags);
841 }
842
843 /*
844 * This routine is called to allocate a quotaoff log item.
845 */
846 xfs_qoff_logitem_t *
847 xfs_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 */
862 xfs_trans_add_item(tp, &q->qql_item);
863 return q;
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 */
872 void
873 xfs_trans_log_quotaoff_item(
874 xfs_trans_t *tp,
875 xfs_qoff_logitem_t *qlp)
876 {
877 tp->t_flags |= XFS_TRANS_DIRTY;
878 qlp->qql_item.li_desc->lid_flags |= XFS_LID_DIRTY;
879 }
880
881 STATIC void
882 xfs_trans_alloc_dqinfo(
883 xfs_trans_t *tp)
884 {
885 tp->t_dqinfo = kmem_zone_zalloc(xfs_qm_dqtrxzone, KM_SLEEP);
886 }
887
888 void
889 xfs_trans_free_dqinfo(
890 xfs_trans_t *tp)
891 {
892 if (!tp->t_dqinfo)
893 return;
894 kmem_zone_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
895 tp->t_dqinfo = NULL;
896 }
This page took 0.059681 seconds and 5 git commands to generate.