GNU Linux-libre 4.14.266-gnu1
[releases.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 <linux/sort.h>
34 #include "internal.h"
35 #include "pnfs.h"
36 #include "iostat.h"
37 #include "nfs4trace.h"
38 #include "delegation.h"
39 #include "nfs42.h"
40
41 #define NFSDBG_FACILITY         NFSDBG_PNFS
42 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
43
44 /* Locking:
45  *
46  * pnfs_spinlock:
47  *      protects pnfs_modules_tbl.
48  */
49 static DEFINE_SPINLOCK(pnfs_spinlock);
50
51 /*
52  * pnfs_modules_tbl holds all pnfs modules
53  */
54 static LIST_HEAD(pnfs_modules_tbl);
55
56 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
57 static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
58                 struct list_head *free_me,
59                 const struct pnfs_layout_range *range,
60                 u32 seq);
61 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
62                                 struct list_head *tmp_list);
63
64 /* Return the registered pnfs layout driver module matching given id */
65 static struct pnfs_layoutdriver_type *
66 find_pnfs_driver_locked(u32 id)
67 {
68         struct pnfs_layoutdriver_type *local;
69
70         list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
71                 if (local->id == id)
72                         goto out;
73         local = NULL;
74 out:
75         dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
76         return local;
77 }
78
79 static struct pnfs_layoutdriver_type *
80 find_pnfs_driver(u32 id)
81 {
82         struct pnfs_layoutdriver_type *local;
83
84         spin_lock(&pnfs_spinlock);
85         local = find_pnfs_driver_locked(id);
86         if (local != NULL && !try_module_get(local->owner)) {
87                 dprintk("%s: Could not grab reference on module\n", __func__);
88                 local = NULL;
89         }
90         spin_unlock(&pnfs_spinlock);
91         return local;
92 }
93
94 void
95 unset_pnfs_layoutdriver(struct nfs_server *nfss)
96 {
97         if (nfss->pnfs_curr_ld) {
98                 if (nfss->pnfs_curr_ld->clear_layoutdriver)
99                         nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
100                 /* Decrement the MDS count. Purge the deviceid cache if zero */
101                 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
102                         nfs4_deviceid_purge_client(nfss->nfs_client);
103                 module_put(nfss->pnfs_curr_ld->owner);
104         }
105         nfss->pnfs_curr_ld = NULL;
106 }
107
108 /*
109  * When the server sends a list of layout types, we choose one in the order
110  * given in the list below.
111  *
112  * FIXME: should this list be configurable in some fashion? module param?
113  *        mount option? something else?
114  */
115 static const u32 ld_prefs[] = {
116         LAYOUT_SCSI,
117         LAYOUT_BLOCK_VOLUME,
118         LAYOUT_OSD2_OBJECTS,
119         LAYOUT_FLEX_FILES,
120         LAYOUT_NFSV4_1_FILES,
121         0
122 };
123
124 static int
125 ld_cmp(const void *e1, const void *e2)
126 {
127         u32 ld1 = *((u32 *)e1);
128         u32 ld2 = *((u32 *)e2);
129         int i;
130
131         for (i = 0; ld_prefs[i] != 0; i++) {
132                 if (ld1 == ld_prefs[i])
133                         return -1;
134
135                 if (ld2 == ld_prefs[i])
136                         return 1;
137         }
138         return 0;
139 }
140
141 /*
142  * Try to set the server's pnfs module to the pnfs layout type specified by id.
143  * Currently only one pNFS layout driver per filesystem is supported.
144  *
145  * @ids array of layout types supported by MDS.
146  */
147 void
148 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
149                       struct nfs_fsinfo *fsinfo)
150 {
151         struct pnfs_layoutdriver_type *ld_type = NULL;
152         u32 id;
153         int i;
154
155         if (fsinfo->nlayouttypes == 0)
156                 goto out_no_driver;
157         if (!(server->nfs_client->cl_exchange_flags &
158                  (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
159                 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
160                         __func__, server->nfs_client->cl_exchange_flags);
161                 goto out_no_driver;
162         }
163
164         sort(fsinfo->layouttype, fsinfo->nlayouttypes,
165                 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
166
167         for (i = 0; i < fsinfo->nlayouttypes; i++) {
168                 id = fsinfo->layouttype[i];
169                 ld_type = find_pnfs_driver(id);
170                 if (!ld_type) {
171                         request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
172                                         id);
173                         ld_type = find_pnfs_driver(id);
174                 }
175                 if (ld_type)
176                         break;
177         }
178
179         if (!ld_type) {
180                 dprintk("%s: No pNFS module found!\n", __func__);
181                 goto out_no_driver;
182         }
183
184         server->pnfs_curr_ld = ld_type;
185         if (ld_type->set_layoutdriver
186             && ld_type->set_layoutdriver(server, mntfh)) {
187                 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
188                         "driver %u.\n", __func__, id);
189                 module_put(ld_type->owner);
190                 goto out_no_driver;
191         }
192         /* Bump the MDS count */
193         atomic_inc(&server->nfs_client->cl_mds_count);
194
195         dprintk("%s: pNFS module for %u set\n", __func__, id);
196         return;
197
198 out_no_driver:
199         dprintk("%s: Using NFSv4 I/O\n", __func__);
200         server->pnfs_curr_ld = NULL;
201 }
202
203 int
204 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
205 {
206         int status = -EINVAL;
207         struct pnfs_layoutdriver_type *tmp;
208
209         if (ld_type->id == 0) {
210                 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
211                 return status;
212         }
213         if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
214                 printk(KERN_ERR "NFS: %s Layout driver must provide "
215                        "alloc_lseg and free_lseg.\n", __func__);
216                 return status;
217         }
218
219         spin_lock(&pnfs_spinlock);
220         tmp = find_pnfs_driver_locked(ld_type->id);
221         if (!tmp) {
222                 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
223                 status = 0;
224                 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
225                         ld_type->name);
226         } else {
227                 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
228                         __func__, ld_type->id);
229         }
230         spin_unlock(&pnfs_spinlock);
231
232         return status;
233 }
234 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
235
236 void
237 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
238 {
239         dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
240         spin_lock(&pnfs_spinlock);
241         list_del(&ld_type->pnfs_tblid);
242         spin_unlock(&pnfs_spinlock);
243 }
244 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
245
246 /*
247  * pNFS client layout cache
248  */
249
250 /* Need to hold i_lock if caller does not already hold reference */
251 void
252 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
253 {
254         atomic_inc(&lo->plh_refcount);
255 }
256
257 static struct pnfs_layout_hdr *
258 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
259 {
260         struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
261         return ld->alloc_layout_hdr(ino, gfp_flags);
262 }
263
264 static void
265 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
266 {
267         struct nfs_server *server = NFS_SERVER(lo->plh_inode);
268         struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
269
270         if (!list_empty(&lo->plh_layouts)) {
271                 struct nfs_client *clp = server->nfs_client;
272
273                 spin_lock(&clp->cl_lock);
274                 list_del_init(&lo->plh_layouts);
275                 spin_unlock(&clp->cl_lock);
276         }
277         put_rpccred(lo->plh_lc_cred);
278         return ld->free_layout_hdr(lo);
279 }
280
281 static void
282 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
283 {
284         struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
285         dprintk("%s: freeing layout cache %p\n", __func__, lo);
286         nfsi->layout = NULL;
287         /* Reset MDS Threshold I/O counters */
288         nfsi->write_io = 0;
289         nfsi->read_io = 0;
290 }
291
292 void
293 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
294 {
295         struct inode *inode;
296
297         if (!lo)
298                 return;
299         inode = lo->plh_inode;
300         pnfs_layoutreturn_before_put_layout_hdr(lo);
301
302         if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
303                 if (!list_empty(&lo->plh_segs))
304                         WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
305                 pnfs_detach_layout_hdr(lo);
306                 spin_unlock(&inode->i_lock);
307                 pnfs_free_layout_hdr(lo);
308         }
309 }
310
311 static void
312 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
313                          u32 seq)
314 {
315         if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
316                 iomode = IOMODE_ANY;
317         lo->plh_return_iomode = iomode;
318         set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
319         if (seq != 0) {
320                 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
321                 lo->plh_return_seq = seq;
322         }
323 }
324
325 static void
326 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
327 {
328         struct pnfs_layout_segment *lseg;
329         lo->plh_return_iomode = 0;
330         lo->plh_return_seq = 0;
331         clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
332         list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
333                 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
334                         continue;
335                 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
336         }
337 }
338
339 static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
340 {
341         clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
342         clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
343         smp_mb__after_atomic();
344         wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
345         rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
346 }
347
348 static void
349 pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg,
350                 struct list_head *free_me)
351 {
352         clear_bit(NFS_LSEG_ROC, &lseg->pls_flags);
353         clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
354         if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags))
355                 pnfs_lseg_dec_and_remove_zero(lseg, free_me);
356         if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
357                 pnfs_lseg_dec_and_remove_zero(lseg, free_me);
358 }
359
360 /*
361  * Mark a pnfs_layout_hdr and all associated layout segments as invalid
362  *
363  * In order to continue using the pnfs_layout_hdr, a full recovery
364  * is required.
365  * Note that caller must hold inode->i_lock.
366  */
367 int
368 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
369                 struct list_head *lseg_list)
370 {
371         struct pnfs_layout_range range = {
372                 .iomode = IOMODE_ANY,
373                 .offset = 0,
374                 .length = NFS4_MAX_UINT64,
375         };
376         struct pnfs_layout_segment *lseg, *next;
377
378         set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
379         list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
380                 pnfs_clear_lseg_state(lseg, lseg_list);
381         pnfs_clear_layoutreturn_info(lo);
382         pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
383         if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
384             !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
385                 pnfs_clear_layoutreturn_waitbit(lo);
386         return !list_empty(&lo->plh_segs);
387 }
388
389 static int
390 pnfs_iomode_to_fail_bit(u32 iomode)
391 {
392         return iomode == IOMODE_RW ?
393                 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
394 }
395
396 static void
397 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
398 {
399         lo->plh_retry_timestamp = jiffies;
400         if (!test_and_set_bit(fail_bit, &lo->plh_flags))
401                 atomic_inc(&lo->plh_refcount);
402 }
403
404 static void
405 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
406 {
407         if (test_and_clear_bit(fail_bit, &lo->plh_flags))
408                 atomic_dec(&lo->plh_refcount);
409 }
410
411 static void
412 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
413 {
414         struct inode *inode = lo->plh_inode;
415         struct pnfs_layout_range range = {
416                 .iomode = iomode,
417                 .offset = 0,
418                 .length = NFS4_MAX_UINT64,
419         };
420         LIST_HEAD(head);
421
422         spin_lock(&inode->i_lock);
423         pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
424         pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
425         spin_unlock(&inode->i_lock);
426         pnfs_free_lseg_list(&head);
427         dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
428                         iomode == IOMODE_RW ?  "RW" : "READ");
429 }
430
431 static bool
432 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
433 {
434         unsigned long start, end;
435         int fail_bit = pnfs_iomode_to_fail_bit(iomode);
436
437         if (test_bit(fail_bit, &lo->plh_flags) == 0)
438                 return false;
439         end = jiffies;
440         start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
441         if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
442                 /* It is time to retry the failed layoutgets */
443                 pnfs_layout_clear_fail_bit(lo, fail_bit);
444                 return false;
445         }
446         return true;
447 }
448
449 static void
450 pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
451                 const struct pnfs_layout_range *range,
452                 const nfs4_stateid *stateid)
453 {
454         INIT_LIST_HEAD(&lseg->pls_list);
455         INIT_LIST_HEAD(&lseg->pls_lc_list);
456         atomic_set(&lseg->pls_refcount, 1);
457         set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
458         lseg->pls_layout = lo;
459         lseg->pls_range = *range;
460         lseg->pls_seq = be32_to_cpu(stateid->seqid);
461 }
462
463 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
464 {
465         if (lseg != NULL) {
466                 struct inode *inode = lseg->pls_layout->plh_inode;
467                 NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
468         }
469 }
470
471 static void
472 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
473                 struct pnfs_layout_segment *lseg)
474 {
475         WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
476         list_del_init(&lseg->pls_list);
477         /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
478         atomic_dec(&lo->plh_refcount);
479         if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
480                 return;
481         if (list_empty(&lo->plh_segs) &&
482             !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
483             !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
484                 if (atomic_read(&lo->plh_outstanding) == 0)
485                         set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
486                 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
487         }
488 }
489
490 static bool
491 pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
492                 struct pnfs_layout_segment *lseg)
493 {
494         if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
495             pnfs_layout_is_valid(lo)) {
496                 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
497                 list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
498                 return true;
499         }
500         return false;
501 }
502
503 void
504 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
505 {
506         struct pnfs_layout_hdr *lo;
507         struct inode *inode;
508
509         if (!lseg)
510                 return;
511
512         dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
513                 atomic_read(&lseg->pls_refcount),
514                 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
515
516         lo = lseg->pls_layout;
517         inode = lo->plh_inode;
518
519         if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
520                 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
521                         spin_unlock(&inode->i_lock);
522                         return;
523                 }
524                 pnfs_get_layout_hdr(lo);
525                 pnfs_layout_remove_lseg(lo, lseg);
526                 if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
527                         lseg = NULL;
528                 spin_unlock(&inode->i_lock);
529                 pnfs_free_lseg(lseg);
530                 pnfs_put_layout_hdr(lo);
531         }
532 }
533 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
534
535 /*
536  * is l2 fully contained in l1?
537  *   start1                             end1
538  *   [----------------------------------)
539  *           start2           end2
540  *           [----------------)
541  */
542 static bool
543 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
544                  const struct pnfs_layout_range *l2)
545 {
546         u64 start1 = l1->offset;
547         u64 end1 = pnfs_end_offset(start1, l1->length);
548         u64 start2 = l2->offset;
549         u64 end2 = pnfs_end_offset(start2, l2->length);
550
551         return (start1 <= start2) && (end1 >= end2);
552 }
553
554 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
555                 struct list_head *tmp_list)
556 {
557         if (!atomic_dec_and_test(&lseg->pls_refcount))
558                 return false;
559         pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
560         list_add(&lseg->pls_list, tmp_list);
561         return true;
562 }
563
564 /* Returns 1 if lseg is removed from list, 0 otherwise */
565 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
566                              struct list_head *tmp_list)
567 {
568         int rv = 0;
569
570         if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
571                 /* Remove the reference keeping the lseg in the
572                  * list.  It will now be removed when all
573                  * outstanding io is finished.
574                  */
575                 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
576                         atomic_read(&lseg->pls_refcount));
577                 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
578                         rv = 1;
579         }
580         return rv;
581 }
582
583 /*
584  * Compare 2 layout stateid sequence ids, to see which is newer,
585  * taking into account wraparound issues.
586  */
587 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
588 {
589         return (s32)(s1 - s2) > 0;
590 }
591
592 static bool
593 pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
594                  const struct pnfs_layout_range *recall_range)
595 {
596         return (recall_range->iomode == IOMODE_ANY ||
597                 lseg_range->iomode == recall_range->iomode) &&
598                pnfs_lseg_range_intersecting(lseg_range, recall_range);
599 }
600
601 static bool
602 pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
603                 const struct pnfs_layout_range *recall_range,
604                 u32 seq)
605 {
606         if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
607                 return false;
608         if (recall_range == NULL)
609                 return true;
610         return pnfs_should_free_range(&lseg->pls_range, recall_range);
611 }
612
613 /**
614  * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
615  * @lo: layout header containing the lsegs
616  * @tmp_list: list head where doomed lsegs should go
617  * @recall_range: optional recall range argument to match (may be NULL)
618  * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
619  *
620  * Walk the list of lsegs in the layout header, and tear down any that should
621  * be destroyed. If "recall_range" is specified then the segment must match
622  * that range. If "seq" is non-zero, then only match segments that were handed
623  * out at or before that sequence.
624  *
625  * Returns number of matching invalid lsegs remaining in list after scanning
626  * it and purging them.
627  */
628 int
629 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
630                             struct list_head *tmp_list,
631                             const struct pnfs_layout_range *recall_range,
632                             u32 seq)
633 {
634         struct pnfs_layout_segment *lseg, *next;
635         int remaining = 0;
636
637         dprintk("%s:Begin lo %p\n", __func__, lo);
638
639         if (list_empty(&lo->plh_segs))
640                 return 0;
641         list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
642                 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
643                         dprintk("%s: freeing lseg %p iomode %d seq %u"
644                                 "offset %llu length %llu\n", __func__,
645                                 lseg, lseg->pls_range.iomode, lseg->pls_seq,
646                                 lseg->pls_range.offset, lseg->pls_range.length);
647                         if (!mark_lseg_invalid(lseg, tmp_list))
648                                 remaining++;
649                 }
650         dprintk("%s:Return %i\n", __func__, remaining);
651         return remaining;
652 }
653
654 static void
655 pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
656                 struct list_head *free_me,
657                 const struct pnfs_layout_range *range,
658                 u32 seq)
659 {
660         struct pnfs_layout_segment *lseg, *next;
661
662         list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
663                 if (pnfs_match_lseg_recall(lseg, range, seq))
664                         list_move_tail(&lseg->pls_list, free_me);
665         }
666 }
667
668 /* note free_me must contain lsegs from a single layout_hdr */
669 void
670 pnfs_free_lseg_list(struct list_head *free_me)
671 {
672         struct pnfs_layout_segment *lseg, *tmp;
673
674         if (list_empty(free_me))
675                 return;
676
677         list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
678                 list_del(&lseg->pls_list);
679                 pnfs_free_lseg(lseg);
680         }
681 }
682
683 void
684 pnfs_destroy_layout(struct nfs_inode *nfsi)
685 {
686         struct pnfs_layout_hdr *lo;
687         LIST_HEAD(tmp_list);
688
689         spin_lock(&nfsi->vfs_inode.i_lock);
690         lo = nfsi->layout;
691         if (lo) {
692                 pnfs_get_layout_hdr(lo);
693                 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
694                 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
695                 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
696                 spin_unlock(&nfsi->vfs_inode.i_lock);
697                 pnfs_free_lseg_list(&tmp_list);
698                 nfs_commit_inode(&nfsi->vfs_inode, 0);
699                 pnfs_put_layout_hdr(lo);
700         } else
701                 spin_unlock(&nfsi->vfs_inode.i_lock);
702 }
703 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
704
705 static bool
706 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
707                 struct list_head *layout_list)
708 {
709         struct pnfs_layout_hdr *lo;
710         bool ret = false;
711
712         spin_lock(&inode->i_lock);
713         lo = NFS_I(inode)->layout;
714         if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
715                 pnfs_get_layout_hdr(lo);
716                 list_add(&lo->plh_bulk_destroy, layout_list);
717                 ret = true;
718         }
719         spin_unlock(&inode->i_lock);
720         return ret;
721 }
722
723 /* Caller must hold rcu_read_lock and clp->cl_lock */
724 static int
725 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
726                 struct nfs_server *server,
727                 struct list_head *layout_list)
728         __must_hold(&clp->cl_lock)
729         __must_hold(RCU)
730 {
731         struct pnfs_layout_hdr *lo, *next;
732         struct inode *inode;
733
734         list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
735                 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
736                     test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) ||
737                     !list_empty(&lo->plh_bulk_destroy))
738                         continue;
739                 /* If the sb is being destroyed, just bail */
740                 if (!nfs_sb_active(server->super))
741                         break;
742                 inode = igrab(lo->plh_inode);
743                 if (inode != NULL) {
744                         list_del_init(&lo->plh_layouts);
745                         if (pnfs_layout_add_bulk_destroy_list(inode,
746                                                 layout_list))
747                                 continue;
748                         rcu_read_unlock();
749                         spin_unlock(&clp->cl_lock);
750                         iput(inode);
751                 } else {
752                         rcu_read_unlock();
753                         spin_unlock(&clp->cl_lock);
754                         set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags);
755                 }
756                 nfs_sb_deactive(server->super);
757                 spin_lock(&clp->cl_lock);
758                 rcu_read_lock();
759                 return -EAGAIN;
760         }
761         return 0;
762 }
763
764 static int
765 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
766                 bool is_bulk_recall)
767 {
768         struct pnfs_layout_hdr *lo;
769         struct inode *inode;
770         LIST_HEAD(lseg_list);
771         int ret = 0;
772
773         while (!list_empty(layout_list)) {
774                 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
775                                 plh_bulk_destroy);
776                 dprintk("%s freeing layout for inode %lu\n", __func__,
777                         lo->plh_inode->i_ino);
778                 inode = lo->plh_inode;
779
780                 pnfs_layoutcommit_inode(inode, false);
781
782                 spin_lock(&inode->i_lock);
783                 list_del_init(&lo->plh_bulk_destroy);
784                 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
785                         if (is_bulk_recall)
786                                 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
787                         ret = -EAGAIN;
788                 }
789                 spin_unlock(&inode->i_lock);
790                 pnfs_free_lseg_list(&lseg_list);
791                 /* Free all lsegs that are attached to commit buckets */
792                 nfs_commit_inode(inode, 0);
793                 pnfs_put_layout_hdr(lo);
794                 nfs_iput_and_deactive(inode);
795         }
796         return ret;
797 }
798
799 int
800 pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
801                 struct nfs_fsid *fsid,
802                 bool is_recall)
803 {
804         struct nfs_server *server;
805         LIST_HEAD(layout_list);
806
807         spin_lock(&clp->cl_lock);
808         rcu_read_lock();
809 restart:
810         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
811                 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
812                         continue;
813                 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
814                                 server,
815                                 &layout_list) != 0)
816                         goto restart;
817         }
818         rcu_read_unlock();
819         spin_unlock(&clp->cl_lock);
820
821         if (list_empty(&layout_list))
822                 return 0;
823         return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
824 }
825
826 int
827 pnfs_destroy_layouts_byclid(struct nfs_client *clp,
828                 bool is_recall)
829 {
830         struct nfs_server *server;
831         LIST_HEAD(layout_list);
832
833         spin_lock(&clp->cl_lock);
834         rcu_read_lock();
835 restart:
836         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
837                 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
838                                         server,
839                                         &layout_list) != 0)
840                         goto restart;
841         }
842         rcu_read_unlock();
843         spin_unlock(&clp->cl_lock);
844
845         if (list_empty(&layout_list))
846                 return 0;
847         return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
848 }
849
850 /*
851  * Called by the state manger to remove all layouts established under an
852  * expired lease.
853  */
854 void
855 pnfs_destroy_all_layouts(struct nfs_client *clp)
856 {
857         nfs4_deviceid_mark_client_invalid(clp);
858         nfs4_deviceid_purge_client(clp);
859
860         pnfs_destroy_layouts_byclid(clp, false);
861 }
862
863 /* update lo->plh_stateid with new if is more recent */
864 void
865 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
866                         bool update_barrier)
867 {
868         u32 oldseq, newseq, new_barrier = 0;
869
870         oldseq = be32_to_cpu(lo->plh_stateid.seqid);
871         newseq = be32_to_cpu(new->seqid);
872
873         if (!pnfs_layout_is_valid(lo)) {
874                 nfs4_stateid_copy(&lo->plh_stateid, new);
875                 lo->plh_barrier = newseq;
876                 pnfs_clear_layoutreturn_info(lo);
877                 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
878                 return;
879         }
880         if (pnfs_seqid_is_newer(newseq, oldseq)) {
881                 nfs4_stateid_copy(&lo->plh_stateid, new);
882                 /*
883                  * Because of wraparound, we want to keep the barrier
884                  * "close" to the current seqids.
885                  */
886                 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
887         }
888         if (update_barrier)
889                 new_barrier = be32_to_cpu(new->seqid);
890         else if (new_barrier == 0)
891                 return;
892         if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
893                 lo->plh_barrier = new_barrier;
894 }
895
896 static bool
897 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
898                 const nfs4_stateid *stateid)
899 {
900         u32 seqid = be32_to_cpu(stateid->seqid);
901
902         return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
903 }
904
905 /* lget is set to 1 if called from inside send_layoutget call chain */
906 static bool
907 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
908 {
909         return lo->plh_block_lgets ||
910                 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
911 }
912
913 /*
914  * Get layout from server.
915  *    for now, assume that whole file layouts are requested.
916  *    arg->offset: 0
917  *    arg->length: all ones
918  */
919 static struct pnfs_layout_segment *
920 send_layoutget(struct pnfs_layout_hdr *lo,
921            struct nfs_open_context *ctx,
922            nfs4_stateid *stateid,
923            const struct pnfs_layout_range *range,
924            long *timeout, gfp_t gfp_flags)
925 {
926         struct inode *ino = lo->plh_inode;
927         struct nfs_server *server = NFS_SERVER(ino);
928         struct nfs4_layoutget *lgp;
929         loff_t i_size;
930
931         dprintk("--> %s\n", __func__);
932
933         /*
934          * Synchronously retrieve layout information from server and
935          * store in lseg. If we race with a concurrent seqid morphing
936          * op, then re-send the LAYOUTGET.
937          */
938         lgp = kzalloc(sizeof(*lgp), gfp_flags);
939         if (lgp == NULL)
940                 return ERR_PTR(-ENOMEM);
941
942         i_size = i_size_read(ino);
943
944         lgp->args.minlength = PAGE_SIZE;
945         if (lgp->args.minlength > range->length)
946                 lgp->args.minlength = range->length;
947         if (range->iomode == IOMODE_READ) {
948                 if (range->offset >= i_size)
949                         lgp->args.minlength = 0;
950                 else if (i_size - range->offset < lgp->args.minlength)
951                         lgp->args.minlength = i_size - range->offset;
952         }
953         lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
954         pnfs_copy_range(&lgp->args.range, range);
955         lgp->args.type = server->pnfs_curr_ld->id;
956         lgp->args.inode = ino;
957         lgp->args.ctx = get_nfs_open_context(ctx);
958         nfs4_stateid_copy(&lgp->args.stateid, stateid);
959         lgp->gfp_flags = gfp_flags;
960         lgp->cred = lo->plh_lc_cred;
961
962         return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
963 }
964
965 static void pnfs_clear_layoutcommit(struct inode *inode,
966                 struct list_head *head)
967 {
968         struct nfs_inode *nfsi = NFS_I(inode);
969         struct pnfs_layout_segment *lseg, *tmp;
970
971         if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
972                 return;
973         list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
974                 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
975                         continue;
976                 pnfs_lseg_dec_and_remove_zero(lseg, head);
977         }
978 }
979
980 void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
981                 const nfs4_stateid *arg_stateid,
982                 const struct pnfs_layout_range *range,
983                 const nfs4_stateid *stateid)
984 {
985         struct inode *inode = lo->plh_inode;
986         LIST_HEAD(freeme);
987
988         spin_lock(&inode->i_lock);
989         if (!pnfs_layout_is_valid(lo) || !arg_stateid ||
990             !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
991                 goto out_unlock;
992         if (stateid) {
993                 u32 seq = be32_to_cpu(arg_stateid->seqid);
994
995                 pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
996                 pnfs_free_returned_lsegs(lo, &freeme, range, seq);
997                 pnfs_set_layout_stateid(lo, stateid, true);
998         } else
999                 pnfs_mark_layout_stateid_invalid(lo, &freeme);
1000 out_unlock:
1001         pnfs_clear_layoutreturn_waitbit(lo);
1002         spin_unlock(&inode->i_lock);
1003         pnfs_free_lseg_list(&freeme);
1004
1005 }
1006
1007 static bool
1008 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
1009                 nfs4_stateid *stateid,
1010                 enum pnfs_iomode *iomode)
1011 {
1012         /* Serialise LAYOUTGET/LAYOUTRETURN */
1013         if (atomic_read(&lo->plh_outstanding) != 0)
1014                 return false;
1015         if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
1016                 return false;
1017         set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1018         pnfs_get_layout_hdr(lo);
1019         if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1020                 if (stateid != NULL) {
1021                         nfs4_stateid_copy(stateid, &lo->plh_stateid);
1022                         if (lo->plh_return_seq != 0)
1023                                 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1024                 }
1025                 if (iomode != NULL)
1026                         *iomode = lo->plh_return_iomode;
1027                 pnfs_clear_layoutreturn_info(lo);
1028                 return true;
1029         }
1030         if (stateid != NULL)
1031                 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1032         if (iomode != NULL)
1033                 *iomode = IOMODE_ANY;
1034         return true;
1035 }
1036
1037 static void
1038 pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1039                 struct pnfs_layout_hdr *lo,
1040                 const nfs4_stateid *stateid,
1041                 enum pnfs_iomode iomode)
1042 {
1043         struct inode *inode = lo->plh_inode;
1044
1045         args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1046         args->inode = inode;
1047         args->range.iomode = iomode;
1048         args->range.offset = 0;
1049         args->range.length = NFS4_MAX_UINT64;
1050         args->layout = lo;
1051         nfs4_stateid_copy(&args->stateid, stateid);
1052 }
1053
1054 static int
1055 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
1056                        enum pnfs_iomode iomode, bool sync)
1057 {
1058         struct inode *ino = lo->plh_inode;
1059         struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1060         struct nfs4_layoutreturn *lrp;
1061         int status = 0;
1062
1063         lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
1064         if (unlikely(lrp == NULL)) {
1065                 status = -ENOMEM;
1066                 spin_lock(&ino->i_lock);
1067                 pnfs_clear_layoutreturn_waitbit(lo);
1068                 spin_unlock(&ino->i_lock);
1069                 pnfs_put_layout_hdr(lo);
1070                 goto out;
1071         }
1072
1073         pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
1074         lrp->args.ld_private = &lrp->ld_private;
1075         lrp->clp = NFS_SERVER(ino)->nfs_client;
1076         lrp->cred = lo->plh_lc_cred;
1077         if (ld->prepare_layoutreturn)
1078                 ld->prepare_layoutreturn(&lrp->args);
1079
1080         status = nfs4_proc_layoutreturn(lrp, sync);
1081 out:
1082         dprintk("<-- %s status: %d\n", __func__, status);
1083         return status;
1084 }
1085
1086 /* Return true if layoutreturn is needed */
1087 static bool
1088 pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1089 {
1090         struct pnfs_layout_segment *s;
1091
1092         if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1093                 return false;
1094
1095         /* Defer layoutreturn until all lsegs are done */
1096         list_for_each_entry(s, &lo->plh_segs, pls_list) {
1097                 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1098                         return false;
1099         }
1100
1101         return true;
1102 }
1103
1104 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1105 {
1106         struct inode *inode= lo->plh_inode;
1107
1108         if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1109                 return;
1110         spin_lock(&inode->i_lock);
1111         if (pnfs_layout_need_return(lo)) {
1112                 nfs4_stateid stateid;
1113                 enum pnfs_iomode iomode;
1114                 bool send;
1115
1116                 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1117                 spin_unlock(&inode->i_lock);
1118                 if (send) {
1119                         /* Send an async layoutreturn so we dont deadlock */
1120                         pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1121                 }
1122         } else
1123                 spin_unlock(&inode->i_lock);
1124 }
1125
1126 /*
1127  * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1128  * when the layout segment list is empty.
1129  *
1130  * Note that a pnfs_layout_hdr can exist with an empty layout segment
1131  * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1132  * deviceid is marked invalid.
1133  */
1134 int
1135 _pnfs_return_layout(struct inode *ino)
1136 {
1137         struct pnfs_layout_hdr *lo = NULL;
1138         struct nfs_inode *nfsi = NFS_I(ino);
1139         struct pnfs_layout_range range = {
1140                 .iomode         = IOMODE_ANY,
1141                 .offset         = 0,
1142                 .length         = NFS4_MAX_UINT64,
1143         };
1144         LIST_HEAD(tmp_list);
1145         nfs4_stateid stateid;
1146         int status = 0;
1147         bool send, valid_layout;
1148
1149         dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1150
1151         spin_lock(&ino->i_lock);
1152         lo = nfsi->layout;
1153         if (!lo) {
1154                 spin_unlock(&ino->i_lock);
1155                 dprintk("NFS: %s no layout to return\n", __func__);
1156                 goto out;
1157         }
1158         /* Reference matched in nfs4_layoutreturn_release */
1159         pnfs_get_layout_hdr(lo);
1160         /* Is there an outstanding layoutreturn ? */
1161         if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1162                 spin_unlock(&ino->i_lock);
1163                 if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1164                                         TASK_UNINTERRUPTIBLE))
1165                         goto out_put_layout_hdr;
1166                 spin_lock(&ino->i_lock);
1167         }
1168         valid_layout = pnfs_layout_is_valid(lo);
1169         pnfs_clear_layoutcommit(ino, &tmp_list);
1170         pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
1171
1172         if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
1173                 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1174
1175         /* Don't send a LAYOUTRETURN if list was initially empty */
1176         if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
1177                         !valid_layout) {
1178                 spin_unlock(&ino->i_lock);
1179                 dprintk("NFS: %s no layout segments to return\n", __func__);
1180                 goto out_put_layout_hdr;
1181         }
1182
1183         send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
1184         spin_unlock(&ino->i_lock);
1185         if (send)
1186                 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1187 out_put_layout_hdr:
1188         pnfs_free_lseg_list(&tmp_list);
1189         pnfs_put_layout_hdr(lo);
1190 out:
1191         dprintk("<-- %s status: %d\n", __func__, status);
1192         return status;
1193 }
1194
1195 int
1196 pnfs_commit_and_return_layout(struct inode *inode)
1197 {
1198         struct pnfs_layout_hdr *lo;
1199         int ret;
1200
1201         spin_lock(&inode->i_lock);
1202         lo = NFS_I(inode)->layout;
1203         if (lo == NULL) {
1204                 spin_unlock(&inode->i_lock);
1205                 return 0;
1206         }
1207         pnfs_get_layout_hdr(lo);
1208         /* Block new layoutgets and read/write to ds */
1209         lo->plh_block_lgets++;
1210         spin_unlock(&inode->i_lock);
1211         filemap_fdatawait(inode->i_mapping);
1212         ret = pnfs_layoutcommit_inode(inode, true);
1213         if (ret == 0)
1214                 ret = _pnfs_return_layout(inode);
1215         spin_lock(&inode->i_lock);
1216         lo->plh_block_lgets--;
1217         spin_unlock(&inode->i_lock);
1218         pnfs_put_layout_hdr(lo);
1219         return ret;
1220 }
1221
1222 bool pnfs_roc(struct inode *ino,
1223                 struct nfs4_layoutreturn_args *args,
1224                 struct nfs4_layoutreturn_res *res,
1225                 const struct rpc_cred *cred)
1226 {
1227         struct nfs_inode *nfsi = NFS_I(ino);
1228         struct nfs_open_context *ctx;
1229         struct nfs4_state *state;
1230         struct pnfs_layout_hdr *lo;
1231         struct pnfs_layout_segment *lseg, *next;
1232         nfs4_stateid stateid;
1233         enum pnfs_iomode iomode = 0;
1234         bool layoutreturn = false, roc = false;
1235         bool skip_read = false;
1236
1237         if (!nfs_have_layout(ino))
1238                 return false;
1239 retry:
1240         spin_lock(&ino->i_lock);
1241         lo = nfsi->layout;
1242         if (!lo || !pnfs_layout_is_valid(lo) ||
1243             test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1244                 lo = NULL;
1245                 goto out_noroc;
1246         }
1247         pnfs_get_layout_hdr(lo);
1248         if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1249                 spin_unlock(&ino->i_lock);
1250                 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1251                                 TASK_UNINTERRUPTIBLE);
1252                 pnfs_put_layout_hdr(lo);
1253                 goto retry;
1254         }
1255
1256         /* no roc if we hold a delegation */
1257         if (nfs4_check_delegation(ino, FMODE_READ)) {
1258                 if (nfs4_check_delegation(ino, FMODE_WRITE))
1259                         goto out_noroc;
1260                 skip_read = true;
1261         }
1262
1263         list_for_each_entry(ctx, &nfsi->open_files, list) {
1264                 state = ctx->state;
1265                 if (state == NULL)
1266                         continue;
1267                 /* Don't return layout if there is open file state */
1268                 if (state->state & FMODE_WRITE)
1269                         goto out_noroc;
1270                 if (state->state & FMODE_READ)
1271                         skip_read = true;
1272         }
1273
1274
1275         list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
1276                 if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
1277                         continue;
1278                 /* If we are sending layoutreturn, invalidate all valid lsegs */
1279                 if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
1280                         continue;
1281                 /*
1282                  * Note: mark lseg for return so pnfs_layout_remove_lseg
1283                  * doesn't invalidate the layout for us.
1284                  */
1285                 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1286                 if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
1287                         continue;
1288                 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
1289         }
1290
1291         if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1292                 goto out_noroc;
1293
1294         /* ROC in two conditions:
1295          * 1. there are ROC lsegs
1296          * 2. we don't send layoutreturn
1297          */
1298         /* lo ref dropped in pnfs_roc_release() */
1299         layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1300         /* If the creds don't match, we can't compound the layoutreturn */
1301         if (!layoutreturn || cred != lo->plh_lc_cred)
1302                 goto out_noroc;
1303
1304         roc = layoutreturn;
1305         pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
1306         res->lrs_present = 0;
1307         layoutreturn = false;
1308
1309 out_noroc:
1310         spin_unlock(&ino->i_lock);
1311         pnfs_layoutcommit_inode(ino, true);
1312         if (roc) {
1313                 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1314                 if (ld->prepare_layoutreturn)
1315                         ld->prepare_layoutreturn(args);
1316                 pnfs_put_layout_hdr(lo);
1317                 return true;
1318         }
1319         if (layoutreturn)
1320                 pnfs_send_layoutreturn(lo, &stateid, iomode, true);
1321         pnfs_put_layout_hdr(lo);
1322         return false;
1323 }
1324
1325 void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
1326                 struct nfs4_layoutreturn_res *res,
1327                 int ret)
1328 {
1329         struct pnfs_layout_hdr *lo = args->layout;
1330         struct inode *inode = args->inode;
1331         const nfs4_stateid *arg_stateid = NULL;
1332         const nfs4_stateid *res_stateid = NULL;
1333         struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
1334
1335         switch (ret) {
1336         case -NFS4ERR_NOMATCHING_LAYOUT:
1337                 spin_lock(&inode->i_lock);
1338                 if (pnfs_layout_is_valid(lo) &&
1339                     nfs4_stateid_match_other(&args->stateid, &lo->plh_stateid))
1340                         pnfs_set_plh_return_info(lo, args->range.iomode, 0);
1341                 spin_unlock(&inode->i_lock);
1342                 break;
1343         case 0:
1344                 if (res->lrs_present)
1345                         res_stateid = &res->stateid;
1346                 /* Fallthrough */
1347         default:
1348                 arg_stateid = &args->stateid;
1349         }
1350         pnfs_layoutreturn_free_lsegs(lo, arg_stateid, &args->range,
1351                         res_stateid);
1352         if (ld_private && ld_private->ops && ld_private->ops->free)
1353                 ld_private->ops->free(ld_private);
1354         pnfs_put_layout_hdr(lo);
1355         trace_nfs4_layoutreturn_on_close(args->inode, 0);
1356 }
1357
1358 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1359 {
1360         struct nfs_inode *nfsi = NFS_I(ino);
1361         struct pnfs_layout_hdr *lo;
1362         bool sleep = false;
1363
1364         /* we might not have grabbed lo reference. so need to check under
1365          * i_lock */
1366         spin_lock(&ino->i_lock);
1367         lo = nfsi->layout;
1368         if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1369                 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1370                 sleep = true;
1371         }
1372         spin_unlock(&ino->i_lock);
1373         return sleep;
1374 }
1375
1376 /*
1377  * Compare two layout segments for sorting into layout cache.
1378  * We want to preferentially return RW over RO layouts, so ensure those
1379  * are seen first.
1380  */
1381 static s64
1382 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1383            const struct pnfs_layout_range *l2)
1384 {
1385         s64 d;
1386
1387         /* high offset > low offset */
1388         d = l1->offset - l2->offset;
1389         if (d)
1390                 return d;
1391
1392         /* short length > long length */
1393         d = l2->length - l1->length;
1394         if (d)
1395                 return d;
1396
1397         /* read > read/write */
1398         return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1399 }
1400
1401 static bool
1402 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1403                 const struct pnfs_layout_range *l2)
1404 {
1405         return pnfs_lseg_range_cmp(l1, l2) > 0;
1406 }
1407
1408 static bool
1409 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1410                 struct pnfs_layout_segment *old)
1411 {
1412         return false;
1413 }
1414
1415 void
1416 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1417                    struct pnfs_layout_segment *lseg,
1418                    bool (*is_after)(const struct pnfs_layout_range *,
1419                            const struct pnfs_layout_range *),
1420                    bool (*do_merge)(struct pnfs_layout_segment *,
1421                            struct pnfs_layout_segment *),
1422                    struct list_head *free_me)
1423 {
1424         struct pnfs_layout_segment *lp, *tmp;
1425
1426         dprintk("%s:Begin\n", __func__);
1427
1428         list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1429                 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1430                         continue;
1431                 if (do_merge(lseg, lp)) {
1432                         mark_lseg_invalid(lp, free_me);
1433                         continue;
1434                 }
1435                 if (is_after(&lseg->pls_range, &lp->pls_range))
1436                         continue;
1437                 list_add_tail(&lseg->pls_list, &lp->pls_list);
1438                 dprintk("%s: inserted lseg %p "
1439                         "iomode %d offset %llu length %llu before "
1440                         "lp %p iomode %d offset %llu length %llu\n",
1441                         __func__, lseg, lseg->pls_range.iomode,
1442                         lseg->pls_range.offset, lseg->pls_range.length,
1443                         lp, lp->pls_range.iomode, lp->pls_range.offset,
1444                         lp->pls_range.length);
1445                 goto out;
1446         }
1447         list_add_tail(&lseg->pls_list, &lo->plh_segs);
1448         dprintk("%s: inserted lseg %p "
1449                 "iomode %d offset %llu length %llu at tail\n",
1450                 __func__, lseg, lseg->pls_range.iomode,
1451                 lseg->pls_range.offset, lseg->pls_range.length);
1452 out:
1453         pnfs_get_layout_hdr(lo);
1454
1455         dprintk("%s:Return\n", __func__);
1456 }
1457 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1458
1459 static void
1460 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1461                    struct pnfs_layout_segment *lseg,
1462                    struct list_head *free_me)
1463 {
1464         struct inode *inode = lo->plh_inode;
1465         struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1466
1467         if (ld->add_lseg != NULL)
1468                 ld->add_lseg(lo, lseg, free_me);
1469         else
1470                 pnfs_generic_layout_insert_lseg(lo, lseg,
1471                                 pnfs_lseg_range_is_after,
1472                                 pnfs_lseg_no_merge,
1473                                 free_me);
1474 }
1475
1476 static struct pnfs_layout_hdr *
1477 alloc_init_layout_hdr(struct inode *ino,
1478                       struct nfs_open_context *ctx,
1479                       gfp_t gfp_flags)
1480 {
1481         struct pnfs_layout_hdr *lo;
1482
1483         lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1484         if (!lo)
1485                 return NULL;
1486         atomic_set(&lo->plh_refcount, 1);
1487         INIT_LIST_HEAD(&lo->plh_layouts);
1488         INIT_LIST_HEAD(&lo->plh_segs);
1489         INIT_LIST_HEAD(&lo->plh_return_segs);
1490         INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1491         lo->plh_inode = ino;
1492         lo->plh_lc_cred = get_rpccred(ctx->cred);
1493         lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1494         return lo;
1495 }
1496
1497 static struct pnfs_layout_hdr *
1498 pnfs_find_alloc_layout(struct inode *ino,
1499                        struct nfs_open_context *ctx,
1500                        gfp_t gfp_flags)
1501         __releases(&ino->i_lock)
1502         __acquires(&ino->i_lock)
1503 {
1504         struct nfs_inode *nfsi = NFS_I(ino);
1505         struct pnfs_layout_hdr *new = NULL;
1506
1507         dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1508
1509         if (nfsi->layout != NULL)
1510                 goto out_existing;
1511         spin_unlock(&ino->i_lock);
1512         new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1513         spin_lock(&ino->i_lock);
1514
1515         if (likely(nfsi->layout == NULL)) {     /* Won the race? */
1516                 nfsi->layout = new;
1517                 return new;
1518         } else if (new != NULL)
1519                 pnfs_free_layout_hdr(new);
1520 out_existing:
1521         pnfs_get_layout_hdr(nfsi->layout);
1522         return nfsi->layout;
1523 }
1524
1525 /*
1526  * iomode matching rules:
1527  * iomode       lseg    strict match
1528  *                      iomode
1529  * -----        -----   ------ -----
1530  * ANY          READ    N/A    true
1531  * ANY          RW      N/A    true
1532  * RW           READ    N/A    false
1533  * RW           RW      N/A    true
1534  * READ         READ    N/A    true
1535  * READ         RW      true   false
1536  * READ         RW      false  true
1537  */
1538 static bool
1539 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1540                  const struct pnfs_layout_range *range,
1541                  bool strict_iomode)
1542 {
1543         struct pnfs_layout_range range1;
1544
1545         if ((range->iomode == IOMODE_RW &&
1546              ls_range->iomode != IOMODE_RW) ||
1547             (range->iomode != ls_range->iomode &&
1548              strict_iomode == true) ||
1549             !pnfs_lseg_range_intersecting(ls_range, range))
1550                 return 0;
1551
1552         /* range1 covers only the first byte in the range */
1553         range1 = *range;
1554         range1.length = 1;
1555         return pnfs_lseg_range_contained(ls_range, &range1);
1556 }
1557
1558 /*
1559  * lookup range in layout
1560  */
1561 static struct pnfs_layout_segment *
1562 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1563                 struct pnfs_layout_range *range,
1564                 bool strict_iomode)
1565 {
1566         struct pnfs_layout_segment *lseg, *ret = NULL;
1567
1568         dprintk("%s:Begin\n", __func__);
1569
1570         list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1571                 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1572                     !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
1573                     pnfs_lseg_range_match(&lseg->pls_range, range,
1574                                           strict_iomode)) {
1575                         ret = pnfs_get_lseg(lseg);
1576                         break;
1577                 }
1578         }
1579
1580         dprintk("%s:Return lseg %p ref %d\n",
1581                 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
1582         return ret;
1583 }
1584
1585 /*
1586  * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1587  * to the MDS or over pNFS
1588  *
1589  * The nfs_inode read_io and write_io fields are cumulative counters reset
1590  * when there are no layout segments. Note that in pnfs_update_layout iomode
1591  * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1592  * WRITE request.
1593  *
1594  * A return of true means use MDS I/O.
1595  *
1596  * From rfc 5661:
1597  * If a file's size is smaller than the file size threshold, data accesses
1598  * SHOULD be sent to the metadata server.  If an I/O request has a length that
1599  * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1600  * server.  If both file size and I/O size are provided, the client SHOULD
1601  * reach or exceed  both thresholds before sending its read or write
1602  * requests to the data server.
1603  */
1604 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1605                                      struct inode *ino, int iomode)
1606 {
1607         struct nfs4_threshold *t = ctx->mdsthreshold;
1608         struct nfs_inode *nfsi = NFS_I(ino);
1609         loff_t fsize = i_size_read(ino);
1610         bool size = false, size_set = false, io = false, io_set = false, ret = false;
1611
1612         if (t == NULL)
1613                 return ret;
1614
1615         dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1616                 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1617
1618         switch (iomode) {
1619         case IOMODE_READ:
1620                 if (t->bm & THRESHOLD_RD) {
1621                         dprintk("%s fsize %llu\n", __func__, fsize);
1622                         size_set = true;
1623                         if (fsize < t->rd_sz)
1624                                 size = true;
1625                 }
1626                 if (t->bm & THRESHOLD_RD_IO) {
1627                         dprintk("%s nfsi->read_io %llu\n", __func__,
1628                                 nfsi->read_io);
1629                         io_set = true;
1630                         if (nfsi->read_io < t->rd_io_sz)
1631                                 io = true;
1632                 }
1633                 break;
1634         case IOMODE_RW:
1635                 if (t->bm & THRESHOLD_WR) {
1636                         dprintk("%s fsize %llu\n", __func__, fsize);
1637                         size_set = true;
1638                         if (fsize < t->wr_sz)
1639                                 size = true;
1640                 }
1641                 if (t->bm & THRESHOLD_WR_IO) {
1642                         dprintk("%s nfsi->write_io %llu\n", __func__,
1643                                 nfsi->write_io);
1644                         io_set = true;
1645                         if (nfsi->write_io < t->wr_io_sz)
1646                                 io = true;
1647                 }
1648                 break;
1649         }
1650         if (size_set && io_set) {
1651                 if (size && io)
1652                         ret = true;
1653         } else if (size || io)
1654                 ret = true;
1655
1656         dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1657         return ret;
1658 }
1659
1660 static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1661 {
1662         /*
1663          * send layoutcommit as it can hold up layoutreturn due to lseg
1664          * reference
1665          */
1666         pnfs_layoutcommit_inode(lo->plh_inode, false);
1667         return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1668                                    nfs_wait_bit_killable,
1669                                    TASK_UNINTERRUPTIBLE);
1670 }
1671
1672 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1673 {
1674         unsigned long *bitlock = &lo->plh_flags;
1675
1676         clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1677         smp_mb__after_atomic();
1678         wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1679 }
1680
1681 /*
1682  * Layout segment is retreived from the server if not cached.
1683  * The appropriate layout segment is referenced and returned to the caller.
1684  */
1685 struct pnfs_layout_segment *
1686 pnfs_update_layout(struct inode *ino,
1687                    struct nfs_open_context *ctx,
1688                    loff_t pos,
1689                    u64 count,
1690                    enum pnfs_iomode iomode,
1691                    bool strict_iomode,
1692                    gfp_t gfp_flags)
1693 {
1694         struct pnfs_layout_range arg = {
1695                 .iomode = iomode,
1696                 .offset = pos,
1697                 .length = count,
1698         };
1699         unsigned pg_offset;
1700         struct nfs_server *server = NFS_SERVER(ino);
1701         struct nfs_client *clp = server->nfs_client;
1702         struct pnfs_layout_hdr *lo = NULL;
1703         struct pnfs_layout_segment *lseg = NULL;
1704         nfs4_stateid stateid;
1705         long timeout = 0;
1706         unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1707         bool first;
1708
1709         if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1710                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1711                                  PNFS_UPDATE_LAYOUT_NO_PNFS);
1712                 goto out;
1713         }
1714
1715         if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
1716                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1717                                  PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
1718                 goto out;
1719         }
1720
1721         if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
1722                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1723                                  PNFS_UPDATE_LAYOUT_MDSTHRESH);
1724                 goto out;
1725         }
1726
1727 lookup_again:
1728         nfs4_client_recover_expired_lease(clp);
1729         first = false;
1730         spin_lock(&ino->i_lock);
1731         lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1732         if (lo == NULL) {
1733                 spin_unlock(&ino->i_lock);
1734                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1735                                  PNFS_UPDATE_LAYOUT_NOMEM);
1736                 goto out;
1737         }
1738
1739         /* Do we even need to bother with this? */
1740         if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1741                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1742                                  PNFS_UPDATE_LAYOUT_BULK_RECALL);
1743                 dprintk("%s matches recall, use MDS\n", __func__);
1744                 goto out_unlock;
1745         }
1746
1747         /* if LAYOUTGET already failed once we don't try again */
1748         if (pnfs_layout_io_test_failed(lo, iomode)) {
1749                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1750                                  PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
1751                 goto out_unlock;
1752         }
1753
1754         lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
1755         if (lseg) {
1756                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1757                                 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1758                 goto out_unlock;
1759         }
1760
1761         if (!nfs4_valid_open_stateid(ctx->state)) {
1762                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1763                                 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1764                 goto out_unlock;
1765         }
1766
1767         /*
1768          * Choose a stateid for the LAYOUTGET. If we don't have a layout
1769          * stateid, or it has been invalidated, then we must use the open
1770          * stateid.
1771          */
1772         if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
1773
1774                 /*
1775                  * The first layoutget for the file. Need to serialize per
1776                  * RFC 5661 Errata 3208.
1777                  */
1778                 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1779                                      &lo->plh_flags)) {
1780                         spin_unlock(&ino->i_lock);
1781                         wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1782                                     TASK_UNINTERRUPTIBLE);
1783                         pnfs_put_layout_hdr(lo);
1784                         dprintk("%s retrying\n", __func__);
1785                         goto lookup_again;
1786                 }
1787
1788                 first = true;
1789                 if (nfs4_select_rw_stateid(ctx->state,
1790                                         iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
1791                                         NULL, &stateid, NULL) != 0) {
1792                         trace_pnfs_update_layout(ino, pos, count,
1793                                         iomode, lo, lseg,
1794                                         PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1795                         goto out_unlock;
1796                 }
1797         } else {
1798                 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
1799         }
1800
1801         /*
1802          * Because we free lsegs before sending LAYOUTRETURN, we need to wait
1803          * for LAYOUTRETURN even if first is true.
1804          */
1805         if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1806                 spin_unlock(&ino->i_lock);
1807                 dprintk("%s wait for layoutreturn\n", __func__);
1808                 if (pnfs_prepare_to_retry_layoutget(lo)) {
1809                         if (first)
1810                                 pnfs_clear_first_layoutget(lo);
1811                         pnfs_put_layout_hdr(lo);
1812                         dprintk("%s retrying\n", __func__);
1813                         trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1814                                         lseg, PNFS_UPDATE_LAYOUT_RETRY);
1815                         goto lookup_again;
1816                 }
1817                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1818                                 PNFS_UPDATE_LAYOUT_RETURN);
1819                 goto out_put_layout_hdr;
1820         }
1821
1822         if (pnfs_layoutgets_blocked(lo)) {
1823                 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1824                                 PNFS_UPDATE_LAYOUT_BLOCKED);
1825                 goto out_unlock;
1826         }
1827         atomic_inc(&lo->plh_outstanding);
1828         spin_unlock(&ino->i_lock);
1829
1830         if (list_empty(&lo->plh_layouts)) {
1831                 /* The lo must be on the clp list if there is any
1832                  * chance of a CB_LAYOUTRECALL(FILE) coming in.
1833                  */
1834                 spin_lock(&clp->cl_lock);
1835                 if (list_empty(&lo->plh_layouts))
1836                         list_add_tail(&lo->plh_layouts, &server->layouts);
1837                 spin_unlock(&clp->cl_lock);
1838         }
1839
1840         pg_offset = arg.offset & ~PAGE_MASK;
1841         if (pg_offset) {
1842                 arg.offset -= pg_offset;
1843                 arg.length += pg_offset;
1844         }
1845         if (arg.length != NFS4_MAX_UINT64)
1846                 arg.length = PAGE_ALIGN(arg.length);
1847
1848         lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1849         trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1850                                  PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
1851         atomic_dec(&lo->plh_outstanding);
1852         if (IS_ERR(lseg)) {
1853                 switch(PTR_ERR(lseg)) {
1854                 case -EBUSY:
1855                         if (time_after(jiffies, giveup))
1856                                 lseg = NULL;
1857                         break;
1858                 case -ERECALLCONFLICT:
1859                         /* Huh? We hold no layouts, how is there a recall? */
1860                         if (first) {
1861                                 lseg = NULL;
1862                                 break;
1863                         }
1864                         /* Destroy the existing layout and start over */
1865                         if (time_after(jiffies, giveup))
1866                                 pnfs_destroy_layout(NFS_I(ino));
1867                         /* Fallthrough */
1868                 case -EAGAIN:
1869                         break;
1870                 default:
1871                         if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1872                                 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1873                                 lseg = NULL;
1874                         }
1875                         goto out_put_layout_hdr;
1876                 }
1877                 if (lseg) {
1878                         if (first)
1879                                 pnfs_clear_first_layoutget(lo);
1880                         trace_pnfs_update_layout(ino, pos, count,
1881                                 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1882                         pnfs_put_layout_hdr(lo);
1883                         goto lookup_again;
1884                 }
1885         } else {
1886                 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1887         }
1888
1889 out_put_layout_hdr:
1890         if (first)
1891                 pnfs_clear_first_layoutget(lo);
1892         pnfs_put_layout_hdr(lo);
1893 out:
1894         dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1895                         "(%s, offset: %llu, length: %llu)\n",
1896                         __func__, ino->i_sb->s_id,
1897                         (unsigned long long)NFS_FILEID(ino),
1898                         IS_ERR_OR_NULL(lseg) ? "not found" : "found",
1899                         iomode==IOMODE_RW ?  "read/write" : "read-only",
1900                         (unsigned long long)pos,
1901                         (unsigned long long)count);
1902         return lseg;
1903 out_unlock:
1904         spin_unlock(&ino->i_lock);
1905         goto out_put_layout_hdr;
1906 }
1907 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1908
1909 static bool
1910 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1911 {
1912         switch (range->iomode) {
1913         case IOMODE_READ:
1914         case IOMODE_RW:
1915                 break;
1916         default:
1917                 return false;
1918         }
1919         if (range->offset == NFS4_MAX_UINT64)
1920                 return false;
1921         if (range->length == 0)
1922                 return false;
1923         if (range->length != NFS4_MAX_UINT64 &&
1924             range->length > NFS4_MAX_UINT64 - range->offset)
1925                 return false;
1926         return true;
1927 }
1928
1929 struct pnfs_layout_segment *
1930 pnfs_layout_process(struct nfs4_layoutget *lgp)
1931 {
1932         struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1933         struct nfs4_layoutget_res *res = &lgp->res;
1934         struct pnfs_layout_segment *lseg;
1935         struct inode *ino = lo->plh_inode;
1936         LIST_HEAD(free_me);
1937
1938         if (!pnfs_sanity_check_layout_range(&res->range))
1939                 return ERR_PTR(-EINVAL);
1940
1941         /* Inject layout blob into I/O device driver */
1942         lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1943         if (IS_ERR_OR_NULL(lseg)) {
1944                 if (!lseg)
1945                         lseg = ERR_PTR(-ENOMEM);
1946
1947                 dprintk("%s: Could not allocate layout: error %ld\n",
1948                        __func__, PTR_ERR(lseg));
1949                 return lseg;
1950         }
1951
1952         pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
1953
1954         spin_lock(&ino->i_lock);
1955         if (pnfs_layoutgets_blocked(lo)) {
1956                 dprintk("%s forget reply due to state\n", __func__);
1957                 goto out_forget;
1958         }
1959
1960         if (!pnfs_layout_is_valid(lo)) {
1961                 /* We have a completely new layout */
1962                 pnfs_set_layout_stateid(lo, &res->stateid, true);
1963         } else if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1964                 /* existing state ID, make sure the sequence number matches. */
1965                 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1966                         dprintk("%s forget reply due to sequence\n", __func__);
1967                         goto out_forget;
1968                 }
1969                 pnfs_set_layout_stateid(lo, &res->stateid, false);
1970         } else {
1971                 /*
1972                  * We got an entirely new state ID.  Mark all segments for the
1973                  * inode invalid, and retry the layoutget
1974                  */
1975                 struct pnfs_layout_range range = {
1976                         .iomode = IOMODE_ANY,
1977                         .length = NFS4_MAX_UINT64,
1978                 };
1979                 pnfs_set_plh_return_info(lo, IOMODE_ANY, 0);
1980                 pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
1981                                                 &range, 0);
1982                 goto out_forget;
1983         }
1984
1985         pnfs_get_lseg(lseg);
1986         pnfs_layout_insert_lseg(lo, lseg, &free_me);
1987
1988
1989         if (res->return_on_close)
1990                 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1991
1992         spin_unlock(&ino->i_lock);
1993         pnfs_free_lseg_list(&free_me);
1994         return lseg;
1995
1996 out_forget:
1997         spin_unlock(&ino->i_lock);
1998         lseg->pls_layout = lo;
1999         NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
2000         if (!pnfs_layout_is_valid(lo))
2001                 nfs_commit_inode(ino, 0);
2002         return ERR_PTR(-EAGAIN);
2003 }
2004
2005 /**
2006  * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
2007  * @lo: pointer to layout header
2008  * @tmp_list: list header to be used with pnfs_free_lseg_list()
2009  * @return_range: describe layout segment ranges to be returned
2010  *
2011  * This function is mainly intended for use by layoutrecall. It attempts
2012  * to free the layout segment immediately, or else to mark it for return
2013  * as soon as its reference count drops to zero.
2014  */
2015 int
2016 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
2017                                 struct list_head *tmp_list,
2018                                 const struct pnfs_layout_range *return_range,
2019                                 u32 seq)
2020 {
2021         struct pnfs_layout_segment *lseg, *next;
2022         int remaining = 0;
2023
2024         dprintk("%s:Begin lo %p\n", __func__, lo);
2025
2026         if (list_empty(&lo->plh_segs))
2027                 return 0;
2028
2029         assert_spin_locked(&lo->plh_inode->i_lock);
2030
2031         list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
2032                 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
2033                         dprintk("%s: marking lseg %p iomode %d "
2034                                 "offset %llu length %llu\n", __func__,
2035                                 lseg, lseg->pls_range.iomode,
2036                                 lseg->pls_range.offset,
2037                                 lseg->pls_range.length);
2038                         if (mark_lseg_invalid(lseg, tmp_list))
2039                                 continue;
2040                         remaining++;
2041                         set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
2042                 }
2043
2044         if (remaining)
2045                 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2046
2047         return remaining;
2048 }
2049
2050 void pnfs_error_mark_layout_for_return(struct inode *inode,
2051                                        struct pnfs_layout_segment *lseg)
2052 {
2053         struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
2054         struct pnfs_layout_range range = {
2055                 .iomode = lseg->pls_range.iomode,
2056                 .offset = 0,
2057                 .length = NFS4_MAX_UINT64,
2058         };
2059         bool return_now = false;
2060
2061         spin_lock(&inode->i_lock);
2062         if (!pnfs_layout_is_valid(lo)) {
2063                 spin_unlock(&inode->i_lock);
2064                 return;
2065         }
2066         pnfs_set_plh_return_info(lo, range.iomode, 0);
2067         /*
2068          * mark all matching lsegs so that we are sure to have no live
2069          * segments at hand when sending layoutreturn. See pnfs_put_lseg()
2070          * for how it works.
2071          */
2072         if (!pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, &range, 0)) {
2073                 nfs4_stateid stateid;
2074                 enum pnfs_iomode iomode;
2075
2076                 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
2077                 spin_unlock(&inode->i_lock);
2078                 if (return_now)
2079                         pnfs_send_layoutreturn(lo, &stateid, iomode, false);
2080         } else {
2081                 spin_unlock(&inode->i_lock);
2082                 nfs_commit_inode(inode, 0);
2083         }
2084 }
2085 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2086
2087 void
2088 pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
2089 {
2090         if (pgio->pg_lseg == NULL ||
2091             test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
2092                 return;
2093         pnfs_put_lseg(pgio->pg_lseg);
2094         pgio->pg_lseg = NULL;
2095 }
2096 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
2097
2098 /*
2099  * Check for any intersection between the request and the pgio->pg_lseg,
2100  * and if none, put this pgio->pg_lseg away.
2101  */
2102 static void
2103 pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2104 {
2105         if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
2106                 pnfs_put_lseg(pgio->pg_lseg);
2107                 pgio->pg_lseg = NULL;
2108         }
2109 }
2110
2111 void
2112 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2113 {
2114         u64 rd_size = req->wb_bytes;
2115
2116         pnfs_generic_pg_check_layout(pgio);
2117         pnfs_generic_pg_check_range(pgio, req);
2118         if (pgio->pg_lseg == NULL) {
2119                 if (pgio->pg_dreq == NULL)
2120                         rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2121                 else
2122                         rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
2123
2124                 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2125                                                    req->wb_context,
2126                                                    req_offset(req),
2127                                                    rd_size,
2128                                                    IOMODE_READ,
2129                                                    false,
2130                                                    GFP_KERNEL);
2131                 if (IS_ERR(pgio->pg_lseg)) {
2132                         pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2133                         pgio->pg_lseg = NULL;
2134                         return;
2135                 }
2136         }
2137         /* If no lseg, fall back to read through mds */
2138         if (pgio->pg_lseg == NULL)
2139                 nfs_pageio_reset_read_mds(pgio);
2140
2141 }
2142 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2143
2144 void
2145 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2146                            struct nfs_page *req, u64 wb_size)
2147 {
2148         pnfs_generic_pg_check_layout(pgio);
2149         pnfs_generic_pg_check_range(pgio, req);
2150         if (pgio->pg_lseg == NULL) {
2151                 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2152                                                    req->wb_context,
2153                                                    req_offset(req),
2154                                                    wb_size,
2155                                                    IOMODE_RW,
2156                                                    false,
2157                                                    GFP_NOFS);
2158                 if (IS_ERR(pgio->pg_lseg)) {
2159                         pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2160                         pgio->pg_lseg = NULL;
2161                         return;
2162                 }
2163         }
2164         /* If no lseg, fall back to write through mds */
2165         if (pgio->pg_lseg == NULL)
2166                 nfs_pageio_reset_write_mds(pgio);
2167 }
2168 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2169
2170 void
2171 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2172 {
2173         if (desc->pg_lseg) {
2174                 pnfs_put_lseg(desc->pg_lseg);
2175                 desc->pg_lseg = NULL;
2176         }
2177 }
2178 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2179
2180 /*
2181  * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2182  * of bytes (maximum @req->wb_bytes) that can be coalesced.
2183  */
2184 size_t
2185 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2186                      struct nfs_page *prev, struct nfs_page *req)
2187 {
2188         unsigned int size;
2189         u64 seg_end, req_start, seg_left;
2190
2191         size = nfs_generic_pg_test(pgio, prev, req);
2192         if (!size)
2193                 return 0;
2194
2195         /*
2196          * 'size' contains the number of bytes left in the current page (up
2197          * to the original size asked for in @req->wb_bytes).
2198          *
2199          * Calculate how many bytes are left in the layout segment
2200          * and if there are less bytes than 'size', return that instead.
2201          *
2202          * Please also note that 'end_offset' is actually the offset of the
2203          * first byte that lies outside the pnfs_layout_range. FIXME?
2204          *
2205          */
2206         if (pgio->pg_lseg) {
2207                 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
2208                                      pgio->pg_lseg->pls_range.length);
2209                 req_start = req_offset(req);
2210
2211                 /* start of request is past the last byte of this segment */
2212                 if (req_start >= seg_end)
2213                         return 0;
2214
2215                 /* adjust 'size' iff there are fewer bytes left in the
2216                  * segment than what nfs_generic_pg_test returned */
2217                 seg_left = seg_end - req_start;
2218                 if (seg_left < size)
2219                         size = (unsigned int)seg_left;
2220         }
2221
2222         return size;
2223 }
2224 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2225
2226 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2227 {
2228         struct nfs_pageio_descriptor pgio;
2229
2230         /* Resend all requests through the MDS */
2231         nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2232                               hdr->completion_ops);
2233         set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
2234         return nfs_pageio_resend(&pgio, hdr);
2235 }
2236 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2237
2238 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2239 {
2240
2241         dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2242         if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2243             PNFS_LAYOUTRET_ON_ERROR) {
2244                 pnfs_return_layout(hdr->inode);
2245         }
2246         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2247                 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2248 }
2249
2250 /*
2251  * Called by non rpc-based layout drivers
2252  */
2253 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2254 {
2255         if (likely(!hdr->pnfs_error)) {
2256                 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2257                                 hdr->mds_offset + hdr->res.count);
2258                 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2259         }
2260         trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2261         if (unlikely(hdr->pnfs_error))
2262                 pnfs_ld_handle_write_error(hdr);
2263         hdr->mds_ops->rpc_release(hdr);
2264 }
2265 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2266
2267 static void
2268 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2269                 struct nfs_pgio_header *hdr)
2270 {
2271         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2272
2273         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2274                 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2275                 nfs_pageio_reset_write_mds(desc);
2276                 mirror->pg_recoalesce = 1;
2277         }
2278         hdr->completion_ops->completion(hdr);
2279 }
2280
2281 static enum pnfs_try_status
2282 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2283                         const struct rpc_call_ops *call_ops,
2284                         struct pnfs_layout_segment *lseg,
2285                         int how)
2286 {
2287         struct inode *inode = hdr->inode;
2288         enum pnfs_try_status trypnfs;
2289         struct nfs_server *nfss = NFS_SERVER(inode);
2290
2291         hdr->mds_ops = call_ops;
2292
2293         dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2294                 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2295         trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2296         if (trypnfs != PNFS_NOT_ATTEMPTED)
2297                 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2298         dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2299         return trypnfs;
2300 }
2301
2302 static void
2303 pnfs_do_write(struct nfs_pageio_descriptor *desc,
2304               struct nfs_pgio_header *hdr, int how)
2305 {
2306         const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2307         struct pnfs_layout_segment *lseg = desc->pg_lseg;
2308         enum pnfs_try_status trypnfs;
2309
2310         trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2311         switch (trypnfs) {
2312         case PNFS_NOT_ATTEMPTED:
2313                 pnfs_write_through_mds(desc, hdr);
2314         case PNFS_ATTEMPTED:
2315                 break;
2316         case PNFS_TRY_AGAIN:
2317                 /* cleanup hdr and prepare to redo pnfs */
2318                 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2319                         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2320                         list_splice_init(&hdr->pages, &mirror->pg_list);
2321                         mirror->pg_recoalesce = 1;
2322                 }
2323                 hdr->mds_ops->rpc_release(hdr);
2324         }
2325 }
2326
2327 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2328 {
2329         pnfs_put_lseg(hdr->lseg);
2330         nfs_pgio_header_free(hdr);
2331 }
2332
2333 int
2334 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2335 {
2336         struct nfs_pgio_header *hdr;
2337         int ret;
2338
2339         hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2340         if (!hdr) {
2341                 desc->pg_error = -ENOMEM;
2342                 return desc->pg_error;
2343         }
2344         nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2345
2346         hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2347         ret = nfs_generic_pgio(desc, hdr);
2348         if (!ret)
2349                 pnfs_do_write(desc, hdr, desc->pg_ioflags);
2350
2351         return ret;
2352 }
2353 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2354
2355 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2356 {
2357         struct nfs_pageio_descriptor pgio;
2358
2359         /* Resend all requests through the MDS */
2360         nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2361         return nfs_pageio_resend(&pgio, hdr);
2362 }
2363 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2364
2365 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2366 {
2367         dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2368         if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2369             PNFS_LAYOUTRET_ON_ERROR) {
2370                 pnfs_return_layout(hdr->inode);
2371         }
2372         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2373                 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2374 }
2375
2376 /*
2377  * Called by non rpc-based layout drivers
2378  */
2379 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
2380 {
2381         if (likely(!hdr->pnfs_error))
2382                 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2383         trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2384         if (unlikely(hdr->pnfs_error))
2385                 pnfs_ld_handle_read_error(hdr);
2386         hdr->mds_ops->rpc_release(hdr);
2387 }
2388 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2389
2390 static void
2391 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
2392                 struct nfs_pgio_header *hdr)
2393 {
2394         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2395
2396         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2397                 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2398                 nfs_pageio_reset_read_mds(desc);
2399                 mirror->pg_recoalesce = 1;
2400         }
2401         hdr->completion_ops->completion(hdr);
2402 }
2403
2404 /*
2405  * Call the appropriate parallel I/O subsystem read function.
2406  */
2407 static enum pnfs_try_status
2408 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
2409                        const struct rpc_call_ops *call_ops,
2410                        struct pnfs_layout_segment *lseg)
2411 {
2412         struct inode *inode = hdr->inode;
2413         struct nfs_server *nfss = NFS_SERVER(inode);
2414         enum pnfs_try_status trypnfs;
2415
2416         hdr->mds_ops = call_ops;
2417
2418         dprintk("%s: Reading ino:%lu %u@%llu\n",
2419                 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
2420
2421         trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
2422         if (trypnfs != PNFS_NOT_ATTEMPTED)
2423                 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
2424         dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2425         return trypnfs;
2426 }
2427
2428 /* Resend all requests through pnfs. */
2429 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2430 {
2431         struct nfs_pageio_descriptor pgio;
2432
2433         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2434                 /* Prevent deadlocks with layoutreturn! */
2435                 pnfs_put_lseg(hdr->lseg);
2436                 hdr->lseg = NULL;
2437
2438                 nfs_pageio_init_read(&pgio, hdr->inode, false,
2439                                         hdr->completion_ops);
2440                 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2441         }
2442 }
2443 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2444
2445 static void
2446 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
2447 {
2448         const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2449         struct pnfs_layout_segment *lseg = desc->pg_lseg;
2450         enum pnfs_try_status trypnfs;
2451
2452         trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
2453         switch (trypnfs) {
2454         case PNFS_NOT_ATTEMPTED:
2455                 pnfs_read_through_mds(desc, hdr);
2456         case PNFS_ATTEMPTED:
2457                 break;
2458         case PNFS_TRY_AGAIN:
2459                 /* cleanup hdr and prepare to redo pnfs */
2460                 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2461                         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2462                         list_splice_init(&hdr->pages, &mirror->pg_list);
2463                         mirror->pg_recoalesce = 1;
2464                 }
2465                 hdr->mds_ops->rpc_release(hdr);
2466         }
2467 }
2468
2469 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2470 {
2471         pnfs_put_lseg(hdr->lseg);
2472         nfs_pgio_header_free(hdr);
2473 }
2474
2475 int
2476 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2477 {
2478         struct nfs_pgio_header *hdr;
2479         int ret;
2480
2481         hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2482         if (!hdr) {
2483                 desc->pg_error = -ENOMEM;
2484                 return desc->pg_error;
2485         }
2486         nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
2487         hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2488         ret = nfs_generic_pgio(desc, hdr);
2489         if (!ret)
2490                 pnfs_do_read(desc, hdr);
2491         return ret;
2492 }
2493 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2494
2495 static void pnfs_clear_layoutcommitting(struct inode *inode)
2496 {
2497         unsigned long *bitlock = &NFS_I(inode)->flags;
2498
2499         clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
2500         smp_mb__after_atomic();
2501         wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2502 }
2503
2504 /*
2505  * There can be multiple RW segments.
2506  */
2507 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
2508 {
2509         struct pnfs_layout_segment *lseg;
2510
2511         list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2512                 if (lseg->pls_range.iomode == IOMODE_RW &&
2513                     test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
2514                         list_add(&lseg->pls_lc_list, listp);
2515         }
2516 }
2517
2518 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2519 {
2520         struct pnfs_layout_segment *lseg, *tmp;
2521
2522         /* Matched by references in pnfs_set_layoutcommit */
2523         list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2524                 list_del_init(&lseg->pls_lc_list);
2525                 pnfs_put_lseg(lseg);
2526         }
2527
2528         pnfs_clear_layoutcommitting(inode);
2529 }
2530
2531 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2532 {
2533         pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
2534 }
2535 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2536
2537 void
2538 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2539                 loff_t end_pos)
2540 {
2541         struct nfs_inode *nfsi = NFS_I(inode);
2542         bool mark_as_dirty = false;
2543
2544         spin_lock(&inode->i_lock);
2545         if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2546                 nfsi->layout->plh_lwb = end_pos;
2547                 mark_as_dirty = true;
2548                 dprintk("%s: Set layoutcommit for inode %lu ",
2549                         __func__, inode->i_ino);
2550         } else if (end_pos > nfsi->layout->plh_lwb)
2551                 nfsi->layout->plh_lwb = end_pos;
2552         if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
2553                 /* references matched in nfs4_layoutcommit_release */
2554                 pnfs_get_lseg(lseg);
2555         }
2556         spin_unlock(&inode->i_lock);
2557         dprintk("%s: lseg %p end_pos %llu\n",
2558                 __func__, lseg, nfsi->layout->plh_lwb);
2559
2560         /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
2561          * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
2562         if (mark_as_dirty)
2563                 mark_inode_dirty_sync(inode);
2564 }
2565 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2566
2567 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2568 {
2569         struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2570
2571         if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2572                 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
2573         pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
2574 }
2575
2576 /*
2577  * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
2578  * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
2579  * data to disk to allow the server to recover the data if it crashes.
2580  * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
2581  * is off, and a COMMIT is sent to a data server, or
2582  * if WRITEs to a data server return NFS_DATA_SYNC.
2583  */
2584 int
2585 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
2586 {
2587         struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2588         struct nfs4_layoutcommit_data *data;
2589         struct nfs_inode *nfsi = NFS_I(inode);
2590         loff_t end_pos;
2591         int status;
2592
2593         if (!pnfs_layoutcommit_outstanding(inode))
2594                 return 0;
2595
2596         dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2597
2598         status = -EAGAIN;
2599         if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2600                 if (!sync)
2601                         goto out;
2602                 status = wait_on_bit_lock_action(&nfsi->flags,
2603                                 NFS_INO_LAYOUTCOMMITTING,
2604                                 nfs_wait_bit_killable,
2605                                 TASK_KILLABLE);
2606                 if (status)
2607                         goto out;
2608         }
2609
2610         status = -ENOMEM;
2611         /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
2612         data = kzalloc(sizeof(*data), GFP_NOFS);
2613         if (!data)
2614                 goto clear_layoutcommitting;
2615
2616         status = 0;
2617         spin_lock(&inode->i_lock);
2618         if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2619                 goto out_unlock;
2620
2621         INIT_LIST_HEAD(&data->lseg_list);
2622         pnfs_list_write_lseg(inode, &data->lseg_list);
2623
2624         end_pos = nfsi->layout->plh_lwb;
2625
2626         nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
2627         spin_unlock(&inode->i_lock);
2628
2629         data->args.inode = inode;
2630         data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
2631         nfs_fattr_init(&data->fattr);
2632         data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2633         data->res.fattr = &data->fattr;
2634         if (end_pos != 0)
2635                 data->args.lastbytewritten = end_pos - 1;
2636         else
2637                 data->args.lastbytewritten = U64_MAX;
2638         data->res.server = NFS_SERVER(inode);
2639
2640         if (ld->prepare_layoutcommit) {
2641                 status = ld->prepare_layoutcommit(&data->args);
2642                 if (status) {
2643                         put_rpccred(data->cred);
2644                         spin_lock(&inode->i_lock);
2645                         set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2646                         if (end_pos > nfsi->layout->plh_lwb)
2647                                 nfsi->layout->plh_lwb = end_pos;
2648                         goto out_unlock;
2649                 }
2650         }
2651
2652
2653         status = nfs4_proc_layoutcommit(data, sync);
2654 out:
2655         if (status)
2656                 mark_inode_dirty_sync(inode);
2657         dprintk("<-- %s status %d\n", __func__, status);
2658         return status;
2659 out_unlock:
2660         spin_unlock(&inode->i_lock);
2661         kfree(data);
2662 clear_layoutcommitting:
2663         pnfs_clear_layoutcommitting(inode);
2664         goto out;
2665 }
2666 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
2667
2668 int
2669 pnfs_generic_sync(struct inode *inode, bool datasync)
2670 {
2671         return pnfs_layoutcommit_inode(inode, true);
2672 }
2673 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2674
2675 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2676 {
2677         struct nfs4_threshold *thp;
2678
2679         thp = kzalloc(sizeof(*thp), GFP_NOFS);
2680         if (!thp) {
2681                 dprintk("%s mdsthreshold allocation failed\n", __func__);
2682                 return NULL;
2683         }
2684         return thp;
2685 }
2686
2687 #if IS_ENABLED(CONFIG_NFS_V4_2)
2688 int
2689 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
2690 {
2691         struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2692         struct nfs_server *server = NFS_SERVER(inode);
2693         struct nfs_inode *nfsi = NFS_I(inode);
2694         struct nfs42_layoutstat_data *data;
2695         struct pnfs_layout_hdr *hdr;
2696         int status = 0;
2697
2698         if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2699                 goto out;
2700
2701         if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2702                 goto out;
2703
2704         if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2705                 goto out;
2706
2707         spin_lock(&inode->i_lock);
2708         if (!NFS_I(inode)->layout) {
2709                 spin_unlock(&inode->i_lock);
2710                 goto out_clear_layoutstats;
2711         }
2712         hdr = NFS_I(inode)->layout;
2713         pnfs_get_layout_hdr(hdr);
2714         spin_unlock(&inode->i_lock);
2715
2716         data = kzalloc(sizeof(*data), gfp_flags);
2717         if (!data) {
2718                 status = -ENOMEM;
2719                 goto out_put;
2720         }
2721
2722         data->args.fh = NFS_FH(inode);
2723         data->args.inode = inode;
2724         status = ld->prepare_layoutstats(&data->args);
2725         if (status)
2726                 goto out_free;
2727
2728         status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2729
2730 out:
2731         dprintk("%s returns %d\n", __func__, status);
2732         return status;
2733
2734 out_free:
2735         kfree(data);
2736 out_put:
2737         pnfs_put_layout_hdr(hdr);
2738 out_clear_layoutstats:
2739         smp_mb__before_atomic();
2740         clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2741         smp_mb__after_atomic();
2742         goto out;
2743 }
2744 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
2745 #endif
2746
2747 unsigned int layoutstats_timer;
2748 module_param(layoutstats_timer, uint, 0644);
2749 EXPORT_SYMBOL_GPL(layoutstats_timer);