Commit | Line | Data |
---|---|---|
85e174ba RL |
1 | /* |
2 | * pNFS functions to call and manage layout drivers. | |
3 | * | |
4 | * Copyright (c) 2002 [year of first publication] | |
5 | * The Regents of the University of Michigan | |
6 | * All Rights Reserved | |
7 | * | |
8 | * Dean Hildebrand <dhildebz@umich.edu> | |
9 | * | |
10 | * Permission is granted to use, copy, create derivative works, and | |
11 | * redistribute this software and such derivative works for any purpose, | |
12 | * so long as the name of the University of Michigan is not used in | |
13 | * any advertising or publicity pertaining to the use or distribution | |
14 | * of this software without specific, written prior authorization. If | |
15 | * the above copyright notice or any other identification of the | |
16 | * University of Michigan is included in any copy of any portion of | |
17 | * this software, then the disclaimer below must also be included. | |
18 | * | |
19 | * This software is provided as is, without representation or warranty | |
20 | * of any kind either express or implied, including without limitation | |
21 | * the implied warranties of merchantability, fitness for a particular | |
22 | * purpose, or noninfringement. The Regents of the University of | |
23 | * Michigan shall not be liable for any damages, including special, | |
24 | * indirect, incidental, or consequential damages, with respect to any | |
25 | * claim arising out of or in connection with the use of the software, | |
26 | * even if it has been or is hereafter advised of the possibility of | |
27 | * such damages. | |
28 | */ | |
29 | ||
30 | #include <linux/nfs_fs.h> | |
493292dd | 31 | #include <linux/nfs_page.h> |
143cb494 | 32 | #include <linux/module.h> |
974cec8c | 33 | #include "internal.h" |
85e174ba | 34 | #include "pnfs.h" |
64419a9b | 35 | #include "iostat.h" |
85e174ba RL |
36 | |
37 | #define NFSDBG_FACILITY NFSDBG_PNFS | |
25c75333 | 38 | #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ) |
85e174ba | 39 | |
02c35fca FI |
40 | /* Locking: |
41 | * | |
42 | * pnfs_spinlock: | |
43 | * protects pnfs_modules_tbl. | |
44 | */ | |
45 | static DEFINE_SPINLOCK(pnfs_spinlock); | |
46 | ||
47 | /* | |
48 | * pnfs_modules_tbl holds all pnfs modules | |
49 | */ | |
50 | static LIST_HEAD(pnfs_modules_tbl); | |
51 | ||
52 | /* Return the registered pnfs layout driver module matching given id */ | |
53 | static struct pnfs_layoutdriver_type * | |
54 | find_pnfs_driver_locked(u32 id) | |
55 | { | |
56 | struct pnfs_layoutdriver_type *local; | |
57 | ||
58 | list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) | |
59 | if (local->id == id) | |
60 | goto out; | |
61 | local = NULL; | |
62 | out: | |
63 | dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); | |
64 | return local; | |
65 | } | |
66 | ||
85e174ba RL |
67 | static struct pnfs_layoutdriver_type * |
68 | find_pnfs_driver(u32 id) | |
69 | { | |
02c35fca FI |
70 | struct pnfs_layoutdriver_type *local; |
71 | ||
72 | spin_lock(&pnfs_spinlock); | |
73 | local = find_pnfs_driver_locked(id); | |
0a9c63fa TM |
74 | if (local != NULL && !try_module_get(local->owner)) { |
75 | dprintk("%s: Could not grab reference on module\n", __func__); | |
76 | local = NULL; | |
77 | } | |
02c35fca FI |
78 | spin_unlock(&pnfs_spinlock); |
79 | return local; | |
85e174ba RL |
80 | } |
81 | ||
82 | void | |
83 | unset_pnfs_layoutdriver(struct nfs_server *nfss) | |
84 | { | |
738fd0f3 BH |
85 | if (nfss->pnfs_curr_ld) { |
86 | if (nfss->pnfs_curr_ld->clear_layoutdriver) | |
87 | nfss->pnfs_curr_ld->clear_layoutdriver(nfss); | |
2a4c8994 TM |
88 | /* Decrement the MDS count. Purge the deviceid cache if zero */ |
89 | if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count)) | |
90 | nfs4_deviceid_purge_client(nfss->nfs_client); | |
02c35fca | 91 | module_put(nfss->pnfs_curr_ld->owner); |
738fd0f3 | 92 | } |
85e174ba RL |
93 | nfss->pnfs_curr_ld = NULL; |
94 | } | |
95 | ||
96 | /* | |
97 | * Try to set the server's pnfs module to the pnfs layout type specified by id. | |
98 | * Currently only one pNFS layout driver per filesystem is supported. | |
99 | * | |
100 | * @id layout type. Zero (illegal layout type) indicates pNFS not in use. | |
101 | */ | |
102 | void | |
738fd0f3 BH |
103 | set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh, |
104 | u32 id) | |
85e174ba RL |
105 | { |
106 | struct pnfs_layoutdriver_type *ld_type = NULL; | |
107 | ||
108 | if (id == 0) | |
109 | goto out_no_driver; | |
110 | if (!(server->nfs_client->cl_exchange_flags & | |
111 | (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { | |
a030889a WAA |
112 | printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n", |
113 | __func__, id, server->nfs_client->cl_exchange_flags); | |
85e174ba RL |
114 | goto out_no_driver; |
115 | } | |
116 | ld_type = find_pnfs_driver(id); | |
117 | if (!ld_type) { | |
118 | request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id); | |
119 | ld_type = find_pnfs_driver(id); | |
120 | if (!ld_type) { | |
121 | dprintk("%s: No pNFS module found for %u.\n", | |
122 | __func__, id); | |
123 | goto out_no_driver; | |
124 | } | |
125 | } | |
126 | server->pnfs_curr_ld = ld_type; | |
738fd0f3 BH |
127 | if (ld_type->set_layoutdriver |
128 | && ld_type->set_layoutdriver(server, mntfh)) { | |
a030889a WAA |
129 | printk(KERN_ERR "NFS: %s: Error initializing pNFS layout " |
130 | "driver %u.\n", __func__, id); | |
738fd0f3 BH |
131 | module_put(ld_type->owner); |
132 | goto out_no_driver; | |
133 | } | |
2a4c8994 TM |
134 | /* Bump the MDS count */ |
135 | atomic_inc(&server->nfs_client->cl_mds_count); | |
ea8eecdd | 136 | |
85e174ba RL |
137 | dprintk("%s: pNFS module for %u set\n", __func__, id); |
138 | return; | |
139 | ||
140 | out_no_driver: | |
141 | dprintk("%s: Using NFSv4 I/O\n", __func__); | |
142 | server->pnfs_curr_ld = NULL; | |
143 | } | |
02c35fca FI |
144 | |
145 | int | |
146 | pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) | |
147 | { | |
148 | int status = -EINVAL; | |
149 | struct pnfs_layoutdriver_type *tmp; | |
150 | ||
151 | if (ld_type->id == 0) { | |
a030889a | 152 | printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__); |
02c35fca FI |
153 | return status; |
154 | } | |
b1f69b75 | 155 | if (!ld_type->alloc_lseg || !ld_type->free_lseg) { |
a030889a | 156 | printk(KERN_ERR "NFS: %s Layout driver must provide " |
b1f69b75 AA |
157 | "alloc_lseg and free_lseg.\n", __func__); |
158 | return status; | |
159 | } | |
02c35fca FI |
160 | |
161 | spin_lock(&pnfs_spinlock); | |
162 | tmp = find_pnfs_driver_locked(ld_type->id); | |
163 | if (!tmp) { | |
164 | list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); | |
165 | status = 0; | |
166 | dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, | |
167 | ld_type->name); | |
168 | } else { | |
a030889a | 169 | printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n", |
02c35fca FI |
170 | __func__, ld_type->id); |
171 | } | |
172 | spin_unlock(&pnfs_spinlock); | |
173 | ||
174 | return status; | |
175 | } | |
176 | EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); | |
177 | ||
178 | void | |
179 | pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) | |
180 | { | |
181 | dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); | |
182 | spin_lock(&pnfs_spinlock); | |
183 | list_del(&ld_type->pnfs_tblid); | |
184 | spin_unlock(&pnfs_spinlock); | |
185 | } | |
186 | EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); | |
e5e94017 | 187 | |
b1f69b75 AA |
188 | /* |
189 | * pNFS client layout cache | |
190 | */ | |
191 | ||
cc6e5340 | 192 | /* Need to hold i_lock if caller does not already hold reference */ |
43f1b3da | 193 | void |
70c3bd2b | 194 | pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo) |
e5e94017 | 195 | { |
cc6e5340 | 196 | atomic_inc(&lo->plh_refcount); |
e5e94017 BH |
197 | } |
198 | ||
636fb9c8 BH |
199 | static struct pnfs_layout_hdr * |
200 | pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) | |
201 | { | |
202 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; | |
203 | return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) : | |
204 | kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags); | |
205 | } | |
206 | ||
207 | static void | |
208 | pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) | |
209 | { | |
210 | struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld; | |
9fa40758 | 211 | put_rpccred(lo->plh_lc_cred); |
636fb9c8 BH |
212 | return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo); |
213 | } | |
214 | ||
e5e94017 | 215 | static void |
cc6e5340 | 216 | destroy_layout_hdr(struct pnfs_layout_hdr *lo) |
e5e94017 | 217 | { |
cc6e5340 FI |
218 | dprintk("%s: freeing layout cache %p\n", __func__, lo); |
219 | BUG_ON(!list_empty(&lo->plh_layouts)); | |
220 | NFS_I(lo->plh_inode)->layout = NULL; | |
636fb9c8 | 221 | pnfs_free_layout_hdr(lo); |
cc6e5340 | 222 | } |
e5e94017 | 223 | |
cc6e5340 | 224 | static void |
70c3bd2b | 225 | pnfs_put_layout_hdr_locked(struct pnfs_layout_hdr *lo) |
cc6e5340 FI |
226 | { |
227 | if (atomic_dec_and_test(&lo->plh_refcount)) | |
228 | destroy_layout_hdr(lo); | |
e5e94017 BH |
229 | } |
230 | ||
b1f69b75 | 231 | void |
70c3bd2b | 232 | pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) |
974cec8c | 233 | { |
cc6e5340 FI |
234 | struct inode *inode = lo->plh_inode; |
235 | ||
236 | if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { | |
237 | destroy_layout_hdr(lo); | |
238 | spin_unlock(&inode->i_lock); | |
239 | } | |
974cec8c AA |
240 | } |
241 | ||
b9e028fd TM |
242 | static int |
243 | pnfs_iomode_to_fail_bit(u32 iomode) | |
244 | { | |
245 | return iomode == IOMODE_RW ? | |
246 | NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED; | |
247 | } | |
248 | ||
249 | static void | |
250 | pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode) | |
251 | { | |
25c75333 | 252 | lo->plh_retry_timestamp = jiffies; |
b9e028fd TM |
253 | set_bit(pnfs_iomode_to_fail_bit(iomode), &lo->plh_flags); |
254 | dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__, | |
255 | iomode == IOMODE_RW ? "RW" : "READ"); | |
256 | } | |
257 | ||
258 | static bool | |
259 | pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode) | |
260 | { | |
25c75333 TM |
261 | unsigned long start, end; |
262 | if (test_bit(pnfs_iomode_to_fail_bit(iomode), &lo->plh_flags) == 0) | |
263 | return false; | |
264 | end = jiffies; | |
265 | start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT; | |
266 | if (!time_in_range(lo->plh_retry_timestamp, start, end)) { | |
267 | /* It is time to retry the failed layoutgets */ | |
268 | clear_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags); | |
269 | clear_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags); | |
270 | return false; | |
271 | } | |
272 | return true; | |
b9e028fd TM |
273 | } |
274 | ||
974cec8c AA |
275 | static void |
276 | init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg) | |
277 | { | |
566052c5 | 278 | INIT_LIST_HEAD(&lseg->pls_list); |
a9bae566 | 279 | INIT_LIST_HEAD(&lseg->pls_lc_list); |
4541d16c FI |
280 | atomic_set(&lseg->pls_refcount, 1); |
281 | smp_mb(); | |
282 | set_bit(NFS_LSEG_VALID, &lseg->pls_flags); | |
566052c5 | 283 | lseg->pls_layout = lo; |
974cec8c AA |
284 | } |
285 | ||
4541d16c | 286 | static void free_lseg(struct pnfs_layout_segment *lseg) |
974cec8c | 287 | { |
b7edfaa1 | 288 | struct inode *ino = lseg->pls_layout->plh_inode; |
974cec8c | 289 | |
b1f69b75 | 290 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); |
70c3bd2b TM |
291 | /* Matched by pnfs_get_layout_hdr in pnfs_insert_layout */ |
292 | pnfs_put_layout_hdr(NFS_I(ino)->layout); | |
974cec8c AA |
293 | } |
294 | ||
d684d2ae | 295 | static void |
9369a431 | 296 | pnfs_put_lseg_common(struct pnfs_layout_segment *lseg) |
d684d2ae FI |
297 | { |
298 | struct inode *inode = lseg->pls_layout->plh_inode; | |
299 | ||
d20581aa | 300 | WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); |
d684d2ae FI |
301 | list_del_init(&lseg->pls_list); |
302 | if (list_empty(&lseg->pls_layout->plh_segs)) { | |
303 | set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags); | |
304 | /* Matched by initial refcount set in alloc_init_layout_hdr */ | |
70c3bd2b | 305 | pnfs_put_layout_hdr_locked(lseg->pls_layout); |
d684d2ae FI |
306 | } |
307 | rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq); | |
308 | } | |
309 | ||
bae724ef | 310 | void |
9369a431 | 311 | pnfs_put_lseg(struct pnfs_layout_segment *lseg) |
974cec8c | 312 | { |
d684d2ae FI |
313 | struct inode *inode; |
314 | ||
315 | if (!lseg) | |
316 | return; | |
317 | ||
4541d16c FI |
318 | dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, |
319 | atomic_read(&lseg->pls_refcount), | |
320 | test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); | |
d684d2ae FI |
321 | inode = lseg->pls_layout->plh_inode; |
322 | if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { | |
323 | LIST_HEAD(free_me); | |
4541d16c | 324 | |
9369a431 | 325 | pnfs_put_lseg_common(lseg); |
d684d2ae FI |
326 | list_add(&lseg->pls_list, &free_me); |
327 | spin_unlock(&inode->i_lock); | |
328 | pnfs_free_lseg_list(&free_me); | |
4541d16c | 329 | } |
4541d16c | 330 | } |
9369a431 | 331 | EXPORT_SYMBOL_GPL(pnfs_put_lseg); |
974cec8c | 332 | |
fb3296eb BH |
333 | static inline u64 |
334 | end_offset(u64 start, u64 len) | |
335 | { | |
336 | u64 end; | |
337 | ||
338 | end = start + len; | |
339 | return end >= start ? end : NFS4_MAX_UINT64; | |
340 | } | |
341 | ||
342 | /* last octet in a range */ | |
343 | static inline u64 | |
344 | last_byte_offset(u64 start, u64 len) | |
345 | { | |
346 | u64 end; | |
347 | ||
348 | BUG_ON(!len); | |
349 | end = start + len; | |
350 | return end > start ? end - 1 : NFS4_MAX_UINT64; | |
351 | } | |
352 | ||
353 | /* | |
354 | * is l2 fully contained in l1? | |
355 | * start1 end1 | |
356 | * [----------------------------------) | |
357 | * start2 end2 | |
358 | * [----------------) | |
359 | */ | |
360 | static inline int | |
361 | lo_seg_contained(struct pnfs_layout_range *l1, | |
362 | struct pnfs_layout_range *l2) | |
363 | { | |
364 | u64 start1 = l1->offset; | |
365 | u64 end1 = end_offset(start1, l1->length); | |
366 | u64 start2 = l2->offset; | |
367 | u64 end2 = end_offset(start2, l2->length); | |
368 | ||
369 | return (start1 <= start2) && (end1 >= end2); | |
370 | } | |
371 | ||
372 | /* | |
373 | * is l1 and l2 intersecting? | |
374 | * start1 end1 | |
375 | * [----------------------------------) | |
376 | * start2 end2 | |
377 | * [----------------) | |
378 | */ | |
379 | static inline int | |
380 | lo_seg_intersecting(struct pnfs_layout_range *l1, | |
381 | struct pnfs_layout_range *l2) | |
382 | { | |
383 | u64 start1 = l1->offset; | |
384 | u64 end1 = end_offset(start1, l1->length); | |
385 | u64 start2 = l2->offset; | |
386 | u64 end2 = end_offset(start2, l2->length); | |
387 | ||
388 | return (end1 == NFS4_MAX_UINT64 || end1 > start2) && | |
389 | (end2 == NFS4_MAX_UINT64 || end2 > start1); | |
390 | } | |
391 | ||
4541d16c | 392 | static bool |
778b5502 BH |
393 | should_free_lseg(struct pnfs_layout_range *lseg_range, |
394 | struct pnfs_layout_range *recall_range) | |
4541d16c | 395 | { |
778b5502 BH |
396 | return (recall_range->iomode == IOMODE_ANY || |
397 | lseg_range->iomode == recall_range->iomode) && | |
398 | lo_seg_intersecting(lseg_range, recall_range); | |
974cec8c AA |
399 | } |
400 | ||
4541d16c FI |
401 | /* Returns 1 if lseg is removed from list, 0 otherwise */ |
402 | static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, | |
403 | struct list_head *tmp_list) | |
404 | { | |
405 | int rv = 0; | |
406 | ||
407 | if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { | |
408 | /* Remove the reference keeping the lseg in the | |
409 | * list. It will now be removed when all | |
410 | * outstanding io is finished. | |
411 | */ | |
d684d2ae FI |
412 | dprintk("%s: lseg %p ref %d\n", __func__, lseg, |
413 | atomic_read(&lseg->pls_refcount)); | |
414 | if (atomic_dec_and_test(&lseg->pls_refcount)) { | |
9369a431 | 415 | pnfs_put_lseg_common(lseg); |
d684d2ae FI |
416 | list_add(&lseg->pls_list, tmp_list); |
417 | rv = 1; | |
418 | } | |
4541d16c FI |
419 | } |
420 | return rv; | |
421 | } | |
422 | ||
423 | /* Returns count of number of matching invalid lsegs remaining in list | |
424 | * after call. | |
425 | */ | |
43f1b3da | 426 | int |
49a85061 | 427 | pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, |
4541d16c | 428 | struct list_head *tmp_list, |
778b5502 | 429 | struct pnfs_layout_range *recall_range) |
974cec8c AA |
430 | { |
431 | struct pnfs_layout_segment *lseg, *next; | |
4541d16c | 432 | int invalid = 0, removed = 0; |
974cec8c AA |
433 | |
434 | dprintk("%s:Begin lo %p\n", __func__, lo); | |
435 | ||
38511722 | 436 | if (list_empty(&lo->plh_segs)) { |
2701d086 AA |
437 | /* Reset MDS Threshold I/O counters */ |
438 | NFS_I(lo->plh_inode)->write_io = 0; | |
439 | NFS_I(lo->plh_inode)->read_io = 0; | |
38511722 | 440 | if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) |
70c3bd2b | 441 | pnfs_put_layout_hdr_locked(lo); |
38511722 FI |
442 | return 0; |
443 | } | |
4541d16c | 444 | list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) |
778b5502 BH |
445 | if (!recall_range || |
446 | should_free_lseg(&lseg->pls_range, recall_range)) { | |
4541d16c FI |
447 | dprintk("%s: freeing lseg %p iomode %d " |
448 | "offset %llu length %llu\n", __func__, | |
449 | lseg, lseg->pls_range.iomode, lseg->pls_range.offset, | |
450 | lseg->pls_range.length); | |
451 | invalid++; | |
452 | removed += mark_lseg_invalid(lseg, tmp_list); | |
453 | } | |
454 | dprintk("%s:Return %i\n", __func__, invalid - removed); | |
455 | return invalid - removed; | |
974cec8c AA |
456 | } |
457 | ||
f49f9baa | 458 | /* note free_me must contain lsegs from a single layout_hdr */ |
43f1b3da | 459 | void |
4541d16c | 460 | pnfs_free_lseg_list(struct list_head *free_me) |
974cec8c | 461 | { |
4541d16c | 462 | struct pnfs_layout_segment *lseg, *tmp; |
f49f9baa FI |
463 | struct pnfs_layout_hdr *lo; |
464 | ||
465 | if (list_empty(free_me)) | |
466 | return; | |
467 | ||
468 | lo = list_first_entry(free_me, struct pnfs_layout_segment, | |
469 | pls_list)->pls_layout; | |
974cec8c | 470 | |
f49f9baa FI |
471 | if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) { |
472 | struct nfs_client *clp; | |
473 | ||
474 | clp = NFS_SERVER(lo->plh_inode)->nfs_client; | |
475 | spin_lock(&clp->cl_lock); | |
476 | list_del_init(&lo->plh_layouts); | |
477 | spin_unlock(&clp->cl_lock); | |
478 | } | |
4541d16c | 479 | list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { |
566052c5 | 480 | list_del(&lseg->pls_list); |
4541d16c | 481 | free_lseg(lseg); |
974cec8c AA |
482 | } |
483 | } | |
484 | ||
e5e94017 BH |
485 | void |
486 | pnfs_destroy_layout(struct nfs_inode *nfsi) | |
487 | { | |
488 | struct pnfs_layout_hdr *lo; | |
974cec8c | 489 | LIST_HEAD(tmp_list); |
e5e94017 BH |
490 | |
491 | spin_lock(&nfsi->vfs_inode.i_lock); | |
492 | lo = nfsi->layout; | |
493 | if (lo) { | |
38511722 | 494 | lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */ |
49a85061 | 495 | pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL); |
e5e94017 BH |
496 | } |
497 | spin_unlock(&nfsi->vfs_inode.i_lock); | |
974cec8c AA |
498 | pnfs_free_lseg_list(&tmp_list); |
499 | } | |
041245c8 | 500 | EXPORT_SYMBOL_GPL(pnfs_destroy_layout); |
974cec8c AA |
501 | |
502 | /* | |
503 | * Called by the state manger to remove all layouts established under an | |
504 | * expired lease. | |
505 | */ | |
506 | void | |
507 | pnfs_destroy_all_layouts(struct nfs_client *clp) | |
508 | { | |
6382a441 | 509 | struct nfs_server *server; |
974cec8c AA |
510 | struct pnfs_layout_hdr *lo; |
511 | LIST_HEAD(tmp_list); | |
512 | ||
c47abcf8 AA |
513 | nfs4_deviceid_mark_client_invalid(clp); |
514 | nfs4_deviceid_purge_client(clp); | |
515 | ||
974cec8c | 516 | spin_lock(&clp->cl_lock); |
6382a441 WAA |
517 | rcu_read_lock(); |
518 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { | |
519 | if (!list_empty(&server->layouts)) | |
520 | list_splice_init(&server->layouts, &tmp_list); | |
521 | } | |
522 | rcu_read_unlock(); | |
974cec8c AA |
523 | spin_unlock(&clp->cl_lock); |
524 | ||
525 | while (!list_empty(&tmp_list)) { | |
526 | lo = list_entry(tmp_list.next, struct pnfs_layout_hdr, | |
b7edfaa1 | 527 | plh_layouts); |
974cec8c | 528 | dprintk("%s freeing layout for inode %lu\n", __func__, |
b7edfaa1 | 529 | lo->plh_inode->i_ino); |
2887fe45 | 530 | list_del_init(&lo->plh_layouts); |
b7edfaa1 | 531 | pnfs_destroy_layout(NFS_I(lo->plh_inode)); |
974cec8c | 532 | } |
e5e94017 BH |
533 | } |
534 | ||
fd6002e9 | 535 | /* update lo->plh_stateid with new if is more recent */ |
43f1b3da FI |
536 | void |
537 | pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, | |
538 | bool update_barrier) | |
b1f69b75 | 539 | { |
fd6002e9 | 540 | u32 oldseq, newseq; |
b1f69b75 | 541 | |
2d2f24ad TM |
542 | oldseq = be32_to_cpu(lo->plh_stateid.seqid); |
543 | newseq = be32_to_cpu(new->seqid); | |
43f1b3da | 544 | if ((int)(newseq - oldseq) > 0) { |
f597c537 | 545 | nfs4_stateid_copy(&lo->plh_stateid, new); |
43f1b3da | 546 | if (update_barrier) { |
2d2f24ad | 547 | u32 new_barrier = be32_to_cpu(new->seqid); |
43f1b3da FI |
548 | |
549 | if ((int)(new_barrier - lo->plh_barrier)) | |
550 | lo->plh_barrier = new_barrier; | |
551 | } else { | |
552 | /* Because of wraparound, we want to keep the barrier | |
553 | * "close" to the current seqids. It needs to be | |
554 | * within 2**31 to count as "behind", so if it | |
555 | * gets too near that limit, give us a litle leeway | |
556 | * and bring it to within 2**30. | |
557 | * NOTE - and yes, this is all unsigned arithmetic. | |
558 | */ | |
559 | if (unlikely((newseq - lo->plh_barrier) > (3 << 29))) | |
560 | lo->plh_barrier = newseq - (1 << 30); | |
561 | } | |
562 | } | |
b1f69b75 AA |
563 | } |
564 | ||
cf7d63f1 FI |
565 | /* lget is set to 1 if called from inside send_layoutget call chain */ |
566 | static bool | |
43f1b3da FI |
567 | pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid, |
568 | int lget) | |
569 | { | |
570 | if ((stateid) && | |
2d2f24ad | 571 | (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0) |
43f1b3da | 572 | return true; |
f7e8917a | 573 | return lo->plh_block_lgets || |
38511722 | 574 | test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) || |
f7e8917a | 575 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) || |
43f1b3da | 576 | (list_empty(&lo->plh_segs) && |
cf7d63f1 FI |
577 | (atomic_read(&lo->plh_outstanding) > lget)); |
578 | } | |
579 | ||
fd6002e9 FI |
580 | int |
581 | pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo, | |
582 | struct nfs4_state *open_state) | |
b1f69b75 | 583 | { |
fd6002e9 | 584 | int status = 0; |
974cec8c | 585 | |
b1f69b75 | 586 | dprintk("--> %s\n", __func__); |
fd6002e9 | 587 | spin_lock(&lo->plh_inode->i_lock); |
43f1b3da | 588 | if (pnfs_layoutgets_blocked(lo, NULL, 1)) { |
cf7d63f1 FI |
589 | status = -EAGAIN; |
590 | } else if (list_empty(&lo->plh_segs)) { | |
fd6002e9 FI |
591 | int seq; |
592 | ||
593 | do { | |
594 | seq = read_seqbegin(&open_state->seqlock); | |
f597c537 | 595 | nfs4_stateid_copy(dst, &open_state->stateid); |
fd6002e9 FI |
596 | } while (read_seqretry(&open_state->seqlock, seq)); |
597 | } else | |
f597c537 | 598 | nfs4_stateid_copy(dst, &lo->plh_stateid); |
fd6002e9 | 599 | spin_unlock(&lo->plh_inode->i_lock); |
b1f69b75 | 600 | dprintk("<-- %s\n", __func__); |
fd6002e9 | 601 | return status; |
b1f69b75 AA |
602 | } |
603 | ||
604 | /* | |
605 | * Get layout from server. | |
606 | * for now, assume that whole file layouts are requested. | |
607 | * arg->offset: 0 | |
608 | * arg->length: all ones | |
609 | */ | |
e5e94017 BH |
610 | static struct pnfs_layout_segment * |
611 | send_layoutget(struct pnfs_layout_hdr *lo, | |
612 | struct nfs_open_context *ctx, | |
fb3296eb | 613 | struct pnfs_layout_range *range, |
a75b9df9 | 614 | gfp_t gfp_flags) |
e5e94017 | 615 | { |
b7edfaa1 | 616 | struct inode *ino = lo->plh_inode; |
b1f69b75 AA |
617 | struct nfs_server *server = NFS_SERVER(ino); |
618 | struct nfs4_layoutget *lgp; | |
a0b0a6e3 | 619 | struct pnfs_layout_segment *lseg; |
b1f69b75 AA |
620 | |
621 | dprintk("--> %s\n", __func__); | |
e5e94017 | 622 | |
b1f69b75 | 623 | BUG_ON(ctx == NULL); |
a75b9df9 | 624 | lgp = kzalloc(sizeof(*lgp), gfp_flags); |
cf7d63f1 | 625 | if (lgp == NULL) |
b1f69b75 | 626 | return NULL; |
35124a09 | 627 | |
fb3296eb BH |
628 | lgp->args.minlength = PAGE_CACHE_SIZE; |
629 | if (lgp->args.minlength > range->length) | |
630 | lgp->args.minlength = range->length; | |
b1f69b75 | 631 | lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; |
fb3296eb | 632 | lgp->args.range = *range; |
b1f69b75 AA |
633 | lgp->args.type = server->pnfs_curr_ld->id; |
634 | lgp->args.inode = ino; | |
635 | lgp->args.ctx = get_nfs_open_context(ctx); | |
a75b9df9 | 636 | lgp->gfp_flags = gfp_flags; |
b1f69b75 AA |
637 | |
638 | /* Synchronously retrieve layout information from server and | |
639 | * store in lseg. | |
640 | */ | |
a0b0a6e3 TM |
641 | lseg = nfs4_proc_layoutget(lgp, gfp_flags); |
642 | if (IS_ERR(lseg)) { | |
643 | switch (PTR_ERR(lseg)) { | |
644 | case -ENOMEM: | |
645 | case -ERESTARTSYS: | |
646 | break; | |
647 | default: | |
648 | /* remember that LAYOUTGET failed and suspend trying */ | |
b9e028fd | 649 | pnfs_layout_io_set_failed(lo, range->iomode); |
a0b0a6e3 TM |
650 | } |
651 | return NULL; | |
974cec8c | 652 | } |
35124a09 | 653 | |
974cec8c AA |
654 | return lseg; |
655 | } | |
656 | ||
293b3b06 AA |
657 | /* |
658 | * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr | |
659 | * when the layout segment list is empty. | |
660 | * | |
661 | * Note that a pnfs_layout_hdr can exist with an empty layout segment | |
662 | * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the | |
663 | * deviceid is marked invalid. | |
664 | */ | |
cbe82603 BH |
665 | int |
666 | _pnfs_return_layout(struct inode *ino) | |
667 | { | |
668 | struct pnfs_layout_hdr *lo = NULL; | |
669 | struct nfs_inode *nfsi = NFS_I(ino); | |
670 | LIST_HEAD(tmp_list); | |
671 | struct nfs4_layoutreturn *lrp; | |
672 | nfs4_stateid stateid; | |
293b3b06 | 673 | int status = 0, empty; |
cbe82603 | 674 | |
366d5052 | 675 | dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino); |
cbe82603 BH |
676 | |
677 | spin_lock(&ino->i_lock); | |
678 | lo = nfsi->layout; | |
366d5052 | 679 | if (!lo || pnfs_test_layout_returned(lo)) { |
cbe82603 | 680 | spin_unlock(&ino->i_lock); |
293b3b06 AA |
681 | dprintk("NFS: %s no layout to return\n", __func__); |
682 | goto out; | |
cbe82603 BH |
683 | } |
684 | stateid = nfsi->layout->plh_stateid; | |
685 | /* Reference matched in nfs4_layoutreturn_release */ | |
70c3bd2b | 686 | pnfs_get_layout_hdr(lo); |
293b3b06 | 687 | empty = list_empty(&lo->plh_segs); |
49a85061 | 688 | pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL); |
293b3b06 AA |
689 | /* Don't send a LAYOUTRETURN if list was initially empty */ |
690 | if (empty) { | |
691 | spin_unlock(&ino->i_lock); | |
70c3bd2b | 692 | pnfs_put_layout_hdr(lo); |
293b3b06 AA |
693 | dprintk("NFS: %s no layout segments to return\n", __func__); |
694 | goto out; | |
695 | } | |
ea0ded74 | 696 | lo->plh_block_lgets++; |
366d5052 | 697 | pnfs_mark_layout_returned(lo); |
cbe82603 BH |
698 | spin_unlock(&ino->i_lock); |
699 | pnfs_free_lseg_list(&tmp_list); | |
700 | ||
701 | WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)); | |
702 | ||
703 | lrp = kzalloc(sizeof(*lrp), GFP_KERNEL); | |
704 | if (unlikely(lrp == NULL)) { | |
705 | status = -ENOMEM; | |
b9e028fd TM |
706 | pnfs_layout_io_set_failed(lo, IOMODE_RW); |
707 | pnfs_layout_io_set_failed(lo, IOMODE_READ); | |
366d5052 | 708 | pnfs_clear_layout_returned(lo); |
70c3bd2b | 709 | pnfs_put_layout_hdr(lo); |
cbe82603 BH |
710 | goto out; |
711 | } | |
712 | ||
713 | lrp->args.stateid = stateid; | |
714 | lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id; | |
715 | lrp->args.inode = ino; | |
a56aaa02 | 716 | lrp->args.layout = lo; |
cbe82603 BH |
717 | lrp->clp = NFS_SERVER(ino)->nfs_client; |
718 | ||
719 | status = nfs4_proc_layoutreturn(lrp); | |
720 | out: | |
721 | dprintk("<-- %s status: %d\n", __func__, status); | |
722 | return status; | |
723 | } | |
0a57cdac | 724 | EXPORT_SYMBOL_GPL(_pnfs_return_layout); |
cbe82603 | 725 | |
f7e8917a FI |
726 | bool pnfs_roc(struct inode *ino) |
727 | { | |
728 | struct pnfs_layout_hdr *lo; | |
729 | struct pnfs_layout_segment *lseg, *tmp; | |
730 | LIST_HEAD(tmp_list); | |
731 | bool found = false; | |
732 | ||
733 | spin_lock(&ino->i_lock); | |
734 | lo = NFS_I(ino)->layout; | |
735 | if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) || | |
736 | test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) | |
737 | goto out_nolayout; | |
738 | list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list) | |
739 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { | |
740 | mark_lseg_invalid(lseg, &tmp_list); | |
741 | found = true; | |
742 | } | |
743 | if (!found) | |
744 | goto out_nolayout; | |
745 | lo->plh_block_lgets++; | |
70c3bd2b | 746 | pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */ |
f7e8917a FI |
747 | spin_unlock(&ino->i_lock); |
748 | pnfs_free_lseg_list(&tmp_list); | |
749 | return true; | |
750 | ||
751 | out_nolayout: | |
752 | spin_unlock(&ino->i_lock); | |
753 | return false; | |
754 | } | |
755 | ||
756 | void pnfs_roc_release(struct inode *ino) | |
757 | { | |
758 | struct pnfs_layout_hdr *lo; | |
759 | ||
760 | spin_lock(&ino->i_lock); | |
761 | lo = NFS_I(ino)->layout; | |
762 | lo->plh_block_lgets--; | |
70c3bd2b | 763 | pnfs_put_layout_hdr_locked(lo); |
f7e8917a FI |
764 | spin_unlock(&ino->i_lock); |
765 | } | |
766 | ||
767 | void pnfs_roc_set_barrier(struct inode *ino, u32 barrier) | |
768 | { | |
769 | struct pnfs_layout_hdr *lo; | |
770 | ||
771 | spin_lock(&ino->i_lock); | |
772 | lo = NFS_I(ino)->layout; | |
773 | if ((int)(barrier - lo->plh_barrier) > 0) | |
774 | lo->plh_barrier = barrier; | |
775 | spin_unlock(&ino->i_lock); | |
776 | } | |
777 | ||
778 | bool pnfs_roc_drain(struct inode *ino, u32 *barrier) | |
779 | { | |
780 | struct nfs_inode *nfsi = NFS_I(ino); | |
781 | struct pnfs_layout_segment *lseg; | |
782 | bool found = false; | |
783 | ||
784 | spin_lock(&ino->i_lock); | |
785 | list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list) | |
786 | if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) { | |
787 | found = true; | |
788 | break; | |
789 | } | |
790 | if (!found) { | |
791 | struct pnfs_layout_hdr *lo = nfsi->layout; | |
2d2f24ad | 792 | u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid); |
f7e8917a FI |
793 | |
794 | /* Since close does not return a layout stateid for use as | |
795 | * a barrier, we choose the worst-case barrier. | |
796 | */ | |
797 | *barrier = current_seqid + atomic_read(&lo->plh_outstanding); | |
798 | } | |
799 | spin_unlock(&ino->i_lock); | |
800 | return found; | |
801 | } | |
802 | ||
b1f69b75 AA |
803 | /* |
804 | * Compare two layout segments for sorting into layout cache. | |
805 | * We want to preferentially return RW over RO layouts, so ensure those | |
806 | * are seen first. | |
807 | */ | |
808 | static s64 | |
fb3296eb BH |
809 | cmp_layout(struct pnfs_layout_range *l1, |
810 | struct pnfs_layout_range *l2) | |
b1f69b75 | 811 | { |
fb3296eb BH |
812 | s64 d; |
813 | ||
814 | /* high offset > low offset */ | |
815 | d = l1->offset - l2->offset; | |
816 | if (d) | |
817 | return d; | |
818 | ||
819 | /* short length > long length */ | |
820 | d = l2->length - l1->length; | |
821 | if (d) | |
822 | return d; | |
823 | ||
b1f69b75 | 824 | /* read > read/write */ |
fb3296eb | 825 | return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); |
b1f69b75 AA |
826 | } |
827 | ||
974cec8c AA |
828 | static void |
829 | pnfs_insert_layout(struct pnfs_layout_hdr *lo, | |
830 | struct pnfs_layout_segment *lseg) | |
831 | { | |
b1f69b75 | 832 | struct pnfs_layout_segment *lp; |
b1f69b75 | 833 | |
974cec8c AA |
834 | dprintk("%s:Begin\n", __func__); |
835 | ||
b7edfaa1 | 836 | assert_spin_locked(&lo->plh_inode->i_lock); |
b7edfaa1 | 837 | list_for_each_entry(lp, &lo->plh_segs, pls_list) { |
fb3296eb | 838 | if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0) |
b1f69b75 | 839 | continue; |
566052c5 | 840 | list_add_tail(&lseg->pls_list, &lp->pls_list); |
b1f69b75 AA |
841 | dprintk("%s: inserted lseg %p " |
842 | "iomode %d offset %llu length %llu before " | |
843 | "lp %p iomode %d offset %llu length %llu\n", | |
566052c5 FI |
844 | __func__, lseg, lseg->pls_range.iomode, |
845 | lseg->pls_range.offset, lseg->pls_range.length, | |
846 | lp, lp->pls_range.iomode, lp->pls_range.offset, | |
847 | lp->pls_range.length); | |
fb3296eb | 848 | goto out; |
974cec8c | 849 | } |
fb3296eb BH |
850 | list_add_tail(&lseg->pls_list, &lo->plh_segs); |
851 | dprintk("%s: inserted lseg %p " | |
852 | "iomode %d offset %llu length %llu at tail\n", | |
853 | __func__, lseg, lseg->pls_range.iomode, | |
854 | lseg->pls_range.offset, lseg->pls_range.length); | |
855 | out: | |
70c3bd2b | 856 | pnfs_get_layout_hdr(lo); |
974cec8c AA |
857 | |
858 | dprintk("%s:Return\n", __func__); | |
e5e94017 BH |
859 | } |
860 | ||
861 | static struct pnfs_layout_hdr * | |
9fa40758 PT |
862 | alloc_init_layout_hdr(struct inode *ino, |
863 | struct nfs_open_context *ctx, | |
864 | gfp_t gfp_flags) | |
e5e94017 BH |
865 | { |
866 | struct pnfs_layout_hdr *lo; | |
867 | ||
636fb9c8 | 868 | lo = pnfs_alloc_layout_hdr(ino, gfp_flags); |
e5e94017 BH |
869 | if (!lo) |
870 | return NULL; | |
cc6e5340 | 871 | atomic_set(&lo->plh_refcount, 1); |
b7edfaa1 FI |
872 | INIT_LIST_HEAD(&lo->plh_layouts); |
873 | INIT_LIST_HEAD(&lo->plh_segs); | |
43f1b3da | 874 | INIT_LIST_HEAD(&lo->plh_bulk_recall); |
b7edfaa1 | 875 | lo->plh_inode = ino; |
9fa40758 | 876 | lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred); |
e5e94017 BH |
877 | return lo; |
878 | } | |
879 | ||
880 | static struct pnfs_layout_hdr * | |
9fa40758 PT |
881 | pnfs_find_alloc_layout(struct inode *ino, |
882 | struct nfs_open_context *ctx, | |
883 | gfp_t gfp_flags) | |
e5e94017 BH |
884 | { |
885 | struct nfs_inode *nfsi = NFS_I(ino); | |
886 | struct pnfs_layout_hdr *new = NULL; | |
887 | ||
888 | dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); | |
889 | ||
890 | assert_spin_locked(&ino->i_lock); | |
4541d16c FI |
891 | if (nfsi->layout) { |
892 | if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags)) | |
893 | return NULL; | |
894 | else | |
895 | return nfsi->layout; | |
896 | } | |
e5e94017 | 897 | spin_unlock(&ino->i_lock); |
9fa40758 | 898 | new = alloc_init_layout_hdr(ino, ctx, gfp_flags); |
e5e94017 BH |
899 | spin_lock(&ino->i_lock); |
900 | ||
901 | if (likely(nfsi->layout == NULL)) /* Won the race? */ | |
902 | nfsi->layout = new; | |
903 | else | |
636fb9c8 | 904 | pnfs_free_layout_hdr(new); |
e5e94017 BH |
905 | return nfsi->layout; |
906 | } | |
907 | ||
b1f69b75 AA |
908 | /* |
909 | * iomode matching rules: | |
910 | * iomode lseg match | |
911 | * ----- ----- ----- | |
912 | * ANY READ true | |
913 | * ANY RW true | |
914 | * RW READ false | |
915 | * RW RW true | |
916 | * READ READ true | |
917 | * READ RW true | |
918 | */ | |
919 | static int | |
fb3296eb BH |
920 | is_matching_lseg(struct pnfs_layout_range *ls_range, |
921 | struct pnfs_layout_range *range) | |
b1f69b75 | 922 | { |
fb3296eb BH |
923 | struct pnfs_layout_range range1; |
924 | ||
925 | if ((range->iomode == IOMODE_RW && | |
926 | ls_range->iomode != IOMODE_RW) || | |
927 | !lo_seg_intersecting(ls_range, range)) | |
928 | return 0; | |
929 | ||
930 | /* range1 covers only the first byte in the range */ | |
931 | range1 = *range; | |
932 | range1.length = 1; | |
933 | return lo_seg_contained(ls_range, &range1); | |
b1f69b75 AA |
934 | } |
935 | ||
936 | /* | |
937 | * lookup range in layout | |
938 | */ | |
e5e94017 | 939 | static struct pnfs_layout_segment * |
fb3296eb BH |
940 | pnfs_find_lseg(struct pnfs_layout_hdr *lo, |
941 | struct pnfs_layout_range *range) | |
e5e94017 | 942 | { |
b1f69b75 AA |
943 | struct pnfs_layout_segment *lseg, *ret = NULL; |
944 | ||
945 | dprintk("%s:Begin\n", __func__); | |
946 | ||
b7edfaa1 FI |
947 | assert_spin_locked(&lo->plh_inode->i_lock); |
948 | list_for_each_entry(lseg, &lo->plh_segs, pls_list) { | |
4541d16c | 949 | if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && |
fb3296eb | 950 | is_matching_lseg(&lseg->pls_range, range)) { |
9369a431 | 951 | ret = pnfs_get_lseg(lseg); |
b1f69b75 AA |
952 | break; |
953 | } | |
d771e3a4 | 954 | if (lseg->pls_range.offset > range->offset) |
b1f69b75 AA |
955 | break; |
956 | } | |
957 | ||
958 | dprintk("%s:Return lseg %p ref %d\n", | |
4541d16c | 959 | __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0); |
b1f69b75 | 960 | return ret; |
e5e94017 BH |
961 | } |
962 | ||
d23d61c8 AA |
963 | /* |
964 | * Use mdsthreshold hints set at each OPEN to determine if I/O should go | |
965 | * to the MDS or over pNFS | |
966 | * | |
967 | * The nfs_inode read_io and write_io fields are cumulative counters reset | |
968 | * when there are no layout segments. Note that in pnfs_update_layout iomode | |
969 | * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a | |
970 | * WRITE request. | |
971 | * | |
972 | * A return of true means use MDS I/O. | |
973 | * | |
974 | * From rfc 5661: | |
975 | * If a file's size is smaller than the file size threshold, data accesses | |
976 | * SHOULD be sent to the metadata server. If an I/O request has a length that | |
977 | * is below the I/O size threshold, the I/O SHOULD be sent to the metadata | |
978 | * server. If both file size and I/O size are provided, the client SHOULD | |
979 | * reach or exceed both thresholds before sending its read or write | |
980 | * requests to the data server. | |
981 | */ | |
982 | static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx, | |
983 | struct inode *ino, int iomode) | |
984 | { | |
985 | struct nfs4_threshold *t = ctx->mdsthreshold; | |
986 | struct nfs_inode *nfsi = NFS_I(ino); | |
987 | loff_t fsize = i_size_read(ino); | |
988 | bool size = false, size_set = false, io = false, io_set = false, ret = false; | |
989 | ||
990 | if (t == NULL) | |
991 | return ret; | |
992 | ||
993 | dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n", | |
994 | __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz); | |
995 | ||
996 | switch (iomode) { | |
997 | case IOMODE_READ: | |
998 | if (t->bm & THRESHOLD_RD) { | |
999 | dprintk("%s fsize %llu\n", __func__, fsize); | |
1000 | size_set = true; | |
1001 | if (fsize < t->rd_sz) | |
1002 | size = true; | |
1003 | } | |
1004 | if (t->bm & THRESHOLD_RD_IO) { | |
1005 | dprintk("%s nfsi->read_io %llu\n", __func__, | |
1006 | nfsi->read_io); | |
1007 | io_set = true; | |
1008 | if (nfsi->read_io < t->rd_io_sz) | |
1009 | io = true; | |
1010 | } | |
1011 | break; | |
1012 | case IOMODE_RW: | |
1013 | if (t->bm & THRESHOLD_WR) { | |
1014 | dprintk("%s fsize %llu\n", __func__, fsize); | |
1015 | size_set = true; | |
1016 | if (fsize < t->wr_sz) | |
1017 | size = true; | |
1018 | } | |
1019 | if (t->bm & THRESHOLD_WR_IO) { | |
1020 | dprintk("%s nfsi->write_io %llu\n", __func__, | |
1021 | nfsi->write_io); | |
1022 | io_set = true; | |
1023 | if (nfsi->write_io < t->wr_io_sz) | |
1024 | io = true; | |
1025 | } | |
1026 | break; | |
1027 | } | |
1028 | if (size_set && io_set) { | |
1029 | if (size && io) | |
1030 | ret = true; | |
1031 | } else if (size || io) | |
1032 | ret = true; | |
1033 | ||
1034 | dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret); | |
1035 | return ret; | |
1036 | } | |
1037 | ||
e5e94017 BH |
1038 | /* |
1039 | * Layout segment is retreived from the server if not cached. | |
1040 | * The appropriate layout segment is referenced and returned to the caller. | |
1041 | */ | |
7c24d948 | 1042 | struct pnfs_layout_segment * |
e5e94017 BH |
1043 | pnfs_update_layout(struct inode *ino, |
1044 | struct nfs_open_context *ctx, | |
fb3296eb BH |
1045 | loff_t pos, |
1046 | u64 count, | |
a75b9df9 TM |
1047 | enum pnfs_iomode iomode, |
1048 | gfp_t gfp_flags) | |
e5e94017 | 1049 | { |
fb3296eb BH |
1050 | struct pnfs_layout_range arg = { |
1051 | .iomode = iomode, | |
1052 | .offset = pos, | |
1053 | .length = count, | |
1054 | }; | |
707ed5fd | 1055 | unsigned pg_offset; |
6382a441 WAA |
1056 | struct nfs_server *server = NFS_SERVER(ino); |
1057 | struct nfs_client *clp = server->nfs_client; | |
e5e94017 BH |
1058 | struct pnfs_layout_hdr *lo; |
1059 | struct pnfs_layout_segment *lseg = NULL; | |
f49f9baa | 1060 | bool first = false; |
e5e94017 BH |
1061 | |
1062 | if (!pnfs_enabled_sb(NFS_SERVER(ino))) | |
f86bbcf8 | 1063 | goto out; |
d23d61c8 AA |
1064 | |
1065 | if (pnfs_within_mdsthreshold(ctx, ino, iomode)) | |
f86bbcf8 | 1066 | goto out; |
d23d61c8 | 1067 | |
e5e94017 | 1068 | spin_lock(&ino->i_lock); |
9fa40758 | 1069 | lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags); |
f86bbcf8 | 1070 | if (lo == NULL) |
e5e94017 | 1071 | goto out_unlock; |
e5e94017 | 1072 | |
43f1b3da | 1073 | /* Do we even need to bother with this? */ |
a59c30ac | 1074 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
43f1b3da | 1075 | dprintk("%s matches recall, use MDS\n", __func__); |
e5e94017 BH |
1076 | goto out_unlock; |
1077 | } | |
1078 | ||
1079 | /* if LAYOUTGET already failed once we don't try again */ | |
b9e028fd | 1080 | if (pnfs_layout_io_test_failed(lo, iomode)) |
e5e94017 BH |
1081 | goto out_unlock; |
1082 | ||
568e8c49 | 1083 | /* Check to see if the layout for the given range already exists */ |
fb3296eb | 1084 | lseg = pnfs_find_lseg(lo, &arg); |
568e8c49 AA |
1085 | if (lseg) |
1086 | goto out_unlock; | |
1087 | ||
43f1b3da | 1088 | if (pnfs_layoutgets_blocked(lo, NULL, 0)) |
cf7d63f1 FI |
1089 | goto out_unlock; |
1090 | atomic_inc(&lo->plh_outstanding); | |
1091 | ||
70c3bd2b | 1092 | pnfs_get_layout_hdr(lo); |
f49f9baa FI |
1093 | if (list_empty(&lo->plh_segs)) |
1094 | first = true; | |
366d5052 AA |
1095 | |
1096 | /* Enable LAYOUTRETURNs */ | |
1097 | pnfs_clear_layout_returned(lo); | |
1098 | ||
f49f9baa FI |
1099 | spin_unlock(&ino->i_lock); |
1100 | if (first) { | |
2130ff66 FI |
1101 | /* The lo must be on the clp list if there is any |
1102 | * chance of a CB_LAYOUTRECALL(FILE) coming in. | |
1103 | */ | |
1104 | spin_lock(&clp->cl_lock); | |
1105 | BUG_ON(!list_empty(&lo->plh_layouts)); | |
6382a441 | 1106 | list_add_tail(&lo->plh_layouts, &server->layouts); |
2130ff66 FI |
1107 | spin_unlock(&clp->cl_lock); |
1108 | } | |
e5e94017 | 1109 | |
707ed5fd BH |
1110 | pg_offset = arg.offset & ~PAGE_CACHE_MASK; |
1111 | if (pg_offset) { | |
1112 | arg.offset -= pg_offset; | |
1113 | arg.length += pg_offset; | |
1114 | } | |
7c24d948 AA |
1115 | if (arg.length != NFS4_MAX_UINT64) |
1116 | arg.length = PAGE_CACHE_ALIGN(arg.length); | |
707ed5fd | 1117 | |
fb3296eb | 1118 | lseg = send_layoutget(lo, ctx, &arg, gfp_flags); |
f49f9baa FI |
1119 | if (!lseg && first) { |
1120 | spin_lock(&clp->cl_lock); | |
1121 | list_del_init(&lo->plh_layouts); | |
1122 | spin_unlock(&clp->cl_lock); | |
2130ff66 | 1123 | } |
cf7d63f1 | 1124 | atomic_dec(&lo->plh_outstanding); |
70c3bd2b | 1125 | pnfs_put_layout_hdr(lo); |
e5e94017 | 1126 | out: |
f86bbcf8 TM |
1127 | dprintk("%s: inode %s/%llu pNFS layout segment %s for " |
1128 | "(%s, offset: %llu, length: %llu)\n", | |
1129 | __func__, ino->i_sb->s_id, | |
1130 | (unsigned long long)NFS_FILEID(ino), | |
1131 | lseg == NULL ? "not found" : "found", | |
1132 | iomode==IOMODE_RW ? "read/write" : "read-only", | |
1133 | (unsigned long long)pos, | |
1134 | (unsigned long long)count); | |
e5e94017 BH |
1135 | return lseg; |
1136 | out_unlock: | |
1137 | spin_unlock(&ino->i_lock); | |
1138 | goto out; | |
1139 | } | |
7c24d948 | 1140 | EXPORT_SYMBOL_GPL(pnfs_update_layout); |
b1f69b75 | 1141 | |
a0b0a6e3 | 1142 | struct pnfs_layout_segment * |
b1f69b75 AA |
1143 | pnfs_layout_process(struct nfs4_layoutget *lgp) |
1144 | { | |
1145 | struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout; | |
1146 | struct nfs4_layoutget_res *res = &lgp->res; | |
1147 | struct pnfs_layout_segment *lseg; | |
b7edfaa1 | 1148 | struct inode *ino = lo->plh_inode; |
b1f69b75 AA |
1149 | int status = 0; |
1150 | ||
1151 | /* Inject layout blob into I/O device driver */ | |
a75b9df9 | 1152 | lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags); |
b1f69b75 AA |
1153 | if (!lseg || IS_ERR(lseg)) { |
1154 | if (!lseg) | |
1155 | status = -ENOMEM; | |
1156 | else | |
1157 | status = PTR_ERR(lseg); | |
1158 | dprintk("%s: Could not allocate layout: error %d\n", | |
1159 | __func__, status); | |
1160 | goto out; | |
1161 | } | |
1162 | ||
1163 | spin_lock(&ino->i_lock); | |
a59c30ac | 1164 | if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { |
43f1b3da FI |
1165 | dprintk("%s forget reply due to recall\n", __func__); |
1166 | goto out_forget_reply; | |
1167 | } | |
1168 | ||
1169 | if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) { | |
1170 | dprintk("%s forget reply due to state\n", __func__); | |
1171 | goto out_forget_reply; | |
1172 | } | |
b1f69b75 | 1173 | init_lseg(lo, lseg); |
566052c5 | 1174 | lseg->pls_range = res->range; |
9369a431 | 1175 | pnfs_get_lseg(lseg); |
b1f69b75 AA |
1176 | pnfs_insert_layout(lo, lseg); |
1177 | ||
f7e8917a FI |
1178 | if (res->return_on_close) { |
1179 | set_bit(NFS_LSEG_ROC, &lseg->pls_flags); | |
1180 | set_bit(NFS_LAYOUT_ROC, &lo->plh_flags); | |
1181 | } | |
1182 | ||
b1f69b75 | 1183 | /* Done processing layoutget. Set the layout stateid */ |
43f1b3da | 1184 | pnfs_set_layout_stateid(lo, &res->stateid, false); |
b1f69b75 | 1185 | spin_unlock(&ino->i_lock); |
a0b0a6e3 | 1186 | return lseg; |
b1f69b75 | 1187 | out: |
a0b0a6e3 | 1188 | return ERR_PTR(status); |
43f1b3da FI |
1189 | |
1190 | out_forget_reply: | |
1191 | spin_unlock(&ino->i_lock); | |
1192 | lseg->pls_layout = lo; | |
1193 | NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); | |
1194 | goto out; | |
b1f69b75 AA |
1195 | } |
1196 | ||
d8007d4d TM |
1197 | void |
1198 | pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) | |
1199 | { | |
1200 | BUG_ON(pgio->pg_lseg != NULL); | |
1201 | ||
1825a0d0 FI |
1202 | if (req->wb_offset != req->wb_pgbase) { |
1203 | nfs_pageio_reset_read_mds(pgio); | |
1204 | return; | |
1205 | } | |
d8007d4d TM |
1206 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
1207 | req->wb_context, | |
1208 | req_offset(req), | |
1209 | req->wb_bytes, | |
1210 | IOMODE_READ, | |
1211 | GFP_KERNEL); | |
e885de1a TM |
1212 | /* If no lseg, fall back to read through mds */ |
1213 | if (pgio->pg_lseg == NULL) | |
1f945357 | 1214 | nfs_pageio_reset_read_mds(pgio); |
e885de1a | 1215 | |
d8007d4d TM |
1216 | } |
1217 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read); | |
1218 | ||
1219 | void | |
1220 | pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) | |
1221 | { | |
1222 | BUG_ON(pgio->pg_lseg != NULL); | |
1223 | ||
1825a0d0 FI |
1224 | if (req->wb_offset != req->wb_pgbase) { |
1225 | nfs_pageio_reset_write_mds(pgio); | |
1226 | return; | |
1227 | } | |
d8007d4d TM |
1228 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
1229 | req->wb_context, | |
1230 | req_offset(req), | |
1231 | req->wb_bytes, | |
1232 | IOMODE_RW, | |
1233 | GFP_NOFS); | |
e885de1a TM |
1234 | /* If no lseg, fall back to write through mds */ |
1235 | if (pgio->pg_lseg == NULL) | |
1f945357 | 1236 | nfs_pageio_reset_write_mds(pgio); |
d8007d4d TM |
1237 | } |
1238 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write); | |
1239 | ||
1abb5088 | 1240 | void |
061ae2ed FI |
1241 | pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode, |
1242 | const struct nfs_pgio_completion_ops *compl_ops) | |
1751c363 TM |
1243 | { |
1244 | struct nfs_server *server = NFS_SERVER(inode); | |
1245 | struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; | |
1246 | ||
1247 | if (ld == NULL) | |
1abb5088 BS |
1248 | nfs_pageio_init_read(pgio, inode, compl_ops); |
1249 | else | |
1250 | nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops, server->rsize, 0); | |
1751c363 TM |
1251 | } |
1252 | ||
57208fa7 | 1253 | void |
061ae2ed FI |
1254 | pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode, |
1255 | int ioflags, | |
1256 | const struct nfs_pgio_completion_ops *compl_ops) | |
1751c363 TM |
1257 | { |
1258 | struct nfs_server *server = NFS_SERVER(inode); | |
1259 | struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; | |
1260 | ||
1261 | if (ld == NULL) | |
57208fa7 BS |
1262 | nfs_pageio_init_write(pgio, inode, ioflags, compl_ops); |
1263 | else | |
1264 | nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops, server->wsize, ioflags); | |
1751c363 TM |
1265 | } |
1266 | ||
18ad0a9f | 1267 | bool |
dfed206b BH |
1268 | pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
1269 | struct nfs_page *req) | |
94ad1c80 | 1270 | { |
d8007d4d TM |
1271 | if (pgio->pg_lseg == NULL) |
1272 | return nfs_generic_pg_test(pgio, prev, req); | |
94ad1c80 | 1273 | |
19982ba8 TM |
1274 | /* |
1275 | * Test if a nfs_page is fully contained in the pnfs_layout_range. | |
1276 | * Note that this test makes several assumptions: | |
1277 | * - that the previous nfs_page in the struct nfs_pageio_descriptor | |
1278 | * is known to lie within the range. | |
1279 | * - that the nfs_page being tested is known to be contiguous with the | |
1280 | * previous nfs_page. | |
1281 | * - Layout ranges are page aligned, so we only have to test the | |
1282 | * start offset of the request. | |
1283 | * | |
1284 | * Please also note that 'end_offset' is actually the offset of the | |
1285 | * first byte that lies outside the pnfs_layout_range. FIXME? | |
1286 | * | |
1287 | */ | |
1288 | return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset, | |
1289 | pgio->pg_lseg->pls_range.length); | |
94ad1c80 | 1290 | } |
89a58e32 | 1291 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); |
94ad1c80 | 1292 | |
e7dd79af | 1293 | int pnfs_write_done_resend_to_mds(struct inode *inode, |
061ae2ed FI |
1294 | struct list_head *head, |
1295 | const struct nfs_pgio_completion_ops *compl_ops) | |
e2fecb21 TM |
1296 | { |
1297 | struct nfs_pageio_descriptor pgio; | |
1298 | LIST_HEAD(failed); | |
1299 | ||
1300 | /* Resend all requests through the MDS */ | |
57208fa7 | 1301 | nfs_pageio_init_write(&pgio, inode, FLUSH_STABLE, compl_ops); |
e2fecb21 TM |
1302 | while (!list_empty(head)) { |
1303 | struct nfs_page *req = nfs_list_entry(head->next); | |
1304 | ||
1305 | nfs_list_remove_request(req); | |
1306 | if (!nfs_pageio_add_request(&pgio, req)) | |
1307 | nfs_list_add_request(req, &failed); | |
1308 | } | |
1309 | nfs_pageio_complete(&pgio); | |
1310 | ||
1311 | if (!list_empty(&failed)) { | |
1312 | /* For some reason our attempt to resend pages. Mark the | |
1313 | * overall send request as having failed, and let | |
1314 | * nfs_writeback_release_full deal with the error. | |
1315 | */ | |
1316 | list_move(&failed, head); | |
1317 | return -EIO; | |
1318 | } | |
1319 | return 0; | |
1320 | } | |
e7dd79af | 1321 | EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds); |
e2fecb21 | 1322 | |
1acbbb4e FI |
1323 | static void pnfs_ld_handle_write_error(struct nfs_write_data *data) |
1324 | { | |
cd841605 FI |
1325 | struct nfs_pgio_header *hdr = data->header; |
1326 | ||
1327 | dprintk("pnfs write error = %d\n", hdr->pnfs_error); | |
1328 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & | |
1acbbb4e | 1329 | PNFS_LAYOUTRET_ON_ERROR) { |
cd841605 FI |
1330 | clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags); |
1331 | pnfs_return_layout(hdr->inode); | |
1acbbb4e | 1332 | } |
6c75dc0d FI |
1333 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
1334 | data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode, | |
061ae2ed FI |
1335 | &hdr->pages, |
1336 | hdr->completion_ops); | |
1acbbb4e FI |
1337 | } |
1338 | ||
d20581aa BH |
1339 | /* |
1340 | * Called by non rpc-based layout drivers | |
1341 | */ | |
8ce160c5 | 1342 | void pnfs_ld_write_done(struct nfs_write_data *data) |
44b83799 | 1343 | { |
cd841605 FI |
1344 | struct nfs_pgio_header *hdr = data->header; |
1345 | ||
1346 | if (!hdr->pnfs_error) { | |
d20581aa | 1347 | pnfs_set_layoutcommit(data); |
cd841605 | 1348 | hdr->mds_ops->rpc_call_done(&data->task, data); |
1acbbb4e FI |
1349 | } else |
1350 | pnfs_ld_handle_write_error(data); | |
cd841605 | 1351 | hdr->mds_ops->rpc_release(data); |
44b83799 | 1352 | } |
d20581aa | 1353 | EXPORT_SYMBOL_GPL(pnfs_ld_write_done); |
44b83799 | 1354 | |
dce81290 TM |
1355 | static void |
1356 | pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, | |
1357 | struct nfs_write_data *data) | |
1358 | { | |
cd841605 FI |
1359 | struct nfs_pgio_header *hdr = data->header; |
1360 | ||
6c75dc0d FI |
1361 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
1362 | list_splice_tail_init(&hdr->pages, &desc->pg_list); | |
1363 | nfs_pageio_reset_write_mds(desc); | |
1364 | desc->pg_recoalesce = 1; | |
1365 | } | |
dce81290 TM |
1366 | nfs_writedata_release(data); |
1367 | } | |
1368 | ||
1369 | static enum pnfs_try_status | |
0382b744 | 1370 | pnfs_try_to_write_data(struct nfs_write_data *wdata, |
dce81290 TM |
1371 | const struct rpc_call_ops *call_ops, |
1372 | struct pnfs_layout_segment *lseg, | |
1373 | int how) | |
0382b744 | 1374 | { |
cd841605 FI |
1375 | struct nfs_pgio_header *hdr = wdata->header; |
1376 | struct inode *inode = hdr->inode; | |
0382b744 AA |
1377 | enum pnfs_try_status trypnfs; |
1378 | struct nfs_server *nfss = NFS_SERVER(inode); | |
1379 | ||
cd841605 | 1380 | hdr->mds_ops = call_ops; |
0382b744 AA |
1381 | |
1382 | dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, | |
1383 | inode->i_ino, wdata->args.count, wdata->args.offset, how); | |
0382b744 | 1384 | trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how); |
6c75dc0d | 1385 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
0382b744 | 1386 | nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); |
0382b744 AA |
1387 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
1388 | return trypnfs; | |
1389 | } | |
1390 | ||
dce81290 TM |
1391 | static void |
1392 | pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how) | |
1393 | { | |
1394 | struct nfs_write_data *data; | |
1395 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; | |
1396 | struct pnfs_layout_segment *lseg = desc->pg_lseg; | |
1397 | ||
1398 | desc->pg_lseg = NULL; | |
1399 | while (!list_empty(head)) { | |
1400 | enum pnfs_try_status trypnfs; | |
1401 | ||
6c75dc0d | 1402 | data = list_first_entry(head, struct nfs_write_data, list); |
dce81290 TM |
1403 | list_del_init(&data->list); |
1404 | ||
1405 | trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how); | |
1406 | if (trypnfs == PNFS_NOT_ATTEMPTED) | |
1407 | pnfs_write_through_mds(desc, data); | |
1408 | } | |
9369a431 | 1409 | pnfs_put_lseg(lseg); |
dce81290 TM |
1410 | } |
1411 | ||
6c75dc0d FI |
1412 | static void pnfs_writehdr_free(struct nfs_pgio_header *hdr) |
1413 | { | |
9369a431 | 1414 | pnfs_put_lseg(hdr->lseg); |
6c75dc0d FI |
1415 | nfs_writehdr_free(hdr); |
1416 | } | |
89d77c8f | 1417 | EXPORT_SYMBOL_GPL(pnfs_writehdr_free); |
6c75dc0d | 1418 | |
dce81290 TM |
1419 | int |
1420 | pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) | |
1421 | { | |
6c75dc0d FI |
1422 | struct nfs_write_header *whdr; |
1423 | struct nfs_pgio_header *hdr; | |
dce81290 TM |
1424 | int ret; |
1425 | ||
6c75dc0d FI |
1426 | whdr = nfs_writehdr_alloc(); |
1427 | if (!whdr) { | |
9b5415b5 | 1428 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
9369a431 | 1429 | pnfs_put_lseg(desc->pg_lseg); |
dce81290 | 1430 | desc->pg_lseg = NULL; |
6c75dc0d | 1431 | return -ENOMEM; |
dce81290 | 1432 | } |
6c75dc0d FI |
1433 | hdr = &whdr->header; |
1434 | nfs_pgheader_init(desc, hdr, pnfs_writehdr_free); | |
9369a431 | 1435 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
6c75dc0d FI |
1436 | atomic_inc(&hdr->refcnt); |
1437 | ret = nfs_generic_flush(desc, hdr); | |
1438 | if (ret != 0) { | |
9369a431 | 1439 | pnfs_put_lseg(desc->pg_lseg); |
6c75dc0d | 1440 | desc->pg_lseg = NULL; |
6c75dc0d FI |
1441 | } else |
1442 | pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags); | |
1443 | if (atomic_dec_and_test(&hdr->refcnt)) | |
061ae2ed | 1444 | hdr->completion_ops->completion(hdr); |
6c75dc0d | 1445 | return ret; |
dce81290 TM |
1446 | } |
1447 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages); | |
1448 | ||
e7dd79af | 1449 | int pnfs_read_done_resend_to_mds(struct inode *inode, |
061ae2ed FI |
1450 | struct list_head *head, |
1451 | const struct nfs_pgio_completion_ops *compl_ops) | |
62e4a769 TM |
1452 | { |
1453 | struct nfs_pageio_descriptor pgio; | |
1acbbb4e | 1454 | LIST_HEAD(failed); |
62e4a769 | 1455 | |
1acbbb4e | 1456 | /* Resend all requests through the MDS */ |
1abb5088 | 1457 | nfs_pageio_init_read(&pgio, inode, compl_ops); |
1acbbb4e FI |
1458 | while (!list_empty(head)) { |
1459 | struct nfs_page *req = nfs_list_entry(head->next); | |
62e4a769 TM |
1460 | |
1461 | nfs_list_remove_request(req); | |
1acbbb4e FI |
1462 | if (!nfs_pageio_add_request(&pgio, req)) |
1463 | nfs_list_add_request(req, &failed); | |
62e4a769 TM |
1464 | } |
1465 | nfs_pageio_complete(&pgio); | |
1acbbb4e FI |
1466 | |
1467 | if (!list_empty(&failed)) { | |
1468 | list_move(&failed, head); | |
1469 | return -EIO; | |
1470 | } | |
1471 | return 0; | |
1472 | } | |
e7dd79af | 1473 | EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds); |
1acbbb4e FI |
1474 | |
1475 | static void pnfs_ld_handle_read_error(struct nfs_read_data *data) | |
1476 | { | |
cd841605 FI |
1477 | struct nfs_pgio_header *hdr = data->header; |
1478 | ||
1479 | dprintk("pnfs read error = %d\n", hdr->pnfs_error); | |
1480 | if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & | |
1acbbb4e | 1481 | PNFS_LAYOUTRET_ON_ERROR) { |
cd841605 FI |
1482 | clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags); |
1483 | pnfs_return_layout(hdr->inode); | |
1acbbb4e | 1484 | } |
4db6e0b7 FI |
1485 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) |
1486 | data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode, | |
061ae2ed FI |
1487 | &hdr->pages, |
1488 | hdr->completion_ops); | |
62e4a769 TM |
1489 | } |
1490 | ||
d20581aa BH |
1491 | /* |
1492 | * Called by non rpc-based layout drivers | |
1493 | */ | |
9b7eecdc | 1494 | void pnfs_ld_read_done(struct nfs_read_data *data) |
d20581aa | 1495 | { |
cd841605 FI |
1496 | struct nfs_pgio_header *hdr = data->header; |
1497 | ||
1498 | if (likely(!hdr->pnfs_error)) { | |
d20581aa | 1499 | __nfs4_read_done_cb(data); |
cd841605 | 1500 | hdr->mds_ops->rpc_call_done(&data->task, data); |
62e4a769 TM |
1501 | } else |
1502 | pnfs_ld_handle_read_error(data); | |
cd841605 | 1503 | hdr->mds_ops->rpc_release(data); |
d20581aa BH |
1504 | } |
1505 | EXPORT_SYMBOL_GPL(pnfs_ld_read_done); | |
1506 | ||
493292dd TM |
1507 | static void |
1508 | pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, | |
1509 | struct nfs_read_data *data) | |
1510 | { | |
cd841605 FI |
1511 | struct nfs_pgio_header *hdr = data->header; |
1512 | ||
4db6e0b7 FI |
1513 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
1514 | list_splice_tail_init(&hdr->pages, &desc->pg_list); | |
1515 | nfs_pageio_reset_read_mds(desc); | |
1516 | desc->pg_recoalesce = 1; | |
1517 | } | |
493292dd TM |
1518 | nfs_readdata_release(data); |
1519 | } | |
1520 | ||
64419a9b AA |
1521 | /* |
1522 | * Call the appropriate parallel I/O subsystem read function. | |
1523 | */ | |
493292dd | 1524 | static enum pnfs_try_status |
64419a9b | 1525 | pnfs_try_to_read_data(struct nfs_read_data *rdata, |
493292dd TM |
1526 | const struct rpc_call_ops *call_ops, |
1527 | struct pnfs_layout_segment *lseg) | |
64419a9b | 1528 | { |
cd841605 FI |
1529 | struct nfs_pgio_header *hdr = rdata->header; |
1530 | struct inode *inode = hdr->inode; | |
64419a9b AA |
1531 | struct nfs_server *nfss = NFS_SERVER(inode); |
1532 | enum pnfs_try_status trypnfs; | |
1533 | ||
cd841605 | 1534 | hdr->mds_ops = call_ops; |
64419a9b AA |
1535 | |
1536 | dprintk("%s: Reading ino:%lu %u@%llu\n", | |
1537 | __func__, inode->i_ino, rdata->args.count, rdata->args.offset); | |
1538 | ||
1539 | trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata); | |
4db6e0b7 | 1540 | if (trypnfs != PNFS_NOT_ATTEMPTED) |
64419a9b | 1541 | nfs_inc_stats(inode, NFSIOS_PNFS_READ); |
64419a9b AA |
1542 | dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); |
1543 | return trypnfs; | |
1544 | } | |
863a3c6c | 1545 | |
493292dd TM |
1546 | static void |
1547 | pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head) | |
1548 | { | |
1549 | struct nfs_read_data *data; | |
1550 | const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; | |
1551 | struct pnfs_layout_segment *lseg = desc->pg_lseg; | |
1552 | ||
1553 | desc->pg_lseg = NULL; | |
1554 | while (!list_empty(head)) { | |
1555 | enum pnfs_try_status trypnfs; | |
1556 | ||
4db6e0b7 | 1557 | data = list_first_entry(head, struct nfs_read_data, list); |
493292dd TM |
1558 | list_del_init(&data->list); |
1559 | ||
1560 | trypnfs = pnfs_try_to_read_data(data, call_ops, lseg); | |
1561 | if (trypnfs == PNFS_NOT_ATTEMPTED) | |
1562 | pnfs_read_through_mds(desc, data); | |
1563 | } | |
9369a431 | 1564 | pnfs_put_lseg(lseg); |
493292dd TM |
1565 | } |
1566 | ||
4db6e0b7 FI |
1567 | static void pnfs_readhdr_free(struct nfs_pgio_header *hdr) |
1568 | { | |
9369a431 | 1569 | pnfs_put_lseg(hdr->lseg); |
4db6e0b7 FI |
1570 | nfs_readhdr_free(hdr); |
1571 | } | |
89d77c8f | 1572 | EXPORT_SYMBOL_GPL(pnfs_readhdr_free); |
4db6e0b7 | 1573 | |
493292dd TM |
1574 | int |
1575 | pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) | |
1576 | { | |
4db6e0b7 FI |
1577 | struct nfs_read_header *rhdr; |
1578 | struct nfs_pgio_header *hdr; | |
493292dd TM |
1579 | int ret; |
1580 | ||
4db6e0b7 FI |
1581 | rhdr = nfs_readhdr_alloc(); |
1582 | if (!rhdr) { | |
061ae2ed | 1583 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
4db6e0b7 | 1584 | ret = -ENOMEM; |
9369a431 | 1585 | pnfs_put_lseg(desc->pg_lseg); |
493292dd TM |
1586 | desc->pg_lseg = NULL; |
1587 | return ret; | |
1588 | } | |
4db6e0b7 FI |
1589 | hdr = &rhdr->header; |
1590 | nfs_pgheader_init(desc, hdr, pnfs_readhdr_free); | |
9369a431 | 1591 | hdr->lseg = pnfs_get_lseg(desc->pg_lseg); |
4db6e0b7 FI |
1592 | atomic_inc(&hdr->refcnt); |
1593 | ret = nfs_generic_pagein(desc, hdr); | |
1594 | if (ret != 0) { | |
9369a431 | 1595 | pnfs_put_lseg(desc->pg_lseg); |
4db6e0b7 | 1596 | desc->pg_lseg = NULL; |
4db6e0b7 FI |
1597 | } else |
1598 | pnfs_do_multiple_reads(desc, &hdr->rpc_list); | |
1599 | if (atomic_dec_and_test(&hdr->refcnt)) | |
061ae2ed | 1600 | hdr->completion_ops->completion(hdr); |
4db6e0b7 | 1601 | return ret; |
493292dd TM |
1602 | } |
1603 | EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages); | |
1604 | ||
863a3c6c | 1605 | /* |
a9bae566 | 1606 | * There can be multiple RW segments. |
863a3c6c | 1607 | */ |
a9bae566 | 1608 | static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp) |
863a3c6c | 1609 | { |
a9bae566 | 1610 | struct pnfs_layout_segment *lseg; |
863a3c6c | 1611 | |
a9bae566 PT |
1612 | list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) { |
1613 | if (lseg->pls_range.iomode == IOMODE_RW && | |
1614 | test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) | |
1615 | list_add(&lseg->pls_lc_list, listp); | |
1616 | } | |
863a3c6c AA |
1617 | } |
1618 | ||
1b0ae068 PT |
1619 | void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg) |
1620 | { | |
b9e028fd | 1621 | pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode); |
1b0ae068 PT |
1622 | } |
1623 | EXPORT_SYMBOL_GPL(pnfs_set_lo_fail); | |
1624 | ||
863a3c6c AA |
1625 | void |
1626 | pnfs_set_layoutcommit(struct nfs_write_data *wdata) | |
1627 | { | |
cd841605 FI |
1628 | struct nfs_pgio_header *hdr = wdata->header; |
1629 | struct inode *inode = hdr->inode; | |
1630 | struct nfs_inode *nfsi = NFS_I(inode); | |
4b8ee2b8 | 1631 | loff_t end_pos = wdata->mds_offset + wdata->res.count; |
79a48a1f | 1632 | bool mark_as_dirty = false; |
863a3c6c | 1633 | |
cd841605 | 1634 | spin_lock(&inode->i_lock); |
863a3c6c | 1635 | if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
79a48a1f | 1636 | mark_as_dirty = true; |
863a3c6c | 1637 | dprintk("%s: Set layoutcommit for inode %lu ", |
cd841605 | 1638 | __func__, inode->i_ino); |
863a3c6c | 1639 | } |
cd841605 | 1640 | if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) { |
a9bae566 | 1641 | /* references matched in nfs4_layoutcommit_release */ |
9369a431 | 1642 | pnfs_get_lseg(hdr->lseg); |
a9bae566 | 1643 | } |
acff5880 PT |
1644 | if (end_pos > nfsi->layout->plh_lwb) |
1645 | nfsi->layout->plh_lwb = end_pos; | |
cd841605 | 1646 | spin_unlock(&inode->i_lock); |
acff5880 | 1647 | dprintk("%s: lseg %p end_pos %llu\n", |
cd841605 | 1648 | __func__, hdr->lseg, nfsi->layout->plh_lwb); |
79a48a1f WAA |
1649 | |
1650 | /* if pnfs_layoutcommit_inode() runs between inode locks, the next one | |
1651 | * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ | |
1652 | if (mark_as_dirty) | |
cd841605 | 1653 | mark_inode_dirty_sync(inode); |
863a3c6c AA |
1654 | } |
1655 | EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); | |
1656 | ||
db29c089 AA |
1657 | void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data) |
1658 | { | |
1659 | struct nfs_server *nfss = NFS_SERVER(data->args.inode); | |
1660 | ||
1661 | if (nfss->pnfs_curr_ld->cleanup_layoutcommit) | |
1662 | nfss->pnfs_curr_ld->cleanup_layoutcommit(data); | |
1663 | } | |
1664 | ||
de4b15c7 AA |
1665 | /* |
1666 | * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and | |
1667 | * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough | |
1668 | * data to disk to allow the server to recover the data if it crashes. | |
1669 | * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag | |
1670 | * is off, and a COMMIT is sent to a data server, or | |
1671 | * if WRITEs to a data server return NFS_DATA_SYNC. | |
1672 | */ | |
863a3c6c | 1673 | int |
ef311537 | 1674 | pnfs_layoutcommit_inode(struct inode *inode, bool sync) |
863a3c6c AA |
1675 | { |
1676 | struct nfs4_layoutcommit_data *data; | |
1677 | struct nfs_inode *nfsi = NFS_I(inode); | |
863a3c6c AA |
1678 | loff_t end_pos; |
1679 | int status = 0; | |
1680 | ||
1681 | dprintk("--> %s inode %lu\n", __func__, inode->i_ino); | |
1682 | ||
de4b15c7 AA |
1683 | if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
1684 | return 0; | |
1685 | ||
863a3c6c AA |
1686 | /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ |
1687 | data = kzalloc(sizeof(*data), GFP_NOFS); | |
de4b15c7 | 1688 | if (!data) { |
de4b15c7 AA |
1689 | status = -ENOMEM; |
1690 | goto out; | |
1691 | } | |
863a3c6c | 1692 | |
92407e75 PT |
1693 | if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) |
1694 | goto out_free; | |
1695 | ||
1696 | if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) { | |
1697 | if (!sync) { | |
1698 | status = -EAGAIN; | |
1699 | goto out_free; | |
1700 | } | |
1701 | status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING, | |
1702 | nfs_wait_bit_killable, TASK_KILLABLE); | |
1703 | if (status) | |
1704 | goto out_free; | |
1705 | } | |
1706 | ||
a9bae566 | 1707 | INIT_LIST_HEAD(&data->lseg_list); |
de4b15c7 | 1708 | spin_lock(&inode->i_lock); |
863a3c6c | 1709 | if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { |
92407e75 | 1710 | clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags); |
863a3c6c | 1711 | spin_unlock(&inode->i_lock); |
92407e75 PT |
1712 | wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING); |
1713 | goto out_free; | |
863a3c6c | 1714 | } |
a9bae566 PT |
1715 | |
1716 | pnfs_list_write_lseg(inode, &data->lseg_list); | |
863a3c6c | 1717 | |
acff5880 | 1718 | end_pos = nfsi->layout->plh_lwb; |
acff5880 | 1719 | nfsi->layout->plh_lwb = 0; |
863a3c6c | 1720 | |
f597c537 | 1721 | nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid); |
863a3c6c AA |
1722 | spin_unlock(&inode->i_lock); |
1723 | ||
1724 | data->args.inode = inode; | |
9fa40758 | 1725 | data->cred = get_rpccred(nfsi->layout->plh_lc_cred); |
863a3c6c AA |
1726 | nfs_fattr_init(&data->fattr); |
1727 | data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; | |
1728 | data->res.fattr = &data->fattr; | |
1729 | data->args.lastbytewritten = end_pos - 1; | |
1730 | data->res.server = NFS_SERVER(inode); | |
1731 | ||
1732 | status = nfs4_proc_layoutcommit(data, sync); | |
1733 | out: | |
92407e75 PT |
1734 | if (status) |
1735 | mark_inode_dirty_sync(inode); | |
863a3c6c AA |
1736 | dprintk("<-- %s status %d\n", __func__, status); |
1737 | return status; | |
92407e75 PT |
1738 | out_free: |
1739 | kfree(data); | |
1740 | goto out; | |
863a3c6c | 1741 | } |
82be417a AA |
1742 | |
1743 | struct nfs4_threshold *pnfs_mdsthreshold_alloc(void) | |
1744 | { | |
1745 | struct nfs4_threshold *thp; | |
1746 | ||
1747 | thp = kzalloc(sizeof(*thp), GFP_NOFS); | |
1748 | if (!thp) { | |
1749 | dprintk("%s mdsthreshold allocation failed\n", __func__); | |
1750 | return NULL; | |
1751 | } | |
1752 | return thp; | |
1753 | } |