inode.c 48 KB

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