Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[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
37 #define NFSDBG_FACILITY         NFSDBG_PNFS
38
39 /* Locking:
40  *
41  * pnfs_spinlock:
42  *      protects pnfs_modules_tbl.
43  */
44 static DEFINE_SPINLOCK(pnfs_spinlock);
45
46 /*
47  * pnfs_modules_tbl holds all pnfs modules
48  */
49 static LIST_HEAD(pnfs_modules_tbl);
50
51 /* Return the registered pnfs layout driver module matching given id */
52 static struct pnfs_layoutdriver_type *
53 find_pnfs_driver_locked(u32 id)
54 {
55         struct pnfs_layoutdriver_type *local;
56
57         list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
58                 if (local->id == id)
59                         goto out;
60         local = NULL;
61 out:
62         dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
63         return local;
64 }
65
66 static struct pnfs_layoutdriver_type *
67 find_pnfs_driver(u32 id)
68 {
69         struct pnfs_layoutdriver_type *local;
70
71         spin_lock(&pnfs_spinlock);
72         local = find_pnfs_driver_locked(id);
73         spin_unlock(&pnfs_spinlock);
74         return local;
75 }
76
77 void
78 unset_pnfs_layoutdriver(struct nfs_server *nfss)
79 {
80         if (nfss->pnfs_curr_ld) {
81                 if (nfss->pnfs_curr_ld->clear_layoutdriver)
82                         nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
83                 module_put(nfss->pnfs_curr_ld->owner);
84         }
85         nfss->pnfs_curr_ld = NULL;
86 }
87
88 /*
89  * Try to set the server's pnfs module to the pnfs layout type specified by id.
90  * Currently only one pNFS layout driver per filesystem is supported.
91  *
92  * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
93  */
94 void
95 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
96                       u32 id)
97 {
98         struct pnfs_layoutdriver_type *ld_type = NULL;
99
100         if (id == 0)
101                 goto out_no_driver;
102         if (!(server->nfs_client->cl_exchange_flags &
103                  (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
104                 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
105                         __func__, id, server->nfs_client->cl_exchange_flags);
106                 goto out_no_driver;
107         }
108         ld_type = find_pnfs_driver(id);
109         if (!ld_type) {
110                 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
111                 ld_type = find_pnfs_driver(id);
112                 if (!ld_type) {
113                         dprintk("%s: No pNFS module found for %u.\n",
114                                 __func__, id);
115                         goto out_no_driver;
116                 }
117         }
118         if (!try_module_get(ld_type->owner)) {
119                 dprintk("%s: Could not grab reference on module\n", __func__);
120                 goto out_no_driver;
121         }
122         server->pnfs_curr_ld = ld_type;
123         if (ld_type->set_layoutdriver
124             && ld_type->set_layoutdriver(server, mntfh)) {
125                 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
126                         "driver %u.\n", __func__, id);
127                 module_put(ld_type->owner);
128                 goto out_no_driver;
129         }
130
131         dprintk("%s: pNFS module for %u set\n", __func__, id);
132         return;
133
134 out_no_driver:
135         dprintk("%s: Using NFSv4 I/O\n", __func__);
136         server->pnfs_curr_ld = NULL;
137 }
138
139 int
140 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
141 {
142         int status = -EINVAL;
143         struct pnfs_layoutdriver_type *tmp;
144
145         if (ld_type->id == 0) {
146                 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
147                 return status;
148         }
149         if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
150                 printk(KERN_ERR "NFS: %s Layout driver must provide "
151                        "alloc_lseg and free_lseg.\n", __func__);
152                 return status;
153         }
154
155         spin_lock(&pnfs_spinlock);
156         tmp = find_pnfs_driver_locked(ld_type->id);
157         if (!tmp) {
158                 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
159                 status = 0;
160                 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
161                         ld_type->name);
162         } else {
163                 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
164                         __func__, ld_type->id);
165         }
166         spin_unlock(&pnfs_spinlock);
167
168         return status;
169 }
170 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
171
172 void
173 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
174 {
175         dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
176         spin_lock(&pnfs_spinlock);
177         list_del(&ld_type->pnfs_tblid);
178         spin_unlock(&pnfs_spinlock);
179 }
180 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
181
182 /*
183  * pNFS client layout cache
184  */
185
186 /* Need to hold i_lock if caller does not already hold reference */
187 void
188 get_layout_hdr(struct pnfs_layout_hdr *lo)
189 {
190         atomic_inc(&lo->plh_refcount);
191 }
192
193 static struct pnfs_layout_hdr *
194 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
195 {
196         struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
197         return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
198                 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
199 }
200
201 static void
202 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
203 {
204         struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
205         put_rpccred(lo->plh_lc_cred);
206         return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
207 }
208
209 static void
210 destroy_layout_hdr(struct pnfs_layout_hdr *lo)
211 {
212         dprintk("%s: freeing layout cache %p\n", __func__, lo);
213         BUG_ON(!list_empty(&lo->plh_layouts));
214         NFS_I(lo->plh_inode)->layout = NULL;
215         pnfs_free_layout_hdr(lo);
216 }
217
218 static void
219 put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
220 {
221         if (atomic_dec_and_test(&lo->plh_refcount))
222                 destroy_layout_hdr(lo);
223 }
224
225 void
226 put_layout_hdr(struct pnfs_layout_hdr *lo)
227 {
228         struct inode *inode = lo->plh_inode;
229
230         if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
231                 destroy_layout_hdr(lo);
232                 spin_unlock(&inode->i_lock);
233         }
234 }
235
236 static void
237 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
238 {
239         INIT_LIST_HEAD(&lseg->pls_list);
240         INIT_LIST_HEAD(&lseg->pls_lc_list);
241         atomic_set(&lseg->pls_refcount, 1);
242         smp_mb();
243         set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
244         lseg->pls_layout = lo;
245 }
246
247 static void free_lseg(struct pnfs_layout_segment *lseg)
248 {
249         struct inode *ino = lseg->pls_layout->plh_inode;
250
251         NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
252         /* Matched by get_layout_hdr in pnfs_insert_layout */
253         put_layout_hdr(NFS_I(ino)->layout);
254 }
255
256 static void
257 put_lseg_common(struct pnfs_layout_segment *lseg)
258 {
259         struct inode *inode = lseg->pls_layout->plh_inode;
260
261         WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
262         list_del_init(&lseg->pls_list);
263         if (list_empty(&lseg->pls_layout->plh_segs)) {
264                 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
265                 /* Matched by initial refcount set in alloc_init_layout_hdr */
266                 put_layout_hdr_locked(lseg->pls_layout);
267         }
268         rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
269 }
270
271 void
272 put_lseg(struct pnfs_layout_segment *lseg)
273 {
274         struct inode *inode;
275
276         if (!lseg)
277                 return;
278
279         dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
280                 atomic_read(&lseg->pls_refcount),
281                 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
282         inode = lseg->pls_layout->plh_inode;
283         if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
284                 LIST_HEAD(free_me);
285
286                 put_lseg_common(lseg);
287                 list_add(&lseg->pls_list, &free_me);
288                 spin_unlock(&inode->i_lock);
289                 pnfs_free_lseg_list(&free_me);
290         }
291 }
292 EXPORT_SYMBOL_GPL(put_lseg);
293
294 static inline u64
295 end_offset(u64 start, u64 len)
296 {
297         u64 end;
298
299         end = start + len;
300         return end >= start ? end : NFS4_MAX_UINT64;
301 }
302
303 /* last octet in a range */
304 static inline u64
305 last_byte_offset(u64 start, u64 len)
306 {
307         u64 end;
308
309         BUG_ON(!len);
310         end = start + len;
311         return end > start ? end - 1 : NFS4_MAX_UINT64;
312 }
313
314 /*
315  * is l2 fully contained in l1?
316  *   start1                             end1
317  *   [----------------------------------)
318  *           start2           end2
319  *           [----------------)
320  */
321 static inline int
322 lo_seg_contained(struct pnfs_layout_range *l1,
323                  struct pnfs_layout_range *l2)
324 {
325         u64 start1 = l1->offset;
326         u64 end1 = end_offset(start1, l1->length);
327         u64 start2 = l2->offset;
328         u64 end2 = end_offset(start2, l2->length);
329
330         return (start1 <= start2) && (end1 >= end2);
331 }
332
333 /*
334  * is l1 and l2 intersecting?
335  *   start1                             end1
336  *   [----------------------------------)
337  *                              start2           end2
338  *                              [----------------)
339  */
340 static inline int
341 lo_seg_intersecting(struct pnfs_layout_range *l1,
342                     struct pnfs_layout_range *l2)
343 {
344         u64 start1 = l1->offset;
345         u64 end1 = end_offset(start1, l1->length);
346         u64 start2 = l2->offset;
347         u64 end2 = end_offset(start2, l2->length);
348
349         return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
350                (end2 == NFS4_MAX_UINT64 || end2 > start1);
351 }
352
353 static bool
354 should_free_lseg(struct pnfs_layout_range *lseg_range,
355                  struct pnfs_layout_range *recall_range)
356 {
357         return (recall_range->iomode == IOMODE_ANY ||
358                 lseg_range->iomode == recall_range->iomode) &&
359                lo_seg_intersecting(lseg_range, recall_range);
360 }
361
362 /* Returns 1 if lseg is removed from list, 0 otherwise */
363 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
364                              struct list_head *tmp_list)
365 {
366         int rv = 0;
367
368         if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
369                 /* Remove the reference keeping the lseg in the
370                  * list.  It will now be removed when all
371                  * outstanding io is finished.
372                  */
373                 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
374                         atomic_read(&lseg->pls_refcount));
375                 if (atomic_dec_and_test(&lseg->pls_refcount)) {
376                         put_lseg_common(lseg);
377                         list_add(&lseg->pls_list, tmp_list);
378                         rv = 1;
379                 }
380         }
381         return rv;
382 }
383
384 /* Returns count of number of matching invalid lsegs remaining in list
385  * after call.
386  */
387 int
388 mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
389                             struct list_head *tmp_list,
390                             struct pnfs_layout_range *recall_range)
391 {
392         struct pnfs_layout_segment *lseg, *next;
393         int invalid = 0, removed = 0;
394
395         dprintk("%s:Begin lo %p\n", __func__, lo);
396
397         if (list_empty(&lo->plh_segs)) {
398                 /* Reset MDS Threshold I/O counters */
399                 NFS_I(lo->plh_inode)->write_io = 0;
400                 NFS_I(lo->plh_inode)->read_io = 0;
401                 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
402                         put_layout_hdr_locked(lo);
403                 return 0;
404         }
405         list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
406                 if (!recall_range ||
407                     should_free_lseg(&lseg->pls_range, recall_range)) {
408                         dprintk("%s: freeing lseg %p iomode %d "
409                                 "offset %llu length %llu\n", __func__,
410                                 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
411                                 lseg->pls_range.length);
412                         invalid++;
413                         removed += mark_lseg_invalid(lseg, tmp_list);
414                 }
415         dprintk("%s:Return %i\n", __func__, invalid - removed);
416         return invalid - removed;
417 }
418
419 /* note free_me must contain lsegs from a single layout_hdr */
420 void
421 pnfs_free_lseg_list(struct list_head *free_me)
422 {
423         struct pnfs_layout_segment *lseg, *tmp;
424         struct pnfs_layout_hdr *lo;
425
426         if (list_empty(free_me))
427                 return;
428
429         lo = list_first_entry(free_me, struct pnfs_layout_segment,
430                               pls_list)->pls_layout;
431
432         if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
433                 struct nfs_client *clp;
434
435                 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
436                 spin_lock(&clp->cl_lock);
437                 list_del_init(&lo->plh_layouts);
438                 spin_unlock(&clp->cl_lock);
439         }
440         list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
441                 list_del(&lseg->pls_list);
442                 free_lseg(lseg);
443         }
444 }
445
446 void
447 pnfs_destroy_layout(struct nfs_inode *nfsi)
448 {
449         struct pnfs_layout_hdr *lo;
450         LIST_HEAD(tmp_list);
451
452         spin_lock(&nfsi->vfs_inode.i_lock);
453         lo = nfsi->layout;
454         if (lo) {
455                 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
456                 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
457         }
458         spin_unlock(&nfsi->vfs_inode.i_lock);
459         pnfs_free_lseg_list(&tmp_list);
460 }
461 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
462
463 /*
464  * Called by the state manger to remove all layouts established under an
465  * expired lease.
466  */
467 void
468 pnfs_destroy_all_layouts(struct nfs_client *clp)
469 {
470         struct nfs_server *server;
471         struct pnfs_layout_hdr *lo;
472         LIST_HEAD(tmp_list);
473
474         nfs4_deviceid_mark_client_invalid(clp);
475         nfs4_deviceid_purge_client(clp);
476
477         spin_lock(&clp->cl_lock);
478         rcu_read_lock();
479         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
480                 if (!list_empty(&server->layouts))
481                         list_splice_init(&server->layouts, &tmp_list);
482         }
483         rcu_read_unlock();
484         spin_unlock(&clp->cl_lock);
485
486         while (!list_empty(&tmp_list)) {
487                 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
488                                 plh_layouts);
489                 dprintk("%s freeing layout for inode %lu\n", __func__,
490                         lo->plh_inode->i_ino);
491                 list_del_init(&lo->plh_layouts);
492                 pnfs_destroy_layout(NFS_I(lo->plh_inode));
493         }
494 }
495
496 /* update lo->plh_stateid with new if is more recent */
497 void
498 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
499                         bool update_barrier)
500 {
501         u32 oldseq, newseq;
502
503         oldseq = be32_to_cpu(lo->plh_stateid.seqid);
504         newseq = be32_to_cpu(new->seqid);
505         if ((int)(newseq - oldseq) > 0) {
506                 nfs4_stateid_copy(&lo->plh_stateid, new);
507                 if (update_barrier) {
508                         u32 new_barrier = be32_to_cpu(new->seqid);
509
510                         if ((int)(new_barrier - lo->plh_barrier))
511                                 lo->plh_barrier = new_barrier;
512                 } else {
513                         /* Because of wraparound, we want to keep the barrier
514                          * "close" to the current seqids.  It needs to be
515                          * within 2**31 to count as "behind", so if it
516                          * gets too near that limit, give us a litle leeway
517                          * and bring it to within 2**30.
518                          * NOTE - and yes, this is all unsigned arithmetic.
519                          */
520                         if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
521                                 lo->plh_barrier = newseq - (1 << 30);
522                 }
523         }
524 }
525
526 /* lget is set to 1 if called from inside send_layoutget call chain */
527 static bool
528 pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
529                         int lget)
530 {
531         if ((stateid) &&
532             (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0)
533                 return true;
534         return lo->plh_block_lgets ||
535                 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
536                 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
537                 (list_empty(&lo->plh_segs) &&
538                  (atomic_read(&lo->plh_outstanding) > lget));
539 }
540
541 int
542 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
543                               struct nfs4_state *open_state)
544 {
545         int status = 0;
546
547         dprintk("--> %s\n", __func__);
548         spin_lock(&lo->plh_inode->i_lock);
549         if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
550                 status = -EAGAIN;
551         } else if (list_empty(&lo->plh_segs)) {
552                 int seq;
553
554                 do {
555                         seq = read_seqbegin(&open_state->seqlock);
556                         nfs4_stateid_copy(dst, &open_state->stateid);
557                 } while (read_seqretry(&open_state->seqlock, seq));
558         } else
559                 nfs4_stateid_copy(dst, &lo->plh_stateid);
560         spin_unlock(&lo->plh_inode->i_lock);
561         dprintk("<-- %s\n", __func__);
562         return status;
563 }
564
565 /*
566 * Get layout from server.
567 *    for now, assume that whole file layouts are requested.
568 *    arg->offset: 0
569 *    arg->length: all ones
570 */
571 static struct pnfs_layout_segment *
572 send_layoutget(struct pnfs_layout_hdr *lo,
573            struct nfs_open_context *ctx,
574            struct pnfs_layout_range *range,
575            gfp_t gfp_flags)
576 {
577         struct inode *ino = lo->plh_inode;
578         struct nfs_server *server = NFS_SERVER(ino);
579         struct nfs4_layoutget *lgp;
580         struct pnfs_layout_segment *lseg = NULL;
581         struct page **pages = NULL;
582         int i;
583         u32 max_resp_sz, max_pages;
584
585         dprintk("--> %s\n", __func__);
586
587         BUG_ON(ctx == NULL);
588         lgp = kzalloc(sizeof(*lgp), gfp_flags);
589         if (lgp == NULL)
590                 return NULL;
591
592         /* allocate pages for xdr post processing */
593         max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
594         max_pages = nfs_page_array_len(0, max_resp_sz);
595
596         pages = kcalloc(max_pages, sizeof(struct page *), gfp_flags);
597         if (!pages)
598                 goto out_err_free;
599
600         for (i = 0; i < max_pages; i++) {
601                 pages[i] = alloc_page(gfp_flags);
602                 if (!pages[i])
603                         goto out_err_free;
604         }
605
606         lgp->args.minlength = PAGE_CACHE_SIZE;
607         if (lgp->args.minlength > range->length)
608                 lgp->args.minlength = range->length;
609         lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
610         lgp->args.range = *range;
611         lgp->args.type = server->pnfs_curr_ld->id;
612         lgp->args.inode = ino;
613         lgp->args.ctx = get_nfs_open_context(ctx);
614         lgp->args.layout.pages = pages;
615         lgp->args.layout.pglen = max_pages * PAGE_SIZE;
616         lgp->lsegpp = &lseg;
617         lgp->gfp_flags = gfp_flags;
618
619         /* Synchronously retrieve layout information from server and
620          * store in lseg.
621          */
622         nfs4_proc_layoutget(lgp);
623         if (!lseg) {
624                 /* remember that LAYOUTGET failed and suspend trying */
625                 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
626         }
627
628         /* free xdr pages */
629         for (i = 0; i < max_pages; i++)
630                 __free_page(pages[i]);
631         kfree(pages);
632
633         return lseg;
634
635 out_err_free:
636         /* free any allocated xdr pages, lgp as it's not used */
637         if (pages) {
638                 for (i = 0; i < max_pages; i++) {
639                         if (!pages[i])
640                                 break;
641                         __free_page(pages[i]);
642                 }
643                 kfree(pages);
644         }
645         kfree(lgp);
646         return NULL;
647 }
648
649 /* Initiates a LAYOUTRETURN(FILE) */
650 int
651 _pnfs_return_layout(struct inode *ino)
652 {
653         struct pnfs_layout_hdr *lo = NULL;
654         struct nfs_inode *nfsi = NFS_I(ino);
655         LIST_HEAD(tmp_list);
656         struct nfs4_layoutreturn *lrp;
657         nfs4_stateid stateid;
658         int status = 0;
659
660         dprintk("--> %s\n", __func__);
661
662         spin_lock(&ino->i_lock);
663         lo = nfsi->layout;
664         if (!lo) {
665                 spin_unlock(&ino->i_lock);
666                 dprintk("%s: no layout to return\n", __func__);
667                 return status;
668         }
669         stateid = nfsi->layout->plh_stateid;
670         /* Reference matched in nfs4_layoutreturn_release */
671         get_layout_hdr(lo);
672         mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
673         lo->plh_block_lgets++;
674         spin_unlock(&ino->i_lock);
675         pnfs_free_lseg_list(&tmp_list);
676
677         WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
678
679         lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
680         if (unlikely(lrp == NULL)) {
681                 status = -ENOMEM;
682                 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
683                 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
684                 put_layout_hdr(lo);
685                 goto out;
686         }
687
688         lrp->args.stateid = stateid;
689         lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
690         lrp->args.inode = ino;
691         lrp->args.layout = lo;
692         lrp->clp = NFS_SERVER(ino)->nfs_client;
693
694         status = nfs4_proc_layoutreturn(lrp);
695 out:
696         dprintk("<-- %s status: %d\n", __func__, status);
697         return status;
698 }
699 EXPORT_SYMBOL_GPL(_pnfs_return_layout);
700
701 bool pnfs_roc(struct inode *ino)
702 {
703         struct pnfs_layout_hdr *lo;
704         struct pnfs_layout_segment *lseg, *tmp;
705         LIST_HEAD(tmp_list);
706         bool found = false;
707
708         spin_lock(&ino->i_lock);
709         lo = NFS_I(ino)->layout;
710         if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
711             test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
712                 goto out_nolayout;
713         list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
714                 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
715                         mark_lseg_invalid(lseg, &tmp_list);
716                         found = true;
717                 }
718         if (!found)
719                 goto out_nolayout;
720         lo->plh_block_lgets++;
721         get_layout_hdr(lo); /* matched in pnfs_roc_release */
722         spin_unlock(&ino->i_lock);
723         pnfs_free_lseg_list(&tmp_list);
724         return true;
725
726 out_nolayout:
727         spin_unlock(&ino->i_lock);
728         return false;
729 }
730
731 void pnfs_roc_release(struct inode *ino)
732 {
733         struct pnfs_layout_hdr *lo;
734
735         spin_lock(&ino->i_lock);
736         lo = NFS_I(ino)->layout;
737         lo->plh_block_lgets--;
738         put_layout_hdr_locked(lo);
739         spin_unlock(&ino->i_lock);
740 }
741
742 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
743 {
744         struct pnfs_layout_hdr *lo;
745
746         spin_lock(&ino->i_lock);
747         lo = NFS_I(ino)->layout;
748         if ((int)(barrier - lo->plh_barrier) > 0)
749                 lo->plh_barrier = barrier;
750         spin_unlock(&ino->i_lock);
751 }
752
753 bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
754 {
755         struct nfs_inode *nfsi = NFS_I(ino);
756         struct pnfs_layout_segment *lseg;
757         bool found = false;
758
759         spin_lock(&ino->i_lock);
760         list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
761                 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
762                         found = true;
763                         break;
764                 }
765         if (!found) {
766                 struct pnfs_layout_hdr *lo = nfsi->layout;
767                 u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
768
769                 /* Since close does not return a layout stateid for use as
770                  * a barrier, we choose the worst-case barrier.
771                  */
772                 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
773         }
774         spin_unlock(&ino->i_lock);
775         return found;
776 }
777
778 /*
779  * Compare two layout segments for sorting into layout cache.
780  * We want to preferentially return RW over RO layouts, so ensure those
781  * are seen first.
782  */
783 static s64
784 cmp_layout(struct pnfs_layout_range *l1,
785            struct pnfs_layout_range *l2)
786 {
787         s64 d;
788
789         /* high offset > low offset */
790         d = l1->offset - l2->offset;
791         if (d)
792                 return d;
793
794         /* short length > long length */
795         d = l2->length - l1->length;
796         if (d)
797                 return d;
798
799         /* read > read/write */
800         return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
801 }
802
803 static void
804 pnfs_insert_layout(struct pnfs_layout_hdr *lo,
805                    struct pnfs_layout_segment *lseg)
806 {
807         struct pnfs_layout_segment *lp;
808
809         dprintk("%s:Begin\n", __func__);
810
811         assert_spin_locked(&lo->plh_inode->i_lock);
812         list_for_each_entry(lp, &lo->plh_segs, pls_list) {
813                 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
814                         continue;
815                 list_add_tail(&lseg->pls_list, &lp->pls_list);
816                 dprintk("%s: inserted lseg %p "
817                         "iomode %d offset %llu length %llu before "
818                         "lp %p iomode %d offset %llu length %llu\n",
819                         __func__, lseg, lseg->pls_range.iomode,
820                         lseg->pls_range.offset, lseg->pls_range.length,
821                         lp, lp->pls_range.iomode, lp->pls_range.offset,
822                         lp->pls_range.length);
823                 goto out;
824         }
825         list_add_tail(&lseg->pls_list, &lo->plh_segs);
826         dprintk("%s: inserted lseg %p "
827                 "iomode %d offset %llu length %llu at tail\n",
828                 __func__, lseg, lseg->pls_range.iomode,
829                 lseg->pls_range.offset, lseg->pls_range.length);
830 out:
831         get_layout_hdr(lo);
832
833         dprintk("%s:Return\n", __func__);
834 }
835
836 static struct pnfs_layout_hdr *
837 alloc_init_layout_hdr(struct inode *ino,
838                       struct nfs_open_context *ctx,
839                       gfp_t gfp_flags)
840 {
841         struct pnfs_layout_hdr *lo;
842
843         lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
844         if (!lo)
845                 return NULL;
846         atomic_set(&lo->plh_refcount, 1);
847         INIT_LIST_HEAD(&lo->plh_layouts);
848         INIT_LIST_HEAD(&lo->plh_segs);
849         INIT_LIST_HEAD(&lo->plh_bulk_recall);
850         lo->plh_inode = ino;
851         lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
852         return lo;
853 }
854
855 static struct pnfs_layout_hdr *
856 pnfs_find_alloc_layout(struct inode *ino,
857                        struct nfs_open_context *ctx,
858                        gfp_t gfp_flags)
859 {
860         struct nfs_inode *nfsi = NFS_I(ino);
861         struct pnfs_layout_hdr *new = NULL;
862
863         dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
864
865         assert_spin_locked(&ino->i_lock);
866         if (nfsi->layout) {
867                 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
868                         return NULL;
869                 else
870                         return nfsi->layout;
871         }
872         spin_unlock(&ino->i_lock);
873         new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
874         spin_lock(&ino->i_lock);
875
876         if (likely(nfsi->layout == NULL))       /* Won the race? */
877                 nfsi->layout = new;
878         else
879                 pnfs_free_layout_hdr(new);
880         return nfsi->layout;
881 }
882
883 /*
884  * iomode matching rules:
885  * iomode       lseg    match
886  * -----        -----   -----
887  * ANY          READ    true
888  * ANY          RW      true
889  * RW           READ    false
890  * RW           RW      true
891  * READ         READ    true
892  * READ         RW      true
893  */
894 static int
895 is_matching_lseg(struct pnfs_layout_range *ls_range,
896                  struct pnfs_layout_range *range)
897 {
898         struct pnfs_layout_range range1;
899
900         if ((range->iomode == IOMODE_RW &&
901              ls_range->iomode != IOMODE_RW) ||
902             !lo_seg_intersecting(ls_range, range))
903                 return 0;
904
905         /* range1 covers only the first byte in the range */
906         range1 = *range;
907         range1.length = 1;
908         return lo_seg_contained(ls_range, &range1);
909 }
910
911 /*
912  * lookup range in layout
913  */
914 static struct pnfs_layout_segment *
915 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
916                 struct pnfs_layout_range *range)
917 {
918         struct pnfs_layout_segment *lseg, *ret = NULL;
919
920         dprintk("%s:Begin\n", __func__);
921
922         assert_spin_locked(&lo->plh_inode->i_lock);
923         list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
924                 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
925                     is_matching_lseg(&lseg->pls_range, range)) {
926                         ret = get_lseg(lseg);
927                         break;
928                 }
929                 if (lseg->pls_range.offset > range->offset)
930                         break;
931         }
932
933         dprintk("%s:Return lseg %p ref %d\n",
934                 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
935         return ret;
936 }
937
938 /*
939  * Use mdsthreshold hints set at each OPEN to determine if I/O should go
940  * to the MDS or over pNFS
941  *
942  * The nfs_inode read_io and write_io fields are cumulative counters reset
943  * when there are no layout segments. Note that in pnfs_update_layout iomode
944  * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
945  * WRITE request.
946  *
947  * A return of true means use MDS I/O.
948  *
949  * From rfc 5661:
950  * If a file's size is smaller than the file size threshold, data accesses
951  * SHOULD be sent to the metadata server.  If an I/O request has a length that
952  * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
953  * server.  If both file size and I/O size are provided, the client SHOULD
954  * reach or exceed  both thresholds before sending its read or write
955  * requests to the data server.
956  */
957 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
958                                      struct inode *ino, int iomode)
959 {
960         struct nfs4_threshold *t = ctx->mdsthreshold;
961         struct nfs_inode *nfsi = NFS_I(ino);
962         loff_t fsize = i_size_read(ino);
963         bool size = false, size_set = false, io = false, io_set = false, ret = false;
964
965         if (t == NULL)
966                 return ret;
967
968         dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
969                 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
970
971         switch (iomode) {
972         case IOMODE_READ:
973                 if (t->bm & THRESHOLD_RD) {
974                         dprintk("%s fsize %llu\n", __func__, fsize);
975                         size_set = true;
976                         if (fsize < t->rd_sz)
977                                 size = true;
978                 }
979                 if (t->bm & THRESHOLD_RD_IO) {
980                         dprintk("%s nfsi->read_io %llu\n", __func__,
981                                 nfsi->read_io);
982                         io_set = true;
983                         if (nfsi->read_io < t->rd_io_sz)
984                                 io = true;
985                 }
986                 break;
987         case IOMODE_RW:
988                 if (t->bm & THRESHOLD_WR) {
989                         dprintk("%s fsize %llu\n", __func__, fsize);
990                         size_set = true;
991                         if (fsize < t->wr_sz)
992                                 size = true;
993                 }
994                 if (t->bm & THRESHOLD_WR_IO) {
995                         dprintk("%s nfsi->write_io %llu\n", __func__,
996                                 nfsi->write_io);
997                         io_set = true;
998                         if (nfsi->write_io < t->wr_io_sz)
999                                 io = true;
1000                 }
1001                 break;
1002         }
1003         if (size_set && io_set) {
1004                 if (size && io)
1005                         ret = true;
1006         } else if (size || io)
1007                 ret = true;
1008
1009         dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1010         return ret;
1011 }
1012
1013 /*
1014  * Layout segment is retreived from the server if not cached.
1015  * The appropriate layout segment is referenced and returned to the caller.
1016  */
1017 struct pnfs_layout_segment *
1018 pnfs_update_layout(struct inode *ino,
1019                    struct nfs_open_context *ctx,
1020                    loff_t pos,
1021                    u64 count,
1022                    enum pnfs_iomode iomode,
1023                    gfp_t gfp_flags)
1024 {
1025         struct pnfs_layout_range arg = {
1026                 .iomode = iomode,
1027                 .offset = pos,
1028                 .length = count,
1029         };
1030         unsigned pg_offset;
1031         struct nfs_inode *nfsi = NFS_I(ino);
1032         struct nfs_server *server = NFS_SERVER(ino);
1033         struct nfs_client *clp = server->nfs_client;
1034         struct pnfs_layout_hdr *lo;
1035         struct pnfs_layout_segment *lseg = NULL;
1036         bool first = false;
1037
1038         if (!pnfs_enabled_sb(NFS_SERVER(ino)))
1039                 return NULL;
1040
1041         if (pnfs_within_mdsthreshold(ctx, ino, iomode))
1042                 return NULL;
1043
1044         spin_lock(&ino->i_lock);
1045         lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1046         if (lo == NULL) {
1047                 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
1048                 goto out_unlock;
1049         }
1050
1051         /* Do we even need to bother with this? */
1052         if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1053                 dprintk("%s matches recall, use MDS\n", __func__);
1054                 goto out_unlock;
1055         }
1056
1057         /* if LAYOUTGET already failed once we don't try again */
1058         if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
1059                 goto out_unlock;
1060
1061         /* Check to see if the layout for the given range already exists */
1062         lseg = pnfs_find_lseg(lo, &arg);
1063         if (lseg)
1064                 goto out_unlock;
1065
1066         if (pnfs_layoutgets_blocked(lo, NULL, 0))
1067                 goto out_unlock;
1068         atomic_inc(&lo->plh_outstanding);
1069
1070         get_layout_hdr(lo);
1071         if (list_empty(&lo->plh_segs))
1072                 first = true;
1073         spin_unlock(&ino->i_lock);
1074         if (first) {
1075                 /* The lo must be on the clp list if there is any
1076                  * chance of a CB_LAYOUTRECALL(FILE) coming in.
1077                  */
1078                 spin_lock(&clp->cl_lock);
1079                 BUG_ON(!list_empty(&lo->plh_layouts));
1080                 list_add_tail(&lo->plh_layouts, &server->layouts);
1081                 spin_unlock(&clp->cl_lock);
1082         }
1083
1084         pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1085         if (pg_offset) {
1086                 arg.offset -= pg_offset;
1087                 arg.length += pg_offset;
1088         }
1089         if (arg.length != NFS4_MAX_UINT64)
1090                 arg.length = PAGE_CACHE_ALIGN(arg.length);
1091
1092         lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
1093         if (!lseg && first) {
1094                 spin_lock(&clp->cl_lock);
1095                 list_del_init(&lo->plh_layouts);
1096                 spin_unlock(&clp->cl_lock);
1097         }
1098         atomic_dec(&lo->plh_outstanding);
1099         put_layout_hdr(lo);
1100 out:
1101         dprintk("%s end, state 0x%lx lseg %p\n", __func__,
1102                 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
1103         return lseg;
1104 out_unlock:
1105         spin_unlock(&ino->i_lock);
1106         goto out;
1107 }
1108 EXPORT_SYMBOL_GPL(pnfs_update_layout);
1109
1110 int
1111 pnfs_layout_process(struct nfs4_layoutget *lgp)
1112 {
1113         struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1114         struct nfs4_layoutget_res *res = &lgp->res;
1115         struct pnfs_layout_segment *lseg;
1116         struct inode *ino = lo->plh_inode;
1117         int status = 0;
1118
1119         /* Inject layout blob into I/O device driver */
1120         lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1121         if (!lseg || IS_ERR(lseg)) {
1122                 if (!lseg)
1123                         status = -ENOMEM;
1124                 else
1125                         status = PTR_ERR(lseg);
1126                 dprintk("%s: Could not allocate layout: error %d\n",
1127                        __func__, status);
1128                 goto out;
1129         }
1130
1131         spin_lock(&ino->i_lock);
1132         if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1133                 dprintk("%s forget reply due to recall\n", __func__);
1134                 goto out_forget_reply;
1135         }
1136
1137         if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1138                 dprintk("%s forget reply due to state\n", __func__);
1139                 goto out_forget_reply;
1140         }
1141         init_lseg(lo, lseg);
1142         lseg->pls_range = res->range;
1143         *lgp->lsegpp = get_lseg(lseg);
1144         pnfs_insert_layout(lo, lseg);
1145
1146         if (res->return_on_close) {
1147                 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1148                 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1149         }
1150
1151         /* Done processing layoutget. Set the layout stateid */
1152         pnfs_set_layout_stateid(lo, &res->stateid, false);
1153         spin_unlock(&ino->i_lock);
1154 out:
1155         return status;
1156
1157 out_forget_reply:
1158         spin_unlock(&ino->i_lock);
1159         lseg->pls_layout = lo;
1160         NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1161         goto out;
1162 }
1163
1164 void
1165 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1166 {
1167         BUG_ON(pgio->pg_lseg != NULL);
1168
1169         if (req->wb_offset != req->wb_pgbase) {
1170                 nfs_pageio_reset_read_mds(pgio);
1171                 return;
1172         }
1173         pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1174                                            req->wb_context,
1175                                            req_offset(req),
1176                                            req->wb_bytes,
1177                                            IOMODE_READ,
1178                                            GFP_KERNEL);
1179         /* If no lseg, fall back to read through mds */
1180         if (pgio->pg_lseg == NULL)
1181                 nfs_pageio_reset_read_mds(pgio);
1182
1183 }
1184 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1185
1186 void
1187 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1188 {
1189         BUG_ON(pgio->pg_lseg != NULL);
1190
1191         if (req->wb_offset != req->wb_pgbase) {
1192                 nfs_pageio_reset_write_mds(pgio);
1193                 return;
1194         }
1195         pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1196                                            req->wb_context,
1197                                            req_offset(req),
1198                                            req->wb_bytes,
1199                                            IOMODE_RW,
1200                                            GFP_NOFS);
1201         /* If no lseg, fall back to write through mds */
1202         if (pgio->pg_lseg == NULL)
1203                 nfs_pageio_reset_write_mds(pgio);
1204 }
1205 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1206
1207 bool
1208 pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1209                       const struct nfs_pgio_completion_ops *compl_ops)
1210 {
1211         struct nfs_server *server = NFS_SERVER(inode);
1212         struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1213
1214         if (ld == NULL)
1215                 return false;
1216         nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops,
1217                         server->rsize, 0);
1218         return true;
1219 }
1220
1221 bool
1222 pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1223                        int ioflags,
1224                        const struct nfs_pgio_completion_ops *compl_ops)
1225 {
1226         struct nfs_server *server = NFS_SERVER(inode);
1227         struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1228
1229         if (ld == NULL)
1230                 return false;
1231         nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops,
1232                         server->wsize, ioflags);
1233         return true;
1234 }
1235
1236 bool
1237 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1238                      struct nfs_page *req)
1239 {
1240         if (pgio->pg_lseg == NULL)
1241                 return nfs_generic_pg_test(pgio, prev, req);
1242
1243         /*
1244          * Test if a nfs_page is fully contained in the pnfs_layout_range.
1245          * Note that this test makes several assumptions:
1246          * - that the previous nfs_page in the struct nfs_pageio_descriptor
1247          *   is known to lie within the range.
1248          *   - that the nfs_page being tested is known to be contiguous with the
1249          *   previous nfs_page.
1250          *   - Layout ranges are page aligned, so we only have to test the
1251          *   start offset of the request.
1252          *
1253          * Please also note that 'end_offset' is actually the offset of the
1254          * first byte that lies outside the pnfs_layout_range. FIXME?
1255          *
1256          */
1257         return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1258                                          pgio->pg_lseg->pls_range.length);
1259 }
1260 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
1261
1262 int pnfs_write_done_resend_to_mds(struct inode *inode,
1263                                 struct list_head *head,
1264                                 const struct nfs_pgio_completion_ops *compl_ops)
1265 {
1266         struct nfs_pageio_descriptor pgio;
1267         LIST_HEAD(failed);
1268
1269         /* Resend all requests through the MDS */
1270         nfs_pageio_init_write_mds(&pgio, inode, FLUSH_STABLE, compl_ops);
1271         while (!list_empty(head)) {
1272                 struct nfs_page *req = nfs_list_entry(head->next);
1273
1274                 nfs_list_remove_request(req);
1275                 if (!nfs_pageio_add_request(&pgio, req))
1276                         nfs_list_add_request(req, &failed);
1277         }
1278         nfs_pageio_complete(&pgio);
1279
1280         if (!list_empty(&failed)) {
1281                 /* For some reason our attempt to resend pages. Mark the
1282                  * overall send request as having failed, and let
1283                  * nfs_writeback_release_full deal with the error.
1284                  */
1285                 list_move(&failed, head);
1286                 return -EIO;
1287         }
1288         return 0;
1289 }
1290 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
1291
1292 static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
1293 {
1294         struct nfs_pgio_header *hdr = data->header;
1295
1296         dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1297         if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1298             PNFS_LAYOUTRET_ON_ERROR) {
1299                 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1300                 pnfs_return_layout(hdr->inode);
1301         }
1302         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1303                 data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
1304                                                         &hdr->pages,
1305                                                         hdr->completion_ops);
1306 }
1307
1308 /*
1309  * Called by non rpc-based layout drivers
1310  */
1311 void pnfs_ld_write_done(struct nfs_write_data *data)
1312 {
1313         struct nfs_pgio_header *hdr = data->header;
1314
1315         if (!hdr->pnfs_error) {
1316                 pnfs_set_layoutcommit(data);
1317                 hdr->mds_ops->rpc_call_done(&data->task, data);
1318         } else
1319                 pnfs_ld_handle_write_error(data);
1320         hdr->mds_ops->rpc_release(data);
1321 }
1322 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
1323
1324 static void
1325 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1326                 struct nfs_write_data *data)
1327 {
1328         struct nfs_pgio_header *hdr = data->header;
1329
1330         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1331                 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1332                 nfs_pageio_reset_write_mds(desc);
1333                 desc->pg_recoalesce = 1;
1334         }
1335         nfs_writedata_release(data);
1336 }
1337
1338 static enum pnfs_try_status
1339 pnfs_try_to_write_data(struct nfs_write_data *wdata,
1340                         const struct rpc_call_ops *call_ops,
1341                         struct pnfs_layout_segment *lseg,
1342                         int how)
1343 {
1344         struct nfs_pgio_header *hdr = wdata->header;
1345         struct inode *inode = hdr->inode;
1346         enum pnfs_try_status trypnfs;
1347         struct nfs_server *nfss = NFS_SERVER(inode);
1348
1349         hdr->mds_ops = call_ops;
1350
1351         dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1352                 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1353         trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1354         if (trypnfs != PNFS_NOT_ATTEMPTED)
1355                 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1356         dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1357         return trypnfs;
1358 }
1359
1360 static void
1361 pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1362 {
1363         struct nfs_write_data *data;
1364         const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1365         struct pnfs_layout_segment *lseg = desc->pg_lseg;
1366
1367         desc->pg_lseg = NULL;
1368         while (!list_empty(head)) {
1369                 enum pnfs_try_status trypnfs;
1370
1371                 data = list_first_entry(head, struct nfs_write_data, list);
1372                 list_del_init(&data->list);
1373
1374                 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1375                 if (trypnfs == PNFS_NOT_ATTEMPTED)
1376                         pnfs_write_through_mds(desc, data);
1377         }
1378         put_lseg(lseg);
1379 }
1380
1381 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1382 {
1383         put_lseg(hdr->lseg);
1384         nfs_writehdr_free(hdr);
1385 }
1386
1387 int
1388 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1389 {
1390         struct nfs_write_header *whdr;
1391         struct nfs_pgio_header *hdr;
1392         int ret;
1393
1394         whdr = nfs_writehdr_alloc();
1395         if (!whdr) {
1396                 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1397                 put_lseg(desc->pg_lseg);
1398                 desc->pg_lseg = NULL;
1399                 return -ENOMEM;
1400         }
1401         hdr = &whdr->header;
1402         nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
1403         hdr->lseg = get_lseg(desc->pg_lseg);
1404         atomic_inc(&hdr->refcnt);
1405         ret = nfs_generic_flush(desc, hdr);
1406         if (ret != 0) {
1407                 put_lseg(desc->pg_lseg);
1408                 desc->pg_lseg = NULL;
1409         } else
1410                 pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
1411         if (atomic_dec_and_test(&hdr->refcnt))
1412                 hdr->completion_ops->completion(hdr);
1413         return ret;
1414 }
1415 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1416
1417 int pnfs_read_done_resend_to_mds(struct inode *inode,
1418                                 struct list_head *head,
1419                                 const struct nfs_pgio_completion_ops *compl_ops)
1420 {
1421         struct nfs_pageio_descriptor pgio;
1422         LIST_HEAD(failed);
1423
1424         /* Resend all requests through the MDS */
1425         nfs_pageio_init_read_mds(&pgio, inode, compl_ops);
1426         while (!list_empty(head)) {
1427                 struct nfs_page *req = nfs_list_entry(head->next);
1428
1429                 nfs_list_remove_request(req);
1430                 if (!nfs_pageio_add_request(&pgio, req))
1431                         nfs_list_add_request(req, &failed);
1432         }
1433         nfs_pageio_complete(&pgio);
1434
1435         if (!list_empty(&failed)) {
1436                 list_move(&failed, head);
1437                 return -EIO;
1438         }
1439         return 0;
1440 }
1441 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
1442
1443 static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
1444 {
1445         struct nfs_pgio_header *hdr = data->header;
1446
1447         dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1448         if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
1449             PNFS_LAYOUTRET_ON_ERROR) {
1450                 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1451                 pnfs_return_layout(hdr->inode);
1452         }
1453         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1454                 data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
1455                                                         &hdr->pages,
1456                                                         hdr->completion_ops);
1457 }
1458
1459 /*
1460  * Called by non rpc-based layout drivers
1461  */
1462 void pnfs_ld_read_done(struct nfs_read_data *data)
1463 {
1464         struct nfs_pgio_header *hdr = data->header;
1465
1466         if (likely(!hdr->pnfs_error)) {
1467                 __nfs4_read_done_cb(data);
1468                 hdr->mds_ops->rpc_call_done(&data->task, data);
1469         } else
1470                 pnfs_ld_handle_read_error(data);
1471         hdr->mds_ops->rpc_release(data);
1472 }
1473 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1474
1475 static void
1476 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1477                 struct nfs_read_data *data)
1478 {
1479         struct nfs_pgio_header *hdr = data->header;
1480
1481         if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1482                 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1483                 nfs_pageio_reset_read_mds(desc);
1484                 desc->pg_recoalesce = 1;
1485         }
1486         nfs_readdata_release(data);
1487 }
1488
1489 /*
1490  * Call the appropriate parallel I/O subsystem read function.
1491  */
1492 static enum pnfs_try_status
1493 pnfs_try_to_read_data(struct nfs_read_data *rdata,
1494                        const struct rpc_call_ops *call_ops,
1495                        struct pnfs_layout_segment *lseg)
1496 {
1497         struct nfs_pgio_header *hdr = rdata->header;
1498         struct inode *inode = hdr->inode;
1499         struct nfs_server *nfss = NFS_SERVER(inode);
1500         enum pnfs_try_status trypnfs;
1501
1502         hdr->mds_ops = call_ops;
1503
1504         dprintk("%s: Reading ino:%lu %u@%llu\n",
1505                 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1506
1507         trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1508         if (trypnfs != PNFS_NOT_ATTEMPTED)
1509                 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1510         dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1511         return trypnfs;
1512 }
1513
1514 static void
1515 pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1516 {
1517         struct nfs_read_data *data;
1518         const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1519         struct pnfs_layout_segment *lseg = desc->pg_lseg;
1520
1521         desc->pg_lseg = NULL;
1522         while (!list_empty(head)) {
1523                 enum pnfs_try_status trypnfs;
1524
1525                 data = list_first_entry(head, struct nfs_read_data, list);
1526                 list_del_init(&data->list);
1527
1528                 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1529                 if (trypnfs == PNFS_NOT_ATTEMPTED)
1530                         pnfs_read_through_mds(desc, data);
1531         }
1532         put_lseg(lseg);
1533 }
1534
1535 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
1536 {
1537         put_lseg(hdr->lseg);
1538         nfs_readhdr_free(hdr);
1539 }
1540
1541 int
1542 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1543 {
1544         struct nfs_read_header *rhdr;
1545         struct nfs_pgio_header *hdr;
1546         int ret;
1547
1548         rhdr = nfs_readhdr_alloc();
1549         if (!rhdr) {
1550                 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1551                 ret = -ENOMEM;
1552                 put_lseg(desc->pg_lseg);
1553                 desc->pg_lseg = NULL;
1554                 return ret;
1555         }
1556         hdr = &rhdr->header;
1557         nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
1558         hdr->lseg = get_lseg(desc->pg_lseg);
1559         atomic_inc(&hdr->refcnt);
1560         ret = nfs_generic_pagein(desc, hdr);
1561         if (ret != 0) {
1562                 put_lseg(desc->pg_lseg);
1563                 desc->pg_lseg = NULL;
1564         } else
1565                 pnfs_do_multiple_reads(desc, &hdr->rpc_list);
1566         if (atomic_dec_and_test(&hdr->refcnt))
1567                 hdr->completion_ops->completion(hdr);
1568         return ret;
1569 }
1570 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1571
1572 /*
1573  * There can be multiple RW segments.
1574  */
1575 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
1576 {
1577         struct pnfs_layout_segment *lseg;
1578
1579         list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1580                 if (lseg->pls_range.iomode == IOMODE_RW &&
1581                     test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1582                         list_add(&lseg->pls_lc_list, listp);
1583         }
1584 }
1585
1586 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
1587 {
1588         if (lseg->pls_range.iomode == IOMODE_RW) {
1589                 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
1590                 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
1591         } else {
1592                 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
1593                 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
1594         }
1595 }
1596 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
1597
1598 void
1599 pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1600 {
1601         struct nfs_pgio_header *hdr = wdata->header;
1602         struct inode *inode = hdr->inode;
1603         struct nfs_inode *nfsi = NFS_I(inode);
1604         loff_t end_pos = wdata->mds_offset + wdata->res.count;
1605         bool mark_as_dirty = false;
1606
1607         spin_lock(&inode->i_lock);
1608         if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1609                 mark_as_dirty = true;
1610                 dprintk("%s: Set layoutcommit for inode %lu ",
1611                         __func__, inode->i_ino);
1612         }
1613         if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
1614                 /* references matched in nfs4_layoutcommit_release */
1615                 get_lseg(hdr->lseg);
1616         }
1617         if (end_pos > nfsi->layout->plh_lwb)
1618                 nfsi->layout->plh_lwb = end_pos;
1619         spin_unlock(&inode->i_lock);
1620         dprintk("%s: lseg %p end_pos %llu\n",
1621                 __func__, hdr->lseg, nfsi->layout->plh_lwb);
1622
1623         /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1624          * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1625         if (mark_as_dirty)
1626                 mark_inode_dirty_sync(inode);
1627 }
1628 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1629
1630 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
1631 {
1632         struct nfs_server *nfss = NFS_SERVER(data->args.inode);
1633
1634         if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1635                 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1636 }
1637
1638 /*
1639  * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1640  * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1641  * data to disk to allow the server to recover the data if it crashes.
1642  * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1643  * is off, and a COMMIT is sent to a data server, or
1644  * if WRITEs to a data server return NFS_DATA_SYNC.
1645  */
1646 int
1647 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
1648 {
1649         struct nfs4_layoutcommit_data *data;
1650         struct nfs_inode *nfsi = NFS_I(inode);
1651         loff_t end_pos;
1652         int status = 0;
1653
1654         dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1655
1656         if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1657                 return 0;
1658
1659         /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1660         data = kzalloc(sizeof(*data), GFP_NOFS);
1661         if (!data) {
1662                 status = -ENOMEM;
1663                 goto out;
1664         }
1665
1666         if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1667                 goto out_free;
1668
1669         if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
1670                 if (!sync) {
1671                         status = -EAGAIN;
1672                         goto out_free;
1673                 }
1674                 status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
1675                                         nfs_wait_bit_killable, TASK_KILLABLE);
1676                 if (status)
1677                         goto out_free;
1678         }
1679
1680         INIT_LIST_HEAD(&data->lseg_list);
1681         spin_lock(&inode->i_lock);
1682         if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1683                 clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
1684                 spin_unlock(&inode->i_lock);
1685                 wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
1686                 goto out_free;
1687         }
1688
1689         pnfs_list_write_lseg(inode, &data->lseg_list);
1690
1691         end_pos = nfsi->layout->plh_lwb;
1692         nfsi->layout->plh_lwb = 0;
1693
1694         nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
1695         spin_unlock(&inode->i_lock);
1696
1697         data->args.inode = inode;
1698         data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
1699         nfs_fattr_init(&data->fattr);
1700         data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1701         data->res.fattr = &data->fattr;
1702         data->args.lastbytewritten = end_pos - 1;
1703         data->res.server = NFS_SERVER(inode);
1704
1705         status = nfs4_proc_layoutcommit(data, sync);
1706 out:
1707         if (status)
1708                 mark_inode_dirty_sync(inode);
1709         dprintk("<-- %s status %d\n", __func__, status);
1710         return status;
1711 out_free:
1712         kfree(data);
1713         goto out;
1714 }
1715
1716 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
1717 {
1718         struct nfs4_threshold *thp;
1719
1720         thp = kzalloc(sizeof(*thp), GFP_NOFS);
1721         if (!thp) {
1722                 dprintk("%s mdsthreshold allocation failed\n", __func__);
1723                 return NULL;
1724         }
1725         return thp;
1726 }