ext4_filedev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include <ext4_config.h>
  29. #include <ext4_blockdev.h>
  30. #include <ext4_errno.h>
  31. #include <stdio.h>
  32. #include <stdbool.h>
  33. #include <string.h>
  34. #include <fcntl.h>
  35. /**@brief Default filename.*/
  36. static const char *fname = "ext2";
  37. /**@brief Image block size.*/
  38. #define EXT4_FILEDEV_BSIZE 512
  39. /**@brief Image file descriptor.*/
  40. static FILE *dev_file;
  41. #define DROP_LINUXCACHE_BUFFERS 1
  42. /**********************BLOCKDEV INTERFACE**************************************/
  43. static int filedev_open(struct ext4_blockdev *bdev);
  44. static int filedev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  45. uint32_t blk_cnt);
  46. static int filedev_bwrite(struct ext4_blockdev *bdev, const void *buf,
  47. uint64_t blk_id, uint32_t blk_cnt);
  48. static int filedev_close(struct ext4_blockdev *bdev);
  49. /******************************************************************************/
  50. EXT4_BLOCKDEV_STATIC_INSTANCE(
  51. _filedev,
  52. EXT4_FILEDEV_BSIZE,
  53. 0,
  54. filedev_open,
  55. filedev_bread,
  56. filedev_bwrite,
  57. filedev_close
  58. );
  59. /******************************************************************************/
  60. EXT4_BCACHE_STATIC_INSTANCE(__cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024);
  61. /******************************************************************************/
  62. static int filedev_open(struct ext4_blockdev *bdev)
  63. {
  64. dev_file = fopen(fname, "r+b");
  65. if(!dev_file)
  66. return EIO;
  67. /*No buffering at file.*/
  68. setbuf(dev_file, 0);
  69. if(fseek(dev_file, 0, SEEK_END))
  70. return EFAULT;
  71. _filedev.ph_bcnt = ftell(dev_file) / _filedev.ph_bsize;
  72. return EOK;
  73. }
  74. /******************************************************************************/
  75. static int filedev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  76. uint32_t blk_cnt)
  77. {
  78. if(fseek(dev_file, blk_id * bdev->ph_bsize, SEEK_SET))
  79. return EIO;
  80. if(!fread(buf, bdev->ph_bsize * blk_cnt, 1, dev_file))
  81. return EIO;
  82. return EOK;
  83. }
  84. static void drop_cache(void)
  85. {
  86. #if defined(__linux__) && DROP_LINUXCACHE_BUFFERS
  87. int fd;
  88. char* data = "3";
  89. sync();
  90. fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
  91. write(fd, data, sizeof(char));
  92. close(fd);
  93. #endif
  94. }
  95. /******************************************************************************/
  96. static int filedev_bwrite(struct ext4_blockdev *bdev, const void *buf,
  97. uint64_t blk_id, uint32_t blk_cnt)
  98. {
  99. if(fseek(dev_file, blk_id * bdev->ph_bsize, SEEK_SET))
  100. return EIO;
  101. if(!fwrite(buf, bdev->ph_bsize * blk_cnt, 1, dev_file))
  102. return EIO;
  103. drop_cache();
  104. return EOK;
  105. }
  106. /******************************************************************************/
  107. static int filedev_close(struct ext4_blockdev *bdev)
  108. {
  109. fclose(dev_file);
  110. return EOK;
  111. }
  112. /******************************************************************************/
  113. struct ext4_bcache* ext4_filecache_get(void)
  114. {
  115. return &__cache;
  116. }
  117. /******************************************************************************/
  118. struct ext4_blockdev* ext4_filedev_get(void)
  119. {
  120. return &_filedev;
  121. }
  122. /******************************************************************************/
  123. void ext4_filedev_filename(const char *n)
  124. {
  125. fname = n;
  126. }
  127. /******************************************************************************/