blob: eaaca897305cc2d399951c524bebc511244440d4 [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 Adamsone7dd79af2012-04-27 17:53:46 -040085static void filelayout_reset_write(struct nfs_write_data *data)
86{
87 struct nfs_pgio_header *hdr = data->header;
88 struct inode *inode = hdr->inode;
89 struct rpc_task *task = &data->task;
90
91 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
92 dprintk("%s Reset task %5u for i/o through MDS "
93 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
94 data->task.tk_pid,
95 inode->i_sb->s_id,
96 (long long)NFS_FILEID(inode),
97 data->args.count,
98 (unsigned long long)data->args.offset);
99
100 task->tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
101 &hdr->pages,
102 hdr->completion_ops);
103 }
Andy Adamson3a7936c2012-04-27 17:53:51 -0400104 /* balance nfs_get_client in filelayout_write_pagelist */
105 nfs_put_client(data->ds_clp);
106 data->ds_clp = NULL;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400107}
108
109static void filelayout_reset_read(struct nfs_read_data *data)
110{
111 struct nfs_pgio_header *hdr = data->header;
112 struct inode *inode = hdr->inode;
113 struct rpc_task *task = &data->task;
114
115 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
116 dprintk("%s Reset task %5u for i/o through MDS "
117 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
118 data->task.tk_pid,
119 inode->i_sb->s_id,
120 (long long)NFS_FILEID(inode),
121 data->args.count,
122 (unsigned long long)data->args.offset);
123
124 task->tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
125 &hdr->pages,
126 hdr->completion_ops);
127 }
Andy Adamson3a7936c2012-04-27 17:53:51 -0400128 /* balance nfs_get_client in filelayout_read_pagelist */
129 nfs_put_client(data->ds_clp);
130 data->ds_clp = NULL;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400131}
132
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000133static int filelayout_async_handle_error(struct rpc_task *task,
134 struct nfs4_state *state,
135 struct nfs_client *clp,
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400136 struct pnfs_layout_segment *lseg)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000137{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400138 struct inode *inode = lseg->pls_layout->plh_inode;
139 struct nfs_server *mds_server = NFS_SERVER(inode);
140 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
Andy Adamson9cb81962012-03-07 10:49:41 -0500141 struct nfs_client *mds_client = mds_server->nfs_client;
Andy Adamson671fb892012-04-27 17:53:49 -0400142 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
Andy Adamson9cb81962012-03-07 10:49:41 -0500143
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000144 if (task->tk_status >= 0)
145 return 0;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000146
147 switch (task->tk_status) {
Andy Adamson9cb81962012-03-07 10:49:41 -0500148 /* MDS state errors */
149 case -NFS4ERR_DELEG_REVOKED:
150 case -NFS4ERR_ADMIN_REVOKED:
151 case -NFS4ERR_BAD_STATEID:
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400152 if (state == NULL)
153 break;
Andy Adamson2dc31752012-03-08 11:03:53 -0500154 nfs_remove_bad_delegation(state->inode);
Andy Adamson9cb81962012-03-07 10:49:41 -0500155 case -NFS4ERR_OPENMODE:
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400156 if (state == NULL)
157 break;
Andy Adamson9cb81962012-03-07 10:49:41 -0500158 nfs4_schedule_stateid_recovery(mds_server, state);
159 goto wait_on_recovery;
160 case -NFS4ERR_EXPIRED:
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400161 if (state != NULL)
162 nfs4_schedule_stateid_recovery(mds_server, state);
Andy Adamson9cb81962012-03-07 10:49:41 -0500163 nfs4_schedule_lease_recovery(mds_client);
164 goto wait_on_recovery;
165 /* DS session errors */
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000166 case -NFS4ERR_BADSESSION:
167 case -NFS4ERR_BADSLOT:
168 case -NFS4ERR_BAD_HIGH_SLOT:
169 case -NFS4ERR_DEADSESSION:
170 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
171 case -NFS4ERR_SEQ_FALSE_RETRY:
172 case -NFS4ERR_SEQ_MISORDERED:
173 dprintk("%s ERROR %d, Reset session. Exchangeid "
174 "flags 0x%x\n", __func__, task->tk_status,
175 clp->cl_exchange_flags);
176 nfs4_schedule_session_recovery(clp->cl_session);
177 break;
178 case -NFS4ERR_DELAY:
179 case -NFS4ERR_GRACE:
180 case -EKEYEXPIRED:
181 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
182 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400183 case -NFS4ERR_RETRY_UNCACHED_REP:
184 break;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400185 /* RPC connection errors */
186 case -ECONNREFUSED:
187 case -EHOSTDOWN:
188 case -EHOSTUNREACH:
189 case -ENETUNREACH:
190 case -EIO:
191 case -ETIMEDOUT:
192 case -EPIPE:
193 dprintk("%s DS connection error %d\n", __func__,
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000194 task->tk_status);
Andy Adamson0a57cda2012-04-27 17:53:50 -0400195 if (!filelayout_test_devid_invalid(devid))
196 _pnfs_return_layout(state->inode);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400197 filelayout_mark_devid_invalid(devid);
Andy Adamson671fb892012-04-27 17:53:49 -0400198 rpc_wake_up(&tbl->slot_tbl_waitq);
Andy Adamsonb4a29672012-04-27 17:53:52 -0400199 nfs4_ds_disconnect(clp);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400200 /* fall through */
201 default:
202 dprintk("%s Retry through MDS. Error %d\n", __func__,
203 task->tk_status);
204 return -NFS4ERR_RESET_TO_MDS;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000205 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500206out:
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000207 task->tk_status = 0;
208 return -EAGAIN;
Andy Adamson9cb81962012-03-07 10:49:41 -0500209wait_on_recovery:
210 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
211 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
212 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
213 goto out;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000214}
215
216/* NFS_PROTO call done callback routines */
217
218static int filelayout_read_done_cb(struct rpc_task *task,
219 struct nfs_read_data *data)
220{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400221 struct nfs_pgio_header *hdr = data->header;
222 int err;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000223
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400224 err = filelayout_async_handle_error(task, data->args.context->state,
225 data->ds_clp, hdr->lseg);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000226
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400227 switch (err) {
228 case -NFS4ERR_RESET_TO_MDS:
229 filelayout_reset_read(data);
230 return task->tk_status;
231 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700232 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000233 return -EAGAIN;
234 }
235
236 return 0;
237}
238
Andy Adamson16b374c2010-10-20 00:18:04 -0400239/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000240 * We reference the rpc_cred of the first WRITE that triggers the need for
241 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
242 * rfc5661 is not clear about which credential should be used.
243 */
244static void
245filelayout_set_layoutcommit(struct nfs_write_data *wdata)
246{
Fred Isamancd841602012-04-20 14:47:44 -0400247 struct nfs_pgio_header *hdr = wdata->header;
248
249 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
Andy Adamson863a3c62011-03-23 13:27:54 +0000250 wdata->res.verf->committed == NFS_FILE_SYNC)
251 return;
252
253 pnfs_set_layoutcommit(wdata);
Fred Isamancd841602012-04-20 14:47:44 -0400254 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
255 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000256}
257
258/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000259 * Call ops for the async read/write cases
260 * In the case of dense layouts, the offset needs to be reset to its
261 * original value.
262 */
263static void filelayout_read_prepare(struct rpc_task *task, void *data)
264{
Fred Isamancd12ae32012-04-20 14:47:42 -0400265 struct nfs_read_data *rdata = data;
Andy Adamson0ad2f372012-04-27 17:53:48 -0400266 struct pnfs_layout_segment *lseg = rdata->header->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000267
Andy Adamson0ad2f372012-04-27 17:53:48 -0400268 if (filelayout_test_devid_invalid(FILELAYOUT_DEVID_NODE(lseg))) {
269 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
270 filelayout_reset_read(rdata);
271 rpc_exit(task, 0);
272 return;
273 }
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000274 rdata->read_done_cb = filelayout_read_done_cb;
275
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000276 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
277 &rdata->args.seq_args, &rdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500278 task))
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000279 return;
280
281 rpc_call_start(task);
282}
283
284static void filelayout_read_call_done(struct rpc_task *task, void *data)
285{
Fred Isamancd12ae32012-04-20 14:47:42 -0400286 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000287
288 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
289
Andy Adamson0ad2f372012-04-27 17:53:48 -0400290 if (test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
291 return;
292
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000293 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400294 rdata->header->mds_ops->rpc_call_done(task, data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000295}
296
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500297static void filelayout_read_count_stats(struct rpc_task *task, void *data)
298{
Fred Isamancd12ae32012-04-20 14:47:42 -0400299 struct nfs_read_data *rdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500300
Fred Isamancd841602012-04-20 14:47:44 -0400301 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500302}
303
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000304static void filelayout_read_release(void *data)
305{
Fred Isamancd12ae32012-04-20 14:47:42 -0400306 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000307
Andy Adamson3a7936c2012-04-27 17:53:51 -0400308 if (!test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
309 nfs_put_client(rdata->ds_clp);
Fred Isamancd841602012-04-20 14:47:44 -0400310 rdata->header->mds_ops->rpc_release(data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000311}
312
Fred Isamana69aef12011-03-03 15:13:47 +0000313static int filelayout_write_done_cb(struct rpc_task *task,
314 struct nfs_write_data *data)
315{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400316 struct nfs_pgio_header *hdr = data->header;
317 int err;
Fred Isamana69aef12011-03-03 15:13:47 +0000318
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400319 err = filelayout_async_handle_error(task, data->args.context->state,
320 data->ds_clp, hdr->lseg);
321
322 switch (err) {
323 case -NFS4ERR_RESET_TO_MDS:
324 filelayout_reset_write(data);
325 return task->tk_status;
326 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700327 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000328 return -EAGAIN;
329 }
330
Andy Adamson863a3c62011-03-23 13:27:54 +0000331 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000332 return 0;
333}
334
Fred Isamane0c2b382011-03-23 13:27:53 +0000335/* Fake up some data that will cause nfs_commit_release to retry the writes. */
Fred Isaman0b7c0152012-04-20 14:47:39 -0400336static void prepare_to_resend_writes(struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000337{
338 struct nfs_page *first = nfs_list_entry(data->pages.next);
339
340 data->task.tk_status = 0;
341 memcpy(data->verf.verifier, first->wb_verf.verifier,
342 sizeof(first->wb_verf.verifier));
343 data->verf.verifier[0]++; /* ensure verifier mismatch */
344}
345
346static int filelayout_commit_done_cb(struct rpc_task *task,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400347 struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000348{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400349 int err;
Fred Isamane0c2b382011-03-23 13:27:53 +0000350
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400351 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
352 data->lseg);
353
354 switch (err) {
355 case -NFS4ERR_RESET_TO_MDS:
356 prepare_to_resend_writes(data);
357 return -EAGAIN;
358 case -EAGAIN:
359 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000360 return -EAGAIN;
361 }
362
363 return 0;
364}
365
Fred Isamana69aef12011-03-03 15:13:47 +0000366static void filelayout_write_prepare(struct rpc_task *task, void *data)
367{
Fred Isamancd12ae32012-04-20 14:47:42 -0400368 struct nfs_write_data *wdata = data;
Andy Adamson0ad2f372012-04-27 17:53:48 -0400369 struct pnfs_layout_segment *lseg = wdata->header->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000370
Andy Adamson0ad2f372012-04-27 17:53:48 -0400371 if (filelayout_test_devid_invalid(FILELAYOUT_DEVID_NODE(lseg))) {
372 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
373 filelayout_reset_write(wdata);
374 rpc_exit(task, 0);
375 return;
376 }
Fred Isamana69aef12011-03-03 15:13:47 +0000377 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
378 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500379 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000380 return;
381
382 rpc_call_start(task);
383}
384
385static void filelayout_write_call_done(struct rpc_task *task, void *data)
386{
Fred Isamancd12ae32012-04-20 14:47:42 -0400387 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000388
Andy Adamson0ad2f372012-04-27 17:53:48 -0400389 if (test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
390 return;
391
Fred Isamana69aef12011-03-03 15:13:47 +0000392 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400393 wdata->header->mds_ops->rpc_call_done(task, data);
Fred Isamana69aef12011-03-03 15:13:47 +0000394}
395
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500396static void filelayout_write_count_stats(struct rpc_task *task, void *data)
397{
Fred Isamancd12ae32012-04-20 14:47:42 -0400398 struct nfs_write_data *wdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500399
Fred Isamancd841602012-04-20 14:47:44 -0400400 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500401}
402
Fred Isamana69aef12011-03-03 15:13:47 +0000403static void filelayout_write_release(void *data)
404{
Fred Isamancd12ae32012-04-20 14:47:42 -0400405 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000406
Andy Adamson3a7936c2012-04-27 17:53:51 -0400407 if (!test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
408 nfs_put_client(wdata->ds_clp);
Fred Isamancd841602012-04-20 14:47:44 -0400409 wdata->header->mds_ops->rpc_release(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000410}
411
Fred Isaman0b7c0152012-04-20 14:47:39 -0400412static void filelayout_commit_prepare(struct rpc_task *task, void *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000413{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400414 struct nfs_commit_data *wdata = data;
Fred Isamane0c2b382011-03-23 13:27:53 +0000415
Fred Isaman0b7c0152012-04-20 14:47:39 -0400416 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
417 &wdata->args.seq_args, &wdata->res.seq_res,
418 task))
419 return;
420
421 rpc_call_start(task);
422}
423
424static void filelayout_write_commit_done(struct rpc_task *task, void *data)
425{
426 struct nfs_commit_data *wdata = data;
427
428 /* Note this may cause RPC to be resent */
429 wdata->mds_ops->rpc_call_done(task, data);
430}
431
432static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
433{
434 struct nfs_commit_data *cdata = data;
435
436 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
437}
438
439static void filelayout_commit_release(void *calldata)
440{
441 struct nfs_commit_data *data = calldata;
442
Fred Isamanf453a542012-04-20 14:47:54 -0400443 data->completion_ops->completion(data);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400444 put_lseg(data->lseg);
Andy Adamson3a7936c2012-04-27 17:53:51 -0400445 nfs_put_client(data->ds_clp);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400446 nfs_commitdata_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000447}
448
Trond Myklebust17280172012-03-11 13:11:00 -0400449static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000450 .rpc_call_prepare = filelayout_read_prepare,
451 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500452 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000453 .rpc_release = filelayout_read_release,
454};
455
Trond Myklebust17280172012-03-11 13:11:00 -0400456static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000457 .rpc_call_prepare = filelayout_write_prepare,
458 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500459 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000460 .rpc_release = filelayout_write_release,
461};
462
Trond Myklebust17280172012-03-11 13:11:00 -0400463static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isaman0b7c0152012-04-20 14:47:39 -0400464 .rpc_call_prepare = filelayout_commit_prepare,
465 .rpc_call_done = filelayout_write_commit_done,
466 .rpc_count_stats = filelayout_commit_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000467 .rpc_release = filelayout_commit_release,
468};
469
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000470static enum pnfs_try_status
471filelayout_read_pagelist(struct nfs_read_data *data)
472{
Fred Isamancd841602012-04-20 14:47:44 -0400473 struct nfs_pgio_header *hdr = data->header;
474 struct pnfs_layout_segment *lseg = hdr->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000475 struct nfs4_pnfs_ds *ds;
476 loff_t offset = data->args.offset;
477 u32 j, idx;
478 struct nfs_fh *fh;
479 int status;
480
481 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
Fred Isamancd841602012-04-20 14:47:44 -0400482 __func__, hdr->inode->i_ino,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000483 data->args.pgbase, (size_t)data->args.count, offset);
484
485 /* Retrieve the correct rpc_client for the byte range */
486 j = nfs4_fl_calc_j_index(lseg, offset);
487 idx = nfs4_fl_calc_ds_index(lseg, j);
488 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400489 if (!ds)
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000490 return PNFS_NOT_ATTEMPTED;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400491 dprintk("%s USE DS: %s cl_count %d\n", __func__,
492 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000493
494 /* No multipath support. Use first DS */
Andy Adamson3a7936c2012-04-27 17:53:51 -0400495 atomic_inc(&ds->ds_clp->cl_count);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000496 data->ds_clp = ds->ds_clp;
497 fh = nfs4_fl_select_ds_fh(lseg, j);
498 if (fh)
499 data->args.fh = fh;
500
501 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
502 data->mds_offset = offset;
503
504 /* Perform an asynchronous read to ds */
Fred Isamanc5996c42012-04-20 14:47:41 -0400505 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400506 &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000507 BUG_ON(status != 0);
508 return PNFS_ATTEMPTED;
509}
510
Fred Isamana69aef12011-03-03 15:13:47 +0000511/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000512static enum pnfs_try_status
513filelayout_write_pagelist(struct nfs_write_data *data, int sync)
514{
Fred Isamancd841602012-04-20 14:47:44 -0400515 struct nfs_pgio_header *hdr = data->header;
516 struct pnfs_layout_segment *lseg = hdr->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000517 struct nfs4_pnfs_ds *ds;
518 loff_t offset = data->args.offset;
519 u32 j, idx;
520 struct nfs_fh *fh;
521 int status;
522
523 /* Retrieve the correct rpc_client for the byte range */
524 j = nfs4_fl_calc_j_index(lseg, offset);
525 idx = nfs4_fl_calc_ds_index(lseg, j);
526 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400527 if (!ds)
Fred Isamana69aef12011-03-03 15:13:47 +0000528 return PNFS_NOT_ATTEMPTED;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400529 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
530 __func__, hdr->inode->i_ino, sync, (size_t) data->args.count,
531 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Fred Isamana69aef12011-03-03 15:13:47 +0000532
Fred Isamana69aef12011-03-03 15:13:47 +0000533 data->write_done_cb = filelayout_write_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400534 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamana69aef12011-03-03 15:13:47 +0000535 data->ds_clp = ds->ds_clp;
536 fh = nfs4_fl_select_ds_fh(lseg, j);
537 if (fh)
538 data->args.fh = fh;
539 /*
540 * Get the file offset on the dserver. Set the write offset to
541 * this offset and save the original offset.
542 */
543 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000544
545 /* Perform an asynchronous write */
Fred Isamanc5996c42012-04-20 14:47:41 -0400546 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400547 &filelayout_write_call_ops, sync,
548 RPC_TASK_SOFTCONN);
Fred Isamana69aef12011-03-03 15:13:47 +0000549 BUG_ON(status != 0);
550 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000551}
552
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000553/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400554 * filelayout_check_layout()
555 *
556 * Make sure layout segment parameters are sane WRT the device.
557 * At this point no generic layer initialization of the lseg has occurred,
558 * and nothing has been added to the layout_hdr cache.
559 *
560 */
561static int
562filelayout_check_layout(struct pnfs_layout_hdr *lo,
563 struct nfs4_filelayout_segment *fl,
564 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400565 struct nfs4_deviceid *id,
566 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400567{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400568 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400569 struct nfs4_file_layout_dsaddr *dsaddr;
570 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000571 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400572
573 dprintk("--> %s\n", __func__);
574
Andy Adamson7c24d942011-06-13 18:22:38 -0400575 /* FIXME: remove this check when layout segment support is added */
576 if (lgr->range.offset != 0 ||
577 lgr->range.length != NFS4_MAX_UINT64) {
578 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
579 __func__);
580 goto out;
581 }
582
Andy Adamson16b374c2010-10-20 00:18:04 -0400583 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500584 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400585 __func__, fl->pattern_offset);
586 goto out;
587 }
588
Benny Halevy75247af2011-02-22 15:56:01 -0800589 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
590 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400591 __func__, fl->stripe_unit);
592 goto out;
593 }
594
595 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300596 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
597 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400598 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400599 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400600 if (dsaddr == NULL)
601 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400602 } else
603 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400604 /* Found deviceid is being reaped */
605 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
606 goto out_put;
607
Andy Adamson16b374c2010-10-20 00:18:04 -0400608 fl->dsaddr = dsaddr;
609
Chuck Levere4149662011-10-25 12:18:03 -0400610 if (fl->first_stripe_index >= dsaddr->stripe_count) {
611 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400612 __func__, fl->first_stripe_index);
613 goto out_put;
614 }
615
616 if ((fl->stripe_type == STRIPE_SPARSE &&
617 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
618 (fl->stripe_type == STRIPE_DENSE &&
619 fl->num_fh != dsaddr->stripe_count)) {
620 dprintk("%s num_fh %u not valid for given packing\n",
621 __func__, fl->num_fh);
622 goto out_put;
623 }
624
625 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
626 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
627 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
628 nfss->wsize);
629 }
630
631 status = 0;
632out:
633 dprintk("--> %s returns %d\n", __func__, status);
634 return status;
635out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000636 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400637 goto out;
638}
639
640static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
641{
642 int i;
643
644 for (i = 0; i < fl->num_fh; i++) {
645 if (!fl->fh_array[i])
646 break;
647 kfree(fl->fh_array[i]);
648 }
649 kfree(fl->fh_array);
650 fl->fh_array = NULL;
651}
652
653static void
654_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
655{
656 filelayout_free_fh_array(fl);
657 kfree(fl);
658}
659
660static int
661filelayout_decode_layout(struct pnfs_layout_hdr *flo,
662 struct nfs4_filelayout_segment *fl,
663 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400664 struct nfs4_deviceid *id,
665 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400666{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400667 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400668 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400669 struct page *scratch;
670 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400671 uint32_t nfl_util;
672 int i;
673
674 dprintk("%s: set_layout_map Begin\n", __func__);
675
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400676 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400677 if (!scratch)
678 return -ENOMEM;
679
Benny Halevyf7da7a12011-05-19 14:16:47 -0400680 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400681 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
682
683 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
684 * num_fh (4) */
685 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
686 if (unlikely(!p))
687 goto out_err;
688
Andy Adamson16b374c2010-10-20 00:18:04 -0400689 memcpy(id, p, sizeof(*id));
690 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400691 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400692
693 nfl_util = be32_to_cpup(p++);
694 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
695 fl->commit_through_mds = 1;
696 if (nfl_util & NFL4_UFLG_DENSE)
697 fl->stripe_type = STRIPE_DENSE;
698 else
699 fl->stripe_type = STRIPE_SPARSE;
700 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
701
702 fl->first_stripe_index = be32_to_cpup(p++);
703 p = xdr_decode_hyper(p, &fl->pattern_offset);
704 fl->num_fh = be32_to_cpup(p++);
705
706 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
707 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
708 fl->pattern_offset);
709
Andy Adamsoncec765c2011-06-13 18:36:17 -0400710 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
711 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400712 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400713 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400714 goto out_err;
715
Andy Adamsoncec765c2011-06-13 18:36:17 -0400716 if (fl->num_fh > 0) {
717 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
718 gfp_flags);
719 if (!fl->fh_array)
720 goto out_err;
721 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400722
723 for (i = 0; i < fl->num_fh; i++) {
724 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400725 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400726 if (!fl->fh_array[i])
727 goto out_err_free;
728
729 p = xdr_inline_decode(&stream, 4);
730 if (unlikely(!p))
731 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400732 fl->fh_array[i]->size = be32_to_cpup(p++);
733 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500734 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400735 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400736 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400737 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400738
739 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
740 if (unlikely(!p))
741 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400742 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400743 dprintk("DEBUG: %s: fh len %d\n", __func__,
744 fl->fh_array[i]->size);
745 }
746
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400747 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400748 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400749
750out_err_free:
751 filelayout_free_fh_array(fl);
752out_err:
753 __free_page(scratch);
754 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400755}
756
Fred Isamanc8795132011-03-23 13:27:49 +0000757static void
758filelayout_free_lseg(struct pnfs_layout_segment *lseg)
759{
760 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
761
762 dprintk("--> %s\n", __func__);
763 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400764 /* This assumes a single RW lseg */
765 if (lseg->pls_range.iomode == IOMODE_RW) {
766 struct nfs4_filelayout *flo;
767
768 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
769 flo->commit_info.nbuckets = 0;
770 kfree(flo->commit_info.buckets);
771 flo->commit_info.buckets = NULL;
772 }
Fred Isamanc8795132011-03-23 13:27:49 +0000773 _filelayout_free_lseg(fl);
774}
775
Fred Isaman799ba8d2012-04-20 14:47:38 -0400776static int
777filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
Fred Isamanea2cf222012-04-20 14:47:53 -0400778 struct nfs_commit_info *cinfo,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400779 gfp_t gfp_flags)
780{
781 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
Fred Isamanea2cf222012-04-20 14:47:53 -0400782 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400783 int size;
784
785 if (fl->commit_through_mds)
786 return 0;
Fred Isamanea2cf222012-04-20 14:47:53 -0400787 if (cinfo->ds->nbuckets != 0) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400788 /* This assumes there is only one IOMODE_RW lseg. What
789 * we really want to do is have a layout_hdr level
790 * dictionary of <multipath_list4, fh> keys, each
791 * associated with a struct list_head, populated by calls
792 * to filelayout_write_pagelist().
793 * */
794 return 0;
795 }
796
797 size = (fl->stripe_type == STRIPE_SPARSE) ?
798 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
799
Fred Isamanea2cf222012-04-20 14:47:53 -0400800 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
Fred Isaman799ba8d2012-04-20 14:47:38 -0400801 gfp_flags);
802 if (!buckets)
803 return -ENOMEM;
804 else {
805 int i;
806
Fred Isamanea2cf222012-04-20 14:47:53 -0400807 spin_lock(cinfo->lock);
808 if (cinfo->ds->nbuckets != 0)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400809 kfree(buckets);
810 else {
Fred Isamanea2cf222012-04-20 14:47:53 -0400811 cinfo->ds->buckets = buckets;
812 cinfo->ds->nbuckets = size;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400813 for (i = 0; i < size; i++) {
814 INIT_LIST_HEAD(&buckets[i].written);
815 INIT_LIST_HEAD(&buckets[i].committing);
816 }
817 }
Fred Isamanea2cf222012-04-20 14:47:53 -0400818 spin_unlock(cinfo->lock);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400819 return 0;
820 }
821}
822
Andy Adamson16b374c2010-10-20 00:18:04 -0400823static struct pnfs_layout_segment *
824filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400825 struct nfs4_layoutget_res *lgr,
826 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400827{
828 struct nfs4_filelayout_segment *fl;
829 int rc;
830 struct nfs4_deviceid id;
831
832 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400833 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400834 if (!fl)
835 return NULL;
836
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400837 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
838 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400839 _filelayout_free_lseg(fl);
840 return NULL;
841 }
842 return &fl->generic_hdr;
843}
844
Fred Isaman94ad1c82011-03-01 01:34:14 +0000845/*
846 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
847 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300848 * return true : coalesce page
849 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000850 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400851static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000852filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
853 struct nfs_page *req)
854{
855 u64 p_stripe, r_stripe;
856 u32 stripe_unit;
857
Benny Halevy19345cb2011-06-19 18:33:46 -0400858 if (!pnfs_generic_pg_test(pgio, prev, req) ||
859 !nfs_generic_pg_test(pgio, prev, req))
860 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300861
Fred Isamanb5542842012-04-20 14:47:43 -0400862 p_stripe = (u64)req_offset(prev);
863 r_stripe = (u64)req_offset(req);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000864 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
865
866 do_div(p_stripe, stripe_unit);
867 do_div(r_stripe, stripe_unit);
868
869 return (p_stripe == r_stripe);
870}
871
Trond Myklebust17280172012-03-11 13:11:00 -0400872static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400873filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
874 struct nfs_page *req)
875{
876 BUG_ON(pgio->pg_lseg != NULL);
877
Fred Isaman1825a0d2012-04-20 19:55:31 -0400878 if (req->wb_offset != req->wb_pgbase) {
879 /*
880 * Handling unaligned pages is difficult, because have to
881 * somehow split a req in two in certain cases in the
882 * pg.test code. Avoid this by just not using pnfs
883 * in this case.
884 */
885 nfs_pageio_reset_read_mds(pgio);
886 return;
887 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400888 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
889 req->wb_context,
890 0,
891 NFS4_MAX_UINT64,
892 IOMODE_READ,
893 GFP_KERNEL);
894 /* If no lseg, fall back to read through mds */
895 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400896 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400897}
898
Trond Myklebust17280172012-03-11 13:11:00 -0400899static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400900filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
901 struct nfs_page *req)
902{
Fred Isamanea2cf222012-04-20 14:47:53 -0400903 struct nfs_commit_info cinfo;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400904 int status;
905
Andy Adamson7c24d942011-06-13 18:22:38 -0400906 BUG_ON(pgio->pg_lseg != NULL);
907
Fred Isaman1825a0d2012-04-20 19:55:31 -0400908 if (req->wb_offset != req->wb_pgbase)
909 goto out_mds;
Andy Adamson7c24d942011-06-13 18:22:38 -0400910 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
911 req->wb_context,
912 0,
913 NFS4_MAX_UINT64,
914 IOMODE_RW,
915 GFP_NOFS);
916 /* If no lseg, fall back to write through mds */
917 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400918 goto out_mds;
Fred Isamanea2cf222012-04-20 14:47:53 -0400919 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
920 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400921 if (status < 0) {
922 put_lseg(pgio->pg_lseg);
923 pgio->pg_lseg = NULL;
924 goto out_mds;
925 }
926 return;
927out_mds:
928 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400929}
930
Trond Myklebust1751c362011-06-10 13:30:23 -0400931static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400932 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400933 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400934 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400935};
936
937static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400938 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400939 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400940 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400941};
942
Fred Isamane0c2b382011-03-23 13:27:53 +0000943static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
944{
945 if (fl->stripe_type == STRIPE_SPARSE)
946 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
947 else
948 return j;
949}
950
Fred Isamand6d6dc72012-03-08 17:29:35 -0500951/* The generic layer is about to remove the req from the commit list.
952 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500953 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400954static void
Fred Isamanea2cf222012-04-20 14:47:53 -0400955filelayout_clear_request_commit(struct nfs_page *req,
956 struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +0000957{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400958 struct pnfs_layout_segment *freeme = NULL;
Trond Myklebust8dd37752012-03-15 17:16:40 -0400959
Fred Isamanea2cf222012-04-20 14:47:53 -0400960 spin_lock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400961 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
962 goto out;
Fred Isamanea2cf222012-04-20 14:47:53 -0400963 cinfo->ds->nwritten--;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500964 if (list_is_singular(&req->wb_list)) {
Fred Isamanea2cf222012-04-20 14:47:53 -0400965 struct pnfs_commit_bucket *bucket;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500966
Fred Isaman799ba8d2012-04-20 14:47:38 -0400967 bucket = list_first_entry(&req->wb_list,
Fred Isamanea2cf222012-04-20 14:47:53 -0400968 struct pnfs_commit_bucket,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400969 written);
970 freeme = bucket->wlseg;
971 bucket->wlseg = NULL;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500972 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400973out:
Fred Isamanea2cf222012-04-20 14:47:53 -0400974 nfs_request_remove_commit_list(req, cinfo);
975 spin_unlock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400976 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500977}
978
979static struct list_head *
980filelayout_choose_commit_list(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -0400981 struct pnfs_layout_segment *lseg,
982 struct nfs_commit_info *cinfo)
Fred Isamand6d6dc72012-03-08 17:29:35 -0500983{
Fred Isamane0c2b382011-03-23 13:27:53 +0000984 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
985 u32 i, j;
986 struct list_head *list;
Fred Isamanea2cf222012-04-20 14:47:53 -0400987 struct pnfs_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +0000988
Fred Isamand6d6dc72012-03-08 17:29:35 -0500989 if (fl->commit_through_mds)
Fred Isamanea2cf222012-04-20 14:47:53 -0400990 return &cinfo->mds->list;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500991
Fred Isamane0c2b382011-03-23 13:27:53 +0000992 /* Note that we are calling nfs4_fl_calc_j_index on each page
993 * that ends up being committed to a data server. An attractive
994 * alternative is to add a field to nfs_write_data and nfs_page
995 * to store the value calculated in filelayout_write_pagelist
996 * and just use that here.
997 */
Fred Isamanb5542842012-04-20 14:47:43 -0400998 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
Fred Isamane0c2b382011-03-23 13:27:53 +0000999 i = select_bucket_index(fl, j);
Fred Isamanea2cf222012-04-20 14:47:53 -04001000 buckets = cinfo->ds->buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001001 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +00001002 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001003 /* Non-empty buckets hold a reference on the lseg. That ref
1004 * is normally transferred to the COMMIT call and released
1005 * there. It could also be released if the last req is pulled
1006 * off due to a rewrite, in which case it will be done in
Fred Isaman799ba8d2012-04-20 14:47:38 -04001007 * filelayout_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -05001008 */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001009 buckets[i].wlseg = get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001010 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001011 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamanea2cf222012-04-20 14:47:53 -04001012 cinfo->ds->nwritten++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001013 return list;
1014}
1015
Trond Myklebust8dd37752012-03-15 17:16:40 -04001016static void
1017filelayout_mark_request_commit(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -04001018 struct pnfs_layout_segment *lseg,
1019 struct nfs_commit_info *cinfo)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001020{
1021 struct list_head *list;
1022
Fred Isamanea2cf222012-04-20 14:47:53 -04001023 list = filelayout_choose_commit_list(req, lseg, cinfo);
1024 nfs_request_add_commit_list(req, list, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001025}
1026
Fred Isamane0c2b382011-03-23 13:27:53 +00001027static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1028{
1029 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1030
1031 if (flseg->stripe_type == STRIPE_SPARSE)
1032 return i;
1033 else
1034 return nfs4_fl_calc_ds_index(lseg, i);
1035}
1036
1037static struct nfs_fh *
1038select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1039{
1040 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1041
1042 if (flseg->stripe_type == STRIPE_SPARSE) {
1043 if (flseg->num_fh == 1)
1044 i = 0;
1045 else if (flseg->num_fh == 0)
1046 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1047 return NULL;
1048 }
1049 return flseg->fh_array[i];
1050}
1051
Fred Isaman0b7c0152012-04-20 14:47:39 -04001052static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
Fred Isamane0c2b382011-03-23 13:27:53 +00001053{
1054 struct pnfs_layout_segment *lseg = data->lseg;
1055 struct nfs4_pnfs_ds *ds;
1056 u32 idx;
1057 struct nfs_fh *fh;
1058
1059 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1060 ds = nfs4_fl_prepare_ds(lseg, idx);
1061 if (!ds) {
Fred Isamane0c2b382011-03-23 13:27:53 +00001062 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001063 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +00001064 return -EAGAIN;
1065 }
Andy Adamson3a7936c2012-04-27 17:53:51 -04001066 dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
1067 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
Fred Isaman0b7c0152012-04-20 14:47:39 -04001068 data->commit_done_cb = filelayout_commit_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -04001069 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamane0c2b382011-03-23 13:27:53 +00001070 data->ds_clp = ds->ds_clp;
1071 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1072 if (fh)
1073 data->args.fh = fh;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001074 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001075 &filelayout_commit_call_ops, how,
1076 RPC_TASK_SOFTCONN);
Fred Isamane0c2b382011-03-23 13:27:53 +00001077}
1078
Trond Myklebust8dd37752012-03-15 17:16:40 -04001079static int
Fred Isaman1763da12012-04-20 14:47:57 -04001080transfer_commit_list(struct list_head *src, struct list_head *dst,
1081 struct nfs_commit_info *cinfo, int max)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001082{
Trond Myklebust8dd37752012-03-15 17:16:40 -04001083 struct nfs_page *req, *tmp;
1084 int ret = 0;
1085
1086 list_for_each_entry_safe(req, tmp, src, wb_list) {
1087 if (!nfs_lock_request(req))
1088 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -04001089 if (cond_resched_lock(cinfo->lock))
Trond Myklebust3b3be882012-03-17 11:59:30 -04001090 list_safe_reset_next(req, tmp, wb_list);
Fred Isamanea2cf222012-04-20 14:47:53 -04001091 nfs_request_remove_commit_list(req, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001092 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1093 nfs_list_add_request(req, dst);
1094 ret++;
Fred Isaman1763da12012-04-20 14:47:57 -04001095 if ((ret == max) && !cinfo->dreq)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001096 break;
1097 }
Fred Isaman1763da12012-04-20 14:47:57 -04001098 return ret;
1099}
1100
1101static int
1102filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
1103 struct nfs_commit_info *cinfo,
1104 int max)
1105{
1106 struct list_head *src = &bucket->written;
1107 struct list_head *dst = &bucket->committing;
1108 int ret;
1109
1110 ret = transfer_commit_list(src, dst, cinfo, max);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001111 if (ret) {
Fred Isamanea2cf222012-04-20 14:47:53 -04001112 cinfo->ds->nwritten -= ret;
1113 cinfo->ds->ncommitting += ret;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001114 bucket->clseg = bucket->wlseg;
1115 if (list_empty(src))
1116 bucket->wlseg = NULL;
1117 else
1118 get_lseg(bucket->clseg);
1119 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001120 return ret;
1121}
1122
Fred Isamand6d6dc72012-03-08 17:29:35 -05001123/* Move reqs from written to committing lists, returning count of number moved.
Fred Isamanea2cf222012-04-20 14:47:53 -04001124 * Note called with cinfo->lock held.
Fred Isamand6d6dc72012-03-08 17:29:35 -05001125 */
Fred Isamanea2cf222012-04-20 14:47:53 -04001126static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1127 int max)
Fred Isamand6d6dc72012-03-08 17:29:35 -05001128{
Fred Isamand6d6dc72012-03-08 17:29:35 -05001129 int i, rv = 0, cnt;
1130
Fred Isamanea2cf222012-04-20 14:47:53 -04001131 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1132 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1133 cinfo, max);
Fred Isamand6d6dc72012-03-08 17:29:35 -05001134 max -= cnt;
1135 rv += cnt;
1136 }
Fred Isamand6d6dc72012-03-08 17:29:35 -05001137 return rv;
1138}
1139
Fred Isaman1763da12012-04-20 14:47:57 -04001140/* Pull everything off the committing lists and dump into @dst */
1141static void filelayout_recover_commit_reqs(struct list_head *dst,
1142 struct nfs_commit_info *cinfo)
1143{
1144 struct pnfs_commit_bucket *b;
1145 int i;
1146
1147 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1148 * only called on single thread per dreq.
1149 * Can't take the lock because need to do put_lseg
1150 */
1151 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1152 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1153 BUG_ON(!list_empty(&b->written));
1154 put_lseg(b->wlseg);
1155 b->wlseg = NULL;
1156 }
1157 }
1158 cinfo->ds->nwritten = 0;
1159}
1160
Trond Myklebust9390f422012-03-16 13:52:45 -04001161static unsigned int
Fred Isamanea2cf222012-04-20 14:47:53 -04001162alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +00001163{
Fred Isamanea2cf222012-04-20 14:47:53 -04001164 struct pnfs_ds_commit_info *fl_cinfo;
1165 struct pnfs_commit_bucket *bucket;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001166 struct nfs_commit_data *data;
Fred Isamane0c2b382011-03-23 13:27:53 +00001167 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -04001168 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001169
Fred Isamanea2cf222012-04-20 14:47:53 -04001170 fl_cinfo = cinfo->ds;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001171 bucket = fl_cinfo->buckets;
1172 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1173 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001174 continue;
1175 data = nfs_commitdata_alloc();
1176 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001177 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001178 data->ds_commit_index = i;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001179 data->lseg = bucket->clseg;
1180 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001181 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001182 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001183 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001184
Trond Myklebust9390f422012-03-16 13:52:45 -04001185 /* Clean up on error */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001186 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1187 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001188 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -04001189 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001190 put_lseg(bucket->clseg);
1191 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001192 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001193 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001194 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001195}
1196
1197/* This follows nfs_commit_list pretty closely */
1198static int
1199filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
Fred Isamanea2cf222012-04-20 14:47:53 -04001200 int how, struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +00001201{
Fred Isaman0b7c0152012-04-20 14:47:39 -04001202 struct nfs_commit_data *data, *tmp;
Fred Isamane0c2b382011-03-23 13:27:53 +00001203 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001204 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001205
1206 if (!list_empty(mds_pages)) {
1207 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001208 if (data != NULL) {
1209 data->lseg = NULL;
1210 list_add(&data->pages, &list);
1211 nreq++;
1212 } else
Fred Isamanea2cf222012-04-20 14:47:53 -04001213 nfs_retry_commit(mds_pages, NULL, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001214 }
1215
Fred Isamanea2cf222012-04-20 14:47:53 -04001216 nreq += alloc_ds_commits(cinfo, &list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001217
1218 if (nreq == 0) {
Fred Isamanf453a542012-04-20 14:47:54 -04001219 cinfo->completion_ops->error_cleanup(NFS_I(inode));
Trond Myklebust9390f422012-03-16 13:52:45 -04001220 goto out;
1221 }
1222
Fred Isamanea2cf222012-04-20 14:47:53 -04001223 atomic_add(nreq, &cinfo->mds->rpcs_out);
Fred Isamane0c2b382011-03-23 13:27:53 +00001224
1225 list_for_each_entry_safe(data, tmp, &list, pages) {
1226 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001227 if (!data->lseg) {
Fred Isamanf453a542012-04-20 14:47:54 -04001228 nfs_init_commit(data, mds_pages, NULL, cinfo);
Fred Isaman0b7c0152012-04-20 14:47:39 -04001229 nfs_initiate_commit(NFS_CLIENT(inode), data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001230 data->mds_ops, how, 0);
Fred Isamane0c2b382011-03-23 13:27:53 +00001231 } else {
Fred Isamanea2cf222012-04-20 14:47:53 -04001232 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001233
Fred Isamanea2cf222012-04-20 14:47:53 -04001234 buckets = cinfo->ds->buckets;
Fred Isamanf453a542012-04-20 14:47:54 -04001235 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001236 filelayout_initiate_commit(data, how);
1237 }
1238 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001239out:
Fred Isamanea2cf222012-04-20 14:47:53 -04001240 cinfo->ds->ncommitting = 0;
Trond Myklebust9390f422012-03-16 13:52:45 -04001241 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001242}
1243
Benny Halevy1775bc32011-05-20 13:47:33 +02001244static void
1245filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1246{
1247 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1248}
1249
Fred Isaman799ba8d2012-04-20 14:47:38 -04001250static struct pnfs_layout_hdr *
1251filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1252{
1253 struct nfs4_filelayout *flo;
1254
1255 flo = kzalloc(sizeof(*flo), gfp_flags);
1256 return &flo->generic_hdr;
1257}
1258
1259static void
1260filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1261{
1262 kfree(FILELAYOUT_FROM_HDR(lo));
1263}
1264
Fred Isamanea2cf222012-04-20 14:47:53 -04001265static struct pnfs_ds_commit_info *
1266filelayout_get_ds_info(struct inode *inode)
1267{
Fred Isamandf011742012-04-24 14:50:34 -04001268 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1269
1270 if (layout == NULL)
1271 return NULL;
1272 else
1273 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
Fred Isamanea2cf222012-04-20 14:47:53 -04001274}
1275
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001276static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001277 .id = LAYOUT_NFSV4_1_FILES,
1278 .name = "LAYOUT_NFSV4_1_FILES",
1279 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001280 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1281 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001282 .alloc_lseg = filelayout_alloc_lseg,
1283 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001284 .pg_read_ops = &filelayout_pg_read_ops,
1285 .pg_write_ops = &filelayout_pg_write_ops,
Fred Isamanea2cf222012-04-20 14:47:53 -04001286 .get_ds_info = &filelayout_get_ds_info,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001287 .mark_request_commit = filelayout_mark_request_commit,
1288 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001289 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isaman1763da12012-04-20 14:47:57 -04001290 .recover_commit_reqs = filelayout_recover_commit_reqs,
Fred Isamane0c2b382011-03-23 13:27:53 +00001291 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001292 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001293 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001294 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001295};
1296
1297static int __init nfs4filelayout_init(void)
1298{
1299 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1300 __func__);
1301 return pnfs_register_layoutdriver(&filelayout_type);
1302}
1303
1304static void __exit nfs4filelayout_exit(void)
1305{
1306 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1307 __func__);
1308 pnfs_unregister_layoutdriver(&filelayout_type);
1309}
1310
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001311MODULE_ALIAS("nfs-layouttype4-1");
1312
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001313module_init(nfs4filelayout_init);
1314module_exit(nfs4filelayout_exit);