]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/nfs4filelayout.c
NFS: remove NFS_PAGE_TAG_LOCKED
[mirror_ubuntu-artful-kernel.git] / fs / nfs / nfs4filelayout.c
CommitLineData
7ab672ce
DH
1/*
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>
19345cb2 33#include <linux/nfs_page.h>
143cb494 34#include <linux/module.h>
16b374ca 35
0a702195
WAA
36#include <linux/sunrpc/metrics.h>
37
16b374ca 38#include "internal.h"
9cb81968 39#include "delegation.h"
16b374ca 40#include "nfs4filelayout.h"
7ab672ce
DH
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
cbdabc7f
AA
48#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
49
cfe7f412
FI
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;
3476f114
CM
55 u64 stripe_no;
56 u32 rem;
cfe7f412
FI
57
58 offset -= flseg->pattern_offset;
3476f114
CM
59 stripe_no = div_u64(offset, stripe_width);
60 div_u64_rem(offset, flseg->stripe_unit, &rem);
cfe7f412 61
3476f114 62 return stripe_no * flseg->stripe_unit + rem;
cfe7f412
FI
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
cbdabc7f
AA
85static int filelayout_async_handle_error(struct rpc_task *task,
86 struct nfs4_state *state,
87 struct nfs_client *clp,
88 int *reset)
89{
9cb81968
AA
90 struct nfs_server *mds_server = NFS_SERVER(state->inode);
91 struct nfs_client *mds_client = mds_server->nfs_client;
92
cbdabc7f
AA
93 if (task->tk_status >= 0)
94 return 0;
cbdabc7f
AA
95 *reset = 0;
96
97 switch (task->tk_status) {
9cb81968
AA
98 /* MDS state errors */
99 case -NFS4ERR_DELEG_REVOKED:
100 case -NFS4ERR_ADMIN_REVOKED:
101 case -NFS4ERR_BAD_STATEID:
2dc31756 102 nfs_remove_bad_delegation(state->inode);
9cb81968 103 case -NFS4ERR_OPENMODE:
9cb81968
AA
104 nfs4_schedule_stateid_recovery(mds_server, state);
105 goto wait_on_recovery;
106 case -NFS4ERR_EXPIRED:
2dc31756 107 nfs4_schedule_stateid_recovery(mds_server, state);
9cb81968
AA
108 nfs4_schedule_lease_recovery(mds_client);
109 goto wait_on_recovery;
110 /* DS session errors */
cbdabc7f
AA
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 case -EKEYEXPIRED:
126 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
127 break;
a8a4ae3a
AA
128 case -NFS4ERR_RETRY_UNCACHED_REP:
129 break;
cbdabc7f
AA
130 default:
131 dprintk("%s DS error. Retry through MDS %d\n", __func__,
132 task->tk_status);
133 *reset = 1;
134 break;
135 }
9cb81968 136out:
cbdabc7f
AA
137 task->tk_status = 0;
138 return -EAGAIN;
9cb81968
AA
139wait_on_recovery:
140 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
141 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
142 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
143 goto out;
cbdabc7f
AA
144}
145
146/* NFS_PROTO call done callback routines */
147
148static int filelayout_read_done_cb(struct rpc_task *task,
149 struct nfs_read_data *data)
150{
cbdabc7f
AA
151 int reset = 0;
152
153 dprintk("%s DS read\n", __func__);
154
155 if (filelayout_async_handle_error(task, data->args.context->state,
156 data->ds_clp, &reset) == -EAGAIN) {
157 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
158 __func__, data->ds_clp, data->ds_clp->cl_session);
159 if (reset) {
1b0ae068 160 pnfs_set_lo_fail(data->lseg);
cbdabc7f 161 nfs4_reset_read(task, data);
cbdabc7f 162 }
d00c5d43 163 rpc_restart_call_prepare(task);
cbdabc7f
AA
164 return -EAGAIN;
165 }
166
167 return 0;
168}
169
863a3c6c
AA
170/*
171 * We reference the rpc_cred of the first WRITE that triggers the need for
172 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
173 * rfc5661 is not clear about which credential should be used.
174 */
175static void
176filelayout_set_layoutcommit(struct nfs_write_data *wdata)
177{
178 if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
179 wdata->res.verf->committed == NFS_FILE_SYNC)
180 return;
181
182 pnfs_set_layoutcommit(wdata);
183 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
acff5880 184 (unsigned long) NFS_I(wdata->inode)->layout->plh_lwb);
863a3c6c
AA
185}
186
dc70d7b3
AA
187/*
188 * Call ops for the async read/write cases
189 * In the case of dense layouts, the offset needs to be reset to its
190 * original value.
191 */
192static void filelayout_read_prepare(struct rpc_task *task, void *data)
193{
194 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
195
cbdabc7f
AA
196 rdata->read_done_cb = filelayout_read_done_cb;
197
dc70d7b3
AA
198 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
199 &rdata->args.seq_args, &rdata->res.seq_res,
9d12b216 200 task))
dc70d7b3
AA
201 return;
202
203 rpc_call_start(task);
204}
205
206static void filelayout_read_call_done(struct rpc_task *task, void *data)
207{
208 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
209
210 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
211
212 /* Note this may cause RPC to be resent */
213 rdata->mds_ops->rpc_call_done(task, data);
214}
215
0a702195
WAA
216static void filelayout_read_count_stats(struct rpc_task *task, void *data)
217{
218 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
219
220 rpc_count_iostats(task, NFS_SERVER(rdata->inode)->client->cl_metrics);
221}
222
dc70d7b3
AA
223static void filelayout_read_release(void *data)
224{
225 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
226
227 rdata->mds_ops->rpc_release(data);
228}
229
a69aef14
FI
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) {
a69aef14
FI
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) {
1b0ae068 240 pnfs_set_lo_fail(data->lseg);
a69aef14 241 nfs4_reset_write(task, data);
d00c5d43
TM
242 }
243 rpc_restart_call_prepare(task);
a69aef14
FI
244 return -EAGAIN;
245 }
246
863a3c6c 247 filelayout_set_layoutcommit(data);
a69aef14
FI
248 return 0;
249}
250
e0c2b380
FI
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);
1b0ae068 273 pnfs_set_lo_fail(data->lseg);
e0c2b380 274 } else
d00c5d43 275 rpc_restart_call_prepare(task);
e0c2b380
FI
276 return -EAGAIN;
277 }
278
279 return 0;
280}
281
a69aef14
FI
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,
9d12b216 288 task))
a69aef14
FI
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
0a702195
WAA
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
a69aef14
FI
309static void filelayout_write_release(void *data)
310{
311 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
312
313 wdata->mds_ops->rpc_release(data);
314}
315
e0c2b380
FI
316static void filelayout_commit_release(void *data)
317{
318 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
319
320 nfs_commit_release_pages(wdata);
321 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
322 nfs_commit_clear_lock(NFS_I(wdata->inode));
323 nfs_commitdata_release(wdata);
324}
325
dc70d7b3
AA
326struct rpc_call_ops filelayout_read_call_ops = {
327 .rpc_call_prepare = filelayout_read_prepare,
328 .rpc_call_done = filelayout_read_call_done,
0a702195 329 .rpc_count_stats = filelayout_read_count_stats,
dc70d7b3
AA
330 .rpc_release = filelayout_read_release,
331};
332
a69aef14
FI
333struct rpc_call_ops filelayout_write_call_ops = {
334 .rpc_call_prepare = filelayout_write_prepare,
335 .rpc_call_done = filelayout_write_call_done,
0a702195 336 .rpc_count_stats = filelayout_write_count_stats,
a69aef14
FI
337 .rpc_release = filelayout_write_release,
338};
339
e0c2b380
FI
340struct rpc_call_ops filelayout_commit_call_ops = {
341 .rpc_call_prepare = filelayout_write_prepare,
342 .rpc_call_done = filelayout_write_call_done,
0a702195 343 .rpc_count_stats = filelayout_write_count_stats,
e0c2b380
FI
344 .rpc_release = filelayout_commit_release,
345};
346
dc70d7b3
AA
347static enum pnfs_try_status
348filelayout_read_pagelist(struct nfs_read_data *data)
349{
350 struct pnfs_layout_segment *lseg = data->lseg;
351 struct nfs4_pnfs_ds *ds;
352 loff_t offset = data->args.offset;
353 u32 j, idx;
354 struct nfs_fh *fh;
355 int status;
356
357 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
358 __func__, data->inode->i_ino,
359 data->args.pgbase, (size_t)data->args.count, offset);
360
c47abcf8
AA
361 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
362 return PNFS_NOT_ATTEMPTED;
363
dc70d7b3
AA
364 /* Retrieve the correct rpc_client for the byte range */
365 j = nfs4_fl_calc_j_index(lseg, offset);
366 idx = nfs4_fl_calc_ds_index(lseg, j);
367 ds = nfs4_fl_prepare_ds(lseg, idx);
368 if (!ds) {
568e8c49
AA
369 /* Either layout fh index faulty, or ds connect failed */
370 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
371 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
dc70d7b3
AA
372 return PNFS_NOT_ATTEMPTED;
373 }
c9895cb6 374 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
dc70d7b3
AA
375
376 /* No multipath support. Use first DS */
377 data->ds_clp = ds->ds_clp;
378 fh = nfs4_fl_select_ds_fh(lseg, j);
379 if (fh)
380 data->args.fh = fh;
381
382 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
383 data->mds_offset = offset;
384
385 /* Perform an asynchronous read to ds */
386 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
387 &filelayout_read_call_ops);
388 BUG_ON(status != 0);
389 return PNFS_ATTEMPTED;
390}
391
a69aef14 392/* Perform async writes. */
0382b744
AA
393static enum pnfs_try_status
394filelayout_write_pagelist(struct nfs_write_data *data, int sync)
395{
a69aef14
FI
396 struct pnfs_layout_segment *lseg = data->lseg;
397 struct nfs4_pnfs_ds *ds;
398 loff_t offset = data->args.offset;
399 u32 j, idx;
400 struct nfs_fh *fh;
401 int status;
402
c47abcf8
AA
403 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
404 return PNFS_NOT_ATTEMPTED;
405
a69aef14
FI
406 /* Retrieve the correct rpc_client for the byte range */
407 j = nfs4_fl_calc_j_index(lseg, offset);
408 idx = nfs4_fl_calc_ds_index(lseg, j);
409 ds = nfs4_fl_prepare_ds(lseg, idx);
410 if (!ds) {
a030889a
WAA
411 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
412 __func__);
a69aef14
FI
413 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
414 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
415 return PNFS_NOT_ATTEMPTED;
416 }
c9895cb6 417 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
a69aef14 418 data->inode->i_ino, sync, (size_t) data->args.count, offset,
c9895cb6 419 ds->ds_remotestr);
a69aef14 420
a69aef14
FI
421 data->write_done_cb = filelayout_write_done_cb;
422 data->ds_clp = ds->ds_clp;
423 fh = nfs4_fl_select_ds_fh(lseg, j);
424 if (fh)
425 data->args.fh = fh;
426 /*
427 * Get the file offset on the dserver. Set the write offset to
428 * this offset and save the original offset.
429 */
430 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
a69aef14
FI
431
432 /* Perform an asynchronous write */
433 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
434 &filelayout_write_call_ops, sync);
435 BUG_ON(status != 0);
436 return PNFS_ATTEMPTED;
0382b744
AA
437}
438
16b374ca
AA
439/*
440 * filelayout_check_layout()
441 *
442 * Make sure layout segment parameters are sane WRT the device.
443 * At this point no generic layer initialization of the lseg has occurred,
444 * and nothing has been added to the layout_hdr cache.
445 *
446 */
447static int
448filelayout_check_layout(struct pnfs_layout_hdr *lo,
449 struct nfs4_filelayout_segment *fl,
450 struct nfs4_layoutget_res *lgr,
a75b9df9
TM
451 struct nfs4_deviceid *id,
452 gfp_t gfp_flags)
16b374ca 453{
a1eaecbc 454 struct nfs4_deviceid_node *d;
16b374ca
AA
455 struct nfs4_file_layout_dsaddr *dsaddr;
456 int status = -EINVAL;
b7edfaa1 457 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
16b374ca
AA
458
459 dprintk("--> %s\n", __func__);
460
7c24d948
AA
461 /* FIXME: remove this check when layout segment support is added */
462 if (lgr->range.offset != 0 ||
463 lgr->range.length != NFS4_MAX_UINT64) {
464 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
465 __func__);
466 goto out;
467 }
468
16b374ca 469 if (fl->pattern_offset > lgr->range.offset) {
3b6445a6 470 dprintk("%s pattern_offset %lld too large\n",
16b374ca
AA
471 __func__, fl->pattern_offset);
472 goto out;
473 }
474
75247aff
BH
475 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
476 dprintk("%s Invalid stripe unit (%u)\n",
16b374ca
AA
477 __func__, fl->stripe_unit);
478 goto out;
479 }
480
481 /* find and reference the deviceid */
35c8bb54
BH
482 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
483 NFS_SERVER(lo->plh_inode)->nfs_client, id);
a1eaecbc 484 if (d == NULL) {
a75b9df9 485 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
16b374ca
AA
486 if (dsaddr == NULL)
487 goto out;
a1eaecbc
BH
488 } else
489 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
c47abcf8
AA
490 /* Found deviceid is being reaped */
491 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
492 goto out_put;
493
16b374ca
AA
494 fl->dsaddr = dsaddr;
495
e414966b
CL
496 if (fl->first_stripe_index >= dsaddr->stripe_count) {
497 dprintk("%s Bad first_stripe_index %u\n",
16b374ca
AA
498 __func__, fl->first_stripe_index);
499 goto out_put;
500 }
501
502 if ((fl->stripe_type == STRIPE_SPARSE &&
503 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
504 (fl->stripe_type == STRIPE_DENSE &&
505 fl->num_fh != dsaddr->stripe_count)) {
506 dprintk("%s num_fh %u not valid for given packing\n",
507 __func__, fl->num_fh);
508 goto out_put;
509 }
510
511 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
512 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
513 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
514 nfss->wsize);
515 }
516
517 status = 0;
518out:
519 dprintk("--> %s returns %d\n", __func__, status);
520 return status;
521out_put:
ea8eecdd 522 nfs4_fl_put_deviceid(dsaddr);
16b374ca
AA
523 goto out;
524}
525
526static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
527{
528 int i;
529
530 for (i = 0; i < fl->num_fh; i++) {
531 if (!fl->fh_array[i])
532 break;
533 kfree(fl->fh_array[i]);
534 }
535 kfree(fl->fh_array);
536 fl->fh_array = NULL;
537}
538
539static void
540_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
541{
542 filelayout_free_fh_array(fl);
543 kfree(fl);
544}
545
546static int
547filelayout_decode_layout(struct pnfs_layout_hdr *flo,
548 struct nfs4_filelayout_segment *fl,
549 struct nfs4_layoutget_res *lgr,
a75b9df9
TM
550 struct nfs4_deviceid *id,
551 gfp_t gfp_flags)
16b374ca 552{
35124a09 553 struct xdr_stream stream;
f7da7a12 554 struct xdr_buf buf;
35124a09
WAA
555 struct page *scratch;
556 __be32 *p;
16b374ca
AA
557 uint32_t nfl_util;
558 int i;
559
560 dprintk("%s: set_layout_map Begin\n", __func__);
561
a75b9df9 562 scratch = alloc_page(gfp_flags);
35124a09
WAA
563 if (!scratch)
564 return -ENOMEM;
565
f7da7a12 566 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
35124a09
WAA
567 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
568
569 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
570 * num_fh (4) */
571 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
572 if (unlikely(!p))
573 goto out_err;
574
16b374ca
AA
575 memcpy(id, p, sizeof(*id));
576 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
a1eaecbc 577 nfs4_print_deviceid(id);
16b374ca
AA
578
579 nfl_util = be32_to_cpup(p++);
580 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
581 fl->commit_through_mds = 1;
582 if (nfl_util & NFL4_UFLG_DENSE)
583 fl->stripe_type = STRIPE_DENSE;
584 else
585 fl->stripe_type = STRIPE_SPARSE;
586 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
587
588 fl->first_stripe_index = be32_to_cpup(p++);
589 p = xdr_decode_hyper(p, &fl->pattern_offset);
590 fl->num_fh = be32_to_cpup(p++);
591
592 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
593 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
594 fl->pattern_offset);
595
cec765cf
AA
596 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
597 * Futher checking is done in filelayout_check_layout */
e414966b 598 if (fl->num_fh >
cec765cf 599 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
35124a09
WAA
600 goto out_err;
601
cec765cf
AA
602 if (fl->num_fh > 0) {
603 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
604 gfp_flags);
605 if (!fl->fh_array)
606 goto out_err;
607 }
16b374ca
AA
608
609 for (i = 0; i < fl->num_fh; i++) {
610 /* Do we want to use a mempool here? */
a75b9df9 611 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
35124a09
WAA
612 if (!fl->fh_array[i])
613 goto out_err_free;
614
615 p = xdr_inline_decode(&stream, 4);
616 if (unlikely(!p))
617 goto out_err_free;
16b374ca
AA
618 fl->fh_array[i]->size = be32_to_cpup(p++);
619 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
f9fd2d9c 620 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
16b374ca 621 i, fl->fh_array[i]->size);
35124a09 622 goto out_err_free;
16b374ca 623 }
35124a09
WAA
624
625 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
626 if (unlikely(!p))
627 goto out_err_free;
16b374ca 628 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
16b374ca
AA
629 dprintk("DEBUG: %s: fh len %d\n", __func__,
630 fl->fh_array[i]->size);
631 }
632
35124a09 633 __free_page(scratch);
16b374ca 634 return 0;
35124a09
WAA
635
636out_err_free:
637 filelayout_free_fh_array(fl);
638out_err:
639 __free_page(scratch);
640 return -EIO;
16b374ca
AA
641}
642
c879513e
FI
643static void
644filelayout_free_lseg(struct pnfs_layout_segment *lseg)
645{
646 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
647
648 dprintk("--> %s\n", __func__);
649 nfs4_fl_put_deviceid(fl->dsaddr);
425eb736 650 kfree(fl->commit_buckets);
c879513e
FI
651 _filelayout_free_lseg(fl);
652}
653
16b374ca
AA
654static struct pnfs_layout_segment *
655filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
a75b9df9
TM
656 struct nfs4_layoutget_res *lgr,
657 gfp_t gfp_flags)
16b374ca
AA
658{
659 struct nfs4_filelayout_segment *fl;
660 int rc;
661 struct nfs4_deviceid id;
662
663 dprintk("--> %s\n", __func__);
a75b9df9 664 fl = kzalloc(sizeof(*fl), gfp_flags);
16b374ca
AA
665 if (!fl)
666 return NULL;
667
a75b9df9
TM
668 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
669 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
16b374ca
AA
670 _filelayout_free_lseg(fl);
671 return NULL;
672 }
425eb736
FI
673
674 /* This assumes there is only one IOMODE_RW lseg. What
675 * we really want to do is have a layout_hdr level
676 * dictionary of <multipath_list4, fh> keys, each
677 * associated with a struct list_head, populated by calls
678 * to filelayout_write_pagelist().
679 * */
680 if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
681 int i;
682 int size = (fl->stripe_type == STRIPE_SPARSE) ?
683 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
684
a75b9df9 685 fl->commit_buckets = kcalloc(size, sizeof(struct list_head), gfp_flags);
425eb736
FI
686 if (!fl->commit_buckets) {
687 filelayout_free_lseg(&fl->generic_hdr);
688 return NULL;
689 }
690 fl->number_of_buckets = size;
691 for (i = 0; i < size; i++)
692 INIT_LIST_HEAD(&fl->commit_buckets[i]);
693 }
16b374ca
AA
694 return &fl->generic_hdr;
695}
696
94ad1c80
FI
697/*
698 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
699 *
18ad0a9f
BH
700 * return true : coalesce page
701 * return false : don't coalesce page
94ad1c80 702 */
1751c363 703static bool
94ad1c80
FI
704filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
705 struct nfs_page *req)
706{
707 u64 p_stripe, r_stripe;
708 u32 stripe_unit;
709
19345cb2
BH
710 if (!pnfs_generic_pg_test(pgio, prev, req) ||
711 !nfs_generic_pg_test(pgio, prev, req))
712 return false;
89a58e32 713
94ad1c80
FI
714 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
715 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
716 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
717
718 do_div(p_stripe, stripe_unit);
719 do_div(r_stripe, stripe_unit);
720
721 return (p_stripe == r_stripe);
722}
723
7c24d948
AA
724void
725filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
726 struct nfs_page *req)
727{
728 BUG_ON(pgio->pg_lseg != NULL);
729
730 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
731 req->wb_context,
732 0,
733 NFS4_MAX_UINT64,
734 IOMODE_READ,
735 GFP_KERNEL);
736 /* If no lseg, fall back to read through mds */
737 if (pgio->pg_lseg == NULL)
1f945357 738 nfs_pageio_reset_read_mds(pgio);
7c24d948
AA
739}
740
741void
742filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
743 struct nfs_page *req)
744{
745 BUG_ON(pgio->pg_lseg != NULL);
746
747 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
748 req->wb_context,
749 0,
750 NFS4_MAX_UINT64,
751 IOMODE_RW,
752 GFP_NOFS);
753 /* If no lseg, fall back to write through mds */
754 if (pgio->pg_lseg == NULL)
1f945357 755 nfs_pageio_reset_write_mds(pgio);
7c24d948
AA
756}
757
1751c363 758static const struct nfs_pageio_ops filelayout_pg_read_ops = {
7c24d948 759 .pg_init = filelayout_pg_init_read,
1751c363 760 .pg_test = filelayout_pg_test,
493292dd 761 .pg_doio = pnfs_generic_pg_readpages,
1751c363
TM
762};
763
764static const struct nfs_pageio_ops filelayout_pg_write_ops = {
7c24d948 765 .pg_init = filelayout_pg_init_write,
1751c363 766 .pg_test = filelayout_pg_test,
dce81290 767 .pg_doio = pnfs_generic_pg_writepages,
1751c363
TM
768};
769
e0c2b380
FI
770static bool filelayout_mark_pnfs_commit(struct pnfs_layout_segment *lseg)
771{
772 return !FILELAYOUT_LSEG(lseg)->commit_through_mds;
773}
774
775static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
776{
777 if (fl->stripe_type == STRIPE_SPARSE)
778 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
779 else
780 return j;
781}
782
783struct list_head *filelayout_choose_commit_list(struct nfs_page *req)
784{
785 struct pnfs_layout_segment *lseg = req->wb_commit_lseg;
786 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
787 u32 i, j;
788 struct list_head *list;
789
790 /* Note that we are calling nfs4_fl_calc_j_index on each page
791 * that ends up being committed to a data server. An attractive
792 * alternative is to add a field to nfs_write_data and nfs_page
793 * to store the value calculated in filelayout_write_pagelist
794 * and just use that here.
795 */
796 j = nfs4_fl_calc_j_index(lseg,
797 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
798 i = select_bucket_index(fl, j);
799 list = &fl->commit_buckets[i];
800 if (list_empty(list)) {
801 /* Non-empty buckets hold a reference on the lseg */
802 get_lseg(lseg);
803 }
804 return list;
805}
806
807static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
808{
809 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
810
811 if (flseg->stripe_type == STRIPE_SPARSE)
812 return i;
813 else
814 return nfs4_fl_calc_ds_index(lseg, i);
815}
816
817static struct nfs_fh *
818select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
819{
820 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
821
822 if (flseg->stripe_type == STRIPE_SPARSE) {
823 if (flseg->num_fh == 1)
824 i = 0;
825 else if (flseg->num_fh == 0)
826 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
827 return NULL;
828 }
829 return flseg->fh_array[i];
830}
831
832static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
833{
834 struct pnfs_layout_segment *lseg = data->lseg;
835 struct nfs4_pnfs_ds *ds;
836 u32 idx;
837 struct nfs_fh *fh;
838
839 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
840 ds = nfs4_fl_prepare_ds(lseg, idx);
841 if (!ds) {
a030889a
WAA
842 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
843 __func__);
e0c2b380
FI
844 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
845 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
846 prepare_to_resend_writes(data);
847 data->mds_ops->rpc_release(data);
848 return -EAGAIN;
849 }
850 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
851 data->write_done_cb = filelayout_commit_done_cb;
852 data->ds_clp = ds->ds_clp;
853 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
854 if (fh)
855 data->args.fh = fh;
856 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
857 &filelayout_commit_call_ops, how);
858}
859
860/*
861 * This is only useful while we are using whole file layouts.
862 */
863static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
864{
865 struct pnfs_layout_segment *lseg, *rv = NULL;
866
867 spin_lock(&inode->i_lock);
868 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
869 if (lseg->pls_range.iomode == IOMODE_RW)
870 rv = get_lseg(lseg);
871 spin_unlock(&inode->i_lock);
872 return rv;
873}
874
875static int alloc_ds_commits(struct inode *inode, struct list_head *list)
876{
877 struct pnfs_layout_segment *lseg;
878 struct nfs4_filelayout_segment *fl;
879 struct nfs_write_data *data;
880 int i, j;
881
882 /* Won't need this when non-whole file layout segments are supported
883 * instead we will use a pnfs_layout_hdr structure */
884 lseg = find_only_write_lseg(inode);
885 if (!lseg)
886 return 0;
887 fl = FILELAYOUT_LSEG(lseg);
888 for (i = 0; i < fl->number_of_buckets; i++) {
889 if (list_empty(&fl->commit_buckets[i]))
890 continue;
891 data = nfs_commitdata_alloc();
892 if (!data)
893 goto out_bad;
894 data->ds_commit_index = i;
895 data->lseg = lseg;
896 list_add(&data->pages, list);
897 }
898 put_lseg(lseg);
899 return 0;
900
901out_bad:
902 for (j = i; j < fl->number_of_buckets; j++) {
903 if (list_empty(&fl->commit_buckets[i]))
904 continue;
905 nfs_retry_commit(&fl->commit_buckets[i], lseg);
906 put_lseg(lseg); /* associated with emptying bucket */
907 }
908 put_lseg(lseg);
909 /* Caller will clean up entries put on list */
910 return -ENOMEM;
911}
912
913/* This follows nfs_commit_list pretty closely */
914static int
915filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
916 int how)
917{
918 struct nfs_write_data *data, *tmp;
919 LIST_HEAD(list);
920
921 if (!list_empty(mds_pages)) {
922 data = nfs_commitdata_alloc();
923 if (!data)
924 goto out_bad;
925 data->lseg = NULL;
926 list_add(&data->pages, &list);
927 }
928
929 if (alloc_ds_commits(inode, &list))
930 goto out_bad;
931
932 list_for_each_entry_safe(data, tmp, &list, pages) {
933 list_del_init(&data->pages);
934 atomic_inc(&NFS_I(inode)->commits_outstanding);
935 if (!data->lseg) {
936 nfs_init_commit(data, mds_pages, NULL);
937 nfs_initiate_commit(data, NFS_CLIENT(inode),
938 data->mds_ops, how);
939 } else {
940 nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index], data->lseg);
941 filelayout_initiate_commit(data, how);
942 }
943 }
944 return 0;
945 out_bad:
946 list_for_each_entry_safe(data, tmp, &list, pages) {
947 nfs_retry_commit(&data->pages, data->lseg);
948 list_del_init(&data->pages);
949 nfs_commit_free(data);
950 }
951 nfs_retry_commit(mds_pages, NULL);
952 nfs_commit_clear_lock(NFS_I(inode));
953 return -ENOMEM;
954}
955
1775bc34
BH
956static void
957filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
958{
959 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
960}
961
7ab672ce 962static struct pnfs_layoutdriver_type filelayout_type = {
ea8eecdd
CH
963 .id = LAYOUT_NFSV4_1_FILES,
964 .name = "LAYOUT_NFSV4_1_FILES",
965 .owner = THIS_MODULE,
966 .alloc_lseg = filelayout_alloc_lseg,
967 .free_lseg = filelayout_free_lseg,
1751c363
TM
968 .pg_read_ops = &filelayout_pg_read_ops,
969 .pg_write_ops = &filelayout_pg_write_ops,
e0c2b380
FI
970 .mark_pnfs_commit = filelayout_mark_pnfs_commit,
971 .choose_commit_list = filelayout_choose_commit_list,
972 .commit_pagelist = filelayout_commit_pagelist,
dc70d7b3 973 .read_pagelist = filelayout_read_pagelist,
0382b744 974 .write_pagelist = filelayout_write_pagelist,
1775bc34 975 .free_deviceid_node = filelayout_free_deveiceid_node,
7ab672ce
DH
976};
977
978static int __init nfs4filelayout_init(void)
979{
980 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
981 __func__);
982 return pnfs_register_layoutdriver(&filelayout_type);
983}
984
985static void __exit nfs4filelayout_exit(void)
986{
987 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
988 __func__);
989 pnfs_unregister_layoutdriver(&filelayout_type);
990}
991
f85ef69c
BF
992MODULE_ALIAS("nfs-layouttype4-1");
993
7ab672ce
DH
994module_init(nfs4filelayout_init);
995module_exit(nfs4filelayout_exit);