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 : #include "xfs_rtbitmap.h"
28 : #include "xfs_rtgroup.h"
29 :
30 : struct kmem_cache *xfs_trans_cache;
31 :
32 : #if defined(CONFIG_TRACEPOINTS)
33 : static void
34 66856 : xfs_trans_trace_reservations(
35 : struct xfs_mount *mp)
36 : {
37 66856 : struct xfs_trans_res *res;
38 66856 : struct xfs_trans_res *end_res;
39 66856 : int i;
40 :
41 66856 : res = (struct xfs_trans_res *)M_RES(mp);
42 66856 : end_res = (struct xfs_trans_res *)(M_RES(mp) + 1);
43 2005680 : for (i = 0; res < end_res; i++, res++)
44 1938824 : trace_xfs_trans_resv_calc(mp, i, res);
45 66856 : }
46 : #else
47 : # define xfs_trans_trace_reservations(mp)
48 : #endif
49 :
50 : /*
51 : * Initialize the precomputed transaction reservation values
52 : * in the mount structure.
53 : */
54 : void
55 66856 : xfs_trans_init(
56 : struct xfs_mount *mp)
57 : {
58 66856 : xfs_trans_resv_calc(mp, M_RES(mp));
59 66856 : xfs_trans_trace_reservations(mp);
60 66856 : }
61 :
62 : /*
63 : * Free the transaction structure. If there is more clean up
64 : * to do when the structure is freed, add it here.
65 : */
66 : STATIC void
67 5458031721 : xfs_trans_free(
68 : struct xfs_trans *tp)
69 : {
70 5458031721 : xfs_extent_busy_sort(&tp->t_busy);
71 5457127056 : xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false);
72 :
73 5458097017 : if (tp->t_dfops_finished > 0)
74 946822730 : trace_xfs_defer_stats(tp);
75 :
76 5458084821 : trace_xfs_trans_free(tp, _RET_IP_);
77 5457635883 : xfs_trans_clear_context(tp);
78 5457226203 : if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
79 2003983941 : sb_end_intwrite(tp->t_mountp->m_super);
80 5457348881 : xfs_trans_free_dqinfo(tp);
81 5457504060 : kmem_cache_free(xfs_trans_cache, tp);
82 5459220829 : }
83 :
84 : /*
85 : * This is called to create a new transaction which will share the
86 : * permanent log reservation of the given transaction. The remaining
87 : * unused block and rt extent reservations are also inherited. This
88 : * implies that the original transaction is no longer allowed to allocate
89 : * blocks. Locks and log items, however, are no inherited. They must
90 : * be added to the new transaction explicitly.
91 : */
92 : STATIC struct xfs_trans *
93 1961886880 : xfs_trans_dup(
94 : struct xfs_trans *tp)
95 : {
96 1961886880 : struct xfs_trans *ntp;
97 :
98 1961886880 : trace_xfs_trans_dup(tp, _RET_IP_);
99 :
100 1961817733 : ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
101 :
102 : /*
103 : * Initialize the new transaction structure.
104 : */
105 1962460817 : ntp->t_magic = XFS_TRANS_HEADER_MAGIC;
106 1962460817 : ntp->t_mountp = tp->t_mountp;
107 1962460817 : INIT_LIST_HEAD(&ntp->t_items);
108 1962460817 : INIT_LIST_HEAD(&ntp->t_busy);
109 1962460817 : INIT_LIST_HEAD(&ntp->t_dfops);
110 1962460817 : ntp->t_highest_agno = NULLAGNUMBER;
111 :
112 1962460817 : ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
113 1962460817 : ASSERT(tp->t_ticket != NULL);
114 :
115 1962460817 : ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
116 1962460817 : (tp->t_flags & XFS_TRANS_RESERVE) |
117 1962460817 : (tp->t_flags & XFS_TRANS_NO_WRITECOUNT) |
118 : (tp->t_flags & XFS_TRANS_RES_FDBLKS);
119 : /* We gave our writer reference to the new transaction */
120 1962460817 : tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
121 1962460817 : ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
122 :
123 1962645907 : ASSERT(tp->t_blk_res >= tp->t_blk_res_used);
124 1962645907 : ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
125 1962645907 : tp->t_blk_res = tp->t_blk_res_used;
126 :
127 1962645907 : ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
128 1962645907 : tp->t_rtx_res = tp->t_rtx_res_used;
129 :
130 1962645907 : xfs_trans_switch_context(tp, ntp);
131 :
132 : /* move deferred ops over to the new tp */
133 1962384093 : xfs_defer_move(ntp, tp);
134 :
135 1962278231 : xfs_trans_dup_dqinfo(tp, ntp);
136 1962043719 : return ntp;
137 : }
138 :
139 : /*
140 : * Try to reserve more blocks for a transaction.
141 : *
142 : * This is for callers that need to attach resources to a transaction, scan
143 : * those resources to determine the space reservation requirements, and then
144 : * modify the attached resources. In other words, online repair. This can
145 : * fail due to ENOSPC, so the caller must be able to cancel the transaction
146 : * without shutting down the fs.
147 : */
148 : int
149 100070838 : xfs_trans_reserve_more(
150 : struct xfs_trans *tp,
151 : unsigned int blocks,
152 : unsigned int rtextents)
153 : {
154 100070838 : struct xfs_mount *mp = tp->t_mountp;
155 100070838 : bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
156 100070838 : int error = 0;
157 :
158 100070838 : ASSERT(!(tp->t_flags & XFS_TRANS_DIRTY));
159 :
160 : /*
161 : * Attempt to reserve the needed disk blocks by decrementing
162 : * the number needed from the number available. This will
163 : * fail if the count would go below zero.
164 : */
165 100070838 : if (blocks > 0) {
166 100041036 : error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
167 100041036 : if (error)
168 : return -ENOSPC;
169 100040472 : tp->t_blk_res += blocks;
170 : }
171 :
172 : /*
173 : * Attempt to reserve the needed realtime extents by decrementing
174 : * the number needed from the number available. This will
175 : * fail if the count would go below zero.
176 : */
177 100070274 : if (rtextents > 0) {
178 5988 : error = xfs_mod_frextents(mp, -((int64_t)rtextents));
179 5988 : if (error) {
180 0 : error = -ENOSPC;
181 0 : goto out_blocks;
182 : }
183 5988 : tp->t_rtx_res += rtextents;
184 : }
185 :
186 : return 0;
187 : out_blocks:
188 0 : if (blocks > 0) {
189 0 : xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
190 0 : tp->t_blk_res -= blocks;
191 : }
192 : return error;
193 : }
194 :
195 : /*
196 : * This is called to reserve free disk blocks and log space for the
197 : * given transaction. This must be done before allocating any resources
198 : * within the transaction.
199 : *
200 : * This will return ENOSPC if there are not enough blocks available.
201 : * It will sleep waiting for available log space.
202 : * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
203 : * is used by long running transactions. If any one of the reservations
204 : * fails then they will all be backed out.
205 : *
206 : * This does not do quota reservations. That typically is done by the
207 : * caller afterwards.
208 : */
209 : static int
210 5459904063 : xfs_trans_reserve(
211 : struct xfs_trans *tp,
212 : struct xfs_trans_res *resp,
213 : uint blocks,
214 : uint rtextents)
215 : {
216 5459904063 : struct xfs_mount *mp = tp->t_mountp;
217 5459904063 : int error = 0;
218 5459904063 : bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
219 :
220 : /*
221 : * Attempt to reserve the needed disk blocks by decrementing
222 : * the number needed from the number available. This will
223 : * fail if the count would go below zero.
224 : */
225 5459904063 : if (blocks > 0) {
226 1374288970 : error = xfs_mod_fdblocks(mp, -((int64_t)blocks), rsvd);
227 1373513341 : if (error != 0)
228 : return -ENOSPC;
229 1355211655 : tp->t_blk_res += blocks;
230 : }
231 :
232 : /*
233 : * Reserve the log space needed for this transaction.
234 : */
235 5440826748 : if (resp->tr_logres > 0) {
236 3947329471 : bool permanent = false;
237 :
238 3947329471 : ASSERT(tp->t_log_res == 0 ||
239 : tp->t_log_res == resp->tr_logres);
240 3947329471 : ASSERT(tp->t_log_count == 0 ||
241 : tp->t_log_count == resp->tr_logcount);
242 :
243 3947329471 : if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) {
244 3725216986 : tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
245 3725216986 : permanent = true;
246 : } else {
247 222112485 : ASSERT(tp->t_ticket == NULL);
248 222112485 : ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
249 : }
250 :
251 3947329471 : if (tp->t_ticket != NULL) {
252 1963247380 : ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES);
253 1963247380 : error = xfs_log_regrant(mp, tp->t_ticket);
254 : } else {
255 1984082091 : error = xfs_log_reserve(mp, resp->tr_logres,
256 : resp->tr_logcount,
257 : &tp->t_ticket, permanent);
258 : }
259 :
260 3948357369 : if (error)
261 2482 : goto undo_blocks;
262 :
263 3948354887 : tp->t_log_res = resp->tr_logres;
264 3948354887 : tp->t_log_count = resp->tr_logcount;
265 : }
266 :
267 : /*
268 : * Attempt to reserve the needed realtime extents by decrementing
269 : * the number needed from the number available. This will
270 : * fail if the count would go below zero.
271 : */
272 5441852164 : if (rtextents > 0) {
273 300739455 : error = xfs_mod_frextents(mp, -((int64_t)rtextents));
274 300718555 : if (error) {
275 6862154 : error = -ENOSPC;
276 6862154 : goto undo_log;
277 : }
278 293856401 : tp->t_rtx_res += rtextents;
279 : }
280 :
281 : return 0;
282 :
283 : /*
284 : * Error cases jump to one of these labels to undo any
285 : * reservations which have already been performed.
286 : */
287 : undo_log:
288 6862154 : if (resp->tr_logres > 0) {
289 6862154 : xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket);
290 6861176 : tp->t_ticket = NULL;
291 6861176 : tp->t_log_res = 0;
292 6861176 : tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
293 : }
294 :
295 0 : undo_blocks:
296 6863658 : if (blocks > 0) {
297 6861300 : xfs_mod_fdblocks(mp, (int64_t)blocks, rsvd);
298 6861020 : tp->t_blk_res = 0;
299 : }
300 : return error;
301 : }
302 :
303 : int
304 3480628441 : xfs_trans_alloc(
305 : struct xfs_mount *mp,
306 : struct xfs_trans_res *resp,
307 : uint blocks,
308 : uint rtextents,
309 : uint flags,
310 : struct xfs_trans **tpp)
311 : {
312 3480628441 : struct xfs_trans *tp;
313 3480628441 : bool want_retry = true;
314 3493303148 : int error;
315 :
316 : /*
317 : * Allocate the handle before we do our freeze accounting and setting up
318 : * GFP_NOFS allocation context so that we avoid lockdep false positives
319 : * by doing GFP_KERNEL allocations inside sb_start_intwrite().
320 : */
321 3493303148 : retry:
322 3493303148 : tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL);
323 3495575175 : if (!(flags & XFS_TRANS_NO_WRITECOUNT))
324 2003663800 : sb_start_intwrite(mp->m_super);
325 3493532330 : xfs_trans_set_context(tp);
326 :
327 : /*
328 : * Zero-reservation ("empty") transactions can't modify anything, so
329 : * they're allowed to run while we're frozen.
330 : */
331 6985359524 : WARN_ON(resp->tr_logres > 0 &&
332 : mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
333 3492679762 : ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) ||
334 : xfs_has_lazysbcount(mp));
335 :
336 3492679762 : tp->t_magic = XFS_TRANS_HEADER_MAGIC;
337 3492679762 : tp->t_flags = flags;
338 3492679762 : tp->t_mountp = mp;
339 3492679762 : INIT_LIST_HEAD(&tp->t_items);
340 3492679762 : INIT_LIST_HEAD(&tp->t_busy);
341 3492679762 : INIT_LIST_HEAD(&tp->t_dfops);
342 3492679762 : tp->t_highest_agno = NULLAGNUMBER;
343 :
344 3492679762 : error = xfs_trans_reserve(tp, resp, blocks, rtextents);
345 3493140399 : if (error == -ENOSPC && want_retry) {
346 12668066 : xfs_trans_cancel(tp);
347 :
348 : /*
349 : * We weren't able to reserve enough space for the transaction.
350 : * Flush the other speculative space allocations to free space.
351 : * Do not perform a synchronous scan because callers can hold
352 : * other locks.
353 : */
354 12651013 : error = xfs_blockgc_flush_all(mp);
355 12674707 : if (error)
356 0 : return error;
357 12674707 : want_retry = false;
358 12674707 : goto retry;
359 : }
360 3480472333 : if (error) {
361 12491546 : xfs_trans_cancel(tp);
362 12491546 : return error;
363 : }
364 :
365 3467980787 : trace_xfs_trans_alloc(tp, _RET_IP_);
366 :
367 3467672310 : *tpp = tp;
368 3467672310 : return 0;
369 : }
370 :
371 : /*
372 : * Create an empty transaction with no reservation. This is a defensive
373 : * mechanism for routines that query metadata without actually modifying them --
374 : * if the metadata being queried is somehow cross-linked (think a btree block
375 : * pointer that points higher in the tree), we risk deadlock. However, blocks
376 : * grabbed as part of a transaction can be re-grabbed. The verifiers will
377 : * notice the corrupt block and the operation will fail back to userspace
378 : * without deadlocking.
379 : *
380 : * Note the zero-length reservation; this transaction MUST be cancelled without
381 : * any dirty data.
382 : *
383 : * Callers should obtain freeze protection to avoid a conflict with fs freezing
384 : * where we can be grabbing buffers at the same time that freeze is trying to
385 : * drain the buffer LRU list.
386 : */
387 : int
388 1491681145 : xfs_trans_alloc_empty(
389 : struct xfs_mount *mp,
390 : struct xfs_trans **tpp)
391 : {
392 1491681145 : struct xfs_trans_res resv = {0};
393 :
394 1491681145 : return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
395 : }
396 :
397 : /*
398 : * Record the indicated change to the given field for application
399 : * to the file system's superblock when the transaction commits.
400 : * For now, just store the change in the transaction structure.
401 : *
402 : * Mark the transaction structure to indicate that the superblock
403 : * needs to be updated before committing.
404 : *
405 : * Because we may not be keeping track of allocated/free inodes and
406 : * used filesystem blocks in the superblock, we do not mark the
407 : * superblock dirty in this transaction if we modify these fields.
408 : * We still need to update the transaction deltas so that they get
409 : * applied to the incore superblock, but we don't want them to
410 : * cause the superblock to get locked and logged if these are the
411 : * only fields in the superblock that the transaction modifies.
412 : */
413 : void
414 544813334 : xfs_trans_mod_sb(
415 : xfs_trans_t *tp,
416 : uint field,
417 : int64_t delta)
418 : {
419 544813334 : uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
420 544813334 : xfs_mount_t *mp = tp->t_mountp;
421 :
422 544813334 : switch (field) {
423 1825781 : case XFS_TRANS_SB_ICOUNT:
424 1825781 : tp->t_icount_delta += delta;
425 1825781 : if (xfs_has_lazysbcount(mp))
426 1825520 : flags &= ~XFS_TRANS_SB_DIRTY;
427 : break;
428 200370654 : case XFS_TRANS_SB_IFREE:
429 200370654 : tp->t_ifree_delta += delta;
430 200370654 : if (xfs_has_lazysbcount(mp))
431 200218385 : flags &= ~XFS_TRANS_SB_DIRTY;
432 : break;
433 171959915 : case XFS_TRANS_SB_FDBLOCKS:
434 : /*
435 : * Track the number of blocks allocated in the transaction.
436 : * Make sure it does not exceed the number reserved. If so,
437 : * shutdown as this can lead to accounting inconsistency.
438 : */
439 171959915 : if (delta < 0) {
440 66955844 : tp->t_blk_res_used += (uint)-delta;
441 66955844 : if (tp->t_blk_res_used > tp->t_blk_res)
442 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
443 105004071 : } else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) {
444 340765 : int64_t blkres_delta;
445 :
446 : /*
447 : * Return freed blocks directly to the reservation
448 : * instead of the global pool, being careful not to
449 : * overflow the trans counter. This is used to preserve
450 : * reservation across chains of transaction rolls that
451 : * repeatedly free and allocate blocks.
452 : */
453 340765 : blkres_delta = min_t(int64_t, delta,
454 : UINT_MAX - tp->t_blk_res);
455 340765 : tp->t_blk_res += blkres_delta;
456 340765 : delta -= blkres_delta;
457 : }
458 171959915 : tp->t_fdblocks_delta += delta;
459 171959915 : if (xfs_has_lazysbcount(mp))
460 171948662 : flags &= ~XFS_TRANS_SB_DIRTY;
461 : break;
462 30997523 : case XFS_TRANS_SB_RES_FDBLOCKS:
463 : /*
464 : * The allocation has already been applied to the
465 : * in-core superblock's counter. This should only
466 : * be applied to the on-disk superblock.
467 : */
468 30997523 : tp->t_res_fdblocks_delta += delta;
469 30997523 : if (xfs_has_lazysbcount(mp))
470 30997352 : flags &= ~XFS_TRANS_SB_DIRTY;
471 : break;
472 139653010 : case XFS_TRANS_SB_FREXTENTS:
473 : /*
474 : * Track the number of blocks allocated in the
475 : * transaction. Make sure it does not exceed the
476 : * number reserved.
477 : */
478 139653010 : if (delta < 0) {
479 86773135 : tp->t_rtx_res_used += (uint)-delta;
480 86773135 : ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
481 : }
482 139653010 : tp->t_frextents_delta += delta;
483 139653010 : if (xfs_has_rtgroups(mp))
484 118583779 : flags &= ~XFS_TRANS_SB_DIRTY;
485 : break;
486 0 : case XFS_TRANS_SB_RES_FREXTENTS:
487 : /*
488 : * The allocation has already been applied to the
489 : * in-core superblock's counter. This should only
490 : * be applied to the on-disk superblock.
491 : */
492 0 : ASSERT(delta < 0);
493 0 : tp->t_res_frextents_delta += delta;
494 0 : break;
495 666 : case XFS_TRANS_SB_DBLOCKS:
496 666 : tp->t_dblocks_delta += delta;
497 666 : break;
498 417 : case XFS_TRANS_SB_AGCOUNT:
499 417 : ASSERT(delta > 0);
500 417 : tp->t_agcount_delta += delta;
501 417 : break;
502 0 : case XFS_TRANS_SB_IMAXPCT:
503 0 : tp->t_imaxpct_delta += delta;
504 0 : break;
505 2 : case XFS_TRANS_SB_REXTSIZE:
506 2 : tp->t_rextsize_delta += delta;
507 2 : break;
508 1479 : case XFS_TRANS_SB_RBMBLOCKS:
509 1479 : tp->t_rbmblocks_delta += delta;
510 1479 : break;
511 1518 : case XFS_TRANS_SB_RBLOCKS:
512 1518 : tp->t_rblocks_delta += delta;
513 1518 : break;
514 1518 : case XFS_TRANS_SB_REXTENTS:
515 1518 : tp->t_rextents_delta += delta;
516 1518 : break;
517 100 : case XFS_TRANS_SB_REXTSLOG:
518 100 : tp->t_rextslog_delta += delta;
519 100 : break;
520 751 : case XFS_TRANS_SB_RGCOUNT:
521 751 : ASSERT(delta > 0);
522 751 : tp->t_rgcount_delta += delta;
523 751 : break;
524 0 : default:
525 0 : ASSERT(0);
526 0 : return;
527 : }
528 :
529 544813334 : tp->t_flags |= flags;
530 : }
531 :
532 : /*
533 : * xfs_trans_apply_sb_deltas() is called from the commit code
534 : * to bring the superblock buffer into the current transaction
535 : * and modify it as requested by earlier calls to xfs_trans_mod_sb().
536 : *
537 : * For now we just look at each field allowed to change and change
538 : * it if necessary.
539 : */
540 : STATIC void
541 19590613 : xfs_trans_apply_sb_deltas(
542 : xfs_trans_t *tp)
543 : {
544 19590613 : struct xfs_dsb *sbp;
545 19590613 : struct xfs_buf *bp;
546 19590613 : bool update_rtsb = false;
547 19590613 : int whole = 0;
548 :
549 19590613 : bp = xfs_trans_getsb(tp);
550 19590613 : sbp = bp->b_addr;
551 :
552 : /*
553 : * Only update the superblock counters if we are logging them
554 : */
555 19590613 : if (!xfs_has_lazysbcount((tp->t_mountp))) {
556 1151 : if (tp->t_icount_delta)
557 11 : be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
558 1151 : if (tp->t_ifree_delta)
559 1131 : be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
560 1151 : if (tp->t_fdblocks_delta)
561 42 : be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
562 1151 : if (tp->t_res_fdblocks_delta)
563 0 : be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
564 : }
565 :
566 : /*
567 : * Updating frextents requires careful handling because it does not
568 : * behave like the lazysb counters because we cannot rely on log
569 : * recovery in older kenels to recompute the value from the rtbitmap.
570 : * This means that the ondisk frextents must be consistent with the
571 : * rtbitmap.
572 : *
573 : * Therefore, log the frextents change to the ondisk superblock and
574 : * update the incore superblock so that future calls to xfs_log_sb
575 : * write the correct value ondisk.
576 : *
577 : * Don't touch m_frextents because it includes incore reservations,
578 : * and those are handled by the unreserve function.
579 : *
580 : * sb_frextents was added to the lazy sb counters when the rt groups
581 : * feature was introduced. This is possible because we know that all
582 : * kernels supporting rtgroups will also recompute frextents from the
583 : * realtime bitmap.
584 : */
585 19590613 : if ((tp->t_frextents_delta || tp->t_res_frextents_delta) &&
586 19588796 : !xfs_has_rtgroups(tp->t_mountp)) {
587 19587286 : struct xfs_mount *mp = tp->t_mountp;
588 19587286 : int64_t rtxdelta;
589 :
590 19587286 : rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta;
591 :
592 19587286 : spin_lock(&mp->m_sb_lock);
593 19587286 : be64_add_cpu(&sbp->sb_frextents, rtxdelta);
594 19587286 : mp->m_sb.sb_frextents += rtxdelta;
595 19587286 : spin_unlock(&mp->m_sb_lock);
596 : }
597 :
598 19590613 : if (tp->t_dblocks_delta) {
599 666 : be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
600 666 : whole = 1;
601 : }
602 19590613 : if (tp->t_agcount_delta) {
603 417 : be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
604 417 : whole = 1;
605 : }
606 19590613 : if (tp->t_imaxpct_delta) {
607 0 : sbp->sb_imax_pct += tp->t_imaxpct_delta;
608 0 : whole = 1;
609 : }
610 19590613 : if (tp->t_rextsize_delta) {
611 2 : be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
612 2 : whole = 1;
613 2 : update_rtsb = true;
614 : }
615 19590613 : if (tp->t_rbmblocks_delta) {
616 1479 : be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
617 1479 : whole = 1;
618 1479 : update_rtsb = true;
619 : }
620 19590613 : if (tp->t_rblocks_delta) {
621 1518 : be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
622 1518 : whole = 1;
623 1518 : update_rtsb = true;
624 : }
625 19590613 : if (tp->t_rextents_delta) {
626 1518 : be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
627 1518 : whole = 1;
628 1518 : update_rtsb = true;
629 : }
630 19590613 : if (tp->t_rextslog_delta) {
631 100 : sbp->sb_rextslog += tp->t_rextslog_delta;
632 100 : whole = 1;
633 100 : update_rtsb = true;
634 : }
635 19590613 : if (tp->t_rgcount_delta) {
636 751 : be32_add_cpu(&sbp->sb_rgcount, tp->t_rgcount_delta);
637 751 : whole = 1;
638 751 : update_rtsb = true;
639 : }
640 :
641 19590613 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
642 19590613 : if (whole)
643 : /*
644 : * Log the whole thing, the fields are noncontiguous.
645 : */
646 2184 : xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
647 : else
648 : /*
649 : * Since all the modifiable fields are contiguous, we
650 : * can get away with this.
651 : */
652 19588429 : xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount),
653 : offsetof(struct xfs_dsb, sb_frextents) +
654 : sizeof(sbp->sb_frextents) - 1);
655 :
656 19590613 : if (update_rtsb)
657 1518 : xfs_rtgroup_log_super(tp, bp);
658 19590613 : }
659 :
660 : /*
661 : * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and
662 : * apply superblock counter changes to the in-core superblock. The
663 : * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
664 : * applied to the in-core superblock. The idea is that that has already been
665 : * done.
666 : *
667 : * If we are not logging superblock counters, then the inode allocated/free and
668 : * used block counts are not updated in the on disk superblock. In this case,
669 : * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
670 : * still need to update the incore superblock with the changes.
671 : *
672 : * Deltas for the inode count are +/-64, hence we use a large batch size of 128
673 : * so we don't need to take the counter lock on every update.
674 : */
675 : #define XFS_ICOUNT_BATCH 128
676 :
677 : void
678 5459634135 : xfs_trans_unreserve_and_mod_sb(
679 : struct xfs_trans *tp)
680 : {
681 5459634135 : struct xfs_mount *mp = tp->t_mountp;
682 5459634135 : bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
683 5459634135 : int64_t blkdelta = 0;
684 5459634135 : int64_t rtxdelta = 0;
685 5459634135 : int64_t idelta = 0;
686 5459634135 : int64_t ifreedelta = 0;
687 5459634135 : int error;
688 :
689 : /* calculate deltas */
690 5459634135 : if (tp->t_blk_res > 0)
691 : blkdelta = tp->t_blk_res;
692 5459634135 : if ((tp->t_fdblocks_delta != 0) &&
693 42 : (xfs_has_lazysbcount(mp) ||
694 42 : (tp->t_flags & XFS_TRANS_SB_DIRTY)))
695 165044574 : blkdelta += tp->t_fdblocks_delta;
696 :
697 5459634135 : if (tp->t_rtx_res > 0)
698 : rtxdelta = tp->t_rtx_res;
699 5459634135 : if ((tp->t_frextents_delta != 0) &&
700 19587286 : (xfs_has_rtgroups(mp) ||
701 19587286 : (tp->t_flags & XFS_TRANS_SB_DIRTY)))
702 135970078 : rtxdelta += tp->t_frextents_delta;
703 :
704 5459634135 : if (xfs_has_lazysbcount(mp) ||
705 2463 : (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
706 5459632823 : idelta = tp->t_icount_delta;
707 5459632823 : ifreedelta = tp->t_ifree_delta;
708 : }
709 :
710 : /* apply the per-cpu counters */
711 5459634135 : if (blkdelta) {
712 1512474113 : error = xfs_mod_fdblocks(mp, blkdelta, rsvd);
713 1512448911 : ASSERT(!error);
714 : }
715 :
716 5459608933 : if (idelta)
717 1825715 : percpu_counter_add_batch(&mp->m_icount, idelta,
718 : XFS_ICOUNT_BATCH);
719 :
720 5459609106 : if (ifreedelta)
721 201363942 : percpu_counter_add(&mp->m_ifree, ifreedelta);
722 :
723 5459603456 : if (rtxdelta) {
724 263829777 : error = xfs_mod_frextents(mp, rtxdelta);
725 263832601 : ASSERT(!error);
726 : }
727 :
728 5459606280 : if (!(tp->t_flags & XFS_TRANS_SB_DIRTY))
729 : return;
730 :
731 : /* apply remaining deltas */
732 19590613 : spin_lock(&mp->m_sb_lock);
733 19590613 : mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta;
734 19590613 : mp->m_sb.sb_icount += idelta;
735 19590613 : mp->m_sb.sb_ifree += ifreedelta;
736 : /*
737 : * Do not touch sb_frextents here because we are dealing with incore
738 : * reservation. sb_frextents is not part of the lazy sb counters so it
739 : * must be consistent with the ondisk rtbitmap and must never include
740 : * incore reservations. sb_frextents was added to the lazy sb counters
741 : * when the realtime groups feature was introduced.
742 : */
743 19590613 : if (xfs_has_rtgroups(mp))
744 1802 : mp->m_sb.sb_frextents += rtxdelta;
745 19590613 : mp->m_sb.sb_dblocks += tp->t_dblocks_delta;
746 19590613 : mp->m_sb.sb_agcount += tp->t_agcount_delta;
747 19590613 : mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta;
748 19590613 : mp->m_sb.sb_rextsize += tp->t_rextsize_delta;
749 19590613 : if (tp->t_rextsize_delta) {
750 2 : mp->m_rtxblklog = log2_if_power2(mp->m_sb.sb_rextsize);
751 4 : mp->m_rtxblkmask = mask64_if_power2(mp->m_sb.sb_rextsize);
752 : }
753 19590613 : mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta;
754 19590613 : mp->m_sb.sb_rblocks += tp->t_rblocks_delta;
755 19590613 : mp->m_sb.sb_rextents += tp->t_rextents_delta;
756 19590613 : mp->m_sb.sb_rextslog += tp->t_rextslog_delta;
757 19590613 : mp->m_sb.sb_rgcount += tp->t_rgcount_delta;
758 19590613 : spin_unlock(&mp->m_sb_lock);
759 :
760 : /*
761 : * Debug checks outside of the spinlock so they don't lock up the
762 : * machine if they fail.
763 : */
764 : ASSERT(mp->m_sb.sb_imax_pct >= 0);
765 : ASSERT(mp->m_sb.sb_rextslog >= 0);
766 : return;
767 : }
768 :
769 : /* Add the given log item to the transaction's list of log items. */
770 : void
771 51971903943 : xfs_trans_add_item(
772 : struct xfs_trans *tp,
773 : struct xfs_log_item *lip)
774 : {
775 51971903943 : ASSERT(lip->li_log == tp->t_mountp->m_log);
776 51971903943 : ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
777 51971903943 : ASSERT(list_empty(&lip->li_trans));
778 51971903943 : ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
779 :
780 51971903943 : list_add_tail(&lip->li_trans, &tp->t_items);
781 51932234808 : trace_xfs_trans_add_item(tp, _RET_IP_);
782 51917061780 : }
783 :
784 : /*
785 : * Unlink the log item from the transaction. the log item is no longer
786 : * considered dirty in this transaction, as the linked transaction has
787 : * finished, either by abort or commit completion.
788 : */
789 : void
790 51905545949 : xfs_trans_del_item(
791 : struct xfs_log_item *lip)
792 : {
793 51905545949 : clear_bit(XFS_LI_DIRTY, &lip->li_flags);
794 51940277805 : list_del_init(&lip->li_trans);
795 51929828963 : }
796 :
797 : /* Detach and unlock all of the items in a transaction */
798 : static void
799 2103414020 : xfs_trans_free_items(
800 : struct xfs_trans *tp,
801 : bool abort)
802 : {
803 2103414020 : struct xfs_log_item *lip, *next;
804 :
805 2103414020 : trace_xfs_trans_free_items(tp, _RET_IP_);
806 :
807 2748285093 : list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
808 645458546 : xfs_trans_del_item(lip);
809 645381615 : if (abort)
810 25315 : set_bit(XFS_LI_ABORTED, &lip->li_flags);
811 645381616 : if (lip->li_ops->iop_release)
812 645381616 : lip->li_ops->iop_release(lip);
813 : }
814 2102826547 : }
815 :
816 : static inline void
817 46913885 : xfs_log_item_batch_insert(
818 : struct xfs_ail *ailp,
819 : struct xfs_ail_cursor *cur,
820 : struct xfs_log_item **log_items,
821 : int nr_items,
822 : xfs_lsn_t commit_lsn)
823 : {
824 46913885 : int i;
825 :
826 46913885 : spin_lock(&ailp->ail_lock);
827 : /* xfs_trans_ail_update_bulk drops ailp->ail_lock */
828 46913885 : xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn);
829 :
830 1467138553 : for (i = 0; i < nr_items; i++) {
831 1373310783 : struct xfs_log_item *lip = log_items[i];
832 :
833 1373310783 : if (lip->li_ops->iop_unpin)
834 1373310783 : lip->li_ops->iop_unpin(lip, 0);
835 : }
836 46913885 : }
837 :
838 : /*
839 : * Bulk operation version of xfs_trans_committed that takes a log vector of
840 : * items to insert into the AIL. This uses bulk AIL insertion techniques to
841 : * minimise lock traffic.
842 : *
843 : * If we are called with the aborted flag set, it is because a log write during
844 : * a CIL checkpoint commit has failed. In this case, all the items in the
845 : * checkpoint have already gone through iop_committed and iop_committing, which
846 : * means that checkpoint commit abort handling is treated exactly the same
847 : * as an iclog write error even though we haven't started any IO yet. Hence in
848 : * this case all we need to do is iop_committed processing, followed by an
849 : * iop_unpin(aborted) call.
850 : *
851 : * The AIL cursor is used to optimise the insert process. If commit_lsn is not
852 : * at the end of the AIL, the insert cursor avoids the need to walk
853 : * the AIL to find the insertion point on every xfs_log_item_batch_insert()
854 : * call. This saves a lot of needless list walking and is a net win, even
855 : * though it slightly increases that amount of AIL lock traffic to set it up
856 : * and tear it down.
857 : */
858 : void
859 6847602 : xfs_trans_committed_bulk(
860 : struct xfs_ail *ailp,
861 : struct list_head *lv_chain,
862 : xfs_lsn_t commit_lsn,
863 : bool aborted)
864 : {
865 : #define LOG_ITEM_BATCH_SIZE 32
866 6847602 : struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE];
867 6847602 : struct xfs_log_vec *lv;
868 6847602 : struct xfs_ail_cursor cur;
869 6847602 : int i = 0;
870 :
871 6847602 : spin_lock(&ailp->ail_lock);
872 6847602 : xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn);
873 6847602 : spin_unlock(&ailp->ail_lock);
874 :
875 : /* unpin all the log items */
876 1407197322 : list_for_each_entry(lv, lv_chain, lv_list) {
877 1400349720 : struct xfs_log_item *lip = lv->lv_item;
878 1400349720 : xfs_lsn_t item_lsn;
879 :
880 1400349720 : if (aborted)
881 1382715 : set_bit(XFS_LI_ABORTED, &lip->li_flags);
882 :
883 1400349721 : if (lip->li_ops->flags & XFS_ITEM_RELEASE_WHEN_COMMITTED) {
884 10262143 : lip->li_ops->iop_release(lip);
885 10262143 : continue;
886 : }
887 :
888 1390087578 : if (lip->li_ops->iop_committed)
889 1290578371 : item_lsn = lip->li_ops->iop_committed(lip, commit_lsn);
890 : else
891 : item_lsn = commit_lsn;
892 :
893 : /* item_lsn of -1 means the item needs no further processing */
894 1390087578 : if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
895 15042687 : continue;
896 :
897 : /*
898 : * if we are aborting the operation, no point in inserting the
899 : * object into the AIL as we are in a shutdown situation.
900 : */
901 1375044891 : if (aborted) {
902 2706104 : ASSERT(xlog_is_shutdown(ailp->ail_log));
903 1353052 : if (lip->li_ops->iop_unpin)
904 1353052 : lip->li_ops->iop_unpin(lip, 1);
905 1353051 : continue;
906 : }
907 :
908 1373691839 : if (item_lsn != commit_lsn) {
909 :
910 : /*
911 : * Not a bulk update option due to unusual item_lsn.
912 : * Push into AIL immediately, rechecking the lsn once
913 : * we have the ail lock. Then unpin the item. This does
914 : * not affect the AIL cursor the bulk insert path is
915 : * using.
916 : */
917 381056 : spin_lock(&ailp->ail_lock);
918 381056 : if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0)
919 0 : xfs_trans_ail_update(ailp, lip, item_lsn);
920 : else
921 381056 : spin_unlock(&ailp->ail_lock);
922 381056 : if (lip->li_ops->iop_unpin)
923 381056 : lip->li_ops->iop_unpin(lip, 0);
924 381056 : continue;
925 : }
926 :
927 : /* Item is a candidate for bulk AIL insert. */
928 1373310783 : log_items[i++] = lv->lv_item;
929 1373310783 : if (i >= LOG_ITEM_BATCH_SIZE) {
930 40190206 : xfs_log_item_batch_insert(ailp, &cur, log_items,
931 : LOG_ITEM_BATCH_SIZE, commit_lsn);
932 40190206 : i = 0;
933 : }
934 : }
935 :
936 : /* make sure we insert the remainder! */
937 6847602 : if (i)
938 6723679 : xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn);
939 :
940 6847602 : spin_lock(&ailp->ail_lock);
941 6847602 : xfs_trans_ail_cursor_done(&cur);
942 6847602 : spin_unlock(&ailp->ail_lock);
943 6847602 : }
944 :
945 : /*
946 : * Sort transaction items prior to running precommit operations. This will
947 : * attempt to order the items such that they will always be locked in the same
948 : * order. Items that have no sort function are moved to the end of the list
949 : * and so are locked last.
950 : *
951 : * This may need refinement as different types of objects add sort functions.
952 : *
953 : * Function is more complex than it needs to be because we are comparing 64 bit
954 : * values and the function only returns 32 bit values.
955 : */
956 : static int
957 19862365224 : xfs_trans_precommit_sort(
958 : void *unused_arg,
959 : const struct list_head *a,
960 : const struct list_head *b)
961 : {
962 19862365224 : struct xfs_log_item *lia = container_of(a,
963 : struct xfs_log_item, li_trans);
964 19862365224 : struct xfs_log_item *lib = container_of(b,
965 : struct xfs_log_item, li_trans);
966 19862365224 : int64_t diff;
967 :
968 : /*
969 : * If both items are non-sortable, leave them alone. If only one is
970 : * sortable, move the non-sortable item towards the end of the list.
971 : */
972 19862365224 : if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort)
973 : return 0;
974 11300109603 : if (!lia->li_ops->iop_sort)
975 : return 1;
976 10923237250 : if (!lib->li_ops->iop_sort)
977 : return -1;
978 :
979 2703272576 : diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib);
980 2703892179 : if (diff < 0)
981 : return -1;
982 999893627 : if (diff > 0)
983 956016706 : return 1;
984 : return 0;
985 : }
986 :
987 : /*
988 : * Run transaction precommit functions.
989 : *
990 : * If there is an error in any of the callouts, then stop immediately and
991 : * trigger a shutdown to abort the transaction. There is no recovery possible
992 : * from errors at this point as the transaction is dirty....
993 : */
994 : static int
995 4682910503 : xfs_trans_run_precommits(
996 : struct xfs_trans *tp)
997 : {
998 4682910503 : struct xfs_mount *mp = tp->t_mountp;
999 4682910503 : struct xfs_log_item *lip, *n;
1000 4682910503 : int error = 0;
1001 :
1002 : /*
1003 : * Sort the item list to avoid ABBA deadlocks with other transactions
1004 : * running precommit operations that lock multiple shared items such as
1005 : * inode cluster buffers.
1006 : */
1007 4682910503 : list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort);
1008 :
1009 : /*
1010 : * Precommit operations can remove the log item from the transaction
1011 : * if the log item exists purely to delay modifications until they
1012 : * can be ordered against other operations. Hence we have to use
1013 : * list_for_each_entry_safe() here.
1014 : */
1015 21471109595 : list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
1016 16786422846 : if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
1017 2581713980 : continue;
1018 14204708866 : if (lip->li_ops->iop_precommit) {
1019 4916403616 : error = lip->li_ops->iop_precommit(tp, lip);
1020 4918184148 : if (error)
1021 : break;
1022 : }
1023 : }
1024 4684687080 : if (error)
1025 331 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1026 4684687080 : return error;
1027 : }
1028 :
1029 : /*
1030 : * Commit the given transaction to the log.
1031 : *
1032 : * XFS disk error handling mechanism is not based on a typical
1033 : * transaction abort mechanism. Logically after the filesystem
1034 : * gets marked 'SHUTDOWN', we can't let any new transactions
1035 : * be durable - ie. committed to disk - because some metadata might
1036 : * be inconsistent. In such cases, this returns an error, and the
1037 : * caller may assume that all locked objects joined to the transaction
1038 : * have already been unlocked as if the commit had succeeded.
1039 : * Do not reference the transaction structure after this call.
1040 : */
1041 : static int
1042 3425162005 : __xfs_trans_commit(
1043 : struct xfs_trans *tp,
1044 : bool regrant)
1045 : {
1046 3425162005 : struct xfs_mount *mp = tp->t_mountp;
1047 3425162005 : struct xlog *log = mp->m_log;
1048 3425162005 : xfs_csn_t commit_seq = 0;
1049 3425162005 : int error = 0;
1050 3425162005 : int sync = tp->t_flags & XFS_TRANS_SYNC;
1051 :
1052 3425162005 : trace_xfs_trans_commit(tp, _RET_IP_);
1053 :
1054 3423827361 : error = xfs_trans_run_precommits(tp);
1055 3424561301 : if (error) {
1056 331 : if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
1057 325 : xfs_defer_cancel(tp);
1058 331 : goto out_unreserve;
1059 : }
1060 :
1061 : /*
1062 : * Finish deferred items on final commit. Only permanent transactions
1063 : * should ever have deferred ops.
1064 : */
1065 6850252701 : WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
1066 : !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
1067 3424560970 : if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
1068 1258103603 : error = xfs_defer_finish_noroll(&tp);
1069 1258354497 : if (error)
1070 3032 : goto out_unreserve;
1071 :
1072 : /* Run precommits from final tx in defer chain. */
1073 1258351465 : error = xfs_trans_run_precommits(tp);
1074 1259045083 : if (error)
1075 0 : goto out_unreserve;
1076 : }
1077 :
1078 : /*
1079 : * If there is nothing to be logged by the transaction,
1080 : * then unlock all of the items associated with the
1081 : * transaction and free the transaction structure.
1082 : * Also make sure to return any reserved blocks to
1083 : * the free pool.
1084 : */
1085 3425502450 : if (!(tp->t_flags & XFS_TRANS_DIRTY))
1086 72993684 : goto out_unreserve;
1087 :
1088 : /*
1089 : * We must check against log shutdown here because we cannot abort log
1090 : * items and leave them dirty, inconsistent and unpinned in memory while
1091 : * the log is active. This leaves them open to being written back to
1092 : * disk, and that will lead to on-disk corruption.
1093 : */
1094 6705017532 : if (xlog_is_shutdown(log)) {
1095 459 : error = -EIO;
1096 459 : goto out_unreserve;
1097 : }
1098 :
1099 3352508307 : ASSERT(tp->t_ticket != NULL);
1100 :
1101 : /*
1102 : * If we need to update the superblock, then do it now.
1103 : */
1104 3352508307 : if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1105 19590613 : xfs_trans_apply_sb_deltas(tp);
1106 3352508307 : xfs_trans_apply_dquot_deltas(tp);
1107 :
1108 3354461720 : xlog_cil_commit(log, tp, &commit_seq, regrant);
1109 :
1110 3354979447 : xfs_trans_free(tp);
1111 :
1112 : /*
1113 : * If the transaction needs to be synchronous, then force the
1114 : * log out now and wait for it.
1115 : */
1116 3354975294 : if (sync) {
1117 282775 : error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL);
1118 282775 : XFS_STATS_INC(mp, xs_trans_sync);
1119 : } else {
1120 3354692519 : XFS_STATS_INC(mp, xs_trans_async);
1121 : }
1122 :
1123 : return error;
1124 :
1125 72997506 : out_unreserve:
1126 72997506 : xfs_trans_unreserve_and_mod_sb(tp);
1127 :
1128 : /*
1129 : * It is indeed possible for the transaction to be not dirty but
1130 : * the dqinfo portion to be. All that means is that we have some
1131 : * (non-persistent) quota reservations that need to be unreserved.
1132 : */
1133 72996970 : xfs_trans_unreserve_and_mod_dquots(tp);
1134 72990432 : if (tp->t_ticket) {
1135 75680540 : if (regrant && !xlog_is_shutdown(log))
1136 2750855 : xfs_log_ticket_regrant(log, tp->t_ticket);
1137 : else
1138 70177773 : xfs_log_ticket_ungrant(log, tp->t_ticket);
1139 72953002 : tp->t_ticket = NULL;
1140 : }
1141 73014806 : xfs_trans_free_items(tp, !!error);
1142 73010369 : xfs_trans_free(tp);
1143 :
1144 73014104 : XFS_STATS_INC(mp, xs_trans_empty);
1145 73017187 : return error;
1146 : }
1147 :
1148 : int
1149 1463846187 : xfs_trans_commit(
1150 : struct xfs_trans *tp)
1151 : {
1152 1463846187 : return __xfs_trans_commit(tp, false);
1153 : }
1154 :
1155 : /*
1156 : * Unlock all of the transaction's items and free the transaction. If the
1157 : * transaction is dirty, we must shut down the filesystem because there is no
1158 : * way to restore them to their previous state.
1159 : *
1160 : * If the transaction has made a log reservation, make sure to release it as
1161 : * well.
1162 : *
1163 : * This is a high level function (equivalent to xfs_trans_commit()) and so can
1164 : * be called after the transaction has effectively been aborted due to the mount
1165 : * being shut down. However, if the mount has not been shut down and the
1166 : * transaction is dirty we will shut the mount down and, in doing so, that
1167 : * guarantees that the log is shut down, too. Hence we don't need to be as
1168 : * careful with shutdown state and dirty items here as we need to be in
1169 : * xfs_trans_commit().
1170 : */
1171 : void
1172 2030820874 : xfs_trans_cancel(
1173 : struct xfs_trans *tp)
1174 : {
1175 2030820874 : struct xfs_mount *mp = tp->t_mountp;
1176 2030820874 : struct xlog *log = mp->m_log;
1177 2030820874 : bool dirty = (tp->t_flags & XFS_TRANS_DIRTY);
1178 :
1179 2030820874 : trace_xfs_trans_cancel(tp, _RET_IP_);
1180 :
1181 : /*
1182 : * It's never valid to cancel a transaction with deferred ops attached,
1183 : * because the transaction is effectively dirty. Complain about this
1184 : * loudly before freeing the in-memory defer items and shutting down the
1185 : * filesystem.
1186 : */
1187 2030330128 : if (!list_empty(&tp->t_dfops)) {
1188 49 : ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1189 49 : dirty = true;
1190 49 : xfs_defer_cancel(tp);
1191 : }
1192 :
1193 : /*
1194 : * See if the caller is relying on us to shut down the filesystem. We
1195 : * only want an error report if there isn't already a shutdown in
1196 : * progress, so we only need to check against the mount shutdown state
1197 : * here.
1198 : */
1199 2030333664 : if (dirty && !xfs_is_shutdown(mp)) {
1200 23 : XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1201 23 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1202 : }
1203 : #ifdef DEBUG
1204 : /* Log items need to be consistent until the log is shut down. */
1205 4060966333 : if (!dirty && !xlog_is_shutdown(log)) {
1206 2030668531 : struct xfs_log_item *lip;
1207 :
1208 2623730352 : list_for_each_entry(lip, &tp->t_items, li_trans)
1209 593061821 : ASSERT(!xlog_item_is_intent_done(lip));
1210 : }
1211 : #endif
1212 2030330128 : xfs_trans_unreserve_and_mod_sb(tp);
1213 2031017290 : xfs_trans_unreserve_and_mod_dquots(tp);
1214 :
1215 2031246435 : if (tp->t_ticket) {
1216 514788000 : xfs_log_ticket_ungrant(log, tp->t_ticket);
1217 514578717 : tp->t_ticket = NULL;
1218 : }
1219 :
1220 2031037152 : xfs_trans_free_items(tp, dirty);
1221 2030268182 : xfs_trans_free(tp);
1222 2032330000 : }
1223 :
1224 : /*
1225 : * Roll from one trans in the sequence of PERMANENT transactions to
1226 : * the next: permanent transactions are only flushed out when
1227 : * committed with xfs_trans_commit(), but we still want as soon
1228 : * as possible to let chunks of it go to the log. So we commit the
1229 : * chunk we've been working on and get a new transaction to continue.
1230 : */
1231 : int
1232 1962025673 : xfs_trans_roll(
1233 : struct xfs_trans **tpp)
1234 : {
1235 1962025673 : struct xfs_trans *trans = *tpp;
1236 1962025673 : struct xfs_trans_res tres;
1237 1962025673 : int error;
1238 :
1239 1962025673 : trace_xfs_trans_roll(trans, _RET_IP_);
1240 :
1241 : /*
1242 : * Copy the critical parameters from one trans to the next.
1243 : */
1244 1961857947 : tres.tr_logres = trans->t_log_res;
1245 1961857947 : tres.tr_logcount = trans->t_log_count;
1246 :
1247 1961857947 : *tpp = xfs_trans_dup(trans);
1248 :
1249 : /*
1250 : * Commit the current transaction.
1251 : * If this commit failed, then it'd just unlock those items that
1252 : * are not marked ihold. That also means that a filesystem shutdown
1253 : * is in progress. The caller takes the responsibility to cancel
1254 : * the duplicate transaction that gets returned.
1255 : */
1256 1962116120 : error = __xfs_trans_commit(trans, true);
1257 1962744509 : if (error)
1258 : return error;
1259 :
1260 : /*
1261 : * Reserve space in the log for the next transaction.
1262 : * This also pushes items in the "AIL", the list of logged items,
1263 : * out to disk if they are taking up space at the tail of the log
1264 : * that we want to use. This requires that either nothing be locked
1265 : * across this call, or that anything that is locked be logged in
1266 : * the prior and the next transactions.
1267 : */
1268 1962743193 : tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1269 1962743193 : return xfs_trans_reserve(*tpp, &tres, 0, 0);
1270 : }
1271 :
1272 : /*
1273 : * Allocate an transaction, lock and join the inode to it, and reserve quota.
1274 : *
1275 : * The caller must ensure that the on-disk dquots attached to this inode have
1276 : * already been allocated and initialized. The caller is responsible for
1277 : * releasing ILOCK_EXCL if a new transaction is returned.
1278 : */
1279 : int
1280 1029109669 : xfs_trans_alloc_inode(
1281 : struct xfs_inode *ip,
1282 : struct xfs_trans_res *resv,
1283 : unsigned int dblocks,
1284 : unsigned int rblocks,
1285 : bool force,
1286 : struct xfs_trans **tpp)
1287 : {
1288 1029109669 : struct xfs_trans *tp;
1289 1029109669 : struct xfs_mount *mp = ip->i_mount;
1290 1029109669 : bool retried = false;
1291 1029110827 : int error;
1292 :
1293 1029110827 : retry:
1294 1956526928 : error = xfs_trans_alloc(mp, resv, dblocks,
1295 : xfs_extlen_to_rtxlen(mp, rblocks),
1296 : force ? XFS_TRANS_RESERVE : 0, &tp);
1297 1028735096 : if (error)
1298 10125412 : return error;
1299 :
1300 1018609684 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1301 1018747397 : xfs_trans_ijoin(tp, ip, 0);
1302 :
1303 1019111307 : error = xfs_qm_dqattach_locked(ip, false);
1304 1019100733 : if (error) {
1305 : /* Caller should have allocated the dquots! */
1306 52 : ASSERT(error != -ENOENT);
1307 52 : goto out_cancel;
1308 : }
1309 :
1310 1019100681 : error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force);
1311 1019427743 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1312 1158 : xfs_trans_cancel(tp);
1313 1158 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1314 1158 : xfs_blockgc_free_quota(ip, 0);
1315 1158 : retried = true;
1316 1158 : goto retry;
1317 : }
1318 1019426585 : if (error)
1319 1054 : goto out_cancel;
1320 :
1321 1019425531 : *tpp = tp;
1322 1019425531 : return 0;
1323 :
1324 1106 : out_cancel:
1325 1106 : xfs_trans_cancel(tp);
1326 1106 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1327 1106 : return error;
1328 : }
1329 :
1330 :
1331 : /* Try to reserve more blocks and file quota for a transaction. */
1332 : int
1333 139711 : xfs_trans_reserve_more_inode(
1334 : struct xfs_trans *tp,
1335 : struct xfs_inode *ip,
1336 : unsigned int dblocks,
1337 : unsigned int rblocks,
1338 : bool force_quota)
1339 : {
1340 139711 : struct xfs_mount *mp = ip->i_mount;
1341 139711 : unsigned int rtx = xfs_extlen_to_rtxlen(mp, rblocks);
1342 139697 : int error;
1343 :
1344 139697 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1345 :
1346 139698 : error = xfs_trans_reserve_more(tp, dblocks, rtx);
1347 139705 : if (error)
1348 : return error;
1349 :
1350 269821 : if (!XFS_IS_QUOTA_ON(mp) || xfs_is_quota_inode(&mp->m_sb, ip->i_ino))
1351 : return 0;
1352 :
1353 101959 : if (tp->t_flags & XFS_TRANS_RESERVE)
1354 0 : force_quota = true;
1355 :
1356 101959 : error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks,
1357 : force_quota);
1358 101955 : if (!error)
1359 : return 0;
1360 :
1361 : /* Quota failed, give back the new reservation. */
1362 0 : xfs_mod_fdblocks(mp, dblocks, tp->t_flags & XFS_TRANS_RESERVE);
1363 0 : tp->t_blk_res -= dblocks;
1364 0 : xfs_mod_frextents(mp, rtx);
1365 0 : tp->t_rtx_res -= rtx;
1366 0 : return error;
1367 : }
1368 :
1369 : /*
1370 : * Allocate an transaction in preparation for inode creation by reserving quota
1371 : * against the given dquots. Callers are not required to hold any inode locks.
1372 : */
1373 : int
1374 132577087 : xfs_trans_alloc_icreate(
1375 : struct xfs_mount *mp,
1376 : struct xfs_trans_res *resv,
1377 : struct xfs_dquot *udqp,
1378 : struct xfs_dquot *gdqp,
1379 : struct xfs_dquot *pdqp,
1380 : unsigned int dblocks,
1381 : struct xfs_trans **tpp)
1382 : {
1383 132577087 : struct xfs_trans *tp;
1384 132577087 : bool retried = false;
1385 132596191 : int error;
1386 :
1387 132596191 : retry:
1388 132596191 : error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
1389 131620636 : if (error)
1390 1538883 : return error;
1391 :
1392 130081753 : error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
1393 131580796 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1394 19105 : xfs_trans_cancel(tp);
1395 19105 : xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1396 19104 : retried = true;
1397 19104 : goto retry;
1398 : }
1399 131561691 : if (error) {
1400 13167 : xfs_trans_cancel(tp);
1401 13167 : return error;
1402 : }
1403 :
1404 131548524 : *tpp = tp;
1405 131548524 : return 0;
1406 : }
1407 :
1408 : /*
1409 : * Allocate an transaction, lock and join the inode to it, and reserve quota
1410 : * in preparation for inode attribute changes that include uid, gid, or prid
1411 : * changes.
1412 : *
1413 : * The caller must ensure that the on-disk dquots attached to this inode have
1414 : * already been allocated and initialized. The ILOCK will be dropped when the
1415 : * transaction is committed or cancelled.
1416 : */
1417 : int
1418 37588993 : xfs_trans_alloc_ichange(
1419 : struct xfs_inode *ip,
1420 : struct xfs_dquot *new_udqp,
1421 : struct xfs_dquot *new_gdqp,
1422 : struct xfs_dquot *new_pdqp,
1423 : bool force,
1424 : struct xfs_trans **tpp)
1425 : {
1426 37588993 : struct xfs_trans *tp;
1427 37588993 : struct xfs_mount *mp = ip->i_mount;
1428 37588993 : struct xfs_dquot *udqp;
1429 37588993 : struct xfs_dquot *gdqp;
1430 37588993 : struct xfs_dquot *pdqp;
1431 37588993 : bool retried = false;
1432 37589004 : int error;
1433 :
1434 37589004 : retry:
1435 37589004 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
1436 37594212 : if (error)
1437 1 : return error;
1438 :
1439 37594211 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1440 37616025 : xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1441 :
1442 37595723 : error = xfs_qm_dqattach_locked(ip, false);
1443 37530143 : if (error) {
1444 : /* Caller should have allocated the dquots! */
1445 312 : ASSERT(error != -ENOENT);
1446 312 : goto out_cancel;
1447 : }
1448 :
1449 : /*
1450 : * For each quota type, skip quota reservations if the inode's dquots
1451 : * now match the ones that came from the caller, or the caller didn't
1452 : * pass one in. The inode's dquots can change if we drop the ILOCK to
1453 : * perform a blockgc scan, so we must preserve the caller's arguments.
1454 : */
1455 37529831 : udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL;
1456 37529831 : gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL;
1457 37529831 : pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL;
1458 37529831 : if (udqp || gdqp || pdqp) {
1459 16341307 : xfs_filblks_t dblocks, rblocks;
1460 16341307 : unsigned int qflags = XFS_QMOPT_RES_REGBLKS;
1461 16341307 : bool isrt = XFS_IS_REALTIME_INODE(ip);
1462 :
1463 16341307 : if (force)
1464 15593267 : qflags |= XFS_QMOPT_FORCE_RES;
1465 :
1466 16341307 : if (isrt) {
1467 6259278 : error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1468 6255300 : if (error)
1469 11 : goto out_cancel;
1470 : }
1471 :
1472 16337329 : xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks);
1473 :
1474 16274693 : if (isrt)
1475 6255155 : rblocks += ip->i_delayed_blks;
1476 : else
1477 10019538 : dblocks += ip->i_delayed_blks;
1478 :
1479 : /*
1480 : * Reserve enough quota to handle blocks on disk and reserved
1481 : * for a delayed allocation. We'll actually transfer the
1482 : * delalloc reservation between dquots at chown time, even
1483 : * though that part is only semi-transactional.
1484 : */
1485 16274693 : error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
1486 : pdqp, dblocks, 1, qflags);
1487 16417876 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1488 6 : xfs_trans_cancel(tp);
1489 6 : xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1490 6 : retried = true;
1491 11 : goto retry;
1492 : }
1493 16417870 : if (error)
1494 6 : goto out_cancel;
1495 :
1496 : /* Do the same for realtime. */
1497 16417864 : qflags = XFS_QMOPT_RES_RTBLKS | (qflags & XFS_QMOPT_FORCE_RES);
1498 16417864 : error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp,
1499 : pdqp, rblocks, 0, qflags);
1500 16414003 : if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
1501 5 : xfs_trans_cancel(tp);
1502 5 : xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
1503 5 : retried = true;
1504 5 : goto retry;
1505 : }
1506 16413998 : if (error)
1507 5 : goto out_cancel;
1508 : }
1509 :
1510 37602517 : *tpp = tp;
1511 37602517 : return 0;
1512 :
1513 323 : out_cancel:
1514 323 : xfs_trans_cancel(tp);
1515 323 : return error;
1516 : }
1517 :
1518 : /*
1519 : * Allocate an transaction, lock and join the directory and child inodes to it,
1520 : * and reserve quota for a directory update. If there isn't sufficient space,
1521 : * @dblocks will be set to zero for a reservationless directory update and
1522 : * @nospace_error will be set to a negative errno describing the space
1523 : * constraint we hit.
1524 : *
1525 : * The caller must ensure that the on-disk dquots attached to this inode have
1526 : * already been allocated and initialized. The ILOCKs will be dropped when the
1527 : * transaction is committed or cancelled.
1528 : *
1529 : * Caller is responsible for unlocking the inodes manually upon return
1530 : */
1531 : int
1532 84501573 : xfs_trans_alloc_dir(
1533 : struct xfs_inode *dp,
1534 : struct xfs_trans_res *resv,
1535 : struct xfs_inode *ip,
1536 : unsigned int *dblocks,
1537 : struct xfs_trans **tpp,
1538 : int *nospace_error)
1539 : {
1540 84501573 : struct xfs_trans *tp;
1541 84501573 : struct xfs_mount *mp = ip->i_mount;
1542 84501573 : unsigned int resblks;
1543 84501573 : bool retried = false;
1544 84512014 : int error;
1545 :
1546 84512014 : retry:
1547 84512014 : *nospace_error = 0;
1548 84512014 : resblks = *dblocks;
1549 84512014 : error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1550 84502110 : if (error == -ENOSPC) {
1551 220019 : *nospace_error = error;
1552 220019 : resblks = 0;
1553 220019 : error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp);
1554 : }
1555 84502103 : if (error)
1556 1 : return error;
1557 :
1558 84502102 : xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL);
1559 :
1560 84525183 : xfs_trans_ijoin(tp, dp, 0);
1561 84523659 : xfs_trans_ijoin(tp, ip, 0);
1562 :
1563 84556666 : error = xfs_qm_dqattach_locked(dp, false);
1564 84549812 : if (error) {
1565 : /* Caller should have allocated the dquots! */
1566 0 : ASSERT(error != -ENOENT);
1567 0 : goto out_cancel;
1568 : }
1569 :
1570 84549812 : error = xfs_qm_dqattach_locked(ip, false);
1571 84548950 : if (error) {
1572 : /* Caller should have allocated the dquots! */
1573 0 : ASSERT(error != -ENOENT);
1574 0 : goto out_cancel;
1575 : }
1576 :
1577 84548950 : if (resblks == 0)
1578 220013 : goto done;
1579 :
1580 84328937 : error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false);
1581 84344344 : if (error == -EDQUOT || error == -ENOSPC) {
1582 19747 : if (!retried) {
1583 10441 : xfs_trans_cancel(tp);
1584 10441 : xfs_iunlock(dp, XFS_ILOCK_EXCL);
1585 10441 : if (dp != ip)
1586 10441 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1587 10441 : xfs_blockgc_free_quota(dp, 0);
1588 10441 : retried = true;
1589 10441 : goto retry;
1590 : }
1591 :
1592 9306 : *nospace_error = error;
1593 9306 : resblks = 0;
1594 9306 : error = 0;
1595 : }
1596 84333903 : if (error)
1597 0 : goto out_cancel;
1598 :
1599 84324597 : done:
1600 84553916 : *tpp = tp;
1601 84553916 : *dblocks = resblks;
1602 84553916 : return 0;
1603 :
1604 0 : out_cancel:
1605 0 : xfs_trans_cancel(tp);
1606 0 : return error;
1607 : }
|