super.c 39 KB

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