blob: eb8eb00f3b52bc83428e45723c46b078cd0dc097 [file] [log] [blame]
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001/*
2 * Module for the pnfs nfs4 file layout driver.
3 * Defines all I/O and Policy interface operations, plus code
4 * to register itself with the pNFS client.
5 *
6 * Copyright (c) 2002
7 * The Regents of the University of Michigan
8 * All Rights Reserved
9 *
10 * Dean Hildebrand <dhildebz@umich.edu>
11 *
12 * Permission is granted to use, copy, create derivative works, and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the University of Michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. If
17 * the above copyright notice or any other identification of the
18 * University of Michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
20 *
21 * This software is provided as is, without representation or warranty
22 * of any kind either express or implied, including without limitation
23 * the implied warranties of merchantability, fitness for a particular
24 * purpose, or noninfringement. The Regents of the University of
25 * Michigan shall not be liable for any damages, including special,
26 * indirect, incidental, or consequential damages, with respect to any
27 * claim arising out of or in connection with the use of the software,
28 * even if it has been or is hereafter advised of the possibility of
29 * such damages.
30 */
31
32#include <linux/nfs_fs.h>
Benny Halevy19345cb2011-06-19 18:33:46 -040033#include <linux/nfs_page.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040034#include <linux/module.h>
Andy Adamson16b374c2010-10-20 00:18:04 -040035
Weston Andros Adamson0a702192012-02-17 13:15:24 -050036#include <linux/sunrpc/metrics.h>
37
Andy Adamson16b374c2010-10-20 00:18:04 -040038#include "internal.h"
Andy Adamson9cb81962012-03-07 10:49:41 -050039#include "delegation.h"
Andy Adamson16b374c2010-10-20 00:18:04 -040040#include "nfs4filelayout.h"
Dean Hildebrand7ab672c2010-10-20 00:18:00 -040041
42#define NFSDBG_FACILITY NFSDBG_PNFS_LD
43
44MODULE_LICENSE("GPL");
45MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
46MODULE_DESCRIPTION("The NFSv4 file layout driver");
47
Andy Adamsoncbdabc72011-03-01 01:34:20 +000048#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
49
Fred Isamancfe7f412011-03-01 01:34:18 +000050static loff_t
51filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
52 loff_t offset)
53{
54 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
Chris Metcalf3476f112011-08-11 13:54:28 -070055 u64 stripe_no;
56 u32 rem;
Fred Isamancfe7f412011-03-01 01:34:18 +000057
58 offset -= flseg->pattern_offset;
Chris Metcalf3476f112011-08-11 13:54:28 -070059 stripe_no = div_u64(offset, stripe_width);
60 div_u64_rem(offset, flseg->stripe_unit, &rem);
Fred Isamancfe7f412011-03-01 01:34:18 +000061
Chris Metcalf3476f112011-08-11 13:54:28 -070062 return stripe_no * flseg->stripe_unit + rem;
Fred Isamancfe7f412011-03-01 01:34:18 +000063}
64
65/* This function is used by the layout driver to calculate the
66 * offset of the file on the dserver based on whether the
67 * layout type is STRIPE_DENSE or STRIPE_SPARSE
68 */
69static loff_t
70filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
71{
72 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
73
74 switch (flseg->stripe_type) {
75 case STRIPE_SPARSE:
76 return offset;
77
78 case STRIPE_DENSE:
79 return filelayout_get_dense_offset(flseg, offset);
80 }
81
82 BUG();
83}
84
Andy 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 Adamsoncbdabc72011-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 Adamsoncbdabc72011-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 Adamsoncbdabc72011-03-01 01:34:20 +0000144 if (task->tk_status >= 0)
145 return 0;
Andy Adamsoncbdabc72011-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 Adamsoncbdabc72011-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 Adamsoncbdabc72011-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 Adamsone7dd79af2012-04-27 17:53:46 -0400199 /* fall through */
200 default:
201 dprintk("%s Retry through MDS. Error %d\n", __func__,
202 task->tk_status);
203 return -NFS4ERR_RESET_TO_MDS;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000204 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500205out:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000206 task->tk_status = 0;
207 return -EAGAIN;
Andy Adamson9cb81962012-03-07 10:49:41 -0500208wait_on_recovery:
209 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
210 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
211 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
212 goto out;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000213}
214
215/* NFS_PROTO call done callback routines */
216
217static int filelayout_read_done_cb(struct rpc_task *task,
218 struct nfs_read_data *data)
219{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400220 struct nfs_pgio_header *hdr = data->header;
221 int err;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000222
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400223 err = filelayout_async_handle_error(task, data->args.context->state,
224 data->ds_clp, hdr->lseg);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000225
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400226 switch (err) {
227 case -NFS4ERR_RESET_TO_MDS:
228 filelayout_reset_read(data);
229 return task->tk_status;
230 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700231 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000232 return -EAGAIN;
233 }
234
235 return 0;
236}
237
Andy Adamson16b374c2010-10-20 00:18:04 -0400238/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000239 * We reference the rpc_cred of the first WRITE that triggers the need for
240 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
241 * rfc5661 is not clear about which credential should be used.
242 */
243static void
244filelayout_set_layoutcommit(struct nfs_write_data *wdata)
245{
Fred Isamancd841602012-04-20 14:47:44 -0400246 struct nfs_pgio_header *hdr = wdata->header;
247
248 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
Andy Adamson863a3c62011-03-23 13:27:54 +0000249 wdata->res.verf->committed == NFS_FILE_SYNC)
250 return;
251
252 pnfs_set_layoutcommit(wdata);
Fred Isamancd841602012-04-20 14:47:44 -0400253 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
254 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000255}
256
257/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000258 * Call ops for the async read/write cases
259 * In the case of dense layouts, the offset needs to be reset to its
260 * original value.
261 */
262static void filelayout_read_prepare(struct rpc_task *task, void *data)
263{
Fred Isamancd12ae32012-04-20 14:47:42 -0400264 struct nfs_read_data *rdata = data;
Andy Adamson0ad2f372012-04-27 17:53:48 -0400265 struct pnfs_layout_segment *lseg = rdata->header->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000266
Andy Adamson0ad2f372012-04-27 17:53:48 -0400267 if (filelayout_test_devid_invalid(FILELAYOUT_DEVID_NODE(lseg))) {
268 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
269 filelayout_reset_read(rdata);
270 rpc_exit(task, 0);
271 return;
272 }
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000273 rdata->read_done_cb = filelayout_read_done_cb;
274
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000275 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
276 &rdata->args.seq_args, &rdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500277 task))
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000278 return;
279
280 rpc_call_start(task);
281}
282
283static void filelayout_read_call_done(struct rpc_task *task, void *data)
284{
Fred Isamancd12ae32012-04-20 14:47:42 -0400285 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000286
287 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
288
Andy Adamson0ad2f372012-04-27 17:53:48 -0400289 if (test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
290 return;
291
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000292 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400293 rdata->header->mds_ops->rpc_call_done(task, data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000294}
295
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500296static void filelayout_read_count_stats(struct rpc_task *task, void *data)
297{
Fred Isamancd12ae32012-04-20 14:47:42 -0400298 struct nfs_read_data *rdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500299
Fred Isamancd841602012-04-20 14:47:44 -0400300 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500301}
302
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000303static void filelayout_read_release(void *data)
304{
Fred Isamancd12ae32012-04-20 14:47:42 -0400305 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000306
Andy Adamson3a7936c2012-04-27 17:53:51 -0400307 if (!test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
308 nfs_put_client(rdata->ds_clp);
Fred Isamancd841602012-04-20 14:47:44 -0400309 rdata->header->mds_ops->rpc_release(data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000310}
311
Fred Isamana69aef12011-03-03 15:13:47 +0000312static int filelayout_write_done_cb(struct rpc_task *task,
313 struct nfs_write_data *data)
314{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400315 struct nfs_pgio_header *hdr = data->header;
316 int err;
Fred Isamana69aef12011-03-03 15:13:47 +0000317
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400318 err = filelayout_async_handle_error(task, data->args.context->state,
319 data->ds_clp, hdr->lseg);
320
321 switch (err) {
322 case -NFS4ERR_RESET_TO_MDS:
323 filelayout_reset_write(data);
324 return task->tk_status;
325 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700326 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000327 return -EAGAIN;
328 }
329
Andy Adamson863a3c62011-03-23 13:27:54 +0000330 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000331 return 0;
332}
333
Fred Isamane0c2b382011-03-23 13:27:53 +0000334/* Fake up some data that will cause nfs_commit_release to retry the writes. */
Fred Isaman0b7c0152012-04-20 14:47:39 -0400335static void prepare_to_resend_writes(struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000336{
337 struct nfs_page *first = nfs_list_entry(data->pages.next);
338
339 data->task.tk_status = 0;
340 memcpy(data->verf.verifier, first->wb_verf.verifier,
341 sizeof(first->wb_verf.verifier));
342 data->verf.verifier[0]++; /* ensure verifier mismatch */
343}
344
345static int filelayout_commit_done_cb(struct rpc_task *task,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400346 struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000347{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400348 int err;
Fred Isamane0c2b382011-03-23 13:27:53 +0000349
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400350 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
351 data->lseg);
352
353 switch (err) {
354 case -NFS4ERR_RESET_TO_MDS:
355 prepare_to_resend_writes(data);
356 return -EAGAIN;
357 case -EAGAIN:
358 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000359 return -EAGAIN;
360 }
361
362 return 0;
363}
364
Fred Isamana69aef12011-03-03 15:13:47 +0000365static void filelayout_write_prepare(struct rpc_task *task, void *data)
366{
Fred Isamancd12ae32012-04-20 14:47:42 -0400367 struct nfs_write_data *wdata = data;
Andy Adamson0ad2f372012-04-27 17:53:48 -0400368 struct pnfs_layout_segment *lseg = wdata->header->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000369
Andy Adamson0ad2f372012-04-27 17:53:48 -0400370 if (filelayout_test_devid_invalid(FILELAYOUT_DEVID_NODE(lseg))) {
371 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
372 filelayout_reset_write(wdata);
373 rpc_exit(task, 0);
374 return;
375 }
Fred Isamana69aef12011-03-03 15:13:47 +0000376 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
377 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500378 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000379 return;
380
381 rpc_call_start(task);
382}
383
384static void filelayout_write_call_done(struct rpc_task *task, void *data)
385{
Fred Isamancd12ae32012-04-20 14:47:42 -0400386 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000387
Andy Adamson0ad2f372012-04-27 17:53:48 -0400388 if (test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
389 return;
390
Fred Isamana69aef12011-03-03 15:13:47 +0000391 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400392 wdata->header->mds_ops->rpc_call_done(task, data);
Fred Isamana69aef12011-03-03 15:13:47 +0000393}
394
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500395static void filelayout_write_count_stats(struct rpc_task *task, void *data)
396{
Fred Isamancd12ae32012-04-20 14:47:42 -0400397 struct nfs_write_data *wdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500398
Fred Isamancd841602012-04-20 14:47:44 -0400399 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500400}
401
Fred Isamana69aef12011-03-03 15:13:47 +0000402static void filelayout_write_release(void *data)
403{
Fred Isamancd12ae32012-04-20 14:47:42 -0400404 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000405
Andy Adamson3a7936c2012-04-27 17:53:51 -0400406 if (!test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
407 nfs_put_client(wdata->ds_clp);
Fred Isamancd841602012-04-20 14:47:44 -0400408 wdata->header->mds_ops->rpc_release(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000409}
410
Fred Isaman0b7c0152012-04-20 14:47:39 -0400411static void filelayout_commit_prepare(struct rpc_task *task, void *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000412{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400413 struct nfs_commit_data *wdata = data;
Fred Isamane0c2b382011-03-23 13:27:53 +0000414
Fred Isaman0b7c0152012-04-20 14:47:39 -0400415 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
416 &wdata->args.seq_args, &wdata->res.seq_res,
417 task))
418 return;
419
420 rpc_call_start(task);
421}
422
423static void filelayout_write_commit_done(struct rpc_task *task, void *data)
424{
425 struct nfs_commit_data *wdata = data;
426
427 /* Note this may cause RPC to be resent */
428 wdata->mds_ops->rpc_call_done(task, data);
429}
430
431static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
432{
433 struct nfs_commit_data *cdata = data;
434
435 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
436}
437
438static void filelayout_commit_release(void *calldata)
439{
440 struct nfs_commit_data *data = calldata;
441
Fred Isamanf453a542012-04-20 14:47:54 -0400442 data->completion_ops->completion(data);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400443 put_lseg(data->lseg);
Andy Adamson3a7936c2012-04-27 17:53:51 -0400444 nfs_put_client(data->ds_clp);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400445 nfs_commitdata_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000446}
447
Trond Myklebust17280172012-03-11 13:11:00 -0400448static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000449 .rpc_call_prepare = filelayout_read_prepare,
450 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500451 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000452 .rpc_release = filelayout_read_release,
453};
454
Trond Myklebust17280172012-03-11 13:11:00 -0400455static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000456 .rpc_call_prepare = filelayout_write_prepare,
457 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500458 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000459 .rpc_release = filelayout_write_release,
460};
461
Trond Myklebust17280172012-03-11 13:11:00 -0400462static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isaman0b7c0152012-04-20 14:47:39 -0400463 .rpc_call_prepare = filelayout_commit_prepare,
464 .rpc_call_done = filelayout_write_commit_done,
465 .rpc_count_stats = filelayout_commit_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000466 .rpc_release = filelayout_commit_release,
467};
468
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000469static enum pnfs_try_status
470filelayout_read_pagelist(struct nfs_read_data *data)
471{
Fred Isamancd841602012-04-20 14:47:44 -0400472 struct nfs_pgio_header *hdr = data->header;
473 struct pnfs_layout_segment *lseg = hdr->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000474 struct nfs4_pnfs_ds *ds;
475 loff_t offset = data->args.offset;
476 u32 j, idx;
477 struct nfs_fh *fh;
478 int status;
479
480 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
Fred Isamancd841602012-04-20 14:47:44 -0400481 __func__, hdr->inode->i_ino,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000482 data->args.pgbase, (size_t)data->args.count, offset);
483
484 /* Retrieve the correct rpc_client for the byte range */
485 j = nfs4_fl_calc_j_index(lseg, offset);
486 idx = nfs4_fl_calc_ds_index(lseg, j);
487 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400488 if (!ds)
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000489 return PNFS_NOT_ATTEMPTED;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400490 dprintk("%s USE DS: %s cl_count %d\n", __func__,
491 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000492
493 /* No multipath support. Use first DS */
Andy Adamson3a7936c2012-04-27 17:53:51 -0400494 atomic_inc(&ds->ds_clp->cl_count);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000495 data->ds_clp = ds->ds_clp;
496 fh = nfs4_fl_select_ds_fh(lseg, j);
497 if (fh)
498 data->args.fh = fh;
499
500 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
501 data->mds_offset = offset;
502
503 /* Perform an asynchronous read to ds */
Fred Isamanc5996c42012-04-20 14:47:41 -0400504 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400505 &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000506 BUG_ON(status != 0);
507 return PNFS_ATTEMPTED;
508}
509
Fred Isamana69aef12011-03-03 15:13:47 +0000510/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000511static enum pnfs_try_status
512filelayout_write_pagelist(struct nfs_write_data *data, int sync)
513{
Fred Isamancd841602012-04-20 14:47:44 -0400514 struct nfs_pgio_header *hdr = data->header;
515 struct pnfs_layout_segment *lseg = hdr->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000516 struct nfs4_pnfs_ds *ds;
517 loff_t offset = data->args.offset;
518 u32 j, idx;
519 struct nfs_fh *fh;
520 int status;
521
522 /* Retrieve the correct rpc_client for the byte range */
523 j = nfs4_fl_calc_j_index(lseg, offset);
524 idx = nfs4_fl_calc_ds_index(lseg, j);
525 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400526 if (!ds)
Fred Isamana69aef12011-03-03 15:13:47 +0000527 return PNFS_NOT_ATTEMPTED;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400528 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
529 __func__, hdr->inode->i_ino, sync, (size_t) data->args.count,
530 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Fred Isamana69aef12011-03-03 15:13:47 +0000531
Fred Isamana69aef12011-03-03 15:13:47 +0000532 data->write_done_cb = filelayout_write_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400533 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamana69aef12011-03-03 15:13:47 +0000534 data->ds_clp = ds->ds_clp;
535 fh = nfs4_fl_select_ds_fh(lseg, j);
536 if (fh)
537 data->args.fh = fh;
538 /*
539 * Get the file offset on the dserver. Set the write offset to
540 * this offset and save the original offset.
541 */
542 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000543
544 /* Perform an asynchronous write */
Fred Isamanc5996c42012-04-20 14:47:41 -0400545 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400546 &filelayout_write_call_ops, sync,
547 RPC_TASK_SOFTCONN);
Fred Isamana69aef12011-03-03 15:13:47 +0000548 BUG_ON(status != 0);
549 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000550}
551
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000552/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400553 * filelayout_check_layout()
554 *
555 * Make sure layout segment parameters are sane WRT the device.
556 * At this point no generic layer initialization of the lseg has occurred,
557 * and nothing has been added to the layout_hdr cache.
558 *
559 */
560static int
561filelayout_check_layout(struct pnfs_layout_hdr *lo,
562 struct nfs4_filelayout_segment *fl,
563 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400564 struct nfs4_deviceid *id,
565 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400566{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400567 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400568 struct nfs4_file_layout_dsaddr *dsaddr;
569 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000570 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400571
572 dprintk("--> %s\n", __func__);
573
Andy Adamson7c24d942011-06-13 18:22:38 -0400574 /* FIXME: remove this check when layout segment support is added */
575 if (lgr->range.offset != 0 ||
576 lgr->range.length != NFS4_MAX_UINT64) {
577 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
578 __func__);
579 goto out;
580 }
581
Andy Adamson16b374c2010-10-20 00:18:04 -0400582 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500583 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400584 __func__, fl->pattern_offset);
585 goto out;
586 }
587
Benny Halevy75247af2011-02-22 15:56:01 -0800588 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
589 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400590 __func__, fl->stripe_unit);
591 goto out;
592 }
593
594 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300595 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
596 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400597 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400598 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400599 if (dsaddr == NULL)
600 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400601 } else
602 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400603 /* Found deviceid is being reaped */
604 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
605 goto out_put;
606
Andy Adamson16b374c2010-10-20 00:18:04 -0400607 fl->dsaddr = dsaddr;
608
Chuck Levere4149662011-10-25 12:18:03 -0400609 if (fl->first_stripe_index >= dsaddr->stripe_count) {
610 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400611 __func__, fl->first_stripe_index);
612 goto out_put;
613 }
614
615 if ((fl->stripe_type == STRIPE_SPARSE &&
616 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
617 (fl->stripe_type == STRIPE_DENSE &&
618 fl->num_fh != dsaddr->stripe_count)) {
619 dprintk("%s num_fh %u not valid for given packing\n",
620 __func__, fl->num_fh);
621 goto out_put;
622 }
623
624 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
625 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
626 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
627 nfss->wsize);
628 }
629
630 status = 0;
631out:
632 dprintk("--> %s returns %d\n", __func__, status);
633 return status;
634out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000635 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400636 goto out;
637}
638
639static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
640{
641 int i;
642
643 for (i = 0; i < fl->num_fh; i++) {
644 if (!fl->fh_array[i])
645 break;
646 kfree(fl->fh_array[i]);
647 }
648 kfree(fl->fh_array);
649 fl->fh_array = NULL;
650}
651
652static void
653_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
654{
655 filelayout_free_fh_array(fl);
656 kfree(fl);
657}
658
659static int
660filelayout_decode_layout(struct pnfs_layout_hdr *flo,
661 struct nfs4_filelayout_segment *fl,
662 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400663 struct nfs4_deviceid *id,
664 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400665{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400666 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400667 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400668 struct page *scratch;
669 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400670 uint32_t nfl_util;
671 int i;
672
673 dprintk("%s: set_layout_map Begin\n", __func__);
674
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400675 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400676 if (!scratch)
677 return -ENOMEM;
678
Benny Halevyf7da7a12011-05-19 14:16:47 -0400679 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400680 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
681
682 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
683 * num_fh (4) */
684 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
685 if (unlikely(!p))
686 goto out_err;
687
Andy Adamson16b374c2010-10-20 00:18:04 -0400688 memcpy(id, p, sizeof(*id));
689 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400690 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400691
692 nfl_util = be32_to_cpup(p++);
693 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
694 fl->commit_through_mds = 1;
695 if (nfl_util & NFL4_UFLG_DENSE)
696 fl->stripe_type = STRIPE_DENSE;
697 else
698 fl->stripe_type = STRIPE_SPARSE;
699 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
700
701 fl->first_stripe_index = be32_to_cpup(p++);
702 p = xdr_decode_hyper(p, &fl->pattern_offset);
703 fl->num_fh = be32_to_cpup(p++);
704
705 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
706 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
707 fl->pattern_offset);
708
Andy Adamsoncec765c2011-06-13 18:36:17 -0400709 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
710 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400711 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400712 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400713 goto out_err;
714
Andy Adamsoncec765c2011-06-13 18:36:17 -0400715 if (fl->num_fh > 0) {
716 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
717 gfp_flags);
718 if (!fl->fh_array)
719 goto out_err;
720 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400721
722 for (i = 0; i < fl->num_fh; i++) {
723 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400724 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400725 if (!fl->fh_array[i])
726 goto out_err_free;
727
728 p = xdr_inline_decode(&stream, 4);
729 if (unlikely(!p))
730 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400731 fl->fh_array[i]->size = be32_to_cpup(p++);
732 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500733 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400734 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400735 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400736 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400737
738 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
739 if (unlikely(!p))
740 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400741 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400742 dprintk("DEBUG: %s: fh len %d\n", __func__,
743 fl->fh_array[i]->size);
744 }
745
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400746 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400747 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400748
749out_err_free:
750 filelayout_free_fh_array(fl);
751out_err:
752 __free_page(scratch);
753 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400754}
755
Fred Isamanc8795132011-03-23 13:27:49 +0000756static void
757filelayout_free_lseg(struct pnfs_layout_segment *lseg)
758{
759 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
760
761 dprintk("--> %s\n", __func__);
762 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400763 /* This assumes a single RW lseg */
764 if (lseg->pls_range.iomode == IOMODE_RW) {
765 struct nfs4_filelayout *flo;
766
767 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
768 flo->commit_info.nbuckets = 0;
769 kfree(flo->commit_info.buckets);
770 flo->commit_info.buckets = NULL;
771 }
Fred Isamanc8795132011-03-23 13:27:49 +0000772 _filelayout_free_lseg(fl);
773}
774
Fred Isaman799ba8d2012-04-20 14:47:38 -0400775static int
776filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
Fred Isamanea2cf222012-04-20 14:47:53 -0400777 struct nfs_commit_info *cinfo,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400778 gfp_t gfp_flags)
779{
780 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
Fred Isamanea2cf222012-04-20 14:47:53 -0400781 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400782 int size;
783
784 if (fl->commit_through_mds)
785 return 0;
Fred Isamanea2cf222012-04-20 14:47:53 -0400786 if (cinfo->ds->nbuckets != 0) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400787 /* This assumes there is only one IOMODE_RW lseg. What
788 * we really want to do is have a layout_hdr level
789 * dictionary of <multipath_list4, fh> keys, each
790 * associated with a struct list_head, populated by calls
791 * to filelayout_write_pagelist().
792 * */
793 return 0;
794 }
795
796 size = (fl->stripe_type == STRIPE_SPARSE) ?
797 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
798
Fred Isamanea2cf222012-04-20 14:47:53 -0400799 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
Fred Isaman799ba8d2012-04-20 14:47:38 -0400800 gfp_flags);
801 if (!buckets)
802 return -ENOMEM;
803 else {
804 int i;
805
Fred Isamanea2cf222012-04-20 14:47:53 -0400806 spin_lock(cinfo->lock);
807 if (cinfo->ds->nbuckets != 0)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400808 kfree(buckets);
809 else {
Fred Isamanea2cf222012-04-20 14:47:53 -0400810 cinfo->ds->buckets = buckets;
811 cinfo->ds->nbuckets = size;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400812 for (i = 0; i < size; i++) {
813 INIT_LIST_HEAD(&buckets[i].written);
814 INIT_LIST_HEAD(&buckets[i].committing);
815 }
816 }
Fred Isamanea2cf222012-04-20 14:47:53 -0400817 spin_unlock(cinfo->lock);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400818 return 0;
819 }
820}
821
Andy Adamson16b374c2010-10-20 00:18:04 -0400822static struct pnfs_layout_segment *
823filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400824 struct nfs4_layoutget_res *lgr,
825 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400826{
827 struct nfs4_filelayout_segment *fl;
828 int rc;
829 struct nfs4_deviceid id;
830
831 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400832 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400833 if (!fl)
834 return NULL;
835
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400836 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
837 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400838 _filelayout_free_lseg(fl);
839 return NULL;
840 }
841 return &fl->generic_hdr;
842}
843
Fred Isaman94ad1c82011-03-01 01:34:14 +0000844/*
845 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
846 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300847 * return true : coalesce page
848 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000849 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400850static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000851filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
852 struct nfs_page *req)
853{
854 u64 p_stripe, r_stripe;
855 u32 stripe_unit;
856
Benny Halevy19345cb2011-06-19 18:33:46 -0400857 if (!pnfs_generic_pg_test(pgio, prev, req) ||
858 !nfs_generic_pg_test(pgio, prev, req))
859 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300860
Fred Isamanb5542842012-04-20 14:47:43 -0400861 p_stripe = (u64)req_offset(prev);
862 r_stripe = (u64)req_offset(req);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000863 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
864
865 do_div(p_stripe, stripe_unit);
866 do_div(r_stripe, stripe_unit);
867
868 return (p_stripe == r_stripe);
869}
870
Trond Myklebust17280172012-03-11 13:11:00 -0400871static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400872filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
873 struct nfs_page *req)
874{
875 BUG_ON(pgio->pg_lseg != NULL);
876
Fred Isaman1825a0d2012-04-20 19:55:31 -0400877 if (req->wb_offset != req->wb_pgbase) {
878 /*
879 * Handling unaligned pages is difficult, because have to
880 * somehow split a req in two in certain cases in the
881 * pg.test code. Avoid this by just not using pnfs
882 * in this case.
883 */
884 nfs_pageio_reset_read_mds(pgio);
885 return;
886 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400887 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
888 req->wb_context,
889 0,
890 NFS4_MAX_UINT64,
891 IOMODE_READ,
892 GFP_KERNEL);
893 /* If no lseg, fall back to read through mds */
894 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400895 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400896}
897
Trond Myklebust17280172012-03-11 13:11:00 -0400898static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400899filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
900 struct nfs_page *req)
901{
Fred Isamanea2cf222012-04-20 14:47:53 -0400902 struct nfs_commit_info cinfo;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400903 int status;
904
Andy Adamson7c24d942011-06-13 18:22:38 -0400905 BUG_ON(pgio->pg_lseg != NULL);
906
Fred Isaman1825a0d2012-04-20 19:55:31 -0400907 if (req->wb_offset != req->wb_pgbase)
908 goto out_mds;
Andy Adamson7c24d942011-06-13 18:22:38 -0400909 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
910 req->wb_context,
911 0,
912 NFS4_MAX_UINT64,
913 IOMODE_RW,
914 GFP_NOFS);
915 /* If no lseg, fall back to write through mds */
916 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400917 goto out_mds;
Fred Isamanea2cf222012-04-20 14:47:53 -0400918 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
919 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400920 if (status < 0) {
921 put_lseg(pgio->pg_lseg);
922 pgio->pg_lseg = NULL;
923 goto out_mds;
924 }
925 return;
926out_mds:
927 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400928}
929
Trond Myklebust1751c362011-06-10 13:30:23 -0400930static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400931 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400932 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400933 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400934};
935
936static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400937 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400938 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400939 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400940};
941
Fred Isamane0c2b382011-03-23 13:27:53 +0000942static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
943{
944 if (fl->stripe_type == STRIPE_SPARSE)
945 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
946 else
947 return j;
948}
949
Fred Isamand6d6dc72012-03-08 17:29:35 -0500950/* The generic layer is about to remove the req from the commit list.
951 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500952 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400953static void
Fred Isamanea2cf222012-04-20 14:47:53 -0400954filelayout_clear_request_commit(struct nfs_page *req,
955 struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +0000956{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400957 struct pnfs_layout_segment *freeme = NULL;
Trond Myklebust8dd37752012-03-15 17:16:40 -0400958
Fred Isamanea2cf222012-04-20 14:47:53 -0400959 spin_lock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400960 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
961 goto out;
Fred Isamanea2cf222012-04-20 14:47:53 -0400962 cinfo->ds->nwritten--;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500963 if (list_is_singular(&req->wb_list)) {
Fred Isamanea2cf222012-04-20 14:47:53 -0400964 struct pnfs_commit_bucket *bucket;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500965
Fred Isaman799ba8d2012-04-20 14:47:38 -0400966 bucket = list_first_entry(&req->wb_list,
Fred Isamanea2cf222012-04-20 14:47:53 -0400967 struct pnfs_commit_bucket,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400968 written);
969 freeme = bucket->wlseg;
970 bucket->wlseg = NULL;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500971 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400972out:
Fred Isamanea2cf222012-04-20 14:47:53 -0400973 nfs_request_remove_commit_list(req, cinfo);
974 spin_unlock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400975 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500976}
977
978static struct list_head *
979filelayout_choose_commit_list(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -0400980 struct pnfs_layout_segment *lseg,
981 struct nfs_commit_info *cinfo)
Fred Isamand6d6dc72012-03-08 17:29:35 -0500982{
Fred Isamane0c2b382011-03-23 13:27:53 +0000983 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
984 u32 i, j;
985 struct list_head *list;
Fred Isamanea2cf222012-04-20 14:47:53 -0400986 struct pnfs_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +0000987
Fred Isamand6d6dc72012-03-08 17:29:35 -0500988 if (fl->commit_through_mds)
Fred Isamanea2cf222012-04-20 14:47:53 -0400989 return &cinfo->mds->list;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500990
Fred Isamane0c2b382011-03-23 13:27:53 +0000991 /* Note that we are calling nfs4_fl_calc_j_index on each page
992 * that ends up being committed to a data server. An attractive
993 * alternative is to add a field to nfs_write_data and nfs_page
994 * to store the value calculated in filelayout_write_pagelist
995 * and just use that here.
996 */
Fred Isamanb5542842012-04-20 14:47:43 -0400997 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
Fred Isamane0c2b382011-03-23 13:27:53 +0000998 i = select_bucket_index(fl, j);
Fred Isamanea2cf222012-04-20 14:47:53 -0400999 buckets = cinfo->ds->buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001000 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +00001001 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001002 /* Non-empty buckets hold a reference on the lseg. That ref
1003 * is normally transferred to the COMMIT call and released
1004 * there. It could also be released if the last req is pulled
1005 * off due to a rewrite, in which case it will be done in
Fred Isaman799ba8d2012-04-20 14:47:38 -04001006 * filelayout_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -05001007 */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001008 buckets[i].wlseg = get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001009 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001010 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamanea2cf222012-04-20 14:47:53 -04001011 cinfo->ds->nwritten++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001012 return list;
1013}
1014
Trond Myklebust8dd37752012-03-15 17:16:40 -04001015static void
1016filelayout_mark_request_commit(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -04001017 struct pnfs_layout_segment *lseg,
1018 struct nfs_commit_info *cinfo)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001019{
1020 struct list_head *list;
1021
Fred Isamanea2cf222012-04-20 14:47:53 -04001022 list = filelayout_choose_commit_list(req, lseg, cinfo);
1023 nfs_request_add_commit_list(req, list, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001024}
1025
Fred Isamane0c2b382011-03-23 13:27:53 +00001026static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1027{
1028 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1029
1030 if (flseg->stripe_type == STRIPE_SPARSE)
1031 return i;
1032 else
1033 return nfs4_fl_calc_ds_index(lseg, i);
1034}
1035
1036static struct nfs_fh *
1037select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1038{
1039 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1040
1041 if (flseg->stripe_type == STRIPE_SPARSE) {
1042 if (flseg->num_fh == 1)
1043 i = 0;
1044 else if (flseg->num_fh == 0)
1045 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1046 return NULL;
1047 }
1048 return flseg->fh_array[i];
1049}
1050
Fred Isaman0b7c0152012-04-20 14:47:39 -04001051static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
Fred Isamane0c2b382011-03-23 13:27:53 +00001052{
1053 struct pnfs_layout_segment *lseg = data->lseg;
1054 struct nfs4_pnfs_ds *ds;
1055 u32 idx;
1056 struct nfs_fh *fh;
1057
1058 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1059 ds = nfs4_fl_prepare_ds(lseg, idx);
1060 if (!ds) {
Fred Isamane0c2b382011-03-23 13:27:53 +00001061 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001062 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +00001063 return -EAGAIN;
1064 }
Andy Adamson3a7936c2012-04-27 17:53:51 -04001065 dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
1066 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
Fred Isaman0b7c0152012-04-20 14:47:39 -04001067 data->commit_done_cb = filelayout_commit_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -04001068 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamane0c2b382011-03-23 13:27:53 +00001069 data->ds_clp = ds->ds_clp;
1070 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1071 if (fh)
1072 data->args.fh = fh;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001073 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001074 &filelayout_commit_call_ops, how,
1075 RPC_TASK_SOFTCONN);
Fred Isamane0c2b382011-03-23 13:27:53 +00001076}
1077
Trond Myklebust8dd37752012-03-15 17:16:40 -04001078static int
Fred Isaman1763da12012-04-20 14:47:57 -04001079transfer_commit_list(struct list_head *src, struct list_head *dst,
1080 struct nfs_commit_info *cinfo, int max)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001081{
Trond Myklebust8dd37752012-03-15 17:16:40 -04001082 struct nfs_page *req, *tmp;
1083 int ret = 0;
1084
1085 list_for_each_entry_safe(req, tmp, src, wb_list) {
1086 if (!nfs_lock_request(req))
1087 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -04001088 if (cond_resched_lock(cinfo->lock))
Trond Myklebust3b3be882012-03-17 11:59:30 -04001089 list_safe_reset_next(req, tmp, wb_list);
Fred Isamanea2cf222012-04-20 14:47:53 -04001090 nfs_request_remove_commit_list(req, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001091 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1092 nfs_list_add_request(req, dst);
1093 ret++;
Fred Isaman1763da12012-04-20 14:47:57 -04001094 if ((ret == max) && !cinfo->dreq)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001095 break;
1096 }
Fred Isaman1763da12012-04-20 14:47:57 -04001097 return ret;
1098}
1099
1100static int
1101filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
1102 struct nfs_commit_info *cinfo,
1103 int max)
1104{
1105 struct list_head *src = &bucket->written;
1106 struct list_head *dst = &bucket->committing;
1107 int ret;
1108
1109 ret = transfer_commit_list(src, dst, cinfo, max);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001110 if (ret) {
Fred Isamanea2cf222012-04-20 14:47:53 -04001111 cinfo->ds->nwritten -= ret;
1112 cinfo->ds->ncommitting += ret;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001113 bucket->clseg = bucket->wlseg;
1114 if (list_empty(src))
1115 bucket->wlseg = NULL;
1116 else
1117 get_lseg(bucket->clseg);
1118 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001119 return ret;
1120}
1121
Fred Isamand6d6dc72012-03-08 17:29:35 -05001122/* Move reqs from written to committing lists, returning count of number moved.
Fred Isamanea2cf222012-04-20 14:47:53 -04001123 * Note called with cinfo->lock held.
Fred Isamand6d6dc72012-03-08 17:29:35 -05001124 */
Fred Isamanea2cf222012-04-20 14:47:53 -04001125static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1126 int max)
Fred Isamand6d6dc72012-03-08 17:29:35 -05001127{
Fred Isamand6d6dc72012-03-08 17:29:35 -05001128 int i, rv = 0, cnt;
1129
Fred Isamanea2cf222012-04-20 14:47:53 -04001130 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1131 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1132 cinfo, max);
Fred Isamand6d6dc72012-03-08 17:29:35 -05001133 max -= cnt;
1134 rv += cnt;
1135 }
Fred Isamand6d6dc72012-03-08 17:29:35 -05001136 return rv;
1137}
1138
Fred Isaman1763da12012-04-20 14:47:57 -04001139/* Pull everything off the committing lists and dump into @dst */
1140static void filelayout_recover_commit_reqs(struct list_head *dst,
1141 struct nfs_commit_info *cinfo)
1142{
1143 struct pnfs_commit_bucket *b;
1144 int i;
1145
1146 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1147 * only called on single thread per dreq.
1148 * Can't take the lock because need to do put_lseg
1149 */
1150 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1151 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1152 BUG_ON(!list_empty(&b->written));
1153 put_lseg(b->wlseg);
1154 b->wlseg = NULL;
1155 }
1156 }
1157 cinfo->ds->nwritten = 0;
1158}
1159
Trond Myklebust9390f422012-03-16 13:52:45 -04001160static unsigned int
Fred Isamanea2cf222012-04-20 14:47:53 -04001161alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +00001162{
Fred Isamanea2cf222012-04-20 14:47:53 -04001163 struct pnfs_ds_commit_info *fl_cinfo;
1164 struct pnfs_commit_bucket *bucket;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001165 struct nfs_commit_data *data;
Fred Isamane0c2b382011-03-23 13:27:53 +00001166 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -04001167 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001168
Fred Isamanea2cf222012-04-20 14:47:53 -04001169 fl_cinfo = cinfo->ds;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001170 bucket = fl_cinfo->buckets;
1171 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1172 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001173 continue;
1174 data = nfs_commitdata_alloc();
1175 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001176 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001177 data->ds_commit_index = i;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001178 data->lseg = bucket->clseg;
1179 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001180 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001181 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001182 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001183
Trond Myklebust9390f422012-03-16 13:52:45 -04001184 /* Clean up on error */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001185 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1186 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001187 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -04001188 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001189 put_lseg(bucket->clseg);
1190 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001191 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001192 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001193 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001194}
1195
1196/* This follows nfs_commit_list pretty closely */
1197static int
1198filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
Fred Isamanea2cf222012-04-20 14:47:53 -04001199 int how, struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +00001200{
Fred Isaman0b7c0152012-04-20 14:47:39 -04001201 struct nfs_commit_data *data, *tmp;
Fred Isamane0c2b382011-03-23 13:27:53 +00001202 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001203 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001204
1205 if (!list_empty(mds_pages)) {
1206 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001207 if (data != NULL) {
1208 data->lseg = NULL;
1209 list_add(&data->pages, &list);
1210 nreq++;
1211 } else
Fred Isamanea2cf222012-04-20 14:47:53 -04001212 nfs_retry_commit(mds_pages, NULL, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001213 }
1214
Fred Isamanea2cf222012-04-20 14:47:53 -04001215 nreq += alloc_ds_commits(cinfo, &list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001216
1217 if (nreq == 0) {
Fred Isamanf453a542012-04-20 14:47:54 -04001218 cinfo->completion_ops->error_cleanup(NFS_I(inode));
Trond Myklebust9390f422012-03-16 13:52:45 -04001219 goto out;
1220 }
1221
Fred Isamanea2cf222012-04-20 14:47:53 -04001222 atomic_add(nreq, &cinfo->mds->rpcs_out);
Fred Isamane0c2b382011-03-23 13:27:53 +00001223
1224 list_for_each_entry_safe(data, tmp, &list, pages) {
1225 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001226 if (!data->lseg) {
Fred Isamanf453a542012-04-20 14:47:54 -04001227 nfs_init_commit(data, mds_pages, NULL, cinfo);
Fred Isaman0b7c0152012-04-20 14:47:39 -04001228 nfs_initiate_commit(NFS_CLIENT(inode), data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001229 data->mds_ops, how, 0);
Fred Isamane0c2b382011-03-23 13:27:53 +00001230 } else {
Fred Isamanea2cf222012-04-20 14:47:53 -04001231 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001232
Fred Isamanea2cf222012-04-20 14:47:53 -04001233 buckets = cinfo->ds->buckets;
Fred Isamanf453a542012-04-20 14:47:54 -04001234 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001235 filelayout_initiate_commit(data, how);
1236 }
1237 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001238out:
Fred Isamanea2cf222012-04-20 14:47:53 -04001239 cinfo->ds->ncommitting = 0;
Trond Myklebust9390f422012-03-16 13:52:45 -04001240 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001241}
1242
Benny Halevy1775bc32011-05-20 13:47:33 +02001243static void
1244filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1245{
1246 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1247}
1248
Fred Isaman799ba8d2012-04-20 14:47:38 -04001249static struct pnfs_layout_hdr *
1250filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1251{
1252 struct nfs4_filelayout *flo;
1253
1254 flo = kzalloc(sizeof(*flo), gfp_flags);
1255 return &flo->generic_hdr;
1256}
1257
1258static void
1259filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1260{
1261 kfree(FILELAYOUT_FROM_HDR(lo));
1262}
1263
Fred Isamanea2cf222012-04-20 14:47:53 -04001264static struct pnfs_ds_commit_info *
1265filelayout_get_ds_info(struct inode *inode)
1266{
Fred Isamandf011742012-04-24 14:50:34 -04001267 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1268
1269 if (layout == NULL)
1270 return NULL;
1271 else
1272 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
Fred Isamanea2cf222012-04-20 14:47:53 -04001273}
1274
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001275static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001276 .id = LAYOUT_NFSV4_1_FILES,
1277 .name = "LAYOUT_NFSV4_1_FILES",
1278 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001279 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1280 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001281 .alloc_lseg = filelayout_alloc_lseg,
1282 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001283 .pg_read_ops = &filelayout_pg_read_ops,
1284 .pg_write_ops = &filelayout_pg_write_ops,
Fred Isamanea2cf222012-04-20 14:47:53 -04001285 .get_ds_info = &filelayout_get_ds_info,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001286 .mark_request_commit = filelayout_mark_request_commit,
1287 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001288 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isaman1763da12012-04-20 14:47:57 -04001289 .recover_commit_reqs = filelayout_recover_commit_reqs,
Fred Isamane0c2b382011-03-23 13:27:53 +00001290 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001291 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001292 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001293 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001294};
1295
1296static int __init nfs4filelayout_init(void)
1297{
1298 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1299 __func__);
1300 return pnfs_register_layoutdriver(&filelayout_type);
1301}
1302
1303static void __exit nfs4filelayout_exit(void)
1304{
1305 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1306 __func__);
1307 pnfs_unregister_layoutdriver(&filelayout_type);
1308}
1309
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001310MODULE_ALIAS("nfs-layouttype4-1");
1311
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001312module_init(nfs4filelayout_init);
1313module_exit(nfs4filelayout_exit);