Skip to content

Commit 74b0099

Browse files
konisakpm00
authored andcommitted
nilfs2: refactor nilfs_segctor_thread()
Simplify nilfs_segctor_thread(), the main loop function of the log writer thread, to make the basic structure easier to understand. In particular, the acquisition and release of the sc_state_lock spinlock was scattered throughout the function, so extract the determination of whether log writing is required into a helper function and make the spinlock lock sections clearer. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ryusuke Konishi <[email protected]> Cc: Huang Xiaojia <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3f66cc2 commit 74b0099

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

fs/nilfs2/segment.c

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,6 +2627,32 @@ static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
26272627
return SC_LSEG_SR;
26282628
}
26292629

2630+
/**
2631+
* nilfs_log_write_required - determine whether log writing is required
2632+
* @sci: nilfs_sc_info struct
2633+
* @modep: location for storing log writing mode
2634+
*
2635+
* Return: true if log writing is required, false otherwise. If log writing
2636+
* is required, the mode is stored in the location pointed to by @modep.
2637+
*/
2638+
static bool nilfs_log_write_required(struct nilfs_sc_info *sci, int *modep)
2639+
{
2640+
bool timedout, ret = true;
2641+
2642+
spin_lock(&sci->sc_state_lock);
2643+
timedout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2644+
time_after_eq(jiffies, sci->sc_timer.expires));
2645+
if (timedout || sci->sc_seq_request != sci->sc_seq_done)
2646+
*modep = SC_LSEG_SR;
2647+
else if (sci->sc_flush_request)
2648+
*modep = nilfs_segctor_flush_mode(sci);
2649+
else
2650+
ret = false;
2651+
2652+
spin_unlock(&sci->sc_state_lock);
2653+
return ret;
2654+
}
2655+
26302656
/**
26312657
* nilfs_segctor_thread - main loop of the log writer thread
26322658
* @arg: pointer to a struct nilfs_sc_info.
@@ -2642,70 +2668,39 @@ static int nilfs_segctor_thread(void *arg)
26422668
{
26432669
struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
26442670
struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2645-
int timeout = 0;
26462671

26472672
nilfs_info(sci->sc_super,
26482673
"segctord starting. Construction interval = %lu seconds, CP frequency < %lu seconds",
26492674
sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
26502675

26512676
set_freezable();
2652-
spin_lock(&sci->sc_state_lock);
2653-
loop:
2654-
for (;;) {
2655-
int mode;
2656-
2657-
if (kthread_should_stop())
2658-
goto end_thread;
2659-
2660-
if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2661-
mode = SC_LSEG_SR;
2662-
else if (sci->sc_flush_request)
2663-
mode = nilfs_segctor_flush_mode(sci);
2664-
else
2665-
break;
2666-
2667-
spin_unlock(&sci->sc_state_lock);
2668-
nilfs_segctor_thread_construct(sci, mode);
2669-
spin_lock(&sci->sc_state_lock);
2670-
timeout = 0;
2671-
}
26722677

2673-
2674-
if (freezing(current)) {
2675-
spin_unlock(&sci->sc_state_lock);
2676-
try_to_freeze();
2677-
spin_lock(&sci->sc_state_lock);
2678-
} else {
2678+
while (!kthread_should_stop()) {
26792679
DEFINE_WAIT(wait);
2680-
int should_sleep = 1;
2680+
bool should_write;
2681+
int mode;
2682+
2683+
if (freezing(current)) {
2684+
try_to_freeze();
2685+
continue;
2686+
}
26812687

26822688
prepare_to_wait(&sci->sc_wait_daemon, &wait,
26832689
TASK_INTERRUPTIBLE);
2684-
2685-
if (sci->sc_seq_request != sci->sc_seq_done)
2686-
should_sleep = 0;
2687-
else if (sci->sc_flush_request)
2688-
should_sleep = 0;
2689-
else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2690-
should_sleep = time_before(jiffies,
2691-
sci->sc_timer.expires);
2692-
2693-
if (should_sleep) {
2694-
spin_unlock(&sci->sc_state_lock);
2690+
should_write = nilfs_log_write_required(sci, &mode);
2691+
if (!should_write)
26952692
schedule();
2696-
spin_lock(&sci->sc_state_lock);
2697-
}
26982693
finish_wait(&sci->sc_wait_daemon, &wait);
2699-
timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2700-
time_after_eq(jiffies, sci->sc_timer.expires));
27012694

27022695
if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
27032696
set_nilfs_discontinued(nilfs);
2697+
2698+
if (should_write)
2699+
nilfs_segctor_thread_construct(sci, mode);
27042700
}
2705-
goto loop;
27062701

2707-
end_thread:
27082702
/* end sync. */
2703+
spin_lock(&sci->sc_state_lock);
27092704
sci->sc_task = NULL;
27102705
timer_shutdown_sync(&sci->sc_timer);
27112706
spin_unlock(&sci->sc_state_lock);

0 commit comments

Comments
 (0)