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