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