blob: 6a7dbd8451db73d40080156ff325c82183437f51 [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 }
438 err = nilfs_segbuf_extend_payload(segbuf, &sci->sc_super_root);
439 if (likely(!err))
440 segbuf->sb_sum.flags |= NILFS_SS_SR;
441 return err;
442}
443
444/*
445 * Functions for making segment summary and payloads
446 */
447static int nilfs_segctor_segsum_block_required(
448 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
449 unsigned binfo_size)
450{
451 unsigned blocksize = sci->sc_super->s_blocksize;
452 /* Size of finfo and binfo is enough small against blocksize */
453
454 return ssp->offset + binfo_size +
455 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
456 blocksize;
457}
458
459static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
460 struct inode *inode)
461{
462 sci->sc_curseg->sb_sum.nfinfo++;
463 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
464 nilfs_segctor_map_segsum_entry(
465 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
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
935/*
936 * CRC calculation routines
937 */
938static void nilfs_fill_in_super_root_crc(struct buffer_head *bh_sr, u32 seed)
939{
940 struct nilfs_super_root *raw_sr =
941 (struct nilfs_super_root *)bh_sr->b_data;
942 u32 crc;
943
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700944 crc = crc32_le(seed,
945 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
946 NILFS_SR_BYTES - sizeof(raw_sr->sr_sum));
947 raw_sr->sr_sum = cpu_to_le32(crc);
948}
949
950static void nilfs_segctor_fill_in_checksums(struct nilfs_sc_info *sci,
951 u32 seed)
952{
953 struct nilfs_segment_buffer *segbuf;
954
955 if (sci->sc_super_root)
956 nilfs_fill_in_super_root_crc(sci->sc_super_root, seed);
957
958 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
959 nilfs_segbuf_fill_in_segsum_crc(segbuf, seed);
960 nilfs_segbuf_fill_in_data_crc(segbuf, seed);
961 }
962}
963
964static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
965 struct the_nilfs *nilfs)
966{
967 struct buffer_head *bh_sr = sci->sc_super_root;
968 struct nilfs_super_root *raw_sr =
969 (struct nilfs_super_root *)bh_sr->b_data;
970 unsigned isz = nilfs->ns_inode_size;
971
972 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
973 raw_sr->sr_nongc_ctime
974 = cpu_to_le64(nilfs_doing_gc() ?
975 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
976 raw_sr->sr_flags = 0;
977
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900978 nilfs_write_inode_common(nilfs_dat_inode(nilfs), (void *)raw_sr +
979 NILFS_SR_DAT_OFFSET(isz), 1);
980 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
981 NILFS_SR_CPFILE_OFFSET(isz), 1);
982 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
983 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700984}
985
986static void nilfs_redirty_inodes(struct list_head *head)
987{
988 struct nilfs_inode_info *ii;
989
990 list_for_each_entry(ii, head, i_dirty) {
991 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
992 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
993 }
994}
995
996static void nilfs_drop_collected_inodes(struct list_head *head)
997{
998 struct nilfs_inode_info *ii;
999
1000 list_for_each_entry(ii, head, i_dirty) {
1001 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
1002 continue;
1003
1004 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
1005 set_bit(NILFS_I_UPDATED, &ii->i_state);
1006 }
1007}
1008
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001009static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
1010 struct inode *inode,
1011 struct list_head *listp,
1012 int (*collect)(struct nilfs_sc_info *,
1013 struct buffer_head *,
1014 struct inode *))
1015{
1016 struct buffer_head *bh, *n;
1017 int err = 0;
1018
1019 if (collect) {
1020 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1021 list_del_init(&bh->b_assoc_buffers);
1022 err = collect(sci, bh, inode);
1023 brelse(bh);
1024 if (unlikely(err))
1025 goto dispose_buffers;
1026 }
1027 return 0;
1028 }
1029
1030 dispose_buffers:
1031 while (!list_empty(listp)) {
1032 bh = list_entry(listp->next, struct buffer_head,
1033 b_assoc_buffers);
1034 list_del_init(&bh->b_assoc_buffers);
1035 brelse(bh);
1036 }
1037 return err;
1038}
1039
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001040static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1041{
1042 /* Remaining number of blocks within segment buffer */
1043 return sci->sc_segbuf_nblocks -
1044 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1045}
1046
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001047static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1048 struct inode *inode,
1049 struct nilfs_sc_operations *sc_ops)
1050{
1051 LIST_HEAD(data_buffers);
1052 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001053 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001054
1055 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001056 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1057
1058 n = nilfs_lookup_dirty_data_buffers(
1059 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1060 if (n > rest) {
1061 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001062 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001063 sc_ops->collect_data);
1064 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001065 goto break_or_fail;
1066 }
1067 }
1068 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1069
1070 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1071 err = nilfs_segctor_apply_buffers(
1072 sci, inode, &data_buffers, sc_ops->collect_data);
1073 if (unlikely(err)) {
1074 /* dispose node list */
1075 nilfs_segctor_apply_buffers(
1076 sci, inode, &node_buffers, NULL);
1077 goto break_or_fail;
1078 }
1079 sci->sc_stage.flags |= NILFS_CF_NODE;
1080 }
1081 /* Collect node */
1082 err = nilfs_segctor_apply_buffers(
1083 sci, inode, &node_buffers, sc_ops->collect_node);
1084 if (unlikely(err))
1085 goto break_or_fail;
1086
1087 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1088 err = nilfs_segctor_apply_buffers(
1089 sci, inode, &node_buffers, sc_ops->collect_bmap);
1090 if (unlikely(err))
1091 goto break_or_fail;
1092
1093 nilfs_segctor_end_finfo(sci, inode);
1094 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1095
1096 break_or_fail:
1097 return err;
1098}
1099
1100static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1101 struct inode *inode)
1102{
1103 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001104 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1105 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001106
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001107 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1108 sci->sc_dsync_start,
1109 sci->sc_dsync_end);
1110
1111 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1112 nilfs_collect_file_data);
1113 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001114 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001115 BUG_ON(n > rest);
1116 /* always receive -E2BIG or true error if n > rest */
1117 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001118 return err;
1119}
1120
1121static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1122{
1123 struct nilfs_sb_info *sbi = sci->sc_sbi;
1124 struct the_nilfs *nilfs = sbi->s_nilfs;
1125 struct list_head *head;
1126 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001127 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001128 int err = 0;
1129
1130 switch (sci->sc_stage.scnt) {
1131 case NILFS_ST_INIT:
1132 /* Pre-processes */
1133 sci->sc_stage.flags = 0;
1134
1135 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1136 sci->sc_nblk_inc = 0;
1137 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1138 if (mode == SC_LSEG_DSYNC) {
1139 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1140 goto dsync_mode;
1141 }
1142 }
1143
1144 sci->sc_stage.dirty_file_ptr = NULL;
1145 sci->sc_stage.gc_inode_ptr = NULL;
1146 if (mode == SC_FLUSH_DAT) {
1147 sci->sc_stage.scnt = NILFS_ST_DAT;
1148 goto dat_stage;
1149 }
1150 sci->sc_stage.scnt++; /* Fall through */
1151 case NILFS_ST_GC:
1152 if (nilfs_doing_gc()) {
1153 head = &sci->sc_gc_inodes;
1154 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1155 head, i_dirty);
1156 list_for_each_entry_continue(ii, head, i_dirty) {
1157 err = nilfs_segctor_scan_file(
1158 sci, &ii->vfs_inode,
1159 &nilfs_sc_file_ops);
1160 if (unlikely(err)) {
1161 sci->sc_stage.gc_inode_ptr = list_entry(
1162 ii->i_dirty.prev,
1163 struct nilfs_inode_info,
1164 i_dirty);
1165 goto break_or_fail;
1166 }
1167 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1168 }
1169 sci->sc_stage.gc_inode_ptr = NULL;
1170 }
1171 sci->sc_stage.scnt++; /* Fall through */
1172 case NILFS_ST_FILE:
1173 head = &sci->sc_dirty_files;
1174 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1175 i_dirty);
1176 list_for_each_entry_continue(ii, head, i_dirty) {
1177 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1178
1179 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1180 &nilfs_sc_file_ops);
1181 if (unlikely(err)) {
1182 sci->sc_stage.dirty_file_ptr =
1183 list_entry(ii->i_dirty.prev,
1184 struct nilfs_inode_info,
1185 i_dirty);
1186 goto break_or_fail;
1187 }
1188 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1189 /* XXX: required ? */
1190 }
1191 sci->sc_stage.dirty_file_ptr = NULL;
1192 if (mode == SC_FLUSH_FILE) {
1193 sci->sc_stage.scnt = NILFS_ST_DONE;
1194 return 0;
1195 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001196 sci->sc_stage.scnt++;
1197 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1198 /* Fall through */
1199 case NILFS_ST_IFILE:
1200 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
1201 &nilfs_sc_file_ops);
1202 if (unlikely(err))
1203 break;
1204 sci->sc_stage.scnt++;
1205 /* Creating a checkpoint */
1206 err = nilfs_segctor_create_checkpoint(sci);
1207 if (unlikely(err))
1208 break;
1209 /* Fall through */
1210 case NILFS_ST_CPFILE:
1211 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1212 &nilfs_sc_file_ops);
1213 if (unlikely(err))
1214 break;
1215 sci->sc_stage.scnt++; /* Fall through */
1216 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001217 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1218 sci->sc_nfreesegs, &ndone);
1219 if (unlikely(err)) {
1220 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1221 sci->sc_freesegs, ndone,
1222 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001223 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001224 }
1225 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1226
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001227 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1228 &nilfs_sc_file_ops);
1229 if (unlikely(err))
1230 break;
1231 sci->sc_stage.scnt++; /* Fall through */
1232 case NILFS_ST_DAT:
1233 dat_stage:
1234 err = nilfs_segctor_scan_file(sci, nilfs_dat_inode(nilfs),
1235 &nilfs_sc_dat_ops);
1236 if (unlikely(err))
1237 break;
1238 if (mode == SC_FLUSH_DAT) {
1239 sci->sc_stage.scnt = NILFS_ST_DONE;
1240 return 0;
1241 }
1242 sci->sc_stage.scnt++; /* Fall through */
1243 case NILFS_ST_SR:
1244 if (mode == SC_LSEG_SR) {
1245 /* Appending a super root */
1246 err = nilfs_segctor_add_super_root(sci);
1247 if (unlikely(err))
1248 break;
1249 }
1250 /* End of a logical segment */
1251 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1252 sci->sc_stage.scnt = NILFS_ST_DONE;
1253 return 0;
1254 case NILFS_ST_DSYNC:
1255 dsync_mode:
1256 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001257 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001258 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1259 break;
1260
1261 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1262 if (unlikely(err))
1263 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001264 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1265 sci->sc_stage.scnt = NILFS_ST_DONE;
1266 return 0;
1267 case NILFS_ST_DONE:
1268 return 0;
1269 default:
1270 BUG();
1271 }
1272
1273 break_or_fail:
1274 return err;
1275}
1276
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001277/**
1278 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1279 * @sci: nilfs_sc_info
1280 * @nilfs: nilfs object
1281 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001282static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1283 struct the_nilfs *nilfs)
1284{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001285 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001286 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001287 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001288
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001289 segbuf = nilfs_segbuf_new(sci->sc_super);
1290 if (unlikely(!segbuf))
1291 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001292
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001293 if (list_empty(&sci->sc_write_logs)) {
1294 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1295 nilfs->ns_pseg_offset, nilfs);
1296 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1297 nilfs_shift_to_next_segment(nilfs);
1298 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1299 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001300
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001301 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001302 nextnum = nilfs->ns_nextnum;
1303
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001304 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1305 /* Start from the head of a new full segment */
1306 alloc++;
1307 } else {
1308 /* Continue logs */
1309 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1310 nilfs_segbuf_map_cont(segbuf, prev);
1311 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1312 nextnum = prev->sb_nextnum;
1313
1314 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1315 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1316 segbuf->sb_sum.seg_seq++;
1317 alloc++;
1318 }
1319 }
1320
1321 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1322 if (err)
1323 goto failed;
1324
1325 if (alloc) {
1326 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1327 if (err)
1328 goto failed;
1329 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001330 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1331
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001332 BUG_ON(!list_empty(&sci->sc_segbufs));
1333 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1334 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001335 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001336
1337 failed:
1338 nilfs_segbuf_free(segbuf);
1339 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001340}
1341
1342static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1343 struct the_nilfs *nilfs, int nadd)
1344{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001345 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001346 struct inode *sufile = nilfs->ns_sufile;
1347 __u64 nextnextnum;
1348 LIST_HEAD(list);
1349 int err, ret, i;
1350
1351 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1352 /*
1353 * Since the segment specified with nextnum might be allocated during
1354 * the previous construction, the buffer including its segusage may
1355 * not be dirty. The following call ensures that the buffer is dirty
1356 * and will pin the buffer on memory until the sufile is written.
1357 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001358 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001359 if (unlikely(err))
1360 return err;
1361
1362 for (i = 0; i < nadd; i++) {
1363 /* extend segment info */
1364 err = -ENOMEM;
1365 segbuf = nilfs_segbuf_new(sci->sc_super);
1366 if (unlikely(!segbuf))
1367 goto failed;
1368
1369 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001370 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001371 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1372
1373 /* allocate the next next full segment */
1374 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1375 if (unlikely(err))
1376 goto failed_segbuf;
1377
1378 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1379 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1380
1381 list_add_tail(&segbuf->sb_list, &list);
1382 prev = segbuf;
1383 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001384 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001385 return 0;
1386
1387 failed_segbuf:
1388 nilfs_segbuf_free(segbuf);
1389 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001390 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001391 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001392 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001393 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001394 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001395 return err;
1396}
1397
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001398static void nilfs_free_incomplete_logs(struct list_head *logs,
1399 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001400{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001401 struct nilfs_segment_buffer *segbuf, *prev;
1402 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001403 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001404
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001405 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001406 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001407 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001408 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001409 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001410 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001411 /* Case 1: The first segment failed */
1412 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1413 /* Case 1a: Partial segment appended into an existing
1414 segment */
1415 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1416 segbuf->sb_fseg_end);
1417 else /* Case 1b: New full segment */
1418 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001419 }
1420
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001421 prev = segbuf;
1422 list_for_each_entry_continue(segbuf, logs, sb_list) {
1423 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1424 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1425 WARN_ON(ret); /* never fails */
1426 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001427 if (atomic_read(&segbuf->sb_err) &&
1428 segbuf->sb_segnum != nilfs->ns_nextnum)
1429 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001430 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1431 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001432 }
1433}
1434
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001435static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1436 struct inode *sufile)
1437{
1438 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001439 unsigned long live_blocks;
1440 int ret;
1441
1442 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001443 live_blocks = segbuf->sb_sum.nblocks +
1444 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001445 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1446 live_blocks,
1447 sci->sc_seg_ctime);
1448 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001449 }
1450}
1451
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001452static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001453{
1454 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001455 int ret;
1456
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001457 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001458 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1459 segbuf->sb_pseg_start -
1460 segbuf->sb_fseg_start, 0);
1461 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001462
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001463 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001464 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1465 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001466 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001467 }
1468}
1469
1470static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1471 struct nilfs_segment_buffer *last,
1472 struct inode *sufile)
1473{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001474 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001475 int ret;
1476
Ryusuke Konishie29df392009-11-29 16:51:16 +09001477 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001478 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1479 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001480 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001481 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001482 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001483}
1484
1485
1486static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1487 struct the_nilfs *nilfs, int mode)
1488{
1489 struct nilfs_cstage prev_stage = sci->sc_stage;
1490 int err, nadd = 1;
1491
1492 /* Collection retry loop */
1493 for (;;) {
1494 sci->sc_super_root = NULL;
1495 sci->sc_nblk_this_inc = 0;
1496 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1497
1498 err = nilfs_segctor_reset_segment_buffer(sci);
1499 if (unlikely(err))
1500 goto failed;
1501
1502 err = nilfs_segctor_collect_blocks(sci, mode);
1503 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1504 if (!err)
1505 break;
1506
1507 if (unlikely(err != -E2BIG))
1508 goto failed;
1509
1510 /* The current segment is filled up */
1511 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1512 break;
1513
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001514 nilfs_clear_logs(&sci->sc_segbufs);
1515
1516 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1517 if (unlikely(err))
1518 return err;
1519
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001520 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1521 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1522 sci->sc_freesegs,
1523 sci->sc_nfreesegs,
1524 NULL);
1525 WARN_ON(err); /* do not happen */
1526 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001527 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1528 sci->sc_stage = prev_stage;
1529 }
1530 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1531 return 0;
1532
1533 failed:
1534 return err;
1535}
1536
1537static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1538 struct buffer_head *new_bh)
1539{
1540 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1541
1542 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1543 /* The caller must release old_bh */
1544}
1545
1546static int
1547nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1548 struct nilfs_segment_buffer *segbuf,
1549 int mode)
1550{
1551 struct inode *inode = NULL;
1552 sector_t blocknr;
1553 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1554 unsigned long nblocks = 0, ndatablk = 0;
1555 struct nilfs_sc_operations *sc_op = NULL;
1556 struct nilfs_segsum_pointer ssp;
1557 struct nilfs_finfo *finfo = NULL;
1558 union nilfs_binfo binfo;
1559 struct buffer_head *bh, *bh_org;
1560 ino_t ino = 0;
1561 int err = 0;
1562
1563 if (!nfinfo)
1564 goto out;
1565
1566 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1567 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1568 ssp.offset = sizeof(struct nilfs_segment_summary);
1569
1570 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1571 if (bh == sci->sc_super_root)
1572 break;
1573 if (!finfo) {
1574 finfo = nilfs_segctor_map_segsum_entry(
1575 sci, &ssp, sizeof(*finfo));
1576 ino = le64_to_cpu(finfo->fi_ino);
1577 nblocks = le32_to_cpu(finfo->fi_nblocks);
1578 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1579
1580 if (buffer_nilfs_node(bh))
1581 inode = NILFS_BTNC_I(bh->b_page->mapping);
1582 else
1583 inode = NILFS_AS_I(bh->b_page->mapping);
1584
1585 if (mode == SC_LSEG_DSYNC)
1586 sc_op = &nilfs_sc_dsync_ops;
1587 else if (ino == NILFS_DAT_INO)
1588 sc_op = &nilfs_sc_dat_ops;
1589 else /* file blocks */
1590 sc_op = &nilfs_sc_file_ops;
1591 }
1592 bh_org = bh;
1593 get_bh(bh_org);
1594 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1595 &binfo);
1596 if (bh != bh_org)
1597 nilfs_list_replace_buffer(bh_org, bh);
1598 brelse(bh_org);
1599 if (unlikely(err))
1600 goto failed_bmap;
1601
1602 if (ndatablk > 0)
1603 sc_op->write_data_binfo(sci, &ssp, &binfo);
1604 else
1605 sc_op->write_node_binfo(sci, &ssp, &binfo);
1606
1607 blocknr++;
1608 if (--nblocks == 0) {
1609 finfo = NULL;
1610 if (--nfinfo == 0)
1611 break;
1612 } else if (ndatablk > 0)
1613 ndatablk--;
1614 }
1615 out:
1616 return 0;
1617
1618 failed_bmap:
1619 err = nilfs_handle_bmap_error(err, __func__, inode, sci->sc_super);
1620 return err;
1621}
1622
1623static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1624{
1625 struct nilfs_segment_buffer *segbuf;
1626 int err;
1627
1628 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1629 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1630 if (unlikely(err))
1631 return err;
1632 nilfs_segbuf_fill_in_segsum(segbuf);
1633 }
1634 return 0;
1635}
1636
1637static int
1638nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1639{
1640 struct page *clone_page;
1641 struct buffer_head *bh, *head, *bh2;
1642 void *kaddr;
1643
1644 bh = head = page_buffers(page);
1645
1646 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1647 if (unlikely(!clone_page))
1648 return -ENOMEM;
1649
1650 bh2 = page_buffers(clone_page);
1651 kaddr = kmap_atomic(page, KM_USER0);
1652 do {
1653 if (list_empty(&bh->b_assoc_buffers))
1654 continue;
1655 get_bh(bh2);
1656 page_cache_get(clone_page); /* for each bh */
1657 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1658 bh2->b_blocknr = bh->b_blocknr;
1659 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1660 list_add_tail(&bh->b_assoc_buffers, out);
1661 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1662 kunmap_atomic(kaddr, KM_USER0);
1663
1664 if (!TestSetPageWriteback(clone_page))
1665 inc_zone_page_state(clone_page, NR_WRITEBACK);
1666 unlock_page(clone_page);
1667
1668 return 0;
1669}
1670
1671static int nilfs_test_page_to_be_frozen(struct page *page)
1672{
1673 struct address_space *mapping = page->mapping;
1674
1675 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1676 return 0;
1677
1678 if (page_mapped(page)) {
1679 ClearPageChecked(page);
1680 return 1;
1681 }
1682 return PageChecked(page);
1683}
1684
1685static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1686{
1687 if (!page || PageWriteback(page))
1688 /* For split b-tree node pages, this function may be called
1689 twice. We ignore the 2nd or later calls by this check. */
1690 return 0;
1691
1692 lock_page(page);
1693 clear_page_dirty_for_io(page);
1694 set_page_writeback(page);
1695 unlock_page(page);
1696
1697 if (nilfs_test_page_to_be_frozen(page)) {
1698 int err = nilfs_copy_replace_page_buffers(page, out);
1699 if (unlikely(err))
1700 return err;
1701 }
1702 return 0;
1703}
1704
1705static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1706 struct page **failed_page)
1707{
1708 struct nilfs_segment_buffer *segbuf;
1709 struct page *bd_page = NULL, *fs_page = NULL;
1710 struct list_head *list = &sci->sc_copied_buffers;
1711 int err;
1712
1713 *failed_page = NULL;
1714 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1715 struct buffer_head *bh;
1716
1717 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1718 b_assoc_buffers) {
1719 if (bh->b_page != bd_page) {
1720 if (bd_page) {
1721 lock_page(bd_page);
1722 clear_page_dirty_for_io(bd_page);
1723 set_page_writeback(bd_page);
1724 unlock_page(bd_page);
1725 }
1726 bd_page = bh->b_page;
1727 }
1728 }
1729
1730 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1731 b_assoc_buffers) {
1732 if (bh == sci->sc_super_root) {
1733 if (bh->b_page != bd_page) {
1734 lock_page(bd_page);
1735 clear_page_dirty_for_io(bd_page);
1736 set_page_writeback(bd_page);
1737 unlock_page(bd_page);
1738 bd_page = bh->b_page;
1739 }
1740 break;
1741 }
1742 if (bh->b_page != fs_page) {
1743 err = nilfs_begin_page_io(fs_page, list);
1744 if (unlikely(err)) {
1745 *failed_page = fs_page;
1746 goto out;
1747 }
1748 fs_page = bh->b_page;
1749 }
1750 }
1751 }
1752 if (bd_page) {
1753 lock_page(bd_page);
1754 clear_page_dirty_for_io(bd_page);
1755 set_page_writeback(bd_page);
1756 unlock_page(bd_page);
1757 }
1758 err = nilfs_begin_page_io(fs_page, list);
1759 if (unlikely(err))
1760 *failed_page = fs_page;
1761 out:
1762 return err;
1763}
1764
1765static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001766 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001767{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001768 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001769
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001770 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001771 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1772 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001773}
1774
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001775static void __nilfs_end_page_io(struct page *page, int err)
1776{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001777 if (!err) {
1778 if (!nilfs_page_buffers_clean(page))
1779 __set_page_dirty_nobuffers(page);
1780 ClearPageError(page);
1781 } else {
1782 __set_page_dirty_nobuffers(page);
1783 SetPageError(page);
1784 }
1785
1786 if (buffer_nilfs_allocated(page_buffers(page))) {
1787 if (TestClearPageWriteback(page))
1788 dec_zone_page_state(page, NR_WRITEBACK);
1789 } else
1790 end_page_writeback(page);
1791}
1792
1793static void nilfs_end_page_io(struct page *page, int err)
1794{
1795 if (!page)
1796 return;
1797
Ryusuke Konishia9777842009-07-28 17:55:29 +09001798 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001799 /*
1800 * For b-tree node pages, this function may be called twice
1801 * or more because they might be split in a segment.
1802 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001803 if (PageDirty(page)) {
1804 /*
1805 * For pages holding split b-tree node buffers, dirty
1806 * flag on the buffers may be cleared discretely.
1807 * In that case, the page is once redirtied for
1808 * remaining buffers, and it must be cancelled if
1809 * all the buffers get cleaned later.
1810 */
1811 lock_page(page);
1812 if (nilfs_page_buffers_clean(page))
1813 __nilfs_clear_page_dirty(page);
1814 unlock_page(page);
1815 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001816 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001817 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001818
1819 __nilfs_end_page_io(page, err);
1820}
1821
1822static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1823{
1824 struct buffer_head *bh, *head;
1825 struct page *page;
1826
1827 while (!list_empty(list)) {
1828 bh = list_entry(list->next, struct buffer_head,
1829 b_assoc_buffers);
1830 page = bh->b_page;
1831 page_cache_get(page);
1832 head = bh = page_buffers(page);
1833 do {
1834 if (!list_empty(&bh->b_assoc_buffers)) {
1835 list_del_init(&bh->b_assoc_buffers);
1836 if (!err) {
1837 set_buffer_uptodate(bh);
1838 clear_buffer_dirty(bh);
1839 clear_buffer_nilfs_volatile(bh);
1840 }
1841 brelse(bh); /* for b_assoc_buffers */
1842 }
1843 } while ((bh = bh->b_this_page) != head);
1844
1845 __nilfs_end_page_io(page, err);
1846 page_cache_release(page);
1847 }
1848}
1849
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001850static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
1851 struct buffer_head *bh_sr, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001852{
1853 struct nilfs_segment_buffer *segbuf;
1854 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001855 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001856
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001857 if (list_empty(logs))
1858 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001859
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001860 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001861 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1862 b_assoc_buffers) {
1863 if (bh->b_page != bd_page) {
1864 if (bd_page)
1865 end_page_writeback(bd_page);
1866 bd_page = bh->b_page;
1867 }
1868 }
1869
1870 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1871 b_assoc_buffers) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001872 if (bh == bh_sr) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001873 if (bh->b_page != bd_page) {
1874 end_page_writeback(bd_page);
1875 bd_page = bh->b_page;
1876 }
1877 break;
1878 }
1879 if (bh->b_page != fs_page) {
1880 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001881 if (fs_page && fs_page == failed_page)
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001882 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001883 fs_page = bh->b_page;
1884 }
1885 }
1886 }
1887 if (bd_page)
1888 end_page_writeback(bd_page);
1889
1890 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001891}
1892
1893static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1894 struct the_nilfs *nilfs, int err)
1895{
1896 LIST_HEAD(logs);
1897 int ret;
1898
1899 list_splice_tail_init(&sci->sc_write_logs, &logs);
1900 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi110d7352010-03-22 21:36:06 +09001901 nilfs_abort_logs(&logs, NULL, sci->sc_super_root, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001902
1903 list_splice_tail_init(&sci->sc_segbufs, &logs);
1904 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1905 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001906 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001907
1908 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1909 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1910 sci->sc_freesegs,
1911 sci->sc_nfreesegs,
1912 NULL);
1913 WARN_ON(ret); /* do not happen */
1914 }
1915
1916 nilfs_destroy_logs(&logs);
1917 sci->sc_super_root = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001918}
1919
1920static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1921 struct nilfs_segment_buffer *segbuf)
1922{
1923 nilfs->ns_segnum = segbuf->sb_segnum;
1924 nilfs->ns_nextnum = segbuf->sb_nextnum;
1925 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1926 + segbuf->sb_sum.nblocks;
1927 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1928 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1929}
1930
1931static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1932{
1933 struct nilfs_segment_buffer *segbuf;
1934 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09001935 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001936 int update_sr = (sci->sc_super_root != NULL);
1937
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001938 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001939 struct buffer_head *bh;
1940
1941 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1942 b_assoc_buffers) {
1943 set_buffer_uptodate(bh);
1944 clear_buffer_dirty(bh);
1945 if (bh->b_page != bd_page) {
1946 if (bd_page)
1947 end_page_writeback(bd_page);
1948 bd_page = bh->b_page;
1949 }
1950 }
1951 /*
1952 * We assume that the buffers which belong to the same page
1953 * continue over the buffer list.
1954 * Under this assumption, the last BHs of pages is
1955 * identifiable by the discontinuity of bh->b_page
1956 * (page != fs_page).
1957 *
1958 * For B-tree node blocks, however, this assumption is not
1959 * guaranteed. The cleanup code of B-tree node pages needs
1960 * special care.
1961 */
1962 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1963 b_assoc_buffers) {
1964 set_buffer_uptodate(bh);
1965 clear_buffer_dirty(bh);
1966 clear_buffer_nilfs_volatile(bh);
1967 if (bh == sci->sc_super_root) {
1968 if (bh->b_page != bd_page) {
1969 end_page_writeback(bd_page);
1970 bd_page = bh->b_page;
1971 }
1972 break;
1973 }
1974 if (bh->b_page != fs_page) {
1975 nilfs_end_page_io(fs_page, 0);
1976 fs_page = bh->b_page;
1977 }
1978 }
1979
1980 if (!NILFS_SEG_SIMPLEX(&segbuf->sb_sum)) {
1981 if (NILFS_SEG_LOGBGN(&segbuf->sb_sum)) {
1982 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1983 sci->sc_lseg_stime = jiffies;
1984 }
1985 if (NILFS_SEG_LOGEND(&segbuf->sb_sum))
1986 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1987 }
1988 }
1989 /*
1990 * Since pages may continue over multiple segment buffers,
1991 * end of the last page must be checked outside of the loop.
1992 */
1993 if (bd_page)
1994 end_page_writeback(bd_page);
1995
1996 nilfs_end_page_io(fs_page, 0);
1997
1998 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
1999
2000 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
2001
2002 if (nilfs_doing_gc()) {
2003 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
2004 if (update_sr)
2005 nilfs_commit_gcdat_inode(nilfs);
Ryusuke Konishi1088dcf2009-04-06 19:01:51 -07002006 } else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002007 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002008
2009 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
2010
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002011 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002012 nilfs_set_next_segment(nilfs, segbuf);
2013
2014 if (update_sr) {
2015 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07002016 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002017 set_nilfs_sb_dirty(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002018
Ryusuke Konishic96fa462009-04-06 19:01:57 -07002019 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002020 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2021 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002022 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002023 } else
2024 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2025}
2026
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002027static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
2028{
2029 int ret;
2030
2031 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
2032 if (!ret) {
2033 nilfs_segctor_complete_write(sci);
2034 nilfs_destroy_logs(&sci->sc_write_logs);
2035 }
2036 return ret;
2037}
2038
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002039static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
2040 struct nilfs_sb_info *sbi)
2041{
2042 struct nilfs_inode_info *ii, *n;
2043 __u64 cno = sbi->s_nilfs->ns_cno;
2044
2045 spin_lock(&sbi->s_inode_lock);
2046 retry:
2047 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
2048 if (!ii->i_bh) {
2049 struct buffer_head *ibh;
2050 int err;
2051
2052 spin_unlock(&sbi->s_inode_lock);
2053 err = nilfs_ifile_get_inode_block(
2054 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
2055 if (unlikely(err)) {
2056 nilfs_warning(sbi->s_super, __func__,
2057 "failed to get inode block.\n");
2058 return err;
2059 }
2060 nilfs_mdt_mark_buffer_dirty(ibh);
2061 nilfs_mdt_mark_dirty(sbi->s_ifile);
2062 spin_lock(&sbi->s_inode_lock);
2063 if (likely(!ii->i_bh))
2064 ii->i_bh = ibh;
2065 else
2066 brelse(ibh);
2067 goto retry;
2068 }
2069 ii->i_cno = cno;
2070
2071 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2072 set_bit(NILFS_I_BUSY, &ii->i_state);
2073 list_del(&ii->i_dirty);
2074 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2075 }
2076 spin_unlock(&sbi->s_inode_lock);
2077
2078 NILFS_I(sbi->s_ifile)->i_cno = cno;
2079
2080 return 0;
2081}
2082
2083static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2084 struct nilfs_sb_info *sbi)
2085{
2086 struct nilfs_transaction_info *ti = current->journal_info;
2087 struct nilfs_inode_info *ii, *n;
2088 __u64 cno = sbi->s_nilfs->ns_cno;
2089
2090 spin_lock(&sbi->s_inode_lock);
2091 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2092 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
2093 test_bit(NILFS_I_DIRTY, &ii->i_state)) {
2094 /* The current checkpoint number (=nilfs->ns_cno) is
2095 changed between check-in and check-out only if the
2096 super root is written out. So, we can update i_cno
2097 for the inodes that remain in the dirty list. */
2098 ii->i_cno = cno;
2099 continue;
2100 }
2101 clear_bit(NILFS_I_BUSY, &ii->i_state);
2102 brelse(ii->i_bh);
2103 ii->i_bh = NULL;
2104 list_del(&ii->i_dirty);
2105 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2106 }
2107 spin_unlock(&sbi->s_inode_lock);
2108}
2109
2110/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002111 * Main procedure of segment constructor
2112 */
2113static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2114{
2115 struct nilfs_sb_info *sbi = sci->sc_sbi;
2116 struct the_nilfs *nilfs = sbi->s_nilfs;
2117 struct page *failed_page;
2118 int err, has_sr = 0;
2119
2120 sci->sc_stage.scnt = NILFS_ST_INIT;
2121
2122 err = nilfs_segctor_check_in_files(sci, sbi);
2123 if (unlikely(err))
2124 goto out;
2125
2126 if (nilfs_test_metadata_dirty(sbi))
2127 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2128
2129 if (nilfs_segctor_clean(sci))
2130 goto out;
2131
2132 do {
2133 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2134
2135 err = nilfs_segctor_begin_construction(sci, nilfs);
2136 if (unlikely(err))
2137 goto out;
2138
2139 /* Update time stamp */
2140 sci->sc_seg_ctime = get_seconds();
2141
2142 err = nilfs_segctor_collect(sci, nilfs, mode);
2143 if (unlikely(err))
2144 goto failed;
2145
2146 has_sr = (sci->sc_super_root != NULL);
2147
2148 /* Avoid empty segment */
2149 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
2150 NILFS_SEG_EMPTY(&sci->sc_curseg->sb_sum)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002151 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002152 goto out;
2153 }
2154
2155 err = nilfs_segctor_assign(sci, mode);
2156 if (unlikely(err))
2157 goto failed;
2158
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002159 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2160 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2161
2162 if (has_sr) {
2163 err = nilfs_segctor_fill_in_checkpoint(sci);
2164 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002165 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002166
2167 nilfs_segctor_fill_in_super_root(sci, nilfs);
2168 }
2169 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2170
2171 /* Write partial segments */
2172 err = nilfs_segctor_prepare_write(sci, &failed_page);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002173 if (err) {
2174 nilfs_abort_logs(&sci->sc_segbufs, failed_page,
2175 sci->sc_super_root, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002176 goto failed_to_write;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002177 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002178 nilfs_segctor_fill_in_checksums(sci, nilfs->ns_crc_seed);
2179
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002180 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002181 if (unlikely(err))
2182 goto failed_to_write;
2183
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002184 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2185 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2186 /*
2187 * At this point, we avoid double buffering
2188 * for blocksize < pagesize because page dirty
2189 * flag is turned off during write and dirty
2190 * buffers are not properly collected for
2191 * pages crossing over segments.
2192 */
2193 err = nilfs_segctor_wait(sci);
2194 if (err)
2195 goto failed_to_write;
2196 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002197 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2198
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002199 sci->sc_super_root = NULL;
2200
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002201 out:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002202 nilfs_segctor_check_out_files(sci, sbi);
2203 return err;
2204
2205 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002206 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2207 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002208
2209 failed:
2210 if (nilfs_doing_gc())
2211 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002212 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002213 goto out;
2214}
2215
2216/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002217 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002218 * @sci: nilfs_sc_info
2219 *
2220 * If the timer has already been set, it ignores the new request.
2221 * This function MUST be called within a section locking the segment
2222 * semaphore.
2223 */
2224static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2225{
2226 spin_lock(&sci->sc_state_lock);
2227 if (sci->sc_timer && !(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2228 sci->sc_timer->expires = jiffies + sci->sc_interval;
2229 add_timer(sci->sc_timer);
2230 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2231 }
2232 spin_unlock(&sci->sc_state_lock);
2233}
2234
2235static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2236{
2237 spin_lock(&sci->sc_state_lock);
2238 if (!(sci->sc_flush_request & (1 << bn))) {
2239 unsigned long prev_req = sci->sc_flush_request;
2240
2241 sci->sc_flush_request |= (1 << bn);
2242 if (!prev_req)
2243 wake_up(&sci->sc_wait_daemon);
2244 }
2245 spin_unlock(&sci->sc_state_lock);
2246}
2247
2248/**
2249 * nilfs_flush_segment - trigger a segment construction for resource control
2250 * @sb: super block
2251 * @ino: inode number of the file to be flushed out.
2252 */
2253void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2254{
2255 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2256 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2257
2258 if (!sci || nilfs_doing_construction())
2259 return;
2260 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2261 /* assign bit 0 to data files */
2262}
2263
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002264struct nilfs_segctor_wait_request {
2265 wait_queue_t wq;
2266 __u32 seq;
2267 int err;
2268 atomic_t done;
2269};
2270
2271static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2272{
2273 struct nilfs_segctor_wait_request wait_req;
2274 int err = 0;
2275
2276 spin_lock(&sci->sc_state_lock);
2277 init_wait(&wait_req.wq);
2278 wait_req.err = 0;
2279 atomic_set(&wait_req.done, 0);
2280 wait_req.seq = ++sci->sc_seq_request;
2281 spin_unlock(&sci->sc_state_lock);
2282
2283 init_waitqueue_entry(&wait_req.wq, current);
2284 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2285 set_current_state(TASK_INTERRUPTIBLE);
2286 wake_up(&sci->sc_wait_daemon);
2287
2288 for (;;) {
2289 if (atomic_read(&wait_req.done)) {
2290 err = wait_req.err;
2291 break;
2292 }
2293 if (!signal_pending(current)) {
2294 schedule();
2295 continue;
2296 }
2297 err = -ERESTARTSYS;
2298 break;
2299 }
2300 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2301 return err;
2302}
2303
2304static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2305{
2306 struct nilfs_segctor_wait_request *wrq, *n;
2307 unsigned long flags;
2308
2309 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2310 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2311 wq.task_list) {
2312 if (!atomic_read(&wrq->done) &&
2313 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2314 wrq->err = err;
2315 atomic_set(&wrq->done, 1);
2316 }
2317 if (atomic_read(&wrq->done)) {
2318 wrq->wq.func(&wrq->wq,
2319 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2320 0, NULL);
2321 }
2322 }
2323 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2324}
2325
2326/**
2327 * nilfs_construct_segment - construct a logical segment
2328 * @sb: super block
2329 *
2330 * Return Value: On success, 0 is retured. On errors, one of the following
2331 * negative error code is returned.
2332 *
2333 * %-EROFS - Read only filesystem.
2334 *
2335 * %-EIO - I/O error
2336 *
2337 * %-ENOSPC - No space left on device (only in a panic state).
2338 *
2339 * %-ERESTARTSYS - Interrupted.
2340 *
2341 * %-ENOMEM - Insufficient memory available.
2342 */
2343int nilfs_construct_segment(struct super_block *sb)
2344{
2345 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2346 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2347 struct nilfs_transaction_info *ti;
2348 int err;
2349
2350 if (!sci)
2351 return -EROFS;
2352
2353 /* A call inside transactions causes a deadlock. */
2354 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2355
2356 err = nilfs_segctor_sync(sci);
2357 return err;
2358}
2359
2360/**
2361 * nilfs_construct_dsync_segment - construct a data-only logical segment
2362 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002363 * @inode: inode whose data blocks should be written out
2364 * @start: start byte offset
2365 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002366 *
2367 * Return Value: On success, 0 is retured. On errors, one of the following
2368 * negative error code is returned.
2369 *
2370 * %-EROFS - Read only filesystem.
2371 *
2372 * %-EIO - I/O error
2373 *
2374 * %-ENOSPC - No space left on device (only in a panic state).
2375 *
2376 * %-ERESTARTSYS - Interrupted.
2377 *
2378 * %-ENOMEM - Insufficient memory available.
2379 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002380int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2381 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002382{
2383 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2384 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2385 struct nilfs_inode_info *ii;
2386 struct nilfs_transaction_info ti;
2387 int err = 0;
2388
2389 if (!sci)
2390 return -EROFS;
2391
2392 nilfs_transaction_lock(sbi, &ti, 0);
2393
2394 ii = NILFS_I(inode);
2395 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2396 nilfs_test_opt(sbi, STRICT_ORDER) ||
2397 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2398 nilfs_discontinued(sbi->s_nilfs)) {
2399 nilfs_transaction_unlock(sbi);
2400 err = nilfs_segctor_sync(sci);
2401 return err;
2402 }
2403
2404 spin_lock(&sbi->s_inode_lock);
2405 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2406 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2407 spin_unlock(&sbi->s_inode_lock);
2408 nilfs_transaction_unlock(sbi);
2409 return 0;
2410 }
2411 spin_unlock(&sbi->s_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002412 sci->sc_dsync_inode = ii;
2413 sci->sc_dsync_start = start;
2414 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002415
2416 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2417
2418 nilfs_transaction_unlock(sbi);
2419 return err;
2420}
2421
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002422#define FLUSH_FILE_BIT (0x1) /* data file only */
2423#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2424
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002425/**
2426 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2427 * @sci: segment constructor object
2428 */
2429static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002430{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002431 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002432 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002433 spin_unlock(&sci->sc_state_lock);
2434
2435 if (sci->sc_timer)
2436 del_timer_sync(sci->sc_timer);
2437}
2438
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002439/**
2440 * nilfs_segctor_notify - notify the result of request to caller threads
2441 * @sci: segment constructor object
2442 * @mode: mode of log forming
2443 * @err: error code to be notified
2444 */
2445static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002446{
2447 /* Clear requests (even when the construction failed) */
2448 spin_lock(&sci->sc_state_lock);
2449
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002450 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002451 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002452 sci->sc_seq_done = sci->sc_seq_accepted;
2453 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002454 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002455 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002456 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002457 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002458 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002459 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002460
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002461 /* re-enable timer if checkpoint creation was not done */
2462 if (sci->sc_timer && (sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2463 time_before(jiffies, sci->sc_timer->expires))
2464 add_timer(sci->sc_timer);
2465 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002466 spin_unlock(&sci->sc_state_lock);
2467}
2468
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002469/**
2470 * nilfs_segctor_construct - form logs and write them to disk
2471 * @sci: segment constructor object
2472 * @mode: mode of log forming
2473 */
2474static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002475{
2476 struct nilfs_sb_info *sbi = sci->sc_sbi;
2477 struct the_nilfs *nilfs = sbi->s_nilfs;
2478 int err = 0;
2479
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002480 nilfs_segctor_accept(sci);
2481
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002482 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002483 mode = SC_LSEG_SR;
2484 if (!nilfs_segctor_confirm(sci))
2485 err = nilfs_segctor_do_construct(sci, mode);
2486
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002487 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002488 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002489 atomic_set(&nilfs->ns_ndirtyblks, 0);
2490 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2491 nilfs_discontinued(nilfs)) {
2492 down_write(&nilfs->ns_sem);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002493 err = nilfs_commit_super(
2494 sbi, nilfs_altsb_need_update(nilfs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002495 up_write(&nilfs->ns_sem);
2496 }
2497 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002498
2499 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002500 return err;
2501}
2502
2503static void nilfs_construction_timeout(unsigned long data)
2504{
2505 struct task_struct *p = (struct task_struct *)data;
2506 wake_up_process(p);
2507}
2508
2509static void
2510nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2511{
2512 struct nilfs_inode_info *ii, *n;
2513
2514 list_for_each_entry_safe(ii, n, head, i_dirty) {
2515 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2516 continue;
2517 hlist_del_init(&ii->vfs_inode.i_hash);
2518 list_del_init(&ii->i_dirty);
2519 nilfs_clear_gcinode(&ii->vfs_inode);
2520 }
2521}
2522
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002523int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2524 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002525{
2526 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2527 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2528 struct the_nilfs *nilfs = sbi->s_nilfs;
2529 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002530 int err;
2531
2532 if (unlikely(!sci))
2533 return -EROFS;
2534
2535 nilfs_transaction_lock(sbi, &ti, 1);
2536
2537 err = nilfs_init_gcdat_inode(nilfs);
2538 if (unlikely(err))
2539 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002540
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002541 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002542 if (unlikely(err))
2543 goto out_unlock;
2544
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002545 sci->sc_freesegs = kbufs[4];
2546 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002547 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002548
2549 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002550 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002551 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002552
2553 if (likely(!err))
2554 break;
2555
2556 nilfs_warning(sb, __func__,
2557 "segment construction failed. (err=%d)", err);
2558 set_current_state(TASK_INTERRUPTIBLE);
2559 schedule_timeout(sci->sc_interval);
2560 }
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002561 if (nilfs_test_opt(sbi, DISCARD)) {
2562 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2563 sci->sc_nfreesegs);
2564 if (ret) {
2565 printk(KERN_WARNING
2566 "NILFS warning: error %d on discard request, "
2567 "turning discards off for the device\n", ret);
2568 nilfs_clear_opt(sbi, DISCARD);
2569 }
2570 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002571
2572 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002573 sci->sc_freesegs = NULL;
2574 sci->sc_nfreesegs = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002575 nilfs_clear_gcdat_inode(nilfs);
2576 nilfs_transaction_unlock(sbi);
2577 return err;
2578}
2579
2580static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2581{
2582 struct nilfs_sb_info *sbi = sci->sc_sbi;
2583 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002584
2585 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002586 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002587
2588 /*
2589 * Unclosed segment should be retried. We do this using sc_timer.
2590 * Timeout of sc_timer will invoke complete construction which leads
2591 * to close the current logical segment.
2592 */
2593 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2594 nilfs_segctor_start_timer(sci);
2595
2596 nilfs_transaction_unlock(sbi);
2597}
2598
2599static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2600{
2601 int mode = 0;
2602 int err;
2603
2604 spin_lock(&sci->sc_state_lock);
2605 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2606 SC_FLUSH_DAT : SC_FLUSH_FILE;
2607 spin_unlock(&sci->sc_state_lock);
2608
2609 if (mode) {
2610 err = nilfs_segctor_do_construct(sci, mode);
2611
2612 spin_lock(&sci->sc_state_lock);
2613 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2614 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2615 spin_unlock(&sci->sc_state_lock);
2616 }
2617 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2618}
2619
2620static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2621{
2622 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2623 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2624 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2625 return SC_FLUSH_FILE;
2626 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2627 return SC_FLUSH_DAT;
2628 }
2629 return SC_LSEG_SR;
2630}
2631
2632/**
2633 * nilfs_segctor_thread - main loop of the segment constructor thread.
2634 * @arg: pointer to a struct nilfs_sc_info.
2635 *
2636 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2637 * to execute segment constructions.
2638 */
2639static int nilfs_segctor_thread(void *arg)
2640{
2641 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002642 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002643 struct timer_list timer;
2644 int timeout = 0;
2645
2646 init_timer(&timer);
2647 timer.data = (unsigned long)current;
2648 timer.function = nilfs_construction_timeout;
2649 sci->sc_timer = &timer;
2650
2651 /* start sync. */
2652 sci->sc_task = current;
2653 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2654 printk(KERN_INFO
2655 "segctord starting. Construction interval = %lu seconds, "
2656 "CP frequency < %lu seconds\n",
2657 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2658
2659 spin_lock(&sci->sc_state_lock);
2660 loop:
2661 for (;;) {
2662 int mode;
2663
2664 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2665 goto end_thread;
2666
2667 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2668 mode = SC_LSEG_SR;
2669 else if (!sci->sc_flush_request)
2670 break;
2671 else
2672 mode = nilfs_segctor_flush_mode(sci);
2673
2674 spin_unlock(&sci->sc_state_lock);
2675 nilfs_segctor_thread_construct(sci, mode);
2676 spin_lock(&sci->sc_state_lock);
2677 timeout = 0;
2678 }
2679
2680
2681 if (freezing(current)) {
2682 spin_unlock(&sci->sc_state_lock);
2683 refrigerator();
2684 spin_lock(&sci->sc_state_lock);
2685 } else {
2686 DEFINE_WAIT(wait);
2687 int should_sleep = 1;
2688
2689 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2690 TASK_INTERRUPTIBLE);
2691
2692 if (sci->sc_seq_request != sci->sc_seq_done)
2693 should_sleep = 0;
2694 else if (sci->sc_flush_request)
2695 should_sleep = 0;
2696 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2697 should_sleep = time_before(jiffies,
2698 sci->sc_timer->expires);
2699
2700 if (should_sleep) {
2701 spin_unlock(&sci->sc_state_lock);
2702 schedule();
2703 spin_lock(&sci->sc_state_lock);
2704 }
2705 finish_wait(&sci->sc_wait_daemon, &wait);
2706 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2707 time_after_eq(jiffies, sci->sc_timer->expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002708
2709 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002710 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002711 }
2712 goto loop;
2713
2714 end_thread:
2715 spin_unlock(&sci->sc_state_lock);
2716 del_timer_sync(sci->sc_timer);
2717 sci->sc_timer = NULL;
2718
2719 /* end sync. */
2720 sci->sc_task = NULL;
2721 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2722 return 0;
2723}
2724
2725static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2726{
2727 struct task_struct *t;
2728
2729 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2730 if (IS_ERR(t)) {
2731 int err = PTR_ERR(t);
2732
2733 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2734 err);
2735 return err;
2736 }
2737 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2738 return 0;
2739}
2740
2741static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2742{
2743 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2744
2745 while (sci->sc_task) {
2746 wake_up(&sci->sc_wait_daemon);
2747 spin_unlock(&sci->sc_state_lock);
2748 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2749 spin_lock(&sci->sc_state_lock);
2750 }
2751}
2752
Ryusuke Konishicece5522009-04-06 19:01:58 -07002753static int nilfs_segctor_init(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002754{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002755 sci->sc_seq_done = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002756
Ryusuke Konishicece5522009-04-06 19:01:58 -07002757 return nilfs_segctor_start_thread(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002758}
2759
2760/*
2761 * Setup & clean-up functions
2762 */
2763static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
2764{
2765 struct nilfs_sc_info *sci;
2766
2767 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2768 if (!sci)
2769 return NULL;
2770
2771 sci->sc_sbi = sbi;
2772 sci->sc_super = sbi->s_super;
2773
2774 init_waitqueue_head(&sci->sc_wait_request);
2775 init_waitqueue_head(&sci->sc_wait_daemon);
2776 init_waitqueue_head(&sci->sc_wait_task);
2777 spin_lock_init(&sci->sc_state_lock);
2778 INIT_LIST_HEAD(&sci->sc_dirty_files);
2779 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002780 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002781 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002782 INIT_LIST_HEAD(&sci->sc_copied_buffers);
2783
2784 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2785 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2786 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2787
2788 if (sbi->s_interval)
2789 sci->sc_interval = sbi->s_interval;
2790 if (sbi->s_watermark)
2791 sci->sc_watermark = sbi->s_watermark;
2792 return sci;
2793}
2794
2795static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2796{
2797 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2798
2799 /* The segctord thread was stopped and its timer was removed.
2800 But some tasks remain. */
2801 do {
2802 struct nilfs_sb_info *sbi = sci->sc_sbi;
2803 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002804
2805 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002806 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002807 nilfs_transaction_unlock(sbi);
2808
2809 } while (ret && retrycount-- > 0);
2810}
2811
2812/**
2813 * nilfs_segctor_destroy - destroy the segment constructor.
2814 * @sci: nilfs_sc_info
2815 *
2816 * nilfs_segctor_destroy() kills the segctord thread and frees
2817 * the nilfs_sc_info struct.
2818 * Caller must hold the segment semaphore.
2819 */
2820static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2821{
2822 struct nilfs_sb_info *sbi = sci->sc_sbi;
2823 int flag;
2824
2825 up_write(&sbi->s_nilfs->ns_segctor_sem);
2826
2827 spin_lock(&sci->sc_state_lock);
2828 nilfs_segctor_kill_thread(sci);
2829 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2830 || sci->sc_seq_request != sci->sc_seq_done);
2831 spin_unlock(&sci->sc_state_lock);
2832
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002833 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002834 nilfs_segctor_write_out(sci);
2835
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002836 WARN_ON(!list_empty(&sci->sc_copied_buffers));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002837
2838 if (!list_empty(&sci->sc_dirty_files)) {
2839 nilfs_warning(sbi->s_super, __func__,
2840 "dirty file(s) after the final construction\n");
2841 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
2842 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002843
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002844 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002845 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002846
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002847 down_write(&sbi->s_nilfs->ns_segctor_sem);
2848
2849 kfree(sci);
2850}
2851
2852/**
2853 * nilfs_attach_segment_constructor - attach a segment constructor
2854 * @sbi: nilfs_sb_info
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002855 *
2856 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
Ryusuke Konishi7a650042010-03-14 03:32:40 +09002857 * initializes it, and starts the segment constructor.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002858 *
2859 * Return Value: On success, 0 is returned. On error, one of the following
2860 * negative error code is returned.
2861 *
2862 * %-ENOMEM - Insufficient memory available.
2863 */
Ryusuke Konishicece5522009-04-06 19:01:58 -07002864int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002865{
2866 struct the_nilfs *nilfs = sbi->s_nilfs;
2867 int err;
2868
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002869 if (NILFS_SC(sbi)) {
2870 /*
2871 * This happens if the filesystem was remounted
2872 * read/write after nilfs_error degenerated it into a
2873 * read-only mount.
2874 */
2875 nilfs_detach_segment_constructor(sbi);
2876 }
2877
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002878 sbi->s_sc_info = nilfs_segctor_new(sbi);
2879 if (!sbi->s_sc_info)
2880 return -ENOMEM;
2881
2882 nilfs_attach_writer(nilfs, sbi);
Ryusuke Konishicece5522009-04-06 19:01:58 -07002883 err = nilfs_segctor_init(NILFS_SC(sbi));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002884 if (err) {
2885 nilfs_detach_writer(nilfs, sbi);
2886 kfree(sbi->s_sc_info);
2887 sbi->s_sc_info = NULL;
2888 }
2889 return err;
2890}
2891
2892/**
2893 * nilfs_detach_segment_constructor - destroy the segment constructor
2894 * @sbi: nilfs_sb_info
2895 *
2896 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2897 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2898 */
2899void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2900{
2901 struct the_nilfs *nilfs = sbi->s_nilfs;
2902 LIST_HEAD(garbage_list);
2903
2904 down_write(&nilfs->ns_segctor_sem);
2905 if (NILFS_SC(sbi)) {
2906 nilfs_segctor_destroy(NILFS_SC(sbi));
2907 sbi->s_sc_info = NULL;
2908 }
2909
2910 /* Force to free the list of dirty files */
2911 spin_lock(&sbi->s_inode_lock);
2912 if (!list_empty(&sbi->s_dirty_files)) {
2913 list_splice_init(&sbi->s_dirty_files, &garbage_list);
2914 nilfs_warning(sbi->s_super, __func__,
2915 "Non empty dirty list after the last "
2916 "segment construction\n");
2917 }
2918 spin_unlock(&sbi->s_inode_lock);
2919 up_write(&nilfs->ns_segctor_sem);
2920
2921 nilfs_dispose_list(sbi, &garbage_list, 1);
2922 nilfs_detach_writer(nilfs, sbi);
2923}