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 void fat12_ent_blocknr(struct super_block
*sb
, int entry
,
21 int *offset
, sector_t
*blocknr
)
23 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
24 int bytes
= entry
+ (entry
>> 1);
25 WARN_ON(entry
< FAT_START_ENT
|| sbi
->max_cluster
<= entry
);
26 *offset
= bytes
& (sb
->s_blocksize
- 1);
27 *blocknr
= sbi
->fat_start
+ (bytes
>> sb
->s_blocksize_bits
);
30 static void fat_ent_blocknr(struct super_block
*sb
, int entry
,
31 int *offset
, sector_t
*blocknr
)
33 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
34 int bytes
= (entry
<< sbi
->fatent_shift
);
35 WARN_ON(entry
< FAT_START_ENT
|| sbi
->max_cluster
<= entry
);
36 *offset
= bytes
& (sb
->s_blocksize
- 1);
37 *blocknr
= sbi
->fat_start
+ (bytes
>> sb
->s_blocksize_bits
);
40 static void fat12_ent_set_ptr(struct fat_entry
*fatent
, int offset
)
42 struct buffer_head
**bhs
= fatent
->bhs
;
43 if (fatent
->nr_bhs
== 1) {
44 WARN_ON(offset
>= (bhs
[0]->b_size
- 1));
45 fatent
->u
.ent12_p
[0] = bhs
[0]->b_data
+ offset
;
46 fatent
->u
.ent12_p
[1] = bhs
[0]->b_data
+ (offset
+ 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
[1]->b_data
;
54 static void fat16_ent_set_ptr(struct fat_entry
*fatent
, int offset
)
56 WARN_ON(offset
& (2 - 1));
57 fatent
->u
.ent16_p
= (__le16
*)(fatent
->bhs
[0]->b_data
+ offset
);
60 static void fat32_ent_set_ptr(struct fat_entry
*fatent
, int offset
)
62 WARN_ON(offset
& (4 - 1));
63 fatent
->u
.ent32_p
= (__le32
*)(fatent
->bhs
[0]->b_data
+ offset
);
66 static int fat12_ent_bread(struct super_block
*sb
, struct fat_entry
*fatent
,
67 int offset
, sector_t blocknr
)
69 struct buffer_head
**bhs
= fatent
->bhs
;
71 WARN_ON(blocknr
< MSDOS_SB(sb
)->fat_start
);
72 bhs
[0] = sb_bread(sb
, blocknr
);
76 if ((offset
+ 1) < sb
->s_blocksize
)
79 /* This entry is block boundary, it needs the next block */
81 bhs
[1] = sb_bread(sb
, blocknr
);
86 fat12_ent_set_ptr(fatent
, offset
);
92 printk(KERN_ERR
"FAT: FAT read failed (blocknr %llu)\n",
93 (unsigned long long)blocknr
);
97 static int fat_ent_bread(struct super_block
*sb
, struct fat_entry
*fatent
,
98 int offset
, sector_t blocknr
)
100 struct fatent_operations
*ops
= MSDOS_SB(sb
)->fatent_ops
;
102 WARN_ON(blocknr
< MSDOS_SB(sb
)->fat_start
);
103 fatent
->bhs
[0] = sb_bread(sb
, blocknr
);
104 if (!fatent
->bhs
[0]) {
105 printk(KERN_ERR
"FAT: FAT read failed (blocknr %llu)\n",
106 (unsigned long long)blocknr
);
110 ops
->ent_set_ptr(fatent
, offset
);
114 static int fat12_ent_get(struct fat_entry
*fatent
)
116 u8
**ent12_p
= fatent
->u
.ent12_p
;
119 if (fatent
->entry
& 1)
120 next
= (*ent12_p
[0] >> 4) | (*ent12_p
[1] << 4);
122 next
= (*ent12_p
[1] << 8) | *ent12_p
[0];
124 if (next
>= BAD_FAT12
)
129 static int fat16_ent_get(struct fat_entry
*fatent
)
131 int next
= le16_to_cpu(*fatent
->u
.ent16_p
);
132 WARN_ON((unsigned long)fatent
->u
.ent16_p
& (2 - 1));
133 if (next
>= BAD_FAT16
)
138 static int fat32_ent_get(struct fat_entry
*fatent
)
140 int next
= le32_to_cpu(*fatent
->u
.ent32_p
) & 0x0fffffff;
141 WARN_ON((unsigned long)fatent
->u
.ent32_p
& (4 - 1));
142 if (next
>= BAD_FAT32
)
147 static void fat12_ent_put(struct fat_entry
*fatent
, int new)
149 u8
**ent12_p
= fatent
->u
.ent12_p
;
151 if (new == FAT_ENT_EOF
)
154 if (fatent
->entry
& 1) {
155 *ent12_p
[0] = (new << 4) | (*ent12_p
[0] & 0x0f);
156 *ent12_p
[1] = new >> 4;
158 *ent12_p
[0] = new & 0xff;
159 *ent12_p
[1] = (*ent12_p
[1] & 0xf0) | (new >> 8);
162 mark_buffer_dirty(fatent
->bhs
[0]);
163 if (fatent
->nr_bhs
== 2)
164 mark_buffer_dirty(fatent
->bhs
[1]);
167 static void fat16_ent_put(struct fat_entry
*fatent
, int new)
169 if (new == FAT_ENT_EOF
)
172 *fatent
->u
.ent16_p
= cpu_to_le16(new);
173 mark_buffer_dirty(fatent
->bhs
[0]);
176 static void fat32_ent_put(struct fat_entry
*fatent
, int new)
178 if (new == FAT_ENT_EOF
)
181 WARN_ON(new & 0xf0000000);
182 new |= le32_to_cpu(*fatent
->u
.ent32_p
) & ~0x0fffffff;
183 *fatent
->u
.ent32_p
= cpu_to_le32(new);
184 mark_buffer_dirty(fatent
->bhs
[0]);
187 static int fat12_ent_next(struct fat_entry
*fatent
)
189 u8
**ent12_p
= fatent
->u
.ent12_p
;
190 struct buffer_head
**bhs
= fatent
->bhs
;
191 u8
*nextp
= ent12_p
[1] + 1 + (fatent
->entry
& 1);
194 if (fatent
->nr_bhs
== 1) {
195 WARN_ON(ent12_p
[0] > (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 2)));
196 WARN_ON(ent12_p
[1] > (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 1)));
197 if (nextp
< (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 1))) {
198 ent12_p
[0] = nextp
- 1;
203 WARN_ON(ent12_p
[0] != (u8
*)(bhs
[0]->b_data
+ (bhs
[0]->b_size
- 1)));
204 WARN_ON(ent12_p
[1] != (u8
*)bhs
[1]->b_data
);
205 ent12_p
[0] = nextp
- 1;
217 static int fat16_ent_next(struct fat_entry
*fatent
)
219 const struct buffer_head
*bh
= fatent
->bhs
[0];
221 if (fatent
->u
.ent16_p
< (__le16
*)(bh
->b_data
+ (bh
->b_size
- 2))) {
225 fatent
->u
.ent16_p
= NULL
;
229 static int fat32_ent_next(struct fat_entry
*fatent
)
231 const struct buffer_head
*bh
= fatent
->bhs
[0];
233 if (fatent
->u
.ent32_p
< (__le32
*)(bh
->b_data
+ (bh
->b_size
- 4))) {
237 fatent
->u
.ent32_p
= NULL
;
241 static struct fatent_operations fat12_ops
= {
242 .ent_blocknr
= fat12_ent_blocknr
,
243 .ent_set_ptr
= fat12_ent_set_ptr
,
244 .ent_bread
= fat12_ent_bread
,
245 .ent_get
= fat12_ent_get
,
246 .ent_put
= fat12_ent_put
,
247 .ent_next
= fat12_ent_next
,
250 static struct fatent_operations fat16_ops
= {
251 .ent_blocknr
= fat_ent_blocknr
,
252 .ent_set_ptr
= fat16_ent_set_ptr
,
253 .ent_bread
= fat_ent_bread
,
254 .ent_get
= fat16_ent_get
,
255 .ent_put
= fat16_ent_put
,
256 .ent_next
= fat16_ent_next
,
259 static struct fatent_operations fat32_ops
= {
260 .ent_blocknr
= fat_ent_blocknr
,
261 .ent_set_ptr
= fat32_ent_set_ptr
,
262 .ent_bread
= fat_ent_bread
,
263 .ent_get
= fat32_ent_get
,
264 .ent_put
= fat32_ent_put
,
265 .ent_next
= fat32_ent_next
,
268 static inline void lock_fat(struct msdos_sb_info
*sbi
)
270 down(&sbi
->fat_lock
);
273 static inline void unlock_fat(struct msdos_sb_info
*sbi
)
278 void fat_ent_access_init(struct super_block
*sb
)
280 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
282 init_MUTEX(&sbi
->fat_lock
);
284 switch (sbi
->fat_bits
) {
286 sbi
->fatent_shift
= 2;
287 sbi
->fatent_ops
= &fat32_ops
;
290 sbi
->fatent_shift
= 1;
291 sbi
->fatent_ops
= &fat16_ops
;
294 sbi
->fatent_shift
= -1;
295 sbi
->fatent_ops
= &fat12_ops
;
300 static inline int fat_ent_update_ptr(struct super_block
*sb
,
301 struct fat_entry
*fatent
,
302 int offset
, sector_t blocknr
)
304 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
305 struct fatent_operations
*ops
= sbi
->fatent_ops
;
306 struct buffer_head
**bhs
= fatent
->bhs
;
308 /* Is this fatent's blocks including this entry? */
309 if (!fatent
->nr_bhs
|| bhs
[0]->b_blocknr
!= blocknr
)
311 /* Does this entry need the next block? */
312 if (sbi
->fat_bits
== 12 && (offset
+ 1) >= sb
->s_blocksize
) {
313 if (fatent
->nr_bhs
!= 2 || bhs
[1]->b_blocknr
!= (blocknr
+ 1))
316 ops
->ent_set_ptr(fatent
, offset
);
320 int fat_ent_read(struct inode
*inode
, struct fat_entry
*fatent
, int entry
)
322 struct super_block
*sb
= inode
->i_sb
;
323 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
324 struct fatent_operations
*ops
= sbi
->fatent_ops
;
328 if (entry
< FAT_START_ENT
|| sbi
->max_cluster
<= entry
) {
329 fatent_brelse(fatent
);
330 fat_fs_panic(sb
, "invalid access to FAT (entry 0x%08x)", entry
);
334 fatent_set_entry(fatent
, entry
);
335 ops
->ent_blocknr(sb
, entry
, &offset
, &blocknr
);
337 if (!fat_ent_update_ptr(sb
, fatent
, offset
, blocknr
)) {
338 fatent_brelse(fatent
);
339 err
= ops
->ent_bread(sb
, fatent
, offset
, blocknr
);
343 return ops
->ent_get(fatent
);
346 /* FIXME: We can write the blocks as more big chunk. */
347 static int fat_mirror_bhs(struct super_block
*sb
, struct buffer_head
**bhs
,
350 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
351 struct buffer_head
*c_bh
;
355 for (copy
= 1; copy
< sbi
->fats
; copy
++) {
356 sector_t backup_fat
= sbi
->fat_length
* copy
;
358 for (n
= 0; n
< nr_bhs
; n
++) {
359 c_bh
= sb_getblk(sb
, backup_fat
+ bhs
[n
]->b_blocknr
);
364 memcpy(c_bh
->b_data
, bhs
[n
]->b_data
, sb
->s_blocksize
);
365 set_buffer_uptodate(c_bh
);
366 mark_buffer_dirty(c_bh
);
367 if (sb
->s_flags
& MS_SYNCHRONOUS
)
368 err
= sync_dirty_buffer(c_bh
);
378 int fat_ent_write(struct inode
*inode
, struct fat_entry
*fatent
,
381 struct super_block
*sb
= inode
->i_sb
;
382 struct fatent_operations
*ops
= MSDOS_SB(sb
)->fatent_ops
;
385 ops
->ent_put(fatent
, new);
387 err
= fat_sync_bhs(fatent
->bhs
, fatent
->nr_bhs
);
391 return fat_mirror_bhs(sb
, fatent
->bhs
, fatent
->nr_bhs
);
394 static inline int fat_ent_next(struct msdos_sb_info
*sbi
,
395 struct fat_entry
*fatent
)
397 if (sbi
->fatent_ops
->ent_next(fatent
)) {
398 if (fatent
->entry
< sbi
->max_cluster
)
404 static inline int fat_ent_read_block(struct super_block
*sb
,
405 struct fat_entry
*fatent
)
407 struct fatent_operations
*ops
= MSDOS_SB(sb
)->fatent_ops
;
411 fatent_brelse(fatent
);
412 ops
->ent_blocknr(sb
, fatent
->entry
, &offset
, &blocknr
);
413 return ops
->ent_bread(sb
, fatent
, offset
, blocknr
);
416 static void fat_collect_bhs(struct buffer_head
**bhs
, int *nr_bhs
,
417 struct fat_entry
*fatent
)
421 for (n
= 0; n
< fatent
->nr_bhs
; n
++) {
422 for (i
= 0; i
< *nr_bhs
; i
++) {
423 if (fatent
->bhs
[n
] == bhs
[i
])
427 get_bh(fatent
->bhs
[n
]);
428 bhs
[i
] = fatent
->bhs
[n
];
434 int fat_alloc_clusters(struct inode
*inode
, int *cluster
, int nr_cluster
)
436 struct super_block
*sb
= inode
->i_sb
;
437 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
438 struct fatent_operations
*ops
= sbi
->fatent_ops
;
439 struct fat_entry fatent
, prev_ent
;
440 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
441 int i
, count
, err
, nr_bhs
, idx_clus
;
443 BUG_ON(nr_cluster
> (MAX_BUF_PER_PAGE
/ 2)); /* fixed limit */
446 if (sbi
->free_clusters
!= -1 && sbi
->free_clusters
< nr_cluster
) {
451 err
= nr_bhs
= idx_clus
= 0;
452 count
= FAT_START_ENT
;
453 fatent_init(&prev_ent
);
454 fatent_init(&fatent
);
455 fatent_set_entry(&fatent
, sbi
->prev_free
+ 1);
456 while (count
< sbi
->max_cluster
) {
457 if (fatent
.entry
>= sbi
->max_cluster
)
458 fatent
.entry
= FAT_START_ENT
;
459 fatent_set_entry(&fatent
, fatent
.entry
);
460 err
= fat_ent_read_block(sb
, &fatent
);
464 /* Find the free entries in a block */
466 if (ops
->ent_get(&fatent
) == FAT_ENT_FREE
) {
467 int entry
= fatent
.entry
;
469 /* make the cluster chain */
470 ops
->ent_put(&fatent
, FAT_ENT_EOF
);
472 ops
->ent_put(&prev_ent
, entry
);
474 fat_collect_bhs(bhs
, &nr_bhs
, &fatent
);
476 sbi
->prev_free
= entry
;
477 if (sbi
->free_clusters
!= -1)
478 sbi
->free_clusters
--;
480 cluster
[idx_clus
] = entry
;
482 if (idx_clus
== nr_cluster
)
486 * fat_collect_bhs() gets ref-count of bhs,
487 * so we can still use the prev_ent.
492 if (count
== sbi
->max_cluster
)
494 } while (fat_ent_next(sbi
, &fatent
));
497 /* Couldn't allocate the free entries */
498 sbi
->free_clusters
= 0;
503 fatent_brelse(&fatent
);
505 if (inode_needs_sync(inode
))
506 err
= fat_sync_bhs(bhs
, nr_bhs
);
508 err
= fat_mirror_bhs(sb
, bhs
, nr_bhs
);
510 for (i
= 0; i
< nr_bhs
; i
++)
512 fat_clusters_flush(sb
);
515 fat_free_clusters(inode
, cluster
[0]);
520 int fat_free_clusters(struct inode
*inode
, int cluster
)
522 struct super_block
*sb
= inode
->i_sb
;
523 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
524 struct fatent_operations
*ops
= sbi
->fatent_ops
;
525 struct fat_entry fatent
;
526 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
530 fatent_init(&fatent
);
533 cluster
= fat_ent_read(inode
, &fatent
, cluster
);
537 } else if (cluster
== FAT_ENT_FREE
) {
538 fat_fs_panic(sb
, "%s: deleting FAT entry beyond EOF",
544 ops
->ent_put(&fatent
, FAT_ENT_FREE
);
545 if (sbi
->free_clusters
!= -1)
546 sbi
->free_clusters
++;
548 if (nr_bhs
+ fatent
.nr_bhs
> MAX_BUF_PER_PAGE
) {
549 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
550 err
= fat_sync_bhs(bhs
, nr_bhs
);
554 err
= fat_mirror_bhs(sb
, bhs
, nr_bhs
);
557 for (i
= 0; i
< nr_bhs
; i
++)
561 fat_collect_bhs(bhs
, &nr_bhs
, &fatent
);
562 } while (cluster
!= FAT_ENT_EOF
);
564 if (sb
->s_flags
& MS_SYNCHRONOUS
) {
565 err
= fat_sync_bhs(bhs
, nr_bhs
);
569 err
= fat_mirror_bhs(sb
, bhs
, nr_bhs
);
571 fatent_brelse(&fatent
);
572 for (i
= 0; i
< nr_bhs
; i
++)
576 fat_clusters_flush(sb
);
581 EXPORT_SYMBOL(fat_free_clusters
);
583 int fat_count_free_clusters(struct super_block
*sb
)
585 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
586 struct fatent_operations
*ops
= sbi
->fatent_ops
;
587 struct fat_entry fatent
;
591 if (sbi
->free_clusters
!= -1)
595 fatent_init(&fatent
);
596 fatent_set_entry(&fatent
, FAT_START_ENT
);
597 while (fatent
.entry
< sbi
->max_cluster
) {
598 err
= fat_ent_read_block(sb
, &fatent
);
603 if (ops
->ent_get(&fatent
) == FAT_ENT_FREE
)
605 } while (fat_ent_next(sbi
, &fatent
));
607 sbi
->free_clusters
= free
;
608 fatent_brelse(&fatent
);