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