blob: 7ea7485f9fb8a9beb63284df9f657662c5bde82c [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_log.h"
21#include "xfs_trans.h"
22#include "xfs_sb.h"
23#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_quota.h"
26#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_itable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_rtalloc.h"
32#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_attr.h"
34#include "xfs_buf_item.h"
35#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_qm.h"
37
38STATIC void xfs_trans_alloc_dqinfo(xfs_trans_t *);
39
40/*
41 * Add the locked dquot to the transaction.
42 * The dquot must be locked, and it cannot be associated with any
43 * transaction.
44 */
45void
46xfs_trans_dqjoin(
47 xfs_trans_t *tp,
48 xfs_dquot_t *dqp)
49{
Christoph Hellwig191f8482010-04-20 17:01:53 +100050 ASSERT(dqp->q_transp != tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwige98c4142010-06-23 18:11:15 +100052 ASSERT(dqp->q_logitem.qli_dquot == dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /*
55 * Get a log_item_desc to point at the new item.
56 */
Christoph Hellwige98c4142010-06-23 18:11:15 +100057 xfs_trans_add_item(tp, &dqp->q_logitem.qli_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /*
Christoph Hellwigf3ca87382011-07-08 14:34:47 +020060 * Initialize d_transp so we can later determine if this dquot is
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * associated with this transaction.
62 */
63 dqp->q_transp = tp;
64}
65
66
67/*
68 * This is called to mark the dquot as needing
69 * to be logged when the transaction is committed. The dquot must
70 * already be associated with the given transaction.
71 * Note that it marks the entire transaction as dirty. In the ordinary
72 * case, this gets called via xfs_trans_commit, after the transaction
73 * is already dirty. However, there's nothing stop this from getting
74 * called directly, as done by xfs_qm_scall_setqlim. Hence, the TRANS_DIRTY
75 * flag.
76 */
77void
78xfs_trans_log_dquot(
79 xfs_trans_t *tp,
80 xfs_dquot_t *dqp)
81{
Christoph Hellwig191f8482010-04-20 17:01:53 +100082 ASSERT(dqp->q_transp == tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ASSERT(XFS_DQ_IS_LOCKED(dqp));
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 tp->t_flags |= XFS_TRANS_DIRTY;
Christoph Hellwige98c4142010-06-23 18:11:15 +100086 dqp->q_logitem.qli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * Carry forward whatever is left of the quota blk reservation to
91 * the spanky new transaction
92 */
Christoph Hellwig7d095252009-06-08 15:33:32 +020093void
Linus Torvalds1da177e2005-04-16 15:20:36 -070094xfs_trans_dup_dqinfo(
95 xfs_trans_t *otp,
96 xfs_trans_t *ntp)
97{
98 xfs_dqtrx_t *oq, *nq;
99 int i,j;
100 xfs_dqtrx_t *oqa, *nqa;
101
102 if (!otp->t_dqinfo)
103 return;
104
105 xfs_trans_alloc_dqinfo(ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /*
108 * Because the quota blk reservation is carried forward,
109 * it is also necessary to carry forward the DQ_DIRTY flag.
110 */
111 if(otp->t_flags & XFS_TRANS_DQ_DIRTY)
112 ntp->t_flags |= XFS_TRANS_DQ_DIRTY;
113
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500114 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
115 oqa = otp->t_dqinfo->dqs[j];
116 nqa = ntp->t_dqinfo->dqs[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
118 if (oqa[i].qt_dquot == NULL)
119 break;
120 oq = &oqa[i];
121 nq = &nqa[i];
122
123 nq->qt_dquot = oq->qt_dquot;
124 nq->qt_bcount_delta = nq->qt_icount_delta = 0;
125 nq->qt_rtbcount_delta = 0;
126
127 /*
128 * Transfer whatever is left of the reservations.
129 */
130 nq->qt_blk_res = oq->qt_blk_res - oq->qt_blk_res_used;
131 oq->qt_blk_res = oq->qt_blk_res_used;
132
133 nq->qt_rtblk_res = oq->qt_rtblk_res -
134 oq->qt_rtblk_res_used;
135 oq->qt_rtblk_res = oq->qt_rtblk_res_used;
136
137 nq->qt_ino_res = oq->qt_ino_res - oq->qt_ino_res_used;
138 oq->qt_ino_res = oq->qt_ino_res_used;
139
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142}
143
144/*
145 * Wrap around mod_dquot to account for both user and group quotas.
146 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200147void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148xfs_trans_mod_dquot_byino(
149 xfs_trans_t *tp,
150 xfs_inode_t *ip,
151 uint field,
152 long delta)
153{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200154 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Christoph Hellwig7d095252009-06-08 15:33:32 +0200156 if (!XFS_IS_QUOTA_RUNNING(mp) ||
157 !XFS_IS_QUOTA_ON(mp) ||
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500158 xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return;
160
161 if (tp->t_dqinfo == NULL)
162 xfs_trans_alloc_dqinfo(tp);
163
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000164 if (XFS_IS_UQUOTA_ON(mp) && ip->i_udquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 (void) xfs_trans_mod_dquot(tp, ip->i_udquot, field, delta);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000166 if (XFS_IS_OQUOTA_ON(mp) && ip->i_gdquot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 (void) xfs_trans_mod_dquot(tp, ip->i_gdquot, field, delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500170STATIC struct xfs_dqtrx *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171xfs_trans_get_dqtrx(
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500172 struct xfs_trans *tp,
173 struct xfs_dquot *dqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500175 int i;
176 struct xfs_dqtrx *qa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500178 if (XFS_QM_ISUDQ(dqp))
179 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_USR];
180 else
181 qa = tp->t_dqinfo->dqs[XFS_QM_TRANS_GRP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Christoph Hellwig191f8482010-04-20 17:01:53 +1000183 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (qa[i].qt_dquot == NULL ||
Christoph Hellwig191f8482010-04-20 17:01:53 +1000185 qa[i].qt_dquot == dqp)
186 return &qa[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Christoph Hellwig191f8482010-04-20 17:01:53 +1000189 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/*
193 * Make the changes in the transaction structure.
194 * The moral equivalent to xfs_trans_mod_sb().
195 * We don't touch any fields in the dquot, so we don't care
196 * if it's locked or not (most of the time it won't be).
197 */
198void
199xfs_trans_mod_dquot(
200 xfs_trans_t *tp,
201 xfs_dquot_t *dqp,
202 uint field,
203 long delta)
204{
205 xfs_dqtrx_t *qtrx;
206
207 ASSERT(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200208 ASSERT(XFS_IS_QUOTA_RUNNING(tp->t_mountp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 qtrx = NULL;
210
211 if (tp->t_dqinfo == NULL)
212 xfs_trans_alloc_dqinfo(tp);
213 /*
214 * Find either the first free slot or the slot that belongs
215 * to this dquot.
216 */
217 qtrx = xfs_trans_get_dqtrx(tp, dqp);
218 ASSERT(qtrx);
219 if (qtrx->qt_dquot == NULL)
220 qtrx->qt_dquot = dqp;
221
222 switch (field) {
223
224 /*
225 * regular disk blk reservation
226 */
227 case XFS_TRANS_DQ_RES_BLKS:
228 qtrx->qt_blk_res += (ulong)delta;
229 break;
230
231 /*
232 * inode reservation
233 */
234 case XFS_TRANS_DQ_RES_INOS:
235 qtrx->qt_ino_res += (ulong)delta;
236 break;
237
238 /*
239 * disk blocks used.
240 */
241 case XFS_TRANS_DQ_BCOUNT:
242 if (qtrx->qt_blk_res && delta > 0) {
243 qtrx->qt_blk_res_used += (ulong)delta;
244 ASSERT(qtrx->qt_blk_res >= qtrx->qt_blk_res_used);
245 }
246 qtrx->qt_bcount_delta += delta;
247 break;
248
249 case XFS_TRANS_DQ_DELBCOUNT:
250 qtrx->qt_delbcnt_delta += delta;
251 break;
252
253 /*
254 * Inode Count
255 */
256 case XFS_TRANS_DQ_ICOUNT:
257 if (qtrx->qt_ino_res && delta > 0) {
258 qtrx->qt_ino_res_used += (ulong)delta;
259 ASSERT(qtrx->qt_ino_res >= qtrx->qt_ino_res_used);
260 }
261 qtrx->qt_icount_delta += delta;
262 break;
263
264 /*
265 * rtblk reservation
266 */
267 case XFS_TRANS_DQ_RES_RTBLKS:
268 qtrx->qt_rtblk_res += (ulong)delta;
269 break;
270
271 /*
272 * rtblk count
273 */
274 case XFS_TRANS_DQ_RTBCOUNT:
275 if (qtrx->qt_rtblk_res && delta > 0) {
276 qtrx->qt_rtblk_res_used += (ulong)delta;
277 ASSERT(qtrx->qt_rtblk_res >= qtrx->qt_rtblk_res_used);
278 }
279 qtrx->qt_rtbcount_delta += delta;
280 break;
281
282 case XFS_TRANS_DQ_DELRTBCOUNT:
283 qtrx->qt_delrtb_delta += delta;
284 break;
285
286 default:
287 ASSERT(0);
288 }
289 tp->t_flags |= XFS_TRANS_DQ_DIRTY;
290}
291
292
293/*
294 * Given an array of dqtrx structures, lock all the dquots associated
295 * and join them to the transaction, provided they have been modified.
296 * We know that the highest number of dquots (of one type - usr OR grp),
297 * involved in a transaction is 2 and that both usr and grp combined - 3.
298 * So, we don't attempt to make this very generic.
299 */
300STATIC void
301xfs_trans_dqlockedjoin(
302 xfs_trans_t *tp,
303 xfs_dqtrx_t *q)
304{
305 ASSERT(q[0].qt_dquot != NULL);
306 if (q[1].qt_dquot == NULL) {
307 xfs_dqlock(q[0].qt_dquot);
308 xfs_trans_dqjoin(tp, q[0].qt_dquot);
309 } else {
310 ASSERT(XFS_QM_TRANS_MAXDQS == 2);
311 xfs_dqlock2(q[0].qt_dquot, q[1].qt_dquot);
312 xfs_trans_dqjoin(tp, q[0].qt_dquot);
313 xfs_trans_dqjoin(tp, q[1].qt_dquot);
314 }
315}
316
317
318/*
319 * Called by xfs_trans_commit() and similar in spirit to
320 * xfs_trans_apply_sb_deltas().
321 * Go thru all the dquots belonging to this transaction and modify the
322 * INCORE dquot to reflect the actual usages.
323 * Unreserve just the reservations done by this transaction.
324 * dquot is still left locked at exit.
325 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200326void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327xfs_trans_apply_dquot_deltas(
Brian Foster4b6eae2e2013-03-18 10:51:45 -0400328 struct xfs_trans *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int i, j;
Brian Foster4b6eae2e2013-03-18 10:51:45 -0400331 struct xfs_dquot *dqp;
332 struct xfs_dqtrx *qtrx, *qa;
333 struct xfs_disk_dquot *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 long totalbdelta;
335 long totalrtbdelta;
336
Christoph Hellwig7d095252009-06-08 15:33:32 +0200337 if (!(tp->t_flags & XFS_TRANS_DQ_DIRTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return;
339
340 ASSERT(tp->t_dqinfo);
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500341 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
342 qa = tp->t_dqinfo->dqs[j];
343 if (qa[0].qt_dquot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /*
347 * Lock all of the dquots and join them to the transaction.
348 */
349 xfs_trans_dqlockedjoin(tp, qa);
350
351 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
352 qtrx = &qa[i];
353 /*
354 * The array of dquots is filled
355 * sequentially, not sparsely.
356 */
357 if ((dqp = qtrx->qt_dquot) == NULL)
358 break;
359
360 ASSERT(XFS_DQ_IS_LOCKED(dqp));
Christoph Hellwig191f8482010-04-20 17:01:53 +1000361 ASSERT(dqp->q_transp == tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /*
364 * adjust the actual number of blocks used
365 */
366 d = &dqp->q_core;
367
368 /*
369 * The issue here is - sometimes we don't make a blkquota
370 * reservation intentionally to be fair to users
371 * (when the amount is small). On the other hand,
372 * delayed allocs do make reservations, but that's
373 * outside of a transaction, so we have no
374 * idea how much was really reserved.
375 * So, here we've accumulated delayed allocation blks and
376 * non-delay blks. The assumption is that the
377 * delayed ones are always reserved (outside of a
378 * transaction), and the others may or may not have
379 * quota reservations.
380 */
381 totalbdelta = qtrx->qt_bcount_delta +
382 qtrx->qt_delbcnt_delta;
383 totalrtbdelta = qtrx->qt_rtbcount_delta +
384 qtrx->qt_delrtb_delta;
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200385#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (totalbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100387 ASSERT(be64_to_cpu(d->d_bcount) >=
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200388 -totalbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (totalrtbdelta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100391 ASSERT(be64_to_cpu(d->d_rtbcount) >=
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200392 -totalrtbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (qtrx->qt_icount_delta < 0)
Christoph Hellwig1149d962005-11-02 15:01:12 +1100395 ASSERT(be64_to_cpu(d->d_icount) >=
Christoph Hellwigea15ab32011-07-13 13:43:50 +0200396 -qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
398 if (totalbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800399 be64_add_cpu(&d->d_bcount, (xfs_qcnt_t)totalbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 if (qtrx->qt_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800402 be64_add_cpu(&d->d_icount, (xfs_qcnt_t)qtrx->qt_icount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (totalrtbdelta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800405 be64_add_cpu(&d->d_rtbcount, (xfs_qcnt_t)totalrtbdelta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
408 * Get any default limits in use.
409 * Start/reset the timer(s) if needed.
410 */
411 if (d->d_id) {
Brian Foster4b6eae2e2013-03-18 10:51:45 -0400412 xfs_qm_adjust_dqlimits(tp->t_mountp, dqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 xfs_qm_adjust_dqtimers(tp->t_mountp, d);
414 }
415
416 dqp->dq_flags |= XFS_DQ_DIRTY;
417 /*
418 * add this to the list of items to get logged
419 */
420 xfs_trans_log_dquot(tp, dqp);
421 /*
422 * Take off what's left of the original reservation.
423 * In case of delayed allocations, there's no
424 * reservation that a transaction structure knows of.
425 */
426 if (qtrx->qt_blk_res != 0) {
427 if (qtrx->qt_blk_res != qtrx->qt_blk_res_used) {
428 if (qtrx->qt_blk_res >
429 qtrx->qt_blk_res_used)
430 dqp->q_res_bcount -= (xfs_qcnt_t)
431 (qtrx->qt_blk_res -
432 qtrx->qt_blk_res_used);
433 else
434 dqp->q_res_bcount -= (xfs_qcnt_t)
435 (qtrx->qt_blk_res_used -
436 qtrx->qt_blk_res);
437 }
438 } else {
439 /*
440 * These blks were never reserved, either inside
441 * a transaction or outside one (in a delayed
442 * allocation). Also, this isn't always a
443 * negative number since we sometimes
444 * deliberately skip quota reservations.
445 */
446 if (qtrx->qt_bcount_delta) {
447 dqp->q_res_bcount +=
448 (xfs_qcnt_t)qtrx->qt_bcount_delta;
449 }
450 }
451 /*
452 * Adjust the RT reservation.
453 */
454 if (qtrx->qt_rtblk_res != 0) {
Nathan Scott06d10dd2005-06-21 15:48:47 +1000455 if (qtrx->qt_rtblk_res != qtrx->qt_rtblk_res_used) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (qtrx->qt_rtblk_res >
457 qtrx->qt_rtblk_res_used)
458 dqp->q_res_rtbcount -= (xfs_qcnt_t)
459 (qtrx->qt_rtblk_res -
460 qtrx->qt_rtblk_res_used);
461 else
462 dqp->q_res_rtbcount -= (xfs_qcnt_t)
463 (qtrx->qt_rtblk_res_used -
464 qtrx->qt_rtblk_res);
465 }
466 } else {
467 if (qtrx->qt_rtbcount_delta)
468 dqp->q_res_rtbcount +=
469 (xfs_qcnt_t)qtrx->qt_rtbcount_delta;
470 }
471
472 /*
473 * Adjust the inode reservation.
474 */
475 if (qtrx->qt_ino_res != 0) {
476 ASSERT(qtrx->qt_ino_res >=
477 qtrx->qt_ino_res_used);
478 if (qtrx->qt_ino_res > qtrx->qt_ino_res_used)
479 dqp->q_res_icount -= (xfs_qcnt_t)
480 (qtrx->qt_ino_res -
481 qtrx->qt_ino_res_used);
482 } else {
483 if (qtrx->qt_icount_delta)
484 dqp->q_res_icount +=
485 (xfs_qcnt_t)qtrx->qt_icount_delta;
486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 ASSERT(dqp->q_res_bcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100489 be64_to_cpu(dqp->q_core.d_bcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ASSERT(dqp->q_res_icount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100491 be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 ASSERT(dqp->q_res_rtbcount >=
Christoph Hellwig1149d962005-11-02 15:01:12 +1100493 be64_to_cpu(dqp->q_core.d_rtbcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496}
497
498/*
499 * Release the reservations, and adjust the dquots accordingly.
500 * This is called only when the transaction is being aborted. If by
501 * any chance we have done dquot modifications incore (ie. deltas) already,
502 * we simply throw those away, since that's the expected behavior
503 * when a transaction is curtailed without a commit.
504 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200505void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506xfs_trans_unreserve_and_mod_dquots(
507 xfs_trans_t *tp)
508{
509 int i, j;
510 xfs_dquot_t *dqp;
511 xfs_dqtrx_t *qtrx, *qa;
Thiago Farina667a9292012-11-12 21:32:59 -0200512 bool locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (!tp->t_dqinfo || !(tp->t_flags & XFS_TRANS_DQ_DIRTY))
515 return;
516
Chandra Seetharaman0e6436d2013-06-27 17:25:09 -0500517 for (j = 0; j < XFS_QM_TRANS_DQTYPES; j++) {
518 qa = tp->t_dqinfo->dqs[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 for (i = 0; i < XFS_QM_TRANS_MAXDQS; i++) {
521 qtrx = &qa[i];
522 /*
523 * We assume that the array of dquots is filled
524 * sequentially, not sparsely.
525 */
526 if ((dqp = qtrx->qt_dquot) == NULL)
527 break;
528 /*
529 * Unreserve the original reservation. We don't care
530 * about the number of blocks used field, or deltas.
531 * Also we don't bother to zero the fields.
532 */
Thiago Farina667a9292012-11-12 21:32:59 -0200533 locked = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (qtrx->qt_blk_res) {
535 xfs_dqlock(dqp);
Thiago Farina667a9292012-11-12 21:32:59 -0200536 locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 dqp->q_res_bcount -=
538 (xfs_qcnt_t)qtrx->qt_blk_res;
539 }
540 if (qtrx->qt_ino_res) {
541 if (!locked) {
542 xfs_dqlock(dqp);
Thiago Farina667a9292012-11-12 21:32:59 -0200543 locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 dqp->q_res_icount -=
546 (xfs_qcnt_t)qtrx->qt_ino_res;
547 }
548
549 if (qtrx->qt_rtblk_res) {
550 if (!locked) {
551 xfs_dqlock(dqp);
Thiago Farina667a9292012-11-12 21:32:59 -0200552 locked = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 dqp->q_res_rtbcount -=
555 (xfs_qcnt_t)qtrx->qt_rtblk_res;
556 }
557 if (locked)
558 xfs_dqunlock(dqp);
559
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562}
563
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000564STATIC void
565xfs_quota_warn(
566 struct xfs_mount *mp,
567 struct xfs_dquot *dqp,
568 int type)
569{
570 /* no warnings for project quotas - we just return ENOSPC later */
571 if (dqp->dq_flags & XFS_DQ_PROJ)
572 return;
Eric W. Biederman431f1972012-09-16 02:32:43 -0700573 quota_send_warning(make_kqid(&init_user_ns,
574 (dqp->dq_flags & XFS_DQ_USER) ?
575 USRQUOTA : GRPQUOTA,
576 be32_to_cpu(dqp->q_core.d_id)),
577 mp->m_super->s_dev, type);
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
581 * This reserves disk blocks and inodes against a dquot.
582 * Flags indicate if the dquot is to be locked here and also
583 * if the blk reservation is for RT or regular blocks.
584 * Sending in XFS_QMOPT_FORCE_RES flag skips the quota check.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
586STATIC int
587xfs_trans_dqresv(
588 xfs_trans_t *tp,
589 xfs_mount_t *mp,
590 xfs_dquot_t *dqp,
591 long nblks,
592 long ninos,
593 uint flags)
594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 xfs_qcnt_t hardlimit;
596 xfs_qcnt_t softlimit;
Nathan Scott06d10dd2005-06-21 15:48:47 +1000597 time_t timer;
598 xfs_qwarncnt_t warns;
599 xfs_qwarncnt_t warnlimit;
Mitsuo Hayasaka70b54372012-02-06 12:51:05 +0000600 xfs_qcnt_t total_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 xfs_qcnt_t *resbcountp;
602 xfs_quotainfo_t *q = mp->m_quotainfo;
603
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100604
605 xfs_dqlock(dqp);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (flags & XFS_TRANS_DQ_RES_BLKS) {
Christoph Hellwig1149d962005-11-02 15:01:12 +1100608 hardlimit = be64_to_cpu(dqp->q_core.d_blk_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!hardlimit)
610 hardlimit = q->qi_bhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100611 softlimit = be64_to_cpu(dqp->q_core.d_blk_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!softlimit)
613 softlimit = q->qi_bsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100614 timer = be32_to_cpu(dqp->q_core.d_btimer);
615 warns = be16_to_cpu(dqp->q_core.d_bwarns);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000616 warnlimit = dqp->q_mount->m_quotainfo->qi_bwarnlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 resbcountp = &dqp->q_res_bcount;
618 } else {
619 ASSERT(flags & XFS_TRANS_DQ_RES_RTBLKS);
Christoph Hellwig1149d962005-11-02 15:01:12 +1100620 hardlimit = be64_to_cpu(dqp->q_core.d_rtb_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!hardlimit)
622 hardlimit = q->qi_rtbhardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100623 softlimit = be64_to_cpu(dqp->q_core.d_rtb_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!softlimit)
625 softlimit = q->qi_rtbsoftlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100626 timer = be32_to_cpu(dqp->q_core.d_rtbtimer);
627 warns = be16_to_cpu(dqp->q_core.d_rtbwarns);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000628 warnlimit = dqp->q_mount->m_quotainfo->qi_rtbwarnlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 resbcountp = &dqp->q_res_rtbcount;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if ((flags & XFS_QMOPT_FORCE_RES) == 0 &&
633 dqp->q_core.d_id &&
Kouta Ooizumie6d29422007-05-08 13:49:33 +1000634 ((XFS_IS_UQUOTA_ENFORCED(dqp->q_mount) && XFS_QM_ISUDQ(dqp)) ||
635 (XFS_IS_OQUOTA_ENFORCED(dqp->q_mount) &&
636 (XFS_QM_ISPDQ(dqp) || XFS_QM_ISGDQ(dqp))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (nblks > 0) {
638 /*
639 * dquot is locked already. See if we'd go over the
640 * hardlimit or exceed the timelimit if we allocate
641 * nblks.
642 */
Mitsuo Hayasaka70b54372012-02-06 12:51:05 +0000643 total_count = *resbcountp + nblks;
644 if (hardlimit && total_count > hardlimit) {
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000645 xfs_quota_warn(mp, dqp, QUOTA_NL_BHARDWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto error_return;
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000647 }
Mitsuo Hayasaka70b54372012-02-06 12:51:05 +0000648 if (softlimit && total_count > softlimit) {
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000649 if ((timer != 0 && get_seconds() > timer) ||
650 (warns != 0 && warns >= warnlimit)) {
651 xfs_quota_warn(mp, dqp,
652 QUOTA_NL_BSOFTLONGWARN);
653 goto error_return;
654 }
655
656 xfs_quota_warn(mp, dqp, QUOTA_NL_BSOFTWARN);
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 if (ninos > 0) {
Mitsuo Hayasaka70b54372012-02-06 12:51:05 +0000660 total_count = be64_to_cpu(dqp->q_core.d_icount) + ninos;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100661 timer = be32_to_cpu(dqp->q_core.d_itimer);
662 warns = be16_to_cpu(dqp->q_core.d_iwarns);
Christoph Hellwig8a7b8a82010-04-20 17:01:30 +1000663 warnlimit = dqp->q_mount->m_quotainfo->qi_iwarnlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100664 hardlimit = be64_to_cpu(dqp->q_core.d_ino_hardlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!hardlimit)
666 hardlimit = q->qi_ihardlimit;
Christoph Hellwig1149d962005-11-02 15:01:12 +1100667 softlimit = be64_to_cpu(dqp->q_core.d_ino_softlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!softlimit)
669 softlimit = q->qi_isoftlimit;
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000670
Mitsuo Hayasaka70b54372012-02-06 12:51:05 +0000671 if (hardlimit && total_count > hardlimit) {
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000672 xfs_quota_warn(mp, dqp, QUOTA_NL_IHARDWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto error_return;
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000674 }
Mitsuo Hayasaka70b54372012-02-06 12:51:05 +0000675 if (softlimit && total_count > softlimit) {
Christoph Hellwiga210c1a2010-01-17 22:36:19 +0000676 if ((timer != 0 && get_seconds() > timer) ||
677 (warns != 0 && warns >= warnlimit)) {
678 xfs_quota_warn(mp, dqp,
679 QUOTA_NL_ISOFTLONGWARN);
680 goto error_return;
681 }
682 xfs_quota_warn(mp, dqp, QUOTA_NL_ISOFTWARN);
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 }
686
687 /*
688 * Change the reservation, but not the actual usage.
689 * Note that q_res_bcount = q_core.d_bcount + resv
690 */
691 (*resbcountp) += (xfs_qcnt_t)nblks;
692 if (ninos != 0)
693 dqp->q_res_icount += (xfs_qcnt_t)ninos;
694
695 /*
696 * note the reservation amt in the trans struct too,
697 * so that the transaction knows how much was reserved by
698 * it against this particular dquot.
699 * We don't do this when we are reserving for a delayed allocation,
700 * because we don't have the luxury of a transaction envelope then.
701 */
702 if (tp) {
703 ASSERT(tp->t_dqinfo);
704 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
705 if (nblks != 0)
706 xfs_trans_mod_dquot(tp, dqp,
707 flags & XFS_QMOPT_RESBLK_MASK,
708 nblks);
709 if (ninos != 0)
710 xfs_trans_mod_dquot(tp, dqp,
711 XFS_TRANS_DQ_RES_INOS,
712 ninos);
713 }
Christoph Hellwig1149d962005-11-02 15:01:12 +1100714 ASSERT(dqp->q_res_bcount >= be64_to_cpu(dqp->q_core.d_bcount));
715 ASSERT(dqp->q_res_rtbcount >= be64_to_cpu(dqp->q_core.d_rtbcount));
716 ASSERT(dqp->q_res_icount >= be64_to_cpu(dqp->q_core.d_icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000718 xfs_dqunlock(dqp);
719 return 0;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721error_return:
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100722 xfs_dqunlock(dqp);
Christoph Hellwig4d1f88d2010-01-13 22:05:49 +0000723 if (flags & XFS_QMOPT_ENOSPC)
724 return ENOSPC;
725 return EDQUOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728
729/*
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000730 * Given dquot(s), make disk block and/or inode reservations against them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * The fact that this does the reservation against both the usr and
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000732 * grp/prj quotas is important, because this follows a both-or-nothing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * approach.
734 *
Christoph Hellwig8e9b6e72009-02-08 21:51:42 +0100735 * flags = XFS_QMOPT_FORCE_RES evades limit enforcement. Used by chown.
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000736 * XFS_QMOPT_ENOSPC returns ENOSPC not EDQUOT. Used by pquota.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * XFS_TRANS_DQ_RES_BLKS reserves regular disk blocks
738 * XFS_TRANS_DQ_RES_RTBLKS reserves realtime disk blocks
739 * dquots are unlocked on return, if they were not locked by caller.
740 */
741int
742xfs_trans_reserve_quota_bydquots(
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500743 struct xfs_trans *tp,
744 struct xfs_mount *mp,
745 struct xfs_dquot *udqp,
746 struct xfs_dquot *gdqp,
747 long nblks,
748 long ninos,
749 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500751 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Christoph Hellwig7d095252009-06-08 15:33:32 +0200753 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (tp && tp->t_dqinfo == NULL)
757 xfs_trans_alloc_dqinfo(tp);
758
759 ASSERT(flags & XFS_QMOPT_RESBLK_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (udqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000762 error = xfs_trans_dqresv(tp, mp, udqp, nblks, ninos,
763 (flags & ~XFS_QMOPT_ENOSPC));
764 if (error)
765 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768 if (gdqp) {
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000769 error = xfs_trans_dqresv(tp, mp, gdqp, nblks, ninos, flags);
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500770 if (error)
771 goto unwind_usr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000775 * Didn't change anything critical, so, no need to log
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000777 return 0;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500778
779unwind_usr:
780 flags |= XFS_QMOPT_FORCE_RES;
781 if (udqp)
782 xfs_trans_dqresv(tp, mp, udqp, -nblks, -ninos, flags);
783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786
787/*
788 * Lock the dquot and change the reservation if we can.
789 * This doesn't change the actual usage, just the reservation.
790 * The inode sent in is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200792int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793xfs_trans_reserve_quota_nblks(
Christoph Hellwig7d095252009-06-08 15:33:32 +0200794 struct xfs_trans *tp,
795 struct xfs_inode *ip,
796 long nblks,
797 long ninos,
798 uint flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Christoph Hellwig7d095252009-06-08 15:33:32 +0200800 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Christoph Hellwig7d095252009-06-08 15:33:32 +0200802 if (!XFS_IS_QUOTA_RUNNING(mp) || !XFS_IS_QUOTA_ON(mp))
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000803 return 0;
804 if (XFS_IS_PQUOTA_ON(mp))
805 flags |= XFS_QMOPT_ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Chandra Seetharaman9cad19d2013-06-27 17:25:04 -0500807 ASSERT(!xfs_is_quota_inode(&mp->m_sb, ip->i_ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000809 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Nathan Scott9a2a7de2006-03-31 13:04:49 +1000810 ASSERT((flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
811 XFS_TRANS_DQ_RES_RTBLKS ||
812 (flags & ~(XFS_QMOPT_FORCE_RES | XFS_QMOPT_ENOSPC)) ==
813 XFS_TRANS_DQ_RES_BLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /*
816 * Reserve nblks against these dquots, with trans as the mediator.
817 */
Christoph Hellwig7d095252009-06-08 15:33:32 +0200818 return xfs_trans_reserve_quota_bydquots(tp, mp,
819 ip->i_udquot, ip->i_gdquot,
820 nblks, ninos, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823/*
824 * This routine is called to allocate a quotaoff log item.
825 */
826xfs_qoff_logitem_t *
827xfs_trans_get_qoff_item(
828 xfs_trans_t *tp,
829 xfs_qoff_logitem_t *startqoff,
830 uint flags)
831{
832 xfs_qoff_logitem_t *q;
833
834 ASSERT(tp != NULL);
835
836 q = xfs_qm_qoff_logitem_init(tp->t_mountp, startqoff, flags);
837 ASSERT(q != NULL);
838
839 /*
840 * Get a log_item_desc to point at the new item.
841 */
Christoph Hellwige98c4142010-06-23 18:11:15 +1000842 xfs_trans_add_item(tp, &q->qql_item);
843 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846
847/*
848 * This is called to mark the quotaoff logitem as needing
849 * to be logged when the transaction is committed. The logitem must
850 * already be associated with the given transaction.
851 */
852void
853xfs_trans_log_quotaoff_item(
854 xfs_trans_t *tp,
855 xfs_qoff_logitem_t *qlp)
856{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 tp->t_flags |= XFS_TRANS_DIRTY;
Christoph Hellwige98c4142010-06-23 18:11:15 +1000858 qlp->qql_item.li_desc->lid_flags |= XFS_LID_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861STATIC void
862xfs_trans_alloc_dqinfo(
863 xfs_trans_t *tp)
864{
Christoph Hellwiga05931c2012-03-13 08:52:37 +0000865 tp->t_dqinfo = kmem_zone_zalloc(xfs_qm_dqtrxzone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Christoph Hellwig7d095252009-06-08 15:33:32 +0200868void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869xfs_trans_free_dqinfo(
870 xfs_trans_t *tp)
871{
872 if (!tp->t_dqinfo)
873 return;
Christoph Hellwiga05931c2012-03-13 08:52:37 +0000874 kmem_zone_free(xfs_qm_dqtrxzone, tp->t_dqinfo);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200875 tp->t_dqinfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}