inode.c 50 KB

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