blob: b9edc8870561d5b66e3ca7aef99eee2e6c4f385d [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:
125 case -EKEYEXPIRED:
126 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
127 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400128 case -NFS4ERR_RETRY_UNCACHED_REP:
129 break;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000130 default:
131 dprintk("%s DS error. Retry through MDS %d\n", __func__,
132 task->tk_status);
133 *reset = 1;
134 break;
135 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500136out:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000137 task->tk_status = 0;
138 return -EAGAIN;
Andy Adamson9cb81962012-03-07 10:49:41 -0500139wait_on_recovery:
140 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
141 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
142 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
143 goto out;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000144}
145
146/* NFS_PROTO call done callback routines */
147
148static int filelayout_read_done_cb(struct rpc_task *task,
149 struct nfs_read_data *data)
150{
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000151 int reset = 0;
152
153 dprintk("%s DS read\n", __func__);
154
155 if (filelayout_async_handle_error(task, data->args.context->state,
156 data->ds_clp, &reset) == -EAGAIN) {
157 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
158 __func__, data->ds_clp, data->ds_clp->cl_session);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400159 if (reset)
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000160 nfs4_reset_read(task, data);
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700161 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000162 return -EAGAIN;
163 }
164
165 return 0;
166}
167
Andy Adamson16b374c2010-10-20 00:18:04 -0400168/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000169 * We reference the rpc_cred of the first WRITE that triggers the need for
170 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
171 * rfc5661 is not clear about which credential should be used.
172 */
173static void
174filelayout_set_layoutcommit(struct nfs_write_data *wdata)
175{
Fred Isamancd841602012-04-20 14:47:44 -0400176 struct nfs_pgio_header *hdr = wdata->header;
177
178 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
Andy Adamson863a3c62011-03-23 13:27:54 +0000179 wdata->res.verf->committed == NFS_FILE_SYNC)
180 return;
181
182 pnfs_set_layoutcommit(wdata);
Fred Isamancd841602012-04-20 14:47:44 -0400183 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
184 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000185}
186
187/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000188 * Call ops for the async read/write cases
189 * In the case of dense layouts, the offset needs to be reset to its
190 * original value.
191 */
192static void filelayout_read_prepare(struct rpc_task *task, void *data)
193{
Fred Isamancd12ae32012-04-20 14:47:42 -0400194 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000195
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000196 rdata->read_done_cb = filelayout_read_done_cb;
197
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000198 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
199 &rdata->args.seq_args, &rdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500200 task))
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000201 return;
202
203 rpc_call_start(task);
204}
205
206static void filelayout_read_call_done(struct rpc_task *task, void *data)
207{
Fred Isamancd12ae32012-04-20 14:47:42 -0400208 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000209
210 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
211
212 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400213 rdata->header->mds_ops->rpc_call_done(task, data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000214}
215
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500216static void filelayout_read_count_stats(struct rpc_task *task, void *data)
217{
Fred Isamancd12ae32012-04-20 14:47:42 -0400218 struct nfs_read_data *rdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500219
Fred Isamancd841602012-04-20 14:47:44 -0400220 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500221}
222
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000223static void filelayout_read_release(void *data)
224{
Fred Isamancd12ae32012-04-20 14:47:42 -0400225 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000226
Fred Isamancd841602012-04-20 14:47:44 -0400227 rdata->header->mds_ops->rpc_release(data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000228}
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);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400239 if (reset)
Fred Isamana69aef12011-03-03 15:13:47 +0000240 nfs4_reset_write(task, data);
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700241 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000242 return -EAGAIN;
243 }
244
Andy Adamson863a3c62011-03-23 13:27:54 +0000245 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000246 return 0;
247}
248
Fred Isamane0c2b382011-03-23 13:27:53 +0000249/* Fake up some data that will cause nfs_commit_release to retry the writes. */
Fred Isaman0b7c0152012-04-20 14:47:39 -0400250static void prepare_to_resend_writes(struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000251{
252 struct nfs_page *first = nfs_list_entry(data->pages.next);
253
254 data->task.tk_status = 0;
255 memcpy(data->verf.verifier, first->wb_verf.verifier,
256 sizeof(first->wb_verf.verifier));
257 data->verf.verifier[0]++; /* ensure verifier mismatch */
258}
259
260static int filelayout_commit_done_cb(struct rpc_task *task,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400261 struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000262{
263 int reset = 0;
264
Fred Isaman0b7c0152012-04-20 14:47:39 -0400265 if (filelayout_async_handle_error(task, data->context->state,
Fred Isamane0c2b382011-03-23 13:27:53 +0000266 data->ds_clp, &reset) == -EAGAIN) {
267 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
268 __func__, data->ds_clp, data->ds_clp->cl_session);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400269 if (reset)
Fred Isamane0c2b382011-03-23 13:27:53 +0000270 prepare_to_resend_writes(data);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400271 else
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700272 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000273 return -EAGAIN;
274 }
275
276 return 0;
277}
278
Fred Isamana69aef12011-03-03 15:13:47 +0000279static void filelayout_write_prepare(struct rpc_task *task, void *data)
280{
Fred Isamancd12ae32012-04-20 14:47:42 -0400281 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000282
283 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
284 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500285 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000286 return;
287
288 rpc_call_start(task);
289}
290
291static void filelayout_write_call_done(struct rpc_task *task, void *data)
292{
Fred Isamancd12ae32012-04-20 14:47:42 -0400293 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000294
295 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400296 wdata->header->mds_ops->rpc_call_done(task, data);
Fred Isamana69aef12011-03-03 15:13:47 +0000297}
298
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500299static void filelayout_write_count_stats(struct rpc_task *task, void *data)
300{
Fred Isamancd12ae32012-04-20 14:47:42 -0400301 struct nfs_write_data *wdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500302
Fred Isamancd841602012-04-20 14:47:44 -0400303 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500304}
305
Fred Isamana69aef12011-03-03 15:13:47 +0000306static void filelayout_write_release(void *data)
307{
Fred Isamancd12ae32012-04-20 14:47:42 -0400308 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000309
Fred Isamancd841602012-04-20 14:47:44 -0400310 wdata->header->mds_ops->rpc_release(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000311}
312
Fred Isaman0b7c0152012-04-20 14:47:39 -0400313static void filelayout_commit_prepare(struct rpc_task *task, void *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000314{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400315 struct nfs_commit_data *wdata = data;
Fred Isamane0c2b382011-03-23 13:27:53 +0000316
Fred Isaman0b7c0152012-04-20 14:47:39 -0400317 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
318 &wdata->args.seq_args, &wdata->res.seq_res,
319 task))
320 return;
321
322 rpc_call_start(task);
323}
324
325static void filelayout_write_commit_done(struct rpc_task *task, void *data)
326{
327 struct nfs_commit_data *wdata = data;
328
329 /* Note this may cause RPC to be resent */
330 wdata->mds_ops->rpc_call_done(task, data);
331}
332
333static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
334{
335 struct nfs_commit_data *cdata = data;
336
337 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
338}
339
340static void filelayout_commit_release(void *calldata)
341{
342 struct nfs_commit_data *data = calldata;
343
Fred Isamanf453a542012-04-20 14:47:54 -0400344 data->completion_ops->completion(data);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400345 put_lseg(data->lseg);
346 nfs_commitdata_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000347}
348
Trond Myklebust17280172012-03-11 13:11:00 -0400349static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000350 .rpc_call_prepare = filelayout_read_prepare,
351 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500352 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000353 .rpc_release = filelayout_read_release,
354};
355
Trond Myklebust17280172012-03-11 13:11:00 -0400356static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000357 .rpc_call_prepare = filelayout_write_prepare,
358 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500359 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000360 .rpc_release = filelayout_write_release,
361};
362
Trond Myklebust17280172012-03-11 13:11:00 -0400363static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isaman0b7c0152012-04-20 14:47:39 -0400364 .rpc_call_prepare = filelayout_commit_prepare,
365 .rpc_call_done = filelayout_write_commit_done,
366 .rpc_count_stats = filelayout_commit_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000367 .rpc_release = filelayout_commit_release,
368};
369
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000370static enum pnfs_try_status
371filelayout_read_pagelist(struct nfs_read_data *data)
372{
Fred Isamancd841602012-04-20 14:47:44 -0400373 struct nfs_pgio_header *hdr = data->header;
374 struct pnfs_layout_segment *lseg = hdr->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000375 struct nfs4_pnfs_ds *ds;
376 loff_t offset = data->args.offset;
377 u32 j, idx;
378 struct nfs_fh *fh;
379 int status;
380
381 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
Fred Isamancd841602012-04-20 14:47:44 -0400382 __func__, hdr->inode->i_ino,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000383 data->args.pgbase, (size_t)data->args.count, offset);
384
385 /* Retrieve the correct rpc_client for the byte range */
386 j = nfs4_fl_calc_j_index(lseg, offset);
387 idx = nfs4_fl_calc_ds_index(lseg, j);
388 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400389 if (!ds)
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000390 return PNFS_NOT_ATTEMPTED;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400391 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000392
393 /* No multipath support. Use first DS */
394 data->ds_clp = ds->ds_clp;
395 fh = nfs4_fl_select_ds_fh(lseg, j);
396 if (fh)
397 data->args.fh = fh;
398
399 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
400 data->mds_offset = offset;
401
402 /* Perform an asynchronous read to ds */
Fred Isamanc5996c42012-04-20 14:47:41 -0400403 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000404 &filelayout_read_call_ops);
405 BUG_ON(status != 0);
406 return PNFS_ATTEMPTED;
407}
408
Fred Isamana69aef12011-03-03 15:13:47 +0000409/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000410static enum pnfs_try_status
411filelayout_write_pagelist(struct nfs_write_data *data, int sync)
412{
Fred Isamancd841602012-04-20 14:47:44 -0400413 struct nfs_pgio_header *hdr = data->header;
414 struct pnfs_layout_segment *lseg = hdr->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000415 struct nfs4_pnfs_ds *ds;
416 loff_t offset = data->args.offset;
417 u32 j, idx;
418 struct nfs_fh *fh;
419 int status;
420
421 /* Retrieve the correct rpc_client for the byte range */
422 j = nfs4_fl_calc_j_index(lseg, offset);
423 idx = nfs4_fl_calc_ds_index(lseg, j);
424 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400425 if (!ds)
Fred Isamana69aef12011-03-03 15:13:47 +0000426 return PNFS_NOT_ATTEMPTED;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400427 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
Fred Isamancd841602012-04-20 14:47:44 -0400428 hdr->inode->i_ino, sync, (size_t) data->args.count, offset,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400429 ds->ds_remotestr);
Fred Isamana69aef12011-03-03 15:13:47 +0000430
Fred Isamana69aef12011-03-03 15:13:47 +0000431 data->write_done_cb = filelayout_write_done_cb;
432 data->ds_clp = ds->ds_clp;
433 fh = nfs4_fl_select_ds_fh(lseg, j);
434 if (fh)
435 data->args.fh = fh;
436 /*
437 * Get the file offset on the dserver. Set the write offset to
438 * this offset and save the original offset.
439 */
440 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000441
442 /* Perform an asynchronous write */
Fred Isamanc5996c42012-04-20 14:47:41 -0400443 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
Fred Isamana69aef12011-03-03 15:13:47 +0000444 &filelayout_write_call_ops, sync);
445 BUG_ON(status != 0);
446 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000447}
448
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000449/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400450 * filelayout_check_layout()
451 *
452 * Make sure layout segment parameters are sane WRT the device.
453 * At this point no generic layer initialization of the lseg has occurred,
454 * and nothing has been added to the layout_hdr cache.
455 *
456 */
457static int
458filelayout_check_layout(struct pnfs_layout_hdr *lo,
459 struct nfs4_filelayout_segment *fl,
460 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400461 struct nfs4_deviceid *id,
462 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400463{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400464 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400465 struct nfs4_file_layout_dsaddr *dsaddr;
466 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000467 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400468
469 dprintk("--> %s\n", __func__);
470
Andy Adamson7c24d942011-06-13 18:22:38 -0400471 /* FIXME: remove this check when layout segment support is added */
472 if (lgr->range.offset != 0 ||
473 lgr->range.length != NFS4_MAX_UINT64) {
474 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
475 __func__);
476 goto out;
477 }
478
Andy Adamson16b374c2010-10-20 00:18:04 -0400479 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500480 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400481 __func__, fl->pattern_offset);
482 goto out;
483 }
484
Benny Halevy75247af2011-02-22 15:56:01 -0800485 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
486 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400487 __func__, fl->stripe_unit);
488 goto out;
489 }
490
491 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300492 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
493 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400494 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400495 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400496 if (dsaddr == NULL)
497 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400498 } else
499 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400500 /* Found deviceid is being reaped */
501 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
502 goto out_put;
503
Andy Adamson16b374c2010-10-20 00:18:04 -0400504 fl->dsaddr = dsaddr;
505
Chuck Levere4149662011-10-25 12:18:03 -0400506 if (fl->first_stripe_index >= dsaddr->stripe_count) {
507 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400508 __func__, fl->first_stripe_index);
509 goto out_put;
510 }
511
512 if ((fl->stripe_type == STRIPE_SPARSE &&
513 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
514 (fl->stripe_type == STRIPE_DENSE &&
515 fl->num_fh != dsaddr->stripe_count)) {
516 dprintk("%s num_fh %u not valid for given packing\n",
517 __func__, fl->num_fh);
518 goto out_put;
519 }
520
521 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
522 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
523 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
524 nfss->wsize);
525 }
526
527 status = 0;
528out:
529 dprintk("--> %s returns %d\n", __func__, status);
530 return status;
531out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000532 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400533 goto out;
534}
535
536static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
537{
538 int i;
539
540 for (i = 0; i < fl->num_fh; i++) {
541 if (!fl->fh_array[i])
542 break;
543 kfree(fl->fh_array[i]);
544 }
545 kfree(fl->fh_array);
546 fl->fh_array = NULL;
547}
548
549static void
550_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
551{
552 filelayout_free_fh_array(fl);
553 kfree(fl);
554}
555
556static int
557filelayout_decode_layout(struct pnfs_layout_hdr *flo,
558 struct nfs4_filelayout_segment *fl,
559 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400560 struct nfs4_deviceid *id,
561 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400562{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400563 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400564 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400565 struct page *scratch;
566 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400567 uint32_t nfl_util;
568 int i;
569
570 dprintk("%s: set_layout_map Begin\n", __func__);
571
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400572 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400573 if (!scratch)
574 return -ENOMEM;
575
Benny Halevyf7da7a12011-05-19 14:16:47 -0400576 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400577 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
578
579 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
580 * num_fh (4) */
581 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
582 if (unlikely(!p))
583 goto out_err;
584
Andy Adamson16b374c2010-10-20 00:18:04 -0400585 memcpy(id, p, sizeof(*id));
586 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400587 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400588
589 nfl_util = be32_to_cpup(p++);
590 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
591 fl->commit_through_mds = 1;
592 if (nfl_util & NFL4_UFLG_DENSE)
593 fl->stripe_type = STRIPE_DENSE;
594 else
595 fl->stripe_type = STRIPE_SPARSE;
596 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
597
598 fl->first_stripe_index = be32_to_cpup(p++);
599 p = xdr_decode_hyper(p, &fl->pattern_offset);
600 fl->num_fh = be32_to_cpup(p++);
601
602 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
603 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
604 fl->pattern_offset);
605
Andy Adamsoncec765c2011-06-13 18:36:17 -0400606 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
607 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400608 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400609 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400610 goto out_err;
611
Andy Adamsoncec765c2011-06-13 18:36:17 -0400612 if (fl->num_fh > 0) {
613 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
614 gfp_flags);
615 if (!fl->fh_array)
616 goto out_err;
617 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400618
619 for (i = 0; i < fl->num_fh; i++) {
620 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400621 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400622 if (!fl->fh_array[i])
623 goto out_err_free;
624
625 p = xdr_inline_decode(&stream, 4);
626 if (unlikely(!p))
627 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400628 fl->fh_array[i]->size = be32_to_cpup(p++);
629 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500630 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400631 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400632 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400633 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400634
635 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
636 if (unlikely(!p))
637 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400638 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400639 dprintk("DEBUG: %s: fh len %d\n", __func__,
640 fl->fh_array[i]->size);
641 }
642
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400643 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400644 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400645
646out_err_free:
647 filelayout_free_fh_array(fl);
648out_err:
649 __free_page(scratch);
650 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400651}
652
Fred Isamanc8795132011-03-23 13:27:49 +0000653static void
654filelayout_free_lseg(struct pnfs_layout_segment *lseg)
655{
656 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
657
658 dprintk("--> %s\n", __func__);
659 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400660 /* This assumes a single RW lseg */
661 if (lseg->pls_range.iomode == IOMODE_RW) {
662 struct nfs4_filelayout *flo;
663
664 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
665 flo->commit_info.nbuckets = 0;
666 kfree(flo->commit_info.buckets);
667 flo->commit_info.buckets = NULL;
668 }
Fred Isamanc8795132011-03-23 13:27:49 +0000669 _filelayout_free_lseg(fl);
670}
671
Fred Isaman799ba8d2012-04-20 14:47:38 -0400672static int
673filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
Fred Isamanea2cf222012-04-20 14:47:53 -0400674 struct nfs_commit_info *cinfo,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400675 gfp_t gfp_flags)
676{
677 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
Fred Isamanea2cf222012-04-20 14:47:53 -0400678 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400679 int size;
680
681 if (fl->commit_through_mds)
682 return 0;
Fred Isamanea2cf222012-04-20 14:47:53 -0400683 if (cinfo->ds->nbuckets != 0) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400684 /* This assumes there is only one IOMODE_RW lseg. What
685 * we really want to do is have a layout_hdr level
686 * dictionary of <multipath_list4, fh> keys, each
687 * associated with a struct list_head, populated by calls
688 * to filelayout_write_pagelist().
689 * */
690 return 0;
691 }
692
693 size = (fl->stripe_type == STRIPE_SPARSE) ?
694 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
695
Fred Isamanea2cf222012-04-20 14:47:53 -0400696 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
Fred Isaman799ba8d2012-04-20 14:47:38 -0400697 gfp_flags);
698 if (!buckets)
699 return -ENOMEM;
700 else {
701 int i;
702
Fred Isamanea2cf222012-04-20 14:47:53 -0400703 spin_lock(cinfo->lock);
704 if (cinfo->ds->nbuckets != 0)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400705 kfree(buckets);
706 else {
Fred Isamanea2cf222012-04-20 14:47:53 -0400707 cinfo->ds->buckets = buckets;
708 cinfo->ds->nbuckets = size;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400709 for (i = 0; i < size; i++) {
710 INIT_LIST_HEAD(&buckets[i].written);
711 INIT_LIST_HEAD(&buckets[i].committing);
712 }
713 }
Fred Isamanea2cf222012-04-20 14:47:53 -0400714 spin_unlock(cinfo->lock);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400715 return 0;
716 }
717}
718
Andy Adamson16b374c2010-10-20 00:18:04 -0400719static struct pnfs_layout_segment *
720filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400721 struct nfs4_layoutget_res *lgr,
722 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400723{
724 struct nfs4_filelayout_segment *fl;
725 int rc;
726 struct nfs4_deviceid id;
727
728 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400729 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400730 if (!fl)
731 return NULL;
732
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400733 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
734 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400735 _filelayout_free_lseg(fl);
736 return NULL;
737 }
738 return &fl->generic_hdr;
739}
740
Fred Isaman94ad1c82011-03-01 01:34:14 +0000741/*
742 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
743 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300744 * return true : coalesce page
745 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000746 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400747static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000748filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
749 struct nfs_page *req)
750{
751 u64 p_stripe, r_stripe;
752 u32 stripe_unit;
753
Benny Halevy19345cb2011-06-19 18:33:46 -0400754 if (!pnfs_generic_pg_test(pgio, prev, req) ||
755 !nfs_generic_pg_test(pgio, prev, req))
756 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300757
Fred Isamanb5542842012-04-20 14:47:43 -0400758 p_stripe = (u64)req_offset(prev);
759 r_stripe = (u64)req_offset(req);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000760 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
761
762 do_div(p_stripe, stripe_unit);
763 do_div(r_stripe, stripe_unit);
764
765 return (p_stripe == r_stripe);
766}
767
Trond Myklebust17280172012-03-11 13:11:00 -0400768static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400769filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
770 struct nfs_page *req)
771{
772 BUG_ON(pgio->pg_lseg != NULL);
773
Fred Isaman1825a0d2012-04-20 19:55:31 -0400774 if (req->wb_offset != req->wb_pgbase) {
775 /*
776 * Handling unaligned pages is difficult, because have to
777 * somehow split a req in two in certain cases in the
778 * pg.test code. Avoid this by just not using pnfs
779 * in this case.
780 */
781 nfs_pageio_reset_read_mds(pgio);
782 return;
783 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400784 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
785 req->wb_context,
786 0,
787 NFS4_MAX_UINT64,
788 IOMODE_READ,
789 GFP_KERNEL);
790 /* If no lseg, fall back to read through mds */
791 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400792 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400793}
794
Trond Myklebust17280172012-03-11 13:11:00 -0400795static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400796filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
797 struct nfs_page *req)
798{
Fred Isamanea2cf222012-04-20 14:47:53 -0400799 struct nfs_commit_info cinfo;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400800 int status;
801
Andy Adamson7c24d942011-06-13 18:22:38 -0400802 BUG_ON(pgio->pg_lseg != NULL);
803
Fred Isaman1825a0d2012-04-20 19:55:31 -0400804 if (req->wb_offset != req->wb_pgbase)
805 goto out_mds;
Andy Adamson7c24d942011-06-13 18:22:38 -0400806 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
807 req->wb_context,
808 0,
809 NFS4_MAX_UINT64,
810 IOMODE_RW,
811 GFP_NOFS);
812 /* If no lseg, fall back to write through mds */
813 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400814 goto out_mds;
Fred Isamanea2cf222012-04-20 14:47:53 -0400815 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
816 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400817 if (status < 0) {
818 put_lseg(pgio->pg_lseg);
819 pgio->pg_lseg = NULL;
820 goto out_mds;
821 }
822 return;
823out_mds:
824 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400825}
826
Trond Myklebust1751c362011-06-10 13:30:23 -0400827static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400828 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400829 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400830 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400831};
832
833static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400834 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400835 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400836 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400837};
838
Fred Isamane0c2b382011-03-23 13:27:53 +0000839static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
840{
841 if (fl->stripe_type == STRIPE_SPARSE)
842 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
843 else
844 return j;
845}
846
Fred Isamand6d6dc72012-03-08 17:29:35 -0500847/* The generic layer is about to remove the req from the commit list.
848 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500849 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400850static void
Fred Isamanea2cf222012-04-20 14:47:53 -0400851filelayout_clear_request_commit(struct nfs_page *req,
852 struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +0000853{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400854 struct pnfs_layout_segment *freeme = NULL;
Trond Myklebust8dd37752012-03-15 17:16:40 -0400855
Fred Isamanea2cf222012-04-20 14:47:53 -0400856 spin_lock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400857 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
858 goto out;
Fred Isamanea2cf222012-04-20 14:47:53 -0400859 cinfo->ds->nwritten--;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500860 if (list_is_singular(&req->wb_list)) {
Fred Isamanea2cf222012-04-20 14:47:53 -0400861 struct pnfs_commit_bucket *bucket;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500862
Fred Isaman799ba8d2012-04-20 14:47:38 -0400863 bucket = list_first_entry(&req->wb_list,
Fred Isamanea2cf222012-04-20 14:47:53 -0400864 struct pnfs_commit_bucket,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400865 written);
866 freeme = bucket->wlseg;
867 bucket->wlseg = NULL;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500868 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400869out:
Fred Isamanea2cf222012-04-20 14:47:53 -0400870 nfs_request_remove_commit_list(req, cinfo);
871 spin_unlock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400872 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500873}
874
875static struct list_head *
876filelayout_choose_commit_list(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -0400877 struct pnfs_layout_segment *lseg,
878 struct nfs_commit_info *cinfo)
Fred Isamand6d6dc72012-03-08 17:29:35 -0500879{
Fred Isamane0c2b382011-03-23 13:27:53 +0000880 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
881 u32 i, j;
882 struct list_head *list;
Fred Isamanea2cf222012-04-20 14:47:53 -0400883 struct pnfs_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +0000884
Fred Isamand6d6dc72012-03-08 17:29:35 -0500885 if (fl->commit_through_mds)
Fred Isamanea2cf222012-04-20 14:47:53 -0400886 return &cinfo->mds->list;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500887
Fred Isamane0c2b382011-03-23 13:27:53 +0000888 /* Note that we are calling nfs4_fl_calc_j_index on each page
889 * that ends up being committed to a data server. An attractive
890 * alternative is to add a field to nfs_write_data and nfs_page
891 * to store the value calculated in filelayout_write_pagelist
892 * and just use that here.
893 */
Fred Isamanb5542842012-04-20 14:47:43 -0400894 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
Fred Isamane0c2b382011-03-23 13:27:53 +0000895 i = select_bucket_index(fl, j);
Fred Isamanea2cf222012-04-20 14:47:53 -0400896 buckets = cinfo->ds->buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400897 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +0000898 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500899 /* Non-empty buckets hold a reference on the lseg. That ref
900 * is normally transferred to the COMMIT call and released
901 * there. It could also be released if the last req is pulled
902 * off due to a rewrite, in which case it will be done in
Fred Isaman799ba8d2012-04-20 14:47:38 -0400903 * filelayout_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -0500904 */
Fred Isaman799ba8d2012-04-20 14:47:38 -0400905 buckets[i].wlseg = get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000906 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400907 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamanea2cf222012-04-20 14:47:53 -0400908 cinfo->ds->nwritten++;
Fred Isamane0c2b382011-03-23 13:27:53 +0000909 return list;
910}
911
Trond Myklebust8dd37752012-03-15 17:16:40 -0400912static void
913filelayout_mark_request_commit(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -0400914 struct pnfs_layout_segment *lseg,
915 struct nfs_commit_info *cinfo)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400916{
917 struct list_head *list;
918
Fred Isamanea2cf222012-04-20 14:47:53 -0400919 list = filelayout_choose_commit_list(req, lseg, cinfo);
920 nfs_request_add_commit_list(req, list, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400921}
922
Fred Isamane0c2b382011-03-23 13:27:53 +0000923static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
924{
925 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
926
927 if (flseg->stripe_type == STRIPE_SPARSE)
928 return i;
929 else
930 return nfs4_fl_calc_ds_index(lseg, i);
931}
932
933static struct nfs_fh *
934select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
935{
936 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
937
938 if (flseg->stripe_type == STRIPE_SPARSE) {
939 if (flseg->num_fh == 1)
940 i = 0;
941 else if (flseg->num_fh == 0)
942 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
943 return NULL;
944 }
945 return flseg->fh_array[i];
946}
947
Fred Isaman0b7c0152012-04-20 14:47:39 -0400948static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
Fred Isamane0c2b382011-03-23 13:27:53 +0000949{
950 struct pnfs_layout_segment *lseg = data->lseg;
951 struct nfs4_pnfs_ds *ds;
952 u32 idx;
953 struct nfs_fh *fh;
954
955 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
956 ds = nfs4_fl_prepare_ds(lseg, idx);
957 if (!ds) {
Fred Isamane0c2b382011-03-23 13:27:53 +0000958 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400959 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000960 return -EAGAIN;
961 }
962 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400963 data->commit_done_cb = filelayout_commit_done_cb;
Fred Isamane0c2b382011-03-23 13:27:53 +0000964 data->ds_clp = ds->ds_clp;
965 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
966 if (fh)
967 data->args.fh = fh;
Fred Isaman0b7c0152012-04-20 14:47:39 -0400968 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
Fred Isamane0c2b382011-03-23 13:27:53 +0000969 &filelayout_commit_call_ops, how);
970}
971
Trond Myklebust8dd37752012-03-15 17:16:40 -0400972static int
Fred Isaman1763da12012-04-20 14:47:57 -0400973transfer_commit_list(struct list_head *src, struct list_head *dst,
974 struct nfs_commit_info *cinfo, int max)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400975{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400976 struct nfs_page *req, *tmp;
977 int ret = 0;
978
979 list_for_each_entry_safe(req, tmp, src, wb_list) {
980 if (!nfs_lock_request(req))
981 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -0400982 if (cond_resched_lock(cinfo->lock))
Trond Myklebust3b3be882012-03-17 11:59:30 -0400983 list_safe_reset_next(req, tmp, wb_list);
Fred Isamanea2cf222012-04-20 14:47:53 -0400984 nfs_request_remove_commit_list(req, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400985 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
986 nfs_list_add_request(req, dst);
987 ret++;
Fred Isaman1763da12012-04-20 14:47:57 -0400988 if ((ret == max) && !cinfo->dreq)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400989 break;
990 }
Fred Isaman1763da12012-04-20 14:47:57 -0400991 return ret;
992}
993
994static int
995filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
996 struct nfs_commit_info *cinfo,
997 int max)
998{
999 struct list_head *src = &bucket->written;
1000 struct list_head *dst = &bucket->committing;
1001 int ret;
1002
1003 ret = transfer_commit_list(src, dst, cinfo, max);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001004 if (ret) {
Fred Isamanea2cf222012-04-20 14:47:53 -04001005 cinfo->ds->nwritten -= ret;
1006 cinfo->ds->ncommitting += ret;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001007 bucket->clseg = bucket->wlseg;
1008 if (list_empty(src))
1009 bucket->wlseg = NULL;
1010 else
1011 get_lseg(bucket->clseg);
1012 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001013 return ret;
1014}
1015
Fred Isamand6d6dc72012-03-08 17:29:35 -05001016/* Move reqs from written to committing lists, returning count of number moved.
Fred Isamanea2cf222012-04-20 14:47:53 -04001017 * Note called with cinfo->lock held.
Fred Isamand6d6dc72012-03-08 17:29:35 -05001018 */
Fred Isamanea2cf222012-04-20 14:47:53 -04001019static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1020 int max)
Fred Isamand6d6dc72012-03-08 17:29:35 -05001021{
Fred Isamand6d6dc72012-03-08 17:29:35 -05001022 int i, rv = 0, cnt;
1023
Fred Isamanea2cf222012-04-20 14:47:53 -04001024 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1025 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1026 cinfo, max);
Fred Isamand6d6dc72012-03-08 17:29:35 -05001027 max -= cnt;
1028 rv += cnt;
1029 }
Fred Isamand6d6dc72012-03-08 17:29:35 -05001030 return rv;
1031}
1032
Fred Isaman1763da12012-04-20 14:47:57 -04001033/* Pull everything off the committing lists and dump into @dst */
1034static void filelayout_recover_commit_reqs(struct list_head *dst,
1035 struct nfs_commit_info *cinfo)
1036{
1037 struct pnfs_commit_bucket *b;
1038 int i;
1039
1040 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1041 * only called on single thread per dreq.
1042 * Can't take the lock because need to do put_lseg
1043 */
1044 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1045 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1046 BUG_ON(!list_empty(&b->written));
1047 put_lseg(b->wlseg);
1048 b->wlseg = NULL;
1049 }
1050 }
1051 cinfo->ds->nwritten = 0;
1052}
1053
Trond Myklebust9390f422012-03-16 13:52:45 -04001054static unsigned int
Fred Isamanea2cf222012-04-20 14:47:53 -04001055alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +00001056{
Fred Isamanea2cf222012-04-20 14:47:53 -04001057 struct pnfs_ds_commit_info *fl_cinfo;
1058 struct pnfs_commit_bucket *bucket;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001059 struct nfs_commit_data *data;
Fred Isamane0c2b382011-03-23 13:27:53 +00001060 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -04001061 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001062
Fred Isamanea2cf222012-04-20 14:47:53 -04001063 fl_cinfo = cinfo->ds;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001064 bucket = fl_cinfo->buckets;
1065 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1066 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001067 continue;
1068 data = nfs_commitdata_alloc();
1069 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001070 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001071 data->ds_commit_index = i;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001072 data->lseg = bucket->clseg;
1073 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001074 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001075 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001076 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001077
Trond Myklebust9390f422012-03-16 13:52:45 -04001078 /* Clean up on error */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001079 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1080 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001081 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -04001082 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001083 put_lseg(bucket->clseg);
1084 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001085 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001086 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001087 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001088}
1089
1090/* This follows nfs_commit_list pretty closely */
1091static int
1092filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
Fred Isamanea2cf222012-04-20 14:47:53 -04001093 int how, struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +00001094{
Fred Isaman0b7c0152012-04-20 14:47:39 -04001095 struct nfs_commit_data *data, *tmp;
Fred Isamane0c2b382011-03-23 13:27:53 +00001096 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001097 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001098
1099 if (!list_empty(mds_pages)) {
1100 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001101 if (data != NULL) {
1102 data->lseg = NULL;
1103 list_add(&data->pages, &list);
1104 nreq++;
1105 } else
Fred Isamanea2cf222012-04-20 14:47:53 -04001106 nfs_retry_commit(mds_pages, NULL, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001107 }
1108
Fred Isamanea2cf222012-04-20 14:47:53 -04001109 nreq += alloc_ds_commits(cinfo, &list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001110
1111 if (nreq == 0) {
Fred Isamanf453a542012-04-20 14:47:54 -04001112 cinfo->completion_ops->error_cleanup(NFS_I(inode));
Trond Myklebust9390f422012-03-16 13:52:45 -04001113 goto out;
1114 }
1115
Fred Isamanea2cf222012-04-20 14:47:53 -04001116 atomic_add(nreq, &cinfo->mds->rpcs_out);
Fred Isamane0c2b382011-03-23 13:27:53 +00001117
1118 list_for_each_entry_safe(data, tmp, &list, pages) {
1119 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001120 if (!data->lseg) {
Fred Isamanf453a542012-04-20 14:47:54 -04001121 nfs_init_commit(data, mds_pages, NULL, cinfo);
Fred Isaman0b7c0152012-04-20 14:47:39 -04001122 nfs_initiate_commit(NFS_CLIENT(inode), data,
Fred Isamane0c2b382011-03-23 13:27:53 +00001123 data->mds_ops, how);
1124 } else {
Fred Isamanea2cf222012-04-20 14:47:53 -04001125 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001126
Fred Isamanea2cf222012-04-20 14:47:53 -04001127 buckets = cinfo->ds->buckets;
Fred Isamanf453a542012-04-20 14:47:54 -04001128 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001129 filelayout_initiate_commit(data, how);
1130 }
1131 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001132out:
Fred Isamanea2cf222012-04-20 14:47:53 -04001133 cinfo->ds->ncommitting = 0;
Trond Myklebust9390f422012-03-16 13:52:45 -04001134 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001135}
1136
Benny Halevy1775bc32011-05-20 13:47:33 +02001137static void
1138filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1139{
1140 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1141}
1142
Fred Isaman799ba8d2012-04-20 14:47:38 -04001143static struct pnfs_layout_hdr *
1144filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1145{
1146 struct nfs4_filelayout *flo;
1147
1148 flo = kzalloc(sizeof(*flo), gfp_flags);
1149 return &flo->generic_hdr;
1150}
1151
1152static void
1153filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1154{
1155 kfree(FILELAYOUT_FROM_HDR(lo));
1156}
1157
Fred Isamanea2cf222012-04-20 14:47:53 -04001158static struct pnfs_ds_commit_info *
1159filelayout_get_ds_info(struct inode *inode)
1160{
Fred Isamandf011742012-04-24 14:50:34 -04001161 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1162
1163 if (layout == NULL)
1164 return NULL;
1165 else
1166 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
Fred Isamanea2cf222012-04-20 14:47:53 -04001167}
1168
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001169static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001170 .id = LAYOUT_NFSV4_1_FILES,
1171 .name = "LAYOUT_NFSV4_1_FILES",
1172 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001173 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1174 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001175 .alloc_lseg = filelayout_alloc_lseg,
1176 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001177 .pg_read_ops = &filelayout_pg_read_ops,
1178 .pg_write_ops = &filelayout_pg_write_ops,
Fred Isamanea2cf222012-04-20 14:47:53 -04001179 .get_ds_info = &filelayout_get_ds_info,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001180 .mark_request_commit = filelayout_mark_request_commit,
1181 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001182 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isaman1763da12012-04-20 14:47:57 -04001183 .recover_commit_reqs = filelayout_recover_commit_reqs,
Fred Isamane0c2b382011-03-23 13:27:53 +00001184 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001185 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001186 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001187 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001188};
1189
1190static int __init nfs4filelayout_init(void)
1191{
1192 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1193 __func__);
1194 return pnfs_register_layoutdriver(&filelayout_type);
1195}
1196
1197static void __exit nfs4filelayout_exit(void)
1198{
1199 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1200 __func__);
1201 pnfs_unregister_layoutdriver(&filelayout_type);
1202}
1203
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001204MODULE_ALIAS("nfs-layouttype4-1");
1205
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001206module_init(nfs4filelayout_init);
1207module_exit(nfs4filelayout_exit);