blob: 08f5604d092f3345f7d09f4c9feab61ca2cbc530 [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_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_quota.h"
28#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_rtalloc.h"
34#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_attr.h"
36#include "xfs_buf_item.h"
37#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_qm.h"
39
40STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
41
42/*
43 * Add the locked dquot to the transaction.
44 * The dquot must be locked, and it cannot be associated with any
45 * transaction.
46 */
47void
48xfs_trans_dqjoin(
49 xfs_trans_t *tp,
50 xfs_dquot_t *dqp)
51{
Christoph Hellwig191f8482010-04-20 17:01:53 +100052 xfs_dq_logitem_t *lp = &dqp->q_logitem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Christoph Hellwig191f8482010-04-20 17:01:53 +100054 ASSERT(dqp->q_transp != tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig191f8482010-04-20 17:01:53 +100056 ASSERT(lp->qli_dquot == dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 /*
59 * Get a log_item_desc to point at the new item.
60 */
61 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)(lp));
62
63 /*
64 * Initialize i_transp so we can later determine if this dquot is
65 * associated with this transaction.
66 */
67 dqp->q_transp = tp;
68}
69
70
71/*
72 * This is called to mark the dquot as needing
73 * to be logged when the transaction is committed. The dquot must
74 * already be associated with the given transaction.
75 * Note that it marks the entire transaction as dirty. In the ordinary
76 * case, this gets called via xfs_trans_commit, after the transaction
77 * is already dirty. However, there's nothing stop this from getting
78 * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
79 * flag.
80 */
81void
82xfs_trans_log_dquot(
83 xfs_trans_t *tp,
84 xfs_dquot_t *dqp)
85{
86 xfs_log_item_desc_t *lidp;
87
Christoph Hellwig191f8482010-04-20 17:01:53 +100088 ASSERT(dqp->q_transp == tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ASSERT(XFS_DQ_IS_LOCKED(dqp));
90
91 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)(&dqp->q_logitem));
92 ASSERT(lidp != NULL);
93
94 tp->t_flags |= XFS_TRANS_DIRTY;
95 lidp->lid_flags |= XFS_LID_DIRTY;
96}
97
98/*
99 * Carry forward whatever is left of the quota blk reservation to
100 * the spanky new transaction
101 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200102void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103xfs_trans_dup_dqinfo(
104 xfs_trans_t *otp,
105 xfs_trans_t *ntp)
106{
107 xfs_dqtrx_t *oq, *nq;
108 int i,j;
109 xfs_dqtrx_t *oqa, *nqa;
110
111 if (!otp->t_dqinfo)
112 return;
113
114 xfs_trans_alloc_dqinfo(ntp);
115 oqa = otp->t_dqinfo->dqa_usrdquots;
116 nqa = ntp->t_dqinfo->dqa_usrdquots;
117
118 /*
119 * Because the quota blk reservation is carried forward,
120 * it is also necessary to carry forward the DQ_DIRTY flag.
121 */
122 if(otp->t_flags & XFS_TRANS_DQ_DIRTY)
123 ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
124
125 for (j = 0; j < 2; j++) {
126 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
127 if (oqa[i].qt_dquot == NULL)
128 break;
129 oq = &oqa[i];
130 nq = &nqa[i];
131
132 nq->qt_dquot = oq->qt_dquot;
133 nq->qt_bcount_delta = nq->qt_icount_delta = 0;
134 nq->qt_rtbcount_delta = 0;
135
136 /*
137 * Transfer whatever is left of the reservations.
138 */
139 nq->qt_blk_res = oq->qt_blk_res - oq->qt_blk_res_used;
140 oq->qt_blk_res = oq->qt_blk_res_used;
141
142 nq->qt_rtblk_res = oq->qt_rtblk_res -
143 oq->qt_rtblk_res_used;
144 oq->qt_rtblk_res = oq->qt_rtblk_res_used;
145
146 nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
147 oq->qt_ino_res = oq->qt_ino_res_used;
148
149 }
150 oqa = otp->t_dqinfo->dqa_grpdquots;
151 nqa = ntp->t_dqinfo->dqa_grpdquots;
152 }
153}
154
155/*
156 * Wrap around mod_dquot to account for both user and group quotas.
157 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200158void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159xfs_trans_mod_dquot_byino(
160 xfs_trans_t *tp,
161 xfs_inode_t *ip,
162 uint field,
163 long delta)
164{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200165 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig7d095252009-06-08 15:33:32 +0200167 if (!XFS_IS_QUOTA_RUNNING(mp) ||
168 !XFS_IS_QUOTA_ON(mp) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 ip->i_ino == mp->m_sb.sb_uquotino ||
170 ip->i_ino == mp->m_sb.sb_gquotino)
171 return;
172
173 if (tp->t_dqinfo == NULL)
174 xfs_trans_alloc_dqinfo(tp);
175
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000176 if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000178 if (XFS_IS_OQUOTA_ON(mp) && ip->i_gdquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182STATIC xfs_dqtrx_t *
183xfs_trans_get_dqtrx(
184 xfs_trans_t *tp,
185 xfs_dquot_t *dqp)
186{
187 int i;
188 xfs_dqtrx_t *qa;
189
Christoph Hellwig191f8482010-04-20 17:01:53 +1000190 qa = XFS_QM_ISUDQ(dqp) ?
191 tp->t_dqinfo->dqa_usrdquots : tp->t_dqinfo->dqa_grpdquots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Christoph Hellwig191f8482010-04-20 17:01:53 +1000193 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (qa[i].qt_dquot == NULL ||
Christoph Hellwig191f8482010-04-20 17:01:53 +1000195 qa[i].qt_dquot == dqp)
196 return &qa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Christoph Hellwig191f8482010-04-20 17:01:53 +1000199 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/*
203 * Make the changes in the transaction structure.
204 * The moral equivalent to xfs_trans_mod_sb().
205 * We don't touch any fields in the dquot, so we don't care
206 * if it's locked or not (most of the time it won't be).
207 */
208void
209xfs_trans_mod_dquot(
210 xfs_trans_t *tp,
211 xfs_dquot_t *dqp,
212 uint field,
213 long delta)
214{
215 xfs_dqtrx_t *qtrx;
216
217 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200218 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 qtrx = NULL;
220
221 if (tp->t_dqinfo == NULL)
222 xfs_trans_alloc_dqinfo(tp);
223 /*
224 * Find either the first free slot or the slot that belongs
225 * to this dquot.
226 */
227 qtrx = xfs_trans_get_dqtrx(tp, dqp);
228 ASSERT(qtrx);
229 if (qtrx->qt_dquot == NULL)
230 qtrx->qt_dquot = dqp;
231
232 switch (field) {
233
234 /*
235 * regular disk blk reservation
236 */
237 case XFS_TRANS_DQ_RES_BLKS:
238 qtrx->qt_blk_res += (ulong)delta;
239 break;
240
241 /*
242 * inode reservation
243 */
244 case XFS_TRANS_DQ_RES_INOS:
245 qtrx->qt_ino_res += (ulong)delta;
246 break;
247
248 /*
249 * disk blocks used.
250 */
251 case XFS_TRANS_DQ_BCOUNT:
252 if (qtrx->qt_blk_res && delta > 0) {
253 qtrx->qt_blk_res_used += (ulong)delta;
254 ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
255 }
256 qtrx->qt_bcount_delta += delta;
257 break;
258
259 case XFS_TRANS_DQ_DELBCOUNT:
260 qtrx->qt_delbcnt_delta += delta;
261 break;
262
263 /*
264 * Inode Count
265 */
266 case XFS_TRANS_DQ_ICOUNT:
267 if (qtrx->qt_ino_res && delta > 0) {
268 qtrx->qt_ino_res_used += (ulong)delta;
269 ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
270 }
271 qtrx->qt_icount_delta += delta;
272 break;
273
274 /*
275 * rtblk reservation
276 */
277 case XFS_TRANS_DQ_RES_RTBLKS:
278 qtrx->qt_rtblk_res += (ulong)delta;
279 break;
280
281 /*
282 * rtblk count
283 */
284 case XFS_TRANS_DQ_RTBCOUNT:
285 if (qtrx->qt_rtblk_res && delta > 0) {
286 qtrx->qt_rtblk_res_used += (ulong)delta;
287 ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
288 }
289 qtrx->qt_rtbcount_delta += delta;
290 break;
291
292 case XFS_TRANS_DQ_DELRTBCOUNT:
293 qtrx->qt_delrtb_delta += delta;
294 break;
295
296 default:
297 ASSERT(0);
298 }
299 tp->t_flags |= XFS_TRANS_DQ_DIRTY;
300}
301
302
303/*
304 * Given an array of dqtrx structures, lock all the dquots associated
305 * and join them to the transaction, provided they have been modified.
306 * We know that the highest number of dquots (of one type - usr OR grp),
307 * involved in a transaction is 2 and that both usr and grp combined - 3.
308 * So, we don't attempt to make this very generic.
309 */
310STATIC void
311xfs_trans_dqlockedjoin(
312 xfs_trans_t *tp,
313 xfs_dqtrx_t *q)
314{
315 ASSERT(q[0].qt_dquot != NULL);
316 if (q[1].qt_dquot == NULL) {
317 xfs_dqlock(q[0].qt_dquot);
318 xfs_trans_dqjoin(tp, q[0].qt_dquot);
319 } else {
320 ASSERT(XFS_QM_TRANS_MAXDQS == 2);
321 xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
322 xfs_trans_dqjoin(tp, q[0].qt_dquot);
323 xfs_trans_dqjoin(tp, q[1].qt_dquot);
324 }
325}
326
327
328/*
329 * Called by xfs_trans_commit() and similar in spirit to
330 * xfs_trans_apply_sb_deltas().
331 * Go thru all the dquots belonging to this transaction and modify the
332 * INCORE dquot to reflect the actual usages.
333 * Unreserve just the reservations done by this transaction.
334 * dquot is still left locked at exit.
335 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200336void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337xfs_trans_apply_dquot_deltas(
338 xfs_trans_t *tp)
339{
340 int i, j;
341 xfs_dquot_t *dqp;
342 xfs_dqtrx_t *qtrx, *qa;
343 xfs_disk_dquot_t *d;
344 long totalbdelta;
345 long totalrtbdelta;
346
Christoph Hellwig7d095252009-06-08 15:33:32 +0200347 if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return;
349
350 ASSERT(tp->t_dqinfo);
351 qa = tp->t_dqinfo->dqa_usrdquots;
352 for (j = 0; j < 2; j++) {
353 if (qa[0].qt_dquot == NULL) {
354 qa = tp->t_dqinfo->dqa_grpdquots;
355 continue;
356 }
357
358 /*
359 * Lock all of the dquots and join them to the transaction.
360 */
361 xfs_trans_dqlockedjoin(tp, qa);
362
363 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
364 qtrx = &qa[i];
365 /*
366 * The array of dquots is filled
367 * sequentially, not sparsely.
368 */
369 if ((dqp = qtrx->qt_dquot) == NULL)
370 break;
371
372 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig191f8482010-04-20 17:01:53 +1000373 ASSERT(dqp->q_transp == tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * adjust the actual number of blocks used
377 */
378 d = &dqp->q_core;
379
380 /*
381 * The issue here is - sometimes we don't make a blkquota
382 * reservation intentionally to be fair to users
383 * (when the amount is small). On the other hand,
384 * delayed allocs do make reservations, but that's
385 * outside of a transaction, so we have no
386 * idea how much was really reserved.
387 * So, here we've accumulated delayed allocation blks and
388 * non-delay blks. The assumption is that the
389 * delayed ones are always reserved (outside of a
390 * transaction), and the others may or may not have
391 * quota reservations.
392 */
393 totalbdelta = qtrx->qt_bcount_delta +
394 qtrx->qt_delbcnt_delta;
395 totalrtbdelta = qtrx->qt_rtbcount_delta +
396 qtrx->qt_delrtb_delta;
397#ifdef QUOTADEBUG
398 if (totalbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100399 ASSERT(be64_to_cpu(d->d_bcount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 (xfs_qcnt_t) -totalbdelta);
401
402 if (totalrtbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100403 ASSERT(be64_to_cpu(d->d_rtbcount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 (xfs_qcnt_t) -totalrtbdelta);
405
406 if (qtrx->qt_icount_delta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100407 ASSERT(be64_to_cpu(d->d_icount) >=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 (xfs_qcnt_t) -qtrx->qt_icount_delta);
409#endif
410 if (totalbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800411 be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (qtrx->qt_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800414 be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (totalrtbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800417 be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /*
420 * Get any default limits in use.
421 * Start/reset the timer(s) if needed.
422 */
423 if (d->d_id) {
424 xfs_qm_adjust_dqlimits(tp->t_mountp, d);
425 xfs_qm_adjust_dqtimers(tp->t_mountp, d);
426 }
427
428 dqp->dq_flags |= XFS_DQ_DIRTY;
429 /*
430 * add this to the list of items to get logged
431 */
432 xfs_trans_log_dquot(tp, dqp);
433 /*
434 * Take off what's left of the original reservation.
435 * In case of delayed allocations, there's no
436 * reservation that a transaction structure knows of.
437 */
438 if (qtrx->qt_blk_res != 0) {
439 if (qtrx->qt_blk_res != qtrx->qt_blk_res_used) {
440 if (qtrx->qt_blk_res >
441 qtrx->qt_blk_res_used)
442 dqp->q_res_bcount -= (xfs_qcnt_t)
443 (qtrx->qt_blk_res -
444 qtrx->qt_blk_res_used);
445 else
446 dqp->q_res_bcount -= (xfs_qcnt_t)
447 (qtrx->qt_blk_res_used -
448 qtrx->qt_blk_res);
449 }
450 } else {
451 /*
452 * These blks were never reserved, either inside
453 * a transaction or outside one (in a delayed
454 * allocation). Also, this isn't always a
455 * negative number since we sometimes
456 * deliberately skip quota reservations.
457 */
458 if (qtrx->qt_bcount_delta) {
459 dqp->q_res_bcount +=
460 (xfs_qcnt_t)qtrx->qt_bcount_delta;
461 }
462 }
463 /*
464 * Adjust the RT reservation.
465 */
466 if (qtrx->qt_rtblk_res != 0) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000467 if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (qtrx->qt_rtblk_res >
469 qtrx->qt_rtblk_res_used)
470 dqp->q_res_rtbcount -= (xfs_qcnt_t)
471 (qtrx->qt_rtblk_res -
472 qtrx->qt_rtblk_res_used);
473 else
474 dqp->q_res_rtbcount -= (xfs_qcnt_t)
475 (qtrx->qt_rtblk_res_used -
476 qtrx->qt_rtblk_res);
477 }
478 } else {
479 if (qtrx->qt_rtbcount_delta)
480 dqp->q_res_rtbcount +=
481 (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
482 }
483
484 /*
485 * Adjust the inode reservation.
486 */
487 if (qtrx->qt_ino_res != 0) {
488 ASSERT(qtrx->qt_ino_res >=
489 qtrx->qt_ino_res_used);
490 if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
491 dqp->q_res_icount -= (xfs_qcnt_t)
492 (qtrx->qt_ino_res -
493 qtrx->qt_ino_res_used);
494 } else {
495 if (qtrx->qt_icount_delta)
496 dqp->q_res_icount +=
497 (xfs_qcnt_t)qtrx->qt_icount_delta;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ASSERT(dqp->q_res_bcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100501 be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ASSERT(dqp->q_res_icount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100503 be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 ASSERT(dqp->q_res_rtbcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100505 be64_to_cpu(dqp->q_core.d_rtbcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 /*
508 * Do the group quotas next
509 */
510 qa = tp->t_dqinfo->dqa_grpdquots;
511 }
512}
513
514/*
515 * Release the reservations, and adjust the dquots accordingly.
516 * This is called only when the transaction is being aborted. If by
517 * any chance we have done dquot modifications incore (ie. deltas) already,
518 * we simply throw those away, since that's the expected behavior
519 * when a transaction is curtailed without a commit.
520 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200521void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522xfs_trans_unreserve_and_mod_dquots(
523 xfs_trans_t *tp)
524{
525 int i, j;
526 xfs_dquot_t *dqp;
527 xfs_dqtrx_t *qtrx, *qa;
528 boolean_t locked;
529
530 if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
531 return;
532
533 qa = tp->t_dqinfo->dqa_usrdquots;
534
535 for (j = 0; j < 2; j++) {
536 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
537 qtrx = &qa[i];
538 /*
539 * We assume that the array of dquots is filled
540 * sequentially, not sparsely.
541 */
542 if ((dqp = qtrx->qt_dquot) == NULL)
543 break;
544 /*
545 * Unreserve the original reservation. We don't care
546 * about the number of blocks used field, or deltas.
547 * Also we don't bother to zero the fields.
548 */
549 locked = B_FALSE;
550 if (qtrx->qt_blk_res) {
551 xfs_dqlock(dqp);
552 locked = B_TRUE;
553 dqp->q_res_bcount -=
554 (xfs_qcnt_t)qtrx->qt_blk_res;
555 }
556 if (qtrx->qt_ino_res) {
557 if (!locked) {
558 xfs_dqlock(dqp);
559 locked = B_TRUE;
560 }
561 dqp->q_res_icount -=
562 (xfs_qcnt_t)qtrx->qt_ino_res;
563 }
564
565 if (qtrx->qt_rtblk_res) {
566 if (!locked) {
567 xfs_dqlock(dqp);
568 locked = B_TRUE;
569 }
570 dqp->q_res_rtbcount -=
571 (xfs_qcnt_t)qtrx->qt_rtblk_res;
572 }
573 if (locked)
574 xfs_dqunlock(dqp);
575
576 }
577 qa = tp->t_dqinfo->dqa_grpdquots;
578 }
579}
580
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000581STATIC void
582xfs_quota_warn(
583 struct xfs_mount *mp,
584 struct xfs_dquot *dqp,
585 int type)
586{
587 /* no warnings for project quotas - we just return ENOSPC later */
588 if (dqp->dq_flags & XFS_DQ_PROJ)
589 return;
590 quota_send_warning((dqp->dq_flags & XFS_DQ_USER) ? USRQUOTA : GRPQUOTA,
591 be32_to_cpu(dqp->q_core.d_id), mp->m_super->s_dev,
592 type);
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * This reserves disk blocks and inodes against a dquot.
597 * Flags indicate if the dquot is to be locked here and also
598 * if the blk reservation is for RT or regular blocks.
599 * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601STATIC int
602xfs_trans_dqresv(
603 xfs_trans_t *tp,
604 xfs_mount_t *mp,
605 xfs_dquot_t *dqp,
606 long nblks,
607 long ninos,
608 uint flags)
609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_qcnt_t hardlimit;
611 xfs_qcnt_t softlimit;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000612 time_t timer;
613 xfs_qwarncnt_t warns;
614 xfs_qwarncnt_t warnlimit;
615 xfs_qcnt_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 xfs_qcnt_t *resbcountp;
617 xfs_quotainfo_t *q = mp->m_quotainfo;
618
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100619
620 xfs_dqlock(dqp);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (flags & XFS_TRANS_DQ_RES_BLKS) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100623 hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!hardlimit)
625 hardlimit = q->qi_bhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100626 softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!softlimit)
628 softlimit = q->qi_bsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100629 timer = be32_to_cpu(dqp->q_core.d_btimer);
630 warns = be16_to_cpu(dqp->q_core.d_bwarns);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000631 warnlimit = dqp->q_mount->m_quotainfo->qi_bwarnlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 resbcountp = &dqp->q_res_bcount;
633 } else {
634 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100635 hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!hardlimit)
637 hardlimit = q->qi_rtbhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100638 softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!softlimit)
640 softlimit = q->qi_rtbsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100641 timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
642 warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000643 warnlimit = dqp->q_mount->m_quotainfo->qi_rtbwarnlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 resbcountp = &dqp->q_res_rtbcount;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
648 dqp->q_core.d_id &&
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000649 ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
650 (XFS_IS_OQUOTA_ENFORCED(dqp->q_mount) &&
651 (XFS_QM_ISPDQ(dqp) || XFS_QM_ISGDQ(dqp))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652#ifdef QUOTADEBUG
653 cmn_err(CE_DEBUG, "BLK Res: nblks=%ld + resbcount=%Ld"
654 " > hardlimit=%Ld?", nblks, *resbcountp, hardlimit);
655#endif
656 if (nblks > 0) {
657 /*
658 * dquot is locked already. See if we'd go over the
659 * hardlimit or exceed the timelimit if we allocate
660 * nblks.
661 */
662 if (hardlimit > 0ULL &&
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000663 hardlimit <= nblks + *resbcountp) {
664 xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto error_return;
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (softlimit > 0ULL &&
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000668 softlimit <= nblks + *resbcountp) {
669 if ((timer != 0 && get_seconds() > timer) ||
670 (warns != 0 && warns >= warnlimit)) {
671 xfs_quota_warn(mp, dqp,
672 QUOTA_NL_BSOFTLONGWARN);
673 goto error_return;
674 }
675
676 xfs_quota_warn(mp, dqp, QUOTA_NL_BSOFTWARN);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 if (ninos > 0) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100680 count = be64_to_cpu(dqp->q_core.d_icount);
681 timer = be32_to_cpu(dqp->q_core.d_itimer);
682 warns = be16_to_cpu(dqp->q_core.d_iwarns);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000683 warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100684 hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!hardlimit)
686 hardlimit = q->qi_ihardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100687 softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!softlimit)
689 softlimit = q->qi_isoftlimit;
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000690
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000691 if (hardlimit > 0ULL && count >= hardlimit) {
692 xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto error_return;
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000694 }
695 if (softlimit > 0ULL && count >= softlimit) {
696 if ((timer != 0 && get_seconds() > timer) ||
697 (warns != 0 && warns >= warnlimit)) {
698 xfs_quota_warn(mp, dqp,
699 QUOTA_NL_ISOFTLONGWARN);
700 goto error_return;
701 }
702 xfs_quota_warn(mp, dqp, QUOTA_NL_ISOFTWARN);
703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
706
707 /*
708 * Change the reservation, but not the actual usage.
709 * Note that q_res_bcount = q_core.d_bcount + resv
710 */
711 (*resbcountp) += (xfs_qcnt_t)nblks;
712 if (ninos != 0)
713 dqp->q_res_icount += (xfs_qcnt_t)ninos;
714
715 /*
716 * note the reservation amt in the trans struct too,
717 * so that the transaction knows how much was reserved by
718 * it against this particular dquot.
719 * We don't do this when we are reserving for a delayed allocation,
720 * because we don't have the luxury of a transaction envelope then.
721 */
722 if (tp) {
723 ASSERT(tp->t_dqinfo);
724 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
725 if (nblks != 0)
726 xfs_trans_mod_dquot(tp, dqp,
727 flags & XFS_QMOPT_RESBLK_MASK,
728 nblks);
729 if (ninos != 0)
730 xfs_trans_mod_dquot(tp, dqp,
731 XFS_TRANS_DQ_RES_INOS,
732 ninos);
733 }
Christoph Hellwig1149d962005-11-02 15:01:12 +1100734 ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
735 ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
736 ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000738 xfs_dqunlock(dqp);
739 return 0;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741error_return:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100742 xfs_dqunlock(dqp);
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000743 if (flags & XFS_QMOPT_ENOSPC)
744 return ENOSPC;
745 return EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748
749/*
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000750 * Given dquot(s), make disk block and/or inode reservations against them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * The fact that this does the reservation against both the usr and
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000752 * grp/prj quotas is important, because this follows a both-or-nothing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * approach.
754 *
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100755 * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000756 * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
758 * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
759 * dquots are unlocked on return, if they were not locked by caller.
760 */
761int
762xfs_trans_reserve_quota_bydquots(
763 xfs_trans_t *tp,
764 xfs_mount_t *mp,
765 xfs_dquot_t *udqp,
766 xfs_dquot_t *gdqp,
767 long nblks,
768 long ninos,
769 uint flags)
770{
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000771 int resvd = 0, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Christoph Hellwig7d095252009-06-08 15:33:32 +0200773 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (tp && tp->t_dqinfo == NULL)
777 xfs_trans_alloc_dqinfo(tp);
778
779 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (udqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000782 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
783 (flags & ~XFS_QMOPT_ENOSPC));
784 if (error)
785 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 resvd = 1;
787 }
788
789 if (gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000790 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
791 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /*
793 * can't do it, so backout previous reservation
794 */
795 if (resvd) {
796 flags |= XFS_QMOPT_FORCE_RES;
797 xfs_trans_dqresv(tp, mp, udqp,
798 -nblks, -ninos, flags);
799 }
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000800 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 }
803
804 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000805 * Didn't change anything critical, so, no need to log
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810
811/*
812 * Lock the dquot and change the reservation if we can.
813 * This doesn't change the actual usage, just the reservation.
814 * The inode sent in is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200816int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817xfs_trans_reserve_quota_nblks(
Christoph Hellwig7d095252009-06-08 15:33:32 +0200818 struct xfs_trans *tp,
819 struct xfs_inode *ip,
820 long nblks,
821 long ninos,
822 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200824 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Christoph Hellwig7d095252009-06-08 15:33:32 +0200826 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000827 return 0;
828 if (XFS_IS_PQUOTA_ON(mp))
829 flags |= XFS_QMOPT_ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 ASSERT(ip->i_ino != mp->m_sb.sb_uquotino);
832 ASSERT(ip->i_ino != mp->m_sb.sb_gquotino);
833
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000834 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000835 ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
836 XFS_TRANS_DQ_RES_RTBLKS ||
837 (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
838 XFS_TRANS_DQ_RES_BLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /*
841 * Reserve nblks against these dquots, with trans as the mediator.
842 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200843 return xfs_trans_reserve_quota_bydquots(tp, mp,
844 ip->i_udquot, ip->i_gdquot,
845 nblks, ninos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848/*
849 * This routine is called to allocate a quotaoff log item.
850 */
851xfs_qoff_logitem_t *
852xfs_trans_get_qoff_item(
853 xfs_trans_t *tp,
854 xfs_qoff_logitem_t *startqoff,
855 uint flags)
856{
857 xfs_qoff_logitem_t *q;
858
859 ASSERT(tp != NULL);
860
861 q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
862 ASSERT(q != NULL);
863
864 /*
865 * Get a log_item_desc to point at the new item.
866 */
867 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)q);
868
869 return (q);
870}
871
872
873/*
874 * This is called to mark the quotaoff logitem as needing
875 * to be logged when the transaction is committed. The logitem must
876 * already be associated with the given transaction.
877 */
878void
879xfs_trans_log_quotaoff_item(
880 xfs_trans_t *tp,
881 xfs_qoff_logitem_t *qlp)
882{
883 xfs_log_item_desc_t *lidp;
884
885 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *)qlp);
886 ASSERT(lidp != NULL);
887
888 tp->t_flags |= XFS_TRANS_DIRTY;
889 lidp->lid_flags |= XFS_LID_DIRTY;
890}
891
892STATIC void
893xfs_trans_alloc_dqinfo(
894 xfs_trans_t *tp)
895{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200896 tp->t_dqinfo = kmem_zone_zalloc(xfs_Gqm->qm_dqtrxzone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Christoph Hellwig7d095252009-06-08 15:33:32 +0200899void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900xfs_trans_free_dqinfo(
901 xfs_trans_t *tp)
902{
903 if (!tp->t_dqinfo)
904 return;
Christoph Hellwig7d095252009-06-08 15:33:32 +0200905 kmem_zone_free(xfs_Gqm->qm_dqtrxzone, tp->t_dqinfo);
906 tp->t_dqinfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}