65455f58b10927d3c63551697d7de5b3b0068204
[pandora-kernel.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 "internal.h"
32 #include "pnfs.h"
33 #include "iostat.h"
34
35 #define NFSDBG_FACILITY         NFSDBG_PNFS
36
37 /* Locking:
38  *
39  * pnfs_spinlock:
40  *      protects pnfs_modules_tbl.
41  */
42 static DEFINE_SPINLOCK(pnfs_spinlock);
43
44 /*
45  * pnfs_modules_tbl holds all pnfs modules
46  */
47 static LIST_HEAD(pnfs_modules_tbl);
48
49 /* Return the registered pnfs layout driver module matching given id */
50 static struct pnfs_layoutdriver_type *
51 find_pnfs_driver_locked(u32 id)
52 {
53         struct pnfs_layoutdriver_type *local;
54
55         list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
56                 if (local->id == id)
57                         goto out;
58         local = NULL;
59 out:
60         dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
61         return local;
62 }
63
64 static struct pnfs_layoutdriver_type *
65 find_pnfs_driver(u32 id)
66 {
67         struct pnfs_layoutdriver_type *local;
68
69         spin_lock(&pnfs_spinlock);
70         local = find_pnfs_driver_locked(id);
71         spin_unlock(&pnfs_spinlock);
72         return local;
73 }
74
75 void
76 unset_pnfs_layoutdriver(struct nfs_server *nfss)
77 {
78         if (nfss->pnfs_curr_ld)
79                 module_put(nfss->pnfs_curr_ld->owner);
80         nfss->pnfs_curr_ld = NULL;
81 }
82
83 /*
84  * Try to set the server's pnfs module to the pnfs layout type specified by id.
85  * Currently only one pNFS layout driver per filesystem is supported.
86  *
87  * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
88  */
89 void
90 set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
91 {
92         struct pnfs_layoutdriver_type *ld_type = NULL;
93
94         if (id == 0)
95                 goto out_no_driver;
96         if (!(server->nfs_client->cl_exchange_flags &
97                  (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
98                 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
99                        id, server->nfs_client->cl_exchange_flags);
100                 goto out_no_driver;
101         }
102         ld_type = find_pnfs_driver(id);
103         if (!ld_type) {
104                 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
105                 ld_type = find_pnfs_driver(id);
106                 if (!ld_type) {
107                         dprintk("%s: No pNFS module found for %u.\n",
108                                 __func__, id);
109                         goto out_no_driver;
110                 }
111         }
112         if (!try_module_get(ld_type->owner)) {
113                 dprintk("%s: Could not grab reference on module\n", __func__);
114                 goto out_no_driver;
115         }
116         server->pnfs_curr_ld = ld_type;
117
118         dprintk("%s: pNFS module for %u set\n", __func__, id);
119         return;
120
121 out_no_driver:
122         dprintk("%s: Using NFSv4 I/O\n", __func__);
123         server->pnfs_curr_ld = NULL;
124 }
125
126 int
127 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
128 {
129         int status = -EINVAL;
130         struct pnfs_layoutdriver_type *tmp;
131
132         if (ld_type->id == 0) {
133                 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
134                 return status;
135         }
136         if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
137                 printk(KERN_ERR "%s Layout driver must provide "
138                        "alloc_lseg and free_lseg.\n", __func__);
139                 return status;
140         }
141
142         spin_lock(&pnfs_spinlock);
143         tmp = find_pnfs_driver_locked(ld_type->id);
144         if (!tmp) {
145                 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
146                 status = 0;
147                 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
148                         ld_type->name);
149         } else {
150                 printk(KERN_ERR "%s Module with id %d already loaded!\n",
151                         __func__, ld_type->id);
152         }
153         spin_unlock(&pnfs_spinlock);
154
155         return status;
156 }
157 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
158
159 void
160 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
161 {
162         dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
163         spin_lock(&pnfs_spinlock);
164         list_del(&ld_type->pnfs_tblid);
165         spin_unlock(&pnfs_spinlock);
166 }
167 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
168
169 /*
170  * pNFS client layout cache
171  */
172
173 /* Need to hold i_lock if caller does not already hold reference */
174 void
175 get_layout_hdr(struct pnfs_layout_hdr *lo)
176 {
177         atomic_inc(&lo->plh_refcount);
178 }
179
180 static void
181 destroy_layout_hdr(struct pnfs_layout_hdr *lo)
182 {
183         dprintk("%s: freeing layout cache %p\n", __func__, lo);
184         BUG_ON(!list_empty(&lo->plh_layouts));
185         NFS_I(lo->plh_inode)->layout = NULL;
186         kfree(lo);
187 }
188
189 static void
190 put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
191 {
192         if (atomic_dec_and_test(&lo->plh_refcount))
193                 destroy_layout_hdr(lo);
194 }
195
196 void
197 put_layout_hdr(struct pnfs_layout_hdr *lo)
198 {
199         struct inode *inode = lo->plh_inode;
200
201         if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
202                 destroy_layout_hdr(lo);
203                 spin_unlock(&inode->i_lock);
204         }
205 }
206
207 static void
208 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
209 {
210         INIT_LIST_HEAD(&lseg->pls_list);
211         atomic_set(&lseg->pls_refcount, 1);
212         smp_mb();
213         set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
214         lseg->pls_layout = lo;
215 }
216
217 static void free_lseg(struct pnfs_layout_segment *lseg)
218 {
219         struct inode *ino = lseg->pls_layout->plh_inode;
220
221         NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
222         /* Matched by get_layout_hdr in pnfs_insert_layout */
223         put_layout_hdr(NFS_I(ino)->layout);
224 }
225
226 static void
227 put_lseg_common(struct pnfs_layout_segment *lseg)
228 {
229         struct inode *inode = lseg->pls_layout->plh_inode;
230
231         BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
232         list_del_init(&lseg->pls_list);
233         if (list_empty(&lseg->pls_layout->plh_segs)) {
234                 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
235                 /* Matched by initial refcount set in alloc_init_layout_hdr */
236                 put_layout_hdr_locked(lseg->pls_layout);
237         }
238         rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
239 }
240
241 void
242 put_lseg(struct pnfs_layout_segment *lseg)
243 {
244         struct inode *inode;
245
246         if (!lseg)
247                 return;
248
249         dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
250                 atomic_read(&lseg->pls_refcount),
251                 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
252         inode = lseg->pls_layout->plh_inode;
253         if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
254                 LIST_HEAD(free_me);
255
256                 put_lseg_common(lseg);
257                 list_add(&lseg->pls_list, &free_me);
258                 spin_unlock(&inode->i_lock);
259                 pnfs_free_lseg_list(&free_me);
260         }
261 }
262 EXPORT_SYMBOL_GPL(put_lseg);
263
264 static bool
265 should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
266 {
267         return (recall_iomode == IOMODE_ANY ||
268                 lseg_iomode == recall_iomode);
269 }
270
271 /* Returns 1 if lseg is removed from list, 0 otherwise */
272 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
273                              struct list_head *tmp_list)
274 {
275         int rv = 0;
276
277         if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
278                 /* Remove the reference keeping the lseg in the
279                  * list.  It will now be removed when all
280                  * outstanding io is finished.
281                  */
282                 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
283                         atomic_read(&lseg->pls_refcount));
284                 if (atomic_dec_and_test(&lseg->pls_refcount)) {
285                         put_lseg_common(lseg);
286                         list_add(&lseg->pls_list, tmp_list);
287                         rv = 1;
288                 }
289         }
290         return rv;
291 }
292
293 /* Returns count of number of matching invalid lsegs remaining in list
294  * after call.
295  */
296 int
297 mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
298                             struct list_head *tmp_list,
299                             u32 iomode)
300 {
301         struct pnfs_layout_segment *lseg, *next;
302         int invalid = 0, removed = 0;
303
304         dprintk("%s:Begin lo %p\n", __func__, lo);
305
306         if (list_empty(&lo->plh_segs)) {
307                 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
308                         put_layout_hdr_locked(lo);
309                 return 0;
310         }
311         list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
312                 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
313                         dprintk("%s: freeing lseg %p iomode %d "
314                                 "offset %llu length %llu\n", __func__,
315                                 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
316                                 lseg->pls_range.length);
317                         invalid++;
318                         removed += mark_lseg_invalid(lseg, tmp_list);
319                 }
320         dprintk("%s:Return %i\n", __func__, invalid - removed);
321         return invalid - removed;
322 }
323
324 /* note free_me must contain lsegs from a single layout_hdr */
325 void
326 pnfs_free_lseg_list(struct list_head *free_me)
327 {
328         struct pnfs_layout_segment *lseg, *tmp;
329         struct pnfs_layout_hdr *lo;
330
331         if (list_empty(free_me))
332                 return;
333
334         lo = list_first_entry(free_me, struct pnfs_layout_segment,
335                               pls_list)->pls_layout;
336
337         if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
338                 struct nfs_client *clp;
339
340                 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
341                 spin_lock(&clp->cl_lock);
342                 list_del_init(&lo->plh_layouts);
343                 spin_unlock(&clp->cl_lock);
344         }
345         list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
346                 list_del(&lseg->pls_list);
347                 free_lseg(lseg);
348         }
349 }
350
351 void
352 pnfs_destroy_layout(struct nfs_inode *nfsi)
353 {
354         struct pnfs_layout_hdr *lo;
355         LIST_HEAD(tmp_list);
356
357         spin_lock(&nfsi->vfs_inode.i_lock);
358         lo = nfsi->layout;
359         if (lo) {
360                 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
361                 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
362         }
363         spin_unlock(&nfsi->vfs_inode.i_lock);
364         pnfs_free_lseg_list(&tmp_list);
365 }
366
367 /*
368  * Called by the state manger to remove all layouts established under an
369  * expired lease.
370  */
371 void
372 pnfs_destroy_all_layouts(struct nfs_client *clp)
373 {
374         struct pnfs_layout_hdr *lo;
375         LIST_HEAD(tmp_list);
376
377         spin_lock(&clp->cl_lock);
378         list_splice_init(&clp->cl_layouts, &tmp_list);
379         spin_unlock(&clp->cl_lock);
380
381         while (!list_empty(&tmp_list)) {
382                 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
383                                 plh_layouts);
384                 dprintk("%s freeing layout for inode %lu\n", __func__,
385                         lo->plh_inode->i_ino);
386                 list_del_init(&lo->plh_layouts);
387                 pnfs_destroy_layout(NFS_I(lo->plh_inode));
388         }
389 }
390
391 /* update lo->plh_stateid with new if is more recent */
392 void
393 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
394                         bool update_barrier)
395 {
396         u32 oldseq, newseq;
397
398         oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
399         newseq = be32_to_cpu(new->stateid.seqid);
400         if ((int)(newseq - oldseq) > 0) {
401                 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
402                 if (update_barrier) {
403                         u32 new_barrier = be32_to_cpu(new->stateid.seqid);
404
405                         if ((int)(new_barrier - lo->plh_barrier))
406                                 lo->plh_barrier = new_barrier;
407                 } else {
408                         /* Because of wraparound, we want to keep the barrier
409                          * "close" to the current seqids.  It needs to be
410                          * within 2**31 to count as "behind", so if it
411                          * gets too near that limit, give us a litle leeway
412                          * and bring it to within 2**30.
413                          * NOTE - and yes, this is all unsigned arithmetic.
414                          */
415                         if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
416                                 lo->plh_barrier = newseq - (1 << 30);
417                 }
418         }
419 }
420
421 /* lget is set to 1 if called from inside send_layoutget call chain */
422 static bool
423 pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
424                         int lget)
425 {
426         if ((stateid) &&
427             (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
428                 return true;
429         return lo->plh_block_lgets ||
430                 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
431                 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
432                 (list_empty(&lo->plh_segs) &&
433                  (atomic_read(&lo->plh_outstanding) > lget));
434 }
435
436 int
437 pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
438                               struct nfs4_state *open_state)
439 {
440         int status = 0;
441
442         dprintk("--> %s\n", __func__);
443         spin_lock(&lo->plh_inode->i_lock);
444         if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
445                 status = -EAGAIN;
446         } else if (list_empty(&lo->plh_segs)) {
447                 int seq;
448
449                 do {
450                         seq = read_seqbegin(&open_state->seqlock);
451                         memcpy(dst->data, open_state->stateid.data,
452                                sizeof(open_state->stateid.data));
453                 } while (read_seqretry(&open_state->seqlock, seq));
454         } else
455                 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
456         spin_unlock(&lo->plh_inode->i_lock);
457         dprintk("<-- %s\n", __func__);
458         return status;
459 }
460
461 /*
462 * Get layout from server.
463 *    for now, assume that whole file layouts are requested.
464 *    arg->offset: 0
465 *    arg->length: all ones
466 */
467 static struct pnfs_layout_segment *
468 send_layoutget(struct pnfs_layout_hdr *lo,
469            struct nfs_open_context *ctx,
470            u32 iomode)
471 {
472         struct inode *ino = lo->plh_inode;
473         struct nfs_server *server = NFS_SERVER(ino);
474         struct nfs4_layoutget *lgp;
475         struct pnfs_layout_segment *lseg = NULL;
476         struct page **pages = NULL;
477         int i;
478         u32 max_resp_sz, max_pages;
479
480         dprintk("--> %s\n", __func__);
481
482         BUG_ON(ctx == NULL);
483         lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
484         if (lgp == NULL)
485                 return NULL;
486
487         /* allocate pages for xdr post processing */
488         max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
489         max_pages = max_resp_sz >> PAGE_SHIFT;
490
491         pages = kzalloc(max_pages * sizeof(struct page *), GFP_KERNEL);
492         if (!pages)
493                 goto out_err_free;
494
495         for (i = 0; i < max_pages; i++) {
496                 pages[i] = alloc_page(GFP_KERNEL);
497                 if (!pages[i])
498                         goto out_err_free;
499         }
500
501         lgp->args.minlength = NFS4_MAX_UINT64;
502         lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
503         lgp->args.range.iomode = iomode;
504         lgp->args.range.offset = 0;
505         lgp->args.range.length = NFS4_MAX_UINT64;
506         lgp->args.type = server->pnfs_curr_ld->id;
507         lgp->args.inode = ino;
508         lgp->args.ctx = get_nfs_open_context(ctx);
509         lgp->args.layout.pages = pages;
510         lgp->args.layout.pglen = max_pages * PAGE_SIZE;
511         lgp->lsegpp = &lseg;
512
513         /* Synchronously retrieve layout information from server and
514          * store in lseg.
515          */
516         nfs4_proc_layoutget(lgp);
517         if (!lseg) {
518                 /* remember that LAYOUTGET failed and suspend trying */
519                 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
520         }
521
522         /* free xdr pages */
523         for (i = 0; i < max_pages; i++)
524                 __free_page(pages[i]);
525         kfree(pages);
526
527         return lseg;
528
529 out_err_free:
530         /* free any allocated xdr pages, lgp as it's not used */
531         if (pages) {
532                 for (i = 0; i < max_pages; i++) {
533                         if (!pages[i])
534                                 break;
535                         __free_page(pages[i]);
536                 }
537                 kfree(pages);
538         }
539         kfree(lgp);
540         return NULL;
541 }
542
543 bool pnfs_roc(struct inode *ino)
544 {
545         struct pnfs_layout_hdr *lo;
546         struct pnfs_layout_segment *lseg, *tmp;
547         LIST_HEAD(tmp_list);
548         bool found = false;
549
550         spin_lock(&ino->i_lock);
551         lo = NFS_I(ino)->layout;
552         if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
553             test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
554                 goto out_nolayout;
555         list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
556                 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
557                         mark_lseg_invalid(lseg, &tmp_list);
558                         found = true;
559                 }
560         if (!found)
561                 goto out_nolayout;
562         lo->plh_block_lgets++;
563         get_layout_hdr(lo); /* matched in pnfs_roc_release */
564         spin_unlock(&ino->i_lock);
565         pnfs_free_lseg_list(&tmp_list);
566         return true;
567
568 out_nolayout:
569         spin_unlock(&ino->i_lock);
570         return false;
571 }
572
573 void pnfs_roc_release(struct inode *ino)
574 {
575         struct pnfs_layout_hdr *lo;
576
577         spin_lock(&ino->i_lock);
578         lo = NFS_I(ino)->layout;
579         lo->plh_block_lgets--;
580         put_layout_hdr_locked(lo);
581         spin_unlock(&ino->i_lock);
582 }
583
584 void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
585 {
586         struct pnfs_layout_hdr *lo;
587
588         spin_lock(&ino->i_lock);
589         lo = NFS_I(ino)->layout;
590         if ((int)(barrier - lo->plh_barrier) > 0)
591                 lo->plh_barrier = barrier;
592         spin_unlock(&ino->i_lock);
593 }
594
595 bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
596 {
597         struct nfs_inode *nfsi = NFS_I(ino);
598         struct pnfs_layout_segment *lseg;
599         bool found = false;
600
601         spin_lock(&ino->i_lock);
602         list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
603                 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
604                         found = true;
605                         break;
606                 }
607         if (!found) {
608                 struct pnfs_layout_hdr *lo = nfsi->layout;
609                 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
610
611                 /* Since close does not return a layout stateid for use as
612                  * a barrier, we choose the worst-case barrier.
613                  */
614                 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
615         }
616         spin_unlock(&ino->i_lock);
617         return found;
618 }
619
620 /*
621  * Compare two layout segments for sorting into layout cache.
622  * We want to preferentially return RW over RO layouts, so ensure those
623  * are seen first.
624  */
625 static s64
626 cmp_layout(u32 iomode1, u32 iomode2)
627 {
628         /* read > read/write */
629         return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
630 }
631
632 static void
633 pnfs_insert_layout(struct pnfs_layout_hdr *lo,
634                    struct pnfs_layout_segment *lseg)
635 {
636         struct pnfs_layout_segment *lp;
637         int found = 0;
638
639         dprintk("%s:Begin\n", __func__);
640
641         assert_spin_locked(&lo->plh_inode->i_lock);
642         list_for_each_entry(lp, &lo->plh_segs, pls_list) {
643                 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
644                         continue;
645                 list_add_tail(&lseg->pls_list, &lp->pls_list);
646                 dprintk("%s: inserted lseg %p "
647                         "iomode %d offset %llu length %llu before "
648                         "lp %p iomode %d offset %llu length %llu\n",
649                         __func__, lseg, lseg->pls_range.iomode,
650                         lseg->pls_range.offset, lseg->pls_range.length,
651                         lp, lp->pls_range.iomode, lp->pls_range.offset,
652                         lp->pls_range.length);
653                 found = 1;
654                 break;
655         }
656         if (!found) {
657                 list_add_tail(&lseg->pls_list, &lo->plh_segs);
658                 dprintk("%s: inserted lseg %p "
659                         "iomode %d offset %llu length %llu at tail\n",
660                         __func__, lseg, lseg->pls_range.iomode,
661                         lseg->pls_range.offset, lseg->pls_range.length);
662         }
663         get_layout_hdr(lo);
664
665         dprintk("%s:Return\n", __func__);
666 }
667
668 static struct pnfs_layout_hdr *
669 alloc_init_layout_hdr(struct inode *ino)
670 {
671         struct pnfs_layout_hdr *lo;
672
673         lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
674         if (!lo)
675                 return NULL;
676         atomic_set(&lo->plh_refcount, 1);
677         INIT_LIST_HEAD(&lo->plh_layouts);
678         INIT_LIST_HEAD(&lo->plh_segs);
679         INIT_LIST_HEAD(&lo->plh_bulk_recall);
680         lo->plh_inode = ino;
681         return lo;
682 }
683
684 static struct pnfs_layout_hdr *
685 pnfs_find_alloc_layout(struct inode *ino)
686 {
687         struct nfs_inode *nfsi = NFS_I(ino);
688         struct pnfs_layout_hdr *new = NULL;
689
690         dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
691
692         assert_spin_locked(&ino->i_lock);
693         if (nfsi->layout) {
694                 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
695                         return NULL;
696                 else
697                         return nfsi->layout;
698         }
699         spin_unlock(&ino->i_lock);
700         new = alloc_init_layout_hdr(ino);
701         spin_lock(&ino->i_lock);
702
703         if (likely(nfsi->layout == NULL))       /* Won the race? */
704                 nfsi->layout = new;
705         else
706                 kfree(new);
707         return nfsi->layout;
708 }
709
710 /*
711  * iomode matching rules:
712  * iomode       lseg    match
713  * -----        -----   -----
714  * ANY          READ    true
715  * ANY          RW      true
716  * RW           READ    false
717  * RW           RW      true
718  * READ         READ    true
719  * READ         RW      true
720  */
721 static int
722 is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
723 {
724         return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
725 }
726
727 /*
728  * lookup range in layout
729  */
730 static struct pnfs_layout_segment *
731 pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
732 {
733         struct pnfs_layout_segment *lseg, *ret = NULL;
734
735         dprintk("%s:Begin\n", __func__);
736
737         assert_spin_locked(&lo->plh_inode->i_lock);
738         list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
739                 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
740                     is_matching_lseg(lseg, iomode)) {
741                         ret = get_lseg(lseg);
742                         break;
743                 }
744                 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
745                         break;
746         }
747
748         dprintk("%s:Return lseg %p ref %d\n",
749                 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
750         return ret;
751 }
752
753 /*
754  * Layout segment is retreived from the server if not cached.
755  * The appropriate layout segment is referenced and returned to the caller.
756  */
757 struct pnfs_layout_segment *
758 pnfs_update_layout(struct inode *ino,
759                    struct nfs_open_context *ctx,
760                    enum pnfs_iomode iomode)
761 {
762         struct nfs_inode *nfsi = NFS_I(ino);
763         struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
764         struct pnfs_layout_hdr *lo;
765         struct pnfs_layout_segment *lseg = NULL;
766         bool first = false;
767
768         if (!pnfs_enabled_sb(NFS_SERVER(ino)))
769                 return NULL;
770         spin_lock(&ino->i_lock);
771         lo = pnfs_find_alloc_layout(ino);
772         if (lo == NULL) {
773                 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
774                 goto out_unlock;
775         }
776
777         /* Do we even need to bother with this? */
778         if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
779             test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
780                 dprintk("%s matches recall, use MDS\n", __func__);
781                 goto out_unlock;
782         }
783
784         /* if LAYOUTGET already failed once we don't try again */
785         if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
786                 goto out_unlock;
787
788         /* Check to see if the layout for the given range already exists */
789         lseg = pnfs_find_lseg(lo, iomode);
790         if (lseg)
791                 goto out_unlock;
792
793         if (pnfs_layoutgets_blocked(lo, NULL, 0))
794                 goto out_unlock;
795         atomic_inc(&lo->plh_outstanding);
796
797         get_layout_hdr(lo);
798         if (list_empty(&lo->plh_segs))
799                 first = true;
800         spin_unlock(&ino->i_lock);
801         if (first) {
802                 /* The lo must be on the clp list if there is any
803                  * chance of a CB_LAYOUTRECALL(FILE) coming in.
804                  */
805                 spin_lock(&clp->cl_lock);
806                 BUG_ON(!list_empty(&lo->plh_layouts));
807                 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
808                 spin_unlock(&clp->cl_lock);
809         }
810
811         lseg = send_layoutget(lo, ctx, iomode);
812         if (!lseg && first) {
813                 spin_lock(&clp->cl_lock);
814                 list_del_init(&lo->plh_layouts);
815                 spin_unlock(&clp->cl_lock);
816         }
817         atomic_dec(&lo->plh_outstanding);
818         put_layout_hdr(lo);
819 out:
820         dprintk("%s end, state 0x%lx lseg %p\n", __func__,
821                 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
822         return lseg;
823 out_unlock:
824         spin_unlock(&ino->i_lock);
825         goto out;
826 }
827
828 int
829 pnfs_layout_process(struct nfs4_layoutget *lgp)
830 {
831         struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
832         struct nfs4_layoutget_res *res = &lgp->res;
833         struct pnfs_layout_segment *lseg;
834         struct inode *ino = lo->plh_inode;
835         struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
836         int status = 0;
837
838         /* Verify we got what we asked for.
839          * Note that because the xdr parsing only accepts a single
840          * element array, this can fail even if the server is behaving
841          * correctly.
842          */
843         if (lgp->args.range.iomode > res->range.iomode ||
844             res->range.offset != 0 ||
845             res->range.length != NFS4_MAX_UINT64) {
846                 status = -EINVAL;
847                 goto out;
848         }
849         /* Inject layout blob into I/O device driver */
850         lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
851         if (!lseg || IS_ERR(lseg)) {
852                 if (!lseg)
853                         status = -ENOMEM;
854                 else
855                         status = PTR_ERR(lseg);
856                 dprintk("%s: Could not allocate layout: error %d\n",
857                        __func__, status);
858                 goto out;
859         }
860
861         spin_lock(&ino->i_lock);
862         if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
863             test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
864                 dprintk("%s forget reply due to recall\n", __func__);
865                 goto out_forget_reply;
866         }
867
868         if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
869                 dprintk("%s forget reply due to state\n", __func__);
870                 goto out_forget_reply;
871         }
872         init_lseg(lo, lseg);
873         lseg->pls_range = res->range;
874         *lgp->lsegpp = get_lseg(lseg);
875         pnfs_insert_layout(lo, lseg);
876
877         if (res->return_on_close) {
878                 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
879                 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
880         }
881
882         /* Done processing layoutget. Set the layout stateid */
883         pnfs_set_layout_stateid(lo, &res->stateid, false);
884         spin_unlock(&ino->i_lock);
885 out:
886         return status;
887
888 out_forget_reply:
889         spin_unlock(&ino->i_lock);
890         lseg->pls_layout = lo;
891         NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
892         goto out;
893 }
894
895 static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
896                              struct nfs_page *prev,
897                              struct nfs_page *req)
898 {
899         if (pgio->pg_count == prev->wb_bytes) {
900                 /* This is first coelesce call for a series of nfs_pages */
901                 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
902                                                    prev->wb_context,
903                                                    IOMODE_READ);
904         }
905         return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
906 }
907
908 void
909 pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
910 {
911         struct pnfs_layoutdriver_type *ld;
912
913         ld = NFS_SERVER(inode)->pnfs_curr_ld;
914         pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
915 }
916
917 static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio,
918                               struct nfs_page *prev,
919                               struct nfs_page *req)
920 {
921         if (pgio->pg_count == prev->wb_bytes) {
922                 /* This is first coelesce call for a series of nfs_pages */
923                 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
924                                                    prev->wb_context,
925                                                    IOMODE_RW);
926         }
927         return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
928 }
929
930 void
931 pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode)
932 {
933         struct pnfs_layoutdriver_type *ld;
934
935         ld = NFS_SERVER(inode)->pnfs_curr_ld;
936         pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL;
937 }
938
939 enum pnfs_try_status
940 pnfs_try_to_write_data(struct nfs_write_data *wdata,
941                         const struct rpc_call_ops *call_ops, int how)
942 {
943         struct inode *inode = wdata->inode;
944         enum pnfs_try_status trypnfs;
945         struct nfs_server *nfss = NFS_SERVER(inode);
946
947         wdata->mds_ops = call_ops;
948
949         dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
950                 inode->i_ino, wdata->args.count, wdata->args.offset, how);
951
952         trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
953         if (trypnfs == PNFS_NOT_ATTEMPTED) {
954                 put_lseg(wdata->lseg);
955                 wdata->lseg = NULL;
956         } else
957                 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
958
959         dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
960         return trypnfs;
961 }
962
963 /*
964  * Call the appropriate parallel I/O subsystem read function.
965  */
966 enum pnfs_try_status
967 pnfs_try_to_read_data(struct nfs_read_data *rdata,
968                        const struct rpc_call_ops *call_ops)
969 {
970         struct inode *inode = rdata->inode;
971         struct nfs_server *nfss = NFS_SERVER(inode);
972         enum pnfs_try_status trypnfs;
973
974         rdata->mds_ops = call_ops;
975
976         dprintk("%s: Reading ino:%lu %u@%llu\n",
977                 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
978
979         trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
980         if (trypnfs == PNFS_NOT_ATTEMPTED) {
981                 put_lseg(rdata->lseg);
982                 rdata->lseg = NULL;
983         } else {
984                 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
985         }
986         dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
987         return trypnfs;
988 }
989
990 /*
991  * Currently there is only one (whole file) write lseg.
992  */
993 static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
994 {
995         struct pnfs_layout_segment *lseg, *rv = NULL;
996
997         list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
998                 if (lseg->pls_range.iomode == IOMODE_RW)
999                         rv = lseg;
1000         return rv;
1001 }
1002
1003 void
1004 pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1005 {
1006         struct nfs_inode *nfsi = NFS_I(wdata->inode);
1007         loff_t end_pos = wdata->args.offset + wdata->res.count;
1008         bool mark_as_dirty = false;
1009
1010         spin_lock(&nfsi->vfs_inode.i_lock);
1011         if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1012                 /* references matched in nfs4_layoutcommit_release */
1013                 get_lseg(wdata->lseg);
1014                 wdata->lseg->pls_lc_cred =
1015                         get_rpccred(wdata->args.context->state->owner->so_cred);
1016                 mark_as_dirty = true;
1017                 dprintk("%s: Set layoutcommit for inode %lu ",
1018                         __func__, wdata->inode->i_ino);
1019         }
1020         if (end_pos > wdata->lseg->pls_end_pos)
1021                 wdata->lseg->pls_end_pos = end_pos;
1022         spin_unlock(&nfsi->vfs_inode.i_lock);
1023
1024         /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1025          * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1026         if (mark_as_dirty)
1027                 mark_inode_dirty_sync(wdata->inode);
1028 }
1029 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1030
1031 /*
1032  * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1033  * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1034  * data to disk to allow the server to recover the data if it crashes.
1035  * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1036  * is off, and a COMMIT is sent to a data server, or
1037  * if WRITEs to a data server return NFS_DATA_SYNC.
1038  */
1039 int
1040 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
1041 {
1042         struct nfs4_layoutcommit_data *data;
1043         struct nfs_inode *nfsi = NFS_I(inode);
1044         struct pnfs_layout_segment *lseg;
1045         struct rpc_cred *cred;
1046         loff_t end_pos;
1047         int status = 0;
1048
1049         dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1050
1051         if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1052                 return 0;
1053
1054         /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1055         data = kzalloc(sizeof(*data), GFP_NOFS);
1056         if (!data) {
1057                 mark_inode_dirty_sync(inode);
1058                 status = -ENOMEM;
1059                 goto out;
1060         }
1061
1062         spin_lock(&inode->i_lock);
1063         if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1064                 spin_unlock(&inode->i_lock);
1065                 kfree(data);
1066                 goto out;
1067         }
1068         /*
1069          * Currently only one (whole file) write lseg which is referenced
1070          * in pnfs_set_layoutcommit and will be found.
1071          */
1072         lseg = pnfs_list_write_lseg(inode);
1073
1074         end_pos = lseg->pls_end_pos;
1075         cred = lseg->pls_lc_cred;
1076         lseg->pls_end_pos = 0;
1077         lseg->pls_lc_cred = NULL;
1078
1079         memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1080                 sizeof(nfsi->layout->plh_stateid.data));
1081         spin_unlock(&inode->i_lock);
1082
1083         data->args.inode = inode;
1084         data->lseg = lseg;
1085         data->cred = cred;
1086         nfs_fattr_init(&data->fattr);
1087         data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1088         data->res.fattr = &data->fattr;
1089         data->args.lastbytewritten = end_pos - 1;
1090         data->res.server = NFS_SERVER(inode);
1091
1092         status = nfs4_proc_layoutcommit(data, sync);
1093 out:
1094         dprintk("<-- %s status %d\n", __func__, status);
1095         return status;
1096 }