g_virstor.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2006-2007 Ivan Voras <ivoras@freebsd.org>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /* Implementation notes:
  29. * - "Components" are wrappers around providers that make up the
  30. * virtual storage (i.e. a virstor has "physical" components)
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/kernel.h>
  35. #include <sys/module.h>
  36. #include <sys/lock.h>
  37. #include <sys/mutex.h>
  38. #include <sys/sx.h>
  39. #include <sys/bio.h>
  40. #include <sys/sbuf.h>
  41. #include <sys/sysctl.h>
  42. #include <sys/malloc.h>
  43. #include <sys/time.h>
  44. #include <sys/proc.h>
  45. #include <sys/kthread.h>
  46. #include <sys/mutex.h>
  47. #include <vm/uma.h>
  48. #include <geom/geom.h>
  49. #include <geom/geom_dbg.h>
  50. #include <geom/virstor/g_virstor.h>
  51. #include <geom/virstor/g_virstor_md.h>
  52. FEATURE(g_virstor, "GEOM virtual storage support");
  53. /* Declare malloc(9) label */
  54. static MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data");
  55. /* GEOM class methods */
  56. static g_init_t g_virstor_init;
  57. static g_fini_t g_virstor_fini;
  58. static g_taste_t g_virstor_taste;
  59. static g_ctl_req_t g_virstor_config;
  60. static g_ctl_destroy_geom_t g_virstor_destroy_geom;
  61. /* Declare & initialize class structure ("geom class") */
  62. struct g_class g_virstor_class = {
  63. .name = G_VIRSTOR_CLASS_NAME,
  64. .version = G_VERSION,
  65. .init = g_virstor_init,
  66. .fini = g_virstor_fini,
  67. .taste = g_virstor_taste,
  68. .ctlreq = g_virstor_config,
  69. .destroy_geom = g_virstor_destroy_geom
  70. /* The .dumpconf and the rest are only usable for a geom instance, so
  71. * they will be set when such instance is created. */
  72. };
  73. /* Declare sysctl's and loader tunables */
  74. SYSCTL_DECL(_kern_geom);
  75. static SYSCTL_NODE(_kern_geom, OID_AUTO, virstor,
  76. CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  77. "GEOM_GVIRSTOR information");
  78. static u_int g_virstor_debug = 2; /* XXX: lower to 2 when released to public */
  79. SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, debug, CTLFLAG_RWTUN, &g_virstor_debug,
  80. 0, "Debug level (2=production, 5=normal, 15=excessive)");
  81. static u_int g_virstor_chunk_watermark = 100;
  82. SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, chunk_watermark, CTLFLAG_RWTUN,
  83. &g_virstor_chunk_watermark, 0,
  84. "Minimum number of free chunks before issuing administrative warning");
  85. static u_int g_virstor_component_watermark = 1;
  86. SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, component_watermark, CTLFLAG_RWTUN,
  87. &g_virstor_component_watermark, 0,
  88. "Minimum number of free components before issuing administrative warning");
  89. static int read_metadata(struct g_consumer *, struct g_virstor_metadata *);
  90. static void write_metadata(struct g_consumer *, struct g_virstor_metadata *);
  91. static int clear_metadata(struct g_virstor_component *);
  92. static int add_provider_to_geom(struct g_virstor_softc *, struct g_provider *,
  93. struct g_virstor_metadata *);
  94. static struct g_geom *create_virstor_geom(struct g_class *,
  95. struct g_virstor_metadata *);
  96. static void virstor_check_and_run(struct g_virstor_softc *);
  97. static u_int virstor_valid_components(struct g_virstor_softc *);
  98. static int virstor_geom_destroy(struct g_virstor_softc *, boolean_t,
  99. boolean_t);
  100. static void remove_component(struct g_virstor_softc *,
  101. struct g_virstor_component *, boolean_t);
  102. static void bioq_dismantle(struct bio_queue_head *);
  103. static int allocate_chunk(struct g_virstor_softc *,
  104. struct g_virstor_component **, u_int *, u_int *);
  105. static void delay_destroy_consumer(void *, int);
  106. static void dump_component(struct g_virstor_component *comp);
  107. #if 0
  108. static void dump_me(struct virstor_map_entry *me, unsigned int nr);
  109. #endif
  110. static void virstor_ctl_stop(struct gctl_req *, struct g_class *);
  111. static void virstor_ctl_add(struct gctl_req *, struct g_class *);
  112. static void virstor_ctl_remove(struct gctl_req *, struct g_class *);
  113. static struct g_virstor_softc * virstor_find_geom(const struct g_class *,
  114. const char *);
  115. static void update_metadata(struct g_virstor_softc *);
  116. static void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *,
  117. u_int, u_int);
  118. static void g_virstor_orphan(struct g_consumer *);
  119. static int g_virstor_access(struct g_provider *, int, int, int);
  120. static void g_virstor_start(struct bio *);
  121. static void g_virstor_dumpconf(struct sbuf *, const char *, struct g_geom *,
  122. struct g_consumer *, struct g_provider *);
  123. static void g_virstor_done(struct bio *);
  124. static void invalid_call(void);
  125. /*
  126. * Initialise GEOM class (per-class callback)
  127. */
  128. static void
  129. g_virstor_init(struct g_class *mp __unused)
  130. {
  131. /* Catch map struct size mismatch at compile time; Map entries must
  132. * fit into maxphys exactly, with no wasted space. */
  133. MPASS(VIRSTOR_MAP_BLOCK_ENTRIES * VIRSTOR_MAP_ENTRY_SIZE == maxphys);
  134. /* Init UMA zones, TAILQ's, other global vars */
  135. }
  136. /*
  137. * Finalise GEOM class (per-class callback)
  138. */
  139. static void
  140. g_virstor_fini(struct g_class *mp __unused)
  141. {
  142. /* Deinit UMA zones & global vars */
  143. }
  144. /*
  145. * Config (per-class callback)
  146. */
  147. static void
  148. g_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb)
  149. {
  150. uint32_t *version;
  151. g_topology_assert();
  152. version = gctl_get_paraml(req, "version", sizeof(*version));
  153. if (version == NULL) {
  154. gctl_error(req, "Failed to get 'version' argument");
  155. return;
  156. }
  157. if (*version != G_VIRSTOR_VERSION) {
  158. gctl_error(req, "Userland and kernel versions out of sync");
  159. return;
  160. }
  161. g_topology_unlock();
  162. if (strcmp(verb, "add") == 0)
  163. virstor_ctl_add(req, cp);
  164. else if (strcmp(verb, "stop") == 0 || strcmp(verb, "destroy") == 0)
  165. virstor_ctl_stop(req, cp);
  166. else if (strcmp(verb, "remove") == 0)
  167. virstor_ctl_remove(req, cp);
  168. else
  169. gctl_error(req, "unknown verb: '%s'", verb);
  170. g_topology_lock();
  171. }
  172. /*
  173. * "stop" verb from userland
  174. */
  175. static void
  176. virstor_ctl_stop(struct gctl_req *req, struct g_class *cp)
  177. {
  178. int *force, *nargs;
  179. int i;
  180. nargs = gctl_get_paraml(req, "nargs", sizeof *nargs);
  181. if (nargs == NULL) {
  182. gctl_error(req, "Error fetching argument '%s'", "nargs");
  183. return;
  184. }
  185. if (*nargs < 1) {
  186. gctl_error(req, "Invalid number of arguments");
  187. return;
  188. }
  189. force = gctl_get_paraml(req, "force", sizeof *force);
  190. if (force == NULL) {
  191. gctl_error(req, "Error fetching argument '%s'", "force");
  192. return;
  193. }
  194. g_topology_lock();
  195. for (i = 0; i < *nargs; i++) {
  196. char param[8];
  197. const char *name;
  198. struct g_virstor_softc *sc;
  199. int error;
  200. snprintf(param, sizeof(param), "arg%d", i);
  201. name = gctl_get_asciiparam(req, param);
  202. if (name == NULL) {
  203. gctl_error(req, "No 'arg%d' argument", i);
  204. g_topology_unlock();
  205. return;
  206. }
  207. sc = virstor_find_geom(cp, name);
  208. if (sc == NULL) {
  209. gctl_error(req, "Don't know anything about '%s'", name);
  210. g_topology_unlock();
  211. return;
  212. }
  213. LOG_MSG(LVL_INFO, "Stopping %s by the userland command",
  214. sc->geom->name);
  215. update_metadata(sc);
  216. if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) {
  217. LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d",
  218. sc->geom->name, error);
  219. }
  220. }
  221. g_topology_unlock();
  222. }
  223. /*
  224. * "add" verb from userland - add new component(s) to the structure.
  225. * This will be done all at once in here, without going through the
  226. * .taste function for new components.
  227. */
  228. static void
  229. virstor_ctl_add(struct gctl_req *req, struct g_class *cp)
  230. {
  231. /* Note: while this is going on, I/O is being done on
  232. * the g_up and g_down threads. The idea is to make changes
  233. * to softc members in a way that can atomically activate
  234. * them all at once. */
  235. struct g_virstor_softc *sc;
  236. int *hardcode, *nargs;
  237. const char *geom_name; /* geom to add a component to */
  238. struct g_consumer *fcp;
  239. struct g_virstor_bio_q *bq;
  240. u_int added;
  241. int error;
  242. int i;
  243. nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
  244. if (nargs == NULL) {
  245. gctl_error(req, "Error fetching argument '%s'", "nargs");
  246. return;
  247. }
  248. if (*nargs < 2) {
  249. gctl_error(req, "Invalid number of arguments");
  250. return;
  251. }
  252. hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
  253. if (hardcode == NULL) {
  254. gctl_error(req, "Error fetching argument '%s'", "hardcode");
  255. return;
  256. }
  257. /* Find "our" geom */
  258. geom_name = gctl_get_asciiparam(req, "arg0");
  259. if (geom_name == NULL) {
  260. gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)");
  261. return;
  262. }
  263. sc = virstor_find_geom(cp, geom_name);
  264. if (sc == NULL) {
  265. gctl_error(req, "Don't know anything about '%s'", geom_name);
  266. return;
  267. }
  268. if (virstor_valid_components(sc) != sc->n_components) {
  269. LOG_MSG(LVL_ERROR, "Cannot add components to incomplete "
  270. "virstor %s", sc->geom->name);
  271. gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
  272. return;
  273. }
  274. fcp = sc->components[0].gcons;
  275. added = 0;
  276. g_topology_lock();
  277. for (i = 1; i < *nargs; i++) {
  278. struct g_virstor_metadata md;
  279. char aname[8];
  280. struct g_provider *pp;
  281. struct g_consumer *cp;
  282. u_int nc;
  283. u_int j;
  284. snprintf(aname, sizeof aname, "arg%d", i);
  285. pp = gctl_get_provider(req, aname);
  286. if (pp == NULL) {
  287. /* This is the most common error so be verbose about it */
  288. if (added != 0) {
  289. gctl_error(req, "Invalid provider. (added"
  290. " %u components)", added);
  291. update_metadata(sc);
  292. }
  293. g_topology_unlock();
  294. return;
  295. }
  296. cp = g_new_consumer(sc->geom);
  297. if (cp == NULL) {
  298. gctl_error(req, "Cannot create consumer");
  299. g_topology_unlock();
  300. return;
  301. }
  302. error = g_attach(cp, pp);
  303. if (error != 0) {
  304. gctl_error(req, "Cannot attach a consumer to %s",
  305. pp->name);
  306. g_destroy_consumer(cp);
  307. g_topology_unlock();
  308. return;
  309. }
  310. if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) {
  311. error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
  312. if (error != 0) {
  313. gctl_error(req, "Access request failed for %s",
  314. pp->name);
  315. g_destroy_consumer(cp);
  316. g_topology_unlock();
  317. return;
  318. }
  319. }
  320. if (fcp->provider->sectorsize != pp->sectorsize) {
  321. gctl_error(req, "Sector size doesn't fit for %s",
  322. pp->name);
  323. g_destroy_consumer(cp);
  324. g_topology_unlock();
  325. return;
  326. }
  327. for (j = 0; j < sc->n_components; j++) {
  328. if (strcmp(sc->components[j].gcons->provider->name,
  329. pp->name) == 0) {
  330. gctl_error(req, "Component %s already in %s",
  331. pp->name, sc->geom->name);
  332. g_destroy_consumer(cp);
  333. g_topology_unlock();
  334. return;
  335. }
  336. }
  337. sc->components = realloc(sc->components,
  338. sizeof(*sc->components) * (sc->n_components + 1),
  339. M_GVIRSTOR, M_WAITOK);
  340. nc = sc->n_components;
  341. sc->components[nc].gcons = cp;
  342. sc->components[nc].sc = sc;
  343. sc->components[nc].index = nc;
  344. sc->components[nc].chunk_count = cp->provider->mediasize /
  345. sc->chunk_size;
  346. sc->components[nc].chunk_next = 0;
  347. sc->components[nc].chunk_reserved = 0;
  348. if (sc->components[nc].chunk_count < 4) {
  349. gctl_error(req, "Provider too small: %s",
  350. cp->provider->name);
  351. g_destroy_consumer(cp);
  352. g_topology_unlock();
  353. return;
  354. }
  355. fill_metadata(sc, &md, nc, *hardcode);
  356. write_metadata(cp, &md);
  357. /* The new component becomes visible when n_components is
  358. * incremented */
  359. sc->n_components++;
  360. added++;
  361. }
  362. /* This call to update_metadata() is critical. In case there's a
  363. * power failure in the middle of it and some components are updated
  364. * while others are not, there will be trouble on next .taste() iff
  365. * a non-updated component is detected first */
  366. update_metadata(sc);
  367. g_topology_unlock();
  368. LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added,
  369. sc->geom->name);
  370. /* Fire off BIOs previously queued because there wasn't any
  371. * physical space left. If the BIOs still can't be satisfied
  372. * they will again be added to the end of the queue (during
  373. * which the mutex will be recursed) */
  374. bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK);
  375. bq->bio = NULL;
  376. mtx_lock(&sc->delayed_bio_q_mtx);
  377. /* First, insert a sentinel to the queue end, so we don't
  378. * end up in an infinite loop if there's still no free
  379. * space available. */
  380. STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage);
  381. while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
  382. bq = STAILQ_FIRST(&sc->delayed_bio_q);
  383. if (bq->bio != NULL) {
  384. g_virstor_start(bq->bio);
  385. STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
  386. free(bq, M_GVIRSTOR);
  387. } else {
  388. STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
  389. free(bq, M_GVIRSTOR);
  390. break;
  391. }
  392. }
  393. mtx_unlock(&sc->delayed_bio_q_mtx);
  394. }
  395. /*
  396. * Find a geom handled by the class
  397. */
  398. static struct g_virstor_softc *
  399. virstor_find_geom(const struct g_class *cp, const char *name)
  400. {
  401. struct g_geom *gp;
  402. LIST_FOREACH(gp, &cp->geom, geom) {
  403. if (strcmp(name, gp->name) == 0)
  404. return (gp->softc);
  405. }
  406. return (NULL);
  407. }
  408. /*
  409. * Update metadata on all components to reflect the current state
  410. * of these fields:
  411. * - chunk_next
  412. * - flags
  413. * - md_count
  414. * Expects things to be set up so write_metadata() can work, i.e.
  415. * the topology lock must be held.
  416. */
  417. static void
  418. update_metadata(struct g_virstor_softc *sc)
  419. {
  420. struct g_virstor_metadata md;
  421. u_int n;
  422. if (virstor_valid_components(sc) != sc->n_components)
  423. return; /* Incomplete device */
  424. LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s",
  425. sc->geom->name);
  426. /* Update metadata on components */
  427. g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
  428. sc->geom->class->name, sc->geom->name);
  429. g_topology_assert();
  430. for (n = 0; n < sc->n_components; n++) {
  431. read_metadata(sc->components[n].gcons, &md);
  432. md.chunk_next = sc->components[n].chunk_next;
  433. md.flags = sc->components[n].flags;
  434. md.md_count = sc->n_components;
  435. write_metadata(sc->components[n].gcons, &md);
  436. }
  437. }
  438. /*
  439. * Fills metadata (struct md) from information stored in softc and the nc'th
  440. * component of virstor
  441. */
  442. static void
  443. fill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md,
  444. u_int nc, u_int hardcode)
  445. {
  446. struct g_virstor_component *c;
  447. bzero(md, sizeof *md);
  448. c = &sc->components[nc];
  449. strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic);
  450. md->md_version = G_VIRSTOR_VERSION;
  451. strncpy(md->md_name, sc->geom->name, sizeof md->md_name);
  452. md->md_id = sc->id;
  453. md->md_virsize = sc->virsize;
  454. md->md_chunk_size = sc->chunk_size;
  455. md->md_count = sc->n_components;
  456. if (hardcode) {
  457. strncpy(md->provider, c->gcons->provider->name,
  458. sizeof md->provider);
  459. }
  460. md->no = nc;
  461. md->provsize = c->gcons->provider->mediasize;
  462. md->chunk_count = c->chunk_count;
  463. md->chunk_next = c->chunk_next;
  464. md->chunk_reserved = c->chunk_reserved;
  465. md->flags = c->flags;
  466. }
  467. /*
  468. * Remove a component from virstor device.
  469. * Can only be done if the component is unallocated.
  470. */
  471. static void
  472. virstor_ctl_remove(struct gctl_req *req, struct g_class *cp)
  473. {
  474. /* As this is executed in parallel to I/O, operations on virstor
  475. * structures must be as atomic as possible. */
  476. struct g_virstor_softc *sc;
  477. int *nargs;
  478. const char *geom_name;
  479. u_int removed;
  480. int i;
  481. nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
  482. if (nargs == NULL) {
  483. gctl_error(req, "Error fetching argument '%s'", "nargs");
  484. return;
  485. }
  486. if (*nargs < 2) {
  487. gctl_error(req, "Invalid number of arguments");
  488. return;
  489. }
  490. /* Find "our" geom */
  491. geom_name = gctl_get_asciiparam(req, "arg0");
  492. if (geom_name == NULL) {
  493. gctl_error(req, "Error fetching argument '%s'",
  494. "geom_name (arg0)");
  495. return;
  496. }
  497. sc = virstor_find_geom(cp, geom_name);
  498. if (sc == NULL) {
  499. gctl_error(req, "Don't know anything about '%s'", geom_name);
  500. return;
  501. }
  502. if (virstor_valid_components(sc) != sc->n_components) {
  503. LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete "
  504. "virstor %s", sc->geom->name);
  505. gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
  506. return;
  507. }
  508. removed = 0;
  509. for (i = 1; i < *nargs; i++) {
  510. char param[8];
  511. const char *prov_name;
  512. int j, found;
  513. struct g_virstor_component *newcomp, *compbak;
  514. snprintf(param, sizeof(param), "arg%d", i);
  515. prov_name = gctl_get_asciiparam(req, param);
  516. if (prov_name == NULL) {
  517. gctl_error(req, "Error fetching argument '%s'", param);
  518. return;
  519. }
  520. if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
  521. prov_name += sizeof(_PATH_DEV) - 1;
  522. found = -1;
  523. for (j = 0; j < sc->n_components; j++) {
  524. if (strcmp(sc->components[j].gcons->provider->name,
  525. prov_name) == 0) {
  526. found = j;
  527. break;
  528. }
  529. }
  530. if (found == -1) {
  531. LOG_MSG(LVL_ERROR, "No %s component in %s",
  532. prov_name, sc->geom->name);
  533. continue;
  534. }
  535. compbak = sc->components;
  536. newcomp = malloc(sc->n_components * sizeof(*sc->components),
  537. M_GVIRSTOR, M_WAITOK | M_ZERO);
  538. bcopy(sc->components, newcomp, found * sizeof(*sc->components));
  539. bcopy(&sc->components[found + 1], newcomp + found,
  540. found * sizeof(*sc->components));
  541. if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) {
  542. LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be "
  543. "removed from %s",
  544. prov_name, sc->geom->name);
  545. free(newcomp, M_GVIRSTOR);
  546. /* We'll consider this non-fatal error */
  547. continue;
  548. }
  549. /* Renumerate unallocated components */
  550. for (j = 0; j < sc->n_components-1; j++) {
  551. if ((sc->components[j].flags &
  552. VIRSTOR_PROVIDER_ALLOCATED) == 0) {
  553. sc->components[j].index = j;
  554. }
  555. }
  556. /* This is the critical section. If a component allocation
  557. * event happens while both variables are not yet set,
  558. * there will be trouble. Something will panic on encountering
  559. * NULL sc->components[x].gcomp member.
  560. * Luckily, component allocation happens very rarely and
  561. * removing components is an abnormal action in any case. */
  562. sc->components = newcomp;
  563. sc->n_components--;
  564. /* End critical section */
  565. g_topology_lock();
  566. if (clear_metadata(&compbak[found]) != 0) {
  567. LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear "
  568. "metadata on %s", prov_name);
  569. }
  570. g_detach(compbak[found].gcons);
  571. g_destroy_consumer(compbak[found].gcons);
  572. g_topology_unlock();
  573. free(compbak, M_GVIRSTOR);
  574. removed++;
  575. }
  576. /* This call to update_metadata() is critical. In case there's a
  577. * power failure in the middle of it and some components are updated
  578. * while others are not, there will be trouble on next .taste() iff
  579. * a non-updated component is detected first */
  580. g_topology_lock();
  581. update_metadata(sc);
  582. g_topology_unlock();
  583. LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed,
  584. sc->geom->name);
  585. }
  586. /*
  587. * Clear metadata sector on component
  588. */
  589. static int
  590. clear_metadata(struct g_virstor_component *comp)
  591. {
  592. char *buf;
  593. int error;
  594. LOG_MSG(LVL_INFO, "Clearing metadata on %s",
  595. comp->gcons->provider->name);
  596. g_topology_assert();
  597. error = g_access(comp->gcons, 0, 1, 0);
  598. if (error != 0)
  599. return (error);
  600. buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR,
  601. M_WAITOK | M_ZERO);
  602. error = g_write_data(comp->gcons,
  603. comp->gcons->provider->mediasize -
  604. comp->gcons->provider->sectorsize,
  605. buf,
  606. comp->gcons->provider->sectorsize);
  607. free(buf, M_GVIRSTOR);
  608. g_access(comp->gcons, 0, -1, 0);
  609. return (error);
  610. }
  611. /*
  612. * Destroy geom forcibly.
  613. */
  614. static int
  615. g_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
  616. struct g_geom *gp)
  617. {
  618. struct g_virstor_softc *sc;
  619. int exitval;
  620. sc = gp->softc;
  621. KASSERT(sc != NULL, ("%s: NULL sc", __func__));
  622. exitval = 0;
  623. LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name,
  624. gp->softc);
  625. if (sc != NULL) {
  626. #ifdef INVARIANTS
  627. char *buf;
  628. int error;
  629. off_t off;
  630. int isclean, count;
  631. int n;
  632. LOG_MSG(LVL_INFO, "INVARIANTS detected");
  633. LOG_MSG(LVL_INFO, "Verifying allocation "
  634. "table for %s", sc->geom->name);
  635. count = 0;
  636. for (n = 0; n < sc->chunk_count; n++) {
  637. if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0)
  638. count++;
  639. }
  640. LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks",
  641. sc->geom->name, count);
  642. n = off = count = 0;
  643. isclean = 1;
  644. if (virstor_valid_components(sc) != sc->n_components) {
  645. /* This is a incomplete virstor device (not all
  646. * components have been found) */
  647. LOG_MSG(LVL_ERROR, "Device %s is incomplete",
  648. sc->geom->name);
  649. goto bailout;
  650. }
  651. error = g_access(sc->components[0].gcons, 1, 0, 0);
  652. KASSERT(error == 0, ("%s: g_access failed (%d)", __func__,
  653. error));
  654. /* Compare the whole on-disk allocation table with what's
  655. * currently in memory */
  656. while (n < sc->chunk_count) {
  657. buf = g_read_data(sc->components[0].gcons, off,
  658. sc->sectorsize, &error);
  659. KASSERT(buf != NULL, ("g_read_data returned NULL (%d) "
  660. "for read at %jd", error, off));
  661. if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) {
  662. LOG_MSG(LVL_ERROR, "ERROR in allocation table, "
  663. "entry %d, offset %jd", n, off);
  664. isclean = 0;
  665. count++;
  666. }
  667. n += sc->me_per_sector;
  668. off += sc->sectorsize;
  669. g_free(buf);
  670. }
  671. error = g_access(sc->components[0].gcons, -1, 0, 0);
  672. KASSERT(error == 0, ("%s: g_access failed (%d) on exit",
  673. __func__, error));
  674. if (isclean != 1) {
  675. LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s "
  676. "(%d sectors don't match, max %zu allocations)",
  677. sc->geom->name, count,
  678. count * sc->me_per_sector);
  679. } else {
  680. LOG_MSG(LVL_INFO, "Allocation table ok for %s",
  681. sc->geom->name);
  682. }
  683. bailout:
  684. #endif
  685. update_metadata(sc);
  686. virstor_geom_destroy(sc, FALSE, FALSE);
  687. exitval = EAGAIN;
  688. } else
  689. exitval = 0;
  690. return (exitval);
  691. }
  692. /*
  693. * Taste event (per-class callback)
  694. * Examines a provider and creates geom instances if needed
  695. */
  696. static struct g_geom *
  697. g_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags)
  698. {
  699. struct g_virstor_metadata md;
  700. struct g_geom *gp;
  701. struct g_consumer *cp;
  702. struct g_virstor_softc *sc;
  703. int error;
  704. g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
  705. g_topology_assert();
  706. LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name);
  707. /* We need a dummy geom to attach a consumer to the given provider */
  708. gp = g_new_geomf(mp, "virstor:taste.helper");
  709. gp->start = (void *)invalid_call; /* XXX: hacked up so the */
  710. gp->access = (void *)invalid_call; /* compiler doesn't complain. */
  711. gp->orphan = (void *)invalid_call; /* I really want these to fail. */
  712. cp = g_new_consumer(gp);
  713. cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
  714. error = g_attach(cp, pp);
  715. if (error == 0) {
  716. error = read_metadata(cp, &md);
  717. g_detach(cp);
  718. }
  719. g_destroy_consumer(cp);
  720. g_destroy_geom(gp);
  721. if (error != 0)
  722. return (NULL);
  723. if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0)
  724. return (NULL);
  725. if (md.md_version != G_VIRSTOR_VERSION) {
  726. LOG_MSG(LVL_ERROR, "Kernel module version invalid "
  727. "to handle %s (%s) : %d should be %d",
  728. md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION);
  729. return (NULL);
  730. }
  731. if (md.provsize != pp->mediasize)
  732. return (NULL);
  733. /* If the provider name is hardcoded, use the offered provider only
  734. * if it's been offered with its proper name (the one used in
  735. * the label command). */
  736. if (md.provider[0] != '\0' &&
  737. !g_compare_names(md.provider, pp->name))
  738. return (NULL);
  739. /* Iterate all geoms this class already knows about to see if a new
  740. * geom instance of this class needs to be created (in case the provider
  741. * is first from a (possibly) multi-consumer geom) or it just needs
  742. * to be added to an existing instance. */
  743. sc = NULL;
  744. gp = NULL;
  745. LIST_FOREACH(gp, &mp->geom, geom) {
  746. sc = gp->softc;
  747. if (sc == NULL)
  748. continue;
  749. if (strcmp(md.md_name, sc->geom->name) != 0)
  750. continue;
  751. if (md.md_id != sc->id)
  752. continue;
  753. break;
  754. }
  755. if (gp != NULL) { /* We found an existing geom instance; add to it */
  756. LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name);
  757. error = add_provider_to_geom(sc, pp, &md);
  758. if (error != 0) {
  759. LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
  760. pp->name, md.md_name, error);
  761. return (NULL);
  762. }
  763. } else { /* New geom instance needs to be created */
  764. gp = create_virstor_geom(mp, &md);
  765. if (gp == NULL) {
  766. LOG_MSG(LVL_ERROR, "Error creating new instance of "
  767. "class %s: %s", mp->name, md.md_name);
  768. LOG_MSG(LVL_DEBUG, "Error creating %s at %s",
  769. md.md_name, pp->name);
  770. return (NULL);
  771. }
  772. sc = gp->softc;
  773. LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name,
  774. md.md_name);
  775. error = add_provider_to_geom(sc, pp, &md);
  776. if (error != 0) {
  777. LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
  778. pp->name, md.md_name, error);
  779. virstor_geom_destroy(sc, TRUE, FALSE);
  780. return (NULL);
  781. }
  782. }
  783. return (gp);
  784. }
  785. /*
  786. * Destroyes consumer passed to it in arguments. Used as a callback
  787. * on g_event queue.
  788. */
  789. static void
  790. delay_destroy_consumer(void *arg, int flags __unused)
  791. {
  792. struct g_consumer *c = arg;
  793. KASSERT(c != NULL, ("%s: invalid consumer", __func__));
  794. LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay",
  795. c->provider->name);
  796. g_detach(c);
  797. g_destroy_consumer(c);
  798. }
  799. /*
  800. * Remove a component (consumer) from geom instance; If it's the first
  801. * component being removed, orphan the provider to announce geom's being
  802. * dismantled
  803. */
  804. static void
  805. remove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp,
  806. boolean_t delay)
  807. {
  808. struct g_consumer *c;
  809. KASSERT(comp->gcons != NULL, ("Component with no consumer in %s",
  810. sc->geom->name));
  811. c = comp->gcons;
  812. comp->gcons = NULL;
  813. KASSERT(c->provider != NULL, ("%s: no provider", __func__));
  814. LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
  815. sc->geom->name);
  816. if (sc->provider != NULL) {
  817. LOG_MSG(LVL_INFO, "Removing provider %s", sc->provider->name);
  818. g_wither_provider(sc->provider, ENXIO);
  819. sc->provider = NULL;
  820. }
  821. if (c->acr > 0 || c->acw > 0 || c->ace > 0)
  822. return;
  823. if (delay) {
  824. /* Destroy consumer after it's tasted */
  825. g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL);
  826. } else {
  827. g_detach(c);
  828. g_destroy_consumer(c);
  829. }
  830. }
  831. /*
  832. * Destroy geom - called internally
  833. * See g_virstor_destroy_geom for the other one
  834. */
  835. static int
  836. virstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force,
  837. boolean_t delay)
  838. {
  839. struct g_provider *pp;
  840. struct g_geom *gp;
  841. u_int n;
  842. g_topology_assert();
  843. if (sc == NULL)
  844. return (ENXIO);
  845. pp = sc->provider;
  846. if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
  847. LOG_MSG(force ? LVL_WARNING : LVL_ERROR,
  848. "Device %s is still open.", pp->name);
  849. if (!force)
  850. return (EBUSY);
  851. }
  852. for (n = 0; n < sc->n_components; n++) {
  853. if (sc->components[n].gcons != NULL)
  854. remove_component(sc, &sc->components[n], delay);
  855. }
  856. gp = sc->geom;
  857. gp->softc = NULL;
  858. KASSERT(sc->provider == NULL, ("Provider still exists for %s",
  859. gp->name));
  860. /* XXX: This might or might not work, since we're called with
  861. * the topology lock held. Also, it might panic the kernel if
  862. * the error'd BIO is in softupdates code. */
  863. mtx_lock(&sc->delayed_bio_q_mtx);
  864. while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
  865. struct g_virstor_bio_q *bq;
  866. bq = STAILQ_FIRST(&sc->delayed_bio_q);
  867. bq->bio->bio_error = ENOSPC;
  868. g_io_deliver(bq->bio, EIO);
  869. STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
  870. free(bq, M_GVIRSTOR);
  871. }
  872. mtx_unlock(&sc->delayed_bio_q_mtx);
  873. mtx_destroy(&sc->delayed_bio_q_mtx);
  874. free(sc->map, M_GVIRSTOR);
  875. free(sc->components, M_GVIRSTOR);
  876. bzero(sc, sizeof *sc);
  877. free(sc, M_GVIRSTOR);
  878. pp = LIST_FIRST(&gp->provider); /* We only offer one provider */
  879. if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
  880. LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name);
  881. g_wither_geom(gp, ENXIO);
  882. return (0);
  883. }
  884. /*
  885. * Utility function: read metadata & decode. Wants topology lock to be
  886. * held.
  887. */
  888. static int
  889. read_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
  890. {
  891. struct g_provider *pp;
  892. char *buf;
  893. int error;
  894. g_topology_assert();
  895. error = g_access(cp, 1, 0, 0);
  896. if (error != 0)
  897. return (error);
  898. pp = cp->provider;
  899. g_topology_unlock();
  900. buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
  901. &error);
  902. g_topology_lock();
  903. g_access(cp, -1, 0, 0);
  904. if (buf == NULL)
  905. return (error);
  906. virstor_metadata_decode(buf, md);
  907. g_free(buf);
  908. return (0);
  909. }
  910. /**
  911. * Utility function: encode & write metadata. Assumes topology lock is
  912. * held.
  913. *
  914. * There is no useful way of recovering from errors in this function,
  915. * not involving panicking the kernel. If the metadata cannot be written
  916. * the most we can do is notify the operator and hope he spots it and
  917. * replaces the broken drive.
  918. */
  919. static void
  920. write_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
  921. {
  922. struct g_provider *pp;
  923. char *buf;
  924. int error;
  925. KASSERT(cp != NULL && md != NULL && cp->provider != NULL,
  926. ("Something's fishy in %s", __func__));
  927. LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name);
  928. g_topology_assert();
  929. error = g_access(cp, 0, 1, 0);
  930. if (error != 0) {
  931. LOG_MSG(LVL_ERROR, "g_access(0,1,0) failed for %s: %d",
  932. cp->provider->name, error);
  933. return;
  934. }
  935. pp = cp->provider;
  936. buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
  937. bzero(buf, pp->sectorsize);
  938. virstor_metadata_encode(md, buf);
  939. g_topology_unlock();
  940. error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,
  941. pp->sectorsize);
  942. g_topology_lock();
  943. g_access(cp, 0, -1, 0);
  944. free(buf, M_GVIRSTOR);
  945. if (error != 0)
  946. LOG_MSG(LVL_ERROR, "Error %d writing metadata to %s",
  947. error, cp->provider->name);
  948. }
  949. /*
  950. * Creates a new instance of this GEOM class, initialise softc
  951. */
  952. static struct g_geom *
  953. create_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md)
  954. {
  955. struct g_geom *gp;
  956. struct g_virstor_softc *sc;
  957. LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)",
  958. md->md_name, md->md_id);
  959. if (md->md_count < 1 || md->md_chunk_size < 1 ||
  960. md->md_virsize < md->md_chunk_size) {
  961. /* This is bogus configuration, and probably means data is
  962. * somehow corrupted. Panic, maybe? */
  963. LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s",
  964. md->md_name);
  965. return (NULL);
  966. }
  967. /* Check if it's already created */
  968. LIST_FOREACH(gp, &mp->geom, geom) {
  969. sc = gp->softc;
  970. if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) {
  971. LOG_MSG(LVL_WARNING, "Geom %s already exists",
  972. md->md_name);
  973. if (sc->id != md->md_id) {
  974. LOG_MSG(LVL_ERROR,
  975. "Some stale or invalid components "
  976. "exist for virstor device named %s. "
  977. "You will need to <CLEAR> all stale "
  978. "components and maybe reconfigure "
  979. "the virstor device. Tune "
  980. "kern.geom.virstor.debug sysctl up "
  981. "for more information.",
  982. sc->geom->name);
  983. }
  984. return (NULL);
  985. }
  986. }
  987. gp = g_new_geomf(mp, "%s", md->md_name);
  988. gp->softc = NULL; /* to circumevent races that test softc */
  989. gp->start = g_virstor_start;
  990. gp->spoiled = g_virstor_orphan;
  991. gp->orphan = g_virstor_orphan;
  992. gp->access = g_virstor_access;
  993. gp->dumpconf = g_virstor_dumpconf;
  994. sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO);
  995. sc->id = md->md_id;
  996. sc->n_components = md->md_count;
  997. sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count,
  998. M_GVIRSTOR, M_WAITOK | M_ZERO);
  999. sc->chunk_size = md->md_chunk_size;
  1000. sc->virsize = md->md_virsize;
  1001. STAILQ_INIT(&sc->delayed_bio_q);
  1002. mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx",
  1003. "gvirstor", MTX_DEF | MTX_RECURSE);
  1004. sc->geom = gp;
  1005. sc->provider = NULL; /* virstor_check_and_run will create it */
  1006. gp->softc = sc;
  1007. LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name);
  1008. return (gp);
  1009. }
  1010. /*
  1011. * Add provider to a GEOM class instance
  1012. */
  1013. static int
  1014. add_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp,
  1015. struct g_virstor_metadata *md)
  1016. {
  1017. struct g_virstor_component *component;
  1018. struct g_consumer *cp, *fcp;
  1019. struct g_geom *gp;
  1020. int error;
  1021. if (md->no >= sc->n_components)
  1022. return (EINVAL);
  1023. /* "Current" compontent */
  1024. component = &(sc->components[md->no]);
  1025. if (component->gcons != NULL)
  1026. return (EEXIST);
  1027. gp = sc->geom;
  1028. fcp = LIST_FIRST(&gp->consumer);
  1029. cp = g_new_consumer(gp);
  1030. error = g_attach(cp, pp);
  1031. if (error != 0) {
  1032. g_destroy_consumer(cp);
  1033. return (error);
  1034. }
  1035. if (fcp != NULL) {
  1036. if (fcp->provider->sectorsize != pp->sectorsize) {
  1037. /* TODO: this can be made to work */
  1038. LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid "
  1039. "sector size (%d)", pp->name, sc->geom->name,
  1040. pp->sectorsize);
  1041. return (EINVAL);
  1042. }
  1043. if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
  1044. /* Replicate access permissions from first "live" consumer
  1045. * to the new one */
  1046. error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
  1047. if (error != 0) {
  1048. g_detach(cp);
  1049. g_destroy_consumer(cp);
  1050. return (error);
  1051. }
  1052. }
  1053. }
  1054. /* Bring up a new component */
  1055. cp->private = component;
  1056. component->gcons = cp;
  1057. component->sc = sc;
  1058. component->index = md->no;
  1059. component->chunk_count = md->chunk_count;
  1060. component->chunk_next = md->chunk_next;
  1061. component->chunk_reserved = md->chunk_reserved;
  1062. component->flags = md->flags;
  1063. LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name);
  1064. virstor_check_and_run(sc);
  1065. return (0);
  1066. }
  1067. /*
  1068. * Check if everything's ready to create the geom provider & device entry,
  1069. * create and start provider.
  1070. * Called ultimately by .taste, from g_event thread
  1071. */
  1072. static void
  1073. virstor_check_and_run(struct g_virstor_softc *sc)
  1074. {
  1075. off_t off;
  1076. size_t n, count;
  1077. int index;
  1078. int error;
  1079. if (virstor_valid_components(sc) != sc->n_components)
  1080. return;
  1081. if (virstor_valid_components(sc) == 0) {
  1082. /* This is actually a candidate for panic() */
  1083. LOG_MSG(LVL_ERROR, "No valid components for %s?",
  1084. sc->provider->name);
  1085. return;
  1086. }
  1087. sc->sectorsize = sc->components[0].gcons->provider->sectorsize;
  1088. /* Initialise allocation map from the first consumer */
  1089. sc->chunk_count = sc->virsize / sc->chunk_size;
  1090. if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) {
  1091. LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes",
  1092. sc->provider->name,
  1093. sc->chunk_count * (off_t)sc->chunk_size);
  1094. }
  1095. sc->map_size = sc->chunk_count * sizeof *(sc->map);
  1096. /* The following allocation is in order of 4MB - 8MB */
  1097. sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK);
  1098. KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s",
  1099. __func__, sc->map_size, sc->provider->name));
  1100. sc->map_sectors = sc->map_size / sc->sectorsize;
  1101. count = 0;
  1102. for (n = 0; n < sc->n_components; n++)
  1103. count += sc->components[n].chunk_count;
  1104. LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual "
  1105. "(%zu KB chunks)",
  1106. sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024);
  1107. error = g_access(sc->components[0].gcons, 1, 0, 0);
  1108. if (error != 0) {
  1109. LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to "
  1110. "read allocation map for %s",
  1111. sc->components[0].gcons->provider->name,
  1112. sc->geom->name);
  1113. return;
  1114. }
  1115. /* Read in the allocation map */
  1116. LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name,
  1117. sc->components[0].gcons->provider->name);
  1118. off = count = n = 0;
  1119. while (count < sc->map_size) {
  1120. struct g_virstor_map_entry *mapbuf;
  1121. size_t bs;
  1122. bs = MIN(maxphys, sc->map_size - count);
  1123. if (bs % sc->sectorsize != 0) {
  1124. /* Check for alignment errors */
  1125. bs = rounddown(bs, sc->sectorsize);
  1126. if (bs == 0)
  1127. break;
  1128. LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned "
  1129. "for %s on %s", sc->geom->name,
  1130. sc->components[0].gcons->provider->name);
  1131. }
  1132. mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error);
  1133. if (mapbuf == NULL) {
  1134. free(sc->map, M_GVIRSTOR);
  1135. LOG_MSG(LVL_ERROR, "Error reading allocation map "
  1136. "for %s from %s (offset %ju) (error %d)",
  1137. sc->geom->name,
  1138. sc->components[0].gcons->provider->name,
  1139. off, error);
  1140. return;
  1141. }
  1142. bcopy(mapbuf, &sc->map[n], bs);
  1143. off += bs;
  1144. count += bs;
  1145. n += bs / sizeof *(sc->map);
  1146. g_free(mapbuf);
  1147. }
  1148. g_access(sc->components[0].gcons, -1, 0, 0);
  1149. LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name);
  1150. /* find first component with allocatable chunks */
  1151. index = -1;
  1152. for (n = 0; n < sc->n_components; n++) {
  1153. if (sc->components[n].chunk_next <
  1154. sc->components[n].chunk_count) {
  1155. index = n;
  1156. break;
  1157. }
  1158. }
  1159. if (index == -1)
  1160. /* not found? set it to the last component and handle it
  1161. * later */
  1162. index = sc->n_components - 1;
  1163. if (index >= sc->n_components - g_virstor_component_watermark - 1) {
  1164. LOG_MSG(LVL_WARNING, "Device %s running out of components "
  1165. "(%d/%u: %s)", sc->geom->name,
  1166. index+1,
  1167. sc->n_components,
  1168. sc->components[index].gcons->provider->name);
  1169. }
  1170. sc->curr_component = index;
  1171. if (sc->components[index].chunk_next >=
  1172. sc->components[index].chunk_count - g_virstor_chunk_watermark) {
  1173. LOG_MSG(LVL_WARNING,
  1174. "Component %s of %s is running out of free space "
  1175. "(%u chunks left)",
  1176. sc->components[index].gcons->provider->name,
  1177. sc->geom->name, sc->components[index].chunk_count -
  1178. sc->components[index].chunk_next);
  1179. }
  1180. sc->me_per_sector = sc->sectorsize / sizeof *(sc->map);
  1181. if (sc->sectorsize % sizeof *(sc->map) != 0) {
  1182. LOG_MSG(LVL_ERROR,
  1183. "%s: Map entries don't fit exactly in a sector (%s)",
  1184. __func__, sc->geom->name);
  1185. return;
  1186. }
  1187. /* Recalculate allocated chunks in components & at the same time
  1188. * verify map data is sane. We could trust metadata on this, but
  1189. * we want to make sure. */
  1190. for (n = 0; n < sc->n_components; n++)
  1191. sc->components[n].chunk_next = sc->components[n].chunk_reserved;
  1192. for (n = 0; n < sc->chunk_count; n++) {
  1193. if (sc->map[n].provider_no >= sc->n_components ||
  1194. sc->map[n].provider_chunk >=
  1195. sc->components[sc->map[n].provider_no].chunk_count) {
  1196. LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s",
  1197. __func__, (u_int)n, sc->geom->name);
  1198. LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u"
  1199. " provider_chunk: %u, chunk_count: %u", __func__,
  1200. sc->map[n].provider_no, sc->n_components,
  1201. sc->map[n].provider_chunk,
  1202. sc->components[sc->map[n].provider_no].chunk_count);
  1203. return;
  1204. }
  1205. if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED)
  1206. sc->components[sc->map[n].provider_no].chunk_next++;
  1207. }
  1208. sc->provider = g_new_providerf(sc->geom, "virstor/%s",
  1209. sc->geom->name);
  1210. sc->provider->sectorsize = sc->sectorsize;
  1211. sc->provider->mediasize = sc->virsize;
  1212. g_error_provider(sc->provider, 0);
  1213. LOG_MSG(LVL_INFO, "%s activated", sc->provider->name);
  1214. LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting "
  1215. "chunk %u", sc->provider->name, sc->curr_component,
  1216. sc->components[sc->curr_component].chunk_next);
  1217. }
  1218. /*
  1219. * Returns count of active providers in this geom instance
  1220. */
  1221. static u_int
  1222. virstor_valid_components(struct g_virstor_softc *sc)
  1223. {
  1224. unsigned int nc, i;
  1225. nc = 0;
  1226. KASSERT(sc != NULL, ("%s: softc is NULL", __func__));
  1227. KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__));
  1228. for (i = 0; i < sc->n_components; i++)
  1229. if (sc->components[i].gcons != NULL)
  1230. nc++;
  1231. return (nc);
  1232. }
  1233. /*
  1234. * Called when the consumer gets orphaned (?)
  1235. */
  1236. static void
  1237. g_virstor_orphan(struct g_consumer *cp)
  1238. {
  1239. struct g_virstor_softc *sc;
  1240. struct g_virstor_component *comp;
  1241. struct g_geom *gp;
  1242. g_topology_assert();
  1243. gp = cp->geom;
  1244. sc = gp->softc;
  1245. if (sc == NULL)
  1246. return;
  1247. comp = cp->private;
  1248. KASSERT(comp != NULL, ("%s: No component in private part of consumer",
  1249. __func__));
  1250. remove_component(sc, comp, FALSE);
  1251. if (LIST_EMPTY(&gp->consumer))
  1252. virstor_geom_destroy(sc, TRUE, FALSE);
  1253. }
  1254. /*
  1255. * Called to notify geom when it's been opened, and for what intent
  1256. */
  1257. static int
  1258. g_virstor_access(struct g_provider *pp, int dr, int dw, int de)
  1259. {
  1260. struct g_consumer *c, *c2, *tmp;
  1261. struct g_virstor_softc *sc;
  1262. struct g_geom *gp;
  1263. int error;
  1264. KASSERT(pp != NULL, ("%s: NULL provider", __func__));
  1265. gp = pp->geom;
  1266. KASSERT(gp != NULL, ("%s: NULL geom", __func__));
  1267. sc = gp->softc;
  1268. /* Grab an exclusive bit to propagate on our consumers on first open */
  1269. if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
  1270. de++;
  1271. /* ... drop it on close */
  1272. if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) {
  1273. de--;
  1274. if (sc != NULL)
  1275. update_metadata(sc);
  1276. }
  1277. error = ENXIO;
  1278. LIST_FOREACH_SAFE(c, &gp->consumer, consumer, tmp) {
  1279. error = g_access(c, dr, dw, de);
  1280. if (error != 0)
  1281. goto fail;
  1282. if (c->acr == 0 && c->acw == 0 && c->ace == 0 &&
  1283. c->flags & G_CF_ORPHAN) {
  1284. g_detach(c);
  1285. g_destroy_consumer(c);
  1286. }
  1287. }
  1288. if (sc != NULL && LIST_EMPTY(&gp->consumer))
  1289. virstor_geom_destroy(sc, TRUE, FALSE);
  1290. return (error);
  1291. fail:
  1292. /* Backout earlier changes */
  1293. LIST_FOREACH(c2, &gp->consumer, consumer) {
  1294. if (c2 == c)
  1295. break;
  1296. g_access(c2, -dr, -dw, -de);
  1297. }
  1298. return (error);
  1299. }
  1300. /*
  1301. * Generate XML dump of current state
  1302. */
  1303. static void
  1304. g_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
  1305. struct g_consumer *cp, struct g_provider *pp)
  1306. {
  1307. struct g_virstor_softc *sc;
  1308. g_topology_assert();
  1309. sc = gp->softc;
  1310. if (sc == NULL || pp != NULL)
  1311. return;
  1312. if (cp != NULL) {
  1313. /* For each component */
  1314. struct g_virstor_component *comp;
  1315. comp = cp->private;
  1316. if (comp == NULL)
  1317. return;
  1318. sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n",
  1319. indent, comp->index);
  1320. sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n",
  1321. indent, comp->chunk_count);
  1322. sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n",
  1323. indent, comp->chunk_next);
  1324. sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n",
  1325. indent, comp->chunk_reserved);
  1326. sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n",
  1327. indent,
  1328. comp->chunk_next > 0 ? 100 -
  1329. ((comp->chunk_next + comp->chunk_reserved) * 100) /
  1330. comp->chunk_count : 100);
  1331. } else {
  1332. /* For the whole thing */
  1333. u_int count, used, i;
  1334. off_t size;
  1335. count = used = size = 0;
  1336. for (i = 0; i < sc->n_components; i++) {
  1337. if (sc->components[i].gcons != NULL) {
  1338. count += sc->components[i].chunk_count;
  1339. used += sc->components[i].chunk_next +
  1340. sc->components[i].chunk_reserved;
  1341. size += sc->components[i].gcons->
  1342. provider->mediasize;
  1343. }
  1344. }
  1345. sbuf_printf(sb, "%s<Status>"
  1346. "Components=%u, Online=%u</Status>\n", indent,
  1347. sc->n_components, virstor_valid_components(sc));
  1348. sbuf_printf(sb, "%s<State>%u%% physical free</State>\n",
  1349. indent, 100-(used * 100) / count);
  1350. sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent,
  1351. sc->chunk_size);
  1352. sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n",
  1353. indent, used > 0 ? 100 - (used * 100) / count : 100);
  1354. sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n",
  1355. indent, count);
  1356. sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n",
  1357. indent, sc->chunk_count);
  1358. sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n",
  1359. indent,
  1360. (count * 100) / sc->chunk_count);
  1361. sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n",
  1362. indent, size);
  1363. sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent,
  1364. sc->virsize);
  1365. }
  1366. }
  1367. /*
  1368. * GEOM .done handler
  1369. * Can't use standard handler because one requested IO may
  1370. * fork into additional data IOs
  1371. */
  1372. static void
  1373. g_virstor_done(struct bio *b)
  1374. {
  1375. struct bio *parent_b;
  1376. parent_b = b->bio_parent;
  1377. if (b->bio_error != 0) {
  1378. LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s",
  1379. b->bio_error, b->bio_offset, b->bio_length,
  1380. b->bio_to->name);
  1381. if (parent_b->bio_error == 0)
  1382. parent_b->bio_error = b->bio_error;
  1383. }
  1384. parent_b->bio_inbed++;
  1385. parent_b->bio_completed += b->bio_completed;
  1386. if (parent_b->bio_children == parent_b->bio_inbed) {
  1387. parent_b->bio_completed = parent_b->bio_length;
  1388. g_io_deliver(parent_b, parent_b->bio_error);
  1389. }
  1390. g_destroy_bio(b);
  1391. }
  1392. /*
  1393. * I/O starts here
  1394. * Called in g_down thread
  1395. */
  1396. static void
  1397. g_virstor_start(struct bio *b)
  1398. {
  1399. struct g_virstor_softc *sc;
  1400. struct g_virstor_component *comp;
  1401. struct bio *cb;
  1402. struct g_provider *pp;
  1403. char *addr;
  1404. off_t offset, length;
  1405. struct bio_queue_head bq;
  1406. size_t chunk_size; /* cached for convenience */
  1407. u_int count;
  1408. pp = b->bio_to;
  1409. sc = pp->geom->softc;
  1410. KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__,
  1411. b->bio_to->error, b->bio_to->name));
  1412. LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__);
  1413. switch (b->bio_cmd) {
  1414. case BIO_READ:
  1415. case BIO_WRITE:
  1416. case BIO_DELETE:
  1417. break;
  1418. default:
  1419. g_io_deliver(b, EOPNOTSUPP);
  1420. return;
  1421. }
  1422. LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length);
  1423. bioq_init(&bq);
  1424. chunk_size = sc->chunk_size;
  1425. addr = b->bio_data;
  1426. offset = b->bio_offset; /* virtual offset and length */
  1427. length = b->bio_length;
  1428. while (length > 0) {
  1429. size_t chunk_index, in_chunk_offset, in_chunk_length;
  1430. struct virstor_map_entry *me;
  1431. chunk_index = offset / chunk_size; /* round downwards */
  1432. in_chunk_offset = offset % chunk_size;
  1433. in_chunk_length = min(length, chunk_size - in_chunk_offset);
  1434. LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)",
  1435. b->bio_cmd == BIO_READ ? "R" : "W",
  1436. offset, length,
  1437. chunk_index, in_chunk_offset, in_chunk_length);
  1438. me = &sc->map[chunk_index];
  1439. if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) {
  1440. if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
  1441. /* Reads from unallocated chunks return zeroed
  1442. * buffers */
  1443. if (b->bio_cmd == BIO_READ)
  1444. bzero(addr, in_chunk_length);
  1445. } else {
  1446. comp = &sc->components[me->provider_no];
  1447. cb = g_clone_bio(b);
  1448. if (cb == NULL) {
  1449. bioq_dismantle(&bq);
  1450. if (b->bio_error == 0)
  1451. b->bio_error = ENOMEM;
  1452. g_io_deliver(b, b->bio_error);
  1453. return;
  1454. }
  1455. cb->bio_to = comp->gcons->provider;
  1456. cb->bio_done = g_virstor_done;
  1457. cb->bio_offset =
  1458. (off_t)me->provider_chunk * (off_t)chunk_size
  1459. + in_chunk_offset;
  1460. cb->bio_length = in_chunk_length;
  1461. cb->bio_data = addr;
  1462. cb->bio_caller1 = comp;
  1463. bioq_disksort(&bq, cb);
  1464. }
  1465. } else { /* handle BIO_WRITE */
  1466. KASSERT(b->bio_cmd == BIO_WRITE,
  1467. ("%s: Unknown command %d", __func__,
  1468. b->bio_cmd));
  1469. if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
  1470. /* We have a virtual chunk, represented by
  1471. * the "me" entry, but it's not yet allocated
  1472. * (tied to) a physical chunk. So do it now. */
  1473. struct virstor_map_entry *data_me;
  1474. u_int phys_chunk, comp_no;
  1475. off_t s_offset;
  1476. int error;
  1477. error = allocate_chunk(sc, &comp, &comp_no,
  1478. &phys_chunk);
  1479. if (error != 0) {
  1480. /* We cannot allocate a physical chunk
  1481. * to satisfy this request, so we'll
  1482. * delay it to when we can...
  1483. * XXX: this will prevent the fs from
  1484. * being umounted! */
  1485. struct g_virstor_bio_q *biq;
  1486. biq = malloc(sizeof *biq, M_GVIRSTOR,
  1487. M_NOWAIT);
  1488. if (biq == NULL) {
  1489. bioq_dismantle(&bq);
  1490. if (b->bio_error == 0)
  1491. b->bio_error = ENOMEM;
  1492. g_io_deliver(b, b->bio_error);
  1493. return;
  1494. }
  1495. biq->bio = b;
  1496. mtx_lock(&sc->delayed_bio_q_mtx);
  1497. STAILQ_INSERT_TAIL(&sc->delayed_bio_q,
  1498. biq, linkage);
  1499. mtx_unlock(&sc->delayed_bio_q_mtx);
  1500. LOG_MSG(LVL_WARNING, "Delaying BIO "
  1501. "(size=%ju) until free physical "
  1502. "space can be found on %s",
  1503. b->bio_length,
  1504. sc->provider->name);
  1505. return;
  1506. }
  1507. LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s "
  1508. "for %s",
  1509. phys_chunk,
  1510. comp->gcons->provider->name,
  1511. sc->provider->name);
  1512. me->provider_no = comp_no;
  1513. me->provider_chunk = phys_chunk;
  1514. me->flags |= VIRSTOR_MAP_ALLOCATED;
  1515. cb = g_clone_bio(b);
  1516. if (cb == NULL) {
  1517. me->flags &= ~VIRSTOR_MAP_ALLOCATED;
  1518. me->provider_no = 0;
  1519. me->provider_chunk = 0;
  1520. bioq_dismantle(&bq);
  1521. if (b->bio_error == 0)
  1522. b->bio_error = ENOMEM;
  1523. g_io_deliver(b, b->bio_error);
  1524. return;
  1525. }
  1526. /* The allocation table is stored continuously
  1527. * at the start of the drive. We need to
  1528. * calculate the offset of the sector that holds
  1529. * this map entry both on the drive and in the
  1530. * map array.
  1531. * sc_offset will end up pointing to the drive
  1532. * sector. */
  1533. s_offset = chunk_index * sizeof *me;
  1534. s_offset = rounddown(s_offset, sc->sectorsize);
  1535. /* data_me points to map entry sector
  1536. * in memory (analogous to offset) */
  1537. data_me = &sc->map[rounddown(chunk_index,
  1538. sc->me_per_sector)];
  1539. /* Commit sector with map entry to storage */
  1540. cb->bio_to = sc->components[0].gcons->provider;
  1541. cb->bio_done = g_virstor_done;
  1542. cb->bio_offset = s_offset;
  1543. cb->bio_data = (char *)data_me;
  1544. cb->bio_length = sc->sectorsize;
  1545. cb->bio_caller1 = &sc->components[0];
  1546. bioq_disksort(&bq, cb);
  1547. }
  1548. comp = &sc->components[me->provider_no];
  1549. cb = g_clone_bio(b);
  1550. if (cb == NULL) {
  1551. bioq_dismantle(&bq);
  1552. if (b->bio_error == 0)
  1553. b->bio_error = ENOMEM;
  1554. g_io_deliver(b, b->bio_error);
  1555. return;
  1556. }
  1557. /* Finally, handle the data */
  1558. cb->bio_to = comp->gcons->provider;
  1559. cb->bio_done = g_virstor_done;
  1560. cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size +
  1561. in_chunk_offset;
  1562. cb->bio_length = in_chunk_length;
  1563. cb->bio_data = addr;
  1564. cb->bio_caller1 = comp;
  1565. bioq_disksort(&bq, cb);
  1566. }
  1567. addr += in_chunk_length;
  1568. length -= in_chunk_length;
  1569. offset += in_chunk_length;
  1570. }
  1571. /* Fire off bio's here */
  1572. count = 0;
  1573. for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
  1574. bioq_remove(&bq, cb);
  1575. LOG_REQ(LVL_MOREDEBUG, cb, "Firing request");
  1576. comp = cb->bio_caller1;
  1577. cb->bio_caller1 = NULL;
  1578. LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju",
  1579. cb->bio_offset, cb->bio_length);
  1580. g_io_request(cb, comp->gcons);
  1581. count++;
  1582. }
  1583. if (count == 0) { /* We handled everything locally */
  1584. b->bio_completed = b->bio_length;
  1585. g_io_deliver(b, 0);
  1586. }
  1587. }
  1588. /*
  1589. * Allocate a chunk from a physical provider. Returns physical component,
  1590. * chunk index relative to the component and the component's index.
  1591. */
  1592. static int
  1593. allocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp,
  1594. u_int *comp_no_p, u_int *chunk)
  1595. {
  1596. u_int comp_no;
  1597. KASSERT(sc->curr_component < sc->n_components,
  1598. ("%s: Invalid curr_component: %u", __func__, sc->curr_component));
  1599. comp_no = sc->curr_component;
  1600. *comp = &sc->components[comp_no];
  1601. dump_component(*comp);
  1602. if ((*comp)->chunk_next >= (*comp)->chunk_count) {
  1603. /* This component is full. Allocate next component */
  1604. if (comp_no >= sc->n_components-1) {
  1605. LOG_MSG(LVL_ERROR, "All physical space allocated for %s",
  1606. sc->geom->name);
  1607. return (-1);
  1608. }
  1609. (*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT;
  1610. sc->curr_component = ++comp_no;
  1611. *comp = &sc->components[comp_no];
  1612. if (comp_no >= sc->n_components - g_virstor_component_watermark-1)
  1613. LOG_MSG(LVL_WARNING, "Device %s running out of components "
  1614. "(switching to %u/%u: %s)", sc->geom->name,
  1615. comp_no+1, sc->n_components,
  1616. (*comp)->gcons->provider->name);
  1617. /* Take care not to overwrite reserved chunks */
  1618. if ( (*comp)->chunk_reserved > 0 &&
  1619. (*comp)->chunk_next < (*comp)->chunk_reserved)
  1620. (*comp)->chunk_next = (*comp)->chunk_reserved;
  1621. (*comp)->flags |=
  1622. VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT;
  1623. dump_component(*comp);
  1624. *comp_no_p = comp_no;
  1625. *chunk = (*comp)->chunk_next++;
  1626. } else {
  1627. *comp_no_p = comp_no;
  1628. *chunk = (*comp)->chunk_next++;
  1629. }
  1630. return (0);
  1631. }
  1632. /* Dump a component */
  1633. static void
  1634. dump_component(struct g_virstor_component *comp)
  1635. {
  1636. if (g_virstor_debug < LVL_DEBUG2)
  1637. return;
  1638. printf("Component %d: %s\n", comp->index, comp->gcons->provider->name);
  1639. printf(" chunk_count: %u\n", comp->chunk_count);
  1640. printf(" chunk_next: %u\n", comp->chunk_next);
  1641. printf(" flags: %u\n", comp->flags);
  1642. }
  1643. #if 0
  1644. /* Dump a map entry */
  1645. static void
  1646. dump_me(struct virstor_map_entry *me, unsigned int nr)
  1647. {
  1648. if (g_virstor_debug < LVL_DEBUG)
  1649. return;
  1650. printf("VIRT. CHUNK #%d: ", nr);
  1651. if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0)
  1652. printf("(unallocated)\n");
  1653. else
  1654. printf("allocated at provider %u, provider_chunk %u\n",
  1655. me->provider_no, me->provider_chunk);
  1656. }
  1657. #endif
  1658. /*
  1659. * Dismantle bio_queue and destroy its components
  1660. */
  1661. static void
  1662. bioq_dismantle(struct bio_queue_head *bq)
  1663. {
  1664. struct bio *b;
  1665. for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
  1666. bioq_remove(bq, b);
  1667. g_destroy_bio(b);
  1668. }
  1669. }
  1670. /*
  1671. * The function that shouldn't be called.
  1672. * When this is called, the stack is already garbled because of
  1673. * argument mismatch. There's nothing to do now but panic, which is
  1674. * accidentally the whole purpose of this function.
  1675. * Motivation: to guard from accidentally calling geom methods when
  1676. * they shouldn't be called. (see g_..._taste)
  1677. */
  1678. static void
  1679. invalid_call(void)
  1680. {
  1681. panic("invalid_call() has just been called. Something's fishy here.");
  1682. }
  1683. DECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */
  1684. MODULE_VERSION(geom_virstor, 0);