sfs_io.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <kern/errno.h>
  31. #include <lib.h>
  32. #include <uio.h>
  33. #include <vfs.h>
  34. #include <device.h>
  35. #include <sfs.h>
  36. ////////////////////////////////////////////////////////////
  37. //
  38. // Basic block-level I/O routines
  39. //
  40. // Note: sfs_rblock is used to read the superblock
  41. // early in mount, before sfs is fully (or even mostly)
  42. // initialized, and so may not use anything from sfs
  43. // except sfs_device.
  44. int
  45. sfs_rwblock(struct sfs_fs *sfs, struct uio *uio)
  46. {
  47. int result;
  48. int tries=0;
  49. KASSERT(vfs_biglock_do_i_hold());
  50. DEBUG(DB_SFS, "sfs: %s %llu\n",
  51. uio->uio_rw == UIO_READ ? "read" : "write",
  52. uio->uio_offset / SFS_BLOCKSIZE);
  53. retry:
  54. result = sfs->sfs_device->d_io(sfs->sfs_device, uio);
  55. if (result == EINVAL) {
  56. /*
  57. * This means the sector we requested was out of range,
  58. * or the seek address we gave wasn't sector-aligned,
  59. * or a couple of other things that are our fault.
  60. */
  61. panic("sfs: d_io returned EINVAL\n");
  62. }
  63. if (result == EIO) {
  64. if (tries == 0) {
  65. tries++;
  66. kprintf("sfs: block %llu I/O error, retrying\n",
  67. uio->uio_offset / SFS_BLOCKSIZE);
  68. goto retry;
  69. }
  70. else if (tries < 10) {
  71. tries++;
  72. goto retry;
  73. }
  74. else {
  75. kprintf("sfs: block %llu I/O error, giving up after "
  76. "%d retries\n",
  77. uio->uio_offset / SFS_BLOCKSIZE, tries);
  78. }
  79. }
  80. return result;
  81. }
  82. int
  83. sfs_rblock(struct sfs_fs *sfs, void *data, uint32_t block)
  84. {
  85. struct iovec iov;
  86. struct uio ku;
  87. SFSUIO(&iov, &ku, data, block, UIO_READ);
  88. return sfs_rwblock(sfs, &ku);
  89. }
  90. int
  91. sfs_wblock(struct sfs_fs *sfs, void *data, uint32_t block)
  92. {
  93. struct iovec iov;
  94. struct uio ku;
  95. SFSUIO(&iov, &ku, data, block, UIO_WRITE);
  96. return sfs_rwblock(sfs, &ku);
  97. }