blob: a17bfa193e3f12a353e2bc219fb365590ac37f81 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070036#include "nilfs.h"
37#include "btnode.h"
38#include "page.h"
39#include "segment.h"
40#include "sufile.h"
41#include "cpfile.h"
42#include "ifile.h"
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070043#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,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070070 NILFS_ST_IFILE,
71 NILFS_ST_CPFILE,
72 NILFS_ST_SUFILE,
73 NILFS_ST_DAT,
74 NILFS_ST_SR, /* Super root */
75 NILFS_ST_DSYNC, /* Data sync blocks */
76 NILFS_ST_DONE,
77};
78
79/* State flags of collection */
80#define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
81#define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +090082#define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
83#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070084
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
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700119static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
120{
121 struct nilfs_transaction_info *cur_ti = current->journal_info;
122 void *save = NULL;
123
124 if (cur_ti) {
125 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
126 return ++cur_ti->ti_count;
127 else {
128 /*
129 * If journal_info field is occupied by other FS,
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700130 * it is saved and will be restored on
131 * nilfs_transaction_commit().
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700132 */
133 printk(KERN_WARNING
134 "NILFS warning: journal info from a different "
135 "FS\n");
136 save = current->journal_info;
137 }
138 }
139 if (!ti) {
140 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
141 if (!ti)
142 return -ENOMEM;
143 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
144 } else {
145 ti->ti_flags = 0;
146 }
147 ti->ti_count = 0;
148 ti->ti_save = save;
149 ti->ti_magic = NILFS_TI_MAGIC;
150 current->journal_info = ti;
151 return 0;
152}
153
154/**
155 * nilfs_transaction_begin - start indivisible file operations.
156 * @sb: super block
157 * @ti: nilfs_transaction_info
158 * @vacancy_check: flags for vacancy rate checks
159 *
160 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
161 * the segment semaphore, to make a segment construction and write tasks
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700162 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700163 * The region enclosed by these two functions can be nested. To avoid a
164 * deadlock, the semaphore is only acquired or released in the outermost call.
165 *
166 * This function allocates a nilfs_transaction_info struct to keep context
167 * information on it. It is initialized and hooked onto the current task in
168 * the outermost call. If a pre-allocated struct is given to @ti, it is used
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900169 * instead; otherwise a new struct is assigned from a slab.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700170 *
171 * When @vacancy_check flag is set, this function will check the amount of
172 * free space, and will wait for the GC to reclaim disk space if low capacity.
173 *
174 * Return Value: On success, 0 is returned. On error, one of the following
175 * negative error code is returned.
176 *
177 * %-ENOMEM - Insufficient memory available.
178 *
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700179 * %-ENOSPC - No space left on device
180 */
181int nilfs_transaction_begin(struct super_block *sb,
182 struct nilfs_transaction_info *ti,
183 int vacancy_check)
184{
185 struct nilfs_sb_info *sbi;
186 struct the_nilfs *nilfs;
187 int ret = nilfs_prepare_segment_lock(ti);
188
189 if (unlikely(ret < 0))
190 return ret;
191 if (ret > 0)
192 return 0;
193
194 sbi = NILFS_SB(sb);
195 nilfs = sbi->s_nilfs;
196 down_read(&nilfs->ns_segctor_sem);
197 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
198 up_read(&nilfs->ns_segctor_sem);
199 ret = -ENOSPC;
200 goto failed;
201 }
202 return 0;
203
204 failed:
205 ti = current->journal_info;
206 current->journal_info = ti->ti_save;
207 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
208 kmem_cache_free(nilfs_transaction_cachep, ti);
209 return ret;
210}
211
212/**
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700213 * nilfs_transaction_commit - commit indivisible file operations.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700214 * @sb: super block
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700215 *
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700216 * nilfs_transaction_commit() releases the read semaphore which is
217 * acquired by nilfs_transaction_begin(). This is only performed
218 * in outermost call of this function. If a commit flag is set,
219 * nilfs_transaction_commit() sets a timer to start the segment
220 * constructor. If a sync flag is set, it starts construction
221 * directly.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700222 */
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700223int nilfs_transaction_commit(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700224{
225 struct nilfs_transaction_info *ti = current->journal_info;
226 struct nilfs_sb_info *sbi;
227 struct nilfs_sc_info *sci;
228 int err = 0;
229
230 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700231 ti->ti_flags |= NILFS_TI_COMMIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700232 if (ti->ti_count > 0) {
233 ti->ti_count--;
234 return 0;
235 }
236 sbi = NILFS_SB(sb);
237 sci = NILFS_SC(sbi);
238 if (sci != NULL) {
239 if (ti->ti_flags & NILFS_TI_COMMIT)
240 nilfs_segctor_start_timer(sci);
241 if (atomic_read(&sbi->s_nilfs->ns_ndirtyblks) >
242 sci->sc_watermark)
243 nilfs_segctor_do_flush(sci, 0);
244 }
245 up_read(&sbi->s_nilfs->ns_segctor_sem);
246 current->journal_info = ti->ti_save;
247
248 if (ti->ti_flags & NILFS_TI_SYNC)
249 err = nilfs_construct_segment(sb);
250 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
251 kmem_cache_free(nilfs_transaction_cachep, ti);
252 return err;
253}
254
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700255void nilfs_transaction_abort(struct super_block *sb)
256{
257 struct nilfs_transaction_info *ti = current->journal_info;
258
259 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
260 if (ti->ti_count > 0) {
261 ti->ti_count--;
262 return;
263 }
264 up_read(&NILFS_SB(sb)->s_nilfs->ns_segctor_sem);
265
266 current->journal_info = ti->ti_save;
267 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
268 kmem_cache_free(nilfs_transaction_cachep, ti);
269}
270
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700271void nilfs_relax_pressure_in_lock(struct super_block *sb)
272{
273 struct nilfs_sb_info *sbi = NILFS_SB(sb);
274 struct nilfs_sc_info *sci = NILFS_SC(sbi);
275 struct the_nilfs *nilfs = sbi->s_nilfs;
276
277 if (!sci || !sci->sc_flush_request)
278 return;
279
280 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
281 up_read(&nilfs->ns_segctor_sem);
282
283 down_write(&nilfs->ns_segctor_sem);
284 if (sci->sc_flush_request &&
285 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
286 struct nilfs_transaction_info *ti = current->journal_info;
287
288 ti->ti_flags |= NILFS_TI_WRITER;
289 nilfs_segctor_do_immediate_flush(sci);
290 ti->ti_flags &= ~NILFS_TI_WRITER;
291 }
292 downgrade_write(&nilfs->ns_segctor_sem);
293}
294
295static void nilfs_transaction_lock(struct nilfs_sb_info *sbi,
296 struct nilfs_transaction_info *ti,
297 int gcflag)
298{
299 struct nilfs_transaction_info *cur_ti = current->journal_info;
300
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700301 WARN_ON(cur_ti);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700302 ti->ti_flags = NILFS_TI_WRITER;
303 ti->ti_count = 0;
304 ti->ti_save = cur_ti;
305 ti->ti_magic = NILFS_TI_MAGIC;
306 INIT_LIST_HEAD(&ti->ti_garbage);
307 current->journal_info = ti;
308
309 for (;;) {
310 down_write(&sbi->s_nilfs->ns_segctor_sem);
311 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &NILFS_SC(sbi)->sc_flags))
312 break;
313
314 nilfs_segctor_do_immediate_flush(NILFS_SC(sbi));
315
316 up_write(&sbi->s_nilfs->ns_segctor_sem);
317 yield();
318 }
319 if (gcflag)
320 ti->ti_flags |= NILFS_TI_GC;
321}
322
323static void nilfs_transaction_unlock(struct nilfs_sb_info *sbi)
324{
325 struct nilfs_transaction_info *ti = current->journal_info;
326
327 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
328 BUG_ON(ti->ti_count > 0);
329
330 up_write(&sbi->s_nilfs->ns_segctor_sem);
331 current->journal_info = ti->ti_save;
332 if (!list_empty(&ti->ti_garbage))
333 nilfs_dispose_list(sbi, &ti->ti_garbage, 0);
334}
335
336static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
337 struct nilfs_segsum_pointer *ssp,
338 unsigned bytes)
339{
340 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
341 unsigned blocksize = sci->sc_super->s_blocksize;
342 void *p;
343
344 if (unlikely(ssp->offset + bytes > blocksize)) {
345 ssp->offset = 0;
346 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
347 &segbuf->sb_segsum_buffers));
348 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
349 }
350 p = ssp->bh->b_data + ssp->offset;
351 ssp->offset += bytes;
352 return p;
353}
354
355/**
356 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
357 * @sci: nilfs_sc_info
358 */
359static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
360{
361 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
362 struct buffer_head *sumbh;
363 unsigned sumbytes;
364 unsigned flags = 0;
365 int err;
366
367 if (nilfs_doing_gc())
368 flags = NILFS_SS_GC;
369 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime);
370 if (unlikely(err))
371 return err;
372
373 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
374 sumbytes = segbuf->sb_sum.sumbytes;
375 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
376 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
377 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
378 return 0;
379}
380
381static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
382{
383 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
384 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
385 return -E2BIG; /* The current segment is filled up
386 (internal code) */
387 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
388 return nilfs_segctor_reset_segment_buffer(sci);
389}
390
391static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
392{
393 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
394 int err;
395
396 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
397 err = nilfs_segctor_feed_segment(sci);
398 if (err)
399 return err;
400 segbuf = sci->sc_curseg;
401 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900402 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700403 if (likely(!err))
404 segbuf->sb_sum.flags |= NILFS_SS_SR;
405 return err;
406}
407
408/*
409 * Functions for making segment summary and payloads
410 */
411static int nilfs_segctor_segsum_block_required(
412 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
413 unsigned binfo_size)
414{
415 unsigned blocksize = sci->sc_super->s_blocksize;
416 /* Size of finfo and binfo is enough small against blocksize */
417
418 return ssp->offset + binfo_size +
419 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
420 blocksize;
421}
422
423static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
424 struct inode *inode)
425{
426 sci->sc_curseg->sb_sum.nfinfo++;
427 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
428 nilfs_segctor_map_segsum_entry(
429 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700430
431 if (inode->i_sb && !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
432 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700433 /* skip finfo */
434}
435
436static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
437 struct inode *inode)
438{
439 struct nilfs_finfo *finfo;
440 struct nilfs_inode_info *ii;
441 struct nilfs_segment_buffer *segbuf;
442
443 if (sci->sc_blk_cnt == 0)
444 return;
445
446 ii = NILFS_I(inode);
447 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
448 sizeof(*finfo));
449 finfo->fi_ino = cpu_to_le64(inode->i_ino);
450 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
451 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
452 finfo->fi_cno = cpu_to_le64(ii->i_cno);
453
454 segbuf = sci->sc_curseg;
455 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
456 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
457 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
458 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
459}
460
461static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
462 struct buffer_head *bh,
463 struct inode *inode,
464 unsigned binfo_size)
465{
466 struct nilfs_segment_buffer *segbuf;
467 int required, err = 0;
468
469 retry:
470 segbuf = sci->sc_curseg;
471 required = nilfs_segctor_segsum_block_required(
472 sci, &sci->sc_binfo_ptr, binfo_size);
473 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
474 nilfs_segctor_end_finfo(sci, inode);
475 err = nilfs_segctor_feed_segment(sci);
476 if (err)
477 return err;
478 goto retry;
479 }
480 if (unlikely(required)) {
481 err = nilfs_segbuf_extend_segsum(segbuf);
482 if (unlikely(err))
483 goto failed;
484 }
485 if (sci->sc_blk_cnt == 0)
486 nilfs_segctor_begin_finfo(sci, inode);
487
488 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
489 /* Substitution to vblocknr is delayed until update_blocknr() */
490 nilfs_segbuf_add_file_buffer(segbuf, bh);
491 sci->sc_blk_cnt++;
492 failed:
493 return err;
494}
495
496static int nilfs_handle_bmap_error(int err, const char *fname,
497 struct inode *inode, struct super_block *sb)
498{
499 if (err == -EINVAL) {
500 nilfs_error(sb, fname, "broken bmap (inode=%lu)\n",
501 inode->i_ino);
502 err = -EIO;
503 }
504 return err;
505}
506
507/*
508 * Callback functions that enumerate, mark, and collect dirty blocks
509 */
510static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
511 struct buffer_head *bh, struct inode *inode)
512{
513 int err;
514
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700515 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
516 if (unlikely(err < 0))
517 return nilfs_handle_bmap_error(err, __func__, inode,
518 sci->sc_super);
519
520 err = nilfs_segctor_add_file_block(sci, bh, inode,
521 sizeof(struct nilfs_binfo_v));
522 if (!err)
523 sci->sc_datablk_cnt++;
524 return err;
525}
526
527static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
528 struct buffer_head *bh,
529 struct inode *inode)
530{
531 int err;
532
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700533 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
534 if (unlikely(err < 0))
535 return nilfs_handle_bmap_error(err, __func__, inode,
536 sci->sc_super);
537 return 0;
538}
539
540static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
541 struct buffer_head *bh,
542 struct inode *inode)
543{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700544 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700545 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
546}
547
548static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
549 struct nilfs_segsum_pointer *ssp,
550 union nilfs_binfo *binfo)
551{
552 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
553 sci, ssp, sizeof(*binfo_v));
554 *binfo_v = binfo->bi_v;
555}
556
557static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
558 struct nilfs_segsum_pointer *ssp,
559 union nilfs_binfo *binfo)
560{
561 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
562 sci, ssp, sizeof(*vblocknr));
563 *vblocknr = binfo->bi_v.bi_vblocknr;
564}
565
566struct nilfs_sc_operations nilfs_sc_file_ops = {
567 .collect_data = nilfs_collect_file_data,
568 .collect_node = nilfs_collect_file_node,
569 .collect_bmap = nilfs_collect_file_bmap,
570 .write_data_binfo = nilfs_write_file_data_binfo,
571 .write_node_binfo = nilfs_write_file_node_binfo,
572};
573
574static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
575 struct buffer_head *bh, struct inode *inode)
576{
577 int err;
578
579 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
580 if (unlikely(err < 0))
581 return nilfs_handle_bmap_error(err, __func__, inode,
582 sci->sc_super);
583
584 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
585 if (!err)
586 sci->sc_datablk_cnt++;
587 return err;
588}
589
590static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
591 struct buffer_head *bh, struct inode *inode)
592{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700593 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700594 return nilfs_segctor_add_file_block(sci, bh, inode,
595 sizeof(struct nilfs_binfo_dat));
596}
597
598static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
599 struct nilfs_segsum_pointer *ssp,
600 union nilfs_binfo *binfo)
601{
602 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
603 sizeof(*blkoff));
604 *blkoff = binfo->bi_dat.bi_blkoff;
605}
606
607static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
608 struct nilfs_segsum_pointer *ssp,
609 union nilfs_binfo *binfo)
610{
611 struct nilfs_binfo_dat *binfo_dat =
612 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
613 *binfo_dat = binfo->bi_dat;
614}
615
616struct nilfs_sc_operations nilfs_sc_dat_ops = {
617 .collect_data = nilfs_collect_dat_data,
618 .collect_node = nilfs_collect_file_node,
619 .collect_bmap = nilfs_collect_dat_bmap,
620 .write_data_binfo = nilfs_write_dat_data_binfo,
621 .write_node_binfo = nilfs_write_dat_node_binfo,
622};
623
624struct nilfs_sc_operations nilfs_sc_dsync_ops = {
625 .collect_data = nilfs_collect_file_data,
626 .collect_node = NULL,
627 .collect_bmap = NULL,
628 .write_data_binfo = nilfs_write_file_data_binfo,
629 .write_node_binfo = NULL,
630};
631
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700632static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
633 struct list_head *listp,
634 size_t nlimit,
635 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700636{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700637 struct address_space *mapping = inode->i_mapping;
638 struct pagevec pvec;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700639 pgoff_t index = 0, last = ULONG_MAX;
640 size_t ndirties = 0;
641 int i;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700642
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700643 if (unlikely(start != 0 || end != LLONG_MAX)) {
644 /*
645 * A valid range is given for sync-ing data pages. The
646 * range is rounded to per-page; extra dirty buffers
647 * may be included if blocksize < pagesize.
648 */
649 index = start >> PAGE_SHIFT;
650 last = end >> PAGE_SHIFT;
651 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700652 pagevec_init(&pvec, 0);
653 repeat:
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700654 if (unlikely(index > last) ||
655 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
656 min_t(pgoff_t, last - index,
657 PAGEVEC_SIZE - 1) + 1))
658 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700659
660 for (i = 0; i < pagevec_count(&pvec); i++) {
661 struct buffer_head *bh, *head;
662 struct page *page = pvec.pages[i];
663
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700664 if (unlikely(page->index > last))
665 break;
666
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700667 if (mapping->host) {
668 lock_page(page);
669 if (!page_has_buffers(page))
670 create_empty_buffers(page,
671 1 << inode->i_blkbits, 0);
672 unlock_page(page);
673 }
674
675 bh = head = page_buffers(page);
676 do {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700677 if (!buffer_dirty(bh))
678 continue;
679 get_bh(bh);
680 list_add_tail(&bh->b_assoc_buffers, listp);
681 ndirties++;
682 if (unlikely(ndirties >= nlimit)) {
683 pagevec_release(&pvec);
684 cond_resched();
685 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700686 }
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700687 } while (bh = bh->b_this_page, bh != head);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700688 }
689 pagevec_release(&pvec);
690 cond_resched();
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700691 goto repeat;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700692}
693
694static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
695 struct list_head *listp)
696{
697 struct nilfs_inode_info *ii = NILFS_I(inode);
698 struct address_space *mapping = &ii->i_btnode_cache;
699 struct pagevec pvec;
700 struct buffer_head *bh, *head;
701 unsigned int i;
702 pgoff_t index = 0;
703
704 pagevec_init(&pvec, 0);
705
706 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
707 PAGEVEC_SIZE)) {
708 for (i = 0; i < pagevec_count(&pvec); i++) {
709 bh = head = page_buffers(pvec.pages[i]);
710 do {
711 if (buffer_dirty(bh)) {
712 get_bh(bh);
713 list_add_tail(&bh->b_assoc_buffers,
714 listp);
715 }
716 bh = bh->b_this_page;
717 } while (bh != head);
718 }
719 pagevec_release(&pvec);
720 cond_resched();
721 }
722}
723
724static void nilfs_dispose_list(struct nilfs_sb_info *sbi,
725 struct list_head *head, int force)
726{
727 struct nilfs_inode_info *ii, *n;
728 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
729 unsigned nv = 0;
730
731 while (!list_empty(head)) {
732 spin_lock(&sbi->s_inode_lock);
733 list_for_each_entry_safe(ii, n, head, i_dirty) {
734 list_del_init(&ii->i_dirty);
735 if (force) {
736 if (unlikely(ii->i_bh)) {
737 brelse(ii->i_bh);
738 ii->i_bh = NULL;
739 }
740 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
741 set_bit(NILFS_I_QUEUED, &ii->i_state);
742 list_add_tail(&ii->i_dirty,
743 &sbi->s_dirty_files);
744 continue;
745 }
746 ivec[nv++] = ii;
747 if (nv == SC_N_INODEVEC)
748 break;
749 }
750 spin_unlock(&sbi->s_inode_lock);
751
752 for (pii = ivec; nv > 0; pii++, nv--)
753 iput(&(*pii)->vfs_inode);
754 }
755}
756
757static int nilfs_test_metadata_dirty(struct nilfs_sb_info *sbi)
758{
759 struct the_nilfs *nilfs = sbi->s_nilfs;
760 int ret = 0;
761
762 if (nilfs_mdt_fetch_dirty(sbi->s_ifile))
763 ret++;
764 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
765 ret++;
766 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
767 ret++;
768 if (ret || nilfs_doing_gc())
769 if (nilfs_mdt_fetch_dirty(nilfs_dat_inode(nilfs)))
770 ret++;
771 return ret;
772}
773
774static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
775{
776 return list_empty(&sci->sc_dirty_files) &&
777 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900778 sci->sc_nfreesegs == 0 &&
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700779 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
780}
781
782static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
783{
784 struct nilfs_sb_info *sbi = sci->sc_sbi;
785 int ret = 0;
786
787 if (nilfs_test_metadata_dirty(sbi))
788 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
789
790 spin_lock(&sbi->s_inode_lock);
791 if (list_empty(&sbi->s_dirty_files) && nilfs_segctor_clean(sci))
792 ret++;
793
794 spin_unlock(&sbi->s_inode_lock);
795 return ret;
796}
797
798static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
799{
800 struct nilfs_sb_info *sbi = sci->sc_sbi;
801 struct the_nilfs *nilfs = sbi->s_nilfs;
802
803 nilfs_mdt_clear_dirty(sbi->s_ifile);
804 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
805 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
806 nilfs_mdt_clear_dirty(nilfs_dat_inode(nilfs));
807}
808
809static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
810{
811 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
812 struct buffer_head *bh_cp;
813 struct nilfs_checkpoint *raw_cp;
814 int err;
815
816 /* XXX: this interface will be changed */
817 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
818 &raw_cp, &bh_cp);
819 if (likely(!err)) {
820 /* The following code is duplicated with cpfile. But, it is
821 needed to collect the checkpoint even if it was not newly
822 created */
823 nilfs_mdt_mark_buffer_dirty(bh_cp);
824 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
825 nilfs_cpfile_put_checkpoint(
826 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700827 } else
828 WARN_ON(err == -EINVAL || err == -ENOENT);
829
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700830 return err;
831}
832
833static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
834{
835 struct nilfs_sb_info *sbi = sci->sc_sbi;
836 struct the_nilfs *nilfs = sbi->s_nilfs;
837 struct buffer_head *bh_cp;
838 struct nilfs_checkpoint *raw_cp;
839 int err;
840
841 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
842 &raw_cp, &bh_cp);
843 if (unlikely(err)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700844 WARN_ON(err == -EINVAL || err == -ENOENT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700845 goto failed_ibh;
846 }
847 raw_cp->cp_snapshot_list.ssl_next = 0;
848 raw_cp->cp_snapshot_list.ssl_prev = 0;
849 raw_cp->cp_inodes_count =
850 cpu_to_le64(atomic_read(&sbi->s_inodes_count));
851 raw_cp->cp_blocks_count =
852 cpu_to_le64(atomic_read(&sbi->s_blocks_count));
853 raw_cp->cp_nblk_inc =
854 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
855 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
856 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700857
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700858 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
859 nilfs_checkpoint_clear_minor(raw_cp);
860 else
861 nilfs_checkpoint_set_minor(raw_cp);
862
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700863 nilfs_write_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode, 1);
864 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
865 return 0;
866
867 failed_ibh:
868 return err;
869}
870
871static void nilfs_fill_in_file_bmap(struct inode *ifile,
872 struct nilfs_inode_info *ii)
873
874{
875 struct buffer_head *ibh;
876 struct nilfs_inode *raw_inode;
877
878 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
879 ibh = ii->i_bh;
880 BUG_ON(!ibh);
881 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
882 ibh);
883 nilfs_bmap_write(ii->i_bmap, raw_inode);
884 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
885 }
886}
887
888static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci,
889 struct inode *ifile)
890{
891 struct nilfs_inode_info *ii;
892
893 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
894 nilfs_fill_in_file_bmap(ifile, ii);
895 set_bit(NILFS_I_COLLECTED, &ii->i_state);
896 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700897}
898
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700899static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
900 struct the_nilfs *nilfs)
901{
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900902 struct buffer_head *bh_sr;
903 struct nilfs_super_root *raw_sr;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700904 unsigned isz = nilfs->ns_inode_size;
905
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900906 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
907 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
908
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700909 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
910 raw_sr->sr_nongc_ctime
911 = cpu_to_le64(nilfs_doing_gc() ?
912 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
913 raw_sr->sr_flags = 0;
914
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900915 nilfs_write_inode_common(nilfs_dat_inode(nilfs), (void *)raw_sr +
916 NILFS_SR_DAT_OFFSET(isz), 1);
917 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
918 NILFS_SR_CPFILE_OFFSET(isz), 1);
919 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
920 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700921}
922
923static void nilfs_redirty_inodes(struct list_head *head)
924{
925 struct nilfs_inode_info *ii;
926
927 list_for_each_entry(ii, head, i_dirty) {
928 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
929 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
930 }
931}
932
933static void nilfs_drop_collected_inodes(struct list_head *head)
934{
935 struct nilfs_inode_info *ii;
936
937 list_for_each_entry(ii, head, i_dirty) {
938 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
939 continue;
940
941 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
942 set_bit(NILFS_I_UPDATED, &ii->i_state);
943 }
944}
945
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700946static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
947 struct inode *inode,
948 struct list_head *listp,
949 int (*collect)(struct nilfs_sc_info *,
950 struct buffer_head *,
951 struct inode *))
952{
953 struct buffer_head *bh, *n;
954 int err = 0;
955
956 if (collect) {
957 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
958 list_del_init(&bh->b_assoc_buffers);
959 err = collect(sci, bh, inode);
960 brelse(bh);
961 if (unlikely(err))
962 goto dispose_buffers;
963 }
964 return 0;
965 }
966
967 dispose_buffers:
968 while (!list_empty(listp)) {
969 bh = list_entry(listp->next, struct buffer_head,
970 b_assoc_buffers);
971 list_del_init(&bh->b_assoc_buffers);
972 brelse(bh);
973 }
974 return err;
975}
976
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700977static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
978{
979 /* Remaining number of blocks within segment buffer */
980 return sci->sc_segbuf_nblocks -
981 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
982}
983
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700984static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
985 struct inode *inode,
986 struct nilfs_sc_operations *sc_ops)
987{
988 LIST_HEAD(data_buffers);
989 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700990 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700991
992 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700993 size_t n, rest = nilfs_segctor_buffer_rest(sci);
994
995 n = nilfs_lookup_dirty_data_buffers(
996 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
997 if (n > rest) {
998 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700999 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001000 sc_ops->collect_data);
1001 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001002 goto break_or_fail;
1003 }
1004 }
1005 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1006
1007 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1008 err = nilfs_segctor_apply_buffers(
1009 sci, inode, &data_buffers, sc_ops->collect_data);
1010 if (unlikely(err)) {
1011 /* dispose node list */
1012 nilfs_segctor_apply_buffers(
1013 sci, inode, &node_buffers, NULL);
1014 goto break_or_fail;
1015 }
1016 sci->sc_stage.flags |= NILFS_CF_NODE;
1017 }
1018 /* Collect node */
1019 err = nilfs_segctor_apply_buffers(
1020 sci, inode, &node_buffers, sc_ops->collect_node);
1021 if (unlikely(err))
1022 goto break_or_fail;
1023
1024 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1025 err = nilfs_segctor_apply_buffers(
1026 sci, inode, &node_buffers, sc_ops->collect_bmap);
1027 if (unlikely(err))
1028 goto break_or_fail;
1029
1030 nilfs_segctor_end_finfo(sci, inode);
1031 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1032
1033 break_or_fail:
1034 return err;
1035}
1036
1037static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1038 struct inode *inode)
1039{
1040 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001041 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1042 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001043
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001044 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1045 sci->sc_dsync_start,
1046 sci->sc_dsync_end);
1047
1048 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1049 nilfs_collect_file_data);
1050 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001051 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001052 BUG_ON(n > rest);
1053 /* always receive -E2BIG or true error if n > rest */
1054 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001055 return err;
1056}
1057
1058static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1059{
1060 struct nilfs_sb_info *sbi = sci->sc_sbi;
1061 struct the_nilfs *nilfs = sbi->s_nilfs;
1062 struct list_head *head;
1063 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001064 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001065 int err = 0;
1066
1067 switch (sci->sc_stage.scnt) {
1068 case NILFS_ST_INIT:
1069 /* Pre-processes */
1070 sci->sc_stage.flags = 0;
1071
1072 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1073 sci->sc_nblk_inc = 0;
1074 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1075 if (mode == SC_LSEG_DSYNC) {
1076 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1077 goto dsync_mode;
1078 }
1079 }
1080
1081 sci->sc_stage.dirty_file_ptr = NULL;
1082 sci->sc_stage.gc_inode_ptr = NULL;
1083 if (mode == SC_FLUSH_DAT) {
1084 sci->sc_stage.scnt = NILFS_ST_DAT;
1085 goto dat_stage;
1086 }
1087 sci->sc_stage.scnt++; /* Fall through */
1088 case NILFS_ST_GC:
1089 if (nilfs_doing_gc()) {
1090 head = &sci->sc_gc_inodes;
1091 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1092 head, i_dirty);
1093 list_for_each_entry_continue(ii, head, i_dirty) {
1094 err = nilfs_segctor_scan_file(
1095 sci, &ii->vfs_inode,
1096 &nilfs_sc_file_ops);
1097 if (unlikely(err)) {
1098 sci->sc_stage.gc_inode_ptr = list_entry(
1099 ii->i_dirty.prev,
1100 struct nilfs_inode_info,
1101 i_dirty);
1102 goto break_or_fail;
1103 }
1104 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1105 }
1106 sci->sc_stage.gc_inode_ptr = NULL;
1107 }
1108 sci->sc_stage.scnt++; /* Fall through */
1109 case NILFS_ST_FILE:
1110 head = &sci->sc_dirty_files;
1111 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1112 i_dirty);
1113 list_for_each_entry_continue(ii, head, i_dirty) {
1114 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1115
1116 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1117 &nilfs_sc_file_ops);
1118 if (unlikely(err)) {
1119 sci->sc_stage.dirty_file_ptr =
1120 list_entry(ii->i_dirty.prev,
1121 struct nilfs_inode_info,
1122 i_dirty);
1123 goto break_or_fail;
1124 }
1125 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1126 /* XXX: required ? */
1127 }
1128 sci->sc_stage.dirty_file_ptr = NULL;
1129 if (mode == SC_FLUSH_FILE) {
1130 sci->sc_stage.scnt = NILFS_ST_DONE;
1131 return 0;
1132 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001133 sci->sc_stage.scnt++;
1134 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1135 /* Fall through */
1136 case NILFS_ST_IFILE:
1137 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
1138 &nilfs_sc_file_ops);
1139 if (unlikely(err))
1140 break;
1141 sci->sc_stage.scnt++;
1142 /* Creating a checkpoint */
1143 err = nilfs_segctor_create_checkpoint(sci);
1144 if (unlikely(err))
1145 break;
1146 /* Fall through */
1147 case NILFS_ST_CPFILE:
1148 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1149 &nilfs_sc_file_ops);
1150 if (unlikely(err))
1151 break;
1152 sci->sc_stage.scnt++; /* Fall through */
1153 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001154 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1155 sci->sc_nfreesegs, &ndone);
1156 if (unlikely(err)) {
1157 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1158 sci->sc_freesegs, ndone,
1159 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001160 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001161 }
1162 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1163
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001164 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1165 &nilfs_sc_file_ops);
1166 if (unlikely(err))
1167 break;
1168 sci->sc_stage.scnt++; /* Fall through */
1169 case NILFS_ST_DAT:
1170 dat_stage:
1171 err = nilfs_segctor_scan_file(sci, nilfs_dat_inode(nilfs),
1172 &nilfs_sc_dat_ops);
1173 if (unlikely(err))
1174 break;
1175 if (mode == SC_FLUSH_DAT) {
1176 sci->sc_stage.scnt = NILFS_ST_DONE;
1177 return 0;
1178 }
1179 sci->sc_stage.scnt++; /* Fall through */
1180 case NILFS_ST_SR:
1181 if (mode == SC_LSEG_SR) {
1182 /* Appending a super root */
1183 err = nilfs_segctor_add_super_root(sci);
1184 if (unlikely(err))
1185 break;
1186 }
1187 /* End of a logical segment */
1188 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1189 sci->sc_stage.scnt = NILFS_ST_DONE;
1190 return 0;
1191 case NILFS_ST_DSYNC:
1192 dsync_mode:
1193 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001194 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001195 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1196 break;
1197
1198 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1199 if (unlikely(err))
1200 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001201 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1202 sci->sc_stage.scnt = NILFS_ST_DONE;
1203 return 0;
1204 case NILFS_ST_DONE:
1205 return 0;
1206 default:
1207 BUG();
1208 }
1209
1210 break_or_fail:
1211 return err;
1212}
1213
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001214/**
1215 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1216 * @sci: nilfs_sc_info
1217 * @nilfs: nilfs object
1218 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001219static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1220 struct the_nilfs *nilfs)
1221{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001222 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001223 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001224 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001225
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001226 segbuf = nilfs_segbuf_new(sci->sc_super);
1227 if (unlikely(!segbuf))
1228 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001229
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001230 if (list_empty(&sci->sc_write_logs)) {
1231 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1232 nilfs->ns_pseg_offset, nilfs);
1233 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1234 nilfs_shift_to_next_segment(nilfs);
1235 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1236 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001237
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001238 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001239 nextnum = nilfs->ns_nextnum;
1240
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001241 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1242 /* Start from the head of a new full segment */
1243 alloc++;
1244 } else {
1245 /* Continue logs */
1246 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1247 nilfs_segbuf_map_cont(segbuf, prev);
1248 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1249 nextnum = prev->sb_nextnum;
1250
1251 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1252 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1253 segbuf->sb_sum.seg_seq++;
1254 alloc++;
1255 }
1256 }
1257
1258 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1259 if (err)
1260 goto failed;
1261
1262 if (alloc) {
1263 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1264 if (err)
1265 goto failed;
1266 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001267 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1268
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001269 BUG_ON(!list_empty(&sci->sc_segbufs));
1270 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1271 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001272 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001273
1274 failed:
1275 nilfs_segbuf_free(segbuf);
1276 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001277}
1278
1279static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1280 struct the_nilfs *nilfs, int nadd)
1281{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001282 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001283 struct inode *sufile = nilfs->ns_sufile;
1284 __u64 nextnextnum;
1285 LIST_HEAD(list);
1286 int err, ret, i;
1287
1288 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1289 /*
1290 * Since the segment specified with nextnum might be allocated during
1291 * the previous construction, the buffer including its segusage may
1292 * not be dirty. The following call ensures that the buffer is dirty
1293 * and will pin the buffer on memory until the sufile is written.
1294 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001295 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001296 if (unlikely(err))
1297 return err;
1298
1299 for (i = 0; i < nadd; i++) {
1300 /* extend segment info */
1301 err = -ENOMEM;
1302 segbuf = nilfs_segbuf_new(sci->sc_super);
1303 if (unlikely(!segbuf))
1304 goto failed;
1305
1306 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001307 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001308 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1309
1310 /* allocate the next next full segment */
1311 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1312 if (unlikely(err))
1313 goto failed_segbuf;
1314
1315 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1316 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1317
1318 list_add_tail(&segbuf->sb_list, &list);
1319 prev = segbuf;
1320 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001321 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001322 return 0;
1323
1324 failed_segbuf:
1325 nilfs_segbuf_free(segbuf);
1326 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001327 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001328 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001329 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001330 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001331 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001332 return err;
1333}
1334
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001335static void nilfs_free_incomplete_logs(struct list_head *logs,
1336 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001337{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001338 struct nilfs_segment_buffer *segbuf, *prev;
1339 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001340 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001341
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001342 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001343 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001344 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001345 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001346 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001347 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001348 /* Case 1: The first segment failed */
1349 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1350 /* Case 1a: Partial segment appended into an existing
1351 segment */
1352 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1353 segbuf->sb_fseg_end);
1354 else /* Case 1b: New full segment */
1355 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001356 }
1357
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001358 prev = segbuf;
1359 list_for_each_entry_continue(segbuf, logs, sb_list) {
1360 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1361 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1362 WARN_ON(ret); /* never fails */
1363 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001364 if (atomic_read(&segbuf->sb_err) &&
1365 segbuf->sb_segnum != nilfs->ns_nextnum)
1366 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001367 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1368 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001369 }
1370}
1371
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001372static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1373 struct inode *sufile)
1374{
1375 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001376 unsigned long live_blocks;
1377 int ret;
1378
1379 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001380 live_blocks = segbuf->sb_sum.nblocks +
1381 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001382 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1383 live_blocks,
1384 sci->sc_seg_ctime);
1385 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001386 }
1387}
1388
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001389static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001390{
1391 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001392 int ret;
1393
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001394 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001395 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1396 segbuf->sb_pseg_start -
1397 segbuf->sb_fseg_start, 0);
1398 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001399
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001400 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001401 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1402 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001403 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001404 }
1405}
1406
1407static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1408 struct nilfs_segment_buffer *last,
1409 struct inode *sufile)
1410{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001411 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001412 int ret;
1413
Ryusuke Konishie29df392009-11-29 16:51:16 +09001414 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001415 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1416 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001417 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001418 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001419 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001420}
1421
1422
1423static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1424 struct the_nilfs *nilfs, int mode)
1425{
1426 struct nilfs_cstage prev_stage = sci->sc_stage;
1427 int err, nadd = 1;
1428
1429 /* Collection retry loop */
1430 for (;;) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001431 sci->sc_nblk_this_inc = 0;
1432 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1433
1434 err = nilfs_segctor_reset_segment_buffer(sci);
1435 if (unlikely(err))
1436 goto failed;
1437
1438 err = nilfs_segctor_collect_blocks(sci, mode);
1439 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1440 if (!err)
1441 break;
1442
1443 if (unlikely(err != -E2BIG))
1444 goto failed;
1445
1446 /* The current segment is filled up */
1447 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1448 break;
1449
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001450 nilfs_clear_logs(&sci->sc_segbufs);
1451
1452 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1453 if (unlikely(err))
1454 return err;
1455
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001456 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1457 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1458 sci->sc_freesegs,
1459 sci->sc_nfreesegs,
1460 NULL);
1461 WARN_ON(err); /* do not happen */
1462 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001463 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1464 sci->sc_stage = prev_stage;
1465 }
1466 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1467 return 0;
1468
1469 failed:
1470 return err;
1471}
1472
1473static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1474 struct buffer_head *new_bh)
1475{
1476 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1477
1478 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1479 /* The caller must release old_bh */
1480}
1481
1482static int
1483nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1484 struct nilfs_segment_buffer *segbuf,
1485 int mode)
1486{
1487 struct inode *inode = NULL;
1488 sector_t blocknr;
1489 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1490 unsigned long nblocks = 0, ndatablk = 0;
1491 struct nilfs_sc_operations *sc_op = NULL;
1492 struct nilfs_segsum_pointer ssp;
1493 struct nilfs_finfo *finfo = NULL;
1494 union nilfs_binfo binfo;
1495 struct buffer_head *bh, *bh_org;
1496 ino_t ino = 0;
1497 int err = 0;
1498
1499 if (!nfinfo)
1500 goto out;
1501
1502 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1503 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1504 ssp.offset = sizeof(struct nilfs_segment_summary);
1505
1506 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001507 if (bh == segbuf->sb_super_root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001508 break;
1509 if (!finfo) {
1510 finfo = nilfs_segctor_map_segsum_entry(
1511 sci, &ssp, sizeof(*finfo));
1512 ino = le64_to_cpu(finfo->fi_ino);
1513 nblocks = le32_to_cpu(finfo->fi_nblocks);
1514 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1515
1516 if (buffer_nilfs_node(bh))
1517 inode = NILFS_BTNC_I(bh->b_page->mapping);
1518 else
1519 inode = NILFS_AS_I(bh->b_page->mapping);
1520
1521 if (mode == SC_LSEG_DSYNC)
1522 sc_op = &nilfs_sc_dsync_ops;
1523 else if (ino == NILFS_DAT_INO)
1524 sc_op = &nilfs_sc_dat_ops;
1525 else /* file blocks */
1526 sc_op = &nilfs_sc_file_ops;
1527 }
1528 bh_org = bh;
1529 get_bh(bh_org);
1530 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1531 &binfo);
1532 if (bh != bh_org)
1533 nilfs_list_replace_buffer(bh_org, bh);
1534 brelse(bh_org);
1535 if (unlikely(err))
1536 goto failed_bmap;
1537
1538 if (ndatablk > 0)
1539 sc_op->write_data_binfo(sci, &ssp, &binfo);
1540 else
1541 sc_op->write_node_binfo(sci, &ssp, &binfo);
1542
1543 blocknr++;
1544 if (--nblocks == 0) {
1545 finfo = NULL;
1546 if (--nfinfo == 0)
1547 break;
1548 } else if (ndatablk > 0)
1549 ndatablk--;
1550 }
1551 out:
1552 return 0;
1553
1554 failed_bmap:
1555 err = nilfs_handle_bmap_error(err, __func__, inode, sci->sc_super);
1556 return err;
1557}
1558
1559static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1560{
1561 struct nilfs_segment_buffer *segbuf;
1562 int err;
1563
1564 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1565 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1566 if (unlikely(err))
1567 return err;
1568 nilfs_segbuf_fill_in_segsum(segbuf);
1569 }
1570 return 0;
1571}
1572
1573static int
1574nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1575{
1576 struct page *clone_page;
1577 struct buffer_head *bh, *head, *bh2;
1578 void *kaddr;
1579
1580 bh = head = page_buffers(page);
1581
1582 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1583 if (unlikely(!clone_page))
1584 return -ENOMEM;
1585
1586 bh2 = page_buffers(clone_page);
1587 kaddr = kmap_atomic(page, KM_USER0);
1588 do {
1589 if (list_empty(&bh->b_assoc_buffers))
1590 continue;
1591 get_bh(bh2);
1592 page_cache_get(clone_page); /* for each bh */
1593 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1594 bh2->b_blocknr = bh->b_blocknr;
1595 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1596 list_add_tail(&bh->b_assoc_buffers, out);
1597 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1598 kunmap_atomic(kaddr, KM_USER0);
1599
1600 if (!TestSetPageWriteback(clone_page))
1601 inc_zone_page_state(clone_page, NR_WRITEBACK);
1602 unlock_page(clone_page);
1603
1604 return 0;
1605}
1606
1607static int nilfs_test_page_to_be_frozen(struct page *page)
1608{
1609 struct address_space *mapping = page->mapping;
1610
1611 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1612 return 0;
1613
1614 if (page_mapped(page)) {
1615 ClearPageChecked(page);
1616 return 1;
1617 }
1618 return PageChecked(page);
1619}
1620
1621static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1622{
1623 if (!page || PageWriteback(page))
1624 /* For split b-tree node pages, this function may be called
1625 twice. We ignore the 2nd or later calls by this check. */
1626 return 0;
1627
1628 lock_page(page);
1629 clear_page_dirty_for_io(page);
1630 set_page_writeback(page);
1631 unlock_page(page);
1632
1633 if (nilfs_test_page_to_be_frozen(page)) {
1634 int err = nilfs_copy_replace_page_buffers(page, out);
1635 if (unlikely(err))
1636 return err;
1637 }
1638 return 0;
1639}
1640
1641static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1642 struct page **failed_page)
1643{
1644 struct nilfs_segment_buffer *segbuf;
1645 struct page *bd_page = NULL, *fs_page = NULL;
1646 struct list_head *list = &sci->sc_copied_buffers;
1647 int err;
1648
1649 *failed_page = NULL;
1650 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1651 struct buffer_head *bh;
1652
1653 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1654 b_assoc_buffers) {
1655 if (bh->b_page != bd_page) {
1656 if (bd_page) {
1657 lock_page(bd_page);
1658 clear_page_dirty_for_io(bd_page);
1659 set_page_writeback(bd_page);
1660 unlock_page(bd_page);
1661 }
1662 bd_page = bh->b_page;
1663 }
1664 }
1665
1666 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1667 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001668 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001669 if (bh->b_page != bd_page) {
1670 lock_page(bd_page);
1671 clear_page_dirty_for_io(bd_page);
1672 set_page_writeback(bd_page);
1673 unlock_page(bd_page);
1674 bd_page = bh->b_page;
1675 }
1676 break;
1677 }
1678 if (bh->b_page != fs_page) {
1679 err = nilfs_begin_page_io(fs_page, list);
1680 if (unlikely(err)) {
1681 *failed_page = fs_page;
1682 goto out;
1683 }
1684 fs_page = bh->b_page;
1685 }
1686 }
1687 }
1688 if (bd_page) {
1689 lock_page(bd_page);
1690 clear_page_dirty_for_io(bd_page);
1691 set_page_writeback(bd_page);
1692 unlock_page(bd_page);
1693 }
1694 err = nilfs_begin_page_io(fs_page, list);
1695 if (unlikely(err))
1696 *failed_page = fs_page;
1697 out:
1698 return err;
1699}
1700
1701static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001702 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001703{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001704 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001705
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001706 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001707 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1708 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001709}
1710
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001711static void __nilfs_end_page_io(struct page *page, int err)
1712{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001713 if (!err) {
1714 if (!nilfs_page_buffers_clean(page))
1715 __set_page_dirty_nobuffers(page);
1716 ClearPageError(page);
1717 } else {
1718 __set_page_dirty_nobuffers(page);
1719 SetPageError(page);
1720 }
1721
1722 if (buffer_nilfs_allocated(page_buffers(page))) {
1723 if (TestClearPageWriteback(page))
1724 dec_zone_page_state(page, NR_WRITEBACK);
1725 } else
1726 end_page_writeback(page);
1727}
1728
1729static void nilfs_end_page_io(struct page *page, int err)
1730{
1731 if (!page)
1732 return;
1733
Ryusuke Konishia9777842009-07-28 17:55:29 +09001734 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001735 /*
1736 * For b-tree node pages, this function may be called twice
1737 * or more because they might be split in a segment.
1738 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001739 if (PageDirty(page)) {
1740 /*
1741 * For pages holding split b-tree node buffers, dirty
1742 * flag on the buffers may be cleared discretely.
1743 * In that case, the page is once redirtied for
1744 * remaining buffers, and it must be cancelled if
1745 * all the buffers get cleaned later.
1746 */
1747 lock_page(page);
1748 if (nilfs_page_buffers_clean(page))
1749 __nilfs_clear_page_dirty(page);
1750 unlock_page(page);
1751 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001752 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001753 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001754
1755 __nilfs_end_page_io(page, err);
1756}
1757
1758static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1759{
1760 struct buffer_head *bh, *head;
1761 struct page *page;
1762
1763 while (!list_empty(list)) {
1764 bh = list_entry(list->next, struct buffer_head,
1765 b_assoc_buffers);
1766 page = bh->b_page;
1767 page_cache_get(page);
1768 head = bh = page_buffers(page);
1769 do {
1770 if (!list_empty(&bh->b_assoc_buffers)) {
1771 list_del_init(&bh->b_assoc_buffers);
1772 if (!err) {
1773 set_buffer_uptodate(bh);
1774 clear_buffer_dirty(bh);
1775 clear_buffer_nilfs_volatile(bh);
1776 }
1777 brelse(bh); /* for b_assoc_buffers */
1778 }
1779 } while ((bh = bh->b_this_page) != head);
1780
1781 __nilfs_end_page_io(page, err);
1782 page_cache_release(page);
1783 }
1784}
1785
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001786static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001787 int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001788{
1789 struct nilfs_segment_buffer *segbuf;
1790 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001791 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001792
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001793 if (list_empty(logs))
1794 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001795
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001796 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001797 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1798 b_assoc_buffers) {
1799 if (bh->b_page != bd_page) {
1800 if (bd_page)
1801 end_page_writeback(bd_page);
1802 bd_page = bh->b_page;
1803 }
1804 }
1805
1806 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1807 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001808 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001809 if (bh->b_page != bd_page) {
1810 end_page_writeback(bd_page);
1811 bd_page = bh->b_page;
1812 }
1813 break;
1814 }
1815 if (bh->b_page != fs_page) {
1816 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001817 if (fs_page && fs_page == failed_page)
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001818 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001819 fs_page = bh->b_page;
1820 }
1821 }
1822 }
1823 if (bd_page)
1824 end_page_writeback(bd_page);
1825
1826 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001827}
1828
1829static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1830 struct the_nilfs *nilfs, int err)
1831{
1832 LIST_HEAD(logs);
1833 int ret;
1834
1835 list_splice_tail_init(&sci->sc_write_logs, &logs);
1836 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001837 nilfs_abort_logs(&logs, NULL, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001838
1839 list_splice_tail_init(&sci->sc_segbufs, &logs);
1840 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1841 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001842 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001843
1844 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1845 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1846 sci->sc_freesegs,
1847 sci->sc_nfreesegs,
1848 NULL);
1849 WARN_ON(ret); /* do not happen */
1850 }
1851
1852 nilfs_destroy_logs(&logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001853}
1854
1855static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1856 struct nilfs_segment_buffer *segbuf)
1857{
1858 nilfs->ns_segnum = segbuf->sb_segnum;
1859 nilfs->ns_nextnum = segbuf->sb_nextnum;
1860 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1861 + segbuf->sb_sum.nblocks;
1862 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1863 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1864}
1865
1866static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1867{
1868 struct nilfs_segment_buffer *segbuf;
1869 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09001870 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001871 int update_sr = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001872
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001873 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001874 struct buffer_head *bh;
1875
1876 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1877 b_assoc_buffers) {
1878 set_buffer_uptodate(bh);
1879 clear_buffer_dirty(bh);
1880 if (bh->b_page != bd_page) {
1881 if (bd_page)
1882 end_page_writeback(bd_page);
1883 bd_page = bh->b_page;
1884 }
1885 }
1886 /*
1887 * We assume that the buffers which belong to the same page
1888 * continue over the buffer list.
1889 * Under this assumption, the last BHs of pages is
1890 * identifiable by the discontinuity of bh->b_page
1891 * (page != fs_page).
1892 *
1893 * For B-tree node blocks, however, this assumption is not
1894 * guaranteed. The cleanup code of B-tree node pages needs
1895 * special care.
1896 */
1897 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1898 b_assoc_buffers) {
1899 set_buffer_uptodate(bh);
1900 clear_buffer_dirty(bh);
1901 clear_buffer_nilfs_volatile(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001902 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001903 if (bh->b_page != bd_page) {
1904 end_page_writeback(bd_page);
1905 bd_page = bh->b_page;
1906 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001907 update_sr = true;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001908 break;
1909 }
1910 if (bh->b_page != fs_page) {
1911 nilfs_end_page_io(fs_page, 0);
1912 fs_page = bh->b_page;
1913 }
1914 }
1915
1916 if (!NILFS_SEG_SIMPLEX(&segbuf->sb_sum)) {
1917 if (NILFS_SEG_LOGBGN(&segbuf->sb_sum)) {
1918 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1919 sci->sc_lseg_stime = jiffies;
1920 }
1921 if (NILFS_SEG_LOGEND(&segbuf->sb_sum))
1922 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1923 }
1924 }
1925 /*
1926 * Since pages may continue over multiple segment buffers,
1927 * end of the last page must be checked outside of the loop.
1928 */
1929 if (bd_page)
1930 end_page_writeback(bd_page);
1931
1932 nilfs_end_page_io(fs_page, 0);
1933
1934 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
1935
1936 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1937
1938 if (nilfs_doing_gc()) {
1939 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
1940 if (update_sr)
1941 nilfs_commit_gcdat_inode(nilfs);
Ryusuke Konishi1088dcf2009-04-06 19:01:51 -07001942 } else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001943 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001944
1945 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1946
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001947 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001948 nilfs_set_next_segment(nilfs, segbuf);
1949
1950 if (update_sr) {
1951 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07001952 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09001953 set_nilfs_sb_dirty(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001954
Ryusuke Konishic96fa462009-04-06 19:01:57 -07001955 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001956 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1957 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001958 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001959 } else
1960 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1961}
1962
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001963static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1964{
1965 int ret;
1966
1967 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1968 if (!ret) {
1969 nilfs_segctor_complete_write(sci);
1970 nilfs_destroy_logs(&sci->sc_write_logs);
1971 }
1972 return ret;
1973}
1974
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001975static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
1976 struct nilfs_sb_info *sbi)
1977{
1978 struct nilfs_inode_info *ii, *n;
1979 __u64 cno = sbi->s_nilfs->ns_cno;
1980
1981 spin_lock(&sbi->s_inode_lock);
1982 retry:
1983 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
1984 if (!ii->i_bh) {
1985 struct buffer_head *ibh;
1986 int err;
1987
1988 spin_unlock(&sbi->s_inode_lock);
1989 err = nilfs_ifile_get_inode_block(
1990 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
1991 if (unlikely(err)) {
1992 nilfs_warning(sbi->s_super, __func__,
1993 "failed to get inode block.\n");
1994 return err;
1995 }
1996 nilfs_mdt_mark_buffer_dirty(ibh);
1997 nilfs_mdt_mark_dirty(sbi->s_ifile);
1998 spin_lock(&sbi->s_inode_lock);
1999 if (likely(!ii->i_bh))
2000 ii->i_bh = ibh;
2001 else
2002 brelse(ibh);
2003 goto retry;
2004 }
2005 ii->i_cno = cno;
2006
2007 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2008 set_bit(NILFS_I_BUSY, &ii->i_state);
2009 list_del(&ii->i_dirty);
2010 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2011 }
2012 spin_unlock(&sbi->s_inode_lock);
2013
2014 NILFS_I(sbi->s_ifile)->i_cno = cno;
2015
2016 return 0;
2017}
2018
2019static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2020 struct nilfs_sb_info *sbi)
2021{
2022 struct nilfs_transaction_info *ti = current->journal_info;
2023 struct nilfs_inode_info *ii, *n;
2024 __u64 cno = sbi->s_nilfs->ns_cno;
2025
2026 spin_lock(&sbi->s_inode_lock);
2027 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2028 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
2029 test_bit(NILFS_I_DIRTY, &ii->i_state)) {
2030 /* The current checkpoint number (=nilfs->ns_cno) is
2031 changed between check-in and check-out only if the
2032 super root is written out. So, we can update i_cno
2033 for the inodes that remain in the dirty list. */
2034 ii->i_cno = cno;
2035 continue;
2036 }
2037 clear_bit(NILFS_I_BUSY, &ii->i_state);
2038 brelse(ii->i_bh);
2039 ii->i_bh = NULL;
2040 list_del(&ii->i_dirty);
2041 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2042 }
2043 spin_unlock(&sbi->s_inode_lock);
2044}
2045
2046/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002047 * Main procedure of segment constructor
2048 */
2049static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2050{
2051 struct nilfs_sb_info *sbi = sci->sc_sbi;
2052 struct the_nilfs *nilfs = sbi->s_nilfs;
2053 struct page *failed_page;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002054 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002055
2056 sci->sc_stage.scnt = NILFS_ST_INIT;
2057
2058 err = nilfs_segctor_check_in_files(sci, sbi);
2059 if (unlikely(err))
2060 goto out;
2061
2062 if (nilfs_test_metadata_dirty(sbi))
2063 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2064
2065 if (nilfs_segctor_clean(sci))
2066 goto out;
2067
2068 do {
2069 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2070
2071 err = nilfs_segctor_begin_construction(sci, nilfs);
2072 if (unlikely(err))
2073 goto out;
2074
2075 /* Update time stamp */
2076 sci->sc_seg_ctime = get_seconds();
2077
2078 err = nilfs_segctor_collect(sci, nilfs, mode);
2079 if (unlikely(err))
2080 goto failed;
2081
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002082 /* Avoid empty segment */
2083 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
2084 NILFS_SEG_EMPTY(&sci->sc_curseg->sb_sum)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002085 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002086 goto out;
2087 }
2088
2089 err = nilfs_segctor_assign(sci, mode);
2090 if (unlikely(err))
2091 goto failed;
2092
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002093 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2094 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2095
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002096 if (mode == SC_LSEG_SR &&
2097 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002098 err = nilfs_segctor_fill_in_checkpoint(sci);
2099 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002100 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002101
2102 nilfs_segctor_fill_in_super_root(sci, nilfs);
2103 }
2104 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2105
2106 /* Write partial segments */
2107 err = nilfs_segctor_prepare_write(sci, &failed_page);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002108 if (err) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002109 nilfs_abort_logs(&sci->sc_segbufs, failed_page, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002110 goto failed_to_write;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002111 }
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +09002112
2113 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2114 nilfs->ns_crc_seed);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002115
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002116 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002117 if (unlikely(err))
2118 goto failed_to_write;
2119
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002120 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2121 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2122 /*
2123 * At this point, we avoid double buffering
2124 * for blocksize < pagesize because page dirty
2125 * flag is turned off during write and dirty
2126 * buffers are not properly collected for
2127 * pages crossing over segments.
2128 */
2129 err = nilfs_segctor_wait(sci);
2130 if (err)
2131 goto failed_to_write;
2132 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002133 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2134
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002135 out:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002136 nilfs_segctor_check_out_files(sci, sbi);
2137 return err;
2138
2139 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002140 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2141 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002142
2143 failed:
2144 if (nilfs_doing_gc())
2145 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002146 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002147 goto out;
2148}
2149
2150/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002151 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002152 * @sci: nilfs_sc_info
2153 *
2154 * If the timer has already been set, it ignores the new request.
2155 * This function MUST be called within a section locking the segment
2156 * semaphore.
2157 */
2158static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2159{
2160 spin_lock(&sci->sc_state_lock);
2161 if (sci->sc_timer && !(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2162 sci->sc_timer->expires = jiffies + sci->sc_interval;
2163 add_timer(sci->sc_timer);
2164 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2165 }
2166 spin_unlock(&sci->sc_state_lock);
2167}
2168
2169static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2170{
2171 spin_lock(&sci->sc_state_lock);
2172 if (!(sci->sc_flush_request & (1 << bn))) {
2173 unsigned long prev_req = sci->sc_flush_request;
2174
2175 sci->sc_flush_request |= (1 << bn);
2176 if (!prev_req)
2177 wake_up(&sci->sc_wait_daemon);
2178 }
2179 spin_unlock(&sci->sc_state_lock);
2180}
2181
2182/**
2183 * nilfs_flush_segment - trigger a segment construction for resource control
2184 * @sb: super block
2185 * @ino: inode number of the file to be flushed out.
2186 */
2187void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2188{
2189 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2190 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2191
2192 if (!sci || nilfs_doing_construction())
2193 return;
2194 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2195 /* assign bit 0 to data files */
2196}
2197
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002198struct nilfs_segctor_wait_request {
2199 wait_queue_t wq;
2200 __u32 seq;
2201 int err;
2202 atomic_t done;
2203};
2204
2205static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2206{
2207 struct nilfs_segctor_wait_request wait_req;
2208 int err = 0;
2209
2210 spin_lock(&sci->sc_state_lock);
2211 init_wait(&wait_req.wq);
2212 wait_req.err = 0;
2213 atomic_set(&wait_req.done, 0);
2214 wait_req.seq = ++sci->sc_seq_request;
2215 spin_unlock(&sci->sc_state_lock);
2216
2217 init_waitqueue_entry(&wait_req.wq, current);
2218 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2219 set_current_state(TASK_INTERRUPTIBLE);
2220 wake_up(&sci->sc_wait_daemon);
2221
2222 for (;;) {
2223 if (atomic_read(&wait_req.done)) {
2224 err = wait_req.err;
2225 break;
2226 }
2227 if (!signal_pending(current)) {
2228 schedule();
2229 continue;
2230 }
2231 err = -ERESTARTSYS;
2232 break;
2233 }
2234 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2235 return err;
2236}
2237
2238static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2239{
2240 struct nilfs_segctor_wait_request *wrq, *n;
2241 unsigned long flags;
2242
2243 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2244 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2245 wq.task_list) {
2246 if (!atomic_read(&wrq->done) &&
2247 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2248 wrq->err = err;
2249 atomic_set(&wrq->done, 1);
2250 }
2251 if (atomic_read(&wrq->done)) {
2252 wrq->wq.func(&wrq->wq,
2253 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2254 0, NULL);
2255 }
2256 }
2257 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2258}
2259
2260/**
2261 * nilfs_construct_segment - construct a logical segment
2262 * @sb: super block
2263 *
2264 * Return Value: On success, 0 is retured. On errors, one of the following
2265 * negative error code is returned.
2266 *
2267 * %-EROFS - Read only filesystem.
2268 *
2269 * %-EIO - I/O error
2270 *
2271 * %-ENOSPC - No space left on device (only in a panic state).
2272 *
2273 * %-ERESTARTSYS - Interrupted.
2274 *
2275 * %-ENOMEM - Insufficient memory available.
2276 */
2277int nilfs_construct_segment(struct super_block *sb)
2278{
2279 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2280 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2281 struct nilfs_transaction_info *ti;
2282 int err;
2283
2284 if (!sci)
2285 return -EROFS;
2286
2287 /* A call inside transactions causes a deadlock. */
2288 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2289
2290 err = nilfs_segctor_sync(sci);
2291 return err;
2292}
2293
2294/**
2295 * nilfs_construct_dsync_segment - construct a data-only logical segment
2296 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002297 * @inode: inode whose data blocks should be written out
2298 * @start: start byte offset
2299 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002300 *
2301 * Return Value: On success, 0 is retured. On errors, one of the following
2302 * negative error code is returned.
2303 *
2304 * %-EROFS - Read only filesystem.
2305 *
2306 * %-EIO - I/O error
2307 *
2308 * %-ENOSPC - No space left on device (only in a panic state).
2309 *
2310 * %-ERESTARTSYS - Interrupted.
2311 *
2312 * %-ENOMEM - Insufficient memory available.
2313 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002314int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2315 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002316{
2317 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2318 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2319 struct nilfs_inode_info *ii;
2320 struct nilfs_transaction_info ti;
2321 int err = 0;
2322
2323 if (!sci)
2324 return -EROFS;
2325
2326 nilfs_transaction_lock(sbi, &ti, 0);
2327
2328 ii = NILFS_I(inode);
2329 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2330 nilfs_test_opt(sbi, STRICT_ORDER) ||
2331 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2332 nilfs_discontinued(sbi->s_nilfs)) {
2333 nilfs_transaction_unlock(sbi);
2334 err = nilfs_segctor_sync(sci);
2335 return err;
2336 }
2337
2338 spin_lock(&sbi->s_inode_lock);
2339 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2340 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2341 spin_unlock(&sbi->s_inode_lock);
2342 nilfs_transaction_unlock(sbi);
2343 return 0;
2344 }
2345 spin_unlock(&sbi->s_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002346 sci->sc_dsync_inode = ii;
2347 sci->sc_dsync_start = start;
2348 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002349
2350 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2351
2352 nilfs_transaction_unlock(sbi);
2353 return err;
2354}
2355
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002356#define FLUSH_FILE_BIT (0x1) /* data file only */
2357#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2358
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002359/**
2360 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2361 * @sci: segment constructor object
2362 */
2363static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002364{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002365 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002366 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002367 spin_unlock(&sci->sc_state_lock);
2368
2369 if (sci->sc_timer)
2370 del_timer_sync(sci->sc_timer);
2371}
2372
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002373/**
2374 * nilfs_segctor_notify - notify the result of request to caller threads
2375 * @sci: segment constructor object
2376 * @mode: mode of log forming
2377 * @err: error code to be notified
2378 */
2379static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002380{
2381 /* Clear requests (even when the construction failed) */
2382 spin_lock(&sci->sc_state_lock);
2383
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002384 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002385 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002386 sci->sc_seq_done = sci->sc_seq_accepted;
2387 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002388 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002389 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002390 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002391 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002392 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002393 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002394
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002395 /* re-enable timer if checkpoint creation was not done */
2396 if (sci->sc_timer && (sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2397 time_before(jiffies, sci->sc_timer->expires))
2398 add_timer(sci->sc_timer);
2399 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002400 spin_unlock(&sci->sc_state_lock);
2401}
2402
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002403/**
2404 * nilfs_segctor_construct - form logs and write them to disk
2405 * @sci: segment constructor object
2406 * @mode: mode of log forming
2407 */
2408static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002409{
2410 struct nilfs_sb_info *sbi = sci->sc_sbi;
2411 struct the_nilfs *nilfs = sbi->s_nilfs;
2412 int err = 0;
2413
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002414 nilfs_segctor_accept(sci);
2415
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002416 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002417 mode = SC_LSEG_SR;
2418 if (!nilfs_segctor_confirm(sci))
2419 err = nilfs_segctor_do_construct(sci, mode);
2420
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002421 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002422 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002423 atomic_set(&nilfs->ns_ndirtyblks, 0);
2424 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2425 nilfs_discontinued(nilfs)) {
2426 down_write(&nilfs->ns_sem);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002427 err = nilfs_commit_super(
2428 sbi, nilfs_altsb_need_update(nilfs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002429 up_write(&nilfs->ns_sem);
2430 }
2431 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002432
2433 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002434 return err;
2435}
2436
2437static void nilfs_construction_timeout(unsigned long data)
2438{
2439 struct task_struct *p = (struct task_struct *)data;
2440 wake_up_process(p);
2441}
2442
2443static void
2444nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2445{
2446 struct nilfs_inode_info *ii, *n;
2447
2448 list_for_each_entry_safe(ii, n, head, i_dirty) {
2449 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2450 continue;
2451 hlist_del_init(&ii->vfs_inode.i_hash);
2452 list_del_init(&ii->i_dirty);
2453 nilfs_clear_gcinode(&ii->vfs_inode);
2454 }
2455}
2456
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002457int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2458 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002459{
2460 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2461 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2462 struct the_nilfs *nilfs = sbi->s_nilfs;
2463 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002464 int err;
2465
2466 if (unlikely(!sci))
2467 return -EROFS;
2468
2469 nilfs_transaction_lock(sbi, &ti, 1);
2470
2471 err = nilfs_init_gcdat_inode(nilfs);
2472 if (unlikely(err))
2473 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002474
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002475 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002476 if (unlikely(err))
2477 goto out_unlock;
2478
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002479 sci->sc_freesegs = kbufs[4];
2480 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002481 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002482
2483 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002484 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002485 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002486
2487 if (likely(!err))
2488 break;
2489
2490 nilfs_warning(sb, __func__,
2491 "segment construction failed. (err=%d)", err);
2492 set_current_state(TASK_INTERRUPTIBLE);
2493 schedule_timeout(sci->sc_interval);
2494 }
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002495 if (nilfs_test_opt(sbi, DISCARD)) {
2496 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2497 sci->sc_nfreesegs);
2498 if (ret) {
2499 printk(KERN_WARNING
2500 "NILFS warning: error %d on discard request, "
2501 "turning discards off for the device\n", ret);
2502 nilfs_clear_opt(sbi, DISCARD);
2503 }
2504 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002505
2506 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002507 sci->sc_freesegs = NULL;
2508 sci->sc_nfreesegs = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002509 nilfs_clear_gcdat_inode(nilfs);
2510 nilfs_transaction_unlock(sbi);
2511 return err;
2512}
2513
2514static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2515{
2516 struct nilfs_sb_info *sbi = sci->sc_sbi;
2517 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002518
2519 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002520 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002521
2522 /*
2523 * Unclosed segment should be retried. We do this using sc_timer.
2524 * Timeout of sc_timer will invoke complete construction which leads
2525 * to close the current logical segment.
2526 */
2527 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2528 nilfs_segctor_start_timer(sci);
2529
2530 nilfs_transaction_unlock(sbi);
2531}
2532
2533static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2534{
2535 int mode = 0;
2536 int err;
2537
2538 spin_lock(&sci->sc_state_lock);
2539 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2540 SC_FLUSH_DAT : SC_FLUSH_FILE;
2541 spin_unlock(&sci->sc_state_lock);
2542
2543 if (mode) {
2544 err = nilfs_segctor_do_construct(sci, mode);
2545
2546 spin_lock(&sci->sc_state_lock);
2547 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2548 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2549 spin_unlock(&sci->sc_state_lock);
2550 }
2551 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2552}
2553
2554static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2555{
2556 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2557 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2558 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2559 return SC_FLUSH_FILE;
2560 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2561 return SC_FLUSH_DAT;
2562 }
2563 return SC_LSEG_SR;
2564}
2565
2566/**
2567 * nilfs_segctor_thread - main loop of the segment constructor thread.
2568 * @arg: pointer to a struct nilfs_sc_info.
2569 *
2570 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2571 * to execute segment constructions.
2572 */
2573static int nilfs_segctor_thread(void *arg)
2574{
2575 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002576 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002577 struct timer_list timer;
2578 int timeout = 0;
2579
2580 init_timer(&timer);
2581 timer.data = (unsigned long)current;
2582 timer.function = nilfs_construction_timeout;
2583 sci->sc_timer = &timer;
2584
2585 /* start sync. */
2586 sci->sc_task = current;
2587 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2588 printk(KERN_INFO
2589 "segctord starting. Construction interval = %lu seconds, "
2590 "CP frequency < %lu seconds\n",
2591 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2592
2593 spin_lock(&sci->sc_state_lock);
2594 loop:
2595 for (;;) {
2596 int mode;
2597
2598 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2599 goto end_thread;
2600
2601 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2602 mode = SC_LSEG_SR;
2603 else if (!sci->sc_flush_request)
2604 break;
2605 else
2606 mode = nilfs_segctor_flush_mode(sci);
2607
2608 spin_unlock(&sci->sc_state_lock);
2609 nilfs_segctor_thread_construct(sci, mode);
2610 spin_lock(&sci->sc_state_lock);
2611 timeout = 0;
2612 }
2613
2614
2615 if (freezing(current)) {
2616 spin_unlock(&sci->sc_state_lock);
2617 refrigerator();
2618 spin_lock(&sci->sc_state_lock);
2619 } else {
2620 DEFINE_WAIT(wait);
2621 int should_sleep = 1;
2622
2623 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2624 TASK_INTERRUPTIBLE);
2625
2626 if (sci->sc_seq_request != sci->sc_seq_done)
2627 should_sleep = 0;
2628 else if (sci->sc_flush_request)
2629 should_sleep = 0;
2630 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2631 should_sleep = time_before(jiffies,
2632 sci->sc_timer->expires);
2633
2634 if (should_sleep) {
2635 spin_unlock(&sci->sc_state_lock);
2636 schedule();
2637 spin_lock(&sci->sc_state_lock);
2638 }
2639 finish_wait(&sci->sc_wait_daemon, &wait);
2640 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2641 time_after_eq(jiffies, sci->sc_timer->expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002642
2643 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002644 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002645 }
2646 goto loop;
2647
2648 end_thread:
2649 spin_unlock(&sci->sc_state_lock);
2650 del_timer_sync(sci->sc_timer);
2651 sci->sc_timer = NULL;
2652
2653 /* end sync. */
2654 sci->sc_task = NULL;
2655 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2656 return 0;
2657}
2658
2659static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2660{
2661 struct task_struct *t;
2662
2663 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2664 if (IS_ERR(t)) {
2665 int err = PTR_ERR(t);
2666
2667 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2668 err);
2669 return err;
2670 }
2671 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2672 return 0;
2673}
2674
2675static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2676{
2677 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2678
2679 while (sci->sc_task) {
2680 wake_up(&sci->sc_wait_daemon);
2681 spin_unlock(&sci->sc_state_lock);
2682 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2683 spin_lock(&sci->sc_state_lock);
2684 }
2685}
2686
Ryusuke Konishicece5522009-04-06 19:01:58 -07002687static int nilfs_segctor_init(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002688{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002689 sci->sc_seq_done = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002690
Ryusuke Konishicece5522009-04-06 19:01:58 -07002691 return nilfs_segctor_start_thread(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002692}
2693
2694/*
2695 * Setup & clean-up functions
2696 */
2697static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
2698{
2699 struct nilfs_sc_info *sci;
2700
2701 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2702 if (!sci)
2703 return NULL;
2704
2705 sci->sc_sbi = sbi;
2706 sci->sc_super = sbi->s_super;
2707
2708 init_waitqueue_head(&sci->sc_wait_request);
2709 init_waitqueue_head(&sci->sc_wait_daemon);
2710 init_waitqueue_head(&sci->sc_wait_task);
2711 spin_lock_init(&sci->sc_state_lock);
2712 INIT_LIST_HEAD(&sci->sc_dirty_files);
2713 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002714 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002715 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002716 INIT_LIST_HEAD(&sci->sc_copied_buffers);
2717
2718 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2719 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2720 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2721
2722 if (sbi->s_interval)
2723 sci->sc_interval = sbi->s_interval;
2724 if (sbi->s_watermark)
2725 sci->sc_watermark = sbi->s_watermark;
2726 return sci;
2727}
2728
2729static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2730{
2731 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2732
2733 /* The segctord thread was stopped and its timer was removed.
2734 But some tasks remain. */
2735 do {
2736 struct nilfs_sb_info *sbi = sci->sc_sbi;
2737 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002738
2739 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002740 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002741 nilfs_transaction_unlock(sbi);
2742
2743 } while (ret && retrycount-- > 0);
2744}
2745
2746/**
2747 * nilfs_segctor_destroy - destroy the segment constructor.
2748 * @sci: nilfs_sc_info
2749 *
2750 * nilfs_segctor_destroy() kills the segctord thread and frees
2751 * the nilfs_sc_info struct.
2752 * Caller must hold the segment semaphore.
2753 */
2754static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2755{
2756 struct nilfs_sb_info *sbi = sci->sc_sbi;
2757 int flag;
2758
2759 up_write(&sbi->s_nilfs->ns_segctor_sem);
2760
2761 spin_lock(&sci->sc_state_lock);
2762 nilfs_segctor_kill_thread(sci);
2763 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2764 || sci->sc_seq_request != sci->sc_seq_done);
2765 spin_unlock(&sci->sc_state_lock);
2766
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002767 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002768 nilfs_segctor_write_out(sci);
2769
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002770 WARN_ON(!list_empty(&sci->sc_copied_buffers));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002771
2772 if (!list_empty(&sci->sc_dirty_files)) {
2773 nilfs_warning(sbi->s_super, __func__,
2774 "dirty file(s) after the final construction\n");
2775 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
2776 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002777
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002778 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002779 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002780
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002781 down_write(&sbi->s_nilfs->ns_segctor_sem);
2782
2783 kfree(sci);
2784}
2785
2786/**
2787 * nilfs_attach_segment_constructor - attach a segment constructor
2788 * @sbi: nilfs_sb_info
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002789 *
2790 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
Ryusuke Konishi7a650042010-03-14 03:32:40 +09002791 * initializes it, and starts the segment constructor.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002792 *
2793 * Return Value: On success, 0 is returned. On error, one of the following
2794 * negative error code is returned.
2795 *
2796 * %-ENOMEM - Insufficient memory available.
2797 */
Ryusuke Konishicece5522009-04-06 19:01:58 -07002798int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002799{
2800 struct the_nilfs *nilfs = sbi->s_nilfs;
2801 int err;
2802
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002803 if (NILFS_SC(sbi)) {
2804 /*
2805 * This happens if the filesystem was remounted
2806 * read/write after nilfs_error degenerated it into a
2807 * read-only mount.
2808 */
2809 nilfs_detach_segment_constructor(sbi);
2810 }
2811
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002812 sbi->s_sc_info = nilfs_segctor_new(sbi);
2813 if (!sbi->s_sc_info)
2814 return -ENOMEM;
2815
2816 nilfs_attach_writer(nilfs, sbi);
Ryusuke Konishicece5522009-04-06 19:01:58 -07002817 err = nilfs_segctor_init(NILFS_SC(sbi));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002818 if (err) {
2819 nilfs_detach_writer(nilfs, sbi);
2820 kfree(sbi->s_sc_info);
2821 sbi->s_sc_info = NULL;
2822 }
2823 return err;
2824}
2825
2826/**
2827 * nilfs_detach_segment_constructor - destroy the segment constructor
2828 * @sbi: nilfs_sb_info
2829 *
2830 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2831 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2832 */
2833void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2834{
2835 struct the_nilfs *nilfs = sbi->s_nilfs;
2836 LIST_HEAD(garbage_list);
2837
2838 down_write(&nilfs->ns_segctor_sem);
2839 if (NILFS_SC(sbi)) {
2840 nilfs_segctor_destroy(NILFS_SC(sbi));
2841 sbi->s_sc_info = NULL;
2842 }
2843
2844 /* Force to free the list of dirty files */
2845 spin_lock(&sbi->s_inode_lock);
2846 if (!list_empty(&sbi->s_dirty_files)) {
2847 list_splice_init(&sbi->s_dirty_files, &garbage_list);
2848 nilfs_warning(sbi->s_super, __func__,
2849 "Non empty dirty list after the last "
2850 "segment construction\n");
2851 }
2852 spin_unlock(&sbi->s_inode_lock);
2853 up_write(&nilfs->ns_segctor_sem);
2854
2855 nilfs_dispose_list(sbi, &garbage_list, 1);
2856 nilfs_detach_writer(nilfs, sbi);
2857}