blob: 08313f536b45525e75a2253f7b6ddd2792d2dba9 [file] [log] [blame]
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001/*
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>
Andy Adamson974cec82010-10-20 00:18:02 -040031#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040032#include "pnfs.h"
33
34#define NFSDBG_FACILITY NFSDBG_PNFS
35
Fred Isaman02c35fc2010-10-20 00:17:59 -040036/* 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
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040063static struct pnfs_layoutdriver_type *
64find_pnfs_driver(u32 id)
65{
Fred Isaman02c35fc2010-10-20 00:17:59 -040066 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;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040072}
73
74void
75unset_pnfs_layoutdriver(struct nfs_server *nfss)
76{
Fred Isaman02c35fc2010-10-20 00:17:59 -040077 if (nfss->pnfs_curr_ld) {
Trond Myklebust1c787092010-10-21 16:56:48 -040078 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Fred Isaman02c35fc2010-10-20 00:17:59 -040079 module_put(nfss->pnfs_curr_ld->owner);
80 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040081 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 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400113 if (!try_module_get(ld_type->owner)) {
114 dprintk("%s: Could not grab reference on module\n", __func__);
115 goto out_no_driver;
116 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400117 server->pnfs_curr_ld = ld_type;
Trond Myklebust1c787092010-10-21 16:56:48 -0400118 if (ld_type->set_layoutdriver(server)) {
Fred Isaman02c35fc2010-10-20 00:17:59 -0400119 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 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400125 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}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400132
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 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400143 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 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400148
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);
Benny Halevye5e94012010-10-20 00:18:01 -0400175
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400176/*
177 * pNFS client layout cache
178 */
179
Benny Halevye5e94012010-10-20 00:18:01 -0400180static void
181get_layout_hdr_locked(struct pnfs_layout_hdr *lo)
182{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000183 assert_spin_locked(&lo->plh_inode->i_lock);
184 lo->plh_refcount++;
Benny Halevye5e94012010-10-20 00:18:01 -0400185}
186
187static void
188put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
189{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000190 assert_spin_locked(&lo->plh_inode->i_lock);
191 BUG_ON(lo->plh_refcount == 0);
Benny Halevye5e94012010-10-20 00:18:01 -0400192
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000193 lo->plh_refcount--;
194 if (!lo->plh_refcount) {
Benny Halevye5e94012010-10-20 00:18:01 -0400195 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000196 BUG_ON(!list_empty(&lo->plh_layouts));
197 NFS_I(lo->plh_inode)->layout = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -0400198 kfree(lo);
199 }
200}
201
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400202void
Andy Adamson974cec82010-10-20 00:18:02 -0400203put_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{
Fred Isaman566052c2011-01-06 11:36:20 +0000213 INIT_LIST_HEAD(&lseg->pls_list);
214 kref_init(&lseg->pls_refcount);
215 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400216}
217
218/* Called without i_lock held, as the free_lseg call may sleep */
219static void
220destroy_lseg(struct kref *kref)
221{
222 struct pnfs_layout_segment *lseg =
Fred Isaman566052c2011-01-06 11:36:20 +0000223 container_of(kref, struct pnfs_layout_segment, pls_refcount);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000224 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400225
226 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400227 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000228 /* Matched by get_layout_hdr in pnfs_insert_layout */
Andy Adamson974cec82010-10-20 00:18:02 -0400229 put_layout_hdr(ino);
230}
231
232static void
233put_lseg(struct pnfs_layout_segment *lseg)
234{
235 if (!lseg)
236 return;
237
238 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
Fred Isaman566052c2011-01-06 11:36:20 +0000239 atomic_read(&lseg->pls_refcount.refcount));
240 kref_put(&lseg->pls_refcount, destroy_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400241}
242
243static void
244pnfs_clear_lseg_list(struct pnfs_layout_hdr *lo, struct list_head *tmp_list)
245{
246 struct pnfs_layout_segment *lseg, *next;
247 struct nfs_client *clp;
248
249 dprintk("%s:Begin lo %p\n", __func__, lo);
250
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000251 assert_spin_locked(&lo->plh_inode->i_lock);
252 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
Andy Adamson974cec82010-10-20 00:18:02 -0400253 dprintk("%s: freeing lseg %p\n", __func__, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000254 list_move(&lseg->pls_list, tmp_list);
Andy Adamson974cec82010-10-20 00:18:02 -0400255 }
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000256 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
Andy Adamson974cec82010-10-20 00:18:02 -0400257 spin_lock(&clp->cl_lock);
258 /* List does not take a reference, so no need for put here */
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000259 list_del_init(&lo->plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400260 spin_unlock(&clp->cl_lock);
261
262 dprintk("%s:Return\n", __func__);
263}
264
265static void
266pnfs_free_lseg_list(struct list_head *tmp_list)
267{
268 struct pnfs_layout_segment *lseg;
269
270 while (!list_empty(tmp_list)) {
271 lseg = list_entry(tmp_list->next, struct pnfs_layout_segment,
Fred Isaman566052c2011-01-06 11:36:20 +0000272 pls_list);
Andy Adamson974cec82010-10-20 00:18:02 -0400273 dprintk("%s calling put_lseg on %p\n", __func__, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000274 list_del(&lseg->pls_list);
Andy Adamson974cec82010-10-20 00:18:02 -0400275 put_lseg(lseg);
276 }
277}
278
Benny Halevye5e94012010-10-20 00:18:01 -0400279void
280pnfs_destroy_layout(struct nfs_inode *nfsi)
281{
282 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400283 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400284
285 spin_lock(&nfsi->vfs_inode.i_lock);
286 lo = nfsi->layout;
287 if (lo) {
Andy Adamson974cec82010-10-20 00:18:02 -0400288 pnfs_clear_lseg_list(lo, &tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400289 /* Matched by refcount set to 1 in alloc_init_layout_hdr */
290 put_layout_hdr_locked(lo);
291 }
292 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400293 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400294}
295
Andy Adamson974cec82010-10-20 00:18:02 -0400296/*
297 * Called by the state manger to remove all layouts established under an
298 * expired lease.
299 */
300void
301pnfs_destroy_all_layouts(struct nfs_client *clp)
302{
303 struct pnfs_layout_hdr *lo;
304 LIST_HEAD(tmp_list);
305
306 spin_lock(&clp->cl_lock);
307 list_splice_init(&clp->cl_layouts, &tmp_list);
308 spin_unlock(&clp->cl_lock);
309
310 while (!list_empty(&tmp_list)) {
311 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000312 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400313 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000314 lo->plh_inode->i_ino);
315 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400316 }
317}
318
Fred Isamanfd6002e2011-01-06 11:36:22 +0000319/* update lo->plh_stateid with new if is more recent */
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400320static void
321pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo,
322 const nfs4_stateid *new)
323{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000324 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400325
Fred Isamanfd6002e2011-01-06 11:36:22 +0000326 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
327 newseq = be32_to_cpu(new->stateid.seqid);
328 if ((int)(newseq - oldseq) > 0)
329 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400330}
331
Fred Isamanfd6002e2011-01-06 11:36:22 +0000332int
333pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
334 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400335{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000336 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400337
338 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000339 spin_lock(&lo->plh_inode->i_lock);
340 if (list_empty(&lo->plh_segs)) {
341 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400342
Fred Isamanfd6002e2011-01-06 11:36:22 +0000343 do {
344 seq = read_seqbegin(&open_state->seqlock);
345 memcpy(dst->data, open_state->stateid.data,
346 sizeof(open_state->stateid.data));
347 } while (read_seqretry(&open_state->seqlock, seq));
348 } else
349 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
350 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400351 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000352 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400353}
354
355/*
356* Get layout from server.
357* for now, assume that whole file layouts are requested.
358* arg->offset: 0
359* arg->length: all ones
360*/
Benny Halevye5e94012010-10-20 00:18:01 -0400361static struct pnfs_layout_segment *
362send_layoutget(struct pnfs_layout_hdr *lo,
363 struct nfs_open_context *ctx,
364 u32 iomode)
365{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000366 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400367 struct nfs_server *server = NFS_SERVER(ino);
368 struct nfs4_layoutget *lgp;
369 struct pnfs_layout_segment *lseg = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -0400370
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400371 dprintk("--> %s\n", __func__);
372
373 BUG_ON(ctx == NULL);
374 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
375 if (lgp == NULL) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000376 put_layout_hdr(lo->plh_inode);
Andy Adamson974cec82010-10-20 00:18:02 -0400377 return NULL;
378 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400379 lgp->args.minlength = NFS4_MAX_UINT64;
380 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
381 lgp->args.range.iomode = iomode;
382 lgp->args.range.offset = 0;
383 lgp->args.range.length = NFS4_MAX_UINT64;
384 lgp->args.type = server->pnfs_curr_ld->id;
385 lgp->args.inode = ino;
386 lgp->args.ctx = get_nfs_open_context(ctx);
387 lgp->lsegpp = &lseg;
388
389 /* Synchronously retrieve layout information from server and
390 * store in lseg.
391 */
392 nfs4_proc_layoutget(lgp);
393 if (!lseg) {
394 /* remember that LAYOUTGET failed and suspend trying */
Fred Isaman566052c2011-01-06 11:36:20 +0000395 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400396 }
Andy Adamson974cec82010-10-20 00:18:02 -0400397 return lseg;
398}
399
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400400/*
401 * Compare two layout segments for sorting into layout cache.
402 * We want to preferentially return RW over RO layouts, so ensure those
403 * are seen first.
404 */
405static s64
406cmp_layout(u32 iomode1, u32 iomode2)
407{
408 /* read > read/write */
409 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
410}
411
Andy Adamson974cec82010-10-20 00:18:02 -0400412static void
413pnfs_insert_layout(struct pnfs_layout_hdr *lo,
414 struct pnfs_layout_segment *lseg)
415{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400416 struct pnfs_layout_segment *lp;
417 int found = 0;
418
Andy Adamson974cec82010-10-20 00:18:02 -0400419 dprintk("%s:Begin\n", __func__);
420
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000421 assert_spin_locked(&lo->plh_inode->i_lock);
422 if (list_empty(&lo->plh_segs)) {
423 struct nfs_client *clp = NFS_SERVER(lo->plh_inode)->nfs_client;
Andy Adamson974cec82010-10-20 00:18:02 -0400424
425 spin_lock(&clp->cl_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000426 BUG_ON(!list_empty(&lo->plh_layouts));
427 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400428 spin_unlock(&clp->cl_lock);
429 }
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000430 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000431 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400432 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000433 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400434 dprintk("%s: inserted lseg %p "
435 "iomode %d offset %llu length %llu before "
436 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000437 __func__, lseg, lseg->pls_range.iomode,
438 lseg->pls_range.offset, lseg->pls_range.length,
439 lp, lp->pls_range.iomode, lp->pls_range.offset,
440 lp->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400441 found = 1;
442 break;
Andy Adamson974cec82010-10-20 00:18:02 -0400443 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400444 if (!found) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000445 list_add_tail(&lseg->pls_list, &lo->plh_segs);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400446 dprintk("%s: inserted lseg %p "
447 "iomode %d offset %llu length %llu at tail\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000448 __func__, lseg, lseg->pls_range.iomode,
449 lseg->pls_range.offset, lseg->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400450 }
451 get_layout_hdr_locked(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400452
453 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400454}
455
456static struct pnfs_layout_hdr *
457alloc_init_layout_hdr(struct inode *ino)
458{
459 struct pnfs_layout_hdr *lo;
460
461 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
462 if (!lo)
463 return NULL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000464 lo->plh_refcount = 1;
465 INIT_LIST_HEAD(&lo->plh_layouts);
466 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000467 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400468 return lo;
469}
470
471static struct pnfs_layout_hdr *
472pnfs_find_alloc_layout(struct inode *ino)
473{
474 struct nfs_inode *nfsi = NFS_I(ino);
475 struct pnfs_layout_hdr *new = NULL;
476
477 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
478
479 assert_spin_locked(&ino->i_lock);
480 if (nfsi->layout)
481 return nfsi->layout;
482
483 spin_unlock(&ino->i_lock);
484 new = alloc_init_layout_hdr(ino);
485 spin_lock(&ino->i_lock);
486
487 if (likely(nfsi->layout == NULL)) /* Won the race? */
488 nfsi->layout = new;
489 else
490 kfree(new);
491 return nfsi->layout;
492}
493
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400494/*
495 * iomode matching rules:
496 * iomode lseg match
497 * ----- ----- -----
498 * ANY READ true
499 * ANY RW true
500 * RW READ false
501 * RW RW true
502 * READ READ true
503 * READ RW true
504 */
505static int
506is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
507{
Fred Isaman566052c2011-01-06 11:36:20 +0000508 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400509}
510
511/*
512 * lookup range in layout
513 */
Benny Halevye5e94012010-10-20 00:18:01 -0400514static struct pnfs_layout_segment *
515pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
516{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400517 struct pnfs_layout_segment *lseg, *ret = NULL;
518
519 dprintk("%s:Begin\n", __func__);
520
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000521 assert_spin_locked(&lo->plh_inode->i_lock);
522 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400523 if (is_matching_lseg(lseg, iomode)) {
524 ret = lseg;
525 break;
526 }
Fred Isaman566052c2011-01-06 11:36:20 +0000527 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400528 break;
529 }
530
531 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000532 __func__, ret, ret ? atomic_read(&ret->pls_refcount.refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400533 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400534}
535
536/*
537 * Layout segment is retreived from the server if not cached.
538 * The appropriate layout segment is referenced and returned to the caller.
539 */
540struct pnfs_layout_segment *
541pnfs_update_layout(struct inode *ino,
542 struct nfs_open_context *ctx,
543 enum pnfs_iomode iomode)
544{
545 struct nfs_inode *nfsi = NFS_I(ino);
546 struct pnfs_layout_hdr *lo;
547 struct pnfs_layout_segment *lseg = NULL;
548
549 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
550 return NULL;
551 spin_lock(&ino->i_lock);
552 lo = pnfs_find_alloc_layout(ino);
553 if (lo == NULL) {
554 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
555 goto out_unlock;
556 }
557
558 /* Check to see if the layout for the given range already exists */
559 lseg = pnfs_has_layout(lo, iomode);
560 if (lseg) {
561 dprintk("%s: Using cached lseg %p for iomode %d)\n",
562 __func__, lseg, iomode);
563 goto out_unlock;
564 }
565
566 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000567 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400568 goto out_unlock;
569
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400570 get_layout_hdr_locked(lo); /* Matched in nfs4_layoutget_release */
Benny Halevye5e94012010-10-20 00:18:01 -0400571 spin_unlock(&ino->i_lock);
572
573 lseg = send_layoutget(lo, ctx, iomode);
574out:
575 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Fred Isaman566052c2011-01-06 11:36:20 +0000576 nfsi->layout->plh_flags, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400577 return lseg;
578out_unlock:
579 spin_unlock(&ino->i_lock);
580 goto out;
581}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400582
583int
584pnfs_layout_process(struct nfs4_layoutget *lgp)
585{
586 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
587 struct nfs4_layoutget_res *res = &lgp->res;
588 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000589 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400590 int status = 0;
591
592 /* Inject layout blob into I/O device driver */
593 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
594 if (!lseg || IS_ERR(lseg)) {
595 if (!lseg)
596 status = -ENOMEM;
597 else
598 status = PTR_ERR(lseg);
599 dprintk("%s: Could not allocate layout: error %d\n",
600 __func__, status);
601 goto out;
602 }
603
604 spin_lock(&ino->i_lock);
605 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000606 lseg->pls_range = res->range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400607 *lgp->lsegpp = lseg;
608 pnfs_insert_layout(lo, lseg);
609
610 /* Done processing layoutget. Set the layout stateid */
611 pnfs_set_layout_stateid(lo, &res->stateid);
612 spin_unlock(&ino->i_lock);
613out:
614 return status;
615}
616
617/*
618 * Device ID cache. Currently supports one layout type per struct nfs_client.
619 * Add layout type to the lookup key to expand to support multiple types.
620 */
621int
622pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
623 void (*free_callback)(struct pnfs_deviceid_node *))
624{
625 struct pnfs_deviceid_cache *c;
626
627 c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
628 if (!c)
629 return -ENOMEM;
630 spin_lock(&clp->cl_lock);
631 if (clp->cl_devid_cache != NULL) {
632 atomic_inc(&clp->cl_devid_cache->dc_ref);
633 dprintk("%s [kref [%d]]\n", __func__,
634 atomic_read(&clp->cl_devid_cache->dc_ref));
635 kfree(c);
636 } else {
637 /* kzalloc initializes hlists */
638 spin_lock_init(&c->dc_lock);
639 atomic_set(&c->dc_ref, 1);
640 c->dc_free_callback = free_callback;
641 clp->cl_devid_cache = c;
642 dprintk("%s [new]\n", __func__);
643 }
644 spin_unlock(&clp->cl_lock);
645 return 0;
646}
647EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
648
649/*
650 * Called from pnfs_layoutdriver_type->free_lseg
651 * last layout segment reference frees deviceid
652 */
653void
654pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
655 struct pnfs_deviceid_node *devid)
656{
657 struct nfs4_deviceid *id = &devid->de_id;
658 struct pnfs_deviceid_node *d;
659 struct hlist_node *n;
660 long h = nfs4_deviceid_hash(id);
661
662 dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
663 if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
664 return;
665
666 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
667 if (!memcmp(&d->de_id, id, sizeof(*id))) {
668 hlist_del_rcu(&d->de_node);
669 spin_unlock(&c->dc_lock);
670 synchronize_rcu();
671 c->dc_free_callback(devid);
672 return;
673 }
674 spin_unlock(&c->dc_lock);
675 /* Why wasn't it found in the list? */
676 BUG();
677}
678EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
679
680/* Find and reference a deviceid */
681struct pnfs_deviceid_node *
682pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
683{
684 struct pnfs_deviceid_node *d;
685 struct hlist_node *n;
686 long hash = nfs4_deviceid_hash(id);
687
688 dprintk("--> %s hash %ld\n", __func__, hash);
689 rcu_read_lock();
690 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
691 if (!memcmp(&d->de_id, id, sizeof(*id))) {
692 if (!atomic_inc_not_zero(&d->de_ref)) {
693 goto fail;
694 } else {
695 rcu_read_unlock();
696 return d;
697 }
698 }
699 }
700fail:
701 rcu_read_unlock();
702 return NULL;
703}
704EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
705
706/*
707 * Add a deviceid to the cache.
708 * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
709 */
710struct pnfs_deviceid_node *
711pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
712{
713 struct pnfs_deviceid_node *d;
714 long hash = nfs4_deviceid_hash(&new->de_id);
715
716 dprintk("--> %s hash %ld\n", __func__, hash);
717 spin_lock(&c->dc_lock);
718 d = pnfs_find_get_deviceid(c, &new->de_id);
719 if (d) {
720 spin_unlock(&c->dc_lock);
721 dprintk("%s [discard]\n", __func__);
722 c->dc_free_callback(new);
723 return d;
724 }
725 INIT_HLIST_NODE(&new->de_node);
726 atomic_set(&new->de_ref, 1);
727 hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
728 spin_unlock(&c->dc_lock);
729 dprintk("%s [new]\n", __func__);
730 return new;
731}
732EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
733
734void
735pnfs_put_deviceid_cache(struct nfs_client *clp)
736{
737 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
738
739 dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache);
740 if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
741 int i;
742 /* Verify cache is empty */
743 for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
744 BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
745 clp->cl_devid_cache = NULL;
746 spin_unlock(&clp->cl_lock);
747 kfree(local);
748 }
749}
750EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);