Skip to content

Commit f47aa3e

Browse files
Kemeng Shitytso
authored andcommitted
jbd2: move escape handle to futher improve jbd2_journal_write_metadata_buffer
Move escape handle to futher improve code readability and remove some repeat check. Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com> Reviewed-by: Jan Kara <jack@suse.cz> Reviewed-by: Zhang Yi <yi.zhang@huawei.com> Link: https://patch.msgid.link/20240801013815.2393869-7-shikemeng@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 7c48e7d commit f47aa3e

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

fs/jbd2/journal.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ static void journal_kill_thread(journal_t *journal)
281281
write_unlock(&journal->j_state_lock);
282282
}
283283

284+
static inline bool jbd2_data_needs_escaping(char *data)
285+
{
286+
return *((__be32 *)data) == cpu_to_be32(JBD2_MAGIC_NUMBER);
287+
}
288+
289+
static inline void jbd2_data_do_escape(char *data)
290+
{
291+
*((unsigned int *)data) = 0;
292+
}
293+
284294
/*
285295
* jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
286296
*
@@ -319,7 +329,6 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
319329
sector_t blocknr)
320330
{
321331
int do_escape = 0;
322-
char *mapped_data;
323332
struct buffer_head *new_bh;
324333
struct folio *new_folio;
325334
unsigned int new_offset;
@@ -350,8 +359,13 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
350359
if (jh_in->b_frozen_data) {
351360
new_folio = virt_to_folio(jh_in->b_frozen_data);
352361
new_offset = offset_in_folio(new_folio, jh_in->b_frozen_data);
353-
mapped_data = jh_in->b_frozen_data;
362+
do_escape = jbd2_data_needs_escaping(jh_in->b_frozen_data);
363+
if (do_escape)
364+
jbd2_data_do_escape(jh_in->b_frozen_data);
354365
} else {
366+
char *tmp;
367+
char *mapped_data;
368+
355369
new_folio = bh_in->b_folio;
356370
new_offset = offset_in_folio(new_folio, bh_in->b_data);
357371
mapped_data = kmap_local_folio(new_folio, new_offset);
@@ -363,21 +377,13 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
363377
*/
364378
jbd2_buffer_frozen_trigger(jh_in, mapped_data,
365379
jh_in->b_triggers);
366-
}
367-
368-
/*
369-
* Check for escaping
370-
*/
371-
if (*((__be32 *)mapped_data) == cpu_to_be32(JBD2_MAGIC_NUMBER))
372-
do_escape = 1;
373-
if (!jh_in->b_frozen_data)
380+
do_escape = jbd2_data_needs_escaping(mapped_data);
374381
kunmap_local(mapped_data);
375-
376-
/*
377-
* Do we need to do a data copy?
378-
*/
379-
if (do_escape && !jh_in->b_frozen_data) {
380-
char *tmp;
382+
/*
383+
* Do we need to do a data copy?
384+
*/
385+
if (!do_escape)
386+
goto escape_done;
381387

382388
spin_unlock(&jh_in->b_state_lock);
383389
tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
@@ -404,17 +410,10 @@ int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
404410
copy_done:
405411
new_folio = virt_to_folio(jh_in->b_frozen_data);
406412
new_offset = offset_in_folio(new_folio, jh_in->b_frozen_data);
413+
jbd2_data_do_escape(jh_in->b_frozen_data);
407414
}
408415

409-
/*
410-
* Did we need to do an escaping? Now we've done all the
411-
* copying, we can finally do so.
412-
* b_frozen_data is from jbd2_alloc() which always provides an
413-
* address from the direct kernels mapping.
414-
*/
415-
if (do_escape)
416-
*((unsigned int *)jh_in->b_frozen_data) = 0;
417-
416+
escape_done:
418417
folio_set_bh(new_bh, new_folio, new_offset);
419418
new_bh->b_size = bh_in->b_size;
420419
new_bh->b_bdev = journal->j_dev;

0 commit comments

Comments
 (0)