inode.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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. kmem_cache_destroy(fat_inode_cachep);
  450. }
  451. static int fat_remount(struct super_block *sb, int *flags, char *data)
  452. {
  453. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  454. *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
  455. return 0;
  456. }
  457. static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
  458. {
  459. struct super_block *sb = dentry->d_sb;
  460. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  461. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  462. /* If the count of free cluster is still unknown, counts it here. */
  463. if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
  464. int err = fat_count_free_clusters(dentry->d_sb);
  465. if (err)
  466. return err;
  467. }
  468. buf->f_type = dentry->d_sb->s_magic;
  469. buf->f_bsize = sbi->cluster_size;
  470. buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
  471. buf->f_bfree = sbi->free_clusters;
  472. buf->f_bavail = sbi->free_clusters;
  473. buf->f_fsid.val[0] = (u32)id;
  474. buf->f_fsid.val[1] = (u32)(id >> 32);
  475. buf->f_namelen =
  476. (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
  477. return 0;
  478. }
  479. static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
  480. struct inode *inode)
  481. {
  482. loff_t i_pos;
  483. #if BITS_PER_LONG == 32
  484. spin_lock(&sbi->inode_hash_lock);
  485. #endif
  486. i_pos = MSDOS_I(inode)->i_pos;
  487. #if BITS_PER_LONG == 32
  488. spin_unlock(&sbi->inode_hash_lock);
  489. #endif
  490. return i_pos;
  491. }
  492. static int __fat_write_inode(struct inode *inode, int wait)
  493. {
  494. struct super_block *sb = inode->i_sb;
  495. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  496. struct buffer_head *bh;
  497. struct msdos_dir_entry *raw_entry;
  498. loff_t i_pos;
  499. int err;
  500. if (inode->i_ino == MSDOS_ROOT_INO)
  501. return 0;
  502. retry:
  503. i_pos = fat_i_pos_read(sbi, inode);
  504. if (!i_pos)
  505. return 0;
  506. bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits);
  507. if (!bh) {
  508. fat_msg(sb, KERN_ERR, "unable to read inode block "
  509. "for updating (i_pos %lld)", i_pos);
  510. return -EIO;
  511. }
  512. spin_lock(&sbi->inode_hash_lock);
  513. if (i_pos != MSDOS_I(inode)->i_pos) {
  514. spin_unlock(&sbi->inode_hash_lock);
  515. brelse(bh);
  516. goto retry;
  517. }
  518. raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
  519. [i_pos & (sbi->dir_per_block - 1)];
  520. if (S_ISDIR(inode->i_mode))
  521. raw_entry->size = 0;
  522. else
  523. raw_entry->size = cpu_to_le32(inode->i_size);
  524. raw_entry->attr = fat_make_attrs(inode);
  525. fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
  526. fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
  527. &raw_entry->date, NULL);
  528. if (sbi->options.isvfat) {
  529. __le16 atime;
  530. fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
  531. &raw_entry->cdate, &raw_entry->ctime_cs);
  532. fat_time_unix2fat(sbi, &inode->i_atime, &atime,
  533. &raw_entry->adate, NULL);
  534. }
  535. spin_unlock(&sbi->inode_hash_lock);
  536. mark_buffer_dirty(bh);
  537. err = 0;
  538. if (wait)
  539. err = sync_dirty_buffer(bh);
  540. brelse(bh);
  541. return err;
  542. }
  543. static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
  544. {
  545. int err;
  546. if (inode->i_ino == MSDOS_FSINFO_INO) {
  547. struct super_block *sb = inode->i_sb;
  548. lock_super(sb);
  549. err = fat_clusters_flush(sb);
  550. unlock_super(sb);
  551. } else
  552. err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  553. return err;
  554. }
  555. int fat_sync_inode(struct inode *inode)
  556. {
  557. return __fat_write_inode(inode, 1);
  558. }
  559. EXPORT_SYMBOL_GPL(fat_sync_inode);
  560. static int fat_show_options(struct seq_file *m, struct dentry *root);
  561. static const struct super_operations fat_sops = {
  562. .alloc_inode = fat_alloc_inode,
  563. .destroy_inode = fat_destroy_inode,
  564. .write_inode = fat_write_inode,
  565. .evict_inode = fat_evict_inode,
  566. .put_super = fat_put_super,
  567. .statfs = fat_statfs,
  568. .remount_fs = fat_remount,
  569. .show_options = fat_show_options,
  570. };
  571. /*
  572. * a FAT file handle with fhtype 3 is
  573. * 0/ i_ino - for fast, reliable lookup if still in the cache
  574. * 1/ i_generation - to see if i_ino is still valid
  575. * bit 0 == 0 iff directory
  576. * 2/ i_pos(8-39) - if ino has changed, but still in cache
  577. * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
  578. * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
  579. *
  580. * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
  581. * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
  582. * of i_logstart is used to store the directory entry offset.
  583. */
  584. static struct dentry *fat_fh_to_dentry(struct super_block *sb,
  585. struct fid *fid, int fh_len, int fh_type)
  586. {
  587. struct inode *inode = NULL;
  588. u32 *fh = fid->raw;
  589. if (fh_len < 5 || fh_type != 3)
  590. return NULL;
  591. inode = ilookup(sb, fh[0]);
  592. if (!inode || inode->i_generation != fh[1]) {
  593. if (inode)
  594. iput(inode);
  595. inode = NULL;
  596. }
  597. if (!inode) {
  598. loff_t i_pos;
  599. int i_logstart = fh[3] & 0x0fffffff;
  600. i_pos = (loff_t)fh[2] << 8;
  601. i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
  602. /* try 2 - see if i_pos is in F-d-c
  603. * require i_logstart to be the same
  604. * Will fail if you truncate and then re-write
  605. */
  606. inode = fat_iget(sb, i_pos);
  607. if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
  608. iput(inode);
  609. inode = NULL;
  610. }
  611. }
  612. /*
  613. * For now, do nothing if the inode is not found.
  614. *
  615. * What we could do is:
  616. *
  617. * - follow the file starting at fh[4], and record the ".." entry,
  618. * and the name of the fh[2] entry.
  619. * - then follow the ".." file finding the next step up.
  620. *
  621. * This way we build a path to the root of the tree. If this works, we
  622. * lookup the path and so get this inode into the cache. Finally try
  623. * the fat_iget lookup again. If that fails, then we are totally out
  624. * of luck. But all that is for another day
  625. */
  626. return d_obtain_alias(inode);
  627. }
  628. static int
  629. fat_encode_fh(struct inode *inode, __u32 *fh, int *lenp, struct inode *parent)
  630. {
  631. int len = *lenp;
  632. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  633. loff_t i_pos;
  634. if (len < 5) {
  635. *lenp = 5;
  636. return 255; /* no room */
  637. }
  638. i_pos = fat_i_pos_read(sbi, inode);
  639. *lenp = 5;
  640. fh[0] = inode->i_ino;
  641. fh[1] = inode->i_generation;
  642. fh[2] = i_pos >> 8;
  643. fh[3] = ((i_pos & 0xf0) << 24) | MSDOS_I(inode)->i_logstart;
  644. fh[4] = (i_pos & 0x0f) << 28;
  645. if (parent)
  646. fh[4] |= MSDOS_I(parent)->i_logstart;
  647. return 3;
  648. }
  649. static struct dentry *fat_get_parent(struct dentry *child)
  650. {
  651. struct super_block *sb = child->d_sb;
  652. struct buffer_head *bh;
  653. struct msdos_dir_entry *de;
  654. loff_t i_pos;
  655. struct dentry *parent;
  656. struct inode *inode;
  657. int err;
  658. lock_super(sb);
  659. err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos);
  660. if (err) {
  661. parent = ERR_PTR(err);
  662. goto out;
  663. }
  664. inode = fat_build_inode(sb, de, i_pos);
  665. brelse(bh);
  666. parent = d_obtain_alias(inode);
  667. out:
  668. unlock_super(sb);
  669. return parent;
  670. }
  671. static const struct export_operations fat_export_ops = {
  672. .encode_fh = fat_encode_fh,
  673. .fh_to_dentry = fat_fh_to_dentry,
  674. .get_parent = fat_get_parent,
  675. };
  676. static int fat_show_options(struct seq_file *m, struct dentry *root)
  677. {
  678. struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
  679. struct fat_mount_options *opts = &sbi->options;
  680. int isvfat = opts->isvfat;
  681. if (opts->fs_uid != 0)
  682. seq_printf(m, ",uid=%u", opts->fs_uid);
  683. if (opts->fs_gid != 0)
  684. seq_printf(m, ",gid=%u", opts->fs_gid);
  685. seq_printf(m, ",fmask=%04o", opts->fs_fmask);
  686. seq_printf(m, ",dmask=%04o", opts->fs_dmask);
  687. if (opts->allow_utime)
  688. seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
  689. if (sbi->nls_disk)
  690. seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
  691. if (isvfat) {
  692. if (sbi->nls_io)
  693. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  694. switch (opts->shortname) {
  695. case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
  696. seq_puts(m, ",shortname=win95");
  697. break;
  698. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
  699. seq_puts(m, ",shortname=winnt");
  700. break;
  701. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
  702. seq_puts(m, ",shortname=mixed");
  703. break;
  704. case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
  705. seq_puts(m, ",shortname=lower");
  706. break;
  707. default:
  708. seq_puts(m, ",shortname=unknown");
  709. break;
  710. }
  711. }
  712. if (opts->name_check != 'n')
  713. seq_printf(m, ",check=%c", opts->name_check);
  714. if (opts->usefree)
  715. seq_puts(m, ",usefree");
  716. if (opts->quiet)
  717. seq_puts(m, ",quiet");
  718. if (opts->showexec)
  719. seq_puts(m, ",showexec");
  720. if (opts->sys_immutable)
  721. seq_puts(m, ",sys_immutable");
  722. if (!isvfat) {
  723. if (opts->dotsOK)
  724. seq_puts(m, ",dotsOK=yes");
  725. if (opts->nocase)
  726. seq_puts(m, ",nocase");
  727. } else {
  728. if (opts->utf8)
  729. seq_puts(m, ",utf8");
  730. if (opts->unicode_xlate)
  731. seq_puts(m, ",uni_xlate");
  732. if (!opts->numtail)
  733. seq_puts(m, ",nonumtail");
  734. if (opts->rodir)
  735. seq_puts(m, ",rodir");
  736. }
  737. if (opts->flush)
  738. seq_puts(m, ",flush");
  739. if (opts->tz_utc)
  740. seq_puts(m, ",tz=UTC");
  741. if (opts->errors == FAT_ERRORS_CONT)
  742. seq_puts(m, ",errors=continue");
  743. else if (opts->errors == FAT_ERRORS_PANIC)
  744. seq_puts(m, ",errors=panic");
  745. else
  746. seq_puts(m, ",errors=remount-ro");
  747. if (opts->discard)
  748. seq_puts(m, ",discard");
  749. return 0;
  750. }
  751. enum {
  752. Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
  753. Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
  754. Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
  755. Opt_immutable, Opt_dots, Opt_nodots,
  756. Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
  757. Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
  758. Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
  759. Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
  760. Opt_err_panic, Opt_err_ro, Opt_discard, Opt_err,
  761. };
  762. static const match_table_t fat_tokens = {
  763. {Opt_check_r, "check=relaxed"},
  764. {Opt_check_s, "check=strict"},
  765. {Opt_check_n, "check=normal"},
  766. {Opt_check_r, "check=r"},
  767. {Opt_check_s, "check=s"},
  768. {Opt_check_n, "check=n"},
  769. {Opt_uid, "uid=%u"},
  770. {Opt_gid, "gid=%u"},
  771. {Opt_umask, "umask=%o"},
  772. {Opt_dmask, "dmask=%o"},
  773. {Opt_fmask, "fmask=%o"},
  774. {Opt_allow_utime, "allow_utime=%o"},
  775. {Opt_codepage, "codepage=%u"},
  776. {Opt_usefree, "usefree"},
  777. {Opt_nocase, "nocase"},
  778. {Opt_quiet, "quiet"},
  779. {Opt_showexec, "showexec"},
  780. {Opt_debug, "debug"},
  781. {Opt_immutable, "sys_immutable"},
  782. {Opt_flush, "flush"},
  783. {Opt_tz_utc, "tz=UTC"},
  784. {Opt_err_cont, "errors=continue"},
  785. {Opt_err_panic, "errors=panic"},
  786. {Opt_err_ro, "errors=remount-ro"},
  787. {Opt_discard, "discard"},
  788. {Opt_obsolete, "conv=binary"},
  789. {Opt_obsolete, "conv=text"},
  790. {Opt_obsolete, "conv=auto"},
  791. {Opt_obsolete, "conv=b"},
  792. {Opt_obsolete, "conv=t"},
  793. {Opt_obsolete, "conv=a"},
  794. {Opt_obsolete, "fat=%u"},
  795. {Opt_obsolete, "blocksize=%u"},
  796. {Opt_obsolete, "cvf_format=%20s"},
  797. {Opt_obsolete, "cvf_options=%100s"},
  798. {Opt_obsolete, "posix"},
  799. {Opt_err, NULL},
  800. };
  801. static const match_table_t msdos_tokens = {
  802. {Opt_nodots, "nodots"},
  803. {Opt_nodots, "dotsOK=no"},
  804. {Opt_dots, "dots"},
  805. {Opt_dots, "dotsOK=yes"},
  806. {Opt_err, NULL}
  807. };
  808. static const match_table_t vfat_tokens = {
  809. {Opt_charset, "iocharset=%s"},
  810. {Opt_shortname_lower, "shortname=lower"},
  811. {Opt_shortname_win95, "shortname=win95"},
  812. {Opt_shortname_winnt, "shortname=winnt"},
  813. {Opt_shortname_mixed, "shortname=mixed"},
  814. {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */
  815. {Opt_utf8_no, "utf8=no"},
  816. {Opt_utf8_no, "utf8=false"},
  817. {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */
  818. {Opt_utf8_yes, "utf8=yes"},
  819. {Opt_utf8_yes, "utf8=true"},
  820. {Opt_utf8_yes, "utf8"},
  821. {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */
  822. {Opt_uni_xl_no, "uni_xlate=no"},
  823. {Opt_uni_xl_no, "uni_xlate=false"},
  824. {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */
  825. {Opt_uni_xl_yes, "uni_xlate=yes"},
  826. {Opt_uni_xl_yes, "uni_xlate=true"},
  827. {Opt_uni_xl_yes, "uni_xlate"},
  828. {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */
  829. {Opt_nonumtail_no, "nonumtail=no"},
  830. {Opt_nonumtail_no, "nonumtail=false"},
  831. {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */
  832. {Opt_nonumtail_yes, "nonumtail=yes"},
  833. {Opt_nonumtail_yes, "nonumtail=true"},
  834. {Opt_nonumtail_yes, "nonumtail"},
  835. {Opt_rodir, "rodir"},
  836. {Opt_err, NULL}
  837. };
  838. static int parse_options(struct super_block *sb, char *options, int is_vfat,
  839. int silent, int *debug, struct fat_mount_options *opts)
  840. {
  841. char *p;
  842. substring_t args[MAX_OPT_ARGS];
  843. int option;
  844. char *iocharset;
  845. opts->isvfat = is_vfat;
  846. opts->fs_uid = current_uid();
  847. opts->fs_gid = current_gid();
  848. opts->fs_fmask = opts->fs_dmask = current_umask();
  849. opts->allow_utime = -1;
  850. opts->codepage = fat_default_codepage;
  851. opts->iocharset = fat_default_iocharset;
  852. if (is_vfat) {
  853. opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
  854. opts->rodir = 0;
  855. } else {
  856. opts->shortname = 0;
  857. opts->rodir = 1;
  858. }
  859. opts->name_check = 'n';
  860. opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
  861. opts->utf8 = opts->unicode_xlate = 0;
  862. opts->numtail = 1;
  863. opts->usefree = opts->nocase = 0;
  864. opts->tz_utc = 0;
  865. opts->errors = FAT_ERRORS_RO;
  866. *debug = 0;
  867. if (!options)
  868. goto out;
  869. while ((p = strsep(&options, ",")) != NULL) {
  870. int token;
  871. if (!*p)
  872. continue;
  873. token = match_token(p, fat_tokens, args);
  874. if (token == Opt_err) {
  875. if (is_vfat)
  876. token = match_token(p, vfat_tokens, args);
  877. else
  878. token = match_token(p, msdos_tokens, args);
  879. }
  880. switch (token) {
  881. case Opt_check_s:
  882. opts->name_check = 's';
  883. break;
  884. case Opt_check_r:
  885. opts->name_check = 'r';
  886. break;
  887. case Opt_check_n:
  888. opts->name_check = 'n';
  889. break;
  890. case Opt_usefree:
  891. opts->usefree = 1;
  892. break;
  893. case Opt_nocase:
  894. if (!is_vfat)
  895. opts->nocase = 1;
  896. else {
  897. /* for backward compatibility */
  898. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  899. | VFAT_SFN_CREATE_WIN95;
  900. }
  901. break;
  902. case Opt_quiet:
  903. opts->quiet = 1;
  904. break;
  905. case Opt_showexec:
  906. opts->showexec = 1;
  907. break;
  908. case Opt_debug:
  909. *debug = 1;
  910. break;
  911. case Opt_immutable:
  912. opts->sys_immutable = 1;
  913. break;
  914. case Opt_uid:
  915. if (match_int(&args[0], &option))
  916. return 0;
  917. opts->fs_uid = option;
  918. break;
  919. case Opt_gid:
  920. if (match_int(&args[0], &option))
  921. return 0;
  922. opts->fs_gid = option;
  923. break;
  924. case Opt_umask:
  925. if (match_octal(&args[0], &option))
  926. return 0;
  927. opts->fs_fmask = opts->fs_dmask = option;
  928. break;
  929. case Opt_dmask:
  930. if (match_octal(&args[0], &option))
  931. return 0;
  932. opts->fs_dmask = option;
  933. break;
  934. case Opt_fmask:
  935. if (match_octal(&args[0], &option))
  936. return 0;
  937. opts->fs_fmask = option;
  938. break;
  939. case Opt_allow_utime:
  940. if (match_octal(&args[0], &option))
  941. return 0;
  942. opts->allow_utime = option & (S_IWGRP | S_IWOTH);
  943. break;
  944. case Opt_codepage:
  945. if (match_int(&args[0], &option))
  946. return 0;
  947. opts->codepage = option;
  948. break;
  949. case Opt_flush:
  950. opts->flush = 1;
  951. break;
  952. case Opt_tz_utc:
  953. opts->tz_utc = 1;
  954. break;
  955. case Opt_err_cont:
  956. opts->errors = FAT_ERRORS_CONT;
  957. break;
  958. case Opt_err_panic:
  959. opts->errors = FAT_ERRORS_PANIC;
  960. break;
  961. case Opt_err_ro:
  962. opts->errors = FAT_ERRORS_RO;
  963. break;
  964. /* msdos specific */
  965. case Opt_dots:
  966. opts->dotsOK = 1;
  967. break;
  968. case Opt_nodots:
  969. opts->dotsOK = 0;
  970. break;
  971. /* vfat specific */
  972. case Opt_charset:
  973. if (opts->iocharset != fat_default_iocharset)
  974. kfree(opts->iocharset);
  975. iocharset = match_strdup(&args[0]);
  976. if (!iocharset)
  977. return -ENOMEM;
  978. opts->iocharset = iocharset;
  979. break;
  980. case Opt_shortname_lower:
  981. opts->shortname = VFAT_SFN_DISPLAY_LOWER
  982. | VFAT_SFN_CREATE_WIN95;
  983. break;
  984. case Opt_shortname_win95:
  985. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  986. | VFAT_SFN_CREATE_WIN95;
  987. break;
  988. case Opt_shortname_winnt:
  989. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  990. | VFAT_SFN_CREATE_WINNT;
  991. break;
  992. case Opt_shortname_mixed:
  993. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  994. | VFAT_SFN_CREATE_WIN95;
  995. break;
  996. case Opt_utf8_no: /* 0 or no or false */
  997. opts->utf8 = 0;
  998. break;
  999. case Opt_utf8_yes: /* empty or 1 or yes or true */
  1000. opts->utf8 = 1;
  1001. break;
  1002. case Opt_uni_xl_no: /* 0 or no or false */
  1003. opts->unicode_xlate = 0;
  1004. break;
  1005. case Opt_uni_xl_yes: /* empty or 1 or yes or true */
  1006. opts->unicode_xlate = 1;
  1007. break;
  1008. case Opt_nonumtail_no: /* 0 or no or false */
  1009. opts->numtail = 1; /* negated option */
  1010. break;
  1011. case Opt_nonumtail_yes: /* empty or 1 or yes or true */
  1012. opts->numtail = 0; /* negated option */
  1013. break;
  1014. case Opt_rodir:
  1015. opts->rodir = 1;
  1016. break;
  1017. case Opt_discard:
  1018. opts->discard = 1;
  1019. break;
  1020. /* obsolete mount options */
  1021. case Opt_obsolete:
  1022. fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
  1023. "not supported now", p);
  1024. break;
  1025. /* unknown option */
  1026. default:
  1027. if (!silent) {
  1028. fat_msg(sb, KERN_ERR,
  1029. "Unrecognized mount option \"%s\" "
  1030. "or missing value", p);
  1031. }
  1032. return -EINVAL;
  1033. }
  1034. }
  1035. out:
  1036. /* UTF-8 doesn't provide FAT semantics */
  1037. if (!strcmp(opts->iocharset, "utf8")) {
  1038. fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
  1039. " for FAT filesystems, filesystem will be "
  1040. "case sensitive!");
  1041. }
  1042. /* If user doesn't specify allow_utime, it's initialized from dmask. */
  1043. if (opts->allow_utime == (unsigned short)-1)
  1044. opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
  1045. if (opts->unicode_xlate)
  1046. opts->utf8 = 0;
  1047. return 0;
  1048. }
  1049. static int fat_read_root(struct inode *inode)
  1050. {
  1051. struct super_block *sb = inode->i_sb;
  1052. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1053. int error;
  1054. MSDOS_I(inode)->i_pos = 0;
  1055. inode->i_uid = sbi->options.fs_uid;
  1056. inode->i_gid = sbi->options.fs_gid;
  1057. inode->i_version++;
  1058. inode->i_generation = 0;
  1059. inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
  1060. inode->i_op = sbi->dir_ops;
  1061. inode->i_fop = &fat_dir_operations;
  1062. if (sbi->fat_bits == 32) {
  1063. MSDOS_I(inode)->i_start = sbi->root_cluster;
  1064. error = fat_calc_dir_size(inode);
  1065. if (error < 0)
  1066. return error;
  1067. } else {
  1068. MSDOS_I(inode)->i_start = 0;
  1069. inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
  1070. }
  1071. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  1072. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  1073. MSDOS_I(inode)->i_logstart = 0;
  1074. MSDOS_I(inode)->mmu_private = inode->i_size;
  1075. fat_save_attrs(inode, ATTR_DIR);
  1076. inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
  1077. inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1078. set_nlink(inode, fat_subdirs(inode)+2);
  1079. return 0;
  1080. }
  1081. /*
  1082. * Read the super block of an MS-DOS FS.
  1083. */
  1084. int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
  1085. void (*setup)(struct super_block *))
  1086. {
  1087. struct inode *root_inode = NULL, *fat_inode = NULL;
  1088. struct inode *fsinfo_inode = NULL;
  1089. struct buffer_head *bh;
  1090. struct fat_boot_sector *b;
  1091. struct msdos_sb_info *sbi;
  1092. u16 logical_sector_size;
  1093. u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
  1094. int debug;
  1095. unsigned int media;
  1096. long error;
  1097. char buf[50];
  1098. /*
  1099. * GFP_KERNEL is ok here, because while we do hold the
  1100. * supeblock lock, memory pressure can't call back into
  1101. * the filesystem, since we're only just about to mount
  1102. * it and have no inodes etc active!
  1103. */
  1104. sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
  1105. if (!sbi)
  1106. return -ENOMEM;
  1107. sb->s_fs_info = sbi;
  1108. sb->s_flags |= MS_NODIRATIME;
  1109. sb->s_magic = MSDOS_SUPER_MAGIC;
  1110. sb->s_op = &fat_sops;
  1111. sb->s_export_op = &fat_export_ops;
  1112. ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
  1113. DEFAULT_RATELIMIT_BURST);
  1114. error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
  1115. if (error)
  1116. goto out_fail;
  1117. setup(sb); /* flavour-specific stuff that needs options */
  1118. error = -EIO;
  1119. sb_min_blocksize(sb, 512);
  1120. bh = sb_bread(sb, 0);
  1121. if (bh == NULL) {
  1122. fat_msg(sb, KERN_ERR, "unable to read boot sector");
  1123. goto out_fail;
  1124. }
  1125. b = (struct fat_boot_sector *) bh->b_data;
  1126. if (!b->reserved) {
  1127. if (!silent)
  1128. fat_msg(sb, KERN_ERR, "bogus number of reserved sectors");
  1129. brelse(bh);
  1130. goto out_invalid;
  1131. }
  1132. if (!b->fats) {
  1133. if (!silent)
  1134. fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
  1135. brelse(bh);
  1136. goto out_invalid;
  1137. }
  1138. /*
  1139. * Earlier we checked here that b->secs_track and b->head are nonzero,
  1140. * but it turns out valid FAT filesystems can have zero there.
  1141. */
  1142. media = b->media;
  1143. if (!fat_valid_media(media)) {
  1144. if (!silent)
  1145. fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
  1146. media);
  1147. brelse(bh);
  1148. goto out_invalid;
  1149. }
  1150. logical_sector_size = get_unaligned_le16(&b->sector_size);
  1151. if (!is_power_of_2(logical_sector_size)
  1152. || (logical_sector_size < 512)
  1153. || (logical_sector_size > 4096)) {
  1154. if (!silent)
  1155. fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
  1156. logical_sector_size);
  1157. brelse(bh);
  1158. goto out_invalid;
  1159. }
  1160. sbi->sec_per_clus = b->sec_per_clus;
  1161. if (!is_power_of_2(sbi->sec_per_clus)) {
  1162. if (!silent)
  1163. fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
  1164. sbi->sec_per_clus);
  1165. brelse(bh);
  1166. goto out_invalid;
  1167. }
  1168. if (logical_sector_size < sb->s_blocksize) {
  1169. fat_msg(sb, KERN_ERR, "logical sector size too small for device"
  1170. " (logical sector size = %u)", logical_sector_size);
  1171. brelse(bh);
  1172. goto out_fail;
  1173. }
  1174. if (logical_sector_size > sb->s_blocksize) {
  1175. brelse(bh);
  1176. if (!sb_set_blocksize(sb, logical_sector_size)) {
  1177. fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
  1178. logical_sector_size);
  1179. goto out_fail;
  1180. }
  1181. bh = sb_bread(sb, 0);
  1182. if (bh == NULL) {
  1183. fat_msg(sb, KERN_ERR, "unable to read boot sector"
  1184. " (logical sector size = %lu)",
  1185. sb->s_blocksize);
  1186. goto out_fail;
  1187. }
  1188. b = (struct fat_boot_sector *) bh->b_data;
  1189. }
  1190. sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
  1191. sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
  1192. sbi->fats = b->fats;
  1193. sbi->fat_bits = 0; /* Don't know yet */
  1194. sbi->fat_start = le16_to_cpu(b->reserved);
  1195. sbi->fat_length = le16_to_cpu(b->fat_length);
  1196. sbi->root_cluster = 0;
  1197. sbi->free_clusters = -1; /* Don't know yet */
  1198. sbi->free_clus_valid = 0;
  1199. sbi->prev_free = FAT_START_ENT;
  1200. sb->s_maxbytes = 0xffffffff;
  1201. if (!sbi->fat_length && b->fat32_length) {
  1202. struct fat_boot_fsinfo *fsinfo;
  1203. struct buffer_head *fsinfo_bh;
  1204. /* Must be FAT32 */
  1205. sbi->fat_bits = 32;
  1206. sbi->fat_length = le32_to_cpu(b->fat32_length);
  1207. sbi->root_cluster = le32_to_cpu(b->root_cluster);
  1208. /* MC - if info_sector is 0, don't multiply by 0 */
  1209. sbi->fsinfo_sector = le16_to_cpu(b->info_sector);
  1210. if (sbi->fsinfo_sector == 0)
  1211. sbi->fsinfo_sector = 1;
  1212. fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
  1213. if (fsinfo_bh == NULL) {
  1214. fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
  1215. " (sector = %lu)", sbi->fsinfo_sector);
  1216. brelse(bh);
  1217. goto out_fail;
  1218. }
  1219. fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
  1220. if (!IS_FSINFO(fsinfo)) {
  1221. fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
  1222. "0x%08x, 0x%08x (sector = %lu)",
  1223. le32_to_cpu(fsinfo->signature1),
  1224. le32_to_cpu(fsinfo->signature2),
  1225. sbi->fsinfo_sector);
  1226. } else {
  1227. if (sbi->options.usefree)
  1228. sbi->free_clus_valid = 1;
  1229. sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
  1230. sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
  1231. }
  1232. brelse(fsinfo_bh);
  1233. }
  1234. sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
  1235. sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
  1236. sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
  1237. sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
  1238. if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
  1239. if (!silent)
  1240. fat_msg(sb, KERN_ERR, "bogus directroy-entries per block"
  1241. " (%u)", sbi->dir_entries);
  1242. brelse(bh);
  1243. goto out_invalid;
  1244. }
  1245. rootdir_sectors = sbi->dir_entries
  1246. * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
  1247. sbi->data_start = sbi->dir_start + rootdir_sectors;
  1248. total_sectors = get_unaligned_le16(&b->sectors);
  1249. if (total_sectors == 0)
  1250. total_sectors = le32_to_cpu(b->total_sect);
  1251. total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
  1252. if (sbi->fat_bits != 32)
  1253. sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
  1254. /* check that FAT table does not overflow */
  1255. fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
  1256. total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
  1257. if (total_clusters > MAX_FAT(sb)) {
  1258. if (!silent)
  1259. fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
  1260. total_clusters);
  1261. brelse(bh);
  1262. goto out_invalid;
  1263. }
  1264. sbi->max_cluster = total_clusters + FAT_START_ENT;
  1265. /* check the free_clusters, it's not necessarily correct */
  1266. if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
  1267. sbi->free_clusters = -1;
  1268. /* check the prev_free, it's not necessarily correct */
  1269. sbi->prev_free %= sbi->max_cluster;
  1270. if (sbi->prev_free < FAT_START_ENT)
  1271. sbi->prev_free = FAT_START_ENT;
  1272. brelse(bh);
  1273. /* set up enough so that it can read an inode */
  1274. fat_hash_init(sb);
  1275. fat_ent_access_init(sb);
  1276. /*
  1277. * The low byte of FAT's first entry must have same value with
  1278. * media-field. But in real world, too many devices is
  1279. * writing wrong value. So, removed that validity check.
  1280. *
  1281. * if (FAT_FIRST_ENT(sb, media) != first)
  1282. */
  1283. error = -EINVAL;
  1284. sprintf(buf, "cp%d", sbi->options.codepage);
  1285. sbi->nls_disk = load_nls(buf);
  1286. if (!sbi->nls_disk) {
  1287. fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
  1288. goto out_fail;
  1289. }
  1290. /* FIXME: utf8 is using iocharset for upper/lower conversion */
  1291. if (sbi->options.isvfat) {
  1292. sbi->nls_io = load_nls(sbi->options.iocharset);
  1293. if (!sbi->nls_io) {
  1294. fat_msg(sb, KERN_ERR, "IO charset %s not found",
  1295. sbi->options.iocharset);
  1296. goto out_fail;
  1297. }
  1298. }
  1299. error = -ENOMEM;
  1300. fat_inode = new_inode(sb);
  1301. if (!fat_inode)
  1302. goto out_fail;
  1303. MSDOS_I(fat_inode)->i_pos = 0;
  1304. sbi->fat_inode = fat_inode;
  1305. fsinfo_inode = new_inode(sb);
  1306. if (!fsinfo_inode)
  1307. goto out_fail;
  1308. fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
  1309. sbi->fsinfo_inode = fsinfo_inode;
  1310. insert_inode_hash(fsinfo_inode);
  1311. root_inode = new_inode(sb);
  1312. if (!root_inode)
  1313. goto out_fail;
  1314. root_inode->i_ino = MSDOS_ROOT_INO;
  1315. root_inode->i_version = 1;
  1316. error = fat_read_root(root_inode);
  1317. if (error < 0) {
  1318. iput(root_inode);
  1319. goto out_fail;
  1320. }
  1321. error = -ENOMEM;
  1322. insert_inode_hash(root_inode);
  1323. sb->s_root = d_make_root(root_inode);
  1324. if (!sb->s_root) {
  1325. fat_msg(sb, KERN_ERR, "get root inode failed");
  1326. goto out_fail;
  1327. }
  1328. return 0;
  1329. out_invalid:
  1330. error = -EINVAL;
  1331. if (!silent)
  1332. fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
  1333. out_fail:
  1334. if (fsinfo_inode)
  1335. iput(fsinfo_inode);
  1336. if (fat_inode)
  1337. iput(fat_inode);
  1338. unload_nls(sbi->nls_io);
  1339. unload_nls(sbi->nls_disk);
  1340. if (sbi->options.iocharset != fat_default_iocharset)
  1341. kfree(sbi->options.iocharset);
  1342. sb->s_fs_info = NULL;
  1343. kfree(sbi);
  1344. return error;
  1345. }
  1346. EXPORT_SYMBOL_GPL(fat_fill_super);
  1347. /*
  1348. * helper function for fat_flush_inodes. This writes both the inode
  1349. * and the file data blocks, waiting for in flight data blocks before
  1350. * the start of the call. It does not wait for any io started
  1351. * during the call
  1352. */
  1353. static int writeback_inode(struct inode *inode)
  1354. {
  1355. int ret;
  1356. struct address_space *mapping = inode->i_mapping;
  1357. struct writeback_control wbc = {
  1358. .sync_mode = WB_SYNC_NONE,
  1359. .nr_to_write = 0,
  1360. };
  1361. /* if we used WB_SYNC_ALL, sync_inode waits for the io for the
  1362. * inode to finish. So WB_SYNC_NONE is sent down to sync_inode
  1363. * and filemap_fdatawrite is used for the data blocks
  1364. */
  1365. ret = sync_inode(inode, &wbc);
  1366. if (!ret)
  1367. ret = filemap_fdatawrite(mapping);
  1368. return ret;
  1369. }
  1370. /*
  1371. * write data and metadata corresponding to i1 and i2. The io is
  1372. * started but we do not wait for any of it to finish.
  1373. *
  1374. * filemap_flush is used for the block device, so if there is a dirty
  1375. * page for a block already in flight, we will not wait and start the
  1376. * io over again
  1377. */
  1378. int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
  1379. {
  1380. int ret = 0;
  1381. if (!MSDOS_SB(sb)->options.flush)
  1382. return 0;
  1383. if (i1)
  1384. ret = writeback_inode(i1);
  1385. if (!ret && i2)
  1386. ret = writeback_inode(i2);
  1387. if (!ret) {
  1388. struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
  1389. ret = filemap_flush(mapping);
  1390. }
  1391. return ret;
  1392. }
  1393. EXPORT_SYMBOL_GPL(fat_flush_inodes);
  1394. static int __init init_fat_fs(void)
  1395. {
  1396. int err;
  1397. err = fat_cache_init();
  1398. if (err)
  1399. return err;
  1400. err = fat_init_inodecache();
  1401. if (err)
  1402. goto failed;
  1403. return 0;
  1404. failed:
  1405. fat_cache_destroy();
  1406. return err;
  1407. }
  1408. static void __exit exit_fat_fs(void)
  1409. {
  1410. fat_cache_destroy();
  1411. fat_destroy_inodecache();
  1412. }
  1413. module_init(init_fat_fs)
  1414. module_exit(exit_fat_fs)
  1415. MODULE_LICENSE("GPL");