softraid_raid0.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* $OpenBSD: softraid_raid0.c,v 1.50 2015/07/21 03:30:51 krw Exp $ */
  2. /*
  3. * Copyright (c) 2008 Marco Peereboom <marco@peereboom.us>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "bio.h"
  18. #include <sys/param.h>
  19. #include <sys/systm.h>
  20. #include <sys/buf.h>
  21. #include <sys/device.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/malloc.h>
  24. #include <sys/kernel.h>
  25. #include <sys/disk.h>
  26. #include <sys/rwlock.h>
  27. #include <sys/queue.h>
  28. #include <sys/fcntl.h>
  29. #include <sys/mount.h>
  30. #include <sys/sensors.h>
  31. #include <sys/stat.h>
  32. #include <sys/conf.h>
  33. #include <sys/uio.h>
  34. #include <scsi/scsi_all.h>
  35. #include <scsi/scsiconf.h>
  36. #include <scsi/scsi_disk.h>
  37. #include <dev/softraidvar.h>
  38. /* RAID 0 functions. */
  39. int sr_raid0_create(struct sr_discipline *, struct bioc_createraid *,
  40. int, int64_t);
  41. int sr_raid0_assemble(struct sr_discipline *, struct bioc_createraid *,
  42. int, void *);
  43. int sr_raid0_init(struct sr_discipline *);
  44. int sr_raid0_rw(struct sr_workunit *);
  45. /* Discipline initialisation. */
  46. void
  47. sr_raid0_discipline_init(struct sr_discipline *sd)
  48. {
  49. /* Fill out discipline members. */
  50. sd->sd_type = SR_MD_RAID0;
  51. strlcpy(sd->sd_name, "RAID 0", sizeof(sd->sd_name));
  52. sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE;
  53. sd->sd_max_wu = SR_RAID0_NOWU;
  54. /* Setup discipline specific function pointers. */
  55. sd->sd_assemble = sr_raid0_assemble;
  56. sd->sd_create = sr_raid0_create;
  57. sd->sd_scsi_rw = sr_raid0_rw;
  58. }
  59. int
  60. sr_raid0_create(struct sr_discipline *sd, struct bioc_createraid *bc,
  61. int no_chunk, int64_t coerced_size)
  62. {
  63. if (no_chunk < 2) {
  64. sr_error(sd->sd_sc, "%s requires two or more chunks",
  65. sd->sd_name);
  66. return EINVAL;
  67. }
  68. /*
  69. * XXX add variable strip size later even though MAXPHYS is really
  70. * the clever value, users like to tinker with that type of stuff.
  71. */
  72. sd->sd_meta->ssdi.ssd_strip_size = MAXPHYS;
  73. sd->sd_meta->ssdi.ssd_size = (coerced_size &
  74. ~(((u_int64_t)sd->sd_meta->ssdi.ssd_strip_size >>
  75. DEV_BSHIFT) - 1)) * no_chunk;
  76. return sr_raid0_init(sd);
  77. }
  78. int
  79. sr_raid0_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
  80. int no_chunks, void *data)
  81. {
  82. return sr_raid0_init(sd);
  83. }
  84. int
  85. sr_raid0_init(struct sr_discipline *sd)
  86. {
  87. /* Initialise runtime values. */
  88. sd->mds.mdd_raid0.sr0_strip_bits =
  89. sr_validate_stripsize(sd->sd_meta->ssdi.ssd_strip_size);
  90. if (sd->mds.mdd_raid0.sr0_strip_bits == -1) {
  91. sr_error(sd->sd_sc, "invalid strip size", sd->sd_name);
  92. return EINVAL;
  93. }
  94. sd->sd_max_ccb_per_wu =
  95. (MAXPHYS / sd->sd_meta->ssdi.ssd_strip_size + 1) *
  96. SR_RAID0_NOWU * sd->sd_meta->ssdi.ssd_chunk_no;
  97. return 0;
  98. }
  99. int
  100. sr_raid0_rw(struct sr_workunit *wu)
  101. {
  102. struct sr_discipline *sd = wu->swu_dis;
  103. struct scsi_xfer *xs = wu->swu_xs;
  104. struct sr_ccb *ccb;
  105. struct sr_chunk *scp;
  106. daddr_t blkno;
  107. int64_t chunkoffs, lbaoffs, offset, stripoffs;
  108. int64_t strip_bits, strip_no, strip_size;
  109. int64_t chunk, no_chunk;
  110. int64_t length, leftover;
  111. u_int8_t *data;
  112. /* blkno and scsi error will be handled by sr_validate_io */
  113. if (sr_validate_io(wu, &blkno, "sr_raid0_rw"))
  114. goto bad;
  115. strip_size = sd->sd_meta->ssdi.ssd_strip_size;
  116. strip_bits = sd->mds.mdd_raid0.sr0_strip_bits;
  117. no_chunk = sd->sd_meta->ssdi.ssd_chunk_no;
  118. DNPRINTF(SR_D_DIS, "%s: %s: front end io: blkno %lld size %d\n",
  119. DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname,
  120. (long long)blkno, xs->datalen);
  121. /* all offs are in bytes */
  122. lbaoffs = blkno << DEV_BSHIFT;
  123. strip_no = lbaoffs >> strip_bits;
  124. chunk = strip_no % no_chunk;
  125. stripoffs = lbaoffs & (strip_size - 1);
  126. chunkoffs = (strip_no / no_chunk) << strip_bits;
  127. offset = chunkoffs + stripoffs;
  128. length = MIN(xs->datalen, strip_size - stripoffs);
  129. leftover = xs->datalen;
  130. data = xs->data;
  131. for (;;) {
  132. /* make sure chunk is online */
  133. scp = sd->sd_vol.sv_chunks[chunk];
  134. if (scp->src_meta.scm_status != BIOC_SDONLINE)
  135. goto bad;
  136. DNPRINTF(SR_D_DIS, "%s: %s %s io lbaoffs %lld "
  137. "strip_no %lld chunk %lld stripoffs %lld "
  138. "chunkoffs %lld offset %lld length %lld "
  139. "leftover %lld data %p\n",
  140. DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, sd->sd_name,
  141. lbaoffs, strip_no, chunk, stripoffs, chunkoffs, offset,
  142. length, leftover, data);
  143. blkno = offset >> DEV_BSHIFT;
  144. ccb = sr_ccb_rw(sd, chunk, blkno, length, data, xs->flags, 0);
  145. if (!ccb) {
  146. /* should never happen but handle more gracefully */
  147. printf("%s: %s: too many ccbs queued\n",
  148. DEVNAME(sd->sd_sc),
  149. sd->sd_meta->ssd_devname);
  150. goto bad;
  151. }
  152. sr_wu_enqueue_ccb(wu, ccb);
  153. leftover -= length;
  154. if (leftover == 0)
  155. break;
  156. data += length;
  157. if (++chunk > no_chunk - 1) {
  158. chunk = 0;
  159. offset += length;
  160. } else if (wu->swu_io_count == 1)
  161. offset -= stripoffs;
  162. length = MIN(leftover,strip_size);
  163. }
  164. sr_schedule_wu(wu);
  165. return (0);
  166. bad:
  167. /* wu is unwound by sr_wu_put */
  168. return (1);
  169. }