Commit | Line | Data |
---|---|---|
ae98043f | 1 | // SPDX-License-Identifier: GPL-2.0+ |
9ff05123 | 2 | /* |
94ee1d91 | 3 | * NILFS segment constructor. |
9ff05123 RK |
4 | * |
5 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. | |
6 | * | |
4b420ab4 | 7 | * Written by Ryusuke Konishi. |
9ff05123 RK |
8 | * |
9 | */ | |
10 | ||
11 | #include <linux/pagemap.h> | |
12 | #include <linux/buffer_head.h> | |
13 | #include <linux/writeback.h> | |
ead8ecff | 14 | #include <linux/bitops.h> |
9ff05123 RK |
15 | #include <linux/bio.h> |
16 | #include <linux/completion.h> | |
17 | #include <linux/blkdev.h> | |
18 | #include <linux/backing-dev.h> | |
19 | #include <linux/freezer.h> | |
20 | #include <linux/kthread.h> | |
21 | #include <linux/crc32.h> | |
22 | #include <linux/pagevec.h> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
174cd4b1 IM |
24 | #include <linux/sched/signal.h> |
25 | ||
9ff05123 RK |
26 | #include "nilfs.h" |
27 | #include "btnode.h" | |
28 | #include "page.h" | |
29 | #include "segment.h" | |
30 | #include "sufile.h" | |
31 | #include "cpfile.h" | |
32 | #include "ifile.h" | |
9ff05123 RK |
33 | #include "segbuf.h" |
34 | ||
35 | ||
36 | /* | |
37 | * Segment constructor | |
38 | */ | |
39 | #define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */ | |
40 | ||
076a378b RK |
41 | #define SC_MAX_SEGDELTA 64 /* |
42 | * Upper limit of the number of segments | |
43 | * appended in collection retry loop | |
44 | */ | |
9ff05123 RK |
45 | |
46 | /* Construction mode */ | |
47 | enum { | |
48 | SC_LSEG_SR = 1, /* Make a logical segment having a super root */ | |
076a378b RK |
49 | SC_LSEG_DSYNC, /* |
50 | * Flush data blocks of a given file and make | |
51 | * a logical segment without a super root. | |
52 | */ | |
53 | SC_FLUSH_FILE, /* | |
54 | * Flush data files, leads to segment writes without | |
55 | * creating a checkpoint. | |
56 | */ | |
57 | SC_FLUSH_DAT, /* | |
58 | * Flush DAT file. This also creates segments | |
59 | * without a checkpoint. | |
60 | */ | |
9ff05123 RK |
61 | }; |
62 | ||
63 | /* Stage numbers of dirty block collection */ | |
64 | enum { | |
65 | NILFS_ST_INIT = 0, | |
66 | NILFS_ST_GC, /* Collecting dirty blocks for GC */ | |
67 | NILFS_ST_FILE, | |
9ff05123 RK |
68 | NILFS_ST_IFILE, |
69 | NILFS_ST_CPFILE, | |
70 | NILFS_ST_SUFILE, | |
71 | NILFS_ST_DAT, | |
72 | NILFS_ST_SR, /* Super root */ | |
73 | NILFS_ST_DSYNC, /* Data sync blocks */ | |
74 | NILFS_ST_DONE, | |
75 | }; | |
76 | ||
58497703 HM |
77 | #define CREATE_TRACE_POINTS |
78 | #include <trace/events/nilfs2.h> | |
79 | ||
80 | /* | |
81 | * nilfs_sc_cstage_inc(), nilfs_sc_cstage_set(), nilfs_sc_cstage_get() are | |
82 | * wrapper functions of stage count (nilfs_sc_info->sc_stage.scnt). Users of | |
83 | * the variable must use them because transition of stage count must involve | |
84 | * trace events (trace_nilfs2_collection_stage_transition). | |
85 | * | |
86 | * nilfs_sc_cstage_get() isn't required for the above purpose because it doesn't | |
87 | * produce tracepoint events. It is provided just for making the intention | |
88 | * clear. | |
89 | */ | |
90 | static inline void nilfs_sc_cstage_inc(struct nilfs_sc_info *sci) | |
91 | { | |
92 | sci->sc_stage.scnt++; | |
93 | trace_nilfs2_collection_stage_transition(sci); | |
94 | } | |
95 | ||
96 | static inline void nilfs_sc_cstage_set(struct nilfs_sc_info *sci, int next_scnt) | |
97 | { | |
98 | sci->sc_stage.scnt = next_scnt; | |
99 | trace_nilfs2_collection_stage_transition(sci); | |
100 | } | |
101 | ||
102 | static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci) | |
103 | { | |
104 | return sci->sc_stage.scnt; | |
105 | } | |
106 | ||
9ff05123 RK |
107 | /* State flags of collection */ |
108 | #define NILFS_CF_NODE 0x0001 /* Collecting node blocks */ | |
109 | #define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */ | |
071cb4b8 RK |
110 | #define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */ |
111 | #define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED) | |
9ff05123 RK |
112 | |
113 | /* Operations depending on the construction mode and file type */ | |
114 | struct nilfs_sc_operations { | |
115 | int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *, | |
116 | struct inode *); | |
117 | int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *, | |
118 | struct inode *); | |
119 | int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *, | |
120 | struct inode *); | |
121 | void (*write_data_binfo)(struct nilfs_sc_info *, | |
122 | struct nilfs_segsum_pointer *, | |
123 | union nilfs_binfo *); | |
124 | void (*write_node_binfo)(struct nilfs_sc_info *, | |
125 | struct nilfs_segsum_pointer *, | |
126 | union nilfs_binfo *); | |
127 | }; | |
128 | ||
129 | /* | |
130 | * Other definitions | |
131 | */ | |
132 | static void nilfs_segctor_start_timer(struct nilfs_sc_info *); | |
133 | static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int); | |
134 | static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *); | |
693dd321 | 135 | static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int); |
9ff05123 | 136 | |
9ff05123 RK |
137 | #define nilfs_cnt32_ge(a, b) \ |
138 | (typecheck(__u32, a) && typecheck(__u32, b) && \ | |
0f3819e8 | 139 | ((__s32)((a) - (b)) >= 0)) |
9ff05123 | 140 | |
feee880f RK |
141 | static int nilfs_prepare_segment_lock(struct super_block *sb, |
142 | struct nilfs_transaction_info *ti) | |
9ff05123 RK |
143 | { |
144 | struct nilfs_transaction_info *cur_ti = current->journal_info; | |
145 | void *save = NULL; | |
146 | ||
147 | if (cur_ti) { | |
148 | if (cur_ti->ti_magic == NILFS_TI_MAGIC) | |
149 | return ++cur_ti->ti_count; | |
7f00184e RK |
150 | |
151 | /* | |
152 | * If journal_info field is occupied by other FS, | |
153 | * it is saved and will be restored on | |
154 | * nilfs_transaction_commit(). | |
155 | */ | |
a1d0747a | 156 | nilfs_warn(sb, "journal info from a different FS"); |
7f00184e | 157 | save = current->journal_info; |
9ff05123 RK |
158 | } |
159 | if (!ti) { | |
160 | ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS); | |
161 | if (!ti) | |
162 | return -ENOMEM; | |
163 | ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC; | |
164 | } else { | |
165 | ti->ti_flags = 0; | |
166 | } | |
167 | ti->ti_count = 0; | |
168 | ti->ti_save = save; | |
169 | ti->ti_magic = NILFS_TI_MAGIC; | |
170 | current->journal_info = ti; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | /** | |
175 | * nilfs_transaction_begin - start indivisible file operations. | |
176 | * @sb: super block | |
177 | * @ti: nilfs_transaction_info | |
178 | * @vacancy_check: flags for vacancy rate checks | |
179 | * | |
180 | * nilfs_transaction_begin() acquires a reader/writer semaphore, called | |
181 | * the segment semaphore, to make a segment construction and write tasks | |
47420c79 | 182 | * exclusive. The function is used with nilfs_transaction_commit() in pairs. |
9ff05123 RK |
183 | * The region enclosed by these two functions can be nested. To avoid a |
184 | * deadlock, the semaphore is only acquired or released in the outermost call. | |
185 | * | |
186 | * This function allocates a nilfs_transaction_info struct to keep context | |
187 | * information on it. It is initialized and hooked onto the current task in | |
188 | * the outermost call. If a pre-allocated struct is given to @ti, it is used | |
7a65004b | 189 | * instead; otherwise a new struct is assigned from a slab. |
9ff05123 RK |
190 | * |
191 | * When @vacancy_check flag is set, this function will check the amount of | |
192 | * free space, and will wait for the GC to reclaim disk space if low capacity. | |
193 | * | |
343d4a33 RK |
194 | * Return: 0 on success, or one of the following negative error codes on |
195 | * failure: | |
196 | * * %-ENOMEM - Insufficient memory available. | |
197 | * * %-ENOSPC - No space left on device (if checking free space). | |
9ff05123 RK |
198 | */ |
199 | int nilfs_transaction_begin(struct super_block *sb, | |
200 | struct nilfs_transaction_info *ti, | |
201 | int vacancy_check) | |
202 | { | |
9ff05123 | 203 | struct the_nilfs *nilfs; |
feee880f | 204 | int ret = nilfs_prepare_segment_lock(sb, ti); |
44fda114 | 205 | struct nilfs_transaction_info *trace_ti; |
9ff05123 RK |
206 | |
207 | if (unlikely(ret < 0)) | |
208 | return ret; | |
44fda114 HM |
209 | if (ret > 0) { |
210 | trace_ti = current->journal_info; | |
211 | ||
212 | trace_nilfs2_transaction_transition(sb, trace_ti, | |
213 | trace_ti->ti_count, trace_ti->ti_flags, | |
214 | TRACE_NILFS2_TRANSACTION_BEGIN); | |
9ff05123 | 215 | return 0; |
44fda114 | 216 | } |
9ff05123 | 217 | |
2c22b337 | 218 | sb_start_intwrite(sb); |
5beb6e0b | 219 | |
e3154e97 | 220 | nilfs = sb->s_fs_info; |
9ff05123 RK |
221 | down_read(&nilfs->ns_segctor_sem); |
222 | if (vacancy_check && nilfs_near_disk_full(nilfs)) { | |
223 | up_read(&nilfs->ns_segctor_sem); | |
224 | ret = -ENOSPC; | |
225 | goto failed; | |
226 | } | |
44fda114 HM |
227 | |
228 | trace_ti = current->journal_info; | |
229 | trace_nilfs2_transaction_transition(sb, trace_ti, trace_ti->ti_count, | |
230 | trace_ti->ti_flags, | |
231 | TRACE_NILFS2_TRANSACTION_BEGIN); | |
9ff05123 RK |
232 | return 0; |
233 | ||
234 | failed: | |
235 | ti = current->journal_info; | |
236 | current->journal_info = ti->ti_save; | |
237 | if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC) | |
238 | kmem_cache_free(nilfs_transaction_cachep, ti); | |
2c22b337 | 239 | sb_end_intwrite(sb); |
9ff05123 RK |
240 | return ret; |
241 | } | |
242 | ||
243 | /** | |
47420c79 | 244 | * nilfs_transaction_commit - commit indivisible file operations. |
9ff05123 | 245 | * @sb: super block |
9ff05123 | 246 | * |
47420c79 RK |
247 | * nilfs_transaction_commit() releases the read semaphore which is |
248 | * acquired by nilfs_transaction_begin(). This is only performed | |
249 | * in outermost call of this function. If a commit flag is set, | |
250 | * nilfs_transaction_commit() sets a timer to start the segment | |
251 | * constructor. If a sync flag is set, it starts construction | |
252 | * directly. | |
fd4e7fad RK |
253 | * |
254 | * Return: 0 on success, or a negative error code on failure. | |
9ff05123 | 255 | */ |
47420c79 | 256 | int nilfs_transaction_commit(struct super_block *sb) |
9ff05123 RK |
257 | { |
258 | struct nilfs_transaction_info *ti = current->journal_info; | |
e3154e97 | 259 | struct the_nilfs *nilfs = sb->s_fs_info; |
9ff05123 RK |
260 | int err = 0; |
261 | ||
262 | BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC); | |
47420c79 | 263 | ti->ti_flags |= NILFS_TI_COMMIT; |
9ff05123 RK |
264 | if (ti->ti_count > 0) { |
265 | ti->ti_count--; | |
44fda114 HM |
266 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, |
267 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT); | |
9ff05123 RK |
268 | return 0; |
269 | } | |
3fd3fe5a RK |
270 | if (nilfs->ns_writer) { |
271 | struct nilfs_sc_info *sci = nilfs->ns_writer; | |
272 | ||
9ff05123 RK |
273 | if (ti->ti_flags & NILFS_TI_COMMIT) |
274 | nilfs_segctor_start_timer(sci); | |
3fd3fe5a | 275 | if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark) |
9ff05123 RK |
276 | nilfs_segctor_do_flush(sci, 0); |
277 | } | |
3fd3fe5a | 278 | up_read(&nilfs->ns_segctor_sem); |
44fda114 HM |
279 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, |
280 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT); | |
281 | ||
9ff05123 RK |
282 | current->journal_info = ti->ti_save; |
283 | ||
284 | if (ti->ti_flags & NILFS_TI_SYNC) | |
285 | err = nilfs_construct_segment(sb); | |
286 | if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC) | |
287 | kmem_cache_free(nilfs_transaction_cachep, ti); | |
2c22b337 | 288 | sb_end_intwrite(sb); |
9ff05123 RK |
289 | return err; |
290 | } | |
291 | ||
47420c79 RK |
292 | void nilfs_transaction_abort(struct super_block *sb) |
293 | { | |
294 | struct nilfs_transaction_info *ti = current->journal_info; | |
e3154e97 | 295 | struct the_nilfs *nilfs = sb->s_fs_info; |
47420c79 RK |
296 | |
297 | BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC); | |
298 | if (ti->ti_count > 0) { | |
299 | ti->ti_count--; | |
44fda114 HM |
300 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, |
301 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT); | |
47420c79 RK |
302 | return; |
303 | } | |
e3154e97 | 304 | up_read(&nilfs->ns_segctor_sem); |
47420c79 | 305 | |
44fda114 HM |
306 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, |
307 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT); | |
308 | ||
47420c79 RK |
309 | current->journal_info = ti->ti_save; |
310 | if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC) | |
311 | kmem_cache_free(nilfs_transaction_cachep, ti); | |
2c22b337 | 312 | sb_end_intwrite(sb); |
47420c79 RK |
313 | } |
314 | ||
9ff05123 RK |
315 | void nilfs_relax_pressure_in_lock(struct super_block *sb) |
316 | { | |
e3154e97 | 317 | struct the_nilfs *nilfs = sb->s_fs_info; |
3fd3fe5a | 318 | struct nilfs_sc_info *sci = nilfs->ns_writer; |
9ff05123 | 319 | |
8cccf05f | 320 | if (sb_rdonly(sb) || unlikely(!sci) || !sci->sc_flush_request) |
9ff05123 RK |
321 | return; |
322 | ||
323 | set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags); | |
324 | up_read(&nilfs->ns_segctor_sem); | |
325 | ||
326 | down_write(&nilfs->ns_segctor_sem); | |
327 | if (sci->sc_flush_request && | |
328 | test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) { | |
329 | struct nilfs_transaction_info *ti = current->journal_info; | |
330 | ||
331 | ti->ti_flags |= NILFS_TI_WRITER; | |
332 | nilfs_segctor_do_immediate_flush(sci); | |
333 | ti->ti_flags &= ~NILFS_TI_WRITER; | |
334 | } | |
335 | downgrade_write(&nilfs->ns_segctor_sem); | |
336 | } | |
337 | ||
f7545144 | 338 | static void nilfs_transaction_lock(struct super_block *sb, |
9ff05123 RK |
339 | struct nilfs_transaction_info *ti, |
340 | int gcflag) | |
341 | { | |
342 | struct nilfs_transaction_info *cur_ti = current->journal_info; | |
e3154e97 | 343 | struct the_nilfs *nilfs = sb->s_fs_info; |
3fd3fe5a | 344 | struct nilfs_sc_info *sci = nilfs->ns_writer; |
9ff05123 | 345 | |
1f5abe7e | 346 | WARN_ON(cur_ti); |
9ff05123 RK |
347 | ti->ti_flags = NILFS_TI_WRITER; |
348 | ti->ti_count = 0; | |
349 | ti->ti_save = cur_ti; | |
350 | ti->ti_magic = NILFS_TI_MAGIC; | |
9ff05123 RK |
351 | current->journal_info = ti; |
352 | ||
353 | for (;;) { | |
44fda114 HM |
354 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, |
355 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_TRYLOCK); | |
356 | ||
3fd3fe5a RK |
357 | down_write(&nilfs->ns_segctor_sem); |
358 | if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) | |
9ff05123 RK |
359 | break; |
360 | ||
3fd3fe5a | 361 | nilfs_segctor_do_immediate_flush(sci); |
9ff05123 | 362 | |
f7545144 | 363 | up_write(&nilfs->ns_segctor_sem); |
aceb4170 | 364 | cond_resched(); |
9ff05123 RK |
365 | } |
366 | if (gcflag) | |
367 | ti->ti_flags |= NILFS_TI_GC; | |
44fda114 HM |
368 | |
369 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, | |
370 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_LOCK); | |
9ff05123 RK |
371 | } |
372 | ||
f7545144 | 373 | static void nilfs_transaction_unlock(struct super_block *sb) |
9ff05123 RK |
374 | { |
375 | struct nilfs_transaction_info *ti = current->journal_info; | |
e3154e97 | 376 | struct the_nilfs *nilfs = sb->s_fs_info; |
9ff05123 RK |
377 | |
378 | BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC); | |
379 | BUG_ON(ti->ti_count > 0); | |
380 | ||
693dd321 | 381 | up_write(&nilfs->ns_segctor_sem); |
9ff05123 | 382 | current->journal_info = ti->ti_save; |
44fda114 HM |
383 | |
384 | trace_nilfs2_transaction_transition(sb, ti, ti->ti_count, | |
385 | ti->ti_flags, TRACE_NILFS2_TRANSACTION_UNLOCK); | |
9ff05123 RK |
386 | } |
387 | ||
388 | static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci, | |
389 | struct nilfs_segsum_pointer *ssp, | |
0c6c44cb | 390 | unsigned int bytes) |
9ff05123 RK |
391 | { |
392 | struct nilfs_segment_buffer *segbuf = sci->sc_curseg; | |
0c6c44cb | 393 | unsigned int blocksize = sci->sc_super->s_blocksize; |
9ff05123 RK |
394 | void *p; |
395 | ||
396 | if (unlikely(ssp->offset + bytes > blocksize)) { | |
397 | ssp->offset = 0; | |
398 | BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh, | |
399 | &segbuf->sb_segsum_buffers)); | |
400 | ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh); | |
401 | } | |
402 | p = ssp->bh->b_data + ssp->offset; | |
403 | ssp->offset += bytes; | |
404 | return p; | |
405 | } | |
406 | ||
407 | /** | |
408 | * nilfs_segctor_reset_segment_buffer - reset the current segment buffer | |
409 | * @sci: nilfs_sc_info | |
fd4e7fad RK |
410 | * |
411 | * Return: 0 on success, or a negative error code on failure. | |
9ff05123 RK |
412 | */ |
413 | static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci) | |
414 | { | |
415 | struct nilfs_segment_buffer *segbuf = sci->sc_curseg; | |
416 | struct buffer_head *sumbh; | |
0c6c44cb RK |
417 | unsigned int sumbytes; |
418 | unsigned int flags = 0; | |
9ff05123 RK |
419 | int err; |
420 | ||
421 | if (nilfs_doing_gc()) | |
422 | flags = NILFS_SS_GC; | |
6c43f410 | 423 | err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno); |
9ff05123 RK |
424 | if (unlikely(err)) |
425 | return err; | |
426 | ||
427 | sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers); | |
428 | sumbytes = segbuf->sb_sum.sumbytes; | |
429 | sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes; | |
430 | sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes; | |
431 | sci->sc_blk_cnt = sci->sc_datablk_cnt = 0; | |
432 | return 0; | |
433 | } | |
434 | ||
ef832747 RK |
435 | /** |
436 | * nilfs_segctor_zeropad_segsum - zero pad the rest of the segment summary area | |
437 | * @sci: segment constructor object | |
438 | * | |
439 | * nilfs_segctor_zeropad_segsum() zero-fills unallocated space at the end of | |
440 | * the current segment summary block. | |
441 | */ | |
442 | static void nilfs_segctor_zeropad_segsum(struct nilfs_sc_info *sci) | |
443 | { | |
444 | struct nilfs_segsum_pointer *ssp; | |
445 | ||
446 | ssp = sci->sc_blk_cnt > 0 ? &sci->sc_binfo_ptr : &sci->sc_finfo_ptr; | |
447 | if (ssp->offset < ssp->bh->b_size) | |
448 | memset(ssp->bh->b_data + ssp->offset, 0, | |
449 | ssp->bh->b_size - ssp->offset); | |
450 | } | |
451 | ||
9ff05123 RK |
452 | static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci) |
453 | { | |
454 | sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks; | |
455 | if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs)) | |
076a378b RK |
456 | return -E2BIG; /* |
457 | * The current segment is filled up | |
458 | * (internal code) | |
459 | */ | |
ef832747 | 460 | nilfs_segctor_zeropad_segsum(sci); |
9ff05123 RK |
461 | sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg); |
462 | return nilfs_segctor_reset_segment_buffer(sci); | |
463 | } | |
464 | ||
465 | static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci) | |
466 | { | |
467 | struct nilfs_segment_buffer *segbuf = sci->sc_curseg; | |
468 | int err; | |
469 | ||
470 | if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) { | |
471 | err = nilfs_segctor_feed_segment(sci); | |
472 | if (err) | |
473 | return err; | |
474 | segbuf = sci->sc_curseg; | |
475 | } | |
1e2b68bf | 476 | err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root); |
9ff05123 RK |
477 | if (likely(!err)) |
478 | segbuf->sb_sum.flags |= NILFS_SS_SR; | |
479 | return err; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Functions for making segment summary and payloads | |
484 | */ | |
485 | static int nilfs_segctor_segsum_block_required( | |
486 | struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp, | |
0c6c44cb | 487 | unsigned int binfo_size) |
9ff05123 | 488 | { |
0c6c44cb | 489 | unsigned int blocksize = sci->sc_super->s_blocksize; |
9ff05123 RK |
490 | /* Size of finfo and binfo is enough small against blocksize */ |
491 | ||
492 | return ssp->offset + binfo_size + | |
493 | (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) > | |
494 | blocksize; | |
495 | } | |
496 | ||
497 | static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci, | |
498 | struct inode *inode) | |
499 | { | |
500 | sci->sc_curseg->sb_sum.nfinfo++; | |
501 | sci->sc_binfo_ptr = sci->sc_finfo_ptr; | |
502 | nilfs_segctor_map_segsum_entry( | |
503 | sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo)); | |
c96fa464 | 504 | |
72746ac6 RK |
505 | if (NILFS_I(inode)->i_root && |
506 | !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags)) | |
c96fa464 | 507 | set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags); |
9ff05123 RK |
508 | /* skip finfo */ |
509 | } | |
510 | ||
511 | static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci, | |
512 | struct inode *inode) | |
513 | { | |
514 | struct nilfs_finfo *finfo; | |
515 | struct nilfs_inode_info *ii; | |
516 | struct nilfs_segment_buffer *segbuf; | |
6c43f410 | 517 | __u64 cno; |
9ff05123 RK |
518 | |
519 | if (sci->sc_blk_cnt == 0) | |
520 | return; | |
521 | ||
522 | ii = NILFS_I(inode); | |
6c43f410 | 523 | |
d7cee0b3 | 524 | if (ii->i_type & NILFS_I_TYPE_GC) |
6c43f410 RK |
525 | cno = ii->i_cno; |
526 | else if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) | |
527 | cno = 0; | |
528 | else | |
529 | cno = sci->sc_cno; | |
530 | ||
9ff05123 RK |
531 | finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr, |
532 | sizeof(*finfo)); | |
533 | finfo->fi_ino = cpu_to_le64(inode->i_ino); | |
534 | finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt); | |
535 | finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt); | |
6c43f410 | 536 | finfo->fi_cno = cpu_to_le64(cno); |
9ff05123 RK |
537 | |
538 | segbuf = sci->sc_curseg; | |
539 | segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset + | |
540 | sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1); | |
541 | sci->sc_finfo_ptr = sci->sc_binfo_ptr; | |
542 | sci->sc_blk_cnt = sci->sc_datablk_cnt = 0; | |
543 | } | |
544 | ||
545 | static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci, | |
546 | struct buffer_head *bh, | |
547 | struct inode *inode, | |
0c6c44cb | 548 | unsigned int binfo_size) |
9ff05123 RK |
549 | { |
550 | struct nilfs_segment_buffer *segbuf; | |
551 | int required, err = 0; | |
552 | ||
553 | retry: | |
554 | segbuf = sci->sc_curseg; | |
555 | required = nilfs_segctor_segsum_block_required( | |
556 | sci, &sci->sc_binfo_ptr, binfo_size); | |
557 | if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) { | |
558 | nilfs_segctor_end_finfo(sci, inode); | |
559 | err = nilfs_segctor_feed_segment(sci); | |
560 | if (err) | |
561 | return err; | |
562 | goto retry; | |
563 | } | |
564 | if (unlikely(required)) { | |
ef832747 | 565 | nilfs_segctor_zeropad_segsum(sci); |
9ff05123 RK |
566 | err = nilfs_segbuf_extend_segsum(segbuf); |
567 | if (unlikely(err)) | |
568 | goto failed; | |
569 | } | |
570 | if (sci->sc_blk_cnt == 0) | |
571 | nilfs_segctor_begin_finfo(sci, inode); | |
572 | ||
573 | nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size); | |
574 | /* Substitution to vblocknr is delayed until update_blocknr() */ | |
575 | nilfs_segbuf_add_file_buffer(segbuf, bh); | |
576 | sci->sc_blk_cnt++; | |
577 | failed: | |
578 | return err; | |
579 | } | |
580 | ||
9ff05123 RK |
581 | /* |
582 | * Callback functions that enumerate, mark, and collect dirty blocks | |
583 | */ | |
584 | static int nilfs_collect_file_data(struct nilfs_sc_info *sci, | |
585 | struct buffer_head *bh, struct inode *inode) | |
586 | { | |
587 | int err; | |
588 | ||
9ff05123 | 589 | err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); |
e828949e RK |
590 | if (err < 0) |
591 | return err; | |
9ff05123 RK |
592 | |
593 | err = nilfs_segctor_add_file_block(sci, bh, inode, | |
594 | sizeof(struct nilfs_binfo_v)); | |
595 | if (!err) | |
596 | sci->sc_datablk_cnt++; | |
597 | return err; | |
598 | } | |
599 | ||
600 | static int nilfs_collect_file_node(struct nilfs_sc_info *sci, | |
601 | struct buffer_head *bh, | |
602 | struct inode *inode) | |
603 | { | |
e828949e | 604 | return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); |
9ff05123 RK |
605 | } |
606 | ||
607 | static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci, | |
608 | struct buffer_head *bh, | |
609 | struct inode *inode) | |
610 | { | |
1f5abe7e | 611 | WARN_ON(!buffer_dirty(bh)); |
9ff05123 RK |
612 | return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64)); |
613 | } | |
614 | ||
615 | static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci, | |
616 | struct nilfs_segsum_pointer *ssp, | |
617 | union nilfs_binfo *binfo) | |
618 | { | |
619 | struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry( | |
620 | sci, ssp, sizeof(*binfo_v)); | |
621 | *binfo_v = binfo->bi_v; | |
622 | } | |
623 | ||
624 | static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci, | |
625 | struct nilfs_segsum_pointer *ssp, | |
626 | union nilfs_binfo *binfo) | |
627 | { | |
628 | __le64 *vblocknr = nilfs_segctor_map_segsum_entry( | |
629 | sci, ssp, sizeof(*vblocknr)); | |
630 | *vblocknr = binfo->bi_v.bi_vblocknr; | |
631 | } | |
632 | ||
1c613cb9 | 633 | static const struct nilfs_sc_operations nilfs_sc_file_ops = { |
9ff05123 RK |
634 | .collect_data = nilfs_collect_file_data, |
635 | .collect_node = nilfs_collect_file_node, | |
636 | .collect_bmap = nilfs_collect_file_bmap, | |
637 | .write_data_binfo = nilfs_write_file_data_binfo, | |
638 | .write_node_binfo = nilfs_write_file_node_binfo, | |
639 | }; | |
640 | ||
641 | static int nilfs_collect_dat_data(struct nilfs_sc_info *sci, | |
642 | struct buffer_head *bh, struct inode *inode) | |
643 | { | |
644 | int err; | |
645 | ||
646 | err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); | |
e828949e RK |
647 | if (err < 0) |
648 | return err; | |
9ff05123 RK |
649 | |
650 | err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64)); | |
651 | if (!err) | |
652 | sci->sc_datablk_cnt++; | |
653 | return err; | |
654 | } | |
655 | ||
656 | static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci, | |
657 | struct buffer_head *bh, struct inode *inode) | |
658 | { | |
1f5abe7e | 659 | WARN_ON(!buffer_dirty(bh)); |
9ff05123 RK |
660 | return nilfs_segctor_add_file_block(sci, bh, inode, |
661 | sizeof(struct nilfs_binfo_dat)); | |
662 | } | |
663 | ||
664 | static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci, | |
665 | struct nilfs_segsum_pointer *ssp, | |
666 | union nilfs_binfo *binfo) | |
667 | { | |
668 | __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp, | |
669 | sizeof(*blkoff)); | |
670 | *blkoff = binfo->bi_dat.bi_blkoff; | |
671 | } | |
672 | ||
673 | static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci, | |
674 | struct nilfs_segsum_pointer *ssp, | |
675 | union nilfs_binfo *binfo) | |
676 | { | |
677 | struct nilfs_binfo_dat *binfo_dat = | |
678 | nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat)); | |
679 | *binfo_dat = binfo->bi_dat; | |
680 | } | |
681 | ||
1c613cb9 | 682 | static const struct nilfs_sc_operations nilfs_sc_dat_ops = { |
9ff05123 RK |
683 | .collect_data = nilfs_collect_dat_data, |
684 | .collect_node = nilfs_collect_file_node, | |
685 | .collect_bmap = nilfs_collect_dat_bmap, | |
686 | .write_data_binfo = nilfs_write_dat_data_binfo, | |
687 | .write_node_binfo = nilfs_write_dat_node_binfo, | |
688 | }; | |
689 | ||
1c613cb9 | 690 | static const struct nilfs_sc_operations nilfs_sc_dsync_ops = { |
9ff05123 RK |
691 | .collect_data = nilfs_collect_file_data, |
692 | .collect_node = NULL, | |
693 | .collect_bmap = NULL, | |
694 | .write_data_binfo = nilfs_write_file_data_binfo, | |
695 | .write_node_binfo = NULL, | |
696 | }; | |
697 | ||
f30bf3e4 RK |
698 | static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode, |
699 | struct list_head *listp, | |
700 | size_t nlimit, | |
701 | loff_t start, loff_t end) | |
9ff05123 | 702 | { |
9ff05123 | 703 | struct address_space *mapping = inode->i_mapping; |
5ee4b25c | 704 | struct folio_batch fbatch; |
f30bf3e4 RK |
705 | pgoff_t index = 0, last = ULONG_MAX; |
706 | size_t ndirties = 0; | |
707 | int i; | |
9ff05123 | 708 | |
f30bf3e4 RK |
709 | if (unlikely(start != 0 || end != LLONG_MAX)) { |
710 | /* | |
711 | * A valid range is given for sync-ing data pages. The | |
712 | * range is rounded to per-page; extra dirty buffers | |
713 | * may be included if blocksize < pagesize. | |
714 | */ | |
715 | index = start >> PAGE_SHIFT; | |
716 | last = end >> PAGE_SHIFT; | |
717 | } | |
5ee4b25c | 718 | folio_batch_init(&fbatch); |
9ff05123 | 719 | repeat: |
f30bf3e4 | 720 | if (unlikely(index > last) || |
5ee4b25c VMO |
721 | !filemap_get_folios_tag(mapping, &index, last, |
722 | PAGECACHE_TAG_DIRTY, &fbatch)) | |
f30bf3e4 | 723 | return ndirties; |
9ff05123 | 724 | |
5ee4b25c | 725 | for (i = 0; i < folio_batch_count(&fbatch); i++) { |
9ff05123 | 726 | struct buffer_head *bh, *head; |
5ee4b25c | 727 | struct folio *folio = fbatch.folios[i]; |
9ff05123 | 728 | |
5ee4b25c | 729 | folio_lock(folio); |
f83913f8 RK |
730 | if (unlikely(folio->mapping != mapping)) { |
731 | /* Exclude folios removed from the address space */ | |
732 | folio_unlock(folio); | |
733 | continue; | |
734 | } | |
5ee4b25c | 735 | head = folio_buffers(folio); |
922b12ef | 736 | if (!head) |
0a88810d | 737 | head = create_empty_buffers(folio, |
922b12ef | 738 | i_blocksize(inode), 0); |
9ff05123 | 739 | |
5ee4b25c | 740 | bh = head; |
9ff05123 | 741 | do { |
7f42ec39 | 742 | if (!buffer_dirty(bh) || buffer_async_write(bh)) |
f30bf3e4 RK |
743 | continue; |
744 | get_bh(bh); | |
745 | list_add_tail(&bh->b_assoc_buffers, listp); | |
746 | ndirties++; | |
747 | if (unlikely(ndirties >= nlimit)) { | |
367a9bff | 748 | folio_unlock(folio); |
5ee4b25c | 749 | folio_batch_release(&fbatch); |
f30bf3e4 RK |
750 | cond_resched(); |
751 | return ndirties; | |
9ff05123 | 752 | } |
f30bf3e4 | 753 | } while (bh = bh->b_this_page, bh != head); |
367a9bff RK |
754 | |
755 | folio_unlock(folio); | |
9ff05123 | 756 | } |
5ee4b25c | 757 | folio_batch_release(&fbatch); |
9ff05123 | 758 | cond_resched(); |
f30bf3e4 | 759 | goto repeat; |
9ff05123 RK |
760 | } |
761 | ||
762 | static void nilfs_lookup_dirty_node_buffers(struct inode *inode, | |
763 | struct list_head *listp) | |
764 | { | |
765 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
e897be17 | 766 | struct inode *btnc_inode = ii->i_assoc_inode; |
a2458658 | 767 | struct folio_batch fbatch; |
9ff05123 RK |
768 | struct buffer_head *bh, *head; |
769 | unsigned int i; | |
770 | pgoff_t index = 0; | |
771 | ||
e897be17 RK |
772 | if (!btnc_inode) |
773 | return; | |
a2458658 | 774 | folio_batch_init(&fbatch); |
e897be17 | 775 | |
a2458658 VMO |
776 | while (filemap_get_folios_tag(btnc_inode->i_mapping, &index, |
777 | (pgoff_t)-1, PAGECACHE_TAG_DIRTY, &fbatch)) { | |
778 | for (i = 0; i < folio_batch_count(&fbatch); i++) { | |
779 | bh = head = folio_buffers(fbatch.folios[i]); | |
9ff05123 | 780 | do { |
7f42ec39 VD |
781 | if (buffer_dirty(bh) && |
782 | !buffer_async_write(bh)) { | |
9ff05123 RK |
783 | get_bh(bh); |
784 | list_add_tail(&bh->b_assoc_buffers, | |
785 | listp); | |
786 | } | |
787 | bh = bh->b_this_page; | |
788 | } while (bh != head); | |
789 | } | |
a2458658 | 790 | folio_batch_release(&fbatch); |
9ff05123 RK |
791 | cond_resched(); |
792 | } | |
793 | } | |
794 | ||
693dd321 | 795 | static void nilfs_dispose_list(struct the_nilfs *nilfs, |
9ff05123 RK |
796 | struct list_head *head, int force) |
797 | { | |
798 | struct nilfs_inode_info *ii, *n; | |
799 | struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii; | |
0c6c44cb | 800 | unsigned int nv = 0; |
9ff05123 RK |
801 | |
802 | while (!list_empty(head)) { | |
693dd321 | 803 | spin_lock(&nilfs->ns_inode_lock); |
9ff05123 RK |
804 | list_for_each_entry_safe(ii, n, head, i_dirty) { |
805 | list_del_init(&ii->i_dirty); | |
806 | if (force) { | |
807 | if (unlikely(ii->i_bh)) { | |
808 | brelse(ii->i_bh); | |
809 | ii->i_bh = NULL; | |
810 | } | |
811 | } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) { | |
812 | set_bit(NILFS_I_QUEUED, &ii->i_state); | |
813 | list_add_tail(&ii->i_dirty, | |
693dd321 | 814 | &nilfs->ns_dirty_files); |
9ff05123 RK |
815 | continue; |
816 | } | |
817 | ivec[nv++] = ii; | |
818 | if (nv == SC_N_INODEVEC) | |
819 | break; | |
820 | } | |
693dd321 | 821 | spin_unlock(&nilfs->ns_inode_lock); |
9ff05123 RK |
822 | |
823 | for (pii = ivec; nv > 0; pii++, nv--) | |
824 | iput(&(*pii)->vfs_inode); | |
825 | } | |
826 | } | |
827 | ||
7ef3ff2f RK |
828 | static void nilfs_iput_work_func(struct work_struct *work) |
829 | { | |
830 | struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info, | |
831 | sc_iput_work); | |
832 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | |
833 | ||
834 | nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0); | |
835 | } | |
836 | ||
e912a5b6 RK |
837 | static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs, |
838 | struct nilfs_root *root) | |
9ff05123 | 839 | { |
9ff05123 RK |
840 | int ret = 0; |
841 | ||
e912a5b6 | 842 | if (nilfs_mdt_fetch_dirty(root->ifile)) |
9ff05123 RK |
843 | ret++; |
844 | if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile)) | |
845 | ret++; | |
846 | if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile)) | |
847 | ret++; | |
365e215c RK |
848 | if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat)) |
849 | ret++; | |
9ff05123 RK |
850 | return ret; |
851 | } | |
852 | ||
853 | static int nilfs_segctor_clean(struct nilfs_sc_info *sci) | |
854 | { | |
855 | return list_empty(&sci->sc_dirty_files) && | |
856 | !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) && | |
071cb4b8 | 857 | sci->sc_nfreesegs == 0 && |
9ff05123 RK |
858 | (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes)); |
859 | } | |
860 | ||
861 | static int nilfs_segctor_confirm(struct nilfs_sc_info *sci) | |
862 | { | |
e3154e97 | 863 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
9ff05123 RK |
864 | int ret = 0; |
865 | ||
693dd321 | 866 | if (nilfs_test_metadata_dirty(nilfs, sci->sc_root)) |
9ff05123 RK |
867 | set_bit(NILFS_SC_DIRTY, &sci->sc_flags); |
868 | ||
693dd321 RK |
869 | spin_lock(&nilfs->ns_inode_lock); |
870 | if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci)) | |
9ff05123 RK |
871 | ret++; |
872 | ||
693dd321 | 873 | spin_unlock(&nilfs->ns_inode_lock); |
9ff05123 RK |
874 | return ret; |
875 | } | |
876 | ||
877 | static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci) | |
878 | { | |
e3154e97 | 879 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
9ff05123 | 880 | |
e912a5b6 | 881 | nilfs_mdt_clear_dirty(sci->sc_root->ifile); |
9ff05123 RK |
882 | nilfs_mdt_clear_dirty(nilfs->ns_cpfile); |
883 | nilfs_mdt_clear_dirty(nilfs->ns_sufile); | |
365e215c | 884 | nilfs_mdt_clear_dirty(nilfs->ns_dat); |
9ff05123 RK |
885 | } |
886 | ||
9ff05123 RK |
887 | static void nilfs_fill_in_file_bmap(struct inode *ifile, |
888 | struct nilfs_inode_info *ii) | |
889 | ||
890 | { | |
891 | struct buffer_head *ibh; | |
892 | struct nilfs_inode *raw_inode; | |
893 | ||
894 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) { | |
895 | ibh = ii->i_bh; | |
896 | BUG_ON(!ibh); | |
897 | raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino, | |
898 | ibh); | |
899 | nilfs_bmap_write(ii->i_bmap, raw_inode); | |
7282f2ae | 900 | nilfs_ifile_unmap_inode(raw_inode); |
9ff05123 RK |
901 | } |
902 | } | |
903 | ||
e912a5b6 | 904 | static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci) |
9ff05123 RK |
905 | { |
906 | struct nilfs_inode_info *ii; | |
907 | ||
908 | list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) { | |
e912a5b6 | 909 | nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii); |
9ff05123 RK |
910 | set_bit(NILFS_I_COLLECTED, &ii->i_state); |
911 | } | |
9ff05123 RK |
912 | } |
913 | ||
9cced6a5 RK |
914 | /** |
915 | * nilfs_write_root_mdt_inode - export root metadata inode information to | |
916 | * the on-disk inode | |
917 | * @inode: inode object of the root metadata file | |
918 | * @raw_inode: on-disk inode | |
919 | * | |
920 | * nilfs_write_root_mdt_inode() writes inode information and bmap data of | |
921 | * @inode to the inode area of the metadata file allocated on the super root | |
922 | * block created to finalize the log. Since super root blocks are configured | |
923 | * each time, this function zero-fills the unused area of @raw_inode. | |
924 | */ | |
925 | static void nilfs_write_root_mdt_inode(struct inode *inode, | |
926 | struct nilfs_inode *raw_inode) | |
927 | { | |
928 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; | |
929 | ||
930 | nilfs_write_inode_common(inode, raw_inode); | |
931 | ||
932 | /* zero-fill unused portion of raw_inode */ | |
933 | raw_inode->i_xattr = 0; | |
934 | raw_inode->i_pad = 0; | |
935 | memset((void *)raw_inode + sizeof(*raw_inode), 0, | |
936 | nilfs->ns_inode_size - sizeof(*raw_inode)); | |
937 | ||
938 | nilfs_bmap_write(NILFS_I(inode)->i_bmap, raw_inode); | |
939 | } | |
940 | ||
9ff05123 RK |
941 | static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci, |
942 | struct the_nilfs *nilfs) | |
943 | { | |
1e2b68bf RK |
944 | struct buffer_head *bh_sr; |
945 | struct nilfs_super_root *raw_sr; | |
0c6c44cb | 946 | unsigned int isz, srsz; |
9ff05123 | 947 | |
1e2b68bf | 948 | bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root; |
679bd7eb RK |
949 | |
950 | lock_buffer(bh_sr); | |
1e2b68bf | 951 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; |
56eb5538 RK |
952 | isz = nilfs->ns_inode_size; |
953 | srsz = NILFS_SR_BYTES(isz); | |
1e2b68bf | 954 | |
679bd7eb | 955 | raw_sr->sr_sum = 0; /* Ensure initialization within this update */ |
56eb5538 | 956 | raw_sr->sr_bytes = cpu_to_le16(srsz); |
9ff05123 RK |
957 | raw_sr->sr_nongc_ctime |
958 | = cpu_to_le64(nilfs_doing_gc() ? | |
959 | nilfs->ns_nongc_ctime : sci->sc_seg_ctime); | |
960 | raw_sr->sr_flags = 0; | |
961 | ||
9cced6a5 RK |
962 | nilfs_write_root_mdt_inode(nilfs->ns_dat, (void *)raw_sr + |
963 | NILFS_SR_DAT_OFFSET(isz)); | |
964 | nilfs_write_root_mdt_inode(nilfs->ns_cpfile, (void *)raw_sr + | |
965 | NILFS_SR_CPFILE_OFFSET(isz)); | |
966 | nilfs_write_root_mdt_inode(nilfs->ns_sufile, (void *)raw_sr + | |
967 | NILFS_SR_SUFILE_OFFSET(isz)); | |
968 | ||
56eb5538 | 969 | memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz); |
679bd7eb RK |
970 | set_buffer_uptodate(bh_sr); |
971 | unlock_buffer(bh_sr); | |
9ff05123 RK |
972 | } |
973 | ||
974 | static void nilfs_redirty_inodes(struct list_head *head) | |
975 | { | |
976 | struct nilfs_inode_info *ii; | |
977 | ||
978 | list_for_each_entry(ii, head, i_dirty) { | |
979 | if (test_bit(NILFS_I_COLLECTED, &ii->i_state)) | |
980 | clear_bit(NILFS_I_COLLECTED, &ii->i_state); | |
981 | } | |
982 | } | |
983 | ||
984 | static void nilfs_drop_collected_inodes(struct list_head *head) | |
985 | { | |
986 | struct nilfs_inode_info *ii; | |
987 | ||
988 | list_for_each_entry(ii, head, i_dirty) { | |
989 | if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state)) | |
990 | continue; | |
991 | ||
b9f66140 | 992 | clear_bit(NILFS_I_INODE_SYNC, &ii->i_state); |
9ff05123 RK |
993 | set_bit(NILFS_I_UPDATED, &ii->i_state); |
994 | } | |
995 | } | |
996 | ||
9ff05123 RK |
997 | static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci, |
998 | struct inode *inode, | |
999 | struct list_head *listp, | |
1000 | int (*collect)(struct nilfs_sc_info *, | |
1001 | struct buffer_head *, | |
1002 | struct inode *)) | |
1003 | { | |
1004 | struct buffer_head *bh, *n; | |
1005 | int err = 0; | |
1006 | ||
1007 | if (collect) { | |
1008 | list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) { | |
1009 | list_del_init(&bh->b_assoc_buffers); | |
1010 | err = collect(sci, bh, inode); | |
1011 | brelse(bh); | |
1012 | if (unlikely(err)) | |
1013 | goto dispose_buffers; | |
1014 | } | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | dispose_buffers: | |
1019 | while (!list_empty(listp)) { | |
0cc12838 RK |
1020 | bh = list_first_entry(listp, struct buffer_head, |
1021 | b_assoc_buffers); | |
9ff05123 RK |
1022 | list_del_init(&bh->b_assoc_buffers); |
1023 | brelse(bh); | |
1024 | } | |
1025 | return err; | |
1026 | } | |
1027 | ||
f30bf3e4 RK |
1028 | static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci) |
1029 | { | |
1030 | /* Remaining number of blocks within segment buffer */ | |
1031 | return sci->sc_segbuf_nblocks - | |
1032 | (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks); | |
1033 | } | |
1034 | ||
9ff05123 RK |
1035 | static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci, |
1036 | struct inode *inode, | |
1c613cb9 | 1037 | const struct nilfs_sc_operations *sc_ops) |
9ff05123 RK |
1038 | { |
1039 | LIST_HEAD(data_buffers); | |
1040 | LIST_HEAD(node_buffers); | |
f30bf3e4 | 1041 | int err; |
9ff05123 RK |
1042 | |
1043 | if (!(sci->sc_stage.flags & NILFS_CF_NODE)) { | |
f30bf3e4 RK |
1044 | size_t n, rest = nilfs_segctor_buffer_rest(sci); |
1045 | ||
1046 | n = nilfs_lookup_dirty_data_buffers( | |
1047 | inode, &data_buffers, rest + 1, 0, LLONG_MAX); | |
1048 | if (n > rest) { | |
1049 | err = nilfs_segctor_apply_buffers( | |
9ff05123 | 1050 | sci, inode, &data_buffers, |
f30bf3e4 RK |
1051 | sc_ops->collect_data); |
1052 | BUG_ON(!err); /* always receive -E2BIG or true error */ | |
9ff05123 RK |
1053 | goto break_or_fail; |
1054 | } | |
1055 | } | |
1056 | nilfs_lookup_dirty_node_buffers(inode, &node_buffers); | |
1057 | ||
1058 | if (!(sci->sc_stage.flags & NILFS_CF_NODE)) { | |
1059 | err = nilfs_segctor_apply_buffers( | |
1060 | sci, inode, &data_buffers, sc_ops->collect_data); | |
1061 | if (unlikely(err)) { | |
1062 | /* dispose node list */ | |
1063 | nilfs_segctor_apply_buffers( | |
1064 | sci, inode, &node_buffers, NULL); | |
1065 | goto break_or_fail; | |
1066 | } | |
1067 | sci->sc_stage.flags |= NILFS_CF_NODE; | |
1068 | } | |
1069 | /* Collect node */ | |
1070 | err = nilfs_segctor_apply_buffers( | |
1071 | sci, inode, &node_buffers, sc_ops->collect_node); | |
1072 | if (unlikely(err)) | |
1073 | goto break_or_fail; | |
1074 | ||
1075 | nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers); | |
1076 | err = nilfs_segctor_apply_buffers( | |
1077 | sci, inode, &node_buffers, sc_ops->collect_bmap); | |
1078 | if (unlikely(err)) | |
1079 | goto break_or_fail; | |
1080 | ||
1081 | nilfs_segctor_end_finfo(sci, inode); | |
1082 | sci->sc_stage.flags &= ~NILFS_CF_NODE; | |
1083 | ||
1084 | break_or_fail: | |
1085 | return err; | |
1086 | } | |
1087 | ||
1088 | static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci, | |
1089 | struct inode *inode) | |
1090 | { | |
1091 | LIST_HEAD(data_buffers); | |
f30bf3e4 RK |
1092 | size_t n, rest = nilfs_segctor_buffer_rest(sci); |
1093 | int err; | |
9ff05123 | 1094 | |
f30bf3e4 RK |
1095 | n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1, |
1096 | sci->sc_dsync_start, | |
1097 | sci->sc_dsync_end); | |
1098 | ||
1099 | err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers, | |
1100 | nilfs_collect_file_data); | |
1101 | if (!err) { | |
9ff05123 | 1102 | nilfs_segctor_end_finfo(sci, inode); |
f30bf3e4 RK |
1103 | BUG_ON(n > rest); |
1104 | /* always receive -E2BIG or true error if n > rest */ | |
1105 | } | |
9ff05123 RK |
1106 | return err; |
1107 | } | |
1108 | ||
0b9aad46 RK |
1109 | /** |
1110 | * nilfs_free_segments - free the segments given by an array of segment numbers | |
1111 | * @nilfs: nilfs object | |
1112 | * @segnumv: array of segment numbers to be freed | |
1113 | * @nsegs: number of segments to be freed in @segnumv | |
1114 | * | |
1115 | * nilfs_free_segments() wraps nilfs_sufile_freev() and | |
1116 | * nilfs_sufile_cancel_freev(), and edits the segment usage metadata file | |
1117 | * (sufile) to free all segments given by @segnumv and @nsegs at once. If | |
1118 | * it fails midway, it cancels the changes so that none of the segments are | |
1119 | * freed. If @nsegs is 0, this function does nothing. | |
1120 | * | |
1121 | * The freeing of segments is not finalized until the writing of a log with | |
1122 | * a super root block containing this sufile change is complete, and it can | |
1123 | * be canceled with nilfs_sufile_cancel_freev() until then. | |
1124 | * | |
2e62857f RK |
1125 | * Return: 0 on success, or one of the following negative error codes on |
1126 | * failure: | |
0b9aad46 RK |
1127 | * * %-EINVAL - Invalid segment number. |
1128 | * * %-EIO - I/O error (including metadata corruption). | |
1129 | * * %-ENOMEM - Insufficient memory available. | |
1130 | */ | |
1131 | static int nilfs_free_segments(struct the_nilfs *nilfs, __u64 *segnumv, | |
1132 | size_t nsegs) | |
1133 | { | |
1134 | size_t ndone; | |
1135 | int ret; | |
1136 | ||
1137 | if (!nsegs) | |
1138 | return 0; | |
1139 | ||
1140 | ret = nilfs_sufile_freev(nilfs->ns_sufile, segnumv, nsegs, &ndone); | |
1141 | if (unlikely(ret)) { | |
1142 | nilfs_sufile_cancel_freev(nilfs->ns_sufile, segnumv, ndone, | |
1143 | NULL); | |
1144 | /* | |
1145 | * If a segment usage of the segments to be freed is in a | |
1146 | * hole block, nilfs_sufile_freev() will return -ENOENT. | |
1147 | * In this case, -EINVAL should be returned to the caller | |
1148 | * since there is something wrong with the given segment | |
1149 | * number array. This error can only occur during GC, so | |
1150 | * there is no need to worry about it propagating to other | |
1151 | * callers (such as fsync). | |
1152 | */ | |
1153 | if (ret == -ENOENT) { | |
1154 | nilfs_err(nilfs->ns_sb, | |
1155 | "The segment usage entry %llu to be freed is invalid (in a hole)", | |
1156 | (unsigned long long)segnumv[ndone]); | |
1157 | ret = -EINVAL; | |
1158 | } | |
1159 | } | |
1160 | return ret; | |
1161 | } | |
1162 | ||
9ff05123 RK |
1163 | static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode) |
1164 | { | |
e3154e97 | 1165 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
9ff05123 RK |
1166 | struct list_head *head; |
1167 | struct nilfs_inode_info *ii; | |
1168 | int err = 0; | |
1169 | ||
58497703 | 1170 | switch (nilfs_sc_cstage_get(sci)) { |
9ff05123 RK |
1171 | case NILFS_ST_INIT: |
1172 | /* Pre-processes */ | |
1173 | sci->sc_stage.flags = 0; | |
1174 | ||
1175 | if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) { | |
1176 | sci->sc_nblk_inc = 0; | |
1177 | sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN; | |
1178 | if (mode == SC_LSEG_DSYNC) { | |
58497703 | 1179 | nilfs_sc_cstage_set(sci, NILFS_ST_DSYNC); |
9ff05123 RK |
1180 | goto dsync_mode; |
1181 | } | |
1182 | } | |
1183 | ||
1184 | sci->sc_stage.dirty_file_ptr = NULL; | |
1185 | sci->sc_stage.gc_inode_ptr = NULL; | |
1186 | if (mode == SC_FLUSH_DAT) { | |
58497703 | 1187 | nilfs_sc_cstage_set(sci, NILFS_ST_DAT); |
9ff05123 RK |
1188 | goto dat_stage; |
1189 | } | |
df561f66 GS |
1190 | nilfs_sc_cstage_inc(sci); |
1191 | fallthrough; | |
9ff05123 RK |
1192 | case NILFS_ST_GC: |
1193 | if (nilfs_doing_gc()) { | |
1194 | head = &sci->sc_gc_inodes; | |
1195 | ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr, | |
1196 | head, i_dirty); | |
1197 | list_for_each_entry_continue(ii, head, i_dirty) { | |
1198 | err = nilfs_segctor_scan_file( | |
1199 | sci, &ii->vfs_inode, | |
1200 | &nilfs_sc_file_ops); | |
1201 | if (unlikely(err)) { | |
1202 | sci->sc_stage.gc_inode_ptr = list_entry( | |
1203 | ii->i_dirty.prev, | |
1204 | struct nilfs_inode_info, | |
1205 | i_dirty); | |
1206 | goto break_or_fail; | |
1207 | } | |
1208 | set_bit(NILFS_I_COLLECTED, &ii->i_state); | |
1209 | } | |
1210 | sci->sc_stage.gc_inode_ptr = NULL; | |
1211 | } | |
df561f66 GS |
1212 | nilfs_sc_cstage_inc(sci); |
1213 | fallthrough; | |
9ff05123 RK |
1214 | case NILFS_ST_FILE: |
1215 | head = &sci->sc_dirty_files; | |
1216 | ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head, | |
1217 | i_dirty); | |
1218 | list_for_each_entry_continue(ii, head, i_dirty) { | |
1219 | clear_bit(NILFS_I_DIRTY, &ii->i_state); | |
1220 | ||
1221 | err = nilfs_segctor_scan_file(sci, &ii->vfs_inode, | |
1222 | &nilfs_sc_file_ops); | |
1223 | if (unlikely(err)) { | |
1224 | sci->sc_stage.dirty_file_ptr = | |
1225 | list_entry(ii->i_dirty.prev, | |
1226 | struct nilfs_inode_info, | |
1227 | i_dirty); | |
1228 | goto break_or_fail; | |
1229 | } | |
1230 | /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */ | |
1231 | /* XXX: required ? */ | |
1232 | } | |
1233 | sci->sc_stage.dirty_file_ptr = NULL; | |
1234 | if (mode == SC_FLUSH_FILE) { | |
58497703 | 1235 | nilfs_sc_cstage_set(sci, NILFS_ST_DONE); |
9ff05123 RK |
1236 | return 0; |
1237 | } | |
58497703 | 1238 | nilfs_sc_cstage_inc(sci); |
9ff05123 | 1239 | sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED; |
df561f66 | 1240 | fallthrough; |
9ff05123 | 1241 | case NILFS_ST_IFILE: |
e912a5b6 | 1242 | err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile, |
9ff05123 RK |
1243 | &nilfs_sc_file_ops); |
1244 | if (unlikely(err)) | |
1245 | break; | |
58497703 | 1246 | nilfs_sc_cstage_inc(sci); |
9ff05123 | 1247 | /* Creating a checkpoint */ |
d37db936 RK |
1248 | err = nilfs_cpfile_create_checkpoint(nilfs->ns_cpfile, |
1249 | nilfs->ns_cno); | |
9ff05123 RK |
1250 | if (unlikely(err)) |
1251 | break; | |
df561f66 | 1252 | fallthrough; |
9ff05123 RK |
1253 | case NILFS_ST_CPFILE: |
1254 | err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile, | |
1255 | &nilfs_sc_file_ops); | |
1256 | if (unlikely(err)) | |
1257 | break; | |
df561f66 GS |
1258 | nilfs_sc_cstage_inc(sci); |
1259 | fallthrough; | |
9ff05123 | 1260 | case NILFS_ST_SUFILE: |
0b9aad46 RK |
1261 | err = nilfs_free_segments(nilfs, sci->sc_freesegs, |
1262 | sci->sc_nfreesegs); | |
1263 | if (unlikely(err)) | |
9ff05123 | 1264 | break; |
071cb4b8 RK |
1265 | sci->sc_stage.flags |= NILFS_CF_SUFREED; |
1266 | ||
9ff05123 RK |
1267 | err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile, |
1268 | &nilfs_sc_file_ops); | |
1269 | if (unlikely(err)) | |
1270 | break; | |
df561f66 GS |
1271 | nilfs_sc_cstage_inc(sci); |
1272 | fallthrough; | |
9ff05123 RK |
1273 | case NILFS_ST_DAT: |
1274 | dat_stage: | |
365e215c | 1275 | err = nilfs_segctor_scan_file(sci, nilfs->ns_dat, |
9ff05123 RK |
1276 | &nilfs_sc_dat_ops); |
1277 | if (unlikely(err)) | |
1278 | break; | |
1279 | if (mode == SC_FLUSH_DAT) { | |
58497703 | 1280 | nilfs_sc_cstage_set(sci, NILFS_ST_DONE); |
9ff05123 RK |
1281 | return 0; |
1282 | } | |
df561f66 GS |
1283 | nilfs_sc_cstage_inc(sci); |
1284 | fallthrough; | |
9ff05123 RK |
1285 | case NILFS_ST_SR: |
1286 | if (mode == SC_LSEG_SR) { | |
1287 | /* Appending a super root */ | |
1288 | err = nilfs_segctor_add_super_root(sci); | |
1289 | if (unlikely(err)) | |
1290 | break; | |
1291 | } | |
1292 | /* End of a logical segment */ | |
1293 | sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND; | |
58497703 | 1294 | nilfs_sc_cstage_set(sci, NILFS_ST_DONE); |
9ff05123 RK |
1295 | return 0; |
1296 | case NILFS_ST_DSYNC: | |
1297 | dsync_mode: | |
1298 | sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT; | |
f30bf3e4 | 1299 | ii = sci->sc_dsync_inode; |
9ff05123 RK |
1300 | if (!test_bit(NILFS_I_BUSY, &ii->i_state)) |
1301 | break; | |
1302 | ||
1303 | err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode); | |
1304 | if (unlikely(err)) | |
1305 | break; | |
9ff05123 | 1306 | sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND; |
58497703 | 1307 | nilfs_sc_cstage_set(sci, NILFS_ST_DONE); |
9ff05123 RK |
1308 | return 0; |
1309 | case NILFS_ST_DONE: | |
1310 | return 0; | |
1311 | default: | |
1312 | BUG(); | |
1313 | } | |
1314 | ||
1315 | break_or_fail: | |
1316 | return err; | |
1317 | } | |
1318 | ||
a694291a RK |
1319 | /** |
1320 | * nilfs_segctor_begin_construction - setup segment buffer to make a new log | |
1321 | * @sci: nilfs_sc_info | |
1322 | * @nilfs: nilfs object | |
fd4e7fad RK |
1323 | * |
1324 | * Return: 0 on success, or a negative error code on failure. | |
a694291a | 1325 | */ |
9ff05123 RK |
1326 | static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci, |
1327 | struct the_nilfs *nilfs) | |
1328 | { | |
a694291a | 1329 | struct nilfs_segment_buffer *segbuf, *prev; |
9ff05123 | 1330 | __u64 nextnum; |
a694291a | 1331 | int err, alloc = 0; |
9ff05123 | 1332 | |
a694291a RK |
1333 | segbuf = nilfs_segbuf_new(sci->sc_super); |
1334 | if (unlikely(!segbuf)) | |
1335 | return -ENOMEM; | |
9ff05123 | 1336 | |
a694291a RK |
1337 | if (list_empty(&sci->sc_write_logs)) { |
1338 | nilfs_segbuf_map(segbuf, nilfs->ns_segnum, | |
1339 | nilfs->ns_pseg_offset, nilfs); | |
1340 | if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) { | |
1341 | nilfs_shift_to_next_segment(nilfs); | |
1342 | nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs); | |
1343 | } | |
9ff05123 | 1344 | |
a694291a RK |
1345 | segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq; |
1346 | nextnum = nilfs->ns_nextnum; | |
1347 | ||
1348 | if (nilfs->ns_segnum == nilfs->ns_nextnum) | |
1349 | /* Start from the head of a new full segment */ | |
1350 | alloc++; | |
1351 | } else { | |
1352 | /* Continue logs */ | |
1353 | prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs); | |
1354 | nilfs_segbuf_map_cont(segbuf, prev); | |
1355 | segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq; | |
1356 | nextnum = prev->sb_nextnum; | |
1357 | ||
1358 | if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) { | |
1359 | nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs); | |
1360 | segbuf->sb_sum.seg_seq++; | |
1361 | alloc++; | |
1362 | } | |
9ff05123 | 1363 | } |
9ff05123 | 1364 | |
61a189e9 | 1365 | err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum); |
a694291a RK |
1366 | if (err) |
1367 | goto failed; | |
9ff05123 | 1368 | |
a694291a | 1369 | if (alloc) { |
cece5520 | 1370 | err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum); |
a694291a RK |
1371 | if (err) |
1372 | goto failed; | |
1373 | } | |
9ff05123 RK |
1374 | nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs); |
1375 | ||
a694291a RK |
1376 | BUG_ON(!list_empty(&sci->sc_segbufs)); |
1377 | list_add_tail(&segbuf->sb_list, &sci->sc_segbufs); | |
1378 | sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks; | |
cece5520 | 1379 | return 0; |
a694291a RK |
1380 | |
1381 | failed: | |
1382 | nilfs_segbuf_free(segbuf); | |
1383 | return err; | |
9ff05123 RK |
1384 | } |
1385 | ||
1386 | static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci, | |
1387 | struct the_nilfs *nilfs, int nadd) | |
1388 | { | |
e29df395 | 1389 | struct nilfs_segment_buffer *segbuf, *prev; |
9ff05123 RK |
1390 | struct inode *sufile = nilfs->ns_sufile; |
1391 | __u64 nextnextnum; | |
1392 | LIST_HEAD(list); | |
1393 | int err, ret, i; | |
1394 | ||
1395 | prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs); | |
1396 | /* | |
1397 | * Since the segment specified with nextnum might be allocated during | |
1398 | * the previous construction, the buffer including its segusage may | |
1399 | * not be dirty. The following call ensures that the buffer is dirty | |
1400 | * and will pin the buffer on memory until the sufile is written. | |
1401 | */ | |
61a189e9 | 1402 | err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum); |
9ff05123 RK |
1403 | if (unlikely(err)) |
1404 | return err; | |
1405 | ||
1406 | for (i = 0; i < nadd; i++) { | |
1407 | /* extend segment info */ | |
1408 | err = -ENOMEM; | |
1409 | segbuf = nilfs_segbuf_new(sci->sc_super); | |
1410 | if (unlikely(!segbuf)) | |
1411 | goto failed; | |
1412 | ||
1413 | /* map this buffer to region of segment on-disk */ | |
cece5520 | 1414 | nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs); |
9ff05123 RK |
1415 | sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks; |
1416 | ||
1417 | /* allocate the next next full segment */ | |
1418 | err = nilfs_sufile_alloc(sufile, &nextnextnum); | |
1419 | if (unlikely(err)) | |
1420 | goto failed_segbuf; | |
1421 | ||
1422 | segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1; | |
1423 | nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs); | |
1424 | ||
1425 | list_add_tail(&segbuf->sb_list, &list); | |
1426 | prev = segbuf; | |
1427 | } | |
0935db74 | 1428 | list_splice_tail(&list, &sci->sc_segbufs); |
9ff05123 RK |
1429 | return 0; |
1430 | ||
1431 | failed_segbuf: | |
1432 | nilfs_segbuf_free(segbuf); | |
1433 | failed: | |
e29df395 | 1434 | list_for_each_entry(segbuf, &list, sb_list) { |
9ff05123 | 1435 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); |
1f5abe7e | 1436 | WARN_ON(ret); /* never fails */ |
9ff05123 | 1437 | } |
e29df395 | 1438 | nilfs_destroy_logs(&list); |
9ff05123 RK |
1439 | return err; |
1440 | } | |
1441 | ||
a694291a RK |
1442 | static void nilfs_free_incomplete_logs(struct list_head *logs, |
1443 | struct the_nilfs *nilfs) | |
9ff05123 | 1444 | { |
a694291a RK |
1445 | struct nilfs_segment_buffer *segbuf, *prev; |
1446 | struct inode *sufile = nilfs->ns_sufile; | |
9284ad2a | 1447 | int ret; |
9ff05123 | 1448 | |
a694291a | 1449 | segbuf = NILFS_FIRST_SEGBUF(logs); |
9ff05123 | 1450 | if (nilfs->ns_nextnum != segbuf->sb_nextnum) { |
a694291a | 1451 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); |
1f5abe7e | 1452 | WARN_ON(ret); /* never fails */ |
9ff05123 | 1453 | } |
9284ad2a | 1454 | if (atomic_read(&segbuf->sb_err)) { |
9ff05123 RK |
1455 | /* Case 1: The first segment failed */ |
1456 | if (segbuf->sb_pseg_start != segbuf->sb_fseg_start) | |
076a378b RK |
1457 | /* |
1458 | * Case 1a: Partial segment appended into an existing | |
1459 | * segment | |
1460 | */ | |
9ff05123 RK |
1461 | nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start, |
1462 | segbuf->sb_fseg_end); | |
1463 | else /* Case 1b: New full segment */ | |
1464 | set_nilfs_discontinued(nilfs); | |
9ff05123 RK |
1465 | } |
1466 | ||
a694291a RK |
1467 | prev = segbuf; |
1468 | list_for_each_entry_continue(segbuf, logs, sb_list) { | |
1469 | if (prev->sb_nextnum != segbuf->sb_nextnum) { | |
1470 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); | |
1471 | WARN_ON(ret); /* never fails */ | |
1472 | } | |
9284ad2a RK |
1473 | if (atomic_read(&segbuf->sb_err) && |
1474 | segbuf->sb_segnum != nilfs->ns_nextnum) | |
1475 | /* Case 2: extended segment (!= next) failed */ | |
a694291a RK |
1476 | nilfs_sufile_set_error(sufile, segbuf->sb_segnum); |
1477 | prev = segbuf; | |
9ff05123 | 1478 | } |
9ff05123 RK |
1479 | } |
1480 | ||
1481 | static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci, | |
1482 | struct inode *sufile) | |
1483 | { | |
1484 | struct nilfs_segment_buffer *segbuf; | |
9ff05123 RK |
1485 | unsigned long live_blocks; |
1486 | int ret; | |
1487 | ||
1488 | list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) { | |
9ff05123 RK |
1489 | live_blocks = segbuf->sb_sum.nblocks + |
1490 | (segbuf->sb_pseg_start - segbuf->sb_fseg_start); | |
071ec54d RK |
1491 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, |
1492 | live_blocks, | |
1493 | sci->sc_seg_ctime); | |
1494 | WARN_ON(ret); /* always succeed because the segusage is dirty */ | |
9ff05123 RK |
1495 | } |
1496 | } | |
1497 | ||
a694291a | 1498 | static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile) |
9ff05123 RK |
1499 | { |
1500 | struct nilfs_segment_buffer *segbuf; | |
9ff05123 RK |
1501 | int ret; |
1502 | ||
a694291a | 1503 | segbuf = NILFS_FIRST_SEGBUF(logs); |
071ec54d RK |
1504 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, |
1505 | segbuf->sb_pseg_start - | |
1506 | segbuf->sb_fseg_start, 0); | |
1507 | WARN_ON(ret); /* always succeed because the segusage is dirty */ | |
9ff05123 | 1508 | |
a694291a | 1509 | list_for_each_entry_continue(segbuf, logs, sb_list) { |
071ec54d RK |
1510 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, |
1511 | 0, 0); | |
1f5abe7e | 1512 | WARN_ON(ret); /* always succeed */ |
9ff05123 RK |
1513 | } |
1514 | } | |
1515 | ||
1516 | static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci, | |
1517 | struct nilfs_segment_buffer *last, | |
1518 | struct inode *sufile) | |
1519 | { | |
e29df395 | 1520 | struct nilfs_segment_buffer *segbuf = last; |
9ff05123 RK |
1521 | int ret; |
1522 | ||
e29df395 | 1523 | list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) { |
9ff05123 RK |
1524 | sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks; |
1525 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); | |
1f5abe7e | 1526 | WARN_ON(ret); |
9ff05123 | 1527 | } |
e29df395 | 1528 | nilfs_truncate_logs(&sci->sc_segbufs, last); |
9ff05123 RK |
1529 | } |
1530 | ||
1531 | ||
1532 | static int nilfs_segctor_collect(struct nilfs_sc_info *sci, | |
1533 | struct the_nilfs *nilfs, int mode) | |
1534 | { | |
1535 | struct nilfs_cstage prev_stage = sci->sc_stage; | |
1536 | int err, nadd = 1; | |
1537 | ||
1538 | /* Collection retry loop */ | |
1539 | for (;;) { | |
9ff05123 RK |
1540 | sci->sc_nblk_this_inc = 0; |
1541 | sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs); | |
1542 | ||
1543 | err = nilfs_segctor_reset_segment_buffer(sci); | |
1544 | if (unlikely(err)) | |
1545 | goto failed; | |
1546 | ||
1547 | err = nilfs_segctor_collect_blocks(sci, mode); | |
1548 | sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks; | |
1549 | if (!err) | |
1550 | break; | |
1551 | ||
1552 | if (unlikely(err != -E2BIG)) | |
1553 | goto failed; | |
1554 | ||
1555 | /* The current segment is filled up */ | |
58497703 HM |
1556 | if (mode != SC_LSEG_SR || |
1557 | nilfs_sc_cstage_get(sci) < NILFS_ST_CPFILE) | |
9ff05123 RK |
1558 | break; |
1559 | ||
2d8428ac RK |
1560 | nilfs_clear_logs(&sci->sc_segbufs); |
1561 | ||
071cb4b8 RK |
1562 | if (sci->sc_stage.flags & NILFS_CF_SUFREED) { |
1563 | err = nilfs_sufile_cancel_freev(nilfs->ns_sufile, | |
1564 | sci->sc_freesegs, | |
1565 | sci->sc_nfreesegs, | |
1566 | NULL); | |
1567 | WARN_ON(err); /* do not happen */ | |
70f2fe3a | 1568 | sci->sc_stage.flags &= ~NILFS_CF_SUFREED; |
071cb4b8 | 1569 | } |
70f2fe3a AR |
1570 | |
1571 | err = nilfs_segctor_extend_segments(sci, nilfs, nadd); | |
1572 | if (unlikely(err)) | |
1573 | return err; | |
1574 | ||
9ff05123 RK |
1575 | nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA); |
1576 | sci->sc_stage = prev_stage; | |
1577 | } | |
ef832747 | 1578 | nilfs_segctor_zeropad_segsum(sci); |
9ff05123 RK |
1579 | nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile); |
1580 | return 0; | |
1581 | ||
1582 | failed: | |
1583 | return err; | |
1584 | } | |
1585 | ||
1586 | static void nilfs_list_replace_buffer(struct buffer_head *old_bh, | |
1587 | struct buffer_head *new_bh) | |
1588 | { | |
1589 | BUG_ON(!list_empty(&new_bh->b_assoc_buffers)); | |
1590 | ||
1591 | list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers); | |
1592 | /* The caller must release old_bh */ | |
1593 | } | |
1594 | ||
1595 | static int | |
1596 | nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci, | |
1597 | struct nilfs_segment_buffer *segbuf, | |
1598 | int mode) | |
1599 | { | |
1600 | struct inode *inode = NULL; | |
1601 | sector_t blocknr; | |
1602 | unsigned long nfinfo = segbuf->sb_sum.nfinfo; | |
1603 | unsigned long nblocks = 0, ndatablk = 0; | |
1c613cb9 | 1604 | const struct nilfs_sc_operations *sc_op = NULL; |
9ff05123 RK |
1605 | struct nilfs_segsum_pointer ssp; |
1606 | struct nilfs_finfo *finfo = NULL; | |
1607 | union nilfs_binfo binfo; | |
1608 | struct buffer_head *bh, *bh_org; | |
1609 | ino_t ino = 0; | |
1610 | int err = 0; | |
1611 | ||
1612 | if (!nfinfo) | |
1613 | goto out; | |
1614 | ||
1615 | blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk; | |
1616 | ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers); | |
1617 | ssp.offset = sizeof(struct nilfs_segment_summary); | |
1618 | ||
1619 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { | |
1e2b68bf | 1620 | if (bh == segbuf->sb_super_root) |
9ff05123 RK |
1621 | break; |
1622 | if (!finfo) { | |
1623 | finfo = nilfs_segctor_map_segsum_entry( | |
1624 | sci, &ssp, sizeof(*finfo)); | |
1625 | ino = le64_to_cpu(finfo->fi_ino); | |
1626 | nblocks = le32_to_cpu(finfo->fi_nblocks); | |
1627 | ndatablk = le32_to_cpu(finfo->fi_ndatablk); | |
1628 | ||
6ad4cd7f | 1629 | inode = bh->b_folio->mapping->host; |
9ff05123 RK |
1630 | |
1631 | if (mode == SC_LSEG_DSYNC) | |
1632 | sc_op = &nilfs_sc_dsync_ops; | |
1633 | else if (ino == NILFS_DAT_INO) | |
1634 | sc_op = &nilfs_sc_dat_ops; | |
1635 | else /* file blocks */ | |
1636 | sc_op = &nilfs_sc_file_ops; | |
1637 | } | |
1638 | bh_org = bh; | |
1639 | get_bh(bh_org); | |
1640 | err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr, | |
1641 | &binfo); | |
1642 | if (bh != bh_org) | |
1643 | nilfs_list_replace_buffer(bh_org, bh); | |
1644 | brelse(bh_org); | |
1645 | if (unlikely(err)) | |
1646 | goto failed_bmap; | |
1647 | ||
1648 | if (ndatablk > 0) | |
1649 | sc_op->write_data_binfo(sci, &ssp, &binfo); | |
1650 | else | |
1651 | sc_op->write_node_binfo(sci, &ssp, &binfo); | |
1652 | ||
1653 | blocknr++; | |
1654 | if (--nblocks == 0) { | |
1655 | finfo = NULL; | |
1656 | if (--nfinfo == 0) | |
1657 | break; | |
1658 | } else if (ndatablk > 0) | |
1659 | ndatablk--; | |
1660 | } | |
1661 | out: | |
1662 | return 0; | |
1663 | ||
1664 | failed_bmap: | |
9ff05123 RK |
1665 | return err; |
1666 | } | |
1667 | ||
1668 | static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode) | |
1669 | { | |
1670 | struct nilfs_segment_buffer *segbuf; | |
1671 | int err; | |
1672 | ||
1673 | list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) { | |
1674 | err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode); | |
1675 | if (unlikely(err)) | |
1676 | return err; | |
1677 | nilfs_segbuf_fill_in_segsum(segbuf); | |
1678 | } | |
1679 | return 0; | |
1680 | } | |
1681 | ||
ff5710c3 | 1682 | static void nilfs_begin_folio_io(struct folio *folio) |
9ff05123 | 1683 | { |
ff5710c3 | 1684 | if (!folio || folio_test_writeback(folio)) |
076a378b RK |
1685 | /* |
1686 | * For split b-tree node pages, this function may be called | |
1687 | * twice. We ignore the 2nd or later calls by this check. | |
1688 | */ | |
1cb2d38c | 1689 | return; |
9ff05123 | 1690 | |
ff5710c3 MWO |
1691 | folio_lock(folio); |
1692 | folio_clear_dirty_for_io(folio); | |
1693 | folio_start_writeback(folio); | |
1694 | folio_unlock(folio); | |
9ff05123 RK |
1695 | } |
1696 | ||
25fa5f99 RK |
1697 | /** |
1698 | * nilfs_prepare_write_logs - prepare to write logs | |
1699 | * @logs: logs to prepare for writing | |
1700 | * @seed: checksum seed value | |
1701 | * | |
1702 | * nilfs_prepare_write_logs() adds checksums and prepares the block | |
1703 | * buffers/folios for writing logs. In order to stabilize folios of | |
1704 | * memory-mapped file blocks by putting them in writeback state before | |
1705 | * calculating the checksums, first prepare to write payload blocks other | |
1706 | * than segment summary and super root blocks in which the checksums will | |
1707 | * be embedded. | |
1708 | */ | |
1709 | static void nilfs_prepare_write_logs(struct list_head *logs, u32 seed) | |
9ff05123 RK |
1710 | { |
1711 | struct nilfs_segment_buffer *segbuf; | |
ff5710c3 | 1712 | struct folio *bd_folio = NULL, *fs_folio = NULL; |
25fa5f99 | 1713 | struct buffer_head *bh; |
9ff05123 | 1714 | |
25fa5f99 RK |
1715 | /* Prepare to write payload blocks */ |
1716 | list_for_each_entry(segbuf, logs, sb_list) { | |
9ff05123 RK |
1717 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, |
1718 | b_assoc_buffers) { | |
25fa5f99 | 1719 | if (bh == segbuf->sb_super_root) |
9ff05123 | 1720 | break; |
5bc09b39 | 1721 | set_buffer_async_write(bh); |
ff5710c3 MWO |
1722 | if (bh->b_folio != fs_folio) { |
1723 | nilfs_begin_folio_io(fs_folio); | |
1724 | fs_folio = bh->b_folio; | |
9ff05123 RK |
1725 | } |
1726 | } | |
1727 | } | |
25fa5f99 RK |
1728 | nilfs_begin_folio_io(fs_folio); |
1729 | ||
1730 | nilfs_add_checksums_on_logs(logs, seed); | |
1731 | ||
1732 | /* Prepare to write segment summary blocks */ | |
1733 | list_for_each_entry(segbuf, logs, sb_list) { | |
1734 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, | |
1735 | b_assoc_buffers) { | |
68142cb6 | 1736 | mark_buffer_dirty(bh); |
25fa5f99 RK |
1737 | if (bh->b_folio == bd_folio) |
1738 | continue; | |
1739 | if (bd_folio) { | |
1740 | folio_lock(bd_folio); | |
1741 | folio_wait_writeback(bd_folio); | |
1742 | folio_clear_dirty_for_io(bd_folio); | |
1743 | folio_start_writeback(bd_folio); | |
1744 | folio_unlock(bd_folio); | |
1745 | } | |
1746 | bd_folio = bh->b_folio; | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | /* Prepare to write super root block */ | |
1751 | bh = NILFS_LAST_SEGBUF(logs)->sb_super_root; | |
1752 | if (bh) { | |
68142cb6 | 1753 | mark_buffer_dirty(bh); |
25fa5f99 RK |
1754 | if (bh->b_folio != bd_folio) { |
1755 | folio_lock(bd_folio); | |
1756 | folio_wait_writeback(bd_folio); | |
1757 | folio_clear_dirty_for_io(bd_folio); | |
1758 | folio_start_writeback(bd_folio); | |
1759 | folio_unlock(bd_folio); | |
1760 | bd_folio = bh->b_folio; | |
1761 | } | |
1762 | } | |
1763 | ||
ff5710c3 MWO |
1764 | if (bd_folio) { |
1765 | folio_lock(bd_folio); | |
a4ca369c | 1766 | folio_wait_writeback(bd_folio); |
ff5710c3 MWO |
1767 | folio_clear_dirty_for_io(bd_folio); |
1768 | folio_start_writeback(bd_folio); | |
1769 | folio_unlock(bd_folio); | |
9ff05123 | 1770 | } |
9ff05123 RK |
1771 | } |
1772 | ||
1773 | static int nilfs_segctor_write(struct nilfs_sc_info *sci, | |
9c965bac | 1774 | struct the_nilfs *nilfs) |
9ff05123 | 1775 | { |
d1c6b72a | 1776 | int ret; |
9ff05123 | 1777 | |
d1c6b72a | 1778 | ret = nilfs_write_logs(&sci->sc_segbufs, nilfs); |
a694291a RK |
1779 | list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs); |
1780 | return ret; | |
9ff05123 RK |
1781 | } |
1782 | ||
8f46eaf6 | 1783 | static void nilfs_end_folio_io(struct folio *folio, int err) |
9ff05123 | 1784 | { |
8f46eaf6 | 1785 | if (!folio) |
9ff05123 RK |
1786 | return; |
1787 | ||
8f46eaf6 MWO |
1788 | if (buffer_nilfs_node(folio_buffers(folio)) && |
1789 | !folio_test_writeback(folio)) { | |
8227b297 RK |
1790 | /* |
1791 | * For b-tree node pages, this function may be called twice | |
1792 | * or more because they might be split in a segment. | |
1793 | */ | |
8f46eaf6 | 1794 | if (folio_test_dirty(folio)) { |
a9777845 RK |
1795 | /* |
1796 | * For pages holding split b-tree node buffers, dirty | |
1797 | * flag on the buffers may be cleared discretely. | |
1798 | * In that case, the page is once redirtied for | |
1799 | * remaining buffers, and it must be cancelled if | |
1800 | * all the buffers get cleaned later. | |
1801 | */ | |
8f46eaf6 | 1802 | folio_lock(folio); |
36319c0c | 1803 | if (nilfs_folio_buffers_clean(folio)) |
6609e235 | 1804 | __nilfs_clear_folio_dirty(folio); |
8f46eaf6 | 1805 | folio_unlock(folio); |
a9777845 | 1806 | } |
9ff05123 | 1807 | return; |
a9777845 | 1808 | } |
9ff05123 | 1809 | |
a7ac59f4 | 1810 | if (err || !nilfs_folio_buffers_clean(folio)) |
8f46eaf6 | 1811 | filemap_dirty_folio(folio->mapping, folio); |
1cb2d38c | 1812 | |
8f46eaf6 MWO |
1813 | folio_end_writeback(folio); |
1814 | } | |
1815 | ||
1cb2d38c | 1816 | static void nilfs_abort_logs(struct list_head *logs, int err) |
9ff05123 RK |
1817 | { |
1818 | struct nilfs_segment_buffer *segbuf; | |
50196f00 | 1819 | struct folio *bd_folio = NULL, *fs_folio = NULL; |
a694291a | 1820 | struct buffer_head *bh; |
9ff05123 | 1821 | |
a694291a RK |
1822 | if (list_empty(logs)) |
1823 | return; | |
9ff05123 | 1824 | |
a694291a | 1825 | list_for_each_entry(segbuf, logs, sb_list) { |
9ff05123 RK |
1826 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, |
1827 | b_assoc_buffers) { | |
679bd7eb | 1828 | clear_buffer_uptodate(bh); |
50196f00 MWO |
1829 | if (bh->b_folio != bd_folio) { |
1830 | if (bd_folio) | |
1831 | folio_end_writeback(bd_folio); | |
1832 | bd_folio = bh->b_folio; | |
9ff05123 RK |
1833 | } |
1834 | } | |
1835 | ||
1836 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, | |
1837 | b_assoc_buffers) { | |
1e2b68bf | 1838 | if (bh == segbuf->sb_super_root) { |
679bd7eb | 1839 | clear_buffer_uptodate(bh); |
50196f00 MWO |
1840 | if (bh->b_folio != bd_folio) { |
1841 | folio_end_writeback(bd_folio); | |
1842 | bd_folio = bh->b_folio; | |
9ff05123 RK |
1843 | } |
1844 | break; | |
1845 | } | |
5bc09b39 | 1846 | clear_buffer_async_write(bh); |
50196f00 MWO |
1847 | if (bh->b_folio != fs_folio) { |
1848 | nilfs_end_folio_io(fs_folio, err); | |
1849 | fs_folio = bh->b_folio; | |
9ff05123 RK |
1850 | } |
1851 | } | |
1852 | } | |
50196f00 MWO |
1853 | if (bd_folio) |
1854 | folio_end_writeback(bd_folio); | |
9ff05123 | 1855 | |
50196f00 | 1856 | nilfs_end_folio_io(fs_folio, err); |
a694291a RK |
1857 | } |
1858 | ||
1859 | static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci, | |
1860 | struct the_nilfs *nilfs, int err) | |
1861 | { | |
1862 | LIST_HEAD(logs); | |
1863 | int ret; | |
1864 | ||
1865 | list_splice_tail_init(&sci->sc_write_logs, &logs); | |
1866 | ret = nilfs_wait_on_logs(&logs); | |
1cb2d38c | 1867 | nilfs_abort_logs(&logs, ret ? : err); |
a694291a RK |
1868 | |
1869 | list_splice_tail_init(&sci->sc_segbufs, &logs); | |
6576dd66 RK |
1870 | if (list_empty(&logs)) |
1871 | return; /* if the first segment buffer preparation failed */ | |
1872 | ||
a694291a RK |
1873 | nilfs_cancel_segusage(&logs, nilfs->ns_sufile); |
1874 | nilfs_free_incomplete_logs(&logs, nilfs); | |
a694291a RK |
1875 | |
1876 | if (sci->sc_stage.flags & NILFS_CF_SUFREED) { | |
1877 | ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile, | |
1878 | sci->sc_freesegs, | |
1879 | sci->sc_nfreesegs, | |
1880 | NULL); | |
1881 | WARN_ON(ret); /* do not happen */ | |
1882 | } | |
1883 | ||
1884 | nilfs_destroy_logs(&logs); | |
9ff05123 RK |
1885 | } |
1886 | ||
1887 | static void nilfs_set_next_segment(struct the_nilfs *nilfs, | |
1888 | struct nilfs_segment_buffer *segbuf) | |
1889 | { | |
1890 | nilfs->ns_segnum = segbuf->sb_segnum; | |
1891 | nilfs->ns_nextnum = segbuf->sb_nextnum; | |
1892 | nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start | |
1893 | + segbuf->sb_sum.nblocks; | |
1894 | nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq; | |
1895 | nilfs->ns_ctime = segbuf->sb_sum.ctime; | |
1896 | } | |
1897 | ||
1898 | static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci) | |
1899 | { | |
1900 | struct nilfs_segment_buffer *segbuf; | |
3cd36212 | 1901 | struct folio *bd_folio = NULL, *fs_folio = NULL; |
e3154e97 | 1902 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
1e2b68bf | 1903 | int update_sr = false; |
9ff05123 | 1904 | |
a694291a | 1905 | list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) { |
9ff05123 RK |
1906 | struct buffer_head *bh; |
1907 | ||
1908 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, | |
1909 | b_assoc_buffers) { | |
1910 | set_buffer_uptodate(bh); | |
1911 | clear_buffer_dirty(bh); | |
3cd36212 MWO |
1912 | if (bh->b_folio != bd_folio) { |
1913 | if (bd_folio) | |
1914 | folio_end_writeback(bd_folio); | |
1915 | bd_folio = bh->b_folio; | |
9ff05123 RK |
1916 | } |
1917 | } | |
1918 | /* | |
3cd36212 | 1919 | * We assume that the buffers which belong to the same folio |
9ff05123 | 1920 | * continue over the buffer list. |
3cd36212 MWO |
1921 | * Under this assumption, the last BHs of folios is |
1922 | * identifiable by the discontinuity of bh->b_folio | |
1923 | * (folio != fs_folio). | |
9ff05123 RK |
1924 | * |
1925 | * For B-tree node blocks, however, this assumption is not | |
3cd36212 | 1926 | * guaranteed. The cleanup code of B-tree node folios needs |
9ff05123 RK |
1927 | * special care. |
1928 | */ | |
1929 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, | |
1930 | b_assoc_buffers) { | |
4ce5c342 | 1931 | const unsigned long set_bits = BIT(BH_Uptodate); |
ead8ecff | 1932 | const unsigned long clear_bits = |
4ce5c342 RK |
1933 | (BIT(BH_Dirty) | BIT(BH_Async_Write) | |
1934 | BIT(BH_Delay) | BIT(BH_NILFS_Volatile) | | |
1935 | BIT(BH_NILFS_Redirected)); | |
ead8ecff | 1936 | |
1e2b68bf | 1937 | if (bh == segbuf->sb_super_root) { |
5bc09b39 RK |
1938 | set_buffer_uptodate(bh); |
1939 | clear_buffer_dirty(bh); | |
3cd36212 MWO |
1940 | if (bh->b_folio != bd_folio) { |
1941 | folio_end_writeback(bd_folio); | |
1942 | bd_folio = bh->b_folio; | |
9ff05123 | 1943 | } |
1e2b68bf | 1944 | update_sr = true; |
9ff05123 RK |
1945 | break; |
1946 | } | |
5bc09b39 | 1947 | set_mask_bits(&bh->b_state, clear_bits, set_bits); |
3cd36212 MWO |
1948 | if (bh->b_folio != fs_folio) { |
1949 | nilfs_end_folio_io(fs_folio, 0); | |
1950 | fs_folio = bh->b_folio; | |
9ff05123 RK |
1951 | } |
1952 | } | |
1953 | ||
4762077c RK |
1954 | if (!nilfs_segbuf_simplex(segbuf)) { |
1955 | if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) { | |
9ff05123 RK |
1956 | set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags); |
1957 | sci->sc_lseg_stime = jiffies; | |
1958 | } | |
4762077c | 1959 | if (segbuf->sb_sum.flags & NILFS_SS_LOGEND) |
9ff05123 RK |
1960 | clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags); |
1961 | } | |
1962 | } | |
1963 | /* | |
3cd36212 MWO |
1964 | * Since folios may continue over multiple segment buffers, |
1965 | * end of the last folio must be checked outside of the loop. | |
9ff05123 | 1966 | */ |
3cd36212 MWO |
1967 | if (bd_folio) |
1968 | folio_end_writeback(bd_folio); | |
9ff05123 | 1969 | |
3cd36212 | 1970 | nilfs_end_folio_io(fs_folio, 0); |
9ff05123 | 1971 | |
9ff05123 RK |
1972 | nilfs_drop_collected_inodes(&sci->sc_dirty_files); |
1973 | ||
c1c1d709 | 1974 | if (nilfs_doing_gc()) |
9ff05123 | 1975 | nilfs_drop_collected_inodes(&sci->sc_gc_inodes); |
c1c1d709 | 1976 | else |
9ff05123 | 1977 | nilfs->ns_nongc_ctime = sci->sc_seg_ctime; |
9ff05123 RK |
1978 | |
1979 | sci->sc_nblk_inc += sci->sc_nblk_this_inc; | |
1980 | ||
a694291a | 1981 | segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs); |
9ff05123 RK |
1982 | nilfs_set_next_segment(nilfs, segbuf); |
1983 | ||
1984 | if (update_sr) { | |
e2c7617a | 1985 | nilfs->ns_flushed_device = 0; |
9ff05123 | 1986 | nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start, |
e339ad31 | 1987 | segbuf->sb_sum.seg_seq, nilfs->ns_cno++); |
9ff05123 | 1988 | |
c96fa464 | 1989 | clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags); |
9ff05123 RK |
1990 | clear_bit(NILFS_SC_DIRTY, &sci->sc_flags); |
1991 | set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags); | |
a694291a | 1992 | nilfs_segctor_clear_metadata_dirty(sci); |
9ff05123 RK |
1993 | } else |
1994 | clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags); | |
1995 | } | |
1996 | ||
a694291a RK |
1997 | static int nilfs_segctor_wait(struct nilfs_sc_info *sci) |
1998 | { | |
1999 | int ret; | |
2000 | ||
2001 | ret = nilfs_wait_on_logs(&sci->sc_write_logs); | |
2002 | if (!ret) { | |
2003 | nilfs_segctor_complete_write(sci); | |
2004 | nilfs_destroy_logs(&sci->sc_write_logs); | |
2005 | } | |
2006 | return ret; | |
2007 | } | |
2008 | ||
693dd321 RK |
2009 | static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci, |
2010 | struct the_nilfs *nilfs) | |
9ff05123 RK |
2011 | { |
2012 | struct nilfs_inode_info *ii, *n; | |
e912a5b6 | 2013 | struct inode *ifile = sci->sc_root->ifile; |
9ff05123 | 2014 | |
693dd321 | 2015 | spin_lock(&nilfs->ns_inode_lock); |
9ff05123 | 2016 | retry: |
693dd321 | 2017 | list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) { |
9ff05123 RK |
2018 | if (!ii->i_bh) { |
2019 | struct buffer_head *ibh; | |
2020 | int err; | |
2021 | ||
693dd321 | 2022 | spin_unlock(&nilfs->ns_inode_lock); |
9ff05123 | 2023 | err = nilfs_ifile_get_inode_block( |
e912a5b6 | 2024 | ifile, ii->vfs_inode.i_ino, &ibh); |
9ff05123 | 2025 | if (unlikely(err)) { |
a1d0747a JP |
2026 | nilfs_warn(sci->sc_super, |
2027 | "log writer: error %d getting inode block (ino=%lu)", | |
2028 | err, ii->vfs_inode.i_ino); | |
9ff05123 RK |
2029 | return err; |
2030 | } | |
693dd321 | 2031 | spin_lock(&nilfs->ns_inode_lock); |
9ff05123 RK |
2032 | if (likely(!ii->i_bh)) |
2033 | ii->i_bh = ibh; | |
2034 | else | |
2035 | brelse(ibh); | |
2036 | goto retry; | |
2037 | } | |
9ff05123 | 2038 | |
31ccb1f7 AR |
2039 | // Always redirty the buffer to avoid race condition |
2040 | mark_buffer_dirty(ii->i_bh); | |
2041 | nilfs_mdt_mark_dirty(ifile); | |
2042 | ||
9ff05123 RK |
2043 | clear_bit(NILFS_I_QUEUED, &ii->i_state); |
2044 | set_bit(NILFS_I_BUSY, &ii->i_state); | |
eaae0f37 | 2045 | list_move_tail(&ii->i_dirty, &sci->sc_dirty_files); |
9ff05123 | 2046 | } |
693dd321 | 2047 | spin_unlock(&nilfs->ns_inode_lock); |
9ff05123 | 2048 | |
9ff05123 RK |
2049 | return 0; |
2050 | } | |
2051 | ||
693dd321 RK |
2052 | static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci, |
2053 | struct the_nilfs *nilfs) | |
9ff05123 | 2054 | { |
9ff05123 | 2055 | struct nilfs_inode_info *ii, *n; |
1751e8a6 | 2056 | int during_mount = !(sci->sc_super->s_flags & SB_ACTIVE); |
7ef3ff2f | 2057 | int defer_iput = false; |
9ff05123 | 2058 | |
693dd321 | 2059 | spin_lock(&nilfs->ns_inode_lock); |
9ff05123 RK |
2060 | list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) { |
2061 | if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) || | |
6c43f410 | 2062 | test_bit(NILFS_I_DIRTY, &ii->i_state)) |
9ff05123 | 2063 | continue; |
6c43f410 | 2064 | |
9ff05123 RK |
2065 | clear_bit(NILFS_I_BUSY, &ii->i_state); |
2066 | brelse(ii->i_bh); | |
2067 | ii->i_bh = NULL; | |
7ef3ff2f | 2068 | list_del_init(&ii->i_dirty); |
283ee148 | 2069 | if (!ii->vfs_inode.i_nlink || during_mount) { |
7ef3ff2f | 2070 | /* |
283ee148 RK |
2071 | * Defer calling iput() to avoid deadlocks if |
2072 | * i_nlink == 0 or mount is not yet finished. | |
7ef3ff2f RK |
2073 | */ |
2074 | list_add_tail(&ii->i_dirty, &sci->sc_iput_queue); | |
2075 | defer_iput = true; | |
2076 | } else { | |
2077 | spin_unlock(&nilfs->ns_inode_lock); | |
2078 | iput(&ii->vfs_inode); | |
2079 | spin_lock(&nilfs->ns_inode_lock); | |
2080 | } | |
9ff05123 | 2081 | } |
693dd321 | 2082 | spin_unlock(&nilfs->ns_inode_lock); |
7ef3ff2f RK |
2083 | |
2084 | if (defer_iput) | |
2085 | schedule_work(&sci->sc_iput_work); | |
9ff05123 RK |
2086 | } |
2087 | ||
9ff05123 RK |
2088 | /* |
2089 | * Main procedure of segment constructor | |
2090 | */ | |
2091 | static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode) | |
2092 | { | |
e3154e97 | 2093 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
1e2b68bf | 2094 | int err; |
9ff05123 | 2095 | |
28a65b49 RK |
2096 | if (sb_rdonly(sci->sc_super)) |
2097 | return -EROFS; | |
2098 | ||
58497703 | 2099 | nilfs_sc_cstage_set(sci, NILFS_ST_INIT); |
6c43f410 | 2100 | sci->sc_cno = nilfs->ns_cno; |
9ff05123 | 2101 | |
693dd321 | 2102 | err = nilfs_segctor_collect_dirty_files(sci, nilfs); |
9ff05123 RK |
2103 | if (unlikely(err)) |
2104 | goto out; | |
2105 | ||
e912a5b6 | 2106 | if (nilfs_test_metadata_dirty(nilfs, sci->sc_root)) |
9ff05123 RK |
2107 | set_bit(NILFS_SC_DIRTY, &sci->sc_flags); |
2108 | ||
2109 | if (nilfs_segctor_clean(sci)) | |
2110 | goto out; | |
2111 | ||
2112 | do { | |
2113 | sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK; | |
2114 | ||
2115 | err = nilfs_segctor_begin_construction(sci, nilfs); | |
2116 | if (unlikely(err)) | |
6576dd66 | 2117 | goto failed; |
9ff05123 RK |
2118 | |
2119 | /* Update time stamp */ | |
fb04b91b | 2120 | sci->sc_seg_ctime = ktime_get_real_seconds(); |
9ff05123 RK |
2121 | |
2122 | err = nilfs_segctor_collect(sci, nilfs, mode); | |
2123 | if (unlikely(err)) | |
2124 | goto failed; | |
2125 | ||
9ff05123 | 2126 | /* Avoid empty segment */ |
58497703 | 2127 | if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE && |
4762077c | 2128 | nilfs_segbuf_empty(sci->sc_curseg)) { |
a694291a | 2129 | nilfs_segctor_abort_construction(sci, nilfs, 1); |
9ff05123 RK |
2130 | goto out; |
2131 | } | |
2132 | ||
2133 | err = nilfs_segctor_assign(sci, mode); | |
2134 | if (unlikely(err)) | |
2135 | goto failed; | |
2136 | ||
9ff05123 | 2137 | if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED) |
e912a5b6 | 2138 | nilfs_segctor_fill_in_file_bmap(sci); |
9ff05123 | 2139 | |
1e2b68bf | 2140 | if (mode == SC_LSEG_SR && |
58497703 | 2141 | nilfs_sc_cstage_get(sci) >= NILFS_ST_CPFILE) { |
cce259b4 RK |
2142 | err = nilfs_cpfile_finalize_checkpoint( |
2143 | nilfs->ns_cpfile, nilfs->ns_cno, sci->sc_root, | |
2144 | sci->sc_nblk_inc + sci->sc_nblk_this_inc, | |
2145 | sci->sc_seg_ctime, | |
2146 | !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags)); | |
9ff05123 | 2147 | if (unlikely(err)) |
a694291a | 2148 | goto failed_to_write; |
9ff05123 RK |
2149 | |
2150 | nilfs_segctor_fill_in_super_root(sci, nilfs); | |
2151 | } | |
2152 | nilfs_segctor_update_segusage(sci, nilfs->ns_sufile); | |
2153 | ||
2154 | /* Write partial segments */ | |
25fa5f99 | 2155 | nilfs_prepare_write_logs(&sci->sc_segbufs, nilfs->ns_crc_seed); |
9ff05123 | 2156 | |
9c965bac | 2157 | err = nilfs_segctor_write(sci, nilfs); |
9ff05123 RK |
2158 | if (unlikely(err)) |
2159 | goto failed_to_write; | |
2160 | ||
58497703 | 2161 | if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE || |
09cbfeaf | 2162 | nilfs->ns_blocksize_bits != PAGE_SHIFT) { |
a694291a RK |
2163 | /* |
2164 | * At this point, we avoid double buffering | |
2165 | * for blocksize < pagesize because page dirty | |
2166 | * flag is turned off during write and dirty | |
2167 | * buffers are not properly collected for | |
2168 | * pages crossing over segments. | |
2169 | */ | |
2170 | err = nilfs_segctor_wait(sci); | |
2171 | if (err) | |
2172 | goto failed_to_write; | |
2173 | } | |
58497703 | 2174 | } while (nilfs_sc_cstage_get(sci) != NILFS_ST_DONE); |
9ff05123 | 2175 | |
9ff05123 | 2176 | out: |
693dd321 | 2177 | nilfs_segctor_drop_written_files(sci, nilfs); |
9ff05123 RK |
2178 | return err; |
2179 | ||
2180 | failed_to_write: | |
9ff05123 | 2181 | failed: |
6576dd66 RK |
2182 | if (mode == SC_LSEG_SR && nilfs_sc_cstage_get(sci) >= NILFS_ST_IFILE) |
2183 | nilfs_redirty_inodes(&sci->sc_dirty_files); | |
9ff05123 RK |
2184 | if (nilfs_doing_gc()) |
2185 | nilfs_redirty_inodes(&sci->sc_gc_inodes); | |
a694291a | 2186 | nilfs_segctor_abort_construction(sci, nilfs, err); |
9ff05123 RK |
2187 | goto out; |
2188 | } | |
2189 | ||
2190 | /** | |
9ccf56c1 | 2191 | * nilfs_segctor_start_timer - set timer of background write |
9ff05123 RK |
2192 | * @sci: nilfs_sc_info |
2193 | * | |
2194 | * If the timer has already been set, it ignores the new request. | |
2195 | * This function MUST be called within a section locking the segment | |
2196 | * semaphore. | |
2197 | */ | |
2198 | static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci) | |
2199 | { | |
2200 | spin_lock(&sci->sc_state_lock); | |
fdce895e | 2201 | if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) { |
f5d4e046 RK |
2202 | if (sci->sc_task) { |
2203 | sci->sc_timer.expires = jiffies + sci->sc_interval; | |
2204 | add_timer(&sci->sc_timer); | |
2205 | } | |
9ff05123 RK |
2206 | sci->sc_state |= NILFS_SEGCTOR_COMMIT; |
2207 | } | |
2208 | spin_unlock(&sci->sc_state_lock); | |
2209 | } | |
2210 | ||
2211 | static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn) | |
2212 | { | |
2213 | spin_lock(&sci->sc_state_lock); | |
4ce5c342 | 2214 | if (!(sci->sc_flush_request & BIT(bn))) { |
9ff05123 RK |
2215 | unsigned long prev_req = sci->sc_flush_request; |
2216 | ||
4ce5c342 | 2217 | sci->sc_flush_request |= BIT(bn); |
9ff05123 RK |
2218 | if (!prev_req) |
2219 | wake_up(&sci->sc_wait_daemon); | |
2220 | } | |
2221 | spin_unlock(&sci->sc_state_lock); | |
2222 | } | |
2223 | ||
9ff05123 | 2224 | struct nilfs_segctor_wait_request { |
ac6424b9 | 2225 | wait_queue_entry_t wq; |
9ff05123 RK |
2226 | __u32 seq; |
2227 | int err; | |
2228 | atomic_t done; | |
2229 | }; | |
2230 | ||
2231 | static int nilfs_segctor_sync(struct nilfs_sc_info *sci) | |
2232 | { | |
2233 | struct nilfs_segctor_wait_request wait_req; | |
2234 | int err = 0; | |
2235 | ||
9ff05123 RK |
2236 | init_wait(&wait_req.wq); |
2237 | wait_req.err = 0; | |
2238 | atomic_set(&wait_req.done, 0); | |
936184ea RK |
2239 | init_waitqueue_entry(&wait_req.wq, current); |
2240 | ||
2241 | /* | |
2242 | * To prevent a race issue where completion notifications from the | |
2243 | * log writer thread are missed, increment the request sequence count | |
2244 | * "sc_seq_request" and insert a wait queue entry using the current | |
2245 | * sequence number into the "sc_wait_request" queue at the same time | |
2246 | * within the lock section of "sc_state_lock". | |
2247 | */ | |
2248 | spin_lock(&sci->sc_state_lock); | |
9ff05123 | 2249 | wait_req.seq = ++sci->sc_seq_request; |
936184ea | 2250 | add_wait_queue(&sci->sc_wait_request, &wait_req.wq); |
9ff05123 RK |
2251 | spin_unlock(&sci->sc_state_lock); |
2252 | ||
9ff05123 RK |
2253 | wake_up(&sci->sc_wait_daemon); |
2254 | ||
2255 | for (;;) { | |
936184ea RK |
2256 | set_current_state(TASK_INTERRUPTIBLE); |
2257 | ||
eb85dace RK |
2258 | /* |
2259 | * Synchronize only while the log writer thread is alive. | |
2260 | * Leave flushing out after the log writer thread exits to | |
2261 | * the cleanup work in nilfs_segctor_destroy(). | |
2262 | */ | |
2263 | if (!sci->sc_task) | |
2264 | break; | |
2265 | ||
9ff05123 RK |
2266 | if (atomic_read(&wait_req.done)) { |
2267 | err = wait_req.err; | |
2268 | break; | |
2269 | } | |
2270 | if (!signal_pending(current)) { | |
2271 | schedule(); | |
2272 | continue; | |
2273 | } | |
2274 | err = -ERESTARTSYS; | |
2275 | break; | |
2276 | } | |
2277 | finish_wait(&sci->sc_wait_request, &wait_req.wq); | |
2278 | return err; | |
2279 | } | |
2280 | ||
eb85dace | 2281 | static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err, bool force) |
9ff05123 RK |
2282 | { |
2283 | struct nilfs_segctor_wait_request *wrq, *n; | |
2284 | unsigned long flags; | |
2285 | ||
2286 | spin_lock_irqsave(&sci->sc_wait_request.lock, flags); | |
2055da97 | 2287 | list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.head, wq.entry) { |
9ff05123 | 2288 | if (!atomic_read(&wrq->done) && |
eb85dace | 2289 | (force || nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq))) { |
9ff05123 RK |
2290 | wrq->err = err; |
2291 | atomic_set(&wrq->done, 1); | |
2292 | } | |
2293 | if (atomic_read(&wrq->done)) { | |
2294 | wrq->wq.func(&wrq->wq, | |
2295 | TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, | |
2296 | 0, NULL); | |
2297 | } | |
2298 | } | |
2299 | spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags); | |
2300 | } | |
2301 | ||
2302 | /** | |
2303 | * nilfs_construct_segment - construct a logical segment | |
2304 | * @sb: super block | |
2305 | * | |
343d4a33 RK |
2306 | * Return: 0 on success, or one of the following negative error codes on |
2307 | * failure: | |
2308 | * * %-EIO - I/O error (including metadata corruption). | |
2309 | * * %-ENOMEM - Insufficient memory available. | |
2310 | * * %-ENOSPC - No space left on device (only in a panic state). | |
2311 | * * %-ERESTARTSYS - Interrupted. | |
2312 | * * %-EROFS - Read only filesystem. | |
9ff05123 RK |
2313 | */ |
2314 | int nilfs_construct_segment(struct super_block *sb) | |
2315 | { | |
e3154e97 | 2316 | struct the_nilfs *nilfs = sb->s_fs_info; |
3fd3fe5a | 2317 | struct nilfs_sc_info *sci = nilfs->ns_writer; |
9ff05123 | 2318 | struct nilfs_transaction_info *ti; |
9ff05123 | 2319 | |
8cccf05f | 2320 | if (sb_rdonly(sb) || unlikely(!sci)) |
9ff05123 RK |
2321 | return -EROFS; |
2322 | ||
2323 | /* A call inside transactions causes a deadlock. */ | |
2324 | BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC); | |
2325 | ||
da6f7916 | 2326 | return nilfs_segctor_sync(sci); |
9ff05123 RK |
2327 | } |
2328 | ||
2329 | /** | |
2330 | * nilfs_construct_dsync_segment - construct a data-only logical segment | |
2331 | * @sb: super block | |
f30bf3e4 RK |
2332 | * @inode: inode whose data blocks should be written out |
2333 | * @start: start byte offset | |
2334 | * @end: end byte offset (inclusive) | |
9ff05123 | 2335 | * |
343d4a33 RK |
2336 | * Return: 0 on success, or one of the following negative error codes on |
2337 | * failure: | |
2338 | * * %-EIO - I/O error (including metadata corruption). | |
2339 | * * %-ENOMEM - Insufficient memory available. | |
2340 | * * %-ENOSPC - No space left on device (only in a panic state). | |
2341 | * * %-ERESTARTSYS - Interrupted. | |
2342 | * * %-EROFS - Read only filesystem. | |
9ff05123 | 2343 | */ |
f30bf3e4 RK |
2344 | int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode, |
2345 | loff_t start, loff_t end) | |
9ff05123 | 2346 | { |
e3154e97 | 2347 | struct the_nilfs *nilfs = sb->s_fs_info; |
3fd3fe5a | 2348 | struct nilfs_sc_info *sci = nilfs->ns_writer; |
9ff05123 RK |
2349 | struct nilfs_inode_info *ii; |
2350 | struct nilfs_transaction_info ti; | |
2351 | int err = 0; | |
2352 | ||
8cccf05f | 2353 | if (sb_rdonly(sb) || unlikely(!sci)) |
9ff05123 RK |
2354 | return -EROFS; |
2355 | ||
f7545144 | 2356 | nilfs_transaction_lock(sb, &ti, 0); |
9ff05123 RK |
2357 | |
2358 | ii = NILFS_I(inode); | |
b9f66140 | 2359 | if (test_bit(NILFS_I_INODE_SYNC, &ii->i_state) || |
3b2ce58b | 2360 | nilfs_test_opt(nilfs, STRICT_ORDER) || |
9ff05123 | 2361 | test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) || |
3b2ce58b | 2362 | nilfs_discontinued(nilfs)) { |
f7545144 | 2363 | nilfs_transaction_unlock(sb); |
9ff05123 RK |
2364 | err = nilfs_segctor_sync(sci); |
2365 | return err; | |
2366 | } | |
2367 | ||
693dd321 | 2368 | spin_lock(&nilfs->ns_inode_lock); |
9ff05123 RK |
2369 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && |
2370 | !test_bit(NILFS_I_BUSY, &ii->i_state)) { | |
693dd321 | 2371 | spin_unlock(&nilfs->ns_inode_lock); |
f7545144 | 2372 | nilfs_transaction_unlock(sb); |
9ff05123 RK |
2373 | return 0; |
2374 | } | |
693dd321 | 2375 | spin_unlock(&nilfs->ns_inode_lock); |
f30bf3e4 RK |
2376 | sci->sc_dsync_inode = ii; |
2377 | sci->sc_dsync_start = start; | |
2378 | sci->sc_dsync_end = end; | |
9ff05123 RK |
2379 | |
2380 | err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC); | |
e2c7617a AR |
2381 | if (!err) |
2382 | nilfs->ns_flushed_device = 0; | |
9ff05123 | 2383 | |
f7545144 | 2384 | nilfs_transaction_unlock(sb); |
9ff05123 RK |
2385 | return err; |
2386 | } | |
2387 | ||
9ff05123 | 2388 | #define FLUSH_FILE_BIT (0x1) /* data file only */ |
4ce5c342 | 2389 | #define FLUSH_DAT_BIT BIT(NILFS_DAT_INO) /* DAT only */ |
9ff05123 | 2390 | |
dcd76186 RK |
2391 | /** |
2392 | * nilfs_segctor_accept - record accepted sequence count of log-write requests | |
2393 | * @sci: segment constructor object | |
2394 | */ | |
2395 | static void nilfs_segctor_accept(struct nilfs_sc_info *sci) | |
9ff05123 | 2396 | { |
f5d4e046 RK |
2397 | bool thread_is_alive; |
2398 | ||
9ff05123 | 2399 | spin_lock(&sci->sc_state_lock); |
dcd76186 | 2400 | sci->sc_seq_accepted = sci->sc_seq_request; |
f5d4e046 | 2401 | thread_is_alive = (bool)sci->sc_task; |
9ff05123 | 2402 | spin_unlock(&sci->sc_state_lock); |
f5d4e046 RK |
2403 | |
2404 | /* | |
2405 | * This function does not race with the log writer thread's | |
2406 | * termination. Therefore, deleting sc_timer, which should not be | |
2407 | * done after the log writer thread exits, can be done safely outside | |
2408 | * the area protected by sc_state_lock. | |
2409 | */ | |
2410 | if (thread_is_alive) | |
8fa7292f | 2411 | timer_delete_sync(&sci->sc_timer); |
9ff05123 RK |
2412 | } |
2413 | ||
dcd76186 RK |
2414 | /** |
2415 | * nilfs_segctor_notify - notify the result of request to caller threads | |
2416 | * @sci: segment constructor object | |
2417 | * @mode: mode of log forming | |
2418 | * @err: error code to be notified | |
2419 | */ | |
2420 | static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err) | |
9ff05123 RK |
2421 | { |
2422 | /* Clear requests (even when the construction failed) */ | |
2423 | spin_lock(&sci->sc_state_lock); | |
2424 | ||
dcd76186 | 2425 | if (mode == SC_LSEG_SR) { |
aeda7f63 | 2426 | sci->sc_state &= ~NILFS_SEGCTOR_COMMIT; |
dcd76186 | 2427 | sci->sc_seq_done = sci->sc_seq_accepted; |
eb85dace | 2428 | nilfs_segctor_wakeup(sci, err, false); |
9ff05123 | 2429 | sci->sc_flush_request = 0; |
aeda7f63 | 2430 | } else { |
dcd76186 | 2431 | if (mode == SC_FLUSH_FILE) |
aeda7f63 | 2432 | sci->sc_flush_request &= ~FLUSH_FILE_BIT; |
dcd76186 | 2433 | else if (mode == SC_FLUSH_DAT) |
aeda7f63 RK |
2434 | sci->sc_flush_request &= ~FLUSH_DAT_BIT; |
2435 | ||
2436 | /* re-enable timer if checkpoint creation was not done */ | |
f5d4e046 | 2437 | if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && sci->sc_task && |
fdce895e LH |
2438 | time_before(jiffies, sci->sc_timer.expires)) |
2439 | add_timer(&sci->sc_timer); | |
aeda7f63 | 2440 | } |
9ff05123 RK |
2441 | spin_unlock(&sci->sc_state_lock); |
2442 | } | |
2443 | ||
dcd76186 RK |
2444 | /** |
2445 | * nilfs_segctor_construct - form logs and write them to disk | |
2446 | * @sci: segment constructor object | |
2447 | * @mode: mode of log forming | |
fd4e7fad RK |
2448 | * |
2449 | * Return: 0 on success, or a negative error code on failure. | |
dcd76186 RK |
2450 | */ |
2451 | static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode) | |
9ff05123 | 2452 | { |
e3154e97 | 2453 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
d26493b6 | 2454 | struct nilfs_super_block **sbp; |
9ff05123 RK |
2455 | int err = 0; |
2456 | ||
dcd76186 RK |
2457 | nilfs_segctor_accept(sci); |
2458 | ||
9ff05123 | 2459 | if (nilfs_discontinued(nilfs)) |
dcd76186 RK |
2460 | mode = SC_LSEG_SR; |
2461 | if (!nilfs_segctor_confirm(sci)) | |
2462 | err = nilfs_segctor_do_construct(sci, mode); | |
2463 | ||
9ff05123 | 2464 | if (likely(!err)) { |
dcd76186 | 2465 | if (mode != SC_FLUSH_DAT) |
9ff05123 RK |
2466 | atomic_set(&nilfs->ns_ndirtyblks, 0); |
2467 | if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) && | |
2468 | nilfs_discontinued(nilfs)) { | |
2469 | down_write(&nilfs->ns_sem); | |
d26493b6 | 2470 | err = -EIO; |
f7545144 | 2471 | sbp = nilfs_prepare_super(sci->sc_super, |
b2ac86e1 JS |
2472 | nilfs_sb_will_flip(nilfs)); |
2473 | if (likely(sbp)) { | |
2474 | nilfs_set_log_cursor(sbp[0], nilfs); | |
f7545144 RK |
2475 | err = nilfs_commit_super(sci->sc_super, |
2476 | NILFS_SB_COMMIT); | |
b2ac86e1 | 2477 | } |
9ff05123 RK |
2478 | up_write(&nilfs->ns_sem); |
2479 | } | |
2480 | } | |
dcd76186 RK |
2481 | |
2482 | nilfs_segctor_notify(sci, mode, err); | |
9ff05123 RK |
2483 | return err; |
2484 | } | |
2485 | ||
7554e9c4 | 2486 | static void nilfs_construction_timeout(struct timer_list *t) |
9ff05123 | 2487 | { |
41cb0855 | 2488 | struct nilfs_sc_info *sci = timer_container_of(sci, t, sc_timer); |
4ad364ca | 2489 | |
cfdfe9e1 | 2490 | wake_up_process(sci->sc_task); |
9ff05123 RK |
2491 | } |
2492 | ||
2493 | static void | |
2494 | nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head) | |
2495 | { | |
2496 | struct nilfs_inode_info *ii, *n; | |
2497 | ||
2498 | list_for_each_entry_safe(ii, n, head, i_dirty) { | |
2499 | if (!test_bit(NILFS_I_UPDATED, &ii->i_state)) | |
2500 | continue; | |
9ff05123 | 2501 | list_del_init(&ii->i_dirty); |
fbb24a3a | 2502 | truncate_inode_pages(&ii->vfs_inode.i_data, 0); |
e897be17 | 2503 | nilfs_btnode_cache_clear(ii->i_assoc_inode->i_mapping); |
263d90ce | 2504 | iput(&ii->vfs_inode); |
9ff05123 RK |
2505 | } |
2506 | } | |
2507 | ||
4f6b8288 RK |
2508 | int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv, |
2509 | void **kbufs) | |
9ff05123 | 2510 | { |
e3154e97 | 2511 | struct the_nilfs *nilfs = sb->s_fs_info; |
3fd3fe5a | 2512 | struct nilfs_sc_info *sci = nilfs->ns_writer; |
9ff05123 | 2513 | struct nilfs_transaction_info ti; |
9ff05123 RK |
2514 | int err; |
2515 | ||
2516 | if (unlikely(!sci)) | |
2517 | return -EROFS; | |
2518 | ||
f7545144 | 2519 | nilfs_transaction_lock(sb, &ti, 1); |
9ff05123 | 2520 | |
c1c1d709 | 2521 | err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat); |
9ff05123 RK |
2522 | if (unlikely(err)) |
2523 | goto out_unlock; | |
071cb4b8 | 2524 | |
4f6b8288 | 2525 | err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs); |
c1c1d709 RK |
2526 | if (unlikely(err)) { |
2527 | nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat); | |
9ff05123 | 2528 | goto out_unlock; |
c1c1d709 | 2529 | } |
9ff05123 | 2530 | |
071cb4b8 RK |
2531 | sci->sc_freesegs = kbufs[4]; |
2532 | sci->sc_nfreesegs = argv[4].v_nmembs; | |
0935db74 | 2533 | list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes); |
9ff05123 RK |
2534 | |
2535 | for (;;) { | |
dcd76186 | 2536 | err = nilfs_segctor_construct(sci, SC_LSEG_SR); |
9ff05123 | 2537 | nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes); |
9ff05123 RK |
2538 | |
2539 | if (likely(!err)) | |
2540 | break; | |
2541 | ||
a1d0747a | 2542 | nilfs_warn(sb, "error %d cleaning segments", err); |
9ff05123 RK |
2543 | set_current_state(TASK_INTERRUPTIBLE); |
2544 | schedule_timeout(sci->sc_interval); | |
2545 | } | |
3b2ce58b | 2546 | if (nilfs_test_opt(nilfs, DISCARD)) { |
e902ec99 JS |
2547 | int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs, |
2548 | sci->sc_nfreesegs); | |
2549 | if (ret) { | |
a1d0747a JP |
2550 | nilfs_warn(sb, |
2551 | "error %d on discard request, turning discards off for the device", | |
2552 | ret); | |
3b2ce58b | 2553 | nilfs_clear_opt(nilfs, DISCARD); |
e902ec99 JS |
2554 | } |
2555 | } | |
9ff05123 RK |
2556 | |
2557 | out_unlock: | |
071cb4b8 RK |
2558 | sci->sc_freesegs = NULL; |
2559 | sci->sc_nfreesegs = 0; | |
c1c1d709 | 2560 | nilfs_mdt_clear_shadow_map(nilfs->ns_dat); |
f7545144 | 2561 | nilfs_transaction_unlock(sb); |
9ff05123 RK |
2562 | return err; |
2563 | } | |
2564 | ||
2565 | static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode) | |
2566 | { | |
9ff05123 | 2567 | struct nilfs_transaction_info ti; |
9ff05123 | 2568 | |
f7545144 | 2569 | nilfs_transaction_lock(sci->sc_super, &ti, 0); |
dcd76186 | 2570 | nilfs_segctor_construct(sci, mode); |
9ff05123 RK |
2571 | |
2572 | /* | |
2573 | * Unclosed segment should be retried. We do this using sc_timer. | |
2574 | * Timeout of sc_timer will invoke complete construction which leads | |
2575 | * to close the current logical segment. | |
2576 | */ | |
2577 | if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) | |
2578 | nilfs_segctor_start_timer(sci); | |
2579 | ||
f7545144 | 2580 | nilfs_transaction_unlock(sci->sc_super); |
9ff05123 RK |
2581 | } |
2582 | ||
2583 | static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci) | |
2584 | { | |
2585 | int mode = 0; | |
9ff05123 RK |
2586 | |
2587 | spin_lock(&sci->sc_state_lock); | |
2588 | mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ? | |
2589 | SC_FLUSH_DAT : SC_FLUSH_FILE; | |
2590 | spin_unlock(&sci->sc_state_lock); | |
2591 | ||
2592 | if (mode) { | |
09ef29e0 | 2593 | nilfs_segctor_do_construct(sci, mode); |
9ff05123 RK |
2594 | |
2595 | spin_lock(&sci->sc_state_lock); | |
2596 | sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ? | |
2597 | ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT; | |
2598 | spin_unlock(&sci->sc_state_lock); | |
2599 | } | |
2600 | clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags); | |
2601 | } | |
2602 | ||
2603 | static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci) | |
2604 | { | |
2605 | if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) || | |
2606 | time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) { | |
2607 | if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT)) | |
2608 | return SC_FLUSH_FILE; | |
2609 | else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT)) | |
2610 | return SC_FLUSH_DAT; | |
2611 | } | |
2612 | return SC_LSEG_SR; | |
2613 | } | |
2614 | ||
2615 | /** | |
74b00993 RK |
2616 | * nilfs_log_write_required - determine whether log writing is required |
2617 | * @sci: nilfs_sc_info struct | |
2618 | * @modep: location for storing log writing mode | |
2619 | * | |
2620 | * Return: true if log writing is required, false otherwise. If log writing | |
2621 | * is required, the mode is stored in the location pointed to by @modep. | |
2622 | */ | |
2623 | static bool nilfs_log_write_required(struct nilfs_sc_info *sci, int *modep) | |
2624 | { | |
2625 | bool timedout, ret = true; | |
2626 | ||
2627 | spin_lock(&sci->sc_state_lock); | |
2628 | timedout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && | |
2629 | time_after_eq(jiffies, sci->sc_timer.expires)); | |
2630 | if (timedout || sci->sc_seq_request != sci->sc_seq_done) | |
2631 | *modep = SC_LSEG_SR; | |
2632 | else if (sci->sc_flush_request) | |
2633 | *modep = nilfs_segctor_flush_mode(sci); | |
2634 | else | |
2635 | ret = false; | |
2636 | ||
2637 | spin_unlock(&sci->sc_state_lock); | |
2638 | return ret; | |
2639 | } | |
2640 | ||
9ff05123 | 2641 | /** |
3f66cc26 | 2642 | * nilfs_segctor_thread - main loop of the log writer thread |
9ff05123 RK |
2643 | * @arg: pointer to a struct nilfs_sc_info. |
2644 | * | |
3f66cc26 RK |
2645 | * nilfs_segctor_thread() is the main loop function of the log writer kernel |
2646 | * thread, which determines whether log writing is necessary, and if so, | |
2647 | * performs the log write in the background, or waits if not. It is also | |
2648 | * used to decide the background writeback of the superblock. | |
2649 | * | |
2650 | * Return: Always 0. | |
9ff05123 RK |
2651 | */ |
2652 | static int nilfs_segctor_thread(void *arg) | |
2653 | { | |
2654 | struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg; | |
e3154e97 | 2655 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
9ff05123 | 2656 | |
a1d0747a JP |
2657 | nilfs_info(sci->sc_super, |
2658 | "segctord starting. Construction interval = %lu seconds, CP frequency < %lu seconds", | |
2659 | sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ); | |
9ff05123 | 2660 | |
5b130948 | 2661 | set_freezable(); |
9ff05123 | 2662 | |
74b00993 | 2663 | while (!kthread_should_stop()) { |
9ff05123 | 2664 | DEFINE_WAIT(wait); |
74b00993 RK |
2665 | bool should_write; |
2666 | int mode; | |
2667 | ||
2668 | if (freezing(current)) { | |
2669 | try_to_freeze(); | |
2670 | continue; | |
2671 | } | |
9ff05123 RK |
2672 | |
2673 | prepare_to_wait(&sci->sc_wait_daemon, &wait, | |
2674 | TASK_INTERRUPTIBLE); | |
74b00993 RK |
2675 | should_write = nilfs_log_write_required(sci, &mode); |
2676 | if (!should_write) | |
9ff05123 | 2677 | schedule(); |
9ff05123 | 2678 | finish_wait(&sci->sc_wait_daemon, &wait); |
e605f0a7 RK |
2679 | |
2680 | if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs)) | |
1dfa2710 | 2681 | set_nilfs_discontinued(nilfs); |
74b00993 RK |
2682 | |
2683 | if (should_write) | |
2684 | nilfs_segctor_thread_construct(sci, mode); | |
9ff05123 | 2685 | } |
9ff05123 | 2686 | |
9ff05123 | 2687 | /* end sync. */ |
74b00993 | 2688 | spin_lock(&sci->sc_state_lock); |
9ff05123 | 2689 | sci->sc_task = NULL; |
f5d4e046 | 2690 | timer_shutdown_sync(&sci->sc_timer); |
6be49d10 | 2691 | spin_unlock(&sci->sc_state_lock); |
9ff05123 RK |
2692 | return 0; |
2693 | } | |
2694 | ||
9ff05123 RK |
2695 | /* |
2696 | * Setup & clean-up functions | |
2697 | */ | |
f7545144 | 2698 | static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb, |
e912a5b6 | 2699 | struct nilfs_root *root) |
9ff05123 | 2700 | { |
e3154e97 | 2701 | struct the_nilfs *nilfs = sb->s_fs_info; |
9ff05123 RK |
2702 | struct nilfs_sc_info *sci; |
2703 | ||
2704 | sci = kzalloc(sizeof(*sci), GFP_KERNEL); | |
2705 | if (!sci) | |
2706 | return NULL; | |
2707 | ||
f7545144 | 2708 | sci->sc_super = sb; |
9ff05123 | 2709 | |
e912a5b6 RK |
2710 | nilfs_get_root(root); |
2711 | sci->sc_root = root; | |
2712 | ||
9ff05123 RK |
2713 | init_waitqueue_head(&sci->sc_wait_request); |
2714 | init_waitqueue_head(&sci->sc_wait_daemon); | |
9ff05123 RK |
2715 | spin_lock_init(&sci->sc_state_lock); |
2716 | INIT_LIST_HEAD(&sci->sc_dirty_files); | |
2717 | INIT_LIST_HEAD(&sci->sc_segbufs); | |
a694291a | 2718 | INIT_LIST_HEAD(&sci->sc_write_logs); |
9ff05123 | 2719 | INIT_LIST_HEAD(&sci->sc_gc_inodes); |
7ef3ff2f RK |
2720 | INIT_LIST_HEAD(&sci->sc_iput_queue); |
2721 | INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func); | |
9ff05123 RK |
2722 | |
2723 | sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT; | |
2724 | sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ; | |
2725 | sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK; | |
2726 | ||
574e6c31 | 2727 | if (nilfs->ns_interval) |
071d73cf | 2728 | sci->sc_interval = HZ * nilfs->ns_interval; |
574e6c31 RK |
2729 | if (nilfs->ns_watermark) |
2730 | sci->sc_watermark = nilfs->ns_watermark; | |
9ff05123 RK |
2731 | return sci; |
2732 | } | |
2733 | ||
2734 | static void nilfs_segctor_write_out(struct nilfs_sc_info *sci) | |
2735 | { | |
2736 | int ret, retrycount = NILFS_SC_CLEANUP_RETRY; | |
2737 | ||
076a378b RK |
2738 | /* |
2739 | * The segctord thread was stopped and its timer was removed. | |
2740 | * But some tasks remain. | |
2741 | */ | |
9ff05123 | 2742 | do { |
9ff05123 | 2743 | struct nilfs_transaction_info ti; |
9ff05123 | 2744 | |
f7545144 | 2745 | nilfs_transaction_lock(sci->sc_super, &ti, 0); |
dcd76186 | 2746 | ret = nilfs_segctor_construct(sci, SC_LSEG_SR); |
f7545144 | 2747 | nilfs_transaction_unlock(sci->sc_super); |
9ff05123 | 2748 | |
7ef3ff2f RK |
2749 | flush_work(&sci->sc_iput_work); |
2750 | ||
28a65b49 | 2751 | } while (ret && ret != -EROFS && retrycount-- > 0); |
9ff05123 RK |
2752 | } |
2753 | ||
2754 | /** | |
2755 | * nilfs_segctor_destroy - destroy the segment constructor. | |
2756 | * @sci: nilfs_sc_info | |
2757 | * | |
2758 | * nilfs_segctor_destroy() kills the segctord thread and frees | |
2759 | * the nilfs_sc_info struct. | |
2760 | * Caller must hold the segment semaphore. | |
2761 | */ | |
2762 | static void nilfs_segctor_destroy(struct nilfs_sc_info *sci) | |
2763 | { | |
e3154e97 | 2764 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; |
9ff05123 RK |
2765 | int flag; |
2766 | ||
693dd321 | 2767 | up_write(&nilfs->ns_segctor_sem); |
9ff05123 | 2768 | |
3f66cc26 RK |
2769 | if (sci->sc_task) { |
2770 | wake_up(&sci->sc_wait_daemon); | |
2771 | kthread_stop(sci->sc_task); | |
2772 | } | |
2773 | ||
9ff05123 | 2774 | spin_lock(&sci->sc_state_lock); |
9ff05123 RK |
2775 | flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request |
2776 | || sci->sc_seq_request != sci->sc_seq_done); | |
2777 | spin_unlock(&sci->sc_state_lock); | |
2778 | ||
eb85dace RK |
2779 | /* |
2780 | * Forcibly wake up tasks waiting in nilfs_segctor_sync(), which can | |
2781 | * be called from delayed iput() via nilfs_evict_inode() and can race | |
2782 | * with the above log writer thread termination. | |
2783 | */ | |
2784 | nilfs_segctor_wakeup(sci, 0, true); | |
2785 | ||
7ef3ff2f RK |
2786 | if (flush_work(&sci->sc_iput_work)) |
2787 | flag = true; | |
2788 | ||
3256a055 | 2789 | if (flag || !nilfs_segctor_confirm(sci)) |
9ff05123 RK |
2790 | nilfs_segctor_write_out(sci); |
2791 | ||
9ff05123 | 2792 | if (!list_empty(&sci->sc_dirty_files)) { |
a1d0747a JP |
2793 | nilfs_warn(sci->sc_super, |
2794 | "disposed unprocessed dirty file(s) when stopping log writer"); | |
693dd321 | 2795 | nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1); |
9ff05123 | 2796 | } |
9ff05123 | 2797 | |
7ef3ff2f | 2798 | if (!list_empty(&sci->sc_iput_queue)) { |
a1d0747a JP |
2799 | nilfs_warn(sci->sc_super, |
2800 | "disposed unprocessed inode(s) in iput queue when stopping log writer"); | |
7ef3ff2f RK |
2801 | nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1); |
2802 | } | |
2803 | ||
1f5abe7e | 2804 | WARN_ON(!list_empty(&sci->sc_segbufs)); |
a694291a | 2805 | WARN_ON(!list_empty(&sci->sc_write_logs)); |
9ff05123 | 2806 | |
e912a5b6 RK |
2807 | nilfs_put_root(sci->sc_root); |
2808 | ||
693dd321 | 2809 | down_write(&nilfs->ns_segctor_sem); |
9ff05123 RK |
2810 | |
2811 | kfree(sci); | |
2812 | } | |
2813 | ||
2814 | /** | |
f7545144 RK |
2815 | * nilfs_attach_log_writer - attach log writer |
2816 | * @sb: super block instance | |
e912a5b6 | 2817 | * @root: root object of the current filesystem tree |
9ff05123 | 2818 | * |
f7545144 RK |
2819 | * This allocates a log writer object, initializes it, and starts the |
2820 | * log writer. | |
9ff05123 | 2821 | * |
2e62857f RK |
2822 | * Return: 0 on success, or one of the following negative error codes on |
2823 | * failure: | |
3f66cc26 RK |
2824 | * * %-EINTR - Log writer thread creation failed due to interruption. |
2825 | * * %-ENOMEM - Insufficient memory available. | |
9ff05123 | 2826 | */ |
f7545144 | 2827 | int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root) |
9ff05123 | 2828 | { |
e3154e97 | 2829 | struct the_nilfs *nilfs = sb->s_fs_info; |
3f66cc26 RK |
2830 | struct nilfs_sc_info *sci; |
2831 | struct task_struct *t; | |
9ff05123 RK |
2832 | int err; |
2833 | ||
3fd3fe5a | 2834 | if (nilfs->ns_writer) { |
fe5f171b | 2835 | /* |
8cccf05f RK |
2836 | * This happens if the filesystem is made read-only by |
2837 | * __nilfs_error or nilfs_remount and then remounted | |
2838 | * read/write. In these cases, reuse the existing | |
2839 | * writer. | |
fe5f171b | 2840 | */ |
8cccf05f | 2841 | return 0; |
fe5f171b RK |
2842 | } |
2843 | ||
3f66cc26 RK |
2844 | sci = nilfs_segctor_new(sb, root); |
2845 | if (unlikely(!sci)) | |
9ff05123 RK |
2846 | return -ENOMEM; |
2847 | ||
3f66cc26 RK |
2848 | nilfs->ns_writer = sci; |
2849 | t = kthread_create(nilfs_segctor_thread, sci, "segctord"); | |
2850 | if (IS_ERR(t)) { | |
2851 | err = PTR_ERR(t); | |
2852 | nilfs_err(sb, "error %d creating segctord thread", err); | |
d0d51a97 | 2853 | nilfs_detach_log_writer(sb); |
3f66cc26 RK |
2854 | return err; |
2855 | } | |
2856 | sci->sc_task = t; | |
2857 | timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0); | |
d0d51a97 | 2858 | |
3f66cc26 RK |
2859 | wake_up_process(sci->sc_task); |
2860 | return 0; | |
9ff05123 RK |
2861 | } |
2862 | ||
2863 | /** | |
f7545144 RK |
2864 | * nilfs_detach_log_writer - destroy log writer |
2865 | * @sb: super block instance | |
9ff05123 | 2866 | * |
f7545144 RK |
2867 | * This kills log writer daemon, frees the log writer object, and |
2868 | * destroys list of dirty files. | |
9ff05123 | 2869 | */ |
f7545144 | 2870 | void nilfs_detach_log_writer(struct super_block *sb) |
9ff05123 | 2871 | { |
e3154e97 | 2872 | struct the_nilfs *nilfs = sb->s_fs_info; |
9ff05123 RK |
2873 | LIST_HEAD(garbage_list); |
2874 | ||
2875 | down_write(&nilfs->ns_segctor_sem); | |
3fd3fe5a RK |
2876 | if (nilfs->ns_writer) { |
2877 | nilfs_segctor_destroy(nilfs->ns_writer); | |
2878 | nilfs->ns_writer = NULL; | |
9ff05123 | 2879 | } |
f8654743 | 2880 | set_nilfs_purging(nilfs); |
9ff05123 RK |
2881 | |
2882 | /* Force to free the list of dirty files */ | |
693dd321 RK |
2883 | spin_lock(&nilfs->ns_inode_lock); |
2884 | if (!list_empty(&nilfs->ns_dirty_files)) { | |
2885 | list_splice_init(&nilfs->ns_dirty_files, &garbage_list); | |
a1d0747a JP |
2886 | nilfs_warn(sb, |
2887 | "disposed unprocessed dirty file(s) when detaching log writer"); | |
9ff05123 | 2888 | } |
693dd321 | 2889 | spin_unlock(&nilfs->ns_inode_lock); |
9ff05123 RK |
2890 | up_write(&nilfs->ns_segctor_sem); |
2891 | ||
693dd321 | 2892 | nilfs_dispose_list(nilfs, &garbage_list, 1); |
f8654743 | 2893 | clear_nilfs_purging(nilfs); |
9ff05123 | 2894 | } |