ore.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
  43. {
  44. return ios->oc->comps[index & ios->oc->single_comp].cred;
  45. }
  46. static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
  47. {
  48. return &ios->oc->comps[index & ios->oc->single_comp].obj;
  49. }
  50. static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
  51. {
  52. return ore_comp_dev(ios->oc, index);
  53. }
  54. int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
  55. bool is_reading, u64 offset, u64 length,
  56. struct ore_io_state **pios)
  57. {
  58. struct ore_io_state *ios;
  59. /*TODO: Maybe use kmem_cach per sbi of size
  60. * exofs_io_state_size(layout->s_numdevs)
  61. */
  62. ios = kzalloc(ore_io_state_size(oc->numdevs), GFP_KERNEL);
  63. if (unlikely(!ios)) {
  64. ORE_DBGMSG("Failed kzalloc bytes=%d\n",
  65. ore_io_state_size(oc->numdevs));
  66. *pios = NULL;
  67. return -ENOMEM;
  68. }
  69. ios->layout = layout;
  70. ios->oc = oc;
  71. ios->offset = offset;
  72. ios->length = length;
  73. ios->reading = is_reading;
  74. *pios = ios;
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(ore_get_rw_state);
  78. int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
  79. struct ore_io_state **ios)
  80. {
  81. return ore_get_rw_state(layout, oc, true, 0, 0, ios);
  82. }
  83. EXPORT_SYMBOL(ore_get_io_state);
  84. void ore_put_io_state(struct ore_io_state *ios)
  85. {
  86. if (ios) {
  87. unsigned i;
  88. for (i = 0; i < ios->numdevs; i++) {
  89. struct ore_per_dev_state *per_dev = &ios->per_dev[i];
  90. if (per_dev->or)
  91. osd_end_request(per_dev->or);
  92. if (per_dev->bio)
  93. bio_put(per_dev->bio);
  94. }
  95. kfree(ios);
  96. }
  97. }
  98. EXPORT_SYMBOL(ore_put_io_state);
  99. static void _sync_done(struct ore_io_state *ios, void *p)
  100. {
  101. struct completion *waiting = p;
  102. complete(waiting);
  103. }
  104. static void _last_io(struct kref *kref)
  105. {
  106. struct ore_io_state *ios = container_of(
  107. kref, struct ore_io_state, kref);
  108. ios->done(ios, ios->private);
  109. }
  110. static void _done_io(struct osd_request *or, void *p)
  111. {
  112. struct ore_io_state *ios = p;
  113. kref_put(&ios->kref, _last_io);
  114. }
  115. static int ore_io_execute(struct ore_io_state *ios)
  116. {
  117. DECLARE_COMPLETION_ONSTACK(wait);
  118. bool sync = (ios->done == NULL);
  119. int i, ret;
  120. if (sync) {
  121. ios->done = _sync_done;
  122. ios->private = &wait;
  123. }
  124. for (i = 0; i < ios->numdevs; i++) {
  125. struct osd_request *or = ios->per_dev[i].or;
  126. if (unlikely(!or))
  127. continue;
  128. ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
  129. if (unlikely(ret)) {
  130. ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
  131. ret);
  132. return ret;
  133. }
  134. }
  135. kref_init(&ios->kref);
  136. for (i = 0; i < ios->numdevs; i++) {
  137. struct osd_request *or = ios->per_dev[i].or;
  138. if (unlikely(!or))
  139. continue;
  140. kref_get(&ios->kref);
  141. osd_execute_request_async(or, _done_io, ios);
  142. }
  143. kref_put(&ios->kref, _last_io);
  144. ret = 0;
  145. if (sync) {
  146. wait_for_completion(&wait);
  147. ret = ore_check_io(ios, NULL);
  148. }
  149. return ret;
  150. }
  151. static void _clear_bio(struct bio *bio)
  152. {
  153. struct bio_vec *bv;
  154. unsigned i;
  155. __bio_for_each_segment(bv, bio, i, 0) {
  156. unsigned this_count = bv->bv_len;
  157. if (likely(PAGE_SIZE == this_count))
  158. clear_highpage(bv->bv_page);
  159. else
  160. zero_user(bv->bv_page, bv->bv_offset, this_count);
  161. }
  162. }
  163. int ore_check_io(struct ore_io_state *ios, u64 *resid)
  164. {
  165. enum osd_err_priority acumulated_osd_err = 0;
  166. int acumulated_lin_err = 0;
  167. int i;
  168. for (i = 0; i < ios->numdevs; i++) {
  169. struct osd_sense_info osi;
  170. struct osd_request *or = ios->per_dev[i].or;
  171. int ret;
  172. if (unlikely(!or))
  173. continue;
  174. ret = osd_req_decode_sense(or, &osi);
  175. if (likely(!ret))
  176. continue;
  177. if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
  178. /* start read offset passed endof file */
  179. _clear_bio(ios->per_dev[i].bio);
  180. ORE_DBGMSG("start read offset passed end of file "
  181. "offset=0x%llx, length=0x%llx\n",
  182. _LLU(ios->per_dev[i].offset),
  183. _LLU(ios->per_dev[i].length));
  184. continue; /* we recovered */
  185. }
  186. if (osi.osd_err_pri >= acumulated_osd_err) {
  187. acumulated_osd_err = osi.osd_err_pri;
  188. acumulated_lin_err = ret;
  189. }
  190. }
  191. /* TODO: raid specific residual calculations */
  192. if (resid) {
  193. if (likely(!acumulated_lin_err))
  194. *resid = 0;
  195. else
  196. *resid = ios->length;
  197. }
  198. return acumulated_lin_err;
  199. }
  200. EXPORT_SYMBOL(ore_check_io);
  201. /*
  202. * L - logical offset into the file
  203. *
  204. * U - The number of bytes in a stripe within a group
  205. *
  206. * U = stripe_unit * group_width
  207. *
  208. * T - The number of bytes striped within a group of component objects
  209. * (before advancing to the next group)
  210. *
  211. * T = stripe_unit * group_width * group_depth
  212. *
  213. * S - The number of bytes striped across all component objects
  214. * before the pattern repeats
  215. *
  216. * S = stripe_unit * group_width * group_depth * group_count
  217. *
  218. * M - The "major" (i.e., across all components) stripe number
  219. *
  220. * M = L / S
  221. *
  222. * G - Counts the groups from the beginning of the major stripe
  223. *
  224. * G = (L - (M * S)) / T [or (L % S) / T]
  225. *
  226. * H - The byte offset within the group
  227. *
  228. * H = (L - (M * S)) % T [or (L % S) % T]
  229. *
  230. * N - The "minor" (i.e., across the group) stripe number
  231. *
  232. * N = H / U
  233. *
  234. * C - The component index coresponding to L
  235. *
  236. * C = (H - (N * U)) / stripe_unit + G * group_width
  237. * [or (L % U) / stripe_unit + G * group_width]
  238. *
  239. * O - The component offset coresponding to L
  240. *
  241. * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
  242. */
  243. static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
  244. struct ore_striping_info *si)
  245. {
  246. u32 stripe_unit = layout->stripe_unit;
  247. u32 group_width = layout->group_width;
  248. u64 group_depth = layout->group_depth;
  249. u32 U = stripe_unit * group_width;
  250. u64 T = U * group_depth;
  251. u64 S = T * layout->group_count;
  252. u64 M = div64_u64(file_offset, S);
  253. /*
  254. G = (L - (M * S)) / T
  255. H = (L - (M * S)) % T
  256. */
  257. u64 LmodS = file_offset - M * S;
  258. u32 G = div64_u64(LmodS, T);
  259. u64 H = LmodS - G * T;
  260. u32 N = div_u64(H, U);
  261. /* "H - (N * U)" is just "H % U" so it's bound to u32 */
  262. si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
  263. si->dev *= layout->mirrors_p1;
  264. div_u64_rem(file_offset, stripe_unit, &si->unit_off);
  265. si->obj_offset = si->unit_off + (N * stripe_unit) +
  266. (M * group_depth * stripe_unit);
  267. si->group_length = T - H;
  268. si->M = M;
  269. }
  270. static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
  271. unsigned pgbase, struct ore_per_dev_state *per_dev,
  272. int cur_len)
  273. {
  274. unsigned pg = *cur_pg;
  275. struct request_queue *q =
  276. osd_request_queue(_ios_od(ios, per_dev->dev));
  277. per_dev->length += cur_len;
  278. if (per_dev->bio == NULL) {
  279. unsigned pages_in_stripe = ios->layout->group_width *
  280. (ios->layout->stripe_unit / PAGE_SIZE);
  281. unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
  282. ios->layout->group_width;
  283. per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
  284. if (unlikely(!per_dev->bio)) {
  285. ORE_DBGMSG("Failed to allocate BIO size=%u\n",
  286. bio_size);
  287. return -ENOMEM;
  288. }
  289. }
  290. while (cur_len > 0) {
  291. unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
  292. unsigned added_len;
  293. BUG_ON(ios->nr_pages <= pg);
  294. cur_len -= pglen;
  295. added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
  296. pglen, pgbase);
  297. if (unlikely(pglen != added_len))
  298. return -ENOMEM;
  299. pgbase = 0;
  300. ++pg;
  301. }
  302. BUG_ON(cur_len);
  303. *cur_pg = pg;
  304. return 0;
  305. }
  306. static int _prepare_one_group(struct ore_io_state *ios, u64 length,
  307. struct ore_striping_info *si)
  308. {
  309. unsigned stripe_unit = ios->layout->stripe_unit;
  310. unsigned mirrors_p1 = ios->layout->mirrors_p1;
  311. unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
  312. unsigned dev = si->dev;
  313. unsigned first_dev = dev - (dev % devs_in_group);
  314. unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
  315. unsigned cur_pg = ios->pages_consumed;
  316. int ret = 0;
  317. while (length) {
  318. struct ore_per_dev_state *per_dev = &ios->per_dev[dev];
  319. unsigned cur_len, page_off = 0;
  320. if (!per_dev->length) {
  321. per_dev->dev = dev;
  322. if (dev < si->dev) {
  323. per_dev->offset = si->obj_offset + stripe_unit -
  324. si->unit_off;
  325. cur_len = stripe_unit;
  326. } else if (dev == si->dev) {
  327. per_dev->offset = si->obj_offset;
  328. cur_len = stripe_unit - si->unit_off;
  329. page_off = si->unit_off & ~PAGE_MASK;
  330. BUG_ON(page_off && (page_off != ios->pgbase));
  331. } else { /* dev > si->dev */
  332. per_dev->offset = si->obj_offset - si->unit_off;
  333. cur_len = stripe_unit;
  334. }
  335. if (max_comp < dev)
  336. max_comp = dev;
  337. } else {
  338. cur_len = stripe_unit;
  339. }
  340. if (cur_len >= length)
  341. cur_len = length;
  342. ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
  343. cur_len);
  344. if (unlikely(ret))
  345. goto out;
  346. dev += mirrors_p1;
  347. dev = (dev % devs_in_group) + first_dev;
  348. length -= cur_len;
  349. }
  350. out:
  351. ios->numdevs = max_comp + mirrors_p1;
  352. ios->pages_consumed = cur_pg;
  353. return ret;
  354. }
  355. static int _prepare_for_striping(struct ore_io_state *ios)
  356. {
  357. u64 length = ios->length;
  358. u64 offset = ios->offset;
  359. struct ore_striping_info si;
  360. int ret = 0;
  361. if (!ios->pages) {
  362. if (ios->kern_buff) {
  363. struct ore_per_dev_state *per_dev = &ios->per_dev[0];
  364. ore_calc_stripe_info(ios->layout, ios->offset, &si);
  365. per_dev->offset = si.obj_offset;
  366. per_dev->dev = si.dev;
  367. /* no cross device without page array */
  368. BUG_ON((ios->layout->group_width > 1) &&
  369. (si.unit_off + ios->length >
  370. ios->layout->stripe_unit));
  371. }
  372. ios->numdevs = ios->layout->mirrors_p1;
  373. return 0;
  374. }
  375. while (length) {
  376. ore_calc_stripe_info(ios->layout, offset, &si);
  377. if (length < si.group_length)
  378. si.group_length = length;
  379. ret = _prepare_one_group(ios, si.group_length, &si);
  380. if (unlikely(ret))
  381. goto out;
  382. offset += si.group_length;
  383. length -= si.group_length;
  384. }
  385. out:
  386. return ret;
  387. }
  388. int ore_create(struct ore_io_state *ios)
  389. {
  390. int i, ret;
  391. for (i = 0; i < ios->oc->numdevs; i++) {
  392. struct osd_request *or;
  393. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  394. if (unlikely(!or)) {
  395. ORE_ERR("%s: osd_start_request failed\n", __func__);
  396. ret = -ENOMEM;
  397. goto out;
  398. }
  399. ios->per_dev[i].or = or;
  400. ios->numdevs++;
  401. osd_req_create_object(or, _ios_obj(ios, i));
  402. }
  403. ret = ore_io_execute(ios);
  404. out:
  405. return ret;
  406. }
  407. EXPORT_SYMBOL(ore_create);
  408. int ore_remove(struct ore_io_state *ios)
  409. {
  410. int i, ret;
  411. for (i = 0; i < ios->oc->numdevs; i++) {
  412. struct osd_request *or;
  413. or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
  414. if (unlikely(!or)) {
  415. ORE_ERR("%s: osd_start_request failed\n", __func__);
  416. ret = -ENOMEM;
  417. goto out;
  418. }
  419. ios->per_dev[i].or = or;
  420. ios->numdevs++;
  421. osd_req_remove_object(or, _ios_obj(ios, i));
  422. }
  423. ret = ore_io_execute(ios);
  424. out:
  425. return ret;
  426. }
  427. EXPORT_SYMBOL(ore_remove);
  428. static int _write_mirror(struct ore_io_state *ios, int cur_comp)
  429. {
  430. struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
  431. unsigned dev = ios->per_dev[cur_comp].dev;
  432. unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
  433. int ret = 0;
  434. if (ios->pages && !master_dev->length)
  435. return 0; /* Just an empty slot */
  436. for (; cur_comp < last_comp; ++cur_comp, ++dev) {
  437. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  438. struct osd_request *or;
  439. or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
  440. if (unlikely(!or)) {
  441. ORE_ERR("%s: osd_start_request failed\n", __func__);
  442. ret = -ENOMEM;
  443. goto out;
  444. }
  445. per_dev->or = or;
  446. per_dev->offset = master_dev->offset;
  447. if (ios->pages) {
  448. struct bio *bio;
  449. if (per_dev != master_dev) {
  450. bio = bio_kmalloc(GFP_KERNEL,
  451. master_dev->bio->bi_max_vecs);
  452. if (unlikely(!bio)) {
  453. ORE_DBGMSG(
  454. "Failed to allocate BIO size=%u\n",
  455. master_dev->bio->bi_max_vecs);
  456. ret = -ENOMEM;
  457. goto out;
  458. }
  459. __bio_clone(bio, master_dev->bio);
  460. bio->bi_bdev = NULL;
  461. bio->bi_next = NULL;
  462. per_dev->length = master_dev->length;
  463. per_dev->bio = bio;
  464. per_dev->dev = dev;
  465. } else {
  466. bio = master_dev->bio;
  467. /* FIXME: bio_set_dir() */
  468. bio->bi_rw |= REQ_WRITE;
  469. }
  470. osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
  471. bio, per_dev->length);
  472. ORE_DBGMSG("write(0x%llx) offset=0x%llx "
  473. "length=0x%llx dev=%d\n",
  474. _LLU(_ios_obj(ios, dev)->id),
  475. _LLU(per_dev->offset),
  476. _LLU(per_dev->length), dev);
  477. } else if (ios->kern_buff) {
  478. ret = osd_req_write_kern(or, _ios_obj(ios, dev),
  479. per_dev->offset,
  480. ios->kern_buff, ios->length);
  481. if (unlikely(ret))
  482. goto out;
  483. ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
  484. "length=0x%llx dev=%d\n",
  485. _LLU(_ios_obj(ios, dev)->id),
  486. _LLU(per_dev->offset),
  487. _LLU(ios->length), dev);
  488. } else {
  489. osd_req_set_attributes(or, _ios_obj(ios, dev));
  490. ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
  491. _LLU(_ios_obj(ios, dev)->id),
  492. ios->out_attr_len, dev);
  493. }
  494. if (ios->out_attr)
  495. osd_req_add_set_attr_list(or, ios->out_attr,
  496. ios->out_attr_len);
  497. if (ios->in_attr)
  498. osd_req_add_get_attr_list(or, ios->in_attr,
  499. ios->in_attr_len);
  500. }
  501. out:
  502. return ret;
  503. }
  504. int ore_write(struct ore_io_state *ios)
  505. {
  506. int i;
  507. int ret;
  508. ret = _prepare_for_striping(ios);
  509. if (unlikely(ret))
  510. return ret;
  511. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  512. ret = _write_mirror(ios, i);
  513. if (unlikely(ret))
  514. return ret;
  515. }
  516. ret = ore_io_execute(ios);
  517. return ret;
  518. }
  519. EXPORT_SYMBOL(ore_write);
  520. static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
  521. {
  522. struct osd_request *or;
  523. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  524. struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
  525. unsigned first_dev = (unsigned)obj->id;
  526. if (ios->pages && !per_dev->length)
  527. return 0; /* Just an empty slot */
  528. first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
  529. or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
  530. if (unlikely(!or)) {
  531. ORE_ERR("%s: osd_start_request failed\n", __func__);
  532. return -ENOMEM;
  533. }
  534. per_dev->or = or;
  535. if (ios->pages) {
  536. osd_req_read(or, obj, per_dev->offset,
  537. per_dev->bio, per_dev->length);
  538. ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
  539. " dev=%d\n", _LLU(obj->id),
  540. _LLU(per_dev->offset), _LLU(per_dev->length),
  541. first_dev);
  542. } else if (ios->kern_buff) {
  543. int ret = osd_req_read_kern(or, obj, per_dev->offset,
  544. ios->kern_buff, ios->length);
  545. ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
  546. "length=0x%llx dev=%d ret=>%d\n",
  547. _LLU(obj->id), _LLU(per_dev->offset),
  548. _LLU(ios->length), first_dev, ret);
  549. if (unlikely(ret))
  550. return ret;
  551. } else {
  552. osd_req_get_attributes(or, obj);
  553. ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
  554. _LLU(obj->id),
  555. ios->in_attr_len, first_dev);
  556. }
  557. if (ios->out_attr)
  558. osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
  559. if (ios->in_attr)
  560. osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
  561. return 0;
  562. }
  563. int ore_read(struct ore_io_state *ios)
  564. {
  565. int i;
  566. int ret;
  567. ret = _prepare_for_striping(ios);
  568. if (unlikely(ret))
  569. return ret;
  570. for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
  571. ret = _read_mirror(ios, i);
  572. if (unlikely(ret))
  573. return ret;
  574. }
  575. ret = ore_io_execute(ios);
  576. return ret;
  577. }
  578. EXPORT_SYMBOL(ore_read);
  579. int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
  580. {
  581. struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
  582. void *iter = NULL;
  583. int nelem;
  584. do {
  585. nelem = 1;
  586. osd_req_decode_get_attr_list(ios->per_dev[0].or,
  587. &cur_attr, &nelem, &iter);
  588. if ((cur_attr.attr_page == attr->attr_page) &&
  589. (cur_attr.attr_id == attr->attr_id)) {
  590. attr->len = cur_attr.len;
  591. attr->val_ptr = cur_attr.val_ptr;
  592. return 0;
  593. }
  594. } while (iter);
  595. return -EIO;
  596. }
  597. EXPORT_SYMBOL(extract_attr_from_ios);
  598. static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
  599. struct osd_attr *attr)
  600. {
  601. int last_comp = cur_comp + ios->layout->mirrors_p1;
  602. for (; cur_comp < last_comp; ++cur_comp) {
  603. struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
  604. struct osd_request *or;
  605. or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
  606. if (unlikely(!or)) {
  607. ORE_ERR("%s: osd_start_request failed\n", __func__);
  608. return -ENOMEM;
  609. }
  610. per_dev->or = or;
  611. osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
  612. osd_req_add_set_attr_list(or, attr, 1);
  613. }
  614. return 0;
  615. }
  616. struct _trunc_info {
  617. struct ore_striping_info si;
  618. u64 prev_group_obj_off;
  619. u64 next_group_obj_off;
  620. unsigned first_group_dev;
  621. unsigned nex_group_dev;
  622. unsigned max_devs;
  623. };
  624. static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
  625. struct _trunc_info *ti)
  626. {
  627. unsigned stripe_unit = layout->stripe_unit;
  628. ore_calc_stripe_info(layout, file_offset, &ti->si);
  629. ti->prev_group_obj_off = ti->si.M * stripe_unit;
  630. ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
  631. ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
  632. ti->nex_group_dev = ti->first_group_dev + layout->group_width;
  633. ti->max_devs = layout->group_width * layout->group_count;
  634. }
  635. int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
  636. u64 size)
  637. {
  638. struct ore_io_state *ios;
  639. struct exofs_trunc_attr {
  640. struct osd_attr attr;
  641. __be64 newsize;
  642. } *size_attrs;
  643. struct _trunc_info ti;
  644. int i, ret;
  645. ret = ore_get_io_state(layout, oc, &ios);
  646. if (unlikely(ret))
  647. return ret;
  648. _calc_trunk_info(ios->layout, size, &ti);
  649. size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
  650. GFP_KERNEL);
  651. if (unlikely(!size_attrs)) {
  652. ret = -ENOMEM;
  653. goto out;
  654. }
  655. ios->numdevs = ios->oc->numdevs;
  656. for (i = 0; i < ti.max_devs; ++i) {
  657. struct exofs_trunc_attr *size_attr = &size_attrs[i];
  658. u64 obj_size;
  659. if (i < ti.first_group_dev)
  660. obj_size = ti.prev_group_obj_off;
  661. else if (i >= ti.nex_group_dev)
  662. obj_size = ti.next_group_obj_off;
  663. else if (i < ti.si.dev) /* dev within this group */
  664. obj_size = ti.si.obj_offset +
  665. ios->layout->stripe_unit - ti.si.unit_off;
  666. else if (i == ti.si.dev)
  667. obj_size = ti.si.obj_offset;
  668. else /* i > ti.dev */
  669. obj_size = ti.si.obj_offset - ti.si.unit_off;
  670. size_attr->newsize = cpu_to_be64(obj_size);
  671. size_attr->attr = g_attr_logical_length;
  672. size_attr->attr.val_ptr = &size_attr->newsize;
  673. ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
  674. _LLU(oc->comps->obj.id), _LLU(obj_size), i);
  675. ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
  676. &size_attr->attr);
  677. if (unlikely(ret))
  678. goto out;
  679. }
  680. ret = ore_io_execute(ios);
  681. out:
  682. kfree(size_attrs);
  683. ore_put_io_state(ios);
  684. return ret;
  685. }
  686. EXPORT_SYMBOL(ore_truncate);
  687. const struct osd_attr g_attr_logical_length = ATTR_DEF(
  688. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  689. EXPORT_SYMBOL(g_attr_logical_length);