ios.c 20 KB

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