blob: f441f836ca8b29338cbc5c1e5b8a7e4b9fad8677 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott4ce31212005-11-02 14:59:41 +11002 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott4ce31212005-11-02 14:59:41 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott4ce31212005-11-02 14:59:41 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott4ce31212005-11-02 14:59:41 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_sf.h"
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_ialloc.h"
39#include "xfs_itable.h"
40#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_rtalloc.h"
43#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_rw.h"
45#include "xfs_acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_qm.h"
50
51STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
52
53/*
54 * Add the locked dquot to the transaction.
55 * The dquot must be locked, and it cannot be associated with any
56 * transaction.
57 */
58void
59xfs_trans_dqjoin(
60 xfs_trans_t *tp,
61 xfs_dquot_t *dqp)
62{
63 xfs_dq_logitem_t *lp;
64
65 ASSERT(! XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
66 ASSERT(XFS_DQ_IS_LOCKED(dqp));
67 ASSERT(XFS_DQ_IS_LOGITEM_INITD(dqp));
68 lp = &dqp->q_logitem;
69
70 /*
71 * Get a log_item_desc to point at the new item.
72 */
73 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(lp));
74
75 /*
76 * Initialize i_transp so we can later determine if this dquot is
77 * associated with this transaction.
78 */
79 dqp->q_transp = tp;
80}
81
82
83/*
84 * This is called to mark the dquot as needing
85 * to be logged when the transaction is committed. The dquot must
86 * already be associated with the given transaction.
87 * Note that it marks the entire transaction as dirty. In the ordinary
88 * case, this gets called via xfs_trans_commit, after the transaction
89 * is already dirty. However, there's nothing stop this from getting
90 * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
91 * flag.
92 */
93void
94xfs_trans_log_dquot(
95 xfs_trans_t *tp,
96 xfs_dquot_t *dqp)
97{
98 xfs_log_item_desc_t *lidp;
99
100 ASSERT(XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
101 ASSERT(XFS_DQ_IS_LOCKED(dqp));
102
103 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(&dqp->q_logitem));
104 ASSERT(lidp != NULL);
105
106 tp->t_flags |= XFS_TRANS_DIRTY;
107 lidp->lid_flags |= XFS_LID_DIRTY;
108}
109
110/*
111 * Carry forward whatever is left of the quota blk reservation to
112 * the spanky new transaction
113 */
114STATIC void
115xfs_trans_dup_dqinfo(
116 xfs_trans_t *otp,
117 xfs_trans_t *ntp)
118{
119 xfs_dqtrx_t *oq, *nq;
120 int i,j;
121 xfs_dqtrx_t *oqa, *nqa;
122
123 if (!otp->t_dqinfo)
124 return;
125
126 xfs_trans_alloc_dqinfo(ntp);
127 oqa = otp->t_dqinfo->dqa_usrdquots;
128 nqa = ntp->t_dqinfo->dqa_usrdquots;
129
130 /*
131 * Because the quota blk reservation is carried forward,
132 * it is also necessary to carry forward the DQ_DIRTY flag.
133 */
134 if(otp->t_flags & XFS_TRANS_DQ_DIRTY)
135 ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
136
137 for (j = 0; j < 2; j++) {
138 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
139 if (oqa[i].qt_dquot == NULL)
140 break;
141 oq = &oqa[i];
142 nq = &nqa[i];
143
144 nq->qt_dquot = oq->qt_dquot;
145 nq->qt_bcount_delta = nq->qt_icount_delta = 0;
146 nq->qt_rtbcount_delta = 0;
147
148 /*
149 * Transfer whatever is left of the reservations.
150 */
151 nq->qt_blk_res = oq->qt_blk_res - oq->qt_blk_res_used;
152 oq->qt_blk_res = oq->qt_blk_res_used;
153
154 nq->qt_rtblk_res = oq->qt_rtblk_res -
155 oq->qt_rtblk_res_used;
156 oq->qt_rtblk_res = oq->qt_rtblk_res_used;
157
158 nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
159 oq->qt_ino_res = oq->qt_ino_res_used;
160
161 }
162 oqa = otp->t_dqinfo->dqa_grpdquots;
163 nqa = ntp->t_dqinfo->dqa_grpdquots;
164 }
165}
166
167/*
168 * Wrap around mod_dquot to account for both user and group quotas.
169 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000170STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171xfs_trans_mod_dquot_byino(
172 xfs_trans_t *tp,
173 xfs_inode_t *ip,
174 uint field,
175 long delta)
176{
177 xfs_mount_t *mp;
178
179 ASSERT(tp);
180 mp = tp->t_mountp;
181
182 if (!XFS_IS_QUOTA_ON(mp) ||
183 ip->i_ino == mp->m_sb.sb_uquotino ||
184 ip->i_ino == mp->m_sb.sb_gquotino)
185 return;
186
187 if (tp->t_dqinfo == NULL)
188 xfs_trans_alloc_dqinfo(tp);
189
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000190 if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000192 if (XFS_IS_OQUOTA_ON(mp) && ip->i_gdquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196STATIC xfs_dqtrx_t *
197xfs_trans_get_dqtrx(
198 xfs_trans_t *tp,
199 xfs_dquot_t *dqp)
200{
201 int i;
202 xfs_dqtrx_t *qa;
203
204 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
205 qa = XFS_QM_DQP_TO_DQACCT(tp, dqp);
206
207 if (qa[i].qt_dquot == NULL ||
208 qa[i].qt_dquot == dqp) {
209 return (&qa[i]);
210 }
211 }
212
213 return (NULL);
214}
215
216/*
217 * Make the changes in the transaction structure.
218 * The moral equivalent to xfs_trans_mod_sb().
219 * We don't touch any fields in the dquot, so we don't care
220 * if it's locked or not (most of the time it won't be).
221 */
222void
223xfs_trans_mod_dquot(
224 xfs_trans_t *tp,
225 xfs_dquot_t *dqp,
226 uint field,
227 long delta)
228{
229 xfs_dqtrx_t *qtrx;
230
231 ASSERT(tp);
232 qtrx = NULL;
233
234 if (tp->t_dqinfo == NULL)
235 xfs_trans_alloc_dqinfo(tp);
236 /*
237 * Find either the first free slot or the slot that belongs
238 * to this dquot.
239 */
240 qtrx = xfs_trans_get_dqtrx(tp, dqp);
241 ASSERT(qtrx);
242 if (qtrx->qt_dquot == NULL)
243 qtrx->qt_dquot = dqp;
244
245 switch (field) {
246
247 /*
248 * regular disk blk reservation
249 */
250 case XFS_TRANS_DQ_RES_BLKS:
251 qtrx->qt_blk_res += (ulong)delta;
252 break;
253
254 /*
255 * inode reservation
256 */
257 case XFS_TRANS_DQ_RES_INOS:
258 qtrx->qt_ino_res += (ulong)delta;
259 break;
260
261 /*
262 * disk blocks used.
263 */
264 case XFS_TRANS_DQ_BCOUNT:
265 if (qtrx->qt_blk_res && delta > 0) {
266 qtrx->qt_blk_res_used += (ulong)delta;
267 ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
268 }
269 qtrx->qt_bcount_delta += delta;
270 break;
271
272 case XFS_TRANS_DQ_DELBCOUNT:
273 qtrx->qt_delbcnt_delta += delta;
274 break;
275
276 /*
277 * Inode Count
278 */
279 case XFS_TRANS_DQ_ICOUNT:
280 if (qtrx->qt_ino_res && delta > 0) {
281 qtrx->qt_ino_res_used += (ulong)delta;
282 ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
283 }
284 qtrx->qt_icount_delta += delta;
285 break;
286
287 /*
288 * rtblk reservation
289 */
290 case XFS_TRANS_DQ_RES_RTBLKS:
291 qtrx->qt_rtblk_res += (ulong)delta;
292 break;
293
294 /*
295 * rtblk count
296 */
297 case XFS_TRANS_DQ_RTBCOUNT:
298 if (qtrx->qt_rtblk_res && delta > 0) {
299 qtrx->qt_rtblk_res_used += (ulong)delta;
300 ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
301 }
302 qtrx->qt_rtbcount_delta += delta;
303 break;
304
305 case XFS_TRANS_DQ_DELRTBCOUNT:
306 qtrx->qt_delrtb_delta += delta;
307 break;
308
309 default:
310 ASSERT(0);
311 }
312 tp->t_flags |= XFS_TRANS_DQ_DIRTY;
313}
314
315
316/*
317 * Given an array of dqtrx structures, lock all the dquots associated
318 * and join them to the transaction, provided they have been modified.
319 * We know that the highest number of dquots (of one type - usr OR grp),
320 * involved in a transaction is 2 and that both usr and grp combined - 3.
321 * So, we don't attempt to make this very generic.
322 */
323STATIC void
324xfs_trans_dqlockedjoin(
325 xfs_trans_t *tp,
326 xfs_dqtrx_t *q)
327{
328 ASSERT(q[0].qt_dquot != NULL);
329 if (q[1].qt_dquot == NULL) {
330 xfs_dqlock(q[0].qt_dquot);
331 xfs_trans_dqjoin(tp, q[0].qt_dquot);
332 } else {
333 ASSERT(XFS_QM_TRANS_MAXDQS == 2);
334 xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
335 xfs_trans_dqjoin(tp, q[0].qt_dquot);
336 xfs_trans_dqjoin(tp, q[1].qt_dquot);
337 }
338}
339
340
341/*
342 * Called by xfs_trans_commit() and similar in spirit to
343 * xfs_trans_apply_sb_deltas().
344 * Go thru all the dquots belonging to this transaction and modify the
345 * INCORE dquot to reflect the actual usages.
346 * Unreserve just the reservations done by this transaction.
347 * dquot is still left locked at exit.
348 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000349STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350xfs_trans_apply_dquot_deltas(
351 xfs_trans_t *tp)
352{
353 int i, j;
354 xfs_dquot_t *dqp;
355 xfs_dqtrx_t *qtrx, *qa;
356 xfs_disk_dquot_t *d;
357 long totalbdelta;
358 long totalrtbdelta;
359
360 if (! (tp->t_flags & XFS_TRANS_DQ_DIRTY))
361 return;
362
363 ASSERT(tp->t_dqinfo);
364 qa = tp->t_dqinfo->dqa_usrdquots;
365 for (j = 0; j < 2; j++) {
366 if (qa[0].qt_dquot == NULL) {
367 qa = tp->t_dqinfo->dqa_grpdquots;
368 continue;
369 }
370
371 /*
372 * Lock all of the dquots and join them to the transaction.
373 */
374 xfs_trans_dqlockedjoin(tp, qa);
375
376 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
377 qtrx = &qa[i];
378 /*
379 * The array of dquots is filled
380 * sequentially, not sparsely.
381 */
382 if ((dqp = qtrx->qt_dquot) == NULL)
383 break;
384
385 ASSERT(XFS_DQ_IS_LOCKED(dqp));
386 ASSERT(XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
387
388 /*
389 * adjust the actual number of blocks used
390 */
391 d = &dqp->q_core;
392
393 /*
394 * The issue here is - sometimes we don't make a blkquota
395 * reservation intentionally to be fair to users
396 * (when the amount is small). On the other hand,
397 * delayed allocs do make reservations, but that's
398 * outside of a transaction, so we have no
399 * idea how much was really reserved.
400 * So, here we've accumulated delayed allocation blks and
401 * non-delay blks. The assumption is that the
402 * delayed ones are always reserved (outside of a
403 * transaction), and the others may or may not have
404 * quota reservations.
405 */
406 totalbdelta = qtrx->qt_bcount_delta +
407 qtrx->qt_delbcnt_delta;
408 totalrtbdelta = qtrx->qt_rtbcount_delta +
409 qtrx->qt_delrtb_delta;
410#ifdef QUOTADEBUG
411 if (totalbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100412 ASSERT(be64_to_cpu(d->d_bcount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 (xfs_qcnt_t) -totalbdelta);
414
415 if (totalrtbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100416 ASSERT(be64_to_cpu(d->d_rtbcount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 (xfs_qcnt_t) -totalrtbdelta);
418
419 if (qtrx->qt_icount_delta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100420 ASSERT(be64_to_cpu(d->d_icount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 (xfs_qcnt_t) -qtrx->qt_icount_delta);
422#endif
423 if (totalbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800424 be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (qtrx->qt_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800427 be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (totalrtbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800430 be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * Get any default limits in use.
434 * Start/reset the timer(s) if needed.
435 */
436 if (d->d_id) {
437 xfs_qm_adjust_dqlimits(tp->t_mountp, d);
438 xfs_qm_adjust_dqtimers(tp->t_mountp, d);
439 }
440
441 dqp->dq_flags |= XFS_DQ_DIRTY;
442 /*
443 * add this to the list of items to get logged
444 */
445 xfs_trans_log_dquot(tp, dqp);
446 /*
447 * Take off what's left of the original reservation.
448 * In case of delayed allocations, there's no
449 * reservation that a transaction structure knows of.
450 */
451 if (qtrx->qt_blk_res != 0) {
452 if (qtrx->qt_blk_res != qtrx->qt_blk_res_used) {
453 if (qtrx->qt_blk_res >
454 qtrx->qt_blk_res_used)
455 dqp->q_res_bcount -= (xfs_qcnt_t)
456 (qtrx->qt_blk_res -
457 qtrx->qt_blk_res_used);
458 else
459 dqp->q_res_bcount -= (xfs_qcnt_t)
460 (qtrx->qt_blk_res_used -
461 qtrx->qt_blk_res);
462 }
463 } else {
464 /*
465 * These blks were never reserved, either inside
466 * a transaction or outside one (in a delayed
467 * allocation). Also, this isn't always a
468 * negative number since we sometimes
469 * deliberately skip quota reservations.
470 */
471 if (qtrx->qt_bcount_delta) {
472 dqp->q_res_bcount +=
473 (xfs_qcnt_t)qtrx->qt_bcount_delta;
474 }
475 }
476 /*
477 * Adjust the RT reservation.
478 */
479 if (qtrx->qt_rtblk_res != 0) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000480 if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (qtrx->qt_rtblk_res >
482 qtrx->qt_rtblk_res_used)
483 dqp->q_res_rtbcount -= (xfs_qcnt_t)
484 (qtrx->qt_rtblk_res -
485 qtrx->qt_rtblk_res_used);
486 else
487 dqp->q_res_rtbcount -= (xfs_qcnt_t)
488 (qtrx->qt_rtblk_res_used -
489 qtrx->qt_rtblk_res);
490 }
491 } else {
492 if (qtrx->qt_rtbcount_delta)
493 dqp->q_res_rtbcount +=
494 (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
495 }
496
497 /*
498 * Adjust the inode reservation.
499 */
500 if (qtrx->qt_ino_res != 0) {
501 ASSERT(qtrx->qt_ino_res >=
502 qtrx->qt_ino_res_used);
503 if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
504 dqp->q_res_icount -= (xfs_qcnt_t)
505 (qtrx->qt_ino_res -
506 qtrx->qt_ino_res_used);
507 } else {
508 if (qtrx->qt_icount_delta)
509 dqp->q_res_icount +=
510 (xfs_qcnt_t)qtrx->qt_icount_delta;
511 }
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ASSERT(dqp->q_res_bcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100514 be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ASSERT(dqp->q_res_icount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100516 be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 ASSERT(dqp->q_res_rtbcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100518 be64_to_cpu(dqp->q_core.d_rtbcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 /*
521 * Do the group quotas next
522 */
523 qa = tp->t_dqinfo->dqa_grpdquots;
524 }
525}
526
527/*
528 * Release the reservations, and adjust the dquots accordingly.
529 * This is called only when the transaction is being aborted. If by
530 * any chance we have done dquot modifications incore (ie. deltas) already,
531 * we simply throw those away, since that's the expected behavior
532 * when a transaction is curtailed without a commit.
533 */
534STATIC void
535xfs_trans_unreserve_and_mod_dquots(
536 xfs_trans_t *tp)
537{
538 int i, j;
539 xfs_dquot_t *dqp;
540 xfs_dqtrx_t *qtrx, *qa;
541 boolean_t locked;
542
543 if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
544 return;
545
546 qa = tp->t_dqinfo->dqa_usrdquots;
547
548 for (j = 0; j < 2; j++) {
549 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
550 qtrx = &qa[i];
551 /*
552 * We assume that the array of dquots is filled
553 * sequentially, not sparsely.
554 */
555 if ((dqp = qtrx->qt_dquot) == NULL)
556 break;
557 /*
558 * Unreserve the original reservation. We don't care
559 * about the number of blocks used field, or deltas.
560 * Also we don't bother to zero the fields.
561 */
562 locked = B_FALSE;
563 if (qtrx->qt_blk_res) {
564 xfs_dqlock(dqp);
565 locked = B_TRUE;
566 dqp->q_res_bcount -=
567 (xfs_qcnt_t)qtrx->qt_blk_res;
568 }
569 if (qtrx->qt_ino_res) {
570 if (!locked) {
571 xfs_dqlock(dqp);
572 locked = B_TRUE;
573 }
574 dqp->q_res_icount -=
575 (xfs_qcnt_t)qtrx->qt_ino_res;
576 }
577
578 if (qtrx->qt_rtblk_res) {
579 if (!locked) {
580 xfs_dqlock(dqp);
581 locked = B_TRUE;
582 }
583 dqp->q_res_rtbcount -=
584 (xfs_qcnt_t)qtrx->qt_rtblk_res;
585 }
586 if (locked)
587 xfs_dqunlock(dqp);
588
589 }
590 qa = tp->t_dqinfo->dqa_grpdquots;
591 }
592}
593
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000594STATIC int
595xfs_quota_error(uint flags)
596{
597 if (flags & XFS_QMOPT_ENOSPC)
598 return ENOSPC;
599 return EDQUOT;
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * This reserves disk blocks and inodes against a dquot.
604 * Flags indicate if the dquot is to be locked here and also
605 * if the blk reservation is for RT or regular blocks.
606 * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608STATIC int
609xfs_trans_dqresv(
610 xfs_trans_t *tp,
611 xfs_mount_t *mp,
612 xfs_dquot_t *dqp,
613 long nblks,
614 long ninos,
615 uint flags)
616{
617 int error;
618 xfs_qcnt_t hardlimit;
619 xfs_qcnt_t softlimit;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000620 time_t timer;
621 xfs_qwarncnt_t warns;
622 xfs_qwarncnt_t warnlimit;
623 xfs_qcnt_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 xfs_qcnt_t *resbcountp;
625 xfs_quotainfo_t *q = mp->m_quotainfo;
626
627 if (! (flags & XFS_QMOPT_DQLOCK)) {
628 xfs_dqlock(dqp);
629 }
630 ASSERT(XFS_DQ_IS_LOCKED(dqp));
631 if (flags & XFS_TRANS_DQ_RES_BLKS) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100632 hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!hardlimit)
634 hardlimit = q->qi_bhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100635 softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!softlimit)
637 softlimit = q->qi_bsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100638 timer = be32_to_cpu(dqp->q_core.d_btimer);
639 warns = be16_to_cpu(dqp->q_core.d_bwarns);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000640 warnlimit = XFS_QI_BWARNLIMIT(dqp->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 resbcountp = &dqp->q_res_bcount;
642 } else {
643 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100644 hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!hardlimit)
646 hardlimit = q->qi_rtbhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100647 softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!softlimit)
649 softlimit = q->qi_rtbsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100650 timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
651 warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000652 warnlimit = XFS_QI_RTBWARNLIMIT(dqp->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 resbcountp = &dqp->q_res_rtbcount;
654 }
655 error = 0;
656
657 if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
658 dqp->q_core.d_id &&
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000659 ((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))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662#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)) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000674 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 goto error_return;
676 }
677
678 if (softlimit > 0ULL &&
679 (softlimit <= nblks + *resbcountp)) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000680 if ((timer != 0 && get_seconds() > timer) ||
681 (warns != 0 && warns >= warnlimit)) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000682 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto error_return;
684 }
685 }
686 }
687 if (ninos > 0) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100688 count = be64_to_cpu(dqp->q_core.d_icount);
689 timer = be32_to_cpu(dqp->q_core.d_itimer);
690 warns = be16_to_cpu(dqp->q_core.d_iwarns);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000691 warnlimit = XFS_QI_IWARNLIMIT(dqp->q_mount);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100692 hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!hardlimit)
694 hardlimit = q->qi_ihardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100695 softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!softlimit)
697 softlimit = q->qi_isoftlimit;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000698 if (hardlimit > 0ULL && count >= hardlimit) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000699 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto error_return;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000701 } else if (softlimit > 0ULL && count >= softlimit) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000702 if ((timer != 0 && get_seconds() > timer) ||
703 (warns != 0 && warns >= warnlimit)) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000704 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto error_return;
706 }
707 }
708 }
709 }
710
711 /*
712 * Change the reservation, but not the actual usage.
713 * Note that q_res_bcount = q_core.d_bcount + resv
714 */
715 (*resbcountp) += (xfs_qcnt_t)nblks;
716 if (ninos != 0)
717 dqp->q_res_icount += (xfs_qcnt_t)ninos;
718
719 /*
720 * note the reservation amt in the trans struct too,
721 * so that the transaction knows how much was reserved by
722 * it against this particular dquot.
723 * We don't do this when we are reserving for a delayed allocation,
724 * because we don't have the luxury of a transaction envelope then.
725 */
726 if (tp) {
727 ASSERT(tp->t_dqinfo);
728 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
729 if (nblks != 0)
730 xfs_trans_mod_dquot(tp, dqp,
731 flags & XFS_QMOPT_RESBLK_MASK,
732 nblks);
733 if (ninos != 0)
734 xfs_trans_mod_dquot(tp, dqp,
735 XFS_TRANS_DQ_RES_INOS,
736 ninos);
737 }
Christoph Hellwig1149d962005-11-02 15:01:12 +1100738 ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
739 ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
740 ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742error_return:
743 if (! (flags & XFS_QMOPT_DQLOCK)) {
744 xfs_dqunlock(dqp);
745 }
746 return (error);
747}
748
749
750/*
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000751 * Given dquot(s), make disk block and/or inode reservations against them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * The fact that this does the reservation against both the usr and
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000753 * grp/prj quotas is important, because this follows a both-or-nothing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * approach.
755 *
756 * flags = XFS_QMOPT_DQLOCK indicate if dquot(s) need to be locked.
757 * XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000758 * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
760 * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
761 * dquots are unlocked on return, if they were not locked by caller.
762 */
763int
764xfs_trans_reserve_quota_bydquots(
765 xfs_trans_t *tp,
766 xfs_mount_t *mp,
767 xfs_dquot_t *udqp,
768 xfs_dquot_t *gdqp,
769 long nblks,
770 long ninos,
771 uint flags)
772{
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000773 int resvd = 0, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000775 if (!XFS_IS_QUOTA_ON(mp))
776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (tp && tp->t_dqinfo == NULL)
779 xfs_trans_alloc_dqinfo(tp);
780
781 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (udqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000784 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
785 (flags & ~XFS_QMOPT_ENOSPC));
786 if (error)
787 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 resvd = 1;
789 }
790
791 if (gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000792 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
793 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /*
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 }
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000802 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 }
805
806 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000807 * Didn't change anything critical, so, no need to log
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
818STATIC int
819xfs_trans_reserve_quota_nblks(
820 xfs_trans_t *tp,
821 xfs_mount_t *mp,
822 xfs_inode_t *ip,
823 long nblks,
824 long ninos,
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000825 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 int error;
828
829 if (!XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000830 return 0;
831 if (XFS_IS_PQUOTA_ON(mp))
832 flags |= XFS_QMOPT_ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
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));
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000839 ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
840 XFS_TRANS_DQ_RES_RTBLKS ||
841 (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
842 XFS_TRANS_DQ_RES_BLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
845 * Reserve nblks against these dquots, with trans as the mediator.
846 */
847 error = xfs_trans_reserve_quota_bydquots(tp, mp,
848 ip->i_udquot, ip->i_gdquot,
849 nblks, ninos,
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000850 flags);
851 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
855 * This routine is called to allocate a quotaoff log item.
856 */
857xfs_qoff_logitem_t *
858xfs_trans_get_qoff_item(
859 xfs_trans_t *tp,
860 xfs_qoff_logitem_t *startqoff,
861 uint flags)
862{
863 xfs_qoff_logitem_t *q;
864
865 ASSERT(tp != NULL);
866
867 q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
868 ASSERT(q != NULL);
869
870 /*
871 * Get a log_item_desc to point at the new item.
872 */
873 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)q);
874
875 return (q);
876}
877
878
879/*
880 * This is called to mark the quotaoff logitem as needing
881 * to be logged when the transaction is committed. The logitem must
882 * already be associated with the given transaction.
883 */
884void
885xfs_trans_log_quotaoff_item(
886 xfs_trans_t *tp,
887 xfs_qoff_logitem_t *qlp)
888{
889 xfs_log_item_desc_t *lidp;
890
891 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *)qlp);
892 ASSERT(lidp != NULL);
893
894 tp->t_flags |= XFS_TRANS_DIRTY;
895 lidp->lid_flags |= XFS_LID_DIRTY;
896}
897
898STATIC void
899xfs_trans_alloc_dqinfo(
900 xfs_trans_t *tp)
901{
902 (tp)->t_dqinfo = kmem_zone_zalloc(xfs_Gqm->qm_dqtrxzone, KM_SLEEP);
903}
904
905STATIC void
906xfs_trans_free_dqinfo(
907 xfs_trans_t *tp)
908{
909 if (!tp->t_dqinfo)
910 return;
911 kmem_zone_free(xfs_Gqm->qm_dqtrxzone, (tp)->t_dqinfo);
912 (tp)->t_dqinfo = NULL;
913}
914
915xfs_dqtrxops_t xfs_trans_dquot_ops = {
916 .qo_dup_dqinfo = xfs_trans_dup_dqinfo,
917 .qo_free_dqinfo = xfs_trans_free_dqinfo,
918 .qo_mod_dquot_byino = xfs_trans_mod_dquot_byino,
919 .qo_apply_dquot_deltas = xfs_trans_apply_dquot_deltas,
920 .qo_reserve_quota_nblks = xfs_trans_reserve_quota_nblks,
921 .qo_reserve_quota_bydquots = xfs_trans_reserve_quota_bydquots,
922 .qo_unreserve_and_mod_dquots = xfs_trans_unreserve_and_mod_dquots,
923};