inode.c 50 KB

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