blob: 891a0c36f9924909e828389b157a78296359994b [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) {
78 nfss->pnfs_curr_ld->uninitialize_mountpoint(nfss);
79 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;
Fred Isaman02c35fc2010-10-20 00:17:59 -0400118 if (ld_type->initialize_mountpoint(server)) {
119 printk(KERN_ERR
120 "%s: Error initializing mount point for layout driver %u.\n",
121 __func__, id);
122 module_put(ld_type->owner);
123 goto out_no_driver;
124 }
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 }
143
144 spin_lock(&pnfs_spinlock);
145 tmp = find_pnfs_driver_locked(ld_type->id);
146 if (!tmp) {
147 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
148 status = 0;
149 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
150 ld_type->name);
151 } else {
152 printk(KERN_ERR "%s Module with id %d already loaded!\n",
153 __func__, ld_type->id);
154 }
155 spin_unlock(&pnfs_spinlock);
156
157 return status;
158}
159EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
160
161void
162pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
163{
164 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
165 spin_lock(&pnfs_spinlock);
166 list_del(&ld_type->pnfs_tblid);
167 spin_unlock(&pnfs_spinlock);
168}
169EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400170
171static void
172get_layout_hdr_locked(struct pnfs_layout_hdr *lo)
173{
174 assert_spin_locked(&lo->inode->i_lock);
175 lo->refcount++;
176}
177
178static void
179put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
180{
181 assert_spin_locked(&lo->inode->i_lock);
182 BUG_ON(lo->refcount == 0);
183
184 lo->refcount--;
185 if (!lo->refcount) {
186 dprintk("%s: freeing layout cache %p\n", __func__, lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400187 BUG_ON(!list_empty(&lo->layouts));
Benny Halevye5e94012010-10-20 00:18:01 -0400188 NFS_I(lo->inode)->layout = NULL;
189 kfree(lo);
190 }
191}
192
Andy Adamson974cec82010-10-20 00:18:02 -0400193static void
194put_layout_hdr(struct inode *inode)
195{
196 spin_lock(&inode->i_lock);
197 put_layout_hdr_locked(NFS_I(inode)->layout);
198 spin_unlock(&inode->i_lock);
199}
200
201static void
202init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
203{
204 INIT_LIST_HEAD(&lseg->fi_list);
205 kref_init(&lseg->kref);
206 lseg->layout = lo;
207}
208
209/* Called without i_lock held, as the free_lseg call may sleep */
210static void
211destroy_lseg(struct kref *kref)
212{
213 struct pnfs_layout_segment *lseg =
214 container_of(kref, struct pnfs_layout_segment, kref);
215 struct inode *ino = lseg->layout->inode;
216
217 dprintk("--> %s\n", __func__);
218 kfree(lseg);
219 /* Matched by get_layout_hdr_locked in pnfs_insert_layout */
220 put_layout_hdr(ino);
221}
222
223static void
224put_lseg(struct pnfs_layout_segment *lseg)
225{
226 if (!lseg)
227 return;
228
229 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
230 atomic_read(&lseg->kref.refcount));
231 kref_put(&lseg->kref, destroy_lseg);
232}
233
234static void
235pnfs_clear_lseg_list(struct pnfs_layout_hdr *lo, struct list_head *tmp_list)
236{
237 struct pnfs_layout_segment *lseg, *next;
238 struct nfs_client *clp;
239
240 dprintk("%s:Begin lo %p\n", __func__, lo);
241
242 assert_spin_locked(&lo->inode->i_lock);
243 list_for_each_entry_safe(lseg, next, &lo->segs, fi_list) {
244 dprintk("%s: freeing lseg %p\n", __func__, lseg);
245 list_move(&lseg->fi_list, tmp_list);
246 }
247 clp = NFS_SERVER(lo->inode)->nfs_client;
248 spin_lock(&clp->cl_lock);
249 /* List does not take a reference, so no need for put here */
250 list_del_init(&lo->layouts);
251 spin_unlock(&clp->cl_lock);
252
253 dprintk("%s:Return\n", __func__);
254}
255
256static void
257pnfs_free_lseg_list(struct list_head *tmp_list)
258{
259 struct pnfs_layout_segment *lseg;
260
261 while (!list_empty(tmp_list)) {
262 lseg = list_entry(tmp_list->next, struct pnfs_layout_segment,
263 fi_list);
264 dprintk("%s calling put_lseg on %p\n", __func__, lseg);
265 list_del(&lseg->fi_list);
266 put_lseg(lseg);
267 }
268}
269
Benny Halevye5e94012010-10-20 00:18:01 -0400270void
271pnfs_destroy_layout(struct nfs_inode *nfsi)
272{
273 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400274 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400275
276 spin_lock(&nfsi->vfs_inode.i_lock);
277 lo = nfsi->layout;
278 if (lo) {
Andy Adamson974cec82010-10-20 00:18:02 -0400279 pnfs_clear_lseg_list(lo, &tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400280 /* Matched by refcount set to 1 in alloc_init_layout_hdr */
281 put_layout_hdr_locked(lo);
282 }
283 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400284 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400285}
286
Andy Adamson974cec82010-10-20 00:18:02 -0400287/*
288 * Called by the state manger to remove all layouts established under an
289 * expired lease.
290 */
291void
292pnfs_destroy_all_layouts(struct nfs_client *clp)
293{
294 struct pnfs_layout_hdr *lo;
295 LIST_HEAD(tmp_list);
296
297 spin_lock(&clp->cl_lock);
298 list_splice_init(&clp->cl_layouts, &tmp_list);
299 spin_unlock(&clp->cl_lock);
300
301 while (!list_empty(&tmp_list)) {
302 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
303 layouts);
304 dprintk("%s freeing layout for inode %lu\n", __func__,
305 lo->inode->i_ino);
306 pnfs_destroy_layout(NFS_I(lo->inode));
307 }
308}
309
310static void pnfs_insert_layout(struct pnfs_layout_hdr *lo,
311 struct pnfs_layout_segment *lseg);
312
313/* Get layout from server. */
Benny Halevye5e94012010-10-20 00:18:01 -0400314static struct pnfs_layout_segment *
315send_layoutget(struct pnfs_layout_hdr *lo,
316 struct nfs_open_context *ctx,
317 u32 iomode)
318{
319 struct inode *ino = lo->inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400320 struct pnfs_layout_segment *lseg;
Benny Halevye5e94012010-10-20 00:18:01 -0400321
Andy Adamson974cec82010-10-20 00:18:02 -0400322 /* Lets pretend we sent LAYOUTGET and got a response */
323 lseg = kzalloc(sizeof(*lseg), GFP_KERNEL);
324 if (!lseg) {
325 set_bit(lo_fail_bit(iomode), &lo->state);
326 spin_lock(&ino->i_lock);
327 put_layout_hdr_locked(lo);
328 spin_unlock(&ino->i_lock);
329 return NULL;
330 }
331 init_lseg(lo, lseg);
332 lseg->iomode = IOMODE_RW;
Benny Halevye5e94012010-10-20 00:18:01 -0400333 spin_lock(&ino->i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400334 pnfs_insert_layout(lo, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400335 put_layout_hdr_locked(lo);
336 spin_unlock(&ino->i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400337 return lseg;
338}
339
340static void
341pnfs_insert_layout(struct pnfs_layout_hdr *lo,
342 struct pnfs_layout_segment *lseg)
343{
344 dprintk("%s:Begin\n", __func__);
345
346 assert_spin_locked(&lo->inode->i_lock);
347 if (list_empty(&lo->segs)) {
348 struct nfs_client *clp = NFS_SERVER(lo->inode)->nfs_client;
349
350 spin_lock(&clp->cl_lock);
351 BUG_ON(!list_empty(&lo->layouts));
352 list_add_tail(&lo->layouts, &clp->cl_layouts);
353 spin_unlock(&clp->cl_lock);
354 }
355 get_layout_hdr_locked(lo);
356 /* STUB - add the constructed lseg if necessary */
357 if (list_empty(&lo->segs)) {
358 list_add_tail(&lseg->fi_list, &lo->segs);
359 dprintk("%s: inserted lseg %p iomode %d at tail\n",
360 __func__, lseg, lseg->iomode);
361 } else {
362 /* There is no harm for the moment in calling this
363 * with the lock held, and the call will be removed
364 * with the STUB.
365 */
366 put_lseg(lseg);
367 }
368
369 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400370}
371
372static struct pnfs_layout_hdr *
373alloc_init_layout_hdr(struct inode *ino)
374{
375 struct pnfs_layout_hdr *lo;
376
377 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
378 if (!lo)
379 return NULL;
380 lo->refcount = 1;
Andy Adamson974cec82010-10-20 00:18:02 -0400381 INIT_LIST_HEAD(&lo->layouts);
382 INIT_LIST_HEAD(&lo->segs);
Benny Halevye5e94012010-10-20 00:18:01 -0400383 lo->inode = ino;
384 return lo;
385}
386
387static struct pnfs_layout_hdr *
388pnfs_find_alloc_layout(struct inode *ino)
389{
390 struct nfs_inode *nfsi = NFS_I(ino);
391 struct pnfs_layout_hdr *new = NULL;
392
393 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
394
395 assert_spin_locked(&ino->i_lock);
396 if (nfsi->layout)
397 return nfsi->layout;
398
399 spin_unlock(&ino->i_lock);
400 new = alloc_init_layout_hdr(ino);
401 spin_lock(&ino->i_lock);
402
403 if (likely(nfsi->layout == NULL)) /* Won the race? */
404 nfsi->layout = new;
405 else
406 kfree(new);
407 return nfsi->layout;
408}
409
410/* STUB - LAYOUTGET never succeeds, so cache is empty */
411static struct pnfs_layout_segment *
412pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
413{
414 return NULL;
415}
416
417/*
418 * Layout segment is retreived from the server if not cached.
419 * The appropriate layout segment is referenced and returned to the caller.
420 */
421struct pnfs_layout_segment *
422pnfs_update_layout(struct inode *ino,
423 struct nfs_open_context *ctx,
424 enum pnfs_iomode iomode)
425{
426 struct nfs_inode *nfsi = NFS_I(ino);
427 struct pnfs_layout_hdr *lo;
428 struct pnfs_layout_segment *lseg = NULL;
429
430 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
431 return NULL;
432 spin_lock(&ino->i_lock);
433 lo = pnfs_find_alloc_layout(ino);
434 if (lo == NULL) {
435 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
436 goto out_unlock;
437 }
438
439 /* Check to see if the layout for the given range already exists */
440 lseg = pnfs_has_layout(lo, iomode);
441 if (lseg) {
442 dprintk("%s: Using cached lseg %p for iomode %d)\n",
443 __func__, lseg, iomode);
444 goto out_unlock;
445 }
446
447 /* if LAYOUTGET already failed once we don't try again */
448 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->state))
449 goto out_unlock;
450
451 get_layout_hdr_locked(lo);
452 spin_unlock(&ino->i_lock);
453
454 lseg = send_layoutget(lo, ctx, iomode);
455out:
456 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
457 nfsi->layout->state, lseg);
458 return lseg;
459out_unlock:
460 spin_unlock(&ino->i_lock);
461 goto out;
462}