inode.c 51 KB

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