nilfs2: convert nilfs_lookup_dirty_data_buffers() to use filemap_get_folios_tag()
[linux-block.git] / fs / nilfs2 / segment.c
CommitLineData
ae98043f 1// SPDX-License-Identifier: GPL-2.0+
9ff05123 2/*
94ee1d91 3 * NILFS segment constructor.
9ff05123
RK
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
4b420ab4 7 * Written by Ryusuke Konishi.
9ff05123
RK
8 *
9 */
10
11#include <linux/pagemap.h>
12#include <linux/buffer_head.h>
13#include <linux/writeback.h>
ead8ecff 14#include <linux/bitops.h>
9ff05123
RK
15#include <linux/bio.h>
16#include <linux/completion.h>
17#include <linux/blkdev.h>
18#include <linux/backing-dev.h>
19#include <linux/freezer.h>
20#include <linux/kthread.h>
21#include <linux/crc32.h>
22#include <linux/pagevec.h>
5a0e3ad6 23#include <linux/slab.h>
174cd4b1
IM
24#include <linux/sched/signal.h>
25
9ff05123
RK
26#include "nilfs.h"
27#include "btnode.h"
28#include "page.h"
29#include "segment.h"
30#include "sufile.h"
31#include "cpfile.h"
32#include "ifile.h"
9ff05123
RK
33#include "segbuf.h"
34
35
36/*
37 * Segment constructor
38 */
39#define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */
40
076a378b
RK
41#define SC_MAX_SEGDELTA 64 /*
42 * Upper limit of the number of segments
43 * appended in collection retry loop
44 */
9ff05123
RK
45
46/* Construction mode */
47enum {
48 SC_LSEG_SR = 1, /* Make a logical segment having a super root */
076a378b
RK
49 SC_LSEG_DSYNC, /*
50 * Flush data blocks of a given file and make
51 * a logical segment without a super root.
52 */
53 SC_FLUSH_FILE, /*
54 * Flush data files, leads to segment writes without
55 * creating a checkpoint.
56 */
57 SC_FLUSH_DAT, /*
58 * Flush DAT file. This also creates segments
59 * without a checkpoint.
60 */
9ff05123
RK
61};
62
63/* Stage numbers of dirty block collection */
64enum {
65 NILFS_ST_INIT = 0,
66 NILFS_ST_GC, /* Collecting dirty blocks for GC */
67 NILFS_ST_FILE,
9ff05123
RK
68 NILFS_ST_IFILE,
69 NILFS_ST_CPFILE,
70 NILFS_ST_SUFILE,
71 NILFS_ST_DAT,
72 NILFS_ST_SR, /* Super root */
73 NILFS_ST_DSYNC, /* Data sync blocks */
74 NILFS_ST_DONE,
75};
76
58497703
HM
77#define CREATE_TRACE_POINTS
78#include <trace/events/nilfs2.h>
79
80/*
81 * nilfs_sc_cstage_inc(), nilfs_sc_cstage_set(), nilfs_sc_cstage_get() are
82 * wrapper functions of stage count (nilfs_sc_info->sc_stage.scnt). Users of
83 * the variable must use them because transition of stage count must involve
84 * trace events (trace_nilfs2_collection_stage_transition).
85 *
86 * nilfs_sc_cstage_get() isn't required for the above purpose because it doesn't
87 * produce tracepoint events. It is provided just for making the intention
88 * clear.
89 */
90static inline void nilfs_sc_cstage_inc(struct nilfs_sc_info *sci)
91{
92 sci->sc_stage.scnt++;
93 trace_nilfs2_collection_stage_transition(sci);
94}
95
96static inline void nilfs_sc_cstage_set(struct nilfs_sc_info *sci, int next_scnt)
97{
98 sci->sc_stage.scnt = next_scnt;
99 trace_nilfs2_collection_stage_transition(sci);
100}
101
102static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci)
103{
104 return sci->sc_stage.scnt;
105}
106
9ff05123
RK
107/* State flags of collection */
108#define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
109#define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
071cb4b8
RK
110#define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
111#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
9ff05123
RK
112
113/* Operations depending on the construction mode and file type */
114struct nilfs_sc_operations {
115 int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *,
116 struct inode *);
117 int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *,
118 struct inode *);
119 int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *,
120 struct inode *);
121 void (*write_data_binfo)(struct nilfs_sc_info *,
122 struct nilfs_segsum_pointer *,
123 union nilfs_binfo *);
124 void (*write_node_binfo)(struct nilfs_sc_info *,
125 struct nilfs_segsum_pointer *,
126 union nilfs_binfo *);
127};
128
129/*
130 * Other definitions
131 */
132static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
133static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
134static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
693dd321 135static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
9ff05123 136
9ff05123
RK
137#define nilfs_cnt32_ge(a, b) \
138 (typecheck(__u32, a) && typecheck(__u32, b) && \
139 ((__s32)(a) - (__s32)(b) >= 0))
9ff05123 140
feee880f
RK
141static int nilfs_prepare_segment_lock(struct super_block *sb,
142 struct nilfs_transaction_info *ti)
9ff05123
RK
143{
144 struct nilfs_transaction_info *cur_ti = current->journal_info;
145 void *save = NULL;
146
147 if (cur_ti) {
148 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
149 return ++cur_ti->ti_count;
7f00184e
RK
150
151 /*
152 * If journal_info field is occupied by other FS,
153 * it is saved and will be restored on
154 * nilfs_transaction_commit().
155 */
a1d0747a 156 nilfs_warn(sb, "journal info from a different FS");
7f00184e 157 save = current->journal_info;
9ff05123
RK
158 }
159 if (!ti) {
160 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
161 if (!ti)
162 return -ENOMEM;
163 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
164 } else {
165 ti->ti_flags = 0;
166 }
167 ti->ti_count = 0;
168 ti->ti_save = save;
169 ti->ti_magic = NILFS_TI_MAGIC;
170 current->journal_info = ti;
171 return 0;
172}
173
174/**
175 * nilfs_transaction_begin - start indivisible file operations.
176 * @sb: super block
177 * @ti: nilfs_transaction_info
178 * @vacancy_check: flags for vacancy rate checks
179 *
180 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
181 * the segment semaphore, to make a segment construction and write tasks
47420c79 182 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
9ff05123
RK
183 * The region enclosed by these two functions can be nested. To avoid a
184 * deadlock, the semaphore is only acquired or released in the outermost call.
185 *
186 * This function allocates a nilfs_transaction_info struct to keep context
187 * information on it. It is initialized and hooked onto the current task in
188 * the outermost call. If a pre-allocated struct is given to @ti, it is used
7a65004b 189 * instead; otherwise a new struct is assigned from a slab.
9ff05123
RK
190 *
191 * When @vacancy_check flag is set, this function will check the amount of
192 * free space, and will wait for the GC to reclaim disk space if low capacity.
193 *
194 * Return Value: On success, 0 is returned. On error, one of the following
195 * negative error code is returned.
196 *
197 * %-ENOMEM - Insufficient memory available.
198 *
9ff05123
RK
199 * %-ENOSPC - No space left on device
200 */
201int nilfs_transaction_begin(struct super_block *sb,
202 struct nilfs_transaction_info *ti,
203 int vacancy_check)
204{
9ff05123 205 struct the_nilfs *nilfs;
feee880f 206 int ret = nilfs_prepare_segment_lock(sb, ti);
44fda114 207 struct nilfs_transaction_info *trace_ti;
9ff05123
RK
208
209 if (unlikely(ret < 0))
210 return ret;
44fda114
HM
211 if (ret > 0) {
212 trace_ti = current->journal_info;
213
214 trace_nilfs2_transaction_transition(sb, trace_ti,
215 trace_ti->ti_count, trace_ti->ti_flags,
216 TRACE_NILFS2_TRANSACTION_BEGIN);
9ff05123 217 return 0;
44fda114 218 }
9ff05123 219
2c22b337 220 sb_start_intwrite(sb);
5beb6e0b 221
e3154e97 222 nilfs = sb->s_fs_info;
9ff05123
RK
223 down_read(&nilfs->ns_segctor_sem);
224 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
225 up_read(&nilfs->ns_segctor_sem);
226 ret = -ENOSPC;
227 goto failed;
228 }
44fda114
HM
229
230 trace_ti = current->journal_info;
231 trace_nilfs2_transaction_transition(sb, trace_ti, trace_ti->ti_count,
232 trace_ti->ti_flags,
233 TRACE_NILFS2_TRANSACTION_BEGIN);
9ff05123
RK
234 return 0;
235
236 failed:
237 ti = current->journal_info;
238 current->journal_info = ti->ti_save;
239 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
240 kmem_cache_free(nilfs_transaction_cachep, ti);
2c22b337 241 sb_end_intwrite(sb);
9ff05123
RK
242 return ret;
243}
244
245/**
47420c79 246 * nilfs_transaction_commit - commit indivisible file operations.
9ff05123 247 * @sb: super block
9ff05123 248 *
47420c79
RK
249 * nilfs_transaction_commit() releases the read semaphore which is
250 * acquired by nilfs_transaction_begin(). This is only performed
251 * in outermost call of this function. If a commit flag is set,
252 * nilfs_transaction_commit() sets a timer to start the segment
253 * constructor. If a sync flag is set, it starts construction
254 * directly.
9ff05123 255 */
47420c79 256int nilfs_transaction_commit(struct super_block *sb)
9ff05123
RK
257{
258 struct nilfs_transaction_info *ti = current->journal_info;
e3154e97 259 struct the_nilfs *nilfs = sb->s_fs_info;
9ff05123
RK
260 int err = 0;
261
262 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
47420c79 263 ti->ti_flags |= NILFS_TI_COMMIT;
9ff05123
RK
264 if (ti->ti_count > 0) {
265 ti->ti_count--;
44fda114
HM
266 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
267 ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
9ff05123
RK
268 return 0;
269 }
3fd3fe5a
RK
270 if (nilfs->ns_writer) {
271 struct nilfs_sc_info *sci = nilfs->ns_writer;
272
9ff05123
RK
273 if (ti->ti_flags & NILFS_TI_COMMIT)
274 nilfs_segctor_start_timer(sci);
3fd3fe5a 275 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
9ff05123
RK
276 nilfs_segctor_do_flush(sci, 0);
277 }
3fd3fe5a 278 up_read(&nilfs->ns_segctor_sem);
44fda114
HM
279 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
280 ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
281
9ff05123
RK
282 current->journal_info = ti->ti_save;
283
284 if (ti->ti_flags & NILFS_TI_SYNC)
285 err = nilfs_construct_segment(sb);
286 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
287 kmem_cache_free(nilfs_transaction_cachep, ti);
2c22b337 288 sb_end_intwrite(sb);
9ff05123
RK
289 return err;
290}
291
47420c79
RK
292void nilfs_transaction_abort(struct super_block *sb)
293{
294 struct nilfs_transaction_info *ti = current->journal_info;
e3154e97 295 struct the_nilfs *nilfs = sb->s_fs_info;
47420c79
RK
296
297 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
298 if (ti->ti_count > 0) {
299 ti->ti_count--;
44fda114
HM
300 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
301 ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
47420c79
RK
302 return;
303 }
e3154e97 304 up_read(&nilfs->ns_segctor_sem);
47420c79 305
44fda114
HM
306 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
307 ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
308
47420c79
RK
309 current->journal_info = ti->ti_save;
310 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
311 kmem_cache_free(nilfs_transaction_cachep, ti);
2c22b337 312 sb_end_intwrite(sb);
47420c79
RK
313}
314
9ff05123
RK
315void nilfs_relax_pressure_in_lock(struct super_block *sb)
316{
e3154e97 317 struct the_nilfs *nilfs = sb->s_fs_info;
3fd3fe5a 318 struct nilfs_sc_info *sci = nilfs->ns_writer;
9ff05123 319
8cccf05f 320 if (sb_rdonly(sb) || unlikely(!sci) || !sci->sc_flush_request)
9ff05123
RK
321 return;
322
323 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
324 up_read(&nilfs->ns_segctor_sem);
325
326 down_write(&nilfs->ns_segctor_sem);
327 if (sci->sc_flush_request &&
328 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
329 struct nilfs_transaction_info *ti = current->journal_info;
330
331 ti->ti_flags |= NILFS_TI_WRITER;
332 nilfs_segctor_do_immediate_flush(sci);
333 ti->ti_flags &= ~NILFS_TI_WRITER;
334 }
335 downgrade_write(&nilfs->ns_segctor_sem);
336}
337
f7545144 338static void nilfs_transaction_lock(struct super_block *sb,
9ff05123
RK
339 struct nilfs_transaction_info *ti,
340 int gcflag)
341{
342 struct nilfs_transaction_info *cur_ti = current->journal_info;
e3154e97 343 struct the_nilfs *nilfs = sb->s_fs_info;
3fd3fe5a 344 struct nilfs_sc_info *sci = nilfs->ns_writer;
9ff05123 345
1f5abe7e 346 WARN_ON(cur_ti);
9ff05123
RK
347 ti->ti_flags = NILFS_TI_WRITER;
348 ti->ti_count = 0;
349 ti->ti_save = cur_ti;
350 ti->ti_magic = NILFS_TI_MAGIC;
9ff05123
RK
351 current->journal_info = ti;
352
353 for (;;) {
44fda114
HM
354 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
355 ti->ti_flags, TRACE_NILFS2_TRANSACTION_TRYLOCK);
356
3fd3fe5a
RK
357 down_write(&nilfs->ns_segctor_sem);
358 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
9ff05123
RK
359 break;
360
3fd3fe5a 361 nilfs_segctor_do_immediate_flush(sci);
9ff05123 362
f7545144 363 up_write(&nilfs->ns_segctor_sem);
aceb4170 364 cond_resched();
9ff05123
RK
365 }
366 if (gcflag)
367 ti->ti_flags |= NILFS_TI_GC;
44fda114
HM
368
369 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
370 ti->ti_flags, TRACE_NILFS2_TRANSACTION_LOCK);
9ff05123
RK
371}
372
f7545144 373static void nilfs_transaction_unlock(struct super_block *sb)
9ff05123
RK
374{
375 struct nilfs_transaction_info *ti = current->journal_info;
e3154e97 376 struct the_nilfs *nilfs = sb->s_fs_info;
9ff05123
RK
377
378 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
379 BUG_ON(ti->ti_count > 0);
380
693dd321 381 up_write(&nilfs->ns_segctor_sem);
9ff05123 382 current->journal_info = ti->ti_save;
44fda114
HM
383
384 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
385 ti->ti_flags, TRACE_NILFS2_TRANSACTION_UNLOCK);
9ff05123
RK
386}
387
388static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
389 struct nilfs_segsum_pointer *ssp,
0c6c44cb 390 unsigned int bytes)
9ff05123
RK
391{
392 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
0c6c44cb 393 unsigned int blocksize = sci->sc_super->s_blocksize;
9ff05123
RK
394 void *p;
395
396 if (unlikely(ssp->offset + bytes > blocksize)) {
397 ssp->offset = 0;
398 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
399 &segbuf->sb_segsum_buffers));
400 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
401 }
402 p = ssp->bh->b_data + ssp->offset;
403 ssp->offset += bytes;
404 return p;
405}
406
407/**
408 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
409 * @sci: nilfs_sc_info
410 */
411static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
412{
413 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
414 struct buffer_head *sumbh;
0c6c44cb
RK
415 unsigned int sumbytes;
416 unsigned int flags = 0;
9ff05123
RK
417 int err;
418
419 if (nilfs_doing_gc())
420 flags = NILFS_SS_GC;
6c43f410 421 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
9ff05123
RK
422 if (unlikely(err))
423 return err;
424
425 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
426 sumbytes = segbuf->sb_sum.sumbytes;
427 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
428 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
429 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
430 return 0;
431}
432
433static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
434{
435 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
436 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
076a378b
RK
437 return -E2BIG; /*
438 * The current segment is filled up
439 * (internal code)
440 */
9ff05123
RK
441 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
442 return nilfs_segctor_reset_segment_buffer(sci);
443}
444
445static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
446{
447 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
448 int err;
449
450 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
451 err = nilfs_segctor_feed_segment(sci);
452 if (err)
453 return err;
454 segbuf = sci->sc_curseg;
455 }
1e2b68bf 456 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
9ff05123
RK
457 if (likely(!err))
458 segbuf->sb_sum.flags |= NILFS_SS_SR;
459 return err;
460}
461
462/*
463 * Functions for making segment summary and payloads
464 */
465static int nilfs_segctor_segsum_block_required(
466 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
0c6c44cb 467 unsigned int binfo_size)
9ff05123 468{
0c6c44cb 469 unsigned int blocksize = sci->sc_super->s_blocksize;
9ff05123
RK
470 /* Size of finfo and binfo is enough small against blocksize */
471
472 return ssp->offset + binfo_size +
473 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
474 blocksize;
475}
476
477static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
478 struct inode *inode)
479{
480 sci->sc_curseg->sb_sum.nfinfo++;
481 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
482 nilfs_segctor_map_segsum_entry(
483 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
c96fa464 484
72746ac6
RK
485 if (NILFS_I(inode)->i_root &&
486 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
c96fa464 487 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
9ff05123
RK
488 /* skip finfo */
489}
490
491static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
492 struct inode *inode)
493{
494 struct nilfs_finfo *finfo;
495 struct nilfs_inode_info *ii;
496 struct nilfs_segment_buffer *segbuf;
6c43f410 497 __u64 cno;
9ff05123
RK
498
499 if (sci->sc_blk_cnt == 0)
500 return;
501
502 ii = NILFS_I(inode);
6c43f410
RK
503
504 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
505 cno = ii->i_cno;
506 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
507 cno = 0;
508 else
509 cno = sci->sc_cno;
510
9ff05123
RK
511 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
512 sizeof(*finfo));
513 finfo->fi_ino = cpu_to_le64(inode->i_ino);
514 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
515 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
6c43f410 516 finfo->fi_cno = cpu_to_le64(cno);
9ff05123
RK
517
518 segbuf = sci->sc_curseg;
519 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
520 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
521 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
522 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
523}
524
525static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
526 struct buffer_head *bh,
527 struct inode *inode,
0c6c44cb 528 unsigned int binfo_size)
9ff05123
RK
529{
530 struct nilfs_segment_buffer *segbuf;
531 int required, err = 0;
532
533 retry:
534 segbuf = sci->sc_curseg;
535 required = nilfs_segctor_segsum_block_required(
536 sci, &sci->sc_binfo_ptr, binfo_size);
537 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
538 nilfs_segctor_end_finfo(sci, inode);
539 err = nilfs_segctor_feed_segment(sci);
540 if (err)
541 return err;
542 goto retry;
543 }
544 if (unlikely(required)) {
545 err = nilfs_segbuf_extend_segsum(segbuf);
546 if (unlikely(err))
547 goto failed;
548 }
549 if (sci->sc_blk_cnt == 0)
550 nilfs_segctor_begin_finfo(sci, inode);
551
552 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
553 /* Substitution to vblocknr is delayed until update_blocknr() */
554 nilfs_segbuf_add_file_buffer(segbuf, bh);
555 sci->sc_blk_cnt++;
556 failed:
557 return err;
558}
559
9ff05123
RK
560/*
561 * Callback functions that enumerate, mark, and collect dirty blocks
562 */
563static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
564 struct buffer_head *bh, struct inode *inode)
565{
566 int err;
567
9ff05123 568 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
e828949e
RK
569 if (err < 0)
570 return err;
9ff05123
RK
571
572 err = nilfs_segctor_add_file_block(sci, bh, inode,
573 sizeof(struct nilfs_binfo_v));
574 if (!err)
575 sci->sc_datablk_cnt++;
576 return err;
577}
578
579static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
580 struct buffer_head *bh,
581 struct inode *inode)
582{
e828949e 583 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
9ff05123
RK
584}
585
586static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
587 struct buffer_head *bh,
588 struct inode *inode)
589{
1f5abe7e 590 WARN_ON(!buffer_dirty(bh));
9ff05123
RK
591 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
592}
593
594static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
595 struct nilfs_segsum_pointer *ssp,
596 union nilfs_binfo *binfo)
597{
598 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
599 sci, ssp, sizeof(*binfo_v));
600 *binfo_v = binfo->bi_v;
601}
602
603static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
604 struct nilfs_segsum_pointer *ssp,
605 union nilfs_binfo *binfo)
606{
607 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
608 sci, ssp, sizeof(*vblocknr));
609 *vblocknr = binfo->bi_v.bi_vblocknr;
610}
611
1c613cb9 612static const struct nilfs_sc_operations nilfs_sc_file_ops = {
9ff05123
RK
613 .collect_data = nilfs_collect_file_data,
614 .collect_node = nilfs_collect_file_node,
615 .collect_bmap = nilfs_collect_file_bmap,
616 .write_data_binfo = nilfs_write_file_data_binfo,
617 .write_node_binfo = nilfs_write_file_node_binfo,
618};
619
620static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
621 struct buffer_head *bh, struct inode *inode)
622{
623 int err;
624
625 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
e828949e
RK
626 if (err < 0)
627 return err;
9ff05123
RK
628
629 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
630 if (!err)
631 sci->sc_datablk_cnt++;
632 return err;
633}
634
635static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
636 struct buffer_head *bh, struct inode *inode)
637{
1f5abe7e 638 WARN_ON(!buffer_dirty(bh));
9ff05123
RK
639 return nilfs_segctor_add_file_block(sci, bh, inode,
640 sizeof(struct nilfs_binfo_dat));
641}
642
643static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
644 struct nilfs_segsum_pointer *ssp,
645 union nilfs_binfo *binfo)
646{
647 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
648 sizeof(*blkoff));
649 *blkoff = binfo->bi_dat.bi_blkoff;
650}
651
652static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
653 struct nilfs_segsum_pointer *ssp,
654 union nilfs_binfo *binfo)
655{
656 struct nilfs_binfo_dat *binfo_dat =
657 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
658 *binfo_dat = binfo->bi_dat;
659}
660
1c613cb9 661static const struct nilfs_sc_operations nilfs_sc_dat_ops = {
9ff05123
RK
662 .collect_data = nilfs_collect_dat_data,
663 .collect_node = nilfs_collect_file_node,
664 .collect_bmap = nilfs_collect_dat_bmap,
665 .write_data_binfo = nilfs_write_dat_data_binfo,
666 .write_node_binfo = nilfs_write_dat_node_binfo,
667};
668
1c613cb9 669static const struct nilfs_sc_operations nilfs_sc_dsync_ops = {
9ff05123
RK
670 .collect_data = nilfs_collect_file_data,
671 .collect_node = NULL,
672 .collect_bmap = NULL,
673 .write_data_binfo = nilfs_write_file_data_binfo,
674 .write_node_binfo = NULL,
675};
676
f30bf3e4
RK
677static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
678 struct list_head *listp,
679 size_t nlimit,
680 loff_t start, loff_t end)
9ff05123 681{
9ff05123 682 struct address_space *mapping = inode->i_mapping;
5ee4b25c 683 struct folio_batch fbatch;
f30bf3e4
RK
684 pgoff_t index = 0, last = ULONG_MAX;
685 size_t ndirties = 0;
686 int i;
9ff05123 687
f30bf3e4
RK
688 if (unlikely(start != 0 || end != LLONG_MAX)) {
689 /*
690 * A valid range is given for sync-ing data pages. The
691 * range is rounded to per-page; extra dirty buffers
692 * may be included if blocksize < pagesize.
693 */
694 index = start >> PAGE_SHIFT;
695 last = end >> PAGE_SHIFT;
696 }
5ee4b25c 697 folio_batch_init(&fbatch);
9ff05123 698 repeat:
f30bf3e4 699 if (unlikely(index > last) ||
5ee4b25c
VMO
700 !filemap_get_folios_tag(mapping, &index, last,
701 PAGECACHE_TAG_DIRTY, &fbatch))
f30bf3e4 702 return ndirties;
9ff05123 703
5ee4b25c 704 for (i = 0; i < folio_batch_count(&fbatch); i++) {
9ff05123 705 struct buffer_head *bh, *head;
5ee4b25c 706 struct folio *folio = fbatch.folios[i];
9ff05123 707
5ee4b25c
VMO
708 folio_lock(folio);
709 head = folio_buffers(folio);
710 if (!head) {
711 create_empty_buffers(&folio->page, i_blocksize(inode), 0);
712 head = folio_buffers(folio);
713 }
714 folio_unlock(folio);
9ff05123 715
5ee4b25c 716 bh = head;
9ff05123 717 do {
7f42ec39 718 if (!buffer_dirty(bh) || buffer_async_write(bh))
f30bf3e4
RK
719 continue;
720 get_bh(bh);
721 list_add_tail(&bh->b_assoc_buffers, listp);
722 ndirties++;
723 if (unlikely(ndirties >= nlimit)) {
5ee4b25c 724 folio_batch_release(&fbatch);
f30bf3e4
RK
725 cond_resched();
726 return ndirties;
9ff05123 727 }
f30bf3e4 728 } while (bh = bh->b_this_page, bh != head);
9ff05123 729 }
5ee4b25c 730 folio_batch_release(&fbatch);
9ff05123 731 cond_resched();
f30bf3e4 732 goto repeat;
9ff05123
RK
733}
734
735static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
736 struct list_head *listp)
737{
738 struct nilfs_inode_info *ii = NILFS_I(inode);
e897be17 739 struct inode *btnc_inode = ii->i_assoc_inode;
9ff05123
RK
740 struct pagevec pvec;
741 struct buffer_head *bh, *head;
742 unsigned int i;
743 pgoff_t index = 0;
744
e897be17
RK
745 if (!btnc_inode)
746 return;
747
86679820 748 pagevec_init(&pvec);
9ff05123 749
e897be17 750 while (pagevec_lookup_tag(&pvec, btnc_inode->i_mapping, &index,
67fd707f 751 PAGECACHE_TAG_DIRTY)) {
9ff05123
RK
752 for (i = 0; i < pagevec_count(&pvec); i++) {
753 bh = head = page_buffers(pvec.pages[i]);
754 do {
7f42ec39
VD
755 if (buffer_dirty(bh) &&
756 !buffer_async_write(bh)) {
9ff05123
RK
757 get_bh(bh);
758 list_add_tail(&bh->b_assoc_buffers,
759 listp);
760 }
761 bh = bh->b_this_page;
762 } while (bh != head);
763 }
764 pagevec_release(&pvec);
765 cond_resched();
766 }
767}
768
693dd321 769static void nilfs_dispose_list(struct the_nilfs *nilfs,
9ff05123
RK
770 struct list_head *head, int force)
771{
772 struct nilfs_inode_info *ii, *n;
773 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
0c6c44cb 774 unsigned int nv = 0;
9ff05123
RK
775
776 while (!list_empty(head)) {
693dd321 777 spin_lock(&nilfs->ns_inode_lock);
9ff05123
RK
778 list_for_each_entry_safe(ii, n, head, i_dirty) {
779 list_del_init(&ii->i_dirty);
780 if (force) {
781 if (unlikely(ii->i_bh)) {
782 brelse(ii->i_bh);
783 ii->i_bh = NULL;
784 }
785 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
786 set_bit(NILFS_I_QUEUED, &ii->i_state);
787 list_add_tail(&ii->i_dirty,
693dd321 788 &nilfs->ns_dirty_files);
9ff05123
RK
789 continue;
790 }
791 ivec[nv++] = ii;
792 if (nv == SC_N_INODEVEC)
793 break;
794 }
693dd321 795 spin_unlock(&nilfs->ns_inode_lock);
9ff05123
RK
796
797 for (pii = ivec; nv > 0; pii++, nv--)
798 iput(&(*pii)->vfs_inode);
799 }
800}
801
7ef3ff2f
RK
802static void nilfs_iput_work_func(struct work_struct *work)
803{
804 struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
805 sc_iput_work);
806 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
807
808 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
809}
810
e912a5b6
RK
811static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
812 struct nilfs_root *root)
9ff05123 813{
9ff05123
RK
814 int ret = 0;
815
e912a5b6 816 if (nilfs_mdt_fetch_dirty(root->ifile))
9ff05123
RK
817 ret++;
818 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
819 ret++;
820 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
821 ret++;
365e215c
RK
822 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
823 ret++;
9ff05123
RK
824 return ret;
825}
826
827static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
828{
829 return list_empty(&sci->sc_dirty_files) &&
830 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
071cb4b8 831 sci->sc_nfreesegs == 0 &&
9ff05123
RK
832 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
833}
834
835static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
836{
e3154e97 837 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123
RK
838 int ret = 0;
839
693dd321 840 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
9ff05123
RK
841 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
842
693dd321
RK
843 spin_lock(&nilfs->ns_inode_lock);
844 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
9ff05123
RK
845 ret++;
846
693dd321 847 spin_unlock(&nilfs->ns_inode_lock);
9ff05123
RK
848 return ret;
849}
850
851static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
852{
e3154e97 853 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123 854
e912a5b6 855 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
9ff05123
RK
856 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
857 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
365e215c 858 nilfs_mdt_clear_dirty(nilfs->ns_dat);
9ff05123
RK
859}
860
861static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
862{
e3154e97 863 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123
RK
864 struct buffer_head *bh_cp;
865 struct nilfs_checkpoint *raw_cp;
866 int err;
867
868 /* XXX: this interface will be changed */
869 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
870 &raw_cp, &bh_cp);
871 if (likely(!err)) {
076a378b
RK
872 /*
873 * The following code is duplicated with cpfile. But, it is
874 * needed to collect the checkpoint even if it was not newly
875 * created.
876 */
5fc7b141 877 mark_buffer_dirty(bh_cp);
9ff05123
RK
878 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
879 nilfs_cpfile_put_checkpoint(
880 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
723ac751
RK
881 } else if (err == -EINVAL || err == -ENOENT) {
882 nilfs_error(sci->sc_super,
883 "checkpoint creation failed due to metadata corruption.");
884 err = -EIO;
885 }
9ff05123
RK
886 return err;
887}
888
889static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
890{
e3154e97 891 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123
RK
892 struct buffer_head *bh_cp;
893 struct nilfs_checkpoint *raw_cp;
894 int err;
895
896 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
897 &raw_cp, &bh_cp);
898 if (unlikely(err)) {
723ac751
RK
899 if (err == -EINVAL || err == -ENOENT) {
900 nilfs_error(sci->sc_super,
901 "checkpoint finalization failed due to metadata corruption.");
902 err = -EIO;
903 }
9ff05123
RK
904 goto failed_ibh;
905 }
906 raw_cp->cp_snapshot_list.ssl_next = 0;
907 raw_cp->cp_snapshot_list.ssl_prev = 0;
908 raw_cp->cp_inodes_count =
e5f7f848 909 cpu_to_le64(atomic64_read(&sci->sc_root->inodes_count));
9ff05123 910 raw_cp->cp_blocks_count =
e5f7f848 911 cpu_to_le64(atomic64_read(&sci->sc_root->blocks_count));
9ff05123
RK
912 raw_cp->cp_nblk_inc =
913 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
914 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
915 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
458c5b08 916
c96fa464
RK
917 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
918 nilfs_checkpoint_clear_minor(raw_cp);
919 else
920 nilfs_checkpoint_set_minor(raw_cp);
921
e912a5b6
RK
922 nilfs_write_inode_common(sci->sc_root->ifile,
923 &raw_cp->cp_ifile_inode, 1);
9ff05123
RK
924 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
925 return 0;
926
927 failed_ibh:
928 return err;
929}
930
931static void nilfs_fill_in_file_bmap(struct inode *ifile,
932 struct nilfs_inode_info *ii)
933
934{
935 struct buffer_head *ibh;
936 struct nilfs_inode *raw_inode;
937
938 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
939 ibh = ii->i_bh;
940 BUG_ON(!ibh);
941 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
942 ibh);
943 nilfs_bmap_write(ii->i_bmap, raw_inode);
944 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
945 }
946}
947
e912a5b6 948static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
9ff05123
RK
949{
950 struct nilfs_inode_info *ii;
951
952 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
e912a5b6 953 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
9ff05123
RK
954 set_bit(NILFS_I_COLLECTED, &ii->i_state);
955 }
9ff05123
RK
956}
957
9ff05123
RK
958static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
959 struct the_nilfs *nilfs)
960{
1e2b68bf
RK
961 struct buffer_head *bh_sr;
962 struct nilfs_super_root *raw_sr;
0c6c44cb 963 unsigned int isz, srsz;
9ff05123 964
1e2b68bf
RK
965 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
966 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
56eb5538
RK
967 isz = nilfs->ns_inode_size;
968 srsz = NILFS_SR_BYTES(isz);
1e2b68bf 969
56eb5538 970 raw_sr->sr_bytes = cpu_to_le16(srsz);
9ff05123
RK
971 raw_sr->sr_nongc_ctime
972 = cpu_to_le64(nilfs_doing_gc() ?
973 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
974 raw_sr->sr_flags = 0;
975
365e215c 976 nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr +
3961f0e2
RK
977 NILFS_SR_DAT_OFFSET(isz), 1);
978 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
979 NILFS_SR_CPFILE_OFFSET(isz), 1);
980 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
981 NILFS_SR_SUFILE_OFFSET(isz), 1);
56eb5538 982 memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz);
9ff05123
RK
983}
984
985static void nilfs_redirty_inodes(struct list_head *head)
986{
987 struct nilfs_inode_info *ii;
988
989 list_for_each_entry(ii, head, i_dirty) {
990 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
991 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
992 }
993}
994
995static void nilfs_drop_collected_inodes(struct list_head *head)
996{
997 struct nilfs_inode_info *ii;
998
999 list_for_each_entry(ii, head, i_dirty) {
1000 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
1001 continue;
1002
b9f66140 1003 clear_bit(NILFS_I_INODE_SYNC, &ii->i_state);
9ff05123
RK
1004 set_bit(NILFS_I_UPDATED, &ii->i_state);
1005 }
1006}
1007
9ff05123
RK
1008static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
1009 struct inode *inode,
1010 struct list_head *listp,
1011 int (*collect)(struct nilfs_sc_info *,
1012 struct buffer_head *,
1013 struct inode *))
1014{
1015 struct buffer_head *bh, *n;
1016 int err = 0;
1017
1018 if (collect) {
1019 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1020 list_del_init(&bh->b_assoc_buffers);
1021 err = collect(sci, bh, inode);
1022 brelse(bh);
1023 if (unlikely(err))
1024 goto dispose_buffers;
1025 }
1026 return 0;
1027 }
1028
1029 dispose_buffers:
1030 while (!list_empty(listp)) {
0cc12838
RK
1031 bh = list_first_entry(listp, struct buffer_head,
1032 b_assoc_buffers);
9ff05123
RK
1033 list_del_init(&bh->b_assoc_buffers);
1034 brelse(bh);
1035 }
1036 return err;
1037}
1038
f30bf3e4
RK
1039static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1040{
1041 /* Remaining number of blocks within segment buffer */
1042 return sci->sc_segbuf_nblocks -
1043 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1044}
1045
9ff05123
RK
1046static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1047 struct inode *inode,
1c613cb9 1048 const struct nilfs_sc_operations *sc_ops)
9ff05123
RK
1049{
1050 LIST_HEAD(data_buffers);
1051 LIST_HEAD(node_buffers);
f30bf3e4 1052 int err;
9ff05123
RK
1053
1054 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
f30bf3e4
RK
1055 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1056
1057 n = nilfs_lookup_dirty_data_buffers(
1058 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1059 if (n > rest) {
1060 err = nilfs_segctor_apply_buffers(
9ff05123 1061 sci, inode, &data_buffers,
f30bf3e4
RK
1062 sc_ops->collect_data);
1063 BUG_ON(!err); /* always receive -E2BIG or true error */
9ff05123
RK
1064 goto break_or_fail;
1065 }
1066 }
1067 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1068
1069 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1070 err = nilfs_segctor_apply_buffers(
1071 sci, inode, &data_buffers, sc_ops->collect_data);
1072 if (unlikely(err)) {
1073 /* dispose node list */
1074 nilfs_segctor_apply_buffers(
1075 sci, inode, &node_buffers, NULL);
1076 goto break_or_fail;
1077 }
1078 sci->sc_stage.flags |= NILFS_CF_NODE;
1079 }
1080 /* Collect node */
1081 err = nilfs_segctor_apply_buffers(
1082 sci, inode, &node_buffers, sc_ops->collect_node);
1083 if (unlikely(err))
1084 goto break_or_fail;
1085
1086 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1087 err = nilfs_segctor_apply_buffers(
1088 sci, inode, &node_buffers, sc_ops->collect_bmap);
1089 if (unlikely(err))
1090 goto break_or_fail;
1091
1092 nilfs_segctor_end_finfo(sci, inode);
1093 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1094
1095 break_or_fail:
1096 return err;
1097}
1098
1099static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1100 struct inode *inode)
1101{
1102 LIST_HEAD(data_buffers);
f30bf3e4
RK
1103 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1104 int err;
9ff05123 1105
f30bf3e4
RK
1106 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1107 sci->sc_dsync_start,
1108 sci->sc_dsync_end);
1109
1110 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1111 nilfs_collect_file_data);
1112 if (!err) {
9ff05123 1113 nilfs_segctor_end_finfo(sci, inode);
f30bf3e4
RK
1114 BUG_ON(n > rest);
1115 /* always receive -E2BIG or true error if n > rest */
1116 }
9ff05123
RK
1117 return err;
1118}
1119
1120static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1121{
e3154e97 1122 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123
RK
1123 struct list_head *head;
1124 struct nilfs_inode_info *ii;
071cb4b8 1125 size_t ndone;
9ff05123
RK
1126 int err = 0;
1127
58497703 1128 switch (nilfs_sc_cstage_get(sci)) {
9ff05123
RK
1129 case NILFS_ST_INIT:
1130 /* Pre-processes */
1131 sci->sc_stage.flags = 0;
1132
1133 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1134 sci->sc_nblk_inc = 0;
1135 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1136 if (mode == SC_LSEG_DSYNC) {
58497703 1137 nilfs_sc_cstage_set(sci, NILFS_ST_DSYNC);
9ff05123
RK
1138 goto dsync_mode;
1139 }
1140 }
1141
1142 sci->sc_stage.dirty_file_ptr = NULL;
1143 sci->sc_stage.gc_inode_ptr = NULL;
1144 if (mode == SC_FLUSH_DAT) {
58497703 1145 nilfs_sc_cstage_set(sci, NILFS_ST_DAT);
9ff05123
RK
1146 goto dat_stage;
1147 }
df561f66
GS
1148 nilfs_sc_cstage_inc(sci);
1149 fallthrough;
9ff05123
RK
1150 case NILFS_ST_GC:
1151 if (nilfs_doing_gc()) {
1152 head = &sci->sc_gc_inodes;
1153 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1154 head, i_dirty);
1155 list_for_each_entry_continue(ii, head, i_dirty) {
1156 err = nilfs_segctor_scan_file(
1157 sci, &ii->vfs_inode,
1158 &nilfs_sc_file_ops);
1159 if (unlikely(err)) {
1160 sci->sc_stage.gc_inode_ptr = list_entry(
1161 ii->i_dirty.prev,
1162 struct nilfs_inode_info,
1163 i_dirty);
1164 goto break_or_fail;
1165 }
1166 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1167 }
1168 sci->sc_stage.gc_inode_ptr = NULL;
1169 }
df561f66
GS
1170 nilfs_sc_cstage_inc(sci);
1171 fallthrough;
9ff05123
RK
1172 case NILFS_ST_FILE:
1173 head = &sci->sc_dirty_files;
1174 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1175 i_dirty);
1176 list_for_each_entry_continue(ii, head, i_dirty) {
1177 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1178
1179 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1180 &nilfs_sc_file_ops);
1181 if (unlikely(err)) {
1182 sci->sc_stage.dirty_file_ptr =
1183 list_entry(ii->i_dirty.prev,
1184 struct nilfs_inode_info,
1185 i_dirty);
1186 goto break_or_fail;
1187 }
1188 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1189 /* XXX: required ? */
1190 }
1191 sci->sc_stage.dirty_file_ptr = NULL;
1192 if (mode == SC_FLUSH_FILE) {
58497703 1193 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
9ff05123
RK
1194 return 0;
1195 }
58497703 1196 nilfs_sc_cstage_inc(sci);
9ff05123 1197 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
df561f66 1198 fallthrough;
9ff05123 1199 case NILFS_ST_IFILE:
e912a5b6 1200 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
9ff05123
RK
1201 &nilfs_sc_file_ops);
1202 if (unlikely(err))
1203 break;
58497703 1204 nilfs_sc_cstage_inc(sci);
9ff05123
RK
1205 /* Creating a checkpoint */
1206 err = nilfs_segctor_create_checkpoint(sci);
1207 if (unlikely(err))
1208 break;
df561f66 1209 fallthrough;
9ff05123
RK
1210 case NILFS_ST_CPFILE:
1211 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1212 &nilfs_sc_file_ops);
1213 if (unlikely(err))
1214 break;
df561f66
GS
1215 nilfs_sc_cstage_inc(sci);
1216 fallthrough;
9ff05123 1217 case NILFS_ST_SUFILE:
071cb4b8
RK
1218 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1219 sci->sc_nfreesegs, &ndone);
1220 if (unlikely(err)) {
1221 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1222 sci->sc_freesegs, ndone,
1223 NULL);
9ff05123 1224 break;
071cb4b8
RK
1225 }
1226 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1227
9ff05123
RK
1228 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1229 &nilfs_sc_file_ops);
1230 if (unlikely(err))
1231 break;
df561f66
GS
1232 nilfs_sc_cstage_inc(sci);
1233 fallthrough;
9ff05123
RK
1234 case NILFS_ST_DAT:
1235 dat_stage:
365e215c 1236 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
9ff05123
RK
1237 &nilfs_sc_dat_ops);
1238 if (unlikely(err))
1239 break;
1240 if (mode == SC_FLUSH_DAT) {
58497703 1241 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
9ff05123
RK
1242 return 0;
1243 }
df561f66
GS
1244 nilfs_sc_cstage_inc(sci);
1245 fallthrough;
9ff05123
RK
1246 case NILFS_ST_SR:
1247 if (mode == SC_LSEG_SR) {
1248 /* Appending a super root */
1249 err = nilfs_segctor_add_super_root(sci);
1250 if (unlikely(err))
1251 break;
1252 }
1253 /* End of a logical segment */
1254 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
58497703 1255 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
9ff05123
RK
1256 return 0;
1257 case NILFS_ST_DSYNC:
1258 dsync_mode:
1259 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
f30bf3e4 1260 ii = sci->sc_dsync_inode;
9ff05123
RK
1261 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1262 break;
1263
1264 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1265 if (unlikely(err))
1266 break;
9ff05123 1267 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
58497703 1268 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
9ff05123
RK
1269 return 0;
1270 case NILFS_ST_DONE:
1271 return 0;
1272 default:
1273 BUG();
1274 }
1275
1276 break_or_fail:
1277 return err;
1278}
1279
a694291a
RK
1280/**
1281 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1282 * @sci: nilfs_sc_info
1283 * @nilfs: nilfs object
1284 */
9ff05123
RK
1285static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1286 struct the_nilfs *nilfs)
1287{
a694291a 1288 struct nilfs_segment_buffer *segbuf, *prev;
9ff05123 1289 __u64 nextnum;
a694291a 1290 int err, alloc = 0;
9ff05123 1291
a694291a
RK
1292 segbuf = nilfs_segbuf_new(sci->sc_super);
1293 if (unlikely(!segbuf))
1294 return -ENOMEM;
9ff05123 1295
a694291a
RK
1296 if (list_empty(&sci->sc_write_logs)) {
1297 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1298 nilfs->ns_pseg_offset, nilfs);
1299 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1300 nilfs_shift_to_next_segment(nilfs);
1301 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1302 }
9ff05123 1303
a694291a
RK
1304 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
1305 nextnum = nilfs->ns_nextnum;
1306
1307 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1308 /* Start from the head of a new full segment */
1309 alloc++;
1310 } else {
1311 /* Continue logs */
1312 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1313 nilfs_segbuf_map_cont(segbuf, prev);
1314 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1315 nextnum = prev->sb_nextnum;
1316
1317 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1318 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1319 segbuf->sb_sum.seg_seq++;
1320 alloc++;
1321 }
9ff05123 1322 }
9ff05123 1323
61a189e9 1324 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
a694291a
RK
1325 if (err)
1326 goto failed;
9ff05123 1327
a694291a 1328 if (alloc) {
cece5520 1329 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
a694291a
RK
1330 if (err)
1331 goto failed;
1332 }
9ff05123
RK
1333 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1334
a694291a
RK
1335 BUG_ON(!list_empty(&sci->sc_segbufs));
1336 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1337 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
cece5520 1338 return 0;
a694291a
RK
1339
1340 failed:
1341 nilfs_segbuf_free(segbuf);
1342 return err;
9ff05123
RK
1343}
1344
1345static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1346 struct the_nilfs *nilfs, int nadd)
1347{
e29df395 1348 struct nilfs_segment_buffer *segbuf, *prev;
9ff05123
RK
1349 struct inode *sufile = nilfs->ns_sufile;
1350 __u64 nextnextnum;
1351 LIST_HEAD(list);
1352 int err, ret, i;
1353
1354 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1355 /*
1356 * Since the segment specified with nextnum might be allocated during
1357 * the previous construction, the buffer including its segusage may
1358 * not be dirty. The following call ensures that the buffer is dirty
1359 * and will pin the buffer on memory until the sufile is written.
1360 */
61a189e9 1361 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
9ff05123
RK
1362 if (unlikely(err))
1363 return err;
1364
1365 for (i = 0; i < nadd; i++) {
1366 /* extend segment info */
1367 err = -ENOMEM;
1368 segbuf = nilfs_segbuf_new(sci->sc_super);
1369 if (unlikely(!segbuf))
1370 goto failed;
1371
1372 /* map this buffer to region of segment on-disk */
cece5520 1373 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
9ff05123
RK
1374 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1375
1376 /* allocate the next next full segment */
1377 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1378 if (unlikely(err))
1379 goto failed_segbuf;
1380
1381 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1382 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1383
1384 list_add_tail(&segbuf->sb_list, &list);
1385 prev = segbuf;
1386 }
0935db74 1387 list_splice_tail(&list, &sci->sc_segbufs);
9ff05123
RK
1388 return 0;
1389
1390 failed_segbuf:
1391 nilfs_segbuf_free(segbuf);
1392 failed:
e29df395 1393 list_for_each_entry(segbuf, &list, sb_list) {
9ff05123 1394 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1f5abe7e 1395 WARN_ON(ret); /* never fails */
9ff05123 1396 }
e29df395 1397 nilfs_destroy_logs(&list);
9ff05123
RK
1398 return err;
1399}
1400
a694291a
RK
1401static void nilfs_free_incomplete_logs(struct list_head *logs,
1402 struct the_nilfs *nilfs)
9ff05123 1403{
a694291a
RK
1404 struct nilfs_segment_buffer *segbuf, *prev;
1405 struct inode *sufile = nilfs->ns_sufile;
9284ad2a 1406 int ret;
9ff05123 1407
a694291a 1408 segbuf = NILFS_FIRST_SEGBUF(logs);
9ff05123 1409 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
a694291a 1410 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1f5abe7e 1411 WARN_ON(ret); /* never fails */
9ff05123 1412 }
9284ad2a 1413 if (atomic_read(&segbuf->sb_err)) {
9ff05123
RK
1414 /* Case 1: The first segment failed */
1415 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
076a378b
RK
1416 /*
1417 * Case 1a: Partial segment appended into an existing
1418 * segment
1419 */
9ff05123
RK
1420 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1421 segbuf->sb_fseg_end);
1422 else /* Case 1b: New full segment */
1423 set_nilfs_discontinued(nilfs);
9ff05123
RK
1424 }
1425
a694291a
RK
1426 prev = segbuf;
1427 list_for_each_entry_continue(segbuf, logs, sb_list) {
1428 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1429 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1430 WARN_ON(ret); /* never fails */
1431 }
9284ad2a
RK
1432 if (atomic_read(&segbuf->sb_err) &&
1433 segbuf->sb_segnum != nilfs->ns_nextnum)
1434 /* Case 2: extended segment (!= next) failed */
a694291a
RK
1435 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1436 prev = segbuf;
9ff05123 1437 }
9ff05123
RK
1438}
1439
1440static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1441 struct inode *sufile)
1442{
1443 struct nilfs_segment_buffer *segbuf;
9ff05123
RK
1444 unsigned long live_blocks;
1445 int ret;
1446
1447 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
9ff05123
RK
1448 live_blocks = segbuf->sb_sum.nblocks +
1449 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
071ec54d
RK
1450 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1451 live_blocks,
1452 sci->sc_seg_ctime);
1453 WARN_ON(ret); /* always succeed because the segusage is dirty */
9ff05123
RK
1454 }
1455}
1456
a694291a 1457static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
9ff05123
RK
1458{
1459 struct nilfs_segment_buffer *segbuf;
9ff05123
RK
1460 int ret;
1461
a694291a 1462 segbuf = NILFS_FIRST_SEGBUF(logs);
071ec54d
RK
1463 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1464 segbuf->sb_pseg_start -
1465 segbuf->sb_fseg_start, 0);
1466 WARN_ON(ret); /* always succeed because the segusage is dirty */
9ff05123 1467
a694291a 1468 list_for_each_entry_continue(segbuf, logs, sb_list) {
071ec54d
RK
1469 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1470 0, 0);
1f5abe7e 1471 WARN_ON(ret); /* always succeed */
9ff05123
RK
1472 }
1473}
1474
1475static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1476 struct nilfs_segment_buffer *last,
1477 struct inode *sufile)
1478{
e29df395 1479 struct nilfs_segment_buffer *segbuf = last;
9ff05123
RK
1480 int ret;
1481
e29df395 1482 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
9ff05123
RK
1483 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1484 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1f5abe7e 1485 WARN_ON(ret);
9ff05123 1486 }
e29df395 1487 nilfs_truncate_logs(&sci->sc_segbufs, last);
9ff05123
RK
1488}
1489
1490
1491static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1492 struct the_nilfs *nilfs, int mode)
1493{
1494 struct nilfs_cstage prev_stage = sci->sc_stage;
1495 int err, nadd = 1;
1496
1497 /* Collection retry loop */
1498 for (;;) {
9ff05123
RK
1499 sci->sc_nblk_this_inc = 0;
1500 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1501
1502 err = nilfs_segctor_reset_segment_buffer(sci);
1503 if (unlikely(err))
1504 goto failed;
1505
1506 err = nilfs_segctor_collect_blocks(sci, mode);
1507 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1508 if (!err)
1509 break;
1510
1511 if (unlikely(err != -E2BIG))
1512 goto failed;
1513
1514 /* The current segment is filled up */
58497703
HM
1515 if (mode != SC_LSEG_SR ||
1516 nilfs_sc_cstage_get(sci) < NILFS_ST_CPFILE)
9ff05123
RK
1517 break;
1518
2d8428ac
RK
1519 nilfs_clear_logs(&sci->sc_segbufs);
1520
071cb4b8
RK
1521 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1522 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1523 sci->sc_freesegs,
1524 sci->sc_nfreesegs,
1525 NULL);
1526 WARN_ON(err); /* do not happen */
70f2fe3a 1527 sci->sc_stage.flags &= ~NILFS_CF_SUFREED;
071cb4b8 1528 }
70f2fe3a
AR
1529
1530 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1531 if (unlikely(err))
1532 return err;
1533
9ff05123
RK
1534 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1535 sci->sc_stage = prev_stage;
1536 }
1537 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1538 return 0;
1539
1540 failed:
1541 return err;
1542}
1543
1544static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1545 struct buffer_head *new_bh)
1546{
1547 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1548
1549 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1550 /* The caller must release old_bh */
1551}
1552
1553static int
1554nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1555 struct nilfs_segment_buffer *segbuf,
1556 int mode)
1557{
1558 struct inode *inode = NULL;
1559 sector_t blocknr;
1560 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1561 unsigned long nblocks = 0, ndatablk = 0;
1c613cb9 1562 const struct nilfs_sc_operations *sc_op = NULL;
9ff05123
RK
1563 struct nilfs_segsum_pointer ssp;
1564 struct nilfs_finfo *finfo = NULL;
1565 union nilfs_binfo binfo;
1566 struct buffer_head *bh, *bh_org;
1567 ino_t ino = 0;
1568 int err = 0;
1569
1570 if (!nfinfo)
1571 goto out;
1572
1573 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1574 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1575 ssp.offset = sizeof(struct nilfs_segment_summary);
1576
1577 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1e2b68bf 1578 if (bh == segbuf->sb_super_root)
9ff05123
RK
1579 break;
1580 if (!finfo) {
1581 finfo = nilfs_segctor_map_segsum_entry(
1582 sci, &ssp, sizeof(*finfo));
1583 ino = le64_to_cpu(finfo->fi_ino);
1584 nblocks = le32_to_cpu(finfo->fi_nblocks);
1585 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1586
6ad4cd7f 1587 inode = bh->b_folio->mapping->host;
9ff05123
RK
1588
1589 if (mode == SC_LSEG_DSYNC)
1590 sc_op = &nilfs_sc_dsync_ops;
1591 else if (ino == NILFS_DAT_INO)
1592 sc_op = &nilfs_sc_dat_ops;
1593 else /* file blocks */
1594 sc_op = &nilfs_sc_file_ops;
1595 }
1596 bh_org = bh;
1597 get_bh(bh_org);
1598 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1599 &binfo);
1600 if (bh != bh_org)
1601 nilfs_list_replace_buffer(bh_org, bh);
1602 brelse(bh_org);
1603 if (unlikely(err))
1604 goto failed_bmap;
1605
1606 if (ndatablk > 0)
1607 sc_op->write_data_binfo(sci, &ssp, &binfo);
1608 else
1609 sc_op->write_node_binfo(sci, &ssp, &binfo);
1610
1611 blocknr++;
1612 if (--nblocks == 0) {
1613 finfo = NULL;
1614 if (--nfinfo == 0)
1615 break;
1616 } else if (ndatablk > 0)
1617 ndatablk--;
1618 }
1619 out:
1620 return 0;
1621
1622 failed_bmap:
9ff05123
RK
1623 return err;
1624}
1625
1626static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1627{
1628 struct nilfs_segment_buffer *segbuf;
1629 int err;
1630
1631 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1632 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1633 if (unlikely(err))
1634 return err;
1635 nilfs_segbuf_fill_in_segsum(segbuf);
1636 }
1637 return 0;
1638}
1639
1cb2d38c 1640static void nilfs_begin_page_io(struct page *page)
9ff05123
RK
1641{
1642 if (!page || PageWriteback(page))
076a378b
RK
1643 /*
1644 * For split b-tree node pages, this function may be called
1645 * twice. We ignore the 2nd or later calls by this check.
1646 */
1cb2d38c 1647 return;
9ff05123
RK
1648
1649 lock_page(page);
1650 clear_page_dirty_for_io(page);
1651 set_page_writeback(page);
1652 unlock_page(page);
9ff05123
RK
1653}
1654
1cb2d38c 1655static void nilfs_segctor_prepare_write(struct nilfs_sc_info *sci)
9ff05123
RK
1656{
1657 struct nilfs_segment_buffer *segbuf;
1658 struct page *bd_page = NULL, *fs_page = NULL;
9ff05123 1659
9ff05123
RK
1660 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1661 struct buffer_head *bh;
1662
1663 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1664 b_assoc_buffers) {
1665 if (bh->b_page != bd_page) {
1666 if (bd_page) {
1667 lock_page(bd_page);
1668 clear_page_dirty_for_io(bd_page);
1669 set_page_writeback(bd_page);
1670 unlock_page(bd_page);
1671 }
1672 bd_page = bh->b_page;
1673 }
1674 }
1675
1676 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1677 b_assoc_buffers) {
7f42ec39 1678 set_buffer_async_write(bh);
1e2b68bf 1679 if (bh == segbuf->sb_super_root) {
9ff05123
RK
1680 if (bh->b_page != bd_page) {
1681 lock_page(bd_page);
1682 clear_page_dirty_for_io(bd_page);
1683 set_page_writeback(bd_page);
1684 unlock_page(bd_page);
1685 bd_page = bh->b_page;
1686 }
1687 break;
1688 }
1689 if (bh->b_page != fs_page) {
1cb2d38c 1690 nilfs_begin_page_io(fs_page);
9ff05123
RK
1691 fs_page = bh->b_page;
1692 }
1693 }
1694 }
1695 if (bd_page) {
1696 lock_page(bd_page);
1697 clear_page_dirty_for_io(bd_page);
1698 set_page_writeback(bd_page);
1699 unlock_page(bd_page);
1700 }
1cb2d38c 1701 nilfs_begin_page_io(fs_page);
9ff05123
RK
1702}
1703
1704static int nilfs_segctor_write(struct nilfs_sc_info *sci,
9c965bac 1705 struct the_nilfs *nilfs)
9ff05123 1706{
d1c6b72a 1707 int ret;
9ff05123 1708
d1c6b72a 1709 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
a694291a
RK
1710 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1711 return ret;
9ff05123
RK
1712}
1713
9ff05123
RK
1714static void nilfs_end_page_io(struct page *page, int err)
1715{
1716 if (!page)
1717 return;
1718
a9777845 1719 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
8227b297
RK
1720 /*
1721 * For b-tree node pages, this function may be called twice
1722 * or more because they might be split in a segment.
1723 */
a9777845
RK
1724 if (PageDirty(page)) {
1725 /*
1726 * For pages holding split b-tree node buffers, dirty
1727 * flag on the buffers may be cleared discretely.
1728 * In that case, the page is once redirtied for
1729 * remaining buffers, and it must be cancelled if
1730 * all the buffers get cleaned later.
1731 */
1732 lock_page(page);
1733 if (nilfs_page_buffers_clean(page))
1734 __nilfs_clear_page_dirty(page);
1735 unlock_page(page);
1736 }
9ff05123 1737 return;
a9777845 1738 }
9ff05123 1739
1cb2d38c
RK
1740 if (!err) {
1741 if (!nilfs_page_buffers_clean(page))
1742 __set_page_dirty_nobuffers(page);
1743 ClearPageError(page);
1744 } else {
1745 __set_page_dirty_nobuffers(page);
1746 SetPageError(page);
9ff05123 1747 }
1cb2d38c
RK
1748
1749 end_page_writeback(page);
9ff05123
RK
1750}
1751
1cb2d38c 1752static void nilfs_abort_logs(struct list_head *logs, int err)
9ff05123
RK
1753{
1754 struct nilfs_segment_buffer *segbuf;
1755 struct page *bd_page = NULL, *fs_page = NULL;
a694291a 1756 struct buffer_head *bh;
9ff05123 1757
a694291a
RK
1758 if (list_empty(logs))
1759 return;
9ff05123 1760
a694291a 1761 list_for_each_entry(segbuf, logs, sb_list) {
9ff05123
RK
1762 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1763 b_assoc_buffers) {
1764 if (bh->b_page != bd_page) {
1765 if (bd_page)
1766 end_page_writeback(bd_page);
1767 bd_page = bh->b_page;
1768 }
1769 }
1770
1771 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1772 b_assoc_buffers) {
7f42ec39 1773 clear_buffer_async_write(bh);
1e2b68bf 1774 if (bh == segbuf->sb_super_root) {
9ff05123
RK
1775 if (bh->b_page != bd_page) {
1776 end_page_writeback(bd_page);
1777 bd_page = bh->b_page;
1778 }
1779 break;
1780 }
1781 if (bh->b_page != fs_page) {
1782 nilfs_end_page_io(fs_page, err);
9ff05123
RK
1783 fs_page = bh->b_page;
1784 }
1785 }
1786 }
1787 if (bd_page)
1788 end_page_writeback(bd_page);
1789
1790 nilfs_end_page_io(fs_page, err);
a694291a
RK
1791}
1792
1793static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1794 struct the_nilfs *nilfs, int err)
1795{
1796 LIST_HEAD(logs);
1797 int ret;
1798
1799 list_splice_tail_init(&sci->sc_write_logs, &logs);
1800 ret = nilfs_wait_on_logs(&logs);
1cb2d38c 1801 nilfs_abort_logs(&logs, ret ? : err);
a694291a
RK
1802
1803 list_splice_tail_init(&sci->sc_segbufs, &logs);
1804 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1805 nilfs_free_incomplete_logs(&logs, nilfs);
a694291a
RK
1806
1807 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1808 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1809 sci->sc_freesegs,
1810 sci->sc_nfreesegs,
1811 NULL);
1812 WARN_ON(ret); /* do not happen */
1813 }
1814
1815 nilfs_destroy_logs(&logs);
9ff05123
RK
1816}
1817
1818static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1819 struct nilfs_segment_buffer *segbuf)
1820{
1821 nilfs->ns_segnum = segbuf->sb_segnum;
1822 nilfs->ns_nextnum = segbuf->sb_nextnum;
1823 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1824 + segbuf->sb_sum.nblocks;
1825 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1826 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1827}
1828
1829static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1830{
1831 struct nilfs_segment_buffer *segbuf;
1832 struct page *bd_page = NULL, *fs_page = NULL;
e3154e97 1833 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1e2b68bf 1834 int update_sr = false;
9ff05123 1835
a694291a 1836 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
9ff05123
RK
1837 struct buffer_head *bh;
1838
1839 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1840 b_assoc_buffers) {
1841 set_buffer_uptodate(bh);
1842 clear_buffer_dirty(bh);
1843 if (bh->b_page != bd_page) {
1844 if (bd_page)
1845 end_page_writeback(bd_page);
1846 bd_page = bh->b_page;
1847 }
1848 }
1849 /*
1850 * We assume that the buffers which belong to the same page
1851 * continue over the buffer list.
1852 * Under this assumption, the last BHs of pages is
1853 * identifiable by the discontinuity of bh->b_page
1854 * (page != fs_page).
1855 *
1856 * For B-tree node blocks, however, this assumption is not
1857 * guaranteed. The cleanup code of B-tree node pages needs
1858 * special care.
1859 */
1860 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1861 b_assoc_buffers) {
4ce5c342 1862 const unsigned long set_bits = BIT(BH_Uptodate);
ead8ecff 1863 const unsigned long clear_bits =
4ce5c342
RK
1864 (BIT(BH_Dirty) | BIT(BH_Async_Write) |
1865 BIT(BH_Delay) | BIT(BH_NILFS_Volatile) |
1866 BIT(BH_NILFS_Redirected));
ead8ecff
RK
1867
1868 set_mask_bits(&bh->b_state, clear_bits, set_bits);
1e2b68bf 1869 if (bh == segbuf->sb_super_root) {
9ff05123
RK
1870 if (bh->b_page != bd_page) {
1871 end_page_writeback(bd_page);
1872 bd_page = bh->b_page;
1873 }
1e2b68bf 1874 update_sr = true;
9ff05123
RK
1875 break;
1876 }
1877 if (bh->b_page != fs_page) {
1878 nilfs_end_page_io(fs_page, 0);
1879 fs_page = bh->b_page;
1880 }
1881 }
1882
4762077c
RK
1883 if (!nilfs_segbuf_simplex(segbuf)) {
1884 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
9ff05123
RK
1885 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1886 sci->sc_lseg_stime = jiffies;
1887 }
4762077c 1888 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
9ff05123
RK
1889 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1890 }
1891 }
1892 /*
1893 * Since pages may continue over multiple segment buffers,
1894 * end of the last page must be checked outside of the loop.
1895 */
1896 if (bd_page)
1897 end_page_writeback(bd_page);
1898
1899 nilfs_end_page_io(fs_page, 0);
1900
9ff05123
RK
1901 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1902
c1c1d709 1903 if (nilfs_doing_gc())
9ff05123 1904 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
c1c1d709 1905 else
9ff05123 1906 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
9ff05123
RK
1907
1908 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1909
a694291a 1910 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
9ff05123
RK
1911 nilfs_set_next_segment(nilfs, segbuf);
1912
1913 if (update_sr) {
e2c7617a 1914 nilfs->ns_flushed_device = 0;
9ff05123 1915 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
e339ad31 1916 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
9ff05123 1917
c96fa464 1918 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
9ff05123
RK
1919 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1920 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
a694291a 1921 nilfs_segctor_clear_metadata_dirty(sci);
9ff05123
RK
1922 } else
1923 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1924}
1925
a694291a
RK
1926static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1927{
1928 int ret;
1929
1930 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1931 if (!ret) {
1932 nilfs_segctor_complete_write(sci);
1933 nilfs_destroy_logs(&sci->sc_write_logs);
1934 }
1935 return ret;
1936}
1937
693dd321
RK
1938static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
1939 struct the_nilfs *nilfs)
9ff05123
RK
1940{
1941 struct nilfs_inode_info *ii, *n;
e912a5b6 1942 struct inode *ifile = sci->sc_root->ifile;
9ff05123 1943
693dd321 1944 spin_lock(&nilfs->ns_inode_lock);
9ff05123 1945 retry:
693dd321 1946 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
9ff05123
RK
1947 if (!ii->i_bh) {
1948 struct buffer_head *ibh;
1949 int err;
1950
693dd321 1951 spin_unlock(&nilfs->ns_inode_lock);
9ff05123 1952 err = nilfs_ifile_get_inode_block(
e912a5b6 1953 ifile, ii->vfs_inode.i_ino, &ibh);
9ff05123 1954 if (unlikely(err)) {
a1d0747a
JP
1955 nilfs_warn(sci->sc_super,
1956 "log writer: error %d getting inode block (ino=%lu)",
1957 err, ii->vfs_inode.i_ino);
9ff05123
RK
1958 return err;
1959 }
693dd321 1960 spin_lock(&nilfs->ns_inode_lock);
9ff05123
RK
1961 if (likely(!ii->i_bh))
1962 ii->i_bh = ibh;
1963 else
1964 brelse(ibh);
1965 goto retry;
1966 }
9ff05123 1967
31ccb1f7
AR
1968 // Always redirty the buffer to avoid race condition
1969 mark_buffer_dirty(ii->i_bh);
1970 nilfs_mdt_mark_dirty(ifile);
1971
9ff05123
RK
1972 clear_bit(NILFS_I_QUEUED, &ii->i_state);
1973 set_bit(NILFS_I_BUSY, &ii->i_state);
eaae0f37 1974 list_move_tail(&ii->i_dirty, &sci->sc_dirty_files);
9ff05123 1975 }
693dd321 1976 spin_unlock(&nilfs->ns_inode_lock);
9ff05123 1977
9ff05123
RK
1978 return 0;
1979}
1980
693dd321
RK
1981static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
1982 struct the_nilfs *nilfs)
9ff05123 1983{
9ff05123 1984 struct nilfs_inode_info *ii, *n;
1751e8a6 1985 int during_mount = !(sci->sc_super->s_flags & SB_ACTIVE);
7ef3ff2f 1986 int defer_iput = false;
9ff05123 1987
693dd321 1988 spin_lock(&nilfs->ns_inode_lock);
9ff05123
RK
1989 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
1990 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
6c43f410 1991 test_bit(NILFS_I_DIRTY, &ii->i_state))
9ff05123 1992 continue;
6c43f410 1993
9ff05123
RK
1994 clear_bit(NILFS_I_BUSY, &ii->i_state);
1995 brelse(ii->i_bh);
1996 ii->i_bh = NULL;
7ef3ff2f 1997 list_del_init(&ii->i_dirty);
283ee148 1998 if (!ii->vfs_inode.i_nlink || during_mount) {
7ef3ff2f 1999 /*
283ee148
RK
2000 * Defer calling iput() to avoid deadlocks if
2001 * i_nlink == 0 or mount is not yet finished.
7ef3ff2f
RK
2002 */
2003 list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
2004 defer_iput = true;
2005 } else {
2006 spin_unlock(&nilfs->ns_inode_lock);
2007 iput(&ii->vfs_inode);
2008 spin_lock(&nilfs->ns_inode_lock);
2009 }
9ff05123 2010 }
693dd321 2011 spin_unlock(&nilfs->ns_inode_lock);
7ef3ff2f
RK
2012
2013 if (defer_iput)
2014 schedule_work(&sci->sc_iput_work);
9ff05123
RK
2015}
2016
9ff05123
RK
2017/*
2018 * Main procedure of segment constructor
2019 */
2020static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2021{
e3154e97 2022 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1e2b68bf 2023 int err;
9ff05123 2024
58497703 2025 nilfs_sc_cstage_set(sci, NILFS_ST_INIT);
6c43f410 2026 sci->sc_cno = nilfs->ns_cno;
9ff05123 2027
693dd321 2028 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
9ff05123
RK
2029 if (unlikely(err))
2030 goto out;
2031
e912a5b6 2032 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
9ff05123
RK
2033 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2034
2035 if (nilfs_segctor_clean(sci))
2036 goto out;
2037
2038 do {
2039 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2040
2041 err = nilfs_segctor_begin_construction(sci, nilfs);
2042 if (unlikely(err))
2043 goto out;
2044
2045 /* Update time stamp */
fb04b91b 2046 sci->sc_seg_ctime = ktime_get_real_seconds();
9ff05123
RK
2047
2048 err = nilfs_segctor_collect(sci, nilfs, mode);
2049 if (unlikely(err))
2050 goto failed;
2051
9ff05123 2052 /* Avoid empty segment */
58497703 2053 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE &&
4762077c 2054 nilfs_segbuf_empty(sci->sc_curseg)) {
a694291a 2055 nilfs_segctor_abort_construction(sci, nilfs, 1);
9ff05123
RK
2056 goto out;
2057 }
2058
2059 err = nilfs_segctor_assign(sci, mode);
2060 if (unlikely(err))
2061 goto failed;
2062
9ff05123 2063 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
e912a5b6 2064 nilfs_segctor_fill_in_file_bmap(sci);
9ff05123 2065
1e2b68bf 2066 if (mode == SC_LSEG_SR &&
58497703 2067 nilfs_sc_cstage_get(sci) >= NILFS_ST_CPFILE) {
9ff05123
RK
2068 err = nilfs_segctor_fill_in_checkpoint(sci);
2069 if (unlikely(err))
a694291a 2070 goto failed_to_write;
9ff05123
RK
2071
2072 nilfs_segctor_fill_in_super_root(sci, nilfs);
2073 }
2074 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2075
2076 /* Write partial segments */
1cb2d38c 2077 nilfs_segctor_prepare_write(sci);
aaed1d5b
RK
2078
2079 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2080 nilfs->ns_crc_seed);
9ff05123 2081
9c965bac 2082 err = nilfs_segctor_write(sci, nilfs);
9ff05123
RK
2083 if (unlikely(err))
2084 goto failed_to_write;
2085
58497703 2086 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE ||
09cbfeaf 2087 nilfs->ns_blocksize_bits != PAGE_SHIFT) {
a694291a
RK
2088 /*
2089 * At this point, we avoid double buffering
2090 * for blocksize < pagesize because page dirty
2091 * flag is turned off during write and dirty
2092 * buffers are not properly collected for
2093 * pages crossing over segments.
2094 */
2095 err = nilfs_segctor_wait(sci);
2096 if (err)
2097 goto failed_to_write;
2098 }
58497703 2099 } while (nilfs_sc_cstage_get(sci) != NILFS_ST_DONE);
9ff05123 2100
9ff05123 2101 out:
693dd321 2102 nilfs_segctor_drop_written_files(sci, nilfs);
9ff05123
RK
2103 return err;
2104
2105 failed_to_write:
9ff05123
RK
2106 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2107 nilfs_redirty_inodes(&sci->sc_dirty_files);
9ff05123
RK
2108
2109 failed:
2110 if (nilfs_doing_gc())
2111 nilfs_redirty_inodes(&sci->sc_gc_inodes);
a694291a 2112 nilfs_segctor_abort_construction(sci, nilfs, err);
9ff05123
RK
2113 goto out;
2114}
2115
2116/**
9ccf56c1 2117 * nilfs_segctor_start_timer - set timer of background write
9ff05123
RK
2118 * @sci: nilfs_sc_info
2119 *
2120 * If the timer has already been set, it ignores the new request.
2121 * This function MUST be called within a section locking the segment
2122 * semaphore.
2123 */
2124static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2125{
2126 spin_lock(&sci->sc_state_lock);
fdce895e
LH
2127 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2128 sci->sc_timer.expires = jiffies + sci->sc_interval;
2129 add_timer(&sci->sc_timer);
9ff05123
RK
2130 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2131 }
2132 spin_unlock(&sci->sc_state_lock);
2133}
2134
2135static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2136{
2137 spin_lock(&sci->sc_state_lock);
4ce5c342 2138 if (!(sci->sc_flush_request & BIT(bn))) {
9ff05123
RK
2139 unsigned long prev_req = sci->sc_flush_request;
2140
4ce5c342 2141 sci->sc_flush_request |= BIT(bn);
9ff05123
RK
2142 if (!prev_req)
2143 wake_up(&sci->sc_wait_daemon);
2144 }
2145 spin_unlock(&sci->sc_state_lock);
2146}
2147
2148/**
2149 * nilfs_flush_segment - trigger a segment construction for resource control
2150 * @sb: super block
2151 * @ino: inode number of the file to be flushed out.
2152 */
2153void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2154{
e3154e97 2155 struct the_nilfs *nilfs = sb->s_fs_info;
3fd3fe5a 2156 struct nilfs_sc_info *sci = nilfs->ns_writer;
9ff05123
RK
2157
2158 if (!sci || nilfs_doing_construction())
2159 return;
2160 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2161 /* assign bit 0 to data files */
2162}
2163
9ff05123 2164struct nilfs_segctor_wait_request {
ac6424b9 2165 wait_queue_entry_t wq;
9ff05123
RK
2166 __u32 seq;
2167 int err;
2168 atomic_t done;
2169};
2170
2171static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2172{
2173 struct nilfs_segctor_wait_request wait_req;
2174 int err = 0;
2175
2176 spin_lock(&sci->sc_state_lock);
2177 init_wait(&wait_req.wq);
2178 wait_req.err = 0;
2179 atomic_set(&wait_req.done, 0);
2180 wait_req.seq = ++sci->sc_seq_request;
2181 spin_unlock(&sci->sc_state_lock);
2182
2183 init_waitqueue_entry(&wait_req.wq, current);
2184 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2185 set_current_state(TASK_INTERRUPTIBLE);
2186 wake_up(&sci->sc_wait_daemon);
2187
2188 for (;;) {
2189 if (atomic_read(&wait_req.done)) {
2190 err = wait_req.err;
2191 break;
2192 }
2193 if (!signal_pending(current)) {
2194 schedule();
2195 continue;
2196 }
2197 err = -ERESTARTSYS;
2198 break;
2199 }
2200 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2201 return err;
2202}
2203
2204static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2205{
2206 struct nilfs_segctor_wait_request *wrq, *n;
2207 unsigned long flags;
2208
2209 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2055da97 2210 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.head, wq.entry) {
9ff05123
RK
2211 if (!atomic_read(&wrq->done) &&
2212 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2213 wrq->err = err;
2214 atomic_set(&wrq->done, 1);
2215 }
2216 if (atomic_read(&wrq->done)) {
2217 wrq->wq.func(&wrq->wq,
2218 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2219 0, NULL);
2220 }
2221 }
2222 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2223}
2224
2225/**
2226 * nilfs_construct_segment - construct a logical segment
2227 * @sb: super block
2228 *
300563e6 2229 * Return Value: On success, 0 is returned. On errors, one of the following
9ff05123
RK
2230 * negative error code is returned.
2231 *
2232 * %-EROFS - Read only filesystem.
2233 *
2234 * %-EIO - I/O error
2235 *
2236 * %-ENOSPC - No space left on device (only in a panic state).
2237 *
2238 * %-ERESTARTSYS - Interrupted.
2239 *
2240 * %-ENOMEM - Insufficient memory available.
2241 */
2242int nilfs_construct_segment(struct super_block *sb)
2243{
e3154e97 2244 struct the_nilfs *nilfs = sb->s_fs_info;
3fd3fe5a 2245 struct nilfs_sc_info *sci = nilfs->ns_writer;
9ff05123 2246 struct nilfs_transaction_info *ti;
9ff05123 2247
8cccf05f 2248 if (sb_rdonly(sb) || unlikely(!sci))
9ff05123
RK
2249 return -EROFS;
2250
2251 /* A call inside transactions causes a deadlock. */
2252 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2253
da6f7916 2254 return nilfs_segctor_sync(sci);
9ff05123
RK
2255}
2256
2257/**
2258 * nilfs_construct_dsync_segment - construct a data-only logical segment
2259 * @sb: super block
f30bf3e4
RK
2260 * @inode: inode whose data blocks should be written out
2261 * @start: start byte offset
2262 * @end: end byte offset (inclusive)
9ff05123 2263 *
300563e6 2264 * Return Value: On success, 0 is returned. On errors, one of the following
9ff05123
RK
2265 * negative error code is returned.
2266 *
2267 * %-EROFS - Read only filesystem.
2268 *
2269 * %-EIO - I/O error
2270 *
2271 * %-ENOSPC - No space left on device (only in a panic state).
2272 *
2273 * %-ERESTARTSYS - Interrupted.
2274 *
2275 * %-ENOMEM - Insufficient memory available.
2276 */
f30bf3e4
RK
2277int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2278 loff_t start, loff_t end)
9ff05123 2279{
e3154e97 2280 struct the_nilfs *nilfs = sb->s_fs_info;
3fd3fe5a 2281 struct nilfs_sc_info *sci = nilfs->ns_writer;
9ff05123
RK
2282 struct nilfs_inode_info *ii;
2283 struct nilfs_transaction_info ti;
2284 int err = 0;
2285
8cccf05f 2286 if (sb_rdonly(sb) || unlikely(!sci))
9ff05123
RK
2287 return -EROFS;
2288
f7545144 2289 nilfs_transaction_lock(sb, &ti, 0);
9ff05123
RK
2290
2291 ii = NILFS_I(inode);
b9f66140 2292 if (test_bit(NILFS_I_INODE_SYNC, &ii->i_state) ||
3b2ce58b 2293 nilfs_test_opt(nilfs, STRICT_ORDER) ||
9ff05123 2294 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
3b2ce58b 2295 nilfs_discontinued(nilfs)) {
f7545144 2296 nilfs_transaction_unlock(sb);
9ff05123
RK
2297 err = nilfs_segctor_sync(sci);
2298 return err;
2299 }
2300
693dd321 2301 spin_lock(&nilfs->ns_inode_lock);
9ff05123
RK
2302 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2303 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
693dd321 2304 spin_unlock(&nilfs->ns_inode_lock);
f7545144 2305 nilfs_transaction_unlock(sb);
9ff05123
RK
2306 return 0;
2307 }
693dd321 2308 spin_unlock(&nilfs->ns_inode_lock);
f30bf3e4
RK
2309 sci->sc_dsync_inode = ii;
2310 sci->sc_dsync_start = start;
2311 sci->sc_dsync_end = end;
9ff05123
RK
2312
2313 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
e2c7617a
AR
2314 if (!err)
2315 nilfs->ns_flushed_device = 0;
9ff05123 2316
f7545144 2317 nilfs_transaction_unlock(sb);
9ff05123
RK
2318 return err;
2319}
2320
9ff05123 2321#define FLUSH_FILE_BIT (0x1) /* data file only */
4ce5c342 2322#define FLUSH_DAT_BIT BIT(NILFS_DAT_INO) /* DAT only */
9ff05123 2323
dcd76186
RK
2324/**
2325 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2326 * @sci: segment constructor object
2327 */
2328static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
9ff05123 2329{
9ff05123 2330 spin_lock(&sci->sc_state_lock);
dcd76186 2331 sci->sc_seq_accepted = sci->sc_seq_request;
9ff05123 2332 spin_unlock(&sci->sc_state_lock);
fdce895e 2333 del_timer_sync(&sci->sc_timer);
9ff05123
RK
2334}
2335
dcd76186
RK
2336/**
2337 * nilfs_segctor_notify - notify the result of request to caller threads
2338 * @sci: segment constructor object
2339 * @mode: mode of log forming
2340 * @err: error code to be notified
2341 */
2342static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
9ff05123
RK
2343{
2344 /* Clear requests (even when the construction failed) */
2345 spin_lock(&sci->sc_state_lock);
2346
dcd76186 2347 if (mode == SC_LSEG_SR) {
aeda7f63 2348 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
dcd76186
RK
2349 sci->sc_seq_done = sci->sc_seq_accepted;
2350 nilfs_segctor_wakeup(sci, err);
9ff05123 2351 sci->sc_flush_request = 0;
aeda7f63 2352 } else {
dcd76186 2353 if (mode == SC_FLUSH_FILE)
aeda7f63 2354 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
dcd76186 2355 else if (mode == SC_FLUSH_DAT)
aeda7f63
RK
2356 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2357
2358 /* re-enable timer if checkpoint creation was not done */
fdce895e
LH
2359 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2360 time_before(jiffies, sci->sc_timer.expires))
2361 add_timer(&sci->sc_timer);
aeda7f63 2362 }
9ff05123
RK
2363 spin_unlock(&sci->sc_state_lock);
2364}
2365
dcd76186
RK
2366/**
2367 * nilfs_segctor_construct - form logs and write them to disk
2368 * @sci: segment constructor object
2369 * @mode: mode of log forming
2370 */
2371static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
9ff05123 2372{
e3154e97 2373 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
d26493b6 2374 struct nilfs_super_block **sbp;
9ff05123
RK
2375 int err = 0;
2376
dcd76186
RK
2377 nilfs_segctor_accept(sci);
2378
9ff05123 2379 if (nilfs_discontinued(nilfs))
dcd76186
RK
2380 mode = SC_LSEG_SR;
2381 if (!nilfs_segctor_confirm(sci))
2382 err = nilfs_segctor_do_construct(sci, mode);
2383
9ff05123 2384 if (likely(!err)) {
dcd76186 2385 if (mode != SC_FLUSH_DAT)
9ff05123
RK
2386 atomic_set(&nilfs->ns_ndirtyblks, 0);
2387 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2388 nilfs_discontinued(nilfs)) {
2389 down_write(&nilfs->ns_sem);
d26493b6 2390 err = -EIO;
f7545144 2391 sbp = nilfs_prepare_super(sci->sc_super,
b2ac86e1
JS
2392 nilfs_sb_will_flip(nilfs));
2393 if (likely(sbp)) {
2394 nilfs_set_log_cursor(sbp[0], nilfs);
f7545144
RK
2395 err = nilfs_commit_super(sci->sc_super,
2396 NILFS_SB_COMMIT);
b2ac86e1 2397 }
9ff05123
RK
2398 up_write(&nilfs->ns_sem);
2399 }
2400 }
dcd76186
RK
2401
2402 nilfs_segctor_notify(sci, mode, err);
9ff05123
RK
2403 return err;
2404}
2405
7554e9c4 2406static void nilfs_construction_timeout(struct timer_list *t)
9ff05123 2407{
7554e9c4 2408 struct nilfs_sc_info *sci = from_timer(sci, t, sc_timer);
4ad364ca 2409
7554e9c4 2410 wake_up_process(sci->sc_timer_task);
9ff05123
RK
2411}
2412
2413static void
2414nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2415{
2416 struct nilfs_inode_info *ii, *n;
2417
2418 list_for_each_entry_safe(ii, n, head, i_dirty) {
2419 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2420 continue;
9ff05123 2421 list_del_init(&ii->i_dirty);
fbb24a3a 2422 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
e897be17 2423 nilfs_btnode_cache_clear(ii->i_assoc_inode->i_mapping);
263d90ce 2424 iput(&ii->vfs_inode);
9ff05123
RK
2425 }
2426}
2427
4f6b8288
RK
2428int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2429 void **kbufs)
9ff05123 2430{
e3154e97 2431 struct the_nilfs *nilfs = sb->s_fs_info;
3fd3fe5a 2432 struct nilfs_sc_info *sci = nilfs->ns_writer;
9ff05123 2433 struct nilfs_transaction_info ti;
9ff05123
RK
2434 int err;
2435
2436 if (unlikely(!sci))
2437 return -EROFS;
2438
f7545144 2439 nilfs_transaction_lock(sb, &ti, 1);
9ff05123 2440
c1c1d709 2441 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
9ff05123
RK
2442 if (unlikely(err))
2443 goto out_unlock;
071cb4b8 2444
4f6b8288 2445 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
c1c1d709
RK
2446 if (unlikely(err)) {
2447 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
9ff05123 2448 goto out_unlock;
c1c1d709 2449 }
9ff05123 2450
071cb4b8
RK
2451 sci->sc_freesegs = kbufs[4];
2452 sci->sc_nfreesegs = argv[4].v_nmembs;
0935db74 2453 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
9ff05123
RK
2454
2455 for (;;) {
dcd76186 2456 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
9ff05123 2457 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
9ff05123
RK
2458
2459 if (likely(!err))
2460 break;
2461
a1d0747a 2462 nilfs_warn(sb, "error %d cleaning segments", err);
9ff05123
RK
2463 set_current_state(TASK_INTERRUPTIBLE);
2464 schedule_timeout(sci->sc_interval);
2465 }
3b2ce58b 2466 if (nilfs_test_opt(nilfs, DISCARD)) {
e902ec99
JS
2467 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2468 sci->sc_nfreesegs);
2469 if (ret) {
a1d0747a
JP
2470 nilfs_warn(sb,
2471 "error %d on discard request, turning discards off for the device",
2472 ret);
3b2ce58b 2473 nilfs_clear_opt(nilfs, DISCARD);
e902ec99
JS
2474 }
2475 }
9ff05123
RK
2476
2477 out_unlock:
071cb4b8
RK
2478 sci->sc_freesegs = NULL;
2479 sci->sc_nfreesegs = 0;
c1c1d709 2480 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
f7545144 2481 nilfs_transaction_unlock(sb);
9ff05123
RK
2482 return err;
2483}
2484
2485static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2486{
9ff05123 2487 struct nilfs_transaction_info ti;
9ff05123 2488
f7545144 2489 nilfs_transaction_lock(sci->sc_super, &ti, 0);
dcd76186 2490 nilfs_segctor_construct(sci, mode);
9ff05123
RK
2491
2492 /*
2493 * Unclosed segment should be retried. We do this using sc_timer.
2494 * Timeout of sc_timer will invoke complete construction which leads
2495 * to close the current logical segment.
2496 */
2497 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2498 nilfs_segctor_start_timer(sci);
2499
f7545144 2500 nilfs_transaction_unlock(sci->sc_super);
9ff05123
RK
2501}
2502
2503static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2504{
2505 int mode = 0;
9ff05123
RK
2506
2507 spin_lock(&sci->sc_state_lock);
2508 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2509 SC_FLUSH_DAT : SC_FLUSH_FILE;
2510 spin_unlock(&sci->sc_state_lock);
2511
2512 if (mode) {
09ef29e0 2513 nilfs_segctor_do_construct(sci, mode);
9ff05123
RK
2514
2515 spin_lock(&sci->sc_state_lock);
2516 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2517 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2518 spin_unlock(&sci->sc_state_lock);
2519 }
2520 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2521}
2522
2523static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2524{
2525 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2526 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2527 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2528 return SC_FLUSH_FILE;
2529 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2530 return SC_FLUSH_DAT;
2531 }
2532 return SC_LSEG_SR;
2533}
2534
2535/**
2536 * nilfs_segctor_thread - main loop of the segment constructor thread.
2537 * @arg: pointer to a struct nilfs_sc_info.
2538 *
2539 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2540 * to execute segment constructions.
2541 */
2542static int nilfs_segctor_thread(void *arg)
2543{
2544 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
e3154e97 2545 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123
RK
2546 int timeout = 0;
2547
7554e9c4 2548 sci->sc_timer_task = current;
9ff05123
RK
2549
2550 /* start sync. */
2551 sci->sc_task = current;
2552 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
a1d0747a
JP
2553 nilfs_info(sci->sc_super,
2554 "segctord starting. Construction interval = %lu seconds, CP frequency < %lu seconds",
2555 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
9ff05123
RK
2556
2557 spin_lock(&sci->sc_state_lock);
2558 loop:
2559 for (;;) {
2560 int mode;
2561
2562 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2563 goto end_thread;
2564
2565 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2566 mode = SC_LSEG_SR;
7f00184e 2567 else if (sci->sc_flush_request)
9ff05123 2568 mode = nilfs_segctor_flush_mode(sci);
7f00184e
RK
2569 else
2570 break;
9ff05123
RK
2571
2572 spin_unlock(&sci->sc_state_lock);
2573 nilfs_segctor_thread_construct(sci, mode);
2574 spin_lock(&sci->sc_state_lock);
2575 timeout = 0;
2576 }
2577
2578
2579 if (freezing(current)) {
2580 spin_unlock(&sci->sc_state_lock);
a0acae0e 2581 try_to_freeze();
9ff05123
RK
2582 spin_lock(&sci->sc_state_lock);
2583 } else {
2584 DEFINE_WAIT(wait);
2585 int should_sleep = 1;
2586
2587 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2588 TASK_INTERRUPTIBLE);
2589
2590 if (sci->sc_seq_request != sci->sc_seq_done)
2591 should_sleep = 0;
2592 else if (sci->sc_flush_request)
2593 should_sleep = 0;
2594 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2595 should_sleep = time_before(jiffies,
fdce895e 2596 sci->sc_timer.expires);
9ff05123
RK
2597
2598 if (should_sleep) {
2599 spin_unlock(&sci->sc_state_lock);
2600 schedule();
2601 spin_lock(&sci->sc_state_lock);
2602 }
2603 finish_wait(&sci->sc_wait_daemon, &wait);
2604 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
fdce895e 2605 time_after_eq(jiffies, sci->sc_timer.expires));
e605f0a7
RK
2606
2607 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
1dfa2710 2608 set_nilfs_discontinued(nilfs);
9ff05123
RK
2609 }
2610 goto loop;
2611
2612 end_thread:
2613 spin_unlock(&sci->sc_state_lock);
9ff05123
RK
2614
2615 /* end sync. */
2616 sci->sc_task = NULL;
2617 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2618 return 0;
2619}
2620
2621static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2622{
2623 struct task_struct *t;
2624
2625 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2626 if (IS_ERR(t)) {
2627 int err = PTR_ERR(t);
2628
a1d0747a
JP
2629 nilfs_err(sci->sc_super, "error %d creating segctord thread",
2630 err);
9ff05123
RK
2631 return err;
2632 }
2633 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2634 return 0;
2635}
2636
2637static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
6b81e14e
JS
2638 __acquires(&sci->sc_state_lock)
2639 __releases(&sci->sc_state_lock)
9ff05123
RK
2640{
2641 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2642
2643 while (sci->sc_task) {
2644 wake_up(&sci->sc_wait_daemon);
2645 spin_unlock(&sci->sc_state_lock);
2646 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2647 spin_lock(&sci->sc_state_lock);
2648 }
2649}
2650
9ff05123
RK
2651/*
2652 * Setup & clean-up functions
2653 */
f7545144 2654static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
e912a5b6 2655 struct nilfs_root *root)
9ff05123 2656{
e3154e97 2657 struct the_nilfs *nilfs = sb->s_fs_info;
9ff05123
RK
2658 struct nilfs_sc_info *sci;
2659
2660 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2661 if (!sci)
2662 return NULL;
2663
f7545144 2664 sci->sc_super = sb;
9ff05123 2665
e912a5b6
RK
2666 nilfs_get_root(root);
2667 sci->sc_root = root;
2668
9ff05123
RK
2669 init_waitqueue_head(&sci->sc_wait_request);
2670 init_waitqueue_head(&sci->sc_wait_daemon);
2671 init_waitqueue_head(&sci->sc_wait_task);
2672 spin_lock_init(&sci->sc_state_lock);
2673 INIT_LIST_HEAD(&sci->sc_dirty_files);
2674 INIT_LIST_HEAD(&sci->sc_segbufs);
a694291a 2675 INIT_LIST_HEAD(&sci->sc_write_logs);
9ff05123 2676 INIT_LIST_HEAD(&sci->sc_gc_inodes);
7ef3ff2f
RK
2677 INIT_LIST_HEAD(&sci->sc_iput_queue);
2678 INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
7554e9c4 2679 timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0);
9ff05123
RK
2680
2681 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2682 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2683 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2684
574e6c31 2685 if (nilfs->ns_interval)
071d73cf 2686 sci->sc_interval = HZ * nilfs->ns_interval;
574e6c31
RK
2687 if (nilfs->ns_watermark)
2688 sci->sc_watermark = nilfs->ns_watermark;
9ff05123
RK
2689 return sci;
2690}
2691
2692static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2693{
2694 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2695
076a378b
RK
2696 /*
2697 * The segctord thread was stopped and its timer was removed.
2698 * But some tasks remain.
2699 */
9ff05123 2700 do {
9ff05123 2701 struct nilfs_transaction_info ti;
9ff05123 2702
f7545144 2703 nilfs_transaction_lock(sci->sc_super, &ti, 0);
dcd76186 2704 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
f7545144 2705 nilfs_transaction_unlock(sci->sc_super);
9ff05123 2706
7ef3ff2f
RK
2707 flush_work(&sci->sc_iput_work);
2708
9ff05123
RK
2709 } while (ret && retrycount-- > 0);
2710}
2711
2712/**
2713 * nilfs_segctor_destroy - destroy the segment constructor.
2714 * @sci: nilfs_sc_info
2715 *
2716 * nilfs_segctor_destroy() kills the segctord thread and frees
2717 * the nilfs_sc_info struct.
2718 * Caller must hold the segment semaphore.
2719 */
2720static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2721{
e3154e97 2722 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
9ff05123
RK
2723 int flag;
2724
693dd321 2725 up_write(&nilfs->ns_segctor_sem);
9ff05123
RK
2726
2727 spin_lock(&sci->sc_state_lock);
2728 nilfs_segctor_kill_thread(sci);
2729 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2730 || sci->sc_seq_request != sci->sc_seq_done);
2731 spin_unlock(&sci->sc_state_lock);
2732
7ef3ff2f
RK
2733 if (flush_work(&sci->sc_iput_work))
2734 flag = true;
2735
3256a055 2736 if (flag || !nilfs_segctor_confirm(sci))
9ff05123
RK
2737 nilfs_segctor_write_out(sci);
2738
9ff05123 2739 if (!list_empty(&sci->sc_dirty_files)) {
a1d0747a
JP
2740 nilfs_warn(sci->sc_super,
2741 "disposed unprocessed dirty file(s) when stopping log writer");
693dd321 2742 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
9ff05123 2743 }
9ff05123 2744
7ef3ff2f 2745 if (!list_empty(&sci->sc_iput_queue)) {
a1d0747a
JP
2746 nilfs_warn(sci->sc_super,
2747 "disposed unprocessed inode(s) in iput queue when stopping log writer");
7ef3ff2f
RK
2748 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
2749 }
2750
1f5abe7e 2751 WARN_ON(!list_empty(&sci->sc_segbufs));
a694291a 2752 WARN_ON(!list_empty(&sci->sc_write_logs));
9ff05123 2753
e912a5b6
RK
2754 nilfs_put_root(sci->sc_root);
2755
693dd321 2756 down_write(&nilfs->ns_segctor_sem);
9ff05123 2757
292a089d 2758 timer_shutdown_sync(&sci->sc_timer);
9ff05123
RK
2759 kfree(sci);
2760}
2761
2762/**
f7545144
RK
2763 * nilfs_attach_log_writer - attach log writer
2764 * @sb: super block instance
e912a5b6 2765 * @root: root object of the current filesystem tree
9ff05123 2766 *
f7545144
RK
2767 * This allocates a log writer object, initializes it, and starts the
2768 * log writer.
9ff05123
RK
2769 *
2770 * Return Value: On success, 0 is returned. On error, one of the following
2771 * negative error code is returned.
2772 *
2773 * %-ENOMEM - Insufficient memory available.
2774 */
f7545144 2775int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
9ff05123 2776{
e3154e97 2777 struct the_nilfs *nilfs = sb->s_fs_info;
9ff05123
RK
2778 int err;
2779
3fd3fe5a 2780 if (nilfs->ns_writer) {
fe5f171b 2781 /*
8cccf05f
RK
2782 * This happens if the filesystem is made read-only by
2783 * __nilfs_error or nilfs_remount and then remounted
2784 * read/write. In these cases, reuse the existing
2785 * writer.
fe5f171b 2786 */
8cccf05f 2787 return 0;
fe5f171b
RK
2788 }
2789
f7545144 2790 nilfs->ns_writer = nilfs_segctor_new(sb, root);
3fd3fe5a 2791 if (!nilfs->ns_writer)
9ff05123
RK
2792 return -ENOMEM;
2793
8301c719
RK
2794 inode_attach_wb(nilfs->ns_bdev->bd_inode, NULL);
2795
3fd3fe5a 2796 err = nilfs_segctor_start_thread(nilfs->ns_writer);
d0d51a97
RK
2797 if (unlikely(err))
2798 nilfs_detach_log_writer(sb);
2799
9ff05123
RK
2800 return err;
2801}
2802
2803/**
f7545144
RK
2804 * nilfs_detach_log_writer - destroy log writer
2805 * @sb: super block instance
9ff05123 2806 *
f7545144
RK
2807 * This kills log writer daemon, frees the log writer object, and
2808 * destroys list of dirty files.
9ff05123 2809 */
f7545144 2810void nilfs_detach_log_writer(struct super_block *sb)
9ff05123 2811{
e3154e97 2812 struct the_nilfs *nilfs = sb->s_fs_info;
9ff05123
RK
2813 LIST_HEAD(garbage_list);
2814
2815 down_write(&nilfs->ns_segctor_sem);
3fd3fe5a
RK
2816 if (nilfs->ns_writer) {
2817 nilfs_segctor_destroy(nilfs->ns_writer);
2818 nilfs->ns_writer = NULL;
9ff05123
RK
2819 }
2820
2821 /* Force to free the list of dirty files */
693dd321
RK
2822 spin_lock(&nilfs->ns_inode_lock);
2823 if (!list_empty(&nilfs->ns_dirty_files)) {
2824 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
a1d0747a
JP
2825 nilfs_warn(sb,
2826 "disposed unprocessed dirty file(s) when detaching log writer");
9ff05123 2827 }
693dd321 2828 spin_unlock(&nilfs->ns_inode_lock);
9ff05123
RK
2829 up_write(&nilfs->ns_segctor_sem);
2830
693dd321 2831 nilfs_dispose_list(nilfs, &garbage_list, 1);
9ff05123 2832}