http://linux-ntfs.sourceforge.net/snapshots/ntfsprogs-200307311516.tar.bz2
[ntfsprogs.git] / libntfs / bitmap.c
1 /*
2  * bitmap.c - Bitmap handling code. Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2002-2003 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
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "types.h"
28 #include "attrib.h"
29 #include "bitmap.h"
30
31
32 /**
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)
38  *
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.
41  *
42  * On success return 0 and on error return -1 with errno set to the error code.
43  */
44 static int ntfs_bitmap_set_bits_in_run(ntfs_attr *na, s64 start_bit, s64 count,
45                 int value)
46 {
47         s64 bufsize, br;
48         u8 *buf, *lastbyte_buf;
49         int bit, firstbyte, lastbyte, lastbyte_pos, tmp, err;
50
51         if (!na || start_bit < 0 || count < 0) {
52                 errno = EINVAL;
53                 return -1;
54         }
55
56         bit = start_bit & 7;
57         if (bit)
58                 firstbyte = 1;
59         else
60                 firstbyte = 0;
61
62         /* Calculate the required buffer size in bytes, capping it at 8kiB. */
63         bufsize = ((count - (bit ? 8 - bit : 0) + 7) >> 3) + firstbyte;
64         if (bufsize > 8192)
65                 bufsize = 8192;
66
67         /* Allocate memory. */
68         buf = (u8*)malloc(bufsize);
69         if (!buf)
70                 return -1;
71         /* Depending on @value, zero or set all bits in the allocated buffer. */
72         memset(buf, value ? 0xff : 0, bufsize);
73
74         /* If there is a first partial byte... */
75         if (bit) {
76                 /* read it in... */
77                 br = ntfs_attr_pread(na, start_bit >> 3, 1, buf);
78                 if (br != 1) {
79                         free(buf);
80                         errno = EIO;
81                         return -1;
82                 }
83                 /* and set or clear the appropriate bits in it. */
84                 while ((bit & 7) && count--) {
85                         if (value)
86                                 *buf |= 1 << bit++;
87                         else
88                                 *buf &= ~(1 << bit++);
89                 }
90                 /* Update @start_bit to the new position. */
91                 start_bit = (start_bit + 7) & ~7;
92         }
93
94         /* Loop until @count reaches zero. */
95         lastbyte = 0;
96         lastbyte_buf = NULL;
97         bit = count & 7;
98         do {
99                 /* If there is a last partial byte... */
100                 if (count > 0 && bit) {
101                         lastbyte_pos = ((count + 7) >> 3) + firstbyte;
102                         if (!lastbyte_pos) {
103                                 // FIXME: Eeek! BUG!
104                                 fprintf(stderr, "%s(): Eeek! lastbyte is zero. "
105                                                 "Leaving inconsistent "
106                                                 "metadata.\n", __FUNCTION__);
107                                 err = EIO;
108                                 goto free_err_out;
109                         }
110                         /* and it is in the currently loaded bitmap window... */
111                         if (lastbyte_pos <= bufsize) {
112                                 lastbyte_buf = buf + lastbyte_pos - 1;
113
114                                 /* read the byte in... */
115                                 br = ntfs_attr_pread(na, (start_bit + count) >>
116                                                 3, 1, lastbyte_buf);
117                                 if (br != 1) {
118                                         // FIXME: Eeek! We need rollback! (AIA)
119                                         fprintf(stderr, "%s(): Eeek! Read of "
120                                                         "last byte failed. "
121                                                         "Leaving inconsistent "
122                                                         "metadata.\n",
123                                                         __FUNCTION__);
124                                         err = EIO;
125                                         goto free_err_out;
126                                 }
127                                 /* and set/clear the appropriate bits in it. */
128                                 while (bit && count--) {
129                                         if (value)
130                                                 *lastbyte_buf |= 1 << --bit;
131                                         else
132                                                 *lastbyte_buf &= ~(1 << --bit);
133                                 }
134                                 /* We don't want to come back here... */
135                                 bit = 0;
136                                 /* We have a last byte that we have handled. */
137                                 lastbyte = 1;
138                         }
139                 }
140
141                 /* Write the prepared buffer to disk. */
142                 tmp = (start_bit >> 3) - firstbyte;
143                 br = ntfs_attr_pwrite(na, tmp, bufsize, buf);
144                 if (br != bufsize) {
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__);
149                         err = EIO;
150                         goto free_err_out;
151                 }
152
153                 /* Update counters. */
154                 tmp = (bufsize - firstbyte - lastbyte) << 3;
155                 firstbyte = 0;
156                 start_bit += tmp;
157                 count -= tmp;
158                 if (bufsize > (tmp = (count + 7) >> 3))
159                         bufsize = tmp;
160
161                 if (lastbyte && count != 0) {
162                         // FIXME: Eeek! BUG!
163                         fprintf(stderr, "%s(): Eeek! Last buffer but count is "
164                                         "not zero (= %Li). Leaving "
165                                         "inconsistent metadata.\n",
166                                         __FUNCTION__, count);
167                         err = EIO;
168                         goto free_err_out;
169                 }
170         } while (count > 0);
171
172         /* Done! */
173         free(buf);
174         return 0;
175
176 free_err_out:
177         free(buf);
178         errno = err;
179         return -1;
180 }
181
182 /**
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
187  *
188  * Set @count bits starting at bit @start_bit in the bitmap described by the
189  * attribute @na.
190  *
191  * On success return 0 and on error return -1 with errno set to the error code.
192  */
193 int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count)
194 {
195         return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 1);
196 }
197
198 /**
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
203  *
204  * Clear @count bits starting at bit @start_bit in the bitmap described by the
205  * attribute @na.
206  *
207  * On success return 0 and on error return -1 with errno set to the error code.
208  */
209 int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count)
210 {
211         return ntfs_bitmap_set_bits_in_run(na, start_bit, count, 0);
212 }
213