blob: 15aeba20d57dd73027ba8003f6d10857373b14c1 [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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-03-01 01:34:20 +000093 if (task->tk_status >= 0)
94 return 0;
Andy Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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 Adamsoncbdabc7f2011-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);
159 if (reset) {
Peng Tao1b0ae062011-09-22 21:50:12 -0400160 pnfs_set_lo_fail(data->lseg);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000161 nfs4_reset_read(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000162 }
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700163 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000164 return -EAGAIN;
165 }
166
167 return 0;
168}
169
Andy Adamson16b374c2010-10-20 00:18:04 -0400170/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000171 * We reference the rpc_cred of the first WRITE that triggers the need for
172 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
173 * rfc5661 is not clear about which credential should be used.
174 */
175static void
176filelayout_set_layoutcommit(struct nfs_write_data *wdata)
177{
178 if (FILELAYOUT_LSEG(wdata->lseg)->commit_through_mds ||
179 wdata->res.verf->committed == NFS_FILE_SYNC)
180 return;
181
182 pnfs_set_layoutcommit(wdata);
183 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, wdata->inode->i_ino,
Peng Taoacff58802011-07-30 20:52:31 -0400184 (unsigned long) NFS_I(wdata->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{
194 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
195
Andy Adamsoncbdabc7f2011-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{
208 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
209
210 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
211
212 /* Note this may cause RPC to be resent */
213 rdata->mds_ops->rpc_call_done(task, data);
214}
215
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500216static void filelayout_read_count_stats(struct rpc_task *task, void *data)
217{
218 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
219
220 rpc_count_iostats(task, NFS_SERVER(rdata->inode)->client->cl_metrics);
221}
222
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000223static void filelayout_read_release(void *data)
224{
225 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
226
Trond Myklebust8dd37752012-03-15 17:16:40 -0400227 put_lseg(rdata->lseg);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000228 rdata->mds_ops->rpc_release(data);
229}
230
Fred Isamana69aef12011-03-03 15:13:47 +0000231static int filelayout_write_done_cb(struct rpc_task *task,
232 struct nfs_write_data *data)
233{
234 int reset = 0;
235
236 if (filelayout_async_handle_error(task, data->args.context->state,
237 data->ds_clp, &reset) == -EAGAIN) {
Fred Isamana69aef12011-03-03 15:13:47 +0000238 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
239 __func__, data->ds_clp, data->ds_clp->cl_session);
240 if (reset) {
Peng Tao1b0ae062011-09-22 21:50:12 -0400241 pnfs_set_lo_fail(data->lseg);
Fred Isamana69aef12011-03-03 15:13:47 +0000242 nfs4_reset_write(task, data);
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700243 }
244 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000245 return -EAGAIN;
246 }
247
Andy Adamson863a3c62011-03-23 13:27:54 +0000248 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000249 return 0;
250}
251
Fred Isamane0c2b382011-03-23 13:27:53 +0000252/* Fake up some data that will cause nfs_commit_release to retry the writes. */
253static void prepare_to_resend_writes(struct nfs_write_data *data)
254{
255 struct nfs_page *first = nfs_list_entry(data->pages.next);
256
257 data->task.tk_status = 0;
258 memcpy(data->verf.verifier, first->wb_verf.verifier,
259 sizeof(first->wb_verf.verifier));
260 data->verf.verifier[0]++; /* ensure verifier mismatch */
261}
262
263static int filelayout_commit_done_cb(struct rpc_task *task,
264 struct nfs_write_data *data)
265{
266 int reset = 0;
267
268 if (filelayout_async_handle_error(task, data->args.context->state,
269 data->ds_clp, &reset) == -EAGAIN) {
270 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
271 __func__, data->ds_clp, data->ds_clp->cl_session);
272 if (reset) {
273 prepare_to_resend_writes(data);
Peng Tao1b0ae062011-09-22 21:50:12 -0400274 pnfs_set_lo_fail(data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000275 } else
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700276 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000277 return -EAGAIN;
278 }
279
280 return 0;
281}
282
Fred Isamana69aef12011-03-03 15:13:47 +0000283static void filelayout_write_prepare(struct rpc_task *task, void *data)
284{
285 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
286
287 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
288 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500289 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000290 return;
291
292 rpc_call_start(task);
293}
294
295static void filelayout_write_call_done(struct rpc_task *task, void *data)
296{
297 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
298
299 /* Note this may cause RPC to be resent */
300 wdata->mds_ops->rpc_call_done(task, data);
301}
302
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500303static void filelayout_write_count_stats(struct rpc_task *task, void *data)
304{
305 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
306
307 rpc_count_iostats(task, NFS_SERVER(wdata->inode)->client->cl_metrics);
308}
309
Fred Isamana69aef12011-03-03 15:13:47 +0000310static void filelayout_write_release(void *data)
311{
312 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
313
Trond Myklebust8dd37752012-03-15 17:16:40 -0400314 put_lseg(wdata->lseg);
Fred Isamana69aef12011-03-03 15:13:47 +0000315 wdata->mds_ops->rpc_release(data);
316}
317
Fred Isamane0c2b382011-03-23 13:27:53 +0000318static void filelayout_commit_release(void *data)
319{
320 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
321
322 nfs_commit_release_pages(wdata);
323 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
324 nfs_commit_clear_lock(NFS_I(wdata->inode));
Trond Myklebust8dd37752012-03-15 17:16:40 -0400325 put_lseg(wdata->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000326 nfs_commitdata_release(wdata);
327}
328
Trond Myklebust17280172012-03-11 13:11:00 -0400329static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000330 .rpc_call_prepare = filelayout_read_prepare,
331 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500332 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000333 .rpc_release = filelayout_read_release,
334};
335
Trond Myklebust17280172012-03-11 13:11:00 -0400336static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000337 .rpc_call_prepare = filelayout_write_prepare,
338 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500339 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000340 .rpc_release = filelayout_write_release,
341};
342
Trond Myklebust17280172012-03-11 13:11:00 -0400343static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isamane0c2b382011-03-23 13:27:53 +0000344 .rpc_call_prepare = filelayout_write_prepare,
345 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500346 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000347 .rpc_release = filelayout_commit_release,
348};
349
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000350static enum pnfs_try_status
351filelayout_read_pagelist(struct nfs_read_data *data)
352{
353 struct pnfs_layout_segment *lseg = data->lseg;
354 struct nfs4_pnfs_ds *ds;
355 loff_t offset = data->args.offset;
356 u32 j, idx;
357 struct nfs_fh *fh;
358 int status;
359
360 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
361 __func__, data->inode->i_ino,
362 data->args.pgbase, (size_t)data->args.count, offset);
363
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400364 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
365 return PNFS_NOT_ATTEMPTED;
366
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000367 /* Retrieve the correct rpc_client for the byte range */
368 j = nfs4_fl_calc_j_index(lseg, offset);
369 idx = nfs4_fl_calc_ds_index(lseg, j);
370 ds = nfs4_fl_prepare_ds(lseg, idx);
371 if (!ds) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000372 /* Either layout fh index faulty, or ds connect failed */
373 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
374 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000375 return PNFS_NOT_ATTEMPTED;
376 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400377 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000378
379 /* No multipath support. Use first DS */
380 data->ds_clp = ds->ds_clp;
381 fh = nfs4_fl_select_ds_fh(lseg, j);
382 if (fh)
383 data->args.fh = fh;
384
385 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
386 data->mds_offset = offset;
387
388 /* Perform an asynchronous read to ds */
389 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
390 &filelayout_read_call_ops);
391 BUG_ON(status != 0);
392 return PNFS_ATTEMPTED;
393}
394
Fred Isamana69aef12011-03-03 15:13:47 +0000395/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000396static enum pnfs_try_status
397filelayout_write_pagelist(struct nfs_write_data *data, int sync)
398{
Fred Isamana69aef12011-03-03 15:13:47 +0000399 struct pnfs_layout_segment *lseg = data->lseg;
400 struct nfs4_pnfs_ds *ds;
401 loff_t offset = data->args.offset;
402 u32 j, idx;
403 struct nfs_fh *fh;
404 int status;
405
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400406 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
407 return PNFS_NOT_ATTEMPTED;
408
Fred Isamana69aef12011-03-03 15:13:47 +0000409 /* Retrieve the correct rpc_client for the byte range */
410 j = nfs4_fl_calc_j_index(lseg, offset);
411 idx = nfs4_fl_calc_ds_index(lseg, j);
412 ds = nfs4_fl_prepare_ds(lseg, idx);
413 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500414 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
415 __func__);
Fred Isamana69aef12011-03-03 15:13:47 +0000416 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
417 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
418 return PNFS_NOT_ATTEMPTED;
419 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400420 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
Fred Isamana69aef12011-03-03 15:13:47 +0000421 data->inode->i_ino, sync, (size_t) data->args.count, offset,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400422 ds->ds_remotestr);
Fred Isamana69aef12011-03-03 15:13:47 +0000423
Fred Isamana69aef12011-03-03 15:13:47 +0000424 data->write_done_cb = filelayout_write_done_cb;
425 data->ds_clp = ds->ds_clp;
426 fh = nfs4_fl_select_ds_fh(lseg, j);
427 if (fh)
428 data->args.fh = fh;
429 /*
430 * Get the file offset on the dserver. Set the write offset to
431 * this offset and save the original offset.
432 */
433 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000434
435 /* Perform an asynchronous write */
436 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
437 &filelayout_write_call_ops, sync);
438 BUG_ON(status != 0);
439 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000440}
441
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000442/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400443 * filelayout_check_layout()
444 *
445 * Make sure layout segment parameters are sane WRT the device.
446 * At this point no generic layer initialization of the lseg has occurred,
447 * and nothing has been added to the layout_hdr cache.
448 *
449 */
450static int
451filelayout_check_layout(struct pnfs_layout_hdr *lo,
452 struct nfs4_filelayout_segment *fl,
453 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400454 struct nfs4_deviceid *id,
455 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400456{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400457 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400458 struct nfs4_file_layout_dsaddr *dsaddr;
459 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000460 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400461
462 dprintk("--> %s\n", __func__);
463
Andy Adamson7c24d942011-06-13 18:22:38 -0400464 /* FIXME: remove this check when layout segment support is added */
465 if (lgr->range.offset != 0 ||
466 lgr->range.length != NFS4_MAX_UINT64) {
467 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
468 __func__);
469 goto out;
470 }
471
Andy Adamson16b374c2010-10-20 00:18:04 -0400472 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500473 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400474 __func__, fl->pattern_offset);
475 goto out;
476 }
477
Benny Halevy75247af2011-02-22 15:56:01 -0800478 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
479 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400480 __func__, fl->stripe_unit);
481 goto out;
482 }
483
484 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300485 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
486 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400487 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400488 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400489 if (dsaddr == NULL)
490 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400491 } else
492 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400493 /* Found deviceid is being reaped */
494 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
495 goto out_put;
496
Andy Adamson16b374c2010-10-20 00:18:04 -0400497 fl->dsaddr = dsaddr;
498
Chuck Levere4149662011-10-25 12:18:03 -0400499 if (fl->first_stripe_index >= dsaddr->stripe_count) {
500 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400501 __func__, fl->first_stripe_index);
502 goto out_put;
503 }
504
505 if ((fl->stripe_type == STRIPE_SPARSE &&
506 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
507 (fl->stripe_type == STRIPE_DENSE &&
508 fl->num_fh != dsaddr->stripe_count)) {
509 dprintk("%s num_fh %u not valid for given packing\n",
510 __func__, fl->num_fh);
511 goto out_put;
512 }
513
514 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
515 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
516 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
517 nfss->wsize);
518 }
519
520 status = 0;
521out:
522 dprintk("--> %s returns %d\n", __func__, status);
523 return status;
524out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000525 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400526 goto out;
527}
528
529static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
530{
531 int i;
532
533 for (i = 0; i < fl->num_fh; i++) {
534 if (!fl->fh_array[i])
535 break;
536 kfree(fl->fh_array[i]);
537 }
538 kfree(fl->fh_array);
539 fl->fh_array = NULL;
540}
541
542static void
543_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
544{
545 filelayout_free_fh_array(fl);
546 kfree(fl);
547}
548
549static int
550filelayout_decode_layout(struct pnfs_layout_hdr *flo,
551 struct nfs4_filelayout_segment *fl,
552 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400553 struct nfs4_deviceid *id,
554 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400555{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400556 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400557 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400558 struct page *scratch;
559 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400560 uint32_t nfl_util;
561 int i;
562
563 dprintk("%s: set_layout_map Begin\n", __func__);
564
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400565 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400566 if (!scratch)
567 return -ENOMEM;
568
Benny Halevyf7da7a12011-05-19 14:16:47 -0400569 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400570 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
571
572 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
573 * num_fh (4) */
574 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
575 if (unlikely(!p))
576 goto out_err;
577
Andy Adamson16b374c2010-10-20 00:18:04 -0400578 memcpy(id, p, sizeof(*id));
579 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400580 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400581
582 nfl_util = be32_to_cpup(p++);
583 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
584 fl->commit_through_mds = 1;
585 if (nfl_util & NFL4_UFLG_DENSE)
586 fl->stripe_type = STRIPE_DENSE;
587 else
588 fl->stripe_type = STRIPE_SPARSE;
589 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
590
591 fl->first_stripe_index = be32_to_cpup(p++);
592 p = xdr_decode_hyper(p, &fl->pattern_offset);
593 fl->num_fh = be32_to_cpup(p++);
594
595 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
596 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
597 fl->pattern_offset);
598
Andy Adamsoncec765c2011-06-13 18:36:17 -0400599 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
600 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400601 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400602 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400603 goto out_err;
604
Andy Adamsoncec765c2011-06-13 18:36:17 -0400605 if (fl->num_fh > 0) {
606 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
607 gfp_flags);
608 if (!fl->fh_array)
609 goto out_err;
610 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400611
612 for (i = 0; i < fl->num_fh; i++) {
613 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400614 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400615 if (!fl->fh_array[i])
616 goto out_err_free;
617
618 p = xdr_inline_decode(&stream, 4);
619 if (unlikely(!p))
620 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400621 fl->fh_array[i]->size = be32_to_cpup(p++);
622 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500623 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400624 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400625 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400626 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400627
628 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
629 if (unlikely(!p))
630 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400631 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400632 dprintk("DEBUG: %s: fh len %d\n", __func__,
633 fl->fh_array[i]->size);
634 }
635
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400636 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400637 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400638
639out_err_free:
640 filelayout_free_fh_array(fl);
641out_err:
642 __free_page(scratch);
643 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400644}
645
Fred Isamanc8795132011-03-23 13:27:49 +0000646static void
647filelayout_free_lseg(struct pnfs_layout_segment *lseg)
648{
649 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
650
651 dprintk("--> %s\n", __func__);
652 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400653 /* This assumes a single RW lseg */
654 if (lseg->pls_range.iomode == IOMODE_RW) {
655 struct nfs4_filelayout *flo;
656
657 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
658 flo->commit_info.nbuckets = 0;
659 kfree(flo->commit_info.buckets);
660 flo->commit_info.buckets = NULL;
661 }
Fred Isamanc8795132011-03-23 13:27:49 +0000662 _filelayout_free_lseg(fl);
663}
664
Fred Isaman799ba8d2012-04-20 14:47:38 -0400665static int
666filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
667 gfp_t gfp_flags)
668{
669 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
670 struct nfs4_filelayout *flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
671
672 struct nfs4_fl_commit_bucket *buckets;
673 int size;
674
675 if (fl->commit_through_mds)
676 return 0;
677 if (flo->commit_info.nbuckets != 0) {
678 /* This assumes there is only one IOMODE_RW lseg. What
679 * we really want to do is have a layout_hdr level
680 * dictionary of <multipath_list4, fh> keys, each
681 * associated with a struct list_head, populated by calls
682 * to filelayout_write_pagelist().
683 * */
684 return 0;
685 }
686
687 size = (fl->stripe_type == STRIPE_SPARSE) ?
688 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
689
690 buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket),
691 gfp_flags);
692 if (!buckets)
693 return -ENOMEM;
694 else {
695 int i;
696
697 spin_lock(&lseg->pls_layout->plh_inode->i_lock);
698 if (flo->commit_info.nbuckets != 0)
699 kfree(buckets);
700 else {
701 flo->commit_info.buckets = buckets;
702 flo->commit_info.nbuckets = size;
703 for (i = 0; i < size; i++) {
704 INIT_LIST_HEAD(&buckets[i].written);
705 INIT_LIST_HEAD(&buckets[i].committing);
706 }
707 }
708 spin_unlock(&lseg->pls_layout->plh_inode->i_lock);
709 return 0;
710 }
711}
712
Andy Adamson16b374c2010-10-20 00:18:04 -0400713static struct pnfs_layout_segment *
714filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400715 struct nfs4_layoutget_res *lgr,
716 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400717{
718 struct nfs4_filelayout_segment *fl;
719 int rc;
720 struct nfs4_deviceid id;
721
722 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400723 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400724 if (!fl)
725 return NULL;
726
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400727 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
728 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400729 _filelayout_free_lseg(fl);
730 return NULL;
731 }
732 return &fl->generic_hdr;
733}
734
Fred Isaman94ad1c82011-03-01 01:34:14 +0000735/*
736 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
737 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300738 * return true : coalesce page
739 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000740 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400741static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000742filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
743 struct nfs_page *req)
744{
745 u64 p_stripe, r_stripe;
746 u32 stripe_unit;
747
Benny Halevy19345cb2011-06-19 18:33:46 -0400748 if (!pnfs_generic_pg_test(pgio, prev, req) ||
749 !nfs_generic_pg_test(pgio, prev, req))
750 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300751
Fred Isaman94ad1c82011-03-01 01:34:14 +0000752 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
753 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
754 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
755
756 do_div(p_stripe, stripe_unit);
757 do_div(r_stripe, stripe_unit);
758
759 return (p_stripe == r_stripe);
760}
761
Trond Myklebust17280172012-03-11 13:11:00 -0400762static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400763filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
764 struct nfs_page *req)
765{
766 BUG_ON(pgio->pg_lseg != NULL);
767
768 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
769 req->wb_context,
770 0,
771 NFS4_MAX_UINT64,
772 IOMODE_READ,
773 GFP_KERNEL);
774 /* If no lseg, fall back to read through mds */
775 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400776 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400777}
778
Trond Myklebust17280172012-03-11 13:11:00 -0400779static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400780filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
781 struct nfs_page *req)
782{
Fred Isaman799ba8d2012-04-20 14:47:38 -0400783 int status;
784
Andy Adamson7c24d942011-06-13 18:22:38 -0400785 BUG_ON(pgio->pg_lseg != NULL);
786
787 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
788 req->wb_context,
789 0,
790 NFS4_MAX_UINT64,
791 IOMODE_RW,
792 GFP_NOFS);
793 /* If no lseg, fall back to write through mds */
794 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400795 goto out_mds;
796 status = filelayout_alloc_commit_info(pgio->pg_lseg, GFP_NOFS);
797 if (status < 0) {
798 put_lseg(pgio->pg_lseg);
799 pgio->pg_lseg = NULL;
800 goto out_mds;
801 }
802 return;
803out_mds:
804 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400805}
806
Trond Myklebust1751c362011-06-10 13:30:23 -0400807static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400808 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400809 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400810 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400811};
812
813static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400814 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400815 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400816 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400817};
818
Fred Isamane0c2b382011-03-23 13:27:53 +0000819static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
820{
821 if (fl->stripe_type == STRIPE_SPARSE)
822 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
823 else
824 return j;
825}
826
Fred Isamand6d6dc72012-03-08 17:29:35 -0500827/* The generic layer is about to remove the req from the commit list.
828 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500829 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400830static void
831filelayout_clear_request_commit(struct nfs_page *req)
Fred Isamane0c2b382011-03-23 13:27:53 +0000832{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400833 struct pnfs_layout_segment *freeme = NULL;
834 struct inode *inode = req->wb_context->dentry->d_inode;
835
836 spin_lock(&inode->i_lock);
837 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
838 goto out;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500839 if (list_is_singular(&req->wb_list)) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400840 struct nfs4_fl_commit_bucket *bucket;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500841
Fred Isaman799ba8d2012-04-20 14:47:38 -0400842 bucket = list_first_entry(&req->wb_list,
843 struct nfs4_fl_commit_bucket,
844 written);
845 freeme = bucket->wlseg;
846 bucket->wlseg = NULL;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500847 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400848out:
849 nfs_request_remove_commit_list(req);
850 spin_unlock(&inode->i_lock);
851 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500852}
853
854static struct list_head *
855filelayout_choose_commit_list(struct nfs_page *req,
856 struct pnfs_layout_segment *lseg)
857{
Fred Isamane0c2b382011-03-23 13:27:53 +0000858 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
859 u32 i, j;
860 struct list_head *list;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400861 struct nfs4_fl_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +0000862
Fred Isamand6d6dc72012-03-08 17:29:35 -0500863 if (fl->commit_through_mds)
864 return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
865
Fred Isamane0c2b382011-03-23 13:27:53 +0000866 /* Note that we are calling nfs4_fl_calc_j_index on each page
867 * that ends up being committed to a data server. An attractive
868 * alternative is to add a field to nfs_write_data and nfs_page
869 * to store the value calculated in filelayout_write_pagelist
870 * and just use that here.
871 */
872 j = nfs4_fl_calc_j_index(lseg,
873 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
874 i = select_bucket_index(fl, j);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400875 buckets = FILELAYOUT_FROM_HDR(lseg->pls_layout)->commit_info.buckets;
876 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +0000877 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500878 /* Non-empty buckets hold a reference on the lseg. That ref
879 * is normally transferred to the COMMIT call and released
880 * there. It could also be released if the last req is pulled
881 * off due to a rewrite, in which case it will be done in
Fred Isaman799ba8d2012-04-20 14:47:38 -0400882 * filelayout_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -0500883 */
Fred Isaman799ba8d2012-04-20 14:47:38 -0400884 buckets[i].wlseg = get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000885 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400886 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamane0c2b382011-03-23 13:27:53 +0000887 return list;
888}
889
Trond Myklebust8dd37752012-03-15 17:16:40 -0400890static void
891filelayout_mark_request_commit(struct nfs_page *req,
892 struct pnfs_layout_segment *lseg)
893{
894 struct list_head *list;
895
896 list = filelayout_choose_commit_list(req, lseg);
897 nfs_request_add_commit_list(req, list);
898}
899
Fred Isamane0c2b382011-03-23 13:27:53 +0000900static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
901{
902 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
903
904 if (flseg->stripe_type == STRIPE_SPARSE)
905 return i;
906 else
907 return nfs4_fl_calc_ds_index(lseg, i);
908}
909
910static struct nfs_fh *
911select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
912{
913 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
914
915 if (flseg->stripe_type == STRIPE_SPARSE) {
916 if (flseg->num_fh == 1)
917 i = 0;
918 else if (flseg->num_fh == 0)
919 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
920 return NULL;
921 }
922 return flseg->fh_array[i];
923}
924
925static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
926{
927 struct pnfs_layout_segment *lseg = data->lseg;
928 struct nfs4_pnfs_ds *ds;
929 u32 idx;
930 struct nfs_fh *fh;
931
932 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
933 ds = nfs4_fl_prepare_ds(lseg, idx);
934 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500935 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
936 __func__);
Fred Isamane0c2b382011-03-23 13:27:53 +0000937 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
938 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
939 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400940 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000941 return -EAGAIN;
942 }
943 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
944 data->write_done_cb = filelayout_commit_done_cb;
945 data->ds_clp = ds->ds_clp;
946 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
947 if (fh)
948 data->args.fh = fh;
949 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
950 &filelayout_commit_call_ops, how);
951}
952
Trond Myklebust8dd37752012-03-15 17:16:40 -0400953static int
Trond Myklebust3b3be882012-03-17 11:59:30 -0400954filelayout_scan_ds_commit_list(struct nfs4_fl_commit_bucket *bucket, int max,
955 spinlock_t *lock)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400956{
957 struct list_head *src = &bucket->written;
958 struct list_head *dst = &bucket->committing;
959 struct nfs_page *req, *tmp;
960 int ret = 0;
961
962 list_for_each_entry_safe(req, tmp, src, wb_list) {
963 if (!nfs_lock_request(req))
964 continue;
Trond Myklebust3b3be882012-03-17 11:59:30 -0400965 if (cond_resched_lock(lock))
966 list_safe_reset_next(req, tmp, wb_list);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400967 nfs_request_remove_commit_list(req);
968 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
969 nfs_list_add_request(req, dst);
970 ret++;
971 if (ret == max)
972 break;
973 }
Fred Isaman799ba8d2012-04-20 14:47:38 -0400974 if (ret) {
975 bucket->clseg = bucket->wlseg;
976 if (list_empty(src))
977 bucket->wlseg = NULL;
978 else
979 get_lseg(bucket->clseg);
980 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400981 return ret;
982}
983
Fred Isamand6d6dc72012-03-08 17:29:35 -0500984/* Move reqs from written to committing lists, returning count of number moved.
985 * Note called with i_lock held.
986 */
Trond Myklebust3b3be882012-03-17 11:59:30 -0400987static int filelayout_scan_commit_lists(struct inode *inode, int max,
988 spinlock_t *lock)
Fred Isamand6d6dc72012-03-08 17:29:35 -0500989{
Fred Isaman799ba8d2012-04-20 14:47:38 -0400990 struct nfs4_fl_commit_info *fl_cinfo;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500991 int i, rv = 0, cnt;
992
Fred Isaman799ba8d2012-04-20 14:47:38 -0400993 fl_cinfo = &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info;
994 if (fl_cinfo->nbuckets == 0)
Trond Myklebust8dd37752012-03-15 17:16:40 -0400995 goto out_done;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400996 for (i = 0; i < fl_cinfo->nbuckets && max != 0; i++) {
997 cnt = filelayout_scan_ds_commit_list(&fl_cinfo->buckets[i],
Trond Myklebust3b3be882012-03-17 11:59:30 -0400998 max, lock);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500999 max -= cnt;
1000 rv += cnt;
1001 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001002out_done:
Fred Isamand6d6dc72012-03-08 17:29:35 -05001003 return rv;
1004}
1005
Trond Myklebust9390f422012-03-16 13:52:45 -04001006static unsigned int
1007alloc_ds_commits(struct inode *inode, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +00001008{
Fred Isaman799ba8d2012-04-20 14:47:38 -04001009 struct nfs4_fl_commit_info *fl_cinfo;
1010 struct nfs4_fl_commit_bucket *bucket;
Fred Isamane0c2b382011-03-23 13:27:53 +00001011 struct nfs_write_data *data;
1012 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -04001013 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001014
Fred Isaman799ba8d2012-04-20 14:47:38 -04001015 fl_cinfo = &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info;
1016 bucket = fl_cinfo->buckets;
1017 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1018 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001019 continue;
1020 data = nfs_commitdata_alloc();
1021 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001022 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001023 data->ds_commit_index = i;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001024 data->lseg = bucket->clseg;
1025 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001026 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001027 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001028 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001029
Trond Myklebust9390f422012-03-16 13:52:45 -04001030 /* Clean up on error */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001031 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1032 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001033 continue;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001034 nfs_retry_commit(&bucket->committing, bucket->clseg);
1035 put_lseg(bucket->clseg);
1036 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001037 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001038 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001039 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001040}
1041
1042/* This follows nfs_commit_list pretty closely */
1043static int
1044filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1045 int how)
1046{
1047 struct nfs_write_data *data, *tmp;
1048 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001049 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001050
1051 if (!list_empty(mds_pages)) {
1052 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001053 if (data != NULL) {
1054 data->lseg = NULL;
1055 list_add(&data->pages, &list);
1056 nreq++;
1057 } else
1058 nfs_retry_commit(mds_pages, NULL);
Fred Isamane0c2b382011-03-23 13:27:53 +00001059 }
1060
Trond Myklebust9390f422012-03-16 13:52:45 -04001061 nreq += alloc_ds_commits(inode, &list);
1062
1063 if (nreq == 0) {
1064 nfs_commit_clear_lock(NFS_I(inode));
1065 goto out;
1066 }
1067
1068 atomic_add(nreq, &NFS_I(inode)->commits_outstanding);
Fred Isamane0c2b382011-03-23 13:27:53 +00001069
1070 list_for_each_entry_safe(data, tmp, &list, pages) {
1071 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001072 if (!data->lseg) {
1073 nfs_init_commit(data, mds_pages, NULL);
1074 nfs_initiate_commit(data, NFS_CLIENT(inode),
1075 data->mds_ops, how);
1076 } else {
Fred Isaman799ba8d2012-04-20 14:47:38 -04001077 struct nfs4_fl_commit_info *fl_cinfo;
1078
1079 fl_cinfo = &FILELAYOUT_FROM_HDR(data->lseg->pls_layout)->commit_info;
1080 nfs_init_commit(data, &fl_cinfo->buckets[data->ds_commit_index].committing, data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001081 filelayout_initiate_commit(data, how);
1082 }
1083 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001084out:
1085 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001086}
1087
Benny Halevy1775bc32011-05-20 13:47:33 +02001088static void
1089filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1090{
1091 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1092}
1093
Fred Isaman799ba8d2012-04-20 14:47:38 -04001094static struct pnfs_layout_hdr *
1095filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1096{
1097 struct nfs4_filelayout *flo;
1098
1099 flo = kzalloc(sizeof(*flo), gfp_flags);
1100 return &flo->generic_hdr;
1101}
1102
1103static void
1104filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1105{
1106 kfree(FILELAYOUT_FROM_HDR(lo));
1107}
1108
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001109static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001110 .id = LAYOUT_NFSV4_1_FILES,
1111 .name = "LAYOUT_NFSV4_1_FILES",
1112 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001113 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1114 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001115 .alloc_lseg = filelayout_alloc_lseg,
1116 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001117 .pg_read_ops = &filelayout_pg_read_ops,
1118 .pg_write_ops = &filelayout_pg_write_ops,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001119 .mark_request_commit = filelayout_mark_request_commit,
1120 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001121 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isamane0c2b382011-03-23 13:27:53 +00001122 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001123 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001124 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001125 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001126};
1127
1128static int __init nfs4filelayout_init(void)
1129{
1130 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1131 __func__);
1132 return pnfs_register_layoutdriver(&filelayout_type);
1133}
1134
1135static void __exit nfs4filelayout_exit(void)
1136{
1137 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1138 __func__);
1139 pnfs_unregister_layoutdriver(&filelayout_type);
1140}
1141
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001142MODULE_ALIAS("nfs-layouttype4-1");
1143
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001144module_init(nfs4filelayout_init);
1145module_exit(nfs4filelayout_exit);