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