super.c 41 KB

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