netfs.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #define FSCACHE_DEBUG_LEVEL COOKIE
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include "internal.h"
  11. /*
  12. * register a network filesystem for caching
  13. */
  14. int __fscache_register_netfs(struct fscache_netfs *netfs)
  15. {
  16. struct fscache_cookie *candidate, *cookie;
  17. _enter("{%s}", netfs->name);
  18. /* allocate a cookie for the primary index */
  19. candidate = fscache_alloc_cookie(&fscache_fsdef_index,
  20. &fscache_fsdef_netfs_def,
  21. netfs->name, strlen(netfs->name),
  22. &netfs->version, sizeof(netfs->version),
  23. netfs, 0);
  24. if (!candidate) {
  25. _leave(" = -ENOMEM");
  26. return -ENOMEM;
  27. }
  28. candidate->flags = 1 << FSCACHE_COOKIE_ENABLED;
  29. /* check the netfs type is not already present */
  30. cookie = fscache_hash_cookie(candidate);
  31. if (!cookie)
  32. goto already_registered;
  33. if (cookie != candidate) {
  34. trace_fscache_cookie(candidate, fscache_cookie_discard, 1);
  35. fscache_free_cookie(candidate);
  36. }
  37. fscache_cookie_get(cookie->parent, fscache_cookie_get_register_netfs);
  38. atomic_inc(&cookie->parent->n_children);
  39. netfs->primary_index = cookie;
  40. pr_notice("Netfs '%s' registered for caching\n", netfs->name);
  41. trace_fscache_netfs(netfs);
  42. _leave(" = 0");
  43. return 0;
  44. already_registered:
  45. fscache_cookie_put(candidate, fscache_cookie_put_dup_netfs);
  46. _leave(" = -EEXIST");
  47. return -EEXIST;
  48. }
  49. EXPORT_SYMBOL(__fscache_register_netfs);
  50. /*
  51. * unregister a network filesystem from the cache
  52. * - all cookies must have been released first
  53. */
  54. void __fscache_unregister_netfs(struct fscache_netfs *netfs)
  55. {
  56. _enter("{%s.%u}", netfs->name, netfs->version);
  57. fscache_relinquish_cookie(netfs->primary_index, NULL, false);
  58. pr_notice("Netfs '%s' unregistered from caching\n", netfs->name);
  59. _leave("");
  60. }
  61. EXPORT_SYMBOL(__fscache_unregister_netfs);