fsdef.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Filesystem index definition
  3. *
  4. * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define FSCACHE_DEBUG_LEVEL CACHE
  8. #include <linux/module.h>
  9. #include "internal.h"
  10. static
  11. enum fscache_checkaux fscache_fsdef_netfs_check_aux(void *cookie_netfs_data,
  12. const void *data,
  13. uint16_t datalen,
  14. loff_t object_size);
  15. /*
  16. * The root index is owned by FS-Cache itself.
  17. *
  18. * When a netfs requests caching facilities, FS-Cache will, if one doesn't
  19. * already exist, create an entry in the root index with the key being the name
  20. * of the netfs ("AFS" for example), and the auxiliary data holding the index
  21. * structure version supplied by the netfs:
  22. *
  23. * FSDEF
  24. * |
  25. * +-----------+
  26. * | |
  27. * NFS AFS
  28. * [v=1] [v=1]
  29. *
  30. * If an entry with the appropriate name does already exist, the version is
  31. * compared. If the version is different, the entire subtree from that entry
  32. * will be discarded and a new entry created.
  33. *
  34. * The new entry will be an index, and a cookie referring to it will be passed
  35. * to the netfs. This is then the root handle by which the netfs accesses the
  36. * cache. It can create whatever objects it likes in that index, including
  37. * further indices.
  38. */
  39. static struct fscache_cookie_def fscache_fsdef_index_def = {
  40. .name = ".FS-Cache",
  41. .type = FSCACHE_COOKIE_TYPE_INDEX,
  42. };
  43. struct fscache_cookie fscache_fsdef_index = {
  44. .usage = ATOMIC_INIT(1),
  45. .n_active = ATOMIC_INIT(1),
  46. .lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock),
  47. .backing_objects = HLIST_HEAD_INIT,
  48. .def = &fscache_fsdef_index_def,
  49. .flags = 1 << FSCACHE_COOKIE_ENABLED,
  50. .type = FSCACHE_COOKIE_TYPE_INDEX,
  51. };
  52. EXPORT_SYMBOL(fscache_fsdef_index);
  53. /*
  54. * Definition of an entry in the root index. Each entry is an index, keyed to
  55. * a specific netfs and only applicable to a particular version of the index
  56. * structure used by that netfs.
  57. */
  58. struct fscache_cookie_def fscache_fsdef_netfs_def = {
  59. .name = "FSDEF.netfs",
  60. .type = FSCACHE_COOKIE_TYPE_INDEX,
  61. .check_aux = fscache_fsdef_netfs_check_aux,
  62. };
  63. /*
  64. * check that the index structure version number stored in the auxiliary data
  65. * matches the one the netfs gave us
  66. */
  67. static enum fscache_checkaux fscache_fsdef_netfs_check_aux(
  68. void *cookie_netfs_data,
  69. const void *data,
  70. uint16_t datalen,
  71. loff_t object_size)
  72. {
  73. struct fscache_netfs *netfs = cookie_netfs_data;
  74. uint32_t version;
  75. _enter("{%s},,%hu", netfs->name, datalen);
  76. if (datalen != sizeof(version)) {
  77. _leave(" = OBSOLETE [dl=%d v=%zu]", datalen, sizeof(version));
  78. return FSCACHE_CHECKAUX_OBSOLETE;
  79. }
  80. memcpy(&version, data, sizeof(version));
  81. if (version != netfs->version) {
  82. _leave(" = OBSOLETE [ver=%x net=%x]", version, netfs->version);
  83. return FSCACHE_CHECKAUX_OBSOLETE;
  84. }
  85. _leave(" = OKAY");
  86. return FSCACHE_CHECKAUX_OKAY;
  87. }