blob: e49d27463b671997739b7963ccb3068e1e77a416 [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);
Al Virob5224122009-06-07 13:44:36 -040076 fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 bhs[0] = sb_bread(sb, blocknr);
79 if (!bhs[0])
80 goto err;
81
82 if ((offset + 1) < sb->s_blocksize)
83 fatent->nr_bhs = 1;
84 else {
85 /* This entry is block boundary, it needs the next block */
86 blocknr++;
87 bhs[1] = sb_bread(sb, blocknr);
88 if (!bhs[1])
89 goto err_brelse;
90 fatent->nr_bhs = 2;
91 }
92 fat12_ent_set_ptr(fatent, offset);
93 return 0;
94
95err_brelse:
96 brelse(bhs[0]);
97err:
Alexey Fisher869f58c2011-04-12 21:08:38 +090098 fat_msg(sb, KERN_ERR, "FAT read failed (blocknr %llu)", (llu)blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EIO;
100}
101
102static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent,
103 int offset, sector_t blocknr)
104{
105 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
106
107 WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
Al Virob5224122009-06-07 13:44:36 -0400108 fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 fatent->bhs[0] = sb_bread(sb, blocknr);
110 if (!fatent->bhs[0]) {
Alexey Fisher869f58c2011-04-12 21:08:38 +0900111 fat_msg(sb, KERN_ERR, "FAT read failed (blocknr %llu)",
OGAWA Hirofumic3302932008-11-06 12:53:58 -0800112 (llu)blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -EIO;
114 }
115 fatent->nr_bhs = 1;
116 ops->ent_set_ptr(fatent, offset);
117 return 0;
118}
119
120static int fat12_ent_get(struct fat_entry *fatent)
121{
122 u8 **ent12_p = fatent->u.ent12_p;
123 int next;
124
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900125 spin_lock(&fat12_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (fatent->entry & 1)
127 next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4);
128 else
129 next = (*ent12_p[1] << 8) | *ent12_p[0];
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900130 spin_unlock(&fat12_entry_lock);
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 next &= 0x0fff;
133 if (next >= BAD_FAT12)
134 next = FAT_ENT_EOF;
135 return next;
136}
137
138static int fat16_ent_get(struct fat_entry *fatent)
139{
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)
143 next = FAT_ENT_EOF;
144 return next;
145}
146
147static int fat32_ent_get(struct fat_entry *fatent)
148{
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)
152 next = FAT_ENT_EOF;
153 return next;
154}
155
156static void fat12_ent_put(struct fat_entry *fatent, int new)
157{
158 u8 **ent12_p = fatent->u.ent12_p;
159
160 if (new == FAT_ENT_EOF)
161 new = EOF_FAT12;
162
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900163 spin_lock(&fat12_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (fatent->entry & 1) {
165 *ent12_p[0] = (new << 4) | (*ent12_p[0] & 0x0f);
166 *ent12_p[1] = new >> 4;
167 } else {
168 *ent12_p[0] = new & 0xff;
169 *ent12_p[1] = (*ent12_p[1] & 0xf0) | (new >> 8);
170 }
OGAWA Hirofumi98283bb2007-07-16 09:40:05 +0900171 spin_unlock(&fat12_entry_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Al Virob5224122009-06-07 13:44:36 -0400173 mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (fatent->nr_bhs == 2)
Al Virob5224122009-06-07 13:44:36 -0400175 mark_buffer_dirty_inode(fatent->bhs[1], fatent->fat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static void fat16_ent_put(struct fat_entry *fatent, int new)
179{
180 if (new == FAT_ENT_EOF)
181 new = EOF_FAT16;
182
183 *fatent->u.ent16_p = cpu_to_le16(new);
Al Virob5224122009-06-07 13:44:36 -0400184 mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static void fat32_ent_put(struct fat_entry *fatent, int new)
188{
189 if (new == FAT_ENT_EOF)
190 new = EOF_FAT32;
191
192 WARN_ON(new & 0xf0000000);
193 new |= le32_to_cpu(*fatent->u.ent32_p) & ~0x0fffffff;
194 *fatent->u.ent32_p = cpu_to_le32(new);
Al Virob5224122009-06-07 13:44:36 -0400195 mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static int fat12_ent_next(struct fat_entry *fatent)
199{
200 u8 **ent12_p = fatent->u.ent12_p;
201 struct buffer_head **bhs = fatent->bhs;
202 u8 *nextp = ent12_p[1] + 1 + (fatent->entry & 1);
203
204 fatent->entry++;
205 if (fatent->nr_bhs == 1) {
206 WARN_ON(ent12_p[0] > (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 2)));
207 WARN_ON(ent12_p[1] > (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1)));
208 if (nextp < (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))) {
209 ent12_p[0] = nextp - 1;
210 ent12_p[1] = nextp;
211 return 1;
212 }
213 } else {
214 WARN_ON(ent12_p[0] != (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1)));
215 WARN_ON(ent12_p[1] != (u8 *)bhs[1]->b_data);
216 ent12_p[0] = nextp - 1;
217 ent12_p[1] = nextp;
218 brelse(bhs[0]);
219 bhs[0] = bhs[1];
220 fatent->nr_bhs = 1;
221 return 1;
222 }
223 ent12_p[0] = NULL;
224 ent12_p[1] = NULL;
225 return 0;
226}
227
228static int fat16_ent_next(struct fat_entry *fatent)
229{
230 const struct buffer_head *bh = fatent->bhs[0];
231 fatent->entry++;
232 if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) {
233 fatent->u.ent16_p++;
234 return 1;
235 }
236 fatent->u.ent16_p = NULL;
237 return 0;
238}
239
240static int fat32_ent_next(struct fat_entry *fatent)
241{
242 const struct buffer_head *bh = fatent->bhs[0];
243 fatent->entry++;
244 if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) {
245 fatent->u.ent32_p++;
246 return 1;
247 }
248 fatent->u.ent32_p = NULL;
249 return 0;
250}
251
252static 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,
259};
260
261static 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,
268};
269
270static 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,
277};
278
279static inline void lock_fat(struct msdos_sb_info *sbi)
280{
Arjan van de Ven6b9438e2006-03-23 03:00:48 -0800281 mutex_lock(&sbi->fat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static inline void unlock_fat(struct msdos_sb_info *sbi)
285{
Arjan van de Ven6b9438e2006-03-23 03:00:48 -0800286 mutex_unlock(&sbi->fat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289void fat_ent_access_init(struct super_block *sb)
290{
291 struct msdos_sb_info *sbi = MSDOS_SB(sb);
292
Arjan van de Ven6b9438e2006-03-23 03:00:48 -0800293 mutex_init(&sbi->fat_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 switch (sbi->fat_bits) {
296 case 32:
297 sbi->fatent_shift = 2;
298 sbi->fatent_ops = &fat32_ops;
299 break;
300 case 16:
301 sbi->fatent_shift = 1;
302 sbi->fatent_ops = &fat16_ops;
303 break;
304 case 12:
305 sbi->fatent_shift = -1;
306 sbi->fatent_ops = &fat12_ops;
307 break;
308 }
309}
310
Artem Bityutskiy90b43662012-05-31 16:26:12 -0700311static void mark_fsinfo_dirty(struct super_block *sb)
312{
313 sb->s_dirt = 1;
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static inline int fat_ent_update_ptr(struct super_block *sb,
317 struct fat_entry *fatent,
318 int offset, sector_t blocknr)
319{
320 struct msdos_sb_info *sbi = MSDOS_SB(sb);
321 struct fatent_operations *ops = sbi->fatent_ops;
322 struct buffer_head **bhs = fatent->bhs;
323
324 /* Is this fatent's blocks including this entry? */
325 if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
326 return 0;
OGAWA Hirofumi5e35dd42008-11-06 12:53:49 -0800327 if (sbi->fat_bits == 12) {
328 if ((offset + 1) < sb->s_blocksize) {
329 /* This entry is on bhs[0]. */
330 if (fatent->nr_bhs == 2) {
331 brelse(bhs[1]);
332 fatent->nr_bhs = 1;
333 }
334 } else {
335 /* This entry needs the next block. */
336 if (fatent->nr_bhs != 2)
337 return 0;
338 if (bhs[1]->b_blocknr != (blocknr + 1))
339 return 0;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342 ops->ent_set_ptr(fatent, offset);
343 return 1;
344}
345
346int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
347{
348 struct super_block *sb = inode->i_sb;
349 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
350 struct fatent_operations *ops = sbi->fatent_ops;
351 int err, offset;
352 sector_t blocknr;
353
354 if (entry < FAT_START_ENT || sbi->max_cluster <= entry) {
355 fatent_brelse(fatent);
Denis Karpov85c78592009-06-04 02:34:22 +0900356 fat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EIO;
358 }
359
360 fatent_set_entry(fatent, entry);
361 ops->ent_blocknr(sb, entry, &offset, &blocknr);
362
363 if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) {
364 fatent_brelse(fatent);
365 err = ops->ent_bread(sb, fatent, offset, blocknr);
366 if (err)
367 return err;
368 }
369 return ops->ent_get(fatent);
370}
371
372/* FIXME: We can write the blocks as more big chunk. */
373static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
374 int nr_bhs)
375{
376 struct msdos_sb_info *sbi = MSDOS_SB(sb);
377 struct buffer_head *c_bh;
378 int err, n, copy;
379
380 err = 0;
381 for (copy = 1; copy < sbi->fats; copy++) {
382 sector_t backup_fat = sbi->fat_length * copy;
383
384 for (n = 0; n < nr_bhs; n++) {
385 c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr);
386 if (!c_bh) {
387 err = -ENOMEM;
388 goto error;
389 }
390 memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
391 set_buffer_uptodate(c_bh);
Al Virob5224122009-06-07 13:44:36 -0400392 mark_buffer_dirty_inode(c_bh, sbi->fat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (sb->s_flags & MS_SYNCHRONOUS)
394 err = sync_dirty_buffer(c_bh);
395 brelse(c_bh);
396 if (err)
397 goto error;
398 }
399 }
400error:
401 return err;
402}
403
404int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
405 int new, int wait)
406{
407 struct super_block *sb = inode->i_sb;
408 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
409 int err;
410
411 ops->ent_put(fatent, new);
412 if (wait) {
413 err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
414 if (err)
415 return err;
416 }
417 return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
418}
419
420static inline int fat_ent_next(struct msdos_sb_info *sbi,
421 struct fat_entry *fatent)
422{
423 if (sbi->fatent_ops->ent_next(fatent)) {
424 if (fatent->entry < sbi->max_cluster)
425 return 1;
426 }
427 return 0;
428}
429
430static inline int fat_ent_read_block(struct super_block *sb,
431 struct fat_entry *fatent)
432{
433 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
434 sector_t blocknr;
435 int offset;
436
437 fatent_brelse(fatent);
438 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
439 return ops->ent_bread(sb, fatent, offset, blocknr);
440}
441
442static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
443 struct fat_entry *fatent)
444{
445 int n, i;
446
447 for (n = 0; n < fatent->nr_bhs; n++) {
448 for (i = 0; i < *nr_bhs; i++) {
449 if (fatent->bhs[n] == bhs[i])
450 break;
451 }
452 if (i == *nr_bhs) {
453 get_bh(fatent->bhs[n]);
454 bhs[i] = fatent->bhs[n];
455 (*nr_bhs)++;
456 }
457 }
458}
459
460int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
461{
462 struct super_block *sb = inode->i_sb;
463 struct msdos_sb_info *sbi = MSDOS_SB(sb);
464 struct fatent_operations *ops = sbi->fatent_ops;
465 struct fat_entry fatent, prev_ent;
466 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
467 int i, count, err, nr_bhs, idx_clus;
468
469 BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */
470
471 lock_fat(sbi);
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700472 if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
473 sbi->free_clusters < nr_cluster) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 unlock_fat(sbi);
475 return -ENOSPC;
476 }
477
478 err = nr_bhs = idx_clus = 0;
479 count = FAT_START_ENT;
480 fatent_init(&prev_ent);
481 fatent_init(&fatent);
482 fatent_set_entry(&fatent, sbi->prev_free + 1);
483 while (count < sbi->max_cluster) {
484 if (fatent.entry >= sbi->max_cluster)
485 fatent.entry = FAT_START_ENT;
486 fatent_set_entry(&fatent, fatent.entry);
487 err = fat_ent_read_block(sb, &fatent);
488 if (err)
489 goto out;
490
491 /* Find the free entries in a block */
492 do {
493 if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
494 int entry = fatent.entry;
495
496 /* make the cluster chain */
497 ops->ent_put(&fatent, FAT_ENT_EOF);
498 if (prev_ent.nr_bhs)
499 ops->ent_put(&prev_ent, entry);
500
501 fat_collect_bhs(bhs, &nr_bhs, &fatent);
502
503 sbi->prev_free = entry;
504 if (sbi->free_clusters != -1)
505 sbi->free_clusters--;
Artem Bityutskiy90b43662012-05-31 16:26:12 -0700506 mark_fsinfo_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 cluster[idx_clus] = entry;
509 idx_clus++;
510 if (idx_clus == nr_cluster)
511 goto out;
512
513 /*
514 * fat_collect_bhs() gets ref-count of bhs,
515 * so we can still use the prev_ent.
516 */
517 prev_ent = fatent;
518 }
519 count++;
520 if (count == sbi->max_cluster)
521 break;
522 } while (fat_ent_next(sbi, &fatent));
523 }
524
525 /* Couldn't allocate the free entries */
526 sbi->free_clusters = 0;
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700527 sbi->free_clus_valid = 1;
Artem Bityutskiy90b43662012-05-31 16:26:12 -0700528 mark_fsinfo_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 err = -ENOSPC;
530
531out:
532 unlock_fat(sbi);
533 fatent_brelse(&fatent);
534 if (!err) {
535 if (inode_needs_sync(inode))
536 err = fat_sync_bhs(bhs, nr_bhs);
537 if (!err)
538 err = fat_mirror_bhs(sb, bhs, nr_bhs);
539 }
540 for (i = 0; i < nr_bhs; i++)
541 brelse(bhs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (err && idx_clus)
544 fat_free_clusters(inode, cluster[0]);
545
546 return err;
547}
548
549int fat_free_clusters(struct inode *inode, int cluster)
550{
551 struct super_block *sb = inode->i_sb;
552 struct msdos_sb_info *sbi = MSDOS_SB(sb);
553 struct fatent_operations *ops = sbi->fatent_ops;
554 struct fat_entry fatent;
555 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
556 int i, err, nr_bhs;
David Woodhouse8c540a92008-08-05 18:05:46 +0100557 int first_cl = cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 nr_bhs = 0;
560 fatent_init(&fatent);
561 lock_fat(sbi);
562 do {
563 cluster = fat_ent_read(inode, &fatent, cluster);
564 if (cluster < 0) {
565 err = cluster;
566 goto error;
567 } else if (cluster == FAT_ENT_FREE) {
Denis Karpov85c78592009-06-04 02:34:22 +0900568 fat_fs_error(sb, "%s: deleting FAT entry beyond EOF",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700569 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 err = -EIO;
571 goto error;
572 }
573
Christoph Hellwig681142f2009-11-21 20:28:52 +0900574 if (sbi->options.discard) {
575 /*
576 * Issue discard for the sectors we no longer
577 * care about, batching contiguous clusters
578 * into one request
579 */
580 if (cluster != fatent.entry + 1) {
581 int nr_clus = fatent.entry - first_cl + 1;
David Woodhouse8c540a92008-08-05 18:05:46 +0100582
Christoph Hellwig681142f2009-11-21 20:28:52 +0900583 sb_issue_discard(sb,
584 fat_clus_to_blknr(sbi, first_cl),
Christoph Hellwig2cf6d262010-08-18 05:29:10 -0400585 nr_clus * sbi->sec_per_clus,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200586 GFP_NOFS, 0);
Christoph Hellwig681142f2009-11-21 20:28:52 +0900587
588 first_cl = cluster;
589 }
David Woodhouse8c540a92008-08-05 18:05:46 +0100590 }
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ops->ent_put(&fatent, FAT_ENT_FREE);
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800593 if (sbi->free_clusters != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 sbi->free_clusters++;
Artem Bityutskiy90b43662012-05-31 16:26:12 -0700595 mark_fsinfo_dirty(sb);
OGAWA Hirofumia6bf6b22006-01-08 01:02:08 -0800596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
599 if (sb->s_flags & MS_SYNCHRONOUS) {
600 err = fat_sync_bhs(bhs, nr_bhs);
601 if (err)
602 goto error;
603 }
604 err = fat_mirror_bhs(sb, bhs, nr_bhs);
605 if (err)
606 goto error;
607 for (i = 0; i < nr_bhs; i++)
608 brelse(bhs[i]);
609 nr_bhs = 0;
610 }
611 fat_collect_bhs(bhs, &nr_bhs, &fatent);
612 } while (cluster != FAT_ENT_EOF);
613
614 if (sb->s_flags & MS_SYNCHRONOUS) {
615 err = fat_sync_bhs(bhs, nr_bhs);
616 if (err)
617 goto error;
618 }
619 err = fat_mirror_bhs(sb, bhs, nr_bhs);
620error:
621 fatent_brelse(&fatent);
622 for (i = 0; i < nr_bhs; i++)
623 brelse(bhs[i]);
624 unlock_fat(sbi);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return err;
627}
628
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800629EXPORT_SYMBOL_GPL(fat_free_clusters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
OGAWA Hirofumi9f966be8992008-01-08 15:32:41 -0800631/* 128kb is the whole sectors for FAT12 and FAT16 */
632#define FAT_READA_SIZE (128 * 1024)
633
634static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent,
635 unsigned long reada_blocks)
636{
637 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
638 sector_t blocknr;
639 int i, offset;
640
641 ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
642
643 for (i = 0; i < reada_blocks; i++)
644 sb_breadahead(sb, blocknr + i);
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647int fat_count_free_clusters(struct super_block *sb)
648{
649 struct msdos_sb_info *sbi = MSDOS_SB(sb);
650 struct fatent_operations *ops = sbi->fatent_ops;
651 struct fat_entry fatent;
OGAWA Hirofumi9f966be8992008-01-08 15:32:41 -0800652 unsigned long reada_blocks, reada_mask, cur_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int err = 0, free;
654
655 lock_fat(sbi);
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700656 if (sbi->free_clusters != -1 && sbi->free_clus_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto out;
658
OGAWA Hirofumi9f966be8992008-01-08 15:32:41 -0800659 reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
660 reada_mask = reada_blocks - 1;
661 cur_block = 0;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 free = 0;
664 fatent_init(&fatent);
665 fatent_set_entry(&fatent, FAT_START_ENT);
666 while (fatent.entry < sbi->max_cluster) {
OGAWA Hirofumi9f966be8992008-01-08 15:32:41 -0800667 /* readahead of fat blocks */
668 if ((cur_block & reada_mask) == 0) {
669 unsigned long rest = sbi->fat_length - cur_block;
670 fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
671 }
672 cur_block++;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 err = fat_ent_read_block(sb, &fatent);
675 if (err)
676 goto out;
677
678 do {
679 if (ops->ent_get(&fatent) == FAT_ENT_FREE)
680 free++;
681 } while (fat_ent_next(sbi, &fatent));
682 }
683 sbi->free_clusters = free;
OGAWA Hirofumi606e4232008-04-28 02:16:27 -0700684 sbi->free_clus_valid = 1;
Artem Bityutskiy90b43662012-05-31 16:26:12 -0700685 mark_fsinfo_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 fatent_brelse(&fatent);
687out:
688 unlock_fat(sbi);
689 return err;
690}