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