inode.c 51 KB

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