inode.c 48 KB

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