dir_search.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Search a directory's hash table.
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. *
  7. * https://tools.ietf.org/html/draft-keiser-afs3-directory-object-00
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/fs.h>
  11. #include <linux/namei.h>
  12. #include <linux/iversion.h>
  13. #include "internal.h"
  14. #include "afs_fs.h"
  15. #include "xdr_fs.h"
  16. /*
  17. * Calculate the name hash.
  18. */
  19. unsigned int afs_dir_hash_name(const struct qstr *name)
  20. {
  21. const unsigned char *p = name->name;
  22. unsigned int hash = 0, i;
  23. int bucket;
  24. for (i = 0; i < name->len; i++)
  25. hash = (hash * 173) + p[i];
  26. bucket = hash & (AFS_DIR_HASHTBL_SIZE - 1);
  27. if (hash > INT_MAX) {
  28. bucket = AFS_DIR_HASHTBL_SIZE - bucket;
  29. bucket &= (AFS_DIR_HASHTBL_SIZE - 1);
  30. }
  31. return bucket;
  32. }
  33. /*
  34. * Reset a directory iterator.
  35. */
  36. static bool afs_dir_reset_iter(struct afs_dir_iter *iter)
  37. {
  38. unsigned long long i_size = i_size_read(&iter->dvnode->netfs.inode);
  39. unsigned int nblocks;
  40. /* Work out the maximum number of steps we can take. */
  41. nblocks = umin(i_size / AFS_DIR_BLOCK_SIZE, AFS_DIR_MAX_BLOCKS);
  42. if (!nblocks)
  43. return false;
  44. iter->loop_check = nblocks * (AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS);
  45. iter->prev_entry = 0; /* Hash head is previous */
  46. return true;
  47. }
  48. /*
  49. * Initialise a directory iterator for looking up a name.
  50. */
  51. bool afs_dir_init_iter(struct afs_dir_iter *iter, const struct qstr *name)
  52. {
  53. iter->nr_slots = afs_dir_calc_slots(name->len);
  54. iter->bucket = afs_dir_hash_name(name);
  55. return afs_dir_reset_iter(iter);
  56. }
  57. /*
  58. * Get a specific block.
  59. */
  60. union afs_xdr_dir_block *afs_dir_find_block(struct afs_dir_iter *iter, size_t block)
  61. {
  62. struct folio_queue *fq = iter->fq;
  63. struct afs_vnode *dvnode = iter->dvnode;
  64. struct folio *folio;
  65. size_t blpos = block * AFS_DIR_BLOCK_SIZE;
  66. size_t blend = (block + 1) * AFS_DIR_BLOCK_SIZE, fpos = iter->fpos;
  67. int slot = iter->fq_slot;
  68. _enter("%zx,%d", block, slot);
  69. if (iter->block) {
  70. kunmap_local(iter->block);
  71. iter->block = NULL;
  72. }
  73. if (dvnode->directory_size < blend)
  74. goto fail;
  75. if (!fq || blpos < fpos) {
  76. fq = dvnode->directory;
  77. slot = 0;
  78. fpos = 0;
  79. }
  80. /* Search the folio queue for the folio containing the block... */
  81. for (; fq; fq = fq->next) {
  82. for (; slot < folioq_count(fq); slot++) {
  83. size_t fsize = folioq_folio_size(fq, slot);
  84. if (blend <= fpos + fsize) {
  85. /* ... and then return the mapped block. */
  86. folio = folioq_folio(fq, slot);
  87. if (WARN_ON_ONCE(folio_pos(folio) != fpos))
  88. goto fail;
  89. iter->fq = fq;
  90. iter->fq_slot = slot;
  91. iter->fpos = fpos;
  92. iter->block = kmap_local_folio(folio, blpos - fpos);
  93. return iter->block;
  94. }
  95. fpos += fsize;
  96. }
  97. slot = 0;
  98. }
  99. fail:
  100. iter->fq = NULL;
  101. iter->fq_slot = 0;
  102. afs_invalidate_dir(dvnode, afs_dir_invalid_edit_get_block);
  103. return NULL;
  104. }
  105. /*
  106. * Search through a directory bucket.
  107. */
  108. int afs_dir_search_bucket(struct afs_dir_iter *iter, const struct qstr *name,
  109. struct afs_fid *_fid)
  110. {
  111. const union afs_xdr_dir_block *meta;
  112. unsigned int entry;
  113. int ret = -ESTALE;
  114. meta = afs_dir_find_block(iter, 0);
  115. if (!meta)
  116. return -ESTALE;
  117. entry = ntohs(meta->meta.hashtable[iter->bucket & (AFS_DIR_HASHTBL_SIZE - 1)]);
  118. _enter("%x,%x", iter->bucket, entry);
  119. while (entry) {
  120. const union afs_xdr_dir_block *block;
  121. const union afs_xdr_dirent *dire;
  122. unsigned int blnum = entry / AFS_DIR_SLOTS_PER_BLOCK;
  123. unsigned int slot = entry % AFS_DIR_SLOTS_PER_BLOCK;
  124. unsigned int resv = (blnum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  125. _debug("search %x", entry);
  126. if (slot < resv) {
  127. kdebug("slot out of range h=%x rs=%2x sl=%2x-%2x",
  128. iter->bucket, resv, slot, slot + iter->nr_slots - 1);
  129. goto bad;
  130. }
  131. block = afs_dir_find_block(iter, blnum);
  132. if (!block)
  133. goto bad;
  134. dire = &block->dirents[slot];
  135. if (slot + iter->nr_slots <= AFS_DIR_SLOTS_PER_BLOCK &&
  136. memcmp(dire->u.name, name->name, name->len) == 0 &&
  137. dire->u.name[name->len] == '\0') {
  138. _fid->vnode = ntohl(dire->u.vnode);
  139. _fid->unique = ntohl(dire->u.unique);
  140. ret = entry;
  141. goto found;
  142. }
  143. iter->prev_entry = entry;
  144. entry = ntohs(dire->u.hash_next);
  145. if (!--iter->loop_check) {
  146. kdebug("dir chain loop h=%x", iter->bucket);
  147. goto bad;
  148. }
  149. }
  150. ret = -ENOENT;
  151. found:
  152. if (iter->block) {
  153. kunmap_local(iter->block);
  154. iter->block = NULL;
  155. }
  156. bad:
  157. if (ret == -ESTALE)
  158. afs_invalidate_dir(iter->dvnode, afs_dir_invalid_iter_stale);
  159. _leave(" = %d", ret);
  160. return ret;
  161. }
  162. /*
  163. * Search the appropriate hash chain in the contents of an AFS directory.
  164. */
  165. int afs_dir_search(struct afs_vnode *dvnode, const struct qstr *name,
  166. struct afs_fid *_fid, afs_dataversion_t *_dir_version)
  167. {
  168. struct afs_dir_iter iter = { .dvnode = dvnode, };
  169. int ret, retry_limit = 3;
  170. _enter("{%lu},,,", dvnode->netfs.inode.i_ino);
  171. if (!afs_dir_init_iter(&iter, name))
  172. return -ENOENT;
  173. do {
  174. if (--retry_limit < 0) {
  175. pr_warn("afs_read_dir(): Too many retries\n");
  176. ret = -ESTALE;
  177. break;
  178. }
  179. ret = afs_read_dir(dvnode, NULL);
  180. if (ret < 0) {
  181. if (ret != -ESTALE)
  182. break;
  183. if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
  184. ret = -ESTALE;
  185. break;
  186. }
  187. continue;
  188. }
  189. *_dir_version = inode_peek_iversion_raw(&dvnode->netfs.inode);
  190. ret = afs_dir_search_bucket(&iter, name, _fid);
  191. up_read(&dvnode->validate_lock);
  192. if (ret == -ESTALE)
  193. afs_dir_reset_iter(&iter);
  194. } while (ret == -ESTALE);
  195. _leave(" = %d", ret);
  196. return ret;
  197. }