dev.c 3.3 KB

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