dev.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. *
  3. * based on code of fs/reiserfs/dev.c by
  4. *
  5. * (C) Copyright 2003 - 2004
  6. * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <common.h>
  23. #include <config.h>
  24. #include <zfs_common.h>
  25. static block_dev_desc_t *zfs_block_dev_desc;
  26. static disk_partition_t *part_info;
  27. void zfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
  28. {
  29. zfs_block_dev_desc = rbdd;
  30. part_info = info;
  31. }
  32. /* err */
  33. int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
  34. {
  35. short sec_buffer[SECTOR_SIZE/sizeof(short)];
  36. char *sec_buf = (char *)sec_buffer;
  37. unsigned block_len;
  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(" ** zfs_devread() read outside partition sector %d\n", sector);
  46. return 1;
  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. debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  54. if (zfs_block_dev_desc == NULL) {
  55. printf("** Invalid Block Device Descriptor (NULL)\n");
  56. return 1;
  57. }
  58. if (byte_offset != 0) {
  59. /* read first part which isn't aligned with start of sector */
  60. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  61. part_info->start + sector, 1,
  62. (unsigned long *)sec_buf) != 1) {
  63. printf(" ** zfs_devread() read error **\n");
  64. return 1;
  65. }
  66. memcpy(buf, sec_buf + byte_offset,
  67. min(SECTOR_SIZE - byte_offset, byte_len));
  68. buf += min(SECTOR_SIZE - byte_offset, byte_len);
  69. byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
  70. sector++;
  71. }
  72. if (byte_len == 0)
  73. return 0;
  74. /* read sector aligned part */
  75. block_len = byte_len & ~(SECTOR_SIZE - 1);
  76. if (block_len == 0) {
  77. u8 p[SECTOR_SIZE];
  78. block_len = SECTOR_SIZE;
  79. zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  80. part_info->start + sector,
  81. 1, (unsigned long *)p);
  82. memcpy(buf, p, byte_len);
  83. return 0;
  84. }
  85. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  86. part_info->start + sector, block_len / SECTOR_SIZE,
  87. (unsigned long *) buf) != block_len / SECTOR_SIZE) {
  88. printf(" ** zfs_devread() read error - block\n");
  89. return 1;
  90. }
  91. block_len = byte_len & ~(SECTOR_SIZE - 1);
  92. buf += block_len;
  93. byte_len -= block_len;
  94. sector += block_len / SECTOR_SIZE;
  95. if (byte_len != 0) {
  96. /* read rest of data which are not in whole sector */
  97. if (zfs_block_dev_desc->
  98. block_read(zfs_block_dev_desc->dev,
  99. part_info->start + sector, 1,
  100. (unsigned long *) sec_buf) != 1) {
  101. printf(" ** zfs_devread() read error - last part\n");
  102. return 1;
  103. }
  104. memcpy(buf, sec_buf, byte_len);
  105. }
  106. return 0;
  107. }