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