inode.c 38 KB

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