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