dir.c 2.4 KB

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