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 | |
| 17 | static LIST_HEAD(fscache_netfs_list); |
| 18 | |
| 19 | /* |
| 20 | * register a network filesystem for caching |
| 21 | */ |
| 22 | int __fscache_register_netfs(struct fscache_netfs *netfs) |
| 23 | { |
| 24 | struct fscache_netfs *ptr; |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 25 | struct fscache_cookie *cookie; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 26 | 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 Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 33 | cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 34 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 35 | if (!cookie) { |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 36 | _leave(" = -ENOMEM"); |
| 37 | return -ENOMEM; |
| 38 | } |
| 39 | |
| 40 | /* initialise the primary index cookie */ |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 41 | atomic_set(&cookie->usage, 1); |
| 42 | atomic_set(&cookie->n_children, 0); |
| 43 | atomic_set(&cookie->n_active, 1); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 44 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 45 | 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 Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 49 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 50 | spin_lock_init(&cookie->lock); |
| 51 | INIT_HLIST_HEAD(&cookie->backing_objects); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 52 | |
| 53 | /* check the netfs type is not already present */ |
| 54 | down_write(&fscache_addremove_sem); |
| 55 | |
| 56 | ret = -EEXIST; |
| 57 | list_for_each_entry(ptr, &fscache_netfs_list, link) { |
| 58 | if (strcmp(ptr->name, netfs->name) == 0) |
| 59 | goto already_registered; |
| 60 | } |
| 61 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 62 | atomic_inc(&cookie->parent->usage); |
| 63 | atomic_inc(&cookie->parent->n_children); |
Kinglong Mee | 86108c2 | 2015-11-04 15:20:15 +0000 | [diff] [blame] | 64 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 65 | netfs->primary_index = cookie; |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 66 | list_add(&netfs->link, &fscache_netfs_list); |
| 67 | ret = 0; |
| 68 | |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 69 | pr_notice("Netfs '%s' registered for caching\n", netfs->name); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 70 | |
| 71 | already_registered: |
| 72 | up_write(&fscache_addremove_sem); |
| 73 | |
Kinglong Mee | b130ed5 | 2015-11-04 15:20:24 +0000 | [diff] [blame] | 74 | if (ret < 0) |
| 75 | kmem_cache_free(fscache_cookie_jar, cookie); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 76 | |
| 77 | _leave(" = %d", ret); |
| 78 | return ret; |
| 79 | } |
| 80 | EXPORT_SYMBOL(__fscache_register_netfs); |
| 81 | |
| 82 | /* |
| 83 | * unregister a network filesystem from the cache |
| 84 | * - all cookies must have been released first |
| 85 | */ |
| 86 | void __fscache_unregister_netfs(struct fscache_netfs *netfs) |
| 87 | { |
| 88 | _enter("{%s.%u}", netfs->name, netfs->version); |
| 89 | |
| 90 | down_write(&fscache_addremove_sem); |
| 91 | |
| 92 | list_del(&netfs->link); |
| 93 | fscache_relinquish_cookie(netfs->primary_index, 0); |
| 94 | |
| 95 | up_write(&fscache_addremove_sem); |
| 96 | |
Fabian Frederick | 36dfd11 | 2014-06-04 16:05:38 -0700 | [diff] [blame] | 97 | pr_notice("Netfs '%s' unregistered from caching\n", |
| 98 | netfs->name); |
David Howells | 726dd7f | 2009-04-03 16:42:38 +0100 | [diff] [blame] | 99 | |
| 100 | _leave(""); |
| 101 | } |
| 102 | EXPORT_SYMBOL(__fscache_unregister_netfs); |