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