super.c 41 KB

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