blob: f8edb14c697ea723985e365c4d52206950ade6d0 [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 Adamson041245c2012-04-27 17:53:53 -0400185 /* Invalidate Layout errors */
186 case -NFS4ERR_PNFS_NO_LAYOUT:
187 case -ESTALE: /* mapped NFS4ERR_STALE */
188 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
189 case -EISDIR: /* mapped NFS4ERR_ISDIR */
190 case -NFS4ERR_FHEXPIRED:
191 case -NFS4ERR_WRONG_TYPE:
192 dprintk("%s Invalid layout error %d\n", __func__,
193 task->tk_status);
194 /*
195 * Destroy layout so new i/o will get a new layout.
196 * Layout will not be destroyed until all current lseg
197 * references are put. Mark layout as invalid to resend failed
198 * i/o and all i/o waiting on the slot table to the MDS until
199 * layout is destroyed and a new valid layout is obtained.
200 */
201 set_bit(NFS_LAYOUT_INVALID,
Andy Adamsond42e7872012-05-22 08:09:28 -0400202 &NFS_I(inode)->layout->plh_flags);
203 pnfs_destroy_layout(NFS_I(inode));
Andy Adamson041245c2012-04-27 17:53:53 -0400204 rpc_wake_up(&tbl->slot_tbl_waitq);
205 goto reset;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400206 /* RPC connection errors */
207 case -ECONNREFUSED:
208 case -EHOSTDOWN:
209 case -EHOSTUNREACH:
210 case -ENETUNREACH:
211 case -EIO:
212 case -ETIMEDOUT:
213 case -EPIPE:
214 dprintk("%s DS connection error %d\n", __func__,
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000215 task->tk_status);
Andy Adamson0a57cda2012-04-27 17:53:50 -0400216 if (!filelayout_test_devid_invalid(devid))
Andy Adamsond42e7872012-05-22 08:09:28 -0400217 _pnfs_return_layout(inode);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400218 filelayout_mark_devid_invalid(devid);
Andy Adamson671fb892012-04-27 17:53:49 -0400219 rpc_wake_up(&tbl->slot_tbl_waitq);
Andy Adamsonb4a29672012-04-27 17:53:52 -0400220 nfs4_ds_disconnect(clp);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400221 /* fall through */
222 default:
Andy Adamson041245c2012-04-27 17:53:53 -0400223reset:
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400224 dprintk("%s Retry through MDS. Error %d\n", __func__,
225 task->tk_status);
226 return -NFS4ERR_RESET_TO_MDS;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000227 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500228out:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000229 task->tk_status = 0;
230 return -EAGAIN;
Andy Adamson9cb81962012-03-07 10:49:41 -0500231wait_on_recovery:
232 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
233 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
234 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
235 goto out;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000236}
237
238/* NFS_PROTO call done callback routines */
239
240static int filelayout_read_done_cb(struct rpc_task *task,
241 struct nfs_read_data *data)
242{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400243 struct nfs_pgio_header *hdr = data->header;
244 int err;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000245
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400246 err = filelayout_async_handle_error(task, data->args.context->state,
247 data->ds_clp, hdr->lseg);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000248
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400249 switch (err) {
250 case -NFS4ERR_RESET_TO_MDS:
251 filelayout_reset_read(data);
252 return task->tk_status;
253 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700254 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000255 return -EAGAIN;
256 }
257
258 return 0;
259}
260
Andy Adamson16b374c2010-10-20 00:18:04 -0400261/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000262 * We reference the rpc_cred of the first WRITE that triggers the need for
263 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
264 * rfc5661 is not clear about which credential should be used.
265 */
266static void
267filelayout_set_layoutcommit(struct nfs_write_data *wdata)
268{
Fred Isamancd841602012-04-20 14:47:44 -0400269 struct nfs_pgio_header *hdr = wdata->header;
270
271 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
Andy Adamson863a3c62011-03-23 13:27:54 +0000272 wdata->res.verf->committed == NFS_FILE_SYNC)
273 return;
274
275 pnfs_set_layoutcommit(wdata);
Fred Isamancd841602012-04-20 14:47:44 -0400276 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
277 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000278}
279
280/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000281 * Call ops for the async read/write cases
282 * In the case of dense layouts, the offset needs to be reset to its
283 * original value.
284 */
285static void filelayout_read_prepare(struct rpc_task *task, void *data)
286{
Fred Isamancd12ae32012-04-20 14:47:42 -0400287 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000288
Andy Adamson041245c2012-04-27 17:53:53 -0400289 if (filelayout_reset_to_mds(rdata->header->lseg)) {
Andy Adamson0ad2f372012-04-27 17:53:48 -0400290 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
291 filelayout_reset_read(rdata);
292 rpc_exit(task, 0);
293 return;
294 }
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000295 rdata->read_done_cb = filelayout_read_done_cb;
296
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000297 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
298 &rdata->args.seq_args, &rdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500299 task))
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000300 return;
301
302 rpc_call_start(task);
303}
304
305static void filelayout_read_call_done(struct rpc_task *task, void *data)
306{
Fred Isamancd12ae32012-04-20 14:47:42 -0400307 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000308
309 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
310
Andy Adamson0ad2f372012-04-27 17:53:48 -0400311 if (test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
312 return;
313
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000314 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400315 rdata->header->mds_ops->rpc_call_done(task, data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000316}
317
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500318static void filelayout_read_count_stats(struct rpc_task *task, void *data)
319{
Fred Isamancd12ae32012-04-20 14:47:42 -0400320 struct nfs_read_data *rdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500321
Fred Isamancd841602012-04-20 14:47:44 -0400322 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500323}
324
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000325static void filelayout_read_release(void *data)
326{
Fred Isamancd12ae32012-04-20 14:47:42 -0400327 struct nfs_read_data *rdata = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000328
Andy Adamson3a7936c2012-04-27 17:53:51 -0400329 if (!test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
330 nfs_put_client(rdata->ds_clp);
Fred Isamancd841602012-04-20 14:47:44 -0400331 rdata->header->mds_ops->rpc_release(data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000332}
333
Fred Isamana69aef12011-03-03 15:13:47 +0000334static int filelayout_write_done_cb(struct rpc_task *task,
335 struct nfs_write_data *data)
336{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400337 struct nfs_pgio_header *hdr = data->header;
338 int err;
Fred Isamana69aef12011-03-03 15:13:47 +0000339
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400340 err = filelayout_async_handle_error(task, data->args.context->state,
341 data->ds_clp, hdr->lseg);
342
343 switch (err) {
344 case -NFS4ERR_RESET_TO_MDS:
345 filelayout_reset_write(data);
346 return task->tk_status;
347 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700348 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000349 return -EAGAIN;
350 }
351
Andy Adamson863a3c62011-03-23 13:27:54 +0000352 filelayout_set_layoutcommit(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000353 return 0;
354}
355
Fred Isamane0c2b382011-03-23 13:27:53 +0000356/* Fake up some data that will cause nfs_commit_release to retry the writes. */
Fred Isaman0b7c0152012-04-20 14:47:39 -0400357static void prepare_to_resend_writes(struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000358{
359 struct nfs_page *first = nfs_list_entry(data->pages.next);
360
361 data->task.tk_status = 0;
362 memcpy(data->verf.verifier, first->wb_verf.verifier,
363 sizeof(first->wb_verf.verifier));
364 data->verf.verifier[0]++; /* ensure verifier mismatch */
365}
366
367static int filelayout_commit_done_cb(struct rpc_task *task,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400368 struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000369{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400370 int err;
Fred Isamane0c2b382011-03-23 13:27:53 +0000371
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400372 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
373 data->lseg);
374
375 switch (err) {
376 case -NFS4ERR_RESET_TO_MDS:
377 prepare_to_resend_writes(data);
378 return -EAGAIN;
379 case -EAGAIN:
380 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000381 return -EAGAIN;
382 }
383
384 return 0;
385}
386
Fred Isamana69aef12011-03-03 15:13:47 +0000387static void filelayout_write_prepare(struct rpc_task *task, void *data)
388{
Fred Isamancd12ae32012-04-20 14:47:42 -0400389 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000390
Andy Adamson041245c2012-04-27 17:53:53 -0400391 if (filelayout_reset_to_mds(wdata->header->lseg)) {
Andy Adamson0ad2f372012-04-27 17:53:48 -0400392 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
393 filelayout_reset_write(wdata);
394 rpc_exit(task, 0);
395 return;
396 }
Fred Isamana69aef12011-03-03 15:13:47 +0000397 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
398 &wdata->args.seq_args, &wdata->res.seq_res,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500399 task))
Fred Isamana69aef12011-03-03 15:13:47 +0000400 return;
401
402 rpc_call_start(task);
403}
404
405static void filelayout_write_call_done(struct rpc_task *task, void *data)
406{
Fred Isamancd12ae32012-04-20 14:47:42 -0400407 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000408
Andy Adamson0ad2f372012-04-27 17:53:48 -0400409 if (test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
410 return;
411
Fred Isamana69aef12011-03-03 15:13:47 +0000412 /* Note this may cause RPC to be resent */
Fred Isamancd841602012-04-20 14:47:44 -0400413 wdata->header->mds_ops->rpc_call_done(task, data);
Fred Isamana69aef12011-03-03 15:13:47 +0000414}
415
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500416static void filelayout_write_count_stats(struct rpc_task *task, void *data)
417{
Fred Isamancd12ae32012-04-20 14:47:42 -0400418 struct nfs_write_data *wdata = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500419
Fred Isamancd841602012-04-20 14:47:44 -0400420 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500421}
422
Fred Isamana69aef12011-03-03 15:13:47 +0000423static void filelayout_write_release(void *data)
424{
Fred Isamancd12ae32012-04-20 14:47:42 -0400425 struct nfs_write_data *wdata = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000426
Andy Adamson3a7936c2012-04-27 17:53:51 -0400427 if (!test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
428 nfs_put_client(wdata->ds_clp);
Fred Isamancd841602012-04-20 14:47:44 -0400429 wdata->header->mds_ops->rpc_release(data);
Fred Isamana69aef12011-03-03 15:13:47 +0000430}
431
Fred Isaman0b7c0152012-04-20 14:47:39 -0400432static void filelayout_commit_prepare(struct rpc_task *task, void *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000433{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400434 struct nfs_commit_data *wdata = data;
Fred Isamane0c2b382011-03-23 13:27:53 +0000435
Fred Isaman0b7c0152012-04-20 14:47:39 -0400436 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
437 &wdata->args.seq_args, &wdata->res.seq_res,
438 task))
439 return;
440
441 rpc_call_start(task);
442}
443
444static void filelayout_write_commit_done(struct rpc_task *task, void *data)
445{
446 struct nfs_commit_data *wdata = data;
447
448 /* Note this may cause RPC to be resent */
449 wdata->mds_ops->rpc_call_done(task, data);
450}
451
452static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
453{
454 struct nfs_commit_data *cdata = data;
455
456 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
457}
458
459static void filelayout_commit_release(void *calldata)
460{
461 struct nfs_commit_data *data = calldata;
462
Fred Isamanf453a542012-04-20 14:47:54 -0400463 data->completion_ops->completion(data);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400464 put_lseg(data->lseg);
Andy Adamson3a7936c2012-04-27 17:53:51 -0400465 nfs_put_client(data->ds_clp);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400466 nfs_commitdata_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +0000467}
468
Trond Myklebust17280172012-03-11 13:11:00 -0400469static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000470 .rpc_call_prepare = filelayout_read_prepare,
471 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500472 .rpc_count_stats = filelayout_read_count_stats,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000473 .rpc_release = filelayout_read_release,
474};
475
Trond Myklebust17280172012-03-11 13:11:00 -0400476static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000477 .rpc_call_prepare = filelayout_write_prepare,
478 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500479 .rpc_count_stats = filelayout_write_count_stats,
Fred Isamana69aef12011-03-03 15:13:47 +0000480 .rpc_release = filelayout_write_release,
481};
482
Trond Myklebust17280172012-03-11 13:11:00 -0400483static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isaman0b7c0152012-04-20 14:47:39 -0400484 .rpc_call_prepare = filelayout_commit_prepare,
485 .rpc_call_done = filelayout_write_commit_done,
486 .rpc_count_stats = filelayout_commit_count_stats,
Fred Isamane0c2b382011-03-23 13:27:53 +0000487 .rpc_release = filelayout_commit_release,
488};
489
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000490static enum pnfs_try_status
491filelayout_read_pagelist(struct nfs_read_data *data)
492{
Fred Isamancd841602012-04-20 14:47:44 -0400493 struct nfs_pgio_header *hdr = data->header;
494 struct pnfs_layout_segment *lseg = hdr->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000495 struct nfs4_pnfs_ds *ds;
496 loff_t offset = data->args.offset;
497 u32 j, idx;
498 struct nfs_fh *fh;
499 int status;
500
501 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
Fred Isamancd841602012-04-20 14:47:44 -0400502 __func__, hdr->inode->i_ino,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000503 data->args.pgbase, (size_t)data->args.count, offset);
504
505 /* Retrieve the correct rpc_client for the byte range */
506 j = nfs4_fl_calc_j_index(lseg, offset);
507 idx = nfs4_fl_calc_ds_index(lseg, j);
508 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400509 if (!ds)
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000510 return PNFS_NOT_ATTEMPTED;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400511 dprintk("%s USE DS: %s cl_count %d\n", __func__,
512 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000513
514 /* No multipath support. Use first DS */
Andy Adamson3a7936c2012-04-27 17:53:51 -0400515 atomic_inc(&ds->ds_clp->cl_count);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000516 data->ds_clp = ds->ds_clp;
517 fh = nfs4_fl_select_ds_fh(lseg, j);
518 if (fh)
519 data->args.fh = fh;
520
521 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
522 data->mds_offset = offset;
523
524 /* Perform an asynchronous read to ds */
Fred Isamanc5996c42012-04-20 14:47:41 -0400525 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400526 &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000527 BUG_ON(status != 0);
528 return PNFS_ATTEMPTED;
529}
530
Fred Isamana69aef12011-03-03 15:13:47 +0000531/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000532static enum pnfs_try_status
533filelayout_write_pagelist(struct nfs_write_data *data, int sync)
534{
Fred Isamancd841602012-04-20 14:47:44 -0400535 struct nfs_pgio_header *hdr = data->header;
536 struct pnfs_layout_segment *lseg = hdr->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000537 struct nfs4_pnfs_ds *ds;
538 loff_t offset = data->args.offset;
539 u32 j, idx;
540 struct nfs_fh *fh;
541 int status;
542
543 /* Retrieve the correct rpc_client for the byte range */
544 j = nfs4_fl_calc_j_index(lseg, offset);
545 idx = nfs4_fl_calc_ds_index(lseg, j);
546 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400547 if (!ds)
Fred Isamana69aef12011-03-03 15:13:47 +0000548 return PNFS_NOT_ATTEMPTED;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400549 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
550 __func__, hdr->inode->i_ino, sync, (size_t) data->args.count,
551 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Fred Isamana69aef12011-03-03 15:13:47 +0000552
Fred Isamana69aef12011-03-03 15:13:47 +0000553 data->write_done_cb = filelayout_write_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400554 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamana69aef12011-03-03 15:13:47 +0000555 data->ds_clp = ds->ds_clp;
556 fh = nfs4_fl_select_ds_fh(lseg, j);
557 if (fh)
558 data->args.fh = fh;
559 /*
560 * Get the file offset on the dserver. Set the write offset to
561 * this offset and save the original offset.
562 */
563 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000564
565 /* Perform an asynchronous write */
Fred Isamanc5996c42012-04-20 14:47:41 -0400566 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -0400567 &filelayout_write_call_ops, sync,
568 RPC_TASK_SOFTCONN);
Fred Isamana69aef12011-03-03 15:13:47 +0000569 BUG_ON(status != 0);
570 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000571}
572
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000573/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400574 * filelayout_check_layout()
575 *
576 * Make sure layout segment parameters are sane WRT the device.
577 * At this point no generic layer initialization of the lseg has occurred,
578 * and nothing has been added to the layout_hdr cache.
579 *
580 */
581static int
582filelayout_check_layout(struct pnfs_layout_hdr *lo,
583 struct nfs4_filelayout_segment *fl,
584 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400585 struct nfs4_deviceid *id,
586 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400587{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400588 struct nfs4_deviceid_node *d;
Andy Adamson16b374c2010-10-20 00:18:04 -0400589 struct nfs4_file_layout_dsaddr *dsaddr;
590 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000591 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400592
593 dprintk("--> %s\n", __func__);
594
Andy Adamson7c24d942011-06-13 18:22:38 -0400595 /* FIXME: remove this check when layout segment support is added */
596 if (lgr->range.offset != 0 ||
597 lgr->range.length != NFS4_MAX_UINT64) {
598 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
599 __func__);
600 goto out;
601 }
602
Andy Adamson16b374c2010-10-20 00:18:04 -0400603 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500604 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400605 __func__, fl->pattern_offset);
606 goto out;
607 }
608
Benny Halevy75247af2011-02-22 15:56:01 -0800609 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
610 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400611 __func__, fl->stripe_unit);
612 goto out;
613 }
614
615 /* find and reference the deviceid */
Benny Halevy35c8bb52011-05-24 18:04:02 +0300616 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
617 NFS_SERVER(lo->plh_inode)->nfs_client, id);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400618 if (d == NULL) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400619 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400620 if (dsaddr == NULL)
621 goto out;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400622 } else
623 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400624 /* Found deviceid is being reaped */
625 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
626 goto out_put;
627
Andy Adamson16b374c2010-10-20 00:18:04 -0400628 fl->dsaddr = dsaddr;
629
Chuck Levere4149662011-10-25 12:18:03 -0400630 if (fl->first_stripe_index >= dsaddr->stripe_count) {
631 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400632 __func__, fl->first_stripe_index);
633 goto out_put;
634 }
635
636 if ((fl->stripe_type == STRIPE_SPARSE &&
637 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
638 (fl->stripe_type == STRIPE_DENSE &&
639 fl->num_fh != dsaddr->stripe_count)) {
640 dprintk("%s num_fh %u not valid for given packing\n",
641 __func__, fl->num_fh);
642 goto out_put;
643 }
644
645 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
646 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
647 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
648 nfss->wsize);
649 }
650
651 status = 0;
652out:
653 dprintk("--> %s returns %d\n", __func__, status);
654 return status;
655out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000656 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400657 goto out;
658}
659
660static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
661{
662 int i;
663
664 for (i = 0; i < fl->num_fh; i++) {
665 if (!fl->fh_array[i])
666 break;
667 kfree(fl->fh_array[i]);
668 }
669 kfree(fl->fh_array);
670 fl->fh_array = NULL;
671}
672
673static void
674_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
675{
676 filelayout_free_fh_array(fl);
677 kfree(fl);
678}
679
680static int
681filelayout_decode_layout(struct pnfs_layout_hdr *flo,
682 struct nfs4_filelayout_segment *fl,
683 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400684 struct nfs4_deviceid *id,
685 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400686{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400687 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400688 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400689 struct page *scratch;
690 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400691 uint32_t nfl_util;
692 int i;
693
694 dprintk("%s: set_layout_map Begin\n", __func__);
695
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400696 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400697 if (!scratch)
698 return -ENOMEM;
699
Benny Halevyf7da7a12011-05-19 14:16:47 -0400700 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400701 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
702
703 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
704 * num_fh (4) */
705 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
706 if (unlikely(!p))
707 goto out_err;
708
Andy Adamson16b374c2010-10-20 00:18:04 -0400709 memcpy(id, p, sizeof(*id));
710 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400711 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400712
713 nfl_util = be32_to_cpup(p++);
714 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
715 fl->commit_through_mds = 1;
716 if (nfl_util & NFL4_UFLG_DENSE)
717 fl->stripe_type = STRIPE_DENSE;
718 else
719 fl->stripe_type = STRIPE_SPARSE;
720 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
721
722 fl->first_stripe_index = be32_to_cpup(p++);
723 p = xdr_decode_hyper(p, &fl->pattern_offset);
724 fl->num_fh = be32_to_cpup(p++);
725
726 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
727 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
728 fl->pattern_offset);
729
Andy Adamsoncec765c2011-06-13 18:36:17 -0400730 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
731 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400732 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400733 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400734 goto out_err;
735
Andy Adamsoncec765c2011-06-13 18:36:17 -0400736 if (fl->num_fh > 0) {
737 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
738 gfp_flags);
739 if (!fl->fh_array)
740 goto out_err;
741 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400742
743 for (i = 0; i < fl->num_fh; i++) {
744 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400745 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400746 if (!fl->fh_array[i])
747 goto out_err_free;
748
749 p = xdr_inline_decode(&stream, 4);
750 if (unlikely(!p))
751 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400752 fl->fh_array[i]->size = be32_to_cpup(p++);
753 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500754 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400755 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400756 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400757 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400758
759 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
760 if (unlikely(!p))
761 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400762 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400763 dprintk("DEBUG: %s: fh len %d\n", __func__,
764 fl->fh_array[i]->size);
765 }
766
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400767 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400768 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400769
770out_err_free:
771 filelayout_free_fh_array(fl);
772out_err:
773 __free_page(scratch);
774 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400775}
776
Fred Isamanc8795132011-03-23 13:27:49 +0000777static void
778filelayout_free_lseg(struct pnfs_layout_segment *lseg)
779{
780 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
781
782 dprintk("--> %s\n", __func__);
783 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400784 /* This assumes a single RW lseg */
785 if (lseg->pls_range.iomode == IOMODE_RW) {
786 struct nfs4_filelayout *flo;
787
788 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
789 flo->commit_info.nbuckets = 0;
790 kfree(flo->commit_info.buckets);
791 flo->commit_info.buckets = NULL;
792 }
Fred Isamanc8795132011-03-23 13:27:49 +0000793 _filelayout_free_lseg(fl);
794}
795
Fred Isaman799ba8d2012-04-20 14:47:38 -0400796static int
797filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
Fred Isamanea2cf222012-04-20 14:47:53 -0400798 struct nfs_commit_info *cinfo,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400799 gfp_t gfp_flags)
800{
801 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
Fred Isamanea2cf222012-04-20 14:47:53 -0400802 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400803 int size;
804
805 if (fl->commit_through_mds)
806 return 0;
Fred Isamanea2cf222012-04-20 14:47:53 -0400807 if (cinfo->ds->nbuckets != 0) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400808 /* This assumes there is only one IOMODE_RW lseg. What
809 * we really want to do is have a layout_hdr level
810 * dictionary of <multipath_list4, fh> keys, each
811 * associated with a struct list_head, populated by calls
812 * to filelayout_write_pagelist().
813 * */
814 return 0;
815 }
816
817 size = (fl->stripe_type == STRIPE_SPARSE) ?
818 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
819
Fred Isamanea2cf222012-04-20 14:47:53 -0400820 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
Fred Isaman799ba8d2012-04-20 14:47:38 -0400821 gfp_flags);
822 if (!buckets)
823 return -ENOMEM;
824 else {
825 int i;
826
Fred Isamanea2cf222012-04-20 14:47:53 -0400827 spin_lock(cinfo->lock);
828 if (cinfo->ds->nbuckets != 0)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400829 kfree(buckets);
830 else {
Fred Isamanea2cf222012-04-20 14:47:53 -0400831 cinfo->ds->buckets = buckets;
832 cinfo->ds->nbuckets = size;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400833 for (i = 0; i < size; i++) {
834 INIT_LIST_HEAD(&buckets[i].written);
835 INIT_LIST_HEAD(&buckets[i].committing);
836 }
837 }
Fred Isamanea2cf222012-04-20 14:47:53 -0400838 spin_unlock(cinfo->lock);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400839 return 0;
840 }
841}
842
Andy Adamson16b374c2010-10-20 00:18:04 -0400843static struct pnfs_layout_segment *
844filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400845 struct nfs4_layoutget_res *lgr,
846 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400847{
848 struct nfs4_filelayout_segment *fl;
849 int rc;
850 struct nfs4_deviceid id;
851
852 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400853 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400854 if (!fl)
855 return NULL;
856
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400857 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
858 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400859 _filelayout_free_lseg(fl);
860 return NULL;
861 }
862 return &fl->generic_hdr;
863}
864
Fred Isaman94ad1c82011-03-01 01:34:14 +0000865/*
866 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
867 *
Benny Halevy18ad0a92011-05-25 21:03:56 +0300868 * return true : coalesce page
869 * return false : don't coalesce page
Fred Isaman94ad1c82011-03-01 01:34:14 +0000870 */
Trond Myklebust1751c362011-06-10 13:30:23 -0400871static bool
Fred Isaman94ad1c82011-03-01 01:34:14 +0000872filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
873 struct nfs_page *req)
874{
875 u64 p_stripe, r_stripe;
876 u32 stripe_unit;
877
Benny Halevy19345cb2011-06-19 18:33:46 -0400878 if (!pnfs_generic_pg_test(pgio, prev, req) ||
879 !nfs_generic_pg_test(pgio, prev, req))
880 return false;
Benny Halevy89a58e32011-05-25 20:54:40 +0300881
Fred Isamanb5542842012-04-20 14:47:43 -0400882 p_stripe = (u64)req_offset(prev);
883 r_stripe = (u64)req_offset(req);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000884 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
885
886 do_div(p_stripe, stripe_unit);
887 do_div(r_stripe, stripe_unit);
888
889 return (p_stripe == r_stripe);
890}
891
Trond Myklebust17280172012-03-11 13:11:00 -0400892static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400893filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
894 struct nfs_page *req)
895{
896 BUG_ON(pgio->pg_lseg != NULL);
897
Fred Isaman1825a0d2012-04-20 19:55:31 -0400898 if (req->wb_offset != req->wb_pgbase) {
899 /*
900 * Handling unaligned pages is difficult, because have to
901 * somehow split a req in two in certain cases in the
902 * pg.test code. Avoid this by just not using pnfs
903 * in this case.
904 */
905 nfs_pageio_reset_read_mds(pgio);
906 return;
907 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400908 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
909 req->wb_context,
910 0,
911 NFS4_MAX_UINT64,
912 IOMODE_READ,
913 GFP_KERNEL);
914 /* If no lseg, fall back to read through mds */
915 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400916 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400917}
918
Trond Myklebust17280172012-03-11 13:11:00 -0400919static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400920filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
921 struct nfs_page *req)
922{
Fred Isamanea2cf222012-04-20 14:47:53 -0400923 struct nfs_commit_info cinfo;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400924 int status;
925
Andy Adamson7c24d942011-06-13 18:22:38 -0400926 BUG_ON(pgio->pg_lseg != NULL);
927
Fred Isaman1825a0d2012-04-20 19:55:31 -0400928 if (req->wb_offset != req->wb_pgbase)
929 goto out_mds;
Andy Adamson7c24d942011-06-13 18:22:38 -0400930 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
931 req->wb_context,
932 0,
933 NFS4_MAX_UINT64,
934 IOMODE_RW,
935 GFP_NOFS);
936 /* If no lseg, fall back to write through mds */
937 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400938 goto out_mds;
Fred Isamanea2cf222012-04-20 14:47:53 -0400939 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
940 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400941 if (status < 0) {
942 put_lseg(pgio->pg_lseg);
943 pgio->pg_lseg = NULL;
944 goto out_mds;
945 }
946 return;
947out_mds:
948 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400949}
950
Trond Myklebust1751c362011-06-10 13:30:23 -0400951static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400952 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400953 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400954 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400955};
956
957static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400958 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400959 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400960 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400961};
962
Fred Isamane0c2b382011-03-23 13:27:53 +0000963static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
964{
965 if (fl->stripe_type == STRIPE_SPARSE)
966 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
967 else
968 return j;
969}
970
Fred Isamand6d6dc72012-03-08 17:29:35 -0500971/* The generic layer is about to remove the req from the commit list.
972 * If this will make the bucket empty, it will need to put the lseg reference.
Fred Isamand6d6dc72012-03-08 17:29:35 -0500973 */
Trond Myklebust8dd37752012-03-15 17:16:40 -0400974static void
Fred Isamanea2cf222012-04-20 14:47:53 -0400975filelayout_clear_request_commit(struct nfs_page *req,
976 struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +0000977{
Trond Myklebust8dd37752012-03-15 17:16:40 -0400978 struct pnfs_layout_segment *freeme = NULL;
Trond Myklebust8dd37752012-03-15 17:16:40 -0400979
Fred Isamanea2cf222012-04-20 14:47:53 -0400980 spin_lock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400981 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
982 goto out;
Fred Isamanea2cf222012-04-20 14:47:53 -0400983 cinfo->ds->nwritten--;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500984 if (list_is_singular(&req->wb_list)) {
Fred Isamanea2cf222012-04-20 14:47:53 -0400985 struct pnfs_commit_bucket *bucket;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500986
Fred Isaman799ba8d2012-04-20 14:47:38 -0400987 bucket = list_first_entry(&req->wb_list,
Fred Isamanea2cf222012-04-20 14:47:53 -0400988 struct pnfs_commit_bucket,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400989 written);
990 freeme = bucket->wlseg;
991 bucket->wlseg = NULL;
Fred Isamand6d6dc72012-03-08 17:29:35 -0500992 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400993out:
Fred Isamanea2cf222012-04-20 14:47:53 -0400994 nfs_request_remove_commit_list(req, cinfo);
995 spin_unlock(cinfo->lock);
Trond Myklebust8dd37752012-03-15 17:16:40 -0400996 put_lseg(freeme);
Fred Isamand6d6dc72012-03-08 17:29:35 -0500997}
998
999static struct list_head *
1000filelayout_choose_commit_list(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -04001001 struct pnfs_layout_segment *lseg,
1002 struct nfs_commit_info *cinfo)
Fred Isamand6d6dc72012-03-08 17:29:35 -05001003{
Fred Isamane0c2b382011-03-23 13:27:53 +00001004 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
1005 u32 i, j;
1006 struct list_head *list;
Fred Isamanea2cf222012-04-20 14:47:53 -04001007 struct pnfs_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +00001008
Fred Isamand6d6dc72012-03-08 17:29:35 -05001009 if (fl->commit_through_mds)
Fred Isamanea2cf222012-04-20 14:47:53 -04001010 return &cinfo->mds->list;
Fred Isamand6d6dc72012-03-08 17:29:35 -05001011
Fred Isamane0c2b382011-03-23 13:27:53 +00001012 /* Note that we are calling nfs4_fl_calc_j_index on each page
1013 * that ends up being committed to a data server. An attractive
1014 * alternative is to add a field to nfs_write_data and nfs_page
1015 * to store the value calculated in filelayout_write_pagelist
1016 * and just use that here.
1017 */
Fred Isamanb5542842012-04-20 14:47:43 -04001018 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
Fred Isamane0c2b382011-03-23 13:27:53 +00001019 i = select_bucket_index(fl, j);
Fred Isamanea2cf222012-04-20 14:47:53 -04001020 buckets = cinfo->ds->buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001021 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +00001022 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -05001023 /* Non-empty buckets hold a reference on the lseg. That ref
1024 * is normally transferred to the COMMIT call and released
1025 * there. It could also be released if the last req is pulled
1026 * off due to a rewrite, in which case it will be done in
Fred Isaman799ba8d2012-04-20 14:47:38 -04001027 * filelayout_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -05001028 */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001029 buckets[i].wlseg = get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +00001030 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001031 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamanea2cf222012-04-20 14:47:53 -04001032 cinfo->ds->nwritten++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001033 return list;
1034}
1035
Trond Myklebust8dd37752012-03-15 17:16:40 -04001036static void
1037filelayout_mark_request_commit(struct nfs_page *req,
Fred Isamanea2cf222012-04-20 14:47:53 -04001038 struct pnfs_layout_segment *lseg,
1039 struct nfs_commit_info *cinfo)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001040{
1041 struct list_head *list;
1042
Fred Isamanea2cf222012-04-20 14:47:53 -04001043 list = filelayout_choose_commit_list(req, lseg, cinfo);
1044 nfs_request_add_commit_list(req, list, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001045}
1046
Fred Isamane0c2b382011-03-23 13:27:53 +00001047static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1048{
1049 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1050
1051 if (flseg->stripe_type == STRIPE_SPARSE)
1052 return i;
1053 else
1054 return nfs4_fl_calc_ds_index(lseg, i);
1055}
1056
1057static struct nfs_fh *
1058select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1059{
1060 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1061
1062 if (flseg->stripe_type == STRIPE_SPARSE) {
1063 if (flseg->num_fh == 1)
1064 i = 0;
1065 else if (flseg->num_fh == 0)
1066 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1067 return NULL;
1068 }
1069 return flseg->fh_array[i];
1070}
1071
Fred Isaman0b7c0152012-04-20 14:47:39 -04001072static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
Fred Isamane0c2b382011-03-23 13:27:53 +00001073{
1074 struct pnfs_layout_segment *lseg = data->lseg;
1075 struct nfs4_pnfs_ds *ds;
1076 u32 idx;
1077 struct nfs_fh *fh;
1078
1079 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1080 ds = nfs4_fl_prepare_ds(lseg, idx);
1081 if (!ds) {
Fred Isamane0c2b382011-03-23 13:27:53 +00001082 prepare_to_resend_writes(data);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001083 filelayout_commit_release(data);
Fred Isamane0c2b382011-03-23 13:27:53 +00001084 return -EAGAIN;
1085 }
Andy Adamson3a7936c2012-04-27 17:53:51 -04001086 dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
1087 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
Fred Isaman0b7c0152012-04-20 14:47:39 -04001088 data->commit_done_cb = filelayout_commit_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -04001089 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamane0c2b382011-03-23 13:27:53 +00001090 data->ds_clp = ds->ds_clp;
1091 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1092 if (fh)
1093 data->args.fh = fh;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001094 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001095 &filelayout_commit_call_ops, how,
1096 RPC_TASK_SOFTCONN);
Fred Isamane0c2b382011-03-23 13:27:53 +00001097}
1098
Trond Myklebust8dd37752012-03-15 17:16:40 -04001099static int
Fred Isaman1763da12012-04-20 14:47:57 -04001100transfer_commit_list(struct list_head *src, struct list_head *dst,
1101 struct nfs_commit_info *cinfo, int max)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001102{
Trond Myklebust8dd37752012-03-15 17:16:40 -04001103 struct nfs_page *req, *tmp;
1104 int ret = 0;
1105
1106 list_for_each_entry_safe(req, tmp, src, wb_list) {
1107 if (!nfs_lock_request(req))
1108 continue;
Trond Myklebust53b8ee32012-05-22 16:36:27 -04001109 kref_get(&req->wb_kref);
Fred Isamanea2cf222012-04-20 14:47:53 -04001110 if (cond_resched_lock(cinfo->lock))
Trond Myklebust3b3be882012-03-17 11:59:30 -04001111 list_safe_reset_next(req, tmp, wb_list);
Fred Isamanea2cf222012-04-20 14:47:53 -04001112 nfs_request_remove_commit_list(req, cinfo);
Trond Myklebust8dd37752012-03-15 17:16:40 -04001113 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1114 nfs_list_add_request(req, dst);
1115 ret++;
Fred Isaman1763da12012-04-20 14:47:57 -04001116 if ((ret == max) && !cinfo->dreq)
Trond Myklebust8dd37752012-03-15 17:16:40 -04001117 break;
1118 }
Fred Isaman1763da12012-04-20 14:47:57 -04001119 return ret;
1120}
1121
1122static int
1123filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
1124 struct nfs_commit_info *cinfo,
1125 int max)
1126{
1127 struct list_head *src = &bucket->written;
1128 struct list_head *dst = &bucket->committing;
1129 int ret;
1130
1131 ret = transfer_commit_list(src, dst, cinfo, max);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001132 if (ret) {
Fred Isamanea2cf222012-04-20 14:47:53 -04001133 cinfo->ds->nwritten -= ret;
1134 cinfo->ds->ncommitting += ret;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001135 bucket->clseg = bucket->wlseg;
1136 if (list_empty(src))
1137 bucket->wlseg = NULL;
1138 else
1139 get_lseg(bucket->clseg);
1140 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001141 return ret;
1142}
1143
Fred Isamand6d6dc72012-03-08 17:29:35 -05001144/* Move reqs from written to committing lists, returning count of number moved.
Fred Isamanea2cf222012-04-20 14:47:53 -04001145 * Note called with cinfo->lock held.
Fred Isamand6d6dc72012-03-08 17:29:35 -05001146 */
Fred Isamanea2cf222012-04-20 14:47:53 -04001147static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1148 int max)
Fred Isamand6d6dc72012-03-08 17:29:35 -05001149{
Fred Isamand6d6dc72012-03-08 17:29:35 -05001150 int i, rv = 0, cnt;
1151
Fred Isamanea2cf222012-04-20 14:47:53 -04001152 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1153 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1154 cinfo, max);
Fred Isamand6d6dc72012-03-08 17:29:35 -05001155 max -= cnt;
1156 rv += cnt;
1157 }
Fred Isamand6d6dc72012-03-08 17:29:35 -05001158 return rv;
1159}
1160
Fred Isaman1763da12012-04-20 14:47:57 -04001161/* Pull everything off the committing lists and dump into @dst */
1162static void filelayout_recover_commit_reqs(struct list_head *dst,
1163 struct nfs_commit_info *cinfo)
1164{
1165 struct pnfs_commit_bucket *b;
1166 int i;
1167
1168 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1169 * only called on single thread per dreq.
1170 * Can't take the lock because need to do put_lseg
1171 */
1172 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1173 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1174 BUG_ON(!list_empty(&b->written));
1175 put_lseg(b->wlseg);
1176 b->wlseg = NULL;
1177 }
1178 }
1179 cinfo->ds->nwritten = 0;
1180}
1181
Trond Myklebust9390f422012-03-16 13:52:45 -04001182static unsigned int
Fred Isamanea2cf222012-04-20 14:47:53 -04001183alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
Fred Isamane0c2b382011-03-23 13:27:53 +00001184{
Fred Isamanea2cf222012-04-20 14:47:53 -04001185 struct pnfs_ds_commit_info *fl_cinfo;
1186 struct pnfs_commit_bucket *bucket;
Fred Isaman0b7c0152012-04-20 14:47:39 -04001187 struct nfs_commit_data *data;
Fred Isamane0c2b382011-03-23 13:27:53 +00001188 int i, j;
Trond Myklebust9390f422012-03-16 13:52:45 -04001189 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001190
Fred Isamanea2cf222012-04-20 14:47:53 -04001191 fl_cinfo = cinfo->ds;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001192 bucket = fl_cinfo->buckets;
1193 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1194 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001195 continue;
1196 data = nfs_commitdata_alloc();
1197 if (!data)
Trond Myklebust9390f422012-03-16 13:52:45 -04001198 break;
Fred Isamane0c2b382011-03-23 13:27:53 +00001199 data->ds_commit_index = i;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001200 data->lseg = bucket->clseg;
1201 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001202 list_add(&data->pages, list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001203 nreq++;
Fred Isamane0c2b382011-03-23 13:27:53 +00001204 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001205
Trond Myklebust9390f422012-03-16 13:52:45 -04001206 /* Clean up on error */
Fred Isaman799ba8d2012-04-20 14:47:38 -04001207 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1208 if (list_empty(&bucket->committing))
Fred Isamane0c2b382011-03-23 13:27:53 +00001209 continue;
Fred Isamanea2cf222012-04-20 14:47:53 -04001210 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
Fred Isaman799ba8d2012-04-20 14:47:38 -04001211 put_lseg(bucket->clseg);
1212 bucket->clseg = NULL;
Fred Isamane0c2b382011-03-23 13:27:53 +00001213 }
Fred Isamane0c2b382011-03-23 13:27:53 +00001214 /* Caller will clean up entries put on list */
Trond Myklebust9390f422012-03-16 13:52:45 -04001215 return nreq;
Fred Isamane0c2b382011-03-23 13:27:53 +00001216}
1217
1218/* This follows nfs_commit_list pretty closely */
1219static int
1220filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
Fred Isamanea2cf222012-04-20 14:47:53 -04001221 int how, struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +00001222{
Fred Isaman0b7c0152012-04-20 14:47:39 -04001223 struct nfs_commit_data *data, *tmp;
Fred Isamane0c2b382011-03-23 13:27:53 +00001224 LIST_HEAD(list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001225 unsigned int nreq = 0;
Fred Isamane0c2b382011-03-23 13:27:53 +00001226
1227 if (!list_empty(mds_pages)) {
1228 data = nfs_commitdata_alloc();
Trond Myklebust9390f422012-03-16 13:52:45 -04001229 if (data != NULL) {
1230 data->lseg = NULL;
1231 list_add(&data->pages, &list);
1232 nreq++;
1233 } else
Fred Isamanea2cf222012-04-20 14:47:53 -04001234 nfs_retry_commit(mds_pages, NULL, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001235 }
1236
Fred Isamanea2cf222012-04-20 14:47:53 -04001237 nreq += alloc_ds_commits(cinfo, &list);
Trond Myklebust9390f422012-03-16 13:52:45 -04001238
1239 if (nreq == 0) {
Fred Isamanf453a542012-04-20 14:47:54 -04001240 cinfo->completion_ops->error_cleanup(NFS_I(inode));
Trond Myklebust9390f422012-03-16 13:52:45 -04001241 goto out;
1242 }
1243
Fred Isamanea2cf222012-04-20 14:47:53 -04001244 atomic_add(nreq, &cinfo->mds->rpcs_out);
Fred Isamane0c2b382011-03-23 13:27:53 +00001245
1246 list_for_each_entry_safe(data, tmp, &list, pages) {
1247 list_del_init(&data->pages);
Fred Isamane0c2b382011-03-23 13:27:53 +00001248 if (!data->lseg) {
Fred Isamanf453a542012-04-20 14:47:54 -04001249 nfs_init_commit(data, mds_pages, NULL, cinfo);
Fred Isaman0b7c0152012-04-20 14:47:39 -04001250 nfs_initiate_commit(NFS_CLIENT(inode), data,
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001251 data->mds_ops, how, 0);
Fred Isamane0c2b382011-03-23 13:27:53 +00001252 } else {
Fred Isamanea2cf222012-04-20 14:47:53 -04001253 struct pnfs_commit_bucket *buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001254
Fred Isamanea2cf222012-04-20 14:47:53 -04001255 buckets = cinfo->ds->buckets;
Fred Isamanf453a542012-04-20 14:47:54 -04001256 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
Fred Isamane0c2b382011-03-23 13:27:53 +00001257 filelayout_initiate_commit(data, how);
1258 }
1259 }
Trond Myklebust9390f422012-03-16 13:52:45 -04001260out:
Fred Isamanea2cf222012-04-20 14:47:53 -04001261 cinfo->ds->ncommitting = 0;
Trond Myklebust9390f422012-03-16 13:52:45 -04001262 return PNFS_ATTEMPTED;
Fred Isamane0c2b382011-03-23 13:27:53 +00001263}
1264
Benny Halevy1775bc32011-05-20 13:47:33 +02001265static void
1266filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1267{
1268 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1269}
1270
Fred Isaman799ba8d2012-04-20 14:47:38 -04001271static struct pnfs_layout_hdr *
1272filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1273{
1274 struct nfs4_filelayout *flo;
1275
1276 flo = kzalloc(sizeof(*flo), gfp_flags);
1277 return &flo->generic_hdr;
1278}
1279
1280static void
1281filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1282{
1283 kfree(FILELAYOUT_FROM_HDR(lo));
1284}
1285
Fred Isamanea2cf222012-04-20 14:47:53 -04001286static struct pnfs_ds_commit_info *
1287filelayout_get_ds_info(struct inode *inode)
1288{
Fred Isamandf011742012-04-24 14:50:34 -04001289 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1290
1291 if (layout == NULL)
1292 return NULL;
1293 else
1294 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
Fred Isamanea2cf222012-04-20 14:47:53 -04001295}
1296
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001297static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001298 .id = LAYOUT_NFSV4_1_FILES,
1299 .name = "LAYOUT_NFSV4_1_FILES",
1300 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001301 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1302 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001303 .alloc_lseg = filelayout_alloc_lseg,
1304 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001305 .pg_read_ops = &filelayout_pg_read_ops,
1306 .pg_write_ops = &filelayout_pg_write_ops,
Fred Isamanea2cf222012-04-20 14:47:53 -04001307 .get_ds_info = &filelayout_get_ds_info,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001308 .mark_request_commit = filelayout_mark_request_commit,
1309 .clear_request_commit = filelayout_clear_request_commit,
Fred Isamand6d6dc72012-03-08 17:29:35 -05001310 .scan_commit_lists = filelayout_scan_commit_lists,
Fred Isaman1763da12012-04-20 14:47:57 -04001311 .recover_commit_reqs = filelayout_recover_commit_reqs,
Fred Isamane0c2b382011-03-23 13:27:53 +00001312 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001313 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001314 .write_pagelist = filelayout_write_pagelist,
Benny Halevy1775bc32011-05-20 13:47:33 +02001315 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001316};
1317
1318static int __init nfs4filelayout_init(void)
1319{
1320 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1321 __func__);
1322 return pnfs_register_layoutdriver(&filelayout_type);
1323}
1324
1325static void __exit nfs4filelayout_exit(void)
1326{
1327 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1328 __func__);
1329 pnfs_unregister_layoutdriver(&filelayout_type);
1330}
1331
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001332MODULE_ALIAS("nfs-layouttype4-1");
1333
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001334module_init(nfs4filelayout_init);
1335module_exit(nfs4filelayout_exit);