ore.c 30 KB

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