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