netfs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. int ret;
  23. _enter("{%s}", netfs->name);
  24. INIT_LIST_HEAD(&netfs->link);
  25. /* allocate a cookie for the primary index */
  26. netfs->primary_index =
  27. kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
  28. if (!netfs->primary_index) {
  29. _leave(" = -ENOMEM");
  30. return -ENOMEM;
  31. }
  32. /* initialise the primary index cookie */
  33. atomic_set(&netfs->primary_index->usage, 1);
  34. atomic_set(&netfs->primary_index->n_children, 0);
  35. atomic_set(&netfs->primary_index->n_active, 1);
  36. netfs->primary_index->def = &fscache_fsdef_netfs_def;
  37. netfs->primary_index->parent = &fscache_fsdef_index;
  38. netfs->primary_index->netfs_data = netfs;
  39. netfs->primary_index->flags = 1 << FSCACHE_COOKIE_ENABLED;
  40. atomic_inc(&netfs->primary_index->parent->usage);
  41. atomic_inc(&netfs->primary_index->parent->n_children);
  42. spin_lock_init(&netfs->primary_index->lock);
  43. INIT_HLIST_HEAD(&netfs->primary_index->backing_objects);
  44. /* check the netfs type is not already present */
  45. down_write(&fscache_addremove_sem);
  46. ret = -EEXIST;
  47. list_for_each_entry(ptr, &fscache_netfs_list, link) {
  48. if (strcmp(ptr->name, netfs->name) == 0)
  49. goto already_registered;
  50. }
  51. list_add(&netfs->link, &fscache_netfs_list);
  52. ret = 0;
  53. pr_notice("Netfs '%s' registered for caching\n", netfs->name);
  54. already_registered:
  55. up_write(&fscache_addremove_sem);
  56. if (ret < 0) {
  57. netfs->primary_index->parent = NULL;
  58. __fscache_cookie_put(netfs->primary_index);
  59. netfs->primary_index = NULL;
  60. }
  61. _leave(" = %d", ret);
  62. return ret;
  63. }
  64. EXPORT_SYMBOL(__fscache_register_netfs);
  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. down_write(&fscache_addremove_sem);
  73. list_del(&netfs->link);
  74. fscache_relinquish_cookie(netfs->primary_index, 0);
  75. up_write(&fscache_addremove_sem);
  76. pr_notice("Netfs '%s' unregistered from caching\n",
  77. netfs->name);
  78. _leave("");
  79. }
  80. EXPORT_SYMBOL(__fscache_unregister_netfs);