inode.c 50 KB

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