dir.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. printk(KERN_WARNING "EFS: WARNING: readdir(): directory size not a multiple of EFS_DIRBSIZE\n");
  24. /* work out where this entry can be found */
  25. block = ctx->pos >> EFS_DIRBSIZE_BITS;
  26. /* each block contains at most 256 slots */
  27. slot = ctx->pos & 0xff;
  28. /* look at all blocks */
  29. while (block < inode->i_blocks) {
  30. struct efs_dir *dirblock;
  31. struct buffer_head *bh;
  32. /* read the dir block */
  33. bh = sb_bread(inode->i_sb, efs_bmap(inode, block));
  34. if (!bh) {
  35. printk(KERN_ERR "EFS: readdir(): failed to read dir block %d\n", block);
  36. break;
  37. }
  38. dirblock = (struct efs_dir *) bh->b_data;
  39. if (be16_to_cpu(dirblock->magic) != EFS_DIRBLK_MAGIC) {
  40. printk(KERN_ERR "EFS: readdir(): invalid directory block\n");
  41. brelse(bh);
  42. break;
  43. }
  44. for (; slot < dirblock->slots; slot++) {
  45. struct efs_dentry *dirslot;
  46. efs_ino_t inodenum;
  47. const char *nameptr;
  48. int namelen;
  49. if (dirblock->space[slot] == 0)
  50. continue;
  51. dirslot = (struct efs_dentry *) (((char *) bh->b_data) + EFS_SLOTAT(dirblock, slot));
  52. inodenum = be32_to_cpu(dirslot->inode);
  53. namelen = dirslot->namelen;
  54. nameptr = dirslot->name;
  55. #ifdef DEBUG
  56. printk(KERN_DEBUG "EFS: readdir(): block %d slot %d/%d: inode %u, name \"%s\", namelen %u\n", block, slot, dirblock->slots-1, inodenum, nameptr, namelen);
  57. #endif
  58. if (!namelen)
  59. continue;
  60. /* found the next entry */
  61. ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
  62. /* sanity check */
  63. if (nameptr - (char *) dirblock + namelen > EFS_DIRBSIZE) {
  64. printk(KERN_WARNING "EFS: directory entry %d exceeds directory block\n", slot);
  65. continue;
  66. }
  67. /* copy filename and data in dirslot */
  68. if (!dir_emit(ctx, nameptr, namelen, inodenum, DT_UNKNOWN)) {
  69. brelse(bh);
  70. return 0;
  71. }
  72. }
  73. brelse(bh);
  74. slot = 0;
  75. block++;
  76. }
  77. ctx->pos = (block << EFS_DIRBSIZE_BITS) | slot;
  78. return 0;
  79. }