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