blob: da6eea47872f448cb2f817e6471c56e0aebbbc1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Woodhouse8c540a92008-08-05 18:05:46 +01009#include <linux/blkdev.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080010#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12struct 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 *,
16 int, sector_t);
17 int (*ent_get)(struct fat_entry *);
18 void (*ent_put)(struct fat_entry *, int);
19 int (*ent_next)(struct fat_entry *);
20};
21
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +090022static DEFINE_SPINLOCK(fat12_entry_lock);
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static void fat12_ent_blocknr(struct super_block *sb, int entry,
25 int *offset, sector_t *blocknr)
26{
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);
32}
33
34static void fat_ent_blocknr(struct super_block *sb, int entry,
35 int *offset, sector_t *blocknr)
36{
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);
42}
43
44static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset)
45{
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);
51 } else {
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;
55 }
56}
57
58static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset)
59{
60 WARN_ON(offset & (2 - 1));
61 fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset);
62}
63
64static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset)
65{
66 WARN_ON(offset & (4 - 1));
67 fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset);
68}
69
70static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent,
71 int offset, sector_t blocknr)
72{
73 struct buffer_head **bhs = fatent->bhs;
74
75 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
76 bhs[0] = sb_bread(sb, blocknr);
77 if (!bhs[0])
78 goto err;
79
80 if ((offset + 1) < sb->s_blocksize)
81 fatent->nr_bhs = 1;
82 else {
83 /* This entry is block boundary, it needs the next block */
84 blocknr++;
85 bhs[1] = sb_bread(sb, blocknr);
86 if (!bhs[1])
87 goto err_brelse;
88 fatent->nr_bhs = 2;
89 }
90 fat12_ent_set_ptr(fatent, offset);
91 return 0;
92
93err_brelse:
94 brelse(bhs[0]);
95err:
OGAWA Hirofumic3302932008-11-06 12:53:58 -080096 printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", (llu)blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EIO;
98}
99
100static 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",
OGAWA Hirofumic3302932008-11-06 12:53:58 -0800109 (llu)blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -EIO;
111 }
112 fatent->nr_bhs = 1;
113 ops->ent_set_ptr(fatent, offset);
114 return 0;
115}
116
117static int fat12_ent_get(struct fat_entry *fatent)
118{
119 u8 **ent12_p = fatent->u.ent12_p;
120 int next;
121
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900122 spin_lock(&fat12_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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];
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900127 spin_unlock(&fat12_entry_lock);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 next &= 0x0fff;
130 if (next >= BAD_FAT12)
131 next = FAT_ENT_EOF;
132 return next;
133}
134
135static 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
144static 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
153static 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
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900160 spin_lock(&fat12_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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 }
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900168 spin_unlock(&fat12_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 mark_buffer_dirty(fatent->bhs[0]);
171 if (fatent->nr_bhs == 2)
172 mark_buffer_dirty(fatent->bhs[1]);
173}
174
175static 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
184static 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
195static 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
225static 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
237static 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
249static 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
258static 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
267static 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
276static inline void lock_fat(struct msdos_sb_info *sbi)
277{
Arjan van de Ven6b9438e2006-03-23 03:00:48 -0800278 mutex_lock(&sbi->fat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static inline void unlock_fat(struct msdos_sb_info *sbi)
282{
Arjan van de Ven6b9438e2006-03-23 03:00:48 -0800283 mutex_unlock(&sbi->fat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286void fat_ent_access_init(struct super_block *sb)
287{
288 struct msdos_sb_info *sbi = MSDOS_SB(sb);
289
Arjan van de Ven6b9438e2006-03-23 03:00:48 -0800290 mutex_init(&sbi->fat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
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
308static 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;
OGAWA Hirofumi5e35dd42008-11-06 12:53:49 -0800319 if (sbi->fat_bits == 12) {
320 if ((offset + 1) < sb->s_blocksize) {
321 /* This entry is on bhs[0]. */
322 if (fatent->nr_bhs == 2) {
323 brelse(bhs[1]);
324 fatent->nr_bhs = 1;
325 }
326 } else {
327 /* This entry needs the next block. */
328 if (fatent->nr_bhs != 2)
329 return 0;
330 if (bhs[1]->b_blocknr != (blocknr + 1))
331 return 0;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 ops->ent_set_ptr(fatent, offset);
335 return 1;
336}
337
338int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
339{
340 struct super_block *sb = inode->i_sb;
341 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
342 struct fatent_operations *ops = sbi->fatent_ops;
343 int err, offset;
344 sector_t blocknr;
345
346 if (entry < FAT_START_ENT || sbi->max_cluster <= entry) {
347 fatent_brelse(fatent);
348 fat_fs_panic(sb, "invalid access to FAT (entry 0x%08x)", entry);
349 return -EIO;
350 }
351
352 fatent_set_entry(fatent, entry);
353 ops->ent_blocknr(sb, entry, &offset, &blocknr);
354
355 if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) {
356 fatent_brelse(fatent);
357 err = ops->ent_bread(sb, fatent, offset, blocknr);
358 if (err)
359 return err;
360 }
361 return ops->ent_get(fatent);
362}
363
364/* FIXME: We can write the blocks as more big chunk. */
365static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
366 int nr_bhs)
367{
368 struct msdos_sb_info *sbi = MSDOS_SB(sb);
369 struct buffer_head *c_bh;
370 int err, n, copy;
371
372 err = 0;
373 for (copy = 1; copy < sbi->fats; copy++) {
374 sector_t backup_fat = sbi->fat_length * copy;
375
376 for (n = 0; n < nr_bhs; n++) {
377 c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr);
378 if (!c_bh) {
379 err = -ENOMEM;
380 goto error;
381 }
382 memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
383 set_buffer_uptodate(c_bh);
384 mark_buffer_dirty(c_bh);
385 if (sb->s_flags & MS_SYNCHRONOUS)
386 err = sync_dirty_buffer(c_bh);
387 brelse(c_bh);
388 if (err)
389 goto error;
390 }
391 }
392error:
393 return err;
394}
395
396int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
397 int new, int wait)
398{
399 struct super_block *sb = inode->i_sb;
400 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
401 int err;
402
403 ops->ent_put(fatent, new);
404 if (wait) {
405 err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
406 if (err)
407 return err;
408 }
409 return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
410}
411
412static inline int fat_ent_next(struct msdos_sb_info *sbi,
413 struct fat_entry *fatent)
414{
415 if (sbi->fatent_ops->ent_next(fatent)) {
416 if (fatent->entry < sbi->max_cluster)
417 return 1;
418 }
419 return 0;
420}
421
422static inline int fat_ent_read_block(struct super_block *sb,
423 struct fat_entry *fatent)
424{
425 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
426 sector_t blocknr;
427 int offset;
428
429 fatent_brelse(fatent);
430 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
431 return ops->ent_bread(sb, fatent, offset, blocknr);
432}
433
434static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
435 struct fat_entry *fatent)
436{
437 int n, i;
438
439 for (n = 0; n < fatent->nr_bhs; n++) {
440 for (i = 0; i < *nr_bhs; i++) {
441 if (fatent->bhs[n] == bhs[i])
442 break;
443 }
444 if (i == *nr_bhs) {
445 get_bh(fatent->bhs[n]);
446 bhs[i] = fatent->bhs[n];
447 (*nr_bhs)++;
448 }
449 }
450}
451
452int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
453{
454 struct super_block *sb = inode->i_sb;
455 struct msdos_sb_info *sbi = MSDOS_SB(sb);
456 struct fatent_operations *ops = sbi->fatent_ops;
457 struct fat_entry fatent, prev_ent;
458 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
459 int i, count, err, nr_bhs, idx_clus;
460
461 BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */
462
463 lock_fat(sbi);
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700464 if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
465 sbi->free_clusters < nr_cluster) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 unlock_fat(sbi);
467 return -ENOSPC;
468 }
469
470 err = nr_bhs = idx_clus = 0;
471 count = FAT_START_ENT;
472 fatent_init(&prev_ent);
473 fatent_init(&fatent);
474 fatent_set_entry(&fatent, sbi->prev_free + 1);
475 while (count < sbi->max_cluster) {
476 if (fatent.entry >= sbi->max_cluster)
477 fatent.entry = FAT_START_ENT;
478 fatent_set_entry(&fatent, fatent.entry);
479 err = fat_ent_read_block(sb, &fatent);
480 if (err)
481 goto out;
482
483 /* Find the free entries in a block */
484 do {
485 if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
486 int entry = fatent.entry;
487
488 /* make the cluster chain */
489 ops->ent_put(&fatent, FAT_ENT_EOF);
490 if (prev_ent.nr_bhs)
491 ops->ent_put(&prev_ent, entry);
492
493 fat_collect_bhs(bhs, &nr_bhs, &fatent);
494
495 sbi->prev_free = entry;
496 if (sbi->free_clusters != -1)
497 sbi->free_clusters--;
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800498 sb->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 cluster[idx_clus] = entry;
501 idx_clus++;
502 if (idx_clus == nr_cluster)
503 goto out;
504
505 /*
506 * fat_collect_bhs() gets ref-count of bhs,
507 * so we can still use the prev_ent.
508 */
509 prev_ent = fatent;
510 }
511 count++;
512 if (count == sbi->max_cluster)
513 break;
514 } while (fat_ent_next(sbi, &fatent));
515 }
516
517 /* Couldn't allocate the free entries */
518 sbi->free_clusters = 0;
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700519 sbi->free_clus_valid = 1;
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800520 sb->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 err = -ENOSPC;
522
523out:
524 unlock_fat(sbi);
525 fatent_brelse(&fatent);
526 if (!err) {
527 if (inode_needs_sync(inode))
528 err = fat_sync_bhs(bhs, nr_bhs);
529 if (!err)
530 err = fat_mirror_bhs(sb, bhs, nr_bhs);
531 }
532 for (i = 0; i < nr_bhs; i++)
533 brelse(bhs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (err && idx_clus)
536 fat_free_clusters(inode, cluster[0]);
537
538 return err;
539}
540
541int fat_free_clusters(struct inode *inode, int cluster)
542{
543 struct super_block *sb = inode->i_sb;
544 struct msdos_sb_info *sbi = MSDOS_SB(sb);
545 struct fatent_operations *ops = sbi->fatent_ops;
546 struct fat_entry fatent;
547 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
548 int i, err, nr_bhs;
David Woodhouse8c540a92008-08-05 18:05:46 +0100549 int first_cl = cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 nr_bhs = 0;
552 fatent_init(&fatent);
553 lock_fat(sbi);
554 do {
555 cluster = fat_ent_read(inode, &fatent, cluster);
556 if (cluster < 0) {
557 err = cluster;
558 goto error;
559 } else if (cluster == FAT_ENT_FREE) {
560 fat_fs_panic(sb, "%s: deleting FAT entry beyond EOF",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700561 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 err = -EIO;
563 goto error;
564 }
565
David Woodhouse8c540a92008-08-05 18:05:46 +0100566 /*
567 * Issue discard for the sectors we no longer care about,
568 * batching contiguous clusters into one request
569 */
570 if (cluster != fatent.entry + 1) {
571 int nr_clus = fatent.entry - first_cl + 1;
572
573 sb_issue_discard(sb, fat_clus_to_blknr(sbi, first_cl),
574 nr_clus * sbi->sec_per_clus);
575 first_cl = cluster;
576 }
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ops->ent_put(&fatent, FAT_ENT_FREE);
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800579 if (sbi->free_clusters != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 sbi->free_clusters++;
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800581 sb->s_dirt = 1;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
585 if (sb->s_flags & MS_SYNCHRONOUS) {
586 err = fat_sync_bhs(bhs, nr_bhs);
587 if (err)
588 goto error;
589 }
590 err = fat_mirror_bhs(sb, bhs, nr_bhs);
591 if (err)
592 goto error;
593 for (i = 0; i < nr_bhs; i++)
594 brelse(bhs[i]);
595 nr_bhs = 0;
596 }
597 fat_collect_bhs(bhs, &nr_bhs, &fatent);
598 } while (cluster != FAT_ENT_EOF);
599
600 if (sb->s_flags & MS_SYNCHRONOUS) {
601 err = fat_sync_bhs(bhs, nr_bhs);
602 if (err)
603 goto error;
604 }
605 err = fat_mirror_bhs(sb, bhs, nr_bhs);
606error:
607 fatent_brelse(&fatent);
608 for (i = 0; i < nr_bhs; i++)
609 brelse(bhs[i]);
610 unlock_fat(sbi);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return err;
613}
614
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800615EXPORT_SYMBOL_GPL(fat_free_clusters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
OGAWA Hirofumi9f966be2008-01-08 15:32:41 -0800617/* 128kb is the whole sectors for FAT12 and FAT16 */
618#define FAT_READA_SIZE (128 * 1024)
619
620static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent,
621 unsigned long reada_blocks)
622{
623 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
624 sector_t blocknr;
625 int i, offset;
626
627 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
628
629 for (i = 0; i < reada_blocks; i++)
630 sb_breadahead(sb, blocknr + i);
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633int fat_count_free_clusters(struct super_block *sb)
634{
635 struct msdos_sb_info *sbi = MSDOS_SB(sb);
636 struct fatent_operations *ops = sbi->fatent_ops;
637 struct fat_entry fatent;
OGAWA Hirofumi9f966be2008-01-08 15:32:41 -0800638 unsigned long reada_blocks, reada_mask, cur_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int err = 0, free;
640
641 lock_fat(sbi);
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700642 if (sbi->free_clusters != -1 && sbi->free_clus_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto out;
644
OGAWA Hirofumi9f966be2008-01-08 15:32:41 -0800645 reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
646 reada_mask = reada_blocks - 1;
647 cur_block = 0;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 free = 0;
650 fatent_init(&fatent);
651 fatent_set_entry(&fatent, FAT_START_ENT);
652 while (fatent.entry < sbi->max_cluster) {
OGAWA Hirofumi9f966be2008-01-08 15:32:41 -0800653 /* readahead of fat blocks */
654 if ((cur_block & reada_mask) == 0) {
655 unsigned long rest = sbi->fat_length - cur_block;
656 fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
657 }
658 cur_block++;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 err = fat_ent_read_block(sb, &fatent);
661 if (err)
662 goto out;
663
664 do {
665 if (ops->ent_get(&fatent) == FAT_ENT_FREE)
666 free++;
667 } while (fat_ent_next(sbi, &fatent));
668 }
669 sbi->free_clusters = free;
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700670 sbi->free_clus_valid = 1;
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800671 sb->s_dirt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 fatent_brelse(&fatent);
673out:
674 unlock_fat(sbi);
675 return err;
676}