blob: 32b66468e5dbcdb3297298646ec4289ebeaf41b3 [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
Fred Isamancc6e5342011-01-06 11:36:28 +0000180/* Need to hold i_lock if caller does not already hold reference */
Benny Halevye5e94012010-10-20 00:18:01 -0400181static void
Fred Isamancc6e5342011-01-06 11:36:28 +0000182get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400183{
Fred Isamancc6e5342011-01-06 11:36:28 +0000184 atomic_inc(&lo->plh_refcount);
185}
186
187static void
188destroy_layout_hdr(struct pnfs_layout_hdr *lo)
189{
190 dprintk("%s: freeing layout cache %p\n", __func__, lo);
191 BUG_ON(!list_empty(&lo->plh_layouts));
192 NFS_I(lo->plh_inode)->layout = NULL;
193 kfree(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400194}
195
196static void
197put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
198{
Fred Isamancc6e5342011-01-06 11:36:28 +0000199 if (atomic_dec_and_test(&lo->plh_refcount))
200 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400201}
202
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400203void
Fred Isamancc6e5342011-01-06 11:36:28 +0000204put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400205{
Fred Isamancc6e5342011-01-06 11:36:28 +0000206 struct inode *inode = lo->plh_inode;
207
208 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
209 destroy_layout_hdr(lo);
210 spin_unlock(&inode->i_lock);
211 }
Andy Adamson974cec82010-10-20 00:18:02 -0400212}
213
214static void
215init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
216{
Fred Isaman566052c2011-01-06 11:36:20 +0000217 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000218 atomic_set(&lseg->pls_refcount, 1);
219 smp_mb();
220 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000221 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400222}
223
Fred Isaman4541d162011-01-06 11:36:23 +0000224static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400225{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000226 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400227
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400228 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000229 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000230 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400231}
232
Fred Isaman4541d162011-01-06 11:36:23 +0000233/* The use of tmp_list is necessary because pnfs_curr_ld->free_lseg
234 * could sleep, so must be called outside of the lock.
235 * Returns 1 if object was removed, otherwise return 0.
236 */
237static int
238put_lseg_locked(struct pnfs_layout_segment *lseg,
239 struct list_head *tmp_list)
Andy Adamson974cec82010-10-20 00:18:02 -0400240{
Fred Isaman4541d162011-01-06 11:36:23 +0000241 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
242 atomic_read(&lseg->pls_refcount),
243 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
244 if (atomic_dec_and_test(&lseg->pls_refcount)) {
245 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400246
Fred Isaman4541d162011-01-06 11:36:23 +0000247 BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
248 list_del(&lseg->pls_list);
249 if (list_empty(&lseg->pls_layout->plh_segs)) {
250 struct nfs_client *clp;
251
252 clp = NFS_SERVER(ino)->nfs_client;
253 spin_lock(&clp->cl_lock);
254 /* List does not take a reference, so no need for put here */
255 list_del_init(&lseg->pls_layout->plh_layouts);
256 spin_unlock(&clp->cl_lock);
257 }
258 list_add(&lseg->pls_list, tmp_list);
259 return 1;
260 }
261 return 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400262}
263
Fred Isaman4541d162011-01-06 11:36:23 +0000264static bool
265should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
266{
267 return (recall_iomode == IOMODE_ANY ||
268 lseg_iomode == recall_iomode);
269}
270
271/* Returns 1 if lseg is removed from list, 0 otherwise */
272static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
273 struct list_head *tmp_list)
274{
275 int rv = 0;
276
277 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
278 /* Remove the reference keeping the lseg in the
279 * list. It will now be removed when all
280 * outstanding io is finished.
281 */
282 rv = put_lseg_locked(lseg, tmp_list);
283 }
284 return rv;
285}
286
287/* Returns count of number of matching invalid lsegs remaining in list
288 * after call.
289 */
290static int
291mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
292 struct list_head *tmp_list,
293 u32 iomode)
Andy Adamson974cec82010-10-20 00:18:02 -0400294{
295 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000296 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400297
298 dprintk("%s:Begin lo %p\n", __func__, lo);
299
Fred Isaman4541d162011-01-06 11:36:23 +0000300 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
301 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
302 dprintk("%s: freeing lseg %p iomode %d "
303 "offset %llu length %llu\n", __func__,
304 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
305 lseg->pls_range.length);
306 invalid++;
307 removed += mark_lseg_invalid(lseg, tmp_list);
308 }
309 dprintk("%s:Return %i\n", __func__, invalid - removed);
310 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400311}
312
313static void
Fred Isaman4541d162011-01-06 11:36:23 +0000314pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400315{
Fred Isaman4541d162011-01-06 11:36:23 +0000316 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400317
Fred Isaman4541d162011-01-06 11:36:23 +0000318 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000319 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000320 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400321 }
322}
323
Benny Halevye5e94012010-10-20 00:18:01 -0400324void
325pnfs_destroy_layout(struct nfs_inode *nfsi)
326{
327 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400328 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400329
330 spin_lock(&nfsi->vfs_inode.i_lock);
331 lo = nfsi->layout;
332 if (lo) {
Fred Isaman4541d162011-01-06 11:36:23 +0000333 set_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags);
334 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
Benny Halevye5e94012010-10-20 00:18:01 -0400335 /* Matched by refcount set to 1 in alloc_init_layout_hdr */
336 put_layout_hdr_locked(lo);
337 }
338 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400339 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400340}
341
Andy Adamson974cec82010-10-20 00:18:02 -0400342/*
343 * Called by the state manger to remove all layouts established under an
344 * expired lease.
345 */
346void
347pnfs_destroy_all_layouts(struct nfs_client *clp)
348{
349 struct pnfs_layout_hdr *lo;
350 LIST_HEAD(tmp_list);
351
352 spin_lock(&clp->cl_lock);
353 list_splice_init(&clp->cl_layouts, &tmp_list);
354 spin_unlock(&clp->cl_lock);
355
356 while (!list_empty(&tmp_list)) {
357 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000358 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400359 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000360 lo->plh_inode->i_ino);
361 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400362 }
363}
364
Fred Isamanfd6002e2011-01-06 11:36:22 +0000365/* update lo->plh_stateid with new if is more recent */
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400366static void
367pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo,
368 const nfs4_stateid *new)
369{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000370 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400371
Fred Isamanfd6002e2011-01-06 11:36:22 +0000372 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
373 newseq = be32_to_cpu(new->stateid.seqid);
374 if ((int)(newseq - oldseq) > 0)
375 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400376}
377
Fred Isamancf7d63f2011-01-06 11:36:25 +0000378/* lget is set to 1 if called from inside send_layoutget call chain */
379static bool
380pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, int lget)
381{
382 return (list_empty(&lo->plh_segs) &&
383 (atomic_read(&lo->plh_outstanding) > lget));
384}
385
Fred Isamanfd6002e2011-01-06 11:36:22 +0000386int
387pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
388 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400389{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000390 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400391
392 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000393 spin_lock(&lo->plh_inode->i_lock);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000394 if (pnfs_layoutgets_blocked(lo, 1)) {
395 status = -EAGAIN;
396 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000397 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400398
Fred Isamanfd6002e2011-01-06 11:36:22 +0000399 do {
400 seq = read_seqbegin(&open_state->seqlock);
401 memcpy(dst->data, open_state->stateid.data,
402 sizeof(open_state->stateid.data));
403 } while (read_seqretry(&open_state->seqlock, seq));
404 } else
405 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
406 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400407 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000408 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400409}
410
411/*
412* Get layout from server.
413* for now, assume that whole file layouts are requested.
414* arg->offset: 0
415* arg->length: all ones
416*/
Benny Halevye5e94012010-10-20 00:18:01 -0400417static struct pnfs_layout_segment *
418send_layoutget(struct pnfs_layout_hdr *lo,
419 struct nfs_open_context *ctx,
420 u32 iomode)
421{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000422 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400423 struct nfs_server *server = NFS_SERVER(ino);
424 struct nfs4_layoutget *lgp;
425 struct pnfs_layout_segment *lseg = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -0400426
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400427 dprintk("--> %s\n", __func__);
428
429 BUG_ON(ctx == NULL);
430 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000431 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400432 return NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400433 lgp->args.minlength = NFS4_MAX_UINT64;
434 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
435 lgp->args.range.iomode = iomode;
436 lgp->args.range.offset = 0;
437 lgp->args.range.length = NFS4_MAX_UINT64;
438 lgp->args.type = server->pnfs_curr_ld->id;
439 lgp->args.inode = ino;
440 lgp->args.ctx = get_nfs_open_context(ctx);
441 lgp->lsegpp = &lseg;
442
443 /* Synchronously retrieve layout information from server and
444 * store in lseg.
445 */
446 nfs4_proc_layoutget(lgp);
447 if (!lseg) {
448 /* remember that LAYOUTGET failed and suspend trying */
Fred Isaman566052c2011-01-06 11:36:20 +0000449 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400450 }
Andy Adamson974cec82010-10-20 00:18:02 -0400451 return lseg;
452}
453
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400454/*
455 * Compare two layout segments for sorting into layout cache.
456 * We want to preferentially return RW over RO layouts, so ensure those
457 * are seen first.
458 */
459static s64
460cmp_layout(u32 iomode1, u32 iomode2)
461{
462 /* read > read/write */
463 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
464}
465
Andy Adamson974cec82010-10-20 00:18:02 -0400466static void
467pnfs_insert_layout(struct pnfs_layout_hdr *lo,
468 struct pnfs_layout_segment *lseg)
469{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400470 struct pnfs_layout_segment *lp;
471 int found = 0;
472
Andy Adamson974cec82010-10-20 00:18:02 -0400473 dprintk("%s:Begin\n", __func__);
474
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000475 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000476 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000477 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400478 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000479 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400480 dprintk("%s: inserted lseg %p "
481 "iomode %d offset %llu length %llu before "
482 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000483 __func__, lseg, lseg->pls_range.iomode,
484 lseg->pls_range.offset, lseg->pls_range.length,
485 lp, lp->pls_range.iomode, lp->pls_range.offset,
486 lp->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400487 found = 1;
488 break;
Andy Adamson974cec82010-10-20 00:18:02 -0400489 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400490 if (!found) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000491 list_add_tail(&lseg->pls_list, &lo->plh_segs);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400492 dprintk("%s: inserted lseg %p "
493 "iomode %d offset %llu length %llu at tail\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000494 __func__, lseg, lseg->pls_range.iomode,
495 lseg->pls_range.offset, lseg->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400496 }
Fred Isamancc6e5342011-01-06 11:36:28 +0000497 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400498
499 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400500}
501
502static struct pnfs_layout_hdr *
503alloc_init_layout_hdr(struct inode *ino)
504{
505 struct pnfs_layout_hdr *lo;
506
507 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
508 if (!lo)
509 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000510 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000511 INIT_LIST_HEAD(&lo->plh_layouts);
512 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000513 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400514 return lo;
515}
516
517static struct pnfs_layout_hdr *
518pnfs_find_alloc_layout(struct inode *ino)
519{
520 struct nfs_inode *nfsi = NFS_I(ino);
521 struct pnfs_layout_hdr *new = NULL;
522
523 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
524
525 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000526 if (nfsi->layout) {
527 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
528 return NULL;
529 else
530 return nfsi->layout;
531 }
Benny Halevye5e94012010-10-20 00:18:01 -0400532 spin_unlock(&ino->i_lock);
533 new = alloc_init_layout_hdr(ino);
534 spin_lock(&ino->i_lock);
535
536 if (likely(nfsi->layout == NULL)) /* Won the race? */
537 nfsi->layout = new;
538 else
539 kfree(new);
540 return nfsi->layout;
541}
542
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400543/*
544 * iomode matching rules:
545 * iomode lseg match
546 * ----- ----- -----
547 * ANY READ true
548 * ANY RW true
549 * RW READ false
550 * RW RW true
551 * READ READ true
552 * READ RW true
553 */
554static int
555is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
556{
Fred Isaman566052c2011-01-06 11:36:20 +0000557 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400558}
559
560/*
561 * lookup range in layout
562 */
Benny Halevye5e94012010-10-20 00:18:01 -0400563static struct pnfs_layout_segment *
564pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
565{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400566 struct pnfs_layout_segment *lseg, *ret = NULL;
567
568 dprintk("%s:Begin\n", __func__);
569
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000570 assert_spin_locked(&lo->plh_inode->i_lock);
571 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000572 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
573 is_matching_lseg(lseg, iomode)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400574 ret = lseg;
575 break;
576 }
Fred Isaman566052c2011-01-06 11:36:20 +0000577 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400578 break;
579 }
580
581 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000582 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400583 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400584}
585
586/*
587 * Layout segment is retreived from the server if not cached.
588 * The appropriate layout segment is referenced and returned to the caller.
589 */
590struct pnfs_layout_segment *
591pnfs_update_layout(struct inode *ino,
592 struct nfs_open_context *ctx,
593 enum pnfs_iomode iomode)
594{
595 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000596 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400597 struct pnfs_layout_hdr *lo;
598 struct pnfs_layout_segment *lseg = NULL;
599
600 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
601 return NULL;
602 spin_lock(&ino->i_lock);
603 lo = pnfs_find_alloc_layout(ino);
604 if (lo == NULL) {
605 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
606 goto out_unlock;
607 }
608
609 /* Check to see if the layout for the given range already exists */
610 lseg = pnfs_has_layout(lo, iomode);
611 if (lseg) {
612 dprintk("%s: Using cached lseg %p for iomode %d)\n",
613 __func__, lseg, iomode);
614 goto out_unlock;
615 }
616
617 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000618 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400619 goto out_unlock;
620
Fred Isamancf7d63f2011-01-06 11:36:25 +0000621 if (pnfs_layoutgets_blocked(lo, 0))
622 goto out_unlock;
623 atomic_inc(&lo->plh_outstanding);
624
Fred Isamancc6e5342011-01-06 11:36:28 +0000625 get_layout_hdr(lo);
Fred Isaman2130ff62011-01-06 11:36:26 +0000626 if (list_empty(&lo->plh_segs)) {
627 /* The lo must be on the clp list if there is any
628 * chance of a CB_LAYOUTRECALL(FILE) coming in.
629 */
630 spin_lock(&clp->cl_lock);
631 BUG_ON(!list_empty(&lo->plh_layouts));
632 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
633 spin_unlock(&clp->cl_lock);
634 }
Benny Halevye5e94012010-10-20 00:18:01 -0400635 spin_unlock(&ino->i_lock);
636
637 lseg = send_layoutget(lo, ctx, iomode);
Fred Isaman2130ff62011-01-06 11:36:26 +0000638 if (!lseg) {
639 spin_lock(&ino->i_lock);
640 if (list_empty(&lo->plh_segs)) {
641 spin_lock(&clp->cl_lock);
642 list_del_init(&lo->plh_layouts);
643 spin_unlock(&clp->cl_lock);
644 }
645 spin_unlock(&ino->i_lock);
646 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000647 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000648 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400649out:
650 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Fred Isaman566052c2011-01-06 11:36:20 +0000651 nfsi->layout->plh_flags, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400652 return lseg;
653out_unlock:
654 spin_unlock(&ino->i_lock);
655 goto out;
656}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400657
658int
659pnfs_layout_process(struct nfs4_layoutget *lgp)
660{
661 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
662 struct nfs4_layoutget_res *res = &lgp->res;
663 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000664 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400665 int status = 0;
666
Fred Isamanfc1794c2011-01-06 11:36:27 +0000667 /* Verify we got what we asked for.
668 * Note that because the xdr parsing only accepts a single
669 * element array, this can fail even if the server is behaving
670 * correctly.
671 */
672 if (lgp->args.range.iomode > res->range.iomode ||
673 res->range.offset != 0 ||
674 res->range.length != NFS4_MAX_UINT64) {
675 status = -EINVAL;
676 goto out;
677 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400678 /* Inject layout blob into I/O device driver */
679 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
680 if (!lseg || IS_ERR(lseg)) {
681 if (!lseg)
682 status = -ENOMEM;
683 else
684 status = PTR_ERR(lseg);
685 dprintk("%s: Could not allocate layout: error %d\n",
686 __func__, status);
687 goto out;
688 }
689
690 spin_lock(&ino->i_lock);
691 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000692 lseg->pls_range = res->range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400693 *lgp->lsegpp = lseg;
694 pnfs_insert_layout(lo, lseg);
695
696 /* Done processing layoutget. Set the layout stateid */
697 pnfs_set_layout_stateid(lo, &res->stateid);
698 spin_unlock(&ino->i_lock);
699out:
700 return status;
701}
702
703/*
704 * Device ID cache. Currently supports one layout type per struct nfs_client.
705 * Add layout type to the lookup key to expand to support multiple types.
706 */
707int
708pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
709 void (*free_callback)(struct pnfs_deviceid_node *))
710{
711 struct pnfs_deviceid_cache *c;
712
713 c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
714 if (!c)
715 return -ENOMEM;
716 spin_lock(&clp->cl_lock);
717 if (clp->cl_devid_cache != NULL) {
718 atomic_inc(&clp->cl_devid_cache->dc_ref);
719 dprintk("%s [kref [%d]]\n", __func__,
720 atomic_read(&clp->cl_devid_cache->dc_ref));
721 kfree(c);
722 } else {
723 /* kzalloc initializes hlists */
724 spin_lock_init(&c->dc_lock);
725 atomic_set(&c->dc_ref, 1);
726 c->dc_free_callback = free_callback;
727 clp->cl_devid_cache = c;
728 dprintk("%s [new]\n", __func__);
729 }
730 spin_unlock(&clp->cl_lock);
731 return 0;
732}
733EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
734
735/*
736 * Called from pnfs_layoutdriver_type->free_lseg
737 * last layout segment reference frees deviceid
738 */
739void
740pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
741 struct pnfs_deviceid_node *devid)
742{
743 struct nfs4_deviceid *id = &devid->de_id;
744 struct pnfs_deviceid_node *d;
745 struct hlist_node *n;
746 long h = nfs4_deviceid_hash(id);
747
748 dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
749 if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
750 return;
751
752 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
753 if (!memcmp(&d->de_id, id, sizeof(*id))) {
754 hlist_del_rcu(&d->de_node);
755 spin_unlock(&c->dc_lock);
756 synchronize_rcu();
757 c->dc_free_callback(devid);
758 return;
759 }
760 spin_unlock(&c->dc_lock);
761 /* Why wasn't it found in the list? */
762 BUG();
763}
764EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
765
766/* Find and reference a deviceid */
767struct pnfs_deviceid_node *
768pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
769{
770 struct pnfs_deviceid_node *d;
771 struct hlist_node *n;
772 long hash = nfs4_deviceid_hash(id);
773
774 dprintk("--> %s hash %ld\n", __func__, hash);
775 rcu_read_lock();
776 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
777 if (!memcmp(&d->de_id, id, sizeof(*id))) {
778 if (!atomic_inc_not_zero(&d->de_ref)) {
779 goto fail;
780 } else {
781 rcu_read_unlock();
782 return d;
783 }
784 }
785 }
786fail:
787 rcu_read_unlock();
788 return NULL;
789}
790EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
791
792/*
793 * Add a deviceid to the cache.
794 * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
795 */
796struct pnfs_deviceid_node *
797pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
798{
799 struct pnfs_deviceid_node *d;
800 long hash = nfs4_deviceid_hash(&new->de_id);
801
802 dprintk("--> %s hash %ld\n", __func__, hash);
803 spin_lock(&c->dc_lock);
804 d = pnfs_find_get_deviceid(c, &new->de_id);
805 if (d) {
806 spin_unlock(&c->dc_lock);
807 dprintk("%s [discard]\n", __func__);
808 c->dc_free_callback(new);
809 return d;
810 }
811 INIT_HLIST_NODE(&new->de_node);
812 atomic_set(&new->de_ref, 1);
813 hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
814 spin_unlock(&c->dc_lock);
815 dprintk("%s [new]\n", __func__);
816 return new;
817}
818EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
819
820void
821pnfs_put_deviceid_cache(struct nfs_client *clp)
822{
823 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
824
825 dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache);
826 if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
827 int i;
828 /* Verify cache is empty */
829 for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
830 BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
831 clp->cl_devid_cache = NULL;
832 spin_unlock(&clp->cl_lock);
833 kfree(local);
834 }
835}
836EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);