1 /* 2 * Copyright (C) 2004, OGAWA Hirofumi 3 * Released under GPL v2. 4 */ 5 6 #include <linux/module.h> 7 #include <linux/fs.h> 8 #include <linux/msdos_fs.h> 9 #include <linux/blkdev.h> 10 11 struct fatent_operations { 12 void (*ent_blocknr)(struct super_block *, int, int *, sector_t *); 13 void (*ent_set_ptr)(struct fat_entry *, int); 14 int (*ent_bread)(struct super_block *, struct fat_entry *, 15 int, sector_t); 16 int (*ent_get)(struct fat_entry *); 17 void (*ent_put)(struct fat_entry *, int); 18 int (*ent_next)(struct fat_entry *); 19 }; 20 21 static DEFINE_SPINLOCK(fat12_entry_lock); 22 23 static void fat12_ent_blocknr(struct super_block *sb, int entry, 24 int *offset, sector_t *blocknr) 25 { 26 struct msdos_sb_info *sbi = MSDOS_SB(sb); 27 int bytes = entry + (entry >> 1); 28 WARN_ON(entry < FAT_START_ENT || sbi->max_cluster <= entry); 29 *offset = bytes & (sb->s_blocksize - 1); 30 *blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits); 31 } 32 33 static void fat_ent_blocknr(struct super_block *sb, int entry, 34 int *offset, sector_t *blocknr) 35 { 36 struct msdos_sb_info *sbi = MSDOS_SB(sb); 37 int bytes = (entry << sbi->fatent_shift); 38 WARN_ON(entry < FAT_START_ENT || sbi->max_cluster <= entry); 39 *offset = bytes & (sb->s_blocksize - 1); 40 *blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits); 41 } 42 43 static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset) 44 { 45 struct buffer_head **bhs = fatent->bhs; 46 if (fatent->nr_bhs == 1) { 47 WARN_ON(offset >= (bhs[0]->b_size - 1)); 48 fatent->u.ent12_p[0] = bhs[0]->b_data + offset; 49 fatent->u.ent12_p[1] = bhs[0]->b_data + (offset + 1); 50 } else { 51 WARN_ON(offset != (bhs[0]->b_size - 1)); 52 fatent->u.ent12_p[0] = bhs[0]->b_data + offset; 53 fatent->u.ent12_p[1] = bhs[1]->b_data; 54 } 55 } 56 57 static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset) 58 { 59 WARN_ON(offset & (2 - 1)); 60 fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset); 61 } 62 63 static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset) 64 { 65 WARN_ON(offset & (4 - 1)); 66 fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset); 67 } 68 69 static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent, 70 int offset, sector_t blocknr) 71 { 72 struct buffer_head **bhs = fatent->bhs; 73 74 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start); 75 bhs[0] = sb_bread(sb, blocknr); 76 if (!bhs[0]) 77 goto err; 78 79 if ((offset + 1) < sb->s_blocksize) 80 fatent->nr_bhs = 1; 81 else { 82 /* This entry is block boundary, it needs the next block */ 83 blocknr++; 84 bhs[1] = sb_bread(sb, blocknr); 85 if (!bhs[1]) 86 goto err_brelse; 87 fatent->nr_bhs = 2; 88 } 89 fat12_ent_set_ptr(fatent, offset); 90 return 0; 91 92 err_brelse: 93 brelse(bhs[0]); 94 err: 95 printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", 96 (unsigned long long)blocknr); 97 return -EIO; 98 } 99 100 static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent, 101 int offset, sector_t blocknr) 102 { 103 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; 104 105 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start); 106 fatent->bhs[0] = sb_bread(sb, blocknr); 107 if (!fatent->bhs[0]) { 108 printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", 109 (unsigned long long)blocknr); 110 return -EIO; 111 } 112 fatent->nr_bhs = 1; 113 ops->ent_set_ptr(fatent, offset); 114 return 0; 115 } 116 117 static int fat12_ent_get(struct fat_entry *fatent) 118 { 119 u8 **ent12_p = fatent->u.ent12_p; 120 int next; 121 122 spin_lock(&fat12_entry_lock); 123 if (fatent->entry & 1) 124 next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4); 125 else 126 next = (*ent12_p[1] << 8) | *ent12_p[0]; 127 spin_unlock(&fat12_entry_lock); 128 129 next &= 0x0fff; 130 if (next >= BAD_FAT12) 131 next = FAT_ENT_EOF; 132 return next; 133 } 134 135 static int fat16_ent_get(struct fat_entry *fatent) 136 { 137 int next = le16_to_cpu(*fatent->u.ent16_p); 138 WARN_ON((unsigned long)fatent->u.ent16_p & (2 - 1)); 139 if (next >= BAD_FAT16) 140 next = FAT_ENT_EOF; 141 return next; 142 } 143 144 static int fat32_ent_get(struct fat_entry *fatent) 145 { 146 int next = le32_to_cpu(*fatent->u.ent32_p) & 0x0fffffff; 147 WARN_ON((unsigned long)fatent->u.ent32_p & (4 - 1)); 148 if (next >= BAD_FAT32) 149 next = FAT_ENT_EOF; 150 return next; 151 } 152 153 static void fat12_ent_put(struct fat_entry *fatent, int new) 154 { 155 u8 **ent12_p = fatent->u.ent12_p; 156 157 if (new == FAT_ENT_EOF) 158 new = EOF_FAT12; 159 160 spin_lock(&fat12_entry_lock); 161 if (fatent->entry & 1) { 162 *ent12_p[0] = (new << 4) | (*ent12_p[0] & 0x0f); 163 *ent12_p[1] = new >> 4; 164 } else { 165 *ent12_p[0] = new & 0xff; 166 *ent12_p[1] = (*ent12_p[1] & 0xf0) | (new >> 8); 167 } 168 spin_unlock(&fat12_entry_lock); 169 170 mark_buffer_dirty(fatent->bhs[0]); 171 if (fatent->nr_bhs == 2) 172 mark_buffer_dirty(fatent->bhs[1]); 173 } 174 175 static void fat16_ent_put(struct fat_entry *fatent, int new) 176 { 177 if (new == FAT_ENT_EOF) 178 new = EOF_FAT16; 179 180 *fatent->u.ent16_p = cpu_to_le16(new); 181 mark_buffer_dirty(fatent->bhs[0]); 182 } 183 184 static void fat32_ent_put(struct fat_entry *fatent, int new) 185 { 186 if (new == FAT_ENT_EOF) 187 new = EOF_FAT32; 188 189 WARN_ON(new & 0xf0000000); 190 new |= le32_to_cpu(*fatent->u.ent32_p) & ~0x0fffffff; 191 *fatent->u.ent32_p = cpu_to_le32(new); 192 mark_buffer_dirty(fatent->bhs[0]); 193 } 194 195 static int fat12_ent_next(struct fat_entry *fatent) 196 { 197 u8 **ent12_p = fatent->u.ent12_p; 198 struct buffer_head **bhs = fatent->bhs; 199 u8 *nextp = ent12_p[1] + 1 + (fatent->entry & 1); 200 201 fatent->entry++; 202 if (fatent->nr_bhs == 1) { 203 WARN_ON(ent12_p[0] > (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 2))); 204 WARN_ON(ent12_p[1] > (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))); 205 if (nextp < (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))) { 206 ent12_p[0] = nextp - 1; 207 ent12_p[1] = nextp; 208 return 1; 209 } 210 } else { 211 WARN_ON(ent12_p[0] != (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))); 212 WARN_ON(ent12_p[1] != (u8 *)bhs[1]->b_data); 213 ent12_p[0] = nextp - 1; 214 ent12_p[1] = nextp; 215 brelse(bhs[0]); 216 bhs[0] = bhs[1]; 217 fatent->nr_bhs = 1; 218 return 1; 219 } 220 ent12_p[0] = NULL; 221 ent12_p[1] = NULL; 222 return 0; 223 } 224 225 static int fat16_ent_next(struct fat_entry *fatent) 226 { 227 const struct buffer_head *bh = fatent->bhs[0]; 228 fatent->entry++; 229 if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) { 230 fatent->u.ent16_p++; 231 return 1; 232 } 233 fatent->u.ent16_p = NULL; 234 return 0; 235 } 236 237 static int fat32_ent_next(struct fat_entry *fatent) 238 { 239 const struct buffer_head *bh = fatent->bhs[0]; 240 fatent->entry++; 241 if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) { 242 fatent->u.ent32_p++; 243 return 1; 244 } 245 fatent->u.ent32_p = NULL; 246 return 0; 247 } 248 249 static struct fatent_operations fat12_ops = { 250 .ent_blocknr = fat12_ent_blocknr, 251 .ent_set_ptr = fat12_ent_set_ptr, 252 .ent_bread = fat12_ent_bread, 253 .ent_get = fat12_ent_get, 254 .ent_put = fat12_ent_put, 255 .ent_next = fat12_ent_next, 256 }; 257 258 static struct fatent_operations fat16_ops = { 259 .ent_blocknr = fat_ent_blocknr, 260 .ent_set_ptr = fat16_ent_set_ptr, 261 .ent_bread = fat_ent_bread, 262 .ent_get = fat16_ent_get, 263 .ent_put = fat16_ent_put, 264 .ent_next = fat16_ent_next, 265 }; 266 267 static struct fatent_operations fat32_ops = { 268 .ent_blocknr = fat_ent_blocknr, 269 .ent_set_ptr = fat32_ent_set_ptr, 270 .ent_bread = fat_ent_bread, 271 .ent_get = fat32_ent_get, 272 .ent_put = fat32_ent_put, 273 .ent_next = fat32_ent_next, 274 }; 275 276 static inline void lock_fat(struct msdos_sb_info *sbi) 277 { 278 mutex_lock(&sbi->fat_lock); 279 } 280 281 static inline void unlock_fat(struct msdos_sb_info *sbi) 282 { 283 mutex_unlock(&sbi->fat_lock); 284 } 285 286 void fat_ent_access_init(struct super_block *sb) 287 { 288 struct msdos_sb_info *sbi = MSDOS_SB(sb); 289 290 mutex_init(&sbi->fat_lock); 291 292 switch (sbi->fat_bits) { 293 case 32: 294 sbi->fatent_shift = 2; 295 sbi->fatent_ops = &fat32_ops; 296 break; 297 case 16: 298 sbi->fatent_shift = 1; 299 sbi->fatent_ops = &fat16_ops; 300 break; 301 case 12: 302 sbi->fatent_shift = -1; 303 sbi->fatent_ops = &fat12_ops; 304 break; 305 } 306 } 307 308 static inline int fat_ent_update_ptr(struct super_block *sb, 309 struct fat_entry *fatent, 310 int offset, sector_t blocknr) 311 { 312 struct msdos_sb_info *sbi = MSDOS_SB(sb); 313 struct fatent_operations *ops = sbi->fatent_ops; 314 struct buffer_head **bhs = fatent->bhs; 315 316 /* Is this fatent's blocks including this entry? */ 317 if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr) 318 return 0; 319 /* Does this entry need the next block? */ 320 if (sbi->fat_bits == 12 && (offset + 1) >= sb->s_blocksize) { 321 if (fatent->nr_bhs != 2 || bhs[1]->b_blocknr != (blocknr + 1)) 322 return 0; 323 } 324 ops->ent_set_ptr(fatent, offset); 325 return 1; 326 } 327 328 int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry) 329 { 330 struct super_block *sb = inode->i_sb; 331 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 332 struct fatent_operations *ops = sbi->fatent_ops; 333 int err, offset; 334 sector_t blocknr; 335 336 if (entry < FAT_START_ENT || sbi->max_cluster <= entry) { 337 fatent_brelse(fatent); 338 fat_fs_panic(sb, "invalid access to FAT (entry 0x%08x)", entry); 339 return -EIO; 340 } 341 342 fatent_set_entry(fatent, entry); 343 ops->ent_blocknr(sb, entry, &offset, &blocknr); 344 345 if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) { 346 fatent_brelse(fatent); 347 err = ops->ent_bread(sb, fatent, offset, blocknr); 348 if (err) 349 return err; 350 } 351 return ops->ent_get(fatent); 352 } 353 354 /* FIXME: We can write the blocks as more big chunk. */ 355 static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs, 356 int nr_bhs) 357 { 358 struct msdos_sb_info *sbi = MSDOS_SB(sb); 359 struct buffer_head *c_bh; 360 int err, n, copy; 361 362 err = 0; 363 for (copy = 1; copy < sbi->fats; copy++) { 364 sector_t backup_fat = sbi->fat_length * copy; 365 366 for (n = 0; n < nr_bhs; n++) { 367 c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr); 368 if (!c_bh) { 369 err = -ENOMEM; 370 goto error; 371 } 372 memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize); 373 set_buffer_uptodate(c_bh); 374 mark_buffer_dirty(c_bh); 375 if (sb->s_flags & MS_SYNCHRONOUS) 376 err = sync_dirty_buffer(c_bh); 377 brelse(c_bh); 378 if (err) 379 goto error; 380 } 381 } 382 error: 383 return err; 384 } 385 386 int fat_ent_write(struct inode *inode, struct fat_entry *fatent, 387 int new, int wait) 388 { 389 struct super_block *sb = inode->i_sb; 390 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; 391 int err; 392 393 ops->ent_put(fatent, new); 394 if (wait) { 395 err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs); 396 if (err) 397 return err; 398 } 399 return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs); 400 } 401 402 static inline int fat_ent_next(struct msdos_sb_info *sbi, 403 struct fat_entry *fatent) 404 { 405 if (sbi->fatent_ops->ent_next(fatent)) { 406 if (fatent->entry < sbi->max_cluster) 407 return 1; 408 } 409 return 0; 410 } 411 412 static inline int fat_ent_read_block(struct super_block *sb, 413 struct fat_entry *fatent) 414 { 415 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; 416 sector_t blocknr; 417 int offset; 418 419 fatent_brelse(fatent); 420 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr); 421 return ops->ent_bread(sb, fatent, offset, blocknr); 422 } 423 424 static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs, 425 struct fat_entry *fatent) 426 { 427 int n, i; 428 429 for (n = 0; n < fatent->nr_bhs; n++) { 430 for (i = 0; i < *nr_bhs; i++) { 431 if (fatent->bhs[n] == bhs[i]) 432 break; 433 } 434 if (i == *nr_bhs) { 435 get_bh(fatent->bhs[n]); 436 bhs[i] = fatent->bhs[n]; 437 (*nr_bhs)++; 438 } 439 } 440 } 441 442 int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster) 443 { 444 struct super_block *sb = inode->i_sb; 445 struct msdos_sb_info *sbi = MSDOS_SB(sb); 446 struct fatent_operations *ops = sbi->fatent_ops; 447 struct fat_entry fatent, prev_ent; 448 struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 449 int i, count, err, nr_bhs, idx_clus; 450 451 BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */ 452 453 lock_fat(sbi); 454 if (sbi->free_clusters != -1 && sbi->free_clus_valid && 455 sbi->free_clusters < nr_cluster) { 456 unlock_fat(sbi); 457 return -ENOSPC; 458 } 459 460 err = nr_bhs = idx_clus = 0; 461 count = FAT_START_ENT; 462 fatent_init(&prev_ent); 463 fatent_init(&fatent); 464 fatent_set_entry(&fatent, sbi->prev_free + 1); 465 while (count < sbi->max_cluster) { 466 if (fatent.entry >= sbi->max_cluster) 467 fatent.entry = FAT_START_ENT; 468 fatent_set_entry(&fatent, fatent.entry); 469 err = fat_ent_read_block(sb, &fatent); 470 if (err) 471 goto out; 472 473 /* Find the free entries in a block */ 474 do { 475 if (ops->ent_get(&fatent) == FAT_ENT_FREE) { 476 int entry = fatent.entry; 477 478 /* make the cluster chain */ 479 ops->ent_put(&fatent, FAT_ENT_EOF); 480 if (prev_ent.nr_bhs) 481 ops->ent_put(&prev_ent, entry); 482 483 fat_collect_bhs(bhs, &nr_bhs, &fatent); 484 485 sbi->prev_free = entry; 486 if (sbi->free_clusters != -1) 487 sbi->free_clusters--; 488 sb->s_dirt = 1; 489 490 cluster[idx_clus] = entry; 491 idx_clus++; 492 if (idx_clus == nr_cluster) 493 goto out; 494 495 /* 496 * fat_collect_bhs() gets ref-count of bhs, 497 * so we can still use the prev_ent. 498 */ 499 prev_ent = fatent; 500 } 501 count++; 502 if (count == sbi->max_cluster) 503 break; 504 } while (fat_ent_next(sbi, &fatent)); 505 } 506 507 /* Couldn't allocate the free entries */ 508 sbi->free_clusters = 0; 509 sbi->free_clus_valid = 1; 510 sb->s_dirt = 1; 511 err = -ENOSPC; 512 513 out: 514 unlock_fat(sbi); 515 fatent_brelse(&fatent); 516 if (!err) { 517 if (inode_needs_sync(inode)) 518 err = fat_sync_bhs(bhs, nr_bhs); 519 if (!err) 520 err = fat_mirror_bhs(sb, bhs, nr_bhs); 521 } 522 for (i = 0; i < nr_bhs; i++) 523 brelse(bhs[i]); 524 525 if (err && idx_clus) 526 fat_free_clusters(inode, cluster[0]); 527 528 return err; 529 } 530 531 int fat_free_clusters(struct inode *inode, int cluster) 532 { 533 struct super_block *sb = inode->i_sb; 534 struct msdos_sb_info *sbi = MSDOS_SB(sb); 535 struct fatent_operations *ops = sbi->fatent_ops; 536 struct fat_entry fatent; 537 struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 538 int i, err, nr_bhs; 539 int first_cl = cluster; 540 541 nr_bhs = 0; 542 fatent_init(&fatent); 543 lock_fat(sbi); 544 do { 545 cluster = fat_ent_read(inode, &fatent, cluster); 546 if (cluster < 0) { 547 err = cluster; 548 goto error; 549 } else if (cluster == FAT_ENT_FREE) { 550 fat_fs_panic(sb, "%s: deleting FAT entry beyond EOF", 551 __func__); 552 err = -EIO; 553 goto error; 554 } 555 556 /* 557 * Issue discard for the sectors we no longer care about, 558 * batching contiguous clusters into one request 559 */ 560 if (cluster != fatent.entry + 1) { 561 int nr_clus = fatent.entry - first_cl + 1; 562 563 sb_issue_discard(sb, fat_clus_to_blknr(sbi, first_cl), 564 nr_clus * sbi->sec_per_clus); 565 first_cl = cluster; 566 } 567 568 ops->ent_put(&fatent, FAT_ENT_FREE); 569 if (sbi->free_clusters != -1) { 570 sbi->free_clusters++; 571 sb->s_dirt = 1; 572 } 573 574 if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) { 575 if (sb->s_flags & MS_SYNCHRONOUS) { 576 err = fat_sync_bhs(bhs, nr_bhs); 577 if (err) 578 goto error; 579 } 580 err = fat_mirror_bhs(sb, bhs, nr_bhs); 581 if (err) 582 goto error; 583 for (i = 0; i < nr_bhs; i++) 584 brelse(bhs[i]); 585 nr_bhs = 0; 586 } 587 fat_collect_bhs(bhs, &nr_bhs, &fatent); 588 } while (cluster != FAT_ENT_EOF); 589 590 if (sb->s_flags & MS_SYNCHRONOUS) { 591 err = fat_sync_bhs(bhs, nr_bhs); 592 if (err) 593 goto error; 594 } 595 err = fat_mirror_bhs(sb, bhs, nr_bhs); 596 error: 597 fatent_brelse(&fatent); 598 for (i = 0; i < nr_bhs; i++) 599 brelse(bhs[i]); 600 unlock_fat(sbi); 601 602 return err; 603 } 604 605 EXPORT_SYMBOL_GPL(fat_free_clusters); 606 607 /* 128kb is the whole sectors for FAT12 and FAT16 */ 608 #define FAT_READA_SIZE (128 * 1024) 609 610 static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent, 611 unsigned long reada_blocks) 612 { 613 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; 614 sector_t blocknr; 615 int i, offset; 616 617 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr); 618 619 for (i = 0; i < reada_blocks; i++) 620 sb_breadahead(sb, blocknr + i); 621 } 622 623 int fat_count_free_clusters(struct super_block *sb) 624 { 625 struct msdos_sb_info *sbi = MSDOS_SB(sb); 626 struct fatent_operations *ops = sbi->fatent_ops; 627 struct fat_entry fatent; 628 unsigned long reada_blocks, reada_mask, cur_block; 629 int err = 0, free; 630 631 lock_fat(sbi); 632 if (sbi->free_clusters != -1 && sbi->free_clus_valid) 633 goto out; 634 635 reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits; 636 reada_mask = reada_blocks - 1; 637 cur_block = 0; 638 639 free = 0; 640 fatent_init(&fatent); 641 fatent_set_entry(&fatent, FAT_START_ENT); 642 while (fatent.entry < sbi->max_cluster) { 643 /* readahead of fat blocks */ 644 if ((cur_block & reada_mask) == 0) { 645 unsigned long rest = sbi->fat_length - cur_block; 646 fat_ent_reada(sb, &fatent, min(reada_blocks, rest)); 647 } 648 cur_block++; 649 650 err = fat_ent_read_block(sb, &fatent); 651 if (err) 652 goto out; 653 654 do { 655 if (ops->ent_get(&fatent) == FAT_ENT_FREE) 656 free++; 657 } while (fat_ent_next(sbi, &fatent)); 658 } 659 sbi->free_clusters = free; 660 sbi->free_clus_valid = 1; 661 sb->s_dirt = 1; 662 fatent_brelse(&fatent); 663 out: 664 unlock_fat(sbi); 665 return err; 666 } 667