super.c 36 KB

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