blob: 6d941f56faf436398f0d20c146ad9b7dc77d0b61 [file] [log] [blame]
David Howells726dd7f2009-04-03 16:42:38 +01001/* 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
17static LIST_HEAD(fscache_netfs_list);
18
19/*
20 * register a network filesystem for caching
21 */
22int __fscache_register_netfs(struct fscache_netfs *netfs)
23{
24 struct fscache_netfs *ptr;
25 int ret;
26
27 _enter("{%s}", netfs->name);
28
29 INIT_LIST_HEAD(&netfs->link);
30
31 /* allocate a cookie for the primary index */
32 netfs->primary_index =
33 kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
34
35 if (!netfs->primary_index) {
36 _leave(" = -ENOMEM");
37 return -ENOMEM;
38 }
39
40 /* initialise the primary index cookie */
41 atomic_set(&netfs->primary_index->usage, 1);
42 atomic_set(&netfs->primary_index->n_children, 0);
David Howells13627292013-05-10 19:50:26 +010043 atomic_set(&netfs->primary_index->n_active, 1);
David Howells726dd7f2009-04-03 16:42:38 +010044
45 netfs->primary_index->def = &fscache_fsdef_netfs_def;
46 netfs->primary_index->parent = &fscache_fsdef_index;
47 netfs->primary_index->netfs_data = netfs;
David Howells94d30ae2013-09-21 00:09:31 +010048 netfs->primary_index->flags = 1 << FSCACHE_COOKIE_ENABLED;
David Howells726dd7f2009-04-03 16:42:38 +010049
50 atomic_inc(&netfs->primary_index->parent->usage);
51 atomic_inc(&netfs->primary_index->parent->n_children);
52
53 spin_lock_init(&netfs->primary_index->lock);
54 INIT_HLIST_HEAD(&netfs->primary_index->backing_objects);
55
56 /* check the netfs type is not already present */
57 down_write(&fscache_addremove_sem);
58
59 ret = -EEXIST;
60 list_for_each_entry(ptr, &fscache_netfs_list, link) {
61 if (strcmp(ptr->name, netfs->name) == 0)
62 goto already_registered;
63 }
64
65 list_add(&netfs->link, &fscache_netfs_list);
66 ret = 0;
67
Fabian Frederick36dfd112014-06-04 16:05:38 -070068 pr_notice("Netfs '%s' registered for caching\n", netfs->name);
David Howells726dd7f2009-04-03 16:42:38 +010069
70already_registered:
71 up_write(&fscache_addremove_sem);
72
73 if (ret < 0) {
74 netfs->primary_index->parent = NULL;
75 __fscache_cookie_put(netfs->primary_index);
76 netfs->primary_index = NULL;
77 }
78
79 _leave(" = %d", ret);
80 return ret;
81}
82EXPORT_SYMBOL(__fscache_register_netfs);
83
84/*
85 * unregister a network filesystem from the cache
86 * - all cookies must have been released first
87 */
88void __fscache_unregister_netfs(struct fscache_netfs *netfs)
89{
90 _enter("{%s.%u}", netfs->name, netfs->version);
91
92 down_write(&fscache_addremove_sem);
93
94 list_del(&netfs->link);
95 fscache_relinquish_cookie(netfs->primary_index, 0);
96
97 up_write(&fscache_addremove_sem);
98
Fabian Frederick36dfd112014-06-04 16:05:38 -070099 pr_notice("Netfs '%s' unregistered from caching\n",
100 netfs->name);
David Howells726dd7f2009-04-03 16:42:38 +0100101
102 _leave("");
103}
104EXPORT_SYMBOL(__fscache_unregister_netfs);