inode.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/fat/inode.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
  7. * Rewritten for the constant inumbers support by Al Viro
  8. *
  9. * Fixes:
  10. *
  11. * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
  12. */
  13. #include <linux/module.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mpage.h>
  16. #include <linux/vfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/uio.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/unaligned.h>
  22. #include <linux/random.h>
  23. #include <linux/iversion.h>
  24. #include "fat.h"
  25. #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
  26. /* if user don't select VFAT, this is undefined. */
  27. #define CONFIG_FAT_DEFAULT_IOCHARSET ""
  28. #endif
  29. #define KB_IN_SECTORS 2
  30. /* DOS dates from 1980/1/1 through 2107/12/31 */
  31. #define FAT_DATE_MIN (0<<9 | 1<<5 | 1)
  32. #define FAT_DATE_MAX (127<<9 | 12<<5 | 31)
  33. #define FAT_TIME_MAX (23<<11 | 59<<5 | 29)
  34. /*
  35. * A deserialized copy of the on-disk structure laid out in struct
  36. * fat_boot_sector.
  37. */
  38. struct fat_bios_param_block {
  39. u16 fat_sector_size;
  40. u8 fat_sec_per_clus;
  41. u16 fat_reserved;
  42. u8 fat_fats;
  43. u16 fat_dir_entries;
  44. u16 fat_sectors;
  45. u16 fat_fat_length;
  46. u32 fat_total_sect;
  47. u8 fat16_state;
  48. u32 fat16_vol_id;
  49. u32 fat32_length;
  50. u32 fat32_root_cluster;
  51. u16 fat32_info_sector;
  52. u8 fat32_state;
  53. u32 fat32_vol_id;
  54. };
  55. static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
  56. static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
  57. static struct fat_floppy_defaults {
  58. unsigned nr_sectors;
  59. unsigned sec_per_clus;
  60. unsigned dir_entries;
  61. unsigned media;
  62. unsigned fat_length;
  63. } floppy_defaults[] = {
  64. {
  65. .nr_sectors = 160 * KB_IN_SECTORS,
  66. .sec_per_clus = 1,
  67. .dir_entries = 64,
  68. .media = 0xFE,
  69. .fat_length = 1,
  70. },
  71. {
  72. .nr_sectors = 180 * KB_IN_SECTORS,
  73. .sec_per_clus = 1,
  74. .dir_entries = 64,
  75. .media = 0xFC,
  76. .fat_length = 2,
  77. },
  78. {
  79. .nr_sectors = 320 * KB_IN_SECTORS,
  80. .sec_per_clus = 2,
  81. .dir_entries = 112,
  82. .media = 0xFF,
  83. .fat_length = 1,
  84. },
  85. {
  86. .nr_sectors = 360 * KB_IN_SECTORS,
  87. .sec_per_clus = 2,
  88. .dir_entries = 112,
  89. .media = 0xFD,
  90. .fat_length = 2,
  91. },
  92. };
  93. int fat_add_cluster(struct inode *inode)
  94. {
  95. int err, cluster;
  96. err = fat_alloc_clusters(inode, &cluster, 1);
  97. if (err)
  98. return err;
  99. /* FIXME: this cluster should be added after data of this
  100. * cluster is writed */
  101. err = fat_chain_add(inode, cluster, 1);
  102. if (err)
  103. fat_free_clusters(inode, cluster);
  104. return err;
  105. }
  106. static inline int __fat_get_block(struct inode *inode, sector_t iblock,
  107. unsigned long *max_blocks,
  108. struct buffer_head *bh_result, int create)
  109. {
  110. struct super_block *sb = inode->i_sb;
  111. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  112. unsigned long mapped_blocks;
  113. sector_t phys, last_block;
  114. int err, offset;
  115. err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
  116. if (err)
  117. return err;
  118. if (phys) {
  119. map_bh(bh_result, sb, phys);
  120. *max_blocks = min(mapped_blocks, *max_blocks);
  121. return 0;
  122. }
  123. if (!create)
  124. return 0;
  125. if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
  126. fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)",
  127. MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
  128. return -EIO;
  129. }
  130. last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9);
  131. offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
  132. /*
  133. * allocate a cluster according to the following.
  134. * 1) no more available blocks
  135. * 2) not part of fallocate region
  136. */
  137. if (!offset && !(iblock < last_block)) {
  138. /* TODO: multiple cluster allocation would be desirable. */
  139. err = fat_add_cluster(inode);
  140. if (err)
  141. return err;
  142. }
  143. /* available blocks on this cluster */
  144. mapped_blocks = sbi->sec_per_clus - offset;
  145. *max_blocks = min(mapped_blocks, *max_blocks);
  146. MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
  147. err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false);
  148. if (err)
  149. return err;
  150. if (!phys) {
  151. fat_fs_error(sb,
  152. "invalid FAT chain (i_pos %lld, last_block %llu)",
  153. MSDOS_I(inode)->i_pos,
  154. (unsigned long long)last_block);
  155. return -EIO;
  156. }
  157. BUG_ON(*max_blocks != mapped_blocks);
  158. set_buffer_new(bh_result);
  159. map_bh(bh_result, sb, phys);
  160. return 0;
  161. }
  162. static int fat_get_block(struct inode *inode, sector_t iblock,
  163. struct buffer_head *bh_result, int create)
  164. {
  165. struct super_block *sb = inode->i_sb;
  166. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  167. int err;
  168. err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
  169. if (err)
  170. return err;
  171. bh_result->b_size = max_blocks << sb->s_blocksize_bits;
  172. return 0;
  173. }
  174. static int fat_writepages(struct address_space *mapping,
  175. struct writeback_control *wbc)
  176. {
  177. return mpage_writepages(mapping, wbc, fat_get_block);
  178. }
  179. static int fat_read_folio(struct file *file, struct folio *folio)
  180. {
  181. return mpage_read_folio(folio, fat_get_block);
  182. }
  183. static void fat_readahead(struct readahead_control *rac)
  184. {
  185. mpage_readahead(rac, fat_get_block);
  186. }
  187. static void fat_write_failed(struct address_space *mapping, loff_t to)
  188. {
  189. struct inode *inode = mapping->host;
  190. if (to > inode->i_size) {
  191. truncate_pagecache(inode, inode->i_size);
  192. fat_truncate_blocks(inode, inode->i_size);
  193. }
  194. }
  195. static int fat_write_begin(struct file *file, struct address_space *mapping,
  196. loff_t pos, unsigned len,
  197. struct folio **foliop, void **fsdata)
  198. {
  199. int err;
  200. err = cont_write_begin(file, mapping, pos, len,
  201. foliop, fsdata, fat_get_block,
  202. &MSDOS_I(mapping->host)->mmu_private);
  203. if (err < 0)
  204. fat_write_failed(mapping, pos + len);
  205. return err;
  206. }
  207. static int fat_write_end(struct file *file, struct address_space *mapping,
  208. loff_t pos, unsigned len, unsigned copied,
  209. struct folio *folio, void *fsdata)
  210. {
  211. struct inode *inode = mapping->host;
  212. int err;
  213. err = generic_write_end(file, mapping, pos, len, copied, folio, fsdata);
  214. if (err < len)
  215. fat_write_failed(mapping, pos + len);
  216. if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
  217. fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
  218. MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
  219. mark_inode_dirty(inode);
  220. }
  221. return err;
  222. }
  223. static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  224. {
  225. struct file *file = iocb->ki_filp;
  226. struct address_space *mapping = file->f_mapping;
  227. struct inode *inode = mapping->host;
  228. size_t count = iov_iter_count(iter);
  229. loff_t offset = iocb->ki_pos;
  230. ssize_t ret;
  231. if (iov_iter_rw(iter) == WRITE) {
  232. /*
  233. * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
  234. * so we need to update the ->mmu_private to block boundary.
  235. *
  236. * But we must fill the remaining area or hole by nul for
  237. * updating ->mmu_private.
  238. *
  239. * Return 0, and fallback to normal buffered write.
  240. */
  241. loff_t size = offset + count;
  242. if (MSDOS_I(inode)->mmu_private < size)
  243. return 0;
  244. }
  245. /*
  246. * FAT need to use the DIO_LOCKING for avoiding the race
  247. * condition of fat_get_block() and ->truncate().
  248. */
  249. ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block);
  250. if (ret < 0 && iov_iter_rw(iter) == WRITE)
  251. fat_write_failed(mapping, offset + count);
  252. return ret;
  253. }
  254. static int fat_get_block_bmap(struct inode *inode, sector_t iblock,
  255. struct buffer_head *bh_result, int create)
  256. {
  257. struct super_block *sb = inode->i_sb;
  258. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  259. int err;
  260. sector_t bmap;
  261. unsigned long mapped_blocks;
  262. BUG_ON(create != 0);
  263. err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true);
  264. if (err)
  265. return err;
  266. if (bmap) {
  267. map_bh(bh_result, sb, bmap);
  268. max_blocks = min(mapped_blocks, max_blocks);
  269. }
  270. bh_result->b_size = max_blocks << sb->s_blocksize_bits;
  271. return 0;
  272. }
  273. static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
  274. {
  275. sector_t blocknr;
  276. /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
  277. down_read(&MSDOS_I(mapping->host)->truncate_lock);
  278. blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap);
  279. up_read(&MSDOS_I(mapping->host)->truncate_lock);
  280. return blocknr;
  281. }
  282. /*
  283. * fat_block_truncate_page() zeroes out a mapping from file offset `from'
  284. * up to the end of the block which corresponds to `from'.
  285. * This is required during truncate to physically zeroout the tail end
  286. * of that block so it doesn't yield old data if the file is later grown.
  287. * Also, avoid causing failure from fsx for cases of "data past EOF"
  288. */
  289. int fat_block_truncate_page(struct inode *inode, loff_t from)
  290. {
  291. return block_truncate_page(inode->i_mapping, from, fat_get_block);
  292. }
  293. static const struct address_space_operations fat_aops = {
  294. .dirty_folio = block_dirty_folio,
  295. .invalidate_folio = block_invalidate_folio,
  296. .read_folio = fat_read_folio,
  297. .readahead = fat_readahead,
  298. .writepages = fat_writepages,
  299. .write_begin = fat_write_begin,
  300. .write_end = fat_write_end,
  301. .direct_IO = fat_direct_IO,
  302. .bmap = _fat_bmap,
  303. .migrate_folio = buffer_migrate_folio,
  304. };
  305. /*
  306. * New FAT inode stuff. We do the following:
  307. * a) i_ino is constant and has nothing with on-disk location.
  308. * b) FAT manages its own cache of directory entries.
  309. * c) *This* cache is indexed by on-disk location.
  310. * d) inode has an associated directory entry, all right, but
  311. * it may be unhashed.
  312. * e) currently entries are stored within struct inode. That should
  313. * change.
  314. * f) we deal with races in the following way:
  315. * 1. readdir() and lookup() do FAT-dir-cache lookup.
  316. * 2. rename() unhashes the F-d-c entry and rehashes it in
  317. * a new place.
  318. * 3. unlink() and rmdir() unhash F-d-c entry.
  319. * 4. fat_write_inode() checks whether the thing is unhashed.
  320. * If it is we silently return. If it isn't we do bread(),
  321. * check if the location is still valid and retry if it
  322. * isn't. Otherwise we do changes.
  323. * 5. Spinlock is used to protect hash/unhash/location check/lookup
  324. * 6. fat_evict_inode() unhashes the F-d-c entry.
  325. * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
  326. * and consider negative result as cache miss.
  327. */
  328. static void fat_hash_init(struct super_block *sb)
  329. {
  330. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  331. int i;
  332. spin_lock_init(&sbi->inode_hash_lock);
  333. for (i = 0; i < FAT_HASH_SIZE; i++)
  334. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  335. }
  336. static inline unsigned long fat_hash(loff_t i_pos)
  337. {
  338. return hash_32(i_pos, FAT_HASH_BITS);
  339. }
  340. static void dir_hash_init(struct super_block *sb)
  341. {
  342. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  343. int i;
  344. spin_lock_init(&sbi->dir_hash_lock);
  345. for (i = 0; i < FAT_HASH_SIZE; i++)
  346. INIT_HLIST_HEAD(&sbi->dir_hashtable[i]);
  347. }
  348. void fat_attach(struct inode *inode, loff_t i_pos)
  349. {
  350. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  351. if (inode->i_ino != MSDOS_ROOT_INO) {
  352. struct hlist_head *head = sbi->inode_hashtable
  353. + fat_hash(i_pos);
  354. spin_lock(&sbi->inode_hash_lock);
  355. MSDOS_I(inode)->i_pos = i_pos;
  356. hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
  357. spin_unlock(&sbi->inode_hash_lock);
  358. }
  359. /* If NFS support is enabled, cache the mapping of start cluster
  360. * to directory inode. This is used during reconnection of
  361. * dentries to the filesystem root.
  362. */
  363. if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
  364. struct hlist_head *d_head = sbi->dir_hashtable;
  365. d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart);
  366. spin_lock(&sbi->dir_hash_lock);
  367. hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head);
  368. spin_unlock(&sbi->dir_hash_lock);
  369. }
  370. }
  371. EXPORT_SYMBOL_GPL(fat_attach);
  372. void fat_detach(struct inode *inode)
  373. {
  374. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  375. spin_lock(&sbi->inode_hash_lock);
  376. MSDOS_I(inode)->i_pos = 0;
  377. hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
  378. spin_unlock(&sbi->inode_hash_lock);
  379. if (S_ISDIR(inode->i_mode) && sbi->options.nfs) {
  380. spin_lock(&sbi->dir_hash_lock);
  381. hlist_del_init(&MSDOS_I(inode)->i_dir_hash);
  382. spin_unlock(&sbi->dir_hash_lock);
  383. }
  384. }
  385. EXPORT_SYMBOL_GPL(fat_detach);
  386. struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
  387. {
  388. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  389. struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
  390. struct msdos_inode_info *i;
  391. struct inode *inode = NULL;
  392. spin_lock(&sbi->inode_hash_lock);
  393. hlist_for_each_entry(i, head, i_fat_hash) {
  394. BUG_ON(i->vfs_inode.i_sb != sb);
  395. if (i->i_pos != i_pos)
  396. continue;
  397. inode = igrab(&i->vfs_inode);
  398. if (inode)
  399. break;
  400. }
  401. spin_unlock(&sbi->inode_hash_lock);
  402. return inode;
  403. }
  404. static int is_exec(unsigned char *extension)
  405. {
  406. unsigned char exe_extensions[] = "EXECOMBAT", *walk;
  407. for (walk = exe_extensions; *walk; walk += 3)
  408. if (!strncmp(extension, walk, 3))
  409. return 1;
  410. return 0;
  411. }
  412. static int fat_calc_dir_size(struct inode *inode)
  413. {
  414. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  415. int ret, fclus, dclus;
  416. inode->i_size = 0;
  417. if (MSDOS_I(inode)->i_start == 0)
  418. return 0;
  419. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  420. if (ret < 0)
  421. return ret;
  422. inode->i_size = (fclus + 1) << sbi->cluster_bits;
  423. return 0;
  424. }
  425. static int fat_validate_dir(struct inode *dir)
  426. {
  427. struct super_block *sb = dir->i_sb;
  428. if (dir->i_nlink < 2) {
  429. /* Directory should have "."/".." entries at least. */
  430. fat_fs_error(sb, "corrupted directory (invalid entries)");
  431. return -EIO;
  432. }
  433. if (MSDOS_I(dir)->i_start == 0 ||
  434. MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) {
  435. /* Directory should point valid cluster. */
  436. fat_fs_error(sb, "corrupted directory (invalid i_start)");
  437. return -EIO;
  438. }
  439. return 0;
  440. }
  441. /* doesn't deal with root inode */
  442. int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
  443. {
  444. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  445. struct timespec64 mtime;
  446. int error;
  447. MSDOS_I(inode)->i_pos = 0;
  448. inode->i_uid = sbi->options.fs_uid;
  449. inode->i_gid = sbi->options.fs_gid;
  450. inode_inc_iversion(inode);
  451. inode->i_generation = get_random_u32();
  452. if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
  453. inode->i_generation &= ~1;
  454. inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
  455. inode->i_op = sbi->dir_ops;
  456. inode->i_fop = &fat_dir_operations;
  457. MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
  458. MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  459. error = fat_calc_dir_size(inode);
  460. if (error < 0)
  461. return error;
  462. MSDOS_I(inode)->mmu_private = inode->i_size;
  463. set_nlink(inode, fat_subdirs(inode));
  464. error = fat_validate_dir(inode);
  465. if (error < 0)
  466. return error;
  467. } else { /* not a directory */
  468. inode->i_generation |= 1;
  469. inode->i_mode = fat_make_mode(sbi, de->attr,
  470. ((sbi->options.showexec && !is_exec(de->name + 8))
  471. ? S_IRUGO|S_IWUGO : S_IRWXUGO));
  472. MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
  473. MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  474. inode->i_size = le32_to_cpu(de->size);
  475. inode->i_op = &fat_file_inode_operations;
  476. inode->i_fop = &fat_file_operations;
  477. inode->i_mapping->a_ops = &fat_aops;
  478. MSDOS_I(inode)->mmu_private = inode->i_size;
  479. }
  480. if (de->attr & ATTR_SYS) {
  481. if (sbi->options.sys_immutable)
  482. inode->i_flags |= S_IMMUTABLE;
  483. }
  484. fat_save_attrs(inode, de->attr);
  485. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  486. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  487. fat_time_fat2unix(sbi, &mtime, de->time, de->date, 0);
  488. inode_set_mtime_to_ts(inode, mtime);
  489. inode_set_ctime_to_ts(inode, mtime);
  490. if (sbi->options.isvfat) {
  491. struct timespec64 atime;
  492. fat_time_fat2unix(sbi, &atime, 0, de->adate, 0);
  493. inode_set_atime_to_ts(inode, atime);
  494. fat_time_fat2unix(sbi, &MSDOS_I(inode)->i_crtime, de->ctime,
  495. de->cdate, de->ctime_cs);
  496. } else
  497. inode_set_atime_to_ts(inode, fat_truncate_atime(sbi, &mtime));
  498. return 0;
  499. }
  500. static inline void fat_lock_build_inode(struct msdos_sb_info *sbi)
  501. {
  502. if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
  503. mutex_lock(&sbi->nfs_build_inode_lock);
  504. }
  505. static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi)
  506. {
  507. if (sbi->options.nfs == FAT_NFS_NOSTALE_RO)
  508. mutex_unlock(&sbi->nfs_build_inode_lock);
  509. }
  510. struct inode *fat_build_inode(struct super_block *sb,
  511. struct msdos_dir_entry *de, loff_t i_pos)
  512. {
  513. struct inode *inode;
  514. int err;
  515. fat_lock_build_inode(MSDOS_SB(sb));
  516. inode = fat_iget(sb, i_pos);
  517. if (inode)
  518. goto out;
  519. inode = new_inode(sb);
  520. if (!inode) {
  521. inode = ERR_PTR(-ENOMEM);
  522. goto out;
  523. }
  524. inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
  525. inode_set_iversion(inode, 1);
  526. err = fat_fill_inode(inode, de);
  527. if (err) {
  528. iput(inode);
  529. inode = ERR_PTR(err);
  530. goto out;
  531. }
  532. fat_attach(inode, i_pos);
  533. insert_inode_hash(inode);
  534. out:
  535. fat_unlock_build_inode(MSDOS_SB(sb));
  536. return inode;
  537. }
  538. EXPORT_SYMBOL_GPL(fat_build_inode);
  539. static int __fat_write_inode(struct inode *inode, int wait);
  540. static void fat_free_eofblocks(struct inode *inode)
  541. {
  542. /* Release unwritten fallocated blocks on inode eviction. */
  543. if ((inode->i_blocks << 9) >
  544. round_up(MSDOS_I(inode)->mmu_private,
  545. MSDOS_SB(inode->i_sb)->cluster_size)) {
  546. int err;
  547. fat_truncate_blocks(inode, MSDOS_I(inode)->mmu_private);
  548. /* Fallocate results in updating the i_start/iogstart
  549. * for the zero byte file. So, make it return to
  550. * original state during evict and commit it to avoid
  551. * any corruption on the next access to the cluster
  552. * chain for the file.
  553. */
  554. err = __fat_write_inode(inode, inode_needs_sync(inode));
  555. if (err) {
  556. fat_msg(inode->i_sb, KERN_WARNING, "Failed to "
  557. "update on disk inode for unused "
  558. "fallocated blocks, inode could be "
  559. "corrupted. Please run fsck");
  560. }
  561. }
  562. }
  563. static void fat_evict_inode(struct inode *inode)
  564. {
  565. truncate_inode_pages_final(&inode->i_data);
  566. if (!inode->i_nlink) {
  567. inode->i_size = 0;
  568. fat_truncate_blocks(inode, 0);
  569. } else
  570. fat_free_eofblocks(inode);
  571. invalidate_inode_buffers(inode);
  572. clear_inode(inode);
  573. fat_cache_inval_inode(inode);
  574. fat_detach(inode);
  575. }
  576. static void fat_set_state(struct super_block *sb,
  577. unsigned int set, unsigned int force)
  578. {
  579. struct buffer_head *bh;
  580. struct fat_boot_sector *b;
  581. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  582. /* do not change any thing if mounted read only */
  583. if (sb_rdonly(sb) && !force)
  584. return;
  585. /* do not change state if fs was dirty */
  586. if (sbi->dirty) {
  587. /* warn only on set (mount). */
  588. if (set)
  589. fat_msg(sb, KERN_WARNING, "Volume was not properly "
  590. "unmounted. Some data may be corrupt. "
  591. "Please run fsck.");
  592. return;
  593. }
  594. bh = sb_bread(sb, 0);
  595. if (bh == NULL) {
  596. fat_msg(sb, KERN_ERR, "unable to read boot sector "
  597. "to mark fs as dirty");
  598. return;
  599. }
  600. b = (struct fat_boot_sector *) bh->b_data;
  601. if (is_fat32(sbi)) {
  602. if (set)
  603. b->fat32.state |= FAT_STATE_DIRTY;
  604. else
  605. b->fat32.state &= ~FAT_STATE_DIRTY;
  606. } else /* fat 16 and 12 */ {
  607. if (set)
  608. b->fat16.state |= FAT_STATE_DIRTY;
  609. else
  610. b->fat16.state &= ~FAT_STATE_DIRTY;
  611. }
  612. mark_buffer_dirty(bh);
  613. sync_dirty_buffer(bh);
  614. brelse(bh);
  615. }
  616. static void fat_reset_iocharset(struct fat_mount_options *opts)
  617. {
  618. if (opts->iocharset != fat_default_iocharset) {
  619. /* Note: opts->iocharset can be NULL here */
  620. kfree(opts->iocharset);
  621. opts->iocharset = fat_default_iocharset;
  622. }
  623. }
  624. static void delayed_free(struct rcu_head *p)
  625. {
  626. struct msdos_sb_info *sbi = container_of(p, struct msdos_sb_info, rcu);
  627. unload_nls(sbi->nls_disk);
  628. unload_nls(sbi->nls_io);
  629. fat_reset_iocharset(&sbi->options);
  630. kfree(sbi);
  631. }
  632. static void fat_put_super(struct super_block *sb)
  633. {
  634. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  635. fat_set_state(sb, 0, 0);
  636. iput(sbi->fsinfo_inode);
  637. iput(sbi->fat_inode);
  638. call_rcu(&sbi->rcu, delayed_free);
  639. }
  640. static struct kmem_cache *fat_inode_cachep;
  641. static struct inode *fat_alloc_inode(struct super_block *sb)
  642. {
  643. struct msdos_inode_info *ei;
  644. ei = alloc_inode_sb(sb, fat_inode_cachep, GFP_NOFS);
  645. if (!ei)
  646. return NULL;
  647. init_rwsem(&ei->truncate_lock);
  648. /* Zeroing to allow iput() even if partial initialized inode. */
  649. ei->mmu_private = 0;
  650. ei->i_start = 0;
  651. ei->i_logstart = 0;
  652. ei->i_attrs = 0;
  653. ei->i_pos = 0;
  654. ei->i_crtime.tv_sec = 0;
  655. ei->i_crtime.tv_nsec = 0;
  656. return &ei->vfs_inode;
  657. }
  658. static void fat_free_inode(struct inode *inode)
  659. {
  660. kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
  661. }
  662. static void init_once(void *foo)
  663. {
  664. struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
  665. spin_lock_init(&ei->cache_lru_lock);
  666. ei->nr_caches = 0;
  667. ei->cache_valid_id = FAT_CACHE_VALID + 1;
  668. INIT_LIST_HEAD(&ei->cache_lru);
  669. INIT_HLIST_NODE(&ei->i_fat_hash);
  670. INIT_HLIST_NODE(&ei->i_dir_hash);
  671. inode_init_once(&ei->vfs_inode);
  672. }
  673. static int __init fat_init_inodecache(void)
  674. {
  675. fat_inode_cachep = kmem_cache_create("fat_inode_cache",
  676. sizeof(struct msdos_inode_info),
  677. 0, (SLAB_RECLAIM_ACCOUNT|
  678. SLAB_ACCOUNT),
  679. init_once);
  680. if (fat_inode_cachep == NULL)
  681. return -ENOMEM;
  682. return 0;
  683. }
  684. static void __exit fat_destroy_inodecache(void)
  685. {
  686. /*
  687. * Make sure all delayed rcu free inodes are flushed before we
  688. * destroy cache.
  689. */
  690. rcu_barrier();
  691. kmem_cache_destroy(fat_inode_cachep);
  692. }
  693. int fat_reconfigure(struct fs_context *fc)
  694. {
  695. bool new_rdonly;
  696. struct super_block *sb = fc->root->d_sb;
  697. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  698. fc->sb_flags |= SB_NODIRATIME | (sbi->options.isvfat ? 0 : SB_NOATIME);
  699. sync_filesystem(sb);
  700. /* make sure we update state on remount. */
  701. new_rdonly = fc->sb_flags & SB_RDONLY;
  702. if (new_rdonly != sb_rdonly(sb)) {
  703. if (new_rdonly)
  704. fat_set_state(sb, 0, 0);
  705. else
  706. fat_set_state(sb, 1, 1);
  707. }
  708. return 0;
  709. }
  710. EXPORT_SYMBOL_GPL(fat_reconfigure);
  711. static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
  712. {
  713. struct super_block *sb = dentry->d_sb;
  714. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  715. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  716. /* If the count of free cluster is still unknown, counts it here. */
  717. if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
  718. int err = fat_count_free_clusters(dentry->d_sb);
  719. if (err)
  720. return err;
  721. }
  722. buf->f_type = dentry->d_sb->s_magic;
  723. buf->f_bsize = sbi->cluster_size;
  724. buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
  725. buf->f_bfree = sbi->free_clusters;
  726. buf->f_bavail = sbi->free_clusters;
  727. buf->f_fsid = u64_to_fsid(id);
  728. buf->f_namelen =
  729. (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
  730. return 0;
  731. }
  732. static int __fat_write_inode(struct inode *inode, int wait)
  733. {
  734. struct super_block *sb = inode->i_sb;
  735. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  736. struct buffer_head *bh;
  737. struct msdos_dir_entry *raw_entry;
  738. struct timespec64 mtime;
  739. loff_t i_pos;
  740. sector_t blocknr;
  741. int err, offset;
  742. if (inode->i_ino == MSDOS_ROOT_INO)
  743. return 0;
  744. retry:
  745. i_pos = fat_i_pos_read(sbi, inode);
  746. if (!i_pos)
  747. return 0;
  748. fat_get_blknr_offset(sbi, i_pos, &blocknr, &offset);
  749. bh = sb_bread(sb, blocknr);
  750. if (!bh) {
  751. fat_msg(sb, KERN_ERR, "unable to read inode block "
  752. "for updating (i_pos %lld)", i_pos);
  753. return -EIO;
  754. }
  755. spin_lock(&sbi->inode_hash_lock);
  756. if (i_pos != MSDOS_I(inode)->i_pos) {
  757. spin_unlock(&sbi->inode_hash_lock);
  758. brelse(bh);
  759. goto retry;
  760. }
  761. raw_entry = &((struct msdos_dir_entry *) (bh->b_data))[offset];
  762. if (S_ISDIR(inode->i_mode))
  763. raw_entry->size = 0;
  764. else
  765. raw_entry->size = cpu_to_le32(inode->i_size);
  766. raw_entry->attr = fat_make_attrs(inode);
  767. fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
  768. mtime = inode_get_mtime(inode);
  769. fat_time_unix2fat(sbi, &mtime, &raw_entry->time,
  770. &raw_entry->date, NULL);
  771. if (sbi->options.isvfat) {
  772. struct timespec64 ts = inode_get_atime(inode);
  773. __le16 atime;
  774. fat_time_unix2fat(sbi, &ts, &atime, &raw_entry->adate, NULL);
  775. fat_time_unix2fat(sbi, &MSDOS_I(inode)->i_crtime, &raw_entry->ctime,
  776. &raw_entry->cdate, &raw_entry->ctime_cs);
  777. }
  778. spin_unlock(&sbi->inode_hash_lock);
  779. mark_buffer_dirty(bh);
  780. err = 0;
  781. if (wait)
  782. err = sync_dirty_buffer(bh);
  783. brelse(bh);
  784. return err;
  785. }
  786. static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
  787. {
  788. int err;
  789. if (inode->i_ino == MSDOS_FSINFO_INO) {
  790. struct super_block *sb = inode->i_sb;
  791. mutex_lock(&MSDOS_SB(sb)->s_lock);
  792. err = fat_clusters_flush(sb);
  793. mutex_unlock(&MSDOS_SB(sb)->s_lock);
  794. } else
  795. err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  796. return err;
  797. }
  798. int fat_sync_inode(struct inode *inode)
  799. {
  800. return __fat_write_inode(inode, 1);
  801. }
  802. EXPORT_SYMBOL_GPL(fat_sync_inode);
  803. static int fat_show_options(struct seq_file *m, struct dentry *root);
  804. static const struct super_operations fat_sops = {
  805. .alloc_inode = fat_alloc_inode,
  806. .free_inode = fat_free_inode,
  807. .write_inode = fat_write_inode,
  808. .evict_inode = fat_evict_inode,
  809. .put_super = fat_put_super,
  810. .statfs = fat_statfs,
  811. .show_options = fat_show_options,
  812. };
  813. static int fat_show_options(struct seq_file *m, struct dentry *root)
  814. {
  815. struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
  816. struct fat_mount_options *opts = &sbi->options;
  817. int isvfat = opts->isvfat;
  818. if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
  819. seq_printf(m, ",uid=%u",
  820. from_kuid_munged(&init_user_ns, opts->fs_uid));
  821. if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
  822. seq_printf(m, ",gid=%u",
  823. from_kgid_munged(&init_user_ns, opts->fs_gid));
  824. seq_printf(m, ",fmask=%04o", opts->fs_fmask);
  825. seq_printf(m, ",dmask=%04o", opts->fs_dmask);
  826. if (opts->allow_utime)
  827. seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
  828. if (sbi->nls_disk)
  829. /* strip "cp" prefix from displayed option */
  830. seq_printf(m, ",codepage=%s", &sbi->nls_disk->charset[2]);
  831. if (isvfat) {
  832. if (sbi->nls_io)
  833. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  834. switch (opts->shortname) {
  835. case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
  836. seq_puts(m, ",shortname=win95");
  837. break;
  838. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
  839. seq_puts(m, ",shortname=winnt");
  840. break;
  841. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
  842. seq_puts(m, ",shortname=mixed");
  843. break;
  844. case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
  845. seq_puts(m, ",shortname=lower");
  846. break;
  847. default:
  848. seq_puts(m, ",shortname=unknown");
  849. break;
  850. }
  851. }
  852. if (opts->name_check != 'n')
  853. seq_printf(m, ",check=%c", opts->name_check);
  854. if (opts->usefree)
  855. seq_puts(m, ",usefree");
  856. if (opts->quiet)
  857. seq_puts(m, ",quiet");
  858. if (opts->showexec)
  859. seq_puts(m, ",showexec");
  860. if (opts->sys_immutable)
  861. seq_puts(m, ",sys_immutable");
  862. if (!isvfat) {
  863. if (opts->dotsOK)
  864. seq_puts(m, ",dotsOK=yes");
  865. if (opts->nocase)
  866. seq_puts(m, ",nocase");
  867. } else {
  868. if (opts->utf8)
  869. seq_puts(m, ",utf8");
  870. if (opts->unicode_xlate)
  871. seq_puts(m, ",uni_xlate");
  872. if (!opts->numtail)
  873. seq_puts(m, ",nonumtail");
  874. if (opts->rodir)
  875. seq_puts(m, ",rodir");
  876. }
  877. if (opts->flush)
  878. seq_puts(m, ",flush");
  879. if (opts->tz_set) {
  880. if (opts->time_offset)
  881. seq_printf(m, ",time_offset=%d", opts->time_offset);
  882. else
  883. seq_puts(m, ",tz=UTC");
  884. }
  885. if (opts->errors == FAT_ERRORS_CONT)
  886. seq_puts(m, ",errors=continue");
  887. else if (opts->errors == FAT_ERRORS_PANIC)
  888. seq_puts(m, ",errors=panic");
  889. else
  890. seq_puts(m, ",errors=remount-ro");
  891. if (opts->nfs == FAT_NFS_NOSTALE_RO)
  892. seq_puts(m, ",nfs=nostale_ro");
  893. else if (opts->nfs)
  894. seq_puts(m, ",nfs=stale_rw");
  895. if (opts->discard)
  896. seq_puts(m, ",discard");
  897. if (opts->dos1xfloppy)
  898. seq_puts(m, ",dos1xfloppy");
  899. return 0;
  900. }
  901. enum {
  902. Opt_check, Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask,
  903. Opt_allow_utime, Opt_codepage, Opt_usefree, Opt_nocase, Opt_quiet,
  904. Opt_showexec, Opt_debug, Opt_immutable, Opt_dots, Opt_dotsOK,
  905. Opt_charset, Opt_shortname, Opt_utf8, Opt_utf8_bool,
  906. Opt_uni_xl, Opt_uni_xl_bool, Opt_nonumtail, Opt_nonumtail_bool,
  907. Opt_obsolete, Opt_flush, Opt_tz, Opt_rodir, Opt_errors, Opt_discard,
  908. Opt_nfs, Opt_nfs_enum, Opt_time_offset, Opt_dos1xfloppy,
  909. };
  910. static const struct constant_table fat_param_check[] = {
  911. {"relaxed", 'r'},
  912. {"r", 'r'},
  913. {"strict", 's'},
  914. {"s", 's'},
  915. {"normal", 'n'},
  916. {"n", 'n'},
  917. {}
  918. };
  919. static const struct constant_table fat_param_tz[] = {
  920. {"UTC", 0},
  921. {}
  922. };
  923. static const struct constant_table fat_param_errors[] = {
  924. {"continue", FAT_ERRORS_CONT},
  925. {"panic", FAT_ERRORS_PANIC},
  926. {"remount-ro", FAT_ERRORS_RO},
  927. {}
  928. };
  929. static const struct constant_table fat_param_nfs[] = {
  930. {"stale_rw", FAT_NFS_STALE_RW},
  931. {"nostale_ro", FAT_NFS_NOSTALE_RO},
  932. {}
  933. };
  934. /*
  935. * These are all obsolete but we still reject invalid options.
  936. * The corresponding values are therefore meaningless.
  937. */
  938. static const struct constant_table fat_param_conv[] = {
  939. {"binary", 0},
  940. {"text", 0},
  941. {"auto", 0},
  942. {"b", 0},
  943. {"t", 0},
  944. {"a", 0},
  945. {}
  946. };
  947. /* Core options. See below for vfat and msdos extras */
  948. const struct fs_parameter_spec fat_param_spec[] = {
  949. fsparam_enum ("check", Opt_check, fat_param_check),
  950. fsparam_uid ("uid", Opt_uid),
  951. fsparam_gid ("gid", Opt_gid),
  952. fsparam_u32oct ("umask", Opt_umask),
  953. fsparam_u32oct ("dmask", Opt_dmask),
  954. fsparam_u32oct ("fmask", Opt_fmask),
  955. fsparam_u32oct ("allow_utime", Opt_allow_utime),
  956. fsparam_u32 ("codepage", Opt_codepage),
  957. fsparam_flag ("usefree", Opt_usefree),
  958. fsparam_flag ("nocase", Opt_nocase),
  959. fsparam_flag ("quiet", Opt_quiet),
  960. fsparam_flag ("showexec", Opt_showexec),
  961. fsparam_flag ("debug", Opt_debug),
  962. fsparam_flag ("sys_immutable", Opt_immutable),
  963. fsparam_flag ("flush", Opt_flush),
  964. fsparam_enum ("tz", Opt_tz, fat_param_tz),
  965. fsparam_s32 ("time_offset", Opt_time_offset),
  966. fsparam_enum ("errors", Opt_errors, fat_param_errors),
  967. fsparam_flag ("discard", Opt_discard),
  968. fsparam_flag ("nfs", Opt_nfs),
  969. fsparam_enum ("nfs", Opt_nfs_enum, fat_param_nfs),
  970. fsparam_flag ("dos1xfloppy", Opt_dos1xfloppy),
  971. __fsparam(fs_param_is_enum, "conv",
  972. Opt_obsolete, fs_param_deprecated, fat_param_conv),
  973. __fsparam(fs_param_is_u32, "fat",
  974. Opt_obsolete, fs_param_deprecated, NULL),
  975. __fsparam(fs_param_is_u32, "blocksize",
  976. Opt_obsolete, fs_param_deprecated, NULL),
  977. __fsparam(fs_param_is_string, "cvf_format",
  978. Opt_obsolete, fs_param_deprecated, NULL),
  979. __fsparam(fs_param_is_string, "cvf_options",
  980. Opt_obsolete, fs_param_deprecated, NULL),
  981. __fsparam(NULL, "posix",
  982. Opt_obsolete, fs_param_deprecated, NULL),
  983. {}
  984. };
  985. EXPORT_SYMBOL_GPL(fat_param_spec);
  986. static const struct fs_parameter_spec msdos_param_spec[] = {
  987. fsparam_flag_no ("dots", Opt_dots),
  988. fsparam_bool ("dotsOK", Opt_dotsOK),
  989. {}
  990. };
  991. static const struct constant_table fat_param_shortname[] = {
  992. {"lower", VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95},
  993. {"win95", VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95},
  994. {"winnt", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT},
  995. {"mixed", VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95},
  996. {}
  997. };
  998. static const struct fs_parameter_spec vfat_param_spec[] = {
  999. fsparam_string ("iocharset", Opt_charset),
  1000. fsparam_enum ("shortname", Opt_shortname, fat_param_shortname),
  1001. fsparam_flag ("utf8", Opt_utf8),
  1002. fsparam_bool ("utf8", Opt_utf8_bool),
  1003. fsparam_flag ("uni_xlate", Opt_uni_xl),
  1004. fsparam_bool ("uni_xlate", Opt_uni_xl_bool),
  1005. fsparam_flag ("nonumtail", Opt_nonumtail),
  1006. fsparam_bool ("nonumtail", Opt_nonumtail_bool),
  1007. fsparam_flag ("rodir", Opt_rodir),
  1008. {}
  1009. };
  1010. int fat_parse_param(struct fs_context *fc, struct fs_parameter *param,
  1011. bool is_vfat)
  1012. {
  1013. struct fat_mount_options *opts = fc->fs_private;
  1014. struct fs_parse_result result;
  1015. int opt;
  1016. /* remount options have traditionally been ignored */
  1017. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
  1018. return 0;
  1019. opt = fs_parse(fc, fat_param_spec, param, &result);
  1020. /* If option not found in fat_param_spec, try vfat/msdos options */
  1021. if (opt == -ENOPARAM) {
  1022. if (is_vfat)
  1023. opt = fs_parse(fc, vfat_param_spec, param, &result);
  1024. else
  1025. opt = fs_parse(fc, msdos_param_spec, param, &result);
  1026. }
  1027. if (opt < 0)
  1028. return opt;
  1029. switch (opt) {
  1030. case Opt_check:
  1031. opts->name_check = result.uint_32;
  1032. break;
  1033. case Opt_usefree:
  1034. opts->usefree = 1;
  1035. break;
  1036. case Opt_nocase:
  1037. if (!is_vfat)
  1038. opts->nocase = 1;
  1039. else {
  1040. /* for backward compatibility */
  1041. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  1042. | VFAT_SFN_CREATE_WIN95;
  1043. }
  1044. break;
  1045. case Opt_quiet:
  1046. opts->quiet = 1;
  1047. break;
  1048. case Opt_showexec:
  1049. opts->showexec = 1;
  1050. break;
  1051. case Opt_debug:
  1052. opts->debug = 1;
  1053. break;
  1054. case Opt_immutable:
  1055. opts->sys_immutable = 1;
  1056. break;
  1057. case Opt_uid:
  1058. opts->fs_uid = result.uid;
  1059. break;
  1060. case Opt_gid:
  1061. opts->fs_gid = result.gid;
  1062. break;
  1063. case Opt_umask:
  1064. opts->fs_fmask = opts->fs_dmask = result.uint_32;
  1065. break;
  1066. case Opt_dmask:
  1067. opts->fs_dmask = result.uint_32;
  1068. break;
  1069. case Opt_fmask:
  1070. opts->fs_fmask = result.uint_32;
  1071. break;
  1072. case Opt_allow_utime:
  1073. opts->allow_utime = result.uint_32 & (S_IWGRP | S_IWOTH);
  1074. break;
  1075. case Opt_codepage:
  1076. opts->codepage = result.uint_32;
  1077. break;
  1078. case Opt_flush:
  1079. opts->flush = 1;
  1080. break;
  1081. case Opt_time_offset:
  1082. /*
  1083. * GMT+-12 zones may have DST corrections so at least
  1084. * 13 hours difference is needed. Make the limit 24
  1085. * just in case someone invents something unusual.
  1086. */
  1087. if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
  1088. return -EINVAL;
  1089. opts->tz_set = 1;
  1090. opts->time_offset = result.int_32;
  1091. break;
  1092. case Opt_tz:
  1093. opts->tz_set = 1;
  1094. opts->time_offset = result.uint_32;
  1095. break;
  1096. case Opt_errors:
  1097. opts->errors = result.uint_32;
  1098. break;
  1099. case Opt_nfs:
  1100. opts->nfs = FAT_NFS_STALE_RW;
  1101. break;
  1102. case Opt_nfs_enum:
  1103. opts->nfs = result.uint_32;
  1104. break;
  1105. case Opt_dos1xfloppy:
  1106. opts->dos1xfloppy = 1;
  1107. break;
  1108. /* msdos specific */
  1109. case Opt_dots: /* dots / nodots */
  1110. opts->dotsOK = !result.negated;
  1111. break;
  1112. case Opt_dotsOK: /* dotsOK = yes/no */
  1113. opts->dotsOK = result.boolean;
  1114. break;
  1115. /* vfat specific */
  1116. case Opt_charset:
  1117. fat_reset_iocharset(opts);
  1118. opts->iocharset = param->string;
  1119. param->string = NULL; /* Steal string */
  1120. break;
  1121. case Opt_shortname:
  1122. opts->shortname = result.uint_32;
  1123. break;
  1124. case Opt_utf8:
  1125. opts->utf8 = 1;
  1126. break;
  1127. case Opt_utf8_bool:
  1128. opts->utf8 = result.boolean;
  1129. break;
  1130. case Opt_uni_xl:
  1131. opts->unicode_xlate = 1;
  1132. break;
  1133. case Opt_uni_xl_bool:
  1134. opts->unicode_xlate = result.boolean;
  1135. break;
  1136. case Opt_nonumtail:
  1137. opts->numtail = 0; /* negated option */
  1138. break;
  1139. case Opt_nonumtail_bool:
  1140. opts->numtail = !result.boolean; /* negated option */
  1141. break;
  1142. case Opt_rodir:
  1143. opts->rodir = 1;
  1144. break;
  1145. case Opt_discard:
  1146. opts->discard = 1;
  1147. break;
  1148. /* obsolete mount options */
  1149. case Opt_obsolete:
  1150. printk(KERN_INFO "FAT-fs: \"%s\" option is obsolete, "
  1151. "not supported now", param->key);
  1152. break;
  1153. default:
  1154. return -EINVAL;
  1155. }
  1156. return 0;
  1157. }
  1158. EXPORT_SYMBOL_GPL(fat_parse_param);
  1159. static int fat_read_root(struct inode *inode)
  1160. {
  1161. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  1162. int error;
  1163. MSDOS_I(inode)->i_pos = MSDOS_ROOT_INO;
  1164. inode->i_uid = sbi->options.fs_uid;
  1165. inode->i_gid = sbi->options.fs_gid;
  1166. inode_inc_iversion(inode);
  1167. inode->i_generation = 0;
  1168. inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
  1169. inode->i_op = sbi->dir_ops;
  1170. inode->i_fop = &fat_dir_operations;
  1171. if (is_fat32(sbi)) {
  1172. MSDOS_I(inode)->i_start = sbi->root_cluster;
  1173. error = fat_calc_dir_size(inode);
  1174. if (error < 0)
  1175. return error;
  1176. } else {
  1177. MSDOS_I(inode)->i_start = 0;
  1178. inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
  1179. }
  1180. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  1181. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  1182. MSDOS_I(inode)->i_logstart = 0;
  1183. MSDOS_I(inode)->mmu_private = inode->i_size;
  1184. fat_save_attrs(inode, ATTR_DIR);
  1185. inode_set_mtime_to_ts(inode,
  1186. inode_set_atime_to_ts(inode, inode_set_ctime(inode, 0, 0)));
  1187. set_nlink(inode, fat_subdirs(inode)+2);
  1188. return 0;
  1189. }
  1190. static unsigned long calc_fat_clusters(struct super_block *sb)
  1191. {
  1192. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1193. /* Divide first to avoid overflow */
  1194. if (!is_fat12(sbi)) {
  1195. unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
  1196. return ent_per_sec * sbi->fat_length;
  1197. }
  1198. return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
  1199. }
  1200. static bool fat_bpb_is_zero(struct fat_boot_sector *b)
  1201. {
  1202. if (get_unaligned_le16(&b->sector_size))
  1203. return false;
  1204. if (b->sec_per_clus)
  1205. return false;
  1206. if (b->reserved)
  1207. return false;
  1208. if (b->fats)
  1209. return false;
  1210. if (get_unaligned_le16(&b->dir_entries))
  1211. return false;
  1212. if (get_unaligned_le16(&b->sectors))
  1213. return false;
  1214. if (b->media)
  1215. return false;
  1216. if (b->fat_length)
  1217. return false;
  1218. if (b->secs_track)
  1219. return false;
  1220. if (b->heads)
  1221. return false;
  1222. return true;
  1223. }
  1224. static int fat_read_bpb(struct super_block *sb, struct fat_boot_sector *b,
  1225. int silent, struct fat_bios_param_block *bpb)
  1226. {
  1227. int error = -EINVAL;
  1228. /* Read in BPB ... */
  1229. memset(bpb, 0, sizeof(*bpb));
  1230. bpb->fat_sector_size = get_unaligned_le16(&b->sector_size);
  1231. bpb->fat_sec_per_clus = b->sec_per_clus;
  1232. bpb->fat_reserved = le16_to_cpu(b->reserved);
  1233. bpb->fat_fats = b->fats;
  1234. bpb->fat_dir_entries = get_unaligned_le16(&b->dir_entries);
  1235. bpb->fat_sectors = get_unaligned_le16(&b->sectors);
  1236. bpb->fat_fat_length = le16_to_cpu(b->fat_length);
  1237. bpb->fat_total_sect = le32_to_cpu(b->total_sect);
  1238. bpb->fat16_state = b->fat16.state;
  1239. bpb->fat16_vol_id = get_unaligned_le32(b->fat16.vol_id);
  1240. bpb->fat32_length = le32_to_cpu(b->fat32.length);
  1241. bpb->fat32_root_cluster = le32_to_cpu(b->fat32.root_cluster);
  1242. bpb->fat32_info_sector = le16_to_cpu(b->fat32.info_sector);
  1243. bpb->fat32_state = b->fat32.state;
  1244. bpb->fat32_vol_id = get_unaligned_le32(b->fat32.vol_id);
  1245. /* Validate this looks like a FAT filesystem BPB */
  1246. if (!bpb->fat_reserved) {
  1247. if (!silent)
  1248. fat_msg(sb, KERN_ERR,
  1249. "bogus number of reserved sectors");
  1250. goto out;
  1251. }
  1252. if (!bpb->fat_fats) {
  1253. if (!silent)
  1254. fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
  1255. goto out;
  1256. }
  1257. /*
  1258. * Earlier we checked here that b->secs_track and b->head are nonzero,
  1259. * but it turns out valid FAT filesystems can have zero there.
  1260. */
  1261. if (!fat_valid_media(b->media)) {
  1262. if (!silent)
  1263. fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
  1264. (unsigned)b->media);
  1265. goto out;
  1266. }
  1267. if (!is_power_of_2(bpb->fat_sector_size)
  1268. || (bpb->fat_sector_size < 512)
  1269. || (bpb->fat_sector_size > 4096)) {
  1270. if (!silent)
  1271. fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
  1272. (unsigned)bpb->fat_sector_size);
  1273. goto out;
  1274. }
  1275. if (!is_power_of_2(bpb->fat_sec_per_clus)) {
  1276. if (!silent)
  1277. fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
  1278. (unsigned)bpb->fat_sec_per_clus);
  1279. goto out;
  1280. }
  1281. if (bpb->fat_fat_length == 0 && bpb->fat32_length == 0) {
  1282. if (!silent)
  1283. fat_msg(sb, KERN_ERR, "bogus number of FAT sectors");
  1284. goto out;
  1285. }
  1286. error = 0;
  1287. out:
  1288. return error;
  1289. }
  1290. static int fat_read_static_bpb(struct super_block *sb,
  1291. struct fat_boot_sector *b, int silent,
  1292. struct fat_bios_param_block *bpb)
  1293. {
  1294. static const char *notdos1x = "This doesn't look like a DOS 1.x volume";
  1295. sector_t bd_sects = bdev_nr_sectors(sb->s_bdev);
  1296. struct fat_floppy_defaults *fdefaults = NULL;
  1297. int error = -EINVAL;
  1298. unsigned i;
  1299. /* 16-bit DOS 1.x reliably wrote bootstrap short-jmp code */
  1300. if (b->ignored[0] != 0xeb || b->ignored[2] != 0x90) {
  1301. if (!silent)
  1302. fat_msg(sb, KERN_ERR,
  1303. "%s; no bootstrapping code", notdos1x);
  1304. goto out;
  1305. }
  1306. /*
  1307. * If any value in this region is non-zero, it isn't archaic
  1308. * DOS.
  1309. */
  1310. if (!fat_bpb_is_zero(b)) {
  1311. if (!silent)
  1312. fat_msg(sb, KERN_ERR,
  1313. "%s; DOS 2.x BPB is non-zero", notdos1x);
  1314. goto out;
  1315. }
  1316. for (i = 0; i < ARRAY_SIZE(floppy_defaults); i++) {
  1317. if (floppy_defaults[i].nr_sectors == bd_sects) {
  1318. fdefaults = &floppy_defaults[i];
  1319. break;
  1320. }
  1321. }
  1322. if (fdefaults == NULL) {
  1323. if (!silent)
  1324. fat_msg(sb, KERN_WARNING,
  1325. "This looks like a DOS 1.x volume, but isn't a recognized floppy size (%llu sectors)",
  1326. (u64)bd_sects);
  1327. goto out;
  1328. }
  1329. if (!silent)
  1330. fat_msg(sb, KERN_INFO,
  1331. "This looks like a DOS 1.x volume; assuming default BPB values");
  1332. memset(bpb, 0, sizeof(*bpb));
  1333. bpb->fat_sector_size = SECTOR_SIZE;
  1334. bpb->fat_sec_per_clus = fdefaults->sec_per_clus;
  1335. bpb->fat_reserved = 1;
  1336. bpb->fat_fats = 2;
  1337. bpb->fat_dir_entries = fdefaults->dir_entries;
  1338. bpb->fat_sectors = fdefaults->nr_sectors;
  1339. bpb->fat_fat_length = fdefaults->fat_length;
  1340. error = 0;
  1341. out:
  1342. return error;
  1343. }
  1344. /*
  1345. * Read the super block of an MS-DOS FS.
  1346. */
  1347. int fat_fill_super(struct super_block *sb, struct fs_context *fc,
  1348. void (*setup)(struct super_block *))
  1349. {
  1350. struct fat_mount_options *opts = fc->fs_private;
  1351. int silent = fc->sb_flags & SB_SILENT;
  1352. struct inode *root_inode = NULL, *fat_inode = NULL;
  1353. struct inode *fsinfo_inode = NULL;
  1354. struct buffer_head *bh;
  1355. struct fat_bios_param_block bpb;
  1356. struct msdos_sb_info *sbi;
  1357. u16 logical_sector_size;
  1358. u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
  1359. long error;
  1360. char buf[50];
  1361. struct timespec64 ts;
  1362. /*
  1363. * GFP_KERNEL is ok here, because while we do hold the
  1364. * superblock lock, memory pressure can't call back into
  1365. * the filesystem, since we're only just about to mount
  1366. * it and have no inodes etc active!
  1367. */
  1368. sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
  1369. if (!sbi)
  1370. return -ENOMEM;
  1371. sb->s_fs_info = sbi;
  1372. sb->s_flags |= SB_NODIRATIME;
  1373. sb->s_magic = MSDOS_SUPER_MAGIC;
  1374. sb->s_op = &fat_sops;
  1375. sb->s_export_op = &fat_export_ops;
  1376. /*
  1377. * fat timestamps are complex and truncated by fat itself, so
  1378. * we set 1 here to be fast
  1379. */
  1380. sb->s_time_gran = 1;
  1381. mutex_init(&sbi->nfs_build_inode_lock);
  1382. ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
  1383. DEFAULT_RATELIMIT_BURST);
  1384. /* UTF-8 doesn't provide FAT semantics */
  1385. if (!strcmp(opts->iocharset, "utf8")) {
  1386. fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
  1387. " for FAT filesystems, filesystem will be"
  1388. " case sensitive!");
  1389. }
  1390. /* If user doesn't specify allow_utime, it's initialized from dmask. */
  1391. if (opts->allow_utime == (unsigned short)-1)
  1392. opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
  1393. if (opts->unicode_xlate)
  1394. opts->utf8 = 0;
  1395. if (opts->nfs == FAT_NFS_NOSTALE_RO) {
  1396. sb->s_flags |= SB_RDONLY;
  1397. sb->s_export_op = &fat_export_ops_nostale;
  1398. }
  1399. /* Apply parsed options to sbi (structure copy) */
  1400. sbi->options = *opts;
  1401. /* Transfer ownership of iocharset to sbi->options */
  1402. opts->iocharset = NULL;
  1403. setup(sb); /* flavour-specific stuff that needs options */
  1404. error = -EIO;
  1405. sb_min_blocksize(sb, 512);
  1406. bh = sb_bread(sb, 0);
  1407. if (bh == NULL) {
  1408. fat_msg(sb, KERN_ERR, "unable to read boot sector");
  1409. goto out_fail;
  1410. }
  1411. error = fat_read_bpb(sb, (struct fat_boot_sector *)bh->b_data, silent,
  1412. &bpb);
  1413. if (error == -EINVAL && sbi->options.dos1xfloppy)
  1414. error = fat_read_static_bpb(sb,
  1415. (struct fat_boot_sector *)bh->b_data, silent, &bpb);
  1416. brelse(bh);
  1417. if (error == -EINVAL)
  1418. goto out_invalid;
  1419. else if (error)
  1420. goto out_fail;
  1421. logical_sector_size = bpb.fat_sector_size;
  1422. sbi->sec_per_clus = bpb.fat_sec_per_clus;
  1423. error = -EIO;
  1424. if (logical_sector_size < sb->s_blocksize) {
  1425. fat_msg(sb, KERN_ERR, "logical sector size too small for device"
  1426. " (logical sector size = %u)", logical_sector_size);
  1427. goto out_fail;
  1428. }
  1429. if (logical_sector_size > sb->s_blocksize) {
  1430. struct buffer_head *bh_resize;
  1431. if (!sb_set_blocksize(sb, logical_sector_size)) {
  1432. fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
  1433. logical_sector_size);
  1434. goto out_fail;
  1435. }
  1436. /* Verify that the larger boot sector is fully readable */
  1437. bh_resize = sb_bread(sb, 0);
  1438. if (bh_resize == NULL) {
  1439. fat_msg(sb, KERN_ERR, "unable to read boot sector"
  1440. " (logical sector size = %lu)",
  1441. sb->s_blocksize);
  1442. goto out_fail;
  1443. }
  1444. brelse(bh_resize);
  1445. }
  1446. mutex_init(&sbi->s_lock);
  1447. sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
  1448. sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
  1449. sbi->fats = bpb.fat_fats;
  1450. sbi->fat_bits = 0; /* Don't know yet */
  1451. sbi->fat_start = bpb.fat_reserved;
  1452. sbi->fat_length = bpb.fat_fat_length;
  1453. sbi->root_cluster = 0;
  1454. sbi->free_clusters = -1; /* Don't know yet */
  1455. sbi->free_clus_valid = 0;
  1456. sbi->prev_free = FAT_START_ENT;
  1457. sb->s_maxbytes = 0xffffffff;
  1458. fat_time_fat2unix(sbi, &ts, 0, cpu_to_le16(FAT_DATE_MIN), 0);
  1459. sb->s_time_min = ts.tv_sec;
  1460. fat_time_fat2unix(sbi, &ts, cpu_to_le16(FAT_TIME_MAX),
  1461. cpu_to_le16(FAT_DATE_MAX), 0);
  1462. sb->s_time_max = ts.tv_sec;
  1463. if (!sbi->fat_length && bpb.fat32_length) {
  1464. struct fat_boot_fsinfo *fsinfo;
  1465. struct buffer_head *fsinfo_bh;
  1466. /* Must be FAT32 */
  1467. sbi->fat_bits = 32;
  1468. sbi->fat_length = bpb.fat32_length;
  1469. sbi->root_cluster = bpb.fat32_root_cluster;
  1470. /* MC - if info_sector is 0, don't multiply by 0 */
  1471. sbi->fsinfo_sector = bpb.fat32_info_sector;
  1472. if (sbi->fsinfo_sector == 0)
  1473. sbi->fsinfo_sector = 1;
  1474. fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
  1475. if (fsinfo_bh == NULL) {
  1476. fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
  1477. " (sector = %lu)", sbi->fsinfo_sector);
  1478. goto out_fail;
  1479. }
  1480. fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
  1481. if (!IS_FSINFO(fsinfo)) {
  1482. fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
  1483. "0x%08x, 0x%08x (sector = %lu)",
  1484. le32_to_cpu(fsinfo->signature1),
  1485. le32_to_cpu(fsinfo->signature2),
  1486. sbi->fsinfo_sector);
  1487. } else {
  1488. if (sbi->options.usefree)
  1489. sbi->free_clus_valid = 1;
  1490. sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
  1491. sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
  1492. }
  1493. brelse(fsinfo_bh);
  1494. }
  1495. /* interpret volume ID as a little endian 32 bit integer */
  1496. if (is_fat32(sbi))
  1497. sbi->vol_id = bpb.fat32_vol_id;
  1498. else /* fat 16 or 12 */
  1499. sbi->vol_id = bpb.fat16_vol_id;
  1500. __le32 vol_id_le = cpu_to_le32(sbi->vol_id);
  1501. super_set_uuid(sb, (void *) &vol_id_le, sizeof(vol_id_le));
  1502. sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
  1503. sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
  1504. sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
  1505. sbi->dir_entries = bpb.fat_dir_entries;
  1506. if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
  1507. if (!silent)
  1508. fat_msg(sb, KERN_ERR, "bogus number of directory entries"
  1509. " (%u)", sbi->dir_entries);
  1510. goto out_invalid;
  1511. }
  1512. rootdir_sectors = sbi->dir_entries
  1513. * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
  1514. sbi->data_start = sbi->dir_start + rootdir_sectors;
  1515. total_sectors = bpb.fat_sectors;
  1516. if (total_sectors == 0)
  1517. total_sectors = bpb.fat_total_sect;
  1518. total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
  1519. if (!is_fat32(sbi))
  1520. sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
  1521. /* some OSes set FAT_STATE_DIRTY and clean it on unmount. */
  1522. if (is_fat32(sbi))
  1523. sbi->dirty = bpb.fat32_state & FAT_STATE_DIRTY;
  1524. else /* fat 16 or 12 */
  1525. sbi->dirty = bpb.fat16_state & FAT_STATE_DIRTY;
  1526. /* check that FAT table does not overflow */
  1527. fat_clusters = calc_fat_clusters(sb);
  1528. total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
  1529. if (total_clusters > max_fat(sb)) {
  1530. if (!silent)
  1531. fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
  1532. total_clusters);
  1533. goto out_invalid;
  1534. }
  1535. sbi->max_cluster = total_clusters + FAT_START_ENT;
  1536. /* check the free_clusters, it's not necessarily correct */
  1537. if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
  1538. sbi->free_clusters = -1;
  1539. /* check the prev_free, it's not necessarily correct */
  1540. sbi->prev_free %= sbi->max_cluster;
  1541. if (sbi->prev_free < FAT_START_ENT)
  1542. sbi->prev_free = FAT_START_ENT;
  1543. /* set up enough so that it can read an inode */
  1544. fat_hash_init(sb);
  1545. dir_hash_init(sb);
  1546. fat_ent_access_init(sb);
  1547. /*
  1548. * The low byte of the first FAT entry must have the same value as
  1549. * the media field of the boot sector. But in real world, too many
  1550. * devices are writing wrong values. So, removed that validity check.
  1551. *
  1552. * The removed check compared the first FAT entry to a value dependent
  1553. * on the media field like this:
  1554. * == (0x0F00 | media), for FAT12
  1555. * == (0XFF00 | media), for FAT16
  1556. * == (0x0FFFFF | media), for FAT32
  1557. */
  1558. error = -EINVAL;
  1559. sprintf(buf, "cp%d", sbi->options.codepage);
  1560. sbi->nls_disk = load_nls(buf);
  1561. if (!sbi->nls_disk) {
  1562. fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
  1563. goto out_fail;
  1564. }
  1565. /* FIXME: utf8 is using iocharset for upper/lower conversion */
  1566. if (sbi->options.isvfat) {
  1567. sbi->nls_io = load_nls(sbi->options.iocharset);
  1568. if (!sbi->nls_io) {
  1569. fat_msg(sb, KERN_ERR, "IO charset %s not found",
  1570. sbi->options.iocharset);
  1571. goto out_fail;
  1572. }
  1573. }
  1574. error = -ENOMEM;
  1575. fat_inode = new_inode(sb);
  1576. if (!fat_inode)
  1577. goto out_fail;
  1578. sbi->fat_inode = fat_inode;
  1579. fsinfo_inode = new_inode(sb);
  1580. if (!fsinfo_inode)
  1581. goto out_fail;
  1582. fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
  1583. sbi->fsinfo_inode = fsinfo_inode;
  1584. insert_inode_hash(fsinfo_inode);
  1585. root_inode = new_inode(sb);
  1586. if (!root_inode)
  1587. goto out_fail;
  1588. root_inode->i_ino = MSDOS_ROOT_INO;
  1589. inode_set_iversion(root_inode, 1);
  1590. error = fat_read_root(root_inode);
  1591. if (error < 0) {
  1592. iput(root_inode);
  1593. goto out_fail;
  1594. }
  1595. error = -ENOMEM;
  1596. insert_inode_hash(root_inode);
  1597. fat_attach(root_inode, 0);
  1598. sb->s_root = d_make_root(root_inode);
  1599. if (!sb->s_root) {
  1600. fat_msg(sb, KERN_ERR, "get root inode failed");
  1601. goto out_fail;
  1602. }
  1603. if (sbi->options.discard && !bdev_max_discard_sectors(sb->s_bdev))
  1604. fat_msg(sb, KERN_WARNING,
  1605. "mounting with \"discard\" option, but the device does not support discard");
  1606. fat_set_state(sb, 1, 0);
  1607. return 0;
  1608. out_invalid:
  1609. error = -EINVAL;
  1610. if (!silent)
  1611. fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
  1612. out_fail:
  1613. iput(fsinfo_inode);
  1614. iput(fat_inode);
  1615. unload_nls(sbi->nls_io);
  1616. unload_nls(sbi->nls_disk);
  1617. fat_reset_iocharset(&sbi->options);
  1618. sb->s_fs_info = NULL;
  1619. kfree(sbi);
  1620. return error;
  1621. }
  1622. EXPORT_SYMBOL_GPL(fat_fill_super);
  1623. /*
  1624. * helper function for fat_flush_inodes. This writes both the inode
  1625. * and the file data blocks, waiting for in flight data blocks before
  1626. * the start of the call. It does not wait for any io started
  1627. * during the call
  1628. */
  1629. static int writeback_inode(struct inode *inode)
  1630. {
  1631. int ret;
  1632. /* if we used wait=1, sync_inode_metadata waits for the io for the
  1633. * inode to finish. So wait=0 is sent down to sync_inode_metadata
  1634. * and filemap_fdatawrite is used for the data blocks
  1635. */
  1636. ret = sync_inode_metadata(inode, 0);
  1637. if (!ret)
  1638. ret = filemap_fdatawrite(inode->i_mapping);
  1639. return ret;
  1640. }
  1641. /*
  1642. * write data and metadata corresponding to i1 and i2. The io is
  1643. * started but we do not wait for any of it to finish.
  1644. *
  1645. * filemap_flush is used for the block device, so if there is a dirty
  1646. * page for a block already in flight, we will not wait and start the
  1647. * io over again
  1648. */
  1649. int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
  1650. {
  1651. int ret = 0;
  1652. if (!MSDOS_SB(sb)->options.flush)
  1653. return 0;
  1654. if (i1)
  1655. ret = writeback_inode(i1);
  1656. if (!ret && i2)
  1657. ret = writeback_inode(i2);
  1658. if (!ret)
  1659. ret = sync_blockdev_nowait(sb->s_bdev);
  1660. return ret;
  1661. }
  1662. EXPORT_SYMBOL_GPL(fat_flush_inodes);
  1663. int fat_init_fs_context(struct fs_context *fc, bool is_vfat)
  1664. {
  1665. struct fat_mount_options *opts;
  1666. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1667. if (!opts)
  1668. return -ENOMEM;
  1669. opts->isvfat = is_vfat;
  1670. opts->fs_uid = current_uid();
  1671. opts->fs_gid = current_gid();
  1672. opts->fs_fmask = opts->fs_dmask = current_umask();
  1673. opts->allow_utime = -1;
  1674. opts->codepage = fat_default_codepage;
  1675. fat_reset_iocharset(opts);
  1676. if (is_vfat) {
  1677. opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
  1678. opts->rodir = 0;
  1679. } else {
  1680. opts->shortname = 0;
  1681. opts->rodir = 1;
  1682. }
  1683. opts->name_check = 'n';
  1684. opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
  1685. opts->unicode_xlate = 0;
  1686. opts->numtail = 1;
  1687. opts->usefree = opts->nocase = 0;
  1688. opts->tz_set = 0;
  1689. opts->nfs = 0;
  1690. opts->errors = FAT_ERRORS_RO;
  1691. opts->debug = 0;
  1692. opts->utf8 = IS_ENABLED(CONFIG_FAT_DEFAULT_UTF8) && is_vfat;
  1693. fc->fs_private = opts;
  1694. /* fc->ops assigned by caller */
  1695. return 0;
  1696. }
  1697. EXPORT_SYMBOL_GPL(fat_init_fs_context);
  1698. void fat_free_fc(struct fs_context *fc)
  1699. {
  1700. struct fat_mount_options *opts = fc->fs_private;
  1701. if (opts->iocharset != fat_default_iocharset)
  1702. kfree(opts->iocharset);
  1703. kfree(fc->fs_private);
  1704. }
  1705. EXPORT_SYMBOL_GPL(fat_free_fc);
  1706. static int __init init_fat_fs(void)
  1707. {
  1708. int err;
  1709. err = fat_cache_init();
  1710. if (err)
  1711. return err;
  1712. err = fat_init_inodecache();
  1713. if (err)
  1714. goto failed;
  1715. return 0;
  1716. failed:
  1717. fat_cache_destroy();
  1718. return err;
  1719. }
  1720. static void __exit exit_fat_fs(void)
  1721. {
  1722. fat_cache_destroy();
  1723. fat_destroy_inodecache();
  1724. }
  1725. module_init(init_fat_fs)
  1726. module_exit(exit_fat_fs)
  1727. MODULE_DESCRIPTION("Core FAT filesystem support");
  1728. MODULE_LICENSE("GPL");