Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Woodhouse | 8c540a9 | 2008-08-05 18:05:46 +0100 | [diff] [blame] | 9 | #include <linux/blkdev.h> |
OGAWA Hirofumi | 9e975da | 2008-11-06 12:53:46 -0800 | [diff] [blame] | 10 | #include "fat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | struct 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 Hirofumi | 98283bb | 2007-07-16 09:40:05 +0900 | [diff] [blame] | 22 | static DEFINE_SPINLOCK(fat12_entry_lock); |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static 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 | |
| 34 | static 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 | |
| 44 | static 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 | |
| 58 | static 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 | |
| 64 | static 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 | |
| 70 | static 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 Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 76 | fatent->fat_inode = MSDOS_SB(sb)->fat_inode; |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 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 | |
| 95 | err_brelse: |
| 96 | brelse(bhs[0]); |
| 97 | err: |
OGAWA Hirofumi | c330293 | 2008-11-06 12:53:58 -0800 | [diff] [blame] | 98 | printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", (llu)blocknr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return -EIO; |
| 100 | } |
| 101 | |
| 102 | static 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 Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 108 | fatent->fat_inode = MSDOS_SB(sb)->fat_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | fatent->bhs[0] = sb_bread(sb, blocknr); |
| 110 | if (!fatent->bhs[0]) { |
| 111 | printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", |
OGAWA Hirofumi | c330293 | 2008-11-06 12:53:58 -0800 | [diff] [blame] | 112 | (llu)blocknr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return -EIO; |
| 114 | } |
| 115 | fatent->nr_bhs = 1; |
| 116 | ops->ent_set_ptr(fatent, offset); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int fat12_ent_get(struct fat_entry *fatent) |
| 121 | { |
| 122 | u8 **ent12_p = fatent->u.ent12_p; |
| 123 | int next; |
| 124 | |
OGAWA Hirofumi | 98283bb | 2007-07-16 09:40:05 +0900 | [diff] [blame] | 125 | spin_lock(&fat12_entry_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 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 Hirofumi | 98283bb | 2007-07-16 09:40:05 +0900 | [diff] [blame] | 130 | spin_unlock(&fat12_entry_lock); |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | next &= 0x0fff; |
| 133 | if (next >= BAD_FAT12) |
| 134 | next = FAT_ENT_EOF; |
| 135 | return next; |
| 136 | } |
| 137 | |
| 138 | static 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 | |
| 147 | static 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 | |
| 156 | static 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 Hirofumi | 98283bb | 2007-07-16 09:40:05 +0900 | [diff] [blame] | 163 | spin_lock(&fat12_entry_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | 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 Hirofumi | 98283bb | 2007-07-16 09:40:05 +0900 | [diff] [blame] | 171 | spin_unlock(&fat12_entry_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 173 | mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | if (fatent->nr_bhs == 2) |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 175 | mark_buffer_dirty_inode(fatent->bhs[1], fatent->fat_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static 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 Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 184 | mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static 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 Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 195 | mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static 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 | |
| 228 | static 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 | |
| 240 | static 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 | |
| 252 | static 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 | |
| 261 | static 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 | |
| 270 | static 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 | |
| 279 | static inline void lock_fat(struct msdos_sb_info *sbi) |
| 280 | { |
Arjan van de Ven | 6b9438e | 2006-03-23 03:00:48 -0800 | [diff] [blame] | 281 | mutex_lock(&sbi->fat_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static inline void unlock_fat(struct msdos_sb_info *sbi) |
| 285 | { |
Arjan van de Ven | 6b9438e | 2006-03-23 03:00:48 -0800 | [diff] [blame] | 286 | mutex_unlock(&sbi->fat_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void fat_ent_access_init(struct super_block *sb) |
| 290 | { |
| 291 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 292 | |
Arjan van de Ven | 6b9438e | 2006-03-23 03:00:48 -0800 | [diff] [blame] | 293 | mutex_init(&sbi->fat_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 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 | |
| 311 | static inline int fat_ent_update_ptr(struct super_block *sb, |
| 312 | struct fat_entry *fatent, |
| 313 | int offset, sector_t blocknr) |
| 314 | { |
| 315 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 316 | struct fatent_operations *ops = sbi->fatent_ops; |
| 317 | struct buffer_head **bhs = fatent->bhs; |
| 318 | |
| 319 | /* Is this fatent's blocks including this entry? */ |
| 320 | if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr) |
| 321 | return 0; |
OGAWA Hirofumi | 5e35dd4 | 2008-11-06 12:53:49 -0800 | [diff] [blame] | 322 | if (sbi->fat_bits == 12) { |
| 323 | if ((offset + 1) < sb->s_blocksize) { |
| 324 | /* This entry is on bhs[0]. */ |
| 325 | if (fatent->nr_bhs == 2) { |
| 326 | brelse(bhs[1]); |
| 327 | fatent->nr_bhs = 1; |
| 328 | } |
| 329 | } else { |
| 330 | /* This entry needs the next block. */ |
| 331 | if (fatent->nr_bhs != 2) |
| 332 | return 0; |
| 333 | if (bhs[1]->b_blocknr != (blocknr + 1)) |
| 334 | return 0; |
| 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | ops->ent_set_ptr(fatent, offset); |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry) |
| 342 | { |
| 343 | struct super_block *sb = inode->i_sb; |
| 344 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
| 345 | struct fatent_operations *ops = sbi->fatent_ops; |
| 346 | int err, offset; |
| 347 | sector_t blocknr; |
| 348 | |
| 349 | if (entry < FAT_START_ENT || sbi->max_cluster <= entry) { |
| 350 | fatent_brelse(fatent); |
Denis Karpov | 85c7859 | 2009-06-04 02:34:22 +0900 | [diff] [blame] | 351 | fat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -EIO; |
| 353 | } |
| 354 | |
| 355 | fatent_set_entry(fatent, entry); |
| 356 | ops->ent_blocknr(sb, entry, &offset, &blocknr); |
| 357 | |
| 358 | if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) { |
| 359 | fatent_brelse(fatent); |
| 360 | err = ops->ent_bread(sb, fatent, offset, blocknr); |
| 361 | if (err) |
| 362 | return err; |
| 363 | } |
| 364 | return ops->ent_get(fatent); |
| 365 | } |
| 366 | |
| 367 | /* FIXME: We can write the blocks as more big chunk. */ |
| 368 | static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs, |
| 369 | int nr_bhs) |
| 370 | { |
| 371 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 372 | struct buffer_head *c_bh; |
| 373 | int err, n, copy; |
| 374 | |
| 375 | err = 0; |
| 376 | for (copy = 1; copy < sbi->fats; copy++) { |
| 377 | sector_t backup_fat = sbi->fat_length * copy; |
| 378 | |
| 379 | for (n = 0; n < nr_bhs; n++) { |
| 380 | c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr); |
| 381 | if (!c_bh) { |
| 382 | err = -ENOMEM; |
| 383 | goto error; |
| 384 | } |
| 385 | memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize); |
| 386 | set_buffer_uptodate(c_bh); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 387 | mark_buffer_dirty_inode(c_bh, sbi->fat_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | if (sb->s_flags & MS_SYNCHRONOUS) |
| 389 | err = sync_dirty_buffer(c_bh); |
| 390 | brelse(c_bh); |
| 391 | if (err) |
| 392 | goto error; |
| 393 | } |
| 394 | } |
| 395 | error: |
| 396 | return err; |
| 397 | } |
| 398 | |
| 399 | int fat_ent_write(struct inode *inode, struct fat_entry *fatent, |
| 400 | int new, int wait) |
| 401 | { |
| 402 | struct super_block *sb = inode->i_sb; |
| 403 | struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; |
| 404 | int err; |
| 405 | |
| 406 | ops->ent_put(fatent, new); |
| 407 | if (wait) { |
| 408 | err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs); |
| 409 | if (err) |
| 410 | return err; |
| 411 | } |
| 412 | return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs); |
| 413 | } |
| 414 | |
| 415 | static inline int fat_ent_next(struct msdos_sb_info *sbi, |
| 416 | struct fat_entry *fatent) |
| 417 | { |
| 418 | if (sbi->fatent_ops->ent_next(fatent)) { |
| 419 | if (fatent->entry < sbi->max_cluster) |
| 420 | return 1; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static inline int fat_ent_read_block(struct super_block *sb, |
| 426 | struct fat_entry *fatent) |
| 427 | { |
| 428 | struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; |
| 429 | sector_t blocknr; |
| 430 | int offset; |
| 431 | |
| 432 | fatent_brelse(fatent); |
| 433 | ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr); |
| 434 | return ops->ent_bread(sb, fatent, offset, blocknr); |
| 435 | } |
| 436 | |
| 437 | static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs, |
| 438 | struct fat_entry *fatent) |
| 439 | { |
| 440 | int n, i; |
| 441 | |
| 442 | for (n = 0; n < fatent->nr_bhs; n++) { |
| 443 | for (i = 0; i < *nr_bhs; i++) { |
| 444 | if (fatent->bhs[n] == bhs[i]) |
| 445 | break; |
| 446 | } |
| 447 | if (i == *nr_bhs) { |
| 448 | get_bh(fatent->bhs[n]); |
| 449 | bhs[i] = fatent->bhs[n]; |
| 450 | (*nr_bhs)++; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster) |
| 456 | { |
| 457 | struct super_block *sb = inode->i_sb; |
| 458 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 459 | struct fatent_operations *ops = sbi->fatent_ops; |
| 460 | struct fat_entry fatent, prev_ent; |
| 461 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
| 462 | int i, count, err, nr_bhs, idx_clus; |
| 463 | |
| 464 | BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */ |
| 465 | |
| 466 | lock_fat(sbi); |
OGAWA Hirofumi | 606e423 | 2008-04-28 02:16:27 -0700 | [diff] [blame] | 467 | if (sbi->free_clusters != -1 && sbi->free_clus_valid && |
| 468 | sbi->free_clusters < nr_cluster) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | unlock_fat(sbi); |
| 470 | return -ENOSPC; |
| 471 | } |
| 472 | |
| 473 | err = nr_bhs = idx_clus = 0; |
| 474 | count = FAT_START_ENT; |
| 475 | fatent_init(&prev_ent); |
| 476 | fatent_init(&fatent); |
| 477 | fatent_set_entry(&fatent, sbi->prev_free + 1); |
| 478 | while (count < sbi->max_cluster) { |
| 479 | if (fatent.entry >= sbi->max_cluster) |
| 480 | fatent.entry = FAT_START_ENT; |
| 481 | fatent_set_entry(&fatent, fatent.entry); |
| 482 | err = fat_ent_read_block(sb, &fatent); |
| 483 | if (err) |
| 484 | goto out; |
| 485 | |
| 486 | /* Find the free entries in a block */ |
| 487 | do { |
| 488 | if (ops->ent_get(&fatent) == FAT_ENT_FREE) { |
| 489 | int entry = fatent.entry; |
| 490 | |
| 491 | /* make the cluster chain */ |
| 492 | ops->ent_put(&fatent, FAT_ENT_EOF); |
| 493 | if (prev_ent.nr_bhs) |
| 494 | ops->ent_put(&prev_ent, entry); |
| 495 | |
| 496 | fat_collect_bhs(bhs, &nr_bhs, &fatent); |
| 497 | |
| 498 | sbi->prev_free = entry; |
| 499 | if (sbi->free_clusters != -1) |
| 500 | sbi->free_clusters--; |
OGAWA Hirofumi | a6bf6b2 | 2006-01-08 01:02:08 -0800 | [diff] [blame] | 501 | sb->s_dirt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | cluster[idx_clus] = entry; |
| 504 | idx_clus++; |
| 505 | if (idx_clus == nr_cluster) |
| 506 | goto out; |
| 507 | |
| 508 | /* |
| 509 | * fat_collect_bhs() gets ref-count of bhs, |
| 510 | * so we can still use the prev_ent. |
| 511 | */ |
| 512 | prev_ent = fatent; |
| 513 | } |
| 514 | count++; |
| 515 | if (count == sbi->max_cluster) |
| 516 | break; |
| 517 | } while (fat_ent_next(sbi, &fatent)); |
| 518 | } |
| 519 | |
| 520 | /* Couldn't allocate the free entries */ |
| 521 | sbi->free_clusters = 0; |
OGAWA Hirofumi | 606e423 | 2008-04-28 02:16:27 -0700 | [diff] [blame] | 522 | sbi->free_clus_valid = 1; |
OGAWA Hirofumi | a6bf6b2 | 2006-01-08 01:02:08 -0800 | [diff] [blame] | 523 | sb->s_dirt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | err = -ENOSPC; |
| 525 | |
| 526 | out: |
| 527 | unlock_fat(sbi); |
| 528 | fatent_brelse(&fatent); |
| 529 | if (!err) { |
| 530 | if (inode_needs_sync(inode)) |
| 531 | err = fat_sync_bhs(bhs, nr_bhs); |
| 532 | if (!err) |
| 533 | err = fat_mirror_bhs(sb, bhs, nr_bhs); |
| 534 | } |
| 535 | for (i = 0; i < nr_bhs; i++) |
| 536 | brelse(bhs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | if (err && idx_clus) |
| 539 | fat_free_clusters(inode, cluster[0]); |
| 540 | |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | int fat_free_clusters(struct inode *inode, int cluster) |
| 545 | { |
| 546 | struct super_block *sb = inode->i_sb; |
| 547 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 548 | struct fatent_operations *ops = sbi->fatent_ops; |
| 549 | struct fat_entry fatent; |
| 550 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
| 551 | int i, err, nr_bhs; |
David Woodhouse | 8c540a9 | 2008-08-05 18:05:46 +0100 | [diff] [blame] | 552 | int first_cl = cluster; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | nr_bhs = 0; |
| 555 | fatent_init(&fatent); |
| 556 | lock_fat(sbi); |
| 557 | do { |
| 558 | cluster = fat_ent_read(inode, &fatent, cluster); |
| 559 | if (cluster < 0) { |
| 560 | err = cluster; |
| 561 | goto error; |
| 562 | } else if (cluster == FAT_ENT_FREE) { |
Denis Karpov | 85c7859 | 2009-06-04 02:34:22 +0900 | [diff] [blame] | 563 | fat_fs_error(sb, "%s: deleting FAT entry beyond EOF", |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 564 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | err = -EIO; |
| 566 | goto error; |
| 567 | } |
| 568 | |
Christoph Hellwig | 681142f | 2009-11-21 20:28:52 +0900 | [diff] [blame] | 569 | if (sbi->options.discard) { |
| 570 | /* |
| 571 | * Issue discard for the sectors we no longer |
| 572 | * care about, batching contiguous clusters |
| 573 | * into one request |
| 574 | */ |
| 575 | if (cluster != fatent.entry + 1) { |
| 576 | int nr_clus = fatent.entry - first_cl + 1; |
David Woodhouse | 8c540a9 | 2008-08-05 18:05:46 +0100 | [diff] [blame] | 577 | |
Christoph Hellwig | 681142f | 2009-11-21 20:28:52 +0900 | [diff] [blame] | 578 | sb_issue_discard(sb, |
| 579 | fat_clus_to_blknr(sbi, first_cl), |
| 580 | nr_clus * sbi->sec_per_clus); |
| 581 | |
| 582 | first_cl = cluster; |
| 583 | } |
David Woodhouse | 8c540a9 | 2008-08-05 18:05:46 +0100 | [diff] [blame] | 584 | } |
| 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | ops->ent_put(&fatent, FAT_ENT_FREE); |
OGAWA Hirofumi | a6bf6b2 | 2006-01-08 01:02:08 -0800 | [diff] [blame] | 587 | if (sbi->free_clusters != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | sbi->free_clusters++; |
OGAWA Hirofumi | a6bf6b2 | 2006-01-08 01:02:08 -0800 | [diff] [blame] | 589 | sb->s_dirt = 1; |
| 590 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) { |
| 593 | if (sb->s_flags & MS_SYNCHRONOUS) { |
| 594 | err = fat_sync_bhs(bhs, nr_bhs); |
| 595 | if (err) |
| 596 | goto error; |
| 597 | } |
| 598 | err = fat_mirror_bhs(sb, bhs, nr_bhs); |
| 599 | if (err) |
| 600 | goto error; |
| 601 | for (i = 0; i < nr_bhs; i++) |
| 602 | brelse(bhs[i]); |
| 603 | nr_bhs = 0; |
| 604 | } |
| 605 | fat_collect_bhs(bhs, &nr_bhs, &fatent); |
| 606 | } while (cluster != FAT_ENT_EOF); |
| 607 | |
| 608 | if (sb->s_flags & MS_SYNCHRONOUS) { |
| 609 | err = fat_sync_bhs(bhs, nr_bhs); |
| 610 | if (err) |
| 611 | goto error; |
| 612 | } |
| 613 | err = fat_mirror_bhs(sb, bhs, nr_bhs); |
| 614 | error: |
| 615 | fatent_brelse(&fatent); |
| 616 | for (i = 0; i < nr_bhs; i++) |
| 617 | brelse(bhs[i]); |
| 618 | unlock_fat(sbi); |
| 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return err; |
| 621 | } |
| 622 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 623 | EXPORT_SYMBOL_GPL(fat_free_clusters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
OGAWA Hirofumi | 9f966be899 | 2008-01-08 15:32:41 -0800 | [diff] [blame] | 625 | /* 128kb is the whole sectors for FAT12 and FAT16 */ |
| 626 | #define FAT_READA_SIZE (128 * 1024) |
| 627 | |
| 628 | static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent, |
| 629 | unsigned long reada_blocks) |
| 630 | { |
| 631 | struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; |
| 632 | sector_t blocknr; |
| 633 | int i, offset; |
| 634 | |
| 635 | ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr); |
| 636 | |
| 637 | for (i = 0; i < reada_blocks; i++) |
| 638 | sb_breadahead(sb, blocknr + i); |
| 639 | } |
| 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | int fat_count_free_clusters(struct super_block *sb) |
| 642 | { |
| 643 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 644 | struct fatent_operations *ops = sbi->fatent_ops; |
| 645 | struct fat_entry fatent; |
OGAWA Hirofumi | 9f966be899 | 2008-01-08 15:32:41 -0800 | [diff] [blame] | 646 | unsigned long reada_blocks, reada_mask, cur_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | int err = 0, free; |
| 648 | |
| 649 | lock_fat(sbi); |
OGAWA Hirofumi | 606e423 | 2008-04-28 02:16:27 -0700 | [diff] [blame] | 650 | if (sbi->free_clusters != -1 && sbi->free_clus_valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | goto out; |
| 652 | |
OGAWA Hirofumi | 9f966be899 | 2008-01-08 15:32:41 -0800 | [diff] [blame] | 653 | reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits; |
| 654 | reada_mask = reada_blocks - 1; |
| 655 | cur_block = 0; |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | free = 0; |
| 658 | fatent_init(&fatent); |
| 659 | fatent_set_entry(&fatent, FAT_START_ENT); |
| 660 | while (fatent.entry < sbi->max_cluster) { |
OGAWA Hirofumi | 9f966be899 | 2008-01-08 15:32:41 -0800 | [diff] [blame] | 661 | /* readahead of fat blocks */ |
| 662 | if ((cur_block & reada_mask) == 0) { |
| 663 | unsigned long rest = sbi->fat_length - cur_block; |
| 664 | fat_ent_reada(sb, &fatent, min(reada_blocks, rest)); |
| 665 | } |
| 666 | cur_block++; |
| 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | err = fat_ent_read_block(sb, &fatent); |
| 669 | if (err) |
| 670 | goto out; |
| 671 | |
| 672 | do { |
| 673 | if (ops->ent_get(&fatent) == FAT_ENT_FREE) |
| 674 | free++; |
| 675 | } while (fat_ent_next(sbi, &fatent)); |
| 676 | } |
| 677 | sbi->free_clusters = free; |
OGAWA Hirofumi | 606e423 | 2008-04-28 02:16:27 -0700 | [diff] [blame] | 678 | sbi->free_clus_valid = 1; |
OGAWA Hirofumi | a6bf6b2 | 2006-01-08 01:02:08 -0800 | [diff] [blame] | 679 | sb->s_dirt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | fatent_brelse(&fatent); |
| 681 | out: |
| 682 | unlock_fat(sbi); |
| 683 | return err; |
| 684 | } |