inode.c 51 KB

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