inode.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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/smp_lock.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/msdos_fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/mount.h>
  22. #include <linux/vfs.h>
  23. #include <linux/parser.h>
  24. #include <asm/unaligned.h>
  25. #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
  26. /* if user don't select VFAT, this is undefined. */
  27. #define CONFIG_FAT_DEFAULT_IOCHARSET ""
  28. #endif
  29. static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
  30. static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
  31. static int fat_add_cluster(struct inode *inode)
  32. {
  33. int err, cluster;
  34. err = fat_alloc_clusters(inode, &cluster, 1);
  35. if (err)
  36. return err;
  37. /* FIXME: this cluster should be added after data of this
  38. * cluster is writed */
  39. err = fat_chain_add(inode, cluster, 1);
  40. if (err)
  41. fat_free_clusters(inode, cluster);
  42. return err;
  43. }
  44. static int fat_get_block(struct inode *inode, sector_t iblock,
  45. struct buffer_head *bh_result, int create)
  46. {
  47. struct super_block *sb = inode->i_sb;
  48. sector_t phys;
  49. int err;
  50. err = fat_bmap(inode, iblock, &phys);
  51. if (err)
  52. return err;
  53. if (phys) {
  54. map_bh(bh_result, sb, phys);
  55. return 0;
  56. }
  57. if (!create)
  58. return 0;
  59. if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
  60. fat_fs_panic(sb, "corrupted file size (i_pos %lld, %lld)",
  61. MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
  62. return -EIO;
  63. }
  64. if (!((unsigned long)iblock & (MSDOS_SB(sb)->sec_per_clus - 1))) {
  65. err = fat_add_cluster(inode);
  66. if (err)
  67. return err;
  68. }
  69. MSDOS_I(inode)->mmu_private += sb->s_blocksize;
  70. err = fat_bmap(inode, iblock, &phys);
  71. if (err)
  72. return err;
  73. if (!phys)
  74. BUG();
  75. set_buffer_new(bh_result);
  76. map_bh(bh_result, sb, phys);
  77. return 0;
  78. }
  79. static int fat_writepage(struct page *page, struct writeback_control *wbc)
  80. {
  81. return block_write_full_page(page, fat_get_block, wbc);
  82. }
  83. static int fat_readpage(struct file *file, struct page *page)
  84. {
  85. return block_read_full_page(page, fat_get_block);
  86. }
  87. static int fat_prepare_write(struct file *file, struct page *page,
  88. unsigned from, unsigned to)
  89. {
  90. return cont_prepare_write(page, from, to, fat_get_block,
  91. &MSDOS_I(page->mapping->host)->mmu_private);
  92. }
  93. static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
  94. {
  95. return generic_block_bmap(mapping, block, fat_get_block);
  96. }
  97. static struct address_space_operations fat_aops = {
  98. .readpage = fat_readpage,
  99. .writepage = fat_writepage,
  100. .sync_page = block_sync_page,
  101. .prepare_write = fat_prepare_write,
  102. .commit_write = generic_commit_write,
  103. .bmap = _fat_bmap
  104. };
  105. /*
  106. * New FAT inode stuff. We do the following:
  107. * a) i_ino is constant and has nothing with on-disk location.
  108. * b) FAT manages its own cache of directory entries.
  109. * c) *This* cache is indexed by on-disk location.
  110. * d) inode has an associated directory entry, all right, but
  111. * it may be unhashed.
  112. * e) currently entries are stored within struct inode. That should
  113. * change.
  114. * f) we deal with races in the following way:
  115. * 1. readdir() and lookup() do FAT-dir-cache lookup.
  116. * 2. rename() unhashes the F-d-c entry and rehashes it in
  117. * a new place.
  118. * 3. unlink() and rmdir() unhash F-d-c entry.
  119. * 4. fat_write_inode() checks whether the thing is unhashed.
  120. * If it is we silently return. If it isn't we do bread(),
  121. * check if the location is still valid and retry if it
  122. * isn't. Otherwise we do changes.
  123. * 5. Spinlock is used to protect hash/unhash/location check/lookup
  124. * 6. fat_clear_inode() unhashes the F-d-c entry.
  125. * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
  126. * and consider negative result as cache miss.
  127. */
  128. static void fat_hash_init(struct super_block *sb)
  129. {
  130. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  131. int i;
  132. spin_lock_init(&sbi->inode_hash_lock);
  133. for (i = 0; i < FAT_HASH_SIZE; i++)
  134. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  135. }
  136. static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
  137. {
  138. unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
  139. tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
  140. return tmp & FAT_HASH_MASK;
  141. }
  142. void fat_attach(struct inode *inode, loff_t i_pos)
  143. {
  144. struct super_block *sb = inode->i_sb;
  145. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  146. spin_lock(&sbi->inode_hash_lock);
  147. MSDOS_I(inode)->i_pos = i_pos;
  148. hlist_add_head(&MSDOS_I(inode)->i_fat_hash,
  149. sbi->inode_hashtable + fat_hash(sb, i_pos));
  150. spin_unlock(&sbi->inode_hash_lock);
  151. }
  152. EXPORT_SYMBOL(fat_attach);
  153. void fat_detach(struct inode *inode)
  154. {
  155. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  156. spin_lock(&sbi->inode_hash_lock);
  157. MSDOS_I(inode)->i_pos = 0;
  158. hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
  159. spin_unlock(&sbi->inode_hash_lock);
  160. }
  161. EXPORT_SYMBOL(fat_detach);
  162. struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
  163. {
  164. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  165. struct hlist_head *head = sbi->inode_hashtable + fat_hash(sb, i_pos);
  166. struct hlist_node *_p;
  167. struct msdos_inode_info *i;
  168. struct inode *inode = NULL;
  169. spin_lock(&sbi->inode_hash_lock);
  170. hlist_for_each_entry(i, _p, head, i_fat_hash) {
  171. BUG_ON(i->vfs_inode.i_sb != sb);
  172. if (i->i_pos != i_pos)
  173. continue;
  174. inode = igrab(&i->vfs_inode);
  175. if (inode)
  176. break;
  177. }
  178. spin_unlock(&sbi->inode_hash_lock);
  179. return inode;
  180. }
  181. static int is_exec(unsigned char *extension)
  182. {
  183. unsigned char *exe_extensions = "EXECOMBAT", *walk;
  184. for (walk = exe_extensions; *walk; walk += 3)
  185. if (!strncmp(extension, walk, 3))
  186. return 1;
  187. return 0;
  188. }
  189. static int fat_calc_dir_size(struct inode *inode)
  190. {
  191. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  192. int ret, fclus, dclus;
  193. inode->i_size = 0;
  194. if (MSDOS_I(inode)->i_start == 0)
  195. return 0;
  196. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  197. if (ret < 0)
  198. return ret;
  199. inode->i_size = (fclus + 1) << sbi->cluster_bits;
  200. return 0;
  201. }
  202. /* doesn't deal with root inode */
  203. static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
  204. {
  205. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  206. int error;
  207. MSDOS_I(inode)->i_pos = 0;
  208. inode->i_uid = sbi->options.fs_uid;
  209. inode->i_gid = sbi->options.fs_gid;
  210. inode->i_version++;
  211. inode->i_generation = get_seconds();
  212. if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
  213. inode->i_generation &= ~1;
  214. inode->i_mode = MSDOS_MKMODE(de->attr,
  215. S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
  216. inode->i_op = sbi->dir_ops;
  217. inode->i_fop = &fat_dir_operations;
  218. MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
  219. if (sbi->fat_bits == 32)
  220. MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
  221. MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  222. error = fat_calc_dir_size(inode);
  223. if (error < 0)
  224. return error;
  225. MSDOS_I(inode)->mmu_private = inode->i_size;
  226. inode->i_nlink = fat_subdirs(inode);
  227. } else { /* not a directory */
  228. inode->i_generation |= 1;
  229. inode->i_mode = MSDOS_MKMODE(de->attr,
  230. ((sbi->options.showexec &&
  231. !is_exec(de->ext))
  232. ? S_IRUGO|S_IWUGO : S_IRWXUGO)
  233. & ~sbi->options.fs_fmask) | S_IFREG;
  234. MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
  235. if (sbi->fat_bits == 32)
  236. MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
  237. MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  238. inode->i_size = le32_to_cpu(de->size);
  239. inode->i_op = &fat_file_inode_operations;
  240. inode->i_fop = &fat_file_operations;
  241. inode->i_mapping->a_ops = &fat_aops;
  242. MSDOS_I(inode)->mmu_private = inode->i_size;
  243. }
  244. if (de->attr & ATTR_SYS) {
  245. if (sbi->options.sys_immutable)
  246. inode->i_flags |= S_IMMUTABLE;
  247. }
  248. MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
  249. /* this is as close to the truth as we can get ... */
  250. inode->i_blksize = sbi->cluster_size;
  251. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  252. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  253. inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
  254. date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date));
  255. inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
  256. if (sbi->options.isvfat) {
  257. int secs = de->ctime_cs / 100;
  258. int csecs = de->ctime_cs % 100;
  259. inode->i_ctime.tv_sec =
  260. date_dos2unix(le16_to_cpu(de->ctime),
  261. le16_to_cpu(de->cdate)) + secs;
  262. inode->i_ctime.tv_nsec = csecs * 10000000;
  263. } else
  264. inode->i_ctime = inode->i_mtime;
  265. return 0;
  266. }
  267. struct inode *fat_build_inode(struct super_block *sb,
  268. struct msdos_dir_entry *de, loff_t i_pos)
  269. {
  270. struct inode *inode;
  271. int err;
  272. inode = fat_iget(sb, i_pos);
  273. if (inode)
  274. goto out;
  275. inode = new_inode(sb);
  276. if (!inode) {
  277. inode = ERR_PTR(-ENOMEM);
  278. goto out;
  279. }
  280. inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
  281. inode->i_version = 1;
  282. err = fat_fill_inode(inode, de);
  283. if (err) {
  284. iput(inode);
  285. inode = ERR_PTR(err);
  286. goto out;
  287. }
  288. fat_attach(inode, i_pos);
  289. insert_inode_hash(inode);
  290. out:
  291. return inode;
  292. }
  293. EXPORT_SYMBOL(fat_build_inode);
  294. static void fat_delete_inode(struct inode *inode)
  295. {
  296. if (!is_bad_inode(inode)) {
  297. inode->i_size = 0;
  298. fat_truncate(inode);
  299. }
  300. clear_inode(inode);
  301. }
  302. static void fat_clear_inode(struct inode *inode)
  303. {
  304. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  305. if (is_bad_inode(inode))
  306. return;
  307. lock_kernel();
  308. spin_lock(&sbi->inode_hash_lock);
  309. fat_cache_inval_inode(inode);
  310. hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
  311. spin_unlock(&sbi->inode_hash_lock);
  312. unlock_kernel();
  313. }
  314. static void fat_put_super(struct super_block *sb)
  315. {
  316. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  317. if (!(sb->s_flags & MS_RDONLY))
  318. fat_clusters_flush(sb);
  319. if (sbi->nls_disk) {
  320. unload_nls(sbi->nls_disk);
  321. sbi->nls_disk = NULL;
  322. sbi->options.codepage = fat_default_codepage;
  323. }
  324. if (sbi->nls_io) {
  325. unload_nls(sbi->nls_io);
  326. sbi->nls_io = NULL;
  327. }
  328. if (sbi->options.iocharset != fat_default_iocharset) {
  329. kfree(sbi->options.iocharset);
  330. sbi->options.iocharset = fat_default_iocharset;
  331. }
  332. sb->s_fs_info = NULL;
  333. kfree(sbi);
  334. }
  335. static kmem_cache_t *fat_inode_cachep;
  336. static struct inode *fat_alloc_inode(struct super_block *sb)
  337. {
  338. struct msdos_inode_info *ei;
  339. ei = kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL);
  340. if (!ei)
  341. return NULL;
  342. return &ei->vfs_inode;
  343. }
  344. static void fat_destroy_inode(struct inode *inode)
  345. {
  346. kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
  347. }
  348. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  349. {
  350. struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
  351. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  352. SLAB_CTOR_CONSTRUCTOR) {
  353. spin_lock_init(&ei->cache_lru_lock);
  354. ei->nr_caches = 0;
  355. ei->cache_valid_id = FAT_CACHE_VALID + 1;
  356. INIT_LIST_HEAD(&ei->cache_lru);
  357. INIT_HLIST_NODE(&ei->i_fat_hash);
  358. inode_init_once(&ei->vfs_inode);
  359. }
  360. }
  361. static int __init fat_init_inodecache(void)
  362. {
  363. fat_inode_cachep = kmem_cache_create("fat_inode_cache",
  364. sizeof(struct msdos_inode_info),
  365. 0, SLAB_RECLAIM_ACCOUNT,
  366. init_once, NULL);
  367. if (fat_inode_cachep == NULL)
  368. return -ENOMEM;
  369. return 0;
  370. }
  371. static void __exit fat_destroy_inodecache(void)
  372. {
  373. if (kmem_cache_destroy(fat_inode_cachep))
  374. printk(KERN_INFO "fat_inode_cache: not all structures were freed\n");
  375. }
  376. static int fat_remount(struct super_block *sb, int *flags, char *data)
  377. {
  378. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  379. *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
  380. return 0;
  381. }
  382. static int fat_statfs(struct super_block *sb, struct kstatfs *buf)
  383. {
  384. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  385. /* If the count of free cluster is still unknown, counts it here. */
  386. if (sbi->free_clusters == -1) {
  387. int err = fat_count_free_clusters(sb);
  388. if (err)
  389. return err;
  390. }
  391. buf->f_type = sb->s_magic;
  392. buf->f_bsize = sbi->cluster_size;
  393. buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
  394. buf->f_bfree = sbi->free_clusters;
  395. buf->f_bavail = sbi->free_clusters;
  396. buf->f_namelen = sbi->options.isvfat ? 260 : 12;
  397. return 0;
  398. }
  399. static int fat_write_inode(struct inode *inode, int wait)
  400. {
  401. struct super_block *sb = inode->i_sb;
  402. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  403. struct buffer_head *bh;
  404. struct msdos_dir_entry *raw_entry;
  405. loff_t i_pos;
  406. int err = 0;
  407. retry:
  408. i_pos = MSDOS_I(inode)->i_pos;
  409. if (inode->i_ino == MSDOS_ROOT_INO || !i_pos)
  410. return 0;
  411. lock_kernel();
  412. bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits);
  413. if (!bh) {
  414. printk(KERN_ERR "FAT: unable to read inode block "
  415. "for updating (i_pos %lld)\n", i_pos);
  416. err = -EIO;
  417. goto out;
  418. }
  419. spin_lock(&sbi->inode_hash_lock);
  420. if (i_pos != MSDOS_I(inode)->i_pos) {
  421. spin_unlock(&sbi->inode_hash_lock);
  422. brelse(bh);
  423. unlock_kernel();
  424. goto retry;
  425. }
  426. raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
  427. [i_pos & (sbi->dir_per_block - 1)];
  428. if (S_ISDIR(inode->i_mode))
  429. raw_entry->size = 0;
  430. else
  431. raw_entry->size = cpu_to_le32(inode->i_size);
  432. raw_entry->attr = fat_attr(inode);
  433. raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart);
  434. raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16);
  435. fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time, &raw_entry->date);
  436. if (sbi->options.isvfat) {
  437. fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
  438. raw_entry->ctime_cs = (inode->i_ctime.tv_sec & 1) * 100 +
  439. inode->i_ctime.tv_nsec / 10000000;
  440. }
  441. spin_unlock(&sbi->inode_hash_lock);
  442. mark_buffer_dirty(bh);
  443. if (wait)
  444. err = sync_dirty_buffer(bh);
  445. brelse(bh);
  446. out:
  447. unlock_kernel();
  448. return err;
  449. }
  450. int fat_sync_inode(struct inode *inode)
  451. {
  452. return fat_write_inode(inode, 1);
  453. }
  454. EXPORT_SYMBOL(fat_sync_inode);
  455. static int fat_show_options(struct seq_file *m, struct vfsmount *mnt);
  456. static struct super_operations fat_sops = {
  457. .alloc_inode = fat_alloc_inode,
  458. .destroy_inode = fat_destroy_inode,
  459. .write_inode = fat_write_inode,
  460. .delete_inode = fat_delete_inode,
  461. .put_super = fat_put_super,
  462. .statfs = fat_statfs,
  463. .clear_inode = fat_clear_inode,
  464. .remount_fs = fat_remount,
  465. .read_inode = make_bad_inode,
  466. .show_options = fat_show_options,
  467. };
  468. /*
  469. * a FAT file handle with fhtype 3 is
  470. * 0/ i_ino - for fast, reliable lookup if still in the cache
  471. * 1/ i_generation - to see if i_ino is still valid
  472. * bit 0 == 0 iff directory
  473. * 2/ i_pos(8-39) - if ino has changed, but still in cache
  474. * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
  475. * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
  476. *
  477. * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
  478. * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
  479. * of i_logstart is used to store the directory entry offset.
  480. */
  481. static struct dentry *
  482. fat_decode_fh(struct super_block *sb, __u32 *fh, int len, int fhtype,
  483. int (*acceptable)(void *context, struct dentry *de),
  484. void *context)
  485. {
  486. if (fhtype != 3)
  487. return ERR_PTR(-ESTALE);
  488. if (len < 5)
  489. return ERR_PTR(-ESTALE);
  490. return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context);
  491. }
  492. static struct dentry *fat_get_dentry(struct super_block *sb, void *inump)
  493. {
  494. struct inode *inode = NULL;
  495. struct dentry *result;
  496. __u32 *fh = inump;
  497. inode = iget(sb, fh[0]);
  498. if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) {
  499. if (inode)
  500. iput(inode);
  501. inode = NULL;
  502. }
  503. if (!inode) {
  504. loff_t i_pos;
  505. int i_logstart = fh[3] & 0x0fffffff;
  506. i_pos = (loff_t)fh[2] << 8;
  507. i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
  508. /* try 2 - see if i_pos is in F-d-c
  509. * require i_logstart to be the same
  510. * Will fail if you truncate and then re-write
  511. */
  512. inode = fat_iget(sb, i_pos);
  513. if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
  514. iput(inode);
  515. inode = NULL;
  516. }
  517. }
  518. if (!inode) {
  519. /* For now, do nothing
  520. * What we could do is:
  521. * follow the file starting at fh[4], and record
  522. * the ".." entry, and the name of the fh[2] entry.
  523. * The follow the ".." file finding the next step up.
  524. * This way we build a path to the root of
  525. * the tree. If this works, we lookup the path and so
  526. * get this inode into the cache.
  527. * Finally try the fat_iget lookup again
  528. * If that fails, then weare totally out of luck
  529. * But all that is for another day
  530. */
  531. }
  532. if (!inode)
  533. return ERR_PTR(-ESTALE);
  534. /* now to find a dentry.
  535. * If possible, get a well-connected one
  536. */
  537. result = d_alloc_anon(inode);
  538. if (result == NULL) {
  539. iput(inode);
  540. return ERR_PTR(-ENOMEM);
  541. }
  542. result->d_op = sb->s_root->d_op;
  543. return result;
  544. }
  545. static int
  546. fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
  547. {
  548. int len = *lenp;
  549. struct inode *inode = de->d_inode;
  550. u32 ipos_h, ipos_m, ipos_l;
  551. if (len < 5)
  552. return 255; /* no room */
  553. ipos_h = MSDOS_I(inode)->i_pos >> 8;
  554. ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
  555. ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
  556. *lenp = 5;
  557. fh[0] = inode->i_ino;
  558. fh[1] = inode->i_generation;
  559. fh[2] = ipos_h;
  560. fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
  561. spin_lock(&de->d_lock);
  562. fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
  563. spin_unlock(&de->d_lock);
  564. return 3;
  565. }
  566. static struct dentry *fat_get_parent(struct dentry *child)
  567. {
  568. struct buffer_head *bh;
  569. struct msdos_dir_entry *de;
  570. loff_t i_pos;
  571. struct dentry *parent;
  572. struct inode *inode;
  573. int err;
  574. lock_kernel();
  575. err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos);
  576. if (err) {
  577. parent = ERR_PTR(err);
  578. goto out;
  579. }
  580. inode = fat_build_inode(child->d_sb, de, i_pos);
  581. brelse(bh);
  582. if (IS_ERR(inode)) {
  583. parent = ERR_PTR(PTR_ERR(inode));
  584. goto out;
  585. }
  586. parent = d_alloc_anon(inode);
  587. if (!parent) {
  588. iput(inode);
  589. parent = ERR_PTR(-ENOMEM);
  590. }
  591. out:
  592. unlock_kernel();
  593. return parent;
  594. }
  595. static struct export_operations fat_export_ops = {
  596. .decode_fh = fat_decode_fh,
  597. .encode_fh = fat_encode_fh,
  598. .get_dentry = fat_get_dentry,
  599. .get_parent = fat_get_parent,
  600. };
  601. static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
  602. {
  603. struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
  604. struct fat_mount_options *opts = &sbi->options;
  605. int isvfat = opts->isvfat;
  606. if (opts->fs_uid != 0)
  607. seq_printf(m, ",uid=%u", opts->fs_uid);
  608. if (opts->fs_gid != 0)
  609. seq_printf(m, ",gid=%u", opts->fs_gid);
  610. seq_printf(m, ",fmask=%04o", opts->fs_fmask);
  611. seq_printf(m, ",dmask=%04o", opts->fs_dmask);
  612. if (sbi->nls_disk)
  613. seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
  614. if (isvfat) {
  615. if (sbi->nls_io)
  616. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  617. switch (opts->shortname) {
  618. case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
  619. seq_puts(m, ",shortname=win95");
  620. break;
  621. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
  622. seq_puts(m, ",shortname=winnt");
  623. break;
  624. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
  625. seq_puts(m, ",shortname=mixed");
  626. break;
  627. case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
  628. /* seq_puts(m, ",shortname=lower"); */
  629. break;
  630. default:
  631. seq_puts(m, ",shortname=unknown");
  632. break;
  633. }
  634. }
  635. if (opts->name_check != 'n')
  636. seq_printf(m, ",check=%c", opts->name_check);
  637. if (opts->quiet)
  638. seq_puts(m, ",quiet");
  639. if (opts->showexec)
  640. seq_puts(m, ",showexec");
  641. if (opts->sys_immutable)
  642. seq_puts(m, ",sys_immutable");
  643. if (!isvfat) {
  644. if (opts->dotsOK)
  645. seq_puts(m, ",dotsOK=yes");
  646. if (opts->nocase)
  647. seq_puts(m, ",nocase");
  648. } else {
  649. if (opts->utf8)
  650. seq_puts(m, ",utf8");
  651. if (opts->unicode_xlate)
  652. seq_puts(m, ",uni_xlate");
  653. if (!opts->numtail)
  654. seq_puts(m, ",nonumtail");
  655. }
  656. return 0;
  657. }
  658. enum {
  659. Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
  660. Opt_umask, Opt_dmask, Opt_fmask, Opt_codepage, Opt_nocase,
  661. Opt_quiet, Opt_showexec, Opt_debug, Opt_immutable,
  662. Opt_dots, Opt_nodots,
  663. Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
  664. Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
  665. Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
  666. Opt_obsolate, Opt_err,
  667. };
  668. static match_table_t fat_tokens = {
  669. {Opt_check_r, "check=relaxed"},
  670. {Opt_check_s, "check=strict"},
  671. {Opt_check_n, "check=normal"},
  672. {Opt_check_r, "check=r"},
  673. {Opt_check_s, "check=s"},
  674. {Opt_check_n, "check=n"},
  675. {Opt_uid, "uid=%u"},
  676. {Opt_gid, "gid=%u"},
  677. {Opt_umask, "umask=%o"},
  678. {Opt_dmask, "dmask=%o"},
  679. {Opt_fmask, "fmask=%o"},
  680. {Opt_codepage, "codepage=%u"},
  681. {Opt_nocase, "nocase"},
  682. {Opt_quiet, "quiet"},
  683. {Opt_showexec, "showexec"},
  684. {Opt_debug, "debug"},
  685. {Opt_immutable, "sys_immutable"},
  686. {Opt_obsolate, "conv=binary"},
  687. {Opt_obsolate, "conv=text"},
  688. {Opt_obsolate, "conv=auto"},
  689. {Opt_obsolate, "conv=b"},
  690. {Opt_obsolate, "conv=t"},
  691. {Opt_obsolate, "conv=a"},
  692. {Opt_obsolate, "fat=%u"},
  693. {Opt_obsolate, "blocksize=%u"},
  694. {Opt_obsolate, "cvf_format=%20s"},
  695. {Opt_obsolate, "cvf_options=%100s"},
  696. {Opt_obsolate, "posix"},
  697. {Opt_err, NULL}
  698. };
  699. static match_table_t msdos_tokens = {
  700. {Opt_nodots, "nodots"},
  701. {Opt_nodots, "dotsOK=no"},
  702. {Opt_dots, "dots"},
  703. {Opt_dots, "dotsOK=yes"},
  704. {Opt_err, NULL}
  705. };
  706. static match_table_t vfat_tokens = {
  707. {Opt_charset, "iocharset=%s"},
  708. {Opt_shortname_lower, "shortname=lower"},
  709. {Opt_shortname_win95, "shortname=win95"},
  710. {Opt_shortname_winnt, "shortname=winnt"},
  711. {Opt_shortname_mixed, "shortname=mixed"},
  712. {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */
  713. {Opt_utf8_no, "utf8=no"},
  714. {Opt_utf8_no, "utf8=false"},
  715. {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */
  716. {Opt_utf8_yes, "utf8=yes"},
  717. {Opt_utf8_yes, "utf8=true"},
  718. {Opt_utf8_yes, "utf8"},
  719. {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */
  720. {Opt_uni_xl_no, "uni_xlate=no"},
  721. {Opt_uni_xl_no, "uni_xlate=false"},
  722. {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */
  723. {Opt_uni_xl_yes, "uni_xlate=yes"},
  724. {Opt_uni_xl_yes, "uni_xlate=true"},
  725. {Opt_uni_xl_yes, "uni_xlate"},
  726. {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */
  727. {Opt_nonumtail_no, "nonumtail=no"},
  728. {Opt_nonumtail_no, "nonumtail=false"},
  729. {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */
  730. {Opt_nonumtail_yes, "nonumtail=yes"},
  731. {Opt_nonumtail_yes, "nonumtail=true"},
  732. {Opt_nonumtail_yes, "nonumtail"},
  733. {Opt_err, NULL}
  734. };
  735. static int parse_options(char *options, int is_vfat, int *debug,
  736. struct fat_mount_options *opts)
  737. {
  738. char *p;
  739. substring_t args[MAX_OPT_ARGS];
  740. int option;
  741. char *iocharset;
  742. opts->isvfat = is_vfat;
  743. opts->fs_uid = current->uid;
  744. opts->fs_gid = current->gid;
  745. opts->fs_fmask = opts->fs_dmask = current->fs->umask;
  746. opts->codepage = fat_default_codepage;
  747. opts->iocharset = fat_default_iocharset;
  748. if (is_vfat)
  749. opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
  750. else
  751. opts->shortname = 0;
  752. opts->name_check = 'n';
  753. opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
  754. opts->utf8 = opts->unicode_xlate = 0;
  755. opts->numtail = 1;
  756. opts->nocase = 0;
  757. *debug = 0;
  758. if (!options)
  759. return 0;
  760. while ((p = strsep(&options, ",")) != NULL) {
  761. int token;
  762. if (!*p)
  763. continue;
  764. token = match_token(p, fat_tokens, args);
  765. if (token == Opt_err) {
  766. if (is_vfat)
  767. token = match_token(p, vfat_tokens, args);
  768. else
  769. token = match_token(p, msdos_tokens, args);
  770. }
  771. switch (token) {
  772. case Opt_check_s:
  773. opts->name_check = 's';
  774. break;
  775. case Opt_check_r:
  776. opts->name_check = 'r';
  777. break;
  778. case Opt_check_n:
  779. opts->name_check = 'n';
  780. break;
  781. case Opt_nocase:
  782. if (!is_vfat)
  783. opts->nocase = 1;
  784. else {
  785. /* for backward compatibility */
  786. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  787. | VFAT_SFN_CREATE_WIN95;
  788. }
  789. break;
  790. case Opt_quiet:
  791. opts->quiet = 1;
  792. break;
  793. case Opt_showexec:
  794. opts->showexec = 1;
  795. break;
  796. case Opt_debug:
  797. *debug = 1;
  798. break;
  799. case Opt_immutable:
  800. opts->sys_immutable = 1;
  801. break;
  802. case Opt_uid:
  803. if (match_int(&args[0], &option))
  804. return 0;
  805. opts->fs_uid = option;
  806. break;
  807. case Opt_gid:
  808. if (match_int(&args[0], &option))
  809. return 0;
  810. opts->fs_gid = option;
  811. break;
  812. case Opt_umask:
  813. if (match_octal(&args[0], &option))
  814. return 0;
  815. opts->fs_fmask = opts->fs_dmask = option;
  816. break;
  817. case Opt_dmask:
  818. if (match_octal(&args[0], &option))
  819. return 0;
  820. opts->fs_dmask = option;
  821. break;
  822. case Opt_fmask:
  823. if (match_octal(&args[0], &option))
  824. return 0;
  825. opts->fs_fmask = option;
  826. break;
  827. case Opt_codepage:
  828. if (match_int(&args[0], &option))
  829. return 0;
  830. opts->codepage = option;
  831. break;
  832. /* msdos specific */
  833. case Opt_dots:
  834. opts->dotsOK = 1;
  835. break;
  836. case Opt_nodots:
  837. opts->dotsOK = 0;
  838. break;
  839. /* vfat specific */
  840. case Opt_charset:
  841. if (opts->iocharset != fat_default_iocharset)
  842. kfree(opts->iocharset);
  843. iocharset = match_strdup(&args[0]);
  844. if (!iocharset)
  845. return -ENOMEM;
  846. opts->iocharset = iocharset;
  847. break;
  848. case Opt_shortname_lower:
  849. opts->shortname = VFAT_SFN_DISPLAY_LOWER
  850. | VFAT_SFN_CREATE_WIN95;
  851. break;
  852. case Opt_shortname_win95:
  853. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  854. | VFAT_SFN_CREATE_WIN95;
  855. break;
  856. case Opt_shortname_winnt:
  857. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  858. | VFAT_SFN_CREATE_WINNT;
  859. break;
  860. case Opt_shortname_mixed:
  861. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  862. | VFAT_SFN_CREATE_WIN95;
  863. break;
  864. case Opt_utf8_no: /* 0 or no or false */
  865. opts->utf8 = 0;
  866. break;
  867. case Opt_utf8_yes: /* empty or 1 or yes or true */
  868. opts->utf8 = 1;
  869. break;
  870. case Opt_uni_xl_no: /* 0 or no or false */
  871. opts->unicode_xlate = 0;
  872. break;
  873. case Opt_uni_xl_yes: /* empty or 1 or yes or true */
  874. opts->unicode_xlate = 1;
  875. break;
  876. case Opt_nonumtail_no: /* 0 or no or false */
  877. opts->numtail = 1; /* negated option */
  878. break;
  879. case Opt_nonumtail_yes: /* empty or 1 or yes or true */
  880. opts->numtail = 0; /* negated option */
  881. break;
  882. /* obsolete mount options */
  883. case Opt_obsolate:
  884. printk(KERN_INFO "FAT: \"%s\" option is obsolete, "
  885. "not supported now\n", p);
  886. break;
  887. /* unknown option */
  888. default:
  889. printk(KERN_ERR "FAT: Unrecognized mount option \"%s\" "
  890. "or missing value\n", p);
  891. return -EINVAL;
  892. }
  893. }
  894. /* UTF8 doesn't provide FAT semantics */
  895. if (!strcmp(opts->iocharset, "utf8")) {
  896. printk(KERN_ERR "FAT: utf8 is not a recommended IO charset"
  897. " for FAT filesystems, filesystem will be case sensitive!\n");
  898. }
  899. if (opts->unicode_xlate)
  900. opts->utf8 = 0;
  901. return 0;
  902. }
  903. static int fat_read_root(struct inode *inode)
  904. {
  905. struct super_block *sb = inode->i_sb;
  906. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  907. int error;
  908. MSDOS_I(inode)->i_pos = 0;
  909. inode->i_uid = sbi->options.fs_uid;
  910. inode->i_gid = sbi->options.fs_gid;
  911. inode->i_version++;
  912. inode->i_generation = 0;
  913. inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
  914. inode->i_op = sbi->dir_ops;
  915. inode->i_fop = &fat_dir_operations;
  916. if (sbi->fat_bits == 32) {
  917. MSDOS_I(inode)->i_start = sbi->root_cluster;
  918. error = fat_calc_dir_size(inode);
  919. if (error < 0)
  920. return error;
  921. } else {
  922. MSDOS_I(inode)->i_start = 0;
  923. inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
  924. }
  925. inode->i_blksize = sbi->cluster_size;
  926. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  927. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  928. MSDOS_I(inode)->i_logstart = 0;
  929. MSDOS_I(inode)->mmu_private = inode->i_size;
  930. MSDOS_I(inode)->i_attrs = ATTR_NONE;
  931. inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
  932. inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  933. inode->i_nlink = fat_subdirs(inode)+2;
  934. return 0;
  935. }
  936. /*
  937. * Read the super block of an MS-DOS FS.
  938. */
  939. int fat_fill_super(struct super_block *sb, void *data, int silent,
  940. struct inode_operations *fs_dir_inode_ops, int isvfat)
  941. {
  942. struct inode *root_inode = NULL;
  943. struct buffer_head *bh;
  944. struct fat_boot_sector *b;
  945. struct msdos_sb_info *sbi;
  946. u16 logical_sector_size;
  947. u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
  948. int debug;
  949. unsigned int media;
  950. long error;
  951. char buf[50];
  952. sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
  953. if (!sbi)
  954. return -ENOMEM;
  955. sb->s_fs_info = sbi;
  956. memset(sbi, 0, sizeof(struct msdos_sb_info));
  957. sb->s_flags |= MS_NODIRATIME;
  958. sb->s_magic = MSDOS_SUPER_MAGIC;
  959. sb->s_op = &fat_sops;
  960. sb->s_export_op = &fat_export_ops;
  961. sbi->dir_ops = fs_dir_inode_ops;
  962. error = parse_options(data, isvfat, &debug, &sbi->options);
  963. if (error)
  964. goto out_fail;
  965. error = -EIO;
  966. sb_min_blocksize(sb, 512);
  967. bh = sb_bread(sb, 0);
  968. if (bh == NULL) {
  969. printk(KERN_ERR "FAT: unable to read boot sector\n");
  970. goto out_fail;
  971. }
  972. b = (struct fat_boot_sector *) bh->b_data;
  973. if (!b->reserved) {
  974. if (!silent)
  975. printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
  976. brelse(bh);
  977. goto out_invalid;
  978. }
  979. if (!b->fats) {
  980. if (!silent)
  981. printk(KERN_ERR "FAT: bogus number of FAT structure\n");
  982. brelse(bh);
  983. goto out_invalid;
  984. }
  985. /*
  986. * Earlier we checked here that b->secs_track and b->head are nonzero,
  987. * but it turns out valid FAT filesystems can have zero there.
  988. */
  989. media = b->media;
  990. if (!FAT_VALID_MEDIA(media)) {
  991. if (!silent)
  992. printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
  993. media);
  994. brelse(bh);
  995. goto out_invalid;
  996. }
  997. logical_sector_size =
  998. le16_to_cpu(get_unaligned((__le16 *)&b->sector_size));
  999. if (!logical_sector_size
  1000. || (logical_sector_size & (logical_sector_size - 1))
  1001. || (logical_sector_size < 512)
  1002. || (PAGE_CACHE_SIZE < logical_sector_size)) {
  1003. if (!silent)
  1004. printk(KERN_ERR "FAT: bogus logical sector size %u\n",
  1005. logical_sector_size);
  1006. brelse(bh);
  1007. goto out_invalid;
  1008. }
  1009. sbi->sec_per_clus = b->sec_per_clus;
  1010. if (!sbi->sec_per_clus
  1011. || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) {
  1012. if (!silent)
  1013. printk(KERN_ERR "FAT: bogus sectors per cluster %u\n",
  1014. sbi->sec_per_clus);
  1015. brelse(bh);
  1016. goto out_invalid;
  1017. }
  1018. if (logical_sector_size < sb->s_blocksize) {
  1019. printk(KERN_ERR "FAT: logical sector size too small for device"
  1020. " (logical sector size = %u)\n", logical_sector_size);
  1021. brelse(bh);
  1022. goto out_fail;
  1023. }
  1024. if (logical_sector_size > sb->s_blocksize) {
  1025. brelse(bh);
  1026. if (!sb_set_blocksize(sb, logical_sector_size)) {
  1027. printk(KERN_ERR "FAT: unable to set blocksize %u\n",
  1028. logical_sector_size);
  1029. goto out_fail;
  1030. }
  1031. bh = sb_bread(sb, 0);
  1032. if (bh == NULL) {
  1033. printk(KERN_ERR "FAT: unable to read boot sector"
  1034. " (logical sector size = %lu)\n",
  1035. sb->s_blocksize);
  1036. goto out_fail;
  1037. }
  1038. b = (struct fat_boot_sector *) bh->b_data;
  1039. }
  1040. sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
  1041. sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
  1042. sbi->fats = b->fats;
  1043. sbi->fat_bits = 0; /* Don't know yet */
  1044. sbi->fat_start = le16_to_cpu(b->reserved);
  1045. sbi->fat_length = le16_to_cpu(b->fat_length);
  1046. sbi->root_cluster = 0;
  1047. sbi->free_clusters = -1; /* Don't know yet */
  1048. sbi->prev_free = FAT_START_ENT;
  1049. if (!sbi->fat_length && b->fat32_length) {
  1050. struct fat_boot_fsinfo *fsinfo;
  1051. struct buffer_head *fsinfo_bh;
  1052. /* Must be FAT32 */
  1053. sbi->fat_bits = 32;
  1054. sbi->fat_length = le32_to_cpu(b->fat32_length);
  1055. sbi->root_cluster = le32_to_cpu(b->root_cluster);
  1056. sb->s_maxbytes = 0xffffffff;
  1057. /* MC - if info_sector is 0, don't multiply by 0 */
  1058. sbi->fsinfo_sector = le16_to_cpu(b->info_sector);
  1059. if (sbi->fsinfo_sector == 0)
  1060. sbi->fsinfo_sector = 1;
  1061. fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
  1062. if (fsinfo_bh == NULL) {
  1063. printk(KERN_ERR "FAT: bread failed, FSINFO block"
  1064. " (sector = %lu)\n", sbi->fsinfo_sector);
  1065. brelse(bh);
  1066. goto out_fail;
  1067. }
  1068. fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
  1069. if (!IS_FSINFO(fsinfo)) {
  1070. printk(KERN_WARNING
  1071. "FAT: Did not find valid FSINFO signature.\n"
  1072. " Found signature1 0x%08x signature2 0x%08x"
  1073. " (sector = %lu)\n",
  1074. le32_to_cpu(fsinfo->signature1),
  1075. le32_to_cpu(fsinfo->signature2),
  1076. sbi->fsinfo_sector);
  1077. } else {
  1078. sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
  1079. sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
  1080. }
  1081. brelse(fsinfo_bh);
  1082. }
  1083. sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
  1084. sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
  1085. sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
  1086. sbi->dir_entries =
  1087. le16_to_cpu(get_unaligned((__le16 *)&b->dir_entries));
  1088. if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
  1089. if (!silent)
  1090. printk(KERN_ERR "FAT: bogus directroy-entries per block"
  1091. " (%u)\n", sbi->dir_entries);
  1092. brelse(bh);
  1093. goto out_invalid;
  1094. }
  1095. rootdir_sectors = sbi->dir_entries
  1096. * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
  1097. sbi->data_start = sbi->dir_start + rootdir_sectors;
  1098. total_sectors = le16_to_cpu(get_unaligned((__le16 *)&b->sectors));
  1099. if (total_sectors == 0)
  1100. total_sectors = le32_to_cpu(b->total_sect);
  1101. total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
  1102. if (sbi->fat_bits != 32)
  1103. sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
  1104. /* check that FAT table does not overflow */
  1105. fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
  1106. total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
  1107. if (total_clusters > MAX_FAT(sb)) {
  1108. if (!silent)
  1109. printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
  1110. total_clusters);
  1111. brelse(bh);
  1112. goto out_invalid;
  1113. }
  1114. sbi->max_cluster = total_clusters + FAT_START_ENT;
  1115. /* check the free_clusters, it's not necessarily correct */
  1116. if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
  1117. sbi->free_clusters = -1;
  1118. /* check the prev_free, it's not necessarily correct */
  1119. sbi->prev_free %= sbi->max_cluster;
  1120. if (sbi->prev_free < FAT_START_ENT)
  1121. sbi->prev_free = FAT_START_ENT;
  1122. brelse(bh);
  1123. /* set up enough so that it can read an inode */
  1124. fat_hash_init(sb);
  1125. fat_ent_access_init(sb);
  1126. /*
  1127. * The low byte of FAT's first entry must have same value with
  1128. * media-field. But in real world, too many devices is
  1129. * writing wrong value. So, removed that validity check.
  1130. *
  1131. * if (FAT_FIRST_ENT(sb, media) != first)
  1132. */
  1133. error = -EINVAL;
  1134. sprintf(buf, "cp%d", sbi->options.codepage);
  1135. sbi->nls_disk = load_nls(buf);
  1136. if (!sbi->nls_disk) {
  1137. printk(KERN_ERR "FAT: codepage %s not found\n", buf);
  1138. goto out_fail;
  1139. }
  1140. /* FIXME: utf8 is using iocharset for upper/lower conversion */
  1141. if (sbi->options.isvfat) {
  1142. sbi->nls_io = load_nls(sbi->options.iocharset);
  1143. if (!sbi->nls_io) {
  1144. printk(KERN_ERR "FAT: IO charset %s not found\n",
  1145. sbi->options.iocharset);
  1146. goto out_fail;
  1147. }
  1148. }
  1149. error = -ENOMEM;
  1150. root_inode = new_inode(sb);
  1151. if (!root_inode)
  1152. goto out_fail;
  1153. root_inode->i_ino = MSDOS_ROOT_INO;
  1154. root_inode->i_version = 1;
  1155. error = fat_read_root(root_inode);
  1156. if (error < 0)
  1157. goto out_fail;
  1158. error = -ENOMEM;
  1159. insert_inode_hash(root_inode);
  1160. sb->s_root = d_alloc_root(root_inode);
  1161. if (!sb->s_root) {
  1162. printk(KERN_ERR "FAT: get root inode failed\n");
  1163. goto out_fail;
  1164. }
  1165. return 0;
  1166. out_invalid:
  1167. error = -EINVAL;
  1168. if (!silent)
  1169. printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
  1170. " on dev %s.\n", sb->s_id);
  1171. out_fail:
  1172. if (root_inode)
  1173. iput(root_inode);
  1174. if (sbi->nls_io)
  1175. unload_nls(sbi->nls_io);
  1176. if (sbi->nls_disk)
  1177. unload_nls(sbi->nls_disk);
  1178. if (sbi->options.iocharset != fat_default_iocharset)
  1179. kfree(sbi->options.iocharset);
  1180. sb->s_fs_info = NULL;
  1181. kfree(sbi);
  1182. return error;
  1183. }
  1184. EXPORT_SYMBOL(fat_fill_super);
  1185. int __init fat_cache_init(void);
  1186. void fat_cache_destroy(void);
  1187. static int __init init_fat_fs(void)
  1188. {
  1189. int err;
  1190. err = fat_cache_init();
  1191. if (err)
  1192. return err;
  1193. err = fat_init_inodecache();
  1194. if (err)
  1195. goto failed;
  1196. return 0;
  1197. failed:
  1198. fat_cache_destroy();
  1199. return err;
  1200. }
  1201. static void __exit exit_fat_fs(void)
  1202. {
  1203. fat_cache_destroy();
  1204. fat_destroy_inodecache();
  1205. }
  1206. module_init(init_fat_fs)
  1207. module_exit(exit_fat_fs)
  1208. MODULE_LICENSE("GPL");