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