xref: /linux/fs/xfs/libxfs/xfs_defer.h (revision 79d2e1919a2728ef49d938eb20ebd5903c14dfb0)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2016 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #ifndef __XFS_DEFER_H__
7 #define	__XFS_DEFER_H__
8 
9 struct xfs_btree_cur;
10 struct xfs_defer_op_type;
11 struct xfs_defer_capture;
12 
13 /*
14  * Save a log intent item and a list of extents, so that we can replay
15  * whatever action had to happen to the extent list and file the log done
16  * item.
17  */
18 struct xfs_defer_pending {
19 	struct list_head		dfp_list;	/* pending items */
20 	struct list_head		dfp_work;	/* work items */
21 	struct xfs_log_item		*dfp_intent;	/* log intent item */
22 	struct xfs_log_item		*dfp_done;	/* log done item */
23 	const struct xfs_defer_op_type	*dfp_ops;
24 	unsigned int			dfp_count;	/* # extent items */
25 	unsigned int			dfp_flags;
26 };
27 
28 /*
29  * Create a log intent item for this deferred item, but don't actually finish
30  * the work.  Caller must clear this before the final transaction commit.
31  */
32 #define XFS_DEFER_PAUSED	(1U << 0)
33 
34 #define XFS_DEFER_PENDING_STRINGS \
35 	{ XFS_DEFER_PAUSED,	"paused" }
36 
37 void xfs_defer_item_pause(struct xfs_trans *tp, struct xfs_defer_pending *dfp);
38 void xfs_defer_item_unpause(struct xfs_trans *tp, struct xfs_defer_pending *dfp);
39 
40 struct xfs_defer_pending *xfs_defer_add(struct xfs_trans *tp, struct list_head *h,
41 		const struct xfs_defer_op_type *ops);
42 int xfs_defer_finish_noroll(struct xfs_trans **tp);
43 int xfs_defer_finish(struct xfs_trans **tp);
44 int xfs_defer_finish_one(struct xfs_trans *tp, struct xfs_defer_pending *dfp);
45 void xfs_defer_cancel(struct xfs_trans *);
46 void xfs_defer_move(struct xfs_trans *dtp, struct xfs_trans *stp);
47 
48 /* Description of a deferred type. */
49 struct xfs_defer_op_type {
50 	const char		*name;
51 	unsigned int		max_items;
52 	struct xfs_log_item *(*create_intent)(struct xfs_trans *tp,
53 			struct list_head *items, unsigned int count, bool sort);
54 	void (*abort_intent)(struct xfs_log_item *intent);
55 	struct xfs_log_item *(*create_done)(struct xfs_trans *tp,
56 			struct xfs_log_item *intent, unsigned int count);
57 	int (*finish_item)(struct xfs_trans *tp, struct xfs_log_item *done,
58 			struct list_head *item, struct xfs_btree_cur **state);
59 	void (*finish_cleanup)(struct xfs_trans *tp,
60 			struct xfs_btree_cur *state, int error);
61 	void (*cancel_item)(struct list_head *item);
62 	int (*recover_work)(struct xfs_defer_pending *dfp,
63 			    struct list_head *capture_list);
64 	struct xfs_log_item *(*relog_intent)(struct xfs_trans *tp,
65 			struct xfs_log_item *intent,
66 			struct xfs_log_item *done_item);
67 };
68 
69 extern const struct xfs_defer_op_type xfs_bmap_update_defer_type;
70 extern const struct xfs_defer_op_type xfs_refcount_update_defer_type;
71 extern const struct xfs_defer_op_type xfs_rmap_update_defer_type;
72 extern const struct xfs_defer_op_type xfs_extent_free_defer_type;
73 extern const struct xfs_defer_op_type xfs_agfl_free_defer_type;
74 extern const struct xfs_defer_op_type xfs_rtextent_free_defer_type;
75 extern const struct xfs_defer_op_type xfs_attr_defer_type;
76 extern const struct xfs_defer_op_type xfs_exchmaps_defer_type;
77 
78 /*
79  * Deferred operation item relogging limits.
80  */
81 
82 /*
83  * Rename w/ parent pointers can require up to 5 inodes with deferred ops to
84  * be joined to the transaction: src_dp, target_dp, src_ip, target_ip, and wip.
85  * These inodes are locked in sorted order by their inode numbers
86  */
87 #define XFS_DEFER_OPS_NR_INODES	5
88 #define XFS_DEFER_OPS_NR_BUFS	2	/* join up to two buffers */
89 
90 /* Resources that must be held across a transaction roll. */
91 struct xfs_defer_resources {
92 	/* held buffers */
93 	struct xfs_buf		*dr_bp[XFS_DEFER_OPS_NR_BUFS];
94 
95 	/* inodes with no unlock flags */
96 	struct xfs_inode	*dr_ip[XFS_DEFER_OPS_NR_INODES];
97 
98 	/* number of held buffers */
99 	unsigned short		dr_bufs;
100 
101 	/* bitmap of ordered buffers */
102 	unsigned short		dr_ordered;
103 
104 	/* number of held inodes */
105 	unsigned short		dr_inos;
106 };
107 
108 /*
109  * This structure enables a dfops user to detach the chain of deferred
110  * operations from a transaction so that they can be continued later.
111  */
112 struct xfs_defer_capture {
113 	/* List of other capture structures. */
114 	struct list_head	dfc_list;
115 
116 	/* Deferred ops state saved from the transaction. */
117 	struct list_head	dfc_dfops;
118 	unsigned int		dfc_tpflags;
119 
120 	/* Block reservations for the data and rt devices. */
121 	unsigned int		dfc_blkres;
122 	unsigned int		dfc_rtxres;
123 
124 	/* Log reservation saved from the transaction. */
125 	unsigned int		dfc_logres;
126 
127 	struct xfs_defer_resources dfc_held;
128 };
129 
130 /*
131  * Functions to capture a chain of deferred operations and continue them later.
132  * This doesn't normally happen except log recovery.
133  */
134 int xfs_defer_ops_capture_and_commit(struct xfs_trans *tp,
135 		struct list_head *capture_list);
136 void xfs_defer_ops_continue(struct xfs_defer_capture *d, struct xfs_trans *tp,
137 		struct xfs_defer_resources *dres);
138 void xfs_defer_ops_capture_abort(struct xfs_mount *mp,
139 		struct xfs_defer_capture *d);
140 void xfs_defer_resources_rele(struct xfs_defer_resources *dres);
141 
142 void xfs_defer_start_recovery(struct xfs_log_item *lip,
143 		struct list_head *r_dfops, const struct xfs_defer_op_type *ops);
144 void xfs_defer_cancel_recovery(struct xfs_mount *mp,
145 		struct xfs_defer_pending *dfp);
146 int xfs_defer_finish_recovery(struct xfs_mount *mp,
147 		struct xfs_defer_pending *dfp, struct list_head *capture_list);
148 
149 static inline void
150 xfs_defer_add_item(
151 	struct xfs_defer_pending	*dfp,
152 	struct list_head		*work)
153 {
154 	list_add_tail(work, &dfp->dfp_work);
155 	dfp->dfp_count++;
156 }
157 
158 int __init xfs_defer_init_item_caches(void);
159 void xfs_defer_destroy_item_caches(void);
160 
161 void xfs_defer_add_barrier(struct xfs_trans *tp);
162 
163 #endif /* __XFS_DEFER_H__ */
164