devio.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "iotrack.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. int readonly;
  15. static int
  16. deverror(char *name, Xfs *xf, int32_t addr, int32_t n, int32_t nret)
  17. {
  18. errno = Eio;
  19. if(nret < 0){
  20. chat("%s errstr=\"%r\"...", name);
  21. close(xf->dev);
  22. xf->dev = -1;
  23. return -1;
  24. }
  25. fprint(2, "dev %d sector %ld, %s: %ld, should be %ld\n", xf->dev, addr, name, nret, n);
  26. return -1;
  27. }
  28. int
  29. devread(Xfs *xf, int32_t addr, void *buf, int32_t n)
  30. {
  31. int32_t nread;
  32. if(xf->dev < 0)
  33. return -1;
  34. nread = pread(xf->dev, buf, n, xf->offset+(int64_t)addr*Sectorsize);
  35. if (nread == n)
  36. return 0;
  37. return deverror("read", xf, addr, n, nread);
  38. }
  39. int
  40. devwrite(Xfs *xf, int32_t addr, void *buf, int32_t n)
  41. {
  42. int32_t nwrite;
  43. if(xf->omode==OREAD)
  44. return -1;
  45. if(xf->dev < 0)
  46. return -1;
  47. nwrite = pwrite(xf->dev, buf, n, xf->offset+(int64_t)addr*Sectorsize);
  48. if (nwrite == n)
  49. return 0;
  50. return deverror("write", xf, addr, n, nwrite);
  51. }
  52. int
  53. devcheck(Xfs *xf)
  54. {
  55. char buf[Sectorsize];
  56. if(xf->dev < 0)
  57. return -1;
  58. if(pread(xf->dev, buf, Sectorsize, 0) != Sectorsize){
  59. close(xf->dev);
  60. xf->dev = -1;
  61. return -1;
  62. }
  63. return 0;
  64. }