xref: /linux/fs/nfs/flexfilelayout/flexfilelayout.h (revision 1d62e659c0bf11649cf48e002c2a55d148f2610a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * NFSv4 flexfile layout driver data structures.
4  *
5  * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
6  *
7  * Tao Peng <bergwolf@primarydata.com>
8  */
9 
10 #ifndef FS_NFS_NFS4FLEXFILELAYOUT_H
11 #define FS_NFS_NFS4FLEXFILELAYOUT_H
12 
13 #define FF_FLAGS_NO_LAYOUTCOMMIT 1
14 #define FF_FLAGS_NO_IO_THRU_MDS  2
15 #define FF_FLAGS_NO_READ_IO      4
16 
17 #include <linux/refcount.h>
18 #include "../pnfs.h"
19 
20 /* XXX: Let's filter out insanely large mirror count for now to avoid oom
21  * due to network error etc. */
22 #define NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT 4096
23 
24 #define NFS4_FLEXFILE_LAYOUT_MAX_STRIPE_CNT 4096
25 
26 /* LAYOUTSTATS report interval in ms */
27 #define FF_LAYOUTSTATS_REPORT_INTERVAL (60000L)
28 #define FF_LAYOUTSTATS_MAXDEV 4
29 
30 struct nfs4_ff_ds_version {
31 	u32				version;
32 	u32				minor_version;
33 	u32				rsize;
34 	u32				wsize;
35 	bool				tightly_coupled;
36 };
37 
38 /* chained in global deviceid hlist */
39 struct nfs4_ff_layout_ds {
40 	struct nfs4_deviceid_node	id_node;
41 	u32				ds_versions_cnt;
42 	struct nfs4_ff_ds_version	*ds_versions;
43 	struct nfs4_pnfs_ds		*ds;
44 };
45 
46 struct nfs4_ff_layout_ds_err {
47 	struct list_head		list; /* linked in mirror error_list */
48 	u64				offset;
49 	u64				length;
50 	int				status;
51 	enum nfs_opnum4			opnum;
52 	nfs4_stateid			stateid;
53 	struct nfs4_deviceid		deviceid;
54 };
55 
56 struct nfs4_ff_io_stat {
57 	__u64				ops_requested;
58 	__u64				bytes_requested;
59 	__u64				ops_completed;
60 	__u64				bytes_completed;
61 	__u64				bytes_not_delivered;
62 	ktime_t				total_busy_time;
63 	ktime_t				aggregate_completion_time;
64 };
65 
66 struct nfs4_ff_busy_timer {
67 	ktime_t start_time;
68 	atomic_t n_ops;
69 };
70 
71 struct nfs4_ff_layoutstat {
72 	struct nfs4_ff_io_stat io_stat;
73 	struct nfs4_ff_busy_timer busy_timer;
74 };
75 
76 struct nfs4_ff_layout_mirror;
77 
78 struct nfs4_ff_layout_ds_stripe {
79 	struct nfs4_ff_layout_mirror   *mirror;
80 	struct nfs4_deviceid		devid;
81 	u32				efficiency;
82 	struct nfs4_ff_layout_ds	*mirror_ds;
83 	u32				fh_versions_cnt;
84 	struct nfs_fh			*fh_versions;
85 	nfs4_stateid			stateid;
86 	const struct cred __rcu		*ro_cred;
87 	const struct cred __rcu		*rw_cred;
88 	struct nfs_file_localio		nfl;
89 	struct nfs4_ff_layoutstat	read_stat;
90 	struct nfs4_ff_layoutstat	write_stat;
91 	ktime_t				start_time;
92 };
93 
94 struct nfs4_ff_layout_mirror {
95 	struct pnfs_layout_hdr		*layout;
96 	struct list_head		mirrors;
97 	u32				dss_count;
98 	struct nfs4_ff_layout_ds_stripe *dss;
99 	refcount_t			ref;
100 	spinlock_t			lock;
101 	unsigned long			flags;
102 	u32				report_interval;
103 };
104 
105 #define NFS4_FF_MIRROR_STAT_AVAIL	(0)
106 
107 struct nfs4_ff_layout_segment {
108 	struct pnfs_layout_segment	generic_hdr;
109 	u64				stripe_unit;
110 	u32				flags;
111 	u32				mirror_array_cnt;
112 	struct nfs4_ff_layout_mirror	*mirror_array[] __counted_by(mirror_array_cnt);
113 };
114 
115 /* nfs4_flexfile_layout::flags bit indices */
116 #define NFS4_FF_HDR_NO_IO_THRU_MDS  0   /* any lseg has had FF_FLAGS_NO_IO_THRU_MDS */
117 
118 struct nfs4_flexfile_layout {
119 	struct pnfs_layout_hdr generic_hdr;
120 	struct pnfs_ds_commit_info commit_info;
121 	struct list_head	mirrors;
122 	struct list_head	error_list; /* nfs4_ff_layout_ds_err */
123 	ktime_t			last_report_time; /* Layoutstat report times */
124 	unsigned long		flags;
125 };
126 
127 struct nfs4_flexfile_layoutreturn_args {
128 	struct list_head errors;
129 	struct nfs42_layoutstat_devinfo devinfo[FF_LAYOUTSTATS_MAXDEV];
130 	unsigned int num_errors;
131 	unsigned int num_dev;
132 	struct page *pages[1];
133 };
134 
135 static inline struct nfs4_flexfile_layout *
136 FF_LAYOUT_FROM_HDR(struct pnfs_layout_hdr *lo)
137 {
138 	return container_of(lo, struct nfs4_flexfile_layout, generic_hdr);
139 }
140 
141 static inline struct nfs4_ff_layout_segment *
142 FF_LAYOUT_LSEG(struct pnfs_layout_segment *lseg)
143 {
144 	return container_of(lseg,
145 			    struct nfs4_ff_layout_segment,
146 			    generic_hdr);
147 }
148 
149 static inline struct nfs4_ff_layout_ds *
150 FF_LAYOUT_MIRROR_DS(struct nfs4_deviceid_node *node)
151 {
152 	return container_of(node, struct nfs4_ff_layout_ds, id_node);
153 }
154 
155 static inline struct nfs4_ff_layout_mirror *
156 FF_LAYOUT_COMP(struct pnfs_layout_segment *lseg, u32 idx)
157 {
158 	struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
159 
160 	if (idx < fls->mirror_array_cnt)
161 		return fls->mirror_array[idx];
162 	return NULL;
163 }
164 
165 static inline struct nfs4_deviceid_node *
166 FF_LAYOUT_DEVID_NODE(struct pnfs_layout_segment *lseg, u32 idx, u32 dss_id)
167 {
168 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, idx);
169 
170 	if (mirror != NULL) {
171 		struct nfs4_ff_layout_ds *mirror_ds = mirror->dss[dss_id].mirror_ds;
172 
173 		if (!IS_ERR_OR_NULL(mirror_ds))
174 			return &mirror_ds->id_node;
175 	}
176 	return NULL;
177 }
178 
179 static inline u32
180 FF_LAYOUT_MIRROR_COUNT(struct pnfs_layout_segment *lseg)
181 {
182 	return FF_LAYOUT_LSEG(lseg)->mirror_array_cnt;
183 }
184 
185 static inline bool
186 ff_layout_no_fallback_to_mds(struct pnfs_layout_segment *lseg)
187 {
188 	return FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_IO_THRU_MDS;
189 }
190 
191 /*
192  * Sticky hdr-level mirror of FF_FLAGS_NO_IO_THRU_MDS so callers that have
193  * no current lseg (e.g. between LAYOUTRETURN and the next LAYOUTGET) can
194  * still honor the no-MDS-fallback policy.
195  */
196 static inline bool
197 ff_layout_hdr_no_fallback_to_mds(struct pnfs_layout_hdr *lo)
198 {
199 	return test_bit(NFS4_FF_HDR_NO_IO_THRU_MDS,
200 			&FF_LAYOUT_FROM_HDR(lo)->flags);
201 }
202 
203 static inline bool
204 ff_layout_no_read_on_rw(struct pnfs_layout_segment *lseg)
205 {
206 	return FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_READ_IO;
207 }
208 
209 static inline int
210 nfs4_ff_layout_ds_version(const struct nfs4_ff_layout_mirror *mirror, u32 dss_id)
211 {
212 	return mirror->dss[dss_id].mirror_ds->ds_versions[0].version;
213 }
214 
215 static inline u32
216 nfs4_ff_layout_calc_dss_id(const u64 stripe_unit, const u32 dss_count, const loff_t offset)
217 {
218 	u64 tmp = offset;
219 
220 	if (dss_count == 1 || stripe_unit == 0)
221 		return 0;
222 
223 	do_div(tmp, stripe_unit);
224 
225 	return do_div(tmp, dss_count);
226 }
227 
228 struct nfs4_ff_layout_ds *
229 nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
230 			    gfp_t gfp_flags);
231 void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
232 void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
233 int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo,
234 			     struct nfs4_ff_layout_mirror *mirror,
235 			     u32 dss_id, u64 offset, u64 length, int status,
236 			     enum nfs_opnum4 opnum, gfp_t gfp_flags);
237 void ff_layout_send_layouterror(struct pnfs_layout_segment *lseg);
238 int ff_layout_encode_ds_ioerr(struct xdr_stream *xdr, const struct list_head *head);
239 void ff_layout_free_ds_ioerr(struct list_head *head);
240 unsigned int ff_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo,
241 		const struct pnfs_layout_range *range,
242 		struct list_head *head,
243 		unsigned int maxnum);
244 struct nfs_fh *
245 nfs4_ff_layout_select_ds_fh(struct nfs4_ff_layout_mirror *mirror, u32 dss_id);
246 void
247 nfs4_ff_layout_select_ds_stateid(const struct nfs4_ff_layout_mirror *mirror,
248 				 u32 dss_id,
249 				 nfs4_stateid *stateid);
250 
251 struct nfs4_pnfs_ds *
252 nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg,
253 			  struct nfs4_ff_layout_mirror *mirror,
254 			  u32 dss_id,
255 			  bool fail_return);
256 
257 struct rpc_clnt *
258 nfs4_ff_find_or_create_ds_client(struct nfs4_ff_layout_mirror *mirror,
259 				 struct nfs_client *ds_clp,
260 				 struct inode *inode,
261 				 u32 dss_id);
262 const struct cred *ff_layout_get_ds_cred(struct nfs4_ff_layout_mirror *mirror,
263 					 const struct pnfs_layout_range *range,
264 					 const struct cred *mdscred,
265 					 u32 dss_id);
266 bool ff_layout_avoid_mds_available_ds(struct pnfs_layout_segment *lseg);
267 bool ff_layout_avoid_read_on_rw(struct pnfs_layout_segment *lseg);
268 
269 #endif /* FS_NFS_NFS4FLEXFILELAYOUT_H */
270