bitmap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * bitmap.c - NTFS kernel bitmap handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2004-2005 Anton Altaparmakov
  5. *
  6. * This program/include file is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program/include file is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program (in the main directory of the Linux-NTFS
  18. * distribution in the file COPYING); if not, write to the Free Software
  19. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #ifdef NTFS_RW
  22. #include <linux/pagemap.h>
  23. #include "bitmap.h"
  24. #include "debug.h"
  25. #include "aops.h"
  26. #include "ntfs.h"
  27. /**
  28. * __ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value
  29. * @vi: vfs inode describing the bitmap
  30. * @start_bit: first bit to set
  31. * @count: number of bits to set
  32. * @value: value to set the bits to (i.e. 0 or 1)
  33. * @is_rollback: if 'true' this is a rollback operation
  34. *
  35. * Set @count bits starting at bit @start_bit in the bitmap described by the
  36. * vfs inode @vi to @value, where @value is either 0 or 1.
  37. *
  38. * @is_rollback should always be 'false', it is for internal use to rollback
  39. * errors. You probably want to use ntfs_bitmap_set_bits_in_run() instead.
  40. *
  41. * Return 0 on success and -errno on error.
  42. */
  43. int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
  44. const s64 count, const u8 value, const bool is_rollback)
  45. {
  46. s64 cnt = count;
  47. pgoff_t index, end_index;
  48. struct address_space *mapping;
  49. struct page *page;
  50. u8 *kaddr;
  51. int pos, len;
  52. u8 bit;
  53. BUG_ON(!vi);
  54. ntfs_debug("Entering for i_ino 0x%lx, start_bit 0x%llx, count 0x%llx, "
  55. "value %u.%s", vi->i_ino, (unsigned long long)start_bit,
  56. (unsigned long long)cnt, (unsigned int)value,
  57. is_rollback ? " (rollback)" : "");
  58. BUG_ON(start_bit < 0);
  59. BUG_ON(cnt < 0);
  60. BUG_ON(value > 1);
  61. /*
  62. * Calculate the indices for the pages containing the first and last
  63. * bits, i.e. @start_bit and @start_bit + @cnt - 1, respectively.
  64. */
  65. index = start_bit >> (3 + PAGE_SHIFT);
  66. end_index = (start_bit + cnt - 1) >> (3 + PAGE_SHIFT);
  67. /* Get the page containing the first bit (@start_bit). */
  68. mapping = vi->i_mapping;
  69. page = ntfs_map_page(mapping, index);
  70. if (IS_ERR(page)) {
  71. if (!is_rollback)
  72. ntfs_error(vi->i_sb, "Failed to map first page (error "
  73. "%li), aborting.", PTR_ERR(page));
  74. return PTR_ERR(page);
  75. }
  76. kaddr = page_address(page);
  77. /* Set @pos to the position of the byte containing @start_bit. */
  78. pos = (start_bit >> 3) & ~PAGE_MASK;
  79. /* Calculate the position of @start_bit in the first byte. */
  80. bit = start_bit & 7;
  81. /* If the first byte is partial, modify the appropriate bits in it. */
  82. if (bit) {
  83. u8 *byte = kaddr + pos;
  84. while ((bit & 7) && cnt) {
  85. cnt--;
  86. if (value)
  87. *byte |= 1 << bit++;
  88. else
  89. *byte &= ~(1 << bit++);
  90. }
  91. /* If we are done, unmap the page and return success. */
  92. if (!cnt)
  93. goto done;
  94. /* Update @pos to the new position. */
  95. pos++;
  96. }
  97. /*
  98. * Depending on @value, modify all remaining whole bytes in the page up
  99. * to @cnt.
  100. */
  101. len = min_t(s64, cnt >> 3, PAGE_SIZE - pos);
  102. memset(kaddr + pos, value ? 0xff : 0, len);
  103. cnt -= len << 3;
  104. /* Update @len to point to the first not-done byte in the page. */
  105. if (cnt < 8)
  106. len += pos;
  107. /* If we are not in the last page, deal with all subsequent pages. */
  108. while (index < end_index) {
  109. BUG_ON(cnt <= 0);
  110. /* Update @index and get the next page. */
  111. flush_dcache_page(page);
  112. set_page_dirty(page);
  113. ntfs_unmap_page(page);
  114. page = ntfs_map_page(mapping, ++index);
  115. if (IS_ERR(page))
  116. goto rollback;
  117. kaddr = page_address(page);
  118. /*
  119. * Depending on @value, modify all remaining whole bytes in the
  120. * page up to @cnt.
  121. */
  122. len = min_t(s64, cnt >> 3, PAGE_SIZE);
  123. memset(kaddr, value ? 0xff : 0, len);
  124. cnt -= len << 3;
  125. }
  126. /*
  127. * The currently mapped page is the last one. If the last byte is
  128. * partial, modify the appropriate bits in it. Note, @len is the
  129. * position of the last byte inside the page.
  130. */
  131. if (cnt) {
  132. u8 *byte;
  133. BUG_ON(cnt > 7);
  134. bit = cnt;
  135. byte = kaddr + len;
  136. while (bit--) {
  137. if (value)
  138. *byte |= 1 << bit;
  139. else
  140. *byte &= ~(1 << bit);
  141. }
  142. }
  143. done:
  144. /* We are done. Unmap the page and return success. */
  145. flush_dcache_page(page);
  146. set_page_dirty(page);
  147. ntfs_unmap_page(page);
  148. ntfs_debug("Done.");
  149. return 0;
  150. rollback:
  151. /*
  152. * Current state:
  153. * - no pages are mapped
  154. * - @count - @cnt is the number of bits that have been modified
  155. */
  156. if (is_rollback)
  157. return PTR_ERR(page);
  158. if (count != cnt)
  159. pos = __ntfs_bitmap_set_bits_in_run(vi, start_bit, count - cnt,
  160. value ? 0 : 1, true);
  161. else
  162. pos = 0;
  163. if (!pos) {
  164. /* Rollback was successful. */
  165. ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
  166. "%li), aborting.", PTR_ERR(page));
  167. } else {
  168. /* Rollback failed. */
  169. ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
  170. "%li) and rollback failed (error %i). "
  171. "Aborting and leaving inconsistent metadata. "
  172. "Unmount and run chkdsk.", PTR_ERR(page), pos);
  173. NVolSetErrors(NTFS_SB(vi->i_sb));
  174. }
  175. return PTR_ERR(page);
  176. }
  177. #endif /* NTFS_RW */