]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nfs/pnfs.c
nfs41: wait for LAYOUTRETURN before retrying LAYOUTGET
[mirror_ubuntu-bionic-kernel.git] / fs / nfs / pnfs.c
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>
31 #include <linux/nfs_page.h>
32 #include <linux/module.h>
33 #include "internal.h"
34 #include "pnfs.h"
35 #include "iostat.h"
36 #include "nfs4trace.h"
37
38 #define NFSDBG_FACILITY NFSDBG_PNFS
39 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
40
41 /* Locking:
42 *
43 * pnfs_spinlock:
44 * protects pnfs_modules_tbl.
45 */
46 static DEFINE_SPINLOCK(pnfs_spinlock);
47
48 /*
49 * pnfs_modules_tbl holds all pnfs modules
50 */
51 static LIST_HEAD(pnfs_modules_tbl);
52
53 static int
54 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
55 enum pnfs_iomode iomode, bool sync);
56
57 /* Return the registered pnfs layout driver module matching given id */
58 static struct pnfs_layoutdriver_type *
59 find_pnfs_driver_locked(u32 id)
60 {
61 struct pnfs_layoutdriver_type *local;
62
63 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
64 if (local->id == id)
65 goto out;
66 local = NULL;
67 out:
68 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
69 return local;
70 }
71
72 static struct pnfs_layoutdriver_type *
73 find_pnfs_driver(u32 id)
74 {
75 struct pnfs_layoutdriver_type *local;
76
77 spin_lock(&pnfs_spinlock);
78 local = find_pnfs_driver_locked(id);
79 if (local != NULL && !try_module_get(local->owner)) {
80 dprintk("%s: Could not grab reference on module\n", __func__);
81 local = NULL;
82 }
83 spin_unlock(&pnfs_spinlock);
84 return local;
85 }
86
87 void
88 unset_pnfs_layoutdriver(struct nfs_server *nfss)
89 {
90 if (nfss->pnfs_curr_ld) {
91 if (nfss->pnfs_curr_ld->clear_layoutdriver)
92 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
93 /* Decrement the MDS count. Purge the deviceid cache if zero */
94 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
95 nfs4_deviceid_purge_client(nfss->nfs_client);
96 module_put(nfss->pnfs_curr_ld->owner);
97 }
98 nfss->pnfs_curr_ld = NULL;
99 }
100
101 /*
102 * Try to set the server's pnfs module to the pnfs layout type specified by id.
103 * Currently only one pNFS layout driver per filesystem is supported.
104 *
105 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
106 */
107 void
108 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
109 u32 id)
110 {
111 struct pnfs_layoutdriver_type *ld_type = NULL;
112
113 if (id == 0)
114 goto out_no_driver;
115 if (!(server->nfs_client->cl_exchange_flags &
116 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
117 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
118 __func__, id, server->nfs_client->cl_exchange_flags);
119 goto out_no_driver;
120 }
121 ld_type = find_pnfs_driver(id);
122 if (!ld_type) {
123 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
124 ld_type = find_pnfs_driver(id);
125 if (!ld_type) {
126 dprintk("%s: No pNFS module found for %u.\n",
127 __func__, id);
128 goto out_no_driver;
129 }
130 }
131 server->pnfs_curr_ld = ld_type;
132 if (ld_type->set_layoutdriver
133 && ld_type->set_layoutdriver(server, mntfh)) {
134 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
135 "driver %u.\n", __func__, id);
136 module_put(ld_type->owner);
137 goto out_no_driver;
138 }
139 /* Bump the MDS count */
140 atomic_inc(&server->nfs_client->cl_mds_count);
141
142 dprintk("%s: pNFS module for %u set\n", __func__, id);
143 return;
144
145 out_no_driver:
146 dprintk("%s: Using NFSv4 I/O\n", __func__);
147 server->pnfs_curr_ld = NULL;
148 }
149
150 int
151 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
152 {
153 int status = -EINVAL;
154 struct pnfs_layoutdriver_type *tmp;
155
156 if (ld_type->id == 0) {
157 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
158 return status;
159 }
160 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
161 printk(KERN_ERR "NFS: %s Layout driver must provide "
162 "alloc_lseg and free_lseg.\n", __func__);
163 return status;
164 }
165
166 spin_lock(&pnfs_spinlock);
167 tmp = find_pnfs_driver_locked(ld_type->id);
168 if (!tmp) {
169 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
170 status = 0;
171 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
172 ld_type->name);
173 } else {
174 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
175 __func__, ld_type->id);
176 }
177 spin_unlock(&pnfs_spinlock);
178
179 return status;
180 }
181 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
182
183 void
184 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
185 {
186 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
187 spin_lock(&pnfs_spinlock);
188 list_del(&ld_type->pnfs_tblid);
189 spin_unlock(&pnfs_spinlock);
190 }
191 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
192
193 /*
194 * pNFS client layout cache
195 */
196
197 /* Need to hold i_lock if caller does not already hold reference */
198 void
199 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
200 {
201 atomic_inc(&lo->plh_refcount);
202 }
203
204 static struct pnfs_layout_hdr *
205 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
206 {
207 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
208 return ld->alloc_layout_hdr(ino, gfp_flags);
209 }
210
211 static void
212 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
213 {
214 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
215 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
216
217 if (!list_empty(&lo->plh_layouts)) {
218 struct nfs_client *clp = server->nfs_client;
219
220 spin_lock(&clp->cl_lock);
221 list_del_init(&lo->plh_layouts);
222 spin_unlock(&clp->cl_lock);
223 }
224 put_rpccred(lo->plh_lc_cred);
225 return ld->free_layout_hdr(lo);
226 }
227
228 static void
229 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
230 {
231 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
232 dprintk("%s: freeing layout cache %p\n", __func__, lo);
233 nfsi->layout = NULL;
234 /* Reset MDS Threshold I/O counters */
235 nfsi->write_io = 0;
236 nfsi->read_io = 0;
237 }
238
239 void
240 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
241 {
242 struct inode *inode = lo->plh_inode;
243
244 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
245 if (!list_empty(&lo->plh_segs))
246 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
247 pnfs_detach_layout_hdr(lo);
248 spin_unlock(&inode->i_lock);
249 pnfs_free_layout_hdr(lo);
250 }
251 }
252
253 static int
254 pnfs_iomode_to_fail_bit(u32 iomode)
255 {
256 return iomode == IOMODE_RW ?
257 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
258 }
259
260 static void
261 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
262 {
263 lo->plh_retry_timestamp = jiffies;
264 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
265 atomic_inc(&lo->plh_refcount);
266 }
267
268 static void
269 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
270 {
271 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
272 atomic_dec(&lo->plh_refcount);
273 }
274
275 static void
276 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
277 {
278 struct inode *inode = lo->plh_inode;
279 struct pnfs_layout_range range = {
280 .iomode = iomode,
281 .offset = 0,
282 .length = NFS4_MAX_UINT64,
283 };
284 LIST_HEAD(head);
285
286 spin_lock(&inode->i_lock);
287 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
288 pnfs_mark_matching_lsegs_invalid(lo, &head, &range);
289 spin_unlock(&inode->i_lock);
290 pnfs_free_lseg_list(&head);
291 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
292 iomode == IOMODE_RW ? "RW" : "READ");
293 }
294
295 static bool
296 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
297 {
298 unsigned long start, end;
299 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
300
301 if (test_bit(fail_bit, &lo->plh_flags) == 0)
302 return false;
303 end = jiffies;
304 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
305 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
306 /* It is time to retry the failed layoutgets */
307 pnfs_layout_clear_fail_bit(lo, fail_bit);
308 return false;
309 }
310 return true;
311 }
312
313 static void
314 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
315 {
316 INIT_LIST_HEAD(&lseg->pls_list);
317 INIT_LIST_HEAD(&lseg->pls_lc_list);
318 atomic_set(&lseg->pls_refcount, 1);
319 smp_mb();
320 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
321 lseg->pls_layout = lo;
322 }
323
324 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
325 {
326 struct inode *ino = lseg->pls_layout->plh_inode;
327
328 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
329 }
330
331 static void
332 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
333 struct pnfs_layout_segment *lseg)
334 {
335 struct inode *inode = lo->plh_inode;
336
337 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
338 list_del_init(&lseg->pls_list);
339 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
340 atomic_dec(&lo->plh_refcount);
341 if (list_empty(&lo->plh_segs))
342 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
343 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
344 }
345
346 /* Return true if layoutreturn is needed */
347 static bool
348 pnfs_layout_need_return(struct pnfs_layout_hdr *lo,
349 struct pnfs_layout_segment *lseg)
350 {
351 struct pnfs_layout_segment *s;
352
353 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
354 return false;
355
356 list_for_each_entry(s, &lo->plh_segs, pls_list)
357 if (s != lseg && test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
358 return false;
359
360 return true;
361 }
362
363 static void pnfs_layoutreturn_free_lseg(struct work_struct *work)
364 {
365 struct pnfs_layout_segment *lseg;
366 struct pnfs_layout_hdr *lo;
367 struct inode *inode;
368
369 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
370 WARN_ON(atomic_read(&lseg->pls_refcount));
371 lo = lseg->pls_layout;
372 inode = lo->plh_inode;
373
374 spin_lock(&inode->i_lock);
375 if (pnfs_layout_need_return(lo, lseg)) {
376 nfs4_stateid stateid;
377 enum pnfs_iomode iomode;
378
379 stateid = lo->plh_stateid;
380 iomode = lo->plh_return_iomode;
381 /* decreased in pnfs_send_layoutreturn() */
382 lo->plh_block_lgets++;
383 lo->plh_return_iomode = 0;
384 spin_unlock(&inode->i_lock);
385
386 pnfs_send_layoutreturn(lo, stateid, iomode, true);
387 spin_lock(&inode->i_lock);
388 } else
389 /* match pnfs_get_layout_hdr #2 in pnfs_put_lseg */
390 pnfs_put_layout_hdr(lo);
391 pnfs_layout_remove_lseg(lo, lseg);
392 spin_unlock(&inode->i_lock);
393 pnfs_free_lseg(lseg);
394 /* match pnfs_get_layout_hdr #1 in pnfs_put_lseg */
395 pnfs_put_layout_hdr(lo);
396 }
397
398 static void
399 pnfs_layoutreturn_free_lseg_async(struct pnfs_layout_segment *lseg)
400 {
401 INIT_WORK(&lseg->pls_work, pnfs_layoutreturn_free_lseg);
402 queue_work(nfsiod_workqueue, &lseg->pls_work);
403 }
404
405 void
406 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
407 {
408 struct pnfs_layout_hdr *lo;
409 struct inode *inode;
410
411 if (!lseg)
412 return;
413
414 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
415 atomic_read(&lseg->pls_refcount),
416 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
417 lo = lseg->pls_layout;
418 inode = lo->plh_inode;
419 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
420 pnfs_get_layout_hdr(lo);
421 if (pnfs_layout_need_return(lo, lseg)) {
422 spin_unlock(&inode->i_lock);
423 /* hdr reference dropped in nfs4_layoutreturn_release */
424 pnfs_get_layout_hdr(lo);
425 pnfs_layoutreturn_free_lseg_async(lseg);
426 } else {
427 pnfs_layout_remove_lseg(lo, lseg);
428 spin_unlock(&inode->i_lock);
429 pnfs_free_lseg(lseg);
430 pnfs_put_layout_hdr(lo);
431 }
432 }
433 }
434 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
435
436 static void pnfs_free_lseg_async_work(struct work_struct *work)
437 {
438 struct pnfs_layout_segment *lseg;
439 struct pnfs_layout_hdr *lo;
440
441 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
442 lo = lseg->pls_layout;
443
444 pnfs_free_lseg(lseg);
445 pnfs_put_layout_hdr(lo);
446 }
447
448 static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
449 {
450 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
451 schedule_work(&lseg->pls_work);
452 }
453
454 void
455 pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
456 {
457 if (!lseg)
458 return;
459
460 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
461
462 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
463 atomic_read(&lseg->pls_refcount),
464 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
465 if (atomic_dec_and_test(&lseg->pls_refcount)) {
466 struct pnfs_layout_hdr *lo = lseg->pls_layout;
467 pnfs_get_layout_hdr(lo);
468 pnfs_layout_remove_lseg(lo, lseg);
469 pnfs_free_lseg_async(lseg);
470 }
471 }
472 EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
473
474 static u64
475 end_offset(u64 start, u64 len)
476 {
477 u64 end;
478
479 end = start + len;
480 return end >= start ? end : NFS4_MAX_UINT64;
481 }
482
483 /*
484 * is l2 fully contained in l1?
485 * start1 end1
486 * [----------------------------------)
487 * start2 end2
488 * [----------------)
489 */
490 static bool
491 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
492 const struct pnfs_layout_range *l2)
493 {
494 u64 start1 = l1->offset;
495 u64 end1 = end_offset(start1, l1->length);
496 u64 start2 = l2->offset;
497 u64 end2 = end_offset(start2, l2->length);
498
499 return (start1 <= start2) && (end1 >= end2);
500 }
501
502 /*
503 * is l1 and l2 intersecting?
504 * start1 end1
505 * [----------------------------------)
506 * start2 end2
507 * [----------------)
508 */
509 static bool
510 pnfs_lseg_range_intersecting(const struct pnfs_layout_range *l1,
511 const struct pnfs_layout_range *l2)
512 {
513 u64 start1 = l1->offset;
514 u64 end1 = end_offset(start1, l1->length);
515 u64 start2 = l2->offset;
516 u64 end2 = end_offset(start2, l2->length);
517
518 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
519 (end2 == NFS4_MAX_UINT64 || end2 > start1);
520 }
521
522 static bool
523 should_free_lseg(const struct pnfs_layout_range *lseg_range,
524 const struct pnfs_layout_range *recall_range)
525 {
526 return (recall_range->iomode == IOMODE_ANY ||
527 lseg_range->iomode == recall_range->iomode) &&
528 pnfs_lseg_range_intersecting(lseg_range, recall_range);
529 }
530
531 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
532 struct list_head *tmp_list)
533 {
534 if (!atomic_dec_and_test(&lseg->pls_refcount))
535 return false;
536 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
537 list_add(&lseg->pls_list, tmp_list);
538 return true;
539 }
540
541 /* Returns 1 if lseg is removed from list, 0 otherwise */
542 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
543 struct list_head *tmp_list)
544 {
545 int rv = 0;
546
547 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
548 /* Remove the reference keeping the lseg in the
549 * list. It will now be removed when all
550 * outstanding io is finished.
551 */
552 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
553 atomic_read(&lseg->pls_refcount));
554 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
555 rv = 1;
556 }
557 return rv;
558 }
559
560 /* Returns count of number of matching invalid lsegs remaining in list
561 * after call.
562 */
563 int
564 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
565 struct list_head *tmp_list,
566 struct pnfs_layout_range *recall_range)
567 {
568 struct pnfs_layout_segment *lseg, *next;
569 int invalid = 0, removed = 0;
570
571 dprintk("%s:Begin lo %p\n", __func__, lo);
572
573 if (list_empty(&lo->plh_segs))
574 return 0;
575 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
576 if (!recall_range ||
577 should_free_lseg(&lseg->pls_range, recall_range)) {
578 dprintk("%s: freeing lseg %p iomode %d "
579 "offset %llu length %llu\n", __func__,
580 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
581 lseg->pls_range.length);
582 invalid++;
583 removed += mark_lseg_invalid(lseg, tmp_list);
584 }
585 dprintk("%s:Return %i\n", __func__, invalid - removed);
586 return invalid - removed;
587 }
588
589 /* note free_me must contain lsegs from a single layout_hdr */
590 void
591 pnfs_free_lseg_list(struct list_head *free_me)
592 {
593 struct pnfs_layout_segment *lseg, *tmp;
594
595 if (list_empty(free_me))
596 return;
597
598 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
599 list_del(&lseg->pls_list);
600 pnfs_free_lseg(lseg);
601 }
602 }
603
604 void
605 pnfs_destroy_layout(struct nfs_inode *nfsi)
606 {
607 struct pnfs_layout_hdr *lo;
608 LIST_HEAD(tmp_list);
609
610 spin_lock(&nfsi->vfs_inode.i_lock);
611 lo = nfsi->layout;
612 if (lo) {
613 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
614 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
615 pnfs_get_layout_hdr(lo);
616 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
617 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
618 pnfs_clear_retry_layoutget(lo);
619 spin_unlock(&nfsi->vfs_inode.i_lock);
620 pnfs_free_lseg_list(&tmp_list);
621 pnfs_put_layout_hdr(lo);
622 } else
623 spin_unlock(&nfsi->vfs_inode.i_lock);
624 }
625 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
626
627 static bool
628 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
629 struct list_head *layout_list)
630 {
631 struct pnfs_layout_hdr *lo;
632 bool ret = false;
633
634 spin_lock(&inode->i_lock);
635 lo = NFS_I(inode)->layout;
636 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
637 pnfs_get_layout_hdr(lo);
638 list_add(&lo->plh_bulk_destroy, layout_list);
639 ret = true;
640 }
641 spin_unlock(&inode->i_lock);
642 return ret;
643 }
644
645 /* Caller must hold rcu_read_lock and clp->cl_lock */
646 static int
647 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
648 struct nfs_server *server,
649 struct list_head *layout_list)
650 {
651 struct pnfs_layout_hdr *lo, *next;
652 struct inode *inode;
653
654 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
655 inode = igrab(lo->plh_inode);
656 if (inode == NULL)
657 continue;
658 list_del_init(&lo->plh_layouts);
659 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
660 continue;
661 rcu_read_unlock();
662 spin_unlock(&clp->cl_lock);
663 iput(inode);
664 spin_lock(&clp->cl_lock);
665 rcu_read_lock();
666 return -EAGAIN;
667 }
668 return 0;
669 }
670
671 static int
672 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
673 bool is_bulk_recall)
674 {
675 struct pnfs_layout_hdr *lo;
676 struct inode *inode;
677 struct pnfs_layout_range range = {
678 .iomode = IOMODE_ANY,
679 .offset = 0,
680 .length = NFS4_MAX_UINT64,
681 };
682 LIST_HEAD(lseg_list);
683 int ret = 0;
684
685 while (!list_empty(layout_list)) {
686 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
687 plh_bulk_destroy);
688 dprintk("%s freeing layout for inode %lu\n", __func__,
689 lo->plh_inode->i_ino);
690 inode = lo->plh_inode;
691
692 pnfs_layoutcommit_inode(inode, false);
693
694 spin_lock(&inode->i_lock);
695 list_del_init(&lo->plh_bulk_destroy);
696 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
697 if (is_bulk_recall)
698 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
699 if (pnfs_mark_matching_lsegs_invalid(lo, &lseg_list, &range))
700 ret = -EAGAIN;
701 spin_unlock(&inode->i_lock);
702 pnfs_free_lseg_list(&lseg_list);
703 pnfs_put_layout_hdr(lo);
704 iput(inode);
705 }
706 return ret;
707 }
708
709 int
710 pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
711 struct nfs_fsid *fsid,
712 bool is_recall)
713 {
714 struct nfs_server *server;
715 LIST_HEAD(layout_list);
716
717 spin_lock(&clp->cl_lock);
718 rcu_read_lock();
719 restart:
720 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
721 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
722 continue;
723 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
724 server,
725 &layout_list) != 0)
726 goto restart;
727 }
728 rcu_read_unlock();
729 spin_unlock(&clp->cl_lock);
730
731 if (list_empty(&layout_list))
732 return 0;
733 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
734 }
735
736 int
737 pnfs_destroy_layouts_byclid(struct nfs_client *clp,
738 bool is_recall)
739 {
740 struct nfs_server *server;
741 LIST_HEAD(layout_list);
742
743 spin_lock(&clp->cl_lock);
744 rcu_read_lock();
745 restart:
746 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
747 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
748 server,
749 &layout_list) != 0)
750 goto restart;
751 }
752 rcu_read_unlock();
753 spin_unlock(&clp->cl_lock);
754
755 if (list_empty(&layout_list))
756 return 0;
757 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
758 }
759
760 /*
761 * Called by the state manger to remove all layouts established under an
762 * expired lease.
763 */
764 void
765 pnfs_destroy_all_layouts(struct nfs_client *clp)
766 {
767 nfs4_deviceid_mark_client_invalid(clp);
768 nfs4_deviceid_purge_client(clp);
769
770 pnfs_destroy_layouts_byclid(clp, false);
771 }
772
773 /*
774 * Compare 2 layout stateid sequence ids, to see which is newer,
775 * taking into account wraparound issues.
776 */
777 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
778 {
779 return (s32)(s1 - s2) > 0;
780 }
781
782 /* update lo->plh_stateid with new if is more recent */
783 void
784 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
785 bool update_barrier)
786 {
787 u32 oldseq, newseq, new_barrier;
788 int empty = list_empty(&lo->plh_segs);
789
790 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
791 newseq = be32_to_cpu(new->seqid);
792 if (empty || pnfs_seqid_is_newer(newseq, oldseq)) {
793 nfs4_stateid_copy(&lo->plh_stateid, new);
794 if (update_barrier) {
795 new_barrier = be32_to_cpu(new->seqid);
796 } else {
797 /* Because of wraparound, we want to keep the barrier
798 * "close" to the current seqids.
799 */
800 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
801 }
802 if (empty || pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
803 lo->plh_barrier = new_barrier;
804 }
805 }
806
807 static bool
808 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
809 const nfs4_stateid *stateid)
810 {
811 u32 seqid = be32_to_cpu(stateid->seqid);
812
813 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
814 }
815
816 static bool
817 pnfs_layout_returning(const struct pnfs_layout_hdr *lo,
818 struct pnfs_layout_range *range)
819 {
820 return test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
821 (lo->plh_return_iomode == IOMODE_ANY ||
822 lo->plh_return_iomode == range->iomode);
823 }
824
825 /* lget is set to 1 if called from inside send_layoutget call chain */
826 static bool
827 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo,
828 struct pnfs_layout_range *range, int lget)
829 {
830 return lo->plh_block_lgets ||
831 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
832 (list_empty(&lo->plh_segs) &&
833 (atomic_read(&lo->plh_outstanding) > lget)) ||
834 pnfs_layout_returning(lo, range);
835 }
836
837 int
838 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
839 struct pnfs_layout_range *range,
840 struct nfs4_state *open_state)
841 {
842 int status = 0;
843
844 dprintk("--> %s\n", __func__);
845 spin_lock(&lo->plh_inode->i_lock);
846 if (pnfs_layoutgets_blocked(lo, range, 1)) {
847 status = -EAGAIN;
848 } else if (!nfs4_valid_open_stateid(open_state)) {
849 status = -EBADF;
850 } else if (list_empty(&lo->plh_segs) ||
851 test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
852 int seq;
853
854 do {
855 seq = read_seqbegin(&open_state->seqlock);
856 nfs4_stateid_copy(dst, &open_state->stateid);
857 } while (read_seqretry(&open_state->seqlock, seq));
858 } else
859 nfs4_stateid_copy(dst, &lo->plh_stateid);
860 spin_unlock(&lo->plh_inode->i_lock);
861 dprintk("<-- %s\n", __func__);
862 return status;
863 }
864
865 /*
866 * Get layout from server.
867 * for now, assume that whole file layouts are requested.
868 * arg->offset: 0
869 * arg->length: all ones
870 */
871 static struct pnfs_layout_segment *
872 send_layoutget(struct pnfs_layout_hdr *lo,
873 struct nfs_open_context *ctx,
874 struct pnfs_layout_range *range,
875 gfp_t gfp_flags)
876 {
877 struct inode *ino = lo->plh_inode;
878 struct nfs_server *server = NFS_SERVER(ino);
879 struct nfs4_layoutget *lgp;
880 struct pnfs_layout_segment *lseg;
881
882 dprintk("--> %s\n", __func__);
883
884 lgp = kzalloc(sizeof(*lgp), gfp_flags);
885 if (lgp == NULL)
886 return NULL;
887
888 lgp->args.minlength = PAGE_CACHE_SIZE;
889 if (lgp->args.minlength > range->length)
890 lgp->args.minlength = range->length;
891 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
892 lgp->args.range = *range;
893 lgp->args.type = server->pnfs_curr_ld->id;
894 lgp->args.inode = ino;
895 lgp->args.ctx = get_nfs_open_context(ctx);
896 lgp->gfp_flags = gfp_flags;
897 lgp->cred = lo->plh_lc_cred;
898
899 /* Synchronously retrieve layout information from server and
900 * store in lseg.
901 */
902 lseg = nfs4_proc_layoutget(lgp, gfp_flags);
903 if (IS_ERR(lseg)) {
904 switch (PTR_ERR(lseg)) {
905 case -ENOMEM:
906 case -ERESTARTSYS:
907 break;
908 default:
909 /* remember that LAYOUTGET failed and suspend trying */
910 pnfs_layout_io_set_failed(lo, range->iomode);
911 }
912 return NULL;
913 }
914
915 return lseg;
916 }
917
918 static void pnfs_clear_layoutcommit(struct inode *inode,
919 struct list_head *head)
920 {
921 struct nfs_inode *nfsi = NFS_I(inode);
922 struct pnfs_layout_segment *lseg, *tmp;
923
924 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
925 return;
926 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
927 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
928 continue;
929 pnfs_lseg_dec_and_remove_zero(lseg, head);
930 }
931 }
932
933 static int
934 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, nfs4_stateid stateid,
935 enum pnfs_iomode iomode, bool sync)
936 {
937 struct inode *ino = lo->plh_inode;
938 struct nfs4_layoutreturn *lrp;
939 int status = 0;
940
941 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
942 if (unlikely(lrp == NULL)) {
943 status = -ENOMEM;
944 spin_lock(&ino->i_lock);
945 lo->plh_block_lgets--;
946 rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq);
947 spin_unlock(&ino->i_lock);
948 pnfs_put_layout_hdr(lo);
949 goto out;
950 }
951
952 lrp->args.stateid = stateid;
953 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
954 lrp->args.inode = ino;
955 lrp->args.range.iomode = iomode;
956 lrp->args.range.offset = 0;
957 lrp->args.range.length = NFS4_MAX_UINT64;
958 lrp->args.layout = lo;
959 lrp->clp = NFS_SERVER(ino)->nfs_client;
960 lrp->cred = lo->plh_lc_cred;
961
962 status = nfs4_proc_layoutreturn(lrp, sync);
963 out:
964 dprintk("<-- %s status: %d\n", __func__, status);
965 return status;
966 }
967
968 /*
969 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
970 * when the layout segment list is empty.
971 *
972 * Note that a pnfs_layout_hdr can exist with an empty layout segment
973 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
974 * deviceid is marked invalid.
975 */
976 int
977 _pnfs_return_layout(struct inode *ino)
978 {
979 struct pnfs_layout_hdr *lo = NULL;
980 struct nfs_inode *nfsi = NFS_I(ino);
981 LIST_HEAD(tmp_list);
982 nfs4_stateid stateid;
983 int status = 0, empty;
984
985 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
986
987 spin_lock(&ino->i_lock);
988 lo = nfsi->layout;
989 if (!lo) {
990 spin_unlock(&ino->i_lock);
991 dprintk("NFS: %s no layout to return\n", __func__);
992 goto out;
993 }
994 stateid = nfsi->layout->plh_stateid;
995 /* Reference matched in nfs4_layoutreturn_release */
996 pnfs_get_layout_hdr(lo);
997 empty = list_empty(&lo->plh_segs);
998 pnfs_clear_layoutcommit(ino, &tmp_list);
999 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
1000
1001 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1002 struct pnfs_layout_range range = {
1003 .iomode = IOMODE_ANY,
1004 .offset = 0,
1005 .length = NFS4_MAX_UINT64,
1006 };
1007 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1008 }
1009
1010 /* Don't send a LAYOUTRETURN if list was initially empty */
1011 if (empty) {
1012 spin_unlock(&ino->i_lock);
1013 pnfs_put_layout_hdr(lo);
1014 dprintk("NFS: %s no layout segments to return\n", __func__);
1015 goto out;
1016 }
1017
1018 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1019 lo->plh_block_lgets++;
1020 spin_unlock(&ino->i_lock);
1021 pnfs_free_lseg_list(&tmp_list);
1022
1023 status = pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
1024 out:
1025 dprintk("<-- %s status: %d\n", __func__, status);
1026 return status;
1027 }
1028 EXPORT_SYMBOL_GPL(_pnfs_return_layout);
1029
1030 int
1031 pnfs_commit_and_return_layout(struct inode *inode)
1032 {
1033 struct pnfs_layout_hdr *lo;
1034 int ret;
1035
1036 spin_lock(&inode->i_lock);
1037 lo = NFS_I(inode)->layout;
1038 if (lo == NULL) {
1039 spin_unlock(&inode->i_lock);
1040 return 0;
1041 }
1042 pnfs_get_layout_hdr(lo);
1043 /* Block new layoutgets and read/write to ds */
1044 lo->plh_block_lgets++;
1045 spin_unlock(&inode->i_lock);
1046 filemap_fdatawait(inode->i_mapping);
1047 ret = pnfs_layoutcommit_inode(inode, true);
1048 if (ret == 0)
1049 ret = _pnfs_return_layout(inode);
1050 spin_lock(&inode->i_lock);
1051 lo->plh_block_lgets--;
1052 spin_unlock(&inode->i_lock);
1053 pnfs_put_layout_hdr(lo);
1054 return ret;
1055 }
1056
1057 bool pnfs_roc(struct inode *ino)
1058 {
1059 struct pnfs_layout_hdr *lo;
1060 struct pnfs_layout_segment *lseg, *tmp;
1061 nfs4_stateid stateid;
1062 LIST_HEAD(tmp_list);
1063 bool found = false, layoutreturn = false;
1064
1065 spin_lock(&ino->i_lock);
1066 lo = NFS_I(ino)->layout;
1067 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
1068 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
1069 goto out_nolayout;
1070 pnfs_clear_retry_layoutget(lo);
1071 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
1072 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1073 mark_lseg_invalid(lseg, &tmp_list);
1074 found = true;
1075 }
1076 if (!found)
1077 goto out_nolayout;
1078 lo->plh_block_lgets++;
1079 pnfs_get_layout_hdr(lo); /* matched in pnfs_roc_release */
1080 spin_unlock(&ino->i_lock);
1081 pnfs_free_lseg_list(&tmp_list);
1082 return true;
1083
1084 out_nolayout:
1085 if (lo) {
1086 stateid = lo->plh_stateid;
1087 layoutreturn =
1088 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1089 &lo->plh_flags);
1090 if (layoutreturn) {
1091 lo->plh_block_lgets++;
1092 pnfs_get_layout_hdr(lo);
1093 }
1094 }
1095 spin_unlock(&ino->i_lock);
1096 if (layoutreturn)
1097 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, true);
1098 return false;
1099 }
1100
1101 void pnfs_roc_release(struct inode *ino)
1102 {
1103 struct pnfs_layout_hdr *lo;
1104
1105 spin_lock(&ino->i_lock);
1106 lo = NFS_I(ino)->layout;
1107 lo->plh_block_lgets--;
1108 if (atomic_dec_and_test(&lo->plh_refcount)) {
1109 pnfs_detach_layout_hdr(lo);
1110 spin_unlock(&ino->i_lock);
1111 pnfs_free_layout_hdr(lo);
1112 } else
1113 spin_unlock(&ino->i_lock);
1114 }
1115
1116 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
1117 {
1118 struct pnfs_layout_hdr *lo;
1119
1120 spin_lock(&ino->i_lock);
1121 lo = NFS_I(ino)->layout;
1122 if (pnfs_seqid_is_newer(barrier, lo->plh_barrier))
1123 lo->plh_barrier = barrier;
1124 spin_unlock(&ino->i_lock);
1125 }
1126
1127 bool pnfs_roc_drain(struct inode *ino, u32 *barrier, struct rpc_task *task)
1128 {
1129 struct nfs_inode *nfsi = NFS_I(ino);
1130 struct pnfs_layout_hdr *lo;
1131 struct pnfs_layout_segment *lseg;
1132 nfs4_stateid stateid;
1133 u32 current_seqid;
1134 bool found = false, layoutreturn = false;
1135
1136 spin_lock(&ino->i_lock);
1137 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
1138 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
1139 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1140 found = true;
1141 goto out;
1142 }
1143 lo = nfsi->layout;
1144 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
1145
1146 /* Since close does not return a layout stateid for use as
1147 * a barrier, we choose the worst-case barrier.
1148 */
1149 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
1150 out:
1151 if (!found) {
1152 stateid = lo->plh_stateid;
1153 layoutreturn =
1154 test_and_clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1155 &lo->plh_flags);
1156 if (layoutreturn) {
1157 lo->plh_block_lgets++;
1158 pnfs_get_layout_hdr(lo);
1159 }
1160 }
1161 spin_unlock(&ino->i_lock);
1162 if (layoutreturn) {
1163 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1164 pnfs_send_layoutreturn(lo, stateid, IOMODE_ANY, false);
1165 }
1166 return found;
1167 }
1168
1169 /*
1170 * Compare two layout segments for sorting into layout cache.
1171 * We want to preferentially return RW over RO layouts, so ensure those
1172 * are seen first.
1173 */
1174 static s64
1175 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1176 const struct pnfs_layout_range *l2)
1177 {
1178 s64 d;
1179
1180 /* high offset > low offset */
1181 d = l1->offset - l2->offset;
1182 if (d)
1183 return d;
1184
1185 /* short length > long length */
1186 d = l2->length - l1->length;
1187 if (d)
1188 return d;
1189
1190 /* read > read/write */
1191 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1192 }
1193
1194 static void
1195 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1196 struct pnfs_layout_segment *lseg)
1197 {
1198 struct pnfs_layout_segment *lp;
1199
1200 dprintk("%s:Begin\n", __func__);
1201
1202 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
1203 if (pnfs_lseg_range_cmp(&lseg->pls_range, &lp->pls_range) > 0)
1204 continue;
1205 list_add_tail(&lseg->pls_list, &lp->pls_list);
1206 dprintk("%s: inserted lseg %p "
1207 "iomode %d offset %llu length %llu before "
1208 "lp %p iomode %d offset %llu length %llu\n",
1209 __func__, lseg, lseg->pls_range.iomode,
1210 lseg->pls_range.offset, lseg->pls_range.length,
1211 lp, lp->pls_range.iomode, lp->pls_range.offset,
1212 lp->pls_range.length);
1213 goto out;
1214 }
1215 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1216 dprintk("%s: inserted lseg %p "
1217 "iomode %d offset %llu length %llu at tail\n",
1218 __func__, lseg, lseg->pls_range.iomode,
1219 lseg->pls_range.offset, lseg->pls_range.length);
1220 out:
1221 pnfs_get_layout_hdr(lo);
1222
1223 dprintk("%s:Return\n", __func__);
1224 }
1225
1226 static struct pnfs_layout_hdr *
1227 alloc_init_layout_hdr(struct inode *ino,
1228 struct nfs_open_context *ctx,
1229 gfp_t gfp_flags)
1230 {
1231 struct pnfs_layout_hdr *lo;
1232
1233 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1234 if (!lo)
1235 return NULL;
1236 atomic_set(&lo->plh_refcount, 1);
1237 INIT_LIST_HEAD(&lo->plh_layouts);
1238 INIT_LIST_HEAD(&lo->plh_segs);
1239 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1240 lo->plh_inode = ino;
1241 lo->plh_lc_cred = get_rpccred(ctx->cred);
1242 return lo;
1243 }
1244
1245 static struct pnfs_layout_hdr *
1246 pnfs_find_alloc_layout(struct inode *ino,
1247 struct nfs_open_context *ctx,
1248 gfp_t gfp_flags)
1249 {
1250 struct nfs_inode *nfsi = NFS_I(ino);
1251 struct pnfs_layout_hdr *new = NULL;
1252
1253 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1254
1255 if (nfsi->layout != NULL)
1256 goto out_existing;
1257 spin_unlock(&ino->i_lock);
1258 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1259 spin_lock(&ino->i_lock);
1260
1261 if (likely(nfsi->layout == NULL)) { /* Won the race? */
1262 nfsi->layout = new;
1263 return new;
1264 } else if (new != NULL)
1265 pnfs_free_layout_hdr(new);
1266 out_existing:
1267 pnfs_get_layout_hdr(nfsi->layout);
1268 return nfsi->layout;
1269 }
1270
1271 /*
1272 * iomode matching rules:
1273 * iomode lseg match
1274 * ----- ----- -----
1275 * ANY READ true
1276 * ANY RW true
1277 * RW READ false
1278 * RW RW true
1279 * READ READ true
1280 * READ RW true
1281 */
1282 static bool
1283 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1284 const struct pnfs_layout_range *range)
1285 {
1286 struct pnfs_layout_range range1;
1287
1288 if ((range->iomode == IOMODE_RW &&
1289 ls_range->iomode != IOMODE_RW) ||
1290 !pnfs_lseg_range_intersecting(ls_range, range))
1291 return 0;
1292
1293 /* range1 covers only the first byte in the range */
1294 range1 = *range;
1295 range1.length = 1;
1296 return pnfs_lseg_range_contained(ls_range, &range1);
1297 }
1298
1299 /*
1300 * lookup range in layout
1301 */
1302 static struct pnfs_layout_segment *
1303 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1304 struct pnfs_layout_range *range)
1305 {
1306 struct pnfs_layout_segment *lseg, *ret = NULL;
1307
1308 dprintk("%s:Begin\n", __func__);
1309
1310 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1311 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1312 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
1313 pnfs_lseg_range_match(&lseg->pls_range, range)) {
1314 ret = pnfs_get_lseg(lseg);
1315 break;
1316 }
1317 if (lseg->pls_range.offset > range->offset)
1318 break;
1319 }
1320
1321 dprintk("%s:Return lseg %p ref %d\n",
1322 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
1323 return ret;
1324 }
1325
1326 /*
1327 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1328 * to the MDS or over pNFS
1329 *
1330 * The nfs_inode read_io and write_io fields are cumulative counters reset
1331 * when there are no layout segments. Note that in pnfs_update_layout iomode
1332 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1333 * WRITE request.
1334 *
1335 * A return of true means use MDS I/O.
1336 *
1337 * From rfc 5661:
1338 * If a file's size is smaller than the file size threshold, data accesses
1339 * SHOULD be sent to the metadata server. If an I/O request has a length that
1340 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1341 * server. If both file size and I/O size are provided, the client SHOULD
1342 * reach or exceed both thresholds before sending its read or write
1343 * requests to the data server.
1344 */
1345 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1346 struct inode *ino, int iomode)
1347 {
1348 struct nfs4_threshold *t = ctx->mdsthreshold;
1349 struct nfs_inode *nfsi = NFS_I(ino);
1350 loff_t fsize = i_size_read(ino);
1351 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1352
1353 if (t == NULL)
1354 return ret;
1355
1356 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1357 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1358
1359 switch (iomode) {
1360 case IOMODE_READ:
1361 if (t->bm & THRESHOLD_RD) {
1362 dprintk("%s fsize %llu\n", __func__, fsize);
1363 size_set = true;
1364 if (fsize < t->rd_sz)
1365 size = true;
1366 }
1367 if (t->bm & THRESHOLD_RD_IO) {
1368 dprintk("%s nfsi->read_io %llu\n", __func__,
1369 nfsi->read_io);
1370 io_set = true;
1371 if (nfsi->read_io < t->rd_io_sz)
1372 io = true;
1373 }
1374 break;
1375 case IOMODE_RW:
1376 if (t->bm & THRESHOLD_WR) {
1377 dprintk("%s fsize %llu\n", __func__, fsize);
1378 size_set = true;
1379 if (fsize < t->wr_sz)
1380 size = true;
1381 }
1382 if (t->bm & THRESHOLD_WR_IO) {
1383 dprintk("%s nfsi->write_io %llu\n", __func__,
1384 nfsi->write_io);
1385 io_set = true;
1386 if (nfsi->write_io < t->wr_io_sz)
1387 io = true;
1388 }
1389 break;
1390 }
1391 if (size_set && io_set) {
1392 if (size && io)
1393 ret = true;
1394 } else if (size || io)
1395 ret = true;
1396
1397 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1398 return ret;
1399 }
1400
1401 /* stop waiting if someone clears NFS_LAYOUT_RETRY_LAYOUTGET bit. */
1402 static int pnfs_layoutget_retry_bit_wait(struct wait_bit_key *key)
1403 {
1404 if (!test_bit(NFS_LAYOUT_RETRY_LAYOUTGET, key->flags))
1405 return 1;
1406 return nfs_wait_bit_killable(key);
1407 }
1408
1409 static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1410 {
1411 /*
1412 * send layoutcommit as it can hold up layoutreturn due to lseg
1413 * reference
1414 */
1415 pnfs_layoutcommit_inode(lo->plh_inode, false);
1416 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1417 pnfs_layoutget_retry_bit_wait,
1418 TASK_UNINTERRUPTIBLE);
1419 }
1420
1421 /*
1422 * Layout segment is retreived from the server if not cached.
1423 * The appropriate layout segment is referenced and returned to the caller.
1424 */
1425 struct pnfs_layout_segment *
1426 pnfs_update_layout(struct inode *ino,
1427 struct nfs_open_context *ctx,
1428 loff_t pos,
1429 u64 count,
1430 enum pnfs_iomode iomode,
1431 gfp_t gfp_flags)
1432 {
1433 struct pnfs_layout_range arg = {
1434 .iomode = iomode,
1435 .offset = pos,
1436 .length = count,
1437 };
1438 unsigned pg_offset;
1439 struct nfs_server *server = NFS_SERVER(ino);
1440 struct nfs_client *clp = server->nfs_client;
1441 struct pnfs_layout_hdr *lo;
1442 struct pnfs_layout_segment *lseg = NULL;
1443 bool first;
1444
1445 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
1446 goto out;
1447
1448 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
1449 goto out;
1450
1451 lookup_again:
1452 first = false;
1453 spin_lock(&ino->i_lock);
1454 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1455 if (lo == NULL) {
1456 spin_unlock(&ino->i_lock);
1457 goto out;
1458 }
1459
1460 /* Do we even need to bother with this? */
1461 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1462 dprintk("%s matches recall, use MDS\n", __func__);
1463 goto out_unlock;
1464 }
1465
1466 /* if LAYOUTGET already failed once we don't try again */
1467 if (pnfs_layout_io_test_failed(lo, iomode) &&
1468 !pnfs_should_retry_layoutget(lo))
1469 goto out_unlock;
1470
1471 first = list_empty(&lo->plh_segs);
1472 if (first) {
1473 /* The first layoutget for the file. Need to serialize per
1474 * RFC 5661 Errata 3208.
1475 */
1476 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1477 &lo->plh_flags)) {
1478 spin_unlock(&ino->i_lock);
1479 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1480 TASK_UNINTERRUPTIBLE);
1481 pnfs_put_layout_hdr(lo);
1482 goto lookup_again;
1483 }
1484 } else {
1485 /* Check to see if the layout for the given range
1486 * already exists
1487 */
1488 lseg = pnfs_find_lseg(lo, &arg);
1489 if (lseg)
1490 goto out_unlock;
1491 }
1492
1493 /*
1494 * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1495 * for LAYOUTRETURN even if first is true.
1496 */
1497 if (!lseg && pnfs_should_retry_layoutget(lo) &&
1498 test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1499 spin_unlock(&ino->i_lock);
1500 dprintk("%s wait for layoutreturn\n", __func__);
1501 if (pnfs_prepare_to_retry_layoutget(lo)) {
1502 pnfs_put_layout_hdr(lo);
1503 dprintk("%s retrying\n", __func__);
1504 goto lookup_again;
1505 }
1506 goto out_put_layout_hdr;
1507 }
1508
1509 if (pnfs_layoutgets_blocked(lo, &arg, 0))
1510 goto out_unlock;
1511 atomic_inc(&lo->plh_outstanding);
1512 spin_unlock(&ino->i_lock);
1513
1514 if (list_empty(&lo->plh_layouts)) {
1515 /* The lo must be on the clp list if there is any
1516 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1517 */
1518 spin_lock(&clp->cl_lock);
1519 if (list_empty(&lo->plh_layouts))
1520 list_add_tail(&lo->plh_layouts, &server->layouts);
1521 spin_unlock(&clp->cl_lock);
1522 }
1523
1524 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1525 if (pg_offset) {
1526 arg.offset -= pg_offset;
1527 arg.length += pg_offset;
1528 }
1529 if (arg.length != NFS4_MAX_UINT64)
1530 arg.length = PAGE_CACHE_ALIGN(arg.length);
1531
1532 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
1533 pnfs_clear_retry_layoutget(lo);
1534 atomic_dec(&lo->plh_outstanding);
1535 out_put_layout_hdr:
1536 if (first) {
1537 unsigned long *bitlock = &lo->plh_flags;
1538
1539 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1540 smp_mb__after_atomic();
1541 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1542 }
1543 pnfs_put_layout_hdr(lo);
1544 out:
1545 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1546 "(%s, offset: %llu, length: %llu)\n",
1547 __func__, ino->i_sb->s_id,
1548 (unsigned long long)NFS_FILEID(ino),
1549 lseg == NULL ? "not found" : "found",
1550 iomode==IOMODE_RW ? "read/write" : "read-only",
1551 (unsigned long long)pos,
1552 (unsigned long long)count);
1553 return lseg;
1554 out_unlock:
1555 spin_unlock(&ino->i_lock);
1556 goto out_put_layout_hdr;
1557 }
1558 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1559
1560 struct pnfs_layout_segment *
1561 pnfs_layout_process(struct nfs4_layoutget *lgp)
1562 {
1563 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1564 struct nfs4_layoutget_res *res = &lgp->res;
1565 struct pnfs_layout_segment *lseg;
1566 struct inode *ino = lo->plh_inode;
1567 LIST_HEAD(free_me);
1568 int status = 0;
1569
1570 /* Inject layout blob into I/O device driver */
1571 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1572 if (!lseg || IS_ERR(lseg)) {
1573 if (!lseg)
1574 status = -ENOMEM;
1575 else
1576 status = PTR_ERR(lseg);
1577 dprintk("%s: Could not allocate layout: error %d\n",
1578 __func__, status);
1579 goto out;
1580 }
1581
1582 init_lseg(lo, lseg);
1583 lseg->pls_range = res->range;
1584
1585 spin_lock(&ino->i_lock);
1586 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1587 dprintk("%s forget reply due to recall\n", __func__);
1588 goto out_forget_reply;
1589 }
1590
1591 if (pnfs_layoutgets_blocked(lo, &lgp->args.range, 1)) {
1592 dprintk("%s forget reply due to state\n", __func__);
1593 goto out_forget_reply;
1594 }
1595
1596 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1597 /* existing state ID, make sure the sequence number matches. */
1598 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1599 dprintk("%s forget reply due to sequence\n", __func__);
1600 goto out_forget_reply;
1601 }
1602 pnfs_set_layout_stateid(lo, &res->stateid, false);
1603 } else {
1604 /*
1605 * We got an entirely new state ID. Mark all segments for the
1606 * inode invalid, and don't bother validating the stateid
1607 * sequence number.
1608 */
1609 pnfs_mark_matching_lsegs_invalid(lo, &free_me, NULL);
1610
1611 nfs4_stateid_copy(&lo->plh_stateid, &res->stateid);
1612 lo->plh_barrier = be32_to_cpu(res->stateid.seqid);
1613 }
1614
1615 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1616
1617 pnfs_get_lseg(lseg);
1618 pnfs_layout_insert_lseg(lo, lseg);
1619
1620 if (res->return_on_close) {
1621 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1622 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1623 }
1624
1625 spin_unlock(&ino->i_lock);
1626 pnfs_free_lseg_list(&free_me);
1627 return lseg;
1628 out:
1629 return ERR_PTR(status);
1630
1631 out_forget_reply:
1632 spin_unlock(&ino->i_lock);
1633 lseg->pls_layout = lo;
1634 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1635 goto out;
1636 }
1637
1638 static void
1639 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
1640 struct list_head *tmp_list,
1641 struct pnfs_layout_range *return_range)
1642 {
1643 struct pnfs_layout_segment *lseg, *next;
1644
1645 dprintk("%s:Begin lo %p\n", __func__, lo);
1646
1647 if (list_empty(&lo->plh_segs))
1648 return;
1649
1650 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
1651 if (should_free_lseg(&lseg->pls_range, return_range)) {
1652 dprintk("%s: marking lseg %p iomode %d "
1653 "offset %llu length %llu\n", __func__,
1654 lseg, lseg->pls_range.iomode,
1655 lseg->pls_range.offset,
1656 lseg->pls_range.length);
1657 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1658 mark_lseg_invalid(lseg, tmp_list);
1659 }
1660 }
1661
1662 void pnfs_error_mark_layout_for_return(struct inode *inode,
1663 struct pnfs_layout_segment *lseg)
1664 {
1665 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
1666 int iomode = pnfs_iomode_to_fail_bit(lseg->pls_range.iomode);
1667 struct pnfs_layout_range range = {
1668 .iomode = lseg->pls_range.iomode,
1669 .offset = 0,
1670 .length = NFS4_MAX_UINT64,
1671 };
1672 LIST_HEAD(free_me);
1673
1674 spin_lock(&inode->i_lock);
1675 /* set failure bit so that pnfs path will be retried later */
1676 pnfs_layout_set_fail_bit(lo, iomode);
1677 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1678 if (lo->plh_return_iomode == 0)
1679 lo->plh_return_iomode = range.iomode;
1680 else if (lo->plh_return_iomode != range.iomode)
1681 lo->plh_return_iomode = IOMODE_ANY;
1682 /*
1683 * mark all matching lsegs so that we are sure to have no live
1684 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
1685 * for how it works.
1686 */
1687 pnfs_mark_matching_lsegs_return(lo, &free_me, &range);
1688 spin_unlock(&inode->i_lock);
1689 pnfs_free_lseg_list(&free_me);
1690 }
1691 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
1692
1693 void
1694 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1695 {
1696 u64 rd_size = req->wb_bytes;
1697
1698 WARN_ON_ONCE(pgio->pg_lseg != NULL);
1699
1700 if (pgio->pg_dreq == NULL)
1701 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
1702 else
1703 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
1704
1705 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1706 req->wb_context,
1707 req_offset(req),
1708 rd_size,
1709 IOMODE_READ,
1710 GFP_KERNEL);
1711 /* If no lseg, fall back to read through mds */
1712 if (pgio->pg_lseg == NULL)
1713 nfs_pageio_reset_read_mds(pgio);
1714
1715 }
1716 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1717
1718 void
1719 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
1720 struct nfs_page *req, u64 wb_size)
1721 {
1722 WARN_ON_ONCE(pgio->pg_lseg != NULL);
1723
1724 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1725 req->wb_context,
1726 req_offset(req),
1727 wb_size,
1728 IOMODE_RW,
1729 GFP_NOFS);
1730 /* If no lseg, fall back to write through mds */
1731 if (pgio->pg_lseg == NULL)
1732 nfs_pageio_reset_write_mds(pgio);
1733 }
1734 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1735
1736 void
1737 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
1738 {
1739 if (desc->pg_lseg) {
1740 pnfs_put_lseg(desc->pg_lseg);
1741 desc->pg_lseg = NULL;
1742 }
1743 }
1744 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
1745
1746 /*
1747 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
1748 * of bytes (maximum @req->wb_bytes) that can be coalesced.
1749 */
1750 size_t
1751 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
1752 struct nfs_page *prev, struct nfs_page *req)
1753 {
1754 unsigned int size;
1755 u64 seg_end, req_start, seg_left;
1756
1757 size = nfs_generic_pg_test(pgio, prev, req);
1758 if (!size)
1759 return 0;
1760
1761 /*
1762 * 'size' contains the number of bytes left in the current page (up
1763 * to the original size asked for in @req->wb_bytes).
1764 *
1765 * Calculate how many bytes are left in the layout segment
1766 * and if there are less bytes than 'size', return that instead.
1767 *
1768 * Please also note that 'end_offset' is actually the offset of the
1769 * first byte that lies outside the pnfs_layout_range. FIXME?
1770 *
1771 */
1772 if (pgio->pg_lseg) {
1773 seg_end = end_offset(pgio->pg_lseg->pls_range.offset,
1774 pgio->pg_lseg->pls_range.length);
1775 req_start = req_offset(req);
1776 WARN_ON_ONCE(req_start > seg_end);
1777 /* start of request is past the last byte of this segment */
1778 if (req_start >= seg_end)
1779 return 0;
1780
1781 /* adjust 'size' iff there are fewer bytes left in the
1782 * segment than what nfs_generic_pg_test returned */
1783 seg_left = seg_end - req_start;
1784 if (seg_left < size)
1785 size = (unsigned int)seg_left;
1786 }
1787
1788 return size;
1789 }
1790 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
1791
1792 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
1793 {
1794 struct nfs_pageio_descriptor pgio;
1795
1796 /* Resend all requests through the MDS */
1797 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
1798 hdr->completion_ops);
1799 return nfs_pageio_resend(&pgio, hdr);
1800 }
1801 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
1802
1803 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
1804 {
1805
1806 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1807 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1808 PNFS_LAYOUTRET_ON_ERROR) {
1809 pnfs_return_layout(hdr->inode);
1810 }
1811 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1812 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
1813 }
1814
1815 /*
1816 * Called by non rpc-based layout drivers
1817 */
1818 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
1819 {
1820 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
1821 if (!hdr->pnfs_error) {
1822 pnfs_set_layoutcommit(hdr);
1823 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
1824 } else
1825 pnfs_ld_handle_write_error(hdr);
1826 hdr->mds_ops->rpc_release(hdr);
1827 }
1828 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
1829
1830 static void
1831 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1832 struct nfs_pgio_header *hdr)
1833 {
1834 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1835
1836 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1837 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
1838 nfs_pageio_reset_write_mds(desc);
1839 mirror->pg_recoalesce = 1;
1840 }
1841 nfs_pgio_data_destroy(hdr);
1842 }
1843
1844 static enum pnfs_try_status
1845 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
1846 const struct rpc_call_ops *call_ops,
1847 struct pnfs_layout_segment *lseg,
1848 int how)
1849 {
1850 struct inode *inode = hdr->inode;
1851 enum pnfs_try_status trypnfs;
1852 struct nfs_server *nfss = NFS_SERVER(inode);
1853
1854 hdr->mds_ops = call_ops;
1855
1856 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1857 inode->i_ino, hdr->args.count, hdr->args.offset, how);
1858 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
1859 if (trypnfs != PNFS_NOT_ATTEMPTED)
1860 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1861 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1862 return trypnfs;
1863 }
1864
1865 static void
1866 pnfs_do_write(struct nfs_pageio_descriptor *desc,
1867 struct nfs_pgio_header *hdr, int how)
1868 {
1869 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1870 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1871 enum pnfs_try_status trypnfs;
1872
1873 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
1874 if (trypnfs == PNFS_NOT_ATTEMPTED)
1875 pnfs_write_through_mds(desc, hdr);
1876 }
1877
1878 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1879 {
1880 pnfs_put_lseg(hdr->lseg);
1881 nfs_pgio_header_free(hdr);
1882 }
1883 EXPORT_SYMBOL_GPL(pnfs_writehdr_free);
1884
1885 int
1886 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1887 {
1888 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1889
1890 struct nfs_pgio_header *hdr;
1891 int ret;
1892
1893 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1894 if (!hdr) {
1895 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
1896 return -ENOMEM;
1897 }
1898 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
1899
1900 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
1901 ret = nfs_generic_pgio(desc, hdr);
1902 if (!ret)
1903 pnfs_do_write(desc, hdr, desc->pg_ioflags);
1904
1905 return ret;
1906 }
1907 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1908
1909 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
1910 {
1911 struct nfs_pageio_descriptor pgio;
1912
1913 /* Resend all requests through the MDS */
1914 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
1915 return nfs_pageio_resend(&pgio, hdr);
1916 }
1917 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
1918
1919 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
1920 {
1921 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1922 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1923 PNFS_LAYOUTRET_ON_ERROR) {
1924 pnfs_return_layout(hdr->inode);
1925 }
1926 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1927 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
1928 }
1929
1930 /*
1931 * Called by non rpc-based layout drivers
1932 */
1933 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
1934 {
1935 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
1936 if (likely(!hdr->pnfs_error)) {
1937 __nfs4_read_done_cb(hdr);
1938 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
1939 } else
1940 pnfs_ld_handle_read_error(hdr);
1941 hdr->mds_ops->rpc_release(hdr);
1942 }
1943 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1944
1945 static void
1946 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1947 struct nfs_pgio_header *hdr)
1948 {
1949 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1950
1951 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1952 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
1953 nfs_pageio_reset_read_mds(desc);
1954 mirror->pg_recoalesce = 1;
1955 }
1956 nfs_pgio_data_destroy(hdr);
1957 }
1958
1959 /*
1960 * Call the appropriate parallel I/O subsystem read function.
1961 */
1962 static enum pnfs_try_status
1963 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
1964 const struct rpc_call_ops *call_ops,
1965 struct pnfs_layout_segment *lseg)
1966 {
1967 struct inode *inode = hdr->inode;
1968 struct nfs_server *nfss = NFS_SERVER(inode);
1969 enum pnfs_try_status trypnfs;
1970
1971 hdr->mds_ops = call_ops;
1972
1973 dprintk("%s: Reading ino:%lu %u@%llu\n",
1974 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
1975
1976 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
1977 if (trypnfs != PNFS_NOT_ATTEMPTED)
1978 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1979 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1980 return trypnfs;
1981 }
1982
1983 /* Resend all requests through pnfs. */
1984 int pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
1985 {
1986 struct nfs_pageio_descriptor pgio;
1987
1988 nfs_pageio_init_read(&pgio, hdr->inode, false, hdr->completion_ops);
1989 return nfs_pageio_resend(&pgio, hdr);
1990 }
1991 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
1992
1993 static void
1994 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
1995 {
1996 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1997 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1998 enum pnfs_try_status trypnfs;
1999 int err = 0;
2000
2001 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
2002 if (trypnfs == PNFS_TRY_AGAIN)
2003 err = pnfs_read_resend_pnfs(hdr);
2004 if (trypnfs == PNFS_NOT_ATTEMPTED || err)
2005 pnfs_read_through_mds(desc, hdr);
2006 }
2007
2008 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2009 {
2010 pnfs_put_lseg(hdr->lseg);
2011 nfs_pgio_header_free(hdr);
2012 }
2013 EXPORT_SYMBOL_GPL(pnfs_readhdr_free);
2014
2015 int
2016 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2017 {
2018 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2019
2020 struct nfs_pgio_header *hdr;
2021 int ret;
2022
2023 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2024 if (!hdr) {
2025 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
2026 return -ENOMEM;
2027 }
2028 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
2029 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2030 ret = nfs_generic_pgio(desc, hdr);
2031 if (!ret)
2032 pnfs_do_read(desc, hdr);
2033 return ret;
2034 }
2035 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2036
2037 static void pnfs_clear_layoutcommitting(struct inode *inode)
2038 {
2039 unsigned long *bitlock = &NFS_I(inode)->flags;
2040
2041 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
2042 smp_mb__after_atomic();
2043 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2044 }
2045
2046 /*
2047 * There can be multiple RW segments.
2048 */
2049 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
2050 {
2051 struct pnfs_layout_segment *lseg;
2052
2053 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2054 if (lseg->pls_range.iomode == IOMODE_RW &&
2055 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
2056 list_add(&lseg->pls_lc_list, listp);
2057 }
2058 }
2059
2060 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2061 {
2062 struct pnfs_layout_segment *lseg, *tmp;
2063
2064 /* Matched by references in pnfs_set_layoutcommit */
2065 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2066 list_del_init(&lseg->pls_lc_list);
2067 pnfs_put_lseg(lseg);
2068 }
2069
2070 pnfs_clear_layoutcommitting(inode);
2071 }
2072
2073 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2074 {
2075 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
2076 }
2077 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2078
2079 void
2080 pnfs_set_layoutcommit(struct nfs_pgio_header *hdr)
2081 {
2082 struct inode *inode = hdr->inode;
2083 struct nfs_inode *nfsi = NFS_I(inode);
2084 loff_t end_pos = hdr->mds_offset + hdr->res.count;
2085 bool mark_as_dirty = false;
2086
2087 spin_lock(&inode->i_lock);
2088 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2089 mark_as_dirty = true;
2090 dprintk("%s: Set layoutcommit for inode %lu ",
2091 __func__, inode->i_ino);
2092 }
2093 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
2094 /* references matched in nfs4_layoutcommit_release */
2095 pnfs_get_lseg(hdr->lseg);
2096 }
2097 if (end_pos > nfsi->layout->plh_lwb)
2098 nfsi->layout->plh_lwb = end_pos;
2099 spin_unlock(&inode->i_lock);
2100 dprintk("%s: lseg %p end_pos %llu\n",
2101 __func__, hdr->lseg, nfsi->layout->plh_lwb);
2102
2103 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2104 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2105 if (mark_as_dirty)
2106 mark_inode_dirty_sync(inode);
2107 }
2108 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2109
2110 void pnfs_commit_set_layoutcommit(struct nfs_commit_data *data)
2111 {
2112 struct inode *inode = data->inode;
2113 struct nfs_inode *nfsi = NFS_I(inode);
2114 bool mark_as_dirty = false;
2115
2116 spin_lock(&inode->i_lock);
2117 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2118 mark_as_dirty = true;
2119 dprintk("%s: Set layoutcommit for inode %lu ",
2120 __func__, inode->i_ino);
2121 }
2122 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &data->lseg->pls_flags)) {
2123 /* references matched in nfs4_layoutcommit_release */
2124 pnfs_get_lseg(data->lseg);
2125 }
2126 if (data->lwb > nfsi->layout->plh_lwb)
2127 nfsi->layout->plh_lwb = data->lwb;
2128 spin_unlock(&inode->i_lock);
2129 dprintk("%s: lseg %p end_pos %llu\n",
2130 __func__, data->lseg, nfsi->layout->plh_lwb);
2131
2132 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2133 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2134 if (mark_as_dirty)
2135 mark_inode_dirty_sync(inode);
2136 }
2137 EXPORT_SYMBOL_GPL(pnfs_commit_set_layoutcommit);
2138
2139 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2140 {
2141 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2142
2143 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2144 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
2145 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
2146 }
2147
2148 /*
2149 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2150 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2151 * data to disk to allow the server to recover the data if it crashes.
2152 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2153 * is off, and a COMMIT is sent to a data server, or
2154 * if WRITEs to a data server return NFS_DATA_SYNC.
2155 */
2156 int
2157 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
2158 {
2159 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2160 struct nfs4_layoutcommit_data *data;
2161 struct nfs_inode *nfsi = NFS_I(inode);
2162 loff_t end_pos;
2163 int status;
2164
2165 if (!pnfs_layoutcommit_outstanding(inode))
2166 return 0;
2167
2168 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2169
2170 status = -EAGAIN;
2171 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2172 if (!sync)
2173 goto out;
2174 status = wait_on_bit_lock_action(&nfsi->flags,
2175 NFS_INO_LAYOUTCOMMITTING,
2176 nfs_wait_bit_killable,
2177 TASK_KILLABLE);
2178 if (status)
2179 goto out;
2180 }
2181
2182 status = -ENOMEM;
2183 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2184 data = kzalloc(sizeof(*data), GFP_NOFS);
2185 if (!data)
2186 goto clear_layoutcommitting;
2187
2188 status = 0;
2189 spin_lock(&inode->i_lock);
2190 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2191 goto out_unlock;
2192
2193 INIT_LIST_HEAD(&data->lseg_list);
2194 pnfs_list_write_lseg(inode, &data->lseg_list);
2195
2196 end_pos = nfsi->layout->plh_lwb;
2197 nfsi->layout->plh_lwb = 0;
2198
2199 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
2200 spin_unlock(&inode->i_lock);
2201
2202 data->args.inode = inode;
2203 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
2204 nfs_fattr_init(&data->fattr);
2205 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2206 data->res.fattr = &data->fattr;
2207 data->args.lastbytewritten = end_pos - 1;
2208 data->res.server = NFS_SERVER(inode);
2209
2210 if (ld->prepare_layoutcommit) {
2211 status = ld->prepare_layoutcommit(&data->args);
2212 if (status) {
2213 spin_lock(&inode->i_lock);
2214 if (end_pos < nfsi->layout->plh_lwb)
2215 nfsi->layout->plh_lwb = end_pos;
2216 spin_unlock(&inode->i_lock);
2217 put_rpccred(data->cred);
2218 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2219 goto clear_layoutcommitting;
2220 }
2221 }
2222
2223
2224 status = nfs4_proc_layoutcommit(data, sync);
2225 out:
2226 if (status)
2227 mark_inode_dirty_sync(inode);
2228 dprintk("<-- %s status %d\n", __func__, status);
2229 return status;
2230 out_unlock:
2231 spin_unlock(&inode->i_lock);
2232 kfree(data);
2233 clear_layoutcommitting:
2234 pnfs_clear_layoutcommitting(inode);
2235 goto out;
2236 }
2237 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
2238
2239 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2240 {
2241 struct nfs4_threshold *thp;
2242
2243 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2244 if (!thp) {
2245 dprintk("%s mdsthreshold allocation failed\n", __func__);
2246 return NULL;
2247 }
2248 return thp;
2249 }