blob: 2af32fc39d600a7345e92c66f1ffa03d20ee8647 [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
Tom Haynesb5968722014-05-12 14:35:52 -070038#include "../nfs4session.h"
39#include "../internal.h"
40#include "../delegation.h"
41#include "filelayout.h"
42#include "../nfs4trace.h"
Dean Hildebrand7ab672c2010-10-20 00:18:00 -040043
44#define NFSDBG_FACILITY NFSDBG_PNFS_LD
45
46MODULE_LICENSE("GPL");
47MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
48MODULE_DESCRIPTION("The NFSv4 file layout driver");
49
Andy Adamsoncbdabc72011-03-01 01:34:20 +000050#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
51
Fred Isamancfe7f412011-03-01 01:34:18 +000052static loff_t
53filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
54 loff_t offset)
55{
56 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
Chris Metcalf3476f112011-08-11 13:54:28 -070057 u64 stripe_no;
58 u32 rem;
Fred Isamancfe7f412011-03-01 01:34:18 +000059
60 offset -= flseg->pattern_offset;
Chris Metcalf3476f112011-08-11 13:54:28 -070061 stripe_no = div_u64(offset, stripe_width);
62 div_u64_rem(offset, flseg->stripe_unit, &rem);
Fred Isamancfe7f412011-03-01 01:34:18 +000063
Chris Metcalf3476f112011-08-11 13:54:28 -070064 return stripe_no * flseg->stripe_unit + rem;
Fred Isamancfe7f412011-03-01 01:34:18 +000065}
66
67/* This function is used by the layout driver to calculate the
68 * offset of the file on the dserver based on whether the
69 * layout type is STRIPE_DENSE or STRIPE_SPARSE
70 */
71static loff_t
72filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
73{
74 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
75
76 switch (flseg->stripe_type) {
77 case STRIPE_SPARSE:
78 return offset;
79
80 case STRIPE_DENSE:
81 return filelayout_get_dense_offset(flseg, offset);
82 }
83
84 BUG();
85}
86
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -040087static void filelayout_reset_write(struct nfs_pgio_header *hdr)
Andy Adamsone7dd79af2012-04-27 17:53:46 -040088{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -040089 struct rpc_task *task = &hdr->task;
Andy Adamsone7dd79af2012-04-27 17:53:46 -040090
91 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
92 dprintk("%s Reset task %5u for i/o through MDS "
Niels de Vos1e8968c2013-12-17 18:20:16 +010093 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -040094 hdr->task.tk_pid,
Bryan Schumaker497826a2012-05-22 10:10:03 -040095 hdr->inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +010096 (unsigned long long)NFS_FILEID(hdr->inode),
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -040097 hdr->args.count,
98 (unsigned long long)hdr->args.offset);
Andy Adamsone7dd79af2012-04-27 17:53:46 -040099
Weston Andros Adamson53113ad2014-06-09 11:48:38 -0400100 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400101 }
102}
103
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400104static void filelayout_reset_read(struct nfs_pgio_header *hdr)
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400105{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400106 struct rpc_task *task = &hdr->task;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400107
108 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
109 dprintk("%s Reset task %5u for i/o through MDS "
Niels de Vos1e8968c2013-12-17 18:20:16 +0100110 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400111 hdr->task.tk_pid,
Bryan Schumaker497826a2012-05-22 10:10:03 -0400112 hdr->inode->i_sb->s_id,
Niels de Vos1e8968c2013-12-17 18:20:16 +0100113 (unsigned long long)NFS_FILEID(hdr->inode),
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400114 hdr->args.count,
115 (unsigned long long)hdr->args.offset);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400116
Weston Andros Adamson53113ad2014-06-09 11:48:38 -0400117 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400118 }
119}
120
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000121static int filelayout_async_handle_error(struct rpc_task *task,
122 struct nfs4_state *state,
123 struct nfs_client *clp,
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400124 struct pnfs_layout_segment *lseg)
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000125{
Trond Myklebustd527e5c2012-10-11 13:43:38 -0400126 struct pnfs_layout_hdr *lo = lseg->pls_layout;
127 struct inode *inode = lo->plh_inode;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400128 struct nfs_server *mds_server = NFS_SERVER(inode);
129 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
Andy Adamson9cb81962012-03-07 10:49:41 -0500130 struct nfs_client *mds_client = mds_server->nfs_client;
Andy Adamson671fb892012-04-27 17:53:49 -0400131 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
Andy Adamson9cb81962012-03-07 10:49:41 -0500132
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000133 if (task->tk_status >= 0)
134 return 0;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000135
136 switch (task->tk_status) {
Andy Adamson9cb81962012-03-07 10:49:41 -0500137 /* MDS state errors */
138 case -NFS4ERR_DELEG_REVOKED:
139 case -NFS4ERR_ADMIN_REVOKED:
140 case -NFS4ERR_BAD_STATEID:
Andy Adamson9cb81962012-03-07 10:49:41 -0500141 case -NFS4ERR_OPENMODE:
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400142 if (state == NULL)
143 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400144 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
145 goto out_bad_stateid;
Andy Adamson9cb81962012-03-07 10:49:41 -0500146 goto wait_on_recovery;
147 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400148 if (state != NULL) {
149 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
150 goto out_bad_stateid;
151 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500152 nfs4_schedule_lease_recovery(mds_client);
153 goto wait_on_recovery;
154 /* DS session errors */
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000155 case -NFS4ERR_BADSESSION:
156 case -NFS4ERR_BADSLOT:
157 case -NFS4ERR_BAD_HIGH_SLOT:
158 case -NFS4ERR_DEADSESSION:
159 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
160 case -NFS4ERR_SEQ_FALSE_RETRY:
161 case -NFS4ERR_SEQ_MISORDERED:
162 dprintk("%s ERROR %d, Reset session. Exchangeid "
163 "flags 0x%x\n", __func__, task->tk_status,
164 clp->cl_exchange_flags);
Trond Myklebust9f594792012-05-27 13:02:53 -0400165 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000166 break;
167 case -NFS4ERR_DELAY:
168 case -NFS4ERR_GRACE:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000169 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
170 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400171 case -NFS4ERR_RETRY_UNCACHED_REP:
172 break;
Andy Adamson041245c2012-04-27 17:53:53 -0400173 /* Invalidate Layout errors */
174 case -NFS4ERR_PNFS_NO_LAYOUT:
175 case -ESTALE: /* mapped NFS4ERR_STALE */
176 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
177 case -EISDIR: /* mapped NFS4ERR_ISDIR */
178 case -NFS4ERR_FHEXPIRED:
179 case -NFS4ERR_WRONG_TYPE:
180 dprintk("%s Invalid layout error %d\n", __func__,
181 task->tk_status);
182 /*
183 * Destroy layout so new i/o will get a new layout.
184 * Layout will not be destroyed until all current lseg
185 * references are put. Mark layout as invalid to resend failed
186 * i/o and all i/o waiting on the slot table to the MDS until
187 * layout is destroyed and a new valid layout is obtained.
188 */
Andy Adamsond42e7872012-05-22 08:09:28 -0400189 pnfs_destroy_layout(NFS_I(inode));
Andy Adamson041245c2012-04-27 17:53:53 -0400190 rpc_wake_up(&tbl->slot_tbl_waitq);
191 goto reset;
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400192 /* RPC connection errors */
193 case -ECONNREFUSED:
194 case -EHOSTDOWN:
195 case -EHOSTUNREACH:
196 case -ENETUNREACH:
197 case -EIO:
198 case -ETIMEDOUT:
199 case -EPIPE:
200 dprintk("%s DS connection error %d\n", __func__,
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000201 task->tk_status);
Trond Myklebust1dfed272012-09-18 19:51:12 -0400202 nfs4_mark_deviceid_unavailable(devid);
Peng Taoc2201062014-09-06 00:53:29 +0800203 pnfs_error_mark_layout_for_return(inode, lseg);
Andy Adamson671fb892012-04-27 17:53:49 -0400204 rpc_wake_up(&tbl->slot_tbl_waitq);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400205 /* fall through */
206 default:
Andy Adamson041245c2012-04-27 17:53:53 -0400207reset:
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400208 dprintk("%s Retry through MDS. Error %d\n", __func__,
209 task->tk_status);
210 return -NFS4ERR_RESET_TO_MDS;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000211 }
Andy Adamson9cb81962012-03-07 10:49:41 -0500212out:
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000213 task->tk_status = 0;
214 return -EAGAIN;
Trond Myklebust5d422302013-03-14 16:57:48 -0400215out_bad_stateid:
216 task->tk_status = -EIO;
217 return 0;
Andy Adamson9cb81962012-03-07 10:49:41 -0500218wait_on_recovery:
219 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
220 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
221 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
222 goto out;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000223}
224
225/* NFS_PROTO call done callback routines */
226
227static int filelayout_read_done_cb(struct rpc_task *task,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400228 struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000229{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400230 int err;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000231
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400232 trace_nfs4_pnfs_read(hdr, task->tk_status);
233 err = filelayout_async_handle_error(task, hdr->args.context->state,
234 hdr->ds_clp, hdr->lseg);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000235
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400236 switch (err) {
237 case -NFS4ERR_RESET_TO_MDS:
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400238 filelayout_reset_read(hdr);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400239 return task->tk_status;
240 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700241 rpc_restart_call_prepare(task);
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000242 return -EAGAIN;
243 }
244
245 return 0;
246}
247
Andy Adamson16b374c2010-10-20 00:18:04 -0400248/*
Andy Adamson863a3c62011-03-23 13:27:54 +0000249 * We reference the rpc_cred of the first WRITE that triggers the need for
250 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
251 * rfc5661 is not clear about which credential should be used.
252 */
253static void
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400254filelayout_set_layoutcommit(struct nfs_pgio_header *hdr)
Andy Adamson863a3c62011-03-23 13:27:54 +0000255{
Fred Isamancd841602012-04-20 14:47:44 -0400256
257 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
Peng Taobc7d4b82014-08-07 10:15:03 +0800258 hdr->res.verf->committed != NFS_DATA_SYNC)
Andy Adamson863a3c62011-03-23 13:27:54 +0000259 return;
260
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400261 pnfs_set_layoutcommit(hdr);
Tom Haynesf383b7e2014-06-04 15:54:57 -0700262 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
Fred Isamancd841602012-04-20 14:47:44 -0400263 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
Andy Adamson863a3c62011-03-23 13:27:54 +0000264}
265
Trond Myklebust1dfed272012-09-18 19:51:12 -0400266bool
267filelayout_test_devid_unavailable(struct nfs4_deviceid_node *node)
268{
269 return filelayout_test_devid_invalid(node) ||
270 nfs4_test_deviceid_unavailable(node);
271}
272
273static bool
274filelayout_reset_to_mds(struct pnfs_layout_segment *lseg)
275{
276 struct nfs4_deviceid_node *node = FILELAYOUT_DEVID_NODE(lseg);
277
Trond Myklebust8006bfb2012-09-21 14:48:04 -0400278 return filelayout_test_devid_unavailable(node);
Trond Myklebust1dfed272012-09-18 19:51:12 -0400279}
280
Andy Adamson863a3c62011-03-23 13:27:54 +0000281/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000282 * Call ops for the async read/write cases
283 * In the case of dense layouts, the offset needs to be reset to its
284 * original value.
285 */
286static void filelayout_read_prepare(struct rpc_task *task, void *data)
287{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400288 struct nfs_pgio_header *hdr = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000289
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400290 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
Trond Myklebustc58c8442013-03-18 19:45:14 -0400291 rpc_exit(task, -EIO);
292 return;
293 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400294 if (filelayout_reset_to_mds(hdr->lseg)) {
Andy Adamson0ad2f372012-04-27 17:53:48 -0400295 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400296 filelayout_reset_read(hdr);
Andy Adamson0ad2f372012-04-27 17:53:48 -0400297 rpc_exit(task, 0);
298 return;
299 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400300 hdr->pgio_done_cb = filelayout_read_done_cb;
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000301
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400302 if (nfs41_setup_sequence(hdr->ds_clp->cl_session,
303 &hdr->args.seq_args,
304 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -0400305 task))
306 return;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400307 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
308 hdr->args.lock_context, FMODE_READ) == -EIO)
Andy Adamson869a9d32014-03-04 12:31:09 -0500309 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000310}
311
312static void filelayout_read_call_done(struct rpc_task *task, void *data)
313{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400314 struct nfs_pgio_header *hdr = data;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000315
316 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
317
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400318 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500319 task->tk_status == 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400320 nfs41_sequence_done(task, &hdr->res.seq_res);
Andy Adamson0ad2f372012-04-27 17:53:48 -0400321 return;
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500322 }
Andy Adamson0ad2f372012-04-27 17:53:48 -0400323
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000324 /* Note this may cause RPC to be resent */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400325 hdr->mds_ops->rpc_call_done(task, data);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000326}
327
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500328static void filelayout_read_count_stats(struct rpc_task *task, void *data)
329{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400330 struct nfs_pgio_header *hdr = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500331
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400332 rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500333}
334
Fred Isamana69aef12011-03-03 15:13:47 +0000335static int filelayout_write_done_cb(struct rpc_task *task,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400336 struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +0000337{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400338 int err;
Fred Isamana69aef12011-03-03 15:13:47 +0000339
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400340 trace_nfs4_pnfs_write(hdr, task->tk_status);
341 err = filelayout_async_handle_error(task, hdr->args.context->state,
342 hdr->ds_clp, hdr->lseg);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400343
344 switch (err) {
345 case -NFS4ERR_RESET_TO_MDS:
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400346 filelayout_reset_write(hdr);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400347 return task->tk_status;
348 case -EAGAIN:
Trond Myklebustd00c5d42011-10-19 12:17:29 -0700349 rpc_restart_call_prepare(task);
Fred Isamana69aef12011-03-03 15:13:47 +0000350 return -EAGAIN;
351 }
352
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400353 filelayout_set_layoutcommit(hdr);
Fred Isamana69aef12011-03-03 15:13:47 +0000354 return 0;
355}
356
Fred Isamane0c2b382011-03-23 13:27:53 +0000357static int filelayout_commit_done_cb(struct rpc_task *task,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400358 struct nfs_commit_data *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000359{
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400360 int err;
Fred Isamane0c2b382011-03-23 13:27:53 +0000361
Trond Myklebustcc668ab2013-08-14 15:31:28 -0400362 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400363 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
364 data->lseg);
365
366 switch (err) {
367 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesf54bcf22014-12-11 15:34:59 -0500368 pnfs_generic_prepare_to_resend_writes(data);
Andy Adamsone7dd79af2012-04-27 17:53:46 -0400369 return -EAGAIN;
370 case -EAGAIN:
371 rpc_restart_call_prepare(task);
Fred Isamane0c2b382011-03-23 13:27:53 +0000372 return -EAGAIN;
373 }
374
Peng Taobc7d4b82014-08-07 10:15:03 +0800375 if (data->verf.committed == NFS_UNSTABLE)
376 pnfs_commit_set_layoutcommit(data);
377
Fred Isamane0c2b382011-03-23 13:27:53 +0000378 return 0;
379}
380
Fred Isamana69aef12011-03-03 15:13:47 +0000381static void filelayout_write_prepare(struct rpc_task *task, void *data)
382{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400383 struct nfs_pgio_header *hdr = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000384
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400385 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
Trond Myklebustc58c8442013-03-18 19:45:14 -0400386 rpc_exit(task, -EIO);
387 return;
388 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400389 if (filelayout_reset_to_mds(hdr->lseg)) {
Andy Adamson0ad2f372012-04-27 17:53:48 -0400390 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400391 filelayout_reset_write(hdr);
Andy Adamson0ad2f372012-04-27 17:53:48 -0400392 rpc_exit(task, 0);
393 return;
394 }
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400395 if (nfs41_setup_sequence(hdr->ds_clp->cl_session,
396 &hdr->args.seq_args,
397 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -0400398 task))
399 return;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400400 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
401 hdr->args.lock_context, FMODE_WRITE) == -EIO)
Andy Adamson869a9d32014-03-04 12:31:09 -0500402 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
Fred Isamana69aef12011-03-03 15:13:47 +0000403}
404
405static void filelayout_write_call_done(struct rpc_task *task, void *data)
406{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400407 struct nfs_pgio_header *hdr = data;
Fred Isamana69aef12011-03-03 15:13:47 +0000408
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400409 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500410 task->tk_status == 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400411 nfs41_sequence_done(task, &hdr->res.seq_res);
Andy Adamson0ad2f372012-04-27 17:53:48 -0400412 return;
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500413 }
Andy Adamson0ad2f372012-04-27 17:53:48 -0400414
Fred Isamana69aef12011-03-03 15:13:47 +0000415 /* Note this may cause RPC to be resent */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400416 hdr->mds_ops->rpc_call_done(task, data);
Fred Isamana69aef12011-03-03 15:13:47 +0000417}
418
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500419static void filelayout_write_count_stats(struct rpc_task *task, void *data)
420{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400421 struct nfs_pgio_header *hdr = data;
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500422
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400423 rpc_count_iostats(task, NFS_SERVER(hdr->inode)->client->cl_metrics);
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500424}
425
Fred Isaman0b7c0152012-04-20 14:47:39 -0400426static void filelayout_commit_prepare(struct rpc_task *task, void *data)
Fred Isamane0c2b382011-03-23 13:27:53 +0000427{
Fred Isaman0b7c0152012-04-20 14:47:39 -0400428 struct nfs_commit_data *wdata = data;
Fred Isamane0c2b382011-03-23 13:27:53 +0000429
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400430 nfs41_setup_sequence(wdata->ds_clp->cl_session,
431 &wdata->args.seq_args,
432 &wdata->res.seq_res,
433 task);
Fred Isaman0b7c0152012-04-20 14:47:39 -0400434}
435
Fred Isaman0b7c0152012-04-20 14:47:39 -0400436static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
437{
438 struct nfs_commit_data *cdata = data;
439
440 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
441}
442
Trond Myklebust17280172012-03-11 13:11:00 -0400443static const struct rpc_call_ops filelayout_read_call_ops = {
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000444 .rpc_call_prepare = filelayout_read_prepare,
445 .rpc_call_done = filelayout_read_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500446 .rpc_count_stats = filelayout_read_count_stats,
Tom Haynesf54bcf22014-12-11 15:34:59 -0500447 .rpc_release = pnfs_generic_rw_release,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000448};
449
Trond Myklebust17280172012-03-11 13:11:00 -0400450static const struct rpc_call_ops filelayout_write_call_ops = {
Fred Isamana69aef12011-03-03 15:13:47 +0000451 .rpc_call_prepare = filelayout_write_prepare,
452 .rpc_call_done = filelayout_write_call_done,
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500453 .rpc_count_stats = filelayout_write_count_stats,
Tom Haynesf54bcf22014-12-11 15:34:59 -0500454 .rpc_release = pnfs_generic_rw_release,
Fred Isamana69aef12011-03-03 15:13:47 +0000455};
456
Trond Myklebust17280172012-03-11 13:11:00 -0400457static const struct rpc_call_ops filelayout_commit_call_ops = {
Fred Isaman0b7c0152012-04-20 14:47:39 -0400458 .rpc_call_prepare = filelayout_commit_prepare,
Tom Haynesf54bcf22014-12-11 15:34:59 -0500459 .rpc_call_done = pnfs_generic_write_commit_done,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400460 .rpc_count_stats = filelayout_commit_count_stats,
Tom Haynesf54bcf22014-12-11 15:34:59 -0500461 .rpc_release = pnfs_generic_commit_release,
Fred Isamane0c2b382011-03-23 13:27:53 +0000462};
463
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000464static enum pnfs_try_status
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400465filelayout_read_pagelist(struct nfs_pgio_header *hdr)
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000466{
Fred Isamancd841602012-04-20 14:47:44 -0400467 struct pnfs_layout_segment *lseg = hdr->lseg;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000468 struct nfs4_pnfs_ds *ds;
Andy Adamson0e201622013-09-06 14:14:00 -0400469 struct rpc_clnt *ds_clnt;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400470 loff_t offset = hdr->args.offset;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000471 u32 j, idx;
472 struct nfs_fh *fh;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000473
474 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
Fred Isamancd841602012-04-20 14:47:44 -0400475 __func__, hdr->inode->i_ino,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400476 hdr->args.pgbase, (size_t)hdr->args.count, offset);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000477
478 /* Retrieve the correct rpc_client for the byte range */
479 j = nfs4_fl_calc_j_index(lseg, offset);
480 idx = nfs4_fl_calc_ds_index(lseg, j);
481 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400482 if (!ds)
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000483 return PNFS_NOT_ATTEMPTED;
Andy Adamson0e201622013-09-06 14:14:00 -0400484
485 ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
486 if (IS_ERR(ds_clnt))
487 return PNFS_NOT_ATTEMPTED;
488
Andy Adamson3a7936c2012-04-27 17:53:51 -0400489 dprintk("%s USE DS: %s cl_count %d\n", __func__,
490 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000491
492 /* No multipath support. Use first DS */
Andy Adamson3a7936c2012-04-27 17:53:51 -0400493 atomic_inc(&ds->ds_clp->cl_count);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400494 hdr->ds_clp = ds->ds_clp;
495 hdr->ds_idx = idx;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000496 fh = nfs4_fl_select_ds_fh(lseg, j);
497 if (fh)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400498 hdr->args.fh = fh;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000499
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400500 hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
501 hdr->mds_offset = offset;
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000502
503 /* Perform an asynchronous read to ds */
Peng Tao46a5ab42014-06-13 23:02:25 +0800504 nfs_initiate_pgio(ds_clnt, hdr, hdr->cred,
505 NFS_PROTO(hdr->inode), &filelayout_read_call_ops,
506 0, RPC_TASK_SOFTCONN);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000507 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
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400512filelayout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
Andy Adamson0382b742011-03-03 15:13:45 +0000513{
Fred Isamancd841602012-04-20 14:47:44 -0400514 struct pnfs_layout_segment *lseg = hdr->lseg;
Fred Isamana69aef12011-03-03 15:13:47 +0000515 struct nfs4_pnfs_ds *ds;
Andy Adamson0e201622013-09-06 14:14:00 -0400516 struct rpc_clnt *ds_clnt;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400517 loff_t offset = hdr->args.offset;
Fred Isamana69aef12011-03-03 15:13:47 +0000518 u32 j, idx;
519 struct nfs_fh *fh;
Fred Isamana69aef12011-03-03 15:13:47 +0000520
521 /* Retrieve the correct rpc_client for the byte range */
522 j = nfs4_fl_calc_j_index(lseg, offset);
523 idx = nfs4_fl_calc_ds_index(lseg, j);
524 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson90fecfc2012-04-27 17:53:43 -0400525 if (!ds)
Fred Isamana69aef12011-03-03 15:13:47 +0000526 return PNFS_NOT_ATTEMPTED;
Andy Adamson0e201622013-09-06 14:14:00 -0400527
528 ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, hdr->inode);
529 if (IS_ERR(ds_clnt))
530 return PNFS_NOT_ATTEMPTED;
531
Andy Adamson3a7936c2012-04-27 17:53:51 -0400532 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400533 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
Andy Adamson3a7936c2012-04-27 17:53:51 -0400534 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
Fred Isamana69aef12011-03-03 15:13:47 +0000535
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400536 hdr->pgio_done_cb = filelayout_write_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -0400537 atomic_inc(&ds->ds_clp->cl_count);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400538 hdr->ds_clp = ds->ds_clp;
539 hdr->ds_idx = idx;
Fred Isamana69aef12011-03-03 15:13:47 +0000540 fh = nfs4_fl_select_ds_fh(lseg, j);
541 if (fh)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400542 hdr->args.fh = fh;
543 hdr->args.offset = filelayout_get_dserver_offset(lseg, offset);
Fred Isamana69aef12011-03-03 15:13:47 +0000544
545 /* Perform an asynchronous write */
Peng Tao46a5ab42014-06-13 23:02:25 +0800546 nfs_initiate_pgio(ds_clnt, hdr, hdr->cred,
547 NFS_PROTO(hdr->inode), &filelayout_write_call_ops,
548 sync, RPC_TASK_SOFTCONN);
Fred Isamana69aef12011-03-03 15:13:47 +0000549 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;
Andy Adamson16b374c2010-10-20 00:18:04 -0400570
571 dprintk("--> %s\n", __func__);
572
Andy Adamson7c24d942011-06-13 18:22:38 -0400573 /* FIXME: remove this check when layout segment support is added */
574 if (lgr->range.offset != 0 ||
575 lgr->range.length != NFS4_MAX_UINT64) {
576 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
577 __func__);
578 goto out;
579 }
580
Andy Adamson16b374c2010-10-20 00:18:04 -0400581 if (fl->pattern_offset > lgr->range.offset) {
Jim Rees3b6445a2011-02-22 19:31:57 -0500582 dprintk("%s pattern_offset %lld too large\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400583 __func__, fl->pattern_offset);
584 goto out;
585 }
586
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400587 if (!fl->stripe_unit) {
Benny Halevy75247af2011-02-22 15:56:01 -0800588 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400589 __func__, fl->stripe_unit);
590 goto out;
591 }
592
593 /* find and reference the deviceid */
Christoph Hellwig661373b2014-09-02 21:27:57 -0700594 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode), id,
595 lo->plh_lc_cred, gfp_flags);
596 if (d == NULL)
597 goto out;
598
599 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
Trond Myklebust1dfed272012-09-18 19:51:12 -0400600 /* Found deviceid is unavailable */
601 if (filelayout_test_devid_unavailable(&dsaddr->id_node))
Christoph Hellwig661373b2014-09-02 21:27:57 -0700602 goto out_put;
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400603
Andy Adamson16b374c2010-10-20 00:18:04 -0400604 fl->dsaddr = dsaddr;
605
Chuck Levere4149662011-10-25 12:18:03 -0400606 if (fl->first_stripe_index >= dsaddr->stripe_count) {
607 dprintk("%s Bad first_stripe_index %u\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400608 __func__, fl->first_stripe_index);
609 goto out_put;
610 }
611
612 if ((fl->stripe_type == STRIPE_SPARSE &&
613 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
614 (fl->stripe_type == STRIPE_DENSE &&
615 fl->num_fh != dsaddr->stripe_count)) {
616 dprintk("%s num_fh %u not valid for given packing\n",
617 __func__, fl->num_fh);
618 goto out_put;
619 }
620
Andy Adamson16b374c2010-10-20 00:18:04 -0400621 status = 0;
622out:
623 dprintk("--> %s returns %d\n", __func__, status);
624 return status;
625out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000626 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400627 goto out;
628}
629
630static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
631{
632 int i;
633
634 for (i = 0; i < fl->num_fh; i++) {
635 if (!fl->fh_array[i])
636 break;
637 kfree(fl->fh_array[i]);
638 }
639 kfree(fl->fh_array);
640 fl->fh_array = NULL;
641}
642
643static void
644_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
645{
646 filelayout_free_fh_array(fl);
647 kfree(fl);
648}
649
650static int
651filelayout_decode_layout(struct pnfs_layout_hdr *flo,
652 struct nfs4_filelayout_segment *fl,
653 struct nfs4_layoutget_res *lgr,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400654 struct nfs4_deviceid *id,
655 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400656{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400657 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400658 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400659 struct page *scratch;
660 __be32 *p;
Andy Adamson16b374c2010-10-20 00:18:04 -0400661 uint32_t nfl_util;
662 int i;
663
664 dprintk("%s: set_layout_map Begin\n", __func__);
665
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400666 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400667 if (!scratch)
668 return -ENOMEM;
669
Benny Halevyf7da7a12011-05-19 14:16:47 -0400670 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400671 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
672
673 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
674 * num_fh (4) */
675 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
676 if (unlikely(!p))
677 goto out_err;
678
Andy Adamson16b374c2010-10-20 00:18:04 -0400679 memcpy(id, p, sizeof(*id));
680 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400681 nfs4_print_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400682
683 nfl_util = be32_to_cpup(p++);
684 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
685 fl->commit_through_mds = 1;
686 if (nfl_util & NFL4_UFLG_DENSE)
687 fl->stripe_type = STRIPE_DENSE;
688 else
689 fl->stripe_type = STRIPE_SPARSE;
690 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
691
692 fl->first_stripe_index = be32_to_cpup(p++);
693 p = xdr_decode_hyper(p, &fl->pattern_offset);
694 fl->num_fh = be32_to_cpup(p++);
695
696 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
697 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
698 fl->pattern_offset);
699
Andy Adamsoncec765c2011-06-13 18:36:17 -0400700 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
701 * Futher checking is done in filelayout_check_layout */
Chuck Levere4149662011-10-25 12:18:03 -0400702 if (fl->num_fh >
Andy Adamsoncec765c2011-06-13 18:36:17 -0400703 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400704 goto out_err;
705
Andy Adamsoncec765c2011-06-13 18:36:17 -0400706 if (fl->num_fh > 0) {
Trond Myklebust1813bad2012-10-11 14:36:52 -0400707 fl->fh_array = kcalloc(fl->num_fh, sizeof(fl->fh_array[0]),
Andy Adamsoncec765c2011-06-13 18:36:17 -0400708 gfp_flags);
709 if (!fl->fh_array)
710 goto out_err;
711 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400712
713 for (i = 0; i < fl->num_fh; i++) {
714 /* Do we want to use a mempool here? */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400715 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400716 if (!fl->fh_array[i])
717 goto out_err_free;
718
719 p = xdr_inline_decode(&stream, 4);
720 if (unlikely(!p))
721 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400722 fl->fh_array[i]->size = be32_to_cpup(p++);
723 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -0500724 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400725 i, fl->fh_array[i]->size);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400726 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400727 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400728
729 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
730 if (unlikely(!p))
731 goto out_err_free;
Andy Adamson16b374c2010-10-20 00:18:04 -0400732 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
Andy Adamson16b374c2010-10-20 00:18:04 -0400733 dprintk("DEBUG: %s: fh len %d\n", __func__,
734 fl->fh_array[i]->size);
735 }
736
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400737 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400738 return 0;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400739
740out_err_free:
741 filelayout_free_fh_array(fl);
742out_err:
743 __free_page(scratch);
744 return -EIO;
Andy Adamson16b374c2010-10-20 00:18:04 -0400745}
746
Fred Isamanc8795132011-03-23 13:27:49 +0000747static void
748filelayout_free_lseg(struct pnfs_layout_segment *lseg)
749{
750 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
751
752 dprintk("--> %s\n", __func__);
753 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400754 /* This assumes a single RW lseg */
755 if (lseg->pls_range.iomode == IOMODE_RW) {
756 struct nfs4_filelayout *flo;
757
758 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
759 flo->commit_info.nbuckets = 0;
760 kfree(flo->commit_info.buckets);
761 flo->commit_info.buckets = NULL;
762 }
Fred Isamanc8795132011-03-23 13:27:49 +0000763 _filelayout_free_lseg(fl);
764}
765
Fred Isaman799ba8d2012-04-20 14:47:38 -0400766static int
767filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
Fred Isamanea2cf222012-04-20 14:47:53 -0400768 struct nfs_commit_info *cinfo,
Fred Isaman799ba8d2012-04-20 14:47:38 -0400769 gfp_t gfp_flags)
770{
771 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
Fred Isamanea2cf222012-04-20 14:47:53 -0400772 struct pnfs_commit_bucket *buckets;
Weston Andros Adamsondd7663e2014-05-15 11:56:49 -0400773 int size, i;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400774
775 if (fl->commit_through_mds)
776 return 0;
Weston Andros Adamsondd7663e2014-05-15 11:56:49 -0400777
778 size = (fl->stripe_type == STRIPE_SPARSE) ?
779 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
780
781 if (cinfo->ds->nbuckets >= size) {
Fred Isaman799ba8d2012-04-20 14:47:38 -0400782 /* This assumes there is only one IOMODE_RW lseg. What
783 * we really want to do is have a layout_hdr level
784 * dictionary of <multipath_list4, fh> keys, each
785 * associated with a struct list_head, populated by calls
786 * to filelayout_write_pagelist().
787 * */
788 return 0;
789 }
790
Fred Isamanea2cf222012-04-20 14:47:53 -0400791 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
Fred Isaman799ba8d2012-04-20 14:47:38 -0400792 gfp_flags);
793 if (!buckets)
794 return -ENOMEM;
Weston Andros Adamsondd7663e2014-05-15 11:56:49 -0400795 for (i = 0; i < size; i++) {
796 INIT_LIST_HEAD(&buckets[i].written);
797 INIT_LIST_HEAD(&buckets[i].committing);
Weston Andros Adamson5002c582014-05-15 11:56:54 -0400798 /* mark direct verifier as unset */
799 buckets[i].direct_verf.committed = NFS_INVALID_STABLE_HOW;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400800 }
Weston Andros Adamsondd7663e2014-05-15 11:56:49 -0400801
802 spin_lock(cinfo->lock);
803 if (cinfo->ds->nbuckets >= size)
804 goto out;
805 for (i = 0; i < cinfo->ds->nbuckets; i++) {
806 list_splice(&cinfo->ds->buckets[i].written,
807 &buckets[i].written);
808 list_splice(&cinfo->ds->buckets[i].committing,
809 &buckets[i].committing);
Weston Andros Adamson5002c582014-05-15 11:56:54 -0400810 buckets[i].direct_verf.committed =
811 cinfo->ds->buckets[i].direct_verf.committed;
Weston Andros Adamsondd7663e2014-05-15 11:56:49 -0400812 buckets[i].wlseg = cinfo->ds->buckets[i].wlseg;
813 buckets[i].clseg = cinfo->ds->buckets[i].clseg;
814 }
815 swap(cinfo->ds->buckets, buckets);
816 cinfo->ds->nbuckets = size;
817out:
818 spin_unlock(cinfo->lock);
819 kfree(buckets);
820 return 0;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400821}
822
Andy Adamson16b374c2010-10-20 00:18:04 -0400823static struct pnfs_layout_segment *
824filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400825 struct nfs4_layoutget_res *lgr,
826 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400827{
828 struct nfs4_filelayout_segment *fl;
829 int rc;
830 struct nfs4_deviceid id;
831
832 dprintk("--> %s\n", __func__);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400833 fl = kzalloc(sizeof(*fl), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400834 if (!fl)
835 return NULL;
836
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400837 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
838 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400839 _filelayout_free_lseg(fl);
840 return NULL;
841 }
842 return &fl->generic_hdr;
843}
844
Fred Isaman94ad1c82011-03-01 01:34:14 +0000845/*
846 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
847 *
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400848 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
849 * of bytes (maximum @req->wb_bytes) that can be coalesced.
Fred Isaman94ad1c82011-03-01 01:34:14 +0000850 */
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400851static size_t
Fred Isaman94ad1c82011-03-01 01:34:14 +0000852filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
853 struct nfs_page *req)
854{
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -0400855 unsigned int size;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000856 u64 p_stripe, r_stripe;
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400857 u32 stripe_offset;
858 u64 segment_offset = pgio->pg_lseg->pls_range.offset;
859 u32 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000860
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -0400861 /* calls nfs_generic_pg_test */
862 size = pnfs_generic_pg_test(pgio, prev, req);
863 if (!size)
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400864 return 0;
Benny Halevy89a58e32011-05-25 20:54:40 +0300865
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400866 /* see if req and prev are in the same stripe */
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -0400867 if (prev) {
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400868 p_stripe = (u64)req_offset(prev) - segment_offset;
869 r_stripe = (u64)req_offset(req) - segment_offset;
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -0400870 do_div(p_stripe, stripe_unit);
871 do_div(r_stripe, stripe_unit);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000872
Weston Andros Adamson0f9c4292014-05-15 11:56:51 -0400873 if (p_stripe != r_stripe)
874 return 0;
875 }
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400876
877 /* calculate remaining bytes in the current stripe */
878 div_u64_rem((u64)req_offset(req) - segment_offset,
879 stripe_unit,
880 &stripe_offset);
881 WARN_ON_ONCE(stripe_offset > stripe_unit);
882 if (stripe_offset >= stripe_unit)
883 return 0;
884 return min(stripe_unit - (unsigned int)stripe_offset, size);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000885}
886
Trond Myklebust17280172012-03-11 13:11:00 -0400887static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400888filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
889 struct nfs_page *req)
890{
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400891 if (!pgio->pg_lseg)
892 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
Andy Adamson7c24d942011-06-13 18:22:38 -0400893 req->wb_context,
894 0,
895 NFS4_MAX_UINT64,
896 IOMODE_READ,
897 GFP_KERNEL);
898 /* If no lseg, fall back to read through mds */
899 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -0400900 nfs_pageio_reset_read_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400901}
902
Trond Myklebust17280172012-03-11 13:11:00 -0400903static void
Andy Adamson7c24d942011-06-13 18:22:38 -0400904filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
905 struct nfs_page *req)
906{
Fred Isamanea2cf222012-04-20 14:47:53 -0400907 struct nfs_commit_info cinfo;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400908 int status;
909
Weston Andros Adamsonc6194272014-05-15 11:56:56 -0400910 if (!pgio->pg_lseg)
911 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
Andy Adamson7c24d942011-06-13 18:22:38 -0400912 req->wb_context,
913 0,
914 NFS4_MAX_UINT64,
915 IOMODE_RW,
916 GFP_NOFS);
917 /* If no lseg, fall back to write through mds */
918 if (pgio->pg_lseg == NULL)
Fred Isaman799ba8d2012-04-20 14:47:38 -0400919 goto out_mds;
Fred Isamanea2cf222012-04-20 14:47:53 -0400920 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
921 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400922 if (status < 0) {
Trond Myklebust9369a432012-09-18 20:57:08 -0400923 pnfs_put_lseg(pgio->pg_lseg);
Fred Isaman799ba8d2012-04-20 14:47:38 -0400924 pgio->pg_lseg = NULL;
925 goto out_mds;
926 }
927 return;
928out_mds:
929 nfs_pageio_reset_write_mds(pgio);
Andy Adamson7c24d942011-06-13 18:22:38 -0400930}
931
Trond Myklebust1751c362011-06-10 13:30:23 -0400932static const struct nfs_pageio_ops filelayout_pg_read_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400933 .pg_init = filelayout_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400934 .pg_test = filelayout_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400935 .pg_doio = pnfs_generic_pg_readpages,
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -0400936 .pg_cleanup = pnfs_generic_pg_cleanup,
Trond Myklebust1751c362011-06-10 13:30:23 -0400937};
938
939static const struct nfs_pageio_ops filelayout_pg_write_ops = {
Andy Adamson7c24d942011-06-13 18:22:38 -0400940 .pg_init = filelayout_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400941 .pg_test = filelayout_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400942 .pg_doio = pnfs_generic_pg_writepages,
Weston Andros Adamson180bb5e2014-09-10 15:48:01 -0400943 .pg_cleanup = pnfs_generic_pg_cleanup,
Trond Myklebust1751c362011-06-10 13:30:23 -0400944};
945
Fred Isamane0c2b382011-03-23 13:27:53 +0000946static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
947{
948 if (fl->stripe_type == STRIPE_SPARSE)
949 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
950 else
951 return j;
952}
953
Peng Taoc8a32922014-07-03 13:07:45 +0800954static void
955filelayout_mark_request_commit(struct nfs_page *req,
956 struct pnfs_layout_segment *lseg,
957 struct nfs_commit_info *cinfo)
958
Fred Isamand6d6dc72012-03-08 17:29:35 -0500959{
Fred Isamane0c2b382011-03-23 13:27:53 +0000960 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
961 u32 i, j;
962 struct list_head *list;
Fred Isamanea2cf222012-04-20 14:47:53 -0400963 struct pnfs_commit_bucket *buckets;
Fred Isamane0c2b382011-03-23 13:27:53 +0000964
Peng Taoc8a32922014-07-03 13:07:45 +0800965 if (fl->commit_through_mds) {
966 list = &cinfo->mds->list;
967 spin_lock(cinfo->lock);
968 goto mds_commit;
969 }
Fred Isamand6d6dc72012-03-08 17:29:35 -0500970
Fred Isamane0c2b382011-03-23 13:27:53 +0000971 /* Note that we are calling nfs4_fl_calc_j_index on each page
972 * that ends up being committed to a data server. An attractive
973 * alternative is to add a field to nfs_write_data and nfs_page
974 * to store the value calculated in filelayout_write_pagelist
975 * and just use that here.
976 */
Fred Isamanb5542842012-04-20 14:47:43 -0400977 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
Fred Isamane0c2b382011-03-23 13:27:53 +0000978 i = select_bucket_index(fl, j);
Weston Andros Adamsond201c4d2014-05-15 11:56:40 -0400979 spin_lock(cinfo->lock);
Fred Isamanea2cf222012-04-20 14:47:53 -0400980 buckets = cinfo->ds->buckets;
Fred Isaman799ba8d2012-04-20 14:47:38 -0400981 list = &buckets[i].written;
Fred Isamane0c2b382011-03-23 13:27:53 +0000982 if (list_empty(list)) {
Fred Isamand6d6dc72012-03-08 17:29:35 -0500983 /* Non-empty buckets hold a reference on the lseg. That ref
984 * is normally transferred to the COMMIT call and released
985 * there. It could also be released if the last req is pulled
986 * off due to a rewrite, in which case it will be done in
Tom Haynesf54bcf22014-12-11 15:34:59 -0500987 * pnfs_generic_clear_request_commit
Fred Isamand6d6dc72012-03-08 17:29:35 -0500988 */
Trond Myklebust9369a432012-09-18 20:57:08 -0400989 buckets[i].wlseg = pnfs_get_lseg(lseg);
Fred Isamane0c2b382011-03-23 13:27:53 +0000990 }
Trond Myklebust8dd37752012-03-15 17:16:40 -0400991 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
Fred Isamanea2cf222012-04-20 14:47:53 -0400992 cinfo->ds->nwritten++;
Peng Taoc8a32922014-07-03 13:07:45 +0800993
994mds_commit:
995 /* nfs_request_add_commit_list(). We need to add req to list without
996 * dropping cinfo lock.
997 */
998 set_bit(PG_CLEAN, &(req)->wb_flags);
999 nfs_list_add_request(req, list);
1000 cinfo->mds->ncommit++;
Weston Andros Adamsond201c4d2014-05-15 11:56:40 -04001001 spin_unlock(cinfo->lock);
Peng Taoc8a32922014-07-03 13:07:45 +08001002 if (!cinfo->dreq) {
1003 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1004 inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
1005 BDI_RECLAIMABLE);
1006 __mark_inode_dirty(req->wb_context->dentry->d_inode,
1007 I_DIRTY_DATASYNC);
1008 }
Trond Myklebust8dd37752012-03-15 17:16:40 -04001009}
1010
Fred Isamane0c2b382011-03-23 13:27:53 +00001011static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1012{
1013 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1014
1015 if (flseg->stripe_type == STRIPE_SPARSE)
1016 return i;
1017 else
1018 return nfs4_fl_calc_ds_index(lseg, i);
1019}
1020
1021static struct nfs_fh *
1022select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1023{
1024 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1025
1026 if (flseg->stripe_type == STRIPE_SPARSE) {
1027 if (flseg->num_fh == 1)
1028 i = 0;
1029 else if (flseg->num_fh == 0)
1030 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1031 return NULL;
1032 }
1033 return flseg->fh_array[i];
1034}
1035
Fred Isaman0b7c0152012-04-20 14:47:39 -04001036static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
Fred Isamane0c2b382011-03-23 13:27:53 +00001037{
1038 struct pnfs_layout_segment *lseg = data->lseg;
1039 struct nfs4_pnfs_ds *ds;
Andy Adamson0e201622013-09-06 14:14:00 -04001040 struct rpc_clnt *ds_clnt;
Fred Isamane0c2b382011-03-23 13:27:53 +00001041 u32 idx;
1042 struct nfs_fh *fh;
1043
1044 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1045 ds = nfs4_fl_prepare_ds(lseg, idx);
Andy Adamson0e201622013-09-06 14:14:00 -04001046 if (!ds)
1047 goto out_err;
1048
1049 ds_clnt = nfs4_find_or_create_ds_client(ds->ds_clp, data->inode);
1050 if (IS_ERR(ds_clnt))
1051 goto out_err;
1052
Andy Adamson3a7936c2012-04-27 17:53:51 -04001053 dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
1054 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
Fred Isaman0b7c0152012-04-20 14:47:39 -04001055 data->commit_done_cb = filelayout_commit_done_cb;
Andy Adamson3a7936c2012-04-27 17:53:51 -04001056 atomic_inc(&ds->ds_clp->cl_count);
Fred Isamane0c2b382011-03-23 13:27:53 +00001057 data->ds_clp = ds->ds_clp;
1058 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1059 if (fh)
1060 data->args.fh = fh;
Peng Taoc36aae92014-06-09 07:10:14 +08001061 return nfs_initiate_commit(ds_clnt, data, NFS_PROTO(data->inode),
Andy Adamson9f0ec1762012-04-27 17:53:44 -04001062 &filelayout_commit_call_ops, how,
1063 RPC_TASK_SOFTCONN);
Andy Adamson0e201622013-09-06 14:14:00 -04001064out_err:
Tom Haynesf54bcf22014-12-11 15:34:59 -05001065 pnfs_generic_prepare_to_resend_writes(data);
1066 pnfs_generic_commit_release(data);
Andy Adamson0e201622013-09-06 14:14:00 -04001067 return -EAGAIN;
Fred Isamane0c2b382011-03-23 13:27:53 +00001068}
1069
Weston Andros Adamson02d14262014-07-17 20:42:17 -04001070/* filelayout_search_commit_reqs - Search lists in @cinfo for the head reqest
1071 * for @page
1072 * @cinfo - commit info for current inode
1073 * @page - page to search for matching head request
1074 *
1075 * Returns a the head request if one is found, otherwise returns NULL.
1076 */
1077static struct nfs_page *
1078filelayout_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page)
1079{
1080 struct nfs_page *freq, *t;
1081 struct pnfs_commit_bucket *b;
1082 int i;
1083
1084 /* Linearly search the commit lists for each bucket until a matching
1085 * request is found */
1086 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1087 list_for_each_entry_safe(freq, t, &b->written, wb_list) {
1088 if (freq->wb_page == page)
1089 return freq->wb_head;
1090 }
1091 list_for_each_entry_safe(freq, t, &b->committing, wb_list) {
1092 if (freq->wb_page == page)
1093 return freq->wb_head;
1094 }
1095 }
1096
1097 return NULL;
1098}
1099
Fred Isamane0c2b382011-03-23 13:27:53 +00001100static int
1101filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
Fred Isamanea2cf222012-04-20 14:47:53 -04001102 int how, struct nfs_commit_info *cinfo)
Fred Isamane0c2b382011-03-23 13:27:53 +00001103{
Tom Haynesf54bcf22014-12-11 15:34:59 -05001104 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1105 filelayout_initiate_commit);
Fred Isamane0c2b382011-03-23 13:27:53 +00001106}
Tom Haynesf54bcf22014-12-11 15:34:59 -05001107
Christoph Hellwig661373b2014-09-02 21:27:57 -07001108static struct nfs4_deviceid_node *
1109filelayout_alloc_deviceid_node(struct nfs_server *server,
1110 struct pnfs_device *pdev, gfp_t gfp_flags)
1111{
1112 struct nfs4_file_layout_dsaddr *dsaddr;
1113
1114 dsaddr = nfs4_fl_alloc_deviceid_node(server, pdev, gfp_flags);
1115 if (!dsaddr)
1116 return NULL;
1117 return &dsaddr->id_node;
1118}
Fred Isamane0c2b382011-03-23 13:27:53 +00001119
Benny Halevy1775bc32011-05-20 13:47:33 +02001120static void
1121filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1122{
1123 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1124}
1125
Fred Isaman799ba8d2012-04-20 14:47:38 -04001126static struct pnfs_layout_hdr *
1127filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1128{
1129 struct nfs4_filelayout *flo;
1130
1131 flo = kzalloc(sizeof(*flo), gfp_flags);
Trond Myklebust6df200f2014-05-29 20:06:55 -04001132 return flo != NULL ? &flo->generic_hdr : NULL;
Fred Isaman799ba8d2012-04-20 14:47:38 -04001133}
1134
1135static void
1136filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1137{
1138 kfree(FILELAYOUT_FROM_HDR(lo));
1139}
1140
Fred Isamanea2cf222012-04-20 14:47:53 -04001141static struct pnfs_ds_commit_info *
1142filelayout_get_ds_info(struct inode *inode)
1143{
Fred Isamandf011742012-04-24 14:50:34 -04001144 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1145
1146 if (layout == NULL)
1147 return NULL;
1148 else
1149 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
Fred Isamanea2cf222012-04-20 14:47:53 -04001150}
1151
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001152static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001153 .id = LAYOUT_NFSV4_1_FILES,
1154 .name = "LAYOUT_NFSV4_1_FILES",
1155 .owner = THIS_MODULE,
Fred Isaman799ba8d2012-04-20 14:47:38 -04001156 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1157 .free_layout_hdr = filelayout_free_layout_hdr,
Christoph Hellwigea8eecd2011-03-01 01:34:21 +00001158 .alloc_lseg = filelayout_alloc_lseg,
1159 .free_lseg = filelayout_free_lseg,
Trond Myklebust1751c362011-06-10 13:30:23 -04001160 .pg_read_ops = &filelayout_pg_read_ops,
1161 .pg_write_ops = &filelayout_pg_write_ops,
Fred Isamanea2cf222012-04-20 14:47:53 -04001162 .get_ds_info = &filelayout_get_ds_info,
Trond Myklebust8dd37752012-03-15 17:16:40 -04001163 .mark_request_commit = filelayout_mark_request_commit,
Tom Haynesf54bcf22014-12-11 15:34:59 -05001164 .clear_request_commit = pnfs_generic_clear_request_commit,
1165 .scan_commit_lists = pnfs_generic_scan_commit_lists,
1166 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
Weston Andros Adamson02d14262014-07-17 20:42:17 -04001167 .search_commit_reqs = filelayout_search_commit_reqs,
Fred Isamane0c2b382011-03-23 13:27:53 +00001168 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +00001169 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +00001170 .write_pagelist = filelayout_write_pagelist,
Christoph Hellwig661373b2014-09-02 21:27:57 -07001171 .alloc_deviceid_node = filelayout_alloc_deviceid_node,
Benny Halevy1775bc32011-05-20 13:47:33 +02001172 .free_deviceid_node = filelayout_free_deveiceid_node,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001173};
1174
1175static int __init nfs4filelayout_init(void)
1176{
1177 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1178 __func__);
1179 return pnfs_register_layoutdriver(&filelayout_type);
1180}
1181
1182static void __exit nfs4filelayout_exit(void)
1183{
1184 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1185 __func__);
1186 pnfs_unregister_layoutdriver(&filelayout_type);
1187}
1188
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001189MODULE_ALIAS("nfs-layouttype4-1");
1190
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001191module_init(nfs4filelayout_init);
1192module_exit(nfs4filelayout_exit);