inode.c 48 KB

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