nilfs2: make snapshots in checkpoint tree exportable
[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
766static int nilfs_test_metadata_dirty(struct nilfs_sb_info *sbi)
767{
768 struct the_nilfs *nilfs = sbi->s_nilfs;
769 int ret = 0;
770
771 if (nilfs_mdt_fetch_dirty(sbi->s_ifile))
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
796 if (nilfs_test_metadata_dirty(sbi))
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
812 nilfs_mdt_clear_dirty(sbi->s_ifile);
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 =
859 cpu_to_le64(atomic_read(&sbi->s_inodes_count));
860 raw_cp->cp_blocks_count =
861 cpu_to_le64(atomic_read(&sbi->s_blocks_count));
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
9ff05123
RK
872 nilfs_write_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode, 1);
873 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
874 return 0;
875
876 failed_ibh:
877 return err;
878}
879
880static void nilfs_fill_in_file_bmap(struct inode *ifile,
881 struct nilfs_inode_info *ii)
882
883{
884 struct buffer_head *ibh;
885 struct nilfs_inode *raw_inode;
886
887 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
888 ibh = ii->i_bh;
889 BUG_ON(!ibh);
890 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
891 ibh);
892 nilfs_bmap_write(ii->i_bmap, raw_inode);
893 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
894 }
895}
896
897static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci,
898 struct inode *ifile)
899{
900 struct nilfs_inode_info *ii;
901
902 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
903 nilfs_fill_in_file_bmap(ifile, ii);
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:
1146 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
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);
1e2b68bf 1911 if (bh == segbuf->sb_super_root) {
9ff05123
RK
1912 if (bh->b_page != bd_page) {
1913 end_page_writeback(bd_page);
1914 bd_page = bh->b_page;
1915 }
1e2b68bf 1916 update_sr = true;
9ff05123
RK
1917 break;
1918 }
1919 if (bh->b_page != fs_page) {
1920 nilfs_end_page_io(fs_page, 0);
1921 fs_page = bh->b_page;
1922 }
1923 }
1924
4762077c
RK
1925 if (!nilfs_segbuf_simplex(segbuf)) {
1926 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
9ff05123
RK
1927 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1928 sci->sc_lseg_stime = jiffies;
1929 }
4762077c 1930 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
9ff05123
RK
1931 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1932 }
1933 }
1934 /*
1935 * Since pages may continue over multiple segment buffers,
1936 * end of the last page must be checked outside of the loop.
1937 */
1938 if (bd_page)
1939 end_page_writeback(bd_page);
1940
1941 nilfs_end_page_io(fs_page, 0);
1942
1943 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
1944
1945 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1946
1947 if (nilfs_doing_gc()) {
1948 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
1949 if (update_sr)
1950 nilfs_commit_gcdat_inode(nilfs);
1088dcf4 1951 } else
9ff05123 1952 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
9ff05123
RK
1953
1954 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1955
a694291a 1956 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
9ff05123
RK
1957 nilfs_set_next_segment(nilfs, segbuf);
1958
1959 if (update_sr) {
1960 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
e339ad31 1961 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
9ff05123 1962
c96fa464 1963 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
9ff05123
RK
1964 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1965 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
a694291a 1966 nilfs_segctor_clear_metadata_dirty(sci);
9ff05123
RK
1967 } else
1968 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1969}
1970
a694291a
RK
1971static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1972{
1973 int ret;
1974
1975 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1976 if (!ret) {
1977 nilfs_segctor_complete_write(sci);
1978 nilfs_destroy_logs(&sci->sc_write_logs);
1979 }
1980 return ret;
1981}
1982
9ff05123
RK
1983static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
1984 struct nilfs_sb_info *sbi)
1985{
1986 struct nilfs_inode_info *ii, *n;
9ff05123
RK
1987
1988 spin_lock(&sbi->s_inode_lock);
1989 retry:
1990 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
1991 if (!ii->i_bh) {
1992 struct buffer_head *ibh;
1993 int err;
1994
1995 spin_unlock(&sbi->s_inode_lock);
1996 err = nilfs_ifile_get_inode_block(
1997 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
1998 if (unlikely(err)) {
1999 nilfs_warning(sbi->s_super, __func__,
2000 "failed to get inode block.\n");
2001 return err;
2002 }
2003 nilfs_mdt_mark_buffer_dirty(ibh);
2004 nilfs_mdt_mark_dirty(sbi->s_ifile);
2005 spin_lock(&sbi->s_inode_lock);
2006 if (likely(!ii->i_bh))
2007 ii->i_bh = ibh;
2008 else
2009 brelse(ibh);
2010 goto retry;
2011 }
9ff05123
RK
2012
2013 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2014 set_bit(NILFS_I_BUSY, &ii->i_state);
2015 list_del(&ii->i_dirty);
2016 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2017 }
2018 spin_unlock(&sbi->s_inode_lock);
2019
9ff05123
RK
2020 return 0;
2021}
2022
2023static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2024 struct nilfs_sb_info *sbi)
2025{
2026 struct nilfs_transaction_info *ti = current->journal_info;
2027 struct nilfs_inode_info *ii, *n;
9ff05123
RK
2028
2029 spin_lock(&sbi->s_inode_lock);
2030 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2031 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
6c43f410 2032 test_bit(NILFS_I_DIRTY, &ii->i_state))
9ff05123 2033 continue;
6c43f410 2034
9ff05123
RK
2035 clear_bit(NILFS_I_BUSY, &ii->i_state);
2036 brelse(ii->i_bh);
2037 ii->i_bh = NULL;
2038 list_del(&ii->i_dirty);
2039 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2040 }
2041 spin_unlock(&sbi->s_inode_lock);
2042}
2043
9ff05123
RK
2044/*
2045 * Main procedure of segment constructor
2046 */
2047static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2048{
2049 struct nilfs_sb_info *sbi = sci->sc_sbi;
2050 struct the_nilfs *nilfs = sbi->s_nilfs;
2051 struct page *failed_page;
1e2b68bf 2052 int err;
9ff05123
RK
2053
2054 sci->sc_stage.scnt = NILFS_ST_INIT;
6c43f410 2055 sci->sc_cno = nilfs->ns_cno;
9ff05123
RK
2056
2057 err = nilfs_segctor_check_in_files(sci, sbi);
2058 if (unlikely(err))
2059 goto out;
2060
2061 if (nilfs_test_metadata_dirty(sbi))
2062 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2063
2064 if (nilfs_segctor_clean(sci))
2065 goto out;
2066
2067 do {
2068 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2069
2070 err = nilfs_segctor_begin_construction(sci, nilfs);
2071 if (unlikely(err))
2072 goto out;
2073
2074 /* Update time stamp */
2075 sci->sc_seg_ctime = get_seconds();
2076
2077 err = nilfs_segctor_collect(sci, nilfs, mode);
2078 if (unlikely(err))
2079 goto failed;
2080
9ff05123
RK
2081 /* Avoid empty segment */
2082 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
4762077c 2083 nilfs_segbuf_empty(sci->sc_curseg)) {
a694291a 2084 nilfs_segctor_abort_construction(sci, nilfs, 1);
9ff05123
RK
2085 goto out;
2086 }
2087
2088 err = nilfs_segctor_assign(sci, mode);
2089 if (unlikely(err))
2090 goto failed;
2091
9ff05123
RK
2092 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2093 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2094
1e2b68bf
RK
2095 if (mode == SC_LSEG_SR &&
2096 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
9ff05123
RK
2097 err = nilfs_segctor_fill_in_checkpoint(sci);
2098 if (unlikely(err))
a694291a 2099 goto failed_to_write;
9ff05123
RK
2100
2101 nilfs_segctor_fill_in_super_root(sci, nilfs);
2102 }
2103 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2104
2105 /* Write partial segments */
2106 err = nilfs_segctor_prepare_write(sci, &failed_page);
a694291a 2107 if (err) {
1e2b68bf 2108 nilfs_abort_logs(&sci->sc_segbufs, failed_page, err);
9ff05123 2109 goto failed_to_write;
a694291a 2110 }
aaed1d5b
RK
2111
2112 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2113 nilfs->ns_crc_seed);
9ff05123 2114
9c965bac 2115 err = nilfs_segctor_write(sci, nilfs);
9ff05123
RK
2116 if (unlikely(err))
2117 goto failed_to_write;
2118
a694291a
RK
2119 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2120 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2121 /*
2122 * At this point, we avoid double buffering
2123 * for blocksize < pagesize because page dirty
2124 * flag is turned off during write and dirty
2125 * buffers are not properly collected for
2126 * pages crossing over segments.
2127 */
2128 err = nilfs_segctor_wait(sci);
2129 if (err)
2130 goto failed_to_write;
2131 }
9ff05123
RK
2132 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2133
9ff05123 2134 out:
9ff05123
RK
2135 nilfs_segctor_check_out_files(sci, sbi);
2136 return err;
2137
2138 failed_to_write:
9ff05123
RK
2139 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2140 nilfs_redirty_inodes(&sci->sc_dirty_files);
9ff05123
RK
2141
2142 failed:
2143 if (nilfs_doing_gc())
2144 nilfs_redirty_inodes(&sci->sc_gc_inodes);
a694291a 2145 nilfs_segctor_abort_construction(sci, nilfs, err);
9ff05123
RK
2146 goto out;
2147}
2148
2149/**
9ccf56c1 2150 * nilfs_segctor_start_timer - set timer of background write
9ff05123
RK
2151 * @sci: nilfs_sc_info
2152 *
2153 * If the timer has already been set, it ignores the new request.
2154 * This function MUST be called within a section locking the segment
2155 * semaphore.
2156 */
2157static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2158{
2159 spin_lock(&sci->sc_state_lock);
fdce895e
LH
2160 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2161 sci->sc_timer.expires = jiffies + sci->sc_interval;
2162 add_timer(&sci->sc_timer);
9ff05123
RK
2163 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2164 }
2165 spin_unlock(&sci->sc_state_lock);
2166}
2167
2168static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2169{
2170 spin_lock(&sci->sc_state_lock);
2171 if (!(sci->sc_flush_request & (1 << bn))) {
2172 unsigned long prev_req = sci->sc_flush_request;
2173
2174 sci->sc_flush_request |= (1 << bn);
2175 if (!prev_req)
2176 wake_up(&sci->sc_wait_daemon);
2177 }
2178 spin_unlock(&sci->sc_state_lock);
2179}
2180
2181/**
2182 * nilfs_flush_segment - trigger a segment construction for resource control
2183 * @sb: super block
2184 * @ino: inode number of the file to be flushed out.
2185 */
2186void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2187{
2188 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2189 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2190
2191 if (!sci || nilfs_doing_construction())
2192 return;
2193 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2194 /* assign bit 0 to data files */
2195}
2196
9ff05123
RK
2197struct nilfs_segctor_wait_request {
2198 wait_queue_t wq;
2199 __u32 seq;
2200 int err;
2201 atomic_t done;
2202};
2203
2204static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2205{
2206 struct nilfs_segctor_wait_request wait_req;
2207 int err = 0;
2208
2209 spin_lock(&sci->sc_state_lock);
2210 init_wait(&wait_req.wq);
2211 wait_req.err = 0;
2212 atomic_set(&wait_req.done, 0);
2213 wait_req.seq = ++sci->sc_seq_request;
2214 spin_unlock(&sci->sc_state_lock);
2215
2216 init_waitqueue_entry(&wait_req.wq, current);
2217 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2218 set_current_state(TASK_INTERRUPTIBLE);
2219 wake_up(&sci->sc_wait_daemon);
2220
2221 for (;;) {
2222 if (atomic_read(&wait_req.done)) {
2223 err = wait_req.err;
2224 break;
2225 }
2226 if (!signal_pending(current)) {
2227 schedule();
2228 continue;
2229 }
2230 err = -ERESTARTSYS;
2231 break;
2232 }
2233 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2234 return err;
2235}
2236
2237static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2238{
2239 struct nilfs_segctor_wait_request *wrq, *n;
2240 unsigned long flags;
2241
2242 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2243 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2244 wq.task_list) {
2245 if (!atomic_read(&wrq->done) &&
2246 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2247 wrq->err = err;
2248 atomic_set(&wrq->done, 1);
2249 }
2250 if (atomic_read(&wrq->done)) {
2251 wrq->wq.func(&wrq->wq,
2252 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2253 0, NULL);
2254 }
2255 }
2256 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2257}
2258
2259/**
2260 * nilfs_construct_segment - construct a logical segment
2261 * @sb: super block
2262 *
2263 * Return Value: On success, 0 is retured. On errors, one of the following
2264 * negative error code is returned.
2265 *
2266 * %-EROFS - Read only filesystem.
2267 *
2268 * %-EIO - I/O error
2269 *
2270 * %-ENOSPC - No space left on device (only in a panic state).
2271 *
2272 * %-ERESTARTSYS - Interrupted.
2273 *
2274 * %-ENOMEM - Insufficient memory available.
2275 */
2276int nilfs_construct_segment(struct super_block *sb)
2277{
2278 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2279 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2280 struct nilfs_transaction_info *ti;
2281 int err;
2282
2283 if (!sci)
2284 return -EROFS;
2285
2286 /* A call inside transactions causes a deadlock. */
2287 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2288
2289 err = nilfs_segctor_sync(sci);
2290 return err;
2291}
2292
2293/**
2294 * nilfs_construct_dsync_segment - construct a data-only logical segment
2295 * @sb: super block
f30bf3e4
RK
2296 * @inode: inode whose data blocks should be written out
2297 * @start: start byte offset
2298 * @end: end byte offset (inclusive)
9ff05123
RK
2299 *
2300 * Return Value: On success, 0 is retured. On errors, one of the following
2301 * negative error code is returned.
2302 *
2303 * %-EROFS - Read only filesystem.
2304 *
2305 * %-EIO - I/O error
2306 *
2307 * %-ENOSPC - No space left on device (only in a panic state).
2308 *
2309 * %-ERESTARTSYS - Interrupted.
2310 *
2311 * %-ENOMEM - Insufficient memory available.
2312 */
f30bf3e4
RK
2313int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2314 loff_t start, loff_t end)
9ff05123
RK
2315{
2316 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2317 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2318 struct nilfs_inode_info *ii;
2319 struct nilfs_transaction_info ti;
2320 int err = 0;
2321
2322 if (!sci)
2323 return -EROFS;
2324
2325 nilfs_transaction_lock(sbi, &ti, 0);
2326
2327 ii = NILFS_I(inode);
2328 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2329 nilfs_test_opt(sbi, STRICT_ORDER) ||
2330 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2331 nilfs_discontinued(sbi->s_nilfs)) {
2332 nilfs_transaction_unlock(sbi);
2333 err = nilfs_segctor_sync(sci);
2334 return err;
2335 }
2336
2337 spin_lock(&sbi->s_inode_lock);
2338 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2339 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2340 spin_unlock(&sbi->s_inode_lock);
2341 nilfs_transaction_unlock(sbi);
2342 return 0;
2343 }
2344 spin_unlock(&sbi->s_inode_lock);
f30bf3e4
RK
2345 sci->sc_dsync_inode = ii;
2346 sci->sc_dsync_start = start;
2347 sci->sc_dsync_end = end;
9ff05123
RK
2348
2349 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2350
2351 nilfs_transaction_unlock(sbi);
2352 return err;
2353}
2354
9ff05123
RK
2355#define FLUSH_FILE_BIT (0x1) /* data file only */
2356#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2357
dcd76186
RK
2358/**
2359 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2360 * @sci: segment constructor object
2361 */
2362static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
9ff05123 2363{
9ff05123 2364 spin_lock(&sci->sc_state_lock);
dcd76186 2365 sci->sc_seq_accepted = sci->sc_seq_request;
9ff05123 2366 spin_unlock(&sci->sc_state_lock);
fdce895e 2367 del_timer_sync(&sci->sc_timer);
9ff05123
RK
2368}
2369
dcd76186
RK
2370/**
2371 * nilfs_segctor_notify - notify the result of request to caller threads
2372 * @sci: segment constructor object
2373 * @mode: mode of log forming
2374 * @err: error code to be notified
2375 */
2376static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
9ff05123
RK
2377{
2378 /* Clear requests (even when the construction failed) */
2379 spin_lock(&sci->sc_state_lock);
2380
dcd76186 2381 if (mode == SC_LSEG_SR) {
aeda7f63 2382 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
dcd76186
RK
2383 sci->sc_seq_done = sci->sc_seq_accepted;
2384 nilfs_segctor_wakeup(sci, err);
9ff05123 2385 sci->sc_flush_request = 0;
aeda7f63 2386 } else {
dcd76186 2387 if (mode == SC_FLUSH_FILE)
aeda7f63 2388 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
dcd76186 2389 else if (mode == SC_FLUSH_DAT)
aeda7f63
RK
2390 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2391
2392 /* re-enable timer if checkpoint creation was not done */
fdce895e
LH
2393 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2394 time_before(jiffies, sci->sc_timer.expires))
2395 add_timer(&sci->sc_timer);
aeda7f63 2396 }
9ff05123
RK
2397 spin_unlock(&sci->sc_state_lock);
2398}
2399
dcd76186
RK
2400/**
2401 * nilfs_segctor_construct - form logs and write them to disk
2402 * @sci: segment constructor object
2403 * @mode: mode of log forming
2404 */
2405static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
9ff05123
RK
2406{
2407 struct nilfs_sb_info *sbi = sci->sc_sbi;
2408 struct the_nilfs *nilfs = sbi->s_nilfs;
d26493b6 2409 struct nilfs_super_block **sbp;
9ff05123
RK
2410 int err = 0;
2411
dcd76186
RK
2412 nilfs_segctor_accept(sci);
2413
9ff05123 2414 if (nilfs_discontinued(nilfs))
dcd76186
RK
2415 mode = SC_LSEG_SR;
2416 if (!nilfs_segctor_confirm(sci))
2417 err = nilfs_segctor_do_construct(sci, mode);
2418
9ff05123 2419 if (likely(!err)) {
dcd76186 2420 if (mode != SC_FLUSH_DAT)
9ff05123
RK
2421 atomic_set(&nilfs->ns_ndirtyblks, 0);
2422 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2423 nilfs_discontinued(nilfs)) {
2424 down_write(&nilfs->ns_sem);
d26493b6 2425 err = -EIO;
b2ac86e1
JS
2426 sbp = nilfs_prepare_super(sbi,
2427 nilfs_sb_will_flip(nilfs));
2428 if (likely(sbp)) {
2429 nilfs_set_log_cursor(sbp[0], nilfs);
2430 err = nilfs_commit_super(sbi, NILFS_SB_COMMIT);
2431 }
9ff05123
RK
2432 up_write(&nilfs->ns_sem);
2433 }
2434 }
dcd76186
RK
2435
2436 nilfs_segctor_notify(sci, mode, err);
9ff05123
RK
2437 return err;
2438}
2439
2440static void nilfs_construction_timeout(unsigned long data)
2441{
2442 struct task_struct *p = (struct task_struct *)data;
2443 wake_up_process(p);
2444}
2445
2446static void
2447nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2448{
2449 struct nilfs_inode_info *ii, *n;
2450
2451 list_for_each_entry_safe(ii, n, head, i_dirty) {
2452 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2453 continue;
9ff05123 2454 list_del_init(&ii->i_dirty);
263d90ce 2455 iput(&ii->vfs_inode);
9ff05123
RK
2456 }
2457}
2458
4f6b8288
RK
2459int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2460 void **kbufs)
9ff05123
RK
2461{
2462 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2463 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2464 struct the_nilfs *nilfs = sbi->s_nilfs;
2465 struct nilfs_transaction_info ti;
9ff05123
RK
2466 int err;
2467
2468 if (unlikely(!sci))
2469 return -EROFS;
2470
2471 nilfs_transaction_lock(sbi, &ti, 1);
2472
2473 err = nilfs_init_gcdat_inode(nilfs);
2474 if (unlikely(err))
2475 goto out_unlock;
071cb4b8 2476
4f6b8288 2477 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
9ff05123
RK
2478 if (unlikely(err))
2479 goto out_unlock;
2480
071cb4b8
RK
2481 sci->sc_freesegs = kbufs[4];
2482 sci->sc_nfreesegs = argv[4].v_nmembs;
0935db74 2483 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
9ff05123
RK
2484
2485 for (;;) {
dcd76186 2486 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
9ff05123 2487 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
9ff05123
RK
2488
2489 if (likely(!err))
2490 break;
2491
2492 nilfs_warning(sb, __func__,
2493 "segment construction failed. (err=%d)", err);
2494 set_current_state(TASK_INTERRUPTIBLE);
2495 schedule_timeout(sci->sc_interval);
2496 }
e902ec99
JS
2497 if (nilfs_test_opt(sbi, DISCARD)) {
2498 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2499 sci->sc_nfreesegs);
2500 if (ret) {
2501 printk(KERN_WARNING
2502 "NILFS warning: error %d on discard request, "
2503 "turning discards off for the device\n", ret);
2504 nilfs_clear_opt(sbi, DISCARD);
2505 }
2506 }
9ff05123
RK
2507
2508 out_unlock:
071cb4b8
RK
2509 sci->sc_freesegs = NULL;
2510 sci->sc_nfreesegs = 0;
9ff05123
RK
2511 nilfs_clear_gcdat_inode(nilfs);
2512 nilfs_transaction_unlock(sbi);
2513 return err;
2514}
2515
2516static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2517{
2518 struct nilfs_sb_info *sbi = sci->sc_sbi;
2519 struct nilfs_transaction_info ti;
9ff05123
RK
2520
2521 nilfs_transaction_lock(sbi, &ti, 0);
dcd76186 2522 nilfs_segctor_construct(sci, mode);
9ff05123
RK
2523
2524 /*
2525 * Unclosed segment should be retried. We do this using sc_timer.
2526 * Timeout of sc_timer will invoke complete construction which leads
2527 * to close the current logical segment.
2528 */
2529 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2530 nilfs_segctor_start_timer(sci);
2531
2532 nilfs_transaction_unlock(sbi);
2533}
2534
2535static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2536{
2537 int mode = 0;
2538 int err;
2539
2540 spin_lock(&sci->sc_state_lock);
2541 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2542 SC_FLUSH_DAT : SC_FLUSH_FILE;
2543 spin_unlock(&sci->sc_state_lock);
2544
2545 if (mode) {
2546 err = nilfs_segctor_do_construct(sci, mode);
2547
2548 spin_lock(&sci->sc_state_lock);
2549 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2550 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2551 spin_unlock(&sci->sc_state_lock);
2552 }
2553 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2554}
2555
2556static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2557{
2558 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2559 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2560 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2561 return SC_FLUSH_FILE;
2562 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2563 return SC_FLUSH_DAT;
2564 }
2565 return SC_LSEG_SR;
2566}
2567
2568/**
2569 * nilfs_segctor_thread - main loop of the segment constructor thread.
2570 * @arg: pointer to a struct nilfs_sc_info.
2571 *
2572 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2573 * to execute segment constructions.
2574 */
2575static int nilfs_segctor_thread(void *arg)
2576{
2577 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
e605f0a7 2578 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
9ff05123
RK
2579 int timeout = 0;
2580
fdce895e
LH
2581 sci->sc_timer.data = (unsigned long)current;
2582 sci->sc_timer.function = nilfs_construction_timeout;
9ff05123
RK
2583
2584 /* start sync. */
2585 sci->sc_task = current;
2586 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2587 printk(KERN_INFO
2588 "segctord starting. Construction interval = %lu seconds, "
2589 "CP frequency < %lu seconds\n",
2590 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2591
2592 spin_lock(&sci->sc_state_lock);
2593 loop:
2594 for (;;) {
2595 int mode;
2596
2597 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2598 goto end_thread;
2599
2600 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2601 mode = SC_LSEG_SR;
2602 else if (!sci->sc_flush_request)
2603 break;
2604 else
2605 mode = nilfs_segctor_flush_mode(sci);
2606
2607 spin_unlock(&sci->sc_state_lock);
2608 nilfs_segctor_thread_construct(sci, mode);
2609 spin_lock(&sci->sc_state_lock);
2610 timeout = 0;
2611 }
2612
2613
2614 if (freezing(current)) {
2615 spin_unlock(&sci->sc_state_lock);
2616 refrigerator();
2617 spin_lock(&sci->sc_state_lock);
2618 } else {
2619 DEFINE_WAIT(wait);
2620 int should_sleep = 1;
2621
2622 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2623 TASK_INTERRUPTIBLE);
2624
2625 if (sci->sc_seq_request != sci->sc_seq_done)
2626 should_sleep = 0;
2627 else if (sci->sc_flush_request)
2628 should_sleep = 0;
2629 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2630 should_sleep = time_before(jiffies,
fdce895e 2631 sci->sc_timer.expires);
9ff05123
RK
2632
2633 if (should_sleep) {
2634 spin_unlock(&sci->sc_state_lock);
2635 schedule();
2636 spin_lock(&sci->sc_state_lock);
2637 }
2638 finish_wait(&sci->sc_wait_daemon, &wait);
2639 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
fdce895e 2640 time_after_eq(jiffies, sci->sc_timer.expires));
e605f0a7
RK
2641
2642 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
1dfa2710 2643 set_nilfs_discontinued(nilfs);
9ff05123
RK
2644 }
2645 goto loop;
2646
2647 end_thread:
2648 spin_unlock(&sci->sc_state_lock);
9ff05123
RK
2649
2650 /* end sync. */
2651 sci->sc_task = NULL;
2652 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2653 return 0;
2654}
2655
2656static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2657{
2658 struct task_struct *t;
2659
2660 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2661 if (IS_ERR(t)) {
2662 int err = PTR_ERR(t);
2663
2664 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2665 err);
2666 return err;
2667 }
2668 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2669 return 0;
2670}
2671
2672static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2673{
2674 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2675
2676 while (sci->sc_task) {
2677 wake_up(&sci->sc_wait_daemon);
2678 spin_unlock(&sci->sc_state_lock);
2679 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2680 spin_lock(&sci->sc_state_lock);
2681 }
2682}
2683
9ff05123
RK
2684/*
2685 * Setup & clean-up functions
2686 */
2687static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
2688{
2689 struct nilfs_sc_info *sci;
2690
2691 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2692 if (!sci)
2693 return NULL;
2694
2695 sci->sc_sbi = sbi;
2696 sci->sc_super = sbi->s_super;
2697
2698 init_waitqueue_head(&sci->sc_wait_request);
2699 init_waitqueue_head(&sci->sc_wait_daemon);
2700 init_waitqueue_head(&sci->sc_wait_task);
2701 spin_lock_init(&sci->sc_state_lock);
2702 INIT_LIST_HEAD(&sci->sc_dirty_files);
2703 INIT_LIST_HEAD(&sci->sc_segbufs);
a694291a 2704 INIT_LIST_HEAD(&sci->sc_write_logs);
9ff05123 2705 INIT_LIST_HEAD(&sci->sc_gc_inodes);
9ff05123 2706 INIT_LIST_HEAD(&sci->sc_copied_buffers);
fdce895e 2707 init_timer(&sci->sc_timer);
9ff05123
RK
2708
2709 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2710 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2711 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2712
2713 if (sbi->s_interval)
2714 sci->sc_interval = sbi->s_interval;
2715 if (sbi->s_watermark)
2716 sci->sc_watermark = sbi->s_watermark;
2717 return sci;
2718}
2719
2720static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2721{
2722 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2723
2724 /* The segctord thread was stopped and its timer was removed.
2725 But some tasks remain. */
2726 do {
2727 struct nilfs_sb_info *sbi = sci->sc_sbi;
2728 struct nilfs_transaction_info ti;
9ff05123
RK
2729
2730 nilfs_transaction_lock(sbi, &ti, 0);
dcd76186 2731 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
9ff05123
RK
2732 nilfs_transaction_unlock(sbi);
2733
2734 } while (ret && retrycount-- > 0);
2735}
2736
2737/**
2738 * nilfs_segctor_destroy - destroy the segment constructor.
2739 * @sci: nilfs_sc_info
2740 *
2741 * nilfs_segctor_destroy() kills the segctord thread and frees
2742 * the nilfs_sc_info struct.
2743 * Caller must hold the segment semaphore.
2744 */
2745static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2746{
2747 struct nilfs_sb_info *sbi = sci->sc_sbi;
2748 int flag;
2749
2750 up_write(&sbi->s_nilfs->ns_segctor_sem);
2751
2752 spin_lock(&sci->sc_state_lock);
2753 nilfs_segctor_kill_thread(sci);
2754 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2755 || sci->sc_seq_request != sci->sc_seq_done);
2756 spin_unlock(&sci->sc_state_lock);
2757
3256a055 2758 if (flag || !nilfs_segctor_confirm(sci))
9ff05123
RK
2759 nilfs_segctor_write_out(sci);
2760
1f5abe7e 2761 WARN_ON(!list_empty(&sci->sc_copied_buffers));
9ff05123
RK
2762
2763 if (!list_empty(&sci->sc_dirty_files)) {
2764 nilfs_warning(sbi->s_super, __func__,
2765 "dirty file(s) after the final construction\n");
2766 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
2767 }
9ff05123 2768
1f5abe7e 2769 WARN_ON(!list_empty(&sci->sc_segbufs));
a694291a 2770 WARN_ON(!list_empty(&sci->sc_write_logs));
9ff05123 2771
9ff05123
RK
2772 down_write(&sbi->s_nilfs->ns_segctor_sem);
2773
fdce895e 2774 del_timer_sync(&sci->sc_timer);
9ff05123
RK
2775 kfree(sci);
2776}
2777
2778/**
2779 * nilfs_attach_segment_constructor - attach a segment constructor
2780 * @sbi: nilfs_sb_info
9ff05123
RK
2781 *
2782 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
7a65004b 2783 * initializes it, and starts the segment constructor.
9ff05123
RK
2784 *
2785 * Return Value: On success, 0 is returned. On error, one of the following
2786 * negative error code is returned.
2787 *
2788 * %-ENOMEM - Insufficient memory available.
2789 */
cece5520 2790int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
9ff05123
RK
2791{
2792 struct the_nilfs *nilfs = sbi->s_nilfs;
2793 int err;
2794
fe5f171b
RK
2795 if (NILFS_SC(sbi)) {
2796 /*
2797 * This happens if the filesystem was remounted
2798 * read/write after nilfs_error degenerated it into a
2799 * read-only mount.
2800 */
2801 nilfs_detach_segment_constructor(sbi);
2802 }
2803
9ff05123
RK
2804 sbi->s_sc_info = nilfs_segctor_new(sbi);
2805 if (!sbi->s_sc_info)
2806 return -ENOMEM;
2807
2808 nilfs_attach_writer(nilfs, sbi);
154ac5a8 2809 err = nilfs_segctor_start_thread(NILFS_SC(sbi));
9ff05123
RK
2810 if (err) {
2811 nilfs_detach_writer(nilfs, sbi);
2812 kfree(sbi->s_sc_info);
2813 sbi->s_sc_info = NULL;
2814 }
2815 return err;
2816}
2817
2818/**
2819 * nilfs_detach_segment_constructor - destroy the segment constructor
2820 * @sbi: nilfs_sb_info
2821 *
2822 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2823 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2824 */
2825void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2826{
2827 struct the_nilfs *nilfs = sbi->s_nilfs;
2828 LIST_HEAD(garbage_list);
2829
2830 down_write(&nilfs->ns_segctor_sem);
2831 if (NILFS_SC(sbi)) {
2832 nilfs_segctor_destroy(NILFS_SC(sbi));
2833 sbi->s_sc_info = NULL;
2834 }
2835
2836 /* Force to free the list of dirty files */
2837 spin_lock(&sbi->s_inode_lock);
2838 if (!list_empty(&sbi->s_dirty_files)) {
2839 list_splice_init(&sbi->s_dirty_files, &garbage_list);
2840 nilfs_warning(sbi->s_super, __func__,
2841 "Non empty dirty list after the last "
2842 "segment construction\n");
2843 }
2844 spin_unlock(&sbi->s_inode_lock);
2845 up_write(&nilfs->ns_segctor_sem);
2846
2847 nilfs_dispose_list(sbi, &garbage_list, 1);
2848 nilfs_detach_writer(nilfs, sbi);
2849}