fsdef.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Filesystem index definition
  2. *
  3. * Copyright (C) 2004-2007 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 License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define FSCACHE_DEBUG_LEVEL CACHE
  12. #include <linux/module.h>
  13. #include "internal.h"
  14. static uint16_t fscache_fsdef_netfs_get_key(const void *cookie_netfs_data,
  15. void *buffer, uint16_t bufmax);
  16. static uint16_t fscache_fsdef_netfs_get_aux(const void *cookie_netfs_data,
  17. void *buffer, uint16_t bufmax);
  18. static
  19. enum fscache_checkaux fscache_fsdef_netfs_check_aux(void *cookie_netfs_data,
  20. const void *data,
  21. uint16_t datalen);
  22. /*
  23. * The root index is owned by FS-Cache itself.
  24. *
  25. * When a netfs requests caching facilities, FS-Cache will, if one doesn't
  26. * already exist, create an entry in the root index with the key being the name
  27. * of the netfs ("AFS" for example), and the auxiliary data holding the index
  28. * structure version supplied by the netfs:
  29. *
  30. * FSDEF
  31. * |
  32. * +-----------+
  33. * | |
  34. * NFS AFS
  35. * [v=1] [v=1]
  36. *
  37. * If an entry with the appropriate name does already exist, the version is
  38. * compared. If the version is different, the entire subtree from that entry
  39. * will be discarded and a new entry created.
  40. *
  41. * The new entry will be an index, and a cookie referring to it will be passed
  42. * to the netfs. This is then the root handle by which the netfs accesses the
  43. * cache. It can create whatever objects it likes in that index, including
  44. * further indices.
  45. */
  46. static struct fscache_cookie_def fscache_fsdef_index_def = {
  47. .name = ".FS-Cache",
  48. .type = FSCACHE_COOKIE_TYPE_INDEX,
  49. };
  50. struct fscache_cookie fscache_fsdef_index = {
  51. .usage = ATOMIC_INIT(1),
  52. .n_active = ATOMIC_INIT(1),
  53. .lock = __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock),
  54. .backing_objects = HLIST_HEAD_INIT,
  55. .def = &fscache_fsdef_index_def,
  56. .flags = 1 << FSCACHE_COOKIE_ENABLED,
  57. };
  58. EXPORT_SYMBOL(fscache_fsdef_index);
  59. /*
  60. * Definition of an entry in the root index. Each entry is an index, keyed to
  61. * a specific netfs and only applicable to a particular version of the index
  62. * structure used by that netfs.
  63. */
  64. struct fscache_cookie_def fscache_fsdef_netfs_def = {
  65. .name = "FSDEF.netfs",
  66. .type = FSCACHE_COOKIE_TYPE_INDEX,
  67. .get_key = fscache_fsdef_netfs_get_key,
  68. .get_aux = fscache_fsdef_netfs_get_aux,
  69. .check_aux = fscache_fsdef_netfs_check_aux,
  70. };
  71. /*
  72. * get the key data for an FSDEF index record - this is the name of the netfs
  73. * for which this entry is created
  74. */
  75. static uint16_t fscache_fsdef_netfs_get_key(const void *cookie_netfs_data,
  76. void *buffer, uint16_t bufmax)
  77. {
  78. const struct fscache_netfs *netfs = cookie_netfs_data;
  79. unsigned klen;
  80. _enter("{%s.%u},", netfs->name, netfs->version);
  81. klen = strlen(netfs->name);
  82. if (klen > bufmax)
  83. return 0;
  84. memcpy(buffer, netfs->name, klen);
  85. return klen;
  86. }
  87. /*
  88. * get the auxiliary data for an FSDEF index record - this is the index
  89. * structure version number of the netfs for which this version is created
  90. */
  91. static uint16_t fscache_fsdef_netfs_get_aux(const void *cookie_netfs_data,
  92. void *buffer, uint16_t bufmax)
  93. {
  94. const struct fscache_netfs *netfs = cookie_netfs_data;
  95. unsigned dlen;
  96. _enter("{%s.%u},", netfs->name, netfs->version);
  97. dlen = sizeof(uint32_t);
  98. if (dlen > bufmax)
  99. return 0;
  100. memcpy(buffer, &netfs->version, dlen);
  101. return dlen;
  102. }
  103. /*
  104. * check that the index structure version number stored in the auxiliary data
  105. * matches the one the netfs gave us
  106. */
  107. static enum fscache_checkaux fscache_fsdef_netfs_check_aux(
  108. void *cookie_netfs_data,
  109. const void *data,
  110. uint16_t datalen)
  111. {
  112. struct fscache_netfs *netfs = cookie_netfs_data;
  113. uint32_t version;
  114. _enter("{%s},,%hu", netfs->name, datalen);
  115. if (datalen != sizeof(version)) {
  116. _leave(" = OBSOLETE [dl=%d v=%zu]", datalen, sizeof(version));
  117. return FSCACHE_CHECKAUX_OBSOLETE;
  118. }
  119. memcpy(&version, data, sizeof(version));
  120. if (version != netfs->version) {
  121. _leave(" = OBSOLETE [ver=%x net=%x]", version, netfs->version);
  122. return FSCACHE_CHECKAUX_OBSOLETE;
  123. }
  124. _leave(" = OKAY");
  125. return FSCACHE_CHECKAUX_OKAY;
  126. }