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