]>
Commit | Line | Data |
---|---|---|
d67ae825 TH |
1 | /* |
2 | * Module for pnfs flexfile layout driver. | |
3 | * | |
4 | * Copyright (c) 2014, Primary Data, Inc. All rights reserved. | |
5 | * | |
6 | * Tao Peng <bergwolf@primarydata.com> | |
7 | */ | |
8 | ||
9 | #include <linux/nfs_fs.h> | |
10 | #include <linux/nfs_page.h> | |
11 | #include <linux/module.h> | |
12 | ||
13 | #include <linux/sunrpc/metrics.h> | |
d67ae825 TH |
14 | |
15 | #include "flexfilelayout.h" | |
16 | #include "../nfs4session.h" | |
40c64c26 | 17 | #include "../nfs4idmap.h" |
d67ae825 TH |
18 | #include "../internal.h" |
19 | #include "../delegation.h" | |
20 | #include "../nfs4trace.h" | |
21 | #include "../iostat.h" | |
22 | #include "../nfs.h" | |
ad4dc53e | 23 | #include "../nfs42.h" |
d67ae825 TH |
24 | |
25 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD | |
26 | ||
27 | #define FF_LAYOUT_POLL_RETRY_MAX (15*HZ) | |
28 | ||
29 | static struct pnfs_layout_hdr * | |
30 | ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags) | |
31 | { | |
32 | struct nfs4_flexfile_layout *ffl; | |
33 | ||
34 | ffl = kzalloc(sizeof(*ffl), gfp_flags); | |
35 | if (ffl) { | |
36 | INIT_LIST_HEAD(&ffl->error_list); | |
37 | return &ffl->generic_hdr; | |
38 | } else | |
39 | return NULL; | |
40 | } | |
41 | ||
42 | static void | |
43 | ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
44 | { | |
45 | struct nfs4_ff_layout_ds_err *err, *n; | |
46 | ||
47 | list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list, | |
48 | list) { | |
49 | list_del(&err->list); | |
50 | kfree(err); | |
51 | } | |
52 | kfree(FF_LAYOUT_FROM_HDR(lo)); | |
53 | } | |
54 | ||
55 | static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) | |
56 | { | |
57 | __be32 *p; | |
58 | ||
59 | p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE); | |
60 | if (unlikely(p == NULL)) | |
61 | return -ENOBUFS; | |
62 | memcpy(stateid, p, NFS4_STATEID_SIZE); | |
63 | dprintk("%s: stateid id= [%x%x%x%x]\n", __func__, | |
64 | p[0], p[1], p[2], p[3]); | |
65 | return 0; | |
66 | } | |
67 | ||
68 | static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid) | |
69 | { | |
70 | __be32 *p; | |
71 | ||
72 | p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE); | |
73 | if (unlikely(!p)) | |
74 | return -ENOBUFS; | |
75 | memcpy(devid, p, NFS4_DEVICEID4_SIZE); | |
76 | nfs4_print_deviceid(devid); | |
77 | return 0; | |
78 | } | |
79 | ||
80 | static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh) | |
81 | { | |
82 | __be32 *p; | |
83 | ||
84 | p = xdr_inline_decode(xdr, 4); | |
85 | if (unlikely(!p)) | |
86 | return -ENOBUFS; | |
87 | fh->size = be32_to_cpup(p++); | |
88 | if (fh->size > sizeof(struct nfs_fh)) { | |
89 | printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n", | |
90 | fh->size); | |
91 | return -EOVERFLOW; | |
92 | } | |
93 | /* fh.data */ | |
94 | p = xdr_inline_decode(xdr, fh->size); | |
95 | if (unlikely(!p)) | |
96 | return -ENOBUFS; | |
97 | memcpy(&fh->data, p, fh->size); | |
98 | dprintk("%s: fh len %d\n", __func__, fh->size); | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | /* | |
104 | * Currently only stringified uids and gids are accepted. | |
105 | * I.e., kerberos is not supported to the DSes, so no pricipals. | |
106 | * | |
107 | * That means that one common function will suffice, but when | |
108 | * principals are added, this should be split to accomodate | |
109 | * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid(). | |
110 | */ | |
111 | static int | |
112 | decode_name(struct xdr_stream *xdr, u32 *id) | |
113 | { | |
114 | __be32 *p; | |
115 | int len; | |
116 | ||
117 | /* opaque_length(4)*/ | |
118 | p = xdr_inline_decode(xdr, 4); | |
119 | if (unlikely(!p)) | |
120 | return -ENOBUFS; | |
121 | len = be32_to_cpup(p++); | |
122 | if (len < 0) | |
123 | return -EINVAL; | |
124 | ||
125 | dprintk("%s: len %u\n", __func__, len); | |
126 | ||
127 | /* opaque body */ | |
128 | p = xdr_inline_decode(xdr, len); | |
129 | if (unlikely(!p)) | |
130 | return -ENOBUFS; | |
131 | ||
132 | if (!nfs_map_string_to_numeric((char *)p, len, id)) | |
133 | return -EINVAL; | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls) | |
139 | { | |
140 | int i; | |
141 | ||
142 | if (fls->mirror_array) { | |
143 | for (i = 0; i < fls->mirror_array_cnt; i++) { | |
144 | /* normally mirror_ds is freed in | |
145 | * .free_deviceid_node but we still do it here | |
146 | * for .alloc_lseg error path */ | |
147 | if (fls->mirror_array[i]) { | |
148 | kfree(fls->mirror_array[i]->fh_versions); | |
149 | nfs4_ff_layout_put_deviceid(fls->mirror_array[i]->mirror_ds); | |
150 | kfree(fls->mirror_array[i]); | |
151 | } | |
152 | } | |
153 | kfree(fls->mirror_array); | |
154 | fls->mirror_array = NULL; | |
155 | } | |
156 | } | |
157 | ||
158 | static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr) | |
159 | { | |
160 | int ret = 0; | |
161 | ||
162 | dprintk("--> %s\n", __func__); | |
163 | ||
164 | /* FIXME: remove this check when layout segment support is added */ | |
165 | if (lgr->range.offset != 0 || | |
166 | lgr->range.length != NFS4_MAX_UINT64) { | |
167 | dprintk("%s Only whole file layouts supported. Use MDS i/o\n", | |
168 | __func__); | |
169 | ret = -EINVAL; | |
170 | } | |
171 | ||
172 | dprintk("--> %s returns %d\n", __func__, ret); | |
173 | return ret; | |
174 | } | |
175 | ||
176 | static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls) | |
177 | { | |
178 | if (fls) { | |
179 | ff_layout_free_mirror_array(fls); | |
180 | kfree(fls); | |
181 | } | |
182 | } | |
183 | ||
184 | static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls) | |
185 | { | |
d67ae825 TH |
186 | int i, j; |
187 | ||
188 | for (i = 0; i < fls->mirror_array_cnt - 1; i++) { | |
189 | for (j = i + 1; j < fls->mirror_array_cnt; j++) | |
190 | if (fls->mirror_array[i]->efficiency < | |
455b6ee6 FF |
191 | fls->mirror_array[j]->efficiency) |
192 | swap(fls->mirror_array[i], | |
193 | fls->mirror_array[j]); | |
d67ae825 TH |
194 | } |
195 | } | |
196 | ||
197 | static struct pnfs_layout_segment * | |
198 | ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, | |
199 | struct nfs4_layoutget_res *lgr, | |
200 | gfp_t gfp_flags) | |
201 | { | |
202 | struct pnfs_layout_segment *ret; | |
203 | struct nfs4_ff_layout_segment *fls = NULL; | |
204 | struct xdr_stream stream; | |
205 | struct xdr_buf buf; | |
206 | struct page *scratch; | |
207 | u64 stripe_unit; | |
208 | u32 mirror_array_cnt; | |
209 | __be32 *p; | |
210 | int i, rc; | |
211 | ||
212 | dprintk("--> %s\n", __func__); | |
213 | scratch = alloc_page(gfp_flags); | |
214 | if (!scratch) | |
215 | return ERR_PTR(-ENOMEM); | |
216 | ||
217 | xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, | |
218 | lgr->layoutp->len); | |
219 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); | |
220 | ||
221 | /* stripe unit and mirror_array_cnt */ | |
222 | rc = -EIO; | |
223 | p = xdr_inline_decode(&stream, 8 + 4); | |
224 | if (!p) | |
225 | goto out_err_free; | |
226 | ||
227 | p = xdr_decode_hyper(p, &stripe_unit); | |
228 | mirror_array_cnt = be32_to_cpup(p++); | |
229 | dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__, | |
230 | stripe_unit, mirror_array_cnt); | |
231 | ||
232 | if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT || | |
233 | mirror_array_cnt == 0) | |
234 | goto out_err_free; | |
235 | ||
236 | rc = -ENOMEM; | |
237 | fls = kzalloc(sizeof(*fls), gfp_flags); | |
238 | if (!fls) | |
239 | goto out_err_free; | |
240 | ||
241 | fls->mirror_array_cnt = mirror_array_cnt; | |
242 | fls->stripe_unit = stripe_unit; | |
243 | fls->mirror_array = kcalloc(fls->mirror_array_cnt, | |
244 | sizeof(fls->mirror_array[0]), gfp_flags); | |
245 | if (fls->mirror_array == NULL) | |
246 | goto out_err_free; | |
247 | ||
248 | for (i = 0; i < fls->mirror_array_cnt; i++) { | |
249 | struct nfs4_deviceid devid; | |
250 | struct nfs4_deviceid_node *idnode; | |
251 | u32 ds_count; | |
252 | u32 fh_count; | |
253 | int j; | |
254 | ||
255 | rc = -EIO; | |
256 | p = xdr_inline_decode(&stream, 4); | |
257 | if (!p) | |
258 | goto out_err_free; | |
259 | ds_count = be32_to_cpup(p); | |
260 | ||
261 | /* FIXME: allow for striping? */ | |
262 | if (ds_count != 1) | |
263 | goto out_err_free; | |
264 | ||
265 | fls->mirror_array[i] = | |
266 | kzalloc(sizeof(struct nfs4_ff_layout_mirror), | |
267 | gfp_flags); | |
268 | if (fls->mirror_array[i] == NULL) { | |
269 | rc = -ENOMEM; | |
270 | goto out_err_free; | |
271 | } | |
272 | ||
273 | spin_lock_init(&fls->mirror_array[i]->lock); | |
274 | fls->mirror_array[i]->ds_count = ds_count; | |
27c43064 | 275 | fls->mirror_array[i]->lseg = &fls->generic_hdr; |
d67ae825 TH |
276 | |
277 | /* deviceid */ | |
278 | rc = decode_deviceid(&stream, &devid); | |
279 | if (rc) | |
280 | goto out_err_free; | |
281 | ||
282 | idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode), | |
283 | &devid, lh->plh_lc_cred, | |
284 | gfp_flags); | |
285 | /* | |
286 | * upon success, mirror_ds is allocated by previous | |
287 | * getdeviceinfo, or newly by .alloc_deviceid_node | |
288 | * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure | |
289 | */ | |
290 | if (idnode) | |
291 | fls->mirror_array[i]->mirror_ds = | |
292 | FF_LAYOUT_MIRROR_DS(idnode); | |
293 | else | |
294 | goto out_err_free; | |
295 | ||
296 | /* efficiency */ | |
297 | rc = -EIO; | |
298 | p = xdr_inline_decode(&stream, 4); | |
299 | if (!p) | |
300 | goto out_err_free; | |
301 | fls->mirror_array[i]->efficiency = be32_to_cpup(p); | |
302 | ||
303 | /* stateid */ | |
304 | rc = decode_stateid(&stream, &fls->mirror_array[i]->stateid); | |
305 | if (rc) | |
306 | goto out_err_free; | |
307 | ||
308 | /* fh */ | |
309 | p = xdr_inline_decode(&stream, 4); | |
310 | if (!p) | |
311 | goto out_err_free; | |
312 | fh_count = be32_to_cpup(p); | |
313 | ||
314 | fls->mirror_array[i]->fh_versions = | |
315 | kzalloc(fh_count * sizeof(struct nfs_fh), | |
316 | gfp_flags); | |
317 | if (fls->mirror_array[i]->fh_versions == NULL) { | |
318 | rc = -ENOMEM; | |
319 | goto out_err_free; | |
320 | } | |
321 | ||
322 | for (j = 0; j < fh_count; j++) { | |
323 | rc = decode_nfs_fh(&stream, | |
324 | &fls->mirror_array[i]->fh_versions[j]); | |
325 | if (rc) | |
326 | goto out_err_free; | |
327 | } | |
328 | ||
329 | fls->mirror_array[i]->fh_versions_cnt = fh_count; | |
330 | ||
331 | /* user */ | |
332 | rc = decode_name(&stream, &fls->mirror_array[i]->uid); | |
333 | if (rc) | |
334 | goto out_err_free; | |
335 | ||
336 | /* group */ | |
337 | rc = decode_name(&stream, &fls->mirror_array[i]->gid); | |
338 | if (rc) | |
339 | goto out_err_free; | |
340 | ||
341 | dprintk("%s: uid %d gid %d\n", __func__, | |
342 | fls->mirror_array[i]->uid, | |
343 | fls->mirror_array[i]->gid); | |
344 | } | |
345 | ||
c0f5f505 TM |
346 | p = xdr_inline_decode(&stream, 4); |
347 | if (p) | |
348 | fls->flags = be32_to_cpup(p); | |
349 | ||
d67ae825 TH |
350 | ff_layout_sort_mirrors(fls); |
351 | rc = ff_layout_check_layout(lgr); | |
352 | if (rc) | |
353 | goto out_err_free; | |
354 | ||
355 | ret = &fls->generic_hdr; | |
356 | dprintk("<-- %s (success)\n", __func__); | |
357 | out_free_page: | |
358 | __free_page(scratch); | |
359 | return ret; | |
360 | out_err_free: | |
361 | _ff_layout_free_lseg(fls); | |
362 | ret = ERR_PTR(rc); | |
363 | dprintk("<-- %s (%d)\n", __func__, rc); | |
364 | goto out_free_page; | |
365 | } | |
366 | ||
367 | static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout) | |
368 | { | |
369 | struct pnfs_layout_segment *lseg; | |
370 | ||
371 | list_for_each_entry(lseg, &layout->plh_segs, pls_list) | |
372 | if (lseg->pls_range.iomode == IOMODE_RW) | |
373 | return true; | |
374 | ||
375 | return false; | |
376 | } | |
377 | ||
378 | static void | |
379 | ff_layout_free_lseg(struct pnfs_layout_segment *lseg) | |
380 | { | |
381 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); | |
382 | int i; | |
383 | ||
384 | dprintk("--> %s\n", __func__); | |
385 | ||
386 | for (i = 0; i < fls->mirror_array_cnt; i++) { | |
387 | if (fls->mirror_array[i]) { | |
388 | nfs4_ff_layout_put_deviceid(fls->mirror_array[i]->mirror_ds); | |
389 | fls->mirror_array[i]->mirror_ds = NULL; | |
390 | if (fls->mirror_array[i]->cred) { | |
391 | put_rpccred(fls->mirror_array[i]->cred); | |
392 | fls->mirror_array[i]->cred = NULL; | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | if (lseg->pls_range.iomode == IOMODE_RW) { | |
398 | struct nfs4_flexfile_layout *ffl; | |
399 | struct inode *inode; | |
400 | ||
401 | ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout); | |
402 | inode = ffl->generic_hdr.plh_inode; | |
403 | spin_lock(&inode->i_lock); | |
404 | if (!ff_layout_has_rw_segments(lseg->pls_layout)) { | |
405 | ffl->commit_info.nbuckets = 0; | |
406 | kfree(ffl->commit_info.buckets); | |
407 | ffl->commit_info.buckets = NULL; | |
408 | } | |
409 | spin_unlock(&inode->i_lock); | |
410 | } | |
411 | _ff_layout_free_lseg(fls); | |
412 | } | |
413 | ||
414 | /* Return 1 until we have multiple lsegs support */ | |
415 | static int | |
416 | ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls) | |
417 | { | |
418 | return 1; | |
419 | } | |
420 | ||
abcb7bfc | 421 | static void |
e76d28dd | 422 | nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now) |
abcb7bfc | 423 | { |
9bbd9bb4 PT |
424 | /* first IO request? */ |
425 | if (atomic_inc_return(&timer->n_ops) == 1) { | |
e76d28dd | 426 | timer->start_time = now; |
abcb7bfc TM |
427 | } |
428 | } | |
429 | ||
430 | static ktime_t | |
e76d28dd | 431 | nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now) |
abcb7bfc | 432 | { |
e76d28dd | 433 | ktime_t start; |
abcb7bfc | 434 | |
9bbd9bb4 PT |
435 | if (atomic_dec_return(&timer->n_ops) < 0) |
436 | WARN_ON_ONCE(1); | |
437 | ||
9bbd9bb4 PT |
438 | start = timer->start_time; |
439 | timer->start_time = now; | |
abcb7bfc TM |
440 | return ktime_sub(now, start); |
441 | } | |
442 | ||
97ba375b | 443 | static bool |
d983803d | 444 | nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror, |
e76d28dd TM |
445 | struct nfs4_ff_layoutstat *layoutstat, |
446 | ktime_t now) | |
abcb7bfc | 447 | { |
d983803d PT |
448 | static const ktime_t notime = {0}; |
449 | ||
e76d28dd | 450 | nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now); |
9bbd9bb4 PT |
451 | if (ktime_equal(mirror->start_time, notime)) |
452 | mirror->start_time = now; | |
453 | if (ktime_equal(mirror->last_report_time, notime)) | |
454 | mirror->last_report_time = now; | |
97ba375b PT |
455 | if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >= |
456 | FF_LAYOUTSTATS_REPORT_INTERVAL) { | |
457 | mirror->last_report_time = now; | |
458 | return true; | |
459 | } | |
460 | ||
461 | return false; | |
abcb7bfc TM |
462 | } |
463 | ||
464 | static void | |
465 | nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat, | |
466 | __u64 requested) | |
467 | { | |
468 | struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; | |
469 | ||
470 | iostat->ops_requested++; | |
471 | iostat->bytes_requested += requested; | |
472 | } | |
473 | ||
474 | static void | |
475 | nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat, | |
476 | __u64 requested, | |
477 | __u64 completed, | |
e76d28dd TM |
478 | ktime_t time_completed, |
479 | ktime_t time_started) | |
abcb7bfc TM |
480 | { |
481 | struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; | |
e76d28dd | 482 | ktime_t completion_time = ktime_sub(time_completed, time_started); |
abcb7bfc TM |
483 | ktime_t timer; |
484 | ||
485 | iostat->ops_completed++; | |
486 | iostat->bytes_completed += completed; | |
487 | iostat->bytes_not_delivered += requested - completed; | |
488 | ||
e76d28dd | 489 | timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed); |
abcb7bfc TM |
490 | iostat->total_busy_time = |
491 | ktime_add(iostat->total_busy_time, timer); | |
492 | iostat->aggregate_completion_time = | |
e76d28dd TM |
493 | ktime_add(iostat->aggregate_completion_time, |
494 | completion_time); | |
abcb7bfc TM |
495 | } |
496 | ||
497 | static void | |
498 | nfs4_ff_layout_stat_io_start_read(struct nfs4_ff_layout_mirror *mirror, | |
e76d28dd | 499 | __u64 requested, ktime_t now) |
abcb7bfc | 500 | { |
97ba375b PT |
501 | bool report; |
502 | ||
abcb7bfc | 503 | spin_lock(&mirror->lock); |
e76d28dd | 504 | report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now); |
abcb7bfc TM |
505 | nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested); |
506 | spin_unlock(&mirror->lock); | |
97ba375b PT |
507 | |
508 | if (report) | |
c8ad8894 TM |
509 | pnfs_report_layoutstat(mirror->lseg->pls_layout->plh_inode, |
510 | GFP_KERNEL); | |
abcb7bfc TM |
511 | } |
512 | ||
513 | static void | |
514 | nfs4_ff_layout_stat_io_end_read(struct rpc_task *task, | |
515 | struct nfs4_ff_layout_mirror *mirror, | |
516 | __u64 requested, | |
517 | __u64 completed) | |
518 | { | |
519 | spin_lock(&mirror->lock); | |
520 | nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat, | |
521 | requested, completed, | |
e76d28dd | 522 | ktime_get(), task->tk_start); |
abcb7bfc TM |
523 | spin_unlock(&mirror->lock); |
524 | } | |
525 | ||
526 | static void | |
527 | nfs4_ff_layout_stat_io_start_write(struct nfs4_ff_layout_mirror *mirror, | |
e76d28dd | 528 | __u64 requested, ktime_t now) |
abcb7bfc | 529 | { |
97ba375b PT |
530 | bool report; |
531 | ||
abcb7bfc | 532 | spin_lock(&mirror->lock); |
e76d28dd | 533 | report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now); |
abcb7bfc TM |
534 | nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested); |
535 | spin_unlock(&mirror->lock); | |
97ba375b PT |
536 | |
537 | if (report) | |
c8ad8894 TM |
538 | pnfs_report_layoutstat(mirror->lseg->pls_layout->plh_inode, |
539 | GFP_NOIO); | |
abcb7bfc TM |
540 | } |
541 | ||
542 | static void | |
543 | nfs4_ff_layout_stat_io_end_write(struct rpc_task *task, | |
544 | struct nfs4_ff_layout_mirror *mirror, | |
545 | __u64 requested, | |
546 | __u64 completed, | |
547 | enum nfs3_stable_how committed) | |
548 | { | |
549 | if (committed == NFS_UNSTABLE) | |
550 | requested = completed = 0; | |
551 | ||
552 | spin_lock(&mirror->lock); | |
553 | nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat, | |
e76d28dd | 554 | requested, completed, ktime_get(), task->tk_start); |
abcb7bfc TM |
555 | spin_unlock(&mirror->lock); |
556 | } | |
557 | ||
d67ae825 TH |
558 | static int |
559 | ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg, | |
560 | struct nfs_commit_info *cinfo, | |
561 | gfp_t gfp_flags) | |
562 | { | |
563 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); | |
564 | struct pnfs_commit_bucket *buckets; | |
565 | int size; | |
566 | ||
567 | if (cinfo->ds->nbuckets != 0) { | |
568 | /* This assumes there is only one RW lseg per file. | |
569 | * To support multiple lseg per file, we need to | |
570 | * change struct pnfs_commit_bucket to allow dynamic | |
571 | * increasing nbuckets. | |
572 | */ | |
573 | return 0; | |
574 | } | |
575 | ||
576 | size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg); | |
577 | ||
578 | buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket), | |
579 | gfp_flags); | |
580 | if (!buckets) | |
581 | return -ENOMEM; | |
582 | else { | |
583 | int i; | |
584 | ||
585 | spin_lock(cinfo->lock); | |
586 | if (cinfo->ds->nbuckets != 0) | |
587 | kfree(buckets); | |
588 | else { | |
589 | cinfo->ds->buckets = buckets; | |
590 | cinfo->ds->nbuckets = size; | |
591 | for (i = 0; i < size; i++) { | |
592 | INIT_LIST_HEAD(&buckets[i].written); | |
593 | INIT_LIST_HEAD(&buckets[i].committing); | |
594 | /* mark direct verifier as unset */ | |
595 | buckets[i].direct_verf.committed = | |
596 | NFS_INVALID_STABLE_HOW; | |
597 | } | |
598 | } | |
599 | spin_unlock(cinfo->lock); | |
600 | return 0; | |
601 | } | |
602 | } | |
603 | ||
604 | static struct nfs4_pnfs_ds * | |
605 | ff_layout_choose_best_ds_for_read(struct nfs_pageio_descriptor *pgio, | |
606 | int *best_idx) | |
607 | { | |
608 | struct nfs4_ff_layout_segment *fls; | |
609 | struct nfs4_pnfs_ds *ds; | |
610 | int idx; | |
611 | ||
612 | fls = FF_LAYOUT_LSEG(pgio->pg_lseg); | |
613 | /* mirrors are sorted by efficiency */ | |
614 | for (idx = 0; idx < fls->mirror_array_cnt; idx++) { | |
615 | ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, idx, false); | |
616 | if (ds) { | |
617 | *best_idx = idx; | |
618 | return ds; | |
619 | } | |
620 | } | |
621 | ||
622 | return NULL; | |
623 | } | |
624 | ||
625 | static void | |
626 | ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio, | |
627 | struct nfs_page *req) | |
628 | { | |
629 | struct nfs_pgio_mirror *pgm; | |
630 | struct nfs4_ff_layout_mirror *mirror; | |
631 | struct nfs4_pnfs_ds *ds; | |
632 | int ds_idx; | |
633 | ||
634 | /* Use full layout for now */ | |
635 | if (!pgio->pg_lseg) | |
636 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, | |
637 | req->wb_context, | |
638 | 0, | |
639 | NFS4_MAX_UINT64, | |
640 | IOMODE_READ, | |
641 | GFP_KERNEL); | |
642 | /* If no lseg, fall back to read through mds */ | |
643 | if (pgio->pg_lseg == NULL) | |
644 | goto out_mds; | |
645 | ||
646 | ds = ff_layout_choose_best_ds_for_read(pgio, &ds_idx); | |
647 | if (!ds) | |
648 | goto out_mds; | |
649 | mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx); | |
650 | ||
651 | pgio->pg_mirror_idx = ds_idx; | |
652 | ||
653 | /* read always uses only one mirror - idx 0 for pgio layer */ | |
654 | pgm = &pgio->pg_mirrors[0]; | |
655 | pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize; | |
656 | ||
657 | return; | |
658 | out_mds: | |
659 | pnfs_put_lseg(pgio->pg_lseg); | |
660 | pgio->pg_lseg = NULL; | |
661 | nfs_pageio_reset_read_mds(pgio); | |
662 | } | |
663 | ||
664 | static void | |
665 | ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio, | |
666 | struct nfs_page *req) | |
667 | { | |
668 | struct nfs4_ff_layout_mirror *mirror; | |
669 | struct nfs_pgio_mirror *pgm; | |
670 | struct nfs_commit_info cinfo; | |
671 | struct nfs4_pnfs_ds *ds; | |
672 | int i; | |
673 | int status; | |
674 | ||
675 | if (!pgio->pg_lseg) | |
676 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, | |
677 | req->wb_context, | |
678 | 0, | |
679 | NFS4_MAX_UINT64, | |
680 | IOMODE_RW, | |
681 | GFP_NOFS); | |
682 | /* If no lseg, fall back to write through mds */ | |
683 | if (pgio->pg_lseg == NULL) | |
684 | goto out_mds; | |
685 | ||
686 | nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq); | |
687 | status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS); | |
688 | if (status < 0) | |
689 | goto out_mds; | |
690 | ||
691 | /* Use a direct mapping of ds_idx to pgio mirror_idx */ | |
692 | if (WARN_ON_ONCE(pgio->pg_mirror_count != | |
693 | FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg))) | |
694 | goto out_mds; | |
695 | ||
696 | for (i = 0; i < pgio->pg_mirror_count; i++) { | |
697 | ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true); | |
698 | if (!ds) | |
699 | goto out_mds; | |
700 | pgm = &pgio->pg_mirrors[i]; | |
701 | mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i); | |
702 | pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize; | |
703 | } | |
704 | ||
705 | return; | |
706 | ||
707 | out_mds: | |
708 | pnfs_put_lseg(pgio->pg_lseg); | |
709 | pgio->pg_lseg = NULL; | |
710 | nfs_pageio_reset_write_mds(pgio); | |
711 | } | |
712 | ||
713 | static unsigned int | |
714 | ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio, | |
715 | struct nfs_page *req) | |
716 | { | |
717 | if (!pgio->pg_lseg) | |
718 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, | |
719 | req->wb_context, | |
720 | 0, | |
721 | NFS4_MAX_UINT64, | |
722 | IOMODE_RW, | |
723 | GFP_NOFS); | |
724 | if (pgio->pg_lseg) | |
725 | return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg); | |
726 | ||
727 | /* no lseg means that pnfs is not in use, so no mirroring here */ | |
d67ae825 TH |
728 | nfs_pageio_reset_write_mds(pgio); |
729 | return 1; | |
730 | } | |
731 | ||
732 | static const struct nfs_pageio_ops ff_layout_pg_read_ops = { | |
733 | .pg_init = ff_layout_pg_init_read, | |
734 | .pg_test = pnfs_generic_pg_test, | |
735 | .pg_doio = pnfs_generic_pg_readpages, | |
736 | .pg_cleanup = pnfs_generic_pg_cleanup, | |
737 | }; | |
738 | ||
739 | static const struct nfs_pageio_ops ff_layout_pg_write_ops = { | |
740 | .pg_init = ff_layout_pg_init_write, | |
741 | .pg_test = pnfs_generic_pg_test, | |
742 | .pg_doio = pnfs_generic_pg_writepages, | |
743 | .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write, | |
744 | .pg_cleanup = pnfs_generic_pg_cleanup, | |
745 | }; | |
746 | ||
747 | static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs) | |
748 | { | |
749 | struct rpc_task *task = &hdr->task; | |
750 | ||
751 | pnfs_layoutcommit_inode(hdr->inode, false); | |
752 | ||
753 | if (retry_pnfs) { | |
754 | dprintk("%s Reset task %5u for i/o through pNFS " | |
755 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, | |
756 | hdr->task.tk_pid, | |
757 | hdr->inode->i_sb->s_id, | |
758 | (unsigned long long)NFS_FILEID(hdr->inode), | |
759 | hdr->args.count, | |
760 | (unsigned long long)hdr->args.offset); | |
761 | ||
762 | if (!hdr->dreq) { | |
763 | struct nfs_open_context *ctx; | |
764 | ||
765 | ctx = nfs_list_entry(hdr->pages.next)->wb_context; | |
766 | set_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); | |
767 | hdr->completion_ops->error_cleanup(&hdr->pages); | |
768 | } else { | |
769 | nfs_direct_set_resched_writes(hdr->dreq); | |
770 | /* fake unstable write to let common nfs resend pages */ | |
771 | hdr->verf.committed = NFS_UNSTABLE; | |
d6208769 | 772 | hdr->good_bytes = hdr->args.count; |
d67ae825 TH |
773 | } |
774 | return; | |
775 | } | |
776 | ||
777 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { | |
778 | dprintk("%s Reset task %5u for i/o through MDS " | |
779 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, | |
780 | hdr->task.tk_pid, | |
781 | hdr->inode->i_sb->s_id, | |
782 | (unsigned long long)NFS_FILEID(hdr->inode), | |
783 | hdr->args.count, | |
784 | (unsigned long long)hdr->args.offset); | |
785 | ||
786 | task->tk_status = pnfs_write_done_resend_to_mds(hdr); | |
787 | } | |
788 | } | |
789 | ||
790 | static void ff_layout_reset_read(struct nfs_pgio_header *hdr) | |
791 | { | |
792 | struct rpc_task *task = &hdr->task; | |
793 | ||
794 | pnfs_layoutcommit_inode(hdr->inode, false); | |
795 | ||
796 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { | |
797 | dprintk("%s Reset task %5u for i/o through MDS " | |
798 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, | |
799 | hdr->task.tk_pid, | |
800 | hdr->inode->i_sb->s_id, | |
801 | (unsigned long long)NFS_FILEID(hdr->inode), | |
802 | hdr->args.count, | |
803 | (unsigned long long)hdr->args.offset); | |
804 | ||
805 | task->tk_status = pnfs_read_done_resend_to_mds(hdr); | |
806 | } | |
807 | } | |
808 | ||
809 | static int ff_layout_async_handle_error_v4(struct rpc_task *task, | |
810 | struct nfs4_state *state, | |
811 | struct nfs_client *clp, | |
812 | struct pnfs_layout_segment *lseg, | |
813 | int idx) | |
814 | { | |
815 | struct pnfs_layout_hdr *lo = lseg->pls_layout; | |
816 | struct inode *inode = lo->plh_inode; | |
817 | struct nfs_server *mds_server = NFS_SERVER(inode); | |
818 | ||
819 | struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); | |
820 | struct nfs_client *mds_client = mds_server->nfs_client; | |
821 | struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table; | |
822 | ||
823 | if (task->tk_status >= 0) | |
824 | return 0; | |
825 | ||
826 | switch (task->tk_status) { | |
827 | /* MDS state errors */ | |
828 | case -NFS4ERR_DELEG_REVOKED: | |
829 | case -NFS4ERR_ADMIN_REVOKED: | |
830 | case -NFS4ERR_BAD_STATEID: | |
831 | if (state == NULL) | |
832 | break; | |
833 | nfs_remove_bad_delegation(state->inode); | |
834 | case -NFS4ERR_OPENMODE: | |
835 | if (state == NULL) | |
836 | break; | |
837 | if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) | |
838 | goto out_bad_stateid; | |
839 | goto wait_on_recovery; | |
840 | case -NFS4ERR_EXPIRED: | |
841 | if (state != NULL) { | |
842 | if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) | |
843 | goto out_bad_stateid; | |
844 | } | |
845 | nfs4_schedule_lease_recovery(mds_client); | |
846 | goto wait_on_recovery; | |
847 | /* DS session errors */ | |
848 | case -NFS4ERR_BADSESSION: | |
849 | case -NFS4ERR_BADSLOT: | |
850 | case -NFS4ERR_BAD_HIGH_SLOT: | |
851 | case -NFS4ERR_DEADSESSION: | |
852 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
853 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
854 | case -NFS4ERR_SEQ_MISORDERED: | |
855 | dprintk("%s ERROR %d, Reset session. Exchangeid " | |
856 | "flags 0x%x\n", __func__, task->tk_status, | |
857 | clp->cl_exchange_flags); | |
858 | nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); | |
859 | break; | |
860 | case -NFS4ERR_DELAY: | |
861 | case -NFS4ERR_GRACE: | |
862 | rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX); | |
863 | break; | |
864 | case -NFS4ERR_RETRY_UNCACHED_REP: | |
865 | break; | |
866 | /* Invalidate Layout errors */ | |
867 | case -NFS4ERR_PNFS_NO_LAYOUT: | |
868 | case -ESTALE: /* mapped NFS4ERR_STALE */ | |
869 | case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */ | |
870 | case -EISDIR: /* mapped NFS4ERR_ISDIR */ | |
871 | case -NFS4ERR_FHEXPIRED: | |
872 | case -NFS4ERR_WRONG_TYPE: | |
873 | dprintk("%s Invalid layout error %d\n", __func__, | |
874 | task->tk_status); | |
875 | /* | |
876 | * Destroy layout so new i/o will get a new layout. | |
877 | * Layout will not be destroyed until all current lseg | |
878 | * references are put. Mark layout as invalid to resend failed | |
879 | * i/o and all i/o waiting on the slot table to the MDS until | |
880 | * layout is destroyed and a new valid layout is obtained. | |
881 | */ | |
882 | pnfs_destroy_layout(NFS_I(inode)); | |
883 | rpc_wake_up(&tbl->slot_tbl_waitq); | |
884 | goto reset; | |
885 | /* RPC connection errors */ | |
886 | case -ECONNREFUSED: | |
887 | case -EHOSTDOWN: | |
888 | case -EHOSTUNREACH: | |
889 | case -ENETUNREACH: | |
890 | case -EIO: | |
891 | case -ETIMEDOUT: | |
892 | case -EPIPE: | |
893 | dprintk("%s DS connection error %d\n", __func__, | |
894 | task->tk_status); | |
895 | nfs4_mark_deviceid_unavailable(devid); | |
896 | rpc_wake_up(&tbl->slot_tbl_waitq); | |
897 | /* fall through */ | |
898 | default: | |
899 | if (ff_layout_has_available_ds(lseg)) | |
900 | return -NFS4ERR_RESET_TO_PNFS; | |
901 | reset: | |
902 | dprintk("%s Retry through MDS. Error %d\n", __func__, | |
903 | task->tk_status); | |
904 | return -NFS4ERR_RESET_TO_MDS; | |
905 | } | |
906 | out: | |
907 | task->tk_status = 0; | |
908 | return -EAGAIN; | |
909 | out_bad_stateid: | |
910 | task->tk_status = -EIO; | |
911 | return 0; | |
912 | wait_on_recovery: | |
913 | rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL); | |
914 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0) | |
915 | rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task); | |
916 | goto out; | |
917 | } | |
918 | ||
919 | /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */ | |
920 | static int ff_layout_async_handle_error_v3(struct rpc_task *task, | |
921 | struct pnfs_layout_segment *lseg, | |
922 | int idx) | |
923 | { | |
924 | struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); | |
925 | ||
926 | if (task->tk_status >= 0) | |
927 | return 0; | |
928 | ||
929 | if (task->tk_status != -EJUKEBOX) { | |
930 | dprintk("%s DS connection error %d\n", __func__, | |
931 | task->tk_status); | |
932 | nfs4_mark_deviceid_unavailable(devid); | |
933 | if (ff_layout_has_available_ds(lseg)) | |
934 | return -NFS4ERR_RESET_TO_PNFS; | |
935 | else | |
936 | return -NFS4ERR_RESET_TO_MDS; | |
937 | } | |
938 | ||
939 | if (task->tk_status == -EJUKEBOX) | |
940 | nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY); | |
941 | task->tk_status = 0; | |
942 | rpc_restart_call(task); | |
943 | rpc_delay(task, NFS_JUKEBOX_RETRY_TIME); | |
944 | return -EAGAIN; | |
945 | } | |
946 | ||
947 | static int ff_layout_async_handle_error(struct rpc_task *task, | |
948 | struct nfs4_state *state, | |
949 | struct nfs_client *clp, | |
950 | struct pnfs_layout_segment *lseg, | |
951 | int idx) | |
952 | { | |
953 | int vers = clp->cl_nfs_mod->rpc_vers->number; | |
954 | ||
955 | switch (vers) { | |
956 | case 3: | |
957 | return ff_layout_async_handle_error_v3(task, lseg, idx); | |
958 | case 4: | |
959 | return ff_layout_async_handle_error_v4(task, state, clp, | |
960 | lseg, idx); | |
961 | default: | |
962 | /* should never happen */ | |
963 | WARN_ON_ONCE(1); | |
964 | return 0; | |
965 | } | |
966 | } | |
967 | ||
968 | static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg, | |
969 | int idx, u64 offset, u64 length, | |
dd52128a | 970 | u32 status, int opnum, int error) |
d67ae825 TH |
971 | { |
972 | struct nfs4_ff_layout_mirror *mirror; | |
973 | int err; | |
974 | ||
dd52128a TM |
975 | if (status == 0) { |
976 | switch (error) { | |
977 | case -ETIMEDOUT: | |
978 | case -EPFNOSUPPORT: | |
979 | case -EPROTONOSUPPORT: | |
980 | case -EOPNOTSUPP: | |
981 | case -ECONNREFUSED: | |
982 | case -ECONNRESET: | |
983 | case -EHOSTDOWN: | |
984 | case -EHOSTUNREACH: | |
985 | case -ENETUNREACH: | |
986 | case -EADDRINUSE: | |
987 | case -ENOBUFS: | |
988 | case -EPIPE: | |
989 | case -EPERM: | |
990 | status = NFS4ERR_NXIO; | |
991 | break; | |
992 | case -EACCES: | |
993 | status = NFS4ERR_ACCESS; | |
994 | break; | |
995 | default: | |
996 | return; | |
997 | } | |
998 | } | |
999 | ||
d67ae825 TH |
1000 | mirror = FF_LAYOUT_COMP(lseg, idx); |
1001 | err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout), | |
1002 | mirror, offset, length, status, opnum, | |
1003 | GFP_NOIO); | |
1004 | dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status); | |
1005 | } | |
1006 | ||
1007 | /* NFS_PROTO call done callback routines */ | |
1008 | ||
1009 | static int ff_layout_read_done_cb(struct rpc_task *task, | |
1010 | struct nfs_pgio_header *hdr) | |
1011 | { | |
1012 | struct inode *inode; | |
1013 | int err; | |
1014 | ||
1015 | trace_nfs4_pnfs_read(hdr, task->tk_status); | |
dd52128a | 1016 | if (task->tk_status < 0) |
d67ae825 TH |
1017 | ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, |
1018 | hdr->args.offset, hdr->args.count, | |
dd52128a TM |
1019 | hdr->res.op_status, OP_READ, |
1020 | task->tk_status); | |
d67ae825 TH |
1021 | err = ff_layout_async_handle_error(task, hdr->args.context->state, |
1022 | hdr->ds_clp, hdr->lseg, | |
1023 | hdr->pgio_mirror_idx); | |
1024 | ||
1025 | switch (err) { | |
1026 | case -NFS4ERR_RESET_TO_PNFS: | |
1027 | set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, | |
1028 | &hdr->lseg->pls_layout->plh_flags); | |
1029 | pnfs_read_resend_pnfs(hdr); | |
1030 | return task->tk_status; | |
1031 | case -NFS4ERR_RESET_TO_MDS: | |
1032 | inode = hdr->lseg->pls_layout->plh_inode; | |
1033 | pnfs_error_mark_layout_for_return(inode, hdr->lseg); | |
1034 | ff_layout_reset_read(hdr); | |
1035 | return task->tk_status; | |
1036 | case -EAGAIN: | |
1037 | rpc_restart_call_prepare(task); | |
1038 | return -EAGAIN; | |
1039 | } | |
1040 | ||
1041 | return 0; | |
1042 | } | |
1043 | ||
c0f5f505 TM |
1044 | static bool |
1045 | ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg) | |
1046 | { | |
1047 | return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT); | |
1048 | } | |
1049 | ||
d67ae825 TH |
1050 | /* |
1051 | * We reference the rpc_cred of the first WRITE that triggers the need for | |
1052 | * a LAYOUTCOMMIT, and use it to send the layoutcommit compound. | |
1053 | * rfc5661 is not clear about which credential should be used. | |
1054 | * | |
1055 | * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so | |
1056 | * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751 | |
1057 | * we always send layoutcommit after DS writes. | |
1058 | */ | |
1059 | static void | |
1060 | ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr) | |
1061 | { | |
c0f5f505 TM |
1062 | if (!ff_layout_need_layoutcommit(hdr->lseg)) |
1063 | return; | |
1064 | ||
67af7611 TM |
1065 | pnfs_set_layoutcommit(hdr->inode, hdr->lseg, |
1066 | hdr->mds_offset + hdr->res.count); | |
d67ae825 TH |
1067 | dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino, |
1068 | (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb); | |
1069 | } | |
1070 | ||
1071 | static bool | |
1072 | ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx) | |
1073 | { | |
1074 | /* No mirroring for now */ | |
1075 | struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx); | |
1076 | ||
1077 | return ff_layout_test_devid_unavailable(node); | |
1078 | } | |
1079 | ||
1080 | static int ff_layout_read_prepare_common(struct rpc_task *task, | |
1081 | struct nfs_pgio_header *hdr) | |
1082 | { | |
abcb7bfc TM |
1083 | nfs4_ff_layout_stat_io_start_read( |
1084 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), | |
e76d28dd TM |
1085 | hdr->args.count, |
1086 | task->tk_start); | |
abcb7bfc | 1087 | |
d67ae825 TH |
1088 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { |
1089 | rpc_exit(task, -EIO); | |
1090 | return -EIO; | |
1091 | } | |
1092 | if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) { | |
1093 | dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid); | |
1094 | if (ff_layout_has_available_ds(hdr->lseg)) | |
1095 | pnfs_read_resend_pnfs(hdr); | |
1096 | else | |
1097 | ff_layout_reset_read(hdr); | |
1098 | rpc_exit(task, 0); | |
1099 | return -EAGAIN; | |
1100 | } | |
1101 | hdr->pgio_done_cb = ff_layout_read_done_cb; | |
1102 | ||
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | /* | |
1107 | * Call ops for the async read/write cases | |
1108 | * In the case of dense layouts, the offset needs to be reset to its | |
1109 | * original value. | |
1110 | */ | |
1111 | static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data) | |
1112 | { | |
1113 | struct nfs_pgio_header *hdr = data; | |
1114 | ||
1115 | if (ff_layout_read_prepare_common(task, hdr)) | |
1116 | return; | |
1117 | ||
1118 | rpc_call_start(task); | |
1119 | } | |
1120 | ||
1121 | static int ff_layout_setup_sequence(struct nfs_client *ds_clp, | |
1122 | struct nfs4_sequence_args *args, | |
1123 | struct nfs4_sequence_res *res, | |
1124 | struct rpc_task *task) | |
1125 | { | |
1126 | if (ds_clp->cl_session) | |
1127 | return nfs41_setup_sequence(ds_clp->cl_session, | |
1128 | args, | |
1129 | res, | |
1130 | task); | |
1131 | return nfs40_setup_sequence(ds_clp->cl_slot_tbl, | |
1132 | args, | |
1133 | res, | |
1134 | task); | |
1135 | } | |
1136 | ||
1137 | static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data) | |
1138 | { | |
1139 | struct nfs_pgio_header *hdr = data; | |
1140 | ||
d67ae825 TH |
1141 | if (ff_layout_setup_sequence(hdr->ds_clp, |
1142 | &hdr->args.seq_args, | |
1143 | &hdr->res.seq_res, | |
1144 | task)) | |
1145 | return; | |
1146 | ||
abcb7bfc TM |
1147 | if (ff_layout_read_prepare_common(task, hdr)) |
1148 | return; | |
1149 | ||
d67ae825 TH |
1150 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
1151 | hdr->args.lock_context, FMODE_READ) == -EIO) | |
1152 | rpc_exit(task, -EIO); /* lost lock, terminate I/O */ | |
1153 | } | |
1154 | ||
1155 | static void ff_layout_read_call_done(struct rpc_task *task, void *data) | |
1156 | { | |
1157 | struct nfs_pgio_header *hdr = data; | |
1158 | ||
1159 | dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status); | |
1160 | ||
abcb7bfc TM |
1161 | nfs4_ff_layout_stat_io_end_read(task, |
1162 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), | |
1163 | hdr->args.count, hdr->res.count); | |
1164 | ||
d67ae825 TH |
1165 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && |
1166 | task->tk_status == 0) { | |
1167 | nfs4_sequence_done(task, &hdr->res.seq_res); | |
1168 | return; | |
1169 | } | |
1170 | ||
1171 | /* Note this may cause RPC to be resent */ | |
1172 | hdr->mds_ops->rpc_call_done(task, hdr); | |
1173 | } | |
1174 | ||
1175 | static void ff_layout_read_count_stats(struct rpc_task *task, void *data) | |
1176 | { | |
1177 | struct nfs_pgio_header *hdr = data; | |
1178 | ||
1179 | rpc_count_iostats_metrics(task, | |
1180 | &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]); | |
1181 | } | |
1182 | ||
1183 | static int ff_layout_write_done_cb(struct rpc_task *task, | |
1184 | struct nfs_pgio_header *hdr) | |
1185 | { | |
1186 | struct inode *inode; | |
1187 | int err; | |
1188 | ||
1189 | trace_nfs4_pnfs_write(hdr, task->tk_status); | |
dd52128a | 1190 | if (task->tk_status < 0) |
d67ae825 TH |
1191 | ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, |
1192 | hdr->args.offset, hdr->args.count, | |
dd52128a TM |
1193 | hdr->res.op_status, OP_WRITE, |
1194 | task->tk_status); | |
d67ae825 TH |
1195 | err = ff_layout_async_handle_error(task, hdr->args.context->state, |
1196 | hdr->ds_clp, hdr->lseg, | |
1197 | hdr->pgio_mirror_idx); | |
1198 | ||
1199 | switch (err) { | |
1200 | case -NFS4ERR_RESET_TO_PNFS: | |
1201 | case -NFS4ERR_RESET_TO_MDS: | |
1202 | inode = hdr->lseg->pls_layout->plh_inode; | |
1203 | pnfs_error_mark_layout_for_return(inode, hdr->lseg); | |
1204 | if (err == -NFS4ERR_RESET_TO_PNFS) { | |
1205 | pnfs_set_retry_layoutget(hdr->lseg->pls_layout); | |
1206 | ff_layout_reset_write(hdr, true); | |
1207 | } else { | |
1208 | pnfs_clear_retry_layoutget(hdr->lseg->pls_layout); | |
1209 | ff_layout_reset_write(hdr, false); | |
1210 | } | |
1211 | return task->tk_status; | |
1212 | case -EAGAIN: | |
1213 | rpc_restart_call_prepare(task); | |
1214 | return -EAGAIN; | |
1215 | } | |
1216 | ||
1217 | if (hdr->res.verf->committed == NFS_FILE_SYNC || | |
1218 | hdr->res.verf->committed == NFS_DATA_SYNC) | |
1219 | ff_layout_set_layoutcommit(hdr); | |
1220 | ||
54204010 PT |
1221 | /* zero out fattr since we don't care DS attr at all */ |
1222 | hdr->fattr.valid = 0; | |
69f230d9 PT |
1223 | if (task->tk_status >= 0) |
1224 | nfs_writeback_update_inode(hdr); | |
1225 | ||
d67ae825 TH |
1226 | return 0; |
1227 | } | |
1228 | ||
1229 | static int ff_layout_commit_done_cb(struct rpc_task *task, | |
1230 | struct nfs_commit_data *data) | |
1231 | { | |
1232 | struct inode *inode; | |
1233 | int err; | |
1234 | ||
1235 | trace_nfs4_pnfs_commit_ds(data, task->tk_status); | |
dd52128a | 1236 | if (task->tk_status < 0) |
d67ae825 TH |
1237 | ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index, |
1238 | data->args.offset, data->args.count, | |
dd52128a TM |
1239 | data->res.op_status, OP_COMMIT, |
1240 | task->tk_status); | |
d67ae825 TH |
1241 | err = ff_layout_async_handle_error(task, NULL, data->ds_clp, |
1242 | data->lseg, data->ds_commit_index); | |
1243 | ||
1244 | switch (err) { | |
1245 | case -NFS4ERR_RESET_TO_PNFS: | |
1246 | case -NFS4ERR_RESET_TO_MDS: | |
1247 | inode = data->lseg->pls_layout->plh_inode; | |
1248 | pnfs_error_mark_layout_for_return(inode, data->lseg); | |
1249 | if (err == -NFS4ERR_RESET_TO_PNFS) | |
1250 | pnfs_set_retry_layoutget(data->lseg->pls_layout); | |
1251 | else | |
1252 | pnfs_clear_retry_layoutget(data->lseg->pls_layout); | |
1253 | pnfs_generic_prepare_to_resend_writes(data); | |
1254 | return -EAGAIN; | |
1255 | case -EAGAIN: | |
1256 | rpc_restart_call_prepare(task); | |
1257 | return -EAGAIN; | |
1258 | } | |
1259 | ||
c0f5f505 TM |
1260 | if (data->verf.committed == NFS_UNSTABLE |
1261 | && ff_layout_need_layoutcommit(data->lseg)) | |
67af7611 | 1262 | pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb); |
d67ae825 TH |
1263 | |
1264 | return 0; | |
1265 | } | |
1266 | ||
1267 | static int ff_layout_write_prepare_common(struct rpc_task *task, | |
1268 | struct nfs_pgio_header *hdr) | |
1269 | { | |
abcb7bfc TM |
1270 | nfs4_ff_layout_stat_io_start_write( |
1271 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), | |
e76d28dd TM |
1272 | hdr->args.count, |
1273 | task->tk_start); | |
abcb7bfc | 1274 | |
d67ae825 TH |
1275 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { |
1276 | rpc_exit(task, -EIO); | |
1277 | return -EIO; | |
1278 | } | |
1279 | ||
1280 | if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) { | |
1281 | bool retry_pnfs; | |
1282 | ||
1283 | retry_pnfs = ff_layout_has_available_ds(hdr->lseg); | |
1284 | dprintk("%s task %u reset io to %s\n", __func__, | |
1285 | task->tk_pid, retry_pnfs ? "pNFS" : "MDS"); | |
1286 | ff_layout_reset_write(hdr, retry_pnfs); | |
1287 | rpc_exit(task, 0); | |
1288 | return -EAGAIN; | |
1289 | } | |
1290 | ||
1291 | return 0; | |
1292 | } | |
1293 | ||
1294 | static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data) | |
1295 | { | |
1296 | struct nfs_pgio_header *hdr = data; | |
1297 | ||
1298 | if (ff_layout_write_prepare_common(task, hdr)) | |
1299 | return; | |
1300 | ||
1301 | rpc_call_start(task); | |
1302 | } | |
1303 | ||
1304 | static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data) | |
1305 | { | |
1306 | struct nfs_pgio_header *hdr = data; | |
1307 | ||
d67ae825 TH |
1308 | if (ff_layout_setup_sequence(hdr->ds_clp, |
1309 | &hdr->args.seq_args, | |
1310 | &hdr->res.seq_res, | |
1311 | task)) | |
1312 | return; | |
1313 | ||
abcb7bfc TM |
1314 | if (ff_layout_write_prepare_common(task, hdr)) |
1315 | return; | |
1316 | ||
d67ae825 TH |
1317 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
1318 | hdr->args.lock_context, FMODE_WRITE) == -EIO) | |
1319 | rpc_exit(task, -EIO); /* lost lock, terminate I/O */ | |
1320 | } | |
1321 | ||
1322 | static void ff_layout_write_call_done(struct rpc_task *task, void *data) | |
1323 | { | |
1324 | struct nfs_pgio_header *hdr = data; | |
1325 | ||
abcb7bfc TM |
1326 | nfs4_ff_layout_stat_io_end_write(task, |
1327 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), | |
1328 | hdr->args.count, hdr->res.count, | |
1329 | hdr->res.verf->committed); | |
1330 | ||
d67ae825 TH |
1331 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && |
1332 | task->tk_status == 0) { | |
1333 | nfs4_sequence_done(task, &hdr->res.seq_res); | |
1334 | return; | |
1335 | } | |
1336 | ||
1337 | /* Note this may cause RPC to be resent */ | |
1338 | hdr->mds_ops->rpc_call_done(task, hdr); | |
1339 | } | |
1340 | ||
1341 | static void ff_layout_write_count_stats(struct rpc_task *task, void *data) | |
1342 | { | |
1343 | struct nfs_pgio_header *hdr = data; | |
1344 | ||
1345 | rpc_count_iostats_metrics(task, | |
1346 | &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]); | |
1347 | } | |
1348 | ||
abcb7bfc TM |
1349 | static void ff_layout_commit_prepare_common(struct rpc_task *task, |
1350 | struct nfs_commit_data *cdata) | |
1351 | { | |
1352 | nfs4_ff_layout_stat_io_start_write( | |
1353 | FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), | |
e76d28dd | 1354 | 0, task->tk_start); |
abcb7bfc TM |
1355 | } |
1356 | ||
d67ae825 TH |
1357 | static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data) |
1358 | { | |
abcb7bfc | 1359 | ff_layout_commit_prepare_common(task, data); |
d67ae825 TH |
1360 | rpc_call_start(task); |
1361 | } | |
1362 | ||
1363 | static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data) | |
1364 | { | |
1365 | struct nfs_commit_data *wdata = data; | |
1366 | ||
abcb7bfc | 1367 | if (ff_layout_setup_sequence(wdata->ds_clp, |
d67ae825 TH |
1368 | &wdata->args.seq_args, |
1369 | &wdata->res.seq_res, | |
abcb7bfc TM |
1370 | task)) |
1371 | return; | |
1372 | ff_layout_commit_prepare_common(task, data); | |
1373 | } | |
1374 | ||
1375 | static void ff_layout_commit_done(struct rpc_task *task, void *data) | |
1376 | { | |
1377 | struct nfs_commit_data *cdata = data; | |
1378 | struct nfs_page *req; | |
1379 | __u64 count = 0; | |
1380 | ||
1381 | if (task->tk_status == 0) { | |
1382 | list_for_each_entry(req, &cdata->pages, wb_list) | |
1383 | count += req->wb_bytes; | |
1384 | } | |
1385 | ||
1386 | nfs4_ff_layout_stat_io_end_write(task, | |
1387 | FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), | |
1388 | count, count, NFS_FILE_SYNC); | |
1389 | ||
1390 | pnfs_generic_write_commit_done(task, data); | |
d67ae825 TH |
1391 | } |
1392 | ||
1393 | static void ff_layout_commit_count_stats(struct rpc_task *task, void *data) | |
1394 | { | |
1395 | struct nfs_commit_data *cdata = data; | |
1396 | ||
1397 | rpc_count_iostats_metrics(task, | |
1398 | &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]); | |
1399 | } | |
1400 | ||
1401 | static const struct rpc_call_ops ff_layout_read_call_ops_v3 = { | |
1402 | .rpc_call_prepare = ff_layout_read_prepare_v3, | |
1403 | .rpc_call_done = ff_layout_read_call_done, | |
1404 | .rpc_count_stats = ff_layout_read_count_stats, | |
1405 | .rpc_release = pnfs_generic_rw_release, | |
1406 | }; | |
1407 | ||
1408 | static const struct rpc_call_ops ff_layout_read_call_ops_v4 = { | |
1409 | .rpc_call_prepare = ff_layout_read_prepare_v4, | |
1410 | .rpc_call_done = ff_layout_read_call_done, | |
1411 | .rpc_count_stats = ff_layout_read_count_stats, | |
1412 | .rpc_release = pnfs_generic_rw_release, | |
1413 | }; | |
1414 | ||
1415 | static const struct rpc_call_ops ff_layout_write_call_ops_v3 = { | |
1416 | .rpc_call_prepare = ff_layout_write_prepare_v3, | |
1417 | .rpc_call_done = ff_layout_write_call_done, | |
1418 | .rpc_count_stats = ff_layout_write_count_stats, | |
1419 | .rpc_release = pnfs_generic_rw_release, | |
1420 | }; | |
1421 | ||
1422 | static const struct rpc_call_ops ff_layout_write_call_ops_v4 = { | |
1423 | .rpc_call_prepare = ff_layout_write_prepare_v4, | |
1424 | .rpc_call_done = ff_layout_write_call_done, | |
1425 | .rpc_count_stats = ff_layout_write_count_stats, | |
1426 | .rpc_release = pnfs_generic_rw_release, | |
1427 | }; | |
1428 | ||
1429 | static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = { | |
1430 | .rpc_call_prepare = ff_layout_commit_prepare_v3, | |
abcb7bfc | 1431 | .rpc_call_done = ff_layout_commit_done, |
d67ae825 TH |
1432 | .rpc_count_stats = ff_layout_commit_count_stats, |
1433 | .rpc_release = pnfs_generic_commit_release, | |
1434 | }; | |
1435 | ||
1436 | static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = { | |
1437 | .rpc_call_prepare = ff_layout_commit_prepare_v4, | |
abcb7bfc | 1438 | .rpc_call_done = ff_layout_commit_done, |
d67ae825 TH |
1439 | .rpc_count_stats = ff_layout_commit_count_stats, |
1440 | .rpc_release = pnfs_generic_commit_release, | |
1441 | }; | |
1442 | ||
1443 | static enum pnfs_try_status | |
1444 | ff_layout_read_pagelist(struct nfs_pgio_header *hdr) | |
1445 | { | |
1446 | struct pnfs_layout_segment *lseg = hdr->lseg; | |
1447 | struct nfs4_pnfs_ds *ds; | |
1448 | struct rpc_clnt *ds_clnt; | |
1449 | struct rpc_cred *ds_cred; | |
1450 | loff_t offset = hdr->args.offset; | |
1451 | u32 idx = hdr->pgio_mirror_idx; | |
1452 | int vers; | |
1453 | struct nfs_fh *fh; | |
1454 | ||
1455 | dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n", | |
1456 | __func__, hdr->inode->i_ino, | |
1457 | hdr->args.pgbase, (size_t)hdr->args.count, offset); | |
1458 | ||
1459 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, false); | |
1460 | if (!ds) | |
1461 | goto out_failed; | |
1462 | ||
1463 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, | |
1464 | hdr->inode); | |
1465 | if (IS_ERR(ds_clnt)) | |
1466 | goto out_failed; | |
1467 | ||
1468 | ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); | |
1469 | if (IS_ERR(ds_cred)) | |
1470 | goto out_failed; | |
1471 | ||
1472 | vers = nfs4_ff_layout_ds_version(lseg, idx); | |
1473 | ||
1474 | dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__, | |
1475 | ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers); | |
1476 | ||
1477 | atomic_inc(&ds->ds_clp->cl_count); | |
1478 | hdr->ds_clp = ds->ds_clp; | |
1479 | fh = nfs4_ff_layout_select_ds_fh(lseg, idx); | |
1480 | if (fh) | |
1481 | hdr->args.fh = fh; | |
d67ae825 TH |
1482 | /* |
1483 | * Note that if we ever decide to split across DSes, | |
1484 | * then we may need to handle dense-like offsets. | |
1485 | */ | |
1486 | hdr->args.offset = offset; | |
1487 | hdr->mds_offset = offset; | |
1488 | ||
1489 | /* Perform an asynchronous read to ds */ | |
1490 | nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, | |
1491 | vers == 3 ? &ff_layout_read_call_ops_v3 : | |
1492 | &ff_layout_read_call_ops_v4, | |
1493 | 0, RPC_TASK_SOFTCONN); | |
1494 | ||
1495 | return PNFS_ATTEMPTED; | |
1496 | ||
1497 | out_failed: | |
1498 | if (ff_layout_has_available_ds(lseg)) | |
1499 | return PNFS_TRY_AGAIN; | |
1500 | return PNFS_NOT_ATTEMPTED; | |
1501 | } | |
1502 | ||
1503 | /* Perform async writes. */ | |
1504 | static enum pnfs_try_status | |
1505 | ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) | |
1506 | { | |
1507 | struct pnfs_layout_segment *lseg = hdr->lseg; | |
1508 | struct nfs4_pnfs_ds *ds; | |
1509 | struct rpc_clnt *ds_clnt; | |
1510 | struct rpc_cred *ds_cred; | |
1511 | loff_t offset = hdr->args.offset; | |
1512 | int vers; | |
1513 | struct nfs_fh *fh; | |
1514 | int idx = hdr->pgio_mirror_idx; | |
1515 | ||
1516 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); | |
1517 | if (!ds) | |
1518 | return PNFS_NOT_ATTEMPTED; | |
1519 | ||
1520 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, | |
1521 | hdr->inode); | |
1522 | if (IS_ERR(ds_clnt)) | |
1523 | return PNFS_NOT_ATTEMPTED; | |
1524 | ||
1525 | ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); | |
1526 | if (IS_ERR(ds_cred)) | |
1527 | return PNFS_NOT_ATTEMPTED; | |
1528 | ||
1529 | vers = nfs4_ff_layout_ds_version(lseg, idx); | |
1530 | ||
1531 | dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n", | |
1532 | __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count, | |
1533 | offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), | |
1534 | vers); | |
1535 | ||
1536 | hdr->pgio_done_cb = ff_layout_write_done_cb; | |
1537 | atomic_inc(&ds->ds_clp->cl_count); | |
1538 | hdr->ds_clp = ds->ds_clp; | |
1539 | hdr->ds_commit_idx = idx; | |
1540 | fh = nfs4_ff_layout_select_ds_fh(lseg, idx); | |
1541 | if (fh) | |
1542 | hdr->args.fh = fh; | |
1543 | ||
1544 | /* | |
1545 | * Note that if we ever decide to split across DSes, | |
1546 | * then we may need to handle dense-like offsets. | |
1547 | */ | |
1548 | hdr->args.offset = offset; | |
1549 | ||
1550 | /* Perform an asynchronous write */ | |
1551 | nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, | |
1552 | vers == 3 ? &ff_layout_write_call_ops_v3 : | |
1553 | &ff_layout_write_call_ops_v4, | |
1554 | sync, RPC_TASK_SOFTCONN); | |
1555 | return PNFS_ATTEMPTED; | |
1556 | } | |
1557 | ||
d67ae825 TH |
1558 | static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i) |
1559 | { | |
1560 | return i; | |
1561 | } | |
1562 | ||
1563 | static struct nfs_fh * | |
1564 | select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i) | |
1565 | { | |
1566 | struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg); | |
1567 | ||
1568 | /* FIXME: Assume that there is only one NFS version available | |
1569 | * for the DS. | |
1570 | */ | |
1571 | return &flseg->mirror_array[i]->fh_versions[0]; | |
1572 | } | |
1573 | ||
1574 | static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) | |
1575 | { | |
1576 | struct pnfs_layout_segment *lseg = data->lseg; | |
1577 | struct nfs4_pnfs_ds *ds; | |
1578 | struct rpc_clnt *ds_clnt; | |
1579 | struct rpc_cred *ds_cred; | |
1580 | u32 idx; | |
1581 | int vers; | |
1582 | struct nfs_fh *fh; | |
1583 | ||
1584 | idx = calc_ds_index_from_commit(lseg, data->ds_commit_index); | |
1585 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); | |
1586 | if (!ds) | |
1587 | goto out_err; | |
1588 | ||
1589 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, | |
1590 | data->inode); | |
1591 | if (IS_ERR(ds_clnt)) | |
1592 | goto out_err; | |
1593 | ||
1594 | ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred); | |
1595 | if (IS_ERR(ds_cred)) | |
1596 | goto out_err; | |
1597 | ||
1598 | vers = nfs4_ff_layout_ds_version(lseg, idx); | |
1599 | ||
1600 | dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__, | |
1601 | data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count), | |
1602 | vers); | |
1603 | data->commit_done_cb = ff_layout_commit_done_cb; | |
1604 | data->cred = ds_cred; | |
1605 | atomic_inc(&ds->ds_clp->cl_count); | |
1606 | data->ds_clp = ds->ds_clp; | |
1607 | fh = select_ds_fh_from_commit(lseg, data->ds_commit_index); | |
1608 | if (fh) | |
1609 | data->args.fh = fh; | |
abcb7bfc | 1610 | |
d67ae825 TH |
1611 | return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops, |
1612 | vers == 3 ? &ff_layout_commit_call_ops_v3 : | |
1613 | &ff_layout_commit_call_ops_v4, | |
1614 | how, RPC_TASK_SOFTCONN); | |
1615 | out_err: | |
1616 | pnfs_generic_prepare_to_resend_writes(data); | |
1617 | pnfs_generic_commit_release(data); | |
1618 | return -EAGAIN; | |
1619 | } | |
1620 | ||
1621 | static int | |
1622 | ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages, | |
1623 | int how, struct nfs_commit_info *cinfo) | |
1624 | { | |
1625 | return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo, | |
1626 | ff_layout_initiate_commit); | |
1627 | } | |
1628 | ||
1629 | static struct pnfs_ds_commit_info * | |
1630 | ff_layout_get_ds_info(struct inode *inode) | |
1631 | { | |
1632 | struct pnfs_layout_hdr *layout = NFS_I(inode)->layout; | |
1633 | ||
1634 | if (layout == NULL) | |
1635 | return NULL; | |
1636 | ||
1637 | return &FF_LAYOUT_FROM_HDR(layout)->commit_info; | |
1638 | } | |
1639 | ||
1640 | static void | |
fc87701b | 1641 | ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d) |
d67ae825 TH |
1642 | { |
1643 | nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds, | |
1644 | id_node)); | |
1645 | } | |
1646 | ||
1647 | static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo, | |
1648 | struct xdr_stream *xdr, | |
1649 | const struct nfs4_layoutreturn_args *args) | |
1650 | { | |
1651 | struct pnfs_layout_hdr *hdr = &flo->generic_hdr; | |
1652 | __be32 *start; | |
1653 | int count = 0, ret = 0; | |
1654 | ||
1655 | start = xdr_reserve_space(xdr, 4); | |
1656 | if (unlikely(!start)) | |
1657 | return -E2BIG; | |
1658 | ||
1659 | /* This assume we always return _ALL_ layouts */ | |
1660 | spin_lock(&hdr->plh_inode->i_lock); | |
1661 | ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range); | |
1662 | spin_unlock(&hdr->plh_inode->i_lock); | |
1663 | ||
1664 | *start = cpu_to_be32(count); | |
1665 | ||
1666 | return ret; | |
1667 | } | |
1668 | ||
1669 | /* report nothing for now */ | |
1670 | static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo, | |
1671 | struct xdr_stream *xdr, | |
1672 | const struct nfs4_layoutreturn_args *args) | |
1673 | { | |
1674 | __be32 *p; | |
1675 | ||
1676 | p = xdr_reserve_space(xdr, 4); | |
1677 | if (likely(p)) | |
1678 | *p = cpu_to_be32(0); | |
1679 | } | |
1680 | ||
1681 | static struct nfs4_deviceid_node * | |
1682 | ff_layout_alloc_deviceid_node(struct nfs_server *server, | |
1683 | struct pnfs_device *pdev, gfp_t gfp_flags) | |
1684 | { | |
1685 | struct nfs4_ff_layout_ds *dsaddr; | |
1686 | ||
1687 | dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags); | |
1688 | if (!dsaddr) | |
1689 | return NULL; | |
1690 | return &dsaddr->id_node; | |
1691 | } | |
1692 | ||
1693 | static void | |
1694 | ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo, | |
1695 | struct xdr_stream *xdr, | |
1696 | const struct nfs4_layoutreturn_args *args) | |
1697 | { | |
1698 | struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo); | |
1699 | __be32 *start; | |
1700 | ||
1701 | dprintk("%s: Begin\n", __func__); | |
1702 | start = xdr_reserve_space(xdr, 4); | |
1703 | BUG_ON(!start); | |
1704 | ||
1705 | if (ff_layout_encode_ioerr(flo, xdr, args)) | |
1706 | goto out; | |
1707 | ||
1708 | ff_layout_encode_iostats(flo, xdr, args); | |
1709 | out: | |
1710 | *start = cpu_to_be32((xdr->p - start - 1) * 4); | |
1711 | dprintk("%s: Return\n", __func__); | |
1712 | } | |
1713 | ||
27c43064 PT |
1714 | static int |
1715 | ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen) | |
1716 | { | |
1717 | const struct sockaddr_in *sin = (struct sockaddr_in *)sap; | |
1718 | ||
1719 | return snprintf(buf, buflen, "%pI4", &sin->sin_addr); | |
1720 | } | |
1721 | ||
1722 | static size_t | |
1723 | ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf, | |
1724 | const int buflen) | |
1725 | { | |
1726 | const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; | |
1727 | const struct in6_addr *addr = &sin6->sin6_addr; | |
1728 | ||
1729 | /* | |
1730 | * RFC 4291, Section 2.2.2 | |
1731 | * | |
1732 | * Shorthanded ANY address | |
1733 | */ | |
1734 | if (ipv6_addr_any(addr)) | |
1735 | return snprintf(buf, buflen, "::"); | |
1736 | ||
1737 | /* | |
1738 | * RFC 4291, Section 2.2.2 | |
1739 | * | |
1740 | * Shorthanded loopback address | |
1741 | */ | |
1742 | if (ipv6_addr_loopback(addr)) | |
1743 | return snprintf(buf, buflen, "::1"); | |
1744 | ||
1745 | /* | |
1746 | * RFC 4291, Section 2.2.3 | |
1747 | * | |
1748 | * Special presentation address format for mapped v4 | |
1749 | * addresses. | |
1750 | */ | |
1751 | if (ipv6_addr_v4mapped(addr)) | |
1752 | return snprintf(buf, buflen, "::ffff:%pI4", | |
1753 | &addr->s6_addr32[3]); | |
1754 | ||
1755 | /* | |
1756 | * RFC 4291, Section 2.2.1 | |
1757 | */ | |
1758 | return snprintf(buf, buflen, "%pI6c", addr); | |
1759 | } | |
1760 | ||
1761 | /* Derived from rpc_sockaddr2uaddr */ | |
1762 | static void | |
1763 | ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da) | |
1764 | { | |
1765 | struct sockaddr *sap = (struct sockaddr *)&da->da_addr; | |
1766 | char portbuf[RPCBIND_MAXUADDRPLEN]; | |
1767 | char addrbuf[RPCBIND_MAXUADDRLEN]; | |
1768 | char *netid; | |
1769 | unsigned short port; | |
1770 | int len, netid_len; | |
1771 | __be32 *p; | |
1772 | ||
1773 | switch (sap->sa_family) { | |
1774 | case AF_INET: | |
1775 | if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0) | |
1776 | return; | |
1777 | port = ntohs(((struct sockaddr_in *)sap)->sin_port); | |
1778 | netid = "tcp"; | |
1779 | netid_len = 3; | |
1780 | break; | |
1781 | case AF_INET6: | |
1782 | if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0) | |
1783 | return; | |
1784 | port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port); | |
1785 | netid = "tcp6"; | |
1786 | netid_len = 4; | |
1787 | break; | |
1788 | default: | |
1789 | /* we only support tcp and tcp6 */ | |
1790 | WARN_ON_ONCE(1); | |
1791 | return; | |
1792 | } | |
1793 | ||
1794 | snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff); | |
1795 | len = strlcat(addrbuf, portbuf, sizeof(addrbuf)); | |
1796 | ||
1797 | p = xdr_reserve_space(xdr, 4 + netid_len); | |
1798 | xdr_encode_opaque(p, netid, netid_len); | |
1799 | ||
1800 | p = xdr_reserve_space(xdr, 4 + len); | |
1801 | xdr_encode_opaque(p, addrbuf, len); | |
1802 | } | |
1803 | ||
1804 | static void | |
1805 | ff_layout_encode_nfstime(struct xdr_stream *xdr, | |
1806 | ktime_t t) | |
1807 | { | |
1808 | struct timespec64 ts; | |
1809 | __be32 *p; | |
1810 | ||
1811 | p = xdr_reserve_space(xdr, 12); | |
1812 | ts = ktime_to_timespec64(t); | |
1813 | p = xdr_encode_hyper(p, ts.tv_sec); | |
1814 | *p++ = cpu_to_be32(ts.tv_nsec); | |
1815 | } | |
1816 | ||
1817 | static void | |
1818 | ff_layout_encode_io_latency(struct xdr_stream *xdr, | |
1819 | struct nfs4_ff_io_stat *stat) | |
1820 | { | |
1821 | __be32 *p; | |
1822 | ||
1823 | p = xdr_reserve_space(xdr, 5 * 8); | |
1824 | p = xdr_encode_hyper(p, stat->ops_requested); | |
1825 | p = xdr_encode_hyper(p, stat->bytes_requested); | |
1826 | p = xdr_encode_hyper(p, stat->ops_completed); | |
1827 | p = xdr_encode_hyper(p, stat->bytes_completed); | |
1828 | p = xdr_encode_hyper(p, stat->bytes_not_delivered); | |
1829 | ff_layout_encode_nfstime(xdr, stat->total_busy_time); | |
1830 | ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time); | |
1831 | } | |
1832 | ||
1833 | static void | |
1834 | ff_layout_encode_layoutstats(struct xdr_stream *xdr, | |
1835 | struct nfs42_layoutstat_args *args, | |
1836 | struct nfs42_layoutstat_devinfo *devinfo) | |
1837 | { | |
1838 | struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private; | |
1839 | struct nfs4_pnfs_ds_addr *da; | |
1840 | struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds; | |
1841 | struct nfs_fh *fh = &mirror->fh_versions[0]; | |
1842 | __be32 *p, *start; | |
1843 | ||
1844 | da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node); | |
1845 | dprintk("%s: DS %s: encoding address %s\n", | |
1846 | __func__, ds->ds_remotestr, da->da_remotestr); | |
1847 | /* layoutupdate length */ | |
1848 | start = xdr_reserve_space(xdr, 4); | |
1849 | /* netaddr4 */ | |
1850 | ff_layout_encode_netaddr(xdr, da); | |
1851 | /* nfs_fh4 */ | |
1852 | p = xdr_reserve_space(xdr, 4 + fh->size); | |
1853 | xdr_encode_opaque(p, fh->data, fh->size); | |
1854 | /* ff_io_latency4 read */ | |
1855 | spin_lock(&mirror->lock); | |
1856 | ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat); | |
1857 | /* ff_io_latency4 write */ | |
1858 | ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat); | |
1859 | spin_unlock(&mirror->lock); | |
1860 | /* nfstime4 */ | |
1861 | ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time)); | |
1862 | /* bool */ | |
1863 | p = xdr_reserve_space(xdr, 4); | |
1864 | *p = cpu_to_be32(false); | |
1865 | ||
1866 | *start = cpu_to_be32((xdr->p - start - 1) * 4); | |
1867 | } | |
1868 | ||
ad4dc53e PT |
1869 | static bool |
1870 | ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args, | |
1871 | struct pnfs_layout_segment *pls, | |
1872 | int *dev_count, int dev_limit) | |
1873 | { | |
1874 | struct nfs4_ff_layout_mirror *mirror; | |
1875 | struct nfs4_deviceid_node *dev; | |
1876 | struct nfs42_layoutstat_devinfo *devinfo; | |
1877 | int i; | |
1878 | ||
690edcfa | 1879 | for (i = 0; i < FF_LAYOUT_MIRROR_COUNT(pls); i++) { |
ad4dc53e PT |
1880 | if (*dev_count >= dev_limit) |
1881 | break; | |
1882 | mirror = FF_LAYOUT_COMP(pls, i); | |
27c43064 PT |
1883 | if (!mirror || !mirror->mirror_ds) |
1884 | continue; | |
ad4dc53e PT |
1885 | dev = FF_LAYOUT_DEVID_NODE(pls, i); |
1886 | devinfo = &args->devinfo[*dev_count]; | |
1887 | memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE); | |
1888 | devinfo->offset = pls->pls_range.offset; | |
1889 | devinfo->length = pls->pls_range.length; | |
d099d7b8 | 1890 | devinfo->read_count = mirror->read_stat.io_stat.ops_completed; |
ad4dc53e | 1891 | devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed; |
d099d7b8 | 1892 | devinfo->write_count = mirror->write_stat.io_stat.ops_completed; |
ad4dc53e PT |
1893 | devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed; |
1894 | devinfo->layout_type = LAYOUT_FLEX_FILES; | |
27c43064 PT |
1895 | devinfo->layoutstats_encode = ff_layout_encode_layoutstats; |
1896 | devinfo->layout_private = mirror; | |
1897 | /* lseg refcount put in cleanup_layoutstats */ | |
1898 | pnfs_get_lseg(pls); | |
ad4dc53e PT |
1899 | |
1900 | ++(*dev_count); | |
1901 | } | |
1902 | ||
1903 | return *dev_count < dev_limit; | |
1904 | } | |
1905 | ||
1906 | static int | |
1907 | ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args) | |
1908 | { | |
1909 | struct pnfs_layout_segment *pls; | |
1910 | int dev_count = 0; | |
1911 | ||
1912 | spin_lock(&args->inode->i_lock); | |
1913 | list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) { | |
1914 | dev_count += FF_LAYOUT_MIRROR_COUNT(pls); | |
1915 | } | |
1916 | spin_unlock(&args->inode->i_lock); | |
1917 | /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */ | |
1918 | if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) { | |
1919 | dprintk("%s: truncating devinfo to limit (%d:%d)\n", | |
1920 | __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV); | |
1921 | dev_count = PNFS_LAYOUTSTATS_MAXDEV; | |
1922 | } | |
1923 | args->devinfo = kmalloc(dev_count * sizeof(*args->devinfo), GFP_KERNEL); | |
1924 | if (!args->devinfo) | |
1925 | return -ENOMEM; | |
1926 | ||
1927 | dev_count = 0; | |
1928 | spin_lock(&args->inode->i_lock); | |
1929 | list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) { | |
1930 | if (!ff_layout_mirror_prepare_stats(args, pls, &dev_count, | |
1931 | PNFS_LAYOUTSTATS_MAXDEV)) { | |
1932 | break; | |
1933 | } | |
1934 | } | |
1935 | spin_unlock(&args->inode->i_lock); | |
1936 | args->num_dev = dev_count; | |
1937 | ||
1938 | return 0; | |
1939 | } | |
1940 | ||
27c43064 PT |
1941 | static void |
1942 | ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data) | |
1943 | { | |
1944 | struct nfs4_ff_layout_mirror *mirror; | |
1945 | int i; | |
1946 | ||
1947 | for (i = 0; i < data->args.num_dev; i++) { | |
1948 | mirror = data->args.devinfo[i].layout_private; | |
1949 | data->args.devinfo[i].layout_private = NULL; | |
1950 | pnfs_put_lseg(mirror->lseg); | |
1951 | } | |
1952 | } | |
1953 | ||
d67ae825 TH |
1954 | static struct pnfs_layoutdriver_type flexfilelayout_type = { |
1955 | .id = LAYOUT_FLEX_FILES, | |
1956 | .name = "LAYOUT_FLEX_FILES", | |
1957 | .owner = THIS_MODULE, | |
1958 | .alloc_layout_hdr = ff_layout_alloc_layout_hdr, | |
1959 | .free_layout_hdr = ff_layout_free_layout_hdr, | |
1960 | .alloc_lseg = ff_layout_alloc_lseg, | |
1961 | .free_lseg = ff_layout_free_lseg, | |
1962 | .pg_read_ops = &ff_layout_pg_read_ops, | |
1963 | .pg_write_ops = &ff_layout_pg_write_ops, | |
1964 | .get_ds_info = ff_layout_get_ds_info, | |
fc87701b | 1965 | .free_deviceid_node = ff_layout_free_deviceid_node, |
338d00cf | 1966 | .mark_request_commit = pnfs_layout_mark_request_commit, |
d67ae825 TH |
1967 | .clear_request_commit = pnfs_generic_clear_request_commit, |
1968 | .scan_commit_lists = pnfs_generic_scan_commit_lists, | |
1969 | .recover_commit_reqs = pnfs_generic_recover_commit_reqs, | |
1970 | .commit_pagelist = ff_layout_commit_pagelist, | |
1971 | .read_pagelist = ff_layout_read_pagelist, | |
1972 | .write_pagelist = ff_layout_write_pagelist, | |
1973 | .alloc_deviceid_node = ff_layout_alloc_deviceid_node, | |
1974 | .encode_layoutreturn = ff_layout_encode_layoutreturn, | |
5bb89b47 | 1975 | .sync = pnfs_nfs_generic_sync, |
ad4dc53e | 1976 | .prepare_layoutstats = ff_layout_prepare_layoutstats, |
27c43064 | 1977 | .cleanup_layoutstats = ff_layout_cleanup_layoutstats, |
d67ae825 TH |
1978 | }; |
1979 | ||
1980 | static int __init nfs4flexfilelayout_init(void) | |
1981 | { | |
1982 | printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n", | |
1983 | __func__); | |
1984 | return pnfs_register_layoutdriver(&flexfilelayout_type); | |
1985 | } | |
1986 | ||
1987 | static void __exit nfs4flexfilelayout_exit(void) | |
1988 | { | |
1989 | printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n", | |
1990 | __func__); | |
1991 | pnfs_unregister_layoutdriver(&flexfilelayout_type); | |
1992 | } | |
1993 | ||
1994 | MODULE_ALIAS("nfs-layouttype4-4"); | |
1995 | ||
1996 | MODULE_LICENSE("GPL"); | |
1997 | MODULE_DESCRIPTION("The NFSv4 flexfile layout driver"); | |
1998 | ||
1999 | module_init(nfs4flexfilelayout_init); | |
2000 | module_exit(nfs4flexfilelayout_exit); |