inode.c 50 KB

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