PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ext2/super.c

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