David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 1 | /* 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 | |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 17 | /* |
| 18 | * register a network filesystem for caching |
| 19 | */ |
| 20 | int __fscache_register_netfs(struct fscache_netfs *netfs) |
| 21 | { |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 22 | struct fscache_cookie *candidate, *cookie; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 23 | |
| 24 | _enter("{%s}", netfs->name); |
| 25 | |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 26 | /* allocate a cookie for the primary index */ |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 27 | candidate = fscache_alloc_cookie(&fscache_fsdef_index, |
| 28 | &fscache_fsdef_netfs_def, |
| 29 | netfs->name, strlen(netfs->name), |
| 30 | &netfs->version, sizeof(netfs->version), |
| 31 | netfs, 0); |
| 32 | if (!candidate) { |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 33 | _leave(" = -ENOMEM"); |
| 34 | return -ENOMEM; |
| 35 | } |
| 36 | |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 37 | candidate->flags = 1 << FSCACHE_COOKIE_ENABLED; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 38 | |
| 39 | /* check the netfs type is not already present */ |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 40 | cookie = fscache_hash_cookie(candidate); |
| 41 | if (!cookie) |
| 42 | goto already_registered; |
| 43 | if (cookie != candidate) { |
| 44 | trace_fscache_cookie(candidate, fscache_cookie_discard, 1); |
| 45 | fscache_free_cookie(candidate); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 46 | } |
| 47 | |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 48 | fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs); |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 49 | atomic_inc(&cookie->parent->n_children); |
Kinglong Mee | 86108c2 | 2015-11-04 15:20:15 +0000 | [diff] [blame] | 50 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 51 | netfs->primary_index = cookie; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 52 | |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 53 | pr_notice("Netfs '%s' registered for caching\n", netfs->name); |
David Howells | a18feb5 | 2018-04-04 13:41:27 +0100 | [diff] [blame] | 54 | trace_fscache_netfs(netfs); |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 55 | _leave(" = 0"); |
| 56 | return 0; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 57 | |
| 58 | already_registered: |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 59 | fscache_cookie_put(candidate, fscache_cookie_put_dup_netfs); |
| 60 | _leave(" = -EEXIST"); |
| 61 | return -EEXIST; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 62 | } |
| 63 | EXPORT_SYMBOL(__fscache_register_netfs); |
| 64 | |
| 65 | /* |
| 66 | * unregister a network filesystem from the cache |
| 67 | * - all cookies must have been released first |
| 68 | */ |
| 69 | void __fscache_unregister_netfs(struct fscache_netfs *netfs) |
| 70 | { |
| 71 | _enter("{%s.%u}", netfs->name, netfs->version); |
| 72 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 73 | fscache_relinquish_cookie(netfs->primary_index, NULL, false); |
David Howells | ec0328e | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 74 | pr_notice("Netfs '%s' unregistered from caching\n", netfs->name); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 75 | |
| 76 | _leave(""); |
| 77 | } |
| 78 | EXPORT_SYMBOL(__fscache_unregister_netfs); |