blob: 6f8192f4cfc759b653c186c5122565bacdd96631 [file] [log] [blame]
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001/*
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>
Andy Adamson16b374c2010-10-20 00:18:04 -040033
34#include "internal.h"
35#include "nfs4filelayout.h"
Dean Hildebrand7ab672c2010-10-20 00:18:00 -040036
37#define NFSDBG_FACILITY NFSDBG_PNFS_LD
38
39MODULE_LICENSE("GPL");
40MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
41MODULE_DESCRIPTION("The NFSv4 file layout driver");
42
Andy Adamsoncbdabc72011-03-01 01:34:20 +000043#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
44
Fred Isamancfe7f412011-03-01 01:34:18 +000045static loff_t
46filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
47 loff_t offset)
48{
49 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
50 u64 tmp;
51
52 offset -= flseg->pattern_offset;
53 tmp = offset;
54 do_div(tmp, stripe_width);
55
56 return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit);
57}
58
59/* This function is used by the layout driver to calculate the
60 * offset of the file on the dserver based on whether the
61 * layout type is STRIPE_DENSE or STRIPE_SPARSE
62 */
63static loff_t
64filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
65{
66 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
67
68 switch (flseg->stripe_type) {
69 case STRIPE_SPARSE:
70 return offset;
71
72 case STRIPE_DENSE:
73 return filelayout_get_dense_offset(flseg, offset);
74 }
75
76 BUG();
77}
78
Andy Adamsoncbdabc72011-03-01 01:34:20 +000079/* For data server errors we don't recover from */
80static void
81filelayout_set_lo_fail(struct pnfs_layout_segment *lseg)
82{
83 if (lseg->pls_range.iomode == IOMODE_RW) {
84 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
85 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
86 } else {
87 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
88 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
89 }
90}
91
92static int filelayout_async_handle_error(struct rpc_task *task,
93 struct nfs4_state *state,
94 struct nfs_client *clp,
95 int *reset)
96{
97 if (task->tk_status >= 0)
98 return 0;
99
100 *reset = 0;
101
102 switch (task->tk_status) {
103 case -NFS4ERR_BADSESSION:
104 case -NFS4ERR_BADSLOT:
105 case -NFS4ERR_BAD_HIGH_SLOT:
106 case -NFS4ERR_DEADSESSION:
107 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
108 case -NFS4ERR_SEQ_FALSE_RETRY:
109 case -NFS4ERR_SEQ_MISORDERED:
110 dprintk("%s ERROR %d, Reset session. Exchangeid "
111 "flags 0x%x\n", __func__, task->tk_status,
112 clp->cl_exchange_flags);
113 nfs4_schedule_session_recovery(clp->cl_session);
114 break;
115 case -NFS4ERR_DELAY:
116 case -NFS4ERR_GRACE:
117 case -EKEYEXPIRED:
118 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
119 break;
120 default:
121 dprintk("%s DS error. Retry through MDS %d\n", __func__,
122 task->tk_status);
123 *reset = 1;
124 break;
125 }
126 task->tk_status = 0;
127 return -EAGAIN;
128}
129
130/* NFS_PROTO call done callback routines */
131
132static int filelayout_read_done_cb(struct rpc_task *task,
133 struct nfs_read_data *data)
134{
135 struct nfs_client *clp = data->ds_clp;
136 int reset = 0;
137
138 dprintk("%s DS read\n", __func__);
139
140 if (filelayout_async_handle_error(task, data->args.context->state,
141 data->ds_clp, &reset) == -EAGAIN) {
142 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
143 __func__, data->ds_clp, data->ds_clp->cl_session);
144 if (reset) {
145 filelayout_set_lo_fail(data->lseg);
146 nfs4_reset_read(task, data);
147 clp = NFS_SERVER(data->inode)->nfs_client;
148 }
149 nfs_restart_rpc(task, clp);
150 return -EAGAIN;
151 }
152
153 return 0;
154}
155
Andy Adamson16b374c2010-10-20 00:18:04 -0400156/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000157 * We reference the rpc_cred of the first WRITE that triggers the need for
158 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
159 * rfc5661 is not clear about which credential should be used.
160 */
161static void
162filelayout_set_layoutcommit(struct nfs_write_data *wdata)
163{
164 if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
165 wdata->res.verf->committed == NFS_FILE_SYNC)
166 return;
167
168 pnfs_set_layoutcommit(wdata);
169 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
170 (unsigned long) wdata->lseg->pls_end_pos);
171}
172
173/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000174 * Call ops for the async read/write cases
175 * In the case of dense layouts, the offset needs to be reset to its
176 * original value.
177 */
178static void filelayout_read_prepare(struct rpc_task *task, void *data)
179{
180 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
181
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000182 rdata->read_done_cb = filelayout_read_done_cb;
183
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000184 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
185 &rdata->args.seq_args, &rdata->res.seq_res,
186 0, task))
187 return;
188
189 rpc_call_start(task);
190}
191
192static void filelayout_read_call_done(struct rpc_task *task, void *data)
193{
194 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
195
196 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
197
198 /* Note this may cause RPC to be resent */
199 rdata->mds_ops->rpc_call_done(task, data);
200}
201
202static void filelayout_read_release(void *data)
203{
204 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
205
206 rdata->mds_ops->rpc_release(data);
207}
208
Fred Isamana69aef12011-03-03 15:13:47 +0000209static int filelayout_write_done_cb(struct rpc_task *task,
210 struct nfs_write_data *data)
211{
212 int reset = 0;
213
214 if (filelayout_async_handle_error(task, data->args.context->state,
215 data->ds_clp, &reset) == -EAGAIN) {
216 struct nfs_client *clp;
217
218 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
219 __func__, data->ds_clp, data->ds_clp->cl_session);
220 if (reset) {
221 filelayout_set_lo_fail(data->lseg);
222 nfs4_reset_write(task, data);
223 clp = NFS_SERVER(data->inode)->nfs_client;
224 } else
225 clp = data->ds_clp;
226 nfs_restart_rpc(task, clp);
227 return -EAGAIN;
228 }
229
Andy Adamson863a3c62011-03-23 13:27:54 +0000230 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000231 return 0;
232}
233
Fred Isamane0c2b382011-03-23 13:27:53 +0000234/* Fake up some data that will cause nfs_commit_release to retry the writes. */
235static void prepare_to_resend_writes(struct nfs_write_data *data)
236{
237 struct nfs_page *first = nfs_list_entry(data->pages.next);
238
239 data->task.tk_status = 0;
240 memcpy(data->verf.verifier, first->wb_verf.verifier,
241 sizeof(first->wb_verf.verifier));
242 data->verf.verifier[0]++; /* ensure verifier mismatch */
243}
244
245static int filelayout_commit_done_cb(struct rpc_task *task,
246 struct nfs_write_data *data)
247{
248 int reset = 0;
249
250 if (filelayout_async_handle_error(task, data->args.context->state,
251 data->ds_clp, &reset) == -EAGAIN) {
252 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
253 __func__, data->ds_clp, data->ds_clp->cl_session);
254 if (reset) {
255 prepare_to_resend_writes(data);
256 filelayout_set_lo_fail(data->lseg);
257 } else
258 nfs_restart_rpc(task, data->ds_clp);
259 return -EAGAIN;
260 }
261
262 return 0;
263}
264
Fred Isamana69aef12011-03-03 15:13:47 +0000265static void filelayout_write_prepare(struct rpc_task *task, void *data)
266{
267 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
268
269 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
270 &wdata->args.seq_args, &wdata->res.seq_res,
271 0, task))
272 return;
273
274 rpc_call_start(task);
275}
276
277static void filelayout_write_call_done(struct rpc_task *task, void *data)
278{
279 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
280
281 /* Note this may cause RPC to be resent */
282 wdata->mds_ops->rpc_call_done(task, data);
283}
284
285static void filelayout_write_release(void *data)
286{
287 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
288
289 wdata->mds_ops->rpc_release(data);
290}
291
Fred Isamane0c2b382011-03-23 13:27:53 +0000292static void filelayout_commit_release(void *data)
293{
294 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
295
296 nfs_commit_release_pages(wdata);
297 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
298 nfs_commit_clear_lock(NFS_I(wdata->inode));
299 nfs_commitdata_release(wdata);
300}
301
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000302struct rpc_call_ops filelayout_read_call_ops = {
303 .rpc_call_prepare = filelayout_read_prepare,
304 .rpc_call_done = filelayout_read_call_done,
305 .rpc_release = filelayout_read_release,
306};
307
Fred Isamana69aef12011-03-03 15:13:47 +0000308struct rpc_call_ops filelayout_write_call_ops = {
309 .rpc_call_prepare = filelayout_write_prepare,
310 .rpc_call_done = filelayout_write_call_done,
311 .rpc_release = filelayout_write_release,
312};
313
Fred Isamane0c2b382011-03-23 13:27:53 +0000314struct rpc_call_ops filelayout_commit_call_ops = {
315 .rpc_call_prepare = filelayout_write_prepare,
316 .rpc_call_done = filelayout_write_call_done,
317 .rpc_release = filelayout_commit_release,
318};
319
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000320static enum pnfs_try_status
321filelayout_read_pagelist(struct nfs_read_data *data)
322{
323 struct pnfs_layout_segment *lseg = data->lseg;
324 struct nfs4_pnfs_ds *ds;
325 loff_t offset = data->args.offset;
326 u32 j, idx;
327 struct nfs_fh *fh;
328 int status;
329
330 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
331 __func__, data->inode->i_ino,
332 data->args.pgbase, (size_t)data->args.count, offset);
333
334 /* Retrieve the correct rpc_client for the byte range */
335 j = nfs4_fl_calc_j_index(lseg, offset);
336 idx = nfs4_fl_calc_ds_index(lseg, j);
337 ds = nfs4_fl_prepare_ds(lseg, idx);
338 if (!ds) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000339 /* Either layout fh index faulty, or ds connect failed */
340 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
341 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000342 return PNFS_NOT_ATTEMPTED;
343 }
344 dprintk("%s USE DS:ip %x %hu\n", __func__,
345 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
346
347 /* No multipath support. Use first DS */
348 data->ds_clp = ds->ds_clp;
349 fh = nfs4_fl_select_ds_fh(lseg, j);
350 if (fh)
351 data->args.fh = fh;
352
353 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
354 data->mds_offset = offset;
355
356 /* Perform an asynchronous read to ds */
357 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
358 &filelayout_read_call_ops);
359 BUG_ON(status != 0);
360 return PNFS_ATTEMPTED;
361}
362
Fred Isamana69aef12011-03-03 15:13:47 +0000363/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000364static enum pnfs_try_status
365filelayout_write_pagelist(struct nfs_write_data *data, int sync)
366{
Fred Isamana69aef12011-03-03 15:13:47 +0000367 struct pnfs_layout_segment *lseg = data->lseg;
368 struct nfs4_pnfs_ds *ds;
369 loff_t offset = data->args.offset;
370 u32 j, idx;
371 struct nfs_fh *fh;
372 int status;
373
374 /* Retrieve the correct rpc_client for the byte range */
375 j = nfs4_fl_calc_j_index(lseg, offset);
376 idx = nfs4_fl_calc_ds_index(lseg, j);
377 ds = nfs4_fl_prepare_ds(lseg, idx);
378 if (!ds) {
379 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
380 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
381 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
382 return PNFS_NOT_ATTEMPTED;
383 }
384 dprintk("%s ino %lu sync %d req %Zu@%llu DS:%x:%hu\n", __func__,
385 data->inode->i_ino, sync, (size_t) data->args.count, offset,
386 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
387
Fred Isamana69aef12011-03-03 15:13:47 +0000388 data->write_done_cb = filelayout_write_done_cb;
389 data->ds_clp = ds->ds_clp;
390 fh = nfs4_fl_select_ds_fh(lseg, j);
391 if (fh)
392 data->args.fh = fh;
393 /*
394 * Get the file offset on the dserver. Set the write offset to
395 * this offset and save the original offset.
396 */
397 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
398 data->mds_offset = offset;
399
400 /* Perform an asynchronous write */
401 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
402 &filelayout_write_call_ops, sync);
403 BUG_ON(status != 0);
404 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000405}
406
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000407/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400408 * filelayout_check_layout()
409 *
410 * Make sure layout segment parameters are sane WRT the device.
411 * At this point no generic layer initialization of the lseg has occurred,
412 * and nothing has been added to the layout_hdr cache.
413 *
414 */
415static int
416filelayout_check_layout(struct pnfs_layout_hdr *lo,
417 struct nfs4_filelayout_segment *fl,
418 struct nfs4_layoutget_res *lgr,
419 struct nfs4_deviceid *id)
420{
421 struct nfs4_file_layout_dsaddr *dsaddr;
422 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000423 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400424
425 dprintk("--> %s\n", __func__);
426
427 if (fl->pattern_offset > lgr->range.offset) {
428 dprintk("%s pattern_offset %lld to large\n",
429 __func__, fl->pattern_offset);
430 goto out;
431 }
432
Benny Halevy75247af2011-02-22 15:56:01 -0800433 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
434 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400435 __func__, fl->stripe_unit);
436 goto out;
437 }
438
439 /* find and reference the deviceid */
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000440 dsaddr = nfs4_fl_find_get_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400441 if (dsaddr == NULL) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000442 dsaddr = get_device_info(lo->plh_inode, id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400443 if (dsaddr == NULL)
444 goto out;
445 }
446 fl->dsaddr = dsaddr;
447
448 if (fl->first_stripe_index < 0 ||
449 fl->first_stripe_index >= dsaddr->stripe_count) {
450 dprintk("%s Bad first_stripe_index %d\n",
451 __func__, fl->first_stripe_index);
452 goto out_put;
453 }
454
455 if ((fl->stripe_type == STRIPE_SPARSE &&
456 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
457 (fl->stripe_type == STRIPE_DENSE &&
458 fl->num_fh != dsaddr->stripe_count)) {
459 dprintk("%s num_fh %u not valid for given packing\n",
460 __func__, fl->num_fh);
461 goto out_put;
462 }
463
464 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
465 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
466 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
467 nfss->wsize);
468 }
469
470 status = 0;
471out:
472 dprintk("--> %s returns %d\n", __func__, status);
473 return status;
474out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000475 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400476 goto out;
477}
478
479static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
480{
481 int i;
482
483 for (i = 0; i < fl->num_fh; i++) {
484 if (!fl->fh_array[i])
485 break;
486 kfree(fl->fh_array[i]);
487 }
488 kfree(fl->fh_array);
489 fl->fh_array = NULL;
490}
491
492static void
493_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
494{
495 filelayout_free_fh_array(fl);
496 kfree(fl);
497}
498
499static int
500filelayout_decode_layout(struct pnfs_layout_hdr *flo,
501 struct nfs4_filelayout_segment *fl,
502 struct nfs4_layoutget_res *lgr,
503 struct nfs4_deviceid *id)
504{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400505 struct xdr_stream stream;
506 struct xdr_buf buf = {
507 .pages = lgr->layoutp->pages,
508 .page_len = lgr->layoutp->len,
509 .buflen = lgr->layoutp->len,
510 .len = lgr->layoutp->len,
511 };
512 struct page *scratch;
513 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400514 uint32_t nfl_util;
515 int i;
516
517 dprintk("%s: set_layout_map Begin\n", __func__);
518
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400519 scratch = alloc_page(GFP_KERNEL);
520 if (!scratch)
521 return -ENOMEM;
522
523 xdr_init_decode(&stream, &buf, NULL);
524 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
525
526 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
527 * num_fh (4) */
528 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
529 if (unlikely(!p))
530 goto out_err;
531
Andy Adamson16b374c2010-10-20 00:18:04 -0400532 memcpy(id, p, sizeof(*id));
533 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
534 print_deviceid(id);
535
536 nfl_util = be32_to_cpup(p++);
537 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
538 fl->commit_through_mds = 1;
539 if (nfl_util & NFL4_UFLG_DENSE)
540 fl->stripe_type = STRIPE_DENSE;
541 else
542 fl->stripe_type = STRIPE_SPARSE;
543 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
544
545 fl->first_stripe_index = be32_to_cpup(p++);
546 p = xdr_decode_hyper(p, &fl->pattern_offset);
547 fl->num_fh = be32_to_cpup(p++);
548
549 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
550 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
551 fl->pattern_offset);
552
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400553 if (!fl->num_fh)
554 goto out_err;
555
Andy Adamson16b374c2010-10-20 00:18:04 -0400556 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
557 GFP_KERNEL);
558 if (!fl->fh_array)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400559 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400560
561 for (i = 0; i < fl->num_fh; i++) {
562 /* Do we want to use a mempool here? */
563 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400564 if (!fl->fh_array[i])
565 goto out_err_free;
566
567 p = xdr_inline_decode(&stream, 4);
568 if (unlikely(!p))
569 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400570 fl->fh_array[i]->size = be32_to_cpup(p++);
571 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
572 printk(KERN_ERR "Too big fh %d received %d\n",
573 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400574 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400575 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400576
577 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
578 if (unlikely(!p))
579 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400580 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400581 dprintk("DEBUG: %s: fh len %d\n", __func__,
582 fl->fh_array[i]->size);
583 }
584
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400585 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400586 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400587
588out_err_free:
589 filelayout_free_fh_array(fl);
590out_err:
591 __free_page(scratch);
592 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400593}
594
Fred Isamanc8795132011-03-23 13:27:49 +0000595static void
596filelayout_free_lseg(struct pnfs_layout_segment *lseg)
597{
598 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
599
600 dprintk("--> %s\n", __func__);
601 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman425eb732011-03-23 13:27:50 +0000602 kfree(fl->commit_buckets);
Fred Isamanc8795132011-03-23 13:27:49 +0000603 _filelayout_free_lseg(fl);
604}
605
Andy Adamson16b374c2010-10-20 00:18:04 -0400606static struct pnfs_layout_segment *
607filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
608 struct nfs4_layoutget_res *lgr)
609{
610 struct nfs4_filelayout_segment *fl;
611 int rc;
612 struct nfs4_deviceid id;
613
614 dprintk("--> %s\n", __func__);
615 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
616 if (!fl)
617 return NULL;
618
619 rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
620 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
621 _filelayout_free_lseg(fl);
622 return NULL;
623 }
Fred Isaman425eb732011-03-23 13:27:50 +0000624
625 /* This assumes there is only one IOMODE_RW lseg. What
626 * we really want to do is have a layout_hdr level
627 * dictionary of <multipath_list4, fh> keys, each
628 * associated with a struct list_head, populated by calls
629 * to filelayout_write_pagelist().
630 * */
631 if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
632 int i;
633 int size = (fl->stripe_type == STRIPE_SPARSE) ?
634 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
635
636 fl->commit_buckets = kcalloc(size, sizeof(struct list_head), GFP_KERNEL);
637 if (!fl->commit_buckets) {
638 filelayout_free_lseg(&fl->generic_hdr);
639 return NULL;
640 }
641 fl->number_of_buckets = size;
642 for (i = 0; i < size; i++)
643 INIT_LIST_HEAD(&fl->commit_buckets[i]);
644 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400645 return &fl->generic_hdr;
646}
647
Fred Isaman94ad1c82011-03-01 01:34:14 +0000648/*
649 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
650 *
651 * return 1 : coalesce page
652 * return 0 : don't coalesce page
653 */
654int
655filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
656 struct nfs_page *req)
657{
658 u64 p_stripe, r_stripe;
659 u32 stripe_unit;
660
661 if (!pgio->pg_lseg)
662 return 1;
663 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
664 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
665 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
666
667 do_div(p_stripe, stripe_unit);
668 do_div(r_stripe, stripe_unit);
669
670 return (p_stripe == r_stripe);
671}
672
Fred Isamane0c2b382011-03-23 13:27:53 +0000673static bool filelayout_mark_pnfs_commit(struct pnfs_layout_segment *lseg)
674{
675 return !FILELAYOUT_LSEG(lseg)->commit_through_mds;
676}
677
678static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
679{
680 if (fl->stripe_type == STRIPE_SPARSE)
681 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
682 else
683 return j;
684}
685
686struct list_head *filelayout_choose_commit_list(struct nfs_page *req)
687{
688 struct pnfs_layout_segment *lseg = req->wb_commit_lseg;
689 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
690 u32 i, j;
691 struct list_head *list;
692
693 /* Note that we are calling nfs4_fl_calc_j_index on each page
694 * that ends up being committed to a data server. An attractive
695 * alternative is to add a field to nfs_write_data and nfs_page
696 * to store the value calculated in filelayout_write_pagelist
697 * and just use that here.
698 */
699 j = nfs4_fl_calc_j_index(lseg,
700 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
701 i = select_bucket_index(fl, j);
702 list = &fl->commit_buckets[i];
703 if (list_empty(list)) {
704 /* Non-empty buckets hold a reference on the lseg */
705 get_lseg(lseg);
706 }
707 return list;
708}
709
710static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
711{
712 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
713
714 if (flseg->stripe_type == STRIPE_SPARSE)
715 return i;
716 else
717 return nfs4_fl_calc_ds_index(lseg, i);
718}
719
720static struct nfs_fh *
721select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
722{
723 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
724
725 if (flseg->stripe_type == STRIPE_SPARSE) {
726 if (flseg->num_fh == 1)
727 i = 0;
728 else if (flseg->num_fh == 0)
729 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
730 return NULL;
731 }
732 return flseg->fh_array[i];
733}
734
735static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
736{
737 struct pnfs_layout_segment *lseg = data->lseg;
738 struct nfs4_pnfs_ds *ds;
739 u32 idx;
740 struct nfs_fh *fh;
741
742 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
743 ds = nfs4_fl_prepare_ds(lseg, idx);
744 if (!ds) {
745 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
746 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
747 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
748 prepare_to_resend_writes(data);
749 data->mds_ops->rpc_release(data);
750 return -EAGAIN;
751 }
752 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
753 data->write_done_cb = filelayout_commit_done_cb;
754 data->ds_clp = ds->ds_clp;
755 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
756 if (fh)
757 data->args.fh = fh;
758 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
759 &filelayout_commit_call_ops, how);
760}
761
762/*
763 * This is only useful while we are using whole file layouts.
764 */
765static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
766{
767 struct pnfs_layout_segment *lseg, *rv = NULL;
768
769 spin_lock(&inode->i_lock);
770 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
771 if (lseg->pls_range.iomode == IOMODE_RW)
772 rv = get_lseg(lseg);
773 spin_unlock(&inode->i_lock);
774 return rv;
775}
776
777static int alloc_ds_commits(struct inode *inode, struct list_head *list)
778{
779 struct pnfs_layout_segment *lseg;
780 struct nfs4_filelayout_segment *fl;
781 struct nfs_write_data *data;
782 int i, j;
783
784 /* Won't need this when non-whole file layout segments are supported
785 * instead we will use a pnfs_layout_hdr structure */
786 lseg = find_only_write_lseg(inode);
787 if (!lseg)
788 return 0;
789 fl = FILELAYOUT_LSEG(lseg);
790 for (i = 0; i < fl->number_of_buckets; i++) {
791 if (list_empty(&fl->commit_buckets[i]))
792 continue;
793 data = nfs_commitdata_alloc();
794 if (!data)
795 goto out_bad;
796 data->ds_commit_index = i;
797 data->lseg = lseg;
798 list_add(&data->pages, list);
799 }
800 put_lseg(lseg);
801 return 0;
802
803out_bad:
804 for (j = i; j < fl->number_of_buckets; j++) {
805 if (list_empty(&fl->commit_buckets[i]))
806 continue;
807 nfs_retry_commit(&fl->commit_buckets[i], lseg);
808 put_lseg(lseg); /* associated with emptying bucket */
809 }
810 put_lseg(lseg);
811 /* Caller will clean up entries put on list */
812 return -ENOMEM;
813}
814
815/* This follows nfs_commit_list pretty closely */
816static int
817filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
818 int how)
819{
820 struct nfs_write_data *data, *tmp;
821 LIST_HEAD(list);
822
823 if (!list_empty(mds_pages)) {
824 data = nfs_commitdata_alloc();
825 if (!data)
826 goto out_bad;
827 data->lseg = NULL;
828 list_add(&data->pages, &list);
829 }
830
831 if (alloc_ds_commits(inode, &list))
832 goto out_bad;
833
834 list_for_each_entry_safe(data, tmp, &list, pages) {
835 list_del_init(&data->pages);
836 atomic_inc(&NFS_I(inode)->commits_outstanding);
837 if (!data->lseg) {
838 nfs_init_commit(data, mds_pages, NULL);
839 nfs_initiate_commit(data, NFS_CLIENT(inode),
840 data->mds_ops, how);
841 } else {
842 nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index], data->lseg);
843 filelayout_initiate_commit(data, how);
844 }
845 }
846 return 0;
847 out_bad:
848 list_for_each_entry_safe(data, tmp, &list, pages) {
849 nfs_retry_commit(&data->pages, data->lseg);
850 list_del_init(&data->pages);
851 nfs_commit_free(data);
852 }
853 nfs_retry_commit(mds_pages, NULL);
854 nfs_commit_clear_lock(NFS_I(inode));
855 return -ENOMEM;
856}
857
Dean Hildebrand7ab672c2010-10-20 00:18:00 -0400858static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000859 .id = LAYOUT_NFSV4_1_FILES,
860 .name = "LAYOUT_NFSV4_1_FILES",
861 .owner = THIS_MODULE,
862 .alloc_lseg = filelayout_alloc_lseg,
863 .free_lseg = filelayout_free_lseg,
Fred Isaman94ad1c82011-03-01 01:34:14 +0000864 .pg_test = filelayout_pg_test,
Fred Isamane0c2b382011-03-23 13:27:53 +0000865 .mark_pnfs_commit = filelayout_mark_pnfs_commit,
866 .choose_commit_list = filelayout_choose_commit_list,
867 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000868 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +0000869 .write_pagelist = filelayout_write_pagelist,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -0400870};
871
872static int __init nfs4filelayout_init(void)
873{
874 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
875 __func__);
876 return pnfs_register_layoutdriver(&filelayout_type);
877}
878
879static void __exit nfs4filelayout_exit(void)
880{
881 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
882 __func__);
883 pnfs_unregister_layoutdriver(&filelayout_type);
884}
885
886module_init(nfs4filelayout_init);
887module_exit(nfs4filelayout_exit);