ore_raid.c 19 KB

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