ore_raid.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2011
  3. * Boaz Harrosh <bharrosh@panasas.com>
  4. *
  5. * This file is part of the objects raid engine (ore).
  6. *
  7. * It is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with "ore". If not, write to the Free Software Foundation, Inc:
  13. * "Free Software Foundation <info@fsf.org>"
  14. */
  15. #include <linux/gfp.h>
  16. #include "ore_raid.h"
  17. struct page *_raid_page_alloc(void)
  18. {
  19. return alloc_page(GFP_KERNEL);
  20. }
  21. void _raid_page_free(struct page *p)
  22. {
  23. __free_page(p);
  24. }
  25. void _ore_add_sg_seg(struct ore_per_dev_state *per_dev, unsigned cur_len,
  26. bool not_last)
  27. {
  28. struct osd_sg_entry *sge;
  29. ORE_DBGMSG("dev=%d cur_len=0x%x not_last=%d cur_sg=%d "
  30. "offset=0x%llx length=0x%x last_sgs_total=0x%x\n",
  31. per_dev->dev, cur_len, not_last, per_dev->cur_sg,
  32. _LLU(per_dev->offset), per_dev->length,
  33. per_dev->last_sgs_total);
  34. if (!per_dev->cur_sg) {
  35. sge = per_dev->sglist;
  36. /* First time we prepare two entries */
  37. if (per_dev->length) {
  38. ++per_dev->cur_sg;
  39. sge->offset = per_dev->offset;
  40. sge->len = per_dev->length;
  41. } else {
  42. /* Here the parity is the first unit of this object.
  43. * This happens every time we reach a parity device on
  44. * the same stripe as the per_dev->offset. We need to
  45. * just skip this unit.
  46. */
  47. per_dev->offset += cur_len;
  48. return;
  49. }
  50. } else {
  51. /* finalize the last one */
  52. sge = &per_dev->sglist[per_dev->cur_sg - 1];
  53. sge->len = per_dev->length - per_dev->last_sgs_total;
  54. }
  55. if (not_last) {
  56. /* Partly prepare the next one */
  57. struct osd_sg_entry *next_sge = sge + 1;
  58. ++per_dev->cur_sg;
  59. next_sge->offset = sge->offset + sge->len + cur_len;
  60. /* Save cur len so we know how mutch was added next time */
  61. per_dev->last_sgs_total = per_dev->length;
  62. next_sge->len = 0;
  63. } else if (!sge->len) {
  64. /* Optimize for when the last unit is a parity */
  65. --per_dev->cur_sg;
  66. }
  67. }
  68. /* In writes @cur_len means length left. .i.e cur_len==0 is the last parity U */
  69. int _ore_add_parity_unit(struct ore_io_state *ios,
  70. struct ore_striping_info *si,
  71. struct ore_per_dev_state *per_dev,
  72. unsigned cur_len)
  73. {
  74. if (ios->reading) {
  75. BUG_ON(per_dev->cur_sg >= ios->sgs_per_dev);
  76. _ore_add_sg_seg(per_dev, cur_len, true);
  77. } else {
  78. struct page **pages = ios->parity_pages + ios->cur_par_page;
  79. unsigned num_pages = ios->layout->stripe_unit / PAGE_SIZE;
  80. unsigned array_start = 0;
  81. unsigned i;
  82. int ret;
  83. for (i = 0; i < num_pages; i++) {
  84. pages[i] = _raid_page_alloc();
  85. if (unlikely(!pages[i]))
  86. return -ENOMEM;
  87. ++(ios->cur_par_page);
  88. /* TODO: only read support for now */
  89. clear_highpage(pages[i]);
  90. }
  91. ORE_DBGMSG("writing dev=%d num_pages=%d cur_par_page=%d",
  92. per_dev->dev, num_pages, ios->cur_par_page);
  93. ret = _ore_add_stripe_unit(ios, &array_start, 0, pages,
  94. per_dev, num_pages * PAGE_SIZE);
  95. if (unlikely(ret))
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. int _ore_post_alloc_raid_stuff(struct ore_io_state *ios)
  101. {
  102. /*TODO: Only raid writes has stuff to add here */
  103. return 0;
  104. }
  105. void _ore_free_raid_stuff(struct ore_io_state *ios)
  106. {
  107. if (ios->parity_pages) { /* writing and raid */
  108. unsigned i;
  109. for (i = 0; i < ios->cur_par_page; i++) {
  110. struct page *page = ios->parity_pages[i];
  111. if (page)
  112. _raid_page_free(page);
  113. }
  114. if (ios->extra_part_alloc)
  115. kfree(ios->parity_pages);
  116. } else {
  117. /* Will only be set if raid reading && sglist is big */
  118. if (ios->extra_part_alloc)
  119. kfree(ios->per_dev[0].sglist);
  120. }
  121. }