2 * Copyright (C) 2004, OGAWA Hirofumi
3 * Released under GPL v2.
6 #include <linux/module.h>
8 #include <linux/msdos_fs.h>
9 #include <linux/blkdev.h>
12 struct fatent_operations {
13 void (*ent_blocknr)(struct super_block *, int, int *, sector_t *);
14 void (*ent_set_ptr)(struct fat_entry *, int);
15 int (*ent_bread)(struct super_block *, struct fat_entry *,
17 int (*ent_get)(struct fat_entry *);
18 void (*ent_put)(struct fat_entry *, int);
19 int (*ent_next)(struct fat_entry *);
22 static DEFINE_SPINLOCK(fat12_entry_lock);
24 static void fat12_ent_blocknr(struct super_block *sb, int entry,
25 int *offset, sector_t *blocknr)
27 struct msdos_sb_info *sbi = MSDOS_SB(sb);
28 int bytes = entry + (entry >> 1);
29 WARN_ON(entry < FAT_START_ENT || sbi->max_cluster <= entry);
30 *offset = bytes & (sb->s_blocksize - 1);
31 *blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits);
34 static void fat_ent_blocknr(struct super_block *sb, int entry,
35 int *offset, sector_t *blocknr)
37 struct msdos_sb_info *sbi = MSDOS_SB(sb);
38 int bytes = (entry << sbi->fatent_shift);
39 WARN_ON(entry < FAT_START_ENT || sbi->max_cluster <= entry);
40 *offset = bytes & (sb->s_blocksize - 1);
41 *blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits);
44 static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset)
46 struct buffer_head **bhs = fatent->bhs;
47 if (fatent->nr_bhs == 1) {
48 WARN_ON(offset >= (bhs[0]->b_size - 1));
49 fatent->u.ent12_p[0] = bhs[0]->b_data + offset;
50 fatent->u.ent12_p[1] = bhs[0]->b_data + (offset + 1);
52 WARN_ON(offset != (bhs[0]->b_size - 1));
53 fatent->u.ent12_p[0] = bhs[0]->b_data + offset;
54 fatent->u.ent12_p[1] = bhs[1]->b_data;
58 static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset)
60 WARN_ON(offset & (2 - 1));
61 fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset);
64 static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset)
66 WARN_ON(offset & (4 - 1));
67 fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset);
70 static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent,
71 int offset, sector_t blocknr)
73 struct buffer_head **bhs = fatent->bhs;
75 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
76 fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
78 bhs[0] = sb_bread(sb, blocknr);
82 if ((offset + 1) < sb->s_blocksize)
85 /* This entry is block boundary, it needs the next block */
87 bhs[1] = sb_bread(sb, blocknr);
92 fat12_ent_set_ptr(fatent, offset);
98 fat_msg(sb, KERN_ERR, "FAT read failed (blocknr %llu)", (llu)blocknr);
102 static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent,
103 int offset, sector_t blocknr)
105 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
107 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
108 fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
109 fatent->bhs[0] = sb_bread(sb, blocknr);
110 if (!fatent->bhs[0]) {
111 fat_msg(sb, KERN_ERR, "FAT read failed (blocknr %llu)",
116 ops->ent_set_ptr(fatent, offset);
120 static int fat12_ent_get(struct fat_entry *fatent)
122 u8 **ent12_p = fatent->u.ent12_p;
125 spin_lock(&fat12_entry_lock);
126 if (fatent->entry & 1)
127 next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4);
129 next = (*ent12_p[1] << 8) | *ent12_p[0];
130 spin_unlock(&fat12_entry_lock);
133 if (next >= BAD_FAT12)
138 static int fat16_ent_get(struct fat_entry *fatent)
140 int next = le16_to_cpu(*fatent->u.ent16_p);
141 WARN_ON((unsigned long)fatent->u.ent16_p & (2 - 1));
142 if (next >= BAD_FAT16)
147 static int fat32_ent_get(struct fat_entry *fatent)
149 int next = le32_to_cpu(*fatent->u.ent32_p) & 0x0fffffff;
150 WARN_ON((unsigned long)fatent->u.ent32_p & (4 - 1));
151 if (next >= BAD_FAT32)
156 static void fat12_ent_put(struct fat_entry *fatent, int new)
158 u8 **ent12_p = fatent->u.ent12_p;
160 if (new == FAT_ENT_EOF)
163 spin_lock(&fat12_entry_lock);
164 if (fatent->entry & 1) {
165 *ent12_p[0] = (new << 4) | (*ent12_p[0] & 0x0f);
166 *ent12_p[1] = new >> 4;
168 *ent12_p[0] = new & 0xff;
169 *ent12_p[1] = (*ent12_p[1] & 0xf0) | (new >> 8);
171 spin_unlock(&fat12_entry_lock);
173 mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
174 if (fatent->nr_bhs == 2)
175 mark_buffer_dirty_inode(fatent->bhs[1], fatent->fat_inode);
178 static void fat16_ent_put(struct fat_entry *fatent, int new)
180 if (new == FAT_ENT_EOF)
183 *fatent->u.ent16_p = cpu_to_le16(new);
184 mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
187 static void fat32_ent_put(struct fat_entry *fatent, int new)
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_inode(fatent->bhs[0], fatent->fat_inode);
195 static int fat12_ent_next(struct fat_entry *fatent)
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);
202 if (fatent->nr_bhs == 1) {
203 WARN_ON(ent12_p[0] > (u8 *)(bhs[0]->b_data +
204 (bhs[0]->b_size - 2)));
205 WARN_ON(ent12_p[1] > (u8 *)(bhs[0]->b_data +
206 (bhs[0]->b_size - 1)));
207 if (nextp < (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))) {
208 ent12_p[0] = nextp - 1;
213 WARN_ON(ent12_p[0] != (u8 *)(bhs[0]->b_data +
214 (bhs[0]->b_size - 1)));
215 WARN_ON(ent12_p[1] != (u8 *)bhs[1]->b_data);
216 ent12_p[0] = nextp - 1;
228 static int fat16_ent_next(struct fat_entry *fatent)
230 const struct buffer_head *bh = fatent->bhs[0];
232 if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) {
236 fatent->u.ent16_p = NULL;
240 static int fat32_ent_next(struct fat_entry *fatent)
242 const struct buffer_head *bh = fatent->bhs[0];
244 if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) {
248 fatent->u.ent32_p = NULL;
252 static struct fatent_operations fat12_ops = {
253 .ent_blocknr = fat12_ent_blocknr,
254 .ent_set_ptr = fat12_ent_set_ptr,
255 .ent_bread = fat12_ent_bread,
256 .ent_get = fat12_ent_get,
257 .ent_put = fat12_ent_put,
258 .ent_next = fat12_ent_next,
261 static struct fatent_operations fat16_ops = {
262 .ent_blocknr = fat_ent_blocknr,
263 .ent_set_ptr = fat16_ent_set_ptr,
264 .ent_bread = fat_ent_bread,
265 .ent_get = fat16_ent_get,
266 .ent_put = fat16_ent_put,
267 .ent_next = fat16_ent_next,
270 static struct fatent_operations fat32_ops = {
271 .ent_blocknr = fat_ent_blocknr,
272 .ent_set_ptr = fat32_ent_set_ptr,
273 .ent_bread = fat_ent_bread,
274 .ent_get = fat32_ent_get,
275 .ent_put = fat32_ent_put,
276 .ent_next = fat32_ent_next,
279 static inline void lock_fat(struct msdos_sb_info *sbi)
281 mutex_lock(&sbi->fat_lock);
284 static inline void unlock_fat(struct msdos_sb_info *sbi)
286 mutex_unlock(&sbi->fat_lock);
289 void fat_ent_access_init(struct super_block *sb)
291 struct msdos_sb_info *sbi = MSDOS_SB(sb);
293 mutex_init(&sbi->fat_lock);
295 switch (sbi->fat_bits) {
297 sbi->fatent_shift = 2;
298 sbi->fatent_ops = &fat32_ops;
301 sbi->fatent_shift = 1;
302 sbi->fatent_ops = &fat16_ops;
305 sbi->fatent_shift = -1;
306 sbi->fatent_ops = &fat12_ops;
311 static void mark_fsinfo_dirty(struct super_block *sb)
313 struct msdos_sb_info *sbi = MSDOS_SB(sb);
315 if (sb->s_flags & MS_RDONLY || sbi->fat_bits != 32)
318 __mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
321 static inline int fat_ent_update_ptr(struct super_block *sb,
322 struct fat_entry *fatent,
323 int offset, sector_t blocknr)
325 struct msdos_sb_info *sbi = MSDOS_SB(sb);
326 struct fatent_operations *ops = sbi->fatent_ops;
327 struct buffer_head **bhs = fatent->bhs;
329 /* Is this fatent's blocks including this entry? */
330 if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
332 if (sbi->fat_bits == 12) {
333 if ((offset + 1) < sb->s_blocksize) {
334 /* This entry is on bhs[0]. */
335 if (fatent->nr_bhs == 2) {
340 /* This entry needs the next block. */
341 if (fatent->nr_bhs != 2)
343 if (bhs[1]->b_blocknr != (blocknr + 1))
347 ops->ent_set_ptr(fatent, offset);
351 int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
353 struct super_block *sb = inode->i_sb;
354 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
355 struct fatent_operations *ops = sbi->fatent_ops;
359 if (entry < FAT_START_ENT || sbi->max_cluster <= entry) {
360 fatent_brelse(fatent);
361 fat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", entry);
365 fatent_set_entry(fatent, entry);
366 ops->ent_blocknr(sb, entry, &offset, &blocknr);
368 if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) {
369 fatent_brelse(fatent);
370 err = ops->ent_bread(sb, fatent, offset, blocknr);
374 return ops->ent_get(fatent);
377 /* FIXME: We can write the blocks as more big chunk. */
378 static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
381 struct msdos_sb_info *sbi = MSDOS_SB(sb);
382 struct buffer_head *c_bh;
386 for (copy = 1; copy < sbi->fats; copy++) {
387 sector_t backup_fat = sbi->fat_length * copy;
389 for (n = 0; n < nr_bhs; n++) {
390 c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr);
395 memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
396 set_buffer_uptodate(c_bh);
397 mark_buffer_dirty_inode(c_bh, sbi->fat_inode);
398 if (sb->s_flags & MS_SYNCHRONOUS)
399 err = sync_dirty_buffer(c_bh);
409 int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
412 struct super_block *sb = inode->i_sb;
413 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
416 ops->ent_put(fatent, new);
418 err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
422 return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
425 static inline int fat_ent_next(struct msdos_sb_info *sbi,
426 struct fat_entry *fatent)
428 if (sbi->fatent_ops->ent_next(fatent)) {
429 if (fatent->entry < sbi->max_cluster)
435 static inline int fat_ent_read_block(struct super_block *sb,
436 struct fat_entry *fatent)
438 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
442 fatent_brelse(fatent);
443 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
444 return ops->ent_bread(sb, fatent, offset, blocknr);
447 static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
448 struct fat_entry *fatent)
452 for (n = 0; n < fatent->nr_bhs; n++) {
453 for (i = 0; i < *nr_bhs; i++) {
454 if (fatent->bhs[n] == bhs[i])
458 get_bh(fatent->bhs[n]);
459 bhs[i] = fatent->bhs[n];
465 int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
467 struct super_block *sb = inode->i_sb;
468 struct msdos_sb_info *sbi = MSDOS_SB(sb);
469 struct fatent_operations *ops = sbi->fatent_ops;
470 struct fat_entry fatent, prev_ent;
471 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
472 int i, count, err, nr_bhs, idx_clus;
474 BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */
477 if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
478 sbi->free_clusters < nr_cluster) {
483 err = nr_bhs = idx_clus = 0;
484 count = FAT_START_ENT;
485 fatent_init(&prev_ent);
486 fatent_init(&fatent);
487 fatent_set_entry(&fatent, sbi->prev_free + 1);
488 while (count < sbi->max_cluster) {
489 if (fatent.entry >= sbi->max_cluster)
490 fatent.entry = FAT_START_ENT;
491 fatent_set_entry(&fatent, fatent.entry);
492 err = fat_ent_read_block(sb, &fatent);
496 /* Find the free entries in a block */
498 if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
499 int entry = fatent.entry;
501 /* make the cluster chain */
502 ops->ent_put(&fatent, FAT_ENT_EOF);
504 ops->ent_put(&prev_ent, entry);
506 fat_collect_bhs(bhs, &nr_bhs, &fatent);
508 sbi->prev_free = entry;
509 if (sbi->free_clusters != -1)
510 sbi->free_clusters--;
512 cluster[idx_clus] = entry;
514 if (idx_clus == nr_cluster)
518 * fat_collect_bhs() gets ref-count of bhs,
519 * so we can still use the prev_ent.
524 if (count == sbi->max_cluster)
526 } while (fat_ent_next(sbi, &fatent));
529 /* Couldn't allocate the free entries */
530 sbi->free_clusters = 0;
531 sbi->free_clus_valid = 1;
536 mark_fsinfo_dirty(sb);
537 fatent_brelse(&fatent);
539 if (inode_needs_sync(inode))
540 err = fat_sync_bhs(bhs, nr_bhs);
542 err = fat_mirror_bhs(sb, bhs, nr_bhs);
544 for (i = 0; i < nr_bhs; i++)
548 fat_free_clusters(inode, cluster[0]);
553 int fat_free_clusters(struct inode *inode, int cluster)
555 struct super_block *sb = inode->i_sb;
556 struct msdos_sb_info *sbi = MSDOS_SB(sb);
557 struct fatent_operations *ops = sbi->fatent_ops;
558 struct fat_entry fatent;
559 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
561 int first_cl = cluster, dirty_fsinfo = 0;
564 fatent_init(&fatent);
567 cluster = fat_ent_read(inode, &fatent, cluster);
571 } else if (cluster == FAT_ENT_FREE) {
572 fat_fs_error(sb, "%s: deleting FAT entry beyond EOF",
578 if (sbi->options.discard) {
580 * Issue discard for the sectors we no longer
581 * care about, batching contiguous clusters
584 if (cluster != fatent.entry + 1) {
585 int nr_clus = fatent.entry - first_cl + 1;
588 fat_clus_to_blknr(sbi, first_cl),
589 nr_clus * sbi->sec_per_clus,
596 ops->ent_put(&fatent, FAT_ENT_FREE);
597 if (sbi->free_clusters != -1) {
598 sbi->free_clusters++;
602 if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
603 if (sb->s_flags & MS_SYNCHRONOUS) {
604 err = fat_sync_bhs(bhs, nr_bhs);
608 err = fat_mirror_bhs(sb, bhs, nr_bhs);
611 for (i = 0; i < nr_bhs; i++)
615 fat_collect_bhs(bhs, &nr_bhs, &fatent);
616 } while (cluster != FAT_ENT_EOF);
618 if (sb->s_flags & MS_SYNCHRONOUS) {
619 err = fat_sync_bhs(bhs, nr_bhs);
623 err = fat_mirror_bhs(sb, bhs, nr_bhs);
625 fatent_brelse(&fatent);
626 for (i = 0; i < nr_bhs; i++)
630 mark_fsinfo_dirty(sb);
634 EXPORT_SYMBOL_GPL(fat_free_clusters);
636 /* 128kb is the whole sectors for FAT12 and FAT16 */
637 #define FAT_READA_SIZE (128 * 1024)
639 static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent,
640 unsigned long reada_blocks)
642 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
646 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
648 for (i = 0; i < reada_blocks; i++)
649 sb_breadahead(sb, blocknr + i);
652 int fat_count_free_clusters(struct super_block *sb)
654 struct msdos_sb_info *sbi = MSDOS_SB(sb);
655 struct fatent_operations *ops = sbi->fatent_ops;
656 struct fat_entry fatent;
657 unsigned long reada_blocks, reada_mask, cur_block;
661 if (sbi->free_clusters != -1 && sbi->free_clus_valid)
664 reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
665 reada_mask = reada_blocks - 1;
669 fatent_init(&fatent);
670 fatent_set_entry(&fatent, FAT_START_ENT);
671 while (fatent.entry < sbi->max_cluster) {
672 /* readahead of fat blocks */
673 if ((cur_block & reada_mask) == 0) {
674 unsigned long rest = sbi->fat_length - cur_block;
675 fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
679 err = fat_ent_read_block(sb, &fatent);
684 if (ops->ent_get(&fatent) == FAT_ENT_FREE)
686 } while (fat_ent_next(sbi, &fatent));
688 sbi->free_clusters = free;
689 sbi->free_clus_valid = 1;
690 mark_fsinfo_dirty(sb);
691 fatent_brelse(&fatent);