ore.c 29 KB

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