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