dev.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * (C) Copyright 2003 - 2004
  3. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <common.h>
  20. #include <config.h>
  21. #include <reiserfs.h>
  22. #include "reiserfs_private.h"
  23. static block_dev_desc_t *reiserfs_block_dev_desc;
  24. static disk_partition_t *part_info;
  25. void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
  26. {
  27. reiserfs_block_dev_desc = rbdd;
  28. part_info = info;
  29. }
  30. int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
  31. {
  32. char sec_buf[SECTOR_SIZE];
  33. unsigned block_len;
  34. /*
  35. unsigned len = byte_len;
  36. u8 *start = buf;
  37. */
  38. /*
  39. * Check partition boundaries
  40. */
  41. if (sector < 0
  42. || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS))
  43. >= part_info->size)) {
  44. /* errnum = ERR_OUTSIDE_PART; */
  45. printf (" ** reiserfs_devread() read outside partition\n");
  46. return 0;
  47. }
  48. /*
  49. * Get the read to the beginning of a partition.
  50. */
  51. sector += byte_offset >> SECTOR_BITS;
  52. byte_offset &= SECTOR_SIZE - 1;
  53. #if defined(DEBUG)
  54. printf (" <%d, %d, %d> ", sector, byte_offset, byte_len);
  55. #endif
  56. if (reiserfs_block_dev_desc == NULL)
  57. return 0;
  58. if (byte_offset != 0) {
  59. /* read first part which isn't aligned with start of sector */
  60. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
  61. part_info->start + sector, 1,
  62. (unsigned long *)sec_buf) != 1) {
  63. printf (" ** reiserfs_devread() read error\n");
  64. return 0;
  65. }
  66. memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len));
  67. buf+=min(SECTOR_SIZE-byte_offset, byte_len);
  68. byte_len-=min(SECTOR_SIZE-byte_offset, byte_len);
  69. sector++;
  70. }
  71. /* read sector aligned part */
  72. block_len = byte_len & ~(SECTOR_SIZE-1);
  73. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
  74. part_info->start + sector, block_len/SECTOR_SIZE,
  75. (unsigned long *)buf) != block_len/SECTOR_SIZE) {
  76. printf (" ** reiserfs_devread() read error - block\n");
  77. return 0;
  78. }
  79. buf+=block_len;
  80. byte_len-=block_len;
  81. sector+= block_len/SECTOR_SIZE;
  82. if ( byte_len != 0 ) {
  83. /* read rest of data which are not in whole sector */
  84. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
  85. part_info->start + sector, 1,
  86. (unsigned long *)sec_buf) != 1) {
  87. printf (" ** reiserfs_devread() read error - last part\n");
  88. return 0;
  89. }
  90. memcpy(buf, sec_buf, byte_len);
  91. }
  92. return 1;
  93. }