2 * Copyright (C) 2004, OGAWA Hirofumi
3 * Released under GPL v2.
6 #include <linux/module.h>
8 #include <linux/msdos_fs.h>
10 struct fatent_operations
{
11 void (*ent_blocknr
)(struct super_block
*, int, int *, sector_t
*);
12 void (*ent_set_ptr
)(struct fat_entry
*, int);
13 int (*ent_bread
)(struct super_block
*, struct fat_entry
*,
15 int (*ent_get
)(struct fat_entry
*);
16 void (*ent_put
)(struct fat_entry
*, int);
17 int (*ent_next
)(struct fat_entry
*);
20 static DEFINE_SPINLOCK(fat12_entry_lock
);
22 static void fat12_ent_blocknr(struct super_block
*sb
, int entry
,
23 int *offset
, sector_t
*blocknr
)
25 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
26 int bytes
= entry
+ (entry
>> 1);
27 WARN_ON(entry
< FAT_START_ENT
|| sbi
->max_cluster
<= entry
);
28 *offset
= bytes
& (sb
->s_blocksize
- 1);
29 *blocknr
= sbi
->fat_start
+ (bytes
>> sb
->s_blocksize_bits
);
32 static void fat_ent_blocknr(struct super_block
*sb
, int entry
,
33 int *offset
, sector_t
*blocknr
)
35 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
36 int bytes
= (entry
<< sbi
->fatent_shift
);
37 WARN_ON(entry
< FAT_START_ENT
|| sbi
->max_cluster
<= entry
);
38 *offset
= bytes
& (sb
->s_blocksize
- 1);
39 *blocknr
= sbi
->fat_start
+ (bytes
>> sb
->s_blocksize_bits
);
42 static void fat12_ent_set_ptr(struct fat_entry
*fatent
, int offset
)
44 struct buffer_head
**bhs
= fatent
->bhs
;
45 if (fatent
->nr_bhs
== 1) {
46 WARN_ON(offset
>= (bhs
[0]->b_size
- 1));
47 fatent
->u
.ent12_p
[0] = bhs
[0]->b_data
+ offset
;
48 fatent
->u
.ent12_p
[1] = bhs
[0]->b_data
+ (offset
+ 1);
50 WARN_ON(offset
!= (bhs
[0]->b_size
- 1));
51 fatent
->u
.ent12_p
[0] = bhs
[0]->b_data
+ offset
;
52 fatent
->u
.ent12_p
[1] = bhs
[1]->b_data
;
56 static void fat16_ent_set_ptr(struct fat_entry
*fatent
, int offset
)
58 WARN_ON(offset
& (2 - 1));
59 fatent
->u
.ent16_p
= (__le16
*)(fatent
->bhs
[0]->b_data
+ offset
);
62 static void fat32_ent_set_ptr(struct fat_entry
*fatent
, int offset
)
64 WARN_ON(offset
& (4 - 1));
65 fatent
->u
.ent32_p
= (__le32
*)(fatent
->bhs
[0]->b_data
+ offset
);
68 static int fat12_ent_bread(struct super_block
*sb
, struct fat_entry
*fatent
,
69 int offset
, sector_t blocknr
)
71 struct buffer_head
**bhs
= fatent
->bhs
;
73 WARN_ON(blocknr
< MSDOS_SB(sb
)->fat_start
);
74 bhs
[0] = sb_bread(sb
, blocknr
);
78 if ((offset
+ 1) < sb
->s_blocksize
)
81 /* This entry is block boundary, it needs the next block */
83 bhs
[1] = sb_bread(sb
, blocknr
);
88 fat12_ent_set_ptr(fatent
, offset
);
94 printk(KERN_ERR
"FAT: FAT read failed (blocknr %llu)\n",
95 (unsigned long long)blocknr
);
99 static int fat_ent_bread(struct super_block
*sb
, struct fat_entry
*fatent
,
100 int offset
, sector_t blocknr
)
102 struct fatent_operations
*ops
= MSDOS_SB(sb
)->fatent_ops
;
104 WARN_ON(blocknr
< MSDOS_SB(sb
)->fat_start
);
105 fatent
->bhs
[0] = sb_bread(sb
, blocknr
);
106 if (!fatent
->bhs
[0]) {
107 printk(KERN_ERR
"FAT: FAT read failed (blocknr %llu)\n",
108 (unsigned long long)blocknr
);
112 ops
->ent_set_ptr(fatent
, offset
);
116 static int fat12_ent_get(struct fat_entry
*fatent
)
118 u8
**ent12_p
= fatent
->u
.ent12_p
;
121 spin_lock(&fat12_entry_lock
);
122 if (fatent
->entry
& 1)
123 next
= (*ent12_p
[0] >> 4) | (*ent12_p
[1] << 4);
125 next
= (*ent12_p
[1] << 8) | *ent12_p
[0];
126 spin_unlock(&fat12_entry_lock
);
129 if (next
>= BAD_FAT12
)
134 static int fat16_ent_get(struct fat_entry
*fatent
)
136 int next
= le16_to_cpu(*fatent
->u
.ent16_p
);
137 WARN_ON((unsigned long)fatent
->u
.ent16_p
& (2 - 1));
138 if (next
>= BAD_FAT16
)
143 static int fat32_ent_get(struct fat_entry
*fatent
)
145 int next
= le32_to_cpu(*fatent
->u
.ent32_p
) & 0x0fffffff;
146 WARN_ON((unsigned long)fatent
->u
.ent32_p
& (4 - 1));
147 if (next
>= BAD_FAT32
)
152 static void fat12_ent_put(struct fat_entry
*fatent
, int new)
154 u8
**ent12_p
= fatent
->u
.ent12_p
;
156 if (new == FAT_ENT_EOF
)
159 spin_lock(&fat12_entry_lock
);
160 if (fatent
->entry
& 1) {
161 *ent12_p
[0] = (new << 4) | (*ent12_p
[0] & 0x0f);
162 *ent12_p
[1] = new >> 4;
164 *ent12_p
[0] = new & 0xff;
165 *ent12_p
[1] = (*ent12_p
[1] & 0xf0) | (new >> 8);
167 spin_unlock(&fat12_entry_lock
);
169 mark_buffer_dirty(fatent
->bhs
[0]);
170 if (fatent
->nr_bhs
== 2)
171 mark_buffer_dirty(fatent
->bhs
[1]);
174 static void fat16_ent_put(struct fat_entry
*fatent
, int new)
176 if (new == FAT_ENT_EOF
)
179 *fatent
->u
.ent16_p
= cpu_to_le16(new);
180 mark_buffer_dirty(fatent
->bhs
[0]);
183 static void fat32_ent_put(struct fat_entry
*fatent
, int new)
185 if (new == FAT_ENT_EOF
)
188 WARN_ON(new & 0xf0000000);
189 new |= le32_to_cpu(*fatent
->u
.ent32_p
) & ~0x0fffffff;
190 *fatent
->u
.ent32_p
= cpu_to_le32(new);
191 mark_buffer_dirty(fatent
->bhs
[0]);
194 static int fat12_ent_next(struct fat_entry
*fatent
)
196 u8
**ent12_p
= fatent
->u
.ent12_p
;
197 struct buffer_head
**bhs
= fatent
->bhs
;
198 u8
*nextp
= ent12_p
[1] + 1 + (fatent
->entry
& 1);
201 if (fatent
->nr_bhs
== 1) {
202 WARN_ON(ent12_p
[0] > (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 2)));
203 WARN_ON(ent12_p
[1] > (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 1)));
204 if (nextp
< (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 1))) {
205 ent12_p
[0] = nextp
- 1;
210 WARN_ON(ent12_p
[0] != (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 1)));
211 WARN_ON(ent12_p
[1] != (u8
*)bhs
[1]->b_data
);
212 ent12_p
[0] = nextp
- 1;
224 static int fat16_ent_next(struct fat_entry
*fatent
)
226 const struct buffer_head
*bh
= fatent
->bhs
[0];
228 if (fatent
->u
.ent16_p
< (__le16
*)(bh
->b_data
+ (bh
->b_size
- 2))) {
232 fatent
->u
.ent16_p
= NULL
;
236 static int fat32_ent_next(struct fat_entry
*fatent
)
238 const struct buffer_head
*bh
= fatent
->bhs
[0];
240 if (fatent
->u
.ent32_p
< (__le32
*)(bh
->b_data
+ (bh
->b_size
- 4))) {
244 fatent
->u
.ent32_p
= NULL
;
248 static struct fatent_operations fat12_ops
= {
249 .ent_blocknr
= fat12_ent_blocknr
,
250 .ent_set_ptr
= fat12_ent_set_ptr
,
251 .ent_bread
= fat12_ent_bread
,
252 .ent_get
= fat12_ent_get
,
253 .ent_put
= fat12_ent_put
,
254 .ent_next
= fat12_ent_next
,
257 static struct fatent_operations fat16_ops
= {
258 .ent_blocknr
= fat_ent_blocknr
,
259 .ent_set_ptr
= fat16_ent_set_ptr
,
260 .ent_bread
= fat_ent_bread
,
261 .ent_get
= fat16_ent_get
,
262 .ent_put
= fat16_ent_put
,
263 .ent_next
= fat16_ent_next
,
266 static struct fatent_operations fat32_ops
= {
267 .ent_blocknr
= fat_ent_blocknr
,
268 .ent_set_ptr
= fat32_ent_set_ptr
,
269 .ent_bread
= fat_ent_bread
,
270 .ent_get
= fat32_ent_get
,
271 .ent_put
= fat32_ent_put
,
272 .ent_next
= fat32_ent_next
,
275 static inline void lock_fat(struct msdos_sb_info
*sbi
)
277 mutex_lock(&sbi
->fat_lock
);
280 static inline void unlock_fat(struct msdos_sb_info
*sbi
)
282 mutex_unlock(&sbi
->fat_lock
);
285 void fat_ent_access_init(struct super_block
*sb
)
287 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
289 mutex_init(&sbi
->fat_lock
);
291 switch (sbi
->fat_bits
) {
293 sbi
->fatent_shift
= 2;
294 sbi
->fatent_ops
= &fat32_ops
;
297 sbi
->fatent_shift
= 1;
298 sbi
->fatent_ops
= &fat16_ops
;
301 sbi
->fatent_shift
= -1;
302 sbi
->fatent_ops
= &fat12_ops
;
307 static inline int fat_ent_update_ptr(struct super_block
*sb
,
308 struct fat_entry
*fatent
,
309 int offset
, sector_t blocknr
)
311 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
312 struct fatent_operations
*ops
= sbi
->fatent_ops
;
313 struct buffer_head
**bhs
= fatent
->bhs
;
315 /* Is this fatent's blocks including this entry? */
316 if (!fatent
->nr_bhs
|| bhs
[0]->b_blocknr
!= blocknr
)
318 /* Does this entry need the next block? */
319 if (sbi
->fat_bits
== 12 && (offset
+ 1) >= sb
->s_blocksize
) {
320 if (fatent
->nr_bhs
!= 2 || bhs
[1]->b_blocknr
!= (blocknr
+ 1))
323 ops
->ent_set_ptr(fatent
, offset
);
327 int fat_ent_read(struct inode
*inode
, struct fat_entry
*fatent
, int entry
)
329 struct super_block
*sb
= inode
->i_sb
;
330 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
331 struct fatent_operations
*ops
= sbi
->fatent_ops
;
335 if (entry
< FAT_START_ENT
|| sbi
->max_cluster
<= entry
) {
336 fatent_brelse(fatent
);
337 fat_fs_panic(sb
, "invalid access to FAT (entry 0x%08x)", entry
);
341 fatent_set_entry(fatent
, entry
);
342 ops
->ent_blocknr(sb
, entry
, &offset
, &blocknr
);
344 if (!fat_ent_update_ptr(sb
, fatent
, offset
, blocknr
)) {
345 fatent_brelse(fatent
);
346 err
= ops
->ent_bread(sb
, fatent
, offset
, blocknr
);
350 return ops
->ent_get(fatent
);
353 /* FIXME: We can write the blocks as more big chunk. */
354 static int fat_mirror_bhs(struct super_block
*sb
, struct buffer_head
**bhs
,
357 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
358 struct buffer_head
*c_bh
;
362 for (copy
= 1; copy
< sbi
->fats
; copy
++) {
363 sector_t backup_fat
= sbi
->fat_length
* copy
;
365 for (n
= 0; n
< nr_bhs
; n
++) {
366 c_bh
= sb_getblk(sb
, backup_fat
+ bhs
[n
]->b_blocknr
);
371 memcpy(c_bh
->b_data
, bhs
[n
]->b_data
, sb
->s_blocksize
);
372 set_buffer_uptodate(c_bh
);
373 mark_buffer_dirty(c_bh
);
374 if (sb
->s_flags
& MS_SYNCHRONOUS
)
375 err
= sync_dirty_buffer(c_bh
);
385 int fat_ent_write(struct inode
*inode
, struct fat_entry
*fatent
,
388 struct super_block
*sb
= inode
->i_sb
;
389 struct fatent_operations
*ops
= MSDOS_SB(sb
)->fatent_ops
;
392 ops
->ent_put(fatent
, new);
394 err
= fat_sync_bhs(fatent
->bhs
, fatent
->nr_bhs
);
398 return fat_mirror_bhs(sb
, fatent
->bhs
, fatent
->nr_bhs
);
401 static inline int fat_ent_next(struct msdos_sb_info
*sbi
,
402 struct fat_entry
*fatent
)
404 if (sbi
->fatent_ops
->ent_next(fatent
)) {
405 if (fatent
->entry
< sbi
->max_cluster
)
411 static inline int fat_ent_read_block(struct super_block
*sb
,
412 struct fat_entry
*fatent
)
414 struct fatent_operations
*ops
= MSDOS_SB(sb
)->fatent_ops
;
418 fatent_brelse(fatent
);
419 ops
->ent_blocknr(sb
, fatent
->entry
, &offset
, &blocknr
);
420 return ops
->ent_bread(sb
, fatent
, offset
, blocknr
);
423 static void fat_collect_bhs(struct buffer_head
**bhs
, int *nr_bhs
,
424 struct fat_entry
*fatent
)
428 for (n
= 0; n
< fatent
->nr_bhs
; n
++) {
429 for (i
= 0; i
< *nr_bhs
; i
++) {
430 if (fatent
->bhs
[n
] == bhs
[i
])
434 get_bh(fatent
->bhs
[n
]);
435 bhs
[i
] = fatent
->bhs
[n
];
441 int fat_alloc_clusters(struct inode
*inode
, int *cluster
, int nr_cluster
)
443 struct super_block
*sb
= inode
->i_sb
;
444 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
445 struct fatent_operations
*ops
= sbi
->fatent_ops
;
446 struct fat_entry fatent
, prev_ent
;
447 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
448 int i
, count
, err
, nr_bhs
, idx_clus
;
450 BUG_ON(nr_cluster
> (MAX_BUF_PER_PAGE
/ 2)); /* fixed limit */
453 if (sbi
->free_clusters
!= -1 && sbi
->free_clusters
< nr_cluster
) {
458 err
= nr_bhs
= idx_clus
= 0;
459 count
= FAT_START_ENT
;
460 fatent_init(&prev_ent
);
461 fatent_init(&fatent
);
462 fatent_set_entry(&fatent
, sbi
->prev_free
+ 1);
463 while (count
< sbi
->max_cluster
) {
464 if (fatent
.entry
>= sbi
->max_cluster
)
465 fatent
.entry
= FAT_START_ENT
;
466 fatent_set_entry(&fatent
, fatent
.entry
);
467 err
= fat_ent_read_block(sb
, &fatent
);
471 /* Find the free entries in a block */
473 if (ops
->ent_get(&fatent
) == FAT_ENT_FREE
) {
474 int entry
= fatent
.entry
;
476 /* make the cluster chain */
477 ops
->ent_put(&fatent
, FAT_ENT_EOF
);
479 ops
->ent_put(&prev_ent
, entry
);
481 fat_collect_bhs(bhs
, &nr_bhs
, &fatent
);
483 sbi
->prev_free
= entry
;
484 if (sbi
->free_clusters
!= -1)
485 sbi
->free_clusters
--;
488 cluster
[idx_clus
] = entry
;
490 if (idx_clus
== nr_cluster
)
494 * fat_collect_bhs() gets ref-count of bhs,
495 * so we can still use the prev_ent.
500 if (count
== sbi
->max_cluster
)
502 } while (fat_ent_next(sbi
, &fatent
));
505 /* Couldn't allocate the free entries */
506 sbi
->free_clusters
= 0;
512 fatent_brelse(&fatent
);
514 if (inode_needs_sync(inode
))
515 err
= fat_sync_bhs(bhs
, nr_bhs
);
517 err
= fat_mirror_bhs(sb
, bhs
, nr_bhs
);
519 for (i
= 0; i
< nr_bhs
; i
++)
523 fat_free_clusters(inode
, cluster
[0]);
528 int fat_free_clusters(struct inode
*inode
, int cluster
)
530 struct super_block
*sb
= inode
->i_sb
;
531 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
532 struct fatent_operations
*ops
= sbi
->fatent_ops
;
533 struct fat_entry fatent
;
534 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
538 fatent_init(&fatent
);
541 cluster
= fat_ent_read(inode
, &fatent
, cluster
);
545 } else if (cluster
== FAT_ENT_FREE
) {
546 fat_fs_panic(sb
, "%s: deleting FAT entry beyond EOF",
552 ops
->ent_put(&fatent
, FAT_ENT_FREE
);
553 if (sbi
->free_clusters
!= -1) {
554 sbi
->free_clusters
++;
558 if (nr_bhs
+ fatent
.nr_bhs
> MAX_BUF_PER_PAGE
) {
559 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
560 err
= fat_sync_bhs(bhs
, nr_bhs
);
564 err
= fat_mirror_bhs(sb
, bhs
, nr_bhs
);
567 for (i
= 0; i
< nr_bhs
; i
++)
571 fat_collect_bhs(bhs
, &nr_bhs
, &fatent
);
572 } while (cluster
!= FAT_ENT_EOF
);
574 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
575 err
= fat_sync_bhs(bhs
, nr_bhs
);
579 err
= fat_mirror_bhs(sb
, bhs
, nr_bhs
);
581 fatent_brelse(&fatent
);
582 for (i
= 0; i
< nr_bhs
; i
++)
586 fat_clusters_flush(sb
);
591 EXPORT_SYMBOL_GPL(fat_free_clusters
);
593 int fat_count_free_clusters(struct super_block
*sb
)
595 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
596 struct fatent_operations
*ops
= sbi
->fatent_ops
;
597 struct fat_entry fatent
;
601 if (sbi
->free_clusters
!= -1)
605 fatent_init(&fatent
);
606 fatent_set_entry(&fatent
, FAT_START_ENT
);
607 while (fatent
.entry
< sbi
->max_cluster
) {
608 err
= fat_ent_read_block(sb
, &fatent
);
613 if (ops
->ent_get(&fatent
) == FAT_ENT_FREE
)
615 } while (fat_ent_next(sbi
, &fatent
));
617 sbi
->free_clusters
= free
;
619 fatent_brelse(&fatent
);