ore.c 30 KB

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