blob: 4d64d5b85e56b54d2d5ccc4f9be0138a94f1ad4b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Module for the pnfs nfs4 file layout driver.
3 * Defines all I/O and Policy interface operations, plus code
4 * to register itself with the pNFS client.
5 *
6 * Copyright (c) 2002
7 * The Regents of the University of Michigan
8 * All Rights Reserved
9 *
10 * Dean Hildebrand <dhildebz@umich.edu>
11 *
12 * Permission is granted to use, copy, create derivative works, and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the University of Michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. If
17 * the above copyright notice or any other identification of the
18 * University of Michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
20 *
21 * This software is provided as is, without representation or warranty
22 * of any kind either express or implied, including without limitation
23 * the implied warranties of merchantability, fitness for a particular
24 * purpose, or noninfringement. The Regents of the University of
25 * Michigan shall not be liable for any damages, including special,
26 * indirect, incidental, or consequential damages, with respect to any
27 * claim arising out of or in connection with the use of the software,
28 * even if it has been or is hereafter advised of the possibility of
29 * such damages.
30 */
31
32#include <linux/nfs_fs.h>
33#include <linux/nfs_page.h>
34#include <linux/module.h>
35
36#include <linux/sunrpc/metrics.h>
37
38#include "internal.h"
39#include "delegation.h"
40#include "nfs4filelayout.h"
41
42#define NFSDBG_FACILITY NFSDBG_PNFS_LD
43
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
46MODULE_DESCRIPTION("The NFSv4 file layout driver");
47
48#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
49
50static loff_t
51filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
52 loff_t offset)
53{
54 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
55 u64 stripe_no;
56 u32 rem;
57
58 offset -= flseg->pattern_offset;
59 stripe_no = div_u64(offset, stripe_width);
60 div_u64_rem(offset, flseg->stripe_unit, &rem);
61
62 return stripe_no * flseg->stripe_unit + rem;
63}
64
65/* This function is used by the layout driver to calculate the
66 * offset of the file on the dserver based on whether the
67 * layout type is STRIPE_DENSE or STRIPE_SPARSE
68 */
69static loff_t
70filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
71{
72 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
73
74 switch (flseg->stripe_type) {
75 case STRIPE_SPARSE:
76 return offset;
77
78 case STRIPE_DENSE:
79 return filelayout_get_dense_offset(flseg, offset);
80 }
81
82 BUG();
83}
84
85static int filelayout_async_handle_error(struct rpc_task *task,
86 struct nfs4_state *state,
87 struct nfs_client *clp,
88 int *reset)
89{
90 struct nfs_server *mds_server = NFS_SERVER(state->inode);
91 struct nfs_client *mds_client = mds_server->nfs_client;
92
93 if (task->tk_status >= 0)
94 return 0;
95 *reset = 0;
96
97 switch (task->tk_status) {
98 /* MDS state errors */
99 case -NFS4ERR_DELEG_REVOKED:
100 case -NFS4ERR_ADMIN_REVOKED:
101 case -NFS4ERR_BAD_STATEID:
102 nfs_remove_bad_delegation(state->inode);
103 case -NFS4ERR_OPENMODE:
104 nfs4_schedule_stateid_recovery(mds_server, state);
105 goto wait_on_recovery;
106 case -NFS4ERR_EXPIRED:
107 nfs4_schedule_stateid_recovery(mds_server, state);
108 nfs4_schedule_lease_recovery(mds_client);
109 goto wait_on_recovery;
110 /* DS session errors */
111 case -NFS4ERR_BADSESSION:
112 case -NFS4ERR_BADSLOT:
113 case -NFS4ERR_BAD_HIGH_SLOT:
114 case -NFS4ERR_DEADSESSION:
115 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
116 case -NFS4ERR_SEQ_FALSE_RETRY:
117 case -NFS4ERR_SEQ_MISORDERED:
118 dprintk("%s ERROR %d, Reset session. Exchangeid "
119 "flags 0x%x\n", __func__, task->tk_status,
120 clp->cl_exchange_flags);
121 nfs4_schedule_session_recovery(clp->cl_session);
122 break;
123 case -NFS4ERR_DELAY:
124 case -NFS4ERR_GRACE:
125 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
126 break;
127 case -NFS4ERR_RETRY_UNCACHED_REP:
128 break;
129 default:
130 dprintk("%s DS error. Retry through MDS %d\n", __func__,
131 task->tk_status);
132 *reset = 1;
133 break;
134 }
135out:
136 task->tk_status = 0;
137 return -EAGAIN;
138wait_on_recovery:
139 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
140 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
141 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
142 goto out;
143}
144
145/* NFS_PROTO call done callback routines */
146
147static int filelayout_read_done_cb(struct rpc_task *task,
148 struct nfs_read_data *data)
149{
150 int reset = 0;
151
152 dprintk("%s DS read\n", __func__);
153
154 if (filelayout_async_handle_error(task, data->args.context->state,
155 data->ds_clp, &reset) == -EAGAIN) {
156 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
157 __func__, data->ds_clp, data->ds_clp->cl_session);
158 if (reset) {
159 pnfs_set_lo_fail(data->lseg);
160 nfs4_reset_read(task, data);
161 }
162 rpc_restart_call_prepare(task);
163 return -EAGAIN;
164 }
165
166 return 0;
167}
168
169/*
170 * We reference the rpc_cred of the first WRITE that triggers the need for
171 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
172 * rfc5661 is not clear about which credential should be used.
173 */
174static void
175filelayout_set_layoutcommit(struct nfs_write_data *wdata)
176{
177 if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
178 wdata->res.verf->committed == NFS_FILE_SYNC)
179 return;
180
181 pnfs_set_layoutcommit(wdata);
182 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
183 (unsigned long) NFS_I(wdata->inode)->layout->plh_lwb);
184}
185
186/*
187 * Call ops for the async read/write cases
188 * In the case of dense layouts, the offset needs to be reset to its
189 * original value.
190 */
191static void filelayout_read_prepare(struct rpc_task *task, void *data)
192{
193 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
194
195 rdata->read_done_cb = filelayout_read_done_cb;
196
197 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
198 &rdata->args.seq_args, &rdata->res.seq_res,
199 task))
200 return;
201
202 rpc_call_start(task);
203}
204
205static void filelayout_read_call_done(struct rpc_task *task, void *data)
206{
207 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
208
209 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
210
211 /* Note this may cause RPC to be resent */
212 rdata->mds_ops->rpc_call_done(task, data);
213}
214
215static void filelayout_read_count_stats(struct rpc_task *task, void *data)
216{
217 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
218
219 rpc_count_iostats(task, NFS_SERVER(rdata->inode)->client->cl_metrics);
220}
221
222static void filelayout_read_release(void *data)
223{
224 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
225
226 put_lseg(rdata->lseg);
227 rdata->mds_ops->rpc_release(data);
228}
229
230static int filelayout_write_done_cb(struct rpc_task *task,
231 struct nfs_write_data *data)
232{
233 int reset = 0;
234
235 if (filelayout_async_handle_error(task, data->args.context->state,
236 data->ds_clp, &reset) == -EAGAIN) {
237 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
238 __func__, data->ds_clp, data->ds_clp->cl_session);
239 if (reset) {
240 pnfs_set_lo_fail(data->lseg);
241 nfs4_reset_write(task, data);
242 }
243 rpc_restart_call_prepare(task);
244 return -EAGAIN;
245 }
246
247 filelayout_set_layoutcommit(data);
248 return 0;
249}
250
251/* Fake up some data that will cause nfs_commit_release to retry the writes. */
252static void prepare_to_resend_writes(struct nfs_write_data *data)
253{
254 struct nfs_page *first = nfs_list_entry(data->pages.next);
255
256 data->task.tk_status = 0;
257 memcpy(data->verf.verifier, first->wb_verf.verifier,
258 sizeof(first->wb_verf.verifier));
259 data->verf.verifier[0]++; /* ensure verifier mismatch */
260}
261
262static int filelayout_commit_done_cb(struct rpc_task *task,
263 struct nfs_write_data *data)
264{
265 int reset = 0;
266
267 if (filelayout_async_handle_error(task, data->args.context->state,
268 data->ds_clp, &reset) == -EAGAIN) {
269 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
270 __func__, data->ds_clp, data->ds_clp->cl_session);
271 if (reset) {
272 prepare_to_resend_writes(data);
273 pnfs_set_lo_fail(data->lseg);
274 } else
275 rpc_restart_call_prepare(task);
276 return -EAGAIN;
277 }
278
279 return 0;
280}
281
282static void filelayout_write_prepare(struct rpc_task *task, void *data)
283{
284 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
285
286 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
287 &wdata->args.seq_args, &wdata->res.seq_res,
288 task))
289 return;
290
291 rpc_call_start(task);
292}
293
294static void filelayout_write_call_done(struct rpc_task *task, void *data)
295{
296 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
297
298 /* Note this may cause RPC to be resent */
299 wdata->mds_ops->rpc_call_done(task, data);
300}
301
302static void filelayout_write_count_stats(struct rpc_task *task, void *data)
303{
304 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
305
306 rpc_count_iostats(task, NFS_SERVER(wdata->inode)->client->cl_metrics);
307}
308
309static void filelayout_write_release(void *data)
310{
311 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
312
313 put_lseg(wdata->lseg);
314 wdata->mds_ops->rpc_release(data);
315}
316
317static void filelayout_commit_release(void *data)
318{
319 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
320
321 nfs_commit_release_pages(wdata);
322 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
323 nfs_commit_clear_lock(NFS_I(wdata->inode));
324 put_lseg(wdata->lseg);
325 nfs_commitdata_release(wdata);
326}
327
328static const struct rpc_call_ops filelayout_read_call_ops = {
329 .rpc_call_prepare = filelayout_read_prepare,
330 .rpc_call_done = filelayout_read_call_done,
331 .rpc_count_stats = filelayout_read_count_stats,
332 .rpc_release = filelayout_read_release,
333};
334
335static const struct rpc_call_ops filelayout_write_call_ops = {
336 .rpc_call_prepare = filelayout_write_prepare,
337 .rpc_call_done = filelayout_write_call_done,
338 .rpc_count_stats = filelayout_write_count_stats,
339 .rpc_release = filelayout_write_release,
340};
341
342static const struct rpc_call_ops filelayout_commit_call_ops = {
343 .rpc_call_prepare = filelayout_write_prepare,
344 .rpc_call_done = filelayout_write_call_done,
345 .rpc_count_stats = filelayout_write_count_stats,
346 .rpc_release = filelayout_commit_release,
347};
348
349static enum pnfs_try_status
350filelayout_read_pagelist(struct nfs_read_data *data)
351{
352 struct pnfs_layout_segment *lseg = data->lseg;
353 struct nfs4_pnfs_ds *ds;
354 loff_t offset = data->args.offset;
355 u32 j, idx;
356 struct nfs_fh *fh;
357 int status;
358
359 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
360 __func__, data->inode->i_ino,
361 data->args.pgbase, (size_t)data->args.count, offset);
362
363 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
364 return PNFS_NOT_ATTEMPTED;
365
366 /* Retrieve the correct rpc_client for the byte range */
367 j = nfs4_fl_calc_j_index(lseg, offset);
368 idx = nfs4_fl_calc_ds_index(lseg, j);
369 ds = nfs4_fl_prepare_ds(lseg, idx);
370 if (!ds) {
371 /* Either layout fh index faulty, or ds connect failed */
372 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
373 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
374 return PNFS_NOT_ATTEMPTED;
375 }
376 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
377
378 /* No multipath support. Use first DS */
379 data->ds_clp = ds->ds_clp;
380 fh = nfs4_fl_select_ds_fh(lseg, j);
381 if (fh)
382 data->args.fh = fh;
383
384 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
385 data->mds_offset = offset;
386
387 /* Perform an asynchronous read to ds */
388 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
389 &filelayout_read_call_ops);
390 BUG_ON(status != 0);
391 return PNFS_ATTEMPTED;
392}
393
394/* Perform async writes. */
395static enum pnfs_try_status
396filelayout_write_pagelist(struct nfs_write_data *data, int sync)
397{
398 struct pnfs_layout_segment *lseg = data->lseg;
399 struct nfs4_pnfs_ds *ds;
400 loff_t offset = data->args.offset;
401 u32 j, idx;
402 struct nfs_fh *fh;
403 int status;
404
405 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
406 return PNFS_NOT_ATTEMPTED;
407
408 /* Retrieve the correct rpc_client for the byte range */
409 j = nfs4_fl_calc_j_index(lseg, offset);
410 idx = nfs4_fl_calc_ds_index(lseg, j);
411 ds = nfs4_fl_prepare_ds(lseg, idx);
412 if (!ds) {
413 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
414 __func__);
415 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
416 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
417 return PNFS_NOT_ATTEMPTED;
418 }
419 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
420 data->inode->i_ino, sync, (size_t) data->args.count, offset,
421 ds->ds_remotestr);
422
423 data->write_done_cb = filelayout_write_done_cb;
424 data->ds_clp = ds->ds_clp;
425 fh = nfs4_fl_select_ds_fh(lseg, j);
426 if (fh)
427 data->args.fh = fh;
428 /*
429 * Get the file offset on the dserver. Set the write offset to
430 * this offset and save the original offset.
431 */
432 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
433
434 /* Perform an asynchronous write */
435 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
436 &filelayout_write_call_ops, sync);
437 BUG_ON(status != 0);
438 return PNFS_ATTEMPTED;
439}
440
441/*
442 * filelayout_check_layout()
443 *
444 * Make sure layout segment parameters are sane WRT the device.
445 * At this point no generic layer initialization of the lseg has occurred,
446 * and nothing has been added to the layout_hdr cache.
447 *
448 */
449static int
450filelayout_check_layout(struct pnfs_layout_hdr *lo,
451 struct nfs4_filelayout_segment *fl,
452 struct nfs4_layoutget_res *lgr,
453 struct nfs4_deviceid *id,
454 gfp_t gfp_flags)
455{
456 struct nfs4_deviceid_node *d;
457 struct nfs4_file_layout_dsaddr *dsaddr;
458 int status = -EINVAL;
459 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
460
461 dprintk("--> %s\n", __func__);
462
463 /* FIXME: remove this check when layout segment support is added */
464 if (lgr->range.offset != 0 ||
465 lgr->range.length != NFS4_MAX_UINT64) {
466 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
467 __func__);
468 goto out;
469 }
470
471 if (fl->pattern_offset > lgr->range.offset) {
472 dprintk("%s pattern_offset %lld too large\n",
473 __func__, fl->pattern_offset);
474 goto out;
475 }
476
477 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
478 dprintk("%s Invalid stripe unit (%u)\n",
479 __func__, fl->stripe_unit);
480 goto out;
481 }
482
483 /* find and reference the deviceid */
484 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
485 NFS_SERVER(lo->plh_inode)->nfs_client, id);
486 if (d == NULL) {
487 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
488 if (dsaddr == NULL)
489 goto out;
490 } else
491 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
492 /* Found deviceid is being reaped */
493 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
494 goto out_put;
495
496 fl->dsaddr = dsaddr;
497
498 if (fl->first_stripe_index >= dsaddr->stripe_count) {
499 dprintk("%s Bad first_stripe_index %u\n",
500 __func__, fl->first_stripe_index);
501 goto out_put;
502 }
503
504 if ((fl->stripe_type == STRIPE_SPARSE &&
505 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
506 (fl->stripe_type == STRIPE_DENSE &&
507 fl->num_fh != dsaddr->stripe_count)) {
508 dprintk("%s num_fh %u not valid for given packing\n",
509 __func__, fl->num_fh);
510 goto out_put;
511 }
512
513 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
514 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
515 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
516 nfss->wsize);
517 }
518
519 status = 0;
520out:
521 dprintk("--> %s returns %d\n", __func__, status);
522 return status;
523out_put:
524 nfs4_fl_put_deviceid(dsaddr);
525 goto out;
526}
527
528static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
529{
530 int i;
531
532 for (i = 0; i < fl->num_fh; i++) {
533 if (!fl->fh_array[i])
534 break;
535 kfree(fl->fh_array[i]);
536 }
537 kfree(fl->fh_array);
538 fl->fh_array = NULL;
539}
540
541static void
542_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
543{
544 filelayout_free_fh_array(fl);
545 kfree(fl);
546}
547
548static int
549filelayout_decode_layout(struct pnfs_layout_hdr *flo,
550 struct nfs4_filelayout_segment *fl,
551 struct nfs4_layoutget_res *lgr,
552 struct nfs4_deviceid *id,
553 gfp_t gfp_flags)
554{
555 struct xdr_stream stream;
556 struct xdr_buf buf;
557 struct page *scratch;
558 __be32 *p;
559 uint32_t nfl_util;
560 int i;
561
562 dprintk("%s: set_layout_map Begin\n", __func__);
563
564 scratch = alloc_page(gfp_flags);
565 if (!scratch)
566 return -ENOMEM;
567
568 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
569 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
570
571 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
572 * num_fh (4) */
573 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
574 if (unlikely(!p))
575 goto out_err;
576
577 memcpy(id, p, sizeof(*id));
578 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
579 nfs4_print_deviceid(id);
580
581 nfl_util = be32_to_cpup(p++);
582 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
583 fl->commit_through_mds = 1;
584 if (nfl_util & NFL4_UFLG_DENSE)
585 fl->stripe_type = STRIPE_DENSE;
586 else
587 fl->stripe_type = STRIPE_SPARSE;
588 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
589
590 fl->first_stripe_index = be32_to_cpup(p++);
591 p = xdr_decode_hyper(p, &fl->pattern_offset);
592 fl->num_fh = be32_to_cpup(p++);
593
594 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
595 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
596 fl->pattern_offset);
597
598 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
599 * Futher checking is done in filelayout_check_layout */
600 if (fl->num_fh >
601 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
602 goto out_err;
603
604 if (fl->num_fh > 0) {
605 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
606 gfp_flags);
607 if (!fl->fh_array)
608 goto out_err;
609 }
610
611 for (i = 0; i < fl->num_fh; i++) {
612 /* Do we want to use a mempool here? */
613 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
614 if (!fl->fh_array[i])
615 goto out_err_free;
616
617 p = xdr_inline_decode(&stream, 4);
618 if (unlikely(!p))
619 goto out_err_free;
620 fl->fh_array[i]->size = be32_to_cpup(p++);
621 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
622 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
623 i, fl->fh_array[i]->size);
624 goto out_err_free;
625 }
626
627 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
628 if (unlikely(!p))
629 goto out_err_free;
630 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
631 dprintk("DEBUG: %s: fh len %d\n", __func__,
632 fl->fh_array[i]->size);
633 }
634
635 __free_page(scratch);
636 return 0;
637
638out_err_free:
639 filelayout_free_fh_array(fl);
640out_err:
641 __free_page(scratch);
642 return -EIO;
643}
644
645static void
646filelayout_free_lseg(struct pnfs_layout_segment *lseg)
647{
648 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
649
650 dprintk("--> %s\n", __func__);
651 nfs4_fl_put_deviceid(fl->dsaddr);
652 kfree(fl->commit_buckets);
653 _filelayout_free_lseg(fl);
654}
655
656static struct pnfs_layout_segment *
657filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
658 struct nfs4_layoutget_res *lgr,
659 gfp_t gfp_flags)
660{
661 struct nfs4_filelayout_segment *fl;
662 int rc;
663 struct nfs4_deviceid id;
664
665 dprintk("--> %s\n", __func__);
666 fl = kzalloc(sizeof(*fl), gfp_flags);
667 if (!fl)
668 return NULL;
669
670 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
671 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
672 _filelayout_free_lseg(fl);
673 return NULL;
674 }
675
676 /* This assumes there is only one IOMODE_RW lseg. What
677 * we really want to do is have a layout_hdr level
678 * dictionary of <multipath_list4, fh> keys, each
679 * associated with a struct list_head, populated by calls
680 * to filelayout_write_pagelist().
681 * */
682 if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
683 int i;
684 int size = (fl->stripe_type == STRIPE_SPARSE) ?
685 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
686
687 fl->commit_buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket), gfp_flags);
688 if (!fl->commit_buckets) {
689 filelayout_free_lseg(&fl->generic_hdr);
690 return NULL;
691 }
692 fl->number_of_buckets = size;
693 for (i = 0; i < size; i++) {
694 INIT_LIST_HEAD(&fl->commit_buckets[i].written);
695 INIT_LIST_HEAD(&fl->commit_buckets[i].committing);
696 }
697 }
698 return &fl->generic_hdr;
699}
700
701/*
702 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
703 *
704 * return true : coalesce page
705 * return false : don't coalesce page
706 */
707static bool
708filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
709 struct nfs_page *req)
710{
711 u64 p_stripe, r_stripe;
712 u32 stripe_unit;
713
714 if (!pnfs_generic_pg_test(pgio, prev, req) ||
715 !nfs_generic_pg_test(pgio, prev, req))
716 return false;
717
718 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
719 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
720 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
721
722 do_div(p_stripe, stripe_unit);
723 do_div(r_stripe, stripe_unit);
724
725 return (p_stripe == r_stripe);
726}
727
728static void
729filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
730 struct nfs_page *req)
731{
732 BUG_ON(pgio->pg_lseg != NULL);
733
734 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
735 req->wb_context,
736 0,
737 NFS4_MAX_UINT64,
738 IOMODE_READ,
739 GFP_KERNEL);
740 /* If no lseg, fall back to read through mds */
741 if (pgio->pg_lseg == NULL)
742 nfs_pageio_reset_read_mds(pgio);
743}
744
745static void
746filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
747 struct nfs_page *req)
748{
749 BUG_ON(pgio->pg_lseg != NULL);
750
751 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
752 req->wb_context,
753 0,
754 NFS4_MAX_UINT64,
755 IOMODE_RW,
756 GFP_NOFS);
757 /* If no lseg, fall back to write through mds */
758 if (pgio->pg_lseg == NULL)
759 nfs_pageio_reset_write_mds(pgio);
760}
761
762static const struct nfs_pageio_ops filelayout_pg_read_ops = {
763 .pg_init = filelayout_pg_init_read,
764 .pg_test = filelayout_pg_test,
765 .pg_doio = pnfs_generic_pg_readpages,
766};
767
768static const struct nfs_pageio_ops filelayout_pg_write_ops = {
769 .pg_init = filelayout_pg_init_write,
770 .pg_test = filelayout_pg_test,
771 .pg_doio = pnfs_generic_pg_writepages,
772};
773
774static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
775{
776 if (fl->stripe_type == STRIPE_SPARSE)
777 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
778 else
779 return j;
780}
781
782/* The generic layer is about to remove the req from the commit list.
783 * If this will make the bucket empty, it will need to put the lseg reference.
784 */
785static void
786filelayout_clear_request_commit(struct nfs_page *req)
787{
788 struct pnfs_layout_segment *freeme = NULL;
789 struct inode *inode = req->wb_context->dentry->d_inode;
790
791 spin_lock(&inode->i_lock);
792 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
793 goto out;
794 if (list_is_singular(&req->wb_list)) {
795 struct pnfs_layout_segment *lseg;
796
797 /* From here we can find the bucket, but for the moment,
798 * since there is only one relevant lseg...
799 */
800 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
801 if (lseg->pls_range.iomode == IOMODE_RW) {
802 freeme = lseg;
803 break;
804 }
805 }
806 }
807out:
808 nfs_request_remove_commit_list(req);
809 spin_unlock(&inode->i_lock);
810 put_lseg(freeme);
811}
812
813static struct list_head *
814filelayout_choose_commit_list(struct nfs_page *req,
815 struct pnfs_layout_segment *lseg)
816{
817 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
818 u32 i, j;
819 struct list_head *list;
820
821 if (fl->commit_through_mds)
822 return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
823
824 /* Note that we are calling nfs4_fl_calc_j_index on each page
825 * that ends up being committed to a data server. An attractive
826 * alternative is to add a field to nfs_write_data and nfs_page
827 * to store the value calculated in filelayout_write_pagelist
828 * and just use that here.
829 */
830 j = nfs4_fl_calc_j_index(lseg,
831 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
832 i = select_bucket_index(fl, j);
833 list = &fl->commit_buckets[i].written;
834 if (list_empty(list)) {
835 /* Non-empty buckets hold a reference on the lseg. That ref
836 * is normally transferred to the COMMIT call and released
837 * there. It could also be released if the last req is pulled
838 * off due to a rewrite, in which case it will be done in
839 * filelayout_remove_commit_req
840 */
841 get_lseg(lseg);
842 }
843 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
844 return list;
845}
846
847static void
848filelayout_mark_request_commit(struct nfs_page *req,
849 struct pnfs_layout_segment *lseg)
850{
851 struct list_head *list;
852
853 list = filelayout_choose_commit_list(req, lseg);
854 nfs_request_add_commit_list(req, list);
855}
856
857static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
858{
859 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
860
861 if (flseg->stripe_type == STRIPE_SPARSE)
862 return i;
863 else
864 return nfs4_fl_calc_ds_index(lseg, i);
865}
866
867static struct nfs_fh *
868select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
869{
870 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
871
872 if (flseg->stripe_type == STRIPE_SPARSE) {
873 if (flseg->num_fh == 1)
874 i = 0;
875 else if (flseg->num_fh == 0)
876 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
877 return NULL;
878 }
879 return flseg->fh_array[i];
880}
881
882static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
883{
884 struct pnfs_layout_segment *lseg = data->lseg;
885 struct nfs4_pnfs_ds *ds;
886 u32 idx;
887 struct nfs_fh *fh;
888
889 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
890 ds = nfs4_fl_prepare_ds(lseg, idx);
891 if (!ds) {
892 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
893 __func__);
894 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
895 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
896 prepare_to_resend_writes(data);
897 filelayout_commit_release(data);
898 return -EAGAIN;
899 }
900 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
901 data->write_done_cb = filelayout_commit_done_cb;
902 data->ds_clp = ds->ds_clp;
903 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
904 if (fh)
905 data->args.fh = fh;
906 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
907 &filelayout_commit_call_ops, how);
908}
909
910/*
911 * This is only useful while we are using whole file layouts.
912 */
913static struct pnfs_layout_segment *
914find_only_write_lseg_locked(struct inode *inode)
915{
916 struct pnfs_layout_segment *lseg;
917
918 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
919 if (lseg->pls_range.iomode == IOMODE_RW)
920 return lseg;
921 return NULL;
922}
923
924static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
925{
926 struct pnfs_layout_segment *rv;
927
928 spin_lock(&inode->i_lock);
929 rv = find_only_write_lseg_locked(inode);
930 if (rv)
931 get_lseg(rv);
932 spin_unlock(&inode->i_lock);
933 return rv;
934}
935
936static int
937filelayout_scan_ds_commit_list(struct nfs4_fl_commit_bucket *bucket, int max,
938 spinlock_t *lock)
939{
940 struct list_head *src = &bucket->written;
941 struct list_head *dst = &bucket->committing;
942 struct nfs_page *req, *tmp;
943 int ret = 0;
944
945 list_for_each_entry_safe(req, tmp, src, wb_list) {
946 if (!nfs_lock_request(req))
947 continue;
948 if (cond_resched_lock(lock))
949 list_safe_reset_next(req, tmp, wb_list);
950 nfs_request_remove_commit_list(req);
951 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
952 nfs_list_add_request(req, dst);
953 ret++;
954 if (ret == max)
955 break;
956 }
957 return ret;
958}
959
960/* Move reqs from written to committing lists, returning count of number moved.
961 * Note called with i_lock held.
962 */
963static int filelayout_scan_commit_lists(struct inode *inode, int max,
964 spinlock_t *lock)
965{
966 struct pnfs_layout_segment *lseg;
967 struct nfs4_filelayout_segment *fl;
968 int i, rv = 0, cnt;
969
970 lseg = find_only_write_lseg_locked(inode);
971 if (!lseg)
972 goto out_done;
973 fl = FILELAYOUT_LSEG(lseg);
974 if (fl->commit_through_mds)
975 goto out_done;
976 for (i = 0; i < fl->number_of_buckets && max != 0; i++) {
977 cnt = filelayout_scan_ds_commit_list(&fl->commit_buckets[i],
978 max, lock);
979 max -= cnt;
980 rv += cnt;
981 }
982out_done:
983 return rv;
984}
985
986static unsigned int
987alloc_ds_commits(struct inode *inode, struct list_head *list)
988{
989 struct pnfs_layout_segment *lseg;
990 struct nfs4_filelayout_segment *fl;
991 struct nfs_write_data *data;
992 int i, j;
993 unsigned int nreq = 0;
994
995 /* Won't need this when non-whole file layout segments are supported
996 * instead we will use a pnfs_layout_hdr structure */
997 lseg = find_only_write_lseg(inode);
998 if (!lseg)
999 return 0;
1000 fl = FILELAYOUT_LSEG(lseg);
1001 for (i = 0; i < fl->number_of_buckets; i++) {
1002 if (list_empty(&fl->commit_buckets[i].committing))
1003 continue;
1004 data = nfs_commitdata_alloc();
1005 if (!data)
1006 break;
1007 data->ds_commit_index = i;
1008 data->lseg = lseg;
1009 list_add(&data->pages, list);
1010 nreq++;
1011 }
1012
1013 /* Clean up on error */
1014 for (j = i; j < fl->number_of_buckets; j++) {
1015 if (list_empty(&fl->commit_buckets[i].committing))
1016 continue;
1017 nfs_retry_commit(&fl->commit_buckets[i].committing, lseg);
1018 put_lseg(lseg); /* associated with emptying bucket */
1019 }
1020 put_lseg(lseg);
1021 /* Caller will clean up entries put on list */
1022 return nreq;
1023}
1024
1025/* This follows nfs_commit_list pretty closely */
1026static int
1027filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1028 int how)
1029{
1030 struct nfs_write_data *data, *tmp;
1031 LIST_HEAD(list);
1032 unsigned int nreq = 0;
1033
1034 if (!list_empty(mds_pages)) {
1035 data = nfs_commitdata_alloc();
1036 if (data != NULL) {
1037 data->lseg = NULL;
1038 list_add(&data->pages, &list);
1039 nreq++;
1040 } else
1041 nfs_retry_commit(mds_pages, NULL);
1042 }
1043
1044 nreq += alloc_ds_commits(inode, &list);
1045
1046 if (nreq == 0) {
1047 nfs_commit_clear_lock(NFS_I(inode));
1048 goto out;
1049 }
1050
1051 atomic_add(nreq, &NFS_I(inode)->commits_outstanding);
1052
1053 list_for_each_entry_safe(data, tmp, &list, pages) {
1054 list_del_init(&data->pages);
1055 if (!data->lseg) {
1056 nfs_init_commit(data, mds_pages, NULL);
1057 nfs_initiate_commit(data, NFS_CLIENT(inode),
1058 data->mds_ops, how);
1059 } else {
1060 nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index].committing, data->lseg);
1061 filelayout_initiate_commit(data, how);
1062 }
1063 }
1064out:
1065 return PNFS_ATTEMPTED;
1066}
1067
1068static void
1069filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1070{
1071 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1072}
1073
1074static struct pnfs_layoutdriver_type filelayout_type = {
1075 .id = LAYOUT_NFSV4_1_FILES,
1076 .name = "LAYOUT_NFSV4_1_FILES",
1077 .owner = THIS_MODULE,
1078 .alloc_lseg = filelayout_alloc_lseg,
1079 .free_lseg = filelayout_free_lseg,
1080 .pg_read_ops = &filelayout_pg_read_ops,
1081 .pg_write_ops = &filelayout_pg_write_ops,
1082 .mark_request_commit = filelayout_mark_request_commit,
1083 .clear_request_commit = filelayout_clear_request_commit,
1084 .scan_commit_lists = filelayout_scan_commit_lists,
1085 .commit_pagelist = filelayout_commit_pagelist,
1086 .read_pagelist = filelayout_read_pagelist,
1087 .write_pagelist = filelayout_write_pagelist,
1088 .free_deviceid_node = filelayout_free_deveiceid_node,
1089};
1090
1091static int __init nfs4filelayout_init(void)
1092{
1093 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1094 __func__);
1095 return pnfs_register_layoutdriver(&filelayout_type);
1096}
1097
1098static void __exit nfs4filelayout_exit(void)
1099{
1100 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1101 __func__);
1102 pnfs_unregister_layoutdriver(&filelayout_type);
1103}
1104
1105MODULE_ALIAS("nfs-layouttype4-1");
1106
1107module_init(nfs4filelayout_init);
1108module_exit(nfs4filelayout_exit);