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