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