super.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. /*
  2. * linux/fs/ext2/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/fs.h>
  21. #include <linux/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/parser.h>
  25. #include <linux/random.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/exportfs.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/vfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/mount.h>
  32. #include <linux/log2.h>
  33. #include <linux/quotaops.h>
  34. #include <asm/uaccess.h>
  35. #include "ext2.h"
  36. #include "xattr.h"
  37. #include "acl.h"
  38. #include "xip.h"
  39. static void ext2_sync_super(struct super_block *sb,
  40. struct ext2_super_block *es);
  41. static int ext2_remount (struct super_block * sb, int * flags, char * data);
  42. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
  43. static int ext2_sync_fs(struct super_block *sb, int wait);
  44. void ext2_error (struct super_block * sb, const char * function,
  45. const char * fmt, ...)
  46. {
  47. va_list args;
  48. struct ext2_sb_info *sbi = EXT2_SB(sb);
  49. struct ext2_super_block *es = sbi->s_es;
  50. if (!(sb->s_flags & MS_RDONLY)) {
  51. sbi->s_mount_state |= EXT2_ERROR_FS;
  52. es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
  53. ext2_sync_super(sb, es);
  54. }
  55. va_start(args, fmt);
  56. printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
  57. vprintk(fmt, args);
  58. printk("\n");
  59. va_end(args);
  60. if (test_opt(sb, ERRORS_PANIC))
  61. panic("EXT2-fs panic from previous error\n");
  62. if (test_opt(sb, ERRORS_RO)) {
  63. printk("Remounting filesystem read-only\n");
  64. sb->s_flags |= MS_RDONLY;
  65. }
  66. }
  67. void ext2_warning (struct super_block * sb, const char * function,
  68. const char * fmt, ...)
  69. {
  70. va_list args;
  71. va_start(args, fmt);
  72. printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
  73. sb->s_id, function);
  74. vprintk(fmt, args);
  75. printk("\n");
  76. va_end(args);
  77. }
  78. void ext2_update_dynamic_rev(struct super_block *sb)
  79. {
  80. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  81. if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
  82. return;
  83. ext2_warning(sb, __func__,
  84. "updating to rev %d because of new feature flag, "
  85. "running e2fsck is recommended",
  86. EXT2_DYNAMIC_REV);
  87. es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
  88. es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
  89. es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
  90. /* leave es->s_feature_*compat flags alone */
  91. /* es->s_uuid will be set by e2fsck if empty */
  92. /*
  93. * The rest of the superblock fields should be zero, and if not it
  94. * means they are likely already in use, so leave them alone. We
  95. * can leave it up to e2fsck to clean up any inconsistencies there.
  96. */
  97. }
  98. static void ext2_put_super (struct super_block * sb)
  99. {
  100. int db_count;
  101. int i;
  102. struct ext2_sb_info *sbi = EXT2_SB(sb);
  103. lock_kernel();
  104. if (sb->s_dirt)
  105. ext2_write_super(sb);
  106. ext2_xattr_put_super(sb);
  107. if (!(sb->s_flags & MS_RDONLY)) {
  108. struct ext2_super_block *es = sbi->s_es;
  109. es->s_state = cpu_to_le16(sbi->s_mount_state);
  110. ext2_sync_super(sb, es);
  111. }
  112. db_count = sbi->s_gdb_count;
  113. for (i = 0; i < db_count; i++)
  114. if (sbi->s_group_desc[i])
  115. brelse (sbi->s_group_desc[i]);
  116. kfree(sbi->s_group_desc);
  117. kfree(sbi->s_debts);
  118. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  119. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  120. percpu_counter_destroy(&sbi->s_dirs_counter);
  121. brelse (sbi->s_sbh);
  122. sb->s_fs_info = NULL;
  123. kfree(sbi->s_blockgroup_lock);
  124. kfree(sbi);
  125. unlock_kernel();
  126. }
  127. static struct kmem_cache * ext2_inode_cachep;
  128. static struct inode *ext2_alloc_inode(struct super_block *sb)
  129. {
  130. struct ext2_inode_info *ei;
  131. ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
  132. if (!ei)
  133. return NULL;
  134. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  135. ei->i_acl = EXT2_ACL_NOT_CACHED;
  136. ei->i_default_acl = EXT2_ACL_NOT_CACHED;
  137. #endif
  138. ei->i_block_alloc_info = NULL;
  139. ei->vfs_inode.i_version = 1;
  140. return &ei->vfs_inode;
  141. }
  142. static void ext2_destroy_inode(struct inode *inode)
  143. {
  144. kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
  145. }
  146. static void init_once(void *foo)
  147. {
  148. struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
  149. rwlock_init(&ei->i_meta_lock);
  150. #ifdef CONFIG_EXT2_FS_XATTR
  151. init_rwsem(&ei->xattr_sem);
  152. #endif
  153. mutex_init(&ei->truncate_mutex);
  154. inode_init_once(&ei->vfs_inode);
  155. }
  156. static int init_inodecache(void)
  157. {
  158. ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
  159. sizeof(struct ext2_inode_info),
  160. 0, (SLAB_RECLAIM_ACCOUNT|
  161. SLAB_MEM_SPREAD),
  162. init_once);
  163. if (ext2_inode_cachep == NULL)
  164. return -ENOMEM;
  165. return 0;
  166. }
  167. static void destroy_inodecache(void)
  168. {
  169. kmem_cache_destroy(ext2_inode_cachep);
  170. }
  171. static void ext2_clear_inode(struct inode *inode)
  172. {
  173. struct ext2_block_alloc_info *rsv = EXT2_I(inode)->i_block_alloc_info;
  174. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  175. struct ext2_inode_info *ei = EXT2_I(inode);
  176. if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
  177. posix_acl_release(ei->i_acl);
  178. ei->i_acl = EXT2_ACL_NOT_CACHED;
  179. }
  180. if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
  181. posix_acl_release(ei->i_default_acl);
  182. ei->i_default_acl = EXT2_ACL_NOT_CACHED;
  183. }
  184. #endif
  185. ext2_discard_reservation(inode);
  186. EXT2_I(inode)->i_block_alloc_info = NULL;
  187. if (unlikely(rsv))
  188. kfree(rsv);
  189. }
  190. static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
  191. {
  192. struct super_block *sb = vfs->mnt_sb;
  193. struct ext2_sb_info *sbi = EXT2_SB(sb);
  194. struct ext2_super_block *es = sbi->s_es;
  195. unsigned long def_mount_opts;
  196. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  197. if (sbi->s_sb_block != 1)
  198. seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
  199. if (test_opt(sb, MINIX_DF))
  200. seq_puts(seq, ",minixdf");
  201. if (test_opt(sb, GRPID))
  202. seq_puts(seq, ",grpid");
  203. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
  204. seq_puts(seq, ",nogrpid");
  205. if (sbi->s_resuid != EXT2_DEF_RESUID ||
  206. le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
  207. seq_printf(seq, ",resuid=%u", sbi->s_resuid);
  208. }
  209. if (sbi->s_resgid != EXT2_DEF_RESGID ||
  210. le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
  211. seq_printf(seq, ",resgid=%u", sbi->s_resgid);
  212. }
  213. if (test_opt(sb, ERRORS_RO)) {
  214. int def_errors = le16_to_cpu(es->s_errors);
  215. if (def_errors == EXT2_ERRORS_PANIC ||
  216. def_errors == EXT2_ERRORS_CONTINUE) {
  217. seq_puts(seq, ",errors=remount-ro");
  218. }
  219. }
  220. if (test_opt(sb, ERRORS_CONT))
  221. seq_puts(seq, ",errors=continue");
  222. if (test_opt(sb, ERRORS_PANIC))
  223. seq_puts(seq, ",errors=panic");
  224. if (test_opt(sb, NO_UID32))
  225. seq_puts(seq, ",nouid32");
  226. if (test_opt(sb, DEBUG))
  227. seq_puts(seq, ",debug");
  228. if (test_opt(sb, OLDALLOC))
  229. seq_puts(seq, ",oldalloc");
  230. #ifdef CONFIG_EXT2_FS_XATTR
  231. if (test_opt(sb, XATTR_USER))
  232. seq_puts(seq, ",user_xattr");
  233. if (!test_opt(sb, XATTR_USER) &&
  234. (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
  235. seq_puts(seq, ",nouser_xattr");
  236. }
  237. #endif
  238. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  239. if (test_opt(sb, POSIX_ACL))
  240. seq_puts(seq, ",acl");
  241. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
  242. seq_puts(seq, ",noacl");
  243. #endif
  244. if (test_opt(sb, NOBH))
  245. seq_puts(seq, ",nobh");
  246. #if defined(CONFIG_QUOTA)
  247. if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
  248. seq_puts(seq, ",usrquota");
  249. if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
  250. seq_puts(seq, ",grpquota");
  251. #endif
  252. #if defined(CONFIG_EXT2_FS_XIP)
  253. if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
  254. seq_puts(seq, ",xip");
  255. #endif
  256. if (!test_opt(sb, RESERVATION))
  257. seq_puts(seq, ",noreservation");
  258. return 0;
  259. }
  260. #ifdef CONFIG_QUOTA
  261. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
  262. static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
  263. #endif
  264. static const struct super_operations ext2_sops = {
  265. .alloc_inode = ext2_alloc_inode,
  266. .destroy_inode = ext2_destroy_inode,
  267. .write_inode = ext2_write_inode,
  268. .delete_inode = ext2_delete_inode,
  269. .put_super = ext2_put_super,
  270. .write_super = ext2_write_super,
  271. .sync_fs = ext2_sync_fs,
  272. .statfs = ext2_statfs,
  273. .remount_fs = ext2_remount,
  274. .clear_inode = ext2_clear_inode,
  275. .show_options = ext2_show_options,
  276. #ifdef CONFIG_QUOTA
  277. .quota_read = ext2_quota_read,
  278. .quota_write = ext2_quota_write,
  279. #endif
  280. };
  281. static struct inode *ext2_nfs_get_inode(struct super_block *sb,
  282. u64 ino, u32 generation)
  283. {
  284. struct inode *inode;
  285. if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
  286. return ERR_PTR(-ESTALE);
  287. if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  288. return ERR_PTR(-ESTALE);
  289. /* iget isn't really right if the inode is currently unallocated!!
  290. * ext2_read_inode currently does appropriate checks, but
  291. * it might be "neater" to call ext2_get_inode first and check
  292. * if the inode is valid.....
  293. */
  294. inode = ext2_iget(sb, ino);
  295. if (IS_ERR(inode))
  296. return ERR_CAST(inode);
  297. if (generation && inode->i_generation != generation) {
  298. /* we didn't find the right inode.. */
  299. iput(inode);
  300. return ERR_PTR(-ESTALE);
  301. }
  302. return inode;
  303. }
  304. static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  305. int fh_len, int fh_type)
  306. {
  307. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  308. ext2_nfs_get_inode);
  309. }
  310. static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
  311. int fh_len, int fh_type)
  312. {
  313. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  314. ext2_nfs_get_inode);
  315. }
  316. /* Yes, most of these are left as NULL!!
  317. * A NULL value implies the default, which works with ext2-like file
  318. * systems, but can be improved upon.
  319. * Currently only get_parent is required.
  320. */
  321. static const struct export_operations ext2_export_ops = {
  322. .fh_to_dentry = ext2_fh_to_dentry,
  323. .fh_to_parent = ext2_fh_to_parent,
  324. .get_parent = ext2_get_parent,
  325. };
  326. static unsigned long get_sb_block(void **data)
  327. {
  328. unsigned long sb_block;
  329. char *options = (char *) *data;
  330. if (!options || strncmp(options, "sb=", 3) != 0)
  331. return 1; /* Default location */
  332. options += 3;
  333. sb_block = simple_strtoul(options, &options, 0);
  334. if (*options && *options != ',') {
  335. printk("EXT2-fs: Invalid sb specification: %s\n",
  336. (char *) *data);
  337. return 1;
  338. }
  339. if (*options == ',')
  340. options++;
  341. *data = (void *) options;
  342. return sb_block;
  343. }
  344. enum {
  345. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  346. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
  347. Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
  348. Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
  349. Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota,
  350. Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
  351. };
  352. static const match_table_t tokens = {
  353. {Opt_bsd_df, "bsddf"},
  354. {Opt_minix_df, "minixdf"},
  355. {Opt_grpid, "grpid"},
  356. {Opt_grpid, "bsdgroups"},
  357. {Opt_nogrpid, "nogrpid"},
  358. {Opt_nogrpid, "sysvgroups"},
  359. {Opt_resgid, "resgid=%u"},
  360. {Opt_resuid, "resuid=%u"},
  361. {Opt_sb, "sb=%u"},
  362. {Opt_err_cont, "errors=continue"},
  363. {Opt_err_panic, "errors=panic"},
  364. {Opt_err_ro, "errors=remount-ro"},
  365. {Opt_nouid32, "nouid32"},
  366. {Opt_nocheck, "check=none"},
  367. {Opt_nocheck, "nocheck"},
  368. {Opt_debug, "debug"},
  369. {Opt_oldalloc, "oldalloc"},
  370. {Opt_orlov, "orlov"},
  371. {Opt_nobh, "nobh"},
  372. {Opt_user_xattr, "user_xattr"},
  373. {Opt_nouser_xattr, "nouser_xattr"},
  374. {Opt_acl, "acl"},
  375. {Opt_noacl, "noacl"},
  376. {Opt_xip, "xip"},
  377. {Opt_grpquota, "grpquota"},
  378. {Opt_ignore, "noquota"},
  379. {Opt_quota, "quota"},
  380. {Opt_usrquota, "usrquota"},
  381. {Opt_reservation, "reservation"},
  382. {Opt_noreservation, "noreservation"},
  383. {Opt_err, NULL}
  384. };
  385. static int parse_options (char * options,
  386. struct ext2_sb_info *sbi)
  387. {
  388. char * p;
  389. substring_t args[MAX_OPT_ARGS];
  390. int option;
  391. if (!options)
  392. return 1;
  393. while ((p = strsep (&options, ",")) != NULL) {
  394. int token;
  395. if (!*p)
  396. continue;
  397. token = match_token(p, tokens, args);
  398. switch (token) {
  399. case Opt_bsd_df:
  400. clear_opt (sbi->s_mount_opt, MINIX_DF);
  401. break;
  402. case Opt_minix_df:
  403. set_opt (sbi->s_mount_opt, MINIX_DF);
  404. break;
  405. case Opt_grpid:
  406. set_opt (sbi->s_mount_opt, GRPID);
  407. break;
  408. case Opt_nogrpid:
  409. clear_opt (sbi->s_mount_opt, GRPID);
  410. break;
  411. case Opt_resuid:
  412. if (match_int(&args[0], &option))
  413. return 0;
  414. sbi->s_resuid = option;
  415. break;
  416. case Opt_resgid:
  417. if (match_int(&args[0], &option))
  418. return 0;
  419. sbi->s_resgid = option;
  420. break;
  421. case Opt_sb:
  422. /* handled by get_sb_block() instead of here */
  423. /* *sb_block = match_int(&args[0]); */
  424. break;
  425. case Opt_err_panic:
  426. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  427. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  428. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  429. break;
  430. case Opt_err_ro:
  431. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  432. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  433. set_opt (sbi->s_mount_opt, ERRORS_RO);
  434. break;
  435. case Opt_err_cont:
  436. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  437. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  438. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  439. break;
  440. case Opt_nouid32:
  441. set_opt (sbi->s_mount_opt, NO_UID32);
  442. break;
  443. case Opt_nocheck:
  444. clear_opt (sbi->s_mount_opt, CHECK);
  445. break;
  446. case Opt_debug:
  447. set_opt (sbi->s_mount_opt, DEBUG);
  448. break;
  449. case Opt_oldalloc:
  450. set_opt (sbi->s_mount_opt, OLDALLOC);
  451. break;
  452. case Opt_orlov:
  453. clear_opt (sbi->s_mount_opt, OLDALLOC);
  454. break;
  455. case Opt_nobh:
  456. set_opt (sbi->s_mount_opt, NOBH);
  457. break;
  458. #ifdef CONFIG_EXT2_FS_XATTR
  459. case Opt_user_xattr:
  460. set_opt (sbi->s_mount_opt, XATTR_USER);
  461. break;
  462. case Opt_nouser_xattr:
  463. clear_opt (sbi->s_mount_opt, XATTR_USER);
  464. break;
  465. #else
  466. case Opt_user_xattr:
  467. case Opt_nouser_xattr:
  468. printk("EXT2 (no)user_xattr options not supported\n");
  469. break;
  470. #endif
  471. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  472. case Opt_acl:
  473. set_opt(sbi->s_mount_opt, POSIX_ACL);
  474. break;
  475. case Opt_noacl:
  476. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  477. break;
  478. #else
  479. case Opt_acl:
  480. case Opt_noacl:
  481. printk("EXT2 (no)acl options not supported\n");
  482. break;
  483. #endif
  484. case Opt_xip:
  485. #ifdef CONFIG_EXT2_FS_XIP
  486. set_opt (sbi->s_mount_opt, XIP);
  487. #else
  488. printk("EXT2 xip option not supported\n");
  489. #endif
  490. break;
  491. #if defined(CONFIG_QUOTA)
  492. case Opt_quota:
  493. case Opt_usrquota:
  494. set_opt(sbi->s_mount_opt, USRQUOTA);
  495. break;
  496. case Opt_grpquota:
  497. set_opt(sbi->s_mount_opt, GRPQUOTA);
  498. break;
  499. #else
  500. case Opt_quota:
  501. case Opt_usrquota:
  502. case Opt_grpquota:
  503. printk(KERN_ERR
  504. "EXT2-fs: quota operations not supported.\n");
  505. break;
  506. #endif
  507. case Opt_reservation:
  508. set_opt(sbi->s_mount_opt, RESERVATION);
  509. printk("reservations ON\n");
  510. break;
  511. case Opt_noreservation:
  512. clear_opt(sbi->s_mount_opt, RESERVATION);
  513. printk("reservations OFF\n");
  514. break;
  515. case Opt_ignore:
  516. break;
  517. default:
  518. return 0;
  519. }
  520. }
  521. return 1;
  522. }
  523. static int ext2_setup_super (struct super_block * sb,
  524. struct ext2_super_block * es,
  525. int read_only)
  526. {
  527. int res = 0;
  528. struct ext2_sb_info *sbi = EXT2_SB(sb);
  529. if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
  530. printk ("EXT2-fs warning: revision level too high, "
  531. "forcing read-only mode\n");
  532. res = MS_RDONLY;
  533. }
  534. if (read_only)
  535. return res;
  536. if (!(sbi->s_mount_state & EXT2_VALID_FS))
  537. printk ("EXT2-fs warning: mounting unchecked fs, "
  538. "running e2fsck is recommended\n");
  539. else if ((sbi->s_mount_state & EXT2_ERROR_FS))
  540. printk ("EXT2-fs warning: mounting fs with errors, "
  541. "running e2fsck is recommended\n");
  542. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  543. le16_to_cpu(es->s_mnt_count) >=
  544. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  545. printk ("EXT2-fs warning: maximal mount count reached, "
  546. "running e2fsck is recommended\n");
  547. else if (le32_to_cpu(es->s_checkinterval) &&
  548. (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  549. printk ("EXT2-fs warning: checktime reached, "
  550. "running e2fsck is recommended\n");
  551. if (!le16_to_cpu(es->s_max_mnt_count))
  552. es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
  553. le16_add_cpu(&es->s_mnt_count, 1);
  554. ext2_write_super(sb);
  555. if (test_opt (sb, DEBUG))
  556. printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
  557. "bpg=%lu, ipg=%lu, mo=%04lx]\n",
  558. EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
  559. sbi->s_frag_size,
  560. sbi->s_groups_count,
  561. EXT2_BLOCKS_PER_GROUP(sb),
  562. EXT2_INODES_PER_GROUP(sb),
  563. sbi->s_mount_opt);
  564. return res;
  565. }
  566. static int ext2_check_descriptors(struct super_block *sb)
  567. {
  568. int i;
  569. struct ext2_sb_info *sbi = EXT2_SB(sb);
  570. ext2_debug ("Checking group descriptors");
  571. for (i = 0; i < sbi->s_groups_count; i++) {
  572. struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
  573. ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
  574. ext2_fsblk_t last_block;
  575. if (i == sbi->s_groups_count - 1)
  576. last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
  577. else
  578. last_block = first_block +
  579. (EXT2_BLOCKS_PER_GROUP(sb) - 1);
  580. if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
  581. le32_to_cpu(gdp->bg_block_bitmap) > last_block)
  582. {
  583. ext2_error (sb, "ext2_check_descriptors",
  584. "Block bitmap for group %d"
  585. " not in group (block %lu)!",
  586. i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
  587. return 0;
  588. }
  589. if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
  590. le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
  591. {
  592. ext2_error (sb, "ext2_check_descriptors",
  593. "Inode bitmap for group %d"
  594. " not in group (block %lu)!",
  595. i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
  596. return 0;
  597. }
  598. if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
  599. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
  600. last_block)
  601. {
  602. ext2_error (sb, "ext2_check_descriptors",
  603. "Inode table for group %d"
  604. " not in group (block %lu)!",
  605. i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
  606. return 0;
  607. }
  608. }
  609. return 1;
  610. }
  611. /*
  612. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  613. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  614. * We need to be 1 filesystem block less than the 2^32 sector limit.
  615. */
  616. static loff_t ext2_max_size(int bits)
  617. {
  618. loff_t res = EXT2_NDIR_BLOCKS;
  619. int meta_blocks;
  620. loff_t upper_limit;
  621. /* This is calculated to be the largest file size for a
  622. * dense, file such that the total number of
  623. * sectors in the file, including data and all indirect blocks,
  624. * does not exceed 2^32 -1
  625. * __u32 i_blocks representing the total number of
  626. * 512 bytes blocks of the file
  627. */
  628. upper_limit = (1LL << 32) - 1;
  629. /* total blocks in file system block size */
  630. upper_limit >>= (bits - 9);
  631. /* indirect blocks */
  632. meta_blocks = 1;
  633. /* double indirect blocks */
  634. meta_blocks += 1 + (1LL << (bits-2));
  635. /* tripple indirect blocks */
  636. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  637. upper_limit -= meta_blocks;
  638. upper_limit <<= bits;
  639. res += 1LL << (bits-2);
  640. res += 1LL << (2*(bits-2));
  641. res += 1LL << (3*(bits-2));
  642. res <<= bits;
  643. if (res > upper_limit)
  644. res = upper_limit;
  645. if (res > MAX_LFS_FILESIZE)
  646. res = MAX_LFS_FILESIZE;
  647. return res;
  648. }
  649. static unsigned long descriptor_loc(struct super_block *sb,
  650. unsigned long logic_sb_block,
  651. int nr)
  652. {
  653. struct ext2_sb_info *sbi = EXT2_SB(sb);
  654. unsigned long bg, first_meta_bg;
  655. int has_super = 0;
  656. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  657. if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
  658. nr < first_meta_bg)
  659. return (logic_sb_block + nr + 1);
  660. bg = sbi->s_desc_per_block * nr;
  661. if (ext2_bg_has_super(sb, bg))
  662. has_super = 1;
  663. return ext2_group_first_block_no(sb, bg) + has_super;
  664. }
  665. static int ext2_fill_super(struct super_block *sb, void *data, int silent)
  666. {
  667. struct buffer_head * bh;
  668. struct ext2_sb_info * sbi;
  669. struct ext2_super_block * es;
  670. struct inode *root;
  671. unsigned long block;
  672. unsigned long sb_block = get_sb_block(&data);
  673. unsigned long logic_sb_block;
  674. unsigned long offset = 0;
  675. unsigned long def_mount_opts;
  676. long ret = -EINVAL;
  677. int blocksize = BLOCK_SIZE;
  678. int db_count;
  679. int i, j;
  680. __le32 features;
  681. int err;
  682. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  683. if (!sbi)
  684. return -ENOMEM;
  685. sbi->s_blockgroup_lock =
  686. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  687. if (!sbi->s_blockgroup_lock) {
  688. kfree(sbi);
  689. return -ENOMEM;
  690. }
  691. sb->s_fs_info = sbi;
  692. sbi->s_sb_block = sb_block;
  693. /*
  694. * See what the current blocksize for the device is, and
  695. * use that as the blocksize. Otherwise (or if the blocksize
  696. * is smaller than the default) use the default.
  697. * This is important for devices that have a hardware
  698. * sectorsize that is larger than the default.
  699. */
  700. blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
  701. if (!blocksize) {
  702. printk ("EXT2-fs: unable to set blocksize\n");
  703. goto failed_sbi;
  704. }
  705. /*
  706. * If the superblock doesn't start on a hardware sector boundary,
  707. * calculate the offset.
  708. */
  709. if (blocksize != BLOCK_SIZE) {
  710. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  711. offset = (sb_block*BLOCK_SIZE) % blocksize;
  712. } else {
  713. logic_sb_block = sb_block;
  714. }
  715. if (!(bh = sb_bread(sb, logic_sb_block))) {
  716. printk ("EXT2-fs: unable to read superblock\n");
  717. goto failed_sbi;
  718. }
  719. /*
  720. * Note: s_es must be initialized as soon as possible because
  721. * some ext2 macro-instructions depend on its value
  722. */
  723. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  724. sbi->s_es = es;
  725. sb->s_magic = le16_to_cpu(es->s_magic);
  726. if (sb->s_magic != EXT2_SUPER_MAGIC)
  727. goto cantfind_ext2;
  728. /* Set defaults before we parse the mount options */
  729. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  730. if (def_mount_opts & EXT2_DEFM_DEBUG)
  731. set_opt(sbi->s_mount_opt, DEBUG);
  732. if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
  733. set_opt(sbi->s_mount_opt, GRPID);
  734. if (def_mount_opts & EXT2_DEFM_UID16)
  735. set_opt(sbi->s_mount_opt, NO_UID32);
  736. #ifdef CONFIG_EXT2_FS_XATTR
  737. if (def_mount_opts & EXT2_DEFM_XATTR_USER)
  738. set_opt(sbi->s_mount_opt, XATTR_USER);
  739. #endif
  740. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  741. if (def_mount_opts & EXT2_DEFM_ACL)
  742. set_opt(sbi->s_mount_opt, POSIX_ACL);
  743. #endif
  744. if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
  745. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  746. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
  747. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  748. else
  749. set_opt(sbi->s_mount_opt, ERRORS_RO);
  750. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  751. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  752. set_opt(sbi->s_mount_opt, RESERVATION);
  753. if (!parse_options ((char *) data, sbi))
  754. goto failed_mount;
  755. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  756. ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
  757. MS_POSIXACL : 0);
  758. ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
  759. EXT2_MOUNT_XIP if not */
  760. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
  761. (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
  762. EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  763. EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  764. printk("EXT2-fs warning: feature flags set on rev 0 fs, "
  765. "running e2fsck is recommended\n");
  766. /*
  767. * Check feature flags regardless of the revision level, since we
  768. * previously didn't change the revision level when setting the flags,
  769. * so there is a chance incompat flags are set on a rev 0 filesystem.
  770. */
  771. features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
  772. if (features) {
  773. printk("EXT2-fs: %s: couldn't mount because of "
  774. "unsupported optional features (%x).\n",
  775. sb->s_id, le32_to_cpu(features));
  776. goto failed_mount;
  777. }
  778. if (!(sb->s_flags & MS_RDONLY) &&
  779. (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
  780. printk("EXT2-fs: %s: couldn't mount RDWR because of "
  781. "unsupported optional features (%x).\n",
  782. sb->s_id, le32_to_cpu(features));
  783. goto failed_mount;
  784. }
  785. blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
  786. if (ext2_use_xip(sb) && blocksize != PAGE_SIZE) {
  787. if (!silent)
  788. printk("XIP: Unsupported blocksize\n");
  789. goto failed_mount;
  790. }
  791. /* If the blocksize doesn't match, re-read the thing.. */
  792. if (sb->s_blocksize != blocksize) {
  793. brelse(bh);
  794. if (!sb_set_blocksize(sb, blocksize)) {
  795. printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
  796. goto failed_sbi;
  797. }
  798. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  799. offset = (sb_block*BLOCK_SIZE) % blocksize;
  800. bh = sb_bread(sb, logic_sb_block);
  801. if(!bh) {
  802. printk("EXT2-fs: Couldn't read superblock on "
  803. "2nd try.\n");
  804. goto failed_sbi;
  805. }
  806. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  807. sbi->s_es = es;
  808. if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
  809. printk ("EXT2-fs: Magic mismatch, very weird !\n");
  810. goto failed_mount;
  811. }
  812. }
  813. sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
  814. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
  815. sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  816. sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  817. } else {
  818. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  819. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  820. if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
  821. !is_power_of_2(sbi->s_inode_size) ||
  822. (sbi->s_inode_size > blocksize)) {
  823. printk ("EXT2-fs: unsupported inode size: %d\n",
  824. sbi->s_inode_size);
  825. goto failed_mount;
  826. }
  827. }
  828. sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
  829. le32_to_cpu(es->s_log_frag_size);
  830. if (sbi->s_frag_size == 0)
  831. goto cantfind_ext2;
  832. sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
  833. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  834. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  835. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  836. if (EXT2_INODE_SIZE(sb) == 0)
  837. goto cantfind_ext2;
  838. sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
  839. if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
  840. goto cantfind_ext2;
  841. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  842. sbi->s_inodes_per_block;
  843. sbi->s_desc_per_block = sb->s_blocksize /
  844. sizeof (struct ext2_group_desc);
  845. sbi->s_sbh = bh;
  846. sbi->s_mount_state = le16_to_cpu(es->s_state);
  847. sbi->s_addr_per_block_bits =
  848. ilog2 (EXT2_ADDR_PER_BLOCK(sb));
  849. sbi->s_desc_per_block_bits =
  850. ilog2 (EXT2_DESC_PER_BLOCK(sb));
  851. if (sb->s_magic != EXT2_SUPER_MAGIC)
  852. goto cantfind_ext2;
  853. if (sb->s_blocksize != bh->b_size) {
  854. if (!silent)
  855. printk ("VFS: Unsupported blocksize on dev "
  856. "%s.\n", sb->s_id);
  857. goto failed_mount;
  858. }
  859. if (sb->s_blocksize != sbi->s_frag_size) {
  860. printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
  861. sbi->s_frag_size, sb->s_blocksize);
  862. goto failed_mount;
  863. }
  864. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  865. printk ("EXT2-fs: #blocks per group too big: %lu\n",
  866. sbi->s_blocks_per_group);
  867. goto failed_mount;
  868. }
  869. if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
  870. printk ("EXT2-fs: #fragments per group too big: %lu\n",
  871. sbi->s_frags_per_group);
  872. goto failed_mount;
  873. }
  874. if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  875. printk ("EXT2-fs: #inodes per group too big: %lu\n",
  876. sbi->s_inodes_per_group);
  877. goto failed_mount;
  878. }
  879. if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
  880. goto cantfind_ext2;
  881. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  882. le32_to_cpu(es->s_first_data_block) - 1)
  883. / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
  884. db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
  885. EXT2_DESC_PER_BLOCK(sb);
  886. sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
  887. if (sbi->s_group_desc == NULL) {
  888. printk ("EXT2-fs: not enough memory\n");
  889. goto failed_mount;
  890. }
  891. bgl_lock_init(sbi->s_blockgroup_lock);
  892. sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
  893. if (!sbi->s_debts) {
  894. printk ("EXT2-fs: not enough memory\n");
  895. goto failed_mount_group_desc;
  896. }
  897. for (i = 0; i < db_count; i++) {
  898. block = descriptor_loc(sb, logic_sb_block, i);
  899. sbi->s_group_desc[i] = sb_bread(sb, block);
  900. if (!sbi->s_group_desc[i]) {
  901. for (j = 0; j < i; j++)
  902. brelse (sbi->s_group_desc[j]);
  903. printk ("EXT2-fs: unable to read group descriptors\n");
  904. goto failed_mount_group_desc;
  905. }
  906. }
  907. if (!ext2_check_descriptors (sb)) {
  908. printk ("EXT2-fs: group descriptors corrupted!\n");
  909. goto failed_mount2;
  910. }
  911. sbi->s_gdb_count = db_count;
  912. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  913. spin_lock_init(&sbi->s_next_gen_lock);
  914. /* per fileystem reservation list head & lock */
  915. spin_lock_init(&sbi->s_rsv_window_lock);
  916. sbi->s_rsv_window_root = RB_ROOT;
  917. /*
  918. * Add a single, static dummy reservation to the start of the
  919. * reservation window list --- it gives us a placeholder for
  920. * append-at-start-of-list which makes the allocation logic
  921. * _much_ simpler.
  922. */
  923. sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  924. sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  925. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  926. sbi->s_rsv_window_head.rsv_goal_size = 0;
  927. ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
  928. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  929. ext2_count_free_blocks(sb));
  930. if (!err) {
  931. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  932. ext2_count_free_inodes(sb));
  933. }
  934. if (!err) {
  935. err = percpu_counter_init(&sbi->s_dirs_counter,
  936. ext2_count_dirs(sb));
  937. }
  938. if (err) {
  939. printk(KERN_ERR "EXT2-fs: insufficient memory\n");
  940. goto failed_mount3;
  941. }
  942. /*
  943. * set up enough so that it can read an inode
  944. */
  945. sb->s_op = &ext2_sops;
  946. sb->s_export_op = &ext2_export_ops;
  947. sb->s_xattr = ext2_xattr_handlers;
  948. root = ext2_iget(sb, EXT2_ROOT_INO);
  949. if (IS_ERR(root)) {
  950. ret = PTR_ERR(root);
  951. goto failed_mount3;
  952. }
  953. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  954. iput(root);
  955. printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
  956. goto failed_mount3;
  957. }
  958. sb->s_root = d_alloc_root(root);
  959. if (!sb->s_root) {
  960. iput(root);
  961. printk(KERN_ERR "EXT2-fs: get root inode failed\n");
  962. ret = -ENOMEM;
  963. goto failed_mount3;
  964. }
  965. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
  966. ext2_warning(sb, __func__,
  967. "mounting ext3 filesystem as ext2");
  968. ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  969. return 0;
  970. cantfind_ext2:
  971. if (!silent)
  972. printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
  973. sb->s_id);
  974. goto failed_mount;
  975. failed_mount3:
  976. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  977. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  978. percpu_counter_destroy(&sbi->s_dirs_counter);
  979. failed_mount2:
  980. for (i = 0; i < db_count; i++)
  981. brelse(sbi->s_group_desc[i]);
  982. failed_mount_group_desc:
  983. kfree(sbi->s_group_desc);
  984. kfree(sbi->s_debts);
  985. failed_mount:
  986. brelse(bh);
  987. failed_sbi:
  988. sb->s_fs_info = NULL;
  989. kfree(sbi->s_blockgroup_lock);
  990. kfree(sbi);
  991. return ret;
  992. }
  993. static void ext2_commit_super (struct super_block * sb,
  994. struct ext2_super_block * es)
  995. {
  996. es->s_wtime = cpu_to_le32(get_seconds());
  997. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  998. sb->s_dirt = 0;
  999. }
  1000. static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
  1001. {
  1002. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  1003. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  1004. es->s_wtime = cpu_to_le32(get_seconds());
  1005. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  1006. sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
  1007. sb->s_dirt = 0;
  1008. }
  1009. /*
  1010. * In the second extended file system, it is not necessary to
  1011. * write the super block since we use a mapping of the
  1012. * disk super block in a buffer.
  1013. *
  1014. * However, this function is still used to set the fs valid
  1015. * flags to 0. We need to set this flag to 0 since the fs
  1016. * may have been checked while mounted and e2fsck may have
  1017. * set s_state to EXT2_VALID_FS after some corrections.
  1018. */
  1019. static int ext2_sync_fs(struct super_block *sb, int wait)
  1020. {
  1021. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  1022. lock_kernel();
  1023. if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
  1024. ext2_debug("setting valid to 0\n");
  1025. es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
  1026. es->s_free_blocks_count =
  1027. cpu_to_le32(ext2_count_free_blocks(sb));
  1028. es->s_free_inodes_count =
  1029. cpu_to_le32(ext2_count_free_inodes(sb));
  1030. es->s_mtime = cpu_to_le32(get_seconds());
  1031. ext2_sync_super(sb, es);
  1032. } else {
  1033. ext2_commit_super(sb, es);
  1034. }
  1035. sb->s_dirt = 0;
  1036. unlock_kernel();
  1037. return 0;
  1038. }
  1039. void ext2_write_super(struct super_block *sb)
  1040. {
  1041. if (!(sb->s_flags & MS_RDONLY))
  1042. ext2_sync_fs(sb, 1);
  1043. else
  1044. sb->s_dirt = 0;
  1045. }
  1046. static int ext2_remount (struct super_block * sb, int * flags, char * data)
  1047. {
  1048. struct ext2_sb_info * sbi = EXT2_SB(sb);
  1049. struct ext2_super_block * es;
  1050. unsigned long old_mount_opt = sbi->s_mount_opt;
  1051. struct ext2_mount_options old_opts;
  1052. unsigned long old_sb_flags;
  1053. int err;
  1054. lock_kernel();
  1055. /* Store the old options */
  1056. old_sb_flags = sb->s_flags;
  1057. old_opts.s_mount_opt = sbi->s_mount_opt;
  1058. old_opts.s_resuid = sbi->s_resuid;
  1059. old_opts.s_resgid = sbi->s_resgid;
  1060. /*
  1061. * Allow the "check" option to be passed as a remount option.
  1062. */
  1063. if (!parse_options (data, sbi)) {
  1064. err = -EINVAL;
  1065. goto restore_opts;
  1066. }
  1067. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1068. ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1069. ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
  1070. EXT2_MOUNT_XIP if not */
  1071. if ((ext2_use_xip(sb)) && (sb->s_blocksize != PAGE_SIZE)) {
  1072. printk("XIP: Unsupported blocksize\n");
  1073. err = -EINVAL;
  1074. goto restore_opts;
  1075. }
  1076. es = sbi->s_es;
  1077. if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
  1078. (old_mount_opt & EXT2_MOUNT_XIP)) &&
  1079. invalidate_inodes(sb)) {
  1080. ext2_warning(sb, __func__, "refusing change of xip flag "
  1081. "with busy inodes while remounting");
  1082. sbi->s_mount_opt &= ~EXT2_MOUNT_XIP;
  1083. sbi->s_mount_opt |= old_mount_opt & EXT2_MOUNT_XIP;
  1084. }
  1085. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
  1086. unlock_kernel();
  1087. return 0;
  1088. }
  1089. if (*flags & MS_RDONLY) {
  1090. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
  1091. !(sbi->s_mount_state & EXT2_VALID_FS)) {
  1092. unlock_kernel();
  1093. return 0;
  1094. }
  1095. /*
  1096. * OK, we are remounting a valid rw partition rdonly, so set
  1097. * the rdonly flag and then mark the partition as valid again.
  1098. */
  1099. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1100. es->s_mtime = cpu_to_le32(get_seconds());
  1101. } else {
  1102. __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1103. ~EXT2_FEATURE_RO_COMPAT_SUPP);
  1104. if (ret) {
  1105. printk("EXT2-fs: %s: couldn't remount RDWR because of "
  1106. "unsupported optional features (%x).\n",
  1107. sb->s_id, le32_to_cpu(ret));
  1108. err = -EROFS;
  1109. goto restore_opts;
  1110. }
  1111. /*
  1112. * Mounting a RDONLY partition read-write, so reread and
  1113. * store the current valid flag. (It may have been changed
  1114. * by e2fsck since we originally mounted the partition.)
  1115. */
  1116. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1117. if (!ext2_setup_super (sb, es, 0))
  1118. sb->s_flags &= ~MS_RDONLY;
  1119. }
  1120. ext2_sync_super(sb, es);
  1121. unlock_kernel();
  1122. return 0;
  1123. restore_opts:
  1124. sbi->s_mount_opt = old_opts.s_mount_opt;
  1125. sbi->s_resuid = old_opts.s_resuid;
  1126. sbi->s_resgid = old_opts.s_resgid;
  1127. sb->s_flags = old_sb_flags;
  1128. unlock_kernel();
  1129. return err;
  1130. }
  1131. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
  1132. {
  1133. struct super_block *sb = dentry->d_sb;
  1134. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1135. struct ext2_super_block *es = sbi->s_es;
  1136. u64 fsid;
  1137. if (test_opt (sb, MINIX_DF))
  1138. sbi->s_overhead_last = 0;
  1139. else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  1140. unsigned long i, overhead = 0;
  1141. smp_rmb();
  1142. /*
  1143. * Compute the overhead (FS structures). This is constant
  1144. * for a given filesystem unless the number of block groups
  1145. * changes so we cache the previous value until it does.
  1146. */
  1147. /*
  1148. * All of the blocks before first_data_block are
  1149. * overhead
  1150. */
  1151. overhead = le32_to_cpu(es->s_first_data_block);
  1152. /*
  1153. * Add the overhead attributed to the superblock and
  1154. * block group descriptors. If the sparse superblocks
  1155. * feature is turned on, then not all groups have this.
  1156. */
  1157. for (i = 0; i < sbi->s_groups_count; i++)
  1158. overhead += ext2_bg_has_super(sb, i) +
  1159. ext2_bg_num_gdb(sb, i);
  1160. /*
  1161. * Every block group has an inode bitmap, a block
  1162. * bitmap, and an inode table.
  1163. */
  1164. overhead += (sbi->s_groups_count *
  1165. (2 + sbi->s_itb_per_group));
  1166. sbi->s_overhead_last = overhead;
  1167. smp_wmb();
  1168. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  1169. }
  1170. buf->f_type = EXT2_SUPER_MAGIC;
  1171. buf->f_bsize = sb->s_blocksize;
  1172. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  1173. buf->f_bfree = ext2_count_free_blocks(sb);
  1174. es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
  1175. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  1176. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  1177. buf->f_bavail = 0;
  1178. buf->f_files = le32_to_cpu(es->s_inodes_count);
  1179. buf->f_ffree = ext2_count_free_inodes(sb);
  1180. es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
  1181. buf->f_namelen = EXT2_NAME_LEN;
  1182. fsid = le64_to_cpup((void *)es->s_uuid) ^
  1183. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  1184. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  1185. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  1186. return 0;
  1187. }
  1188. static int ext2_get_sb(struct file_system_type *fs_type,
  1189. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1190. {
  1191. return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super, mnt);
  1192. }
  1193. #ifdef CONFIG_QUOTA
  1194. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1195. * acquiring the locks... As quota files are never truncated and quota code
  1196. * itself serializes the operations (and noone else should touch the files)
  1197. * we don't have to be afraid of races */
  1198. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
  1199. size_t len, loff_t off)
  1200. {
  1201. struct inode *inode = sb_dqopt(sb)->files[type];
  1202. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1203. int err = 0;
  1204. int offset = off & (sb->s_blocksize - 1);
  1205. int tocopy;
  1206. size_t toread;
  1207. struct buffer_head tmp_bh;
  1208. struct buffer_head *bh;
  1209. loff_t i_size = i_size_read(inode);
  1210. if (off > i_size)
  1211. return 0;
  1212. if (off+len > i_size)
  1213. len = i_size-off;
  1214. toread = len;
  1215. while (toread > 0) {
  1216. tocopy = sb->s_blocksize - offset < toread ?
  1217. sb->s_blocksize - offset : toread;
  1218. tmp_bh.b_state = 0;
  1219. tmp_bh.b_size = sb->s_blocksize;
  1220. err = ext2_get_block(inode, blk, &tmp_bh, 0);
  1221. if (err < 0)
  1222. return err;
  1223. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1224. memset(data, 0, tocopy);
  1225. else {
  1226. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1227. if (!bh)
  1228. return -EIO;
  1229. memcpy(data, bh->b_data+offset, tocopy);
  1230. brelse(bh);
  1231. }
  1232. offset = 0;
  1233. toread -= tocopy;
  1234. data += tocopy;
  1235. blk++;
  1236. }
  1237. return len;
  1238. }
  1239. /* Write to quotafile */
  1240. static ssize_t ext2_quota_write(struct super_block *sb, int type,
  1241. const char *data, size_t len, loff_t off)
  1242. {
  1243. struct inode *inode = sb_dqopt(sb)->files[type];
  1244. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1245. int err = 0;
  1246. int offset = off & (sb->s_blocksize - 1);
  1247. int tocopy;
  1248. size_t towrite = len;
  1249. struct buffer_head tmp_bh;
  1250. struct buffer_head *bh;
  1251. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1252. while (towrite > 0) {
  1253. tocopy = sb->s_blocksize - offset < towrite ?
  1254. sb->s_blocksize - offset : towrite;
  1255. tmp_bh.b_state = 0;
  1256. err = ext2_get_block(inode, blk, &tmp_bh, 1);
  1257. if (err < 0)
  1258. goto out;
  1259. if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
  1260. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1261. else
  1262. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1263. if (!bh) {
  1264. err = -EIO;
  1265. goto out;
  1266. }
  1267. lock_buffer(bh);
  1268. memcpy(bh->b_data+offset, data, tocopy);
  1269. flush_dcache_page(bh->b_page);
  1270. set_buffer_uptodate(bh);
  1271. mark_buffer_dirty(bh);
  1272. unlock_buffer(bh);
  1273. brelse(bh);
  1274. offset = 0;
  1275. towrite -= tocopy;
  1276. data += tocopy;
  1277. blk++;
  1278. }
  1279. out:
  1280. if (len == towrite) {
  1281. mutex_unlock(&inode->i_mutex);
  1282. return err;
  1283. }
  1284. if (inode->i_size < off+len-towrite)
  1285. i_size_write(inode, off+len-towrite);
  1286. inode->i_version++;
  1287. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1288. mark_inode_dirty(inode);
  1289. mutex_unlock(&inode->i_mutex);
  1290. return len - towrite;
  1291. }
  1292. #endif
  1293. static struct file_system_type ext2_fs_type = {
  1294. .owner = THIS_MODULE,
  1295. .name = "ext2",
  1296. .get_sb = ext2_get_sb,
  1297. .kill_sb = kill_block_super,
  1298. .fs_flags = FS_REQUIRES_DEV,
  1299. };
  1300. static int __init init_ext2_fs(void)
  1301. {
  1302. int err = init_ext2_xattr();
  1303. if (err)
  1304. return err;
  1305. err = init_inodecache();
  1306. if (err)
  1307. goto out1;
  1308. err = register_filesystem(&ext2_fs_type);
  1309. if (err)
  1310. goto out;
  1311. return 0;
  1312. out:
  1313. destroy_inodecache();
  1314. out1:
  1315. exit_ext2_xattr();
  1316. return err;
  1317. }
  1318. static void __exit exit_ext2_fs(void)
  1319. {
  1320. unregister_filesystem(&ext2_fs_type);
  1321. destroy_inodecache();
  1322. exit_ext2_xattr();
  1323. }
  1324. module_init(init_ext2_fs)
  1325. module_exit(exit_ext2_fs)