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 /* LAYOUTSTATS report interval in ms */
25 #define FF_LAYOUTSTATS_REPORT_INTERVAL (60000L)
26 #define FF_LAYOUTSTATS_MAXDEV 4
27 
28 struct nfs4_ff_ds_version {
29 	u32				version;
30 	u32				minor_version;
31 	u32				rsize;
32 	u32				wsize;
33 	bool				tightly_coupled;
34 };
35 
36 /* chained in global deviceid hlist */
37 struct nfs4_ff_layout_ds {
38 	struct nfs4_deviceid_node	id_node;
39 	u32				ds_versions_cnt;
40 	struct nfs4_ff_ds_version	*ds_versions;
41 	struct nfs4_pnfs_ds		*ds;
42 };
43 
44 struct nfs4_ff_layout_ds_err {
45 	struct list_head		list; /* linked in mirror error_list */
46 	u64				offset;
47 	u64				length;
48 	int				status;
49 	enum nfs_opnum4			opnum;
50 	nfs4_stateid			stateid;
51 	struct nfs4_deviceid		deviceid;
52 };
53 
54 struct nfs4_ff_io_stat {
55 	__u64				ops_requested;
56 	__u64				bytes_requested;
57 	__u64				ops_completed;
58 	__u64				bytes_completed;
59 	__u64				bytes_not_delivered;
60 	ktime_t				total_busy_time;
61 	ktime_t				aggregate_completion_time;
62 };
63 
64 struct nfs4_ff_busy_timer {
65 	ktime_t start_time;
66 	atomic_t n_ops;
67 };
68 
69 struct nfs4_ff_layoutstat {
70 	struct nfs4_ff_io_stat io_stat;
71 	struct nfs4_ff_busy_timer busy_timer;
72 };
73 
74 struct nfs4_ff_layout_mirror {
75 	struct pnfs_layout_hdr		*layout;
76 	struct list_head		mirrors;
77 	u32				ds_count;
78 	u32				efficiency;
79 	struct nfs4_deviceid		devid;
80 	struct nfs4_ff_layout_ds	*mirror_ds;
81 	u32				fh_versions_cnt;
82 	struct nfs_fh			*fh_versions;
83 	nfs4_stateid			stateid;
84 	struct rpc_cred	__rcu		*ro_cred;
85 	struct rpc_cred	__rcu		*rw_cred;
86 	refcount_t			ref;
87 	spinlock_t			lock;
88 	unsigned long			flags;
89 	struct nfs4_ff_layoutstat	read_stat;
90 	struct nfs4_ff_layoutstat	write_stat;
91 	ktime_t				start_time;
92 	u32				report_interval;
93 };
94 
95 #define NFS4_FF_MIRROR_STAT_AVAIL	(0)
96 
97 struct nfs4_ff_layout_segment {
98 	struct pnfs_layout_segment	generic_hdr;
99 	u64				stripe_unit;
100 	u32				flags;
101 	u32				mirror_array_cnt;
102 	struct nfs4_ff_layout_mirror	**mirror_array;
103 };
104 
105 struct nfs4_flexfile_layout {
106 	struct pnfs_layout_hdr generic_hdr;
107 	struct pnfs_ds_commit_info commit_info;
108 	struct list_head	mirrors;
109 	struct list_head	error_list; /* nfs4_ff_layout_ds_err */
110 	ktime_t			last_report_time; /* Layoutstat report times */
111 };
112 
113 struct nfs4_flexfile_layoutreturn_args {
114 	struct list_head errors;
115 	struct nfs42_layoutstat_devinfo devinfo[FF_LAYOUTSTATS_MAXDEV];
116 	unsigned int num_errors;
117 	unsigned int num_dev;
118 	struct page *pages[1];
119 };
120 
121 static inline struct nfs4_flexfile_layout *
122 FF_LAYOUT_FROM_HDR(struct pnfs_layout_hdr *lo)
123 {
124 	return container_of(lo, struct nfs4_flexfile_layout, generic_hdr);
125 }
126 
127 static inline struct nfs4_ff_layout_segment *
128 FF_LAYOUT_LSEG(struct pnfs_layout_segment *lseg)
129 {
130 	return container_of(lseg,
131 			    struct nfs4_ff_layout_segment,
132 			    generic_hdr);
133 }
134 
135 static inline struct nfs4_deviceid_node *
136 FF_LAYOUT_DEVID_NODE(struct pnfs_layout_segment *lseg, u32 idx)
137 {
138 	if (idx >= FF_LAYOUT_LSEG(lseg)->mirror_array_cnt ||
139 	    FF_LAYOUT_LSEG(lseg)->mirror_array[idx] == NULL ||
140 	    FF_LAYOUT_LSEG(lseg)->mirror_array[idx]->mirror_ds == NULL)
141 		return NULL;
142 	return &FF_LAYOUT_LSEG(lseg)->mirror_array[idx]->mirror_ds->id_node;
143 }
144 
145 static inline struct nfs4_ff_layout_ds *
146 FF_LAYOUT_MIRROR_DS(struct nfs4_deviceid_node *node)
147 {
148 	return container_of(node, struct nfs4_ff_layout_ds, id_node);
149 }
150 
151 static inline struct nfs4_ff_layout_mirror *
152 FF_LAYOUT_COMP(struct pnfs_layout_segment *lseg, u32 idx)
153 {
154 	if (idx >= FF_LAYOUT_LSEG(lseg)->mirror_array_cnt)
155 		return NULL;
156 	return FF_LAYOUT_LSEG(lseg)->mirror_array[idx];
157 }
158 
159 static inline u32
160 FF_LAYOUT_MIRROR_COUNT(struct pnfs_layout_segment *lseg)
161 {
162 	return FF_LAYOUT_LSEG(lseg)->mirror_array_cnt;
163 }
164 
165 static inline bool
166 ff_layout_no_fallback_to_mds(struct pnfs_layout_segment *lseg)
167 {
168 	return FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_IO_THRU_MDS;
169 }
170 
171 static inline bool
172 ff_layout_no_read_on_rw(struct pnfs_layout_segment *lseg)
173 {
174 	return FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_READ_IO;
175 }
176 
177 static inline bool
178 ff_layout_test_devid_unavailable(struct nfs4_deviceid_node *node)
179 {
180 	/*
181 	 * Flexfiles should never mark a DS unavailable, but if it does
182 	 * print a (ratelimited) warning as this can affect performance.
183 	 */
184 	if (nfs4_test_deviceid_unavailable(node)) {
185 		u32 *p = (u32 *)node->deviceid.data;
186 
187 		pr_warn_ratelimited("NFS: flexfiles layout referencing an "
188 				"unavailable device [%x%x%x%x]\n",
189 				p[0], p[1], p[2], p[3]);
190 		return true;
191 	}
192 	return false;
193 }
194 
195 static inline int
196 nfs4_ff_layout_ds_version(struct pnfs_layout_segment *lseg, u32 ds_idx)
197 {
198 	return FF_LAYOUT_COMP(lseg, ds_idx)->mirror_ds->ds_versions[0].version;
199 }
200 
201 struct nfs4_ff_layout_ds *
202 nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
203 			    gfp_t gfp_flags);
204 void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
205 void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
206 int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo,
207 			     struct nfs4_ff_layout_mirror *mirror, u64 offset,
208 			     u64 length, int status, enum nfs_opnum4 opnum,
209 			     gfp_t gfp_flags);
210 int ff_layout_encode_ds_ioerr(struct xdr_stream *xdr, const struct list_head *head);
211 void ff_layout_free_ds_ioerr(struct list_head *head);
212 unsigned int ff_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo,
213 		const struct pnfs_layout_range *range,
214 		struct list_head *head,
215 		unsigned int maxnum);
216 struct nfs_fh *
217 nfs4_ff_layout_select_ds_fh(struct pnfs_layout_segment *lseg, u32 mirror_idx);
218 
219 struct nfs4_pnfs_ds *
220 nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx,
221 			  bool fail_return);
222 
223 struct rpc_clnt *
224 nfs4_ff_find_or_create_ds_client(struct pnfs_layout_segment *lseg,
225 				 u32 ds_idx,
226 				 struct nfs_client *ds_clp,
227 				 struct inode *inode);
228 struct rpc_cred *ff_layout_get_ds_cred(struct pnfs_layout_segment *lseg,
229 				       u32 ds_idx, struct rpc_cred *mdscred);
230 bool ff_layout_avoid_mds_available_ds(struct pnfs_layout_segment *lseg);
231 bool ff_layout_avoid_read_on_rw(struct pnfs_layout_segment *lseg);
232 
233 #endif /* FS_NFS_NFS4FLEXFILELAYOUT_H */
234