1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * bitmap.c - NTFS kernel bitmap handling. Part of the Linux-NTFS project.
5 * Copyright (c) 2004-2005 Anton Altaparmakov
10 #include <linux/pagemap.h>
18 * __ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value
19 * @vi: vfs inode describing the bitmap
20 * @start_bit: first bit to set
21 * @count: number of bits to set
22 * @value: value to set the bits to (i.e. 0 or 1)
23 * @is_rollback: if 'true' this is a rollback operation
25 * Set @count bits starting at bit @start_bit in the bitmap described by the
26 * vfs inode @vi to @value, where @value is either 0 or 1.
28 * @is_rollback should always be 'false', it is for internal use to rollback
29 * errors. You probably want to use ntfs_bitmap_set_bits_in_run() instead.
31 * Return 0 on success and -errno on error.
33 int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit,
34 const s64 count, const u8 value, const bool is_rollback)
37 pgoff_t index, end_index;
38 struct address_space *mapping;
45 ntfs_debug("Entering for i_ino 0x%lx, start_bit 0x%llx, count 0x%llx, "
46 "value %u.%s", vi->i_ino, (unsigned long long)start_bit,
47 (unsigned long long)cnt, (unsigned int)value,
48 is_rollback ? " (rollback)" : "");
49 BUG_ON(start_bit < 0);
53 * Calculate the indices for the pages containing the first and last
54 * bits, i.e. @start_bit and @start_bit + @cnt - 1, respectively.
56 index = start_bit >> (3 + PAGE_SHIFT);
57 end_index = (start_bit + cnt - 1) >> (3 + PAGE_SHIFT);
59 /* Get the page containing the first bit (@start_bit). */
60 mapping = vi->i_mapping;
61 page = ntfs_map_page(mapping, index);
64 ntfs_error(vi->i_sb, "Failed to map first page (error "
65 "%li), aborting.", PTR_ERR(page));
68 kaddr = page_address(page);
70 /* Set @pos to the position of the byte containing @start_bit. */
71 pos = (start_bit >> 3) & ~PAGE_MASK;
73 /* Calculate the position of @start_bit in the first byte. */
76 /* If the first byte is partial, modify the appropriate bits in it. */
78 u8 *byte = kaddr + pos;
79 while ((bit & 7) && cnt) {
84 *byte &= ~(1 << bit++);
86 /* If we are done, unmap the page and return success. */
90 /* Update @pos to the new position. */
94 * Depending on @value, modify all remaining whole bytes in the page up
97 len = min_t(s64, cnt >> 3, PAGE_SIZE - pos);
98 memset(kaddr + pos, value ? 0xff : 0, len);
101 /* Update @len to point to the first not-done byte in the page. */
105 /* If we are not in the last page, deal with all subsequent pages. */
106 while (index < end_index) {
109 /* Update @index and get the next page. */
110 flush_dcache_page(page);
111 set_page_dirty(page);
112 ntfs_unmap_page(page);
113 page = ntfs_map_page(mapping, ++index);
116 kaddr = page_address(page);
118 * Depending on @value, modify all remaining whole bytes in the
121 len = min_t(s64, cnt >> 3, PAGE_SIZE);
122 memset(kaddr, value ? 0xff : 0, len);
126 * The currently mapped page is the last one. If the last byte is
127 * partial, modify the appropriate bits in it. Note, @len is the
128 * position of the last byte inside the page.
141 *byte &= ~(1 << bit);
145 /* We are done. Unmap the page and return success. */
146 flush_dcache_page(page);
147 set_page_dirty(page);
148 ntfs_unmap_page(page);
154 * - no pages are mapped
155 * - @count - @cnt is the number of bits that have been modified
158 return PTR_ERR(page);
160 pos = __ntfs_bitmap_set_bits_in_run(vi, start_bit, count - cnt,
161 value ? 0 : 1, true);
165 /* Rollback was successful. */
166 ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
167 "%li), aborting.", PTR_ERR(page));
169 /* Rollback failed. */
170 ntfs_error(vi->i_sb, "Failed to map subsequent page (error "
171 "%li) and rollback failed (error %i). "
172 "Aborting and leaving inconsistent metadata. "
173 "Unmount and run chkdsk.", PTR_ERR(page), pos);
174 NVolSetErrors(NTFS_SB(vi->i_sb));
176 return PTR_ERR(page);