ore.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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_all(bv, bio, i) {
  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. per_dev->bio) {
  377. /* start read offset passed endof file.
  378. * Note: if we do not have bio it means read-attributes
  379. * In this case we should return error to caller.
  380. */
  381. _clear_bio(per_dev->bio);
  382. ORE_DBGMSG("start read offset passed end of file "
  383. "offset=0x%llx, length=0x%llx\n",
  384. _LLU(per_dev->offset),
  385. _LLU(per_dev->length));
  386. continue; /* we recovered */
  387. }
  388. if (on_dev_error) {
  389. u64 residual = ios->reading ?
  390. or->in.residual : or->out.residual;
  391. u64 offset = (ios->offset + ios->length) - residual;
  392. unsigned dev = per_dev->dev - ios->oc->first_dev;
  393. struct ore_dev *od = ios->oc->ods[dev];
  394. on_dev_error(ios, od, dev, osi.osd_err_pri,
  395. offset, residual);
  396. }
  397. if (osi.osd_err_pri >= acumulated_osd_err) {
  398. acumulated_osd_err = osi.osd_err_pri;
  399. acumulated_lin_err = ret;
  400. }
  401. }
  402. return acumulated_lin_err;
  403. }
  404. EXPORT_SYMBOL(ore_check_io);
  405. /*
  406. * L - logical offset into the file
  407. *
  408. * D - number of Data devices
  409. * D = group_width - parity
  410. *
  411. * U - The number of bytes in a stripe within a group
  412. * U = stripe_unit * D
  413. *
  414. * T - The number of bytes striped within a group of component objects
  415. * (before advancing to the next group)
  416. * T = U * group_depth
  417. *
  418. * S - The number of bytes striped across all component objects
  419. * before the pattern repeats
  420. * S = T * group_count
  421. *
  422. * M - The "major" (i.e., across all components) cycle number
  423. * M = L / S
  424. *
  425. * G - Counts the groups from the beginning of the major cycle
  426. * G = (L - (M * S)) / T [or (L % S) / T]
  427. *
  428. * H - The byte offset within the group
  429. * H = (L - (M * S)) % T [or (L % S) % T]
  430. *
  431. * N - The "minor" (i.e., across the group) stripe number
  432. * N = H / U
  433. *
  434. * C - The component index coresponding to L
  435. *
  436. * C = (H - (N * U)) / stripe_unit + G * D
  437. * [or (L % U) / stripe_unit + G * D]
  438. *
  439. * O - The component offset coresponding to L
  440. * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
  441. *
  442. * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
  443. * divide by parity
  444. * LCMdP = lcm(group_width, parity) / parity
  445. *
  446. * R - The parity Rotation stripe
  447. * (Note parity cycle always starts at a group's boundary)
  448. * R = N % LCMdP
  449. *
  450. * I = the first parity device index
  451. * I = (group_width + group_width - R*parity - parity) % group_width
  452. *
  453. * Craid - The component index Rotated
  454. * Craid = (group_width + C - R*parity) % group_width
  455. * (We add the group_width to avoid negative numbers modulo math)
  456. */
  457. void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
  458. u64 length, struct ore_striping_info *si)
  459. {
  460. u32 stripe_unit = layout->stripe_unit;
  461. u32 group_width = layout->group_width;
  462. u64 group_depth = layout->group_depth;
  463. u32 parity = layout->parity;
  464. u32 D = group_width - parity;
  465. u32 U = D * stripe_unit;
  466. u64 T = U * group_depth;
  467. u64 S = T * layout->group_count;
  468. u64 M = div64_u64(file_offset, S);
  469. /*
  470. G = (L - (M * S)) / T
  471. H = (L - (M * S)) % T
  472. */
  473. u64 LmodS = file_offset - M * S;
  474. u32 G = div64_u64(LmodS, T);
  475. u64 H = LmodS - G * T;
  476. u32 N = div_u64(H, U);
  477. u32 Nlast;
  478. /* "H - (N * U)" is just "H % U" so it's bound to u32 */
  479. u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
  480. div_u64_rem(file_offset, stripe_unit, &si->unit_off);
  481. si->obj_offset = si->unit_off + (N * stripe_unit) +
  482. (M * group_depth * stripe_unit);
  483. if (parity) {
  484. u32 LCMdP = lcm(group_width, parity) / parity;
  485. /* R = N % LCMdP; */
  486. u32 RxP = (N % LCMdP) * parity;
  487. u32 first_dev = C - C % group_width;
  488. si->par_dev = (group_width + group_width - parity - RxP) %
  489. group_width + first_dev;
  490. si->dev = (group_width + C - RxP) % group_width + first_dev;
  491. si->bytes_in_stripe = U;
  492. si->first_stripe_start = M * S + G * T + N * U;
  493. } else {
  494. /* Make the math correct see _prepare_one_group */
  495. si->par_dev = group_width;
  496. si->dev = C;
  497. }
  498. si->dev *= layout->mirrors_p1;
  499. si->par_dev *= layout->mirrors_p1;
  500. si->offset = file_offset;
  501. si->length = T - H;
  502. if (si->length > length)
  503. si->length = length;
  504. Nlast = div_u64(H + si->length + U - 1, U);
  505. si->maxdevUnits = Nlast - N;
  506. si->M = M;
  507. }
  508. EXPORT_SYMBOL(ore_calc_stripe_info);
  509. int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
  510. unsigned pgbase, struct page **pages,
  511. struct ore_per_dev_state *per_dev, int cur_len)
  512. {
  513. unsigned pg = *cur_pg;
  514. struct request_queue *q =
  515. osd_request_queue(_ios_od(ios, per_dev->dev));
  516. unsigned len = cur_len;
  517. int ret;
  518. if (per_dev->bio == NULL) {
  519. unsigned bio_size;
  520. if (!ios->reading) {
  521. bio_size = ios->si.maxdevUnits;
  522. } else {
  523. bio_size = (ios->si.maxdevUnits + 1) *
  524. (ios->layout->group_width - ios->layout->parity) /
  525. ios->layout->group_width;
  526. }
  527. bio_size *= (ios->layout->stripe_unit / PAGE_SIZE);
  528. per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
  529. if (unlikely(!per_dev->bio)) {
  530. ORE_DBGMSG("Failed to allocate BIO size=%u\n",
  531. bio_size);
  532. ret = -ENOMEM;
  533. goto out;
  534. }
  535. }
  536. while (cur_len > 0) {
  537. unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
  538. unsigned added_len;
  539. cur_len -= pglen;
  540. added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
  541. pglen, pgbase);
  542. if (unlikely(pglen != added_len)) {
  543. /* If bi_vcnt == bi_max then this is a SW BUG */
  544. ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=0x%x "
  545. "bi_max=0x%x BIO_MAX=0x%x cur_len=0x%x\n",
  546. per_dev->bio->bi_vcnt,
  547. per_dev->bio->bi_max_vecs,
  548. BIO_MAX_PAGES_KMALLOC, cur_len);
  549. ret = -ENOMEM;
  550. goto out;
  551. }
  552. _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
  553. pgbase = 0;
  554. ++pg;
  555. }
  556. BUG_ON(cur_len);
  557. per_dev->length += len;
  558. *cur_pg = pg;
  559. ret = 0;
  560. out: /* we fail the complete unit on an error eg don't advance
  561. * per_dev->length and cur_pg. This means that we might have a bigger
  562. * bio than the CDB requested length (per_dev->length). That's fine
  563. * only the oposite is fatal.
  564. */
  565. return ret;
  566. }
  567. static int _prepare_for_striping(struct ore_io_state *ios)
  568. {
  569. struct ore_striping_info *si = &ios->si;
  570. unsigned stripe_unit = ios->layout->stripe_unit;
  571. unsigned mirrors_p1 = ios->layout->mirrors_p1;
  572. unsigned group_width = ios->layout->group_width;
  573. unsigned devs_in_group = group_width * mirrors_p1;
  574. unsigned dev = si->dev;
  575. unsigned first_dev = dev - (dev % devs_in_group);
  576. unsigned dev_order;
  577. unsigned cur_pg = ios->pages_consumed;
  578. u64 length = ios->length;
  579. int ret = 0;
  580. if (!ios->pages) {
  581. ios->numdevs = ios->layout->mirrors_p1;
  582. return 0;
  583. }
  584. BUG_ON(length > si->length);
  585. dev_order = _dev_order(devs_in_group, mirrors_p1, si->par_dev, dev);
  586. si->cur_comp = dev_order;
  587. si->cur_pg = si->unit_off / PAGE_SIZE;
  588. while (length) {
  589. unsigned comp = dev - first_dev;
  590. struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
  591. unsigned cur_len, page_off = 0;
  592. if (!per_dev->length) {
  593. per_dev->dev = dev;
  594. if (dev == si->dev) {
  595. WARN_ON(dev == si->par_dev);
  596. per_dev->offset = si->obj_offset;
  597. cur_len = stripe_unit - si->unit_off;
  598. page_off = si->unit_off & ~PAGE_MASK;
  599. BUG_ON(page_off && (page_off != ios->pgbase));
  600. } else {
  601. if (si->cur_comp > dev_order)
  602. per_dev->offset =
  603. si->obj_offset - si->unit_off;
  604. else /* si->cur_comp < dev_order */
  605. per_dev->offset =
  606. si->obj_offset + stripe_unit -
  607. si->unit_off;
  608. cur_len = stripe_unit;
  609. }
  610. } else {
  611. cur_len = stripe_unit;
  612. }
  613. if (cur_len >= length)
  614. cur_len = length;
  615. ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
  616. per_dev, cur_len);
  617. if (unlikely(ret))
  618. goto out;
  619. dev += mirrors_p1;
  620. dev = (dev % devs_in_group) + first_dev;
  621. length -= cur_len;
  622. si->cur_comp = (si->cur_comp + 1) % group_width;
  623. if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
  624. if (!length && ios->sp2d) {
  625. /* If we are writing and this is the very last
  626. * stripe. then operate on parity dev.
  627. */
  628. dev = si->par_dev;
  629. }
  630. if (ios->sp2d)
  631. /* In writes cur_len just means if it's the
  632. * last one. See _ore_add_parity_unit.
  633. */
  634. cur_len = length;
  635. per_dev = &ios->per_dev[dev - first_dev];
  636. if (!per_dev->length) {
  637. /* Only/always the parity unit of the first
  638. * stripe will be empty. So this is a chance to
  639. * initialize the per_dev info.
  640. */
  641. per_dev->dev = dev;
  642. per_dev->offset = si->obj_offset - si->unit_off;
  643. }
  644. ret = _ore_add_parity_unit(ios, si, per_dev, cur_len);
  645. if (unlikely(ret))
  646. goto out;
  647. /* Rotate next par_dev backwards with wraping */
  648. si->par_dev = (devs_in_group + si->par_dev -
  649. ios->layout->parity * mirrors_p1) %
  650. devs_in_group + first_dev;
  651. /* Next stripe, start fresh */
  652. si->cur_comp = 0;
  653. si->cur_pg = 0;
  654. }
  655. }
  656. out:
  657. ios->numdevs = devs_in_group;
  658. ios->pages_consumed = cur_pg;
  659. return ret;
  660. }
  661. int ore_create(struct ore_io_state *ios)
  662. {
  663. int i, ret;
  664. for (i = 0; i < ios->oc->numdevs; i++) {
  665. struct osd_request *or;
  666. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  667. if (unlikely(!or)) {
  668. ORE_ERR("%s: osd_start_request failed\n", __func__);
  669. ret = -ENOMEM;
  670. goto out;
  671. }
  672. ios->per_dev[i].or = or;
  673. ios->numdevs++;
  674. osd_req_create_object(or, _ios_obj(ios, i));
  675. }
  676. ret = ore_io_execute(ios);
  677. out:
  678. return ret;
  679. }
  680. EXPORT_SYMBOL(ore_create);
  681. int ore_remove(struct ore_io_state *ios)
  682. {
  683. int i, ret;
  684. for (i = 0; i < ios->oc->numdevs; i++) {
  685. struct osd_request *or;
  686. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  687. if (unlikely(!or)) {
  688. ORE_ERR("%s: osd_start_request failed\n", __func__);
  689. ret = -ENOMEM;
  690. goto out;
  691. }
  692. ios->per_dev[i].or = or;
  693. ios->numdevs++;
  694. osd_req_remove_object(or, _ios_obj(ios, i));
  695. }
  696. ret = ore_io_execute(ios);
  697. out:
  698. return ret;
  699. }
  700. EXPORT_SYMBOL(ore_remove);
  701. static int _write_mirror(struct ore_io_state *ios, int cur_comp)
  702. {
  703. struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
  704. unsigned dev = ios->per_dev[cur_comp].dev;
  705. unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
  706. int ret = 0;
  707. if (ios->pages && !master_dev->length)
  708. return 0; /* Just an empty slot */
  709. for (; cur_comp < last_comp; ++cur_comp, ++dev) {
  710. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  711. struct osd_request *or;
  712. or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
  713. if (unlikely(!or)) {
  714. ORE_ERR("%s: osd_start_request failed\n", __func__);
  715. ret = -ENOMEM;
  716. goto out;
  717. }
  718. per_dev->or = or;
  719. if (ios->pages) {
  720. struct bio *bio;
  721. if (per_dev != master_dev) {
  722. bio = bio_clone_kmalloc(master_dev->bio,
  723. GFP_KERNEL);
  724. if (unlikely(!bio)) {
  725. ORE_DBGMSG(
  726. "Failed to allocate BIO size=%u\n",
  727. master_dev->bio->bi_max_vecs);
  728. ret = -ENOMEM;
  729. goto out;
  730. }
  731. bio->bi_bdev = NULL;
  732. bio->bi_next = NULL;
  733. per_dev->offset = master_dev->offset;
  734. per_dev->length = master_dev->length;
  735. per_dev->bio = bio;
  736. per_dev->dev = dev;
  737. } else {
  738. bio = master_dev->bio;
  739. /* FIXME: bio_set_dir() */
  740. bio->bi_rw |= REQ_WRITE;
  741. }
  742. osd_req_write(or, _ios_obj(ios, cur_comp),
  743. per_dev->offset, bio, per_dev->length);
  744. ORE_DBGMSG("write(0x%llx) offset=0x%llx "
  745. "length=0x%llx dev=%d\n",
  746. _LLU(_ios_obj(ios, cur_comp)->id),
  747. _LLU(per_dev->offset),
  748. _LLU(per_dev->length), dev);
  749. } else if (ios->kern_buff) {
  750. per_dev->offset = ios->si.obj_offset;
  751. per_dev->dev = ios->si.dev + dev;
  752. /* no cross device without page array */
  753. BUG_ON((ios->layout->group_width > 1) &&
  754. (ios->si.unit_off + ios->length >
  755. ios->layout->stripe_unit));
  756. ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp),
  757. per_dev->offset,
  758. ios->kern_buff, ios->length);
  759. if (unlikely(ret))
  760. goto out;
  761. ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
  762. "length=0x%llx dev=%d\n",
  763. _LLU(_ios_obj(ios, cur_comp)->id),
  764. _LLU(per_dev->offset),
  765. _LLU(ios->length), per_dev->dev);
  766. } else {
  767. osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
  768. ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
  769. _LLU(_ios_obj(ios, cur_comp)->id),
  770. ios->out_attr_len, dev);
  771. }
  772. if (ios->out_attr)
  773. osd_req_add_set_attr_list(or, ios->out_attr,
  774. ios->out_attr_len);
  775. if (ios->in_attr)
  776. osd_req_add_get_attr_list(or, ios->in_attr,
  777. ios->in_attr_len);
  778. }
  779. out:
  780. return ret;
  781. }
  782. int ore_write(struct ore_io_state *ios)
  783. {
  784. int i;
  785. int ret;
  786. if (unlikely(ios->sp2d && !ios->r4w)) {
  787. /* A library is attempting a RAID-write without providing
  788. * a pages lock interface.
  789. */
  790. WARN_ON_ONCE(1);
  791. return -ENOTSUPP;
  792. }
  793. ret = _prepare_for_striping(ios);
  794. if (unlikely(ret))
  795. return ret;
  796. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  797. ret = _write_mirror(ios, i);
  798. if (unlikely(ret))
  799. return ret;
  800. }
  801. ret = ore_io_execute(ios);
  802. return ret;
  803. }
  804. EXPORT_SYMBOL(ore_write);
  805. int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
  806. {
  807. struct osd_request *or;
  808. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  809. struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
  810. unsigned first_dev = (unsigned)obj->id;
  811. if (ios->pages && !per_dev->length)
  812. return 0; /* Just an empty slot */
  813. first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
  814. or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
  815. if (unlikely(!or)) {
  816. ORE_ERR("%s: osd_start_request failed\n", __func__);
  817. return -ENOMEM;
  818. }
  819. per_dev->or = or;
  820. if (ios->pages) {
  821. if (per_dev->cur_sg) {
  822. /* finalize the last sg_entry */
  823. _ore_add_sg_seg(per_dev, 0, false);
  824. if (unlikely(!per_dev->cur_sg))
  825. return 0; /* Skip parity only device */
  826. osd_req_read_sg(or, obj, per_dev->bio,
  827. per_dev->sglist, per_dev->cur_sg);
  828. } else {
  829. /* The no raid case */
  830. osd_req_read(or, obj, per_dev->offset,
  831. per_dev->bio, per_dev->length);
  832. }
  833. ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
  834. " dev=%d sg_len=%d\n", _LLU(obj->id),
  835. _LLU(per_dev->offset), _LLU(per_dev->length),
  836. first_dev, per_dev->cur_sg);
  837. } else {
  838. BUG_ON(ios->kern_buff);
  839. osd_req_get_attributes(or, obj);
  840. ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
  841. _LLU(obj->id),
  842. ios->in_attr_len, first_dev);
  843. }
  844. if (ios->out_attr)
  845. osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
  846. if (ios->in_attr)
  847. osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
  848. return 0;
  849. }
  850. int ore_read(struct ore_io_state *ios)
  851. {
  852. int i;
  853. int ret;
  854. ret = _prepare_for_striping(ios);
  855. if (unlikely(ret))
  856. return ret;
  857. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  858. ret = _ore_read_mirror(ios, i);
  859. if (unlikely(ret))
  860. return ret;
  861. }
  862. ret = ore_io_execute(ios);
  863. return ret;
  864. }
  865. EXPORT_SYMBOL(ore_read);
  866. int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
  867. {
  868. struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
  869. void *iter = NULL;
  870. int nelem;
  871. do {
  872. nelem = 1;
  873. osd_req_decode_get_attr_list(ios->per_dev[0].or,
  874. &cur_attr, &nelem, &iter);
  875. if ((cur_attr.attr_page == attr->attr_page) &&
  876. (cur_attr.attr_id == attr->attr_id)) {
  877. attr->len = cur_attr.len;
  878. attr->val_ptr = cur_attr.val_ptr;
  879. return 0;
  880. }
  881. } while (iter);
  882. return -EIO;
  883. }
  884. EXPORT_SYMBOL(extract_attr_from_ios);
  885. static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
  886. struct osd_attr *attr)
  887. {
  888. int last_comp = cur_comp + ios->layout->mirrors_p1;
  889. for (; cur_comp < last_comp; ++cur_comp) {
  890. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  891. struct osd_request *or;
  892. or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
  893. if (unlikely(!or)) {
  894. ORE_ERR("%s: osd_start_request failed\n", __func__);
  895. return -ENOMEM;
  896. }
  897. per_dev->or = or;
  898. osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
  899. osd_req_add_set_attr_list(or, attr, 1);
  900. }
  901. return 0;
  902. }
  903. struct _trunc_info {
  904. struct ore_striping_info si;
  905. u64 prev_group_obj_off;
  906. u64 next_group_obj_off;
  907. unsigned first_group_dev;
  908. unsigned nex_group_dev;
  909. };
  910. static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
  911. struct _trunc_info *ti)
  912. {
  913. unsigned stripe_unit = layout->stripe_unit;
  914. ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
  915. ti->prev_group_obj_off = ti->si.M * stripe_unit;
  916. ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
  917. ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
  918. ti->nex_group_dev = ti->first_group_dev + layout->group_width;
  919. }
  920. int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
  921. u64 size)
  922. {
  923. struct ore_io_state *ios;
  924. struct exofs_trunc_attr {
  925. struct osd_attr attr;
  926. __be64 newsize;
  927. } *size_attrs;
  928. struct _trunc_info ti;
  929. int i, ret;
  930. ret = ore_get_io_state(layout, oc, &ios);
  931. if (unlikely(ret))
  932. return ret;
  933. _calc_trunk_info(ios->layout, size, &ti);
  934. size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
  935. GFP_KERNEL);
  936. if (unlikely(!size_attrs)) {
  937. ret = -ENOMEM;
  938. goto out;
  939. }
  940. ios->numdevs = ios->oc->numdevs;
  941. for (i = 0; i < ios->numdevs; ++i) {
  942. struct exofs_trunc_attr *size_attr = &size_attrs[i];
  943. u64 obj_size;
  944. if (i < ti.first_group_dev)
  945. obj_size = ti.prev_group_obj_off;
  946. else if (i >= ti.nex_group_dev)
  947. obj_size = ti.next_group_obj_off;
  948. else if (i < ti.si.dev) /* dev within this group */
  949. obj_size = ti.si.obj_offset +
  950. ios->layout->stripe_unit - ti.si.unit_off;
  951. else if (i == ti.si.dev)
  952. obj_size = ti.si.obj_offset;
  953. else /* i > ti.dev */
  954. obj_size = ti.si.obj_offset - ti.si.unit_off;
  955. size_attr->newsize = cpu_to_be64(obj_size);
  956. size_attr->attr = g_attr_logical_length;
  957. size_attr->attr.val_ptr = &size_attr->newsize;
  958. ORE_DBGMSG2("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
  959. _LLU(oc->comps->obj.id), _LLU(obj_size), i);
  960. ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
  961. &size_attr->attr);
  962. if (unlikely(ret))
  963. goto out;
  964. }
  965. ret = ore_io_execute(ios);
  966. out:
  967. kfree(size_attrs);
  968. ore_put_io_state(ios);
  969. return ret;
  970. }
  971. EXPORT_SYMBOL(ore_truncate);
  972. const struct osd_attr g_attr_logical_length = ATTR_DEF(
  973. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  974. EXPORT_SYMBOL(g_attr_logical_length);