blob: eafa7ab340856bd635215bef070e68a959b21282 [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 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200114void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115xfs_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 Hellwig7d095252009-06-08 15:33:32 +0200170void
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{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200177 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Christoph Hellwig7d095252009-06-08 15:33:32 +0200179 if (!XFS_IS_QUOTA_RUNNING(mp) ||
180 !XFS_IS_QUOTA_ON(mp) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ip->i_ino == mp->m_sb.sb_uquotino ||
182 ip->i_ino == mp->m_sb.sb_gquotino)
183 return;
184
185 if (tp->t_dqinfo == NULL)
186 xfs_trans_alloc_dqinfo(tp);
187
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000188 if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000190 if (XFS_IS_OQUOTA_ON(mp) && ip->i_gdquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194STATIC xfs_dqtrx_t *
195xfs_trans_get_dqtrx(
196 xfs_trans_t *tp,
197 xfs_dquot_t *dqp)
198{
199 int i;
200 xfs_dqtrx_t *qa;
201
202 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
203 qa = XFS_QM_DQP_TO_DQACCT(tp, dqp);
204
205 if (qa[i].qt_dquot == NULL ||
206 qa[i].qt_dquot == dqp) {
207 return (&qa[i]);
208 }
209 }
210
211 return (NULL);
212}
213
214/*
215 * Make the changes in the transaction structure.
216 * The moral equivalent to xfs_trans_mod_sb().
217 * We don't touch any fields in the dquot, so we don't care
218 * if it's locked or not (most of the time it won't be).
219 */
220void
221xfs_trans_mod_dquot(
222 xfs_trans_t *tp,
223 xfs_dquot_t *dqp,
224 uint field,
225 long delta)
226{
227 xfs_dqtrx_t *qtrx;
228
229 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200230 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 qtrx = NULL;
232
233 if (tp->t_dqinfo == NULL)
234 xfs_trans_alloc_dqinfo(tp);
235 /*
236 * Find either the first free slot or the slot that belongs
237 * to this dquot.
238 */
239 qtrx = xfs_trans_get_dqtrx(tp, dqp);
240 ASSERT(qtrx);
241 if (qtrx->qt_dquot == NULL)
242 qtrx->qt_dquot = dqp;
243
244 switch (field) {
245
246 /*
247 * regular disk blk reservation
248 */
249 case XFS_TRANS_DQ_RES_BLKS:
250 qtrx->qt_blk_res += (ulong)delta;
251 break;
252
253 /*
254 * inode reservation
255 */
256 case XFS_TRANS_DQ_RES_INOS:
257 qtrx->qt_ino_res += (ulong)delta;
258 break;
259
260 /*
261 * disk blocks used.
262 */
263 case XFS_TRANS_DQ_BCOUNT:
264 if (qtrx->qt_blk_res && delta > 0) {
265 qtrx->qt_blk_res_used += (ulong)delta;
266 ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
267 }
268 qtrx->qt_bcount_delta += delta;
269 break;
270
271 case XFS_TRANS_DQ_DELBCOUNT:
272 qtrx->qt_delbcnt_delta += delta;
273 break;
274
275 /*
276 * Inode Count
277 */
278 case XFS_TRANS_DQ_ICOUNT:
279 if (qtrx->qt_ino_res && delta > 0) {
280 qtrx->qt_ino_res_used += (ulong)delta;
281 ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
282 }
283 qtrx->qt_icount_delta += delta;
284 break;
285
286 /*
287 * rtblk reservation
288 */
289 case XFS_TRANS_DQ_RES_RTBLKS:
290 qtrx->qt_rtblk_res += (ulong)delta;
291 break;
292
293 /*
294 * rtblk count
295 */
296 case XFS_TRANS_DQ_RTBCOUNT:
297 if (qtrx->qt_rtblk_res && delta > 0) {
298 qtrx->qt_rtblk_res_used += (ulong)delta;
299 ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
300 }
301 qtrx->qt_rtbcount_delta += delta;
302 break;
303
304 case XFS_TRANS_DQ_DELRTBCOUNT:
305 qtrx->qt_delrtb_delta += delta;
306 break;
307
308 default:
309 ASSERT(0);
310 }
311 tp->t_flags |= XFS_TRANS_DQ_DIRTY;
312}
313
314
315/*
316 * Given an array of dqtrx structures, lock all the dquots associated
317 * and join them to the transaction, provided they have been modified.
318 * We know that the highest number of dquots (of one type - usr OR grp),
319 * involved in a transaction is 2 and that both usr and grp combined - 3.
320 * So, we don't attempt to make this very generic.
321 */
322STATIC void
323xfs_trans_dqlockedjoin(
324 xfs_trans_t *tp,
325 xfs_dqtrx_t *q)
326{
327 ASSERT(q[0].qt_dquot != NULL);
328 if (q[1].qt_dquot == NULL) {
329 xfs_dqlock(q[0].qt_dquot);
330 xfs_trans_dqjoin(tp, q[0].qt_dquot);
331 } else {
332 ASSERT(XFS_QM_TRANS_MAXDQS == 2);
333 xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
334 xfs_trans_dqjoin(tp, q[0].qt_dquot);
335 xfs_trans_dqjoin(tp, q[1].qt_dquot);
336 }
337}
338
339
340/*
341 * Called by xfs_trans_commit() and similar in spirit to
342 * xfs_trans_apply_sb_deltas().
343 * Go thru all the dquots belonging to this transaction and modify the
344 * INCORE dquot to reflect the actual usages.
345 * Unreserve just the reservations done by this transaction.
346 * dquot is still left locked at exit.
347 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200348void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349xfs_trans_apply_dquot_deltas(
350 xfs_trans_t *tp)
351{
352 int i, j;
353 xfs_dquot_t *dqp;
354 xfs_dqtrx_t *qtrx, *qa;
355 xfs_disk_dquot_t *d;
356 long totalbdelta;
357 long totalrtbdelta;
358
Christoph Hellwig7d095252009-06-08 15:33:32 +0200359 if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return;
361
362 ASSERT(tp->t_dqinfo);
363 qa = tp->t_dqinfo->dqa_usrdquots;
364 for (j = 0; j < 2; j++) {
365 if (qa[0].qt_dquot == NULL) {
366 qa = tp->t_dqinfo->dqa_grpdquots;
367 continue;
368 }
369
370 /*
371 * Lock all of the dquots and join them to the transaction.
372 */
373 xfs_trans_dqlockedjoin(tp, qa);
374
375 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
376 qtrx = &qa[i];
377 /*
378 * The array of dquots is filled
379 * sequentially, not sparsely.
380 */
381 if ((dqp = qtrx->qt_dquot) == NULL)
382 break;
383
384 ASSERT(XFS_DQ_IS_LOCKED(dqp));
385 ASSERT(XFS_DQ_IS_ADDEDTO_TRX(tp, dqp));
386
387 /*
388 * adjust the actual number of blocks used
389 */
390 d = &dqp->q_core;
391
392 /*
393 * The issue here is - sometimes we don't make a blkquota
394 * reservation intentionally to be fair to users
395 * (when the amount is small). On the other hand,
396 * delayed allocs do make reservations, but that's
397 * outside of a transaction, so we have no
398 * idea how much was really reserved.
399 * So, here we've accumulated delayed allocation blks and
400 * non-delay blks. The assumption is that the
401 * delayed ones are always reserved (outside of a
402 * transaction), and the others may or may not have
403 * quota reservations.
404 */
405 totalbdelta = qtrx->qt_bcount_delta +
406 qtrx->qt_delbcnt_delta;
407 totalrtbdelta = qtrx->qt_rtbcount_delta +
408 qtrx->qt_delrtb_delta;
409#ifdef QUOTADEBUG
410 if (totalbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100411 ASSERT(be64_to_cpu(d->d_bcount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 (xfs_qcnt_t) -totalbdelta);
413
414 if (totalrtbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100415 ASSERT(be64_to_cpu(d->d_rtbcount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 (xfs_qcnt_t) -totalrtbdelta);
417
418 if (qtrx->qt_icount_delta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100419 ASSERT(be64_to_cpu(d->d_icount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 (xfs_qcnt_t) -qtrx->qt_icount_delta);
421#endif
422 if (totalbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800423 be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (qtrx->qt_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800426 be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (totalrtbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800429 be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * Get any default limits in use.
433 * Start/reset the timer(s) if needed.
434 */
435 if (d->d_id) {
436 xfs_qm_adjust_dqlimits(tp->t_mountp, d);
437 xfs_qm_adjust_dqtimers(tp->t_mountp, d);
438 }
439
440 dqp->dq_flags |= XFS_DQ_DIRTY;
441 /*
442 * add this to the list of items to get logged
443 */
444 xfs_trans_log_dquot(tp, dqp);
445 /*
446 * Take off what's left of the original reservation.
447 * In case of delayed allocations, there's no
448 * reservation that a transaction structure knows of.
449 */
450 if (qtrx->qt_blk_res != 0) {
451 if (qtrx->qt_blk_res != qtrx->qt_blk_res_used) {
452 if (qtrx->qt_blk_res >
453 qtrx->qt_blk_res_used)
454 dqp->q_res_bcount -= (xfs_qcnt_t)
455 (qtrx->qt_blk_res -
456 qtrx->qt_blk_res_used);
457 else
458 dqp->q_res_bcount -= (xfs_qcnt_t)
459 (qtrx->qt_blk_res_used -
460 qtrx->qt_blk_res);
461 }
462 } else {
463 /*
464 * These blks were never reserved, either inside
465 * a transaction or outside one (in a delayed
466 * allocation). Also, this isn't always a
467 * negative number since we sometimes
468 * deliberately skip quota reservations.
469 */
470 if (qtrx->qt_bcount_delta) {
471 dqp->q_res_bcount +=
472 (xfs_qcnt_t)qtrx->qt_bcount_delta;
473 }
474 }
475 /*
476 * Adjust the RT reservation.
477 */
478 if (qtrx->qt_rtblk_res != 0) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000479 if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (qtrx->qt_rtblk_res >
481 qtrx->qt_rtblk_res_used)
482 dqp->q_res_rtbcount -= (xfs_qcnt_t)
483 (qtrx->qt_rtblk_res -
484 qtrx->qt_rtblk_res_used);
485 else
486 dqp->q_res_rtbcount -= (xfs_qcnt_t)
487 (qtrx->qt_rtblk_res_used -
488 qtrx->qt_rtblk_res);
489 }
490 } else {
491 if (qtrx->qt_rtbcount_delta)
492 dqp->q_res_rtbcount +=
493 (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
494 }
495
496 /*
497 * Adjust the inode reservation.
498 */
499 if (qtrx->qt_ino_res != 0) {
500 ASSERT(qtrx->qt_ino_res >=
501 qtrx->qt_ino_res_used);
502 if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
503 dqp->q_res_icount -= (xfs_qcnt_t)
504 (qtrx->qt_ino_res -
505 qtrx->qt_ino_res_used);
506 } else {
507 if (qtrx->qt_icount_delta)
508 dqp->q_res_icount +=
509 (xfs_qcnt_t)qtrx->qt_icount_delta;
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ASSERT(dqp->q_res_bcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100513 be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ASSERT(dqp->q_res_icount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100515 be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ASSERT(dqp->q_res_rtbcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100517 be64_to_cpu(dqp->q_core.d_rtbcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 /*
520 * Do the group quotas next
521 */
522 qa = tp->t_dqinfo->dqa_grpdquots;
523 }
524}
525
526/*
527 * Release the reservations, and adjust the dquots accordingly.
528 * This is called only when the transaction is being aborted. If by
529 * any chance we have done dquot modifications incore (ie. deltas) already,
530 * we simply throw those away, since that's the expected behavior
531 * when a transaction is curtailed without a commit.
532 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200533void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534xfs_trans_unreserve_and_mod_dquots(
535 xfs_trans_t *tp)
536{
537 int i, j;
538 xfs_dquot_t *dqp;
539 xfs_dqtrx_t *qtrx, *qa;
540 boolean_t locked;
541
542 if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
543 return;
544
545 qa = tp->t_dqinfo->dqa_usrdquots;
546
547 for (j = 0; j < 2; j++) {
548 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
549 qtrx = &qa[i];
550 /*
551 * We assume that the array of dquots is filled
552 * sequentially, not sparsely.
553 */
554 if ((dqp = qtrx->qt_dquot) == NULL)
555 break;
556 /*
557 * Unreserve the original reservation. We don't care
558 * about the number of blocks used field, or deltas.
559 * Also we don't bother to zero the fields.
560 */
561 locked = B_FALSE;
562 if (qtrx->qt_blk_res) {
563 xfs_dqlock(dqp);
564 locked = B_TRUE;
565 dqp->q_res_bcount -=
566 (xfs_qcnt_t)qtrx->qt_blk_res;
567 }
568 if (qtrx->qt_ino_res) {
569 if (!locked) {
570 xfs_dqlock(dqp);
571 locked = B_TRUE;
572 }
573 dqp->q_res_icount -=
574 (xfs_qcnt_t)qtrx->qt_ino_res;
575 }
576
577 if (qtrx->qt_rtblk_res) {
578 if (!locked) {
579 xfs_dqlock(dqp);
580 locked = B_TRUE;
581 }
582 dqp->q_res_rtbcount -=
583 (xfs_qcnt_t)qtrx->qt_rtblk_res;
584 }
585 if (locked)
586 xfs_dqunlock(dqp);
587
588 }
589 qa = tp->t_dqinfo->dqa_grpdquots;
590 }
591}
592
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000593STATIC int
594xfs_quota_error(uint flags)
595{
596 if (flags & XFS_QMOPT_ENOSPC)
597 return ENOSPC;
598 return EDQUOT;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
602 * This reserves disk blocks and inodes against a dquot.
603 * Flags indicate if the dquot is to be locked here and also
604 * if the blk reservation is for RT or regular blocks.
605 * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
607STATIC int
608xfs_trans_dqresv(
609 xfs_trans_t *tp,
610 xfs_mount_t *mp,
611 xfs_dquot_t *dqp,
612 long nblks,
613 long ninos,
614 uint flags)
615{
616 int error;
617 xfs_qcnt_t hardlimit;
618 xfs_qcnt_t softlimit;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000619 time_t timer;
620 xfs_qwarncnt_t warns;
621 xfs_qwarncnt_t warnlimit;
622 xfs_qcnt_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 xfs_qcnt_t *resbcountp;
624 xfs_quotainfo_t *q = mp->m_quotainfo;
625
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100626
627 xfs_dqlock(dqp);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (flags & XFS_TRANS_DQ_RES_BLKS) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100630 hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!hardlimit)
632 hardlimit = q->qi_bhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100633 softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!softlimit)
635 softlimit = q->qi_bsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100636 timer = be32_to_cpu(dqp->q_core.d_btimer);
637 warns = be16_to_cpu(dqp->q_core.d_bwarns);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000638 warnlimit = XFS_QI_BWARNLIMIT(dqp->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 resbcountp = &dqp->q_res_bcount;
640 } else {
641 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100642 hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!hardlimit)
644 hardlimit = q->qi_rtbhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100645 softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!softlimit)
647 softlimit = q->qi_rtbsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100648 timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
649 warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000650 warnlimit = XFS_QI_RTBWARNLIMIT(dqp->q_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 resbcountp = &dqp->q_res_rtbcount;
652 }
653 error = 0;
654
655 if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
656 dqp->q_core.d_id &&
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000657 ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
658 (XFS_IS_OQUOTA_ENFORCED(dqp->q_mount) &&
659 (XFS_QM_ISPDQ(dqp) || XFS_QM_ISGDQ(dqp))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660#ifdef QUOTADEBUG
661 cmn_err(CE_DEBUG, "BLK Res: nblks=%ld + resbcount=%Ld"
662 " > hardlimit=%Ld?", nblks, *resbcountp, hardlimit);
663#endif
664 if (nblks > 0) {
665 /*
666 * dquot is locked already. See if we'd go over the
667 * hardlimit or exceed the timelimit if we allocate
668 * nblks.
669 */
670 if (hardlimit > 0ULL &&
671 (hardlimit <= nblks + *resbcountp)) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000672 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto error_return;
674 }
675
676 if (softlimit > 0ULL &&
677 (softlimit <= nblks + *resbcountp)) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000678 if ((timer != 0 && get_seconds() > timer) ||
679 (warns != 0 && warns >= warnlimit)) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000680 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto error_return;
682 }
683 }
684 }
685 if (ninos > 0) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100686 count = be64_to_cpu(dqp->q_core.d_icount);
687 timer = be32_to_cpu(dqp->q_core.d_itimer);
688 warns = be16_to_cpu(dqp->q_core.d_iwarns);
Nathan Scott06d10dd2005-06-21 15:48:47 +1000689 warnlimit = XFS_QI_IWARNLIMIT(dqp->q_mount);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100690 hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!hardlimit)
692 hardlimit = q->qi_ihardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100693 softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!softlimit)
695 softlimit = q->qi_isoftlimit;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000696 if (hardlimit > 0ULL && count >= hardlimit) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000697 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto error_return;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000699 } else if (softlimit > 0ULL && count >= softlimit) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000700 if ((timer != 0 && get_seconds() > timer) ||
701 (warns != 0 && warns >= warnlimit)) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000702 error = xfs_quota_error(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto error_return;
704 }
705 }
706 }
707 }
708
709 /*
710 * Change the reservation, but not the actual usage.
711 * Note that q_res_bcount = q_core.d_bcount + resv
712 */
713 (*resbcountp) += (xfs_qcnt_t)nblks;
714 if (ninos != 0)
715 dqp->q_res_icount += (xfs_qcnt_t)ninos;
716
717 /*
718 * note the reservation amt in the trans struct too,
719 * so that the transaction knows how much was reserved by
720 * it against this particular dquot.
721 * We don't do this when we are reserving for a delayed allocation,
722 * because we don't have the luxury of a transaction envelope then.
723 */
724 if (tp) {
725 ASSERT(tp->t_dqinfo);
726 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
727 if (nblks != 0)
728 xfs_trans_mod_dquot(tp, dqp,
729 flags & XFS_QMOPT_RESBLK_MASK,
730 nblks);
731 if (ninos != 0)
732 xfs_trans_mod_dquot(tp, dqp,
733 XFS_TRANS_DQ_RES_INOS,
734 ninos);
735 }
Christoph Hellwig1149d962005-11-02 15:01:12 +1100736 ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
737 ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
738 ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740error_return:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100741 xfs_dqunlock(dqp);
742 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745
746/*
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000747 * Given dquot(s), make disk block and/or inode reservations against them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * The fact that this does the reservation against both the usr and
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000749 * grp/prj quotas is important, because this follows a both-or-nothing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * approach.
751 *
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100752 * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000753 * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
755 * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
756 * dquots are unlocked on return, if they were not locked by caller.
757 */
758int
759xfs_trans_reserve_quota_bydquots(
760 xfs_trans_t *tp,
761 xfs_mount_t *mp,
762 xfs_dquot_t *udqp,
763 xfs_dquot_t *gdqp,
764 long nblks,
765 long ninos,
766 uint flags)
767{
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000768 int resvd = 0, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Christoph Hellwig7d095252009-06-08 15:33:32 +0200770 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (tp && tp->t_dqinfo == NULL)
774 xfs_trans_alloc_dqinfo(tp);
775
776 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (udqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000779 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
780 (flags & ~XFS_QMOPT_ENOSPC));
781 if (error)
782 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 resvd = 1;
784 }
785
786 if (gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000787 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
788 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
790 * can't do it, so backout previous reservation
791 */
792 if (resvd) {
793 flags |= XFS_QMOPT_FORCE_RES;
794 xfs_trans_dqresv(tp, mp, udqp,
795 -nblks, -ninos, flags);
796 }
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000797 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 }
800
801 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000802 * Didn't change anything critical, so, no need to log
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807
808/*
809 * Lock the dquot and change the reservation if we can.
810 * This doesn't change the actual usage, just the reservation.
811 * The inode sent in is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200813int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814xfs_trans_reserve_quota_nblks(
Christoph Hellwig7d095252009-06-08 15:33:32 +0200815 struct xfs_trans *tp,
816 struct xfs_inode *ip,
817 long nblks,
818 long ninos,
819 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200821 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Christoph Hellwig7d095252009-06-08 15:33:32 +0200823 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000824 return 0;
825 if (XFS_IS_PQUOTA_ON(mp))
826 flags |= XFS_QMOPT_ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 ASSERT(ip->i_ino != mp->m_sb.sb_uquotino);
829 ASSERT(ip->i_ino != mp->m_sb.sb_gquotino);
830
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000831 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000832 ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
833 XFS_TRANS_DQ_RES_RTBLKS ||
834 (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
835 XFS_TRANS_DQ_RES_BLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /*
838 * Reserve nblks against these dquots, with trans as the mediator.
839 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200840 return xfs_trans_reserve_quota_bydquots(tp, mp,
841 ip->i_udquot, ip->i_gdquot,
842 nblks, ninos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845/*
846 * This routine is called to allocate a quotaoff log item.
847 */
848xfs_qoff_logitem_t *
849xfs_trans_get_qoff_item(
850 xfs_trans_t *tp,
851 xfs_qoff_logitem_t *startqoff,
852 uint flags)
853{
854 xfs_qoff_logitem_t *q;
855
856 ASSERT(tp != NULL);
857
858 q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
859 ASSERT(q != NULL);
860
861 /*
862 * Get a log_item_desc to point at the new item.
863 */
864 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)q);
865
866 return (q);
867}
868
869
870/*
871 * This is called to mark the quotaoff logitem as needing
872 * to be logged when the transaction is committed. The logitem must
873 * already be associated with the given transaction.
874 */
875void
876xfs_trans_log_quotaoff_item(
877 xfs_trans_t *tp,
878 xfs_qoff_logitem_t *qlp)
879{
880 xfs_log_item_desc_t *lidp;
881
882 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *)qlp);
883 ASSERT(lidp != NULL);
884
885 tp->t_flags |= XFS_TRANS_DIRTY;
886 lidp->lid_flags |= XFS_LID_DIRTY;
887}
888
889STATIC void
890xfs_trans_alloc_dqinfo(
891 xfs_trans_t *tp)
892{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200893 tp->t_dqinfo = kmem_zone_zalloc(xfs_Gqm->qm_dqtrxzone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Christoph Hellwig7d095252009-06-08 15:33:32 +0200896void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897xfs_trans_free_dqinfo(
898 xfs_trans_t *tp)
899{
900 if (!tp->t_dqinfo)
901 return;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200902 kmem_zone_free(xfs_Gqm->qm_dqtrxzone, tp->t_dqinfo);
903 tp->t_dqinfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}