blob: 4d64d5b85e56b54d2d5ccc4f9be0138a94f1ad4b [file] [log] [blame]
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001/*
2 * Module for the pnfs nfs4 file layout driver.
3 * Defines all I/O and Policy interface operations, plus code
4 * to register itself with the pNFS client.
5 *
6 * Copyright (c) 2002
7 * The Regents of the University of Michigan
8 * All Rights Reserved
9 *
10 * Dean Hildebrand <dhildebz@umich.edu>
11 *
12 * Permission is granted to use, copy, create derivative works, and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the University of Michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. If
17 * the above copyright notice or any other identification of the
18 * University of Michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
20 *
21 * This software is provided as is, without representation or warranty
22 * of any kind either express or implied, including without limitation
23 * the implied warranties of merchantability, fitness for a particular
24 * purpose, or noninfringement. The Regents of the University of
25 * Michigan shall not be liable for any damages, including special,
26 * indirect, incidental, or consequential damages, with respect to any
27 * claim arising out of or in connection with the use of the software,
28 * even if it has been or is hereafter advised of the possibility of
29 * such damages.
30 */
31
32#include <linux/nfs_fs.h>
Benny Halevy19345cb2011-06-19 18:33:46 -040033#include <linux/nfs_page.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040034#include <linux/module.h>
Andy Adamson16b374c2010-10-20 00:18:04 -040035
Weston Andros Adamson0a702192012-02-17 13:15:24 -050036#include <linux/sunrpc/metrics.h>
37
Andy Adamson16b374c2010-10-20 00:18:04 -040038#include "internal.h"
Andy Adamson9cb81962012-03-07 10:49:41 -050039#include "delegation.h"
Andy Adamson16b374c2010-10-20 00:18:04 -040040#include "nfs4filelayout.h"
Dean Hildebrand7ab672c2010-10-20 00:18:00 -040041
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
Andy Adamsoncbdabc72011-03-01 01:34:20 +000048#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
49
Fred Isamancfe7f412011-03-01 01:34:18 +000050static 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;
Chris Metcalf3476f112011-08-11 13:54:28 -070055 u64 stripe_no;
56 u32 rem;
Fred Isamancfe7f412011-03-01 01:34:18 +000057
58 offset -= flseg->pattern_offset;
Chris Metcalf3476f112011-08-11 13:54:28 -070059 stripe_no = div_u64(offset, stripe_width);
60 div_u64_rem(offset, flseg->stripe_unit, &rem);
Fred Isamancfe7f412011-03-01 01:34:18 +000061
Chris Metcalf3476f112011-08-11 13:54:28 -070062 return stripe_no * flseg->stripe_unit + rem;
Fred Isamancfe7f412011-03-01 01:34:18 +000063}
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
Andy Adamsoncbdabc72011-03-01 01:34:20 +000085static int filelayout_async_handle_error(struct rpc_task *task,
86 struct nfs4_state *state,
87 struct nfs_client *clp,
88 int *reset)
89{
Andy Adamson9cb81962012-03-07 10:49:41 -050090 struct nfs_server *mds_server = NFS_SERVER(state->inode);
91 struct nfs_client *mds_client = mds_server->nfs_client;
92
Andy Adamsoncbdabc72011-03-01 01:34:20 +000093 if (task->tk_status >= 0)
94 return 0;
Andy Adamsoncbdabc72011-03-01 01:34:20 +000095 *reset = 0;
96
97 switch (task->tk_status) {
Andy Adamson9cb81962012-03-07 10:49:41 -050098 /* MDS state errors */
99 case -NFS4ERR_DELEG_REVOKED:
100 case -NFS4ERR_ADMIN_REVOKED:
101 case -NFS4ERR_BAD_STATEID:
Andy Adamson2dc31752012-03-08 11:03:53 -0500102 nfs_remove_bad_delegation(state->inode);
Andy Adamson9cb81962012-03-07 10:49:41 -0500103 case -NFS4ERR_OPENMODE:
Andy Adamson9cb81962012-03-07 10:49:41 -0500104 nfs4_schedule_stateid_recovery(mds_server, state);
105 goto wait_on_recovery;
106 case -NFS4ERR_EXPIRED:
Andy Adamson2dc31752012-03-08 11:03:53 -0500107 nfs4_schedule_stateid_recovery(mds_server, state);
Andy Adamson9cb81962012-03-07 10:49:41 -0500108 nfs4_schedule_lease_recovery(mds_client);
109 goto wait_on_recovery;
110 /* DS session errors */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000111 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:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000125 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
126 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400127 case -NFS4ERR_RETRY_UNCACHED_REP:
128 break;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000129 default:
130 dprintk("%s DS error. Retry through MDS %d\n", __func__,
131 task->tk_status);
132 *reset = 1;
133 break;
134 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500135out:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000136 task->tk_status = 0;
137 return -EAGAIN;
Andy Adamson9cb81962012-03-07 10:49:41 -0500138wait_on_recovery:
139 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
140 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
141 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
142 goto out;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000143}
144
145/* NFS_PROTO call done callback routines */
146
147static int filelayout_read_done_cb(struct rpc_task *task,
148 struct nfs_read_data *data)
149{
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000150 int reset = 0;
151
152 dprintk("%s DS read\n", __func__);
153
154 if (filelayout_async_handle_error(task, data->args.context->state,
155 data->ds_clp, &reset) == -EAGAIN) {
156 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
157 __func__, data->ds_clp, data->ds_clp->cl_session);
158 if (reset) {
Peng Tao1b0ae062011-09-22 21:50:12 -0400159 pnfs_set_lo_fail(data->lseg);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000160 nfs4_reset_read(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000161 }
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700162 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000163 return -EAGAIN;
164 }
165
166 return 0;
167}
168
Andy Adamson16b374c2010-10-20 00:18:04 -0400169/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000170 * We reference the rpc_cred of the first WRITE that triggers the need for
171 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
172 * rfc5661 is not clear about which credential should be used.
173 */
174static void
175filelayout_set_layoutcommit(struct nfs_write_data *wdata)
176{
177 if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
178 wdata->res.verf->committed == NFS_FILE_SYNC)
179 return;
180
181 pnfs_set_layoutcommit(wdata);
182 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
Peng Taoacff58802011-07-30 20:52:31 -0400183 (unsigned long) NFS_I(wdata->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000184}
185
186/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000187 * Call ops for the async read/write cases
188 * In the case of dense layouts, the offset needs to be reset to its
189 * original value.
190 */
191static void filelayout_read_prepare(struct rpc_task *task, void *data)
192{
193 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
194
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000195 rdata->read_done_cb = filelayout_read_done_cb;
196
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000197 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
198 &rdata->args.seq_args, &rdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500199 task))
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000200 return;
201
202 rpc_call_start(task);
203}
204
205static void filelayout_read_call_done(struct rpc_task *task, void *data)
206{
207 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
208
209 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
210
211 /* Note this may cause RPC to be resent */
212 rdata->mds_ops->rpc_call_done(task, data);
213}
214
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500215static void filelayout_read_count_stats(struct rpc_task *task, void *data)
216{
217 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
218
219 rpc_count_iostats(task, NFS_SERVER(rdata->inode)->client->cl_metrics);
220}
221
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000222static void filelayout_read_release(void *data)
223{
224 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
225
Trond Myklebust8dd37752012-03-15 17:16:40 -0400226 put_lseg(rdata->lseg);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000227 rdata->mds_ops->rpc_release(data);
228}
229
Fred Isamana69aef12011-03-03 15:13:47 +0000230static 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) {
Fred Isamana69aef12011-03-03 15:13:47 +0000237 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) {
Peng Tao1b0ae062011-09-22 21:50:12 -0400240 pnfs_set_lo_fail(data->lseg);
Fred Isamana69aef12011-03-03 15:13:47 +0000241 nfs4_reset_write(task, data);
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700242 }
243 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000244 return -EAGAIN;
245 }
246
Andy Adamson863a3c62011-03-23 13:27:54 +0000247 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000248 return 0;
249}
250
Fred Isamane0c2b382011-03-23 13:27:53 +0000251/* 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);
Peng Tao1b0ae062011-09-22 21:50:12 -0400273 pnfs_set_lo_fail(data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000274 } else
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700275 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000276 return -EAGAIN;
277 }
278
279 return 0;
280}
281
Fred Isamana69aef12011-03-03 15:13:47 +0000282static 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,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500288 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000289 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
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500302static 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
Fred Isamana69aef12011-03-03 15:13:47 +0000309static void filelayout_write_release(void *data)
310{
311 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
312
Trond Myklebust8dd37752012-03-15 17:16:40 -0400313 put_lseg(wdata->lseg);
Fred Isamana69aef12011-03-03 15:13:47 +0000314 wdata->mds_ops->rpc_release(data);
315}
316
Fred Isamane0c2b382011-03-23 13:27:53 +0000317static void filelayout_commit_release(void *data)
318{
319 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
320
321 nfs_commit_release_pages(wdata);
322 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
323 nfs_commit_clear_lock(NFS_I(wdata->inode));
Trond Myklebust8dd37752012-03-15 17:16:40 -0400324 put_lseg(wdata->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000325 nfs_commitdata_release(wdata);
326}
327
Trond Myklebust17280172012-03-11 13:11:00 -0400328static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000329 .rpc_call_prepare = filelayout_read_prepare,
330 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500331 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000332 .rpc_release = filelayout_read_release,
333};
334
Trond Myklebust17280172012-03-11 13:11:00 -0400335static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000336 .rpc_call_prepare = filelayout_write_prepare,
337 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500338 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000339 .rpc_release = filelayout_write_release,
340};
341
Trond Myklebust17280172012-03-11 13:11:00 -0400342static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isamane0c2b382011-03-23 13:27:53 +0000343 .rpc_call_prepare = filelayout_write_prepare,
344 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500345 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000346 .rpc_release = filelayout_commit_release,
347};
348
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000349static enum pnfs_try_status
350filelayout_read_pagelist(struct nfs_read_data *data)
351{
352 struct pnfs_layout_segment *lseg = data->lseg;
353 struct nfs4_pnfs_ds *ds;
354 loff_t offset = data->args.offset;
355 u32 j, idx;
356 struct nfs_fh *fh;
357 int status;
358
359 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
360 __func__, data->inode->i_ino,
361 data->args.pgbase, (size_t)data->args.count, offset);
362
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400363 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
364 return PNFS_NOT_ATTEMPTED;
365
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000366 /* Retrieve the correct rpc_client for the byte range */
367 j = nfs4_fl_calc_j_index(lseg, offset);
368 idx = nfs4_fl_calc_ds_index(lseg, j);
369 ds = nfs4_fl_prepare_ds(lseg, idx);
370 if (!ds) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000371 /* Either layout fh index faulty, or ds connect failed */
372 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
373 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000374 return PNFS_NOT_ATTEMPTED;
375 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400376 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000377
378 /* No multipath support. Use first DS */
379 data->ds_clp = ds->ds_clp;
380 fh = nfs4_fl_select_ds_fh(lseg, j);
381 if (fh)
382 data->args.fh = fh;
383
384 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
385 data->mds_offset = offset;
386
387 /* Perform an asynchronous read to ds */
388 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
389 &filelayout_read_call_ops);
390 BUG_ON(status != 0);
391 return PNFS_ATTEMPTED;
392}
393
Fred Isamana69aef12011-03-03 15:13:47 +0000394/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000395static enum pnfs_try_status
396filelayout_write_pagelist(struct nfs_write_data *data, int sync)
397{
Fred Isamana69aef12011-03-03 15:13:47 +0000398 struct pnfs_layout_segment *lseg = data->lseg;
399 struct nfs4_pnfs_ds *ds;
400 loff_t offset = data->args.offset;
401 u32 j, idx;
402 struct nfs_fh *fh;
403 int status;
404
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400405 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
406 return PNFS_NOT_ATTEMPTED;
407
Fred Isamana69aef12011-03-03 15:13:47 +0000408 /* Retrieve the correct rpc_client for the byte range */
409 j = nfs4_fl_calc_j_index(lseg, offset);
410 idx = nfs4_fl_calc_ds_index(lseg, j);
411 ds = nfs4_fl_prepare_ds(lseg, idx);
412 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500413 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
414 __func__);
Fred Isamana69aef12011-03-03 15:13:47 +0000415 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
416 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
417 return PNFS_NOT_ATTEMPTED;
418 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400419 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
Fred Isamana69aef12011-03-03 15:13:47 +0000420 data->inode->i_ino, sync, (size_t) data->args.count, offset,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400421 ds->ds_remotestr);
Fred Isamana69aef12011-03-03 15:13:47 +0000422
Fred Isamana69aef12011-03-03 15:13:47 +0000423 data->write_done_cb = filelayout_write_done_cb;
424 data->ds_clp = ds->ds_clp;
425 fh = nfs4_fl_select_ds_fh(lseg, j);
426 if (fh)
427 data->args.fh = fh;
428 /*
429 * Get the file offset on the dserver. Set the write offset to
430 * this offset and save the original offset.
431 */
432 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000433
434 /* Perform an asynchronous write */
435 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
436 &filelayout_write_call_ops, sync);
437 BUG_ON(status != 0);
438 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000439}
440
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000441/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400442 * filelayout_check_layout()
443 *
444 * Make sure layout segment parameters are sane WRT the device.
445 * At this point no generic layer initialization of the lseg has occurred,
446 * and nothing has been added to the layout_hdr cache.
447 *
448 */
449static int
450filelayout_check_layout(struct pnfs_layout_hdr *lo,
451 struct nfs4_filelayout_segment *fl,
452 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400453 struct nfs4_deviceid *id,
454 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400455{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400456 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400457 struct nfs4_file_layout_dsaddr *dsaddr;
458 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000459 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400460
461 dprintk("--> %s\n", __func__);
462
Andy Adamson7c24d942011-06-13 18:22:38 -0400463 /* FIXME: remove this check when layout segment support is added */
464 if (lgr->range.offset != 0 ||
465 lgr->range.length != NFS4_MAX_UINT64) {
466 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
467 __func__);
468 goto out;
469 }
470
Andy Adamson16b374c2010-10-20 00:18:04 -0400471 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500472 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400473 __func__, fl->pattern_offset);
474 goto out;
475 }
476
Benny Halevy75247af2011-02-22 15:56:01 -0800477 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
478 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400479 __func__, fl->stripe_unit);
480 goto out;
481 }
482
483 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300484 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
485 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400486 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400487 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400488 if (dsaddr == NULL)
489 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400490 } else
491 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400492 /* Found deviceid is being reaped */
493 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
494 goto out_put;
495
Andy Adamson16b374c2010-10-20 00:18:04 -0400496 fl->dsaddr = dsaddr;
497
Chuck Levere4149662011-10-25 12:18:03 -0400498 if (fl->first_stripe_index >= dsaddr->stripe_count) {
499 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400500 __func__, fl->first_stripe_index);
501 goto out_put;
502 }
503
504 if ((fl->stripe_type == STRIPE_SPARSE &&
505 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
506 (fl->stripe_type == STRIPE_DENSE &&
507 fl->num_fh != dsaddr->stripe_count)) {
508 dprintk("%s num_fh %u not valid for given packing\n",
509 __func__, fl->num_fh);
510 goto out_put;
511 }
512
513 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
514 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
515 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
516 nfss->wsize);
517 }
518
519 status = 0;
520out:
521 dprintk("--> %s returns %d\n", __func__, status);
522 return status;
523out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000524 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400525 goto out;
526}
527
528static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
529{
530 int i;
531
532 for (i = 0; i < fl->num_fh; i++) {
533 if (!fl->fh_array[i])
534 break;
535 kfree(fl->fh_array[i]);
536 }
537 kfree(fl->fh_array);
538 fl->fh_array = NULL;
539}
540
541static void
542_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
543{
544 filelayout_free_fh_array(fl);
545 kfree(fl);
546}
547
548static int
549filelayout_decode_layout(struct pnfs_layout_hdr *flo,
550 struct nfs4_filelayout_segment *fl,
551 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400552 struct nfs4_deviceid *id,
553 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400554{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400555 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400556 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400557 struct page *scratch;
558 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400559 uint32_t nfl_util;
560 int i;
561
562 dprintk("%s: set_layout_map Begin\n", __func__);
563
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400564 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400565 if (!scratch)
566 return -ENOMEM;
567
Benny Halevyf7da7a12011-05-19 14:16:47 -0400568 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400569 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
570
571 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
572 * num_fh (4) */
573 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
574 if (unlikely(!p))
575 goto out_err;
576
Andy Adamson16b374c2010-10-20 00:18:04 -0400577 memcpy(id, p, sizeof(*id));
578 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400579 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400580
581 nfl_util = be32_to_cpup(p++);
582 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
583 fl->commit_through_mds = 1;
584 if (nfl_util & NFL4_UFLG_DENSE)
585 fl->stripe_type = STRIPE_DENSE;
586 else
587 fl->stripe_type = STRIPE_SPARSE;
588 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
589
590 fl->first_stripe_index = be32_to_cpup(p++);
591 p = xdr_decode_hyper(p, &fl->pattern_offset);
592 fl->num_fh = be32_to_cpup(p++);
593
594 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
595 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
596 fl->pattern_offset);
597
Andy Adamsoncec765c2011-06-13 18:36:17 -0400598 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
599 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400600 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400601 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400602 goto out_err;
603
Andy Adamsoncec765c2011-06-13 18:36:17 -0400604 if (fl->num_fh > 0) {
605 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
606 gfp_flags);
607 if (!fl->fh_array)
608 goto out_err;
609 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400610
611 for (i = 0; i < fl->num_fh; i++) {
612 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400613 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400614 if (!fl->fh_array[i])
615 goto out_err_free;
616
617 p = xdr_inline_decode(&stream, 4);
618 if (unlikely(!p))
619 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400620 fl->fh_array[i]->size = be32_to_cpup(p++);
621 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500622 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400623 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400624 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400625 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400626
627 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
628 if (unlikely(!p))
629 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400630 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400631 dprintk("DEBUG: %s: fh len %d\n", __func__,
632 fl->fh_array[i]->size);
633 }
634
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400635 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400636 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400637
638out_err_free:
639 filelayout_free_fh_array(fl);
640out_err:
641 __free_page(scratch);
642 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400643}
644
Fred Isamanc8795132011-03-23 13:27:49 +0000645static void
646filelayout_free_lseg(struct pnfs_layout_segment *lseg)
647{
648 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
649
650 dprintk("--> %s\n", __func__);
651 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman425eb732011-03-23 13:27:50 +0000652 kfree(fl->commit_buckets);
Fred Isamanc8795132011-03-23 13:27:49 +0000653 _filelayout_free_lseg(fl);
654}
655
Andy Adamson16b374c2010-10-20 00:18:04 -0400656static struct pnfs_layout_segment *
657filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400658 struct nfs4_layoutget_res *lgr,
659 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400660{
661 struct nfs4_filelayout_segment *fl;
662 int rc;
663 struct nfs4_deviceid id;
664
665 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400666 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400667 if (!fl)
668 return NULL;
669
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400670 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
671 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400672 _filelayout_free_lseg(fl);
673 return NULL;
674 }
Fred Isaman425eb732011-03-23 13:27:50 +0000675
676 /* This assumes there is only one IOMODE_RW lseg. What
677 * we really want to do is have a layout_hdr level
678 * dictionary of <multipath_list4, fh> keys, each
679 * associated with a struct list_head, populated by calls
680 * to filelayout_write_pagelist().
681 * */
682 if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
683 int i;
684 int size = (fl->stripe_type == STRIPE_SPARSE) ?
685 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
686
Fred Isamand6d6dc72012-03-08 17:29:35 -0500687 fl->commit_buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket), gfp_flags);
Fred Isaman425eb732011-03-23 13:27:50 +0000688 if (!fl->commit_buckets) {
689 filelayout_free_lseg(&fl->generic_hdr);
690 return NULL;
691 }
692 fl->number_of_buckets = size;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500693 for (i = 0; i < size; i++) {
694 INIT_LIST_HEAD(&fl->commit_buckets[i].written);
695 INIT_LIST_HEAD(&fl->commit_buckets[i].committing);
696 }
Fred Isaman425eb732011-03-23 13:27:50 +0000697 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400698 return &fl->generic_hdr;
699}
700
Fred Isaman94ad1c82011-03-01 01:34:14 +0000701/*
702 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
703 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300704 * return true : coalesce page
705 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000706 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400707static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000708filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
709 struct nfs_page *req)
710{
711 u64 p_stripe, r_stripe;
712 u32 stripe_unit;
713
Benny Halevy19345cb2011-06-19 18:33:46 -0400714 if (!pnfs_generic_pg_test(pgio, prev, req) ||
715 !nfs_generic_pg_test(pgio, prev, req))
716 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300717
Fred Isaman94ad1c82011-03-01 01:34:14 +0000718 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
719 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
720 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
721
722 do_div(p_stripe, stripe_unit);
723 do_div(r_stripe, stripe_unit);
724
725 return (p_stripe == r_stripe);
726}
727
Trond Myklebust17280172012-03-11 13:11:00 -0400728static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400729filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
730 struct nfs_page *req)
731{
732 BUG_ON(pgio->pg_lseg != NULL);
733
734 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
735 req->wb_context,
736 0,
737 NFS4_MAX_UINT64,
738 IOMODE_READ,
739 GFP_KERNEL);
740 /* If no lseg, fall back to read through mds */
741 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400742 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400743}
744
Trond Myklebust17280172012-03-11 13:11:00 -0400745static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400746filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
747 struct nfs_page *req)
748{
749 BUG_ON(pgio->pg_lseg != NULL);
750
751 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
752 req->wb_context,
753 0,
754 NFS4_MAX_UINT64,
755 IOMODE_RW,
756 GFP_NOFS);
757 /* If no lseg, fall back to write through mds */
758 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400759 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400760}
761
Trond Myklebust1751c362011-06-10 13:30:23 -0400762static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400763 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400764 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400765 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400766};
767
768static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400769 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400770 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400771 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400772};
773
Fred Isamane0c2b382011-03-23 13:27:53 +0000774static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
775{
776 if (fl->stripe_type == STRIPE_SPARSE)
777 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
778 else
779 return j;
780}
781
Fred Isamand6d6dc72012-03-08 17:29:35 -0500782/* The generic layer is about to remove the req from the commit list.
783 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500784 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400785static void
786filelayout_clear_request_commit(struct nfs_page *req)
Fred Isamane0c2b382011-03-23 13:27:53 +0000787{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400788 struct pnfs_layout_segment *freeme = NULL;
789 struct inode *inode = req->wb_context->dentry->d_inode;
790
791 spin_lock(&inode->i_lock);
792 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
793 goto out;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500794 if (list_is_singular(&req->wb_list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500795 struct pnfs_layout_segment *lseg;
796
797 /* From here we can find the bucket, but for the moment,
798 * since there is only one relevant lseg...
799 */
800 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
Trond Myklebust8dd37752012-03-15 17:16:40 -0400801 if (lseg->pls_range.iomode == IOMODE_RW) {
802 freeme = lseg;
803 break;
804 }
Fred Isamand6d6dc72012-03-08 17:29:35 -0500805 }
806 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400807out:
808 nfs_request_remove_commit_list(req);
809 spin_unlock(&inode->i_lock);
810 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500811}
812
813static struct list_head *
814filelayout_choose_commit_list(struct nfs_page *req,
815 struct pnfs_layout_segment *lseg)
816{
Fred Isamane0c2b382011-03-23 13:27:53 +0000817 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
818 u32 i, j;
819 struct list_head *list;
820
Fred Isamand6d6dc72012-03-08 17:29:35 -0500821 if (fl->commit_through_mds)
822 return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
823
Fred Isamane0c2b382011-03-23 13:27:53 +0000824 /* Note that we are calling nfs4_fl_calc_j_index on each page
825 * that ends up being committed to a data server. An attractive
826 * alternative is to add a field to nfs_write_data and nfs_page
827 * to store the value calculated in filelayout_write_pagelist
828 * and just use that here.
829 */
830 j = nfs4_fl_calc_j_index(lseg,
831 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
832 i = select_bucket_index(fl, j);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500833 list = &fl->commit_buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +0000834 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500835 /* Non-empty buckets hold a reference on the lseg. That ref
836 * is normally transferred to the COMMIT call and released
837 * there. It could also be released if the last req is pulled
838 * off due to a rewrite, in which case it will be done in
839 * filelayout_remove_commit_req
840 */
Fred Isamane0c2b382011-03-23 13:27:53 +0000841 get_lseg(lseg);
842 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400843 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamane0c2b382011-03-23 13:27:53 +0000844 return list;
845}
846
Trond Myklebust8dd37752012-03-15 17:16:40 -0400847static void
848filelayout_mark_request_commit(struct nfs_page *req,
849 struct pnfs_layout_segment *lseg)
850{
851 struct list_head *list;
852
853 list = filelayout_choose_commit_list(req, lseg);
854 nfs_request_add_commit_list(req, list);
855}
856
Fred Isamane0c2b382011-03-23 13:27:53 +0000857static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
858{
859 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
860
861 if (flseg->stripe_type == STRIPE_SPARSE)
862 return i;
863 else
864 return nfs4_fl_calc_ds_index(lseg, i);
865}
866
867static struct nfs_fh *
868select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
869{
870 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
871
872 if (flseg->stripe_type == STRIPE_SPARSE) {
873 if (flseg->num_fh == 1)
874 i = 0;
875 else if (flseg->num_fh == 0)
876 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
877 return NULL;
878 }
879 return flseg->fh_array[i];
880}
881
882static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
883{
884 struct pnfs_layout_segment *lseg = data->lseg;
885 struct nfs4_pnfs_ds *ds;
886 u32 idx;
887 struct nfs_fh *fh;
888
889 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
890 ds = nfs4_fl_prepare_ds(lseg, idx);
891 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500892 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
893 __func__);
Fred Isamane0c2b382011-03-23 13:27:53 +0000894 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
895 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
896 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400897 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000898 return -EAGAIN;
899 }
900 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
901 data->write_done_cb = filelayout_commit_done_cb;
902 data->ds_clp = ds->ds_clp;
903 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
904 if (fh)
905 data->args.fh = fh;
906 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
907 &filelayout_commit_call_ops, how);
908}
909
910/*
911 * This is only useful while we are using whole file layouts.
912 */
Fred Isamand6d6dc72012-03-08 17:29:35 -0500913static struct pnfs_layout_segment *
914find_only_write_lseg_locked(struct inode *inode)
Fred Isamane0c2b382011-03-23 13:27:53 +0000915{
Fred Isamand6d6dc72012-03-08 17:29:35 -0500916 struct pnfs_layout_segment *lseg;
Fred Isamane0c2b382011-03-23 13:27:53 +0000917
Fred Isamane0c2b382011-03-23 13:27:53 +0000918 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
919 if (lseg->pls_range.iomode == IOMODE_RW)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400920 return lseg;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500921 return NULL;
922}
923
924static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
925{
926 struct pnfs_layout_segment *rv;
927
928 spin_lock(&inode->i_lock);
929 rv = find_only_write_lseg_locked(inode);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400930 if (rv)
931 get_lseg(rv);
Fred Isamane0c2b382011-03-23 13:27:53 +0000932 spin_unlock(&inode->i_lock);
933 return rv;
934}
935
Trond Myklebust8dd37752012-03-15 17:16:40 -0400936static int
Trond Myklebust3b3be882012-03-17 11:59:30 -0400937filelayout_scan_ds_commit_list(struct nfs4_fl_commit_bucket *bucket, int max,
938 spinlock_t *lock)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400939{
940 struct list_head *src = &bucket->written;
941 struct list_head *dst = &bucket->committing;
942 struct nfs_page *req, *tmp;
943 int ret = 0;
944
945 list_for_each_entry_safe(req, tmp, src, wb_list) {
946 if (!nfs_lock_request(req))
947 continue;
Trond Myklebust3b3be882012-03-17 11:59:30 -0400948 if (cond_resched_lock(lock))
949 list_safe_reset_next(req, tmp, wb_list);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400950 nfs_request_remove_commit_list(req);
951 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
952 nfs_list_add_request(req, dst);
953 ret++;
954 if (ret == max)
955 break;
956 }
957 return ret;
958}
959
Fred Isamand6d6dc72012-03-08 17:29:35 -0500960/* Move reqs from written to committing lists, returning count of number moved.
961 * Note called with i_lock held.
962 */
Trond Myklebust3b3be882012-03-17 11:59:30 -0400963static int filelayout_scan_commit_lists(struct inode *inode, int max,
964 spinlock_t *lock)
Fred Isamand6d6dc72012-03-08 17:29:35 -0500965{
966 struct pnfs_layout_segment *lseg;
967 struct nfs4_filelayout_segment *fl;
968 int i, rv = 0, cnt;
969
970 lseg = find_only_write_lseg_locked(inode);
971 if (!lseg)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400972 goto out_done;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500973 fl = FILELAYOUT_LSEG(lseg);
974 if (fl->commit_through_mds)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400975 goto out_done;
976 for (i = 0; i < fl->number_of_buckets && max != 0; i++) {
Trond Myklebust3b3be882012-03-17 11:59:30 -0400977 cnt = filelayout_scan_ds_commit_list(&fl->commit_buckets[i],
978 max, lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500979 max -= cnt;
980 rv += cnt;
981 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400982out_done:
Fred Isamand6d6dc72012-03-08 17:29:35 -0500983 return rv;
984}
985
Trond Myklebust9390f422012-03-16 13:52:45 -0400986static unsigned int
987alloc_ds_commits(struct inode *inode, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +0000988{
989 struct pnfs_layout_segment *lseg;
990 struct nfs4_filelayout_segment *fl;
991 struct nfs_write_data *data;
992 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -0400993 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +0000994
995 /* Won't need this when non-whole file layout segments are supported
996 * instead we will use a pnfs_layout_hdr structure */
997 lseg = find_only_write_lseg(inode);
998 if (!lseg)
999 return 0;
1000 fl = FILELAYOUT_LSEG(lseg);
1001 for (i = 0; i < fl->number_of_buckets; i++) {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001002 if (list_empty(&fl->commit_buckets[i].committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001003 continue;
1004 data = nfs_commitdata_alloc();
1005 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001006 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001007 data->ds_commit_index = i;
1008 data->lseg = lseg;
1009 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001010 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001011 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001012
Trond Myklebust9390f422012-03-16 13:52:45 -04001013 /* Clean up on error */
Fred Isamane0c2b382011-03-23 13:27:53 +00001014 for (j = i; j < fl->number_of_buckets; j++) {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001015 if (list_empty(&fl->commit_buckets[i].committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001016 continue;
Fred Isamand6d6dc72012-03-08 17:29:35 -05001017 nfs_retry_commit(&fl->commit_buckets[i].committing, lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001018 put_lseg(lseg); /* associated with emptying bucket */
1019 }
1020 put_lseg(lseg);
1021 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001022 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001023}
1024
1025/* This follows nfs_commit_list pretty closely */
1026static int
1027filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1028 int how)
1029{
1030 struct nfs_write_data *data, *tmp;
1031 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001032 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001033
1034 if (!list_empty(mds_pages)) {
1035 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001036 if (data != NULL) {
1037 data->lseg = NULL;
1038 list_add(&data->pages, &list);
1039 nreq++;
1040 } else
1041 nfs_retry_commit(mds_pages, NULL);
Fred Isamane0c2b382011-03-23 13:27:53 +00001042 }
1043
Trond Myklebust9390f422012-03-16 13:52:45 -04001044 nreq += alloc_ds_commits(inode, &list);
1045
1046 if (nreq == 0) {
1047 nfs_commit_clear_lock(NFS_I(inode));
1048 goto out;
1049 }
1050
1051 atomic_add(nreq, &NFS_I(inode)->commits_outstanding);
Fred Isamane0c2b382011-03-23 13:27:53 +00001052
1053 list_for_each_entry_safe(data, tmp, &list, pages) {
1054 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001055 if (!data->lseg) {
1056 nfs_init_commit(data, mds_pages, NULL);
1057 nfs_initiate_commit(data, NFS_CLIENT(inode),
1058 data->mds_ops, how);
1059 } else {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001060 nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index].committing, data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001061 filelayout_initiate_commit(data, how);
1062 }
1063 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001064out:
1065 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001066}
1067
Benny Halevy1775bc32011-05-20 13:47:33 +02001068static void
1069filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1070{
1071 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1072}
1073
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001074static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001075 .id = LAYOUT_NFSV4_1_FILES,
1076 .name = "LAYOUT_NFSV4_1_FILES",
1077 .owner = THIS_MODULE,
1078 .alloc_lseg = filelayout_alloc_lseg,
1079 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001080 .pg_read_ops = &filelayout_pg_read_ops,
1081 .pg_write_ops = &filelayout_pg_write_ops,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001082 .mark_request_commit = filelayout_mark_request_commit,
1083 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001084 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isamane0c2b382011-03-23 13:27:53 +00001085 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001086 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001087 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001088 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001089};
1090
1091static int __init nfs4filelayout_init(void)
1092{
1093 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1094 __func__);
1095 return pnfs_register_layoutdriver(&filelayout_type);
1096}
1097
1098static void __exit nfs4filelayout_exit(void)
1099{
1100 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1101 __func__);
1102 pnfs_unregister_layoutdriver(&filelayout_type);
1103}
1104
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001105MODULE_ALIAS("nfs-layouttype4-1");
1106
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001107module_init(nfs4filelayout_init);
1108module_exit(nfs4filelayout_exit);