1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #ifndef __XFS_TRANS_H__
7*4882a593Smuzhiyun #define __XFS_TRANS_H__
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /* kernel only transaction subsystem defines */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun struct xfs_buf;
12*4882a593Smuzhiyun struct xfs_buftarg;
13*4882a593Smuzhiyun struct xfs_efd_log_item;
14*4882a593Smuzhiyun struct xfs_efi_log_item;
15*4882a593Smuzhiyun struct xfs_inode;
16*4882a593Smuzhiyun struct xfs_item_ops;
17*4882a593Smuzhiyun struct xfs_log_iovec;
18*4882a593Smuzhiyun struct xfs_mount;
19*4882a593Smuzhiyun struct xfs_trans;
20*4882a593Smuzhiyun struct xfs_trans_res;
21*4882a593Smuzhiyun struct xfs_dquot_acct;
22*4882a593Smuzhiyun struct xfs_rud_log_item;
23*4882a593Smuzhiyun struct xfs_rui_log_item;
24*4882a593Smuzhiyun struct xfs_btree_cur;
25*4882a593Smuzhiyun struct xfs_cui_log_item;
26*4882a593Smuzhiyun struct xfs_cud_log_item;
27*4882a593Smuzhiyun struct xfs_bui_log_item;
28*4882a593Smuzhiyun struct xfs_bud_log_item;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun struct xfs_log_item {
31*4882a593Smuzhiyun struct list_head li_ail; /* AIL pointers */
32*4882a593Smuzhiyun struct list_head li_trans; /* transaction list */
33*4882a593Smuzhiyun xfs_lsn_t li_lsn; /* last on-disk lsn */
34*4882a593Smuzhiyun struct xfs_mount *li_mountp; /* ptr to fs mount */
35*4882a593Smuzhiyun struct xfs_ail *li_ailp; /* ptr to AIL */
36*4882a593Smuzhiyun uint li_type; /* item type */
37*4882a593Smuzhiyun unsigned long li_flags; /* misc flags */
38*4882a593Smuzhiyun struct xfs_buf *li_buf; /* real buffer pointer */
39*4882a593Smuzhiyun struct list_head li_bio_list; /* buffer item list */
40*4882a593Smuzhiyun const struct xfs_item_ops *li_ops; /* function list */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* delayed logging */
43*4882a593Smuzhiyun struct list_head li_cil; /* CIL pointers */
44*4882a593Smuzhiyun struct xfs_log_vec *li_lv; /* active log vector */
45*4882a593Smuzhiyun struct xfs_log_vec *li_lv_shadow; /* standby vector */
46*4882a593Smuzhiyun xfs_csn_t li_seq; /* CIL commit seq */
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * li_flags use the (set/test/clear)_bit atomic interfaces because updates can
51*4882a593Smuzhiyun * race with each other and we don't want to have to use the AIL lock to
52*4882a593Smuzhiyun * serialise all updates.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun #define XFS_LI_IN_AIL 0
55*4882a593Smuzhiyun #define XFS_LI_ABORTED 1
56*4882a593Smuzhiyun #define XFS_LI_FAILED 2
57*4882a593Smuzhiyun #define XFS_LI_DIRTY 3 /* log item dirty in transaction */
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #define XFS_LI_FLAGS \
60*4882a593Smuzhiyun { (1 << XFS_LI_IN_AIL), "IN_AIL" }, \
61*4882a593Smuzhiyun { (1 << XFS_LI_ABORTED), "ABORTED" }, \
62*4882a593Smuzhiyun { (1 << XFS_LI_FAILED), "FAILED" }, \
63*4882a593Smuzhiyun { (1 << XFS_LI_DIRTY), "DIRTY" }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun struct xfs_item_ops {
66*4882a593Smuzhiyun unsigned flags;
67*4882a593Smuzhiyun void (*iop_size)(struct xfs_log_item *, int *, int *);
68*4882a593Smuzhiyun void (*iop_format)(struct xfs_log_item *, struct xfs_log_vec *);
69*4882a593Smuzhiyun void (*iop_pin)(struct xfs_log_item *);
70*4882a593Smuzhiyun void (*iop_unpin)(struct xfs_log_item *, int remove);
71*4882a593Smuzhiyun uint (*iop_push)(struct xfs_log_item *, struct list_head *);
72*4882a593Smuzhiyun void (*iop_committing)(struct xfs_log_item *lip, xfs_csn_t seq);
73*4882a593Smuzhiyun void (*iop_release)(struct xfs_log_item *);
74*4882a593Smuzhiyun xfs_lsn_t (*iop_committed)(struct xfs_log_item *, xfs_lsn_t);
75*4882a593Smuzhiyun int (*iop_recover)(struct xfs_log_item *lip,
76*4882a593Smuzhiyun struct list_head *capture_list);
77*4882a593Smuzhiyun bool (*iop_match)(struct xfs_log_item *item, uint64_t id);
78*4882a593Smuzhiyun struct xfs_log_item *(*iop_relog)(struct xfs_log_item *intent,
79*4882a593Smuzhiyun struct xfs_trans *tp);
80*4882a593Smuzhiyun };
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* Is this log item a deferred action intent? */
83*4882a593Smuzhiyun static inline bool
xlog_item_is_intent(struct xfs_log_item * lip)84*4882a593Smuzhiyun xlog_item_is_intent(struct xfs_log_item *lip)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun return lip->li_ops->iop_recover != NULL &&
87*4882a593Smuzhiyun lip->li_ops->iop_match != NULL;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Is this a log intent-done item? */
91*4882a593Smuzhiyun static inline bool
xlog_item_is_intent_done(struct xfs_log_item * lip)92*4882a593Smuzhiyun xlog_item_is_intent_done(struct xfs_log_item *lip)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun return lip->li_ops->iop_unpin == NULL &&
95*4882a593Smuzhiyun lip->li_ops->iop_push == NULL;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * Release the log item as soon as committed. This is for items just logging
100*4882a593Smuzhiyun * intents that never need to be written back in place.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun #define XFS_ITEM_RELEASE_WHEN_COMMITTED (1 << 0)
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun void xfs_log_item_init(struct xfs_mount *mp, struct xfs_log_item *item,
105*4882a593Smuzhiyun int type, const struct xfs_item_ops *ops);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun * Return values for the iop_push() routines.
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun #define XFS_ITEM_SUCCESS 0
111*4882a593Smuzhiyun #define XFS_ITEM_PINNED 1
112*4882a593Smuzhiyun #define XFS_ITEM_LOCKED 2
113*4882a593Smuzhiyun #define XFS_ITEM_FLUSHING 3
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun * Deferred operation item relogging limits.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun #define XFS_DEFER_OPS_NR_INODES 2 /* join up to two inodes */
119*4882a593Smuzhiyun #define XFS_DEFER_OPS_NR_BUFS 2 /* join up to two buffers */
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * This is the structure maintained for every active transaction.
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun typedef struct xfs_trans {
125*4882a593Smuzhiyun unsigned int t_magic; /* magic number */
126*4882a593Smuzhiyun unsigned int t_log_res; /* amt of log space resvd */
127*4882a593Smuzhiyun unsigned int t_log_count; /* count for perm log res */
128*4882a593Smuzhiyun unsigned int t_blk_res; /* # of blocks resvd */
129*4882a593Smuzhiyun unsigned int t_blk_res_used; /* # of resvd blocks used */
130*4882a593Smuzhiyun unsigned int t_rtx_res; /* # of rt extents resvd */
131*4882a593Smuzhiyun unsigned int t_rtx_res_used; /* # of resvd rt extents used */
132*4882a593Smuzhiyun unsigned int t_flags; /* misc flags */
133*4882a593Smuzhiyun xfs_fsblock_t t_firstblock; /* first block allocated */
134*4882a593Smuzhiyun struct xlog_ticket *t_ticket; /* log mgr ticket */
135*4882a593Smuzhiyun struct xfs_mount *t_mountp; /* ptr to fs mount struct */
136*4882a593Smuzhiyun struct xfs_dquot_acct *t_dqinfo; /* acctg info for dquots */
137*4882a593Smuzhiyun int64_t t_icount_delta; /* superblock icount change */
138*4882a593Smuzhiyun int64_t t_ifree_delta; /* superblock ifree change */
139*4882a593Smuzhiyun int64_t t_fdblocks_delta; /* superblock fdblocks chg */
140*4882a593Smuzhiyun int64_t t_res_fdblocks_delta; /* on-disk only chg */
141*4882a593Smuzhiyun int64_t t_frextents_delta;/* superblock freextents chg*/
142*4882a593Smuzhiyun int64_t t_res_frextents_delta; /* on-disk only chg */
143*4882a593Smuzhiyun #if defined(DEBUG) || defined(XFS_WARN)
144*4882a593Smuzhiyun int64_t t_ag_freeblks_delta; /* debugging counter */
145*4882a593Smuzhiyun int64_t t_ag_flist_delta; /* debugging counter */
146*4882a593Smuzhiyun int64_t t_ag_btree_delta; /* debugging counter */
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun int64_t t_dblocks_delta;/* superblock dblocks change */
149*4882a593Smuzhiyun int64_t t_agcount_delta;/* superblock agcount change */
150*4882a593Smuzhiyun int64_t t_imaxpct_delta;/* superblock imaxpct change */
151*4882a593Smuzhiyun int64_t t_rextsize_delta;/* superblock rextsize chg */
152*4882a593Smuzhiyun int64_t t_rbmblocks_delta;/* superblock rbmblocks chg */
153*4882a593Smuzhiyun int64_t t_rblocks_delta;/* superblock rblocks change */
154*4882a593Smuzhiyun int64_t t_rextents_delta;/* superblocks rextents chg */
155*4882a593Smuzhiyun int64_t t_rextslog_delta;/* superblocks rextslog chg */
156*4882a593Smuzhiyun struct list_head t_items; /* log item descriptors */
157*4882a593Smuzhiyun struct list_head t_busy; /* list of busy extents */
158*4882a593Smuzhiyun struct list_head t_dfops; /* deferred operations */
159*4882a593Smuzhiyun unsigned long t_pflags; /* saved process flags state */
160*4882a593Smuzhiyun } xfs_trans_t;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun * XFS transaction mechanism exported interfaces that are
164*4882a593Smuzhiyun * actually macros.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun #define xfs_trans_set_sync(tp) ((tp)->t_flags |= XFS_TRANS_SYNC)
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun #if defined(DEBUG) || defined(XFS_WARN)
169*4882a593Smuzhiyun #define xfs_trans_agblocks_delta(tp, d) ((tp)->t_ag_freeblks_delta += (int64_t)d)
170*4882a593Smuzhiyun #define xfs_trans_agflist_delta(tp, d) ((tp)->t_ag_flist_delta += (int64_t)d)
171*4882a593Smuzhiyun #define xfs_trans_agbtree_delta(tp, d) ((tp)->t_ag_btree_delta += (int64_t)d)
172*4882a593Smuzhiyun #else
173*4882a593Smuzhiyun #define xfs_trans_agblocks_delta(tp, d)
174*4882a593Smuzhiyun #define xfs_trans_agflist_delta(tp, d)
175*4882a593Smuzhiyun #define xfs_trans_agbtree_delta(tp, d)
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun * XFS transaction mechanism exported interfaces.
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun int xfs_trans_alloc(struct xfs_mount *mp, struct xfs_trans_res *resp,
182*4882a593Smuzhiyun uint blocks, uint rtextents, uint flags,
183*4882a593Smuzhiyun struct xfs_trans **tpp);
184*4882a593Smuzhiyun int xfs_trans_alloc_empty(struct xfs_mount *mp,
185*4882a593Smuzhiyun struct xfs_trans **tpp);
186*4882a593Smuzhiyun void xfs_trans_mod_sb(xfs_trans_t *, uint, int64_t);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun int xfs_trans_get_buf_map(struct xfs_trans *tp, struct xfs_buftarg *target,
189*4882a593Smuzhiyun struct xfs_buf_map *map, int nmaps, xfs_buf_flags_t flags,
190*4882a593Smuzhiyun struct xfs_buf **bpp);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun static inline int
xfs_trans_get_buf(struct xfs_trans * tp,struct xfs_buftarg * target,xfs_daddr_t blkno,int numblks,uint flags,struct xfs_buf ** bpp)193*4882a593Smuzhiyun xfs_trans_get_buf(
194*4882a593Smuzhiyun struct xfs_trans *tp,
195*4882a593Smuzhiyun struct xfs_buftarg *target,
196*4882a593Smuzhiyun xfs_daddr_t blkno,
197*4882a593Smuzhiyun int numblks,
198*4882a593Smuzhiyun uint flags,
199*4882a593Smuzhiyun struct xfs_buf **bpp)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
202*4882a593Smuzhiyun return xfs_trans_get_buf_map(tp, target, &map, 1, flags, bpp);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun int xfs_trans_read_buf_map(struct xfs_mount *mp,
206*4882a593Smuzhiyun struct xfs_trans *tp,
207*4882a593Smuzhiyun struct xfs_buftarg *target,
208*4882a593Smuzhiyun struct xfs_buf_map *map, int nmaps,
209*4882a593Smuzhiyun xfs_buf_flags_t flags,
210*4882a593Smuzhiyun struct xfs_buf **bpp,
211*4882a593Smuzhiyun const struct xfs_buf_ops *ops);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun static inline int
xfs_trans_read_buf(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_buftarg * target,xfs_daddr_t blkno,int numblks,xfs_buf_flags_t flags,struct xfs_buf ** bpp,const struct xfs_buf_ops * ops)214*4882a593Smuzhiyun xfs_trans_read_buf(
215*4882a593Smuzhiyun struct xfs_mount *mp,
216*4882a593Smuzhiyun struct xfs_trans *tp,
217*4882a593Smuzhiyun struct xfs_buftarg *target,
218*4882a593Smuzhiyun xfs_daddr_t blkno,
219*4882a593Smuzhiyun int numblks,
220*4882a593Smuzhiyun xfs_buf_flags_t flags,
221*4882a593Smuzhiyun struct xfs_buf **bpp,
222*4882a593Smuzhiyun const struct xfs_buf_ops *ops)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun DEFINE_SINGLE_BUF_MAP(map, blkno, numblks);
225*4882a593Smuzhiyun return xfs_trans_read_buf_map(mp, tp, target, &map, 1,
226*4882a593Smuzhiyun flags, bpp, ops);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun struct xfs_buf *xfs_trans_getsb(struct xfs_trans *);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun void xfs_trans_brelse(xfs_trans_t *, struct xfs_buf *);
232*4882a593Smuzhiyun void xfs_trans_bjoin(xfs_trans_t *, struct xfs_buf *);
233*4882a593Smuzhiyun void xfs_trans_bhold(xfs_trans_t *, struct xfs_buf *);
234*4882a593Smuzhiyun void xfs_trans_bhold_release(xfs_trans_t *, struct xfs_buf *);
235*4882a593Smuzhiyun void xfs_trans_binval(xfs_trans_t *, struct xfs_buf *);
236*4882a593Smuzhiyun void xfs_trans_inode_buf(xfs_trans_t *, struct xfs_buf *);
237*4882a593Smuzhiyun void xfs_trans_stale_inode_buf(xfs_trans_t *, struct xfs_buf *);
238*4882a593Smuzhiyun bool xfs_trans_ordered_buf(xfs_trans_t *, struct xfs_buf *);
239*4882a593Smuzhiyun void xfs_trans_dquot_buf(xfs_trans_t *, struct xfs_buf *, uint);
240*4882a593Smuzhiyun void xfs_trans_inode_alloc_buf(xfs_trans_t *, struct xfs_buf *);
241*4882a593Smuzhiyun void xfs_trans_ichgtime(struct xfs_trans *, struct xfs_inode *, int);
242*4882a593Smuzhiyun void xfs_trans_ijoin(struct xfs_trans *, struct xfs_inode *, uint);
243*4882a593Smuzhiyun void xfs_trans_log_buf(struct xfs_trans *, struct xfs_buf *, uint,
244*4882a593Smuzhiyun uint);
245*4882a593Smuzhiyun void xfs_trans_dirty_buf(struct xfs_trans *, struct xfs_buf *);
246*4882a593Smuzhiyun bool xfs_trans_buf_is_dirty(struct xfs_buf *bp);
247*4882a593Smuzhiyun void xfs_trans_log_inode(xfs_trans_t *, struct xfs_inode *, uint);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun int xfs_trans_commit(struct xfs_trans *);
250*4882a593Smuzhiyun int xfs_trans_roll(struct xfs_trans **);
251*4882a593Smuzhiyun int xfs_trans_roll_inode(struct xfs_trans **, struct xfs_inode *);
252*4882a593Smuzhiyun void xfs_trans_cancel(xfs_trans_t *);
253*4882a593Smuzhiyun int xfs_trans_ail_init(struct xfs_mount *);
254*4882a593Smuzhiyun void xfs_trans_ail_destroy(struct xfs_mount *);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun void xfs_trans_buf_set_type(struct xfs_trans *, struct xfs_buf *,
257*4882a593Smuzhiyun enum xfs_blft);
258*4882a593Smuzhiyun void xfs_trans_buf_copy_type(struct xfs_buf *dst_bp,
259*4882a593Smuzhiyun struct xfs_buf *src_bp);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun extern kmem_zone_t *xfs_trans_zone;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun static inline struct xfs_log_item *
xfs_trans_item_relog(struct xfs_log_item * lip,struct xfs_trans * tp)264*4882a593Smuzhiyun xfs_trans_item_relog(
265*4882a593Smuzhiyun struct xfs_log_item *lip,
266*4882a593Smuzhiyun struct xfs_trans *tp)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun return lip->li_ops->iop_relog(lip, tp);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun static inline void
xfs_trans_set_context(struct xfs_trans * tp)272*4882a593Smuzhiyun xfs_trans_set_context(
273*4882a593Smuzhiyun struct xfs_trans *tp)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun ASSERT(current->journal_info == NULL);
276*4882a593Smuzhiyun tp->t_pflags = memalloc_nofs_save();
277*4882a593Smuzhiyun current->journal_info = tp;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun static inline void
xfs_trans_clear_context(struct xfs_trans * tp)281*4882a593Smuzhiyun xfs_trans_clear_context(
282*4882a593Smuzhiyun struct xfs_trans *tp)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun if (current->journal_info == tp) {
285*4882a593Smuzhiyun memalloc_nofs_restore(tp->t_pflags);
286*4882a593Smuzhiyun current->journal_info = NULL;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun static inline void
xfs_trans_switch_context(struct xfs_trans * old_tp,struct xfs_trans * new_tp)291*4882a593Smuzhiyun xfs_trans_switch_context(
292*4882a593Smuzhiyun struct xfs_trans *old_tp,
293*4882a593Smuzhiyun struct xfs_trans *new_tp)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun ASSERT(current->journal_info == old_tp);
296*4882a593Smuzhiyun new_tp->t_pflags = old_tp->t_pflags;
297*4882a593Smuzhiyun old_tp->t_pflags = 0;
298*4882a593Smuzhiyun current->journal_info = new_tp;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun #endif /* __XFS_TRANS_H__ */
302