super.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /*
  2. * linux/fs/ext2/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/fs.h>
  21. #include <linux/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/parser.h>
  25. #include <linux/random.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/exportfs.h>
  28. #include <linux/vfs.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/mount.h>
  31. #include <linux/log2.h>
  32. #include <linux/quotaops.h>
  33. #include <asm/uaccess.h>
  34. #include "ext2.h"
  35. #include "xattr.h"
  36. #include "acl.h"
  37. #include "xip.h"
  38. static void ext2_sync_super(struct super_block *sb,
  39. struct ext2_super_block *es, int wait);
  40. static int ext2_remount (struct super_block * sb, int * flags, char * data);
  41. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
  42. static int ext2_sync_fs(struct super_block *sb, int wait);
  43. void ext2_error (struct super_block * sb, const char * function,
  44. const char * fmt, ...)
  45. {
  46. va_list args;
  47. struct ext2_sb_info *sbi = EXT2_SB(sb);
  48. struct ext2_super_block *es = sbi->s_es;
  49. if (!(sb->s_flags & MS_RDONLY)) {
  50. spin_lock(&sbi->s_lock);
  51. sbi->s_mount_state |= EXT2_ERROR_FS;
  52. es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
  53. spin_unlock(&sbi->s_lock);
  54. ext2_sync_super(sb, es, 1);
  55. }
  56. va_start(args, fmt);
  57. printk(KERN_CRIT "EXT2-fs (%s): error: %s: ", sb->s_id, function);
  58. vprintk(fmt, args);
  59. printk("\n");
  60. va_end(args);
  61. if (test_opt(sb, ERRORS_PANIC))
  62. panic("EXT2-fs: panic from previous error\n");
  63. if (test_opt(sb, ERRORS_RO)) {
  64. ext2_msg(sb, KERN_CRIT,
  65. "error: remounting filesystem read-only");
  66. sb->s_flags |= MS_RDONLY;
  67. }
  68. }
  69. void ext2_msg(struct super_block *sb, const char *prefix,
  70. const char *fmt, ...)
  71. {
  72. va_list args;
  73. va_start(args, fmt);
  74. printk("%sEXT2-fs (%s): ", prefix, sb->s_id);
  75. vprintk(fmt, args);
  76. printk("\n");
  77. va_end(args);
  78. }
  79. /*
  80. * This must be called with sbi->s_lock held.
  81. */
  82. void ext2_update_dynamic_rev(struct super_block *sb)
  83. {
  84. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  85. if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
  86. return;
  87. ext2_msg(sb, KERN_WARNING,
  88. "warning: updating to rev %d because of "
  89. "new feature flag, running e2fsck is recommended",
  90. EXT2_DYNAMIC_REV);
  91. es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
  92. es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
  93. es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
  94. /* leave es->s_feature_*compat flags alone */
  95. /* es->s_uuid will be set by e2fsck if empty */
  96. /*
  97. * The rest of the superblock fields should be zero, and if not it
  98. * means they are likely already in use, so leave them alone. We
  99. * can leave it up to e2fsck to clean up any inconsistencies there.
  100. */
  101. }
  102. static void ext2_put_super (struct super_block * sb)
  103. {
  104. int db_count;
  105. int i;
  106. struct ext2_sb_info *sbi = EXT2_SB(sb);
  107. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  108. if (sb->s_dirt)
  109. ext2_write_super(sb);
  110. ext2_xattr_put_super(sb);
  111. if (!(sb->s_flags & MS_RDONLY)) {
  112. struct ext2_super_block *es = sbi->s_es;
  113. spin_lock(&sbi->s_lock);
  114. es->s_state = cpu_to_le16(sbi->s_mount_state);
  115. spin_unlock(&sbi->s_lock);
  116. ext2_sync_super(sb, es, 1);
  117. }
  118. db_count = sbi->s_gdb_count;
  119. for (i = 0; i < db_count; i++)
  120. if (sbi->s_group_desc[i])
  121. brelse (sbi->s_group_desc[i]);
  122. kfree(sbi->s_group_desc);
  123. kfree(sbi->s_debts);
  124. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  125. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  126. percpu_counter_destroy(&sbi->s_dirs_counter);
  127. brelse (sbi->s_sbh);
  128. sb->s_fs_info = NULL;
  129. kfree(sbi->s_blockgroup_lock);
  130. kfree(sbi);
  131. }
  132. static struct kmem_cache * ext2_inode_cachep;
  133. static struct inode *ext2_alloc_inode(struct super_block *sb)
  134. {
  135. struct ext2_inode_info *ei;
  136. ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
  137. if (!ei)
  138. return NULL;
  139. ei->i_block_alloc_info = NULL;
  140. ei->vfs_inode.i_version = 1;
  141. return &ei->vfs_inode;
  142. }
  143. static void ext2_i_callback(struct rcu_head *head)
  144. {
  145. struct inode *inode = container_of(head, struct inode, i_rcu);
  146. INIT_LIST_HEAD(&inode->i_dentry);
  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 vfsmount *vfs)
  179. {
  180. struct super_block *sb = vfs->mnt_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. /* iget isn't really right if the inode is currently unallocated!!
  279. * ext2_read_inode currently does appropriate checks, but
  280. * it might be "neater" to call ext2_get_inode first and check
  281. * if the inode is valid.....
  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, "error: blocksize is too small");
  796. goto failed_sbi;
  797. }
  798. logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
  799. offset = (sb_block*BLOCK_SIZE) % blocksize;
  800. bh = sb_bread(sb, logic_sb_block);
  801. if(!bh) {
  802. ext2_msg(sb, KERN_ERR, "error: couldn't read"
  803. "superblock on 2nd try");
  804. goto failed_sbi;
  805. }
  806. es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
  807. sbi->s_es = es;
  808. if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
  809. ext2_msg(sb, KERN_ERR, "error: magic mismatch");
  810. goto failed_mount;
  811. }
  812. }
  813. sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
  814. if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
  815. sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  816. sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  817. } else {
  818. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  819. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  820. if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
  821. !is_power_of_2(sbi->s_inode_size) ||
  822. (sbi->s_inode_size > blocksize)) {
  823. ext2_msg(sb, KERN_ERR,
  824. "error: unsupported inode size: %d",
  825. sbi->s_inode_size);
  826. goto failed_mount;
  827. }
  828. }
  829. sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
  830. le32_to_cpu(es->s_log_frag_size);
  831. if (sbi->s_frag_size == 0)
  832. goto cantfind_ext2;
  833. sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
  834. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  835. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  836. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  837. if (EXT2_INODE_SIZE(sb) == 0)
  838. goto cantfind_ext2;
  839. sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
  840. if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
  841. goto cantfind_ext2;
  842. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  843. sbi->s_inodes_per_block;
  844. sbi->s_desc_per_block = sb->s_blocksize /
  845. sizeof (struct ext2_group_desc);
  846. sbi->s_sbh = bh;
  847. sbi->s_mount_state = le16_to_cpu(es->s_state);
  848. sbi->s_addr_per_block_bits =
  849. ilog2 (EXT2_ADDR_PER_BLOCK(sb));
  850. sbi->s_desc_per_block_bits =
  851. ilog2 (EXT2_DESC_PER_BLOCK(sb));
  852. if (sb->s_magic != EXT2_SUPER_MAGIC)
  853. goto cantfind_ext2;
  854. if (sb->s_blocksize != bh->b_size) {
  855. if (!silent)
  856. ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
  857. goto failed_mount;
  858. }
  859. if (sb->s_blocksize != sbi->s_frag_size) {
  860. ext2_msg(sb, KERN_ERR,
  861. "error: fragsize %lu != blocksize %lu"
  862. "(not supported yet)",
  863. sbi->s_frag_size, sb->s_blocksize);
  864. goto failed_mount;
  865. }
  866. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  867. ext2_msg(sb, KERN_ERR,
  868. "error: #blocks per group too big: %lu",
  869. sbi->s_blocks_per_group);
  870. goto failed_mount;
  871. }
  872. if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
  873. ext2_msg(sb, KERN_ERR,
  874. "error: #fragments per group too big: %lu",
  875. sbi->s_frags_per_group);
  876. goto failed_mount;
  877. }
  878. if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  879. ext2_msg(sb, KERN_ERR,
  880. "error: #inodes per group too big: %lu",
  881. sbi->s_inodes_per_group);
  882. goto failed_mount;
  883. }
  884. if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
  885. goto cantfind_ext2;
  886. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  887. le32_to_cpu(es->s_first_data_block) - 1)
  888. / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
  889. db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
  890. EXT2_DESC_PER_BLOCK(sb);
  891. sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
  892. if (sbi->s_group_desc == NULL) {
  893. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  894. goto failed_mount;
  895. }
  896. bgl_lock_init(sbi->s_blockgroup_lock);
  897. sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
  898. if (!sbi->s_debts) {
  899. ext2_msg(sb, KERN_ERR, "error: not enough memory");
  900. goto failed_mount_group_desc;
  901. }
  902. for (i = 0; i < db_count; i++) {
  903. block = descriptor_loc(sb, logic_sb_block, i);
  904. sbi->s_group_desc[i] = sb_bread(sb, block);
  905. if (!sbi->s_group_desc[i]) {
  906. for (j = 0; j < i; j++)
  907. brelse (sbi->s_group_desc[j]);
  908. ext2_msg(sb, KERN_ERR,
  909. "error: unable to read group descriptors");
  910. goto failed_mount_group_desc;
  911. }
  912. }
  913. if (!ext2_check_descriptors (sb)) {
  914. ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
  915. goto failed_mount2;
  916. }
  917. sbi->s_gdb_count = db_count;
  918. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  919. spin_lock_init(&sbi->s_next_gen_lock);
  920. /* per fileystem reservation list head & lock */
  921. spin_lock_init(&sbi->s_rsv_window_lock);
  922. sbi->s_rsv_window_root = RB_ROOT;
  923. /*
  924. * Add a single, static dummy reservation to the start of the
  925. * reservation window list --- it gives us a placeholder for
  926. * append-at-start-of-list which makes the allocation logic
  927. * _much_ simpler.
  928. */
  929. sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  930. sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
  931. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  932. sbi->s_rsv_window_head.rsv_goal_size = 0;
  933. ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
  934. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  935. ext2_count_free_blocks(sb));
  936. if (!err) {
  937. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  938. ext2_count_free_inodes(sb));
  939. }
  940. if (!err) {
  941. err = percpu_counter_init(&sbi->s_dirs_counter,
  942. ext2_count_dirs(sb));
  943. }
  944. if (err) {
  945. ext2_msg(sb, KERN_ERR, "error: insufficient memory");
  946. goto failed_mount3;
  947. }
  948. /*
  949. * set up enough so that it can read an inode
  950. */
  951. sb->s_op = &ext2_sops;
  952. sb->s_export_op = &ext2_export_ops;
  953. sb->s_xattr = ext2_xattr_handlers;
  954. #ifdef CONFIG_QUOTA
  955. sb->dq_op = &dquot_operations;
  956. sb->s_qcop = &dquot_quotactl_ops;
  957. #endif
  958. root = ext2_iget(sb, EXT2_ROOT_INO);
  959. if (IS_ERR(root)) {
  960. ret = PTR_ERR(root);
  961. goto failed_mount3;
  962. }
  963. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  964. iput(root);
  965. ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
  966. goto failed_mount3;
  967. }
  968. sb->s_root = d_alloc_root(root);
  969. if (!sb->s_root) {
  970. iput(root);
  971. ext2_msg(sb, KERN_ERR, "error: get root inode failed");
  972. ret = -ENOMEM;
  973. goto failed_mount3;
  974. }
  975. if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
  976. ext2_msg(sb, KERN_WARNING,
  977. "warning: mounting ext3 filesystem as ext2");
  978. if (ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY))
  979. sb->s_flags |= MS_RDONLY;
  980. ext2_write_super(sb);
  981. return 0;
  982. cantfind_ext2:
  983. if (!silent)
  984. ext2_msg(sb, KERN_ERR,
  985. "error: can't find an ext2 filesystem on dev %s.",
  986. sb->s_id);
  987. goto failed_mount;
  988. failed_mount3:
  989. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  990. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  991. percpu_counter_destroy(&sbi->s_dirs_counter);
  992. failed_mount2:
  993. for (i = 0; i < db_count; i++)
  994. brelse(sbi->s_group_desc[i]);
  995. failed_mount_group_desc:
  996. kfree(sbi->s_group_desc);
  997. kfree(sbi->s_debts);
  998. failed_mount:
  999. brelse(bh);
  1000. failed_sbi:
  1001. sb->s_fs_info = NULL;
  1002. kfree(sbi->s_blockgroup_lock);
  1003. kfree(sbi);
  1004. failed_unlock:
  1005. return ret;
  1006. }
  1007. static void ext2_clear_super_error(struct super_block *sb)
  1008. {
  1009. struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
  1010. if (buffer_write_io_error(sbh)) {
  1011. /*
  1012. * Oh, dear. A previous attempt to write the
  1013. * superblock failed. This could happen because the
  1014. * USB device was yanked out. Or it could happen to
  1015. * be a transient write error and maybe the block will
  1016. * be remapped. Nothing we can do but to retry the
  1017. * write and hope for the best.
  1018. */
  1019. ext2_msg(sb, KERN_ERR,
  1020. "previous I/O error to superblock detected\n");
  1021. clear_buffer_write_io_error(sbh);
  1022. set_buffer_uptodate(sbh);
  1023. }
  1024. }
  1025. static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
  1026. int wait)
  1027. {
  1028. ext2_clear_super_error(sb);
  1029. spin_lock(&EXT2_SB(sb)->s_lock);
  1030. es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
  1031. es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
  1032. es->s_wtime = cpu_to_le32(get_seconds());
  1033. /* unlock before we do IO */
  1034. spin_unlock(&EXT2_SB(sb)->s_lock);
  1035. mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
  1036. if (wait)
  1037. sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
  1038. sb->s_dirt = 0;
  1039. }
  1040. /*
  1041. * In the second extended file system, it is not necessary to
  1042. * write the super block since we use a mapping of the
  1043. * disk super block in a buffer.
  1044. *
  1045. * However, this function is still used to set the fs valid
  1046. * flags to 0. We need to set this flag to 0 since the fs
  1047. * may have been checked while mounted and e2fsck may have
  1048. * set s_state to EXT2_VALID_FS after some corrections.
  1049. */
  1050. static int ext2_sync_fs(struct super_block *sb, int wait)
  1051. {
  1052. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1053. struct ext2_super_block *es = EXT2_SB(sb)->s_es;
  1054. spin_lock(&sbi->s_lock);
  1055. if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
  1056. ext2_debug("setting valid to 0\n");
  1057. es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
  1058. }
  1059. spin_unlock(&sbi->s_lock);
  1060. ext2_sync_super(sb, es, wait);
  1061. return 0;
  1062. }
  1063. void ext2_write_super(struct super_block *sb)
  1064. {
  1065. if (!(sb->s_flags & MS_RDONLY))
  1066. ext2_sync_fs(sb, 1);
  1067. else
  1068. sb->s_dirt = 0;
  1069. }
  1070. static int ext2_remount (struct super_block * sb, int * flags, char * data)
  1071. {
  1072. struct ext2_sb_info * sbi = EXT2_SB(sb);
  1073. struct ext2_super_block * es;
  1074. unsigned long old_mount_opt = sbi->s_mount_opt;
  1075. struct ext2_mount_options old_opts;
  1076. unsigned long old_sb_flags;
  1077. int err;
  1078. spin_lock(&sbi->s_lock);
  1079. /* Store the old options */
  1080. old_sb_flags = sb->s_flags;
  1081. old_opts.s_mount_opt = sbi->s_mount_opt;
  1082. old_opts.s_resuid = sbi->s_resuid;
  1083. old_opts.s_resgid = sbi->s_resgid;
  1084. /*
  1085. * Allow the "check" option to be passed as a remount option.
  1086. */
  1087. if (!parse_options(data, sb)) {
  1088. err = -EINVAL;
  1089. goto restore_opts;
  1090. }
  1091. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1092. ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1093. ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
  1094. EXT2_MOUNT_XIP if not */
  1095. if ((ext2_use_xip(sb)) && (sb->s_blocksize != PAGE_SIZE)) {
  1096. ext2_msg(sb, KERN_WARNING,
  1097. "warning: unsupported blocksize for xip");
  1098. err = -EINVAL;
  1099. goto restore_opts;
  1100. }
  1101. es = sbi->s_es;
  1102. if ((sbi->s_mount_opt ^ old_mount_opt) & EXT2_MOUNT_XIP) {
  1103. ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
  1104. "xip flag with busy inodes while remounting");
  1105. sbi->s_mount_opt &= ~EXT2_MOUNT_XIP;
  1106. sbi->s_mount_opt |= old_mount_opt & EXT2_MOUNT_XIP;
  1107. }
  1108. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
  1109. spin_unlock(&sbi->s_lock);
  1110. return 0;
  1111. }
  1112. if (*flags & MS_RDONLY) {
  1113. if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
  1114. !(sbi->s_mount_state & EXT2_VALID_FS)) {
  1115. spin_unlock(&sbi->s_lock);
  1116. return 0;
  1117. }
  1118. /*
  1119. * OK, we are remounting a valid rw partition rdonly, so set
  1120. * the rdonly flag and then mark the partition as valid again.
  1121. */
  1122. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1123. es->s_mtime = cpu_to_le32(get_seconds());
  1124. spin_unlock(&sbi->s_lock);
  1125. err = dquot_suspend(sb, -1);
  1126. if (err < 0) {
  1127. spin_lock(&sbi->s_lock);
  1128. goto restore_opts;
  1129. }
  1130. ext2_sync_super(sb, es, 1);
  1131. } else {
  1132. __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1133. ~EXT2_FEATURE_RO_COMPAT_SUPP);
  1134. if (ret) {
  1135. ext2_msg(sb, KERN_WARNING,
  1136. "warning: couldn't remount RDWR because of "
  1137. "unsupported optional features (%x).",
  1138. le32_to_cpu(ret));
  1139. err = -EROFS;
  1140. goto restore_opts;
  1141. }
  1142. /*
  1143. * Mounting a RDONLY partition read-write, so reread and
  1144. * store the current valid flag. (It may have been changed
  1145. * by e2fsck since we originally mounted the partition.)
  1146. */
  1147. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1148. if (!ext2_setup_super (sb, es, 0))
  1149. sb->s_flags &= ~MS_RDONLY;
  1150. spin_unlock(&sbi->s_lock);
  1151. ext2_write_super(sb);
  1152. dquot_resume(sb, -1);
  1153. }
  1154. return 0;
  1155. restore_opts:
  1156. sbi->s_mount_opt = old_opts.s_mount_opt;
  1157. sbi->s_resuid = old_opts.s_resuid;
  1158. sbi->s_resgid = old_opts.s_resgid;
  1159. sb->s_flags = old_sb_flags;
  1160. spin_unlock(&sbi->s_lock);
  1161. return err;
  1162. }
  1163. static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
  1164. {
  1165. struct super_block *sb = dentry->d_sb;
  1166. struct ext2_sb_info *sbi = EXT2_SB(sb);
  1167. struct ext2_super_block *es = sbi->s_es;
  1168. u64 fsid;
  1169. spin_lock(&sbi->s_lock);
  1170. if (test_opt (sb, MINIX_DF))
  1171. sbi->s_overhead_last = 0;
  1172. else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  1173. unsigned long i, overhead = 0;
  1174. smp_rmb();
  1175. /*
  1176. * Compute the overhead (FS structures). This is constant
  1177. * for a given filesystem unless the number of block groups
  1178. * changes so we cache the previous value until it does.
  1179. */
  1180. /*
  1181. * All of the blocks before first_data_block are
  1182. * overhead
  1183. */
  1184. overhead = le32_to_cpu(es->s_first_data_block);
  1185. /*
  1186. * Add the overhead attributed to the superblock and
  1187. * block group descriptors. If the sparse superblocks
  1188. * feature is turned on, then not all groups have this.
  1189. */
  1190. for (i = 0; i < sbi->s_groups_count; i++)
  1191. overhead += ext2_bg_has_super(sb, i) +
  1192. ext2_bg_num_gdb(sb, i);
  1193. /*
  1194. * Every block group has an inode bitmap, a block
  1195. * bitmap, and an inode table.
  1196. */
  1197. overhead += (sbi->s_groups_count *
  1198. (2 + sbi->s_itb_per_group));
  1199. sbi->s_overhead_last = overhead;
  1200. smp_wmb();
  1201. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  1202. }
  1203. buf->f_type = EXT2_SUPER_MAGIC;
  1204. buf->f_bsize = sb->s_blocksize;
  1205. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  1206. buf->f_bfree = ext2_count_free_blocks(sb);
  1207. es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
  1208. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  1209. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  1210. buf->f_bavail = 0;
  1211. buf->f_files = le32_to_cpu(es->s_inodes_count);
  1212. buf->f_ffree = ext2_count_free_inodes(sb);
  1213. es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
  1214. buf->f_namelen = EXT2_NAME_LEN;
  1215. fsid = le64_to_cpup((void *)es->s_uuid) ^
  1216. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  1217. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  1218. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  1219. spin_unlock(&sbi->s_lock);
  1220. return 0;
  1221. }
  1222. static struct dentry *ext2_mount(struct file_system_type *fs_type,
  1223. int flags, const char *dev_name, void *data)
  1224. {
  1225. return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
  1226. }
  1227. #ifdef CONFIG_QUOTA
  1228. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  1229. * acquiring the locks... As quota files are never truncated and quota code
  1230. * itself serializes the operations (and noone else should touch the files)
  1231. * we don't have to be afraid of races */
  1232. static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
  1233. size_t len, loff_t off)
  1234. {
  1235. struct inode *inode = sb_dqopt(sb)->files[type];
  1236. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1237. int err = 0;
  1238. int offset = off & (sb->s_blocksize - 1);
  1239. int tocopy;
  1240. size_t toread;
  1241. struct buffer_head tmp_bh;
  1242. struct buffer_head *bh;
  1243. loff_t i_size = i_size_read(inode);
  1244. if (off > i_size)
  1245. return 0;
  1246. if (off+len > i_size)
  1247. len = i_size-off;
  1248. toread = len;
  1249. while (toread > 0) {
  1250. tocopy = sb->s_blocksize - offset < toread ?
  1251. sb->s_blocksize - offset : toread;
  1252. tmp_bh.b_state = 0;
  1253. tmp_bh.b_size = sb->s_blocksize;
  1254. err = ext2_get_block(inode, blk, &tmp_bh, 0);
  1255. if (err < 0)
  1256. return err;
  1257. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  1258. memset(data, 0, tocopy);
  1259. else {
  1260. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1261. if (!bh)
  1262. return -EIO;
  1263. memcpy(data, bh->b_data+offset, tocopy);
  1264. brelse(bh);
  1265. }
  1266. offset = 0;
  1267. toread -= tocopy;
  1268. data += tocopy;
  1269. blk++;
  1270. }
  1271. return len;
  1272. }
  1273. /* Write to quotafile */
  1274. static ssize_t ext2_quota_write(struct super_block *sb, int type,
  1275. const char *data, size_t len, loff_t off)
  1276. {
  1277. struct inode *inode = sb_dqopt(sb)->files[type];
  1278. sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
  1279. int err = 0;
  1280. int offset = off & (sb->s_blocksize - 1);
  1281. int tocopy;
  1282. size_t towrite = len;
  1283. struct buffer_head tmp_bh;
  1284. struct buffer_head *bh;
  1285. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  1286. while (towrite > 0) {
  1287. tocopy = sb->s_blocksize - offset < towrite ?
  1288. sb->s_blocksize - offset : towrite;
  1289. tmp_bh.b_state = 0;
  1290. err = ext2_get_block(inode, blk, &tmp_bh, 1);
  1291. if (err < 0)
  1292. goto out;
  1293. if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
  1294. bh = sb_bread(sb, tmp_bh.b_blocknr);
  1295. else
  1296. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  1297. if (!bh) {
  1298. err = -EIO;
  1299. goto out;
  1300. }
  1301. lock_buffer(bh);
  1302. memcpy(bh->b_data+offset, data, tocopy);
  1303. flush_dcache_page(bh->b_page);
  1304. set_buffer_uptodate(bh);
  1305. mark_buffer_dirty(bh);
  1306. unlock_buffer(bh);
  1307. brelse(bh);
  1308. offset = 0;
  1309. towrite -= tocopy;
  1310. data += tocopy;
  1311. blk++;
  1312. }
  1313. out:
  1314. if (len == towrite) {
  1315. mutex_unlock(&inode->i_mutex);
  1316. return err;
  1317. }
  1318. if (inode->i_size < off+len-towrite)
  1319. i_size_write(inode, off+len-towrite);
  1320. inode->i_version++;
  1321. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1322. mark_inode_dirty(inode);
  1323. mutex_unlock(&inode->i_mutex);
  1324. return len - towrite;
  1325. }
  1326. #endif
  1327. static struct file_system_type ext2_fs_type = {
  1328. .owner = THIS_MODULE,
  1329. .name = "ext2",
  1330. .mount = ext2_mount,
  1331. .kill_sb = kill_block_super,
  1332. .fs_flags = FS_REQUIRES_DEV,
  1333. };
  1334. static int __init init_ext2_fs(void)
  1335. {
  1336. int err = init_ext2_xattr();
  1337. if (err)
  1338. return err;
  1339. err = init_inodecache();
  1340. if (err)
  1341. goto out1;
  1342. err = register_filesystem(&ext2_fs_type);
  1343. if (err)
  1344. goto out;
  1345. return 0;
  1346. out:
  1347. destroy_inodecache();
  1348. out1:
  1349. exit_ext2_xattr();
  1350. return err;
  1351. }
  1352. static void __exit exit_ext2_fs(void)
  1353. {
  1354. unregister_filesystem(&ext2_fs_type);
  1355. destroy_inodecache();
  1356. exit_ext2_xattr();
  1357. }
  1358. module_init(init_ext2_fs)
  1359. module_exit(exit_ext2_fs)