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