blob: ef354043b87ca5cf4ba85715e6ef94e534cc50aa [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>
Ryusuke Konishiead8ecf2015-04-16 12:46:28 -070027#include <linux/bitops.h>
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070028#include <linux/bio.h>
29#include <linux/completion.h>
30#include <linux/blkdev.h>
31#include <linux/backing-dev.h>
32#include <linux/freezer.h>
33#include <linux/kthread.h>
34#include <linux/crc32.h>
35#include <linux/pagevec.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070037#include "nilfs.h"
38#include "btnode.h"
39#include "page.h"
40#include "segment.h"
41#include "sufile.h"
42#include "cpfile.h"
43#include "ifile.h"
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070044#include "segbuf.h"
45
46
47/*
48 * Segment constructor
49 */
50#define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */
51
52#define SC_MAX_SEGDELTA 64 /* Upper limit of the number of segments
53 appended in collection retry loop */
54
55/* Construction mode */
56enum {
57 SC_LSEG_SR = 1, /* Make a logical segment having a super root */
58 SC_LSEG_DSYNC, /* Flush data blocks of a given file and make
59 a logical segment without a super root */
60 SC_FLUSH_FILE, /* Flush data files, leads to segment writes without
61 creating a checkpoint */
62 SC_FLUSH_DAT, /* Flush DAT file. This also creates segments without
63 a checkpoint */
64};
65
66/* Stage numbers of dirty block collection */
67enum {
68 NILFS_ST_INIT = 0,
69 NILFS_ST_GC, /* Collecting dirty blocks for GC */
70 NILFS_ST_FILE,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070071 NILFS_ST_IFILE,
72 NILFS_ST_CPFILE,
73 NILFS_ST_SUFILE,
74 NILFS_ST_DAT,
75 NILFS_ST_SR, /* Super root */
76 NILFS_ST_DSYNC, /* Data sync blocks */
77 NILFS_ST_DONE,
78};
79
Hitoshi Mitake58497702015-11-06 16:31:59 -080080#define CREATE_TRACE_POINTS
81#include <trace/events/nilfs2.h>
82
83/*
84 * nilfs_sc_cstage_inc(), nilfs_sc_cstage_set(), nilfs_sc_cstage_get() are
85 * wrapper functions of stage count (nilfs_sc_info->sc_stage.scnt). Users of
86 * the variable must use them because transition of stage count must involve
87 * trace events (trace_nilfs2_collection_stage_transition).
88 *
89 * nilfs_sc_cstage_get() isn't required for the above purpose because it doesn't
90 * produce tracepoint events. It is provided just for making the intention
91 * clear.
92 */
93static inline void nilfs_sc_cstage_inc(struct nilfs_sc_info *sci)
94{
95 sci->sc_stage.scnt++;
96 trace_nilfs2_collection_stage_transition(sci);
97}
98
99static inline void nilfs_sc_cstage_set(struct nilfs_sc_info *sci, int next_scnt)
100{
101 sci->sc_stage.scnt = next_scnt;
102 trace_nilfs2_collection_stage_transition(sci);
103}
104
105static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci)
106{
107 return sci->sc_stage.scnt;
108}
109
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700110/* State flags of collection */
111#define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
112#define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900113#define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
114#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700115
116/* Operations depending on the construction mode and file type */
117struct nilfs_sc_operations {
118 int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *,
119 struct inode *);
120 int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *,
121 struct inode *);
122 int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *,
123 struct inode *);
124 void (*write_data_binfo)(struct nilfs_sc_info *,
125 struct nilfs_segsum_pointer *,
126 union nilfs_binfo *);
127 void (*write_node_binfo)(struct nilfs_sc_info *,
128 struct nilfs_segsum_pointer *,
129 union nilfs_binfo *);
130};
131
132/*
133 * Other definitions
134 */
135static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
136static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
137static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900138static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700139
140#define nilfs_cnt32_gt(a, b) \
141 (typecheck(__u32, a) && typecheck(__u32, b) && \
142 ((__s32)(b) - (__s32)(a) < 0))
143#define nilfs_cnt32_ge(a, b) \
144 (typecheck(__u32, a) && typecheck(__u32, b) && \
145 ((__s32)(a) - (__s32)(b) >= 0))
146#define nilfs_cnt32_lt(a, b) nilfs_cnt32_gt(b, a)
147#define nilfs_cnt32_le(a, b) nilfs_cnt32_ge(b, a)
148
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700149static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
150{
151 struct nilfs_transaction_info *cur_ti = current->journal_info;
152 void *save = NULL;
153
154 if (cur_ti) {
155 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
156 return ++cur_ti->ti_count;
157 else {
158 /*
159 * If journal_info field is occupied by other FS,
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700160 * it is saved and will be restored on
161 * nilfs_transaction_commit().
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700162 */
163 printk(KERN_WARNING
164 "NILFS warning: journal info from a different "
165 "FS\n");
166 save = current->journal_info;
167 }
168 }
169 if (!ti) {
170 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
171 if (!ti)
172 return -ENOMEM;
173 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
174 } else {
175 ti->ti_flags = 0;
176 }
177 ti->ti_count = 0;
178 ti->ti_save = save;
179 ti->ti_magic = NILFS_TI_MAGIC;
180 current->journal_info = ti;
181 return 0;
182}
183
184/**
185 * nilfs_transaction_begin - start indivisible file operations.
186 * @sb: super block
187 * @ti: nilfs_transaction_info
188 * @vacancy_check: flags for vacancy rate checks
189 *
190 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
191 * the segment semaphore, to make a segment construction and write tasks
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700192 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700193 * The region enclosed by these two functions can be nested. To avoid a
194 * deadlock, the semaphore is only acquired or released in the outermost call.
195 *
196 * This function allocates a nilfs_transaction_info struct to keep context
197 * information on it. It is initialized and hooked onto the current task in
198 * the outermost call. If a pre-allocated struct is given to @ti, it is used
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900199 * instead; otherwise a new struct is assigned from a slab.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700200 *
201 * When @vacancy_check flag is set, this function will check the amount of
202 * free space, and will wait for the GC to reclaim disk space if low capacity.
203 *
204 * Return Value: On success, 0 is returned. On error, one of the following
205 * negative error code is returned.
206 *
207 * %-ENOMEM - Insufficient memory available.
208 *
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700209 * %-ENOSPC - No space left on device
210 */
211int nilfs_transaction_begin(struct super_block *sb,
212 struct nilfs_transaction_info *ti,
213 int vacancy_check)
214{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700215 struct the_nilfs *nilfs;
216 int ret = nilfs_prepare_segment_lock(ti);
217
218 if (unlikely(ret < 0))
219 return ret;
220 if (ret > 0)
221 return 0;
222
Jan Kara2c22b332012-06-12 16:20:44 +0200223 sb_start_intwrite(sb);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900224
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900225 nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700226 down_read(&nilfs->ns_segctor_sem);
227 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
228 up_read(&nilfs->ns_segctor_sem);
229 ret = -ENOSPC;
230 goto failed;
231 }
232 return 0;
233
234 failed:
235 ti = current->journal_info;
236 current->journal_info = ti->ti_save;
237 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
238 kmem_cache_free(nilfs_transaction_cachep, ti);
Jan Kara2c22b332012-06-12 16:20:44 +0200239 sb_end_intwrite(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700240 return ret;
241}
242
243/**
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700244 * nilfs_transaction_commit - commit indivisible file operations.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700245 * @sb: super block
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700246 *
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700247 * nilfs_transaction_commit() releases the read semaphore which is
248 * acquired by nilfs_transaction_begin(). This is only performed
249 * in outermost call of this function. If a commit flag is set,
250 * nilfs_transaction_commit() sets a timer to start the segment
251 * constructor. If a sync flag is set, it starts construction
252 * directly.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700253 */
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700254int nilfs_transaction_commit(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700255{
256 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900257 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700258 int err = 0;
259
260 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700261 ti->ti_flags |= NILFS_TI_COMMIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700262 if (ti->ti_count > 0) {
263 ti->ti_count--;
264 return 0;
265 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900266 if (nilfs->ns_writer) {
267 struct nilfs_sc_info *sci = nilfs->ns_writer;
268
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700269 if (ti->ti_flags & NILFS_TI_COMMIT)
270 nilfs_segctor_start_timer(sci);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900271 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700272 nilfs_segctor_do_flush(sci, 0);
273 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900274 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700275 current->journal_info = ti->ti_save;
276
277 if (ti->ti_flags & NILFS_TI_SYNC)
278 err = nilfs_construct_segment(sb);
279 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
280 kmem_cache_free(nilfs_transaction_cachep, ti);
Jan Kara2c22b332012-06-12 16:20:44 +0200281 sb_end_intwrite(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700282 return err;
283}
284
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700285void nilfs_transaction_abort(struct super_block *sb)
286{
287 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900288 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700289
290 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
291 if (ti->ti_count > 0) {
292 ti->ti_count--;
293 return;
294 }
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900295 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700296
297 current->journal_info = ti->ti_save;
298 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
299 kmem_cache_free(nilfs_transaction_cachep, ti);
Jan Kara2c22b332012-06-12 16:20:44 +0200300 sb_end_intwrite(sb);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700301}
302
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700303void nilfs_relax_pressure_in_lock(struct super_block *sb)
304{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900305 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900306 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700307
308 if (!sci || !sci->sc_flush_request)
309 return;
310
311 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
312 up_read(&nilfs->ns_segctor_sem);
313
314 down_write(&nilfs->ns_segctor_sem);
315 if (sci->sc_flush_request &&
316 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
317 struct nilfs_transaction_info *ti = current->journal_info;
318
319 ti->ti_flags |= NILFS_TI_WRITER;
320 nilfs_segctor_do_immediate_flush(sci);
321 ti->ti_flags &= ~NILFS_TI_WRITER;
322 }
323 downgrade_write(&nilfs->ns_segctor_sem);
324}
325
Ryusuke Konishif7545142011-03-09 11:05:08 +0900326static void nilfs_transaction_lock(struct super_block *sb,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700327 struct nilfs_transaction_info *ti,
328 int gcflag)
329{
330 struct nilfs_transaction_info *cur_ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900331 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900332 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700333
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700334 WARN_ON(cur_ti);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700335 ti->ti_flags = NILFS_TI_WRITER;
336 ti->ti_count = 0;
337 ti->ti_save = cur_ti;
338 ti->ti_magic = NILFS_TI_MAGIC;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700339 current->journal_info = ti;
340
341 for (;;) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900342 down_write(&nilfs->ns_segctor_sem);
343 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700344 break;
345
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900346 nilfs_segctor_do_immediate_flush(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700347
Ryusuke Konishif7545142011-03-09 11:05:08 +0900348 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700349 yield();
350 }
351 if (gcflag)
352 ti->ti_flags |= NILFS_TI_GC;
353}
354
Ryusuke Konishif7545142011-03-09 11:05:08 +0900355static void nilfs_transaction_unlock(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700356{
357 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900358 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700359
360 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
361 BUG_ON(ti->ti_count > 0);
362
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900363 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700364 current->journal_info = ti->ti_save;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700365}
366
367static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
368 struct nilfs_segsum_pointer *ssp,
369 unsigned bytes)
370{
371 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
372 unsigned blocksize = sci->sc_super->s_blocksize;
373 void *p;
374
375 if (unlikely(ssp->offset + bytes > blocksize)) {
376 ssp->offset = 0;
377 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
378 &segbuf->sb_segsum_buffers));
379 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
380 }
381 p = ssp->bh->b_data + ssp->offset;
382 ssp->offset += bytes;
383 return p;
384}
385
386/**
387 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
388 * @sci: nilfs_sc_info
389 */
390static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
391{
392 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
393 struct buffer_head *sumbh;
394 unsigned sumbytes;
395 unsigned flags = 0;
396 int err;
397
398 if (nilfs_doing_gc())
399 flags = NILFS_SS_GC;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900400 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700401 if (unlikely(err))
402 return err;
403
404 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
405 sumbytes = segbuf->sb_sum.sumbytes;
406 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
407 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
408 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
409 return 0;
410}
411
412static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
413{
414 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
415 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
416 return -E2BIG; /* The current segment is filled up
417 (internal code) */
418 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
419 return nilfs_segctor_reset_segment_buffer(sci);
420}
421
422static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
423{
424 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
425 int err;
426
427 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
428 err = nilfs_segctor_feed_segment(sci);
429 if (err)
430 return err;
431 segbuf = sci->sc_curseg;
432 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900433 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700434 if (likely(!err))
435 segbuf->sb_sum.flags |= NILFS_SS_SR;
436 return err;
437}
438
439/*
440 * Functions for making segment summary and payloads
441 */
442static int nilfs_segctor_segsum_block_required(
443 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
444 unsigned binfo_size)
445{
446 unsigned blocksize = sci->sc_super->s_blocksize;
447 /* Size of finfo and binfo is enough small against blocksize */
448
449 return ssp->offset + binfo_size +
450 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
451 blocksize;
452}
453
454static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
455 struct inode *inode)
456{
457 sci->sc_curseg->sb_sum.nfinfo++;
458 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
459 nilfs_segctor_map_segsum_entry(
460 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700461
Ryusuke Konishi72746ac2011-02-28 13:41:11 +0900462 if (NILFS_I(inode)->i_root &&
463 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700464 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700465 /* skip finfo */
466}
467
468static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
469 struct inode *inode)
470{
471 struct nilfs_finfo *finfo;
472 struct nilfs_inode_info *ii;
473 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900474 __u64 cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700475
476 if (sci->sc_blk_cnt == 0)
477 return;
478
479 ii = NILFS_I(inode);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900480
481 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
482 cno = ii->i_cno;
483 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
484 cno = 0;
485 else
486 cno = sci->sc_cno;
487
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700488 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
489 sizeof(*finfo));
490 finfo->fi_ino = cpu_to_le64(inode->i_ino);
491 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
492 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900493 finfo->fi_cno = cpu_to_le64(cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700494
495 segbuf = sci->sc_curseg;
496 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
497 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
498 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
499 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
500}
501
502static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
503 struct buffer_head *bh,
504 struct inode *inode,
505 unsigned binfo_size)
506{
507 struct nilfs_segment_buffer *segbuf;
508 int required, err = 0;
509
510 retry:
511 segbuf = sci->sc_curseg;
512 required = nilfs_segctor_segsum_block_required(
513 sci, &sci->sc_binfo_ptr, binfo_size);
514 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
515 nilfs_segctor_end_finfo(sci, inode);
516 err = nilfs_segctor_feed_segment(sci);
517 if (err)
518 return err;
519 goto retry;
520 }
521 if (unlikely(required)) {
522 err = nilfs_segbuf_extend_segsum(segbuf);
523 if (unlikely(err))
524 goto failed;
525 }
526 if (sci->sc_blk_cnt == 0)
527 nilfs_segctor_begin_finfo(sci, inode);
528
529 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
530 /* Substitution to vblocknr is delayed until update_blocknr() */
531 nilfs_segbuf_add_file_buffer(segbuf, bh);
532 sci->sc_blk_cnt++;
533 failed:
534 return err;
535}
536
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700537/*
538 * Callback functions that enumerate, mark, and collect dirty blocks
539 */
540static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
541 struct buffer_head *bh, struct inode *inode)
542{
543 int err;
544
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700545 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900546 if (err < 0)
547 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700548
549 err = nilfs_segctor_add_file_block(sci, bh, inode,
550 sizeof(struct nilfs_binfo_v));
551 if (!err)
552 sci->sc_datablk_cnt++;
553 return err;
554}
555
556static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
557 struct buffer_head *bh,
558 struct inode *inode)
559{
Ryusuke Konishie8289492010-11-19 15:26:20 +0900560 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700561}
562
563static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
564 struct buffer_head *bh,
565 struct inode *inode)
566{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700567 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700568 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
569}
570
571static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
572 struct nilfs_segsum_pointer *ssp,
573 union nilfs_binfo *binfo)
574{
575 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
576 sci, ssp, sizeof(*binfo_v));
577 *binfo_v = binfo->bi_v;
578}
579
580static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
581 struct nilfs_segsum_pointer *ssp,
582 union nilfs_binfo *binfo)
583{
584 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
585 sci, ssp, sizeof(*vblocknr));
586 *vblocknr = binfo->bi_v.bi_vblocknr;
587}
588
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900589static struct nilfs_sc_operations nilfs_sc_file_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700590 .collect_data = nilfs_collect_file_data,
591 .collect_node = nilfs_collect_file_node,
592 .collect_bmap = nilfs_collect_file_bmap,
593 .write_data_binfo = nilfs_write_file_data_binfo,
594 .write_node_binfo = nilfs_write_file_node_binfo,
595};
596
597static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
598 struct buffer_head *bh, struct inode *inode)
599{
600 int err;
601
602 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900603 if (err < 0)
604 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700605
606 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
607 if (!err)
608 sci->sc_datablk_cnt++;
609 return err;
610}
611
612static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
613 struct buffer_head *bh, struct inode *inode)
614{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700615 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700616 return nilfs_segctor_add_file_block(sci, bh, inode,
617 sizeof(struct nilfs_binfo_dat));
618}
619
620static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
621 struct nilfs_segsum_pointer *ssp,
622 union nilfs_binfo *binfo)
623{
624 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
625 sizeof(*blkoff));
626 *blkoff = binfo->bi_dat.bi_blkoff;
627}
628
629static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
630 struct nilfs_segsum_pointer *ssp,
631 union nilfs_binfo *binfo)
632{
633 struct nilfs_binfo_dat *binfo_dat =
634 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
635 *binfo_dat = binfo->bi_dat;
636}
637
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900638static struct nilfs_sc_operations nilfs_sc_dat_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700639 .collect_data = nilfs_collect_dat_data,
640 .collect_node = nilfs_collect_file_node,
641 .collect_bmap = nilfs_collect_dat_bmap,
642 .write_data_binfo = nilfs_write_dat_data_binfo,
643 .write_node_binfo = nilfs_write_dat_node_binfo,
644};
645
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900646static struct nilfs_sc_operations nilfs_sc_dsync_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700647 .collect_data = nilfs_collect_file_data,
648 .collect_node = NULL,
649 .collect_bmap = NULL,
650 .write_data_binfo = nilfs_write_file_data_binfo,
651 .write_node_binfo = NULL,
652};
653
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700654static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
655 struct list_head *listp,
656 size_t nlimit,
657 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700658{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700659 struct address_space *mapping = inode->i_mapping;
660 struct pagevec pvec;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700661 pgoff_t index = 0, last = ULONG_MAX;
662 size_t ndirties = 0;
663 int i;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700664
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700665 if (unlikely(start != 0 || end != LLONG_MAX)) {
666 /*
667 * A valid range is given for sync-ing data pages. The
668 * range is rounded to per-page; extra dirty buffers
669 * may be included if blocksize < pagesize.
670 */
671 index = start >> PAGE_SHIFT;
672 last = end >> PAGE_SHIFT;
673 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700674 pagevec_init(&pvec, 0);
675 repeat:
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700676 if (unlikely(index > last) ||
677 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
678 min_t(pgoff_t, last - index,
679 PAGEVEC_SIZE - 1) + 1))
680 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700681
682 for (i = 0; i < pagevec_count(&pvec); i++) {
683 struct buffer_head *bh, *head;
684 struct page *page = pvec.pages[i];
685
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700686 if (unlikely(page->index > last))
687 break;
688
Ryusuke Konishiaa405b12011-05-05 12:56:51 +0900689 lock_page(page);
690 if (!page_has_buffers(page))
691 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
692 unlock_page(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700693
694 bh = head = page_buffers(page);
695 do {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -0700696 if (!buffer_dirty(bh) || buffer_async_write(bh))
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700697 continue;
698 get_bh(bh);
699 list_add_tail(&bh->b_assoc_buffers, listp);
700 ndirties++;
701 if (unlikely(ndirties >= nlimit)) {
702 pagevec_release(&pvec);
703 cond_resched();
704 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700705 }
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700706 } while (bh = bh->b_this_page, bh != head);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700707 }
708 pagevec_release(&pvec);
709 cond_resched();
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700710 goto repeat;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700711}
712
713static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
714 struct list_head *listp)
715{
716 struct nilfs_inode_info *ii = NILFS_I(inode);
717 struct address_space *mapping = &ii->i_btnode_cache;
718 struct pagevec pvec;
719 struct buffer_head *bh, *head;
720 unsigned int i;
721 pgoff_t index = 0;
722
723 pagevec_init(&pvec, 0);
724
725 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
726 PAGEVEC_SIZE)) {
727 for (i = 0; i < pagevec_count(&pvec); i++) {
728 bh = head = page_buffers(pvec.pages[i]);
729 do {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -0700730 if (buffer_dirty(bh) &&
731 !buffer_async_write(bh)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700732 get_bh(bh);
733 list_add_tail(&bh->b_assoc_buffers,
734 listp);
735 }
736 bh = bh->b_this_page;
737 } while (bh != head);
738 }
739 pagevec_release(&pvec);
740 cond_resched();
741 }
742}
743
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900744static void nilfs_dispose_list(struct the_nilfs *nilfs,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700745 struct list_head *head, int force)
746{
747 struct nilfs_inode_info *ii, *n;
748 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
749 unsigned nv = 0;
750
751 while (!list_empty(head)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900752 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700753 list_for_each_entry_safe(ii, n, head, i_dirty) {
754 list_del_init(&ii->i_dirty);
755 if (force) {
756 if (unlikely(ii->i_bh)) {
757 brelse(ii->i_bh);
758 ii->i_bh = NULL;
759 }
760 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
761 set_bit(NILFS_I_QUEUED, &ii->i_state);
762 list_add_tail(&ii->i_dirty,
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900763 &nilfs->ns_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700764 continue;
765 }
766 ivec[nv++] = ii;
767 if (nv == SC_N_INODEVEC)
768 break;
769 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900770 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700771
772 for (pii = ivec; nv > 0; pii++, nv--)
773 iput(&(*pii)->vfs_inode);
774 }
775}
776
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -0800777static void nilfs_iput_work_func(struct work_struct *work)
778{
779 struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
780 sc_iput_work);
781 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
782
783 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
784}
785
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900786static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
787 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700788{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700789 int ret = 0;
790
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900791 if (nilfs_mdt_fetch_dirty(root->ifile))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700792 ret++;
793 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
794 ret++;
795 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
796 ret++;
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900797 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
798 ret++;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700799 return ret;
800}
801
802static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
803{
804 return list_empty(&sci->sc_dirty_files) &&
805 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900806 sci->sc_nfreesegs == 0 &&
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700807 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
808}
809
810static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
811{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900812 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700813 int ret = 0;
814
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900815 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700816 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
817
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900818 spin_lock(&nilfs->ns_inode_lock);
819 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700820 ret++;
821
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900822 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700823 return ret;
824}
825
826static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
827{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900828 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700829
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900830 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700831 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
832 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900833 nilfs_mdt_clear_dirty(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700834}
835
836static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
837{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900838 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700839 struct buffer_head *bh_cp;
840 struct nilfs_checkpoint *raw_cp;
841 int err;
842
843 /* XXX: this interface will be changed */
844 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
845 &raw_cp, &bh_cp);
846 if (likely(!err)) {
847 /* The following code is duplicated with cpfile. But, it is
848 needed to collect the checkpoint even if it was not newly
849 created */
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900850 mark_buffer_dirty(bh_cp);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700851 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
852 nilfs_cpfile_put_checkpoint(
853 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700854 } else
855 WARN_ON(err == -EINVAL || err == -ENOENT);
856
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700857 return err;
858}
859
860static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
861{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900862 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700863 struct buffer_head *bh_cp;
864 struct nilfs_checkpoint *raw_cp;
865 int err;
866
867 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
868 &raw_cp, &bh_cp);
869 if (unlikely(err)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700870 WARN_ON(err == -EINVAL || err == -ENOENT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700871 goto failed_ibh;
872 }
873 raw_cp->cp_snapshot_list.ssl_next = 0;
874 raw_cp->cp_snapshot_list.ssl_prev = 0;
875 raw_cp->cp_inodes_count =
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700876 cpu_to_le64(atomic64_read(&sci->sc_root->inodes_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700877 raw_cp->cp_blocks_count =
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700878 cpu_to_le64(atomic64_read(&sci->sc_root->blocks_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700879 raw_cp->cp_nblk_inc =
880 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
881 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
882 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700883
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700884 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
885 nilfs_checkpoint_clear_minor(raw_cp);
886 else
887 nilfs_checkpoint_set_minor(raw_cp);
888
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900889 nilfs_write_inode_common(sci->sc_root->ifile,
890 &raw_cp->cp_ifile_inode, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700891 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
892 return 0;
893
894 failed_ibh:
895 return err;
896}
897
898static void nilfs_fill_in_file_bmap(struct inode *ifile,
899 struct nilfs_inode_info *ii)
900
901{
902 struct buffer_head *ibh;
903 struct nilfs_inode *raw_inode;
904
905 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
906 ibh = ii->i_bh;
907 BUG_ON(!ibh);
908 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
909 ibh);
910 nilfs_bmap_write(ii->i_bmap, raw_inode);
911 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
912 }
913}
914
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900915static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700916{
917 struct nilfs_inode_info *ii;
918
919 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900920 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700921 set_bit(NILFS_I_COLLECTED, &ii->i_state);
922 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700923}
924
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700925static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
926 struct the_nilfs *nilfs)
927{
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900928 struct buffer_head *bh_sr;
929 struct nilfs_super_root *raw_sr;
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900930 unsigned isz, srsz;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700931
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900932 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
933 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900934 isz = nilfs->ns_inode_size;
935 srsz = NILFS_SR_BYTES(isz);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900936
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900937 raw_sr->sr_bytes = cpu_to_le16(srsz);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700938 raw_sr->sr_nongc_ctime
939 = cpu_to_le64(nilfs_doing_gc() ?
940 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
941 raw_sr->sr_flags = 0;
942
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900943 nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr +
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900944 NILFS_SR_DAT_OFFSET(isz), 1);
945 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
946 NILFS_SR_CPFILE_OFFSET(isz), 1);
947 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
948 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900949 memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700950}
951
952static void nilfs_redirty_inodes(struct list_head *head)
953{
954 struct nilfs_inode_info *ii;
955
956 list_for_each_entry(ii, head, i_dirty) {
957 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
958 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
959 }
960}
961
962static void nilfs_drop_collected_inodes(struct list_head *head)
963{
964 struct nilfs_inode_info *ii;
965
966 list_for_each_entry(ii, head, i_dirty) {
967 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
968 continue;
969
Andreas Rohnerb9f66142014-10-13 15:53:22 -0700970 clear_bit(NILFS_I_INODE_SYNC, &ii->i_state);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700971 set_bit(NILFS_I_UPDATED, &ii->i_state);
972 }
973}
974
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700975static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
976 struct inode *inode,
977 struct list_head *listp,
978 int (*collect)(struct nilfs_sc_info *,
979 struct buffer_head *,
980 struct inode *))
981{
982 struct buffer_head *bh, *n;
983 int err = 0;
984
985 if (collect) {
986 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
987 list_del_init(&bh->b_assoc_buffers);
988 err = collect(sci, bh, inode);
989 brelse(bh);
990 if (unlikely(err))
991 goto dispose_buffers;
992 }
993 return 0;
994 }
995
996 dispose_buffers:
997 while (!list_empty(listp)) {
Ryusuke Konishi0cc1283882011-05-05 12:56:51 +0900998 bh = list_first_entry(listp, struct buffer_head,
999 b_assoc_buffers);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001000 list_del_init(&bh->b_assoc_buffers);
1001 brelse(bh);
1002 }
1003 return err;
1004}
1005
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001006static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1007{
1008 /* Remaining number of blocks within segment buffer */
1009 return sci->sc_segbuf_nblocks -
1010 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1011}
1012
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001013static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1014 struct inode *inode,
1015 struct nilfs_sc_operations *sc_ops)
1016{
1017 LIST_HEAD(data_buffers);
1018 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001019 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001020
1021 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001022 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1023
1024 n = nilfs_lookup_dirty_data_buffers(
1025 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1026 if (n > rest) {
1027 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001028 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001029 sc_ops->collect_data);
1030 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001031 goto break_or_fail;
1032 }
1033 }
1034 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1035
1036 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1037 err = nilfs_segctor_apply_buffers(
1038 sci, inode, &data_buffers, sc_ops->collect_data);
1039 if (unlikely(err)) {
1040 /* dispose node list */
1041 nilfs_segctor_apply_buffers(
1042 sci, inode, &node_buffers, NULL);
1043 goto break_or_fail;
1044 }
1045 sci->sc_stage.flags |= NILFS_CF_NODE;
1046 }
1047 /* Collect node */
1048 err = nilfs_segctor_apply_buffers(
1049 sci, inode, &node_buffers, sc_ops->collect_node);
1050 if (unlikely(err))
1051 goto break_or_fail;
1052
1053 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1054 err = nilfs_segctor_apply_buffers(
1055 sci, inode, &node_buffers, sc_ops->collect_bmap);
1056 if (unlikely(err))
1057 goto break_or_fail;
1058
1059 nilfs_segctor_end_finfo(sci, inode);
1060 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1061
1062 break_or_fail:
1063 return err;
1064}
1065
1066static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1067 struct inode *inode)
1068{
1069 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001070 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1071 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001072
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001073 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1074 sci->sc_dsync_start,
1075 sci->sc_dsync_end);
1076
1077 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1078 nilfs_collect_file_data);
1079 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001080 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001081 BUG_ON(n > rest);
1082 /* always receive -E2BIG or true error if n > rest */
1083 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001084 return err;
1085}
1086
1087static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1088{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001089 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001090 struct list_head *head;
1091 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001092 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001093 int err = 0;
1094
Hitoshi Mitake58497702015-11-06 16:31:59 -08001095 switch (nilfs_sc_cstage_get(sci)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001096 case NILFS_ST_INIT:
1097 /* Pre-processes */
1098 sci->sc_stage.flags = 0;
1099
1100 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1101 sci->sc_nblk_inc = 0;
1102 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1103 if (mode == SC_LSEG_DSYNC) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001104 nilfs_sc_cstage_set(sci, NILFS_ST_DSYNC);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001105 goto dsync_mode;
1106 }
1107 }
1108
1109 sci->sc_stage.dirty_file_ptr = NULL;
1110 sci->sc_stage.gc_inode_ptr = NULL;
1111 if (mode == SC_FLUSH_DAT) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001112 nilfs_sc_cstage_set(sci, NILFS_ST_DAT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001113 goto dat_stage;
1114 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001115 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001116 case NILFS_ST_GC:
1117 if (nilfs_doing_gc()) {
1118 head = &sci->sc_gc_inodes;
1119 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1120 head, i_dirty);
1121 list_for_each_entry_continue(ii, head, i_dirty) {
1122 err = nilfs_segctor_scan_file(
1123 sci, &ii->vfs_inode,
1124 &nilfs_sc_file_ops);
1125 if (unlikely(err)) {
1126 sci->sc_stage.gc_inode_ptr = list_entry(
1127 ii->i_dirty.prev,
1128 struct nilfs_inode_info,
1129 i_dirty);
1130 goto break_or_fail;
1131 }
1132 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1133 }
1134 sci->sc_stage.gc_inode_ptr = NULL;
1135 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001136 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001137 case NILFS_ST_FILE:
1138 head = &sci->sc_dirty_files;
1139 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1140 i_dirty);
1141 list_for_each_entry_continue(ii, head, i_dirty) {
1142 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1143
1144 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1145 &nilfs_sc_file_ops);
1146 if (unlikely(err)) {
1147 sci->sc_stage.dirty_file_ptr =
1148 list_entry(ii->i_dirty.prev,
1149 struct nilfs_inode_info,
1150 i_dirty);
1151 goto break_or_fail;
1152 }
1153 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1154 /* XXX: required ? */
1155 }
1156 sci->sc_stage.dirty_file_ptr = NULL;
1157 if (mode == SC_FLUSH_FILE) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001158 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001159 return 0;
1160 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001161 nilfs_sc_cstage_inc(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001162 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1163 /* Fall through */
1164 case NILFS_ST_IFILE:
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001165 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001166 &nilfs_sc_file_ops);
1167 if (unlikely(err))
1168 break;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001169 nilfs_sc_cstage_inc(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001170 /* Creating a checkpoint */
1171 err = nilfs_segctor_create_checkpoint(sci);
1172 if (unlikely(err))
1173 break;
1174 /* Fall through */
1175 case NILFS_ST_CPFILE:
1176 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1177 &nilfs_sc_file_ops);
1178 if (unlikely(err))
1179 break;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001180 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001181 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001182 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1183 sci->sc_nfreesegs, &ndone);
1184 if (unlikely(err)) {
1185 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1186 sci->sc_freesegs, ndone,
1187 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001188 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001189 }
1190 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1191
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001192 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1193 &nilfs_sc_file_ops);
1194 if (unlikely(err))
1195 break;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001196 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001197 case NILFS_ST_DAT:
1198 dat_stage:
Ryusuke Konishi365e2152010-12-27 00:07:30 +09001199 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001200 &nilfs_sc_dat_ops);
1201 if (unlikely(err))
1202 break;
1203 if (mode == SC_FLUSH_DAT) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001204 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001205 return 0;
1206 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001207 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001208 case NILFS_ST_SR:
1209 if (mode == SC_LSEG_SR) {
1210 /* Appending a super root */
1211 err = nilfs_segctor_add_super_root(sci);
1212 if (unlikely(err))
1213 break;
1214 }
1215 /* End of a logical segment */
1216 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001217 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001218 return 0;
1219 case NILFS_ST_DSYNC:
1220 dsync_mode:
1221 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001222 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001223 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1224 break;
1225
1226 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1227 if (unlikely(err))
1228 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001229 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001230 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001231 return 0;
1232 case NILFS_ST_DONE:
1233 return 0;
1234 default:
1235 BUG();
1236 }
1237
1238 break_or_fail:
1239 return err;
1240}
1241
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001242/**
1243 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1244 * @sci: nilfs_sc_info
1245 * @nilfs: nilfs object
1246 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001247static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1248 struct the_nilfs *nilfs)
1249{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001250 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001251 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001252 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001253
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001254 segbuf = nilfs_segbuf_new(sci->sc_super);
1255 if (unlikely(!segbuf))
1256 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001257
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001258 if (list_empty(&sci->sc_write_logs)) {
1259 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1260 nilfs->ns_pseg_offset, nilfs);
1261 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1262 nilfs_shift_to_next_segment(nilfs);
1263 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1264 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001265
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001266 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001267 nextnum = nilfs->ns_nextnum;
1268
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001269 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1270 /* Start from the head of a new full segment */
1271 alloc++;
1272 } else {
1273 /* Continue logs */
1274 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1275 nilfs_segbuf_map_cont(segbuf, prev);
1276 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1277 nextnum = prev->sb_nextnum;
1278
1279 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1280 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1281 segbuf->sb_sum.seg_seq++;
1282 alloc++;
1283 }
1284 }
1285
1286 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1287 if (err)
1288 goto failed;
1289
1290 if (alloc) {
1291 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1292 if (err)
1293 goto failed;
1294 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001295 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1296
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001297 BUG_ON(!list_empty(&sci->sc_segbufs));
1298 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1299 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001300 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001301
1302 failed:
1303 nilfs_segbuf_free(segbuf);
1304 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001305}
1306
1307static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1308 struct the_nilfs *nilfs, int nadd)
1309{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001310 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001311 struct inode *sufile = nilfs->ns_sufile;
1312 __u64 nextnextnum;
1313 LIST_HEAD(list);
1314 int err, ret, i;
1315
1316 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1317 /*
1318 * Since the segment specified with nextnum might be allocated during
1319 * the previous construction, the buffer including its segusage may
1320 * not be dirty. The following call ensures that the buffer is dirty
1321 * and will pin the buffer on memory until the sufile is written.
1322 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001323 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001324 if (unlikely(err))
1325 return err;
1326
1327 for (i = 0; i < nadd; i++) {
1328 /* extend segment info */
1329 err = -ENOMEM;
1330 segbuf = nilfs_segbuf_new(sci->sc_super);
1331 if (unlikely(!segbuf))
1332 goto failed;
1333
1334 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001335 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001336 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1337
1338 /* allocate the next next full segment */
1339 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1340 if (unlikely(err))
1341 goto failed_segbuf;
1342
1343 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1344 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1345
1346 list_add_tail(&segbuf->sb_list, &list);
1347 prev = segbuf;
1348 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001349 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001350 return 0;
1351
1352 failed_segbuf:
1353 nilfs_segbuf_free(segbuf);
1354 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001355 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001356 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001357 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001358 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001359 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001360 return err;
1361}
1362
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001363static void nilfs_free_incomplete_logs(struct list_head *logs,
1364 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001365{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001366 struct nilfs_segment_buffer *segbuf, *prev;
1367 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001368 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001369
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001370 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001371 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001372 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001373 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001374 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001375 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001376 /* Case 1: The first segment failed */
1377 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1378 /* Case 1a: Partial segment appended into an existing
1379 segment */
1380 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1381 segbuf->sb_fseg_end);
1382 else /* Case 1b: New full segment */
1383 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001384 }
1385
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001386 prev = segbuf;
1387 list_for_each_entry_continue(segbuf, logs, sb_list) {
1388 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1389 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1390 WARN_ON(ret); /* never fails */
1391 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001392 if (atomic_read(&segbuf->sb_err) &&
1393 segbuf->sb_segnum != nilfs->ns_nextnum)
1394 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001395 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1396 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001397 }
1398}
1399
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001400static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1401 struct inode *sufile)
1402{
1403 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001404 unsigned long live_blocks;
1405 int ret;
1406
1407 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001408 live_blocks = segbuf->sb_sum.nblocks +
1409 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001410 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1411 live_blocks,
1412 sci->sc_seg_ctime);
1413 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001414 }
1415}
1416
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001417static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001418{
1419 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001420 int ret;
1421
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001422 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001423 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1424 segbuf->sb_pseg_start -
1425 segbuf->sb_fseg_start, 0);
1426 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001427
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001428 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001429 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1430 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001431 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001432 }
1433}
1434
1435static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1436 struct nilfs_segment_buffer *last,
1437 struct inode *sufile)
1438{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001439 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001440 int ret;
1441
Ryusuke Konishie29df392009-11-29 16:51:16 +09001442 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001443 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1444 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001445 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001446 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001447 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001448}
1449
1450
1451static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1452 struct the_nilfs *nilfs, int mode)
1453{
1454 struct nilfs_cstage prev_stage = sci->sc_stage;
1455 int err, nadd = 1;
1456
1457 /* Collection retry loop */
1458 for (;;) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001459 sci->sc_nblk_this_inc = 0;
1460 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1461
1462 err = nilfs_segctor_reset_segment_buffer(sci);
1463 if (unlikely(err))
1464 goto failed;
1465
1466 err = nilfs_segctor_collect_blocks(sci, mode);
1467 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1468 if (!err)
1469 break;
1470
1471 if (unlikely(err != -E2BIG))
1472 goto failed;
1473
1474 /* The current segment is filled up */
Hitoshi Mitake58497702015-11-06 16:31:59 -08001475 if (mode != SC_LSEG_SR ||
1476 nilfs_sc_cstage_get(sci) < NILFS_ST_CPFILE)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001477 break;
1478
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001479 nilfs_clear_logs(&sci->sc_segbufs);
1480
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001481 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1482 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1483 sci->sc_freesegs,
1484 sci->sc_nfreesegs,
1485 NULL);
1486 WARN_ON(err); /* do not happen */
Andreas Rohner70f2fe32014-01-14 17:56:36 -08001487 sci->sc_stage.flags &= ~NILFS_CF_SUFREED;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001488 }
Andreas Rohner70f2fe32014-01-14 17:56:36 -08001489
1490 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1491 if (unlikely(err))
1492 return err;
1493
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001494 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1495 sci->sc_stage = prev_stage;
1496 }
1497 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1498 return 0;
1499
1500 failed:
1501 return err;
1502}
1503
1504static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1505 struct buffer_head *new_bh)
1506{
1507 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1508
1509 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1510 /* The caller must release old_bh */
1511}
1512
1513static int
1514nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1515 struct nilfs_segment_buffer *segbuf,
1516 int mode)
1517{
1518 struct inode *inode = NULL;
1519 sector_t blocknr;
1520 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1521 unsigned long nblocks = 0, ndatablk = 0;
1522 struct nilfs_sc_operations *sc_op = NULL;
1523 struct nilfs_segsum_pointer ssp;
1524 struct nilfs_finfo *finfo = NULL;
1525 union nilfs_binfo binfo;
1526 struct buffer_head *bh, *bh_org;
1527 ino_t ino = 0;
1528 int err = 0;
1529
1530 if (!nfinfo)
1531 goto out;
1532
1533 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1534 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1535 ssp.offset = sizeof(struct nilfs_segment_summary);
1536
1537 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001538 if (bh == segbuf->sb_super_root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001539 break;
1540 if (!finfo) {
1541 finfo = nilfs_segctor_map_segsum_entry(
1542 sci, &ssp, sizeof(*finfo));
1543 ino = le64_to_cpu(finfo->fi_ino);
1544 nblocks = le32_to_cpu(finfo->fi_nblocks);
1545 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1546
Ryusuke Konishiaa405b12011-05-05 12:56:51 +09001547 inode = bh->b_page->mapping->host;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001548
1549 if (mode == SC_LSEG_DSYNC)
1550 sc_op = &nilfs_sc_dsync_ops;
1551 else if (ino == NILFS_DAT_INO)
1552 sc_op = &nilfs_sc_dat_ops;
1553 else /* file blocks */
1554 sc_op = &nilfs_sc_file_ops;
1555 }
1556 bh_org = bh;
1557 get_bh(bh_org);
1558 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1559 &binfo);
1560 if (bh != bh_org)
1561 nilfs_list_replace_buffer(bh_org, bh);
1562 brelse(bh_org);
1563 if (unlikely(err))
1564 goto failed_bmap;
1565
1566 if (ndatablk > 0)
1567 sc_op->write_data_binfo(sci, &ssp, &binfo);
1568 else
1569 sc_op->write_node_binfo(sci, &ssp, &binfo);
1570
1571 blocknr++;
1572 if (--nblocks == 0) {
1573 finfo = NULL;
1574 if (--nfinfo == 0)
1575 break;
1576 } else if (ndatablk > 0)
1577 ndatablk--;
1578 }
1579 out:
1580 return 0;
1581
1582 failed_bmap:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001583 return err;
1584}
1585
1586static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1587{
1588 struct nilfs_segment_buffer *segbuf;
1589 int err;
1590
1591 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1592 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1593 if (unlikely(err))
1594 return err;
1595 nilfs_segbuf_fill_in_segsum(segbuf);
1596 }
1597 return 0;
1598}
1599
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001600static void nilfs_begin_page_io(struct page *page)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001601{
1602 if (!page || PageWriteback(page))
1603 /* For split b-tree node pages, this function may be called
1604 twice. We ignore the 2nd or later calls by this check. */
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001605 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001606
1607 lock_page(page);
1608 clear_page_dirty_for_io(page);
1609 set_page_writeback(page);
1610 unlock_page(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001611}
1612
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001613static void nilfs_segctor_prepare_write(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001614{
1615 struct nilfs_segment_buffer *segbuf;
1616 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001617
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001618 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1619 struct buffer_head *bh;
1620
1621 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1622 b_assoc_buffers) {
1623 if (bh->b_page != bd_page) {
1624 if (bd_page) {
1625 lock_page(bd_page);
1626 clear_page_dirty_for_io(bd_page);
1627 set_page_writeback(bd_page);
1628 unlock_page(bd_page);
1629 }
1630 bd_page = bh->b_page;
1631 }
1632 }
1633
1634 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1635 b_assoc_buffers) {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -07001636 set_buffer_async_write(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001637 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001638 if (bh->b_page != bd_page) {
1639 lock_page(bd_page);
1640 clear_page_dirty_for_io(bd_page);
1641 set_page_writeback(bd_page);
1642 unlock_page(bd_page);
1643 bd_page = bh->b_page;
1644 }
1645 break;
1646 }
1647 if (bh->b_page != fs_page) {
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001648 nilfs_begin_page_io(fs_page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001649 fs_page = bh->b_page;
1650 }
1651 }
1652 }
1653 if (bd_page) {
1654 lock_page(bd_page);
1655 clear_page_dirty_for_io(bd_page);
1656 set_page_writeback(bd_page);
1657 unlock_page(bd_page);
1658 }
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001659 nilfs_begin_page_io(fs_page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001660}
1661
1662static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001663 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001664{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001665 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001666
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001667 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001668 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1669 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001670}
1671
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001672static void nilfs_end_page_io(struct page *page, int err)
1673{
1674 if (!page)
1675 return;
1676
Ryusuke Konishia9777842009-07-28 17:55:29 +09001677 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001678 /*
1679 * For b-tree node pages, this function may be called twice
1680 * or more because they might be split in a segment.
1681 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001682 if (PageDirty(page)) {
1683 /*
1684 * For pages holding split b-tree node buffers, dirty
1685 * flag on the buffers may be cleared discretely.
1686 * In that case, the page is once redirtied for
1687 * remaining buffers, and it must be cancelled if
1688 * all the buffers get cleaned later.
1689 */
1690 lock_page(page);
1691 if (nilfs_page_buffers_clean(page))
1692 __nilfs_clear_page_dirty(page);
1693 unlock_page(page);
1694 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001695 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001696 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001697
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001698 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);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001705 }
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001706
1707 end_page_writeback(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001708}
1709
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001710static void nilfs_abort_logs(struct list_head *logs, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001711{
1712 struct nilfs_segment_buffer *segbuf;
1713 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001714 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001715
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001716 if (list_empty(logs))
1717 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001718
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001719 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001720 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1721 b_assoc_buffers) {
1722 if (bh->b_page != bd_page) {
1723 if (bd_page)
1724 end_page_writeback(bd_page);
1725 bd_page = bh->b_page;
1726 }
1727 }
1728
1729 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1730 b_assoc_buffers) {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -07001731 clear_buffer_async_write(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001732 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001733 if (bh->b_page != bd_page) {
1734 end_page_writeback(bd_page);
1735 bd_page = bh->b_page;
1736 }
1737 break;
1738 }
1739 if (bh->b_page != fs_page) {
1740 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001741 fs_page = bh->b_page;
1742 }
1743 }
1744 }
1745 if (bd_page)
1746 end_page_writeback(bd_page);
1747
1748 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001749}
1750
1751static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1752 struct the_nilfs *nilfs, int err)
1753{
1754 LIST_HEAD(logs);
1755 int ret;
1756
1757 list_splice_tail_init(&sci->sc_write_logs, &logs);
1758 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001759 nilfs_abort_logs(&logs, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001760
1761 list_splice_tail_init(&sci->sc_segbufs, &logs);
1762 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1763 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001764
1765 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1766 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1767 sci->sc_freesegs,
1768 sci->sc_nfreesegs,
1769 NULL);
1770 WARN_ON(ret); /* do not happen */
1771 }
1772
1773 nilfs_destroy_logs(&logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001774}
1775
1776static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1777 struct nilfs_segment_buffer *segbuf)
1778{
1779 nilfs->ns_segnum = segbuf->sb_segnum;
1780 nilfs->ns_nextnum = segbuf->sb_nextnum;
1781 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1782 + segbuf->sb_sum.nblocks;
1783 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1784 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1785}
1786
1787static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1788{
1789 struct nilfs_segment_buffer *segbuf;
1790 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001791 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001792 int update_sr = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001793
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001794 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001795 struct buffer_head *bh;
1796
1797 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1798 b_assoc_buffers) {
1799 set_buffer_uptodate(bh);
1800 clear_buffer_dirty(bh);
1801 if (bh->b_page != bd_page) {
1802 if (bd_page)
1803 end_page_writeback(bd_page);
1804 bd_page = bh->b_page;
1805 }
1806 }
1807 /*
1808 * We assume that the buffers which belong to the same page
1809 * continue over the buffer list.
1810 * Under this assumption, the last BHs of pages is
1811 * identifiable by the discontinuity of bh->b_page
1812 * (page != fs_page).
1813 *
1814 * For B-tree node blocks, however, this assumption is not
1815 * guaranteed. The cleanup code of B-tree node pages needs
1816 * special care.
1817 */
1818 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1819 b_assoc_buffers) {
Ryusuke Konishiead8ecf2015-04-16 12:46:28 -07001820 const unsigned long set_bits = (1 << BH_Uptodate);
1821 const unsigned long clear_bits =
1822 (1 << BH_Dirty | 1 << BH_Async_Write |
1823 1 << BH_Delay | 1 << BH_NILFS_Volatile |
1824 1 << BH_NILFS_Redirected);
1825
1826 set_mask_bits(&bh->b_state, clear_bits, set_bits);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001827 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001828 if (bh->b_page != bd_page) {
1829 end_page_writeback(bd_page);
1830 bd_page = bh->b_page;
1831 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001832 update_sr = true;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001833 break;
1834 }
1835 if (bh->b_page != fs_page) {
1836 nilfs_end_page_io(fs_page, 0);
1837 fs_page = bh->b_page;
1838 }
1839 }
1840
Ryusuke Konishi47620772010-05-23 21:48:36 +09001841 if (!nilfs_segbuf_simplex(segbuf)) {
1842 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001843 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1844 sci->sc_lseg_stime = jiffies;
1845 }
Ryusuke Konishi47620772010-05-23 21:48:36 +09001846 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001847 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1848 }
1849 }
1850 /*
1851 * Since pages may continue over multiple segment buffers,
1852 * end of the last page must be checked outside of the loop.
1853 */
1854 if (bd_page)
1855 end_page_writeback(bd_page);
1856
1857 nilfs_end_page_io(fs_page, 0);
1858
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001859 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1860
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001861 if (nilfs_doing_gc())
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001862 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001863 else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001864 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001865
1866 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1867
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001868 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001869 nilfs_set_next_segment(nilfs, segbuf);
1870
1871 if (update_sr) {
Andreas Rohnere2c76172014-10-13 15:53:20 -07001872 nilfs->ns_flushed_device = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001873 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07001874 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001875
Ryusuke Konishic96fa462009-04-06 19:01:57 -07001876 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001877 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1878 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001879 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001880 } else
1881 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1882}
1883
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001884static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1885{
1886 int ret;
1887
1888 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1889 if (!ret) {
1890 nilfs_segctor_complete_write(sci);
1891 nilfs_destroy_logs(&sci->sc_write_logs);
1892 }
1893 return ret;
1894}
1895
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001896static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
1897 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001898{
1899 struct nilfs_inode_info *ii, *n;
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001900 struct inode *ifile = sci->sc_root->ifile;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001901
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001902 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001903 retry:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001904 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001905 if (!ii->i_bh) {
1906 struct buffer_head *ibh;
1907 int err;
1908
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001909 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001910 err = nilfs_ifile_get_inode_block(
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001911 ifile, ii->vfs_inode.i_ino, &ibh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001912 if (unlikely(err)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001913 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001914 "failed to get inode block.\n");
1915 return err;
1916 }
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001917 mark_buffer_dirty(ibh);
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001918 nilfs_mdt_mark_dirty(ifile);
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001919 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001920 if (likely(!ii->i_bh))
1921 ii->i_bh = ibh;
1922 else
1923 brelse(ibh);
1924 goto retry;
1925 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001926
1927 clear_bit(NILFS_I_QUEUED, &ii->i_state);
1928 set_bit(NILFS_I_BUSY, &ii->i_state);
Nicolas Kaisereaae0f32011-03-19 16:45:30 +01001929 list_move_tail(&ii->i_dirty, &sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001930 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001931 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001932
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001933 return 0;
1934}
1935
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001936static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
1937 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001938{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001939 struct nilfs_inode_info *ii, *n;
Ryusuke Konishi283ee1482015-03-12 16:26:00 -07001940 int during_mount = !(sci->sc_super->s_flags & MS_ACTIVE);
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001941 int defer_iput = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001942
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001943 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001944 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
1945 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09001946 test_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001947 continue;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09001948
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001949 clear_bit(NILFS_I_BUSY, &ii->i_state);
1950 brelse(ii->i_bh);
1951 ii->i_bh = NULL;
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001952 list_del_init(&ii->i_dirty);
Ryusuke Konishi283ee1482015-03-12 16:26:00 -07001953 if (!ii->vfs_inode.i_nlink || during_mount) {
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001954 /*
Ryusuke Konishi283ee1482015-03-12 16:26:00 -07001955 * Defer calling iput() to avoid deadlocks if
1956 * i_nlink == 0 or mount is not yet finished.
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001957 */
1958 list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
1959 defer_iput = true;
1960 } else {
1961 spin_unlock(&nilfs->ns_inode_lock);
1962 iput(&ii->vfs_inode);
1963 spin_lock(&nilfs->ns_inode_lock);
1964 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001965 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001966 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001967
1968 if (defer_iput)
1969 schedule_work(&sci->sc_iput_work);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001970}
1971
1972/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001973 * Main procedure of segment constructor
1974 */
1975static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
1976{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001977 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001978 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001979
Hitoshi Mitake58497702015-11-06 16:31:59 -08001980 nilfs_sc_cstage_set(sci, NILFS_ST_INIT);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09001981 sci->sc_cno = nilfs->ns_cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001982
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001983 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001984 if (unlikely(err))
1985 goto out;
1986
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001987 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001988 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1989
1990 if (nilfs_segctor_clean(sci))
1991 goto out;
1992
1993 do {
1994 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
1995
1996 err = nilfs_segctor_begin_construction(sci, nilfs);
1997 if (unlikely(err))
1998 goto out;
1999
2000 /* Update time stamp */
2001 sci->sc_seg_ctime = get_seconds();
2002
2003 err = nilfs_segctor_collect(sci, nilfs, mode);
2004 if (unlikely(err))
2005 goto failed;
2006
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002007 /* Avoid empty segment */
Hitoshi Mitake58497702015-11-06 16:31:59 -08002008 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE &&
Ryusuke Konishi47620772010-05-23 21:48:36 +09002009 nilfs_segbuf_empty(sci->sc_curseg)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002010 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002011 goto out;
2012 }
2013
2014 err = nilfs_segctor_assign(sci, mode);
2015 if (unlikely(err))
2016 goto failed;
2017
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002018 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002019 nilfs_segctor_fill_in_file_bmap(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002020
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002021 if (mode == SC_LSEG_SR &&
Hitoshi Mitake58497702015-11-06 16:31:59 -08002022 nilfs_sc_cstage_get(sci) >= NILFS_ST_CPFILE) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002023 err = nilfs_segctor_fill_in_checkpoint(sci);
2024 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002025 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002026
2027 nilfs_segctor_fill_in_super_root(sci, nilfs);
2028 }
2029 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2030
2031 /* Write partial segments */
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09002032 nilfs_segctor_prepare_write(sci);
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +09002033
2034 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2035 nilfs->ns_crc_seed);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002036
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002037 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002038 if (unlikely(err))
2039 goto failed_to_write;
2040
Hitoshi Mitake58497702015-11-06 16:31:59 -08002041 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE ||
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002042 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2043 /*
2044 * At this point, we avoid double buffering
2045 * for blocksize < pagesize because page dirty
2046 * flag is turned off during write and dirty
2047 * buffers are not properly collected for
2048 * pages crossing over segments.
2049 */
2050 err = nilfs_segctor_wait(sci);
2051 if (err)
2052 goto failed_to_write;
2053 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08002054 } while (nilfs_sc_cstage_get(sci) != NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002055
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002056 out:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002057 nilfs_segctor_drop_written_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002058 return err;
2059
2060 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002061 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2062 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002063
2064 failed:
2065 if (nilfs_doing_gc())
2066 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002067 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002068 goto out;
2069}
2070
2071/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002072 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002073 * @sci: nilfs_sc_info
2074 *
2075 * If the timer has already been set, it ignores the new request.
2076 * This function MUST be called within a section locking the segment
2077 * semaphore.
2078 */
2079static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2080{
2081 spin_lock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002082 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2083 sci->sc_timer.expires = jiffies + sci->sc_interval;
2084 add_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002085 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2086 }
2087 spin_unlock(&sci->sc_state_lock);
2088}
2089
2090static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2091{
2092 spin_lock(&sci->sc_state_lock);
2093 if (!(sci->sc_flush_request & (1 << bn))) {
2094 unsigned long prev_req = sci->sc_flush_request;
2095
2096 sci->sc_flush_request |= (1 << bn);
2097 if (!prev_req)
2098 wake_up(&sci->sc_wait_daemon);
2099 }
2100 spin_unlock(&sci->sc_state_lock);
2101}
2102
2103/**
2104 * nilfs_flush_segment - trigger a segment construction for resource control
2105 * @sb: super block
2106 * @ino: inode number of the file to be flushed out.
2107 */
2108void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2109{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002110 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002111 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002112
2113 if (!sci || nilfs_doing_construction())
2114 return;
2115 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2116 /* assign bit 0 to data files */
2117}
2118
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002119struct nilfs_segctor_wait_request {
2120 wait_queue_t wq;
2121 __u32 seq;
2122 int err;
2123 atomic_t done;
2124};
2125
2126static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2127{
2128 struct nilfs_segctor_wait_request wait_req;
2129 int err = 0;
2130
2131 spin_lock(&sci->sc_state_lock);
2132 init_wait(&wait_req.wq);
2133 wait_req.err = 0;
2134 atomic_set(&wait_req.done, 0);
2135 wait_req.seq = ++sci->sc_seq_request;
2136 spin_unlock(&sci->sc_state_lock);
2137
2138 init_waitqueue_entry(&wait_req.wq, current);
2139 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2140 set_current_state(TASK_INTERRUPTIBLE);
2141 wake_up(&sci->sc_wait_daemon);
2142
2143 for (;;) {
2144 if (atomic_read(&wait_req.done)) {
2145 err = wait_req.err;
2146 break;
2147 }
2148 if (!signal_pending(current)) {
2149 schedule();
2150 continue;
2151 }
2152 err = -ERESTARTSYS;
2153 break;
2154 }
2155 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2156 return err;
2157}
2158
2159static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2160{
2161 struct nilfs_segctor_wait_request *wrq, *n;
2162 unsigned long flags;
2163
2164 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2165 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2166 wq.task_list) {
2167 if (!atomic_read(&wrq->done) &&
2168 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2169 wrq->err = err;
2170 atomic_set(&wrq->done, 1);
2171 }
2172 if (atomic_read(&wrq->done)) {
2173 wrq->wq.func(&wrq->wq,
2174 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2175 0, NULL);
2176 }
2177 }
2178 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2179}
2180
2181/**
2182 * nilfs_construct_segment - construct a logical segment
2183 * @sb: super block
2184 *
2185 * Return Value: On success, 0 is retured. On errors, one of the following
2186 * negative error code is returned.
2187 *
2188 * %-EROFS - Read only filesystem.
2189 *
2190 * %-EIO - I/O error
2191 *
2192 * %-ENOSPC - No space left on device (only in a panic state).
2193 *
2194 * %-ERESTARTSYS - Interrupted.
2195 *
2196 * %-ENOMEM - Insufficient memory available.
2197 */
2198int nilfs_construct_segment(struct super_block *sb)
2199{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002200 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002201 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002202 struct nilfs_transaction_info *ti;
2203 int err;
2204
2205 if (!sci)
2206 return -EROFS;
2207
2208 /* A call inside transactions causes a deadlock. */
2209 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2210
2211 err = nilfs_segctor_sync(sci);
2212 return err;
2213}
2214
2215/**
2216 * nilfs_construct_dsync_segment - construct a data-only logical segment
2217 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002218 * @inode: inode whose data blocks should be written out
2219 * @start: start byte offset
2220 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002221 *
2222 * Return Value: On success, 0 is retured. On errors, one of the following
2223 * negative error code is returned.
2224 *
2225 * %-EROFS - Read only filesystem.
2226 *
2227 * %-EIO - I/O error
2228 *
2229 * %-ENOSPC - No space left on device (only in a panic state).
2230 *
2231 * %-ERESTARTSYS - Interrupted.
2232 *
2233 * %-ENOMEM - Insufficient memory available.
2234 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002235int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2236 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002237{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002238 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002239 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002240 struct nilfs_inode_info *ii;
2241 struct nilfs_transaction_info ti;
2242 int err = 0;
2243
2244 if (!sci)
2245 return -EROFS;
2246
Ryusuke Konishif7545142011-03-09 11:05:08 +09002247 nilfs_transaction_lock(sb, &ti, 0);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002248
2249 ii = NILFS_I(inode);
Andreas Rohnerb9f66142014-10-13 15:53:22 -07002250 if (test_bit(NILFS_I_INODE_SYNC, &ii->i_state) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002251 nilfs_test_opt(nilfs, STRICT_ORDER) ||
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002252 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002253 nilfs_discontinued(nilfs)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09002254 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002255 err = nilfs_segctor_sync(sci);
2256 return err;
2257 }
2258
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002259 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002260 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2261 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002262 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002263 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002264 return 0;
2265 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002266 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002267 sci->sc_dsync_inode = ii;
2268 sci->sc_dsync_start = start;
2269 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002270
2271 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
Andreas Rohnere2c76172014-10-13 15:53:20 -07002272 if (!err)
2273 nilfs->ns_flushed_device = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002274
Ryusuke Konishif7545142011-03-09 11:05:08 +09002275 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002276 return err;
2277}
2278
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002279#define FLUSH_FILE_BIT (0x1) /* data file only */
2280#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2281
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002282/**
2283 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2284 * @sci: segment constructor object
2285 */
2286static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002287{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002288 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002289 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002290 spin_unlock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002291 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002292}
2293
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002294/**
2295 * nilfs_segctor_notify - notify the result of request to caller threads
2296 * @sci: segment constructor object
2297 * @mode: mode of log forming
2298 * @err: error code to be notified
2299 */
2300static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002301{
2302 /* Clear requests (even when the construction failed) */
2303 spin_lock(&sci->sc_state_lock);
2304
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002305 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002306 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002307 sci->sc_seq_done = sci->sc_seq_accepted;
2308 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002309 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002310 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002311 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002312 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002313 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002314 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002315
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002316 /* re-enable timer if checkpoint creation was not done */
Li Hongfdce8952010-04-10 23:25:39 +08002317 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2318 time_before(jiffies, sci->sc_timer.expires))
2319 add_timer(&sci->sc_timer);
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002320 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002321 spin_unlock(&sci->sc_state_lock);
2322}
2323
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002324/**
2325 * nilfs_segctor_construct - form logs and write them to disk
2326 * @sci: segment constructor object
2327 * @mode: mode of log forming
2328 */
2329static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002330{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002331 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002332 struct nilfs_super_block **sbp;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002333 int err = 0;
2334
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002335 nilfs_segctor_accept(sci);
2336
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002337 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002338 mode = SC_LSEG_SR;
2339 if (!nilfs_segctor_confirm(sci))
2340 err = nilfs_segctor_do_construct(sci, mode);
2341
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002342 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002343 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002344 atomic_set(&nilfs->ns_ndirtyblks, 0);
2345 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2346 nilfs_discontinued(nilfs)) {
2347 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002348 err = -EIO;
Ryusuke Konishif7545142011-03-09 11:05:08 +09002349 sbp = nilfs_prepare_super(sci->sc_super,
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002350 nilfs_sb_will_flip(nilfs));
2351 if (likely(sbp)) {
2352 nilfs_set_log_cursor(sbp[0], nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002353 err = nilfs_commit_super(sci->sc_super,
2354 NILFS_SB_COMMIT);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002355 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002356 up_write(&nilfs->ns_sem);
2357 }
2358 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002359
2360 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002361 return err;
2362}
2363
2364static void nilfs_construction_timeout(unsigned long data)
2365{
2366 struct task_struct *p = (struct task_struct *)data;
2367 wake_up_process(p);
2368}
2369
2370static void
2371nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2372{
2373 struct nilfs_inode_info *ii, *n;
2374
2375 list_for_each_entry_safe(ii, n, head, i_dirty) {
2376 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2377 continue;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002378 list_del_init(&ii->i_dirty);
Ryusuke Konishifbb24a32012-06-20 12:52:57 -07002379 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
2380 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +09002381 iput(&ii->vfs_inode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002382 }
2383}
2384
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002385int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2386 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002387{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002388 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002389 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002390 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002391 int err;
2392
2393 if (unlikely(!sci))
2394 return -EROFS;
2395
Ryusuke Konishif7545142011-03-09 11:05:08 +09002396 nilfs_transaction_lock(sb, &ti, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002397
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002398 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002399 if (unlikely(err))
2400 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002401
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002402 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002403 if (unlikely(err)) {
2404 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002405 goto out_unlock;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002406 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002407
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002408 sci->sc_freesegs = kbufs[4];
2409 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002410 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002411
2412 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002413 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002414 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002415
2416 if (likely(!err))
2417 break;
2418
2419 nilfs_warning(sb, __func__,
2420 "segment construction failed. (err=%d)", err);
2421 set_current_state(TASK_INTERRUPTIBLE);
2422 schedule_timeout(sci->sc_interval);
2423 }
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002424 if (nilfs_test_opt(nilfs, DISCARD)) {
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002425 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2426 sci->sc_nfreesegs);
2427 if (ret) {
2428 printk(KERN_WARNING
2429 "NILFS warning: error %d on discard request, "
2430 "turning discards off for the device\n", ret);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002431 nilfs_clear_opt(nilfs, DISCARD);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002432 }
2433 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002434
2435 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002436 sci->sc_freesegs = NULL;
2437 sci->sc_nfreesegs = 0;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002438 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002439 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002440 return err;
2441}
2442
2443static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2444{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002445 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002446
Ryusuke Konishif7545142011-03-09 11:05:08 +09002447 nilfs_transaction_lock(sci->sc_super, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002448 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002449
2450 /*
2451 * Unclosed segment should be retried. We do this using sc_timer.
2452 * Timeout of sc_timer will invoke complete construction which leads
2453 * to close the current logical segment.
2454 */
2455 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2456 nilfs_segctor_start_timer(sci);
2457
Ryusuke Konishif7545142011-03-09 11:05:08 +09002458 nilfs_transaction_unlock(sci->sc_super);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002459}
2460
2461static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2462{
2463 int mode = 0;
2464 int err;
2465
2466 spin_lock(&sci->sc_state_lock);
2467 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2468 SC_FLUSH_DAT : SC_FLUSH_FILE;
2469 spin_unlock(&sci->sc_state_lock);
2470
2471 if (mode) {
2472 err = nilfs_segctor_do_construct(sci, mode);
2473
2474 spin_lock(&sci->sc_state_lock);
2475 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2476 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2477 spin_unlock(&sci->sc_state_lock);
2478 }
2479 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2480}
2481
2482static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2483{
2484 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2485 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2486 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2487 return SC_FLUSH_FILE;
2488 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2489 return SC_FLUSH_DAT;
2490 }
2491 return SC_LSEG_SR;
2492}
2493
2494/**
2495 * nilfs_segctor_thread - main loop of the segment constructor thread.
2496 * @arg: pointer to a struct nilfs_sc_info.
2497 *
2498 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2499 * to execute segment constructions.
2500 */
2501static int nilfs_segctor_thread(void *arg)
2502{
2503 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002504 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002505 int timeout = 0;
2506
Li Hongfdce8952010-04-10 23:25:39 +08002507 sci->sc_timer.data = (unsigned long)current;
2508 sci->sc_timer.function = nilfs_construction_timeout;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002509
2510 /* start sync. */
2511 sci->sc_task = current;
2512 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2513 printk(KERN_INFO
2514 "segctord starting. Construction interval = %lu seconds, "
2515 "CP frequency < %lu seconds\n",
2516 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2517
2518 spin_lock(&sci->sc_state_lock);
2519 loop:
2520 for (;;) {
2521 int mode;
2522
2523 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2524 goto end_thread;
2525
2526 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2527 mode = SC_LSEG_SR;
2528 else if (!sci->sc_flush_request)
2529 break;
2530 else
2531 mode = nilfs_segctor_flush_mode(sci);
2532
2533 spin_unlock(&sci->sc_state_lock);
2534 nilfs_segctor_thread_construct(sci, mode);
2535 spin_lock(&sci->sc_state_lock);
2536 timeout = 0;
2537 }
2538
2539
2540 if (freezing(current)) {
2541 spin_unlock(&sci->sc_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -08002542 try_to_freeze();
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002543 spin_lock(&sci->sc_state_lock);
2544 } else {
2545 DEFINE_WAIT(wait);
2546 int should_sleep = 1;
2547
2548 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2549 TASK_INTERRUPTIBLE);
2550
2551 if (sci->sc_seq_request != sci->sc_seq_done)
2552 should_sleep = 0;
2553 else if (sci->sc_flush_request)
2554 should_sleep = 0;
2555 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2556 should_sleep = time_before(jiffies,
Li Hongfdce8952010-04-10 23:25:39 +08002557 sci->sc_timer.expires);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002558
2559 if (should_sleep) {
2560 spin_unlock(&sci->sc_state_lock);
2561 schedule();
2562 spin_lock(&sci->sc_state_lock);
2563 }
2564 finish_wait(&sci->sc_wait_daemon, &wait);
2565 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
Li Hongfdce8952010-04-10 23:25:39 +08002566 time_after_eq(jiffies, sci->sc_timer.expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002567
2568 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002569 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002570 }
2571 goto loop;
2572
2573 end_thread:
2574 spin_unlock(&sci->sc_state_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002575
2576 /* end sync. */
2577 sci->sc_task = NULL;
2578 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2579 return 0;
2580}
2581
2582static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2583{
2584 struct task_struct *t;
2585
2586 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2587 if (IS_ERR(t)) {
2588 int err = PTR_ERR(t);
2589
2590 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2591 err);
2592 return err;
2593 }
2594 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2595 return 0;
2596}
2597
2598static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
Jiro SEKIBA6b81e142010-10-14 13:52:00 +09002599 __acquires(&sci->sc_state_lock)
2600 __releases(&sci->sc_state_lock)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002601{
2602 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2603
2604 while (sci->sc_task) {
2605 wake_up(&sci->sc_wait_daemon);
2606 spin_unlock(&sci->sc_state_lock);
2607 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2608 spin_lock(&sci->sc_state_lock);
2609 }
2610}
2611
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002612/*
2613 * Setup & clean-up functions
2614 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002615static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002616 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002617{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002618 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002619 struct nilfs_sc_info *sci;
2620
2621 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2622 if (!sci)
2623 return NULL;
2624
Ryusuke Konishif7545142011-03-09 11:05:08 +09002625 sci->sc_super = sb;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002626
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002627 nilfs_get_root(root);
2628 sci->sc_root = root;
2629
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002630 init_waitqueue_head(&sci->sc_wait_request);
2631 init_waitqueue_head(&sci->sc_wait_daemon);
2632 init_waitqueue_head(&sci->sc_wait_task);
2633 spin_lock_init(&sci->sc_state_lock);
2634 INIT_LIST_HEAD(&sci->sc_dirty_files);
2635 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002636 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002637 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002638 INIT_LIST_HEAD(&sci->sc_iput_queue);
2639 INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
Li Hongfdce8952010-04-10 23:25:39 +08002640 init_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002641
2642 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2643 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2644 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2645
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002646 if (nilfs->ns_interval)
Ryusuke Konishi071d73c2011-06-10 00:33:06 +09002647 sci->sc_interval = HZ * nilfs->ns_interval;
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002648 if (nilfs->ns_watermark)
2649 sci->sc_watermark = nilfs->ns_watermark;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002650 return sci;
2651}
2652
2653static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2654{
2655 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2656
2657 /* The segctord thread was stopped and its timer was removed.
2658 But some tasks remain. */
2659 do {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002660 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002661
Ryusuke Konishif7545142011-03-09 11:05:08 +09002662 nilfs_transaction_lock(sci->sc_super, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002663 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002664 nilfs_transaction_unlock(sci->sc_super);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002665
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002666 flush_work(&sci->sc_iput_work);
2667
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002668 } while (ret && retrycount-- > 0);
2669}
2670
2671/**
2672 * nilfs_segctor_destroy - destroy the segment constructor.
2673 * @sci: nilfs_sc_info
2674 *
2675 * nilfs_segctor_destroy() kills the segctord thread and frees
2676 * the nilfs_sc_info struct.
2677 * Caller must hold the segment semaphore.
2678 */
2679static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2680{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002681 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002682 int flag;
2683
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002684 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002685
2686 spin_lock(&sci->sc_state_lock);
2687 nilfs_segctor_kill_thread(sci);
2688 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2689 || sci->sc_seq_request != sci->sc_seq_done);
2690 spin_unlock(&sci->sc_state_lock);
2691
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002692 if (flush_work(&sci->sc_iput_work))
2693 flag = true;
2694
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002695 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002696 nilfs_segctor_write_out(sci);
2697
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002698 if (!list_empty(&sci->sc_dirty_files)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002699 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002700 "dirty file(s) after the final construction\n");
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002701 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002702 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002703
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002704 if (!list_empty(&sci->sc_iput_queue)) {
2705 nilfs_warning(sci->sc_super, __func__,
2706 "iput queue is not empty\n");
2707 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
2708 }
2709
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002710 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002711 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002712
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002713 nilfs_put_root(sci->sc_root);
2714
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002715 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002716
Li Hongfdce8952010-04-10 23:25:39 +08002717 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002718 kfree(sci);
2719}
2720
2721/**
Ryusuke Konishif7545142011-03-09 11:05:08 +09002722 * nilfs_attach_log_writer - attach log writer
2723 * @sb: super block instance
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002724 * @root: root object of the current filesystem tree
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002725 *
Ryusuke Konishif7545142011-03-09 11:05:08 +09002726 * This allocates a log writer object, initializes it, and starts the
2727 * log writer.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002728 *
2729 * Return Value: On success, 0 is returned. On error, one of the following
2730 * negative error code is returned.
2731 *
2732 * %-ENOMEM - Insufficient memory available.
2733 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002734int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002735{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002736 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002737 int err;
2738
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002739 if (nilfs->ns_writer) {
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002740 /*
2741 * This happens if the filesystem was remounted
2742 * read/write after nilfs_error degenerated it into a
2743 * read-only mount.
2744 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002745 nilfs_detach_log_writer(sb);
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002746 }
2747
Ryusuke Konishif7545142011-03-09 11:05:08 +09002748 nilfs->ns_writer = nilfs_segctor_new(sb, root);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002749 if (!nilfs->ns_writer)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002750 return -ENOMEM;
2751
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002752 err = nilfs_segctor_start_thread(nilfs->ns_writer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002753 if (err) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002754 kfree(nilfs->ns_writer);
2755 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002756 }
2757 return err;
2758}
2759
2760/**
Ryusuke Konishif7545142011-03-09 11:05:08 +09002761 * nilfs_detach_log_writer - destroy log writer
2762 * @sb: super block instance
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002763 *
Ryusuke Konishif7545142011-03-09 11:05:08 +09002764 * This kills log writer daemon, frees the log writer object, and
2765 * destroys list of dirty files.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002766 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002767void nilfs_detach_log_writer(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002768{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002769 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002770 LIST_HEAD(garbage_list);
2771
2772 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002773 if (nilfs->ns_writer) {
2774 nilfs_segctor_destroy(nilfs->ns_writer);
2775 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002776 }
2777
2778 /* Force to free the list of dirty files */
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002779 spin_lock(&nilfs->ns_inode_lock);
2780 if (!list_empty(&nilfs->ns_dirty_files)) {
2781 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002782 nilfs_warning(sb, __func__,
2783 "Hit dirty file after stopped log writer\n");
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002784 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002785 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002786 up_write(&nilfs->ns_segctor_sem);
2787
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002788 nilfs_dispose_list(nilfs, &garbage_list, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002789}