├── .gitignore ├── Kconfig ├── LICENSE ├── Makefile ├── README.md ├── balloc.c ├── cache.c ├── compat.h ├── config.h ├── debian ├── changelog ├── compat ├── control ├── copyright ├── exfat-dkms.dkms ├── exfat-dkms.install ├── local │ └── print_rule.mk └── rules ├── dir.c ├── exfat_fs.h ├── exfat_raw.h ├── fatent.c ├── file.c ├── inode.c ├── misc.c ├── namei.c ├── nls.c ├── super.c ├── version.h └── xattr.c /.gitignore: -------------------------------------------------------------------------------- 1 | # 2 | # NOTE! Don't add files that are generated in specific 3 | # subdirectories here. Add them in the ".gitignore" file 4 | # in that subdirectory instead. 5 | # 6 | # NOTE! Please use 'git ls-files -i --exclude-standard' 7 | # command after changing this file, to see if there are 8 | # any tracked files which get ignored after the change. 9 | # 10 | # Normal rules (sorted alphabetically) 11 | # 12 | .* 13 | *.a 14 | *.asn1.[ch] 15 | *.bin 16 | *.bz2 17 | *.c.[012]*.* 18 | *.dt.yaml 19 | *.dtb 20 | *.dtb.S 21 | *.dwo 22 | *.elf 23 | *.gcno 24 | *.gz 25 | *.i 26 | *.ko 27 | *.lex.c 28 | *.ll 29 | *.lst 30 | *.lz4 31 | *.lzma 32 | *.lzo 33 | *.mod 34 | *.mod.c 35 | *.o 36 | *.o.* 37 | *.order 38 | *.patch 39 | *.s 40 | *.so 41 | *.so.dbg 42 | *.su 43 | *.symtypes 44 | *.tab.[ch] 45 | *.tar 46 | *.xz 47 | Module.symvers 48 | modules.builtin 49 | 50 | # 51 | # Top-level generic files 52 | # 53 | /tags 54 | /TAGS 55 | /linux 56 | /vmlinux 57 | /vmlinux.32 58 | /vmlinux-gdb.py 59 | /vmlinuz 60 | /System.map 61 | /Module.markers 62 | /modules.builtin.modinfo 63 | 64 | # 65 | # RPM spec file (make rpm-pkg) 66 | # 67 | /*.spec 68 | 69 | # 70 | # Snap directory (make snap-pkg) 71 | # 72 | /snap/ 73 | 74 | # 75 | # tar directory (make tar*-pkg) 76 | # 77 | /tar-install/ 78 | 79 | # 80 | # We don't want to ignore the following even if they are dot-files 81 | # 82 | !.clang-format 83 | !.cocciconfig 84 | !.get_maintainer.ignore 85 | !.gitattributes 86 | !.gitignore 87 | !.mailmap 88 | 89 | # 90 | # Generated include files 91 | # 92 | /include/config/ 93 | /include/generated/ 94 | /include/ksym/ 95 | /arch/*/include/generated/ 96 | 97 | # stgit generated dirs 98 | patches-* 99 | 100 | # quilt's files 101 | patches 102 | series 103 | 104 | # cscope files 105 | cscope.* 106 | ncscope.* 107 | 108 | # gnu global files 109 | GPATH 110 | GRTAGS 111 | GSYMS 112 | GTAGS 113 | 114 | # id-utils files 115 | ID 116 | 117 | *.orig 118 | *~ 119 | \#*# 120 | 121 | # 122 | # Leavings from module signing 123 | # 124 | extra_certificates 125 | signing_key.pem 126 | signing_key.priv 127 | signing_key.x509 128 | x509.genkey 129 | 130 | # Kconfig presets 131 | /all.config 132 | /alldef.config 133 | /allmod.config 134 | /allno.config 135 | /allrandom.config 136 | /allyes.config 137 | 138 | # Kdevelop4 139 | *.kdev4 140 | 141 | # Clang's compilation database file 142 | /compile_commands.json 143 | -------------------------------------------------------------------------------- /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 | if EXFAT_FS 14 | 15 | config EXFAT_DEFAULT_IOCHARSET 16 | string "Default iocharset for exFAT" 17 | default "utf8" 18 | help 19 | Set this to the default input/output character set to use for 20 | converting between the encoding that is used for user visible 21 | filenames and the UTF-16 character encoding that the exFAT 22 | filesystem uses. This can be overridden with the "iocharset" mount 23 | option for the exFAT filesystems. 24 | 25 | config EXFAT_VIRTUAL_XATTR 26 | bool "Virtual xattr support for exFAT" 27 | default y 28 | help 29 | To support virtual xattr. 30 | 31 | config EXFAT_VIRTUAL_XATTR_SELINUX_LABEL 32 | string "Default string for SELinux label" 33 | default "u:object_r:exfat:s0" 34 | depends on EXFAT_VIRTUAL_XATTR 35 | help 36 | Set this to the default string for SELinux label. 37 | Support for "u:object_r:exfat:s0" was added in Android Pie, 38 | if you're running Oreo or lower, use "u:object_r:vfat:s0" instead. 39 | 40 | endif # if EXFAT_FS 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Valid-License-Identifier: GPL-2.0 2 | Valid-License-Identifier: GPL-2.0-only 3 | Valid-License-Identifier: GPL-2.0+ 4 | Valid-License-Identifier: GPL-2.0-or-later 5 | SPDX-URL: https://spdx.org/licenses/GPL-2.0.html 6 | Usage-Guide: 7 | To use this license in source code, put one of the following SPDX 8 | tag/value pairs into a comment according to the placement 9 | guidelines in the licensing rules documentation. 10 | For 'GNU General Public License (GPL) version 2 only' use: 11 | SPDX-License-Identifier: GPL-2.0 12 | or 13 | SPDX-License-Identifier: GPL-2.0-only 14 | For 'GNU General Public License (GPL) version 2 or any later version' use: 15 | SPDX-License-Identifier: GPL-2.0+ 16 | or 17 | SPDX-License-Identifier: GPL-2.0-or-later 18 | License-Text: 19 | 20 | GNU GENERAL PUBLIC LICENSE 21 | Version 2, June 1991 22 | 23 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 24 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 25 | Everyone is permitted to copy and distribute verbatim copies 26 | of this license document, but changing it is not allowed. 27 | 28 | Preamble 29 | 30 | The licenses for most software are designed to take away your 31 | freedom to share and change it. By contrast, the GNU General Public 32 | License is intended to guarantee your freedom to share and change free 33 | software--to make sure the software is free for all its users. This 34 | General Public License applies to most of the Free Software 35 | Foundation's software and to any other program whose authors commit to 36 | using it. (Some other Free Software Foundation software is covered by 37 | the GNU Library General Public License instead.) You can apply it to 38 | your programs, too. 39 | 40 | When we speak of free software, we are referring to freedom, not 41 | price. Our General Public Licenses are designed to make sure that you 42 | have the freedom to distribute copies of free software (and charge for 43 | this service if you wish), that you receive source code or can get it 44 | if you want it, that you can change the software or use pieces of it 45 | in new free programs; and that you know you can do these things. 46 | 47 | To protect your rights, we need to make restrictions that forbid 48 | anyone to deny you these rights or to ask you to surrender the rights. 49 | These restrictions translate to certain responsibilities for you if you 50 | distribute copies of the software, or if you modify it. 51 | 52 | For example, if you distribute copies of such a program, whether 53 | gratis or for a fee, you must give the recipients all the rights that 54 | you have. You must make sure that they, too, receive or can get the 55 | source code. And you must show them these terms so they know their 56 | rights. 57 | 58 | We protect your rights with two steps: (1) copyright the software, and 59 | (2) offer you this license which gives you legal permission to copy, 60 | distribute and/or modify the software. 61 | 62 | Also, for each author's protection and ours, we want to make certain 63 | that everyone understands that there is no warranty for this free 64 | software. If the software is modified by someone else and passed on, we 65 | want its recipients to know that what they have is not the original, so 66 | that any problems introduced by others will not reflect on the original 67 | authors' reputations. 68 | 69 | Finally, any free program is threatened constantly by software 70 | patents. We wish to avoid the danger that redistributors of a free 71 | program will individually obtain patent licenses, in effect making the 72 | program proprietary. To prevent this, we have made it clear that any 73 | patent must be licensed for everyone's free use or not licensed at all. 74 | 75 | The precise terms and conditions for copying, distribution and 76 | modification follow. 77 | 78 | GNU GENERAL PUBLIC LICENSE 79 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 80 | 81 | 0. This License applies to any program or other work which contains 82 | a notice placed by the copyright holder saying it may be distributed 83 | under the terms of this General Public License. The "Program", below, 84 | refers to any such program or work, and a "work based on the Program" 85 | means either the Program or any derivative work under copyright law: 86 | that is to say, a work containing the Program or a portion of it, 87 | either verbatim or with modifications and/or translated into another 88 | language. (Hereinafter, translation is included without limitation in 89 | the term "modification".) Each licensee is addressed as "you". 90 | 91 | Activities other than copying, distribution and modification are not 92 | covered by this License; they are outside its scope. The act of 93 | running the Program is not restricted, and the output from the Program 94 | is covered only if its contents constitute a work based on the 95 | Program (independent of having been made by running the Program). 96 | Whether that is true depends on what the Program does. 97 | 98 | 1. You may copy and distribute verbatim copies of the Program's 99 | source code as you receive it, in any medium, provided that you 100 | conspicuously and appropriately publish on each copy an appropriate 101 | copyright notice and disclaimer of warranty; keep intact all the 102 | notices that refer to this License and to the absence of any warranty; 103 | and give any other recipients of the Program a copy of this License 104 | along with the Program. 105 | 106 | You may charge a fee for the physical act of transferring a copy, and 107 | you may at your option offer warranty protection in exchange for a fee. 108 | 109 | 2. You may modify your copy or copies of the Program or any portion 110 | of it, thus forming a work based on the Program, and copy and 111 | distribute such modifications or work under the terms of Section 1 112 | above, provided that you also meet all of these conditions: 113 | 114 | a) You must cause the modified files to carry prominent notices 115 | stating that you changed the files and the date of any change. 116 | 117 | b) You must cause any work that you distribute or publish, that in 118 | whole or in part contains or is derived from the Program or any 119 | part thereof, to be licensed as a whole at no charge to all third 120 | parties under the terms of this License. 121 | 122 | c) If the modified program normally reads commands interactively 123 | when run, you must cause it, when started running for such 124 | interactive use in the most ordinary way, to print or display an 125 | announcement including an appropriate copyright notice and a 126 | notice that there is no warranty (or else, saying that you provide 127 | a warranty) and that users may redistribute the program under 128 | these conditions, and telling the user how to view a copy of this 129 | License. (Exception: if the Program itself is interactive but 130 | does not normally print such an announcement, your work based on 131 | the Program is not required to print an announcement.) 132 | 133 | These requirements apply to the modified work as a whole. If 134 | identifiable sections of that work are not derived from the Program, 135 | and can be reasonably considered independent and separate works in 136 | themselves, then this License, and its terms, do not apply to those 137 | sections when you distribute them as separate works. But when you 138 | distribute the same sections as part of a whole which is a work based 139 | on the Program, the distribution of the whole must be on the terms of 140 | this License, whose permissions for other licensees extend to the 141 | entire whole, and thus to each and every part regardless of who wrote it. 142 | 143 | Thus, it is not the intent of this section to claim rights or contest 144 | your rights to work written entirely by you; rather, the intent is to 145 | exercise the right to control the distribution of derivative or 146 | collective works based on the Program. 147 | 148 | In addition, mere aggregation of another work not based on the Program 149 | with the Program (or with a work based on the Program) on a volume of 150 | a storage or distribution medium does not bring the other work under 151 | the scope of this License. 152 | 153 | 3. You may copy and distribute the Program (or a work based on it, 154 | under Section 2) in object code or executable form under the terms of 155 | Sections 1 and 2 above provided that you also do one of the following: 156 | 157 | a) Accompany it with the complete corresponding machine-readable 158 | source code, which must be distributed under the terms of Sections 159 | 1 and 2 above on a medium customarily used for software interchange; or, 160 | 161 | b) Accompany it with a written offer, valid for at least three 162 | years, to give any third party, for a charge no more than your 163 | cost of physically performing source distribution, a complete 164 | machine-readable copy of the corresponding source code, to be 165 | distributed under the terms of Sections 1 and 2 above on a medium 166 | customarily used for software interchange; or, 167 | 168 | c) Accompany it with the information you received as to the offer 169 | to distribute corresponding source code. (This alternative is 170 | allowed only for noncommercial distribution and only if you 171 | received the program in object code or executable form with such 172 | an offer, in accord with Subsection b above.) 173 | 174 | The source code for a work means the preferred form of the work for 175 | making modifications to it. For an executable work, complete source 176 | code means all the source code for all modules it contains, plus any 177 | associated interface definition files, plus the scripts used to 178 | control compilation and installation of the executable. However, as a 179 | special exception, the source code distributed need not include 180 | anything that is normally distributed (in either source or binary 181 | form) with the major components (compiler, kernel, and so on) of the 182 | operating system on which the executable runs, unless that component 183 | itself accompanies the executable. 184 | 185 | If distribution of executable or object code is made by offering 186 | access to copy from a designated place, then offering equivalent 187 | access to copy the source code from the same place counts as 188 | distribution of the source code, even though third parties are not 189 | compelled to copy the source along with the object code. 190 | 191 | 4. You may not copy, modify, sublicense, or distribute the Program 192 | except as expressly provided under this License. Any attempt 193 | otherwise to copy, modify, sublicense or distribute the Program is 194 | void, and will automatically terminate your rights under this License. 195 | However, parties who have received copies, or rights, from you under 196 | this License will not have their licenses terminated so long as such 197 | parties remain in full compliance. 198 | 199 | 5. You are not required to accept this License, since you have not 200 | signed it. However, nothing else grants you permission to modify or 201 | distribute the Program or its derivative works. These actions are 202 | prohibited by law if you do not accept this License. Therefore, by 203 | modifying or distributing the Program (or any work based on the 204 | Program), you indicate your acceptance of this License to do so, and 205 | all its terms and conditions for copying, distributing or modifying 206 | the Program or works based on it. 207 | 208 | 6. Each time you redistribute the Program (or any work based on the 209 | Program), the recipient automatically receives a license from the 210 | original licensor to copy, distribute or modify the Program subject to 211 | these terms and conditions. You may not impose any further 212 | restrictions on the recipients' exercise of the rights granted herein. 213 | You are not responsible for enforcing compliance by third parties to 214 | this License. 215 | 216 | 7. If, as a consequence of a court judgment or allegation of patent 217 | infringement or for any other reason (not limited to patent issues), 218 | conditions are imposed on you (whether by court order, agreement or 219 | otherwise) that contradict the conditions of this License, they do not 220 | excuse you from the conditions of this License. If you cannot 221 | distribute so as to satisfy simultaneously your obligations under this 222 | License and any other pertinent obligations, then as a consequence you 223 | may not distribute the Program at all. For example, if a patent 224 | license would not permit royalty-free redistribution of the Program by 225 | all those who receive copies directly or indirectly through you, then 226 | the only way you could satisfy both it and this License would be to 227 | refrain entirely from distribution of the Program. 228 | 229 | If any portion of this section is held invalid or unenforceable under 230 | any particular circumstance, the balance of the section is intended to 231 | apply and the section as a whole is intended to apply in other 232 | circumstances. 233 | 234 | It is not the purpose of this section to induce you to infringe any 235 | patents or other property right claims or to contest validity of any 236 | such claims; this section has the sole purpose of protecting the 237 | integrity of the free software distribution system, which is 238 | implemented by public license practices. Many people have made 239 | generous contributions to the wide range of software distributed 240 | through that system in reliance on consistent application of that 241 | system; it is up to the author/donor to decide if he or she is willing 242 | to distribute software through any other system and a licensee cannot 243 | impose that choice. 244 | 245 | This section is intended to make thoroughly clear what is believed to 246 | be a consequence of the rest of this License. 247 | 248 | 8. If the distribution and/or use of the Program is restricted in 249 | certain countries either by patents or by copyrighted interfaces, the 250 | original copyright holder who places the Program under this License 251 | may add an explicit geographical distribution limitation excluding 252 | those countries, so that distribution is permitted only in or among 253 | countries not thus excluded. In such case, this License incorporates 254 | the limitation as if written in the body of this License. 255 | 256 | 9. The Free Software Foundation may publish revised and/or new versions 257 | of the General Public License from time to time. Such new versions will 258 | be similar in spirit to the present version, but may differ in detail to 259 | address new problems or concerns. 260 | 261 | Each version is given a distinguishing version number. If the Program 262 | specifies a version number of this License which applies to it and "any 263 | later version", you have the option of following the terms and conditions 264 | either of that version or of any later version published by the Free 265 | Software Foundation. If the Program does not specify a version number of 266 | this License, you may choose any version ever published by the Free Software 267 | Foundation. 268 | 269 | 10. If you wish to incorporate parts of the Program into other free 270 | programs whose distribution conditions are different, write to the author 271 | to ask for permission. For software which is copyrighted by the Free 272 | Software Foundation, write to the Free Software Foundation; we sometimes 273 | make exceptions for this. Our decision will be guided by the two goals 274 | of preserving the free status of all derivatives of our free software and 275 | of promoting the sharing and reuse of software generally. 276 | 277 | NO WARRANTY 278 | 279 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 280 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 281 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 282 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 283 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 284 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 285 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 286 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 287 | REPAIR OR CORRECTION. 288 | 289 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 290 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 291 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 292 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 293 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 294 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 295 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 296 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 297 | POSSIBILITY OF SUCH DAMAGES. 298 | 299 | END OF TERMS AND CONDITIONS 300 | 301 | How to Apply These Terms to Your New Programs 302 | 303 | If you develop a new program, and you want it to be of the greatest 304 | possible use to the public, the best way to achieve this is to make it 305 | free software which everyone can redistribute and change under these terms. 306 | 307 | To do so, attach the following notices to the program. It is safest 308 | to attach them to the start of each source file to most effectively 309 | convey the exclusion of warranty; and each file should have at least 310 | the "copyright" line and a pointer to where the full notice is found. 311 | 312 | 313 | Copyright (C) 314 | 315 | This program is free software; you can redistribute it and/or modify 316 | it under the terms of the GNU General Public License as published by 317 | the Free Software Foundation; either version 2 of the License, or 318 | (at your option) any later version. 319 | 320 | This program is distributed in the hope that it will be useful, 321 | but WITHOUT ANY WARRANTY; without even the implied warranty of 322 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 323 | GNU General Public License for more details. 324 | 325 | You should have received a copy of the GNU General Public License 326 | along with this program; if not, write to the Free Software 327 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 328 | 329 | 330 | Also add information on how to contact you by electronic and paper mail. 331 | 332 | If the program is interactive, make it output a short notice like this 333 | when it starts in an interactive mode: 334 | 335 | Gnomovision version 69, Copyright (C) year name of author 336 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 337 | This is free software, and you are welcome to redistribute it 338 | under certain conditions; type `show c' for details. 339 | 340 | The hypothetical commands `show w' and `show c' should show the appropriate 341 | parts of the General Public License. Of course, the commands you use may 342 | be called something other than `show w' and `show c'; they could even be 343 | mouse-clicks or menu items--whatever suits your program. 344 | 345 | You should also get your employer (if you work as a programmer) or your 346 | school, if any, to sign a "copyright disclaimer" for the program, if 347 | necessary. Here is a sample; alter the names: 348 | 349 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 350 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 351 | 352 | , 1 April 1989 353 | Ty Coon, President of Vice 354 | 355 | This General Public License does not permit incorporating your program into 356 | proprietary programs. If your program is a subroutine library, you may 357 | consider it more useful to permit linking proprietary applications with the 358 | library. If this is what you want to do, use the GNU Library General 359 | Public License instead of this License. 360 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: GPL-2.0-or-later 2 | # 3 | # Makefile for the linux exFAT filesystem support. 4 | # 5 | 6 | ifneq ($(KERNELRELEASE),) 7 | # Called from inline kernel build 8 | # DKMS_DEFINE 9 | obj-$(CONFIG_EXFAT_FS) += exfat.o 10 | 11 | exfat-objs := inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \ 12 | file.o balloc.o xattr.o 13 | else 14 | # Called from external kernel module build 15 | 16 | KERNELRELEASE ?= $(shell uname -r) 17 | KDIR ?= /lib/modules/${KERNELRELEASE}/build 18 | MDIR ?= /lib/modules/${KERNELRELEASE} 19 | PWD := $(shell pwd) 20 | 21 | export CONFIG_EXFAT_FS := m 22 | 23 | all: 24 | $(MAKE) -C $(KDIR) M=$(PWD) modules 25 | 26 | clean: 27 | $(MAKE) -C $(KDIR) M=$(PWD) clean 28 | 29 | help: 30 | $(MAKE) -C $(KDIR) M=$(PWD) help 31 | 32 | install: exfat.ko 33 | rm -f ${MDIR}/kernel/fs/exfat/exfat.ko 34 | install -m644 -b -D exfat.ko ${MDIR}/kernel/fs/exfat/exfat.ko 35 | depmod -aq 36 | 37 | uninstall: 38 | rm -rf ${MDIR}/kernel/fs/exfat 39 | depmod -aq 40 | 41 | endif 42 | 43 | .PHONY : all clean install uninstall 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # exfat-linux 2 | 3 | This __exFAT filesystem module for Linux kernel__ is a backport of the latest Linux mainline's exFAT drivers by Samsung. 4 | 5 | This project can be used for everyday Linux users by simply doing `make && make install`. Ubuntu users can simply add a PPA and start using it, without even downloading the code. This can also be directly dropped-in to an existing Linux kernel source for building the filesystem drivers inline, which should be useful for Android kernel developers. 6 | 7 | **exfat-linux** has been tested with all major LTS kernels ranging from v4.9 to v5.4 and the ones Canonical uses for Ubuntu: `v4.9`, `v4.14`, `v4.19`, `v5.4` and `v4.15`, `v5.3`, and `v5.6`. 8 | 9 | It's also been tested with `x86(i386)`, `x86_64(amd64)`, `arm32(AArch32)` and `arm64(AArch64)`. 10 | 11 | Linux kernels since `v5.4` includes an exFAT driver, but it is an extremely outdated version from 2016. This was later revised by Samsung directly with `v5.7`. 12 | 13 | People on `v5.7` kernel or higher can just use the bundled exFAT drivers. 14 | 15 | People on `v5.4+` are highly recommended to use this drivers. 16 | 17 | Support for kernel versions lower than `v4.9` were dropped for easier maintenance. For people interested in exFAT support for said kernels, please use the [old branch](https://github.com/arter97/exfat-linux/tree/old). It still works nicely and it's actively being shipped to production smartphones. 18 | 19 | exfat-linux is planned to be maintained until Android devices with `v5.7+` LTS kernel become more common. 20 | 21 | ## Disclaimer 22 | 23 | #### ● Original authorship and copyright: Samsung 24 | 25 | #### ● Maintainer of exfat-linux: Park Ju Hyung([arter97](https://twitter.com/arter97)) 26 | 27 | ## Using exfat-linux 28 | 29 | ### ● Ubuntu PPA 30 | 31 | If you're an Ubuntu user, you can simply add a [PPA repository](https://launchpad.net/~arter97/+archive/ubuntu/exfat-linux) and start using the exFAT module. 32 | 33 | Ubuntu will handle upgrades automatically as well. 34 | 35 | 1. Add the exfat-linux repository 36 | 37 | ``` 38 | sudo add-apt-repository ppa:arter97/exfat-linux 39 | sudo apt update 40 | ``` 41 | 42 | 2. Install the module 43 | 44 | `sudo apt install exfat-dkms` 45 | 46 | This will use DKMS(Dynamic Kernel Module Support) and automatically build exFAT module for your current Ubuntu installation. 47 | 48 | ### ● Manually installing the module 49 | 50 | 1. Download the code 51 | 52 | ``` 53 | git clone https://github.com/arter97/exfat-linux 54 | cd exfat-linux 55 | ``` 56 | 57 | 2. Build 58 | 59 | `make` 60 | 61 | 3. Install 62 | 63 | `sudo make install` 64 | 65 | This will install the module to your __currently running kernel__. 66 | 67 | __If you're running a `v5.4+` kernel, it is highly recommended to reboot at this point to prevent the existing staging exFAT drivers to load.__ 68 | 69 | 4. And finally load 70 | 71 | `sudo modprobe exfat` 72 | 73 | If you upgrade the kernel, you'll have to repeat this process. 74 | 75 | If you want to update **exfat-linux** to the latest version, you'll have to repeat this process. 76 | 77 | ### ● Merging the drivers to existing Linux kernel source 78 | 79 | If you're using `git`, using `git subtree` or `git submodule` is highly recommended. 80 | 81 | 1. Add this repository to `fs/exfat` 82 | 83 | 2. Modify `fs/Kconfig` 84 | 85 | ``` 86 | menu "DOS/FAT/NT Filesystems" 87 | 88 | source "fs/fat/Kconfig" 89 | +source "fs/exfat/Kconfig" 90 | source "fs/ntfs/Kconfig" 91 | endmenu 92 | ``` 93 | 94 | 3. Modify `fs/Makefile` 95 | 96 | ``` 97 | obj-$(CONFIG_FAT_FS) += fat/ 98 | +obj-$(CONFIG_EXFAT_FS) += exfat/ 99 | obj-$(CONFIG_BFS_FS) += bfs/ 100 | ``` 101 | 102 | And you're good to go! 103 | 104 | ## Benchmarks 105 | 106 | For reference, existing exFAT implementations were tested and compared on a server running Ubuntu 16.04 with Linux kernel 4.14 under a contained virtual machine. 107 | 108 | Linux 4.14 was used as higher LTS kernels don't work with [exfat-nofuse] at the time of testing. 109 | 110 | __The new base backported from mainline is not benchmarked yet.__ 111 | 112 | ### ● Ramdisk 113 | 114 | #### fio sequential I/O 115 | 116 | | Implementation | Base | Read | Write | 117 | | --------------- | ------ | ------------ | ------------ | 118 | | **exfat-linux** | 2.2.0 | 7042 MB/s | 2173 MB/s | 119 | | [exfat-nofuse] | 1.2.9 | 6849 MB/s | 1961 MB/s | 120 | | [exfat-fuse] | N/A | 3097 MB/s | 1710 MB/s | 121 | | ext4 | N/A | 7352 MB/s | 3333 MB/s | 122 | 123 | #### fio random I/O 124 | 125 | | Implementation | Base | Read | Write | 126 | | --------------- | ------ | ------------ | ------------ | 127 | | **exfat-linux** | 2.2.0 | 760 MB/s | 2222 MB/s | 128 | | [exfat-nofuse] | 1.2.9 | 760 MB/s | 2160 MB/s | 129 | | [exfat-fuse] | N/A | 1.7 MB/s | 1.6 MB/s | 130 | | ext4 | N/A | 747 MB/s | 2816 MB/s | 131 | 132 | ### ● NVMe device 133 | 134 | #### fio sequential I/O 135 | 136 | | Implementation | Base | Read | Write | 137 | | --------------- | ------ | ------------ | ------------ | 138 | | **exfat-linux** | 2.2.0 | 1283 MB/s | 1832 MB/s | 139 | | [exfat-nofuse] | 1.2.9 | 1285 MB/s | 1678 MB/s | 140 | | [exfat-fuse] | N/A | 751 MB/s | 1464 MB/s | 141 | | ext4 | N/A | 1283 MB/s | 3356 MB/s | 142 | 143 | #### fio random I/O 144 | 145 | | Implementation | Base | Read | Write | 146 | | --------------- | ------ | ------------ | ------------ | 147 | | **exfat-linux** | 2.2.0 | 26 MB/s | 1885 MB/s | 148 | | [exfat-nofuse] | 1.2.9 | 24 MB/s | 1827 MB/s | 149 | | [exfat-fuse] | N/A | 1.6 MB/s | 1.6 MB/s | 150 | | ext4 | N/A | 29 MB/s | 2821 MB/s | 151 | 152 | [exfat-fuse]: https://github.com/relan/exfat 153 | 154 | ## Mount options 155 | 156 | * uid 157 | * gid 158 | * umask 159 | * dmask 160 | * fmask 161 | * allow_utime 162 | * iocharset 163 | * quiet 164 | * time_offset 165 | 166 | * Please refer to the [vfat](https://github.com/torvalds/linux/blob/master/Documentation/filesystems/vfat.txt)'s documentation. 167 | 168 | * errors=continue 169 | 170 | * Keep going on a filesystem error. 171 | 172 | * errors=panic 173 | 174 | * Panic and halt the machine if an error occurs. 175 | 176 | * errors=remount-ro 177 | 178 | * Remount the filesystem read-only on an error. 179 | 180 | * discard 181 | 182 | * Enable the use of discard/TRIM commands to ensure flash storage doesn't run out of free blocks. This option may introduce latency penalty on file removal operations. 183 | 184 | ## Enjoy! 185 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | 10 | #include "exfat_fs.h" 11 | 12 | static const unsigned char free_bit[] = { 13 | 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/ 14 | 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/ 15 | 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/ 16 | 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/ 17 | 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~ 99*/ 18 | 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/ 19 | 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/ 20 | 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/ 21 | 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/ 22 | 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/ 23 | 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/ 24 | 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/ 25 | 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /*240 ~ 254*/ 26 | }; 27 | 28 | static const unsigned char used_bit[] = { 29 | 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/* 0 ~ 19*/ 30 | 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~ 39*/ 31 | 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~ 59*/ 32 | 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~ 79*/ 33 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/ 34 | 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/ 35 | 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/ 36 | 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/ 37 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/ 38 | 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/ 39 | 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/ 40 | 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/ 41 | 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/ 42 | }; 43 | 44 | /* 45 | * Allocation Bitmap Management Functions 46 | */ 47 | static int exfat_allocate_bitmap(struct super_block *sb, 48 | struct exfat_dentry *ep) 49 | { 50 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 51 | long long map_size; 52 | unsigned int i, need_map_size; 53 | sector_t sector; 54 | 55 | sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu); 56 | map_size = le64_to_cpu(ep->dentry.bitmap.size); 57 | need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE) 58 | + 1; 59 | if (need_map_size != map_size) { 60 | exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)", 61 | need_map_size, map_size); 62 | /* 63 | * Only allowed when bogus allocation 64 | * bitmap size is large 65 | */ 66 | if (need_map_size > map_size) 67 | return -EIO; 68 | } 69 | sbi->map_sectors = ((need_map_size - 1) >> 70 | (sb->s_blocksize_bits)) + 1; 71 | sbi->vol_amap = kmalloc_array(sbi->map_sectors, 72 | sizeof(struct buffer_head *), GFP_KERNEL); 73 | if (!sbi->vol_amap) 74 | return -ENOMEM; 75 | 76 | sector = exfat_cluster_to_sector(sbi, sbi->map_clu); 77 | for (i = 0; i < sbi->map_sectors; i++) { 78 | sbi->vol_amap[i] = sb_bread(sb, sector + i); 79 | if (!sbi->vol_amap[i]) { 80 | /* release all buffers and free vol_amap */ 81 | int j = 0; 82 | 83 | while (j < i) 84 | brelse(sbi->vol_amap[j++]); 85 | 86 | kfree(sbi->vol_amap); 87 | sbi->vol_amap = NULL; 88 | return -EIO; 89 | } 90 | } 91 | 92 | return 0; 93 | } 94 | 95 | int exfat_load_bitmap(struct super_block *sb) 96 | { 97 | unsigned int i, type; 98 | struct exfat_chain clu; 99 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 100 | 101 | exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 102 | while (clu.dir != EXFAT_EOF_CLUSTER) { 103 | for (i = 0; i < sbi->dentries_per_clu; i++) { 104 | struct exfat_dentry *ep; 105 | struct buffer_head *bh; 106 | 107 | ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 108 | if (!ep) 109 | return -EIO; 110 | 111 | type = exfat_get_entry_type(ep); 112 | if (type == TYPE_UNUSED) 113 | break; 114 | if (type != TYPE_BITMAP) 115 | continue; 116 | if (ep->dentry.bitmap.flags == 0x0) { 117 | int err; 118 | 119 | err = exfat_allocate_bitmap(sb, ep); 120 | brelse(bh); 121 | return err; 122 | } 123 | brelse(bh); 124 | } 125 | 126 | if (exfat_get_next_cluster(sb, &clu.dir)) 127 | return -EIO; 128 | } 129 | 130 | return -EINVAL; 131 | } 132 | 133 | void exfat_free_bitmap(struct exfat_sb_info *sbi) 134 | { 135 | int i; 136 | 137 | for (i = 0; i < sbi->map_sectors; i++) 138 | __brelse(sbi->vol_amap[i]); 139 | 140 | kfree(sbi->vol_amap); 141 | } 142 | 143 | /* 144 | * If the value of "clu" is 0, it means cluster 2 which is the first cluster of 145 | * the cluster heap. 146 | */ 147 | int exfat_set_bitmap(struct inode *inode, unsigned int clu) 148 | { 149 | int i, b; 150 | unsigned int ent_idx; 151 | struct super_block *sb = inode->i_sb; 152 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 153 | 154 | WARN_ON(clu < EXFAT_FIRST_CLUSTER); 155 | ent_idx = CLUSTER_TO_BITMAP_ENT(clu); 156 | i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); 157 | b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); 158 | 159 | set_bit_le(b, sbi->vol_amap[i]->b_data); 160 | exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode)); 161 | return 0; 162 | } 163 | 164 | /* 165 | * If the value of "clu" is 0, it means cluster 2 which is the first cluster of 166 | * the cluster heap. 167 | */ 168 | void exfat_clear_bitmap(struct inode *inode, unsigned int clu) 169 | { 170 | int i, b; 171 | unsigned int ent_idx; 172 | struct super_block *sb = inode->i_sb; 173 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 174 | struct exfat_mount_options *opts = &sbi->options; 175 | 176 | WARN_ON(clu < EXFAT_FIRST_CLUSTER); 177 | ent_idx = CLUSTER_TO_BITMAP_ENT(clu); 178 | i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); 179 | b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); 180 | 181 | clear_bit_le(b, sbi->vol_amap[i]->b_data); 182 | exfat_update_bh(sb, sbi->vol_amap[i], IS_DIRSYNC(inode)); 183 | 184 | if (opts->discard) { 185 | int ret_discard; 186 | 187 | ret_discard = sb_issue_discard(sb, 188 | exfat_cluster_to_sector(sbi, clu + 189 | EXFAT_RESERVED_CLUSTERS), 190 | (1 << sbi->sect_per_clus_bits), GFP_NOFS, 0); 191 | 192 | if (ret_discard == -EOPNOTSUPP) { 193 | exfat_err(sb, "discard not supported by device, disabling"); 194 | opts->discard = 0; 195 | } 196 | } 197 | } 198 | 199 | /* 200 | * If the value of "clu" is 0, it means cluster 2 which is the first cluster of 201 | * the cluster heap. 202 | */ 203 | unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu) 204 | { 205 | unsigned int i, map_i, map_b, ent_idx; 206 | unsigned int clu_base, clu_free; 207 | unsigned char k, clu_mask; 208 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 209 | 210 | WARN_ON(clu < EXFAT_FIRST_CLUSTER); 211 | ent_idx = CLUSTER_TO_BITMAP_ENT(clu); 212 | clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx & ~(BITS_PER_BYTE_MASK)); 213 | clu_mask = IGNORED_BITS_REMAINED(clu, clu_base); 214 | 215 | map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); 216 | map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx); 217 | 218 | for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; 219 | i += BITS_PER_BYTE) { 220 | k = *(sbi->vol_amap[map_i]->b_data + map_b); 221 | if (clu_mask > 0) { 222 | k |= clu_mask; 223 | clu_mask = 0; 224 | } 225 | if (k < 0xFF) { 226 | clu_free = clu_base + free_bit[k]; 227 | if (clu_free < sbi->num_clusters) 228 | return clu_free; 229 | } 230 | clu_base += BITS_PER_BYTE; 231 | 232 | if (++map_b >= sb->s_blocksize || 233 | clu_base >= sbi->num_clusters) { 234 | if (++map_i >= sbi->map_sectors) { 235 | clu_base = EXFAT_FIRST_CLUSTER; 236 | map_i = 0; 237 | } 238 | map_b = 0; 239 | } 240 | } 241 | 242 | return EXFAT_EOF_CLUSTER; 243 | } 244 | 245 | int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count) 246 | { 247 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 248 | unsigned int count = 0; 249 | unsigned int i, map_i = 0, map_b = 0; 250 | unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi); 251 | unsigned int last_mask = total_clus & BITS_PER_BYTE_MASK; 252 | unsigned char clu_bits; 253 | const unsigned char last_bit_mask[] = {0, 0b00000001, 0b00000011, 254 | 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111}; 255 | 256 | total_clus &= ~last_mask; 257 | for (i = 0; i < total_clus; i += BITS_PER_BYTE) { 258 | clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b); 259 | count += used_bit[clu_bits]; 260 | if (++map_b >= (unsigned int)sb->s_blocksize) { 261 | map_i++; 262 | map_b = 0; 263 | } 264 | } 265 | 266 | if (last_mask) { 267 | clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b); 268 | clu_bits &= last_bit_mask[last_mask]; 269 | count += used_bit[clu_bits]; 270 | } 271 | 272 | *ret_count = count; 273 | return 0; 274 | } 275 | -------------------------------------------------------------------------------- /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 14 | #include 15 | #include 16 | 17 | #include "exfat_fs.h" 18 | 19 | #define EXFAT_CACHE_VALID 0 20 | #define EXFAT_MAX_CACHE 16 21 | 22 | struct exfat_cache { 23 | struct list_head cache_list; 24 | unsigned int nr_contig; /* number of contiguous clusters */ 25 | unsigned int fcluster; /* cluster number in the file. */ 26 | unsigned int dcluster; /* cluster number on disk. */ 27 | }; 28 | 29 | struct exfat_cache_id { 30 | unsigned int id; 31 | unsigned int nr_contig; 32 | unsigned int fcluster; 33 | unsigned int dcluster; 34 | }; 35 | 36 | static struct kmem_cache *exfat_cachep; 37 | 38 | static void exfat_cache_init_once(void *c) 39 | { 40 | struct exfat_cache *cache = (struct exfat_cache *)c; 41 | 42 | INIT_LIST_HEAD(&cache->cache_list); 43 | } 44 | 45 | int exfat_cache_init(void) 46 | { 47 | exfat_cachep = kmem_cache_create("exfat_cache", 48 | sizeof(struct exfat_cache), 49 | 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, 50 | exfat_cache_init_once); 51 | if (!exfat_cachep) 52 | return -ENOMEM; 53 | return 0; 54 | } 55 | 56 | void exfat_cache_shutdown(void) 57 | { 58 | if (!exfat_cachep) 59 | return; 60 | kmem_cache_destroy(exfat_cachep); 61 | } 62 | 63 | void exfat_cache_init_inode(struct inode *inode) 64 | { 65 | struct exfat_inode_info *ei = EXFAT_I(inode); 66 | 67 | spin_lock_init(&ei->cache_lru_lock); 68 | ei->nr_caches = 0; 69 | ei->cache_valid_id = EXFAT_CACHE_VALID + 1; 70 | INIT_LIST_HEAD(&ei->cache_lru); 71 | } 72 | 73 | static inline struct exfat_cache *exfat_cache_alloc(void) 74 | { 75 | return kmem_cache_alloc(exfat_cachep, GFP_NOFS); 76 | } 77 | 78 | static inline void exfat_cache_free(struct exfat_cache *cache) 79 | { 80 | WARN_ON(!list_empty(&cache->cache_list)); 81 | kmem_cache_free(exfat_cachep, cache); 82 | } 83 | 84 | static inline void exfat_cache_update_lru(struct inode *inode, 85 | struct exfat_cache *cache) 86 | { 87 | struct exfat_inode_info *ei = EXFAT_I(inode); 88 | 89 | if (ei->cache_lru.next != &cache->cache_list) 90 | list_move(&cache->cache_list, &ei->cache_lru); 91 | } 92 | 93 | static unsigned int exfat_cache_lookup(struct inode *inode, 94 | unsigned int fclus, struct exfat_cache_id *cid, 95 | unsigned int *cached_fclus, unsigned int *cached_dclus) 96 | { 97 | struct exfat_inode_info *ei = EXFAT_I(inode); 98 | static struct exfat_cache nohit = { .fcluster = 0, }; 99 | struct exfat_cache *hit = &nohit, *p; 100 | unsigned int offset = EXFAT_EOF_CLUSTER; 101 | 102 | spin_lock(&ei->cache_lru_lock); 103 | list_for_each_entry(p, &ei->cache_lru, cache_list) { 104 | /* Find the cache of "fclus" or nearest cache. */ 105 | if (p->fcluster <= fclus && hit->fcluster < p->fcluster) { 106 | hit = p; 107 | if (hit->fcluster + hit->nr_contig < fclus) { 108 | offset = hit->nr_contig; 109 | } else { 110 | offset = fclus - hit->fcluster; 111 | break; 112 | } 113 | } 114 | } 115 | if (hit != &nohit) { 116 | exfat_cache_update_lru(inode, hit); 117 | 118 | cid->id = ei->cache_valid_id; 119 | cid->nr_contig = hit->nr_contig; 120 | cid->fcluster = hit->fcluster; 121 | cid->dcluster = hit->dcluster; 122 | *cached_fclus = cid->fcluster + offset; 123 | *cached_dclus = cid->dcluster + offset; 124 | } 125 | spin_unlock(&ei->cache_lru_lock); 126 | 127 | return offset; 128 | } 129 | 130 | static struct exfat_cache *exfat_cache_merge(struct inode *inode, 131 | struct exfat_cache_id *new) 132 | { 133 | struct exfat_inode_info *ei = EXFAT_I(inode); 134 | struct exfat_cache *p; 135 | 136 | list_for_each_entry(p, &ei->cache_lru, cache_list) { 137 | /* Find the same part as "new" in cluster-chain. */ 138 | if (p->fcluster == new->fcluster) { 139 | if (new->nr_contig > p->nr_contig) 140 | p->nr_contig = new->nr_contig; 141 | return p; 142 | } 143 | } 144 | return NULL; 145 | } 146 | 147 | static void exfat_cache_add(struct inode *inode, 148 | struct exfat_cache_id *new) 149 | { 150 | struct exfat_inode_info *ei = EXFAT_I(inode); 151 | struct exfat_cache *cache, *tmp; 152 | 153 | if (new->fcluster == EXFAT_EOF_CLUSTER) /* dummy cache */ 154 | return; 155 | 156 | spin_lock(&ei->cache_lru_lock); 157 | if (new->id != EXFAT_CACHE_VALID && 158 | new->id != ei->cache_valid_id) 159 | goto unlock; /* this cache was invalidated */ 160 | 161 | cache = exfat_cache_merge(inode, new); 162 | if (cache == NULL) { 163 | if (ei->nr_caches < EXFAT_MAX_CACHE) { 164 | ei->nr_caches++; 165 | spin_unlock(&ei->cache_lru_lock); 166 | 167 | tmp = exfat_cache_alloc(); 168 | if (!tmp) { 169 | spin_lock(&ei->cache_lru_lock); 170 | ei->nr_caches--; 171 | spin_unlock(&ei->cache_lru_lock); 172 | return; 173 | } 174 | 175 | spin_lock(&ei->cache_lru_lock); 176 | cache = exfat_cache_merge(inode, new); 177 | if (cache != NULL) { 178 | ei->nr_caches--; 179 | exfat_cache_free(tmp); 180 | goto out_update_lru; 181 | } 182 | cache = tmp; 183 | } else { 184 | struct list_head *p = ei->cache_lru.prev; 185 | 186 | cache = list_entry(p, 187 | struct exfat_cache, cache_list); 188 | } 189 | cache->fcluster = new->fcluster; 190 | cache->dcluster = new->dcluster; 191 | cache->nr_contig = new->nr_contig; 192 | } 193 | out_update_lru: 194 | exfat_cache_update_lru(inode, cache); 195 | unlock: 196 | spin_unlock(&ei->cache_lru_lock); 197 | } 198 | 199 | /* 200 | * Cache invalidation occurs rarely, thus the LRU chain is not updated. It 201 | * fixes itself after a while. 202 | */ 203 | static void __exfat_cache_inval_inode(struct inode *inode) 204 | { 205 | struct exfat_inode_info *ei = EXFAT_I(inode); 206 | struct exfat_cache *cache; 207 | 208 | while (!list_empty(&ei->cache_lru)) { 209 | cache = list_entry(ei->cache_lru.next, 210 | struct exfat_cache, cache_list); 211 | list_del_init(&cache->cache_list); 212 | ei->nr_caches--; 213 | exfat_cache_free(cache); 214 | } 215 | /* Update. The copy of caches before this id is discarded. */ 216 | ei->cache_valid_id++; 217 | if (ei->cache_valid_id == EXFAT_CACHE_VALID) 218 | ei->cache_valid_id++; 219 | } 220 | 221 | void exfat_cache_inval_inode(struct inode *inode) 222 | { 223 | struct exfat_inode_info *ei = EXFAT_I(inode); 224 | 225 | spin_lock(&ei->cache_lru_lock); 226 | __exfat_cache_inval_inode(inode); 227 | spin_unlock(&ei->cache_lru_lock); 228 | } 229 | 230 | static inline int cache_contiguous(struct exfat_cache_id *cid, 231 | unsigned int dclus) 232 | { 233 | cid->nr_contig++; 234 | return cid->dcluster + cid->nr_contig == dclus; 235 | } 236 | 237 | static inline void cache_init(struct exfat_cache_id *cid, 238 | unsigned int fclus, unsigned int dclus) 239 | { 240 | cid->id = EXFAT_CACHE_VALID; 241 | cid->fcluster = fclus; 242 | cid->dcluster = dclus; 243 | cid->nr_contig = 0; 244 | } 245 | 246 | int exfat_get_cluster(struct inode *inode, unsigned int cluster, 247 | unsigned int *fclus, unsigned int *dclus, 248 | unsigned int *last_dclus, int allow_eof) 249 | { 250 | struct super_block *sb = inode->i_sb; 251 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 252 | unsigned int limit = sbi->num_clusters; 253 | struct exfat_inode_info *ei = EXFAT_I(inode); 254 | struct exfat_cache_id cid; 255 | unsigned int content; 256 | 257 | if (ei->start_clu == EXFAT_FREE_CLUSTER) { 258 | exfat_fs_error(sb, 259 | "invalid access to exfat cache (entry 0x%08x)", 260 | ei->start_clu); 261 | return -EIO; 262 | } 263 | 264 | *fclus = 0; 265 | *dclus = ei->start_clu; 266 | *last_dclus = *dclus; 267 | 268 | /* 269 | * Don`t use exfat_cache if zero offset or non-cluster allocation 270 | */ 271 | if (cluster == 0 || *dclus == EXFAT_EOF_CLUSTER) 272 | return 0; 273 | 274 | cache_init(&cid, EXFAT_EOF_CLUSTER, EXFAT_EOF_CLUSTER); 275 | 276 | if (exfat_cache_lookup(inode, cluster, &cid, fclus, dclus) == 277 | EXFAT_EOF_CLUSTER) { 278 | /* 279 | * dummy, always not contiguous 280 | * This is reinitialized by cache_init(), later. 281 | */ 282 | WARN_ON(cid.id != EXFAT_CACHE_VALID || 283 | cid.fcluster != EXFAT_EOF_CLUSTER || 284 | cid.dcluster != EXFAT_EOF_CLUSTER || 285 | cid.nr_contig != 0); 286 | } 287 | 288 | if (*fclus == cluster) 289 | return 0; 290 | 291 | while (*fclus < cluster) { 292 | /* prevent the infinite loop of cluster chain */ 293 | if (*fclus > limit) { 294 | exfat_fs_error(sb, 295 | "detected the cluster chain loop (i_pos %u)", 296 | (*fclus)); 297 | return -EIO; 298 | } 299 | 300 | if (exfat_ent_get(sb, *dclus, &content)) 301 | return -EIO; 302 | 303 | *last_dclus = *dclus; 304 | *dclus = content; 305 | (*fclus)++; 306 | 307 | if (content == EXFAT_EOF_CLUSTER) { 308 | if (!allow_eof) { 309 | exfat_fs_error(sb, 310 | "invalid cluster chain (i_pos %u, last_clus 0x%08x is EOF)", 311 | *fclus, (*last_dclus)); 312 | return -EIO; 313 | } 314 | 315 | break; 316 | } 317 | 318 | if (!cache_contiguous(&cid, *dclus)) 319 | cache_init(&cid, *fclus, *dclus); 320 | } 321 | 322 | exfat_cache_add(inode, &cid); 323 | return 0; 324 | } 325 | -------------------------------------------------------------------------------- /compat.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_COMPAT_H 7 | #define _EXFAT_COMPAT_H 8 | 9 | #include 10 | 11 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) 12 | #error "This driver doesn't support v5.8+, " \ 13 | "please use the included driver from your kernel" 14 | #endif 15 | 16 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) 17 | #error "This driver doesn't support kernel versions lower than v4.9, " \ 18 | "please use the driver from https://github.com/arter97/exfat-linux/tree/old" 19 | #endif 20 | 21 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0) 22 | #include 23 | #else 24 | #define inode_inc_iversion(inode) (inode->i_version++) 25 | #define inode_query_iversion(inode) (inode->i_version) 26 | #define inode_eq_iversion(inode, version) (inode->i_version == version) 27 | #define inode_peek_iversion_raw(inode) (inode->i_version) 28 | #define inode_set_iversion(inode, val) (inode->i_version = val) 29 | #endif 30 | 31 | /* MS flags were renamed to SB on v4.15 */ 32 | #ifndef SB_NODIRATIME 33 | #define SB_NODIRATIME MS_NODIRATIME 34 | #endif 35 | 36 | #ifndef SB_RDONLY 37 | #define SB_RDONLY MS_RDONLY 38 | #endif 39 | 40 | #ifndef SB_SYNCHRONOUS 41 | #define SB_SYNCHRONOUS MS_SYNCHRONOUS 42 | #endif 43 | 44 | #ifndef sb_rdonly 45 | #define sb_rdonly(sb) ((sb)->s_flags & SB_RDONLY) 46 | #endif 47 | 48 | #endif /* _EXFAT_COMPAT_H */ 49 | -------------------------------------------------------------------------------- /config.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_CONFIG_H 7 | #define _EXFAT_CONFIG_H 8 | 9 | #ifndef CONFIG_EXFAT_DEFAULT_IOCHARSET /* if Kconfig lacked iocharset */ 10 | #define CONFIG_EXFAT_DEFAULT_IOCHARSET "utf8" 11 | #endif 12 | 13 | #endif /* _EXFAT_CONFIG_H */ 14 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | exfat-dkms (5.8~2arter97) UNRELEASED; urgency=medium 2 | 3 | * Applied fixes from Linux v5.8-rc4 4 | * Fixed sanity-checks on allow_utime and discard mount options 5 | * Added build-time error messages for unsupported kernel versions 6 | 7 | -- Park Ju Hyung Wed, 01 Jul 2020 02:47:26 +0900 8 | 9 | exfat-dkms (5.8~1arter97) UNRELEASED; urgency=medium 10 | 11 | * Rebased to Linux mainline's drivers 12 | * Supports for kernels lower than v4.9 dropped 13 | 14 | -- Park Ju Hyung Sat, 27 Jun 2020 08:32:23 +0900 15 | 16 | exfat-dkms (2.2.0-3arter97) UNRELEASED; urgency=medium 17 | 18 | * Added "quiet" mount option. 19 | 20 | -- Park Ju Hyung Tue, 29 Oct 2019 17:34:32 +0900 21 | 22 | exfat-dkms (2.2.0-2arter97) UNRELEASED; urgency=medium 23 | 24 | * Fix xattr support. 25 | * Merged upstream exFAT changes. 26 | * More code clean-ups. 27 | 28 | -- Park Ju Hyung Wed, 9 Oct 2019 23:16:22 +0900 29 | 30 | exfat-dkms (2.2.0-1arter97) UNRELEASED; urgency=medium 31 | 32 | * Initial release. 33 | 34 | -- Park Ju Hyung Tue, 13 Aug 2019 08:53:31 +0900 35 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: exfat-dkms 2 | Section: misc 3 | Priority: optional 4 | Maintainer: Park Ju Hyung 5 | Build-Depends: debhelper (>= 9), dkms 6 | 7 | Package: exfat-dkms 8 | Architecture: all 9 | Depends: ${misc:Depends}, dkms 10 | Conflicts: exfat-fuse 11 | Description: exFAT filesystem driver for Linux kernel 12 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Author: Park Ju Hyung 2 | 3 | License: 4 | 5 | GNU General Public License version 2, see /usr/share/common-licenses/GPL-2 6 | for more information. 7 | -------------------------------------------------------------------------------- /debian/exfat-dkms.dkms: -------------------------------------------------------------------------------- 1 | PACKAGE_NAME="exfat" 2 | PACKAGE_VERSION="#MODULE_VERSION#" 3 | BUILT_MODULE_NAME[0]="$PACKAGE_NAME" 4 | DEST_MODULE_LOCATION[0]="/extra/dkms" 5 | AUTOINSTALL="YES" 6 | -------------------------------------------------------------------------------- /debian/exfat-dkms.install: -------------------------------------------------------------------------------- 1 | usr/src 2 | -------------------------------------------------------------------------------- /debian/local/print_rule.mk: -------------------------------------------------------------------------------- 1 | writeKbuild: 2 | @printf 'obj-m += exfat.o\nexfat-objs := %s\n' "$(strip $(exfat-objs))" > $(KBUILD_PATH) 3 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | include /usr/share/dpkg/pkg-info.mk 4 | 5 | version := $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/~[^~]*$$//') 6 | targetdir := debian/tmp/usr/src/exfat-$(version) 7 | 8 | %: 9 | dh $@ --with dkms 10 | 11 | override_dh_auto_install: 12 | mkdir -p $(targetdir) 13 | install -m644 *.c *.h Kconfig LICENSE Makefile $(targetdir) 14 | # create a Kbuild file instead of patching the Makefile 15 | $(MAKE) KERNELRELEASE=1 KBUILD_PATH=$(targetdir)/Kbuild -s -f Makefile -f debian/local/print_rule.mk writeKbuild 16 | 17 | override_dh_dkms: 18 | dh_dkms -V $(version) 19 | 20 | override_dh_auto_configure: 21 | override_dh_auto_build: 22 | override_dh_auto_test: 23 | override_dh_auto_clean: 24 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | 10 | #include "exfat_fs.h" 11 | 12 | static int exfat_extract_uni_name(struct exfat_dentry *ep, 13 | unsigned short *uniname) 14 | { 15 | int i, len = 0; 16 | 17 | for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { 18 | *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]); 19 | if (*uniname == 0x0) 20 | return len; 21 | uniname++; 22 | len++; 23 | } 24 | 25 | *uniname = 0x0; 26 | return len; 27 | 28 | } 29 | 30 | static void exfat_get_uniname_from_ext_entry(struct super_block *sb, 31 | struct exfat_chain *p_dir, int entry, unsigned short *uniname) 32 | { 33 | int i; 34 | struct exfat_entry_set_cache *es; 35 | 36 | es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES); 37 | if (!es) 38 | return; 39 | 40 | /* 41 | * First entry : file entry 42 | * Second entry : stream-extension entry 43 | * Third entry : first file-name entry 44 | * So, the index of first file-name dentry should start from 2. 45 | */ 46 | for (i = 2; i < es->num_entries; i++) { 47 | struct exfat_dentry *ep = exfat_get_dentry_cached(es, i); 48 | 49 | /* end of name entry */ 50 | if (exfat_get_entry_type(ep) != TYPE_EXTEND) 51 | break; 52 | 53 | exfat_extract_uni_name(ep, uniname); 54 | uniname += EXFAT_FILE_NAME_LEN; 55 | } 56 | 57 | exfat_free_dentry_set(es, false); 58 | } 59 | 60 | /* read a directory entry from the opened directory */ 61 | static int exfat_readdir(struct inode *inode, struct exfat_dir_entry *dir_entry) 62 | { 63 | int i, dentries_per_clu, dentries_per_clu_bits = 0; 64 | unsigned int type, clu_offset; 65 | sector_t sector; 66 | struct exfat_chain dir, clu; 67 | struct exfat_uni_name uni_name; 68 | struct exfat_dentry *ep; 69 | struct super_block *sb = inode->i_sb; 70 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 71 | struct exfat_inode_info *ei = EXFAT_I(inode); 72 | unsigned int dentry = ei->rwoffset & 0xFFFFFFFF; 73 | struct buffer_head *bh; 74 | 75 | /* check if the given file ID is opened */ 76 | if (ei->type != TYPE_DIR) 77 | return -EPERM; 78 | 79 | if (ei->entry == -1) 80 | exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 81 | else 82 | exfat_chain_set(&dir, ei->start_clu, 83 | EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); 84 | 85 | dentries_per_clu = sbi->dentries_per_clu; 86 | dentries_per_clu_bits = ilog2(dentries_per_clu); 87 | 88 | clu_offset = dentry >> dentries_per_clu_bits; 89 | exfat_chain_dup(&clu, &dir); 90 | 91 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { 92 | clu.dir += clu_offset; 93 | clu.size -= clu_offset; 94 | } else { 95 | /* hint_information */ 96 | if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && 97 | ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { 98 | clu_offset -= ei->hint_bmap.off; 99 | clu.dir = ei->hint_bmap.clu; 100 | } 101 | 102 | while (clu_offset > 0) { 103 | if (exfat_get_next_cluster(sb, &(clu.dir))) 104 | return -EIO; 105 | 106 | clu_offset--; 107 | } 108 | } 109 | 110 | while (clu.dir != EXFAT_EOF_CLUSTER) { 111 | i = dentry & (dentries_per_clu - 1); 112 | 113 | for ( ; i < dentries_per_clu; i++, dentry++) { 114 | ep = exfat_get_dentry(sb, &clu, i, &bh, §or); 115 | if (!ep) 116 | return -EIO; 117 | 118 | type = exfat_get_entry_type(ep); 119 | if (type == TYPE_UNUSED) { 120 | brelse(bh); 121 | break; 122 | } 123 | 124 | if (type != TYPE_FILE && type != TYPE_DIR) { 125 | brelse(bh); 126 | continue; 127 | } 128 | 129 | dir_entry->attr = le16_to_cpu(ep->dentry.file.attr); 130 | exfat_get_entry_time(sbi, &dir_entry->crtime, 131 | ep->dentry.file.create_tz, 132 | ep->dentry.file.create_time, 133 | ep->dentry.file.create_date, 134 | ep->dentry.file.create_time_cs); 135 | exfat_get_entry_time(sbi, &dir_entry->mtime, 136 | ep->dentry.file.modify_tz, 137 | ep->dentry.file.modify_time, 138 | ep->dentry.file.modify_date, 139 | ep->dentry.file.modify_time_cs); 140 | exfat_get_entry_time(sbi, &dir_entry->atime, 141 | ep->dentry.file.access_tz, 142 | ep->dentry.file.access_time, 143 | ep->dentry.file.access_date, 144 | 0); 145 | 146 | *uni_name.name = 0x0; 147 | exfat_get_uniname_from_ext_entry(sb, &dir, dentry, 148 | uni_name.name); 149 | exfat_utf16_to_nls(sb, &uni_name, 150 | dir_entry->namebuf.lfn, 151 | dir_entry->namebuf.lfnbuf_len); 152 | brelse(bh); 153 | 154 | ep = exfat_get_dentry(sb, &clu, i + 1, &bh, NULL); 155 | if (!ep) 156 | return -EIO; 157 | dir_entry->size = 158 | le64_to_cpu(ep->dentry.stream.valid_size); 159 | brelse(bh); 160 | 161 | ei->hint_bmap.off = dentry >> dentries_per_clu_bits; 162 | ei->hint_bmap.clu = clu.dir; 163 | 164 | ei->rwoffset = ++dentry; 165 | return 0; 166 | } 167 | 168 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { 169 | if (--clu.size > 0) 170 | clu.dir++; 171 | else 172 | clu.dir = EXFAT_EOF_CLUSTER; 173 | } else { 174 | if (exfat_get_next_cluster(sb, &(clu.dir))) 175 | return -EIO; 176 | } 177 | } 178 | 179 | dir_entry->namebuf.lfn[0] = '\0'; 180 | ei->rwoffset = dentry; 181 | return 0; 182 | } 183 | 184 | static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb) 185 | { 186 | nb->lfn = NULL; 187 | nb->lfnbuf_len = 0; 188 | } 189 | 190 | static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb) 191 | { 192 | nb->lfn = __getname(); 193 | if (!nb->lfn) 194 | return -ENOMEM; 195 | nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE; 196 | return 0; 197 | } 198 | 199 | static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb) 200 | { 201 | if (!nb->lfn) 202 | return; 203 | 204 | __putname(nb->lfn); 205 | exfat_init_namebuf(nb); 206 | } 207 | 208 | /* skip iterating emit_dots when dir is empty */ 209 | #define ITER_POS_FILLED_DOTS (2) 210 | static int exfat_iterate(struct file *filp, struct dir_context *ctx) 211 | { 212 | struct inode *inode = filp->f_path.dentry->d_inode; 213 | struct super_block *sb = inode->i_sb; 214 | struct inode *tmp; 215 | struct exfat_dir_entry de; 216 | struct exfat_dentry_namebuf *nb = &(de.namebuf); 217 | struct exfat_inode_info *ei = EXFAT_I(inode); 218 | unsigned long inum; 219 | loff_t cpos, i_pos; 220 | int err = 0, fake_offset = 0; 221 | 222 | exfat_init_namebuf(nb); 223 | mutex_lock(&EXFAT_SB(sb)->s_lock); 224 | 225 | cpos = ctx->pos; 226 | if (!dir_emit_dots(filp, ctx)) 227 | goto unlock; 228 | 229 | if (ctx->pos == ITER_POS_FILLED_DOTS) { 230 | cpos = 0; 231 | fake_offset = 1; 232 | } 233 | 234 | if (cpos & (DENTRY_SIZE - 1)) { 235 | err = -ENOENT; 236 | goto unlock; 237 | } 238 | 239 | /* name buffer should be allocated before use */ 240 | err = exfat_alloc_namebuf(nb); 241 | if (err) 242 | goto unlock; 243 | get_new: 244 | ei->rwoffset = EXFAT_B_TO_DEN(cpos); 245 | 246 | if (cpos >= i_size_read(inode)) 247 | goto end_of_dir; 248 | 249 | err = exfat_readdir(inode, &de); 250 | if (err) { 251 | /* 252 | * At least we tried to read a sector. Move cpos to next sector 253 | * position (should be aligned). 254 | */ 255 | if (err == -EIO) { 256 | cpos += 1 << (sb->s_blocksize_bits); 257 | cpos &= ~(sb->s_blocksize - 1); 258 | } 259 | 260 | err = -EIO; 261 | goto end_of_dir; 262 | } 263 | 264 | cpos = EXFAT_DEN_TO_B(ei->rwoffset); 265 | 266 | if (!nb->lfn[0]) 267 | goto end_of_dir; 268 | 269 | i_pos = ((loff_t)ei->start_clu << 32) | 270 | ((ei->rwoffset - 1) & 0xffffffff); 271 | tmp = exfat_iget(sb, i_pos); 272 | if (tmp) { 273 | inum = tmp->i_ino; 274 | iput(tmp); 275 | } else { 276 | inum = iunique(sb, EXFAT_ROOT_INO); 277 | } 278 | 279 | /* 280 | * Before calling dir_emit(), sb_lock should be released. 281 | * Because page fault can occur in dir_emit() when the size 282 | * of buffer given from user is larger than one page size. 283 | */ 284 | mutex_unlock(&EXFAT_SB(sb)->s_lock); 285 | if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum, 286 | (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG)) 287 | goto out_unlocked; 288 | mutex_lock(&EXFAT_SB(sb)->s_lock); 289 | ctx->pos = cpos; 290 | goto get_new; 291 | 292 | end_of_dir: 293 | if (!cpos && fake_offset) 294 | cpos = ITER_POS_FILLED_DOTS; 295 | ctx->pos = cpos; 296 | unlock: 297 | mutex_unlock(&EXFAT_SB(sb)->s_lock); 298 | out_unlocked: 299 | /* 300 | * To improve performance, free namebuf after unlock sb_lock. 301 | * If namebuf is not allocated, this function do nothing 302 | */ 303 | exfat_free_namebuf(nb); 304 | return err; 305 | } 306 | 307 | const struct file_operations exfat_dir_operations = { 308 | .llseek = generic_file_llseek, 309 | .read = generic_read_dir, 310 | .iterate = exfat_iterate, 311 | .fsync = exfat_file_fsync, 312 | }; 313 | 314 | int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu) 315 | { 316 | int ret; 317 | 318 | exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN); 319 | 320 | ret = exfat_alloc_cluster(inode, 1, clu); 321 | if (ret) 322 | return ret; 323 | 324 | return exfat_zeroed_cluster(inode, clu->dir); 325 | } 326 | 327 | int exfat_calc_num_entries(struct exfat_uni_name *p_uniname) 328 | { 329 | int len; 330 | 331 | len = p_uniname->name_len; 332 | if (len == 0) 333 | return -EINVAL; 334 | 335 | /* 1 file entry + 1 stream entry + name entries */ 336 | return ((len - 1) / EXFAT_FILE_NAME_LEN + 3); 337 | } 338 | 339 | unsigned int exfat_get_entry_type(struct exfat_dentry *ep) 340 | { 341 | if (ep->type == EXFAT_UNUSED) 342 | return TYPE_UNUSED; 343 | if (IS_EXFAT_DELETED(ep->type)) 344 | return TYPE_DELETED; 345 | if (ep->type == EXFAT_INVAL) 346 | return TYPE_INVALID; 347 | if (IS_EXFAT_CRITICAL_PRI(ep->type)) { 348 | if (ep->type == EXFAT_BITMAP) 349 | return TYPE_BITMAP; 350 | if (ep->type == EXFAT_UPCASE) 351 | return TYPE_UPCASE; 352 | if (ep->type == EXFAT_VOLUME) 353 | return TYPE_VOLUME; 354 | if (ep->type == EXFAT_FILE) { 355 | if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR) 356 | return TYPE_DIR; 357 | return TYPE_FILE; 358 | } 359 | return TYPE_CRITICAL_PRI; 360 | } 361 | if (IS_EXFAT_BENIGN_PRI(ep->type)) { 362 | if (ep->type == EXFAT_GUID) 363 | return TYPE_GUID; 364 | if (ep->type == EXFAT_PADDING) 365 | return TYPE_PADDING; 366 | if (ep->type == EXFAT_ACLTAB) 367 | return TYPE_ACLTAB; 368 | return TYPE_BENIGN_PRI; 369 | } 370 | if (IS_EXFAT_CRITICAL_SEC(ep->type)) { 371 | if (ep->type == EXFAT_STREAM) 372 | return TYPE_STREAM; 373 | if (ep->type == EXFAT_NAME) 374 | return TYPE_EXTEND; 375 | if (ep->type == EXFAT_ACL) 376 | return TYPE_ACL; 377 | return TYPE_CRITICAL_SEC; 378 | } 379 | return TYPE_BENIGN_SEC; 380 | } 381 | 382 | static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type) 383 | { 384 | if (type == TYPE_UNUSED) { 385 | ep->type = EXFAT_UNUSED; 386 | } else if (type == TYPE_DELETED) { 387 | ep->type &= EXFAT_DELETE; 388 | } else if (type == TYPE_STREAM) { 389 | ep->type = EXFAT_STREAM; 390 | } else if (type == TYPE_EXTEND) { 391 | ep->type = EXFAT_NAME; 392 | } else if (type == TYPE_BITMAP) { 393 | ep->type = EXFAT_BITMAP; 394 | } else if (type == TYPE_UPCASE) { 395 | ep->type = EXFAT_UPCASE; 396 | } else if (type == TYPE_VOLUME) { 397 | ep->type = EXFAT_VOLUME; 398 | } else if (type == TYPE_DIR) { 399 | ep->type = EXFAT_FILE; 400 | ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR); 401 | } else if (type == TYPE_FILE) { 402 | ep->type = EXFAT_FILE; 403 | ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE); 404 | } 405 | } 406 | 407 | static void exfat_init_stream_entry(struct exfat_dentry *ep, 408 | unsigned char flags, unsigned int start_clu, 409 | unsigned long long size) 410 | { 411 | exfat_set_entry_type(ep, TYPE_STREAM); 412 | ep->dentry.stream.flags = flags; 413 | ep->dentry.stream.start_clu = cpu_to_le32(start_clu); 414 | ep->dentry.stream.valid_size = cpu_to_le64(size); 415 | ep->dentry.stream.size = cpu_to_le64(size); 416 | } 417 | 418 | static void exfat_init_name_entry(struct exfat_dentry *ep, 419 | unsigned short *uniname) 420 | { 421 | int i; 422 | 423 | exfat_set_entry_type(ep, TYPE_EXTEND); 424 | ep->dentry.name.flags = 0x0; 425 | 426 | for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) { 427 | if (*uniname != 0x0) { 428 | ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname); 429 | uniname++; 430 | } else { 431 | ep->dentry.name.unicode_0_14[i] = 0x0; 432 | } 433 | } 434 | } 435 | 436 | int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, 437 | int entry, unsigned int type, unsigned int start_clu, 438 | unsigned long long size) 439 | { 440 | struct super_block *sb = inode->i_sb; 441 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 442 | struct timespec64 ts = current_time(inode); 443 | sector_t sector; 444 | struct exfat_dentry *ep; 445 | struct buffer_head *bh; 446 | 447 | /* 448 | * We cannot use exfat_get_dentry_set here because file ep is not 449 | * initialized yet. 450 | */ 451 | ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or); 452 | if (!ep) 453 | return -EIO; 454 | 455 | exfat_set_entry_type(ep, type); 456 | exfat_set_entry_time(sbi, &ts, 457 | &ep->dentry.file.create_tz, 458 | &ep->dentry.file.create_time, 459 | &ep->dentry.file.create_date, 460 | &ep->dentry.file.create_time_cs); 461 | exfat_set_entry_time(sbi, &ts, 462 | &ep->dentry.file.modify_tz, 463 | &ep->dentry.file.modify_time, 464 | &ep->dentry.file.modify_date, 465 | &ep->dentry.file.modify_time_cs); 466 | exfat_set_entry_time(sbi, &ts, 467 | &ep->dentry.file.access_tz, 468 | &ep->dentry.file.access_time, 469 | &ep->dentry.file.access_date, 470 | NULL); 471 | 472 | exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 473 | brelse(bh); 474 | 475 | ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or); 476 | if (!ep) 477 | return -EIO; 478 | 479 | exfat_init_stream_entry(ep, 480 | (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN, 481 | start_clu, size); 482 | exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 483 | brelse(bh); 484 | 485 | return 0; 486 | } 487 | 488 | int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, 489 | int entry) 490 | { 491 | struct super_block *sb = inode->i_sb; 492 | int ret = 0; 493 | int i, num_entries; 494 | sector_t sector; 495 | u16 chksum; 496 | struct exfat_dentry *ep, *fep; 497 | struct buffer_head *fbh, *bh; 498 | 499 | fep = exfat_get_dentry(sb, p_dir, entry, &fbh, §or); 500 | if (!fep) 501 | return -EIO; 502 | 503 | num_entries = fep->dentry.file.num_ext + 1; 504 | chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY); 505 | 506 | for (i = 1; i < num_entries; i++) { 507 | ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL); 508 | if (!ep) { 509 | ret = -EIO; 510 | goto release_fbh; 511 | } 512 | chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, 513 | CS_DEFAULT); 514 | brelse(bh); 515 | } 516 | 517 | fep->dentry.file.checksum = cpu_to_le16(chksum); 518 | exfat_update_bh(sb, fbh, IS_DIRSYNC(inode)); 519 | release_fbh: 520 | brelse(fbh); 521 | return ret; 522 | } 523 | 524 | int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, 525 | int entry, int num_entries, struct exfat_uni_name *p_uniname) 526 | { 527 | struct super_block *sb = inode->i_sb; 528 | int i; 529 | sector_t sector; 530 | unsigned short *uniname = p_uniname->name; 531 | struct exfat_dentry *ep; 532 | struct buffer_head *bh; 533 | int sync = IS_DIRSYNC(inode); 534 | 535 | ep = exfat_get_dentry(sb, p_dir, entry, &bh, §or); 536 | if (!ep) 537 | return -EIO; 538 | 539 | ep->dentry.file.num_ext = (unsigned char)(num_entries - 1); 540 | exfat_update_bh(sb, bh, sync); 541 | brelse(bh); 542 | 543 | ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh, §or); 544 | if (!ep) 545 | return -EIO; 546 | 547 | ep->dentry.stream.name_len = p_uniname->name_len; 548 | ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash); 549 | exfat_update_bh(sb, bh, sync); 550 | brelse(bh); 551 | 552 | for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) { 553 | ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or); 554 | if (!ep) 555 | return -EIO; 556 | 557 | exfat_init_name_entry(ep, uniname); 558 | exfat_update_bh(sb, bh, sync); 559 | brelse(bh); 560 | uniname += EXFAT_FILE_NAME_LEN; 561 | } 562 | 563 | exfat_update_dir_chksum(inode, p_dir, entry); 564 | return 0; 565 | } 566 | 567 | int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, 568 | int entry, int order, int num_entries) 569 | { 570 | struct super_block *sb = inode->i_sb; 571 | int i; 572 | sector_t sector; 573 | struct exfat_dentry *ep; 574 | struct buffer_head *bh; 575 | 576 | for (i = order; i < num_entries; i++) { 577 | ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, §or); 578 | if (!ep) 579 | return -EIO; 580 | 581 | exfat_set_entry_type(ep, TYPE_DELETED); 582 | exfat_update_bh(sb, bh, IS_DIRSYNC(inode)); 583 | brelse(bh); 584 | } 585 | 586 | return 0; 587 | } 588 | 589 | void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es) 590 | { 591 | int chksum_type = CS_DIR_ENTRY, i; 592 | unsigned short chksum = 0; 593 | struct exfat_dentry *ep; 594 | 595 | for (i = 0; i < es->num_entries; i++) { 596 | ep = exfat_get_dentry_cached(es, i); 597 | chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum, 598 | chksum_type); 599 | chksum_type = CS_DEFAULT; 600 | } 601 | ep = exfat_get_dentry_cached(es, 0); 602 | ep->dentry.file.checksum = cpu_to_le16(chksum); 603 | es->modified = true; 604 | } 605 | 606 | void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync) 607 | { 608 | int i; 609 | 610 | for (i = 0; i < es->num_bh; i++) { 611 | if (es->modified) 612 | exfat_update_bh(es->sb, es->bh[i], sync); 613 | brelse(es->bh[i]); 614 | } 615 | kfree(es); 616 | } 617 | 618 | static int exfat_walk_fat_chain(struct super_block *sb, 619 | struct exfat_chain *p_dir, unsigned int byte_offset, 620 | unsigned int *clu) 621 | { 622 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 623 | unsigned int clu_offset; 624 | unsigned int cur_clu; 625 | 626 | clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi); 627 | cur_clu = p_dir->dir; 628 | 629 | if (p_dir->flags == ALLOC_NO_FAT_CHAIN) { 630 | cur_clu += clu_offset; 631 | } else { 632 | while (clu_offset > 0) { 633 | if (exfat_get_next_cluster(sb, &cur_clu)) 634 | return -EIO; 635 | if (cur_clu == EXFAT_EOF_CLUSTER) { 636 | exfat_fs_error(sb, 637 | "invalid dentry access beyond EOF (clu : %u, eidx : %d)", 638 | p_dir->dir, 639 | EXFAT_B_TO_DEN(byte_offset)); 640 | return -EIO; 641 | } 642 | clu_offset--; 643 | } 644 | } 645 | 646 | *clu = cur_clu; 647 | return 0; 648 | } 649 | 650 | int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, 651 | int entry, sector_t *sector, int *offset) 652 | { 653 | int ret; 654 | unsigned int off, clu = 0; 655 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 656 | 657 | off = EXFAT_DEN_TO_B(entry); 658 | 659 | ret = exfat_walk_fat_chain(sb, p_dir, off, &clu); 660 | if (ret) 661 | return ret; 662 | 663 | /* byte offset in cluster */ 664 | off = EXFAT_CLU_OFFSET(off, sbi); 665 | 666 | /* byte offset in sector */ 667 | *offset = EXFAT_BLK_OFFSET(off, sb); 668 | 669 | /* sector offset in cluster */ 670 | *sector = EXFAT_B_TO_BLK(off, sb); 671 | *sector += exfat_cluster_to_sector(sbi, clu); 672 | return 0; 673 | } 674 | 675 | #define EXFAT_MAX_RA_SIZE (128*1024) 676 | static int exfat_dir_readahead(struct super_block *sb, sector_t sec) 677 | { 678 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 679 | struct buffer_head *bh; 680 | unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits; 681 | unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits; 682 | unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count); 683 | unsigned int ra_count = min(adj_ra_count, max_ra_count); 684 | 685 | /* Read-ahead is not required */ 686 | if (sbi->sect_per_clus == 1) 687 | return 0; 688 | 689 | if (sec < sbi->data_start_sector) { 690 | exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)", 691 | (unsigned long long)sec, sbi->data_start_sector); 692 | return -EIO; 693 | } 694 | 695 | /* Not sector aligned with ra_count, resize ra_count to page size */ 696 | if ((sec - sbi->data_start_sector) & (ra_count - 1)) 697 | ra_count = page_ra_count; 698 | 699 | bh = sb_find_get_block(sb, sec); 700 | if (!bh || !buffer_uptodate(bh)) { 701 | unsigned int i; 702 | 703 | for (i = 0; i < ra_count; i++) 704 | sb_breadahead(sb, (sector_t)(sec + i)); 705 | } 706 | brelse(bh); 707 | return 0; 708 | } 709 | 710 | struct exfat_dentry *exfat_get_dentry(struct super_block *sb, 711 | struct exfat_chain *p_dir, int entry, struct buffer_head **bh, 712 | sector_t *sector) 713 | { 714 | unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE); 715 | int off; 716 | sector_t sec; 717 | 718 | if (p_dir->dir == DIR_DELETED) { 719 | exfat_err(sb, "abnormal access to deleted dentry"); 720 | return NULL; 721 | } 722 | 723 | if (exfat_find_location(sb, p_dir, entry, &sec, &off)) 724 | return NULL; 725 | 726 | if (p_dir->dir != EXFAT_FREE_CLUSTER && 727 | !(entry & (dentries_per_page - 1))) 728 | exfat_dir_readahead(sb, sec); 729 | 730 | *bh = sb_bread(sb, sec); 731 | if (!*bh) 732 | return NULL; 733 | 734 | if (sector) 735 | *sector = sec; 736 | return (struct exfat_dentry *)((*bh)->b_data + off); 737 | } 738 | 739 | enum exfat_validate_dentry_mode { 740 | ES_MODE_STARTED, 741 | ES_MODE_GET_FILE_ENTRY, 742 | ES_MODE_GET_STRM_ENTRY, 743 | ES_MODE_GET_NAME_ENTRY, 744 | ES_MODE_GET_CRITICAL_SEC_ENTRY, 745 | }; 746 | 747 | static bool exfat_validate_entry(unsigned int type, 748 | enum exfat_validate_dentry_mode *mode) 749 | { 750 | if (type == TYPE_UNUSED || type == TYPE_DELETED) 751 | return false; 752 | 753 | switch (*mode) { 754 | case ES_MODE_STARTED: 755 | if (type != TYPE_FILE && type != TYPE_DIR) 756 | return false; 757 | *mode = ES_MODE_GET_FILE_ENTRY; 758 | return true; 759 | case ES_MODE_GET_FILE_ENTRY: 760 | if (type != TYPE_STREAM) 761 | return false; 762 | *mode = ES_MODE_GET_STRM_ENTRY; 763 | return true; 764 | case ES_MODE_GET_STRM_ENTRY: 765 | if (type != TYPE_EXTEND) 766 | return false; 767 | *mode = ES_MODE_GET_NAME_ENTRY; 768 | return true; 769 | case ES_MODE_GET_NAME_ENTRY: 770 | if (type == TYPE_STREAM) 771 | return false; 772 | if (type != TYPE_EXTEND) { 773 | if (!(type & TYPE_CRITICAL_SEC)) 774 | return false; 775 | *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY; 776 | } 777 | return true; 778 | case ES_MODE_GET_CRITICAL_SEC_ENTRY: 779 | if (type == TYPE_EXTEND || type == TYPE_STREAM) 780 | return false; 781 | if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC) 782 | return false; 783 | return true; 784 | default: 785 | WARN_ON_ONCE(1); 786 | return false; 787 | } 788 | } 789 | 790 | struct exfat_dentry *exfat_get_dentry_cached( 791 | struct exfat_entry_set_cache *es, int num) 792 | { 793 | int off = es->start_off + num * DENTRY_SIZE; 794 | struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)]; 795 | char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb); 796 | 797 | return (struct exfat_dentry *)p; 798 | } 799 | 800 | /* 801 | * Returns a set of dentries for a file or dir. 802 | * 803 | * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached(). 804 | * User should call exfat_get_dentry_set() after setting 'modified' to apply 805 | * changes made in this entry set to the real device. 806 | * 807 | * in: 808 | * sb+p_dir+entry: indicates a file/dir 809 | * type: specifies how many dentries should be included. 810 | * return: 811 | * pointer of entry set on success, 812 | * NULL on failure. 813 | */ 814 | struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, 815 | struct exfat_chain *p_dir, int entry, unsigned int type) 816 | { 817 | int ret, i, num_bh; 818 | unsigned int off, byte_offset, clu = 0; 819 | sector_t sec; 820 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 821 | struct exfat_entry_set_cache *es; 822 | struct exfat_dentry *ep; 823 | int num_entries; 824 | enum exfat_validate_dentry_mode mode = ES_MODE_STARTED; 825 | struct buffer_head *bh; 826 | 827 | if (p_dir->dir == DIR_DELETED) { 828 | exfat_err(sb, "access to deleted dentry"); 829 | return NULL; 830 | } 831 | 832 | byte_offset = EXFAT_DEN_TO_B(entry); 833 | ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu); 834 | if (ret) 835 | return NULL; 836 | 837 | es = kzalloc(sizeof(*es), GFP_KERNEL); 838 | if (!es) 839 | return NULL; 840 | es->sb = sb; 841 | es->modified = false; 842 | 843 | /* byte offset in cluster */ 844 | byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi); 845 | 846 | /* byte offset in sector */ 847 | off = EXFAT_BLK_OFFSET(byte_offset, sb); 848 | es->start_off = off; 849 | 850 | /* sector offset in cluster */ 851 | sec = EXFAT_B_TO_BLK(byte_offset, sb); 852 | sec += exfat_cluster_to_sector(sbi, clu); 853 | 854 | bh = sb_bread(sb, sec); 855 | if (!bh) 856 | goto free_es; 857 | es->bh[es->num_bh++] = bh; 858 | 859 | ep = exfat_get_dentry_cached(es, 0); 860 | if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) 861 | goto free_es; 862 | 863 | num_entries = type == ES_ALL_ENTRIES ? 864 | ep->dentry.file.num_ext + 1 : type; 865 | es->num_entries = num_entries; 866 | 867 | num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb); 868 | for (i = 1; i < num_bh; i++) { 869 | /* get the next sector */ 870 | if (exfat_is_last_sector_in_cluster(sbi, sec)) { 871 | if (p_dir->flags == ALLOC_NO_FAT_CHAIN) 872 | clu++; 873 | else if (exfat_get_next_cluster(sb, &clu)) 874 | goto free_es; 875 | sec = exfat_cluster_to_sector(sbi, clu); 876 | } else { 877 | sec++; 878 | } 879 | 880 | bh = sb_bread(sb, sec); 881 | if (!bh) 882 | goto free_es; 883 | es->bh[es->num_bh++] = bh; 884 | } 885 | 886 | /* validiate cached dentries */ 887 | for (i = 1; i < num_entries; i++) { 888 | ep = exfat_get_dentry_cached(es, i); 889 | if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode)) 890 | goto free_es; 891 | } 892 | return es; 893 | 894 | free_es: 895 | exfat_free_dentry_set(es, false); 896 | return NULL; 897 | } 898 | 899 | enum { 900 | DIRENT_STEP_FILE, 901 | DIRENT_STEP_STRM, 902 | DIRENT_STEP_NAME, 903 | DIRENT_STEP_SECD, 904 | }; 905 | 906 | /* 907 | * return values: 908 | * >= 0 : return dir entiry position with the name in dir 909 | * -EEXIST : (root dir, ".") it is the root dir itself 910 | * -ENOENT : entry with the name does not exist 911 | * -EIO : I/O error 912 | */ 913 | int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, 914 | struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 915 | int num_entries, unsigned int type) 916 | { 917 | int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len; 918 | int order, step, name_len = 0; 919 | int dentries_per_clu, num_empty = 0; 920 | unsigned int entry_type; 921 | unsigned short *uniname = NULL; 922 | struct exfat_chain clu; 923 | struct exfat_hint *hint_stat = &ei->hint_stat; 924 | struct exfat_hint_femp candi_empty; 925 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 926 | 927 | dentries_per_clu = sbi->dentries_per_clu; 928 | 929 | exfat_chain_dup(&clu, p_dir); 930 | 931 | if (hint_stat->eidx) { 932 | clu.dir = hint_stat->clu; 933 | dentry = hint_stat->eidx; 934 | end_eidx = dentry; 935 | } 936 | 937 | candi_empty.eidx = EXFAT_HINT_NONE; 938 | rewind: 939 | order = 0; 940 | step = DIRENT_STEP_FILE; 941 | while (clu.dir != EXFAT_EOF_CLUSTER) { 942 | i = dentry & (dentries_per_clu - 1); 943 | for (; i < dentries_per_clu; i++, dentry++) { 944 | struct exfat_dentry *ep; 945 | struct buffer_head *bh; 946 | 947 | if (rewind && dentry == end_eidx) 948 | goto not_found; 949 | 950 | ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 951 | if (!ep) 952 | return -EIO; 953 | 954 | entry_type = exfat_get_entry_type(ep); 955 | 956 | if (entry_type == TYPE_UNUSED || 957 | entry_type == TYPE_DELETED) { 958 | step = DIRENT_STEP_FILE; 959 | 960 | num_empty++; 961 | if (candi_empty.eidx == EXFAT_HINT_NONE && 962 | num_empty == 1) { 963 | exfat_chain_set(&candi_empty.cur, 964 | clu.dir, clu.size, clu.flags); 965 | } 966 | 967 | if (candi_empty.eidx == EXFAT_HINT_NONE && 968 | num_empty >= num_entries) { 969 | candi_empty.eidx = 970 | dentry - (num_empty - 1); 971 | WARN_ON(candi_empty.eidx < 0); 972 | candi_empty.count = num_empty; 973 | 974 | if (ei->hint_femp.eidx == 975 | EXFAT_HINT_NONE || 976 | candi_empty.eidx <= 977 | ei->hint_femp.eidx) { 978 | memcpy(&ei->hint_femp, 979 | &candi_empty, 980 | sizeof(candi_empty)); 981 | } 982 | } 983 | 984 | brelse(bh); 985 | if (entry_type == TYPE_UNUSED) 986 | goto not_found; 987 | continue; 988 | } 989 | 990 | num_empty = 0; 991 | candi_empty.eidx = EXFAT_HINT_NONE; 992 | 993 | if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) { 994 | step = DIRENT_STEP_FILE; 995 | if (type == TYPE_ALL || type == entry_type) { 996 | num_ext = ep->dentry.file.num_ext; 997 | step = DIRENT_STEP_STRM; 998 | } 999 | brelse(bh); 1000 | continue; 1001 | } 1002 | 1003 | if (entry_type == TYPE_STREAM) { 1004 | u16 name_hash; 1005 | 1006 | if (step != DIRENT_STEP_STRM) { 1007 | step = DIRENT_STEP_FILE; 1008 | brelse(bh); 1009 | continue; 1010 | } 1011 | step = DIRENT_STEP_FILE; 1012 | name_hash = le16_to_cpu( 1013 | ep->dentry.stream.name_hash); 1014 | if (p_uniname->name_hash == name_hash && 1015 | p_uniname->name_len == 1016 | ep->dentry.stream.name_len) { 1017 | step = DIRENT_STEP_NAME; 1018 | order = 1; 1019 | name_len = 0; 1020 | } 1021 | brelse(bh); 1022 | continue; 1023 | } 1024 | 1025 | brelse(bh); 1026 | if (entry_type == TYPE_EXTEND) { 1027 | unsigned short entry_uniname[16], unichar; 1028 | 1029 | if (step != DIRENT_STEP_NAME) { 1030 | step = DIRENT_STEP_FILE; 1031 | continue; 1032 | } 1033 | 1034 | if (++order == 2) 1035 | uniname = p_uniname->name; 1036 | else 1037 | uniname += EXFAT_FILE_NAME_LEN; 1038 | 1039 | len = exfat_extract_uni_name(ep, entry_uniname); 1040 | name_len += len; 1041 | 1042 | unichar = *(uniname+len); 1043 | *(uniname+len) = 0x0; 1044 | 1045 | if (exfat_uniname_ncmp(sb, uniname, 1046 | entry_uniname, len)) { 1047 | step = DIRENT_STEP_FILE; 1048 | } else if (p_uniname->name_len == name_len) { 1049 | if (order == num_ext) 1050 | goto found; 1051 | step = DIRENT_STEP_SECD; 1052 | } 1053 | 1054 | *(uniname+len) = unichar; 1055 | continue; 1056 | } 1057 | 1058 | if (entry_type & 1059 | (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) { 1060 | if (step == DIRENT_STEP_SECD) { 1061 | if (++order == num_ext) 1062 | goto found; 1063 | continue; 1064 | } 1065 | } 1066 | step = DIRENT_STEP_FILE; 1067 | } 1068 | 1069 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1070 | if (--clu.size > 0) 1071 | clu.dir++; 1072 | else 1073 | clu.dir = EXFAT_EOF_CLUSTER; 1074 | } else { 1075 | if (exfat_get_next_cluster(sb, &clu.dir)) 1076 | return -EIO; 1077 | } 1078 | } 1079 | 1080 | not_found: 1081 | /* 1082 | * We started at not 0 index,so we should try to find target 1083 | * from 0 index to the index we started at. 1084 | */ 1085 | if (!rewind && end_eidx) { 1086 | rewind = 1; 1087 | dentry = 0; 1088 | clu.dir = p_dir->dir; 1089 | /* reset empty hint */ 1090 | num_empty = 0; 1091 | candi_empty.eidx = EXFAT_HINT_NONE; 1092 | goto rewind; 1093 | } 1094 | 1095 | /* initialized hint_stat */ 1096 | hint_stat->clu = p_dir->dir; 1097 | hint_stat->eidx = 0; 1098 | return -ENOENT; 1099 | 1100 | found: 1101 | /* next dentry we'll find is out of this cluster */ 1102 | if (!((dentry + 1) & (dentries_per_clu - 1))) { 1103 | int ret = 0; 1104 | 1105 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1106 | if (--clu.size > 0) 1107 | clu.dir++; 1108 | else 1109 | clu.dir = EXFAT_EOF_CLUSTER; 1110 | } else { 1111 | ret = exfat_get_next_cluster(sb, &clu.dir); 1112 | } 1113 | 1114 | if (ret || clu.dir != EXFAT_EOF_CLUSTER) { 1115 | /* just initialized hint_stat */ 1116 | hint_stat->clu = p_dir->dir; 1117 | hint_stat->eidx = 0; 1118 | return (dentry - num_ext); 1119 | } 1120 | } 1121 | 1122 | hint_stat->clu = clu.dir; 1123 | hint_stat->eidx = dentry + 1; 1124 | return dentry - num_ext; 1125 | } 1126 | 1127 | int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir, 1128 | int entry, struct exfat_dentry *ep) 1129 | { 1130 | int i, count = 0; 1131 | unsigned int type; 1132 | struct exfat_dentry *ext_ep; 1133 | struct buffer_head *bh; 1134 | 1135 | for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) { 1136 | ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh, NULL); 1137 | if (!ext_ep) 1138 | return -EIO; 1139 | 1140 | type = exfat_get_entry_type(ext_ep); 1141 | brelse(bh); 1142 | if (type == TYPE_EXTEND || type == TYPE_STREAM) 1143 | count++; 1144 | else 1145 | break; 1146 | } 1147 | return count; 1148 | } 1149 | 1150 | int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir) 1151 | { 1152 | int i, count = 0; 1153 | int dentries_per_clu; 1154 | unsigned int entry_type; 1155 | struct exfat_chain clu; 1156 | struct exfat_dentry *ep; 1157 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 1158 | struct buffer_head *bh; 1159 | 1160 | dentries_per_clu = sbi->dentries_per_clu; 1161 | 1162 | exfat_chain_dup(&clu, p_dir); 1163 | 1164 | while (clu.dir != EXFAT_EOF_CLUSTER) { 1165 | for (i = 0; i < dentries_per_clu; i++) { 1166 | ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 1167 | if (!ep) 1168 | return -EIO; 1169 | entry_type = exfat_get_entry_type(ep); 1170 | brelse(bh); 1171 | 1172 | if (entry_type == TYPE_UNUSED) 1173 | return count; 1174 | if (entry_type != TYPE_DIR) 1175 | continue; 1176 | count++; 1177 | } 1178 | 1179 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { 1180 | if (--clu.size > 0) 1181 | clu.dir++; 1182 | else 1183 | clu.dir = EXFAT_EOF_CLUSTER; 1184 | } else { 1185 | if (exfat_get_next_cluster(sb, &(clu.dir))) 1186 | return -EIO; 1187 | } 1188 | } 1189 | 1190 | return count; 1191 | } 1192 | -------------------------------------------------------------------------------- /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 10 | #include 11 | #include 12 | 13 | #include "config.h" 14 | #include "compat.h" 15 | #include "version.h" 16 | #include "exfat_raw.h" 17 | 18 | #define EXFAT_SUPER_MAGIC 0x2011BAB0UL 19 | #define EXFAT_ROOT_INO 1 20 | 21 | #define EXFAT_SB_DIRTY 0 22 | 23 | #define EXFAT_CLUSTERS_UNTRACKED (~0u) 24 | 25 | /* 26 | * exfat error flags 27 | */ 28 | enum exfat_error_mode { 29 | EXFAT_ERRORS_CONT, /* ignore error and continue */ 30 | EXFAT_ERRORS_PANIC, /* panic on error */ 31 | EXFAT_ERRORS_RO, /* remount r/o on error */ 32 | }; 33 | 34 | /* 35 | * exfat nls lossy flag 36 | */ 37 | enum { 38 | NLS_NAME_NO_LOSSY, /* no lossy */ 39 | NLS_NAME_LOSSY, /* just detected incorrect filename(s) */ 40 | NLS_NAME_OVERLEN, /* the length is over than its limit */ 41 | }; 42 | 43 | #define EXFAT_HASH_BITS 8 44 | #define EXFAT_HASH_SIZE (1UL << EXFAT_HASH_BITS) 45 | 46 | /* 47 | * Type Definitions 48 | */ 49 | #define ES_2_ENTRIES 2 50 | #define ES_ALL_ENTRIES 0 51 | 52 | #define DIR_DELETED 0xFFFF0321 53 | 54 | /* type values */ 55 | #define TYPE_UNUSED 0x0000 56 | #define TYPE_DELETED 0x0001 57 | #define TYPE_INVALID 0x0002 58 | #define TYPE_CRITICAL_PRI 0x0100 59 | #define TYPE_BITMAP 0x0101 60 | #define TYPE_UPCASE 0x0102 61 | #define TYPE_VOLUME 0x0103 62 | #define TYPE_DIR 0x0104 63 | #define TYPE_FILE 0x011F 64 | #define TYPE_CRITICAL_SEC 0x0200 65 | #define TYPE_STREAM 0x0201 66 | #define TYPE_EXTEND 0x0202 67 | #define TYPE_ACL 0x0203 68 | #define TYPE_BENIGN_PRI 0x0400 69 | #define TYPE_GUID 0x0401 70 | #define TYPE_PADDING 0x0402 71 | #define TYPE_ACLTAB 0x0403 72 | #define TYPE_BENIGN_SEC 0x0800 73 | #define TYPE_ALL 0x0FFF 74 | 75 | #define MAX_CHARSET_SIZE 6 /* max size of multi-byte character */ 76 | #define MAX_NAME_LENGTH 255 /* max len of file name excluding NULL */ 77 | #define MAX_VFSNAME_BUF_SIZE ((MAX_NAME_LENGTH + 1) * MAX_CHARSET_SIZE) 78 | 79 | /* Enough size to hold 256 dentry (even 512 Byte sector) */ 80 | #define DIR_CACHE_SIZE (256*sizeof(struct exfat_dentry)/512+1) 81 | 82 | #define EXFAT_HINT_NONE -1 83 | #define EXFAT_MIN_SUBDIR 2 84 | 85 | /* 86 | * helpers for cluster size to byte conversion. 87 | */ 88 | #define EXFAT_CLU_TO_B(b, sbi) ((b) << (sbi)->cluster_size_bits) 89 | #define EXFAT_B_TO_CLU(b, sbi) ((b) >> (sbi)->cluster_size_bits) 90 | #define EXFAT_B_TO_CLU_ROUND_UP(b, sbi) \ 91 | (((b - 1) >> (sbi)->cluster_size_bits) + 1) 92 | #define EXFAT_CLU_OFFSET(off, sbi) ((off) & ((sbi)->cluster_size - 1)) 93 | 94 | /* 95 | * helpers for block size to byte conversion. 96 | */ 97 | #define EXFAT_BLK_TO_B(b, sb) ((b) << (sb)->s_blocksize_bits) 98 | #define EXFAT_B_TO_BLK(b, sb) ((b) >> (sb)->s_blocksize_bits) 99 | #define EXFAT_B_TO_BLK_ROUND_UP(b, sb) \ 100 | (((b - 1) >> (sb)->s_blocksize_bits) + 1) 101 | #define EXFAT_BLK_OFFSET(off, sb) ((off) & ((sb)->s_blocksize - 1)) 102 | 103 | /* 104 | * helpers for block size to dentry size conversion. 105 | */ 106 | #define EXFAT_B_TO_DEN_IDX(b, sbi) \ 107 | ((b) << ((sbi)->cluster_size_bits - DENTRY_SIZE_BITS)) 108 | #define EXFAT_B_TO_DEN(b) ((b) >> DENTRY_SIZE_BITS) 109 | #define EXFAT_DEN_TO_B(b) ((b) << DENTRY_SIZE_BITS) 110 | 111 | /* 112 | * helpers for fat entry. 113 | */ 114 | #define FAT_ENT_SIZE (4) 115 | #define FAT_ENT_SIZE_BITS (2) 116 | #define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \ 117 | (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits)) 118 | #define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \ 119 | ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1)) 120 | 121 | /* 122 | * helpers for bitmap. 123 | */ 124 | #define CLUSTER_TO_BITMAP_ENT(clu) ((clu) - EXFAT_RESERVED_CLUSTERS) 125 | #define BITMAP_ENT_TO_CLUSTER(ent) ((ent) + EXFAT_RESERVED_CLUSTERS) 126 | #define BITS_PER_SECTOR(sb) ((sb)->s_blocksize * BITS_PER_BYTE) 127 | #define BITS_PER_SECTOR_MASK(sb) (BITS_PER_SECTOR(sb) - 1) 128 | #define BITMAP_OFFSET_SECTOR_INDEX(sb, ent) \ 129 | ((ent / BITS_PER_BYTE) >> (sb)->s_blocksize_bits) 130 | #define BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent) (ent & BITS_PER_SECTOR_MASK(sb)) 131 | #define BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent) \ 132 | ((ent / BITS_PER_BYTE) & ((sb)->s_blocksize - 1)) 133 | #define BITS_PER_BYTE_MASK 0x7 134 | #define IGNORED_BITS_REMAINED(clu, clu_base) ((1 << ((clu) - (clu_base))) - 1) 135 | 136 | struct exfat_dentry_namebuf { 137 | char *lfn; 138 | int lfnbuf_len; /* usally MAX_UNINAME_BUF_SIZE */ 139 | }; 140 | 141 | /* unicode name structure */ 142 | struct exfat_uni_name { 143 | /* +3 for null and for converting */ 144 | unsigned short name[MAX_NAME_LENGTH + 3]; 145 | u16 name_hash; 146 | unsigned char name_len; 147 | }; 148 | 149 | /* directory structure */ 150 | struct exfat_chain { 151 | unsigned int dir; 152 | unsigned int size; 153 | unsigned char flags; 154 | }; 155 | 156 | /* first empty entry hint information */ 157 | struct exfat_hint_femp { 158 | /* entry index of a directory */ 159 | int eidx; 160 | /* count of continuous empty entry */ 161 | int count; 162 | /* the cluster that first empty slot exists in */ 163 | struct exfat_chain cur; 164 | }; 165 | 166 | /* hint structure */ 167 | struct exfat_hint { 168 | unsigned int clu; 169 | union { 170 | unsigned int off; /* cluster offset */ 171 | int eidx; /* entry index */ 172 | }; 173 | }; 174 | 175 | struct exfat_entry_set_cache { 176 | struct super_block *sb; 177 | bool modified; 178 | unsigned int start_off; 179 | int num_bh; 180 | struct buffer_head *bh[DIR_CACHE_SIZE]; 181 | unsigned int num_entries; 182 | }; 183 | 184 | struct exfat_dir_entry { 185 | struct exfat_chain dir; 186 | int entry; 187 | unsigned int type; 188 | unsigned int start_clu; 189 | unsigned char flags; 190 | unsigned short attr; 191 | loff_t size; 192 | unsigned int num_subdirs; 193 | struct timespec64 atime; 194 | struct timespec64 mtime; 195 | struct timespec64 crtime; 196 | struct exfat_dentry_namebuf namebuf; 197 | }; 198 | 199 | /* 200 | * exfat mount in-memory data 201 | */ 202 | struct exfat_mount_options { 203 | kuid_t fs_uid; 204 | kgid_t fs_gid; 205 | unsigned short fs_fmask; 206 | unsigned short fs_dmask; 207 | /* permission for setting the [am]time */ 208 | unsigned short allow_utime; 209 | /* charset for filename input/display */ 210 | char *iocharset; 211 | /* fake return success on setattr(e.g. chmods/chowns) */ 212 | unsigned char quiet; 213 | /* on error: continue, panic, remount-ro */ 214 | enum exfat_error_mode errors; 215 | unsigned utf8:1, /* Use of UTF-8 character set */ 216 | discard:1; /* Issue discard requests on deletions */ 217 | int time_offset; /* Offset of timestamps from UTC (in minutes) */ 218 | }; 219 | 220 | /* 221 | * EXFAT file system superblock in-memory data 222 | */ 223 | struct exfat_sb_info { 224 | unsigned long long num_sectors; /* num of sectors in volume */ 225 | unsigned int num_clusters; /* num of clusters in volume */ 226 | unsigned int cluster_size; /* cluster size in bytes */ 227 | unsigned int cluster_size_bits; 228 | unsigned int sect_per_clus; /* cluster size in sectors */ 229 | unsigned int sect_per_clus_bits; 230 | unsigned long long FAT1_start_sector; /* FAT1 start sector */ 231 | unsigned long long FAT2_start_sector; /* FAT2 start sector */ 232 | unsigned long long data_start_sector; /* data area start sector */ 233 | unsigned int num_FAT_sectors; /* num of FAT sectors */ 234 | unsigned int root_dir; /* root dir cluster */ 235 | unsigned int dentries_per_clu; /* num of dentries per cluster */ 236 | unsigned int vol_flag; /* volume dirty flag */ 237 | struct buffer_head *boot_bh; /* buffer_head of BOOT sector */ 238 | 239 | unsigned int map_clu; /* allocation bitmap start cluster */ 240 | unsigned int map_sectors; /* num of allocation bitmap sectors */ 241 | struct buffer_head **vol_amap; /* allocation bitmap */ 242 | 243 | unsigned short *vol_utbl; /* upcase table */ 244 | 245 | unsigned int clu_srch_ptr; /* cluster search pointer */ 246 | unsigned int used_clusters; /* number of used clusters */ 247 | 248 | unsigned long s_state; 249 | struct mutex s_lock; /* superblock lock */ 250 | struct exfat_mount_options options; 251 | struct nls_table *nls_io; /* Charset used for input and display */ 252 | struct ratelimit_state ratelimit; 253 | 254 | spinlock_t inode_hash_lock; 255 | struct hlist_head inode_hashtable[EXFAT_HASH_SIZE]; 256 | 257 | struct rcu_head rcu; 258 | }; 259 | 260 | /* 261 | * EXFAT file system inode in-memory data 262 | */ 263 | struct exfat_inode_info { 264 | struct exfat_chain dir; 265 | int entry; 266 | unsigned int type; 267 | unsigned short attr; 268 | unsigned int start_clu; 269 | unsigned char flags; 270 | /* 271 | * the copy of low 32bit of i_version to check 272 | * the validation of hint_stat. 273 | */ 274 | unsigned int version; 275 | /* file offset or dentry index for readdir */ 276 | loff_t rwoffset; 277 | 278 | /* hint for cluster last accessed */ 279 | struct exfat_hint hint_bmap; 280 | /* hint for entry index we try to lookup next time */ 281 | struct exfat_hint hint_stat; 282 | /* hint for first empty entry */ 283 | struct exfat_hint_femp hint_femp; 284 | 285 | spinlock_t cache_lru_lock; 286 | struct list_head cache_lru; 287 | int nr_caches; 288 | /* for avoiding the race between alloc and free */ 289 | unsigned int cache_valid_id; 290 | 291 | /* 292 | * NOTE: i_size_ondisk is 64bits, so must hold ->inode_lock to access. 293 | * physically allocated size. 294 | */ 295 | loff_t i_size_ondisk; 296 | /* block-aligned i_size (used in cont_write_begin) */ 297 | loff_t i_size_aligned; 298 | /* on-disk position of directory entry or 0 */ 299 | loff_t i_pos; 300 | /* hash by i_location */ 301 | struct hlist_node i_hash_fat; 302 | /* protect bmap against truncate */ 303 | struct rw_semaphore truncate_lock; 304 | struct inode vfs_inode; 305 | /* File creation time */ 306 | struct timespec64 i_crtime; 307 | }; 308 | 309 | static inline struct exfat_sb_info *EXFAT_SB(struct super_block *sb) 310 | { 311 | return sb->s_fs_info; 312 | } 313 | 314 | static inline struct exfat_inode_info *EXFAT_I(struct inode *inode) 315 | { 316 | return container_of(inode, struct exfat_inode_info, vfs_inode); 317 | } 318 | 319 | /* 320 | * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to 321 | * save ATTR_RO instead of ->i_mode. 322 | * 323 | * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only 324 | * bit, it's just used as flag for app. 325 | */ 326 | static inline int exfat_mode_can_hold_ro(struct inode *inode) 327 | { 328 | struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 329 | 330 | if (S_ISDIR(inode->i_mode)) 331 | return 0; 332 | 333 | if ((~sbi->options.fs_fmask) & 0222) 334 | return 1; 335 | return 0; 336 | } 337 | 338 | /* Convert attribute bits and a mask to the UNIX mode. */ 339 | static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi, 340 | unsigned short attr, mode_t mode) 341 | { 342 | if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR)) 343 | mode &= ~0222; 344 | 345 | if (attr & ATTR_SUBDIR) 346 | return (mode & ~sbi->options.fs_dmask) | S_IFDIR; 347 | 348 | return (mode & ~sbi->options.fs_fmask) | S_IFREG; 349 | } 350 | 351 | /* Return the FAT attribute byte for this inode */ 352 | static inline unsigned short exfat_make_attr(struct inode *inode) 353 | { 354 | unsigned short attr = EXFAT_I(inode)->attr; 355 | 356 | if (S_ISDIR(inode->i_mode)) 357 | attr |= ATTR_SUBDIR; 358 | if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222)) 359 | attr |= ATTR_READONLY; 360 | return attr; 361 | } 362 | 363 | static inline void exfat_save_attr(struct inode *inode, unsigned short attr) 364 | { 365 | if (exfat_mode_can_hold_ro(inode)) 366 | EXFAT_I(inode)->attr = attr & (ATTR_RWMASK | ATTR_READONLY); 367 | else 368 | EXFAT_I(inode)->attr = attr & ATTR_RWMASK; 369 | } 370 | 371 | static inline bool exfat_is_last_sector_in_cluster(struct exfat_sb_info *sbi, 372 | sector_t sec) 373 | { 374 | return ((sec - sbi->data_start_sector + 1) & 375 | ((1 << sbi->sect_per_clus_bits) - 1)) == 0; 376 | } 377 | 378 | static inline sector_t exfat_cluster_to_sector(struct exfat_sb_info *sbi, 379 | unsigned int clus) 380 | { 381 | return ((clus - EXFAT_RESERVED_CLUSTERS) << sbi->sect_per_clus_bits) + 382 | sbi->data_start_sector; 383 | } 384 | 385 | static inline int exfat_sector_to_cluster(struct exfat_sb_info *sbi, 386 | sector_t sec) 387 | { 388 | return ((sec - sbi->data_start_sector) >> sbi->sect_per_clus_bits) + 389 | EXFAT_RESERVED_CLUSTERS; 390 | } 391 | 392 | /* super.c */ 393 | int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag); 394 | 395 | /* fatent.c */ 396 | #define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu) 397 | 398 | int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, 399 | struct exfat_chain *p_chain); 400 | int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain); 401 | int exfat_ent_get(struct super_block *sb, unsigned int loc, 402 | unsigned int *content); 403 | int exfat_ent_set(struct super_block *sb, unsigned int loc, 404 | unsigned int content); 405 | int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir, 406 | int entry, struct exfat_dentry *p_entry); 407 | int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain, 408 | unsigned int len); 409 | int exfat_zeroed_cluster(struct inode *dir, unsigned int clu); 410 | int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, 411 | unsigned int *ret_clu); 412 | int exfat_count_num_clusters(struct super_block *sb, 413 | struct exfat_chain *p_chain, unsigned int *ret_count); 414 | 415 | /* balloc.c */ 416 | int exfat_load_bitmap(struct super_block *sb); 417 | void exfat_free_bitmap(struct exfat_sb_info *sbi); 418 | int exfat_set_bitmap(struct inode *inode, unsigned int clu); 419 | void exfat_clear_bitmap(struct inode *inode, unsigned int clu); 420 | unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu); 421 | int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count); 422 | 423 | /* file.c */ 424 | extern const struct file_operations exfat_file_operations; 425 | int __exfat_truncate(struct inode *inode, loff_t new_size); 426 | void exfat_truncate(struct inode *inode, loff_t size); 427 | int exfat_setattr(struct dentry *dentry, struct iattr *attr); 428 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) 429 | int exfat_getattr(const struct path *path, struct kstat *stat, 430 | unsigned int request_mask, unsigned int query_flags); 431 | #else 432 | int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat); 433 | #endif 434 | int exfat_file_fsync(struct file *file, loff_t start, loff_t end, int datasync); 435 | 436 | /* namei.c */ 437 | extern const struct dentry_operations exfat_dentry_ops; 438 | extern const struct dentry_operations exfat_utf8_dentry_ops; 439 | 440 | /* cache.c */ 441 | int exfat_cache_init(void); 442 | void exfat_cache_shutdown(void); 443 | void exfat_cache_init_inode(struct inode *inode); 444 | void exfat_cache_inval_inode(struct inode *inode); 445 | int exfat_get_cluster(struct inode *inode, unsigned int cluster, 446 | unsigned int *fclus, unsigned int *dclus, 447 | unsigned int *last_dclus, int allow_eof); 448 | 449 | /* dir.c */ 450 | extern const struct inode_operations exfat_dir_inode_operations; 451 | extern const struct file_operations exfat_dir_operations; 452 | unsigned int exfat_get_entry_type(struct exfat_dentry *p_entry); 453 | int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir, 454 | int entry, unsigned int type, unsigned int start_clu, 455 | unsigned long long size); 456 | int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir, 457 | int entry, int num_entries, struct exfat_uni_name *p_uniname); 458 | int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir, 459 | int entry, int order, int num_entries); 460 | int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir, 461 | int entry); 462 | void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es); 463 | int exfat_calc_num_entries(struct exfat_uni_name *p_uniname); 464 | int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei, 465 | struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 466 | int num_entries, unsigned int type); 467 | int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu); 468 | int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir, 469 | int entry, sector_t *sector, int *offset); 470 | struct exfat_dentry *exfat_get_dentry(struct super_block *sb, 471 | struct exfat_chain *p_dir, int entry, struct buffer_head **bh, 472 | sector_t *sector); 473 | struct exfat_dentry *exfat_get_dentry_cached(struct exfat_entry_set_cache *es, 474 | int num); 475 | struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb, 476 | struct exfat_chain *p_dir, int entry, unsigned int type); 477 | void exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync); 478 | int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir); 479 | 480 | /* inode.c */ 481 | extern const struct inode_operations exfat_file_inode_operations; 482 | void exfat_sync_inode(struct inode *inode); 483 | struct inode *exfat_build_inode(struct super_block *sb, 484 | struct exfat_dir_entry *info, loff_t i_pos); 485 | void exfat_hash_inode(struct inode *inode, loff_t i_pos); 486 | void exfat_unhash_inode(struct inode *inode); 487 | struct inode *exfat_iget(struct super_block *sb, loff_t i_pos); 488 | int exfat_write_inode(struct inode *inode, struct writeback_control *wbc); 489 | void exfat_evict_inode(struct inode *inode); 490 | int exfat_block_truncate_page(struct inode *inode, loff_t from); 491 | 492 | /* xattr.c */ 493 | #ifdef CONFIG_EXFAT_VIRTUAL_XATTR 494 | extern int exfat_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); 495 | extern ssize_t exfat_getxattr(struct dentry *dentry, const char *name, void *value, size_t size); 496 | extern ssize_t exfat_listxattr(struct dentry *dentry, char *list, size_t size); 497 | extern int exfat_removexattr(struct dentry *dentry, const char *name); 498 | extern const struct xattr_handler *exfat_xattr_handlers[]; 499 | #else 500 | #define exfat_xattr_handlers NULL 501 | #endif 502 | 503 | /* exfat/nls.c */ 504 | unsigned short exfat_toupper(struct super_block *sb, unsigned short a); 505 | int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a, 506 | unsigned short *b, unsigned int len); 507 | int exfat_utf16_to_nls(struct super_block *sb, 508 | struct exfat_uni_name *uniname, unsigned char *p_cstring, 509 | int len); 510 | int exfat_nls_to_utf16(struct super_block *sb, 511 | const unsigned char *p_cstring, const int len, 512 | struct exfat_uni_name *uniname, int *p_lossy); 513 | int exfat_create_upcase_table(struct super_block *sb); 514 | void exfat_free_upcase_table(struct exfat_sb_info *sbi); 515 | 516 | /* exfat/misc.c */ 517 | void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) 518 | __printf(3, 4) __cold; 519 | #define exfat_fs_error(sb, fmt, args...) \ 520 | __exfat_fs_error(sb, 1, fmt, ## args) 521 | #define exfat_fs_error_ratelimit(sb, fmt, args...) \ 522 | __exfat_fs_error(sb, __ratelimit(&EXFAT_SB(sb)->ratelimit), \ 523 | fmt, ## args) 524 | void exfat_msg(struct super_block *sb, const char *lv, const char *fmt, ...) 525 | __printf(3, 4) __cold; 526 | #define exfat_err(sb, fmt, ...) \ 527 | exfat_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__) 528 | #define exfat_warn(sb, fmt, ...) \ 529 | exfat_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__) 530 | #define exfat_info(sb, fmt, ...) \ 531 | exfat_msg(sb, KERN_INFO, fmt, ##__VA_ARGS__) 532 | 533 | void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 534 | u8 tz, __le16 time, __le16 date, u8 time_cs); 535 | void exfat_truncate_atime(struct timespec64 *ts); 536 | void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 537 | u8 *tz, __le16 *time, __le16 *date, u8 *time_cs); 538 | u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type); 539 | u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type); 540 | void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync); 541 | void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, 542 | unsigned int size, unsigned char flags); 543 | void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec); 544 | 545 | #endif /* !_EXFAT_FS_H */ 546 | -------------------------------------------------------------------------------- /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 10 | 11 | #define BOOT_SIGNATURE 0xAA55 12 | #define EXBOOT_SIGNATURE 0xAA550000 13 | #define STR_EXFAT "EXFAT " /* size should be 8 */ 14 | 15 | #define EXFAT_MAX_FILE_LEN 255 16 | 17 | #define VOL_CLEAN 0x0000 18 | #define VOL_DIRTY 0x0002 19 | #define ERR_MEDIUM 0x0004 20 | 21 | #define EXFAT_EOF_CLUSTER 0xFFFFFFFFu 22 | #define EXFAT_BAD_CLUSTER 0xFFFFFFF7u 23 | #define EXFAT_FREE_CLUSTER 0 24 | /* Cluster 0, 1 are reserved, the first cluster is 2 in the cluster heap. */ 25 | #define EXFAT_RESERVED_CLUSTERS 2 26 | #define EXFAT_FIRST_CLUSTER 2 27 | #define EXFAT_DATA_CLUSTER_COUNT(sbi) \ 28 | ((sbi)->num_clusters - EXFAT_RESERVED_CLUSTERS) 29 | 30 | /* AllocationPossible and NoFatChain field in GeneralSecondaryFlags Field */ 31 | #define ALLOC_FAT_CHAIN 0x01 32 | #define ALLOC_NO_FAT_CHAIN 0x03 33 | 34 | #define DENTRY_SIZE 32 /* directory entry size */ 35 | #define DENTRY_SIZE_BITS 5 36 | /* exFAT allows 8388608(256MB) directory entries */ 37 | #define MAX_EXFAT_DENTRIES 8388608 38 | 39 | /* dentry types */ 40 | #define EXFAT_UNUSED 0x00 /* end of directory */ 41 | #define EXFAT_DELETE (~0x80) 42 | #define IS_EXFAT_DELETED(x) ((x) < 0x80) /* deleted file (0x01~0x7F) */ 43 | #define EXFAT_INVAL 0x80 /* invalid value */ 44 | #define EXFAT_BITMAP 0x81 /* allocation bitmap */ 45 | #define EXFAT_UPCASE 0x82 /* upcase table */ 46 | #define EXFAT_VOLUME 0x83 /* volume label */ 47 | #define EXFAT_FILE 0x85 /* file or dir */ 48 | #define EXFAT_GUID 0xA0 49 | #define EXFAT_PADDING 0xA1 50 | #define EXFAT_ACLTAB 0xA2 51 | #define EXFAT_STREAM 0xC0 /* stream entry */ 52 | #define EXFAT_NAME 0xC1 /* file name entry */ 53 | #define EXFAT_ACL 0xC2 /* stream entry */ 54 | 55 | #define IS_EXFAT_CRITICAL_PRI(x) (x < 0xA0) 56 | #define IS_EXFAT_BENIGN_PRI(x) (x < 0xC0) 57 | #define IS_EXFAT_CRITICAL_SEC(x) (x < 0xE0) 58 | 59 | /* checksum types */ 60 | #define CS_DIR_ENTRY 0 61 | #define CS_BOOT_SECTOR 1 62 | #define CS_DEFAULT 2 63 | 64 | /* file attributes */ 65 | #define ATTR_READONLY 0x0001 66 | #define ATTR_HIDDEN 0x0002 67 | #define ATTR_SYSTEM 0x0004 68 | #define ATTR_VOLUME 0x0008 69 | #define ATTR_SUBDIR 0x0010 70 | #define ATTR_ARCHIVE 0x0020 71 | 72 | #define ATTR_RWMASK (ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME | \ 73 | ATTR_SUBDIR | ATTR_ARCHIVE) 74 | 75 | #define BOOTSEC_JUMP_BOOT_LEN 3 76 | #define BOOTSEC_FS_NAME_LEN 8 77 | #define BOOTSEC_OLDBPB_LEN 53 78 | 79 | #define EXFAT_FILE_NAME_LEN 15 80 | 81 | /* EXFAT: Main and Backup Boot Sector (512 bytes) */ 82 | struct boot_sector { 83 | __u8 jmp_boot[BOOTSEC_JUMP_BOOT_LEN]; 84 | __u8 fs_name[BOOTSEC_FS_NAME_LEN]; 85 | __u8 must_be_zero[BOOTSEC_OLDBPB_LEN]; 86 | __le64 partition_offset; 87 | __le64 vol_length; 88 | __le32 fat_offset; 89 | __le32 fat_length; 90 | __le32 clu_offset; 91 | __le32 clu_count; 92 | __le32 root_cluster; 93 | __le32 vol_serial; 94 | __u8 fs_revision[2]; 95 | __le16 vol_flags; 96 | __u8 sect_size_bits; 97 | __u8 sect_per_clus_bits; 98 | __u8 num_fats; 99 | __u8 drv_sel; 100 | __u8 percent_in_use; 101 | __u8 reserved[7]; 102 | __u8 boot_code[390]; 103 | __le16 signature; 104 | } __packed; 105 | 106 | struct exfat_dentry { 107 | __u8 type; 108 | union { 109 | struct { 110 | __u8 num_ext; 111 | __le16 checksum; 112 | __le16 attr; 113 | __le16 reserved1; 114 | __le16 create_time; 115 | __le16 create_date; 116 | __le16 modify_time; 117 | __le16 modify_date; 118 | __le16 access_time; 119 | __le16 access_date; 120 | __u8 create_time_cs; 121 | __u8 modify_time_cs; 122 | __u8 create_tz; 123 | __u8 modify_tz; 124 | __u8 access_tz; 125 | __u8 reserved2[7]; 126 | } __packed file; /* file directory entry */ 127 | struct { 128 | __u8 flags; 129 | __u8 reserved1; 130 | __u8 name_len; 131 | __le16 name_hash; 132 | __le16 reserved2; 133 | __le64 valid_size; 134 | __le32 reserved3; 135 | __le32 start_clu; 136 | __le64 size; 137 | } __packed stream; /* stream extension directory entry */ 138 | struct { 139 | __u8 flags; 140 | __le16 unicode_0_14[EXFAT_FILE_NAME_LEN]; 141 | } __packed name; /* file name directory entry */ 142 | struct { 143 | __u8 flags; 144 | __u8 reserved[18]; 145 | __le32 start_clu; 146 | __le64 size; 147 | } __packed bitmap; /* allocation bitmap directory entry */ 148 | struct { 149 | __u8 reserved1[3]; 150 | __le32 checksum; 151 | __u8 reserved2[12]; 152 | __le32 start_clu; 153 | __le64 size; 154 | } __packed upcase; /* up-case table directory entry */ 155 | } __packed dentry; 156 | } __packed; 157 | 158 | #define EXFAT_TZ_VALID (1 << 7) 159 | 160 | /* Jan 1 GMT 00:00:00 1980 */ 161 | #define EXFAT_MIN_TIMESTAMP_SECS 315532800LL 162 | /* Dec 31 GMT 23:59:59 2107 */ 163 | #define EXFAT_MAX_TIMESTAMP_SECS 4354819199LL 164 | 165 | #endif /* !_EXFAT_RAW_H */ 166 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | 10 | #include "exfat_fs.h" 11 | 12 | static int exfat_mirror_bh(struct super_block *sb, sector_t sec, 13 | struct buffer_head *bh) 14 | { 15 | struct buffer_head *c_bh; 16 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 17 | sector_t sec2; 18 | int err = 0; 19 | 20 | if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) { 21 | sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector; 22 | c_bh = sb_getblk(sb, sec2); 23 | if (!c_bh) 24 | return -ENOMEM; 25 | memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize); 26 | set_buffer_uptodate(c_bh); 27 | mark_buffer_dirty(c_bh); 28 | if (sb->s_flags & SB_SYNCHRONOUS) 29 | err = sync_dirty_buffer(c_bh); 30 | brelse(c_bh); 31 | } 32 | 33 | return err; 34 | } 35 | 36 | static int __exfat_ent_get(struct super_block *sb, unsigned int loc, 37 | unsigned int *content) 38 | { 39 | unsigned int off; 40 | sector_t sec; 41 | struct buffer_head *bh; 42 | 43 | sec = FAT_ENT_OFFSET_SECTOR(sb, loc); 44 | off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc); 45 | 46 | bh = sb_bread(sb, sec); 47 | if (!bh) 48 | return -EIO; 49 | 50 | *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off])); 51 | 52 | /* remap reserved clusters to simplify code */ 53 | if (*content > EXFAT_BAD_CLUSTER) 54 | *content = EXFAT_EOF_CLUSTER; 55 | 56 | brelse(bh); 57 | return 0; 58 | } 59 | 60 | int exfat_ent_set(struct super_block *sb, unsigned int loc, 61 | unsigned int content) 62 | { 63 | unsigned int off; 64 | sector_t sec; 65 | __le32 *fat_entry; 66 | struct buffer_head *bh; 67 | 68 | sec = FAT_ENT_OFFSET_SECTOR(sb, loc); 69 | off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc); 70 | 71 | bh = sb_bread(sb, sec); 72 | if (!bh) 73 | return -EIO; 74 | 75 | fat_entry = (__le32 *)&(bh->b_data[off]); 76 | *fat_entry = cpu_to_le32(content); 77 | exfat_update_bh(sb, bh, sb->s_flags & SB_SYNCHRONOUS); 78 | exfat_mirror_bh(sb, sec, bh); 79 | brelse(bh); 80 | return 0; 81 | } 82 | 83 | static inline bool is_valid_cluster(struct exfat_sb_info *sbi, 84 | unsigned int clus) 85 | { 86 | if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus) 87 | return false; 88 | return true; 89 | } 90 | 91 | int exfat_ent_get(struct super_block *sb, unsigned int loc, 92 | unsigned int *content) 93 | { 94 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 95 | int err; 96 | 97 | if (!is_valid_cluster(sbi, loc)) { 98 | exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", 99 | loc); 100 | return -EIO; 101 | } 102 | 103 | err = __exfat_ent_get(sb, loc, content); 104 | if (err) { 105 | exfat_fs_error(sb, 106 | "failed to access to FAT (entry 0x%08x, err:%d)", 107 | loc, err); 108 | return err; 109 | } 110 | 111 | if (*content == EXFAT_FREE_CLUSTER) { 112 | exfat_fs_error(sb, 113 | "invalid access to FAT free cluster (entry 0x%08x)", 114 | loc); 115 | return -EIO; 116 | } 117 | 118 | if (*content == EXFAT_BAD_CLUSTER) { 119 | exfat_fs_error(sb, 120 | "invalid access to FAT bad cluster (entry 0x%08x)", 121 | loc); 122 | return -EIO; 123 | } 124 | 125 | if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) { 126 | exfat_fs_error(sb, 127 | "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)", 128 | loc, *content); 129 | return -EIO; 130 | } 131 | 132 | return 0; 133 | } 134 | 135 | int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain, 136 | unsigned int len) 137 | { 138 | if (!len) 139 | return 0; 140 | 141 | while (len > 1) { 142 | if (exfat_ent_set(sb, chain, chain + 1)) 143 | return -EIO; 144 | chain++; 145 | len--; 146 | } 147 | 148 | if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER)) 149 | return -EIO; 150 | return 0; 151 | } 152 | 153 | int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain) 154 | { 155 | unsigned int num_clusters = 0; 156 | unsigned int clu; 157 | struct super_block *sb = inode->i_sb; 158 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 159 | 160 | /* invalid cluster number */ 161 | if (p_chain->dir == EXFAT_FREE_CLUSTER || 162 | p_chain->dir == EXFAT_EOF_CLUSTER || 163 | p_chain->dir < EXFAT_FIRST_CLUSTER) 164 | return 0; 165 | 166 | /* no cluster to truncate */ 167 | if (p_chain->size == 0) 168 | return 0; 169 | 170 | /* check cluster validation */ 171 | if (!is_valid_cluster(sbi, p_chain->dir)) { 172 | exfat_err(sb, "invalid start cluster (%u)", p_chain->dir); 173 | return -EIO; 174 | } 175 | 176 | set_bit(EXFAT_SB_DIRTY, &sbi->s_state); 177 | clu = p_chain->dir; 178 | 179 | if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 180 | do { 181 | exfat_clear_bitmap(inode, clu); 182 | clu++; 183 | 184 | num_clusters++; 185 | } while (num_clusters < p_chain->size); 186 | } else { 187 | do { 188 | exfat_clear_bitmap(inode, clu); 189 | 190 | if (exfat_get_next_cluster(sb, &clu)) 191 | goto dec_used_clus; 192 | 193 | num_clusters++; 194 | } while (clu != EXFAT_EOF_CLUSTER); 195 | } 196 | 197 | dec_used_clus: 198 | sbi->used_clusters -= num_clusters; 199 | return 0; 200 | } 201 | 202 | int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, 203 | unsigned int *ret_clu) 204 | { 205 | unsigned int clu, next; 206 | unsigned int count = 0; 207 | 208 | next = p_chain->dir; 209 | if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 210 | *ret_clu = next + p_chain->size - 1; 211 | return 0; 212 | } 213 | 214 | do { 215 | count++; 216 | clu = next; 217 | if (exfat_ent_get(sb, clu, &next)) 218 | return -EIO; 219 | } while (next != EXFAT_EOF_CLUSTER); 220 | 221 | if (p_chain->size != count) { 222 | exfat_fs_error(sb, 223 | "bogus directory size (clus : ondisk(%d) != counted(%d))", 224 | p_chain->size, count); 225 | return -EIO; 226 | } 227 | 228 | *ret_clu = clu; 229 | return 0; 230 | } 231 | 232 | static inline int exfat_sync_bhs(struct buffer_head **bhs, int nr_bhs) 233 | { 234 | int i, err = 0; 235 | 236 | for (i = 0; i < nr_bhs; i++) 237 | write_dirty_buffer(bhs[i], 0); 238 | 239 | for (i = 0; i < nr_bhs; i++) { 240 | wait_on_buffer(bhs[i]); 241 | if (!err && !buffer_uptodate(bhs[i])) 242 | err = -EIO; 243 | } 244 | return err; 245 | } 246 | 247 | int exfat_zeroed_cluster(struct inode *dir, unsigned int clu) 248 | { 249 | struct super_block *sb = dir->i_sb; 250 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 251 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 252 | int nr_bhs = MAX_BUF_PER_PAGE; 253 | sector_t blknr, last_blknr; 254 | int err, i, n; 255 | 256 | blknr = exfat_cluster_to_sector(sbi, clu); 257 | last_blknr = blknr + sbi->sect_per_clus; 258 | 259 | if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) { 260 | exfat_fs_error_ratelimit(sb, 261 | "%s: out of range(sect:%llu len:%u)", 262 | __func__, (unsigned long long)blknr, 263 | sbi->sect_per_clus); 264 | return -EIO; 265 | } 266 | 267 | /* Zeroing the unused blocks on this cluster */ 268 | n = 0; 269 | while (blknr < last_blknr) { 270 | bhs[n] = sb_getblk(sb, blknr); 271 | if (!bhs[n]) { 272 | err = -ENOMEM; 273 | goto release_bhs; 274 | } 275 | memset(bhs[n]->b_data, 0, sb->s_blocksize); 276 | exfat_update_bh(sb, bhs[n], 0); 277 | 278 | n++; 279 | blknr++; 280 | 281 | if (n == nr_bhs) { 282 | if (IS_DIRSYNC(dir)) { 283 | err = exfat_sync_bhs(bhs, n); 284 | if (err) 285 | goto release_bhs; 286 | } 287 | 288 | for (i = 0; i < n; i++) 289 | brelse(bhs[i]); 290 | n = 0; 291 | } 292 | } 293 | 294 | if (IS_DIRSYNC(dir)) { 295 | err = exfat_sync_bhs(bhs, n); 296 | if (err) 297 | goto release_bhs; 298 | } 299 | 300 | for (i = 0; i < n; i++) 301 | brelse(bhs[i]); 302 | 303 | return 0; 304 | 305 | release_bhs: 306 | exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr); 307 | for (i = 0; i < n; i++) 308 | bforget(bhs[i]); 309 | return err; 310 | } 311 | 312 | int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, 313 | struct exfat_chain *p_chain) 314 | { 315 | int ret = -ENOSPC; 316 | unsigned int num_clusters = 0, total_cnt; 317 | unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER; 318 | struct super_block *sb = inode->i_sb; 319 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 320 | 321 | total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi); 322 | 323 | if (unlikely(total_cnt < sbi->used_clusters)) { 324 | exfat_fs_error_ratelimit(sb, 325 | "%s: invalid used clusters(t:%u,u:%u)\n", 326 | __func__, total_cnt, sbi->used_clusters); 327 | return -EIO; 328 | } 329 | 330 | if (num_alloc > total_cnt - sbi->used_clusters) 331 | return -ENOSPC; 332 | 333 | hint_clu = p_chain->dir; 334 | /* find new cluster */ 335 | if (hint_clu == EXFAT_EOF_CLUSTER) { 336 | if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) { 337 | exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n", 338 | sbi->clu_srch_ptr); 339 | sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; 340 | } 341 | 342 | hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr); 343 | if (hint_clu == EXFAT_EOF_CLUSTER) 344 | return -ENOSPC; 345 | } 346 | 347 | /* check cluster validation */ 348 | if (!is_valid_cluster(sbi, hint_clu)) { 349 | exfat_err(sb, "hint_cluster is invalid (%u)", 350 | hint_clu); 351 | hint_clu = EXFAT_FIRST_CLUSTER; 352 | if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 353 | if (exfat_chain_cont_cluster(sb, p_chain->dir, 354 | num_clusters)) 355 | return -EIO; 356 | p_chain->flags = ALLOC_FAT_CHAIN; 357 | } 358 | } 359 | 360 | set_bit(EXFAT_SB_DIRTY, &sbi->s_state); 361 | 362 | p_chain->dir = EXFAT_EOF_CLUSTER; 363 | 364 | while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) != 365 | EXFAT_EOF_CLUSTER) { 366 | if (new_clu != hint_clu && 367 | p_chain->flags == ALLOC_NO_FAT_CHAIN) { 368 | if (exfat_chain_cont_cluster(sb, p_chain->dir, 369 | num_clusters)) { 370 | ret = -EIO; 371 | goto free_cluster; 372 | } 373 | p_chain->flags = ALLOC_FAT_CHAIN; 374 | } 375 | 376 | /* update allocation bitmap */ 377 | if (exfat_set_bitmap(inode, new_clu)) { 378 | ret = -EIO; 379 | goto free_cluster; 380 | } 381 | 382 | num_clusters++; 383 | 384 | /* update FAT table */ 385 | if (p_chain->flags == ALLOC_FAT_CHAIN) { 386 | if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) { 387 | ret = -EIO; 388 | goto free_cluster; 389 | } 390 | } 391 | 392 | if (p_chain->dir == EXFAT_EOF_CLUSTER) { 393 | p_chain->dir = new_clu; 394 | } else if (p_chain->flags == ALLOC_FAT_CHAIN) { 395 | if (exfat_ent_set(sb, last_clu, new_clu)) { 396 | ret = -EIO; 397 | goto free_cluster; 398 | } 399 | } 400 | last_clu = new_clu; 401 | 402 | if (--num_alloc == 0) { 403 | sbi->clu_srch_ptr = hint_clu; 404 | sbi->used_clusters += num_clusters; 405 | 406 | p_chain->size += num_clusters; 407 | return 0; 408 | } 409 | 410 | hint_clu = new_clu + 1; 411 | if (hint_clu >= sbi->num_clusters) { 412 | hint_clu = EXFAT_FIRST_CLUSTER; 413 | 414 | if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 415 | if (exfat_chain_cont_cluster(sb, p_chain->dir, 416 | num_clusters)) { 417 | ret = -EIO; 418 | goto free_cluster; 419 | } 420 | p_chain->flags = ALLOC_FAT_CHAIN; 421 | } 422 | } 423 | } 424 | free_cluster: 425 | if (num_clusters) 426 | exfat_free_cluster(inode, p_chain); 427 | return ret; 428 | } 429 | 430 | int exfat_count_num_clusters(struct super_block *sb, 431 | struct exfat_chain *p_chain, unsigned int *ret_count) 432 | { 433 | unsigned int i, count; 434 | unsigned int clu; 435 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 436 | 437 | if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) { 438 | *ret_count = 0; 439 | return 0; 440 | } 441 | 442 | if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 443 | *ret_count = p_chain->size; 444 | return 0; 445 | } 446 | 447 | clu = p_chain->dir; 448 | count = 0; 449 | for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) { 450 | count++; 451 | if (exfat_ent_get(sb, clu, &clu)) 452 | return -EIO; 453 | if (clu == EXFAT_EOF_CLUSTER) 454 | break; 455 | } 456 | 457 | *ret_count = count; 458 | return 0; 459 | } 460 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | #include 10 | 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 super_block *sb = inode->i_sb; 100 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 101 | struct exfat_inode_info *ei = EXFAT_I(inode); 102 | int evict = (ei->dir.dir == DIR_DELETED) ? 1 : 0; 103 | 104 | /* check if the given file ID is opened */ 105 | if (ei->type != TYPE_FILE && ei->type != TYPE_DIR) 106 | return -EPERM; 107 | 108 | exfat_set_vol_flags(sb, VOL_DIRTY); 109 | 110 | num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); 111 | num_clusters_phys = 112 | EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, sbi); 113 | 114 | exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); 115 | 116 | if (new_size > 0) { 117 | /* 118 | * Truncate FAT chain num_clusters after the first cluster 119 | * num_clusters = min(new, phys); 120 | */ 121 | unsigned int num_clusters = 122 | min(num_clusters_new, num_clusters_phys); 123 | 124 | /* 125 | * Follow FAT chain 126 | * (defensive coding - works fine even with corrupted FAT table 127 | */ 128 | if (clu.flags == ALLOC_NO_FAT_CHAIN) { 129 | clu.dir += num_clusters; 130 | clu.size -= num_clusters; 131 | } else { 132 | while (num_clusters > 0) { 133 | last_clu = clu.dir; 134 | if (exfat_get_next_cluster(sb, &(clu.dir))) 135 | return -EIO; 136 | 137 | num_clusters--; 138 | clu.size--; 139 | } 140 | } 141 | } else { 142 | ei->flags = ALLOC_NO_FAT_CHAIN; 143 | ei->start_clu = EXFAT_EOF_CLUSTER; 144 | } 145 | 146 | i_size_write(inode, new_size); 147 | 148 | if (ei->type == TYPE_FILE) 149 | ei->attr |= ATTR_ARCHIVE; 150 | 151 | /* update the directory entry */ 152 | if (!evict) { 153 | struct timespec64 ts; 154 | struct exfat_dentry *ep, *ep2; 155 | struct exfat_entry_set_cache *es; 156 | 157 | es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, 158 | ES_ALL_ENTRIES); 159 | if (!es) 160 | return -EIO; 161 | ep = exfat_get_dentry_cached(es, 0); 162 | ep2 = exfat_get_dentry_cached(es, 1); 163 | 164 | ts = current_time(inode); 165 | exfat_set_entry_time(sbi, &ts, 166 | &ep->dentry.file.modify_tz, 167 | &ep->dentry.file.modify_time, 168 | &ep->dentry.file.modify_date, 169 | &ep->dentry.file.modify_time_cs); 170 | ep->dentry.file.attr = cpu_to_le16(ei->attr); 171 | 172 | /* File size should be zero if there is no cluster allocated */ 173 | if (ei->start_clu == EXFAT_EOF_CLUSTER) { 174 | ep2->dentry.stream.valid_size = 0; 175 | ep2->dentry.stream.size = 0; 176 | } else { 177 | ep2->dentry.stream.valid_size = cpu_to_le64(new_size); 178 | ep2->dentry.stream.size = ep->dentry.stream.valid_size; 179 | } 180 | 181 | if (new_size == 0) { 182 | /* Any directory can not be truncated to zero */ 183 | WARN_ON(ei->type != TYPE_FILE); 184 | 185 | ep2->dentry.stream.flags = ALLOC_FAT_CHAIN; 186 | ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER; 187 | } 188 | 189 | exfat_update_dir_chksum_with_entry_set(es); 190 | exfat_free_dentry_set(es, inode_needs_sync(inode)); 191 | } 192 | 193 | /* cut off from the FAT chain */ 194 | if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER && 195 | last_clu != EXFAT_EOF_CLUSTER) { 196 | if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER)) 197 | return -EIO; 198 | } 199 | 200 | /* invalidate cache and free the clusters */ 201 | /* clear exfat cache */ 202 | exfat_cache_inval_inode(inode); 203 | 204 | /* hint information */ 205 | ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 206 | ei->hint_bmap.clu = EXFAT_EOF_CLUSTER; 207 | if (ei->rwoffset > new_size) 208 | ei->rwoffset = new_size; 209 | 210 | /* hint_stat will be used if this is directory. */ 211 | ei->hint_stat.eidx = 0; 212 | ei->hint_stat.clu = ei->start_clu; 213 | ei->hint_femp.eidx = EXFAT_HINT_NONE; 214 | 215 | /* free the clusters */ 216 | if (exfat_free_cluster(inode, &clu)) 217 | return -EIO; 218 | 219 | exfat_set_vol_flags(sb, VOL_CLEAN); 220 | 221 | return 0; 222 | } 223 | 224 | void exfat_truncate(struct inode *inode, loff_t size) 225 | { 226 | struct super_block *sb = inode->i_sb; 227 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 228 | unsigned int blocksize = 1 << inode->i_blkbits; 229 | loff_t aligned_size; 230 | int err; 231 | 232 | mutex_lock(&sbi->s_lock); 233 | if (EXFAT_I(inode)->start_clu == 0) { 234 | /* 235 | * Empty start_clu != ~0 (not allocated) 236 | */ 237 | exfat_fs_error(sb, "tried to truncate zeroed cluster."); 238 | goto write_size; 239 | } 240 | 241 | err = __exfat_truncate(inode, i_size_read(inode)); 242 | if (err) 243 | goto write_size; 244 | 245 | inode->i_ctime = inode->i_mtime = current_time(inode); 246 | if (IS_DIRSYNC(inode)) 247 | exfat_sync_inode(inode); 248 | else 249 | mark_inode_dirty(inode); 250 | 251 | inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) & 252 | ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 253 | write_size: 254 | aligned_size = i_size_read(inode); 255 | if (aligned_size & (blocksize - 1)) { 256 | aligned_size |= (blocksize - 1); 257 | aligned_size++; 258 | } 259 | 260 | if (EXFAT_I(inode)->i_size_ondisk > i_size_read(inode)) 261 | EXFAT_I(inode)->i_size_ondisk = aligned_size; 262 | 263 | if (EXFAT_I(inode)->i_size_aligned > i_size_read(inode)) 264 | EXFAT_I(inode)->i_size_aligned = aligned_size; 265 | mutex_unlock(&sbi->s_lock); 266 | } 267 | 268 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) 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 | exfat_truncate_atime(&stat->atime); 277 | stat->result_mask |= STATX_BTIME; 278 | stat->btime.tv_sec = ei->i_crtime.tv_sec; 279 | stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 280 | stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size; 281 | return 0; 282 | } 283 | #else 284 | int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 285 | { 286 | struct inode *inode = dentry->d_inode; 287 | 288 | generic_fillattr(inode, stat); 289 | exfat_truncate_atime(&stat->atime); 290 | stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size; 291 | 292 | return 0; 293 | } 294 | #endif 295 | 296 | int exfat_setattr(struct dentry *dentry, struct iattr *attr) 297 | { 298 | struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb); 299 | struct inode *inode = dentry->d_inode; 300 | unsigned int ia_valid; 301 | int error; 302 | 303 | if ((attr->ia_valid & ATTR_SIZE) && 304 | attr->ia_size > i_size_read(inode)) { 305 | error = exfat_cont_expand(inode, attr->ia_size); 306 | if (error || attr->ia_valid == ATTR_SIZE) 307 | goto out; 308 | attr->ia_valid &= ~ATTR_SIZE; 309 | } 310 | 311 | /* Check for setting the inode time. */ 312 | ia_valid = attr->ia_valid; 313 | if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) && 314 | exfat_allow_set_time(sbi, inode)) { 315 | attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | 316 | ATTR_TIMES_SET); 317 | } 318 | 319 | error = setattr_prepare(dentry, attr); 320 | attr->ia_valid = ia_valid; 321 | if (error) { 322 | if (sbi->options.quiet) 323 | error = 0; 324 | goto out; 325 | } 326 | 327 | if (((attr->ia_valid & ATTR_UID) && 328 | !uid_eq(attr->ia_uid, sbi->options.fs_uid)) || 329 | ((attr->ia_valid & ATTR_GID) && 330 | !gid_eq(attr->ia_gid, sbi->options.fs_gid)) || 331 | ((attr->ia_valid & ATTR_MODE) && 332 | (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) { 333 | error = -EPERM; 334 | goto out; 335 | } 336 | 337 | if (error) { 338 | if (sbi->options.quiet) 339 | error = 0; 340 | goto out; 341 | } 342 | 343 | /* 344 | * We don't return -EPERM here. Yes, strange, but this is too 345 | * old behavior. 346 | */ 347 | if (attr->ia_valid & ATTR_MODE) { 348 | if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) 349 | attr->ia_valid &= ~ATTR_MODE; 350 | } 351 | 352 | if (attr->ia_valid & ATTR_SIZE) { 353 | error = exfat_block_truncate_page(inode, attr->ia_size); 354 | if (error) 355 | goto out; 356 | 357 | down_write(&EXFAT_I(inode)->truncate_lock); 358 | truncate_setsize(inode, attr->ia_size); 359 | exfat_truncate(inode, attr->ia_size); 360 | up_write(&EXFAT_I(inode)->truncate_lock); 361 | } 362 | 363 | setattr_copy(inode, attr); 364 | exfat_truncate_atime(&inode->i_atime); 365 | mark_inode_dirty(inode); 366 | 367 | out: 368 | return error; 369 | } 370 | 371 | int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) 372 | { 373 | struct inode *inode = filp->f_mapping->host; 374 | int err; 375 | 376 | err = __generic_file_fsync(filp, start, end, datasync); 377 | if (err) 378 | return err; 379 | 380 | err = sync_blockdev(inode->i_sb->s_bdev); 381 | if (err) 382 | return err; 383 | 384 | return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); 385 | } 386 | 387 | const struct file_operations exfat_file_operations = { 388 | .llseek = generic_file_llseek, 389 | .read_iter = generic_file_read_iter, 390 | .write_iter = generic_file_write_iter, 391 | .mmap = generic_file_mmap, 392 | .fsync = exfat_file_fsync, 393 | .splice_read = generic_file_splice_read, 394 | .splice_write = iter_file_splice_write, 395 | }; 396 | 397 | const struct inode_operations exfat_file_inode_operations = { 398 | .setattr = exfat_setattr, 399 | .getattr = exfat_getattr, 400 | #ifdef CONFIG_EXFAT_VIRTUAL_XATTR 401 | .listxattr = exfat_listxattr, 402 | #endif 403 | }; 404 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "exfat_fs.h" 17 | 18 | static int __exfat_write_inode(struct inode *inode, int sync) 19 | { 20 | unsigned long long on_disk_size; 21 | struct exfat_dentry *ep, *ep2; 22 | struct exfat_entry_set_cache *es = NULL; 23 | struct super_block *sb = inode->i_sb; 24 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 25 | struct exfat_inode_info *ei = EXFAT_I(inode); 26 | bool is_dir = (ei->type == TYPE_DIR) ? true : false; 27 | 28 | if (inode->i_ino == EXFAT_ROOT_INO) 29 | return 0; 30 | 31 | /* 32 | * If the indode is already unlinked, there is no need for updating it. 33 | */ 34 | if (ei->dir.dir == DIR_DELETED) 35 | return 0; 36 | 37 | if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1) 38 | return 0; 39 | 40 | exfat_set_vol_flags(sb, VOL_DIRTY); 41 | 42 | /* get the directory entry of given file or directory */ 43 | es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES); 44 | if (!es) 45 | return -EIO; 46 | ep = exfat_get_dentry_cached(es, 0); 47 | ep2 = exfat_get_dentry_cached(es, 1); 48 | 49 | ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode)); 50 | 51 | /* set FILE_INFO structure using the acquired struct exfat_dentry */ 52 | exfat_set_entry_time(sbi, &ei->i_crtime, 53 | &ep->dentry.file.create_tz, 54 | &ep->dentry.file.create_time, 55 | &ep->dentry.file.create_date, 56 | &ep->dentry.file.create_time_cs); 57 | exfat_set_entry_time(sbi, &inode->i_mtime, 58 | &ep->dentry.file.modify_tz, 59 | &ep->dentry.file.modify_time, 60 | &ep->dentry.file.modify_date, 61 | &ep->dentry.file.modify_time_cs); 62 | exfat_set_entry_time(sbi, &inode->i_atime, 63 | &ep->dentry.file.access_tz, 64 | &ep->dentry.file.access_time, 65 | &ep->dentry.file.access_date, 66 | NULL); 67 | 68 | /* File size should be zero if there is no cluster allocated */ 69 | on_disk_size = i_size_read(inode); 70 | 71 | if (ei->start_clu == EXFAT_EOF_CLUSTER) 72 | on_disk_size = 0; 73 | 74 | ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size); 75 | ep2->dentry.stream.size = ep2->dentry.stream.valid_size; 76 | 77 | exfat_update_dir_chksum_with_entry_set(es); 78 | exfat_free_dentry_set(es, sync); 79 | return 0; 80 | } 81 | 82 | int exfat_write_inode(struct inode *inode, struct writeback_control *wbc) 83 | { 84 | int ret; 85 | 86 | mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 87 | ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 88 | mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 89 | 90 | return ret; 91 | } 92 | 93 | void exfat_sync_inode(struct inode *inode) 94 | { 95 | lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock); 96 | __exfat_write_inode(inode, 1); 97 | } 98 | 99 | /* 100 | * Input: inode, (logical) clu_offset, target allocation area 101 | * Output: errcode, cluster number 102 | * *clu = (~0), if it's unable to allocate a new cluster 103 | */ 104 | static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, 105 | unsigned int *clu, int create) 106 | { 107 | int ret, modified = false; 108 | unsigned int last_clu; 109 | struct exfat_chain new_clu; 110 | struct super_block *sb = inode->i_sb; 111 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 112 | struct exfat_inode_info *ei = EXFAT_I(inode); 113 | unsigned int local_clu_offset = clu_offset; 114 | unsigned int num_to_be_allocated = 0, num_clusters = 0; 115 | 116 | ei->rwoffset = EXFAT_CLU_TO_B(clu_offset, sbi); 117 | 118 | if (EXFAT_I(inode)->i_size_ondisk > 0) 119 | num_clusters = 120 | EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, 121 | sbi); 122 | 123 | if (clu_offset >= num_clusters) 124 | num_to_be_allocated = clu_offset - num_clusters + 1; 125 | 126 | if (!create && (num_to_be_allocated > 0)) { 127 | *clu = EXFAT_EOF_CLUSTER; 128 | return 0; 129 | } 130 | 131 | *clu = last_clu = ei->start_clu; 132 | 133 | if (ei->flags == ALLOC_NO_FAT_CHAIN) { 134 | if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) { 135 | last_clu += clu_offset - 1; 136 | 137 | if (clu_offset == num_clusters) 138 | *clu = EXFAT_EOF_CLUSTER; 139 | else 140 | *clu += clu_offset; 141 | } 142 | } else if (ei->type == TYPE_FILE) { 143 | unsigned int fclus = 0; 144 | int err = exfat_get_cluster(inode, clu_offset, 145 | &fclus, clu, &last_clu, 1); 146 | if (err) 147 | return -EIO; 148 | 149 | clu_offset -= fclus; 150 | } else { 151 | /* hint information */ 152 | if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && 153 | ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { 154 | clu_offset -= ei->hint_bmap.off; 155 | /* hint_bmap.clu should be valid */ 156 | WARN_ON(ei->hint_bmap.clu < 2); 157 | *clu = ei->hint_bmap.clu; 158 | } 159 | 160 | while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) { 161 | last_clu = *clu; 162 | if (exfat_get_next_cluster(sb, clu)) 163 | return -EIO; 164 | clu_offset--; 165 | } 166 | } 167 | 168 | if (*clu == EXFAT_EOF_CLUSTER) { 169 | exfat_set_vol_flags(sb, VOL_DIRTY); 170 | 171 | new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ? 172 | EXFAT_EOF_CLUSTER : last_clu + 1; 173 | new_clu.size = 0; 174 | new_clu.flags = ei->flags; 175 | 176 | /* allocate a cluster */ 177 | if (num_to_be_allocated < 1) { 178 | /* Broken FAT (i_sze > allocated FAT) */ 179 | exfat_fs_error(sb, "broken FAT chain."); 180 | return -EIO; 181 | } 182 | 183 | ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu); 184 | if (ret) 185 | return ret; 186 | 187 | if (new_clu.dir == EXFAT_EOF_CLUSTER || 188 | new_clu.dir == EXFAT_FREE_CLUSTER) { 189 | exfat_fs_error(sb, 190 | "bogus cluster new allocated (last_clu : %u, new_clu : %u)", 191 | last_clu, new_clu.dir); 192 | return -EIO; 193 | } 194 | 195 | /* append to the FAT chain */ 196 | if (last_clu == EXFAT_EOF_CLUSTER) { 197 | if (new_clu.flags == ALLOC_FAT_CHAIN) 198 | ei->flags = ALLOC_FAT_CHAIN; 199 | ei->start_clu = new_clu.dir; 200 | modified = true; 201 | } else { 202 | if (new_clu.flags != ei->flags) { 203 | /* no-fat-chain bit is disabled, 204 | * so fat-chain should be synced with 205 | * alloc-bitmap 206 | */ 207 | exfat_chain_cont_cluster(sb, ei->start_clu, 208 | num_clusters); 209 | ei->flags = ALLOC_FAT_CHAIN; 210 | modified = true; 211 | } 212 | if (new_clu.flags == ALLOC_FAT_CHAIN) 213 | if (exfat_ent_set(sb, last_clu, new_clu.dir)) 214 | return -EIO; 215 | } 216 | 217 | num_clusters += num_to_be_allocated; 218 | *clu = new_clu.dir; 219 | 220 | if (ei->dir.dir != DIR_DELETED && modified) { 221 | struct exfat_dentry *ep; 222 | struct exfat_entry_set_cache *es; 223 | 224 | es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, 225 | ES_ALL_ENTRIES); 226 | if (!es) 227 | return -EIO; 228 | /* get stream entry */ 229 | ep = exfat_get_dentry_cached(es, 1); 230 | 231 | /* update directory entry */ 232 | ep->dentry.stream.flags = ei->flags; 233 | ep->dentry.stream.start_clu = 234 | cpu_to_le32(ei->start_clu); 235 | ep->dentry.stream.valid_size = 236 | cpu_to_le64(i_size_read(inode)); 237 | ep->dentry.stream.size = 238 | ep->dentry.stream.valid_size; 239 | 240 | exfat_update_dir_chksum_with_entry_set(es); 241 | exfat_free_dentry_set(es, inode_needs_sync(inode)); 242 | 243 | } /* end of if != DIR_DELETED */ 244 | 245 | inode->i_blocks += 246 | num_to_be_allocated << sbi->sect_per_clus_bits; 247 | 248 | /* 249 | * Move *clu pointer along FAT chains (hole care) because the 250 | * caller of this function expect *clu to be the last cluster. 251 | * This only works when num_to_be_allocated >= 2, 252 | * *clu = (the first cluster of the allocated chain) => 253 | * (the last cluster of ...) 254 | */ 255 | if (ei->flags == ALLOC_NO_FAT_CHAIN) { 256 | *clu += num_to_be_allocated - 1; 257 | } else { 258 | while (num_to_be_allocated > 1) { 259 | if (exfat_get_next_cluster(sb, clu)) 260 | return -EIO; 261 | num_to_be_allocated--; 262 | } 263 | } 264 | 265 | } 266 | 267 | /* hint information */ 268 | ei->hint_bmap.off = local_clu_offset; 269 | ei->hint_bmap.clu = *clu; 270 | 271 | return 0; 272 | } 273 | 274 | static int exfat_map_new_buffer(struct exfat_inode_info *ei, 275 | struct buffer_head *bh, loff_t pos) 276 | { 277 | if (buffer_delay(bh) && pos > ei->i_size_aligned) 278 | return -EIO; 279 | set_buffer_new(bh); 280 | 281 | /* 282 | * Adjust i_size_aligned if i_size_ondisk is bigger than it. 283 | */ 284 | if (ei->i_size_ondisk > ei->i_size_aligned) 285 | ei->i_size_aligned = ei->i_size_ondisk; 286 | return 0; 287 | } 288 | 289 | static int exfat_get_block(struct inode *inode, sector_t iblock, 290 | struct buffer_head *bh_result, int create) 291 | { 292 | struct exfat_inode_info *ei = EXFAT_I(inode); 293 | struct super_block *sb = inode->i_sb; 294 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 295 | unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 296 | int err = 0; 297 | unsigned long mapped_blocks = 0; 298 | unsigned int cluster, sec_offset; 299 | sector_t last_block; 300 | sector_t phys = 0; 301 | loff_t pos; 302 | 303 | mutex_lock(&sbi->s_lock); 304 | last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb); 305 | if (iblock >= last_block && !create) 306 | goto done; 307 | 308 | /* Is this block already allocated? */ 309 | err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits, 310 | &cluster, create); 311 | if (err) { 312 | if (err != -ENOSPC) 313 | exfat_fs_error_ratelimit(sb, 314 | "failed to bmap (inode : %p iblock : %llu, err : %d)", 315 | inode, (unsigned long long)iblock, err); 316 | goto unlock_ret; 317 | } 318 | 319 | if (cluster == EXFAT_EOF_CLUSTER) 320 | goto done; 321 | 322 | /* sector offset in cluster */ 323 | sec_offset = iblock & (sbi->sect_per_clus - 1); 324 | 325 | phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset; 326 | mapped_blocks = sbi->sect_per_clus - sec_offset; 327 | max_blocks = min(mapped_blocks, max_blocks); 328 | 329 | /* Treat newly added block / cluster */ 330 | if (iblock < last_block) 331 | create = 0; 332 | 333 | if (create || buffer_delay(bh_result)) { 334 | pos = EXFAT_BLK_TO_B((iblock + 1), sb); 335 | if (ei->i_size_ondisk < pos) 336 | ei->i_size_ondisk = pos; 337 | } 338 | 339 | if (create) { 340 | err = exfat_map_new_buffer(ei, bh_result, pos); 341 | if (err) { 342 | exfat_fs_error(sb, 343 | "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n", 344 | pos, ei->i_size_aligned); 345 | goto unlock_ret; 346 | } 347 | } 348 | 349 | if (buffer_delay(bh_result)) 350 | clear_buffer_delay(bh_result); 351 | map_bh(bh_result, sb, phys); 352 | done: 353 | bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb); 354 | unlock_ret: 355 | mutex_unlock(&sbi->s_lock); 356 | return err; 357 | } 358 | 359 | static int exfat_readpage(struct file *file, struct page *page) 360 | { 361 | return mpage_readpage(page, exfat_get_block); 362 | } 363 | 364 | static int exfat_readpages(struct file *file, struct address_space *mapping, 365 | struct list_head *pages, unsigned int nr_pages) 366 | { 367 | return mpage_readpages(mapping, pages, nr_pages, exfat_get_block); 368 | } 369 | 370 | static int exfat_writepage(struct page *page, struct writeback_control *wbc) 371 | { 372 | return block_write_full_page(page, exfat_get_block, wbc); 373 | } 374 | 375 | static int exfat_writepages(struct address_space *mapping, 376 | struct writeback_control *wbc) 377 | { 378 | return mpage_writepages(mapping, wbc, exfat_get_block); 379 | } 380 | 381 | static void exfat_write_failed(struct address_space *mapping, loff_t to) 382 | { 383 | struct inode *inode = mapping->host; 384 | 385 | if (to > i_size_read(inode)) { 386 | truncate_pagecache(inode, i_size_read(inode)); 387 | exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned); 388 | } 389 | } 390 | 391 | static int exfat_write_begin(struct file *file, struct address_space *mapping, 392 | loff_t pos, unsigned int len, unsigned int flags, 393 | struct page **pagep, void **fsdata) 394 | { 395 | int ret; 396 | 397 | *pagep = NULL; 398 | ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 399 | exfat_get_block, 400 | &EXFAT_I(mapping->host)->i_size_ondisk); 401 | 402 | if (ret < 0) 403 | exfat_write_failed(mapping, pos+len); 404 | 405 | return ret; 406 | } 407 | 408 | static int exfat_write_end(struct file *file, struct address_space *mapping, 409 | loff_t pos, unsigned int len, unsigned int copied, 410 | struct page *pagep, void *fsdata) 411 | { 412 | struct inode *inode = mapping->host; 413 | struct exfat_inode_info *ei = EXFAT_I(inode); 414 | int err; 415 | 416 | err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); 417 | 418 | if (EXFAT_I(inode)->i_size_aligned < i_size_read(inode)) { 419 | exfat_fs_error(inode->i_sb, 420 | "invalid size(size(%llu) > aligned(%llu)\n", 421 | i_size_read(inode), EXFAT_I(inode)->i_size_aligned); 422 | return -EIO; 423 | } 424 | 425 | if (err < len) 426 | exfat_write_failed(mapping, pos+len); 427 | 428 | if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) { 429 | inode->i_mtime = inode->i_ctime = current_time(inode); 430 | ei->attr |= ATTR_ARCHIVE; 431 | mark_inode_dirty(inode); 432 | } 433 | 434 | return err; 435 | } 436 | 437 | static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 438 | { 439 | struct address_space *mapping = iocb->ki_filp->f_mapping; 440 | struct inode *inode = mapping->host; 441 | loff_t size = iocb->ki_pos + iov_iter_count(iter); 442 | int rw = iov_iter_rw(iter); 443 | ssize_t ret; 444 | 445 | if (rw == WRITE) { 446 | /* 447 | * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), 448 | * so we need to update the ->i_size_aligned to block boundary. 449 | * 450 | * But we must fill the remaining area or hole by nul for 451 | * updating ->i_size_aligned 452 | * 453 | * Return 0, and fallback to normal buffered write. 454 | */ 455 | if (EXFAT_I(inode)->i_size_aligned < size) 456 | return 0; 457 | } 458 | 459 | /* 460 | * Need to use the DIO_LOCKING for avoiding the race 461 | * condition of exfat_get_block() and ->truncate(). 462 | */ 463 | ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block); 464 | if (ret < 0 && (rw & WRITE)) 465 | exfat_write_failed(mapping, size); 466 | return ret; 467 | } 468 | 469 | static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block) 470 | { 471 | sector_t blocknr; 472 | 473 | /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */ 474 | down_read(&EXFAT_I(mapping->host)->truncate_lock); 475 | blocknr = generic_block_bmap(mapping, block, exfat_get_block); 476 | up_read(&EXFAT_I(mapping->host)->truncate_lock); 477 | return blocknr; 478 | } 479 | 480 | /* 481 | * exfat_block_truncate_page() zeroes out a mapping from file offset `from' 482 | * up to the end of the block which corresponds to `from'. 483 | * This is required during truncate to physically zeroout the tail end 484 | * of that block so it doesn't yield old data if the file is later grown. 485 | * Also, avoid causing failure from fsx for cases of "data past EOF" 486 | */ 487 | int exfat_block_truncate_page(struct inode *inode, loff_t from) 488 | { 489 | return block_truncate_page(inode->i_mapping, from, exfat_get_block); 490 | } 491 | 492 | static const struct address_space_operations exfat_aops = { 493 | .readpage = exfat_readpage, 494 | .readpages = exfat_readpages, 495 | .writepage = exfat_writepage, 496 | .writepages = exfat_writepages, 497 | .write_begin = exfat_write_begin, 498 | .write_end = exfat_write_end, 499 | .direct_IO = exfat_direct_IO, 500 | .bmap = exfat_aop_bmap 501 | }; 502 | 503 | static inline unsigned long exfat_hash(loff_t i_pos) 504 | { 505 | return hash_32(i_pos, EXFAT_HASH_BITS); 506 | } 507 | 508 | void exfat_hash_inode(struct inode *inode, loff_t i_pos) 509 | { 510 | struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 511 | struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 512 | 513 | spin_lock(&sbi->inode_hash_lock); 514 | EXFAT_I(inode)->i_pos = i_pos; 515 | hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head); 516 | spin_unlock(&sbi->inode_hash_lock); 517 | } 518 | 519 | void exfat_unhash_inode(struct inode *inode) 520 | { 521 | struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 522 | 523 | spin_lock(&sbi->inode_hash_lock); 524 | hlist_del_init(&EXFAT_I(inode)->i_hash_fat); 525 | EXFAT_I(inode)->i_pos = 0; 526 | spin_unlock(&sbi->inode_hash_lock); 527 | } 528 | 529 | struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) 530 | { 531 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 532 | struct exfat_inode_info *info; 533 | struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 534 | struct inode *inode = NULL; 535 | 536 | spin_lock(&sbi->inode_hash_lock); 537 | hlist_for_each_entry(info, head, i_hash_fat) { 538 | WARN_ON(info->vfs_inode.i_sb != sb); 539 | 540 | if (i_pos != info->i_pos) 541 | continue; 542 | inode = igrab(&info->vfs_inode); 543 | if (inode) 544 | break; 545 | } 546 | spin_unlock(&sbi->inode_hash_lock); 547 | return inode; 548 | } 549 | 550 | /* doesn't deal with root inode */ 551 | static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info) 552 | { 553 | struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 554 | struct exfat_inode_info *ei = EXFAT_I(inode); 555 | loff_t size = info->size; 556 | 557 | memcpy(&ei->dir, &info->dir, sizeof(struct exfat_chain)); 558 | ei->entry = info->entry; 559 | ei->attr = info->attr; 560 | ei->start_clu = info->start_clu; 561 | ei->flags = info->flags; 562 | ei->type = info->type; 563 | 564 | ei->version = 0; 565 | ei->hint_stat.eidx = 0; 566 | ei->hint_stat.clu = info->start_clu; 567 | ei->hint_femp.eidx = EXFAT_HINT_NONE; 568 | ei->rwoffset = 0; 569 | ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 570 | ei->i_pos = 0; 571 | 572 | inode->i_uid = sbi->options.fs_uid; 573 | inode->i_gid = sbi->options.fs_gid; 574 | inode_inc_iversion(inode); 575 | inode->i_generation = prandom_u32(); 576 | 577 | if (info->attr & ATTR_SUBDIR) { /* directory */ 578 | inode->i_generation &= ~1; 579 | inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 580 | inode->i_op = &exfat_dir_inode_operations; 581 | inode->i_fop = &exfat_dir_operations; 582 | set_nlink(inode, info->num_subdirs); 583 | } else { /* regular file */ 584 | inode->i_generation |= 1; 585 | inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 586 | inode->i_op = &exfat_file_inode_operations; 587 | inode->i_fop = &exfat_file_operations; 588 | inode->i_mapping->a_ops = &exfat_aops; 589 | inode->i_mapping->nrpages = 0; 590 | } 591 | 592 | i_size_write(inode, size); 593 | 594 | /* ondisk and aligned size should be aligned with block size */ 595 | if (size & (inode->i_sb->s_blocksize - 1)) { 596 | size |= (inode->i_sb->s_blocksize - 1); 597 | size++; 598 | } 599 | 600 | ei->i_size_aligned = size; 601 | ei->i_size_ondisk = size; 602 | 603 | exfat_save_attr(inode, info->attr); 604 | 605 | inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) & 606 | ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 607 | inode->i_mtime = info->mtime; 608 | inode->i_ctime = info->mtime; 609 | ei->i_crtime = info->crtime; 610 | inode->i_atime = info->atime; 611 | 612 | exfat_cache_init_inode(inode); 613 | 614 | return 0; 615 | } 616 | 617 | struct inode *exfat_build_inode(struct super_block *sb, 618 | struct exfat_dir_entry *info, loff_t i_pos) 619 | { 620 | struct inode *inode; 621 | int err; 622 | 623 | inode = exfat_iget(sb, i_pos); 624 | if (inode) 625 | goto out; 626 | inode = new_inode(sb); 627 | if (!inode) { 628 | inode = ERR_PTR(-ENOMEM); 629 | goto out; 630 | } 631 | inode->i_ino = iunique(sb, EXFAT_ROOT_INO); 632 | inode_set_iversion(inode, 1); 633 | err = exfat_fill_inode(inode, info); 634 | if (err) { 635 | iput(inode); 636 | inode = ERR_PTR(err); 637 | goto out; 638 | } 639 | exfat_hash_inode(inode, i_pos); 640 | insert_inode_hash(inode); 641 | out: 642 | return inode; 643 | } 644 | 645 | void exfat_evict_inode(struct inode *inode) 646 | { 647 | truncate_inode_pages(&inode->i_data, 0); 648 | 649 | if (!inode->i_nlink) { 650 | i_size_write(inode, 0); 651 | mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 652 | __exfat_truncate(inode, 0); 653 | mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 654 | } 655 | 656 | invalidate_inode_buffers(inode); 657 | clear_inode(inode); 658 | exfat_cache_inval_inode(inode); 659 | exfat_unhash_inode(inode); 660 | } 661 | -------------------------------------------------------------------------------- /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 10 | #include 11 | #include 12 | #include 13 | 14 | #include "exfat_fs.h" 15 | 16 | /* 17 | * exfat_fs_error reports a file system problem that might indicate fa data 18 | * corruption/inconsistency. Depending on 'errors' mount option the 19 | * panic() is called, or error message is printed FAT and nothing is done, 20 | * or filesystem is remounted read-only (default behavior). 21 | * In case the file system is remounted read-only, it can be made writable 22 | * again by remounting it. 23 | */ 24 | void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...) 25 | { 26 | struct exfat_mount_options *opts = &EXFAT_SB(sb)->options; 27 | va_list args; 28 | struct va_format vaf; 29 | 30 | if (report) { 31 | va_start(args, fmt); 32 | vaf.fmt = fmt; 33 | vaf.va = &args; 34 | exfat_err(sb, "error, %pV", &vaf); 35 | va_end(args); 36 | } 37 | 38 | if (opts->errors == EXFAT_ERRORS_PANIC) { 39 | panic("exFAT-fs (%s): fs panic from previous error\n", 40 | sb->s_id); 41 | } else if (opts->errors == EXFAT_ERRORS_RO && !sb_rdonly(sb)) { 42 | sb->s_flags |= SB_RDONLY; 43 | exfat_err(sb, "Filesystem has been set read-only"); 44 | } 45 | } 46 | 47 | /* 48 | * exfat_msg() - print preformated EXFAT specific messages. 49 | * All logs except what uses exfat_fs_error() should be written by exfat_msg() 50 | */ 51 | void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...) 52 | { 53 | struct va_format vaf; 54 | va_list args; 55 | 56 | va_start(args, fmt); 57 | vaf.fmt = fmt; 58 | vaf.va = &args; 59 | /* level means KERN_ pacility level */ 60 | printk("%sexFAT-fs (%s): %pV\n", level, sb->s_id, &vaf); 61 | va_end(args); 62 | } 63 | 64 | #define SECS_PER_MIN (60) 65 | #define TIMEZONE_SEC(x) ((x) * 15 * SECS_PER_MIN) 66 | 67 | static void exfat_adjust_tz(struct timespec64 *ts, u8 tz_off) 68 | { 69 | if (tz_off <= 0x3F) 70 | ts->tv_sec -= TIMEZONE_SEC(tz_off); 71 | else /* 0x40 <= (tz_off & 0x7F) <=0x7F */ 72 | ts->tv_sec += TIMEZONE_SEC(0x80 - tz_off); 73 | } 74 | 75 | /* Convert a EXFAT time/date pair to a UNIX date (seconds since 1 1 70). */ 76 | void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, 77 | u8 tz, __le16 time, __le16 date, u8 time_cs) 78 | { 79 | u16 t = le16_to_cpu(time); 80 | u16 d = le16_to_cpu(date); 81 | 82 | ts->tv_sec = mktime64(1980 + (d >> 9), d >> 5 & 0x000F, d & 0x001F, 83 | t >> 11, (t >> 5) & 0x003F, (t & 0x001F) << 1); 84 | 85 | 86 | /* time_cs field represent 0 ~ 199cs(1990 ms) */ 87 | if (time_cs) { 88 | ts->tv_sec += time_cs / 100; 89 | ts->tv_nsec = (time_cs % 100) * 10 * NSEC_PER_MSEC; 90 | } else 91 | ts->tv_nsec = 0; 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_cs) 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_cs field represent 0 ~ 199cs(1990 ms) */ 116 | if (time_cs) 117 | *time_cs = (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 | /* 128 | * The timestamp for access_time has double seconds granularity. 129 | * (There is no 10msIncrement field for access_time unlike create/modify_time) 130 | * atime also has only a 2-second resolution. 131 | */ 132 | void exfat_truncate_atime(struct timespec64 *ts) 133 | { 134 | ts->tv_sec = round_down(ts->tv_sec, 2); 135 | ts->tv_nsec = 0; 136 | } 137 | 138 | u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type) 139 | { 140 | int i; 141 | u8 *c = (u8 *)data; 142 | 143 | for (i = 0; i < len; i++, c++) { 144 | if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3))) 145 | continue; 146 | chksum = ((chksum << 15) | (chksum >> 1)) + *c; 147 | } 148 | return chksum; 149 | } 150 | 151 | u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type) 152 | { 153 | int i; 154 | u8 *c = (u8 *)data; 155 | 156 | for (i = 0; i < len; i++, c++) { 157 | if (unlikely(type == CS_BOOT_SECTOR && 158 | (i == 106 || i == 107 || i == 112))) 159 | continue; 160 | chksum = ((chksum << 31) | (chksum >> 1)) + *c; 161 | } 162 | return chksum; 163 | } 164 | 165 | void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync) 166 | { 167 | set_bit(EXFAT_SB_DIRTY, &EXFAT_SB(sb)->s_state); 168 | set_buffer_uptodate(bh); 169 | mark_buffer_dirty(bh); 170 | 171 | if (sync) 172 | sync_dirty_buffer(bh); 173 | } 174 | 175 | void exfat_chain_set(struct exfat_chain *ec, unsigned int dir, 176 | unsigned int size, unsigned char flags) 177 | { 178 | ec->dir = dir; 179 | ec->size = size; 180 | ec->flags = flags; 181 | } 182 | 183 | void exfat_chain_dup(struct exfat_chain *dup, struct exfat_chain *ec) 184 | { 185 | return exfat_chain_set(dup, ec->dir, ec->size, ec->flags); 186 | } 187 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | #include 10 | 11 | #include "exfat_fs.h" 12 | 13 | /* Upcase tabel macro */ 14 | #define EXFAT_NUM_UPCASE (2918) 15 | #define UTBL_COUNT (0x10000) 16 | 17 | /* 18 | * Upcase table in compressed format (7.2.5.1 Recommended Up-case Table 19 | * in exfat specification, See: 20 | * https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification). 21 | */ 22 | static const unsigned short uni_def_upcase[EXFAT_NUM_UPCASE] = { 23 | 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 24 | 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 25 | 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 26 | 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, 27 | 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 28 | 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f, 29 | 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 30 | 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, 31 | 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 32 | 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 33 | 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 34 | 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 35 | 0x0060, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 36 | 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 37 | 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 38 | 0x0058, 0x0059, 0x005a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f, 39 | 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 40 | 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, 41 | 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 42 | 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, 43 | 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 44 | 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, 45 | 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, 46 | 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, 47 | 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 48 | 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, 49 | 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, 50 | 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, 51 | 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, 52 | 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, 53 | 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00f7, 54 | 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x0178, 55 | 0x0100, 0x0100, 0x0102, 0x0102, 0x0104, 0x0104, 0x0106, 0x0106, 56 | 0x0108, 0x0108, 0x010a, 0x010a, 0x010c, 0x010c, 0x010e, 0x010e, 57 | 0x0110, 0x0110, 0x0112, 0x0112, 0x0114, 0x0114, 0x0116, 0x0116, 58 | 0x0118, 0x0118, 0x011a, 0x011a, 0x011c, 0x011c, 0x011e, 0x011e, 59 | 0x0120, 0x0120, 0x0122, 0x0122, 0x0124, 0x0124, 0x0126, 0x0126, 60 | 0x0128, 0x0128, 0x012a, 0x012a, 0x012c, 0x012c, 0x012e, 0x012e, 61 | 0x0130, 0x0131, 0x0132, 0x0132, 0x0134, 0x0134, 0x0136, 0x0136, 62 | 0x0138, 0x0139, 0x0139, 0x013b, 0x013b, 0x013d, 0x013d, 0x013f, 63 | 0x013f, 0x0141, 0x0141, 0x0143, 0x0143, 0x0145, 0x0145, 0x0147, 64 | 0x0147, 0x0149, 0x014a, 0x014a, 0x014c, 0x014c, 0x014e, 0x014e, 65 | 0x0150, 0x0150, 0x0152, 0x0152, 0x0154, 0x0154, 0x0156, 0x0156, 66 | 0x0158, 0x0158, 0x015a, 0x015a, 0x015c, 0x015c, 0x015e, 0x015e, 67 | 0x0160, 0x0160, 0x0162, 0x0162, 0x0164, 0x0164, 0x0166, 0x0166, 68 | 0x0168, 0x0168, 0x016a, 0x016a, 0x016c, 0x016c, 0x016e, 0x016e, 69 | 0x0170, 0x0170, 0x0172, 0x0172, 0x0174, 0x0174, 0x0176, 0x0176, 70 | 0x0178, 0x0179, 0x0179, 0x017b, 0x017b, 0x017d, 0x017d, 0x017f, 71 | 0x0243, 0x0181, 0x0182, 0x0182, 0x0184, 0x0184, 0x0186, 0x0187, 72 | 0x0187, 0x0189, 0x018a, 0x018b, 0x018b, 0x018d, 0x018e, 0x018f, 73 | 0x0190, 0x0191, 0x0191, 0x0193, 0x0194, 0x01f6, 0x0196, 0x0197, 74 | 0x0198, 0x0198, 0x023d, 0x019b, 0x019c, 0x019d, 0x0220, 0x019f, 75 | 0x01a0, 0x01a0, 0x01a2, 0x01a2, 0x01a4, 0x01a4, 0x01a6, 0x01a7, 76 | 0x01a7, 0x01a9, 0x01aa, 0x01ab, 0x01ac, 0x01ac, 0x01ae, 0x01af, 77 | 0x01af, 0x01b1, 0x01b2, 0x01b3, 0x01b3, 0x01b5, 0x01b5, 0x01b7, 78 | 0x01b8, 0x01b8, 0x01ba, 0x01bb, 0x01bc, 0x01bc, 0x01be, 0x01f7, 79 | 0x01c0, 0x01c1, 0x01c2, 0x01c3, 0x01c4, 0x01c5, 0x01c4, 0x01c7, 80 | 0x01c8, 0x01c7, 0x01ca, 0x01cb, 0x01ca, 0x01cd, 0x01cd, 0x01cf, 81 | 0x01cf, 0x01d1, 0x01d1, 0x01d3, 0x01d3, 0x01d5, 0x01d5, 0x01d7, 82 | 0x01d7, 0x01d9, 0x01d9, 0x01db, 0x01db, 0x018e, 0x01de, 0x01de, 83 | 0x01e0, 0x01e0, 0x01e2, 0x01e2, 0x01e4, 0x01e4, 0x01e6, 0x01e6, 84 | 0x01e8, 0x01e8, 0x01ea, 0x01ea, 0x01ec, 0x01ec, 0x01ee, 0x01ee, 85 | 0x01f0, 0x01f1, 0x01f2, 0x01f1, 0x01f4, 0x01f4, 0x01f6, 0x01f7, 86 | 0x01f8, 0x01f8, 0x01fa, 0x01fa, 0x01fc, 0x01fc, 0x01fe, 0x01fe, 87 | 0x0200, 0x0200, 0x0202, 0x0202, 0x0204, 0x0204, 0x0206, 0x0206, 88 | 0x0208, 0x0208, 0x020a, 0x020a, 0x020c, 0x020c, 0x020e, 0x020e, 89 | 0x0210, 0x0210, 0x0212, 0x0212, 0x0214, 0x0214, 0x0216, 0x0216, 90 | 0x0218, 0x0218, 0x021a, 0x021a, 0x021c, 0x021c, 0x021e, 0x021e, 91 | 0x0220, 0x0221, 0x0222, 0x0222, 0x0224, 0x0224, 0x0226, 0x0226, 92 | 0x0228, 0x0228, 0x022a, 0x022a, 0x022c, 0x022c, 0x022e, 0x022e, 93 | 0x0230, 0x0230, 0x0232, 0x0232, 0x0234, 0x0235, 0x0236, 0x0237, 94 | 0x0238, 0x0239, 0x2c65, 0x023b, 0x023b, 0x023d, 0x2c66, 0x023f, 95 | 0x0240, 0x0241, 0x0241, 0x0243, 0x0244, 0x0245, 0x0246, 0x0246, 96 | 0x0248, 0x0248, 0x024a, 0x024a, 0x024c, 0x024c, 0x024e, 0x024e, 97 | 0x0250, 0x0251, 0x0252, 0x0181, 0x0186, 0x0255, 0x0189, 0x018a, 98 | 0x0258, 0x018f, 0x025a, 0x0190, 0x025c, 0x025d, 0x025e, 0x025f, 99 | 0x0193, 0x0261, 0x0262, 0x0194, 0x0264, 0x0265, 0x0266, 0x0267, 100 | 0x0197, 0x0196, 0x026a, 0x2c62, 0x026c, 0x026d, 0x026e, 0x019c, 101 | 0x0270, 0x0271, 0x019d, 0x0273, 0x0274, 0x019f, 0x0276, 0x0277, 102 | 0x0278, 0x0279, 0x027a, 0x027b, 0x027c, 0x2c64, 0x027e, 0x027f, 103 | 0x01a6, 0x0281, 0x0282, 0x01a9, 0x0284, 0x0285, 0x0286, 0x0287, 104 | 0x01ae, 0x0244, 0x01b1, 0x01b2, 0x0245, 0x028d, 0x028e, 0x028f, 105 | 0x0290, 0x0291, 0x01b7, 0x0293, 0x0294, 0x0295, 0x0296, 0x0297, 106 | 0x0298, 0x0299, 0x029a, 0x029b, 0x029c, 0x029d, 0x029e, 0x029f, 107 | 0x02a0, 0x02a1, 0x02a2, 0x02a3, 0x02a4, 0x02a5, 0x02a6, 0x02a7, 108 | 0x02a8, 0x02a9, 0x02aa, 0x02ab, 0x02ac, 0x02ad, 0x02ae, 0x02af, 109 | 0x02b0, 0x02b1, 0x02b2, 0x02b3, 0x02b4, 0x02b5, 0x02b6, 0x02b7, 110 | 0x02b8, 0x02b9, 0x02ba, 0x02bb, 0x02bc, 0x02bd, 0x02be, 0x02bf, 111 | 0x02c0, 0x02c1, 0x02c2, 0x02c3, 0x02c4, 0x02c5, 0x02c6, 0x02c7, 112 | 0x02c8, 0x02c9, 0x02ca, 0x02cb, 0x02cc, 0x02cd, 0x02ce, 0x02cf, 113 | 0x02d0, 0x02d1, 0x02d2, 0x02d3, 0x02d4, 0x02d5, 0x02d6, 0x02d7, 114 | 0x02d8, 0x02d9, 0x02da, 0x02db, 0x02dc, 0x02dd, 0x02de, 0x02df, 115 | 0x02e0, 0x02e1, 0x02e2, 0x02e3, 0x02e4, 0x02e5, 0x02e6, 0x02e7, 116 | 0x02e8, 0x02e9, 0x02ea, 0x02eb, 0x02ec, 0x02ed, 0x02ee, 0x02ef, 117 | 0x02f0, 0x02f1, 0x02f2, 0x02f3, 0x02f4, 0x02f5, 0x02f6, 0x02f7, 118 | 0x02f8, 0x02f9, 0x02fa, 0x02fb, 0x02fc, 0x02fd, 0x02fe, 0x02ff, 119 | 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 120 | 0x0308, 0x0309, 0x030a, 0x030b, 0x030c, 0x030d, 0x030e, 0x030f, 121 | 0x0310, 0x0311, 0x0312, 0x0313, 0x0314, 0x0315, 0x0316, 0x0317, 122 | 0x0318, 0x0319, 0x031a, 0x031b, 0x031c, 0x031d, 0x031e, 0x031f, 123 | 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326, 0x0327, 124 | 0x0328, 0x0329, 0x032a, 0x032b, 0x032c, 0x032d, 0x032e, 0x032f, 125 | 0x0330, 0x0331, 0x0332, 0x0333, 0x0334, 0x0335, 0x0336, 0x0337, 126 | 0x0338, 0x0339, 0x033a, 0x033b, 0x033c, 0x033d, 0x033e, 0x033f, 127 | 0x0340, 0x0341, 0x0342, 0x0343, 0x0344, 0x0345, 0x0346, 0x0347, 128 | 0x0348, 0x0349, 0x034a, 0x034b, 0x034c, 0x034d, 0x034e, 0x034f, 129 | 0x0350, 0x0351, 0x0352, 0x0353, 0x0354, 0x0355, 0x0356, 0x0357, 130 | 0x0358, 0x0359, 0x035a, 0x035b, 0x035c, 0x035d, 0x035e, 0x035f, 131 | 0x0360, 0x0361, 0x0362, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367, 132 | 0x0368, 0x0369, 0x036a, 0x036b, 0x036c, 0x036d, 0x036e, 0x036f, 133 | 0x0370, 0x0371, 0x0372, 0x0373, 0x0374, 0x0375, 0x0376, 0x0377, 134 | 0x0378, 0x0379, 0x037a, 0x03fd, 0x03fe, 0x03ff, 0x037e, 0x037f, 135 | 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386, 0x0387, 136 | 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e, 0x038f, 137 | 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 138 | 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, 139 | 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, 140 | 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x0386, 0x0388, 0x0389, 0x038a, 141 | 0x03b0, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 142 | 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, 143 | 0x03a0, 0x03a1, 0x03a3, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, 144 | 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x038c, 0x038e, 0x038f, 0x03cf, 145 | 0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7, 146 | 0x03d8, 0x03d8, 0x03da, 0x03da, 0x03dc, 0x03dc, 0x03de, 0x03de, 147 | 0x03e0, 0x03e0, 0x03e2, 0x03e2, 0x03e4, 0x03e4, 0x03e6, 0x03e6, 148 | 0x03e8, 0x03e8, 0x03ea, 0x03ea, 0x03ec, 0x03ec, 0x03ee, 0x03ee, 149 | 0x03f0, 0x03f1, 0x03f9, 0x03f3, 0x03f4, 0x03f5, 0x03f6, 0x03f7, 150 | 0x03f7, 0x03f9, 0x03fa, 0x03fa, 0x03fc, 0x03fd, 0x03fe, 0x03ff, 151 | 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 152 | 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x040d, 0x040e, 0x040f, 153 | 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 154 | 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, 155 | 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 156 | 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, 157 | 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 158 | 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, 159 | 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 160 | 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, 161 | 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 162 | 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x040d, 0x040e, 0x040f, 163 | 0x0460, 0x0460, 0x0462, 0x0462, 0x0464, 0x0464, 0x0466, 0x0466, 164 | 0x0468, 0x0468, 0x046a, 0x046a, 0x046c, 0x046c, 0x046e, 0x046e, 165 | 0x0470, 0x0470, 0x0472, 0x0472, 0x0474, 0x0474, 0x0476, 0x0476, 166 | 0x0478, 0x0478, 0x047a, 0x047a, 0x047c, 0x047c, 0x047e, 0x047e, 167 | 0x0480, 0x0480, 0x0482, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487, 168 | 0x0488, 0x0489, 0x048a, 0x048a, 0x048c, 0x048c, 0x048e, 0x048e, 169 | 0x0490, 0x0490, 0x0492, 0x0492, 0x0494, 0x0494, 0x0496, 0x0496, 170 | 0x0498, 0x0498, 0x049a, 0x049a, 0x049c, 0x049c, 0x049e, 0x049e, 171 | 0x04a0, 0x04a0, 0x04a2, 0x04a2, 0x04a4, 0x04a4, 0x04a6, 0x04a6, 172 | 0x04a8, 0x04a8, 0x04aa, 0x04aa, 0x04ac, 0x04ac, 0x04ae, 0x04ae, 173 | 0x04b0, 0x04b0, 0x04b2, 0x04b2, 0x04b4, 0x04b4, 0x04b6, 0x04b6, 174 | 0x04b8, 0x04b8, 0x04ba, 0x04ba, 0x04bc, 0x04bc, 0x04be, 0x04be, 175 | 0x04c0, 0x04c1, 0x04c1, 0x04c3, 0x04c3, 0x04c5, 0x04c5, 0x04c7, 176 | 0x04c7, 0x04c9, 0x04c9, 0x04cb, 0x04cb, 0x04cd, 0x04cd, 0x04c0, 177 | 0x04d0, 0x04d0, 0x04d2, 0x04d2, 0x04d4, 0x04d4, 0x04d6, 0x04d6, 178 | 0x04d8, 0x04d8, 0x04da, 0x04da, 0x04dc, 0x04dc, 0x04de, 0x04de, 179 | 0x04e0, 0x04e0, 0x04e2, 0x04e2, 0x04e4, 0x04e4, 0x04e6, 0x04e6, 180 | 0x04e8, 0x04e8, 0x04ea, 0x04ea, 0x04ec, 0x04ec, 0x04ee, 0x04ee, 181 | 0x04f0, 0x04f0, 0x04f2, 0x04f2, 0x04f4, 0x04f4, 0x04f6, 0x04f6, 182 | 0x04f8, 0x04f8, 0x04fa, 0x04fa, 0x04fc, 0x04fc, 0x04fe, 0x04fe, 183 | 0x0500, 0x0500, 0x0502, 0x0502, 0x0504, 0x0504, 0x0506, 0x0506, 184 | 0x0508, 0x0508, 0x050a, 0x050a, 0x050c, 0x050c, 0x050e, 0x050e, 185 | 0x0510, 0x0510, 0x0512, 0x0512, 0x0514, 0x0515, 0x0516, 0x0517, 186 | 0x0518, 0x0519, 0x051a, 0x051b, 0x051c, 0x051d, 0x051e, 0x051f, 187 | 0x0520, 0x0521, 0x0522, 0x0523, 0x0524, 0x0525, 0x0526, 0x0527, 188 | 0x0528, 0x0529, 0x052a, 0x052b, 0x052c, 0x052d, 0x052e, 0x052f, 189 | 0x0530, 0x0531, 0x0532, 0x0533, 0x0534, 0x0535, 0x0536, 0x0537, 190 | 0x0538, 0x0539, 0x053a, 0x053b, 0x053c, 0x053d, 0x053e, 0x053f, 191 | 0x0540, 0x0541, 0x0542, 0x0543, 0x0544, 0x0545, 0x0546, 0x0547, 192 | 0x0548, 0x0549, 0x054a, 0x054b, 0x054c, 0x054d, 0x054e, 0x054f, 193 | 0x0550, 0x0551, 0x0552, 0x0553, 0x0554, 0x0555, 0x0556, 0x0557, 194 | 0x0558, 0x0559, 0x055a, 0x055b, 0x055c, 0x055d, 0x055e, 0x055f, 195 | 0x0560, 0x0531, 0x0532, 0x0533, 0x0534, 0x0535, 0x0536, 0x0537, 196 | 0x0538, 0x0539, 0x053a, 0x053b, 0x053c, 0x053d, 0x053e, 0x053f, 197 | 0x0540, 0x0541, 0x0542, 0x0543, 0x0544, 0x0545, 0x0546, 0x0547, 198 | 0x0548, 0x0549, 0x054a, 0x054b, 0x054c, 0x054d, 0x054e, 0x054f, 199 | 0x0550, 0x0551, 0x0552, 0x0553, 0x0554, 0x0555, 0x0556, 0xffff, 200 | 0x17f6, 0x2c63, 0x1d7e, 0x1d7f, 0x1d80, 0x1d81, 0x1d82, 0x1d83, 201 | 0x1d84, 0x1d85, 0x1d86, 0x1d87, 0x1d88, 0x1d89, 0x1d8a, 0x1d8b, 202 | 0x1d8c, 0x1d8d, 0x1d8e, 0x1d8f, 0x1d90, 0x1d91, 0x1d92, 0x1d93, 203 | 0x1d94, 0x1d95, 0x1d96, 0x1d97, 0x1d98, 0x1d99, 0x1d9a, 0x1d9b, 204 | 0x1d9c, 0x1d9d, 0x1d9e, 0x1d9f, 0x1da0, 0x1da1, 0x1da2, 0x1da3, 205 | 0x1da4, 0x1da5, 0x1da6, 0x1da7, 0x1da8, 0x1da9, 0x1daa, 0x1dab, 206 | 0x1dac, 0x1dad, 0x1dae, 0x1daf, 0x1db0, 0x1db1, 0x1db2, 0x1db3, 207 | 0x1db4, 0x1db5, 0x1db6, 0x1db7, 0x1db8, 0x1db9, 0x1dba, 0x1dbb, 208 | 0x1dbc, 0x1dbd, 0x1dbe, 0x1dbf, 0x1dc0, 0x1dc1, 0x1dc2, 0x1dc3, 209 | 0x1dc4, 0x1dc5, 0x1dc6, 0x1dc7, 0x1dc8, 0x1dc9, 0x1dca, 0x1dcb, 210 | 0x1dcc, 0x1dcd, 0x1dce, 0x1dcf, 0x1dd0, 0x1dd1, 0x1dd2, 0x1dd3, 211 | 0x1dd4, 0x1dd5, 0x1dd6, 0x1dd7, 0x1dd8, 0x1dd9, 0x1dda, 0x1ddb, 212 | 0x1ddc, 0x1ddd, 0x1dde, 0x1ddf, 0x1de0, 0x1de1, 0x1de2, 0x1de3, 213 | 0x1de4, 0x1de5, 0x1de6, 0x1de7, 0x1de8, 0x1de9, 0x1dea, 0x1deb, 214 | 0x1dec, 0x1ded, 0x1dee, 0x1def, 0x1df0, 0x1df1, 0x1df2, 0x1df3, 215 | 0x1df4, 0x1df5, 0x1df6, 0x1df7, 0x1df8, 0x1df9, 0x1dfa, 0x1dfb, 216 | 0x1dfc, 0x1dfd, 0x1dfe, 0x1dff, 0x1e00, 0x1e00, 0x1e02, 0x1e02, 217 | 0x1e04, 0x1e04, 0x1e06, 0x1e06, 0x1e08, 0x1e08, 0x1e0a, 0x1e0a, 218 | 0x1e0c, 0x1e0c, 0x1e0e, 0x1e0e, 0x1e10, 0x1e10, 0x1e12, 0x1e12, 219 | 0x1e14, 0x1e14, 0x1e16, 0x1e16, 0x1e18, 0x1e18, 0x1e1a, 0x1e1a, 220 | 0x1e1c, 0x1e1c, 0x1e1e, 0x1e1e, 0x1e20, 0x1e20, 0x1e22, 0x1e22, 221 | 0x1e24, 0x1e24, 0x1e26, 0x1e26, 0x1e28, 0x1e28, 0x1e2a, 0x1e2a, 222 | 0x1e2c, 0x1e2c, 0x1e2e, 0x1e2e, 0x1e30, 0x1e30, 0x1e32, 0x1e32, 223 | 0x1e34, 0x1e34, 0x1e36, 0x1e36, 0x1e38, 0x1e38, 0x1e3a, 0x1e3a, 224 | 0x1e3c, 0x1e3c, 0x1e3e, 0x1e3e, 0x1e40, 0x1e40, 0x1e42, 0x1e42, 225 | 0x1e44, 0x1e44, 0x1e46, 0x1e46, 0x1e48, 0x1e48, 0x1e4a, 0x1e4a, 226 | 0x1e4c, 0x1e4c, 0x1e4e, 0x1e4e, 0x1e50, 0x1e50, 0x1e52, 0x1e52, 227 | 0x1e54, 0x1e54, 0x1e56, 0x1e56, 0x1e58, 0x1e58, 0x1e5a, 0x1e5a, 228 | 0x1e5c, 0x1e5c, 0x1e5e, 0x1e5e, 0x1e60, 0x1e60, 0x1e62, 0x1e62, 229 | 0x1e64, 0x1e64, 0x1e66, 0x1e66, 0x1e68, 0x1e68, 0x1e6a, 0x1e6a, 230 | 0x1e6c, 0x1e6c, 0x1e6e, 0x1e6e, 0x1e70, 0x1e70, 0x1e72, 0x1e72, 231 | 0x1e74, 0x1e74, 0x1e76, 0x1e76, 0x1e78, 0x1e78, 0x1e7a, 0x1e7a, 232 | 0x1e7c, 0x1e7c, 0x1e7e, 0x1e7e, 0x1e80, 0x1e80, 0x1e82, 0x1e82, 233 | 0x1e84, 0x1e84, 0x1e86, 0x1e86, 0x1e88, 0x1e88, 0x1e8a, 0x1e8a, 234 | 0x1e8c, 0x1e8c, 0x1e8e, 0x1e8e, 0x1e90, 0x1e90, 0x1e92, 0x1e92, 235 | 0x1e94, 0x1e94, 0x1e96, 0x1e97, 0x1e98, 0x1e99, 0x1e9a, 0x1e9b, 236 | 0x1e9c, 0x1e9d, 0x1e9e, 0x1e9f, 0x1ea0, 0x1ea0, 0x1ea2, 0x1ea2, 237 | 0x1ea4, 0x1ea4, 0x1ea6, 0x1ea6, 0x1ea8, 0x1ea8, 0x1eaa, 0x1eaa, 238 | 0x1eac, 0x1eac, 0x1eae, 0x1eae, 0x1eb0, 0x1eb0, 0x1eb2, 0x1eb2, 239 | 0x1eb4, 0x1eb4, 0x1eb6, 0x1eb6, 0x1eb8, 0x1eb8, 0x1eba, 0x1eba, 240 | 0x1ebc, 0x1ebc, 0x1ebe, 0x1ebe, 0x1ec0, 0x1ec0, 0x1ec2, 0x1ec2, 241 | 0x1ec4, 0x1ec4, 0x1ec6, 0x1ec6, 0x1ec8, 0x1ec8, 0x1eca, 0x1eca, 242 | 0x1ecc, 0x1ecc, 0x1ece, 0x1ece, 0x1ed0, 0x1ed0, 0x1ed2, 0x1ed2, 243 | 0x1ed4, 0x1ed4, 0x1ed6, 0x1ed6, 0x1ed8, 0x1ed8, 0x1eda, 0x1eda, 244 | 0x1edc, 0x1edc, 0x1ede, 0x1ede, 0x1ee0, 0x1ee0, 0x1ee2, 0x1ee2, 245 | 0x1ee4, 0x1ee4, 0x1ee6, 0x1ee6, 0x1ee8, 0x1ee8, 0x1eea, 0x1eea, 246 | 0x1eec, 0x1eec, 0x1eee, 0x1eee, 0x1ef0, 0x1ef0, 0x1ef2, 0x1ef2, 247 | 0x1ef4, 0x1ef4, 0x1ef6, 0x1ef6, 0x1ef8, 0x1ef8, 0x1efa, 0x1efb, 248 | 0x1efc, 0x1efd, 0x1efe, 0x1eff, 0x1f08, 0x1f09, 0x1f0a, 0x1f0b, 249 | 0x1f0c, 0x1f0d, 0x1f0e, 0x1f0f, 0x1f08, 0x1f09, 0x1f0a, 0x1f0b, 250 | 0x1f0c, 0x1f0d, 0x1f0e, 0x1f0f, 0x1f18, 0x1f19, 0x1f1a, 0x1f1b, 251 | 0x1f1c, 0x1f1d, 0x1f16, 0x1f17, 0x1f18, 0x1f19, 0x1f1a, 0x1f1b, 252 | 0x1f1c, 0x1f1d, 0x1f1e, 0x1f1f, 0x1f28, 0x1f29, 0x1f2a, 0x1f2b, 253 | 0x1f2c, 0x1f2d, 0x1f2e, 0x1f2f, 0x1f28, 0x1f29, 0x1f2a, 0x1f2b, 254 | 0x1f2c, 0x1f2d, 0x1f2e, 0x1f2f, 0x1f38, 0x1f39, 0x1f3a, 0x1f3b, 255 | 0x1f3c, 0x1f3d, 0x1f3e, 0x1f3f, 0x1f38, 0x1f39, 0x1f3a, 0x1f3b, 256 | 0x1f3c, 0x1f3d, 0x1f3e, 0x1f3f, 0x1f48, 0x1f49, 0x1f4a, 0x1f4b, 257 | 0x1f4c, 0x1f4d, 0x1f46, 0x1f47, 0x1f48, 0x1f49, 0x1f4a, 0x1f4b, 258 | 0x1f4c, 0x1f4d, 0x1f4e, 0x1f4f, 0x1f50, 0x1f59, 0x1f52, 0x1f5b, 259 | 0x1f54, 0x1f5d, 0x1f56, 0x1f5f, 0x1f58, 0x1f59, 0x1f5a, 0x1f5b, 260 | 0x1f5c, 0x1f5d, 0x1f5e, 0x1f5f, 0x1f68, 0x1f69, 0x1f6a, 0x1f6b, 261 | 0x1f6c, 0x1f6d, 0x1f6e, 0x1f6f, 0x1f68, 0x1f69, 0x1f6a, 0x1f6b, 262 | 0x1f6c, 0x1f6d, 0x1f6e, 0x1f6f, 0x1fba, 0x1fbb, 0x1fc8, 0x1fc9, 263 | 0x1fca, 0x1fcb, 0x1fda, 0x1fdb, 0x1ff8, 0x1ff9, 0x1fea, 0x1feb, 264 | 0x1ffa, 0x1ffb, 0x1f7e, 0x1f7f, 0x1f88, 0x1f89, 0x1f8a, 0x1f8b, 265 | 0x1f8c, 0x1f8d, 0x1f8e, 0x1f8f, 0x1f88, 0x1f89, 0x1f8a, 0x1f8b, 266 | 0x1f8c, 0x1f8d, 0x1f8e, 0x1f8f, 0x1f98, 0x1f99, 0x1f9a, 0x1f9b, 267 | 0x1f9c, 0x1f9d, 0x1f9e, 0x1f9f, 0x1f98, 0x1f99, 0x1f9a, 0x1f9b, 268 | 0x1f9c, 0x1f9d, 0x1f9e, 0x1f9f, 0x1fa8, 0x1fa9, 0x1faa, 0x1fab, 269 | 0x1fac, 0x1fad, 0x1fae, 0x1faf, 0x1fa8, 0x1fa9, 0x1faa, 0x1fab, 270 | 0x1fac, 0x1fad, 0x1fae, 0x1faf, 0x1fb8, 0x1fb9, 0x1fb2, 0x1fbc, 271 | 0x1fb4, 0x1fb5, 0x1fb6, 0x1fb7, 0x1fb8, 0x1fb9, 0x1fba, 0x1fbb, 272 | 0x1fbc, 0x1fbd, 0x1fbe, 0x1fbf, 0x1fc0, 0x1fc1, 0x1fc2, 0x1fc3, 273 | 0x1fc4, 0x1fc5, 0x1fc6, 0x1fc7, 0x1fc8, 0x1fc9, 0x1fca, 0x1fcb, 274 | 0x1fc3, 0x1fcd, 0x1fce, 0x1fcf, 0x1fd8, 0x1fd9, 0x1fd2, 0x1fd3, 275 | 0x1fd4, 0x1fd5, 0x1fd6, 0x1fd7, 0x1fd8, 0x1fd9, 0x1fda, 0x1fdb, 276 | 0x1fdc, 0x1fdd, 0x1fde, 0x1fdf, 0x1fe8, 0x1fe9, 0x1fe2, 0x1fe3, 277 | 0x1fe4, 0x1fec, 0x1fe6, 0x1fe7, 0x1fe8, 0x1fe9, 0x1fea, 0x1feb, 278 | 0x1fec, 0x1fed, 0x1fee, 0x1fef, 0x1ff0, 0x1ff1, 0x1ff2, 0x1ff3, 279 | 0x1ff4, 0x1ff5, 0x1ff6, 0x1ff7, 0x1ff8, 0x1ff9, 0x1ffa, 0x1ffb, 280 | 0x1ff3, 0x1ffd, 0x1ffe, 0x1fff, 0x2000, 0x2001, 0x2002, 0x2003, 281 | 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200a, 0x200b, 282 | 0x200c, 0x200d, 0x200e, 0x200f, 0x2010, 0x2011, 0x2012, 0x2013, 283 | 0x2014, 0x2015, 0x2016, 0x2017, 0x2018, 0x2019, 0x201a, 0x201b, 284 | 0x201c, 0x201d, 0x201e, 0x201f, 0x2020, 0x2021, 0x2022, 0x2023, 285 | 0x2024, 0x2025, 0x2026, 0x2027, 0x2028, 0x2029, 0x202a, 0x202b, 286 | 0x202c, 0x202d, 0x202e, 0x202f, 0x2030, 0x2031, 0x2032, 0x2033, 287 | 0x2034, 0x2035, 0x2036, 0x2037, 0x2038, 0x2039, 0x203a, 0x203b, 288 | 0x203c, 0x203d, 0x203e, 0x203f, 0x2040, 0x2041, 0x2042, 0x2043, 289 | 0x2044, 0x2045, 0x2046, 0x2047, 0x2048, 0x2049, 0x204a, 0x204b, 290 | 0x204c, 0x204d, 0x204e, 0x204f, 0x2050, 0x2051, 0x2052, 0x2053, 291 | 0x2054, 0x2055, 0x2056, 0x2057, 0x2058, 0x2059, 0x205a, 0x205b, 292 | 0x205c, 0x205d, 0x205e, 0x205f, 0x2060, 0x2061, 0x2062, 0x2063, 293 | 0x2064, 0x2065, 0x2066, 0x2067, 0x2068, 0x2069, 0x206a, 0x206b, 294 | 0x206c, 0x206d, 0x206e, 0x206f, 0x2070, 0x2071, 0x2072, 0x2073, 295 | 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079, 0x207a, 0x207b, 296 | 0x207c, 0x207d, 0x207e, 0x207f, 0x2080, 0x2081, 0x2082, 0x2083, 297 | 0x2084, 0x2085, 0x2086, 0x2087, 0x2088, 0x2089, 0x208a, 0x208b, 298 | 0x208c, 0x208d, 0x208e, 0x208f, 0x2090, 0x2091, 0x2092, 0x2093, 299 | 0x2094, 0x2095, 0x2096, 0x2097, 0x2098, 0x2099, 0x209a, 0x209b, 300 | 0x209c, 0x209d, 0x209e, 0x209f, 0x20a0, 0x20a1, 0x20a2, 0x20a3, 301 | 0x20a4, 0x20a5, 0x20a6, 0x20a7, 0x20a8, 0x20a9, 0x20aa, 0x20ab, 302 | 0x20ac, 0x20ad, 0x20ae, 0x20af, 0x20b0, 0x20b1, 0x20b2, 0x20b3, 303 | 0x20b4, 0x20b5, 0x20b6, 0x20b7, 0x20b8, 0x20b9, 0x20ba, 0x20bb, 304 | 0x20bc, 0x20bd, 0x20be, 0x20bf, 0x20c0, 0x20c1, 0x20c2, 0x20c3, 305 | 0x20c4, 0x20c5, 0x20c6, 0x20c7, 0x20c8, 0x20c9, 0x20ca, 0x20cb, 306 | 0x20cc, 0x20cd, 0x20ce, 0x20cf, 0x20d0, 0x20d1, 0x20d2, 0x20d3, 307 | 0x20d4, 0x20d5, 0x20d6, 0x20d7, 0x20d8, 0x20d9, 0x20da, 0x20db, 308 | 0x20dc, 0x20dd, 0x20de, 0x20df, 0x20e0, 0x20e1, 0x20e2, 0x20e3, 309 | 0x20e4, 0x20e5, 0x20e6, 0x20e7, 0x20e8, 0x20e9, 0x20ea, 0x20eb, 310 | 0x20ec, 0x20ed, 0x20ee, 0x20ef, 0x20f0, 0x20f1, 0x20f2, 0x20f3, 311 | 0x20f4, 0x20f5, 0x20f6, 0x20f7, 0x20f8, 0x20f9, 0x20fa, 0x20fb, 312 | 0x20fc, 0x20fd, 0x20fe, 0x20ff, 0x2100, 0x2101, 0x2102, 0x2103, 313 | 0x2104, 0x2105, 0x2106, 0x2107, 0x2108, 0x2109, 0x210a, 0x210b, 314 | 0x210c, 0x210d, 0x210e, 0x210f, 0x2110, 0x2111, 0x2112, 0x2113, 315 | 0x2114, 0x2115, 0x2116, 0x2117, 0x2118, 0x2119, 0x211a, 0x211b, 316 | 0x211c, 0x211d, 0x211e, 0x211f, 0x2120, 0x2121, 0x2122, 0x2123, 317 | 0x2124, 0x2125, 0x2126, 0x2127, 0x2128, 0x2129, 0x212a, 0x212b, 318 | 0x212c, 0x212d, 0x212e, 0x212f, 0x2130, 0x2131, 0x2132, 0x2133, 319 | 0x2134, 0x2135, 0x2136, 0x2137, 0x2138, 0x2139, 0x213a, 0x213b, 320 | 0x213c, 0x213d, 0x213e, 0x213f, 0x2140, 0x2141, 0x2142, 0x2143, 321 | 0x2144, 0x2145, 0x2146, 0x2147, 0x2148, 0x2149, 0x214a, 0x214b, 322 | 0x214c, 0x214d, 0x2132, 0x214f, 0x2150, 0x2151, 0x2152, 0x2153, 323 | 0x2154, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215a, 0x215b, 324 | 0x215c, 0x215d, 0x215e, 0x215f, 0x2160, 0x2161, 0x2162, 0x2163, 325 | 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169, 0x216a, 0x216b, 326 | 0x216c, 0x216d, 0x216e, 0x216f, 0x2160, 0x2161, 0x2162, 0x2163, 327 | 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169, 0x216a, 0x216b, 328 | 0x216c, 0x216d, 0x216e, 0x216f, 0x2180, 0x2181, 0x2182, 0x2183, 329 | 0x2183, 0xffff, 0x034b, 0x24b6, 0x24b7, 0x24b8, 0x24b9, 0x24ba, 330 | 0x24bb, 0x24bc, 0x24bd, 0x24be, 0x24bf, 0x24c0, 0x24c1, 0x24c2, 331 | 0x24c3, 0x24c4, 0x24c5, 0x24c6, 0x24c7, 0x24c8, 0x24c9, 0x24ca, 332 | 0x24cb, 0x24cc, 0x24cd, 0x24ce, 0x24cf, 0xffff, 0x0746, 0x2c00, 333 | 0x2c01, 0x2c02, 0x2c03, 0x2c04, 0x2c05, 0x2c06, 0x2c07, 0x2c08, 334 | 0x2c09, 0x2c0a, 0x2c0b, 0x2c0c, 0x2c0d, 0x2c0e, 0x2c0f, 0x2c10, 335 | 0x2c11, 0x2c12, 0x2c13, 0x2c14, 0x2c15, 0x2c16, 0x2c17, 0x2c18, 336 | 0x2c19, 0x2c1a, 0x2c1b, 0x2c1c, 0x2c1d, 0x2c1e, 0x2c1f, 0x2c20, 337 | 0x2c21, 0x2c22, 0x2c23, 0x2c24, 0x2c25, 0x2c26, 0x2c27, 0x2c28, 338 | 0x2c29, 0x2c2a, 0x2c2b, 0x2c2c, 0x2c2d, 0x2c2e, 0x2c5f, 0x2c60, 339 | 0x2c60, 0x2c62, 0x2c63, 0x2c64, 0x2c65, 0x2c66, 0x2c67, 0x2c67, 340 | 0x2c69, 0x2c69, 0x2c6b, 0x2c6b, 0x2c6d, 0x2c6e, 0x2c6f, 0x2c70, 341 | 0x2c71, 0x2c72, 0x2c73, 0x2c74, 0x2c75, 0x2c75, 0x2c77, 0x2c78, 342 | 0x2c79, 0x2c7a, 0x2c7b, 0x2c7c, 0x2c7d, 0x2c7e, 0x2c7f, 0x2c80, 343 | 0x2c80, 0x2c82, 0x2c82, 0x2c84, 0x2c84, 0x2c86, 0x2c86, 0x2c88, 344 | 0x2c88, 0x2c8a, 0x2c8a, 0x2c8c, 0x2c8c, 0x2c8e, 0x2c8e, 0x2c90, 345 | 0x2c90, 0x2c92, 0x2c92, 0x2c94, 0x2c94, 0x2c96, 0x2c96, 0x2c98, 346 | 0x2c98, 0x2c9a, 0x2c9a, 0x2c9c, 0x2c9c, 0x2c9e, 0x2c9e, 0x2ca0, 347 | 0x2ca0, 0x2ca2, 0x2ca2, 0x2ca4, 0x2ca4, 0x2ca6, 0x2ca6, 0x2ca8, 348 | 0x2ca8, 0x2caa, 0x2caa, 0x2cac, 0x2cac, 0x2cae, 0x2cae, 0x2cb0, 349 | 0x2cb0, 0x2cb2, 0x2cb2, 0x2cb4, 0x2cb4, 0x2cb6, 0x2cb6, 0x2cb8, 350 | 0x2cb8, 0x2cba, 0x2cba, 0x2cbc, 0x2cbc, 0x2cbe, 0x2cbe, 0x2cc0, 351 | 0x2cc0, 0x2cc2, 0x2cc2, 0x2cc4, 0x2cc4, 0x2cc6, 0x2cc6, 0x2cc8, 352 | 0x2cc8, 0x2cca, 0x2cca, 0x2ccc, 0x2ccc, 0x2cce, 0x2cce, 0x2cd0, 353 | 0x2cd0, 0x2cd2, 0x2cd2, 0x2cd4, 0x2cd4, 0x2cd6, 0x2cd6, 0x2cd8, 354 | 0x2cd8, 0x2cda, 0x2cda, 0x2cdc, 0x2cdc, 0x2cde, 0x2cde, 0x2ce0, 355 | 0x2ce0, 0x2ce2, 0x2ce2, 0x2ce4, 0x2ce5, 0x2ce6, 0x2ce7, 0x2ce8, 356 | 0x2ce9, 0x2cea, 0x2ceb, 0x2cec, 0x2ced, 0x2cee, 0x2cef, 0x2cf0, 357 | 0x2cf1, 0x2cf2, 0x2cf3, 0x2cf4, 0x2cf5, 0x2cf6, 0x2cf7, 0x2cf8, 358 | 0x2cf9, 0x2cfa, 0x2cfb, 0x2cfc, 0x2cfd, 0x2cfe, 0x2cff, 0x10a0, 359 | 0x10a1, 0x10a2, 0x10a3, 0x10a4, 0x10a5, 0x10a6, 0x10a7, 0x10a8, 360 | 0x10a9, 0x10aa, 0x10ab, 0x10ac, 0x10ad, 0x10ae, 0x10af, 0x10b0, 361 | 0x10b1, 0x10b2, 0x10b3, 0x10b4, 0x10b5, 0x10b6, 0x10b7, 0x10b8, 362 | 0x10b9, 0x10ba, 0x10bb, 0x10bc, 0x10bd, 0x10be, 0x10bf, 0x10c0, 363 | 0x10c1, 0x10c2, 0x10c3, 0x10c4, 0x10c5, 0xffff, 0xd21b, 0xff21, 364 | 0xff22, 0xff23, 0xff24, 0xff25, 0xff26, 0xff27, 0xff28, 0xff29, 365 | 0xff2a, 0xff2b, 0xff2c, 0xff2d, 0xff2e, 0xff2f, 0xff30, 0xff31, 366 | 0xff32, 0xff33, 0xff34, 0xff35, 0xff36, 0xff37, 0xff38, 0xff39, 367 | 0xff3a, 0xff5b, 0xff5c, 0xff5d, 0xff5e, 0xff5f, 0xff60, 0xff61, 368 | 0xff62, 0xff63, 0xff64, 0xff65, 0xff66, 0xff67, 0xff68, 0xff69, 369 | 0xff6a, 0xff6b, 0xff6c, 0xff6d, 0xff6e, 0xff6f, 0xff70, 0xff71, 370 | 0xff72, 0xff73, 0xff74, 0xff75, 0xff76, 0xff77, 0xff78, 0xff79, 371 | 0xff7a, 0xff7b, 0xff7c, 0xff7d, 0xff7e, 0xff7f, 0xff80, 0xff81, 372 | 0xff82, 0xff83, 0xff84, 0xff85, 0xff86, 0xff87, 0xff88, 0xff89, 373 | 0xff8a, 0xff8b, 0xff8c, 0xff8d, 0xff8e, 0xff8f, 0xff90, 0xff91, 374 | 0xff92, 0xff93, 0xff94, 0xff95, 0xff96, 0xff97, 0xff98, 0xff99, 375 | 0xff9a, 0xff9b, 0xff9c, 0xff9d, 0xff9e, 0xff9f, 0xffa0, 0xffa1, 376 | 0xffa2, 0xffa3, 0xffa4, 0xffa5, 0xffa6, 0xffa7, 0xffa8, 0xffa9, 377 | 0xffaa, 0xffab, 0xffac, 0xffad, 0xffae, 0xffaf, 0xffb0, 0xffb1, 378 | 0xffb2, 0xffb3, 0xffb4, 0xffb5, 0xffb6, 0xffb7, 0xffb8, 0xffb9, 379 | 0xffba, 0xffbb, 0xffbc, 0xffbd, 0xffbe, 0xffbf, 0xffc0, 0xffc1, 380 | 0xffc2, 0xffc3, 0xffc4, 0xffc5, 0xffc6, 0xffc7, 0xffc8, 0xffc9, 381 | 0xffca, 0xffcb, 0xffcc, 0xffcd, 0xffce, 0xffcf, 0xffd0, 0xffd1, 382 | 0xffd2, 0xffd3, 0xffd4, 0xffd5, 0xffd6, 0xffd7, 0xffd8, 0xffd9, 383 | 0xffda, 0xffdb, 0xffdc, 0xffdd, 0xffde, 0xffdf, 0xffe0, 0xffe1, 384 | 0xffe2, 0xffe3, 0xffe4, 0xffe5, 0xffe6, 0xffe7, 0xffe8, 0xffe9, 385 | 0xffea, 0xffeb, 0xffec, 0xffed, 0xffee, 0xffef, 0xfff0, 0xfff1, 386 | 0xfff2, 0xfff3, 0xfff4, 0xfff5, 0xfff6, 0xfff7, 0xfff8, 0xfff9, 387 | 0xfffa, 0xfffb, 0xfffc, 0xfffd, 0xfffe, 0xffff, 388 | }; 389 | 390 | /* 391 | * Allow full-width illegal characters : 392 | * "MS windows 7" supports full-width-invalid-name-characters. 393 | * So we should check half-width-invalid-name-characters(ASCII) only 394 | * for compatibility. 395 | * 396 | * " * / : < > ? \ | 397 | */ 398 | static unsigned short bad_uni_chars[] = { 399 | 0x0022, 0x002A, 0x002F, 0x003A, 400 | 0x003C, 0x003E, 0x003F, 0x005C, 0x007C, 401 | 0 402 | }; 403 | 404 | static int exfat_convert_char_to_ucs2(struct nls_table *nls, 405 | const unsigned char *ch, int ch_len, unsigned short *ucs2, 406 | int *lossy) 407 | { 408 | int len; 409 | 410 | *ucs2 = 0x0; 411 | 412 | if (ch[0] < 0x80) { 413 | *ucs2 = ch[0]; 414 | return 1; 415 | } 416 | 417 | len = nls->char2uni(ch, ch_len, ucs2); 418 | if (len < 0) { 419 | /* conversion failed */ 420 | if (lossy != NULL) 421 | *lossy |= NLS_NAME_LOSSY; 422 | *ucs2 = '_'; 423 | return 1; 424 | } 425 | return len; 426 | } 427 | 428 | static int exfat_convert_ucs2_to_char(struct nls_table *nls, 429 | unsigned short ucs2, unsigned char *ch, int *lossy) 430 | { 431 | int len; 432 | 433 | ch[0] = 0x0; 434 | 435 | if (ucs2 < 0x0080) { 436 | ch[0] = ucs2; 437 | return 1; 438 | } 439 | 440 | len = nls->uni2char(ucs2, ch, MAX_CHARSET_SIZE); 441 | if (len < 0) { 442 | /* conversion failed */ 443 | if (lossy != NULL) 444 | *lossy |= NLS_NAME_LOSSY; 445 | ch[0] = '_'; 446 | return 1; 447 | } 448 | return len; 449 | } 450 | 451 | unsigned short exfat_toupper(struct super_block *sb, unsigned short a) 452 | { 453 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 454 | 455 | return sbi->vol_utbl[a] ? sbi->vol_utbl[a] : a; 456 | } 457 | 458 | static unsigned short *exfat_wstrchr(unsigned short *str, unsigned short wchar) 459 | { 460 | while (*str) { 461 | if (*(str++) == wchar) 462 | return str; 463 | } 464 | return NULL; 465 | } 466 | 467 | int exfat_uniname_ncmp(struct super_block *sb, unsigned short *a, 468 | unsigned short *b, unsigned int len) 469 | { 470 | int i; 471 | 472 | for (i = 0; i < len; i++, a++, b++) 473 | if (exfat_toupper(sb, *a) != exfat_toupper(sb, *b)) 474 | return 1; 475 | return 0; 476 | } 477 | 478 | static int exfat_utf16_to_utf8(struct super_block *sb, 479 | struct exfat_uni_name *p_uniname, unsigned char *p_cstring, 480 | int buflen) 481 | { 482 | int len; 483 | const unsigned short *uniname = p_uniname->name; 484 | 485 | /* always len >= 0 */ 486 | len = utf16s_to_utf8s(uniname, MAX_NAME_LENGTH, UTF16_HOST_ENDIAN, 487 | p_cstring, buflen); 488 | p_cstring[len] = '\0'; 489 | return len; 490 | } 491 | 492 | static int exfat_utf8_to_utf16(struct super_block *sb, 493 | const unsigned char *p_cstring, const int len, 494 | struct exfat_uni_name *p_uniname, int *p_lossy) 495 | { 496 | int i, unilen, lossy = NLS_NAME_NO_LOSSY; 497 | unsigned short upname[MAX_NAME_LENGTH + 1]; 498 | unsigned short *uniname = p_uniname->name; 499 | 500 | WARN_ON(!len); 501 | 502 | unilen = utf8s_to_utf16s(p_cstring, len, UTF16_HOST_ENDIAN, 503 | (wchar_t *)uniname, MAX_NAME_LENGTH + 2); 504 | if (unilen < 0) { 505 | exfat_err(sb, "failed to %s (err : %d) nls len : %d", 506 | __func__, unilen, len); 507 | return unilen; 508 | } 509 | 510 | if (unilen > MAX_NAME_LENGTH) { 511 | exfat_err(sb, "failed to %s (estr:ENAMETOOLONG) nls len : %d, unilen : %d > %d", 512 | __func__, len, unilen, MAX_NAME_LENGTH); 513 | return -ENAMETOOLONG; 514 | } 515 | 516 | for (i = 0; i < unilen; i++) { 517 | if (*uniname < 0x0020 || 518 | exfat_wstrchr(bad_uni_chars, *uniname)) 519 | lossy |= NLS_NAME_LOSSY; 520 | 521 | upname[i] = exfat_toupper(sb, *uniname); 522 | uniname++; 523 | } 524 | 525 | *uniname = '\0'; 526 | p_uniname->name_len = unilen; 527 | p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0, 528 | CS_DEFAULT); 529 | 530 | if (p_lossy) 531 | *p_lossy = lossy; 532 | return unilen; 533 | } 534 | 535 | #define SURROGATE_MASK 0xfffff800 536 | #define SURROGATE_PAIR 0x0000d800 537 | #define SURROGATE_LOW 0x00000400 538 | 539 | static int __exfat_utf16_to_nls(struct super_block *sb, 540 | struct exfat_uni_name *p_uniname, unsigned char *p_cstring, 541 | int buflen) 542 | { 543 | int i, j, len, out_len = 0; 544 | unsigned char buf[MAX_CHARSET_SIZE]; 545 | const unsigned short *uniname = p_uniname->name; 546 | struct nls_table *nls = EXFAT_SB(sb)->nls_io; 547 | 548 | i = 0; 549 | while (i < MAX_NAME_LENGTH && out_len < (buflen - 1)) { 550 | if (*uniname == '\0') 551 | break; 552 | if ((*uniname & SURROGATE_MASK) != SURROGATE_PAIR) { 553 | len = exfat_convert_ucs2_to_char(nls, *uniname, buf, 554 | NULL); 555 | } else { 556 | /* Process UTF-16 surrogate pair as one character */ 557 | if (!(*uniname & SURROGATE_LOW) && 558 | i+1 < MAX_NAME_LENGTH && 559 | (*(uniname+1) & SURROGATE_MASK) == SURROGATE_PAIR && 560 | (*(uniname+1) & SURROGATE_LOW)) { 561 | uniname++; 562 | i++; 563 | } 564 | 565 | /* 566 | * UTF-16 surrogate pair encodes code points above 567 | * U+FFFF. Code points above U+FFFF are not supported 568 | * by kernel NLS framework therefore use replacement 569 | * character 570 | */ 571 | len = 1; 572 | buf[0] = '_'; 573 | } 574 | 575 | if (out_len + len >= buflen) 576 | len = buflen - 1 - out_len; 577 | out_len += len; 578 | 579 | if (len > 1) { 580 | for (j = 0; j < len; j++) 581 | *p_cstring++ = buf[j]; 582 | } else { /* len == 1 */ 583 | *p_cstring++ = *buf; 584 | } 585 | 586 | uniname++; 587 | i++; 588 | } 589 | 590 | *p_cstring = '\0'; 591 | return out_len; 592 | } 593 | 594 | static int exfat_nls_to_ucs2(struct super_block *sb, 595 | const unsigned char *p_cstring, const int len, 596 | struct exfat_uni_name *p_uniname, int *p_lossy) 597 | { 598 | int i = 0, unilen = 0, lossy = NLS_NAME_NO_LOSSY; 599 | unsigned short upname[MAX_NAME_LENGTH + 1]; 600 | unsigned short *uniname = p_uniname->name; 601 | struct nls_table *nls = EXFAT_SB(sb)->nls_io; 602 | 603 | WARN_ON(!len); 604 | 605 | while (unilen < MAX_NAME_LENGTH && i < len) { 606 | i += exfat_convert_char_to_ucs2(nls, p_cstring + i, len - i, 607 | uniname, &lossy); 608 | 609 | if (*uniname < 0x0020 || 610 | exfat_wstrchr(bad_uni_chars, *uniname)) 611 | lossy |= NLS_NAME_LOSSY; 612 | 613 | upname[unilen] = exfat_toupper(sb, *uniname); 614 | uniname++; 615 | unilen++; 616 | } 617 | 618 | if (p_cstring[i] != '\0') 619 | lossy |= NLS_NAME_OVERLEN; 620 | 621 | *uniname = '\0'; 622 | p_uniname->name_len = unilen; 623 | p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0, 624 | CS_DEFAULT); 625 | 626 | if (p_lossy) 627 | *p_lossy = lossy; 628 | return unilen; 629 | } 630 | 631 | int exfat_utf16_to_nls(struct super_block *sb, struct exfat_uni_name *uniname, 632 | unsigned char *p_cstring, int buflen) 633 | { 634 | if (EXFAT_SB(sb)->options.utf8) 635 | return exfat_utf16_to_utf8(sb, uniname, p_cstring, 636 | buflen); 637 | return __exfat_utf16_to_nls(sb, uniname, p_cstring, buflen); 638 | } 639 | 640 | int exfat_nls_to_utf16(struct super_block *sb, const unsigned char *p_cstring, 641 | const int len, struct exfat_uni_name *uniname, int *p_lossy) 642 | { 643 | if (EXFAT_SB(sb)->options.utf8) 644 | return exfat_utf8_to_utf16(sb, p_cstring, len, 645 | uniname, p_lossy); 646 | return exfat_nls_to_ucs2(sb, p_cstring, len, uniname, p_lossy); 647 | } 648 | 649 | static int exfat_load_upcase_table(struct super_block *sb, 650 | sector_t sector, unsigned long long num_sectors, 651 | unsigned int utbl_checksum) 652 | { 653 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 654 | unsigned int sect_size = sb->s_blocksize; 655 | unsigned int i, index = 0; 656 | u32 chksum = 0; 657 | int ret; 658 | unsigned char skip = false; 659 | unsigned short *upcase_table; 660 | 661 | upcase_table = kcalloc(UTBL_COUNT, sizeof(unsigned short), GFP_KERNEL); 662 | if (!upcase_table) 663 | return -ENOMEM; 664 | 665 | sbi->vol_utbl = upcase_table; 666 | num_sectors += sector; 667 | 668 | while (sector < num_sectors) { 669 | struct buffer_head *bh; 670 | 671 | bh = sb_bread(sb, sector); 672 | if (!bh) { 673 | exfat_err(sb, "failed to read sector(0x%llx)\n", 674 | (unsigned long long)sector); 675 | ret = -EIO; 676 | goto free_table; 677 | } 678 | sector++; 679 | for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) { 680 | unsigned short uni = get_unaligned_le16(bh->b_data + i); 681 | 682 | if (skip) { 683 | index += uni; 684 | skip = false; 685 | } else if (uni == index) { 686 | index++; 687 | } else if (uni == 0xFFFF) { 688 | skip = true; 689 | } else { /* uni != index , uni != 0xFFFF */ 690 | upcase_table[index] = uni; 691 | index++; 692 | } 693 | } 694 | chksum = exfat_calc_chksum32(bh->b_data, i, chksum, CS_DEFAULT); 695 | brelse(bh); 696 | } 697 | 698 | if (index >= 0xFFFF && utbl_checksum == chksum) 699 | return 0; 700 | 701 | exfat_err(sb, "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)", 702 | index, chksum, utbl_checksum); 703 | ret = -EINVAL; 704 | free_table: 705 | exfat_free_upcase_table(sbi); 706 | return ret; 707 | } 708 | 709 | static int exfat_load_default_upcase_table(struct super_block *sb) 710 | { 711 | int i, ret = -EIO; 712 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 713 | unsigned char skip = false; 714 | unsigned short uni = 0, *upcase_table; 715 | unsigned int index = 0; 716 | 717 | upcase_table = kcalloc(UTBL_COUNT, sizeof(unsigned short), GFP_KERNEL); 718 | if (!upcase_table) 719 | return -ENOMEM; 720 | 721 | sbi->vol_utbl = upcase_table; 722 | 723 | for (i = 0; index <= 0xFFFF && i < EXFAT_NUM_UPCASE; i++) { 724 | uni = uni_def_upcase[i]; 725 | if (skip) { 726 | index += uni; 727 | skip = false; 728 | } else if (uni == index) { 729 | index++; 730 | } else if (uni == 0xFFFF) { 731 | skip = true; 732 | } else { 733 | upcase_table[index] = uni; 734 | index++; 735 | } 736 | } 737 | 738 | if (index >= 0xFFFF) 739 | return 0; 740 | 741 | /* FATAL error: default upcase table has error */ 742 | exfat_free_upcase_table(sbi); 743 | return ret; 744 | } 745 | 746 | int exfat_create_upcase_table(struct super_block *sb) 747 | { 748 | int i, ret; 749 | unsigned int tbl_clu, type; 750 | sector_t sector; 751 | unsigned long long tbl_size, num_sectors; 752 | unsigned char blksize_bits = sb->s_blocksize_bits; 753 | struct exfat_chain clu; 754 | struct exfat_dentry *ep; 755 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 756 | struct buffer_head *bh; 757 | 758 | clu.dir = sbi->root_dir; 759 | clu.flags = ALLOC_FAT_CHAIN; 760 | 761 | while (clu.dir != EXFAT_EOF_CLUSTER) { 762 | for (i = 0; i < sbi->dentries_per_clu; i++) { 763 | ep = exfat_get_dentry(sb, &clu, i, &bh, NULL); 764 | if (!ep) 765 | return -EIO; 766 | 767 | type = exfat_get_entry_type(ep); 768 | if (type == TYPE_UNUSED) { 769 | brelse(bh); 770 | break; 771 | } 772 | 773 | if (type != TYPE_UPCASE) { 774 | brelse(bh); 775 | continue; 776 | } 777 | 778 | tbl_clu = le32_to_cpu(ep->dentry.upcase.start_clu); 779 | tbl_size = le64_to_cpu(ep->dentry.upcase.size); 780 | 781 | sector = exfat_cluster_to_sector(sbi, tbl_clu); 782 | num_sectors = ((tbl_size - 1) >> blksize_bits) + 1; 783 | ret = exfat_load_upcase_table(sb, sector, num_sectors, 784 | le32_to_cpu(ep->dentry.upcase.checksum)); 785 | 786 | brelse(bh); 787 | if (ret && ret != -EIO) 788 | goto load_default; 789 | 790 | /* load successfully */ 791 | return ret; 792 | } 793 | 794 | if (exfat_get_next_cluster(sb, &(clu.dir))) 795 | return -EIO; 796 | } 797 | 798 | load_default: 799 | /* load default upcase table */ 800 | return exfat_load_default_upcase_table(sb); 801 | } 802 | 803 | void exfat_free_upcase_table(struct exfat_sb_info *sbi) 804 | { 805 | kfree(sbi->vol_utbl); 806 | } 807 | -------------------------------------------------------------------------------- /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 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "exfat_fs.h" 20 | 21 | static int exfat_init_sb_info(struct super_block *sb); 22 | static int exfat_parse_options(struct super_block *sb, char *options, int silent, 23 | struct exfat_mount_options *opts); 24 | 25 | static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET; 26 | static struct kmem_cache *exfat_inode_cachep; 27 | 28 | static void exfat_free_iocharset(struct exfat_sb_info *sbi) 29 | { 30 | if (sbi->options.iocharset != exfat_default_iocharset) 31 | kfree(sbi->options.iocharset); 32 | } 33 | 34 | static void exfat_delayed_free(struct rcu_head *p) 35 | { 36 | struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu); 37 | 38 | unload_nls(sbi->nls_io); 39 | exfat_free_iocharset(sbi); 40 | exfat_free_upcase_table(sbi); 41 | kfree(sbi); 42 | } 43 | 44 | static void exfat_put_super(struct super_block *sb) 45 | { 46 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 47 | 48 | mutex_lock(&sbi->s_lock); 49 | if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) 50 | sync_blockdev(sb->s_bdev); 51 | exfat_set_vol_flags(sb, VOL_CLEAN); 52 | exfat_free_bitmap(sbi); 53 | brelse(sbi->boot_bh); 54 | mutex_unlock(&sbi->s_lock); 55 | 56 | call_rcu(&sbi->rcu, exfat_delayed_free); 57 | } 58 | 59 | static int exfat_sync_fs(struct super_block *sb, int wait) 60 | { 61 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 62 | int err = 0; 63 | 64 | /* If there are some dirty buffers in the bdev inode */ 65 | mutex_lock(&sbi->s_lock); 66 | if (test_and_clear_bit(EXFAT_SB_DIRTY, &sbi->s_state)) { 67 | sync_blockdev(sb->s_bdev); 68 | if (exfat_set_vol_flags(sb, VOL_CLEAN)) 69 | err = -EIO; 70 | } 71 | mutex_unlock(&sbi->s_lock); 72 | return err; 73 | } 74 | 75 | static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf) 76 | { 77 | struct super_block *sb = dentry->d_sb; 78 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 79 | unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev); 80 | 81 | if (sbi->used_clusters == EXFAT_CLUSTERS_UNTRACKED) { 82 | mutex_lock(&sbi->s_lock); 83 | if (exfat_count_used_clusters(sb, &sbi->used_clusters)) { 84 | mutex_unlock(&sbi->s_lock); 85 | return -EIO; 86 | } 87 | mutex_unlock(&sbi->s_lock); 88 | } 89 | 90 | buf->f_type = sb->s_magic; 91 | buf->f_bsize = sbi->cluster_size; 92 | buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */ 93 | buf->f_bfree = buf->f_blocks - sbi->used_clusters; 94 | buf->f_bavail = buf->f_bfree; 95 | buf->f_fsid.val[0] = (unsigned int)id; 96 | buf->f_fsid.val[1] = (unsigned int)(id >> 32); 97 | /* Unicode utf16 255 characters */ 98 | buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE; 99 | return 0; 100 | } 101 | 102 | int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flag) 103 | { 104 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 105 | struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data; 106 | bool sync; 107 | 108 | /* flags are not changed */ 109 | if (sbi->vol_flag == new_flag) 110 | return 0; 111 | 112 | sbi->vol_flag = new_flag; 113 | 114 | /* skip updating volume dirty flag, 115 | * if this volume has been mounted with read-only 116 | */ 117 | if (sb_rdonly(sb)) 118 | return 0; 119 | 120 | p_boot->vol_flags = cpu_to_le16(new_flag); 121 | 122 | if (new_flag == VOL_DIRTY && !buffer_dirty(sbi->boot_bh)) 123 | sync = true; 124 | else 125 | sync = false; 126 | 127 | set_buffer_uptodate(sbi->boot_bh); 128 | mark_buffer_dirty(sbi->boot_bh); 129 | 130 | if (sync) 131 | sync_dirty_buffer(sbi->boot_bh); 132 | return 0; 133 | } 134 | 135 | static int exfat_show_options(struct seq_file *m, struct dentry *root) 136 | { 137 | struct super_block *sb = root->d_sb; 138 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 139 | struct exfat_mount_options *opts = &sbi->options; 140 | 141 | /* Show partition info */ 142 | if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID)) 143 | seq_printf(m, ",uid=%u", 144 | from_kuid_munged(&init_user_ns, opts->fs_uid)); 145 | if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID)) 146 | seq_printf(m, ",gid=%u", 147 | from_kgid_munged(&init_user_ns, opts->fs_gid)); 148 | seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask); 149 | if (opts->allow_utime) 150 | seq_printf(m, ",allow_utime=%04o", opts->allow_utime); 151 | if (opts->quiet) 152 | seq_puts(m, ",quiet"); 153 | if (opts->utf8) 154 | seq_puts(m, ",iocharset=utf8"); 155 | else if (sbi->nls_io) 156 | seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); 157 | if (opts->errors == EXFAT_ERRORS_CONT) 158 | seq_puts(m, ",errors=continue"); 159 | else if (opts->errors == EXFAT_ERRORS_PANIC) 160 | seq_puts(m, ",errors=panic"); 161 | else 162 | seq_puts(m, ",errors=remount-ro"); 163 | if (opts->discard) 164 | seq_puts(m, ",discard"); 165 | if (opts->time_offset) 166 | seq_printf(m, ",time_offset=%d", opts->time_offset); 167 | return 0; 168 | } 169 | 170 | static struct inode *exfat_alloc_inode(struct super_block *sb) 171 | { 172 | struct exfat_inode_info *ei; 173 | 174 | ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS); 175 | if (!ei) 176 | return NULL; 177 | 178 | init_rwsem(&ei->truncate_lock); 179 | return &ei->vfs_inode; 180 | } 181 | 182 | static void exfat_free_inode(struct inode *inode) 183 | { 184 | kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode)); 185 | } 186 | 187 | static int exfat_remount(struct super_block *sb, int *flags, char *opt) 188 | { 189 | int ret = 0; 190 | 191 | *flags |= SB_NODIRATIME; 192 | 193 | /* volume flag will be updated in exfat_sync_fs */ 194 | sync_filesystem(sb); 195 | 196 | ret = exfat_parse_options(sb, opt, 0, &EXFAT_SB(sb)->options); 197 | if (ret) 198 | exfat_err(sb, "failed to parse options"); 199 | 200 | return ret; 201 | } 202 | 203 | static const struct super_operations exfat_sops = { 204 | .alloc_inode = exfat_alloc_inode, 205 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0) 206 | .free_inode = exfat_free_inode, 207 | #else 208 | .destroy_inode = exfat_free_inode, 209 | #endif 210 | .write_inode = exfat_write_inode, 211 | .evict_inode = exfat_evict_inode, 212 | .put_super = exfat_put_super, 213 | .sync_fs = exfat_sync_fs, 214 | .statfs = exfat_statfs, 215 | .show_options = exfat_show_options, 216 | .remount_fs = exfat_remount, 217 | }; 218 | 219 | enum { 220 | Opt_uid, 221 | Opt_gid, 222 | Opt_umask, 223 | Opt_dmask, 224 | Opt_fmask, 225 | Opt_allow_utime, 226 | Opt_charset, 227 | Opt_quiet, 228 | Opt_err_cont, 229 | Opt_err_panic, 230 | Opt_err_ro, 231 | Opt_discard, 232 | Opt_time_offset, 233 | 234 | /* Deprecated options */ 235 | Opt_utf8, 236 | Opt_debug, 237 | Opt_namecase, 238 | Opt_codepage, 239 | }; 240 | 241 | static const match_table_t exfat_tokens = { 242 | {Opt_uid, "uid=%u"}, 243 | {Opt_gid, "gid=%u"}, 244 | {Opt_umask, "umask=%o"}, 245 | {Opt_dmask, "dmask=%o"}, 246 | {Opt_fmask, "fmask=%o"}, 247 | {Opt_allow_utime, "allow_utime=%o"}, 248 | {Opt_charset, "iocharset=%s"}, 249 | {Opt_quiet, "quiet"}, 250 | {Opt_err_cont, "errors=continue"}, 251 | {Opt_err_panic, "errors=panic"}, 252 | {Opt_err_ro, "errors=remount-ro"}, 253 | {Opt_discard, "discard"}, 254 | {Opt_time_offset, "time_offset=%d"}, 255 | 256 | /* Deprecated options */ 257 | {Opt_utf8, "utf8"}, 258 | {Opt_debug, "debug"}, 259 | {Opt_namecase, "namecase=%u"}, 260 | {Opt_codepage, "codepage=%u"}, 261 | }; 262 | 263 | static int __exfat_parse_option(struct super_block *sb, char *p, substring_t *args, int token, int silent) 264 | { 265 | struct exfat_sb_info *sbi = sb->s_fs_info; 266 | struct exfat_mount_options *opts = &sbi->options; 267 | int option; 268 | char *tmpstr; 269 | 270 | switch (token) { 271 | case Opt_uid: 272 | if (match_int(&args[0], &option)) 273 | return -EINVAL; 274 | opts->fs_uid = make_kuid(current_user_ns(), option); 275 | break; 276 | case Opt_gid: 277 | if (match_int(&args[0], &option)) 278 | return -EINVAL; 279 | opts->fs_gid = make_kgid(current_user_ns(), option); 280 | break; 281 | case Opt_umask: 282 | case Opt_dmask: 283 | case Opt_fmask: 284 | if (match_octal(&args[0], &option)) 285 | return -EINVAL; 286 | if (token != Opt_dmask) 287 | opts->fs_fmask = option; 288 | if (token != Opt_fmask) 289 | opts->fs_dmask = option; 290 | break; 291 | case Opt_allow_utime: 292 | if (match_octal(&args[0], &option)) 293 | return -EINVAL; 294 | opts->allow_utime = option & (S_IWGRP | S_IWOTH); 295 | break; 296 | case Opt_charset: 297 | exfat_free_iocharset(sbi); 298 | tmpstr = match_strdup(&args[0]); 299 | if (!tmpstr) 300 | return -ENOMEM; 301 | opts->iocharset = tmpstr; 302 | break; 303 | case Opt_quiet: 304 | opts->quiet = 1; 305 | break; 306 | case Opt_err_cont: 307 | opts->errors = EXFAT_ERRORS_CONT; 308 | break; 309 | case Opt_err_panic: 310 | opts->errors = EXFAT_ERRORS_PANIC; 311 | break; 312 | case Opt_err_ro: 313 | opts->errors = EXFAT_ERRORS_RO; 314 | break; 315 | case Opt_discard: 316 | opts->discard = 1; 317 | break; 318 | case Opt_time_offset: 319 | if (match_int(&args[0], &option)) 320 | return -EINVAL; 321 | /* 322 | * Make the limit 24 just in case someone invents something 323 | * unusual. 324 | */ 325 | if (option < -24 * 60 || option > 24 * 60) 326 | return -EINVAL; 327 | opts->time_offset = option; 328 | break; 329 | case Opt_utf8: 330 | case Opt_debug: 331 | case Opt_namecase: 332 | case Opt_codepage: 333 | if (!silent) 334 | exfat_warn(sb, "deprecated mount option \"%s\" ", p); 335 | break; 336 | default: 337 | return -EINVAL; 338 | } 339 | 340 | return 0; 341 | } 342 | 343 | static int exfat_parse_options(struct super_block *sb, char *options, int silent, 344 | struct exfat_mount_options *opts) 345 | { 346 | char *p; 347 | substring_t args[MAX_OPT_ARGS]; 348 | int ret; 349 | 350 | if (!options) 351 | goto out; 352 | 353 | while ((p = strsep(&options, ",")) != NULL) { 354 | int token; 355 | 356 | if (!*p) 357 | continue; 358 | token = match_token(p, exfat_tokens, args); 359 | ret = __exfat_parse_option(sb, p, args, token, silent); 360 | if (ret < 0) { 361 | if (ret == -EINVAL && !silent) { 362 | exfat_msg(sb, KERN_ERR, 363 | "unrecognized mount option \"%s\" " 364 | "or missing value", p); 365 | } 366 | return ret; 367 | } 368 | } 369 | 370 | if (opts->allow_utime == (unsigned short)-1) 371 | opts->allow_utime = ~opts->fs_dmask & 0022; 372 | 373 | if (opts->discard) { 374 | struct request_queue *q = bdev_get_queue(sb->s_bdev); 375 | 376 | if (!blk_queue_discard(q)) { 377 | exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard"); 378 | opts->discard = 0; 379 | } 380 | } 381 | out: 382 | return 0; 383 | } 384 | 385 | static void exfat_hash_init(struct super_block *sb) 386 | { 387 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 388 | int i; 389 | 390 | spin_lock_init(&sbi->inode_hash_lock); 391 | for (i = 0; i < EXFAT_HASH_SIZE; i++) 392 | INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); 393 | } 394 | 395 | static int exfat_read_root(struct inode *inode) 396 | { 397 | struct super_block *sb = inode->i_sb; 398 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 399 | struct exfat_inode_info *ei = EXFAT_I(inode); 400 | struct exfat_chain cdir; 401 | int num_subdirs, num_clu = 0; 402 | 403 | exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 404 | ei->entry = -1; 405 | ei->start_clu = sbi->root_dir; 406 | ei->flags = ALLOC_FAT_CHAIN; 407 | ei->type = TYPE_DIR; 408 | ei->version = 0; 409 | ei->rwoffset = 0; 410 | ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 411 | ei->hint_stat.eidx = 0; 412 | ei->hint_stat.clu = sbi->root_dir; 413 | ei->hint_femp.eidx = EXFAT_HINT_NONE; 414 | 415 | exfat_chain_set(&cdir, sbi->root_dir, 0, ALLOC_FAT_CHAIN); 416 | if (exfat_count_num_clusters(sb, &cdir, &num_clu)) 417 | return -EIO; 418 | i_size_write(inode, num_clu << sbi->cluster_size_bits); 419 | 420 | num_subdirs = exfat_count_dir_entries(sb, &cdir); 421 | if (num_subdirs < 0) 422 | return -EIO; 423 | set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR); 424 | 425 | inode->i_uid = sbi->options.fs_uid; 426 | inode->i_gid = sbi->options.fs_gid; 427 | inode_inc_iversion(inode); 428 | inode->i_generation = 0; 429 | inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777); 430 | inode->i_op = &exfat_dir_inode_operations; 431 | inode->i_fop = &exfat_dir_operations; 432 | 433 | inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) 434 | & ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 435 | EXFAT_I(inode)->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff; 436 | EXFAT_I(inode)->i_size_aligned = i_size_read(inode); 437 | EXFAT_I(inode)->i_size_ondisk = i_size_read(inode); 438 | 439 | exfat_save_attr(inode, ATTR_SUBDIR); 440 | inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime = 441 | current_time(inode); 442 | exfat_truncate_atime(&inode->i_atime); 443 | exfat_cache_init_inode(inode); 444 | return 0; 445 | } 446 | 447 | static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect) 448 | { 449 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 450 | 451 | if (!is_power_of_2(logical_sect) || 452 | logical_sect < 512 || logical_sect > 4096) { 453 | exfat_err(sb, "bogus logical sector size %u", logical_sect); 454 | return -EIO; 455 | } 456 | 457 | if (logical_sect < sb->s_blocksize) { 458 | exfat_err(sb, "logical sector size too small for device (logical sector size = %u)", 459 | logical_sect); 460 | return -EIO; 461 | } 462 | 463 | if (logical_sect > sb->s_blocksize) { 464 | brelse(sbi->boot_bh); 465 | sbi->boot_bh = NULL; 466 | 467 | if (!sb_set_blocksize(sb, logical_sect)) { 468 | exfat_err(sb, "unable to set blocksize %u", 469 | logical_sect); 470 | return -EIO; 471 | } 472 | sbi->boot_bh = sb_bread(sb, 0); 473 | if (!sbi->boot_bh) { 474 | exfat_err(sb, "unable to read boot sector (logical sector size = %lu)", 475 | sb->s_blocksize); 476 | return -EIO; 477 | } 478 | } 479 | return 0; 480 | } 481 | 482 | static int exfat_read_boot_sector(struct super_block *sb) 483 | { 484 | struct boot_sector *p_boot; 485 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 486 | 487 | /* set block size to read super block */ 488 | sb_min_blocksize(sb, 512); 489 | 490 | /* read boot sector */ 491 | sbi->boot_bh = sb_bread(sb, 0); 492 | if (!sbi->boot_bh) { 493 | exfat_err(sb, "unable to read boot sector"); 494 | return -EIO; 495 | } 496 | p_boot = (struct boot_sector *)sbi->boot_bh->b_data; 497 | 498 | /* check the validity of BOOT */ 499 | if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) { 500 | exfat_err(sb, "invalid boot record signature"); 501 | return -EINVAL; 502 | } 503 | 504 | if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) { 505 | exfat_err(sb, "invalid fs_name"); /* fs_name may unprintable */ 506 | return -EINVAL; 507 | } 508 | 509 | /* 510 | * must_be_zero field must be filled with zero to prevent mounting 511 | * from FAT volume. 512 | */ 513 | if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero))) 514 | return -EINVAL; 515 | 516 | if (p_boot->num_fats != 1 && p_boot->num_fats != 2) { 517 | exfat_err(sb, "bogus number of FAT structure"); 518 | return -EINVAL; 519 | } 520 | 521 | sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits; 522 | sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits; 523 | sbi->cluster_size_bits = p_boot->sect_per_clus_bits + 524 | p_boot->sect_size_bits; 525 | sbi->cluster_size = 1 << sbi->cluster_size_bits; 526 | sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length); 527 | sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset); 528 | sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset); 529 | if (p_boot->num_fats == 2) 530 | sbi->FAT2_start_sector += sbi->num_FAT_sectors; 531 | sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset); 532 | sbi->num_sectors = le64_to_cpu(p_boot->vol_length); 533 | /* because the cluster index starts with 2 */ 534 | sbi->num_clusters = le32_to_cpu(p_boot->clu_count) + 535 | EXFAT_RESERVED_CLUSTERS; 536 | 537 | sbi->root_dir = le32_to_cpu(p_boot->root_cluster); 538 | sbi->dentries_per_clu = 1 << 539 | (sbi->cluster_size_bits - DENTRY_SIZE_BITS); 540 | 541 | sbi->vol_flag = le16_to_cpu(p_boot->vol_flags); 542 | sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; 543 | sbi->used_clusters = EXFAT_CLUSTERS_UNTRACKED; 544 | 545 | /* check consistencies */ 546 | if (sbi->num_FAT_sectors << p_boot->sect_size_bits < 547 | sbi->num_clusters * 4) { 548 | exfat_err(sb, "bogus fat length"); 549 | return -EINVAL; 550 | } 551 | if (sbi->data_start_sector < 552 | sbi->FAT1_start_sector + sbi->num_FAT_sectors * p_boot->num_fats) { 553 | exfat_err(sb, "bogus data start sector"); 554 | return -EINVAL; 555 | } 556 | if (sbi->vol_flag & VOL_DIRTY) 557 | exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck."); 558 | if (sbi->vol_flag & ERR_MEDIUM) 559 | exfat_warn(sb, "Medium has reported failures. Some data may be lost."); 560 | 561 | /* exFAT file size is limited by a disk volume size */ 562 | sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) << 563 | sbi->cluster_size_bits; 564 | 565 | /* check logical sector size */ 566 | if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits)) 567 | return -EIO; 568 | 569 | return 0; 570 | } 571 | 572 | static int exfat_verify_boot_region(struct super_block *sb) 573 | { 574 | struct buffer_head *bh = NULL; 575 | u32 chksum = 0; 576 | __le32 *p_sig, *p_chksum; 577 | int sn, i; 578 | 579 | /* read boot sector sub-regions */ 580 | for (sn = 0; sn < 11; sn++) { 581 | bh = sb_bread(sb, sn); 582 | if (!bh) 583 | return -EIO; 584 | 585 | if (sn != 0 && sn <= 8) { 586 | /* extended boot sector sub-regions */ 587 | p_sig = (__le32 *)&bh->b_data[sb->s_blocksize - 4]; 588 | if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE) 589 | exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x", 590 | sn, le32_to_cpu(*p_sig)); 591 | } 592 | 593 | chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize, 594 | chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR); 595 | brelse(bh); 596 | } 597 | 598 | /* boot checksum sub-regions */ 599 | bh = sb_bread(sb, sn); 600 | if (!bh) 601 | return -EIO; 602 | 603 | for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) { 604 | p_chksum = (__le32 *)&bh->b_data[i]; 605 | if (le32_to_cpu(*p_chksum) != chksum) { 606 | exfat_err(sb, "Invalid boot checksum (boot checksum : 0x%08x, checksum : 0x%08x)", 607 | le32_to_cpu(*p_chksum), chksum); 608 | brelse(bh); 609 | return -EINVAL; 610 | } 611 | } 612 | brelse(bh); 613 | return 0; 614 | } 615 | 616 | /* mount the file system volume */ 617 | static int __exfat_fill_super(struct super_block *sb) 618 | { 619 | int ret; 620 | struct exfat_sb_info *sbi = EXFAT_SB(sb); 621 | 622 | ret = exfat_read_boot_sector(sb); 623 | if (ret) { 624 | exfat_err(sb, "failed to read boot sector"); 625 | goto free_bh; 626 | } 627 | 628 | ret = exfat_verify_boot_region(sb); 629 | if (ret) { 630 | exfat_err(sb, "invalid boot region"); 631 | goto free_bh; 632 | } 633 | 634 | ret = exfat_create_upcase_table(sb); 635 | if (ret) { 636 | exfat_err(sb, "failed to load upcase table"); 637 | goto free_bh; 638 | } 639 | 640 | ret = exfat_load_bitmap(sb); 641 | if (ret) { 642 | exfat_err(sb, "failed to load alloc-bitmap"); 643 | goto free_upcase_table; 644 | } 645 | 646 | ret = exfat_count_used_clusters(sb, &sbi->used_clusters); 647 | if (ret) { 648 | exfat_err(sb, "failed to scan clusters"); 649 | goto free_alloc_bitmap; 650 | } 651 | 652 | return 0; 653 | 654 | free_alloc_bitmap: 655 | exfat_free_bitmap(sbi); 656 | free_upcase_table: 657 | exfat_free_upcase_table(sbi); 658 | free_bh: 659 | brelse(sbi->boot_bh); 660 | return ret; 661 | } 662 | 663 | static int exfat_fill_super(struct super_block *sb, void *data, int silent) 664 | { 665 | struct exfat_sb_info *sbi; 666 | struct exfat_mount_options *opts; 667 | struct inode *root_inode; 668 | int err; 669 | 670 | err = exfat_init_sb_info(sb); 671 | if (err) { 672 | exfat_err(sb, "failed to initialize superblock info"); 673 | goto failed; 674 | } 675 | 676 | sbi = sb->s_fs_info; 677 | opts = &sbi->options; 678 | 679 | sb->s_flags |= SB_NODIRATIME; 680 | sb->s_magic = EXFAT_SUPER_MAGIC; 681 | sb->s_op = &exfat_sops; 682 | sb->s_xattr = exfat_xattr_handlers; 683 | 684 | sb->s_time_gran = 10 * NSEC_PER_MSEC; 685 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0) 686 | sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS; 687 | sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS; 688 | #endif 689 | 690 | err = exfat_parse_options(sb, data, silent, &sbi->options); 691 | if (err) { 692 | exfat_err(sb, "failed to parse options"); 693 | goto check_nls_io; 694 | } 695 | 696 | err = __exfat_fill_super(sb); 697 | if (err) { 698 | exfat_err(sb, "failed to recognize exfat type"); 699 | goto check_nls_io; 700 | } 701 | 702 | /* set up enough so that it can read an inode */ 703 | exfat_hash_init(sb); 704 | 705 | if (!strcmp(sbi->options.iocharset, "utf8")) 706 | opts->utf8 = 1; 707 | else { 708 | sbi->nls_io = load_nls(sbi->options.iocharset); 709 | if (!sbi->nls_io) { 710 | exfat_err(sb, "IO charset %s not found", 711 | sbi->options.iocharset); 712 | err = -EINVAL; 713 | goto free_table; 714 | } 715 | } 716 | 717 | if (sbi->options.utf8) 718 | sb->s_d_op = &exfat_utf8_dentry_ops; 719 | else 720 | sb->s_d_op = &exfat_dentry_ops; 721 | 722 | root_inode = new_inode(sb); 723 | if (!root_inode) { 724 | exfat_err(sb, "failed to allocate root inode"); 725 | err = -ENOMEM; 726 | goto free_table; 727 | } 728 | 729 | root_inode->i_ino = EXFAT_ROOT_INO; 730 | inode_set_iversion(root_inode, 1); 731 | err = exfat_read_root(root_inode); 732 | if (err) { 733 | exfat_err(sb, "failed to initialize root inode"); 734 | goto put_inode; 735 | } 736 | 737 | exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos); 738 | insert_inode_hash(root_inode); 739 | 740 | sb->s_root = d_make_root(root_inode); 741 | if (!sb->s_root) { 742 | exfat_err(sb, "failed to get the root dentry"); 743 | err = -ENOMEM; 744 | goto put_inode; 745 | } 746 | 747 | return 0; 748 | 749 | put_inode: 750 | iput(root_inode); 751 | sb->s_root = NULL; 752 | 753 | free_table: 754 | exfat_free_upcase_table(sbi); 755 | exfat_free_bitmap(sbi); 756 | brelse(sbi->boot_bh); 757 | 758 | check_nls_io: 759 | unload_nls(sbi->nls_io); 760 | exfat_free_iocharset(sbi); 761 | sb->s_fs_info = NULL; 762 | kfree(sbi); 763 | 764 | failed: 765 | return err; 766 | } 767 | 768 | static int exfat_init_sb_info(struct super_block *sb) 769 | { 770 | struct exfat_sb_info *sbi; 771 | 772 | sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL); 773 | if (!sbi) 774 | return -ENOMEM; 775 | 776 | mutex_init(&sbi->s_lock); 777 | ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL, 778 | DEFAULT_RATELIMIT_BURST); 779 | 780 | sbi->options.fs_uid = current_uid(); 781 | sbi->options.fs_gid = current_gid(); 782 | sbi->options.fs_fmask = current->fs->umask; 783 | sbi->options.fs_dmask = current->fs->umask; 784 | sbi->options.allow_utime = -1; 785 | sbi->options.iocharset = exfat_default_iocharset; 786 | sbi->options.errors = EXFAT_ERRORS_RO; 787 | 788 | sb->s_fs_info = sbi; 789 | return 0; 790 | } 791 | 792 | static struct dentry *exfat_fs_mount(struct file_system_type *fs_type, 793 | int flags, const char *dev_name, void *data) 794 | { 795 | return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super); 796 | } 797 | 798 | static struct file_system_type exfat_fs_type = { 799 | .owner = THIS_MODULE, 800 | .name = "exfat", 801 | .mount = exfat_fs_mount, 802 | .kill_sb = kill_block_super, 803 | .fs_flags = FS_REQUIRES_DEV, 804 | }; 805 | 806 | static void exfat_inode_init_once(void *foo) 807 | { 808 | struct exfat_inode_info *ei = (struct exfat_inode_info *)foo; 809 | 810 | INIT_HLIST_NODE(&ei->i_hash_fat); 811 | inode_init_once(&ei->vfs_inode); 812 | } 813 | 814 | static int __init init_exfat_fs(void) 815 | { 816 | int err; 817 | 818 | pr_info("exFAT: file-system version %s\n", EXFAT_VERSION); 819 | 820 | err = exfat_cache_init(); 821 | if (err) 822 | return err; 823 | 824 | exfat_inode_cachep = kmem_cache_create("exfat_inode_cache", 825 | sizeof(struct exfat_inode_info), 826 | 0, SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, 827 | exfat_inode_init_once); 828 | if (!exfat_inode_cachep) { 829 | err = -ENOMEM; 830 | goto shutdown_cache; 831 | } 832 | 833 | err = register_filesystem(&exfat_fs_type); 834 | if (err) 835 | goto destroy_cache; 836 | 837 | return 0; 838 | 839 | destroy_cache: 840 | kmem_cache_destroy(exfat_inode_cachep); 841 | shutdown_cache: 842 | exfat_cache_shutdown(); 843 | return err; 844 | } 845 | 846 | static void __exit exit_exfat_fs(void) 847 | { 848 | /* 849 | * Make sure all delayed rcu free inodes are flushed before we 850 | * destroy cache. 851 | */ 852 | rcu_barrier(); 853 | kmem_cache_destroy(exfat_inode_cachep); 854 | unregister_filesystem(&exfat_fs_type); 855 | exfat_cache_shutdown(); 856 | } 857 | 858 | module_init(init_exfat_fs); 859 | module_exit(exit_exfat_fs); 860 | 861 | MODULE_ALIAS_FS("exfat"); 862 | MODULE_LICENSE("GPL"); 863 | MODULE_DESCRIPTION("exFAT filesystem support"); 864 | MODULE_AUTHOR("Samsung Electronics Co., Ltd."); 865 | -------------------------------------------------------------------------------- /version.h: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0-or-later 2 | 3 | #define EXFAT_BASE_VERSION "5.8" 4 | #define EXFAT_EXTRAVERSION "2" 5 | #define EXFAT_VARIANT "arter97" 6 | #define EXFAT_VERSION EXFAT_BASE_VERSION "-" EXFAT_EXTRAVERSION EXFAT_VARIANT 7 | -------------------------------------------------------------------------------- /xattr.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0-or-later 2 | /* 3 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 | * 5 | * xattr.c: exFAT code for supporting xattr(Extended File Attributes) 6 | */ 7 | 8 | #include "exfat_fs.h" 9 | 10 | #ifdef CONFIG_EXFAT_VIRTUAL_XATTR 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #ifndef CONFIG_EXFAT_VIRTUAL_XATTR_SELINUX_LABEL 18 | #define CONFIG_EXFAT_VIRTUAL_XATTR_SELINUX_LABEL ("undefined") 19 | #endif 20 | 21 | static const char default_xattr[] = CONFIG_EXFAT_VIRTUAL_XATTR_SELINUX_LABEL; 22 | 23 | static int can_support(const char *name) 24 | { 25 | if (!name || strcmp(name, "security.selinux")) 26 | return -1; 27 | return 0; 28 | } 29 | 30 | ssize_t exfat_listxattr(struct dentry *dentry, char *list, size_t size) 31 | { 32 | return 0; 33 | } 34 | 35 | static int __exfat_xattr_check_support(const char *name) 36 | { 37 | if (can_support(name)) 38 | return -EOPNOTSUPP; 39 | 40 | return 0; 41 | } 42 | 43 | ssize_t __exfat_getxattr(const char *name, void *value, size_t size) 44 | { 45 | if (can_support(name)) 46 | return -EOPNOTSUPP; 47 | 48 | if ((size > strlen(default_xattr)+1) && value) 49 | strcpy(value, default_xattr); 50 | 51 | return strlen(default_xattr); 52 | } 53 | 54 | static int exfat_xattr_get(const struct xattr_handler *handler, 55 | struct dentry *dentry, struct inode *inode, 56 | const char *name, void *buffer, size_t size) 57 | { 58 | return __exfat_getxattr(name, buffer, size); 59 | } 60 | 61 | static int exfat_xattr_set(const struct xattr_handler *handler, 62 | struct dentry *dentry, struct inode *inode, 63 | const char *name, const void *value, size_t size, 64 | int flags) 65 | { 66 | return __exfat_xattr_check_support(name); 67 | } 68 | 69 | static const struct xattr_handler exfat_xattr_handler = { 70 | .prefix = "", /* match anything */ 71 | .get = exfat_xattr_get, 72 | .set = exfat_xattr_set, 73 | }; 74 | 75 | const struct xattr_handler *exfat_xattr_handlers[] = { 76 | &exfat_xattr_handler, 77 | NULL 78 | }; 79 | 80 | #endif /* CONFIG_EXFAT_VIRTUAL_XATTR */ 81 | --------------------------------------------------------------------------------