Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 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) |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define FSCACHE_DEBUG_LEVEL COOKIE |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include "internal.h" |
| 12 | |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 13 | /* |
| 14 | * register a network filesystem for caching |
| 15 | */ |
| 16 | int __fscache_register_netfs(struct fscache_netfs *netfs) |
| 17 | { |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 18 | struct fscache_cookie *candidate, *cookie; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 19 | |
| 20 | _enter("{%s}", netfs->name); |
| 21 | |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 22 | /* allocate a cookie for the primary index */ |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 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) { |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 29 | _leave(" = -ENOMEM"); |
| 30 | return -ENOMEM; |
| 31 | } |
| 32 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 33 | candidate->flags = 1 << FSCACHE_COOKIE_ENABLED; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 34 | |
| 35 | /* check the netfs type is not already present */ |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 36 | cookie = fscache_hash_cookie(candidate); |
| 37 | if (!cookie) |
| 38 | goto already_registered; |
| 39 | if (cookie != candidate) { |
David Howells | 33cba85 | 2021-06-18 11:19:49 +0100 | [diff] [blame] | 40 | trace_fscache_cookie(candidate->debug_id, 1, fscache_cookie_discard); |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 41 | fscache_free_cookie(candidate); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 42 | } |
| 43 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 44 | fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs); |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 45 | atomic_inc(&cookie->parent->n_children); |
Kinglong Mee | 86108c2 | 2015-11-04 15:20:15 +0000 | [diff] [blame] | 46 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 47 | netfs->primary_index = cookie; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 48 | |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 49 | pr_notice("Netfs '%s' registered for caching\n", netfs->name); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 50 | trace_fscache_netfs(netfs); |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 51 | _leave(" = 0"); |
| 52 | return 0; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 53 | |
| 54 | already_registered: |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 55 | fscache_cookie_put(candidate, fscache_cookie_put_dup_netfs); |
| 56 | _leave(" = -EEXIST"); |
| 57 | return -EEXIST; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 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 | */ |
| 65 | void __fscache_unregister_netfs(struct fscache_netfs *netfs) |
| 66 | { |
| 67 | _enter("{%s.%u}", netfs->name, netfs->version); |
| 68 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 69 | fscache_relinquish_cookie(netfs->primary_index, NULL, false); |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 70 | pr_notice("Netfs '%s' unregistered from caching\n", netfs->name); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 71 | |
| 72 | _leave(""); |
| 73 | } |
| 74 | EXPORT_SYMBOL(__fscache_unregister_netfs); |