dir.c 2.3 KB

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