inode.c 48 KB

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