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