ore.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <bharrosh@panasas.com>
  6. *
  7. * This file is part of exofs.
  8. *
  9. * exofs is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation. Since it is based on ext2, and the only
  12. * valid version of GPL for the Linux kernel is version 2, the only valid
  13. * version of GPL for exofs is version 2.
  14. *
  15. * exofs is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with exofs; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <asm/div64.h>
  27. #include <linux/lcm.h>
  28. #include "ore_raid.h"
  29. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  30. MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
  31. MODULE_LICENSE("GPL");
  32. /* ore_verify_layout does a couple of things:
  33. * 1. Given a minimum number of needed parameters fixes up the rest of the
  34. * members to be operatonals for the ore. The needed parameters are those
  35. * that are defined by the pnfs-objects layout STD.
  36. * 2. Check to see if the current ore code actually supports these parameters
  37. * for example stripe_unit must be a multple of the system PAGE_SIZE,
  38. * and etc...
  39. * 3. Cache some havily used calculations that will be needed by users.
  40. */
  41. enum { BIO_MAX_PAGES_KMALLOC =
  42. (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),};
  43. int ore_verify_layout(unsigned total_comps, struct ore_layout *layout)
  44. {
  45. u64 stripe_length;
  46. switch (layout->raid_algorithm) {
  47. case PNFS_OSD_RAID_0:
  48. layout->parity = 0;
  49. break;
  50. case PNFS_OSD_RAID_5:
  51. layout->parity = 1;
  52. break;
  53. case PNFS_OSD_RAID_PQ:
  54. case PNFS_OSD_RAID_4:
  55. default:
  56. ORE_ERR("Only RAID_0/5 for now\n");
  57. return -EINVAL;
  58. }
  59. if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
  60. ORE_ERR("Stripe Unit(0x%llx)"
  61. " must be Multples of PAGE_SIZE(0x%lx)\n",
  62. _LLU(layout->stripe_unit), PAGE_SIZE);
  63. return -EINVAL;
  64. }
  65. if (layout->group_width) {
  66. if (!layout->group_depth) {
  67. ORE_ERR("group_depth == 0 && group_width != 0\n");
  68. return -EINVAL;
  69. }
  70. if (total_comps < (layout->group_width * layout->mirrors_p1)) {
  71. ORE_ERR("Data Map wrong, "
  72. "numdevs=%d < group_width=%d * mirrors=%d\n",
  73. total_comps, layout->group_width,
  74. layout->mirrors_p1);
  75. return -EINVAL;
  76. }
  77. layout->group_count = total_comps / layout->mirrors_p1 /
  78. layout->group_width;
  79. } else {
  80. if (layout->group_depth) {
  81. printk(KERN_NOTICE "Warning: group_depth ignored "
  82. "group_width == 0 && group_depth == %lld\n",
  83. _LLU(layout->group_depth));
  84. }
  85. layout->group_width = total_comps / layout->mirrors_p1;
  86. layout->group_depth = -1;
  87. layout->group_count = 1;
  88. }
  89. stripe_length = (u64)layout->group_width * layout->stripe_unit;
  90. if (stripe_length >= (1ULL << 32)) {
  91. ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
  92. _LLU(stripe_length));
  93. return -EINVAL;
  94. }
  95. layout->max_io_length =
  96. (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
  97. (layout->group_width - layout->parity);
  98. if (layout->parity) {
  99. unsigned stripe_length =
  100. (layout->group_width - layout->parity) *
  101. layout->stripe_unit;
  102. layout->max_io_length /= stripe_length;
  103. layout->max_io_length *= stripe_length;
  104. }
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(ore_verify_layout);
  108. static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
  109. {
  110. return ios->oc->comps[index & ios->oc->single_comp].cred;
  111. }
  112. static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
  113. {
  114. return &ios->oc->comps[index & ios->oc->single_comp].obj;
  115. }
  116. static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
  117. {
  118. ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
  119. ios->oc->first_dev, ios->oc->numdevs, index,
  120. ios->oc->ods);
  121. return ore_comp_dev(ios->oc, index);
  122. }
  123. int _ore_get_io_state(struct ore_layout *layout,
  124. struct ore_components *oc, unsigned numdevs,
  125. unsigned sgs_per_dev, unsigned num_par_pages,
  126. struct ore_io_state **pios)
  127. {
  128. struct ore_io_state *ios;
  129. struct page **pages;
  130. struct osd_sg_entry *sgilist;
  131. struct __alloc_all_io_state {
  132. struct ore_io_state ios;
  133. struct ore_per_dev_state per_dev[numdevs];
  134. union {
  135. struct osd_sg_entry sglist[sgs_per_dev * numdevs];
  136. struct page *pages[num_par_pages];
  137. };
  138. } *_aios;
  139. if (likely(sizeof(*_aios) <= PAGE_SIZE)) {
  140. _aios = kzalloc(sizeof(*_aios), GFP_KERNEL);
  141. if (unlikely(!_aios)) {
  142. ORE_DBGMSG("Failed kzalloc bytes=%zd\n",
  143. sizeof(*_aios));
  144. *pios = NULL;
  145. return -ENOMEM;
  146. }
  147. pages = num_par_pages ? _aios->pages : NULL;
  148. sgilist = sgs_per_dev ? _aios->sglist : NULL;
  149. ios = &_aios->ios;
  150. } else {
  151. struct __alloc_small_io_state {
  152. struct ore_io_state ios;
  153. struct ore_per_dev_state per_dev[numdevs];
  154. } *_aio_small;
  155. union __extra_part {
  156. struct osd_sg_entry sglist[sgs_per_dev * numdevs];
  157. struct page *pages[num_par_pages];
  158. } *extra_part;
  159. _aio_small = kzalloc(sizeof(*_aio_small), GFP_KERNEL);
  160. if (unlikely(!_aio_small)) {
  161. ORE_DBGMSG("Failed alloc first part bytes=%zd\n",
  162. sizeof(*_aio_small));
  163. *pios = NULL;
  164. return -ENOMEM;
  165. }
  166. extra_part = kzalloc(sizeof(*extra_part), GFP_KERNEL);
  167. if (unlikely(!extra_part)) {
  168. ORE_DBGMSG("Failed alloc second part bytes=%zd\n",
  169. sizeof(*extra_part));
  170. kfree(_aio_small);
  171. *pios = NULL;
  172. return -ENOMEM;
  173. }
  174. pages = num_par_pages ? extra_part->pages : NULL;
  175. sgilist = sgs_per_dev ? extra_part->sglist : NULL;
  176. /* In this case the per_dev[0].sgilist holds the pointer to
  177. * be freed
  178. */
  179. ios = &_aio_small->ios;
  180. ios->extra_part_alloc = true;
  181. }
  182. if (pages) {
  183. ios->parity_pages = pages;
  184. ios->max_par_pages = num_par_pages;
  185. }
  186. if (sgilist) {
  187. unsigned d;
  188. for (d = 0; d < numdevs; ++d) {
  189. ios->per_dev[d].sglist = sgilist;
  190. sgilist += sgs_per_dev;
  191. }
  192. ios->sgs_per_dev = sgs_per_dev;
  193. }
  194. ios->layout = layout;
  195. ios->oc = oc;
  196. *pios = ios;
  197. return 0;
  198. }
  199. /* Allocate an io_state for only a single group of devices
  200. *
  201. * If a user needs to call ore_read/write() this version must be used becase it
  202. * allocates extra stuff for striping and raid.
  203. * The ore might decide to only IO less then @length bytes do to alignmets
  204. * and constrains as follows:
  205. * - The IO cannot cross group boundary.
  206. * - In raid5/6 The end of the IO must align at end of a stripe eg.
  207. * (@offset + @length) % strip_size == 0. Or the complete range is within a
  208. * single stripe.
  209. * - Memory condition only permitted a shorter IO. (A user can use @length=~0
  210. * And check the returned ios->length for max_io_size.)
  211. *
  212. * The caller must check returned ios->length (and/or ios->nr_pages) and
  213. * re-issue these pages that fall outside of ios->length
  214. */
  215. int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
  216. bool is_reading, u64 offset, u64 length,
  217. struct ore_io_state **pios)
  218. {
  219. struct ore_io_state *ios;
  220. unsigned numdevs = layout->group_width * layout->mirrors_p1;
  221. unsigned sgs_per_dev = 0, max_par_pages = 0;
  222. int ret;
  223. if (layout->parity && length) {
  224. unsigned data_devs = layout->group_width - layout->parity;
  225. unsigned stripe_size = layout->stripe_unit * data_devs;
  226. unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;
  227. u32 remainder;
  228. u64 num_stripes;
  229. u64 num_raid_units;
  230. num_stripes = div_u64_rem(length, stripe_size, &remainder);
  231. if (remainder)
  232. ++num_stripes;
  233. num_raid_units = num_stripes * layout->parity;
  234. if (is_reading) {
  235. /* For reads add per_dev sglist array */
  236. /* TODO: Raid 6 we need twice more. Actually:
  237. * num_stripes / LCMdP(W,P);
  238. * if (W%P != 0) num_stripes *= parity;
  239. */
  240. /* first/last seg is split */
  241. num_raid_units += layout->group_width;
  242. sgs_per_dev = div_u64(num_raid_units, data_devs) + 2;
  243. } else {
  244. /* For Writes add parity pages array. */
  245. max_par_pages = num_raid_units * pages_in_unit *
  246. sizeof(struct page *);
  247. }
  248. }
  249. ret = _ore_get_io_state(layout, oc, numdevs, sgs_per_dev, max_par_pages,
  250. pios);
  251. if (unlikely(ret))
  252. return ret;
  253. ios = *pios;
  254. ios->reading = is_reading;
  255. ios->offset = offset;
  256. if (length) {
  257. ore_calc_stripe_info(layout, offset, length, &ios->si);
  258. ios->length = ios->si.length;
  259. ios->nr_pages = ((ios->offset & (PAGE_SIZE - 1)) +
  260. ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
  261. if (layout->parity)
  262. _ore_post_alloc_raid_stuff(ios);
  263. }
  264. return 0;
  265. }
  266. EXPORT_SYMBOL(ore_get_rw_state);
  267. /* Allocate an io_state for all the devices in the comps array
  268. *
  269. * This version of io_state allocation is used mostly by create/remove
  270. * and trunc where we currently need all the devices. The only wastful
  271. * bit is the read/write_attributes with no IO. Those sites should
  272. * be converted to use ore_get_rw_state() with length=0
  273. */
  274. int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
  275. struct ore_io_state **pios)
  276. {
  277. return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
  278. }
  279. EXPORT_SYMBOL(ore_get_io_state);
  280. void ore_put_io_state(struct ore_io_state *ios)
  281. {
  282. if (ios) {
  283. unsigned i;
  284. for (i = 0; i < ios->numdevs; i++) {
  285. struct ore_per_dev_state *per_dev = &ios->per_dev[i];
  286. if (per_dev->or)
  287. osd_end_request(per_dev->or);
  288. if (per_dev->bio)
  289. bio_put(per_dev->bio);
  290. }
  291. _ore_free_raid_stuff(ios);
  292. kfree(ios);
  293. }
  294. }
  295. EXPORT_SYMBOL(ore_put_io_state);
  296. static void _sync_done(struct ore_io_state *ios, void *p)
  297. {
  298. struct completion *waiting = p;
  299. complete(waiting);
  300. }
  301. static void _last_io(struct kref *kref)
  302. {
  303. struct ore_io_state *ios = container_of(
  304. kref, struct ore_io_state, kref);
  305. ios->done(ios, ios->private);
  306. }
  307. static void _done_io(struct osd_request *or, void *p)
  308. {
  309. struct ore_io_state *ios = p;
  310. kref_put(&ios->kref, _last_io);
  311. }
  312. int ore_io_execute(struct ore_io_state *ios)
  313. {
  314. DECLARE_COMPLETION_ONSTACK(wait);
  315. bool sync = (ios->done == NULL);
  316. int i, ret;
  317. if (sync) {
  318. ios->done = _sync_done;
  319. ios->private = &wait;
  320. }
  321. for (i = 0; i < ios->numdevs; i++) {
  322. struct osd_request *or = ios->per_dev[i].or;
  323. if (unlikely(!or))
  324. continue;
  325. ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
  326. if (unlikely(ret)) {
  327. ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
  328. ret);
  329. return ret;
  330. }
  331. }
  332. kref_init(&ios->kref);
  333. for (i = 0; i < ios->numdevs; i++) {
  334. struct osd_request *or = ios->per_dev[i].or;
  335. if (unlikely(!or))
  336. continue;
  337. kref_get(&ios->kref);
  338. osd_execute_request_async(or, _done_io, ios);
  339. }
  340. kref_put(&ios->kref, _last_io);
  341. ret = 0;
  342. if (sync) {
  343. wait_for_completion(&wait);
  344. ret = ore_check_io(ios, NULL);
  345. }
  346. return ret;
  347. }
  348. static void _clear_bio(struct bio *bio)
  349. {
  350. struct bio_vec *bv;
  351. unsigned i;
  352. __bio_for_each_segment(bv, bio, i, 0) {
  353. unsigned this_count = bv->bv_len;
  354. if (likely(PAGE_SIZE == this_count))
  355. clear_highpage(bv->bv_page);
  356. else
  357. zero_user(bv->bv_page, bv->bv_offset, this_count);
  358. }
  359. }
  360. int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
  361. {
  362. enum osd_err_priority acumulated_osd_err = 0;
  363. int acumulated_lin_err = 0;
  364. int i;
  365. for (i = 0; i < ios->numdevs; i++) {
  366. struct osd_sense_info osi;
  367. struct ore_per_dev_state *per_dev = &ios->per_dev[i];
  368. struct osd_request *or = per_dev->or;
  369. int ret;
  370. if (unlikely(!or))
  371. continue;
  372. ret = osd_req_decode_sense(or, &osi);
  373. if (likely(!ret))
  374. continue;
  375. if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
  376. /* start read offset passed endof file */
  377. _clear_bio(per_dev->bio);
  378. ORE_DBGMSG("start read offset passed end of file "
  379. "offset=0x%llx, length=0x%llx\n",
  380. _LLU(per_dev->offset),
  381. _LLU(per_dev->length));
  382. continue; /* we recovered */
  383. }
  384. if (on_dev_error) {
  385. u64 residual = ios->reading ?
  386. or->in.residual : or->out.residual;
  387. u64 offset = (ios->offset + ios->length) - residual;
  388. unsigned dev = per_dev->dev - ios->oc->first_dev;
  389. struct ore_dev *od = ios->oc->ods[dev];
  390. on_dev_error(ios, od, dev, osi.osd_err_pri,
  391. offset, residual);
  392. }
  393. if (osi.osd_err_pri >= acumulated_osd_err) {
  394. acumulated_osd_err = osi.osd_err_pri;
  395. acumulated_lin_err = ret;
  396. }
  397. }
  398. return acumulated_lin_err;
  399. }
  400. EXPORT_SYMBOL(ore_check_io);
  401. /*
  402. * L - logical offset into the file
  403. *
  404. * D - number of Data devices
  405. * D = group_width - parity
  406. *
  407. * U - The number of bytes in a stripe within a group
  408. * U = stripe_unit * D
  409. *
  410. * T - The number of bytes striped within a group of component objects
  411. * (before advancing to the next group)
  412. * T = U * group_depth
  413. *
  414. * S - The number of bytes striped across all component objects
  415. * before the pattern repeats
  416. * S = T * group_count
  417. *
  418. * M - The "major" (i.e., across all components) cycle number
  419. * M = L / S
  420. *
  421. * G - Counts the groups from the beginning of the major cycle
  422. * G = (L - (M * S)) / T [or (L % S) / T]
  423. *
  424. * H - The byte offset within the group
  425. * H = (L - (M * S)) % T [or (L % S) % T]
  426. *
  427. * N - The "minor" (i.e., across the group) stripe number
  428. * N = H / U
  429. *
  430. * C - The component index coresponding to L
  431. *
  432. * C = (H - (N * U)) / stripe_unit + G * D
  433. * [or (L % U) / stripe_unit + G * D]
  434. *
  435. * O - The component offset coresponding to L
  436. * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
  437. *
  438. * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
  439. * divide by parity
  440. * LCMdP = lcm(group_width, parity) / parity
  441. *
  442. * R - The parity Rotation stripe
  443. * (Note parity cycle always starts at a group's boundary)
  444. * R = N % LCMdP
  445. *
  446. * I = the first parity device index
  447. * I = (group_width + group_width - R*parity - parity) % group_width
  448. *
  449. * Craid - The component index Rotated
  450. * Craid = (group_width + C - R*parity) % group_width
  451. * (We add the group_width to avoid negative numbers modulo math)
  452. */
  453. void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
  454. u64 length, struct ore_striping_info *si)
  455. {
  456. u32 stripe_unit = layout->stripe_unit;
  457. u32 group_width = layout->group_width;
  458. u64 group_depth = layout->group_depth;
  459. u32 parity = layout->parity;
  460. u32 D = group_width - parity;
  461. u32 U = D * stripe_unit;
  462. u64 T = U * group_depth;
  463. u64 S = T * layout->group_count;
  464. u64 M = div64_u64(file_offset, S);
  465. /*
  466. G = (L - (M * S)) / T
  467. H = (L - (M * S)) % T
  468. */
  469. u64 LmodS = file_offset - M * S;
  470. u32 G = div64_u64(LmodS, T);
  471. u64 H = LmodS - G * T;
  472. u32 N = div_u64(H, U);
  473. u32 Nlast;
  474. /* "H - (N * U)" is just "H % U" so it's bound to u32 */
  475. u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
  476. div_u64_rem(file_offset, stripe_unit, &si->unit_off);
  477. si->obj_offset = si->unit_off + (N * stripe_unit) +
  478. (M * group_depth * stripe_unit);
  479. if (parity) {
  480. u32 LCMdP = lcm(group_width, parity) / parity;
  481. /* R = N % LCMdP; */
  482. u32 RxP = (N % LCMdP) * parity;
  483. u32 first_dev = C - C % group_width;
  484. si->par_dev = (group_width + group_width - parity - RxP) %
  485. group_width + first_dev;
  486. si->dev = (group_width + C - RxP) % group_width + first_dev;
  487. si->bytes_in_stripe = U;
  488. si->first_stripe_start = M * S + G * T + N * U;
  489. } else {
  490. /* Make the math correct see _prepare_one_group */
  491. si->par_dev = group_width;
  492. si->dev = C;
  493. }
  494. si->dev *= layout->mirrors_p1;
  495. si->par_dev *= layout->mirrors_p1;
  496. si->offset = file_offset;
  497. si->length = T - H;
  498. if (si->length > length)
  499. si->length = length;
  500. Nlast = div_u64(H + si->length + U - 1, U);
  501. si->maxdevUnits = Nlast - N;
  502. si->M = M;
  503. }
  504. EXPORT_SYMBOL(ore_calc_stripe_info);
  505. int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
  506. unsigned pgbase, struct page **pages,
  507. struct ore_per_dev_state *per_dev, int cur_len)
  508. {
  509. unsigned pg = *cur_pg;
  510. struct request_queue *q =
  511. osd_request_queue(_ios_od(ios, per_dev->dev));
  512. unsigned len = cur_len;
  513. int ret;
  514. if (per_dev->bio == NULL) {
  515. unsigned bio_size;
  516. if (!ios->reading) {
  517. bio_size = ios->si.maxdevUnits;
  518. } else {
  519. bio_size = (ios->si.maxdevUnits + 1) *
  520. (ios->layout->group_width - ios->layout->parity) /
  521. ios->layout->group_width;
  522. }
  523. bio_size *= (ios->layout->stripe_unit / PAGE_SIZE);
  524. per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
  525. if (unlikely(!per_dev->bio)) {
  526. ORE_DBGMSG("Failed to allocate BIO size=%u\n",
  527. bio_size);
  528. ret = -ENOMEM;
  529. goto out;
  530. }
  531. }
  532. while (cur_len > 0) {
  533. unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
  534. unsigned added_len;
  535. cur_len -= pglen;
  536. added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
  537. pglen, pgbase);
  538. if (unlikely(pglen != added_len)) {
  539. /* If bi_vcnt == bi_max then this is a SW BUG */
  540. ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=0x%x "
  541. "bi_max=0x%x BIO_MAX=0x%x cur_len=0x%x\n",
  542. per_dev->bio->bi_vcnt,
  543. per_dev->bio->bi_max_vecs,
  544. BIO_MAX_PAGES_KMALLOC, cur_len);
  545. ret = -ENOMEM;
  546. goto out;
  547. }
  548. _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
  549. pgbase = 0;
  550. ++pg;
  551. }
  552. BUG_ON(cur_len);
  553. per_dev->length += len;
  554. *cur_pg = pg;
  555. ret = 0;
  556. out: /* we fail the complete unit on an error eg don't advance
  557. * per_dev->length and cur_pg. This means that we might have a bigger
  558. * bio than the CDB requested length (per_dev->length). That's fine
  559. * only the oposite is fatal.
  560. */
  561. return ret;
  562. }
  563. static int _prepare_for_striping(struct ore_io_state *ios)
  564. {
  565. struct ore_striping_info *si = &ios->si;
  566. unsigned stripe_unit = ios->layout->stripe_unit;
  567. unsigned mirrors_p1 = ios->layout->mirrors_p1;
  568. unsigned group_width = ios->layout->group_width;
  569. unsigned devs_in_group = group_width * mirrors_p1;
  570. unsigned dev = si->dev;
  571. unsigned first_dev = dev - (dev % devs_in_group);
  572. unsigned dev_order;
  573. unsigned cur_pg = ios->pages_consumed;
  574. u64 length = ios->length;
  575. int ret = 0;
  576. if (!ios->pages) {
  577. ios->numdevs = ios->layout->mirrors_p1;
  578. return 0;
  579. }
  580. BUG_ON(length > si->length);
  581. dev_order = _dev_order(devs_in_group, mirrors_p1, si->par_dev, dev);
  582. si->cur_comp = dev_order;
  583. si->cur_pg = si->unit_off / PAGE_SIZE;
  584. while (length) {
  585. unsigned comp = dev - first_dev;
  586. struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
  587. unsigned cur_len, page_off = 0;
  588. if (!per_dev->length) {
  589. per_dev->dev = dev;
  590. if (dev == si->dev) {
  591. WARN_ON(dev == si->par_dev);
  592. per_dev->offset = si->obj_offset;
  593. cur_len = stripe_unit - si->unit_off;
  594. page_off = si->unit_off & ~PAGE_MASK;
  595. BUG_ON(page_off && (page_off != ios->pgbase));
  596. } else {
  597. if (si->cur_comp > dev_order)
  598. per_dev->offset =
  599. si->obj_offset - si->unit_off;
  600. else /* si->cur_comp < dev_order */
  601. per_dev->offset =
  602. si->obj_offset + stripe_unit -
  603. si->unit_off;
  604. cur_len = stripe_unit;
  605. }
  606. } else {
  607. cur_len = stripe_unit;
  608. }
  609. if (cur_len >= length)
  610. cur_len = length;
  611. ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
  612. per_dev, cur_len);
  613. if (unlikely(ret))
  614. goto out;
  615. dev += mirrors_p1;
  616. dev = (dev % devs_in_group) + first_dev;
  617. length -= cur_len;
  618. si->cur_comp = (si->cur_comp + 1) % group_width;
  619. if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
  620. if (!length && ios->sp2d) {
  621. /* If we are writing and this is the very last
  622. * stripe. then operate on parity dev.
  623. */
  624. dev = si->par_dev;
  625. }
  626. if (ios->sp2d)
  627. /* In writes cur_len just means if it's the
  628. * last one. See _ore_add_parity_unit.
  629. */
  630. cur_len = length;
  631. per_dev = &ios->per_dev[dev - first_dev];
  632. if (!per_dev->length) {
  633. /* Only/always the parity unit of the first
  634. * stripe will be empty. So this is a chance to
  635. * initialize the per_dev info.
  636. */
  637. per_dev->dev = dev;
  638. per_dev->offset = si->obj_offset - si->unit_off;
  639. }
  640. ret = _ore_add_parity_unit(ios, si, per_dev, cur_len);
  641. if (unlikely(ret))
  642. goto out;
  643. /* Rotate next par_dev backwards with wraping */
  644. si->par_dev = (devs_in_group + si->par_dev -
  645. ios->layout->parity * mirrors_p1) %
  646. devs_in_group + first_dev;
  647. /* Next stripe, start fresh */
  648. si->cur_comp = 0;
  649. si->cur_pg = 0;
  650. }
  651. }
  652. out:
  653. ios->numdevs = devs_in_group;
  654. ios->pages_consumed = cur_pg;
  655. return ret;
  656. }
  657. int ore_create(struct ore_io_state *ios)
  658. {
  659. int i, ret;
  660. for (i = 0; i < ios->oc->numdevs; i++) {
  661. struct osd_request *or;
  662. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  663. if (unlikely(!or)) {
  664. ORE_ERR("%s: osd_start_request failed\n", __func__);
  665. ret = -ENOMEM;
  666. goto out;
  667. }
  668. ios->per_dev[i].or = or;
  669. ios->numdevs++;
  670. osd_req_create_object(or, _ios_obj(ios, i));
  671. }
  672. ret = ore_io_execute(ios);
  673. out:
  674. return ret;
  675. }
  676. EXPORT_SYMBOL(ore_create);
  677. int ore_remove(struct ore_io_state *ios)
  678. {
  679. int i, ret;
  680. for (i = 0; i < ios->oc->numdevs; i++) {
  681. struct osd_request *or;
  682. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  683. if (unlikely(!or)) {
  684. ORE_ERR("%s: osd_start_request failed\n", __func__);
  685. ret = -ENOMEM;
  686. goto out;
  687. }
  688. ios->per_dev[i].or = or;
  689. ios->numdevs++;
  690. osd_req_remove_object(or, _ios_obj(ios, i));
  691. }
  692. ret = ore_io_execute(ios);
  693. out:
  694. return ret;
  695. }
  696. EXPORT_SYMBOL(ore_remove);
  697. static int _write_mirror(struct ore_io_state *ios, int cur_comp)
  698. {
  699. struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
  700. unsigned dev = ios->per_dev[cur_comp].dev;
  701. unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
  702. int ret = 0;
  703. if (ios->pages && !master_dev->length)
  704. return 0; /* Just an empty slot */
  705. for (; cur_comp < last_comp; ++cur_comp, ++dev) {
  706. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  707. struct osd_request *or;
  708. or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
  709. if (unlikely(!or)) {
  710. ORE_ERR("%s: osd_start_request failed\n", __func__);
  711. ret = -ENOMEM;
  712. goto out;
  713. }
  714. per_dev->or = or;
  715. if (ios->pages) {
  716. struct bio *bio;
  717. if (per_dev != master_dev) {
  718. bio = bio_kmalloc(GFP_KERNEL,
  719. master_dev->bio->bi_max_vecs);
  720. if (unlikely(!bio)) {
  721. ORE_DBGMSG(
  722. "Failed to allocate BIO size=%u\n",
  723. master_dev->bio->bi_max_vecs);
  724. ret = -ENOMEM;
  725. goto out;
  726. }
  727. __bio_clone(bio, master_dev->bio);
  728. bio->bi_bdev = NULL;
  729. bio->bi_next = NULL;
  730. per_dev->offset = master_dev->offset;
  731. per_dev->length = master_dev->length;
  732. per_dev->bio = bio;
  733. per_dev->dev = dev;
  734. } else {
  735. bio = master_dev->bio;
  736. /* FIXME: bio_set_dir() */
  737. bio->bi_rw |= REQ_WRITE;
  738. }
  739. osd_req_write(or, _ios_obj(ios, cur_comp),
  740. per_dev->offset, bio, per_dev->length);
  741. ORE_DBGMSG("write(0x%llx) offset=0x%llx "
  742. "length=0x%llx dev=%d\n",
  743. _LLU(_ios_obj(ios, cur_comp)->id),
  744. _LLU(per_dev->offset),
  745. _LLU(per_dev->length), dev);
  746. } else if (ios->kern_buff) {
  747. per_dev->offset = ios->si.obj_offset;
  748. per_dev->dev = ios->si.dev + dev;
  749. /* no cross device without page array */
  750. BUG_ON((ios->layout->group_width > 1) &&
  751. (ios->si.unit_off + ios->length >
  752. ios->layout->stripe_unit));
  753. ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp),
  754. per_dev->offset,
  755. ios->kern_buff, ios->length);
  756. if (unlikely(ret))
  757. goto out;
  758. ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
  759. "length=0x%llx dev=%d\n",
  760. _LLU(_ios_obj(ios, cur_comp)->id),
  761. _LLU(per_dev->offset),
  762. _LLU(ios->length), per_dev->dev);
  763. } else {
  764. osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
  765. ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
  766. _LLU(_ios_obj(ios, cur_comp)->id),
  767. ios->out_attr_len, dev);
  768. }
  769. if (ios->out_attr)
  770. osd_req_add_set_attr_list(or, ios->out_attr,
  771. ios->out_attr_len);
  772. if (ios->in_attr)
  773. osd_req_add_get_attr_list(or, ios->in_attr,
  774. ios->in_attr_len);
  775. }
  776. out:
  777. return ret;
  778. }
  779. int ore_write(struct ore_io_state *ios)
  780. {
  781. int i;
  782. int ret;
  783. if (unlikely(ios->sp2d && !ios->r4w)) {
  784. /* A library is attempting a RAID-write without providing
  785. * a pages lock interface.
  786. */
  787. WARN_ON_ONCE(1);
  788. return -ENOTSUPP;
  789. }
  790. ret = _prepare_for_striping(ios);
  791. if (unlikely(ret))
  792. return ret;
  793. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  794. ret = _write_mirror(ios, i);
  795. if (unlikely(ret))
  796. return ret;
  797. }
  798. ret = ore_io_execute(ios);
  799. return ret;
  800. }
  801. EXPORT_SYMBOL(ore_write);
  802. int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
  803. {
  804. struct osd_request *or;
  805. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  806. struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
  807. unsigned first_dev = (unsigned)obj->id;
  808. if (ios->pages && !per_dev->length)
  809. return 0; /* Just an empty slot */
  810. first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
  811. or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
  812. if (unlikely(!or)) {
  813. ORE_ERR("%s: osd_start_request failed\n", __func__);
  814. return -ENOMEM;
  815. }
  816. per_dev->or = or;
  817. if (ios->pages) {
  818. if (per_dev->cur_sg) {
  819. /* finalize the last sg_entry */
  820. _ore_add_sg_seg(per_dev, 0, false);
  821. if (unlikely(!per_dev->cur_sg))
  822. return 0; /* Skip parity only device */
  823. osd_req_read_sg(or, obj, per_dev->bio,
  824. per_dev->sglist, per_dev->cur_sg);
  825. } else {
  826. /* The no raid case */
  827. osd_req_read(or, obj, per_dev->offset,
  828. per_dev->bio, per_dev->length);
  829. }
  830. ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
  831. " dev=%d sg_len=%d\n", _LLU(obj->id),
  832. _LLU(per_dev->offset), _LLU(per_dev->length),
  833. first_dev, per_dev->cur_sg);
  834. } else {
  835. BUG_ON(ios->kern_buff);
  836. osd_req_get_attributes(or, obj);
  837. ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
  838. _LLU(obj->id),
  839. ios->in_attr_len, first_dev);
  840. }
  841. if (ios->out_attr)
  842. osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
  843. if (ios->in_attr)
  844. osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
  845. return 0;
  846. }
  847. int ore_read(struct ore_io_state *ios)
  848. {
  849. int i;
  850. int ret;
  851. ret = _prepare_for_striping(ios);
  852. if (unlikely(ret))
  853. return ret;
  854. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  855. ret = _ore_read_mirror(ios, i);
  856. if (unlikely(ret))
  857. return ret;
  858. }
  859. ret = ore_io_execute(ios);
  860. return ret;
  861. }
  862. EXPORT_SYMBOL(ore_read);
  863. int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
  864. {
  865. struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
  866. void *iter = NULL;
  867. int nelem;
  868. do {
  869. nelem = 1;
  870. osd_req_decode_get_attr_list(ios->per_dev[0].or,
  871. &cur_attr, &nelem, &iter);
  872. if ((cur_attr.attr_page == attr->attr_page) &&
  873. (cur_attr.attr_id == attr->attr_id)) {
  874. attr->len = cur_attr.len;
  875. attr->val_ptr = cur_attr.val_ptr;
  876. return 0;
  877. }
  878. } while (iter);
  879. return -EIO;
  880. }
  881. EXPORT_SYMBOL(extract_attr_from_ios);
  882. static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
  883. struct osd_attr *attr)
  884. {
  885. int last_comp = cur_comp + ios->layout->mirrors_p1;
  886. for (; cur_comp < last_comp; ++cur_comp) {
  887. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  888. struct osd_request *or;
  889. or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
  890. if (unlikely(!or)) {
  891. ORE_ERR("%s: osd_start_request failed\n", __func__);
  892. return -ENOMEM;
  893. }
  894. per_dev->or = or;
  895. osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
  896. osd_req_add_set_attr_list(or, attr, 1);
  897. }
  898. return 0;
  899. }
  900. struct _trunc_info {
  901. struct ore_striping_info si;
  902. u64 prev_group_obj_off;
  903. u64 next_group_obj_off;
  904. unsigned first_group_dev;
  905. unsigned nex_group_dev;
  906. };
  907. static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
  908. struct _trunc_info *ti)
  909. {
  910. unsigned stripe_unit = layout->stripe_unit;
  911. ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
  912. ti->prev_group_obj_off = ti->si.M * stripe_unit;
  913. ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
  914. ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
  915. ti->nex_group_dev = ti->first_group_dev + layout->group_width;
  916. }
  917. int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
  918. u64 size)
  919. {
  920. struct ore_io_state *ios;
  921. struct exofs_trunc_attr {
  922. struct osd_attr attr;
  923. __be64 newsize;
  924. } *size_attrs;
  925. struct _trunc_info ti;
  926. int i, ret;
  927. ret = ore_get_io_state(layout, oc, &ios);
  928. if (unlikely(ret))
  929. return ret;
  930. _calc_trunk_info(ios->layout, size, &ti);
  931. size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
  932. GFP_KERNEL);
  933. if (unlikely(!size_attrs)) {
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. ios->numdevs = ios->oc->numdevs;
  938. for (i = 0; i < ios->numdevs; ++i) {
  939. struct exofs_trunc_attr *size_attr = &size_attrs[i];
  940. u64 obj_size;
  941. if (i < ti.first_group_dev)
  942. obj_size = ti.prev_group_obj_off;
  943. else if (i >= ti.nex_group_dev)
  944. obj_size = ti.next_group_obj_off;
  945. else if (i < ti.si.dev) /* dev within this group */
  946. obj_size = ti.si.obj_offset +
  947. ios->layout->stripe_unit - ti.si.unit_off;
  948. else if (i == ti.si.dev)
  949. obj_size = ti.si.obj_offset;
  950. else /* i > ti.dev */
  951. obj_size = ti.si.obj_offset - ti.si.unit_off;
  952. size_attr->newsize = cpu_to_be64(obj_size);
  953. size_attr->attr = g_attr_logical_length;
  954. size_attr->attr.val_ptr = &size_attr->newsize;
  955. ORE_DBGMSG2("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
  956. _LLU(oc->comps->obj.id), _LLU(obj_size), i);
  957. ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
  958. &size_attr->attr);
  959. if (unlikely(ret))
  960. goto out;
  961. }
  962. ret = ore_io_execute(ios);
  963. out:
  964. kfree(size_attrs);
  965. ore_put_io_state(ios);
  966. return ret;
  967. }
  968. EXPORT_SYMBOL(ore_truncate);
  969. const struct osd_attr g_attr_logical_length = ATTR_DEF(
  970. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  971. EXPORT_SYMBOL(g_attr_logical_length);