Commit | Line | Data |
---|---|---|
7ab672ce DH |
1 | /* |
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> | |
19345cb2 | 33 | #include <linux/nfs_page.h> |
143cb494 | 34 | #include <linux/module.h> |
16b374ca | 35 | |
0a702195 WAA |
36 | #include <linux/sunrpc/metrics.h> |
37 | ||
16b374ca | 38 | #include "internal.h" |
9cb81968 | 39 | #include "delegation.h" |
16b374ca | 40 | #include "nfs4filelayout.h" |
7ab672ce DH |
41 | |
42 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD | |
43 | ||
44 | MODULE_LICENSE("GPL"); | |
45 | MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>"); | |
46 | MODULE_DESCRIPTION("The NFSv4 file layout driver"); | |
47 | ||
cbdabc7f AA |
48 | #define FILELAYOUT_POLL_RETRY_MAX (15*HZ) |
49 | ||
cfe7f412 FI |
50 | static loff_t |
51 | filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg, | |
52 | loff_t offset) | |
53 | { | |
54 | u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count; | |
3476f114 CM |
55 | u64 stripe_no; |
56 | u32 rem; | |
cfe7f412 FI |
57 | |
58 | offset -= flseg->pattern_offset; | |
3476f114 CM |
59 | stripe_no = div_u64(offset, stripe_width); |
60 | div_u64_rem(offset, flseg->stripe_unit, &rem); | |
cfe7f412 | 61 | |
3476f114 | 62 | return stripe_no * flseg->stripe_unit + rem; |
cfe7f412 FI |
63 | } |
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 | */ | |
69 | static loff_t | |
70 | filelayout_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 | ||
cbdabc7f AA |
85 | static int filelayout_async_handle_error(struct rpc_task *task, |
86 | struct nfs4_state *state, | |
87 | struct nfs_client *clp, | |
88 | int *reset) | |
89 | { | |
9cb81968 AA |
90 | struct nfs_server *mds_server = NFS_SERVER(state->inode); |
91 | struct nfs_client *mds_client = mds_server->nfs_client; | |
92 | ||
cbdabc7f AA |
93 | if (task->tk_status >= 0) |
94 | return 0; | |
cbdabc7f AA |
95 | *reset = 0; |
96 | ||
97 | switch (task->tk_status) { | |
9cb81968 AA |
98 | /* MDS state errors */ |
99 | case -NFS4ERR_DELEG_REVOKED: | |
100 | case -NFS4ERR_ADMIN_REVOKED: | |
101 | case -NFS4ERR_BAD_STATEID: | |
2dc31756 | 102 | nfs_remove_bad_delegation(state->inode); |
9cb81968 | 103 | case -NFS4ERR_OPENMODE: |
9cb81968 AA |
104 | nfs4_schedule_stateid_recovery(mds_server, state); |
105 | goto wait_on_recovery; | |
106 | case -NFS4ERR_EXPIRED: | |
2dc31756 | 107 | nfs4_schedule_stateid_recovery(mds_server, state); |
9cb81968 AA |
108 | nfs4_schedule_lease_recovery(mds_client); |
109 | goto wait_on_recovery; | |
110 | /* DS session errors */ | |
cbdabc7f AA |
111 | case -NFS4ERR_BADSESSION: |
112 | case -NFS4ERR_BADSLOT: | |
113 | case -NFS4ERR_BAD_HIGH_SLOT: | |
114 | case -NFS4ERR_DEADSESSION: | |
115 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
116 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
117 | case -NFS4ERR_SEQ_MISORDERED: | |
118 | dprintk("%s ERROR %d, Reset session. Exchangeid " | |
119 | "flags 0x%x\n", __func__, task->tk_status, | |
120 | clp->cl_exchange_flags); | |
121 | nfs4_schedule_session_recovery(clp->cl_session); | |
122 | break; | |
123 | case -NFS4ERR_DELAY: | |
124 | case -NFS4ERR_GRACE: | |
125 | case -EKEYEXPIRED: | |
126 | rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX); | |
127 | break; | |
a8a4ae3a AA |
128 | case -NFS4ERR_RETRY_UNCACHED_REP: |
129 | break; | |
cbdabc7f AA |
130 | default: |
131 | dprintk("%s DS error. Retry through MDS %d\n", __func__, | |
132 | task->tk_status); | |
133 | *reset = 1; | |
134 | break; | |
135 | } | |
9cb81968 | 136 | out: |
cbdabc7f AA |
137 | task->tk_status = 0; |
138 | return -EAGAIN; | |
9cb81968 AA |
139 | wait_on_recovery: |
140 | rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL); | |
141 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0) | |
142 | rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task); | |
143 | goto out; | |
cbdabc7f AA |
144 | } |
145 | ||
146 | /* NFS_PROTO call done callback routines */ | |
147 | ||
148 | static int filelayout_read_done_cb(struct rpc_task *task, | |
149 | struct nfs_read_data *data) | |
150 | { | |
cd841605 | 151 | struct nfs_pgio_header *hdr = data->header; |
cbdabc7f AA |
152 | int reset = 0; |
153 | ||
154 | dprintk("%s DS read\n", __func__); | |
155 | ||
156 | if (filelayout_async_handle_error(task, data->args.context->state, | |
157 | data->ds_clp, &reset) == -EAGAIN) { | |
158 | dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n", | |
159 | __func__, data->ds_clp, data->ds_clp->cl_session); | |
160 | if (reset) { | |
cd841605 | 161 | pnfs_set_lo_fail(hdr->lseg); |
cbdabc7f | 162 | nfs4_reset_read(task, data); |
cbdabc7f | 163 | } |
d00c5d43 | 164 | rpc_restart_call_prepare(task); |
cbdabc7f AA |
165 | return -EAGAIN; |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
863a3c6c AA |
171 | /* |
172 | * We reference the rpc_cred of the first WRITE that triggers the need for | |
173 | * a LAYOUTCOMMIT, and use it to send the layoutcommit compound. | |
174 | * rfc5661 is not clear about which credential should be used. | |
175 | */ | |
176 | static void | |
177 | filelayout_set_layoutcommit(struct nfs_write_data *wdata) | |
178 | { | |
cd841605 FI |
179 | struct nfs_pgio_header *hdr = wdata->header; |
180 | ||
181 | if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds || | |
863a3c6c AA |
182 | wdata->res.verf->committed == NFS_FILE_SYNC) |
183 | return; | |
184 | ||
185 | pnfs_set_layoutcommit(wdata); | |
cd841605 FI |
186 | dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino, |
187 | (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb); | |
863a3c6c AA |
188 | } |
189 | ||
dc70d7b3 AA |
190 | /* |
191 | * Call ops for the async read/write cases | |
192 | * In the case of dense layouts, the offset needs to be reset to its | |
193 | * original value. | |
194 | */ | |
195 | static void filelayout_read_prepare(struct rpc_task *task, void *data) | |
196 | { | |
cd12ae32 | 197 | struct nfs_read_data *rdata = data; |
dc70d7b3 | 198 | |
cbdabc7f AA |
199 | rdata->read_done_cb = filelayout_read_done_cb; |
200 | ||
dc70d7b3 AA |
201 | if (nfs41_setup_sequence(rdata->ds_clp->cl_session, |
202 | &rdata->args.seq_args, &rdata->res.seq_res, | |
9d12b216 | 203 | task)) |
dc70d7b3 AA |
204 | return; |
205 | ||
206 | rpc_call_start(task); | |
207 | } | |
208 | ||
209 | static void filelayout_read_call_done(struct rpc_task *task, void *data) | |
210 | { | |
cd12ae32 | 211 | struct nfs_read_data *rdata = data; |
dc70d7b3 AA |
212 | |
213 | dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status); | |
214 | ||
215 | /* Note this may cause RPC to be resent */ | |
cd841605 | 216 | rdata->header->mds_ops->rpc_call_done(task, data); |
dc70d7b3 AA |
217 | } |
218 | ||
0a702195 WAA |
219 | static void filelayout_read_count_stats(struct rpc_task *task, void *data) |
220 | { | |
cd12ae32 | 221 | struct nfs_read_data *rdata = data; |
0a702195 | 222 | |
cd841605 | 223 | rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics); |
0a702195 WAA |
224 | } |
225 | ||
dc70d7b3 AA |
226 | static void filelayout_read_release(void *data) |
227 | { | |
cd12ae32 | 228 | struct nfs_read_data *rdata = data; |
dc70d7b3 | 229 | |
cd841605 | 230 | rdata->header->mds_ops->rpc_release(data); |
dc70d7b3 AA |
231 | } |
232 | ||
a69aef14 FI |
233 | static int filelayout_write_done_cb(struct rpc_task *task, |
234 | struct nfs_write_data *data) | |
235 | { | |
cd841605 | 236 | struct nfs_pgio_header *hdr = data->header; |
a69aef14 FI |
237 | int reset = 0; |
238 | ||
239 | if (filelayout_async_handle_error(task, data->args.context->state, | |
240 | data->ds_clp, &reset) == -EAGAIN) { | |
a69aef14 FI |
241 | dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n", |
242 | __func__, data->ds_clp, data->ds_clp->cl_session); | |
243 | if (reset) { | |
cd841605 | 244 | pnfs_set_lo_fail(hdr->lseg); |
a69aef14 | 245 | nfs4_reset_write(task, data); |
d00c5d43 TM |
246 | } |
247 | rpc_restart_call_prepare(task); | |
a69aef14 FI |
248 | return -EAGAIN; |
249 | } | |
250 | ||
863a3c6c | 251 | filelayout_set_layoutcommit(data); |
a69aef14 FI |
252 | return 0; |
253 | } | |
254 | ||
e0c2b380 | 255 | /* Fake up some data that will cause nfs_commit_release to retry the writes. */ |
0b7c0153 | 256 | static void prepare_to_resend_writes(struct nfs_commit_data *data) |
e0c2b380 FI |
257 | { |
258 | struct nfs_page *first = nfs_list_entry(data->pages.next); | |
259 | ||
260 | data->task.tk_status = 0; | |
261 | memcpy(data->verf.verifier, first->wb_verf.verifier, | |
262 | sizeof(first->wb_verf.verifier)); | |
263 | data->verf.verifier[0]++; /* ensure verifier mismatch */ | |
264 | } | |
265 | ||
266 | static int filelayout_commit_done_cb(struct rpc_task *task, | |
0b7c0153 | 267 | struct nfs_commit_data *data) |
e0c2b380 FI |
268 | { |
269 | int reset = 0; | |
270 | ||
0b7c0153 | 271 | if (filelayout_async_handle_error(task, data->context->state, |
e0c2b380 FI |
272 | data->ds_clp, &reset) == -EAGAIN) { |
273 | dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n", | |
274 | __func__, data->ds_clp, data->ds_clp->cl_session); | |
275 | if (reset) { | |
276 | prepare_to_resend_writes(data); | |
1b0ae068 | 277 | pnfs_set_lo_fail(data->lseg); |
e0c2b380 | 278 | } else |
d00c5d43 | 279 | rpc_restart_call_prepare(task); |
e0c2b380 FI |
280 | return -EAGAIN; |
281 | } | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
a69aef14 FI |
286 | static void filelayout_write_prepare(struct rpc_task *task, void *data) |
287 | { | |
cd12ae32 | 288 | struct nfs_write_data *wdata = data; |
a69aef14 FI |
289 | |
290 | if (nfs41_setup_sequence(wdata->ds_clp->cl_session, | |
291 | &wdata->args.seq_args, &wdata->res.seq_res, | |
9d12b216 | 292 | task)) |
a69aef14 FI |
293 | return; |
294 | ||
295 | rpc_call_start(task); | |
296 | } | |
297 | ||
298 | static void filelayout_write_call_done(struct rpc_task *task, void *data) | |
299 | { | |
cd12ae32 | 300 | struct nfs_write_data *wdata = data; |
a69aef14 FI |
301 | |
302 | /* Note this may cause RPC to be resent */ | |
cd841605 | 303 | wdata->header->mds_ops->rpc_call_done(task, data); |
a69aef14 FI |
304 | } |
305 | ||
0a702195 WAA |
306 | static void filelayout_write_count_stats(struct rpc_task *task, void *data) |
307 | { | |
cd12ae32 | 308 | struct nfs_write_data *wdata = data; |
0a702195 | 309 | |
cd841605 | 310 | rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics); |
0a702195 WAA |
311 | } |
312 | ||
a69aef14 FI |
313 | static void filelayout_write_release(void *data) |
314 | { | |
cd12ae32 | 315 | struct nfs_write_data *wdata = data; |
a69aef14 | 316 | |
cd841605 | 317 | wdata->header->mds_ops->rpc_release(data); |
a69aef14 FI |
318 | } |
319 | ||
0b7c0153 | 320 | static void filelayout_commit_prepare(struct rpc_task *task, void *data) |
e0c2b380 | 321 | { |
0b7c0153 | 322 | struct nfs_commit_data *wdata = data; |
e0c2b380 | 323 | |
0b7c0153 FI |
324 | if (nfs41_setup_sequence(wdata->ds_clp->cl_session, |
325 | &wdata->args.seq_args, &wdata->res.seq_res, | |
326 | task)) | |
327 | return; | |
328 | ||
329 | rpc_call_start(task); | |
330 | } | |
331 | ||
332 | static void filelayout_write_commit_done(struct rpc_task *task, void *data) | |
333 | { | |
334 | struct nfs_commit_data *wdata = data; | |
335 | ||
336 | /* Note this may cause RPC to be resent */ | |
337 | wdata->mds_ops->rpc_call_done(task, data); | |
338 | } | |
339 | ||
340 | static void filelayout_commit_count_stats(struct rpc_task *task, void *data) | |
341 | { | |
342 | struct nfs_commit_data *cdata = data; | |
343 | ||
344 | rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics); | |
345 | } | |
346 | ||
347 | static void filelayout_commit_release(void *calldata) | |
348 | { | |
349 | struct nfs_commit_data *data = calldata; | |
350 | ||
f453a54a | 351 | data->completion_ops->completion(data); |
0b7c0153 FI |
352 | put_lseg(data->lseg); |
353 | nfs_commitdata_release(data); | |
e0c2b380 FI |
354 | } |
355 | ||
17280175 | 356 | static const struct rpc_call_ops filelayout_read_call_ops = { |
dc70d7b3 AA |
357 | .rpc_call_prepare = filelayout_read_prepare, |
358 | .rpc_call_done = filelayout_read_call_done, | |
0a702195 | 359 | .rpc_count_stats = filelayout_read_count_stats, |
dc70d7b3 AA |
360 | .rpc_release = filelayout_read_release, |
361 | }; | |
362 | ||
17280175 | 363 | static const struct rpc_call_ops filelayout_write_call_ops = { |
a69aef14 FI |
364 | .rpc_call_prepare = filelayout_write_prepare, |
365 | .rpc_call_done = filelayout_write_call_done, | |
0a702195 | 366 | .rpc_count_stats = filelayout_write_count_stats, |
a69aef14 FI |
367 | .rpc_release = filelayout_write_release, |
368 | }; | |
369 | ||
17280175 | 370 | static const struct rpc_call_ops filelayout_commit_call_ops = { |
0b7c0153 FI |
371 | .rpc_call_prepare = filelayout_commit_prepare, |
372 | .rpc_call_done = filelayout_write_commit_done, | |
373 | .rpc_count_stats = filelayout_commit_count_stats, | |
e0c2b380 FI |
374 | .rpc_release = filelayout_commit_release, |
375 | }; | |
376 | ||
dc70d7b3 AA |
377 | static enum pnfs_try_status |
378 | filelayout_read_pagelist(struct nfs_read_data *data) | |
379 | { | |
cd841605 FI |
380 | struct nfs_pgio_header *hdr = data->header; |
381 | struct pnfs_layout_segment *lseg = hdr->lseg; | |
dc70d7b3 AA |
382 | struct nfs4_pnfs_ds *ds; |
383 | loff_t offset = data->args.offset; | |
384 | u32 j, idx; | |
385 | struct nfs_fh *fh; | |
386 | int status; | |
387 | ||
388 | dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n", | |
cd841605 | 389 | __func__, hdr->inode->i_ino, |
dc70d7b3 AA |
390 | data->args.pgbase, (size_t)data->args.count, offset); |
391 | ||
c47abcf8 AA |
392 | if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags)) |
393 | return PNFS_NOT_ATTEMPTED; | |
394 | ||
dc70d7b3 AA |
395 | /* Retrieve the correct rpc_client for the byte range */ |
396 | j = nfs4_fl_calc_j_index(lseg, offset); | |
397 | idx = nfs4_fl_calc_ds_index(lseg, j); | |
398 | ds = nfs4_fl_prepare_ds(lseg, idx); | |
399 | if (!ds) { | |
568e8c49 AA |
400 | /* Either layout fh index faulty, or ds connect failed */ |
401 | set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags); | |
402 | set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags); | |
dc70d7b3 AA |
403 | return PNFS_NOT_ATTEMPTED; |
404 | } | |
c9895cb6 | 405 | dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr); |
dc70d7b3 AA |
406 | |
407 | /* No multipath support. Use first DS */ | |
408 | data->ds_clp = ds->ds_clp; | |
409 | fh = nfs4_fl_select_ds_fh(lseg, j); | |
410 | if (fh) | |
411 | data->args.fh = fh; | |
412 | ||
413 | data->args.offset = filelayout_get_dserver_offset(lseg, offset); | |
414 | data->mds_offset = offset; | |
415 | ||
416 | /* Perform an asynchronous read to ds */ | |
c5996c4e | 417 | status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data, |
dc70d7b3 AA |
418 | &filelayout_read_call_ops); |
419 | BUG_ON(status != 0); | |
420 | return PNFS_ATTEMPTED; | |
421 | } | |
422 | ||
a69aef14 | 423 | /* Perform async writes. */ |
0382b744 AA |
424 | static enum pnfs_try_status |
425 | filelayout_write_pagelist(struct nfs_write_data *data, int sync) | |
426 | { | |
cd841605 FI |
427 | struct nfs_pgio_header *hdr = data->header; |
428 | struct pnfs_layout_segment *lseg = hdr->lseg; | |
a69aef14 FI |
429 | struct nfs4_pnfs_ds *ds; |
430 | loff_t offset = data->args.offset; | |
431 | u32 j, idx; | |
432 | struct nfs_fh *fh; | |
433 | int status; | |
434 | ||
c47abcf8 AA |
435 | if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags)) |
436 | return PNFS_NOT_ATTEMPTED; | |
437 | ||
a69aef14 FI |
438 | /* Retrieve the correct rpc_client for the byte range */ |
439 | j = nfs4_fl_calc_j_index(lseg, offset); | |
440 | idx = nfs4_fl_calc_ds_index(lseg, j); | |
441 | ds = nfs4_fl_prepare_ds(lseg, idx); | |
442 | if (!ds) { | |
a030889a WAA |
443 | printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n", |
444 | __func__); | |
a69aef14 FI |
445 | set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags); |
446 | set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags); | |
447 | return PNFS_NOT_ATTEMPTED; | |
448 | } | |
c9895cb6 | 449 | dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__, |
cd841605 | 450 | hdr->inode->i_ino, sync, (size_t) data->args.count, offset, |
c9895cb6 | 451 | ds->ds_remotestr); |
a69aef14 | 452 | |
a69aef14 FI |
453 | data->write_done_cb = filelayout_write_done_cb; |
454 | data->ds_clp = ds->ds_clp; | |
455 | fh = nfs4_fl_select_ds_fh(lseg, j); | |
456 | if (fh) | |
457 | data->args.fh = fh; | |
458 | /* | |
459 | * Get the file offset on the dserver. Set the write offset to | |
460 | * this offset and save the original offset. | |
461 | */ | |
462 | data->args.offset = filelayout_get_dserver_offset(lseg, offset); | |
a69aef14 FI |
463 | |
464 | /* Perform an asynchronous write */ | |
c5996c4e | 465 | status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data, |
a69aef14 FI |
466 | &filelayout_write_call_ops, sync); |
467 | BUG_ON(status != 0); | |
468 | return PNFS_ATTEMPTED; | |
0382b744 AA |
469 | } |
470 | ||
16b374ca AA |
471 | /* |
472 | * filelayout_check_layout() | |
473 | * | |
474 | * Make sure layout segment parameters are sane WRT the device. | |
475 | * At this point no generic layer initialization of the lseg has occurred, | |
476 | * and nothing has been added to the layout_hdr cache. | |
477 | * | |
478 | */ | |
479 | static int | |
480 | filelayout_check_layout(struct pnfs_layout_hdr *lo, | |
481 | struct nfs4_filelayout_segment *fl, | |
482 | struct nfs4_layoutget_res *lgr, | |
a75b9df9 TM |
483 | struct nfs4_deviceid *id, |
484 | gfp_t gfp_flags) | |
16b374ca | 485 | { |
a1eaecbc | 486 | struct nfs4_deviceid_node *d; |
16b374ca AA |
487 | struct nfs4_file_layout_dsaddr *dsaddr; |
488 | int status = -EINVAL; | |
b7edfaa1 | 489 | struct nfs_server *nfss = NFS_SERVER(lo->plh_inode); |
16b374ca AA |
490 | |
491 | dprintk("--> %s\n", __func__); | |
492 | ||
7c24d948 AA |
493 | /* FIXME: remove this check when layout segment support is added */ |
494 | if (lgr->range.offset != 0 || | |
495 | lgr->range.length != NFS4_MAX_UINT64) { | |
496 | dprintk("%s Only whole file layouts supported. Use MDS i/o\n", | |
497 | __func__); | |
498 | goto out; | |
499 | } | |
500 | ||
16b374ca | 501 | if (fl->pattern_offset > lgr->range.offset) { |
3b6445a6 | 502 | dprintk("%s pattern_offset %lld too large\n", |
16b374ca AA |
503 | __func__, fl->pattern_offset); |
504 | goto out; | |
505 | } | |
506 | ||
75247aff BH |
507 | if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) { |
508 | dprintk("%s Invalid stripe unit (%u)\n", | |
16b374ca AA |
509 | __func__, fl->stripe_unit); |
510 | goto out; | |
511 | } | |
512 | ||
513 | /* find and reference the deviceid */ | |
35c8bb54 BH |
514 | d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld, |
515 | NFS_SERVER(lo->plh_inode)->nfs_client, id); | |
a1eaecbc | 516 | if (d == NULL) { |
a75b9df9 | 517 | dsaddr = get_device_info(lo->plh_inode, id, gfp_flags); |
16b374ca AA |
518 | if (dsaddr == NULL) |
519 | goto out; | |
a1eaecbc BH |
520 | } else |
521 | dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node); | |
c47abcf8 AA |
522 | /* Found deviceid is being reaped */ |
523 | if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags)) | |
524 | goto out_put; | |
525 | ||
16b374ca AA |
526 | fl->dsaddr = dsaddr; |
527 | ||
e414966b CL |
528 | if (fl->first_stripe_index >= dsaddr->stripe_count) { |
529 | dprintk("%s Bad first_stripe_index %u\n", | |
16b374ca AA |
530 | __func__, fl->first_stripe_index); |
531 | goto out_put; | |
532 | } | |
533 | ||
534 | if ((fl->stripe_type == STRIPE_SPARSE && | |
535 | fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) || | |
536 | (fl->stripe_type == STRIPE_DENSE && | |
537 | fl->num_fh != dsaddr->stripe_count)) { | |
538 | dprintk("%s num_fh %u not valid for given packing\n", | |
539 | __func__, fl->num_fh); | |
540 | goto out_put; | |
541 | } | |
542 | ||
543 | if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) { | |
544 | dprintk("%s Stripe unit (%u) not aligned with rsize %u " | |
545 | "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize, | |
546 | nfss->wsize); | |
547 | } | |
548 | ||
549 | status = 0; | |
550 | out: | |
551 | dprintk("--> %s returns %d\n", __func__, status); | |
552 | return status; | |
553 | out_put: | |
ea8eecdd | 554 | nfs4_fl_put_deviceid(dsaddr); |
16b374ca AA |
555 | goto out; |
556 | } | |
557 | ||
558 | static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl) | |
559 | { | |
560 | int i; | |
561 | ||
562 | for (i = 0; i < fl->num_fh; i++) { | |
563 | if (!fl->fh_array[i]) | |
564 | break; | |
565 | kfree(fl->fh_array[i]); | |
566 | } | |
567 | kfree(fl->fh_array); | |
568 | fl->fh_array = NULL; | |
569 | } | |
570 | ||
571 | static void | |
572 | _filelayout_free_lseg(struct nfs4_filelayout_segment *fl) | |
573 | { | |
574 | filelayout_free_fh_array(fl); | |
575 | kfree(fl); | |
576 | } | |
577 | ||
578 | static int | |
579 | filelayout_decode_layout(struct pnfs_layout_hdr *flo, | |
580 | struct nfs4_filelayout_segment *fl, | |
581 | struct nfs4_layoutget_res *lgr, | |
a75b9df9 TM |
582 | struct nfs4_deviceid *id, |
583 | gfp_t gfp_flags) | |
16b374ca | 584 | { |
35124a09 | 585 | struct xdr_stream stream; |
f7da7a12 | 586 | struct xdr_buf buf; |
35124a09 WAA |
587 | struct page *scratch; |
588 | __be32 *p; | |
16b374ca AA |
589 | uint32_t nfl_util; |
590 | int i; | |
591 | ||
592 | dprintk("%s: set_layout_map Begin\n", __func__); | |
593 | ||
a75b9df9 | 594 | scratch = alloc_page(gfp_flags); |
35124a09 WAA |
595 | if (!scratch) |
596 | return -ENOMEM; | |
597 | ||
f7da7a12 | 598 | xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len); |
35124a09 WAA |
599 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
600 | ||
601 | /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8), | |
602 | * num_fh (4) */ | |
603 | p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20); | |
604 | if (unlikely(!p)) | |
605 | goto out_err; | |
606 | ||
16b374ca AA |
607 | memcpy(id, p, sizeof(*id)); |
608 | p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); | |
a1eaecbc | 609 | nfs4_print_deviceid(id); |
16b374ca AA |
610 | |
611 | nfl_util = be32_to_cpup(p++); | |
612 | if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS) | |
613 | fl->commit_through_mds = 1; | |
614 | if (nfl_util & NFL4_UFLG_DENSE) | |
615 | fl->stripe_type = STRIPE_DENSE; | |
616 | else | |
617 | fl->stripe_type = STRIPE_SPARSE; | |
618 | fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK; | |
619 | ||
620 | fl->first_stripe_index = be32_to_cpup(p++); | |
621 | p = xdr_decode_hyper(p, &fl->pattern_offset); | |
622 | fl->num_fh = be32_to_cpup(p++); | |
623 | ||
624 | dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n", | |
625 | __func__, nfl_util, fl->num_fh, fl->first_stripe_index, | |
626 | fl->pattern_offset); | |
627 | ||
cec765cf AA |
628 | /* Note that a zero value for num_fh is legal for STRIPE_SPARSE. |
629 | * Futher checking is done in filelayout_check_layout */ | |
e414966b | 630 | if (fl->num_fh > |
cec765cf | 631 | max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT)) |
35124a09 WAA |
632 | goto out_err; |
633 | ||
cec765cf AA |
634 | if (fl->num_fh > 0) { |
635 | fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *), | |
636 | gfp_flags); | |
637 | if (!fl->fh_array) | |
638 | goto out_err; | |
639 | } | |
16b374ca AA |
640 | |
641 | for (i = 0; i < fl->num_fh; i++) { | |
642 | /* Do we want to use a mempool here? */ | |
a75b9df9 | 643 | fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags); |
35124a09 WAA |
644 | if (!fl->fh_array[i]) |
645 | goto out_err_free; | |
646 | ||
647 | p = xdr_inline_decode(&stream, 4); | |
648 | if (unlikely(!p)) | |
649 | goto out_err_free; | |
16b374ca AA |
650 | fl->fh_array[i]->size = be32_to_cpup(p++); |
651 | if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) { | |
f9fd2d9c | 652 | printk(KERN_ERR "NFS: Too big fh %d received %d\n", |
16b374ca | 653 | i, fl->fh_array[i]->size); |
35124a09 | 654 | goto out_err_free; |
16b374ca | 655 | } |
35124a09 WAA |
656 | |
657 | p = xdr_inline_decode(&stream, fl->fh_array[i]->size); | |
658 | if (unlikely(!p)) | |
659 | goto out_err_free; | |
16b374ca | 660 | memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size); |
16b374ca AA |
661 | dprintk("DEBUG: %s: fh len %d\n", __func__, |
662 | fl->fh_array[i]->size); | |
663 | } | |
664 | ||
35124a09 | 665 | __free_page(scratch); |
16b374ca | 666 | return 0; |
35124a09 WAA |
667 | |
668 | out_err_free: | |
669 | filelayout_free_fh_array(fl); | |
670 | out_err: | |
671 | __free_page(scratch); | |
672 | return -EIO; | |
16b374ca AA |
673 | } |
674 | ||
c879513e FI |
675 | static void |
676 | filelayout_free_lseg(struct pnfs_layout_segment *lseg) | |
677 | { | |
678 | struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg); | |
679 | ||
680 | dprintk("--> %s\n", __func__); | |
681 | nfs4_fl_put_deviceid(fl->dsaddr); | |
799ba8d5 FI |
682 | /* This assumes a single RW lseg */ |
683 | if (lseg->pls_range.iomode == IOMODE_RW) { | |
684 | struct nfs4_filelayout *flo; | |
685 | ||
686 | flo = FILELAYOUT_FROM_HDR(lseg->pls_layout); | |
687 | flo->commit_info.nbuckets = 0; | |
688 | kfree(flo->commit_info.buckets); | |
689 | flo->commit_info.buckets = NULL; | |
690 | } | |
c879513e FI |
691 | _filelayout_free_lseg(fl); |
692 | } | |
693 | ||
799ba8d5 FI |
694 | static int |
695 | filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg, | |
ea2cf228 | 696 | struct nfs_commit_info *cinfo, |
799ba8d5 FI |
697 | gfp_t gfp_flags) |
698 | { | |
699 | struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg); | |
ea2cf228 | 700 | struct pnfs_commit_bucket *buckets; |
799ba8d5 FI |
701 | int size; |
702 | ||
703 | if (fl->commit_through_mds) | |
704 | return 0; | |
ea2cf228 | 705 | if (cinfo->ds->nbuckets != 0) { |
799ba8d5 FI |
706 | /* This assumes there is only one IOMODE_RW lseg. What |
707 | * we really want to do is have a layout_hdr level | |
708 | * dictionary of <multipath_list4, fh> keys, each | |
709 | * associated with a struct list_head, populated by calls | |
710 | * to filelayout_write_pagelist(). | |
711 | * */ | |
712 | return 0; | |
713 | } | |
714 | ||
715 | size = (fl->stripe_type == STRIPE_SPARSE) ? | |
716 | fl->dsaddr->ds_num : fl->dsaddr->stripe_count; | |
717 | ||
ea2cf228 | 718 | buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket), |
799ba8d5 FI |
719 | gfp_flags); |
720 | if (!buckets) | |
721 | return -ENOMEM; | |
722 | else { | |
723 | int i; | |
724 | ||
ea2cf228 FI |
725 | spin_lock(cinfo->lock); |
726 | if (cinfo->ds->nbuckets != 0) | |
799ba8d5 FI |
727 | kfree(buckets); |
728 | else { | |
ea2cf228 FI |
729 | cinfo->ds->buckets = buckets; |
730 | cinfo->ds->nbuckets = size; | |
799ba8d5 FI |
731 | for (i = 0; i < size; i++) { |
732 | INIT_LIST_HEAD(&buckets[i].written); | |
733 | INIT_LIST_HEAD(&buckets[i].committing); | |
734 | } | |
735 | } | |
ea2cf228 | 736 | spin_unlock(cinfo->lock); |
799ba8d5 FI |
737 | return 0; |
738 | } | |
739 | } | |
740 | ||
16b374ca AA |
741 | static struct pnfs_layout_segment * |
742 | filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid, | |
a75b9df9 TM |
743 | struct nfs4_layoutget_res *lgr, |
744 | gfp_t gfp_flags) | |
16b374ca AA |
745 | { |
746 | struct nfs4_filelayout_segment *fl; | |
747 | int rc; | |
748 | struct nfs4_deviceid id; | |
749 | ||
750 | dprintk("--> %s\n", __func__); | |
a75b9df9 | 751 | fl = kzalloc(sizeof(*fl), gfp_flags); |
16b374ca AA |
752 | if (!fl) |
753 | return NULL; | |
754 | ||
a75b9df9 TM |
755 | rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags); |
756 | if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) { | |
16b374ca AA |
757 | _filelayout_free_lseg(fl); |
758 | return NULL; | |
759 | } | |
760 | return &fl->generic_hdr; | |
761 | } | |
762 | ||
94ad1c80 FI |
763 | /* |
764 | * filelayout_pg_test(). Called by nfs_can_coalesce_requests() | |
765 | * | |
18ad0a9f BH |
766 | * return true : coalesce page |
767 | * return false : don't coalesce page | |
94ad1c80 | 768 | */ |
1751c363 | 769 | static bool |
94ad1c80 FI |
770 | filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
771 | struct nfs_page *req) | |
772 | { | |
773 | u64 p_stripe, r_stripe; | |
774 | u32 stripe_unit; | |
775 | ||
19345cb2 BH |
776 | if (!pnfs_generic_pg_test(pgio, prev, req) || |
777 | !nfs_generic_pg_test(pgio, prev, req)) | |
778 | return false; | |
89a58e32 | 779 | |
b5542849 FI |
780 | p_stripe = (u64)req_offset(prev); |
781 | r_stripe = (u64)req_offset(req); | |
94ad1c80 FI |
782 | stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit; |
783 | ||
784 | do_div(p_stripe, stripe_unit); | |
785 | do_div(r_stripe, stripe_unit); | |
786 | ||
787 | return (p_stripe == r_stripe); | |
788 | } | |
789 | ||
17280175 | 790 | static void |
7c24d948 AA |
791 | filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio, |
792 | struct nfs_page *req) | |
793 | { | |
794 | BUG_ON(pgio->pg_lseg != NULL); | |
795 | ||
1825a0d0 FI |
796 | if (req->wb_offset != req->wb_pgbase) { |
797 | /* | |
798 | * Handling unaligned pages is difficult, because have to | |
799 | * somehow split a req in two in certain cases in the | |
800 | * pg.test code. Avoid this by just not using pnfs | |
801 | * in this case. | |
802 | */ | |
803 | nfs_pageio_reset_read_mds(pgio); | |
804 | return; | |
805 | } | |
7c24d948 AA |
806 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
807 | req->wb_context, | |
808 | 0, | |
809 | NFS4_MAX_UINT64, | |
810 | IOMODE_READ, | |
811 | GFP_KERNEL); | |
812 | /* If no lseg, fall back to read through mds */ | |
813 | if (pgio->pg_lseg == NULL) | |
1f945357 | 814 | nfs_pageio_reset_read_mds(pgio); |
7c24d948 AA |
815 | } |
816 | ||
17280175 | 817 | static void |
7c24d948 AA |
818 | filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio, |
819 | struct nfs_page *req) | |
820 | { | |
ea2cf228 | 821 | struct nfs_commit_info cinfo; |
799ba8d5 FI |
822 | int status; |
823 | ||
7c24d948 AA |
824 | BUG_ON(pgio->pg_lseg != NULL); |
825 | ||
1825a0d0 FI |
826 | if (req->wb_offset != req->wb_pgbase) |
827 | goto out_mds; | |
7c24d948 AA |
828 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
829 | req->wb_context, | |
830 | 0, | |
831 | NFS4_MAX_UINT64, | |
832 | IOMODE_RW, | |
833 | GFP_NOFS); | |
834 | /* If no lseg, fall back to write through mds */ | |
835 | if (pgio->pg_lseg == NULL) | |
799ba8d5 | 836 | goto out_mds; |
ea2cf228 FI |
837 | nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq); |
838 | status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS); | |
799ba8d5 FI |
839 | if (status < 0) { |
840 | put_lseg(pgio->pg_lseg); | |
841 | pgio->pg_lseg = NULL; | |
842 | goto out_mds; | |
843 | } | |
844 | return; | |
845 | out_mds: | |
846 | nfs_pageio_reset_write_mds(pgio); | |
7c24d948 AA |
847 | } |
848 | ||
1751c363 | 849 | static const struct nfs_pageio_ops filelayout_pg_read_ops = { |
7c24d948 | 850 | .pg_init = filelayout_pg_init_read, |
1751c363 | 851 | .pg_test = filelayout_pg_test, |
493292dd | 852 | .pg_doio = pnfs_generic_pg_readpages, |
1751c363 TM |
853 | }; |
854 | ||
855 | static const struct nfs_pageio_ops filelayout_pg_write_ops = { | |
7c24d948 | 856 | .pg_init = filelayout_pg_init_write, |
1751c363 | 857 | .pg_test = filelayout_pg_test, |
dce81290 | 858 | .pg_doio = pnfs_generic_pg_writepages, |
1751c363 TM |
859 | }; |
860 | ||
e0c2b380 FI |
861 | static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j) |
862 | { | |
863 | if (fl->stripe_type == STRIPE_SPARSE) | |
864 | return nfs4_fl_calc_ds_index(&fl->generic_hdr, j); | |
865 | else | |
866 | return j; | |
867 | } | |
868 | ||
d6d6dc7c FI |
869 | /* The generic layer is about to remove the req from the commit list. |
870 | * If this will make the bucket empty, it will need to put the lseg reference. | |
d6d6dc7c | 871 | */ |
8dd37758 | 872 | static void |
ea2cf228 FI |
873 | filelayout_clear_request_commit(struct nfs_page *req, |
874 | struct nfs_commit_info *cinfo) | |
d6d6dc7c | 875 | { |
8dd37758 | 876 | struct pnfs_layout_segment *freeme = NULL; |
8dd37758 | 877 | |
ea2cf228 | 878 | spin_lock(cinfo->lock); |
8dd37758 TM |
879 | if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags)) |
880 | goto out; | |
ea2cf228 | 881 | cinfo->ds->nwritten--; |
d6d6dc7c | 882 | if (list_is_singular(&req->wb_list)) { |
ea2cf228 | 883 | struct pnfs_commit_bucket *bucket; |
d6d6dc7c | 884 | |
799ba8d5 | 885 | bucket = list_first_entry(&req->wb_list, |
ea2cf228 | 886 | struct pnfs_commit_bucket, |
799ba8d5 FI |
887 | written); |
888 | freeme = bucket->wlseg; | |
889 | bucket->wlseg = NULL; | |
d6d6dc7c | 890 | } |
8dd37758 | 891 | out: |
ea2cf228 FI |
892 | nfs_request_remove_commit_list(req, cinfo); |
893 | spin_unlock(cinfo->lock); | |
8dd37758 | 894 | put_lseg(freeme); |
d6d6dc7c FI |
895 | } |
896 | ||
897 | static struct list_head * | |
898 | filelayout_choose_commit_list(struct nfs_page *req, | |
ea2cf228 FI |
899 | struct pnfs_layout_segment *lseg, |
900 | struct nfs_commit_info *cinfo) | |
e0c2b380 | 901 | { |
e0c2b380 FI |
902 | struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg); |
903 | u32 i, j; | |
904 | struct list_head *list; | |
ea2cf228 | 905 | struct pnfs_commit_bucket *buckets; |
e0c2b380 | 906 | |
d6d6dc7c | 907 | if (fl->commit_through_mds) |
ea2cf228 | 908 | return &cinfo->mds->list; |
d6d6dc7c | 909 | |
e0c2b380 FI |
910 | /* Note that we are calling nfs4_fl_calc_j_index on each page |
911 | * that ends up being committed to a data server. An attractive | |
912 | * alternative is to add a field to nfs_write_data and nfs_page | |
913 | * to store the value calculated in filelayout_write_pagelist | |
914 | * and just use that here. | |
915 | */ | |
b5542849 | 916 | j = nfs4_fl_calc_j_index(lseg, req_offset(req)); |
e0c2b380 | 917 | i = select_bucket_index(fl, j); |
ea2cf228 | 918 | buckets = cinfo->ds->buckets; |
799ba8d5 | 919 | list = &buckets[i].written; |
e0c2b380 | 920 | if (list_empty(list)) { |
d6d6dc7c FI |
921 | /* Non-empty buckets hold a reference on the lseg. That ref |
922 | * is normally transferred to the COMMIT call and released | |
923 | * there. It could also be released if the last req is pulled | |
924 | * off due to a rewrite, in which case it will be done in | |
799ba8d5 | 925 | * filelayout_clear_request_commit |
d6d6dc7c | 926 | */ |
799ba8d5 | 927 | buckets[i].wlseg = get_lseg(lseg); |
e0c2b380 | 928 | } |
8dd37758 | 929 | set_bit(PG_COMMIT_TO_DS, &req->wb_flags); |
ea2cf228 | 930 | cinfo->ds->nwritten++; |
e0c2b380 FI |
931 | return list; |
932 | } | |
933 | ||
8dd37758 TM |
934 | static void |
935 | filelayout_mark_request_commit(struct nfs_page *req, | |
ea2cf228 FI |
936 | struct pnfs_layout_segment *lseg, |
937 | struct nfs_commit_info *cinfo) | |
8dd37758 TM |
938 | { |
939 | struct list_head *list; | |
940 | ||
ea2cf228 FI |
941 | list = filelayout_choose_commit_list(req, lseg, cinfo); |
942 | nfs_request_add_commit_list(req, list, cinfo); | |
8dd37758 TM |
943 | } |
944 | ||
e0c2b380 FI |
945 | static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i) |
946 | { | |
947 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); | |
948 | ||
949 | if (flseg->stripe_type == STRIPE_SPARSE) | |
950 | return i; | |
951 | else | |
952 | return nfs4_fl_calc_ds_index(lseg, i); | |
953 | } | |
954 | ||
955 | static struct nfs_fh * | |
956 | select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i) | |
957 | { | |
958 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); | |
959 | ||
960 | if (flseg->stripe_type == STRIPE_SPARSE) { | |
961 | if (flseg->num_fh == 1) | |
962 | i = 0; | |
963 | else if (flseg->num_fh == 0) | |
964 | /* Use the MDS OPEN fh set in nfs_read_rpcsetup */ | |
965 | return NULL; | |
966 | } | |
967 | return flseg->fh_array[i]; | |
968 | } | |
969 | ||
0b7c0153 | 970 | static int filelayout_initiate_commit(struct nfs_commit_data *data, int how) |
e0c2b380 FI |
971 | { |
972 | struct pnfs_layout_segment *lseg = data->lseg; | |
973 | struct nfs4_pnfs_ds *ds; | |
974 | u32 idx; | |
975 | struct nfs_fh *fh; | |
976 | ||
977 | idx = calc_ds_index_from_commit(lseg, data->ds_commit_index); | |
978 | ds = nfs4_fl_prepare_ds(lseg, idx); | |
979 | if (!ds) { | |
a030889a WAA |
980 | printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n", |
981 | __func__); | |
e0c2b380 FI |
982 | set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags); |
983 | set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags); | |
984 | prepare_to_resend_writes(data); | |
8dd37758 | 985 | filelayout_commit_release(data); |
e0c2b380 FI |
986 | return -EAGAIN; |
987 | } | |
988 | dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how); | |
0b7c0153 | 989 | data->commit_done_cb = filelayout_commit_done_cb; |
e0c2b380 FI |
990 | data->ds_clp = ds->ds_clp; |
991 | fh = select_ds_fh_from_commit(lseg, data->ds_commit_index); | |
992 | if (fh) | |
993 | data->args.fh = fh; | |
0b7c0153 | 994 | return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data, |
e0c2b380 FI |
995 | &filelayout_commit_call_ops, how); |
996 | } | |
997 | ||
8dd37758 | 998 | static int |
ea2cf228 FI |
999 | filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket, |
1000 | struct nfs_commit_info *cinfo, | |
1001 | int max) | |
8dd37758 TM |
1002 | { |
1003 | struct list_head *src = &bucket->written; | |
1004 | struct list_head *dst = &bucket->committing; | |
1005 | struct nfs_page *req, *tmp; | |
1006 | int ret = 0; | |
1007 | ||
1008 | list_for_each_entry_safe(req, tmp, src, wb_list) { | |
1009 | if (!nfs_lock_request(req)) | |
1010 | continue; | |
ea2cf228 | 1011 | if (cond_resched_lock(cinfo->lock)) |
3b3be88d | 1012 | list_safe_reset_next(req, tmp, wb_list); |
ea2cf228 | 1013 | nfs_request_remove_commit_list(req, cinfo); |
8dd37758 TM |
1014 | clear_bit(PG_COMMIT_TO_DS, &req->wb_flags); |
1015 | nfs_list_add_request(req, dst); | |
1016 | ret++; | |
1017 | if (ret == max) | |
1018 | break; | |
1019 | } | |
799ba8d5 | 1020 | if (ret) { |
ea2cf228 FI |
1021 | cinfo->ds->nwritten -= ret; |
1022 | cinfo->ds->ncommitting += ret; | |
799ba8d5 FI |
1023 | bucket->clseg = bucket->wlseg; |
1024 | if (list_empty(src)) | |
1025 | bucket->wlseg = NULL; | |
1026 | else | |
1027 | get_lseg(bucket->clseg); | |
1028 | } | |
8dd37758 TM |
1029 | return ret; |
1030 | } | |
1031 | ||
d6d6dc7c | 1032 | /* Move reqs from written to committing lists, returning count of number moved. |
ea2cf228 | 1033 | * Note called with cinfo->lock held. |
d6d6dc7c | 1034 | */ |
ea2cf228 FI |
1035 | static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo, |
1036 | int max) | |
d6d6dc7c | 1037 | { |
d6d6dc7c FI |
1038 | int i, rv = 0, cnt; |
1039 | ||
ea2cf228 FI |
1040 | for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) { |
1041 | cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i], | |
1042 | cinfo, max); | |
d6d6dc7c FI |
1043 | max -= cnt; |
1044 | rv += cnt; | |
1045 | } | |
d6d6dc7c FI |
1046 | return rv; |
1047 | } | |
1048 | ||
9390f425 | 1049 | static unsigned int |
ea2cf228 | 1050 | alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list) |
e0c2b380 | 1051 | { |
ea2cf228 FI |
1052 | struct pnfs_ds_commit_info *fl_cinfo; |
1053 | struct pnfs_commit_bucket *bucket; | |
0b7c0153 | 1054 | struct nfs_commit_data *data; |
e0c2b380 | 1055 | int i, j; |
9390f425 | 1056 | unsigned int nreq = 0; |
e0c2b380 | 1057 | |
ea2cf228 | 1058 | fl_cinfo = cinfo->ds; |
799ba8d5 FI |
1059 | bucket = fl_cinfo->buckets; |
1060 | for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) { | |
1061 | if (list_empty(&bucket->committing)) | |
e0c2b380 FI |
1062 | continue; |
1063 | data = nfs_commitdata_alloc(); | |
1064 | if (!data) | |
9390f425 | 1065 | break; |
e0c2b380 | 1066 | data->ds_commit_index = i; |
799ba8d5 FI |
1067 | data->lseg = bucket->clseg; |
1068 | bucket->clseg = NULL; | |
e0c2b380 | 1069 | list_add(&data->pages, list); |
9390f425 | 1070 | nreq++; |
e0c2b380 | 1071 | } |
e0c2b380 | 1072 | |
9390f425 | 1073 | /* Clean up on error */ |
799ba8d5 FI |
1074 | for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) { |
1075 | if (list_empty(&bucket->committing)) | |
e0c2b380 | 1076 | continue; |
ea2cf228 | 1077 | nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo); |
799ba8d5 FI |
1078 | put_lseg(bucket->clseg); |
1079 | bucket->clseg = NULL; | |
e0c2b380 | 1080 | } |
e0c2b380 | 1081 | /* Caller will clean up entries put on list */ |
9390f425 | 1082 | return nreq; |
e0c2b380 FI |
1083 | } |
1084 | ||
1085 | /* This follows nfs_commit_list pretty closely */ | |
1086 | static int | |
1087 | filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages, | |
ea2cf228 | 1088 | int how, struct nfs_commit_info *cinfo) |
e0c2b380 | 1089 | { |
0b7c0153 | 1090 | struct nfs_commit_data *data, *tmp; |
e0c2b380 | 1091 | LIST_HEAD(list); |
9390f425 | 1092 | unsigned int nreq = 0; |
e0c2b380 FI |
1093 | |
1094 | if (!list_empty(mds_pages)) { | |
1095 | data = nfs_commitdata_alloc(); | |
9390f425 TM |
1096 | if (data != NULL) { |
1097 | data->lseg = NULL; | |
1098 | list_add(&data->pages, &list); | |
1099 | nreq++; | |
1100 | } else | |
ea2cf228 | 1101 | nfs_retry_commit(mds_pages, NULL, cinfo); |
e0c2b380 FI |
1102 | } |
1103 | ||
ea2cf228 | 1104 | nreq += alloc_ds_commits(cinfo, &list); |
9390f425 TM |
1105 | |
1106 | if (nreq == 0) { | |
f453a54a | 1107 | cinfo->completion_ops->error_cleanup(NFS_I(inode)); |
9390f425 TM |
1108 | goto out; |
1109 | } | |
1110 | ||
ea2cf228 | 1111 | atomic_add(nreq, &cinfo->mds->rpcs_out); |
e0c2b380 FI |
1112 | |
1113 | list_for_each_entry_safe(data, tmp, &list, pages) { | |
1114 | list_del_init(&data->pages); | |
e0c2b380 | 1115 | if (!data->lseg) { |
f453a54a | 1116 | nfs_init_commit(data, mds_pages, NULL, cinfo); |
0b7c0153 | 1117 | nfs_initiate_commit(NFS_CLIENT(inode), data, |
e0c2b380 FI |
1118 | data->mds_ops, how); |
1119 | } else { | |
ea2cf228 | 1120 | struct pnfs_commit_bucket *buckets; |
799ba8d5 | 1121 | |
ea2cf228 | 1122 | buckets = cinfo->ds->buckets; |
f453a54a | 1123 | nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo); |
e0c2b380 FI |
1124 | filelayout_initiate_commit(data, how); |
1125 | } | |
1126 | } | |
9390f425 | 1127 | out: |
ea2cf228 | 1128 | cinfo->ds->ncommitting = 0; |
9390f425 | 1129 | return PNFS_ATTEMPTED; |
e0c2b380 FI |
1130 | } |
1131 | ||
1775bc34 BH |
1132 | static void |
1133 | filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d) | |
1134 | { | |
1135 | nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node)); | |
1136 | } | |
1137 | ||
799ba8d5 FI |
1138 | static struct pnfs_layout_hdr * |
1139 | filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags) | |
1140 | { | |
1141 | struct nfs4_filelayout *flo; | |
1142 | ||
1143 | flo = kzalloc(sizeof(*flo), gfp_flags); | |
1144 | return &flo->generic_hdr; | |
1145 | } | |
1146 | ||
1147 | static void | |
1148 | filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
1149 | { | |
1150 | kfree(FILELAYOUT_FROM_HDR(lo)); | |
1151 | } | |
1152 | ||
ea2cf228 FI |
1153 | static struct pnfs_ds_commit_info * |
1154 | filelayout_get_ds_info(struct inode *inode) | |
1155 | { | |
1156 | return &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info; | |
1157 | } | |
1158 | ||
7ab672ce | 1159 | static struct pnfs_layoutdriver_type filelayout_type = { |
ea8eecdd CH |
1160 | .id = LAYOUT_NFSV4_1_FILES, |
1161 | .name = "LAYOUT_NFSV4_1_FILES", | |
1162 | .owner = THIS_MODULE, | |
799ba8d5 FI |
1163 | .alloc_layout_hdr = filelayout_alloc_layout_hdr, |
1164 | .free_layout_hdr = filelayout_free_layout_hdr, | |
ea8eecdd CH |
1165 | .alloc_lseg = filelayout_alloc_lseg, |
1166 | .free_lseg = filelayout_free_lseg, | |
1751c363 TM |
1167 | .pg_read_ops = &filelayout_pg_read_ops, |
1168 | .pg_write_ops = &filelayout_pg_write_ops, | |
ea2cf228 | 1169 | .get_ds_info = &filelayout_get_ds_info, |
8dd37758 TM |
1170 | .mark_request_commit = filelayout_mark_request_commit, |
1171 | .clear_request_commit = filelayout_clear_request_commit, | |
d6d6dc7c | 1172 | .scan_commit_lists = filelayout_scan_commit_lists, |
e0c2b380 | 1173 | .commit_pagelist = filelayout_commit_pagelist, |
dc70d7b3 | 1174 | .read_pagelist = filelayout_read_pagelist, |
0382b744 | 1175 | .write_pagelist = filelayout_write_pagelist, |
1775bc34 | 1176 | .free_deviceid_node = filelayout_free_deveiceid_node, |
7ab672ce DH |
1177 | }; |
1178 | ||
1179 | static int __init nfs4filelayout_init(void) | |
1180 | { | |
1181 | printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n", | |
1182 | __func__); | |
1183 | return pnfs_register_layoutdriver(&filelayout_type); | |
1184 | } | |
1185 | ||
1186 | static void __exit nfs4filelayout_exit(void) | |
1187 | { | |
1188 | printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n", | |
1189 | __func__); | |
1190 | pnfs_unregister_layoutdriver(&filelayout_type); | |
1191 | } | |
1192 | ||
f85ef69c BF |
1193 | MODULE_ALIAS("nfs-layouttype4-1"); |
1194 | ||
7ab672ce DH |
1195 | module_init(nfs4filelayout_init); |
1196 | module_exit(nfs4filelayout_exit); |