2 * pNFS functions to call and manage layout drivers.
4 * Copyright (c) 2002 [year of first publication]
5 * The Regents of the University of Michigan
8 * Dean Hildebrand <dhildebz@umich.edu>
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.
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
30 #include <linux/nfs_fs.h>
34 #define NFSDBG_FACILITY NFSDBG_PNFS
39 * protects pnfs_modules_tbl.
41 static DEFINE_SPINLOCK(pnfs_spinlock);
44 * pnfs_modules_tbl holds all pnfs modules
46 static LIST_HEAD(pnfs_modules_tbl);
48 /* Return the registered pnfs layout driver module matching given id */
49 static struct pnfs_layoutdriver_type *
50 find_pnfs_driver_locked(u32 id)
52 struct pnfs_layoutdriver_type *local;
54 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
59 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
63 static struct pnfs_layoutdriver_type *
64 find_pnfs_driver(u32 id)
66 struct pnfs_layoutdriver_type *local;
68 spin_lock(&pnfs_spinlock);
69 local = find_pnfs_driver_locked(id);
70 spin_unlock(&pnfs_spinlock);
75 unset_pnfs_layoutdriver(struct nfs_server *nfss)
77 if (nfss->pnfs_curr_ld) {
78 nfss->pnfs_curr_ld->uninitialize_mountpoint(nfss);
79 module_put(nfss->pnfs_curr_ld->owner);
81 nfss->pnfs_curr_ld = NULL;
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.
88 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
91 set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
93 struct pnfs_layoutdriver_type *ld_type = NULL;
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);
103 ld_type = find_pnfs_driver(id);
105 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
106 ld_type = find_pnfs_driver(id);
108 dprintk("%s: No pNFS module found for %u.\n",
113 if (!try_module_get(ld_type->owner)) {
114 dprintk("%s: Could not grab reference on module\n", __func__);
117 server->pnfs_curr_ld = ld_type;
118 if (ld_type->initialize_mountpoint(server)) {
120 "%s: Error initializing mount point for layout driver %u.\n",
122 module_put(ld_type->owner);
125 dprintk("%s: pNFS module for %u set\n", __func__, id);
129 dprintk("%s: Using NFSv4 I/O\n", __func__);
130 server->pnfs_curr_ld = NULL;
134 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
136 int status = -EINVAL;
137 struct pnfs_layoutdriver_type *tmp;
139 if (ld_type->id == 0) {
140 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
144 spin_lock(&pnfs_spinlock);
145 tmp = find_pnfs_driver_locked(ld_type->id);
147 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
149 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
152 printk(KERN_ERR "%s Module with id %d already loaded!\n",
153 __func__, ld_type->id);
155 spin_unlock(&pnfs_spinlock);
159 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
162 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
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);
169 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
172 get_layout_hdr_locked(struct pnfs_layout_hdr *lo)
174 assert_spin_locked(&lo->inode->i_lock);
179 put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
181 assert_spin_locked(&lo->inode->i_lock);
182 BUG_ON(lo->refcount == 0);
186 dprintk("%s: freeing layout cache %p\n", __func__, lo);
187 BUG_ON(!list_empty(&lo->layouts));
188 NFS_I(lo->inode)->layout = NULL;
194 put_layout_hdr(struct inode *inode)
196 spin_lock(&inode->i_lock);
197 put_layout_hdr_locked(NFS_I(inode)->layout);
198 spin_unlock(&inode->i_lock);
202 init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
204 INIT_LIST_HEAD(&lseg->fi_list);
205 kref_init(&lseg->kref);
209 /* Called without i_lock held, as the free_lseg call may sleep */
211 destroy_lseg(struct kref *kref)
213 struct pnfs_layout_segment *lseg =
214 container_of(kref, struct pnfs_layout_segment, kref);
215 struct inode *ino = lseg->layout->inode;
217 dprintk("--> %s\n", __func__);
219 /* Matched by get_layout_hdr_locked in pnfs_insert_layout */
224 put_lseg(struct pnfs_layout_segment *lseg)
229 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
230 atomic_read(&lseg->kref.refcount));
231 kref_put(&lseg->kref, destroy_lseg);
235 pnfs_clear_lseg_list(struct pnfs_layout_hdr *lo, struct list_head *tmp_list)
237 struct pnfs_layout_segment *lseg, *next;
238 struct nfs_client *clp;
240 dprintk("%s:Begin lo %p\n", __func__, lo);
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);
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);
253 dprintk("%s:Return\n", __func__);
257 pnfs_free_lseg_list(struct list_head *tmp_list)
259 struct pnfs_layout_segment *lseg;
261 while (!list_empty(tmp_list)) {
262 lseg = list_entry(tmp_list->next, struct pnfs_layout_segment,
264 dprintk("%s calling put_lseg on %p\n", __func__, lseg);
265 list_del(&lseg->fi_list);
271 pnfs_destroy_layout(struct nfs_inode *nfsi)
273 struct pnfs_layout_hdr *lo;
276 spin_lock(&nfsi->vfs_inode.i_lock);
279 pnfs_clear_lseg_list(lo, &tmp_list);
280 /* Matched by refcount set to 1 in alloc_init_layout_hdr */
281 put_layout_hdr_locked(lo);
283 spin_unlock(&nfsi->vfs_inode.i_lock);
284 pnfs_free_lseg_list(&tmp_list);
288 * Called by the state manger to remove all layouts established under an
292 pnfs_destroy_all_layouts(struct nfs_client *clp)
294 struct pnfs_layout_hdr *lo;
297 spin_lock(&clp->cl_lock);
298 list_splice_init(&clp->cl_layouts, &tmp_list);
299 spin_unlock(&clp->cl_lock);
301 while (!list_empty(&tmp_list)) {
302 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
304 dprintk("%s freeing layout for inode %lu\n", __func__,
306 pnfs_destroy_layout(NFS_I(lo->inode));
310 static void pnfs_insert_layout(struct pnfs_layout_hdr *lo,
311 struct pnfs_layout_segment *lseg);
313 /* Get layout from server. */
314 static struct pnfs_layout_segment *
315 send_layoutget(struct pnfs_layout_hdr *lo,
316 struct nfs_open_context *ctx,
319 struct inode *ino = lo->inode;
320 struct pnfs_layout_segment *lseg;
322 /* Lets pretend we sent LAYOUTGET and got a response */
323 lseg = kzalloc(sizeof(*lseg), GFP_KERNEL);
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);
332 lseg->iomode = IOMODE_RW;
333 spin_lock(&ino->i_lock);
334 pnfs_insert_layout(lo, lseg);
335 put_layout_hdr_locked(lo);
336 spin_unlock(&ino->i_lock);
341 pnfs_insert_layout(struct pnfs_layout_hdr *lo,
342 struct pnfs_layout_segment *lseg)
344 dprintk("%s:Begin\n", __func__);
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;
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);
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);
362 /* There is no harm for the moment in calling this
363 * with the lock held, and the call will be removed
369 dprintk("%s:Return\n", __func__);
372 static struct pnfs_layout_hdr *
373 alloc_init_layout_hdr(struct inode *ino)
375 struct pnfs_layout_hdr *lo;
377 lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
381 INIT_LIST_HEAD(&lo->layouts);
382 INIT_LIST_HEAD(&lo->segs);
387 static struct pnfs_layout_hdr *
388 pnfs_find_alloc_layout(struct inode *ino)
390 struct nfs_inode *nfsi = NFS_I(ino);
391 struct pnfs_layout_hdr *new = NULL;
393 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
395 assert_spin_locked(&ino->i_lock);
399 spin_unlock(&ino->i_lock);
400 new = alloc_init_layout_hdr(ino);
401 spin_lock(&ino->i_lock);
403 if (likely(nfsi->layout == NULL)) /* Won the race? */
410 /* STUB - LAYOUTGET never succeeds, so cache is empty */
411 static struct pnfs_layout_segment *
412 pnfs_has_layout(struct pnfs_layout_hdr *lo, u32 iomode)
418 * Layout segment is retreived from the server if not cached.
419 * The appropriate layout segment is referenced and returned to the caller.
421 struct pnfs_layout_segment *
422 pnfs_update_layout(struct inode *ino,
423 struct nfs_open_context *ctx,
424 enum pnfs_iomode iomode)
426 struct nfs_inode *nfsi = NFS_I(ino);
427 struct pnfs_layout_hdr *lo;
428 struct pnfs_layout_segment *lseg = NULL;
430 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
432 spin_lock(&ino->i_lock);
433 lo = pnfs_find_alloc_layout(ino);
435 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
439 /* Check to see if the layout for the given range already exists */
440 lseg = pnfs_has_layout(lo, iomode);
442 dprintk("%s: Using cached lseg %p for iomode %d)\n",
443 __func__, lseg, iomode);
447 /* if LAYOUTGET already failed once we don't try again */
448 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->state))
451 get_layout_hdr_locked(lo);
452 spin_unlock(&ino->i_lock);
454 lseg = send_layoutget(lo, ctx, iomode);
456 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
457 nfsi->layout->state, lseg);
460 spin_unlock(&ino->i_lock);