blob: e3d1785faf1bd9aa5cb48d5cc9c1ece18d160bc5 [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 *);
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900107static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700108
109#define nilfs_cnt32_gt(a, b) \
110 (typecheck(__u32, a) && typecheck(__u32, b) && \
111 ((__s32)(b) - (__s32)(a) < 0))
112#define nilfs_cnt32_ge(a, b) \
113 (typecheck(__u32, a) && typecheck(__u32, b) && \
114 ((__s32)(a) - (__s32)(b) >= 0))
115#define nilfs_cnt32_lt(a, b) nilfs_cnt32_gt(b, a)
116#define nilfs_cnt32_le(a, b) nilfs_cnt32_ge(b, a)
117
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700118static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
119{
120 struct nilfs_transaction_info *cur_ti = current->journal_info;
121 void *save = NULL;
122
123 if (cur_ti) {
124 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
125 return ++cur_ti->ti_count;
126 else {
127 /*
128 * If journal_info field is occupied by other FS,
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700129 * it is saved and will be restored on
130 * nilfs_transaction_commit().
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700131 */
132 printk(KERN_WARNING
133 "NILFS warning: journal info from a different "
134 "FS\n");
135 save = current->journal_info;
136 }
137 }
138 if (!ti) {
139 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
140 if (!ti)
141 return -ENOMEM;
142 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
143 } else {
144 ti->ti_flags = 0;
145 }
146 ti->ti_count = 0;
147 ti->ti_save = save;
148 ti->ti_magic = NILFS_TI_MAGIC;
149 current->journal_info = ti;
150 return 0;
151}
152
153/**
154 * nilfs_transaction_begin - start indivisible file operations.
155 * @sb: super block
156 * @ti: nilfs_transaction_info
157 * @vacancy_check: flags for vacancy rate checks
158 *
159 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
160 * the segment semaphore, to make a segment construction and write tasks
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700161 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700162 * The region enclosed by these two functions can be nested. To avoid a
163 * deadlock, the semaphore is only acquired or released in the outermost call.
164 *
165 * This function allocates a nilfs_transaction_info struct to keep context
166 * information on it. It is initialized and hooked onto the current task in
167 * the outermost call. If a pre-allocated struct is given to @ti, it is used
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900168 * instead; otherwise a new struct is assigned from a slab.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700169 *
170 * When @vacancy_check flag is set, this function will check the amount of
171 * free space, and will wait for the GC to reclaim disk space if low capacity.
172 *
173 * Return Value: On success, 0 is returned. On error, one of the following
174 * negative error code is returned.
175 *
176 * %-ENOMEM - Insufficient memory available.
177 *
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700178 * %-ENOSPC - No space left on device
179 */
180int nilfs_transaction_begin(struct super_block *sb,
181 struct nilfs_transaction_info *ti,
182 int vacancy_check)
183{
184 struct nilfs_sb_info *sbi;
185 struct the_nilfs *nilfs;
186 int ret = nilfs_prepare_segment_lock(ti);
187
188 if (unlikely(ret < 0))
189 return ret;
190 if (ret > 0)
191 return 0;
192
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900193 vfs_check_frozen(sb, SB_FREEZE_WRITE);
194
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700195 sbi = NILFS_SB(sb);
196 nilfs = sbi->s_nilfs;
197 down_read(&nilfs->ns_segctor_sem);
198 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
199 up_read(&nilfs->ns_segctor_sem);
200 ret = -ENOSPC;
201 goto failed;
202 }
203 return 0;
204
205 failed:
206 ti = current->journal_info;
207 current->journal_info = ti->ti_save;
208 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
209 kmem_cache_free(nilfs_transaction_cachep, ti);
210 return ret;
211}
212
213/**
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700214 * nilfs_transaction_commit - commit indivisible file operations.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700215 * @sb: super block
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700216 *
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700217 * nilfs_transaction_commit() releases the read semaphore which is
218 * acquired by nilfs_transaction_begin(). This is only performed
219 * in outermost call of this function. If a commit flag is set,
220 * nilfs_transaction_commit() sets a timer to start the segment
221 * constructor. If a sync flag is set, it starts construction
222 * directly.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700223 */
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700224int nilfs_transaction_commit(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700225{
226 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900227 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700228 int err = 0;
229
230 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700231 ti->ti_flags |= NILFS_TI_COMMIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700232 if (ti->ti_count > 0) {
233 ti->ti_count--;
234 return 0;
235 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900236 if (nilfs->ns_writer) {
237 struct nilfs_sc_info *sci = nilfs->ns_writer;
238
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700239 if (ti->ti_flags & NILFS_TI_COMMIT)
240 nilfs_segctor_start_timer(sci);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900241 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700242 nilfs_segctor_do_flush(sci, 0);
243 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900244 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700245 current->journal_info = ti->ti_save;
246
247 if (ti->ti_flags & NILFS_TI_SYNC)
248 err = nilfs_construct_segment(sb);
249 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
250 kmem_cache_free(nilfs_transaction_cachep, ti);
251 return err;
252}
253
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700254void nilfs_transaction_abort(struct super_block *sb)
255{
256 struct nilfs_transaction_info *ti = current->journal_info;
257
258 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
259 if (ti->ti_count > 0) {
260 ti->ti_count--;
261 return;
262 }
263 up_read(&NILFS_SB(sb)->s_nilfs->ns_segctor_sem);
264
265 current->journal_info = ti->ti_save;
266 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
267 kmem_cache_free(nilfs_transaction_cachep, ti);
268}
269
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700270void nilfs_relax_pressure_in_lock(struct super_block *sb)
271{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900272 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
273 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700274
275 if (!sci || !sci->sc_flush_request)
276 return;
277
278 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
279 up_read(&nilfs->ns_segctor_sem);
280
281 down_write(&nilfs->ns_segctor_sem);
282 if (sci->sc_flush_request &&
283 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
284 struct nilfs_transaction_info *ti = current->journal_info;
285
286 ti->ti_flags |= NILFS_TI_WRITER;
287 nilfs_segctor_do_immediate_flush(sci);
288 ti->ti_flags &= ~NILFS_TI_WRITER;
289 }
290 downgrade_write(&nilfs->ns_segctor_sem);
291}
292
293static void nilfs_transaction_lock(struct nilfs_sb_info *sbi,
294 struct nilfs_transaction_info *ti,
295 int gcflag)
296{
297 struct nilfs_transaction_info *cur_ti = current->journal_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900298 struct the_nilfs *nilfs = sbi->s_nilfs;
299 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700300
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700301 WARN_ON(cur_ti);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700302 ti->ti_flags = NILFS_TI_WRITER;
303 ti->ti_count = 0;
304 ti->ti_save = cur_ti;
305 ti->ti_magic = NILFS_TI_MAGIC;
306 INIT_LIST_HEAD(&ti->ti_garbage);
307 current->journal_info = ti;
308
309 for (;;) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900310 down_write(&nilfs->ns_segctor_sem);
311 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700312 break;
313
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900314 nilfs_segctor_do_immediate_flush(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700315
316 up_write(&sbi->s_nilfs->ns_segctor_sem);
317 yield();
318 }
319 if (gcflag)
320 ti->ti_flags |= NILFS_TI_GC;
321}
322
323static void nilfs_transaction_unlock(struct nilfs_sb_info *sbi)
324{
325 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900326 struct the_nilfs *nilfs = sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700327
328 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
329 BUG_ON(ti->ti_count > 0);
330
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900331 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700332 current->journal_info = ti->ti_save;
333 if (!list_empty(&ti->ti_garbage))
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900334 nilfs_dispose_list(nilfs, &ti->ti_garbage, 0);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700335}
336
337static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
338 struct nilfs_segsum_pointer *ssp,
339 unsigned bytes)
340{
341 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
342 unsigned blocksize = sci->sc_super->s_blocksize;
343 void *p;
344
345 if (unlikely(ssp->offset + bytes > blocksize)) {
346 ssp->offset = 0;
347 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
348 &segbuf->sb_segsum_buffers));
349 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
350 }
351 p = ssp->bh->b_data + ssp->offset;
352 ssp->offset += bytes;
353 return p;
354}
355
356/**
357 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
358 * @sci: nilfs_sc_info
359 */
360static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
361{
362 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
363 struct buffer_head *sumbh;
364 unsigned sumbytes;
365 unsigned flags = 0;
366 int err;
367
368 if (nilfs_doing_gc())
369 flags = NILFS_SS_GC;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900370 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700371 if (unlikely(err))
372 return err;
373
374 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
375 sumbytes = segbuf->sb_sum.sumbytes;
376 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
377 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
378 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
379 return 0;
380}
381
382static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
383{
384 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
385 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
386 return -E2BIG; /* The current segment is filled up
387 (internal code) */
388 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
389 return nilfs_segctor_reset_segment_buffer(sci);
390}
391
392static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
393{
394 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
395 int err;
396
397 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
398 err = nilfs_segctor_feed_segment(sci);
399 if (err)
400 return err;
401 segbuf = sci->sc_curseg;
402 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900403 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700404 if (likely(!err))
405 segbuf->sb_sum.flags |= NILFS_SS_SR;
406 return err;
407}
408
409/*
410 * Functions for making segment summary and payloads
411 */
412static int nilfs_segctor_segsum_block_required(
413 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
414 unsigned binfo_size)
415{
416 unsigned blocksize = sci->sc_super->s_blocksize;
417 /* Size of finfo and binfo is enough small against blocksize */
418
419 return ssp->offset + binfo_size +
420 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
421 blocksize;
422}
423
424static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
425 struct inode *inode)
426{
427 sci->sc_curseg->sb_sum.nfinfo++;
428 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
429 nilfs_segctor_map_segsum_entry(
430 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700431
Ryusuke Konishi72746ac2011-02-28 13:41:11 +0900432 if (NILFS_I(inode)->i_root &&
433 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700434 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700435 /* skip finfo */
436}
437
438static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
439 struct inode *inode)
440{
441 struct nilfs_finfo *finfo;
442 struct nilfs_inode_info *ii;
443 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900444 __u64 cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700445
446 if (sci->sc_blk_cnt == 0)
447 return;
448
449 ii = NILFS_I(inode);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900450
451 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
452 cno = ii->i_cno;
453 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
454 cno = 0;
455 else
456 cno = sci->sc_cno;
457
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700458 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
459 sizeof(*finfo));
460 finfo->fi_ino = cpu_to_le64(inode->i_ino);
461 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
462 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900463 finfo->fi_cno = cpu_to_le64(cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700464
465 segbuf = sci->sc_curseg;
466 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
467 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
468 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
469 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
470}
471
472static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
473 struct buffer_head *bh,
474 struct inode *inode,
475 unsigned binfo_size)
476{
477 struct nilfs_segment_buffer *segbuf;
478 int required, err = 0;
479
480 retry:
481 segbuf = sci->sc_curseg;
482 required = nilfs_segctor_segsum_block_required(
483 sci, &sci->sc_binfo_ptr, binfo_size);
484 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
485 nilfs_segctor_end_finfo(sci, inode);
486 err = nilfs_segctor_feed_segment(sci);
487 if (err)
488 return err;
489 goto retry;
490 }
491 if (unlikely(required)) {
492 err = nilfs_segbuf_extend_segsum(segbuf);
493 if (unlikely(err))
494 goto failed;
495 }
496 if (sci->sc_blk_cnt == 0)
497 nilfs_segctor_begin_finfo(sci, inode);
498
499 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
500 /* Substitution to vblocknr is delayed until update_blocknr() */
501 nilfs_segbuf_add_file_buffer(segbuf, bh);
502 sci->sc_blk_cnt++;
503 failed:
504 return err;
505}
506
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700507/*
508 * Callback functions that enumerate, mark, and collect dirty blocks
509 */
510static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
511 struct buffer_head *bh, struct inode *inode)
512{
513 int err;
514
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700515 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900516 if (err < 0)
517 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700518
519 err = nilfs_segctor_add_file_block(sci, bh, inode,
520 sizeof(struct nilfs_binfo_v));
521 if (!err)
522 sci->sc_datablk_cnt++;
523 return err;
524}
525
526static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
527 struct buffer_head *bh,
528 struct inode *inode)
529{
Ryusuke Konishie8289492010-11-19 15:26:20 +0900530 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700531}
532
533static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
534 struct buffer_head *bh,
535 struct inode *inode)
536{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700537 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700538 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
539}
540
541static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
542 struct nilfs_segsum_pointer *ssp,
543 union nilfs_binfo *binfo)
544{
545 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
546 sci, ssp, sizeof(*binfo_v));
547 *binfo_v = binfo->bi_v;
548}
549
550static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
551 struct nilfs_segsum_pointer *ssp,
552 union nilfs_binfo *binfo)
553{
554 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
555 sci, ssp, sizeof(*vblocknr));
556 *vblocknr = binfo->bi_v.bi_vblocknr;
557}
558
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900559static struct nilfs_sc_operations nilfs_sc_file_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700560 .collect_data = nilfs_collect_file_data,
561 .collect_node = nilfs_collect_file_node,
562 .collect_bmap = nilfs_collect_file_bmap,
563 .write_data_binfo = nilfs_write_file_data_binfo,
564 .write_node_binfo = nilfs_write_file_node_binfo,
565};
566
567static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
568 struct buffer_head *bh, struct inode *inode)
569{
570 int err;
571
572 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900573 if (err < 0)
574 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700575
576 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
577 if (!err)
578 sci->sc_datablk_cnt++;
579 return err;
580}
581
582static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
583 struct buffer_head *bh, struct inode *inode)
584{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700585 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700586 return nilfs_segctor_add_file_block(sci, bh, inode,
587 sizeof(struct nilfs_binfo_dat));
588}
589
590static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
591 struct nilfs_segsum_pointer *ssp,
592 union nilfs_binfo *binfo)
593{
594 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
595 sizeof(*blkoff));
596 *blkoff = binfo->bi_dat.bi_blkoff;
597}
598
599static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
600 struct nilfs_segsum_pointer *ssp,
601 union nilfs_binfo *binfo)
602{
603 struct nilfs_binfo_dat *binfo_dat =
604 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
605 *binfo_dat = binfo->bi_dat;
606}
607
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900608static struct nilfs_sc_operations nilfs_sc_dat_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700609 .collect_data = nilfs_collect_dat_data,
610 .collect_node = nilfs_collect_file_node,
611 .collect_bmap = nilfs_collect_dat_bmap,
612 .write_data_binfo = nilfs_write_dat_data_binfo,
613 .write_node_binfo = nilfs_write_dat_node_binfo,
614};
615
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900616static struct nilfs_sc_operations nilfs_sc_dsync_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700617 .collect_data = nilfs_collect_file_data,
618 .collect_node = NULL,
619 .collect_bmap = NULL,
620 .write_data_binfo = nilfs_write_file_data_binfo,
621 .write_node_binfo = NULL,
622};
623
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700624static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
625 struct list_head *listp,
626 size_t nlimit,
627 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700628{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700629 struct address_space *mapping = inode->i_mapping;
630 struct pagevec pvec;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700631 pgoff_t index = 0, last = ULONG_MAX;
632 size_t ndirties = 0;
633 int i;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700634
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700635 if (unlikely(start != 0 || end != LLONG_MAX)) {
636 /*
637 * A valid range is given for sync-ing data pages. The
638 * range is rounded to per-page; extra dirty buffers
639 * may be included if blocksize < pagesize.
640 */
641 index = start >> PAGE_SHIFT;
642 last = end >> PAGE_SHIFT;
643 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700644 pagevec_init(&pvec, 0);
645 repeat:
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700646 if (unlikely(index > last) ||
647 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
648 min_t(pgoff_t, last - index,
649 PAGEVEC_SIZE - 1) + 1))
650 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700651
652 for (i = 0; i < pagevec_count(&pvec); i++) {
653 struct buffer_head *bh, *head;
654 struct page *page = pvec.pages[i];
655
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700656 if (unlikely(page->index > last))
657 break;
658
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700659 if (mapping->host) {
660 lock_page(page);
661 if (!page_has_buffers(page))
662 create_empty_buffers(page,
663 1 << inode->i_blkbits, 0);
664 unlock_page(page);
665 }
666
667 bh = head = page_buffers(page);
668 do {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700669 if (!buffer_dirty(bh))
670 continue;
671 get_bh(bh);
672 list_add_tail(&bh->b_assoc_buffers, listp);
673 ndirties++;
674 if (unlikely(ndirties >= nlimit)) {
675 pagevec_release(&pvec);
676 cond_resched();
677 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700678 }
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700679 } while (bh = bh->b_this_page, bh != head);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700680 }
681 pagevec_release(&pvec);
682 cond_resched();
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700683 goto repeat;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700684}
685
686static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
687 struct list_head *listp)
688{
689 struct nilfs_inode_info *ii = NILFS_I(inode);
690 struct address_space *mapping = &ii->i_btnode_cache;
691 struct pagevec pvec;
692 struct buffer_head *bh, *head;
693 unsigned int i;
694 pgoff_t index = 0;
695
696 pagevec_init(&pvec, 0);
697
698 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
699 PAGEVEC_SIZE)) {
700 for (i = 0; i < pagevec_count(&pvec); i++) {
701 bh = head = page_buffers(pvec.pages[i]);
702 do {
703 if (buffer_dirty(bh)) {
704 get_bh(bh);
705 list_add_tail(&bh->b_assoc_buffers,
706 listp);
707 }
708 bh = bh->b_this_page;
709 } while (bh != head);
710 }
711 pagevec_release(&pvec);
712 cond_resched();
713 }
714}
715
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900716static void nilfs_dispose_list(struct the_nilfs *nilfs,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700717 struct list_head *head, int force)
718{
719 struct nilfs_inode_info *ii, *n;
720 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
721 unsigned nv = 0;
722
723 while (!list_empty(head)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900724 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700725 list_for_each_entry_safe(ii, n, head, i_dirty) {
726 list_del_init(&ii->i_dirty);
727 if (force) {
728 if (unlikely(ii->i_bh)) {
729 brelse(ii->i_bh);
730 ii->i_bh = NULL;
731 }
732 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
733 set_bit(NILFS_I_QUEUED, &ii->i_state);
734 list_add_tail(&ii->i_dirty,
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900735 &nilfs->ns_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700736 continue;
737 }
738 ivec[nv++] = ii;
739 if (nv == SC_N_INODEVEC)
740 break;
741 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900742 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700743
744 for (pii = ivec; nv > 0; pii++, nv--)
745 iput(&(*pii)->vfs_inode);
746 }
747}
748
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900749static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
750 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700751{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700752 int ret = 0;
753
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900754 if (nilfs_mdt_fetch_dirty(root->ifile))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700755 ret++;
756 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
757 ret++;
758 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
759 ret++;
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900760 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
761 ret++;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700762 return ret;
763}
764
765static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
766{
767 return list_empty(&sci->sc_dirty_files) &&
768 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900769 sci->sc_nfreesegs == 0 &&
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700770 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
771}
772
773static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
774{
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900775 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700776 int ret = 0;
777
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900778 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700779 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
780
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900781 spin_lock(&nilfs->ns_inode_lock);
782 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700783 ret++;
784
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900785 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700786 return ret;
787}
788
789static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
790{
791 struct nilfs_sb_info *sbi = sci->sc_sbi;
792 struct the_nilfs *nilfs = sbi->s_nilfs;
793
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900794 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700795 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
796 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900797 nilfs_mdt_clear_dirty(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700798}
799
800static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
801{
802 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
803 struct buffer_head *bh_cp;
804 struct nilfs_checkpoint *raw_cp;
805 int err;
806
807 /* XXX: this interface will be changed */
808 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
809 &raw_cp, &bh_cp);
810 if (likely(!err)) {
811 /* The following code is duplicated with cpfile. But, it is
812 needed to collect the checkpoint even if it was not newly
813 created */
814 nilfs_mdt_mark_buffer_dirty(bh_cp);
815 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
816 nilfs_cpfile_put_checkpoint(
817 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700818 } else
819 WARN_ON(err == -EINVAL || err == -ENOENT);
820
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700821 return err;
822}
823
824static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
825{
826 struct nilfs_sb_info *sbi = sci->sc_sbi;
827 struct the_nilfs *nilfs = sbi->s_nilfs;
828 struct buffer_head *bh_cp;
829 struct nilfs_checkpoint *raw_cp;
830 int err;
831
832 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
833 &raw_cp, &bh_cp);
834 if (unlikely(err)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700835 WARN_ON(err == -EINVAL || err == -ENOENT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700836 goto failed_ibh;
837 }
838 raw_cp->cp_snapshot_list.ssl_next = 0;
839 raw_cp->cp_snapshot_list.ssl_prev = 0;
840 raw_cp->cp_inodes_count =
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900841 cpu_to_le64(atomic_read(&sci->sc_root->inodes_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700842 raw_cp->cp_blocks_count =
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900843 cpu_to_le64(atomic_read(&sci->sc_root->blocks_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700844 raw_cp->cp_nblk_inc =
845 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
846 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
847 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700848
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700849 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
850 nilfs_checkpoint_clear_minor(raw_cp);
851 else
852 nilfs_checkpoint_set_minor(raw_cp);
853
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900854 nilfs_write_inode_common(sci->sc_root->ifile,
855 &raw_cp->cp_ifile_inode, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700856 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
857 return 0;
858
859 failed_ibh:
860 return err;
861}
862
863static void nilfs_fill_in_file_bmap(struct inode *ifile,
864 struct nilfs_inode_info *ii)
865
866{
867 struct buffer_head *ibh;
868 struct nilfs_inode *raw_inode;
869
870 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
871 ibh = ii->i_bh;
872 BUG_ON(!ibh);
873 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
874 ibh);
875 nilfs_bmap_write(ii->i_bmap, raw_inode);
876 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
877 }
878}
879
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900880static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700881{
882 struct nilfs_inode_info *ii;
883
884 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900885 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700886 set_bit(NILFS_I_COLLECTED, &ii->i_state);
887 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700888}
889
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700890static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
891 struct the_nilfs *nilfs)
892{
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900893 struct buffer_head *bh_sr;
894 struct nilfs_super_root *raw_sr;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700895 unsigned isz = nilfs->ns_inode_size;
896
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900897 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
898 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
899
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700900 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
901 raw_sr->sr_nongc_ctime
902 = cpu_to_le64(nilfs_doing_gc() ?
903 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
904 raw_sr->sr_flags = 0;
905
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900906 nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr +
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900907 NILFS_SR_DAT_OFFSET(isz), 1);
908 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
909 NILFS_SR_CPFILE_OFFSET(isz), 1);
910 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
911 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700912}
913
914static void nilfs_redirty_inodes(struct list_head *head)
915{
916 struct nilfs_inode_info *ii;
917
918 list_for_each_entry(ii, head, i_dirty) {
919 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
920 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
921 }
922}
923
924static void nilfs_drop_collected_inodes(struct list_head *head)
925{
926 struct nilfs_inode_info *ii;
927
928 list_for_each_entry(ii, head, i_dirty) {
929 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
930 continue;
931
932 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
933 set_bit(NILFS_I_UPDATED, &ii->i_state);
934 }
935}
936
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700937static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
938 struct inode *inode,
939 struct list_head *listp,
940 int (*collect)(struct nilfs_sc_info *,
941 struct buffer_head *,
942 struct inode *))
943{
944 struct buffer_head *bh, *n;
945 int err = 0;
946
947 if (collect) {
948 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
949 list_del_init(&bh->b_assoc_buffers);
950 err = collect(sci, bh, inode);
951 brelse(bh);
952 if (unlikely(err))
953 goto dispose_buffers;
954 }
955 return 0;
956 }
957
958 dispose_buffers:
959 while (!list_empty(listp)) {
960 bh = list_entry(listp->next, struct buffer_head,
961 b_assoc_buffers);
962 list_del_init(&bh->b_assoc_buffers);
963 brelse(bh);
964 }
965 return err;
966}
967
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700968static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
969{
970 /* Remaining number of blocks within segment buffer */
971 return sci->sc_segbuf_nblocks -
972 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
973}
974
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700975static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
976 struct inode *inode,
977 struct nilfs_sc_operations *sc_ops)
978{
979 LIST_HEAD(data_buffers);
980 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700981 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700982
983 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700984 size_t n, rest = nilfs_segctor_buffer_rest(sci);
985
986 n = nilfs_lookup_dirty_data_buffers(
987 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
988 if (n > rest) {
989 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700990 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700991 sc_ops->collect_data);
992 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700993 goto break_or_fail;
994 }
995 }
996 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
997
998 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
999 err = nilfs_segctor_apply_buffers(
1000 sci, inode, &data_buffers, sc_ops->collect_data);
1001 if (unlikely(err)) {
1002 /* dispose node list */
1003 nilfs_segctor_apply_buffers(
1004 sci, inode, &node_buffers, NULL);
1005 goto break_or_fail;
1006 }
1007 sci->sc_stage.flags |= NILFS_CF_NODE;
1008 }
1009 /* Collect node */
1010 err = nilfs_segctor_apply_buffers(
1011 sci, inode, &node_buffers, sc_ops->collect_node);
1012 if (unlikely(err))
1013 goto break_or_fail;
1014
1015 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1016 err = nilfs_segctor_apply_buffers(
1017 sci, inode, &node_buffers, sc_ops->collect_bmap);
1018 if (unlikely(err))
1019 goto break_or_fail;
1020
1021 nilfs_segctor_end_finfo(sci, inode);
1022 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1023
1024 break_or_fail:
1025 return err;
1026}
1027
1028static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1029 struct inode *inode)
1030{
1031 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001032 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1033 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001034
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001035 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1036 sci->sc_dsync_start,
1037 sci->sc_dsync_end);
1038
1039 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1040 nilfs_collect_file_data);
1041 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001042 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001043 BUG_ON(n > rest);
1044 /* always receive -E2BIG or true error if n > rest */
1045 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001046 return err;
1047}
1048
1049static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1050{
1051 struct nilfs_sb_info *sbi = sci->sc_sbi;
1052 struct the_nilfs *nilfs = sbi->s_nilfs;
1053 struct list_head *head;
1054 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001055 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001056 int err = 0;
1057
1058 switch (sci->sc_stage.scnt) {
1059 case NILFS_ST_INIT:
1060 /* Pre-processes */
1061 sci->sc_stage.flags = 0;
1062
1063 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1064 sci->sc_nblk_inc = 0;
1065 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1066 if (mode == SC_LSEG_DSYNC) {
1067 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1068 goto dsync_mode;
1069 }
1070 }
1071
1072 sci->sc_stage.dirty_file_ptr = NULL;
1073 sci->sc_stage.gc_inode_ptr = NULL;
1074 if (mode == SC_FLUSH_DAT) {
1075 sci->sc_stage.scnt = NILFS_ST_DAT;
1076 goto dat_stage;
1077 }
1078 sci->sc_stage.scnt++; /* Fall through */
1079 case NILFS_ST_GC:
1080 if (nilfs_doing_gc()) {
1081 head = &sci->sc_gc_inodes;
1082 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1083 head, i_dirty);
1084 list_for_each_entry_continue(ii, head, i_dirty) {
1085 err = nilfs_segctor_scan_file(
1086 sci, &ii->vfs_inode,
1087 &nilfs_sc_file_ops);
1088 if (unlikely(err)) {
1089 sci->sc_stage.gc_inode_ptr = list_entry(
1090 ii->i_dirty.prev,
1091 struct nilfs_inode_info,
1092 i_dirty);
1093 goto break_or_fail;
1094 }
1095 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1096 }
1097 sci->sc_stage.gc_inode_ptr = NULL;
1098 }
1099 sci->sc_stage.scnt++; /* Fall through */
1100 case NILFS_ST_FILE:
1101 head = &sci->sc_dirty_files;
1102 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1103 i_dirty);
1104 list_for_each_entry_continue(ii, head, i_dirty) {
1105 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1106
1107 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1108 &nilfs_sc_file_ops);
1109 if (unlikely(err)) {
1110 sci->sc_stage.dirty_file_ptr =
1111 list_entry(ii->i_dirty.prev,
1112 struct nilfs_inode_info,
1113 i_dirty);
1114 goto break_or_fail;
1115 }
1116 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1117 /* XXX: required ? */
1118 }
1119 sci->sc_stage.dirty_file_ptr = NULL;
1120 if (mode == SC_FLUSH_FILE) {
1121 sci->sc_stage.scnt = NILFS_ST_DONE;
1122 return 0;
1123 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001124 sci->sc_stage.scnt++;
1125 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1126 /* Fall through */
1127 case NILFS_ST_IFILE:
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001128 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001129 &nilfs_sc_file_ops);
1130 if (unlikely(err))
1131 break;
1132 sci->sc_stage.scnt++;
1133 /* Creating a checkpoint */
1134 err = nilfs_segctor_create_checkpoint(sci);
1135 if (unlikely(err))
1136 break;
1137 /* Fall through */
1138 case NILFS_ST_CPFILE:
1139 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1140 &nilfs_sc_file_ops);
1141 if (unlikely(err))
1142 break;
1143 sci->sc_stage.scnt++; /* Fall through */
1144 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001145 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1146 sci->sc_nfreesegs, &ndone);
1147 if (unlikely(err)) {
1148 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1149 sci->sc_freesegs, ndone,
1150 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001151 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001152 }
1153 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1154
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001155 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1156 &nilfs_sc_file_ops);
1157 if (unlikely(err))
1158 break;
1159 sci->sc_stage.scnt++; /* Fall through */
1160 case NILFS_ST_DAT:
1161 dat_stage:
Ryusuke Konishi365e2152010-12-27 00:07:30 +09001162 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001163 &nilfs_sc_dat_ops);
1164 if (unlikely(err))
1165 break;
1166 if (mode == SC_FLUSH_DAT) {
1167 sci->sc_stage.scnt = NILFS_ST_DONE;
1168 return 0;
1169 }
1170 sci->sc_stage.scnt++; /* Fall through */
1171 case NILFS_ST_SR:
1172 if (mode == SC_LSEG_SR) {
1173 /* Appending a super root */
1174 err = nilfs_segctor_add_super_root(sci);
1175 if (unlikely(err))
1176 break;
1177 }
1178 /* End of a logical segment */
1179 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1180 sci->sc_stage.scnt = NILFS_ST_DONE;
1181 return 0;
1182 case NILFS_ST_DSYNC:
1183 dsync_mode:
1184 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001185 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001186 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1187 break;
1188
1189 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1190 if (unlikely(err))
1191 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001192 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1193 sci->sc_stage.scnt = NILFS_ST_DONE;
1194 return 0;
1195 case NILFS_ST_DONE:
1196 return 0;
1197 default:
1198 BUG();
1199 }
1200
1201 break_or_fail:
1202 return err;
1203}
1204
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001205/**
1206 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1207 * @sci: nilfs_sc_info
1208 * @nilfs: nilfs object
1209 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001210static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1211 struct the_nilfs *nilfs)
1212{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001213 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001214 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001215 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001216
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001217 segbuf = nilfs_segbuf_new(sci->sc_super);
1218 if (unlikely(!segbuf))
1219 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001220
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001221 if (list_empty(&sci->sc_write_logs)) {
1222 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1223 nilfs->ns_pseg_offset, nilfs);
1224 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1225 nilfs_shift_to_next_segment(nilfs);
1226 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1227 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001228
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001229 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001230 nextnum = nilfs->ns_nextnum;
1231
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001232 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1233 /* Start from the head of a new full segment */
1234 alloc++;
1235 } else {
1236 /* Continue logs */
1237 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1238 nilfs_segbuf_map_cont(segbuf, prev);
1239 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1240 nextnum = prev->sb_nextnum;
1241
1242 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1243 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1244 segbuf->sb_sum.seg_seq++;
1245 alloc++;
1246 }
1247 }
1248
1249 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1250 if (err)
1251 goto failed;
1252
1253 if (alloc) {
1254 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1255 if (err)
1256 goto failed;
1257 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001258 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1259
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001260 BUG_ON(!list_empty(&sci->sc_segbufs));
1261 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1262 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001263 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001264
1265 failed:
1266 nilfs_segbuf_free(segbuf);
1267 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001268}
1269
1270static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1271 struct the_nilfs *nilfs, int nadd)
1272{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001273 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001274 struct inode *sufile = nilfs->ns_sufile;
1275 __u64 nextnextnum;
1276 LIST_HEAD(list);
1277 int err, ret, i;
1278
1279 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1280 /*
1281 * Since the segment specified with nextnum might be allocated during
1282 * the previous construction, the buffer including its segusage may
1283 * not be dirty. The following call ensures that the buffer is dirty
1284 * and will pin the buffer on memory until the sufile is written.
1285 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001286 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001287 if (unlikely(err))
1288 return err;
1289
1290 for (i = 0; i < nadd; i++) {
1291 /* extend segment info */
1292 err = -ENOMEM;
1293 segbuf = nilfs_segbuf_new(sci->sc_super);
1294 if (unlikely(!segbuf))
1295 goto failed;
1296
1297 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001298 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001299 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1300
1301 /* allocate the next next full segment */
1302 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1303 if (unlikely(err))
1304 goto failed_segbuf;
1305
1306 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1307 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1308
1309 list_add_tail(&segbuf->sb_list, &list);
1310 prev = segbuf;
1311 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001312 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001313 return 0;
1314
1315 failed_segbuf:
1316 nilfs_segbuf_free(segbuf);
1317 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001318 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001319 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001320 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001321 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001322 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001323 return err;
1324}
1325
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001326static void nilfs_free_incomplete_logs(struct list_head *logs,
1327 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001328{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001329 struct nilfs_segment_buffer *segbuf, *prev;
1330 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001331 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001332
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001333 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001334 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001335 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001336 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001337 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001338 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001339 /* Case 1: The first segment failed */
1340 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1341 /* Case 1a: Partial segment appended into an existing
1342 segment */
1343 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1344 segbuf->sb_fseg_end);
1345 else /* Case 1b: New full segment */
1346 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001347 }
1348
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001349 prev = segbuf;
1350 list_for_each_entry_continue(segbuf, logs, sb_list) {
1351 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1352 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1353 WARN_ON(ret); /* never fails */
1354 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001355 if (atomic_read(&segbuf->sb_err) &&
1356 segbuf->sb_segnum != nilfs->ns_nextnum)
1357 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001358 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1359 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001360 }
1361}
1362
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001363static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1364 struct inode *sufile)
1365{
1366 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001367 unsigned long live_blocks;
1368 int ret;
1369
1370 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001371 live_blocks = segbuf->sb_sum.nblocks +
1372 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001373 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1374 live_blocks,
1375 sci->sc_seg_ctime);
1376 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001377 }
1378}
1379
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001380static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001381{
1382 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001383 int ret;
1384
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001385 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001386 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1387 segbuf->sb_pseg_start -
1388 segbuf->sb_fseg_start, 0);
1389 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001390
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001391 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001392 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1393 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001394 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001395 }
1396}
1397
1398static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1399 struct nilfs_segment_buffer *last,
1400 struct inode *sufile)
1401{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001402 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001403 int ret;
1404
Ryusuke Konishie29df392009-11-29 16:51:16 +09001405 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001406 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1407 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001408 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001409 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001410 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001411}
1412
1413
1414static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1415 struct the_nilfs *nilfs, int mode)
1416{
1417 struct nilfs_cstage prev_stage = sci->sc_stage;
1418 int err, nadd = 1;
1419
1420 /* Collection retry loop */
1421 for (;;) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001422 sci->sc_nblk_this_inc = 0;
1423 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1424
1425 err = nilfs_segctor_reset_segment_buffer(sci);
1426 if (unlikely(err))
1427 goto failed;
1428
1429 err = nilfs_segctor_collect_blocks(sci, mode);
1430 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1431 if (!err)
1432 break;
1433
1434 if (unlikely(err != -E2BIG))
1435 goto failed;
1436
1437 /* The current segment is filled up */
1438 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1439 break;
1440
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001441 nilfs_clear_logs(&sci->sc_segbufs);
1442
1443 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1444 if (unlikely(err))
1445 return err;
1446
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001447 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1448 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1449 sci->sc_freesegs,
1450 sci->sc_nfreesegs,
1451 NULL);
1452 WARN_ON(err); /* do not happen */
1453 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001454 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1455 sci->sc_stage = prev_stage;
1456 }
1457 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1458 return 0;
1459
1460 failed:
1461 return err;
1462}
1463
1464static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1465 struct buffer_head *new_bh)
1466{
1467 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1468
1469 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1470 /* The caller must release old_bh */
1471}
1472
1473static int
1474nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1475 struct nilfs_segment_buffer *segbuf,
1476 int mode)
1477{
1478 struct inode *inode = NULL;
1479 sector_t blocknr;
1480 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1481 unsigned long nblocks = 0, ndatablk = 0;
1482 struct nilfs_sc_operations *sc_op = NULL;
1483 struct nilfs_segsum_pointer ssp;
1484 struct nilfs_finfo *finfo = NULL;
1485 union nilfs_binfo binfo;
1486 struct buffer_head *bh, *bh_org;
1487 ino_t ino = 0;
1488 int err = 0;
1489
1490 if (!nfinfo)
1491 goto out;
1492
1493 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1494 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1495 ssp.offset = sizeof(struct nilfs_segment_summary);
1496
1497 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001498 if (bh == segbuf->sb_super_root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001499 break;
1500 if (!finfo) {
1501 finfo = nilfs_segctor_map_segsum_entry(
1502 sci, &ssp, sizeof(*finfo));
1503 ino = le64_to_cpu(finfo->fi_ino);
1504 nblocks = le32_to_cpu(finfo->fi_nblocks);
1505 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1506
1507 if (buffer_nilfs_node(bh))
1508 inode = NILFS_BTNC_I(bh->b_page->mapping);
1509 else
1510 inode = NILFS_AS_I(bh->b_page->mapping);
1511
1512 if (mode == SC_LSEG_DSYNC)
1513 sc_op = &nilfs_sc_dsync_ops;
1514 else if (ino == NILFS_DAT_INO)
1515 sc_op = &nilfs_sc_dat_ops;
1516 else /* file blocks */
1517 sc_op = &nilfs_sc_file_ops;
1518 }
1519 bh_org = bh;
1520 get_bh(bh_org);
1521 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1522 &binfo);
1523 if (bh != bh_org)
1524 nilfs_list_replace_buffer(bh_org, bh);
1525 brelse(bh_org);
1526 if (unlikely(err))
1527 goto failed_bmap;
1528
1529 if (ndatablk > 0)
1530 sc_op->write_data_binfo(sci, &ssp, &binfo);
1531 else
1532 sc_op->write_node_binfo(sci, &ssp, &binfo);
1533
1534 blocknr++;
1535 if (--nblocks == 0) {
1536 finfo = NULL;
1537 if (--nfinfo == 0)
1538 break;
1539 } else if (ndatablk > 0)
1540 ndatablk--;
1541 }
1542 out:
1543 return 0;
1544
1545 failed_bmap:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001546 return err;
1547}
1548
1549static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1550{
1551 struct nilfs_segment_buffer *segbuf;
1552 int err;
1553
1554 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1555 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1556 if (unlikely(err))
1557 return err;
1558 nilfs_segbuf_fill_in_segsum(segbuf);
1559 }
1560 return 0;
1561}
1562
1563static int
1564nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1565{
1566 struct page *clone_page;
1567 struct buffer_head *bh, *head, *bh2;
1568 void *kaddr;
1569
1570 bh = head = page_buffers(page);
1571
1572 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1573 if (unlikely(!clone_page))
1574 return -ENOMEM;
1575
1576 bh2 = page_buffers(clone_page);
1577 kaddr = kmap_atomic(page, KM_USER0);
1578 do {
1579 if (list_empty(&bh->b_assoc_buffers))
1580 continue;
1581 get_bh(bh2);
1582 page_cache_get(clone_page); /* for each bh */
1583 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1584 bh2->b_blocknr = bh->b_blocknr;
1585 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1586 list_add_tail(&bh->b_assoc_buffers, out);
1587 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1588 kunmap_atomic(kaddr, KM_USER0);
1589
1590 if (!TestSetPageWriteback(clone_page))
Michael Rubinf629d1c2010-10-26 14:21:33 -07001591 account_page_writeback(clone_page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001592 unlock_page(clone_page);
1593
1594 return 0;
1595}
1596
1597static int nilfs_test_page_to_be_frozen(struct page *page)
1598{
1599 struct address_space *mapping = page->mapping;
1600
1601 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1602 return 0;
1603
1604 if (page_mapped(page)) {
1605 ClearPageChecked(page);
1606 return 1;
1607 }
1608 return PageChecked(page);
1609}
1610
1611static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1612{
1613 if (!page || PageWriteback(page))
1614 /* For split b-tree node pages, this function may be called
1615 twice. We ignore the 2nd or later calls by this check. */
1616 return 0;
1617
1618 lock_page(page);
1619 clear_page_dirty_for_io(page);
1620 set_page_writeback(page);
1621 unlock_page(page);
1622
1623 if (nilfs_test_page_to_be_frozen(page)) {
1624 int err = nilfs_copy_replace_page_buffers(page, out);
1625 if (unlikely(err))
1626 return err;
1627 }
1628 return 0;
1629}
1630
1631static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1632 struct page **failed_page)
1633{
1634 struct nilfs_segment_buffer *segbuf;
1635 struct page *bd_page = NULL, *fs_page = NULL;
1636 struct list_head *list = &sci->sc_copied_buffers;
1637 int err;
1638
1639 *failed_page = NULL;
1640 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1641 struct buffer_head *bh;
1642
1643 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1644 b_assoc_buffers) {
1645 if (bh->b_page != bd_page) {
1646 if (bd_page) {
1647 lock_page(bd_page);
1648 clear_page_dirty_for_io(bd_page);
1649 set_page_writeback(bd_page);
1650 unlock_page(bd_page);
1651 }
1652 bd_page = bh->b_page;
1653 }
1654 }
1655
1656 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1657 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001658 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001659 if (bh->b_page != bd_page) {
1660 lock_page(bd_page);
1661 clear_page_dirty_for_io(bd_page);
1662 set_page_writeback(bd_page);
1663 unlock_page(bd_page);
1664 bd_page = bh->b_page;
1665 }
1666 break;
1667 }
1668 if (bh->b_page != fs_page) {
1669 err = nilfs_begin_page_io(fs_page, list);
1670 if (unlikely(err)) {
1671 *failed_page = fs_page;
1672 goto out;
1673 }
1674 fs_page = bh->b_page;
1675 }
1676 }
1677 }
1678 if (bd_page) {
1679 lock_page(bd_page);
1680 clear_page_dirty_for_io(bd_page);
1681 set_page_writeback(bd_page);
1682 unlock_page(bd_page);
1683 }
1684 err = nilfs_begin_page_io(fs_page, list);
1685 if (unlikely(err))
1686 *failed_page = fs_page;
1687 out:
1688 return err;
1689}
1690
1691static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001692 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001693{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001694 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001695
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001696 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001697 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1698 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001699}
1700
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001701static void __nilfs_end_page_io(struct page *page, int err)
1702{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001703 if (!err) {
1704 if (!nilfs_page_buffers_clean(page))
1705 __set_page_dirty_nobuffers(page);
1706 ClearPageError(page);
1707 } else {
1708 __set_page_dirty_nobuffers(page);
1709 SetPageError(page);
1710 }
1711
1712 if (buffer_nilfs_allocated(page_buffers(page))) {
1713 if (TestClearPageWriteback(page))
1714 dec_zone_page_state(page, NR_WRITEBACK);
1715 } else
1716 end_page_writeback(page);
1717}
1718
1719static void nilfs_end_page_io(struct page *page, int err)
1720{
1721 if (!page)
1722 return;
1723
Ryusuke Konishia9777842009-07-28 17:55:29 +09001724 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001725 /*
1726 * For b-tree node pages, this function may be called twice
1727 * or more because they might be split in a segment.
1728 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001729 if (PageDirty(page)) {
1730 /*
1731 * For pages holding split b-tree node buffers, dirty
1732 * flag on the buffers may be cleared discretely.
1733 * In that case, the page is once redirtied for
1734 * remaining buffers, and it must be cancelled if
1735 * all the buffers get cleaned later.
1736 */
1737 lock_page(page);
1738 if (nilfs_page_buffers_clean(page))
1739 __nilfs_clear_page_dirty(page);
1740 unlock_page(page);
1741 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001742 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001743 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001744
1745 __nilfs_end_page_io(page, err);
1746}
1747
1748static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1749{
1750 struct buffer_head *bh, *head;
1751 struct page *page;
1752
1753 while (!list_empty(list)) {
1754 bh = list_entry(list->next, struct buffer_head,
1755 b_assoc_buffers);
1756 page = bh->b_page;
1757 page_cache_get(page);
1758 head = bh = page_buffers(page);
1759 do {
1760 if (!list_empty(&bh->b_assoc_buffers)) {
1761 list_del_init(&bh->b_assoc_buffers);
1762 if (!err) {
1763 set_buffer_uptodate(bh);
1764 clear_buffer_dirty(bh);
Ryusuke Konishi27e6c7a2010-12-26 16:28:28 +09001765 clear_buffer_delay(bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001766 clear_buffer_nilfs_volatile(bh);
1767 }
1768 brelse(bh); /* for b_assoc_buffers */
1769 }
1770 } while ((bh = bh->b_this_page) != head);
1771
1772 __nilfs_end_page_io(page, err);
1773 page_cache_release(page);
1774 }
1775}
1776
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001777static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001778 int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001779{
1780 struct nilfs_segment_buffer *segbuf;
1781 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001782 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001783
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001784 if (list_empty(logs))
1785 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001786
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001787 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001788 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1789 b_assoc_buffers) {
1790 if (bh->b_page != bd_page) {
1791 if (bd_page)
1792 end_page_writeback(bd_page);
1793 bd_page = bh->b_page;
1794 }
1795 }
1796
1797 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1798 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001799 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001800 if (bh->b_page != bd_page) {
1801 end_page_writeback(bd_page);
1802 bd_page = bh->b_page;
1803 }
1804 break;
1805 }
1806 if (bh->b_page != fs_page) {
1807 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001808 if (fs_page && fs_page == failed_page)
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001809 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001810 fs_page = bh->b_page;
1811 }
1812 }
1813 }
1814 if (bd_page)
1815 end_page_writeback(bd_page);
1816
1817 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001818}
1819
1820static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1821 struct the_nilfs *nilfs, int err)
1822{
1823 LIST_HEAD(logs);
1824 int ret;
1825
1826 list_splice_tail_init(&sci->sc_write_logs, &logs);
1827 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001828 nilfs_abort_logs(&logs, NULL, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001829
1830 list_splice_tail_init(&sci->sc_segbufs, &logs);
1831 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1832 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001833 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001834
1835 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1836 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1837 sci->sc_freesegs,
1838 sci->sc_nfreesegs,
1839 NULL);
1840 WARN_ON(ret); /* do not happen */
1841 }
1842
1843 nilfs_destroy_logs(&logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001844}
1845
1846static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1847 struct nilfs_segment_buffer *segbuf)
1848{
1849 nilfs->ns_segnum = segbuf->sb_segnum;
1850 nilfs->ns_nextnum = segbuf->sb_nextnum;
1851 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1852 + segbuf->sb_sum.nblocks;
1853 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1854 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1855}
1856
1857static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1858{
1859 struct nilfs_segment_buffer *segbuf;
1860 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09001861 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001862 int update_sr = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001863
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001864 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001865 struct buffer_head *bh;
1866
1867 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1868 b_assoc_buffers) {
1869 set_buffer_uptodate(bh);
1870 clear_buffer_dirty(bh);
1871 if (bh->b_page != bd_page) {
1872 if (bd_page)
1873 end_page_writeback(bd_page);
1874 bd_page = bh->b_page;
1875 }
1876 }
1877 /*
1878 * We assume that the buffers which belong to the same page
1879 * continue over the buffer list.
1880 * Under this assumption, the last BHs of pages is
1881 * identifiable by the discontinuity of bh->b_page
1882 * (page != fs_page).
1883 *
1884 * For B-tree node blocks, however, this assumption is not
1885 * guaranteed. The cleanup code of B-tree node pages needs
1886 * special care.
1887 */
1888 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1889 b_assoc_buffers) {
1890 set_buffer_uptodate(bh);
1891 clear_buffer_dirty(bh);
Ryusuke Konishi27e6c7a2010-12-26 16:28:28 +09001892 clear_buffer_delay(bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001893 clear_buffer_nilfs_volatile(bh);
Ryusuke Konishib1f6a4f2010-08-31 11:40:34 +09001894 clear_buffer_nilfs_redirected(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001895 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001896 if (bh->b_page != bd_page) {
1897 end_page_writeback(bd_page);
1898 bd_page = bh->b_page;
1899 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001900 update_sr = true;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001901 break;
1902 }
1903 if (bh->b_page != fs_page) {
1904 nilfs_end_page_io(fs_page, 0);
1905 fs_page = bh->b_page;
1906 }
1907 }
1908
Ryusuke Konishi47620772010-05-23 21:48:36 +09001909 if (!nilfs_segbuf_simplex(segbuf)) {
1910 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001911 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1912 sci->sc_lseg_stime = jiffies;
1913 }
Ryusuke Konishi47620772010-05-23 21:48:36 +09001914 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001915 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1916 }
1917 }
1918 /*
1919 * Since pages may continue over multiple segment buffers,
1920 * end of the last page must be checked outside of the loop.
1921 */
1922 if (bd_page)
1923 end_page_writeback(bd_page);
1924
1925 nilfs_end_page_io(fs_page, 0);
1926
1927 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
1928
1929 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1930
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001931 if (nilfs_doing_gc())
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001932 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001933 else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001934 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001935
1936 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1937
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001938 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001939 nilfs_set_next_segment(nilfs, segbuf);
1940
1941 if (update_sr) {
1942 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07001943 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001944
Ryusuke Konishic96fa462009-04-06 19:01:57 -07001945 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001946 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1947 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001948 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001949 } else
1950 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1951}
1952
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001953static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1954{
1955 int ret;
1956
1957 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1958 if (!ret) {
1959 nilfs_segctor_complete_write(sci);
1960 nilfs_destroy_logs(&sci->sc_write_logs);
1961 }
1962 return ret;
1963}
1964
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001965static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
1966 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001967{
1968 struct nilfs_inode_info *ii, *n;
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001969 struct inode *ifile = sci->sc_root->ifile;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001970
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001971 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001972 retry:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001973 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001974 if (!ii->i_bh) {
1975 struct buffer_head *ibh;
1976 int err;
1977
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001978 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001979 err = nilfs_ifile_get_inode_block(
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001980 ifile, ii->vfs_inode.i_ino, &ibh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001981 if (unlikely(err)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001982 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001983 "failed to get inode block.\n");
1984 return err;
1985 }
1986 nilfs_mdt_mark_buffer_dirty(ibh);
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001987 nilfs_mdt_mark_dirty(ifile);
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001988 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001989 if (likely(!ii->i_bh))
1990 ii->i_bh = ibh;
1991 else
1992 brelse(ibh);
1993 goto retry;
1994 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001995
1996 clear_bit(NILFS_I_QUEUED, &ii->i_state);
1997 set_bit(NILFS_I_BUSY, &ii->i_state);
1998 list_del(&ii->i_dirty);
1999 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2000 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002001 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002002
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002003 return 0;
2004}
2005
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002006static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
2007 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002008{
2009 struct nilfs_transaction_info *ti = current->journal_info;
2010 struct nilfs_inode_info *ii, *n;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002011
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002012 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002013 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2014 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002015 test_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002016 continue;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002017
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002018 clear_bit(NILFS_I_BUSY, &ii->i_state);
2019 brelse(ii->i_bh);
2020 ii->i_bh = NULL;
2021 list_del(&ii->i_dirty);
2022 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2023 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002024 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002025}
2026
2027/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002028 * Main procedure of segment constructor
2029 */
2030static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2031{
2032 struct nilfs_sb_info *sbi = sci->sc_sbi;
2033 struct the_nilfs *nilfs = sbi->s_nilfs;
2034 struct page *failed_page;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002035 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002036
2037 sci->sc_stage.scnt = NILFS_ST_INIT;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002038 sci->sc_cno = nilfs->ns_cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002039
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002040 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002041 if (unlikely(err))
2042 goto out;
2043
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002044 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002045 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2046
2047 if (nilfs_segctor_clean(sci))
2048 goto out;
2049
2050 do {
2051 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2052
2053 err = nilfs_segctor_begin_construction(sci, nilfs);
2054 if (unlikely(err))
2055 goto out;
2056
2057 /* Update time stamp */
2058 sci->sc_seg_ctime = get_seconds();
2059
2060 err = nilfs_segctor_collect(sci, nilfs, mode);
2061 if (unlikely(err))
2062 goto failed;
2063
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002064 /* Avoid empty segment */
2065 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
Ryusuke Konishi47620772010-05-23 21:48:36 +09002066 nilfs_segbuf_empty(sci->sc_curseg)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002067 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002068 goto out;
2069 }
2070
2071 err = nilfs_segctor_assign(sci, mode);
2072 if (unlikely(err))
2073 goto failed;
2074
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002075 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002076 nilfs_segctor_fill_in_file_bmap(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002077
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002078 if (mode == SC_LSEG_SR &&
2079 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002080 err = nilfs_segctor_fill_in_checkpoint(sci);
2081 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002082 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002083
2084 nilfs_segctor_fill_in_super_root(sci, nilfs);
2085 }
2086 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2087
2088 /* Write partial segments */
2089 err = nilfs_segctor_prepare_write(sci, &failed_page);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002090 if (err) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002091 nilfs_abort_logs(&sci->sc_segbufs, failed_page, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002092 goto failed_to_write;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002093 }
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +09002094
2095 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2096 nilfs->ns_crc_seed);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002097
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002098 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002099 if (unlikely(err))
2100 goto failed_to_write;
2101
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002102 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2103 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2104 /*
2105 * At this point, we avoid double buffering
2106 * for blocksize < pagesize because page dirty
2107 * flag is turned off during write and dirty
2108 * buffers are not properly collected for
2109 * pages crossing over segments.
2110 */
2111 err = nilfs_segctor_wait(sci);
2112 if (err)
2113 goto failed_to_write;
2114 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002115 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2116
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002117 out:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002118 nilfs_segctor_drop_written_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002119 return err;
2120
2121 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002122 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2123 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002124
2125 failed:
2126 if (nilfs_doing_gc())
2127 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002128 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002129 goto out;
2130}
2131
2132/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002133 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002134 * @sci: nilfs_sc_info
2135 *
2136 * If the timer has already been set, it ignores the new request.
2137 * This function MUST be called within a section locking the segment
2138 * semaphore.
2139 */
2140static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2141{
2142 spin_lock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002143 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2144 sci->sc_timer.expires = jiffies + sci->sc_interval;
2145 add_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002146 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2147 }
2148 spin_unlock(&sci->sc_state_lock);
2149}
2150
2151static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2152{
2153 spin_lock(&sci->sc_state_lock);
2154 if (!(sci->sc_flush_request & (1 << bn))) {
2155 unsigned long prev_req = sci->sc_flush_request;
2156
2157 sci->sc_flush_request |= (1 << bn);
2158 if (!prev_req)
2159 wake_up(&sci->sc_wait_daemon);
2160 }
2161 spin_unlock(&sci->sc_state_lock);
2162}
2163
2164/**
2165 * nilfs_flush_segment - trigger a segment construction for resource control
2166 * @sb: super block
2167 * @ino: inode number of the file to be flushed out.
2168 */
2169void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2170{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002171 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
2172 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002173
2174 if (!sci || nilfs_doing_construction())
2175 return;
2176 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2177 /* assign bit 0 to data files */
2178}
2179
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002180struct nilfs_segctor_wait_request {
2181 wait_queue_t wq;
2182 __u32 seq;
2183 int err;
2184 atomic_t done;
2185};
2186
2187static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2188{
2189 struct nilfs_segctor_wait_request wait_req;
2190 int err = 0;
2191
2192 spin_lock(&sci->sc_state_lock);
2193 init_wait(&wait_req.wq);
2194 wait_req.err = 0;
2195 atomic_set(&wait_req.done, 0);
2196 wait_req.seq = ++sci->sc_seq_request;
2197 spin_unlock(&sci->sc_state_lock);
2198
2199 init_waitqueue_entry(&wait_req.wq, current);
2200 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2201 set_current_state(TASK_INTERRUPTIBLE);
2202 wake_up(&sci->sc_wait_daemon);
2203
2204 for (;;) {
2205 if (atomic_read(&wait_req.done)) {
2206 err = wait_req.err;
2207 break;
2208 }
2209 if (!signal_pending(current)) {
2210 schedule();
2211 continue;
2212 }
2213 err = -ERESTARTSYS;
2214 break;
2215 }
2216 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2217 return err;
2218}
2219
2220static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2221{
2222 struct nilfs_segctor_wait_request *wrq, *n;
2223 unsigned long flags;
2224
2225 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2226 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2227 wq.task_list) {
2228 if (!atomic_read(&wrq->done) &&
2229 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2230 wrq->err = err;
2231 atomic_set(&wrq->done, 1);
2232 }
2233 if (atomic_read(&wrq->done)) {
2234 wrq->wq.func(&wrq->wq,
2235 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2236 0, NULL);
2237 }
2238 }
2239 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2240}
2241
2242/**
2243 * nilfs_construct_segment - construct a logical segment
2244 * @sb: super block
2245 *
2246 * Return Value: On success, 0 is retured. On errors, one of the following
2247 * negative error code is returned.
2248 *
2249 * %-EROFS - Read only filesystem.
2250 *
2251 * %-EIO - I/O error
2252 *
2253 * %-ENOSPC - No space left on device (only in a panic state).
2254 *
2255 * %-ERESTARTSYS - Interrupted.
2256 *
2257 * %-ENOMEM - Insufficient memory available.
2258 */
2259int nilfs_construct_segment(struct super_block *sb)
2260{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002261 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
2262 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002263 struct nilfs_transaction_info *ti;
2264 int err;
2265
2266 if (!sci)
2267 return -EROFS;
2268
2269 /* A call inside transactions causes a deadlock. */
2270 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2271
2272 err = nilfs_segctor_sync(sci);
2273 return err;
2274}
2275
2276/**
2277 * nilfs_construct_dsync_segment - construct a data-only logical segment
2278 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002279 * @inode: inode whose data blocks should be written out
2280 * @start: start byte offset
2281 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002282 *
2283 * Return Value: On success, 0 is retured. On errors, one of the following
2284 * negative error code is returned.
2285 *
2286 * %-EROFS - Read only filesystem.
2287 *
2288 * %-EIO - I/O error
2289 *
2290 * %-ENOSPC - No space left on device (only in a panic state).
2291 *
2292 * %-ERESTARTSYS - Interrupted.
2293 *
2294 * %-ENOMEM - Insufficient memory available.
2295 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002296int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2297 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002298{
2299 struct nilfs_sb_info *sbi = NILFS_SB(sb);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002300 struct the_nilfs *nilfs = sbi->s_nilfs;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002301 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002302 struct nilfs_inode_info *ii;
2303 struct nilfs_transaction_info ti;
2304 int err = 0;
2305
2306 if (!sci)
2307 return -EROFS;
2308
2309 nilfs_transaction_lock(sbi, &ti, 0);
2310
2311 ii = NILFS_I(inode);
2312 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002313 nilfs_test_opt(nilfs, STRICT_ORDER) ||
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002314 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002315 nilfs_discontinued(nilfs)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002316 nilfs_transaction_unlock(sbi);
2317 err = nilfs_segctor_sync(sci);
2318 return err;
2319 }
2320
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002321 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002322 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2323 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002324 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002325 nilfs_transaction_unlock(sbi);
2326 return 0;
2327 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002328 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002329 sci->sc_dsync_inode = ii;
2330 sci->sc_dsync_start = start;
2331 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002332
2333 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2334
2335 nilfs_transaction_unlock(sbi);
2336 return err;
2337}
2338
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002339#define FLUSH_FILE_BIT (0x1) /* data file only */
2340#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2341
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002342/**
2343 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2344 * @sci: segment constructor object
2345 */
2346static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002347{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002348 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002349 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002350 spin_unlock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002351 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002352}
2353
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002354/**
2355 * nilfs_segctor_notify - notify the result of request to caller threads
2356 * @sci: segment constructor object
2357 * @mode: mode of log forming
2358 * @err: error code to be notified
2359 */
2360static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002361{
2362 /* Clear requests (even when the construction failed) */
2363 spin_lock(&sci->sc_state_lock);
2364
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002365 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002366 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002367 sci->sc_seq_done = sci->sc_seq_accepted;
2368 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002369 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002370 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002371 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002372 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002373 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002374 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002375
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002376 /* re-enable timer if checkpoint creation was not done */
Li Hongfdce8952010-04-10 23:25:39 +08002377 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2378 time_before(jiffies, sci->sc_timer.expires))
2379 add_timer(&sci->sc_timer);
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002380 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002381 spin_unlock(&sci->sc_state_lock);
2382}
2383
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002384/**
2385 * nilfs_segctor_construct - form logs and write them to disk
2386 * @sci: segment constructor object
2387 * @mode: mode of log forming
2388 */
2389static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002390{
2391 struct nilfs_sb_info *sbi = sci->sc_sbi;
2392 struct the_nilfs *nilfs = sbi->s_nilfs;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002393 struct nilfs_super_block **sbp;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002394 int err = 0;
2395
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002396 nilfs_segctor_accept(sci);
2397
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002398 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002399 mode = SC_LSEG_SR;
2400 if (!nilfs_segctor_confirm(sci))
2401 err = nilfs_segctor_do_construct(sci, mode);
2402
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002403 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002404 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002405 atomic_set(&nilfs->ns_ndirtyblks, 0);
2406 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2407 nilfs_discontinued(nilfs)) {
2408 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002409 err = -EIO;
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002410 sbp = nilfs_prepare_super(sbi,
2411 nilfs_sb_will_flip(nilfs));
2412 if (likely(sbp)) {
2413 nilfs_set_log_cursor(sbp[0], nilfs);
2414 err = nilfs_commit_super(sbi, NILFS_SB_COMMIT);
2415 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002416 up_write(&nilfs->ns_sem);
2417 }
2418 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002419
2420 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002421 return err;
2422}
2423
2424static void nilfs_construction_timeout(unsigned long data)
2425{
2426 struct task_struct *p = (struct task_struct *)data;
2427 wake_up_process(p);
2428}
2429
2430static void
2431nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2432{
2433 struct nilfs_inode_info *ii, *n;
2434
2435 list_for_each_entry_safe(ii, n, head, i_dirty) {
2436 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2437 continue;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002438 list_del_init(&ii->i_dirty);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +09002439 iput(&ii->vfs_inode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002440 }
2441}
2442
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002443int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2444 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002445{
2446 struct nilfs_sb_info *sbi = NILFS_SB(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002447 struct the_nilfs *nilfs = sbi->s_nilfs;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002448 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002449 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002450 int err;
2451
2452 if (unlikely(!sci))
2453 return -EROFS;
2454
2455 nilfs_transaction_lock(sbi, &ti, 1);
2456
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002457 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002458 if (unlikely(err))
2459 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002460
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002461 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002462 if (unlikely(err)) {
2463 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002464 goto out_unlock;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002465 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002466
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002467 sci->sc_freesegs = kbufs[4];
2468 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002469 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002470
2471 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002472 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002473 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002474
2475 if (likely(!err))
2476 break;
2477
2478 nilfs_warning(sb, __func__,
2479 "segment construction failed. (err=%d)", err);
2480 set_current_state(TASK_INTERRUPTIBLE);
2481 schedule_timeout(sci->sc_interval);
2482 }
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002483 if (nilfs_test_opt(nilfs, DISCARD)) {
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002484 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2485 sci->sc_nfreesegs);
2486 if (ret) {
2487 printk(KERN_WARNING
2488 "NILFS warning: error %d on discard request, "
2489 "turning discards off for the device\n", ret);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002490 nilfs_clear_opt(nilfs, DISCARD);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002491 }
2492 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002493
2494 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002495 sci->sc_freesegs = NULL;
2496 sci->sc_nfreesegs = 0;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002497 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002498 nilfs_transaction_unlock(sbi);
2499 return err;
2500}
2501
2502static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2503{
2504 struct nilfs_sb_info *sbi = sci->sc_sbi;
2505 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002506
2507 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002508 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002509
2510 /*
2511 * Unclosed segment should be retried. We do this using sc_timer.
2512 * Timeout of sc_timer will invoke complete construction which leads
2513 * to close the current logical segment.
2514 */
2515 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2516 nilfs_segctor_start_timer(sci);
2517
2518 nilfs_transaction_unlock(sbi);
2519}
2520
2521static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2522{
2523 int mode = 0;
2524 int err;
2525
2526 spin_lock(&sci->sc_state_lock);
2527 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2528 SC_FLUSH_DAT : SC_FLUSH_FILE;
2529 spin_unlock(&sci->sc_state_lock);
2530
2531 if (mode) {
2532 err = nilfs_segctor_do_construct(sci, mode);
2533
2534 spin_lock(&sci->sc_state_lock);
2535 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2536 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2537 spin_unlock(&sci->sc_state_lock);
2538 }
2539 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2540}
2541
2542static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2543{
2544 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2545 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2546 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2547 return SC_FLUSH_FILE;
2548 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2549 return SC_FLUSH_DAT;
2550 }
2551 return SC_LSEG_SR;
2552}
2553
2554/**
2555 * nilfs_segctor_thread - main loop of the segment constructor thread.
2556 * @arg: pointer to a struct nilfs_sc_info.
2557 *
2558 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2559 * to execute segment constructions.
2560 */
2561static int nilfs_segctor_thread(void *arg)
2562{
2563 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002564 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002565 int timeout = 0;
2566
Li Hongfdce8952010-04-10 23:25:39 +08002567 sci->sc_timer.data = (unsigned long)current;
2568 sci->sc_timer.function = nilfs_construction_timeout;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002569
2570 /* start sync. */
2571 sci->sc_task = current;
2572 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2573 printk(KERN_INFO
2574 "segctord starting. Construction interval = %lu seconds, "
2575 "CP frequency < %lu seconds\n",
2576 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2577
2578 spin_lock(&sci->sc_state_lock);
2579 loop:
2580 for (;;) {
2581 int mode;
2582
2583 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2584 goto end_thread;
2585
2586 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2587 mode = SC_LSEG_SR;
2588 else if (!sci->sc_flush_request)
2589 break;
2590 else
2591 mode = nilfs_segctor_flush_mode(sci);
2592
2593 spin_unlock(&sci->sc_state_lock);
2594 nilfs_segctor_thread_construct(sci, mode);
2595 spin_lock(&sci->sc_state_lock);
2596 timeout = 0;
2597 }
2598
2599
2600 if (freezing(current)) {
2601 spin_unlock(&sci->sc_state_lock);
2602 refrigerator();
2603 spin_lock(&sci->sc_state_lock);
2604 } else {
2605 DEFINE_WAIT(wait);
2606 int should_sleep = 1;
2607
2608 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2609 TASK_INTERRUPTIBLE);
2610
2611 if (sci->sc_seq_request != sci->sc_seq_done)
2612 should_sleep = 0;
2613 else if (sci->sc_flush_request)
2614 should_sleep = 0;
2615 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2616 should_sleep = time_before(jiffies,
Li Hongfdce8952010-04-10 23:25:39 +08002617 sci->sc_timer.expires);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002618
2619 if (should_sleep) {
2620 spin_unlock(&sci->sc_state_lock);
2621 schedule();
2622 spin_lock(&sci->sc_state_lock);
2623 }
2624 finish_wait(&sci->sc_wait_daemon, &wait);
2625 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
Li Hongfdce8952010-04-10 23:25:39 +08002626 time_after_eq(jiffies, sci->sc_timer.expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002627
2628 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002629 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002630 }
2631 goto loop;
2632
2633 end_thread:
2634 spin_unlock(&sci->sc_state_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002635
2636 /* end sync. */
2637 sci->sc_task = NULL;
2638 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2639 return 0;
2640}
2641
2642static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2643{
2644 struct task_struct *t;
2645
2646 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2647 if (IS_ERR(t)) {
2648 int err = PTR_ERR(t);
2649
2650 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2651 err);
2652 return err;
2653 }
2654 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2655 return 0;
2656}
2657
2658static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
Jiro SEKIBA6b81e142010-10-14 13:52:00 +09002659 __acquires(&sci->sc_state_lock)
2660 __releases(&sci->sc_state_lock)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002661{
2662 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2663
2664 while (sci->sc_task) {
2665 wake_up(&sci->sc_wait_daemon);
2666 spin_unlock(&sci->sc_state_lock);
2667 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2668 spin_lock(&sci->sc_state_lock);
2669 }
2670}
2671
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002672/*
2673 * Setup & clean-up functions
2674 */
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002675static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi,
2676 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002677{
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002678 struct the_nilfs *nilfs = sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002679 struct nilfs_sc_info *sci;
2680
2681 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2682 if (!sci)
2683 return NULL;
2684
2685 sci->sc_sbi = sbi;
2686 sci->sc_super = sbi->s_super;
2687
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002688 nilfs_get_root(root);
2689 sci->sc_root = root;
2690
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002691 init_waitqueue_head(&sci->sc_wait_request);
2692 init_waitqueue_head(&sci->sc_wait_daemon);
2693 init_waitqueue_head(&sci->sc_wait_task);
2694 spin_lock_init(&sci->sc_state_lock);
2695 INIT_LIST_HEAD(&sci->sc_dirty_files);
2696 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002697 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002698 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002699 INIT_LIST_HEAD(&sci->sc_copied_buffers);
Li Hongfdce8952010-04-10 23:25:39 +08002700 init_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002701
2702 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2703 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2704 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2705
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002706 if (nilfs->ns_interval)
2707 sci->sc_interval = nilfs->ns_interval;
2708 if (nilfs->ns_watermark)
2709 sci->sc_watermark = nilfs->ns_watermark;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002710 return sci;
2711}
2712
2713static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2714{
2715 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2716
2717 /* The segctord thread was stopped and its timer was removed.
2718 But some tasks remain. */
2719 do {
2720 struct nilfs_sb_info *sbi = sci->sc_sbi;
2721 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002722
2723 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002724 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002725 nilfs_transaction_unlock(sbi);
2726
2727 } while (ret && retrycount-- > 0);
2728}
2729
2730/**
2731 * nilfs_segctor_destroy - destroy the segment constructor.
2732 * @sci: nilfs_sc_info
2733 *
2734 * nilfs_segctor_destroy() kills the segctord thread and frees
2735 * the nilfs_sc_info struct.
2736 * Caller must hold the segment semaphore.
2737 */
2738static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2739{
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002740 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002741 int flag;
2742
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002743 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002744
2745 spin_lock(&sci->sc_state_lock);
2746 nilfs_segctor_kill_thread(sci);
2747 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2748 || sci->sc_seq_request != sci->sc_seq_done);
2749 spin_unlock(&sci->sc_state_lock);
2750
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002751 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002752 nilfs_segctor_write_out(sci);
2753
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002754 WARN_ON(!list_empty(&sci->sc_copied_buffers));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002755
2756 if (!list_empty(&sci->sc_dirty_files)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002757 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002758 "dirty file(s) after the final construction\n");
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002759 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002760 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002761
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002762 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002763 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002764
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002765 nilfs_put_root(sci->sc_root);
2766
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002767 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002768
Li Hongfdce8952010-04-10 23:25:39 +08002769 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002770 kfree(sci);
2771}
2772
2773/**
2774 * nilfs_attach_segment_constructor - attach a segment constructor
2775 * @sbi: nilfs_sb_info
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002776 * @root: root object of the current filesystem tree
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002777 *
2778 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
Ryusuke Konishi7a650042010-03-14 03:32:40 +09002779 * initializes it, and starts the segment constructor.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002780 *
2781 * Return Value: On success, 0 is returned. On error, one of the following
2782 * negative error code is returned.
2783 *
2784 * %-ENOMEM - Insufficient memory available.
2785 */
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002786int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi,
2787 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002788{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002789 struct the_nilfs *nilfs = sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002790 int err;
2791
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002792 if (nilfs->ns_writer) {
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002793 /*
2794 * This happens if the filesystem was remounted
2795 * read/write after nilfs_error degenerated it into a
2796 * read-only mount.
2797 */
2798 nilfs_detach_segment_constructor(sbi);
2799 }
2800
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002801 nilfs->ns_writer = nilfs_segctor_new(sbi, root);
2802 if (!nilfs->ns_writer)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002803 return -ENOMEM;
2804
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002805 err = nilfs_segctor_start_thread(nilfs->ns_writer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002806 if (err) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002807 kfree(nilfs->ns_writer);
2808 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002809 }
2810 return err;
2811}
2812
2813/**
2814 * nilfs_detach_segment_constructor - destroy the segment constructor
2815 * @sbi: nilfs_sb_info
2816 *
2817 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2818 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2819 */
2820void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2821{
2822 struct the_nilfs *nilfs = sbi->s_nilfs;
2823 LIST_HEAD(garbage_list);
2824
2825 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002826 if (nilfs->ns_writer) {
2827 nilfs_segctor_destroy(nilfs->ns_writer);
2828 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002829 }
2830
2831 /* Force to free the list of dirty files */
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002832 spin_lock(&nilfs->ns_inode_lock);
2833 if (!list_empty(&nilfs->ns_dirty_files)) {
2834 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002835 nilfs_warning(sbi->s_super, __func__,
2836 "Non empty dirty list after the last "
2837 "segment construction\n");
2838 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002839 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002840 up_write(&nilfs->ns_segctor_sem);
2841
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002842 nilfs_dispose_list(nilfs, &garbage_list, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002843}