super.c 41 KB

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