inode.c 48 KB

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