]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/pnfs.c
pnfs: serialize LAYOUTGET(openstateid)
[mirror_ubuntu-bionic-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
RL
32#include "pnfs.h"
33
34#define NFSDBG_FACILITY NFSDBG_PNFS
35
02c35fca
FI
36/* Locking:
37 *
38 * pnfs_spinlock:
39 * protects pnfs_modules_tbl.
40 */
41static DEFINE_SPINLOCK(pnfs_spinlock);
42
43/*
44 * pnfs_modules_tbl holds all pnfs modules
45 */
46static LIST_HEAD(pnfs_modules_tbl);
47
48/* Return the registered pnfs layout driver module matching given id */
49static struct pnfs_layoutdriver_type *
50find_pnfs_driver_locked(u32 id)
51{
52 struct pnfs_layoutdriver_type *local;
53
54 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
55 if (local->id == id)
56 goto out;
57 local = NULL;
58out:
59 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
60 return local;
61}
62
85e174ba
RL
63static struct pnfs_layoutdriver_type *
64find_pnfs_driver(u32 id)
65{
02c35fca
FI
66 struct pnfs_layoutdriver_type *local;
67
68 spin_lock(&pnfs_spinlock);
69 local = find_pnfs_driver_locked(id);
70 spin_unlock(&pnfs_spinlock);
71 return local;
85e174ba
RL
72}
73
74void
75unset_pnfs_layoutdriver(struct nfs_server *nfss)
76{
02c35fca 77 if (nfss->pnfs_curr_ld) {
1c787096 78 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
02c35fca
FI
79 module_put(nfss->pnfs_curr_ld->owner);
80 }
85e174ba
RL
81 nfss->pnfs_curr_ld = NULL;
82}
83
84/*
85 * Try to set the server's pnfs module to the pnfs layout type specified by id.
86 * Currently only one pNFS layout driver per filesystem is supported.
87 *
88 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
89 */
90void
91set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
92{
93 struct pnfs_layoutdriver_type *ld_type = NULL;
94
95 if (id == 0)
96 goto out_no_driver;
97 if (!(server->nfs_client->cl_exchange_flags &
98 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
99 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
100 id, server->nfs_client->cl_exchange_flags);
101 goto out_no_driver;
102 }
103 ld_type = find_pnfs_driver(id);
104 if (!ld_type) {
105 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
106 ld_type = find_pnfs_driver(id);
107 if (!ld_type) {
108 dprintk("%s: No pNFS module found for %u.\n",
109 __func__, id);
110 goto out_no_driver;
111 }
112 }
02c35fca
FI
113 if (!try_module_get(ld_type->owner)) {
114 dprintk("%s: Could not grab reference on module\n", __func__);
115 goto out_no_driver;
116 }
85e174ba 117 server->pnfs_curr_ld = ld_type;
1c787096 118 if (ld_type->set_layoutdriver(server)) {
02c35fca
FI
119 printk(KERN_ERR
120 "%s: Error initializing mount point for layout driver %u.\n",
121 __func__, id);
122 module_put(ld_type->owner);
123 goto out_no_driver;
124 }
85e174ba
RL
125 dprintk("%s: pNFS module for %u set\n", __func__, id);
126 return;
127
128out_no_driver:
129 dprintk("%s: Using NFSv4 I/O\n", __func__);
130 server->pnfs_curr_ld = NULL;
131}
02c35fca
FI
132
133int
134pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
135{
136 int status = -EINVAL;
137 struct pnfs_layoutdriver_type *tmp;
138
139 if (ld_type->id == 0) {
140 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
141 return status;
142 }
b1f69b75
AA
143 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
144 printk(KERN_ERR "%s Layout driver must provide "
145 "alloc_lseg and free_lseg.\n", __func__);
146 return status;
147 }
02c35fca
FI
148
149 spin_lock(&pnfs_spinlock);
150 tmp = find_pnfs_driver_locked(ld_type->id);
151 if (!tmp) {
152 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
153 status = 0;
154 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
155 ld_type->name);
156 } else {
157 printk(KERN_ERR "%s Module with id %d already loaded!\n",
158 __func__, ld_type->id);
159 }
160 spin_unlock(&pnfs_spinlock);
161
162 return status;
163}
164EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
165
166void
167pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
168{
169 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
170 spin_lock(&pnfs_spinlock);
171 list_del(&ld_type->pnfs_tblid);
172 spin_unlock(&pnfs_spinlock);
173}
174EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
e5e94017 175
b1f69b75
AA
176/*
177 * pNFS client layout cache
178 */
179
e5e94017
BH
180static void
181get_layout_hdr_locked(struct pnfs_layout_hdr *lo)
182{
b7edfaa1
FI
183 assert_spin_locked(&lo->plh_inode->i_lock);
184 lo->plh_refcount++;
e5e94017
BH
185}
186
187static void
188put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
189{
b7edfaa1
FI
190 assert_spin_locked(&lo->plh_inode->i_lock);
191 BUG_ON(lo->plh_refcount == 0);
e5e94017 192
b7edfaa1
FI
193 lo->plh_refcount--;
194 if (!lo->plh_refcount) {
e5e94017 195 dprintk("%s: freeing layout cache %p\n", __func__, lo);
b7edfaa1
FI
196 BUG_ON(!list_empty(&lo->plh_layouts));
197 NFS_I(lo->plh_inode)->layout = NULL;
e5e94017
BH
198 kfree(lo);
199 }
200}
201
b1f69b75 202void
974cec8c
AA
203put_layout_hdr(struct inode *inode)
204{
205 spin_lock(&inode->i_lock);
206 put_layout_hdr_locked(NFS_I(inode)->layout);
207 spin_unlock(&inode->i_lock);
208}
209
210static void
211init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
212{
566052c5 213 INIT_LIST_HEAD(&lseg->pls_list);
4541d16c
FI
214 atomic_set(&lseg->pls_refcount, 1);
215 smp_mb();
216 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
566052c5 217 lseg->pls_layout = lo;
974cec8c
AA
218}
219
4541d16c 220static void free_lseg(struct pnfs_layout_segment *lseg)
974cec8c 221{
b7edfaa1 222 struct inode *ino = lseg->pls_layout->plh_inode;
974cec8c 223
b1f69b75 224 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
52fabd73 225 /* Matched by get_layout_hdr in pnfs_insert_layout */
974cec8c
AA
226 put_layout_hdr(ino);
227}
228
4541d16c
FI
229/* The use of tmp_list is necessary because pnfs_curr_ld->free_lseg
230 * could sleep, so must be called outside of the lock.
231 * Returns 1 if object was removed, otherwise return 0.
232 */
233static int
234put_lseg_locked(struct pnfs_layout_segment *lseg,
235 struct list_head *tmp_list)
974cec8c 236{
4541d16c
FI
237 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
238 atomic_read(&lseg->pls_refcount),
239 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
240 if (atomic_dec_and_test(&lseg->pls_refcount)) {
241 struct inode *ino = lseg->pls_layout->plh_inode;
242
243 BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
244 list_del(&lseg->pls_list);
245 if (list_empty(&lseg->pls_layout->plh_segs)) {
246 struct nfs_client *clp;
247
248 clp = NFS_SERVER(ino)->nfs_client;
249 spin_lock(&clp->cl_lock);
250 /* List does not take a reference, so no need for put here */
251 list_del_init(&lseg->pls_layout->plh_layouts);
252 spin_unlock(&clp->cl_lock);
253 }
254 list_add(&lseg->pls_list, tmp_list);
255 return 1;
256 }
257 return 0;
258}
974cec8c 259
4541d16c
FI
260static bool
261should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
262{
263 return (recall_iomode == IOMODE_ANY ||
264 lseg_iomode == recall_iomode);
974cec8c
AA
265}
266
4541d16c
FI
267/* Returns 1 if lseg is removed from list, 0 otherwise */
268static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
269 struct list_head *tmp_list)
270{
271 int rv = 0;
272
273 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
274 /* Remove the reference keeping the lseg in the
275 * list. It will now be removed when all
276 * outstanding io is finished.
277 */
278 rv = put_lseg_locked(lseg, tmp_list);
279 }
280 return rv;
281}
282
283/* Returns count of number of matching invalid lsegs remaining in list
284 * after call.
285 */
286static int
287mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
288 struct list_head *tmp_list,
289 u32 iomode)
974cec8c
AA
290{
291 struct pnfs_layout_segment *lseg, *next;
4541d16c 292 int invalid = 0, removed = 0;
974cec8c
AA
293
294 dprintk("%s:Begin lo %p\n", __func__, lo);
295
4541d16c
FI
296 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
297 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
298 dprintk("%s: freeing lseg %p iomode %d "
299 "offset %llu length %llu\n", __func__,
300 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
301 lseg->pls_range.length);
302 invalid++;
303 removed += mark_lseg_invalid(lseg, tmp_list);
304 }
305 dprintk("%s:Return %i\n", __func__, invalid - removed);
306 return invalid - removed;
974cec8c
AA
307}
308
309static void
4541d16c 310pnfs_free_lseg_list(struct list_head *free_me)
974cec8c 311{
4541d16c 312 struct pnfs_layout_segment *lseg, *tmp;
974cec8c 313
4541d16c 314 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
566052c5 315 list_del(&lseg->pls_list);
4541d16c 316 free_lseg(lseg);
974cec8c
AA
317 }
318}
319
e5e94017
BH
320void
321pnfs_destroy_layout(struct nfs_inode *nfsi)
322{
323 struct pnfs_layout_hdr *lo;
974cec8c 324 LIST_HEAD(tmp_list);
e5e94017
BH
325
326 spin_lock(&nfsi->vfs_inode.i_lock);
327 lo = nfsi->layout;
328 if (lo) {
4541d16c
FI
329 set_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags);
330 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
e5e94017
BH
331 /* Matched by refcount set to 1 in alloc_init_layout_hdr */
332 put_layout_hdr_locked(lo);
333 }
334 spin_unlock(&nfsi->vfs_inode.i_lock);
974cec8c
AA
335 pnfs_free_lseg_list(&tmp_list);
336}
337
338/*
339 * Called by the state manger to remove all layouts established under an
340 * expired lease.
341 */
342void
343pnfs_destroy_all_layouts(struct nfs_client *clp)
344{
345 struct pnfs_layout_hdr *lo;
346 LIST_HEAD(tmp_list);
347
348 spin_lock(&clp->cl_lock);
349 list_splice_init(&clp->cl_layouts, &tmp_list);
350 spin_unlock(&clp->cl_lock);
351
352 while (!list_empty(&tmp_list)) {
353 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
b7edfaa1 354 plh_layouts);
974cec8c 355 dprintk("%s freeing layout for inode %lu\n", __func__,
b7edfaa1
FI
356 lo->plh_inode->i_ino);
357 pnfs_destroy_layout(NFS_I(lo->plh_inode));
974cec8c 358 }
e5e94017
BH
359}
360
fd6002e9 361/* update lo->plh_stateid with new if is more recent */
b1f69b75
AA
362static void
363pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo,
364 const nfs4_stateid *new)
365{
fd6002e9 366 u32 oldseq, newseq;
b1f69b75 367
fd6002e9
FI
368 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
369 newseq = be32_to_cpu(new->stateid.seqid);
370 if ((int)(newseq - oldseq) > 0)
371 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
b1f69b75
AA
372}
373
cf7d63f1
FI
374/* lget is set to 1 if called from inside send_layoutget call chain */
375static bool
376pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, int lget)
377{
378 return (list_empty(&lo->plh_segs) &&
379 (atomic_read(&lo->plh_outstanding) > lget));
380}
381
fd6002e9
FI
382int
383pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
384 struct nfs4_state *open_state)
b1f69b75 385{
fd6002e9 386 int status = 0;
974cec8c 387
b1f69b75 388 dprintk("--> %s\n", __func__);
fd6002e9 389 spin_lock(&lo->plh_inode->i_lock);
cf7d63f1
FI
390 if (pnfs_layoutgets_blocked(lo, 1)) {
391 status = -EAGAIN;
392 } else if (list_empty(&lo->plh_segs)) {
fd6002e9
FI
393 int seq;
394
395 do {
396 seq = read_seqbegin(&open_state->seqlock);
397 memcpy(dst->data, open_state->stateid.data,
398 sizeof(open_state->stateid.data));
399 } while (read_seqretry(&open_state->seqlock, seq));
400 } else
401 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
402 spin_unlock(&lo->plh_inode->i_lock);
b1f69b75 403 dprintk("<-- %s\n", __func__);
fd6002e9 404 return status;
b1f69b75
AA
405}
406
407/*
408* Get layout from server.
409* for now, assume that whole file layouts are requested.
410* arg->offset: 0
411* arg->length: all ones
412*/
e5e94017
BH
413static struct pnfs_layout_segment *
414send_layoutget(struct pnfs_layout_hdr *lo,
415 struct nfs_open_context *ctx,
416 u32 iomode)
417{
b7edfaa1 418 struct inode *ino = lo->plh_inode;
b1f69b75
AA
419 struct nfs_server *server = NFS_SERVER(ino);
420 struct nfs4_layoutget *lgp;
421 struct pnfs_layout_segment *lseg = NULL;
422
423 dprintk("--> %s\n", __func__);
e5e94017 424
b1f69b75
AA
425 BUG_ON(ctx == NULL);
426 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
cf7d63f1 427 if (lgp == NULL)
b1f69b75 428 return NULL;
b1f69b75
AA
429 lgp->args.minlength = NFS4_MAX_UINT64;
430 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
431 lgp->args.range.iomode = iomode;
432 lgp->args.range.offset = 0;
433 lgp->args.range.length = NFS4_MAX_UINT64;
434 lgp->args.type = server->pnfs_curr_ld->id;
435 lgp->args.inode = ino;
436 lgp->args.ctx = get_nfs_open_context(ctx);
437 lgp->lsegpp = &lseg;
438
439 /* Synchronously retrieve layout information from server and
440 * store in lseg.
441 */
442 nfs4_proc_layoutget(lgp);
974cec8c 443 if (!lseg) {
b1f69b75 444 /* remember that LAYOUTGET failed and suspend trying */
566052c5 445 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
974cec8c 446 }
974cec8c
AA
447 return lseg;
448}
449
b1f69b75
AA
450/*
451 * Compare two layout segments for sorting into layout cache.
452 * We want to preferentially return RW over RO layouts, so ensure those
453 * are seen first.
454 */
455static s64
456cmp_layout(u32 iomode1, u32 iomode2)
457{
458 /* read > read/write */
459 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
460}
461
974cec8c
AA
462static void
463pnfs_insert_layout(struct pnfs_layout_hdr *lo,
464 struct pnfs_layout_segment *lseg)
465{
b1f69b75
AA
466 struct pnfs_layout_segment *lp;
467 int found = 0;
468
974cec8c
AA
469 dprintk("%s:Begin\n", __func__);
470
b7edfaa1
FI
471 assert_spin_locked(&lo->plh_inode->i_lock);
472 if (list_empty(&lo->plh_segs)) {
473 struct nfs_client *clp = NFS_SERVER(lo->plh_inode)->nfs_client;
974cec8c
AA
474
475 spin_lock(&clp->cl_lock);
b7edfaa1
FI
476 BUG_ON(!list_empty(&lo->plh_layouts));
477 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
974cec8c
AA
478 spin_unlock(&clp->cl_lock);
479 }
b7edfaa1 480 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
566052c5 481 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
b1f69b75 482 continue;
566052c5 483 list_add_tail(&lseg->pls_list, &lp->pls_list);
b1f69b75
AA
484 dprintk("%s: inserted lseg %p "
485 "iomode %d offset %llu length %llu before "
486 "lp %p iomode %d offset %llu length %llu\n",
566052c5
FI
487 __func__, lseg, lseg->pls_range.iomode,
488 lseg->pls_range.offset, lseg->pls_range.length,
489 lp, lp->pls_range.iomode, lp->pls_range.offset,
490 lp->pls_range.length);
b1f69b75
AA
491 found = 1;
492 break;
493 }
494 if (!found) {
b7edfaa1 495 list_add_tail(&lseg->pls_list, &lo->plh_segs);
b1f69b75
AA
496 dprintk("%s: inserted lseg %p "
497 "iomode %d offset %llu length %llu at tail\n",
566052c5
FI
498 __func__, lseg, lseg->pls_range.iomode,
499 lseg->pls_range.offset, lseg->pls_range.length);
974cec8c 500 }
b1f69b75 501 get_layout_hdr_locked(lo);
974cec8c
AA
502
503 dprintk("%s:Return\n", __func__);
e5e94017
BH
504}
505
506static struct pnfs_layout_hdr *
507alloc_init_layout_hdr(struct inode *ino)
508{
509 struct pnfs_layout_hdr *lo;
510
511 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
512 if (!lo)
513 return NULL;
b7edfaa1
FI
514 lo->plh_refcount = 1;
515 INIT_LIST_HEAD(&lo->plh_layouts);
516 INIT_LIST_HEAD(&lo->plh_segs);
b7edfaa1 517 lo->plh_inode = ino;
e5e94017
BH
518 return lo;
519}
520
521static struct pnfs_layout_hdr *
522pnfs_find_alloc_layout(struct inode *ino)
523{
524 struct nfs_inode *nfsi = NFS_I(ino);
525 struct pnfs_layout_hdr *new = NULL;
526
527 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
528
529 assert_spin_locked(&ino->i_lock);
4541d16c
FI
530 if (nfsi->layout) {
531 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
532 return NULL;
533 else
534 return nfsi->layout;
535 }
e5e94017
BH
536 spin_unlock(&ino->i_lock);
537 new = alloc_init_layout_hdr(ino);
538 spin_lock(&ino->i_lock);
539
540 if (likely(nfsi->layout == NULL)) /* Won the race? */
541 nfsi->layout = new;
542 else
543 kfree(new);
544 return nfsi->layout;
545}
546
b1f69b75
AA
547/*
548 * iomode matching rules:
549 * iomode lseg match
550 * ----- ----- -----
551 * ANY READ true
552 * ANY RW true
553 * RW READ false
554 * RW RW true
555 * READ READ true
556 * READ RW true
557 */
558static int
559is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
560{
566052c5 561 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
b1f69b75
AA
562}
563
564/*
565 * lookup range in layout
566 */
e5e94017
BH
567static struct pnfs_layout_segment *
568pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
569{
b1f69b75
AA
570 struct pnfs_layout_segment *lseg, *ret = NULL;
571
572 dprintk("%s:Begin\n", __func__);
573
b7edfaa1
FI
574 assert_spin_locked(&lo->plh_inode->i_lock);
575 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
4541d16c
FI
576 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
577 is_matching_lseg(lseg, iomode)) {
b1f69b75
AA
578 ret = lseg;
579 break;
580 }
566052c5 581 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
b1f69b75
AA
582 break;
583 }
584
585 dprintk("%s:Return lseg %p ref %d\n",
4541d16c 586 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
b1f69b75 587 return ret;
e5e94017
BH
588}
589
590/*
591 * Layout segment is retreived from the server if not cached.
592 * The appropriate layout segment is referenced and returned to the caller.
593 */
594struct pnfs_layout_segment *
595pnfs_update_layout(struct inode *ino,
596 struct nfs_open_context *ctx,
597 enum pnfs_iomode iomode)
598{
599 struct nfs_inode *nfsi = NFS_I(ino);
600 struct pnfs_layout_hdr *lo;
601 struct pnfs_layout_segment *lseg = NULL;
602
603 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
604 return NULL;
605 spin_lock(&ino->i_lock);
606 lo = pnfs_find_alloc_layout(ino);
607 if (lo == NULL) {
608 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
609 goto out_unlock;
610 }
611
612 /* Check to see if the layout for the given range already exists */
613 lseg = pnfs_has_layout(lo, iomode);
614 if (lseg) {
615 dprintk("%s: Using cached lseg %p for iomode %d)\n",
616 __func__, lseg, iomode);
617 goto out_unlock;
618 }
619
620 /* if LAYOUTGET already failed once we don't try again */
566052c5 621 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
e5e94017
BH
622 goto out_unlock;
623
cf7d63f1
FI
624 if (pnfs_layoutgets_blocked(lo, 0))
625 goto out_unlock;
626 atomic_inc(&lo->plh_outstanding);
627
628 get_layout_hdr_locked(lo);
e5e94017
BH
629 spin_unlock(&ino->i_lock);
630
631 lseg = send_layoutget(lo, ctx, iomode);
cf7d63f1
FI
632 atomic_dec(&lo->plh_outstanding);
633 put_layout_hdr(ino);
e5e94017
BH
634out:
635 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
566052c5 636 nfsi->layout->plh_flags, lseg);
e5e94017
BH
637 return lseg;
638out_unlock:
639 spin_unlock(&ino->i_lock);
640 goto out;
641}
b1f69b75
AA
642
643int
644pnfs_layout_process(struct nfs4_layoutget *lgp)
645{
646 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
647 struct nfs4_layoutget_res *res = &lgp->res;
648 struct pnfs_layout_segment *lseg;
b7edfaa1 649 struct inode *ino = lo->plh_inode;
b1f69b75
AA
650 int status = 0;
651
652 /* Inject layout blob into I/O device driver */
653 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
654 if (!lseg || IS_ERR(lseg)) {
655 if (!lseg)
656 status = -ENOMEM;
657 else
658 status = PTR_ERR(lseg);
659 dprintk("%s: Could not allocate layout: error %d\n",
660 __func__, status);
661 goto out;
662 }
663
664 spin_lock(&ino->i_lock);
665 init_lseg(lo, lseg);
566052c5 666 lseg->pls_range = res->range;
b1f69b75
AA
667 *lgp->lsegpp = lseg;
668 pnfs_insert_layout(lo, lseg);
669
670 /* Done processing layoutget. Set the layout stateid */
671 pnfs_set_layout_stateid(lo, &res->stateid);
672 spin_unlock(&ino->i_lock);
673out:
674 return status;
675}
676
677/*
678 * Device ID cache. Currently supports one layout type per struct nfs_client.
679 * Add layout type to the lookup key to expand to support multiple types.
680 */
681int
682pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
683 void (*free_callback)(struct pnfs_deviceid_node *))
684{
685 struct pnfs_deviceid_cache *c;
686
687 c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
688 if (!c)
689 return -ENOMEM;
690 spin_lock(&clp->cl_lock);
691 if (clp->cl_devid_cache != NULL) {
692 atomic_inc(&clp->cl_devid_cache->dc_ref);
693 dprintk("%s [kref [%d]]\n", __func__,
694 atomic_read(&clp->cl_devid_cache->dc_ref));
695 kfree(c);
696 } else {
697 /* kzalloc initializes hlists */
698 spin_lock_init(&c->dc_lock);
699 atomic_set(&c->dc_ref, 1);
700 c->dc_free_callback = free_callback;
701 clp->cl_devid_cache = c;
702 dprintk("%s [new]\n", __func__);
703 }
704 spin_unlock(&clp->cl_lock);
705 return 0;
706}
707EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
708
709/*
710 * Called from pnfs_layoutdriver_type->free_lseg
711 * last layout segment reference frees deviceid
712 */
713void
714pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
715 struct pnfs_deviceid_node *devid)
716{
717 struct nfs4_deviceid *id = &devid->de_id;
718 struct pnfs_deviceid_node *d;
719 struct hlist_node *n;
720 long h = nfs4_deviceid_hash(id);
721
722 dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
723 if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
724 return;
725
726 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
727 if (!memcmp(&d->de_id, id, sizeof(*id))) {
728 hlist_del_rcu(&d->de_node);
729 spin_unlock(&c->dc_lock);
730 synchronize_rcu();
731 c->dc_free_callback(devid);
732 return;
733 }
734 spin_unlock(&c->dc_lock);
735 /* Why wasn't it found in the list? */
736 BUG();
737}
738EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
739
740/* Find and reference a deviceid */
741struct pnfs_deviceid_node *
742pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
743{
744 struct pnfs_deviceid_node *d;
745 struct hlist_node *n;
746 long hash = nfs4_deviceid_hash(id);
747
748 dprintk("--> %s hash %ld\n", __func__, hash);
749 rcu_read_lock();
750 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
751 if (!memcmp(&d->de_id, id, sizeof(*id))) {
752 if (!atomic_inc_not_zero(&d->de_ref)) {
753 goto fail;
754 } else {
755 rcu_read_unlock();
756 return d;
757 }
758 }
759 }
760fail:
761 rcu_read_unlock();
762 return NULL;
763}
764EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
765
766/*
767 * Add a deviceid to the cache.
768 * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
769 */
770struct pnfs_deviceid_node *
771pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
772{
773 struct pnfs_deviceid_node *d;
774 long hash = nfs4_deviceid_hash(&new->de_id);
775
776 dprintk("--> %s hash %ld\n", __func__, hash);
777 spin_lock(&c->dc_lock);
778 d = pnfs_find_get_deviceid(c, &new->de_id);
779 if (d) {
780 spin_unlock(&c->dc_lock);
781 dprintk("%s [discard]\n", __func__);
782 c->dc_free_callback(new);
783 return d;
784 }
785 INIT_HLIST_NODE(&new->de_node);
786 atomic_set(&new->de_ref, 1);
787 hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
788 spin_unlock(&c->dc_lock);
789 dprintk("%s [new]\n", __func__);
790 return new;
791}
792EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
793
794void
795pnfs_put_deviceid_cache(struct nfs_client *clp)
796{
797 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
798
799 dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache);
800 if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
801 int i;
802 /* Verify cache is empty */
803 for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
804 BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
805 clp->cl_devid_cache = NULL;
806 spin_unlock(&clp->cl_lock);
807 kfree(local);
808 }
809}
810EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);