netfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. cookie->def = &fscache_fsdef_netfs_def;
  36. cookie->parent = &fscache_fsdef_index;
  37. cookie->netfs_data = netfs;
  38. spin_lock_init(&cookie->lock);
  39. INIT_HLIST_HEAD(&cookie->backing_objects);
  40. /* check the netfs type is not already present */
  41. down_write(&fscache_addremove_sem);
  42. ret = -EEXIST;
  43. list_for_each_entry(ptr, &fscache_netfs_list, link) {
  44. if (strcmp(ptr->name, netfs->name) == 0)
  45. goto already_registered;
  46. }
  47. atomic_inc(&cookie->parent->usage);
  48. atomic_inc(&cookie->parent->n_children);
  49. netfs->primary_index = cookie;
  50. list_add(&netfs->link, &fscache_netfs_list);
  51. ret = 0;
  52. printk(KERN_NOTICE "FS-Cache: Netfs '%s' registered for caching\n",
  53. netfs->name);
  54. already_registered:
  55. up_write(&fscache_addremove_sem);
  56. if (ret < 0)
  57. kmem_cache_free(fscache_cookie_jar, cookie);
  58. _leave(" = %d", ret);
  59. return ret;
  60. }
  61. EXPORT_SYMBOL(__fscache_register_netfs);
  62. /*
  63. * unregister a network filesystem from the cache
  64. * - all cookies must have been released first
  65. */
  66. void __fscache_unregister_netfs(struct fscache_netfs *netfs)
  67. {
  68. _enter("{%s.%u}", netfs->name, netfs->version);
  69. down_write(&fscache_addremove_sem);
  70. list_del(&netfs->link);
  71. fscache_relinquish_cookie(netfs->primary_index, 0);
  72. up_write(&fscache_addremove_sem);
  73. printk(KERN_NOTICE "FS-Cache: Netfs '%s' unregistered from caching\n",
  74. netfs->name);
  75. _leave("");
  76. }
  77. EXPORT_SYMBOL(__fscache_unregister_netfs);