PageRenderTime 85ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/fs/ext4/super.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 5054 lines | 3929 code | 561 blank | 564 comment | 674 complexity | 1908ad9899d5e89a4757b72a72c2b11e MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * linux/fs/ext4/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/time.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/jbd2.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/parser.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/exportfs.h>
  30. #include <linux/vfs.h>
  31. #include <linux/random.h>
  32. #include <linux/mount.h>
  33. #include <linux/namei.h>
  34. #include <linux/quotaops.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/ctype.h>
  38. #include <linux/log2.h>
  39. #include <linux/crc16.h>
  40. #include <linux/cleancache.h>
  41. #include <asm/uaccess.h>
  42. #include <linux/kthread.h>
  43. #include <linux/freezer.h>
  44. #include "ext4.h"
  45. #include "ext4_jbd2.h"
  46. #include "xattr.h"
  47. #include "acl.h"
  48. #include "mballoc.h"
  49. #define CREATE_TRACE_POINTS
  50. #include <trace/events/ext4.h>
  51. static struct proc_dir_entry *ext4_proc_root;
  52. static struct kset *ext4_kset;
  53. static struct ext4_lazy_init *ext4_li_info;
  54. static struct mutex ext4_li_mtx;
  55. static struct ext4_features *ext4_feat;
  56. static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
  57. unsigned long journal_devnum);
  58. static int ext4_commit_super(struct super_block *sb, int sync);
  59. static void ext4_mark_recovery_complete(struct super_block *sb,
  60. struct ext4_super_block *es);
  61. static void ext4_clear_journal_err(struct super_block *sb,
  62. struct ext4_super_block *es);
  63. static int ext4_sync_fs(struct super_block *sb, int wait);
  64. static const char *ext4_decode_error(struct super_block *sb, int errno,
  65. char nbuf[16]);
  66. static int ext4_remount(struct super_block *sb, int *flags, char *data);
  67. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
  68. static int ext4_unfreeze(struct super_block *sb);
  69. static void ext4_write_super(struct super_block *sb);
  70. static int ext4_freeze(struct super_block *sb);
  71. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  72. const char *dev_name, void *data);
  73. static inline int ext2_feature_set_ok(struct super_block *sb);
  74. static inline int ext3_feature_set_ok(struct super_block *sb);
  75. static int ext4_feature_set_ok(struct super_block *sb, int readonly);
  76. static void ext4_destroy_lazyinit_thread(void);
  77. static void ext4_unregister_li_request(struct super_block *sb);
  78. static void ext4_clear_request_list(void);
  79. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  80. static struct file_system_type ext2_fs_type = {
  81. .owner = THIS_MODULE,
  82. .name = "ext2",
  83. .mount = ext4_mount,
  84. .kill_sb = kill_block_super,
  85. .fs_flags = FS_REQUIRES_DEV,
  86. };
  87. #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
  88. #else
  89. #define IS_EXT2_SB(sb) (0)
  90. #endif
  91. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  92. static struct file_system_type ext3_fs_type = {
  93. .owner = THIS_MODULE,
  94. .name = "ext3",
  95. .mount = ext4_mount,
  96. .kill_sb = kill_block_super,
  97. .fs_flags = FS_REQUIRES_DEV,
  98. };
  99. #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
  100. #else
  101. #define IS_EXT3_SB(sb) (0)
  102. #endif
  103. void *ext4_kvmalloc(size_t size, gfp_t flags)
  104. {
  105. void *ret;
  106. ret = kmalloc(size, flags);
  107. if (!ret)
  108. ret = __vmalloc(size, flags, PAGE_KERNEL);
  109. return ret;
  110. }
  111. void *ext4_kvzalloc(size_t size, gfp_t flags)
  112. {
  113. void *ret;
  114. ret = kzalloc(size, flags);
  115. if (!ret)
  116. ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
  117. return ret;
  118. }
  119. void ext4_kvfree(void *ptr)
  120. {
  121. if (is_vmalloc_addr(ptr))
  122. vfree(ptr);
  123. else
  124. kfree(ptr);
  125. }
  126. ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
  127. struct ext4_group_desc *bg)
  128. {
  129. return le32_to_cpu(bg->bg_block_bitmap_lo) |
  130. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  131. (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
  132. }
  133. ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
  134. struct ext4_group_desc *bg)
  135. {
  136. return le32_to_cpu(bg->bg_inode_bitmap_lo) |
  137. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  138. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
  139. }
  140. ext4_fsblk_t ext4_inode_table(struct super_block *sb,
  141. struct ext4_group_desc *bg)
  142. {
  143. return le32_to_cpu(bg->bg_inode_table_lo) |
  144. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  145. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
  146. }
  147. __u32 ext4_free_blks_count(struct super_block *sb,
  148. struct ext4_group_desc *bg)
  149. {
  150. return le16_to_cpu(bg->bg_free_blocks_count_lo) |
  151. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  152. (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
  153. }
  154. __u32 ext4_free_inodes_count(struct super_block *sb,
  155. struct ext4_group_desc *bg)
  156. {
  157. return le16_to_cpu(bg->bg_free_inodes_count_lo) |
  158. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  159. (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
  160. }
  161. __u32 ext4_used_dirs_count(struct super_block *sb,
  162. struct ext4_group_desc *bg)
  163. {
  164. return le16_to_cpu(bg->bg_used_dirs_count_lo) |
  165. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  166. (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
  167. }
  168. __u32 ext4_itable_unused_count(struct super_block *sb,
  169. struct ext4_group_desc *bg)
  170. {
  171. return le16_to_cpu(bg->bg_itable_unused_lo) |
  172. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  173. (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
  174. }
  175. void ext4_block_bitmap_set(struct super_block *sb,
  176. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  177. {
  178. bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
  179. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  180. bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
  181. }
  182. void ext4_inode_bitmap_set(struct super_block *sb,
  183. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  184. {
  185. bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
  186. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  187. bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
  188. }
  189. void ext4_inode_table_set(struct super_block *sb,
  190. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  191. {
  192. bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
  193. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  194. bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
  195. }
  196. void ext4_free_blks_set(struct super_block *sb,
  197. struct ext4_group_desc *bg, __u32 count)
  198. {
  199. bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
  200. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  201. bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
  202. }
  203. void ext4_free_inodes_set(struct super_block *sb,
  204. struct ext4_group_desc *bg, __u32 count)
  205. {
  206. bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
  207. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  208. bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
  209. }
  210. void ext4_used_dirs_set(struct super_block *sb,
  211. struct ext4_group_desc *bg, __u32 count)
  212. {
  213. bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
  214. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  215. bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
  216. }
  217. void ext4_itable_unused_set(struct super_block *sb,
  218. struct ext4_group_desc *bg, __u32 count)
  219. {
  220. bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
  221. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  222. bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
  223. }
  224. /* Just increment the non-pointer handle value */
  225. static handle_t *ext4_get_nojournal(void)
  226. {
  227. handle_t *handle = current->journal_info;
  228. unsigned long ref_cnt = (unsigned long)handle;
  229. BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
  230. ref_cnt++;
  231. handle = (handle_t *)ref_cnt;
  232. current->journal_info = handle;
  233. return handle;
  234. }
  235. /* Decrement the non-pointer handle value */
  236. static void ext4_put_nojournal(handle_t *handle)
  237. {
  238. unsigned long ref_cnt = (unsigned long)handle;
  239. BUG_ON(ref_cnt == 0);
  240. ref_cnt--;
  241. handle = (handle_t *)ref_cnt;
  242. current->journal_info = handle;
  243. }
  244. /*
  245. * Wrappers for jbd2_journal_start/end.
  246. *
  247. * The only special thing we need to do here is to make sure that all
  248. * journal_end calls result in the superblock being marked dirty, so
  249. * that sync() will call the filesystem's write_super callback if
  250. * appropriate.
  251. *
  252. * To avoid j_barrier hold in userspace when a user calls freeze(),
  253. * ext4 prevents a new handle from being started by s_frozen, which
  254. * is in an upper layer.
  255. */
  256. handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
  257. {
  258. journal_t *journal;
  259. handle_t *handle;
  260. trace_ext4_journal_start(sb, nblocks, _RET_IP_);
  261. if (sb->s_flags & MS_RDONLY)
  262. return ERR_PTR(-EROFS);
  263. journal = EXT4_SB(sb)->s_journal;
  264. handle = ext4_journal_current_handle();
  265. /*
  266. * If a handle has been started, it should be allowed to
  267. * finish, otherwise deadlock could happen between freeze
  268. * and others(e.g. truncate) due to the restart of the
  269. * journal handle if the filesystem is forzen and active
  270. * handles are not stopped.
  271. */
  272. if (!handle)
  273. vfs_check_frozen(sb, SB_FREEZE_TRANS);
  274. if (!journal)
  275. return ext4_get_nojournal();
  276. /*
  277. * Special case here: if the journal has aborted behind our
  278. * backs (eg. EIO in the commit thread), then we still need to
  279. * take the FS itself readonly cleanly.
  280. */
  281. if (is_journal_aborted(journal)) {
  282. ext4_abort(sb, "Detected aborted journal");
  283. return ERR_PTR(-EROFS);
  284. }
  285. return jbd2_journal_start(journal, nblocks);
  286. }
  287. /*
  288. * The only special thing we need to do here is to make sure that all
  289. * jbd2_journal_stop calls result in the superblock being marked dirty, so
  290. * that sync() will call the filesystem's write_super callback if
  291. * appropriate.
  292. */
  293. int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
  294. {
  295. struct super_block *sb;
  296. int err;
  297. int rc;
  298. if (!ext4_handle_valid(handle)) {
  299. ext4_put_nojournal(handle);
  300. return 0;
  301. }
  302. sb = handle->h_transaction->t_journal->j_private;
  303. err = handle->h_err;
  304. rc = jbd2_journal_stop(handle);
  305. if (!err)
  306. err = rc;
  307. if (err)
  308. __ext4_std_error(sb, where, line, err);
  309. return err;
  310. }
  311. void ext4_journal_abort_handle(const char *caller, unsigned int line,
  312. const char *err_fn, struct buffer_head *bh,
  313. handle_t *handle, int err)
  314. {
  315. char nbuf[16];
  316. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  317. BUG_ON(!ext4_handle_valid(handle));
  318. if (bh)
  319. BUFFER_TRACE(bh, "abort");
  320. if (!handle->h_err)
  321. handle->h_err = err;
  322. if (is_handle_aborted(handle))
  323. return;
  324. printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n",
  325. caller, line, errstr, err_fn);
  326. jbd2_journal_abort_handle(handle);
  327. }
  328. static void __save_error_info(struct super_block *sb, const char *func,
  329. unsigned int line)
  330. {
  331. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  332. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  333. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  334. es->s_last_error_time = cpu_to_le32(get_seconds());
  335. strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
  336. es->s_last_error_line = cpu_to_le32(line);
  337. if (!es->s_first_error_time) {
  338. es->s_first_error_time = es->s_last_error_time;
  339. strncpy(es->s_first_error_func, func,
  340. sizeof(es->s_first_error_func));
  341. es->s_first_error_line = cpu_to_le32(line);
  342. es->s_first_error_ino = es->s_last_error_ino;
  343. es->s_first_error_block = es->s_last_error_block;
  344. }
  345. /*
  346. * Start the daily error reporting function if it hasn't been
  347. * started already
  348. */
  349. if (!es->s_error_count)
  350. mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
  351. es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1);
  352. }
  353. static void save_error_info(struct super_block *sb, const char *func,
  354. unsigned int line)
  355. {
  356. __save_error_info(sb, func, line);
  357. ext4_commit_super(sb, 1);
  358. }
  359. /* Deal with the reporting of failure conditions on a filesystem such as
  360. * inconsistencies detected or read IO failures.
  361. *
  362. * On ext2, we can store the error state of the filesystem in the
  363. * superblock. That is not possible on ext4, because we may have other
  364. * write ordering constraints on the superblock which prevent us from
  365. * writing it out straight away; and given that the journal is about to
  366. * be aborted, we can't rely on the current, or future, transactions to
  367. * write out the superblock safely.
  368. *
  369. * We'll just use the jbd2_journal_abort() error code to record an error in
  370. * the journal instead. On recovery, the journal will complain about
  371. * that error until we've noted it down and cleared it.
  372. */
  373. static void ext4_handle_error(struct super_block *sb)
  374. {
  375. if (sb->s_flags & MS_RDONLY)
  376. return;
  377. if (!test_opt(sb, ERRORS_CONT)) {
  378. journal_t *journal = EXT4_SB(sb)->s_journal;
  379. EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
  380. if (journal)
  381. jbd2_journal_abort(journal, -EIO);
  382. }
  383. if (test_opt(sb, ERRORS_RO)) {
  384. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  385. sb->s_flags |= MS_RDONLY;
  386. }
  387. if (test_opt(sb, ERRORS_PANIC))
  388. panic("EXT4-fs (device %s): panic forced after error\n",
  389. sb->s_id);
  390. }
  391. void __ext4_error(struct super_block *sb, const char *function,
  392. unsigned int line, const char *fmt, ...)
  393. {
  394. struct va_format vaf;
  395. va_list args;
  396. va_start(args, fmt);
  397. vaf.fmt = fmt;
  398. vaf.va = &args;
  399. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
  400. sb->s_id, function, line, current->comm, &vaf);
  401. va_end(args);
  402. save_error_info(sb, function, line);
  403. ext4_handle_error(sb);
  404. }
  405. void ext4_error_inode(struct inode *inode, const char *function,
  406. unsigned int line, ext4_fsblk_t block,
  407. const char *fmt, ...)
  408. {
  409. va_list args;
  410. struct va_format vaf;
  411. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  412. es->s_last_error_ino = cpu_to_le32(inode->i_ino);
  413. es->s_last_error_block = cpu_to_le64(block);
  414. save_error_info(inode->i_sb, function, line);
  415. va_start(args, fmt);
  416. vaf.fmt = fmt;
  417. vaf.va = &args;
  418. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
  419. inode->i_sb->s_id, function, line, inode->i_ino);
  420. if (block)
  421. printk(KERN_CONT "block %llu: ", block);
  422. printk(KERN_CONT "comm %s: %pV\n", current->comm, &vaf);
  423. va_end(args);
  424. ext4_handle_error(inode->i_sb);
  425. }
  426. void ext4_error_file(struct file *file, const char *function,
  427. unsigned int line, ext4_fsblk_t block,
  428. const char *fmt, ...)
  429. {
  430. va_list args;
  431. struct va_format vaf;
  432. struct ext4_super_block *es;
  433. struct inode *inode = file->f_dentry->d_inode;
  434. char pathname[80], *path;
  435. es = EXT4_SB(inode->i_sb)->s_es;
  436. es->s_last_error_ino = cpu_to_le32(inode->i_ino);
  437. save_error_info(inode->i_sb, function, line);
  438. path = d_path(&(file->f_path), pathname, sizeof(pathname));
  439. if (IS_ERR(path))
  440. path = "(unknown)";
  441. printk(KERN_CRIT
  442. "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
  443. inode->i_sb->s_id, function, line, inode->i_ino);
  444. if (block)
  445. printk(KERN_CONT "block %llu: ", block);
  446. va_start(args, fmt);
  447. vaf.fmt = fmt;
  448. vaf.va = &args;
  449. printk(KERN_CONT "comm %s: path %s: %pV\n", current->comm, path, &vaf);
  450. va_end(args);
  451. ext4_handle_error(inode->i_sb);
  452. }
  453. static const char *ext4_decode_error(struct super_block *sb, int errno,
  454. char nbuf[16])
  455. {
  456. char *errstr = NULL;
  457. switch (errno) {
  458. case -EIO:
  459. errstr = "IO failure";
  460. break;
  461. case -ENOMEM:
  462. errstr = "Out of memory";
  463. break;
  464. case -EROFS:
  465. if (!sb || (EXT4_SB(sb)->s_journal &&
  466. EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
  467. errstr = "Journal has aborted";
  468. else
  469. errstr = "Readonly filesystem";
  470. break;
  471. default:
  472. /* If the caller passed in an extra buffer for unknown
  473. * errors, textualise them now. Else we just return
  474. * NULL. */
  475. if (nbuf) {
  476. /* Check for truncated error codes... */
  477. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  478. errstr = nbuf;
  479. }
  480. break;
  481. }
  482. return errstr;
  483. }
  484. /* __ext4_std_error decodes expected errors from journaling functions
  485. * automatically and invokes the appropriate error response. */
  486. void __ext4_std_error(struct super_block *sb, const char *function,
  487. unsigned int line, int errno)
  488. {
  489. char nbuf[16];
  490. const char *errstr;
  491. /* Special case: if the error is EROFS, and we're not already
  492. * inside a transaction, then there's really no point in logging
  493. * an error. */
  494. if (errno == -EROFS && journal_current_handle() == NULL &&
  495. (sb->s_flags & MS_RDONLY))
  496. return;
  497. errstr = ext4_decode_error(sb, errno, nbuf);
  498. printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
  499. sb->s_id, function, line, errstr);
  500. save_error_info(sb, function, line);
  501. ext4_handle_error(sb);
  502. }
  503. /*
  504. * ext4_abort is a much stronger failure handler than ext4_error. The
  505. * abort function may be used to deal with unrecoverable failures such
  506. * as journal IO errors or ENOMEM at a critical moment in log management.
  507. *
  508. * We unconditionally force the filesystem into an ABORT|READONLY state,
  509. * unless the error response on the fs has been set to panic in which
  510. * case we take the easy way out and panic immediately.
  511. */
  512. void __ext4_abort(struct super_block *sb, const char *function,
  513. unsigned int line, const char *fmt, ...)
  514. {
  515. va_list args;
  516. save_error_info(sb, function, line);
  517. va_start(args, fmt);
  518. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
  519. function, line);
  520. vprintk(fmt, args);
  521. printk("\n");
  522. va_end(args);
  523. if ((sb->s_flags & MS_RDONLY) == 0) {
  524. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  525. sb->s_flags |= MS_RDONLY;
  526. EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
  527. if (EXT4_SB(sb)->s_journal)
  528. jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
  529. save_error_info(sb, function, line);
  530. }
  531. if (test_opt(sb, ERRORS_PANIC))
  532. panic("EXT4-fs panic from previous error\n");
  533. }
  534. void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
  535. {
  536. struct va_format vaf;
  537. va_list args;
  538. va_start(args, fmt);
  539. vaf.fmt = fmt;
  540. vaf.va = &args;
  541. printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  542. va_end(args);
  543. }
  544. void __ext4_warning(struct super_block *sb, const char *function,
  545. unsigned int line, const char *fmt, ...)
  546. {
  547. struct va_format vaf;
  548. va_list args;
  549. va_start(args, fmt);
  550. vaf.fmt = fmt;
  551. vaf.va = &args;
  552. printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
  553. sb->s_id, function, line, &vaf);
  554. va_end(args);
  555. }
  556. void __ext4_grp_locked_error(const char *function, unsigned int line,
  557. struct super_block *sb, ext4_group_t grp,
  558. unsigned long ino, ext4_fsblk_t block,
  559. const char *fmt, ...)
  560. __releases(bitlock)
  561. __acquires(bitlock)
  562. {
  563. struct va_format vaf;
  564. va_list args;
  565. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  566. es->s_last_error_ino = cpu_to_le32(ino);
  567. es->s_last_error_block = cpu_to_le64(block);
  568. __save_error_info(sb, function, line);
  569. va_start(args, fmt);
  570. vaf.fmt = fmt;
  571. vaf.va = &args;
  572. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
  573. sb->s_id, function, line, grp);
  574. if (ino)
  575. printk(KERN_CONT "inode %lu: ", ino);
  576. if (block)
  577. printk(KERN_CONT "block %llu:", (unsigned long long) block);
  578. printk(KERN_CONT "%pV\n", &vaf);
  579. va_end(args);
  580. if (test_opt(sb, ERRORS_CONT)) {
  581. ext4_commit_super(sb, 0);
  582. return;
  583. }
  584. ext4_unlock_group(sb, grp);
  585. ext4_handle_error(sb);
  586. /*
  587. * We only get here in the ERRORS_RO case; relocking the group
  588. * may be dangerous, but nothing bad will happen since the
  589. * filesystem will have already been marked read/only and the
  590. * journal has been aborted. We return 1 as a hint to callers
  591. * who might what to use the return value from
  592. * ext4_grp_locked_error() to distinguish between the
  593. * ERRORS_CONT and ERRORS_RO case, and perhaps return more
  594. * aggressively from the ext4 function in question, with a
  595. * more appropriate error code.
  596. */
  597. ext4_lock_group(sb, grp);
  598. return;
  599. }
  600. void ext4_update_dynamic_rev(struct super_block *sb)
  601. {
  602. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  603. if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
  604. return;
  605. ext4_warning(sb,
  606. "updating to rev %d because of new feature flag, "
  607. "running e2fsck is recommended",
  608. EXT4_DYNAMIC_REV);
  609. es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
  610. es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
  611. es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
  612. /* leave es->s_feature_*compat flags alone */
  613. /* es->s_uuid will be set by e2fsck if empty */
  614. /*
  615. * The rest of the superblock fields should be zero, and if not it
  616. * means they are likely already in use, so leave them alone. We
  617. * can leave it up to e2fsck to clean up any inconsistencies there.
  618. */
  619. }
  620. /*
  621. * Open the external journal device
  622. */
  623. static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
  624. {
  625. struct block_device *bdev;
  626. char b[BDEVNAME_SIZE];
  627. bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
  628. if (IS_ERR(bdev))
  629. goto fail;
  630. return bdev;
  631. fail:
  632. ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
  633. __bdevname(dev, b), PTR_ERR(bdev));
  634. return NULL;
  635. }
  636. /*
  637. * Release the journal device
  638. */
  639. static int ext4_blkdev_put(struct block_device *bdev)
  640. {
  641. return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  642. }
  643. static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
  644. {
  645. struct block_device *bdev;
  646. int ret = -ENODEV;
  647. bdev = sbi->journal_bdev;
  648. if (bdev) {
  649. ret = ext4_blkdev_put(bdev);
  650. sbi->journal_bdev = NULL;
  651. }
  652. return ret;
  653. }
  654. static inline struct inode *orphan_list_entry(struct list_head *l)
  655. {
  656. return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
  657. }
  658. static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
  659. {
  660. struct list_head *l;
  661. ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
  662. le32_to_cpu(sbi->s_es->s_last_orphan));
  663. printk(KERN_ERR "sb_info orphan list:\n");
  664. list_for_each(l, &sbi->s_orphan) {
  665. struct inode *inode = orphan_list_entry(l);
  666. printk(KERN_ERR " "
  667. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  668. inode->i_sb->s_id, inode->i_ino, inode,
  669. inode->i_mode, inode->i_nlink,
  670. NEXT_ORPHAN(inode));
  671. }
  672. }
  673. static void ext4_put_super(struct super_block *sb)
  674. {
  675. struct ext4_sb_info *sbi = EXT4_SB(sb);
  676. struct ext4_super_block *es = sbi->s_es;
  677. int i, err;
  678. ext4_unregister_li_request(sb);
  679. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  680. flush_workqueue(sbi->dio_unwritten_wq);
  681. destroy_workqueue(sbi->dio_unwritten_wq);
  682. lock_super(sb);
  683. if (sb->s_dirt)
  684. ext4_commit_super(sb, 1);
  685. if (sbi->s_journal) {
  686. err = jbd2_journal_destroy(sbi->s_journal);
  687. sbi->s_journal = NULL;
  688. if (err < 0)
  689. ext4_abort(sb, "Couldn't clean up the journal");
  690. }
  691. del_timer(&sbi->s_err_report);
  692. ext4_release_system_zone(sb);
  693. ext4_mb_release(sb);
  694. ext4_ext_release(sb);
  695. ext4_xattr_put_super(sb);
  696. if (!(sb->s_flags & MS_RDONLY)) {
  697. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  698. es->s_state = cpu_to_le16(sbi->s_mount_state);
  699. ext4_commit_super(sb, 1);
  700. }
  701. if (sbi->s_proc) {
  702. remove_proc_entry(sb->s_id, ext4_proc_root);
  703. }
  704. kobject_del(&sbi->s_kobj);
  705. for (i = 0; i < sbi->s_gdb_count; i++)
  706. brelse(sbi->s_group_desc[i]);
  707. ext4_kvfree(sbi->s_group_desc);
  708. ext4_kvfree(sbi->s_flex_groups);
  709. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  710. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  711. percpu_counter_destroy(&sbi->s_dirs_counter);
  712. percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
  713. brelse(sbi->s_sbh);
  714. #ifdef CONFIG_QUOTA
  715. for (i = 0; i < MAXQUOTAS; i++)
  716. kfree(sbi->s_qf_names[i]);
  717. #endif
  718. /* Debugging code just in case the in-memory inode orphan list
  719. * isn't empty. The on-disk one can be non-empty if we've
  720. * detected an error and taken the fs readonly, but the
  721. * in-memory list had better be clean by this point. */
  722. if (!list_empty(&sbi->s_orphan))
  723. dump_orphan_list(sb, sbi);
  724. J_ASSERT(list_empty(&sbi->s_orphan));
  725. invalidate_bdev(sb->s_bdev);
  726. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  727. /*
  728. * Invalidate the journal device's buffers. We don't want them
  729. * floating about in memory - the physical journal device may
  730. * hotswapped, and it breaks the `ro-after' testing code.
  731. */
  732. sync_blockdev(sbi->journal_bdev);
  733. invalidate_bdev(sbi->journal_bdev);
  734. ext4_blkdev_remove(sbi);
  735. }
  736. if (sbi->s_mmp_tsk)
  737. kthread_stop(sbi->s_mmp_tsk);
  738. sb->s_fs_info = NULL;
  739. /*
  740. * Now that we are completely done shutting down the
  741. * superblock, we need to actually destroy the kobject.
  742. */
  743. unlock_super(sb);
  744. kobject_put(&sbi->s_kobj);
  745. wait_for_completion(&sbi->s_kobj_unregister);
  746. kfree(sbi->s_blockgroup_lock);
  747. kfree(sbi);
  748. }
  749. static struct kmem_cache *ext4_inode_cachep;
  750. /*
  751. * Called inside transaction, so use GFP_NOFS
  752. */
  753. static struct inode *ext4_alloc_inode(struct super_block *sb)
  754. {
  755. struct ext4_inode_info *ei;
  756. ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
  757. if (!ei)
  758. return NULL;
  759. ei->vfs_inode.i_version = 1;
  760. ei->vfs_inode.i_data.writeback_index = 0;
  761. memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
  762. INIT_LIST_HEAD(&ei->i_prealloc_list);
  763. spin_lock_init(&ei->i_prealloc_lock);
  764. ei->i_reserved_data_blocks = 0;
  765. ei->i_reserved_meta_blocks = 0;
  766. ei->i_allocated_meta_blocks = 0;
  767. ei->i_da_metadata_calc_len = 0;
  768. spin_lock_init(&(ei->i_block_reservation_lock));
  769. #ifdef CONFIG_QUOTA
  770. ei->i_reserved_quota = 0;
  771. #endif
  772. ei->jinode = NULL;
  773. INIT_LIST_HEAD(&ei->i_completed_io_list);
  774. spin_lock_init(&ei->i_completed_io_lock);
  775. ei->cur_aio_dio = NULL;
  776. ei->i_sync_tid = 0;
  777. ei->i_datasync_tid = 0;
  778. atomic_set(&ei->i_ioend_count, 0);
  779. atomic_set(&ei->i_aiodio_unwritten, 0);
  780. return &ei->vfs_inode;
  781. }
  782. static int ext4_drop_inode(struct inode *inode)
  783. {
  784. int drop = generic_drop_inode(inode);
  785. trace_ext4_drop_inode(inode, drop);
  786. return drop;
  787. }
  788. static void ext4_i_callback(struct rcu_head *head)
  789. {
  790. struct inode *inode = container_of(head, struct inode, i_rcu);
  791. INIT_LIST_HEAD(&inode->i_dentry);
  792. kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
  793. }
  794. static void ext4_destroy_inode(struct inode *inode)
  795. {
  796. if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
  797. ext4_msg(inode->i_sb, KERN_ERR,
  798. "Inode %lu (%p): orphan list check failed!",
  799. inode->i_ino, EXT4_I(inode));
  800. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
  801. EXT4_I(inode), sizeof(struct ext4_inode_info),
  802. true);
  803. dump_stack();
  804. }
  805. call_rcu(&inode->i_rcu, ext4_i_callback);
  806. }
  807. static void init_once(void *foo)
  808. {
  809. struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
  810. INIT_LIST_HEAD(&ei->i_orphan);
  811. #ifdef CONFIG_EXT4_FS_XATTR
  812. init_rwsem(&ei->xattr_sem);
  813. #endif
  814. init_rwsem(&ei->i_data_sem);
  815. inode_init_once(&ei->vfs_inode);
  816. }
  817. static int init_inodecache(void)
  818. {
  819. ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
  820. sizeof(struct ext4_inode_info),
  821. 0, (SLAB_RECLAIM_ACCOUNT|
  822. SLAB_MEM_SPREAD),
  823. init_once);
  824. if (ext4_inode_cachep == NULL)
  825. return -ENOMEM;
  826. return 0;
  827. }
  828. static void destroy_inodecache(void)
  829. {
  830. kmem_cache_destroy(ext4_inode_cachep);
  831. }
  832. void ext4_clear_inode(struct inode *inode)
  833. {
  834. invalidate_inode_buffers(inode);
  835. end_writeback(inode);
  836. dquot_drop(inode);
  837. ext4_discard_preallocations(inode);
  838. if (EXT4_I(inode)->jinode) {
  839. jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
  840. EXT4_I(inode)->jinode);
  841. jbd2_free_inode(EXT4_I(inode)->jinode);
  842. EXT4_I(inode)->jinode = NULL;
  843. }
  844. }
  845. static inline void ext4_show_quota_options(struct seq_file *seq,
  846. struct super_block *sb)
  847. {
  848. #if defined(CONFIG_QUOTA)
  849. struct ext4_sb_info *sbi = EXT4_SB(sb);
  850. if (sbi->s_jquota_fmt) {
  851. char *fmtname = "";
  852. switch (sbi->s_jquota_fmt) {
  853. case QFMT_VFS_OLD:
  854. fmtname = "vfsold";
  855. break;
  856. case QFMT_VFS_V0:
  857. fmtname = "vfsv0";
  858. break;
  859. case QFMT_VFS_V1:
  860. fmtname = "vfsv1";
  861. break;
  862. }
  863. seq_printf(seq, ",jqfmt=%s", fmtname);
  864. }
  865. if (sbi->s_qf_names[USRQUOTA])
  866. seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
  867. if (sbi->s_qf_names[GRPQUOTA])
  868. seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
  869. if (test_opt(sb, USRQUOTA))
  870. seq_puts(seq, ",usrquota");
  871. if (test_opt(sb, GRPQUOTA))
  872. seq_puts(seq, ",grpquota");
  873. #endif
  874. }
  875. /*
  876. * Show an option if
  877. * - it's set to a non-default value OR
  878. * - if the per-sb default is different from the global default
  879. */
  880. static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
  881. {
  882. int def_errors;
  883. unsigned long def_mount_opts;
  884. struct super_block *sb = vfs->mnt_sb;
  885. struct ext4_sb_info *sbi = EXT4_SB(sb);
  886. struct ext4_super_block *es = sbi->s_es;
  887. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  888. def_errors = le16_to_cpu(es->s_errors);
  889. if (sbi->s_sb_block != 1)
  890. seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
  891. if (test_opt(sb, MINIX_DF))
  892. seq_puts(seq, ",minixdf");
  893. if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
  894. seq_puts(seq, ",grpid");
  895. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
  896. seq_puts(seq, ",nogrpid");
  897. if (sbi->s_resuid != EXT4_DEF_RESUID ||
  898. le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
  899. seq_printf(seq, ",resuid=%u", sbi->s_resuid);
  900. }
  901. if (sbi->s_resgid != EXT4_DEF_RESGID ||
  902. le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
  903. seq_printf(seq, ",resgid=%u", sbi->s_resgid);
  904. }
  905. if (test_opt(sb, ERRORS_RO)) {
  906. if (def_errors == EXT4_ERRORS_PANIC ||
  907. def_errors == EXT4_ERRORS_CONTINUE) {
  908. seq_puts(seq, ",errors=remount-ro");
  909. }
  910. }
  911. if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
  912. seq_puts(seq, ",errors=continue");
  913. if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
  914. seq_puts(seq, ",errors=panic");
  915. if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
  916. seq_puts(seq, ",nouid32");
  917. if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
  918. seq_puts(seq, ",debug");
  919. if (test_opt(sb, OLDALLOC))
  920. seq_puts(seq, ",oldalloc");
  921. #ifdef CONFIG_EXT4_FS_XATTR
  922. if (test_opt(sb, XATTR_USER))
  923. seq_puts(seq, ",user_xattr");
  924. if (!test_opt(sb, XATTR_USER))
  925. seq_puts(seq, ",nouser_xattr");
  926. #endif
  927. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  928. if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
  929. seq_puts(seq, ",acl");
  930. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
  931. seq_puts(seq, ",noacl");
  932. #endif
  933. if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
  934. seq_printf(seq, ",commit=%u",
  935. (unsigned) (sbi->s_commit_interval / HZ));
  936. }
  937. if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
  938. seq_printf(seq, ",min_batch_time=%u",
  939. (unsigned) sbi->s_min_batch_time);
  940. }
  941. if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
  942. seq_printf(seq, ",max_batch_time=%u",
  943. (unsigned) sbi->s_min_batch_time);
  944. }
  945. /*
  946. * We're changing the default of barrier mount option, so
  947. * let's always display its mount state so it's clear what its
  948. * status is.
  949. */
  950. seq_puts(seq, ",barrier=");
  951. seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
  952. if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
  953. seq_puts(seq, ",journal_async_commit");
  954. else if (test_opt(sb, JOURNAL_CHECKSUM))
  955. seq_puts(seq, ",journal_checksum");
  956. if (test_opt(sb, I_VERSION))
  957. seq_puts(seq, ",i_version");
  958. if (!test_opt(sb, DELALLOC) &&
  959. !(def_mount_opts & EXT4_DEFM_NODELALLOC))
  960. seq_puts(seq, ",nodelalloc");
  961. if (!test_opt(sb, MBLK_IO_SUBMIT))
  962. seq_puts(seq, ",nomblk_io_submit");
  963. if (sbi->s_stripe)
  964. seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
  965. /*
  966. * journal mode get enabled in different ways
  967. * So just print the value even if we didn't specify it
  968. */
  969. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  970. seq_puts(seq, ",data=journal");
  971. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  972. seq_puts(seq, ",data=ordered");
  973. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  974. seq_puts(seq, ",data=writeback");
  975. if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
  976. seq_printf(seq, ",inode_readahead_blks=%u",
  977. sbi->s_inode_readahead_blks);
  978. if (test_opt(sb, DATA_ERR_ABORT))
  979. seq_puts(seq, ",data_err=abort");
  980. if (test_opt(sb, NO_AUTO_DA_ALLOC))
  981. seq_puts(seq, ",noauto_da_alloc");
  982. if (test_opt(sb, DISCARD) && !(def_mount_opts & EXT4_DEFM_DISCARD))
  983. seq_puts(seq, ",discard");
  984. if (test_opt(sb, NOLOAD))
  985. seq_puts(seq, ",norecovery");
  986. if (test_opt(sb, DIOREAD_NOLOCK))
  987. seq_puts(seq, ",dioread_nolock");
  988. if (test_opt(sb, BLOCK_VALIDITY) &&
  989. !(def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY))
  990. seq_puts(seq, ",block_validity");
  991. if (!test_opt(sb, INIT_INODE_TABLE))
  992. seq_puts(seq, ",noinit_itable");
  993. else if (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)
  994. seq_printf(seq, ",init_itable=%u",
  995. (unsigned) sbi->s_li_wait_mult);
  996. ext4_show_quota_options(seq, sb);
  997. return 0;
  998. }
  999. static struct inode *ext4_nfs_get_inode(struct super_block *sb,
  1000. u64 ino, u32 generation)
  1001. {
  1002. struct inode *inode;
  1003. if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
  1004. return ERR_PTR(-ESTALE);
  1005. if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
  1006. return ERR_PTR(-ESTALE);
  1007. /* iget isn't really right if the inode is currently unallocated!!
  1008. *
  1009. * ext4_read_inode will return a bad_inode if the inode had been
  1010. * deleted, so we should be safe.
  1011. *
  1012. * Currently we don't know the generation for parent directory, so
  1013. * a generation of 0 means "accept any"
  1014. */
  1015. inode = ext4_iget(sb, ino);
  1016. if (IS_ERR(inode))
  1017. return ERR_CAST(inode);
  1018. if (generation && inode->i_generation != generation) {
  1019. iput(inode);
  1020. return ERR_PTR(-ESTALE);
  1021. }
  1022. return inode;
  1023. }
  1024. static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1025. int fh_len, int fh_type)
  1026. {
  1027. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1028. ext4_nfs_get_inode);
  1029. }
  1030. static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
  1031. int fh_len, int fh_type)
  1032. {
  1033. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1034. ext4_nfs_get_inode);
  1035. }
  1036. /*
  1037. * Try to release metadata pages (indirect blocks, directories) which are
  1038. * mapped via the block device. Since these pages could have journal heads
  1039. * which would prevent try_to_free_buffers() from freeing them, we must use
  1040. * jbd2 layer's try_to_free_buffers() function to release them.
  1041. */
  1042. static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
  1043. gfp_t wait)
  1044. {
  1045. journal_t *journal = EXT4_SB(sb)->s_journal;
  1046. WARN_ON(PageChecked(page));
  1047. if (!page_has_buffers(page))
  1048. return 0;
  1049. if (journal)
  1050. return jbd2_journal_try_to_free_buffers(journal, page,
  1051. wait & ~__GFP_WAIT);
  1052. return try_to_free_buffers(page);
  1053. }
  1054. #ifdef CONFIG_QUOTA
  1055. #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
  1056. #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
  1057. static int ext4_write_dquot(struct dquot *dquot);
  1058. static int ext4_acquire_dquot(struct dquot *dquot);
  1059. static int ext4_release_dquot(struct dquot *dquot);
  1060. static int ext4_mark_dquot_dirty(struct dquot *dquot);
  1061. static int ext4_write_info(struct super_block *sb, int type);
  1062. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  1063. struct path *path);
  1064. static int ext4_quota_off(struct super_block *sb, int type);
  1065. static int ext4_quota_on_mount(struct super_block *sb, int type);
  1066. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  1067. size_t len, loff_t off);
  1068. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  1069. const char *data, size_t len, loff_t off);
  1070. static const struct dquot_operations ext4_quota_operations = {
  1071. .get_reserved_space = ext4_get_reserved_space,
  1072. .write_dquot = ext4_write_dquot,
  1073. .acquire_dquot = ext4_acquire_dquot,
  1074. .release_dquot = ext4_release_dquot,
  1075. .mark_dirty = ext4_mark_dquot_dirty,
  1076. .write_info = ext4_write_info,
  1077. .alloc_dquot = dquot_alloc,
  1078. .destroy_dquot = dquot_destroy,
  1079. };
  1080. static const struct quotactl_ops ext4_qctl_operations = {
  1081. .quota_on = ext4_quota_on,
  1082. .quota_off = ext4_quota_off,
  1083. .quota_sync = dquot_quota_sync,
  1084. .get_info = dquot_get_dqinfo,
  1085. .set_info = dquot_set_dqinfo,
  1086. .get_dqblk = dquot_get_dqblk,
  1087. .set_dqblk = dquot_set_dqblk
  1088. };
  1089. #endif
  1090. static const struct super_operations ext4_sops = {
  1091. .alloc_inode = ext4_alloc_inode,
  1092. .destroy_inode = ext4_destroy_inode,
  1093. .write_inode = ext4_write_inode,
  1094. .dirty_inode = ext4_dirty_inode,
  1095. .drop_inode = ext4_drop_inode,
  1096. .evict_inode = ext4_evict_inode,
  1097. .put_super = ext4_put_super,
  1098. .sync_fs = ext4_sync_fs,
  1099. .freeze_fs = ext4_freeze,
  1100. .unfreeze_fs = ext4_unfreeze,
  1101. .statfs = ext4_statfs,
  1102. .remount_fs = ext4_remount,
  1103. .show_options = ext4_show_options,
  1104. #ifdef CONFIG_QUOTA
  1105. .quota_read = ext4_quota_read,
  1106. .quota_write = ext4_quota_write,
  1107. #endif
  1108. .bdev_try_to_free_page = bdev_try_to_free_page,
  1109. };
  1110. static const struct super_operations ext4_nojournal_sops = {
  1111. .alloc_inode = ext4_alloc_inode,
  1112. .destroy_inode = ext4_destroy_inode,
  1113. .write_inode = ext4_write_inode,
  1114. .dirty_inode = ext4_dirty_inode,
  1115. .drop_inode = ext4_drop_inode,
  1116. .evict_inode = ext4_evict_inode,
  1117. .write_super = ext4_write_super,
  1118. .put_super = ext4_put_super,
  1119. .statfs = ext4_statfs,
  1120. .remount_fs = ext4_remount,
  1121. .show_options = ext4_show_options,
  1122. #ifdef CONFIG_QUOTA
  1123. .quota_read = ext4_quota_read,
  1124. .quota_write = ext4_quota_write,
  1125. #endif
  1126. .bdev_try_to_free_page = bdev_try_to_free_page,
  1127. };
  1128. static const struct export_operations ext4_export_ops = {
  1129. .fh_to_dentry = ext4_fh_to_dentry,
  1130. .fh_to_parent = ext4_fh_to_parent,
  1131. .get_parent = ext4_get_parent,
  1132. };
  1133. enum {
  1134. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  1135. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  1136. Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
  1137. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  1138. Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
  1139. Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
  1140. Opt_journal_update, Opt_journal_dev,
  1141. Opt_journal_checksum, Opt_journal_async_commit,
  1142. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  1143. Opt_data_err_abort, Opt_data_err_ignore,
  1144. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  1145. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
  1146. Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
  1147. Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
  1148. Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
  1149. Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
  1150. Opt_inode_readahead_blks, Opt_journal_ioprio,
  1151. Opt_dioread_nolock, Opt_dioread_lock,
  1152. Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
  1153. };
  1154. static const match_table_t tokens = {
  1155. {Opt_bsd_df, "bsddf"},
  1156. {Opt_minix_df, "minixdf"},
  1157. {Opt_grpid, "grpid"},
  1158. {Opt_grpid, "bsdgroups"},
  1159. {Opt_nogrpid, "nogrpid"},
  1160. {Opt_nogrpid, "sysvgroups"},
  1161. {Opt_resgid, "resgid=%u"},
  1162. {Opt_resuid, "resuid=%u"},
  1163. {Opt_sb, "sb=%u"},
  1164. {Opt_err_cont, "errors=continue"},
  1165. {Opt_err_panic, "errors=panic"},
  1166. {Opt_err_ro, "errors=remount-ro"},
  1167. {Opt_nouid32, "nouid32"},
  1168. {Opt_debug, "debug"},
  1169. {Opt_oldalloc, "oldalloc"},
  1170. {Opt_orlov, "orlov"},
  1171. {Opt_user_xattr, "user_xattr"},
  1172. {Opt_nouser_xattr, "nouser_xattr"},
  1173. {Opt_acl, "acl"},
  1174. {Opt_noacl, "noacl"},
  1175. {Opt_noload, "noload"},
  1176. {Opt_noload, "norecovery"},
  1177. {Opt_nobh, "nobh"},
  1178. {Opt_bh, "bh"},
  1179. {Opt_commit, "commit=%u"},
  1180. {Opt_min_batch_time, "min_batch_time=%u"},
  1181. {Opt_max_batch_time, "max_batch_time=%u"},
  1182. {Opt_journal_update, "journal=update"},
  1183. {Opt_journal_dev, "journal_dev=%u"},
  1184. {Opt_journal_checksum, "journal_checksum"},
  1185. {Opt_journal_async_commit, "journal_async_commit"},
  1186. {Opt_abort, "abort"},
  1187. {Opt_data_journal, "data=journal"},
  1188. {Opt_data_ordered, "data=ordered"},
  1189. {Opt_data_writeback, "data=writeback"},
  1190. {Opt_data_err_abort, "data_err=abort"},
  1191. {Opt_data_err_ignore, "data_err=ignore"},
  1192. {Opt_offusrjquota, "usrjquota="},
  1193. {Opt_usrjquota, "usrjquota=%s"},
  1194. {Opt_offgrpjquota, "grpjquota="},
  1195. {Opt_grpjquota, "grpjquota=%s"},
  1196. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  1197. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  1198. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  1199. {Opt_grpquota, "grpquota"},
  1200. {Opt_noquota, "noquota"},
  1201. {Opt_quota, "quota"},
  1202. {Opt_usrquota, "usrquota"},
  1203. {Opt_barrier, "barrier=%u"},
  1204. {Opt_barrier, "barrier"},
  1205. {Opt_nobarrier, "nobarrier"},
  1206. {Opt_i_version, "i_version"},
  1207. {Opt_stripe, "stripe=%u"},
  1208. {Opt_resize, "resize"},
  1209. {Opt_delalloc, "delalloc"},
  1210. {Opt_nodelalloc, "nodelalloc"},
  1211. {Opt_mblk_io_submit, "mblk_io_submit"},
  1212. {Opt_nomblk_io_submit, "nomblk_io_submit"},
  1213. {Opt_block_validity, "block_validity"},
  1214. {Opt_noblock_validity, "noblock_validity"},
  1215. {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
  1216. {Opt_journal_ioprio, "journal_ioprio=%u"},
  1217. {Opt_auto_da_alloc, "auto_da_alloc=%u"},
  1218. {Opt_auto_da_alloc, "auto_da_alloc"},
  1219. {Opt_noauto_da_alloc, "noauto_da_alloc"},
  1220. {Opt_dioread_nolock, "dioread_nolock"},
  1221. {Opt_dioread_lock, "dioread_lock"},
  1222. {Opt_discard, "discard"},
  1223. {Opt_nodiscard, "nodiscard"},
  1224. {Opt_init_itable, "init_itable=%u"},
  1225. {Opt_init_itable, "init_itable"},
  1226. {Opt_noinit_itable, "noinit_itable"},
  1227. {Opt_err, NULL},
  1228. };
  1229. static ext4_fsblk_t get_sb_block(void **data)
  1230. {
  1231. ext4_fsblk_t sb_block;
  1232. char *options = (char *) *data;
  1233. if (!options || strncmp(options, "sb=", 3) != 0)
  1234. return 1; /* Default location */
  1235. options += 3;
  1236. /* TODO: use simple_strtoll with >32bit ext4 */
  1237. sb_block = simple_strtoul(options, &options, 0);
  1238. if (*options && *options != ',') {
  1239. printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
  1240. (char *) *data);
  1241. return 1;
  1242. }
  1243. if (*options == ',')
  1244. options++;
  1245. *data = (void *) options;
  1246. return sb_block;
  1247. }
  1248. #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
  1249. static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
  1250. "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
  1251. #ifdef CONFIG_QUOTA
  1252. static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
  1253. {
  1254. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1255. char *qname;
  1256. if (sb_any_quota_loaded(sb) &&
  1257. !sbi->s_qf_names[qtype]) {
  1258. ext4_msg(sb, KERN_ERR,
  1259. "Cannot change journaled "
  1260. "quota options when quota turned on");
  1261. return 0;
  1262. }
  1263. qname = match_strdup(args);
  1264. if (!qname) {
  1265. ext4_msg(sb, KERN_ERR,
  1266. "Not enough memory for storing quotafile name");
  1267. return 0;
  1268. }
  1269. if (sbi->s_qf_names[qtype] &&
  1270. strcmp(sbi->s_qf_names[qtype], qname)) {
  1271. ext4_msg(sb, KERN_ERR,
  1272. "%s quota file already specified", QTYPE2NAME(qtype));
  1273. kfree(qname);
  1274. return 0;
  1275. }
  1276. sbi->s_qf_names[qtype] = qname;
  1277. if (strchr(sbi->s_qf_names[qtype], '/')) {
  1278. ext4_msg(sb, KERN_ERR,
  1279. "quotafile must be on filesystem root");
  1280. kfree(sbi->s_qf_names[qtype]);
  1281. sbi->s_qf_names[qtype] = NULL;
  1282. return 0;
  1283. }
  1284. set_opt(sb, QUOTA);
  1285. return 1;
  1286. }
  1287. static int clear_qf_name(struct super_block *sb, int qtype)
  1288. {
  1289. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1290. if (sb_any_quota_loaded(sb) &&
  1291. sbi->s_qf_names[qtype]) {
  1292. ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  1293. " when quota turned on");
  1294. return 0;
  1295. }
  1296. /*
  1297. * The space will be released later when all options are confirmed
  1298. * to be correct
  1299. */
  1300. sbi->s_qf_names[qtype] = NULL;
  1301. return 1;
  1302. }
  1303. #endif
  1304. static int parse_options(char *options, struct super_block *sb,
  1305. unsigned long *journal_devnum,
  1306. unsigned int *journal_ioprio,
  1307. ext4_fsblk_t *n_blocks_count, int is_remount)
  1308. {
  1309. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1310. char *p;
  1311. substring_t args[MAX_OPT_ARGS];
  1312. int data_opt = 0;
  1313. int option;
  1314. #ifdef CONFIG_QUOTA
  1315. int qfmt;
  1316. #endif
  1317. if (!options)
  1318. return 1;
  1319. while ((p = strsep(&options, ",")) != NULL) {
  1320. int token;
  1321. if (!*p)
  1322. continue;
  1323. /*
  1324. * Initialize args struct so we know whether arg was
  1325. * found; some options take optional arguments.
  1326. */
  1327. args[0].to = args[0].from = NULL;
  1328. token = match_token(p, tokens, args);
  1329. switch (token) {
  1330. case Opt_bsd_df:
  1331. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1332. clear_opt(sb, MINIX_DF);
  1333. break;
  1334. case Opt_minix_df:
  1335. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1336. set_opt(sb, MINIX_DF);
  1337. break;
  1338. case Opt_grpid:
  1339. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1340. set_opt(sb, GRPID);
  1341. break;
  1342. case Opt_nogrpid:
  1343. ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
  1344. clear_opt(sb, GRPID);
  1345. break;
  1346. case Opt_resuid:
  1347. if (match_int(&args[0], &option))
  1348. return 0;
  1349. sbi->s_resuid = option;
  1350. break;
  1351. case Opt_resgid:
  1352. if (match_int(&args[0], &option))
  1353. return 0;
  1354. sbi->s_resgid = option;
  1355. break;
  1356. case Opt_sb:
  1357. /* handled by get_sb_block() instead of here */
  1358. /* *sb_block = match_int(&args[0]); */
  1359. break;
  1360. case Opt_err_panic:
  1361. clear_opt(sb, ERRORS_CONT);
  1362. clear_opt(sb, ERRORS_RO);
  1363. set_opt(sb, ERRORS_PANIC);
  1364. break;
  1365. case Opt_err_ro:
  1366. clear_opt(sb, ERRORS_CONT);
  1367. clear_opt(sb, ERRORS_PANIC);
  1368. set_opt(sb, ERRORS_RO);
  1369. break;
  1370. case Opt_err_cont:
  1371. clear_opt(sb, ERRORS_RO);
  1372. clear_opt(sb, ERRORS_PANIC);
  1373. set_opt(sb, ERRORS_CONT);
  1374. break;
  1375. case Opt_nouid32:
  1376. set_opt(sb, NO_UID32);
  1377. break;
  1378. case Opt_debug:
  1379. set_opt(sb, DEBUG);
  1380. break;
  1381. case Opt_oldalloc:
  1382. set_opt(sb, OLDALLOC);
  1383. break;
  1384. case Opt_orlov:
  1385. clear_opt(sb, OLDALLOC);
  1386. break;
  1387. #ifdef CONFIG_EXT4_FS_XATTR
  1388. case Opt_user_xattr:
  1389. set_opt(sb, XATTR_USER);
  1390. break;
  1391. case Opt_nouser_xattr:
  1392. clear_opt(sb, XATTR_USER);
  1393. break;
  1394. #else
  1395. case Opt_user_xattr:
  1396. case Opt_nouser_xattr:
  1397. ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
  1398. break;
  1399. #endif
  1400. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  1401. case Opt_acl:
  1402. set_opt(sb, POSIX_ACL);
  1403. break;
  1404. case Opt_noacl:
  1405. clear_opt(sb, POSIX_ACL);
  1406. break;
  1407. #else
  1408. case Opt_acl:
  1409. case Opt_noacl:
  1410. ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
  1411. break;
  1412. #endif
  1413. case Opt_journal_update:
  1414. /* @@@ FIXME */
  1415. /* Eventually we will want to be able to create
  1416. a journal file here. For now, only allow the
  1417. user to specify an existing inode to be the
  1418. journal file. */
  1419. if (is_remount) {
  1420. ext4_msg(sb, KERN_ERR,
  1421. "Cannot specify journal on remount");
  1422. return 0;
  1423. }
  1424. set_opt(sb, UPDATE_JOURNAL);
  1425. break;
  1426. case Opt_journal_dev:
  1427. if (is_remount) {
  1428. ext4_msg(sb, KERN_ERR,
  1429. "Cannot specify journal on remount");
  1430. return 0;
  1431. }
  1432. if (match_int(&args[0], &option))
  1433. return 0;
  1434. *journal_devnum = option;
  1435. break;
  1436. case Opt_journal_checksum:
  1437. set_opt(sb, JOURNAL_CHECKSUM);
  1438. break;
  1439. case Opt_journal_async_commit:
  1440. set_opt(sb, JOURNAL_ASYNC_COMMIT);
  1441. set_opt(sb, JOURNAL_CHECKSUM);
  1442. break;
  1443. case Opt_noload:
  1444. set_opt(sb, NOLOAD);
  1445. break;
  1446. case Opt_commit:
  1447. if (match_int(&args[0], &option))
  1448. return 0;
  1449. if (option < 0)
  1450. return 0;
  1451. if (option == 0)
  1452. option = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1453. sbi->s_commit_interval = HZ * option;
  1454. break;
  1455. case Opt_max_batch_time:
  1456. if (match_int(&args[0], &option))
  1457. return 0;
  1458. if (option < 0)
  1459. return 0;
  1460. if (option == 0)
  1461. option = EXT4_DEF_MAX_BATCH_TIME;
  1462. sbi->s_max_batch_time = option;
  1463. break;
  1464. case Opt_min_batch_time:
  1465. if (match_int(&args[0], &option))
  1466. return 0;
  1467. if (option < 0)
  1468. return 0;
  1469. sbi->s_min_batch_time = option;
  1470. break;
  1471. case Opt_data_journal:
  1472. data_opt = EXT4_MOUNT_JOURNAL_DATA;
  1473. goto datacheck;
  1474. case Opt_data_ordered:
  1475. data_opt = EXT4_MOUNT_ORDERED_DATA;
  1476. goto datacheck;
  1477. case Opt_data_writeback:
  1478. data_opt = EXT4_MOUNT_WRITEBACK_DATA;
  1479. datacheck:
  1480. if (is_remount) {
  1481. if (test_opt(sb, DATA_FLAGS) != data_opt) {
  1482. ext4_msg(sb, KERN_ERR,
  1483. "Cannot change data mode on remount");
  1484. return 0;
  1485. }
  1486. } else {
  1487. clear_opt(sb, DATA_FLAGS);
  1488. sbi->s_mount_opt |= data_opt;
  1489. }
  1490. break;
  1491. case Opt_data_err_abort:
  1492. set_opt(sb, DATA_ERR_ABORT);
  1493. break;
  1494. case Opt_data_err_ignore:
  1495. clear_opt(sb, DATA_ERR_ABORT);
  1496. break;
  1497. #ifdef CONFIG_QUOTA
  1498. case Opt_usrjquota:
  1499. if (!set_qf_name(sb, USRQUOTA, &args[0]))
  1500. return 0;
  1501. break;
  1502. case Opt_grpjquota:
  1503. if (!set_qf_name(sb, GRPQUOTA, &args[0]))
  1504. return 0;
  1505. break;
  1506. case Opt_offusrjquota:
  1507. if (!clear_qf_name(sb, USRQUOTA))
  1508. return 0;
  1509. break;
  1510. case Opt_offgrpjquota:
  1511. if (!clear_qf_name(sb, GRPQUOTA))
  1512. return 0;
  1513. break;
  1514. case Opt_jqfmt_vfsold:
  1515. qfmt = QFMT_VFS_OLD;
  1516. goto set_qf_format;
  1517. case Opt_jqfmt_vfsv0:
  1518. qfmt = QFMT_VFS_V0;
  1519. goto set_qf_format;
  1520. case Opt_jqfmt_vfsv1:
  1521. qfmt = QFMT_VFS_V1;
  1522. set_qf_format:
  1523. if (sb_any_quota_loaded(sb) &&
  1524. sbi->s_jquota_fmt != qfmt) {
  1525. ext4_msg(sb, KERN_ERR, "Cannot change "
  1526. "journaled quota options when "
  1527. "quota turned on");
  1528. return 0;
  1529. }
  1530. sbi->s_jquota_fmt = qfmt;
  1531. break;
  1532. case Opt_quota:
  1533. case Opt_usrquota:
  1534. set_opt(sb, QUOTA);
  1535. set_opt(sb, USRQUOTA);
  1536. break;
  1537. case Opt_grpquota:
  1538. set_opt(sb, QUOTA);
  1539. set_opt(sb, GRPQUOTA);
  1540. break;
  1541. case Opt_noquota:
  1542. if (sb_any_quota_loaded(sb)) {
  1543. ext4_msg(sb, KERN_ERR, "Cannot change quota "
  1544. "options when quota turned on");
  1545. return 0;
  1546. }
  1547. clear_opt(sb, QUOTA);
  1548. clear_opt(sb, USRQUOTA);
  1549. clear_opt(sb, GRPQUOTA);
  1550. break;
  1551. #else
  1552. case Opt_quota:
  1553. case Opt_usrquota:
  1554. case Opt_grpquota:
  1555. ext4_msg(sb, KERN_ERR,
  1556. "quota options not supported");
  1557. break;
  1558. case Opt_usrjquota:
  1559. case Opt_grpjquota:
  1560. case Opt_offusrjquota:
  1561. case Opt_offgrpjquota:
  1562. case Opt_jqfmt_vfsold:
  1563. case Opt_jqfmt_vfsv0:
  1564. case Opt_jqfmt_vfsv1:
  1565. ext4_msg(sb, KERN_ERR,
  1566. "journaled quota options not supported");
  1567. break;
  1568. case Opt_noquota:
  1569. break;
  1570. #endif
  1571. case Opt_abort:
  1572. sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
  1573. break;
  1574. case Opt_nobarrier:
  1575. clear_opt(sb, BARRIER);
  1576. break;
  1577. case Opt_barrier:
  1578. if (args[0].from) {
  1579. if (match_int(&args[0], &option))
  1580. return 0;
  1581. } else
  1582. option = 1; /* No argument, default to 1 */
  1583. if (option)
  1584. set_opt(sb, BARRIER);
  1585. else
  1586. clear_opt(sb, BARRIER);
  1587. break;
  1588. case Opt_ignore:
  1589. break;
  1590. case Opt_resize:
  1591. if (!is_remount) {
  1592. ext4_msg(sb, KERN_ERR,
  1593. "resize option only available "
  1594. "for remount");
  1595. return 0;
  1596. }
  1597. if (match_int(&args[0], &option) != 0)
  1598. return 0;
  1599. *n_blocks_count = option;
  1600. break;
  1601. case Opt_nobh:
  1602. ext4_msg(sb, KERN_WARNING,
  1603. "Ignoring deprecated nobh option");
  1604. break;
  1605. case Opt_bh:
  1606. ext4_msg(sb, KERN_WARNING,
  1607. "Ignoring deprecated bh option");
  1608. break;
  1609. case Opt_i_version:
  1610. set_opt(sb, I_VERSION);
  1611. sb->s_flags |= MS_I_VERSION;
  1612. break;
  1613. case Opt_nodelalloc:
  1614. clear_opt(sb, DELALLOC);
  1615. break;
  1616. case Opt_mblk_io_submit:
  1617. set_opt(sb, MBLK_IO_SUBMIT);
  1618. break;
  1619. case Opt_nomblk_io_submit:
  1620. clear_opt(sb, MBLK_IO_SUBMIT);
  1621. break;
  1622. case Opt_stripe:
  1623. if (match_int(&args[0], &option))
  1624. return 0;
  1625. if (option < 0)
  1626. return 0;
  1627. sbi->s_stripe = option;
  1628. break;
  1629. case Opt_delalloc:
  1630. set_opt(sb, DELALLOC);
  1631. break;
  1632. case Opt_block_validity:
  1633. set_opt(sb, BLOCK_VALIDITY);
  1634. break;
  1635. case Opt_noblock_validity:
  1636. clear_opt(sb, BLOCK_VALIDITY);
  1637. break;
  1638. case Opt_inode_readahead_blks:
  1639. if (match_int(&args[0], &option))
  1640. return 0;
  1641. if (option < 0 || option > (1 << 30))
  1642. return 0;
  1643. if (option && !is_power_of_2(option)) {
  1644. ext4_msg(sb, KERN_ERR,
  1645. "EXT4-fs: inode_readahead_blks"
  1646. " must be a power of 2");
  1647. return 0;
  1648. }
  1649. sbi->s_inode_readahead_blks = option;
  1650. break;
  1651. case Opt_journal_ioprio:
  1652. if (match_int(&args[0], &option))
  1653. return 0;
  1654. if (option < 0 || option > 7)
  1655. break;
  1656. *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
  1657. option);
  1658. break;
  1659. case Opt_noauto_da_alloc:
  1660. set_opt(sb, NO_AUTO_DA_ALLOC);
  1661. break;
  1662. case Opt_auto_da_alloc:
  1663. if (args[0].from) {
  1664. if (match_int(&args[0], &option))
  1665. return 0;
  1666. } else
  1667. option = 1; /* No argument, default to 1 */
  1668. if (option)
  1669. clear_opt(sb, NO_AUTO_DA_ALLOC);
  1670. else
  1671. set_opt(sb,NO_AUTO_DA_ALLOC);
  1672. break;
  1673. case Opt_discard:
  1674. set_opt(sb, DISCARD);
  1675. break;
  1676. case Opt_nodiscard:
  1677. clear_opt(sb, DISCARD);
  1678. break;
  1679. case Opt_dioread_nolock:
  1680. set_opt(sb, DIOREAD_NOLOCK);
  1681. break;
  1682. case Opt_dioread_lock:
  1683. clear_opt(sb, DIOREAD_NOLOCK);
  1684. break;
  1685. case Opt_init_itable:
  1686. set_opt(sb, INIT_INODE_TABLE);
  1687. if (args[0].from) {
  1688. if (match_int(&args[0], &option))
  1689. return 0;
  1690. } else
  1691. option = EXT4_DEF_LI_WAIT_MULT;
  1692. if (option < 0)
  1693. return 0;
  1694. sbi->s_li_wait_mult = option;
  1695. break;
  1696. case Opt_noinit_itable:
  1697. clear_opt(sb, INIT_INODE_TABLE);
  1698. break;
  1699. default:
  1700. ext4_msg(sb, KERN_ERR,
  1701. "Unrecognized mount option \"%s\" "
  1702. "or missing value", p);
  1703. return 0;
  1704. }
  1705. }
  1706. #ifdef CONFIG_QUOTA
  1707. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1708. if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  1709. clear_opt(sb, USRQUOTA);
  1710. if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  1711. clear_opt(sb, GRPQUOTA);
  1712. if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
  1713. ext4_msg(sb, KERN_ERR, "old and new quota "
  1714. "format mixing");
  1715. return 0;
  1716. }
  1717. if (!sbi->s_jquota_fmt) {
  1718. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1719. "not specified");
  1720. return 0;
  1721. }
  1722. } else {
  1723. if (sbi->s_jquota_fmt) {
  1724. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1725. "specified with no journaling "
  1726. "enabled");
  1727. return 0;
  1728. }
  1729. }
  1730. #endif
  1731. return 1;
  1732. }
  1733. static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
  1734. int read_only)
  1735. {
  1736. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1737. int res = 0;
  1738. if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
  1739. ext4_msg(sb, KERN_ERR, "revision level too high, "
  1740. "forcing read-only mode");
  1741. res = MS_RDONLY;
  1742. }
  1743. if (read_only)
  1744. return res;
  1745. if (!(sbi->s_mount_state & EXT4_VALID_FS))
  1746. ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
  1747. "running e2fsck is recommended");
  1748. else if ((sbi->s_mount_state & EXT4_ERROR_FS))
  1749. ext4_msg(sb, KERN_WARNING,
  1750. "warning: mounting fs with errors, "
  1751. "running e2fsck is recommended");
  1752. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
  1753. le16_to_cpu(es->s_mnt_count) >=
  1754. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  1755. ext4_msg(sb, KERN_WARNING,
  1756. "warning: maximal mount count reached, "
  1757. "running e2fsck is recommended");
  1758. else if (le32_to_cpu(es->s_checkinterval) &&
  1759. (le32_to_cpu(es->s_lastcheck) +
  1760. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1761. ext4_msg(sb, KERN_WARNING,
  1762. "warning: checktime reached, "
  1763. "running e2fsck is recommended");
  1764. if (!sbi->s_journal)
  1765. es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  1766. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  1767. es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
  1768. le16_add_cpu(&es->s_mnt_count, 1);
  1769. es->s_mtime = cpu_to_le32(get_seconds());
  1770. ext4_update_dynamic_rev(sb);
  1771. if (sbi->s_journal)
  1772. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  1773. ext4_commit_super(sb, 1);
  1774. if (test_opt(sb, DEBUG))
  1775. printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
  1776. "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
  1777. sb->s_blocksize,
  1778. sbi->s_groups_count,
  1779. EXT4_BLOCKS_PER_GROUP(sb),
  1780. EXT4_INODES_PER_GROUP(sb),
  1781. sbi->s_mount_opt, sbi->s_mount_opt2);
  1782. cleancache_init_fs(sb);
  1783. return res;
  1784. }
  1785. static int ext4_fill_flex_info(struct super_block *sb)
  1786. {
  1787. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1788. struct ext4_group_desc *gdp = NULL;
  1789. ext4_group_t flex_group_count;
  1790. ext4_group_t flex_group;
  1791. unsigned int groups_per_flex = 0;
  1792. size_t size;
  1793. int i;
  1794. sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
  1795. if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
  1796. sbi->s_log_groups_per_flex = 0;
  1797. return 1;
  1798. }
  1799. groups_per_flex = 1 << sbi->s_log_groups_per_flex;
  1800. /* We allocate both existing and potentially added groups */
  1801. flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
  1802. ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
  1803. EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
  1804. size = flex_group_count * sizeof(struct flex_groups);
  1805. sbi->s_flex_groups = ext4_kvzalloc(size, GFP_KERNEL);
  1806. if (sbi->s_flex_groups == NULL) {
  1807. ext4_msg(sb, KERN_ERR, "not enough memory for %u flex groups",
  1808. flex_group_count);
  1809. goto failed;
  1810. }
  1811. for (i = 0; i < sbi->s_groups_count; i++) {
  1812. gdp = ext4_get_group_desc(sb, i, NULL);
  1813. flex_group = ext4_flex_group(sbi, i);
  1814. atomic_add(ext4_free_inodes_count(sb, gdp),
  1815. &sbi->s_flex_groups[flex_group].free_inodes);
  1816. atomic_add(ext4_free_blks_count(sb, gdp),
  1817. &sbi->s_flex_groups[flex_group].free_blocks);
  1818. atomic_add(ext4_used_dirs_count(sb, gdp),
  1819. &sbi->s_flex_groups[flex_group].used_dirs);
  1820. }
  1821. return 1;
  1822. failed:
  1823. return 0;
  1824. }
  1825. __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
  1826. struct ext4_group_desc *gdp)
  1827. {
  1828. __u16 crc = 0;
  1829. if (sbi->s_es->s_feature_ro_compat &
  1830. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  1831. int offset = offsetof(struct ext4_group_desc, bg_checksum);
  1832. __le32 le_group = cpu_to_le32(block_group);
  1833. crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
  1834. crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
  1835. crc = crc16(crc, (__u8 *)gdp, offset);
  1836. offset += sizeof(gdp->bg_checksum); /* skip checksum */
  1837. /* for checksum of struct ext4_group_desc do the rest...*/
  1838. if ((sbi->s_es->s_feature_incompat &
  1839. cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
  1840. offset < le16_to_cpu(sbi->s_es->s_desc_size))
  1841. crc = crc16(crc, (__u8 *)gdp + offset,
  1842. le16_to_cpu(sbi->s_es->s_desc_size) -
  1843. offset);
  1844. }
  1845. return cpu_to_le16(crc);
  1846. }
  1847. int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
  1848. struct ext4_group_desc *gdp)
  1849. {
  1850. if ((sbi->s_es->s_feature_ro_compat &
  1851. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
  1852. (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
  1853. return 0;
  1854. return 1;
  1855. }
  1856. /* Called at mount-time, super-block is locked */
  1857. static int ext4_check_descriptors(struct super_block *sb,
  1858. ext4_group_t *first_not_zeroed)
  1859. {
  1860. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1861. ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  1862. ext4_fsblk_t last_block;
  1863. ext4_fsblk_t block_bitmap;
  1864. ext4_fsblk_t inode_bitmap;
  1865. ext4_fsblk_t inode_table;
  1866. int flexbg_flag = 0;
  1867. ext4_group_t i, grp = sbi->s_groups_count;
  1868. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  1869. flexbg_flag = 1;
  1870. ext4_debug("Checking group descriptors");
  1871. for (i = 0; i < sbi->s_groups_count; i++) {
  1872. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1873. if (i == sbi->s_groups_count - 1 || flexbg_flag)
  1874. last_block = ext4_blocks_count(sbi->s_es) - 1;
  1875. else
  1876. last_block = first_block +
  1877. (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1878. if ((grp == sbi->s_groups_count) &&
  1879. !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  1880. grp = i;
  1881. block_bitmap = ext4_block_bitmap(sb, gdp);
  1882. if (block_bitmap < first_block || block_bitmap > last_block) {
  1883. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1884. "Block bitmap for group %u not in group "
  1885. "(block %llu)!", i, block_bitmap);
  1886. return 0;
  1887. }
  1888. inode_bitmap = ext4_inode_bitmap(sb, gdp);
  1889. if (inode_bitmap < first_block || inode_bitmap > last_block) {
  1890. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1891. "Inode bitmap for group %u not in group "
  1892. "(block %llu)!", i, inode_bitmap);
  1893. return 0;
  1894. }
  1895. inode_table = ext4_inode_table(sb, gdp);
  1896. if (inode_table < first_block ||
  1897. inode_table + sbi->s_itb_per_group - 1 > last_block) {
  1898. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1899. "Inode table for group %u not in group "
  1900. "(block %llu)!", i, inode_table);
  1901. return 0;
  1902. }
  1903. ext4_lock_group(sb, i);
  1904. if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
  1905. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1906. "Checksum for group %u failed (%u!=%u)",
  1907. i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
  1908. gdp)), le16_to_cpu(gdp->bg_checksum));
  1909. if (!(sb->s_flags & MS_RDONLY)) {
  1910. ext4_unlock_group(sb, i);
  1911. return 0;
  1912. }
  1913. }
  1914. ext4_unlock_group(sb, i);
  1915. if (!flexbg_flag)
  1916. first_block += EXT4_BLOCKS_PER_GROUP(sb);
  1917. }
  1918. if (NULL != first_not_zeroed)
  1919. *first_not_zeroed = grp;
  1920. ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
  1921. sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
  1922. return 1;
  1923. }
  1924. /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1925. * the superblock) which were deleted from all directories, but held open by
  1926. * a process at the time of a crash. We walk the list and try to delete these
  1927. * inodes at recovery time (only with a read-write filesystem).
  1928. *
  1929. * In order to keep the orphan inode chain consistent during traversal (in
  1930. * case of crash during recovery), we link each inode into the superblock
  1931. * orphan list_head and handle it the same way as an inode deletion during
  1932. * normal operation (which journals the operations for us).
  1933. *
  1934. * We only do an iget() and an iput() on each inode, which is very safe if we
  1935. * accidentally point at an in-use or already deleted inode. The worst that
  1936. * can happen in this case is that we get a "bit already cleared" message from
  1937. * ext4_free_inode(). The only reason we would point at a wrong inode is if
  1938. * e2fsck was run on this filesystem, and it must have already done the orphan
  1939. * inode cleanup for us, so we can safely abort without any further action.
  1940. */
  1941. static void ext4_orphan_cleanup(struct super_block *sb,
  1942. struct ext4_super_block *es)
  1943. {
  1944. unsigned int s_flags = sb->s_flags;
  1945. int nr_orphans = 0, nr_truncates = 0;
  1946. #ifdef CONFIG_QUOTA
  1947. int i;
  1948. #endif
  1949. if (!es->s_last_orphan) {
  1950. jbd_debug(4, "no orphan inodes to clean up\n");
  1951. return;
  1952. }
  1953. if (bdev_read_only(sb->s_bdev)) {
  1954. ext4_msg(sb, KERN_ERR, "write access "
  1955. "unavailable, skipping orphan cleanup");
  1956. return;
  1957. }
  1958. /* Check if feature set would not allow a r/w mount */
  1959. if (!ext4_feature_set_ok(sb, 0)) {
  1960. ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
  1961. "unknown ROCOMPAT features");
  1962. return;
  1963. }
  1964. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  1965. if (es->s_last_orphan)
  1966. jbd_debug(1, "Errors on filesystem, "
  1967. "clearing orphan list.\n");
  1968. es->s_last_orphan = 0;
  1969. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1970. return;
  1971. }
  1972. if (s_flags & MS_RDONLY) {
  1973. ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  1974. sb->s_flags &= ~MS_RDONLY;
  1975. }
  1976. #ifdef CONFIG_QUOTA
  1977. /* Needed for iput() to work correctly and not trash data */
  1978. sb->s_flags |= MS_ACTIVE;
  1979. /* Turn on quotas so that they are updated correctly */
  1980. for (i = 0; i < MAXQUOTAS; i++) {
  1981. if (EXT4_SB(sb)->s_qf_names[i]) {
  1982. int ret = ext4_quota_on_mount(sb, i);
  1983. if (ret < 0)
  1984. ext4_msg(sb, KERN_ERR,
  1985. "Cannot turn on journaled "
  1986. "quota: error %d", ret);
  1987. }
  1988. }
  1989. #endif
  1990. while (es->s_last_orphan) {
  1991. struct inode *inode;
  1992. inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  1993. if (IS_ERR(inode)) {
  1994. es->s_last_orphan = 0;
  1995. break;
  1996. }
  1997. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1998. dquot_initialize(inode);
  1999. if (inode->i_nlink) {
  2000. ext4_msg(sb, KERN_DEBUG,
  2001. "%s: truncating inode %lu to %lld bytes",
  2002. __func__, inode->i_ino, inode->i_size);
  2003. jbd_debug(2, "truncating inode %lu to %lld bytes\n",
  2004. inode->i_ino, inode->i_size);
  2005. ext4_truncate(inode);
  2006. nr_truncates++;
  2007. } else {
  2008. ext4_msg(sb, KERN_DEBUG,
  2009. "%s: deleting unreferenced inode %lu",
  2010. __func__, inode->i_ino);
  2011. jbd_debug(2, "deleting unreferenced inode %lu\n",
  2012. inode->i_ino);
  2013. nr_orphans++;
  2014. }
  2015. iput(inode); /* The delete magic happens here! */
  2016. }
  2017. #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
  2018. if (nr_orphans)
  2019. ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  2020. PLURAL(nr_orphans));
  2021. if (nr_truncates)
  2022. ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  2023. PLURAL(nr_truncates));
  2024. #ifdef CONFIG_QUOTA
  2025. /* Turn quotas off */
  2026. for (i = 0; i < MAXQUOTAS; i++) {
  2027. if (sb_dqopt(sb)->files[i])
  2028. dquot_quota_off(sb, i);
  2029. }
  2030. #endif
  2031. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  2032. }
  2033. /*
  2034. * Maximal extent format file size.
  2035. * Resulting logical blkno at s_maxbytes must fit in our on-disk
  2036. * extent format containers, within a sector_t, and within i_blocks
  2037. * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
  2038. * so that won't be a limiting factor.
  2039. *
  2040. * However there is other limiting factor. We do store extents in the form
  2041. * of starting block and length, hence the resulting length of the extent
  2042. * covering maximum file size must fit into on-disk format containers as
  2043. * well. Given that length is always by 1 unit bigger than max unit (because
  2044. * we count 0 as well) we have to lower the s_maxbytes by one fs block.
  2045. *
  2046. * Note, this does *not* consider any metadata overhead for vfs i_blocks.
  2047. */
  2048. static loff_t ext4_max_size(int blkbits, int has_huge_files)
  2049. {
  2050. loff_t res;
  2051. loff_t upper_limit = MAX_LFS_FILESIZE;
  2052. /* small i_blocks in vfs inode? */
  2053. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  2054. /*
  2055. * CONFIG_LBDAF is not enabled implies the inode
  2056. * i_block represent total blocks in 512 bytes
  2057. * 32 == size of vfs inode i_blocks * 8
  2058. */
  2059. upper_limit = (1LL << 32) - 1;
  2060. /* total blocks in file system block size */
  2061. upper_limit >>= (blkbits - 9);
  2062. upper_limit <<= blkbits;
  2063. }
  2064. /*
  2065. * 32-bit extent-start container, ee_block. We lower the maxbytes
  2066. * by one fs block, so ee_len can cover the extent of maximum file
  2067. * size
  2068. */
  2069. res = (1LL << 32) - 1;
  2070. res <<= blkbits;
  2071. /* Sanity check against vm- & vfs- imposed limits */
  2072. if (res > upper_limit)
  2073. res = upper_limit;
  2074. return res;
  2075. }
  2076. /*
  2077. * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
  2078. * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
  2079. * We need to be 1 filesystem block less than the 2^48 sector limit.
  2080. */
  2081. static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
  2082. {
  2083. loff_t res = EXT4_NDIR_BLOCKS;
  2084. int meta_blocks;
  2085. loff_t upper_limit;
  2086. /* This is calculated to be the largest file size for a dense, block
  2087. * mapped file such that the file's total number of 512-byte sectors,
  2088. * including data and all indirect blocks, does not exceed (2^48 - 1).
  2089. *
  2090. * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
  2091. * number of 512-byte sectors of the file.
  2092. */
  2093. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  2094. /*
  2095. * !has_huge_files or CONFIG_LBDAF not enabled implies that
  2096. * the inode i_block field represents total file blocks in
  2097. * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
  2098. */
  2099. upper_limit = (1LL << 32) - 1;
  2100. /* total blocks in file system block size */
  2101. upper_limit >>= (bits - 9);
  2102. } else {
  2103. /*
  2104. * We use 48 bit ext4_inode i_blocks
  2105. * With EXT4_HUGE_FILE_FL set the i_blocks
  2106. * represent total number of blocks in
  2107. * file system block size
  2108. */
  2109. upper_limit = (1LL << 48) - 1;
  2110. }
  2111. /* indirect blocks */
  2112. meta_blocks = 1;
  2113. /* double indirect blocks */
  2114. meta_blocks += 1 + (1LL << (bits-2));
  2115. /* tripple indirect blocks */
  2116. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  2117. upper_limit -= meta_blocks;
  2118. upper_limit <<= bits;
  2119. res += 1LL << (bits-2);
  2120. res += 1LL << (2*(bits-2));
  2121. res += 1LL << (3*(bits-2));
  2122. res <<= bits;
  2123. if (res > upper_limit)
  2124. res = upper_limit;
  2125. if (res > MAX_LFS_FILESIZE)
  2126. res = MAX_LFS_FILESIZE;
  2127. return res;
  2128. }
  2129. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  2130. ext4_fsblk_t logical_sb_block, int nr)
  2131. {
  2132. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2133. ext4_group_t bg, first_meta_bg;
  2134. int has_super = 0;
  2135. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  2136. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  2137. nr < first_meta_bg)
  2138. return logical_sb_block + nr + 1;
  2139. bg = sbi->s_desc_per_block * nr;
  2140. if (ext4_bg_has_super(sb, bg))
  2141. has_super = 1;
  2142. return (has_super + ext4_group_first_block_no(sb, bg));
  2143. }
  2144. /**
  2145. * ext4_get_stripe_size: Get the stripe size.
  2146. * @sbi: In memory super block info
  2147. *
  2148. * If we have specified it via mount option, then
  2149. * use the mount option value. If the value specified at mount time is
  2150. * greater than the blocks per group use the super block value.
  2151. * If the super block value is greater than blocks per group return 0.
  2152. * Allocator needs it be less than blocks per group.
  2153. *
  2154. */
  2155. static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
  2156. {
  2157. unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
  2158. unsigned long stripe_width =
  2159. le32_to_cpu(sbi->s_es->s_raid_stripe_width);
  2160. int ret;
  2161. if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
  2162. ret = sbi->s_stripe;
  2163. else if (stripe_width <= sbi->s_blocks_per_group)
  2164. ret = stripe_width;
  2165. else if (stride <= sbi->s_blocks_per_group)
  2166. ret = stride;
  2167. else
  2168. ret = 0;
  2169. /*
  2170. * If the stripe width is 1, this makes no sense and
  2171. * we set it to 0 to turn off stripe handling code.
  2172. */
  2173. if (ret <= 1)
  2174. ret = 0;
  2175. return ret;
  2176. }
  2177. /* sysfs supprt */
  2178. struct ext4_attr {
  2179. struct attribute attr;
  2180. ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
  2181. ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
  2182. const char *, size_t);
  2183. int offset;
  2184. };
  2185. static int parse_strtoul(const char *buf,
  2186. unsigned long max, unsigned long *value)
  2187. {
  2188. char *endp;
  2189. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  2190. endp = skip_spaces(endp);
  2191. if (*endp || *value > max)
  2192. return -EINVAL;
  2193. return 0;
  2194. }
  2195. static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
  2196. struct ext4_sb_info *sbi,
  2197. char *buf)
  2198. {
  2199. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2200. (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
  2201. }
  2202. static ssize_t session_write_kbytes_show(struct ext4_attr *a,
  2203. struct ext4_sb_info *sbi, char *buf)
  2204. {
  2205. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2206. if (!sb->s_bdev->bd_part)
  2207. return snprintf(buf, PAGE_SIZE, "0\n");
  2208. return snprintf(buf, PAGE_SIZE, "%lu\n",
  2209. (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2210. sbi->s_sectors_written_start) >> 1);
  2211. }
  2212. static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
  2213. struct ext4_sb_info *sbi, char *buf)
  2214. {
  2215. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2216. if (!sb->s_bdev->bd_part)
  2217. return snprintf(buf, PAGE_SIZE, "0\n");
  2218. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2219. (unsigned long long)(sbi->s_kbytes_written +
  2220. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2221. EXT4_SB(sb)->s_sectors_written_start) >> 1)));
  2222. }
  2223. static ssize_t extent_cache_hits_show(struct ext4_attr *a,
  2224. struct ext4_sb_info *sbi, char *buf)
  2225. {
  2226. return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_hits);
  2227. }
  2228. static ssize_t extent_cache_misses_show(struct ext4_attr *a,
  2229. struct ext4_sb_info *sbi, char *buf)
  2230. {
  2231. return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_misses);
  2232. }
  2233. static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
  2234. struct ext4_sb_info *sbi,
  2235. const char *buf, size_t count)
  2236. {
  2237. unsigned long t;
  2238. if (parse_strtoul(buf, 0x40000000, &t))
  2239. return -EINVAL;
  2240. if (t && !is_power_of_2(t))
  2241. return -EINVAL;
  2242. sbi->s_inode_readahead_blks = t;
  2243. return count;
  2244. }
  2245. static ssize_t sbi_ui_show(struct ext4_attr *a,
  2246. struct ext4_sb_info *sbi, char *buf)
  2247. {
  2248. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2249. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  2250. }
  2251. static ssize_t sbi_ui_store(struct ext4_attr *a,
  2252. struct ext4_sb_info *sbi,
  2253. const char *buf, size_t count)
  2254. {
  2255. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2256. unsigned long t;
  2257. if (parse_strtoul(buf, 0xffffffff, &t))
  2258. return -EINVAL;
  2259. *ui = t;
  2260. return count;
  2261. }
  2262. #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
  2263. static struct ext4_attr ext4_attr_##_name = { \
  2264. .attr = {.name = __stringify(_name), .mode = _mode }, \
  2265. .show = _show, \
  2266. .store = _store, \
  2267. .offset = offsetof(struct ext4_sb_info, _elname), \
  2268. }
  2269. #define EXT4_ATTR(name, mode, show, store) \
  2270. static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
  2271. #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
  2272. #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
  2273. #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
  2274. #define EXT4_RW_ATTR_SBI_UI(name, elname) \
  2275. EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
  2276. #define ATTR_LIST(name) &ext4_attr_##name.attr
  2277. EXT4_RO_ATTR(delayed_allocation_blocks);
  2278. EXT4_RO_ATTR(session_write_kbytes);
  2279. EXT4_RO_ATTR(lifetime_write_kbytes);
  2280. EXT4_RO_ATTR(extent_cache_hits);
  2281. EXT4_RO_ATTR(extent_cache_misses);
  2282. EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
  2283. inode_readahead_blks_store, s_inode_readahead_blks);
  2284. EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
  2285. EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
  2286. EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
  2287. EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
  2288. EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
  2289. EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
  2290. EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
  2291. EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
  2292. static struct attribute *ext4_attrs[] = {
  2293. ATTR_LIST(delayed_allocation_blocks),
  2294. ATTR_LIST(session_write_kbytes),
  2295. ATTR_LIST(lifetime_write_kbytes),
  2296. ATTR_LIST(extent_cache_hits),
  2297. ATTR_LIST(extent_cache_misses),
  2298. ATTR_LIST(inode_readahead_blks),
  2299. ATTR_LIST(inode_goal),
  2300. ATTR_LIST(mb_stats),
  2301. ATTR_LIST(mb_max_to_scan),
  2302. ATTR_LIST(mb_min_to_scan),
  2303. ATTR_LIST(mb_order2_req),
  2304. ATTR_LIST(mb_stream_req),
  2305. ATTR_LIST(mb_group_prealloc),
  2306. ATTR_LIST(max_writeback_mb_bump),
  2307. NULL,
  2308. };
  2309. /* Features this copy of ext4 supports */
  2310. EXT4_INFO_ATTR(lazy_itable_init);
  2311. EXT4_INFO_ATTR(batched_discard);
  2312. static struct attribute *ext4_feat_attrs[] = {
  2313. ATTR_LIST(lazy_itable_init),
  2314. ATTR_LIST(batched_discard),
  2315. NULL,
  2316. };
  2317. static ssize_t ext4_attr_show(struct kobject *kobj,
  2318. struct attribute *attr, char *buf)
  2319. {
  2320. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2321. s_kobj);
  2322. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2323. return a->show ? a->show(a, sbi, buf) : 0;
  2324. }
  2325. static ssize_t ext4_attr_store(struct kobject *kobj,
  2326. struct attribute *attr,
  2327. const char *buf, size_t len)
  2328. {
  2329. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2330. s_kobj);
  2331. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2332. return a->store ? a->store(a, sbi, buf, len) : 0;
  2333. }
  2334. static void ext4_sb_release(struct kobject *kobj)
  2335. {
  2336. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2337. s_kobj);
  2338. complete(&sbi->s_kobj_unregister);
  2339. }
  2340. static const struct sysfs_ops ext4_attr_ops = {
  2341. .show = ext4_attr_show,
  2342. .store = ext4_attr_store,
  2343. };
  2344. static struct kobj_type ext4_ktype = {
  2345. .default_attrs = ext4_attrs,
  2346. .sysfs_ops = &ext4_attr_ops,
  2347. .release = ext4_sb_release,
  2348. };
  2349. static void ext4_feat_release(struct kobject *kobj)
  2350. {
  2351. complete(&ext4_feat->f_kobj_unregister);
  2352. }
  2353. static struct kobj_type ext4_feat_ktype = {
  2354. .default_attrs = ext4_feat_attrs,
  2355. .sysfs_ops = &ext4_attr_ops,
  2356. .release = ext4_feat_release,
  2357. };
  2358. /*
  2359. * Check whether this filesystem can be mounted based on
  2360. * the features present and the RDONLY/RDWR mount requested.
  2361. * Returns 1 if this filesystem can be mounted as requested,
  2362. * 0 if it cannot be.
  2363. */
  2364. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2365. {
  2366. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
  2367. ext4_msg(sb, KERN_ERR,
  2368. "Couldn't mount because of "
  2369. "unsupported optional features (%x)",
  2370. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2371. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2372. return 0;
  2373. }
  2374. if (readonly)
  2375. return 1;
  2376. /* Check that feature set is OK for a read-write mount */
  2377. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
  2378. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2379. "unsupported optional features (%x)",
  2380. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2381. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2382. return 0;
  2383. }
  2384. /*
  2385. * Large file size enabled file system can only be mounted
  2386. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2387. */
  2388. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  2389. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2390. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2391. "cannot be mounted RDWR without "
  2392. "CONFIG_LBDAF");
  2393. return 0;
  2394. }
  2395. }
  2396. return 1;
  2397. }
  2398. /*
  2399. * This function is called once a day if we have errors logged
  2400. * on the file system
  2401. */
  2402. static void print_daily_error_info(unsigned long arg)
  2403. {
  2404. struct super_block *sb = (struct super_block *) arg;
  2405. struct ext4_sb_info *sbi;
  2406. struct ext4_super_block *es;
  2407. sbi = EXT4_SB(sb);
  2408. es = sbi->s_es;
  2409. if (es->s_error_count)
  2410. ext4_msg(sb, KERN_NOTICE, "error count: %u",
  2411. le32_to_cpu(es->s_error_count));
  2412. if (es->s_first_error_time) {
  2413. printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
  2414. sb->s_id, le32_to_cpu(es->s_first_error_time),
  2415. (int) sizeof(es->s_first_error_func),
  2416. es->s_first_error_func,
  2417. le32_to_cpu(es->s_first_error_line));
  2418. if (es->s_first_error_ino)
  2419. printk(": inode %u",
  2420. le32_to_cpu(es->s_first_error_ino));
  2421. if (es->s_first_error_block)
  2422. printk(": block %llu", (unsigned long long)
  2423. le64_to_cpu(es->s_first_error_block));
  2424. printk("\n");
  2425. }
  2426. if (es->s_last_error_time) {
  2427. printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
  2428. sb->s_id, le32_to_cpu(es->s_last_error_time),
  2429. (int) sizeof(es->s_last_error_func),
  2430. es->s_last_error_func,
  2431. le32_to_cpu(es->s_last_error_line));
  2432. if (es->s_last_error_ino)
  2433. printk(": inode %u",
  2434. le32_to_cpu(es->s_last_error_ino));
  2435. if (es->s_last_error_block)
  2436. printk(": block %llu", (unsigned long long)
  2437. le64_to_cpu(es->s_last_error_block));
  2438. printk("\n");
  2439. }
  2440. mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
  2441. }
  2442. /* Find next suitable group and run ext4_init_inode_table */
  2443. static int ext4_run_li_request(struct ext4_li_request *elr)
  2444. {
  2445. struct ext4_group_desc *gdp = NULL;
  2446. ext4_group_t group, ngroups;
  2447. struct super_block *sb;
  2448. unsigned long timeout = 0;
  2449. int ret = 0;
  2450. sb = elr->lr_super;
  2451. ngroups = EXT4_SB(sb)->s_groups_count;
  2452. for (group = elr->lr_next_group; group < ngroups; group++) {
  2453. gdp = ext4_get_group_desc(sb, group, NULL);
  2454. if (!gdp) {
  2455. ret = 1;
  2456. break;
  2457. }
  2458. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2459. break;
  2460. }
  2461. if (group == ngroups)
  2462. ret = 1;
  2463. if (!ret) {
  2464. timeout = jiffies;
  2465. ret = ext4_init_inode_table(sb, group,
  2466. elr->lr_timeout ? 0 : 1);
  2467. if (elr->lr_timeout == 0) {
  2468. timeout = (jiffies - timeout) *
  2469. elr->lr_sbi->s_li_wait_mult;
  2470. elr->lr_timeout = timeout;
  2471. }
  2472. elr->lr_next_sched = jiffies + elr->lr_timeout;
  2473. elr->lr_next_group = group + 1;
  2474. }
  2475. return ret;
  2476. }
  2477. /*
  2478. * Remove lr_request from the list_request and free the
  2479. * request structure. Should be called with li_list_mtx held
  2480. */
  2481. static void ext4_remove_li_request(struct ext4_li_request *elr)
  2482. {
  2483. struct ext4_sb_info *sbi;
  2484. if (!elr)
  2485. return;
  2486. sbi = elr->lr_sbi;
  2487. list_del(&elr->lr_request);
  2488. sbi->s_li_request = NULL;
  2489. kfree(elr);
  2490. }
  2491. static void ext4_unregister_li_request(struct super_block *sb)
  2492. {
  2493. mutex_lock(&ext4_li_mtx);
  2494. if (!ext4_li_info) {
  2495. mutex_unlock(&ext4_li_mtx);
  2496. return;
  2497. }
  2498. mutex_lock(&ext4_li_info->li_list_mtx);
  2499. ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
  2500. mutex_unlock(&ext4_li_info->li_list_mtx);
  2501. mutex_unlock(&ext4_li_mtx);
  2502. }
  2503. static struct task_struct *ext4_lazyinit_task;
  2504. /*
  2505. * This is the function where ext4lazyinit thread lives. It walks
  2506. * through the request list searching for next scheduled filesystem.
  2507. * When such a fs is found, run the lazy initialization request
  2508. * (ext4_rn_li_request) and keep track of the time spend in this
  2509. * function. Based on that time we compute next schedule time of
  2510. * the request. When walking through the list is complete, compute
  2511. * next waking time and put itself into sleep.
  2512. */
  2513. static int ext4_lazyinit_thread(void *arg)
  2514. {
  2515. struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
  2516. struct list_head *pos, *n;
  2517. struct ext4_li_request *elr;
  2518. unsigned long next_wakeup, cur;
  2519. BUG_ON(NULL == eli);
  2520. cont_thread:
  2521. while (true) {
  2522. next_wakeup = MAX_JIFFY_OFFSET;
  2523. mutex_lock(&eli->li_list_mtx);
  2524. if (list_empty(&eli->li_request_list)) {
  2525. mutex_unlock(&eli->li_list_mtx);
  2526. goto exit_thread;
  2527. }
  2528. list_for_each_safe(pos, n, &eli->li_request_list) {
  2529. elr = list_entry(pos, struct ext4_li_request,
  2530. lr_request);
  2531. if (time_after_eq(jiffies, elr->lr_next_sched)) {
  2532. if (ext4_run_li_request(elr) != 0) {
  2533. /* error, remove the lazy_init job */
  2534. ext4_remove_li_request(elr);
  2535. continue;
  2536. }
  2537. }
  2538. if (time_before(elr->lr_next_sched, next_wakeup))
  2539. next_wakeup = elr->lr_next_sched;
  2540. }
  2541. mutex_unlock(&eli->li_list_mtx);
  2542. if (freezing(current))
  2543. refrigerator();
  2544. cur = jiffies;
  2545. if ((time_after_eq(cur, next_wakeup)) ||
  2546. (MAX_JIFFY_OFFSET == next_wakeup)) {
  2547. cond_resched();
  2548. continue;
  2549. }
  2550. schedule_timeout_interruptible(next_wakeup - cur);
  2551. if (kthread_should_stop()) {
  2552. ext4_clear_request_list();
  2553. goto exit_thread;
  2554. }
  2555. }
  2556. exit_thread:
  2557. /*
  2558. * It looks like the request list is empty, but we need
  2559. * to check it under the li_list_mtx lock, to prevent any
  2560. * additions into it, and of course we should lock ext4_li_mtx
  2561. * to atomically free the list and ext4_li_info, because at
  2562. * this point another ext4 filesystem could be registering
  2563. * new one.
  2564. */
  2565. mutex_lock(&ext4_li_mtx);
  2566. mutex_lock(&eli->li_list_mtx);
  2567. if (!list_empty(&eli->li_request_list)) {
  2568. mutex_unlock(&eli->li_list_mtx);
  2569. mutex_unlock(&ext4_li_mtx);
  2570. goto cont_thread;
  2571. }
  2572. mutex_unlock(&eli->li_list_mtx);
  2573. kfree(ext4_li_info);
  2574. ext4_li_info = NULL;
  2575. mutex_unlock(&ext4_li_mtx);
  2576. return 0;
  2577. }
  2578. static void ext4_clear_request_list(void)
  2579. {
  2580. struct list_head *pos, *n;
  2581. struct ext4_li_request *elr;
  2582. mutex_lock(&ext4_li_info->li_list_mtx);
  2583. list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
  2584. elr = list_entry(pos, struct ext4_li_request,
  2585. lr_request);
  2586. ext4_remove_li_request(elr);
  2587. }
  2588. mutex_unlock(&ext4_li_info->li_list_mtx);
  2589. }
  2590. static int ext4_run_lazyinit_thread(void)
  2591. {
  2592. ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
  2593. ext4_li_info, "ext4lazyinit");
  2594. if (IS_ERR(ext4_lazyinit_task)) {
  2595. int err = PTR_ERR(ext4_lazyinit_task);
  2596. ext4_clear_request_list();
  2597. kfree(ext4_li_info);
  2598. ext4_li_info = NULL;
  2599. printk(KERN_CRIT "EXT4: error %d creating inode table "
  2600. "initialization thread\n",
  2601. err);
  2602. return err;
  2603. }
  2604. ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
  2605. return 0;
  2606. }
  2607. /*
  2608. * Check whether it make sense to run itable init. thread or not.
  2609. * If there is at least one uninitialized inode table, return
  2610. * corresponding group number, else the loop goes through all
  2611. * groups and return total number of groups.
  2612. */
  2613. static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
  2614. {
  2615. ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
  2616. struct ext4_group_desc *gdp = NULL;
  2617. for (group = 0; group < ngroups; group++) {
  2618. gdp = ext4_get_group_desc(sb, group, NULL);
  2619. if (!gdp)
  2620. continue;
  2621. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2622. break;
  2623. }
  2624. return group;
  2625. }
  2626. static int ext4_li_info_new(void)
  2627. {
  2628. struct ext4_lazy_init *eli = NULL;
  2629. eli = kzalloc(sizeof(*eli), GFP_KERNEL);
  2630. if (!eli)
  2631. return -ENOMEM;
  2632. INIT_LIST_HEAD(&eli->li_request_list);
  2633. mutex_init(&eli->li_list_mtx);
  2634. eli->li_state |= EXT4_LAZYINIT_QUIT;
  2635. ext4_li_info = eli;
  2636. return 0;
  2637. }
  2638. static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
  2639. ext4_group_t start)
  2640. {
  2641. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2642. struct ext4_li_request *elr;
  2643. unsigned long rnd;
  2644. elr = kzalloc(sizeof(*elr), GFP_KERNEL);
  2645. if (!elr)
  2646. return NULL;
  2647. elr->lr_super = sb;
  2648. elr->lr_sbi = sbi;
  2649. elr->lr_next_group = start;
  2650. /*
  2651. * Randomize first schedule time of the request to
  2652. * spread the inode table initialization requests
  2653. * better.
  2654. */
  2655. get_random_bytes(&rnd, sizeof(rnd));
  2656. elr->lr_next_sched = jiffies + (unsigned long)rnd %
  2657. (EXT4_DEF_LI_MAX_START_DELAY * HZ);
  2658. return elr;
  2659. }
  2660. static int ext4_register_li_request(struct super_block *sb,
  2661. ext4_group_t first_not_zeroed)
  2662. {
  2663. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2664. struct ext4_li_request *elr;
  2665. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  2666. int ret = 0;
  2667. if (sbi->s_li_request != NULL) {
  2668. /*
  2669. * Reset timeout so it can be computed again, because
  2670. * s_li_wait_mult might have changed.
  2671. */
  2672. sbi->s_li_request->lr_timeout = 0;
  2673. return 0;
  2674. }
  2675. if (first_not_zeroed == ngroups ||
  2676. (sb->s_flags & MS_RDONLY) ||
  2677. !test_opt(sb, INIT_INODE_TABLE))
  2678. return 0;
  2679. elr = ext4_li_request_new(sb, first_not_zeroed);
  2680. if (!elr)
  2681. return -ENOMEM;
  2682. mutex_lock(&ext4_li_mtx);
  2683. if (NULL == ext4_li_info) {
  2684. ret = ext4_li_info_new();
  2685. if (ret)
  2686. goto out;
  2687. }
  2688. mutex_lock(&ext4_li_info->li_list_mtx);
  2689. list_add(&elr->lr_request, &ext4_li_info->li_request_list);
  2690. mutex_unlock(&ext4_li_info->li_list_mtx);
  2691. sbi->s_li_request = elr;
  2692. /*
  2693. * set elr to NULL here since it has been inserted to
  2694. * the request_list and the removal and free of it is
  2695. * handled by ext4_clear_request_list from now on.
  2696. */
  2697. elr = NULL;
  2698. if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
  2699. ret = ext4_run_lazyinit_thread();
  2700. if (ret)
  2701. goto out;
  2702. }
  2703. out:
  2704. mutex_unlock(&ext4_li_mtx);
  2705. if (ret)
  2706. kfree(elr);
  2707. return ret;
  2708. }
  2709. /*
  2710. * We do not need to lock anything since this is called on
  2711. * module unload.
  2712. */
  2713. static void ext4_destroy_lazyinit_thread(void)
  2714. {
  2715. /*
  2716. * If thread exited earlier
  2717. * there's nothing to be done.
  2718. */
  2719. if (!ext4_li_info || !ext4_lazyinit_task)
  2720. return;
  2721. kthread_stop(ext4_lazyinit_task);
  2722. }
  2723. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  2724. __releases(kernel_lock)
  2725. __acquires(kernel_lock)
  2726. {
  2727. char *orig_data = kstrdup(data, GFP_KERNEL);
  2728. struct buffer_head *bh;
  2729. struct ext4_super_block *es = NULL;
  2730. struct ext4_sb_info *sbi;
  2731. ext4_fsblk_t block;
  2732. ext4_fsblk_t sb_block = get_sb_block(&data);
  2733. ext4_fsblk_t logical_sb_block;
  2734. unsigned long offset = 0;
  2735. unsigned long journal_devnum = 0;
  2736. unsigned long def_mount_opts;
  2737. struct inode *root;
  2738. char *cp;
  2739. const char *descr;
  2740. int ret = -ENOMEM;
  2741. int blocksize;
  2742. unsigned int db_count;
  2743. unsigned int i;
  2744. int needs_recovery, has_huge_files;
  2745. __u64 blocks_count;
  2746. int err;
  2747. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  2748. ext4_group_t first_not_zeroed;
  2749. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  2750. if (!sbi)
  2751. goto out_free_orig;
  2752. sbi->s_blockgroup_lock =
  2753. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  2754. if (!sbi->s_blockgroup_lock) {
  2755. kfree(sbi);
  2756. goto out_free_orig;
  2757. }
  2758. sb->s_fs_info = sbi;
  2759. sbi->s_mount_opt = 0;
  2760. sbi->s_resuid = EXT4_DEF_RESUID;
  2761. sbi->s_resgid = EXT4_DEF_RESGID;
  2762. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  2763. sbi->s_sb_block = sb_block;
  2764. if (sb->s_bdev->bd_part)
  2765. sbi->s_sectors_written_start =
  2766. part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  2767. /* Cleanup superblock name */
  2768. for (cp = sb->s_id; (cp = strchr(cp, '/'));)
  2769. *cp = '!';
  2770. ret = -EINVAL;
  2771. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  2772. if (!blocksize) {
  2773. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  2774. goto out_fail;
  2775. }
  2776. /*
  2777. * The ext4 superblock will not be buffer aligned for other than 1kB
  2778. * block sizes. We need to calculate the offset from buffer start.
  2779. */
  2780. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  2781. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2782. offset = do_div(logical_sb_block, blocksize);
  2783. } else {
  2784. logical_sb_block = sb_block;
  2785. }
  2786. if (!(bh = sb_bread(sb, logical_sb_block))) {
  2787. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  2788. goto out_fail;
  2789. }
  2790. /*
  2791. * Note: s_es must be initialized as soon as possible because
  2792. * some ext4 macro-instructions depend on its value
  2793. */
  2794. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2795. sbi->s_es = es;
  2796. sb->s_magic = le16_to_cpu(es->s_magic);
  2797. if (sb->s_magic != EXT4_SUPER_MAGIC)
  2798. goto cantfind_ext4;
  2799. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  2800. /* Set defaults before we parse the mount options */
  2801. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  2802. set_opt(sb, INIT_INODE_TABLE);
  2803. if (def_mount_opts & EXT4_DEFM_DEBUG)
  2804. set_opt(sb, DEBUG);
  2805. if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
  2806. ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
  2807. "2.6.38");
  2808. set_opt(sb, GRPID);
  2809. }
  2810. if (def_mount_opts & EXT4_DEFM_UID16)
  2811. set_opt(sb, NO_UID32);
  2812. /* xattr user namespace & acls are now defaulted on */
  2813. #ifdef CONFIG_EXT4_FS_XATTR
  2814. set_opt(sb, XATTR_USER);
  2815. #endif
  2816. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  2817. set_opt(sb, POSIX_ACL);
  2818. #endif
  2819. set_opt(sb, MBLK_IO_SUBMIT);
  2820. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  2821. set_opt(sb, JOURNAL_DATA);
  2822. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  2823. set_opt(sb, ORDERED_DATA);
  2824. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  2825. set_opt(sb, WRITEBACK_DATA);
  2826. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  2827. set_opt(sb, ERRORS_PANIC);
  2828. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  2829. set_opt(sb, ERRORS_CONT);
  2830. else
  2831. set_opt(sb, ERRORS_RO);
  2832. if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
  2833. set_opt(sb, BLOCK_VALIDITY);
  2834. if (def_mount_opts & EXT4_DEFM_DISCARD)
  2835. set_opt(sb, DISCARD);
  2836. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  2837. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  2838. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  2839. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  2840. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  2841. if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
  2842. set_opt(sb, BARRIER);
  2843. /*
  2844. * enable delayed allocation by default
  2845. * Use -o nodelalloc to turn it off
  2846. */
  2847. if (!IS_EXT3_SB(sb) &&
  2848. ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
  2849. set_opt(sb, DELALLOC);
  2850. /*
  2851. * set default s_li_wait_mult for lazyinit, for the case there is
  2852. * no mount option specified.
  2853. */
  2854. sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  2855. if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
  2856. &journal_devnum, &journal_ioprio, NULL, 0)) {
  2857. ext4_msg(sb, KERN_WARNING,
  2858. "failed to parse options in superblock: %s",
  2859. sbi->s_es->s_mount_opts);
  2860. }
  2861. if (!parse_options((char *) data, sb, &journal_devnum,
  2862. &journal_ioprio, NULL, 0))
  2863. goto failed_mount;
  2864. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2865. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2866. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  2867. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  2868. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  2869. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  2870. ext4_msg(sb, KERN_WARNING,
  2871. "feature flags set on rev 0 fs, "
  2872. "running e2fsck is recommended");
  2873. if (IS_EXT2_SB(sb)) {
  2874. if (ext2_feature_set_ok(sb))
  2875. ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
  2876. "using the ext4 subsystem");
  2877. else {
  2878. ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
  2879. "to feature incompatibilities");
  2880. goto failed_mount;
  2881. }
  2882. }
  2883. if (IS_EXT3_SB(sb)) {
  2884. if (ext3_feature_set_ok(sb))
  2885. ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
  2886. "using the ext4 subsystem");
  2887. else {
  2888. ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
  2889. "to feature incompatibilities");
  2890. goto failed_mount;
  2891. }
  2892. }
  2893. /*
  2894. * Check feature flags regardless of the revision level, since we
  2895. * previously didn't change the revision level when setting the flags,
  2896. * so there is a chance incompat flags are set on a rev 0 filesystem.
  2897. */
  2898. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  2899. goto failed_mount;
  2900. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  2901. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  2902. blocksize > EXT4_MAX_BLOCK_SIZE) {
  2903. ext4_msg(sb, KERN_ERR,
  2904. "Unsupported filesystem blocksize %d", blocksize);
  2905. goto failed_mount;
  2906. }
  2907. if (sb->s_blocksize != blocksize) {
  2908. /* Validate the filesystem blocksize */
  2909. if (!sb_set_blocksize(sb, blocksize)) {
  2910. ext4_msg(sb, KERN_ERR, "bad block size %d",
  2911. blocksize);
  2912. goto failed_mount;
  2913. }
  2914. brelse(bh);
  2915. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2916. offset = do_div(logical_sb_block, blocksize);
  2917. bh = sb_bread(sb, logical_sb_block);
  2918. if (!bh) {
  2919. ext4_msg(sb, KERN_ERR,
  2920. "Can't read superblock on 2nd try");
  2921. goto failed_mount;
  2922. }
  2923. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  2924. sbi->s_es = es;
  2925. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  2926. ext4_msg(sb, KERN_ERR,
  2927. "Magic mismatch, very weird!");
  2928. goto failed_mount;
  2929. }
  2930. }
  2931. has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2932. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  2933. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  2934. has_huge_files);
  2935. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  2936. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  2937. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  2938. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  2939. } else {
  2940. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  2941. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  2942. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  2943. (!is_power_of_2(sbi->s_inode_size)) ||
  2944. (sbi->s_inode_size > blocksize)) {
  2945. ext4_msg(sb, KERN_ERR,
  2946. "unsupported inode size: %d",
  2947. sbi->s_inode_size);
  2948. goto failed_mount;
  2949. }
  2950. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  2951. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  2952. }
  2953. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  2954. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  2955. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  2956. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  2957. !is_power_of_2(sbi->s_desc_size)) {
  2958. ext4_msg(sb, KERN_ERR,
  2959. "unsupported descriptor size %lu",
  2960. sbi->s_desc_size);
  2961. goto failed_mount;
  2962. }
  2963. } else
  2964. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  2965. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  2966. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  2967. if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
  2968. goto cantfind_ext4;
  2969. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  2970. if (sbi->s_inodes_per_block == 0)
  2971. goto cantfind_ext4;
  2972. sbi->s_itb_per_group = DIV_ROUND_UP(sbi->s_inodes_per_group,
  2973. sbi->s_inodes_per_block);
  2974. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  2975. sbi->s_sbh = bh;
  2976. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2977. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  2978. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  2979. for (i = 0; i < 4; i++)
  2980. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  2981. sbi->s_def_hash_version = es->s_def_hash_version;
  2982. i = le32_to_cpu(es->s_flags);
  2983. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  2984. sbi->s_hash_unsigned = 3;
  2985. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  2986. #ifdef __CHAR_UNSIGNED__
  2987. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  2988. sbi->s_hash_unsigned = 3;
  2989. #else
  2990. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  2991. #endif
  2992. sb->s_dirt = 1;
  2993. }
  2994. if (sbi->s_blocks_per_group > blocksize * 8) {
  2995. ext4_msg(sb, KERN_ERR,
  2996. "#blocks per group too big: %lu",
  2997. sbi->s_blocks_per_group);
  2998. goto failed_mount;
  2999. }
  3000. if (sbi->s_inodes_per_group > blocksize * 8) {
  3001. ext4_msg(sb, KERN_ERR,
  3002. "#inodes per group too big: %lu",
  3003. sbi->s_inodes_per_group);
  3004. goto failed_mount;
  3005. }
  3006. /*
  3007. * Test whether we have more sectors than will fit in sector_t,
  3008. * and whether the max offset is addressable by the page cache.
  3009. */
  3010. err = generic_check_addressable(sb->s_blocksize_bits,
  3011. ext4_blocks_count(es));
  3012. if (err) {
  3013. ext4_msg(sb, KERN_ERR, "filesystem"
  3014. " too large to mount safely on this system");
  3015. if (sizeof(sector_t) < 8)
  3016. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  3017. ret = err;
  3018. goto failed_mount;
  3019. }
  3020. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  3021. goto cantfind_ext4;
  3022. /* check blocks count against device size */
  3023. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  3024. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  3025. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  3026. "exceeds size of device (%llu blocks)",
  3027. ext4_blocks_count(es), blocks_count);
  3028. goto failed_mount;
  3029. }
  3030. /*
  3031. * It makes no sense for the first data block to be beyond the end
  3032. * of the filesystem.
  3033. */
  3034. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  3035. ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
  3036. "block %u is beyond end of filesystem (%llu)",
  3037. le32_to_cpu(es->s_first_data_block),
  3038. ext4_blocks_count(es));
  3039. goto failed_mount;
  3040. }
  3041. blocks_count = (ext4_blocks_count(es) -
  3042. le32_to_cpu(es->s_first_data_block) +
  3043. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  3044. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  3045. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  3046. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  3047. "(block count %llu, first data block %u, "
  3048. "blocks per group %lu)", sbi->s_groups_count,
  3049. ext4_blocks_count(es),
  3050. le32_to_cpu(es->s_first_data_block),
  3051. EXT4_BLOCKS_PER_GROUP(sb));
  3052. goto failed_mount;
  3053. }
  3054. sbi->s_groups_count = blocks_count;
  3055. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  3056. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  3057. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  3058. EXT4_DESC_PER_BLOCK(sb);
  3059. sbi->s_group_desc = ext4_kvmalloc(db_count *
  3060. sizeof(struct buffer_head *),
  3061. GFP_KERNEL);
  3062. if (sbi->s_group_desc == NULL) {
  3063. ext4_msg(sb, KERN_ERR, "not enough memory");
  3064. goto failed_mount;
  3065. }
  3066. #ifdef CONFIG_PROC_FS
  3067. if (ext4_proc_root)
  3068. sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
  3069. #endif
  3070. bgl_lock_init(sbi->s_blockgroup_lock);
  3071. for (i = 0; i < db_count; i++) {
  3072. block = descriptor_loc(sb, logical_sb_block, i);
  3073. sbi->s_group_desc[i] = sb_bread(sb, block);
  3074. if (!sbi->s_group_desc[i]) {
  3075. ext4_msg(sb, KERN_ERR,
  3076. "can't read group descriptor %d", i);
  3077. db_count = i;
  3078. goto failed_mount2;
  3079. }
  3080. }
  3081. if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
  3082. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  3083. goto failed_mount2;
  3084. }
  3085. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  3086. if (!ext4_fill_flex_info(sb)) {
  3087. ext4_msg(sb, KERN_ERR,
  3088. "unable to initialize "
  3089. "flex_bg meta info!");
  3090. goto failed_mount2;
  3091. }
  3092. sbi->s_gdb_count = db_count;
  3093. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  3094. spin_lock_init(&sbi->s_next_gen_lock);
  3095. init_timer(&sbi->s_err_report);
  3096. sbi->s_err_report.function = print_daily_error_info;
  3097. sbi->s_err_report.data = (unsigned long) sb;
  3098. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  3099. ext4_count_free_blocks(sb));
  3100. if (!err) {
  3101. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  3102. ext4_count_free_inodes(sb));
  3103. }
  3104. if (!err) {
  3105. err = percpu_counter_init(&sbi->s_dirs_counter,
  3106. ext4_count_dirs(sb));
  3107. }
  3108. if (!err) {
  3109. err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
  3110. }
  3111. if (err) {
  3112. ext4_msg(sb, KERN_ERR, "insufficient memory");
  3113. goto failed_mount3;
  3114. }
  3115. sbi->s_stripe = ext4_get_stripe_size(sbi);
  3116. sbi->s_max_writeback_mb_bump = 128;
  3117. /*
  3118. * set up enough so that it can read an inode
  3119. */
  3120. if (!test_opt(sb, NOLOAD) &&
  3121. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  3122. sb->s_op = &ext4_sops;
  3123. else
  3124. sb->s_op = &ext4_nojournal_sops;
  3125. sb->s_export_op = &ext4_export_ops;
  3126. sb->s_xattr = ext4_xattr_handlers;
  3127. #ifdef CONFIG_QUOTA
  3128. sb->s_qcop = &ext4_qctl_operations;
  3129. sb->dq_op = &ext4_quota_operations;
  3130. #endif
  3131. memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
  3132. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  3133. mutex_init(&sbi->s_orphan_lock);
  3134. sbi->s_resize_flags = 0;
  3135. sb->s_root = NULL;
  3136. needs_recovery = (es->s_last_orphan != 0 ||
  3137. EXT4_HAS_INCOMPAT_FEATURE(sb,
  3138. EXT4_FEATURE_INCOMPAT_RECOVER));
  3139. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
  3140. !(sb->s_flags & MS_RDONLY))
  3141. if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
  3142. goto failed_mount3;
  3143. /*
  3144. * The first inode we look at is the journal inode. Don't try
  3145. * root first: it may be modified in the journal!
  3146. */
  3147. if (!test_opt(sb, NOLOAD) &&
  3148. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3149. if (ext4_load_journal(sb, es, journal_devnum))
  3150. goto failed_mount3;
  3151. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  3152. EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3153. ext4_msg(sb, KERN_ERR, "required journal recovery "
  3154. "suppressed and not mounted read-only");
  3155. goto failed_mount_wq;
  3156. } else {
  3157. clear_opt(sb, DATA_FLAGS);
  3158. sbi->s_journal = NULL;
  3159. needs_recovery = 0;
  3160. goto no_journal;
  3161. }
  3162. if (ext4_blocks_count(es) > 0xffffffffULL &&
  3163. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  3164. JBD2_FEATURE_INCOMPAT_64BIT)) {
  3165. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  3166. goto failed_mount_wq;
  3167. }
  3168. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3169. jbd2_journal_set_features(sbi->s_journal,
  3170. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3171. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3172. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  3173. jbd2_journal_set_features(sbi->s_journal,
  3174. JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
  3175. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  3176. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3177. } else {
  3178. jbd2_journal_clear_features(sbi->s_journal,
  3179. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3180. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3181. }
  3182. /* We have now updated the journal if required, so we can
  3183. * validate the data journaling mode. */
  3184. switch (test_opt(sb, DATA_FLAGS)) {
  3185. case 0:
  3186. /* No mode set, assume a default based on the journal
  3187. * capabilities: ORDERED_DATA if the journal can
  3188. * cope, else JOURNAL_DATA
  3189. */
  3190. if (jbd2_journal_check_available_features
  3191. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  3192. set_opt(sb, ORDERED_DATA);
  3193. else
  3194. set_opt(sb, JOURNAL_DATA);
  3195. break;
  3196. case EXT4_MOUNT_ORDERED_DATA:
  3197. case EXT4_MOUNT_WRITEBACK_DATA:
  3198. if (!jbd2_journal_check_available_features
  3199. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  3200. ext4_msg(sb, KERN_ERR, "Journal does not support "
  3201. "requested data journaling mode");
  3202. goto failed_mount_wq;
  3203. }
  3204. default:
  3205. break;
  3206. }
  3207. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3208. /*
  3209. * The journal may have updated the bg summary counts, so we
  3210. * need to update the global counters.
  3211. */
  3212. percpu_counter_set(&sbi->s_freeblocks_counter,
  3213. ext4_count_free_blocks(sb));
  3214. percpu_counter_set(&sbi->s_freeinodes_counter,
  3215. ext4_count_free_inodes(sb));
  3216. percpu_counter_set(&sbi->s_dirs_counter,
  3217. ext4_count_dirs(sb));
  3218. percpu_counter_set(&sbi->s_dirtyblocks_counter, 0);
  3219. no_journal:
  3220. /*
  3221. * The maximum number of concurrent works can be high and
  3222. * concurrency isn't really necessary. Limit it to 1.
  3223. */
  3224. EXT4_SB(sb)->dio_unwritten_wq =
  3225. alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  3226. if (!EXT4_SB(sb)->dio_unwritten_wq) {
  3227. printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
  3228. goto failed_mount_wq;
  3229. }
  3230. /*
  3231. * The jbd2_journal_load will have done any necessary log recovery,
  3232. * so we can safely mount the rest of the filesystem now.
  3233. */
  3234. root = ext4_iget(sb, EXT4_ROOT_INO);
  3235. if (IS_ERR(root)) {
  3236. ext4_msg(sb, KERN_ERR, "get root inode failed");
  3237. ret = PTR_ERR(root);
  3238. root = NULL;
  3239. goto failed_mount4;
  3240. }
  3241. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  3242. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  3243. goto failed_mount4;
  3244. }
  3245. sb->s_root = d_alloc_root(root);
  3246. if (!sb->s_root) {
  3247. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  3248. ret = -ENOMEM;
  3249. goto failed_mount4;
  3250. }
  3251. ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
  3252. /* determine the minimum size of new large inodes, if present */
  3253. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  3254. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3255. EXT4_GOOD_OLD_INODE_SIZE;
  3256. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3257. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  3258. if (sbi->s_want_extra_isize <
  3259. le16_to_cpu(es->s_want_extra_isize))
  3260. sbi->s_want_extra_isize =
  3261. le16_to_cpu(es->s_want_extra_isize);
  3262. if (sbi->s_want_extra_isize <
  3263. le16_to_cpu(es->s_min_extra_isize))
  3264. sbi->s_want_extra_isize =
  3265. le16_to_cpu(es->s_min_extra_isize);
  3266. }
  3267. }
  3268. /* Check if enough inode space is available */
  3269. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  3270. sbi->s_inode_size) {
  3271. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3272. EXT4_GOOD_OLD_INODE_SIZE;
  3273. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  3274. "available");
  3275. }
  3276. if (test_opt(sb, DELALLOC) &&
  3277. (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
  3278. ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
  3279. "requested data journaling mode");
  3280. clear_opt(sb, DELALLOC);
  3281. }
  3282. if (test_opt(sb, DIOREAD_NOLOCK)) {
  3283. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  3284. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  3285. "option - requested data journaling mode");
  3286. clear_opt(sb, DIOREAD_NOLOCK);
  3287. }
  3288. if (sb->s_blocksize < PAGE_SIZE) {
  3289. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  3290. "option - block size is too small");
  3291. clear_opt(sb, DIOREAD_NOLOCK);
  3292. }
  3293. }
  3294. err = ext4_setup_system_zone(sb);
  3295. if (err) {
  3296. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  3297. "zone (%d)", err);
  3298. goto failed_mount4;
  3299. }
  3300. ext4_ext_init(sb);
  3301. err = ext4_mb_init(sb, needs_recovery);
  3302. if (err) {
  3303. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  3304. err);
  3305. goto failed_mount4;
  3306. }
  3307. err = ext4_register_li_request(sb, first_not_zeroed);
  3308. if (err)
  3309. goto failed_mount4;
  3310. sbi->s_kobj.kset = ext4_kset;
  3311. init_completion(&sbi->s_kobj_unregister);
  3312. err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
  3313. "%s", sb->s_id);
  3314. if (err) {
  3315. ext4_mb_release(sb);
  3316. ext4_ext_release(sb);
  3317. goto failed_mount4;
  3318. };
  3319. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  3320. ext4_orphan_cleanup(sb, es);
  3321. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  3322. if (needs_recovery) {
  3323. ext4_msg(sb, KERN_INFO, "recovery complete");
  3324. ext4_mark_recovery_complete(sb, es);
  3325. }
  3326. if (EXT4_SB(sb)->s_journal) {
  3327. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  3328. descr = " journalled data mode";
  3329. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  3330. descr = " ordered data mode";
  3331. else
  3332. descr = " writeback data mode";
  3333. } else
  3334. descr = "out journal";
  3335. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  3336. "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
  3337. *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
  3338. if (es->s_error_count)
  3339. mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
  3340. kfree(orig_data);
  3341. return 0;
  3342. cantfind_ext4:
  3343. if (!silent)
  3344. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  3345. goto failed_mount;
  3346. failed_mount4:
  3347. iput(root);
  3348. sb->s_root = NULL;
  3349. ext4_msg(sb, KERN_ERR, "mount failed");
  3350. destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
  3351. failed_mount_wq:
  3352. ext4_release_system_zone(sb);
  3353. if (sbi->s_journal) {
  3354. jbd2_journal_destroy(sbi->s_journal);
  3355. sbi->s_journal = NULL;
  3356. }
  3357. failed_mount3:
  3358. del_timer(&sbi->s_err_report);
  3359. if (sbi->s_flex_groups)
  3360. ext4_kvfree(sbi->s_flex_groups);
  3361. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  3362. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  3363. percpu_counter_destroy(&sbi->s_dirs_counter);
  3364. percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
  3365. if (sbi->s_mmp_tsk)
  3366. kthread_stop(sbi->s_mmp_tsk);
  3367. failed_mount2:
  3368. for (i = 0; i < db_count; i++)
  3369. brelse(sbi->s_group_desc[i]);
  3370. ext4_kvfree(sbi->s_group_desc);
  3371. failed_mount:
  3372. if (sbi->s_proc) {
  3373. remove_proc_entry(sb->s_id, ext4_proc_root);
  3374. }
  3375. #ifdef CONFIG_QUOTA
  3376. for (i = 0; i < MAXQUOTAS; i++)
  3377. kfree(sbi->s_qf_names[i]);
  3378. #endif
  3379. ext4_blkdev_remove(sbi);
  3380. brelse(bh);
  3381. out_fail:
  3382. sb->s_fs_info = NULL;
  3383. kfree(sbi->s_blockgroup_lock);
  3384. kfree(sbi);
  3385. out_free_orig:
  3386. kfree(orig_data);
  3387. return ret;
  3388. }
  3389. /*
  3390. * Setup any per-fs journal parameters now. We'll do this both on
  3391. * initial mount, once the journal has been initialised but before we've
  3392. * done any recovery; and again on any subsequent remount.
  3393. */
  3394. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  3395. {
  3396. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3397. journal->j_commit_interval = sbi->s_commit_interval;
  3398. journal->j_min_batch_time = sbi->s_min_batch_time;
  3399. journal->j_max_batch_time = sbi->s_max_batch_time;
  3400. write_lock(&journal->j_state_lock);
  3401. if (test_opt(sb, BARRIER))
  3402. journal->j_flags |= JBD2_BARRIER;
  3403. else
  3404. journal->j_flags &= ~JBD2_BARRIER;
  3405. if (test_opt(sb, DATA_ERR_ABORT))
  3406. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  3407. else
  3408. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  3409. write_unlock(&journal->j_state_lock);
  3410. }
  3411. static journal_t *ext4_get_journal(struct super_block *sb,
  3412. unsigned int journal_inum)
  3413. {
  3414. struct inode *journal_inode;
  3415. journal_t *journal;
  3416. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3417. /* First, test for the existence of a valid inode on disk. Bad
  3418. * things happen if we iget() an unused inode, as the subsequent
  3419. * iput() will try to delete it. */
  3420. journal_inode = ext4_iget(sb, journal_inum);
  3421. if (IS_ERR(journal_inode)) {
  3422. ext4_msg(sb, KERN_ERR, "no journal found");
  3423. return NULL;
  3424. }
  3425. if (!journal_inode->i_nlink) {
  3426. make_bad_inode(journal_inode);
  3427. iput(journal_inode);
  3428. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  3429. return NULL;
  3430. }
  3431. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  3432. journal_inode, journal_inode->i_size);
  3433. if (!S_ISREG(journal_inode->i_mode)) {
  3434. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  3435. iput(journal_inode);
  3436. return NULL;
  3437. }
  3438. journal = jbd2_journal_init_inode(journal_inode);
  3439. if (!journal) {
  3440. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  3441. iput(journal_inode);
  3442. return NULL;
  3443. }
  3444. journal->j_private = sb;
  3445. ext4_init_journal_params(sb, journal);
  3446. return journal;
  3447. }
  3448. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  3449. dev_t j_dev)
  3450. {
  3451. struct buffer_head *bh;
  3452. journal_t *journal;
  3453. ext4_fsblk_t start;
  3454. ext4_fsblk_t len;
  3455. int hblock, blocksize;
  3456. ext4_fsblk_t sb_block;
  3457. unsigned long offset;
  3458. struct ext4_super_block *es;
  3459. struct block_device *bdev;
  3460. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3461. bdev = ext4_blkdev_get(j_dev, sb);
  3462. if (bdev == NULL)
  3463. return NULL;
  3464. blocksize = sb->s_blocksize;
  3465. hblock = bdev_logical_block_size(bdev);
  3466. if (blocksize < hblock) {
  3467. ext4_msg(sb, KERN_ERR,
  3468. "blocksize too small for journal device");
  3469. goto out_bdev;
  3470. }
  3471. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  3472. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  3473. set_blocksize(bdev, blocksize);
  3474. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  3475. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  3476. "external journal");
  3477. goto out_bdev;
  3478. }
  3479. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  3480. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  3481. !(le32_to_cpu(es->s_feature_incompat) &
  3482. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  3483. ext4_msg(sb, KERN_ERR, "external journal has "
  3484. "bad superblock");
  3485. brelse(bh);
  3486. goto out_bdev;
  3487. }
  3488. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  3489. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  3490. brelse(bh);
  3491. goto out_bdev;
  3492. }
  3493. len = ext4_blocks_count(es);
  3494. start = sb_block + 1;
  3495. brelse(bh); /* we're done with the superblock */
  3496. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  3497. start, len, blocksize);
  3498. if (!journal) {
  3499. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  3500. goto out_bdev;
  3501. }
  3502. journal->j_private = sb;
  3503. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  3504. wait_on_buffer(journal->j_sb_buffer);
  3505. if (!buffer_uptodate(journal->j_sb_buffer)) {
  3506. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  3507. goto out_journal;
  3508. }
  3509. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  3510. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  3511. "user (unsupported) - %d",
  3512. be32_to_cpu(journal->j_superblock->s_nr_users));
  3513. goto out_journal;
  3514. }
  3515. EXT4_SB(sb)->journal_bdev = bdev;
  3516. ext4_init_journal_params(sb, journal);
  3517. return journal;
  3518. out_journal:
  3519. jbd2_journal_destroy(journal);
  3520. out_bdev:
  3521. ext4_blkdev_put(bdev);
  3522. return NULL;
  3523. }
  3524. static int ext4_load_journal(struct super_block *sb,
  3525. struct ext4_super_block *es,
  3526. unsigned long journal_devnum)
  3527. {
  3528. journal_t *journal;
  3529. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  3530. dev_t journal_dev;
  3531. int err = 0;
  3532. int really_read_only;
  3533. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3534. if (journal_devnum &&
  3535. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3536. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  3537. "numbers have changed");
  3538. journal_dev = new_decode_dev(journal_devnum);
  3539. } else
  3540. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  3541. really_read_only = bdev_read_only(sb->s_bdev);
  3542. /*
  3543. * Are we loading a blank journal or performing recovery after a
  3544. * crash? For recovery, we need to check in advance whether we
  3545. * can get read-write access to the device.
  3546. */
  3547. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3548. if (sb->s_flags & MS_RDONLY) {
  3549. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  3550. "required on readonly filesystem");
  3551. if (really_read_only) {
  3552. ext4_msg(sb, KERN_ERR, "write access "
  3553. "unavailable, cannot proceed");
  3554. return -EROFS;
  3555. }
  3556. ext4_msg(sb, KERN_INFO, "write access will "
  3557. "be enabled during recovery");
  3558. }
  3559. }
  3560. if (journal_inum && journal_dev) {
  3561. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  3562. "and inode journals!");
  3563. return -EINVAL;
  3564. }
  3565. if (journal_inum) {
  3566. if (!(journal = ext4_get_journal(sb, journal_inum)))
  3567. return -EINVAL;
  3568. } else {
  3569. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  3570. return -EINVAL;
  3571. }
  3572. if (!(journal->j_flags & JBD2_BARRIER))
  3573. ext4_msg(sb, KERN_INFO, "barriers disabled");
  3574. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  3575. err = jbd2_journal_update_format(journal);
  3576. if (err) {
  3577. ext4_msg(sb, KERN_ERR, "error updating journal");
  3578. jbd2_journal_destroy(journal);
  3579. return err;
  3580. }
  3581. }
  3582. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  3583. err = jbd2_journal_wipe(journal, !really_read_only);
  3584. if (!err) {
  3585. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  3586. if (save)
  3587. memcpy(save, ((char *) es) +
  3588. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  3589. err = jbd2_journal_load(journal);
  3590. if (save)
  3591. memcpy(((char *) es) + EXT4_S_ERR_START,
  3592. save, EXT4_S_ERR_LEN);
  3593. kfree(save);
  3594. }
  3595. if (err) {
  3596. ext4_msg(sb, KERN_ERR, "error loading journal");
  3597. jbd2_journal_destroy(journal);
  3598. return err;
  3599. }
  3600. EXT4_SB(sb)->s_journal = journal;
  3601. ext4_clear_journal_err(sb, es);
  3602. if (!really_read_only && journal_devnum &&
  3603. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3604. es->s_journal_dev = cpu_to_le32(journal_devnum);
  3605. /* Make sure we flush the recovery flag to disk. */
  3606. ext4_commit_super(sb, 1);
  3607. }
  3608. return 0;
  3609. }
  3610. static int ext4_commit_super(struct super_block *sb, int sync)
  3611. {
  3612. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  3613. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  3614. int error = 0;
  3615. if (!sbh)
  3616. return error;
  3617. if (buffer_write_io_error(sbh)) {
  3618. /*
  3619. * Oh, dear. A previous attempt to write the
  3620. * superblock failed. This could happen because the
  3621. * USB device was yanked out. Or it could happen to
  3622. * be a transient write error and maybe the block will
  3623. * be remapped. Nothing we can do but to retry the
  3624. * write and hope for the best.
  3625. */
  3626. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  3627. "superblock detected");
  3628. clear_buffer_write_io_error(sbh);
  3629. set_buffer_uptodate(sbh);
  3630. }
  3631. /*
  3632. * If the file system is mounted read-only, don't update the
  3633. * superblock write time. This avoids updating the superblock
  3634. * write time when we are mounting the root file system
  3635. * read/only but we need to replay the journal; at that point,
  3636. * for people who are east of GMT and who make their clock
  3637. * tick in localtime for Windows bug-for-bug compatibility,
  3638. * the clock is set in the future, and this will cause e2fsck
  3639. * to complain and force a full file system check.
  3640. */
  3641. if (!(sb->s_flags & MS_RDONLY))
  3642. es->s_wtime = cpu_to_le32(get_seconds());
  3643. if (sb->s_bdev->bd_part)
  3644. es->s_kbytes_written =
  3645. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  3646. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  3647. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  3648. else
  3649. es->s_kbytes_written =
  3650. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
  3651. ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
  3652. &EXT4_SB(sb)->s_freeblocks_counter));
  3653. es->s_free_inodes_count =
  3654. cpu_to_le32(percpu_counter_sum_positive(
  3655. &EXT4_SB(sb)->s_freeinodes_counter));
  3656. sb->s_dirt = 0;
  3657. BUFFER_TRACE(sbh, "marking dirty");
  3658. mark_buffer_dirty(sbh);
  3659. if (sync) {
  3660. error = sync_dirty_buffer(sbh);
  3661. if (error)
  3662. return error;
  3663. error = buffer_write_io_error(sbh);
  3664. if (error) {
  3665. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  3666. "superblock");
  3667. clear_buffer_write_io_error(sbh);
  3668. set_buffer_uptodate(sbh);
  3669. }
  3670. }
  3671. return error;
  3672. }
  3673. /*
  3674. * Have we just finished recovery? If so, and if we are mounting (or
  3675. * remounting) the filesystem readonly, then we will end up with a
  3676. * consistent fs on disk. Record that fact.
  3677. */
  3678. static void ext4_mark_recovery_complete(struct super_block *sb,
  3679. struct ext4_super_block *es)
  3680. {
  3681. journal_t *journal = EXT4_SB(sb)->s_journal;
  3682. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3683. BUG_ON(journal != NULL);
  3684. return;
  3685. }
  3686. jbd2_journal_lock_updates(journal);
  3687. if (jbd2_journal_flush(journal) < 0)
  3688. goto out;
  3689. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  3690. sb->s_flags & MS_RDONLY) {
  3691. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3692. ext4_commit_super(sb, 1);
  3693. }
  3694. out:
  3695. jbd2_journal_unlock_updates(journal);
  3696. }
  3697. /*
  3698. * If we are mounting (or read-write remounting) a filesystem whose journal
  3699. * has recorded an error from a previous lifetime, move that error to the
  3700. * main filesystem now.
  3701. */
  3702. static void ext4_clear_journal_err(struct super_block *sb,
  3703. struct ext4_super_block *es)
  3704. {
  3705. journal_t *journal;
  3706. int j_errno;
  3707. const char *errstr;
  3708. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3709. journal = EXT4_SB(sb)->s_journal;
  3710. /*
  3711. * Now check for any error status which may have been recorded in the
  3712. * journal by a prior ext4_error() or ext4_abort()
  3713. */
  3714. j_errno = jbd2_journal_errno(journal);
  3715. if (j_errno) {
  3716. char nbuf[16];
  3717. errstr = ext4_decode_error(sb, j_errno, nbuf);
  3718. ext4_warning(sb, "Filesystem error recorded "
  3719. "from previous mount: %s", errstr);
  3720. ext4_warning(sb, "Marking fs in need of filesystem check.");
  3721. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  3722. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  3723. ext4_commit_super(sb, 1);
  3724. jbd2_journal_clear_err(journal);
  3725. }
  3726. }
  3727. /*
  3728. * Force the running and committing transactions to commit,
  3729. * and wait on the commit.
  3730. */
  3731. int ext4_force_commit(struct super_block *sb)
  3732. {
  3733. journal_t *journal;
  3734. int ret = 0;
  3735. if (sb->s_flags & MS_RDONLY)
  3736. return 0;
  3737. journal = EXT4_SB(sb)->s_journal;
  3738. if (journal) {
  3739. vfs_check_frozen(sb, SB_FREEZE_TRANS);
  3740. ret = ext4_journal_force_commit(journal);
  3741. }
  3742. return ret;
  3743. }
  3744. static void ext4_write_super(struct super_block *sb)
  3745. {
  3746. lock_super(sb);
  3747. ext4_commit_super(sb, 1);
  3748. unlock_super(sb);
  3749. }
  3750. static int ext4_sync_fs(struct super_block *sb, int wait)
  3751. {
  3752. int ret = 0;
  3753. tid_t target;
  3754. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3755. trace_ext4_sync_fs(sb, wait);
  3756. flush_workqueue(sbi->dio_unwritten_wq);
  3757. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  3758. if (wait)
  3759. jbd2_log_wait_commit(sbi->s_journal, target);
  3760. }
  3761. return ret;
  3762. }
  3763. /*
  3764. * LVM calls this function before a (read-only) snapshot is created. This
  3765. * gives us a chance to flush the journal completely and mark the fs clean.
  3766. *
  3767. * Note that only this function cannot bring a filesystem to be in a clean
  3768. * state independently, because ext4 prevents a new handle from being started
  3769. * by @sb->s_frozen, which stays in an upper layer. It thus needs help from
  3770. * the upper layer.
  3771. */
  3772. static int ext4_freeze(struct super_block *sb)
  3773. {
  3774. int error = 0;
  3775. journal_t *journal;
  3776. if (sb->s_flags & MS_RDONLY)
  3777. return 0;
  3778. journal = EXT4_SB(sb)->s_journal;
  3779. /* Now we set up the journal barrier. */
  3780. jbd2_journal_lock_updates(journal);
  3781. /*
  3782. * Don't clear the needs_recovery flag if we failed to flush
  3783. * the journal.
  3784. */
  3785. error = jbd2_journal_flush(journal);
  3786. if (error < 0)
  3787. goto out;
  3788. /* Journal blocked and flushed, clear needs_recovery flag. */
  3789. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3790. error = ext4_commit_super(sb, 1);
  3791. out:
  3792. /* we rely on s_frozen to stop further updates */
  3793. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3794. return error;
  3795. }
  3796. /*
  3797. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  3798. * flag here, even though the filesystem is not technically dirty yet.
  3799. */
  3800. static int ext4_unfreeze(struct super_block *sb)
  3801. {
  3802. if (sb->s_flags & MS_RDONLY)
  3803. return 0;
  3804. lock_super(sb);
  3805. /* Reset the needs_recovery flag before the fs is unlocked. */
  3806. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3807. ext4_commit_super(sb, 1);
  3808. unlock_super(sb);
  3809. return 0;
  3810. }
  3811. /*
  3812. * Structure to save mount options for ext4_remount's benefit
  3813. */
  3814. struct ext4_mount_options {
  3815. unsigned long s_mount_opt;
  3816. unsigned long s_mount_opt2;
  3817. uid_t s_resuid;
  3818. gid_t s_resgid;
  3819. unsigned long s_commit_interval;
  3820. u32 s_min_batch_time, s_max_batch_time;
  3821. #ifdef CONFIG_QUOTA
  3822. int s_jquota_fmt;
  3823. char *s_qf_names[MAXQUOTAS];
  3824. #endif
  3825. };
  3826. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  3827. {
  3828. struct ext4_super_block *es;
  3829. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3830. ext4_fsblk_t n_blocks_count = 0;
  3831. unsigned long old_sb_flags;
  3832. struct ext4_mount_options old_opts;
  3833. int enable_quota = 0;
  3834. ext4_group_t g;
  3835. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3836. int err = 0;
  3837. #ifdef CONFIG_QUOTA
  3838. int i;
  3839. #endif
  3840. char *orig_data = kstrdup(data, GFP_KERNEL);
  3841. /* Store the original options */
  3842. lock_super(sb);
  3843. old_sb_flags = sb->s_flags;
  3844. old_opts.s_mount_opt = sbi->s_mount_opt;
  3845. old_opts.s_mount_opt2 = sbi->s_mount_opt2;
  3846. old_opts.s_resuid = sbi->s_resuid;
  3847. old_opts.s_resgid = sbi->s_resgid;
  3848. old_opts.s_commit_interval = sbi->s_commit_interval;
  3849. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  3850. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  3851. #ifdef CONFIG_QUOTA
  3852. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  3853. for (i = 0; i < MAXQUOTAS; i++)
  3854. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  3855. #endif
  3856. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  3857. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  3858. /*
  3859. * Allow the "check" option to be passed as a remount option.
  3860. */
  3861. if (!parse_options(data, sb, NULL, &journal_ioprio,
  3862. &n_blocks_count, 1)) {
  3863. err = -EINVAL;
  3864. goto restore_opts;
  3865. }
  3866. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  3867. ext4_abort(sb, "Abort forced by user");
  3868. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3869. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3870. es = sbi->s_es;
  3871. if (sbi->s_journal) {
  3872. ext4_init_journal_params(sb, sbi->s_journal);
  3873. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3874. }
  3875. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  3876. n_blocks_count > ext4_blocks_count(es)) {
  3877. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  3878. err = -EROFS;
  3879. goto restore_opts;
  3880. }
  3881. if (*flags & MS_RDONLY) {
  3882. err = dquot_suspend(sb, -1);
  3883. if (err < 0)
  3884. goto restore_opts;
  3885. /*
  3886. * First of all, the unconditional stuff we have to do
  3887. * to disable replay of the journal when we next remount
  3888. */
  3889. sb->s_flags |= MS_RDONLY;
  3890. /*
  3891. * OK, test if we are remounting a valid rw partition
  3892. * readonly, and if so set the rdonly flag and then
  3893. * mark the partition as valid again.
  3894. */
  3895. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  3896. (sbi->s_mount_state & EXT4_VALID_FS))
  3897. es->s_state = cpu_to_le16(sbi->s_mount_state);
  3898. if (sbi->s_journal)
  3899. ext4_mark_recovery_complete(sb, es);
  3900. } else {
  3901. /* Make sure we can mount this feature set readwrite */
  3902. if (!ext4_feature_set_ok(sb, 0)) {
  3903. err = -EROFS;
  3904. goto restore_opts;
  3905. }
  3906. /*
  3907. * Make sure the group descriptor checksums
  3908. * are sane. If they aren't, refuse to remount r/w.
  3909. */
  3910. for (g = 0; g < sbi->s_groups_count; g++) {
  3911. struct ext4_group_desc *gdp =
  3912. ext4_get_group_desc(sb, g, NULL);
  3913. if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
  3914. ext4_msg(sb, KERN_ERR,
  3915. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  3916. g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
  3917. le16_to_cpu(gdp->bg_checksum));
  3918. err = -EINVAL;
  3919. goto restore_opts;
  3920. }
  3921. }
  3922. /*
  3923. * If we have an unprocessed orphan list hanging
  3924. * around from a previously readonly bdev mount,
  3925. * require a full umount/remount for now.
  3926. */
  3927. if (es->s_last_orphan) {
  3928. ext4_msg(sb, KERN_WARNING, "Couldn't "
  3929. "remount RDWR because of unprocessed "
  3930. "orphan inode list. Please "
  3931. "umount/remount instead");
  3932. err = -EINVAL;
  3933. goto restore_opts;
  3934. }
  3935. /*
  3936. * Mounting a RDONLY partition read-write, so reread
  3937. * and store the current valid flag. (It may have
  3938. * been changed by e2fsck since we originally mounted
  3939. * the partition.)
  3940. */
  3941. if (sbi->s_journal)
  3942. ext4_clear_journal_err(sb, es);
  3943. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3944. if ((err = ext4_group_extend(sb, es, n_blocks_count)))
  3945. goto restore_opts;
  3946. if (!ext4_setup_super(sb, es, 0))
  3947. sb->s_flags &= ~MS_RDONLY;
  3948. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  3949. EXT4_FEATURE_INCOMPAT_MMP))
  3950. if (ext4_multi_mount_protect(sb,
  3951. le64_to_cpu(es->s_mmp_block))) {
  3952. err = -EROFS;
  3953. goto restore_opts;
  3954. }
  3955. enable_quota = 1;
  3956. }
  3957. }
  3958. /*
  3959. * Reinitialize lazy itable initialization thread based on
  3960. * current settings
  3961. */
  3962. if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
  3963. ext4_unregister_li_request(sb);
  3964. else {
  3965. ext4_group_t first_not_zeroed;
  3966. first_not_zeroed = ext4_has_uninit_itable(sb);
  3967. ext4_register_li_request(sb, first_not_zeroed);
  3968. }
  3969. ext4_setup_system_zone(sb);
  3970. if (sbi->s_journal == NULL)
  3971. ext4_commit_super(sb, 1);
  3972. #ifdef CONFIG_QUOTA
  3973. /* Release old quota file names */
  3974. for (i = 0; i < MAXQUOTAS; i++)
  3975. if (old_opts.s_qf_names[i] &&
  3976. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3977. kfree(old_opts.s_qf_names[i]);
  3978. #endif
  3979. unlock_super(sb);
  3980. if (enable_quota)
  3981. dquot_resume(sb, -1);
  3982. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  3983. kfree(orig_data);
  3984. return 0;
  3985. restore_opts:
  3986. sb->s_flags = old_sb_flags;
  3987. sbi->s_mount_opt = old_opts.s_mount_opt;
  3988. sbi->s_mount_opt2 = old_opts.s_mount_opt2;
  3989. sbi->s_resuid = old_opts.s_resuid;
  3990. sbi->s_resgid = old_opts.s_resgid;
  3991. sbi->s_commit_interval = old_opts.s_commit_interval;
  3992. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  3993. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  3994. #ifdef CONFIG_QUOTA
  3995. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  3996. for (i = 0; i < MAXQUOTAS; i++) {
  3997. if (sbi->s_qf_names[i] &&
  3998. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3999. kfree(sbi->s_qf_names[i]);
  4000. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  4001. }
  4002. #endif
  4003. unlock_super(sb);
  4004. kfree(orig_data);
  4005. return err;
  4006. }
  4007. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  4008. {
  4009. struct super_block *sb = dentry->d_sb;
  4010. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4011. struct ext4_super_block *es = sbi->s_es;
  4012. u64 fsid;
  4013. s64 bfree;
  4014. if (test_opt(sb, MINIX_DF)) {
  4015. sbi->s_overhead_last = 0;
  4016. } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
  4017. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  4018. ext4_fsblk_t overhead = 0;
  4019. /*
  4020. * Compute the overhead (FS structures). This is constant
  4021. * for a given filesystem unless the number of block groups
  4022. * changes so we cache the previous value until it does.
  4023. */
  4024. /*
  4025. * All of the blocks before first_data_block are
  4026. * overhead
  4027. */
  4028. overhead = le32_to_cpu(es->s_first_data_block);
  4029. /*
  4030. * Add the overhead attributed to the superblock and
  4031. * block group descriptors. If the sparse superblocks
  4032. * feature is turned on, then not all groups have this.
  4033. */
  4034. for (i = 0; i < ngroups; i++) {
  4035. overhead += ext4_bg_has_super(sb, i) +
  4036. ext4_bg_num_gdb(sb, i);
  4037. cond_resched();
  4038. }
  4039. /*
  4040. * Every block group has an inode bitmap, a block
  4041. * bitmap, and an inode table.
  4042. */
  4043. overhead += ngroups * (2 + sbi->s_itb_per_group);
  4044. sbi->s_overhead_last = overhead;
  4045. smp_wmb();
  4046. sbi->s_blocks_last = ext4_blocks_count(es);
  4047. }
  4048. buf->f_type = EXT4_SUPER_MAGIC;
  4049. buf->f_bsize = sb->s_blocksize;
  4050. buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
  4051. bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
  4052. percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
  4053. /* prevent underflow in case that few free space is available */
  4054. buf->f_bfree = max_t(s64, bfree, 0);
  4055. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  4056. if (buf->f_bfree < ext4_r_blocks_count(es))
  4057. buf->f_bavail = 0;
  4058. buf->f_files = le32_to_cpu(es->s_inodes_count);
  4059. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  4060. buf->f_namelen = EXT4_NAME_LEN;
  4061. fsid = le64_to_cpup((void *)es->s_uuid) ^
  4062. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  4063. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  4064. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  4065. return 0;
  4066. }
  4067. /* Helper function for writing quotas on sync - we need to start transaction
  4068. * before quota file is locked for write. Otherwise the are possible deadlocks:
  4069. * Process 1 Process 2
  4070. * ext4_create() quota_sync()
  4071. * jbd2_journal_start() write_dquot()
  4072. * dquot_initialize() down(dqio_mutex)
  4073. * down(dqio_mutex) jbd2_journal_start()
  4074. *
  4075. */
  4076. #ifdef CONFIG_QUOTA
  4077. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  4078. {
  4079. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  4080. }
  4081. static int ext4_write_dquot(struct dquot *dquot)
  4082. {
  4083. int ret, err;
  4084. handle_t *handle;
  4085. struct inode *inode;
  4086. inode = dquot_to_inode(dquot);
  4087. handle = ext4_journal_start(inode,
  4088. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  4089. if (IS_ERR(handle))
  4090. return PTR_ERR(handle);
  4091. ret = dquot_commit(dquot);
  4092. err = ext4_journal_stop(handle);
  4093. if (!ret)
  4094. ret = err;
  4095. return ret;
  4096. }
  4097. static int ext4_acquire_dquot(struct dquot *dquot)
  4098. {
  4099. int ret, err;
  4100. handle_t *handle;
  4101. handle = ext4_journal_start(dquot_to_inode(dquot),
  4102. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  4103. if (IS_ERR(handle))
  4104. return PTR_ERR(handle);
  4105. ret = dquot_acquire(dquot);
  4106. err = ext4_journal_stop(handle);
  4107. if (!ret)
  4108. ret = err;
  4109. return ret;
  4110. }
  4111. static int ext4_release_dquot(struct dquot *dquot)
  4112. {
  4113. int ret, err;
  4114. handle_t *handle;
  4115. handle = ext4_journal_start(dquot_to_inode(dquot),
  4116. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  4117. if (IS_ERR(handle)) {
  4118. /* Release dquot anyway to avoid endless cycle in dqput() */
  4119. dquot_release(dquot);
  4120. return PTR_ERR(handle);
  4121. }
  4122. ret = dquot_release(dquot);
  4123. err = ext4_journal_stop(handle);
  4124. if (!ret)
  4125. ret = err;
  4126. return ret;
  4127. }
  4128. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  4129. {
  4130. /* Are we journaling quotas? */
  4131. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  4132. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  4133. dquot_mark_dquot_dirty(dquot);
  4134. return ext4_write_dquot(dquot);
  4135. } else {
  4136. return dquot_mark_dquot_dirty(dquot);
  4137. }
  4138. }
  4139. static int ext4_write_info(struct super_block *sb, int type)
  4140. {
  4141. int ret, err;
  4142. handle_t *handle;
  4143. /* Data block + inode block */
  4144. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  4145. if (IS_ERR(handle))
  4146. return PTR_ERR(handle);
  4147. ret = dquot_commit_info(sb, type);
  4148. err = ext4_journal_stop(handle);
  4149. if (!ret)
  4150. ret = err;
  4151. return ret;
  4152. }
  4153. /*
  4154. * Turn on quotas during mount time - we need to find
  4155. * the quota file and such...
  4156. */
  4157. static int ext4_quota_on_mount(struct super_block *sb, int type)
  4158. {
  4159. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  4160. EXT4_SB(sb)->s_jquota_fmt, type);
  4161. }
  4162. /*
  4163. * Standard function to be called on quota_on
  4164. */
  4165. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  4166. struct path *path)
  4167. {
  4168. int err;
  4169. if (!test_opt(sb, QUOTA))
  4170. return -EINVAL;
  4171. /* Quotafile not on the same filesystem? */
  4172. if (path->mnt->mnt_sb != sb)
  4173. return -EXDEV;
  4174. /* Journaling quota? */
  4175. if (EXT4_SB(sb)->s_qf_names[type]) {
  4176. /* Quotafile not in fs root? */
  4177. if (path->dentry->d_parent != sb->s_root)
  4178. ext4_msg(sb, KERN_WARNING,
  4179. "Quota file not on filesystem root. "
  4180. "Journaled quota will not work");
  4181. }
  4182. /*
  4183. * When we journal data on quota file, we have to flush journal to see
  4184. * all updates to the file when we bypass pagecache...
  4185. */
  4186. if (EXT4_SB(sb)->s_journal &&
  4187. ext4_should_journal_data(path->dentry->d_inode)) {
  4188. /*
  4189. * We don't need to lock updates but journal_flush() could
  4190. * otherwise be livelocked...
  4191. */
  4192. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  4193. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  4194. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  4195. if (err)
  4196. return err;
  4197. }
  4198. return dquot_quota_on(sb, type, format_id, path);
  4199. }
  4200. static int ext4_quota_off(struct super_block *sb, int type)
  4201. {
  4202. struct inode *inode = sb_dqopt(sb)->files[type];
  4203. handle_t *handle;
  4204. /* Force all delayed allocation blocks to be allocated.
  4205. * Caller already holds s_umount sem */
  4206. if (test_opt(sb, DELALLOC))
  4207. sync_filesystem(sb);
  4208. if (!inode)
  4209. goto out;
  4210. /* Update modification times of quota files when userspace can
  4211. * start looking at them */
  4212. handle = ext4_journal_start(inode, 1);
  4213. if (IS_ERR(handle))
  4214. goto out;
  4215. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  4216. ext4_mark_inode_dirty(handle, inode);
  4217. ext4_journal_stop(handle);
  4218. out:
  4219. return dquot_quota_off(sb, type);
  4220. }
  4221. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  4222. * acquiring the locks... As quota files are never truncated and quota code
  4223. * itself serializes the operations (and no one else should touch the files)
  4224. * we don't have to be afraid of races */
  4225. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  4226. size_t len, loff_t off)
  4227. {
  4228. struct inode *inode = sb_dqopt(sb)->files[type];
  4229. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4230. int err = 0;
  4231. int offset = off & (sb->s_blocksize - 1);
  4232. int tocopy;
  4233. size_t toread;
  4234. struct buffer_head *bh;
  4235. loff_t i_size = i_size_read(inode);
  4236. if (off > i_size)
  4237. return 0;
  4238. if (off+len > i_size)
  4239. len = i_size-off;
  4240. toread = len;
  4241. while (toread > 0) {
  4242. tocopy = sb->s_blocksize - offset < toread ?
  4243. sb->s_blocksize - offset : toread;
  4244. bh = ext4_bread(NULL, inode, blk, 0, &err);
  4245. if (err)
  4246. return err;
  4247. if (!bh) /* A hole? */
  4248. memset(data, 0, tocopy);
  4249. else
  4250. memcpy(data, bh->b_data+offset, tocopy);
  4251. brelse(bh);
  4252. offset = 0;
  4253. toread -= tocopy;
  4254. data += tocopy;
  4255. blk++;
  4256. }
  4257. return len;
  4258. }
  4259. /* Write to quotafile (we know the transaction is already started and has
  4260. * enough credits) */
  4261. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  4262. const char *data, size_t len, loff_t off)
  4263. {
  4264. struct inode *inode = sb_dqopt(sb)->files[type];
  4265. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4266. int err = 0;
  4267. int offset = off & (sb->s_blocksize - 1);
  4268. struct buffer_head *bh;
  4269. handle_t *handle = journal_current_handle();
  4270. if (EXT4_SB(sb)->s_journal && !handle) {
  4271. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4272. " cancelled because transaction is not started",
  4273. (unsigned long long)off, (unsigned long long)len);
  4274. return -EIO;
  4275. }
  4276. /*
  4277. * Since we account only one data block in transaction credits,
  4278. * then it is impossible to cross a block boundary.
  4279. */
  4280. if (sb->s_blocksize - offset < len) {
  4281. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4282. " cancelled because not block aligned",
  4283. (unsigned long long)off, (unsigned long long)len);
  4284. return -EIO;
  4285. }
  4286. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  4287. bh = ext4_bread(handle, inode, blk, 1, &err);
  4288. if (!bh)
  4289. goto out;
  4290. err = ext4_journal_get_write_access(handle, bh);
  4291. if (err) {
  4292. brelse(bh);
  4293. goto out;
  4294. }
  4295. lock_buffer(bh);
  4296. memcpy(bh->b_data+offset, data, len);
  4297. flush_dcache_page(bh->b_page);
  4298. unlock_buffer(bh);
  4299. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  4300. brelse(bh);
  4301. out:
  4302. if (err) {
  4303. mutex_unlock(&inode->i_mutex);
  4304. return err;
  4305. }
  4306. if (inode->i_size < off + len) {
  4307. i_size_write(inode, off + len);
  4308. EXT4_I(inode)->i_disksize = inode->i_size;
  4309. ext4_mark_inode_dirty(handle, inode);
  4310. }
  4311. mutex_unlock(&inode->i_mutex);
  4312. return len;
  4313. }
  4314. #endif
  4315. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  4316. const char *dev_name, void *data)
  4317. {
  4318. return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
  4319. }
  4320. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4321. static inline void register_as_ext2(void)
  4322. {
  4323. int err = register_filesystem(&ext2_fs_type);
  4324. if (err)
  4325. printk(KERN_WARNING
  4326. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  4327. }
  4328. static inline void unregister_as_ext2(void)
  4329. {
  4330. unregister_filesystem(&ext2_fs_type);
  4331. }
  4332. static inline int ext2_feature_set_ok(struct super_block *sb)
  4333. {
  4334. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
  4335. return 0;
  4336. if (sb->s_flags & MS_RDONLY)
  4337. return 1;
  4338. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
  4339. return 0;
  4340. return 1;
  4341. }
  4342. MODULE_ALIAS("ext2");
  4343. #else
  4344. static inline void register_as_ext2(void) { }
  4345. static inline void unregister_as_ext2(void) { }
  4346. static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
  4347. #endif
  4348. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4349. static inline void register_as_ext3(void)
  4350. {
  4351. int err = register_filesystem(&ext3_fs_type);
  4352. if (err)
  4353. printk(KERN_WARNING
  4354. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  4355. }
  4356. static inline void unregister_as_ext3(void)
  4357. {
  4358. unregister_filesystem(&ext3_fs_type);
  4359. }
  4360. static inline int ext3_feature_set_ok(struct super_block *sb)
  4361. {
  4362. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
  4363. return 0;
  4364. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  4365. return 0;
  4366. if (sb->s_flags & MS_RDONLY)
  4367. return 1;
  4368. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
  4369. return 0;
  4370. return 1;
  4371. }
  4372. MODULE_ALIAS("ext3");
  4373. #else
  4374. static inline void register_as_ext3(void) { }
  4375. static inline void unregister_as_ext3(void) { }
  4376. static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
  4377. #endif
  4378. static struct file_system_type ext4_fs_type = {
  4379. .owner = THIS_MODULE,
  4380. .name = "ext4",
  4381. .mount = ext4_mount,
  4382. .kill_sb = kill_block_super,
  4383. .fs_flags = FS_REQUIRES_DEV,
  4384. };
  4385. static int __init ext4_init_feat_adverts(void)
  4386. {
  4387. struct ext4_features *ef;
  4388. int ret = -ENOMEM;
  4389. ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
  4390. if (!ef)
  4391. goto out;
  4392. ef->f_kobj.kset = ext4_kset;
  4393. init_completion(&ef->f_kobj_unregister);
  4394. ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
  4395. "features");
  4396. if (ret) {
  4397. kfree(ef);
  4398. goto out;
  4399. }
  4400. ext4_feat = ef;
  4401. ret = 0;
  4402. out:
  4403. return ret;
  4404. }
  4405. static void ext4_exit_feat_adverts(void)
  4406. {
  4407. kobject_put(&ext4_feat->f_kobj);
  4408. wait_for_completion(&ext4_feat->f_kobj_unregister);
  4409. kfree(ext4_feat);
  4410. }
  4411. /* Shared across all ext4 file systems */
  4412. wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
  4413. struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
  4414. static int __init ext4_init_fs(void)
  4415. {
  4416. int i, err;
  4417. ext4_check_flag_values();
  4418. for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
  4419. mutex_init(&ext4__aio_mutex[i]);
  4420. init_waitqueue_head(&ext4__ioend_wq[i]);
  4421. }
  4422. err = ext4_init_pageio();
  4423. if (err)
  4424. return err;
  4425. err = ext4_init_system_zone();
  4426. if (err)
  4427. goto out7;
  4428. ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
  4429. if (!ext4_kset)
  4430. goto out6;
  4431. ext4_proc_root = proc_mkdir("fs/ext4", NULL);
  4432. if (!ext4_proc_root)
  4433. goto out5;
  4434. err = ext4_init_feat_adverts();
  4435. if (err)
  4436. goto out4;
  4437. err = ext4_init_mballoc();
  4438. if (err)
  4439. goto out3;
  4440. err = ext4_init_xattr();
  4441. if (err)
  4442. goto out2;
  4443. err = init_inodecache();
  4444. if (err)
  4445. goto out1;
  4446. register_as_ext3();
  4447. register_as_ext2();
  4448. err = register_filesystem(&ext4_fs_type);
  4449. if (err)
  4450. goto out;
  4451. ext4_li_info = NULL;
  4452. mutex_init(&ext4_li_mtx);
  4453. return 0;
  4454. out:
  4455. unregister_as_ext2();
  4456. unregister_as_ext3();
  4457. destroy_inodecache();
  4458. out1:
  4459. ext4_exit_xattr();
  4460. out2:
  4461. ext4_exit_mballoc();
  4462. out3:
  4463. ext4_exit_feat_adverts();
  4464. out4:
  4465. remove_proc_entry("fs/ext4", NULL);
  4466. out5:
  4467. kset_unregister(ext4_kset);
  4468. out6:
  4469. ext4_exit_system_zone();
  4470. out7:
  4471. ext4_exit_pageio();
  4472. return err;
  4473. }
  4474. static void __exit ext4_exit_fs(void)
  4475. {
  4476. ext4_destroy_lazyinit_thread();
  4477. unregister_as_ext2();
  4478. unregister_as_ext3();
  4479. unregister_filesystem(&ext4_fs_type);
  4480. destroy_inodecache();
  4481. ext4_exit_xattr();
  4482. ext4_exit_mballoc();
  4483. ext4_exit_feat_adverts();
  4484. remove_proc_entry("fs/ext4", NULL);
  4485. kset_unregister(ext4_kset);
  4486. ext4_exit_system_zone();
  4487. ext4_exit_pageio();
  4488. }
  4489. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  4490. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  4491. MODULE_LICENSE("GPL");
  4492. module_init(ext4_init_fs)
  4493. module_exit(ext4_exit_fs)