blob: c8d9b2148cb15f5f0001ec4c480c77c0320553d1 [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 Isaman38511722011-02-03 18:28:50 +0000258 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
259 /* Matched by initial refcount set in alloc_init_layout_hdr */
260 put_layout_hdr_locked(lseg->pls_layout);
Fred Isaman4541d162011-01-06 11:36:23 +0000261 }
Fred Isamanf7e89172011-01-06 11:36:32 +0000262 rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq);
Fred Isaman4541d162011-01-06 11:36:23 +0000263 list_add(&lseg->pls_list, tmp_list);
264 return 1;
265 }
266 return 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400267}
268
Fred Isaman4541d162011-01-06 11:36:23 +0000269static bool
270should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
271{
272 return (recall_iomode == IOMODE_ANY ||
273 lseg_iomode == recall_iomode);
274}
275
276/* Returns 1 if lseg is removed from list, 0 otherwise */
277static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
278 struct list_head *tmp_list)
279{
280 int rv = 0;
281
282 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
283 /* Remove the reference keeping the lseg in the
284 * list. It will now be removed when all
285 * outstanding io is finished.
286 */
287 rv = put_lseg_locked(lseg, tmp_list);
288 }
289 return rv;
290}
291
292/* Returns count of number of matching invalid lsegs remaining in list
293 * after call.
294 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000295int
Fred Isaman4541d162011-01-06 11:36:23 +0000296mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
297 struct list_head *tmp_list,
298 u32 iomode)
Andy Adamson974cec82010-10-20 00:18:02 -0400299{
300 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000301 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400302
303 dprintk("%s:Begin lo %p\n", __func__, lo);
304
Fred Isaman38511722011-02-03 18:28:50 +0000305 if (list_empty(&lo->plh_segs)) {
306 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
307 put_layout_hdr_locked(lo);
308 return 0;
309 }
Fred Isaman4541d162011-01-06 11:36:23 +0000310 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
311 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
312 dprintk("%s: freeing lseg %p iomode %d "
313 "offset %llu length %llu\n", __func__,
314 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
315 lseg->pls_range.length);
316 invalid++;
317 removed += mark_lseg_invalid(lseg, tmp_list);
318 }
319 dprintk("%s:Return %i\n", __func__, invalid - removed);
320 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400321}
322
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000323void
Fred Isaman4541d162011-01-06 11:36:23 +0000324pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400325{
Fred Isaman4541d162011-01-06 11:36:23 +0000326 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson974cec82010-10-20 00:18:02 -0400327
Fred Isaman4541d162011-01-06 11:36:23 +0000328 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000329 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000330 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400331 }
332}
333
Benny Halevye5e94012010-10-20 00:18:01 -0400334void
335pnfs_destroy_layout(struct nfs_inode *nfsi)
336{
337 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400338 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400339
340 spin_lock(&nfsi->vfs_inode.i_lock);
341 lo = nfsi->layout;
342 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000343 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Fred Isaman4541d162011-01-06 11:36:23 +0000344 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
Benny Halevye5e94012010-10-20 00:18:01 -0400345 }
346 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400347 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400348}
349
Andy Adamson974cec82010-10-20 00:18:02 -0400350/*
351 * Called by the state manger to remove all layouts established under an
352 * expired lease.
353 */
354void
355pnfs_destroy_all_layouts(struct nfs_client *clp)
356{
357 struct pnfs_layout_hdr *lo;
358 LIST_HEAD(tmp_list);
359
360 spin_lock(&clp->cl_lock);
361 list_splice_init(&clp->cl_layouts, &tmp_list);
362 spin_unlock(&clp->cl_lock);
363
364 while (!list_empty(&tmp_list)) {
365 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000366 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400367 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000368 lo->plh_inode->i_ino);
369 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400370 }
371}
372
Fred Isamanfd6002e2011-01-06 11:36:22 +0000373/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000374void
375pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
376 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400377{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000378 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400379
Fred Isamanfd6002e2011-01-06 11:36:22 +0000380 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
381 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000382 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000383 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000384 if (update_barrier) {
385 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
386
387 if ((int)(new_barrier - lo->plh_barrier))
388 lo->plh_barrier = new_barrier;
389 } else {
390 /* Because of wraparound, we want to keep the barrier
391 * "close" to the current seqids. It needs to be
392 * within 2**31 to count as "behind", so if it
393 * gets too near that limit, give us a litle leeway
394 * and bring it to within 2**30.
395 * NOTE - and yes, this is all unsigned arithmetic.
396 */
397 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
398 lo->plh_barrier = newseq - (1 << 30);
399 }
400 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400401}
402
Fred Isamancf7d63f2011-01-06 11:36:25 +0000403/* lget is set to 1 if called from inside send_layoutget call chain */
404static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000405pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
406 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000407{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000408 if ((stateid) &&
409 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
410 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000411 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000412 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000413 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000414 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000415 (atomic_read(&lo->plh_outstanding) > lget));
416}
417
Fred Isamanfd6002e2011-01-06 11:36:22 +0000418int
419pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
420 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400421{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000422 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400423
424 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000425 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000426 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000427 status = -EAGAIN;
428 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000429 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400430
Fred Isamanfd6002e2011-01-06 11:36:22 +0000431 do {
432 seq = read_seqbegin(&open_state->seqlock);
433 memcpy(dst->data, open_state->stateid.data,
434 sizeof(open_state->stateid.data));
435 } while (read_seqretry(&open_state->seqlock, seq));
436 } else
437 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
438 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400439 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000440 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400441}
442
443/*
444* Get layout from server.
445* for now, assume that whole file layouts are requested.
446* arg->offset: 0
447* arg->length: all ones
448*/
Benny Halevye5e94012010-10-20 00:18:01 -0400449static struct pnfs_layout_segment *
450send_layoutget(struct pnfs_layout_hdr *lo,
451 struct nfs_open_context *ctx,
452 u32 iomode)
453{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000454 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400455 struct nfs_server *server = NFS_SERVER(ino);
456 struct nfs4_layoutget *lgp;
457 struct pnfs_layout_segment *lseg = NULL;
Benny Halevye5e94012010-10-20 00:18:01 -0400458
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400459 dprintk("--> %s\n", __func__);
460
461 BUG_ON(ctx == NULL);
462 lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000463 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400464 return NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400465 lgp->args.minlength = NFS4_MAX_UINT64;
466 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
467 lgp->args.range.iomode = iomode;
468 lgp->args.range.offset = 0;
469 lgp->args.range.length = NFS4_MAX_UINT64;
470 lgp->args.type = server->pnfs_curr_ld->id;
471 lgp->args.inode = ino;
472 lgp->args.ctx = get_nfs_open_context(ctx);
473 lgp->lsegpp = &lseg;
474
475 /* Synchronously retrieve layout information from server and
476 * store in lseg.
477 */
478 nfs4_proc_layoutget(lgp);
479 if (!lseg) {
480 /* remember that LAYOUTGET failed and suspend trying */
Fred Isaman566052c2011-01-06 11:36:20 +0000481 set_bit(lo_fail_bit(iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400482 }
Andy Adamson974cec82010-10-20 00:18:02 -0400483 return lseg;
484}
485
Fred Isamanf7e89172011-01-06 11:36:32 +0000486bool pnfs_roc(struct inode *ino)
487{
488 struct pnfs_layout_hdr *lo;
489 struct pnfs_layout_segment *lseg, *tmp;
490 LIST_HEAD(tmp_list);
491 bool found = false;
492
493 spin_lock(&ino->i_lock);
494 lo = NFS_I(ino)->layout;
495 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
496 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
497 goto out_nolayout;
498 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
499 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
500 mark_lseg_invalid(lseg, &tmp_list);
501 found = true;
502 }
503 if (!found)
504 goto out_nolayout;
505 lo->plh_block_lgets++;
506 get_layout_hdr(lo); /* matched in pnfs_roc_release */
507 spin_unlock(&ino->i_lock);
508 pnfs_free_lseg_list(&tmp_list);
509 return true;
510
511out_nolayout:
512 spin_unlock(&ino->i_lock);
513 return false;
514}
515
516void pnfs_roc_release(struct inode *ino)
517{
518 struct pnfs_layout_hdr *lo;
519
520 spin_lock(&ino->i_lock);
521 lo = NFS_I(ino)->layout;
522 lo->plh_block_lgets--;
523 put_layout_hdr_locked(lo);
524 spin_unlock(&ino->i_lock);
525}
526
527void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
528{
529 struct pnfs_layout_hdr *lo;
530
531 spin_lock(&ino->i_lock);
532 lo = NFS_I(ino)->layout;
533 if ((int)(barrier - lo->plh_barrier) > 0)
534 lo->plh_barrier = barrier;
535 spin_unlock(&ino->i_lock);
536}
537
538bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
539{
540 struct nfs_inode *nfsi = NFS_I(ino);
541 struct pnfs_layout_segment *lseg;
542 bool found = false;
543
544 spin_lock(&ino->i_lock);
545 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
546 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
547 found = true;
548 break;
549 }
550 if (!found) {
551 struct pnfs_layout_hdr *lo = nfsi->layout;
552 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
553
554 /* Since close does not return a layout stateid for use as
555 * a barrier, we choose the worst-case barrier.
556 */
557 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
558 }
559 spin_unlock(&ino->i_lock);
560 return found;
561}
562
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400563/*
564 * Compare two layout segments for sorting into layout cache.
565 * We want to preferentially return RW over RO layouts, so ensure those
566 * are seen first.
567 */
568static s64
569cmp_layout(u32 iomode1, u32 iomode2)
570{
571 /* read > read/write */
572 return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
573}
574
Andy Adamson974cec82010-10-20 00:18:02 -0400575static void
576pnfs_insert_layout(struct pnfs_layout_hdr *lo,
577 struct pnfs_layout_segment *lseg)
578{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400579 struct pnfs_layout_segment *lp;
580 int found = 0;
581
Andy Adamson974cec82010-10-20 00:18:02 -0400582 dprintk("%s:Begin\n", __func__);
583
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000584 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000585 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000586 if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400587 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000588 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400589 dprintk("%s: inserted lseg %p "
590 "iomode %d offset %llu length %llu before "
591 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000592 __func__, lseg, lseg->pls_range.iomode,
593 lseg->pls_range.offset, lseg->pls_range.length,
594 lp, lp->pls_range.iomode, lp->pls_range.offset,
595 lp->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400596 found = 1;
597 break;
Andy Adamson974cec82010-10-20 00:18:02 -0400598 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400599 if (!found) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000600 list_add_tail(&lseg->pls_list, &lo->plh_segs);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400601 dprintk("%s: inserted lseg %p "
602 "iomode %d offset %llu length %llu at tail\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000603 __func__, lseg, lseg->pls_range.iomode,
604 lseg->pls_range.offset, lseg->pls_range.length);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400605 }
Fred Isamancc6e5342011-01-06 11:36:28 +0000606 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400607
608 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400609}
610
611static struct pnfs_layout_hdr *
612alloc_init_layout_hdr(struct inode *ino)
613{
614 struct pnfs_layout_hdr *lo;
615
616 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
617 if (!lo)
618 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000619 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000620 INIT_LIST_HEAD(&lo->plh_layouts);
621 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000622 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000623 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400624 return lo;
625}
626
627static struct pnfs_layout_hdr *
628pnfs_find_alloc_layout(struct inode *ino)
629{
630 struct nfs_inode *nfsi = NFS_I(ino);
631 struct pnfs_layout_hdr *new = NULL;
632
633 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
634
635 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000636 if (nfsi->layout) {
637 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
638 return NULL;
639 else
640 return nfsi->layout;
641 }
Benny Halevye5e94012010-10-20 00:18:01 -0400642 spin_unlock(&ino->i_lock);
643 new = alloc_init_layout_hdr(ino);
644 spin_lock(&ino->i_lock);
645
646 if (likely(nfsi->layout == NULL)) /* Won the race? */
647 nfsi->layout = new;
648 else
649 kfree(new);
650 return nfsi->layout;
651}
652
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400653/*
654 * iomode matching rules:
655 * iomode lseg match
656 * ----- ----- -----
657 * ANY READ true
658 * ANY RW true
659 * RW READ false
660 * RW RW true
661 * READ READ true
662 * READ RW true
663 */
664static int
665is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
666{
Fred Isaman566052c2011-01-06 11:36:20 +0000667 return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400668}
669
670/*
671 * lookup range in layout
672 */
Benny Halevye5e94012010-10-20 00:18:01 -0400673static struct pnfs_layout_segment *
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000674pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
Benny Halevye5e94012010-10-20 00:18:01 -0400675{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400676 struct pnfs_layout_segment *lseg, *ret = NULL;
677
678 dprintk("%s:Begin\n", __func__);
679
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000680 assert_spin_locked(&lo->plh_inode->i_lock);
681 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000682 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
683 is_matching_lseg(lseg, iomode)) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400684 ret = lseg;
685 break;
686 }
Fred Isaman566052c2011-01-06 11:36:20 +0000687 if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400688 break;
689 }
690
691 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000692 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400693 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400694}
695
696/*
697 * Layout segment is retreived from the server if not cached.
698 * The appropriate layout segment is referenced and returned to the caller.
699 */
700struct pnfs_layout_segment *
701pnfs_update_layout(struct inode *ino,
702 struct nfs_open_context *ctx,
703 enum pnfs_iomode iomode)
704{
705 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000706 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400707 struct pnfs_layout_hdr *lo;
708 struct pnfs_layout_segment *lseg = NULL;
709
710 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
711 return NULL;
712 spin_lock(&ino->i_lock);
713 lo = pnfs_find_alloc_layout(ino);
714 if (lo == NULL) {
715 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
716 goto out_unlock;
717 }
718
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000719 /* Do we even need to bother with this? */
720 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
721 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
722 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400723 goto out_unlock;
724 }
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000725 /* Check to see if the layout for the given range already exists */
726 lseg = pnfs_find_lseg(lo, iomode);
727 if (lseg)
728 goto out_unlock;
Benny Halevye5e94012010-10-20 00:18:01 -0400729
730 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000731 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400732 goto out_unlock;
733
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000734 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000735 goto out_unlock;
736 atomic_inc(&lo->plh_outstanding);
737
Fred Isamancc6e5342011-01-06 11:36:28 +0000738 get_layout_hdr(lo);
Fred Isaman2130ff62011-01-06 11:36:26 +0000739 if (list_empty(&lo->plh_segs)) {
740 /* The lo must be on the clp list if there is any
741 * chance of a CB_LAYOUTRECALL(FILE) coming in.
742 */
743 spin_lock(&clp->cl_lock);
744 BUG_ON(!list_empty(&lo->plh_layouts));
745 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
746 spin_unlock(&clp->cl_lock);
747 }
Benny Halevye5e94012010-10-20 00:18:01 -0400748 spin_unlock(&ino->i_lock);
749
750 lseg = send_layoutget(lo, ctx, iomode);
Fred Isaman2130ff62011-01-06 11:36:26 +0000751 if (!lseg) {
752 spin_lock(&ino->i_lock);
753 if (list_empty(&lo->plh_segs)) {
754 spin_lock(&clp->cl_lock);
755 list_del_init(&lo->plh_layouts);
756 spin_unlock(&clp->cl_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000757 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
Fred Isaman2130ff62011-01-06 11:36:26 +0000758 }
759 spin_unlock(&ino->i_lock);
760 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000761 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000762 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400763out:
764 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Fred Isaman566052c2011-01-06 11:36:20 +0000765 nfsi->layout->plh_flags, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400766 return lseg;
767out_unlock:
768 spin_unlock(&ino->i_lock);
769 goto out;
770}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400771
772int
773pnfs_layout_process(struct nfs4_layoutget *lgp)
774{
775 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
776 struct nfs4_layoutget_res *res = &lgp->res;
777 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000778 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000779 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400780 int status = 0;
781
Fred Isamanfc1794c2011-01-06 11:36:27 +0000782 /* Verify we got what we asked for.
783 * Note that because the xdr parsing only accepts a single
784 * element array, this can fail even if the server is behaving
785 * correctly.
786 */
787 if (lgp->args.range.iomode > res->range.iomode ||
788 res->range.offset != 0 ||
789 res->range.length != NFS4_MAX_UINT64) {
790 status = -EINVAL;
791 goto out;
792 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400793 /* Inject layout blob into I/O device driver */
794 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
795 if (!lseg || IS_ERR(lseg)) {
796 if (!lseg)
797 status = -ENOMEM;
798 else
799 status = PTR_ERR(lseg);
800 dprintk("%s: Could not allocate layout: error %d\n",
801 __func__, status);
802 goto out;
803 }
804
805 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000806 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
807 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
808 dprintk("%s forget reply due to recall\n", __func__);
809 goto out_forget_reply;
810 }
811
812 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
813 dprintk("%s forget reply due to state\n", __func__);
814 goto out_forget_reply;
815 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400816 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000817 lseg->pls_range = res->range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400818 *lgp->lsegpp = lseg;
819 pnfs_insert_layout(lo, lseg);
820
Fred Isamanf7e89172011-01-06 11:36:32 +0000821 if (res->return_on_close) {
822 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
823 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
824 }
825
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400826 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000827 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400828 spin_unlock(&ino->i_lock);
829out:
830 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000831
832out_forget_reply:
833 spin_unlock(&ino->i_lock);
834 lseg->pls_layout = lo;
835 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
836 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400837}
838
839/*
840 * Device ID cache. Currently supports one layout type per struct nfs_client.
841 * Add layout type to the lookup key to expand to support multiple types.
842 */
843int
844pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
845 void (*free_callback)(struct pnfs_deviceid_node *))
846{
847 struct pnfs_deviceid_cache *c;
848
849 c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
850 if (!c)
851 return -ENOMEM;
852 spin_lock(&clp->cl_lock);
853 if (clp->cl_devid_cache != NULL) {
854 atomic_inc(&clp->cl_devid_cache->dc_ref);
855 dprintk("%s [kref [%d]]\n", __func__,
856 atomic_read(&clp->cl_devid_cache->dc_ref));
857 kfree(c);
858 } else {
859 /* kzalloc initializes hlists */
860 spin_lock_init(&c->dc_lock);
861 atomic_set(&c->dc_ref, 1);
862 c->dc_free_callback = free_callback;
863 clp->cl_devid_cache = c;
864 dprintk("%s [new]\n", __func__);
865 }
866 spin_unlock(&clp->cl_lock);
867 return 0;
868}
869EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
870
871/*
872 * Called from pnfs_layoutdriver_type->free_lseg
873 * last layout segment reference frees deviceid
874 */
875void
876pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
877 struct pnfs_deviceid_node *devid)
878{
879 struct nfs4_deviceid *id = &devid->de_id;
880 struct pnfs_deviceid_node *d;
881 struct hlist_node *n;
882 long h = nfs4_deviceid_hash(id);
883
884 dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
885 if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
886 return;
887
888 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
889 if (!memcmp(&d->de_id, id, sizeof(*id))) {
890 hlist_del_rcu(&d->de_node);
891 spin_unlock(&c->dc_lock);
892 synchronize_rcu();
893 c->dc_free_callback(devid);
894 return;
895 }
896 spin_unlock(&c->dc_lock);
897 /* Why wasn't it found in the list? */
898 BUG();
899}
900EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
901
902/* Find and reference a deviceid */
903struct pnfs_deviceid_node *
904pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
905{
906 struct pnfs_deviceid_node *d;
907 struct hlist_node *n;
908 long hash = nfs4_deviceid_hash(id);
909
910 dprintk("--> %s hash %ld\n", __func__, hash);
911 rcu_read_lock();
912 hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
913 if (!memcmp(&d->de_id, id, sizeof(*id))) {
914 if (!atomic_inc_not_zero(&d->de_ref)) {
915 goto fail;
916 } else {
917 rcu_read_unlock();
918 return d;
919 }
920 }
921 }
922fail:
923 rcu_read_unlock();
924 return NULL;
925}
926EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
927
928/*
929 * Add a deviceid to the cache.
930 * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
931 */
932struct pnfs_deviceid_node *
933pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
934{
935 struct pnfs_deviceid_node *d;
936 long hash = nfs4_deviceid_hash(&new->de_id);
937
938 dprintk("--> %s hash %ld\n", __func__, hash);
939 spin_lock(&c->dc_lock);
940 d = pnfs_find_get_deviceid(c, &new->de_id);
941 if (d) {
942 spin_unlock(&c->dc_lock);
943 dprintk("%s [discard]\n", __func__);
944 c->dc_free_callback(new);
945 return d;
946 }
947 INIT_HLIST_NODE(&new->de_node);
948 atomic_set(&new->de_ref, 1);
949 hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
950 spin_unlock(&c->dc_lock);
951 dprintk("%s [new]\n", __func__);
952 return new;
953}
954EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
955
956void
957pnfs_put_deviceid_cache(struct nfs_client *clp)
958{
959 struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
960
Andy Adamsonb2a28972011-01-25 15:38:03 +0000961 dprintk("--> %s ({%d})\n", __func__, atomic_read(&local->dc_ref));
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400962 if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
963 int i;
964 /* Verify cache is empty */
965 for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
966 BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
967 clp->cl_devid_cache = NULL;
968 spin_unlock(&clp->cl_lock);
969 kfree(local);
970 }
971}
972EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);