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