inode.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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. #if defined(CONFIG_VMWARE_MVP)
  212. int _fat_fallocate(struct inode *inode, loff_t len)
  213. {
  214. struct super_block *sb = inode->i_sb;
  215. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  216. int err;
  217. sector_t nblocks, iblock;
  218. unsigned short offset;
  219. if (!S_ISREG(inode->i_mode)) {
  220. printk(KERN_ERR "_fat_fallocate: supported only for regular files\n");
  221. return -EOPNOTSUPP;
  222. }
  223. if (IS_IMMUTABLE(inode)) {
  224. return -EPERM;
  225. }
  226. mutex_lock(&inode->i_mutex);
  227. /* file is already big enough */
  228. if (len <= i_size_read(inode)) {
  229. mutex_unlock(&inode->i_mutex);
  230. return 0;
  231. }
  232. nblocks = (len + sb->s_blocksize - 1 ) >> sb->s_blocksize_bits;
  233. iblock = (MSDOS_I(inode)->mmu_private + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  234. /* validate new size */
  235. err = inode_newsize_ok(inode, len);
  236. if (err) {
  237. mutex_unlock(&inode->i_mutex);
  238. return err;
  239. }
  240. /* check for available blocks on last cluster */
  241. offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
  242. if (offset) {
  243. iblock += min((unsigned long) (sbi->sec_per_clus - offset),
  244. (unsigned long) (nblocks - iblock));
  245. }
  246. /* now allocate new clusters */
  247. while (iblock < nblocks) {
  248. err = fat_add_cluster(inode);
  249. if (err) {
  250. break;
  251. }
  252. iblock += min((unsigned long) sbi->sec_per_clus,
  253. (unsigned long) (nblocks - iblock));
  254. }
  255. /* update inode informations */
  256. len = min(len, (loff_t)(iblock << sb->s_blocksize_bits));
  257. i_size_write(inode, len);
  258. MSDOS_I(inode)->mmu_private = len;
  259. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  260. mark_inode_dirty(inode);
  261. mutex_unlock(&inode->i_mutex);
  262. return err;
  263. }
  264. #endif
  265. /*
  266. * New FAT inode stuff. We do the following:
  267. * a) i_ino is constant and has nothing with on-disk location.
  268. * b) FAT manages its own cache of directory entries.
  269. * c) *This* cache is indexed by on-disk location.
  270. * d) inode has an associated directory entry, all right, but
  271. * it may be unhashed.
  272. * e) currently entries are stored within struct inode. That should
  273. * change.
  274. * f) we deal with races in the following way:
  275. * 1. readdir() and lookup() do FAT-dir-cache lookup.
  276. * 2. rename() unhashes the F-d-c entry and rehashes it in
  277. * a new place.
  278. * 3. unlink() and rmdir() unhash F-d-c entry.
  279. * 4. fat_write_inode() checks whether the thing is unhashed.
  280. * If it is we silently return. If it isn't we do bread(),
  281. * check if the location is still valid and retry if it
  282. * isn't. Otherwise we do changes.
  283. * 5. Spinlock is used to protect hash/unhash/location check/lookup
  284. * 6. fat_evict_inode() unhashes the F-d-c entry.
  285. * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
  286. * and consider negative result as cache miss.
  287. */
  288. static void fat_hash_init(struct super_block *sb)
  289. {
  290. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  291. int i;
  292. spin_lock_init(&sbi->inode_hash_lock);
  293. for (i = 0; i < FAT_HASH_SIZE; i++)
  294. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  295. }
  296. static inline unsigned long fat_hash(loff_t i_pos)
  297. {
  298. return hash_32(i_pos, FAT_HASH_BITS);
  299. }
  300. void fat_attach(struct inode *inode, loff_t i_pos)
  301. {
  302. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  303. struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
  304. spin_lock(&sbi->inode_hash_lock);
  305. MSDOS_I(inode)->i_pos = i_pos;
  306. hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head);
  307. spin_unlock(&sbi->inode_hash_lock);
  308. }
  309. EXPORT_SYMBOL_GPL(fat_attach);
  310. void fat_detach(struct inode *inode)
  311. {
  312. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  313. spin_lock(&sbi->inode_hash_lock);
  314. MSDOS_I(inode)->i_pos = 0;
  315. hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
  316. spin_unlock(&sbi->inode_hash_lock);
  317. }
  318. EXPORT_SYMBOL_GPL(fat_detach);
  319. struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
  320. {
  321. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  322. struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos);
  323. struct hlist_node *_p;
  324. struct msdos_inode_info *i;
  325. struct inode *inode = NULL;
  326. spin_lock(&sbi->inode_hash_lock);
  327. hlist_for_each_entry(i, _p, head, i_fat_hash) {
  328. BUG_ON(i->vfs_inode.i_sb != sb);
  329. if (i->i_pos != i_pos)
  330. continue;
  331. inode = igrab(&i->vfs_inode);
  332. if (inode)
  333. break;
  334. }
  335. spin_unlock(&sbi->inode_hash_lock);
  336. return inode;
  337. }
  338. static int is_exec(unsigned char *extension)
  339. {
  340. unsigned char *exe_extensions = "EXECOMBAT", *walk;
  341. for (walk = exe_extensions; *walk; walk += 3)
  342. if (!strncmp(extension, walk, 3))
  343. return 1;
  344. return 0;
  345. }
  346. static int fat_calc_dir_size(struct inode *inode)
  347. {
  348. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  349. int ret, fclus, dclus;
  350. inode->i_size = 0;
  351. if (MSDOS_I(inode)->i_start == 0)
  352. return 0;
  353. ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
  354. if (ret < 0)
  355. return ret;
  356. inode->i_size = (fclus + 1) << sbi->cluster_bits;
  357. return 0;
  358. }
  359. /* doesn't deal with root inode */
  360. static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
  361. {
  362. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  363. int error;
  364. MSDOS_I(inode)->i_pos = 0;
  365. inode->i_uid = sbi->options.fs_uid;
  366. inode->i_gid = sbi->options.fs_gid;
  367. inode->i_version++;
  368. inode->i_generation = get_seconds();
  369. if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
  370. inode->i_generation &= ~1;
  371. inode->i_mode = fat_make_mode(sbi, de->attr, S_IRWXUGO);
  372. inode->i_op = sbi->dir_ops;
  373. inode->i_fop = &fat_dir_operations;
  374. MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
  375. MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  376. error = fat_calc_dir_size(inode);
  377. if (error < 0)
  378. return error;
  379. MSDOS_I(inode)->mmu_private = inode->i_size;
  380. set_nlink(inode, fat_subdirs(inode));
  381. } else { /* not a directory */
  382. inode->i_generation |= 1;
  383. inode->i_mode = fat_make_mode(sbi, de->attr,
  384. ((sbi->options.showexec && !is_exec(de->name + 8))
  385. ? S_IRUGO|S_IWUGO : S_IRWXUGO));
  386. MSDOS_I(inode)->i_start = fat_get_start(sbi, de);
  387. MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
  388. inode->i_size = le32_to_cpu(de->size);
  389. inode->i_op = &fat_file_inode_operations;
  390. inode->i_fop = &fat_file_operations;
  391. inode->i_mapping->a_ops = &fat_aops;
  392. MSDOS_I(inode)->mmu_private = inode->i_size;
  393. }
  394. if (de->attr & ATTR_SYS) {
  395. if (sbi->options.sys_immutable)
  396. inode->i_flags |= S_IMMUTABLE;
  397. }
  398. fat_save_attrs(inode, de->attr);
  399. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  400. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  401. fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
  402. if (sbi->options.isvfat) {
  403. fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
  404. de->cdate, de->ctime_cs);
  405. fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
  406. } else
  407. inode->i_ctime = inode->i_atime = inode->i_mtime;
  408. return 0;
  409. }
  410. struct inode *fat_build_inode(struct super_block *sb,
  411. struct msdos_dir_entry *de, loff_t i_pos)
  412. {
  413. struct inode *inode;
  414. int err;
  415. inode = fat_iget(sb, i_pos);
  416. if (inode)
  417. goto out;
  418. inode = new_inode(sb);
  419. if (!inode) {
  420. inode = ERR_PTR(-ENOMEM);
  421. goto out;
  422. }
  423. inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
  424. inode->i_version = 1;
  425. err = fat_fill_inode(inode, de);
  426. if (err) {
  427. iput(inode);
  428. inode = ERR_PTR(err);
  429. goto out;
  430. }
  431. fat_attach(inode, i_pos);
  432. insert_inode_hash(inode);
  433. out:
  434. return inode;
  435. }
  436. EXPORT_SYMBOL_GPL(fat_build_inode);
  437. static void fat_evict_inode(struct inode *inode)
  438. {
  439. truncate_inode_pages(&inode->i_data, 0);
  440. if (!inode->i_nlink) {
  441. inode->i_size = 0;
  442. fat_truncate_blocks(inode, 0);
  443. }
  444. invalidate_inode_buffers(inode);
  445. end_writeback(inode);
  446. fat_cache_inval_inode(inode);
  447. fat_detach(inode);
  448. }
  449. static void fat_put_super(struct super_block *sb)
  450. {
  451. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  452. fat_msg(sb, KERN_INFO, "trying to unmount...");
  453. ST_LOG("<%s> trying to umount... %d:%d",__func__,MAJOR(sb->s_dev),MINOR(sb->s_dev));
  454. iput(sbi->fsinfo_inode);
  455. iput(sbi->fat_inode);
  456. unload_nls(sbi->nls_disk);
  457. unload_nls(sbi->nls_io);
  458. if (sbi->options.iocharset != fat_default_iocharset)
  459. kfree(sbi->options.iocharset);
  460. sb->s_fs_info = NULL;
  461. kfree(sbi);
  462. fat_msg(sb, KERN_INFO, "unmounted successfully!");
  463. ST_LOG("<%s> unmounted successfully! %d:%d",__func__,MAJOR(sb->s_dev),MINOR(sb->s_dev));
  464. }
  465. static struct kmem_cache *fat_inode_cachep;
  466. static struct inode *fat_alloc_inode(struct super_block *sb)
  467. {
  468. struct msdos_inode_info *ei;
  469. ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
  470. if (!ei)
  471. return NULL;
  472. init_rwsem(&ei->truncate_lock);
  473. /* Zeroing to allow iput() even if partial initialized inode. */
  474. ei->mmu_private = 0;
  475. ei->i_start = 0;
  476. ei->i_logstart = 0;
  477. ei->i_attrs = 0;
  478. ei->i_pos = 0;
  479. return &ei->vfs_inode;
  480. }
  481. static void fat_i_callback(struct rcu_head *head)
  482. {
  483. struct inode *inode = container_of(head, struct inode, i_rcu);
  484. kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
  485. }
  486. static void fat_destroy_inode(struct inode *inode)
  487. {
  488. call_rcu(&inode->i_rcu, fat_i_callback);
  489. }
  490. static void init_once(void *foo)
  491. {
  492. struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
  493. spin_lock_init(&ei->cache_lru_lock);
  494. ei->nr_caches = 0;
  495. ei->cache_valid_id = FAT_CACHE_VALID + 1;
  496. INIT_LIST_HEAD(&ei->cache_lru);
  497. INIT_HLIST_NODE(&ei->i_fat_hash);
  498. inode_init_once(&ei->vfs_inode);
  499. }
  500. static int __init fat_init_inodecache(void)
  501. {
  502. fat_inode_cachep = kmem_cache_create("fat_inode_cache",
  503. sizeof(struct msdos_inode_info),
  504. 0, (SLAB_RECLAIM_ACCOUNT|
  505. SLAB_MEM_SPREAD),
  506. init_once);
  507. if (fat_inode_cachep == NULL)
  508. return -ENOMEM;
  509. return 0;
  510. }
  511. static void __exit fat_destroy_inodecache(void)
  512. {
  513. /*
  514. * Make sure all delayed rcu free inodes are flushed before we
  515. * destroy cache.
  516. */
  517. rcu_barrier();
  518. kmem_cache_destroy(fat_inode_cachep);
  519. }
  520. static int fat_remount(struct super_block *sb, int *flags, char *data)
  521. {
  522. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  523. *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
  524. sync_filesystem(sb);
  525. return 0;
  526. }
  527. static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
  528. {
  529. struct super_block *sb = dentry->d_sb;
  530. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  531. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  532. /* If the count of free cluster is still unknown, counts it here. */
  533. if (sbi->free_clusters == -1 || !sbi->free_clus_valid) {
  534. int err = fat_count_free_clusters(dentry->d_sb);
  535. if (err)
  536. return err;
  537. }
  538. buf->f_type = dentry->d_sb->s_magic;
  539. buf->f_bsize = sbi->cluster_size;
  540. buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
  541. buf->f_bfree = sbi->free_clusters;
  542. buf->f_bavail = sbi->free_clusters;
  543. buf->f_fsid.val[0] = (u32)id;
  544. buf->f_fsid.val[1] = (u32)(id >> 32);
  545. buf->f_namelen =
  546. (sbi->options.isvfat ? FAT_LFN_LEN : 12) * NLS_MAX_CHARSET_SIZE;
  547. return 0;
  548. }
  549. static inline loff_t fat_i_pos_read(struct msdos_sb_info *sbi,
  550. struct inode *inode)
  551. {
  552. loff_t i_pos;
  553. #if BITS_PER_LONG == 32
  554. spin_lock(&sbi->inode_hash_lock);
  555. #endif
  556. i_pos = MSDOS_I(inode)->i_pos;
  557. #if BITS_PER_LONG == 32
  558. spin_unlock(&sbi->inode_hash_lock);
  559. #endif
  560. return i_pos;
  561. }
  562. static int __fat_write_inode(struct inode *inode, int wait)
  563. {
  564. struct super_block *sb = inode->i_sb;
  565. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  566. struct buffer_head *bh;
  567. struct msdos_dir_entry *raw_entry;
  568. loff_t i_pos;
  569. int err;
  570. if (inode->i_ino == MSDOS_ROOT_INO)
  571. return 0;
  572. retry:
  573. i_pos = fat_i_pos_read(sbi, inode);
  574. if (!i_pos)
  575. return 0;
  576. bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits);
  577. if (!bh) {
  578. fat_msg(sb, KERN_ERR, "unable to read inode block "
  579. "for updating (i_pos %lld)", i_pos);
  580. return -EIO;
  581. }
  582. spin_lock(&sbi->inode_hash_lock);
  583. if (i_pos != MSDOS_I(inode)->i_pos) {
  584. spin_unlock(&sbi->inode_hash_lock);
  585. brelse(bh);
  586. goto retry;
  587. }
  588. raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
  589. [i_pos & (sbi->dir_per_block - 1)];
  590. if (S_ISDIR(inode->i_mode))
  591. raw_entry->size = 0;
  592. else
  593. raw_entry->size = cpu_to_le32(inode->i_size);
  594. raw_entry->attr = fat_make_attrs(inode);
  595. fat_set_start(raw_entry, MSDOS_I(inode)->i_logstart);
  596. fat_time_unix2fat(sbi, &inode->i_mtime, &raw_entry->time,
  597. &raw_entry->date, NULL);
  598. if (sbi->options.isvfat) {
  599. __le16 atime;
  600. fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
  601. &raw_entry->cdate, &raw_entry->ctime_cs);
  602. fat_time_unix2fat(sbi, &inode->i_atime, &atime,
  603. &raw_entry->adate, NULL);
  604. }
  605. spin_unlock(&sbi->inode_hash_lock);
  606. mark_buffer_dirty_sync(bh);
  607. err = 0;
  608. if (wait)
  609. err = sync_dirty_buffer(bh);
  610. brelse(bh);
  611. return err;
  612. }
  613. static int fat_write_inode(struct inode *inode, struct writeback_control *wbc)
  614. {
  615. int err;
  616. if (inode->i_ino == MSDOS_FSINFO_INO) {
  617. struct super_block *sb = inode->i_sb;
  618. lock_super(sb);
  619. err = fat_clusters_flush(sb);
  620. unlock_super(sb);
  621. } else
  622. err = __fat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  623. return err;
  624. }
  625. int fat_sync_inode(struct inode *inode)
  626. {
  627. return __fat_write_inode(inode, 1);
  628. }
  629. EXPORT_SYMBOL_GPL(fat_sync_inode);
  630. static int fat_show_options(struct seq_file *m, struct dentry *root);
  631. static const struct super_operations fat_sops = {
  632. .alloc_inode = fat_alloc_inode,
  633. .destroy_inode = fat_destroy_inode,
  634. .write_inode = fat_write_inode,
  635. .evict_inode = fat_evict_inode,
  636. .put_super = fat_put_super,
  637. .statfs = fat_statfs,
  638. .remount_fs = fat_remount,
  639. .show_options = fat_show_options,
  640. };
  641. /*
  642. * a FAT file handle with fhtype 3 is
  643. * 0/ i_ino - for fast, reliable lookup if still in the cache
  644. * 1/ i_generation - to see if i_ino is still valid
  645. * bit 0 == 0 iff directory
  646. * 2/ i_pos(8-39) - if ino has changed, but still in cache
  647. * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
  648. * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
  649. *
  650. * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
  651. * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
  652. * of i_logstart is used to store the directory entry offset.
  653. */
  654. static struct dentry *fat_fh_to_dentry(struct super_block *sb,
  655. struct fid *fid, int fh_len, int fh_type)
  656. {
  657. struct inode *inode = NULL;
  658. u32 *fh = fid->raw;
  659. if (fh_len < 5 || fh_type != 3)
  660. return NULL;
  661. inode = ilookup(sb, fh[0]);
  662. if (!inode || inode->i_generation != fh[1]) {
  663. if (inode)
  664. iput(inode);
  665. inode = NULL;
  666. }
  667. if (!inode) {
  668. loff_t i_pos;
  669. int i_logstart = fh[3] & 0x0fffffff;
  670. i_pos = (loff_t)fh[2] << 8;
  671. i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
  672. /* try 2 - see if i_pos is in F-d-c
  673. * require i_logstart to be the same
  674. * Will fail if you truncate and then re-write
  675. */
  676. inode = fat_iget(sb, i_pos);
  677. if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
  678. iput(inode);
  679. inode = NULL;
  680. }
  681. }
  682. /*
  683. * For now, do nothing if the inode is not found.
  684. *
  685. * What we could do is:
  686. *
  687. * - follow the file starting at fh[4], and record the ".." entry,
  688. * and the name of the fh[2] entry.
  689. * - then follow the ".." file finding the next step up.
  690. *
  691. * This way we build a path to the root of the tree. If this works, we
  692. * lookup the path and so get this inode into the cache. Finally try
  693. * the fat_iget lookup again. If that fails, then we are totally out
  694. * of luck. But all that is for another day
  695. */
  696. return d_obtain_alias(inode);
  697. }
  698. static int
  699. fat_encode_fh(struct inode *inode, __u32 *fh, int *lenp, struct inode *parent)
  700. {
  701. int len = *lenp;
  702. u32 ipos_h, ipos_m, ipos_l;
  703. if (len < 5) {
  704. *lenp = 5;
  705. return 255; /* no room */
  706. }
  707. ipos_h = MSDOS_I(inode)->i_pos >> 8;
  708. ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
  709. ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
  710. *lenp = 5;
  711. fh[0] = inode->i_ino;
  712. fh[1] = inode->i_generation;
  713. fh[2] = ipos_h;
  714. fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
  715. fh[4] = ipos_l;
  716. if (parent)
  717. fh[4] |= MSDOS_I(parent)->i_logstart;
  718. return 3;
  719. }
  720. static struct dentry *fat_get_parent(struct dentry *child)
  721. {
  722. struct super_block *sb = child->d_sb;
  723. struct buffer_head *bh;
  724. struct msdos_dir_entry *de;
  725. loff_t i_pos;
  726. struct dentry *parent;
  727. struct inode *inode;
  728. int err;
  729. lock_super(sb);
  730. err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos);
  731. if (err) {
  732. parent = ERR_PTR(err);
  733. goto out;
  734. }
  735. inode = fat_build_inode(sb, de, i_pos);
  736. brelse(bh);
  737. parent = d_obtain_alias(inode);
  738. out:
  739. unlock_super(sb);
  740. return parent;
  741. }
  742. static const struct export_operations fat_export_ops = {
  743. .encode_fh = fat_encode_fh,
  744. .fh_to_dentry = fat_fh_to_dentry,
  745. .get_parent = fat_get_parent,
  746. };
  747. static int fat_show_options(struct seq_file *m, struct dentry *root)
  748. {
  749. struct msdos_sb_info *sbi = MSDOS_SB(root->d_sb);
  750. struct fat_mount_options *opts = &sbi->options;
  751. int isvfat = opts->isvfat;
  752. if (opts->fs_uid != 0)
  753. seq_printf(m, ",uid=%u", opts->fs_uid);
  754. if (opts->fs_gid != 0)
  755. seq_printf(m, ",gid=%u", opts->fs_gid);
  756. seq_printf(m, ",fmask=%04o", opts->fs_fmask);
  757. seq_printf(m, ",dmask=%04o", opts->fs_dmask);
  758. if (opts->allow_utime)
  759. seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
  760. if (sbi->nls_disk)
  761. seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
  762. if (isvfat) {
  763. if (sbi->nls_io)
  764. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  765. switch (opts->shortname) {
  766. case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
  767. seq_puts(m, ",shortname=win95");
  768. break;
  769. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
  770. seq_puts(m, ",shortname=winnt");
  771. break;
  772. case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
  773. seq_puts(m, ",shortname=mixed");
  774. break;
  775. case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
  776. seq_puts(m, ",shortname=lower");
  777. break;
  778. default:
  779. seq_puts(m, ",shortname=unknown");
  780. break;
  781. }
  782. }
  783. if (opts->name_check != 'n')
  784. seq_printf(m, ",check=%c", opts->name_check);
  785. if (opts->usefree)
  786. seq_puts(m, ",usefree");
  787. if (opts->quiet)
  788. seq_puts(m, ",quiet");
  789. if (opts->showexec)
  790. seq_puts(m, ",showexec");
  791. if (opts->sys_immutable)
  792. seq_puts(m, ",sys_immutable");
  793. if (!isvfat) {
  794. if (opts->dotsOK)
  795. seq_puts(m, ",dotsOK=yes");
  796. if (opts->nocase)
  797. seq_puts(m, ",nocase");
  798. } else {
  799. if (opts->utf8)
  800. seq_puts(m, ",utf8");
  801. if (opts->unicode_xlate)
  802. seq_puts(m, ",uni_xlate");
  803. if (!opts->numtail)
  804. seq_puts(m, ",nonumtail");
  805. if (opts->rodir)
  806. seq_puts(m, ",rodir");
  807. }
  808. if (opts->flush)
  809. seq_puts(m, ",flush");
  810. if (opts->tz_utc)
  811. seq_puts(m, ",tz=UTC");
  812. if (opts->errors == FAT_ERRORS_CONT)
  813. seq_puts(m, ",errors=continue");
  814. else if (opts->errors == FAT_ERRORS_PANIC)
  815. seq_puts(m, ",errors=panic");
  816. else
  817. seq_puts(m, ",errors=remount-ro");
  818. if (opts->discard)
  819. seq_puts(m, ",discard");
  820. return 0;
  821. }
  822. enum {
  823. Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
  824. Opt_umask, Opt_dmask, Opt_fmask, Opt_allow_utime, Opt_codepage,
  825. Opt_usefree, Opt_nocase, Opt_quiet, Opt_showexec, Opt_debug,
  826. Opt_immutable, Opt_dots, Opt_nodots,
  827. Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
  828. Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
  829. Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
  830. Opt_obsolete, Opt_flush, Opt_tz_utc, Opt_rodir, Opt_err_cont,
  831. Opt_err_panic, Opt_err_ro, Opt_discard, Opt_err,
  832. };
  833. static const match_table_t fat_tokens = {
  834. {Opt_check_r, "check=relaxed"},
  835. {Opt_check_s, "check=strict"},
  836. {Opt_check_n, "check=normal"},
  837. {Opt_check_r, "check=r"},
  838. {Opt_check_s, "check=s"},
  839. {Opt_check_n, "check=n"},
  840. {Opt_uid, "uid=%u"},
  841. {Opt_gid, "gid=%u"},
  842. {Opt_umask, "umask=%o"},
  843. {Opt_dmask, "dmask=%o"},
  844. {Opt_fmask, "fmask=%o"},
  845. {Opt_allow_utime, "allow_utime=%o"},
  846. {Opt_codepage, "codepage=%u"},
  847. {Opt_usefree, "usefree"},
  848. {Opt_nocase, "nocase"},
  849. {Opt_quiet, "quiet"},
  850. {Opt_showexec, "showexec"},
  851. {Opt_debug, "debug"},
  852. {Opt_immutable, "sys_immutable"},
  853. {Opt_flush, "flush"},
  854. {Opt_tz_utc, "tz=UTC"},
  855. {Opt_err_cont, "errors=continue"},
  856. {Opt_err_panic, "errors=panic"},
  857. {Opt_err_ro, "errors=remount-ro"},
  858. {Opt_discard, "discard"},
  859. {Opt_obsolete, "conv=binary"},
  860. {Opt_obsolete, "conv=text"},
  861. {Opt_obsolete, "conv=auto"},
  862. {Opt_obsolete, "conv=b"},
  863. {Opt_obsolete, "conv=t"},
  864. {Opt_obsolete, "conv=a"},
  865. {Opt_obsolete, "fat=%u"},
  866. {Opt_obsolete, "blocksize=%u"},
  867. {Opt_obsolete, "cvf_format=%20s"},
  868. {Opt_obsolete, "cvf_options=%100s"},
  869. {Opt_obsolete, "posix"},
  870. {Opt_err, NULL},
  871. };
  872. static const match_table_t msdos_tokens = {
  873. {Opt_nodots, "nodots"},
  874. {Opt_nodots, "dotsOK=no"},
  875. {Opt_dots, "dots"},
  876. {Opt_dots, "dotsOK=yes"},
  877. {Opt_err, NULL}
  878. };
  879. static const match_table_t vfat_tokens = {
  880. {Opt_charset, "iocharset=%s"},
  881. {Opt_shortname_lower, "shortname=lower"},
  882. {Opt_shortname_win95, "shortname=win95"},
  883. {Opt_shortname_winnt, "shortname=winnt"},
  884. {Opt_shortname_mixed, "shortname=mixed"},
  885. {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */
  886. {Opt_utf8_no, "utf8=no"},
  887. {Opt_utf8_no, "utf8=false"},
  888. {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */
  889. {Opt_utf8_yes, "utf8=yes"},
  890. {Opt_utf8_yes, "utf8=true"},
  891. {Opt_utf8_yes, "utf8"},
  892. {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */
  893. {Opt_uni_xl_no, "uni_xlate=no"},
  894. {Opt_uni_xl_no, "uni_xlate=false"},
  895. {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */
  896. {Opt_uni_xl_yes, "uni_xlate=yes"},
  897. {Opt_uni_xl_yes, "uni_xlate=true"},
  898. {Opt_uni_xl_yes, "uni_xlate"},
  899. {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */
  900. {Opt_nonumtail_no, "nonumtail=no"},
  901. {Opt_nonumtail_no, "nonumtail=false"},
  902. {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */
  903. {Opt_nonumtail_yes, "nonumtail=yes"},
  904. {Opt_nonumtail_yes, "nonumtail=true"},
  905. {Opt_nonumtail_yes, "nonumtail"},
  906. {Opt_rodir, "rodir"},
  907. {Opt_err, NULL}
  908. };
  909. static int parse_options(struct super_block *sb, char *options, int is_vfat,
  910. int silent, int *debug, struct fat_mount_options *opts)
  911. {
  912. char *p;
  913. substring_t args[MAX_OPT_ARGS];
  914. int option;
  915. char *iocharset;
  916. opts->isvfat = is_vfat;
  917. opts->fs_uid = current_uid();
  918. opts->fs_gid = current_gid();
  919. opts->fs_fmask = opts->fs_dmask = current_umask();
  920. opts->allow_utime = -1;
  921. opts->codepage = fat_default_codepage;
  922. opts->iocharset = fat_default_iocharset;
  923. if (is_vfat) {
  924. opts->shortname = VFAT_SFN_DISPLAY_WINNT|VFAT_SFN_CREATE_WIN95;
  925. opts->rodir = 0;
  926. } else {
  927. opts->shortname = 0;
  928. opts->rodir = 1;
  929. }
  930. opts->name_check = 'n';
  931. opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
  932. opts->utf8 = opts->unicode_xlate = 0;
  933. opts->numtail = 1;
  934. opts->usefree = opts->nocase = 0;
  935. opts->tz_utc = 0;
  936. opts->errors = FAT_ERRORS_RO;
  937. *debug = 0;
  938. if (!options)
  939. goto out;
  940. while ((p = strsep(&options, ",")) != NULL) {
  941. int token;
  942. if (!*p)
  943. continue;
  944. token = match_token(p, fat_tokens, args);
  945. if (token == Opt_err) {
  946. if (is_vfat)
  947. token = match_token(p, vfat_tokens, args);
  948. else
  949. token = match_token(p, msdos_tokens, args);
  950. }
  951. switch (token) {
  952. case Opt_check_s:
  953. opts->name_check = 's';
  954. break;
  955. case Opt_check_r:
  956. opts->name_check = 'r';
  957. break;
  958. case Opt_check_n:
  959. opts->name_check = 'n';
  960. break;
  961. case Opt_usefree:
  962. opts->usefree = 1;
  963. break;
  964. case Opt_nocase:
  965. if (!is_vfat)
  966. opts->nocase = 1;
  967. else {
  968. /* for backward compatibility */
  969. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  970. | VFAT_SFN_CREATE_WIN95;
  971. }
  972. break;
  973. case Opt_quiet:
  974. opts->quiet = 1;
  975. break;
  976. case Opt_showexec:
  977. opts->showexec = 1;
  978. break;
  979. case Opt_debug:
  980. *debug = 1;
  981. break;
  982. case Opt_immutable:
  983. opts->sys_immutable = 1;
  984. break;
  985. case Opt_uid:
  986. if (match_int(&args[0], &option))
  987. return 0;
  988. opts->fs_uid = option;
  989. break;
  990. case Opt_gid:
  991. if (match_int(&args[0], &option))
  992. return 0;
  993. opts->fs_gid = option;
  994. break;
  995. case Opt_umask:
  996. if (match_octal(&args[0], &option))
  997. return 0;
  998. opts->fs_fmask = opts->fs_dmask = option;
  999. break;
  1000. case Opt_dmask:
  1001. if (match_octal(&args[0], &option))
  1002. return 0;
  1003. opts->fs_dmask = option;
  1004. break;
  1005. case Opt_fmask:
  1006. if (match_octal(&args[0], &option))
  1007. return 0;
  1008. opts->fs_fmask = option;
  1009. break;
  1010. case Opt_allow_utime:
  1011. if (match_octal(&args[0], &option))
  1012. return 0;
  1013. opts->allow_utime = option & (S_IWGRP | S_IWOTH);
  1014. break;
  1015. case Opt_codepage:
  1016. if (match_int(&args[0], &option))
  1017. return 0;
  1018. opts->codepage = option;
  1019. break;
  1020. case Opt_flush:
  1021. opts->flush = 1;
  1022. break;
  1023. case Opt_tz_utc:
  1024. opts->tz_utc = 1;
  1025. break;
  1026. case Opt_err_cont:
  1027. opts->errors = FAT_ERRORS_CONT;
  1028. break;
  1029. case Opt_err_panic:
  1030. opts->errors = FAT_ERRORS_PANIC;
  1031. break;
  1032. case Opt_err_ro:
  1033. opts->errors = FAT_ERRORS_RO;
  1034. break;
  1035. /* msdos specific */
  1036. case Opt_dots:
  1037. opts->dotsOK = 1;
  1038. break;
  1039. case Opt_nodots:
  1040. opts->dotsOK = 0;
  1041. break;
  1042. /* vfat specific */
  1043. case Opt_charset:
  1044. if (opts->iocharset != fat_default_iocharset)
  1045. kfree(opts->iocharset);
  1046. iocharset = match_strdup(&args[0]);
  1047. if (!iocharset)
  1048. return -ENOMEM;
  1049. opts->iocharset = iocharset;
  1050. break;
  1051. case Opt_shortname_lower:
  1052. opts->shortname = VFAT_SFN_DISPLAY_LOWER
  1053. | VFAT_SFN_CREATE_WIN95;
  1054. break;
  1055. case Opt_shortname_win95:
  1056. opts->shortname = VFAT_SFN_DISPLAY_WIN95
  1057. | VFAT_SFN_CREATE_WIN95;
  1058. break;
  1059. case Opt_shortname_winnt:
  1060. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  1061. | VFAT_SFN_CREATE_WINNT;
  1062. break;
  1063. case Opt_shortname_mixed:
  1064. opts->shortname = VFAT_SFN_DISPLAY_WINNT
  1065. | VFAT_SFN_CREATE_WIN95;
  1066. break;
  1067. case Opt_utf8_no: /* 0 or no or false */
  1068. opts->utf8 = 0;
  1069. break;
  1070. case Opt_utf8_yes: /* empty or 1 or yes or true */
  1071. opts->utf8 = 1;
  1072. break;
  1073. case Opt_uni_xl_no: /* 0 or no or false */
  1074. opts->unicode_xlate = 0;
  1075. break;
  1076. case Opt_uni_xl_yes: /* empty or 1 or yes or true */
  1077. opts->unicode_xlate = 1;
  1078. break;
  1079. case Opt_nonumtail_no: /* 0 or no or false */
  1080. opts->numtail = 1; /* negated option */
  1081. break;
  1082. case Opt_nonumtail_yes: /* empty or 1 or yes or true */
  1083. opts->numtail = 0; /* negated option */
  1084. break;
  1085. case Opt_rodir:
  1086. opts->rodir = 1;
  1087. break;
  1088. case Opt_discard:
  1089. opts->discard = 1;
  1090. break;
  1091. /* obsolete mount options */
  1092. case Opt_obsolete:
  1093. fat_msg(sb, KERN_INFO, "\"%s\" option is obsolete, "
  1094. "not supported now", p);
  1095. break;
  1096. /* unknown option */
  1097. default:
  1098. if (!silent) {
  1099. fat_msg(sb, KERN_ERR,
  1100. "Unrecognized mount option \"%s\" "
  1101. "or missing value", p);
  1102. }
  1103. return -EINVAL;
  1104. }
  1105. }
  1106. out:
  1107. /* UTF-8 doesn't provide FAT semantics */
  1108. if (!strcmp(opts->iocharset, "utf8")) {
  1109. fat_msg(sb, KERN_WARNING, "utf8 is not a recommended IO charset"
  1110. " for FAT filesystems, filesystem will be "
  1111. "case sensitive!");
  1112. }
  1113. /* If user doesn't specify allow_utime, it's initialized from dmask. */
  1114. if (opts->allow_utime == (unsigned short)-1)
  1115. opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
  1116. if (opts->unicode_xlate)
  1117. opts->utf8 = 0;
  1118. return 0;
  1119. }
  1120. static int fat_read_root(struct inode *inode)
  1121. {
  1122. struct super_block *sb = inode->i_sb;
  1123. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1124. int error;
  1125. MSDOS_I(inode)->i_pos = 0;
  1126. inode->i_uid = sbi->options.fs_uid;
  1127. inode->i_gid = sbi->options.fs_gid;
  1128. inode->i_version++;
  1129. inode->i_generation = 0;
  1130. inode->i_mode = fat_make_mode(sbi, ATTR_DIR, S_IRWXUGO);
  1131. inode->i_op = sbi->dir_ops;
  1132. inode->i_fop = &fat_dir_operations;
  1133. if (sbi->fat_bits == 32) {
  1134. MSDOS_I(inode)->i_start = sbi->root_cluster;
  1135. error = fat_calc_dir_size(inode);
  1136. if (error < 0)
  1137. return error;
  1138. } else {
  1139. MSDOS_I(inode)->i_start = 0;
  1140. inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
  1141. }
  1142. inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
  1143. & ~((loff_t)sbi->cluster_size - 1)) >> 9;
  1144. MSDOS_I(inode)->i_logstart = 0;
  1145. MSDOS_I(inode)->mmu_private = inode->i_size;
  1146. fat_save_attrs(inode, ATTR_DIR);
  1147. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  1148. set_nlink(inode, fat_subdirs(inode)+2);
  1149. return 0;
  1150. }
  1151. static unsigned long calc_fat_clusters(struct super_block *sb)
  1152. {
  1153. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  1154. /* Divide first to avoid overflow */
  1155. if (sbi->fat_bits != 12) {
  1156. unsigned long ent_per_sec = sb->s_blocksize * 8 / sbi->fat_bits;
  1157. return ent_per_sec * sbi->fat_length;
  1158. }
  1159. return sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
  1160. }
  1161. /*
  1162. * Read the super block of an MS-DOS FS.
  1163. */
  1164. int fat_fill_super(struct super_block *sb, void *data, int silent, int isvfat,
  1165. void (*setup)(struct super_block *))
  1166. {
  1167. struct inode *root_inode = NULL, *fat_inode = NULL;
  1168. struct inode *fsinfo_inode = NULL;
  1169. struct buffer_head *bh;
  1170. struct fat_boot_sector *b;
  1171. struct fat_boot_bsx *bsx;
  1172. struct msdos_sb_info *sbi;
  1173. u16 logical_sector_size;
  1174. u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
  1175. int debug;
  1176. unsigned int media;
  1177. long error;
  1178. char buf[50];
  1179. fat_msg(sb, KERN_INFO, "trying to mount...");
  1180. ST_LOG("<%s> trying to mount... %d:%d",__func__,MAJOR(sb->s_dev),MINOR(sb->s_dev));
  1181. /*
  1182. * GFP_KERNEL is ok here, because while we do hold the
  1183. * supeblock lock, memory pressure can't call back into
  1184. * the filesystem, since we're only just about to mount
  1185. * it and have no inodes etc active!
  1186. */
  1187. sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
  1188. if (!sbi) {
  1189. fat_msg(sb, KERN_ERR, "failed to mount! (ENOMEM)");
  1190. ST_LOG("<%s> failed to mount! %d:%d (ENOMEM)",__func__,MAJOR(sb->s_dev),MINOR(sb->s_dev));
  1191. return -ENOMEM;
  1192. }
  1193. sb->s_fs_info = sbi;
  1194. sb->s_flags |= MS_NODIRATIME;
  1195. sb->s_magic = MSDOS_SUPER_MAGIC;
  1196. sb->s_op = &fat_sops;
  1197. sb->s_export_op = &fat_export_ops;
  1198. ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
  1199. DEFAULT_RATELIMIT_BURST);
  1200. error = parse_options(sb, data, isvfat, silent, &debug, &sbi->options);
  1201. if (error)
  1202. goto out_fail;
  1203. setup(sb); /* flavour-specific stuff that needs options */
  1204. error = -EIO;
  1205. sb_min_blocksize(sb, 512);
  1206. bh = sb_bread(sb, 0);
  1207. if (bh == NULL) {
  1208. fat_msg(sb, KERN_ERR, "unable to read boot sector");
  1209. goto out_fail;
  1210. }
  1211. b = (struct fat_boot_sector *) bh->b_data;
  1212. if (!b->reserved) {
  1213. if (!silent)
  1214. fat_msg(sb, KERN_ERR, "bogus number of reserved sectors");
  1215. brelse(bh);
  1216. goto out_invalid;
  1217. }
  1218. if (!b->fats) {
  1219. if (!silent)
  1220. fat_msg(sb, KERN_ERR, "bogus number of FAT structure");
  1221. brelse(bh);
  1222. goto out_invalid;
  1223. }
  1224. /*
  1225. * Earlier we checked here that b->secs_track and b->head are nonzero,
  1226. * but it turns out valid FAT filesystems can have zero there.
  1227. */
  1228. media = b->media;
  1229. if (!fat_valid_media(media)) {
  1230. if (!silent)
  1231. fat_msg(sb, KERN_ERR, "invalid media value (0x%02x)",
  1232. media);
  1233. brelse(bh);
  1234. goto out_invalid;
  1235. }
  1236. logical_sector_size = get_unaligned_le16(&b->sector_size);
  1237. if (!is_power_of_2(logical_sector_size)
  1238. || (logical_sector_size < 512)
  1239. || (logical_sector_size > 4096)) {
  1240. if (!silent)
  1241. fat_msg(sb, KERN_ERR, "bogus logical sector size %u",
  1242. logical_sector_size);
  1243. brelse(bh);
  1244. goto out_invalid;
  1245. }
  1246. sbi->sec_per_clus = b->sec_per_clus;
  1247. if (!is_power_of_2(sbi->sec_per_clus)) {
  1248. if (!silent)
  1249. fat_msg(sb, KERN_ERR, "bogus sectors per cluster %u",
  1250. sbi->sec_per_clus);
  1251. brelse(bh);
  1252. goto out_invalid;
  1253. }
  1254. if (logical_sector_size < sb->s_blocksize) {
  1255. fat_msg(sb, KERN_ERR, "logical sector size too small for device"
  1256. " (logical sector size = %u)", logical_sector_size);
  1257. brelse(bh);
  1258. goto out_fail;
  1259. }
  1260. if (logical_sector_size > sb->s_blocksize) {
  1261. brelse(bh);
  1262. if (!sb_set_blocksize(sb, logical_sector_size)) {
  1263. fat_msg(sb, KERN_ERR, "unable to set blocksize %u",
  1264. logical_sector_size);
  1265. goto out_fail;
  1266. }
  1267. bh = sb_bread(sb, 0);
  1268. if (bh == NULL) {
  1269. fat_msg(sb, KERN_ERR, "unable to read boot sector"
  1270. " (logical sector size = %lu)",
  1271. sb->s_blocksize);
  1272. goto out_fail;
  1273. }
  1274. b = (struct fat_boot_sector *) bh->b_data;
  1275. }
  1276. sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
  1277. sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
  1278. sbi->fats = b->fats;
  1279. sbi->fat_bits = 0; /* Don't know yet */
  1280. sbi->fat_start = le16_to_cpu(b->reserved);
  1281. sbi->fat_length = le16_to_cpu(b->fat_length);
  1282. sbi->root_cluster = 0;
  1283. sbi->free_clusters = -1; /* Don't know yet */
  1284. sbi->free_clus_valid = 0;
  1285. sbi->prev_free = FAT_START_ENT;
  1286. sb->s_maxbytes = 0xffffffff;
  1287. if (!sbi->fat_length && b->fat32_length) {
  1288. struct fat_boot_fsinfo *fsinfo;
  1289. struct buffer_head *fsinfo_bh;
  1290. /* Must be FAT32 */
  1291. sbi->fat_bits = 32;
  1292. sbi->fat_length = le32_to_cpu(b->fat32_length);
  1293. sbi->root_cluster = le32_to_cpu(b->root_cluster);
  1294. /* MC - if info_sector is 0, don't multiply by 0 */
  1295. sbi->fsinfo_sector = le16_to_cpu(b->info_sector);
  1296. if (sbi->fsinfo_sector == 0)
  1297. sbi->fsinfo_sector = 1;
  1298. fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
  1299. if (fsinfo_bh == NULL) {
  1300. fat_msg(sb, KERN_ERR, "bread failed, FSINFO block"
  1301. " (sector = %lu)", sbi->fsinfo_sector);
  1302. brelse(bh);
  1303. goto out_fail;
  1304. }
  1305. bsx = (struct fat_boot_bsx *)(bh->b_data + FAT32_BSX_OFFSET);
  1306. fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
  1307. if (!IS_FSINFO(fsinfo)) {
  1308. fat_msg(sb, KERN_WARNING, "Invalid FSINFO signature: "
  1309. "0x%08x, 0x%08x (sector = %lu)",
  1310. le32_to_cpu(fsinfo->signature1),
  1311. le32_to_cpu(fsinfo->signature2),
  1312. sbi->fsinfo_sector);
  1313. } else {
  1314. if (sbi->options.usefree)
  1315. sbi->free_clus_valid = 1;
  1316. sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
  1317. sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
  1318. }
  1319. brelse(fsinfo_bh);
  1320. } else {
  1321. bsx = (struct fat_boot_bsx *)(bh->b_data + FAT16_BSX_OFFSET);
  1322. }
  1323. /* interpret volume ID as a little endian 32 bit integer */
  1324. sbi->vol_id = (((u32)bsx->vol_id[0]) | ((u32)bsx->vol_id[1] << 8) |
  1325. ((u32)bsx->vol_id[2] << 16) | ((u32)bsx->vol_id[3] << 24));
  1326. sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
  1327. sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
  1328. sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
  1329. sbi->dir_entries = get_unaligned_le16(&b->dir_entries);
  1330. if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
  1331. if (!silent)
  1332. fat_msg(sb, KERN_ERR, "bogus directroy-entries per block"
  1333. " (%u)", sbi->dir_entries);
  1334. brelse(bh);
  1335. goto out_invalid;
  1336. }
  1337. rootdir_sectors = sbi->dir_entries
  1338. * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
  1339. sbi->data_start = sbi->dir_start + rootdir_sectors;
  1340. total_sectors = get_unaligned_le16(&b->sectors);
  1341. if (total_sectors == 0)
  1342. total_sectors = le32_to_cpu(b->total_sect);
  1343. total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
  1344. if (sbi->fat_bits != 32)
  1345. sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
  1346. /* check that FAT table does not overflow */
  1347. fat_clusters = calc_fat_clusters(sb);
  1348. total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
  1349. if (total_clusters > MAX_FAT(sb)) {
  1350. if (!silent)
  1351. fat_msg(sb, KERN_ERR, "count of clusters too big (%u)",
  1352. total_clusters);
  1353. brelse(bh);
  1354. goto out_invalid;
  1355. }
  1356. sbi->max_cluster = total_clusters + FAT_START_ENT;
  1357. /* check the free_clusters, it's not necessarily correct */
  1358. if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
  1359. sbi->free_clusters = -1;
  1360. /* check the prev_free, it's not necessarily correct */
  1361. sbi->prev_free %= sbi->max_cluster;
  1362. if (sbi->prev_free < FAT_START_ENT)
  1363. sbi->prev_free = FAT_START_ENT;
  1364. brelse(bh);
  1365. /* set up enough so that it can read an inode */
  1366. fat_hash_init(sb);
  1367. fat_ent_access_init(sb);
  1368. /*
  1369. * The low byte of FAT's first entry must have same value with
  1370. * media-field. But in real world, too many devices is
  1371. * writing wrong value. So, removed that validity check.
  1372. *
  1373. * if (FAT_FIRST_ENT(sb, media) != first)
  1374. */
  1375. error = -EINVAL;
  1376. sprintf(buf, "cp%d", sbi->options.codepage);
  1377. sbi->nls_disk = load_nls(buf);
  1378. if (!sbi->nls_disk) {
  1379. fat_msg(sb, KERN_ERR, "codepage %s not found", buf);
  1380. goto out_fail;
  1381. }
  1382. /* FIXME: utf8 is using iocharset for upper/lower conversion */
  1383. if (sbi->options.isvfat) {
  1384. sbi->nls_io = load_nls(sbi->options.iocharset);
  1385. if (!sbi->nls_io) {
  1386. fat_msg(sb, KERN_ERR, "IO charset %s not found",
  1387. sbi->options.iocharset);
  1388. goto out_fail;
  1389. }
  1390. }
  1391. error = -ENOMEM;
  1392. fat_inode = new_inode(sb);
  1393. if (!fat_inode)
  1394. goto out_fail;
  1395. MSDOS_I(fat_inode)->i_pos = 0;
  1396. sbi->fat_inode = fat_inode;
  1397. fsinfo_inode = new_inode(sb);
  1398. if (!fsinfo_inode)
  1399. goto out_fail;
  1400. fsinfo_inode->i_ino = MSDOS_FSINFO_INO;
  1401. sbi->fsinfo_inode = fsinfo_inode;
  1402. insert_inode_hash(fsinfo_inode);
  1403. root_inode = new_inode(sb);
  1404. if (!root_inode)
  1405. goto out_fail;
  1406. root_inode->i_ino = MSDOS_ROOT_INO;
  1407. root_inode->i_version = 1;
  1408. error = fat_read_root(root_inode);
  1409. if (error < 0) {
  1410. iput(root_inode);
  1411. goto out_fail;
  1412. }
  1413. error = -ENOMEM;
  1414. insert_inode_hash(root_inode);
  1415. sb->s_root = d_make_root(root_inode);
  1416. if (!sb->s_root) {
  1417. fat_msg(sb, KERN_ERR, "get root inode failed");
  1418. goto out_fail;
  1419. }
  1420. fat_msg(sb, KERN_INFO, "mounted successfully!");
  1421. ST_LOG("<%s> mounted successfully! %d:%d",__func__,MAJOR(sb->s_dev),MINOR(sb->s_dev));
  1422. return 0;
  1423. out_invalid:
  1424. error = -EINVAL;
  1425. if (!silent)
  1426. fat_msg(sb, KERN_INFO, "Can't find a valid FAT filesystem");
  1427. out_fail:
  1428. fat_msg(sb, KERN_ERR, "failed to mount!");
  1429. ST_LOG("<%s> failed to mount %d:%d",__func__,MAJOR(sb->s_dev),MINOR(sb->s_dev));
  1430. if (fsinfo_inode)
  1431. iput(fsinfo_inode);
  1432. if (fat_inode)
  1433. iput(fat_inode);
  1434. unload_nls(sbi->nls_io);
  1435. unload_nls(sbi->nls_disk);
  1436. if (sbi->options.iocharset != fat_default_iocharset)
  1437. kfree(sbi->options.iocharset);
  1438. sb->s_fs_info = NULL;
  1439. kfree(sbi);
  1440. return error;
  1441. }
  1442. EXPORT_SYMBOL_GPL(fat_fill_super);
  1443. /*
  1444. * helper function for fat_flush_inodes. This writes both the inode
  1445. * and the file data blocks, waiting for in flight data blocks before
  1446. * the start of the call. It does not wait for any io started
  1447. * during the call
  1448. */
  1449. static int writeback_inode(struct inode *inode)
  1450. {
  1451. int ret;
  1452. struct address_space *mapping = inode->i_mapping;
  1453. struct writeback_control wbc = {
  1454. .sync_mode = WB_SYNC_NONE,
  1455. .nr_to_write = 0,
  1456. };
  1457. /* if we used WB_SYNC_ALL, sync_inode waits for the io for the
  1458. * inode to finish. So WB_SYNC_NONE is sent down to sync_inode
  1459. * and filemap_fdatawrite is used for the data blocks
  1460. */
  1461. ret = sync_inode(inode, &wbc);
  1462. if (!ret)
  1463. ret = filemap_fdatawrite(mapping);
  1464. return ret;
  1465. }
  1466. /*
  1467. * write data and metadata corresponding to i1 and i2. The io is
  1468. * started but we do not wait for any of it to finish.
  1469. *
  1470. * filemap_flush is used for the block device, so if there is a dirty
  1471. * page for a block already in flight, we will not wait and start the
  1472. * io over again
  1473. */
  1474. int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
  1475. {
  1476. int ret = 0;
  1477. if (!MSDOS_SB(sb)->options.flush)
  1478. return 0;
  1479. if (i1)
  1480. ret = writeback_inode(i1);
  1481. if (!ret && i2)
  1482. ret = writeback_inode(i2);
  1483. if (!ret) {
  1484. struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
  1485. ret = filemap_flush(mapping);
  1486. }
  1487. return ret;
  1488. }
  1489. EXPORT_SYMBOL_GPL(fat_flush_inodes);
  1490. static int __init init_fat_fs(void)
  1491. {
  1492. int err;
  1493. err = fat_cache_init();
  1494. if (err)
  1495. return err;
  1496. err = fat_init_inodecache();
  1497. if (err)
  1498. goto failed;
  1499. return 0;
  1500. failed:
  1501. fat_cache_destroy();
  1502. return err;
  1503. }
  1504. static void __exit exit_fat_fs(void)
  1505. {
  1506. fat_cache_destroy();
  1507. fat_destroy_inodecache();
  1508. }
  1509. module_init(init_fat_fs)
  1510. module_exit(exit_fat_fs)
  1511. MODULE_LICENSE("GPL");