]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/trans.c
libxfs: shorten inode item lifetime
[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 static void
509 xfs_buf_item_put(
510 struct xfs_buf_log_item *bip)
511 {
512 struct xfs_buf *bp = bip->bli_buf;
513
514 bp->b_log_item = NULL;
515 kmem_zone_free(xfs_buf_item_zone, bip);
516 }
517
518 void
519 libxfs_trans_brelse(
520 xfs_trans_t *tp,
521 xfs_buf_t *bp)
522 {
523 xfs_buf_log_item_t *bip;
524 #ifdef XACT_DEBUG
525 fprintf(stderr, "released buffer %p, transaction %p\n", bp, tp);
526 #endif
527
528 if (tp == NULL) {
529 ASSERT(bp->bp_transp == NULL);
530 libxfs_putbuf(bp);
531 return;
532 }
533 ASSERT(bp->bp_transp == tp);
534 bip = bp->b_log_item;
535 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
536 if (bip->bli_recur > 0) {
537 bip->bli_recur--;
538 return;
539 }
540 /* If dirty/stale, can't release till transaction committed */
541 if (bip->bli_flags & XFS_BLI_STALE)
542 return;
543 if (test_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags))
544 return;
545 xfs_trans_del_item(&bip->bli_item);
546 if (bip->bli_flags & XFS_BLI_HOLD)
547 bip->bli_flags &= ~XFS_BLI_HOLD;
548 xfs_buf_item_put(bip);
549 bp->b_transp = NULL;
550 libxfs_putbuf(bp);
551 }
552
553 void
554 libxfs_trans_binval(
555 xfs_trans_t *tp,
556 xfs_buf_t *bp)
557 {
558 xfs_buf_log_item_t *bip = bp->b_log_item;
559 #ifdef XACT_DEBUG
560 fprintf(stderr, "binval'd buffer %p, transaction %p\n", bp, tp);
561 #endif
562
563 ASSERT(bp->bp_transp == tp);
564 ASSERT(bip != NULL);
565
566 if (bip->bli_flags & XFS_BLI_STALE)
567 return;
568 XFS_BUF_UNDELAYWRITE(bp);
569 xfs_buf_stale(bp);
570 bip->bli_flags |= XFS_BLI_STALE;
571 bip->bli_flags &= ~XFS_BLI_DIRTY;
572 bip->bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
573 bip->bli_format.blf_flags |= XFS_BLF_CANCEL;
574 set_bit(XFS_LI_DIRTY, &bip->bli_item.li_flags);
575 tp->t_flags |= XFS_TRANS_DIRTY;
576 }
577
578 void
579 libxfs_trans_bjoin(
580 xfs_trans_t *tp,
581 xfs_buf_t *bp)
582 {
583 xfs_buf_log_item_t *bip;
584
585 ASSERT(bp->bp_transp == NULL);
586 #ifdef XACT_DEBUG
587 fprintf(stderr, "bjoin'd buffer %p, transaction %p\n", bp, tp);
588 #endif
589
590 xfs_buf_item_init(bp, tp->t_mountp);
591 bip = bp->b_log_item;
592 xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
593 bp->b_transp = tp;
594 }
595
596 void
597 libxfs_trans_bhold(
598 xfs_trans_t *tp,
599 xfs_buf_t *bp)
600 {
601 xfs_buf_log_item_t *bip = bp->b_log_item;
602
603 ASSERT(bp->bp_transp == tp);
604 ASSERT(bip != NULL);
605 #ifdef XACT_DEBUG
606 fprintf(stderr, "bhold'd buffer %p, transaction %p\n", bp, tp);
607 #endif
608
609 bip->bli_flags |= XFS_BLI_HOLD;
610 }
611
612 xfs_buf_t *
613 libxfs_trans_get_buf_map(
614 xfs_trans_t *tp,
615 struct xfs_buftarg *btp,
616 struct xfs_buf_map *map,
617 int nmaps,
618 uint f)
619 {
620 xfs_buf_t *bp;
621 xfs_buf_log_item_t *bip;
622
623 if (tp == NULL)
624 return libxfs_getbuf_map(btp, map, nmaps, 0);
625
626 bp = xfs_trans_buf_item_match(tp, btp, map, nmaps);
627 if (bp != NULL) {
628 ASSERT(bp->bp_transp == tp);
629 bip = bp->b_log_item;
630 ASSERT(bip != NULL);
631 bip->bli_recur++;
632 return bp;
633 }
634
635 bp = libxfs_getbuf_map(btp, map, nmaps, 0);
636 if (bp == NULL)
637 return NULL;
638 #ifdef XACT_DEBUG
639 fprintf(stderr, "trans_get_buf buffer %p, transaction %p\n", bp, tp);
640 #endif
641
642 libxfs_trans_bjoin(tp, bp);
643 bip = bp->b_log_item;
644 bip->bli_recur = 0;
645 return bp;
646 }
647
648 xfs_buf_t *
649 libxfs_trans_getsb(
650 xfs_trans_t *tp,
651 xfs_mount_t *mp,
652 int flags)
653 {
654 xfs_buf_t *bp;
655 xfs_buf_log_item_t *bip;
656 int len = XFS_FSS_TO_BB(mp, 1);
657 DEFINE_SINGLE_BUF_MAP(map, XFS_SB_DADDR, len);
658
659 if (tp == NULL)
660 return libxfs_getsb(mp, flags);
661
662 bp = xfs_trans_buf_item_match(tp, mp->m_dev, &map, 1);
663 if (bp != NULL) {
664 ASSERT(bp->bp_transp == tp);
665 bip = bp->b_log_item;
666 ASSERT(bip != NULL);
667 bip->bli_recur++;
668 return bp;
669 }
670
671 bp = libxfs_getsb(mp, flags);
672 #ifdef XACT_DEBUG
673 fprintf(stderr, "trans_get_sb buffer %p, transaction %p\n", bp, tp);
674 #endif
675
676 libxfs_trans_bjoin(tp, bp);
677 bip = bp->b_log_item;
678 bip->bli_recur = 0;
679 return bp;
680 }
681
682 int
683 libxfs_trans_read_buf_map(
684 xfs_mount_t *mp,
685 xfs_trans_t *tp,
686 struct xfs_buftarg *btp,
687 struct xfs_buf_map *map,
688 int nmaps,
689 uint flags,
690 xfs_buf_t **bpp,
691 const struct xfs_buf_ops *ops)
692 {
693 xfs_buf_t *bp;
694 xfs_buf_log_item_t *bip;
695 int error;
696
697 *bpp = NULL;
698
699 if (tp == NULL) {
700 bp = libxfs_readbuf_map(btp, map, nmaps, flags, ops);
701 if (!bp) {
702 return (flags & XBF_TRYLOCK) ? -EAGAIN : -ENOMEM;
703 }
704 if (bp->b_error)
705 goto out_relse;
706 goto done;
707 }
708
709 bp = xfs_trans_buf_item_match(tp, btp, map, nmaps);
710 if (bp != NULL) {
711 ASSERT(bp->bp_transp == tp);
712 ASSERT(bp->b_log_item != NULL);
713 bip = bp->b_log_item;
714 bip->bli_recur++;
715 goto done;
716 }
717
718 bp = libxfs_readbuf_map(btp, map, nmaps, flags, ops);
719 if (!bp) {
720 return (flags & XBF_TRYLOCK) ? -EAGAIN : -ENOMEM;
721 }
722 if (bp->b_error)
723 goto out_relse;
724
725 #ifdef XACT_DEBUG
726 fprintf(stderr, "trans_read_buf buffer %p, transaction %p\n", bp, tp);
727 #endif
728
729 xfs_trans_bjoin(tp, bp);
730 bip = bp->b_log_item;
731 bip->bli_recur = 0;
732 done:
733 *bpp = bp;
734 return 0;
735 out_relse:
736 error = bp->b_error;
737 xfs_buf_relse(bp);
738 return error;
739 }
740
741 /*
742 * Record the indicated change to the given field for application
743 * to the file system's superblock when the transaction commits.
744 * For now, just store the change in the transaction structure.
745 * Mark the transaction structure to indicate that the superblock
746 * needs to be updated before committing.
747 *
748 * Originally derived from xfs_trans_mod_sb().
749 */
750 void
751 libxfs_trans_mod_sb(
752 xfs_trans_t *tp,
753 uint field,
754 long delta)
755 {
756 switch (field) {
757 case XFS_TRANS_SB_RES_FDBLOCKS:
758 return;
759 case XFS_TRANS_SB_FDBLOCKS:
760 if (delta < 0) {
761 tp->t_blk_res_used += (uint)-delta;
762 if (tp->t_blk_res_used > tp->t_blk_res) {
763 fprintf(stderr,
764 _("Transaction block reservation exceeded! %u > %u\n"),
765 tp->t_blk_res_used, tp->t_blk_res);
766 ASSERT(0);
767 }
768 }
769 tp->t_fdblocks_delta += delta;
770 break;
771 case XFS_TRANS_SB_ICOUNT:
772 ASSERT(delta > 0);
773 tp->t_icount_delta += delta;
774 break;
775 case XFS_TRANS_SB_IFREE:
776 tp->t_ifree_delta += delta;
777 break;
778 case XFS_TRANS_SB_FREXTENTS:
779 tp->t_frextents_delta += delta;
780 break;
781 default:
782 ASSERT(0);
783 return;
784 }
785 tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
786 }
787
788 static void
789 xfs_inode_item_put(
790 struct xfs_inode_log_item *iip)
791 {
792 struct xfs_inode *ip = iip->ili_inode;
793
794 ip->i_itemp = NULL;
795 kmem_zone_free(xfs_ili_zone, iip);
796 }
797
798
799 /*
800 * Transaction commital code follows (i.e. write to disk in libxfs)
801 *
802 * XXX (dgc): should failure to flush the inode (e.g. due to uncorrected
803 * corruption) result in transaction commit failure w/ EFSCORRUPTED?
804 */
805 static void
806 inode_item_done(
807 xfs_inode_log_item_t *iip)
808 {
809 xfs_dinode_t *dip;
810 xfs_inode_t *ip;
811 xfs_mount_t *mp;
812 xfs_buf_t *bp;
813 int error;
814
815 ip = iip->ili_inode;
816 mp = iip->ili_item.li_mountp;
817 ASSERT(ip != NULL);
818
819 if (!(iip->ili_fields & XFS_ILOG_ALL)) {
820 ip->i_transp = NULL; /* disassociate from transaction */
821 iip->ili_flags = 0; /* reset all flags */
822 goto free;
823 }
824
825 /*
826 * Get the buffer containing the on-disk inode.
827 */
828 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, 0, 0);
829 if (error) {
830 fprintf(stderr, _("%s: warning - imap_to_bp failed (%d)\n"),
831 progname, error);
832 goto free;
833 }
834
835 /*
836 * Flush the inode and disassociate it from the transaction regardless
837 * of whether the flush succeed or not. If we fail the flush, make sure
838 * we still release the buffer reference we currently hold.
839 */
840 error = libxfs_iflush_int(ip, bp);
841 ip->i_transp = NULL; /* disassociate from transaction */
842 bp->b_transp = NULL; /* remove xact ptr */
843
844 if (error) {
845 fprintf(stderr, _("%s: warning - iflush_int failed (%d)\n"),
846 progname, error);
847 libxfs_putbuf(bp);
848 goto free;
849 }
850
851 libxfs_writebuf(bp, 0);
852 #ifdef XACT_DEBUG
853 fprintf(stderr, "flushing dirty inode %llu, buffer %p\n",
854 ip->i_ino, bp);
855 #endif
856 free:
857 xfs_inode_item_put(iip);
858 }
859
860 static void
861 buf_item_done(
862 xfs_buf_log_item_t *bip)
863 {
864 xfs_buf_t *bp;
865 int hold;
866 extern kmem_zone_t *xfs_buf_item_zone;
867
868 bp = bip->bli_buf;
869 ASSERT(bp != NULL);
870 bp->b_transp = NULL; /* remove xact ptr */
871
872 hold = (bip->bli_flags & XFS_BLI_HOLD);
873 if (bip->bli_flags & XFS_BLI_DIRTY) {
874 #ifdef XACT_DEBUG
875 fprintf(stderr, "flushing/staling buffer %p (hold=%d)\n",
876 bp, hold);
877 #endif
878 libxfs_writebuf_int(bp, 0);
879 }
880
881 bip->bli_flags &= ~XFS_BLI_HOLD;
882 xfs_buf_item_put(bip);
883 if (hold)
884 return;
885 libxfs_putbuf(bp);
886 }
887
888 static void
889 trans_committed(
890 xfs_trans_t *tp)
891 {
892 struct xfs_log_item *lip, *next;
893
894 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
895 xfs_trans_del_item(lip);
896
897 if (lip->li_type == XFS_LI_BUF)
898 buf_item_done((xfs_buf_log_item_t *)lip);
899 else if (lip->li_type == XFS_LI_INODE)
900 inode_item_done((xfs_inode_log_item_t *)lip);
901 else {
902 fprintf(stderr, _("%s: unrecognised log item type\n"),
903 progname);
904 ASSERT(0);
905 }
906 }
907 }
908
909 static void
910 buf_item_unlock(
911 xfs_buf_log_item_t *bip)
912 {
913 xfs_buf_t *bp = bip->bli_buf;
914 uint hold;
915
916 /* Clear the buffer's association with this transaction. */
917 bip->bli_buf->b_transp = NULL;
918
919 hold = bip->bli_flags & XFS_BLI_HOLD;
920 bip->bli_flags &= ~XFS_BLI_HOLD;
921 xfs_buf_item_put(bip);
922 if (!hold)
923 libxfs_putbuf(bp);
924 }
925
926 static void
927 inode_item_unlock(
928 xfs_inode_log_item_t *iip)
929 {
930 xfs_inode_t *ip = iip->ili_inode;
931
932 /* Clear the transaction pointer in the inode. */
933 ip->i_transp = NULL;
934
935 iip->ili_flags = 0;
936 xfs_inode_item_put(iip);
937 }
938
939 /* Detach and unlock all of the items in a transaction */
940 static void
941 xfs_trans_free_items(
942 struct xfs_trans *tp)
943 {
944 struct xfs_log_item *lip, *next;
945
946 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
947 xfs_trans_del_item(lip);
948 if (lip->li_type == XFS_LI_BUF)
949 buf_item_unlock((xfs_buf_log_item_t *)lip);
950 else if (lip->li_type == XFS_LI_INODE)
951 inode_item_unlock((xfs_inode_log_item_t *)lip);
952 else {
953 fprintf(stderr, _("%s: unrecognised log item type\n"),
954 progname);
955 ASSERT(0);
956 }
957 }
958 }
959
960 /*
961 * Commit the changes represented by this transaction
962 */
963 static int
964 __xfs_trans_commit(
965 struct xfs_trans *tp,
966 bool regrant)
967 {
968 struct xfs_sb *sbp;
969 int error = 0;
970
971 if (tp == NULL)
972 return 0;
973
974 /*
975 * Finish deferred items on final commit. Only permanent transactions
976 * should ever have deferred ops.
977 */
978 WARN_ON_ONCE(!list_empty(&tp->t_dfops) &&
979 !(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
980 if (!regrant && (tp->t_flags & XFS_TRANS_PERM_LOG_RES)) {
981 error = xfs_defer_finish_noroll(&tp);
982 if (error)
983 goto out_unreserve;
984 }
985
986 if (!(tp->t_flags & XFS_TRANS_DIRTY)) {
987 #ifdef XACT_DEBUG
988 fprintf(stderr, "committed clean transaction %p\n", tp);
989 #endif
990 goto out_unreserve;
991 }
992
993 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
994 sbp = &(tp->t_mountp->m_sb);
995 if (tp->t_icount_delta)
996 sbp->sb_icount += tp->t_icount_delta;
997 if (tp->t_ifree_delta)
998 sbp->sb_ifree += tp->t_ifree_delta;
999 if (tp->t_fdblocks_delta)
1000 sbp->sb_fdblocks += tp->t_fdblocks_delta;
1001 if (tp->t_frextents_delta)
1002 sbp->sb_frextents += tp->t_frextents_delta;
1003 xfs_log_sb(tp);
1004 }
1005
1006 #ifdef XACT_DEBUG
1007 fprintf(stderr, "committing dirty transaction %p\n", tp);
1008 #endif
1009 trans_committed(tp);
1010
1011 /* That's it for the transaction structure. Free it. */
1012 xfs_trans_free(tp);
1013 return 0;
1014
1015 out_unreserve:
1016 xfs_trans_free_items(tp);
1017 xfs_trans_free(tp);
1018 return error;
1019 }
1020
1021 int
1022 libxfs_trans_commit(
1023 struct xfs_trans *tp)
1024 {
1025 return __xfs_trans_commit(tp, false);
1026 }