blob: a8aa00be44442f59d6cf08516ab7403d0c02ab9e [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;
Kinglong Meeb130ed52015-11-04 15:20:24 +000025 struct fscache_cookie *cookie;
David Howells726dd7f2009-04-03 16:42:38 +010026 int ret;
27
28 _enter("{%s}", netfs->name);
29
30 INIT_LIST_HEAD(&netfs->link);
31
32 /* allocate a cookie for the primary index */
Kinglong Meeb130ed52015-11-04 15:20:24 +000033 cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
David Howells726dd7f2009-04-03 16:42:38 +010034
Kinglong Meeb130ed52015-11-04 15:20:24 +000035 if (!cookie) {
David Howells726dd7f2009-04-03 16:42:38 +010036 _leave(" = -ENOMEM");
37 return -ENOMEM;
38 }
39
40 /* initialise the primary index cookie */
Kinglong Meeb130ed52015-11-04 15:20:24 +000041 atomic_set(&cookie->usage, 1);
42 atomic_set(&cookie->n_children, 0);
43 atomic_set(&cookie->n_active, 1);
David Howells726dd7f2009-04-03 16:42:38 +010044
Kinglong Meeb130ed52015-11-04 15:20:24 +000045 cookie->def = &fscache_fsdef_netfs_def;
46 cookie->parent = &fscache_fsdef_index;
47 cookie->netfs_data = netfs;
48 cookie->flags = 1 << FSCACHE_COOKIE_ENABLED;
David Howells726dd7f2009-04-03 16:42:38 +010049
Kinglong Meeb130ed52015-11-04 15:20:24 +000050 spin_lock_init(&cookie->lock);
David Howells0542f972017-05-23 21:54:06 -040051 spin_lock_init(&cookie->stores_lock);
Kinglong Meeb130ed52015-11-04 15:20:24 +000052 INIT_HLIST_HEAD(&cookie->backing_objects);
David Howells726dd7f2009-04-03 16:42:38 +010053
54 /* check the netfs type is not already present */
55 down_write(&fscache_addremove_sem);
56
57 ret = -EEXIST;
58 list_for_each_entry(ptr, &fscache_netfs_list, link) {
59 if (strcmp(ptr->name, netfs->name) == 0)
60 goto already_registered;
61 }
62
Kinglong Meeb130ed52015-11-04 15:20:24 +000063 atomic_inc(&cookie->parent->usage);
64 atomic_inc(&cookie->parent->n_children);
Kinglong Mee86108c22015-11-04 15:20:15 +000065
Kinglong Meeb130ed52015-11-04 15:20:24 +000066 netfs->primary_index = cookie;
David Howells726dd7f2009-04-03 16:42:38 +010067 list_add(&netfs->link, &fscache_netfs_list);
68 ret = 0;
69
Fabian Frederick36dfd112014-06-04 16:05:38 -070070 pr_notice("Netfs '%s' registered for caching\n", netfs->name);
David Howells726dd7f2009-04-03 16:42:38 +010071
72already_registered:
73 up_write(&fscache_addremove_sem);
74
Kinglong Meeb130ed52015-11-04 15:20:24 +000075 if (ret < 0)
76 kmem_cache_free(fscache_cookie_jar, cookie);
David Howells726dd7f2009-04-03 16:42:38 +010077
78 _leave(" = %d", ret);
79 return ret;
80}
81EXPORT_SYMBOL(__fscache_register_netfs);
82
83/*
84 * unregister a network filesystem from the cache
85 * - all cookies must have been released first
86 */
87void __fscache_unregister_netfs(struct fscache_netfs *netfs)
88{
89 _enter("{%s.%u}", netfs->name, netfs->version);
90
91 down_write(&fscache_addremove_sem);
92
93 list_del(&netfs->link);
94 fscache_relinquish_cookie(netfs->primary_index, 0);
95
96 up_write(&fscache_addremove_sem);
97
Fabian Frederick36dfd112014-06-04 16:05:38 -070098 pr_notice("Netfs '%s' unregistered from caching\n",
99 netfs->name);
David Howells726dd7f2009-04-03 16:42:38 +0100100
101 _leave("");
102}
103EXPORT_SYMBOL(__fscache_unregister_netfs);