Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 : * Copyright (C) 2010 Red Hat, Inc.
5 : * All Rights Reserved.
6 : */
7 : #include "xfs.h"
8 : #include "xfs_fs.h"
9 : #include "xfs_shared.h"
10 : #include "xfs_format.h"
11 : #include "xfs_log_format.h"
12 : #include "xfs_trans_resv.h"
13 : #include "xfs_mount.h"
14 : #include "xfs_extent_busy.h"
15 : #include "xfs_quota.h"
16 : #include "xfs_trans.h"
17 : #include "xfs_trans_priv.h"
18 : #include "xfs_log.h"
19 : #include "xfs_log_priv.h"
20 : #include "xfs_trace.h"
21 : #include "xfs_error.h"
22 : #include "xfs_defer.h"
23 : #include "xfs_inode.h"
24 : #include "xfs_dquot_item.h"
25 : #include "xfs_dquot.h"
26 : #include "xfs_icache.h"
27 :
28 : struct kmem_cache *xfs_trans_cache;
29 :
30 : #if defined(CONFIG_TRACEPOINTS)
31 : static void
32 60764 : xfs_trans_trace_reservations(
33 : struct xfs_mount *mp)
34 : {
35 60764 : struct xfs_trans_res *res;
36 60764 : struct xfs_trans_res *end_res;
37 60764 : int i;
38 :
39 60764 : res = (struct xfs_trans_res *)M_RES(mp);
40 60764 : end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
41 1640628 : for (i = 0; res < end_res; i++, res++)
42 1579864 : trace_xfs_trans_resv_calc(mp, i, res);
43 60764 : }
44 : #else
45 : # define xfs_trans_trace_reservations(mp)
46 : #endif
47 :
48 : /*
49 : * Initialize the precomputed transaction reservation values
50 : * in the mount structure.
51 : */
52 : void
53 60764 : xfs_trans_init(
54 : struct xfs_mount *mp)
55 : {
56 60764 : xfs_trans_resv_calc(mp, M_RES(mp));
57 60764 : xfs_trans_trace_reservations(mp);
58 60764 : }
59 :
60 : /*
61 : * Free the transaction structure. If there is more clean up
62 : * to do when the structure is freed, add it here.
63 : */
64 : STATIC void
65 4504140151 : xfs_trans_free(
66 : struct xfs_trans *tp)
67 : {
68 4504140151 : xfs_extent_busy_sort(&tp->t_busy);
69 4502569600 : xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
70 :
71 4503008410 : if (tp->t_dfops_finished > 0)
72 598876731 : trace_xfs_defer_stats(tp);
73 :
74 4502839388 : trace_xfs_trans_free(tp, _RET_IP_);
75 4502633636 : xfs_trans_clear_context(tp);
76 4502120980 : if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
77 1549480474 : sb_end_intwrite(tp->t_mountp->m_super);
78 4502658646 : xfs_trans_free_dqinfo(tp);
79 4502960192 : kmem_cache_free(xfs_trans_cache, tp);
80 4505567347 : }
81 :
82 : /*
83 : * This is called to create a new transaction which will share the
84 : * permanent log reservation of the given transaction. The remaining
85 : * unused block and rt extent reservations are also inherited. This
86 : * implies that the original transaction is no longer allowed to allocate
87 : * blocks. Locks and log items, however, are no inherited. They must
88 : * be added to the new transaction explicitly.
89 : */
90 : STATIC struct xfs_trans *
91 1330438415 : xfs_trans_dup(
92 : struct xfs_trans *tp)
93 : {
94 1330438415 : struct xfs_trans *ntp;
95 :
96 1330438415 : trace_xfs_trans_dup(tp, _RET_IP_);
97 :
98 1330422329 : ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
99 :
100 : /*
101 : * Initialize the new transaction structure.
102 : */
103 1330793336 : ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
104 1330793336 : ntp->t_mountp = tp->t_mountp;
105 1330793336 : INIT_LIST_HEAD(&ntp->t_items);
106 1330793336 : INIT_LIST_HEAD(&ntp->t_busy);
107 1330793336 : INIT_LIST_HEAD(&ntp->t_dfops);
108 1330793336 : ntp->t_highest_agno = NULLAGNUMBER;
109 :
110 1330793336 : ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
111 1330793336 : ASSERT(tp->t_ticket != NULL);
112 :
113 1330793336 : ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
114 1330793336 : (tp->t_flags & XFS_TRANS_RESERVE) |
115 1330793336 : (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
116 : (tp->t_flags & XFS_TRANS_RES_FDBLKS);
117 : /* We gave our writer reference to the new transaction */
118 1330793336 : tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
119 1330793336 : ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
120 :
121 1331067539 : ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
122 1331067539 : ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
123 1331067539 : tp->t_blk_res = tp->t_blk_res_used;
124 :
125 1331067539 : ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
126 1331067539 : tp->t_rtx_res = tp->t_rtx_res_used;
127 :
128 1331067539 : xfs_trans_switch_context(tp, ntp);
129 :
130 : /* move deferred ops over to the new tp */
131 1330950564 : xfs_defer_move(ntp, tp);
132 :
133 1330817843 : xfs_trans_dup_dqinfo(tp, ntp);
134 1330594309 : return ntp;
135 : }
136 :
137 : /*
138 : * Try to reserve more blocks for a transaction.
139 : *
140 : * This is for callers that need to attach resources to a transaction, scan
141 : * those resources to determine the space reservation requirements, and then
142 : * modify the attached resources. In other words, online repair. This can
143 : * fail due to ENOSPC, so the caller must be able to cancel the transaction
144 : * without shutting down the fs.
145 : */
146 : int
147 103387592 : xfs_trans_reserve_more(
148 : struct xfs_trans *tp,
149 : unsigned int blocks,
150 : unsigned int rtextents)
151 : {
152 103387592 : struct xfs_mount *mp = tp->t_mountp;
153 103387592 : bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
154 103387592 : int error = 0;
155 :
156 103387592 : ASSERT(!(tp->t_flags & XFS_TRANS_DIRTY));
157 :
158 : /*
159 : * Attempt to reserve the needed disk blocks by decrementing
160 : * the number needed from the number available. This will
161 : * fail if the count would go below zero.
162 : */
163 103387592 : if (blocks > 0) {
164 103387592 : error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
165 103387592 : if (error)
166 : return -ENOSPC;
167 103387580 : tp->t_blk_res += blocks;
168 : }
169 :
170 : /*
171 : * Attempt to reserve the needed realtime extents by decrementing
172 : * the number needed from the number available. This will
173 : * fail if the count would go below zero.
174 : */
175 103387580 : if (rtextents > 0) {
176 0 : error = xfs_mod_frextents(mp, -((int64_t)rtextents));
177 0 : if (error) {
178 0 : error = -ENOSPC;
179 0 : goto out_blocks;
180 : }
181 0 : tp->t_rtx_res += rtextents;
182 : }
183 :
184 : return 0;
185 : out_blocks:
186 0 : if (blocks > 0) {
187 0 : xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
188 0 : tp->t_blk_res -= blocks;
189 : }
190 : return error;
191 : }
192 :
193 : /*
194 : * This is called to reserve free disk blocks and log space for the
195 : * given transaction. This must be done before allocating any resources
196 : * within the transaction.
197 : *
198 : * This will return ENOSPC if there are not enough blocks available.
199 : * It will sleep waiting for available log space.
200 : * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
201 : * is used by long running transactions. If any one of the reservations
202 : * fails then they will all be backed out.
203 : *
204 : * This does not do quota reservations. That typically is done by the
205 : * caller afterwards.
206 : */
207 : static int
208 4506741308 : xfs_trans_reserve(
209 : struct xfs_trans *tp,
210 : struct xfs_trans_res *resp,
211 : uint blocks,
212 : uint rtextents)
213 : {
214 4506741308 : struct xfs_mount *mp = tp->t_mountp;
215 4506741308 : int error = 0;
216 4506741308 : bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
217 :
218 : /*
219 : * Attempt to reserve the needed disk blocks by decrementing
220 : * the number needed from the number available. This will
221 : * fail if the count would go below zero.
222 : */
223 4506741308 : if (blocks > 0) {
224 920280501 : error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
225 919485952 : if (error != 0)
226 : return -ENOSPC;
227 907555075 : tp->t_blk_res += blocks;
228 : }
229 :
230 : /*
231 : * Reserve the log space needed for this transaction.
232 : */
233 4494015882 : if (resp->tr_logres > 0) {
234 2868058441 : bool permanent = false;
235 :
236 2868058441 : ASSERT(tp->t_log_res == 0 ||
237 : tp->t_log_res == resp->tr_logres);
238 2868058441 : ASSERT(tp->t_log_count == 0 ||
239 : tp->t_log_count == resp->tr_logcount);
240 :
241 2868058441 : if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
242 2673846033 : tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
243 2673846033 : permanent = true;
244 : } else {
245 194212408 : ASSERT(tp->t_ticket == NULL);
246 194212408 : ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
247 : }
248 :
249 2868058441 : if (tp->t_ticket != NULL) {
250 1331368474 : ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
251 1331368474 : error = xfs_log_regrant(mp, tp->t_ticket);
252 : } else {
253 1536689967 : error = xfs_log_reserve(mp, resp->tr_logres,
254 : resp->tr_logcount,
255 : &tp->t_ticket, permanent);
256 : }
257 :
258 2869377441 : if (error)
259 2653 : goto undo_blocks;
260 :
261 2869374788 : tp->t_log_res = resp->tr_logres;
262 2869374788 : tp->t_log_count = resp->tr_logcount;
263 : }
264 :
265 : /*
266 : * Attempt to reserve the needed realtime extents by decrementing
267 : * the number needed from the number available. This will
268 : * fail if the count would go below zero.
269 : */
270 4495332229 : if (rtextents > 0) {
271 64838521 : error = xfs_mod_frextents(mp, -((int64_t)rtextents));
272 64827896 : if (error) {
273 3108900 : error = -ENOSPC;
274 3108900 : goto undo_log;
275 : }
276 61718996 : tp->t_rtx_res += rtextents;
277 : }
278 :
279 : return 0;
280 :
281 : /*
282 : * Error cases jump to one of these labels to undo any
283 : * reservations which have already been performed.
284 : */
285 : undo_log:
286 3108900 : if (resp->tr_logres > 0) {
287 3108900 : xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
288 3107579 : tp->t_ticket = NULL;
289 3107579 : tp->t_log_res = 0;
290 3107579 : tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
291 : }
292 :
293 0 : undo_blocks:
294 3110232 : if (blocks > 0) {
295 3107612 : xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
296 3106492 : tp->t_blk_res = 0;
297 : }
298 : return error;
299 : }
300 :
301 : int
302 3162525544 : xfs_trans_alloc(
303 : struct xfs_mount *mp,
304 : struct xfs_trans_res *resp,
305 : uint blocks,
306 : uint rtextents,
307 : uint flags,
308 : struct xfs_trans **tpp)
309 : {
310 3162525544 : struct xfs_trans *tp;
311 3162525544 : bool want_retry = true;
312 3170102018 : int error;
313 :
314 : /*
315 : * Allocate the handle before we do our freeze accounting and setting up
316 : * GFP_NOFS allocation context so that we avoid lockdep false positives
317 : * by doing GFP_KERNEL allocations inside sb_start_intwrite().
318 : */
319 3170102018 : retry:
320 3170102018 : tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
321 3172092120 : if (!(flags & XFS_TRANS_NO_WRITECOUNT))
322 1549072598 : sb_start_intwrite(mp->m_super);
323 3172051952 : xfs_trans_set_context(tp);
324 :
325 : /*
326 : * Zero-reservation ("empty") transactions can't modify anything, so
327 : * they're allowed to run while we're frozen.
328 : */
329 6335850688 : WARN_ON(resp->tr_logres > 0 &&
330 : mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
331 3167925344 : ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
332 : xfs_has_lazysbcount(mp));
333 :
334 3167925344 : tp->t_magic = XFS_TRANS_HEADER_MAGIC;
335 3167925344 : tp->t_flags = flags;
336 3167925344 : tp->t_mountp = mp;
337 3167925344 : INIT_LIST_HEAD(&tp->t_items);
338 3167925344 : INIT_LIST_HEAD(&tp->t_busy);
339 3167925344 : INIT_LIST_HEAD(&tp->t_dfops);
340 3167925344 : tp->t_highest_agno = NULLAGNUMBER;
341 :
342 3167925344 : error = xfs_trans_reserve(tp, resp, blocks, rtextents);
343 3168748095 : if (error == -ENOSPC && want_retry) {
344 7571967 : xfs_trans_cancel(tp);
345 :
346 : /*
347 : * We weren't able to reserve enough space for the transaction.
348 : * Flush the other speculative space allocations to free space.
349 : * Do not perform a synchronous scan because callers can hold
350 : * other locks.
351 : */
352 7560945 : error = xfs_blockgc_flush_all(mp);
353 7576474 : if (error)
354 0 : return error;
355 7576474 : want_retry = false;
356 7576474 : goto retry;
357 : }
358 3161176128 : if (error) {
359 7463499 : xfs_trans_cancel(tp);
360 7463499 : return error;
361 : }
362 :
363 3153712629 : trace_xfs_trans_alloc(tp, _RET_IP_);
364 :
365 3156005971 : *tpp = tp;
366 3156005971 : return 0;
367 : }
368 :
369 : /*
370 : * Create an empty transaction with no reservation. This is a defensive
371 : * mechanism for routines that query metadata without actually modifying them --
372 : * if the metadata being queried is somehow cross-linked (think a btree block
373 : * pointer that points higher in the tree), we risk deadlock. However, blocks
374 : * grabbed as part of a transaction can be re-grabbed. The verifiers will
375 : * notice the corrupt block and the operation will fail back to userspace
376 : * without deadlocking.
377 : *
378 : * Note the zero-length reservation; this transaction MUST be cancelled without
379 : * any dirty data.
380 : *
381 : * Callers should obtain freeze protection to avoid a conflict with fs freezing
382 : * where we can be grabbing buffers at the same time that freeze is trying to
383 : * drain the buffer LRU list.
384 : */
385 : int
386 1622702148 : xfs_trans_alloc_empty(
387 : struct xfs_mount *mp,
388 : struct xfs_trans **tpp)
389 : {
390 1622702148 : struct xfs_trans_res resv = {0};
391 :
392 1622702148 : return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
393 : }
394 :
395 : /*
396 : * Record the indicated change to the given field for application
397 : * to the file system's superblock when the transaction commits.
398 : * For now, just store the change in the transaction structure.
399 : *
400 : * Mark the transaction structure to indicate that the superblock
401 : * needs to be updated before committing.
402 : *
403 : * Because we may not be keeping track of allocated/free inodes and
404 : * used filesystem blocks in the superblock, we do not mark the
405 : * superblock dirty in this transaction if we modify these fields.
406 : * We still need to update the transaction deltas so that they get
407 : * applied to the incore superblock, but we don't want them to
408 : * cause the superblock to get locked and logged if these are the
409 : * only fields in the superblock that the transaction modifies.
410 : */
411 : void
412 491810787 : xfs_trans_mod_sb(
413 : xfs_trans_t *tp,
414 : uint field,
415 : int64_t delta)
416 : {
417 491810787 : uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
418 491810787 : xfs_mount_t *mp = tp->t_mountp;
419 :
420 491810787 : switch (field) {
421 1757168 : case XFS_TRANS_SB_ICOUNT:
422 1757168 : tp->t_icount_delta += delta;
423 1757168 : if (xfs_has_lazysbcount(mp))
424 1757079 : flags &= ~XFS_TRANS_SB_DIRTY;
425 : break;
426 214323295 : case XFS_TRANS_SB_IFREE:
427 214323295 : tp->t_ifree_delta += delta;
428 214323295 : if (xfs_has_lazysbcount(mp))
429 214126784 : flags &= ~XFS_TRANS_SB_DIRTY;
430 : break;
431 154967186 : case XFS_TRANS_SB_FDBLOCKS:
432 : /*
433 : * Track the number of blocks allocated in the transaction.
434 : * Make sure it does not exceed the number reserved. If so,
435 : * shutdown as this can lead to accounting inconsistency.
436 : */
437 154967186 : if (delta < 0) {
438 56720036 : tp->t_blk_res_used += (uint)-delta;
439 56720036 : if (tp->t_blk_res_used > tp->t_blk_res)
440 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
441 98247150 : } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
442 222299 : int64_t blkres_delta;
443 :
444 : /*
445 : * Return freed blocks directly to the reservation
446 : * instead of the global pool, being careful not to
447 : * overflow the trans counter. This is used to preserve
448 : * reservation across chains of transaction rolls that
449 : * repeatedly free and allocate blocks.
450 : */
451 222299 : blkres_delta = min_t(int64_t, delta,
452 : UINT_MAX - tp->t_blk_res);
453 222299 : tp->t_blk_res += blkres_delta;
454 222299 : delta -= blkres_delta;
455 : }
456 154967186 : tp->t_fdblocks_delta += delta;
457 154967186 : if (xfs_has_lazysbcount(mp))
458 154955914 : flags &= ~XFS_TRANS_SB_DIRTY;
459 : break;
460 28562719 : case XFS_TRANS_SB_RES_FDBLOCKS:
461 : /*
462 : * The allocation has already been applied to the
463 : * in-core superblock's counter. This should only
464 : * be applied to the on-disk superblock.
465 : */
466 28562719 : tp->t_res_fdblocks_delta += delta;
467 28562719 : if (xfs_has_lazysbcount(mp))
468 28562693 : flags &= ~XFS_TRANS_SB_DIRTY;
469 : break;
470 92199045 : case XFS_TRANS_SB_FREXTENTS:
471 : /*
472 : * Track the number of blocks allocated in the
473 : * transaction. Make sure it does not exceed the
474 : * number reserved.
475 : */
476 92199045 : if (delta < 0) {
477 57355570 : tp->t_rtx_res_used += (uint)-delta;
478 57355570 : ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
479 : }
480 92199045 : tp->t_frextents_delta += delta;
481 92199045 : break;
482 0 : case XFS_TRANS_SB_RES_FREXTENTS:
483 : /*
484 : * The allocation has already been applied to the
485 : * in-core superblock's counter. This should only
486 : * be applied to the on-disk superblock.
487 : */
488 0 : ASSERT(delta < 0);
489 0 : tp->t_res_frextents_delta += delta;
490 0 : break;
491 753 : case XFS_TRANS_SB_DBLOCKS:
492 753 : tp->t_dblocks_delta += delta;
493 753 : break;
494 453 : case XFS_TRANS_SB_AGCOUNT:
495 453 : ASSERT(delta > 0);
496 453 : tp->t_agcount_delta += delta;
497 453 : break;
498 0 : case XFS_TRANS_SB_IMAXPCT:
499 0 : tp->t_imaxpct_delta += delta;
500 0 : break;
501 0 : case XFS_TRANS_SB_REXTSIZE:
502 0 : tp->t_rextsize_delta += delta;
503 0 : break;
504 27 : case XFS_TRANS_SB_RBMBLOCKS:
505 27 : tp->t_rbmblocks_delta += delta;
506 27 : break;
507 58 : case XFS_TRANS_SB_RBLOCKS:
508 58 : tp->t_rblocks_delta += delta;
509 58 : break;
510 58 : case XFS_TRANS_SB_REXTENTS:
511 58 : tp->t_rextents_delta += delta;
512 58 : break;
513 25 : case XFS_TRANS_SB_REXTSLOG:
514 25 : tp->t_rextslog_delta += delta;
515 25 : break;
516 0 : default:
517 0 : ASSERT(0);
518 0 : return;
519 : }
520 :
521 491810787 : tp->t_flags |= flags;
522 : }
523 :
524 : /*
525 : * xfs_trans_apply_sb_deltas() is called from the commit code
526 : * to bring the superblock buffer into the current transaction
527 : * and modify it as requested by earlier calls to xfs_trans_mod_sb().
528 : *
529 : * For now we just look at each field allowed to change and change
530 : * it if necessary.
531 : */
532 : STATIC void
533 86193419 : xfs_trans_apply_sb_deltas(
534 : xfs_trans_t *tp)
535 : {
536 86193419 : struct xfs_dsb *sbp;
537 86193419 : struct xfs_buf *bp;
538 86193419 : int whole = 0;
539 :
540 86193419 : bp = xfs_trans_getsb(tp);
541 86193419 : sbp = bp->b_addr;
542 :
543 : /*
544 : * Only update the superblock counters if we are logging them
545 : */
546 86193419 : if (!xfs_has_lazysbcount((tp->t_mountp))) {
547 1131 : if (tp->t_icount_delta)
548 11 : be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
549 1131 : if (tp->t_ifree_delta)
550 1121 : be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
551 1131 : if (tp->t_fdblocks_delta)
552 32 : be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
553 1131 : if (tp->t_res_fdblocks_delta)
554 0 : be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
555 : }
556 :
557 : /*
558 : * Updating frextents requires careful handling because it does not
559 : * behave like the lazysb counters because we cannot rely on log
560 : * recovery in older kenels to recompute the value from the rtbitmap.
561 : * This means that the ondisk frextents must be consistent with the
562 : * rtbitmap.
563 : *
564 : * Therefore, log the frextents change to the ondisk superblock and
565 : * update the incore superblock so that future calls to xfs_log_sb
566 : * write the correct value ondisk.
567 : *
568 : * Don't touch m_frextents because it includes incore reservations,
569 : * and those are handled by the unreserve function.
570 : */
571 86193419 : if (tp->t_frextents_delta || tp->t_res_frextents_delta) {
572 86191535 : struct xfs_mount *mp = tp->t_mountp;
573 86191535 : int64_t rtxdelta;
574 :
575 86191535 : rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta;
576 :
577 86191535 : spin_lock(&mp->m_sb_lock);
578 86191535 : be64_add_cpu(&sbp->sb_frextents, rtxdelta);
579 86191535 : mp->m_sb.sb_frextents += rtxdelta;
580 86191535 : spin_unlock(&mp->m_sb_lock);
581 : }
582 :
583 86193419 : if (tp->t_dblocks_delta) {
584 753 : be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
585 753 : whole = 1;
586 : }
587 86193419 : if (tp->t_agcount_delta) {
588 453 : be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
589 453 : whole = 1;
590 : }
591 86193419 : if (tp->t_imaxpct_delta) {
592 0 : sbp->sb_imax_pct += tp->t_imaxpct_delta;
593 0 : whole = 1;
594 : }
595 86193419 : if (tp->t_rextsize_delta) {
596 0 : be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
597 0 : whole = 1;
598 : }
599 86193419 : if (tp->t_rbmblocks_delta) {
600 27 : be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
601 27 : whole = 1;
602 : }
603 86193419 : if (tp->t_rblocks_delta) {
604 58 : be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
605 58 : whole = 1;
606 : }
607 86193419 : if (tp->t_rextents_delta) {
608 58 : be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
609 58 : whole = 1;
610 : }
611 86193419 : if (tp->t_rextslog_delta) {
612 25 : sbp->sb_rextslog += tp->t_rextslog_delta;
613 25 : whole = 1;
614 : }
615 :
616 86193419 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
617 86193419 : if (whole)
618 : /*
619 : * Log the whole thing, the fields are noncontiguous.
620 : */
621 811 : xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
622 : else
623 : /*
624 : * Since all the modifiable fields are contiguous, we
625 : * can get away with this.
626 : */
627 86192608 : xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount),
628 : offsetof(struct xfs_dsb, sb_frextents) +
629 : sizeof(sbp->sb_frextents) - 1);
630 86193419 : }
631 :
632 : /*
633 : * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
634 : * apply superblock counter changes to the in-core superblock. The
635 : * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
636 : * applied to the in-core superblock. The idea is that that has already been
637 : * done.
638 : *
639 : * If we are not logging superblock counters, then the inode allocated/free and
640 : * used block counts are not updated in the on disk superblock. In this case,
641 : * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
642 : * still need to update the incore superblock with the changes.
643 : *
644 : * Deltas for the inode count are +/-64, hence we use a large batch size of 128
645 : * so we don't need to take the counter lock on every update.
646 : */
647 : #define XFS_ICOUNT_BATCH 128
648 :
649 : void
650 4505582599 : xfs_trans_unreserve_and_mod_sb(
651 : struct xfs_trans *tp)
652 : {
653 4505582599 : struct xfs_mount *mp = tp->t_mountp;
654 4505582599 : bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
655 4505582599 : int64_t blkdelta = 0;
656 4505582599 : int64_t rtxdelta = 0;
657 4505582599 : int64_t idelta = 0;
658 4505582599 : int64_t ifreedelta = 0;
659 4505582599 : int error;
660 :
661 : /* calculate deltas */
662 4505582599 : if (tp->t_blk_res > 0)
663 : blkdelta = tp->t_blk_res;
664 4505582599 : if ((tp->t_fdblocks_delta != 0) &&
665 32 : (xfs_has_lazysbcount(mp) ||
666 32 : (tp->t_flags & XFS_TRANS_SB_DIRTY)))
667 147986080 : blkdelta += tp->t_fdblocks_delta;
668 :
669 4505582599 : if (tp->t_rtx_res > 0)
670 : rtxdelta = tp->t_rtx_res;
671 4505582599 : if ((tp->t_frextents_delta != 0) &&
672 86191535 : (tp->t_flags & XFS_TRANS_SB_DIRTY))
673 86191535 : rtxdelta += tp->t_frextents_delta;
674 :
675 4505582599 : if (xfs_has_lazysbcount(mp) ||
676 2403 : (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
677 4505581327 : idelta = tp->t_icount_delta;
678 4505581327 : ifreedelta = tp->t_ifree_delta;
679 : }
680 :
681 : /* apply the per-cpu counters */
682 4505582599 : if (blkdelta) {
683 1068296898 : error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
684 1068370831 : ASSERT(!error);
685 : }
686 :
687 4505656532 : if (idelta)
688 1757250 : percpu_counter_add_batch(&mp->m_icount, idelta,
689 : XFS_ICOUNT_BATCH);
690 :
691 4505656624 : if (ifreedelta)
692 214966099 : percpu_counter_add(&mp->m_ifree, ifreedelta);
693 :
694 4505672894 : if (rtxdelta) {
695 37451434 : error = xfs_mod_frextents(mp, rtxdelta);
696 37451435 : ASSERT(!error);
697 : }
698 :
699 4505672895 : if (!(tp->t_flags & XFS_TRANS_SB_DIRTY))
700 : return;
701 :
702 : /* apply remaining deltas */
703 86193419 : spin_lock(&mp->m_sb_lock);
704 86193419 : mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
705 86193419 : mp->m_sb.sb_icount += idelta;
706 86193419 : mp->m_sb.sb_ifree += ifreedelta;
707 : /*
708 : * Do not touch sb_frextents here because we are dealing with incore
709 : * reservation. sb_frextents is not part of the lazy sb counters so it
710 : * must be consistent with the ondisk rtbitmap and must never include
711 : * incore reservations.
712 : */
713 86193419 : mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
714 86193419 : mp->m_sb.sb_agcount += tp->t_agcount_delta;
715 86193419 : mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
716 86193419 : mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
717 86193419 : mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
718 86193419 : mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
719 86193419 : mp->m_sb.sb_rextents += tp->t_rextents_delta;
720 86193419 : mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
721 86193419 : spin_unlock(&mp->m_sb_lock);
722 :
723 : /*
724 : * Debug checks outside of the spinlock so they don't lock up the
725 : * machine if they fail.
726 : */
727 : ASSERT(mp->m_sb.sb_imax_pct >= 0);
728 : ASSERT(mp->m_sb.sb_rextslog >= 0);
729 : return;
730 : }
731 :
732 : /* Add the given log item to the transaction's list of log items. */
733 : void
734 32754354259 : xfs_trans_add_item(
735 : struct xfs_trans *tp,
736 : struct xfs_log_item *lip)
737 : {
738 32754354259 : ASSERT(lip->li_log == tp->t_mountp->m_log);
739 32754354259 : ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
740 32754354259 : ASSERT(list_empty(&lip->li_trans));
741 32754354259 : ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
742 :
743 32754354259 : list_add_tail(&lip->li_trans, &tp->t_items);
744 32732303830 : trace_xfs_trans_add_item(tp, _RET_IP_);
745 32726026254 : }
746 :
747 : /*
748 : * Unlink the log item from the transaction. the log item is no longer
749 : * considered dirty in this transaction, as the linked transaction has
750 : * finished, either by abort or commit completion.
751 : */
752 : void
753 32678134794 : xfs_trans_del_item(
754 : struct xfs_log_item *lip)
755 : {
756 32678134794 : clear_bit(XFS_LI_DIRTY, &lip->li_flags);
757 32696374178 : list_del_init(&lip->li_trans);
758 32689808146 : }
759 :
760 : /* Detach and unlock all of the items in a transaction */
761 : static void
762 2053606849 : xfs_trans_free_items(
763 : struct xfs_trans *tp,
764 : bool abort)
765 : {
766 2053606849 : struct xfs_log_item *lip, *next;
767 :
768 2053606849 : trace_xfs_trans_free_items(tp, _RET_IP_);
769 :
770 2501635393 : list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
771 448805655 : xfs_trans_del_item(lip);
772 448568234 : if (abort)
773 25670 : set_bit(XFS_LI_ABORTED, &lip->li_flags);
774 448568232 : if (lip->li_ops->iop_release)
775 448568232 : lip->li_ops->iop_release(lip);
776 : }
777 2052829738 : }
778 :
779 : static inline void
780 31830749 : xfs_log_item_batch_insert(
781 : struct xfs_ail *ailp,
782 : struct xfs_ail_cursor *cur,
783 : struct xfs_log_item **log_items,
784 : int nr_items,
785 : xfs_lsn_t commit_lsn)
786 : {
787 31830749 : int i;
788 :
789 31830749 : spin_lock(&ailp->ail_lock);
790 : /* xfs_trans_ail_update_bulk drops ailp->ail_lock */
791 31830749 : xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
792 :
793 964481223 : for (i = 0; i < nr_items; i++) {
794 900819725 : struct xfs_log_item *lip = log_items[i];
795 :
796 900819725 : if (lip->li_ops->iop_unpin)
797 900819725 : lip->li_ops->iop_unpin(lip, 0);
798 : }
799 31830749 : }
800 :
801 : /*
802 : * Bulk operation version of xfs_trans_committed that takes a log vector of
803 : * items to insert into the AIL. This uses bulk AIL insertion techniques to
804 : * minimise lock traffic.
805 : *
806 : * If we are called with the aborted flag set, it is because a log write during
807 : * a CIL checkpoint commit has failed. In this case, all the items in the
808 : * checkpoint have already gone through iop_committed and iop_committing, which
809 : * means that checkpoint commit abort handling is treated exactly the same
810 : * as an iclog write error even though we haven't started any IO yet. Hence in
811 : * this case all we need to do is iop_committed processing, followed by an
812 : * iop_unpin(aborted) call.
813 : *
814 : * The AIL cursor is used to optimise the insert process. If commit_lsn is not
815 : * at the end of the AIL, the insert cursor avoids the need to walk
816 : * the AIL to find the insertion point on every xfs_log_item_batch_insert()
817 : * call. This saves a lot of needless list walking and is a net win, even
818 : * though it slightly increases that amount of AIL lock traffic to set it up
819 : * and tear it down.
820 : */
821 : void
822 5800524 : xfs_trans_committed_bulk(
823 : struct xfs_ail *ailp,
824 : struct list_head *lv_chain,
825 : xfs_lsn_t commit_lsn,
826 : bool aborted)
827 : {
828 : #define LOG_ITEM_BATCH_SIZE 32
829 5800524 : struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
830 5800524 : struct xfs_log_vec *lv;
831 5800524 : struct xfs_ail_cursor cur;
832 5800524 : int i = 0;
833 :
834 5800524 : spin_lock(&ailp->ail_lock);
835 5800524 : xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
836 5800524 : spin_unlock(&ailp->ail_lock);
837 :
838 : /* unpin all the log items */
839 931503834 : list_for_each_entry(lv, lv_chain, lv_list) {
840 925703310 : struct xfs_log_item *lip = lv->lv_item;
841 925703310 : xfs_lsn_t item_lsn;
842 :
843 925703310 : if (aborted)
844 1481968 : set_bit(XFS_LI_ABORTED, &lip->li_flags);
845 :
846 925703308 : if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) {
847 6963951 : lip->li_ops->iop_release(lip);
848 6963951 : continue;
849 : }
850 :
851 918739357 : if (lip->li_ops->iop_committed)
852 866379940 : item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
853 : else
854 : item_lsn = commit_lsn;
855 :
856 : /* item_lsn of -1 means the item needs no further processing */
857 918739359 : if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
858 16054572 : continue;
859 :
860 : /*
861 : * if we are aborting the operation, no point in inserting the
862 : * object into the AIL as we are in a shutdown situation.
863 : */
864 902684787 : if (aborted) {
865 2876626 : ASSERT(xlog_is_shutdown(ailp->ail_log));
866 1438313 : if (lip->li_ops->iop_unpin)
867 1438313 : lip->li_ops->iop_unpin(lip, 1);
868 1438313 : continue;
869 : }
870 :
871 901246474 : if (item_lsn != commit_lsn) {
872 :
873 : /*
874 : * Not a bulk update option due to unusual item_lsn.
875 : * Push into AIL immediately, rechecking the lsn once
876 : * we have the ail lock. Then unpin the item. This does
877 : * not affect the AIL cursor the bulk insert path is
878 : * using.
879 : */
880 426749 : spin_lock(&ailp->ail_lock);
881 426749 : if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
882 0 : xfs_trans_ail_update(ailp, lip, item_lsn);
883 : else
884 426749 : spin_unlock(&ailp->ail_lock);
885 426749 : if (lip->li_ops->iop_unpin)
886 426749 : lip->li_ops->iop_unpin(lip, 0);
887 426749 : continue;
888 : }
889 :
890 : /* Item is a candidate for bulk AIL insert. */
891 900819725 : log_items[i++] = lv->lv_item;
892 900819725 : if (i >= LOG_ITEM_BATCH_SIZE) {
893 26121119 : xfs_log_item_batch_insert(ailp, &cur, log_items,
894 : LOG_ITEM_BATCH_SIZE, commit_lsn);
895 26121119 : i = 0;
896 : }
897 : }
898 :
899 : /* make sure we insert the remainder! */
900 5800524 : if (i)
901 5709630 : xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
902 :
903 5800524 : spin_lock(&ailp->ail_lock);
904 5800524 : xfs_trans_ail_cursor_done(&cur);
905 5800524 : spin_unlock(&ailp->ail_lock);
906 5800524 : }
907 :
908 : /*
909 : * Sort transaction items prior to running precommit operations. This will
910 : * attempt to order the items such that they will always be locked in the same
911 : * order. Items that have no sort function are moved to the end of the list
912 : * and so are locked last.
913 : *
914 : * This may need refinement as different types of objects add sort functions.
915 : *
916 : * Function is more complex than it needs to be because we are comparing 64 bit
917 : * values and the function only returns 32 bit values.
918 : */
919 : static int
920 15093352193 : xfs_trans_precommit_sort(
921 : void *unused_arg,
922 : const struct list_head *a,
923 : const struct list_head *b)
924 : {
925 15093352193 : struct xfs_log_item *lia = container_of(a,
926 : struct xfs_log_item, li_trans);
927 15093352193 : struct xfs_log_item *lib = container_of(b,
928 : struct xfs_log_item, li_trans);
929 15093352193 : int64_t diff;
930 :
931 : /*
932 : * If both items are non-sortable, leave them alone. If only one is
933 : * sortable, move the non-sortable item towards the end of the list.
934 : */
935 15093352193 : if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort)
936 : return 0;
937 7949686062 : if (!lia->li_ops->iop_sort)
938 : return 1;
939 7548672592 : if (!lib->li_ops->iop_sort)
940 : return -1;
941 :
942 1844394397 : diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib);
943 1844446762 : if (diff < 0)
944 : return -1;
945 416359413 : if (diff > 0)
946 361912241 : return 1;
947 : return 0;
948 : }
949 :
950 : /*
951 : * Run transaction precommit functions.
952 : *
953 : * If there is an error in any of the callouts, then stop immediately and
954 : * trigger a shutdown to abort the transaction. There is no recovery possible
955 : * from errors at this point as the transaction is dirty....
956 : */
957 : static int
958 3525468276 : xfs_trans_run_precommits(
959 : struct xfs_trans *tp)
960 : {
961 3525468276 : struct xfs_mount *mp = tp->t_mountp;
962 3525468276 : struct xfs_log_item *lip, *n;
963 3525468276 : int error = 0;
964 :
965 : /*
966 : * Sort the item list to avoid ABBA deadlocks with other transactions
967 : * running precommit operations that lock multiple shared items such as
968 : * inode cluster buffers.
969 : */
970 3525468276 : list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort);
971 :
972 : /*
973 : * Precommit operations can remove the log item from the transaction
974 : * if the log item exists purely to delay modifications until they
975 : * can be ordered against other operations. Hence we have to use
976 : * list_for_each_entry_safe() here.
977 : */
978 16406453939 : list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
979 12879109263 : if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
980 1814804939 : continue;
981 11064304324 : if (lip->li_ops->iop_precommit) {
982 3925881225 : error = lip->li_ops->iop_precommit(tp, lip);
983 3926744228 : if (error)
984 : break;
985 : }
986 : }
987 3527344981 : if (error)
988 305 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
989 3527344981 : return error;
990 : }
991 :
992 : /*
993 : * Commit the given transaction to the log.
994 : *
995 : * XFS disk error handling mechanism is not based on a typical
996 : * transaction abort mechanism. Logically after the filesystem
997 : * gets marked 'SHUTDOWN', we can't let any new transactions
998 : * be durable - ie. committed to disk - because some metadata might
999 : * be inconsistent. In such cases, this returns an error, and the
1000 : * caller may assume that all locked objects joined to the transaction
1001 : * have already been unlocked as if the commit had succeeded.
1002 : * Do not reference the transaction structure after this call.
1003 : */
1004 : static int
1005 2518809247 : __xfs_trans_commit(
1006 : struct xfs_trans *tp,
1007 : bool regrant)
1008 : {
1009 2518809247 : struct xfs_mount *mp = tp->t_mountp;
1010 2518809247 : struct xlog *log = mp->m_log;
1011 2518809247 : xfs_csn_t commit_seq = 0;
1012 2518809247 : int error = 0;
1013 2518809247 : int sync = tp->t_flags & XFS_TRANS_SYNC;
1014 :
1015 2518809247 : trace_xfs_trans_commit(tp, _RET_IP_);
1016 :
1017 2517808507 : error = xfs_trans_run_precommits(tp);
1018 2518111314 : if (error) {
1019 305 : if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
1020 294 : xfs_defer_cancel(tp);
1021 305 : goto out_unreserve;
1022 : }
1023 :
1024 : /*
1025 : * Finish deferred items on final commit. Only permanent transactions
1026 : * should ever have deferred ops.
1027 : */
1028 5037292772 : WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
1029 : !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
1030 2518111009 : if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
1031 1007522280 : error = xfs_defer_finish_noroll(&tp);
1032 1007363340 : if (error)
1033 3193 : goto out_unreserve;
1034 :
1035 : /* Run precommits from final tx in defer chain. */
1036 1007360147 : error = xfs_trans_run_precommits(tp);
1037 1008211995 : if (error)
1038 0 : goto out_unreserve;
1039 : }
1040 :
1041 : /*
1042 : * If there is nothing to be logged by the transaction,
1043 : * then unlock all of the items associated with the
1044 : * transaction and free the transaction structure.
1045 : * Also make sure to return any reserved blocks to
1046 : * the free pool.
1047 : */
1048 2518800724 : if (!(tp->t_flags & XFS_TRANS_DIRTY))
1049 69233434 : goto out_unreserve;
1050 :
1051 : /*
1052 : * We must check against log shutdown here because we cannot abort log
1053 : * items and leave them dirty, inconsistent and unpinned in memory while
1054 : * the log is active. This leaves them open to being written back to
1055 : * disk, and that will lead to on-disk corruption.
1056 : */
1057 4899134580 : if (xlog_is_shutdown(log)) {
1058 416 : error = -EIO;
1059 416 : goto out_unreserve;
1060 : }
1061 :
1062 2449566874 : ASSERT(tp->t_ticket != NULL);
1063 :
1064 : /*
1065 : * If we need to update the superblock, then do it now.
1066 : */
1067 2449566874 : if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1068 86193419 : xfs_trans_apply_sb_deltas(tp);
1069 2449566874 : xfs_trans_apply_dquot_deltas(tp);
1070 :
1071 2450351784 : xlog_cil_commit(log, tp, &commit_seq, regrant);
1072 :
1073 2451046471 : xfs_trans_free(tp);
1074 :
1075 : /*
1076 : * If the transaction needs to be synchronous, then force the
1077 : * log out now and wait for it.
1078 : */
1079 2450949160 : if (sync) {
1080 235135 : error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL);
1081 235135 : XFS_STATS_INC(mp, xs_trans_sync);
1082 : } else {
1083 2450714025 : XFS_STATS_INC(mp, xs_trans_async);
1084 : }
1085 :
1086 : return error;
1087 :
1088 69237348 : out_unreserve:
1089 69237348 : xfs_trans_unreserve_and_mod_sb(tp);
1090 :
1091 : /*
1092 : * It is indeed possible for the transaction to be not dirty but
1093 : * the dqinfo portion to be. All that means is that we have some
1094 : * (non-persistent) quota reservations that need to be unreserved.
1095 : */
1096 69235207 : xfs_trans_unreserve_and_mod_dquots(tp);
1097 69228605 : if (tp->t_ticket) {
1098 71378764 : if (regrant && !xlog_is_shutdown(log))
1099 2200303 : xfs_log_ticket_regrant(log, tp->t_ticket);
1100 : else
1101 66977641 : xfs_log_ticket_ungrant(log, tp->t_ticket);
1102 69195228 : tp->t_ticket = NULL;
1103 : }
1104 69245889 : xfs_trans_free_items(tp, !!error);
1105 69240714 : xfs_trans_free(tp);
1106 :
1107 69244769 : XFS_STATS_INC(mp, xs_trans_empty);
1108 69248726 : return error;
1109 : }
1110 :
1111 : int
1112 1188623536 : xfs_trans_commit(
1113 : struct xfs_trans *tp)
1114 : {
1115 1188623536 : return __xfs_trans_commit(tp, false);
1116 : }
1117 :
1118 : /*
1119 : * Unlock all of the transaction's items and free the transaction. If the
1120 : * transaction is dirty, we must shut down the filesystem because there is no
1121 : * way to restore them to their previous state.
1122 : *
1123 : * If the transaction has made a log reservation, make sure to release it as
1124 : * well.
1125 : *
1126 : * This is a high level function (equivalent to xfs_trans_commit()) and so can
1127 : * be called after the transaction has effectively been aborted due to the mount
1128 : * being shut down. However, if the mount has not been shut down and the
1129 : * transaction is dirty we will shut the mount down and, in doing so, that
1130 : * guarantees that the log is shut down, too. Hence we don't need to be as
1131 : * careful with shutdown state and dirty items here as we need to be in
1132 : * xfs_trans_commit().
1133 : */
1134 : void
1135 1985824827 : xfs_trans_cancel(
1136 : struct xfs_trans *tp)
1137 : {
1138 1985824827 : struct xfs_mount *mp = tp->t_mountp;
1139 1985824827 : struct xlog *log = mp->m_log;
1140 1985824827 : bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
1141 :
1142 1985824827 : trace_xfs_trans_cancel(tp, _RET_IP_);
1143 :
1144 : /*
1145 : * It's never valid to cancel a transaction with deferred ops attached,
1146 : * because the transaction is effectively dirty. Complain about this
1147 : * loudly before freeing the in-memory defer items and shutting down the
1148 : * filesystem.
1149 : */
1150 1984180258 : if (!list_empty(&tp->t_dfops)) {
1151 50 : ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1152 50 : dirty = true;
1153 50 : xfs_defer_cancel(tp);
1154 : }
1155 :
1156 : /*
1157 : * See if the caller is relying on us to shut down the filesystem. We
1158 : * only want an error report if there isn't already a shutdown in
1159 : * progress, so we only need to check against the mount shutdown state
1160 : * here.
1161 : */
1162 1984184035 : if (dirty && !xfs_is_shutdown(mp)) {
1163 18 : XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1164 18 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1165 : }
1166 : #ifdef DEBUG
1167 : /* Log items need to be consistent until the log is shut down. */
1168 3969088246 : if (!dirty && !xlog_is_shutdown(log)) {
1169 1984920833 : struct xfs_log_item *lip;
1170 :
1171 2388603895 : list_for_each_entry(lip, &tp->t_items, li_trans)
1172 403683062 : ASSERT(!xlog_item_is_intent_done(lip));
1173 : }
1174 : #endif
1175 1984180258 : xfs_trans_unreserve_and_mod_sb(tp);
1176 1984758868 : xfs_trans_unreserve_and_mod_dquots(tp);
1177 :
1178 1985241932 : if (tp->t_ticket) {
1179 346477018 : xfs_log_ticket_ungrant(log, tp->t_ticket);
1180 346446689 : tp->t_ticket = NULL;
1181 : }
1182 :
1183 1985211603 : xfs_trans_free_items(tp, dirty);
1184 1984264521 : xfs_trans_free(tp);
1185 1986223441 : }
1186 :
1187 : /*
1188 : * Roll from one trans in the sequence of PERMANENT transactions to
1189 : * the next: permanent transactions are only flushed out when
1190 : * committed with xfs_trans_commit(), but we still want as soon
1191 : * as possible to let chunks of it go to the log. So we commit the
1192 : * chunk we've been working on and get a new transaction to continue.
1193 : */
1194 : int
1195 1330521417 : xfs_trans_roll(
1196 : struct xfs_trans **tpp)
1197 : {
1198 1330521417 : struct xfs_trans *trans = *tpp;
1199 1330521417 : struct xfs_trans_res tres;
1200 1330521417 : int error;
1201 :
1202 1330521417 : trace_xfs_trans_roll(trans, _RET_IP_);
1203 :
1204 : /*
1205 : * Copy the critical parameters from one trans to the next.
1206 : */
1207 1330416448 : tres.tr_logres = trans->t_log_res;
1208 1330416448 : tres.tr_logcount = trans->t_log_count;
1209 :
1210 1330416448 : *tpp = xfs_trans_dup(trans);
1211 :
1212 : /*
1213 : * Commit the current transaction.
1214 : * If this commit failed, then it'd just unlock those items that
1215 : * are not marked ihold. That also means that a filesystem shutdown
1216 : * is in progress. The caller takes the responsibility to cancel
1217 : * the duplicate transaction that gets returned.
1218 : */
1219 1330620170 : error = __xfs_trans_commit(trans, true);
1220 1331033774 : if (error)
1221 : return error;
1222 :
1223 : /*
1224 : * Reserve space in the log for the next transaction.
1225 : * This also pushes items in the "AIL", the list of logged items,
1226 : * out to disk if they are taking up space at the tail of the log
1227 : * that we want to use. This requires that either nothing be locked
1228 : * across this call, or that anything that is locked be logged in
1229 : * the prior and the next transactions.
1230 : */
1231 1331022781 : tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1232 1331022781 : return xfs_trans_reserve(*tpp, &tres, 0, 0);
1233 : }
1234 :
1235 : /*
1236 : * Allocate an transaction, lock and join the inode to it, and reserve quota.
1237 : *
1238 : * The caller must ensure that the on-disk dquots attached to this inode have
1239 : * already been allocated and initialized. The caller is responsible for
1240 : * releasing ILOCK_EXCL if a new transaction is returned.
1241 : */
1242 : int
1243 623290808 : xfs_trans_alloc_inode(
1244 : struct xfs_inode *ip,
1245 : struct xfs_trans_res *resv,
1246 : unsigned int dblocks,
1247 : unsigned int rblocks,
1248 : bool force,
1249 : struct xfs_trans **tpp)
1250 : {
1251 623290808 : struct xfs_trans *tp;
1252 623290808 : struct xfs_mount *mp = ip->i_mount;
1253 623290808 : bool retried = false;
1254 623291749 : int error;
1255 :
1256 623291749 : retry:
1257 623291749 : error = xfs_trans_alloc(mp, resv, dblocks,
1258 623291749 : rblocks / mp->m_sb.sb_rextsize,
1259 : force ? XFS_TRANS_RESERVE : 0, &tp);
1260 623228645 : if (error)
1261 5828490 : return error;
1262 :
1263 617400155 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1264 617415741 : xfs_trans_ijoin(tp, ip, 0);
1265 :
1266 617548921 : error = xfs_qm_dqattach_locked(ip, false);
1267 617551751 : if (error) {
1268 : /* Caller should have allocated the dquots! */
1269 4 : ASSERT(error != -ENOENT);
1270 4 : goto out_cancel;
1271 : }
1272 :
1273 617551747 : error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force);
1274 617627921 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1275 941 : xfs_trans_cancel(tp);
1276 941 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1277 941 : xfs_blockgc_free_quota(ip, 0);
1278 941 : retried = true;
1279 941 : goto retry;
1280 : }
1281 617626980 : if (error)
1282 840 : goto out_cancel;
1283 :
1284 617626140 : *tpp = tp;
1285 617626140 : return 0;
1286 :
1287 844 : out_cancel:
1288 844 : xfs_trans_cancel(tp);
1289 844 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1290 844 : return error;
1291 : }
1292 :
1293 :
1294 : /* Try to reserve more blocks and file quota for a transaction. */
1295 : int
1296 23769 : xfs_trans_reserve_more_inode(
1297 : struct xfs_trans *tp,
1298 : struct xfs_inode *ip,
1299 : unsigned int dblocks,
1300 : unsigned int rblocks,
1301 : bool force_quota)
1302 : {
1303 23769 : struct xfs_mount *mp = ip->i_mount;
1304 23769 : unsigned int rtx = rblocks / mp->m_sb.sb_rextsize;
1305 23769 : int error;
1306 :
1307 23769 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1308 :
1309 23769 : error = xfs_trans_reserve_more(tp, dblocks, rtx);
1310 23769 : if (error)
1311 : return error;
1312 :
1313 46774 : if (!XFS_IS_QUOTA_ON(mp) || xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
1314 : return 0;
1315 :
1316 22560 : if (tp->t_flags & XFS_TRANS_RESERVE)
1317 0 : force_quota = true;
1318 :
1319 22560 : error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks,
1320 : force_quota);
1321 22560 : if (!error)
1322 : return 0;
1323 :
1324 : /* Quota failed, give back the new reservation. */
1325 0 : xfs_mod_fdblocks(mp, dblocks, tp->t_flags & XFS_TRANS_RESERVE);
1326 0 : tp->t_blk_res -= dblocks;
1327 0 : xfs_mod_frextents(mp, rtx);
1328 0 : tp->t_rtx_res -= rtx;
1329 0 : return error;
1330 : }
1331 :
1332 : /*
1333 : * Allocate an transaction in preparation for inode creation by reserving quota
1334 : * against the given dquots. Callers are not required to hold any inode locks.
1335 : */
1336 : int
1337 135582521 : xfs_trans_alloc_icreate(
1338 : struct xfs_mount *mp,
1339 : struct xfs_trans_res *resv,
1340 : struct xfs_dquot *udqp,
1341 : struct xfs_dquot *gdqp,
1342 : struct xfs_dquot *pdqp,
1343 : unsigned int dblocks,
1344 : struct xfs_trans **tpp)
1345 : {
1346 135582521 : struct xfs_trans *tp;
1347 135582521 : bool retried = false;
1348 135589759 : int error;
1349 :
1350 135589759 : retry:
1351 135589759 : error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
1352 134921589 : if (error)
1353 1083194 : return error;
1354 :
1355 133838395 : error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
1356 134467420 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1357 7238 : xfs_trans_cancel(tp);
1358 7238 : xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1359 7238 : retried = true;
1360 7238 : goto retry;
1361 : }
1362 134460182 : if (error) {
1363 6283 : xfs_trans_cancel(tp);
1364 6283 : return error;
1365 : }
1366 :
1367 134453899 : *tpp = tp;
1368 134453899 : return 0;
1369 : }
1370 :
1371 : /*
1372 : * Allocate an transaction, lock and join the inode to it, and reserve quota
1373 : * in preparation for inode attribute changes that include uid, gid, or prid
1374 : * changes.
1375 : *
1376 : * The caller must ensure that the on-disk dquots attached to this inode have
1377 : * already been allocated and initialized. The ILOCK will be dropped when the
1378 : * transaction is committed or cancelled.
1379 : */
1380 : int
1381 42171063 : xfs_trans_alloc_ichange(
1382 : struct xfs_inode *ip,
1383 : struct xfs_dquot *new_udqp,
1384 : struct xfs_dquot *new_gdqp,
1385 : struct xfs_dquot *new_pdqp,
1386 : bool force,
1387 : struct xfs_trans **tpp)
1388 : {
1389 42171063 : struct xfs_trans *tp;
1390 42171063 : struct xfs_mount *mp = ip->i_mount;
1391 42171063 : struct xfs_dquot *udqp;
1392 42171063 : struct xfs_dquot *gdqp;
1393 42171063 : struct xfs_dquot *pdqp;
1394 42171063 : bool retried = false;
1395 42171069 : int error;
1396 :
1397 42171069 : retry:
1398 42171069 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1399 42188819 : if (error)
1400 1 : return error;
1401 :
1402 42188818 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1403 42198629 : xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1404 :
1405 42208538 : error = xfs_qm_dqattach_locked(ip, false);
1406 42128323 : if (error) {
1407 : /* Caller should have allocated the dquots! */
1408 313 : ASSERT(error != -ENOENT);
1409 313 : goto out_cancel;
1410 : }
1411 :
1412 : /*
1413 : * For each quota type, skip quota reservations if the inode's dquots
1414 : * now match the ones that came from the caller, or the caller didn't
1415 : * pass one in. The inode's dquots can change if we drop the ILOCK to
1416 : * perform a blockgc scan, so we must preserve the caller's arguments.
1417 : */
1418 42128010 : udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL;
1419 42128010 : gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL;
1420 42128010 : pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL;
1421 42128010 : if (udqp || gdqp || pdqp) {
1422 8202954 : unsigned int qflags = XFS_QMOPT_RES_REGBLKS;
1423 :
1424 8202954 : if (force)
1425 7872949 : qflags |= XFS_QMOPT_FORCE_RES;
1426 :
1427 : /*
1428 : * Reserve enough quota to handle blocks on disk and reserved
1429 : * for a delayed allocation. We'll actually transfer the
1430 : * delalloc reservation between dquots at chown time, even
1431 : * though that part is only semi-transactional.
1432 : */
1433 8202954 : error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
1434 8202954 : pdqp, ip->i_nblocks + ip->i_delayed_blks,
1435 : 1, qflags);
1436 8276386 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1437 6 : xfs_trans_cancel(tp);
1438 6 : xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1439 6 : retried = true;
1440 6 : goto retry;
1441 : }
1442 8276380 : if (error)
1443 6 : goto out_cancel;
1444 : }
1445 :
1446 42201430 : *tpp = tp;
1447 42201430 : return 0;
1448 :
1449 319 : out_cancel:
1450 319 : xfs_trans_cancel(tp);
1451 319 : return error;
1452 : }
1453 :
1454 : /*
1455 : * Allocate an transaction, lock and join the directory and child inodes to it,
1456 : * and reserve quota for a directory update. If there isn't sufficient space,
1457 : * @dblocks will be set to zero for a reservationless directory update and
1458 : * @nospace_error will be set to a negative errno describing the space
1459 : * constraint we hit.
1460 : *
1461 : * The caller must ensure that the on-disk dquots attached to this inode have
1462 : * already been allocated and initialized. The ILOCKs will be dropped when the
1463 : * transaction is committed or cancelled.
1464 : *
1465 : * Caller is responsible for unlocking the inodes manually upon return
1466 : */
1467 : int
1468 79878815 : xfs_trans_alloc_dir(
1469 : struct xfs_inode *dp,
1470 : struct xfs_trans_res *resv,
1471 : struct xfs_inode *ip,
1472 : unsigned int *dblocks,
1473 : struct xfs_trans **tpp,
1474 : int *nospace_error)
1475 : {
1476 79878815 : struct xfs_trans *tp;
1477 79878815 : struct xfs_mount *mp = ip->i_mount;
1478 79878815 : unsigned int resblks;
1479 79878815 : bool retried = false;
1480 79888390 : int error;
1481 :
1482 79888390 : retry:
1483 79888390 : *nospace_error = 0;
1484 79888390 : resblks = *dblocks;
1485 79888390 : error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1486 79877175 : if (error == -ENOSPC) {
1487 145915 : *nospace_error = error;
1488 145915 : resblks = 0;
1489 145915 : error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1490 : }
1491 79877169 : if (error)
1492 0 : return error;
1493 :
1494 79877169 : xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
1495 :
1496 79884243 : xfs_trans_ijoin(tp, dp, 0);
1497 79901751 : xfs_trans_ijoin(tp, ip, 0);
1498 :
1499 79947040 : error = xfs_qm_dqattach_locked(dp, false);
1500 79939780 : if (error) {
1501 : /* Caller should have allocated the dquots! */
1502 0 : ASSERT(error != -ENOENT);
1503 0 : goto out_cancel;
1504 : }
1505 :
1506 79939780 : error = xfs_qm_dqattach_locked(ip, false);
1507 79938577 : if (error) {
1508 : /* Caller should have allocated the dquots! */
1509 0 : ASSERT(error != -ENOENT);
1510 0 : goto out_cancel;
1511 : }
1512 :
1513 79938577 : if (resblks == 0)
1514 145912 : goto done;
1515 :
1516 79792665 : error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false);
1517 79792367 : if (error == -EDQUOT || error == -ENOSPC) {
1518 18118 : if (!retried) {
1519 9575 : xfs_trans_cancel(tp);
1520 9575 : xfs_iunlock(dp, XFS_ILOCK_EXCL);
1521 9575 : if (dp != ip)
1522 9575 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1523 9575 : xfs_blockgc_free_quota(dp, 0);
1524 9575 : retried = true;
1525 9575 : goto retry;
1526 : }
1527 :
1528 8543 : *nospace_error = error;
1529 8543 : resblks = 0;
1530 8543 : error = 0;
1531 : }
1532 79782792 : if (error)
1533 0 : goto out_cancel;
1534 :
1535 79774249 : done:
1536 79928704 : *tpp = tp;
1537 79928704 : *dblocks = resblks;
1538 79928704 : return 0;
1539 :
1540 0 : out_cancel:
1541 0 : xfs_trans_cancel(tp);
1542 0 : return error;
1543 : }
|