1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* FS-Cache netfs (client) registration
3  *
4  * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define FSCACHE_DEBUG_LEVEL COOKIE
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include "internal.h"
12 
13 /*
14  * register a network filesystem for caching
15  */
__fscache_register_netfs(struct fscache_netfs * netfs)16 int __fscache_register_netfs(struct fscache_netfs *netfs)
17 {
18 	struct fscache_cookie *candidate, *cookie;
19 
20 	_enter("{%s}", netfs->name);
21 
22 	/* allocate a cookie for the primary index */
23 	candidate = fscache_alloc_cookie(&fscache_fsdef_index,
24 					 &fscache_fsdef_netfs_def,
25 					 netfs->name, strlen(netfs->name),
26 					 &netfs->version, sizeof(netfs->version),
27 					 netfs, 0);
28 	if (!candidate) {
29 		_leave(" = -ENOMEM");
30 		return -ENOMEM;
31 	}
32 
33 	candidate->flags = 1 << FSCACHE_COOKIE_ENABLED;
34 
35 	/* check the netfs type is not already present */
36 	cookie = fscache_hash_cookie(candidate);
37 	if (!cookie)
38 		goto already_registered;
39 	if (cookie != candidate) {
40 		trace_fscache_cookie(candidate->debug_id, 1, fscache_cookie_discard);
41 		fscache_free_cookie(candidate);
42 	}
43 
44 	fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs);
45 	atomic_inc(&cookie->parent->n_children);
46 
47 	netfs->primary_index = cookie;
48 
49 	pr_notice("Netfs '%s' registered for caching\n", netfs->name);
50 	trace_fscache_netfs(netfs);
51 	_leave(" = 0");
52 	return 0;
53 
54 already_registered:
55 	fscache_cookie_put(candidate, fscache_cookie_put_dup_netfs);
56 	_leave(" = -EEXIST");
57 	return -EEXIST;
58 }
59 EXPORT_SYMBOL(__fscache_register_netfs);
60 
61 /*
62  * unregister a network filesystem from the cache
63  * - all cookies must have been released first
64  */
__fscache_unregister_netfs(struct fscache_netfs * netfs)65 void __fscache_unregister_netfs(struct fscache_netfs *netfs)
66 {
67 	_enter("{%s.%u}", netfs->name, netfs->version);
68 
69 	fscache_relinquish_cookie(netfs->primary_index, NULL, false);
70 	pr_notice("Netfs '%s' unregistered from caching\n", netfs->name);
71 
72 	_leave("");
73 }
74 EXPORT_SYMBOL(__fscache_unregister_netfs);
75