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