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