blob: d6bdb7b5e72321f62aef46cccfe6a6d6e28953ef [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells726dd7f2009-04-03 16:42:38 +01002/* FS-Cache netfs (client) registration
3 *
4 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells726dd7f2009-04-03 16:42:38 +01006 */
7
8#define FSCACHE_DEBUG_LEVEL COOKIE
9#include <linux/module.h>
10#include <linux/slab.h>
11#include "internal.h"
12
David Howells726dd7f2009-04-03 16:42:38 +010013/*
14 * register a network filesystem for caching
15 */
16int __fscache_register_netfs(struct fscache_netfs *netfs)
17{
David Howellsec0328e2018-04-04 13:41:28 +010018 struct fscache_cookie *candidate, *cookie;
David Howells726dd7f2009-04-03 16:42:38 +010019
20 _enter("{%s}", netfs->name);
21
David Howells726dd7f2009-04-03 16:42:38 +010022 /* allocate a cookie for the primary index */
David Howellsec0328e2018-04-04 13:41:28 +010023 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) {
David Howells726dd7f2009-04-03 16:42:38 +010029 _leave(" = -ENOMEM");
30 return -ENOMEM;
31 }
32
David Howellsec0328e2018-04-04 13:41:28 +010033 candidate->flags = 1 << FSCACHE_COOKIE_ENABLED;
David Howells726dd7f2009-04-03 16:42:38 +010034
35 /* check the netfs type is not already present */
David Howellsec0328e2018-04-04 13:41:28 +010036 cookie = fscache_hash_cookie(candidate);
37 if (!cookie)
38 goto already_registered;
39 if (cookie != candidate) {
David Howells33cba852021-06-18 11:19:49 +010040 trace_fscache_cookie(candidate->debug_id, 1, fscache_cookie_discard);
David Howellsec0328e2018-04-04 13:41:28 +010041 fscache_free_cookie(candidate);
David Howells726dd7f2009-04-03 16:42:38 +010042 }
43
David Howellsa18feb52018-04-04 13:41:27 +010044 fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs);
Kinglong Meeb130ed52015-11-04 15:20:24 +000045 atomic_inc(&cookie->parent->n_children);
Kinglong Mee86108c22015-11-04 15:20:15 +000046
Kinglong Meeb130ed52015-11-04 15:20:24 +000047 netfs->primary_index = cookie;
David Howells726dd7f2009-04-03 16:42:38 +010048
Fabian Frederick36dfd112014-06-04 16:05:38 -070049 pr_notice("Netfs '%s' registered for caching\n", netfs->name);
David Howellsa18feb52018-04-04 13:41:27 +010050 trace_fscache_netfs(netfs);
David Howellsec0328e2018-04-04 13:41:28 +010051 _leave(" = 0");
52 return 0;
David Howells726dd7f2009-04-03 16:42:38 +010053
54already_registered:
David Howellsec0328e2018-04-04 13:41:28 +010055 fscache_cookie_put(candidate, fscache_cookie_put_dup_netfs);
56 _leave(" = -EEXIST");
57 return -EEXIST;
David Howells726dd7f2009-04-03 16:42:38 +010058}
59EXPORT_SYMBOL(__fscache_register_netfs);
60
61/*
62 * unregister a network filesystem from the cache
63 * - all cookies must have been released first
64 */
65void __fscache_unregister_netfs(struct fscache_netfs *netfs)
66{
67 _enter("{%s.%u}", netfs->name, netfs->version);
68
David Howells402cb8d2018-04-04 13:41:28 +010069 fscache_relinquish_cookie(netfs->primary_index, NULL, false);
David Howellsec0328e2018-04-04 13:41:28 +010070 pr_notice("Netfs '%s' unregistered from caching\n", netfs->name);
David Howells726dd7f2009-04-03 16:42:38 +010071
72 _leave("");
73}
74EXPORT_SYMBOL(__fscache_unregister_netfs);