fscache: Attach the index key and aux data to the cookie
[linux-2.6-block.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         cookie->key_len = strlen(netfs->name);
41         if (cookie->key_len <= sizeof(cookie->inline_key)) {
42                 memcpy(cookie->inline_key, netfs->name, strlen(netfs->name));
43         } else {
44                 ret = -ENOMEM;
45                 cookie->key = kmemdup(netfs->name, cookie->key_len, GFP_KERNEL);
46                 if (!cookie->key)
47                         goto nomem;
48         }
49
50         cookie->aux_len = sizeof(netfs->version);
51         memcpy(cookie->inline_aux, &netfs->version, cookie->aux_len);
52
53         /* initialise the primary index cookie */
54         atomic_set(&cookie->usage, 1);
55         atomic_set(&cookie->n_children, 0);
56         atomic_set(&cookie->n_active, 1);
57
58         cookie->def             = &fscache_fsdef_netfs_def;
59         cookie->parent          = &fscache_fsdef_index;
60         cookie->netfs_data      = netfs;
61         cookie->flags           = 1 << FSCACHE_COOKIE_ENABLED;
62         cookie->type            = FSCACHE_COOKIE_TYPE_INDEX;
63
64         spin_lock_init(&cookie->lock);
65         spin_lock_init(&cookie->stores_lock);
66         INIT_HLIST_HEAD(&cookie->backing_objects);
67
68         /* check the netfs type is not already present */
69         down_write(&fscache_addremove_sem);
70
71         ret = -EEXIST;
72         list_for_each_entry(ptr, &fscache_netfs_list, link) {
73                 if (strcmp(ptr->name, netfs->name) == 0)
74                         goto already_registered;
75         }
76
77         fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs);
78         atomic_inc(&cookie->parent->n_children);
79
80         netfs->primary_index = cookie;
81         list_add(&netfs->link, &fscache_netfs_list);
82         ret = 0;
83
84         pr_notice("Netfs '%s' registered for caching\n", netfs->name);
85         trace_fscache_netfs(netfs);
86
87 already_registered:
88         up_write(&fscache_addremove_sem);
89
90 nomem:
91         if (ret < 0)
92                 kmem_cache_free(fscache_cookie_jar, cookie);
93
94         _leave(" = %d", ret);
95         return ret;
96 }
97 EXPORT_SYMBOL(__fscache_register_netfs);
98
99 /*
100  * unregister a network filesystem from the cache
101  * - all cookies must have been released first
102  */
103 void __fscache_unregister_netfs(struct fscache_netfs *netfs)
104 {
105         _enter("{%s.%u}", netfs->name, netfs->version);
106
107         down_write(&fscache_addremove_sem);
108
109         list_del(&netfs->link);
110         fscache_relinquish_cookie(netfs->primary_index, NULL, false);
111
112         up_write(&fscache_addremove_sem);
113
114         pr_notice("Netfs '%s' unregistered from caching\n",
115                   netfs->name);
116
117         _leave("");
118 }
119 EXPORT_SYMBOL(__fscache_unregister_netfs);