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 
14 #include "../pnfs.h"
15 
16 /* XXX: Let's filter out insanely large mirror count for now to avoid oom
17  * due to network error etc. */
18 #define NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT 4096
19 
20 /* LAYOUTSTATS report interval in ms */
21 #define FF_LAYOUTSTATS_REPORT_INTERVAL (60000L)
22 
23 struct nfs4_ff_ds_version {
24 	u32				version;
25 	u32				minor_version;
26 	u32				rsize;
27 	u32				wsize;
28 	bool				tightly_coupled;
29 };
30 
31 /* chained in global deviceid hlist */
32 struct nfs4_ff_layout_ds {
33 	struct nfs4_deviceid_node	id_node;
34 	u32				ds_versions_cnt;
35 	struct nfs4_ff_ds_version	*ds_versions;
36 	struct nfs4_pnfs_ds		*ds;
37 };
38 
39 struct nfs4_ff_layout_ds_err {
40 	struct list_head		list; /* linked in mirror error_list */
41 	u64				offset;
42 	u64				length;
43 	int				status;
44 	enum nfs_opnum4			opnum;
45 	nfs4_stateid			stateid;
46 	struct nfs4_deviceid		deviceid;
47 };
48 
49 struct nfs4_ff_io_stat {
50 	__u64				ops_requested;
51 	__u64				bytes_requested;
52 	__u64				ops_completed;
53 	__u64				bytes_completed;
54 	__u64				bytes_not_delivered;
55 	ktime_t				total_busy_time;
56 	ktime_t				aggregate_completion_time;
57 };
58 
59 struct nfs4_ff_busy_timer {
60 	ktime_t start_time;
61 	atomic_t n_ops;
62 };
63 
64 struct nfs4_ff_layoutstat {
65 	struct nfs4_ff_io_stat io_stat;
66 	struct nfs4_ff_busy_timer busy_timer;
67 };
68 
69 struct nfs4_ff_layout_mirror {
70 	struct pnfs_layout_segment	*lseg; /* back pointer */
71 	u32				ds_count;
72 	u32				efficiency;
73 	struct nfs4_ff_layout_ds	*mirror_ds;
74 	u32				fh_versions_cnt;
75 	struct nfs_fh			*fh_versions;
76 	nfs4_stateid			stateid;
77 	u32				uid;
78 	u32				gid;
79 	struct rpc_cred			*cred;
80 	spinlock_t			lock;
81 	struct nfs4_ff_layoutstat	read_stat;
82 	struct nfs4_ff_layoutstat	write_stat;
83 	ktime_t				start_time;
84 	ktime_t				last_report_time;
85 };
86 
87 struct nfs4_ff_layout_segment {
88 	struct pnfs_layout_segment	generic_hdr;
89 	u64				stripe_unit;
90 	u32				flags;
91 	u32				mirror_array_cnt;
92 	struct nfs4_ff_layout_mirror	**mirror_array;
93 };
94 
95 struct nfs4_flexfile_layout {
96 	struct pnfs_layout_hdr generic_hdr;
97 	struct pnfs_ds_commit_info commit_info;
98 	struct list_head	error_list; /* nfs4_ff_layout_ds_err */
99 };
100 
101 static inline struct nfs4_flexfile_layout *
102 FF_LAYOUT_FROM_HDR(struct pnfs_layout_hdr *lo)
103 {
104 	return container_of(lo, struct nfs4_flexfile_layout, generic_hdr);
105 }
106 
107 static inline struct nfs4_ff_layout_segment *
108 FF_LAYOUT_LSEG(struct pnfs_layout_segment *lseg)
109 {
110 	return container_of(lseg,
111 			    struct nfs4_ff_layout_segment,
112 			    generic_hdr);
113 }
114 
115 static inline struct nfs4_deviceid_node *
116 FF_LAYOUT_DEVID_NODE(struct pnfs_layout_segment *lseg, u32 idx)
117 {
118 	if (idx >= FF_LAYOUT_LSEG(lseg)->mirror_array_cnt ||
119 	    FF_LAYOUT_LSEG(lseg)->mirror_array[idx] == NULL ||
120 	    FF_LAYOUT_LSEG(lseg)->mirror_array[idx]->mirror_ds == NULL)
121 		return NULL;
122 	return &FF_LAYOUT_LSEG(lseg)->mirror_array[idx]->mirror_ds->id_node;
123 }
124 
125 static inline struct nfs4_ff_layout_ds *
126 FF_LAYOUT_MIRROR_DS(struct nfs4_deviceid_node *node)
127 {
128 	return container_of(node, struct nfs4_ff_layout_ds, id_node);
129 }
130 
131 static inline struct nfs4_ff_layout_mirror *
132 FF_LAYOUT_COMP(struct pnfs_layout_segment *lseg, u32 idx)
133 {
134 	if (idx >= FF_LAYOUT_LSEG(lseg)->mirror_array_cnt)
135 		return NULL;
136 	return FF_LAYOUT_LSEG(lseg)->mirror_array[idx];
137 }
138 
139 static inline u32
140 FF_LAYOUT_MIRROR_COUNT(struct pnfs_layout_segment *lseg)
141 {
142 	return FF_LAYOUT_LSEG(lseg)->mirror_array_cnt;
143 }
144 
145 static inline bool
146 ff_layout_test_devid_unavailable(struct nfs4_deviceid_node *node)
147 {
148 	return nfs4_test_deviceid_unavailable(node);
149 }
150 
151 static inline int
152 nfs4_ff_layout_ds_version(struct pnfs_layout_segment *lseg, u32 ds_idx)
153 {
154 	return FF_LAYOUT_COMP(lseg, ds_idx)->mirror_ds->ds_versions[0].version;
155 }
156 
157 struct nfs4_ff_layout_ds *
158 nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
159 			    gfp_t gfp_flags);
160 void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
161 void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds);
162 int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo,
163 			     struct nfs4_ff_layout_mirror *mirror, u64 offset,
164 			     u64 length, int status, enum nfs_opnum4 opnum,
165 			     gfp_t gfp_flags);
166 int ff_layout_encode_ds_ioerr(struct nfs4_flexfile_layout *flo,
167 			      struct xdr_stream *xdr, int *count,
168 			      const struct pnfs_layout_range *range);
169 struct nfs_fh *
170 nfs4_ff_layout_select_ds_fh(struct pnfs_layout_segment *lseg, u32 mirror_idx);
171 
172 struct nfs4_pnfs_ds *
173 nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx,
174 			  bool fail_return);
175 
176 struct rpc_clnt *
177 nfs4_ff_find_or_create_ds_client(struct pnfs_layout_segment *lseg,
178 				 u32 ds_idx,
179 				 struct nfs_client *ds_clp,
180 				 struct inode *inode);
181 struct rpc_cred *ff_layout_get_ds_cred(struct pnfs_layout_segment *lseg,
182 				       u32 ds_idx, struct rpc_cred *mdscred);
183 bool ff_layout_has_available_ds(struct pnfs_layout_segment *lseg);
184 #endif /* FS_NFS_NFS4FLEXFILELAYOUT_H */
185