]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/pnfs.c
NFSv4.1: filelayout async error handler
[mirror_ubuntu-artful-kernel.git] / fs / nfs / pnfs.c
CommitLineData
85e174ba
RL
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>
974cec8c 31#include "internal.h"
85e174ba 32#include "pnfs.h"
64419a9b 33#include "iostat.h"
85e174ba
RL
34
35#define NFSDBG_FACILITY NFSDBG_PNFS
36
02c35fca
FI
37/* Locking:
38 *
39 * pnfs_spinlock:
40 * protects pnfs_modules_tbl.
41 */
42static DEFINE_SPINLOCK(pnfs_spinlock);
43
44/*
45 * pnfs_modules_tbl holds all pnfs modules
46 */
47static LIST_HEAD(pnfs_modules_tbl);
48
49/* Return the registered pnfs layout driver module matching given id */
50static struct pnfs_layoutdriver_type *
51find_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;
59out:
60 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
61 return local;
62}
63
85e174ba
RL
64static struct pnfs_layoutdriver_type *
65find_pnfs_driver(u32 id)
66{
02c35fca
FI
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;
85e174ba
RL
73}
74
75void
76unset_pnfs_layoutdriver(struct nfs_server *nfss)
77{
02c35fca 78 if (nfss->pnfs_curr_ld) {
1c787096 79 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
02c35fca
FI
80 module_put(nfss->pnfs_curr_ld->owner);
81 }
85e174ba
RL
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 */
91void
92set_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 }
02c35fca
FI
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 }
85e174ba 118 server->pnfs_curr_ld = ld_type;
1c787096 119 if (ld_type->set_layoutdriver(server)) {
02c35fca
FI
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 }
85e174ba
RL
126 dprintk("%s: pNFS module for %u set\n", __func__, id);
127 return;
128
129out_no_driver:
130 dprintk("%s: Using NFSv4 I/O\n", __func__);
131 server->pnfs_curr_ld = NULL;
132}
02c35fca
FI
133
134int
135pnfs_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 }
b1f69b75
AA
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 }
02c35fca
FI
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}
165EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
166
167void
168pnfs_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}
175EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
e5e94017 176
b1f69b75
AA
177/*
178 * pNFS client layout cache
179 */
180
cc6e5340 181/* Need to hold i_lock if caller does not already hold reference */
43f1b3da 182void
cc6e5340 183get_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 184{
cc6e5340 185 atomic_inc(&lo->plh_refcount);
e5e94017
BH
186}
187
188static void
cc6e5340 189destroy_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 190{
cc6e5340
FI
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}
e5e94017 196
cc6e5340
FI
197static void
198put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
199{
200 if (atomic_dec_and_test(&lo->plh_refcount))
201 destroy_layout_hdr(lo);
e5e94017
BH
202}
203
b1f69b75 204void
cc6e5340 205put_layout_hdr(struct pnfs_layout_hdr *lo)
974cec8c 206{
cc6e5340
FI
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 }
974cec8c
AA
213}
214
215static void
216init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
217{
566052c5 218 INIT_LIST_HEAD(&lseg->pls_list);
4541d16c
FI
219 atomic_set(&lseg->pls_refcount, 1);
220 smp_mb();
221 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
566052c5 222 lseg->pls_layout = lo;
974cec8c
AA
223}
224
4541d16c 225static void free_lseg(struct pnfs_layout_segment *lseg)
974cec8c 226{
b7edfaa1 227 struct inode *ino = lseg->pls_layout->plh_inode;
974cec8c 228
b1f69b75 229 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
52fabd73 230 /* Matched by get_layout_hdr in pnfs_insert_layout */
cc6e5340 231 put_layout_hdr(NFS_I(ino)->layout);
974cec8c
AA
232}
233
d684d2ae
FI
234static void
235put_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
bae724ef 249void
d684d2ae 250put_lseg(struct pnfs_layout_segment *lseg)
974cec8c 251{
d684d2ae
FI
252 struct inode *inode;
253
254 if (!lseg)
255 return;
256
4541d16c
FI
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));
d684d2ae
FI
260 inode = lseg->pls_layout->plh_inode;
261 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
262 LIST_HEAD(free_me);
4541d16c 263
d684d2ae
FI
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);
4541d16c 268 }
4541d16c 269}
974cec8c 270
4541d16c
FI
271static bool
272should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
273{
274 return (recall_iomode == IOMODE_ANY ||
275 lseg_iomode == recall_iomode);
974cec8c
AA
276}
277
4541d16c
FI
278/* Returns 1 if lseg is removed from list, 0 otherwise */
279static 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 */
d684d2ae
FI
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 }
4541d16c
FI
296 }
297 return rv;
298}
299
300/* Returns count of number of matching invalid lsegs remaining in list
301 * after call.
302 */
43f1b3da 303int
4541d16c
FI
304mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
305 struct list_head *tmp_list,
306 u32 iomode)
974cec8c
AA
307{
308 struct pnfs_layout_segment *lseg, *next;
4541d16c 309 int invalid = 0, removed = 0;
974cec8c
AA
310
311 dprintk("%s:Begin lo %p\n", __func__, lo);
312
38511722
FI
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 }
4541d16c
FI
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;
974cec8c
AA
329}
330
f49f9baa 331/* note free_me must contain lsegs from a single layout_hdr */
43f1b3da 332void
4541d16c 333pnfs_free_lseg_list(struct list_head *free_me)
974cec8c 334{
4541d16c 335 struct pnfs_layout_segment *lseg, *tmp;
f49f9baa
FI
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;
974cec8c 343
f49f9baa
FI
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 }
4541d16c 352 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
566052c5 353 list_del(&lseg->pls_list);
4541d16c 354 free_lseg(lseg);
974cec8c
AA
355 }
356}
357
e5e94017
BH
358void
359pnfs_destroy_layout(struct nfs_inode *nfsi)
360{
361 struct pnfs_layout_hdr *lo;
974cec8c 362 LIST_HEAD(tmp_list);
e5e94017
BH
363
364 spin_lock(&nfsi->vfs_inode.i_lock);
365 lo = nfsi->layout;
366 if (lo) {
38511722 367 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
4541d16c 368 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
e5e94017
BH
369 }
370 spin_unlock(&nfsi->vfs_inode.i_lock);
974cec8c
AA
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 */
378void
379pnfs_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,
b7edfaa1 390 plh_layouts);
974cec8c 391 dprintk("%s freeing layout for inode %lu\n", __func__,
b7edfaa1
FI
392 lo->plh_inode->i_ino);
393 pnfs_destroy_layout(NFS_I(lo->plh_inode));
974cec8c 394 }
e5e94017
BH
395}
396
fd6002e9 397/* update lo->plh_stateid with new if is more recent */
43f1b3da
FI
398void
399pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
400 bool update_barrier)
b1f69b75 401{
fd6002e9 402 u32 oldseq, newseq;
b1f69b75 403
fd6002e9
FI
404 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
405 newseq = be32_to_cpu(new->stateid.seqid);
43f1b3da 406 if ((int)(newseq - oldseq) > 0) {
fd6002e9 407 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
43f1b3da
FI
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 }
b1f69b75
AA
425}
426
cf7d63f1
FI
427/* lget is set to 1 if called from inside send_layoutget call chain */
428static bool
43f1b3da
FI
429pnfs_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;
f7e8917a 435 return lo->plh_block_lgets ||
38511722 436 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
f7e8917a 437 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
43f1b3da 438 (list_empty(&lo->plh_segs) &&
cf7d63f1
FI
439 (atomic_read(&lo->plh_outstanding) > lget));
440}
441
fd6002e9
FI
442int
443pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
444 struct nfs4_state *open_state)
b1f69b75 445{
fd6002e9 446 int status = 0;
974cec8c 447
b1f69b75 448 dprintk("--> %s\n", __func__);
fd6002e9 449 spin_lock(&lo->plh_inode->i_lock);
43f1b3da 450 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
cf7d63f1
FI
451 status = -EAGAIN;
452 } else if (list_empty(&lo->plh_segs)) {
fd6002e9
FI
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);
b1f69b75 463 dprintk("<-- %s\n", __func__);
fd6002e9 464 return status;
b1f69b75
AA
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*/
e5e94017
BH
473static struct pnfs_layout_segment *
474send_layoutget(struct pnfs_layout_hdr *lo,
475 struct nfs_open_context *ctx,
476 u32 iomode)
477{
b7edfaa1 478 struct inode *ino = lo->plh_inode;
b1f69b75
AA
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__);
e5e94017 484
b1f69b75
AA
485 BUG_ON(ctx == NULL);
486 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
cf7d63f1 487 if (lgp == NULL)
b1f69b75 488 return NULL;
b1f69b75
AA
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);
974cec8c 503 if (!lseg) {
b1f69b75 504 /* remember that LAYOUTGET failed and suspend trying */
566052c5 505 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
974cec8c 506 }
974cec8c
AA
507 return lseg;
508}
509
f7e8917a
FI
510bool 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
535out_nolayout:
536 spin_unlock(&ino->i_lock);
537 return false;
538}
539
540void 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
551void 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
562bool 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
b1f69b75
AA
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 */
592static s64
593cmp_layout(u32 iomode1, u32 iomode2)
594{
595 /* read > read/write */
596 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
597}
598
974cec8c
AA
599static void
600pnfs_insert_layout(struct pnfs_layout_hdr *lo,
601 struct pnfs_layout_segment *lseg)
602{
b1f69b75
AA
603 struct pnfs_layout_segment *lp;
604 int found = 0;
605
974cec8c
AA
606 dprintk("%s:Begin\n", __func__);
607
b7edfaa1 608 assert_spin_locked(&lo->plh_inode->i_lock);
b7edfaa1 609 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
566052c5 610 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
b1f69b75 611 continue;
566052c5 612 list_add_tail(&lseg->pls_list, &lp->pls_list);
b1f69b75
AA
613 dprintk("%s: inserted lseg %p "
614 "iomode %d offset %llu length %llu before "
615 "lp %p iomode %d offset %llu length %llu\n",
566052c5
FI
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);
b1f69b75
AA
620 found = 1;
621 break;
622 }
623 if (!found) {
b7edfaa1 624 list_add_tail(&lseg->pls_list, &lo->plh_segs);
b1f69b75
AA
625 dprintk("%s: inserted lseg %p "
626 "iomode %d offset %llu length %llu at tail\n",
566052c5
FI
627 __func__, lseg, lseg->pls_range.iomode,
628 lseg->pls_range.offset, lseg->pls_range.length);
974cec8c 629 }
cc6e5340 630 get_layout_hdr(lo);
974cec8c
AA
631
632 dprintk("%s:Return\n", __func__);
e5e94017
BH
633}
634
635static struct pnfs_layout_hdr *
636alloc_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;
cc6e5340 643 atomic_set(&lo->plh_refcount, 1);
b7edfaa1
FI
644 INIT_LIST_HEAD(&lo->plh_layouts);
645 INIT_LIST_HEAD(&lo->plh_segs);
43f1b3da 646 INIT_LIST_HEAD(&lo->plh_bulk_recall);
b7edfaa1 647 lo->plh_inode = ino;
e5e94017
BH
648 return lo;
649}
650
651static struct pnfs_layout_hdr *
652pnfs_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);
4541d16c
FI
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 }
e5e94017
BH
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
b1f69b75
AA
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 */
688static int
689is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
690{
566052c5 691 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
b1f69b75
AA
692}
693
694/*
695 * lookup range in layout
696 */
e5e94017 697static struct pnfs_layout_segment *
43f1b3da 698pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
e5e94017 699{
b1f69b75
AA
700 struct pnfs_layout_segment *lseg, *ret = NULL;
701
702 dprintk("%s:Begin\n", __func__);
703
b7edfaa1
FI
704 assert_spin_locked(&lo->plh_inode->i_lock);
705 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
4541d16c
FI
706 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
707 is_matching_lseg(lseg, iomode)) {
d684d2ae 708 ret = get_lseg(lseg);
b1f69b75
AA
709 break;
710 }
566052c5 711 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
b1f69b75
AA
712 break;
713 }
714
715 dprintk("%s:Return lseg %p ref %d\n",
4541d16c 716 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
b1f69b75 717 return ret;
e5e94017
BH
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 */
724struct pnfs_layout_segment *
725pnfs_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);
2130ff66 730 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
e5e94017
BH
731 struct pnfs_layout_hdr *lo;
732 struct pnfs_layout_segment *lseg = NULL;
f49f9baa 733 bool first = false;
e5e94017
BH
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
43f1b3da
FI
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__);
e5e94017
BH
748 goto out_unlock;
749 }
43f1b3da
FI
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;
e5e94017
BH
754
755 /* if LAYOUTGET already failed once we don't try again */
566052c5 756 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
e5e94017
BH
757 goto out_unlock;
758
43f1b3da 759 if (pnfs_layoutgets_blocked(lo, NULL, 0))
cf7d63f1
FI
760 goto out_unlock;
761 atomic_inc(&lo->plh_outstanding);
762
cc6e5340 763 get_layout_hdr(lo);
f49f9baa
FI
764 if (list_empty(&lo->plh_segs))
765 first = true;
766 spin_unlock(&ino->i_lock);
767 if (first) {
2130ff66
FI
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 }
e5e94017
BH
776
777 lseg = send_layoutget(lo, ctx, iomode);
f49f9baa
FI
778 if (!lseg && first) {
779 spin_lock(&clp->cl_lock);
780 list_del_init(&lo->plh_layouts);
781 spin_unlock(&clp->cl_lock);
2130ff66 782 }
cf7d63f1 783 atomic_dec(&lo->plh_outstanding);
cc6e5340 784 put_layout_hdr(lo);
e5e94017
BH
785out:
786 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
bf9c1387 787 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
e5e94017
BH
788 return lseg;
789out_unlock:
790 spin_unlock(&ino->i_lock);
791 goto out;
792}
b1f69b75
AA
793
794int
795pnfs_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;
b7edfaa1 800 struct inode *ino = lo->plh_inode;
43f1b3da 801 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
b1f69b75
AA
802 int status = 0;
803
fc1794c5
FI
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 }
b1f69b75
AA
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);
43f1b3da
FI
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 }
b1f69b75 838 init_lseg(lo, lseg);
566052c5 839 lseg->pls_range = res->range;
d684d2ae 840 *lgp->lsegpp = get_lseg(lseg);
b1f69b75
AA
841 pnfs_insert_layout(lo, lseg);
842
f7e8917a
FI
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
b1f69b75 848 /* Done processing layoutget. Set the layout stateid */
43f1b3da 849 pnfs_set_layout_stateid(lo, &res->stateid, false);
b1f69b75
AA
850 spin_unlock(&ino->i_lock);
851out:
852 return status;
43f1b3da
FI
853
854out_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;
b1f69b75
AA
859}
860
bae724ef
FI
861static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
862 struct nfs_page *prev,
863 struct nfs_page *req)
94ad1c80 864{
bae724ef
FI
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);
94ad1c80
FI
872}
873
874void
bae724ef 875pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
94ad1c80 876{
bae724ef
FI
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;
94ad1c80
FI
881}
882
64419a9b
AA
883/*
884 * Call the appropriate parallel I/O subsystem read function.
885 */
886enum pnfs_try_status
887pnfs_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
b1f69b75
AA
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 */
914int
915pnfs_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}
940EXPORT_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 */
946void
947pnfs_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}
971EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
972
973/* Find and reference a deviceid */
974struct pnfs_deviceid_node *
975pnfs_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 }
993fail:
994 rcu_read_unlock();
995 return NULL;
996}
997EXPORT_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 */
1003struct pnfs_deviceid_node *
1004pnfs_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}
1025EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
1026
1027void
1028pnfs_put_deviceid_cache(struct nfs_client *clp)
1029{
1030 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
1031
b2a2897d 1032 dprintk("--> %s ({%d})\n", __func__, atomic_read(&local->dc_ref));
b1f69b75
AA
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}
1043EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);