ore.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 <asm/div64.h>
  26. #include <scsi/osd_ore.h>
  27. #define ORE_ERR(fmt, a...) printk(KERN_ERR "ore: " fmt, ##a)
  28. #ifdef CONFIG_EXOFS_DEBUG
  29. #define ORE_DBGMSG(fmt, a...) \
  30. printk(KERN_NOTICE "ore @%s:%d: " fmt, __func__, __LINE__, ##a)
  31. #else
  32. #define ORE_DBGMSG(fmt, a...) \
  33. do { if (0) printk(fmt, ##a); } while (0)
  34. #endif
  35. /* u64 has problems with printk this will cast it to unsigned long long */
  36. #define _LLU(x) (unsigned long long)(x)
  37. #define ORE_DBGMSG2(M...) do {} while (0)
  38. /* #define ORE_DBGMSG2 ORE_DBGMSG */
  39. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  40. MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
  41. MODULE_LICENSE("GPL");
  42. /* ore_verify_layout does a couple of things:
  43. * 1. Given a minimum number of needed parameters fixes up the rest of the
  44. * members to be operatonals for the ore. The needed parameters are those
  45. * that are defined by the pnfs-objects layout STD.
  46. * 2. Check to see if the current ore code actually supports these parameters
  47. * for example stripe_unit must be a multple of the system PAGE_SIZE,
  48. * and etc...
  49. * 3. Cache some havily used calculations that will be needed by users.
  50. */
  51. enum { BIO_MAX_PAGES_KMALLOC =
  52. (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),};
  53. int ore_verify_layout(unsigned total_comps, struct ore_layout *layout)
  54. {
  55. u64 stripe_length;
  56. /* FIXME: Only raid0 is supported for now. */
  57. if (layout->raid_algorithm != PNFS_OSD_RAID_0) {
  58. ORE_ERR("Only RAID_0 for now\n");
  59. return -EINVAL;
  60. }
  61. if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
  62. ORE_ERR("Stripe Unit(0x%llx)"
  63. " must be Multples of PAGE_SIZE(0x%lx)\n",
  64. _LLU(layout->stripe_unit), PAGE_SIZE);
  65. return -EINVAL;
  66. }
  67. if (layout->group_width) {
  68. if (!layout->group_depth) {
  69. ORE_ERR("group_depth == 0 && group_width != 0\n");
  70. return -EINVAL;
  71. }
  72. if (total_comps < (layout->group_width * layout->mirrors_p1)) {
  73. ORE_ERR("Data Map wrong, "
  74. "numdevs=%d < group_width=%d * mirrors=%d\n",
  75. total_comps, layout->group_width,
  76. layout->mirrors_p1);
  77. return -EINVAL;
  78. }
  79. layout->group_count = total_comps / layout->mirrors_p1 /
  80. layout->group_width;
  81. } else {
  82. if (layout->group_depth) {
  83. printk(KERN_NOTICE "Warning: group_depth ignored "
  84. "group_width == 0 && group_depth == %lld\n",
  85. _LLU(layout->group_depth));
  86. }
  87. layout->group_width = total_comps / layout->mirrors_p1;
  88. layout->group_depth = -1;
  89. layout->group_count = 1;
  90. }
  91. stripe_length = (u64)layout->group_width * layout->stripe_unit;
  92. if (stripe_length >= (1ULL << 32)) {
  93. ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
  94. _LLU(stripe_length));
  95. return -EINVAL;
  96. }
  97. layout->max_io_length =
  98. (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
  99. layout->group_width;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(ore_verify_layout);
  103. static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
  104. {
  105. return ios->oc->comps[index & ios->oc->single_comp].cred;
  106. }
  107. static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
  108. {
  109. return &ios->oc->comps[index & ios->oc->single_comp].obj;
  110. }
  111. static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
  112. {
  113. ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
  114. ios->oc->first_dev, ios->oc->numdevs, index,
  115. ios->oc->ods);
  116. return ore_comp_dev(ios->oc, index);
  117. }
  118. static int _get_io_state(struct ore_layout *layout,
  119. struct ore_components *oc, unsigned numdevs,
  120. struct ore_io_state **pios)
  121. {
  122. struct ore_io_state *ios;
  123. /*TODO: Maybe use kmem_cach per sbi of size
  124. * exofs_io_state_size(layout->s_numdevs)
  125. */
  126. ios = kzalloc(ore_io_state_size(numdevs), GFP_KERNEL);
  127. if (unlikely(!ios)) {
  128. ORE_DBGMSG("Failed kzalloc bytes=%d\n",
  129. ore_io_state_size(numdevs));
  130. *pios = NULL;
  131. return -ENOMEM;
  132. }
  133. ios->layout = layout;
  134. ios->oc = oc;
  135. *pios = ios;
  136. return 0;
  137. }
  138. /* Allocate an io_state for only a single group of devices
  139. *
  140. * If a user needs to call ore_read/write() this version must be used becase it
  141. * allocates extra stuff for striping and raid.
  142. * The ore might decide to only IO less then @length bytes do to alignmets
  143. * and constrains as follows:
  144. * - The IO cannot cross group boundary.
  145. * - In raid5/6 The end of the IO must align at end of a stripe eg.
  146. * (@offset + @length) % strip_size == 0. Or the complete range is within a
  147. * single stripe.
  148. * - Memory condition only permitted a shorter IO. (A user can use @length=~0
  149. * And check the returned ios->length for max_io_size.)
  150. *
  151. * The caller must check returned ios->length (and/or ios->nr_pages) and
  152. * re-issue these pages that fall outside of ios->length
  153. */
  154. int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
  155. bool is_reading, u64 offset, u64 length,
  156. struct ore_io_state **pios)
  157. {
  158. struct ore_io_state *ios;
  159. unsigned numdevs = layout->group_width * layout->mirrors_p1;
  160. int ret;
  161. ret = _get_io_state(layout, oc, numdevs, pios);
  162. if (unlikely(ret))
  163. return ret;
  164. ios = *pios;
  165. ios->reading = is_reading;
  166. ios->offset = offset;
  167. if (length) {
  168. ore_calc_stripe_info(layout, offset, &ios->si);
  169. ios->length = (length <= ios->si.group_length) ? length :
  170. ios->si.group_length;
  171. ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
  172. }
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(ore_get_rw_state);
  176. /* Allocate an io_state for all the devices in the comps array
  177. *
  178. * This version of io_state allocation is used mostly by create/remove
  179. * and trunc where we currently need all the devices. The only wastful
  180. * bit is the read/write_attributes with no IO. Those sites should
  181. * be converted to use ore_get_rw_state() with length=0
  182. */
  183. int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
  184. struct ore_io_state **pios)
  185. {
  186. return _get_io_state(layout, oc, oc->numdevs, pios);
  187. }
  188. EXPORT_SYMBOL(ore_get_io_state);
  189. void ore_put_io_state(struct ore_io_state *ios)
  190. {
  191. if (ios) {
  192. unsigned i;
  193. for (i = 0; i < ios->numdevs; i++) {
  194. struct ore_per_dev_state *per_dev = &ios->per_dev[i];
  195. if (per_dev->or)
  196. osd_end_request(per_dev->or);
  197. if (per_dev->bio)
  198. bio_put(per_dev->bio);
  199. }
  200. kfree(ios);
  201. }
  202. }
  203. EXPORT_SYMBOL(ore_put_io_state);
  204. static void _sync_done(struct ore_io_state *ios, void *p)
  205. {
  206. struct completion *waiting = p;
  207. complete(waiting);
  208. }
  209. static void _last_io(struct kref *kref)
  210. {
  211. struct ore_io_state *ios = container_of(
  212. kref, struct ore_io_state, kref);
  213. ios->done(ios, ios->private);
  214. }
  215. static void _done_io(struct osd_request *or, void *p)
  216. {
  217. struct ore_io_state *ios = p;
  218. kref_put(&ios->kref, _last_io);
  219. }
  220. static int ore_io_execute(struct ore_io_state *ios)
  221. {
  222. DECLARE_COMPLETION_ONSTACK(wait);
  223. bool sync = (ios->done == NULL);
  224. int i, ret;
  225. if (sync) {
  226. ios->done = _sync_done;
  227. ios->private = &wait;
  228. }
  229. for (i = 0; i < ios->numdevs; i++) {
  230. struct osd_request *or = ios->per_dev[i].or;
  231. if (unlikely(!or))
  232. continue;
  233. ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
  234. if (unlikely(ret)) {
  235. ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
  236. ret);
  237. return ret;
  238. }
  239. }
  240. kref_init(&ios->kref);
  241. for (i = 0; i < ios->numdevs; i++) {
  242. struct osd_request *or = ios->per_dev[i].or;
  243. if (unlikely(!or))
  244. continue;
  245. kref_get(&ios->kref);
  246. osd_execute_request_async(or, _done_io, ios);
  247. }
  248. kref_put(&ios->kref, _last_io);
  249. ret = 0;
  250. if (sync) {
  251. wait_for_completion(&wait);
  252. ret = ore_check_io(ios, NULL);
  253. }
  254. return ret;
  255. }
  256. static void _clear_bio(struct bio *bio)
  257. {
  258. struct bio_vec *bv;
  259. unsigned i;
  260. __bio_for_each_segment(bv, bio, i, 0) {
  261. unsigned this_count = bv->bv_len;
  262. if (likely(PAGE_SIZE == this_count))
  263. clear_highpage(bv->bv_page);
  264. else
  265. zero_user(bv->bv_page, bv->bv_offset, this_count);
  266. }
  267. }
  268. int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
  269. {
  270. enum osd_err_priority acumulated_osd_err = 0;
  271. int acumulated_lin_err = 0;
  272. int i;
  273. for (i = 0; i < ios->numdevs; i++) {
  274. struct osd_sense_info osi;
  275. struct ore_per_dev_state *per_dev = &ios->per_dev[i];
  276. struct osd_request *or = per_dev->or;
  277. int ret;
  278. if (unlikely(!or))
  279. continue;
  280. ret = osd_req_decode_sense(or, &osi);
  281. if (likely(!ret))
  282. continue;
  283. if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
  284. /* start read offset passed endof file */
  285. _clear_bio(per_dev->bio);
  286. ORE_DBGMSG("start read offset passed end of file "
  287. "offset=0x%llx, length=0x%llx\n",
  288. _LLU(per_dev->offset),
  289. _LLU(per_dev->length));
  290. continue; /* we recovered */
  291. }
  292. if (on_dev_error) {
  293. u64 residual = ios->reading ?
  294. or->in.residual : or->out.residual;
  295. u64 offset = (ios->offset + ios->length) - residual;
  296. struct ore_dev *od = ios->oc->ods[
  297. per_dev->dev - ios->oc->first_dev];
  298. on_dev_error(ios, od, per_dev->dev, osi.osd_err_pri,
  299. offset, residual);
  300. }
  301. if (osi.osd_err_pri >= acumulated_osd_err) {
  302. acumulated_osd_err = osi.osd_err_pri;
  303. acumulated_lin_err = ret;
  304. }
  305. }
  306. return acumulated_lin_err;
  307. }
  308. EXPORT_SYMBOL(ore_check_io);
  309. /*
  310. * L - logical offset into the file
  311. *
  312. * U - The number of bytes in a stripe within a group
  313. *
  314. * U = stripe_unit * group_width
  315. *
  316. * T - The number of bytes striped within a group of component objects
  317. * (before advancing to the next group)
  318. *
  319. * T = stripe_unit * group_width * group_depth
  320. *
  321. * S - The number of bytes striped across all component objects
  322. * before the pattern repeats
  323. *
  324. * S = stripe_unit * group_width * group_depth * group_count
  325. *
  326. * M - The "major" (i.e., across all components) stripe number
  327. *
  328. * M = L / S
  329. *
  330. * G - Counts the groups from the beginning of the major stripe
  331. *
  332. * G = (L - (M * S)) / T [or (L % S) / T]
  333. *
  334. * H - The byte offset within the group
  335. *
  336. * H = (L - (M * S)) % T [or (L % S) % T]
  337. *
  338. * N - The "minor" (i.e., across the group) stripe number
  339. *
  340. * N = H / U
  341. *
  342. * C - The component index coresponding to L
  343. *
  344. * C = (H - (N * U)) / stripe_unit + G * group_width
  345. * [or (L % U) / stripe_unit + G * group_width]
  346. *
  347. * O - The component offset coresponding to L
  348. *
  349. * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
  350. */
  351. void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
  352. struct ore_striping_info *si)
  353. {
  354. u32 stripe_unit = layout->stripe_unit;
  355. u32 group_width = layout->group_width;
  356. u64 group_depth = layout->group_depth;
  357. u32 U = stripe_unit * group_width;
  358. u64 T = U * group_depth;
  359. u64 S = T * layout->group_count;
  360. u64 M = div64_u64(file_offset, S);
  361. /*
  362. G = (L - (M * S)) / T
  363. H = (L - (M * S)) % T
  364. */
  365. u64 LmodS = file_offset - M * S;
  366. u32 G = div64_u64(LmodS, T);
  367. u64 H = LmodS - G * T;
  368. u32 N = div_u64(H, U);
  369. /* "H - (N * U)" is just "H % U" so it's bound to u32 */
  370. si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
  371. si->dev *= layout->mirrors_p1;
  372. div_u64_rem(file_offset, stripe_unit, &si->unit_off);
  373. si->obj_offset = si->unit_off + (N * stripe_unit) +
  374. (M * group_depth * stripe_unit);
  375. si->group_length = T - H;
  376. si->M = M;
  377. }
  378. EXPORT_SYMBOL(ore_calc_stripe_info);
  379. static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
  380. unsigned pgbase, struct ore_per_dev_state *per_dev,
  381. int cur_len)
  382. {
  383. unsigned pg = *cur_pg;
  384. struct request_queue *q =
  385. osd_request_queue(_ios_od(ios, per_dev->dev));
  386. unsigned len = cur_len;
  387. int ret;
  388. if (per_dev->bio == NULL) {
  389. unsigned pages_in_stripe = ios->layout->group_width *
  390. (ios->layout->stripe_unit / PAGE_SIZE);
  391. unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
  392. ios->layout->group_width;
  393. per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
  394. if (unlikely(!per_dev->bio)) {
  395. ORE_DBGMSG("Failed to allocate BIO size=%u\n",
  396. bio_size);
  397. ret = -ENOMEM;
  398. goto out;
  399. }
  400. }
  401. while (cur_len > 0) {
  402. unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
  403. unsigned added_len;
  404. BUG_ON(ios->nr_pages <= pg);
  405. cur_len -= pglen;
  406. added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
  407. pglen, pgbase);
  408. if (unlikely(pglen != added_len)) {
  409. ret = -ENOMEM;
  410. goto out;
  411. }
  412. pgbase = 0;
  413. ++pg;
  414. }
  415. BUG_ON(cur_len);
  416. per_dev->length += len;
  417. *cur_pg = pg;
  418. ret = 0;
  419. out: /* we fail the complete unit on an error eg don't advance
  420. * per_dev->length and cur_pg. This means that we might have a bigger
  421. * bio than the CDB requested length (per_dev->length). That's fine
  422. * only the oposite is fatal.
  423. */
  424. return ret;
  425. }
  426. static int _prepare_for_striping(struct ore_io_state *ios)
  427. {
  428. struct ore_striping_info *si = &ios->si;
  429. unsigned stripe_unit = ios->layout->stripe_unit;
  430. unsigned mirrors_p1 = ios->layout->mirrors_p1;
  431. unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
  432. unsigned dev = si->dev;
  433. unsigned first_dev = dev - (dev % devs_in_group);
  434. unsigned cur_pg = ios->pages_consumed;
  435. u64 length = ios->length;
  436. int ret = 0;
  437. if (!ios->pages) {
  438. ios->numdevs = ios->layout->mirrors_p1;
  439. return 0;
  440. }
  441. BUG_ON(length > si->group_length);
  442. while (length) {
  443. unsigned comp = dev - first_dev;
  444. struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
  445. unsigned cur_len, page_off = 0;
  446. if (!per_dev->length) {
  447. per_dev->dev = dev;
  448. if (dev < si->dev) {
  449. per_dev->offset = si->obj_offset + stripe_unit -
  450. si->unit_off;
  451. cur_len = stripe_unit;
  452. } else if (dev == si->dev) {
  453. per_dev->offset = si->obj_offset;
  454. cur_len = stripe_unit - si->unit_off;
  455. page_off = si->unit_off & ~PAGE_MASK;
  456. BUG_ON(page_off && (page_off != ios->pgbase));
  457. } else { /* dev > si->dev */
  458. per_dev->offset = si->obj_offset - si->unit_off;
  459. cur_len = stripe_unit;
  460. }
  461. } else {
  462. cur_len = stripe_unit;
  463. }
  464. if (cur_len >= length)
  465. cur_len = length;
  466. ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
  467. cur_len);
  468. if (unlikely(ret))
  469. goto out;
  470. dev += mirrors_p1;
  471. dev = (dev % devs_in_group) + first_dev;
  472. length -= cur_len;
  473. }
  474. out:
  475. ios->numdevs = devs_in_group;
  476. ios->pages_consumed = cur_pg;
  477. if (unlikely(ret)) {
  478. if (length == ios->length)
  479. return ret;
  480. else
  481. ios->length -= length;
  482. }
  483. return 0;
  484. }
  485. int ore_create(struct ore_io_state *ios)
  486. {
  487. int i, ret;
  488. for (i = 0; i < ios->oc->numdevs; i++) {
  489. struct osd_request *or;
  490. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  491. if (unlikely(!or)) {
  492. ORE_ERR("%s: osd_start_request failed\n", __func__);
  493. ret = -ENOMEM;
  494. goto out;
  495. }
  496. ios->per_dev[i].or = or;
  497. ios->numdevs++;
  498. osd_req_create_object(or, _ios_obj(ios, i));
  499. }
  500. ret = ore_io_execute(ios);
  501. out:
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(ore_create);
  505. int ore_remove(struct ore_io_state *ios)
  506. {
  507. int i, ret;
  508. for (i = 0; i < ios->oc->numdevs; i++) {
  509. struct osd_request *or;
  510. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  511. if (unlikely(!or)) {
  512. ORE_ERR("%s: osd_start_request failed\n", __func__);
  513. ret = -ENOMEM;
  514. goto out;
  515. }
  516. ios->per_dev[i].or = or;
  517. ios->numdevs++;
  518. osd_req_remove_object(or, _ios_obj(ios, i));
  519. }
  520. ret = ore_io_execute(ios);
  521. out:
  522. return ret;
  523. }
  524. EXPORT_SYMBOL(ore_remove);
  525. static int _write_mirror(struct ore_io_state *ios, int cur_comp)
  526. {
  527. struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
  528. unsigned dev = ios->per_dev[cur_comp].dev;
  529. unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
  530. int ret = 0;
  531. if (ios->pages && !master_dev->length)
  532. return 0; /* Just an empty slot */
  533. for (; cur_comp < last_comp; ++cur_comp, ++dev) {
  534. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  535. struct osd_request *or;
  536. or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
  537. if (unlikely(!or)) {
  538. ORE_ERR("%s: osd_start_request failed\n", __func__);
  539. ret = -ENOMEM;
  540. goto out;
  541. }
  542. per_dev->or = or;
  543. if (ios->pages) {
  544. struct bio *bio;
  545. if (per_dev != master_dev) {
  546. bio = bio_kmalloc(GFP_KERNEL,
  547. master_dev->bio->bi_max_vecs);
  548. if (unlikely(!bio)) {
  549. ORE_DBGMSG(
  550. "Failed to allocate BIO size=%u\n",
  551. master_dev->bio->bi_max_vecs);
  552. ret = -ENOMEM;
  553. goto out;
  554. }
  555. __bio_clone(bio, master_dev->bio);
  556. bio->bi_bdev = NULL;
  557. bio->bi_next = NULL;
  558. per_dev->offset = master_dev->offset;
  559. per_dev->length = master_dev->length;
  560. per_dev->bio = bio;
  561. per_dev->dev = dev;
  562. } else {
  563. bio = master_dev->bio;
  564. /* FIXME: bio_set_dir() */
  565. bio->bi_rw |= REQ_WRITE;
  566. }
  567. osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
  568. bio, per_dev->length);
  569. ORE_DBGMSG("write(0x%llx) offset=0x%llx "
  570. "length=0x%llx dev=%d\n",
  571. _LLU(_ios_obj(ios, dev)->id),
  572. _LLU(per_dev->offset),
  573. _LLU(per_dev->length), dev);
  574. } else if (ios->kern_buff) {
  575. per_dev->offset = ios->si.obj_offset;
  576. per_dev->dev = ios->si.dev + dev;
  577. /* no cross device without page array */
  578. BUG_ON((ios->layout->group_width > 1) &&
  579. (ios->si.unit_off + ios->length >
  580. ios->layout->stripe_unit));
  581. ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
  582. per_dev->offset,
  583. ios->kern_buff, ios->length);
  584. if (unlikely(ret))
  585. goto out;
  586. ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
  587. "length=0x%llx dev=%d\n",
  588. _LLU(_ios_obj(ios, dev)->id),
  589. _LLU(per_dev->offset),
  590. _LLU(ios->length), per_dev->dev);
  591. } else {
  592. osd_req_set_attributes(or, _ios_obj(ios, dev));
  593. ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
  594. _LLU(_ios_obj(ios, dev)->id),
  595. ios->out_attr_len, dev);
  596. }
  597. if (ios->out_attr)
  598. osd_req_add_set_attr_list(or, ios->out_attr,
  599. ios->out_attr_len);
  600. if (ios->in_attr)
  601. osd_req_add_get_attr_list(or, ios->in_attr,
  602. ios->in_attr_len);
  603. }
  604. out:
  605. return ret;
  606. }
  607. int ore_write(struct ore_io_state *ios)
  608. {
  609. int i;
  610. int ret;
  611. ret = _prepare_for_striping(ios);
  612. if (unlikely(ret))
  613. return ret;
  614. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  615. ret = _write_mirror(ios, i);
  616. if (unlikely(ret))
  617. return ret;
  618. }
  619. ret = ore_io_execute(ios);
  620. return ret;
  621. }
  622. EXPORT_SYMBOL(ore_write);
  623. static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
  624. {
  625. struct osd_request *or;
  626. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  627. struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
  628. unsigned first_dev = (unsigned)obj->id;
  629. if (ios->pages && !per_dev->length)
  630. return 0; /* Just an empty slot */
  631. first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
  632. or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
  633. if (unlikely(!or)) {
  634. ORE_ERR("%s: osd_start_request failed\n", __func__);
  635. return -ENOMEM;
  636. }
  637. per_dev->or = or;
  638. if (ios->pages) {
  639. osd_req_read(or, obj, per_dev->offset,
  640. per_dev->bio, per_dev->length);
  641. ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
  642. " dev=%d\n", _LLU(obj->id),
  643. _LLU(per_dev->offset), _LLU(per_dev->length),
  644. first_dev);
  645. } else {
  646. BUG_ON(ios->kern_buff);
  647. osd_req_get_attributes(or, obj);
  648. ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
  649. _LLU(obj->id),
  650. ios->in_attr_len, first_dev);
  651. }
  652. if (ios->out_attr)
  653. osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
  654. if (ios->in_attr)
  655. osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
  656. return 0;
  657. }
  658. int ore_read(struct ore_io_state *ios)
  659. {
  660. int i;
  661. int ret;
  662. ret = _prepare_for_striping(ios);
  663. if (unlikely(ret))
  664. return ret;
  665. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  666. ret = _read_mirror(ios, i);
  667. if (unlikely(ret))
  668. return ret;
  669. }
  670. ret = ore_io_execute(ios);
  671. return ret;
  672. }
  673. EXPORT_SYMBOL(ore_read);
  674. int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
  675. {
  676. struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
  677. void *iter = NULL;
  678. int nelem;
  679. do {
  680. nelem = 1;
  681. osd_req_decode_get_attr_list(ios->per_dev[0].or,
  682. &cur_attr, &nelem, &iter);
  683. if ((cur_attr.attr_page == attr->attr_page) &&
  684. (cur_attr.attr_id == attr->attr_id)) {
  685. attr->len = cur_attr.len;
  686. attr->val_ptr = cur_attr.val_ptr;
  687. return 0;
  688. }
  689. } while (iter);
  690. return -EIO;
  691. }
  692. EXPORT_SYMBOL(extract_attr_from_ios);
  693. static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
  694. struct osd_attr *attr)
  695. {
  696. int last_comp = cur_comp + ios->layout->mirrors_p1;
  697. for (; cur_comp < last_comp; ++cur_comp) {
  698. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  699. struct osd_request *or;
  700. or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
  701. if (unlikely(!or)) {
  702. ORE_ERR("%s: osd_start_request failed\n", __func__);
  703. return -ENOMEM;
  704. }
  705. per_dev->or = or;
  706. osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
  707. osd_req_add_set_attr_list(or, attr, 1);
  708. }
  709. return 0;
  710. }
  711. struct _trunc_info {
  712. struct ore_striping_info si;
  713. u64 prev_group_obj_off;
  714. u64 next_group_obj_off;
  715. unsigned first_group_dev;
  716. unsigned nex_group_dev;
  717. };
  718. static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
  719. struct _trunc_info *ti)
  720. {
  721. unsigned stripe_unit = layout->stripe_unit;
  722. ore_calc_stripe_info(layout, file_offset, &ti->si);
  723. ti->prev_group_obj_off = ti->si.M * stripe_unit;
  724. ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
  725. ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
  726. ti->nex_group_dev = ti->first_group_dev + layout->group_width;
  727. }
  728. int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
  729. u64 size)
  730. {
  731. struct ore_io_state *ios;
  732. struct exofs_trunc_attr {
  733. struct osd_attr attr;
  734. __be64 newsize;
  735. } *size_attrs;
  736. struct _trunc_info ti;
  737. int i, ret;
  738. ret = ore_get_io_state(layout, oc, &ios);
  739. if (unlikely(ret))
  740. return ret;
  741. _calc_trunk_info(ios->layout, size, &ti);
  742. size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
  743. GFP_KERNEL);
  744. if (unlikely(!size_attrs)) {
  745. ret = -ENOMEM;
  746. goto out;
  747. }
  748. ios->numdevs = ios->oc->numdevs;
  749. for (i = 0; i < ios->numdevs; ++i) {
  750. struct exofs_trunc_attr *size_attr = &size_attrs[i];
  751. u64 obj_size;
  752. if (i < ti.first_group_dev)
  753. obj_size = ti.prev_group_obj_off;
  754. else if (i >= ti.nex_group_dev)
  755. obj_size = ti.next_group_obj_off;
  756. else if (i < ti.si.dev) /* dev within this group */
  757. obj_size = ti.si.obj_offset +
  758. ios->layout->stripe_unit - ti.si.unit_off;
  759. else if (i == ti.si.dev)
  760. obj_size = ti.si.obj_offset;
  761. else /* i > ti.dev */
  762. obj_size = ti.si.obj_offset - ti.si.unit_off;
  763. size_attr->newsize = cpu_to_be64(obj_size);
  764. size_attr->attr = g_attr_logical_length;
  765. size_attr->attr.val_ptr = &size_attr->newsize;
  766. ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
  767. _LLU(oc->comps->obj.id), _LLU(obj_size), i);
  768. ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
  769. &size_attr->attr);
  770. if (unlikely(ret))
  771. goto out;
  772. }
  773. ret = ore_io_execute(ios);
  774. out:
  775. kfree(size_attrs);
  776. ore_put_io_state(ios);
  777. return ret;
  778. }
  779. EXPORT_SYMBOL(ore_truncate);
  780. const struct osd_attr g_attr_logical_length = ATTR_DEF(
  781. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  782. EXPORT_SYMBOL(g_attr_logical_length);