]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/nfs4filelayout.c
NFSv4.1 remove nfs4_reset_write and nfs4_reset_read
[mirror_ubuntu-bionic-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
e7dd79af
AA
85static void filelayout_reset_write(struct nfs_write_data *data)
86{
87 struct nfs_pgio_header *hdr = data->header;
88 struct inode *inode = hdr->inode;
89 struct rpc_task *task = &data->task;
90
91 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
92 dprintk("%s Reset task %5u for i/o through MDS "
93 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
94 data->task.tk_pid,
95 inode->i_sb->s_id,
96 (long long)NFS_FILEID(inode),
97 data->args.count,
98 (unsigned long long)data->args.offset);
99
100 task->tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
101 &hdr->pages,
102 hdr->completion_ops);
103 }
104}
105
106static void filelayout_reset_read(struct nfs_read_data *data)
107{
108 struct nfs_pgio_header *hdr = data->header;
109 struct inode *inode = hdr->inode;
110 struct rpc_task *task = &data->task;
111
112 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
113 dprintk("%s Reset task %5u for i/o through MDS "
114 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
115 data->task.tk_pid,
116 inode->i_sb->s_id,
117 (long long)NFS_FILEID(inode),
118 data->args.count,
119 (unsigned long long)data->args.offset);
120
121 task->tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
122 &hdr->pages,
123 hdr->completion_ops);
124 }
125}
126
cbdabc7f
AA
127static int filelayout_async_handle_error(struct rpc_task *task,
128 struct nfs4_state *state,
129 struct nfs_client *clp,
e7dd79af 130 struct pnfs_layout_segment *lseg)
cbdabc7f 131{
e7dd79af
AA
132 struct inode *inode = lseg->pls_layout->plh_inode;
133 struct nfs_server *mds_server = NFS_SERVER(inode);
134 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
9cb81968
AA
135 struct nfs_client *mds_client = mds_server->nfs_client;
136
cbdabc7f
AA
137 if (task->tk_status >= 0)
138 return 0;
cbdabc7f
AA
139
140 switch (task->tk_status) {
9cb81968
AA
141 /* MDS state errors */
142 case -NFS4ERR_DELEG_REVOKED:
143 case -NFS4ERR_ADMIN_REVOKED:
144 case -NFS4ERR_BAD_STATEID:
e7dd79af
AA
145 if (state == NULL)
146 break;
2dc31756 147 nfs_remove_bad_delegation(state->inode);
9cb81968 148 case -NFS4ERR_OPENMODE:
e7dd79af
AA
149 if (state == NULL)
150 break;
9cb81968
AA
151 nfs4_schedule_stateid_recovery(mds_server, state);
152 goto wait_on_recovery;
153 case -NFS4ERR_EXPIRED:
e7dd79af
AA
154 if (state != NULL)
155 nfs4_schedule_stateid_recovery(mds_server, state);
9cb81968
AA
156 nfs4_schedule_lease_recovery(mds_client);
157 goto wait_on_recovery;
158 /* DS session errors */
cbdabc7f
AA
159 case -NFS4ERR_BADSESSION:
160 case -NFS4ERR_BADSLOT:
161 case -NFS4ERR_BAD_HIGH_SLOT:
162 case -NFS4ERR_DEADSESSION:
163 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
164 case -NFS4ERR_SEQ_FALSE_RETRY:
165 case -NFS4ERR_SEQ_MISORDERED:
166 dprintk("%s ERROR %d, Reset session. Exchangeid "
167 "flags 0x%x\n", __func__, task->tk_status,
168 clp->cl_exchange_flags);
169 nfs4_schedule_session_recovery(clp->cl_session);
170 break;
171 case -NFS4ERR_DELAY:
172 case -NFS4ERR_GRACE:
173 case -EKEYEXPIRED:
174 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
175 break;
a8a4ae3a
AA
176 case -NFS4ERR_RETRY_UNCACHED_REP:
177 break;
e7dd79af
AA
178 /* RPC connection errors */
179 case -ECONNREFUSED:
180 case -EHOSTDOWN:
181 case -EHOSTUNREACH:
182 case -ENETUNREACH:
183 case -EIO:
184 case -ETIMEDOUT:
185 case -EPIPE:
186 dprintk("%s DS connection error %d\n", __func__,
187 task->tk_status);
188 filelayout_mark_devid_invalid(devid);
189 /* fall through */
cbdabc7f 190 default:
e7dd79af 191 dprintk("%s Retry through MDS. Error %d\n", __func__,
cbdabc7f 192 task->tk_status);
e7dd79af 193 return -NFS4ERR_RESET_TO_MDS;
cbdabc7f 194 }
9cb81968 195out:
cbdabc7f
AA
196 task->tk_status = 0;
197 return -EAGAIN;
9cb81968
AA
198wait_on_recovery:
199 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
200 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
201 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
202 goto out;
cbdabc7f
AA
203}
204
205/* NFS_PROTO call done callback routines */
206
207static int filelayout_read_done_cb(struct rpc_task *task,
208 struct nfs_read_data *data)
209{
e7dd79af
AA
210 struct nfs_pgio_header *hdr = data->header;
211 int err;
cbdabc7f 212
e7dd79af
AA
213 err = filelayout_async_handle_error(task, data->args.context->state,
214 data->ds_clp, hdr->lseg);
cbdabc7f 215
e7dd79af
AA
216 switch (err) {
217 case -NFS4ERR_RESET_TO_MDS:
218 filelayout_reset_read(data);
219 return task->tk_status;
220 case -EAGAIN:
d00c5d43 221 rpc_restart_call_prepare(task);
cbdabc7f
AA
222 return -EAGAIN;
223 }
224
225 return 0;
226}
227
863a3c6c
AA
228/*
229 * We reference the rpc_cred of the first WRITE that triggers the need for
230 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
231 * rfc5661 is not clear about which credential should be used.
232 */
233static void
234filelayout_set_layoutcommit(struct nfs_write_data *wdata)
235{
cd841605
FI
236 struct nfs_pgio_header *hdr = wdata->header;
237
238 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
863a3c6c
AA
239 wdata->res.verf->committed == NFS_FILE_SYNC)
240 return;
241
242 pnfs_set_layoutcommit(wdata);
cd841605
FI
243 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
244 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
863a3c6c
AA
245}
246
dc70d7b3
AA
247/*
248 * Call ops for the async read/write cases
249 * In the case of dense layouts, the offset needs to be reset to its
250 * original value.
251 */
252static void filelayout_read_prepare(struct rpc_task *task, void *data)
253{
cd12ae32 254 struct nfs_read_data *rdata = data;
dc70d7b3 255
cbdabc7f
AA
256 rdata->read_done_cb = filelayout_read_done_cb;
257
dc70d7b3
AA
258 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
259 &rdata->args.seq_args, &rdata->res.seq_res,
9d12b216 260 task))
dc70d7b3
AA
261 return;
262
263 rpc_call_start(task);
264}
265
266static void filelayout_read_call_done(struct rpc_task *task, void *data)
267{
cd12ae32 268 struct nfs_read_data *rdata = data;
dc70d7b3
AA
269
270 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
271
272 /* Note this may cause RPC to be resent */
cd841605 273 rdata->header->mds_ops->rpc_call_done(task, data);
dc70d7b3
AA
274}
275
0a702195
WAA
276static void filelayout_read_count_stats(struct rpc_task *task, void *data)
277{
cd12ae32 278 struct nfs_read_data *rdata = data;
0a702195 279
cd841605 280 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
0a702195
WAA
281}
282
dc70d7b3
AA
283static void filelayout_read_release(void *data)
284{
cd12ae32 285 struct nfs_read_data *rdata = data;
dc70d7b3 286
cd841605 287 rdata->header->mds_ops->rpc_release(data);
dc70d7b3
AA
288}
289
a69aef14
FI
290static int filelayout_write_done_cb(struct rpc_task *task,
291 struct nfs_write_data *data)
292{
e7dd79af
AA
293 struct nfs_pgio_header *hdr = data->header;
294 int err;
295
296 err = filelayout_async_handle_error(task, data->args.context->state,
297 data->ds_clp, hdr->lseg);
298
299 switch (err) {
300 case -NFS4ERR_RESET_TO_MDS:
301 filelayout_reset_write(data);
302 return task->tk_status;
303 case -EAGAIN:
d00c5d43 304 rpc_restart_call_prepare(task);
a69aef14
FI
305 return -EAGAIN;
306 }
307
863a3c6c 308 filelayout_set_layoutcommit(data);
a69aef14
FI
309 return 0;
310}
311
e0c2b380 312/* Fake up some data that will cause nfs_commit_release to retry the writes. */
0b7c0153 313static void prepare_to_resend_writes(struct nfs_commit_data *data)
e0c2b380
FI
314{
315 struct nfs_page *first = nfs_list_entry(data->pages.next);
316
317 data->task.tk_status = 0;
318 memcpy(data->verf.verifier, first->wb_verf.verifier,
319 sizeof(first->wb_verf.verifier));
320 data->verf.verifier[0]++; /* ensure verifier mismatch */
321}
322
323static int filelayout_commit_done_cb(struct rpc_task *task,
0b7c0153 324 struct nfs_commit_data *data)
e0c2b380 325{
e7dd79af
AA
326 int err;
327
328 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
329 data->lseg);
330
331 switch (err) {
332 case -NFS4ERR_RESET_TO_MDS:
333 prepare_to_resend_writes(data);
334 return -EAGAIN;
335 case -EAGAIN:
336 rpc_restart_call_prepare(task);
e0c2b380
FI
337 return -EAGAIN;
338 }
339
340 return 0;
341}
342
a69aef14
FI
343static void filelayout_write_prepare(struct rpc_task *task, void *data)
344{
cd12ae32 345 struct nfs_write_data *wdata = data;
a69aef14
FI
346
347 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
348 &wdata->args.seq_args, &wdata->res.seq_res,
9d12b216 349 task))
a69aef14
FI
350 return;
351
352 rpc_call_start(task);
353}
354
355static void filelayout_write_call_done(struct rpc_task *task, void *data)
356{
cd12ae32 357 struct nfs_write_data *wdata = data;
a69aef14
FI
358
359 /* Note this may cause RPC to be resent */
cd841605 360 wdata->header->mds_ops->rpc_call_done(task, data);
a69aef14
FI
361}
362
0a702195
WAA
363static void filelayout_write_count_stats(struct rpc_task *task, void *data)
364{
cd12ae32 365 struct nfs_write_data *wdata = data;
0a702195 366
cd841605 367 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
0a702195
WAA
368}
369
a69aef14
FI
370static void filelayout_write_release(void *data)
371{
cd12ae32 372 struct nfs_write_data *wdata = data;
a69aef14 373
cd841605 374 wdata->header->mds_ops->rpc_release(data);
a69aef14
FI
375}
376
0b7c0153 377static void filelayout_commit_prepare(struct rpc_task *task, void *data)
e0c2b380 378{
0b7c0153 379 struct nfs_commit_data *wdata = data;
e0c2b380 380
0b7c0153
FI
381 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
382 &wdata->args.seq_args, &wdata->res.seq_res,
383 task))
384 return;
385
386 rpc_call_start(task);
387}
388
389static void filelayout_write_commit_done(struct rpc_task *task, void *data)
390{
391 struct nfs_commit_data *wdata = data;
392
393 /* Note this may cause RPC to be resent */
394 wdata->mds_ops->rpc_call_done(task, data);
395}
396
397static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
398{
399 struct nfs_commit_data *cdata = data;
400
401 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
402}
403
404static void filelayout_commit_release(void *calldata)
405{
406 struct nfs_commit_data *data = calldata;
407
f453a54a 408 data->completion_ops->completion(data);
0b7c0153
FI
409 put_lseg(data->lseg);
410 nfs_commitdata_release(data);
e0c2b380
FI
411}
412
17280175 413static const struct rpc_call_ops filelayout_read_call_ops = {
dc70d7b3
AA
414 .rpc_call_prepare = filelayout_read_prepare,
415 .rpc_call_done = filelayout_read_call_done,
0a702195 416 .rpc_count_stats = filelayout_read_count_stats,
dc70d7b3
AA
417 .rpc_release = filelayout_read_release,
418};
419
17280175 420static const struct rpc_call_ops filelayout_write_call_ops = {
a69aef14
FI
421 .rpc_call_prepare = filelayout_write_prepare,
422 .rpc_call_done = filelayout_write_call_done,
0a702195 423 .rpc_count_stats = filelayout_write_count_stats,
a69aef14
FI
424 .rpc_release = filelayout_write_release,
425};
426
17280175 427static const struct rpc_call_ops filelayout_commit_call_ops = {
0b7c0153
FI
428 .rpc_call_prepare = filelayout_commit_prepare,
429 .rpc_call_done = filelayout_write_commit_done,
430 .rpc_count_stats = filelayout_commit_count_stats,
e0c2b380
FI
431 .rpc_release = filelayout_commit_release,
432};
433
dc70d7b3
AA
434static enum pnfs_try_status
435filelayout_read_pagelist(struct nfs_read_data *data)
436{
cd841605
FI
437 struct nfs_pgio_header *hdr = data->header;
438 struct pnfs_layout_segment *lseg = hdr->lseg;
dc70d7b3
AA
439 struct nfs4_pnfs_ds *ds;
440 loff_t offset = data->args.offset;
441 u32 j, idx;
442 struct nfs_fh *fh;
443 int status;
444
445 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
cd841605 446 __func__, hdr->inode->i_ino,
dc70d7b3
AA
447 data->args.pgbase, (size_t)data->args.count, offset);
448
449 /* Retrieve the correct rpc_client for the byte range */
450 j = nfs4_fl_calc_j_index(lseg, offset);
451 idx = nfs4_fl_calc_ds_index(lseg, j);
452 ds = nfs4_fl_prepare_ds(lseg, idx);
90fecfcb 453 if (!ds)
dc70d7b3 454 return PNFS_NOT_ATTEMPTED;
c9895cb6 455 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
dc70d7b3
AA
456
457 /* No multipath support. Use first DS */
458 data->ds_clp = ds->ds_clp;
459 fh = nfs4_fl_select_ds_fh(lseg, j);
460 if (fh)
461 data->args.fh = fh;
462
463 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
464 data->mds_offset = offset;
465
466 /* Perform an asynchronous read to ds */
c5996c4e 467 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
9f0ec176 468 &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
dc70d7b3
AA
469 BUG_ON(status != 0);
470 return PNFS_ATTEMPTED;
471}
472
a69aef14 473/* Perform async writes. */
0382b744
AA
474static enum pnfs_try_status
475filelayout_write_pagelist(struct nfs_write_data *data, int sync)
476{
cd841605
FI
477 struct nfs_pgio_header *hdr = data->header;
478 struct pnfs_layout_segment *lseg = hdr->lseg;
a69aef14
FI
479 struct nfs4_pnfs_ds *ds;
480 loff_t offset = data->args.offset;
481 u32 j, idx;
482 struct nfs_fh *fh;
483 int status;
484
485 /* Retrieve the correct rpc_client for the byte range */
486 j = nfs4_fl_calc_j_index(lseg, offset);
487 idx = nfs4_fl_calc_ds_index(lseg, j);
488 ds = nfs4_fl_prepare_ds(lseg, idx);
90fecfcb 489 if (!ds)
a69aef14 490 return PNFS_NOT_ATTEMPTED;
c9895cb6 491 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
cd841605 492 hdr->inode->i_ino, sync, (size_t) data->args.count, offset,
c9895cb6 493 ds->ds_remotestr);
a69aef14 494
a69aef14
FI
495 data->write_done_cb = filelayout_write_done_cb;
496 data->ds_clp = ds->ds_clp;
497 fh = nfs4_fl_select_ds_fh(lseg, j);
498 if (fh)
499 data->args.fh = fh;
500 /*
501 * Get the file offset on the dserver. Set the write offset to
502 * this offset and save the original offset.
503 */
504 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
a69aef14
FI
505
506 /* Perform an asynchronous write */
c5996c4e 507 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
9f0ec176
AA
508 &filelayout_write_call_ops, sync,
509 RPC_TASK_SOFTCONN);
a69aef14
FI
510 BUG_ON(status != 0);
511 return PNFS_ATTEMPTED;
0382b744
AA
512}
513
16b374ca
AA
514/*
515 * filelayout_check_layout()
516 *
517 * Make sure layout segment parameters are sane WRT the device.
518 * At this point no generic layer initialization of the lseg has occurred,
519 * and nothing has been added to the layout_hdr cache.
520 *
521 */
522static int
523filelayout_check_layout(struct pnfs_layout_hdr *lo,
524 struct nfs4_filelayout_segment *fl,
525 struct nfs4_layoutget_res *lgr,
a75b9df9
TM
526 struct nfs4_deviceid *id,
527 gfp_t gfp_flags)
16b374ca 528{
a1eaecbc 529 struct nfs4_deviceid_node *d;
16b374ca
AA
530 struct nfs4_file_layout_dsaddr *dsaddr;
531 int status = -EINVAL;
b7edfaa1 532 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
16b374ca
AA
533
534 dprintk("--> %s\n", __func__);
535
7c24d948
AA
536 /* FIXME: remove this check when layout segment support is added */
537 if (lgr->range.offset != 0 ||
538 lgr->range.length != NFS4_MAX_UINT64) {
539 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
540 __func__);
541 goto out;
542 }
543
16b374ca 544 if (fl->pattern_offset > lgr->range.offset) {
3b6445a6 545 dprintk("%s pattern_offset %lld too large\n",
16b374ca
AA
546 __func__, fl->pattern_offset);
547 goto out;
548 }
549
75247aff
BH
550 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
551 dprintk("%s Invalid stripe unit (%u)\n",
16b374ca
AA
552 __func__, fl->stripe_unit);
553 goto out;
554 }
555
556 /* find and reference the deviceid */
35c8bb54
BH
557 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
558 NFS_SERVER(lo->plh_inode)->nfs_client, id);
a1eaecbc 559 if (d == NULL) {
a75b9df9 560 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
16b374ca
AA
561 if (dsaddr == NULL)
562 goto out;
a1eaecbc
BH
563 } else
564 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
c47abcf8
AA
565 /* Found deviceid is being reaped */
566 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
567 goto out_put;
568
16b374ca
AA
569 fl->dsaddr = dsaddr;
570
e414966b
CL
571 if (fl->first_stripe_index >= dsaddr->stripe_count) {
572 dprintk("%s Bad first_stripe_index %u\n",
16b374ca
AA
573 __func__, fl->first_stripe_index);
574 goto out_put;
575 }
576
577 if ((fl->stripe_type == STRIPE_SPARSE &&
578 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
579 (fl->stripe_type == STRIPE_DENSE &&
580 fl->num_fh != dsaddr->stripe_count)) {
581 dprintk("%s num_fh %u not valid for given packing\n",
582 __func__, fl->num_fh);
583 goto out_put;
584 }
585
586 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
587 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
588 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
589 nfss->wsize);
590 }
591
592 status = 0;
593out:
594 dprintk("--> %s returns %d\n", __func__, status);
595 return status;
596out_put:
ea8eecdd 597 nfs4_fl_put_deviceid(dsaddr);
16b374ca
AA
598 goto out;
599}
600
601static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
602{
603 int i;
604
605 for (i = 0; i < fl->num_fh; i++) {
606 if (!fl->fh_array[i])
607 break;
608 kfree(fl->fh_array[i]);
609 }
610 kfree(fl->fh_array);
611 fl->fh_array = NULL;
612}
613
614static void
615_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
616{
617 filelayout_free_fh_array(fl);
618 kfree(fl);
619}
620
621static int
622filelayout_decode_layout(struct pnfs_layout_hdr *flo,
623 struct nfs4_filelayout_segment *fl,
624 struct nfs4_layoutget_res *lgr,
a75b9df9
TM
625 struct nfs4_deviceid *id,
626 gfp_t gfp_flags)
16b374ca 627{
35124a09 628 struct xdr_stream stream;
f7da7a12 629 struct xdr_buf buf;
35124a09
WAA
630 struct page *scratch;
631 __be32 *p;
16b374ca
AA
632 uint32_t nfl_util;
633 int i;
634
635 dprintk("%s: set_layout_map Begin\n", __func__);
636
a75b9df9 637 scratch = alloc_page(gfp_flags);
35124a09
WAA
638 if (!scratch)
639 return -ENOMEM;
640
f7da7a12 641 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
35124a09
WAA
642 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
643
644 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
645 * num_fh (4) */
646 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
647 if (unlikely(!p))
648 goto out_err;
649
16b374ca
AA
650 memcpy(id, p, sizeof(*id));
651 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
a1eaecbc 652 nfs4_print_deviceid(id);
16b374ca
AA
653
654 nfl_util = be32_to_cpup(p++);
655 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
656 fl->commit_through_mds = 1;
657 if (nfl_util & NFL4_UFLG_DENSE)
658 fl->stripe_type = STRIPE_DENSE;
659 else
660 fl->stripe_type = STRIPE_SPARSE;
661 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
662
663 fl->first_stripe_index = be32_to_cpup(p++);
664 p = xdr_decode_hyper(p, &fl->pattern_offset);
665 fl->num_fh = be32_to_cpup(p++);
666
667 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
668 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
669 fl->pattern_offset);
670
cec765cf
AA
671 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
672 * Futher checking is done in filelayout_check_layout */
e414966b 673 if (fl->num_fh >
cec765cf 674 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
35124a09
WAA
675 goto out_err;
676
cec765cf
AA
677 if (fl->num_fh > 0) {
678 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
679 gfp_flags);
680 if (!fl->fh_array)
681 goto out_err;
682 }
16b374ca
AA
683
684 for (i = 0; i < fl->num_fh; i++) {
685 /* Do we want to use a mempool here? */
a75b9df9 686 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
35124a09
WAA
687 if (!fl->fh_array[i])
688 goto out_err_free;
689
690 p = xdr_inline_decode(&stream, 4);
691 if (unlikely(!p))
692 goto out_err_free;
16b374ca
AA
693 fl->fh_array[i]->size = be32_to_cpup(p++);
694 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
f9fd2d9c 695 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
16b374ca 696 i, fl->fh_array[i]->size);
35124a09 697 goto out_err_free;
16b374ca 698 }
35124a09
WAA
699
700 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
701 if (unlikely(!p))
702 goto out_err_free;
16b374ca 703 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
16b374ca
AA
704 dprintk("DEBUG: %s: fh len %d\n", __func__,
705 fl->fh_array[i]->size);
706 }
707
35124a09 708 __free_page(scratch);
16b374ca 709 return 0;
35124a09
WAA
710
711out_err_free:
712 filelayout_free_fh_array(fl);
713out_err:
714 __free_page(scratch);
715 return -EIO;
16b374ca
AA
716}
717
c879513e
FI
718static void
719filelayout_free_lseg(struct pnfs_layout_segment *lseg)
720{
721 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
722
723 dprintk("--> %s\n", __func__);
724 nfs4_fl_put_deviceid(fl->dsaddr);
799ba8d5
FI
725 /* This assumes a single RW lseg */
726 if (lseg->pls_range.iomode == IOMODE_RW) {
727 struct nfs4_filelayout *flo;
728
729 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
730 flo->commit_info.nbuckets = 0;
731 kfree(flo->commit_info.buckets);
732 flo->commit_info.buckets = NULL;
733 }
c879513e
FI
734 _filelayout_free_lseg(fl);
735}
736
799ba8d5
FI
737static int
738filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
ea2cf228 739 struct nfs_commit_info *cinfo,
799ba8d5
FI
740 gfp_t gfp_flags)
741{
742 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
ea2cf228 743 struct pnfs_commit_bucket *buckets;
799ba8d5
FI
744 int size;
745
746 if (fl->commit_through_mds)
747 return 0;
ea2cf228 748 if (cinfo->ds->nbuckets != 0) {
799ba8d5
FI
749 /* This assumes there is only one IOMODE_RW lseg. What
750 * we really want to do is have a layout_hdr level
751 * dictionary of <multipath_list4, fh> keys, each
752 * associated with a struct list_head, populated by calls
753 * to filelayout_write_pagelist().
754 * */
755 return 0;
756 }
757
758 size = (fl->stripe_type == STRIPE_SPARSE) ?
759 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
760
ea2cf228 761 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
799ba8d5
FI
762 gfp_flags);
763 if (!buckets)
764 return -ENOMEM;
765 else {
766 int i;
767
ea2cf228
FI
768 spin_lock(cinfo->lock);
769 if (cinfo->ds->nbuckets != 0)
799ba8d5
FI
770 kfree(buckets);
771 else {
ea2cf228
FI
772 cinfo->ds->buckets = buckets;
773 cinfo->ds->nbuckets = size;
799ba8d5
FI
774 for (i = 0; i < size; i++) {
775 INIT_LIST_HEAD(&buckets[i].written);
776 INIT_LIST_HEAD(&buckets[i].committing);
777 }
778 }
ea2cf228 779 spin_unlock(cinfo->lock);
799ba8d5
FI
780 return 0;
781 }
782}
783
16b374ca
AA
784static struct pnfs_layout_segment *
785filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
a75b9df9
TM
786 struct nfs4_layoutget_res *lgr,
787 gfp_t gfp_flags)
16b374ca
AA
788{
789 struct nfs4_filelayout_segment *fl;
790 int rc;
791 struct nfs4_deviceid id;
792
793 dprintk("--> %s\n", __func__);
a75b9df9 794 fl = kzalloc(sizeof(*fl), gfp_flags);
16b374ca
AA
795 if (!fl)
796 return NULL;
797
a75b9df9
TM
798 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
799 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
16b374ca
AA
800 _filelayout_free_lseg(fl);
801 return NULL;
802 }
803 return &fl->generic_hdr;
804}
805
94ad1c80
FI
806/*
807 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
808 *
18ad0a9f
BH
809 * return true : coalesce page
810 * return false : don't coalesce page
94ad1c80 811 */
1751c363 812static bool
94ad1c80
FI
813filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
814 struct nfs_page *req)
815{
816 u64 p_stripe, r_stripe;
817 u32 stripe_unit;
818
19345cb2
BH
819 if (!pnfs_generic_pg_test(pgio, prev, req) ||
820 !nfs_generic_pg_test(pgio, prev, req))
821 return false;
89a58e32 822
b5542849
FI
823 p_stripe = (u64)req_offset(prev);
824 r_stripe = (u64)req_offset(req);
94ad1c80
FI
825 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
826
827 do_div(p_stripe, stripe_unit);
828 do_div(r_stripe, stripe_unit);
829
830 return (p_stripe == r_stripe);
831}
832
17280175 833static void
7c24d948
AA
834filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
835 struct nfs_page *req)
836{
837 BUG_ON(pgio->pg_lseg != NULL);
838
1825a0d0
FI
839 if (req->wb_offset != req->wb_pgbase) {
840 /*
841 * Handling unaligned pages is difficult, because have to
842 * somehow split a req in two in certain cases in the
843 * pg.test code. Avoid this by just not using pnfs
844 * in this case.
845 */
846 nfs_pageio_reset_read_mds(pgio);
847 return;
848 }
7c24d948
AA
849 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
850 req->wb_context,
851 0,
852 NFS4_MAX_UINT64,
853 IOMODE_READ,
854 GFP_KERNEL);
855 /* If no lseg, fall back to read through mds */
856 if (pgio->pg_lseg == NULL)
1f945357 857 nfs_pageio_reset_read_mds(pgio);
7c24d948
AA
858}
859
17280175 860static void
7c24d948
AA
861filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
862 struct nfs_page *req)
863{
ea2cf228 864 struct nfs_commit_info cinfo;
799ba8d5
FI
865 int status;
866
7c24d948
AA
867 BUG_ON(pgio->pg_lseg != NULL);
868
1825a0d0
FI
869 if (req->wb_offset != req->wb_pgbase)
870 goto out_mds;
7c24d948
AA
871 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
872 req->wb_context,
873 0,
874 NFS4_MAX_UINT64,
875 IOMODE_RW,
876 GFP_NOFS);
877 /* If no lseg, fall back to write through mds */
878 if (pgio->pg_lseg == NULL)
799ba8d5 879 goto out_mds;
ea2cf228
FI
880 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
881 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
799ba8d5
FI
882 if (status < 0) {
883 put_lseg(pgio->pg_lseg);
884 pgio->pg_lseg = NULL;
885 goto out_mds;
886 }
887 return;
888out_mds:
889 nfs_pageio_reset_write_mds(pgio);
7c24d948
AA
890}
891
1751c363 892static const struct nfs_pageio_ops filelayout_pg_read_ops = {
7c24d948 893 .pg_init = filelayout_pg_init_read,
1751c363 894 .pg_test = filelayout_pg_test,
493292dd 895 .pg_doio = pnfs_generic_pg_readpages,
1751c363
TM
896};
897
898static const struct nfs_pageio_ops filelayout_pg_write_ops = {
7c24d948 899 .pg_init = filelayout_pg_init_write,
1751c363 900 .pg_test = filelayout_pg_test,
dce81290 901 .pg_doio = pnfs_generic_pg_writepages,
1751c363
TM
902};
903
e0c2b380
FI
904static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
905{
906 if (fl->stripe_type == STRIPE_SPARSE)
907 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
908 else
909 return j;
910}
911
d6d6dc7c
FI
912/* The generic layer is about to remove the req from the commit list.
913 * If this will make the bucket empty, it will need to put the lseg reference.
d6d6dc7c 914 */
8dd37758 915static void
ea2cf228
FI
916filelayout_clear_request_commit(struct nfs_page *req,
917 struct nfs_commit_info *cinfo)
d6d6dc7c 918{
8dd37758 919 struct pnfs_layout_segment *freeme = NULL;
8dd37758 920
ea2cf228 921 spin_lock(cinfo->lock);
8dd37758
TM
922 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
923 goto out;
ea2cf228 924 cinfo->ds->nwritten--;
d6d6dc7c 925 if (list_is_singular(&req->wb_list)) {
ea2cf228 926 struct pnfs_commit_bucket *bucket;
d6d6dc7c 927
799ba8d5 928 bucket = list_first_entry(&req->wb_list,
ea2cf228 929 struct pnfs_commit_bucket,
799ba8d5
FI
930 written);
931 freeme = bucket->wlseg;
932 bucket->wlseg = NULL;
d6d6dc7c 933 }
8dd37758 934out:
ea2cf228
FI
935 nfs_request_remove_commit_list(req, cinfo);
936 spin_unlock(cinfo->lock);
8dd37758 937 put_lseg(freeme);
d6d6dc7c
FI
938}
939
940static struct list_head *
941filelayout_choose_commit_list(struct nfs_page *req,
ea2cf228
FI
942 struct pnfs_layout_segment *lseg,
943 struct nfs_commit_info *cinfo)
e0c2b380 944{
e0c2b380
FI
945 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
946 u32 i, j;
947 struct list_head *list;
ea2cf228 948 struct pnfs_commit_bucket *buckets;
e0c2b380 949
d6d6dc7c 950 if (fl->commit_through_mds)
ea2cf228 951 return &cinfo->mds->list;
d6d6dc7c 952
e0c2b380
FI
953 /* Note that we are calling nfs4_fl_calc_j_index on each page
954 * that ends up being committed to a data server. An attractive
955 * alternative is to add a field to nfs_write_data and nfs_page
956 * to store the value calculated in filelayout_write_pagelist
957 * and just use that here.
958 */
b5542849 959 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
e0c2b380 960 i = select_bucket_index(fl, j);
ea2cf228 961 buckets = cinfo->ds->buckets;
799ba8d5 962 list = &buckets[i].written;
e0c2b380 963 if (list_empty(list)) {
d6d6dc7c
FI
964 /* Non-empty buckets hold a reference on the lseg. That ref
965 * is normally transferred to the COMMIT call and released
966 * there. It could also be released if the last req is pulled
967 * off due to a rewrite, in which case it will be done in
799ba8d5 968 * filelayout_clear_request_commit
d6d6dc7c 969 */
799ba8d5 970 buckets[i].wlseg = get_lseg(lseg);
e0c2b380 971 }
8dd37758 972 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
ea2cf228 973 cinfo->ds->nwritten++;
e0c2b380
FI
974 return list;
975}
976
8dd37758
TM
977static void
978filelayout_mark_request_commit(struct nfs_page *req,
ea2cf228
FI
979 struct pnfs_layout_segment *lseg,
980 struct nfs_commit_info *cinfo)
8dd37758
TM
981{
982 struct list_head *list;
983
ea2cf228
FI
984 list = filelayout_choose_commit_list(req, lseg, cinfo);
985 nfs_request_add_commit_list(req, list, cinfo);
8dd37758
TM
986}
987
e0c2b380
FI
988static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
989{
990 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
991
992 if (flseg->stripe_type == STRIPE_SPARSE)
993 return i;
994 else
995 return nfs4_fl_calc_ds_index(lseg, i);
996}
997
998static struct nfs_fh *
999select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1000{
1001 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1002
1003 if (flseg->stripe_type == STRIPE_SPARSE) {
1004 if (flseg->num_fh == 1)
1005 i = 0;
1006 else if (flseg->num_fh == 0)
1007 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1008 return NULL;
1009 }
1010 return flseg->fh_array[i];
1011}
1012
0b7c0153 1013static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
e0c2b380
FI
1014{
1015 struct pnfs_layout_segment *lseg = data->lseg;
1016 struct nfs4_pnfs_ds *ds;
1017 u32 idx;
1018 struct nfs_fh *fh;
1019
1020 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1021 ds = nfs4_fl_prepare_ds(lseg, idx);
1022 if (!ds) {
e0c2b380 1023 prepare_to_resend_writes(data);
8dd37758 1024 filelayout_commit_release(data);
e0c2b380
FI
1025 return -EAGAIN;
1026 }
1027 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
0b7c0153 1028 data->commit_done_cb = filelayout_commit_done_cb;
e0c2b380
FI
1029 data->ds_clp = ds->ds_clp;
1030 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1031 if (fh)
1032 data->args.fh = fh;
0b7c0153 1033 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
9f0ec176
AA
1034 &filelayout_commit_call_ops, how,
1035 RPC_TASK_SOFTCONN);
e0c2b380
FI
1036}
1037
8dd37758 1038static int
1763da12
FI
1039transfer_commit_list(struct list_head *src, struct list_head *dst,
1040 struct nfs_commit_info *cinfo, int max)
8dd37758 1041{
8dd37758
TM
1042 struct nfs_page *req, *tmp;
1043 int ret = 0;
1044
1045 list_for_each_entry_safe(req, tmp, src, wb_list) {
1046 if (!nfs_lock_request(req))
1047 continue;
ea2cf228 1048 if (cond_resched_lock(cinfo->lock))
3b3be88d 1049 list_safe_reset_next(req, tmp, wb_list);
ea2cf228 1050 nfs_request_remove_commit_list(req, cinfo);
8dd37758
TM
1051 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1052 nfs_list_add_request(req, dst);
1053 ret++;
1763da12 1054 if ((ret == max) && !cinfo->dreq)
8dd37758
TM
1055 break;
1056 }
1763da12
FI
1057 return ret;
1058}
1059
1060static int
1061filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
1062 struct nfs_commit_info *cinfo,
1063 int max)
1064{
1065 struct list_head *src = &bucket->written;
1066 struct list_head *dst = &bucket->committing;
1067 int ret;
1068
1069 ret = transfer_commit_list(src, dst, cinfo, max);
799ba8d5 1070 if (ret) {
ea2cf228
FI
1071 cinfo->ds->nwritten -= ret;
1072 cinfo->ds->ncommitting += ret;
799ba8d5
FI
1073 bucket->clseg = bucket->wlseg;
1074 if (list_empty(src))
1075 bucket->wlseg = NULL;
1076 else
1077 get_lseg(bucket->clseg);
1078 }
8dd37758
TM
1079 return ret;
1080}
1081
d6d6dc7c 1082/* Move reqs from written to committing lists, returning count of number moved.
ea2cf228 1083 * Note called with cinfo->lock held.
d6d6dc7c 1084 */
ea2cf228
FI
1085static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1086 int max)
d6d6dc7c 1087{
d6d6dc7c
FI
1088 int i, rv = 0, cnt;
1089
ea2cf228
FI
1090 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1091 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1092 cinfo, max);
d6d6dc7c
FI
1093 max -= cnt;
1094 rv += cnt;
1095 }
d6d6dc7c
FI
1096 return rv;
1097}
1098
1763da12
FI
1099/* Pull everything off the committing lists and dump into @dst */
1100static void filelayout_recover_commit_reqs(struct list_head *dst,
1101 struct nfs_commit_info *cinfo)
1102{
1103 struct pnfs_commit_bucket *b;
1104 int i;
1105
1106 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1107 * only called on single thread per dreq.
1108 * Can't take the lock because need to do put_lseg
1109 */
1110 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1111 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1112 BUG_ON(!list_empty(&b->written));
1113 put_lseg(b->wlseg);
1114 b->wlseg = NULL;
1115 }
1116 }
1117 cinfo->ds->nwritten = 0;
1118}
1119
9390f425 1120static unsigned int
ea2cf228 1121alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
e0c2b380 1122{
ea2cf228
FI
1123 struct pnfs_ds_commit_info *fl_cinfo;
1124 struct pnfs_commit_bucket *bucket;
0b7c0153 1125 struct nfs_commit_data *data;
e0c2b380 1126 int i, j;
9390f425 1127 unsigned int nreq = 0;
e0c2b380 1128
ea2cf228 1129 fl_cinfo = cinfo->ds;
799ba8d5
FI
1130 bucket = fl_cinfo->buckets;
1131 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1132 if (list_empty(&bucket->committing))
e0c2b380
FI
1133 continue;
1134 data = nfs_commitdata_alloc();
1135 if (!data)
9390f425 1136 break;
e0c2b380 1137 data->ds_commit_index = i;
799ba8d5
FI
1138 data->lseg = bucket->clseg;
1139 bucket->clseg = NULL;
e0c2b380 1140 list_add(&data->pages, list);
9390f425 1141 nreq++;
e0c2b380 1142 }
e0c2b380 1143
9390f425 1144 /* Clean up on error */
799ba8d5
FI
1145 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1146 if (list_empty(&bucket->committing))
e0c2b380 1147 continue;
ea2cf228 1148 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
799ba8d5
FI
1149 put_lseg(bucket->clseg);
1150 bucket->clseg = NULL;
e0c2b380 1151 }
e0c2b380 1152 /* Caller will clean up entries put on list */
9390f425 1153 return nreq;
e0c2b380
FI
1154}
1155
1156/* This follows nfs_commit_list pretty closely */
1157static int
1158filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
ea2cf228 1159 int how, struct nfs_commit_info *cinfo)
e0c2b380 1160{
0b7c0153 1161 struct nfs_commit_data *data, *tmp;
e0c2b380 1162 LIST_HEAD(list);
9390f425 1163 unsigned int nreq = 0;
e0c2b380
FI
1164
1165 if (!list_empty(mds_pages)) {
1166 data = nfs_commitdata_alloc();
9390f425
TM
1167 if (data != NULL) {
1168 data->lseg = NULL;
1169 list_add(&data->pages, &list);
1170 nreq++;
1171 } else
ea2cf228 1172 nfs_retry_commit(mds_pages, NULL, cinfo);
e0c2b380
FI
1173 }
1174
ea2cf228 1175 nreq += alloc_ds_commits(cinfo, &list);
9390f425
TM
1176
1177 if (nreq == 0) {
f453a54a 1178 cinfo->completion_ops->error_cleanup(NFS_I(inode));
9390f425
TM
1179 goto out;
1180 }
1181
ea2cf228 1182 atomic_add(nreq, &cinfo->mds->rpcs_out);
e0c2b380
FI
1183
1184 list_for_each_entry_safe(data, tmp, &list, pages) {
1185 list_del_init(&data->pages);
e0c2b380 1186 if (!data->lseg) {
f453a54a 1187 nfs_init_commit(data, mds_pages, NULL, cinfo);
0b7c0153 1188 nfs_initiate_commit(NFS_CLIENT(inode), data,
9f0ec176 1189 data->mds_ops, how, 0);
e0c2b380 1190 } else {
ea2cf228 1191 struct pnfs_commit_bucket *buckets;
799ba8d5 1192
ea2cf228 1193 buckets = cinfo->ds->buckets;
f453a54a 1194 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
e0c2b380
FI
1195 filelayout_initiate_commit(data, how);
1196 }
1197 }
9390f425 1198out:
ea2cf228 1199 cinfo->ds->ncommitting = 0;
9390f425 1200 return PNFS_ATTEMPTED;
e0c2b380
FI
1201}
1202
1775bc34
BH
1203static void
1204filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1205{
1206 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1207}
1208
799ba8d5
FI
1209static struct pnfs_layout_hdr *
1210filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1211{
1212 struct nfs4_filelayout *flo;
1213
1214 flo = kzalloc(sizeof(*flo), gfp_flags);
1215 return &flo->generic_hdr;
1216}
1217
1218static void
1219filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1220{
1221 kfree(FILELAYOUT_FROM_HDR(lo));
1222}
1223
ea2cf228
FI
1224static struct pnfs_ds_commit_info *
1225filelayout_get_ds_info(struct inode *inode)
1226{
df011748
FI
1227 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1228
1229 if (layout == NULL)
1230 return NULL;
1231 else
1232 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
ea2cf228
FI
1233}
1234
7ab672ce 1235static struct pnfs_layoutdriver_type filelayout_type = {
ea8eecdd
CH
1236 .id = LAYOUT_NFSV4_1_FILES,
1237 .name = "LAYOUT_NFSV4_1_FILES",
1238 .owner = THIS_MODULE,
799ba8d5
FI
1239 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1240 .free_layout_hdr = filelayout_free_layout_hdr,
ea8eecdd
CH
1241 .alloc_lseg = filelayout_alloc_lseg,
1242 .free_lseg = filelayout_free_lseg,
1751c363
TM
1243 .pg_read_ops = &filelayout_pg_read_ops,
1244 .pg_write_ops = &filelayout_pg_write_ops,
ea2cf228 1245 .get_ds_info = &filelayout_get_ds_info,
8dd37758
TM
1246 .mark_request_commit = filelayout_mark_request_commit,
1247 .clear_request_commit = filelayout_clear_request_commit,
d6d6dc7c 1248 .scan_commit_lists = filelayout_scan_commit_lists,
1763da12 1249 .recover_commit_reqs = filelayout_recover_commit_reqs,
e0c2b380 1250 .commit_pagelist = filelayout_commit_pagelist,
dc70d7b3 1251 .read_pagelist = filelayout_read_pagelist,
0382b744 1252 .write_pagelist = filelayout_write_pagelist,
1775bc34 1253 .free_deviceid_node = filelayout_free_deveiceid_node,
7ab672ce
DH
1254};
1255
1256static int __init nfs4filelayout_init(void)
1257{
1258 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1259 __func__);
1260 return pnfs_register_layoutdriver(&filelayout_type);
1261}
1262
1263static void __exit nfs4filelayout_exit(void)
1264{
1265 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1266 __func__);
1267 pnfs_unregister_layoutdriver(&filelayout_type);
1268}
1269
f85ef69c
BF
1270MODULE_ALIAS("nfs-layouttype4-1");
1271
7ab672ce
DH
1272module_init(nfs4filelayout_init);
1273module_exit(nfs4filelayout_exit);