/filesystems/unixfs/common/linux/linux.c

http://macfuse.googlecode.com/ · C · 49 lines · 43 code · 6 blank · 0 comment · 8 complexity · d4b75bc9f06aed0770b5f0bfec53600c MD5 · raw file

  1. #include <sys/types.h>
  2. #include <sys/uio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include "unixfs_internal.h"
  6. #include "linux.h"
  7. int
  8. sb_bread_intobh(struct super_block* sb, off_t block, struct buffer_head* bh)
  9. {
  10. if (pread(sb->s_bdev, bh->b_data, sb->s_blocksize,
  11. block * (off_t)sb->s_blocksize) != sb->s_blocksize)
  12. return EIO;
  13. return 0;
  14. }
  15. void
  16. brelse(struct buffer_head* bh)
  17. {
  18. if (bh && bh->b_flags.dynamic)
  19. free((void*)bh);
  20. }
  21. struct buffer_head*
  22. sb_getblk(struct super_block* sb, sector_t block)
  23. {
  24. struct buffer_head* bh = calloc(1, sizeof(struct buffer_head));
  25. if (!bh) {
  26. fprintf(stderr, "*** fatal error: cannot allocate buffer\n");
  27. abort();
  28. }
  29. bh->b_flags.dynamic = 1;
  30. bh->b_size = PAGE_SIZE;
  31. bh->b_blocknr = block;
  32. return bh;
  33. }
  34. struct buffer_head*
  35. sb_bread(struct super_block* sb, off_t block)
  36. {
  37. struct buffer_head* bh = sb_getblk(sb, block);
  38. if (bh && sb_bread_intobh(sb, block, bh) != 0) {
  39. brelse(bh);
  40. return NULL;
  41. }
  42. return bh;
  43. }