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