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