inode.c 47 KB

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