2 * bitmap.c - Bitmap handling code. Originated from the Linux-NTFS project.
4 * Copyright (c) 2002-2006 Anton Altaparmakov
5 * Copyright (c) 2004-2005 Richard Russon
6 * Copyright (c) 2004-2008 Szabolcs Szakacsits
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the NTFS-3G
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 * ntfs_bit_set - set a bit in a field of bits
50 * @bitmap: field of bits
52 * @new_value: value to set bit to (0 or 1)
54 * Set the bit @bit in the @bitmap to @new_value. Ignore all errors.
56 void ntfs_bit_set(u8
*bitmap
, const u64 bit
, const u8 new_value
)
58 if (!bitmap
|| new_value
> 1)
61 bitmap
[bit
>> 3] &= ~(1 << (bit
& 7));
63 bitmap
[bit
>> 3] |= (1 << (bit
& 7));
67 * ntfs_bit_get - get value of a bit in a field of bits
68 * @bitmap: field of bits
71 * Get and return the value of the bit @bit in @bitmap (0 or 1).
74 char ntfs_bit_get(const u8
*bitmap
, const u64 bit
)
78 return (bitmap
[bit
>> 3] >> (bit
& 7)) & 1;
82 * ntfs_bit_get_and_set - get value of a bit in a field of bits and set it
83 * @bitmap: field of bits
84 * @bit: bit to get/set
85 * @new_value: value to set bit to (0 or 1)
87 * Return the value of the bit @bit and set it to @new_value (0 or 1).
90 char ntfs_bit_get_and_set(u8
*bitmap
, const u64 bit
, const u8 new_value
)
92 register u8 old_bit
, shift
;
94 if (!bitmap
|| new_value
> 1)
97 old_bit
= (bitmap
[bit
>> 3] >> shift
) & 1;
98 if (new_value
!= old_bit
)
99 bitmap
[bit
>> 3] ^= 1 << shift
;
104 * ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value
105 * @na: attribute containing the bitmap
106 * @start_bit: first bit to set
107 * @count: number of bits to set
108 * @value: value to set the bits to (i.e. 0 or 1)
110 * Set @count bits starting at bit @start_bit in the bitmap described by the
111 * attribute @na to @value, where @value is either 0 or 1.
113 * On success return 0 and on error return -1 with errno set to the error code.
115 static int ntfs_bitmap_set_bits_in_run(ntfs_attr
*na
, s64 start_bit
,
116 s64 count
, int value
)
119 u8
*buf
, *lastbyte_buf
;
120 int bit
, firstbyte
, lastbyte
, lastbyte_pos
, tmp
, ret
= -1;
122 if (!na
|| start_bit
< 0 || count
< 0) {
124 ntfs_log_perror("%s: Invalid argument (%p, %lld, %lld)",
125 __FUNCTION__
, na
, (long long)start_bit
, (long long)count
);
135 /* Calculate the required buffer size in bytes, capping it at 8kiB. */
136 bufsize
= ((count
- (bit
? 8 - bit
: 0) + 7) >> 3) + firstbyte
;
140 buf
= ntfs_malloc(bufsize
);
144 /* Depending on @value, zero or set all bits in the allocated buffer. */
145 memset(buf
, value
? 0xff : 0, bufsize
);
147 /* If there is a first partial byte... */
150 br
= ntfs_attr_pread(na
, start_bit
>> 3, 1, buf
);
156 /* and set or clear the appropriate bits in it. */
157 while ((bit
& 7) && count
--) {
161 *buf
&= ~(1 << bit
++);
163 /* Update @start_bit to the new position. */
164 start_bit
= (start_bit
+ 7) & ~7;
167 /* Loop until @count reaches zero. */
172 /* If there is a last partial byte... */
173 if (count
> 0 && bit
) {
174 lastbyte_pos
= ((count
+ 7) >> 3) + firstbyte
;
177 ntfs_log_error("Lastbyte is zero. Leaving "
178 "inconsistent metadata.\n");
182 /* and it is in the currently loaded bitmap window... */
183 if (lastbyte_pos
<= bufsize
) {
184 lastbyte_buf
= buf
+ lastbyte_pos
- 1;
186 /* read the byte in... */
187 br
= ntfs_attr_pread(na
, (start_bit
+ count
) >>
190 // FIXME: Eeek! We need rollback! (AIA)
193 ntfs_log_perror("Reading of last byte "
194 "failed (%lld). Leaving inconsistent "
195 "metadata", (long long)br
);
198 /* and set/clear the appropriate bits in it. */
199 while (bit
&& count
--) {
201 *lastbyte_buf
|= 1 << --bit
;
203 *lastbyte_buf
&= ~(1 << --bit
);
205 /* We don't want to come back here... */
207 /* We have a last byte that we have handled. */
212 /* Write the prepared buffer to disk. */
213 tmp
= (start_bit
>> 3) - firstbyte
;
214 br
= ntfs_attr_pwrite(na
, tmp
, bufsize
, buf
);
216 // FIXME: Eeek! We need rollback! (AIA)
219 ntfs_log_perror("Failed to write buffer to bitmap "
220 "(%lld != %lld). Leaving inconsistent metadata",
221 (long long)br
, (long long)bufsize
);
225 /* Update counters. */
226 tmp
= (bufsize
- firstbyte
- lastbyte
) << 3;
230 * Re-set the partial first byte so a subsequent write
231 * of the buffer does not have stale, incorrect bits.
233 *buf
= value
? 0xff : 0;
237 if (bufsize
> (tmp
= (count
+ 7) >> 3))
240 if (lastbyte
&& count
!= 0) {
242 ntfs_log_error("Last buffer but count is not zero "
243 "(%lld). Leaving inconsistent metadata.\n",
258 * ntfs_bitmap_set_run - set a run of bits in a bitmap
259 * @na: attribute containing the bitmap
260 * @start_bit: first bit to set
261 * @count: number of bits to set
263 * Set @count bits starting at bit @start_bit in the bitmap described by the
266 * On success return 0 and on error return -1 with errno set to the error code.
268 int ntfs_bitmap_set_run(ntfs_attr
*na
, s64 start_bit
, s64 count
)
272 ntfs_log_enter("Set from bit %lld, count %lld\n",
273 (long long)start_bit
, (long long)count
);
274 ret
= ntfs_bitmap_set_bits_in_run(na
, start_bit
, count
, 1);
275 ntfs_log_leave("\n");
280 * ntfs_bitmap_clear_run - clear a run of bits in a bitmap
281 * @na: attribute containing the bitmap
282 * @start_bit: first bit to clear
283 * @count: number of bits to clear
285 * Clear @count bits starting at bit @start_bit in the bitmap described by the
288 * On success return 0 and on error return -1 with errno set to the error code.
290 int ntfs_bitmap_clear_run(ntfs_attr
*na
, s64 start_bit
, s64 count
)
294 ntfs_log_enter("Clear from bit %lld, count %lld\n",
295 (long long)start_bit
, (long long)count
);
296 ret
= ntfs_bitmap_set_bits_in_run(na
, start_bit
, count
, 0);
297 ntfs_log_leave("\n");