ocfs2: NFS hangs in __ocfs2_cluster_lock due to race with ocfs2_unblock_lock
[pandora-kernel.git] / fs / fscache / netfs.c
1 /* FS-Cache netfs (client) registration
2  *
3  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11
12 #define FSCACHE_DEBUG_LEVEL COOKIE
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include "internal.h"
16
17 static LIST_HEAD(fscache_netfs_list);
18
19 /*
20  * register a network filesystem for caching
21  */
22 int __fscache_register_netfs(struct fscache_netfs *netfs)
23 {
24         struct fscache_netfs *ptr;
25         struct fscache_cookie *cookie;
26         int ret;
27
28         _enter("{%s}", netfs->name);
29
30         INIT_LIST_HEAD(&netfs->link);
31
32         /* allocate a cookie for the primary index */
33         cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
34
35         if (!cookie) {
36                 _leave(" = -ENOMEM");
37                 return -ENOMEM;
38         }
39
40         /* initialise the primary index cookie */
41         atomic_set(&cookie->usage, 1);
42         atomic_set(&cookie->n_children, 0);
43
44         cookie->def             = &fscache_fsdef_netfs_def;
45         cookie->parent          = &fscache_fsdef_index;
46         cookie->netfs_data      = netfs;
47
48         spin_lock_init(&cookie->lock);
49         INIT_HLIST_HEAD(&cookie->backing_objects);
50
51         /* check the netfs type is not already present */
52         down_write(&fscache_addremove_sem);
53
54         ret = -EEXIST;
55         list_for_each_entry(ptr, &fscache_netfs_list, link) {
56                 if (strcmp(ptr->name, netfs->name) == 0)
57                         goto already_registered;
58         }
59
60         atomic_inc(&cookie->parent->usage);
61         atomic_inc(&cookie->parent->n_children);
62
63         netfs->primary_index = cookie;
64         list_add(&netfs->link, &fscache_netfs_list);
65         ret = 0;
66
67         printk(KERN_NOTICE "FS-Cache: Netfs '%s' registered for caching\n",
68                netfs->name);
69
70 already_registered:
71         up_write(&fscache_addremove_sem);
72
73         if (ret < 0)
74                 kmem_cache_free(fscache_cookie_jar, cookie);
75
76         _leave(" = %d", ret);
77         return ret;
78 }
79 EXPORT_SYMBOL(__fscache_register_netfs);
80
81 /*
82  * unregister a network filesystem from the cache
83  * - all cookies must have been released first
84  */
85 void __fscache_unregister_netfs(struct fscache_netfs *netfs)
86 {
87         _enter("{%s.%u}", netfs->name, netfs->version);
88
89         down_write(&fscache_addremove_sem);
90
91         list_del(&netfs->link);
92         fscache_relinquish_cookie(netfs->primary_index, 0);
93
94         up_write(&fscache_addremove_sem);
95
96         printk(KERN_NOTICE "FS-Cache: Netfs '%s' unregistered from caching\n",
97                netfs->name);
98
99         _leave("");
100 }
101 EXPORT_SYMBOL(__fscache_unregister_netfs);