super.c 42 KB

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