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