ore_raid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Copyright (C) 2011
  3. * Boaz Harrosh <ooo@electrozaur.com>
  4. *
  5. * This file is part of the objects raid engine (ore).
  6. *
  7. * It is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with "ore". If not, write to the Free Software Foundation, Inc:
  13. * "Free Software Foundation <info@fsf.org>"
  14. */
  15. #include <linux/gfp.h>
  16. #include <linux/async_tx.h>
  17. #include "ore_raid.h"
  18. #undef ORE_DBGMSG2
  19. #define ORE_DBGMSG2 ORE_DBGMSG
  20. static struct page *_raid_page_alloc(void)
  21. {
  22. return alloc_page(GFP_KERNEL);
  23. }
  24. static void _raid_page_free(struct page *p)
  25. {
  26. __free_page(p);
  27. }
  28. /* This struct is forward declare in ore_io_state, but is private to here.
  29. * It is put on ios->sp2d for RAID5/6 writes only. See _gen_xor_unit.
  30. *
  31. * __stripe_pages_2d is a 2d array of pages, and it is also a corner turn.
  32. * Ascending page index access is sp2d(p-minor, c-major). But storage is
  33. * sp2d[p-minor][c-major], so it can be properlly presented to the async-xor
  34. * API.
  35. */
  36. struct __stripe_pages_2d {
  37. /* Cache some hot path repeated calculations */
  38. unsigned parity;
  39. unsigned data_devs;
  40. unsigned pages_in_unit;
  41. bool needed ;
  42. /* Array size is pages_in_unit (layout->stripe_unit / PAGE_SIZE) */
  43. struct __1_page_stripe {
  44. bool alloc;
  45. unsigned write_count;
  46. struct async_submit_ctl submit;
  47. struct dma_async_tx_descriptor *tx;
  48. /* The size of this array is data_devs + parity */
  49. struct page **pages;
  50. struct page **scribble;
  51. /* bool array, size of this array is data_devs */
  52. char *page_is_read;
  53. } _1p_stripes[];
  54. };
  55. /* This can get bigger then a page. So support multiple page allocations
  56. * _sp2d_free should be called even if _sp2d_alloc fails (by returning
  57. * none-zero).
  58. */
  59. static int _sp2d_alloc(unsigned pages_in_unit, unsigned group_width,
  60. unsigned parity, struct __stripe_pages_2d **psp2d)
  61. {
  62. struct __stripe_pages_2d *sp2d;
  63. unsigned data_devs = group_width - parity;
  64. /*
  65. * Desired allocation layout is, though when larger than PAGE_SIZE,
  66. * each struct __alloc_1p_arrays is separately allocated:
  67. struct _alloc_all_bytes {
  68. struct __alloc_stripe_pages_2d {
  69. struct __stripe_pages_2d sp2d;
  70. struct __1_page_stripe _1p_stripes[pages_in_unit];
  71. } __asp2d;
  72. struct __alloc_1p_arrays {
  73. struct page *pages[group_width];
  74. struct page *scribble[group_width];
  75. char page_is_read[data_devs];
  76. } __a1pa[pages_in_unit];
  77. } *_aab;
  78. struct __alloc_1p_arrays *__a1pa;
  79. struct __alloc_1p_arrays *__a1pa_end;
  80. */
  81. char *__a1pa;
  82. char *__a1pa_end;
  83. const size_t sizeof_stripe_pages_2d =
  84. sizeof(struct __stripe_pages_2d) +
  85. sizeof(struct __1_page_stripe) * pages_in_unit;
  86. const size_t sizeof__a1pa =
  87. ALIGN(sizeof(struct page *) * (2 * group_width) + data_devs,
  88. sizeof(void *));
  89. const size_t sizeof__a1pa_arrays = sizeof__a1pa * pages_in_unit;
  90. const size_t alloc_total = sizeof_stripe_pages_2d +
  91. sizeof__a1pa_arrays;
  92. unsigned num_a1pa, alloc_size, i;
  93. /* FIXME: check these numbers in ore_verify_layout */
  94. BUG_ON(sizeof_stripe_pages_2d > PAGE_SIZE);
  95. BUG_ON(sizeof__a1pa > PAGE_SIZE);
  96. /*
  97. * If alloc_total would be larger than PAGE_SIZE, only allocate
  98. * as many a1pa items as would fill the rest of the page, instead
  99. * of the full pages_in_unit count.
  100. */
  101. if (alloc_total > PAGE_SIZE) {
  102. num_a1pa = (PAGE_SIZE - sizeof_stripe_pages_2d) / sizeof__a1pa;
  103. alloc_size = sizeof_stripe_pages_2d + sizeof__a1pa * num_a1pa;
  104. } else {
  105. num_a1pa = pages_in_unit;
  106. alloc_size = alloc_total;
  107. }
  108. *psp2d = sp2d = kzalloc(alloc_size, GFP_KERNEL);
  109. if (unlikely(!sp2d)) {
  110. ORE_DBGMSG("!! Failed to alloc sp2d size=%d\n", alloc_size);
  111. return -ENOMEM;
  112. }
  113. /* From here Just call _sp2d_free */
  114. /* Find start of a1pa area. */
  115. __a1pa = (char *)sp2d + sizeof_stripe_pages_2d;
  116. /* Find end of the _allocated_ a1pa area. */
  117. __a1pa_end = __a1pa + alloc_size;
  118. /* Allocate additionally needed a1pa items in PAGE_SIZE chunks. */
  119. for (i = 0; i < pages_in_unit; ++i) {
  120. struct __1_page_stripe *stripe = &sp2d->_1p_stripes[i];
  121. if (unlikely(__a1pa >= __a1pa_end)) {
  122. num_a1pa = min_t(unsigned, PAGE_SIZE / sizeof__a1pa,
  123. pages_in_unit - i);
  124. alloc_size = sizeof__a1pa * num_a1pa;
  125. __a1pa = kzalloc(alloc_size, GFP_KERNEL);
  126. if (unlikely(!__a1pa)) {
  127. ORE_DBGMSG("!! Failed to _alloc_1p_arrays=%d\n",
  128. num_a1pa);
  129. return -ENOMEM;
  130. }
  131. __a1pa_end = __a1pa + alloc_size;
  132. /* First *pages is marked for kfree of the buffer */
  133. stripe->alloc = true;
  134. }
  135. /*
  136. * Attach all _lp_stripes pointers to the allocation for
  137. * it which was either part of the original PAGE_SIZE
  138. * allocation or the subsequent allocation in this loop.
  139. */
  140. stripe->pages = (void *)__a1pa;
  141. stripe->scribble = stripe->pages + group_width;
  142. stripe->page_is_read = (char *)stripe->scribble + group_width;
  143. __a1pa += sizeof__a1pa;
  144. }
  145. sp2d->parity = parity;
  146. sp2d->data_devs = data_devs;
  147. sp2d->pages_in_unit = pages_in_unit;
  148. return 0;
  149. }
  150. static void _sp2d_reset(struct __stripe_pages_2d *sp2d,
  151. const struct _ore_r4w_op *r4w, void *priv)
  152. {
  153. unsigned data_devs = sp2d->data_devs;
  154. unsigned group_width = data_devs + sp2d->parity;
  155. int p, c;
  156. if (!sp2d->needed)
  157. return;
  158. for (c = data_devs - 1; c >= 0; --c)
  159. for (p = sp2d->pages_in_unit - 1; p >= 0; --p) {
  160. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  161. if (_1ps->page_is_read[c]) {
  162. struct page *page = _1ps->pages[c];
  163. r4w->put_page(priv, page);
  164. _1ps->page_is_read[c] = false;
  165. }
  166. }
  167. for (p = 0; p < sp2d->pages_in_unit; p++) {
  168. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  169. memset(_1ps->pages, 0, group_width * sizeof(*_1ps->pages));
  170. _1ps->write_count = 0;
  171. _1ps->tx = NULL;
  172. }
  173. sp2d->needed = false;
  174. }
  175. static void _sp2d_free(struct __stripe_pages_2d *sp2d)
  176. {
  177. unsigned i;
  178. if (!sp2d)
  179. return;
  180. for (i = 0; i < sp2d->pages_in_unit; ++i) {
  181. if (sp2d->_1p_stripes[i].alloc)
  182. kfree(sp2d->_1p_stripes[i].pages);
  183. }
  184. kfree(sp2d);
  185. }
  186. static unsigned _sp2d_min_pg(struct __stripe_pages_2d *sp2d)
  187. {
  188. unsigned p;
  189. for (p = 0; p < sp2d->pages_in_unit; p++) {
  190. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  191. if (_1ps->write_count)
  192. return p;
  193. }
  194. return ~0;
  195. }
  196. static unsigned _sp2d_max_pg(struct __stripe_pages_2d *sp2d)
  197. {
  198. int p;
  199. for (p = sp2d->pages_in_unit - 1; p >= 0; --p) {
  200. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  201. if (_1ps->write_count)
  202. return p;
  203. }
  204. return ~0;
  205. }
  206. static void _gen_xor_unit(struct __stripe_pages_2d *sp2d)
  207. {
  208. unsigned p;
  209. unsigned tx_flags = ASYNC_TX_ACK;
  210. if (sp2d->parity == 1)
  211. tx_flags |= ASYNC_TX_XOR_ZERO_DST;
  212. for (p = 0; p < sp2d->pages_in_unit; p++) {
  213. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  214. if (!_1ps->write_count)
  215. continue;
  216. init_async_submit(&_1ps->submit, tx_flags,
  217. NULL, NULL, NULL, (addr_conv_t *)_1ps->scribble);
  218. if (sp2d->parity == 1)
  219. _1ps->tx = async_xor(_1ps->pages[sp2d->data_devs],
  220. _1ps->pages, 0, sp2d->data_devs,
  221. PAGE_SIZE, &_1ps->submit);
  222. else /* parity == 2 */
  223. _1ps->tx = async_gen_syndrome(_1ps->pages, 0,
  224. sp2d->data_devs + sp2d->parity,
  225. PAGE_SIZE, &_1ps->submit);
  226. }
  227. for (p = 0; p < sp2d->pages_in_unit; p++) {
  228. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  229. /* NOTE: We wait for HW synchronously (I don't have such HW
  230. * to test with.) Is parallelism needed with today's multi
  231. * cores?
  232. */
  233. async_tx_issue_pending(_1ps->tx);
  234. }
  235. }
  236. void _ore_add_stripe_page(struct __stripe_pages_2d *sp2d,
  237. struct ore_striping_info *si, struct page *page)
  238. {
  239. struct __1_page_stripe *_1ps;
  240. sp2d->needed = true;
  241. _1ps = &sp2d->_1p_stripes[si->cur_pg];
  242. _1ps->pages[si->cur_comp] = page;
  243. ++_1ps->write_count;
  244. si->cur_pg = (si->cur_pg + 1) % sp2d->pages_in_unit;
  245. /* si->cur_comp is advanced outside at main loop */
  246. }
  247. void _ore_add_sg_seg(struct ore_per_dev_state *per_dev, unsigned cur_len,
  248. bool not_last)
  249. {
  250. struct osd_sg_entry *sge;
  251. ORE_DBGMSG("dev=%d cur_len=0x%x not_last=%d cur_sg=%d "
  252. "offset=0x%llx length=0x%x last_sgs_total=0x%x\n",
  253. per_dev->dev, cur_len, not_last, per_dev->cur_sg,
  254. _LLU(per_dev->offset), per_dev->length,
  255. per_dev->last_sgs_total);
  256. if (!per_dev->cur_sg) {
  257. sge = per_dev->sglist;
  258. /* First time we prepare two entries */
  259. if (per_dev->length) {
  260. ++per_dev->cur_sg;
  261. sge->offset = per_dev->offset;
  262. sge->len = per_dev->length;
  263. } else {
  264. /* Here the parity is the first unit of this object.
  265. * This happens every time we reach a parity device on
  266. * the same stripe as the per_dev->offset. We need to
  267. * just skip this unit.
  268. */
  269. per_dev->offset += cur_len;
  270. return;
  271. }
  272. } else {
  273. /* finalize the last one */
  274. sge = &per_dev->sglist[per_dev->cur_sg - 1];
  275. sge->len = per_dev->length - per_dev->last_sgs_total;
  276. }
  277. if (not_last) {
  278. /* Partly prepare the next one */
  279. struct osd_sg_entry *next_sge = sge + 1;
  280. ++per_dev->cur_sg;
  281. next_sge->offset = sge->offset + sge->len + cur_len;
  282. /* Save cur len so we know how mutch was added next time */
  283. per_dev->last_sgs_total = per_dev->length;
  284. next_sge->len = 0;
  285. } else if (!sge->len) {
  286. /* Optimize for when the last unit is a parity */
  287. --per_dev->cur_sg;
  288. }
  289. }
  290. static int _alloc_read_4_write(struct ore_io_state *ios)
  291. {
  292. struct ore_layout *layout = ios->layout;
  293. int ret;
  294. /* We want to only read those pages not in cache so worst case
  295. * is a stripe populated with every other page
  296. */
  297. unsigned sgs_per_dev = ios->sp2d->pages_in_unit + 2;
  298. ret = _ore_get_io_state(layout, ios->oc,
  299. layout->group_width * layout->mirrors_p1,
  300. sgs_per_dev, 0, &ios->ios_read_4_write);
  301. return ret;
  302. }
  303. /* @si contains info of the to-be-inserted page. Update of @si should be
  304. * maintained by caller. Specificaly si->dev, si->obj_offset, ...
  305. */
  306. static int _add_to_r4w(struct ore_io_state *ios, struct ore_striping_info *si,
  307. struct page *page, unsigned pg_len)
  308. {
  309. struct request_queue *q;
  310. struct ore_per_dev_state *per_dev;
  311. struct ore_io_state *read_ios;
  312. unsigned first_dev = si->dev - (si->dev %
  313. (ios->layout->group_width * ios->layout->mirrors_p1));
  314. unsigned comp = si->dev - first_dev;
  315. unsigned added_len;
  316. if (!ios->ios_read_4_write) {
  317. int ret = _alloc_read_4_write(ios);
  318. if (unlikely(ret))
  319. return ret;
  320. }
  321. read_ios = ios->ios_read_4_write;
  322. read_ios->numdevs = ios->layout->group_width * ios->layout->mirrors_p1;
  323. per_dev = &read_ios->per_dev[comp];
  324. if (!per_dev->length) {
  325. per_dev->bio = bio_kmalloc(GFP_KERNEL,
  326. ios->sp2d->pages_in_unit);
  327. if (unlikely(!per_dev->bio)) {
  328. ORE_DBGMSG("Failed to allocate BIO size=%u\n",
  329. ios->sp2d->pages_in_unit);
  330. return -ENOMEM;
  331. }
  332. per_dev->offset = si->obj_offset;
  333. per_dev->dev = si->dev;
  334. } else if (si->obj_offset != (per_dev->offset + per_dev->length)) {
  335. u64 gap = si->obj_offset - (per_dev->offset + per_dev->length);
  336. _ore_add_sg_seg(per_dev, gap, true);
  337. }
  338. q = osd_request_queue(ore_comp_dev(read_ios->oc, per_dev->dev));
  339. added_len = bio_add_pc_page(q, per_dev->bio, page, pg_len,
  340. si->obj_offset % PAGE_SIZE);
  341. if (unlikely(added_len != pg_len)) {
  342. ORE_DBGMSG("Failed to bio_add_pc_page bi_vcnt=%d\n",
  343. per_dev->bio->bi_vcnt);
  344. return -ENOMEM;
  345. }
  346. per_dev->length += pg_len;
  347. return 0;
  348. }
  349. /* read the beginning of an unaligned first page */
  350. static int _add_to_r4w_first_page(struct ore_io_state *ios, struct page *page)
  351. {
  352. struct ore_striping_info si;
  353. unsigned pg_len;
  354. ore_calc_stripe_info(ios->layout, ios->offset, 0, &si);
  355. pg_len = si.obj_offset % PAGE_SIZE;
  356. si.obj_offset -= pg_len;
  357. ORE_DBGMSG("offset=0x%llx len=0x%x index=0x%lx dev=%x\n",
  358. _LLU(si.obj_offset), pg_len, page->index, si.dev);
  359. return _add_to_r4w(ios, &si, page, pg_len);
  360. }
  361. /* read the end of an incomplete last page */
  362. static int _add_to_r4w_last_page(struct ore_io_state *ios, u64 *offset)
  363. {
  364. struct ore_striping_info si;
  365. struct page *page;
  366. unsigned pg_len, p, c;
  367. ore_calc_stripe_info(ios->layout, *offset, 0, &si);
  368. p = si.cur_pg;
  369. c = si.cur_comp;
  370. page = ios->sp2d->_1p_stripes[p].pages[c];
  371. pg_len = PAGE_SIZE - (si.unit_off % PAGE_SIZE);
  372. *offset += pg_len;
  373. ORE_DBGMSG("p=%d, c=%d next-offset=0x%llx len=0x%x dev=%x par_dev=%d\n",
  374. p, c, _LLU(*offset), pg_len, si.dev, si.par_dev);
  375. BUG_ON(!page);
  376. return _add_to_r4w(ios, &si, page, pg_len);
  377. }
  378. static void _mark_read4write_pages_uptodate(struct ore_io_state *ios, int ret)
  379. {
  380. struct bio_vec *bv;
  381. unsigned i, d;
  382. /* loop on all devices all pages */
  383. for (d = 0; d < ios->numdevs; d++) {
  384. struct bio *bio = ios->per_dev[d].bio;
  385. if (!bio)
  386. continue;
  387. bio_for_each_segment_all(bv, bio, i) {
  388. struct page *page = bv->bv_page;
  389. SetPageUptodate(page);
  390. if (PageError(page))
  391. ClearPageError(page);
  392. }
  393. }
  394. }
  395. /* read_4_write is hacked to read the start of the first stripe and/or
  396. * the end of the last stripe. If needed, with an sg-gap at each device/page.
  397. * It is assumed to be called after the to_be_written pages of the first stripe
  398. * are populating ios->sp2d[][]
  399. *
  400. * NOTE: We call ios->r4w->lock_fn for all pages needed for parity calculations
  401. * These pages are held at sp2d[p].pages[c] but with
  402. * sp2d[p].page_is_read[c] = true. At _sp2d_reset these pages are
  403. * ios->r4w->lock_fn(). The ios->r4w->lock_fn might signal that the page is
  404. * @uptodate=true, so we don't need to read it, only unlock, after IO.
  405. *
  406. * TODO: The read_4_write should calc a need_to_read_pages_count, if bigger then
  407. * to-be-written count, we should consider the xor-in-place mode.
  408. * need_to_read_pages_count is the actual number of pages not present in cache.
  409. * maybe "devs_in_group - ios->sp2d[p].write_count" is a good enough
  410. * approximation? In this mode the read pages are put in the empty places of
  411. * ios->sp2d[p][*], xor is calculated the same way. These pages are
  412. * allocated/freed and don't go through cache
  413. */
  414. static int _read_4_write_first_stripe(struct ore_io_state *ios)
  415. {
  416. struct ore_striping_info read_si;
  417. struct __stripe_pages_2d *sp2d = ios->sp2d;
  418. u64 offset = ios->si.first_stripe_start;
  419. unsigned c, p, min_p = sp2d->pages_in_unit, max_p = -1;
  420. if (offset == ios->offset) /* Go to start collect $200 */
  421. goto read_last_stripe;
  422. min_p = _sp2d_min_pg(sp2d);
  423. max_p = _sp2d_max_pg(sp2d);
  424. ORE_DBGMSG("stripe_start=0x%llx ios->offset=0x%llx min_p=%d max_p=%d\n",
  425. offset, ios->offset, min_p, max_p);
  426. for (c = 0; ; c++) {
  427. ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
  428. read_si.obj_offset += min_p * PAGE_SIZE;
  429. offset += min_p * PAGE_SIZE;
  430. for (p = min_p; p <= max_p; p++) {
  431. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  432. struct page **pp = &_1ps->pages[c];
  433. bool uptodate;
  434. if (*pp) {
  435. if (ios->offset % PAGE_SIZE)
  436. /* Read the remainder of the page */
  437. _add_to_r4w_first_page(ios, *pp);
  438. /* to-be-written pages start here */
  439. goto read_last_stripe;
  440. }
  441. *pp = ios->r4w->get_page(ios->private, offset,
  442. &uptodate);
  443. if (unlikely(!*pp))
  444. return -ENOMEM;
  445. if (!uptodate)
  446. _add_to_r4w(ios, &read_si, *pp, PAGE_SIZE);
  447. /* Mark read-pages to be cache_released */
  448. _1ps->page_is_read[c] = true;
  449. read_si.obj_offset += PAGE_SIZE;
  450. offset += PAGE_SIZE;
  451. }
  452. offset += (sp2d->pages_in_unit - p) * PAGE_SIZE;
  453. }
  454. read_last_stripe:
  455. return 0;
  456. }
  457. static int _read_4_write_last_stripe(struct ore_io_state *ios)
  458. {
  459. struct ore_striping_info read_si;
  460. struct __stripe_pages_2d *sp2d = ios->sp2d;
  461. u64 offset;
  462. u64 last_stripe_end;
  463. unsigned bytes_in_stripe = ios->si.bytes_in_stripe;
  464. unsigned c, p, min_p = sp2d->pages_in_unit, max_p = -1;
  465. offset = ios->offset + ios->length;
  466. if (offset % PAGE_SIZE)
  467. _add_to_r4w_last_page(ios, &offset);
  468. /* offset will be aligned to next page */
  469. last_stripe_end = div_u64(offset + bytes_in_stripe - 1, bytes_in_stripe)
  470. * bytes_in_stripe;
  471. if (offset == last_stripe_end) /* Optimize for the aligned case */
  472. goto read_it;
  473. ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
  474. p = read_si.cur_pg;
  475. c = read_si.cur_comp;
  476. if (min_p == sp2d->pages_in_unit) {
  477. /* Didn't do it yet */
  478. min_p = _sp2d_min_pg(sp2d);
  479. max_p = _sp2d_max_pg(sp2d);
  480. }
  481. ORE_DBGMSG("offset=0x%llx stripe_end=0x%llx min_p=%d max_p=%d\n",
  482. offset, last_stripe_end, min_p, max_p);
  483. while (offset < last_stripe_end) {
  484. struct __1_page_stripe *_1ps = &sp2d->_1p_stripes[p];
  485. if ((min_p <= p) && (p <= max_p)) {
  486. struct page *page;
  487. bool uptodate;
  488. BUG_ON(_1ps->pages[c]);
  489. page = ios->r4w->get_page(ios->private, offset,
  490. &uptodate);
  491. if (unlikely(!page))
  492. return -ENOMEM;
  493. _1ps->pages[c] = page;
  494. /* Mark read-pages to be cache_released */
  495. _1ps->page_is_read[c] = true;
  496. if (!uptodate)
  497. _add_to_r4w(ios, &read_si, page, PAGE_SIZE);
  498. }
  499. offset += PAGE_SIZE;
  500. if (p == (sp2d->pages_in_unit - 1)) {
  501. ++c;
  502. p = 0;
  503. ore_calc_stripe_info(ios->layout, offset, 0, &read_si);
  504. } else {
  505. read_si.obj_offset += PAGE_SIZE;
  506. ++p;
  507. }
  508. }
  509. read_it:
  510. return 0;
  511. }
  512. static int _read_4_write_execute(struct ore_io_state *ios)
  513. {
  514. struct ore_io_state *ios_read;
  515. unsigned i;
  516. int ret;
  517. ios_read = ios->ios_read_4_write;
  518. if (!ios_read)
  519. return 0;
  520. /* FIXME: Ugly to signal _sbi_read_mirror that we have bio(s). Change
  521. * to check for per_dev->bio
  522. */
  523. ios_read->pages = ios->pages;
  524. /* Now read these devices */
  525. for (i = 0; i < ios_read->numdevs; i += ios_read->layout->mirrors_p1) {
  526. ret = _ore_read_mirror(ios_read, i);
  527. if (unlikely(ret))
  528. return ret;
  529. }
  530. ret = ore_io_execute(ios_read); /* Synchronus execution */
  531. if (unlikely(ret)) {
  532. ORE_DBGMSG("!! ore_io_execute => %d\n", ret);
  533. return ret;
  534. }
  535. _mark_read4write_pages_uptodate(ios_read, ret);
  536. ore_put_io_state(ios_read);
  537. ios->ios_read_4_write = NULL; /* Might need a reuse at last stripe */
  538. return 0;
  539. }
  540. /* In writes @cur_len means length left. .i.e cur_len==0 is the last parity U */
  541. int _ore_add_parity_unit(struct ore_io_state *ios,
  542. struct ore_striping_info *si,
  543. struct ore_per_dev_state *per_dev,
  544. unsigned cur_len, bool do_xor)
  545. {
  546. if (ios->reading) {
  547. if (per_dev->cur_sg >= ios->sgs_per_dev) {
  548. ORE_DBGMSG("cur_sg(%d) >= sgs_per_dev(%d)\n" ,
  549. per_dev->cur_sg, ios->sgs_per_dev);
  550. return -ENOMEM;
  551. }
  552. _ore_add_sg_seg(per_dev, cur_len, true);
  553. } else {
  554. struct __stripe_pages_2d *sp2d = ios->sp2d;
  555. struct page **pages = ios->parity_pages + ios->cur_par_page;
  556. unsigned num_pages;
  557. unsigned array_start = 0;
  558. unsigned i;
  559. int ret;
  560. si->cur_pg = _sp2d_min_pg(sp2d);
  561. num_pages = _sp2d_max_pg(sp2d) + 1 - si->cur_pg;
  562. if (!per_dev->length) {
  563. per_dev->offset += si->cur_pg * PAGE_SIZE;
  564. /* If first stripe, Read in all read4write pages
  565. * (if needed) before we calculate the first parity.
  566. */
  567. if (do_xor)
  568. _read_4_write_first_stripe(ios);
  569. }
  570. if (!cur_len && do_xor)
  571. /* If last stripe r4w pages of last stripe */
  572. _read_4_write_last_stripe(ios);
  573. _read_4_write_execute(ios);
  574. for (i = 0; i < num_pages; i++) {
  575. pages[i] = _raid_page_alloc();
  576. if (unlikely(!pages[i]))
  577. return -ENOMEM;
  578. ++(ios->cur_par_page);
  579. }
  580. BUG_ON(si->cur_comp < sp2d->data_devs);
  581. BUG_ON(si->cur_pg + num_pages > sp2d->pages_in_unit);
  582. ret = _ore_add_stripe_unit(ios, &array_start, 0, pages,
  583. per_dev, num_pages * PAGE_SIZE);
  584. if (unlikely(ret))
  585. return ret;
  586. if (do_xor) {
  587. _gen_xor_unit(sp2d);
  588. _sp2d_reset(sp2d, ios->r4w, ios->private);
  589. }
  590. }
  591. return 0;
  592. }
  593. int _ore_post_alloc_raid_stuff(struct ore_io_state *ios)
  594. {
  595. if (ios->parity_pages) {
  596. struct ore_layout *layout = ios->layout;
  597. unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;
  598. if (_sp2d_alloc(pages_in_unit, layout->group_width,
  599. layout->parity, &ios->sp2d)) {
  600. return -ENOMEM;
  601. }
  602. }
  603. return 0;
  604. }
  605. void _ore_free_raid_stuff(struct ore_io_state *ios)
  606. {
  607. if (ios->sp2d) { /* writing and raid */
  608. unsigned i;
  609. for (i = 0; i < ios->cur_par_page; i++) {
  610. struct page *page = ios->parity_pages[i];
  611. if (page)
  612. _raid_page_free(page);
  613. }
  614. if (ios->extra_part_alloc)
  615. kfree(ios->parity_pages);
  616. /* If IO returned an error pages might need unlocking */
  617. _sp2d_reset(ios->sp2d, ios->r4w, ios->private);
  618. _sp2d_free(ios->sp2d);
  619. } else {
  620. /* Will only be set if raid reading && sglist is big */
  621. if (ios->extra_part_alloc)
  622. kfree(ios->per_dev[0].sglist);
  623. }
  624. if (ios->ios_read_4_write)
  625. ore_put_io_state(ios->ios_read_4_write);
  626. }