inode.c 51 KB

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