fsdef.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. };
  57. EXPORT_SYMBOL(fscache_fsdef_index);
  58. /*
  59. * Definition of an entry in the root index. Each entry is an index, keyed to
  60. * a specific netfs and only applicable to a particular version of the index
  61. * structure used by that netfs.
  62. */
  63. struct fscache_cookie_def fscache_fsdef_netfs_def = {
  64. .name = "FSDEF.netfs",
  65. .type = FSCACHE_COOKIE_TYPE_INDEX,
  66. .get_key = fscache_fsdef_netfs_get_key,
  67. .get_aux = fscache_fsdef_netfs_get_aux,
  68. .check_aux = fscache_fsdef_netfs_check_aux,
  69. };
  70. /*
  71. * get the key data for an FSDEF index record - this is the name of the netfs
  72. * for which this entry is created
  73. */
  74. static uint16_t fscache_fsdef_netfs_get_key(const void *cookie_netfs_data,
  75. void *buffer, uint16_t bufmax)
  76. {
  77. const struct fscache_netfs *netfs = cookie_netfs_data;
  78. unsigned klen;
  79. _enter("{%s.%u},", netfs->name, netfs->version);
  80. klen = strlen(netfs->name);
  81. if (klen > bufmax)
  82. return 0;
  83. memcpy(buffer, netfs->name, klen);
  84. return klen;
  85. }
  86. /*
  87. * get the auxiliary data for an FSDEF index record - this is the index
  88. * structure version number of the netfs for which this version is created
  89. */
  90. static uint16_t fscache_fsdef_netfs_get_aux(const void *cookie_netfs_data,
  91. void *buffer, uint16_t bufmax)
  92. {
  93. const struct fscache_netfs *netfs = cookie_netfs_data;
  94. unsigned dlen;
  95. _enter("{%s.%u},", netfs->name, netfs->version);
  96. dlen = sizeof(uint32_t);
  97. if (dlen > bufmax)
  98. return 0;
  99. memcpy(buffer, &netfs->version, dlen);
  100. return dlen;
  101. }
  102. /*
  103. * check that the index structure version number stored in the auxiliary data
  104. * matches the one the netfs gave us
  105. */
  106. static enum fscache_checkaux fscache_fsdef_netfs_check_aux(
  107. void *cookie_netfs_data,
  108. const void *data,
  109. uint16_t datalen)
  110. {
  111. struct fscache_netfs *netfs = cookie_netfs_data;
  112. uint32_t version;
  113. _enter("{%s},,%hu", netfs->name, datalen);
  114. if (datalen != sizeof(version)) {
  115. _leave(" = OBSOLETE [dl=%d v=%zu]", datalen, sizeof(version));
  116. return FSCACHE_CHECKAUX_OBSOLETE;
  117. }
  118. memcpy(&version, data, sizeof(version));
  119. if (version != netfs->version) {
  120. _leave(" = OBSOLETE [ver=%x net=%x]", version, netfs->version);
  121. return FSCACHE_CHECKAUX_OBSOLETE;
  122. }
  123. _leave(" = OKAY");
  124. return FSCACHE_CHECKAUX_OKAY;
  125. }