blob: 02d8170ce0f3b7a4a3e15ac706fc9ced77fd16b0 [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{
Fred Isamancd841602012-04-20 14:47:44 -0400151 struct nfs_pgio_header *hdr = data->header;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000152 int reset = 0;
153
154 dprintk("%s DS read\n", __func__);
155
156 if (filelayout_async_handle_error(task, data->args.context->state,
157 data->ds_clp, &reset) == -EAGAIN) {
158 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
159 __func__, data->ds_clp, data->ds_clp->cl_session);
160 if (reset) {
Fred Isamancd841602012-04-20 14:47:44 -0400161 pnfs_set_lo_fail(hdr->lseg);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000162 nfs4_reset_read(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000163 }
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700164 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000165 return -EAGAIN;
166 }
167
168 return 0;
169}
170
Andy Adamson16b374c2010-10-20 00:18:04 -0400171/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000172 * We reference the rpc_cred of the first WRITE that triggers the need for
173 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
174 * rfc5661 is not clear about which credential should be used.
175 */
176static void
177filelayout_set_layoutcommit(struct nfs_write_data *wdata)
178{
Fred Isamancd841602012-04-20 14:47:44 -0400179 struct nfs_pgio_header *hdr = wdata->header;
180
181 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
Andy Adamson863a3c62011-03-23 13:27:54 +0000182 wdata->res.verf->committed == NFS_FILE_SYNC)
183 return;
184
185 pnfs_set_layoutcommit(wdata);
Fred Isamancd841602012-04-20 14:47:44 -0400186 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
187 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000188}
189
190/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000191 * Call ops for the async read/write cases
192 * In the case of dense layouts, the offset needs to be reset to its
193 * original value.
194 */
195static void filelayout_read_prepare(struct rpc_task *task, void *data)
196{
Fred Isamancd12ae32012-04-20 14:47:42 -0400197 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000198
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000199 rdata->read_done_cb = filelayout_read_done_cb;
200
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000201 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
202 &rdata->args.seq_args, &rdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500203 task))
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000204 return;
205
206 rpc_call_start(task);
207}
208
209static void filelayout_read_call_done(struct rpc_task *task, void *data)
210{
Fred Isamancd12ae32012-04-20 14:47:42 -0400211 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000212
213 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
214
215 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400216 rdata->header->mds_ops->rpc_call_done(task, data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000217}
218
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500219static void filelayout_read_count_stats(struct rpc_task *task, void *data)
220{
Fred Isamancd12ae32012-04-20 14:47:42 -0400221 struct nfs_read_data *rdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500222
Fred Isamancd841602012-04-20 14:47:44 -0400223 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500224}
225
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000226static void filelayout_read_release(void *data)
227{
Fred Isamancd12ae32012-04-20 14:47:42 -0400228 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000229
Fred Isamancd841602012-04-20 14:47:44 -0400230 rdata->header->mds_ops->rpc_release(data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000231}
232
Fred Isamana69aef12011-03-03 15:13:47 +0000233static int filelayout_write_done_cb(struct rpc_task *task,
234 struct nfs_write_data *data)
235{
Fred Isamancd841602012-04-20 14:47:44 -0400236 struct nfs_pgio_header *hdr = data->header;
Fred Isamana69aef12011-03-03 15:13:47 +0000237 int reset = 0;
238
239 if (filelayout_async_handle_error(task, data->args.context->state,
240 data->ds_clp, &reset) == -EAGAIN) {
Fred Isamana69aef12011-03-03 15:13:47 +0000241 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
242 __func__, data->ds_clp, data->ds_clp->cl_session);
243 if (reset) {
Fred Isamancd841602012-04-20 14:47:44 -0400244 pnfs_set_lo_fail(hdr->lseg);
Fred Isamana69aef12011-03-03 15:13:47 +0000245 nfs4_reset_write(task, data);
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700246 }
247 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000248 return -EAGAIN;
249 }
250
Andy Adamson863a3c62011-03-23 13:27:54 +0000251 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000252 return 0;
253}
254
Fred Isamane0c2b382011-03-23 13:27:53 +0000255/* Fake up some data that will cause nfs_commit_release to retry the writes. */
Fred Isaman0b7c0152012-04-20 14:47:39 -0400256static void prepare_to_resend_writes(struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000257{
258 struct nfs_page *first = nfs_list_entry(data->pages.next);
259
260 data->task.tk_status = 0;
261 memcpy(data->verf.verifier, first->wb_verf.verifier,
262 sizeof(first->wb_verf.verifier));
263 data->verf.verifier[0]++; /* ensure verifier mismatch */
264}
265
266static int filelayout_commit_done_cb(struct rpc_task *task,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400267 struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000268{
269 int reset = 0;
270
Fred Isaman0b7c0152012-04-20 14:47:39 -0400271 if (filelayout_async_handle_error(task, data->context->state,
Fred Isamane0c2b382011-03-23 13:27:53 +0000272 data->ds_clp, &reset) == -EAGAIN) {
273 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
274 __func__, data->ds_clp, data->ds_clp->cl_session);
275 if (reset) {
276 prepare_to_resend_writes(data);
Peng Tao1b0ae062011-09-22 21:50:12 -0400277 pnfs_set_lo_fail(data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000278 } else
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700279 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000280 return -EAGAIN;
281 }
282
283 return 0;
284}
285
Fred Isamana69aef12011-03-03 15:13:47 +0000286static void filelayout_write_prepare(struct rpc_task *task, void *data)
287{
Fred Isamancd12ae32012-04-20 14:47:42 -0400288 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000289
290 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
291 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500292 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000293 return;
294
295 rpc_call_start(task);
296}
297
298static void filelayout_write_call_done(struct rpc_task *task, void *data)
299{
Fred Isamancd12ae32012-04-20 14:47:42 -0400300 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000301
302 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400303 wdata->header->mds_ops->rpc_call_done(task, data);
Fred Isamana69aef12011-03-03 15:13:47 +0000304}
305
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500306static void filelayout_write_count_stats(struct rpc_task *task, void *data)
307{
Fred Isamancd12ae32012-04-20 14:47:42 -0400308 struct nfs_write_data *wdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500309
Fred Isamancd841602012-04-20 14:47:44 -0400310 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500311}
312
Fred Isamana69aef12011-03-03 15:13:47 +0000313static void filelayout_write_release(void *data)
314{
Fred Isamancd12ae32012-04-20 14:47:42 -0400315 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000316
Fred Isamancd841602012-04-20 14:47:44 -0400317 wdata->header->mds_ops->rpc_release(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000318}
319
Fred Isaman0b7c0152012-04-20 14:47:39 -0400320static void filelayout_commit_prepare(struct rpc_task *task, void *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000321{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400322 struct nfs_commit_data *wdata = data;
Fred Isamane0c2b382011-03-23 13:27:53 +0000323
Fred Isaman0b7c0152012-04-20 14:47:39 -0400324 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
325 &wdata->args.seq_args, &wdata->res.seq_res,
326 task))
327 return;
328
329 rpc_call_start(task);
330}
331
332static void filelayout_write_commit_done(struct rpc_task *task, void *data)
333{
334 struct nfs_commit_data *wdata = data;
335
336 /* Note this may cause RPC to be resent */
337 wdata->mds_ops->rpc_call_done(task, data);
338}
339
340static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
341{
342 struct nfs_commit_data *cdata = data;
343
344 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
345}
346
347static void filelayout_commit_release(void *calldata)
348{
349 struct nfs_commit_data *data = calldata;
350
351 nfs_commit_release_pages(data);
352 if (atomic_dec_and_test(&NFS_I(data->inode)->commits_outstanding))
353 nfs_commit_clear_lock(NFS_I(data->inode));
354 put_lseg(data->lseg);
355 nfs_commitdata_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000356}
357
Trond Myklebust17280172012-03-11 13:11:00 -0400358static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000359 .rpc_call_prepare = filelayout_read_prepare,
360 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500361 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000362 .rpc_release = filelayout_read_release,
363};
364
Trond Myklebust17280172012-03-11 13:11:00 -0400365static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000366 .rpc_call_prepare = filelayout_write_prepare,
367 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500368 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000369 .rpc_release = filelayout_write_release,
370};
371
Trond Myklebust17280172012-03-11 13:11:00 -0400372static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isaman0b7c0152012-04-20 14:47:39 -0400373 .rpc_call_prepare = filelayout_commit_prepare,
374 .rpc_call_done = filelayout_write_commit_done,
375 .rpc_count_stats = filelayout_commit_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000376 .rpc_release = filelayout_commit_release,
377};
378
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000379static enum pnfs_try_status
380filelayout_read_pagelist(struct nfs_read_data *data)
381{
Fred Isamancd841602012-04-20 14:47:44 -0400382 struct nfs_pgio_header *hdr = data->header;
383 struct pnfs_layout_segment *lseg = hdr->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000384 struct nfs4_pnfs_ds *ds;
385 loff_t offset = data->args.offset;
386 u32 j, idx;
387 struct nfs_fh *fh;
388 int status;
389
390 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
Fred Isamancd841602012-04-20 14:47:44 -0400391 __func__, hdr->inode->i_ino,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000392 data->args.pgbase, (size_t)data->args.count, offset);
393
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400394 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
395 return PNFS_NOT_ATTEMPTED;
396
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000397 /* Retrieve the correct rpc_client for the byte range */
398 j = nfs4_fl_calc_j_index(lseg, offset);
399 idx = nfs4_fl_calc_ds_index(lseg, j);
400 ds = nfs4_fl_prepare_ds(lseg, idx);
401 if (!ds) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000402 /* Either layout fh index faulty, or ds connect failed */
403 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
404 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000405 return PNFS_NOT_ATTEMPTED;
406 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400407 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000408
409 /* No multipath support. Use first DS */
410 data->ds_clp = ds->ds_clp;
411 fh = nfs4_fl_select_ds_fh(lseg, j);
412 if (fh)
413 data->args.fh = fh;
414
415 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
416 data->mds_offset = offset;
417
418 /* Perform an asynchronous read to ds */
Fred Isamanc5996c42012-04-20 14:47:41 -0400419 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000420 &filelayout_read_call_ops);
421 BUG_ON(status != 0);
422 return PNFS_ATTEMPTED;
423}
424
Fred Isamana69aef12011-03-03 15:13:47 +0000425/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000426static enum pnfs_try_status
427filelayout_write_pagelist(struct nfs_write_data *data, int sync)
428{
Fred Isamancd841602012-04-20 14:47:44 -0400429 struct nfs_pgio_header *hdr = data->header;
430 struct pnfs_layout_segment *lseg = hdr->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000431 struct nfs4_pnfs_ds *ds;
432 loff_t offset = data->args.offset;
433 u32 j, idx;
434 struct nfs_fh *fh;
435 int status;
436
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400437 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
438 return PNFS_NOT_ATTEMPTED;
439
Fred Isamana69aef12011-03-03 15:13:47 +0000440 /* Retrieve the correct rpc_client for the byte range */
441 j = nfs4_fl_calc_j_index(lseg, offset);
442 idx = nfs4_fl_calc_ds_index(lseg, j);
443 ds = nfs4_fl_prepare_ds(lseg, idx);
444 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500445 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
446 __func__);
Fred Isamana69aef12011-03-03 15:13:47 +0000447 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
448 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
449 return PNFS_NOT_ATTEMPTED;
450 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400451 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
Fred Isamancd841602012-04-20 14:47:44 -0400452 hdr->inode->i_ino, sync, (size_t) data->args.count, offset,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400453 ds->ds_remotestr);
Fred Isamana69aef12011-03-03 15:13:47 +0000454
Fred Isamana69aef12011-03-03 15:13:47 +0000455 data->write_done_cb = filelayout_write_done_cb;
456 data->ds_clp = ds->ds_clp;
457 fh = nfs4_fl_select_ds_fh(lseg, j);
458 if (fh)
459 data->args.fh = fh;
460 /*
461 * Get the file offset on the dserver. Set the write offset to
462 * this offset and save the original offset.
463 */
464 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000465
466 /* Perform an asynchronous write */
Fred Isamanc5996c42012-04-20 14:47:41 -0400467 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
Fred Isamana69aef12011-03-03 15:13:47 +0000468 &filelayout_write_call_ops, sync);
469 BUG_ON(status != 0);
470 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000471}
472
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000473/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400474 * filelayout_check_layout()
475 *
476 * Make sure layout segment parameters are sane WRT the device.
477 * At this point no generic layer initialization of the lseg has occurred,
478 * and nothing has been added to the layout_hdr cache.
479 *
480 */
481static int
482filelayout_check_layout(struct pnfs_layout_hdr *lo,
483 struct nfs4_filelayout_segment *fl,
484 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400485 struct nfs4_deviceid *id,
486 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400487{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400488 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400489 struct nfs4_file_layout_dsaddr *dsaddr;
490 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000491 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400492
493 dprintk("--> %s\n", __func__);
494
Andy Adamson7c24d942011-06-13 18:22:38 -0400495 /* FIXME: remove this check when layout segment support is added */
496 if (lgr->range.offset != 0 ||
497 lgr->range.length != NFS4_MAX_UINT64) {
498 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
499 __func__);
500 goto out;
501 }
502
Andy Adamson16b374c2010-10-20 00:18:04 -0400503 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500504 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400505 __func__, fl->pattern_offset);
506 goto out;
507 }
508
Benny Halevy75247af2011-02-22 15:56:01 -0800509 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
510 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400511 __func__, fl->stripe_unit);
512 goto out;
513 }
514
515 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300516 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
517 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400518 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400519 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400520 if (dsaddr == NULL)
521 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400522 } else
523 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400524 /* Found deviceid is being reaped */
525 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
526 goto out_put;
527
Andy Adamson16b374c2010-10-20 00:18:04 -0400528 fl->dsaddr = dsaddr;
529
Chuck Levere4149662011-10-25 12:18:03 -0400530 if (fl->first_stripe_index >= dsaddr->stripe_count) {
531 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400532 __func__, fl->first_stripe_index);
533 goto out_put;
534 }
535
536 if ((fl->stripe_type == STRIPE_SPARSE &&
537 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
538 (fl->stripe_type == STRIPE_DENSE &&
539 fl->num_fh != dsaddr->stripe_count)) {
540 dprintk("%s num_fh %u not valid for given packing\n",
541 __func__, fl->num_fh);
542 goto out_put;
543 }
544
545 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
546 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
547 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
548 nfss->wsize);
549 }
550
551 status = 0;
552out:
553 dprintk("--> %s returns %d\n", __func__, status);
554 return status;
555out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000556 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400557 goto out;
558}
559
560static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
561{
562 int i;
563
564 for (i = 0; i < fl->num_fh; i++) {
565 if (!fl->fh_array[i])
566 break;
567 kfree(fl->fh_array[i]);
568 }
569 kfree(fl->fh_array);
570 fl->fh_array = NULL;
571}
572
573static void
574_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
575{
576 filelayout_free_fh_array(fl);
577 kfree(fl);
578}
579
580static int
581filelayout_decode_layout(struct pnfs_layout_hdr *flo,
582 struct nfs4_filelayout_segment *fl,
583 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400584 struct nfs4_deviceid *id,
585 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400586{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400587 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400588 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400589 struct page *scratch;
590 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400591 uint32_t nfl_util;
592 int i;
593
594 dprintk("%s: set_layout_map Begin\n", __func__);
595
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400596 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400597 if (!scratch)
598 return -ENOMEM;
599
Benny Halevyf7da7a12011-05-19 14:16:47 -0400600 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400601 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
602
603 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
604 * num_fh (4) */
605 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
606 if (unlikely(!p))
607 goto out_err;
608
Andy Adamson16b374c2010-10-20 00:18:04 -0400609 memcpy(id, p, sizeof(*id));
610 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400611 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400612
613 nfl_util = be32_to_cpup(p++);
614 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
615 fl->commit_through_mds = 1;
616 if (nfl_util & NFL4_UFLG_DENSE)
617 fl->stripe_type = STRIPE_DENSE;
618 else
619 fl->stripe_type = STRIPE_SPARSE;
620 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
621
622 fl->first_stripe_index = be32_to_cpup(p++);
623 p = xdr_decode_hyper(p, &fl->pattern_offset);
624 fl->num_fh = be32_to_cpup(p++);
625
626 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
627 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
628 fl->pattern_offset);
629
Andy Adamsoncec765c2011-06-13 18:36:17 -0400630 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
631 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400632 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400633 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400634 goto out_err;
635
Andy Adamsoncec765c2011-06-13 18:36:17 -0400636 if (fl->num_fh > 0) {
637 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
638 gfp_flags);
639 if (!fl->fh_array)
640 goto out_err;
641 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400642
643 for (i = 0; i < fl->num_fh; i++) {
644 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400645 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400646 if (!fl->fh_array[i])
647 goto out_err_free;
648
649 p = xdr_inline_decode(&stream, 4);
650 if (unlikely(!p))
651 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400652 fl->fh_array[i]->size = be32_to_cpup(p++);
653 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500654 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400655 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400656 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400657 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400658
659 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
660 if (unlikely(!p))
661 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400662 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400663 dprintk("DEBUG: %s: fh len %d\n", __func__,
664 fl->fh_array[i]->size);
665 }
666
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400667 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400668 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400669
670out_err_free:
671 filelayout_free_fh_array(fl);
672out_err:
673 __free_page(scratch);
674 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400675}
676
Fred Isamanc8795132011-03-23 13:27:49 +0000677static void
678filelayout_free_lseg(struct pnfs_layout_segment *lseg)
679{
680 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
681
682 dprintk("--> %s\n", __func__);
683 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400684 /* This assumes a single RW lseg */
685 if (lseg->pls_range.iomode == IOMODE_RW) {
686 struct nfs4_filelayout *flo;
687
688 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
689 flo->commit_info.nbuckets = 0;
690 kfree(flo->commit_info.buckets);
691 flo->commit_info.buckets = NULL;
692 }
Fred Isamanc8795132011-03-23 13:27:49 +0000693 _filelayout_free_lseg(fl);
694}
695
Fred Isaman799ba8d2012-04-20 14:47:38 -0400696static int
697filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
698 gfp_t gfp_flags)
699{
700 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
701 struct nfs4_filelayout *flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
702
703 struct nfs4_fl_commit_bucket *buckets;
704 int size;
705
706 if (fl->commit_through_mds)
707 return 0;
708 if (flo->commit_info.nbuckets != 0) {
709 /* This assumes there is only one IOMODE_RW lseg. What
710 * we really want to do is have a layout_hdr level
711 * dictionary of <multipath_list4, fh> keys, each
712 * associated with a struct list_head, populated by calls
713 * to filelayout_write_pagelist().
714 * */
715 return 0;
716 }
717
718 size = (fl->stripe_type == STRIPE_SPARSE) ?
719 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
720
721 buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket),
722 gfp_flags);
723 if (!buckets)
724 return -ENOMEM;
725 else {
726 int i;
727
728 spin_lock(&lseg->pls_layout->plh_inode->i_lock);
729 if (flo->commit_info.nbuckets != 0)
730 kfree(buckets);
731 else {
732 flo->commit_info.buckets = buckets;
733 flo->commit_info.nbuckets = size;
734 for (i = 0; i < size; i++) {
735 INIT_LIST_HEAD(&buckets[i].written);
736 INIT_LIST_HEAD(&buckets[i].committing);
737 }
738 }
739 spin_unlock(&lseg->pls_layout->plh_inode->i_lock);
740 return 0;
741 }
742}
743
Andy Adamson16b374c2010-10-20 00:18:04 -0400744static struct pnfs_layout_segment *
745filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400746 struct nfs4_layoutget_res *lgr,
747 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400748{
749 struct nfs4_filelayout_segment *fl;
750 int rc;
751 struct nfs4_deviceid id;
752
753 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400754 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400755 if (!fl)
756 return NULL;
757
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400758 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
759 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400760 _filelayout_free_lseg(fl);
761 return NULL;
762 }
763 return &fl->generic_hdr;
764}
765
Fred Isaman94ad1c82011-03-01 01:34:14 +0000766/*
767 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
768 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300769 * return true : coalesce page
770 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000771 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400772static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000773filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
774 struct nfs_page *req)
775{
776 u64 p_stripe, r_stripe;
777 u32 stripe_unit;
778
Benny Halevy19345cb2011-06-19 18:33:46 -0400779 if (!pnfs_generic_pg_test(pgio, prev, req) ||
780 !nfs_generic_pg_test(pgio, prev, req))
781 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300782
Fred Isamanb5542842012-04-20 14:47:43 -0400783 p_stripe = (u64)req_offset(prev);
784 r_stripe = (u64)req_offset(req);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000785 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
786
787 do_div(p_stripe, stripe_unit);
788 do_div(r_stripe, stripe_unit);
789
790 return (p_stripe == r_stripe);
791}
792
Trond Myklebust17280172012-03-11 13:11:00 -0400793static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400794filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
795 struct nfs_page *req)
796{
797 BUG_ON(pgio->pg_lseg != NULL);
798
799 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
800 req->wb_context,
801 0,
802 NFS4_MAX_UINT64,
803 IOMODE_READ,
804 GFP_KERNEL);
805 /* If no lseg, fall back to read through mds */
806 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400807 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400808}
809
Trond Myklebust17280172012-03-11 13:11:00 -0400810static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400811filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
812 struct nfs_page *req)
813{
Fred Isaman799ba8d2012-04-20 14:47:38 -0400814 int status;
815
Andy Adamson7c24d942011-06-13 18:22:38 -0400816 BUG_ON(pgio->pg_lseg != NULL);
817
818 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
819 req->wb_context,
820 0,
821 NFS4_MAX_UINT64,
822 IOMODE_RW,
823 GFP_NOFS);
824 /* If no lseg, fall back to write through mds */
825 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400826 goto out_mds;
827 status = filelayout_alloc_commit_info(pgio->pg_lseg, GFP_NOFS);
828 if (status < 0) {
829 put_lseg(pgio->pg_lseg);
830 pgio->pg_lseg = NULL;
831 goto out_mds;
832 }
833 return;
834out_mds:
835 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400836}
837
Trond Myklebust1751c362011-06-10 13:30:23 -0400838static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400839 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400840 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400841 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400842};
843
844static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400845 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400846 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400847 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400848};
849
Fred Isamane0c2b382011-03-23 13:27:53 +0000850static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
851{
852 if (fl->stripe_type == STRIPE_SPARSE)
853 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
854 else
855 return j;
856}
857
Fred Isamand6d6dc72012-03-08 17:29:35 -0500858/* The generic layer is about to remove the req from the commit list.
859 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500860 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400861static void
862filelayout_clear_request_commit(struct nfs_page *req)
Fred Isamane0c2b382011-03-23 13:27:53 +0000863{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400864 struct pnfs_layout_segment *freeme = NULL;
865 struct inode *inode = req->wb_context->dentry->d_inode;
866
867 spin_lock(&inode->i_lock);
868 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
869 goto out;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500870 if (list_is_singular(&req->wb_list)) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400871 struct nfs4_fl_commit_bucket *bucket;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500872
Fred Isaman799ba8d2012-04-20 14:47:38 -0400873 bucket = list_first_entry(&req->wb_list,
874 struct nfs4_fl_commit_bucket,
875 written);
876 freeme = bucket->wlseg;
877 bucket->wlseg = NULL;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500878 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400879out:
880 nfs_request_remove_commit_list(req);
881 spin_unlock(&inode->i_lock);
882 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500883}
884
885static struct list_head *
886filelayout_choose_commit_list(struct nfs_page *req,
887 struct pnfs_layout_segment *lseg)
888{
Fred Isamane0c2b382011-03-23 13:27:53 +0000889 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
890 u32 i, j;
891 struct list_head *list;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400892 struct nfs4_fl_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +0000893
Fred Isamand6d6dc72012-03-08 17:29:35 -0500894 if (fl->commit_through_mds)
895 return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
896
Fred Isamane0c2b382011-03-23 13:27:53 +0000897 /* Note that we are calling nfs4_fl_calc_j_index on each page
898 * that ends up being committed to a data server. An attractive
899 * alternative is to add a field to nfs_write_data and nfs_page
900 * to store the value calculated in filelayout_write_pagelist
901 * and just use that here.
902 */
Fred Isamanb5542842012-04-20 14:47:43 -0400903 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
Fred Isamane0c2b382011-03-23 13:27:53 +0000904 i = select_bucket_index(fl, j);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400905 buckets = FILELAYOUT_FROM_HDR(lseg->pls_layout)->commit_info.buckets;
906 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +0000907 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500908 /* Non-empty buckets hold a reference on the lseg. That ref
909 * is normally transferred to the COMMIT call and released
910 * there. It could also be released if the last req is pulled
911 * off due to a rewrite, in which case it will be done in
Fred Isaman799ba8d2012-04-20 14:47:38 -0400912 * filelayout_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -0500913 */
Fred Isaman799ba8d2012-04-20 14:47:38 -0400914 buckets[i].wlseg = get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000915 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400916 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamane0c2b382011-03-23 13:27:53 +0000917 return list;
918}
919
Trond Myklebust8dd37752012-03-15 17:16:40 -0400920static void
921filelayout_mark_request_commit(struct nfs_page *req,
922 struct pnfs_layout_segment *lseg)
923{
924 struct list_head *list;
925
926 list = filelayout_choose_commit_list(req, lseg);
927 nfs_request_add_commit_list(req, list);
928}
929
Fred Isamane0c2b382011-03-23 13:27:53 +0000930static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
931{
932 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
933
934 if (flseg->stripe_type == STRIPE_SPARSE)
935 return i;
936 else
937 return nfs4_fl_calc_ds_index(lseg, i);
938}
939
940static struct nfs_fh *
941select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
942{
943 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
944
945 if (flseg->stripe_type == STRIPE_SPARSE) {
946 if (flseg->num_fh == 1)
947 i = 0;
948 else if (flseg->num_fh == 0)
949 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
950 return NULL;
951 }
952 return flseg->fh_array[i];
953}
954
Fred Isaman0b7c0152012-04-20 14:47:39 -0400955static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
Fred Isamane0c2b382011-03-23 13:27:53 +0000956{
957 struct pnfs_layout_segment *lseg = data->lseg;
958 struct nfs4_pnfs_ds *ds;
959 u32 idx;
960 struct nfs_fh *fh;
961
962 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
963 ds = nfs4_fl_prepare_ds(lseg, idx);
964 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500965 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
966 __func__);
Fred Isamane0c2b382011-03-23 13:27:53 +0000967 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
968 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
969 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400970 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000971 return -EAGAIN;
972 }
973 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400974 data->commit_done_cb = filelayout_commit_done_cb;
Fred Isamane0c2b382011-03-23 13:27:53 +0000975 data->ds_clp = ds->ds_clp;
976 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
977 if (fh)
978 data->args.fh = fh;
Fred Isaman0b7c0152012-04-20 14:47:39 -0400979 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
Fred Isamane0c2b382011-03-23 13:27:53 +0000980 &filelayout_commit_call_ops, how);
981}
982
Trond Myklebust8dd37752012-03-15 17:16:40 -0400983static int
Trond Myklebust3b3be882012-03-17 11:59:30 -0400984filelayout_scan_ds_commit_list(struct nfs4_fl_commit_bucket *bucket, int max,
985 spinlock_t *lock)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400986{
987 struct list_head *src = &bucket->written;
988 struct list_head *dst = &bucket->committing;
989 struct nfs_page *req, *tmp;
990 int ret = 0;
991
992 list_for_each_entry_safe(req, tmp, src, wb_list) {
993 if (!nfs_lock_request(req))
994 continue;
Trond Myklebust3b3be882012-03-17 11:59:30 -0400995 if (cond_resched_lock(lock))
996 list_safe_reset_next(req, tmp, wb_list);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400997 nfs_request_remove_commit_list(req);
998 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
999 nfs_list_add_request(req, dst);
1000 ret++;
1001 if (ret == max)
1002 break;
1003 }
Fred Isaman799ba8d2012-04-20 14:47:38 -04001004 if (ret) {
1005 bucket->clseg = bucket->wlseg;
1006 if (list_empty(src))
1007 bucket->wlseg = NULL;
1008 else
1009 get_lseg(bucket->clseg);
1010 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001011 return ret;
1012}
1013
Fred Isamand6d6dc72012-03-08 17:29:35 -05001014/* Move reqs from written to committing lists, returning count of number moved.
1015 * Note called with i_lock held.
1016 */
Trond Myklebust3b3be882012-03-17 11:59:30 -04001017static int filelayout_scan_commit_lists(struct inode *inode, int max,
1018 spinlock_t *lock)
Fred Isamand6d6dc72012-03-08 17:29:35 -05001019{
Fred Isaman799ba8d2012-04-20 14:47:38 -04001020 struct nfs4_fl_commit_info *fl_cinfo;
Fred Isamand6d6dc72012-03-08 17:29:35 -05001021 int i, rv = 0, cnt;
1022
Fred Isaman799ba8d2012-04-20 14:47:38 -04001023 fl_cinfo = &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info;
1024 if (fl_cinfo->nbuckets == 0)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001025 goto out_done;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001026 for (i = 0; i < fl_cinfo->nbuckets && max != 0; i++) {
1027 cnt = filelayout_scan_ds_commit_list(&fl_cinfo->buckets[i],
Trond Myklebust3b3be882012-03-17 11:59:30 -04001028 max, lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -05001029 max -= cnt;
1030 rv += cnt;
1031 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001032out_done:
Fred Isamand6d6dc72012-03-08 17:29:35 -05001033 return rv;
1034}
1035
Trond Myklebust9390f422012-03-16 13:52:45 -04001036static unsigned int
1037alloc_ds_commits(struct inode *inode, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +00001038{
Fred Isaman799ba8d2012-04-20 14:47:38 -04001039 struct nfs4_fl_commit_info *fl_cinfo;
1040 struct nfs4_fl_commit_bucket *bucket;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001041 struct nfs_commit_data *data;
Fred Isamane0c2b382011-03-23 13:27:53 +00001042 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -04001043 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001044
Fred Isaman799ba8d2012-04-20 14:47:38 -04001045 fl_cinfo = &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info;
1046 bucket = fl_cinfo->buckets;
1047 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1048 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001049 continue;
1050 data = nfs_commitdata_alloc();
1051 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001052 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001053 data->ds_commit_index = i;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001054 data->lseg = bucket->clseg;
1055 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001056 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001057 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001058 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001059
Trond Myklebust9390f422012-03-16 13:52:45 -04001060 /* Clean up on error */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001061 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1062 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001063 continue;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001064 nfs_retry_commit(&bucket->committing, bucket->clseg);
1065 put_lseg(bucket->clseg);
1066 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001067 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001068 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001069 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001070}
1071
1072/* This follows nfs_commit_list pretty closely */
1073static int
1074filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1075 int how)
1076{
Fred Isaman0b7c0152012-04-20 14:47:39 -04001077 struct nfs_commit_data *data, *tmp;
Fred Isamane0c2b382011-03-23 13:27:53 +00001078 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001079 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001080
1081 if (!list_empty(mds_pages)) {
1082 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001083 if (data != NULL) {
1084 data->lseg = NULL;
1085 list_add(&data->pages, &list);
1086 nreq++;
1087 } else
1088 nfs_retry_commit(mds_pages, NULL);
Fred Isamane0c2b382011-03-23 13:27:53 +00001089 }
1090
Trond Myklebust9390f422012-03-16 13:52:45 -04001091 nreq += alloc_ds_commits(inode, &list);
1092
1093 if (nreq == 0) {
1094 nfs_commit_clear_lock(NFS_I(inode));
1095 goto out;
1096 }
1097
1098 atomic_add(nreq, &NFS_I(inode)->commits_outstanding);
Fred Isamane0c2b382011-03-23 13:27:53 +00001099
1100 list_for_each_entry_safe(data, tmp, &list, pages) {
1101 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001102 if (!data->lseg) {
1103 nfs_init_commit(data, mds_pages, NULL);
Fred Isaman0b7c0152012-04-20 14:47:39 -04001104 nfs_initiate_commit(NFS_CLIENT(inode), data,
Fred Isamane0c2b382011-03-23 13:27:53 +00001105 data->mds_ops, how);
1106 } else {
Fred Isaman799ba8d2012-04-20 14:47:38 -04001107 struct nfs4_fl_commit_info *fl_cinfo;
1108
1109 fl_cinfo = &FILELAYOUT_FROM_HDR(data->lseg->pls_layout)->commit_info;
1110 nfs_init_commit(data, &fl_cinfo->buckets[data->ds_commit_index].committing, data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001111 filelayout_initiate_commit(data, how);
1112 }
1113 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001114out:
1115 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001116}
1117
Benny Halevy1775bc32011-05-20 13:47:33 +02001118static void
1119filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1120{
1121 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1122}
1123
Fred Isaman799ba8d2012-04-20 14:47:38 -04001124static struct pnfs_layout_hdr *
1125filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1126{
1127 struct nfs4_filelayout *flo;
1128
1129 flo = kzalloc(sizeof(*flo), gfp_flags);
1130 return &flo->generic_hdr;
1131}
1132
1133static void
1134filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1135{
1136 kfree(FILELAYOUT_FROM_HDR(lo));
1137}
1138
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001139static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001140 .id = LAYOUT_NFSV4_1_FILES,
1141 .name = "LAYOUT_NFSV4_1_FILES",
1142 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001143 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1144 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001145 .alloc_lseg = filelayout_alloc_lseg,
1146 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001147 .pg_read_ops = &filelayout_pg_read_ops,
1148 .pg_write_ops = &filelayout_pg_write_ops,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001149 .mark_request_commit = filelayout_mark_request_commit,
1150 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001151 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isamane0c2b382011-03-23 13:27:53 +00001152 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001153 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001154 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001155 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001156};
1157
1158static int __init nfs4filelayout_init(void)
1159{
1160 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1161 __func__);
1162 return pnfs_register_layoutdriver(&filelayout_type);
1163}
1164
1165static void __exit nfs4filelayout_exit(void)
1166{
1167 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1168 __func__);
1169 pnfs_unregister_layoutdriver(&filelayout_type);
1170}
1171
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001172MODULE_ALIAS("nfs-layouttype4-1");
1173
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001174module_init(nfs4filelayout_init);
1175module_exit(nfs4filelayout_exit);