dev.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. int reiserfs_set_blk_dev(block_dev_desc_t *rbdd, int part)
  26. {
  27. reiserfs_block_dev_desc = rbdd;
  28. if (part == 0) {
  29. /* disk doesn't use partition table */
  30. part_info.start = 0;
  31. part_info.size = rbdd->lba;
  32. part_info.blksz = rbdd->blksz;
  33. } else {
  34. if (get_partition_info (reiserfs_block_dev_desc, part, &part_info)) {
  35. return 0;
  36. }
  37. }
  38. return (part_info.size);
  39. }
  40. int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
  41. {
  42. char sec_buf[SECTOR_SIZE];
  43. unsigned block_len;
  44. /*
  45. unsigned len = byte_len;
  46. u8 *start = buf;
  47. */
  48. /*
  49. * Check partition boundaries
  50. */
  51. if (sector < 0
  52. || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS))
  53. >= part_info.size)) {
  54. /* errnum = ERR_OUTSIDE_PART; */
  55. printf (" ** reiserfs_devread() read outside partition\n");
  56. return 0;
  57. }
  58. /*
  59. * Get the read to the beginning of a partition.
  60. */
  61. sector += byte_offset >> SECTOR_BITS;
  62. byte_offset &= SECTOR_SIZE - 1;
  63. #if defined(DEBUG)
  64. printf (" <%d, %d, %d> ", sector, byte_offset, byte_len);
  65. #endif
  66. if (reiserfs_block_dev_desc == NULL)
  67. return 0;
  68. if (byte_offset != 0) {
  69. /* read first part which isn't aligned with start of sector */
  70. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
  71. part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) {
  72. printf (" ** reiserfs_devread() read error\n");
  73. return 0;
  74. }
  75. memcpy(buf, sec_buf+byte_offset, min(SECTOR_SIZE-byte_offset, byte_len));
  76. buf+=min(SECTOR_SIZE-byte_offset, byte_len);
  77. byte_len-=min(SECTOR_SIZE-byte_offset, byte_len);
  78. sector++;
  79. }
  80. /* read sector aligned part */
  81. block_len = byte_len & ~(SECTOR_SIZE-1);
  82. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
  83. part_info.start+sector, block_len/SECTOR_SIZE, (unsigned long *)buf) !=
  84. block_len/SECTOR_SIZE) {
  85. printf (" ** reiserfs_devread() read error - block\n");
  86. return 0;
  87. }
  88. buf+=block_len;
  89. byte_len-=block_len;
  90. sector+= block_len/SECTOR_SIZE;
  91. if ( byte_len != 0 ) {
  92. /* read rest of data which are not in whole sector */
  93. if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
  94. part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) {
  95. printf (" ** reiserfs_devread() read error - last part\n");
  96. return 0;
  97. }
  98. memcpy(buf, sec_buf, byte_len);
  99. }
  100. return 1;
  101. }