blob: 716fac6bc082c7860f7caaded65fb2dc836811f4 [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
227 rdata->mds_ops->rpc_release(data);
228}
229
Fred Isamana69aef12011-03-03 15:13:47 +0000230static int filelayout_write_done_cb(struct rpc_task *task,
231 struct nfs_write_data *data)
232{
233 int reset = 0;
234
235 if (filelayout_async_handle_error(task, data->args.context->state,
236 data->ds_clp, &reset) == -EAGAIN) {
Fred Isamana69aef12011-03-03 15:13:47 +0000237 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
238 __func__, data->ds_clp, data->ds_clp->cl_session);
239 if (reset) {
Peng Tao1b0ae062011-09-22 21:50:12 -0400240 pnfs_set_lo_fail(data->lseg);
Fred Isamana69aef12011-03-03 15:13:47 +0000241 nfs4_reset_write(task, data);
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700242 }
243 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000244 return -EAGAIN;
245 }
246
Andy Adamson863a3c62011-03-23 13:27:54 +0000247 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000248 return 0;
249}
250
Fred Isamane0c2b382011-03-23 13:27:53 +0000251/* Fake up some data that will cause nfs_commit_release to retry the writes. */
252static void prepare_to_resend_writes(struct nfs_write_data *data)
253{
254 struct nfs_page *first = nfs_list_entry(data->pages.next);
255
256 data->task.tk_status = 0;
257 memcpy(data->verf.verifier, first->wb_verf.verifier,
258 sizeof(first->wb_verf.verifier));
259 data->verf.verifier[0]++; /* ensure verifier mismatch */
260}
261
262static int filelayout_commit_done_cb(struct rpc_task *task,
263 struct nfs_write_data *data)
264{
265 int reset = 0;
266
267 if (filelayout_async_handle_error(task, data->args.context->state,
268 data->ds_clp, &reset) == -EAGAIN) {
269 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
270 __func__, data->ds_clp, data->ds_clp->cl_session);
271 if (reset) {
272 prepare_to_resend_writes(data);
Peng Tao1b0ae062011-09-22 21:50:12 -0400273 pnfs_set_lo_fail(data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000274 } else
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700275 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000276 return -EAGAIN;
277 }
278
279 return 0;
280}
281
Fred Isamana69aef12011-03-03 15:13:47 +0000282static void filelayout_write_prepare(struct rpc_task *task, void *data)
283{
284 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
285
286 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
287 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500288 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000289 return;
290
291 rpc_call_start(task);
292}
293
294static void filelayout_write_call_done(struct rpc_task *task, void *data)
295{
296 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
297
298 /* Note this may cause RPC to be resent */
299 wdata->mds_ops->rpc_call_done(task, data);
300}
301
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500302static void filelayout_write_count_stats(struct rpc_task *task, void *data)
303{
304 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
305
306 rpc_count_iostats(task, NFS_SERVER(wdata->inode)->client->cl_metrics);
307}
308
Fred Isamana69aef12011-03-03 15:13:47 +0000309static void filelayout_write_release(void *data)
310{
311 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
312
313 wdata->mds_ops->rpc_release(data);
314}
315
Fred Isamane0c2b382011-03-23 13:27:53 +0000316static void filelayout_commit_release(void *data)
317{
318 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
319
320 nfs_commit_release_pages(wdata);
321 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
322 nfs_commit_clear_lock(NFS_I(wdata->inode));
323 nfs_commitdata_release(wdata);
324}
325
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000326struct rpc_call_ops filelayout_read_call_ops = {
327 .rpc_call_prepare = filelayout_read_prepare,
328 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500329 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000330 .rpc_release = filelayout_read_release,
331};
332
Fred Isamana69aef12011-03-03 15:13:47 +0000333struct rpc_call_ops filelayout_write_call_ops = {
334 .rpc_call_prepare = filelayout_write_prepare,
335 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500336 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000337 .rpc_release = filelayout_write_release,
338};
339
Fred Isamane0c2b382011-03-23 13:27:53 +0000340struct rpc_call_ops filelayout_commit_call_ops = {
341 .rpc_call_prepare = filelayout_write_prepare,
342 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500343 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000344 .rpc_release = filelayout_commit_release,
345};
346
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000347static enum pnfs_try_status
348filelayout_read_pagelist(struct nfs_read_data *data)
349{
350 struct pnfs_layout_segment *lseg = data->lseg;
351 struct nfs4_pnfs_ds *ds;
352 loff_t offset = data->args.offset;
353 u32 j, idx;
354 struct nfs_fh *fh;
355 int status;
356
357 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
358 __func__, data->inode->i_ino,
359 data->args.pgbase, (size_t)data->args.count, offset);
360
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400361 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
362 return PNFS_NOT_ATTEMPTED;
363
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000364 /* Retrieve the correct rpc_client for the byte range */
365 j = nfs4_fl_calc_j_index(lseg, offset);
366 idx = nfs4_fl_calc_ds_index(lseg, j);
367 ds = nfs4_fl_prepare_ds(lseg, idx);
368 if (!ds) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000369 /* Either layout fh index faulty, or ds connect failed */
370 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
371 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000372 return PNFS_NOT_ATTEMPTED;
373 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400374 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000375
376 /* No multipath support. Use first DS */
377 data->ds_clp = ds->ds_clp;
378 fh = nfs4_fl_select_ds_fh(lseg, j);
379 if (fh)
380 data->args.fh = fh;
381
382 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
383 data->mds_offset = offset;
384
385 /* Perform an asynchronous read to ds */
386 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
387 &filelayout_read_call_ops);
388 BUG_ON(status != 0);
389 return PNFS_ATTEMPTED;
390}
391
Fred Isamana69aef12011-03-03 15:13:47 +0000392/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000393static enum pnfs_try_status
394filelayout_write_pagelist(struct nfs_write_data *data, int sync)
395{
Fred Isamana69aef12011-03-03 15:13:47 +0000396 struct pnfs_layout_segment *lseg = data->lseg;
397 struct nfs4_pnfs_ds *ds;
398 loff_t offset = data->args.offset;
399 u32 j, idx;
400 struct nfs_fh *fh;
401 int status;
402
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400403 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
404 return PNFS_NOT_ATTEMPTED;
405
Fred Isamana69aef12011-03-03 15:13:47 +0000406 /* Retrieve the correct rpc_client for the byte range */
407 j = nfs4_fl_calc_j_index(lseg, offset);
408 idx = nfs4_fl_calc_ds_index(lseg, j);
409 ds = nfs4_fl_prepare_ds(lseg, idx);
410 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500411 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
412 __func__);
Fred Isamana69aef12011-03-03 15:13:47 +0000413 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
414 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
415 return PNFS_NOT_ATTEMPTED;
416 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400417 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
Fred Isamana69aef12011-03-03 15:13:47 +0000418 data->inode->i_ino, sync, (size_t) data->args.count, offset,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400419 ds->ds_remotestr);
Fred Isamana69aef12011-03-03 15:13:47 +0000420
Fred Isamana69aef12011-03-03 15:13:47 +0000421 data->write_done_cb = filelayout_write_done_cb;
422 data->ds_clp = ds->ds_clp;
423 fh = nfs4_fl_select_ds_fh(lseg, j);
424 if (fh)
425 data->args.fh = fh;
426 /*
427 * Get the file offset on the dserver. Set the write offset to
428 * this offset and save the original offset.
429 */
430 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000431
432 /* Perform an asynchronous write */
433 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
434 &filelayout_write_call_ops, sync);
435 BUG_ON(status != 0);
436 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000437}
438
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000439/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400440 * filelayout_check_layout()
441 *
442 * Make sure layout segment parameters are sane WRT the device.
443 * At this point no generic layer initialization of the lseg has occurred,
444 * and nothing has been added to the layout_hdr cache.
445 *
446 */
447static int
448filelayout_check_layout(struct pnfs_layout_hdr *lo,
449 struct nfs4_filelayout_segment *fl,
450 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400451 struct nfs4_deviceid *id,
452 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400453{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400454 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400455 struct nfs4_file_layout_dsaddr *dsaddr;
456 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000457 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400458
459 dprintk("--> %s\n", __func__);
460
Andy Adamson7c24d942011-06-13 18:22:38 -0400461 /* FIXME: remove this check when layout segment support is added */
462 if (lgr->range.offset != 0 ||
463 lgr->range.length != NFS4_MAX_UINT64) {
464 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
465 __func__);
466 goto out;
467 }
468
Andy Adamson16b374c2010-10-20 00:18:04 -0400469 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500470 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400471 __func__, fl->pattern_offset);
472 goto out;
473 }
474
Benny Halevy75247af2011-02-22 15:56:01 -0800475 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
476 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400477 __func__, fl->stripe_unit);
478 goto out;
479 }
480
481 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300482 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
483 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400484 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400485 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400486 if (dsaddr == NULL)
487 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400488 } else
489 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400490 /* Found deviceid is being reaped */
491 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
492 goto out_put;
493
Andy Adamson16b374c2010-10-20 00:18:04 -0400494 fl->dsaddr = dsaddr;
495
Chuck Levere4149662011-10-25 12:18:03 -0400496 if (fl->first_stripe_index >= dsaddr->stripe_count) {
497 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400498 __func__, fl->first_stripe_index);
499 goto out_put;
500 }
501
502 if ((fl->stripe_type == STRIPE_SPARSE &&
503 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
504 (fl->stripe_type == STRIPE_DENSE &&
505 fl->num_fh != dsaddr->stripe_count)) {
506 dprintk("%s num_fh %u not valid for given packing\n",
507 __func__, fl->num_fh);
508 goto out_put;
509 }
510
511 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
512 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
513 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
514 nfss->wsize);
515 }
516
517 status = 0;
518out:
519 dprintk("--> %s returns %d\n", __func__, status);
520 return status;
521out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000522 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400523 goto out;
524}
525
526static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
527{
528 int i;
529
530 for (i = 0; i < fl->num_fh; i++) {
531 if (!fl->fh_array[i])
532 break;
533 kfree(fl->fh_array[i]);
534 }
535 kfree(fl->fh_array);
536 fl->fh_array = NULL;
537}
538
539static void
540_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
541{
542 filelayout_free_fh_array(fl);
543 kfree(fl);
544}
545
546static int
547filelayout_decode_layout(struct pnfs_layout_hdr *flo,
548 struct nfs4_filelayout_segment *fl,
549 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400550 struct nfs4_deviceid *id,
551 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400552{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400553 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400554 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400555 struct page *scratch;
556 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400557 uint32_t nfl_util;
558 int i;
559
560 dprintk("%s: set_layout_map Begin\n", __func__);
561
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400562 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400563 if (!scratch)
564 return -ENOMEM;
565
Benny Halevyf7da7a12011-05-19 14:16:47 -0400566 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400567 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
568
569 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
570 * num_fh (4) */
571 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
572 if (unlikely(!p))
573 goto out_err;
574
Andy Adamson16b374c2010-10-20 00:18:04 -0400575 memcpy(id, p, sizeof(*id));
576 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400577 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400578
579 nfl_util = be32_to_cpup(p++);
580 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
581 fl->commit_through_mds = 1;
582 if (nfl_util & NFL4_UFLG_DENSE)
583 fl->stripe_type = STRIPE_DENSE;
584 else
585 fl->stripe_type = STRIPE_SPARSE;
586 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
587
588 fl->first_stripe_index = be32_to_cpup(p++);
589 p = xdr_decode_hyper(p, &fl->pattern_offset);
590 fl->num_fh = be32_to_cpup(p++);
591
592 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
593 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
594 fl->pattern_offset);
595
Andy Adamsoncec765c2011-06-13 18:36:17 -0400596 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
597 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400598 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400599 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400600 goto out_err;
601
Andy Adamsoncec765c2011-06-13 18:36:17 -0400602 if (fl->num_fh > 0) {
603 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
604 gfp_flags);
605 if (!fl->fh_array)
606 goto out_err;
607 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400608
609 for (i = 0; i < fl->num_fh; i++) {
610 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400611 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400612 if (!fl->fh_array[i])
613 goto out_err_free;
614
615 p = xdr_inline_decode(&stream, 4);
616 if (unlikely(!p))
617 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400618 fl->fh_array[i]->size = be32_to_cpup(p++);
619 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500620 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400621 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400622 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400623 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400624
625 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
626 if (unlikely(!p))
627 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400628 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400629 dprintk("DEBUG: %s: fh len %d\n", __func__,
630 fl->fh_array[i]->size);
631 }
632
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400633 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400634 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400635
636out_err_free:
637 filelayout_free_fh_array(fl);
638out_err:
639 __free_page(scratch);
640 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400641}
642
Fred Isamanc8795132011-03-23 13:27:49 +0000643static void
644filelayout_free_lseg(struct pnfs_layout_segment *lseg)
645{
646 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
647
648 dprintk("--> %s\n", __func__);
649 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman425eb732011-03-23 13:27:50 +0000650 kfree(fl->commit_buckets);
Fred Isamanc8795132011-03-23 13:27:49 +0000651 _filelayout_free_lseg(fl);
652}
653
Andy Adamson16b374c2010-10-20 00:18:04 -0400654static struct pnfs_layout_segment *
655filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400656 struct nfs4_layoutget_res *lgr,
657 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400658{
659 struct nfs4_filelayout_segment *fl;
660 int rc;
661 struct nfs4_deviceid id;
662
663 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400664 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400665 if (!fl)
666 return NULL;
667
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400668 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
669 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400670 _filelayout_free_lseg(fl);
671 return NULL;
672 }
Fred Isaman425eb732011-03-23 13:27:50 +0000673
674 /* This assumes there is only one IOMODE_RW lseg. What
675 * we really want to do is have a layout_hdr level
676 * dictionary of <multipath_list4, fh> keys, each
677 * associated with a struct list_head, populated by calls
678 * to filelayout_write_pagelist().
679 * */
680 if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
681 int i;
682 int size = (fl->stripe_type == STRIPE_SPARSE) ?
683 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
684
Fred Isamand6d6dc72012-03-08 17:29:35 -0500685 fl->commit_buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket), gfp_flags);
Fred Isaman425eb732011-03-23 13:27:50 +0000686 if (!fl->commit_buckets) {
687 filelayout_free_lseg(&fl->generic_hdr);
688 return NULL;
689 }
690 fl->number_of_buckets = size;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500691 for (i = 0; i < size; i++) {
692 INIT_LIST_HEAD(&fl->commit_buckets[i].written);
693 INIT_LIST_HEAD(&fl->commit_buckets[i].committing);
694 }
Fred Isaman425eb732011-03-23 13:27:50 +0000695 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400696 return &fl->generic_hdr;
697}
698
Fred Isaman94ad1c82011-03-01 01:34:14 +0000699/*
700 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
701 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300702 * return true : coalesce page
703 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000704 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400705static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000706filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
707 struct nfs_page *req)
708{
709 u64 p_stripe, r_stripe;
710 u32 stripe_unit;
711
Benny Halevy19345cb2011-06-19 18:33:46 -0400712 if (!pnfs_generic_pg_test(pgio, prev, req) ||
713 !nfs_generic_pg_test(pgio, prev, req))
714 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300715
Fred Isaman94ad1c82011-03-01 01:34:14 +0000716 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
717 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
718 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
719
720 do_div(p_stripe, stripe_unit);
721 do_div(r_stripe, stripe_unit);
722
723 return (p_stripe == r_stripe);
724}
725
Andy Adamson7c24d942011-06-13 18:22:38 -0400726void
727filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
728 struct nfs_page *req)
729{
730 BUG_ON(pgio->pg_lseg != NULL);
731
732 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
733 req->wb_context,
734 0,
735 NFS4_MAX_UINT64,
736 IOMODE_READ,
737 GFP_KERNEL);
738 /* If no lseg, fall back to read through mds */
739 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400740 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400741}
742
743void
744filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
745 struct nfs_page *req)
746{
747 BUG_ON(pgio->pg_lseg != NULL);
748
749 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
750 req->wb_context,
751 0,
752 NFS4_MAX_UINT64,
753 IOMODE_RW,
754 GFP_NOFS);
755 /* If no lseg, fall back to write through mds */
756 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400757 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400758}
759
Trond Myklebust1751c362011-06-10 13:30:23 -0400760static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400761 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400762 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400763 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400764};
765
766static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400767 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400768 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400769 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400770};
771
Fred Isamane0c2b382011-03-23 13:27:53 +0000772static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
773{
774 if (fl->stripe_type == STRIPE_SPARSE)
775 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
776 else
777 return j;
778}
779
Fred Isamand6d6dc72012-03-08 17:29:35 -0500780/* The generic layer is about to remove the req from the commit list.
781 * If this will make the bucket empty, it will need to put the lseg reference.
782 * Note inode lock is held, so we can't do the put here.
783 */
784static struct pnfs_layout_segment *
785filelayout_remove_commit_req(struct nfs_page *req)
Fred Isamane0c2b382011-03-23 13:27:53 +0000786{
Fred Isamand6d6dc72012-03-08 17:29:35 -0500787 if (list_is_singular(&req->wb_list)) {
788 struct inode *inode = req->wb_context->dentry->d_inode;
789 struct pnfs_layout_segment *lseg;
790
791 /* From here we can find the bucket, but for the moment,
792 * since there is only one relevant lseg...
793 */
794 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
795 if (lseg->pls_range.iomode == IOMODE_RW)
796 return lseg;
797 }
798 }
799 return NULL;
800}
801
802static struct list_head *
803filelayout_choose_commit_list(struct nfs_page *req,
804 struct pnfs_layout_segment *lseg)
805{
Fred Isamane0c2b382011-03-23 13:27:53 +0000806 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
807 u32 i, j;
808 struct list_head *list;
809
Fred Isamand6d6dc72012-03-08 17:29:35 -0500810 if (fl->commit_through_mds)
811 return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
812
Fred Isamane0c2b382011-03-23 13:27:53 +0000813 /* Note that we are calling nfs4_fl_calc_j_index on each page
814 * that ends up being committed to a data server. An attractive
815 * alternative is to add a field to nfs_write_data and nfs_page
816 * to store the value calculated in filelayout_write_pagelist
817 * and just use that here.
818 */
819 j = nfs4_fl_calc_j_index(lseg,
820 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
821 i = select_bucket_index(fl, j);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500822 list = &fl->commit_buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +0000823 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500824 /* Non-empty buckets hold a reference on the lseg. That ref
825 * is normally transferred to the COMMIT call and released
826 * there. It could also be released if the last req is pulled
827 * off due to a rewrite, in which case it will be done in
828 * filelayout_remove_commit_req
829 */
Fred Isamane0c2b382011-03-23 13:27:53 +0000830 get_lseg(lseg);
831 }
832 return list;
833}
834
835static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
836{
837 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
838
839 if (flseg->stripe_type == STRIPE_SPARSE)
840 return i;
841 else
842 return nfs4_fl_calc_ds_index(lseg, i);
843}
844
845static struct nfs_fh *
846select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
847{
848 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
849
850 if (flseg->stripe_type == STRIPE_SPARSE) {
851 if (flseg->num_fh == 1)
852 i = 0;
853 else if (flseg->num_fh == 0)
854 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
855 return NULL;
856 }
857 return flseg->fh_array[i];
858}
859
860static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
861{
862 struct pnfs_layout_segment *lseg = data->lseg;
863 struct nfs4_pnfs_ds *ds;
864 u32 idx;
865 struct nfs_fh *fh;
866
867 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
868 ds = nfs4_fl_prepare_ds(lseg, idx);
869 if (!ds) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500870 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
871 __func__);
Fred Isamane0c2b382011-03-23 13:27:53 +0000872 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
873 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
874 prepare_to_resend_writes(data);
875 data->mds_ops->rpc_release(data);
876 return -EAGAIN;
877 }
878 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
879 data->write_done_cb = filelayout_commit_done_cb;
880 data->ds_clp = ds->ds_clp;
881 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
882 if (fh)
883 data->args.fh = fh;
884 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
885 &filelayout_commit_call_ops, how);
886}
887
888/*
889 * This is only useful while we are using whole file layouts.
890 */
Fred Isamand6d6dc72012-03-08 17:29:35 -0500891static struct pnfs_layout_segment *
892find_only_write_lseg_locked(struct inode *inode)
Fred Isamane0c2b382011-03-23 13:27:53 +0000893{
Fred Isamand6d6dc72012-03-08 17:29:35 -0500894 struct pnfs_layout_segment *lseg;
Fred Isamane0c2b382011-03-23 13:27:53 +0000895
Fred Isamane0c2b382011-03-23 13:27:53 +0000896 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
897 if (lseg->pls_range.iomode == IOMODE_RW)
Fred Isamand6d6dc72012-03-08 17:29:35 -0500898 return get_lseg(lseg);
899 return NULL;
900}
901
902static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
903{
904 struct pnfs_layout_segment *rv;
905
906 spin_lock(&inode->i_lock);
907 rv = find_only_write_lseg_locked(inode);
Fred Isamane0c2b382011-03-23 13:27:53 +0000908 spin_unlock(&inode->i_lock);
909 return rv;
910}
911
Fred Isamand6d6dc72012-03-08 17:29:35 -0500912/* Move reqs from written to committing lists, returning count of number moved.
913 * Note called with i_lock held.
914 */
915static int filelayout_scan_commit_lists(struct inode *inode, int max)
916{
917 struct pnfs_layout_segment *lseg;
918 struct nfs4_filelayout_segment *fl;
919 int i, rv = 0, cnt;
920
921 lseg = find_only_write_lseg_locked(inode);
922 if (!lseg)
923 return 0;
924 fl = FILELAYOUT_LSEG(lseg);
925 if (fl->commit_through_mds)
926 goto out_put;
927 for (i = 0; i < fl->number_of_buckets; i++) {
928 if (list_empty(&fl->commit_buckets[i].written))
929 continue;
930 cnt = nfs_scan_commit_list(&fl->commit_buckets[i].written,
931 &fl->commit_buckets[i].committing,
932 max);
933 max -= cnt;
934 rv += cnt;
935 }
936out_put:
937 put_lseg(lseg);
938 return rv;
939}
940
Fred Isamane0c2b382011-03-23 13:27:53 +0000941static int alloc_ds_commits(struct inode *inode, struct list_head *list)
942{
943 struct pnfs_layout_segment *lseg;
944 struct nfs4_filelayout_segment *fl;
945 struct nfs_write_data *data;
946 int i, j;
947
948 /* Won't need this when non-whole file layout segments are supported
949 * instead we will use a pnfs_layout_hdr structure */
950 lseg = find_only_write_lseg(inode);
951 if (!lseg)
952 return 0;
953 fl = FILELAYOUT_LSEG(lseg);
954 for (i = 0; i < fl->number_of_buckets; i++) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500955 if (list_empty(&fl->commit_buckets[i].committing))
Fred Isamane0c2b382011-03-23 13:27:53 +0000956 continue;
957 data = nfs_commitdata_alloc();
958 if (!data)
959 goto out_bad;
960 data->ds_commit_index = i;
961 data->lseg = lseg;
962 list_add(&data->pages, list);
963 }
964 put_lseg(lseg);
965 return 0;
966
967out_bad:
968 for (j = i; j < fl->number_of_buckets; j++) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500969 if (list_empty(&fl->commit_buckets[i].committing))
Fred Isamane0c2b382011-03-23 13:27:53 +0000970 continue;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500971 nfs_retry_commit(&fl->commit_buckets[i].committing, lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000972 put_lseg(lseg); /* associated with emptying bucket */
973 }
974 put_lseg(lseg);
975 /* Caller will clean up entries put on list */
976 return -ENOMEM;
977}
978
979/* This follows nfs_commit_list pretty closely */
980static int
981filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
982 int how)
983{
984 struct nfs_write_data *data, *tmp;
985 LIST_HEAD(list);
986
987 if (!list_empty(mds_pages)) {
988 data = nfs_commitdata_alloc();
989 if (!data)
990 goto out_bad;
991 data->lseg = NULL;
992 list_add(&data->pages, &list);
993 }
994
995 if (alloc_ds_commits(inode, &list))
996 goto out_bad;
997
998 list_for_each_entry_safe(data, tmp, &list, pages) {
999 list_del_init(&data->pages);
1000 atomic_inc(&NFS_I(inode)->commits_outstanding);
1001 if (!data->lseg) {
1002 nfs_init_commit(data, mds_pages, NULL);
1003 nfs_initiate_commit(data, NFS_CLIENT(inode),
1004 data->mds_ops, how);
1005 } else {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001006 nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index].committing, data->lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001007 filelayout_initiate_commit(data, how);
1008 }
1009 }
1010 return 0;
1011 out_bad:
1012 list_for_each_entry_safe(data, tmp, &list, pages) {
1013 nfs_retry_commit(&data->pages, data->lseg);
1014 list_del_init(&data->pages);
1015 nfs_commit_free(data);
1016 }
1017 nfs_retry_commit(mds_pages, NULL);
1018 nfs_commit_clear_lock(NFS_I(inode));
1019 return -ENOMEM;
1020}
1021
Benny Halevy1775bc32011-05-20 13:47:33 +02001022static void
1023filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1024{
1025 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1026}
1027
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001028static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001029 .id = LAYOUT_NFSV4_1_FILES,
1030 .name = "LAYOUT_NFSV4_1_FILES",
1031 .owner = THIS_MODULE,
1032 .alloc_lseg = filelayout_alloc_lseg,
1033 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001034 .pg_read_ops = &filelayout_pg_read_ops,
1035 .pg_write_ops = &filelayout_pg_write_ops,
Fred Isamane0c2b382011-03-23 13:27:53 +00001036 .choose_commit_list = filelayout_choose_commit_list,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001037 .remove_commit_req = filelayout_remove_commit_req,
1038 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isamane0c2b382011-03-23 13:27:53 +00001039 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001040 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001041 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001042 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001043};
1044
1045static int __init nfs4filelayout_init(void)
1046{
1047 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1048 __func__);
1049 return pnfs_register_layoutdriver(&filelayout_type);
1050}
1051
1052static void __exit nfs4filelayout_exit(void)
1053{
1054 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1055 __func__);
1056 pnfs_unregister_layoutdriver(&filelayout_type);
1057}
1058
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001059MODULE_ALIAS("nfs-layouttype4-1");
1060
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001061module_init(nfs4filelayout_init);
1062module_exit(nfs4filelayout_exit);