Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'work.exfat' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs

Pull exfat filesystem from Al Viro:
"Shiny new fs/exfat replacement for drivers/staging/exfat"

* 'work.exfat' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs:
exfat: update file system parameter handling
staging: exfat: make staging/exfat and fs/exfat mutually exclusive
MAINTAINERS: add exfat filesystem
exfat: add Kconfig and Makefile
exfat: add nls operations
exfat: add misc operations
exfat: add exfat cache
exfat: add bitmap operations
exfat: add fat entry operations
exfat: add file operations
exfat: add directory operations
exfat: add inode operations
exfat: add super block operations
exfat: add in-memory and on-disk structures and headers

+7243 -1
+7
MAINTAINERS
··· 6385 6385 F: include/uapi/linux/mdio.h 6386 6386 F: include/uapi/linux/mii.h 6387 6387 6388 + EXFAT FILE SYSTEM 6389 + M: Namjae Jeon <namjae.jeon@samsung.com> 6390 + M: Sungjong Seo <sj1557.seo@samsung.com> 6391 + L: linux-fsdevel@vger.kernel.org 6392 + S: Maintained 6393 + F: fs/exfat/ 6394 + 6388 6395 EXT2 FILE SYSTEM 6389 6396 M: Jan Kara <jack@suse.com> 6390 6397 L: linux-ext4@vger.kernel.org
+2 -1
fs/Kconfig
··· 140 140 endif # BLOCK 141 141 142 142 if BLOCK 143 - menu "DOS/FAT/NT Filesystems" 143 + menu "DOS/FAT/EXFAT/NT Filesystems" 144 144 145 145 source "fs/fat/Kconfig" 146 + source "fs/exfat/Kconfig" 146 147 source "fs/ntfs/Kconfig" 147 148 148 149 endmenu
+1
fs/Makefile
··· 83 83 obj-$(CONFIG_CODA_FS) += coda/ 84 84 obj-$(CONFIG_MINIX_FS) += minix/ 85 85 obj-$(CONFIG_FAT_FS) += fat/ 86 + obj-$(CONFIG_EXFAT_FS) += exfat/ 86 87 obj-$(CONFIG_BFS_FS) += bfs/ 87 88 obj-$(CONFIG_ISO9660_FS) += isofs/ 88 89 obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
+21
fs/exfat/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + config EXFAT_FS 4 + tristate "exFAT filesystem support" 5 + select NLS 6 + help 7 + This allows you to mount devices formatted with the exFAT file system. 8 + exFAT is typically used on SD-Cards or USB sticks. 9 + 10 + To compile this as a module, choose M here: the module will be called 11 + exfat. 12 + 13 + config EXFAT_DEFAULT_IOCHARSET 14 + string "Default iocharset for exFAT" 15 + default "utf8" 16 + depends on EXFAT_FS 17 + help 18 + Set this to the default input/output character set to use for 19 + converting between the encoding is used for user visible filename and 20 + UTF-16 character that exfat filesystem use, and can be overridden with 21 + the "iocharset" mount option for exFAT filesystems.
+8
fs/exfat/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + # 3 + # Makefile for the linux exFAT filesystem support. 4 + # 5 + obj-$(CONFIG_EXFAT_FS) += exfat.o 6 + 7 + exfat-y := inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \ 8 + file.o balloc.o
+280
fs/exfat/balloc.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/blkdev.h> 7 + #include <linux/slab.h> 8 + #include <linux/buffer_head.h> 9 + 10 + #include "exfat_raw.h" 11 + #include "exfat_fs.h" 12 + 13 + static const unsigned char free_bit[] = { 14 + 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/ 15 + 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/ 16 + 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/ 17 + 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/ 18 + 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~ 99*/ 19 + 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/ 20 + 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/ 21 + 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/ 22 + 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/ 23 + 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/ 24 + 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/ 25 + 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/ 26 + 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /*240 ~ 254*/ 27 + }; 28 + 29 + static const unsigned char used_bit[] = { 30 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/* 0 ~ 19*/ 31 + 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~ 39*/ 32 + 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~ 59*/ 33 + 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~ 79*/ 34 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/ 35 + 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/ 36 + 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/ 37 + 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/ 38 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/ 39 + 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/ 40 + 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/ 41 + 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/ 42 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/ 43 + }; 44 + 45 + /* 46 + * Allocation Bitmap Management Functions 47 + */ 48 + static int exfat_allocate_bitmap(struct super_block *sb, 49 + struct exfat_dentry *ep) 50 + { 51 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 52 + long long map_size; 53 + unsigned int i, need_map_size; 54 + sector_t sector; 55 + 56 + sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu); 57 + map_size = le64_to_cpu(ep->dentry.bitmap.size); 58 + need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE) 59 + + 1; 60 + if (need_map_size != map_size) { 61 + exfat_msg(sb, KERN_ERR, 62 + "bogus allocation bitmap size(need : %u, cur : %lld)", 63 + need_map_size, map_size); 64 + /* 65 + * Only allowed when bogus allocation 66 + * bitmap size is large 67 + */ 68 + if (need_map_size > map_size) 69 + return -EIO; 70 + } 71 + sbi->map_sectors = ((need_map_size - 1) >> 72 + (sb->s_blocksize_bits)) + 1; 73 + sbi->vol_amap = kmalloc_array(sbi->map_sectors, 74 + sizeof(struct buffer_head *), GFP_KERNEL); 75 + if (!sbi->vol_amap) 76 + return -ENOMEM; 77 + 78 + sector = exfat_cluster_to_sector(sbi, sbi->map_clu); 79 + for (i = 0; i < sbi->map_sectors; i++) { 80 + sbi->vol_amap[i] = sb_bread(sb, sector + i); 81 + if (!sbi->vol_amap[i]) { 82 + /* release all buffers and free vol_amap */ 83 + int j = 0; 84 + 85 + while (j < i) 86 + brelse(sbi->vol_amap[j++]); 87 + 88 + kfree(sbi->vol_amap); 89 + sbi->vol_amap = NULL; 90 + return -EIO; 91 + } 92 + } 93 + 94 + sbi->pbr_bh = NULL; 95 + return 0; 96 + } 97 + 98 + int exfat_load_bitmap(struct super_block *sb) 99 + { 100 + unsigned int i, type; 101 + struct exfat_chain clu; 102 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 103 + 104 + exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 105 + while (clu.dir != EXFAT_EOF_CLUSTER) { 106 + for (i = 0; i < sbi->dentries_per_clu; i++) { 107 + struct exfat_dentry *ep; 108 + struct buffer_head *bh; 109 + 110 + ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 111 + if (!ep) 112 + return -EIO; 113 + 114 + type = exfat_get_entry_type(ep); 115 + if (type == TYPE_UNUSED) 116 + break; 117 + if (type != TYPE_BITMAP) 118 + continue; 119 + if (ep->dentry.bitmap.flags == 0x0) { 120 + int err; 121 + 122 + err = exfat_allocate_bitmap(sb, ep); 123 + brelse(bh); 124 + return err; 125 + } 126 + brelse(bh); 127 + } 128 + 129 + if (exfat_get_next_cluster(sb, &clu.dir)) 130 + return -EIO; 131 + } 132 + 133 + return -EINVAL; 134 + } 135 + 136 + void exfat_free_bitmap(struct exfat_sb_info *sbi) 137 + { 138 + int i; 139 + 140 + brelse(sbi->pbr_bh); 141 + 142 + for (i = 0; i < sbi->map_sectors; i++) 143 + __brelse(sbi->vol_amap[i]); 144 + 145 + kfree(sbi->vol_amap); 146 + } 147 + 148 + /* 149 + * If the value of "clu" is 0, it means cluster 2 which is the first cluster of 150 + * the cluster heap. 151 + */ 152 + int exfat_set_bitmap(struct inode *inode, unsigned int clu) 153 + { 154 + int i, b; 155 + unsigned int ent_idx; 156 + struct super_block *sb = inode->i_sb; 157 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 158 + 159 + WARN_ON(clu < EXFAT_FIRST_CLUSTER); 160 + ent_idx = CLUSTER_TO_BITMAP_ENT(clu); 161 + i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); 162 + b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); 163 + 164 + set_bit_le(b, sbi->vol_amap[i]->b_data); 165 + exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode)); 166 + return 0; 167 + } 168 + 169 + /* 170 + * If the value of "clu" is 0, it means cluster 2 which is the first cluster of 171 + * the cluster heap. 172 + */ 173 + void exfat_clear_bitmap(struct inode *inode, unsigned int clu) 174 + { 175 + int i, b; 176 + unsigned int ent_idx; 177 + struct super_block *sb = inode->i_sb; 178 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 179 + struct exfat_mount_options *opts = &sbi->options; 180 + 181 + WARN_ON(clu < EXFAT_FIRST_CLUSTER); 182 + ent_idx = CLUSTER_TO_BITMAP_ENT(clu); 183 + i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); 184 + b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); 185 + 186 + clear_bit_le(b, sbi->vol_amap[i]->b_data); 187 + exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode)); 188 + 189 + if (opts->discard) { 190 + int ret_discard; 191 + 192 + ret_discard = sb_issue_discard(sb, 193 + exfat_cluster_to_sector(sbi, clu + 194 + EXFAT_RESERVED_CLUSTERS), 195 + (1 << sbi->sect_per_clus_bits), GFP_NOFS, 0); 196 + 197 + if (ret_discard == -EOPNOTSUPP) { 198 + exfat_msg(sb, KERN_ERR, 199 + "discard not supported by device, disabling"); 200 + opts->discard = 0; 201 + } 202 + } 203 + } 204 + 205 + /* 206 + * If the value of "clu" is 0, it means cluster 2 which is the first cluster of 207 + * the cluster heap. 208 + */ 209 + unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu) 210 + { 211 + unsigned int i, map_i, map_b, ent_idx; 212 + unsigned int clu_base, clu_free; 213 + unsigned char k, clu_mask; 214 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 215 + 216 + WARN_ON(clu < EXFAT_FIRST_CLUSTER); 217 + ent_idx = CLUSTER_TO_BITMAP_ENT(clu); 218 + clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx & ~(BITS_PER_BYTE_MASK)); 219 + clu_mask = IGNORED_BITS_REMAINED(clu, clu_base); 220 + 221 + map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); 222 + map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx); 223 + 224 + for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; 225 + i += BITS_PER_BYTE) { 226 + k = *(sbi->vol_amap[map_i]->b_data + map_b); 227 + if (clu_mask > 0) { 228 + k |= clu_mask; 229 + clu_mask = 0; 230 + } 231 + if (k < 0xFF) { 232 + clu_free = clu_base + free_bit[k]; 233 + if (clu_free < sbi->num_clusters) 234 + return clu_free; 235 + } 236 + clu_base += BITS_PER_BYTE; 237 + 238 + if (++map_b >= sb->s_blocksize || 239 + clu_base >= sbi->num_clusters) { 240 + if (++map_i >= sbi->map_sectors) { 241 + clu_base = EXFAT_FIRST_CLUSTER; 242 + map_i = 0; 243 + } 244 + map_b = 0; 245 + } 246 + } 247 + 248 + return EXFAT_EOF_CLUSTER; 249 + } 250 + 251 + int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count) 252 + { 253 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 254 + unsigned int count = 0; 255 + unsigned int i, map_i = 0, map_b = 0; 256 + unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi); 257 + unsigned int last_mask = total_clus & BITS_PER_BYTE_MASK; 258 + unsigned char clu_bits; 259 + const unsigned char last_bit_mask[] = {0, 0b00000001, 0b00000011, 260 + 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111}; 261 + 262 + total_clus &= ~last_mask; 263 + for (i = 0; i < total_clus; i += BITS_PER_BYTE) { 264 + clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b); 265 + count += used_bit[clu_bits]; 266 + if (++map_b >= (unsigned int)sb->s_blocksize) { 267 + map_i++; 268 + map_b = 0; 269 + } 270 + } 271 + 272 + if (last_mask) { 273 + clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b); 274 + clu_bits &= last_bit_mask[last_mask]; 275 + count += used_bit[clu_bits]; 276 + } 277 + 278 + *ret_count = count; 279 + return 0; 280 + }
+325
fs/exfat/cache.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * linux/fs/fat/cache.c 4 + * 5 + * Written 1992,1993 by Werner Almesberger 6 + * 7 + * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead 8 + * of inode number. 9 + * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers. 10 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 11 + */ 12 + 13 + #include <linux/slab.h> 14 + #include <asm/unaligned.h> 15 + #include <linux/buffer_head.h> 16 + 17 + #include "exfat_raw.h" 18 + #include "exfat_fs.h" 19 + 20 + #define EXFAT_CACHE_VALID 0 21 + #define EXFAT_MAX_CACHE 16 22 + 23 + struct exfat_cache { 24 + struct list_head cache_list; 25 + unsigned int nr_contig; /* number of contiguous clusters */ 26 + unsigned int fcluster; /* cluster number in the file. */ 27 + unsigned int dcluster; /* cluster number on disk. */ 28 + }; 29 + 30 + struct exfat_cache_id { 31 + unsigned int id; 32 + unsigned int nr_contig; 33 + unsigned int fcluster; 34 + unsigned int dcluster; 35 + }; 36 + 37 + static struct kmem_cache *exfat_cachep; 38 + 39 + static void exfat_cache_init_once(void *c) 40 + { 41 + struct exfat_cache *cache = (struct exfat_cache *)c; 42 + 43 + INIT_LIST_HEAD(&cache->cache_list); 44 + } 45 + 46 + int exfat_cache_init(void) 47 + { 48 + exfat_cachep = kmem_cache_create("exfat_cache", 49 + sizeof(struct exfat_cache), 50 + 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, 51 + exfat_cache_init_once); 52 + if (!exfat_cachep) 53 + return -ENOMEM; 54 + return 0; 55 + } 56 + 57 + void exfat_cache_shutdown(void) 58 + { 59 + if (!exfat_cachep) 60 + return; 61 + kmem_cache_destroy(exfat_cachep); 62 + } 63 + 64 + void exfat_cache_init_inode(struct inode *inode) 65 + { 66 + struct exfat_inode_info *ei = EXFAT_I(inode); 67 + 68 + spin_lock_init(&ei->cache_lru_lock); 69 + ei->nr_caches = 0; 70 + ei->cache_valid_id = EXFAT_CACHE_VALID + 1; 71 + INIT_LIST_HEAD(&ei->cache_lru); 72 + } 73 + 74 + static inline struct exfat_cache *exfat_cache_alloc(void) 75 + { 76 + return kmem_cache_alloc(exfat_cachep, GFP_NOFS); 77 + } 78 + 79 + static inline void exfat_cache_free(struct exfat_cache *cache) 80 + { 81 + WARN_ON(!list_empty(&cache->cache_list)); 82 + kmem_cache_free(exfat_cachep, cache); 83 + } 84 + 85 + static inline void exfat_cache_update_lru(struct inode *inode, 86 + struct exfat_cache *cache) 87 + { 88 + struct exfat_inode_info *ei = EXFAT_I(inode); 89 + 90 + if (ei->cache_lru.next != &cache->cache_list) 91 + list_move(&cache->cache_list, &ei->cache_lru); 92 + } 93 + 94 + static unsigned int exfat_cache_lookup(struct inode *inode, 95 + unsigned int fclus, struct exfat_cache_id *cid, 96 + unsigned int *cached_fclus, unsigned int *cached_dclus) 97 + { 98 + struct exfat_inode_info *ei = EXFAT_I(inode); 99 + static struct exfat_cache nohit = { .fcluster = 0, }; 100 + struct exfat_cache *hit = &nohit, *p; 101 + unsigned int offset = EXFAT_EOF_CLUSTER; 102 + 103 + spin_lock(&ei->cache_lru_lock); 104 + list_for_each_entry(p, &ei->cache_lru, cache_list) { 105 + /* Find the cache of "fclus" or nearest cache. */ 106 + if (p->fcluster <= fclus && hit->fcluster < p->fcluster) { 107 + hit = p; 108 + if (hit->fcluster + hit->nr_contig < fclus) { 109 + offset = hit->nr_contig; 110 + } else { 111 + offset = fclus - hit->fcluster; 112 + break; 113 + } 114 + } 115 + } 116 + if (hit != &nohit) { 117 + exfat_cache_update_lru(inode, hit); 118 + 119 + cid->id = ei->cache_valid_id; 120 + cid->nr_contig = hit->nr_contig; 121 + cid->fcluster = hit->fcluster; 122 + cid->dcluster = hit->dcluster; 123 + *cached_fclus = cid->fcluster + offset; 124 + *cached_dclus = cid->dcluster + offset; 125 + } 126 + spin_unlock(&ei->cache_lru_lock); 127 + 128 + return offset; 129 + } 130 + 131 + static struct exfat_cache *exfat_cache_merge(struct inode *inode, 132 + struct exfat_cache_id *new) 133 + { 134 + struct exfat_inode_info *ei = EXFAT_I(inode); 135 + struct exfat_cache *p; 136 + 137 + list_for_each_entry(p, &ei->cache_lru, cache_list) { 138 + /* Find the same part as "new" in cluster-chain. */ 139 + if (p->fcluster == new->fcluster) { 140 + if (new->nr_contig > p->nr_contig) 141 + p->nr_contig = new->nr_contig; 142 + return p; 143 + } 144 + } 145 + return NULL; 146 + } 147 + 148 + static void exfat_cache_add(struct inode *inode, 149 + struct exfat_cache_id *new) 150 + { 151 + struct exfat_inode_info *ei = EXFAT_I(inode); 152 + struct exfat_cache *cache, *tmp; 153 + 154 + if (new->fcluster == EXFAT_EOF_CLUSTER) /* dummy cache */ 155 + return; 156 + 157 + spin_lock(&ei->cache_lru_lock); 158 + if (new->id != EXFAT_CACHE_VALID && 159 + new->id != ei->cache_valid_id) 160 + goto unlock; /* this cache was invalidated */ 161 + 162 + cache = exfat_cache_merge(inode, new); 163 + if (cache == NULL) { 164 + if (ei->nr_caches < EXFAT_MAX_CACHE) { 165 + ei->nr_caches++; 166 + spin_unlock(&ei->cache_lru_lock); 167 + 168 + tmp = exfat_cache_alloc(); 169 + if (!tmp) { 170 + spin_lock(&ei->cache_lru_lock); 171 + ei->nr_caches--; 172 + spin_unlock(&ei->cache_lru_lock); 173 + return; 174 + } 175 + 176 + spin_lock(&ei->cache_lru_lock); 177 + cache = exfat_cache_merge(inode, new); 178 + if (cache != NULL) { 179 + ei->nr_caches--; 180 + exfat_cache_free(tmp); 181 + goto out_update_lru; 182 + } 183 + cache = tmp; 184 + } else { 185 + struct list_head *p = ei->cache_lru.prev; 186 + 187 + cache = list_entry(p, 188 + struct exfat_cache, cache_list); 189 + } 190 + cache->fcluster = new->fcluster; 191 + cache->dcluster = new->dcluster; 192 + cache->nr_contig = new->nr_contig; 193 + } 194 + out_update_lru: 195 + exfat_cache_update_lru(inode, cache); 196 + unlock: 197 + spin_unlock(&ei->cache_lru_lock); 198 + } 199 + 200 + /* 201 + * Cache invalidation occurs rarely, thus the LRU chain is not updated. It 202 + * fixes itself after a while. 203 + */ 204 + static void __exfat_cache_inval_inode(struct inode *inode) 205 + { 206 + struct exfat_inode_info *ei = EXFAT_I(inode); 207 + struct exfat_cache *cache; 208 + 209 + while (!list_empty(&ei->cache_lru)) { 210 + cache = list_entry(ei->cache_lru.next, 211 + struct exfat_cache, cache_list); 212 + list_del_init(&cache->cache_list); 213 + ei->nr_caches--; 214 + exfat_cache_free(cache); 215 + } 216 + /* Update. The copy of caches before this id is discarded. */ 217 + ei->cache_valid_id++; 218 + if (ei->cache_valid_id == EXFAT_CACHE_VALID) 219 + ei->cache_valid_id++; 220 + } 221 + 222 + void exfat_cache_inval_inode(struct inode *inode) 223 + { 224 + struct exfat_inode_info *ei = EXFAT_I(inode); 225 + 226 + spin_lock(&ei->cache_lru_lock); 227 + __exfat_cache_inval_inode(inode); 228 + spin_unlock(&ei->cache_lru_lock); 229 + } 230 + 231 + static inline int cache_contiguous(struct exfat_cache_id *cid, 232 + unsigned int dclus) 233 + { 234 + cid->nr_contig++; 235 + return cid->dcluster + cid->nr_contig == dclus; 236 + } 237 + 238 + static inline void cache_init(struct exfat_cache_id *cid, 239 + unsigned int fclus, unsigned int dclus) 240 + { 241 + cid->id = EXFAT_CACHE_VALID; 242 + cid->fcluster = fclus; 243 + cid->dcluster = dclus; 244 + cid->nr_contig = 0; 245 + } 246 + 247 + int exfat_get_cluster(struct inode *inode, unsigned int cluster, 248 + unsigned int *fclus, unsigned int *dclus, 249 + unsigned int *last_dclus, int allow_eof) 250 + { 251 + struct super_block *sb = inode->i_sb; 252 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 253 + unsigned int limit = sbi->num_clusters; 254 + struct exfat_inode_info *ei = EXFAT_I(inode); 255 + struct exfat_cache_id cid; 256 + unsigned int content; 257 + 258 + if (ei->start_clu == EXFAT_FREE_CLUSTER) { 259 + exfat_fs_error(sb, 260 + "invalid access to exfat cache (entry 0x%08x)", 261 + ei->start_clu); 262 + return -EIO; 263 + } 264 + 265 + *fclus = 0; 266 + *dclus = ei->start_clu; 267 + *last_dclus = *dclus; 268 + 269 + /* 270 + * Don`t use exfat_cache if zero offset or non-cluster allocation 271 + */ 272 + if (cluster == 0 || *dclus == EXFAT_EOF_CLUSTER) 273 + return 0; 274 + 275 + cache_init(&cid, EXFAT_EOF_CLUSTER, EXFAT_EOF_CLUSTER); 276 + 277 + if (exfat_cache_lookup(inode, cluster, &cid, fclus, dclus) == 278 + EXFAT_EOF_CLUSTER) { 279 + /* 280 + * dummy, always not contiguous 281 + * This is reinitialized by cache_init(), later. 282 + */ 283 + WARN_ON(cid.id != EXFAT_CACHE_VALID || 284 + cid.fcluster != EXFAT_EOF_CLUSTER || 285 + cid.dcluster != EXFAT_EOF_CLUSTER || 286 + cid.nr_contig != 0); 287 + } 288 + 289 + if (*fclus == cluster) 290 + return 0; 291 + 292 + while (*fclus < cluster) { 293 + /* prevent the infinite loop of cluster chain */ 294 + if (*fclus > limit) { 295 + exfat_fs_error(sb, 296 + "detected the cluster chain loop (i_pos %u)", 297 + (*fclus)); 298 + return -EIO; 299 + } 300 + 301 + if (exfat_ent_get(sb, *dclus, &content)) 302 + return -EIO; 303 + 304 + *last_dclus = *dclus; 305 + *dclus = content; 306 + (*fclus)++; 307 + 308 + if (content == EXFAT_EOF_CLUSTER) { 309 + if (!allow_eof) { 310 + exfat_fs_error(sb, 311 + "invalid cluster chain (i_pos %u, last_clus 0x%08x is EOF)", 312 + *fclus, (*last_dclus)); 313 + return -EIO; 314 + } 315 + 316 + break; 317 + } 318 + 319 + if (!cache_contiguous(&cid, *dclus)) 320 + cache_init(&cid, *fclus, *dclus); 321 + } 322 + 323 + exfat_cache_add(inode, &cid); 324 + return 0; 325 + }
+1238
fs/exfat/dir.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/slab.h> 7 + #include <linux/bio.h> 8 + #include <linux/buffer_head.h> 9 + 10 + #include "exfat_raw.h" 11 + #include "exfat_fs.h" 12 + 13 + static int exfat_extract_uni_name(struct exfat_dentry *ep, 14 + unsigned short *uniname) 15 + { 16 + int i, len = 0; 17 + 18 + for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { 19 + *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]); 20 + if (*uniname == 0x0) 21 + return len; 22 + uniname++; 23 + len++; 24 + } 25 + 26 + *uniname = 0x0; 27 + return len; 28 + 29 + } 30 + 31 + static void exfat_get_uniname_from_ext_entry(struct super_block *sb, 32 + struct exfat_chain *p_dir, int entry, unsigned short *uniname) 33 + { 34 + int i; 35 + struct exfat_dentry *ep; 36 + struct exfat_entry_set_cache *es; 37 + 38 + es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES, &ep); 39 + if (!es) 40 + return; 41 + 42 + if (es->num_entries < 3) 43 + goto free_es; 44 + 45 + ep += 2; 46 + 47 + /* 48 + * First entry : file entry 49 + * Second entry : stream-extension entry 50 + * Third entry : first file-name entry 51 + * So, the index of first file-name dentry should start from 2. 52 + */ 53 + for (i = 2; i < es->num_entries; i++, ep++) { 54 + /* end of name entry */ 55 + if (exfat_get_entry_type(ep) != TYPE_EXTEND) 56 + goto free_es; 57 + 58 + exfat_extract_uni_name(ep, uniname); 59 + uniname += EXFAT_FILE_NAME_LEN; 60 + } 61 + 62 + free_es: 63 + kfree(es); 64 + } 65 + 66 + /* read a directory entry from the opened directory */ 67 + static int exfat_readdir(struct inode *inode, struct exfat_dir_entry *dir_entry) 68 + { 69 + int i, dentries_per_clu, dentries_per_clu_bits = 0; 70 + unsigned int type, clu_offset; 71 + sector_t sector; 72 + struct exfat_chain dir, clu; 73 + struct exfat_uni_name uni_name; 74 + struct exfat_dentry *ep; 75 + struct super_block *sb = inode->i_sb; 76 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 77 + struct exfat_inode_info *ei = EXFAT_I(inode); 78 + unsigned int dentry = ei->rwoffset & 0xFFFFFFFF; 79 + struct buffer_head *bh; 80 + 81 + /* check if the given file ID is opened */ 82 + if (ei->type != TYPE_DIR) 83 + return -EPERM; 84 + 85 + if (ei->entry == -1) 86 + exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 87 + else 88 + exfat_chain_set(&dir, ei->start_clu, 89 + EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); 90 + 91 + dentries_per_clu = sbi->dentries_per_clu; 92 + dentries_per_clu_bits = ilog2(dentries_per_clu); 93 + 94 + clu_offset = dentry >> dentries_per_clu_bits; 95 + exfat_chain_dup(&clu, &dir); 96 + 97 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 98 + clu.dir += clu_offset; 99 + clu.size -= clu_offset; 100 + } else { 101 + /* hint_information */ 102 + if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && 103 + ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { 104 + clu_offset -= ei->hint_bmap.off; 105 + clu.dir = ei->hint_bmap.clu; 106 + } 107 + 108 + while (clu_offset > 0) { 109 + if (exfat_get_next_cluster(sb, &(clu.dir))) 110 + return -EIO; 111 + 112 + clu_offset--; 113 + } 114 + } 115 + 116 + while (clu.dir != EXFAT_EOF_CLUSTER) { 117 + i = dentry & (dentries_per_clu - 1); 118 + 119 + for ( ; i < dentries_per_clu; i++, dentry++) { 120 + ep = exfat_get_dentry(sb, &clu, i, &bh, &sector); 121 + if (!ep) 122 + return -EIO; 123 + 124 + type = exfat_get_entry_type(ep); 125 + if (type == TYPE_UNUSED) { 126 + brelse(bh); 127 + break; 128 + } 129 + 130 + if (type != TYPE_FILE && type != TYPE_DIR) { 131 + brelse(bh); 132 + continue; 133 + } 134 + 135 + dir_entry->attr = le16_to_cpu(ep->dentry.file.attr); 136 + exfat_get_entry_time(sbi, &dir_entry->crtime, 137 + ep->dentry.file.create_tz, 138 + ep->dentry.file.create_time, 139 + ep->dentry.file.create_date, 140 + ep->dentry.file.create_time_ms); 141 + exfat_get_entry_time(sbi, &dir_entry->mtime, 142 + ep->dentry.file.modify_tz, 143 + ep->dentry.file.modify_time, 144 + ep->dentry.file.modify_date, 145 + ep->dentry.file.modify_time_ms); 146 + exfat_get_entry_time(sbi, &dir_entry->atime, 147 + ep->dentry.file.access_tz, 148 + ep->dentry.file.access_time, 149 + ep->dentry.file.access_date, 150 + 0); 151 + 152 + *uni_name.name = 0x0; 153 + exfat_get_uniname_from_ext_entry(sb, &dir, dentry, 154 + uni_name.name); 155 + exfat_utf16_to_nls(sb, &uni_name, 156 + dir_entry->namebuf.lfn, 157 + dir_entry->namebuf.lfnbuf_len); 158 + brelse(bh); 159 + 160 + ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL); 161 + if (!ep) 162 + return -EIO; 163 + dir_entry->size = 164 + le64_to_cpu(ep->dentry.stream.valid_size); 165 + brelse(bh); 166 + 167 + ei->hint_bmap.off = dentry >> dentries_per_clu_bits; 168 + ei->hint_bmap.clu = clu.dir; 169 + 170 + ei->rwoffset = ++dentry; 171 + return 0; 172 + } 173 + 174 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 175 + if (--clu.size > 0) 176 + clu.dir++; 177 + else 178 + clu.dir = EXFAT_EOF_CLUSTER; 179 + } else { 180 + if (exfat_get_next_cluster(sb, &(clu.dir))) 181 + return -EIO; 182 + } 183 + } 184 + 185 + dir_entry->namebuf.lfn[0] = '\0'; 186 + ei->rwoffset = dentry; 187 + return 0; 188 + } 189 + 190 + static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb) 191 + { 192 + nb->lfn = NULL; 193 + nb->lfnbuf_len = 0; 194 + } 195 + 196 + static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb) 197 + { 198 + nb->lfn = __getname(); 199 + if (!nb->lfn) 200 + return -ENOMEM; 201 + nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE; 202 + return 0; 203 + } 204 + 205 + static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb) 206 + { 207 + if (!nb->lfn) 208 + return; 209 + 210 + __putname(nb->lfn); 211 + exfat_init_namebuf(nb); 212 + } 213 + 214 + /* skip iterating emit_dots when dir is empty */ 215 + #define ITER_POS_FILLED_DOTS (2) 216 + static int exfat_iterate(struct file *filp, struct dir_context *ctx) 217 + { 218 + struct inode *inode = filp->f_path.dentry->d_inode; 219 + struct super_block *sb = inode->i_sb; 220 + struct inode *tmp; 221 + struct exfat_dir_entry de; 222 + struct exfat_dentry_namebuf *nb = &(de.namebuf); 223 + struct exfat_inode_info *ei = EXFAT_I(inode); 224 + unsigned long inum; 225 + loff_t cpos, i_pos; 226 + int err = 0, fake_offset = 0; 227 + 228 + exfat_init_namebuf(nb); 229 + mutex_lock(&EXFAT_SB(sb)->s_lock); 230 + 231 + cpos = ctx->pos; 232 + if (!dir_emit_dots(filp, ctx)) 233 + goto unlock; 234 + 235 + if (ctx->pos == ITER_POS_FILLED_DOTS) { 236 + cpos = 0; 237 + fake_offset = 1; 238 + } 239 + 240 + if (cpos & (DENTRY_SIZE - 1)) { 241 + err = -ENOENT; 242 + goto unlock; 243 + } 244 + 245 + /* name buffer should be allocated before use */ 246 + err = exfat_alloc_namebuf(nb); 247 + if (err) 248 + goto unlock; 249 + get_new: 250 + ei->rwoffset = EXFAT_B_TO_DEN(cpos); 251 + 252 + if (cpos >= i_size_read(inode)) 253 + goto end_of_dir; 254 + 255 + err = exfat_readdir(inode, &de); 256 + if (err) { 257 + /* 258 + * At least we tried to read a sector. Move cpos to next sector 259 + * position (should be aligned). 260 + */ 261 + if (err == -EIO) { 262 + cpos += 1 << (sb->s_blocksize_bits); 263 + cpos &= ~(sb->s_blocksize - 1); 264 + } 265 + 266 + err = -EIO; 267 + goto end_of_dir; 268 + } 269 + 270 + cpos = EXFAT_DEN_TO_B(ei->rwoffset); 271 + 272 + if (!nb->lfn[0]) 273 + goto end_of_dir; 274 + 275 + i_pos = ((loff_t)ei->start_clu << 32) | 276 + ((ei->rwoffset - 1) & 0xffffffff); 277 + tmp = exfat_iget(sb, i_pos); 278 + if (tmp) { 279 + inum = tmp->i_ino; 280 + iput(tmp); 281 + } else { 282 + inum = iunique(sb, EXFAT_ROOT_INO); 283 + } 284 + 285 + /* 286 + * Before calling dir_emit(), sb_lock should be released. 287 + * Because page fault can occur in dir_emit() when the size 288 + * of buffer given from user is larger than one page size. 289 + */ 290 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 291 + if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum, 292 + (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG)) 293 + goto out_unlocked; 294 + mutex_lock(&EXFAT_SB(sb)->s_lock); 295 + ctx->pos = cpos; 296 + goto get_new; 297 + 298 + end_of_dir: 299 + if (!cpos && fake_offset) 300 + cpos = ITER_POS_FILLED_DOTS; 301 + ctx->pos = cpos; 302 + unlock: 303 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 304 + out_unlocked: 305 + /* 306 + * To improve performance, free namebuf after unlock sb_lock. 307 + * If namebuf is not allocated, this function do nothing 308 + */ 309 + exfat_free_namebuf(nb); 310 + return err; 311 + } 312 + 313 + const struct file_operations exfat_dir_operations = { 314 + .llseek = generic_file_llseek, 315 + .read = generic_read_dir, 316 + .iterate = exfat_iterate, 317 + .fsync = generic_file_fsync, 318 + }; 319 + 320 + int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu) 321 + { 322 + int ret; 323 + 324 + exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN); 325 + 326 + ret = exfat_alloc_cluster(inode, 1, clu); 327 + if (ret) 328 + return ret; 329 + 330 + return exfat_zeroed_cluster(inode, clu->dir); 331 + } 332 + 333 + int exfat_calc_num_entries(struct exfat_uni_name *p_uniname) 334 + { 335 + int len; 336 + 337 + len = p_uniname->name_len; 338 + if (len == 0) 339 + return -EINVAL; 340 + 341 + /* 1 file entry + 1 stream entry + name entries */ 342 + return ((len - 1) / EXFAT_FILE_NAME_LEN + 3); 343 + } 344 + 345 + unsigned int exfat_get_entry_type(struct exfat_dentry *ep) 346 + { 347 + if (ep->type == EXFAT_UNUSED) 348 + return TYPE_UNUSED; 349 + if (IS_EXFAT_DELETED(ep->type)) 350 + return TYPE_DELETED; 351 + if (ep->type == EXFAT_INVAL) 352 + return TYPE_INVALID; 353 + if (IS_EXFAT_CRITICAL_PRI(ep->type)) { 354 + if (ep->type == EXFAT_BITMAP) 355 + return TYPE_BITMAP; 356 + if (ep->type == EXFAT_UPCASE) 357 + return TYPE_UPCASE; 358 + if (ep->type == EXFAT_VOLUME) 359 + return TYPE_VOLUME; 360 + if (ep->type == EXFAT_FILE) { 361 + if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR) 362 + return TYPE_DIR; 363 + return TYPE_FILE; 364 + } 365 + return TYPE_CRITICAL_PRI; 366 + } 367 + if (IS_EXFAT_BENIGN_PRI(ep->type)) { 368 + if (ep->type == EXFAT_GUID) 369 + return TYPE_GUID; 370 + if (ep->type == EXFAT_PADDING) 371 + return TYPE_PADDING; 372 + if (ep->type == EXFAT_ACLTAB) 373 + return TYPE_ACLTAB; 374 + return TYPE_BENIGN_PRI; 375 + } 376 + if (IS_EXFAT_CRITICAL_SEC(ep->type)) { 377 + if (ep->type == EXFAT_STREAM) 378 + return TYPE_STREAM; 379 + if (ep->type == EXFAT_NAME) 380 + return TYPE_EXTEND; 381 + if (ep->type == EXFAT_ACL) 382 + return TYPE_ACL; 383 + return TYPE_CRITICAL_SEC; 384 + } 385 + return TYPE_BENIGN_SEC; 386 + } 387 + 388 + static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type) 389 + { 390 + if (type == TYPE_UNUSED) { 391 + ep->type = EXFAT_UNUSED; 392 + } else if (type == TYPE_DELETED) { 393 + ep->type &= EXFAT_DELETE; 394 + } else if (type == TYPE_STREAM) { 395 + ep->type = EXFAT_STREAM; 396 + } else if (type == TYPE_EXTEND) { 397 + ep->type = EXFAT_NAME; 398 + } else if (type == TYPE_BITMAP) { 399 + ep->type = EXFAT_BITMAP; 400 + } else if (type == TYPE_UPCASE) { 401 + ep->type = EXFAT_UPCASE; 402 + } else if (type == TYPE_VOLUME) { 403 + ep->type = EXFAT_VOLUME; 404 + } else if (type == TYPE_DIR) { 405 + ep->type = EXFAT_FILE; 406 + ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR); 407 + } else if (type == TYPE_FILE) { 408 + ep->type = EXFAT_FILE; 409 + ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE); 410 + } 411 + } 412 + 413 + static void exfat_init_stream_entry(struct exfat_dentry *ep, 414 + unsigned char flags, unsigned int start_clu, 415 + unsigned long long size) 416 + { 417 + exfat_set_entry_type(ep, TYPE_STREAM); 418 + ep->dentry.stream.flags = flags; 419 + ep->dentry.stream.start_clu = cpu_to_le32(start_clu); 420 + ep->dentry.stream.valid_size = cpu_to_le64(size); 421 + ep->dentry.stream.size = cpu_to_le64(size); 422 + } 423 + 424 + static void exfat_init_name_entry(struct exfat_dentry *ep, 425 + unsigned short *uniname) 426 + { 427 + int i; 428 + 429 + exfat_set_entry_type(ep, TYPE_EXTEND); 430 + ep->dentry.name.flags = 0x0; 431 + 432 + for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { 433 + ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname); 434 + if (*uniname == 0x0) 435 + break; 436 + uniname++; 437 + } 438 + } 439 + 440 + int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, 441 + int entry, unsigned int type, unsigned int start_clu, 442 + unsigned long long size) 443 + { 444 + struct super_block *sb = inode->i_sb; 445 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 446 + struct timespec64 ts = current_time(inode); 447 + sector_t sector; 448 + struct exfat_dentry *ep; 449 + struct buffer_head *bh; 450 + 451 + /* 452 + * We cannot use exfat_get_dentry_set here because file ep is not 453 + * initialized yet. 454 + */ 455 + ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector); 456 + if (!ep) 457 + return -EIO; 458 + 459 + exfat_set_entry_type(ep, type); 460 + exfat_set_entry_time(sbi, &ts, 461 + &ep->dentry.file.create_tz, 462 + &ep->dentry.file.create_time, 463 + &ep->dentry.file.create_date, 464 + &ep->dentry.file.create_time_ms); 465 + exfat_set_entry_time(sbi, &ts, 466 + &ep->dentry.file.modify_tz, 467 + &ep->dentry.file.modify_time, 468 + &ep->dentry.file.modify_date, 469 + &ep->dentry.file.modify_time_ms); 470 + exfat_set_entry_time(sbi, &ts, 471 + &ep->dentry.file.access_tz, 472 + &ep->dentry.file.access_time, 473 + &ep->dentry.file.access_date, 474 + NULL); 475 + 476 + exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 477 + brelse(bh); 478 + 479 + ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector); 480 + if (!ep) 481 + return -EIO; 482 + 483 + exfat_init_stream_entry(ep, 484 + (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN, 485 + start_clu, size); 486 + exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 487 + brelse(bh); 488 + 489 + return 0; 490 + } 491 + 492 + int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, 493 + int entry) 494 + { 495 + struct super_block *sb = inode->i_sb; 496 + int ret = 0; 497 + int i, num_entries; 498 + sector_t sector; 499 + unsigned short chksum; 500 + struct exfat_dentry *ep, *fep; 501 + struct buffer_head *fbh, *bh; 502 + 503 + fep = exfat_get_dentry(sb, p_dir, entry, &fbh, &sector); 504 + if (!fep) 505 + return -EIO; 506 + 507 + num_entries = fep->dentry.file.num_ext + 1; 508 + chksum = exfat_calc_chksum_2byte(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY); 509 + 510 + for (i = 1; i < num_entries; i++) { 511 + ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL); 512 + if (!ep) { 513 + ret = -EIO; 514 + goto release_fbh; 515 + } 516 + chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum, 517 + CS_DEFAULT); 518 + brelse(bh); 519 + } 520 + 521 + fep->dentry.file.checksum = cpu_to_le16(chksum); 522 + exfat_update_bh(sb, fbh, IS_DIRSYNC(inode)); 523 + release_fbh: 524 + brelse(fbh); 525 + return ret; 526 + } 527 + 528 + int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, 529 + int entry, int num_entries, struct exfat_uni_name *p_uniname) 530 + { 531 + struct super_block *sb = inode->i_sb; 532 + int i; 533 + sector_t sector; 534 + unsigned short *uniname = p_uniname->name; 535 + struct exfat_dentry *ep; 536 + struct buffer_head *bh; 537 + int sync = IS_DIRSYNC(inode); 538 + 539 + ep = exfat_get_dentry(sb, p_dir, entry, &bh, &sector); 540 + if (!ep) 541 + return -EIO; 542 + 543 + ep->dentry.file.num_ext = (unsigned char)(num_entries - 1); 544 + exfat_update_bh(sb, bh, sync); 545 + brelse(bh); 546 + 547 + ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, &sector); 548 + if (!ep) 549 + return -EIO; 550 + 551 + ep->dentry.stream.name_len = p_uniname->name_len; 552 + ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash); 553 + exfat_update_bh(sb, bh, sync); 554 + brelse(bh); 555 + 556 + for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) { 557 + ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector); 558 + if (!ep) 559 + return -EIO; 560 + 561 + exfat_init_name_entry(ep, uniname); 562 + exfat_update_bh(sb, bh, sync); 563 + brelse(bh); 564 + uniname += EXFAT_FILE_NAME_LEN; 565 + } 566 + 567 + exfat_update_dir_chksum(inode, p_dir, entry); 568 + return 0; 569 + } 570 + 571 + int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, 572 + int entry, int order, int num_entries) 573 + { 574 + struct super_block *sb = inode->i_sb; 575 + int i; 576 + sector_t sector; 577 + struct exfat_dentry *ep; 578 + struct buffer_head *bh; 579 + 580 + for (i = order; i < num_entries; i++) { 581 + ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, &sector); 582 + if (!ep) 583 + return -EIO; 584 + 585 + exfat_set_entry_type(ep, TYPE_DELETED); 586 + exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 587 + brelse(bh); 588 + } 589 + 590 + return 0; 591 + } 592 + 593 + int exfat_update_dir_chksum_with_entry_set(struct super_block *sb, 594 + struct exfat_entry_set_cache *es, int sync) 595 + { 596 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 597 + struct buffer_head *bh; 598 + sector_t sec = es->sector; 599 + unsigned int off = es->offset; 600 + int chksum_type = CS_DIR_ENTRY, i, num_entries = es->num_entries; 601 + unsigned int buf_off = (off - es->offset); 602 + unsigned int remaining_byte_in_sector, copy_entries, clu; 603 + unsigned short chksum = 0; 604 + 605 + for (i = 0; i < num_entries; i++) { 606 + chksum = exfat_calc_chksum_2byte(&es->entries[i], DENTRY_SIZE, 607 + chksum, chksum_type); 608 + chksum_type = CS_DEFAULT; 609 + } 610 + 611 + es->entries[0].dentry.file.checksum = cpu_to_le16(chksum); 612 + 613 + while (num_entries) { 614 + /* write per sector base */ 615 + remaining_byte_in_sector = (1 << sb->s_blocksize_bits) - off; 616 + copy_entries = min_t(int, 617 + EXFAT_B_TO_DEN(remaining_byte_in_sector), 618 + num_entries); 619 + bh = sb_bread(sb, sec); 620 + if (!bh) 621 + goto err_out; 622 + memcpy(bh->b_data + off, 623 + (unsigned char *)&es->entries[0] + buf_off, 624 + EXFAT_DEN_TO_B(copy_entries)); 625 + exfat_update_bh(sb, bh, sync); 626 + brelse(bh); 627 + num_entries -= copy_entries; 628 + 629 + if (num_entries) { 630 + /* get next sector */ 631 + if (exfat_is_last_sector_in_cluster(sbi, sec)) { 632 + clu = exfat_sector_to_cluster(sbi, sec); 633 + if (es->alloc_flag == ALLOC_NO_FAT_CHAIN) 634 + clu++; 635 + else if (exfat_get_next_cluster(sb, &clu)) 636 + goto err_out; 637 + sec = exfat_cluster_to_sector(sbi, clu); 638 + } else { 639 + sec++; 640 + } 641 + off = 0; 642 + buf_off += EXFAT_DEN_TO_B(copy_entries); 643 + } 644 + } 645 + 646 + return 0; 647 + err_out: 648 + return -EIO; 649 + } 650 + 651 + static int exfat_walk_fat_chain(struct super_block *sb, 652 + struct exfat_chain *p_dir, unsigned int byte_offset, 653 + unsigned int *clu) 654 + { 655 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 656 + unsigned int clu_offset; 657 + unsigned int cur_clu; 658 + 659 + clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi); 660 + cur_clu = p_dir->dir; 661 + 662 + if (p_dir->flags == ALLOC_NO_FAT_CHAIN) { 663 + cur_clu += clu_offset; 664 + } else { 665 + while (clu_offset > 0) { 666 + if (exfat_get_next_cluster(sb, &cur_clu)) 667 + return -EIO; 668 + if (cur_clu == EXFAT_EOF_CLUSTER) { 669 + exfat_fs_error(sb, 670 + "invalid dentry access beyond EOF (clu : %u, eidx : %d)", 671 + p_dir->dir, 672 + EXFAT_B_TO_DEN(byte_offset)); 673 + return -EIO; 674 + } 675 + clu_offset--; 676 + } 677 + } 678 + 679 + *clu = cur_clu; 680 + return 0; 681 + } 682 + 683 + int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, 684 + int entry, sector_t *sector, int *offset) 685 + { 686 + int ret; 687 + unsigned int off, clu = 0; 688 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 689 + 690 + off = EXFAT_DEN_TO_B(entry); 691 + 692 + ret = exfat_walk_fat_chain(sb, p_dir, off, &clu); 693 + if (ret) 694 + return ret; 695 + 696 + /* byte offset in cluster */ 697 + off = EXFAT_CLU_OFFSET(off, sbi); 698 + 699 + /* byte offset in sector */ 700 + *offset = EXFAT_BLK_OFFSET(off, sb); 701 + 702 + /* sector offset in cluster */ 703 + *sector = EXFAT_B_TO_BLK(off, sb); 704 + *sector += exfat_cluster_to_sector(sbi, clu); 705 + return 0; 706 + } 707 + 708 + #define EXFAT_MAX_RA_SIZE (128*1024) 709 + static int exfat_dir_readahead(struct super_block *sb, sector_t sec) 710 + { 711 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 712 + struct buffer_head *bh; 713 + unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits; 714 + unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits; 715 + unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count); 716 + unsigned int ra_count = min(adj_ra_count, max_ra_count); 717 + 718 + /* Read-ahead is not required */ 719 + if (sbi->sect_per_clus == 1) 720 + return 0; 721 + 722 + if (sec < sbi->data_start_sector) { 723 + exfat_msg(sb, KERN_ERR, 724 + "requested sector is invalid(sect:%llu, root:%llu)", 725 + (unsigned long long)sec, sbi->data_start_sector); 726 + return -EIO; 727 + } 728 + 729 + /* Not sector aligned with ra_count, resize ra_count to page size */ 730 + if ((sec - sbi->data_start_sector) & (ra_count - 1)) 731 + ra_count = page_ra_count; 732 + 733 + bh = sb_find_get_block(sb, sec); 734 + if (!bh || !buffer_uptodate(bh)) { 735 + unsigned int i; 736 + 737 + for (i = 0; i < ra_count; i++) 738 + sb_breadahead(sb, (sector_t)(sec + i)); 739 + } 740 + brelse(bh); 741 + return 0; 742 + } 743 + 744 + struct exfat_dentry *exfat_get_dentry(struct super_block *sb, 745 + struct exfat_chain *p_dir, int entry, struct buffer_head **bh, 746 + sector_t *sector) 747 + { 748 + unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE); 749 + int off; 750 + sector_t sec; 751 + 752 + if (p_dir->dir == DIR_DELETED) { 753 + exfat_msg(sb, KERN_ERR, "abnormal access to deleted dentry\n"); 754 + return NULL; 755 + } 756 + 757 + if (exfat_find_location(sb, p_dir, entry, &sec, &off)) 758 + return NULL; 759 + 760 + if (p_dir->dir != EXFAT_FREE_CLUSTER && 761 + !(entry & (dentries_per_page - 1))) 762 + exfat_dir_readahead(sb, sec); 763 + 764 + *bh = sb_bread(sb, sec); 765 + if (!*bh) 766 + return NULL; 767 + 768 + if (sector) 769 + *sector = sec; 770 + return (struct exfat_dentry *)((*bh)->b_data + off); 771 + } 772 + 773 + enum exfat_validate_dentry_mode { 774 + ES_MODE_STARTED, 775 + ES_MODE_GET_FILE_ENTRY, 776 + ES_MODE_GET_STRM_ENTRY, 777 + ES_MODE_GET_NAME_ENTRY, 778 + ES_MODE_GET_CRITICAL_SEC_ENTRY, 779 + }; 780 + 781 + static bool exfat_validate_entry(unsigned int type, 782 + enum exfat_validate_dentry_mode *mode) 783 + { 784 + if (type == TYPE_UNUSED || type == TYPE_DELETED) 785 + return false; 786 + 787 + switch (*mode) { 788 + case ES_MODE_STARTED: 789 + if (type != TYPE_FILE && type != TYPE_DIR) 790 + return false; 791 + *mode = ES_MODE_GET_FILE_ENTRY; 792 + return true; 793 + case ES_MODE_GET_FILE_ENTRY: 794 + if (type != TYPE_STREAM) 795 + return false; 796 + *mode = ES_MODE_GET_STRM_ENTRY; 797 + return true; 798 + case ES_MODE_GET_STRM_ENTRY: 799 + if (type != TYPE_EXTEND) 800 + return false; 801 + *mode = ES_MODE_GET_NAME_ENTRY; 802 + return true; 803 + case ES_MODE_GET_NAME_ENTRY: 804 + if (type == TYPE_STREAM) 805 + return false; 806 + if (type != TYPE_EXTEND) { 807 + if (!(type & TYPE_CRITICAL_SEC)) 808 + return false; 809 + *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY; 810 + } 811 + return true; 812 + case ES_MODE_GET_CRITICAL_SEC_ENTRY: 813 + if (type == TYPE_EXTEND || type == TYPE_STREAM) 814 + return false; 815 + if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC) 816 + return false; 817 + return true; 818 + default: 819 + WARN_ON_ONCE(1); 820 + return false; 821 + } 822 + } 823 + 824 + /* 825 + * Returns a set of dentries for a file or dir. 826 + * 827 + * Note that this is a copy (dump) of dentries so that user should 828 + * call write_entry_set() to apply changes made in this entry set 829 + * to the real device. 830 + * 831 + * in: 832 + * sb+p_dir+entry: indicates a file/dir 833 + * type: specifies how many dentries should be included. 834 + * out: 835 + * file_ep: will point the first dentry(= file dentry) on success 836 + * return: 837 + * pointer of entry set on success, 838 + * NULL on failure. 839 + */ 840 + struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, 841 + struct exfat_chain *p_dir, int entry, unsigned int type, 842 + struct exfat_dentry **file_ep) 843 + { 844 + int ret; 845 + unsigned int off, byte_offset, clu = 0; 846 + unsigned int entry_type; 847 + sector_t sec; 848 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 849 + struct exfat_entry_set_cache *es; 850 + struct exfat_dentry *ep, *pos; 851 + unsigned char num_entries; 852 + enum exfat_validate_dentry_mode mode = ES_MODE_STARTED; 853 + struct buffer_head *bh; 854 + 855 + if (p_dir->dir == DIR_DELETED) { 856 + exfat_msg(sb, KERN_ERR, "access to deleted dentry\n"); 857 + return NULL; 858 + } 859 + 860 + byte_offset = EXFAT_DEN_TO_B(entry); 861 + ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu); 862 + if (ret) 863 + return NULL; 864 + 865 + /* byte offset in cluster */ 866 + byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi); 867 + 868 + /* byte offset in sector */ 869 + off = EXFAT_BLK_OFFSET(byte_offset, sb); 870 + 871 + /* sector offset in cluster */ 872 + sec = EXFAT_B_TO_BLK(byte_offset, sb); 873 + sec += exfat_cluster_to_sector(sbi, clu); 874 + 875 + bh = sb_bread(sb, sec); 876 + if (!bh) 877 + return NULL; 878 + 879 + ep = (struct exfat_dentry *)(bh->b_data + off); 880 + entry_type = exfat_get_entry_type(ep); 881 + 882 + if (entry_type != TYPE_FILE && entry_type != TYPE_DIR) 883 + goto release_bh; 884 + 885 + num_entries = type == ES_ALL_ENTRIES ? 886 + ep->dentry.file.num_ext + 1 : type; 887 + es = kmalloc(struct_size(es, entries, num_entries), GFP_KERNEL); 888 + if (!es) 889 + goto release_bh; 890 + 891 + es->num_entries = num_entries; 892 + es->sector = sec; 893 + es->offset = off; 894 + es->alloc_flag = p_dir->flags; 895 + 896 + pos = &es->entries[0]; 897 + 898 + while (num_entries) { 899 + if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) 900 + goto free_es; 901 + 902 + /* copy dentry */ 903 + memcpy(pos, ep, sizeof(struct exfat_dentry)); 904 + 905 + if (--num_entries == 0) 906 + break; 907 + 908 + if (((off + DENTRY_SIZE) & (sb->s_blocksize - 1)) < 909 + (off & (sb->s_blocksize - 1))) { 910 + /* get the next sector */ 911 + if (exfat_is_last_sector_in_cluster(sbi, sec)) { 912 + if (es->alloc_flag == ALLOC_NO_FAT_CHAIN) 913 + clu++; 914 + else if (exfat_get_next_cluster(sb, &clu)) 915 + goto free_es; 916 + sec = exfat_cluster_to_sector(sbi, clu); 917 + } else { 918 + sec++; 919 + } 920 + 921 + brelse(bh); 922 + bh = sb_bread(sb, sec); 923 + if (!bh) 924 + goto free_es; 925 + off = 0; 926 + ep = (struct exfat_dentry *)bh->b_data; 927 + } else { 928 + ep++; 929 + off += DENTRY_SIZE; 930 + } 931 + pos++; 932 + } 933 + 934 + if (file_ep) 935 + *file_ep = &es->entries[0]; 936 + brelse(bh); 937 + return es; 938 + 939 + free_es: 940 + kfree(es); 941 + release_bh: 942 + brelse(bh); 943 + return NULL; 944 + } 945 + 946 + enum { 947 + DIRENT_STEP_FILE, 948 + DIRENT_STEP_STRM, 949 + DIRENT_STEP_NAME, 950 + DIRENT_STEP_SECD, 951 + }; 952 + 953 + /* 954 + * return values: 955 + * >= 0 : return dir entiry position with the name in dir 956 + * -EEXIST : (root dir, ".") it is the root dir itself 957 + * -ENOENT : entry with the name does not exist 958 + * -EIO : I/O error 959 + */ 960 + int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, 961 + struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 962 + int num_entries, unsigned int type) 963 + { 964 + int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len; 965 + int order, step, name_len = 0; 966 + int dentries_per_clu, num_empty = 0; 967 + unsigned int entry_type; 968 + unsigned short *uniname = NULL; 969 + struct exfat_chain clu; 970 + struct exfat_hint *hint_stat = &ei->hint_stat; 971 + struct exfat_hint_femp candi_empty; 972 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 973 + 974 + dentries_per_clu = sbi->dentries_per_clu; 975 + 976 + exfat_chain_dup(&clu, p_dir); 977 + 978 + if (hint_stat->eidx) { 979 + clu.dir = hint_stat->clu; 980 + dentry = hint_stat->eidx; 981 + end_eidx = dentry; 982 + } 983 + 984 + candi_empty.eidx = EXFAT_HINT_NONE; 985 + rewind: 986 + order = 0; 987 + step = DIRENT_STEP_FILE; 988 + while (clu.dir != EXFAT_EOF_CLUSTER) { 989 + i = dentry & (dentries_per_clu - 1); 990 + for (; i < dentries_per_clu; i++, dentry++) { 991 + struct exfat_dentry *ep; 992 + struct buffer_head *bh; 993 + 994 + if (rewind && dentry == end_eidx) 995 + goto not_found; 996 + 997 + ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 998 + if (!ep) 999 + return -EIO; 1000 + 1001 + entry_type = exfat_get_entry_type(ep); 1002 + 1003 + if (entry_type == TYPE_UNUSED || 1004 + entry_type == TYPE_DELETED) { 1005 + step = DIRENT_STEP_FILE; 1006 + 1007 + num_empty++; 1008 + if (candi_empty.eidx == EXFAT_HINT_NONE && 1009 + num_empty == 1) { 1010 + exfat_chain_set(&candi_empty.cur, 1011 + clu.dir, clu.size, clu.flags); 1012 + } 1013 + 1014 + if (candi_empty.eidx == EXFAT_HINT_NONE && 1015 + num_empty >= num_entries) { 1016 + candi_empty.eidx = 1017 + dentry - (num_empty - 1); 1018 + WARN_ON(candi_empty.eidx < 0); 1019 + candi_empty.count = num_empty; 1020 + 1021 + if (ei->hint_femp.eidx == 1022 + EXFAT_HINT_NONE || 1023 + candi_empty.eidx <= 1024 + ei->hint_femp.eidx) { 1025 + memcpy(&ei->hint_femp, 1026 + &candi_empty, 1027 + sizeof(candi_empty)); 1028 + } 1029 + } 1030 + 1031 + brelse(bh); 1032 + if (entry_type == TYPE_UNUSED) 1033 + goto not_found; 1034 + continue; 1035 + } 1036 + 1037 + num_empty = 0; 1038 + candi_empty.eidx = EXFAT_HINT_NONE; 1039 + 1040 + if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) { 1041 + step = DIRENT_STEP_FILE; 1042 + if (type == TYPE_ALL || type == entry_type) { 1043 + num_ext = ep->dentry.file.num_ext; 1044 + step = DIRENT_STEP_STRM; 1045 + } 1046 + brelse(bh); 1047 + continue; 1048 + } 1049 + 1050 + if (entry_type == TYPE_STREAM) { 1051 + unsigned short name_hash; 1052 + 1053 + if (step != DIRENT_STEP_STRM) { 1054 + step = DIRENT_STEP_FILE; 1055 + brelse(bh); 1056 + continue; 1057 + } 1058 + step = DIRENT_STEP_FILE; 1059 + name_hash = le16_to_cpu( 1060 + ep->dentry.stream.name_hash); 1061 + if (p_uniname->name_hash == name_hash && 1062 + p_uniname->name_len == 1063 + ep->dentry.stream.name_len) { 1064 + step = DIRENT_STEP_NAME; 1065 + order = 1; 1066 + name_len = 0; 1067 + } 1068 + brelse(bh); 1069 + continue; 1070 + } 1071 + 1072 + brelse(bh); 1073 + if (entry_type == TYPE_EXTEND) { 1074 + unsigned short entry_uniname[16], unichar; 1075 + 1076 + if (step != DIRENT_STEP_NAME) { 1077 + step = DIRENT_STEP_FILE; 1078 + continue; 1079 + } 1080 + 1081 + if (++order == 2) 1082 + uniname = p_uniname->name; 1083 + else 1084 + uniname += EXFAT_FILE_NAME_LEN; 1085 + 1086 + len = exfat_extract_uni_name(ep, entry_uniname); 1087 + name_len += len; 1088 + 1089 + unichar = *(uniname+len); 1090 + *(uniname+len) = 0x0; 1091 + 1092 + if (exfat_uniname_ncmp(sb, uniname, 1093 + entry_uniname, len)) { 1094 + step = DIRENT_STEP_FILE; 1095 + } else if (p_uniname->name_len == name_len) { 1096 + if (order == num_ext) 1097 + goto found; 1098 + step = DIRENT_STEP_SECD; 1099 + } 1100 + 1101 + *(uniname+len) = unichar; 1102 + continue; 1103 + } 1104 + 1105 + if (entry_type & 1106 + (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) { 1107 + if (step == DIRENT_STEP_SECD) { 1108 + if (++order == num_ext) 1109 + goto found; 1110 + continue; 1111 + } 1112 + } 1113 + step = DIRENT_STEP_FILE; 1114 + } 1115 + 1116 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1117 + if (--clu.size > 0) 1118 + clu.dir++; 1119 + else 1120 + clu.dir = EXFAT_EOF_CLUSTER; 1121 + } else { 1122 + if (exfat_get_next_cluster(sb, &clu.dir)) 1123 + return -EIO; 1124 + } 1125 + } 1126 + 1127 + not_found: 1128 + /* 1129 + * We started at not 0 index,so we should try to find target 1130 + * from 0 index to the index we started at. 1131 + */ 1132 + if (!rewind && end_eidx) { 1133 + rewind = 1; 1134 + dentry = 0; 1135 + clu.dir = p_dir->dir; 1136 + /* reset empty hint */ 1137 + num_empty = 0; 1138 + candi_empty.eidx = EXFAT_HINT_NONE; 1139 + goto rewind; 1140 + } 1141 + 1142 + /* initialized hint_stat */ 1143 + hint_stat->clu = p_dir->dir; 1144 + hint_stat->eidx = 0; 1145 + return -ENOENT; 1146 + 1147 + found: 1148 + /* next dentry we'll find is out of this cluster */ 1149 + if (!((dentry + 1) & (dentries_per_clu - 1))) { 1150 + int ret = 0; 1151 + 1152 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1153 + if (--clu.size > 0) 1154 + clu.dir++; 1155 + else 1156 + clu.dir = EXFAT_EOF_CLUSTER; 1157 + } else { 1158 + ret = exfat_get_next_cluster(sb, &clu.dir); 1159 + } 1160 + 1161 + if (ret || clu.dir != EXFAT_EOF_CLUSTER) { 1162 + /* just initialized hint_stat */ 1163 + hint_stat->clu = p_dir->dir; 1164 + hint_stat->eidx = 0; 1165 + return (dentry - num_ext); 1166 + } 1167 + } 1168 + 1169 + hint_stat->clu = clu.dir; 1170 + hint_stat->eidx = dentry + 1; 1171 + return dentry - num_ext; 1172 + } 1173 + 1174 + int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir, 1175 + int entry, struct exfat_dentry *ep) 1176 + { 1177 + int i, count = 0; 1178 + unsigned int type; 1179 + struct exfat_dentry *ext_ep; 1180 + struct buffer_head *bh; 1181 + 1182 + for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) { 1183 + ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL); 1184 + if (!ext_ep) 1185 + return -EIO; 1186 + 1187 + type = exfat_get_entry_type(ext_ep); 1188 + brelse(bh); 1189 + if (type == TYPE_EXTEND || type == TYPE_STREAM) 1190 + count++; 1191 + else 1192 + break; 1193 + } 1194 + return count; 1195 + } 1196 + 1197 + int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) 1198 + { 1199 + int i, count = 0; 1200 + int dentries_per_clu; 1201 + unsigned int entry_type; 1202 + struct exfat_chain clu; 1203 + struct exfat_dentry *ep; 1204 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 1205 + struct buffer_head *bh; 1206 + 1207 + dentries_per_clu = sbi->dentries_per_clu; 1208 + 1209 + exfat_chain_dup(&clu, p_dir); 1210 + 1211 + while (clu.dir != EXFAT_EOF_CLUSTER) { 1212 + for (i = 0; i < dentries_per_clu; i++) { 1213 + ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 1214 + if (!ep) 1215 + return -EIO; 1216 + entry_type = exfat_get_entry_type(ep); 1217 + brelse(bh); 1218 + 1219 + if (entry_type == TYPE_UNUSED) 1220 + return count; 1221 + if (entry_type != TYPE_DIR) 1222 + continue; 1223 + count++; 1224 + } 1225 + 1226 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1227 + if (--clu.size > 0) 1228 + clu.dir++; 1229 + else 1230 + clu.dir = EXFAT_EOF_CLUSTER; 1231 + } else { 1232 + if (exfat_get_next_cluster(sb, &(clu.dir))) 1233 + return -EIO; 1234 + } 1235 + } 1236 + 1237 + return count; 1238 + }
+519
fs/exfat/exfat_fs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #ifndef _EXFAT_FS_H 7 + #define _EXFAT_FS_H 8 + 9 + #include <linux/fs.h> 10 + #include <linux/ratelimit.h> 11 + #include <linux/nls.h> 12 + 13 + #define EXFAT_SUPER_MAGIC 0x2011BAB0UL 14 + #define EXFAT_ROOT_INO 1 15 + 16 + #define EXFAT_SB_DIRTY 0 17 + 18 + #define EXFAT_CLUSTERS_UNTRACKED (~0u) 19 + 20 + /* 21 + * exfat error flags 22 + */ 23 + enum exfat_error_mode { 24 + EXFAT_ERRORS_CONT, /* ignore error and continue */ 25 + EXFAT_ERRORS_PANIC, /* panic on error */ 26 + EXFAT_ERRORS_RO, /* remount r/o on error */ 27 + }; 28 + 29 + /* 30 + * exfat nls lossy flag 31 + */ 32 + enum { 33 + NLS_NAME_NO_LOSSY, /* no lossy */ 34 + NLS_NAME_LOSSY, /* just detected incorrect filename(s) */ 35 + NLS_NAME_OVERLEN, /* the length is over than its limit */ 36 + }; 37 + 38 + #define EXFAT_HASH_BITS 8 39 + #define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS) 40 + 41 + /* 42 + * Type Definitions 43 + */ 44 + #define ES_2_ENTRIES 2 45 + #define ES_ALL_ENTRIES 0 46 + 47 + #define DIR_DELETED 0xFFFF0321 48 + 49 + /* type values */ 50 + #define TYPE_UNUSED 0x0000 51 + #define TYPE_DELETED 0x0001 52 + #define TYPE_INVALID 0x0002 53 + #define TYPE_CRITICAL_PRI 0x0100 54 + #define TYPE_BITMAP 0x0101 55 + #define TYPE_UPCASE 0x0102 56 + #define TYPE_VOLUME 0x0103 57 + #define TYPE_DIR 0x0104 58 + #define TYPE_FILE 0x011F 59 + #define TYPE_CRITICAL_SEC 0x0200 60 + #define TYPE_STREAM 0x0201 61 + #define TYPE_EXTEND 0x0202 62 + #define TYPE_ACL 0x0203 63 + #define TYPE_BENIGN_PRI 0x0400 64 + #define TYPE_GUID 0x0401 65 + #define TYPE_PADDING 0x0402 66 + #define TYPE_ACLTAB 0x0403 67 + #define TYPE_BENIGN_SEC 0x0800 68 + #define TYPE_ALL 0x0FFF 69 + 70 + #define MAX_CHARSET_SIZE 6 /* max size of multi-byte character */ 71 + #define MAX_NAME_LENGTH 255 /* max len of file name excluding NULL */ 72 + #define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE) 73 + 74 + #define FAT_CACHE_SIZE 128 75 + #define FAT_CACHE_HASH_SIZE 64 76 + #define BUF_CACHE_SIZE 256 77 + #define BUF_CACHE_HASH_SIZE 64 78 + 79 + #define EXFAT_HINT_NONE -1 80 + #define EXFAT_MIN_SUBDIR 2 81 + 82 + /* 83 + * helpers for cluster size to byte conversion. 84 + */ 85 + #define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits) 86 + #define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits) 87 + #define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \ 88 + (((b - 1) >> (sbi)->cluster_size_bits) + 1) 89 + #define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1)) 90 + 91 + /* 92 + * helpers for block size to byte conversion. 93 + */ 94 + #define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits) 95 + #define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits) 96 + #define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \ 97 + (((b - 1) >> (sb)->s_blocksize_bits) + 1) 98 + #define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1)) 99 + 100 + /* 101 + * helpers for block size to dentry size conversion. 102 + */ 103 + #define EXFAT_B_TO_DEN_IDX(b, sbi) \ 104 + ((b) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS)) 105 + #define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS) 106 + #define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS) 107 + 108 + /* 109 + * helpers for fat entry. 110 + */ 111 + #define FAT_ENT_SIZE (4) 112 + #define FAT_ENT_SIZE_BITS (2) 113 + #define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \ 114 + (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits)) 115 + #define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \ 116 + ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1)) 117 + 118 + /* 119 + * helpers for bitmap. 120 + */ 121 + #define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS) 122 + #define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS) 123 + #define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE) 124 + #define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1) 125 + #define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \ 126 + ((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits) 127 + #define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb)) 128 + #define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \ 129 + ((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1)) 130 + #define BITS_PER_BYTE_MASK 0x7 131 + #define IGNORED_BITS_REMAINED(clu, clu_base) ((1 << ((clu) - (clu_base))) - 1) 132 + 133 + struct exfat_dentry_namebuf { 134 + char *lfn; 135 + int lfnbuf_len; /* usally MAX_UNINAME_BUF_SIZE */ 136 + }; 137 + 138 + /* unicode name structure */ 139 + struct exfat_uni_name { 140 + /* +3 for null and for converting */ 141 + unsigned short name[MAX_NAME_LENGTH + 3]; 142 + unsigned short name_hash; 143 + unsigned char name_len; 144 + }; 145 + 146 + /* directory structure */ 147 + struct exfat_chain { 148 + unsigned int dir; 149 + unsigned int size; 150 + unsigned char flags; 151 + }; 152 + 153 + /* first empty entry hint information */ 154 + struct exfat_hint_femp { 155 + /* entry index of a directory */ 156 + int eidx; 157 + /* count of continuous empty entry */ 158 + int count; 159 + /* the cluster that first empty slot exists in */ 160 + struct exfat_chain cur; 161 + }; 162 + 163 + /* hint structure */ 164 + struct exfat_hint { 165 + unsigned int clu; 166 + union { 167 + unsigned int off; /* cluster offset */ 168 + int eidx; /* entry index */ 169 + }; 170 + }; 171 + 172 + struct exfat_entry_set_cache { 173 + /* sector number that contains file_entry */ 174 + sector_t sector; 175 + /* byte offset in the sector */ 176 + unsigned int offset; 177 + /* flag in stream entry. 01 for cluster chain, 03 for contig. */ 178 + int alloc_flag; 179 + unsigned int num_entries; 180 + struct exfat_dentry entries[]; 181 + }; 182 + 183 + struct exfat_dir_entry { 184 + struct exfat_chain dir; 185 + int entry; 186 + unsigned int type; 187 + unsigned int start_clu; 188 + unsigned char flags; 189 + unsigned short attr; 190 + loff_t size; 191 + unsigned int num_subdirs; 192 + struct timespec64 atime; 193 + struct timespec64 mtime; 194 + struct timespec64 crtime; 195 + struct exfat_dentry_namebuf namebuf; 196 + }; 197 + 198 + /* 199 + * exfat mount in-memory data 200 + */ 201 + struct exfat_mount_options { 202 + kuid_t fs_uid; 203 + kgid_t fs_gid; 204 + unsigned short fs_fmask; 205 + unsigned short fs_dmask; 206 + /* permission for setting the [am]time */ 207 + unsigned short allow_utime; 208 + /* charset for filename input/display */ 209 + char *iocharset; 210 + /* on error: continue, panic, remount-ro */ 211 + enum exfat_error_mode errors; 212 + unsigned utf8:1, /* Use of UTF-8 character set */ 213 + discard:1; /* Issue discard requests on deletions */ 214 + int time_offset; /* Offset of timestamps from UTC (in minutes) */ 215 + }; 216 + 217 + /* 218 + * EXFAT file system superblock in-memory data 219 + */ 220 + struct exfat_sb_info { 221 + unsigned long long num_sectors; /* num of sectors in volume */ 222 + unsigned int num_clusters; /* num of clusters in volume */ 223 + unsigned int cluster_size; /* cluster size in bytes */ 224 + unsigned int cluster_size_bits; 225 + unsigned int sect_per_clus; /* cluster size in sectors */ 226 + unsigned int sect_per_clus_bits; 227 + unsigned long long FAT1_start_sector; /* FAT1 start sector */ 228 + unsigned long long FAT2_start_sector; /* FAT2 start sector */ 229 + unsigned long long data_start_sector; /* data area start sector */ 230 + unsigned int num_FAT_sectors; /* num of FAT sectors */ 231 + unsigned int root_dir; /* root dir cluster */ 232 + unsigned int dentries_per_clu; /* num of dentries per cluster */ 233 + unsigned int vol_flag; /* volume dirty flag */ 234 + struct buffer_head *pbr_bh; /* buffer_head of PBR sector */ 235 + 236 + unsigned int map_clu; /* allocation bitmap start cluster */ 237 + unsigned int map_sectors; /* num of allocation bitmap sectors */ 238 + struct buffer_head **vol_amap; /* allocation bitmap */ 239 + 240 + unsigned short *vol_utbl; /* upcase table */ 241 + 242 + unsigned int clu_srch_ptr; /* cluster search pointer */ 243 + unsigned int used_clusters; /* number of used clusters */ 244 + 245 + unsigned long s_state; 246 + struct mutex s_lock; /* superblock lock */ 247 + struct exfat_mount_options options; 248 + struct nls_table *nls_io; /* Charset used for input and display */ 249 + struct ratelimit_state ratelimit; 250 + 251 + spinlock_t inode_hash_lock; 252 + struct hlist_head inode_hashtable[EXFAT_HASH_SIZE]; 253 + 254 + struct rcu_head rcu; 255 + }; 256 + 257 + /* 258 + * EXFAT file system inode in-memory data 259 + */ 260 + struct exfat_inode_info { 261 + struct exfat_chain dir; 262 + int entry; 263 + unsigned int type; 264 + unsigned short attr; 265 + unsigned int start_clu; 266 + unsigned char flags; 267 + /* 268 + * the copy of low 32bit of i_version to check 269 + * the validation of hint_stat. 270 + */ 271 + unsigned int version; 272 + /* file offset or dentry index for readdir */ 273 + loff_t rwoffset; 274 + 275 + /* hint for cluster last accessed */ 276 + struct exfat_hint hint_bmap; 277 + /* hint for entry index we try to lookup next time */ 278 + struct exfat_hint hint_stat; 279 + /* hint for first empty entry */ 280 + struct exfat_hint_femp hint_femp; 281 + 282 + spinlock_t cache_lru_lock; 283 + struct list_head cache_lru; 284 + int nr_caches; 285 + /* for avoiding the race between alloc and free */ 286 + unsigned int cache_valid_id; 287 + 288 + /* 289 + * NOTE: i_size_ondisk is 64bits, so must hold ->inode_lock to access. 290 + * physically allocated size. 291 + */ 292 + loff_t i_size_ondisk; 293 + /* block-aligned i_size (used in cont_write_begin) */ 294 + loff_t i_size_aligned; 295 + /* on-disk position of directory entry or 0 */ 296 + loff_t i_pos; 297 + /* hash by i_location */ 298 + struct hlist_node i_hash_fat; 299 + /* protect bmap against truncate */ 300 + struct rw_semaphore truncate_lock; 301 + struct inode vfs_inode; 302 + /* File creation time */ 303 + struct timespec64 i_crtime; 304 + }; 305 + 306 + static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb) 307 + { 308 + return sb->s_fs_info; 309 + } 310 + 311 + static inline struct exfat_inode_info *EXFAT_I(struct inode *inode) 312 + { 313 + return container_of(inode, struct exfat_inode_info, vfs_inode); 314 + } 315 + 316 + /* 317 + * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to 318 + * save ATTR_RO instead of ->i_mode. 319 + * 320 + * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only 321 + * bit, it's just used as flag for app. 322 + */ 323 + static inline int exfat_mode_can_hold_ro(struct inode *inode) 324 + { 325 + struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 326 + 327 + if (S_ISDIR(inode->i_mode)) 328 + return 0; 329 + 330 + if ((~sbi->options.fs_fmask) & 0222) 331 + return 1; 332 + return 0; 333 + } 334 + 335 + /* Convert attribute bits and a mask to the UNIX mode. */ 336 + static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi, 337 + unsigned short attr, mode_t mode) 338 + { 339 + if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR)) 340 + mode &= ~0222; 341 + 342 + if (attr & ATTR_SUBDIR) 343 + return (mode & ~sbi->options.fs_dmask) | S_IFDIR; 344 + 345 + return (mode & ~sbi->options.fs_fmask) | S_IFREG; 346 + } 347 + 348 + /* Return the FAT attribute byte for this inode */ 349 + static inline unsigned short exfat_make_attr(struct inode *inode) 350 + { 351 + unsigned short attr = EXFAT_I(inode)->attr; 352 + 353 + if (S_ISDIR(inode->i_mode)) 354 + attr |= ATTR_SUBDIR; 355 + if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222)) 356 + attr |= ATTR_READONLY; 357 + return attr; 358 + } 359 + 360 + static inline void exfat_save_attr(struct inode *inode, unsigned short attr) 361 + { 362 + if (exfat_mode_can_hold_ro(inode)) 363 + EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY); 364 + else 365 + EXFAT_I(inode)->attr = attr & ATTR_RWMASK; 366 + } 367 + 368 + static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi, 369 + sector_t sec) 370 + { 371 + return ((sec - sbi->data_start_sector + 1) & 372 + ((1 << sbi->sect_per_clus_bits) - 1)) == 0; 373 + } 374 + 375 + static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi, 376 + unsigned int clus) 377 + { 378 + return ((clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) + 379 + sbi->data_start_sector; 380 + } 381 + 382 + static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi, 383 + sector_t sec) 384 + { 385 + return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) + 386 + EXFAT_RESERVED_CLUSTERS; 387 + } 388 + 389 + /* super.c */ 390 + int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag); 391 + 392 + /* fatent.c */ 393 + #define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu) 394 + 395 + int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, 396 + struct exfat_chain *p_chain); 397 + int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain); 398 + int exfat_ent_get(struct super_block *sb, unsigned int loc, 399 + unsigned int *content); 400 + int exfat_ent_set(struct super_block *sb, unsigned int loc, 401 + unsigned int content); 402 + int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir, 403 + int entry, struct exfat_dentry *p_entry); 404 + int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain, 405 + unsigned int len); 406 + int exfat_zeroed_cluster(struct inode *dir, unsigned int clu); 407 + int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, 408 + unsigned int *ret_clu); 409 + int exfat_count_num_clusters(struct super_block *sb, 410 + struct exfat_chain *p_chain, unsigned int *ret_count); 411 + 412 + /* balloc.c */ 413 + int exfat_load_bitmap(struct super_block *sb); 414 + void exfat_free_bitmap(struct exfat_sb_info *sbi); 415 + int exfat_set_bitmap(struct inode *inode, unsigned int clu); 416 + void exfat_clear_bitmap(struct inode *inode, unsigned int clu); 417 + unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu); 418 + int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count); 419 + 420 + /* file.c */ 421 + extern const struct file_operations exfat_file_operations; 422 + int __exfat_truncate(struct inode *inode, loff_t new_size); 423 + void exfat_truncate(struct inode *inode, loff_t size); 424 + int exfat_setattr(struct dentry *dentry, struct iattr *attr); 425 + int exfat_getattr(const struct path *path, struct kstat *stat, 426 + unsigned int request_mask, unsigned int query_flags); 427 + 428 + /* namei.c */ 429 + extern const struct dentry_operations exfat_dentry_ops; 430 + extern const struct dentry_operations exfat_utf8_dentry_ops; 431 + 432 + /* cache.c */ 433 + int exfat_cache_init(void); 434 + void exfat_cache_shutdown(void); 435 + void exfat_cache_init_inode(struct inode *inode); 436 + void exfat_cache_inval_inode(struct inode *inode); 437 + int exfat_get_cluster(struct inode *inode, unsigned int cluster, 438 + unsigned int *fclus, unsigned int *dclus, 439 + unsigned int *last_dclus, int allow_eof); 440 + 441 + /* dir.c */ 442 + extern const struct inode_operations exfat_dir_inode_operations; 443 + extern const struct file_operations exfat_dir_operations; 444 + unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry); 445 + int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, 446 + int entry, unsigned int type, unsigned int start_clu, 447 + unsigned long long size); 448 + int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, 449 + int entry, int num_entries, struct exfat_uni_name *p_uniname); 450 + int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, 451 + int entry, int order, int num_entries); 452 + int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, 453 + int entry); 454 + int exfat_update_dir_chksum_with_entry_set(struct super_block *sb, 455 + struct exfat_entry_set_cache *es, int sync); 456 + int exfat_calc_num_entries(struct exfat_uni_name *p_uniname); 457 + int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, 458 + struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 459 + int num_entries, unsigned int type); 460 + int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu); 461 + int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, 462 + int entry, sector_t *sector, int *offset); 463 + struct exfat_dentry *exfat_get_dentry(struct super_block *sb, 464 + struct exfat_chain *p_dir, int entry, struct buffer_head **bh, 465 + sector_t *sector); 466 + struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, 467 + struct exfat_chain *p_dir, int entry, unsigned int type, 468 + struct exfat_dentry **file_ep); 469 + int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir); 470 + 471 + /* inode.c */ 472 + extern const struct inode_operations exfat_file_inode_operations; 473 + void exfat_sync_inode(struct inode *inode); 474 + struct inode *exfat_build_inode(struct super_block *sb, 475 + struct exfat_dir_entry *info, loff_t i_pos); 476 + void exfat_hash_inode(struct inode *inode, loff_t i_pos); 477 + void exfat_unhash_inode(struct inode *inode); 478 + struct inode *exfat_iget(struct super_block *sb, loff_t i_pos); 479 + int exfat_write_inode(struct inode *inode, struct writeback_control *wbc); 480 + void exfat_evict_inode(struct inode *inode); 481 + int exfat_block_truncate_page(struct inode *inode, loff_t from); 482 + 483 + /* exfat/nls.c */ 484 + unsigned short exfat_toupper(struct super_block *sb, unsigned short a); 485 + int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a, 486 + unsigned short *b, unsigned int len); 487 + int exfat_utf16_to_nls(struct super_block *sb, 488 + struct exfat_uni_name *uniname, unsigned char *p_cstring, 489 + int len); 490 + int exfat_nls_to_utf16(struct super_block *sb, 491 + const unsigned char *p_cstring, const int len, 492 + struct exfat_uni_name *uniname, int *p_lossy); 493 + int exfat_create_upcase_table(struct super_block *sb); 494 + void exfat_free_upcase_table(struct exfat_sb_info *sbi); 495 + unsigned short exfat_high_surrogate(unicode_t u); 496 + unsigned short exfat_low_surrogate(unicode_t u); 497 + 498 + /* exfat/misc.c */ 499 + void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) 500 + __printf(3, 4) __cold; 501 + #define exfat_fs_error(sb, fmt, args...) \ 502 + __exfat_fs_error(sb, 1, fmt, ## args) 503 + #define exfat_fs_error_ratelimit(sb, fmt, args...) \ 504 + __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \ 505 + fmt, ## args) 506 + void exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...) 507 + __printf(3, 4) __cold; 508 + void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 509 + u8 tz, __le16 time, __le16 date, u8 time_ms); 510 + void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 511 + u8 *tz, __le16 *time, __le16 *date, u8 *time_ms); 512 + unsigned short exfat_calc_chksum_2byte(void *data, int len, 513 + unsigned short chksum, int type); 514 + void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync); 515 + void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, 516 + unsigned int size, unsigned char flags); 517 + void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec); 518 + 519 + #endif /* !_EXFAT_FS_H */
+184
fs/exfat/exfat_raw.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #ifndef _EXFAT_RAW_H 7 + #define _EXFAT_RAW_H 8 + 9 + #include <linux/types.h> 10 + 11 + #define PBR_SIGNATURE 0xAA55 12 + 13 + #define EXFAT_MAX_FILE_LEN 255 14 + 15 + #define VOL_CLEAN 0x0000 16 + #define VOL_DIRTY 0x0002 17 + 18 + #define EXFAT_EOF_CLUSTER 0xFFFFFFFFu 19 + #define EXFAT_BAD_CLUSTER 0xFFFFFFF7u 20 + #define EXFAT_FREE_CLUSTER 0 21 + /* Cluster 0, 1 are reserved, the first cluster is 2 in the cluster heap. */ 22 + #define EXFAT_RESERVED_CLUSTERS 2 23 + #define EXFAT_FIRST_CLUSTER 2 24 + #define EXFAT_DATA_CLUSTER_COUNT(sbi) \ 25 + ((sbi)->num_clusters - EXFAT_RESERVED_CLUSTERS) 26 + 27 + /* AllocationPossible and NoFatChain field in GeneralSecondaryFlags Field */ 28 + #define ALLOC_FAT_CHAIN 0x01 29 + #define ALLOC_NO_FAT_CHAIN 0x03 30 + 31 + #define DENTRY_SIZE 32 /* directory entry size */ 32 + #define DENTRY_SIZE_BITS 5 33 + /* exFAT allows 8388608(256MB) directory entries */ 34 + #define MAX_EXFAT_DENTRIES 8388608 35 + 36 + /* dentry types */ 37 + #define EXFAT_UNUSED 0x00 /* end of directory */ 38 + #define EXFAT_DELETE (~0x80) 39 + #define IS_EXFAT_DELETED(x) ((x) < 0x80) /* deleted file (0x01~0x7F) */ 40 + #define EXFAT_INVAL 0x80 /* invalid value */ 41 + #define EXFAT_BITMAP 0x81 /* allocation bitmap */ 42 + #define EXFAT_UPCASE 0x82 /* upcase table */ 43 + #define EXFAT_VOLUME 0x83 /* volume label */ 44 + #define EXFAT_FILE 0x85 /* file or dir */ 45 + #define EXFAT_GUID 0xA0 46 + #define EXFAT_PADDING 0xA1 47 + #define EXFAT_ACLTAB 0xA2 48 + #define EXFAT_STREAM 0xC0 /* stream entry */ 49 + #define EXFAT_NAME 0xC1 /* file name entry */ 50 + #define EXFAT_ACL 0xC2 /* stream entry */ 51 + 52 + #define IS_EXFAT_CRITICAL_PRI(x) (x < 0xA0) 53 + #define IS_EXFAT_BENIGN_PRI(x) (x < 0xC0) 54 + #define IS_EXFAT_CRITICAL_SEC(x) (x < 0xE0) 55 + 56 + /* checksum types */ 57 + #define CS_DIR_ENTRY 0 58 + #define CS_PBR_SECTOR 1 59 + #define CS_DEFAULT 2 60 + 61 + /* file attributes */ 62 + #define ATTR_READONLY 0x0001 63 + #define ATTR_HIDDEN 0x0002 64 + #define ATTR_SYSTEM 0x0004 65 + #define ATTR_VOLUME 0x0008 66 + #define ATTR_SUBDIR 0x0010 67 + #define ATTR_ARCHIVE 0x0020 68 + 69 + #define ATTR_RWMASK (ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME | \ 70 + ATTR_SUBDIR | ATTR_ARCHIVE) 71 + 72 + #define PBR64_JUMP_BOOT_LEN 3 73 + #define PBR64_OEM_NAME_LEN 8 74 + #define PBR64_RESERVED_LEN 53 75 + 76 + #define EXFAT_FILE_NAME_LEN 15 77 + 78 + /* EXFAT BIOS parameter block (64 bytes) */ 79 + struct bpb64 { 80 + __u8 jmp_boot[PBR64_JUMP_BOOT_LEN]; 81 + __u8 oem_name[PBR64_OEM_NAME_LEN]; 82 + __u8 res_zero[PBR64_RESERVED_LEN]; 83 + } __packed; 84 + 85 + /* EXFAT EXTEND BIOS parameter block (56 bytes) */ 86 + struct bsx64 { 87 + __le64 vol_offset; 88 + __le64 vol_length; 89 + __le32 fat_offset; 90 + __le32 fat_length; 91 + __le32 clu_offset; 92 + __le32 clu_count; 93 + __le32 root_cluster; 94 + __le32 vol_serial; 95 + __u8 fs_version[2]; 96 + __le16 vol_flags; 97 + __u8 sect_size_bits; 98 + __u8 sect_per_clus_bits; 99 + __u8 num_fats; 100 + __u8 phy_drv_no; 101 + __u8 perc_in_use; 102 + __u8 reserved2[7]; 103 + } __packed; 104 + 105 + /* EXFAT PBR[BPB+BSX] (120 bytes) */ 106 + struct pbr64 { 107 + struct bpb64 bpb; 108 + struct bsx64 bsx; 109 + } __packed; 110 + 111 + /* Common PBR[Partition Boot Record] (512 bytes) */ 112 + struct pbr { 113 + union { 114 + __u8 raw[64]; 115 + struct bpb64 f64; 116 + } bpb; 117 + union { 118 + __u8 raw[56]; 119 + struct bsx64 f64; 120 + } bsx; 121 + __u8 boot_code[390]; 122 + __le16 signature; 123 + } __packed; 124 + 125 + struct exfat_dentry { 126 + __u8 type; 127 + union { 128 + struct { 129 + __u8 num_ext; 130 + __le16 checksum; 131 + __le16 attr; 132 + __le16 reserved1; 133 + __le16 create_time; 134 + __le16 create_date; 135 + __le16 modify_time; 136 + __le16 modify_date; 137 + __le16 access_time; 138 + __le16 access_date; 139 + __u8 create_time_ms; 140 + __u8 modify_time_ms; 141 + __u8 create_tz; 142 + __u8 modify_tz; 143 + __u8 access_tz; 144 + __u8 reserved2[7]; 145 + } __packed file; /* file directory entry */ 146 + struct { 147 + __u8 flags; 148 + __u8 reserved1; 149 + __u8 name_len; 150 + __le16 name_hash; 151 + __le16 reserved2; 152 + __le64 valid_size; 153 + __le32 reserved3; 154 + __le32 start_clu; 155 + __le64 size; 156 + } __packed stream; /* stream extension directory entry */ 157 + struct { 158 + __u8 flags; 159 + __le16 unicode_0_14[EXFAT_FILE_NAME_LEN]; 160 + } __packed name; /* file name directory entry */ 161 + struct { 162 + __u8 flags; 163 + __u8 reserved[18]; 164 + __le32 start_clu; 165 + __le64 size; 166 + } __packed bitmap; /* allocation bitmap directory entry */ 167 + struct { 168 + __u8 reserved1[3]; 169 + __le32 checksum; 170 + __u8 reserved2[12]; 171 + __le32 start_clu; 172 + __le64 size; 173 + } __packed upcase; /* up-case table directory entry */ 174 + } __packed dentry; 175 + } __packed; 176 + 177 + #define EXFAT_TZ_VALID (1 << 7) 178 + 179 + /* Jan 1 GMT 00:00:00 1980 */ 180 + #define EXFAT_MIN_TIMESTAMP_SECS 315532800LL 181 + /* Dec 31 GMT 23:59:59 2107 */ 182 + #define EXFAT_MAX_TIMESTAMP_SECS 4354819199LL 183 + 184 + #endif /* !_EXFAT_RAW_H */
+463
fs/exfat/fatent.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/slab.h> 7 + #include <asm/unaligned.h> 8 + #include <linux/buffer_head.h> 9 + 10 + #include "exfat_raw.h" 11 + #include "exfat_fs.h" 12 + 13 + static int exfat_mirror_bh(struct super_block *sb, sector_t sec, 14 + struct buffer_head *bh) 15 + { 16 + struct buffer_head *c_bh; 17 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 18 + sector_t sec2; 19 + int err = 0; 20 + 21 + if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) { 22 + sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector; 23 + c_bh = sb_getblk(sb, sec2); 24 + if (!c_bh) 25 + return -ENOMEM; 26 + memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize); 27 + set_buffer_uptodate(c_bh); 28 + mark_buffer_dirty(c_bh); 29 + if (sb->s_flags & SB_SYNCHRONOUS) 30 + err = sync_dirty_buffer(c_bh); 31 + brelse(c_bh); 32 + } 33 + 34 + return err; 35 + } 36 + 37 + static int __exfat_ent_get(struct super_block *sb, unsigned int loc, 38 + unsigned int *content) 39 + { 40 + unsigned int off; 41 + sector_t sec; 42 + struct buffer_head *bh; 43 + 44 + sec = FAT_ENT_OFFSET_SECTOR(sb, loc); 45 + off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc); 46 + 47 + bh = sb_bread(sb, sec); 48 + if (!bh) 49 + return -EIO; 50 + 51 + *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off])); 52 + 53 + /* remap reserved clusters to simplify code */ 54 + if (*content > EXFAT_BAD_CLUSTER) 55 + *content = EXFAT_EOF_CLUSTER; 56 + 57 + brelse(bh); 58 + return 0; 59 + } 60 + 61 + int exfat_ent_set(struct super_block *sb, unsigned int loc, 62 + unsigned int content) 63 + { 64 + unsigned int off; 65 + sector_t sec; 66 + __le32 *fat_entry; 67 + struct buffer_head *bh; 68 + 69 + sec = FAT_ENT_OFFSET_SECTOR(sb, loc); 70 + off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc); 71 + 72 + bh = sb_bread(sb, sec); 73 + if (!bh) 74 + return -EIO; 75 + 76 + fat_entry = (__le32 *)&(bh->b_data[off]); 77 + *fat_entry = cpu_to_le32(content); 78 + exfat_update_bh(sb, bh, sb->s_flags & SB_SYNCHRONOUS); 79 + exfat_mirror_bh(sb, sec, bh); 80 + brelse(bh); 81 + return 0; 82 + } 83 + 84 + static inline bool is_valid_cluster(struct exfat_sb_info *sbi, 85 + unsigned int clus) 86 + { 87 + if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus) 88 + return false; 89 + return true; 90 + } 91 + 92 + int exfat_ent_get(struct super_block *sb, unsigned int loc, 93 + unsigned int *content) 94 + { 95 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 96 + int err; 97 + 98 + if (!is_valid_cluster(sbi, loc)) { 99 + exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", 100 + loc); 101 + return -EIO; 102 + } 103 + 104 + err = __exfat_ent_get(sb, loc, content); 105 + if (err) { 106 + exfat_fs_error(sb, 107 + "failed to access to FAT (entry 0x%08x, err:%d)", 108 + loc, err); 109 + return err; 110 + } 111 + 112 + if (*content == EXFAT_FREE_CLUSTER) { 113 + exfat_fs_error(sb, 114 + "invalid access to FAT free cluster (entry 0x%08x)", 115 + loc); 116 + return -EIO; 117 + } 118 + 119 + if (*content == EXFAT_BAD_CLUSTER) { 120 + exfat_fs_error(sb, 121 + "invalid access to FAT bad cluster (entry 0x%08x)", 122 + loc); 123 + return -EIO; 124 + } 125 + 126 + if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) { 127 + exfat_fs_error(sb, 128 + "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)", 129 + loc, *content); 130 + return -EIO; 131 + } 132 + 133 + return 0; 134 + } 135 + 136 + int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain, 137 + unsigned int len) 138 + { 139 + if (!len) 140 + return 0; 141 + 142 + while (len > 1) { 143 + if (exfat_ent_set(sb, chain, chain + 1)) 144 + return -EIO; 145 + chain++; 146 + len--; 147 + } 148 + 149 + if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER)) 150 + return -EIO; 151 + return 0; 152 + } 153 + 154 + int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain) 155 + { 156 + unsigned int num_clusters = 0; 157 + unsigned int clu; 158 + struct super_block *sb = inode->i_sb; 159 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 160 + 161 + /* invalid cluster number */ 162 + if (p_chain->dir == EXFAT_FREE_CLUSTER || 163 + p_chain->dir == EXFAT_EOF_CLUSTER || 164 + p_chain->dir < EXFAT_FIRST_CLUSTER) 165 + return 0; 166 + 167 + /* no cluster to truncate */ 168 + if (p_chain->size == 0) 169 + return 0; 170 + 171 + /* check cluster validation */ 172 + if (p_chain->dir < 2 && p_chain->dir >= sbi->num_clusters) { 173 + exfat_msg(sb, KERN_ERR, "invalid start cluster (%u)", 174 + p_chain->dir); 175 + return -EIO; 176 + } 177 + 178 + set_bit(EXFAT_SB_DIRTY, &sbi->s_state); 179 + clu = p_chain->dir; 180 + 181 + if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 182 + do { 183 + exfat_clear_bitmap(inode, clu); 184 + clu++; 185 + 186 + num_clusters++; 187 + } while (num_clusters < p_chain->size); 188 + } else { 189 + do { 190 + exfat_clear_bitmap(inode, clu); 191 + 192 + if (exfat_get_next_cluster(sb, &clu)) 193 + goto dec_used_clus; 194 + 195 + num_clusters++; 196 + } while (clu != EXFAT_EOF_CLUSTER); 197 + } 198 + 199 + dec_used_clus: 200 + sbi->used_clusters -= num_clusters; 201 + return 0; 202 + } 203 + 204 + int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, 205 + unsigned int *ret_clu) 206 + { 207 + unsigned int clu, next; 208 + unsigned int count = 0; 209 + 210 + next = p_chain->dir; 211 + if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 212 + *ret_clu = next + p_chain->size - 1; 213 + return 0; 214 + } 215 + 216 + do { 217 + count++; 218 + clu = next; 219 + if (exfat_ent_get(sb, clu, &next)) 220 + return -EIO; 221 + } while (next != EXFAT_EOF_CLUSTER); 222 + 223 + if (p_chain->size != count) { 224 + exfat_fs_error(sb, 225 + "bogus directory size (clus : ondisk(%d) != counted(%d))", 226 + p_chain->size, count); 227 + return -EIO; 228 + } 229 + 230 + *ret_clu = clu; 231 + return 0; 232 + } 233 + 234 + static inline int exfat_sync_bhs(struct buffer_head **bhs, int nr_bhs) 235 + { 236 + int i, err = 0; 237 + 238 + for (i = 0; i < nr_bhs; i++) 239 + write_dirty_buffer(bhs[i], 0); 240 + 241 + for (i = 0; i < nr_bhs; i++) { 242 + wait_on_buffer(bhs[i]); 243 + if (!err && !buffer_uptodate(bhs[i])) 244 + err = -EIO; 245 + } 246 + return err; 247 + } 248 + 249 + int exfat_zeroed_cluster(struct inode *dir, unsigned int clu) 250 + { 251 + struct super_block *sb = dir->i_sb; 252 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 253 + struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 254 + int nr_bhs = MAX_BUF_PER_PAGE; 255 + sector_t blknr, last_blknr; 256 + int err, i, n; 257 + 258 + blknr = exfat_cluster_to_sector(sbi, clu); 259 + last_blknr = blknr + sbi->sect_per_clus; 260 + 261 + if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) { 262 + exfat_fs_error_ratelimit(sb, 263 + "%s: out of range(sect:%llu len:%u)", 264 + __func__, (unsigned long long)blknr, 265 + sbi->sect_per_clus); 266 + return -EIO; 267 + } 268 + 269 + /* Zeroing the unused blocks on this cluster */ 270 + n = 0; 271 + while (blknr < last_blknr) { 272 + bhs[n] = sb_getblk(sb, blknr); 273 + if (!bhs[n]) { 274 + err = -ENOMEM; 275 + goto release_bhs; 276 + } 277 + memset(bhs[n]->b_data, 0, sb->s_blocksize); 278 + exfat_update_bh(sb, bhs[n], 0); 279 + 280 + n++; 281 + blknr++; 282 + 283 + if (n == nr_bhs) { 284 + if (IS_DIRSYNC(dir)) { 285 + err = exfat_sync_bhs(bhs, n); 286 + if (err) 287 + goto release_bhs; 288 + } 289 + 290 + for (i = 0; i < n; i++) 291 + brelse(bhs[i]); 292 + n = 0; 293 + } 294 + } 295 + 296 + if (IS_DIRSYNC(dir)) { 297 + err = exfat_sync_bhs(bhs, n); 298 + if (err) 299 + goto release_bhs; 300 + } 301 + 302 + for (i = 0; i < n; i++) 303 + brelse(bhs[i]); 304 + 305 + return 0; 306 + 307 + release_bhs: 308 + exfat_msg(sb, KERN_ERR, "failed zeroed sect %llu\n", 309 + (unsigned long long)blknr); 310 + for (i = 0; i < n; i++) 311 + bforget(bhs[i]); 312 + return err; 313 + } 314 + 315 + int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, 316 + struct exfat_chain *p_chain) 317 + { 318 + int ret = -ENOSPC; 319 + unsigned int num_clusters = 0, total_cnt; 320 + unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER; 321 + struct super_block *sb = inode->i_sb; 322 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 323 + 324 + total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi); 325 + 326 + if (unlikely(total_cnt < sbi->used_clusters)) { 327 + exfat_fs_error_ratelimit(sb, 328 + "%s: invalid used clusters(t:%u,u:%u)\n", 329 + __func__, total_cnt, sbi->used_clusters); 330 + return -EIO; 331 + } 332 + 333 + if (num_alloc > total_cnt - sbi->used_clusters) 334 + return -ENOSPC; 335 + 336 + hint_clu = p_chain->dir; 337 + /* find new cluster */ 338 + if (hint_clu == EXFAT_EOF_CLUSTER) { 339 + if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) { 340 + exfat_msg(sb, KERN_ERR, 341 + "sbi->clu_srch_ptr is invalid (%u)\n", 342 + sbi->clu_srch_ptr); 343 + sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; 344 + } 345 + 346 + hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr); 347 + if (hint_clu == EXFAT_EOF_CLUSTER) 348 + return -ENOSPC; 349 + } 350 + 351 + /* check cluster validation */ 352 + if (hint_clu < EXFAT_FIRST_CLUSTER && hint_clu >= sbi->num_clusters) { 353 + exfat_msg(sb, KERN_ERR, "hint_cluster is invalid (%u)\n", 354 + hint_clu); 355 + hint_clu = EXFAT_FIRST_CLUSTER; 356 + if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 357 + if (exfat_chain_cont_cluster(sb, p_chain->dir, 358 + num_clusters)) 359 + return -EIO; 360 + p_chain->flags = ALLOC_FAT_CHAIN; 361 + } 362 + } 363 + 364 + set_bit(EXFAT_SB_DIRTY, &sbi->s_state); 365 + 366 + p_chain->dir = EXFAT_EOF_CLUSTER; 367 + 368 + while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) != 369 + EXFAT_EOF_CLUSTER) { 370 + if (new_clu != hint_clu && 371 + p_chain->flags == ALLOC_NO_FAT_CHAIN) { 372 + if (exfat_chain_cont_cluster(sb, p_chain->dir, 373 + num_clusters)) { 374 + ret = -EIO; 375 + goto free_cluster; 376 + } 377 + p_chain->flags = ALLOC_FAT_CHAIN; 378 + } 379 + 380 + /* update allocation bitmap */ 381 + if (exfat_set_bitmap(inode, new_clu)) { 382 + ret = -EIO; 383 + goto free_cluster; 384 + } 385 + 386 + num_clusters++; 387 + 388 + /* update FAT table */ 389 + if (p_chain->flags == ALLOC_FAT_CHAIN) { 390 + if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) { 391 + ret = -EIO; 392 + goto free_cluster; 393 + } 394 + } 395 + 396 + if (p_chain->dir == EXFAT_EOF_CLUSTER) { 397 + p_chain->dir = new_clu; 398 + } else if (p_chain->flags == ALLOC_FAT_CHAIN) { 399 + if (exfat_ent_set(sb, last_clu, new_clu)) { 400 + ret = -EIO; 401 + goto free_cluster; 402 + } 403 + } 404 + last_clu = new_clu; 405 + 406 + if (--num_alloc == 0) { 407 + sbi->clu_srch_ptr = hint_clu; 408 + sbi->used_clusters += num_clusters; 409 + 410 + p_chain->size += num_clusters; 411 + return 0; 412 + } 413 + 414 + hint_clu = new_clu + 1; 415 + if (hint_clu >= sbi->num_clusters) { 416 + hint_clu = EXFAT_FIRST_CLUSTER; 417 + 418 + if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 419 + if (exfat_chain_cont_cluster(sb, p_chain->dir, 420 + num_clusters)) { 421 + ret = -EIO; 422 + goto free_cluster; 423 + } 424 + p_chain->flags = ALLOC_FAT_CHAIN; 425 + } 426 + } 427 + } 428 + free_cluster: 429 + if (num_clusters) 430 + exfat_free_cluster(inode, p_chain); 431 + return ret; 432 + } 433 + 434 + int exfat_count_num_clusters(struct super_block *sb, 435 + struct exfat_chain *p_chain, unsigned int *ret_count) 436 + { 437 + unsigned int i, count; 438 + unsigned int clu; 439 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 440 + 441 + if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) { 442 + *ret_count = 0; 443 + return 0; 444 + } 445 + 446 + if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 447 + *ret_count = p_chain->size; 448 + return 0; 449 + } 450 + 451 + clu = p_chain->dir; 452 + count = 0; 453 + for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) { 454 + count++; 455 + if (exfat_ent_get(sb, clu, &clu)) 456 + return -EIO; 457 + if (clu == EXFAT_EOF_CLUSTER) 458 + break; 459 + } 460 + 461 + *ret_count = count; 462 + return 0; 463 + }
+360
fs/exfat/file.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/slab.h> 7 + #include <linux/cred.h> 8 + #include <linux/buffer_head.h> 9 + 10 + #include "exfat_raw.h" 11 + #include "exfat_fs.h" 12 + 13 + static int exfat_cont_expand(struct inode *inode, loff_t size) 14 + { 15 + struct address_space *mapping = inode->i_mapping; 16 + loff_t start = i_size_read(inode), count = size - i_size_read(inode); 17 + int err, err2; 18 + 19 + err = generic_cont_expand_simple(inode, size); 20 + if (err) 21 + return err; 22 + 23 + inode->i_ctime = inode->i_mtime = current_time(inode); 24 + mark_inode_dirty(inode); 25 + 26 + if (!IS_SYNC(inode)) 27 + return 0; 28 + 29 + err = filemap_fdatawrite_range(mapping, start, start + count - 1); 30 + err2 = sync_mapping_buffers(mapping); 31 + if (!err) 32 + err = err2; 33 + err2 = write_inode_now(inode, 1); 34 + if (!err) 35 + err = err2; 36 + if (err) 37 + return err; 38 + 39 + return filemap_fdatawait_range(mapping, start, start + count - 1); 40 + } 41 + 42 + static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode) 43 + { 44 + mode_t allow_utime = sbi->options.allow_utime; 45 + 46 + if (!uid_eq(current_fsuid(), inode->i_uid)) { 47 + if (in_group_p(inode->i_gid)) 48 + allow_utime >>= 3; 49 + if (allow_utime & MAY_WRITE) 50 + return true; 51 + } 52 + 53 + /* use a default check */ 54 + return false; 55 + } 56 + 57 + static int exfat_sanitize_mode(const struct exfat_sb_info *sbi, 58 + struct inode *inode, umode_t *mode_ptr) 59 + { 60 + mode_t i_mode, mask, perm; 61 + 62 + i_mode = inode->i_mode; 63 + 64 + mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ? 65 + sbi->options.fs_fmask : sbi->options.fs_dmask; 66 + perm = *mode_ptr & ~(S_IFMT | mask); 67 + 68 + /* Of the r and x bits, all (subject to umask) must be present.*/ 69 + if ((perm & 0555) != (i_mode & 0555)) 70 + return -EPERM; 71 + 72 + if (exfat_mode_can_hold_ro(inode)) { 73 + /* 74 + * Of the w bits, either all (subject to umask) or none must 75 + * be present. 76 + */ 77 + if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask))) 78 + return -EPERM; 79 + } else { 80 + /* 81 + * If exfat_mode_can_hold_ro(inode) is false, can't change 82 + * w bits. 83 + */ 84 + if ((perm & 0222) != (0222 & ~mask)) 85 + return -EPERM; 86 + } 87 + 88 + *mode_ptr &= S_IFMT | perm; 89 + 90 + return 0; 91 + } 92 + 93 + /* resize the file length */ 94 + int __exfat_truncate(struct inode *inode, loff_t new_size) 95 + { 96 + unsigned int num_clusters_new, num_clusters_phys; 97 + unsigned int last_clu = EXFAT_FREE_CLUSTER; 98 + struct exfat_chain clu; 99 + struct exfat_dentry *ep, *ep2; 100 + struct super_block *sb = inode->i_sb; 101 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 102 + struct exfat_inode_info *ei = EXFAT_I(inode); 103 + struct exfat_entry_set_cache *es = NULL; 104 + int evict = (ei->dir.dir == DIR_DELETED) ? 1 : 0; 105 + 106 + /* check if the given file ID is opened */ 107 + if (ei->type != TYPE_FILE && ei->type != TYPE_DIR) 108 + return -EPERM; 109 + 110 + exfat_set_vol_flags(sb, VOL_DIRTY); 111 + 112 + num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); 113 + num_clusters_phys = 114 + EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, sbi); 115 + 116 + exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); 117 + 118 + if (new_size > 0) { 119 + /* 120 + * Truncate FAT chain num_clusters after the first cluster 121 + * num_clusters = min(new, phys); 122 + */ 123 + unsigned int num_clusters = 124 + min(num_clusters_new, num_clusters_phys); 125 + 126 + /* 127 + * Follow FAT chain 128 + * (defensive coding - works fine even with corrupted FAT table 129 + */ 130 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 131 + clu.dir += num_clusters; 132 + clu.size -= num_clusters; 133 + } else { 134 + while (num_clusters > 0) { 135 + last_clu = clu.dir; 136 + if (exfat_get_next_cluster(sb, &(clu.dir))) 137 + return -EIO; 138 + 139 + num_clusters--; 140 + clu.size--; 141 + } 142 + } 143 + } else { 144 + ei->flags = ALLOC_NO_FAT_CHAIN; 145 + ei->start_clu = EXFAT_EOF_CLUSTER; 146 + } 147 + 148 + i_size_write(inode, new_size); 149 + 150 + if (ei->type == TYPE_FILE) 151 + ei->attr |= ATTR_ARCHIVE; 152 + 153 + /* update the directory entry */ 154 + if (!evict) { 155 + struct timespec64 ts; 156 + 157 + es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, 158 + ES_ALL_ENTRIES, &ep); 159 + if (!es) 160 + return -EIO; 161 + ep2 = ep + 1; 162 + 163 + ts = current_time(inode); 164 + exfat_set_entry_time(sbi, &ts, 165 + &ep->dentry.file.modify_tz, 166 + &ep->dentry.file.modify_time, 167 + &ep->dentry.file.modify_date, 168 + &ep->dentry.file.modify_time_ms); 169 + ep->dentry.file.attr = cpu_to_le16(ei->attr); 170 + 171 + /* File size should be zero if there is no cluster allocated */ 172 + if (ei->start_clu == EXFAT_EOF_CLUSTER) { 173 + ep->dentry.stream.valid_size = 0; 174 + ep->dentry.stream.size = 0; 175 + } else { 176 + ep->dentry.stream.valid_size = cpu_to_le64(new_size); 177 + ep->dentry.stream.size = ep->dentry.stream.valid_size; 178 + } 179 + 180 + if (new_size == 0) { 181 + /* Any directory can not be truncated to zero */ 182 + WARN_ON(ei->type != TYPE_FILE); 183 + 184 + ep2->dentry.stream.flags = ALLOC_FAT_CHAIN; 185 + ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER; 186 + } 187 + 188 + if (exfat_update_dir_chksum_with_entry_set(sb, es, 189 + inode_needs_sync(inode))) 190 + return -EIO; 191 + kfree(es); 192 + } 193 + 194 + /* cut off from the FAT chain */ 195 + if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER && 196 + last_clu != EXFAT_EOF_CLUSTER) { 197 + if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER)) 198 + return -EIO; 199 + } 200 + 201 + /* invalidate cache and free the clusters */ 202 + /* clear exfat cache */ 203 + exfat_cache_inval_inode(inode); 204 + 205 + /* hint information */ 206 + ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 207 + ei->hint_bmap.clu = EXFAT_EOF_CLUSTER; 208 + if (ei->rwoffset > new_size) 209 + ei->rwoffset = new_size; 210 + 211 + /* hint_stat will be used if this is directory. */ 212 + ei->hint_stat.eidx = 0; 213 + ei->hint_stat.clu = ei->start_clu; 214 + ei->hint_femp.eidx = EXFAT_HINT_NONE; 215 + 216 + /* free the clusters */ 217 + if (exfat_free_cluster(inode, &clu)) 218 + return -EIO; 219 + 220 + exfat_set_vol_flags(sb, VOL_CLEAN); 221 + 222 + return 0; 223 + } 224 + 225 + void exfat_truncate(struct inode *inode, loff_t size) 226 + { 227 + struct super_block *sb = inode->i_sb; 228 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 229 + unsigned int blocksize = 1 << inode->i_blkbits; 230 + loff_t aligned_size; 231 + int err; 232 + 233 + mutex_lock(&sbi->s_lock); 234 + if (EXFAT_I(inode)->start_clu == 0) { 235 + /* 236 + * Empty start_clu != ~0 (not allocated) 237 + */ 238 + exfat_fs_error(sb, "tried to truncate zeroed cluster."); 239 + goto write_size; 240 + } 241 + 242 + err = __exfat_truncate(inode, i_size_read(inode)); 243 + if (err) 244 + goto write_size; 245 + 246 + inode->i_ctime = inode->i_mtime = current_time(inode); 247 + if (IS_DIRSYNC(inode)) 248 + exfat_sync_inode(inode); 249 + else 250 + mark_inode_dirty(inode); 251 + 252 + inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) & 253 + ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 254 + write_size: 255 + aligned_size = i_size_read(inode); 256 + if (aligned_size & (blocksize - 1)) { 257 + aligned_size |= (blocksize - 1); 258 + aligned_size++; 259 + } 260 + 261 + if (EXFAT_I(inode)->i_size_ondisk > i_size_read(inode)) 262 + EXFAT_I(inode)->i_size_ondisk = aligned_size; 263 + 264 + if (EXFAT_I(inode)->i_size_aligned > i_size_read(inode)) 265 + EXFAT_I(inode)->i_size_aligned = aligned_size; 266 + mutex_unlock(&sbi->s_lock); 267 + } 268 + 269 + int exfat_getattr(const struct path *path, struct kstat *stat, 270 + unsigned int request_mask, unsigned int query_flags) 271 + { 272 + struct inode *inode = d_backing_inode(path->dentry); 273 + struct exfat_inode_info *ei = EXFAT_I(inode); 274 + 275 + generic_fillattr(inode, stat); 276 + stat->result_mask |= STATX_BTIME; 277 + stat->btime.tv_sec = ei->i_crtime.tv_sec; 278 + stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 279 + stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size; 280 + return 0; 281 + } 282 + 283 + int exfat_setattr(struct dentry *dentry, struct iattr *attr) 284 + { 285 + struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb); 286 + struct inode *inode = dentry->d_inode; 287 + unsigned int ia_valid; 288 + int error; 289 + 290 + if ((attr->ia_valid & ATTR_SIZE) && 291 + attr->ia_size > i_size_read(inode)) { 292 + error = exfat_cont_expand(inode, attr->ia_size); 293 + if (error || attr->ia_valid == ATTR_SIZE) 294 + return error; 295 + attr->ia_valid &= ~ATTR_SIZE; 296 + } 297 + 298 + /* Check for setting the inode time. */ 299 + ia_valid = attr->ia_valid; 300 + if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) && 301 + exfat_allow_set_time(sbi, inode)) { 302 + attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | 303 + ATTR_TIMES_SET); 304 + } 305 + 306 + error = setattr_prepare(dentry, attr); 307 + attr->ia_valid = ia_valid; 308 + if (error) 309 + goto out; 310 + 311 + if (((attr->ia_valid & ATTR_UID) && 312 + !uid_eq(attr->ia_uid, sbi->options.fs_uid)) || 313 + ((attr->ia_valid & ATTR_GID) && 314 + !gid_eq(attr->ia_gid, sbi->options.fs_gid)) || 315 + ((attr->ia_valid & ATTR_MODE) && 316 + (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) { 317 + error = -EPERM; 318 + goto out; 319 + } 320 + 321 + /* 322 + * We don't return -EPERM here. Yes, strange, but this is too 323 + * old behavior. 324 + */ 325 + if (attr->ia_valid & ATTR_MODE) { 326 + if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) 327 + attr->ia_valid &= ~ATTR_MODE; 328 + } 329 + 330 + if (attr->ia_valid & ATTR_SIZE) { 331 + error = exfat_block_truncate_page(inode, attr->ia_size); 332 + if (error) 333 + goto out; 334 + 335 + down_write(&EXFAT_I(inode)->truncate_lock); 336 + truncate_setsize(inode, attr->ia_size); 337 + exfat_truncate(inode, attr->ia_size); 338 + up_write(&EXFAT_I(inode)->truncate_lock); 339 + } 340 + 341 + setattr_copy(inode, attr); 342 + mark_inode_dirty(inode); 343 + 344 + out: 345 + return error; 346 + } 347 + 348 + const struct file_operations exfat_file_operations = { 349 + .llseek = generic_file_llseek, 350 + .read_iter = generic_file_read_iter, 351 + .write_iter = generic_file_write_iter, 352 + .mmap = generic_file_mmap, 353 + .fsync = generic_file_fsync, 354 + .splice_read = generic_file_splice_read, 355 + }; 356 + 357 + const struct inode_operations exfat_file_inode_operations = { 358 + .setattr = exfat_setattr, 359 + .getattr = exfat_getattr, 360 + };
+671
fs/exfat/inode.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/init.h> 7 + #include <linux/buffer_head.h> 8 + #include <linux/mpage.h> 9 + #include <linux/bio.h> 10 + #include <linux/blkdev.h> 11 + #include <linux/time.h> 12 + #include <linux/writeback.h> 13 + #include <linux/uio.h> 14 + #include <linux/random.h> 15 + #include <linux/iversion.h> 16 + 17 + #include "exfat_raw.h" 18 + #include "exfat_fs.h" 19 + 20 + static int __exfat_write_inode(struct inode *inode, int sync) 21 + { 22 + int ret = -EIO; 23 + unsigned long long on_disk_size; 24 + struct exfat_dentry *ep, *ep2; 25 + struct exfat_entry_set_cache *es = NULL; 26 + struct super_block *sb = inode->i_sb; 27 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 28 + struct exfat_inode_info *ei = EXFAT_I(inode); 29 + bool is_dir = (ei->type == TYPE_DIR) ? true : false; 30 + 31 + if (inode->i_ino == EXFAT_ROOT_INO) 32 + return 0; 33 + 34 + /* 35 + * If the indode is already unlinked, there is no need for updating it. 36 + */ 37 + if (ei->dir.dir == DIR_DELETED) 38 + return 0; 39 + 40 + if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1) 41 + return 0; 42 + 43 + exfat_set_vol_flags(sb, VOL_DIRTY); 44 + 45 + /* get the directory entry of given file or directory */ 46 + es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES, 47 + &ep); 48 + if (!es) 49 + return -EIO; 50 + ep2 = ep + 1; 51 + 52 + ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode)); 53 + 54 + /* set FILE_INFO structure using the acquired struct exfat_dentry */ 55 + exfat_set_entry_time(sbi, &ei->i_crtime, 56 + &ep->dentry.file.create_tz, 57 + &ep->dentry.file.create_time, 58 + &ep->dentry.file.create_date, 59 + &ep->dentry.file.create_time_ms); 60 + exfat_set_entry_time(sbi, &inode->i_mtime, 61 + &ep->dentry.file.modify_tz, 62 + &ep->dentry.file.modify_time, 63 + &ep->dentry.file.modify_date, 64 + &ep->dentry.file.modify_time_ms); 65 + exfat_set_entry_time(sbi, &inode->i_atime, 66 + &ep->dentry.file.access_tz, 67 + &ep->dentry.file.access_time, 68 + &ep->dentry.file.access_date, 69 + NULL); 70 + 71 + /* File size should be zero if there is no cluster allocated */ 72 + on_disk_size = i_size_read(inode); 73 + 74 + if (ei->start_clu == EXFAT_EOF_CLUSTER) 75 + on_disk_size = 0; 76 + 77 + ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size); 78 + ep2->dentry.stream.size = ep2->dentry.stream.valid_size; 79 + 80 + ret = exfat_update_dir_chksum_with_entry_set(sb, es, sync); 81 + kfree(es); 82 + return ret; 83 + } 84 + 85 + int exfat_write_inode(struct inode *inode, struct writeback_control *wbc) 86 + { 87 + int ret; 88 + 89 + mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 90 + ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 91 + mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 92 + 93 + return ret; 94 + } 95 + 96 + void exfat_sync_inode(struct inode *inode) 97 + { 98 + lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock); 99 + __exfat_write_inode(inode, 1); 100 + } 101 + 102 + /* 103 + * Input: inode, (logical) clu_offset, target allocation area 104 + * Output: errcode, cluster number 105 + * *clu = (~0), if it's unable to allocate a new cluster 106 + */ 107 + static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, 108 + unsigned int *clu, int create) 109 + { 110 + int ret, modified = false; 111 + unsigned int last_clu; 112 + struct exfat_chain new_clu; 113 + struct exfat_dentry *ep; 114 + struct exfat_entry_set_cache *es = NULL; 115 + struct super_block *sb = inode->i_sb; 116 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 117 + struct exfat_inode_info *ei = EXFAT_I(inode); 118 + unsigned int local_clu_offset = clu_offset; 119 + unsigned int num_to_be_allocated = 0, num_clusters = 0; 120 + 121 + ei->rwoffset = EXFAT_CLU_TO_B(clu_offset, sbi); 122 + 123 + if (EXFAT_I(inode)->i_size_ondisk > 0) 124 + num_clusters = 125 + EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, 126 + sbi); 127 + 128 + if (clu_offset >= num_clusters) 129 + num_to_be_allocated = clu_offset - num_clusters + 1; 130 + 131 + if (!create && (num_to_be_allocated > 0)) { 132 + *clu = EXFAT_EOF_CLUSTER; 133 + return 0; 134 + } 135 + 136 + *clu = last_clu = ei->start_clu; 137 + 138 + if (ei->flags == ALLOC_NO_FAT_CHAIN) { 139 + if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) { 140 + last_clu += clu_offset - 1; 141 + 142 + if (clu_offset == num_clusters) 143 + *clu = EXFAT_EOF_CLUSTER; 144 + else 145 + *clu += clu_offset; 146 + } 147 + } else if (ei->type == TYPE_FILE) { 148 + unsigned int fclus = 0; 149 + int err = exfat_get_cluster(inode, clu_offset, 150 + &fclus, clu, &last_clu, 1); 151 + if (err) 152 + return -EIO; 153 + 154 + clu_offset -= fclus; 155 + } else { 156 + /* hint information */ 157 + if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && 158 + ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { 159 + clu_offset -= ei->hint_bmap.off; 160 + /* hint_bmap.clu should be valid */ 161 + WARN_ON(ei->hint_bmap.clu < 2); 162 + *clu = ei->hint_bmap.clu; 163 + } 164 + 165 + while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) { 166 + last_clu = *clu; 167 + if (exfat_get_next_cluster(sb, clu)) 168 + return -EIO; 169 + clu_offset--; 170 + } 171 + } 172 + 173 + if (*clu == EXFAT_EOF_CLUSTER) { 174 + exfat_set_vol_flags(sb, VOL_DIRTY); 175 + 176 + new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ? 177 + EXFAT_EOF_CLUSTER : last_clu + 1; 178 + new_clu.size = 0; 179 + new_clu.flags = ei->flags; 180 + 181 + /* allocate a cluster */ 182 + if (num_to_be_allocated < 1) { 183 + /* Broken FAT (i_sze > allocated FAT) */ 184 + exfat_fs_error(sb, "broken FAT chain."); 185 + return -EIO; 186 + } 187 + 188 + ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu); 189 + if (ret) 190 + return ret; 191 + 192 + if (new_clu.dir == EXFAT_EOF_CLUSTER || 193 + new_clu.dir == EXFAT_FREE_CLUSTER) { 194 + exfat_fs_error(sb, 195 + "bogus cluster new allocated (last_clu : %u, new_clu : %u)", 196 + last_clu, new_clu.dir); 197 + return -EIO; 198 + } 199 + 200 + /* append to the FAT chain */ 201 + if (last_clu == EXFAT_EOF_CLUSTER) { 202 + if (new_clu.flags == ALLOC_FAT_CHAIN) 203 + ei->flags = ALLOC_FAT_CHAIN; 204 + ei->start_clu = new_clu.dir; 205 + modified = true; 206 + } else { 207 + if (new_clu.flags != ei->flags) { 208 + /* no-fat-chain bit is disabled, 209 + * so fat-chain should be synced with 210 + * alloc-bitmap 211 + */ 212 + exfat_chain_cont_cluster(sb, ei->start_clu, 213 + num_clusters); 214 + ei->flags = ALLOC_FAT_CHAIN; 215 + modified = true; 216 + } 217 + if (new_clu.flags == ALLOC_FAT_CHAIN) 218 + if (exfat_ent_set(sb, last_clu, new_clu.dir)) 219 + return -EIO; 220 + } 221 + 222 + num_clusters += num_to_be_allocated; 223 + *clu = new_clu.dir; 224 + 225 + if (ei->dir.dir != DIR_DELETED) { 226 + es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, 227 + ES_ALL_ENTRIES, &ep); 228 + if (!es) 229 + return -EIO; 230 + /* get stream entry */ 231 + ep++; 232 + 233 + /* update directory entry */ 234 + if (modified) { 235 + if (ep->dentry.stream.flags != ei->flags) 236 + ep->dentry.stream.flags = ei->flags; 237 + 238 + if (le32_to_cpu(ep->dentry.stream.start_clu) != 239 + ei->start_clu) 240 + ep->dentry.stream.start_clu = 241 + cpu_to_le32(ei->start_clu); 242 + 243 + ep->dentry.stream.valid_size = 244 + cpu_to_le64(i_size_read(inode)); 245 + ep->dentry.stream.size = 246 + ep->dentry.stream.valid_size; 247 + } 248 + 249 + if (exfat_update_dir_chksum_with_entry_set(sb, es, 250 + inode_needs_sync(inode))) 251 + return -EIO; 252 + kfree(es); 253 + 254 + } /* end of if != DIR_DELETED */ 255 + 256 + inode->i_blocks += 257 + num_to_be_allocated << sbi->sect_per_clus_bits; 258 + 259 + /* 260 + * Move *clu pointer along FAT chains (hole care) because the 261 + * caller of this function expect *clu to be the last cluster. 262 + * This only works when num_to_be_allocated >= 2, 263 + * *clu = (the first cluster of the allocated chain) => 264 + * (the last cluster of ...) 265 + */ 266 + if (ei->flags == ALLOC_NO_FAT_CHAIN) { 267 + *clu += num_to_be_allocated - 1; 268 + } else { 269 + while (num_to_be_allocated > 1) { 270 + if (exfat_get_next_cluster(sb, clu)) 271 + return -EIO; 272 + num_to_be_allocated--; 273 + } 274 + } 275 + 276 + } 277 + 278 + /* hint information */ 279 + ei->hint_bmap.off = local_clu_offset; 280 + ei->hint_bmap.clu = *clu; 281 + 282 + return 0; 283 + } 284 + 285 + static int exfat_map_new_buffer(struct exfat_inode_info *ei, 286 + struct buffer_head *bh, loff_t pos) 287 + { 288 + if (buffer_delay(bh) && pos > ei->i_size_aligned) 289 + return -EIO; 290 + set_buffer_new(bh); 291 + 292 + /* 293 + * Adjust i_size_aligned if i_size_ondisk is bigger than it. 294 + */ 295 + if (ei->i_size_ondisk > ei->i_size_aligned) 296 + ei->i_size_aligned = ei->i_size_ondisk; 297 + return 0; 298 + } 299 + 300 + static int exfat_get_block(struct inode *inode, sector_t iblock, 301 + struct buffer_head *bh_result, int create) 302 + { 303 + struct exfat_inode_info *ei = EXFAT_I(inode); 304 + struct super_block *sb = inode->i_sb; 305 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 306 + unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 307 + int err = 0; 308 + unsigned long mapped_blocks = 0; 309 + unsigned int cluster, sec_offset; 310 + sector_t last_block; 311 + sector_t phys = 0; 312 + loff_t pos; 313 + 314 + mutex_lock(&sbi->s_lock); 315 + last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb); 316 + if (iblock >= last_block && !create) 317 + goto done; 318 + 319 + /* Is this block already allocated? */ 320 + err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits, 321 + &cluster, create); 322 + if (err) { 323 + if (err != -ENOSPC) 324 + exfat_fs_error_ratelimit(sb, 325 + "failed to bmap (inode : %p iblock : %llu, err : %d)", 326 + inode, (unsigned long long)iblock, err); 327 + goto unlock_ret; 328 + } 329 + 330 + if (cluster == EXFAT_EOF_CLUSTER) 331 + goto done; 332 + 333 + /* sector offset in cluster */ 334 + sec_offset = iblock & (sbi->sect_per_clus - 1); 335 + 336 + phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset; 337 + mapped_blocks = sbi->sect_per_clus - sec_offset; 338 + max_blocks = min(mapped_blocks, max_blocks); 339 + 340 + /* Treat newly added block / cluster */ 341 + if (iblock < last_block) 342 + create = 0; 343 + 344 + if (create || buffer_delay(bh_result)) { 345 + pos = EXFAT_BLK_TO_B((iblock + 1), sb); 346 + if (ei->i_size_ondisk < pos) 347 + ei->i_size_ondisk = pos; 348 + } 349 + 350 + if (create) { 351 + err = exfat_map_new_buffer(ei, bh_result, pos); 352 + if (err) { 353 + exfat_fs_error(sb, 354 + "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n", 355 + pos, ei->i_size_aligned); 356 + goto unlock_ret; 357 + } 358 + } 359 + 360 + if (buffer_delay(bh_result)) 361 + clear_buffer_delay(bh_result); 362 + map_bh(bh_result, sb, phys); 363 + done: 364 + bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb); 365 + unlock_ret: 366 + mutex_unlock(&sbi->s_lock); 367 + return err; 368 + } 369 + 370 + static int exfat_readpage(struct file *file, struct page *page) 371 + { 372 + return mpage_readpage(page, exfat_get_block); 373 + } 374 + 375 + static int exfat_readpages(struct file *file, struct address_space *mapping, 376 + struct list_head *pages, unsigned int nr_pages) 377 + { 378 + return mpage_readpages(mapping, pages, nr_pages, exfat_get_block); 379 + } 380 + 381 + static int exfat_writepage(struct page *page, struct writeback_control *wbc) 382 + { 383 + return block_write_full_page(page, exfat_get_block, wbc); 384 + } 385 + 386 + static int exfat_writepages(struct address_space *mapping, 387 + struct writeback_control *wbc) 388 + { 389 + return mpage_writepages(mapping, wbc, exfat_get_block); 390 + } 391 + 392 + static void exfat_write_failed(struct address_space *mapping, loff_t to) 393 + { 394 + struct inode *inode = mapping->host; 395 + 396 + if (to > i_size_read(inode)) { 397 + truncate_pagecache(inode, i_size_read(inode)); 398 + exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned); 399 + } 400 + } 401 + 402 + static int exfat_write_begin(struct file *file, struct address_space *mapping, 403 + loff_t pos, unsigned int len, unsigned int flags, 404 + struct page **pagep, void **fsdata) 405 + { 406 + int ret; 407 + 408 + *pagep = NULL; 409 + ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 410 + exfat_get_block, 411 + &EXFAT_I(mapping->host)->i_size_ondisk); 412 + 413 + if (ret < 0) 414 + exfat_write_failed(mapping, pos+len); 415 + 416 + return ret; 417 + } 418 + 419 + static int exfat_write_end(struct file *file, struct address_space *mapping, 420 + loff_t pos, unsigned int len, unsigned int copied, 421 + struct page *pagep, void *fsdata) 422 + { 423 + struct inode *inode = mapping->host; 424 + struct exfat_inode_info *ei = EXFAT_I(inode); 425 + int err; 426 + 427 + err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); 428 + 429 + if (EXFAT_I(inode)->i_size_aligned < i_size_read(inode)) { 430 + exfat_fs_error(inode->i_sb, 431 + "invalid size(size(%llu) > aligned(%llu)\n", 432 + i_size_read(inode), EXFAT_I(inode)->i_size_aligned); 433 + return -EIO; 434 + } 435 + 436 + if (err < len) 437 + exfat_write_failed(mapping, pos+len); 438 + 439 + if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) { 440 + inode->i_mtime = inode->i_ctime = current_time(inode); 441 + ei->attr |= ATTR_ARCHIVE; 442 + mark_inode_dirty(inode); 443 + } 444 + 445 + return err; 446 + } 447 + 448 + static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 449 + { 450 + struct address_space *mapping = iocb->ki_filp->f_mapping; 451 + struct inode *inode = mapping->host; 452 + loff_t size = iocb->ki_pos + iov_iter_count(iter); 453 + int rw = iov_iter_rw(iter); 454 + ssize_t ret; 455 + 456 + if (rw == WRITE) { 457 + /* 458 + * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), 459 + * so we need to update the ->i_size_aligned to block boundary. 460 + * 461 + * But we must fill the remaining area or hole by nul for 462 + * updating ->i_size_aligned 463 + * 464 + * Return 0, and fallback to normal buffered write. 465 + */ 466 + if (EXFAT_I(inode)->i_size_aligned < size) 467 + return 0; 468 + } 469 + 470 + /* 471 + * Need to use the DIO_LOCKING for avoiding the race 472 + * condition of exfat_get_block() and ->truncate(). 473 + */ 474 + ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block); 475 + if (ret < 0 && (rw & WRITE)) 476 + exfat_write_failed(mapping, size); 477 + return ret; 478 + } 479 + 480 + static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block) 481 + { 482 + sector_t blocknr; 483 + 484 + /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */ 485 + down_read(&EXFAT_I(mapping->host)->truncate_lock); 486 + blocknr = generic_block_bmap(mapping, block, exfat_get_block); 487 + up_read(&EXFAT_I(mapping->host)->truncate_lock); 488 + return blocknr; 489 + } 490 + 491 + /* 492 + * exfat_block_truncate_page() zeroes out a mapping from file offset `from' 493 + * up to the end of the block which corresponds to `from'. 494 + * This is required during truncate to physically zeroout the tail end 495 + * of that block so it doesn't yield old data if the file is later grown. 496 + * Also, avoid causing failure from fsx for cases of "data past EOF" 497 + */ 498 + int exfat_block_truncate_page(struct inode *inode, loff_t from) 499 + { 500 + return block_truncate_page(inode->i_mapping, from, exfat_get_block); 501 + } 502 + 503 + static const struct address_space_operations exfat_aops = { 504 + .readpage = exfat_readpage, 505 + .readpages = exfat_readpages, 506 + .writepage = exfat_writepage, 507 + .writepages = exfat_writepages, 508 + .write_begin = exfat_write_begin, 509 + .write_end = exfat_write_end, 510 + .direct_IO = exfat_direct_IO, 511 + .bmap = exfat_aop_bmap 512 + }; 513 + 514 + static inline unsigned long exfat_hash(loff_t i_pos) 515 + { 516 + return hash_32(i_pos, EXFAT_HASH_BITS); 517 + } 518 + 519 + void exfat_hash_inode(struct inode *inode, loff_t i_pos) 520 + { 521 + struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 522 + struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 523 + 524 + spin_lock(&sbi->inode_hash_lock); 525 + EXFAT_I(inode)->i_pos = i_pos; 526 + hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head); 527 + spin_unlock(&sbi->inode_hash_lock); 528 + } 529 + 530 + void exfat_unhash_inode(struct inode *inode) 531 + { 532 + struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 533 + 534 + spin_lock(&sbi->inode_hash_lock); 535 + hlist_del_init(&EXFAT_I(inode)->i_hash_fat); 536 + EXFAT_I(inode)->i_pos = 0; 537 + spin_unlock(&sbi->inode_hash_lock); 538 + } 539 + 540 + struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) 541 + { 542 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 543 + struct exfat_inode_info *info; 544 + struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 545 + struct inode *inode = NULL; 546 + 547 + spin_lock(&sbi->inode_hash_lock); 548 + hlist_for_each_entry(info, head, i_hash_fat) { 549 + WARN_ON(info->vfs_inode.i_sb != sb); 550 + 551 + if (i_pos != info->i_pos) 552 + continue; 553 + inode = igrab(&info->vfs_inode); 554 + if (inode) 555 + break; 556 + } 557 + spin_unlock(&sbi->inode_hash_lock); 558 + return inode; 559 + } 560 + 561 + /* doesn't deal with root inode */ 562 + static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info) 563 + { 564 + struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 565 + struct exfat_inode_info *ei = EXFAT_I(inode); 566 + loff_t size = info->size; 567 + 568 + memcpy(&ei->dir, &info->dir, sizeof(struct exfat_chain)); 569 + ei->entry = info->entry; 570 + ei->attr = info->attr; 571 + ei->start_clu = info->start_clu; 572 + ei->flags = info->flags; 573 + ei->type = info->type; 574 + 575 + ei->version = 0; 576 + ei->hint_stat.eidx = 0; 577 + ei->hint_stat.clu = info->start_clu; 578 + ei->hint_femp.eidx = EXFAT_HINT_NONE; 579 + ei->rwoffset = 0; 580 + ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 581 + ei->i_pos = 0; 582 + 583 + inode->i_uid = sbi->options.fs_uid; 584 + inode->i_gid = sbi->options.fs_gid; 585 + inode_inc_iversion(inode); 586 + inode->i_generation = prandom_u32(); 587 + 588 + if (info->attr & ATTR_SUBDIR) { /* directory */ 589 + inode->i_generation &= ~1; 590 + inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 591 + inode->i_op = &exfat_dir_inode_operations; 592 + inode->i_fop = &exfat_dir_operations; 593 + set_nlink(inode, info->num_subdirs); 594 + } else { /* regular file */ 595 + inode->i_generation |= 1; 596 + inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 597 + inode->i_op = &exfat_file_inode_operations; 598 + inode->i_fop = &exfat_file_operations; 599 + inode->i_mapping->a_ops = &exfat_aops; 600 + inode->i_mapping->nrpages = 0; 601 + } 602 + 603 + i_size_write(inode, size); 604 + 605 + /* ondisk and aligned size should be aligned with block size */ 606 + if (size & (inode->i_sb->s_blocksize - 1)) { 607 + size |= (inode->i_sb->s_blocksize - 1); 608 + size++; 609 + } 610 + 611 + ei->i_size_aligned = size; 612 + ei->i_size_ondisk = size; 613 + 614 + exfat_save_attr(inode, info->attr); 615 + 616 + inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) & 617 + ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 618 + inode->i_mtime = info->mtime; 619 + inode->i_ctime = info->mtime; 620 + ei->i_crtime = info->crtime; 621 + inode->i_atime = info->atime; 622 + 623 + exfat_cache_init_inode(inode); 624 + 625 + return 0; 626 + } 627 + 628 + struct inode *exfat_build_inode(struct super_block *sb, 629 + struct exfat_dir_entry *info, loff_t i_pos) 630 + { 631 + struct inode *inode; 632 + int err; 633 + 634 + inode = exfat_iget(sb, i_pos); 635 + if (inode) 636 + goto out; 637 + inode = new_inode(sb); 638 + if (!inode) { 639 + inode = ERR_PTR(-ENOMEM); 640 + goto out; 641 + } 642 + inode->i_ino = iunique(sb, EXFAT_ROOT_INO); 643 + inode_set_iversion(inode, 1); 644 + err = exfat_fill_inode(inode, info); 645 + if (err) { 646 + iput(inode); 647 + inode = ERR_PTR(err); 648 + goto out; 649 + } 650 + exfat_hash_inode(inode, i_pos); 651 + insert_inode_hash(inode); 652 + out: 653 + return inode; 654 + } 655 + 656 + void exfat_evict_inode(struct inode *inode) 657 + { 658 + truncate_inode_pages(&inode->i_data, 0); 659 + 660 + if (!inode->i_nlink) { 661 + i_size_write(inode, 0); 662 + mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 663 + __exfat_truncate(inode, 0); 664 + mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 665 + } 666 + 667 + invalidate_inode_buffers(inode); 668 + clear_inode(inode); 669 + exfat_cache_inval_inode(inode); 670 + exfat_unhash_inode(inode); 671 + }
+163
fs/exfat/misc.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Written 1992,1993 by Werner Almesberger 4 + * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980 5 + * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru) 6 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 7 + */ 8 + 9 + #include <linux/time.h> 10 + #include <linux/fs.h> 11 + #include <linux/slab.h> 12 + #include <linux/buffer_head.h> 13 + 14 + #include "exfat_raw.h" 15 + #include "exfat_fs.h" 16 + 17 + /* 18 + * exfat_fs_error reports a file system problem that might indicate fa data 19 + * corruption/inconsistency. Depending on 'errors' mount option the 20 + * panic() is called, or error message is printed FAT and nothing is done, 21 + * or filesystem is remounted read-only (default behavior). 22 + * In case the file system is remounted read-only, it can be made writable 23 + * again by remounting it. 24 + */ 25 + void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) 26 + { 27 + struct exfat_mount_options *opts = &EXFAT_SB(sb)->options; 28 + va_list args; 29 + struct va_format vaf; 30 + 31 + if (report) { 32 + va_start(args, fmt); 33 + vaf.fmt = fmt; 34 + vaf.va = &args; 35 + exfat_msg(sb, KERN_ERR, "error, %pV\n", &vaf); 36 + va_end(args); 37 + } 38 + 39 + if (opts->errors == EXFAT_ERRORS_PANIC) { 40 + panic("exFAT-fs (%s): fs panic from previous error\n", 41 + sb->s_id); 42 + } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) { 43 + sb->s_flags |= SB_RDONLY; 44 + exfat_msg(sb, KERN_ERR, "Filesystem has been set read-only"); 45 + } 46 + } 47 + 48 + /* 49 + * exfat_msg() - print preformated EXFAT specific messages. 50 + * All logs except what uses exfat_fs_error() should be written by exfat_msg() 51 + */ 52 + void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...) 53 + { 54 + struct va_format vaf; 55 + va_list args; 56 + 57 + va_start(args, fmt); 58 + vaf.fmt = fmt; 59 + vaf.va = &args; 60 + /* level means KERN_ pacility level */ 61 + printk("%sexFAT-fs (%s): %pV\n", level, sb->s_id, &vaf); 62 + va_end(args); 63 + } 64 + 65 + #define SECS_PER_MIN (60) 66 + #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN) 67 + 68 + static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off) 69 + { 70 + if (tz_off <= 0x3F) 71 + ts->tv_sec -= TIMEZONE_SEC(tz_off); 72 + else /* 0x40 <= (tz_off & 0x7F) <=0x7F */ 73 + ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off); 74 + } 75 + 76 + /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */ 77 + void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 78 + u8 tz, __le16 time, __le16 date, u8 time_ms) 79 + { 80 + u16 t = le16_to_cpu(time); 81 + u16 d = le16_to_cpu(date); 82 + 83 + ts->tv_sec = mktime64(1980 + (d >> 9), d >> 5 & 0x000F, d & 0x001F, 84 + t >> 11, (t >> 5) & 0x003F, (t & 0x001F) << 1); 85 + 86 + 87 + /* time_ms field represent 0 ~ 199(1990 ms) */ 88 + if (time_ms) { 89 + ts->tv_sec += time_ms / 100; 90 + ts->tv_nsec = (time_ms % 100) * 10 * NSEC_PER_MSEC; 91 + } 92 + 93 + if (tz & EXFAT_TZ_VALID) 94 + /* Adjust timezone to UTC0. */ 95 + exfat_adjust_tz(ts, tz & ~EXFAT_TZ_VALID); 96 + else 97 + /* Convert from local time to UTC using time_offset. */ 98 + ts->tv_sec -= sbi->options.time_offset * SECS_PER_MIN; 99 + } 100 + 101 + /* Convert linear UNIX date to a EXFAT time/date pair. */ 102 + void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 103 + u8 *tz, __le16 *time, __le16 *date, u8 *time_ms) 104 + { 105 + struct tm tm; 106 + u16 t, d; 107 + 108 + time64_to_tm(ts->tv_sec, 0, &tm); 109 + t = (tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1); 110 + d = ((tm.tm_year - 80) << 9) | ((tm.tm_mon + 1) << 5) | tm.tm_mday; 111 + 112 + *time = cpu_to_le16(t); 113 + *date = cpu_to_le16(d); 114 + 115 + /* time_ms field represent 0 ~ 199(1990 ms) */ 116 + if (time_ms) 117 + *time_ms = (tm.tm_sec & 1) * 100 + 118 + ts->tv_nsec / (10 * NSEC_PER_MSEC); 119 + 120 + /* 121 + * Record 00h value for OffsetFromUtc field and 1 value for OffsetValid 122 + * to indicate that local time and UTC are the same. 123 + */ 124 + *tz = EXFAT_TZ_VALID; 125 + } 126 + 127 + unsigned short exfat_calc_chksum_2byte(void *data, int len, 128 + unsigned short chksum, int type) 129 + { 130 + int i; 131 + unsigned char *c = (unsigned char *)data; 132 + 133 + for (i = 0; i < len; i++, c++) { 134 + if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY)) 135 + continue; 136 + chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) + 137 + (unsigned short)*c; 138 + } 139 + return chksum; 140 + } 141 + 142 + void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync) 143 + { 144 + set_bit(EXFAT_SB_DIRTY, &EXFAT_SB(sb)->s_state); 145 + set_buffer_uptodate(bh); 146 + mark_buffer_dirty(bh); 147 + 148 + if (sync) 149 + sync_dirty_buffer(bh); 150 + } 151 + 152 + void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, 153 + unsigned int size, unsigned char flags) 154 + { 155 + ec->dir = dir; 156 + ec->size = size; 157 + ec->flags = flags; 158 + } 159 + 160 + void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec) 161 + { 162 + return exfat_chain_set(dup, ec->dir, ec->size, ec->flags); 163 + }
+1448
fs/exfat/namei.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/iversion.h> 7 + #include <linux/namei.h> 8 + #include <linux/slab.h> 9 + #include <linux/buffer_head.h> 10 + #include <linux/nls.h> 11 + 12 + #include "exfat_raw.h" 13 + #include "exfat_fs.h" 14 + 15 + static inline unsigned long exfat_d_version(struct dentry *dentry) 16 + { 17 + return (unsigned long) dentry->d_fsdata; 18 + } 19 + 20 + static inline void exfat_d_version_set(struct dentry *dentry, 21 + unsigned long version) 22 + { 23 + dentry->d_fsdata = (void *) version; 24 + } 25 + 26 + /* 27 + * If new entry was created in the parent, it could create the 8.3 alias (the 28 + * shortname of logname). So, the parent may have the negative-dentry which 29 + * matches the created 8.3 alias. 30 + * 31 + * If it happened, the negative dentry isn't actually negative anymore. So, 32 + * drop it. 33 + */ 34 + static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags) 35 + { 36 + int ret; 37 + 38 + if (flags & LOOKUP_RCU) 39 + return -ECHILD; 40 + 41 + /* 42 + * This is not negative dentry. Always valid. 43 + * 44 + * Note, rename() to existing directory entry will have ->d_inode, and 45 + * will use existing name which isn't specified name by user. 46 + * 47 + * We may be able to drop this positive dentry here. But dropping 48 + * positive dentry isn't good idea. So it's unsupported like 49 + * rename("filename", "FILENAME") for now. 50 + */ 51 + if (d_really_is_positive(dentry)) 52 + return 1; 53 + 54 + /* 55 + * Drop the negative dentry, in order to make sure to use the case 56 + * sensitive name which is specified by user if this is for creation. 57 + */ 58 + if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 59 + return 0; 60 + 61 + spin_lock(&dentry->d_lock); 62 + ret = inode_eq_iversion(d_inode(dentry->d_parent), 63 + exfat_d_version(dentry)); 64 + spin_unlock(&dentry->d_lock); 65 + return ret; 66 + } 67 + 68 + /* returns the length of a struct qstr, ignoring trailing dots */ 69 + static unsigned int exfat_striptail_len(unsigned int len, const char *name) 70 + { 71 + while (len && name[len - 1] == '.') 72 + len--; 73 + return len; 74 + } 75 + 76 + /* 77 + * Compute the hash for the exfat name corresponding to the dentry. If the name 78 + * is invalid, we leave the hash code unchanged so that the existing dentry can 79 + * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate. 80 + */ 81 + static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr) 82 + { 83 + struct super_block *sb = dentry->d_sb; 84 + struct nls_table *t = EXFAT_SB(sb)->nls_io; 85 + const unsigned char *name = qstr->name; 86 + unsigned int len = exfat_striptail_len(qstr->len, qstr->name); 87 + unsigned long hash = init_name_hash(dentry); 88 + int i, charlen; 89 + wchar_t c; 90 + 91 + for (i = 0; i < len; i += charlen) { 92 + charlen = t->char2uni(&name[i], len - i, &c); 93 + if (charlen < 0) 94 + return charlen; 95 + hash = partial_name_hash(exfat_toupper(sb, c), hash); 96 + } 97 + 98 + qstr->hash = end_name_hash(hash); 99 + return 0; 100 + } 101 + 102 + static int exfat_d_cmp(const struct dentry *dentry, unsigned int len, 103 + const char *str, const struct qstr *name) 104 + { 105 + struct super_block *sb = dentry->d_sb; 106 + struct nls_table *t = EXFAT_SB(sb)->nls_io; 107 + unsigned int alen = exfat_striptail_len(name->len, name->name); 108 + unsigned int blen = exfat_striptail_len(len, str); 109 + wchar_t c1, c2; 110 + int charlen, i; 111 + 112 + if (alen != blen) 113 + return 1; 114 + 115 + for (i = 0; i < len; i += charlen) { 116 + charlen = t->char2uni(&name->name[i], alen - i, &c1); 117 + if (charlen < 0) 118 + return 1; 119 + if (charlen != t->char2uni(&str[i], blen - i, &c2)) 120 + return 1; 121 + 122 + if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2)) 123 + return 1; 124 + } 125 + 126 + return 0; 127 + } 128 + 129 + const struct dentry_operations exfat_dentry_ops = { 130 + .d_revalidate = exfat_d_revalidate, 131 + .d_hash = exfat_d_hash, 132 + .d_compare = exfat_d_cmp, 133 + }; 134 + 135 + static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr) 136 + { 137 + struct super_block *sb = dentry->d_sb; 138 + const unsigned char *name = qstr->name; 139 + unsigned int len = exfat_striptail_len(qstr->len, qstr->name); 140 + unsigned long hash = init_name_hash(dentry); 141 + int i, charlen; 142 + unicode_t u; 143 + 144 + for (i = 0; i < len; i += charlen) { 145 + charlen = utf8_to_utf32(&name[i], len - i, &u); 146 + if (charlen < 0) 147 + return charlen; 148 + 149 + /* 150 + * Convert to UTF-16: code points above U+FFFF are encoded as 151 + * surrogate pairs. 152 + * exfat_toupper() works only for code points up to the U+FFFF. 153 + */ 154 + if (u > 0xFFFF) { 155 + hash = partial_name_hash(exfat_high_surrogate(u), hash); 156 + hash = partial_name_hash(exfat_low_surrogate(u), hash); 157 + } else { 158 + hash = partial_name_hash(exfat_toupper(sb, u), hash); 159 + } 160 + } 161 + 162 + qstr->hash = end_name_hash(hash); 163 + return 0; 164 + } 165 + 166 + static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len, 167 + const char *str, const struct qstr *name) 168 + { 169 + struct super_block *sb = dentry->d_sb; 170 + unsigned int alen = exfat_striptail_len(name->len, name->name); 171 + unsigned int blen = exfat_striptail_len(len, str); 172 + unicode_t u_a, u_b; 173 + int charlen, i; 174 + 175 + if (alen != blen) 176 + return 1; 177 + 178 + for (i = 0; i < alen; i += charlen) { 179 + charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a); 180 + if (charlen < 0) 181 + return 1; 182 + if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b)) 183 + return 1; 184 + 185 + if (u_a <= 0xFFFF && u_b <= 0xFFFF) { 186 + if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b)) 187 + return 1; 188 + } else if (u_a > 0xFFFF && u_b > 0xFFFF) { 189 + if (exfat_low_surrogate(u_a) != 190 + exfat_low_surrogate(u_b) || 191 + exfat_high_surrogate(u_a) != 192 + exfat_high_surrogate(u_b)) 193 + return 1; 194 + } else { 195 + return 1; 196 + } 197 + } 198 + 199 + return 0; 200 + } 201 + 202 + const struct dentry_operations exfat_utf8_dentry_ops = { 203 + .d_revalidate = exfat_d_revalidate, 204 + .d_hash = exfat_utf8_d_hash, 205 + .d_compare = exfat_utf8_d_cmp, 206 + }; 207 + 208 + /* used only in search empty_slot() */ 209 + #define CNT_UNUSED_NOHIT (-1) 210 + #define CNT_UNUSED_HIT (-2) 211 + /* search EMPTY CONTINUOUS "num_entries" entries */ 212 + static int exfat_search_empty_slot(struct super_block *sb, 213 + struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir, 214 + int num_entries) 215 + { 216 + int i, dentry, num_empty = 0; 217 + int dentries_per_clu; 218 + unsigned int type; 219 + struct exfat_chain clu; 220 + struct exfat_dentry *ep; 221 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 222 + struct buffer_head *bh; 223 + 224 + dentries_per_clu = sbi->dentries_per_clu; 225 + 226 + if (hint_femp->eidx != EXFAT_HINT_NONE) { 227 + dentry = hint_femp->eidx; 228 + if (num_entries <= hint_femp->count) { 229 + hint_femp->eidx = EXFAT_HINT_NONE; 230 + return dentry; 231 + } 232 + 233 + exfat_chain_dup(&clu, &hint_femp->cur); 234 + } else { 235 + exfat_chain_dup(&clu, p_dir); 236 + dentry = 0; 237 + } 238 + 239 + while (clu.dir != EXFAT_EOF_CLUSTER) { 240 + i = dentry & (dentries_per_clu - 1); 241 + 242 + for (; i < dentries_per_clu; i++, dentry++) { 243 + ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 244 + if (!ep) 245 + return -EIO; 246 + type = exfat_get_entry_type(ep); 247 + brelse(bh); 248 + 249 + if (type == TYPE_UNUSED || type == TYPE_DELETED) { 250 + num_empty++; 251 + if (hint_femp->eidx == EXFAT_HINT_NONE) { 252 + hint_femp->eidx = dentry; 253 + hint_femp->count = CNT_UNUSED_NOHIT; 254 + exfat_chain_set(&hint_femp->cur, 255 + clu.dir, clu.size, clu.flags); 256 + } 257 + 258 + if (type == TYPE_UNUSED && 259 + hint_femp->count != CNT_UNUSED_HIT) 260 + hint_femp->count = CNT_UNUSED_HIT; 261 + } else { 262 + if (hint_femp->eidx != EXFAT_HINT_NONE && 263 + hint_femp->count == CNT_UNUSED_HIT) { 264 + /* unused empty group means 265 + * an empty group which includes 266 + * unused dentry 267 + */ 268 + exfat_fs_error(sb, 269 + "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)", 270 + dentry, hint_femp->eidx, 271 + p_dir->dir, clu.dir); 272 + return -EIO; 273 + } 274 + 275 + num_empty = 0; 276 + hint_femp->eidx = EXFAT_HINT_NONE; 277 + } 278 + 279 + if (num_empty >= num_entries) { 280 + /* found and invalidate hint_femp */ 281 + hint_femp->eidx = EXFAT_HINT_NONE; 282 + return (dentry - (num_entries - 1)); 283 + } 284 + } 285 + 286 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 287 + if (--clu.size > 0) 288 + clu.dir++; 289 + else 290 + clu.dir = EXFAT_EOF_CLUSTER; 291 + } else { 292 + if (exfat_get_next_cluster(sb, &clu.dir)) 293 + return -EIO; 294 + } 295 + } 296 + 297 + return -ENOSPC; 298 + } 299 + 300 + static int exfat_check_max_dentries(struct inode *inode) 301 + { 302 + if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) { 303 + /* 304 + * exFAT spec allows a dir to grow upto 8388608(256MB) 305 + * dentries 306 + */ 307 + return -ENOSPC; 308 + } 309 + return 0; 310 + } 311 + 312 + /* find empty directory entry. 313 + * if there isn't any empty slot, expand cluster chain. 314 + */ 315 + static int exfat_find_empty_entry(struct inode *inode, 316 + struct exfat_chain *p_dir, int num_entries) 317 + { 318 + int dentry; 319 + unsigned int ret, last_clu; 320 + sector_t sector; 321 + loff_t size = 0; 322 + struct exfat_chain clu; 323 + struct exfat_dentry *ep = NULL; 324 + struct super_block *sb = inode->i_sb; 325 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 326 + struct exfat_inode_info *ei = EXFAT_I(inode); 327 + struct exfat_hint_femp hint_femp; 328 + 329 + hint_femp.eidx = EXFAT_HINT_NONE; 330 + 331 + if (ei->hint_femp.eidx != EXFAT_HINT_NONE) { 332 + memcpy(&hint_femp, &ei->hint_femp, 333 + sizeof(struct exfat_hint_femp)); 334 + ei->hint_femp.eidx = EXFAT_HINT_NONE; 335 + } 336 + 337 + while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir, 338 + num_entries)) < 0) { 339 + if (dentry == -EIO) 340 + break; 341 + 342 + if (exfat_check_max_dentries(inode)) 343 + return -ENOSPC; 344 + 345 + /* we trust p_dir->size regardless of FAT type */ 346 + if (exfat_find_last_cluster(sb, p_dir, &last_clu)) 347 + return -EIO; 348 + 349 + /* 350 + * Allocate new cluster to this directory 351 + */ 352 + exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags); 353 + 354 + /* allocate a cluster */ 355 + ret = exfat_alloc_cluster(inode, 1, &clu); 356 + if (ret) 357 + return ret; 358 + 359 + if (exfat_zeroed_cluster(inode, clu.dir)) 360 + return -EIO; 361 + 362 + /* append to the FAT chain */ 363 + if (clu.flags != p_dir->flags) { 364 + /* no-fat-chain bit is disabled, 365 + * so fat-chain should be synced with alloc-bitmap 366 + */ 367 + exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size); 368 + p_dir->flags = ALLOC_FAT_CHAIN; 369 + hint_femp.cur.flags = ALLOC_FAT_CHAIN; 370 + } 371 + 372 + if (clu.flags == ALLOC_FAT_CHAIN) 373 + if (exfat_ent_set(sb, last_clu, clu.dir)) 374 + return -EIO; 375 + 376 + if (hint_femp.eidx == EXFAT_HINT_NONE) { 377 + /* the special case that new dentry 378 + * should be allocated from the start of new cluster 379 + */ 380 + hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi); 381 + hint_femp.count = sbi->dentries_per_clu; 382 + 383 + exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags); 384 + } 385 + hint_femp.cur.size++; 386 + p_dir->size++; 387 + size = EXFAT_CLU_TO_B(p_dir->size, sbi); 388 + 389 + /* update the directory entry */ 390 + if (p_dir->dir != sbi->root_dir) { 391 + struct buffer_head *bh; 392 + 393 + ep = exfat_get_dentry(sb, 394 + &(ei->dir), ei->entry + 1, &bh, &sector); 395 + if (!ep) 396 + return -EIO; 397 + 398 + ep->dentry.stream.valid_size = cpu_to_le64(size); 399 + ep->dentry.stream.size = ep->dentry.stream.valid_size; 400 + ep->dentry.stream.flags = p_dir->flags; 401 + exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 402 + brelse(bh); 403 + if (exfat_update_dir_chksum(inode, &(ei->dir), 404 + ei->entry)) 405 + return -EIO; 406 + } 407 + 408 + /* directory inode should be updated in here */ 409 + i_size_write(inode, size); 410 + EXFAT_I(inode)->i_size_ondisk += sbi->cluster_size; 411 + EXFAT_I(inode)->i_size_aligned += sbi->cluster_size; 412 + EXFAT_I(inode)->flags = p_dir->flags; 413 + inode->i_blocks += 1 << sbi->sect_per_clus_bits; 414 + } 415 + 416 + return dentry; 417 + } 418 + 419 + /* 420 + * Name Resolution Functions : 421 + * Zero if it was successful; otherwise nonzero. 422 + */ 423 + static int __exfat_resolve_path(struct inode *inode, const unsigned char *path, 424 + struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 425 + int lookup) 426 + { 427 + int namelen; 428 + int lossy = NLS_NAME_NO_LOSSY; 429 + struct super_block *sb = inode->i_sb; 430 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 431 + struct exfat_inode_info *ei = EXFAT_I(inode); 432 + 433 + /* strip all trailing periods */ 434 + namelen = exfat_striptail_len(strlen(path), path); 435 + if (!namelen) 436 + return -ENOENT; 437 + 438 + if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE)) 439 + return -ENAMETOOLONG; 440 + 441 + /* 442 + * strip all leading spaces : 443 + * "MS windows 7" supports leading spaces. 444 + * So we should skip this preprocessing for compatibility. 445 + */ 446 + 447 + /* file name conversion : 448 + * If lookup case, we allow bad-name for compatibility. 449 + */ 450 + namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname, 451 + &lossy); 452 + if (namelen < 0) 453 + return namelen; /* return error value */ 454 + 455 + if ((lossy && !lookup) || !namelen) 456 + return -EINVAL; 457 + 458 + exfat_chain_set(p_dir, ei->start_clu, 459 + EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); 460 + 461 + return 0; 462 + } 463 + 464 + static inline int exfat_resolve_path(struct inode *inode, 465 + const unsigned char *path, struct exfat_chain *dir, 466 + struct exfat_uni_name *uni) 467 + { 468 + return __exfat_resolve_path(inode, path, dir, uni, 0); 469 + } 470 + 471 + static inline int exfat_resolve_path_for_lookup(struct inode *inode, 472 + const unsigned char *path, struct exfat_chain *dir, 473 + struct exfat_uni_name *uni) 474 + { 475 + return __exfat_resolve_path(inode, path, dir, uni, 1); 476 + } 477 + 478 + static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info) 479 + { 480 + return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff); 481 + } 482 + 483 + static int exfat_add_entry(struct inode *inode, const char *path, 484 + struct exfat_chain *p_dir, unsigned int type, 485 + struct exfat_dir_entry *info) 486 + { 487 + int ret, dentry, num_entries; 488 + struct super_block *sb = inode->i_sb; 489 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 490 + struct exfat_uni_name uniname; 491 + struct exfat_chain clu; 492 + int clu_size = 0; 493 + unsigned int start_clu = EXFAT_FREE_CLUSTER; 494 + 495 + ret = exfat_resolve_path(inode, path, p_dir, &uniname); 496 + if (ret) 497 + goto out; 498 + 499 + num_entries = exfat_calc_num_entries(&uniname); 500 + if (num_entries < 0) { 501 + ret = num_entries; 502 + goto out; 503 + } 504 + 505 + /* exfat_find_empty_entry must be called before alloc_cluster() */ 506 + dentry = exfat_find_empty_entry(inode, p_dir, num_entries); 507 + if (dentry < 0) { 508 + ret = dentry; /* -EIO or -ENOSPC */ 509 + goto out; 510 + } 511 + 512 + if (type == TYPE_DIR) { 513 + ret = exfat_alloc_new_dir(inode, &clu); 514 + if (ret) 515 + goto out; 516 + start_clu = clu.dir; 517 + clu_size = sbi->cluster_size; 518 + } 519 + 520 + /* update the directory entry */ 521 + /* fill the dos name directory entry information of the created file. 522 + * the first cluster is not determined yet. (0) 523 + */ 524 + ret = exfat_init_dir_entry(inode, p_dir, dentry, type, 525 + start_clu, clu_size); 526 + if (ret) 527 + goto out; 528 + 529 + ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname); 530 + if (ret) 531 + goto out; 532 + 533 + memcpy(&info->dir, p_dir, sizeof(struct exfat_chain)); 534 + info->entry = dentry; 535 + info->flags = ALLOC_NO_FAT_CHAIN; 536 + info->type = type; 537 + 538 + if (type == TYPE_FILE) { 539 + info->attr = ATTR_ARCHIVE; 540 + info->start_clu = EXFAT_EOF_CLUSTER; 541 + info->size = 0; 542 + info->num_subdirs = 0; 543 + } else { 544 + int count; 545 + struct exfat_chain cdir; 546 + 547 + info->attr = ATTR_SUBDIR; 548 + info->start_clu = start_clu; 549 + info->size = clu_size; 550 + 551 + exfat_chain_set(&cdir, info->start_clu, 552 + EXFAT_B_TO_CLU(info->size, sbi), info->flags); 553 + count = exfat_count_dir_entries(sb, &cdir); 554 + if (count < 0) 555 + return -EIO; 556 + info->num_subdirs = count + EXFAT_MIN_SUBDIR; 557 + } 558 + memset(&info->crtime, 0, sizeof(info->crtime)); 559 + memset(&info->mtime, 0, sizeof(info->mtime)); 560 + memset(&info->atime, 0, sizeof(info->atime)); 561 + out: 562 + return ret; 563 + } 564 + 565 + static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode, 566 + bool excl) 567 + { 568 + struct super_block *sb = dir->i_sb; 569 + struct inode *inode; 570 + struct exfat_chain cdir; 571 + struct exfat_dir_entry info; 572 + loff_t i_pos; 573 + int err; 574 + 575 + mutex_lock(&EXFAT_SB(sb)->s_lock); 576 + exfat_set_vol_flags(sb, VOL_DIRTY); 577 + err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE, 578 + &info); 579 + exfat_set_vol_flags(sb, VOL_CLEAN); 580 + if (err) 581 + goto unlock; 582 + 583 + inode_inc_iversion(dir); 584 + dir->i_ctime = dir->i_mtime = current_time(dir); 585 + if (IS_DIRSYNC(dir)) 586 + exfat_sync_inode(dir); 587 + else 588 + mark_inode_dirty(dir); 589 + 590 + i_pos = exfat_make_i_pos(&info); 591 + inode = exfat_build_inode(sb, &info, i_pos); 592 + if (IS_ERR(inode)) 593 + goto unlock; 594 + 595 + inode_inc_iversion(inode); 596 + inode->i_mtime = inode->i_atime = inode->i_ctime = 597 + EXFAT_I(inode)->i_crtime = current_time(inode); 598 + /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 599 + 600 + d_instantiate(dentry, inode); 601 + unlock: 602 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 603 + return err; 604 + } 605 + 606 + /* lookup a file */ 607 + static int exfat_find(struct inode *dir, struct qstr *qname, 608 + struct exfat_dir_entry *info) 609 + { 610 + int ret, dentry, num_entries, count; 611 + struct exfat_chain cdir; 612 + struct exfat_uni_name uni_name; 613 + struct exfat_dentry *ep, *ep2; 614 + struct exfat_entry_set_cache *es = NULL; 615 + struct super_block *sb = dir->i_sb; 616 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 617 + struct exfat_inode_info *ei = EXFAT_I(dir); 618 + 619 + if (qname->len == 0) 620 + return -ENOENT; 621 + 622 + /* check the validity of directory name in the given pathname */ 623 + ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name); 624 + if (ret) 625 + return ret; 626 + 627 + num_entries = exfat_calc_num_entries(&uni_name); 628 + if (num_entries < 0) 629 + return num_entries; 630 + 631 + /* check the validation of hint_stat and initialize it if required */ 632 + if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) { 633 + ei->hint_stat.clu = cdir.dir; 634 + ei->hint_stat.eidx = 0; 635 + ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff); 636 + ei->hint_femp.eidx = EXFAT_HINT_NONE; 637 + } 638 + 639 + /* search the file name for directories */ 640 + dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, 641 + num_entries, TYPE_ALL); 642 + 643 + if ((dentry < 0) && (dentry != -EEXIST)) 644 + return dentry; /* -error value */ 645 + 646 + memcpy(&info->dir, &cdir.dir, sizeof(struct exfat_chain)); 647 + info->entry = dentry; 648 + info->num_subdirs = 0; 649 + 650 + /* root directory itself */ 651 + if (unlikely(dentry == -EEXIST)) { 652 + int num_clu = 0; 653 + 654 + info->type = TYPE_DIR; 655 + info->attr = ATTR_SUBDIR; 656 + info->flags = ALLOC_FAT_CHAIN; 657 + info->start_clu = sbi->root_dir; 658 + memset(&info->crtime, 0, sizeof(info->crtime)); 659 + memset(&info->mtime, 0, sizeof(info->mtime)); 660 + memset(&info->atime, 0, sizeof(info->atime)); 661 + 662 + exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 663 + if (exfat_count_num_clusters(sb, &cdir, &num_clu)) 664 + return -EIO; 665 + info->size = num_clu << sbi->cluster_size_bits; 666 + 667 + count = exfat_count_dir_entries(sb, &cdir); 668 + if (count < 0) 669 + return -EIO; 670 + 671 + info->num_subdirs = count; 672 + } else { 673 + es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES, &ep); 674 + if (!es) 675 + return -EIO; 676 + ep2 = ep + 1; 677 + 678 + info->type = exfat_get_entry_type(ep); 679 + info->attr = le16_to_cpu(ep->dentry.file.attr); 680 + info->size = le64_to_cpu(ep2->dentry.stream.valid_size); 681 + if ((info->type == TYPE_FILE) && (info->size == 0)) { 682 + info->flags = ALLOC_NO_FAT_CHAIN; 683 + info->start_clu = EXFAT_EOF_CLUSTER; 684 + } else { 685 + info->flags = ep2->dentry.stream.flags; 686 + info->start_clu = 687 + le32_to_cpu(ep2->dentry.stream.start_clu); 688 + } 689 + 690 + if (ei->start_clu == EXFAT_FREE_CLUSTER) { 691 + exfat_fs_error(sb, 692 + "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)", 693 + i_size_read(dir), ei->dir.dir, ei->entry); 694 + return -EIO; 695 + } 696 + 697 + exfat_get_entry_time(sbi, &info->crtime, 698 + ep->dentry.file.create_tz, 699 + ep->dentry.file.create_time, 700 + ep->dentry.file.create_date, 701 + ep->dentry.file.create_time_ms); 702 + exfat_get_entry_time(sbi, &info->mtime, 703 + ep->dentry.file.modify_tz, 704 + ep->dentry.file.modify_time, 705 + ep->dentry.file.modify_date, 706 + ep->dentry.file.modify_time_ms); 707 + exfat_get_entry_time(sbi, &info->atime, 708 + ep->dentry.file.access_tz, 709 + ep->dentry.file.access_time, 710 + ep->dentry.file.access_date, 711 + 0); 712 + kfree(es); 713 + 714 + if (info->type == TYPE_DIR) { 715 + exfat_chain_set(&cdir, info->start_clu, 716 + EXFAT_B_TO_CLU(info->size, sbi), info->flags); 717 + count = exfat_count_dir_entries(sb, &cdir); 718 + if (count < 0) 719 + return -EIO; 720 + 721 + info->num_subdirs = count + EXFAT_MIN_SUBDIR; 722 + } 723 + } 724 + return 0; 725 + } 726 + 727 + static int exfat_d_anon_disconn(struct dentry *dentry) 728 + { 729 + return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED); 730 + } 731 + 732 + static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry, 733 + unsigned int flags) 734 + { 735 + struct super_block *sb = dir->i_sb; 736 + struct inode *inode; 737 + struct dentry *alias; 738 + struct exfat_dir_entry info; 739 + int err; 740 + loff_t i_pos; 741 + mode_t i_mode; 742 + 743 + mutex_lock(&EXFAT_SB(sb)->s_lock); 744 + err = exfat_find(dir, &dentry->d_name, &info); 745 + if (err) { 746 + if (err == -ENOENT) { 747 + inode = NULL; 748 + goto out; 749 + } 750 + goto unlock; 751 + } 752 + 753 + i_pos = exfat_make_i_pos(&info); 754 + inode = exfat_build_inode(sb, &info, i_pos); 755 + if (IS_ERR(inode)) { 756 + err = PTR_ERR(inode); 757 + goto unlock; 758 + } 759 + 760 + i_mode = inode->i_mode; 761 + alias = d_find_alias(inode); 762 + 763 + /* 764 + * Checking "alias->d_parent == dentry->d_parent" to make sure 765 + * FS is not corrupted (especially double linked dir). 766 + */ 767 + if (alias && alias->d_parent == dentry->d_parent && 768 + !exfat_d_anon_disconn(alias)) { 769 + 770 + /* 771 + * Unhashed alias is able to exist because of revalidate() 772 + * called by lookup_fast. You can easily make this status 773 + * by calling create and lookup concurrently 774 + * In such case, we reuse an alias instead of new dentry 775 + */ 776 + if (d_unhashed(alias)) { 777 + WARN_ON(alias->d_name.hash_len != 778 + dentry->d_name.hash_len); 779 + exfat_msg(sb, KERN_INFO, 780 + "rehashed a dentry(%p) in read lookup", alias); 781 + d_drop(dentry); 782 + d_rehash(alias); 783 + } else if (!S_ISDIR(i_mode)) { 784 + /* 785 + * This inode has non anonymous-DCACHE_DISCONNECTED 786 + * dentry. This means, the user did ->lookup() by an 787 + * another name (longname vs 8.3 alias of it) in past. 788 + * 789 + * Switch to new one for reason of locality if possible. 790 + */ 791 + d_move(alias, dentry); 792 + } 793 + iput(inode); 794 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 795 + return alias; 796 + } 797 + dput(alias); 798 + out: 799 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 800 + if (!inode) 801 + exfat_d_version_set(dentry, inode_query_iversion(dir)); 802 + 803 + return d_splice_alias(inode, dentry); 804 + unlock: 805 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 806 + return ERR_PTR(err); 807 + } 808 + 809 + /* remove an entry, BUT don't truncate */ 810 + static int exfat_unlink(struct inode *dir, struct dentry *dentry) 811 + { 812 + struct exfat_chain cdir; 813 + struct exfat_dentry *ep; 814 + struct super_block *sb = dir->i_sb; 815 + struct inode *inode = dentry->d_inode; 816 + struct exfat_inode_info *ei = EXFAT_I(inode); 817 + struct buffer_head *bh; 818 + sector_t sector; 819 + int num_entries, entry, err = 0; 820 + 821 + mutex_lock(&EXFAT_SB(sb)->s_lock); 822 + exfat_chain_dup(&cdir, &ei->dir); 823 + entry = ei->entry; 824 + if (ei->dir.dir == DIR_DELETED) { 825 + exfat_msg(sb, KERN_ERR, "abnormal access to deleted dentry"); 826 + err = -ENOENT; 827 + goto unlock; 828 + } 829 + 830 + ep = exfat_get_dentry(sb, &cdir, entry, &bh, &sector); 831 + if (!ep) { 832 + err = -EIO; 833 + goto unlock; 834 + } 835 + num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep); 836 + if (num_entries < 0) { 837 + err = -EIO; 838 + brelse(bh); 839 + goto unlock; 840 + } 841 + num_entries++; 842 + brelse(bh); 843 + 844 + exfat_set_vol_flags(sb, VOL_DIRTY); 845 + /* update the directory entry */ 846 + if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) { 847 + err = -EIO; 848 + goto unlock; 849 + } 850 + 851 + /* This doesn't modify ei */ 852 + ei->dir.dir = DIR_DELETED; 853 + exfat_set_vol_flags(sb, VOL_CLEAN); 854 + 855 + inode_inc_iversion(dir); 856 + dir->i_mtime = dir->i_atime = current_time(dir); 857 + if (IS_DIRSYNC(dir)) 858 + exfat_sync_inode(dir); 859 + else 860 + mark_inode_dirty(dir); 861 + 862 + clear_nlink(inode); 863 + inode->i_mtime = inode->i_atime = current_time(inode); 864 + exfat_unhash_inode(inode); 865 + exfat_d_version_set(dentry, inode_query_iversion(dir)); 866 + unlock: 867 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 868 + return err; 869 + } 870 + 871 + static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 872 + { 873 + struct super_block *sb = dir->i_sb; 874 + struct inode *inode; 875 + struct exfat_dir_entry info; 876 + struct exfat_chain cdir; 877 + loff_t i_pos; 878 + int err; 879 + 880 + mutex_lock(&EXFAT_SB(sb)->s_lock); 881 + exfat_set_vol_flags(sb, VOL_DIRTY); 882 + err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR, 883 + &info); 884 + exfat_set_vol_flags(sb, VOL_CLEAN); 885 + if (err) 886 + goto unlock; 887 + 888 + inode_inc_iversion(dir); 889 + dir->i_ctime = dir->i_mtime = current_time(dir); 890 + if (IS_DIRSYNC(dir)) 891 + exfat_sync_inode(dir); 892 + else 893 + mark_inode_dirty(dir); 894 + inc_nlink(dir); 895 + 896 + i_pos = exfat_make_i_pos(&info); 897 + inode = exfat_build_inode(sb, &info, i_pos); 898 + if (IS_ERR(inode)) { 899 + err = PTR_ERR(inode); 900 + goto unlock; 901 + } 902 + 903 + inode_inc_iversion(inode); 904 + inode->i_mtime = inode->i_atime = inode->i_ctime = 905 + EXFAT_I(inode)->i_crtime = current_time(inode); 906 + /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 907 + 908 + d_instantiate(dentry, inode); 909 + 910 + unlock: 911 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 912 + return err; 913 + } 914 + 915 + static int exfat_check_dir_empty(struct super_block *sb, 916 + struct exfat_chain *p_dir) 917 + { 918 + int i, dentries_per_clu; 919 + unsigned int type; 920 + struct exfat_chain clu; 921 + struct exfat_dentry *ep; 922 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 923 + struct buffer_head *bh; 924 + 925 + dentries_per_clu = sbi->dentries_per_clu; 926 + 927 + exfat_chain_dup(&clu, p_dir); 928 + 929 + while (clu.dir != EXFAT_EOF_CLUSTER) { 930 + for (i = 0; i < dentries_per_clu; i++) { 931 + ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 932 + if (!ep) 933 + return -EIO; 934 + type = exfat_get_entry_type(ep); 935 + brelse(bh); 936 + if (type == TYPE_UNUSED) 937 + return 0; 938 + 939 + if (type != TYPE_FILE && type != TYPE_DIR) 940 + continue; 941 + 942 + return -ENOTEMPTY; 943 + } 944 + 945 + if (clu.flags == ALLOC_NO_FAT_CHAIN) { 946 + if (--clu.size > 0) 947 + clu.dir++; 948 + else 949 + clu.dir = EXFAT_EOF_CLUSTER; 950 + } else { 951 + if (exfat_get_next_cluster(sb, &(clu.dir))) 952 + return -EIO; 953 + } 954 + } 955 + 956 + return 0; 957 + } 958 + 959 + static int exfat_rmdir(struct inode *dir, struct dentry *dentry) 960 + { 961 + struct inode *inode = dentry->d_inode; 962 + struct exfat_dentry *ep; 963 + struct exfat_chain cdir, clu_to_free; 964 + struct super_block *sb = inode->i_sb; 965 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 966 + struct exfat_inode_info *ei = EXFAT_I(inode); 967 + struct buffer_head *bh; 968 + sector_t sector; 969 + int num_entries, entry, err; 970 + 971 + mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 972 + 973 + exfat_chain_dup(&cdir, &ei->dir); 974 + entry = ei->entry; 975 + 976 + if (ei->dir.dir == DIR_DELETED) { 977 + exfat_msg(sb, KERN_ERR, "abnormal access to deleted dentry"); 978 + err = -ENOENT; 979 + goto unlock; 980 + } 981 + 982 + exfat_set_vol_flags(sb, VOL_DIRTY); 983 + exfat_chain_set(&clu_to_free, ei->start_clu, 984 + EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags); 985 + 986 + err = exfat_check_dir_empty(sb, &clu_to_free); 987 + if (err) { 988 + if (err == -EIO) 989 + exfat_msg(sb, KERN_ERR, 990 + "failed to exfat_check_dir_empty : err(%d)", 991 + err); 992 + goto unlock; 993 + } 994 + 995 + ep = exfat_get_dentry(sb, &cdir, entry, &bh, &sector); 996 + if (!ep) { 997 + err = -EIO; 998 + goto unlock; 999 + } 1000 + 1001 + num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep); 1002 + if (num_entries < 0) { 1003 + err = -EIO; 1004 + brelse(bh); 1005 + goto unlock; 1006 + } 1007 + num_entries++; 1008 + brelse(bh); 1009 + 1010 + err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries); 1011 + if (err) { 1012 + exfat_msg(sb, KERN_ERR, 1013 + "failed to exfat_remove_entries : err(%d)", 1014 + err); 1015 + goto unlock; 1016 + } 1017 + ei->dir.dir = DIR_DELETED; 1018 + exfat_set_vol_flags(sb, VOL_CLEAN); 1019 + 1020 + inode_inc_iversion(dir); 1021 + dir->i_mtime = dir->i_atime = current_time(dir); 1022 + if (IS_DIRSYNC(dir)) 1023 + exfat_sync_inode(dir); 1024 + else 1025 + mark_inode_dirty(dir); 1026 + drop_nlink(dir); 1027 + 1028 + clear_nlink(inode); 1029 + inode->i_mtime = inode->i_atime = current_time(inode); 1030 + exfat_unhash_inode(inode); 1031 + exfat_d_version_set(dentry, inode_query_iversion(dir)); 1032 + unlock: 1033 + mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 1034 + return err; 1035 + } 1036 + 1037 + static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir, 1038 + int oldentry, struct exfat_uni_name *p_uniname, 1039 + struct exfat_inode_info *ei) 1040 + { 1041 + int ret, num_old_entries, num_new_entries; 1042 + sector_t sector_old, sector_new; 1043 + struct exfat_dentry *epold, *epnew; 1044 + struct super_block *sb = inode->i_sb; 1045 + struct buffer_head *new_bh, *old_bh; 1046 + int sync = IS_DIRSYNC(inode); 1047 + 1048 + epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, &sector_old); 1049 + if (!epold) 1050 + return -EIO; 1051 + 1052 + num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold); 1053 + if (num_old_entries < 0) 1054 + return -EIO; 1055 + num_old_entries++; 1056 + 1057 + num_new_entries = exfat_calc_num_entries(p_uniname); 1058 + if (num_new_entries < 0) 1059 + return num_new_entries; 1060 + 1061 + if (num_old_entries < num_new_entries) { 1062 + int newentry; 1063 + 1064 + newentry = 1065 + exfat_find_empty_entry(inode, p_dir, num_new_entries); 1066 + if (newentry < 0) 1067 + return newentry; /* -EIO or -ENOSPC */ 1068 + 1069 + epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh, 1070 + &sector_new); 1071 + if (!epnew) 1072 + return -EIO; 1073 + 1074 + memcpy(epnew, epold, DENTRY_SIZE); 1075 + if (exfat_get_entry_type(epnew) == TYPE_FILE) { 1076 + epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); 1077 + ei->attr |= ATTR_ARCHIVE; 1078 + } 1079 + exfat_update_bh(sb, new_bh, sync); 1080 + brelse(old_bh); 1081 + brelse(new_bh); 1082 + 1083 + epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh, 1084 + &sector_old); 1085 + epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh, 1086 + &sector_new); 1087 + if (!epold || !epnew) 1088 + return -EIO; 1089 + 1090 + memcpy(epnew, epold, DENTRY_SIZE); 1091 + exfat_update_bh(sb, new_bh, sync); 1092 + brelse(old_bh); 1093 + brelse(new_bh); 1094 + 1095 + ret = exfat_init_ext_entry(inode, p_dir, newentry, 1096 + num_new_entries, p_uniname); 1097 + if (ret) 1098 + return ret; 1099 + 1100 + exfat_remove_entries(inode, p_dir, oldentry, 0, 1101 + num_old_entries); 1102 + ei->entry = newentry; 1103 + } else { 1104 + if (exfat_get_entry_type(epold) == TYPE_FILE) { 1105 + epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); 1106 + ei->attr |= ATTR_ARCHIVE; 1107 + } 1108 + exfat_update_bh(sb, old_bh, sync); 1109 + brelse(old_bh); 1110 + ret = exfat_init_ext_entry(inode, p_dir, oldentry, 1111 + num_new_entries, p_uniname); 1112 + if (ret) 1113 + return ret; 1114 + 1115 + exfat_remove_entries(inode, p_dir, oldentry, num_new_entries, 1116 + num_old_entries); 1117 + } 1118 + return 0; 1119 + } 1120 + 1121 + static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir, 1122 + int oldentry, struct exfat_chain *p_newdir, 1123 + struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei) 1124 + { 1125 + int ret, newentry, num_new_entries, num_old_entries; 1126 + sector_t sector_mov, sector_new; 1127 + struct exfat_dentry *epmov, *epnew; 1128 + struct super_block *sb = inode->i_sb; 1129 + struct buffer_head *mov_bh, *new_bh; 1130 + 1131 + epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, &sector_mov); 1132 + if (!epmov) 1133 + return -EIO; 1134 + 1135 + /* check if the source and target directory is the same */ 1136 + if (exfat_get_entry_type(epmov) == TYPE_DIR && 1137 + le32_to_cpu(epmov->dentry.stream.start_clu) == p_newdir->dir) 1138 + return -EINVAL; 1139 + 1140 + num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry, 1141 + epmov); 1142 + if (num_old_entries < 0) 1143 + return -EIO; 1144 + num_old_entries++; 1145 + 1146 + num_new_entries = exfat_calc_num_entries(p_uniname); 1147 + if (num_new_entries < 0) 1148 + return num_new_entries; 1149 + 1150 + newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries); 1151 + if (newentry < 0) 1152 + return newentry; /* -EIO or -ENOSPC */ 1153 + 1154 + epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, &sector_new); 1155 + if (!epnew) 1156 + return -EIO; 1157 + 1158 + memcpy(epnew, epmov, DENTRY_SIZE); 1159 + if (exfat_get_entry_type(epnew) == TYPE_FILE) { 1160 + epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); 1161 + ei->attr |= ATTR_ARCHIVE; 1162 + } 1163 + exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode)); 1164 + brelse(mov_bh); 1165 + brelse(new_bh); 1166 + 1167 + epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh, 1168 + &sector_mov); 1169 + epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh, 1170 + &sector_new); 1171 + if (!epmov || !epnew) 1172 + return -EIO; 1173 + 1174 + memcpy(epnew, epmov, DENTRY_SIZE); 1175 + exfat_update_bh(sb, new_bh, IS_DIRSYNC(inode)); 1176 + brelse(mov_bh); 1177 + brelse(new_bh); 1178 + 1179 + ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries, 1180 + p_uniname); 1181 + if (ret) 1182 + return ret; 1183 + 1184 + exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries); 1185 + 1186 + exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size, 1187 + p_newdir->flags); 1188 + 1189 + ei->entry = newentry; 1190 + return 0; 1191 + } 1192 + 1193 + static void exfat_update_parent_info(struct exfat_inode_info *ei, 1194 + struct inode *parent_inode) 1195 + { 1196 + struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb); 1197 + struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode); 1198 + loff_t parent_isize = i_size_read(parent_inode); 1199 + 1200 + /* 1201 + * the problem that struct exfat_inode_info caches wrong parent info. 1202 + * 1203 + * because of flag-mismatch of ei->dir, 1204 + * there is abnormal traversing cluster chain. 1205 + */ 1206 + if (unlikely(parent_ei->flags != ei->dir.flags || 1207 + parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) || 1208 + parent_ei->start_clu != ei->dir.dir)) { 1209 + exfat_chain_set(&ei->dir, parent_ei->start_clu, 1210 + EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi), 1211 + parent_ei->flags); 1212 + } 1213 + } 1214 + 1215 + /* rename or move a old file into a new file */ 1216 + static int __exfat_rename(struct inode *old_parent_inode, 1217 + struct exfat_inode_info *ei, struct inode *new_parent_inode, 1218 + struct dentry *new_dentry) 1219 + { 1220 + int ret; 1221 + int dentry; 1222 + struct exfat_chain olddir, newdir; 1223 + struct exfat_chain *p_dir = NULL; 1224 + struct exfat_uni_name uni_name; 1225 + struct exfat_dentry *ep; 1226 + struct super_block *sb = old_parent_inode->i_sb; 1227 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 1228 + const unsigned char *new_path = new_dentry->d_name.name; 1229 + struct inode *new_inode = new_dentry->d_inode; 1230 + int num_entries; 1231 + struct exfat_inode_info *new_ei = NULL; 1232 + unsigned int new_entry_type = TYPE_UNUSED; 1233 + int new_entry = 0; 1234 + struct buffer_head *old_bh, *new_bh = NULL; 1235 + 1236 + /* check the validity of pointer parameters */ 1237 + if (new_path == NULL || strlen(new_path) == 0) 1238 + return -EINVAL; 1239 + 1240 + if (ei->dir.dir == DIR_DELETED) { 1241 + exfat_msg(sb, KERN_ERR, 1242 + "abnormal access to deleted source dentry"); 1243 + return -ENOENT; 1244 + } 1245 + 1246 + exfat_update_parent_info(ei, old_parent_inode); 1247 + 1248 + exfat_chain_dup(&olddir, &ei->dir); 1249 + dentry = ei->entry; 1250 + 1251 + ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL); 1252 + if (!ep) { 1253 + ret = -EIO; 1254 + goto out; 1255 + } 1256 + brelse(old_bh); 1257 + 1258 + /* check whether new dir is existing directory and empty */ 1259 + if (new_inode) { 1260 + ret = -EIO; 1261 + new_ei = EXFAT_I(new_inode); 1262 + 1263 + if (new_ei->dir.dir == DIR_DELETED) { 1264 + exfat_msg(sb, KERN_ERR, 1265 + "abnormal access to deleted target dentry"); 1266 + goto out; 1267 + } 1268 + 1269 + exfat_update_parent_info(new_ei, new_parent_inode); 1270 + 1271 + p_dir = &(new_ei->dir); 1272 + new_entry = new_ei->entry; 1273 + ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL); 1274 + if (!ep) 1275 + goto out; 1276 + 1277 + new_entry_type = exfat_get_entry_type(ep); 1278 + brelse(new_bh); 1279 + 1280 + /* if new_inode exists, update ei */ 1281 + if (new_entry_type == TYPE_DIR) { 1282 + struct exfat_chain new_clu; 1283 + 1284 + new_clu.dir = new_ei->start_clu; 1285 + new_clu.size = 1286 + EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), 1287 + sbi); 1288 + new_clu.flags = new_ei->flags; 1289 + 1290 + ret = exfat_check_dir_empty(sb, &new_clu); 1291 + if (ret) 1292 + goto out; 1293 + } 1294 + } 1295 + 1296 + /* check the validity of directory name in the given new pathname */ 1297 + ret = exfat_resolve_path(new_parent_inode, new_path, &newdir, 1298 + &uni_name); 1299 + if (ret) 1300 + goto out; 1301 + 1302 + exfat_set_vol_flags(sb, VOL_DIRTY); 1303 + 1304 + if (olddir.dir == newdir.dir) 1305 + ret = exfat_rename_file(new_parent_inode, &olddir, dentry, 1306 + &uni_name, ei); 1307 + else 1308 + ret = exfat_move_file(new_parent_inode, &olddir, dentry, 1309 + &newdir, &uni_name, ei); 1310 + 1311 + if (!ret && new_inode) { 1312 + /* delete entries of new_dir */ 1313 + ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL); 1314 + if (!ep) { 1315 + ret = -EIO; 1316 + goto del_out; 1317 + } 1318 + 1319 + num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep); 1320 + if (num_entries < 0) { 1321 + ret = -EIO; 1322 + goto del_out; 1323 + } 1324 + brelse(new_bh); 1325 + 1326 + if (exfat_remove_entries(new_inode, p_dir, new_entry, 0, 1327 + num_entries + 1)) { 1328 + ret = -EIO; 1329 + goto del_out; 1330 + } 1331 + 1332 + /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */ 1333 + if (new_entry_type == TYPE_DIR) { 1334 + /* new_ei, new_clu_to_free */ 1335 + struct exfat_chain new_clu_to_free; 1336 + 1337 + exfat_chain_set(&new_clu_to_free, new_ei->start_clu, 1338 + EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), 1339 + sbi), new_ei->flags); 1340 + 1341 + if (exfat_free_cluster(new_inode, &new_clu_to_free)) { 1342 + /* just set I/O error only */ 1343 + ret = -EIO; 1344 + } 1345 + 1346 + i_size_write(new_inode, 0); 1347 + new_ei->start_clu = EXFAT_EOF_CLUSTER; 1348 + new_ei->flags = ALLOC_NO_FAT_CHAIN; 1349 + } 1350 + del_out: 1351 + /* Update new_inode ei 1352 + * Prevent syncing removed new_inode 1353 + * (new_ei is already initialized above code ("if (new_inode)") 1354 + */ 1355 + new_ei->dir.dir = DIR_DELETED; 1356 + } 1357 + exfat_set_vol_flags(sb, VOL_CLEAN); 1358 + out: 1359 + return ret; 1360 + } 1361 + 1362 + static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry, 1363 + struct inode *new_dir, struct dentry *new_dentry, 1364 + unsigned int flags) 1365 + { 1366 + struct inode *old_inode, *new_inode; 1367 + struct super_block *sb = old_dir->i_sb; 1368 + loff_t i_pos; 1369 + int err; 1370 + 1371 + /* 1372 + * The VFS already checks for existence, so for local filesystems 1373 + * the RENAME_NOREPLACE implementation is equivalent to plain rename. 1374 + * Don't support any other flags 1375 + */ 1376 + if (flags & ~RENAME_NOREPLACE) 1377 + return -EINVAL; 1378 + 1379 + mutex_lock(&EXFAT_SB(sb)->s_lock); 1380 + old_inode = old_dentry->d_inode; 1381 + new_inode = new_dentry->d_inode; 1382 + 1383 + err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry); 1384 + if (err) 1385 + goto unlock; 1386 + 1387 + inode_inc_iversion(new_dir); 1388 + new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = 1389 + EXFAT_I(new_dir)->i_crtime = current_time(new_dir); 1390 + if (IS_DIRSYNC(new_dir)) 1391 + exfat_sync_inode(new_dir); 1392 + else 1393 + mark_inode_dirty(new_dir); 1394 + 1395 + i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) | 1396 + (EXFAT_I(old_inode)->entry & 0xffffffff); 1397 + exfat_unhash_inode(old_inode); 1398 + exfat_hash_inode(old_inode, i_pos); 1399 + if (IS_DIRSYNC(new_dir)) 1400 + exfat_sync_inode(old_inode); 1401 + else 1402 + mark_inode_dirty(old_inode); 1403 + 1404 + if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { 1405 + drop_nlink(old_dir); 1406 + if (!new_inode) 1407 + inc_nlink(new_dir); 1408 + } 1409 + 1410 + inode_inc_iversion(old_dir); 1411 + old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir); 1412 + if (IS_DIRSYNC(old_dir)) 1413 + exfat_sync_inode(old_dir); 1414 + else 1415 + mark_inode_dirty(old_dir); 1416 + 1417 + if (new_inode) { 1418 + exfat_unhash_inode(new_inode); 1419 + 1420 + /* skip drop_nlink if new_inode already has been dropped */ 1421 + if (new_inode->i_nlink) { 1422 + drop_nlink(new_inode); 1423 + if (S_ISDIR(new_inode->i_mode)) 1424 + drop_nlink(new_inode); 1425 + } else { 1426 + exfat_msg(sb, KERN_WARNING, 1427 + "abnormal access to an inode dropped"); 1428 + WARN_ON(new_inode->i_nlink == 0); 1429 + } 1430 + new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime = 1431 + current_time(new_inode); 1432 + } 1433 + 1434 + unlock: 1435 + mutex_unlock(&EXFAT_SB(sb)->s_lock); 1436 + return err; 1437 + } 1438 + 1439 + const struct inode_operations exfat_dir_inode_operations = { 1440 + .create = exfat_create, 1441 + .lookup = exfat_lookup, 1442 + .unlink = exfat_unlink, 1443 + .mkdir = exfat_mkdir, 1444 + .rmdir = exfat_rmdir, 1445 + .rename = exfat_rename, 1446 + .setattr = exfat_setattr, 1447 + .getattr = exfat_getattr, 1448 + };
+831
fs/exfat/nls.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/string.h> 7 + #include <linux/slab.h> 8 + #include <linux/buffer_head.h> 9 + #include <asm/unaligned.h> 10 + 11 + #include "exfat_raw.h" 12 + #include "exfat_fs.h" 13 + 14 + /* Upcase tabel macro */ 15 + #define EXFAT_NUM_UPCASE (2918) 16 + #define UTBL_COUNT (0x10000) 17 + 18 + /* 19 + * Upcase table in compressed format (7.2.5.1 Recommended Up-case Table 20 + * in exfat specification, See: 21 + * https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification). 22 + */ 23 + static const unsigned short uni_def_upcase[EXFAT_NUM_UPCASE] = { 24 + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 25 + 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 26 + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 27 + 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, 28 + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 29 + 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f, 30 + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 31 + 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, 32 + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 33 + 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 34 + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 35 + 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 36 + 0x0060, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 37 + 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 38 + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 39 + 0x0058, 0x0059, 0x005a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f, 40 + 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 41 + 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, 42 + 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 43 + 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, 44 + 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 45 + 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, 46 + 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, 47 + 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, 48 + 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 49 + 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, 50 + 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, 51 + 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, 52 + 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 53 + 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, 54 + 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00f7, 55 + 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x0178, 56 + 0x0100, 0x0100, 0x0102, 0x0102, 0x0104, 0x0104, 0x0106, 0x0106, 57 + 0x0108, 0x0108, 0x010a, 0x010a, 0x010c, 0x010c, 0x010e, 0x010e, 58 + 0x0110, 0x0110, 0x0112, 0x0112, 0x0114, 0x0114, 0x0116, 0x0116, 59 + 0x0118, 0x0118, 0x011a, 0x011a, 0x011c, 0x011c, 0x011e, 0x011e, 60 + 0x0120, 0x0120, 0x0122, 0x0122, 0x0124, 0x0124, 0x0126, 0x0126, 61 + 0x0128, 0x0128, 0x012a, 0x012a, 0x012c, 0x012c, 0x012e, 0x012e, 62 + 0x0130, 0x0131, 0x0132, 0x0132, 0x0134, 0x0134, 0x0136, 0x0136, 63 + 0x0138, 0x0139, 0x0139, 0x013b, 0x013b, 0x013d, 0x013d, 0x013f, 64 + 0x013f, 0x0141, 0x0141, 0x0143, 0x0143, 0x0145, 0x0145, 0x0147, 65 + 0x0147, 0x0149, 0x014a, 0x014a, 0x014c, 0x014c, 0x014e, 0x014e, 66 + 0x0150, 0x0150, 0x0152, 0x0152, 0x0154, 0x0154, 0x0156, 0x0156, 67 + 0x0158, 0x0158, 0x015a, 0x015a, 0x015c, 0x015c, 0x015e, 0x015e, 68 + 0x0160, 0x0160, 0x0162, 0x0162, 0x0164, 0x0164, 0x0166, 0x0166, 69 + 0x0168, 0x0168, 0x016a, 0x016a, 0x016c, 0x016c, 0x016e, 0x016e, 70 + 0x0170, 0x0170, 0x0172, 0x0172, 0x0174, 0x0174, 0x0176, 0x0176, 71 + 0x0178, 0x0179, 0x0179, 0x017b, 0x017b, 0x017d, 0x017d, 0x017f, 72 + 0x0243, 0x0181, 0x0182, 0x0182, 0x0184, 0x0184, 0x0186, 0x0187, 73 + 0x0187, 0x0189, 0x018a, 0x018b, 0x018b, 0x018d, 0x018e, 0x018f, 74 + 0x0190, 0x0191, 0x0191, 0x0193, 0x0194, 0x01f6, 0x0196, 0x0197, 75 + 0x0198, 0x0198, 0x023d, 0x019b, 0x019c, 0x019d, 0x0220, 0x019f, 76 + 0x01a0, 0x01a0, 0x01a2, 0x01a2, 0x01a4, 0x01a4, 0x01a6, 0x01a7, 77 + 0x01a7, 0x01a9, 0x01aa, 0x01ab, 0x01ac, 0x01ac, 0x01ae, 0x01af, 78 + 0x01af, 0x01b1, 0x01b2, 0x01b3, 0x01b3, 0x01b5, 0x01b5, 0x01b7, 79 + 0x01b8, 0x01b8, 0x01ba, 0x01bb, 0x01bc, 0x01bc, 0x01be, 0x01f7, 80 + 0x01c0, 0x01c1, 0x01c2, 0x01c3, 0x01c4, 0x01c5, 0x01c4, 0x01c7, 81 + 0x01c8, 0x01c7, 0x01ca, 0x01cb, 0x01ca, 0x01cd, 0x01cd, 0x01cf, 82 + 0x01cf, 0x01d1, 0x01d1, 0x01d3, 0x01d3, 0x01d5, 0x01d5, 0x01d7, 83 + 0x01d7, 0x01d9, 0x01d9, 0x01db, 0x01db, 0x018e, 0x01de, 0x01de, 84 + 0x01e0, 0x01e0, 0x01e2, 0x01e2, 0x01e4, 0x01e4, 0x01e6, 0x01e6, 85 + 0x01e8, 0x01e8, 0x01ea, 0x01ea, 0x01ec, 0x01ec, 0x01ee, 0x01ee, 86 + 0x01f0, 0x01f1, 0x01f2, 0x01f1, 0x01f4, 0x01f4, 0x01f6, 0x01f7, 87 + 0x01f8, 0x01f8, 0x01fa, 0x01fa, 0x01fc, 0x01fc, 0x01fe, 0x01fe, 88 + 0x0200, 0x0200, 0x0202, 0x0202, 0x0204, 0x0204, 0x0206, 0x0206, 89 + 0x0208, 0x0208, 0x020a, 0x020a, 0x020c, 0x020c, 0x020e, 0x020e, 90 + 0x0210, 0x0210, 0x0212, 0x0212, 0x0214, 0x0214, 0x0216, 0x0216, 91 + 0x0218, 0x0218, 0x021a, 0x021a, 0x021c, 0x021c, 0x021e, 0x021e, 92 + 0x0220, 0x0221, 0x0222, 0x0222, 0x0224, 0x0224, 0x0226, 0x0226, 93 + 0x0228, 0x0228, 0x022a, 0x022a, 0x022c, 0x022c, 0x022e, 0x022e, 94 + 0x0230, 0x0230, 0x0232, 0x0232, 0x0234, 0x0235, 0x0236, 0x0237, 95 + 0x0238, 0x0239, 0x2c65, 0x023b, 0x023b, 0x023d, 0x2c66, 0x023f, 96 + 0x0240, 0x0241, 0x0241, 0x0243, 0x0244, 0x0245, 0x0246, 0x0246, 97 + 0x0248, 0x0248, 0x024a, 0x024a, 0x024c, 0x024c, 0x024e, 0x024e, 98 + 0x0250, 0x0251, 0x0252, 0x0181, 0x0186, 0x0255, 0x0189, 0x018a, 99 + 0x0258, 0x018f, 0x025a, 0x0190, 0x025c, 0x025d, 0x025e, 0x025f, 100 + 0x0193, 0x0261, 0x0262, 0x0194, 0x0264, 0x0265, 0x0266, 0x0267, 101 + 0x0197, 0x0196, 0x026a, 0x2c62, 0x026c, 0x026d, 0x026e, 0x019c, 102 + 0x0270, 0x0271, 0x019d, 0x0273, 0x0274, 0x019f, 0x0276, 0x0277, 103 + 0x0278, 0x0279, 0x027a, 0x027b, 0x027c, 0x2c64, 0x027e, 0x027f, 104 + 0x01a6, 0x0281, 0x0282, 0x01a9, 0x0284, 0x0285, 0x0286, 0x0287, 105 + 0x01ae, 0x0244, 0x01b1, 0x01b2, 0x0245, 0x028d, 0x028e, 0x028f, 106 + 0x0290, 0x0291, 0x01b7, 0x0293, 0x0294, 0x0295, 0x0296, 0x0297, 107 + 0x0298, 0x0299, 0x029a, 0x029b, 0x029c, 0x029d, 0x029e, 0x029f, 108 + 0x02a0, 0x02a1, 0x02a2, 0x02a3, 0x02a4, 0x02a5, 0x02a6, 0x02a7, 109 + 0x02a8, 0x02a9, 0x02aa, 0x02ab, 0x02ac, 0x02ad, 0x02ae, 0x02af, 110 + 0x02b0, 0x02b1, 0x02b2, 0x02b3, 0x02b4, 0x02b5, 0x02b6, 0x02b7, 111 + 0x02b8, 0x02b9, 0x02ba, 0x02bb, 0x02bc, 0x02bd, 0x02be, 0x02bf, 112 + 0x02c0, 0x02c1, 0x02c2, 0x02c3, 0x02c4, 0x02c5, 0x02c6, 0x02c7, 113 + 0x02c8, 0x02c9, 0x02ca, 0x02cb, 0x02cc, 0x02cd, 0x02ce, 0x02cf, 114 + 0x02d0, 0x02d1, 0x02d2, 0x02d3, 0x02d4, 0x02d5, 0x02d6, 0x02d7, 115 + 0x02d8, 0x02d9, 0x02da, 0x02db, 0x02dc, 0x02dd, 0x02de, 0x02df, 116 + 0x02e0, 0x02e1, 0x02e2, 0x02e3, 0x02e4, 0x02e5, 0x02e6, 0x02e7, 117 + 0x02e8, 0x02e9, 0x02ea, 0x02eb, 0x02ec, 0x02ed, 0x02ee, 0x02ef, 118 + 0x02f0, 0x02f1, 0x02f2, 0x02f3, 0x02f4, 0x02f5, 0x02f6, 0x02f7, 119 + 0x02f8, 0x02f9, 0x02fa, 0x02fb, 0x02fc, 0x02fd, 0x02fe, 0x02ff, 120 + 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 121 + 0x0308, 0x0309, 0x030a, 0x030b, 0x030c, 0x030d, 0x030e, 0x030f, 122 + 0x0310, 0x0311, 0x0312, 0x0313, 0x0314, 0x0315, 0x0316, 0x0317, 123 + 0x0318, 0x0319, 0x031a, 0x031b, 0x031c, 0x031d, 0x031e, 0x031f, 124 + 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326, 0x0327, 125 + 0x0328, 0x0329, 0x032a, 0x032b, 0x032c, 0x032d, 0x032e, 0x032f, 126 + 0x0330, 0x0331, 0x0332, 0x0333, 0x0334, 0x0335, 0x0336, 0x0337, 127 + 0x0338, 0x0339, 0x033a, 0x033b, 0x033c, 0x033d, 0x033e, 0x033f, 128 + 0x0340, 0x0341, 0x0342, 0x0343, 0x0344, 0x0345, 0x0346, 0x0347, 129 + 0x0348, 0x0349, 0x034a, 0x034b, 0x034c, 0x034d, 0x034e, 0x034f, 130 + 0x0350, 0x0351, 0x0352, 0x0353, 0x0354, 0x0355, 0x0356, 0x0357, 131 + 0x0358, 0x0359, 0x035a, 0x035b, 0x035c, 0x035d, 0x035e, 0x035f, 132 + 0x0360, 0x0361, 0x0362, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367, 133 + 0x0368, 0x0369, 0x036a, 0x036b, 0x036c, 0x036d, 0x036e, 0x036f, 134 + 0x0370, 0x0371, 0x0372, 0x0373, 0x0374, 0x0375, 0x0376, 0x0377, 135 + 0x0378, 0x0379, 0x037a, 0x03fd, 0x03fe, 0x03ff, 0x037e, 0x037f, 136 + 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386, 0x0387, 137 + 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e, 0x038f, 138 + 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 139 + 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, 140 + 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, 141 + 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x0386, 0x0388, 0x0389, 0x038a, 142 + 0x03b0, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 143 + 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, 144 + 0x03a0, 0x03a1, 0x03a3, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, 145 + 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x038c, 0x038e, 0x038f, 0x03cf, 146 + 0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7, 147 + 0x03d8, 0x03d8, 0x03da, 0x03da, 0x03dc, 0x03dc, 0x03de, 0x03de, 148 + 0x03e0, 0x03e0, 0x03e2, 0x03e2, 0x03e4, 0x03e4, 0x03e6, 0x03e6, 149 + 0x03e8, 0x03e8, 0x03ea, 0x03ea, 0x03ec, 0x03ec, 0x03ee, 0x03ee, 150 + 0x03f0, 0x03f1, 0x03f9, 0x03f3, 0x03f4, 0x03f5, 0x03f6, 0x03f7, 151 + 0x03f7, 0x03f9, 0x03fa, 0x03fa, 0x03fc, 0x03fd, 0x03fe, 0x03ff, 152 + 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 153 + 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x040d, 0x040e, 0x040f, 154 + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 155 + 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, 156 + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 157 + 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, 158 + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 159 + 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, 160 + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 161 + 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, 162 + 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 163 + 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x040d, 0x040e, 0x040f, 164 + 0x0460, 0x0460, 0x0462, 0x0462, 0x0464, 0x0464, 0x0466, 0x0466, 165 + 0x0468, 0x0468, 0x046a, 0x046a, 0x046c, 0x046c, 0x046e, 0x046e, 166 + 0x0470, 0x0470, 0x0472, 0x0472, 0x0474, 0x0474, 0x0476, 0x0476, 167 + 0x0478, 0x0478, 0x047a, 0x047a, 0x047c, 0x047c, 0x047e, 0x047e, 168 + 0x0480, 0x0480, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487, 169 + 0x0488, 0x0489, 0x048a, 0x048a, 0x048c, 0x048c, 0x048e, 0x048e, 170 + 0x0490, 0x0490, 0x0492, 0x0492, 0x0494, 0x0494, 0x0496, 0x0496, 171 + 0x0498, 0x0498, 0x049a, 0x049a, 0x049c, 0x049c, 0x049e, 0x049e, 172 + 0x04a0, 0x04a0, 0x04a2, 0x04a2, 0x04a4, 0x04a4, 0x04a6, 0x04a6, 173 + 0x04a8, 0x04a8, 0x04aa, 0x04aa, 0x04ac, 0x04ac, 0x04ae, 0x04ae, 174 + 0x04b0, 0x04b0, 0x04b2, 0x04b2, 0x04b4, 0x04b4, 0x04b6, 0x04b6, 175 + 0x04b8, 0x04b8, 0x04ba, 0x04ba, 0x04bc, 0x04bc, 0x04be, 0x04be, 176 + 0x04c0, 0x04c1, 0x04c1, 0x04c3, 0x04c3, 0x04c5, 0x04c5, 0x04c7, 177 + 0x04c7, 0x04c9, 0x04c9, 0x04cb, 0x04cb, 0x04cd, 0x04cd, 0x04c0, 178 + 0x04d0, 0x04d0, 0x04d2, 0x04d2, 0x04d4, 0x04d4, 0x04d6, 0x04d6, 179 + 0x04d8, 0x04d8, 0x04da, 0x04da, 0x04dc, 0x04dc, 0x04de, 0x04de, 180 + 0x04e0, 0x04e0, 0x04e2, 0x04e2, 0x04e4, 0x04e4, 0x04e6, 0x04e6, 181 + 0x04e8, 0x04e8, 0x04ea, 0x04ea, 0x04ec, 0x04ec, 0x04ee, 0x04ee, 182 + 0x04f0, 0x04f0, 0x04f2, 0x04f2, 0x04f4, 0x04f4, 0x04f6, 0x04f6, 183 + 0x04f8, 0x04f8, 0x04fa, 0x04fa, 0x04fc, 0x04fc, 0x04fe, 0x04fe, 184 + 0x0500, 0x0500, 0x0502, 0x0502, 0x0504, 0x0504, 0x0506, 0x0506, 185 + 0x0508, 0x0508, 0x050a, 0x050a, 0x050c, 0x050c, 0x050e, 0x050e, 186 + 0x0510, 0x0510, 0x0512, 0x0512, 0x0514, 0x0515, 0x0516, 0x0517, 187 + 0x0518, 0x0519, 0x051a, 0x051b, 0x051c, 0x051d, 0x051e, 0x051f, 188 + 0x0520, 0x0521, 0x0522, 0x0523, 0x0524, 0x0525, 0x0526, 0x0527, 189 + 0x0528, 0x0529, 0x052a, 0x052b, 0x052c, 0x052d, 0x052e, 0x052f, 190 + 0x0530, 0x0531, 0x0532, 0x0533, 0x0534, 0x0535, 0x0536, 0x0537, 191 + 0x0538, 0x0539, 0x053a, 0x053b, 0x053c, 0x053d, 0x053e, 0x053f, 192 + 0x0540, 0x0541, 0x0542, 0x0543, 0x0544, 0x0545, 0x0546, 0x0547, 193 + 0x0548, 0x0549, 0x054a, 0x054b, 0x054c, 0x054d, 0x054e, 0x054f, 194 + 0x0550, 0x0551, 0x0552, 0x0553, 0x0554, 0x0555, 0x0556, 0x0557, 195 + 0x0558, 0x0559, 0x055a, 0x055b, 0x055c, 0x055d, 0x055e, 0x055f, 196 + 0x0560, 0x0531, 0x0532, 0x0533, 0x0534, 0x0535, 0x0536, 0x0537, 197 + 0x0538, 0x0539, 0x053a, 0x053b, 0x053c, 0x053d, 0x053e, 0x053f, 198 + 0x0540, 0x0541, 0x0542, 0x0543, 0x0544, 0x0545, 0x0546, 0x0547, 199 + 0x0548, 0x0549, 0x054a, 0x054b, 0x054c, 0x054d, 0x054e, 0x054f, 200 + 0x0550, 0x0551, 0x0552, 0x0553, 0x0554, 0x0555, 0x0556, 0xffff, 201 + 0x17f6, 0x2c63, 0x1d7e, 0x1d7f, 0x1d80, 0x1d81, 0x1d82, 0x1d83, 202 + 0x1d84, 0x1d85, 0x1d86, 0x1d87, 0x1d88, 0x1d89, 0x1d8a, 0x1d8b, 203 + 0x1d8c, 0x1d8d, 0x1d8e, 0x1d8f, 0x1d90, 0x1d91, 0x1d92, 0x1d93, 204 + 0x1d94, 0x1d95, 0x1d96, 0x1d97, 0x1d98, 0x1d99, 0x1d9a, 0x1d9b, 205 + 0x1d9c, 0x1d9d, 0x1d9e, 0x1d9f, 0x1da0, 0x1da1, 0x1da2, 0x1da3, 206 + 0x1da4, 0x1da5, 0x1da6, 0x1da7, 0x1da8, 0x1da9, 0x1daa, 0x1dab, 207 + 0x1dac, 0x1dad, 0x1dae, 0x1daf, 0x1db0, 0x1db1, 0x1db2, 0x1db3, 208 + 0x1db4, 0x1db5, 0x1db6, 0x1db7, 0x1db8, 0x1db9, 0x1dba, 0x1dbb, 209 + 0x1dbc, 0x1dbd, 0x1dbe, 0x1dbf, 0x1dc0, 0x1dc1, 0x1dc2, 0x1dc3, 210 + 0x1dc4, 0x1dc5, 0x1dc6, 0x1dc7, 0x1dc8, 0x1dc9, 0x1dca, 0x1dcb, 211 + 0x1dcc, 0x1dcd, 0x1dce, 0x1dcf, 0x1dd0, 0x1dd1, 0x1dd2, 0x1dd3, 212 + 0x1dd4, 0x1dd5, 0x1dd6, 0x1dd7, 0x1dd8, 0x1dd9, 0x1dda, 0x1ddb, 213 + 0x1ddc, 0x1ddd, 0x1dde, 0x1ddf, 0x1de0, 0x1de1, 0x1de2, 0x1de3, 214 + 0x1de4, 0x1de5, 0x1de6, 0x1de7, 0x1de8, 0x1de9, 0x1dea, 0x1deb, 215 + 0x1dec, 0x1ded, 0x1dee, 0x1def, 0x1df0, 0x1df1, 0x1df2, 0x1df3, 216 + 0x1df4, 0x1df5, 0x1df6, 0x1df7, 0x1df8, 0x1df9, 0x1dfa, 0x1dfb, 217 + 0x1dfc, 0x1dfd, 0x1dfe, 0x1dff, 0x1e00, 0x1e00, 0x1e02, 0x1e02, 218 + 0x1e04, 0x1e04, 0x1e06, 0x1e06, 0x1e08, 0x1e08, 0x1e0a, 0x1e0a, 219 + 0x1e0c, 0x1e0c, 0x1e0e, 0x1e0e, 0x1e10, 0x1e10, 0x1e12, 0x1e12, 220 + 0x1e14, 0x1e14, 0x1e16, 0x1e16, 0x1e18, 0x1e18, 0x1e1a, 0x1e1a, 221 + 0x1e1c, 0x1e1c, 0x1e1e, 0x1e1e, 0x1e20, 0x1e20, 0x1e22, 0x1e22, 222 + 0x1e24, 0x1e24, 0x1e26, 0x1e26, 0x1e28, 0x1e28, 0x1e2a, 0x1e2a, 223 + 0x1e2c, 0x1e2c, 0x1e2e, 0x1e2e, 0x1e30, 0x1e30, 0x1e32, 0x1e32, 224 + 0x1e34, 0x1e34, 0x1e36, 0x1e36, 0x1e38, 0x1e38, 0x1e3a, 0x1e3a, 225 + 0x1e3c, 0x1e3c, 0x1e3e, 0x1e3e, 0x1e40, 0x1e40, 0x1e42, 0x1e42, 226 + 0x1e44, 0x1e44, 0x1e46, 0x1e46, 0x1e48, 0x1e48, 0x1e4a, 0x1e4a, 227 + 0x1e4c, 0x1e4c, 0x1e4e, 0x1e4e, 0x1e50, 0x1e50, 0x1e52, 0x1e52, 228 + 0x1e54, 0x1e54, 0x1e56, 0x1e56, 0x1e58, 0x1e58, 0x1e5a, 0x1e5a, 229 + 0x1e5c, 0x1e5c, 0x1e5e, 0x1e5e, 0x1e60, 0x1e60, 0x1e62, 0x1e62, 230 + 0x1e64, 0x1e64, 0x1e66, 0x1e66, 0x1e68, 0x1e68, 0x1e6a, 0x1e6a, 231 + 0x1e6c, 0x1e6c, 0x1e6e, 0x1e6e, 0x1e70, 0x1e70, 0x1e72, 0x1e72, 232 + 0x1e74, 0x1e74, 0x1e76, 0x1e76, 0x1e78, 0x1e78, 0x1e7a, 0x1e7a, 233 + 0x1e7c, 0x1e7c, 0x1e7e, 0x1e7e, 0x1e80, 0x1e80, 0x1e82, 0x1e82, 234 + 0x1e84, 0x1e84, 0x1e86, 0x1e86, 0x1e88, 0x1e88, 0x1e8a, 0x1e8a, 235 + 0x1e8c, 0x1e8c, 0x1e8e, 0x1e8e, 0x1e90, 0x1e90, 0x1e92, 0x1e92, 236 + 0x1e94, 0x1e94, 0x1e96, 0x1e97, 0x1e98, 0x1e99, 0x1e9a, 0x1e9b, 237 + 0x1e9c, 0x1e9d, 0x1e9e, 0x1e9f, 0x1ea0, 0x1ea0, 0x1ea2, 0x1ea2, 238 + 0x1ea4, 0x1ea4, 0x1ea6, 0x1ea6, 0x1ea8, 0x1ea8, 0x1eaa, 0x1eaa, 239 + 0x1eac, 0x1eac, 0x1eae, 0x1eae, 0x1eb0, 0x1eb0, 0x1eb2, 0x1eb2, 240 + 0x1eb4, 0x1eb4, 0x1eb6, 0x1eb6, 0x1eb8, 0x1eb8, 0x1eba, 0x1eba, 241 + 0x1ebc, 0x1ebc, 0x1ebe, 0x1ebe, 0x1ec0, 0x1ec0, 0x1ec2, 0x1ec2, 242 + 0x1ec4, 0x1ec4, 0x1ec6, 0x1ec6, 0x1ec8, 0x1ec8, 0x1eca, 0x1eca, 243 + 0x1ecc, 0x1ecc, 0x1ece, 0x1ece, 0x1ed0, 0x1ed0, 0x1ed2, 0x1ed2, 244 + 0x1ed4, 0x1ed4, 0x1ed6, 0x1ed6, 0x1ed8, 0x1ed8, 0x1eda, 0x1eda, 245 + 0x1edc, 0x1edc, 0x1ede, 0x1ede, 0x1ee0, 0x1ee0, 0x1ee2, 0x1ee2, 246 + 0x1ee4, 0x1ee4, 0x1ee6, 0x1ee6, 0x1ee8, 0x1ee8, 0x1eea, 0x1eea, 247 + 0x1eec, 0x1eec, 0x1eee, 0x1eee, 0x1ef0, 0x1ef0, 0x1ef2, 0x1ef2, 248 + 0x1ef4, 0x1ef4, 0x1ef6, 0x1ef6, 0x1ef8, 0x1ef8, 0x1efa, 0x1efb, 249 + 0x1efc, 0x1efd, 0x1efe, 0x1eff, 0x1f08, 0x1f09, 0x1f0a, 0x1f0b, 250 + 0x1f0c, 0x1f0d, 0x1f0e, 0x1f0f, 0x1f08, 0x1f09, 0x1f0a, 0x1f0b, 251 + 0x1f0c, 0x1f0d, 0x1f0e, 0x1f0f, 0x1f18, 0x1f19, 0x1f1a, 0x1f1b, 252 + 0x1f1c, 0x1f1d, 0x1f16, 0x1f17, 0x1f18, 0x1f19, 0x1f1a, 0x1f1b, 253 + 0x1f1c, 0x1f1d, 0x1f1e, 0x1f1f, 0x1f28, 0x1f29, 0x1f2a, 0x1f2b, 254 + 0x1f2c, 0x1f2d, 0x1f2e, 0x1f2f, 0x1f28, 0x1f29, 0x1f2a, 0x1f2b, 255 + 0x1f2c, 0x1f2d, 0x1f2e, 0x1f2f, 0x1f38, 0x1f39, 0x1f3a, 0x1f3b, 256 + 0x1f3c, 0x1f3d, 0x1f3e, 0x1f3f, 0x1f38, 0x1f39, 0x1f3a, 0x1f3b, 257 + 0x1f3c, 0x1f3d, 0x1f3e, 0x1f3f, 0x1f48, 0x1f49, 0x1f4a, 0x1f4b, 258 + 0x1f4c, 0x1f4d, 0x1f46, 0x1f47, 0x1f48, 0x1f49, 0x1f4a, 0x1f4b, 259 + 0x1f4c, 0x1f4d, 0x1f4e, 0x1f4f, 0x1f50, 0x1f59, 0x1f52, 0x1f5b, 260 + 0x1f54, 0x1f5d, 0x1f56, 0x1f5f, 0x1f58, 0x1f59, 0x1f5a, 0x1f5b, 261 + 0x1f5c, 0x1f5d, 0x1f5e, 0x1f5f, 0x1f68, 0x1f69, 0x1f6a, 0x1f6b, 262 + 0x1f6c, 0x1f6d, 0x1f6e, 0x1f6f, 0x1f68, 0x1f69, 0x1f6a, 0x1f6b, 263 + 0x1f6c, 0x1f6d, 0x1f6e, 0x1f6f, 0x1fba, 0x1fbb, 0x1fc8, 0x1fc9, 264 + 0x1fca, 0x1fcb, 0x1fda, 0x1fdb, 0x1ff8, 0x1ff9, 0x1fea, 0x1feb, 265 + 0x1ffa, 0x1ffb, 0x1f7e, 0x1f7f, 0x1f88, 0x1f89, 0x1f8a, 0x1f8b, 266 + 0x1f8c, 0x1f8d, 0x1f8e, 0x1f8f, 0x1f88, 0x1f89, 0x1f8a, 0x1f8b, 267 + 0x1f8c, 0x1f8d, 0x1f8e, 0x1f8f, 0x1f98, 0x1f99, 0x1f9a, 0x1f9b, 268 + 0x1f9c, 0x1f9d, 0x1f9e, 0x1f9f, 0x1f98, 0x1f99, 0x1f9a, 0x1f9b, 269 + 0x1f9c, 0x1f9d, 0x1f9e, 0x1f9f, 0x1fa8, 0x1fa9, 0x1faa, 0x1fab, 270 + 0x1fac, 0x1fad, 0x1fae, 0x1faf, 0x1fa8, 0x1fa9, 0x1faa, 0x1fab, 271 + 0x1fac, 0x1fad, 0x1fae, 0x1faf, 0x1fb8, 0x1fb9, 0x1fb2, 0x1fbc, 272 + 0x1fb4, 0x1fb5, 0x1fb6, 0x1fb7, 0x1fb8, 0x1fb9, 0x1fba, 0x1fbb, 273 + 0x1fbc, 0x1fbd, 0x1fbe, 0x1fbf, 0x1fc0, 0x1fc1, 0x1fc2, 0x1fc3, 274 + 0x1fc4, 0x1fc5, 0x1fc6, 0x1fc7, 0x1fc8, 0x1fc9, 0x1fca, 0x1fcb, 275 + 0x1fc3, 0x1fcd, 0x1fce, 0x1fcf, 0x1fd8, 0x1fd9, 0x1fd2, 0x1fd3, 276 + 0x1fd4, 0x1fd5, 0x1fd6, 0x1fd7, 0x1fd8, 0x1fd9, 0x1fda, 0x1fdb, 277 + 0x1fdc, 0x1fdd, 0x1fde, 0x1fdf, 0x1fe8, 0x1fe9, 0x1fe2, 0x1fe3, 278 + 0x1fe4, 0x1fec, 0x1fe6, 0x1fe7, 0x1fe8, 0x1fe9, 0x1fea, 0x1feb, 279 + 0x1fec, 0x1fed, 0x1fee, 0x1fef, 0x1ff0, 0x1ff1, 0x1ff2, 0x1ff3, 280 + 0x1ff4, 0x1ff5, 0x1ff6, 0x1ff7, 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 281 + 0x1ff3, 0x1ffd, 0x1ffe, 0x1fff, 0x2000, 0x2001, 0x2002, 0x2003, 282 + 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200a, 0x200b, 283 + 0x200c, 0x200d, 0x200e, 0x200f, 0x2010, 0x2011, 0x2012, 0x2013, 284 + 0x2014, 0x2015, 0x2016, 0x2017, 0x2018, 0x2019, 0x201a, 0x201b, 285 + 0x201c, 0x201d, 0x201e, 0x201f, 0x2020, 0x2021, 0x2022, 0x2023, 286 + 0x2024, 0x2025, 0x2026, 0x2027, 0x2028, 0x2029, 0x202a, 0x202b, 287 + 0x202c, 0x202d, 0x202e, 0x202f, 0x2030, 0x2031, 0x2032, 0x2033, 288 + 0x2034, 0x2035, 0x2036, 0x2037, 0x2038, 0x2039, 0x203a, 0x203b, 289 + 0x203c, 0x203d, 0x203e, 0x203f, 0x2040, 0x2041, 0x2042, 0x2043, 290 + 0x2044, 0x2045, 0x2046, 0x2047, 0x2048, 0x2049, 0x204a, 0x204b, 291 + 0x204c, 0x204d, 0x204e, 0x204f, 0x2050, 0x2051, 0x2052, 0x2053, 292 + 0x2054, 0x2055, 0x2056, 0x2057, 0x2058, 0x2059, 0x205a, 0x205b, 293 + 0x205c, 0x205d, 0x205e, 0x205f, 0x2060, 0x2061, 0x2062, 0x2063, 294 + 0x2064, 0x2065, 0x2066, 0x2067, 0x2068, 0x2069, 0x206a, 0x206b, 295 + 0x206c, 0x206d, 0x206e, 0x206f, 0x2070, 0x2071, 0x2072, 0x2073, 296 + 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079, 0x207a, 0x207b, 297 + 0x207c, 0x207d, 0x207e, 0x207f, 0x2080, 0x2081, 0x2082, 0x2083, 298 + 0x2084, 0x2085, 0x2086, 0x2087, 0x2088, 0x2089, 0x208a, 0x208b, 299 + 0x208c, 0x208d, 0x208e, 0x208f, 0x2090, 0x2091, 0x2092, 0x2093, 300 + 0x2094, 0x2095, 0x2096, 0x2097, 0x2098, 0x2099, 0x209a, 0x209b, 301 + 0x209c, 0x209d, 0x209e, 0x209f, 0x20a0, 0x20a1, 0x20a2, 0x20a3, 302 + 0x20a4, 0x20a5, 0x20a6, 0x20a7, 0x20a8, 0x20a9, 0x20aa, 0x20ab, 303 + 0x20ac, 0x20ad, 0x20ae, 0x20af, 0x20b0, 0x20b1, 0x20b2, 0x20b3, 304 + 0x20b4, 0x20b5, 0x20b6, 0x20b7, 0x20b8, 0x20b9, 0x20ba, 0x20bb, 305 + 0x20bc, 0x20bd, 0x20be, 0x20bf, 0x20c0, 0x20c1, 0x20c2, 0x20c3, 306 + 0x20c4, 0x20c5, 0x20c6, 0x20c7, 0x20c8, 0x20c9, 0x20ca, 0x20cb, 307 + 0x20cc, 0x20cd, 0x20ce, 0x20cf, 0x20d0, 0x20d1, 0x20d2, 0x20d3, 308 + 0x20d4, 0x20d5, 0x20d6, 0x20d7, 0x20d8, 0x20d9, 0x20da, 0x20db, 309 + 0x20dc, 0x20dd, 0x20de, 0x20df, 0x20e0, 0x20e1, 0x20e2, 0x20e3, 310 + 0x20e4, 0x20e5, 0x20e6, 0x20e7, 0x20e8, 0x20e9, 0x20ea, 0x20eb, 311 + 0x20ec, 0x20ed, 0x20ee, 0x20ef, 0x20f0, 0x20f1, 0x20f2, 0x20f3, 312 + 0x20f4, 0x20f5, 0x20f6, 0x20f7, 0x20f8, 0x20f9, 0x20fa, 0x20fb, 313 + 0x20fc, 0x20fd, 0x20fe, 0x20ff, 0x2100, 0x2101, 0x2102, 0x2103, 314 + 0x2104, 0x2105, 0x2106, 0x2107, 0x2108, 0x2109, 0x210a, 0x210b, 315 + 0x210c, 0x210d, 0x210e, 0x210f, 0x2110, 0x2111, 0x2112, 0x2113, 316 + 0x2114, 0x2115, 0x2116, 0x2117, 0x2118, 0x2119, 0x211a, 0x211b, 317 + 0x211c, 0x211d, 0x211e, 0x211f, 0x2120, 0x2121, 0x2122, 0x2123, 318 + 0x2124, 0x2125, 0x2126, 0x2127, 0x2128, 0x2129, 0x212a, 0x212b, 319 + 0x212c, 0x212d, 0x212e, 0x212f, 0x2130, 0x2131, 0x2132, 0x2133, 320 + 0x2134, 0x2135, 0x2136, 0x2137, 0x2138, 0x2139, 0x213a, 0x213b, 321 + 0x213c, 0x213d, 0x213e, 0x213f, 0x2140, 0x2141, 0x2142, 0x2143, 322 + 0x2144, 0x2145, 0x2146, 0x2147, 0x2148, 0x2149, 0x214a, 0x214b, 323 + 0x214c, 0x214d, 0x2132, 0x214f, 0x2150, 0x2151, 0x2152, 0x2153, 324 + 0x2154, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215a, 0x215b, 325 + 0x215c, 0x215d, 0x215e, 0x215f, 0x2160, 0x2161, 0x2162, 0x2163, 326 + 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169, 0x216a, 0x216b, 327 + 0x216c, 0x216d, 0x216e, 0x216f, 0x2160, 0x2161, 0x2162, 0x2163, 328 + 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169, 0x216a, 0x216b, 329 + 0x216c, 0x216d, 0x216e, 0x216f, 0x2180, 0x2181, 0x2182, 0x2183, 330 + 0x2183, 0xffff, 0x034b, 0x24b6, 0x24b7, 0x24b8, 0x24b9, 0x24ba, 331 + 0x24bb, 0x24bc, 0x24bd, 0x24be, 0x24bf, 0x24c0, 0x24c1, 0x24c2, 332 + 0x24c3, 0x24c4, 0x24c5, 0x24c6, 0x24c7, 0x24c8, 0x24c9, 0x24ca, 333 + 0x24cb, 0x24cc, 0x24cd, 0x24ce, 0x24cf, 0xffff, 0x0746, 0x2c00, 334 + 0x2c01, 0x2c02, 0x2c03, 0x2c04, 0x2c05, 0x2c06, 0x2c07, 0x2c08, 335 + 0x2c09, 0x2c0a, 0x2c0b, 0x2c0c, 0x2c0d, 0x2c0e, 0x2c0f, 0x2c10, 336 + 0x2c11, 0x2c12, 0x2c13, 0x2c14, 0x2c15, 0x2c16, 0x2c17, 0x2c18, 337 + 0x2c19, 0x2c1a, 0x2c1b, 0x2c1c, 0x2c1d, 0x2c1e, 0x2c1f, 0x2c20, 338 + 0x2c21, 0x2c22, 0x2c23, 0x2c24, 0x2c25, 0x2c26, 0x2c27, 0x2c28, 339 + 0x2c29, 0x2c2a, 0x2c2b, 0x2c2c, 0x2c2d, 0x2c2e, 0x2c5f, 0x2c60, 340 + 0x2c60, 0x2c62, 0x2c63, 0x2c64, 0x2c65, 0x2c66, 0x2c67, 0x2c67, 341 + 0x2c69, 0x2c69, 0x2c6b, 0x2c6b, 0x2c6d, 0x2c6e, 0x2c6f, 0x2c70, 342 + 0x2c71, 0x2c72, 0x2c73, 0x2c74, 0x2c75, 0x2c75, 0x2c77, 0x2c78, 343 + 0x2c79, 0x2c7a, 0x2c7b, 0x2c7c, 0x2c7d, 0x2c7e, 0x2c7f, 0x2c80, 344 + 0x2c80, 0x2c82, 0x2c82, 0x2c84, 0x2c84, 0x2c86, 0x2c86, 0x2c88, 345 + 0x2c88, 0x2c8a, 0x2c8a, 0x2c8c, 0x2c8c, 0x2c8e, 0x2c8e, 0x2c90, 346 + 0x2c90, 0x2c92, 0x2c92, 0x2c94, 0x2c94, 0x2c96, 0x2c96, 0x2c98, 347 + 0x2c98, 0x2c9a, 0x2c9a, 0x2c9c, 0x2c9c, 0x2c9e, 0x2c9e, 0x2ca0, 348 + 0x2ca0, 0x2ca2, 0x2ca2, 0x2ca4, 0x2ca4, 0x2ca6, 0x2ca6, 0x2ca8, 349 + 0x2ca8, 0x2caa, 0x2caa, 0x2cac, 0x2cac, 0x2cae, 0x2cae, 0x2cb0, 350 + 0x2cb0, 0x2cb2, 0x2cb2, 0x2cb4, 0x2cb4, 0x2cb6, 0x2cb6, 0x2cb8, 351 + 0x2cb8, 0x2cba, 0x2cba, 0x2cbc, 0x2cbc, 0x2cbe, 0x2cbe, 0x2cc0, 352 + 0x2cc0, 0x2cc2, 0x2cc2, 0x2cc4, 0x2cc4, 0x2cc6, 0x2cc6, 0x2cc8, 353 + 0x2cc8, 0x2cca, 0x2cca, 0x2ccc, 0x2ccc, 0x2cce, 0x2cce, 0x2cd0, 354 + 0x2cd0, 0x2cd2, 0x2cd2, 0x2cd4, 0x2cd4, 0x2cd6, 0x2cd6, 0x2cd8, 355 + 0x2cd8, 0x2cda, 0x2cda, 0x2cdc, 0x2cdc, 0x2cde, 0x2cde, 0x2ce0, 356 + 0x2ce0, 0x2ce2, 0x2ce2, 0x2ce4, 0x2ce5, 0x2ce6, 0x2ce7, 0x2ce8, 357 + 0x2ce9, 0x2cea, 0x2ceb, 0x2cec, 0x2ced, 0x2cee, 0x2cef, 0x2cf0, 358 + 0x2cf1, 0x2cf2, 0x2cf3, 0x2cf4, 0x2cf5, 0x2cf6, 0x2cf7, 0x2cf8, 359 + 0x2cf9, 0x2cfa, 0x2cfb, 0x2cfc, 0x2cfd, 0x2cfe, 0x2cff, 0x10a0, 360 + 0x10a1, 0x10a2, 0x10a3, 0x10a4, 0x10a5, 0x10a6, 0x10a7, 0x10a8, 361 + 0x10a9, 0x10aa, 0x10ab, 0x10ac, 0x10ad, 0x10ae, 0x10af, 0x10b0, 362 + 0x10b1, 0x10b2, 0x10b3, 0x10b4, 0x10b5, 0x10b6, 0x10b7, 0x10b8, 363 + 0x10b9, 0x10ba, 0x10bb, 0x10bc, 0x10bd, 0x10be, 0x10bf, 0x10c0, 364 + 0x10c1, 0x10c2, 0x10c3, 0x10c4, 0x10c5, 0xffff, 0xd21b, 0xff21, 365 + 0xff22, 0xff23, 0xff24, 0xff25, 0xff26, 0xff27, 0xff28, 0xff29, 366 + 0xff2a, 0xff2b, 0xff2c, 0xff2d, 0xff2e, 0xff2f, 0xff30, 0xff31, 367 + 0xff32, 0xff33, 0xff34, 0xff35, 0xff36, 0xff37, 0xff38, 0xff39, 368 + 0xff3a, 0xff5b, 0xff5c, 0xff5d, 0xff5e, 0xff5f, 0xff60, 0xff61, 369 + 0xff62, 0xff63, 0xff64, 0xff65, 0xff66, 0xff67, 0xff68, 0xff69, 370 + 0xff6a, 0xff6b, 0xff6c, 0xff6d, 0xff6e, 0xff6f, 0xff70, 0xff71, 371 + 0xff72, 0xff73, 0xff74, 0xff75, 0xff76, 0xff77, 0xff78, 0xff79, 372 + 0xff7a, 0xff7b, 0xff7c, 0xff7d, 0xff7e, 0xff7f, 0xff80, 0xff81, 373 + 0xff82, 0xff83, 0xff84, 0xff85, 0xff86, 0xff87, 0xff88, 0xff89, 374 + 0xff8a, 0xff8b, 0xff8c, 0xff8d, 0xff8e, 0xff8f, 0xff90, 0xff91, 375 + 0xff92, 0xff93, 0xff94, 0xff95, 0xff96, 0xff97, 0xff98, 0xff99, 376 + 0xff9a, 0xff9b, 0xff9c, 0xff9d, 0xff9e, 0xff9f, 0xffa0, 0xffa1, 377 + 0xffa2, 0xffa3, 0xffa4, 0xffa5, 0xffa6, 0xffa7, 0xffa8, 0xffa9, 378 + 0xffaa, 0xffab, 0xffac, 0xffad, 0xffae, 0xffaf, 0xffb0, 0xffb1, 379 + 0xffb2, 0xffb3, 0xffb4, 0xffb5, 0xffb6, 0xffb7, 0xffb8, 0xffb9, 380 + 0xffba, 0xffbb, 0xffbc, 0xffbd, 0xffbe, 0xffbf, 0xffc0, 0xffc1, 381 + 0xffc2, 0xffc3, 0xffc4, 0xffc5, 0xffc6, 0xffc7, 0xffc8, 0xffc9, 382 + 0xffca, 0xffcb, 0xffcc, 0xffcd, 0xffce, 0xffcf, 0xffd0, 0xffd1, 383 + 0xffd2, 0xffd3, 0xffd4, 0xffd5, 0xffd6, 0xffd7, 0xffd8, 0xffd9, 384 + 0xffda, 0xffdb, 0xffdc, 0xffdd, 0xffde, 0xffdf, 0xffe0, 0xffe1, 385 + 0xffe2, 0xffe3, 0xffe4, 0xffe5, 0xffe6, 0xffe7, 0xffe8, 0xffe9, 386 + 0xffea, 0xffeb, 0xffec, 0xffed, 0xffee, 0xffef, 0xfff0, 0xfff1, 387 + 0xfff2, 0xfff3, 0xfff4, 0xfff5, 0xfff6, 0xfff7, 0xfff8, 0xfff9, 388 + 0xfffa, 0xfffb, 0xfffc, 0xfffd, 0xfffe, 0xffff, 389 + }; 390 + 391 + /* 392 + * Allow full-width illegal characters : 393 + * "MS windows 7" supports full-width-invalid-name-characters. 394 + * So we should check half-width-invalid-name-characters(ASCII) only 395 + * for compatibility. 396 + * 397 + * " * / : < > ? \ | 398 + */ 399 + static unsigned short bad_uni_chars[] = { 400 + 0x0022, 0x002A, 0x002F, 0x003A, 401 + 0x003C, 0x003E, 0x003F, 0x005C, 0x007C, 402 + 0 403 + }; 404 + 405 + static int exfat_convert_char_to_ucs2(struct nls_table *nls, 406 + const unsigned char *ch, int ch_len, unsigned short *ucs2, 407 + int *lossy) 408 + { 409 + int len; 410 + 411 + *ucs2 = 0x0; 412 + 413 + if (ch[0] < 0x80) { 414 + *ucs2 = ch[0]; 415 + return 1; 416 + } 417 + 418 + len = nls->char2uni(ch, ch_len, ucs2); 419 + if (len < 0) { 420 + /* conversion failed */ 421 + if (lossy != NULL) 422 + *lossy |= NLS_NAME_LOSSY; 423 + *ucs2 = '_'; 424 + return 1; 425 + } 426 + return len; 427 + } 428 + 429 + static int exfat_convert_ucs2_to_char(struct nls_table *nls, 430 + unsigned short ucs2, unsigned char *ch, int *lossy) 431 + { 432 + int len; 433 + 434 + ch[0] = 0x0; 435 + 436 + if (ucs2 < 0x0080) { 437 + ch[0] = ucs2; 438 + return 1; 439 + } 440 + 441 + len = nls->uni2char(ucs2, ch, MAX_CHARSET_SIZE); 442 + if (len < 0) { 443 + /* conversion failed */ 444 + if (lossy != NULL) 445 + *lossy |= NLS_NAME_LOSSY; 446 + ch[0] = '_'; 447 + return 1; 448 + } 449 + return len; 450 + } 451 + 452 + unsigned short exfat_toupper(struct super_block *sb, unsigned short a) 453 + { 454 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 455 + 456 + return sbi->vol_utbl[a] ? sbi->vol_utbl[a] : a; 457 + } 458 + 459 + static unsigned short *exfat_wstrchr(unsigned short *str, unsigned short wchar) 460 + { 461 + while (*str) { 462 + if (*(str++) == wchar) 463 + return str; 464 + } 465 + return NULL; 466 + } 467 + 468 + int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a, 469 + unsigned short *b, unsigned int len) 470 + { 471 + int i; 472 + 473 + for (i = 0; i < len; i++, a++, b++) 474 + if (exfat_toupper(sb, *a) != exfat_toupper(sb, *b)) 475 + return 1; 476 + return 0; 477 + } 478 + 479 + static int exfat_utf16_to_utf8(struct super_block *sb, 480 + struct exfat_uni_name *p_uniname, unsigned char *p_cstring, 481 + int buflen) 482 + { 483 + int len; 484 + const unsigned short *uniname = p_uniname->name; 485 + 486 + /* always len >= 0 */ 487 + len = utf16s_to_utf8s(uniname, MAX_NAME_LENGTH, UTF16_HOST_ENDIAN, 488 + p_cstring, buflen); 489 + p_cstring[len] = '\0'; 490 + return len; 491 + } 492 + 493 + static int exfat_utf8_to_utf16(struct super_block *sb, 494 + const unsigned char *p_cstring, const int len, 495 + struct exfat_uni_name *p_uniname, int *p_lossy) 496 + { 497 + int i, unilen, lossy = NLS_NAME_NO_LOSSY; 498 + unsigned short upname[MAX_NAME_LENGTH + 1]; 499 + unsigned short *uniname = p_uniname->name; 500 + 501 + WARN_ON(!len); 502 + 503 + unilen = utf8s_to_utf16s(p_cstring, len, UTF16_HOST_ENDIAN, 504 + (wchar_t *)uniname, MAX_NAME_LENGTH + 2); 505 + if (unilen < 0) { 506 + exfat_msg(sb, KERN_ERR, 507 + "failed to %s (err : %d) nls len : %d", 508 + __func__, unilen, len); 509 + return unilen; 510 + } 511 + 512 + if (unilen > MAX_NAME_LENGTH) { 513 + exfat_msg(sb, KERN_ERR, 514 + "failed to %s (estr:ENAMETOOLONG) nls len : %d, unilen : %d > %d", 515 + __func__, len, unilen, MAX_NAME_LENGTH); 516 + return -ENAMETOOLONG; 517 + } 518 + 519 + p_uniname->name_len = unilen & 0xFF; 520 + 521 + for (i = 0; i < unilen; i++) { 522 + if (*uniname < 0x0020 || 523 + exfat_wstrchr(bad_uni_chars, *uniname)) 524 + lossy |= NLS_NAME_LOSSY; 525 + 526 + upname[i] = exfat_toupper(sb, *uniname); 527 + uniname++; 528 + } 529 + 530 + *uniname = '\0'; 531 + p_uniname->name_len = unilen; 532 + p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0, 533 + CS_DEFAULT); 534 + 535 + if (p_lossy) 536 + *p_lossy = lossy; 537 + return unilen; 538 + } 539 + 540 + #define PLANE_SIZE 0x00010000 541 + #define SURROGATE_MASK 0xfffff800 542 + #define SURROGATE_PAIR 0x0000d800 543 + #define SURROGATE_LOW 0x00000400 544 + #define SURROGATE_BITS 0x000003ff 545 + 546 + unsigned short exfat_high_surrogate(unicode_t u) 547 + { 548 + return ((u - PLANE_SIZE) >> 10) + SURROGATE_PAIR; 549 + } 550 + 551 + unsigned short exfat_low_surrogate(unicode_t u) 552 + { 553 + return ((u - PLANE_SIZE) & SURROGATE_BITS) | SURROGATE_PAIR | 554 + SURROGATE_LOW; 555 + } 556 + 557 + static int __exfat_utf16_to_nls(struct super_block *sb, 558 + struct exfat_uni_name *p_uniname, unsigned char *p_cstring, 559 + int buflen) 560 + { 561 + int i, j, len, out_len = 0; 562 + unsigned char buf[MAX_CHARSET_SIZE]; 563 + const unsigned short *uniname = p_uniname->name; 564 + struct nls_table *nls = EXFAT_SB(sb)->nls_io; 565 + 566 + i = 0; 567 + while (i < MAX_NAME_LENGTH && out_len < (buflen - 1)) { 568 + if (*uniname == '\0') 569 + break; 570 + if ((*uniname & SURROGATE_MASK) != SURROGATE_PAIR) { 571 + len = exfat_convert_ucs2_to_char(nls, *uniname, buf, 572 + NULL); 573 + } else { 574 + /* Process UTF-16 surrogate pair as one character */ 575 + if (!(*uniname & SURROGATE_LOW) && 576 + i+1 < MAX_NAME_LENGTH && 577 + (*(uniname+1) & SURROGATE_MASK) == SURROGATE_PAIR && 578 + (*(uniname+1) & SURROGATE_LOW)) { 579 + uniname++; 580 + i++; 581 + } 582 + 583 + /* 584 + * UTF-16 surrogate pair encodes code points above 585 + * U+FFFF. Code points above U+FFFF are not supported 586 + * by kernel NLS framework therefore use replacement 587 + * character 588 + */ 589 + len = 1; 590 + buf[0] = '_'; 591 + } 592 + 593 + if (out_len + len >= buflen) 594 + len = buflen - 1 - out_len; 595 + out_len += len; 596 + 597 + if (len > 1) { 598 + for (j = 0; j < len; j++) 599 + *p_cstring++ = buf[j]; 600 + } else { /* len == 1 */ 601 + *p_cstring++ = *buf; 602 + } 603 + 604 + uniname++; 605 + i++; 606 + } 607 + 608 + *p_cstring = '\0'; 609 + return out_len; 610 + } 611 + 612 + static int exfat_nls_to_ucs2(struct super_block *sb, 613 + const unsigned char *p_cstring, const int len, 614 + struct exfat_uni_name *p_uniname, int *p_lossy) 615 + { 616 + int i = 0, unilen = 0, lossy = NLS_NAME_NO_LOSSY; 617 + unsigned short upname[MAX_NAME_LENGTH + 1]; 618 + unsigned short *uniname = p_uniname->name; 619 + struct nls_table *nls = EXFAT_SB(sb)->nls_io; 620 + 621 + WARN_ON(!len); 622 + 623 + while (unilen < MAX_NAME_LENGTH && i < len) { 624 + i += exfat_convert_char_to_ucs2(nls, p_cstring + i, len - i, 625 + uniname, &lossy); 626 + 627 + if (*uniname < 0x0020 || 628 + exfat_wstrchr(bad_uni_chars, *uniname)) 629 + lossy |= NLS_NAME_LOSSY; 630 + 631 + upname[unilen] = exfat_toupper(sb, *uniname); 632 + uniname++; 633 + unilen++; 634 + } 635 + 636 + if (p_cstring[i] != '\0') 637 + lossy |= NLS_NAME_OVERLEN; 638 + 639 + *uniname = '\0'; 640 + p_uniname->name_len = unilen; 641 + p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0, 642 + CS_DEFAULT); 643 + 644 + if (p_lossy) 645 + *p_lossy = lossy; 646 + return unilen; 647 + } 648 + 649 + int exfat_utf16_to_nls(struct super_block *sb, struct exfat_uni_name *uniname, 650 + unsigned char *p_cstring, int buflen) 651 + { 652 + if (EXFAT_SB(sb)->options.utf8) 653 + return exfat_utf16_to_utf8(sb, uniname, p_cstring, 654 + buflen); 655 + return __exfat_utf16_to_nls(sb, uniname, p_cstring, buflen); 656 + } 657 + 658 + int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring, 659 + const int len, struct exfat_uni_name *uniname, int *p_lossy) 660 + { 661 + if (EXFAT_SB(sb)->options.utf8) 662 + return exfat_utf8_to_utf16(sb, p_cstring, len, 663 + uniname, p_lossy); 664 + return exfat_nls_to_ucs2(sb, p_cstring, len, uniname, p_lossy); 665 + } 666 + 667 + static int exfat_load_upcase_table(struct super_block *sb, 668 + sector_t sector, unsigned long long num_sectors, 669 + unsigned int utbl_checksum) 670 + { 671 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 672 + unsigned int sect_size = sb->s_blocksize; 673 + unsigned int i, index = 0, checksum = 0; 674 + int ret; 675 + unsigned char skip = false; 676 + unsigned short *upcase_table; 677 + 678 + upcase_table = kcalloc(UTBL_COUNT, sizeof(unsigned short), GFP_KERNEL); 679 + if (!upcase_table) 680 + return -ENOMEM; 681 + 682 + sbi->vol_utbl = upcase_table; 683 + num_sectors += sector; 684 + 685 + while (sector < num_sectors) { 686 + struct buffer_head *bh; 687 + 688 + bh = sb_bread(sb, sector); 689 + if (!bh) { 690 + exfat_msg(sb, KERN_ERR, 691 + "failed to read sector(0x%llx)\n", 692 + (unsigned long long)sector); 693 + ret = -EIO; 694 + goto free_table; 695 + } 696 + sector++; 697 + for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) { 698 + unsigned short uni = get_unaligned_le16(bh->b_data + i); 699 + 700 + checksum = ((checksum & 1) ? 0x80000000 : 0) + 701 + (checksum >> 1) + 702 + *(((unsigned char *)bh->b_data) + i); 703 + checksum = ((checksum & 1) ? 0x80000000 : 0) + 704 + (checksum >> 1) + 705 + *(((unsigned char *)bh->b_data) + (i + 1)); 706 + 707 + if (skip) { 708 + index += uni; 709 + skip = false; 710 + } else if (uni == index) { 711 + index++; 712 + } else if (uni == 0xFFFF) { 713 + skip = true; 714 + } else { /* uni != index , uni != 0xFFFF */ 715 + upcase_table[index] = uni; 716 + index++; 717 + } 718 + } 719 + brelse(bh); 720 + } 721 + 722 + if (index >= 0xFFFF && utbl_checksum == checksum) 723 + return 0; 724 + 725 + exfat_msg(sb, KERN_ERR, 726 + "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)\n", 727 + index, checksum, utbl_checksum); 728 + ret = -EINVAL; 729 + free_table: 730 + exfat_free_upcase_table(sbi); 731 + return ret; 732 + } 733 + 734 + static int exfat_load_default_upcase_table(struct super_block *sb) 735 + { 736 + int i, ret = -EIO; 737 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 738 + unsigned char skip = false; 739 + unsigned short uni = 0, *upcase_table; 740 + unsigned int index = 0; 741 + 742 + upcase_table = kcalloc(UTBL_COUNT, sizeof(unsigned short), GFP_KERNEL); 743 + if (!upcase_table) 744 + return -ENOMEM; 745 + 746 + sbi->vol_utbl = upcase_table; 747 + 748 + for (i = 0; index <= 0xFFFF && i < EXFAT_NUM_UPCASE; i++) { 749 + uni = uni_def_upcase[i]; 750 + if (skip) { 751 + index += uni; 752 + skip = false; 753 + } else if (uni == index) { 754 + index++; 755 + } else if (uni == 0xFFFF) { 756 + skip = true; 757 + } else { 758 + upcase_table[index] = uni; 759 + index++; 760 + } 761 + } 762 + 763 + if (index >= 0xFFFF) 764 + return 0; 765 + 766 + /* FATAL error: default upcase table has error */ 767 + exfat_free_upcase_table(sbi); 768 + return ret; 769 + } 770 + 771 + int exfat_create_upcase_table(struct super_block *sb) 772 + { 773 + int i, ret; 774 + unsigned int tbl_clu, type; 775 + sector_t sector; 776 + unsigned long long tbl_size, num_sectors; 777 + unsigned char blksize_bits = sb->s_blocksize_bits; 778 + struct exfat_chain clu; 779 + struct exfat_dentry *ep; 780 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 781 + struct buffer_head *bh; 782 + 783 + clu.dir = sbi->root_dir; 784 + clu.flags = ALLOC_FAT_CHAIN; 785 + 786 + while (clu.dir != EXFAT_EOF_CLUSTER) { 787 + for (i = 0; i < sbi->dentries_per_clu; i++) { 788 + ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 789 + if (!ep) 790 + return -EIO; 791 + 792 + type = exfat_get_entry_type(ep); 793 + if (type == TYPE_UNUSED) { 794 + brelse(bh); 795 + break; 796 + } 797 + 798 + if (type != TYPE_UPCASE) { 799 + brelse(bh); 800 + continue; 801 + } 802 + 803 + tbl_clu = le32_to_cpu(ep->dentry.upcase.start_clu); 804 + tbl_size = le64_to_cpu(ep->dentry.upcase.size); 805 + 806 + sector = exfat_cluster_to_sector(sbi, tbl_clu); 807 + num_sectors = ((tbl_size - 1) >> blksize_bits) + 1; 808 + ret = exfat_load_upcase_table(sb, sector, num_sectors, 809 + le32_to_cpu(ep->dentry.upcase.checksum)); 810 + 811 + brelse(bh); 812 + if (ret && ret != -EIO) 813 + goto load_default; 814 + 815 + /* load successfully */ 816 + return ret; 817 + } 818 + 819 + if (exfat_get_next_cluster(sb, &(clu.dir))) 820 + return -EIO; 821 + } 822 + 823 + load_default: 824 + /* load default upcase table */ 825 + return exfat_load_default_upcase_table(sb); 826 + } 827 + 828 + void exfat_free_upcase_table(struct exfat_sb_info *sbi) 829 + { 830 + kfree(sbi->vol_utbl); 831 + }
+722
fs/exfat/super.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 + */ 5 + 6 + #include <linux/fs_context.h> 7 + #include <linux/fs_parser.h> 8 + #include <linux/module.h> 9 + #include <linux/init.h> 10 + #include <linux/time.h> 11 + #include <linux/mount.h> 12 + #include <linux/cred.h> 13 + #include <linux/statfs.h> 14 + #include <linux/seq_file.h> 15 + #include <linux/blkdev.h> 16 + #include <linux/fs_struct.h> 17 + #include <linux/iversion.h> 18 + #include <linux/nls.h> 19 + #include <linux/buffer_head.h> 20 + 21 + #include "exfat_raw.h" 22 + #include "exfat_fs.h" 23 + 24 + static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET; 25 + static struct kmem_cache *exfat_inode_cachep; 26 + 27 + static void exfat_free_iocharset(struct exfat_sb_info *sbi) 28 + { 29 + if (sbi->options.iocharset != exfat_default_iocharset) 30 + kfree(sbi->options.iocharset); 31 + } 32 + 33 + static void exfat_delayed_free(struct rcu_head *p) 34 + { 35 + struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu); 36 + 37 + unload_nls(sbi->nls_io); 38 + exfat_free_iocharset(sbi); 39 + exfat_free_upcase_table(sbi); 40 + kfree(sbi); 41 + } 42 + 43 + static void exfat_put_super(struct super_block *sb) 44 + { 45 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 46 + 47 + mutex_lock(&sbi->s_lock); 48 + if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) 49 + sync_blockdev(sb->s_bdev); 50 + exfat_set_vol_flags(sb, VOL_CLEAN); 51 + exfat_free_bitmap(sbi); 52 + mutex_unlock(&sbi->s_lock); 53 + 54 + call_rcu(&sbi->rcu, exfat_delayed_free); 55 + } 56 + 57 + static int exfat_sync_fs(struct super_block *sb, int wait) 58 + { 59 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 60 + int err = 0; 61 + 62 + /* If there are some dirty buffers in the bdev inode */ 63 + mutex_lock(&sbi->s_lock); 64 + if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) { 65 + sync_blockdev(sb->s_bdev); 66 + if (exfat_set_vol_flags(sb, VOL_CLEAN)) 67 + err = -EIO; 68 + } 69 + mutex_unlock(&sbi->s_lock); 70 + return err; 71 + } 72 + 73 + static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf) 74 + { 75 + struct super_block *sb = dentry->d_sb; 76 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 77 + unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev); 78 + 79 + if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) { 80 + mutex_lock(&sbi->s_lock); 81 + if (exfat_count_used_clusters(sb, &sbi->used_clusters)) { 82 + mutex_unlock(&sbi->s_lock); 83 + return -EIO; 84 + } 85 + mutex_unlock(&sbi->s_lock); 86 + } 87 + 88 + buf->f_type = sb->s_magic; 89 + buf->f_bsize = sbi->cluster_size; 90 + buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */ 91 + buf->f_bfree = buf->f_blocks - sbi->used_clusters; 92 + buf->f_bavail = buf->f_bfree; 93 + buf->f_fsid.val[0] = (unsigned int)id; 94 + buf->f_fsid.val[1] = (unsigned int)(id >> 32); 95 + /* Unicode utf16 255 characters */ 96 + buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE; 97 + return 0; 98 + } 99 + 100 + int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag) 101 + { 102 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 103 + struct pbr64 *bpb; 104 + bool sync = 0; 105 + 106 + /* flags are not changed */ 107 + if (sbi->vol_flag == new_flag) 108 + return 0; 109 + 110 + sbi->vol_flag = new_flag; 111 + 112 + /* skip updating volume dirty flag, 113 + * if this volume has been mounted with read-only 114 + */ 115 + if (sb_rdonly(sb)) 116 + return 0; 117 + 118 + if (!sbi->pbr_bh) { 119 + sbi->pbr_bh = sb_bread(sb, 0); 120 + if (!sbi->pbr_bh) { 121 + exfat_msg(sb, KERN_ERR, "failed to read boot sector"); 122 + return -ENOMEM; 123 + } 124 + } 125 + 126 + bpb = (struct pbr64 *)sbi->pbr_bh->b_data; 127 + bpb->bsx.vol_flags = cpu_to_le16(new_flag); 128 + 129 + if (new_flag == VOL_DIRTY && !buffer_dirty(sbi->pbr_bh)) 130 + sync = true; 131 + else 132 + sync = false; 133 + 134 + set_buffer_uptodate(sbi->pbr_bh); 135 + mark_buffer_dirty(sbi->pbr_bh); 136 + 137 + if (sync) 138 + sync_dirty_buffer(sbi->pbr_bh); 139 + return 0; 140 + } 141 + 142 + static int exfat_show_options(struct seq_file *m, struct dentry *root) 143 + { 144 + struct super_block *sb = root->d_sb; 145 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 146 + struct exfat_mount_options *opts = &sbi->options; 147 + 148 + /* Show partition info */ 149 + if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID)) 150 + seq_printf(m, ",uid=%u", 151 + from_kuid_munged(&init_user_ns, opts->fs_uid)); 152 + if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID)) 153 + seq_printf(m, ",gid=%u", 154 + from_kgid_munged(&init_user_ns, opts->fs_gid)); 155 + seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask); 156 + if (opts->allow_utime) 157 + seq_printf(m, ",allow_utime=%04o", opts->allow_utime); 158 + if (opts->utf8) 159 + seq_puts(m, ",iocharset=utf8"); 160 + else if (sbi->nls_io) 161 + seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); 162 + seq_printf(m, ",bps=%ld", sb->s_blocksize); 163 + if (opts->errors == EXFAT_ERRORS_CONT) 164 + seq_puts(m, ",errors=continue"); 165 + else if (opts->errors == EXFAT_ERRORS_PANIC) 166 + seq_puts(m, ",errors=panic"); 167 + else 168 + seq_puts(m, ",errors=remount-ro"); 169 + if (opts->discard) 170 + seq_puts(m, ",discard"); 171 + if (opts->time_offset) 172 + seq_printf(m, ",time_offset=%d", opts->time_offset); 173 + return 0; 174 + } 175 + 176 + static struct inode *exfat_alloc_inode(struct super_block *sb) 177 + { 178 + struct exfat_inode_info *ei; 179 + 180 + ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS); 181 + if (!ei) 182 + return NULL; 183 + 184 + init_rwsem(&ei->truncate_lock); 185 + return &ei->vfs_inode; 186 + } 187 + 188 + static void exfat_free_inode(struct inode *inode) 189 + { 190 + kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode)); 191 + } 192 + 193 + static const struct super_operations exfat_sops = { 194 + .alloc_inode = exfat_alloc_inode, 195 + .free_inode = exfat_free_inode, 196 + .write_inode = exfat_write_inode, 197 + .evict_inode = exfat_evict_inode, 198 + .put_super = exfat_put_super, 199 + .sync_fs = exfat_sync_fs, 200 + .statfs = exfat_statfs, 201 + .show_options = exfat_show_options, 202 + }; 203 + 204 + enum { 205 + Opt_uid, 206 + Opt_gid, 207 + Opt_umask, 208 + Opt_dmask, 209 + Opt_fmask, 210 + Opt_allow_utime, 211 + Opt_charset, 212 + Opt_errors, 213 + Opt_discard, 214 + Opt_time_offset, 215 + }; 216 + 217 + static const struct constant_table exfat_param_enums[] = { 218 + { "continue", EXFAT_ERRORS_CONT }, 219 + { "panic", EXFAT_ERRORS_PANIC }, 220 + { "remount-ro", EXFAT_ERRORS_RO }, 221 + {} 222 + }; 223 + 224 + static const struct fs_parameter_spec exfat_parameters[] = { 225 + fsparam_u32("uid", Opt_uid), 226 + fsparam_u32("gid", Opt_gid), 227 + fsparam_u32oct("umask", Opt_umask), 228 + fsparam_u32oct("dmask", Opt_dmask), 229 + fsparam_u32oct("fmask", Opt_fmask), 230 + fsparam_u32oct("allow_utime", Opt_allow_utime), 231 + fsparam_string("iocharset", Opt_charset), 232 + fsparam_enum("errors", Opt_errors, exfat_param_enums), 233 + fsparam_flag("discard", Opt_discard), 234 + fsparam_s32("time_offset", Opt_time_offset), 235 + {} 236 + }; 237 + 238 + static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param) 239 + { 240 + struct exfat_sb_info *sbi = fc->s_fs_info; 241 + struct exfat_mount_options *opts = &sbi->options; 242 + struct fs_parse_result result; 243 + int opt; 244 + 245 + opt = fs_parse(fc, exfat_parameters, param, &result); 246 + if (opt < 0) 247 + return opt; 248 + 249 + switch (opt) { 250 + case Opt_uid: 251 + opts->fs_uid = make_kuid(current_user_ns(), result.uint_32); 252 + break; 253 + case Opt_gid: 254 + opts->fs_gid = make_kgid(current_user_ns(), result.uint_32); 255 + break; 256 + case Opt_umask: 257 + opts->fs_fmask = result.uint_32; 258 + opts->fs_dmask = result.uint_32; 259 + break; 260 + case Opt_dmask: 261 + opts->fs_dmask = result.uint_32; 262 + break; 263 + case Opt_fmask: 264 + opts->fs_fmask = result.uint_32; 265 + break; 266 + case Opt_allow_utime: 267 + opts->allow_utime = result.uint_32 & 0022; 268 + break; 269 + case Opt_charset: 270 + exfat_free_iocharset(sbi); 271 + opts->iocharset = kstrdup(param->string, GFP_KERNEL); 272 + if (!opts->iocharset) 273 + return -ENOMEM; 274 + break; 275 + case Opt_errors: 276 + opts->errors = result.uint_32; 277 + break; 278 + case Opt_discard: 279 + opts->discard = 1; 280 + break; 281 + case Opt_time_offset: 282 + /* 283 + * Make the limit 24 just in case someone invents something 284 + * unusual. 285 + */ 286 + if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60) 287 + return -EINVAL; 288 + opts->time_offset = result.int_32; 289 + break; 290 + default: 291 + return -EINVAL; 292 + } 293 + 294 + return 0; 295 + } 296 + 297 + static void exfat_hash_init(struct super_block *sb) 298 + { 299 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 300 + int i; 301 + 302 + spin_lock_init(&sbi->inode_hash_lock); 303 + for (i = 0; i < EXFAT_HASH_SIZE; i++) 304 + INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); 305 + } 306 + 307 + static int exfat_read_root(struct inode *inode) 308 + { 309 + struct super_block *sb = inode->i_sb; 310 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 311 + struct exfat_inode_info *ei = EXFAT_I(inode); 312 + struct exfat_chain cdir; 313 + int num_subdirs, num_clu = 0; 314 + 315 + exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 316 + ei->entry = -1; 317 + ei->start_clu = sbi->root_dir; 318 + ei->flags = ALLOC_FAT_CHAIN; 319 + ei->type = TYPE_DIR; 320 + ei->version = 0; 321 + ei->rwoffset = 0; 322 + ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 323 + ei->hint_stat.eidx = 0; 324 + ei->hint_stat.clu = sbi->root_dir; 325 + ei->hint_femp.eidx = EXFAT_HINT_NONE; 326 + 327 + exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 328 + if (exfat_count_num_clusters(sb, &cdir, &num_clu)) 329 + return -EIO; 330 + i_size_write(inode, num_clu << sbi->cluster_size_bits); 331 + 332 + num_subdirs = exfat_count_dir_entries(sb, &cdir); 333 + if (num_subdirs < 0) 334 + return -EIO; 335 + set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR); 336 + 337 + inode->i_uid = sbi->options.fs_uid; 338 + inode->i_gid = sbi->options.fs_gid; 339 + inode_inc_iversion(inode); 340 + inode->i_generation = 0; 341 + inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777); 342 + inode->i_op = &exfat_dir_inode_operations; 343 + inode->i_fop = &exfat_dir_operations; 344 + 345 + inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) 346 + & ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 347 + EXFAT_I(inode)->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff; 348 + EXFAT_I(inode)->i_size_aligned = i_size_read(inode); 349 + EXFAT_I(inode)->i_size_ondisk = i_size_read(inode); 350 + 351 + exfat_save_attr(inode, ATTR_SUBDIR); 352 + inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 353 + current_time(inode); 354 + exfat_cache_init_inode(inode); 355 + return 0; 356 + } 357 + 358 + static struct pbr *exfat_read_pbr_with_logical_sector(struct super_block *sb, 359 + struct buffer_head **prev_bh) 360 + { 361 + struct pbr *p_pbr = (struct pbr *) (*prev_bh)->b_data; 362 + unsigned short logical_sect = 0; 363 + 364 + logical_sect = 1 << p_pbr->bsx.f64.sect_size_bits; 365 + 366 + if (!is_power_of_2(logical_sect) || 367 + logical_sect < 512 || logical_sect > 4096) { 368 + exfat_msg(sb, KERN_ERR, "bogus logical sector size %u", 369 + logical_sect); 370 + return NULL; 371 + } 372 + 373 + if (logical_sect < sb->s_blocksize) { 374 + exfat_msg(sb, KERN_ERR, 375 + "logical sector size too small for device (logical sector size = %u)", 376 + logical_sect); 377 + return NULL; 378 + } 379 + 380 + if (logical_sect > sb->s_blocksize) { 381 + struct buffer_head *bh = NULL; 382 + 383 + __brelse(*prev_bh); 384 + *prev_bh = NULL; 385 + 386 + if (!sb_set_blocksize(sb, logical_sect)) { 387 + exfat_msg(sb, KERN_ERR, 388 + "unable to set blocksize %u", logical_sect); 389 + return NULL; 390 + } 391 + bh = sb_bread(sb, 0); 392 + if (!bh) { 393 + exfat_msg(sb, KERN_ERR, 394 + "unable to read boot sector (logical sector size = %lu)", 395 + sb->s_blocksize); 396 + return NULL; 397 + } 398 + 399 + *prev_bh = bh; 400 + p_pbr = (struct pbr *) bh->b_data; 401 + } 402 + return p_pbr; 403 + } 404 + 405 + /* mount the file system volume */ 406 + static int __exfat_fill_super(struct super_block *sb) 407 + { 408 + int ret; 409 + struct pbr *p_pbr; 410 + struct pbr64 *p_bpb; 411 + struct buffer_head *bh; 412 + struct exfat_sb_info *sbi = EXFAT_SB(sb); 413 + 414 + /* set block size to read super block */ 415 + sb_min_blocksize(sb, 512); 416 + 417 + /* read boot sector */ 418 + bh = sb_bread(sb, 0); 419 + if (!bh) { 420 + exfat_msg(sb, KERN_ERR, "unable to read boot sector"); 421 + return -EIO; 422 + } 423 + 424 + /* PRB is read */ 425 + p_pbr = (struct pbr *)bh->b_data; 426 + 427 + /* check the validity of PBR */ 428 + if (le16_to_cpu((p_pbr->signature)) != PBR_SIGNATURE) { 429 + exfat_msg(sb, KERN_ERR, "invalid boot record signature"); 430 + ret = -EINVAL; 431 + goto free_bh; 432 + } 433 + 434 + 435 + /* check logical sector size */ 436 + p_pbr = exfat_read_pbr_with_logical_sector(sb, &bh); 437 + if (!p_pbr) { 438 + ret = -EIO; 439 + goto free_bh; 440 + } 441 + 442 + /* 443 + * res_zero field must be filled with zero to prevent mounting 444 + * from FAT volume. 445 + */ 446 + if (memchr_inv(p_pbr->bpb.f64.res_zero, 0, 447 + sizeof(p_pbr->bpb.f64.res_zero))) { 448 + ret = -EINVAL; 449 + goto free_bh; 450 + } 451 + 452 + p_bpb = (struct pbr64 *)p_pbr; 453 + if (!p_bpb->bsx.num_fats) { 454 + exfat_msg(sb, KERN_ERR, "bogus number of FAT structure"); 455 + ret = -EINVAL; 456 + goto free_bh; 457 + } 458 + 459 + sbi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits; 460 + sbi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits; 461 + sbi->cluster_size_bits = sbi->sect_per_clus_bits + sb->s_blocksize_bits; 462 + sbi->cluster_size = 1 << sbi->cluster_size_bits; 463 + sbi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length); 464 + sbi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset); 465 + sbi->FAT2_start_sector = p_bpb->bsx.num_fats == 1 ? 466 + sbi->FAT1_start_sector : 467 + sbi->FAT1_start_sector + sbi->num_FAT_sectors; 468 + sbi->data_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset); 469 + sbi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length); 470 + /* because the cluster index starts with 2 */ 471 + sbi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) + 472 + EXFAT_RESERVED_CLUSTERS; 473 + 474 + sbi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster); 475 + sbi->dentries_per_clu = 1 << 476 + (sbi->cluster_size_bits - DENTRY_SIZE_BITS); 477 + 478 + sbi->vol_flag = le16_to_cpu(p_bpb->bsx.vol_flags); 479 + sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; 480 + sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED; 481 + 482 + if (le16_to_cpu(p_bpb->bsx.vol_flags) & VOL_DIRTY) { 483 + sbi->vol_flag |= VOL_DIRTY; 484 + exfat_msg(sb, KERN_WARNING, 485 + "Volume was not properly unmounted. Some data may be corrupt. Please run fsck."); 486 + } 487 + 488 + /* exFAT file size is limited by a disk volume size */ 489 + sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) << 490 + sbi->cluster_size_bits; 491 + 492 + ret = exfat_create_upcase_table(sb); 493 + if (ret) { 494 + exfat_msg(sb, KERN_ERR, "failed to load upcase table"); 495 + goto free_bh; 496 + } 497 + 498 + ret = exfat_load_bitmap(sb); 499 + if (ret) { 500 + exfat_msg(sb, KERN_ERR, "failed to load alloc-bitmap"); 501 + goto free_upcase_table; 502 + } 503 + 504 + ret = exfat_count_used_clusters(sb, &sbi->used_clusters); 505 + if (ret) { 506 + exfat_msg(sb, KERN_ERR, "failed to scan clusters"); 507 + goto free_alloc_bitmap; 508 + } 509 + 510 + return 0; 511 + 512 + free_alloc_bitmap: 513 + exfat_free_bitmap(sbi); 514 + free_upcase_table: 515 + exfat_free_upcase_table(sbi); 516 + free_bh: 517 + brelse(bh); 518 + return ret; 519 + } 520 + 521 + static int exfat_fill_super(struct super_block *sb, struct fs_context *fc) 522 + { 523 + struct exfat_sb_info *sbi = sb->s_fs_info; 524 + struct exfat_mount_options *opts = &sbi->options; 525 + struct inode *root_inode; 526 + int err; 527 + 528 + if (opts->allow_utime == (unsigned short)-1) 529 + opts->allow_utime = ~opts->fs_dmask & 0022; 530 + 531 + if (opts->discard) { 532 + struct request_queue *q = bdev_get_queue(sb->s_bdev); 533 + 534 + if (!blk_queue_discard(q)) 535 + exfat_msg(sb, KERN_WARNING, 536 + "mounting with \"discard\" option, but the device does not support discard"); 537 + opts->discard = 0; 538 + } 539 + 540 + sb->s_flags |= SB_NODIRATIME; 541 + sb->s_magic = EXFAT_SUPER_MAGIC; 542 + sb->s_op = &exfat_sops; 543 + 544 + sb->s_time_gran = 1; 545 + sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS; 546 + sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS; 547 + 548 + err = __exfat_fill_super(sb); 549 + if (err) { 550 + exfat_msg(sb, KERN_ERR, "failed to recognize exfat type"); 551 + goto check_nls_io; 552 + } 553 + 554 + /* set up enough so that it can read an inode */ 555 + exfat_hash_init(sb); 556 + 557 + if (!strcmp(sbi->options.iocharset, "utf8")) 558 + opts->utf8 = 1; 559 + else { 560 + sbi->nls_io = load_nls(sbi->options.iocharset); 561 + if (!sbi->nls_io) { 562 + exfat_msg(sb, KERN_ERR, "IO charset %s not found", 563 + sbi->options.iocharset); 564 + err = -EINVAL; 565 + goto free_table; 566 + } 567 + } 568 + 569 + if (sbi->options.utf8) 570 + sb->s_d_op = &exfat_utf8_dentry_ops; 571 + else 572 + sb->s_d_op = &exfat_dentry_ops; 573 + 574 + root_inode = new_inode(sb); 575 + if (!root_inode) { 576 + exfat_msg(sb, KERN_ERR, "failed to allocate root inode."); 577 + err = -ENOMEM; 578 + goto free_table; 579 + } 580 + 581 + root_inode->i_ino = EXFAT_ROOT_INO; 582 + inode_set_iversion(root_inode, 1); 583 + err = exfat_read_root(root_inode); 584 + if (err) { 585 + exfat_msg(sb, KERN_ERR, "failed to initialize root inode."); 586 + goto put_inode; 587 + } 588 + 589 + exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos); 590 + insert_inode_hash(root_inode); 591 + 592 + sb->s_root = d_make_root(root_inode); 593 + if (!sb->s_root) { 594 + exfat_msg(sb, KERN_ERR, "failed to get the root dentry"); 595 + err = -ENOMEM; 596 + goto put_inode; 597 + } 598 + 599 + return 0; 600 + 601 + put_inode: 602 + iput(root_inode); 603 + sb->s_root = NULL; 604 + 605 + free_table: 606 + exfat_free_upcase_table(sbi); 607 + exfat_free_bitmap(sbi); 608 + 609 + check_nls_io: 610 + unload_nls(sbi->nls_io); 611 + exfat_free_iocharset(sbi); 612 + sb->s_fs_info = NULL; 613 + kfree(sbi); 614 + return err; 615 + } 616 + 617 + static int exfat_get_tree(struct fs_context *fc) 618 + { 619 + return get_tree_bdev(fc, exfat_fill_super); 620 + } 621 + 622 + static void exfat_free(struct fs_context *fc) 623 + { 624 + kfree(fc->s_fs_info); 625 + } 626 + 627 + static const struct fs_context_operations exfat_context_ops = { 628 + .parse_param = exfat_parse_param, 629 + .get_tree = exfat_get_tree, 630 + .free = exfat_free, 631 + }; 632 + 633 + static int exfat_init_fs_context(struct fs_context *fc) 634 + { 635 + struct exfat_sb_info *sbi; 636 + 637 + sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL); 638 + if (!sbi) 639 + return -ENOMEM; 640 + 641 + mutex_init(&sbi->s_lock); 642 + ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, 643 + DEFAULT_RATELIMIT_BURST); 644 + 645 + sbi->options.fs_uid = current_uid(); 646 + sbi->options.fs_gid = current_gid(); 647 + sbi->options.fs_fmask = current->fs->umask; 648 + sbi->options.fs_dmask = current->fs->umask; 649 + sbi->options.allow_utime = -1; 650 + sbi->options.iocharset = exfat_default_iocharset; 651 + sbi->options.errors = EXFAT_ERRORS_RO; 652 + 653 + fc->s_fs_info = sbi; 654 + fc->ops = &exfat_context_ops; 655 + return 0; 656 + } 657 + 658 + static struct file_system_type exfat_fs_type = { 659 + .owner = THIS_MODULE, 660 + .name = "exfat", 661 + .init_fs_context = exfat_init_fs_context, 662 + .parameters = exfat_parameters, 663 + .kill_sb = kill_block_super, 664 + .fs_flags = FS_REQUIRES_DEV, 665 + }; 666 + 667 + static void exfat_inode_init_once(void *foo) 668 + { 669 + struct exfat_inode_info *ei = (struct exfat_inode_info *)foo; 670 + 671 + INIT_HLIST_NODE(&ei->i_hash_fat); 672 + inode_init_once(&ei->vfs_inode); 673 + } 674 + 675 + static int __init init_exfat_fs(void) 676 + { 677 + int err; 678 + 679 + err = exfat_cache_init(); 680 + if (err) 681 + return err; 682 + 683 + exfat_inode_cachep = kmem_cache_create("exfat_inode_cache", 684 + sizeof(struct exfat_inode_info), 685 + 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, 686 + exfat_inode_init_once); 687 + if (!exfat_inode_cachep) { 688 + err = -ENOMEM; 689 + goto shutdown_cache; 690 + } 691 + 692 + err = register_filesystem(&exfat_fs_type); 693 + if (err) 694 + goto destroy_cache; 695 + 696 + return 0; 697 + 698 + destroy_cache: 699 + kmem_cache_destroy(exfat_inode_cachep); 700 + shutdown_cache: 701 + exfat_cache_shutdown(); 702 + return err; 703 + } 704 + 705 + static void __exit exit_exfat_fs(void) 706 + { 707 + /* 708 + * Make sure all delayed rcu free inodes are flushed before we 709 + * destroy cache. 710 + */ 711 + rcu_barrier(); 712 + kmem_cache_destroy(exfat_inode_cachep); 713 + unregister_filesystem(&exfat_fs_type); 714 + exfat_cache_shutdown(); 715 + } 716 + 717 + module_init(init_exfat_fs); 718 + module_exit(exit_exfat_fs); 719 + 720 + MODULE_LICENSE("GPL"); 721 + MODULE_DESCRIPTION("exFAT filesystem support"); 722 + MODULE_AUTHOR("Samsung Electronics Co., Ltd.");