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