netfs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #define FSCACHE_DEBUG_LEVEL COOKIE
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include "internal.h"
  15. static LIST_HEAD(fscache_netfs_list);
  16. /*
  17. * register a network filesystem for caching
  18. */
  19. int __fscache_register_netfs(struct fscache_netfs *netfs)
  20. {
  21. struct fscache_netfs *ptr;
  22. struct fscache_cookie *cookie;
  23. int ret;
  24. _enter("{%s}", netfs->name);
  25. INIT_LIST_HEAD(&netfs->link);
  26. /* allocate a cookie for the primary index */
  27. cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
  28. if (!cookie) {
  29. _leave(" = -ENOMEM");
  30. return -ENOMEM;
  31. }
  32. /* initialise the primary index cookie */
  33. atomic_set(&cookie->usage, 1);
  34. atomic_set(&cookie->n_children, 0);
  35. atomic_set(&cookie->n_active, 1);
  36. cookie->def = &fscache_fsdef_netfs_def;
  37. cookie->parent = &fscache_fsdef_index;
  38. cookie->netfs_data = netfs;
  39. cookie->flags = 1 << FSCACHE_COOKIE_ENABLED;
  40. spin_lock_init(&cookie->lock);
  41. spin_lock_init(&cookie->stores_lock);
  42. INIT_HLIST_HEAD(&cookie->backing_objects);
  43. /* check the netfs type is not already present */
  44. down_write(&fscache_addremove_sem);
  45. ret = -EEXIST;
  46. list_for_each_entry(ptr, &fscache_netfs_list, link) {
  47. if (strcmp(ptr->name, netfs->name) == 0)
  48. goto already_registered;
  49. }
  50. atomic_inc(&cookie->parent->usage);
  51. atomic_inc(&cookie->parent->n_children);
  52. netfs->primary_index = cookie;
  53. list_add(&netfs->link, &fscache_netfs_list);
  54. ret = 0;
  55. pr_notice("Netfs '%s' registered for caching\n", netfs->name);
  56. already_registered:
  57. up_write(&fscache_addremove_sem);
  58. if (ret < 0)
  59. kmem_cache_free(fscache_cookie_jar, cookie);
  60. _leave(" = %d", ret);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL(__fscache_register_netfs);
  64. /*
  65. * unregister a network filesystem from the cache
  66. * - all cookies must have been released first
  67. */
  68. void __fscache_unregister_netfs(struct fscache_netfs *netfs)
  69. {
  70. _enter("{%s.%u}", netfs->name, netfs->version);
  71. down_write(&fscache_addremove_sem);
  72. list_del(&netfs->link);
  73. fscache_relinquish_cookie(netfs->primary_index, 0);
  74. up_write(&fscache_addremove_sem);
  75. pr_notice("Netfs '%s' unregistered from caching\n",
  76. netfs->name);
  77. _leave("");
  78. }
  79. EXPORT_SYMBOL(__fscache_unregister_netfs);