uio.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef _UIO_H_
  30. #define _UIO_H_
  31. /*
  32. * Like BSD uio, but simplified a bit. (In BSD, there can be more than one
  33. * iovec in a uio.)
  34. *
  35. * struct iovec is in <kern/iovec.h>.
  36. */
  37. #include <kern/iovec.h>
  38. /* Direction. */
  39. enum uio_rw {
  40. UIO_READ, /* From kernel to uio_seg */
  41. UIO_WRITE, /* From uio_seg to kernel */
  42. };
  43. /* Source/destination. */
  44. enum uio_seg {
  45. UIO_USERISPACE, /* User process code. */
  46. UIO_USERSPACE, /* User process data. */
  47. UIO_SYSSPACE, /* Kernel. */
  48. };
  49. struct uio {
  50. struct iovec *uio_iov; /* Data blocks */
  51. unsigned uio_iovcnt; /* Number of iovecs */
  52. off_t uio_offset; /* Desired offset into object */
  53. size_t uio_resid; /* Remaining amt of data to xfer */
  54. enum uio_seg uio_segflg; /* What kind of pointer we have */
  55. enum uio_rw uio_rw; /* Whether op is a read or write */
  56. struct addrspace *uio_space; /* Address space for user pointer */
  57. };
  58. /*
  59. * Copy data from a kernel buffer to a data region defined by a uio struct,
  60. * updating the uio struct's offset and resid fields. May alter the iovec
  61. * fields as well.
  62. *
  63. * Before calling this, you should
  64. * (1) set up uio_iov to point to the buffer(s) you want to transfer
  65. * to, and set uio_iovcnt to the number of such buffers;
  66. * (2) initialize uio_offset as desired;
  67. * (3) initialize uio_resid to the total amount of data that can be
  68. * transferred through this uio;
  69. * (4) set up uio_seg and uio_rw correctly;
  70. * (5) if uio_seg is UIO_SYSSPACE, set uio_space to NULL; otherwise,
  71. * initialize uio_space to the address space in which the buffer
  72. * should be found.
  73. *
  74. * After calling,
  75. * (1) the contents of uio_iov and uio_iovcnt may be altered and
  76. * should not be interpreted;
  77. * (2) uio_offset will have been incremented by the amount transferred;
  78. * (3) uio_resid will have been decremented by the amount transferred;
  79. * (4) uio_segflg, uio_rw, and uio_space will be unchanged.
  80. *
  81. * uiomove() may be called repeatedly on the same uio to transfer
  82. * additional data until the available buffer space the uio refers to
  83. * is exhausted.
  84. *
  85. * Note that the actual value of uio_offset is not interpreted. It is
  86. * provided to allow for easier file seek pointer management.
  87. *
  88. * When uiomove is called, the address space presently in context must
  89. * be the same as the one recorded in uio_space. This is an important
  90. * sanity check if I/O has been queued.
  91. */
  92. int uiomove(void *kbuffer, size_t len, struct uio *uio);
  93. /*
  94. * Like uiomove, but sends zeros.
  95. */
  96. int uiomovezeros(size_t len, struct uio *uio);
  97. /*
  98. * Initialize a uio suitable for I/O from a kernel buffer.
  99. *
  100. * Usage example;
  101. * char buf[128];
  102. * struct iovec iov;
  103. * struct uio myuio;
  104. *
  105. * uio_kinit(&iov, &myuio, buf, sizeof(buf), 0, UIO_READ);
  106. * result = VOP_READ(vn, &myuio);
  107. * ...
  108. */
  109. void uio_kinit(struct iovec *, struct uio *,
  110. void *kbuf, size_t len, off_t pos, enum uio_rw rw);
  111. #endif /* _UIO_H_ */