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