blob: bf4186b8f2fcea6a0ca0deb884f63ad16ecff6f4 [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 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000181void
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);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000257 clear_bit(NFS_LAYOUT_BULK_RECALL, &lseg->pls_layout->plh_flags);
Fred Isaman4541d162011-01-06 11:36:23 +0000258 }
259 list_add(&lseg->pls_list, tmp_list);
260 return 1;
261 }
262 return 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400263}
264
Fred Isaman4541d162011-01-06 11:36:23 +0000265static bool
266should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
267{
268 return (recall_iomode == IOMODE_ANY ||
269 lseg_iomode == recall_iomode);
270}
271
272/* Returns 1 if lseg is removed from list, 0 otherwise */
273static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
274 struct list_head *tmp_list)
275{
276 int rv = 0;
277
278 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
279 /* Remove the reference keeping the lseg in the
280 * list. It will now be removed when all
281 * outstanding io is finished.
282 */
283 rv = put_lseg_locked(lseg, tmp_list);
284 }
285 return rv;
286}
287
288/* Returns count of number of matching invalid lsegs remaining in list
289 * after call.
290 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000291int
Fred Isaman4541d162011-01-06 11:36:23 +0000292mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
293 struct list_head *tmp_list,
294 u32 iomode)
Andy Adamson974cec82010-10-20 00:18:02 -0400295{
296 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000297 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400298
299 dprintk("%s:Begin lo %p\n", __func__, lo);
300
Fred Isaman4541d162011-01-06 11:36:23 +0000301 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
302 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
303 dprintk("%s: freeing lseg %p iomode %d "
304 "offset %llu length %llu\n", __func__,
305 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
306 lseg->pls_range.length);
307 invalid++;
308 removed += mark_lseg_invalid(lseg, tmp_list);
309 }
310 dprintk("%s:Return %i\n", __func__, invalid - removed);
311 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400312}
313
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000314void
Fred Isaman4541d162011-01-06 11:36:23 +0000315pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400316{
Fred Isaman4541d162011-01-06 11:36:23 +0000317 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400318
Fred Isaman4541d162011-01-06 11:36:23 +0000319 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000320 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000321 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400322 }
323}
324
Benny Halevye5e94012010-10-20 00:18:01 -0400325void
326pnfs_destroy_layout(struct nfs_inode *nfsi)
327{
328 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400329 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400330
331 spin_lock(&nfsi->vfs_inode.i_lock);
332 lo = nfsi->layout;
333 if (lo) {
Fred Isaman4541d162011-01-06 11:36:23 +0000334 set_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags);
335 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
Benny Halevye5e94012010-10-20 00:18:01 -0400336 /* Matched by refcount set to 1 in alloc_init_layout_hdr */
337 put_layout_hdr_locked(lo);
338 }
339 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400340 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400341}
342
Andy Adamson974cec82010-10-20 00:18:02 -0400343/*
344 * Called by the state manger to remove all layouts established under an
345 * expired lease.
346 */
347void
348pnfs_destroy_all_layouts(struct nfs_client *clp)
349{
350 struct pnfs_layout_hdr *lo;
351 LIST_HEAD(tmp_list);
352
353 spin_lock(&clp->cl_lock);
354 list_splice_init(&clp->cl_layouts, &tmp_list);
355 spin_unlock(&clp->cl_lock);
356
357 while (!list_empty(&tmp_list)) {
358 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000359 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400360 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000361 lo->plh_inode->i_ino);
362 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400363 }
364}
365
Fred Isamanfd6002e2011-01-06 11:36:22 +0000366/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000367void
368pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
369 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400370{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000371 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400372
Fred Isamanfd6002e2011-01-06 11:36:22 +0000373 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
374 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000375 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000376 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000377 if (update_barrier) {
378 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
379
380 if ((int)(new_barrier - lo->plh_barrier))
381 lo->plh_barrier = new_barrier;
382 } else {
383 /* Because of wraparound, we want to keep the barrier
384 * "close" to the current seqids. It needs to be
385 * within 2**31 to count as "behind", so if it
386 * gets too near that limit, give us a litle leeway
387 * and bring it to within 2**30.
388 * NOTE - and yes, this is all unsigned arithmetic.
389 */
390 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
391 lo->plh_barrier = newseq - (1 << 30);
392 }
393 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400394}
395
Fred Isamancf7d63f2011-01-06 11:36:25 +0000396/* lget is set to 1 if called from inside send_layoutget call chain */
397static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000398pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
399 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000400{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000401 if ((stateid) &&
402 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
403 return true;
404 return test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
405 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000406 (atomic_read(&lo->plh_outstanding) > lget));
407}
408
Fred Isamanfd6002e2011-01-06 11:36:22 +0000409int
410pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
411 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400412{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000413 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400414
415 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000416 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000417 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000418 status = -EAGAIN;
419 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000420 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400421
Fred Isamanfd6002e2011-01-06 11:36:22 +0000422 do {
423 seq = read_seqbegin(&open_state->seqlock);
424 memcpy(dst->data, open_state->stateid.data,
425 sizeof(open_state->stateid.data));
426 } while (read_seqretry(&open_state->seqlock, seq));
427 } else
428 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
429 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400430 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000431 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400432}
433
434/*
435* Get layout from server.
436* for now, assume that whole file layouts are requested.
437* arg->offset: 0
438* arg->length: all ones
439*/
Benny Halevye5e94012010-10-20 00:18:01 -0400440static struct pnfs_layout_segment *
441send_layoutget(struct pnfs_layout_hdr *lo,
442 struct nfs_open_context *ctx,
443 u32 iomode)
444{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000445 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400446 struct nfs_server *server = NFS_SERVER(ino);
447 struct nfs4_layoutget *lgp;
448 struct pnfs_layout_segment *lseg = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -0400449
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400450 dprintk("--> %s\n", __func__);
451
452 BUG_ON(ctx == NULL);
453 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000454 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400455 return NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400456 lgp->args.minlength = NFS4_MAX_UINT64;
457 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
458 lgp->args.range.iomode = iomode;
459 lgp->args.range.offset = 0;
460 lgp->args.range.length = NFS4_MAX_UINT64;
461 lgp->args.type = server->pnfs_curr_ld->id;
462 lgp->args.inode = ino;
463 lgp->args.ctx = get_nfs_open_context(ctx);
464 lgp->lsegpp = &lseg;
465
466 /* Synchronously retrieve layout information from server and
467 * store in lseg.
468 */
469 nfs4_proc_layoutget(lgp);
470 if (!lseg) {
471 /* remember that LAYOUTGET failed and suspend trying */
Fred Isaman566052c2011-01-06 11:36:20 +0000472 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400473 }
Andy Adamson974cec82010-10-20 00:18:02 -0400474 return lseg;
475}
476
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400477/*
478 * Compare two layout segments for sorting into layout cache.
479 * We want to preferentially return RW over RO layouts, so ensure those
480 * are seen first.
481 */
482static s64
483cmp_layout(u32 iomode1, u32 iomode2)
484{
485 /* read > read/write */
486 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
487}
488
Andy Adamson974cec82010-10-20 00:18:02 -0400489static void
490pnfs_insert_layout(struct pnfs_layout_hdr *lo,
491 struct pnfs_layout_segment *lseg)
492{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400493 struct pnfs_layout_segment *lp;
494 int found = 0;
495
Andy Adamson974cec82010-10-20 00:18:02 -0400496 dprintk("%s:Begin\n", __func__);
497
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000498 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000499 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000500 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400501 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000502 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400503 dprintk("%s: inserted lseg %p "
504 "iomode %d offset %llu length %llu before "
505 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000506 __func__, lseg, lseg->pls_range.iomode,
507 lseg->pls_range.offset, lseg->pls_range.length,
508 lp, lp->pls_range.iomode, lp->pls_range.offset,
509 lp->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400510 found = 1;
511 break;
Andy Adamson974cec82010-10-20 00:18:02 -0400512 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400513 if (!found) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000514 list_add_tail(&lseg->pls_list, &lo->plh_segs);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400515 dprintk("%s: inserted lseg %p "
516 "iomode %d offset %llu length %llu at tail\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000517 __func__, lseg, lseg->pls_range.iomode,
518 lseg->pls_range.offset, lseg->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400519 }
Fred Isamancc6e5342011-01-06 11:36:28 +0000520 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400521
522 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400523}
524
525static struct pnfs_layout_hdr *
526alloc_init_layout_hdr(struct inode *ino)
527{
528 struct pnfs_layout_hdr *lo;
529
530 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
531 if (!lo)
532 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000533 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000534 INIT_LIST_HEAD(&lo->plh_layouts);
535 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000536 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000537 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400538 return lo;
539}
540
541static struct pnfs_layout_hdr *
542pnfs_find_alloc_layout(struct inode *ino)
543{
544 struct nfs_inode *nfsi = NFS_I(ino);
545 struct pnfs_layout_hdr *new = NULL;
546
547 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
548
549 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000550 if (nfsi->layout) {
551 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
552 return NULL;
553 else
554 return nfsi->layout;
555 }
Benny Halevye5e94012010-10-20 00:18:01 -0400556 spin_unlock(&ino->i_lock);
557 new = alloc_init_layout_hdr(ino);
558 spin_lock(&ino->i_lock);
559
560 if (likely(nfsi->layout == NULL)) /* Won the race? */
561 nfsi->layout = new;
562 else
563 kfree(new);
564 return nfsi->layout;
565}
566
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400567/*
568 * iomode matching rules:
569 * iomode lseg match
570 * ----- ----- -----
571 * ANY READ true
572 * ANY RW true
573 * RW READ false
574 * RW RW true
575 * READ READ true
576 * READ RW true
577 */
578static int
579is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
580{
Fred Isaman566052c2011-01-06 11:36:20 +0000581 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400582}
583
584/*
585 * lookup range in layout
586 */
Benny Halevye5e94012010-10-20 00:18:01 -0400587static struct pnfs_layout_segment *
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000588pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
Benny Halevye5e94012010-10-20 00:18:01 -0400589{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400590 struct pnfs_layout_segment *lseg, *ret = NULL;
591
592 dprintk("%s:Begin\n", __func__);
593
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000594 assert_spin_locked(&lo->plh_inode->i_lock);
595 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000596 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
597 is_matching_lseg(lseg, iomode)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400598 ret = lseg;
599 break;
600 }
Fred Isaman566052c2011-01-06 11:36:20 +0000601 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400602 break;
603 }
604
605 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000606 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400607 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400608}
609
610/*
611 * Layout segment is retreived from the server if not cached.
612 * The appropriate layout segment is referenced and returned to the caller.
613 */
614struct pnfs_layout_segment *
615pnfs_update_layout(struct inode *ino,
616 struct nfs_open_context *ctx,
617 enum pnfs_iomode iomode)
618{
619 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000620 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400621 struct pnfs_layout_hdr *lo;
622 struct pnfs_layout_segment *lseg = NULL;
623
624 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
625 return NULL;
626 spin_lock(&ino->i_lock);
627 lo = pnfs_find_alloc_layout(ino);
628 if (lo == NULL) {
629 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
630 goto out_unlock;
631 }
632
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000633 /* Do we even need to bother with this? */
634 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
635 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
636 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400637 goto out_unlock;
638 }
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000639 /* Check to see if the layout for the given range already exists */
640 lseg = pnfs_find_lseg(lo, iomode);
641 if (lseg)
642 goto out_unlock;
Benny Halevye5e94012010-10-20 00:18:01 -0400643
644 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000645 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400646 goto out_unlock;
647
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000648 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000649 goto out_unlock;
650 atomic_inc(&lo->plh_outstanding);
651
Fred Isamancc6e5342011-01-06 11:36:28 +0000652 get_layout_hdr(lo);
Fred Isaman2130ff62011-01-06 11:36:26 +0000653 if (list_empty(&lo->plh_segs)) {
654 /* The lo must be on the clp list if there is any
655 * chance of a CB_LAYOUTRECALL(FILE) coming in.
656 */
657 spin_lock(&clp->cl_lock);
658 BUG_ON(!list_empty(&lo->plh_layouts));
659 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
660 spin_unlock(&clp->cl_lock);
661 }
Benny Halevye5e94012010-10-20 00:18:01 -0400662 spin_unlock(&ino->i_lock);
663
664 lseg = send_layoutget(lo, ctx, iomode);
Fred Isaman2130ff62011-01-06 11:36:26 +0000665 if (!lseg) {
666 spin_lock(&ino->i_lock);
667 if (list_empty(&lo->plh_segs)) {
668 spin_lock(&clp->cl_lock);
669 list_del_init(&lo->plh_layouts);
670 spin_unlock(&clp->cl_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000671 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isaman2130ff62011-01-06 11:36:26 +0000672 }
673 spin_unlock(&ino->i_lock);
674 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000675 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000676 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400677out:
678 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Fred Isaman566052c2011-01-06 11:36:20 +0000679 nfsi->layout->plh_flags, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400680 return lseg;
681out_unlock:
682 spin_unlock(&ino->i_lock);
683 goto out;
684}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400685
686int
687pnfs_layout_process(struct nfs4_layoutget *lgp)
688{
689 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
690 struct nfs4_layoutget_res *res = &lgp->res;
691 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000692 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000693 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400694 int status = 0;
695
Fred Isamanfc1794c2011-01-06 11:36:27 +0000696 /* Verify we got what we asked for.
697 * Note that because the xdr parsing only accepts a single
698 * element array, this can fail even if the server is behaving
699 * correctly.
700 */
701 if (lgp->args.range.iomode > res->range.iomode ||
702 res->range.offset != 0 ||
703 res->range.length != NFS4_MAX_UINT64) {
704 status = -EINVAL;
705 goto out;
706 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400707 /* Inject layout blob into I/O device driver */
708 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
709 if (!lseg || IS_ERR(lseg)) {
710 if (!lseg)
711 status = -ENOMEM;
712 else
713 status = PTR_ERR(lseg);
714 dprintk("%s: Could not allocate layout: error %d\n",
715 __func__, status);
716 goto out;
717 }
718
719 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000720 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
721 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
722 dprintk("%s forget reply due to recall\n", __func__);
723 goto out_forget_reply;
724 }
725
726 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
727 dprintk("%s forget reply due to state\n", __func__);
728 goto out_forget_reply;
729 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400730 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000731 lseg->pls_range = res->range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400732 *lgp->lsegpp = lseg;
733 pnfs_insert_layout(lo, lseg);
734
735 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000736 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400737 spin_unlock(&ino->i_lock);
738out:
739 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000740
741out_forget_reply:
742 spin_unlock(&ino->i_lock);
743 lseg->pls_layout = lo;
744 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
745 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400746}
747
748/*
749 * Device ID cache. Currently supports one layout type per struct nfs_client.
750 * Add layout type to the lookup key to expand to support multiple types.
751 */
752int
753pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
754 void (*free_callback)(struct pnfs_deviceid_node *))
755{
756 struct pnfs_deviceid_cache *c;
757
758 c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
759 if (!c)
760 return -ENOMEM;
761 spin_lock(&clp->cl_lock);
762 if (clp->cl_devid_cache != NULL) {
763 atomic_inc(&clp->cl_devid_cache->dc_ref);
764 dprintk("%s [kref [%d]]\n", __func__,
765 atomic_read(&clp->cl_devid_cache->dc_ref));
766 kfree(c);
767 } else {
768 /* kzalloc initializes hlists */
769 spin_lock_init(&c->dc_lock);
770 atomic_set(&c->dc_ref, 1);
771 c->dc_free_callback = free_callback;
772 clp->cl_devid_cache = c;
773 dprintk("%s [new]\n", __func__);
774 }
775 spin_unlock(&clp->cl_lock);
776 return 0;
777}
778EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
779
780/*
781 * Called from pnfs_layoutdriver_type->free_lseg
782 * last layout segment reference frees deviceid
783 */
784void
785pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
786 struct pnfs_deviceid_node *devid)
787{
788 struct nfs4_deviceid *id = &devid->de_id;
789 struct pnfs_deviceid_node *d;
790 struct hlist_node *n;
791 long h = nfs4_deviceid_hash(id);
792
793 dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
794 if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
795 return;
796
797 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
798 if (!memcmp(&d->de_id, id, sizeof(*id))) {
799 hlist_del_rcu(&d->de_node);
800 spin_unlock(&c->dc_lock);
801 synchronize_rcu();
802 c->dc_free_callback(devid);
803 return;
804 }
805 spin_unlock(&c->dc_lock);
806 /* Why wasn't it found in the list? */
807 BUG();
808}
809EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
810
811/* Find and reference a deviceid */
812struct pnfs_deviceid_node *
813pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
814{
815 struct pnfs_deviceid_node *d;
816 struct hlist_node *n;
817 long hash = nfs4_deviceid_hash(id);
818
819 dprintk("--> %s hash %ld\n", __func__, hash);
820 rcu_read_lock();
821 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
822 if (!memcmp(&d->de_id, id, sizeof(*id))) {
823 if (!atomic_inc_not_zero(&d->de_ref)) {
824 goto fail;
825 } else {
826 rcu_read_unlock();
827 return d;
828 }
829 }
830 }
831fail:
832 rcu_read_unlock();
833 return NULL;
834}
835EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
836
837/*
838 * Add a deviceid to the cache.
839 * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
840 */
841struct pnfs_deviceid_node *
842pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
843{
844 struct pnfs_deviceid_node *d;
845 long hash = nfs4_deviceid_hash(&new->de_id);
846
847 dprintk("--> %s hash %ld\n", __func__, hash);
848 spin_lock(&c->dc_lock);
849 d = pnfs_find_get_deviceid(c, &new->de_id);
850 if (d) {
851 spin_unlock(&c->dc_lock);
852 dprintk("%s [discard]\n", __func__);
853 c->dc_free_callback(new);
854 return d;
855 }
856 INIT_HLIST_NODE(&new->de_node);
857 atomic_set(&new->de_ref, 1);
858 hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
859 spin_unlock(&c->dc_lock);
860 dprintk("%s [new]\n", __func__);
861 return new;
862}
863EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
864
865void
866pnfs_put_deviceid_cache(struct nfs_client *clp)
867{
868 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
869
870 dprintk("--> %s cl_devid_cache %p\n", __func__, clp->cl_devid_cache);
871 if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
872 int i;
873 /* Verify cache is empty */
874 for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
875 BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
876 clp->cl_devid_cache = NULL;
877 spin_unlock(&clp->cl_lock);
878 kfree(local);
879 }
880}
881EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);