blob: c9ee03c262ea0f9bf5a2cc1d2cc65428930f69fc [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 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Ryusuke Konishi.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070017 *
18 */
19
20#include <linux/pagemap.h>
21#include <linux/buffer_head.h>
22#include <linux/writeback.h>
Ryusuke Konishiead8ecf2015-04-16 12:46:28 -070023#include <linux/bitops.h>
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070024#include <linux/bio.h>
25#include <linux/completion.h>
26#include <linux/blkdev.h>
27#include <linux/backing-dev.h>
28#include <linux/freezer.h>
29#include <linux/kthread.h>
30#include <linux/crc32.h>
31#include <linux/pagevec.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070033#include "nilfs.h"
34#include "btnode.h"
35#include "page.h"
36#include "segment.h"
37#include "sufile.h"
38#include "cpfile.h"
39#include "ifile.h"
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070040#include "segbuf.h"
41
42
43/*
44 * Segment constructor
45 */
46#define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */
47
48#define SC_MAX_SEGDELTA 64 /* Upper limit of the number of segments
49 appended in collection retry loop */
50
51/* Construction mode */
52enum {
53 SC_LSEG_SR = 1, /* Make a logical segment having a super root */
54 SC_LSEG_DSYNC, /* Flush data blocks of a given file and make
55 a logical segment without a super root */
56 SC_FLUSH_FILE, /* Flush data files, leads to segment writes without
57 creating a checkpoint */
58 SC_FLUSH_DAT, /* Flush DAT file. This also creates segments without
59 a checkpoint */
60};
61
62/* Stage numbers of dirty block collection */
63enum {
64 NILFS_ST_INIT = 0,
65 NILFS_ST_GC, /* Collecting dirty blocks for GC */
66 NILFS_ST_FILE,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070067 NILFS_ST_IFILE,
68 NILFS_ST_CPFILE,
69 NILFS_ST_SUFILE,
70 NILFS_ST_DAT,
71 NILFS_ST_SR, /* Super root */
72 NILFS_ST_DSYNC, /* Data sync blocks */
73 NILFS_ST_DONE,
74};
75
Hitoshi Mitake58497702015-11-06 16:31:59 -080076#define CREATE_TRACE_POINTS
77#include <trace/events/nilfs2.h>
78
79/*
80 * nilfs_sc_cstage_inc(), nilfs_sc_cstage_set(), nilfs_sc_cstage_get() are
81 * wrapper functions of stage count (nilfs_sc_info->sc_stage.scnt). Users of
82 * the variable must use them because transition of stage count must involve
83 * trace events (trace_nilfs2_collection_stage_transition).
84 *
85 * nilfs_sc_cstage_get() isn't required for the above purpose because it doesn't
86 * produce tracepoint events. It is provided just for making the intention
87 * clear.
88 */
89static inline void nilfs_sc_cstage_inc(struct nilfs_sc_info *sci)
90{
91 sci->sc_stage.scnt++;
92 trace_nilfs2_collection_stage_transition(sci);
93}
94
95static inline void nilfs_sc_cstage_set(struct nilfs_sc_info *sci, int next_scnt)
96{
97 sci->sc_stage.scnt = next_scnt;
98 trace_nilfs2_collection_stage_transition(sci);
99}
100
101static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci)
102{
103 return sci->sc_stage.scnt;
104}
105
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700106/* State flags of collection */
107#define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
108#define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900109#define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
110#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700111
112/* Operations depending on the construction mode and file type */
113struct nilfs_sc_operations {
114 int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *,
115 struct inode *);
116 int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *,
117 struct inode *);
118 int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *,
119 struct inode *);
120 void (*write_data_binfo)(struct nilfs_sc_info *,
121 struct nilfs_segsum_pointer *,
122 union nilfs_binfo *);
123 void (*write_node_binfo)(struct nilfs_sc_info *,
124 struct nilfs_segsum_pointer *,
125 union nilfs_binfo *);
126};
127
128/*
129 * Other definitions
130 */
131static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
132static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
133static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900134static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700135
136#define nilfs_cnt32_gt(a, b) \
137 (typecheck(__u32, a) && typecheck(__u32, b) && \
138 ((__s32)(b) - (__s32)(a) < 0))
139#define nilfs_cnt32_ge(a, b) \
140 (typecheck(__u32, a) && typecheck(__u32, b) && \
141 ((__s32)(a) - (__s32)(b) >= 0))
142#define nilfs_cnt32_lt(a, b) nilfs_cnt32_gt(b, a)
143#define nilfs_cnt32_le(a, b) nilfs_cnt32_ge(b, a)
144
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700145static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
146{
147 struct nilfs_transaction_info *cur_ti = current->journal_info;
148 void *save = NULL;
149
150 if (cur_ti) {
151 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
152 return ++cur_ti->ti_count;
Ryusuke Konishi7f001842016-05-23 16:23:42 -0700153
154 /*
155 * If journal_info field is occupied by other FS,
156 * it is saved and will be restored on
157 * nilfs_transaction_commit().
158 */
159 printk(KERN_WARNING
160 "NILFS warning: journal info from a different FS\n");
161 save = current->journal_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700162 }
163 if (!ti) {
164 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
165 if (!ti)
166 return -ENOMEM;
167 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
168 } else {
169 ti->ti_flags = 0;
170 }
171 ti->ti_count = 0;
172 ti->ti_save = save;
173 ti->ti_magic = NILFS_TI_MAGIC;
174 current->journal_info = ti;
175 return 0;
176}
177
178/**
179 * nilfs_transaction_begin - start indivisible file operations.
180 * @sb: super block
181 * @ti: nilfs_transaction_info
182 * @vacancy_check: flags for vacancy rate checks
183 *
184 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
185 * the segment semaphore, to make a segment construction and write tasks
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700186 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700187 * The region enclosed by these two functions can be nested. To avoid a
188 * deadlock, the semaphore is only acquired or released in the outermost call.
189 *
190 * This function allocates a nilfs_transaction_info struct to keep context
191 * information on it. It is initialized and hooked onto the current task in
192 * the outermost call. If a pre-allocated struct is given to @ti, it is used
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900193 * instead; otherwise a new struct is assigned from a slab.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700194 *
195 * When @vacancy_check flag is set, this function will check the amount of
196 * free space, and will wait for the GC to reclaim disk space if low capacity.
197 *
198 * Return Value: On success, 0 is returned. On error, one of the following
199 * negative error code is returned.
200 *
201 * %-ENOMEM - Insufficient memory available.
202 *
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700203 * %-ENOSPC - No space left on device
204 */
205int nilfs_transaction_begin(struct super_block *sb,
206 struct nilfs_transaction_info *ti,
207 int vacancy_check)
208{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700209 struct the_nilfs *nilfs;
210 int ret = nilfs_prepare_segment_lock(ti);
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800211 struct nilfs_transaction_info *trace_ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700212
213 if (unlikely(ret < 0))
214 return ret;
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800215 if (ret > 0) {
216 trace_ti = current->journal_info;
217
218 trace_nilfs2_transaction_transition(sb, trace_ti,
219 trace_ti->ti_count, trace_ti->ti_flags,
220 TRACE_NILFS2_TRANSACTION_BEGIN);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700221 return 0;
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800222 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700223
Jan Kara2c22b332012-06-12 16:20:44 +0200224 sb_start_intwrite(sb);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900225
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900226 nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700227 down_read(&nilfs->ns_segctor_sem);
228 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
229 up_read(&nilfs->ns_segctor_sem);
230 ret = -ENOSPC;
231 goto failed;
232 }
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800233
234 trace_ti = current->journal_info;
235 trace_nilfs2_transaction_transition(sb, trace_ti, trace_ti->ti_count,
236 trace_ti->ti_flags,
237 TRACE_NILFS2_TRANSACTION_BEGIN);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700238 return 0;
239
240 failed:
241 ti = current->journal_info;
242 current->journal_info = ti->ti_save;
243 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
244 kmem_cache_free(nilfs_transaction_cachep, ti);
Jan Kara2c22b332012-06-12 16:20:44 +0200245 sb_end_intwrite(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700246 return ret;
247}
248
249/**
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700250 * nilfs_transaction_commit - commit indivisible file operations.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700251 * @sb: super block
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700252 *
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700253 * nilfs_transaction_commit() releases the read semaphore which is
254 * acquired by nilfs_transaction_begin(). This is only performed
255 * in outermost call of this function. If a commit flag is set,
256 * nilfs_transaction_commit() sets a timer to start the segment
257 * constructor. If a sync flag is set, it starts construction
258 * directly.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700259 */
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700260int nilfs_transaction_commit(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700261{
262 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900263 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700264 int err = 0;
265
266 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700267 ti->ti_flags |= NILFS_TI_COMMIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700268 if (ti->ti_count > 0) {
269 ti->ti_count--;
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800270 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
271 ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700272 return 0;
273 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900274 if (nilfs->ns_writer) {
275 struct nilfs_sc_info *sci = nilfs->ns_writer;
276
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700277 if (ti->ti_flags & NILFS_TI_COMMIT)
278 nilfs_segctor_start_timer(sci);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900279 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700280 nilfs_segctor_do_flush(sci, 0);
281 }
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900282 up_read(&nilfs->ns_segctor_sem);
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800283 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
284 ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
285
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700286 current->journal_info = ti->ti_save;
287
288 if (ti->ti_flags & NILFS_TI_SYNC)
289 err = nilfs_construct_segment(sb);
290 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
291 kmem_cache_free(nilfs_transaction_cachep, ti);
Jan Kara2c22b332012-06-12 16:20:44 +0200292 sb_end_intwrite(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700293 return err;
294}
295
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700296void nilfs_transaction_abort(struct super_block *sb)
297{
298 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900299 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700300
301 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
302 if (ti->ti_count > 0) {
303 ti->ti_count--;
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800304 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
305 ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700306 return;
307 }
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900308 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700309
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800310 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
311 ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
312
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700313 current->journal_info = ti->ti_save;
314 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
315 kmem_cache_free(nilfs_transaction_cachep, ti);
Jan Kara2c22b332012-06-12 16:20:44 +0200316 sb_end_intwrite(sb);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700317}
318
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700319void nilfs_relax_pressure_in_lock(struct super_block *sb)
320{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900321 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900322 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700323
324 if (!sci || !sci->sc_flush_request)
325 return;
326
327 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
328 up_read(&nilfs->ns_segctor_sem);
329
330 down_write(&nilfs->ns_segctor_sem);
331 if (sci->sc_flush_request &&
332 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
333 struct nilfs_transaction_info *ti = current->journal_info;
334
335 ti->ti_flags |= NILFS_TI_WRITER;
336 nilfs_segctor_do_immediate_flush(sci);
337 ti->ti_flags &= ~NILFS_TI_WRITER;
338 }
339 downgrade_write(&nilfs->ns_segctor_sem);
340}
341
Ryusuke Konishif7545142011-03-09 11:05:08 +0900342static void nilfs_transaction_lock(struct super_block *sb,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700343 struct nilfs_transaction_info *ti,
344 int gcflag)
345{
346 struct nilfs_transaction_info *cur_ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900347 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900348 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700349
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700350 WARN_ON(cur_ti);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700351 ti->ti_flags = NILFS_TI_WRITER;
352 ti->ti_count = 0;
353 ti->ti_save = cur_ti;
354 ti->ti_magic = NILFS_TI_MAGIC;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700355 current->journal_info = ti;
356
357 for (;;) {
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800358 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
359 ti->ti_flags, TRACE_NILFS2_TRANSACTION_TRYLOCK);
360
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900361 down_write(&nilfs->ns_segctor_sem);
362 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700363 break;
364
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +0900365 nilfs_segctor_do_immediate_flush(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700366
Ryusuke Konishif7545142011-03-09 11:05:08 +0900367 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700368 yield();
369 }
370 if (gcflag)
371 ti->ti_flags |= NILFS_TI_GC;
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800372
373 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
374 ti->ti_flags, TRACE_NILFS2_TRANSACTION_LOCK);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700375}
376
Ryusuke Konishif7545142011-03-09 11:05:08 +0900377static void nilfs_transaction_unlock(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700378{
379 struct nilfs_transaction_info *ti = current->journal_info;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900380 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700381
382 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
383 BUG_ON(ti->ti_count > 0);
384
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900385 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700386 current->journal_info = ti->ti_save;
Hitoshi Mitake44fda112015-11-06 16:32:02 -0800387
388 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
389 ti->ti_flags, TRACE_NILFS2_TRANSACTION_UNLOCK);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700390}
391
392static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
393 struct nilfs_segsum_pointer *ssp,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700394 unsigned int bytes)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700395{
396 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700397 unsigned int blocksize = sci->sc_super->s_blocksize;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700398 void *p;
399
400 if (unlikely(ssp->offset + bytes > blocksize)) {
401 ssp->offset = 0;
402 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
403 &segbuf->sb_segsum_buffers));
404 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
405 }
406 p = ssp->bh->b_data + ssp->offset;
407 ssp->offset += bytes;
408 return p;
409}
410
411/**
412 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
413 * @sci: nilfs_sc_info
414 */
415static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
416{
417 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
418 struct buffer_head *sumbh;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700419 unsigned int sumbytes;
420 unsigned int flags = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700421 int err;
422
423 if (nilfs_doing_gc())
424 flags = NILFS_SS_GC;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900425 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700426 if (unlikely(err))
427 return err;
428
429 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
430 sumbytes = segbuf->sb_sum.sumbytes;
431 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
432 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
433 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
434 return 0;
435}
436
437static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
438{
439 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
440 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
441 return -E2BIG; /* The current segment is filled up
442 (internal code) */
443 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
444 return nilfs_segctor_reset_segment_buffer(sci);
445}
446
447static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
448{
449 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
450 int err;
451
452 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
453 err = nilfs_segctor_feed_segment(sci);
454 if (err)
455 return err;
456 segbuf = sci->sc_curseg;
457 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900458 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700459 if (likely(!err))
460 segbuf->sb_sum.flags |= NILFS_SS_SR;
461 return err;
462}
463
464/*
465 * Functions for making segment summary and payloads
466 */
467static int nilfs_segctor_segsum_block_required(
468 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700469 unsigned int binfo_size)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700470{
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700471 unsigned int blocksize = sci->sc_super->s_blocksize;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700472 /* Size of finfo and binfo is enough small against blocksize */
473
474 return ssp->offset + binfo_size +
475 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
476 blocksize;
477}
478
479static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
480 struct inode *inode)
481{
482 sci->sc_curseg->sb_sum.nfinfo++;
483 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
484 nilfs_segctor_map_segsum_entry(
485 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700486
Ryusuke Konishi72746ac2011-02-28 13:41:11 +0900487 if (NILFS_I(inode)->i_root &&
488 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700489 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700490 /* skip finfo */
491}
492
493static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
494 struct inode *inode)
495{
496 struct nilfs_finfo *finfo;
497 struct nilfs_inode_info *ii;
498 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900499 __u64 cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700500
501 if (sci->sc_blk_cnt == 0)
502 return;
503
504 ii = NILFS_I(inode);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900505
506 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
507 cno = ii->i_cno;
508 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
509 cno = 0;
510 else
511 cno = sci->sc_cno;
512
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700513 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
514 sizeof(*finfo));
515 finfo->fi_ino = cpu_to_le64(inode->i_ino);
516 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
517 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900518 finfo->fi_cno = cpu_to_le64(cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700519
520 segbuf = sci->sc_curseg;
521 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
522 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
523 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
524 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
525}
526
527static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
528 struct buffer_head *bh,
529 struct inode *inode,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700530 unsigned int binfo_size)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700531{
532 struct nilfs_segment_buffer *segbuf;
533 int required, err = 0;
534
535 retry:
536 segbuf = sci->sc_curseg;
537 required = nilfs_segctor_segsum_block_required(
538 sci, &sci->sc_binfo_ptr, binfo_size);
539 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
540 nilfs_segctor_end_finfo(sci, inode);
541 err = nilfs_segctor_feed_segment(sci);
542 if (err)
543 return err;
544 goto retry;
545 }
546 if (unlikely(required)) {
547 err = nilfs_segbuf_extend_segsum(segbuf);
548 if (unlikely(err))
549 goto failed;
550 }
551 if (sci->sc_blk_cnt == 0)
552 nilfs_segctor_begin_finfo(sci, inode);
553
554 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
555 /* Substitution to vblocknr is delayed until update_blocknr() */
556 nilfs_segbuf_add_file_buffer(segbuf, bh);
557 sci->sc_blk_cnt++;
558 failed:
559 return err;
560}
561
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700562/*
563 * Callback functions that enumerate, mark, and collect dirty blocks
564 */
565static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
566 struct buffer_head *bh, struct inode *inode)
567{
568 int err;
569
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700570 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900571 if (err < 0)
572 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700573
574 err = nilfs_segctor_add_file_block(sci, bh, inode,
575 sizeof(struct nilfs_binfo_v));
576 if (!err)
577 sci->sc_datablk_cnt++;
578 return err;
579}
580
581static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
582 struct buffer_head *bh,
583 struct inode *inode)
584{
Ryusuke Konishie8289492010-11-19 15:26:20 +0900585 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700586}
587
588static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
589 struct buffer_head *bh,
590 struct inode *inode)
591{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700592 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700593 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
594}
595
596static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
597 struct nilfs_segsum_pointer *ssp,
598 union nilfs_binfo *binfo)
599{
600 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
601 sci, ssp, sizeof(*binfo_v));
602 *binfo_v = binfo->bi_v;
603}
604
605static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
606 struct nilfs_segsum_pointer *ssp,
607 union nilfs_binfo *binfo)
608{
609 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
610 sci, ssp, sizeof(*vblocknr));
611 *vblocknr = binfo->bi_v.bi_vblocknr;
612}
613
Julia Lawall1c613cb2016-05-23 16:22:57 -0700614static const struct nilfs_sc_operations nilfs_sc_file_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700615 .collect_data = nilfs_collect_file_data,
616 .collect_node = nilfs_collect_file_node,
617 .collect_bmap = nilfs_collect_file_bmap,
618 .write_data_binfo = nilfs_write_file_data_binfo,
619 .write_node_binfo = nilfs_write_file_node_binfo,
620};
621
622static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
623 struct buffer_head *bh, struct inode *inode)
624{
625 int err;
626
627 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
Ryusuke Konishie8289492010-11-19 15:26:20 +0900628 if (err < 0)
629 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700630
631 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
632 if (!err)
633 sci->sc_datablk_cnt++;
634 return err;
635}
636
637static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
638 struct buffer_head *bh, struct inode *inode)
639{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700640 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700641 return nilfs_segctor_add_file_block(sci, bh, inode,
642 sizeof(struct nilfs_binfo_dat));
643}
644
645static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
646 struct nilfs_segsum_pointer *ssp,
647 union nilfs_binfo *binfo)
648{
649 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
650 sizeof(*blkoff));
651 *blkoff = binfo->bi_dat.bi_blkoff;
652}
653
654static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
655 struct nilfs_segsum_pointer *ssp,
656 union nilfs_binfo *binfo)
657{
658 struct nilfs_binfo_dat *binfo_dat =
659 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
660 *binfo_dat = binfo->bi_dat;
661}
662
Julia Lawall1c613cb2016-05-23 16:22:57 -0700663static const struct nilfs_sc_operations nilfs_sc_dat_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700664 .collect_data = nilfs_collect_dat_data,
665 .collect_node = nilfs_collect_file_node,
666 .collect_bmap = nilfs_collect_dat_bmap,
667 .write_data_binfo = nilfs_write_dat_data_binfo,
668 .write_node_binfo = nilfs_write_dat_node_binfo,
669};
670
Julia Lawall1c613cb2016-05-23 16:22:57 -0700671static const struct nilfs_sc_operations nilfs_sc_dsync_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700672 .collect_data = nilfs_collect_file_data,
673 .collect_node = NULL,
674 .collect_bmap = NULL,
675 .write_data_binfo = nilfs_write_file_data_binfo,
676 .write_node_binfo = NULL,
677};
678
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700679static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
680 struct list_head *listp,
681 size_t nlimit,
682 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700683{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700684 struct address_space *mapping = inode->i_mapping;
685 struct pagevec pvec;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700686 pgoff_t index = 0, last = ULONG_MAX;
687 size_t ndirties = 0;
688 int i;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700689
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700690 if (unlikely(start != 0 || end != LLONG_MAX)) {
691 /*
692 * A valid range is given for sync-ing data pages. The
693 * range is rounded to per-page; extra dirty buffers
694 * may be included if blocksize < pagesize.
695 */
696 index = start >> PAGE_SHIFT;
697 last = end >> PAGE_SHIFT;
698 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700699 pagevec_init(&pvec, 0);
700 repeat:
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700701 if (unlikely(index > last) ||
702 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
703 min_t(pgoff_t, last - index,
704 PAGEVEC_SIZE - 1) + 1))
705 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700706
707 for (i = 0; i < pagevec_count(&pvec); i++) {
708 struct buffer_head *bh, *head;
709 struct page *page = pvec.pages[i];
710
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700711 if (unlikely(page->index > last))
712 break;
713
Ryusuke Konishiaa405b12011-05-05 12:56:51 +0900714 lock_page(page);
715 if (!page_has_buffers(page))
716 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
717 unlock_page(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700718
719 bh = head = page_buffers(page);
720 do {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -0700721 if (!buffer_dirty(bh) || buffer_async_write(bh))
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700722 continue;
723 get_bh(bh);
724 list_add_tail(&bh->b_assoc_buffers, listp);
725 ndirties++;
726 if (unlikely(ndirties >= nlimit)) {
727 pagevec_release(&pvec);
728 cond_resched();
729 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700730 }
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700731 } while (bh = bh->b_this_page, bh != head);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700732 }
733 pagevec_release(&pvec);
734 cond_resched();
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700735 goto repeat;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700736}
737
738static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
739 struct list_head *listp)
740{
741 struct nilfs_inode_info *ii = NILFS_I(inode);
742 struct address_space *mapping = &ii->i_btnode_cache;
743 struct pagevec pvec;
744 struct buffer_head *bh, *head;
745 unsigned int i;
746 pgoff_t index = 0;
747
748 pagevec_init(&pvec, 0);
749
750 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
751 PAGEVEC_SIZE)) {
752 for (i = 0; i < pagevec_count(&pvec); i++) {
753 bh = head = page_buffers(pvec.pages[i]);
754 do {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -0700755 if (buffer_dirty(bh) &&
756 !buffer_async_write(bh)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700757 get_bh(bh);
758 list_add_tail(&bh->b_assoc_buffers,
759 listp);
760 }
761 bh = bh->b_this_page;
762 } while (bh != head);
763 }
764 pagevec_release(&pvec);
765 cond_resched();
766 }
767}
768
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900769static void nilfs_dispose_list(struct the_nilfs *nilfs,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700770 struct list_head *head, int force)
771{
772 struct nilfs_inode_info *ii, *n;
773 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700774 unsigned int nv = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700775
776 while (!list_empty(head)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900777 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700778 list_for_each_entry_safe(ii, n, head, i_dirty) {
779 list_del_init(&ii->i_dirty);
780 if (force) {
781 if (unlikely(ii->i_bh)) {
782 brelse(ii->i_bh);
783 ii->i_bh = NULL;
784 }
785 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
786 set_bit(NILFS_I_QUEUED, &ii->i_state);
787 list_add_tail(&ii->i_dirty,
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900788 &nilfs->ns_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700789 continue;
790 }
791 ivec[nv++] = ii;
792 if (nv == SC_N_INODEVEC)
793 break;
794 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900795 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700796
797 for (pii = ivec; nv > 0; pii++, nv--)
798 iput(&(*pii)->vfs_inode);
799 }
800}
801
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -0800802static void nilfs_iput_work_func(struct work_struct *work)
803{
804 struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
805 sc_iput_work);
806 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
807
808 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
809}
810
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900811static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
812 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700813{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700814 int ret = 0;
815
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900816 if (nilfs_mdt_fetch_dirty(root->ifile))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700817 ret++;
818 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
819 ret++;
820 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
821 ret++;
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900822 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
823 ret++;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700824 return ret;
825}
826
827static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
828{
829 return list_empty(&sci->sc_dirty_files) &&
830 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900831 sci->sc_nfreesegs == 0 &&
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700832 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
833}
834
835static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
836{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900837 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700838 int ret = 0;
839
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900840 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700841 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
842
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900843 spin_lock(&nilfs->ns_inode_lock);
844 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700845 ret++;
846
Ryusuke Konishi693dd322011-03-09 11:05:07 +0900847 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700848 return ret;
849}
850
851static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
852{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900853 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700854
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900855 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700856 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
857 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900858 nilfs_mdt_clear_dirty(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700859}
860
861static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
862{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900863 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700864 struct buffer_head *bh_cp;
865 struct nilfs_checkpoint *raw_cp;
866 int err;
867
868 /* XXX: this interface will be changed */
869 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
870 &raw_cp, &bh_cp);
871 if (likely(!err)) {
872 /* The following code is duplicated with cpfile. But, it is
873 needed to collect the checkpoint even if it was not newly
874 created */
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900875 mark_buffer_dirty(bh_cp);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700876 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
877 nilfs_cpfile_put_checkpoint(
878 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700879 } else
880 WARN_ON(err == -EINVAL || err == -ENOENT);
881
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700882 return err;
883}
884
885static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
886{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900887 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700888 struct buffer_head *bh_cp;
889 struct nilfs_checkpoint *raw_cp;
890 int err;
891
892 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
893 &raw_cp, &bh_cp);
894 if (unlikely(err)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700895 WARN_ON(err == -EINVAL || err == -ENOENT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700896 goto failed_ibh;
897 }
898 raw_cp->cp_snapshot_list.ssl_next = 0;
899 raw_cp->cp_snapshot_list.ssl_prev = 0;
900 raw_cp->cp_inodes_count =
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700901 cpu_to_le64(atomic64_read(&sci->sc_root->inodes_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700902 raw_cp->cp_blocks_count =
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700903 cpu_to_le64(atomic64_read(&sci->sc_root->blocks_count));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700904 raw_cp->cp_nblk_inc =
905 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
906 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
907 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700908
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700909 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
910 nilfs_checkpoint_clear_minor(raw_cp);
911 else
912 nilfs_checkpoint_set_minor(raw_cp);
913
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900914 nilfs_write_inode_common(sci->sc_root->ifile,
915 &raw_cp->cp_ifile_inode, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700916 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
917 return 0;
918
919 failed_ibh:
920 return err;
921}
922
923static void nilfs_fill_in_file_bmap(struct inode *ifile,
924 struct nilfs_inode_info *ii)
925
926{
927 struct buffer_head *ibh;
928 struct nilfs_inode *raw_inode;
929
930 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
931 ibh = ii->i_bh;
932 BUG_ON(!ibh);
933 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
934 ibh);
935 nilfs_bmap_write(ii->i_bmap, raw_inode);
936 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
937 }
938}
939
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900940static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700941{
942 struct nilfs_inode_info *ii;
943
944 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900945 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700946 set_bit(NILFS_I_COLLECTED, &ii->i_state);
947 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700948}
949
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700950static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
951 struct the_nilfs *nilfs)
952{
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900953 struct buffer_head *bh_sr;
954 struct nilfs_super_root *raw_sr;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700955 unsigned int isz, srsz;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700956
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900957 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
958 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900959 isz = nilfs->ns_inode_size;
960 srsz = NILFS_SR_BYTES(isz);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900961
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900962 raw_sr->sr_bytes = cpu_to_le16(srsz);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700963 raw_sr->sr_nongc_ctime
964 = cpu_to_le64(nilfs_doing_gc() ?
965 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
966 raw_sr->sr_flags = 0;
967
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900968 nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr +
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900969 NILFS_SR_DAT_OFFSET(isz), 1);
970 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
971 NILFS_SR_CPFILE_OFFSET(isz), 1);
972 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
973 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi56eb5532011-04-30 18:56:12 +0900974 memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700975}
976
977static void nilfs_redirty_inodes(struct list_head *head)
978{
979 struct nilfs_inode_info *ii;
980
981 list_for_each_entry(ii, head, i_dirty) {
982 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
983 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
984 }
985}
986
987static void nilfs_drop_collected_inodes(struct list_head *head)
988{
989 struct nilfs_inode_info *ii;
990
991 list_for_each_entry(ii, head, i_dirty) {
992 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
993 continue;
994
Andreas Rohnerb9f66142014-10-13 15:53:22 -0700995 clear_bit(NILFS_I_INODE_SYNC, &ii->i_state);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700996 set_bit(NILFS_I_UPDATED, &ii->i_state);
997 }
998}
999
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001000static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
1001 struct inode *inode,
1002 struct list_head *listp,
1003 int (*collect)(struct nilfs_sc_info *,
1004 struct buffer_head *,
1005 struct inode *))
1006{
1007 struct buffer_head *bh, *n;
1008 int err = 0;
1009
1010 if (collect) {
1011 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1012 list_del_init(&bh->b_assoc_buffers);
1013 err = collect(sci, bh, inode);
1014 brelse(bh);
1015 if (unlikely(err))
1016 goto dispose_buffers;
1017 }
1018 return 0;
1019 }
1020
1021 dispose_buffers:
1022 while (!list_empty(listp)) {
Ryusuke Konishi0cc1283882011-05-05 12:56:51 +09001023 bh = list_first_entry(listp, struct buffer_head,
1024 b_assoc_buffers);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001025 list_del_init(&bh->b_assoc_buffers);
1026 brelse(bh);
1027 }
1028 return err;
1029}
1030
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001031static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1032{
1033 /* Remaining number of blocks within segment buffer */
1034 return sci->sc_segbuf_nblocks -
1035 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1036}
1037
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001038static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1039 struct inode *inode,
Julia Lawall1c613cb2016-05-23 16:22:57 -07001040 const struct nilfs_sc_operations *sc_ops)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001041{
1042 LIST_HEAD(data_buffers);
1043 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001044 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001045
1046 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001047 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1048
1049 n = nilfs_lookup_dirty_data_buffers(
1050 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1051 if (n > rest) {
1052 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001053 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001054 sc_ops->collect_data);
1055 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001056 goto break_or_fail;
1057 }
1058 }
1059 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1060
1061 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1062 err = nilfs_segctor_apply_buffers(
1063 sci, inode, &data_buffers, sc_ops->collect_data);
1064 if (unlikely(err)) {
1065 /* dispose node list */
1066 nilfs_segctor_apply_buffers(
1067 sci, inode, &node_buffers, NULL);
1068 goto break_or_fail;
1069 }
1070 sci->sc_stage.flags |= NILFS_CF_NODE;
1071 }
1072 /* Collect node */
1073 err = nilfs_segctor_apply_buffers(
1074 sci, inode, &node_buffers, sc_ops->collect_node);
1075 if (unlikely(err))
1076 goto break_or_fail;
1077
1078 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1079 err = nilfs_segctor_apply_buffers(
1080 sci, inode, &node_buffers, sc_ops->collect_bmap);
1081 if (unlikely(err))
1082 goto break_or_fail;
1083
1084 nilfs_segctor_end_finfo(sci, inode);
1085 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1086
1087 break_or_fail:
1088 return err;
1089}
1090
1091static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1092 struct inode *inode)
1093{
1094 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001095 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1096 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001097
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001098 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1099 sci->sc_dsync_start,
1100 sci->sc_dsync_end);
1101
1102 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1103 nilfs_collect_file_data);
1104 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001105 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001106 BUG_ON(n > rest);
1107 /* always receive -E2BIG or true error if n > rest */
1108 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001109 return err;
1110}
1111
1112static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1113{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001114 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001115 struct list_head *head;
1116 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001117 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001118 int err = 0;
1119
Hitoshi Mitake58497702015-11-06 16:31:59 -08001120 switch (nilfs_sc_cstage_get(sci)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001121 case NILFS_ST_INIT:
1122 /* Pre-processes */
1123 sci->sc_stage.flags = 0;
1124
1125 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1126 sci->sc_nblk_inc = 0;
1127 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1128 if (mode == SC_LSEG_DSYNC) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001129 nilfs_sc_cstage_set(sci, NILFS_ST_DSYNC);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001130 goto dsync_mode;
1131 }
1132 }
1133
1134 sci->sc_stage.dirty_file_ptr = NULL;
1135 sci->sc_stage.gc_inode_ptr = NULL;
1136 if (mode == SC_FLUSH_DAT) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001137 nilfs_sc_cstage_set(sci, NILFS_ST_DAT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001138 goto dat_stage;
1139 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001140 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001141 case NILFS_ST_GC:
1142 if (nilfs_doing_gc()) {
1143 head = &sci->sc_gc_inodes;
1144 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1145 head, i_dirty);
1146 list_for_each_entry_continue(ii, head, i_dirty) {
1147 err = nilfs_segctor_scan_file(
1148 sci, &ii->vfs_inode,
1149 &nilfs_sc_file_ops);
1150 if (unlikely(err)) {
1151 sci->sc_stage.gc_inode_ptr = list_entry(
1152 ii->i_dirty.prev,
1153 struct nilfs_inode_info,
1154 i_dirty);
1155 goto break_or_fail;
1156 }
1157 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1158 }
1159 sci->sc_stage.gc_inode_ptr = NULL;
1160 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001161 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001162 case NILFS_ST_FILE:
1163 head = &sci->sc_dirty_files;
1164 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1165 i_dirty);
1166 list_for_each_entry_continue(ii, head, i_dirty) {
1167 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1168
1169 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1170 &nilfs_sc_file_ops);
1171 if (unlikely(err)) {
1172 sci->sc_stage.dirty_file_ptr =
1173 list_entry(ii->i_dirty.prev,
1174 struct nilfs_inode_info,
1175 i_dirty);
1176 goto break_or_fail;
1177 }
1178 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1179 /* XXX: required ? */
1180 }
1181 sci->sc_stage.dirty_file_ptr = NULL;
1182 if (mode == SC_FLUSH_FILE) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001183 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001184 return 0;
1185 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001186 nilfs_sc_cstage_inc(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001187 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1188 /* Fall through */
1189 case NILFS_ST_IFILE:
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001190 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001191 &nilfs_sc_file_ops);
1192 if (unlikely(err))
1193 break;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001194 nilfs_sc_cstage_inc(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001195 /* Creating a checkpoint */
1196 err = nilfs_segctor_create_checkpoint(sci);
1197 if (unlikely(err))
1198 break;
1199 /* Fall through */
1200 case NILFS_ST_CPFILE:
1201 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1202 &nilfs_sc_file_ops);
1203 if (unlikely(err))
1204 break;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001205 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001206 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001207 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1208 sci->sc_nfreesegs, &ndone);
1209 if (unlikely(err)) {
1210 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1211 sci->sc_freesegs, ndone,
1212 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001213 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001214 }
1215 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1216
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001217 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1218 &nilfs_sc_file_ops);
1219 if (unlikely(err))
1220 break;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001221 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001222 case NILFS_ST_DAT:
1223 dat_stage:
Ryusuke Konishi365e2152010-12-27 00:07:30 +09001224 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001225 &nilfs_sc_dat_ops);
1226 if (unlikely(err))
1227 break;
1228 if (mode == SC_FLUSH_DAT) {
Hitoshi Mitake58497702015-11-06 16:31:59 -08001229 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001230 return 0;
1231 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08001232 nilfs_sc_cstage_inc(sci); /* Fall through */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001233 case NILFS_ST_SR:
1234 if (mode == SC_LSEG_SR) {
1235 /* Appending a super root */
1236 err = nilfs_segctor_add_super_root(sci);
1237 if (unlikely(err))
1238 break;
1239 }
1240 /* End of a logical segment */
1241 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001242 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001243 return 0;
1244 case NILFS_ST_DSYNC:
1245 dsync_mode:
1246 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001247 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001248 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1249 break;
1250
1251 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1252 if (unlikely(err))
1253 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001254 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
Hitoshi Mitake58497702015-11-06 16:31:59 -08001255 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001256 return 0;
1257 case NILFS_ST_DONE:
1258 return 0;
1259 default:
1260 BUG();
1261 }
1262
1263 break_or_fail:
1264 return err;
1265}
1266
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001267/**
1268 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1269 * @sci: nilfs_sc_info
1270 * @nilfs: nilfs object
1271 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001272static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1273 struct the_nilfs *nilfs)
1274{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001275 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001276 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001277 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001278
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001279 segbuf = nilfs_segbuf_new(sci->sc_super);
1280 if (unlikely(!segbuf))
1281 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001282
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001283 if (list_empty(&sci->sc_write_logs)) {
1284 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1285 nilfs->ns_pseg_offset, nilfs);
1286 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1287 nilfs_shift_to_next_segment(nilfs);
1288 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1289 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001290
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001291 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001292 nextnum = nilfs->ns_nextnum;
1293
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001294 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1295 /* Start from the head of a new full segment */
1296 alloc++;
1297 } else {
1298 /* Continue logs */
1299 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1300 nilfs_segbuf_map_cont(segbuf, prev);
1301 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1302 nextnum = prev->sb_nextnum;
1303
1304 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1305 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1306 segbuf->sb_sum.seg_seq++;
1307 alloc++;
1308 }
1309 }
1310
1311 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1312 if (err)
1313 goto failed;
1314
1315 if (alloc) {
1316 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1317 if (err)
1318 goto failed;
1319 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001320 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1321
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001322 BUG_ON(!list_empty(&sci->sc_segbufs));
1323 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1324 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001325 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001326
1327 failed:
1328 nilfs_segbuf_free(segbuf);
1329 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001330}
1331
1332static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1333 struct the_nilfs *nilfs, int nadd)
1334{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001335 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001336 struct inode *sufile = nilfs->ns_sufile;
1337 __u64 nextnextnum;
1338 LIST_HEAD(list);
1339 int err, ret, i;
1340
1341 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1342 /*
1343 * Since the segment specified with nextnum might be allocated during
1344 * the previous construction, the buffer including its segusage may
1345 * not be dirty. The following call ensures that the buffer is dirty
1346 * and will pin the buffer on memory until the sufile is written.
1347 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001348 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001349 if (unlikely(err))
1350 return err;
1351
1352 for (i = 0; i < nadd; i++) {
1353 /* extend segment info */
1354 err = -ENOMEM;
1355 segbuf = nilfs_segbuf_new(sci->sc_super);
1356 if (unlikely(!segbuf))
1357 goto failed;
1358
1359 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001360 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001361 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1362
1363 /* allocate the next next full segment */
1364 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1365 if (unlikely(err))
1366 goto failed_segbuf;
1367
1368 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1369 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1370
1371 list_add_tail(&segbuf->sb_list, &list);
1372 prev = segbuf;
1373 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001374 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001375 return 0;
1376
1377 failed_segbuf:
1378 nilfs_segbuf_free(segbuf);
1379 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001380 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001381 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001382 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001383 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001384 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001385 return err;
1386}
1387
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001388static void nilfs_free_incomplete_logs(struct list_head *logs,
1389 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001390{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001391 struct nilfs_segment_buffer *segbuf, *prev;
1392 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001393 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001394
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001395 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001396 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001397 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001398 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001399 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001400 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001401 /* Case 1: The first segment failed */
1402 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1403 /* Case 1a: Partial segment appended into an existing
1404 segment */
1405 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1406 segbuf->sb_fseg_end);
1407 else /* Case 1b: New full segment */
1408 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001409 }
1410
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001411 prev = segbuf;
1412 list_for_each_entry_continue(segbuf, logs, sb_list) {
1413 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1414 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1415 WARN_ON(ret); /* never fails */
1416 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001417 if (atomic_read(&segbuf->sb_err) &&
1418 segbuf->sb_segnum != nilfs->ns_nextnum)
1419 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001420 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1421 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001422 }
1423}
1424
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001425static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1426 struct inode *sufile)
1427{
1428 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001429 unsigned long live_blocks;
1430 int ret;
1431
1432 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001433 live_blocks = segbuf->sb_sum.nblocks +
1434 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001435 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1436 live_blocks,
1437 sci->sc_seg_ctime);
1438 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001439 }
1440}
1441
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001442static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001443{
1444 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001445 int ret;
1446
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001447 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001448 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1449 segbuf->sb_pseg_start -
1450 segbuf->sb_fseg_start, 0);
1451 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001452
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001453 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001454 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1455 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001456 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001457 }
1458}
1459
1460static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1461 struct nilfs_segment_buffer *last,
1462 struct inode *sufile)
1463{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001464 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001465 int ret;
1466
Ryusuke Konishie29df392009-11-29 16:51:16 +09001467 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001468 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1469 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001470 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001471 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001472 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001473}
1474
1475
1476static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1477 struct the_nilfs *nilfs, int mode)
1478{
1479 struct nilfs_cstage prev_stage = sci->sc_stage;
1480 int err, nadd = 1;
1481
1482 /* Collection retry loop */
1483 for (;;) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001484 sci->sc_nblk_this_inc = 0;
1485 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1486
1487 err = nilfs_segctor_reset_segment_buffer(sci);
1488 if (unlikely(err))
1489 goto failed;
1490
1491 err = nilfs_segctor_collect_blocks(sci, mode);
1492 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1493 if (!err)
1494 break;
1495
1496 if (unlikely(err != -E2BIG))
1497 goto failed;
1498
1499 /* The current segment is filled up */
Hitoshi Mitake58497702015-11-06 16:31:59 -08001500 if (mode != SC_LSEG_SR ||
1501 nilfs_sc_cstage_get(sci) < NILFS_ST_CPFILE)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001502 break;
1503
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001504 nilfs_clear_logs(&sci->sc_segbufs);
1505
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001506 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1507 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1508 sci->sc_freesegs,
1509 sci->sc_nfreesegs,
1510 NULL);
1511 WARN_ON(err); /* do not happen */
Andreas Rohner70f2fe32014-01-14 17:56:36 -08001512 sci->sc_stage.flags &= ~NILFS_CF_SUFREED;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001513 }
Andreas Rohner70f2fe32014-01-14 17:56:36 -08001514
1515 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1516 if (unlikely(err))
1517 return err;
1518
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001519 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1520 sci->sc_stage = prev_stage;
1521 }
1522 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1523 return 0;
1524
1525 failed:
1526 return err;
1527}
1528
1529static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1530 struct buffer_head *new_bh)
1531{
1532 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1533
1534 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1535 /* The caller must release old_bh */
1536}
1537
1538static int
1539nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1540 struct nilfs_segment_buffer *segbuf,
1541 int mode)
1542{
1543 struct inode *inode = NULL;
1544 sector_t blocknr;
1545 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1546 unsigned long nblocks = 0, ndatablk = 0;
Julia Lawall1c613cb2016-05-23 16:22:57 -07001547 const struct nilfs_sc_operations *sc_op = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001548 struct nilfs_segsum_pointer ssp;
1549 struct nilfs_finfo *finfo = NULL;
1550 union nilfs_binfo binfo;
1551 struct buffer_head *bh, *bh_org;
1552 ino_t ino = 0;
1553 int err = 0;
1554
1555 if (!nfinfo)
1556 goto out;
1557
1558 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1559 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1560 ssp.offset = sizeof(struct nilfs_segment_summary);
1561
1562 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001563 if (bh == segbuf->sb_super_root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001564 break;
1565 if (!finfo) {
1566 finfo = nilfs_segctor_map_segsum_entry(
1567 sci, &ssp, sizeof(*finfo));
1568 ino = le64_to_cpu(finfo->fi_ino);
1569 nblocks = le32_to_cpu(finfo->fi_nblocks);
1570 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1571
Ryusuke Konishiaa405b12011-05-05 12:56:51 +09001572 inode = bh->b_page->mapping->host;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001573
1574 if (mode == SC_LSEG_DSYNC)
1575 sc_op = &nilfs_sc_dsync_ops;
1576 else if (ino == NILFS_DAT_INO)
1577 sc_op = &nilfs_sc_dat_ops;
1578 else /* file blocks */
1579 sc_op = &nilfs_sc_file_ops;
1580 }
1581 bh_org = bh;
1582 get_bh(bh_org);
1583 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1584 &binfo);
1585 if (bh != bh_org)
1586 nilfs_list_replace_buffer(bh_org, bh);
1587 brelse(bh_org);
1588 if (unlikely(err))
1589 goto failed_bmap;
1590
1591 if (ndatablk > 0)
1592 sc_op->write_data_binfo(sci, &ssp, &binfo);
1593 else
1594 sc_op->write_node_binfo(sci, &ssp, &binfo);
1595
1596 blocknr++;
1597 if (--nblocks == 0) {
1598 finfo = NULL;
1599 if (--nfinfo == 0)
1600 break;
1601 } else if (ndatablk > 0)
1602 ndatablk--;
1603 }
1604 out:
1605 return 0;
1606
1607 failed_bmap:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001608 return err;
1609}
1610
1611static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1612{
1613 struct nilfs_segment_buffer *segbuf;
1614 int err;
1615
1616 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1617 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1618 if (unlikely(err))
1619 return err;
1620 nilfs_segbuf_fill_in_segsum(segbuf);
1621 }
1622 return 0;
1623}
1624
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001625static void nilfs_begin_page_io(struct page *page)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001626{
1627 if (!page || PageWriteback(page))
1628 /* For split b-tree node pages, this function may be called
1629 twice. We ignore the 2nd or later calls by this check. */
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001630 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001631
1632 lock_page(page);
1633 clear_page_dirty_for_io(page);
1634 set_page_writeback(page);
1635 unlock_page(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001636}
1637
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001638static void nilfs_segctor_prepare_write(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001639{
1640 struct nilfs_segment_buffer *segbuf;
1641 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001642
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001643 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1644 struct buffer_head *bh;
1645
1646 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1647 b_assoc_buffers) {
1648 if (bh->b_page != bd_page) {
1649 if (bd_page) {
1650 lock_page(bd_page);
1651 clear_page_dirty_for_io(bd_page);
1652 set_page_writeback(bd_page);
1653 unlock_page(bd_page);
1654 }
1655 bd_page = bh->b_page;
1656 }
1657 }
1658
1659 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1660 b_assoc_buffers) {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -07001661 set_buffer_async_write(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001662 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001663 if (bh->b_page != bd_page) {
1664 lock_page(bd_page);
1665 clear_page_dirty_for_io(bd_page);
1666 set_page_writeback(bd_page);
1667 unlock_page(bd_page);
1668 bd_page = bh->b_page;
1669 }
1670 break;
1671 }
1672 if (bh->b_page != fs_page) {
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001673 nilfs_begin_page_io(fs_page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001674 fs_page = bh->b_page;
1675 }
1676 }
1677 }
1678 if (bd_page) {
1679 lock_page(bd_page);
1680 clear_page_dirty_for_io(bd_page);
1681 set_page_writeback(bd_page);
1682 unlock_page(bd_page);
1683 }
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001684 nilfs_begin_page_io(fs_page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001685}
1686
1687static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001688 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001689{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001690 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001691
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001692 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001693 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1694 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001695}
1696
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001697static void nilfs_end_page_io(struct page *page, int err)
1698{
1699 if (!page)
1700 return;
1701
Ryusuke Konishia9777842009-07-28 17:55:29 +09001702 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001703 /*
1704 * For b-tree node pages, this function may be called twice
1705 * or more because they might be split in a segment.
1706 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001707 if (PageDirty(page)) {
1708 /*
1709 * For pages holding split b-tree node buffers, dirty
1710 * flag on the buffers may be cleared discretely.
1711 * In that case, the page is once redirtied for
1712 * remaining buffers, and it must be cancelled if
1713 * all the buffers get cleaned later.
1714 */
1715 lock_page(page);
1716 if (nilfs_page_buffers_clean(page))
1717 __nilfs_clear_page_dirty(page);
1718 unlock_page(page);
1719 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001720 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001721 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001722
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001723 if (!err) {
1724 if (!nilfs_page_buffers_clean(page))
1725 __set_page_dirty_nobuffers(page);
1726 ClearPageError(page);
1727 } else {
1728 __set_page_dirty_nobuffers(page);
1729 SetPageError(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001730 }
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001731
1732 end_page_writeback(page);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001733}
1734
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001735static void nilfs_abort_logs(struct list_head *logs, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001736{
1737 struct nilfs_segment_buffer *segbuf;
1738 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001739 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001740
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001741 if (list_empty(logs))
1742 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001743
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001744 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001745 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1746 b_assoc_buffers) {
1747 if (bh->b_page != bd_page) {
1748 if (bd_page)
1749 end_page_writeback(bd_page);
1750 bd_page = bh->b_page;
1751 }
1752 }
1753
1754 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1755 b_assoc_buffers) {
Vyacheslav Dubeyko7f42ec32013-09-30 13:45:12 -07001756 clear_buffer_async_write(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001757 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001758 if (bh->b_page != bd_page) {
1759 end_page_writeback(bd_page);
1760 bd_page = bh->b_page;
1761 }
1762 break;
1763 }
1764 if (bh->b_page != fs_page) {
1765 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001766 fs_page = bh->b_page;
1767 }
1768 }
1769 }
1770 if (bd_page)
1771 end_page_writeback(bd_page);
1772
1773 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001774}
1775
1776static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1777 struct the_nilfs *nilfs, int err)
1778{
1779 LIST_HEAD(logs);
1780 int ret;
1781
1782 list_splice_tail_init(&sci->sc_write_logs, &logs);
1783 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09001784 nilfs_abort_logs(&logs, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001785
1786 list_splice_tail_init(&sci->sc_segbufs, &logs);
1787 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1788 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001789
1790 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1791 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1792 sci->sc_freesegs,
1793 sci->sc_nfreesegs,
1794 NULL);
1795 WARN_ON(ret); /* do not happen */
1796 }
1797
1798 nilfs_destroy_logs(&logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001799}
1800
1801static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1802 struct nilfs_segment_buffer *segbuf)
1803{
1804 nilfs->ns_segnum = segbuf->sb_segnum;
1805 nilfs->ns_nextnum = segbuf->sb_nextnum;
1806 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1807 + segbuf->sb_sum.nblocks;
1808 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1809 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1810}
1811
1812static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1813{
1814 struct nilfs_segment_buffer *segbuf;
1815 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001816 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001817 int update_sr = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001818
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001819 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001820 struct buffer_head *bh;
1821
1822 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1823 b_assoc_buffers) {
1824 set_buffer_uptodate(bh);
1825 clear_buffer_dirty(bh);
1826 if (bh->b_page != bd_page) {
1827 if (bd_page)
1828 end_page_writeback(bd_page);
1829 bd_page = bh->b_page;
1830 }
1831 }
1832 /*
1833 * We assume that the buffers which belong to the same page
1834 * continue over the buffer list.
1835 * Under this assumption, the last BHs of pages is
1836 * identifiable by the discontinuity of bh->b_page
1837 * (page != fs_page).
1838 *
1839 * For B-tree node blocks, however, this assumption is not
1840 * guaranteed. The cleanup code of B-tree node pages needs
1841 * special care.
1842 */
1843 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1844 b_assoc_buffers) {
Ryusuke Konishiead8ecf2015-04-16 12:46:28 -07001845 const unsigned long set_bits = (1 << BH_Uptodate);
1846 const unsigned long clear_bits =
1847 (1 << BH_Dirty | 1 << BH_Async_Write |
1848 1 << BH_Delay | 1 << BH_NILFS_Volatile |
1849 1 << BH_NILFS_Redirected);
1850
1851 set_mask_bits(&bh->b_state, clear_bits, set_bits);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001852 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001853 if (bh->b_page != bd_page) {
1854 end_page_writeback(bd_page);
1855 bd_page = bh->b_page;
1856 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001857 update_sr = true;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001858 break;
1859 }
1860 if (bh->b_page != fs_page) {
1861 nilfs_end_page_io(fs_page, 0);
1862 fs_page = bh->b_page;
1863 }
1864 }
1865
Ryusuke Konishi47620772010-05-23 21:48:36 +09001866 if (!nilfs_segbuf_simplex(segbuf)) {
1867 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001868 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1869 sci->sc_lseg_stime = jiffies;
1870 }
Ryusuke Konishi47620772010-05-23 21:48:36 +09001871 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001872 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1873 }
1874 }
1875 /*
1876 * Since pages may continue over multiple segment buffers,
1877 * end of the last page must be checked outside of the loop.
1878 */
1879 if (bd_page)
1880 end_page_writeback(bd_page);
1881
1882 nilfs_end_page_io(fs_page, 0);
1883
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001884 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1885
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001886 if (nilfs_doing_gc())
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001887 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09001888 else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001889 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001890
1891 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1892
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001893 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001894 nilfs_set_next_segment(nilfs, segbuf);
1895
1896 if (update_sr) {
Andreas Rohnere2c76172014-10-13 15:53:20 -07001897 nilfs->ns_flushed_device = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001898 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07001899 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001900
Ryusuke Konishic96fa462009-04-06 19:01:57 -07001901 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001902 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1903 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001904 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001905 } else
1906 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1907}
1908
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001909static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1910{
1911 int ret;
1912
1913 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1914 if (!ret) {
1915 nilfs_segctor_complete_write(sci);
1916 nilfs_destroy_logs(&sci->sc_write_logs);
1917 }
1918 return ret;
1919}
1920
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001921static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
1922 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001923{
1924 struct nilfs_inode_info *ii, *n;
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001925 struct inode *ifile = sci->sc_root->ifile;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001926
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001927 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001928 retry:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001929 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001930 if (!ii->i_bh) {
1931 struct buffer_head *ibh;
1932 int err;
1933
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001934 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001935 err = nilfs_ifile_get_inode_block(
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001936 ifile, ii->vfs_inode.i_ino, &ibh);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001937 if (unlikely(err)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001938 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -07001939 "failed to get inode block.");
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001940 return err;
1941 }
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001942 mark_buffer_dirty(ibh);
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001943 nilfs_mdt_mark_dirty(ifile);
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001944 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001945 if (likely(!ii->i_bh))
1946 ii->i_bh = ibh;
1947 else
1948 brelse(ibh);
1949 goto retry;
1950 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001951
1952 clear_bit(NILFS_I_QUEUED, &ii->i_state);
1953 set_bit(NILFS_I_BUSY, &ii->i_state);
Nicolas Kaisereaae0f32011-03-19 16:45:30 +01001954 list_move_tail(&ii->i_dirty, &sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001955 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001956 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001957
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001958 return 0;
1959}
1960
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001961static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
1962 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001963{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001964 struct nilfs_inode_info *ii, *n;
Ryusuke Konishi283ee1482015-03-12 16:26:00 -07001965 int during_mount = !(sci->sc_super->s_flags & MS_ACTIVE);
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001966 int defer_iput = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001967
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001968 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001969 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
1970 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09001971 test_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001972 continue;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09001973
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001974 clear_bit(NILFS_I_BUSY, &ii->i_state);
1975 brelse(ii->i_bh);
1976 ii->i_bh = NULL;
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001977 list_del_init(&ii->i_dirty);
Ryusuke Konishi283ee1482015-03-12 16:26:00 -07001978 if (!ii->vfs_inode.i_nlink || during_mount) {
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001979 /*
Ryusuke Konishi283ee1482015-03-12 16:26:00 -07001980 * Defer calling iput() to avoid deadlocks if
1981 * i_nlink == 0 or mount is not yet finished.
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001982 */
1983 list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
1984 defer_iput = true;
1985 } else {
1986 spin_unlock(&nilfs->ns_inode_lock);
1987 iput(&ii->vfs_inode);
1988 spin_lock(&nilfs->ns_inode_lock);
1989 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001990 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09001991 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08001992
1993 if (defer_iput)
1994 schedule_work(&sci->sc_iput_work);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001995}
1996
1997/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001998 * Main procedure of segment constructor
1999 */
2000static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2001{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002002 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002003 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002004
Hitoshi Mitake58497702015-11-06 16:31:59 -08002005 nilfs_sc_cstage_set(sci, NILFS_ST_INIT);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002006 sci->sc_cno = nilfs->ns_cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002007
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002008 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002009 if (unlikely(err))
2010 goto out;
2011
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002012 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002013 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2014
2015 if (nilfs_segctor_clean(sci))
2016 goto out;
2017
2018 do {
2019 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2020
2021 err = nilfs_segctor_begin_construction(sci, nilfs);
2022 if (unlikely(err))
2023 goto out;
2024
2025 /* Update time stamp */
2026 sci->sc_seg_ctime = get_seconds();
2027
2028 err = nilfs_segctor_collect(sci, nilfs, mode);
2029 if (unlikely(err))
2030 goto failed;
2031
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002032 /* Avoid empty segment */
Hitoshi Mitake58497702015-11-06 16:31:59 -08002033 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE &&
Ryusuke Konishi47620772010-05-23 21:48:36 +09002034 nilfs_segbuf_empty(sci->sc_curseg)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002035 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002036 goto out;
2037 }
2038
2039 err = nilfs_segctor_assign(sci, mode);
2040 if (unlikely(err))
2041 goto failed;
2042
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002043 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002044 nilfs_segctor_fill_in_file_bmap(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002045
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002046 if (mode == SC_LSEG_SR &&
Hitoshi Mitake58497702015-11-06 16:31:59 -08002047 nilfs_sc_cstage_get(sci) >= NILFS_ST_CPFILE) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002048 err = nilfs_segctor_fill_in_checkpoint(sci);
2049 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002050 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002051
2052 nilfs_segctor_fill_in_super_root(sci, nilfs);
2053 }
2054 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2055
2056 /* Write partial segments */
Ryusuke Konishi1cb2d382011-04-04 12:53:28 +09002057 nilfs_segctor_prepare_write(sci);
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +09002058
2059 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2060 nilfs->ns_crc_seed);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002061
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002062 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002063 if (unlikely(err))
2064 goto failed_to_write;
2065
Hitoshi Mitake58497702015-11-06 16:31:59 -08002066 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002067 nilfs->ns_blocksize_bits != PAGE_SHIFT) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002068 /*
2069 * At this point, we avoid double buffering
2070 * for blocksize < pagesize because page dirty
2071 * flag is turned off during write and dirty
2072 * buffers are not properly collected for
2073 * pages crossing over segments.
2074 */
2075 err = nilfs_segctor_wait(sci);
2076 if (err)
2077 goto failed_to_write;
2078 }
Hitoshi Mitake58497702015-11-06 16:31:59 -08002079 } while (nilfs_sc_cstage_get(sci) != NILFS_ST_DONE);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002080
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002081 out:
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002082 nilfs_segctor_drop_written_files(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002083 return err;
2084
2085 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002086 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2087 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002088
2089 failed:
2090 if (nilfs_doing_gc())
2091 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002092 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002093 goto out;
2094}
2095
2096/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002097 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002098 * @sci: nilfs_sc_info
2099 *
2100 * If the timer has already been set, it ignores the new request.
2101 * This function MUST be called within a section locking the segment
2102 * semaphore.
2103 */
2104static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2105{
2106 spin_lock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002107 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2108 sci->sc_timer.expires = jiffies + sci->sc_interval;
2109 add_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002110 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2111 }
2112 spin_unlock(&sci->sc_state_lock);
2113}
2114
2115static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2116{
2117 spin_lock(&sci->sc_state_lock);
2118 if (!(sci->sc_flush_request & (1 << bn))) {
2119 unsigned long prev_req = sci->sc_flush_request;
2120
2121 sci->sc_flush_request |= (1 << bn);
2122 if (!prev_req)
2123 wake_up(&sci->sc_wait_daemon);
2124 }
2125 spin_unlock(&sci->sc_state_lock);
2126}
2127
2128/**
2129 * nilfs_flush_segment - trigger a segment construction for resource control
2130 * @sb: super block
2131 * @ino: inode number of the file to be flushed out.
2132 */
2133void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2134{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002135 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002136 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002137
2138 if (!sci || nilfs_doing_construction())
2139 return;
2140 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2141 /* assign bit 0 to data files */
2142}
2143
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002144struct nilfs_segctor_wait_request {
2145 wait_queue_t wq;
2146 __u32 seq;
2147 int err;
2148 atomic_t done;
2149};
2150
2151static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2152{
2153 struct nilfs_segctor_wait_request wait_req;
2154 int err = 0;
2155
2156 spin_lock(&sci->sc_state_lock);
2157 init_wait(&wait_req.wq);
2158 wait_req.err = 0;
2159 atomic_set(&wait_req.done, 0);
2160 wait_req.seq = ++sci->sc_seq_request;
2161 spin_unlock(&sci->sc_state_lock);
2162
2163 init_waitqueue_entry(&wait_req.wq, current);
2164 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2165 set_current_state(TASK_INTERRUPTIBLE);
2166 wake_up(&sci->sc_wait_daemon);
2167
2168 for (;;) {
2169 if (atomic_read(&wait_req.done)) {
2170 err = wait_req.err;
2171 break;
2172 }
2173 if (!signal_pending(current)) {
2174 schedule();
2175 continue;
2176 }
2177 err = -ERESTARTSYS;
2178 break;
2179 }
2180 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2181 return err;
2182}
2183
2184static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2185{
2186 struct nilfs_segctor_wait_request *wrq, *n;
2187 unsigned long flags;
2188
2189 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2190 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2191 wq.task_list) {
2192 if (!atomic_read(&wrq->done) &&
2193 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2194 wrq->err = err;
2195 atomic_set(&wrq->done, 1);
2196 }
2197 if (atomic_read(&wrq->done)) {
2198 wrq->wq.func(&wrq->wq,
2199 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2200 0, NULL);
2201 }
2202 }
2203 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2204}
2205
2206/**
2207 * nilfs_construct_segment - construct a logical segment
2208 * @sb: super block
2209 *
2210 * Return Value: On success, 0 is retured. On errors, one of the following
2211 * negative error code is returned.
2212 *
2213 * %-EROFS - Read only filesystem.
2214 *
2215 * %-EIO - I/O error
2216 *
2217 * %-ENOSPC - No space left on device (only in a panic state).
2218 *
2219 * %-ERESTARTSYS - Interrupted.
2220 *
2221 * %-ENOMEM - Insufficient memory available.
2222 */
2223int nilfs_construct_segment(struct super_block *sb)
2224{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002225 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002226 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002227 struct nilfs_transaction_info *ti;
2228 int err;
2229
2230 if (!sci)
2231 return -EROFS;
2232
2233 /* A call inside transactions causes a deadlock. */
2234 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2235
2236 err = nilfs_segctor_sync(sci);
2237 return err;
2238}
2239
2240/**
2241 * nilfs_construct_dsync_segment - construct a data-only logical segment
2242 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002243 * @inode: inode whose data blocks should be written out
2244 * @start: start byte offset
2245 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002246 *
2247 * Return Value: On success, 0 is retured. On errors, one of the following
2248 * negative error code is returned.
2249 *
2250 * %-EROFS - Read only filesystem.
2251 *
2252 * %-EIO - I/O error
2253 *
2254 * %-ENOSPC - No space left on device (only in a panic state).
2255 *
2256 * %-ERESTARTSYS - Interrupted.
2257 *
2258 * %-ENOMEM - Insufficient memory available.
2259 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002260int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2261 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002262{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002263 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002264 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002265 struct nilfs_inode_info *ii;
2266 struct nilfs_transaction_info ti;
2267 int err = 0;
2268
2269 if (!sci)
2270 return -EROFS;
2271
Ryusuke Konishif7545142011-03-09 11:05:08 +09002272 nilfs_transaction_lock(sb, &ti, 0);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002273
2274 ii = NILFS_I(inode);
Andreas Rohnerb9f66142014-10-13 15:53:22 -07002275 if (test_bit(NILFS_I_INODE_SYNC, &ii->i_state) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002276 nilfs_test_opt(nilfs, STRICT_ORDER) ||
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002277 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002278 nilfs_discontinued(nilfs)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09002279 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002280 err = nilfs_segctor_sync(sci);
2281 return err;
2282 }
2283
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002284 spin_lock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002285 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2286 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002287 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002288 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002289 return 0;
2290 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002291 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002292 sci->sc_dsync_inode = ii;
2293 sci->sc_dsync_start = start;
2294 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002295
2296 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
Andreas Rohnere2c76172014-10-13 15:53:20 -07002297 if (!err)
2298 nilfs->ns_flushed_device = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002299
Ryusuke Konishif7545142011-03-09 11:05:08 +09002300 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002301 return err;
2302}
2303
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002304#define FLUSH_FILE_BIT (0x1) /* data file only */
2305#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2306
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002307/**
2308 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2309 * @sci: segment constructor object
2310 */
2311static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002312{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002313 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002314 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002315 spin_unlock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002316 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002317}
2318
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002319/**
2320 * nilfs_segctor_notify - notify the result of request to caller threads
2321 * @sci: segment constructor object
2322 * @mode: mode of log forming
2323 * @err: error code to be notified
2324 */
2325static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002326{
2327 /* Clear requests (even when the construction failed) */
2328 spin_lock(&sci->sc_state_lock);
2329
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002330 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002331 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002332 sci->sc_seq_done = sci->sc_seq_accepted;
2333 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002334 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002335 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002336 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002337 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002338 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002339 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002340
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002341 /* re-enable timer if checkpoint creation was not done */
Li Hongfdce8952010-04-10 23:25:39 +08002342 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2343 time_before(jiffies, sci->sc_timer.expires))
2344 add_timer(&sci->sc_timer);
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002345 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002346 spin_unlock(&sci->sc_state_lock);
2347}
2348
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002349/**
2350 * nilfs_segctor_construct - form logs and write them to disk
2351 * @sci: segment constructor object
2352 * @mode: mode of log forming
2353 */
2354static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002355{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002356 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002357 struct nilfs_super_block **sbp;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002358 int err = 0;
2359
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002360 nilfs_segctor_accept(sci);
2361
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002362 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002363 mode = SC_LSEG_SR;
2364 if (!nilfs_segctor_confirm(sci))
2365 err = nilfs_segctor_do_construct(sci, mode);
2366
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002367 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002368 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002369 atomic_set(&nilfs->ns_ndirtyblks, 0);
2370 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2371 nilfs_discontinued(nilfs)) {
2372 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002373 err = -EIO;
Ryusuke Konishif7545142011-03-09 11:05:08 +09002374 sbp = nilfs_prepare_super(sci->sc_super,
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002375 nilfs_sb_will_flip(nilfs));
2376 if (likely(sbp)) {
2377 nilfs_set_log_cursor(sbp[0], nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002378 err = nilfs_commit_super(sci->sc_super,
2379 NILFS_SB_COMMIT);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002380 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002381 up_write(&nilfs->ns_sem);
2382 }
2383 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002384
2385 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002386 return err;
2387}
2388
2389static void nilfs_construction_timeout(unsigned long data)
2390{
2391 struct task_struct *p = (struct task_struct *)data;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -07002392
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002393 wake_up_process(p);
2394}
2395
2396static void
2397nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2398{
2399 struct nilfs_inode_info *ii, *n;
2400
2401 list_for_each_entry_safe(ii, n, head, i_dirty) {
2402 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2403 continue;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002404 list_del_init(&ii->i_dirty);
Ryusuke Konishifbb24a32012-06-20 12:52:57 -07002405 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
2406 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +09002407 iput(&ii->vfs_inode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002408 }
2409}
2410
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002411int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2412 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002413{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002414 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002415 struct nilfs_sc_info *sci = nilfs->ns_writer;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002416 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002417 int err;
2418
2419 if (unlikely(!sci))
2420 return -EROFS;
2421
Ryusuke Konishif7545142011-03-09 11:05:08 +09002422 nilfs_transaction_lock(sb, &ti, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002423
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002424 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002425 if (unlikely(err))
2426 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002427
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002428 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002429 if (unlikely(err)) {
2430 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002431 goto out_unlock;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002432 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002433
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002434 sci->sc_freesegs = kbufs[4];
2435 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002436 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002437
2438 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002439 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002440 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002441
2442 if (likely(!err))
2443 break;
2444
2445 nilfs_warning(sb, __func__,
2446 "segment construction failed. (err=%d)", err);
2447 set_current_state(TASK_INTERRUPTIBLE);
2448 schedule_timeout(sci->sc_interval);
2449 }
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002450 if (nilfs_test_opt(nilfs, DISCARD)) {
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002451 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2452 sci->sc_nfreesegs);
2453 if (ret) {
2454 printk(KERN_WARNING
2455 "NILFS warning: error %d on discard request, "
2456 "turning discards off for the device\n", ret);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09002457 nilfs_clear_opt(nilfs, DISCARD);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002458 }
2459 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002460
2461 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002462 sci->sc_freesegs = NULL;
2463 sci->sc_nfreesegs = 0;
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +09002464 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002465 nilfs_transaction_unlock(sb);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002466 return err;
2467}
2468
2469static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2470{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002471 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002472
Ryusuke Konishif7545142011-03-09 11:05:08 +09002473 nilfs_transaction_lock(sci->sc_super, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002474 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002475
2476 /*
2477 * Unclosed segment should be retried. We do this using sc_timer.
2478 * Timeout of sc_timer will invoke complete construction which leads
2479 * to close the current logical segment.
2480 */
2481 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2482 nilfs_segctor_start_timer(sci);
2483
Ryusuke Konishif7545142011-03-09 11:05:08 +09002484 nilfs_transaction_unlock(sci->sc_super);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002485}
2486
2487static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2488{
2489 int mode = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002490
2491 spin_lock(&sci->sc_state_lock);
2492 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2493 SC_FLUSH_DAT : SC_FLUSH_FILE;
2494 spin_unlock(&sci->sc_state_lock);
2495
2496 if (mode) {
Ryusuke Konishi09ef29e2015-11-06 16:32:14 -08002497 nilfs_segctor_do_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002498
2499 spin_lock(&sci->sc_state_lock);
2500 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2501 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2502 spin_unlock(&sci->sc_state_lock);
2503 }
2504 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2505}
2506
2507static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2508{
2509 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2510 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2511 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2512 return SC_FLUSH_FILE;
2513 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2514 return SC_FLUSH_DAT;
2515 }
2516 return SC_LSEG_SR;
2517}
2518
2519/**
2520 * nilfs_segctor_thread - main loop of the segment constructor thread.
2521 * @arg: pointer to a struct nilfs_sc_info.
2522 *
2523 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2524 * to execute segment constructions.
2525 */
2526static int nilfs_segctor_thread(void *arg)
2527{
2528 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002529 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002530 int timeout = 0;
2531
Li Hongfdce8952010-04-10 23:25:39 +08002532 sci->sc_timer.data = (unsigned long)current;
2533 sci->sc_timer.function = nilfs_construction_timeout;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002534
2535 /* start sync. */
2536 sci->sc_task = current;
2537 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2538 printk(KERN_INFO
2539 "segctord starting. Construction interval = %lu seconds, "
2540 "CP frequency < %lu seconds\n",
2541 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2542
2543 spin_lock(&sci->sc_state_lock);
2544 loop:
2545 for (;;) {
2546 int mode;
2547
2548 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2549 goto end_thread;
2550
2551 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2552 mode = SC_LSEG_SR;
Ryusuke Konishi7f001842016-05-23 16:23:42 -07002553 else if (sci->sc_flush_request)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002554 mode = nilfs_segctor_flush_mode(sci);
Ryusuke Konishi7f001842016-05-23 16:23:42 -07002555 else
2556 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002557
2558 spin_unlock(&sci->sc_state_lock);
2559 nilfs_segctor_thread_construct(sci, mode);
2560 spin_lock(&sci->sc_state_lock);
2561 timeout = 0;
2562 }
2563
2564
2565 if (freezing(current)) {
2566 spin_unlock(&sci->sc_state_lock);
Tejun Heoa0acae02011-11-21 12:32:22 -08002567 try_to_freeze();
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002568 spin_lock(&sci->sc_state_lock);
2569 } else {
2570 DEFINE_WAIT(wait);
2571 int should_sleep = 1;
2572
2573 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2574 TASK_INTERRUPTIBLE);
2575
2576 if (sci->sc_seq_request != sci->sc_seq_done)
2577 should_sleep = 0;
2578 else if (sci->sc_flush_request)
2579 should_sleep = 0;
2580 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2581 should_sleep = time_before(jiffies,
Li Hongfdce8952010-04-10 23:25:39 +08002582 sci->sc_timer.expires);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002583
2584 if (should_sleep) {
2585 spin_unlock(&sci->sc_state_lock);
2586 schedule();
2587 spin_lock(&sci->sc_state_lock);
2588 }
2589 finish_wait(&sci->sc_wait_daemon, &wait);
2590 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
Li Hongfdce8952010-04-10 23:25:39 +08002591 time_after_eq(jiffies, sci->sc_timer.expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002592
2593 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002594 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002595 }
2596 goto loop;
2597
2598 end_thread:
2599 spin_unlock(&sci->sc_state_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002600
2601 /* end sync. */
2602 sci->sc_task = NULL;
2603 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2604 return 0;
2605}
2606
2607static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2608{
2609 struct task_struct *t;
2610
2611 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2612 if (IS_ERR(t)) {
2613 int err = PTR_ERR(t);
2614
2615 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2616 err);
2617 return err;
2618 }
2619 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2620 return 0;
2621}
2622
2623static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
Jiro SEKIBA6b81e142010-10-14 13:52:00 +09002624 __acquires(&sci->sc_state_lock)
2625 __releases(&sci->sc_state_lock)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002626{
2627 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2628
2629 while (sci->sc_task) {
2630 wake_up(&sci->sc_wait_daemon);
2631 spin_unlock(&sci->sc_state_lock);
2632 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2633 spin_lock(&sci->sc_state_lock);
2634 }
2635}
2636
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002637/*
2638 * Setup & clean-up functions
2639 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002640static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002641 struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002642{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002643 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002644 struct nilfs_sc_info *sci;
2645
2646 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2647 if (!sci)
2648 return NULL;
2649
Ryusuke Konishif7545142011-03-09 11:05:08 +09002650 sci->sc_super = sb;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002651
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002652 nilfs_get_root(root);
2653 sci->sc_root = root;
2654
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002655 init_waitqueue_head(&sci->sc_wait_request);
2656 init_waitqueue_head(&sci->sc_wait_daemon);
2657 init_waitqueue_head(&sci->sc_wait_task);
2658 spin_lock_init(&sci->sc_state_lock);
2659 INIT_LIST_HEAD(&sci->sc_dirty_files);
2660 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002661 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002662 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002663 INIT_LIST_HEAD(&sci->sc_iput_queue);
2664 INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
Li Hongfdce8952010-04-10 23:25:39 +08002665 init_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002666
2667 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2668 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2669 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2670
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002671 if (nilfs->ns_interval)
Ryusuke Konishi071d73c2011-06-10 00:33:06 +09002672 sci->sc_interval = HZ * nilfs->ns_interval;
Ryusuke Konishi574e6c32011-03-09 11:05:07 +09002673 if (nilfs->ns_watermark)
2674 sci->sc_watermark = nilfs->ns_watermark;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002675 return sci;
2676}
2677
2678static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2679{
2680 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2681
2682 /* The segctord thread was stopped and its timer was removed.
2683 But some tasks remain. */
2684 do {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002685 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002686
Ryusuke Konishif7545142011-03-09 11:05:08 +09002687 nilfs_transaction_lock(sci->sc_super, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002688 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002689 nilfs_transaction_unlock(sci->sc_super);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002690
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002691 flush_work(&sci->sc_iput_work);
2692
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002693 } while (ret && retrycount-- > 0);
2694}
2695
2696/**
2697 * nilfs_segctor_destroy - destroy the segment constructor.
2698 * @sci: nilfs_sc_info
2699 *
2700 * nilfs_segctor_destroy() kills the segctord thread and frees
2701 * the nilfs_sc_info struct.
2702 * Caller must hold the segment semaphore.
2703 */
2704static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2705{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002706 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002707 int flag;
2708
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002709 up_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002710
2711 spin_lock(&sci->sc_state_lock);
2712 nilfs_segctor_kill_thread(sci);
2713 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2714 || sci->sc_seq_request != sci->sc_seq_done);
2715 spin_unlock(&sci->sc_state_lock);
2716
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002717 if (flush_work(&sci->sc_iput_work))
2718 flag = true;
2719
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002720 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002721 nilfs_segctor_write_out(sci);
2722
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002723 if (!list_empty(&sci->sc_dirty_files)) {
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002724 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -07002725 "dirty file(s) after the final construction");
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002726 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002727 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002728
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002729 if (!list_empty(&sci->sc_iput_queue)) {
2730 nilfs_warning(sci->sc_super, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -07002731 "iput queue is not empty");
Ryusuke Konishi7ef3ff22015-02-05 12:25:20 -08002732 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
2733 }
2734
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002735 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002736 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002737
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002738 nilfs_put_root(sci->sc_root);
2739
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002740 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002741
Li Hongfdce8952010-04-10 23:25:39 +08002742 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002743 kfree(sci);
2744}
2745
2746/**
Ryusuke Konishif7545142011-03-09 11:05:08 +09002747 * nilfs_attach_log_writer - attach log writer
2748 * @sb: super block instance
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09002749 * @root: root object of the current filesystem tree
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002750 *
Ryusuke Konishif7545142011-03-09 11:05:08 +09002751 * This allocates a log writer object, initializes it, and starts the
2752 * log writer.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002753 *
2754 * Return Value: On success, 0 is returned. On error, one of the following
2755 * negative error code is returned.
2756 *
2757 * %-ENOMEM - Insufficient memory available.
2758 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002759int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002760{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002761 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002762 int err;
2763
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002764 if (nilfs->ns_writer) {
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002765 /*
2766 * This happens if the filesystem was remounted
2767 * read/write after nilfs_error degenerated it into a
2768 * read-only mount.
2769 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002770 nilfs_detach_log_writer(sb);
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002771 }
2772
Ryusuke Konishif7545142011-03-09 11:05:08 +09002773 nilfs->ns_writer = nilfs_segctor_new(sb, root);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002774 if (!nilfs->ns_writer)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002775 return -ENOMEM;
2776
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002777 err = nilfs_segctor_start_thread(nilfs->ns_writer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002778 if (err) {
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002779 kfree(nilfs->ns_writer);
2780 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002781 }
2782 return err;
2783}
2784
2785/**
Ryusuke Konishif7545142011-03-09 11:05:08 +09002786 * nilfs_detach_log_writer - destroy log writer
2787 * @sb: super block instance
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002788 *
Ryusuke Konishif7545142011-03-09 11:05:08 +09002789 * This kills log writer daemon, frees the log writer object, and
2790 * destroys list of dirty files.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002791 */
Ryusuke Konishif7545142011-03-09 11:05:08 +09002792void nilfs_detach_log_writer(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002793{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09002794 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002795 LIST_HEAD(garbage_list);
2796
2797 down_write(&nilfs->ns_segctor_sem);
Ryusuke Konishi3fd3fe52011-03-09 11:05:08 +09002798 if (nilfs->ns_writer) {
2799 nilfs_segctor_destroy(nilfs->ns_writer);
2800 nilfs->ns_writer = NULL;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002801 }
2802
2803 /* Force to free the list of dirty files */
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002804 spin_lock(&nilfs->ns_inode_lock);
2805 if (!list_empty(&nilfs->ns_dirty_files)) {
2806 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
Ryusuke Konishif7545142011-03-09 11:05:08 +09002807 nilfs_warning(sb, __func__,
Ryusuke Konishi06f4abf2016-05-23 16:23:31 -07002808 "Hit dirty file after stopped log writer");
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002809 }
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002810 spin_unlock(&nilfs->ns_inode_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002811 up_write(&nilfs->ns_segctor_sem);
2812
Ryusuke Konishi693dd322011-03-09 11:05:07 +09002813 nilfs_dispose_list(nilfs, &garbage_list, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002814}