dir.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dir.c
  4. *
  5. * Copyright (c) 1999 Al Smith
  6. */
  7. #include <linux/buffer_head.h>
  8. #include <linux/filelock.h>
  9. #include "efs.h"
  10. static int efs_readdir(struct file *, struct dir_context *);
  11. const struct file_operations efs_dir_operations = {
  12. .llseek = generic_file_llseek,
  13. .read = generic_read_dir,
  14. .iterate_shared = efs_readdir,
  15. .setlease = generic_setlease,
  16. };
  17. const struct inode_operations efs_dir_inode_operations = {
  18. .lookup = efs_lookup,
  19. };
  20. static int efs_readdir(struct file *file, struct dir_context *ctx)
  21. {
  22. struct inode *inode = file_inode(file);
  23. efs_block_t block;
  24. int slot;
  25. if (inode->i_size & (EFS_DIRBSIZE-1))
  26. pr_warn("%s(): directory size not a multiple of EFS_DIRBSIZE\n",
  27. __func__);
  28. /* work out where this entry can be found */
  29. block = ctx->pos >> EFS_DIRBSIZE_BITS;
  30. /* each block contains at most 256 slots */
  31. slot = ctx->pos & 0xff;
  32. /* look at all blocks */
  33. while (block < inode->i_blocks) {
  34. struct efs_dir *dirblock;
  35. struct buffer_head *bh;
  36. /* read the dir block */
  37. bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
  38. if (!bh) {
  39. pr_err("%s(): failed to read dir block %d\n",
  40. __func__, block);
  41. break;
  42. }
  43. dirblock = (struct efs_dir *) bh->b_data;
  44. if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
  45. pr_err("%s(): invalid directory block\n", __func__);
  46. brelse(bh);
  47. break;
  48. }
  49. for (; slot < dirblock->slots; slot++) {
  50. struct efs_dentry *dirslot;
  51. efs_ino_t inodenum;
  52. const char *nameptr;
  53. int namelen;
  54. if (dirblock->space[slot] == 0)
  55. continue;
  56. dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
  57. inodenum = be32_to_cpu(dirslot->inode);
  58. namelen = dirslot->namelen;
  59. nameptr = dirslot->name;
  60. pr_debug("%s(): block %d slot %d/%d: inode %u, name \"%s\", namelen %u\n",
  61. __func__, block, slot, dirblock->slots-1,
  62. inodenum, nameptr, namelen);
  63. if (!namelen)
  64. continue;
  65. /* found the next entry */
  66. ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
  67. /* sanity check */
  68. if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) {
  69. pr_warn("directory entry %d exceeds directory block\n",
  70. slot);
  71. continue;
  72. }
  73. /* copy filename and data in dirslot */
  74. if (!dir_emit(ctx, nameptr, namelen, inodenum, DT_UNKNOWN)) {
  75. brelse(bh);
  76. return 0;
  77. }
  78. }
  79. brelse(bh);
  80. slot = 0;
  81. block++;
  82. }
  83. ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
  84. return 0;
  85. }