123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- /*
- * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
- */
- /************************************************************************/
- /* */
- /* PROJECT : exFAT & FAT12/16/32 File System */
- /* FILE : fatent.c */
- /* PURPOSE : sdFAT FAT entry manager */
- /* */
- /*----------------------------------------------------------------------*/
- /* NOTES */
- /* */
- /* */
- /************************************************************************/
- #include <asm/unaligned.h>
- #include "sdfat.h"
- #include "core.h"
- /*----------------------------------------------------------------------*/
- /* Global Variable Definitions */
- /*----------------------------------------------------------------------*/
- /* All buffer structures are protected w/ fsi->v_sem */
- /*----------------------------------------------------------------------*/
- /* Static functions */
- /*----------------------------------------------------------------------*/
- /*======================================================================*/
- /* FAT Read/Write Functions */
- /*======================================================================*/
- /* in : sb, loc
- * out: content
- * returns 0 on success, -1 on error
- */
- static s32 exfat_ent_get(struct super_block *sb, u32 loc, u32 *content)
- {
- u32 off, _content;
- u64 sec;
- u8 *fat_sector;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- /* fsi->vol_type == EXFAT */
- sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
- off = (loc << 2) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- _content = le32_to_cpu(*(__le32 *)(&fat_sector[off]));
- /* remap reserved clusters to simplify code */
- if (_content >= CLUSTER_32(0xFFFFFFF8))
- _content = CLUS_EOF;
- *content = CLUSTER_32(_content);
- return 0;
- }
- static s32 exfat_ent_set(struct super_block *sb, u32 loc, u32 content)
- {
- u32 off;
- u64 sec;
- u8 *fat_sector;
- __le32 *fat_entry;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
- off = (loc << 2) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- fat_entry = (__le32 *)&(fat_sector[off]);
- *fat_entry = cpu_to_le32(content);
- return fcache_modify(sb, sec);
- }
- #define FATENT_FAT32_VALID_MASK (0x0FFFFFFFU)
- #define FATENT_FAT32_IGNORE_MASK (0xF0000000U)
- static s32 fat32_ent_get(struct super_block *sb, u32 loc, u32 *content)
- {
- u32 off, _content;
- u64 sec;
- u8 *fat_sector;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
- off = (loc << 2) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- _content = le32_to_cpu(*(__le32 *)(&fat_sector[off]));
- _content &= FATENT_FAT32_VALID_MASK;
- /* remap reserved clusters to simplify code */
- if (_content == CLUSTER_32(0x0FFFFFF7U))
- _content = CLUS_BAD;
- else if (_content >= CLUSTER_32(0x0FFFFFF8U))
- _content = CLUS_EOF;
- *content = CLUSTER_32(_content);
- return 0;
- }
- static s32 fat32_ent_set(struct super_block *sb, u32 loc, u32 content)
- {
- u32 off;
- u64 sec;
- u8 *fat_sector;
- __le32 *fat_entry;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- content &= FATENT_FAT32_VALID_MASK;
- sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-2));
- off = (loc << 2) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- fat_entry = (__le32 *)&(fat_sector[off]);
- content |= (le32_to_cpu(*fat_entry) & FATENT_FAT32_IGNORE_MASK);
- *fat_entry = cpu_to_le32(content);
- return fcache_modify(sb, sec);
- }
- #define FATENT_FAT16_VALID_MASK (0x0000FFFFU)
- static s32 fat16_ent_get(struct super_block *sb, u32 loc, u32 *content)
- {
- u32 off, _content;
- u64 sec;
- u8 *fat_sector;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-1));
- off = (loc << 1) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- _content = (u32)le16_to_cpu(*(__le16 *)(&fat_sector[off]));
- _content &= FATENT_FAT16_VALID_MASK;
- /* remap reserved clusters to simplify code */
- if (_content == CLUSTER_16(0xFFF7U))
- _content = CLUS_BAD;
- else if (_content >= CLUSTER_16(0xFFF8U))
- _content = CLUS_EOF;
- *content = CLUSTER_32(_content);
- return 0;
- }
- static s32 fat16_ent_set(struct super_block *sb, u32 loc, u32 content)
- {
- u32 off;
- u64 sec;
- u8 *fat_sector;
- __le16 *fat_entry;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- content &= FATENT_FAT16_VALID_MASK;
- sec = fsi->FAT1_start_sector + (loc >> (sb->s_blocksize_bits-1));
- off = (loc << 1) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- fat_entry = (__le16 *)&(fat_sector[off]);
- *fat_entry = cpu_to_le16(content);
- return fcache_modify(sb, sec);
- }
- #define FATENT_FAT12_VALID_MASK (0x00000FFFU)
- static s32 fat12_ent_get(struct super_block *sb, u32 loc, u32 *content)
- {
- u32 off, _content;
- u64 sec;
- u8 *fat_sector;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- sec = fsi->FAT1_start_sector + ((loc + (loc >> 1)) >> sb->s_blocksize_bits);
- off = (loc + (loc >> 1)) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- if (off == (u32)(sb->s_blocksize - 1)) {
- _content = (u32) fat_sector[off];
- fat_sector = fcache_getblk(sb, ++sec);
- if (!fat_sector)
- return -EIO;
- _content |= (u32) fat_sector[0] << 8;
- } else {
- _content = get_unaligned_le16(&fat_sector[off]);
- }
- if (loc & 1)
- _content >>= 4;
- _content &= FATENT_FAT12_VALID_MASK;
- /* remap reserved clusters to simplify code */
- if (_content == CLUSTER_16(0x0FF7U))
- _content = CLUS_BAD;
- else if (_content >= CLUSTER_16(0x0FF8U))
- _content = CLUS_EOF;
- *content = CLUSTER_32(_content);
- return 0;
- }
- static s32 fat12_ent_set(struct super_block *sb, u32 loc, u32 content)
- {
- u32 off;
- u64 sec;
- u8 *fat_sector, *fat_entry;
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- content &= FATENT_FAT12_VALID_MASK;
- sec = fsi->FAT1_start_sector + ((loc + (loc >> 1)) >> sb->s_blocksize_bits);
- off = (loc + (loc >> 1)) & (u32)(sb->s_blocksize - 1);
- fat_sector = fcache_getblk(sb, sec);
- if (!fat_sector)
- return -EIO;
- if (loc & 1) { /* odd */
- content <<= 4;
- if (off == (u32)(sb->s_blocksize-1)) {
- fat_sector[off] = (u8)(content | (fat_sector[off] & 0x0F));
- if (fcache_modify(sb, sec))
- return -EIO;
- fat_sector = fcache_getblk(sb, ++sec);
- if (!fat_sector)
- return -EIO;
- fat_sector[0] = (u8)(content >> 8);
- } else {
- fat_entry = &(fat_sector[off]);
- content |= 0x000F & get_unaligned_le16(fat_entry);
- put_unaligned_le16(content, fat_entry);
- }
- } else { /* even */
- fat_sector[off] = (u8)(content);
- if (off == (u32)(sb->s_blocksize-1)) {
- fat_sector[off] = (u8)(content);
- if (fcache_modify(sb, sec))
- return -EIO;
- fat_sector = fcache_getblk(sb, ++sec);
- if (!fat_sector)
- return -EIO;
- fat_sector[0] = (u8)((fat_sector[0] & 0xF0) | (content >> 8));
- } else {
- fat_entry = &(fat_sector[off]);
- content |= 0xF000 & get_unaligned_le16(fat_entry);
- put_unaligned_le16(content, fat_entry);
- }
- }
- return fcache_modify(sb, sec);
- }
- static FATENT_OPS_T fat12_ent_ops = {
- fat12_ent_get,
- fat12_ent_set
- };
- static FATENT_OPS_T fat16_ent_ops = {
- fat16_ent_get,
- fat16_ent_set
- };
- static FATENT_OPS_T fat32_ent_ops = {
- fat32_ent_get,
- fat32_ent_set
- };
- static FATENT_OPS_T exfat_ent_ops = {
- exfat_ent_get,
- exfat_ent_set
- };
- s32 fat_ent_ops_init(struct super_block *sb)
- {
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- switch (fsi->vol_type) {
- case EXFAT:
- fsi->fatent_ops = &exfat_ent_ops;
- break;
- case FAT32:
- fsi->fatent_ops = &fat32_ent_ops;
- break;
- case FAT16:
- fsi->fatent_ops = &fat16_ent_ops;
- break;
- case FAT12:
- fsi->fatent_ops = &fat12_ent_ops;
- break;
- default:
- fsi->fatent_ops = NULL;
- EMSG("Unknown volume type : %d", (int)fsi->vol_type);
- return -ENOTSUPP;
- }
- return 0;
- }
- static inline bool is_reserved_clus(u32 clus)
- {
- if (IS_CLUS_FREE(clus))
- return true;
- if (IS_CLUS_EOF(clus))
- return true;
- if (IS_CLUS_BAD(clus))
- return true;
- return false;
- }
- s32 fat_ent_get(struct super_block *sb, u32 loc, u32 *content)
- {
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- s32 err;
- if (!is_valid_clus(fsi, loc)) {
- sdfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", loc);
- return -EIO;
- }
- err = fsi->fatent_ops->ent_get(sb, loc, content);
- if (err) {
- sdfat_fs_error(sb, "failed to access to FAT "
- "(entry 0x%08x, err:%d)", loc, err);
- return err;
- }
- if (!is_reserved_clus(*content) && !is_valid_clus(fsi, *content)) {
- sdfat_fs_error(sb, "invalid access to FAT (entry 0x%08x) "
- "bogus content (0x%08x)", loc, *content);
- return -EIO;
- }
- return 0;
- }
- s32 fat_ent_set(struct super_block *sb, u32 loc, u32 content)
- {
- FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
- return fsi->fatent_ops->ent_set(sb, loc, content);
- }
- s32 fat_ent_get_safe(struct super_block *sb, u32 loc, u32 *content)
- {
- s32 err = fat_ent_get(sb, loc, content);
- if (err)
- return err;
- if (IS_CLUS_FREE(*content)) {
- sdfat_fs_error(sb, "invalid access to FAT free cluster "
- "(entry 0x%08x)", loc);
- return -EIO;
- }
- if (IS_CLUS_BAD(*content)) {
- sdfat_fs_error(sb, "invalid access to FAT bad cluster "
- "(entry 0x%08x)", loc);
- return -EIO;
- }
- return 0;
- }
- /* end of fatent.c */
|