xref: /linux/fs/nfs/flexfilelayout/flexfilelayout.h (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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 __rcu	*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 	unsigned long			*cancelled_stripes;
113 	struct nfs4_ff_layout_mirror	*mirror_array[] __counted_by(mirror_array_cnt);
114 };
115 
116 /* nfs4_flexfile_layout::flags bit indices */
117 #define NFS4_FF_HDR_NO_IO_THRU_MDS  0   /* any lseg has had FF_FLAGS_NO_IO_THRU_MDS */
118 
119 struct nfs4_flexfile_layout {
120 	struct pnfs_layout_hdr generic_hdr;
121 	struct pnfs_ds_commit_info commit_info;
122 	struct list_head	mirrors;
123 	struct list_head	error_list; /* nfs4_ff_layout_ds_err */
124 	ktime_t			last_report_time; /* Layoutstat report times */
125 	unsigned long		flags;
126 };
127 
128 /*
129  * Per-devinfo private data for a layoutstats/layoutreturn encode: the
130  * stripe the stats describe plus a reference on its device node so the
131  * node (and its DS addresses) stay valid until the XDR encode runs.
132  */
133 struct nfs4_ff_layoutstat_priv {
134 	struct nfs4_ff_layout_ds_stripe *dss_info;
135 	struct nfs4_ff_layout_ds *mirror_ds;
136 };
137 
138 struct nfs4_flexfile_layoutreturn_args {
139 	struct list_head errors;
140 	struct nfs42_layoutstat_devinfo devinfo[FF_LAYOUTSTATS_MAXDEV];
141 	struct nfs4_ff_layoutstat_priv priv[FF_LAYOUTSTATS_MAXDEV];
142 	unsigned int num_errors;
143 	unsigned int num_dev;
144 	struct page *pages[1];
145 };
146 
147 static inline struct nfs4_flexfile_layout *
148 FF_LAYOUT_FROM_HDR(struct pnfs_layout_hdr *lo)
149 {
150 	return container_of(lo, struct nfs4_flexfile_layout, generic_hdr);
151 }
152 
153 static inline struct nfs4_ff_layout_segment *
154 FF_LAYOUT_LSEG(struct pnfs_layout_segment *lseg)
155 {
156 	return container_of(lseg,
157 			    struct nfs4_ff_layout_segment,
158 			    generic_hdr);
159 }
160 
161 static inline struct nfs4_ff_layout_ds *
162 FF_LAYOUT_MIRROR_DS(struct nfs4_deviceid_node *node)
163 {
164 	return container_of(node, struct nfs4_ff_layout_ds, id_node);
165 }
166 
167 static inline struct nfs4_ff_layout_mirror *
168 FF_LAYOUT_COMP(struct pnfs_layout_segment *lseg, u32 idx)
169 {
170 	struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
171 
172 	if (idx < fls->mirror_array_cnt)
173 		return fls->mirror_array[idx];
174 	return NULL;
175 }
176 
177 static inline u32
178 FF_LAYOUT_MIRROR_COUNT(struct pnfs_layout_segment *lseg)
179 {
180 	return FF_LAYOUT_LSEG(lseg)->mirror_array_cnt;
181 }
182 
183 static inline bool
184 ff_layout_no_fallback_to_mds(struct pnfs_layout_segment *lseg)
185 {
186 	return FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_IO_THRU_MDS;
187 }
188 
189 /*
190  * Sticky hdr-level mirror of FF_FLAGS_NO_IO_THRU_MDS so callers that have
191  * no current lseg (e.g. between LAYOUTRETURN and the next LAYOUTGET) can
192  * still honor the no-MDS-fallback policy.
193  */
194 static inline bool
195 ff_layout_hdr_no_fallback_to_mds(struct pnfs_layout_hdr *lo)
196 {
197 	return test_bit(NFS4_FF_HDR_NO_IO_THRU_MDS,
198 			&FF_LAYOUT_FROM_HDR(lo)->flags);
199 }
200 
201 static inline bool
202 ff_layout_no_read_on_rw(struct pnfs_layout_segment *lseg)
203 {
204 	return FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_READ_IO;
205 }
206 
207 static inline int
208 nfs4_ff_layout_ds_version(const struct nfs4_ff_layout_ds *mirror_ds)
209 {
210 	return mirror_ds->ds_versions[0].version;
211 }
212 
213 static inline u32
214 nfs4_ff_layout_calc_dss_id(const u64 stripe_unit, const u32 dss_count, const loff_t offset)
215 {
216 	u64 tmp = offset;
217 
218 	if (dss_count == 1 || stripe_unit == 0)
219 		return 0;
220 
221 	tmp = div64_u64(tmp, stripe_unit);
222 
223 	return do_div(tmp, dss_count);
224 }
225 
226 struct nfs4_ff_layout_ds *
227 nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
228 			    gfp_t gfp_flags);
229 void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
230 void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
231 int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo,
232 			     struct nfs4_ff_layout_mirror *mirror,
233 			     const struct nfs4_deviceid_node *devid,
234 			     u32 dss_id, u64 offset, u64 length, int status,
235 			     enum nfs_opnum4 opnum, gfp_t gfp_flags);
236 void ff_layout_send_layouterror(struct pnfs_layout_segment *lseg);
237 int ff_layout_encode_ds_ioerr(struct xdr_stream *xdr, const struct list_head *head);
238 void ff_layout_free_ds_ioerr(struct list_head *head);
239 unsigned int ff_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo,
240 		const struct pnfs_layout_range *range,
241 		struct list_head *head,
242 		unsigned int maxnum);
243 struct nfs_fh *
244 nfs4_ff_layout_select_ds_fh(struct nfs4_ff_layout_mirror *mirror, u32 dss_id);
245 void
246 nfs4_ff_layout_select_ds_stateid(const struct nfs4_ff_layout_mirror *mirror,
247 				 const struct nfs4_ff_layout_ds *mirror_ds,
248 				 u32 dss_id,
249 				 nfs4_stateid *stateid);
250 
251 struct nfs4_ff_layout_ds *
252 ff_layout_get_mirror_ds(struct pnfs_layout_hdr *lo,
253 			struct nfs4_ff_layout_mirror *mirror,
254 			u32 dss_id);
255 struct nfs4_pnfs_ds *
256 nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg,
257 			  struct nfs4_ff_layout_mirror *mirror,
258 			  struct nfs4_ff_layout_ds *mirror_ds,
259 			  u32 dss_id,
260 			  enum nfs_opnum4 opnum);
261 
262 struct rpc_clnt *
263 nfs4_ff_find_or_create_ds_client(const struct nfs4_ff_layout_ds *mirror_ds,
264 				 struct nfs_client *ds_clp,
265 				 struct inode *inode);
266 const struct cred *ff_layout_get_ds_cred(struct nfs4_ff_layout_mirror *mirror,
267 					 const struct pnfs_layout_range *range,
268 					 const struct cred *mdscred,
269 					 const struct nfs4_ff_layout_ds *mirror_ds,
270 					 u32 dss_id);
271 bool ff_layout_avoid_mds_available_ds(struct pnfs_layout_segment *lseg);
272 bool ff_layout_avoid_read_on_rw(struct pnfs_layout_segment *lseg);
273 
274 #endif /* FS_NFS_NFS4FLEXFILELAYOUT_H */
275