super.c 39 KB

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