blob: 90e3130303a3328f695e68bbbf9d91aae7da65d1 [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{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700184 struct the_nilfs *nilfs;
185 int ret = nilfs_prepare_segment_lock(ti);
186
187 if (unlikely(ret < 0))
188 return ret;
189 if (ret > 0)
190 return 0;
191
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900192 vfs_check_frozen(sb, SB_FREEZE_WRITE);
193
Ryusuke Konishif7545142011-03-09 11:05:08 +0900194 nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700195 down_read(&nilfs->ns_segctor_sem);
196 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
197 up_read(&nilfs->ns_segctor_sem);
198 ret = -ENOSPC;
199 goto failed;
200 }
201 return 0;
202
203 failed:
204 ti = current->journal_info;
205 current->journal_info = ti->ti_save;
206 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
207 kmem_cache_free(nilfs_transaction_cachep, ti);
208 return ret;
209}
210
211/**
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700212 * nilfs_transaction_commit - commit indivisible file operations.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700213 * @sb: super block
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700214 *
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700215 * nilfs_transaction_commit() releases the read semaphore which is
216 * acquired by nilfs_transaction_begin(). This is only performed
217 * in outermost call of this function. If a commit flag is set,
218 * nilfs_transaction_commit() sets a timer to start the segment
219 * constructor. If a sync flag is set, it starts construction
220 * directly.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700221 */
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700222int nilfs_transaction_commit(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700223{
224 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900225 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700226 int err = 0;
227
228 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700229 ti->ti_flags |= NILFS_TI_COMMIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700230 if (ti->ti_count > 0) {
231 ti->ti_count--;
232 return 0;
233 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900234 if (nilfs->ns_writer) {
235 struct nilfs_sc_info *sci = nilfs->ns_writer;
236
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700237 if (ti->ti_flags & NILFS_TI_COMMIT)
238 nilfs_segctor_start_timer(sci);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900239 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700240 nilfs_segctor_do_flush(sci, 0);
241 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900242 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700243 current->journal_info = ti->ti_save;
244
245 if (ti->ti_flags & NILFS_TI_SYNC)
246 err = nilfs_construct_segment(sb);
247 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
248 kmem_cache_free(nilfs_transaction_cachep, ti);
249 return err;
250}
251
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700252void nilfs_transaction_abort(struct super_block *sb)
253{
254 struct nilfs_transaction_info *ti = current->journal_info;
255
256 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
257 if (ti->ti_count > 0) {
258 ti->ti_count--;
259 return;
260 }
261 up_read(&NILFS_SB(sb)->s_nilfs->ns_segctor_sem);
262
263 current->journal_info = ti->ti_save;
264 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
265 kmem_cache_free(nilfs_transaction_cachep, ti);
266}
267
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700268void nilfs_relax_pressure_in_lock(struct super_block *sb)
269{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900270 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
271 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700272
273 if (!sci || !sci->sc_flush_request)
274 return;
275
276 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
277 up_read(&nilfs->ns_segctor_sem);
278
279 down_write(&nilfs->ns_segctor_sem);
280 if (sci->sc_flush_request &&
281 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
282 struct nilfs_transaction_info *ti = current->journal_info;
283
284 ti->ti_flags |= NILFS_TI_WRITER;
285 nilfs_segctor_do_immediate_flush(sci);
286 ti->ti_flags &= ~NILFS_TI_WRITER;
287 }
288 downgrade_write(&nilfs->ns_segctor_sem);
289}
290
Ryusuke Konishif7545142011-03-09 11:05:08 +0900291static void nilfs_transaction_lock(struct super_block *sb,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700292 struct nilfs_transaction_info *ti,
293 int gcflag)
294{
295 struct nilfs_transaction_info *cur_ti = current->journal_info;
Ryusuke Konishif7545142011-03-09 11:05:08 +0900296 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900297 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700298
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700299 WARN_ON(cur_ti);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700300 ti->ti_flags = NILFS_TI_WRITER;
301 ti->ti_count = 0;
302 ti->ti_save = cur_ti;
303 ti->ti_magic = NILFS_TI_MAGIC;
304 INIT_LIST_HEAD(&ti->ti_garbage);
305 current->journal_info = ti;
306
307 for (;;) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900308 down_write(&nilfs->ns_segctor_sem);
309 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700310 break;
311
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900312 nilfs_segctor_do_immediate_flush(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700313
Ryusuke Konishif7545142011-03-09 11:05:08 +0900314 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700315 yield();
316 }
317 if (gcflag)
318 ti->ti_flags |= NILFS_TI_GC;
319}
320
Ryusuke Konishif7545142011-03-09 11:05:08 +0900321static void nilfs_transaction_unlock(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700322{
323 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishif7545142011-03-09 11:05:08 +0900324 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700325
326 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
327 BUG_ON(ti->ti_count > 0);
328
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900329 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700330 current->journal_info = ti->ti_save;
331 if (!list_empty(&ti->ti_garbage))
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900332 nilfs_dispose_list(nilfs, &ti->ti_garbage, 0);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700333}
334
335static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
336 struct nilfs_segsum_pointer *ssp,
337 unsigned bytes)
338{
339 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
340 unsigned blocksize = sci->sc_super->s_blocksize;
341 void *p;
342
343 if (unlikely(ssp->offset + bytes > blocksize)) {
344 ssp->offset = 0;
345 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
346 &segbuf->sb_segsum_buffers));
347 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
348 }
349 p = ssp->bh->b_data + ssp->offset;
350 ssp->offset += bytes;
351 return p;
352}
353
354/**
355 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
356 * @sci: nilfs_sc_info
357 */
358static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
359{
360 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
361 struct buffer_head *sumbh;
362 unsigned sumbytes;
363 unsigned flags = 0;
364 int err;
365
366 if (nilfs_doing_gc())
367 flags = NILFS_SS_GC;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900368 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700369 if (unlikely(err))
370 return err;
371
372 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
373 sumbytes = segbuf->sb_sum.sumbytes;
374 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
375 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
376 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
377 return 0;
378}
379
380static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
381{
382 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
383 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
384 return -E2BIG; /* The current segment is filled up
385 (internal code) */
386 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
387 return nilfs_segctor_reset_segment_buffer(sci);
388}
389
390static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
391{
392 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
393 int err;
394
395 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
396 err = nilfs_segctor_feed_segment(sci);
397 if (err)
398 return err;
399 segbuf = sci->sc_curseg;
400 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900401 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700402 if (likely(!err))
403 segbuf->sb_sum.flags |= NILFS_SS_SR;
404 return err;
405}
406
407/*
408 * Functions for making segment summary and payloads
409 */
410static int nilfs_segctor_segsum_block_required(
411 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
412 unsigned binfo_size)
413{
414 unsigned blocksize = sci->sc_super->s_blocksize;
415 /* Size of finfo and binfo is enough small against blocksize */
416
417 return ssp->offset + binfo_size +
418 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
419 blocksize;
420}
421
422static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
423 struct inode *inode)
424{
425 sci->sc_curseg->sb_sum.nfinfo++;
426 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
427 nilfs_segctor_map_segsum_entry(
428 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700429
Ryusuke Konishi72746ac2011-02-28 13:41:11 +0900430 if (NILFS_I(inode)->i_root &&
431 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700432 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700433 /* skip finfo */
434}
435
436static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
437 struct inode *inode)
438{
439 struct nilfs_finfo *finfo;
440 struct nilfs_inode_info *ii;
441 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900442 __u64 cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700443
444 if (sci->sc_blk_cnt == 0)
445 return;
446
447 ii = NILFS_I(inode);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900448
449 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
450 cno = ii->i_cno;
451 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
452 cno = 0;
453 else
454 cno = sci->sc_cno;
455
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700456 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
457 sizeof(*finfo));
458 finfo->fi_ino = cpu_to_le64(inode->i_ino);
459 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
460 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900461 finfo->fi_cno = cpu_to_le64(cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700462
463 segbuf = sci->sc_curseg;
464 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
465 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
466 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
467 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
468}
469
470static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
471 struct buffer_head *bh,
472 struct inode *inode,
473 unsigned binfo_size)
474{
475 struct nilfs_segment_buffer *segbuf;
476 int required, err = 0;
477
478 retry:
479 segbuf = sci->sc_curseg;
480 required = nilfs_segctor_segsum_block_required(
481 sci, &sci->sc_binfo_ptr, binfo_size);
482 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
483 nilfs_segctor_end_finfo(sci, inode);
484 err = nilfs_segctor_feed_segment(sci);
485 if (err)
486 return err;
487 goto retry;
488 }
489 if (unlikely(required)) {
490 err = nilfs_segbuf_extend_segsum(segbuf);
491 if (unlikely(err))
492 goto failed;
493 }
494 if (sci->sc_blk_cnt == 0)
495 nilfs_segctor_begin_finfo(sci, inode);
496
497 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
498 /* Substitution to vblocknr is delayed until update_blocknr() */
499 nilfs_segbuf_add_file_buffer(segbuf, bh);
500 sci->sc_blk_cnt++;
501 failed:
502 return err;
503}
504
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700505/*
506 * Callback functions that enumerate, mark, and collect dirty blocks
507 */
508static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
509 struct buffer_head *bh, struct inode *inode)
510{
511 int err;
512
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700513 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900514 if (err < 0)
515 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700516
517 err = nilfs_segctor_add_file_block(sci, bh, inode,
518 sizeof(struct nilfs_binfo_v));
519 if (!err)
520 sci->sc_datablk_cnt++;
521 return err;
522}
523
524static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
525 struct buffer_head *bh,
526 struct inode *inode)
527{
Ryusuke Konishie8289492010-11-19 15:26:20 +0900528 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700529}
530
531static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
532 struct buffer_head *bh,
533 struct inode *inode)
534{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700535 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700536 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
537}
538
539static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
540 struct nilfs_segsum_pointer *ssp,
541 union nilfs_binfo *binfo)
542{
543 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
544 sci, ssp, sizeof(*binfo_v));
545 *binfo_v = binfo->bi_v;
546}
547
548static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
549 struct nilfs_segsum_pointer *ssp,
550 union nilfs_binfo *binfo)
551{
552 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
553 sci, ssp, sizeof(*vblocknr));
554 *vblocknr = binfo->bi_v.bi_vblocknr;
555}
556
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900557static struct nilfs_sc_operations nilfs_sc_file_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700558 .collect_data = nilfs_collect_file_data,
559 .collect_node = nilfs_collect_file_node,
560 .collect_bmap = nilfs_collect_file_bmap,
561 .write_data_binfo = nilfs_write_file_data_binfo,
562 .write_node_binfo = nilfs_write_file_node_binfo,
563};
564
565static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
566 struct buffer_head *bh, struct inode *inode)
567{
568 int err;
569
570 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900571 if (err < 0)
572 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700573
574 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
575 if (!err)
576 sci->sc_datablk_cnt++;
577 return err;
578}
579
580static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
581 struct buffer_head *bh, struct inode *inode)
582{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700583 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700584 return nilfs_segctor_add_file_block(sci, bh, inode,
585 sizeof(struct nilfs_binfo_dat));
586}
587
588static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
589 struct nilfs_segsum_pointer *ssp,
590 union nilfs_binfo *binfo)
591{
592 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
593 sizeof(*blkoff));
594 *blkoff = binfo->bi_dat.bi_blkoff;
595}
596
597static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
598 struct nilfs_segsum_pointer *ssp,
599 union nilfs_binfo *binfo)
600{
601 struct nilfs_binfo_dat *binfo_dat =
602 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
603 *binfo_dat = binfo->bi_dat;
604}
605
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900606static struct nilfs_sc_operations nilfs_sc_dat_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700607 .collect_data = nilfs_collect_dat_data,
608 .collect_node = nilfs_collect_file_node,
609 .collect_bmap = nilfs_collect_dat_bmap,
610 .write_data_binfo = nilfs_write_dat_data_binfo,
611 .write_node_binfo = nilfs_write_dat_node_binfo,
612};
613
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900614static struct nilfs_sc_operations nilfs_sc_dsync_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700615 .collect_data = nilfs_collect_file_data,
616 .collect_node = NULL,
617 .collect_bmap = NULL,
618 .write_data_binfo = nilfs_write_file_data_binfo,
619 .write_node_binfo = NULL,
620};
621
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700622static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
623 struct list_head *listp,
624 size_t nlimit,
625 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700626{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700627 struct address_space *mapping = inode->i_mapping;
628 struct pagevec pvec;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700629 pgoff_t index = 0, last = ULONG_MAX;
630 size_t ndirties = 0;
631 int i;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700632
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700633 if (unlikely(start != 0 || end != LLONG_MAX)) {
634 /*
635 * A valid range is given for sync-ing data pages. The
636 * range is rounded to per-page; extra dirty buffers
637 * may be included if blocksize < pagesize.
638 */
639 index = start >> PAGE_SHIFT;
640 last = end >> PAGE_SHIFT;
641 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700642 pagevec_init(&pvec, 0);
643 repeat:
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700644 if (unlikely(index > last) ||
645 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
646 min_t(pgoff_t, last - index,
647 PAGEVEC_SIZE - 1) + 1))
648 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700649
650 for (i = 0; i < pagevec_count(&pvec); i++) {
651 struct buffer_head *bh, *head;
652 struct page *page = pvec.pages[i];
653
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700654 if (unlikely(page->index > last))
655 break;
656
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700657 if (mapping->host) {
658 lock_page(page);
659 if (!page_has_buffers(page))
660 create_empty_buffers(page,
661 1 << inode->i_blkbits, 0);
662 unlock_page(page);
663 }
664
665 bh = head = page_buffers(page);
666 do {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700667 if (!buffer_dirty(bh))
668 continue;
669 get_bh(bh);
670 list_add_tail(&bh->b_assoc_buffers, listp);
671 ndirties++;
672 if (unlikely(ndirties >= nlimit)) {
673 pagevec_release(&pvec);
674 cond_resched();
675 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700676 }
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700677 } while (bh = bh->b_this_page, bh != head);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700678 }
679 pagevec_release(&pvec);
680 cond_resched();
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700681 goto repeat;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700682}
683
684static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
685 struct list_head *listp)
686{
687 struct nilfs_inode_info *ii = NILFS_I(inode);
688 struct address_space *mapping = &ii->i_btnode_cache;
689 struct pagevec pvec;
690 struct buffer_head *bh, *head;
691 unsigned int i;
692 pgoff_t index = 0;
693
694 pagevec_init(&pvec, 0);
695
696 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
697 PAGEVEC_SIZE)) {
698 for (i = 0; i < pagevec_count(&pvec); i++) {
699 bh = head = page_buffers(pvec.pages[i]);
700 do {
701 if (buffer_dirty(bh)) {
702 get_bh(bh);
703 list_add_tail(&bh->b_assoc_buffers,
704 listp);
705 }
706 bh = bh->b_this_page;
707 } while (bh != head);
708 }
709 pagevec_release(&pvec);
710 cond_resched();
711 }
712}
713
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900714static void nilfs_dispose_list(struct the_nilfs *nilfs,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700715 struct list_head *head, int force)
716{
717 struct nilfs_inode_info *ii, *n;
718 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
719 unsigned nv = 0;
720
721 while (!list_empty(head)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900722 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700723 list_for_each_entry_safe(ii, n, head, i_dirty) {
724 list_del_init(&ii->i_dirty);
725 if (force) {
726 if (unlikely(ii->i_bh)) {
727 brelse(ii->i_bh);
728 ii->i_bh = NULL;
729 }
730 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
731 set_bit(NILFS_I_QUEUED, &ii->i_state);
732 list_add_tail(&ii->i_dirty,
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900733 &nilfs->ns_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700734 continue;
735 }
736 ivec[nv++] = ii;
737 if (nv == SC_N_INODEVEC)
738 break;
739 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900740 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700741
742 for (pii = ivec; nv > 0; pii++, nv--)
743 iput(&(*pii)->vfs_inode);
744 }
745}
746
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900747static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
748 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700749{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700750 int ret = 0;
751
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900752 if (nilfs_mdt_fetch_dirty(root->ifile))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700753 ret++;
754 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
755 ret++;
756 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
757 ret++;
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900758 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
759 ret++;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700760 return ret;
761}
762
763static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
764{
765 return list_empty(&sci->sc_dirty_files) &&
766 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900767 sci->sc_nfreesegs == 0 &&
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700768 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
769}
770
771static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
772{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +0900773 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700774 int ret = 0;
775
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900776 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700777 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
778
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900779 spin_lock(&nilfs->ns_inode_lock);
780 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700781 ret++;
782
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900783 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700784 return ret;
785}
786
787static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
788{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +0900789 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700790
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900791 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700792 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
793 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900794 nilfs_mdt_clear_dirty(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700795}
796
797static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
798{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +0900799 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700800 struct buffer_head *bh_cp;
801 struct nilfs_checkpoint *raw_cp;
802 int err;
803
804 /* XXX: this interface will be changed */
805 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
806 &raw_cp, &bh_cp);
807 if (likely(!err)) {
808 /* The following code is duplicated with cpfile. But, it is
809 needed to collect the checkpoint even if it was not newly
810 created */
811 nilfs_mdt_mark_buffer_dirty(bh_cp);
812 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
813 nilfs_cpfile_put_checkpoint(
814 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700815 } else
816 WARN_ON(err == -EINVAL || err == -ENOENT);
817
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700818 return err;
819}
820
821static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
822{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +0900823 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700824 struct buffer_head *bh_cp;
825 struct nilfs_checkpoint *raw_cp;
826 int err;
827
828 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
829 &raw_cp, &bh_cp);
830 if (unlikely(err)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700831 WARN_ON(err == -EINVAL || err == -ENOENT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700832 goto failed_ibh;
833 }
834 raw_cp->cp_snapshot_list.ssl_next = 0;
835 raw_cp->cp_snapshot_list.ssl_prev = 0;
836 raw_cp->cp_inodes_count =
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900837 cpu_to_le64(atomic_read(&sci->sc_root->inodes_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700838 raw_cp->cp_blocks_count =
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900839 cpu_to_le64(atomic_read(&sci->sc_root->blocks_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700840 raw_cp->cp_nblk_inc =
841 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
842 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
843 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700844
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700845 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
846 nilfs_checkpoint_clear_minor(raw_cp);
847 else
848 nilfs_checkpoint_set_minor(raw_cp);
849
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900850 nilfs_write_inode_common(sci->sc_root->ifile,
851 &raw_cp->cp_ifile_inode, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700852 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
853 return 0;
854
855 failed_ibh:
856 return err;
857}
858
859static void nilfs_fill_in_file_bmap(struct inode *ifile,
860 struct nilfs_inode_info *ii)
861
862{
863 struct buffer_head *ibh;
864 struct nilfs_inode *raw_inode;
865
866 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
867 ibh = ii->i_bh;
868 BUG_ON(!ibh);
869 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
870 ibh);
871 nilfs_bmap_write(ii->i_bmap, raw_inode);
872 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
873 }
874}
875
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900876static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700877{
878 struct nilfs_inode_info *ii;
879
880 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900881 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700882 set_bit(NILFS_I_COLLECTED, &ii->i_state);
883 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700884}
885
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700886static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
887 struct the_nilfs *nilfs)
888{
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900889 struct buffer_head *bh_sr;
890 struct nilfs_super_root *raw_sr;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700891 unsigned isz = nilfs->ns_inode_size;
892
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900893 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
894 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
895
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700896 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
897 raw_sr->sr_nongc_ctime
898 = cpu_to_le64(nilfs_doing_gc() ?
899 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
900 raw_sr->sr_flags = 0;
901
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900902 nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr +
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900903 NILFS_SR_DAT_OFFSET(isz), 1);
904 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
905 NILFS_SR_CPFILE_OFFSET(isz), 1);
906 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
907 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700908}
909
910static void nilfs_redirty_inodes(struct list_head *head)
911{
912 struct nilfs_inode_info *ii;
913
914 list_for_each_entry(ii, head, i_dirty) {
915 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
916 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
917 }
918}
919
920static void nilfs_drop_collected_inodes(struct list_head *head)
921{
922 struct nilfs_inode_info *ii;
923
924 list_for_each_entry(ii, head, i_dirty) {
925 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
926 continue;
927
928 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
929 set_bit(NILFS_I_UPDATED, &ii->i_state);
930 }
931}
932
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700933static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
934 struct inode *inode,
935 struct list_head *listp,
936 int (*collect)(struct nilfs_sc_info *,
937 struct buffer_head *,
938 struct inode *))
939{
940 struct buffer_head *bh, *n;
941 int err = 0;
942
943 if (collect) {
944 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
945 list_del_init(&bh->b_assoc_buffers);
946 err = collect(sci, bh, inode);
947 brelse(bh);
948 if (unlikely(err))
949 goto dispose_buffers;
950 }
951 return 0;
952 }
953
954 dispose_buffers:
955 while (!list_empty(listp)) {
956 bh = list_entry(listp->next, struct buffer_head,
957 b_assoc_buffers);
958 list_del_init(&bh->b_assoc_buffers);
959 brelse(bh);
960 }
961 return err;
962}
963
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700964static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
965{
966 /* Remaining number of blocks within segment buffer */
967 return sci->sc_segbuf_nblocks -
968 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
969}
970
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700971static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
972 struct inode *inode,
973 struct nilfs_sc_operations *sc_ops)
974{
975 LIST_HEAD(data_buffers);
976 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700977 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700978
979 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700980 size_t n, rest = nilfs_segctor_buffer_rest(sci);
981
982 n = nilfs_lookup_dirty_data_buffers(
983 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
984 if (n > rest) {
985 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700986 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700987 sc_ops->collect_data);
988 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700989 goto break_or_fail;
990 }
991 }
992 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
993
994 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
995 err = nilfs_segctor_apply_buffers(
996 sci, inode, &data_buffers, sc_ops->collect_data);
997 if (unlikely(err)) {
998 /* dispose node list */
999 nilfs_segctor_apply_buffers(
1000 sci, inode, &node_buffers, NULL);
1001 goto break_or_fail;
1002 }
1003 sci->sc_stage.flags |= NILFS_CF_NODE;
1004 }
1005 /* Collect node */
1006 err = nilfs_segctor_apply_buffers(
1007 sci, inode, &node_buffers, sc_ops->collect_node);
1008 if (unlikely(err))
1009 goto break_or_fail;
1010
1011 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1012 err = nilfs_segctor_apply_buffers(
1013 sci, inode, &node_buffers, sc_ops->collect_bmap);
1014 if (unlikely(err))
1015 goto break_or_fail;
1016
1017 nilfs_segctor_end_finfo(sci, inode);
1018 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1019
1020 break_or_fail:
1021 return err;
1022}
1023
1024static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1025 struct inode *inode)
1026{
1027 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001028 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1029 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001030
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001031 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1032 sci->sc_dsync_start,
1033 sci->sc_dsync_end);
1034
1035 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1036 nilfs_collect_file_data);
1037 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001038 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001039 BUG_ON(n > rest);
1040 /* always receive -E2BIG or true error if n > rest */
1041 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001042 return err;
1043}
1044
1045static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1046{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +09001047 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001048 struct list_head *head;
1049 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001050 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001051 int err = 0;
1052
1053 switch (sci->sc_stage.scnt) {
1054 case NILFS_ST_INIT:
1055 /* Pre-processes */
1056 sci->sc_stage.flags = 0;
1057
1058 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1059 sci->sc_nblk_inc = 0;
1060 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1061 if (mode == SC_LSEG_DSYNC) {
1062 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1063 goto dsync_mode;
1064 }
1065 }
1066
1067 sci->sc_stage.dirty_file_ptr = NULL;
1068 sci->sc_stage.gc_inode_ptr = NULL;
1069 if (mode == SC_FLUSH_DAT) {
1070 sci->sc_stage.scnt = NILFS_ST_DAT;
1071 goto dat_stage;
1072 }
1073 sci->sc_stage.scnt++; /* Fall through */
1074 case NILFS_ST_GC:
1075 if (nilfs_doing_gc()) {
1076 head = &sci->sc_gc_inodes;
1077 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1078 head, i_dirty);
1079 list_for_each_entry_continue(ii, head, i_dirty) {
1080 err = nilfs_segctor_scan_file(
1081 sci, &ii->vfs_inode,
1082 &nilfs_sc_file_ops);
1083 if (unlikely(err)) {
1084 sci->sc_stage.gc_inode_ptr = list_entry(
1085 ii->i_dirty.prev,
1086 struct nilfs_inode_info,
1087 i_dirty);
1088 goto break_or_fail;
1089 }
1090 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1091 }
1092 sci->sc_stage.gc_inode_ptr = NULL;
1093 }
1094 sci->sc_stage.scnt++; /* Fall through */
1095 case NILFS_ST_FILE:
1096 head = &sci->sc_dirty_files;
1097 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1098 i_dirty);
1099 list_for_each_entry_continue(ii, head, i_dirty) {
1100 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1101
1102 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1103 &nilfs_sc_file_ops);
1104 if (unlikely(err)) {
1105 sci->sc_stage.dirty_file_ptr =
1106 list_entry(ii->i_dirty.prev,
1107 struct nilfs_inode_info,
1108 i_dirty);
1109 goto break_or_fail;
1110 }
1111 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1112 /* XXX: required ? */
1113 }
1114 sci->sc_stage.dirty_file_ptr = NULL;
1115 if (mode == SC_FLUSH_FILE) {
1116 sci->sc_stage.scnt = NILFS_ST_DONE;
1117 return 0;
1118 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001119 sci->sc_stage.scnt++;
1120 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1121 /* Fall through */
1122 case NILFS_ST_IFILE:
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001123 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001124 &nilfs_sc_file_ops);
1125 if (unlikely(err))
1126 break;
1127 sci->sc_stage.scnt++;
1128 /* Creating a checkpoint */
1129 err = nilfs_segctor_create_checkpoint(sci);
1130 if (unlikely(err))
1131 break;
1132 /* Fall through */
1133 case NILFS_ST_CPFILE:
1134 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1135 &nilfs_sc_file_ops);
1136 if (unlikely(err))
1137 break;
1138 sci->sc_stage.scnt++; /* Fall through */
1139 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001140 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1141 sci->sc_nfreesegs, &ndone);
1142 if (unlikely(err)) {
1143 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1144 sci->sc_freesegs, ndone,
1145 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001146 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001147 }
1148 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1149
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001150 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1151 &nilfs_sc_file_ops);
1152 if (unlikely(err))
1153 break;
1154 sci->sc_stage.scnt++; /* Fall through */
1155 case NILFS_ST_DAT:
1156 dat_stage:
Ryusuke Konishi365e2152010-12-27 00:07:30 +09001157 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001158 &nilfs_sc_dat_ops);
1159 if (unlikely(err))
1160 break;
1161 if (mode == SC_FLUSH_DAT) {
1162 sci->sc_stage.scnt = NILFS_ST_DONE;
1163 return 0;
1164 }
1165 sci->sc_stage.scnt++; /* Fall through */
1166 case NILFS_ST_SR:
1167 if (mode == SC_LSEG_SR) {
1168 /* Appending a super root */
1169 err = nilfs_segctor_add_super_root(sci);
1170 if (unlikely(err))
1171 break;
1172 }
1173 /* End of a logical segment */
1174 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1175 sci->sc_stage.scnt = NILFS_ST_DONE;
1176 return 0;
1177 case NILFS_ST_DSYNC:
1178 dsync_mode:
1179 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001180 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001181 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1182 break;
1183
1184 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1185 if (unlikely(err))
1186 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001187 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1188 sci->sc_stage.scnt = NILFS_ST_DONE;
1189 return 0;
1190 case NILFS_ST_DONE:
1191 return 0;
1192 default:
1193 BUG();
1194 }
1195
1196 break_or_fail:
1197 return err;
1198}
1199
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001200/**
1201 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1202 * @sci: nilfs_sc_info
1203 * @nilfs: nilfs object
1204 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001205static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1206 struct the_nilfs *nilfs)
1207{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001208 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001209 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001210 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001211
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001212 segbuf = nilfs_segbuf_new(sci->sc_super);
1213 if (unlikely(!segbuf))
1214 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001215
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001216 if (list_empty(&sci->sc_write_logs)) {
1217 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1218 nilfs->ns_pseg_offset, nilfs);
1219 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1220 nilfs_shift_to_next_segment(nilfs);
1221 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1222 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001223
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001224 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001225 nextnum = nilfs->ns_nextnum;
1226
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001227 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1228 /* Start from the head of a new full segment */
1229 alloc++;
1230 } else {
1231 /* Continue logs */
1232 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1233 nilfs_segbuf_map_cont(segbuf, prev);
1234 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1235 nextnum = prev->sb_nextnum;
1236
1237 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1238 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1239 segbuf->sb_sum.seg_seq++;
1240 alloc++;
1241 }
1242 }
1243
1244 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1245 if (err)
1246 goto failed;
1247
1248 if (alloc) {
1249 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1250 if (err)
1251 goto failed;
1252 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001253 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1254
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001255 BUG_ON(!list_empty(&sci->sc_segbufs));
1256 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1257 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001258 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001259
1260 failed:
1261 nilfs_segbuf_free(segbuf);
1262 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001263}
1264
1265static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1266 struct the_nilfs *nilfs, int nadd)
1267{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001268 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001269 struct inode *sufile = nilfs->ns_sufile;
1270 __u64 nextnextnum;
1271 LIST_HEAD(list);
1272 int err, ret, i;
1273
1274 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1275 /*
1276 * Since the segment specified with nextnum might be allocated during
1277 * the previous construction, the buffer including its segusage may
1278 * not be dirty. The following call ensures that the buffer is dirty
1279 * and will pin the buffer on memory until the sufile is written.
1280 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001281 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001282 if (unlikely(err))
1283 return err;
1284
1285 for (i = 0; i < nadd; i++) {
1286 /* extend segment info */
1287 err = -ENOMEM;
1288 segbuf = nilfs_segbuf_new(sci->sc_super);
1289 if (unlikely(!segbuf))
1290 goto failed;
1291
1292 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001293 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001294 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1295
1296 /* allocate the next next full segment */
1297 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1298 if (unlikely(err))
1299 goto failed_segbuf;
1300
1301 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1302 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1303
1304 list_add_tail(&segbuf->sb_list, &list);
1305 prev = segbuf;
1306 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001307 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001308 return 0;
1309
1310 failed_segbuf:
1311 nilfs_segbuf_free(segbuf);
1312 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001313 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001314 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001315 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001316 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001317 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001318 return err;
1319}
1320
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001321static void nilfs_free_incomplete_logs(struct list_head *logs,
1322 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001323{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001324 struct nilfs_segment_buffer *segbuf, *prev;
1325 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001326 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001327
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001328 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001329 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001330 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001331 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001332 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001333 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001334 /* Case 1: The first segment failed */
1335 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1336 /* Case 1a: Partial segment appended into an existing
1337 segment */
1338 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1339 segbuf->sb_fseg_end);
1340 else /* Case 1b: New full segment */
1341 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001342 }
1343
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001344 prev = segbuf;
1345 list_for_each_entry_continue(segbuf, logs, sb_list) {
1346 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1347 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1348 WARN_ON(ret); /* never fails */
1349 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001350 if (atomic_read(&segbuf->sb_err) &&
1351 segbuf->sb_segnum != nilfs->ns_nextnum)
1352 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001353 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1354 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001355 }
1356}
1357
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001358static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1359 struct inode *sufile)
1360{
1361 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001362 unsigned long live_blocks;
1363 int ret;
1364
1365 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001366 live_blocks = segbuf->sb_sum.nblocks +
1367 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001368 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1369 live_blocks,
1370 sci->sc_seg_ctime);
1371 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001372 }
1373}
1374
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001375static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001376{
1377 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001378 int ret;
1379
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001380 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001381 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1382 segbuf->sb_pseg_start -
1383 segbuf->sb_fseg_start, 0);
1384 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001385
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001386 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001387 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1388 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001389 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001390 }
1391}
1392
1393static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1394 struct nilfs_segment_buffer *last,
1395 struct inode *sufile)
1396{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001397 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001398 int ret;
1399
Ryusuke Konishie29df392009-11-29 16:51:16 +09001400 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001401 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1402 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001403 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001404 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001405 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001406}
1407
1408
1409static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1410 struct the_nilfs *nilfs, int mode)
1411{
1412 struct nilfs_cstage prev_stage = sci->sc_stage;
1413 int err, nadd = 1;
1414
1415 /* Collection retry loop */
1416 for (;;) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001417 sci->sc_nblk_this_inc = 0;
1418 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1419
1420 err = nilfs_segctor_reset_segment_buffer(sci);
1421 if (unlikely(err))
1422 goto failed;
1423
1424 err = nilfs_segctor_collect_blocks(sci, mode);
1425 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1426 if (!err)
1427 break;
1428
1429 if (unlikely(err != -E2BIG))
1430 goto failed;
1431
1432 /* The current segment is filled up */
1433 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1434 break;
1435
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001436 nilfs_clear_logs(&sci->sc_segbufs);
1437
1438 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1439 if (unlikely(err))
1440 return err;
1441
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001442 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1443 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1444 sci->sc_freesegs,
1445 sci->sc_nfreesegs,
1446 NULL);
1447 WARN_ON(err); /* do not happen */
1448 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001449 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1450 sci->sc_stage = prev_stage;
1451 }
1452 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1453 return 0;
1454
1455 failed:
1456 return err;
1457}
1458
1459static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1460 struct buffer_head *new_bh)
1461{
1462 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1463
1464 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1465 /* The caller must release old_bh */
1466}
1467
1468static int
1469nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1470 struct nilfs_segment_buffer *segbuf,
1471 int mode)
1472{
1473 struct inode *inode = NULL;
1474 sector_t blocknr;
1475 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1476 unsigned long nblocks = 0, ndatablk = 0;
1477 struct nilfs_sc_operations *sc_op = NULL;
1478 struct nilfs_segsum_pointer ssp;
1479 struct nilfs_finfo *finfo = NULL;
1480 union nilfs_binfo binfo;
1481 struct buffer_head *bh, *bh_org;
1482 ino_t ino = 0;
1483 int err = 0;
1484
1485 if (!nfinfo)
1486 goto out;
1487
1488 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1489 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1490 ssp.offset = sizeof(struct nilfs_segment_summary);
1491
1492 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001493 if (bh == segbuf->sb_super_root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001494 break;
1495 if (!finfo) {
1496 finfo = nilfs_segctor_map_segsum_entry(
1497 sci, &ssp, sizeof(*finfo));
1498 ino = le64_to_cpu(finfo->fi_ino);
1499 nblocks = le32_to_cpu(finfo->fi_nblocks);
1500 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1501
1502 if (buffer_nilfs_node(bh))
1503 inode = NILFS_BTNC_I(bh->b_page->mapping);
1504 else
1505 inode = NILFS_AS_I(bh->b_page->mapping);
1506
1507 if (mode == SC_LSEG_DSYNC)
1508 sc_op = &nilfs_sc_dsync_ops;
1509 else if (ino == NILFS_DAT_INO)
1510 sc_op = &nilfs_sc_dat_ops;
1511 else /* file blocks */
1512 sc_op = &nilfs_sc_file_ops;
1513 }
1514 bh_org = bh;
1515 get_bh(bh_org);
1516 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1517 &binfo);
1518 if (bh != bh_org)
1519 nilfs_list_replace_buffer(bh_org, bh);
1520 brelse(bh_org);
1521 if (unlikely(err))
1522 goto failed_bmap;
1523
1524 if (ndatablk > 0)
1525 sc_op->write_data_binfo(sci, &ssp, &binfo);
1526 else
1527 sc_op->write_node_binfo(sci, &ssp, &binfo);
1528
1529 blocknr++;
1530 if (--nblocks == 0) {
1531 finfo = NULL;
1532 if (--nfinfo == 0)
1533 break;
1534 } else if (ndatablk > 0)
1535 ndatablk--;
1536 }
1537 out:
1538 return 0;
1539
1540 failed_bmap:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001541 return err;
1542}
1543
1544static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1545{
1546 struct nilfs_segment_buffer *segbuf;
1547 int err;
1548
1549 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1550 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1551 if (unlikely(err))
1552 return err;
1553 nilfs_segbuf_fill_in_segsum(segbuf);
1554 }
1555 return 0;
1556}
1557
1558static int
1559nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1560{
1561 struct page *clone_page;
1562 struct buffer_head *bh, *head, *bh2;
1563 void *kaddr;
1564
1565 bh = head = page_buffers(page);
1566
1567 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1568 if (unlikely(!clone_page))
1569 return -ENOMEM;
1570
1571 bh2 = page_buffers(clone_page);
1572 kaddr = kmap_atomic(page, KM_USER0);
1573 do {
1574 if (list_empty(&bh->b_assoc_buffers))
1575 continue;
1576 get_bh(bh2);
1577 page_cache_get(clone_page); /* for each bh */
1578 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1579 bh2->b_blocknr = bh->b_blocknr;
1580 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1581 list_add_tail(&bh->b_assoc_buffers, out);
1582 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1583 kunmap_atomic(kaddr, KM_USER0);
1584
1585 if (!TestSetPageWriteback(clone_page))
Michael Rubinf629d1c2010-10-26 14:21:33 -07001586 account_page_writeback(clone_page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001587 unlock_page(clone_page);
1588
1589 return 0;
1590}
1591
1592static int nilfs_test_page_to_be_frozen(struct page *page)
1593{
1594 struct address_space *mapping = page->mapping;
1595
1596 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1597 return 0;
1598
1599 if (page_mapped(page)) {
1600 ClearPageChecked(page);
1601 return 1;
1602 }
1603 return PageChecked(page);
1604}
1605
1606static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1607{
1608 if (!page || PageWriteback(page))
1609 /* For split b-tree node pages, this function may be called
1610 twice. We ignore the 2nd or later calls by this check. */
1611 return 0;
1612
1613 lock_page(page);
1614 clear_page_dirty_for_io(page);
1615 set_page_writeback(page);
1616 unlock_page(page);
1617
1618 if (nilfs_test_page_to_be_frozen(page)) {
1619 int err = nilfs_copy_replace_page_buffers(page, out);
1620 if (unlikely(err))
1621 return err;
1622 }
1623 return 0;
1624}
1625
1626static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1627 struct page **failed_page)
1628{
1629 struct nilfs_segment_buffer *segbuf;
1630 struct page *bd_page = NULL, *fs_page = NULL;
1631 struct list_head *list = &sci->sc_copied_buffers;
1632 int err;
1633
1634 *failed_page = NULL;
1635 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1636 struct buffer_head *bh;
1637
1638 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1639 b_assoc_buffers) {
1640 if (bh->b_page != bd_page) {
1641 if (bd_page) {
1642 lock_page(bd_page);
1643 clear_page_dirty_for_io(bd_page);
1644 set_page_writeback(bd_page);
1645 unlock_page(bd_page);
1646 }
1647 bd_page = bh->b_page;
1648 }
1649 }
1650
1651 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1652 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001653 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001654 if (bh->b_page != bd_page) {
1655 lock_page(bd_page);
1656 clear_page_dirty_for_io(bd_page);
1657 set_page_writeback(bd_page);
1658 unlock_page(bd_page);
1659 bd_page = bh->b_page;
1660 }
1661 break;
1662 }
1663 if (bh->b_page != fs_page) {
1664 err = nilfs_begin_page_io(fs_page, list);
1665 if (unlikely(err)) {
1666 *failed_page = fs_page;
1667 goto out;
1668 }
1669 fs_page = bh->b_page;
1670 }
1671 }
1672 }
1673 if (bd_page) {
1674 lock_page(bd_page);
1675 clear_page_dirty_for_io(bd_page);
1676 set_page_writeback(bd_page);
1677 unlock_page(bd_page);
1678 }
1679 err = nilfs_begin_page_io(fs_page, list);
1680 if (unlikely(err))
1681 *failed_page = fs_page;
1682 out:
1683 return err;
1684}
1685
1686static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001687 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001688{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001689 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001690
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001691 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001692 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1693 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001694}
1695
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001696static void __nilfs_end_page_io(struct page *page, int err)
1697{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001698 if (!err) {
1699 if (!nilfs_page_buffers_clean(page))
1700 __set_page_dirty_nobuffers(page);
1701 ClearPageError(page);
1702 } else {
1703 __set_page_dirty_nobuffers(page);
1704 SetPageError(page);
1705 }
1706
1707 if (buffer_nilfs_allocated(page_buffers(page))) {
1708 if (TestClearPageWriteback(page))
1709 dec_zone_page_state(page, NR_WRITEBACK);
1710 } else
1711 end_page_writeback(page);
1712}
1713
1714static void nilfs_end_page_io(struct page *page, int err)
1715{
1716 if (!page)
1717 return;
1718
Ryusuke Konishia9777842009-07-28 17:55:29 +09001719 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001720 /*
1721 * For b-tree node pages, this function may be called twice
1722 * or more because they might be split in a segment.
1723 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001724 if (PageDirty(page)) {
1725 /*
1726 * For pages holding split b-tree node buffers, dirty
1727 * flag on the buffers may be cleared discretely.
1728 * In that case, the page is once redirtied for
1729 * remaining buffers, and it must be cancelled if
1730 * all the buffers get cleaned later.
1731 */
1732 lock_page(page);
1733 if (nilfs_page_buffers_clean(page))
1734 __nilfs_clear_page_dirty(page);
1735 unlock_page(page);
1736 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001737 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001738 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001739
1740 __nilfs_end_page_io(page, err);
1741}
1742
1743static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1744{
1745 struct buffer_head *bh, *head;
1746 struct page *page;
1747
1748 while (!list_empty(list)) {
1749 bh = list_entry(list->next, struct buffer_head,
1750 b_assoc_buffers);
1751 page = bh->b_page;
1752 page_cache_get(page);
1753 head = bh = page_buffers(page);
1754 do {
1755 if (!list_empty(&bh->b_assoc_buffers)) {
1756 list_del_init(&bh->b_assoc_buffers);
1757 if (!err) {
1758 set_buffer_uptodate(bh);
1759 clear_buffer_dirty(bh);
Ryusuke Konishi27e6c7a2010-12-26 16:28:28 +09001760 clear_buffer_delay(bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001761 clear_buffer_nilfs_volatile(bh);
1762 }
1763 brelse(bh); /* for b_assoc_buffers */
1764 }
1765 } while ((bh = bh->b_this_page) != head);
1766
1767 __nilfs_end_page_io(page, err);
1768 page_cache_release(page);
1769 }
1770}
1771
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001772static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001773 int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001774{
1775 struct nilfs_segment_buffer *segbuf;
1776 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001777 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001778
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001779 if (list_empty(logs))
1780 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001781
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001782 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001783 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1784 b_assoc_buffers) {
1785 if (bh->b_page != bd_page) {
1786 if (bd_page)
1787 end_page_writeback(bd_page);
1788 bd_page = bh->b_page;
1789 }
1790 }
1791
1792 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1793 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001794 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001795 if (bh->b_page != bd_page) {
1796 end_page_writeback(bd_page);
1797 bd_page = bh->b_page;
1798 }
1799 break;
1800 }
1801 if (bh->b_page != fs_page) {
1802 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001803 if (fs_page && fs_page == failed_page)
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001804 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001805 fs_page = bh->b_page;
1806 }
1807 }
1808 }
1809 if (bd_page)
1810 end_page_writeback(bd_page);
1811
1812 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001813}
1814
1815static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1816 struct the_nilfs *nilfs, int err)
1817{
1818 LIST_HEAD(logs);
1819 int ret;
1820
1821 list_splice_tail_init(&sci->sc_write_logs, &logs);
1822 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001823 nilfs_abort_logs(&logs, NULL, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001824
1825 list_splice_tail_init(&sci->sc_segbufs, &logs);
1826 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1827 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001828 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001829
1830 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1831 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1832 sci->sc_freesegs,
1833 sci->sc_nfreesegs,
1834 NULL);
1835 WARN_ON(ret); /* do not happen */
1836 }
1837
1838 nilfs_destroy_logs(&logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001839}
1840
1841static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1842 struct nilfs_segment_buffer *segbuf)
1843{
1844 nilfs->ns_segnum = segbuf->sb_segnum;
1845 nilfs->ns_nextnum = segbuf->sb_nextnum;
1846 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1847 + segbuf->sb_sum.nblocks;
1848 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1849 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1850}
1851
1852static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1853{
1854 struct nilfs_segment_buffer *segbuf;
1855 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +09001856 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001857 int update_sr = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001858
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001859 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001860 struct buffer_head *bh;
1861
1862 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1863 b_assoc_buffers) {
1864 set_buffer_uptodate(bh);
1865 clear_buffer_dirty(bh);
1866 if (bh->b_page != bd_page) {
1867 if (bd_page)
1868 end_page_writeback(bd_page);
1869 bd_page = bh->b_page;
1870 }
1871 }
1872 /*
1873 * We assume that the buffers which belong to the same page
1874 * continue over the buffer list.
1875 * Under this assumption, the last BHs of pages is
1876 * identifiable by the discontinuity of bh->b_page
1877 * (page != fs_page).
1878 *
1879 * For B-tree node blocks, however, this assumption is not
1880 * guaranteed. The cleanup code of B-tree node pages needs
1881 * special care.
1882 */
1883 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1884 b_assoc_buffers) {
1885 set_buffer_uptodate(bh);
1886 clear_buffer_dirty(bh);
Ryusuke Konishi27e6c7a2010-12-26 16:28:28 +09001887 clear_buffer_delay(bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001888 clear_buffer_nilfs_volatile(bh);
Ryusuke Konishib1f6a4f2010-08-31 11:40:34 +09001889 clear_buffer_nilfs_redirected(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001890 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001891 if (bh->b_page != bd_page) {
1892 end_page_writeback(bd_page);
1893 bd_page = bh->b_page;
1894 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001895 update_sr = true;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001896 break;
1897 }
1898 if (bh->b_page != fs_page) {
1899 nilfs_end_page_io(fs_page, 0);
1900 fs_page = bh->b_page;
1901 }
1902 }
1903
Ryusuke Konishi47620772010-05-23 21:48:36 +09001904 if (!nilfs_segbuf_simplex(segbuf)) {
1905 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001906 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1907 sci->sc_lseg_stime = jiffies;
1908 }
Ryusuke Konishi47620772010-05-23 21:48:36 +09001909 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001910 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1911 }
1912 }
1913 /*
1914 * Since pages may continue over multiple segment buffers,
1915 * end of the last page must be checked outside of the loop.
1916 */
1917 if (bd_page)
1918 end_page_writeback(bd_page);
1919
1920 nilfs_end_page_io(fs_page, 0);
1921
1922 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
1923
1924 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1925
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001926 if (nilfs_doing_gc())
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001927 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001928 else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001929 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001930
1931 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1932
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001933 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001934 nilfs_set_next_segment(nilfs, segbuf);
1935
1936 if (update_sr) {
1937 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07001938 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001939
Ryusuke Konishic96fa462009-04-06 19:01:57 -07001940 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001941 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1942 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001943 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001944 } else
1945 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1946}
1947
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001948static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1949{
1950 int ret;
1951
1952 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1953 if (!ret) {
1954 nilfs_segctor_complete_write(sci);
1955 nilfs_destroy_logs(&sci->sc_write_logs);
1956 }
1957 return ret;
1958}
1959
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001960static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
1961 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001962{
1963 struct nilfs_inode_info *ii, *n;
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001964 struct inode *ifile = sci->sc_root->ifile;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001965
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001966 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001967 retry:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001968 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001969 if (!ii->i_bh) {
1970 struct buffer_head *ibh;
1971 int err;
1972
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001973 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001974 err = nilfs_ifile_get_inode_block(
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001975 ifile, ii->vfs_inode.i_ino, &ibh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001976 if (unlikely(err)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001977 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001978 "failed to get inode block.\n");
1979 return err;
1980 }
1981 nilfs_mdt_mark_buffer_dirty(ibh);
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001982 nilfs_mdt_mark_dirty(ifile);
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001983 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001984 if (likely(!ii->i_bh))
1985 ii->i_bh = ibh;
1986 else
1987 brelse(ibh);
1988 goto retry;
1989 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001990
1991 clear_bit(NILFS_I_QUEUED, &ii->i_state);
1992 set_bit(NILFS_I_BUSY, &ii->i_state);
1993 list_del(&ii->i_dirty);
1994 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
1995 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001996 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001997
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001998 return 0;
1999}
2000
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002001static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
2002 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002003{
2004 struct nilfs_transaction_info *ti = current->journal_info;
2005 struct nilfs_inode_info *ii, *n;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002006
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002007 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002008 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2009 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002010 test_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002011 continue;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002012
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002013 clear_bit(NILFS_I_BUSY, &ii->i_state);
2014 brelse(ii->i_bh);
2015 ii->i_bh = NULL;
2016 list_del(&ii->i_dirty);
2017 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2018 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002019 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002020}
2021
2022/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002023 * Main procedure of segment constructor
2024 */
2025static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2026{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +09002027 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002028 struct page *failed_page;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002029 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002030
2031 sci->sc_stage.scnt = NILFS_ST_INIT;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002032 sci->sc_cno = nilfs->ns_cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002033
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002034 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002035 if (unlikely(err))
2036 goto out;
2037
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002038 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002039 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2040
2041 if (nilfs_segctor_clean(sci))
2042 goto out;
2043
2044 do {
2045 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2046
2047 err = nilfs_segctor_begin_construction(sci, nilfs);
2048 if (unlikely(err))
2049 goto out;
2050
2051 /* Update time stamp */
2052 sci->sc_seg_ctime = get_seconds();
2053
2054 err = nilfs_segctor_collect(sci, nilfs, mode);
2055 if (unlikely(err))
2056 goto failed;
2057
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002058 /* Avoid empty segment */
2059 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
Ryusuke Konishi47620772010-05-23 21:48:36 +09002060 nilfs_segbuf_empty(sci->sc_curseg)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002061 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002062 goto out;
2063 }
2064
2065 err = nilfs_segctor_assign(sci, mode);
2066 if (unlikely(err))
2067 goto failed;
2068
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002069 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002070 nilfs_segctor_fill_in_file_bmap(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002071
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002072 if (mode == SC_LSEG_SR &&
2073 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002074 err = nilfs_segctor_fill_in_checkpoint(sci);
2075 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002076 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002077
2078 nilfs_segctor_fill_in_super_root(sci, nilfs);
2079 }
2080 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2081
2082 /* Write partial segments */
2083 err = nilfs_segctor_prepare_write(sci, &failed_page);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002084 if (err) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002085 nilfs_abort_logs(&sci->sc_segbufs, failed_page, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002086 goto failed_to_write;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002087 }
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +09002088
2089 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2090 nilfs->ns_crc_seed);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002091
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002092 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002093 if (unlikely(err))
2094 goto failed_to_write;
2095
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002096 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2097 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2098 /*
2099 * At this point, we avoid double buffering
2100 * for blocksize < pagesize because page dirty
2101 * flag is turned off during write and dirty
2102 * buffers are not properly collected for
2103 * pages crossing over segments.
2104 */
2105 err = nilfs_segctor_wait(sci);
2106 if (err)
2107 goto failed_to_write;
2108 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002109 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2110
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002111 out:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002112 nilfs_segctor_drop_written_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002113 return err;
2114
2115 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002116 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2117 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002118
2119 failed:
2120 if (nilfs_doing_gc())
2121 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002122 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002123 goto out;
2124}
2125
2126/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002127 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002128 * @sci: nilfs_sc_info
2129 *
2130 * If the timer has already been set, it ignores the new request.
2131 * This function MUST be called within a section locking the segment
2132 * semaphore.
2133 */
2134static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2135{
2136 spin_lock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002137 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2138 sci->sc_timer.expires = jiffies + sci->sc_interval;
2139 add_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002140 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2141 }
2142 spin_unlock(&sci->sc_state_lock);
2143}
2144
2145static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2146{
2147 spin_lock(&sci->sc_state_lock);
2148 if (!(sci->sc_flush_request & (1 << bn))) {
2149 unsigned long prev_req = sci->sc_flush_request;
2150
2151 sci->sc_flush_request |= (1 << bn);
2152 if (!prev_req)
2153 wake_up(&sci->sc_wait_daemon);
2154 }
2155 spin_unlock(&sci->sc_state_lock);
2156}
2157
2158/**
2159 * nilfs_flush_segment - trigger a segment construction for resource control
2160 * @sb: super block
2161 * @ino: inode number of the file to be flushed out.
2162 */
2163void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2164{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002165 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
2166 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002167
2168 if (!sci || nilfs_doing_construction())
2169 return;
2170 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2171 /* assign bit 0 to data files */
2172}
2173
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002174struct nilfs_segctor_wait_request {
2175 wait_queue_t wq;
2176 __u32 seq;
2177 int err;
2178 atomic_t done;
2179};
2180
2181static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2182{
2183 struct nilfs_segctor_wait_request wait_req;
2184 int err = 0;
2185
2186 spin_lock(&sci->sc_state_lock);
2187 init_wait(&wait_req.wq);
2188 wait_req.err = 0;
2189 atomic_set(&wait_req.done, 0);
2190 wait_req.seq = ++sci->sc_seq_request;
2191 spin_unlock(&sci->sc_state_lock);
2192
2193 init_waitqueue_entry(&wait_req.wq, current);
2194 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2195 set_current_state(TASK_INTERRUPTIBLE);
2196 wake_up(&sci->sc_wait_daemon);
2197
2198 for (;;) {
2199 if (atomic_read(&wait_req.done)) {
2200 err = wait_req.err;
2201 break;
2202 }
2203 if (!signal_pending(current)) {
2204 schedule();
2205 continue;
2206 }
2207 err = -ERESTARTSYS;
2208 break;
2209 }
2210 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2211 return err;
2212}
2213
2214static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2215{
2216 struct nilfs_segctor_wait_request *wrq, *n;
2217 unsigned long flags;
2218
2219 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2220 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2221 wq.task_list) {
2222 if (!atomic_read(&wrq->done) &&
2223 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2224 wrq->err = err;
2225 atomic_set(&wrq->done, 1);
2226 }
2227 if (atomic_read(&wrq->done)) {
2228 wrq->wq.func(&wrq->wq,
2229 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2230 0, NULL);
2231 }
2232 }
2233 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2234}
2235
2236/**
2237 * nilfs_construct_segment - construct a logical segment
2238 * @sb: super block
2239 *
2240 * Return Value: On success, 0 is retured. On errors, one of the following
2241 * negative error code is returned.
2242 *
2243 * %-EROFS - Read only filesystem.
2244 *
2245 * %-EIO - I/O error
2246 *
2247 * %-ENOSPC - No space left on device (only in a panic state).
2248 *
2249 * %-ERESTARTSYS - Interrupted.
2250 *
2251 * %-ENOMEM - Insufficient memory available.
2252 */
2253int nilfs_construct_segment(struct super_block *sb)
2254{
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002255 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
2256 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002257 struct nilfs_transaction_info *ti;
2258 int err;
2259
2260 if (!sci)
2261 return -EROFS;
2262
2263 /* A call inside transactions causes a deadlock. */
2264 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2265
2266 err = nilfs_segctor_sync(sci);
2267 return err;
2268}
2269
2270/**
2271 * nilfs_construct_dsync_segment - construct a data-only logical segment
2272 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002273 * @inode: inode whose data blocks should be written out
2274 * @start: start byte offset
2275 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002276 *
2277 * Return Value: On success, 0 is retured. On errors, one of the following
2278 * negative error code is returned.
2279 *
2280 * %-EROFS - Read only filesystem.
2281 *
2282 * %-EIO - I/O error
2283 *
2284 * %-ENOSPC - No space left on device (only in a panic state).
2285 *
2286 * %-ERESTARTSYS - Interrupted.
2287 *
2288 * %-ENOMEM - Insufficient memory available.
2289 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002290int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2291 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002292{
Ryusuke Konishif7545142011-03-09 11:05:08 +09002293 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002294 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002295 struct nilfs_inode_info *ii;
2296 struct nilfs_transaction_info ti;
2297 int err = 0;
2298
2299 if (!sci)
2300 return -EROFS;
2301
Ryusuke Konishif7545142011-03-09 11:05:08 +09002302 nilfs_transaction_lock(sb, &ti, 0);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002303
2304 ii = NILFS_I(inode);
2305 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002306 nilfs_test_opt(nilfs, STRICT_ORDER) ||
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002307 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002308 nilfs_discontinued(nilfs)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09002309 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002310 err = nilfs_segctor_sync(sci);
2311 return err;
2312 }
2313
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002314 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002315 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2316 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002317 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002318 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002319 return 0;
2320 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002321 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002322 sci->sc_dsync_inode = ii;
2323 sci->sc_dsync_start = start;
2324 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002325
2326 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2327
Ryusuke Konishif7545142011-03-09 11:05:08 +09002328 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002329 return err;
2330}
2331
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002332#define FLUSH_FILE_BIT (0x1) /* data file only */
2333#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2334
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002335/**
2336 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2337 * @sci: segment constructor object
2338 */
2339static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002340{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002341 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002342 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002343 spin_unlock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002344 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002345}
2346
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002347/**
2348 * nilfs_segctor_notify - notify the result of request to caller threads
2349 * @sci: segment constructor object
2350 * @mode: mode of log forming
2351 * @err: error code to be notified
2352 */
2353static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002354{
2355 /* Clear requests (even when the construction failed) */
2356 spin_lock(&sci->sc_state_lock);
2357
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002358 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002359 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002360 sci->sc_seq_done = sci->sc_seq_accepted;
2361 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002362 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002363 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002364 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002365 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002366 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002367 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002368
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002369 /* re-enable timer if checkpoint creation was not done */
Li Hongfdce8952010-04-10 23:25:39 +08002370 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2371 time_before(jiffies, sci->sc_timer.expires))
2372 add_timer(&sci->sc_timer);
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002373 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002374 spin_unlock(&sci->sc_state_lock);
2375}
2376
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002377/**
2378 * nilfs_segctor_construct - form logs and write them to disk
2379 * @sci: segment constructor object
2380 * @mode: mode of log forming
2381 */
2382static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002383{
Ryusuke Konishif7545142011-03-09 11:05:08 +09002384 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002385 struct nilfs_super_block **sbp;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002386 int err = 0;
2387
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002388 nilfs_segctor_accept(sci);
2389
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002390 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002391 mode = SC_LSEG_SR;
2392 if (!nilfs_segctor_confirm(sci))
2393 err = nilfs_segctor_do_construct(sci, mode);
2394
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002395 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002396 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002397 atomic_set(&nilfs->ns_ndirtyblks, 0);
2398 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2399 nilfs_discontinued(nilfs)) {
2400 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002401 err = -EIO;
Ryusuke Konishif7545142011-03-09 11:05:08 +09002402 sbp = nilfs_prepare_super(sci->sc_super,
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002403 nilfs_sb_will_flip(nilfs));
2404 if (likely(sbp)) {
2405 nilfs_set_log_cursor(sbp[0], nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002406 err = nilfs_commit_super(sci->sc_super,
2407 NILFS_SB_COMMIT);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002408 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002409 up_write(&nilfs->ns_sem);
2410 }
2411 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002412
2413 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002414 return err;
2415}
2416
2417static void nilfs_construction_timeout(unsigned long data)
2418{
2419 struct task_struct *p = (struct task_struct *)data;
2420 wake_up_process(p);
2421}
2422
2423static void
2424nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2425{
2426 struct nilfs_inode_info *ii, *n;
2427
2428 list_for_each_entry_safe(ii, n, head, i_dirty) {
2429 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2430 continue;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002431 list_del_init(&ii->i_dirty);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +09002432 iput(&ii->vfs_inode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002433 }
2434}
2435
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002436int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2437 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002438{
Ryusuke Konishif7545142011-03-09 11:05:08 +09002439 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002440 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002441 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002442 int err;
2443
2444 if (unlikely(!sci))
2445 return -EROFS;
2446
Ryusuke Konishif7545142011-03-09 11:05:08 +09002447 nilfs_transaction_lock(sb, &ti, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002448
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002449 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002450 if (unlikely(err))
2451 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002452
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002453 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002454 if (unlikely(err)) {
2455 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002456 goto out_unlock;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002457 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002458
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002459 sci->sc_freesegs = kbufs[4];
2460 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002461 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002462
2463 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002464 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002465 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002466
2467 if (likely(!err))
2468 break;
2469
2470 nilfs_warning(sb, __func__,
2471 "segment construction failed. (err=%d)", err);
2472 set_current_state(TASK_INTERRUPTIBLE);
2473 schedule_timeout(sci->sc_interval);
2474 }
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002475 if (nilfs_test_opt(nilfs, DISCARD)) {
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002476 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2477 sci->sc_nfreesegs);
2478 if (ret) {
2479 printk(KERN_WARNING
2480 "NILFS warning: error %d on discard request, "
2481 "turning discards off for the device\n", ret);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002482 nilfs_clear_opt(nilfs, DISCARD);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002483 }
2484 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002485
2486 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002487 sci->sc_freesegs = NULL;
2488 sci->sc_nfreesegs = 0;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002489 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002490 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002491 return err;
2492}
2493
2494static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2495{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002496 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002497
Ryusuke Konishif7545142011-03-09 11:05:08 +09002498 nilfs_transaction_lock(sci->sc_super, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002499 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002500
2501 /*
2502 * Unclosed segment should be retried. We do this using sc_timer.
2503 * Timeout of sc_timer will invoke complete construction which leads
2504 * to close the current logical segment.
2505 */
2506 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2507 nilfs_segctor_start_timer(sci);
2508
Ryusuke Konishif7545142011-03-09 11:05:08 +09002509 nilfs_transaction_unlock(sci->sc_super);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002510}
2511
2512static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2513{
2514 int mode = 0;
2515 int err;
2516
2517 spin_lock(&sci->sc_state_lock);
2518 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2519 SC_FLUSH_DAT : SC_FLUSH_FILE;
2520 spin_unlock(&sci->sc_state_lock);
2521
2522 if (mode) {
2523 err = nilfs_segctor_do_construct(sci, mode);
2524
2525 spin_lock(&sci->sc_state_lock);
2526 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2527 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2528 spin_unlock(&sci->sc_state_lock);
2529 }
2530 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2531}
2532
2533static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2534{
2535 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2536 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2537 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2538 return SC_FLUSH_FILE;
2539 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2540 return SC_FLUSH_DAT;
2541 }
2542 return SC_LSEG_SR;
2543}
2544
2545/**
2546 * nilfs_segctor_thread - main loop of the segment constructor thread.
2547 * @arg: pointer to a struct nilfs_sc_info.
2548 *
2549 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2550 * to execute segment constructions.
2551 */
2552static int nilfs_segctor_thread(void *arg)
2553{
2554 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +09002555 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002556 int timeout = 0;
2557
Li Hongfdce8952010-04-10 23:25:39 +08002558 sci->sc_timer.data = (unsigned long)current;
2559 sci->sc_timer.function = nilfs_construction_timeout;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002560
2561 /* start sync. */
2562 sci->sc_task = current;
2563 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2564 printk(KERN_INFO
2565 "segctord starting. Construction interval = %lu seconds, "
2566 "CP frequency < %lu seconds\n",
2567 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2568
2569 spin_lock(&sci->sc_state_lock);
2570 loop:
2571 for (;;) {
2572 int mode;
2573
2574 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2575 goto end_thread;
2576
2577 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2578 mode = SC_LSEG_SR;
2579 else if (!sci->sc_flush_request)
2580 break;
2581 else
2582 mode = nilfs_segctor_flush_mode(sci);
2583
2584 spin_unlock(&sci->sc_state_lock);
2585 nilfs_segctor_thread_construct(sci, mode);
2586 spin_lock(&sci->sc_state_lock);
2587 timeout = 0;
2588 }
2589
2590
2591 if (freezing(current)) {
2592 spin_unlock(&sci->sc_state_lock);
2593 refrigerator();
2594 spin_lock(&sci->sc_state_lock);
2595 } else {
2596 DEFINE_WAIT(wait);
2597 int should_sleep = 1;
2598
2599 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2600 TASK_INTERRUPTIBLE);
2601
2602 if (sci->sc_seq_request != sci->sc_seq_done)
2603 should_sleep = 0;
2604 else if (sci->sc_flush_request)
2605 should_sleep = 0;
2606 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2607 should_sleep = time_before(jiffies,
Li Hongfdce8952010-04-10 23:25:39 +08002608 sci->sc_timer.expires);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002609
2610 if (should_sleep) {
2611 spin_unlock(&sci->sc_state_lock);
2612 schedule();
2613 spin_lock(&sci->sc_state_lock);
2614 }
2615 finish_wait(&sci->sc_wait_daemon, &wait);
2616 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
Li Hongfdce8952010-04-10 23:25:39 +08002617 time_after_eq(jiffies, sci->sc_timer.expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002618
2619 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002620 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002621 }
2622 goto loop;
2623
2624 end_thread:
2625 spin_unlock(&sci->sc_state_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002626
2627 /* end sync. */
2628 sci->sc_task = NULL;
2629 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2630 return 0;
2631}
2632
2633static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2634{
2635 struct task_struct *t;
2636
2637 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2638 if (IS_ERR(t)) {
2639 int err = PTR_ERR(t);
2640
2641 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2642 err);
2643 return err;
2644 }
2645 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2646 return 0;
2647}
2648
2649static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
Jiro SEKIBA6b81e142010-10-14 13:52:00 +09002650 __acquires(&sci->sc_state_lock)
2651 __releases(&sci->sc_state_lock)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002652{
2653 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2654
2655 while (sci->sc_task) {
2656 wake_up(&sci->sc_wait_daemon);
2657 spin_unlock(&sci->sc_state_lock);
2658 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2659 spin_lock(&sci->sc_state_lock);
2660 }
2661}
2662
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002663/*
2664 * Setup & clean-up functions
2665 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002666static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002667 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002668{
Ryusuke Konishif7545142011-03-09 11:05:08 +09002669 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002670 struct nilfs_sc_info *sci;
2671
2672 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2673 if (!sci)
2674 return NULL;
2675
Ryusuke Konishif7545142011-03-09 11:05:08 +09002676 sci->sc_super = sb;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002677
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002678 nilfs_get_root(root);
2679 sci->sc_root = root;
2680
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002681 init_waitqueue_head(&sci->sc_wait_request);
2682 init_waitqueue_head(&sci->sc_wait_daemon);
2683 init_waitqueue_head(&sci->sc_wait_task);
2684 spin_lock_init(&sci->sc_state_lock);
2685 INIT_LIST_HEAD(&sci->sc_dirty_files);
2686 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002687 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002688 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002689 INIT_LIST_HEAD(&sci->sc_copied_buffers);
Li Hongfdce8952010-04-10 23:25:39 +08002690 init_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002691
2692 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2693 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2694 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2695
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002696 if (nilfs->ns_interval)
2697 sci->sc_interval = nilfs->ns_interval;
2698 if (nilfs->ns_watermark)
2699 sci->sc_watermark = nilfs->ns_watermark;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002700 return sci;
2701}
2702
2703static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2704{
2705 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2706
2707 /* The segctord thread was stopped and its timer was removed.
2708 But some tasks remain. */
2709 do {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002710 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002711
Ryusuke Konishif7545142011-03-09 11:05:08 +09002712 nilfs_transaction_lock(sci->sc_super, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002713 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002714 nilfs_transaction_unlock(sci->sc_super);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002715
2716 } while (ret && retrycount-- > 0);
2717}
2718
2719/**
2720 * nilfs_segctor_destroy - destroy the segment constructor.
2721 * @sci: nilfs_sc_info
2722 *
2723 * nilfs_segctor_destroy() kills the segctord thread and frees
2724 * the nilfs_sc_info struct.
2725 * Caller must hold the segment semaphore.
2726 */
2727static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2728{
Ryusuke Konishid96bbfa2011-03-09 11:05:08 +09002729 struct the_nilfs *nilfs = NILFS_SB(sci->sc_super)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002730 int flag;
2731
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002732 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002733
2734 spin_lock(&sci->sc_state_lock);
2735 nilfs_segctor_kill_thread(sci);
2736 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2737 || sci->sc_seq_request != sci->sc_seq_done);
2738 spin_unlock(&sci->sc_state_lock);
2739
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002740 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002741 nilfs_segctor_write_out(sci);
2742
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002743 WARN_ON(!list_empty(&sci->sc_copied_buffers));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002744
2745 if (!list_empty(&sci->sc_dirty_files)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002746 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002747 "dirty file(s) after the final construction\n");
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002748 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002749 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002750
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002751 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002752 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002753
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002754 nilfs_put_root(sci->sc_root);
2755
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002756 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002757
Li Hongfdce8952010-04-10 23:25:39 +08002758 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002759 kfree(sci);
2760}
2761
2762/**
Ryusuke Konishif7545142011-03-09 11:05:08 +09002763 * nilfs_attach_log_writer - attach log writer
2764 * @sb: super block instance
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002765 * @root: root object of the current filesystem tree
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002766 *
Ryusuke Konishif7545142011-03-09 11:05:08 +09002767 * This allocates a log writer object, initializes it, and starts the
2768 * log writer.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002769 *
2770 * Return Value: On success, 0 is returned. On error, one of the following
2771 * negative error code is returned.
2772 *
2773 * %-ENOMEM - Insufficient memory available.
2774 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002775int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002776{
Ryusuke Konishif7545142011-03-09 11:05:08 +09002777 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002778 int err;
2779
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002780 if (nilfs->ns_writer) {
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002781 /*
2782 * This happens if the filesystem was remounted
2783 * read/write after nilfs_error degenerated it into a
2784 * read-only mount.
2785 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002786 nilfs_detach_log_writer(sb);
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002787 }
2788
Ryusuke Konishif7545142011-03-09 11:05:08 +09002789 nilfs->ns_writer = nilfs_segctor_new(sb, root);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002790 if (!nilfs->ns_writer)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002791 return -ENOMEM;
2792
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002793 err = nilfs_segctor_start_thread(nilfs->ns_writer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002794 if (err) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002795 kfree(nilfs->ns_writer);
2796 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002797 }
2798 return err;
2799}
2800
2801/**
Ryusuke Konishif7545142011-03-09 11:05:08 +09002802 * nilfs_detach_log_writer - destroy log writer
2803 * @sb: super block instance
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002804 *
Ryusuke Konishif7545142011-03-09 11:05:08 +09002805 * This kills log writer daemon, frees the log writer object, and
2806 * destroys list of dirty files.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002807 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002808void nilfs_detach_log_writer(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002809{
Ryusuke Konishif7545142011-03-09 11:05:08 +09002810 struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002811 LIST_HEAD(garbage_list);
2812
2813 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002814 if (nilfs->ns_writer) {
2815 nilfs_segctor_destroy(nilfs->ns_writer);
2816 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002817 }
2818
2819 /* Force to free the list of dirty files */
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002820 spin_lock(&nilfs->ns_inode_lock);
2821 if (!list_empty(&nilfs->ns_dirty_files)) {
2822 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002823 nilfs_warning(sb, __func__,
2824 "Hit dirty file after stopped log writer\n");
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002825 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002826 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002827 up_write(&nilfs->ns_segctor_sem);
2828
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002829 nilfs_dispose_list(nilfs, &garbage_list, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002830}