dev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. int zfs_set_blk_dev(block_dev_desc_t *rbdd, int part)
  28. {
  29. zfs_block_dev_desc = rbdd;
  30. if (part == 0) {
  31. /* disk doesn't use partition table */
  32. part_info.start = 0;
  33. part_info.size = rbdd->lba;
  34. part_info.blksz = rbdd->blksz;
  35. } else {
  36. if (get_partition_info(zfs_block_dev_desc, part, &part_info))
  37. return 0;
  38. }
  39. return part_info.size;
  40. }
  41. /* err */
  42. int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
  43. {
  44. short sec_buffer[SECTOR_SIZE/sizeof(short)];
  45. char *sec_buf = (char *)sec_buffer;
  46. unsigned block_len;
  47. /*
  48. * Check partition boundaries
  49. */
  50. if ((sector < 0) ||
  51. ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
  52. part_info.size)) {
  53. /* errnum = ERR_OUTSIDE_PART; */
  54. printf(" ** zfs_devread() read outside partition sector %d\n", sector);
  55. return 1;
  56. }
  57. /*
  58. * Get the read to the beginning of a partition.
  59. */
  60. sector += byte_offset >> SECTOR_BITS;
  61. byte_offset &= SECTOR_SIZE - 1;
  62. debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
  63. if (zfs_block_dev_desc == NULL) {
  64. printf("** Invalid Block Device Descriptor (NULL)\n");
  65. return 1;
  66. }
  67. if (byte_offset != 0) {
  68. /* read first part which isn't aligned with start of sector */
  69. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  70. part_info.start + sector, 1,
  71. (unsigned long *) sec_buf) != 1) {
  72. printf(" ** zfs_devread() read error **\n");
  73. return 1;
  74. }
  75. memcpy(buf, sec_buf + byte_offset,
  76. 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. if (byte_len == 0)
  82. return 0;
  83. /* read sector aligned part */
  84. block_len = byte_len & ~(SECTOR_SIZE - 1);
  85. if (block_len == 0) {
  86. u8 p[SECTOR_SIZE];
  87. block_len = SECTOR_SIZE;
  88. zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  89. part_info.start + sector,
  90. 1, (unsigned long *)p);
  91. memcpy(buf, p, byte_len);
  92. return 0;
  93. }
  94. if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
  95. part_info.start + sector,
  96. block_len / SECTOR_SIZE,
  97. (unsigned long *) buf) !=
  98. block_len / SECTOR_SIZE) {
  99. printf(" ** zfs_devread() read error - block\n");
  100. return 1;
  101. }
  102. block_len = byte_len & ~(SECTOR_SIZE - 1);
  103. buf += block_len;
  104. byte_len -= block_len;
  105. sector += block_len / SECTOR_SIZE;
  106. if (byte_len != 0) {
  107. /* read rest of data which are not in whole sector */
  108. if (zfs_block_dev_desc->
  109. block_read(zfs_block_dev_desc->dev,
  110. part_info.start + sector, 1,
  111. (unsigned long *) sec_buf) != 1) {
  112. printf(" ** zfs_devread() read error - last part\n");
  113. return 1;
  114. }
  115. memcpy(buf, sec_buf, byte_len);
  116. }
  117. return 0;
  118. }