geom_ccd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*-
  2. * SPDX-License-Identifier: (BSD-2-Clause AND BSD-3-Clause)
  3. *
  4. * Copyright (c) 2003 Poul-Henning Kamp.
  5. * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
  6. * All rights reserved.
  7. *
  8. * This code is derived from software contributed to The NetBSD Foundation
  9. * by Jason R. Thorpe.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * $NetBSD: ccd.c,v 1.22 1995/12/08 19:13:26 thorpej Exp $
  33. */
  34. /*-
  35. * Copyright (c) 1988 University of Utah.
  36. * Copyright (c) 1990, 1993
  37. * The Regents of the University of California. All rights reserved.
  38. *
  39. * This code is derived from software contributed to Berkeley by
  40. * the Systems Programming Group of the University of Utah Computer
  41. * Science Department.
  42. *
  43. * Redistribution and use in source and binary forms, with or without
  44. * modification, are permitted provided that the following conditions
  45. * are met:
  46. * 1. Redistributions of source code must retain the above copyright
  47. * notice, this list of conditions and the following disclaimer.
  48. * 2. Redistributions in binary form must reproduce the above copyright
  49. * notice, this list of conditions and the following disclaimer in the
  50. * documentation and/or other materials provided with the distribution.
  51. * 3. Neither the name of the University nor the names of its contributors
  52. * may be used to endorse or promote products derived from this software
  53. * without specific prior written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  56. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  59. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  61. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  62. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  65. * SUCH DAMAGE.
  66. *
  67. * from: Utah $Hdr: cd.c 1.6 90/11/28$
  68. */
  69. /*
  70. * Dynamic configuration and disklabel support by:
  71. * Jason R. Thorpe <thorpej@nas.nasa.gov>
  72. * Numerical Aerodynamic Simulation Facility
  73. * Mail Stop 258-6
  74. * NASA Ames Research Center
  75. * Moffett Field, CA 94035
  76. */
  77. #include <sys/param.h>
  78. #include <sys/systm.h>
  79. #include <sys/kernel.h>
  80. #include <sys/module.h>
  81. #include <sys/bio.h>
  82. #include <sys/malloc.h>
  83. #include <sys/sbuf.h>
  84. #include <geom/geom.h>
  85. /*
  86. * Number of blocks to untouched in front of a component partition.
  87. * This is to avoid violating its disklabel area when it starts at the
  88. * beginning of the slice.
  89. */
  90. #if !defined(CCD_OFFSET)
  91. #define CCD_OFFSET 16
  92. #endif
  93. /* sc_flags */
  94. #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
  95. #define CCDF_MIRROR 0x04 /* use mirroring */
  96. #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
  97. #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
  98. /* Mask of user-settable ccd flags. */
  99. #define CCDF_USERMASK (CCDF_UNIFORM|CCDF_MIRROR)
  100. /*
  101. * Interleave description table.
  102. * Computed at boot time to speed irregular-interleave lookups.
  103. * The idea is that we interleave in "groups". First we interleave
  104. * evenly over all component disks up to the size of the smallest
  105. * component (the first group), then we interleave evenly over all
  106. * remaining disks up to the size of the next-smallest (second group),
  107. * and so on.
  108. *
  109. * Each table entry describes the interleave characteristics of one
  110. * of these groups. For example if a concatenated disk consisted of
  111. * three components of 5, 3, and 7 DEV_BSIZE blocks interleaved at
  112. * DEV_BSIZE (1), the table would have three entries:
  113. *
  114. * ndisk startblk startoff dev
  115. * 3 0 0 0, 1, 2
  116. * 2 9 3 0, 2
  117. * 1 13 5 2
  118. * 0 - - -
  119. *
  120. * which says that the first nine blocks (0-8) are interleaved over
  121. * 3 disks (0, 1, 2) starting at block offset 0 on any component disk,
  122. * the next 4 blocks (9-12) are interleaved over 2 disks (0, 2) starting
  123. * at component block 3, and the remaining blocks (13-14) are on disk
  124. * 2 starting at offset 5.
  125. */
  126. struct ccdiinfo {
  127. int ii_ndisk; /* # of disks range is interleaved over */
  128. daddr_t ii_startblk; /* starting scaled block # for range */
  129. daddr_t ii_startoff; /* starting component offset (block #) */
  130. int *ii_index; /* ordered list of components in range */
  131. };
  132. /*
  133. * Component info table.
  134. * Describes a single component of a concatenated disk.
  135. */
  136. struct ccdcinfo {
  137. daddr_t ci_size; /* size */
  138. struct g_provider *ci_provider; /* provider */
  139. struct g_consumer *ci_consumer; /* consumer */
  140. };
  141. /*
  142. * A concatenated disk is described by this structure.
  143. */
  144. struct ccd_s {
  145. LIST_ENTRY(ccd_s) list;
  146. int sc_unit; /* logical unit number */
  147. int sc_flags; /* flags */
  148. daddr_t sc_size; /* size of ccd */
  149. int sc_ileave; /* interleave */
  150. u_int sc_ndisks; /* number of components */
  151. struct ccdcinfo *sc_cinfo; /* component info */
  152. struct ccdiinfo *sc_itable; /* interleave table */
  153. uint32_t sc_secsize; /* # bytes per sector */
  154. int sc_pick; /* side of mirror picked */
  155. daddr_t sc_blk[2]; /* mirror localization */
  156. uint32_t sc_offset; /* actual offset used */
  157. };
  158. static g_start_t g_ccd_start;
  159. static void ccdiodone(struct bio *bp);
  160. static void ccdinterleave(struct ccd_s *);
  161. static int ccdinit(struct gctl_req *req, struct ccd_s *);
  162. static int ccdbuffer(struct bio **ret, struct ccd_s *,
  163. struct bio *, daddr_t, caddr_t, long);
  164. static void
  165. g_ccd_orphan(struct g_consumer *cp)
  166. {
  167. /*
  168. * XXX: We don't do anything here. It is not obvious
  169. * XXX: what DTRT would be, so we do what the previous
  170. * XXX: code did: ignore it and let the user cope.
  171. */
  172. }
  173. static int
  174. g_ccd_access(struct g_provider *pp, int dr, int dw, int de)
  175. {
  176. struct g_geom *gp;
  177. struct g_consumer *cp1, *cp2;
  178. int error;
  179. de += dr;
  180. de += dw;
  181. gp = pp->geom;
  182. error = ENXIO;
  183. LIST_FOREACH(cp1, &gp->consumer, consumer) {
  184. error = g_access(cp1, dr, dw, de);
  185. if (error) {
  186. LIST_FOREACH(cp2, &gp->consumer, consumer) {
  187. if (cp1 == cp2)
  188. break;
  189. g_access(cp2, -dr, -dw, -de);
  190. }
  191. break;
  192. }
  193. }
  194. return (error);
  195. }
  196. /*
  197. * Free the softc and its substructures.
  198. */
  199. static void
  200. g_ccd_freesc(struct ccd_s *sc)
  201. {
  202. struct ccdiinfo *ii;
  203. g_free(sc->sc_cinfo);
  204. if (sc->sc_itable != NULL) {
  205. for (ii = sc->sc_itable; ii->ii_ndisk > 0; ii++)
  206. g_free(ii->ii_index);
  207. g_free(sc->sc_itable);
  208. }
  209. g_free(sc);
  210. }
  211. static int
  212. ccdinit(struct gctl_req *req, struct ccd_s *cs)
  213. {
  214. struct ccdcinfo *ci;
  215. daddr_t size;
  216. int ix;
  217. daddr_t minsize;
  218. int maxsecsize;
  219. off_t mediasize;
  220. u_int sectorsize;
  221. cs->sc_size = 0;
  222. maxsecsize = 0;
  223. minsize = 0;
  224. if (cs->sc_flags & CCDF_LINUX) {
  225. cs->sc_offset = 0;
  226. cs->sc_ileave *= 2;
  227. if (cs->sc_flags & CCDF_MIRROR && cs->sc_ndisks != 2)
  228. gctl_error(req, "Mirror mode for Linux raids is "
  229. "only supported with 2 devices");
  230. } else {
  231. if (cs->sc_flags & CCDF_NO_OFFSET)
  232. cs->sc_offset = 0;
  233. else
  234. cs->sc_offset = CCD_OFFSET;
  235. }
  236. for (ix = 0; ix < cs->sc_ndisks; ix++) {
  237. ci = &cs->sc_cinfo[ix];
  238. mediasize = ci->ci_provider->mediasize;
  239. sectorsize = ci->ci_provider->sectorsize;
  240. if (sectorsize > maxsecsize)
  241. maxsecsize = sectorsize;
  242. size = mediasize / DEV_BSIZE - cs->sc_offset;
  243. /* Truncate to interleave boundary */
  244. if (cs->sc_ileave > 1)
  245. size -= size % cs->sc_ileave;
  246. if (size == 0) {
  247. gctl_error(req, "Component %s has effective size zero",
  248. ci->ci_provider->name);
  249. return(ENODEV);
  250. }
  251. if (minsize == 0 || size < minsize)
  252. minsize = size;
  253. ci->ci_size = size;
  254. cs->sc_size += size;
  255. }
  256. /*
  257. * Don't allow the interleave to be smaller than
  258. * the biggest component sector.
  259. */
  260. if ((cs->sc_ileave > 0) &&
  261. (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
  262. gctl_error(req, "Interleave to small for sector size");
  263. return(EINVAL);
  264. }
  265. /*
  266. * If uniform interleave is desired set all sizes to that of
  267. * the smallest component. This will guarantee that a single
  268. * interleave table is generated.
  269. *
  270. * Lost space must be taken into account when calculating the
  271. * overall size. Half the space is lost when CCDF_MIRROR is
  272. * specified.
  273. */
  274. if (cs->sc_flags & CCDF_UNIFORM) {
  275. for (ix = 0; ix < cs->sc_ndisks; ix++) {
  276. ci = &cs->sc_cinfo[ix];
  277. ci->ci_size = minsize;
  278. }
  279. cs->sc_size = cs->sc_ndisks * minsize;
  280. }
  281. if (cs->sc_flags & CCDF_MIRROR) {
  282. /*
  283. * Check to see if an even number of components
  284. * have been specified. The interleave must also
  285. * be non-zero in order for us to be able to
  286. * guarantee the topology.
  287. */
  288. if (cs->sc_ndisks % 2) {
  289. gctl_error(req,
  290. "Mirroring requires an even number of disks");
  291. return(EINVAL);
  292. }
  293. if (cs->sc_ileave == 0) {
  294. gctl_error(req,
  295. "An interleave must be specified when mirroring");
  296. return(EINVAL);
  297. }
  298. cs->sc_size = (cs->sc_ndisks/2) * minsize;
  299. }
  300. /*
  301. * Construct the interleave table.
  302. */
  303. ccdinterleave(cs);
  304. /*
  305. * Create pseudo-geometry based on 1MB cylinders. It's
  306. * pretty close.
  307. */
  308. cs->sc_secsize = maxsecsize;
  309. return (0);
  310. }
  311. static void
  312. ccdinterleave(struct ccd_s *cs)
  313. {
  314. struct ccdcinfo *ci, *smallci;
  315. struct ccdiinfo *ii;
  316. daddr_t bn, lbn;
  317. int ix;
  318. daddr_t size;
  319. /*
  320. * Allocate an interleave table. The worst case occurs when each
  321. * of N disks is of a different size, resulting in N interleave
  322. * tables.
  323. *
  324. * Chances are this is too big, but we don't care.
  325. */
  326. size = (cs->sc_ndisks + 1) * sizeof(struct ccdiinfo);
  327. cs->sc_itable = g_malloc(size, M_WAITOK | M_ZERO);
  328. /*
  329. * Trivial case: no interleave (actually interleave of disk size).
  330. * Each table entry represents a single component in its entirety.
  331. *
  332. * An interleave of 0 may not be used with a mirror setup.
  333. */
  334. if (cs->sc_ileave == 0) {
  335. bn = 0;
  336. ii = cs->sc_itable;
  337. for (ix = 0; ix < cs->sc_ndisks; ix++) {
  338. /* Allocate space for ii_index. */
  339. ii->ii_index = g_malloc(sizeof(int), M_WAITOK);
  340. ii->ii_ndisk = 1;
  341. ii->ii_startblk = bn;
  342. ii->ii_startoff = 0;
  343. ii->ii_index[0] = ix;
  344. bn += cs->sc_cinfo[ix].ci_size;
  345. ii++;
  346. }
  347. ii->ii_ndisk = 0;
  348. return;
  349. }
  350. /*
  351. * The following isn't fast or pretty; it doesn't have to be.
  352. */
  353. size = 0;
  354. bn = lbn = 0;
  355. for (ii = cs->sc_itable; ; ii++) {
  356. /*
  357. * Allocate space for ii_index. We might allocate more then
  358. * we use.
  359. */
  360. ii->ii_index = g_malloc((sizeof(int) * cs->sc_ndisks),
  361. M_WAITOK);
  362. /*
  363. * Locate the smallest of the remaining components
  364. */
  365. smallci = NULL;
  366. for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_ndisks];
  367. ci++) {
  368. if (ci->ci_size > size &&
  369. (smallci == NULL ||
  370. ci->ci_size < smallci->ci_size)) {
  371. smallci = ci;
  372. }
  373. }
  374. /*
  375. * Nobody left, all done
  376. */
  377. if (smallci == NULL) {
  378. ii->ii_ndisk = 0;
  379. g_free(ii->ii_index);
  380. ii->ii_index = NULL;
  381. break;
  382. }
  383. /*
  384. * Record starting logical block using an sc_ileave blocksize.
  385. */
  386. ii->ii_startblk = bn / cs->sc_ileave;
  387. /*
  388. * Record starting component block using an sc_ileave
  389. * blocksize. This value is relative to the beginning of
  390. * a component disk.
  391. */
  392. ii->ii_startoff = lbn;
  393. /*
  394. * Determine how many disks take part in this interleave
  395. * and record their indices.
  396. */
  397. ix = 0;
  398. for (ci = cs->sc_cinfo;
  399. ci < &cs->sc_cinfo[cs->sc_ndisks]; ci++) {
  400. if (ci->ci_size >= smallci->ci_size) {
  401. ii->ii_index[ix++] = ci - cs->sc_cinfo;
  402. }
  403. }
  404. ii->ii_ndisk = ix;
  405. bn += ix * (smallci->ci_size - size);
  406. lbn = smallci->ci_size / cs->sc_ileave;
  407. size = smallci->ci_size;
  408. }
  409. }
  410. static void
  411. g_ccd_start(struct bio *bp)
  412. {
  413. long bcount, rcount;
  414. struct bio *cbp[2];
  415. caddr_t addr;
  416. daddr_t bn;
  417. int err;
  418. struct ccd_s *cs;
  419. cs = bp->bio_to->geom->softc;
  420. /*
  421. * Block all GETATTR requests, we wouldn't know which of our
  422. * subdevices we should ship it off to.
  423. * XXX: this may not be the right policy.
  424. */
  425. if(bp->bio_cmd == BIO_GETATTR) {
  426. g_io_deliver(bp, EINVAL);
  427. return;
  428. }
  429. /*
  430. * Translate the partition-relative block number to an absolute.
  431. */
  432. bn = bp->bio_offset / cs->sc_secsize;
  433. /*
  434. * Allocate component buffers and fire off the requests
  435. */
  436. addr = bp->bio_data;
  437. for (bcount = bp->bio_length; bcount > 0; bcount -= rcount) {
  438. err = ccdbuffer(cbp, cs, bp, bn, addr, bcount);
  439. if (err) {
  440. bp->bio_completed += bcount;
  441. if (bp->bio_error == 0)
  442. bp->bio_error = err;
  443. if (bp->bio_completed == bp->bio_length)
  444. g_io_deliver(bp, bp->bio_error);
  445. return;
  446. }
  447. rcount = cbp[0]->bio_length;
  448. if (cs->sc_flags & CCDF_MIRROR) {
  449. /*
  450. * Mirroring. Writes go to both disks, reads are
  451. * taken from whichever disk seems most appropriate.
  452. *
  453. * We attempt to localize reads to the disk whos arm
  454. * is nearest the read request. We ignore seeks due
  455. * to writes when making this determination and we
  456. * also try to avoid hogging.
  457. */
  458. if (cbp[0]->bio_cmd != BIO_READ) {
  459. g_io_request(cbp[0], cbp[0]->bio_from);
  460. g_io_request(cbp[1], cbp[1]->bio_from);
  461. } else {
  462. int pick = cs->sc_pick;
  463. daddr_t range = cs->sc_size / 16;
  464. if (bn < cs->sc_blk[pick] - range ||
  465. bn > cs->sc_blk[pick] + range
  466. ) {
  467. cs->sc_pick = pick = 1 - pick;
  468. }
  469. cs->sc_blk[pick] = bn + btodb(rcount);
  470. g_io_request(cbp[pick], cbp[pick]->bio_from);
  471. }
  472. } else {
  473. /*
  474. * Not mirroring
  475. */
  476. g_io_request(cbp[0], cbp[0]->bio_from);
  477. }
  478. bn += btodb(rcount);
  479. addr += rcount;
  480. }
  481. }
  482. /*
  483. * Build a component buffer header.
  484. */
  485. static int
  486. ccdbuffer(struct bio **cb, struct ccd_s *cs, struct bio *bp, daddr_t bn, caddr_t addr, long bcount)
  487. {
  488. struct ccdcinfo *ci, *ci2 = NULL;
  489. struct bio *cbp;
  490. daddr_t cbn, cboff;
  491. off_t cbc;
  492. /*
  493. * Determine which component bn falls in.
  494. */
  495. cbn = bn;
  496. cboff = 0;
  497. if (cs->sc_ileave == 0) {
  498. /*
  499. * Serially concatenated and neither a mirror nor a parity
  500. * config. This is a special case.
  501. */
  502. daddr_t sblk;
  503. sblk = 0;
  504. for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
  505. sblk += ci->ci_size;
  506. cbn -= sblk;
  507. } else {
  508. struct ccdiinfo *ii;
  509. int ccdisk, off;
  510. /*
  511. * Calculate cbn, the logical superblock (sc_ileave chunks),
  512. * and cboff, a normal block offset (DEV_BSIZE chunks) relative
  513. * to cbn.
  514. */
  515. cboff = cbn % cs->sc_ileave; /* DEV_BSIZE gran */
  516. cbn = cbn / cs->sc_ileave; /* DEV_BSIZE * ileave gran */
  517. /*
  518. * Figure out which interleave table to use.
  519. */
  520. for (ii = cs->sc_itable; ii->ii_ndisk; ii++) {
  521. if (ii->ii_startblk > cbn)
  522. break;
  523. }
  524. ii--;
  525. /*
  526. * off is the logical superblock relative to the beginning
  527. * of this interleave block.
  528. */
  529. off = cbn - ii->ii_startblk;
  530. /*
  531. * We must calculate which disk component to use (ccdisk),
  532. * and recalculate cbn to be the superblock relative to
  533. * the beginning of the component. This is typically done by
  534. * adding 'off' and ii->ii_startoff together. However, 'off'
  535. * must typically be divided by the number of components in
  536. * this interleave array to be properly convert it from a
  537. * CCD-relative logical superblock number to a
  538. * component-relative superblock number.
  539. */
  540. if (ii->ii_ndisk == 1) {
  541. /*
  542. * When we have just one disk, it can't be a mirror
  543. * or a parity config.
  544. */
  545. ccdisk = ii->ii_index[0];
  546. cbn = ii->ii_startoff + off;
  547. } else {
  548. if (cs->sc_flags & CCDF_MIRROR) {
  549. /*
  550. * We have forced a uniform mapping, resulting
  551. * in a single interleave array. We double
  552. * up on the first half of the available
  553. * components and our mirror is in the second
  554. * half. This only works with a single
  555. * interleave array because doubling up
  556. * doubles the number of sectors, so there
  557. * cannot be another interleave array because
  558. * the next interleave array's calculations
  559. * would be off.
  560. */
  561. int ndisk2 = ii->ii_ndisk / 2;
  562. ccdisk = ii->ii_index[off % ndisk2];
  563. cbn = ii->ii_startoff + off / ndisk2;
  564. ci2 = &cs->sc_cinfo[ccdisk + ndisk2];
  565. } else {
  566. ccdisk = ii->ii_index[off % ii->ii_ndisk];
  567. cbn = ii->ii_startoff + off / ii->ii_ndisk;
  568. }
  569. }
  570. ci = &cs->sc_cinfo[ccdisk];
  571. /*
  572. * Convert cbn from a superblock to a normal block so it
  573. * can be used to calculate (along with cboff) the normal
  574. * block index into this particular disk.
  575. */
  576. cbn *= cs->sc_ileave;
  577. }
  578. /*
  579. * Fill in the component buf structure.
  580. */
  581. cbp = g_clone_bio(bp);
  582. if (cbp == NULL)
  583. return (ENOMEM);
  584. cbp->bio_done = g_std_done;
  585. cbp->bio_offset = dbtob(cbn + cboff + cs->sc_offset);
  586. cbp->bio_data = addr;
  587. if (cs->sc_ileave == 0)
  588. cbc = dbtob((off_t)(ci->ci_size - cbn));
  589. else
  590. cbc = dbtob((off_t)(cs->sc_ileave - cboff));
  591. cbp->bio_length = (cbc < bcount) ? cbc : bcount;
  592. cbp->bio_from = ci->ci_consumer;
  593. cb[0] = cbp;
  594. if (cs->sc_flags & CCDF_MIRROR) {
  595. cbp = g_clone_bio(bp);
  596. if (cbp == NULL)
  597. return (ENOMEM);
  598. cbp->bio_done = cb[0]->bio_done = ccdiodone;
  599. cbp->bio_offset = cb[0]->bio_offset;
  600. cbp->bio_data = cb[0]->bio_data;
  601. cbp->bio_length = cb[0]->bio_length;
  602. cbp->bio_from = ci2->ci_consumer;
  603. cbp->bio_caller1 = cb[0];
  604. cb[0]->bio_caller1 = cbp;
  605. cb[1] = cbp;
  606. }
  607. return (0);
  608. }
  609. /*
  610. * Called only for mirrored operations.
  611. */
  612. static void
  613. ccdiodone(struct bio *cbp)
  614. {
  615. struct bio *mbp, *pbp;
  616. mbp = cbp->bio_caller1;
  617. pbp = cbp->bio_parent;
  618. if (pbp->bio_cmd == BIO_READ) {
  619. if (cbp->bio_error == 0) {
  620. /* We will not be needing the partner bio */
  621. if (mbp != NULL) {
  622. pbp->bio_inbed++;
  623. g_destroy_bio(mbp);
  624. }
  625. g_std_done(cbp);
  626. return;
  627. }
  628. if (mbp != NULL) {
  629. /* Try partner the bio instead */
  630. mbp->bio_caller1 = NULL;
  631. pbp->bio_inbed++;
  632. g_destroy_bio(cbp);
  633. g_io_request(mbp, mbp->bio_from);
  634. /*
  635. * XXX: If this comes back OK, we should actually
  636. * try to write the good data on the failed mirror
  637. */
  638. return;
  639. }
  640. g_std_done(cbp);
  641. return;
  642. }
  643. if (mbp != NULL) {
  644. mbp->bio_caller1 = NULL;
  645. pbp->bio_inbed++;
  646. if (cbp->bio_error != 0 && pbp->bio_error == 0)
  647. pbp->bio_error = cbp->bio_error;
  648. g_destroy_bio(cbp);
  649. return;
  650. }
  651. g_std_done(cbp);
  652. }
  653. static void
  654. g_ccd_create(struct gctl_req *req, struct g_class *mp)
  655. {
  656. int *unit, *ileave, *nprovider;
  657. struct g_geom *gp;
  658. struct g_consumer *cp;
  659. struct g_provider *pp;
  660. struct ccd_s *sc;
  661. struct sbuf *sb;
  662. char buf[20];
  663. int i, error;
  664. g_topology_assert();
  665. unit = gctl_get_paraml(req, "unit", sizeof (*unit));
  666. if (unit == NULL) {
  667. gctl_error(req, "unit parameter not given");
  668. return;
  669. }
  670. ileave = gctl_get_paraml(req, "ileave", sizeof (*ileave));
  671. if (ileave == NULL) {
  672. gctl_error(req, "ileave parameter not given");
  673. return;
  674. }
  675. nprovider = gctl_get_paraml(req, "nprovider", sizeof (*nprovider));
  676. if (nprovider == NULL) {
  677. gctl_error(req, "nprovider parameter not given");
  678. return;
  679. }
  680. /* Check for duplicate unit */
  681. LIST_FOREACH(gp, &mp->geom, geom) {
  682. sc = gp->softc;
  683. if (sc != NULL && sc->sc_unit == *unit) {
  684. gctl_error(req, "Unit %d already configured", *unit);
  685. return;
  686. }
  687. }
  688. if (*nprovider <= 0) {
  689. gctl_error(req, "Bogus nprovider argument (= %d)", *nprovider);
  690. return;
  691. }
  692. /* Check all providers are valid */
  693. for (i = 0; i < *nprovider; i++) {
  694. snprintf(buf, sizeof(buf), "provider%d", i);
  695. pp = gctl_get_provider(req, buf);
  696. if (pp == NULL)
  697. return;
  698. }
  699. gp = g_new_geomf(mp, "ccd%d", *unit);
  700. sc = g_malloc(sizeof *sc, M_WAITOK | M_ZERO);
  701. gp->softc = sc;
  702. sc->sc_ndisks = *nprovider;
  703. /* Allocate space for the component info. */
  704. sc->sc_cinfo = g_malloc(sc->sc_ndisks * sizeof(struct ccdcinfo),
  705. M_WAITOK | M_ZERO);
  706. /* Create consumers and attach to all providers */
  707. for (i = 0; i < *nprovider; i++) {
  708. snprintf(buf, sizeof(buf), "provider%d", i);
  709. pp = gctl_get_provider(req, buf);
  710. cp = g_new_consumer(gp);
  711. error = g_attach(cp, pp);
  712. KASSERT(error == 0, ("attach to %s failed", pp->name));
  713. sc->sc_cinfo[i].ci_consumer = cp;
  714. sc->sc_cinfo[i].ci_provider = pp;
  715. }
  716. sc->sc_unit = *unit;
  717. sc->sc_ileave = *ileave;
  718. if (gctl_get_param(req, "no_offset", NULL))
  719. sc->sc_flags |= CCDF_NO_OFFSET;
  720. if (gctl_get_param(req, "linux", NULL))
  721. sc->sc_flags |= CCDF_LINUX;
  722. if (gctl_get_param(req, "uniform", NULL))
  723. sc->sc_flags |= CCDF_UNIFORM;
  724. if (gctl_get_param(req, "mirror", NULL))
  725. sc->sc_flags |= CCDF_MIRROR;
  726. if (sc->sc_ileave == 0 && (sc->sc_flags & CCDF_MIRROR)) {
  727. printf("%s: disabling mirror, interleave is 0\n", gp->name);
  728. sc->sc_flags &= ~(CCDF_MIRROR);
  729. }
  730. if ((sc->sc_flags & CCDF_MIRROR) && !(sc->sc_flags & CCDF_UNIFORM)) {
  731. printf("%s: mirror/parity forces uniform flag\n", gp->name);
  732. sc->sc_flags |= CCDF_UNIFORM;
  733. }
  734. error = ccdinit(req, sc);
  735. if (error != 0) {
  736. g_ccd_freesc(sc);
  737. gp->softc = NULL;
  738. g_wither_geom(gp, ENXIO);
  739. return;
  740. }
  741. pp = g_new_providerf(gp, "%s", gp->name);
  742. pp->mediasize = sc->sc_size * (off_t)sc->sc_secsize;
  743. pp->sectorsize = sc->sc_secsize;
  744. g_error_provider(pp, 0);
  745. sb = sbuf_new_auto();
  746. sbuf_printf(sb, "ccd%d: %d components ", sc->sc_unit, *nprovider);
  747. for (i = 0; i < *nprovider; i++) {
  748. sbuf_printf(sb, "%s%s",
  749. i == 0 ? "(" : ", ",
  750. sc->sc_cinfo[i].ci_provider->name);
  751. }
  752. sbuf_printf(sb, "), %jd blocks ", (off_t)pp->mediasize / DEV_BSIZE);
  753. if (sc->sc_ileave != 0)
  754. sbuf_printf(sb, "interleaved at %d blocks\n",
  755. sc->sc_ileave);
  756. else
  757. sbuf_printf(sb, "concatenated\n");
  758. sbuf_finish(sb);
  759. gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  760. sbuf_delete(sb);
  761. }
  762. static int
  763. g_ccd_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
  764. {
  765. struct g_provider *pp;
  766. struct ccd_s *sc;
  767. g_topology_assert();
  768. sc = gp->softc;
  769. pp = LIST_FIRST(&gp->provider);
  770. if (sc == NULL || pp == NULL)
  771. return (EBUSY);
  772. if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
  773. gctl_error(req, "%s is open(r%dw%de%d)", gp->name,
  774. pp->acr, pp->acw, pp->ace);
  775. return (EBUSY);
  776. }
  777. g_ccd_freesc(sc);
  778. gp->softc = NULL;
  779. g_wither_geom(gp, ENXIO);
  780. return (0);
  781. }
  782. static void
  783. g_ccd_list(struct gctl_req *req, struct g_class *mp)
  784. {
  785. struct sbuf *sb;
  786. struct ccd_s *cs;
  787. struct g_geom *gp;
  788. int i, unit, *up;
  789. up = gctl_get_paraml(req, "unit", sizeof (*up));
  790. if (up == NULL) {
  791. gctl_error(req, "unit parameter not given");
  792. return;
  793. }
  794. unit = *up;
  795. sb = sbuf_new_auto();
  796. LIST_FOREACH(gp, &mp->geom, geom) {
  797. cs = gp->softc;
  798. if (cs == NULL || (unit >= 0 && unit != cs->sc_unit))
  799. continue;
  800. sbuf_printf(sb, "ccd%d\t\t%d\t%d\t",
  801. cs->sc_unit, cs->sc_ileave, cs->sc_flags & CCDF_USERMASK);
  802. for (i = 0; i < cs->sc_ndisks; ++i) {
  803. sbuf_printf(sb, "%s/dev/%s", i == 0 ? "" : " ",
  804. cs->sc_cinfo[i].ci_provider->name);
  805. }
  806. sbuf_printf(sb, "\n");
  807. }
  808. sbuf_finish(sb);
  809. gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
  810. sbuf_delete(sb);
  811. }
  812. static void
  813. g_ccd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
  814. {
  815. struct g_geom *gp;
  816. g_topology_assert();
  817. if (!strcmp(verb, "create geom")) {
  818. g_ccd_create(req, mp);
  819. } else if (!strcmp(verb, "destroy geom")) {
  820. gp = gctl_get_geom(req, mp, "geom");
  821. if (gp != NULL)
  822. g_ccd_destroy_geom(req, mp, gp);
  823. } else if (!strcmp(verb, "list")) {
  824. g_ccd_list(req, mp);
  825. } else {
  826. gctl_error(req, "unknown verb");
  827. }
  828. }
  829. static struct g_class g_ccd_class = {
  830. .name = "CCD",
  831. .version = G_VERSION,
  832. .ctlreq = g_ccd_config,
  833. .destroy_geom = g_ccd_destroy_geom,
  834. .start = g_ccd_start,
  835. .orphan = g_ccd_orphan,
  836. .access = g_ccd_access,
  837. };
  838. DECLARE_GEOM_CLASS(g_ccd_class, g_ccd);
  839. MODULE_VERSION(geom_ccd, 0);