Add TASK_WAKEKILL
[linux-2.6/mini2440.git] / fs / fat / fatent.c
blob2c1b73fb82ae29f5a135342c75c2d73fe0d5d931
1 /*
2 * Copyright (C) 2004, OGAWA Hirofumi
3 * Released under GPL v2.
4 */
6 #include <linux/module.h>
7 #include <linux/fs.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 *,
14 int, sector_t);
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);
49 } else {
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);
75 if (!bhs[0])
76 goto err;
78 if ((offset + 1) < sb->s_blocksize)
79 fatent->nr_bhs = 1;
80 else {
81 /* This entry is block boundary, it needs the next block */
82 blocknr++;
83 bhs[1] = sb_bread(sb, blocknr);
84 if (!bhs[1])
85 goto err_brelse;
86 fatent->nr_bhs = 2;
88 fat12_ent_set_ptr(fatent, offset);
89 return 0;
91 err_brelse:
92 brelse(bhs[0]);
93 err:
94 printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n",
95 (unsigned long long)blocknr);
96 return -EIO;
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);
109 return -EIO;
111 fatent->nr_bhs = 1;
112 ops->ent_set_ptr(fatent, offset);
113 return 0;
116 static int fat12_ent_get(struct fat_entry *fatent)
118 u8 **ent12_p = fatent->u.ent12_p;
119 int next;
121 spin_lock(&fat12_entry_lock);
122 if (fatent->entry & 1)
123 next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4);
124 else
125 next = (*ent12_p[1] << 8) | *ent12_p[0];
126 spin_unlock(&fat12_entry_lock);
128 next &= 0x0fff;
129 if (next >= BAD_FAT12)
130 next = FAT_ENT_EOF;
131 return next;
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)
139 next = FAT_ENT_EOF;
140 return next;
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)
148 next = FAT_ENT_EOF;
149 return next;
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)
157 new = EOF_FAT12;
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;
163 } else {
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)
177 new = EOF_FAT16;
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)
186 new = EOF_FAT32;
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);
200 fatent->entry++;
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;
206 ent12_p[1] = nextp;
207 return 1;
209 } else {
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;
213 ent12_p[1] = nextp;
214 brelse(bhs[0]);
215 bhs[0] = bhs[1];
216 fatent->nr_bhs = 1;
217 return 1;
219 ent12_p[0] = NULL;
220 ent12_p[1] = NULL;
221 return 0;
224 static int fat16_ent_next(struct fat_entry *fatent)
226 const struct buffer_head *bh = fatent->bhs[0];
227 fatent->entry++;
228 if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) {
229 fatent->u.ent16_p++;
230 return 1;
232 fatent->u.ent16_p = NULL;
233 return 0;
236 static int fat32_ent_next(struct fat_entry *fatent)
238 const struct buffer_head *bh = fatent->bhs[0];
239 fatent->entry++;
240 if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) {
241 fatent->u.ent32_p++;
242 return 1;
244 fatent->u.ent32_p = NULL;
245 return 0;
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) {
292 case 32:
293 sbi->fatent_shift = 2;
294 sbi->fatent_ops = &fat32_ops;
295 break;
296 case 16:
297 sbi->fatent_shift = 1;
298 sbi->fatent_ops = &fat16_ops;
299 break;
300 case 12:
301 sbi->fatent_shift = -1;
302 sbi->fatent_ops = &fat12_ops;
303 break;
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)
317 return 0;
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))
321 return 0;
323 ops->ent_set_ptr(fatent, offset);
324 return 1;
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;
332 int err, offset;
333 sector_t blocknr;
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);
338 return -EIO;
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);
347 if (err)
348 return err;
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,
355 int nr_bhs)
357 struct msdos_sb_info *sbi = MSDOS_SB(sb);
358 struct buffer_head *c_bh;
359 int err, n, copy;
361 err = 0;
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);
367 if (!c_bh) {
368 err = -ENOMEM;
369 goto error;
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);
376 brelse(c_bh);
377 if (err)
378 goto error;
381 error:
382 return err;
385 int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
386 int new, int wait)
388 struct super_block *sb = inode->i_sb;
389 struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
390 int err;
392 ops->ent_put(fatent, new);
393 if (wait) {
394 err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
395 if (err)
396 return err;
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)
406 return 1;
408 return 0;
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;
415 sector_t blocknr;
416 int offset;
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)
426 int n, i;
428 for (n = 0; n < fatent->nr_bhs; n++) {
429 for (i = 0; i < *nr_bhs; i++) {
430 if (fatent->bhs[n] == bhs[i])
431 break;
433 if (i == *nr_bhs) {
434 get_bh(fatent->bhs[n]);
435 bhs[i] = fatent->bhs[n];
436 (*nr_bhs)++;
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 */
452 lock_fat(sbi);
453 if (sbi->free_clusters != -1 && sbi->free_clusters < nr_cluster) {
454 unlock_fat(sbi);
455 return -ENOSPC;
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);
468 if (err)
469 goto out;
471 /* Find the free entries in a block */
472 do {
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);
478 if (prev_ent.nr_bhs)
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--;
486 sb->s_dirt = 1;
488 cluster[idx_clus] = entry;
489 idx_clus++;
490 if (idx_clus == nr_cluster)
491 goto out;
494 * fat_collect_bhs() gets ref-count of bhs,
495 * so we can still use the prev_ent.
497 prev_ent = fatent;
499 count++;
500 if (count == sbi->max_cluster)
501 break;
502 } while (fat_ent_next(sbi, &fatent));
505 /* Couldn't allocate the free entries */
506 sbi->free_clusters = 0;
507 sb->s_dirt = 1;
508 err = -ENOSPC;
510 out:
511 unlock_fat(sbi);
512 fatent_brelse(&fatent);
513 if (!err) {
514 if (inode_needs_sync(inode))
515 err = fat_sync_bhs(bhs, nr_bhs);
516 if (!err)
517 err = fat_mirror_bhs(sb, bhs, nr_bhs);
519 for (i = 0; i < nr_bhs; i++)
520 brelse(bhs[i]);
522 if (err && idx_clus)
523 fat_free_clusters(inode, cluster[0]);
525 return err;
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];
535 int i, err, nr_bhs;
537 nr_bhs = 0;
538 fatent_init(&fatent);
539 lock_fat(sbi);
540 do {
541 cluster = fat_ent_read(inode, &fatent, cluster);
542 if (cluster < 0) {
543 err = cluster;
544 goto error;
545 } else if (cluster == FAT_ENT_FREE) {
546 fat_fs_panic(sb, "%s: deleting FAT entry beyond EOF",
547 __FUNCTION__);
548 err = -EIO;
549 goto error;
552 ops->ent_put(&fatent, FAT_ENT_FREE);
553 if (sbi->free_clusters != -1) {
554 sbi->free_clusters++;
555 sb->s_dirt = 1;
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);
561 if (err)
562 goto error;
564 err = fat_mirror_bhs(sb, bhs, nr_bhs);
565 if (err)
566 goto error;
567 for (i = 0; i < nr_bhs; i++)
568 brelse(bhs[i]);
569 nr_bhs = 0;
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);
576 if (err)
577 goto error;
579 err = fat_mirror_bhs(sb, bhs, nr_bhs);
580 error:
581 fatent_brelse(&fatent);
582 for (i = 0; i < nr_bhs; i++)
583 brelse(bhs[i]);
584 unlock_fat(sbi);
586 fat_clusters_flush(sb);
588 return err;
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;
598 int err = 0, free;
600 lock_fat(sbi);
601 if (sbi->free_clusters != -1)
602 goto out;
604 free = 0;
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);
609 if (err)
610 goto out;
612 do {
613 if (ops->ent_get(&fatent) == FAT_ENT_FREE)
614 free++;
615 } while (fat_ent_next(sbi, &fatent));
617 sbi->free_clusters = free;
618 sb->s_dirt = 1;
619 fatent_brelse(&fatent);
620 out:
621 unlock_fat(sbi);
622 return err;