]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/trans.c
libxfs: remove libxfs_trans_iget
[thirdparty/xfsprogs-dev.git] / libxfs / trans.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2001,2005-2006 Silicon Graphics, Inc.
4 * Copyright (C) 2010 Red Hat, Inc.
5 * All Rights Reserved.
6 */
7
8 #include "libxfs_priv.h"
9 #include "xfs_fs.h"
10 #include "xfs_shared.h"
11 #include "xfs_format.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_inode_buf.h"
16 #include "xfs_inode_fork.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
19 #include "xfs_sb.h"
20 #include "xfs_defer.h"
21
22 static void xfs_trans_free_items(struct xfs_trans *tp);
23 STATIC struct xfs_trans *xfs_trans_dup(struct xfs_trans *tp);
24 static int xfs_trans_reserve(struct xfs_trans *tp, struct xfs_trans_res *resp,
25 uint blocks, uint rtextents);
26 static int __xfs_trans_commit(struct xfs_trans *tp, bool regrant);
27
28 /*
29 * Simple transaction interface
30 */
31
32 kmem_zone_t *xfs_trans_zone;
33
34 /*
35 * Initialize the precomputed transaction reservation values
36 * in the mount structure.
37 */
38 void
39 libxfs_trans_init(
40 struct xfs_mount *mp)
41 {
42 xfs_trans_resv_calc(mp, &mp->m_resv);
43 }
44
45 /*
46 * Add the given log item to the transaction's list of log items.
47 */
48 void
49 libxfs_trans_add_item(
50 struct xfs_trans *tp,
51 struct xfs_log_item *lip)
52 {
53 ASSERT(lip->li_mountp == tp->t_mountp);
54 ASSERT(lip->li_ailp == tp->t_mountp->m_ail);
55 ASSERT(list_empty(&lip->li_trans));
56 ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags));
57
58 list_add_tail(&lip->li_trans, &tp->t_items);
59 }
60
61 /*
62 * Unlink and free the given descriptor.
63 */
64 void
65 libxfs_trans_del_item(
66 struct xfs_log_item *lip)
67 {
68 clear_bit(XFS_LI_DIRTY, &lip->li_flags);
69 list_del_init(&lip->li_trans);
70 }
71
72 /*
73 * Roll from one trans in the sequence of PERMANENT transactions to
74 * the next: permanent transactions are only flushed out when
75 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
76 * as possible to let chunks of it go to the log. So we commit the
77 * chunk we've been working on and get a new transaction to continue.
78 */
79 int
80 libxfs_trans_roll(
81 struct xfs_trans **tpp)
82 {
83 struct xfs_trans *trans = *tpp;
84 struct xfs_trans_res tres;
85 int error;
86
87 /*
88 * Copy the critical parameters from one trans to the next.
89 */
90 tres.tr_logres = trans->t_log_res;
91 tres.tr_logcount = trans->t_log_count;
92
93 *tpp = xfs_trans_dup(trans);
94
95 /*
96 * Commit the current transaction.
97 * If this commit failed, then it'd just unlock those items that
98 * are marked to be released. That also means that a filesystem shutdown
99 * is in progress. The caller takes the responsibility to cancel
100 * the duplicate transaction that gets returned.
101 */
102 error = __xfs_trans_commit(trans, true);
103 if (error)
104 return error;
105
106 /*
107 * Reserve space in the log for the next transaction.
108 * This also pushes items in the "AIL", the list of logged items,
109 * out to disk if they are taking up space at the tail of the log
110 * that we want to use. This requires that either nothing be locked
111 * across this call, or that anything that is locked be logged in
112 * the prior and the next transactions.
113 */
114 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
115 return xfs_trans_reserve(*tpp, &tres, 0, 0);
116 }
117
118 /*
119 * Free the transaction structure. If there is more clean up
120 * to do when the structure is freed, add it here.
121 */
122 static void
123 xfs_trans_free(
124 struct xfs_trans *tp)
125 {
126 kmem_zone_free(xfs_trans_zone, tp);
127 }
128
129 /*
130 * This is called to create a new transaction which will share the
131 * permanent log reservation of the given transaction. The remaining
132 * unused block and rt extent reservations are also inherited. This
133 * implies that the original transaction is no longer allowed to allocate
134 * blocks. Locks and log items, however, are no inherited. They must
135 * be added to the new transaction explicitly.
136 */
137 STATIC struct xfs_trans *
138 xfs_trans_dup(
139 struct xfs_trans *tp)
140 {
141 struct xfs_trans *ntp;
142
143 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
144
145 /*
146 * Initialize the new transaction structure.
147 */
148 ntp->t_mountp = tp->t_mountp;
149 INIT_LIST_HEAD(&ntp->t_items);
150 INIT_LIST_HEAD(&ntp->t_dfops);
151 ntp->t_firstblock = NULLFSBLOCK;
152
153 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
154
155 ntp->t_flags = XFS_TRANS_PERM_LOG_RES |
156 (tp->t_flags & XFS_TRANS_RESERVE) |
157 (tp->t_flags & XFS_TRANS_NO_WRITECOUNT);
158 /* We gave our writer reference to the new transaction */
159 tp->t_flags |= XFS_TRANS_NO_WRITECOUNT;
160
161 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
162 tp->t_blk_res = tp->t_blk_res_used;
163
164 /* move deferred ops over to the new tp */
165 xfs_defer_move(ntp, tp);
166
167 return ntp;
168 }
169
170 /*
171 * This is called to reserve free disk blocks and log space for the
172 * given transaction. This must be done before allocating any resources
173 * within the transaction.
174 *
175 * This will return ENOSPC if there are not enough blocks available.
176 * It will sleep waiting for available log space.
177 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
178 * is used by long running transactions. If any one of the reservations
179 * fails then they will all be backed out.
180 *
181 * This does not do quota reservations. That typically is done by the
182 * caller afterwards.
183 */
184 static int
185 xfs_trans_reserve(
186 struct xfs_trans *tp,
187 struct xfs_trans_res *resp,
188 uint blocks,
189 uint rtextents)
190 {
191 int error = 0;
192
193 /*
194 * Attempt to reserve the needed disk blocks by decrementing
195 * the number needed from the number available. This will
196 * fail if the count would go below zero.
197 */
198 if (blocks > 0) {
199 if (tp->t_mountp->m_sb.sb_fdblocks < blocks)
200 return -ENOSPC;
201 tp->t_blk_res += blocks;
202 }
203
204 /*
205 * Reserve the log space needed for this transaction.
206 */
207 if (resp->tr_logres > 0) {
208 ASSERT(tp->t_log_res == 0 ||
209 tp->t_log_res == resp->tr_logres);
210 ASSERT(tp->t_log_count == 0 ||
211 tp->t_log_count == resp->tr_logcount);
212
213 if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES)
214 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
215 else
216 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
217
218 tp->t_log_res = resp->tr_logres;
219 tp->t_log_count = resp->tr_logcount;
220 }
221
222 /*
223 * Attempt to reserve the needed realtime extents by decrementing
224 * the number needed from the number available. This will
225 * fail if the count would go below zero.
226 */
227 if (rtextents > 0) {
228 if (tp->t_mountp->m_sb.sb_rextents < rtextents) {
229 error = -ENOSPC;
230 goto undo_blocks;
231 }
232 }
233
234 return 0;
235
236 /*
237 * Error cases jump to one of these labels to undo any
238 * reservations which have already been performed.
239 */
240 undo_blocks:
241 if (blocks > 0)
242 tp->t_blk_res = 0;
243
244 return error;
245 }
246
247 int
248 libxfs_trans_alloc(
249 struct xfs_mount *mp,
250 struct xfs_trans_res *resp,
251 unsigned int blocks,
252 unsigned int rtextents,
253 unsigned int flags,
254 struct xfs_trans **tpp)
255
256 {
257 struct xfs_trans *tp;
258 int error;
259
260 tp = kmem_zone_zalloc(xfs_trans_zone,
261 (flags & XFS_TRANS_NOFS) ? KM_NOFS : KM_SLEEP);
262 tp->t_mountp = mp;
263 INIT_LIST_HEAD(&tp->t_items);
264 INIT_LIST_HEAD(&tp->t_dfops);
265 tp->t_firstblock = NULLFSBLOCK;
266
267 error = xfs_trans_reserve(tp, resp, blocks, rtextents);
268 if (error) {
269 xfs_trans_cancel(tp);
270 return error;
271 }
272 #ifdef XACT_DEBUG
273 fprintf(stderr, "allocated new transaction %p\n", tp);
274 #endif
275 *tpp = tp;
276 return 0;
277 }
278
279 /*
280 * Create an empty transaction with no reservation. This is a defensive
281 * mechanism for routines that query metadata without actually modifying
282 * them -- if the metadata being queried is somehow cross-linked (think a
283 * btree block pointer that points higher in the tree), we risk deadlock.
284 * However, blocks grabbed as part of a transaction can be re-grabbed.
285 * The verifiers will notice the corrupt block and the operation will fail
286 * back to userspace without deadlocking.
287 *
288 * Note the zero-length reservation; this transaction MUST be cancelled
289 * without any dirty data.
290 */
291 int
292 libxfs_trans_alloc_empty(
293 struct xfs_mount *mp,
294 struct xfs_trans **tpp)
295 {
296 struct xfs_trans_res resv = {0};
297
298 return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp);
299 }
300
301 /*
302 * Allocate a transaction that can be rolled. Since userspace doesn't have
303 * a need for log reservations, we really only tr_itruncate to get the
304 * permanent log reservation flag to avoid blowing asserts.
305 */
306 int
307 libxfs_trans_alloc_rollable(
308 struct xfs_mount *mp,
309 unsigned int blocks,
310 struct xfs_trans **tpp)
311 {
312 return libxfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, blocks,
313 0, 0, tpp);
314 }
315
316 void
317 libxfs_trans_cancel(
318 struct xfs_trans *tp)
319 {
320 #ifdef XACT_DEBUG
321 struct xfs_trans *otp = tp;
322 #endif
323 if (tp == NULL)
324 goto out;
325
326 if (tp->t_flags & XFS_TRANS_PERM_LOG_RES)
327 xfs_defer_cancel(tp);
328
329 xfs_trans_free_items(tp);
330 xfs_trans_free(tp);
331
332 out:
333 #ifdef XACT_DEBUG
334 fprintf(stderr, "## cancelled transaction %p\n", otp);
335 #endif
336 return;
337 }
338
339 void
340 libxfs_trans_ijoin(
341 xfs_trans_t *tp,
342 xfs_inode_t *ip,
343 uint lock_flags)
344 {
345 xfs_inode_log_item_t *iip;
346
347 ASSERT(ip->i_transp == NULL);
348 if (ip->i_itemp == NULL)
349 xfs_inode_item_init(ip, ip->i_mount);
350 iip = ip->i_itemp;
351 ASSERT(iip->ili_flags == 0);
352 ASSERT(iip->ili_inode != NULL);
353
354 ASSERT(iip->ili_lock_flags == 0);
355 iip->ili_lock_flags = lock_flags;
356
357 xfs_trans_add_item(tp, (xfs_log_item_t *)(iip));
358
359 ip->i_transp = tp;
360 #ifdef XACT_DEBUG
361 fprintf(stderr, "ijoin'd inode %llu, transaction %p\n", ip->i_ino, tp);
362 #endif
363 }
364
365 void
366 libxfs_trans_ijoin_ref(
367 xfs_trans_t *tp,
368 xfs_inode_t *ip,
369 int lock_flags)
370 {
371 ASSERT(ip->i_transp == tp);
372 ASSERT(ip->i_itemp != NULL);
373
374 xfs_trans_ijoin(tp, ip, lock_flags);
375
376 #ifdef XACT_DEBUG
377 fprintf(stderr, "ijoin_ref'd inode %llu, transaction %p\n", ip->i_ino, tp);
378 #endif
379 }
380
381 void
382 libxfs_trans_inode_alloc_buf(
383 xfs_trans_t *tp,
384 xfs_buf_t *bp)
385 {
386 xfs_buf_log_item_t *bip = bp->b_log_item;
387
388 ASSERT(bp->bp_transp == tp);
389 ASSERT(bip != NULL);
390 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
391 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
392 }
393
394 /*
395 * This is called to mark the fields indicated in fieldmask as needing
396 * to be logged when the transaction is committed. The inode must
397 * already be associated with the given transaction.
398 *
399 * The values for fieldmask are defined in xfs_log_format.h. We always
400 * log all of the core inode if any of it has changed, and we always log
401 * all of the inline data/extents/b-tree root if any of them has changed.
402 */
403 void
404 xfs_trans_log_inode(
405 xfs_trans_t *tp,
406 xfs_inode_t *ip,
407 uint flags)
408 {
409 ASSERT(ip->i_transp == tp);
410 ASSERT(ip->i_itemp != NULL);
411 #ifdef XACT_DEBUG
412 fprintf(stderr, "dirtied inode %llu, transaction %p\n", ip->i_ino, tp);
413 #endif
414
415 tp->t_flags |= XFS_TRANS_DIRTY;
416 set_bit(XFS_LI_DIRTY, &ip->i_itemp->ili_item.li_flags);
417
418 /*
419 * Always OR in the bits from the ili_last_fields field.
420 * This is to coordinate with the xfs_iflush() and xfs_iflush_done()
421 * routines in the eventual clearing of the ilf_fields bits.
422 * See the big comment in xfs_iflush() for an explanation of
423 * this coordination mechanism.
424 */
425 flags |= ip->i_itemp->ili_last_fields;
426 ip->i_itemp->ili_fields |= flags;
427 }
428
429 int
430 libxfs_trans_roll_inode(
431 struct xfs_trans **tpp,
432 struct xfs_inode *ip)
433 {
434 int error;
435
436 xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
437 error = xfs_trans_roll(tpp);
438 if (!error)
439 xfs_trans_ijoin(*tpp, ip, 0);
440 return error;
441 }
442
443
444 /*
445 * Mark a buffer dirty in the transaction.
446 */
447 void
448 libxfs_trans_dirty_buf(
449 struct xfs_trans *tp,
450 struct xfs_buf *bp)
451 {
452 struct xfs_buf_log_item *bip = bp->b_log_item;
453
454 ASSERT(bp->bp_transp == tp);
455 ASSERT(bip != NULL);
456
457 #ifdef XACT_DEBUG
458 fprintf(stderr, "dirtied buffer %p, transaction %p\n", bp, tp);
459 #endif
460 tp->t_flags |= XFS_TRANS_DIRTY;
461 set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
462 }
463
464 /*
465 * This is called to mark bytes first through last inclusive of the given
466 * buffer as needing to be logged when the transaction is committed.
467 * The buffer must already be associated with the given transaction.
468 *
469 * First and last are numbers relative to the beginning of this buffer,
470 * so the first byte in the buffer is numbered 0 regardless of the
471 * value of b_blkno.
472 */
473 void
474 libxfs_trans_log_buf(
475 struct xfs_trans *tp,
476 struct xfs_buf *bp,
477 uint first,
478 uint last)
479 {
480 struct xfs_buf_log_item *bip = bp->b_log_item;
481
482 ASSERT((first <= last) && (last < bp->b_bcount));
483
484 xfs_trans_dirty_buf(tp, bp);
485 xfs_buf_item_log(bip, first, last);
486 }
487
488 /*
489 * For userspace, ordered buffers just need to be marked dirty so
490 * the transaction commit will write them and mark them up-to-date.
491 * In essence, they are just like any other logged buffer in userspace.
492 *
493 * If the buffer is already dirty, trigger the "already logged" return condition.
494 */
495 bool
496 libxfs_trans_ordered_buf(
497 struct xfs_trans *tp,
498 struct xfs_buf *bp)
499 {
500 struct xfs_buf_log_item *bip = bp->b_log_item;
501 bool ret;
502
503 ret = test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
504 libxfs_trans_log_buf(tp, bp, 0, bp->b_bcount);
505 return ret;
506 }
507
508 void
509 libxfs_trans_brelse(
510 xfs_trans_t *tp,
511 xfs_buf_t *bp)
512 {
513 xfs_buf_log_item_t *bip;
514 #ifdef XACT_DEBUG
515 fprintf(stderr, "released buffer %p, transaction %p\n", bp, tp);
516 #endif
517
518 if (tp == NULL) {
519 ASSERT(bp->bp_transp == NULL);
520 libxfs_putbuf(bp);
521 return;
522 }
523 ASSERT(bp->bp_transp == tp);
524 bip = bp->b_log_item;
525 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
526 if (bip->bli_recur > 0) {
527 bip->bli_recur--;
528 return;
529 }
530 /* If dirty/stale, can't release till transaction committed */
531 if (bip->bli_flags & XFS_BLI_STALE)
532 return;
533 if (test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags))
534 return;
535 xfs_trans_del_item(&bip->bli_item);
536 if (bip->bli_flags & XFS_BLI_HOLD)
537 bip->bli_flags &= ~XFS_BLI_HOLD;
538 bp->b_transp = NULL;
539 libxfs_putbuf(bp);
540 }
541
542 void
543 libxfs_trans_binval(
544 xfs_trans_t *tp,
545 xfs_buf_t *bp)
546 {
547 xfs_buf_log_item_t *bip = bp->b_log_item;
548 #ifdef XACT_DEBUG
549 fprintf(stderr, "binval'd buffer %p, transaction %p\n", bp, tp);
550 #endif
551
552 ASSERT(bp->bp_transp == tp);
553 ASSERT(bip != NULL);
554
555 if (bip->bli_flags & XFS_BLI_STALE)
556 return;
557 XFS_BUF_UNDELAYWRITE(bp);
558 xfs_buf_stale(bp);
559 bip->bli_flags |= XFS_BLI_STALE;
560 bip->bli_flags &= ~XFS_BLI_DIRTY;
561 bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
562 bip->bli_format.blf_flags |= XFS_BLF_CANCEL;
563 set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
564 tp->t_flags |= XFS_TRANS_DIRTY;
565 }
566
567 void
568 libxfs_trans_bjoin(
569 xfs_trans_t *tp,
570 xfs_buf_t *bp)
571 {
572 xfs_buf_log_item_t *bip;
573
574 ASSERT(bp->bp_transp == NULL);
575 #ifdef XACT_DEBUG
576 fprintf(stderr, "bjoin'd buffer %p, transaction %p\n", bp, tp);
577 #endif
578
579 xfs_buf_item_init(bp, tp->t_mountp);
580 bip = bp->b_log_item;
581 xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
582 bp->b_transp = tp;
583 }
584
585 void
586 libxfs_trans_bhold(
587 xfs_trans_t *tp,
588 xfs_buf_t *bp)
589 {
590 xfs_buf_log_item_t *bip = bp->b_log_item;
591
592 ASSERT(bp->bp_transp == tp);
593 ASSERT(bip != NULL);
594 #ifdef XACT_DEBUG
595 fprintf(stderr, "bhold'd buffer %p, transaction %p\n", bp, tp);
596 #endif
597
598 bip->bli_flags |= XFS_BLI_HOLD;
599 }
600
601 xfs_buf_t *
602 libxfs_trans_get_buf_map(
603 xfs_trans_t *tp,
604 struct xfs_buftarg *btp,
605 struct xfs_buf_map *map,
606 int nmaps,
607 uint f)
608 {
609 xfs_buf_t *bp;
610 xfs_buf_log_item_t *bip;
611
612 if (tp == NULL)
613 return libxfs_getbuf_map(btp, map, nmaps, 0);
614
615 bp = xfs_trans_buf_item_match(tp, btp, map, nmaps);
616 if (bp != NULL) {
617 ASSERT(bp->bp_transp == tp);
618 bip = bp->b_log_item;
619 ASSERT(bip != NULL);
620 bip->bli_recur++;
621 return bp;
622 }
623
624 bp = libxfs_getbuf_map(btp, map, nmaps, 0);
625 if (bp == NULL)
626 return NULL;
627 #ifdef XACT_DEBUG
628 fprintf(stderr, "trans_get_buf buffer %p, transaction %p\n", bp, tp);
629 #endif
630
631 libxfs_trans_bjoin(tp, bp);
632 bip = bp->b_log_item;
633 bip->bli_recur = 0;
634 return bp;
635 }
636
637 xfs_buf_t *
638 libxfs_trans_getsb(
639 xfs_trans_t *tp,
640 xfs_mount_t *mp,
641 int flags)
642 {
643 xfs_buf_t *bp;
644 xfs_buf_log_item_t *bip;
645 int len = XFS_FSS_TO_BB(mp, 1);
646 DEFINE_SINGLE_BUF_MAP(map, XFS_SB_DADDR, len);
647
648 if (tp == NULL)
649 return libxfs_getsb(mp, flags);
650
651 bp = xfs_trans_buf_item_match(tp, mp->m_dev, &map, 1);
652 if (bp != NULL) {
653 ASSERT(bp->bp_transp == tp);
654 bip = bp->b_log_item;
655 ASSERT(bip != NULL);
656 bip->bli_recur++;
657 return bp;
658 }
659
660 bp = libxfs_getsb(mp, flags);
661 #ifdef XACT_DEBUG
662 fprintf(stderr, "trans_get_sb buffer %p, transaction %p\n", bp, tp);
663 #endif
664
665 libxfs_trans_bjoin(tp, bp);
666 bip = bp->b_log_item;
667 bip->bli_recur = 0;
668 return bp;
669 }
670
671 int
672 libxfs_trans_read_buf_map(
673 xfs_mount_t *mp,
674 xfs_trans_t *tp,
675 struct xfs_buftarg *btp,
676 struct xfs_buf_map *map,
677 int nmaps,
678 uint flags,
679 xfs_buf_t **bpp,
680 const struct xfs_buf_ops *ops)
681 {
682 xfs_buf_t *bp;
683 xfs_buf_log_item_t *bip;
684 int error;
685
686 *bpp = NULL;
687
688 if (tp == NULL) {
689 bp = libxfs_readbuf_map(btp, map, nmaps, flags, ops);
690 if (!bp) {
691 return (flags & XBF_TRYLOCK) ? -EAGAIN : -ENOMEM;
692 }
693 if (bp->b_error)
694 goto out_relse;
695 goto done;
696 }
697
698 bp = xfs_trans_buf_item_match(tp, btp, map, nmaps);
699 if (bp != NULL) {
700 ASSERT(bp->bp_transp == tp);
701 ASSERT(bp->b_log_item != NULL);
702 bip = bp->b_log_item;
703 bip->bli_recur++;
704 goto done;
705 }
706
707 bp = libxfs_readbuf_map(btp, map, nmaps, flags, ops);
708 if (!bp) {
709 return (flags & XBF_TRYLOCK) ? -EAGAIN : -ENOMEM;
710 }
711 if (bp->b_error)
712 goto out_relse;
713
714 #ifdef XACT_DEBUG
715 fprintf(stderr, "trans_read_buf buffer %p, transaction %p\n", bp, tp);
716 #endif
717
718 xfs_trans_bjoin(tp, bp);
719 bip = bp->b_log_item;
720 bip->bli_recur = 0;
721 done:
722 *bpp = bp;
723 return 0;
724 out_relse:
725 error = bp->b_error;
726 xfs_buf_relse(bp);
727 return error;
728 }
729
730 /*
731 * Record the indicated change to the given field for application
732 * to the file system's superblock when the transaction commits.
733 * For now, just store the change in the transaction structure.
734 * Mark the transaction structure to indicate that the superblock
735 * needs to be updated before committing.
736 *
737 * Originally derived from xfs_trans_mod_sb().
738 */
739 void
740 libxfs_trans_mod_sb(
741 xfs_trans_t *tp,
742 uint field,
743 long delta)
744 {
745 switch (field) {
746 case XFS_TRANS_SB_RES_FDBLOCKS:
747 return;
748 case XFS_TRANS_SB_FDBLOCKS:
749 if (delta < 0) {
750 tp->t_blk_res_used += (uint)-delta;
751 if (tp->t_blk_res_used > tp->t_blk_res) {
752 fprintf(stderr,
753 _("Transaction block reservation exceeded! %u > %u\n"),
754 tp->t_blk_res_used, tp->t_blk_res);
755 ASSERT(0);
756 }
757 }
758 tp->t_fdblocks_delta += delta;
759 break;
760 case XFS_TRANS_SB_ICOUNT:
761 ASSERT(delta > 0);
762 tp->t_icount_delta += delta;
763 break;
764 case XFS_TRANS_SB_IFREE:
765 tp->t_ifree_delta += delta;
766 break;
767 case XFS_TRANS_SB_FREXTENTS:
768 tp->t_frextents_delta += delta;
769 break;
770 default:
771 ASSERT(0);
772 return;
773 }
774 tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
775 }
776
777
778 /*
779 * Transaction commital code follows (i.e. write to disk in libxfs)
780 *
781 * XXX (dgc): should failure to flush the inode (e.g. due to uncorrected
782 * corruption) result in transaction commit failure w/ EFSCORRUPTED?
783 */
784 static void
785 inode_item_done(
786 xfs_inode_log_item_t *iip)
787 {
788 xfs_dinode_t *dip;
789 xfs_inode_t *ip;
790 xfs_mount_t *mp;
791 xfs_buf_t *bp;
792 int error;
793
794 ip = iip->ili_inode;
795 mp = iip->ili_item.li_mountp;
796 ASSERT(ip != NULL);
797
798 if (!(iip->ili_fields & XFS_ILOG_ALL)) {
799 ip->i_transp = NULL; /* disassociate from transaction */
800 iip->ili_flags = 0; /* reset all flags */
801 return;
802 }
803
804 /*
805 * Get the buffer containing the on-disk inode.
806 */
807 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, 0, 0);
808 if (error) {
809 fprintf(stderr, _("%s: warning - imap_to_bp failed (%d)\n"),
810 progname, error);
811 return;
812 }
813
814 /*
815 * Flush the inode and disassociate it from the transaction regardless
816 * of whether the flush succeed or not. If we fail the flush, make sure
817 * we still release the buffer reference we currently hold.
818 */
819 bp->b_log_item = iip;
820 error = libxfs_iflush_int(ip, bp);
821 ip->i_transp = NULL; /* disassociate from transaction */
822 bp->b_log_item = NULL; /* remove log item */
823 bp->b_transp = NULL; /* remove xact ptr */
824
825 if (error) {
826 fprintf(stderr, _("%s: warning - iflush_int failed (%d)\n"),
827 progname, error);
828 libxfs_putbuf(bp);
829 return;
830 }
831
832 libxfs_writebuf(bp, 0);
833 #ifdef XACT_DEBUG
834 fprintf(stderr, "flushing dirty inode %llu, buffer %p\n",
835 ip->i_ino, bp);
836 #endif
837 }
838
839 static void
840 buf_item_done(
841 xfs_buf_log_item_t *bip)
842 {
843 xfs_buf_t *bp;
844 int hold;
845 extern kmem_zone_t *xfs_buf_item_zone;
846
847 bp = bip->bli_buf;
848 ASSERT(bp != NULL);
849 bp->b_log_item = NULL; /* remove log item */
850 bp->b_transp = NULL; /* remove xact ptr */
851
852 hold = (bip->bli_flags & XFS_BLI_HOLD);
853 if (bip->bli_flags & XFS_BLI_DIRTY) {
854 #ifdef XACT_DEBUG
855 fprintf(stderr, "flushing/staling buffer %p (hold=%d)\n",
856 bp, hold);
857 #endif
858 libxfs_writebuf_int(bp, 0);
859 }
860 if (hold)
861 bip->bli_flags &= ~XFS_BLI_HOLD;
862 else
863 libxfs_putbuf(bp);
864 /* release the buf item */
865 kmem_zone_free(xfs_buf_item_zone, bip);
866 }
867
868 static void
869 trans_committed(
870 xfs_trans_t *tp)
871 {
872 struct xfs_log_item *lip, *next;
873
874 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
875 xfs_trans_del_item(lip);
876
877 if (lip->li_type == XFS_LI_BUF)
878 buf_item_done((xfs_buf_log_item_t *)lip);
879 else if (lip->li_type == XFS_LI_INODE)
880 inode_item_done((xfs_inode_log_item_t *)lip);
881 else {
882 fprintf(stderr, _("%s: unrecognised log item type\n"),
883 progname);
884 ASSERT(0);
885 }
886 }
887 }
888
889 static void
890 buf_item_unlock(
891 xfs_buf_log_item_t *bip)
892 {
893 xfs_buf_t *bp = bip->bli_buf;
894 uint hold;
895
896 /* Clear the buffer's association with this transaction. */
897 bip->bli_buf->b_transp = NULL;
898
899 hold = bip->bli_flags & XFS_BLI_HOLD;
900 bip->bli_flags &= ~XFS_BLI_HOLD;
901 if (!hold)
902 libxfs_putbuf(bp);
903 }
904
905 static void
906 inode_item_unlock(
907 xfs_inode_log_item_t *iip)
908 {
909 xfs_inode_t *ip = iip->ili_inode;
910
911 /* Clear the transaction pointer in the inode. */
912 ip->i_transp = NULL;
913
914 iip->ili_flags = 0;
915 }
916
917 /* Detach and unlock all of the items in a transaction */
918 static void
919 xfs_trans_free_items(
920 struct xfs_trans *tp)
921 {
922 struct xfs_log_item *lip, *next;
923
924 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
925 xfs_trans_del_item(lip);
926 if (lip->li_type == XFS_LI_BUF)
927 buf_item_unlock((xfs_buf_log_item_t *)lip);
928 else if (lip->li_type == XFS_LI_INODE)
929 inode_item_unlock((xfs_inode_log_item_t *)lip);
930 else {
931 fprintf(stderr, _("%s: unrecognised log item type\n"),
932 progname);
933 ASSERT(0);
934 }
935 }
936 }
937
938 /*
939 * Commit the changes represented by this transaction
940 */
941 static int
942 __xfs_trans_commit(
943 struct xfs_trans *tp,
944 bool regrant)
945 {
946 struct xfs_sb *sbp;
947 int error = 0;
948
949 if (tp == NULL)
950 return 0;
951
952 /*
953 * Finish deferred items on final commit. Only permanent transactions
954 * should ever have deferred ops.
955 */
956 WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
957 !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
958 if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
959 error = xfs_defer_finish_noroll(&tp);
960 if (error)
961 goto out_unreserve;
962 }
963
964 if (!(tp->t_flags & XFS_TRANS_DIRTY)) {
965 #ifdef XACT_DEBUG
966 fprintf(stderr, "committed clean transaction %p\n", tp);
967 #endif
968 goto out_unreserve;
969 }
970
971 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
972 sbp = &(tp->t_mountp->m_sb);
973 if (tp->t_icount_delta)
974 sbp->sb_icount += tp->t_icount_delta;
975 if (tp->t_ifree_delta)
976 sbp->sb_ifree += tp->t_ifree_delta;
977 if (tp->t_fdblocks_delta)
978 sbp->sb_fdblocks += tp->t_fdblocks_delta;
979 if (tp->t_frextents_delta)
980 sbp->sb_frextents += tp->t_frextents_delta;
981 xfs_log_sb(tp);
982 }
983
984 #ifdef XACT_DEBUG
985 fprintf(stderr, "committing dirty transaction %p\n", tp);
986 #endif
987 trans_committed(tp);
988
989 /* That's it for the transaction structure. Free it. */
990 xfs_trans_free(tp);
991 return 0;
992
993 out_unreserve:
994 xfs_trans_free_items(tp);
995 xfs_trans_free(tp);
996 return error;
997 }
998
999 int
1000 libxfs_trans_commit(
1001 struct xfs_trans *tp)
1002 {
1003 return __xfs_trans_commit(tp, false);
1004 }