ext4_filedev.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #define _FILE_OFFSET_BITS 64
  29. #include <ext4_config.h>
  30. #include <ext4_blockdev.h>
  31. #include <ext4_errno.h>
  32. #include <stdio.h>
  33. #include <stdbool.h>
  34. #include <string.h>
  35. #include <fcntl.h>
  36. /**@brief Default filename.*/
  37. static const char *fname = "ext2";
  38. /**@brief Image block size.*/
  39. #define EXT4_FILEDEV_BSIZE 512
  40. /**@brief Image file descriptor.*/
  41. static FILE *dev_file;
  42. #define DROP_LINUXCACHE_BUFFERS 0
  43. /**********************BLOCKDEV INTERFACE**************************************/
  44. static int filedev_open(struct ext4_blockdev *bdev);
  45. static int filedev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  46. uint32_t blk_cnt);
  47. static int filedev_bwrite(struct ext4_blockdev *bdev, const void *buf,
  48. uint64_t blk_id, uint32_t blk_cnt);
  49. static int filedev_close(struct ext4_blockdev *bdev);
  50. /******************************************************************************/
  51. EXT4_BLOCKDEV_STATIC_INSTANCE(
  52. _filedev,
  53. EXT4_FILEDEV_BSIZE,
  54. 0,
  55. filedev_open,
  56. filedev_bread,
  57. filedev_bwrite,
  58. filedev_close
  59. );
  60. /******************************************************************************/
  61. static int filedev_open(struct ext4_blockdev *bdev)
  62. {
  63. dev_file = fopen(fname, "r+b");
  64. if(!dev_file)
  65. return EIO;
  66. /*No buffering at file.*/
  67. setbuf(dev_file, 0);
  68. if(fseek(dev_file, 0, SEEK_END))
  69. return EFAULT;
  70. _filedev.ph_bcnt = ftell(dev_file) / _filedev.ph_bsize;
  71. return EOK;
  72. }
  73. /******************************************************************************/
  74. static int filedev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  75. uint32_t blk_cnt)
  76. {
  77. if(fseek(dev_file, blk_id * bdev->ph_bsize, SEEK_SET))
  78. return EIO;
  79. if(!fread(buf, bdev->ph_bsize * blk_cnt, 1, dev_file))
  80. return EIO;
  81. return EOK;
  82. }
  83. static void drop_cache(void)
  84. {
  85. #if defined(__linux__) && DROP_LINUXCACHE_BUFFERS
  86. int fd;
  87. char* data = "3";
  88. sync();
  89. fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
  90. write(fd, data, sizeof(char));
  91. close(fd);
  92. #endif
  93. }
  94. /******************************************************************************/
  95. static int filedev_bwrite(struct ext4_blockdev *bdev, const void *buf,
  96. uint64_t blk_id, uint32_t blk_cnt)
  97. {
  98. if(fseek(dev_file, blk_id * bdev->ph_bsize, SEEK_SET))
  99. return EIO;
  100. if(!fwrite(buf, bdev->ph_bsize * blk_cnt, 1, dev_file))
  101. return EIO;
  102. drop_cache();
  103. return EOK;
  104. }
  105. /******************************************************************************/
  106. static int filedev_close(struct ext4_blockdev *bdev)
  107. {
  108. fclose(dev_file);
  109. return EOK;
  110. }
  111. /******************************************************************************/
  112. struct ext4_blockdev* ext4_filedev_get(void)
  113. {
  114. return &_filedev;
  115. }
  116. /******************************************************************************/
  117. void ext4_filedev_filename(const char *n)
  118. {
  119. fname = n;
  120. }
  121. /******************************************************************************/