1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_da_btree.h"
32#include "xfs_bmap_btree.h"
33#include "xfs_alloc_btree.h"
34#include "xfs_ialloc_btree.h"
35#include "xfs_dir2_sf.h"
36#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
39#include "xfs_btree.h"
40#include "xfs_ialloc.h"
41#include "xfs_alloc.h"
42#include "xfs_bmap.h"
43#include "xfs_quota.h"
44#include "xfs_trans_priv.h"
45#include "xfs_trans_space.h"
46#include "xfs_inode_item.h"
47
48
49STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *);
50STATIC uint xfs_trans_count_vecs(xfs_trans_t *);
51STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *);
52STATIC void xfs_trans_uncommit(xfs_trans_t *, uint);
53STATIC void xfs_trans_committed(xfs_trans_t *, int);
54STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
55STATIC void xfs_trans_free(xfs_trans_t *);
56
57kmem_zone_t *xfs_trans_zone;
58
59
60
61
62
63
64
65STATIC uint
66xfs_calc_write_reservation(xfs_mount_t *mp)
67{
68 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
69}
70
71STATIC uint
72xfs_calc_itruncate_reservation(xfs_mount_t *mp)
73{
74 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
75}
76
77STATIC uint
78xfs_calc_rename_reservation(xfs_mount_t *mp)
79{
80 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
81}
82
83STATIC uint
84xfs_calc_link_reservation(xfs_mount_t *mp)
85{
86 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
87}
88
89STATIC uint
90xfs_calc_remove_reservation(xfs_mount_t *mp)
91{
92 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
93}
94
95STATIC uint
96xfs_calc_symlink_reservation(xfs_mount_t *mp)
97{
98 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
99}
100
101STATIC uint
102xfs_calc_create_reservation(xfs_mount_t *mp)
103{
104 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
105}
106
107STATIC uint
108xfs_calc_mkdir_reservation(xfs_mount_t *mp)
109{
110 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
111}
112
113STATIC uint
114xfs_calc_ifree_reservation(xfs_mount_t *mp)
115{
116 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
117}
118
119STATIC uint
120xfs_calc_ichange_reservation(xfs_mount_t *mp)
121{
122 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
123}
124
125STATIC uint
126xfs_calc_growdata_reservation(xfs_mount_t *mp)
127{
128 return XFS_CALC_GROWDATA_LOG_RES(mp);
129}
130
131STATIC uint
132xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
133{
134 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
135}
136
137STATIC uint
138xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
139{
140 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
141}
142
143STATIC uint
144xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
145{
146 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
147}
148
149STATIC uint
150xfs_calc_swrite_reservation(xfs_mount_t *mp)
151{
152 return XFS_CALC_SWRITE_LOG_RES(mp);
153}
154
155STATIC uint
156xfs_calc_writeid_reservation(xfs_mount_t *mp)
157{
158 return XFS_CALC_WRITEID_LOG_RES(mp);
159}
160
161STATIC uint
162xfs_calc_addafork_reservation(xfs_mount_t *mp)
163{
164 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
165}
166
167STATIC uint
168xfs_calc_attrinval_reservation(xfs_mount_t *mp)
169{
170 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
171}
172
173STATIC uint
174xfs_calc_attrset_reservation(xfs_mount_t *mp)
175{
176 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
177}
178
179STATIC uint
180xfs_calc_attrrm_reservation(xfs_mount_t *mp)
181{
182 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
183}
184
185STATIC uint
186xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
187{
188 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
189}
190
191
192
193
194
195void
196xfs_trans_init(
197 xfs_mount_t *mp)
198{
199 xfs_trans_reservations_t *resp;
200
201 resp = &(mp->m_reservations);
202 resp->tr_write = xfs_calc_write_reservation(mp);
203 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
204 resp->tr_rename = xfs_calc_rename_reservation(mp);
205 resp->tr_link = xfs_calc_link_reservation(mp);
206 resp->tr_remove = xfs_calc_remove_reservation(mp);
207 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
208 resp->tr_create = xfs_calc_create_reservation(mp);
209 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
210 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
211 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
212 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
213 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
214 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
215 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
216 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
217 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
218 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
219 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
220 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
221 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
222 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
223}
224
225
226
227
228
229
230
231
232
233xfs_trans_t *
234xfs_trans_alloc(
235 xfs_mount_t *mp,
236 uint type)
237{
238 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
239 return _xfs_trans_alloc(mp, type);
240}
241
242xfs_trans_t *
243_xfs_trans_alloc(
244 xfs_mount_t *mp,
245 uint type)
246{
247 xfs_trans_t *tp;
248
249 atomic_inc(&mp->m_active_trans);
250
251 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
252 tp->t_magic = XFS_TRANS_MAGIC;
253 tp->t_type = type;
254 tp->t_mountp = mp;
255 tp->t_items_free = XFS_LIC_NUM_SLOTS;
256 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
257 xfs_lic_init(&(tp->t_items));
258 XFS_LBC_INIT(&(tp->t_busy));
259 return tp;
260}
261
262
263
264
265
266
267
268
269
270xfs_trans_t *
271xfs_trans_dup(
272 xfs_trans_t *tp)
273{
274 xfs_trans_t *ntp;
275
276 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
277
278
279
280
281 ntp->t_magic = XFS_TRANS_MAGIC;
282 ntp->t_type = tp->t_type;
283 ntp->t_mountp = tp->t_mountp;
284 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
285 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
286 xfs_lic_init(&(ntp->t_items));
287 XFS_LBC_INIT(&(ntp->t_busy));
288
289 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
290 ASSERT(tp->t_ticket != NULL);
291
292 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
293 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
294 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
295 tp->t_blk_res = tp->t_blk_res_used;
296 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
297 tp->t_rtx_res = tp->t_rtx_res_used;
298 ntp->t_pflags = tp->t_pflags;
299
300 xfs_trans_dup_dqinfo(tp, ntp);
301
302 atomic_inc(&tp->t_mountp->m_active_trans);
303 return ntp;
304}
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320int
321xfs_trans_reserve(
322 xfs_trans_t *tp,
323 uint blocks,
324 uint logspace,
325 uint rtextents,
326 uint flags,
327 uint logcount)
328{
329 int log_flags;
330 int error = 0;
331 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
332
333
334 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
335
336
337
338
339
340
341 if (blocks > 0) {
342 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
343 -((int64_t)blocks), rsvd);
344 if (error != 0) {
345 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
346 return (XFS_ERROR(ENOSPC));
347 }
348 tp->t_blk_res += blocks;
349 }
350
351
352
353
354 if (logspace > 0) {
355 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
356 ASSERT((tp->t_log_count == 0) ||
357 (tp->t_log_count == logcount));
358 if (flags & XFS_TRANS_PERM_LOG_RES) {
359 log_flags = XFS_LOG_PERM_RESERV;
360 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
361 } else {
362 ASSERT(tp->t_ticket == NULL);
363 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
364 log_flags = 0;
365 }
366
367 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
368 &tp->t_ticket,
369 XFS_TRANSACTION, log_flags, tp->t_type);
370 if (error) {
371 goto undo_blocks;
372 }
373 tp->t_log_res = logspace;
374 tp->t_log_count = logcount;
375 }
376
377
378
379
380
381
382 if (rtextents > 0) {
383 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
384 -((int64_t)rtextents), rsvd);
385 if (error) {
386 error = XFS_ERROR(ENOSPC);
387 goto undo_log;
388 }
389 tp->t_rtx_res += rtextents;
390 }
391
392 return 0;
393
394
395
396
397
398undo_log:
399 if (logspace > 0) {
400 if (flags & XFS_TRANS_PERM_LOG_RES) {
401 log_flags = XFS_LOG_REL_PERM_RESERV;
402 } else {
403 log_flags = 0;
404 }
405 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
406 tp->t_ticket = NULL;
407 tp->t_log_res = 0;
408 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
409 }
410
411undo_blocks:
412 if (blocks > 0) {
413 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
414 (int64_t)blocks, rsvd);
415 tp->t_blk_res = 0;
416 }
417
418 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
419
420 return error;
421}
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440void
441xfs_trans_mod_sb(
442 xfs_trans_t *tp,
443 uint field,
444 int64_t delta)
445{
446 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
447 xfs_mount_t *mp = tp->t_mountp;
448
449 switch (field) {
450 case XFS_TRANS_SB_ICOUNT:
451 tp->t_icount_delta += delta;
452 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
453 flags &= ~XFS_TRANS_SB_DIRTY;
454 break;
455 case XFS_TRANS_SB_IFREE:
456 tp->t_ifree_delta += delta;
457 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
458 flags &= ~XFS_TRANS_SB_DIRTY;
459 break;
460 case XFS_TRANS_SB_FDBLOCKS:
461
462
463
464
465
466 if (delta < 0) {
467 tp->t_blk_res_used += (uint)-delta;
468 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
469 }
470 tp->t_fdblocks_delta += delta;
471 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
472 flags &= ~XFS_TRANS_SB_DIRTY;
473 break;
474 case XFS_TRANS_SB_RES_FDBLOCKS:
475
476
477
478
479
480 ASSERT(delta < 0);
481 tp->t_res_fdblocks_delta += delta;
482 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
483 flags &= ~XFS_TRANS_SB_DIRTY;
484 break;
485 case XFS_TRANS_SB_FREXTENTS:
486
487
488
489
490
491 if (delta < 0) {
492 tp->t_rtx_res_used += (uint)-delta;
493 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
494 }
495 tp->t_frextents_delta += delta;
496 break;
497 case XFS_TRANS_SB_RES_FREXTENTS:
498
499
500
501
502
503 ASSERT(delta < 0);
504 tp->t_res_frextents_delta += delta;
505 break;
506 case XFS_TRANS_SB_DBLOCKS:
507 ASSERT(delta > 0);
508 tp->t_dblocks_delta += delta;
509 break;
510 case XFS_TRANS_SB_AGCOUNT:
511 ASSERT(delta > 0);
512 tp->t_agcount_delta += delta;
513 break;
514 case XFS_TRANS_SB_IMAXPCT:
515 tp->t_imaxpct_delta += delta;
516 break;
517 case XFS_TRANS_SB_REXTSIZE:
518 tp->t_rextsize_delta += delta;
519 break;
520 case XFS_TRANS_SB_RBMBLOCKS:
521 tp->t_rbmblocks_delta += delta;
522 break;
523 case XFS_TRANS_SB_RBLOCKS:
524 tp->t_rblocks_delta += delta;
525 break;
526 case XFS_TRANS_SB_REXTENTS:
527 tp->t_rextents_delta += delta;
528 break;
529 case XFS_TRANS_SB_REXTSLOG:
530 tp->t_rextslog_delta += delta;
531 break;
532 default:
533 ASSERT(0);
534 return;
535 }
536
537 tp->t_flags |= flags;
538}
539
540
541
542
543
544
545
546
547
548STATIC void
549xfs_trans_apply_sb_deltas(
550 xfs_trans_t *tp)
551{
552 xfs_dsb_t *sbp;
553 xfs_buf_t *bp;
554 int whole = 0;
555
556 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
557 sbp = XFS_BUF_TO_SBP(bp);
558
559
560
561
562 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
563 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
564 tp->t_ag_btree_delta));
565
566
567
568
569 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
570 if (tp->t_icount_delta)
571 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
572 if (tp->t_ifree_delta)
573 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
574 if (tp->t_fdblocks_delta)
575 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
576 if (tp->t_res_fdblocks_delta)
577 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
578 }
579
580 if (tp->t_frextents_delta)
581 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
582 if (tp->t_res_frextents_delta)
583 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
584
585 if (tp->t_dblocks_delta) {
586 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
587 whole = 1;
588 }
589 if (tp->t_agcount_delta) {
590 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
591 whole = 1;
592 }
593 if (tp->t_imaxpct_delta) {
594 sbp->sb_imax_pct += tp->t_imaxpct_delta;
595 whole = 1;
596 }
597 if (tp->t_rextsize_delta) {
598 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
599 whole = 1;
600 }
601 if (tp->t_rbmblocks_delta) {
602 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
603 whole = 1;
604 }
605 if (tp->t_rblocks_delta) {
606 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
607 whole = 1;
608 }
609 if (tp->t_rextents_delta) {
610 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
611 whole = 1;
612 }
613 if (tp->t_rextslog_delta) {
614 sbp->sb_rextslog += tp->t_rextslog_delta;
615 whole = 1;
616 }
617
618 if (whole)
619
620
621
622 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
623 else
624
625
626
627
628 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
629 offsetof(xfs_dsb_t, sb_frextents) +
630 sizeof(sbp->sb_frextents) - 1);
631}
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652STATIC void
653xfs_trans_unreserve_and_mod_sb(
654 xfs_trans_t *tp)
655{
656 xfs_mod_sb_t msb[14];
657 xfs_mod_sb_t *msbp;
658 xfs_mount_t *mp = tp->t_mountp;
659
660 int error;
661 int rsvd;
662 int64_t blkdelta = 0;
663 int64_t rtxdelta = 0;
664
665 msbp = msb;
666 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
667
668
669 if (tp->t_blk_res > 0)
670 blkdelta = tp->t_blk_res;
671
672 if ((tp->t_fdblocks_delta != 0) &&
673 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
674 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
675 blkdelta += tp->t_fdblocks_delta;
676
677 if (blkdelta != 0) {
678 msbp->msb_field = XFS_SBS_FDBLOCKS;
679 msbp->msb_delta = blkdelta;
680 msbp++;
681 }
682
683
684 if (tp->t_rtx_res > 0)
685 rtxdelta = tp->t_rtx_res;
686
687 if ((tp->t_frextents_delta != 0) &&
688 (tp->t_flags & XFS_TRANS_SB_DIRTY))
689 rtxdelta += tp->t_frextents_delta;
690
691 if (rtxdelta != 0) {
692 msbp->msb_field = XFS_SBS_FREXTENTS;
693 msbp->msb_delta = rtxdelta;
694 msbp++;
695 }
696
697
698
699 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
700 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
701 if (tp->t_icount_delta != 0) {
702 msbp->msb_field = XFS_SBS_ICOUNT;
703 msbp->msb_delta = tp->t_icount_delta;
704 msbp++;
705 }
706 if (tp->t_ifree_delta != 0) {
707 msbp->msb_field = XFS_SBS_IFREE;
708 msbp->msb_delta = tp->t_ifree_delta;
709 msbp++;
710 }
711 }
712
713 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
714 if (tp->t_dblocks_delta != 0) {
715 msbp->msb_field = XFS_SBS_DBLOCKS;
716 msbp->msb_delta = tp->t_dblocks_delta;
717 msbp++;
718 }
719 if (tp->t_agcount_delta != 0) {
720 msbp->msb_field = XFS_SBS_AGCOUNT;
721 msbp->msb_delta = tp->t_agcount_delta;
722 msbp++;
723 }
724 if (tp->t_imaxpct_delta != 0) {
725 msbp->msb_field = XFS_SBS_IMAX_PCT;
726 msbp->msb_delta = tp->t_imaxpct_delta;
727 msbp++;
728 }
729 if (tp->t_rextsize_delta != 0) {
730 msbp->msb_field = XFS_SBS_REXTSIZE;
731 msbp->msb_delta = tp->t_rextsize_delta;
732 msbp++;
733 }
734 if (tp->t_rbmblocks_delta != 0) {
735 msbp->msb_field = XFS_SBS_RBMBLOCKS;
736 msbp->msb_delta = tp->t_rbmblocks_delta;
737 msbp++;
738 }
739 if (tp->t_rblocks_delta != 0) {
740 msbp->msb_field = XFS_SBS_RBLOCKS;
741 msbp->msb_delta = tp->t_rblocks_delta;
742 msbp++;
743 }
744 if (tp->t_rextents_delta != 0) {
745 msbp->msb_field = XFS_SBS_REXTENTS;
746 msbp->msb_delta = tp->t_rextents_delta;
747 msbp++;
748 }
749 if (tp->t_rextslog_delta != 0) {
750 msbp->msb_field = XFS_SBS_REXTSLOG;
751 msbp->msb_delta = tp->t_rextslog_delta;
752 msbp++;
753 }
754 }
755
756
757
758
759 if (msbp > msb) {
760 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
761 (uint)(msbp - msb), rsvd);
762 ASSERT(error == 0);
763 }
764}
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782int
783_xfs_trans_commit(
784 xfs_trans_t *tp,
785 uint flags,
786 int *log_flushed)
787{
788 xfs_log_iovec_t *log_vector;
789 int nvec;
790 xfs_mount_t *mp;
791 xfs_lsn_t commit_lsn;
792
793 int error;
794 int log_flags;
795 int sync;
796#define XFS_TRANS_LOGVEC_COUNT 16
797 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
798 void *commit_iclog;
799 int shutdown;
800
801 commit_lsn = -1;
802
803
804
805
806
807 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
808 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
809 log_flags = XFS_LOG_REL_PERM_RESERV;
810 } else {
811 log_flags = 0;
812 }
813 mp = tp->t_mountp;
814
815
816
817
818
819
820
821
822shut_us_down:
823 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
824 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
825 xfs_trans_unreserve_and_mod_sb(tp);
826
827
828
829
830
831
832 xfs_trans_unreserve_and_mod_dquots(tp);
833 if (tp->t_ticket) {
834 commit_lsn = xfs_log_done(mp, tp->t_ticket,
835 NULL, log_flags);
836 if (commit_lsn == -1 && !shutdown)
837 shutdown = XFS_ERROR(EIO);
838 }
839 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
840 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
841 xfs_trans_free_busy(tp);
842 xfs_trans_free(tp);
843 XFS_STATS_INC(xs_trans_empty);
844 return (shutdown);
845 }
846 ASSERT(tp->t_ticket != NULL);
847
848
849
850
851 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
852 xfs_trans_apply_sb_deltas(tp);
853 xfs_trans_apply_dquot_deltas(tp);
854
855
856
857
858
859
860
861 nvec = xfs_trans_count_vecs(tp);
862 if (nvec == 0) {
863 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
864 goto shut_us_down;
865 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
866 log_vector = log_vector_fast;
867 } else {
868 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
869 sizeof(xfs_log_iovec_t),
870 KM_SLEEP);
871 }
872
873
874
875
876
877 xfs_trans_fill_vecs(tp, log_vector);
878
879 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
880
881
882
883
884
885
886 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
887
888 tp->t_commit_lsn = commit_lsn;
889 if (nvec > XFS_TRANS_LOGVEC_COUNT) {
890 kmem_free(log_vector);
891 }
892
893
894
895
896
897 if (error || commit_lsn == -1) {
898 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
899 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
900 return XFS_ERROR(EIO);
901 }
902
903
904
905
906
907
908
909 xfs_trans_unreserve_and_mod_sb(tp);
910
911 sync = tp->t_flags & XFS_TRANS_SYNC;
912
913
914
915
916
917
918
919
920
921
922
923
924
925 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
926 tp->t_logcb.cb_arg = tp;
927
928
929
930
931
932
933
934
935 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
936
937
938
939
940 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955 xfs_trans_unlock_items(tp, commit_lsn);
956
957
958
959
960
961
962
963
964
965
966
967 if (shutdown)
968 xfs_trans_committed(tp, XFS_LI_ABORTED);
969
970
971
972
973
974
975 error = xfs_log_release_iclog(mp, commit_iclog);
976
977
978
979
980
981 if (sync) {
982 if (!error) {
983 error = _xfs_log_force(mp, commit_lsn,
984 XFS_LOG_FORCE | XFS_LOG_SYNC,
985 log_flushed);
986 }
987 XFS_STATS_INC(xs_trans_sync);
988 } else {
989 XFS_STATS_INC(xs_trans_async);
990 }
991
992 return (error);
993}
994
995
996
997
998
999
1000
1001
1002STATIC uint
1003xfs_trans_count_vecs(
1004 xfs_trans_t *tp)
1005{
1006 int nvecs;
1007 xfs_log_item_desc_t *lidp;
1008
1009 nvecs = 1;
1010 lidp = xfs_trans_first_item(tp);
1011 ASSERT(lidp != NULL);
1012
1013
1014
1015
1016
1017 if (lidp == NULL)
1018 return 0;
1019
1020 while (lidp != NULL) {
1021
1022
1023
1024 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1025 lidp = xfs_trans_next_item(tp, lidp);
1026 continue;
1027 }
1028 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1029 nvecs += lidp->lid_size;
1030 lidp = xfs_trans_next_item(tp, lidp);
1031 }
1032
1033 return nvecs;
1034}
1035
1036
1037
1038
1039
1040STATIC void
1041xfs_trans_uncommit(
1042 xfs_trans_t *tp,
1043 uint flags)
1044{
1045 xfs_log_item_desc_t *lidp;
1046
1047 for (lidp = xfs_trans_first_item(tp);
1048 lidp != NULL;
1049 lidp = xfs_trans_next_item(tp, lidp)) {
1050
1051
1052
1053 if (lidp->lid_flags & XFS_LID_DIRTY)
1054 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1055 }
1056
1057 xfs_trans_unreserve_and_mod_sb(tp);
1058 xfs_trans_unreserve_and_mod_dquots(tp);
1059
1060 xfs_trans_free_items(tp, flags);
1061 xfs_trans_free_busy(tp);
1062 xfs_trans_free(tp);
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074STATIC void
1075xfs_trans_fill_vecs(
1076 xfs_trans_t *tp,
1077 xfs_log_iovec_t *log_vector)
1078{
1079 xfs_log_item_desc_t *lidp;
1080 xfs_log_iovec_t *vecp;
1081 uint nitems;
1082
1083
1084
1085
1086
1087 vecp = log_vector + 1;
1088
1089 nitems = 0;
1090 lidp = xfs_trans_first_item(tp);
1091 ASSERT(lidp != NULL);
1092 while (lidp != NULL) {
1093
1094
1095
1096 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1097 lidp = xfs_trans_next_item(tp, lidp);
1098 continue;
1099 }
1100
1101
1102
1103
1104
1105 if (lidp->lid_size) {
1106 nitems++;
1107 }
1108 IOP_FORMAT(lidp->lid_item, vecp);
1109 vecp += lidp->lid_size;
1110 IOP_PIN(lidp->lid_item);
1111 lidp = xfs_trans_next_item(tp, lidp);
1112 }
1113
1114
1115
1116
1117
1118 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1119 tp->t_header.th_type = tp->t_type;
1120 tp->t_header.th_num_items = nitems;
1121 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1122 log_vector->i_len = sizeof(xfs_trans_header_t);
1123 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR);
1124}
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135void
1136xfs_trans_cancel(
1137 xfs_trans_t *tp,
1138 int flags)
1139{
1140 int log_flags;
1141#ifdef DEBUG
1142 xfs_log_item_chunk_t *licp;
1143 xfs_log_item_desc_t *lidp;
1144 xfs_log_item_t *lip;
1145 int i;
1146#endif
1147 xfs_mount_t *mp = tp->t_mountp;
1148
1149
1150
1151
1152
1153 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1154 flags &= ~XFS_TRANS_ABORT;
1155
1156
1157
1158
1159
1160 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
1161 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1162 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1163 }
1164#ifdef DEBUG
1165 if (!(flags & XFS_TRANS_ABORT)) {
1166 licp = &(tp->t_items);
1167 while (licp != NULL) {
1168 lidp = licp->lic_descs;
1169 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1170 if (xfs_lic_isfree(licp, i)) {
1171 continue;
1172 }
1173
1174 lip = lidp->lid_item;
1175 if (!XFS_FORCED_SHUTDOWN(mp))
1176 ASSERT(!(lip->li_type == XFS_LI_EFD));
1177 }
1178 licp = licp->lic_next;
1179 }
1180 }
1181#endif
1182 xfs_trans_unreserve_and_mod_sb(tp);
1183 xfs_trans_unreserve_and_mod_dquots(tp);
1184
1185 if (tp->t_ticket) {
1186 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1187 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1188 log_flags = XFS_LOG_REL_PERM_RESERV;
1189 } else {
1190 log_flags = 0;
1191 }
1192 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1193 }
1194
1195
1196 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1197
1198 xfs_trans_free_items(tp, flags);
1199 xfs_trans_free_busy(tp);
1200 xfs_trans_free(tp);
1201}
1202
1203
1204
1205
1206
1207
1208STATIC void
1209xfs_trans_free(
1210 xfs_trans_t *tp)
1211{
1212 atomic_dec(&tp->t_mountp->m_active_trans);
1213 xfs_trans_free_dqinfo(tp);
1214 kmem_zone_free(xfs_trans_zone, tp);
1215}
1216
1217
1218
1219
1220
1221
1222
1223
1224int
1225xfs_trans_roll(
1226 struct xfs_trans **tpp,
1227 struct xfs_inode *dp)
1228{
1229 struct xfs_trans *trans;
1230 unsigned int logres, count;
1231 int error;
1232
1233
1234
1235
1236 trans = *tpp;
1237 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1238
1239
1240
1241
1242 logres = trans->t_log_res;
1243 count = trans->t_log_count;
1244 *tpp = xfs_trans_dup(trans);
1245
1246
1247
1248
1249
1250
1251
1252
1253 error = xfs_trans_commit(trans, 0);
1254 if (error)
1255 return (error);
1256
1257 trans = *tpp;
1258
1259
1260
1261
1262
1263 xfs_log_ticket_put(trans->t_ticket);
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274 error = xfs_trans_reserve(trans, 0, logres, 0,
1275 XFS_TRANS_PERM_LOG_RES, count);
1276
1277
1278
1279 if (error)
1280 return error;
1281
1282 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1283 xfs_trans_ihold(trans, dp);
1284 return 0;
1285}
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300STATIC void
1301xfs_trans_committed(
1302 xfs_trans_t *tp,
1303 int abortflag)
1304{
1305 xfs_log_item_chunk_t *licp;
1306 xfs_log_item_chunk_t *next_licp;
1307 xfs_log_busy_chunk_t *lbcp;
1308 xfs_log_busy_slot_t *lbsp;
1309 int i;
1310
1311
1312
1313
1314
1315 if (tp->t_callback != NULL) {
1316 tp->t_callback(tp, tp->t_callarg);
1317 }
1318
1319
1320
1321
1322 licp = &(tp->t_items);
1323 if (!(xfs_lic_are_all_free(licp))) {
1324 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1325 }
1326
1327
1328
1329
1330 licp = licp->lic_next;
1331 while (licp != NULL) {
1332 ASSERT(!xfs_lic_are_all_free(licp));
1333 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1334 next_licp = licp->lic_next;
1335 kmem_free(licp);
1336 licp = next_licp;
1337 }
1338
1339
1340
1341
1342 lbcp = &tp->t_busy;
1343 while (lbcp != NULL) {
1344 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1345 if (!XFS_LBC_ISFREE(lbcp, i)) {
1346 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1347 lbsp->lbc_idx);
1348 }
1349 }
1350 lbcp = lbcp->lbc_next;
1351 }
1352 xfs_trans_free_busy(tp);
1353
1354
1355
1356
1357 xfs_trans_free(tp);
1358}
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381STATIC void
1382xfs_trans_chunk_committed(
1383 xfs_log_item_chunk_t *licp,
1384 xfs_lsn_t lsn,
1385 int aborted)
1386{
1387 xfs_log_item_desc_t *lidp;
1388 xfs_log_item_t *lip;
1389 xfs_lsn_t item_lsn;
1390 int i;
1391
1392 lidp = licp->lic_descs;
1393 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1394 struct xfs_ail *ailp;
1395
1396 if (xfs_lic_isfree(licp, i)) {
1397 continue;
1398 }
1399
1400 lip = lidp->lid_item;
1401 if (aborted)
1402 lip->li_flags |= XFS_LI_ABORTED;
1403
1404
1405
1406
1407
1408
1409 item_lsn = IOP_COMMITTED(lip, lsn);
1410
1411
1412
1413
1414
1415 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1416 continue;
1417 }
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432 ailp = lip->li_ailp;
1433 spin_lock(&ailp->xa_lock);
1434 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1435
1436
1437
1438
1439
1440
1441
1442 xfs_trans_ail_update(ailp, lip, item_lsn);
1443 } else {
1444 spin_unlock(&ailp->xa_lock);
1445 }
1446
1447
1448
1449
1450
1451
1452
1453
1454 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
1455 }
1456}
1457