super.c 35 KB

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