forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/fs/jbd2/journal.c
....@@ -66,9 +66,6 @@
6666 EXPORT_SYMBOL(jbd2_journal_set_triggers);
6767 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
6868 EXPORT_SYMBOL(jbd2_journal_forget);
69
-#if 0
70
-EXPORT_SYMBOL(journal_sync_buffer);
71
-#endif
7269 EXPORT_SYMBOL(jbd2_journal_flush);
7370 EXPORT_SYMBOL(jbd2_journal_revoke);
7471
....@@ -92,16 +89,15 @@
9289 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
9390 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
9491 EXPORT_SYMBOL(jbd2_journal_force_commit);
95
-EXPORT_SYMBOL(jbd2_journal_inode_add_write);
96
-EXPORT_SYMBOL(jbd2_journal_inode_add_wait);
9792 EXPORT_SYMBOL(jbd2_journal_inode_ranged_write);
9893 EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait);
94
+EXPORT_SYMBOL(jbd2_journal_submit_inode_data_buffers);
95
+EXPORT_SYMBOL(jbd2_journal_finish_inode_data_buffers);
9996 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
10097 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
10198 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
10299 EXPORT_SYMBOL(jbd2_inode_cache);
103100
104
-static void __journal_abort_soft (journal_t *journal, int errno);
105101 static int jbd2_journal_create_slab(size_t slab_size);
106102
107103 #ifdef CONFIG_JBD2_DEBUG
....@@ -144,22 +140,6 @@
144140 return cpu_to_be32(csum);
145141 }
146142
147
-static int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
148
-{
149
- if (!jbd2_journal_has_csum_v2or3(j))
150
- return 1;
151
-
152
- return sb->s_checksum == jbd2_superblock_csum(j, sb);
153
-}
154
-
155
-static void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
156
-{
157
- if (!jbd2_journal_has_csum_v2or3(j))
158
- return;
159
-
160
- sb->s_checksum = jbd2_superblock_csum(j, sb);
161
-}
162
-
163143 /*
164144 * Helper function used to manage commit timeouts
165145 */
....@@ -179,7 +159,9 @@
179159 *
180160 * 1) COMMIT: Every so often we need to commit the current state of the
181161 * filesystem to disk. The journal thread is responsible for writing
182
- * all of the metadata buffers to disk.
162
+ * all of the metadata buffers to disk. If a fast commit is ongoing
163
+ * journal thread waits until it's done and then continues from
164
+ * there on.
183165 *
184166 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
185167 * of the data in that part of the log has been rewritten elsewhere on
....@@ -221,7 +203,7 @@
221203 if (journal->j_flags & JBD2_UNMOUNT)
222204 goto end_loop;
223205
224
- jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
206
+ jbd_debug(1, "commit_sequence=%u, commit_request=%u\n",
225207 journal->j_commit_sequence, journal->j_commit_request);
226208
227209 if (journal->j_commit_sequence != journal->j_commit_request) {
....@@ -342,7 +324,7 @@
342324 * IO is in progress. do_get_write_access() handles this.
343325 *
344326 * The function returns a pointer to the buffer_head to be used for IO.
345
- *
327
+ *
346328 *
347329 * Return value:
348330 * <0: Error
....@@ -384,7 +366,7 @@
384366 /* keep subsequent assertions sane */
385367 atomic_set(&new_bh->b_count, 1);
386368
387
- jbd_lock_bh_state(bh_in);
369
+ spin_lock(&jh_in->b_state_lock);
388370 repeat:
389371 /*
390372 * If a new transaction has already done a buffer copy-out, then
....@@ -426,13 +408,13 @@
426408 if (need_copy_out && !done_copy_out) {
427409 char *tmp;
428410
429
- jbd_unlock_bh_state(bh_in);
411
+ spin_unlock(&jh_in->b_state_lock);
430412 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
431413 if (!tmp) {
432414 brelse(new_bh);
433415 return -ENOMEM;
434416 }
435
- jbd_lock_bh_state(bh_in);
417
+ spin_lock(&jh_in->b_state_lock);
436418 if (jh_in->b_frozen_data) {
437419 jbd2_free(tmp, bh_in->b_size);
438420 goto repeat;
....@@ -485,7 +467,7 @@
485467 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
486468 spin_unlock(&journal->j_list_lock);
487469 set_buffer_shadow(bh_in);
488
- jbd_unlock_bh_state(bh_in);
470
+ spin_unlock(&jh_in->b_state_lock);
489471
490472 return do_escape | (done_copy_out << 1);
491473 }
....@@ -518,7 +500,7 @@
518500 */
519501
520502 journal->j_commit_request = target;
521
- jbd_debug(1, "JBD2: requesting commit %d/%d\n",
503
+ jbd_debug(1, "JBD2: requesting commit %u/%u\n",
522504 journal->j_commit_request,
523505 journal->j_commit_sequence);
524506 journal->j_running_transaction->t_requested = jiffies;
....@@ -531,7 +513,7 @@
531513 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
532514 journal->j_commit_request,
533515 journal->j_commit_sequence,
534
- target, journal->j_running_transaction ?
516
+ target, journal->j_running_transaction ?
535517 journal->j_running_transaction->t_tid : 0);
536518 return 0;
537519 }
....@@ -584,12 +566,14 @@
584566 }
585567
586568 /**
587
- * Force and wait upon a commit if the calling process is not within
588
- * transaction. This is used for forcing out undo-protected data which contains
589
- * bitmaps, when the fs is running out of space.
569
+ * jbd2_journal_force_commit_nested - Force and wait upon a commit if the
570
+ * calling process is not within transaction.
590571 *
591572 * @journal: journal to force
592573 * Returns true if progress was made.
574
+ *
575
+ * This is used for forcing out undo-protected data which contains
576
+ * bitmaps, when the fs is running out of space.
593577 */
594578 int jbd2_journal_force_commit_nested(journal_t *journal)
595579 {
....@@ -600,7 +584,7 @@
600584 }
601585
602586 /**
603
- * int journal_force_commit() - force any uncommitted transactions
587
+ * jbd2_journal_force_commit() - force any uncommitted transactions
604588 * @journal: journal to force
605589 *
606590 * Caller want unconditional commit. We can only force the running transaction
....@@ -716,12 +700,12 @@
716700 #ifdef CONFIG_JBD2_DEBUG
717701 if (!tid_geq(journal->j_commit_request, tid)) {
718702 printk(KERN_ERR
719
- "%s: error: j_commit_request=%d, tid=%d\n",
703
+ "%s: error: j_commit_request=%u, tid=%u\n",
720704 __func__, journal->j_commit_request, tid);
721705 }
722706 #endif
723707 while (tid_gt(tid, journal->j_commit_sequence)) {
724
- jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
708
+ jbd_debug(1, "JBD2: want %u, j_commit_sequence=%u\n",
725709 tid, journal->j_commit_sequence);
726710 read_unlock(&journal->j_state_lock);
727711 wake_up(&journal->j_wait_commit);
....@@ -735,6 +719,85 @@
735719 err = -EIO;
736720 return err;
737721 }
722
+
723
+/*
724
+ * Start a fast commit. If there's an ongoing fast or full commit wait for
725
+ * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY
726
+ * if a fast commit is not needed, either because there's an already a commit
727
+ * going on or this tid has already been committed. Returns -EINVAL if no jbd2
728
+ * commit has yet been performed.
729
+ */
730
+int jbd2_fc_begin_commit(journal_t *journal, tid_t tid)
731
+{
732
+ if (unlikely(is_journal_aborted(journal)))
733
+ return -EIO;
734
+ /*
735
+ * Fast commits only allowed if at least one full commit has
736
+ * been processed.
737
+ */
738
+ if (!journal->j_stats.ts_tid)
739
+ return -EINVAL;
740
+
741
+ write_lock(&journal->j_state_lock);
742
+ if (tid <= journal->j_commit_sequence) {
743
+ write_unlock(&journal->j_state_lock);
744
+ return -EALREADY;
745
+ }
746
+
747
+ if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
748
+ (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) {
749
+ DEFINE_WAIT(wait);
750
+
751
+ prepare_to_wait(&journal->j_fc_wait, &wait,
752
+ TASK_UNINTERRUPTIBLE);
753
+ write_unlock(&journal->j_state_lock);
754
+ schedule();
755
+ finish_wait(&journal->j_fc_wait, &wait);
756
+ return -EALREADY;
757
+ }
758
+ journal->j_flags |= JBD2_FAST_COMMIT_ONGOING;
759
+ write_unlock(&journal->j_state_lock);
760
+
761
+ return 0;
762
+}
763
+EXPORT_SYMBOL(jbd2_fc_begin_commit);
764
+
765
+/*
766
+ * Stop a fast commit. If fallback is set, this function starts commit of
767
+ * TID tid before any other fast commit can start.
768
+ */
769
+static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback)
770
+{
771
+ if (journal->j_fc_cleanup_callback)
772
+ journal->j_fc_cleanup_callback(journal, 0);
773
+ write_lock(&journal->j_state_lock);
774
+ journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
775
+ if (fallback)
776
+ journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
777
+ write_unlock(&journal->j_state_lock);
778
+ wake_up(&journal->j_fc_wait);
779
+ if (fallback)
780
+ return jbd2_complete_transaction(journal, tid);
781
+ return 0;
782
+}
783
+
784
+int jbd2_fc_end_commit(journal_t *journal)
785
+{
786
+ return __jbd2_fc_end_commit(journal, 0, false);
787
+}
788
+EXPORT_SYMBOL(jbd2_fc_end_commit);
789
+
790
+int jbd2_fc_end_commit_fallback(journal_t *journal)
791
+{
792
+ tid_t tid;
793
+
794
+ read_lock(&journal->j_state_lock);
795
+ tid = journal->j_running_transaction ?
796
+ journal->j_running_transaction->t_tid : 0;
797
+ read_unlock(&journal->j_state_lock);
798
+ return __jbd2_fc_end_commit(journal, tid, true);
799
+}
800
+EXPORT_SYMBOL(jbd2_fc_end_commit_fallback);
738801
739802 /* Return 1 when transaction with given tid has already committed. */
740803 int jbd2_transaction_committed(journal_t *journal, tid_t tid)
....@@ -804,6 +867,106 @@
804867 return jbd2_journal_bmap(journal, blocknr, retp);
805868 }
806869
870
+/* Map one fast commit buffer for use by the file system */
871
+int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out)
872
+{
873
+ unsigned long long pblock;
874
+ unsigned long blocknr;
875
+ int ret = 0;
876
+ struct buffer_head *bh;
877
+ int fc_off;
878
+
879
+ *bh_out = NULL;
880
+
881
+ if (journal->j_fc_off + journal->j_fc_first < journal->j_fc_last) {
882
+ fc_off = journal->j_fc_off;
883
+ blocknr = journal->j_fc_first + fc_off;
884
+ journal->j_fc_off++;
885
+ } else {
886
+ ret = -EINVAL;
887
+ }
888
+
889
+ if (ret)
890
+ return ret;
891
+
892
+ ret = jbd2_journal_bmap(journal, blocknr, &pblock);
893
+ if (ret)
894
+ return ret;
895
+
896
+ bh = __getblk(journal->j_dev, pblock, journal->j_blocksize);
897
+ if (!bh)
898
+ return -ENOMEM;
899
+
900
+
901
+ journal->j_fc_wbuf[fc_off] = bh;
902
+
903
+ *bh_out = bh;
904
+
905
+ return 0;
906
+}
907
+EXPORT_SYMBOL(jbd2_fc_get_buf);
908
+
909
+/*
910
+ * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf
911
+ * for completion.
912
+ */
913
+int jbd2_fc_wait_bufs(journal_t *journal, int num_blks)
914
+{
915
+ struct buffer_head *bh;
916
+ int i, j_fc_off;
917
+
918
+ j_fc_off = journal->j_fc_off;
919
+
920
+ /*
921
+ * Wait in reverse order to minimize chances of us being woken up before
922
+ * all IOs have completed
923
+ */
924
+ for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
925
+ bh = journal->j_fc_wbuf[i];
926
+ wait_on_buffer(bh);
927
+ /*
928
+ * Update j_fc_off so jbd2_fc_release_bufs can release remain
929
+ * buffer head.
930
+ */
931
+ if (unlikely(!buffer_uptodate(bh))) {
932
+ journal->j_fc_off = i + 1;
933
+ return -EIO;
934
+ }
935
+ put_bh(bh);
936
+ journal->j_fc_wbuf[i] = NULL;
937
+ }
938
+
939
+ return 0;
940
+}
941
+EXPORT_SYMBOL(jbd2_fc_wait_bufs);
942
+
943
+/*
944
+ * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf
945
+ * for completion.
946
+ */
947
+int jbd2_fc_release_bufs(journal_t *journal)
948
+{
949
+ struct buffer_head *bh;
950
+ int i, j_fc_off;
951
+
952
+ j_fc_off = journal->j_fc_off;
953
+
954
+ /*
955
+ * Wait in reverse order to minimize chances of us being woken up before
956
+ * all IOs have completed
957
+ */
958
+ for (i = j_fc_off - 1; i >= 0; i--) {
959
+ bh = journal->j_fc_wbuf[i];
960
+ if (!bh)
961
+ break;
962
+ put_bh(bh);
963
+ journal->j_fc_wbuf[i] = NULL;
964
+ }
965
+
966
+ return 0;
967
+}
968
+EXPORT_SYMBOL(jbd2_fc_release_bufs);
969
+
807970 /*
808971 * Conversion of logical to physical block numbers for the journal
809972 *
....@@ -816,18 +979,22 @@
816979 {
817980 int err = 0;
818981 unsigned long long ret;
982
+ sector_t block = 0;
819983
820984 if (journal->j_inode) {
821
- ret = bmap(journal->j_inode, blocknr);
822
- if (ret)
823
- *retp = ret;
824
- else {
985
+ block = blocknr;
986
+ ret = bmap(journal->j_inode, &block);
987
+
988
+ if (ret || !block) {
825989 printk(KERN_ALERT "%s: journal block not found "
826990 "at offset %lu on %s\n",
827991 __func__, blocknr, journal->j_devname);
828992 err = -EIO;
829
- __journal_abort_soft(journal, err);
993
+ jbd2_journal_abort(journal, err);
994
+ } else {
995
+ *retp = block;
830996 }
997
+
831998 } else {
832999 *retp = blocknr; /* +journal->j_blk_offset */
8331000 }
....@@ -861,6 +1028,7 @@
8611028 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
8621029 if (!bh)
8631030 return NULL;
1031
+ atomic_dec(&transaction->t_outstanding_credits);
8641032 lock_buffer(bh);
8651033 memset(bh->b_data, 0, journal->j_blocksize);
8661034 header = (journal_header_t *)bh->b_data;
....@@ -962,7 +1130,7 @@
9621130
9631131 trace_jbd2_update_log_tail(journal, tid, block, freed);
9641132 jbd_debug(1,
965
- "Cleaning journal tail from %d to %d (offset %lu), "
1133
+ "Cleaning journal tail from %u to %u (offset %lu), "
9661134 "freeing %lu\n",
9671135 journal->j_tail_sequence, tid, block, freed);
9681136
....@@ -1095,12 +1263,11 @@
10951263 return seq_release(inode, file);
10961264 }
10971265
1098
-static const struct file_operations jbd2_seq_info_fops = {
1099
- .owner = THIS_MODULE,
1100
- .open = jbd2_seq_info_open,
1101
- .read = seq_read,
1102
- .llseek = seq_lseek,
1103
- .release = jbd2_seq_info_release,
1266
+static const struct proc_ops jbd2_info_proc_ops = {
1267
+ .proc_open = jbd2_seq_info_open,
1268
+ .proc_read = seq_read,
1269
+ .proc_lseek = seq_lseek,
1270
+ .proc_release = jbd2_seq_info_release,
11041271 };
11051272
11061273 static struct proc_dir_entry *proc_jbd2_stats;
....@@ -1110,7 +1277,7 @@
11101277 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
11111278 if (journal->j_proc_entry) {
11121279 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1113
- &jbd2_seq_info_fops, journal);
1280
+ &jbd2_info_proc_ops, journal);
11141281 }
11151282 }
11161283
....@@ -1118,6 +1285,16 @@
11181285 {
11191286 remove_proc_entry("info", journal->j_proc_entry);
11201287 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
1288
+}
1289
+
1290
+/* Minimum size of descriptor tag */
1291
+static int jbd2_min_tag_size(void)
1292
+{
1293
+ /*
1294
+ * Tag with 32-bit block numbers does not use last four bytes of the
1295
+ * structure
1296
+ */
1297
+ return sizeof(journal_block_tag_t) - 4;
11211298 }
11221299
11231300 /*
....@@ -1148,6 +1325,8 @@
11481325 init_waitqueue_head(&journal->j_wait_commit);
11491326 init_waitqueue_head(&journal->j_wait_updates);
11501327 init_waitqueue_head(&journal->j_wait_reserved);
1328
+ init_waitqueue_head(&journal->j_fc_wait);
1329
+ mutex_init(&journal->j_abort_mutex);
11511330 mutex_init(&journal->j_barrier);
11521331 mutex_init(&journal->j_checkpoint_mutex);
11531332 spin_lock_init(&journal->j_revoke_lock);
....@@ -1177,9 +1356,11 @@
11771356 journal->j_dev = bdev;
11781357 journal->j_fs_dev = fs_dev;
11791358 journal->j_blk_offset = start;
1180
- journal->j_maxlen = len;
1181
- n = journal->j_blocksize / sizeof(journal_block_tag_t);
1359
+ journal->j_total_len = len;
1360
+ /* We need enough buffers to write out full descriptor block. */
1361
+ n = journal->j_blocksize / jbd2_min_tag_size();
11821362 journal->j_wbufsize = n;
1363
+ journal->j_fc_wbuf = NULL;
11831364 journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
11841365 GFP_KERNEL);
11851366 if (!journal->j_wbuf)
....@@ -1254,11 +1435,14 @@
12541435 journal_t *jbd2_journal_init_inode(struct inode *inode)
12551436 {
12561437 journal_t *journal;
1438
+ sector_t blocknr;
12571439 char *p;
1258
- unsigned long long blocknr;
1440
+ int err = 0;
12591441
1260
- blocknr = bmap(inode, 0);
1261
- if (!blocknr) {
1442
+ blocknr = 0;
1443
+ err = bmap(inode, &blocknr);
1444
+
1445
+ if (err || !blocknr) {
12621446 pr_err("%s: Cannot locate journal superblock\n",
12631447 __func__);
12641448 return NULL;
....@@ -1288,7 +1472,7 @@
12881472 * superblock as being NULL to prevent the journal destroy from writing
12891473 * back a bogus superblock.
12901474 */
1291
-static void journal_fail_superblock (journal_t *journal)
1475
+static void journal_fail_superblock(journal_t *journal)
12921476 {
12931477 struct buffer_head *bh = journal->j_sb_buffer;
12941478 brelse(bh);
....@@ -1319,15 +1503,22 @@
13191503 journal->j_first = first;
13201504 journal->j_last = last;
13211505
1322
- journal->j_head = first;
1323
- journal->j_tail = first;
1324
- journal->j_free = last - first;
1506
+ journal->j_head = journal->j_first;
1507
+ journal->j_tail = journal->j_first;
1508
+ journal->j_free = journal->j_last - journal->j_first;
13251509
13261510 journal->j_tail_sequence = journal->j_transaction_sequence;
13271511 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
13281512 journal->j_commit_request = journal->j_commit_sequence;
13291513
1330
- journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1514
+ journal->j_max_transaction_buffers = jbd2_journal_get_max_txn_bufs(journal);
1515
+
1516
+ /*
1517
+ * Now that journal recovery is done, turn fast commits off here. This
1518
+ * way, if fast commit was enabled before the crash but if now FS has
1519
+ * disabled it, we don't enable fast commits.
1520
+ */
1521
+ jbd2_clear_feature_fast_commit(journal);
13311522
13321523 /*
13331524 * As a special case, if the on-disk copy is already marked as needing
....@@ -1337,7 +1528,7 @@
13371528 */
13381529 if (sb->s_start == 0) {
13391530 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1340
- "(start %ld, seq %d, errno %d)\n",
1531
+ "(start %ld, seq %u, errno %d)\n",
13411532 journal->j_tail, journal->j_tail_sequence,
13421533 journal->j_errno);
13431534 journal->j_flags |= JBD2_FLUSHED;
....@@ -1393,7 +1584,8 @@
13931584 clear_buffer_write_io_error(bh);
13941585 set_buffer_uptodate(bh);
13951586 }
1396
- jbd2_superblock_csum_set(journal, sb);
1587
+ if (jbd2_journal_has_csum_v2or3(journal))
1588
+ sb->s_checksum = jbd2_superblock_csum(journal, sb);
13971589 get_bh(bh);
13981590 bh->b_end_io = end_buffer_write_sync;
13991591 ret = submit_bh(REQ_OP_WRITE, write_flags, bh);
....@@ -1407,7 +1599,8 @@
14071599 printk(KERN_ERR "JBD2: Error %d detected when updating "
14081600 "journal superblock for %s.\n", ret,
14091601 journal->j_devname);
1410
- jbd2_journal_abort(journal, ret);
1602
+ if (!is_journal_aborted(journal))
1603
+ jbd2_journal_abort(journal, ret);
14111604 }
14121605
14131606 return ret;
....@@ -1465,6 +1658,7 @@
14651658 static void jbd2_mark_journal_empty(journal_t *journal, int write_op)
14661659 {
14671660 journal_superblock_t *sb = journal->j_superblock;
1661
+ bool had_fast_commit = false;
14681662
14691663 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
14701664 lock_buffer(journal->j_sb_buffer);
....@@ -1473,13 +1667,24 @@
14731667 return;
14741668 }
14751669
1476
- jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1670
+ jbd_debug(1, "JBD2: Marking journal as empty (seq %u)\n",
14771671 journal->j_tail_sequence);
14781672
14791673 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
14801674 sb->s_start = cpu_to_be32(0);
1675
+ if (jbd2_has_feature_fast_commit(journal)) {
1676
+ /*
1677
+ * When journal is clean, no need to commit fast commit flag and
1678
+ * make file system incompatible with older kernels.
1679
+ */
1680
+ jbd2_clear_feature_fast_commit(journal);
1681
+ had_fast_commit = true;
1682
+ }
14811683
14821684 jbd2_write_superblock(journal, write_op);
1685
+
1686
+ if (had_fast_commit)
1687
+ jbd2_set_feature_fast_commit(journal);
14831688
14841689 /* Log is no longer empty */
14851690 write_lock(&journal->j_state_lock);
....@@ -1510,6 +1715,21 @@
15101715 jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA);
15111716 }
15121717 EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1718
+
1719
+static int journal_revoke_records_per_block(journal_t *journal)
1720
+{
1721
+ int record_size;
1722
+ int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t);
1723
+
1724
+ if (jbd2_has_feature_64bit(journal))
1725
+ record_size = 8;
1726
+ else
1727
+ record_size = 4;
1728
+
1729
+ if (jbd2_journal_has_csum_v2or3(journal))
1730
+ space -= sizeof(struct jbd2_journal_block_tail);
1731
+ return space / record_size;
1732
+}
15131733
15141734 /*
15151735 * Read the superblock for a given journal, performing initial
....@@ -1559,15 +1779,15 @@
15591779 goto out;
15601780 }
15611781
1562
- if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1563
- journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1564
- else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1782
+ if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len)
1783
+ journal->j_total_len = be32_to_cpu(sb->s_maxlen);
1784
+ else if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) {
15651785 printk(KERN_WARNING "JBD2: journal file too short\n");
15661786 goto out;
15671787 }
15681788
15691789 if (be32_to_cpu(sb->s_first) == 0 ||
1570
- be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1790
+ be32_to_cpu(sb->s_first) >= journal->j_total_len) {
15711791 printk(KERN_WARNING
15721792 "JBD2: Invalid start block of journal: %u\n",
15731793 be32_to_cpu(sb->s_first));
....@@ -1606,18 +1826,21 @@
16061826 }
16071827 }
16081828
1609
- /* Check superblock checksum */
1610
- if (!jbd2_superblock_csum_verify(journal, sb)) {
1611
- printk(KERN_ERR "JBD2: journal checksum error\n");
1612
- err = -EFSBADCRC;
1613
- goto out;
1614
- }
1829
+ if (jbd2_journal_has_csum_v2or3(journal)) {
1830
+ /* Check superblock checksum */
1831
+ if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) {
1832
+ printk(KERN_ERR "JBD2: journal checksum error\n");
1833
+ err = -EFSBADCRC;
1834
+ goto out;
1835
+ }
16151836
1616
- /* Precompute checksum seed for all metadata */
1617
- if (jbd2_journal_has_csum_v2or3(journal))
1837
+ /* Precompute checksum seed for all metadata */
16181838 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
16191839 sizeof(sb->s_uuid));
1840
+ }
16201841
1842
+ journal->j_revoke_records_per_block =
1843
+ journal_revoke_records_per_block(journal);
16211844 set_buffer_verified(bh);
16221845
16231846 return 0;
....@@ -1636,6 +1859,7 @@
16361859 {
16371860 int err;
16381861 journal_superblock_t *sb;
1862
+ int num_fc_blocks;
16391863
16401864 err = journal_get_superblock(journal);
16411865 if (err)
....@@ -1646,15 +1870,26 @@
16461870 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
16471871 journal->j_tail = be32_to_cpu(sb->s_start);
16481872 journal->j_first = be32_to_cpu(sb->s_first);
1649
- journal->j_last = be32_to_cpu(sb->s_maxlen);
16501873 journal->j_errno = be32_to_cpu(sb->s_errno);
1874
+ journal->j_last = be32_to_cpu(sb->s_maxlen);
1875
+
1876
+ if (jbd2_has_feature_fast_commit(journal)) {
1877
+ journal->j_fc_last = be32_to_cpu(sb->s_maxlen);
1878
+ num_fc_blocks = be32_to_cpu(sb->s_num_fc_blks);
1879
+ if (!num_fc_blocks)
1880
+ num_fc_blocks = JBD2_MIN_FC_BLOCKS;
1881
+ if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS)
1882
+ journal->j_last = journal->j_fc_last - num_fc_blocks;
1883
+ journal->j_fc_first = journal->j_last + 1;
1884
+ journal->j_fc_off = 0;
1885
+ }
16511886
16521887 return 0;
16531888 }
16541889
16551890
16561891 /**
1657
- * int jbd2_journal_load() - Read journal from disk.
1892
+ * jbd2_journal_load() - Read journal from disk.
16581893 * @journal: Journal to act on.
16591894 *
16601895 * Given a journal_t structure which tells us which disk blocks contain
....@@ -1724,7 +1959,7 @@
17241959 }
17251960
17261961 /**
1727
- * void jbd2_journal_destroy() - Release a journal_t structure.
1962
+ * jbd2_journal_destroy() - Release a journal_t structure.
17281963 * @journal: Journal to act on.
17291964 *
17301965 * Release a journal_t structure once it is no longer in use by the
....@@ -1792,6 +2027,7 @@
17922027 jbd2_journal_destroy_revoke(journal);
17932028 if (journal->j_chksum_driver)
17942029 crypto_free_shash(journal->j_chksum_driver);
2030
+ kfree(journal->j_fc_wbuf);
17952031 kfree(journal->j_wbuf);
17962032 kfree(journal);
17972033
....@@ -1800,7 +2036,7 @@
18002036
18012037
18022038 /**
1803
- *int jbd2_journal_check_used_features () - Check if features specified are used.
2039
+ * jbd2_journal_check_used_features() - Check if features specified are used.
18042040 * @journal: Journal to check.
18052041 * @compat: bitmask of compatible features
18062042 * @ro: bitmask of features that force read-only mount
....@@ -1810,7 +2046,7 @@
18102046 * features. Return true (non-zero) if it does.
18112047 **/
18122048
1813
-int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
2049
+int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat,
18142050 unsigned long ro, unsigned long incompat)
18152051 {
18162052 journal_superblock_t *sb;
....@@ -1835,7 +2071,7 @@
18352071 }
18362072
18372073 /**
1838
- * int jbd2_journal_check_available_features() - Check feature set in journalling layer
2074
+ * jbd2_journal_check_available_features() - Check feature set in journalling layer
18392075 * @journal: Journal to check.
18402076 * @compat: bitmask of compatible features
18412077 * @ro: bitmask of features that force read-only mount
....@@ -1845,7 +2081,7 @@
18452081 * all of a given set of features on this journal. Return true
18462082 * (non-zero) if it can. */
18472083
1848
-int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
2084
+int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat,
18492085 unsigned long ro, unsigned long incompat)
18502086 {
18512087 if (!compat && !ro && !incompat)
....@@ -1866,8 +2102,39 @@
18662102 return 0;
18672103 }
18682104
2105
+static int
2106
+jbd2_journal_initialize_fast_commit(journal_t *journal)
2107
+{
2108
+ journal_superblock_t *sb = journal->j_superblock;
2109
+ unsigned long long num_fc_blks;
2110
+
2111
+ num_fc_blks = be32_to_cpu(sb->s_num_fc_blks);
2112
+ if (num_fc_blks == 0)
2113
+ num_fc_blks = JBD2_MIN_FC_BLOCKS;
2114
+ if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
2115
+ return -ENOSPC;
2116
+
2117
+ /* Are we called twice? */
2118
+ WARN_ON(journal->j_fc_wbuf != NULL);
2119
+ journal->j_fc_wbuf = kmalloc_array(num_fc_blks,
2120
+ sizeof(struct buffer_head *), GFP_KERNEL);
2121
+ if (!journal->j_fc_wbuf)
2122
+ return -ENOMEM;
2123
+
2124
+ journal->j_fc_wbufsize = num_fc_blks;
2125
+ journal->j_fc_last = journal->j_last;
2126
+ journal->j_last = journal->j_fc_last - num_fc_blks;
2127
+ journal->j_fc_first = journal->j_last + 1;
2128
+ journal->j_fc_off = 0;
2129
+ journal->j_free = journal->j_last - journal->j_first;
2130
+ journal->j_max_transaction_buffers =
2131
+ jbd2_journal_get_max_txn_bufs(journal);
2132
+
2133
+ return 0;
2134
+}
2135
+
18692136 /**
1870
- * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
2137
+ * jbd2_journal_set_features() - Mark a given journal feature in the superblock
18712138 * @journal: Journal to act on.
18722139 * @compat: bitmask of compatible features
18732140 * @ro: bitmask of features that force read-only mount
....@@ -1878,7 +2145,7 @@
18782145 *
18792146 */
18802147
1881
-int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
2148
+int jbd2_journal_set_features(journal_t *journal, unsigned long compat,
18822149 unsigned long ro, unsigned long incompat)
18832150 {
18842151 #define INCOMPAT_FEATURE_ON(f) \
....@@ -1908,6 +2175,13 @@
19082175 compat, ro, incompat);
19092176
19102177 sb = journal->j_superblock;
2178
+
2179
+ if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) {
2180
+ if (jbd2_journal_initialize_fast_commit(journal)) {
2181
+ pr_err("JBD2: Cannot enable fast commits.\n");
2182
+ return 0;
2183
+ }
2184
+ }
19112185
19122186 /* Load the checksum driver if necessary */
19132187 if ((journal->j_chksum_driver == NULL) &&
....@@ -1942,6 +2216,8 @@
19422216 sb->s_feature_ro_compat |= cpu_to_be32(ro);
19432217 sb->s_feature_incompat |= cpu_to_be32(incompat);
19442218 unlock_buffer(journal->j_sb_buffer);
2219
+ journal->j_revoke_records_per_block =
2220
+ journal_revoke_records_per_block(journal);
19452221
19462222 return 1;
19472223 #undef COMPAT_FEATURE_ON
....@@ -1949,7 +2225,7 @@
19492225 }
19502226
19512227 /*
1952
- * jbd2_journal_clear_features () - Clear a given journal feature in the
2228
+ * jbd2_journal_clear_features() - Clear a given journal feature in the
19532229 * superblock
19542230 * @journal: Journal to act on.
19552231 * @compat: bitmask of compatible features
....@@ -1972,11 +2248,13 @@
19722248 sb->s_feature_compat &= ~cpu_to_be32(compat);
19732249 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
19742250 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
2251
+ journal->j_revoke_records_per_block =
2252
+ journal_revoke_records_per_block(journal);
19752253 }
19762254 EXPORT_SYMBOL(jbd2_journal_clear_features);
19772255
19782256 /**
1979
- * int jbd2_journal_flush () - Flush journal
2257
+ * jbd2_journal_flush() - Flush journal
19802258 * @journal: Journal to act on.
19812259 *
19822260 * Flush all data for a given journal to disk and empty the journal.
....@@ -2051,7 +2329,7 @@
20512329 }
20522330
20532331 /**
2054
- * int jbd2_journal_wipe() - Wipe journal contents
2332
+ * jbd2_journal_wipe() - Wipe journal contents
20552333 * @journal: Journal to act on.
20562334 * @write: flag (see below)
20572335 *
....@@ -2082,7 +2360,7 @@
20822360 err = jbd2_journal_skip_recovery(journal);
20832361 if (write) {
20842362 /* Lock to make assertions happy... */
2085
- mutex_lock(&journal->j_checkpoint_mutex);
2363
+ mutex_lock_io(&journal->j_checkpoint_mutex);
20862364 jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA);
20872365 mutex_unlock(&journal->j_checkpoint_mutex);
20882366 }
....@@ -2091,66 +2369,8 @@
20912369 return err;
20922370 }
20932371
2094
-/*
2095
- * Journal abort has very specific semantics, which we describe
2096
- * for journal abort.
2097
- *
2098
- * Two internal functions, which provide abort to the jbd layer
2099
- * itself are here.
2100
- */
2101
-
2102
-/*
2103
- * Quick version for internal journal use (doesn't lock the journal).
2104
- * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
2105
- * and don't attempt to make any other journal updates.
2106
- */
2107
-void __jbd2_journal_abort_hard(journal_t *journal)
2108
-{
2109
- transaction_t *transaction;
2110
-
2111
- if (journal->j_flags & JBD2_ABORT)
2112
- return;
2113
-
2114
- printk(KERN_ERR "Aborting journal on device %s.\n",
2115
- journal->j_devname);
2116
-
2117
- write_lock(&journal->j_state_lock);
2118
- journal->j_flags |= JBD2_ABORT;
2119
- transaction = journal->j_running_transaction;
2120
- if (transaction)
2121
- __jbd2_log_start_commit(journal, transaction->t_tid);
2122
- write_unlock(&journal->j_state_lock);
2123
-}
2124
-
2125
-/* Soft abort: record the abort error status in the journal superblock,
2126
- * but don't do any other IO. */
2127
-static void __journal_abort_soft (journal_t *journal, int errno)
2128
-{
2129
- int old_errno;
2130
-
2131
- write_lock(&journal->j_state_lock);
2132
- old_errno = journal->j_errno;
2133
- if (!journal->j_errno || errno == -ESHUTDOWN)
2134
- journal->j_errno = errno;
2135
-
2136
- if (journal->j_flags & JBD2_ABORT) {
2137
- write_unlock(&journal->j_state_lock);
2138
- if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN)
2139
- jbd2_journal_update_sb_errno(journal);
2140
- return;
2141
- }
2142
- write_unlock(&journal->j_state_lock);
2143
-
2144
- __jbd2_journal_abort_hard(journal);
2145
-
2146
- jbd2_journal_update_sb_errno(journal);
2147
- write_lock(&journal->j_state_lock);
2148
- journal->j_flags |= JBD2_REC_ERR;
2149
- write_unlock(&journal->j_state_lock);
2150
-}
2151
-
21522372 /**
2153
- * void jbd2_journal_abort () - Shutdown the journal immediately.
2373
+ * jbd2_journal_abort () - Shutdown the journal immediately.
21542374 * @journal: the journal to shutdown.
21552375 * @errno: an error number to record in the journal indicating
21562376 * the reason for the shutdown.
....@@ -2192,11 +2412,56 @@
21922412
21932413 void jbd2_journal_abort(journal_t *journal, int errno)
21942414 {
2195
- __journal_abort_soft(journal, errno);
2415
+ transaction_t *transaction;
2416
+
2417
+ /*
2418
+ * Lock the aborting procedure until everything is done, this avoid
2419
+ * races between filesystem's error handling flow (e.g. ext4_abort()),
2420
+ * ensure panic after the error info is written into journal's
2421
+ * superblock.
2422
+ */
2423
+ mutex_lock(&journal->j_abort_mutex);
2424
+ /*
2425
+ * ESHUTDOWN always takes precedence because a file system check
2426
+ * caused by any other journal abort error is not required after
2427
+ * a shutdown triggered.
2428
+ */
2429
+ write_lock(&journal->j_state_lock);
2430
+ if (journal->j_flags & JBD2_ABORT) {
2431
+ int old_errno = journal->j_errno;
2432
+
2433
+ write_unlock(&journal->j_state_lock);
2434
+ if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) {
2435
+ journal->j_errno = errno;
2436
+ jbd2_journal_update_sb_errno(journal);
2437
+ }
2438
+ mutex_unlock(&journal->j_abort_mutex);
2439
+ return;
2440
+ }
2441
+
2442
+ /*
2443
+ * Mark the abort as occurred and start current running transaction
2444
+ * to release all journaled buffer.
2445
+ */
2446
+ pr_err("Aborting journal on device %s.\n", journal->j_devname);
2447
+
2448
+ journal->j_flags |= JBD2_ABORT;
2449
+ journal->j_errno = errno;
2450
+ transaction = journal->j_running_transaction;
2451
+ if (transaction)
2452
+ __jbd2_log_start_commit(journal, transaction->t_tid);
2453
+ write_unlock(&journal->j_state_lock);
2454
+
2455
+ /*
2456
+ * Record errno to the journal super block, so that fsck and jbd2
2457
+ * layer could realise that a filesystem check is needed.
2458
+ */
2459
+ jbd2_journal_update_sb_errno(journal);
2460
+ mutex_unlock(&journal->j_abort_mutex);
21962461 }
21972462
21982463 /**
2199
- * int jbd2_journal_errno () - returns the journal's error state.
2464
+ * jbd2_journal_errno() - returns the journal's error state.
22002465 * @journal: journal to examine.
22012466 *
22022467 * This is the errno number set with jbd2_journal_abort(), the last
....@@ -2220,7 +2485,7 @@
22202485 }
22212486
22222487 /**
2223
- * int jbd2_journal_clear_err () - clears the journal's error state
2488
+ * jbd2_journal_clear_err() - clears the journal's error state
22242489 * @journal: journal to act on.
22252490 *
22262491 * An error must be cleared or acked to take a FS out of readonly
....@@ -2240,7 +2505,7 @@
22402505 }
22412506
22422507 /**
2243
- * void jbd2_journal_ack_err() - Ack journal err.
2508
+ * jbd2_journal_ack_err() - Ack journal err.
22442509 * @journal: journal to act on.
22452510 *
22462511 * An error must be cleared or acked to take a FS out of readonly
....@@ -2428,6 +2693,8 @@
24282693 ret = kmem_cache_zalloc(jbd2_journal_head_cache,
24292694 GFP_NOFS | __GFP_NOFAIL);
24302695 }
2696
+ if (ret)
2697
+ spin_lock_init(&ret->b_state_lock);
24312698 return ret;
24322699 }
24332700
....@@ -2534,12 +2801,12 @@
25342801 jbd_unlock_bh_journal_head(bh);
25352802 return jh;
25362803 }
2804
+EXPORT_SYMBOL(jbd2_journal_grab_journal_head);
25372805
25382806 static void __journal_remove_journal_head(struct buffer_head *bh)
25392807 {
25402808 struct journal_head *jh = bh2jh(bh);
25412809
2542
- J_ASSERT_JH(jh, jh->b_jcount >= 0);
25432810 J_ASSERT_JH(jh, jh->b_transaction == NULL);
25442811 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
25452812 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
....@@ -2547,17 +2814,23 @@
25472814 J_ASSERT_BH(bh, buffer_jbd(bh));
25482815 J_ASSERT_BH(bh, jh2bh(jh) == bh);
25492816 BUFFER_TRACE(bh, "remove journal_head");
2550
- if (jh->b_frozen_data) {
2551
- printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2552
- jbd2_free(jh->b_frozen_data, bh->b_size);
2553
- }
2554
- if (jh->b_committed_data) {
2555
- printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2556
- jbd2_free(jh->b_committed_data, bh->b_size);
2557
- }
2817
+
2818
+ /* Unlink before dropping the lock */
25582819 bh->b_private = NULL;
25592820 jh->b_bh = NULL; /* debug, really */
25602821 clear_buffer_jbd(bh);
2822
+}
2823
+
2824
+static void journal_release_journal_head(struct journal_head *jh, size_t b_size)
2825
+{
2826
+ if (jh->b_frozen_data) {
2827
+ printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2828
+ jbd2_free(jh->b_frozen_data, b_size);
2829
+ }
2830
+ if (jh->b_committed_data) {
2831
+ printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2832
+ jbd2_free(jh->b_committed_data, b_size);
2833
+ }
25612834 journal_free_journal_head(jh);
25622835 }
25632836
....@@ -2575,10 +2848,13 @@
25752848 if (!jh->b_jcount) {
25762849 __journal_remove_journal_head(bh);
25772850 jbd_unlock_bh_journal_head(bh);
2851
+ journal_release_journal_head(jh, bh->b_size);
25782852 __brelse(bh);
2579
- } else
2853
+ } else {
25802854 jbd_unlock_bh_journal_head(bh);
2855
+ }
25812856 }
2857
+EXPORT_SYMBOL(jbd2_journal_put_journal_head);
25822858
25832859 /*
25842860 * Initialize jbd inode head
....@@ -2744,6 +3020,7 @@
27443020 }
27453021
27463022 MODULE_LICENSE("GPL");
3023
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
27473024 module_init(journal_init);
27483025 module_exit(journal_exit);
27493026