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