2 * bitmap.c - Bitmap handling code. Part of the Linux-NTFS project.
4 * Copyright (c) 2002-2003 Anton Altaparmakov
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.
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.
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
33 * ntfs_bitmap_set_bits_in__run - set a run of bits in a bitmap to a value
34 * @na: attribute containing the bitmap
35 * @start_bit: first bit to set
36 * @count: number of bits to set
37 * @value: value to set the bits to (i.e. 0 or 1)
39 * Set @count bits starting at bit @start_bit in the bitmap described by the
40 * attribute @na to @value, where @value is either 0 or 1.
42 * On success return 0 and on error return -1 with errno set to the error code.
44 static int ntfs_bitmap_set_bits_in_run(ntfs_attr *na, s64 start_bit, s64 count,
48 u8 *buf, *lastbyte_buf;
49 int bit, firstbyte, lastbyte, lastbyte_pos, tmp, err;
51 if (!na || start_bit < 0 || count < 0) {
62 /* Calculate the required buffer size in bytes, capping it at 8kiB. */
63 bufsize = ((count - (bit ? 8 - bit : 0) + 7) >> 3) + firstbyte;
67 /* Allocate memory. */
68 buf = (u8*)malloc(bufsize);
71 /* Depending on @value, zero or set all bits in the allocated buffer. */
72 memset(buf, value ? 0xff : 0, bufsize);
74 /* If there is a first partial byte... */
77 br = ntfs_attr_pread(na, start_bit >> 3, 1, buf);
83 /* and set or clear the appropriate bits in it. */
84 while ((bit & 7) && count--) {
88 *buf &= ~(1 << bit++);
90 /* Update @start_bit to the new position. */
91 start_bit = (start_bit + 7) & ~7;
94 /* Loop until @count reaches zero. */
99 /* If there is a last partial byte... */
100 if (count > 0 && bit) {
101 lastbyte_pos = ((count + 7) >> 3) + firstbyte;
104 fprintf(stderr, "%s(): Eeek! lastbyte is zero. "
105 "Leaving inconsistent "
106 "metadata.\n", __FUNCTION__);
110 /* and it is in the currently loaded bitmap window... */
111 if (lastbyte_pos <= bufsize) {
112 lastbyte_buf = buf + lastbyte_pos - 1;
114 /* read the byte in... */
115 br = ntfs_attr_pread(na, (start_bit + count) >>
118 // FIXME: Eeek! We need rollback! (AIA)
119 fprintf(stderr, "%s(): Eeek! Read of "
121 "Leaving inconsistent "
127 /* and set/clear the appropriate bits in it. */
128 while (bit && count--) {
130 *lastbyte_buf |= 1 << --bit;
132 *lastbyte_buf &= ~(1 << --bit);
134 /* We don't want to come back here... */
136 /* We have a last byte that we have handled. */
141 /* Write the prepared buffer to disk. */
142 tmp = (start_bit >> 3) - firstbyte;
143 br = ntfs_attr_pwrite(na, tmp, bufsize, buf);
145 // FIXME: Eeek! We need rollback! (AIA)
146 fprintf(stderr, "%s(): Eeek! Failed to write buffer "
147 "to bitmap. Leaving inconsistent "
148 "metadata.\n", __FUNCTION__);
153 /* Update counters. */
154 tmp = (bufsize - firstbyte - lastbyte) << 3;
158 if (bufsize > (tmp = (count + 7) >> 3))
161 if (lastbyte && count != 0) {
163 fprintf(stderr, "%s(): Eeek! Last buffer but count is "
164 "not zero (= %Li). Leaving "
165 "inconsistent metadata.\n",
166 __FUNCTION__, count);
183 * ntfs_bitmap_set_run - set a run of bits in a bitmap
184 * @na: attribute containing the bitmap
185 * @start_bit: first bit to set
186 * @count: number of bits to set
188 * Set @count bits starting at bit @start_bit in the bitmap described by the
191 * On success return 0 and on error return -1 with errno set to the error code.
193 int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count)
195 return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 1);
199 * ntfs_bitmap_clear_run - clear a run of bits in a bitmap
200 * @na: attribute containing the bitmap
201 * @start_bit: first bit to clear
202 * @count: number of bits to clear
204 * Clear @count bits starting at bit @start_bit in the bitmap described by the
207 * On success return 0 and on error return -1 with errno set to the error code.
209 int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count)
211 return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 0);