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