Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | /* * linux/fs/hfs/bitmap.c * * Copyright (C) 1996-1997 Paul H. Hargrove * This file may be distributed under the terms of the GNU General Public License. * * Based on GPLed code Copyright (C) 1995 Michael Dreher * * This file contains the code to modify the volume bitmap: * search/set/clear bits. * * "XXX" in a comment is a note to myself to consider changing something. * * In function preconditions the term "valid" applied to a pointer to * a structure means that the pointer is non-NULL and the structure it * points to has all fields initialized to consistent values. */ #include "hfs.h" /*================ Global functions ================*/ /* * hfs_vbm_count_free() * * Description: * Count the number of consecutive cleared bits in the bitmap blocks of * the hfs MDB starting at bit number 'start'. 'mdb' had better * be locked or the indicated number of blocks may be no longer free, * when this functions returns! * Input Variable(s): * struct hfs_mdb *mdb: Pointer to the hfs MDB * hfs_u16 start: bit number to start at * Output Variable(s): * NONE * Returns: * The number of consecutive cleared bits starting at bit 'start' * Preconditions: * 'mdb' points to a "valid" (struct hfs_mdb). * Postconditions: * NONE */ hfs_u16 hfs_vbm_count_free(const struct hfs_mdb *mdb, hfs_u16 start) { hfs_u16 block_nr; /* index of the current bitmap block */ hfs_u16 bit_nr; /* index of the current bit in block */ hfs_u16 count; /* number of bits found so far */ hfs_u16 len; /* number of bits found in this block */ hfs_u16 max_block; /* index of last bitmap block */ hfs_u16 max_bits; /* index of last bit in block */ /* is this a valid HFS MDB? */ if (!mdb) { return 0; } block_nr = start / HFS_BM_BPB; bit_nr = start % HFS_BM_BPB; max_block = (mdb->fs_ablocks + HFS_BM_BPB - 1) / HFS_BM_BPB - 1; count = 0; while (block_nr <= max_block) { if (block_nr != max_block) { max_bits = HFS_BM_BPB; } else { max_bits = mdb->fs_ablocks % HFS_BM_BPB; } len=hfs_count_zero_bits(hfs_buffer_data(mdb->bitmap[block_nr]), max_bits, bit_nr); count += len; /* see if we fell short of the end of this block */ if ((len + bit_nr) < max_bits) { break; } ++block_nr; bit_nr = 0; } return count; } /* * hfs_vbm_search_free() * * Description: * Search for 'num_bits' consecutive cleared bits in the bitmap blocks of * the hfs MDB. 'mdb' had better be locked or the returned range * may be no longer free, when this functions returns! * XXX Currently the search starts from bit 0, but it should start with * the bit number stored in 's_alloc_ptr' of the MDB. * Input Variable(s): * struct hfs_mdb *mdb: Pointer to the hfs MDB * hfs_u16 *num_bits: Pointer to the number of cleared bits * to search for * Output Variable(s): * hfs_u16 *num_bits: The number of consecutive clear bits of the * returned range. If the bitmap is fragmented, this will be less than * requested and it will be zero, when the disk is full. * Returns: * The number of the first bit of the range of cleared bits which has been * found. When 'num_bits' is zero, this is invalid! * Preconditions: * 'mdb' points to a "valid" (struct hfs_mdb). * 'num_bits' points to a variable of type (hfs_u16), which contains * the number of cleared bits to find. * Postconditions: * 'num_bits' is set to the length of the found sequence. */ hfs_u16 hfs_vbm_search_free(const struct hfs_mdb *mdb, hfs_u16 *num_bits) { hfs_u16 block_nr; /* index of the current bitmap block */ /* position and length of current portion of a run */ hfs_u16 cur_pos, cur_len; /* position and length of current complete run */ hfs_u16 pos=0, len=0; /* position and length of longest complete run */ hfs_u16 longest_pos=0, longest_len=0; void *bitmap; /* contents of the current bitmap block */ hfs_u16 max_block; /* upper limit of outer loop */ hfs_u16 max_bits; /* upper limit of inner loop */ /* is this a valid HFS MDB? */ if (!mdb) { *num_bits = 0; hfs_warn("hfs_vbm_search_free: not a valid MDB\n"); return 0; } /* make sure we have actual work to perform */ if (!(*num_bits)) { return 0; } max_block = (mdb->fs_ablocks+HFS_BM_BPB-1) / HFS_BM_BPB - 1; /* search all bitmap blocks */ for (block_nr = 0; block_nr <= max_block; block_nr++) { bitmap = hfs_buffer_data(mdb->bitmap[block_nr]); if (block_nr != max_block) { max_bits = HFS_BM_BPB; } else { max_bits = mdb->fs_ablocks % HFS_BM_BPB; } cur_pos = 0; do { cur_len = hfs_count_zero_bits(bitmap, max_bits, cur_pos); len += cur_len; if (len > longest_len) { longest_pos = pos; longest_len = len; if (len >= *num_bits) { goto search_end; } } if ((cur_pos + cur_len) == max_bits) { break; /* zeros may continue into next block */ } /* find start of next run of zeros */ cur_pos = hfs_find_zero_bit(bitmap, max_bits, cur_pos + cur_len); pos = cur_pos + HFS_BM_BPB*block_nr; len = 0; } while (cur_pos < max_bits); } search_end: *num_bits = longest_len; return longest_pos; } /* * hfs_set_vbm_bits() * * Description: * Set the requested bits in the volume bitmap of the hfs filesystem * Input Variable(s): * struct hfs_mdb *mdb: Pointer to the hfs MDB * hfs_u16 start: The offset of the first bit * hfs_u16 count: The number of bits * Output Variable(s): * None * Returns: * 0: no error * -1: One of the bits was already set. This is a strange * error and when it happens, the filesystem must be repaired! * -2: One or more of the bits are out of range of the bitmap. * -3: The 's_magic' field of the MDB does not match * Preconditions: * 'mdb' points to a "valid" (struct hfs_mdb). * Postconditions: * Starting with bit number 'start', 'count' bits in the volume bitmap * are set. The affected bitmap blocks are marked "dirty", the free * block count of the MDB is updated and the MDB is marked dirty. */ int hfs_set_vbm_bits(struct hfs_mdb *mdb, hfs_u16 start, hfs_u16 count) { hfs_u16 block_nr; /* index of the current bitmap block */ hfs_u16 u32_nr; /* index of the current hfs_u32 in block */ hfs_u16 bit_nr; /* index of the current bit in hfs_u32 */ hfs_u16 left = count; /* number of bits left to be set */ hfs_u32 *bitmap; /* the current bitmap block's contents */ /* is this a valid HFS MDB? */ if (!mdb) { return -3; } /* is there any actual work to be done? */ if (!count) { return 0; } /* are all of the bits in range? */ if ((start + count) > mdb->fs_ablocks) { return -2; } block_nr = start / HFS_BM_BPB; u32_nr = (start % HFS_BM_BPB) / 32; bit_nr = start % 32; /* bitmap is always on a 32-bit boundary */ bitmap = (hfs_u32 *)hfs_buffer_data(mdb->bitmap[block_nr]); /* do any partial hfs_u32 at the start */ if (bit_nr != 0) { while ((bit_nr < 32) && left) { if (hfs_set_bit(bit_nr, bitmap + u32_nr)) { hfs_buffer_dirty(mdb->bitmap[block_nr]); return -1; } ++bit_nr; --left; } bit_nr=0; /* advance u32_nr and check for end of this block */ if (++u32_nr > 127) { u32_nr = 0; hfs_buffer_dirty(mdb->bitmap[block_nr]); ++block_nr; /* bitmap is always on a 32-bit boundary */ bitmap = (hfs_u32 *) hfs_buffer_data(mdb->bitmap[block_nr]); } } /* do full hfs_u32s */ while (left > 31) { if (bitmap[u32_nr] != ((hfs_u32)0)) { hfs_buffer_dirty(mdb->bitmap[block_nr]); return -1; } bitmap[u32_nr] = ~((hfs_u32)0); left -= 32; /* advance u32_nr and check for end of this block */ if (++u32_nr > 127) { u32_nr = 0; hfs_buffer_dirty(mdb->bitmap[block_nr]); ++block_nr; /* bitmap is always on a 32-bit boundary */ bitmap = (hfs_u32 *) hfs_buffer_data(mdb->bitmap[block_nr]); } } /* do any partial hfs_u32 at end */ while (left) { if (hfs_set_bit(bit_nr, bitmap + u32_nr)) { hfs_buffer_dirty(mdb->bitmap[block_nr]); return -1; } ++bit_nr; --left; } hfs_buffer_dirty(mdb->bitmap[block_nr]); mdb->free_ablocks -= count; /* successful completion */ hfs_mdb_dirty(mdb->sys_mdb); return 0; } /* * hfs_clear_vbm_bits() * * Description: * Clear the requested bits in the volume bitmap of the hfs filesystem * Input Variable(s): * struct hfs_mdb *mdb: Pointer to the hfs MDB * hfs_u16 start: The offset of the first bit * hfs_u16 count: The number of bits * Output Variable(s): * None * Returns: * 0: no error * -1: One of the bits was already clear. This is a strange * error and when it happens, the filesystem must be repaired! * -2: One or more of the bits are out of range of the bitmap. * -3: The 's_magic' field of the MDB does not match * Preconditions: * 'mdb' points to a "valid" (struct hfs_mdb). * Postconditions: * Starting with bit number 'start', 'count' bits in the volume bitmap * are cleared. The affected bitmap blocks are marked "dirty", the free * block count of the MDB is updated and the MDB is marked dirty. */ int hfs_clear_vbm_bits(struct hfs_mdb *mdb, hfs_u16 start, hfs_u16 count) { hfs_u16 block_nr; /* index of the current bitmap block */ hfs_u16 u32_nr; /* index of the current hfs_u32 in block */ hfs_u16 bit_nr; /* index of the current bit in hfs_u32 */ hfs_u16 left = count; /* number of bits left to be set */ hfs_u32 *bitmap; /* the current bitmap block's contents */ /* is this a valid HFS MDB? */ if (!mdb) { return -3; } /* is there any actual work to be done? */ if (!count) { return 0; } /* are all of the bits in range? */ if ((start + count) > mdb->fs_ablocks) { return -2; } block_nr = start / HFS_BM_BPB; u32_nr = (start % HFS_BM_BPB) / 32; bit_nr = start % 32; /* bitmap is always on a 32-bit boundary */ bitmap = (hfs_u32 *)hfs_buffer_data(mdb->bitmap[block_nr]); /* do any partial hfs_u32 at the start */ if (bit_nr != 0) { while ((bit_nr < 32) && left) { if (!hfs_clear_bit(bit_nr, bitmap + u32_nr)) { hfs_buffer_dirty(mdb->bitmap[block_nr]); return -1; } ++bit_nr; --left; } bit_nr=0; /* advance u32_nr and check for end of this block */ if (++u32_nr > 127) { u32_nr = 0; hfs_buffer_dirty(mdb->bitmap[block_nr]); ++block_nr; /* bitmap is always on a 32-bit boundary */ bitmap = (hfs_u32 *) hfs_buffer_data(mdb->bitmap[block_nr]); } } /* do full hfs_u32s */ while (left > 31) { if (bitmap[u32_nr] != ~((hfs_u32)0)) { hfs_buffer_dirty(mdb->bitmap[block_nr]); return -1; } bitmap[u32_nr] = ((hfs_u32)0); left -= 32; /* advance u32_nr and check for end of this block */ if (++u32_nr > 127) { u32_nr = 0; hfs_buffer_dirty(mdb->bitmap[block_nr]); ++block_nr; /* bitmap is always on a 32-bit boundary */ bitmap = (hfs_u32 *) hfs_buffer_data(mdb->bitmap[block_nr]); } } /* do any partial hfs_u32 at end */ while (left) { if (!hfs_clear_bit(bit_nr, bitmap + u32_nr)) { hfs_buffer_dirty(mdb->bitmap[block_nr]); return -1; } ++bit_nr; --left; } hfs_buffer_dirty(mdb->bitmap[block_nr]); mdb->free_ablocks += count; /* successful completion */ hfs_mdb_dirty(mdb->sys_mdb); return 0; } |