blob: 9cf71389f369d0cb125cab8ce890a8d71b0a732c [file] [log] [blame]
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070036#include "nilfs.h"
37#include "btnode.h"
38#include "page.h"
39#include "segment.h"
40#include "sufile.h"
41#include "cpfile.h"
42#include "ifile.h"
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070043#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 */
55enum {
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 */
66enum {
67 NILFS_ST_INIT = 0,
68 NILFS_ST_GC, /* Collecting dirty blocks for GC */
69 NILFS_ST_FILE,
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070070 NILFS_ST_IFILE,
71 NILFS_ST_CPFILE,
72 NILFS_ST_SUFILE,
73 NILFS_ST_DAT,
74 NILFS_ST_SR, /* Super root */
75 NILFS_ST_DSYNC, /* Data sync blocks */
76 NILFS_ST_DONE,
77};
78
79/* State flags of collection */
80#define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
81#define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +090082#define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
83#define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -070084
85/* Operations depending on the construction mode and file type */
86struct 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 */
104static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
105static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
106static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
107static 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
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700119static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
120{
121 struct nilfs_transaction_info *cur_ti = current->journal_info;
122 void *save = NULL;
123
124 if (cur_ti) {
125 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
126 return ++cur_ti->ti_count;
127 else {
128 /*
129 * If journal_info field is occupied by other FS,
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700130 * it is saved and will be restored on
131 * nilfs_transaction_commit().
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700132 */
133 printk(KERN_WARNING
134 "NILFS warning: journal info from a different "
135 "FS\n");
136 save = current->journal_info;
137 }
138 }
139 if (!ti) {
140 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
141 if (!ti)
142 return -ENOMEM;
143 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
144 } else {
145 ti->ti_flags = 0;
146 }
147 ti->ti_count = 0;
148 ti->ti_save = save;
149 ti->ti_magic = NILFS_TI_MAGIC;
150 current->journal_info = ti;
151 return 0;
152}
153
154/**
155 * nilfs_transaction_begin - start indivisible file operations.
156 * @sb: super block
157 * @ti: nilfs_transaction_info
158 * @vacancy_check: flags for vacancy rate checks
159 *
160 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
161 * the segment semaphore, to make a segment construction and write tasks
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700162 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700163 * The region enclosed by these two functions can be nested. To avoid a
164 * deadlock, the semaphore is only acquired or released in the outermost call.
165 *
166 * This function allocates a nilfs_transaction_info struct to keep context
167 * information on it. It is initialized and hooked onto the current task in
168 * the outermost call. If a pre-allocated struct is given to @ti, it is used
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900169 * instead; otherwise a new struct is assigned from a slab.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700170 *
171 * When @vacancy_check flag is set, this function will check the amount of
172 * free space, and will wait for the GC to reclaim disk space if low capacity.
173 *
174 * Return Value: On success, 0 is returned. On error, one of the following
175 * negative error code is returned.
176 *
177 * %-ENOMEM - Insufficient memory available.
178 *
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700179 * %-ENOSPC - No space left on device
180 */
181int nilfs_transaction_begin(struct super_block *sb,
182 struct nilfs_transaction_info *ti,
183 int vacancy_check)
184{
185 struct nilfs_sb_info *sbi;
186 struct the_nilfs *nilfs;
187 int ret = nilfs_prepare_segment_lock(ti);
188
189 if (unlikely(ret < 0))
190 return ret;
191 if (ret > 0)
192 return 0;
193
194 sbi = NILFS_SB(sb);
195 nilfs = sbi->s_nilfs;
196 down_read(&nilfs->ns_segctor_sem);
197 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
198 up_read(&nilfs->ns_segctor_sem);
199 ret = -ENOSPC;
200 goto failed;
201 }
202 return 0;
203
204 failed:
205 ti = current->journal_info;
206 current->journal_info = ti->ti_save;
207 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
208 kmem_cache_free(nilfs_transaction_cachep, ti);
209 return ret;
210}
211
212/**
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700213 * nilfs_transaction_commit - commit indivisible file operations.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700214 * @sb: super block
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700215 *
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700216 * nilfs_transaction_commit() releases the read semaphore which is
217 * acquired by nilfs_transaction_begin(). This is only performed
218 * in outermost call of this function. If a commit flag is set,
219 * nilfs_transaction_commit() sets a timer to start the segment
220 * constructor. If a sync flag is set, it starts construction
221 * directly.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700222 */
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700223int nilfs_transaction_commit(struct super_block *sb)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700224{
225 struct nilfs_transaction_info *ti = current->journal_info;
226 struct nilfs_sb_info *sbi;
227 struct nilfs_sc_info *sci;
228 int err = 0;
229
230 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700231 ti->ti_flags |= NILFS_TI_COMMIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700232 if (ti->ti_count > 0) {
233 ti->ti_count--;
234 return 0;
235 }
236 sbi = NILFS_SB(sb);
237 sci = NILFS_SC(sbi);
238 if (sci != NULL) {
239 if (ti->ti_flags & NILFS_TI_COMMIT)
240 nilfs_segctor_start_timer(sci);
241 if (atomic_read(&sbi->s_nilfs->ns_ndirtyblks) >
242 sci->sc_watermark)
243 nilfs_segctor_do_flush(sci, 0);
244 }
245 up_read(&sbi->s_nilfs->ns_segctor_sem);
246 current->journal_info = ti->ti_save;
247
248 if (ti->ti_flags & NILFS_TI_SYNC)
249 err = nilfs_construct_segment(sb);
250 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
251 kmem_cache_free(nilfs_transaction_cachep, ti);
252 return err;
253}
254
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700255void nilfs_transaction_abort(struct super_block *sb)
256{
257 struct nilfs_transaction_info *ti = current->journal_info;
258
259 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
260 if (ti->ti_count > 0) {
261 ti->ti_count--;
262 return;
263 }
264 up_read(&NILFS_SB(sb)->s_nilfs->ns_segctor_sem);
265
266 current->journal_info = ti->ti_save;
267 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
268 kmem_cache_free(nilfs_transaction_cachep, ti);
269}
270
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700271void nilfs_relax_pressure_in_lock(struct super_block *sb)
272{
273 struct nilfs_sb_info *sbi = NILFS_SB(sb);
274 struct nilfs_sc_info *sci = NILFS_SC(sbi);
275 struct the_nilfs *nilfs = sbi->s_nilfs;
276
277 if (!sci || !sci->sc_flush_request)
278 return;
279
280 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
281 up_read(&nilfs->ns_segctor_sem);
282
283 down_write(&nilfs->ns_segctor_sem);
284 if (sci->sc_flush_request &&
285 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
286 struct nilfs_transaction_info *ti = current->journal_info;
287
288 ti->ti_flags |= NILFS_TI_WRITER;
289 nilfs_segctor_do_immediate_flush(sci);
290 ti->ti_flags &= ~NILFS_TI_WRITER;
291 }
292 downgrade_write(&nilfs->ns_segctor_sem);
293}
294
295static void nilfs_transaction_lock(struct nilfs_sb_info *sbi,
296 struct nilfs_transaction_info *ti,
297 int gcflag)
298{
299 struct nilfs_transaction_info *cur_ti = current->journal_info;
300
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700301 WARN_ON(cur_ti);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700302 ti->ti_flags = NILFS_TI_WRITER;
303 ti->ti_count = 0;
304 ti->ti_save = cur_ti;
305 ti->ti_magic = NILFS_TI_MAGIC;
306 INIT_LIST_HEAD(&ti->ti_garbage);
307 current->journal_info = ti;
308
309 for (;;) {
310 down_write(&sbi->s_nilfs->ns_segctor_sem);
311 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &NILFS_SC(sbi)->sc_flags))
312 break;
313
314 nilfs_segctor_do_immediate_flush(NILFS_SC(sbi));
315
316 up_write(&sbi->s_nilfs->ns_segctor_sem);
317 yield();
318 }
319 if (gcflag)
320 ti->ti_flags |= NILFS_TI_GC;
321}
322
323static void nilfs_transaction_unlock(struct nilfs_sb_info *sbi)
324{
325 struct nilfs_transaction_info *ti = current->journal_info;
326
327 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
328 BUG_ON(ti->ti_count > 0);
329
330 up_write(&sbi->s_nilfs->ns_segctor_sem);
331 current->journal_info = ti->ti_save;
332 if (!list_empty(&ti->ti_garbage))
333 nilfs_dispose_list(sbi, &ti->ti_garbage, 0);
334}
335
336static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
337 struct nilfs_segsum_pointer *ssp,
338 unsigned bytes)
339{
340 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
341 unsigned blocksize = sci->sc_super->s_blocksize;
342 void *p;
343
344 if (unlikely(ssp->offset + bytes > blocksize)) {
345 ssp->offset = 0;
346 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
347 &segbuf->sb_segsum_buffers));
348 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
349 }
350 p = ssp->bh->b_data + ssp->offset;
351 ssp->offset += bytes;
352 return p;
353}
354
355/**
356 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
357 * @sci: nilfs_sc_info
358 */
359static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
360{
361 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
362 struct buffer_head *sumbh;
363 unsigned sumbytes;
364 unsigned flags = 0;
365 int err;
366
367 if (nilfs_doing_gc())
368 flags = NILFS_SS_GC;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900369 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700370 if (unlikely(err))
371 return err;
372
373 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
374 sumbytes = segbuf->sb_sum.sumbytes;
375 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
376 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
377 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
378 return 0;
379}
380
381static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
382{
383 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
384 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
385 return -E2BIG; /* The current segment is filled up
386 (internal code) */
387 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
388 return nilfs_segctor_reset_segment_buffer(sci);
389}
390
391static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
392{
393 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
394 int err;
395
396 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
397 err = nilfs_segctor_feed_segment(sci);
398 if (err)
399 return err;
400 segbuf = sci->sc_curseg;
401 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900402 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700403 if (likely(!err))
404 segbuf->sb_sum.flags |= NILFS_SS_SR;
405 return err;
406}
407
408/*
409 * Functions for making segment summary and payloads
410 */
411static int nilfs_segctor_segsum_block_required(
412 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
413 unsigned binfo_size)
414{
415 unsigned blocksize = sci->sc_super->s_blocksize;
416 /* Size of finfo and binfo is enough small against blocksize */
417
418 return ssp->offset + binfo_size +
419 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
420 blocksize;
421}
422
423static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
424 struct inode *inode)
425{
426 sci->sc_curseg->sb_sum.nfinfo++;
427 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
428 nilfs_segctor_map_segsum_entry(
429 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700430
431 if (inode->i_sb && !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
432 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700433 /* skip finfo */
434}
435
436static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
437 struct inode *inode)
438{
439 struct nilfs_finfo *finfo;
440 struct nilfs_inode_info *ii;
441 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900442 __u64 cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700443
444 if (sci->sc_blk_cnt == 0)
445 return;
446
447 ii = NILFS_I(inode);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900448
449 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
450 cno = ii->i_cno;
451 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
452 cno = 0;
453 else
454 cno = sci->sc_cno;
455
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700456 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
457 sizeof(*finfo));
458 finfo->fi_ino = cpu_to_le64(inode->i_ino);
459 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
460 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
Ryusuke Konishi6c43f412010-08-20 20:10:38 +0900461 finfo->fi_cno = cpu_to_le64(cno);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700462
463 segbuf = sci->sc_curseg;
464 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
465 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
466 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
467 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
468}
469
470static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
471 struct buffer_head *bh,
472 struct inode *inode,
473 unsigned binfo_size)
474{
475 struct nilfs_segment_buffer *segbuf;
476 int required, err = 0;
477
478 retry:
479 segbuf = sci->sc_curseg;
480 required = nilfs_segctor_segsum_block_required(
481 sci, &sci->sc_binfo_ptr, binfo_size);
482 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
483 nilfs_segctor_end_finfo(sci, inode);
484 err = nilfs_segctor_feed_segment(sci);
485 if (err)
486 return err;
487 goto retry;
488 }
489 if (unlikely(required)) {
490 err = nilfs_segbuf_extend_segsum(segbuf);
491 if (unlikely(err))
492 goto failed;
493 }
494 if (sci->sc_blk_cnt == 0)
495 nilfs_segctor_begin_finfo(sci, inode);
496
497 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
498 /* Substitution to vblocknr is delayed until update_blocknr() */
499 nilfs_segbuf_add_file_buffer(segbuf, bh);
500 sci->sc_blk_cnt++;
501 failed:
502 return err;
503}
504
505static int nilfs_handle_bmap_error(int err, const char *fname,
506 struct inode *inode, struct super_block *sb)
507{
508 if (err == -EINVAL) {
509 nilfs_error(sb, fname, "broken bmap (inode=%lu)\n",
510 inode->i_ino);
511 err = -EIO;
512 }
513 return err;
514}
515
516/*
517 * Callback functions that enumerate, mark, and collect dirty blocks
518 */
519static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
520 struct buffer_head *bh, struct inode *inode)
521{
522 int err;
523
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700524 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
525 if (unlikely(err < 0))
526 return nilfs_handle_bmap_error(err, __func__, inode,
527 sci->sc_super);
528
529 err = nilfs_segctor_add_file_block(sci, bh, inode,
530 sizeof(struct nilfs_binfo_v));
531 if (!err)
532 sci->sc_datablk_cnt++;
533 return err;
534}
535
536static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
537 struct buffer_head *bh,
538 struct inode *inode)
539{
540 int err;
541
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700542 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
543 if (unlikely(err < 0))
544 return nilfs_handle_bmap_error(err, __func__, inode,
545 sci->sc_super);
546 return 0;
547}
548
549static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
550 struct buffer_head *bh,
551 struct inode *inode)
552{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700553 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700554 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
555}
556
557static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
558 struct nilfs_segsum_pointer *ssp,
559 union nilfs_binfo *binfo)
560{
561 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
562 sci, ssp, sizeof(*binfo_v));
563 *binfo_v = binfo->bi_v;
564}
565
566static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
567 struct nilfs_segsum_pointer *ssp,
568 union nilfs_binfo *binfo)
569{
570 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
571 sci, ssp, sizeof(*vblocknr));
572 *vblocknr = binfo->bi_v.bi_vblocknr;
573}
574
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900575static struct nilfs_sc_operations nilfs_sc_file_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700576 .collect_data = nilfs_collect_file_data,
577 .collect_node = nilfs_collect_file_node,
578 .collect_bmap = nilfs_collect_file_bmap,
579 .write_data_binfo = nilfs_write_file_data_binfo,
580 .write_node_binfo = nilfs_write_file_node_binfo,
581};
582
583static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
584 struct buffer_head *bh, struct inode *inode)
585{
586 int err;
587
588 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
589 if (unlikely(err < 0))
590 return nilfs_handle_bmap_error(err, __func__, inode,
591 sci->sc_super);
592
593 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
594 if (!err)
595 sci->sc_datablk_cnt++;
596 return err;
597}
598
599static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
600 struct buffer_head *bh, struct inode *inode)
601{
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700602 WARN_ON(!buffer_dirty(bh));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700603 return nilfs_segctor_add_file_block(sci, bh, inode,
604 sizeof(struct nilfs_binfo_dat));
605}
606
607static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
608 struct nilfs_segsum_pointer *ssp,
609 union nilfs_binfo *binfo)
610{
611 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
612 sizeof(*blkoff));
613 *blkoff = binfo->bi_dat.bi_blkoff;
614}
615
616static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
617 struct nilfs_segsum_pointer *ssp,
618 union nilfs_binfo *binfo)
619{
620 struct nilfs_binfo_dat *binfo_dat =
621 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
622 *binfo_dat = binfo->bi_dat;
623}
624
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900625static struct nilfs_sc_operations nilfs_sc_dat_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700626 .collect_data = nilfs_collect_dat_data,
627 .collect_node = nilfs_collect_file_node,
628 .collect_bmap = nilfs_collect_dat_bmap,
629 .write_data_binfo = nilfs_write_dat_data_binfo,
630 .write_node_binfo = nilfs_write_dat_node_binfo,
631};
632
Ryusuke Konishi4e819502010-04-23 17:35:23 +0900633static struct nilfs_sc_operations nilfs_sc_dsync_ops = {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700634 .collect_data = nilfs_collect_file_data,
635 .collect_node = NULL,
636 .collect_bmap = NULL,
637 .write_data_binfo = nilfs_write_file_data_binfo,
638 .write_node_binfo = NULL,
639};
640
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700641static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
642 struct list_head *listp,
643 size_t nlimit,
644 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700645{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700646 struct address_space *mapping = inode->i_mapping;
647 struct pagevec pvec;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700648 pgoff_t index = 0, last = ULONG_MAX;
649 size_t ndirties = 0;
650 int i;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700651
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700652 if (unlikely(start != 0 || end != LLONG_MAX)) {
653 /*
654 * A valid range is given for sync-ing data pages. The
655 * range is rounded to per-page; extra dirty buffers
656 * may be included if blocksize < pagesize.
657 */
658 index = start >> PAGE_SHIFT;
659 last = end >> PAGE_SHIFT;
660 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700661 pagevec_init(&pvec, 0);
662 repeat:
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700663 if (unlikely(index > last) ||
664 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
665 min_t(pgoff_t, last - index,
666 PAGEVEC_SIZE - 1) + 1))
667 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700668
669 for (i = 0; i < pagevec_count(&pvec); i++) {
670 struct buffer_head *bh, *head;
671 struct page *page = pvec.pages[i];
672
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700673 if (unlikely(page->index > last))
674 break;
675
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700676 if (mapping->host) {
677 lock_page(page);
678 if (!page_has_buffers(page))
679 create_empty_buffers(page,
680 1 << inode->i_blkbits, 0);
681 unlock_page(page);
682 }
683
684 bh = head = page_buffers(page);
685 do {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700686 if (!buffer_dirty(bh))
687 continue;
688 get_bh(bh);
689 list_add_tail(&bh->b_assoc_buffers, listp);
690 ndirties++;
691 if (unlikely(ndirties >= nlimit)) {
692 pagevec_release(&pvec);
693 cond_resched();
694 return ndirties;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700695 }
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700696 } while (bh = bh->b_this_page, bh != head);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700697 }
698 pagevec_release(&pvec);
699 cond_resched();
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700700 goto repeat;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700701}
702
703static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
704 struct list_head *listp)
705{
706 struct nilfs_inode_info *ii = NILFS_I(inode);
707 struct address_space *mapping = &ii->i_btnode_cache;
708 struct pagevec pvec;
709 struct buffer_head *bh, *head;
710 unsigned int i;
711 pgoff_t index = 0;
712
713 pagevec_init(&pvec, 0);
714
715 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
716 PAGEVEC_SIZE)) {
717 for (i = 0; i < pagevec_count(&pvec); i++) {
718 bh = head = page_buffers(pvec.pages[i]);
719 do {
720 if (buffer_dirty(bh)) {
721 get_bh(bh);
722 list_add_tail(&bh->b_assoc_buffers,
723 listp);
724 }
725 bh = bh->b_this_page;
726 } while (bh != head);
727 }
728 pagevec_release(&pvec);
729 cond_resched();
730 }
731}
732
733static void nilfs_dispose_list(struct nilfs_sb_info *sbi,
734 struct list_head *head, int force)
735{
736 struct nilfs_inode_info *ii, *n;
737 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
738 unsigned nv = 0;
739
740 while (!list_empty(head)) {
741 spin_lock(&sbi->s_inode_lock);
742 list_for_each_entry_safe(ii, n, head, i_dirty) {
743 list_del_init(&ii->i_dirty);
744 if (force) {
745 if (unlikely(ii->i_bh)) {
746 brelse(ii->i_bh);
747 ii->i_bh = NULL;
748 }
749 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
750 set_bit(NILFS_I_QUEUED, &ii->i_state);
751 list_add_tail(&ii->i_dirty,
752 &sbi->s_dirty_files);
753 continue;
754 }
755 ivec[nv++] = ii;
756 if (nv == SC_N_INODEVEC)
757 break;
758 }
759 spin_unlock(&sbi->s_inode_lock);
760
761 for (pii = ivec; nv > 0; pii++, nv--)
762 iput(&(*pii)->vfs_inode);
763 }
764}
765
766static int nilfs_test_metadata_dirty(struct nilfs_sb_info *sbi)
767{
768 struct the_nilfs *nilfs = sbi->s_nilfs;
769 int ret = 0;
770
771 if (nilfs_mdt_fetch_dirty(sbi->s_ifile))
772 ret++;
773 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
774 ret++;
775 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
776 ret++;
777 if (ret || nilfs_doing_gc())
778 if (nilfs_mdt_fetch_dirty(nilfs_dat_inode(nilfs)))
779 ret++;
780 return ret;
781}
782
783static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
784{
785 return list_empty(&sci->sc_dirty_files) &&
786 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +0900787 sci->sc_nfreesegs == 0 &&
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700788 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
789}
790
791static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
792{
793 struct nilfs_sb_info *sbi = sci->sc_sbi;
794 int ret = 0;
795
796 if (nilfs_test_metadata_dirty(sbi))
797 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
798
799 spin_lock(&sbi->s_inode_lock);
800 if (list_empty(&sbi->s_dirty_files) && nilfs_segctor_clean(sci))
801 ret++;
802
803 spin_unlock(&sbi->s_inode_lock);
804 return ret;
805}
806
807static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
808{
809 struct nilfs_sb_info *sbi = sci->sc_sbi;
810 struct the_nilfs *nilfs = sbi->s_nilfs;
811
812 nilfs_mdt_clear_dirty(sbi->s_ifile);
813 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
814 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
815 nilfs_mdt_clear_dirty(nilfs_dat_inode(nilfs));
816}
817
818static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
819{
820 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
821 struct buffer_head *bh_cp;
822 struct nilfs_checkpoint *raw_cp;
823 int err;
824
825 /* XXX: this interface will be changed */
826 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
827 &raw_cp, &bh_cp);
828 if (likely(!err)) {
829 /* The following code is duplicated with cpfile. But, it is
830 needed to collect the checkpoint even if it was not newly
831 created */
832 nilfs_mdt_mark_buffer_dirty(bh_cp);
833 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
834 nilfs_cpfile_put_checkpoint(
835 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700836 } else
837 WARN_ON(err == -EINVAL || err == -ENOENT);
838
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700839 return err;
840}
841
842static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
843{
844 struct nilfs_sb_info *sbi = sci->sc_sbi;
845 struct the_nilfs *nilfs = sbi->s_nilfs;
846 struct buffer_head *bh_cp;
847 struct nilfs_checkpoint *raw_cp;
848 int err;
849
850 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
851 &raw_cp, &bh_cp);
852 if (unlikely(err)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700853 WARN_ON(err == -EINVAL || err == -ENOENT);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700854 goto failed_ibh;
855 }
856 raw_cp->cp_snapshot_list.ssl_next = 0;
857 raw_cp->cp_snapshot_list.ssl_prev = 0;
858 raw_cp->cp_inodes_count =
859 cpu_to_le64(atomic_read(&sbi->s_inodes_count));
860 raw_cp->cp_blocks_count =
861 cpu_to_le64(atomic_read(&sbi->s_blocks_count));
862 raw_cp->cp_nblk_inc =
863 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
864 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
865 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
Ryusuke Konishi458c5b02009-04-06 19:01:56 -0700866
Ryusuke Konishic96fa462009-04-06 19:01:57 -0700867 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
868 nilfs_checkpoint_clear_minor(raw_cp);
869 else
870 nilfs_checkpoint_set_minor(raw_cp);
871
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700872 nilfs_write_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode, 1);
873 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
874 return 0;
875
876 failed_ibh:
877 return err;
878}
879
880static void nilfs_fill_in_file_bmap(struct inode *ifile,
881 struct nilfs_inode_info *ii)
882
883{
884 struct buffer_head *ibh;
885 struct nilfs_inode *raw_inode;
886
887 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
888 ibh = ii->i_bh;
889 BUG_ON(!ibh);
890 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
891 ibh);
892 nilfs_bmap_write(ii->i_bmap, raw_inode);
893 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
894 }
895}
896
897static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci,
898 struct inode *ifile)
899{
900 struct nilfs_inode_info *ii;
901
902 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
903 nilfs_fill_in_file_bmap(ifile, ii);
904 set_bit(NILFS_I_COLLECTED, &ii->i_state);
905 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700906}
907
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700908static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
909 struct the_nilfs *nilfs)
910{
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900911 struct buffer_head *bh_sr;
912 struct nilfs_super_root *raw_sr;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700913 unsigned isz = nilfs->ns_inode_size;
914
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900915 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
916 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
917
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700918 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
919 raw_sr->sr_nongc_ctime
920 = cpu_to_le64(nilfs_doing_gc() ?
921 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
922 raw_sr->sr_flags = 0;
923
Ryusuke Konishi3961f0e2009-11-13 01:55:02 +0900924 nilfs_write_inode_common(nilfs_dat_inode(nilfs), (void *)raw_sr +
925 NILFS_SR_DAT_OFFSET(isz), 1);
926 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
927 NILFS_SR_CPFILE_OFFSET(isz), 1);
928 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
929 NILFS_SR_SUFILE_OFFSET(isz), 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700930}
931
932static void nilfs_redirty_inodes(struct list_head *head)
933{
934 struct nilfs_inode_info *ii;
935
936 list_for_each_entry(ii, head, i_dirty) {
937 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
938 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
939 }
940}
941
942static void nilfs_drop_collected_inodes(struct list_head *head)
943{
944 struct nilfs_inode_info *ii;
945
946 list_for_each_entry(ii, head, i_dirty) {
947 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
948 continue;
949
950 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
951 set_bit(NILFS_I_UPDATED, &ii->i_state);
952 }
953}
954
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700955static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
956 struct inode *inode,
957 struct list_head *listp,
958 int (*collect)(struct nilfs_sc_info *,
959 struct buffer_head *,
960 struct inode *))
961{
962 struct buffer_head *bh, *n;
963 int err = 0;
964
965 if (collect) {
966 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
967 list_del_init(&bh->b_assoc_buffers);
968 err = collect(sci, bh, inode);
969 brelse(bh);
970 if (unlikely(err))
971 goto dispose_buffers;
972 }
973 return 0;
974 }
975
976 dispose_buffers:
977 while (!list_empty(listp)) {
978 bh = list_entry(listp->next, struct buffer_head,
979 b_assoc_buffers);
980 list_del_init(&bh->b_assoc_buffers);
981 brelse(bh);
982 }
983 return err;
984}
985
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700986static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
987{
988 /* Remaining number of blocks within segment buffer */
989 return sci->sc_segbuf_nblocks -
990 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
991}
992
Ryusuke Konishi9ff051232009-04-06 19:01:37 -0700993static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
994 struct inode *inode,
995 struct nilfs_sc_operations *sc_ops)
996{
997 LIST_HEAD(data_buffers);
998 LIST_HEAD(node_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -0700999 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001000
1001 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001002 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1003
1004 n = nilfs_lookup_dirty_data_buffers(
1005 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1006 if (n > rest) {
1007 err = nilfs_segctor_apply_buffers(
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001008 sci, inode, &data_buffers,
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001009 sc_ops->collect_data);
1010 BUG_ON(!err); /* always receive -E2BIG or true error */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001011 goto break_or_fail;
1012 }
1013 }
1014 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1015
1016 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1017 err = nilfs_segctor_apply_buffers(
1018 sci, inode, &data_buffers, sc_ops->collect_data);
1019 if (unlikely(err)) {
1020 /* dispose node list */
1021 nilfs_segctor_apply_buffers(
1022 sci, inode, &node_buffers, NULL);
1023 goto break_or_fail;
1024 }
1025 sci->sc_stage.flags |= NILFS_CF_NODE;
1026 }
1027 /* Collect node */
1028 err = nilfs_segctor_apply_buffers(
1029 sci, inode, &node_buffers, sc_ops->collect_node);
1030 if (unlikely(err))
1031 goto break_or_fail;
1032
1033 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1034 err = nilfs_segctor_apply_buffers(
1035 sci, inode, &node_buffers, sc_ops->collect_bmap);
1036 if (unlikely(err))
1037 goto break_or_fail;
1038
1039 nilfs_segctor_end_finfo(sci, inode);
1040 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1041
1042 break_or_fail:
1043 return err;
1044}
1045
1046static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1047 struct inode *inode)
1048{
1049 LIST_HEAD(data_buffers);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001050 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1051 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001052
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001053 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1054 sci->sc_dsync_start,
1055 sci->sc_dsync_end);
1056
1057 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1058 nilfs_collect_file_data);
1059 if (!err) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001060 nilfs_segctor_end_finfo(sci, inode);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001061 BUG_ON(n > rest);
1062 /* always receive -E2BIG or true error if n > rest */
1063 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001064 return err;
1065}
1066
1067static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1068{
1069 struct nilfs_sb_info *sbi = sci->sc_sbi;
1070 struct the_nilfs *nilfs = sbi->s_nilfs;
1071 struct list_head *head;
1072 struct nilfs_inode_info *ii;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001073 size_t ndone;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001074 int err = 0;
1075
1076 switch (sci->sc_stage.scnt) {
1077 case NILFS_ST_INIT:
1078 /* Pre-processes */
1079 sci->sc_stage.flags = 0;
1080
1081 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1082 sci->sc_nblk_inc = 0;
1083 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1084 if (mode == SC_LSEG_DSYNC) {
1085 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1086 goto dsync_mode;
1087 }
1088 }
1089
1090 sci->sc_stage.dirty_file_ptr = NULL;
1091 sci->sc_stage.gc_inode_ptr = NULL;
1092 if (mode == SC_FLUSH_DAT) {
1093 sci->sc_stage.scnt = NILFS_ST_DAT;
1094 goto dat_stage;
1095 }
1096 sci->sc_stage.scnt++; /* Fall through */
1097 case NILFS_ST_GC:
1098 if (nilfs_doing_gc()) {
1099 head = &sci->sc_gc_inodes;
1100 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1101 head, i_dirty);
1102 list_for_each_entry_continue(ii, head, i_dirty) {
1103 err = nilfs_segctor_scan_file(
1104 sci, &ii->vfs_inode,
1105 &nilfs_sc_file_ops);
1106 if (unlikely(err)) {
1107 sci->sc_stage.gc_inode_ptr = list_entry(
1108 ii->i_dirty.prev,
1109 struct nilfs_inode_info,
1110 i_dirty);
1111 goto break_or_fail;
1112 }
1113 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1114 }
1115 sci->sc_stage.gc_inode_ptr = NULL;
1116 }
1117 sci->sc_stage.scnt++; /* Fall through */
1118 case NILFS_ST_FILE:
1119 head = &sci->sc_dirty_files;
1120 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1121 i_dirty);
1122 list_for_each_entry_continue(ii, head, i_dirty) {
1123 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1124
1125 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1126 &nilfs_sc_file_ops);
1127 if (unlikely(err)) {
1128 sci->sc_stage.dirty_file_ptr =
1129 list_entry(ii->i_dirty.prev,
1130 struct nilfs_inode_info,
1131 i_dirty);
1132 goto break_or_fail;
1133 }
1134 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1135 /* XXX: required ? */
1136 }
1137 sci->sc_stage.dirty_file_ptr = NULL;
1138 if (mode == SC_FLUSH_FILE) {
1139 sci->sc_stage.scnt = NILFS_ST_DONE;
1140 return 0;
1141 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001142 sci->sc_stage.scnt++;
1143 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1144 /* Fall through */
1145 case NILFS_ST_IFILE:
1146 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
1147 &nilfs_sc_file_ops);
1148 if (unlikely(err))
1149 break;
1150 sci->sc_stage.scnt++;
1151 /* Creating a checkpoint */
1152 err = nilfs_segctor_create_checkpoint(sci);
1153 if (unlikely(err))
1154 break;
1155 /* Fall through */
1156 case NILFS_ST_CPFILE:
1157 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1158 &nilfs_sc_file_ops);
1159 if (unlikely(err))
1160 break;
1161 sci->sc_stage.scnt++; /* Fall through */
1162 case NILFS_ST_SUFILE:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001163 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1164 sci->sc_nfreesegs, &ndone);
1165 if (unlikely(err)) {
1166 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1167 sci->sc_freesegs, ndone,
1168 NULL);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001169 break;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001170 }
1171 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1172
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001173 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1174 &nilfs_sc_file_ops);
1175 if (unlikely(err))
1176 break;
1177 sci->sc_stage.scnt++; /* Fall through */
1178 case NILFS_ST_DAT:
1179 dat_stage:
1180 err = nilfs_segctor_scan_file(sci, nilfs_dat_inode(nilfs),
1181 &nilfs_sc_dat_ops);
1182 if (unlikely(err))
1183 break;
1184 if (mode == SC_FLUSH_DAT) {
1185 sci->sc_stage.scnt = NILFS_ST_DONE;
1186 return 0;
1187 }
1188 sci->sc_stage.scnt++; /* Fall through */
1189 case NILFS_ST_SR:
1190 if (mode == SC_LSEG_SR) {
1191 /* Appending a super root */
1192 err = nilfs_segctor_add_super_root(sci);
1193 if (unlikely(err))
1194 break;
1195 }
1196 /* End of a logical segment */
1197 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1198 sci->sc_stage.scnt = NILFS_ST_DONE;
1199 return 0;
1200 case NILFS_ST_DSYNC:
1201 dsync_mode:
1202 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07001203 ii = sci->sc_dsync_inode;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001204 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1205 break;
1206
1207 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1208 if (unlikely(err))
1209 break;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001210 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1211 sci->sc_stage.scnt = NILFS_ST_DONE;
1212 return 0;
1213 case NILFS_ST_DONE:
1214 return 0;
1215 default:
1216 BUG();
1217 }
1218
1219 break_or_fail:
1220 return err;
1221}
1222
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001223/**
1224 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1225 * @sci: nilfs_sc_info
1226 * @nilfs: nilfs object
1227 */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001228static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1229 struct the_nilfs *nilfs)
1230{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001231 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001232 __u64 nextnum;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001233 int err, alloc = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001234
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001235 segbuf = nilfs_segbuf_new(sci->sc_super);
1236 if (unlikely(!segbuf))
1237 return -ENOMEM;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001238
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001239 if (list_empty(&sci->sc_write_logs)) {
1240 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1241 nilfs->ns_pseg_offset, nilfs);
1242 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1243 nilfs_shift_to_next_segment(nilfs);
1244 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1245 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001246
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001247 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001248 nextnum = nilfs->ns_nextnum;
1249
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001250 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1251 /* Start from the head of a new full segment */
1252 alloc++;
1253 } else {
1254 /* Continue logs */
1255 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1256 nilfs_segbuf_map_cont(segbuf, prev);
1257 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1258 nextnum = prev->sb_nextnum;
1259
1260 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1261 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1262 segbuf->sb_sum.seg_seq++;
1263 alloc++;
1264 }
1265 }
1266
1267 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1268 if (err)
1269 goto failed;
1270
1271 if (alloc) {
1272 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1273 if (err)
1274 goto failed;
1275 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001276 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1277
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001278 BUG_ON(!list_empty(&sci->sc_segbufs));
1279 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1280 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
Ryusuke Konishicece5522009-04-06 19:01:58 -07001281 return 0;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001282
1283 failed:
1284 nilfs_segbuf_free(segbuf);
1285 return err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001286}
1287
1288static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1289 struct the_nilfs *nilfs, int nadd)
1290{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001291 struct nilfs_segment_buffer *segbuf, *prev;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001292 struct inode *sufile = nilfs->ns_sufile;
1293 __u64 nextnextnum;
1294 LIST_HEAD(list);
1295 int err, ret, i;
1296
1297 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1298 /*
1299 * Since the segment specified with nextnum might be allocated during
1300 * the previous construction, the buffer including its segusage may
1301 * not be dirty. The following call ensures that the buffer is dirty
1302 * and will pin the buffer on memory until the sufile is written.
1303 */
Ryusuke Konishi61a189e2009-11-18 17:25:12 +09001304 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001305 if (unlikely(err))
1306 return err;
1307
1308 for (i = 0; i < nadd; i++) {
1309 /* extend segment info */
1310 err = -ENOMEM;
1311 segbuf = nilfs_segbuf_new(sci->sc_super);
1312 if (unlikely(!segbuf))
1313 goto failed;
1314
1315 /* map this buffer to region of segment on-disk */
Ryusuke Konishicece5522009-04-06 19:01:58 -07001316 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001317 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1318
1319 /* allocate the next next full segment */
1320 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1321 if (unlikely(err))
1322 goto failed_segbuf;
1323
1324 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1325 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1326
1327 list_add_tail(&segbuf->sb_list, &list);
1328 prev = segbuf;
1329 }
Ryusuke Konishi0935db72009-11-29 02:39:11 +09001330 list_splice_tail(&list, &sci->sc_segbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001331 return 0;
1332
1333 failed_segbuf:
1334 nilfs_segbuf_free(segbuf);
1335 failed:
Ryusuke Konishie29df392009-11-29 16:51:16 +09001336 list_for_each_entry(segbuf, &list, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001337 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001338 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001339 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001340 nilfs_destroy_logs(&list);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001341 return err;
1342}
1343
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001344static void nilfs_free_incomplete_logs(struct list_head *logs,
1345 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001346{
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001347 struct nilfs_segment_buffer *segbuf, *prev;
1348 struct inode *sufile = nilfs->ns_sufile;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001349 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001350
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001351 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001352 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001353 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001354 WARN_ON(ret); /* never fails */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001355 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001356 if (atomic_read(&segbuf->sb_err)) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001357 /* Case 1: The first segment failed */
1358 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1359 /* Case 1a: Partial segment appended into an existing
1360 segment */
1361 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1362 segbuf->sb_fseg_end);
1363 else /* Case 1b: New full segment */
1364 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001365 }
1366
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001367 prev = segbuf;
1368 list_for_each_entry_continue(segbuf, logs, sb_list) {
1369 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1370 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1371 WARN_ON(ret); /* never fails */
1372 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +09001373 if (atomic_read(&segbuf->sb_err) &&
1374 segbuf->sb_segnum != nilfs->ns_nextnum)
1375 /* Case 2: extended segment (!= next) failed */
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001376 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1377 prev = segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001378 }
1379}
1380
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001381static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1382 struct inode *sufile)
1383{
1384 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001385 unsigned long live_blocks;
1386 int ret;
1387
1388 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001389 live_blocks = segbuf->sb_sum.nblocks +
1390 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001391 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1392 live_blocks,
1393 sci->sc_seg_ctime);
1394 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001395 }
1396}
1397
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001398static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001399{
1400 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001401 int ret;
1402
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001403 segbuf = NILFS_FIRST_SEGBUF(logs);
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001404 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1405 segbuf->sb_pseg_start -
1406 segbuf->sb_fseg_start, 0);
1407 WARN_ON(ret); /* always succeed because the segusage is dirty */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001408
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001409 list_for_each_entry_continue(segbuf, logs, sb_list) {
Ryusuke Konishi071ec542009-11-18 18:23:34 +09001410 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1411 0, 0);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001412 WARN_ON(ret); /* always succeed */
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001413 }
1414}
1415
1416static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1417 struct nilfs_segment_buffer *last,
1418 struct inode *sufile)
1419{
Ryusuke Konishie29df392009-11-29 16:51:16 +09001420 struct nilfs_segment_buffer *segbuf = last;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001421 int ret;
1422
Ryusuke Konishie29df392009-11-29 16:51:16 +09001423 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001424 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1425 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001426 WARN_ON(ret);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001427 }
Ryusuke Konishie29df392009-11-29 16:51:16 +09001428 nilfs_truncate_logs(&sci->sc_segbufs, last);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001429}
1430
1431
1432static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1433 struct the_nilfs *nilfs, int mode)
1434{
1435 struct nilfs_cstage prev_stage = sci->sc_stage;
1436 int err, nadd = 1;
1437
1438 /* Collection retry loop */
1439 for (;;) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001440 sci->sc_nblk_this_inc = 0;
1441 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1442
1443 err = nilfs_segctor_reset_segment_buffer(sci);
1444 if (unlikely(err))
1445 goto failed;
1446
1447 err = nilfs_segctor_collect_blocks(sci, mode);
1448 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1449 if (!err)
1450 break;
1451
1452 if (unlikely(err != -E2BIG))
1453 goto failed;
1454
1455 /* The current segment is filled up */
1456 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1457 break;
1458
Ryusuke Konishi2d8428a2010-03-22 14:01:24 +09001459 nilfs_clear_logs(&sci->sc_segbufs);
1460
1461 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1462 if (unlikely(err))
1463 return err;
1464
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09001465 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1466 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1467 sci->sc_freesegs,
1468 sci->sc_nfreesegs,
1469 NULL);
1470 WARN_ON(err); /* do not happen */
1471 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001472 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1473 sci->sc_stage = prev_stage;
1474 }
1475 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1476 return 0;
1477
1478 failed:
1479 return err;
1480}
1481
1482static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1483 struct buffer_head *new_bh)
1484{
1485 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1486
1487 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1488 /* The caller must release old_bh */
1489}
1490
1491static int
1492nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1493 struct nilfs_segment_buffer *segbuf,
1494 int mode)
1495{
1496 struct inode *inode = NULL;
1497 sector_t blocknr;
1498 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1499 unsigned long nblocks = 0, ndatablk = 0;
1500 struct nilfs_sc_operations *sc_op = NULL;
1501 struct nilfs_segsum_pointer ssp;
1502 struct nilfs_finfo *finfo = NULL;
1503 union nilfs_binfo binfo;
1504 struct buffer_head *bh, *bh_org;
1505 ino_t ino = 0;
1506 int err = 0;
1507
1508 if (!nfinfo)
1509 goto out;
1510
1511 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1512 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1513 ssp.offset = sizeof(struct nilfs_segment_summary);
1514
1515 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001516 if (bh == segbuf->sb_super_root)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001517 break;
1518 if (!finfo) {
1519 finfo = nilfs_segctor_map_segsum_entry(
1520 sci, &ssp, sizeof(*finfo));
1521 ino = le64_to_cpu(finfo->fi_ino);
1522 nblocks = le32_to_cpu(finfo->fi_nblocks);
1523 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1524
1525 if (buffer_nilfs_node(bh))
1526 inode = NILFS_BTNC_I(bh->b_page->mapping);
1527 else
1528 inode = NILFS_AS_I(bh->b_page->mapping);
1529
1530 if (mode == SC_LSEG_DSYNC)
1531 sc_op = &nilfs_sc_dsync_ops;
1532 else if (ino == NILFS_DAT_INO)
1533 sc_op = &nilfs_sc_dat_ops;
1534 else /* file blocks */
1535 sc_op = &nilfs_sc_file_ops;
1536 }
1537 bh_org = bh;
1538 get_bh(bh_org);
1539 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1540 &binfo);
1541 if (bh != bh_org)
1542 nilfs_list_replace_buffer(bh_org, bh);
1543 brelse(bh_org);
1544 if (unlikely(err))
1545 goto failed_bmap;
1546
1547 if (ndatablk > 0)
1548 sc_op->write_data_binfo(sci, &ssp, &binfo);
1549 else
1550 sc_op->write_node_binfo(sci, &ssp, &binfo);
1551
1552 blocknr++;
1553 if (--nblocks == 0) {
1554 finfo = NULL;
1555 if (--nfinfo == 0)
1556 break;
1557 } else if (ndatablk > 0)
1558 ndatablk--;
1559 }
1560 out:
1561 return 0;
1562
1563 failed_bmap:
1564 err = nilfs_handle_bmap_error(err, __func__, inode, sci->sc_super);
1565 return err;
1566}
1567
1568static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1569{
1570 struct nilfs_segment_buffer *segbuf;
1571 int err;
1572
1573 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1574 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1575 if (unlikely(err))
1576 return err;
1577 nilfs_segbuf_fill_in_segsum(segbuf);
1578 }
1579 return 0;
1580}
1581
1582static int
1583nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1584{
1585 struct page *clone_page;
1586 struct buffer_head *bh, *head, *bh2;
1587 void *kaddr;
1588
1589 bh = head = page_buffers(page);
1590
1591 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1592 if (unlikely(!clone_page))
1593 return -ENOMEM;
1594
1595 bh2 = page_buffers(clone_page);
1596 kaddr = kmap_atomic(page, KM_USER0);
1597 do {
1598 if (list_empty(&bh->b_assoc_buffers))
1599 continue;
1600 get_bh(bh2);
1601 page_cache_get(clone_page); /* for each bh */
1602 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1603 bh2->b_blocknr = bh->b_blocknr;
1604 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1605 list_add_tail(&bh->b_assoc_buffers, out);
1606 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1607 kunmap_atomic(kaddr, KM_USER0);
1608
1609 if (!TestSetPageWriteback(clone_page))
1610 inc_zone_page_state(clone_page, NR_WRITEBACK);
1611 unlock_page(clone_page);
1612
1613 return 0;
1614}
1615
1616static int nilfs_test_page_to_be_frozen(struct page *page)
1617{
1618 struct address_space *mapping = page->mapping;
1619
1620 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1621 return 0;
1622
1623 if (page_mapped(page)) {
1624 ClearPageChecked(page);
1625 return 1;
1626 }
1627 return PageChecked(page);
1628}
1629
1630static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1631{
1632 if (!page || PageWriteback(page))
1633 /* For split b-tree node pages, this function may be called
1634 twice. We ignore the 2nd or later calls by this check. */
1635 return 0;
1636
1637 lock_page(page);
1638 clear_page_dirty_for_io(page);
1639 set_page_writeback(page);
1640 unlock_page(page);
1641
1642 if (nilfs_test_page_to_be_frozen(page)) {
1643 int err = nilfs_copy_replace_page_buffers(page, out);
1644 if (unlikely(err))
1645 return err;
1646 }
1647 return 0;
1648}
1649
1650static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1651 struct page **failed_page)
1652{
1653 struct nilfs_segment_buffer *segbuf;
1654 struct page *bd_page = NULL, *fs_page = NULL;
1655 struct list_head *list = &sci->sc_copied_buffers;
1656 int err;
1657
1658 *failed_page = NULL;
1659 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1660 struct buffer_head *bh;
1661
1662 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1663 b_assoc_buffers) {
1664 if (bh->b_page != bd_page) {
1665 if (bd_page) {
1666 lock_page(bd_page);
1667 clear_page_dirty_for_io(bd_page);
1668 set_page_writeback(bd_page);
1669 unlock_page(bd_page);
1670 }
1671 bd_page = bh->b_page;
1672 }
1673 }
1674
1675 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1676 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001677 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001678 if (bh->b_page != bd_page) {
1679 lock_page(bd_page);
1680 clear_page_dirty_for_io(bd_page);
1681 set_page_writeback(bd_page);
1682 unlock_page(bd_page);
1683 bd_page = bh->b_page;
1684 }
1685 break;
1686 }
1687 if (bh->b_page != fs_page) {
1688 err = nilfs_begin_page_io(fs_page, list);
1689 if (unlikely(err)) {
1690 *failed_page = fs_page;
1691 goto out;
1692 }
1693 fs_page = bh->b_page;
1694 }
1695 }
1696 }
1697 if (bd_page) {
1698 lock_page(bd_page);
1699 clear_page_dirty_for_io(bd_page);
1700 set_page_writeback(bd_page);
1701 unlock_page(bd_page);
1702 }
1703 err = nilfs_begin_page_io(fs_page, list);
1704 if (unlikely(err))
1705 *failed_page = fs_page;
1706 out:
1707 return err;
1708}
1709
1710static int nilfs_segctor_write(struct nilfs_sc_info *sci,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09001711 struct the_nilfs *nilfs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001712{
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001713 int ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001714
Ryusuke Konishid1c6b722009-12-17 00:55:40 +09001715 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001716 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1717 return ret;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001718}
1719
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001720static void __nilfs_end_page_io(struct page *page, int err)
1721{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001722 if (!err) {
1723 if (!nilfs_page_buffers_clean(page))
1724 __set_page_dirty_nobuffers(page);
1725 ClearPageError(page);
1726 } else {
1727 __set_page_dirty_nobuffers(page);
1728 SetPageError(page);
1729 }
1730
1731 if (buffer_nilfs_allocated(page_buffers(page))) {
1732 if (TestClearPageWriteback(page))
1733 dec_zone_page_state(page, NR_WRITEBACK);
1734 } else
1735 end_page_writeback(page);
1736}
1737
1738static void nilfs_end_page_io(struct page *page, int err)
1739{
1740 if (!page)
1741 return;
1742
Ryusuke Konishia9777842009-07-28 17:55:29 +09001743 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001744 /*
1745 * For b-tree node pages, this function may be called twice
1746 * or more because they might be split in a segment.
1747 */
Ryusuke Konishia9777842009-07-28 17:55:29 +09001748 if (PageDirty(page)) {
1749 /*
1750 * For pages holding split b-tree node buffers, dirty
1751 * flag on the buffers may be cleared discretely.
1752 * In that case, the page is once redirtied for
1753 * remaining buffers, and it must be cancelled if
1754 * all the buffers get cleaned later.
1755 */
1756 lock_page(page);
1757 if (nilfs_page_buffers_clean(page))
1758 __nilfs_clear_page_dirty(page);
1759 unlock_page(page);
1760 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001761 return;
Ryusuke Konishia9777842009-07-28 17:55:29 +09001762 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001763
1764 __nilfs_end_page_io(page, err);
1765}
1766
1767static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1768{
1769 struct buffer_head *bh, *head;
1770 struct page *page;
1771
1772 while (!list_empty(list)) {
1773 bh = list_entry(list->next, struct buffer_head,
1774 b_assoc_buffers);
1775 page = bh->b_page;
1776 page_cache_get(page);
1777 head = bh = page_buffers(page);
1778 do {
1779 if (!list_empty(&bh->b_assoc_buffers)) {
1780 list_del_init(&bh->b_assoc_buffers);
1781 if (!err) {
1782 set_buffer_uptodate(bh);
1783 clear_buffer_dirty(bh);
1784 clear_buffer_nilfs_volatile(bh);
1785 }
1786 brelse(bh); /* for b_assoc_buffers */
1787 }
1788 } while ((bh = bh->b_this_page) != head);
1789
1790 __nilfs_end_page_io(page, err);
1791 page_cache_release(page);
1792 }
1793}
1794
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001795static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001796 int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001797{
1798 struct nilfs_segment_buffer *segbuf;
1799 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001800 struct buffer_head *bh;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001801
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001802 if (list_empty(logs))
1803 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001804
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001805 list_for_each_entry(segbuf, logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001806 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1807 b_assoc_buffers) {
1808 if (bh->b_page != bd_page) {
1809 if (bd_page)
1810 end_page_writeback(bd_page);
1811 bd_page = bh->b_page;
1812 }
1813 }
1814
1815 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1816 b_assoc_buffers) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001817 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001818 if (bh->b_page != bd_page) {
1819 end_page_writeback(bd_page);
1820 bd_page = bh->b_page;
1821 }
1822 break;
1823 }
1824 if (bh->b_page != fs_page) {
1825 nilfs_end_page_io(fs_page, err);
Ryusuke Konishi8227b292009-06-18 23:52:23 +09001826 if (fs_page && fs_page == failed_page)
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001827 return;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001828 fs_page = bh->b_page;
1829 }
1830 }
1831 }
1832 if (bd_page)
1833 end_page_writeback(bd_page);
1834
1835 nilfs_end_page_io(fs_page, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001836}
1837
1838static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1839 struct the_nilfs *nilfs, int err)
1840{
1841 LIST_HEAD(logs);
1842 int ret;
1843
1844 list_splice_tail_init(&sci->sc_write_logs, &logs);
1845 ret = nilfs_wait_on_logs(&logs);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001846 nilfs_abort_logs(&logs, NULL, ret ? : err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001847
1848 list_splice_tail_init(&sci->sc_segbufs, &logs);
1849 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1850 nilfs_free_incomplete_logs(&logs, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001851 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001852
1853 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1854 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1855 sci->sc_freesegs,
1856 sci->sc_nfreesegs,
1857 NULL);
1858 WARN_ON(ret); /* do not happen */
1859 }
1860
1861 nilfs_destroy_logs(&logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001862}
1863
1864static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1865 struct nilfs_segment_buffer *segbuf)
1866{
1867 nilfs->ns_segnum = segbuf->sb_segnum;
1868 nilfs->ns_nextnum = segbuf->sb_nextnum;
1869 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1870 + segbuf->sb_sum.nblocks;
1871 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1872 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1873}
1874
1875static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1876{
1877 struct nilfs_segment_buffer *segbuf;
1878 struct page *bd_page = NULL, *fs_page = NULL;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09001879 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001880 int update_sr = false;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001881
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001882 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001883 struct buffer_head *bh;
1884
1885 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1886 b_assoc_buffers) {
1887 set_buffer_uptodate(bh);
1888 clear_buffer_dirty(bh);
1889 if (bh->b_page != bd_page) {
1890 if (bd_page)
1891 end_page_writeback(bd_page);
1892 bd_page = bh->b_page;
1893 }
1894 }
1895 /*
1896 * We assume that the buffers which belong to the same page
1897 * continue over the buffer list.
1898 * Under this assumption, the last BHs of pages is
1899 * identifiable by the discontinuity of bh->b_page
1900 * (page != fs_page).
1901 *
1902 * For B-tree node blocks, however, this assumption is not
1903 * guaranteed. The cleanup code of B-tree node pages needs
1904 * special care.
1905 */
1906 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1907 b_assoc_buffers) {
1908 set_buffer_uptodate(bh);
1909 clear_buffer_dirty(bh);
1910 clear_buffer_nilfs_volatile(bh);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001911 if (bh == segbuf->sb_super_root) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001912 if (bh->b_page != bd_page) {
1913 end_page_writeback(bd_page);
1914 bd_page = bh->b_page;
1915 }
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09001916 update_sr = true;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001917 break;
1918 }
1919 if (bh->b_page != fs_page) {
1920 nilfs_end_page_io(fs_page, 0);
1921 fs_page = bh->b_page;
1922 }
1923 }
1924
Ryusuke Konishi47620772010-05-23 21:48:36 +09001925 if (!nilfs_segbuf_simplex(segbuf)) {
1926 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001927 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1928 sci->sc_lseg_stime = jiffies;
1929 }
Ryusuke Konishi47620772010-05-23 21:48:36 +09001930 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001931 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1932 }
1933 }
1934 /*
1935 * Since pages may continue over multiple segment buffers,
1936 * end of the last page must be checked outside of the loop.
1937 */
1938 if (bd_page)
1939 end_page_writeback(bd_page);
1940
1941 nilfs_end_page_io(fs_page, 0);
1942
1943 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
1944
1945 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1946
1947 if (nilfs_doing_gc()) {
1948 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
1949 if (update_sr)
1950 nilfs_commit_gcdat_inode(nilfs);
Ryusuke Konishi1088dcf2009-04-06 19:01:51 -07001951 } else
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001952 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001953
1954 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1955
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001956 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001957 nilfs_set_next_segment(nilfs, segbuf);
1958
1959 if (update_sr) {
1960 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
Ryusuke Konishie339ad32009-04-06 19:01:59 -07001961 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001962
Ryusuke Konishic96fa462009-04-06 19:01:57 -07001963 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001964 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1965 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001966 nilfs_segctor_clear_metadata_dirty(sci);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001967 } else
1968 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1969}
1970
Ryusuke Konishia694291a2009-11-29 23:03:04 +09001971static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1972{
1973 int ret;
1974
1975 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1976 if (!ret) {
1977 nilfs_segctor_complete_write(sci);
1978 nilfs_destroy_logs(&sci->sc_write_logs);
1979 }
1980 return ret;
1981}
1982
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001983static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
1984 struct nilfs_sb_info *sbi)
1985{
1986 struct nilfs_inode_info *ii, *n;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07001987
1988 spin_lock(&sbi->s_inode_lock);
1989 retry:
1990 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
1991 if (!ii->i_bh) {
1992 struct buffer_head *ibh;
1993 int err;
1994
1995 spin_unlock(&sbi->s_inode_lock);
1996 err = nilfs_ifile_get_inode_block(
1997 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
1998 if (unlikely(err)) {
1999 nilfs_warning(sbi->s_super, __func__,
2000 "failed to get inode block.\n");
2001 return err;
2002 }
2003 nilfs_mdt_mark_buffer_dirty(ibh);
2004 nilfs_mdt_mark_dirty(sbi->s_ifile);
2005 spin_lock(&sbi->s_inode_lock);
2006 if (likely(!ii->i_bh))
2007 ii->i_bh = ibh;
2008 else
2009 brelse(ibh);
2010 goto retry;
2011 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002012
2013 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2014 set_bit(NILFS_I_BUSY, &ii->i_state);
2015 list_del(&ii->i_dirty);
2016 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2017 }
2018 spin_unlock(&sbi->s_inode_lock);
2019
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002020 return 0;
2021}
2022
2023static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2024 struct nilfs_sb_info *sbi)
2025{
2026 struct nilfs_transaction_info *ti = current->journal_info;
2027 struct nilfs_inode_info *ii, *n;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002028
2029 spin_lock(&sbi->s_inode_lock);
2030 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2031 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002032 test_bit(NILFS_I_DIRTY, &ii->i_state))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002033 continue;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002034
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002035 clear_bit(NILFS_I_BUSY, &ii->i_state);
2036 brelse(ii->i_bh);
2037 ii->i_bh = NULL;
2038 list_del(&ii->i_dirty);
2039 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2040 }
2041 spin_unlock(&sbi->s_inode_lock);
2042}
2043
2044/*
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002045 * Main procedure of segment constructor
2046 */
2047static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2048{
2049 struct nilfs_sb_info *sbi = sci->sc_sbi;
2050 struct the_nilfs *nilfs = sbi->s_nilfs;
2051 struct page *failed_page;
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002052 int err;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002053
2054 sci->sc_stage.scnt = NILFS_ST_INIT;
Ryusuke Konishi6c43f412010-08-20 20:10:38 +09002055 sci->sc_cno = nilfs->ns_cno;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002056
2057 err = nilfs_segctor_check_in_files(sci, sbi);
2058 if (unlikely(err))
2059 goto out;
2060
2061 if (nilfs_test_metadata_dirty(sbi))
2062 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2063
2064 if (nilfs_segctor_clean(sci))
2065 goto out;
2066
2067 do {
2068 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2069
2070 err = nilfs_segctor_begin_construction(sci, nilfs);
2071 if (unlikely(err))
2072 goto out;
2073
2074 /* Update time stamp */
2075 sci->sc_seg_ctime = get_seconds();
2076
2077 err = nilfs_segctor_collect(sci, nilfs, mode);
2078 if (unlikely(err))
2079 goto failed;
2080
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002081 /* Avoid empty segment */
2082 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
Ryusuke Konishi47620772010-05-23 21:48:36 +09002083 nilfs_segbuf_empty(sci->sc_curseg)) {
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002084 nilfs_segctor_abort_construction(sci, nilfs, 1);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002085 goto out;
2086 }
2087
2088 err = nilfs_segctor_assign(sci, mode);
2089 if (unlikely(err))
2090 goto failed;
2091
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002092 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2093 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2094
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002095 if (mode == SC_LSEG_SR &&
2096 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002097 err = nilfs_segctor_fill_in_checkpoint(sci);
2098 if (unlikely(err))
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002099 goto failed_to_write;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002100
2101 nilfs_segctor_fill_in_super_root(sci, nilfs);
2102 }
2103 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2104
2105 /* Write partial segments */
2106 err = nilfs_segctor_prepare_write(sci, &failed_page);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002107 if (err) {
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +09002108 nilfs_abort_logs(&sci->sc_segbufs, failed_page, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002109 goto failed_to_write;
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002110 }
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +09002111
2112 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2113 nilfs->ns_crc_seed);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002114
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +09002115 err = nilfs_segctor_write(sci, nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002116 if (unlikely(err))
2117 goto failed_to_write;
2118
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002119 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2120 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2121 /*
2122 * At this point, we avoid double buffering
2123 * for blocksize < pagesize because page dirty
2124 * flag is turned off during write and dirty
2125 * buffers are not properly collected for
2126 * pages crossing over segments.
2127 */
2128 err = nilfs_segctor_wait(sci);
2129 if (err)
2130 goto failed_to_write;
2131 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002132 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2133
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002134 out:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002135 nilfs_segctor_check_out_files(sci, sbi);
2136 return err;
2137
2138 failed_to_write:
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002139 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2140 nilfs_redirty_inodes(&sci->sc_dirty_files);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002141
2142 failed:
2143 if (nilfs_doing_gc())
2144 nilfs_redirty_inodes(&sci->sc_gc_inodes);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002145 nilfs_segctor_abort_construction(sci, nilfs, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002146 goto out;
2147}
2148
2149/**
Ryusuke Konishi9ccf56c2010-03-14 03:01:03 +09002150 * nilfs_segctor_start_timer - set timer of background write
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002151 * @sci: nilfs_sc_info
2152 *
2153 * If the timer has already been set, it ignores the new request.
2154 * This function MUST be called within a section locking the segment
2155 * semaphore.
2156 */
2157static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2158{
2159 spin_lock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002160 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2161 sci->sc_timer.expires = jiffies + sci->sc_interval;
2162 add_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002163 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2164 }
2165 spin_unlock(&sci->sc_state_lock);
2166}
2167
2168static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2169{
2170 spin_lock(&sci->sc_state_lock);
2171 if (!(sci->sc_flush_request & (1 << bn))) {
2172 unsigned long prev_req = sci->sc_flush_request;
2173
2174 sci->sc_flush_request |= (1 << bn);
2175 if (!prev_req)
2176 wake_up(&sci->sc_wait_daemon);
2177 }
2178 spin_unlock(&sci->sc_state_lock);
2179}
2180
2181/**
2182 * nilfs_flush_segment - trigger a segment construction for resource control
2183 * @sb: super block
2184 * @ino: inode number of the file to be flushed out.
2185 */
2186void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2187{
2188 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2189 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2190
2191 if (!sci || nilfs_doing_construction())
2192 return;
2193 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2194 /* assign bit 0 to data files */
2195}
2196
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002197struct nilfs_segctor_wait_request {
2198 wait_queue_t wq;
2199 __u32 seq;
2200 int err;
2201 atomic_t done;
2202};
2203
2204static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2205{
2206 struct nilfs_segctor_wait_request wait_req;
2207 int err = 0;
2208
2209 spin_lock(&sci->sc_state_lock);
2210 init_wait(&wait_req.wq);
2211 wait_req.err = 0;
2212 atomic_set(&wait_req.done, 0);
2213 wait_req.seq = ++sci->sc_seq_request;
2214 spin_unlock(&sci->sc_state_lock);
2215
2216 init_waitqueue_entry(&wait_req.wq, current);
2217 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2218 set_current_state(TASK_INTERRUPTIBLE);
2219 wake_up(&sci->sc_wait_daemon);
2220
2221 for (;;) {
2222 if (atomic_read(&wait_req.done)) {
2223 err = wait_req.err;
2224 break;
2225 }
2226 if (!signal_pending(current)) {
2227 schedule();
2228 continue;
2229 }
2230 err = -ERESTARTSYS;
2231 break;
2232 }
2233 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2234 return err;
2235}
2236
2237static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2238{
2239 struct nilfs_segctor_wait_request *wrq, *n;
2240 unsigned long flags;
2241
2242 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2243 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2244 wq.task_list) {
2245 if (!atomic_read(&wrq->done) &&
2246 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2247 wrq->err = err;
2248 atomic_set(&wrq->done, 1);
2249 }
2250 if (atomic_read(&wrq->done)) {
2251 wrq->wq.func(&wrq->wq,
2252 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2253 0, NULL);
2254 }
2255 }
2256 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2257}
2258
2259/**
2260 * nilfs_construct_segment - construct a logical segment
2261 * @sb: super block
2262 *
2263 * Return Value: On success, 0 is retured. On errors, one of the following
2264 * negative error code is returned.
2265 *
2266 * %-EROFS - Read only filesystem.
2267 *
2268 * %-EIO - I/O error
2269 *
2270 * %-ENOSPC - No space left on device (only in a panic state).
2271 *
2272 * %-ERESTARTSYS - Interrupted.
2273 *
2274 * %-ENOMEM - Insufficient memory available.
2275 */
2276int nilfs_construct_segment(struct super_block *sb)
2277{
2278 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2279 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2280 struct nilfs_transaction_info *ti;
2281 int err;
2282
2283 if (!sci)
2284 return -EROFS;
2285
2286 /* A call inside transactions causes a deadlock. */
2287 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2288
2289 err = nilfs_segctor_sync(sci);
2290 return err;
2291}
2292
2293/**
2294 * nilfs_construct_dsync_segment - construct a data-only logical segment
2295 * @sb: super block
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002296 * @inode: inode whose data blocks should be written out
2297 * @start: start byte offset
2298 * @end: end byte offset (inclusive)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002299 *
2300 * Return Value: On success, 0 is retured. On errors, one of the following
2301 * negative error code is returned.
2302 *
2303 * %-EROFS - Read only filesystem.
2304 *
2305 * %-EIO - I/O error
2306 *
2307 * %-ENOSPC - No space left on device (only in a panic state).
2308 *
2309 * %-ERESTARTSYS - Interrupted.
2310 *
2311 * %-ENOMEM - Insufficient memory available.
2312 */
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002313int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2314 loff_t start, loff_t end)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002315{
2316 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2317 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2318 struct nilfs_inode_info *ii;
2319 struct nilfs_transaction_info ti;
2320 int err = 0;
2321
2322 if (!sci)
2323 return -EROFS;
2324
2325 nilfs_transaction_lock(sbi, &ti, 0);
2326
2327 ii = NILFS_I(inode);
2328 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2329 nilfs_test_opt(sbi, STRICT_ORDER) ||
2330 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2331 nilfs_discontinued(sbi->s_nilfs)) {
2332 nilfs_transaction_unlock(sbi);
2333 err = nilfs_segctor_sync(sci);
2334 return err;
2335 }
2336
2337 spin_lock(&sbi->s_inode_lock);
2338 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2339 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2340 spin_unlock(&sbi->s_inode_lock);
2341 nilfs_transaction_unlock(sbi);
2342 return 0;
2343 }
2344 spin_unlock(&sbi->s_inode_lock);
Ryusuke Konishif30bf3e2009-04-06 19:01:38 -07002345 sci->sc_dsync_inode = ii;
2346 sci->sc_dsync_start = start;
2347 sci->sc_dsync_end = end;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002348
2349 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2350
2351 nilfs_transaction_unlock(sbi);
2352 return err;
2353}
2354
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002355#define FLUSH_FILE_BIT (0x1) /* data file only */
2356#define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2357
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002358/**
2359 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2360 * @sci: segment constructor object
2361 */
2362static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002363{
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002364 spin_lock(&sci->sc_state_lock);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002365 sci->sc_seq_accepted = sci->sc_seq_request;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002366 spin_unlock(&sci->sc_state_lock);
Li Hongfdce8952010-04-10 23:25:39 +08002367 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002368}
2369
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002370/**
2371 * nilfs_segctor_notify - notify the result of request to caller threads
2372 * @sci: segment constructor object
2373 * @mode: mode of log forming
2374 * @err: error code to be notified
2375 */
2376static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002377{
2378 /* Clear requests (even when the construction failed) */
2379 spin_lock(&sci->sc_state_lock);
2380
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002381 if (mode == SC_LSEG_SR) {
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002382 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002383 sci->sc_seq_done = sci->sc_seq_accepted;
2384 nilfs_segctor_wakeup(sci, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002385 sci->sc_flush_request = 0;
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002386 } else {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002387 if (mode == SC_FLUSH_FILE)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002388 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002389 else if (mode == SC_FLUSH_DAT)
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002390 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002391
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002392 /* re-enable timer if checkpoint creation was not done */
Li Hongfdce8952010-04-10 23:25:39 +08002393 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2394 time_before(jiffies, sci->sc_timer.expires))
2395 add_timer(&sci->sc_timer);
Ryusuke Konishiaeda7f62009-11-02 15:08:13 +09002396 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002397 spin_unlock(&sci->sc_state_lock);
2398}
2399
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002400/**
2401 * nilfs_segctor_construct - form logs and write them to disk
2402 * @sci: segment constructor object
2403 * @mode: mode of log forming
2404 */
2405static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002406{
2407 struct nilfs_sb_info *sbi = sci->sc_sbi;
2408 struct the_nilfs *nilfs = sbi->s_nilfs;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002409 struct nilfs_super_block **sbp;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002410 int err = 0;
2411
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002412 nilfs_segctor_accept(sci);
2413
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002414 if (nilfs_discontinued(nilfs))
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002415 mode = SC_LSEG_SR;
2416 if (!nilfs_segctor_confirm(sci))
2417 err = nilfs_segctor_do_construct(sci, mode);
2418
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002419 if (likely(!err)) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002420 if (mode != SC_FLUSH_DAT)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002421 atomic_set(&nilfs->ns_ndirtyblks, 0);
2422 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2423 nilfs_discontinued(nilfs)) {
2424 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +09002425 err = -EIO;
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +09002426 sbp = nilfs_prepare_super(sbi,
2427 nilfs_sb_will_flip(nilfs));
2428 if (likely(sbp)) {
2429 nilfs_set_log_cursor(sbp[0], nilfs);
2430 err = nilfs_commit_super(sbi, NILFS_SB_COMMIT);
2431 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002432 up_write(&nilfs->ns_sem);
2433 }
2434 }
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002435
2436 nilfs_segctor_notify(sci, mode, err);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002437 return err;
2438}
2439
2440static void nilfs_construction_timeout(unsigned long data)
2441{
2442 struct task_struct *p = (struct task_struct *)data;
2443 wake_up_process(p);
2444}
2445
2446static void
2447nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2448{
2449 struct nilfs_inode_info *ii, *n;
2450
2451 list_for_each_entry_safe(ii, n, head, i_dirty) {
2452 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2453 continue;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002454 list_del_init(&ii->i_dirty);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +09002455 iput(&ii->vfs_inode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002456 }
2457}
2458
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002459int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2460 void **kbufs)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002461{
2462 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2463 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2464 struct the_nilfs *nilfs = sbi->s_nilfs;
2465 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002466 int err;
2467
2468 if (unlikely(!sci))
2469 return -EROFS;
2470
2471 nilfs_transaction_lock(sbi, &ti, 1);
2472
2473 err = nilfs_init_gcdat_inode(nilfs);
2474 if (unlikely(err))
2475 goto out_unlock;
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002476
Ryusuke Konishi4f6b8282009-05-10 22:41:43 +09002477 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002478 if (unlikely(err))
2479 goto out_unlock;
2480
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002481 sci->sc_freesegs = kbufs[4];
2482 sci->sc_nfreesegs = argv[4].v_nmembs;
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002483 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002484
2485 for (;;) {
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002486 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002487 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002488
2489 if (likely(!err))
2490 break;
2491
2492 nilfs_warning(sb, __func__,
2493 "segment construction failed. (err=%d)", err);
2494 set_current_state(TASK_INTERRUPTIBLE);
2495 schedule_timeout(sci->sc_interval);
2496 }
Jiro SEKIBAe902ec92010-01-30 18:06:35 +09002497 if (nilfs_test_opt(sbi, DISCARD)) {
2498 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2499 sci->sc_nfreesegs);
2500 if (ret) {
2501 printk(KERN_WARNING
2502 "NILFS warning: error %d on discard request, "
2503 "turning discards off for the device\n", ret);
2504 nilfs_clear_opt(sbi, DISCARD);
2505 }
2506 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002507
2508 out_unlock:
Ryusuke Konishi071cb4b2009-05-16 23:44:55 +09002509 sci->sc_freesegs = NULL;
2510 sci->sc_nfreesegs = 0;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002511 nilfs_clear_gcdat_inode(nilfs);
2512 nilfs_transaction_unlock(sbi);
2513 return err;
2514}
2515
2516static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2517{
2518 struct nilfs_sb_info *sbi = sci->sc_sbi;
2519 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002520
2521 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002522 nilfs_segctor_construct(sci, mode);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002523
2524 /*
2525 * Unclosed segment should be retried. We do this using sc_timer.
2526 * Timeout of sc_timer will invoke complete construction which leads
2527 * to close the current logical segment.
2528 */
2529 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2530 nilfs_segctor_start_timer(sci);
2531
2532 nilfs_transaction_unlock(sbi);
2533}
2534
2535static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2536{
2537 int mode = 0;
2538 int err;
2539
2540 spin_lock(&sci->sc_state_lock);
2541 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2542 SC_FLUSH_DAT : SC_FLUSH_FILE;
2543 spin_unlock(&sci->sc_state_lock);
2544
2545 if (mode) {
2546 err = nilfs_segctor_do_construct(sci, mode);
2547
2548 spin_lock(&sci->sc_state_lock);
2549 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2550 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2551 spin_unlock(&sci->sc_state_lock);
2552 }
2553 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2554}
2555
2556static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2557{
2558 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2559 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2560 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2561 return SC_FLUSH_FILE;
2562 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2563 return SC_FLUSH_DAT;
2564 }
2565 return SC_LSEG_SR;
2566}
2567
2568/**
2569 * nilfs_segctor_thread - main loop of the segment constructor thread.
2570 * @arg: pointer to a struct nilfs_sc_info.
2571 *
2572 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2573 * to execute segment constructions.
2574 */
2575static int nilfs_segctor_thread(void *arg)
2576{
2577 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002578 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002579 int timeout = 0;
2580
Li Hongfdce8952010-04-10 23:25:39 +08002581 sci->sc_timer.data = (unsigned long)current;
2582 sci->sc_timer.function = nilfs_construction_timeout;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002583
2584 /* start sync. */
2585 sci->sc_task = current;
2586 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2587 printk(KERN_INFO
2588 "segctord starting. Construction interval = %lu seconds, "
2589 "CP frequency < %lu seconds\n",
2590 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2591
2592 spin_lock(&sci->sc_state_lock);
2593 loop:
2594 for (;;) {
2595 int mode;
2596
2597 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2598 goto end_thread;
2599
2600 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2601 mode = SC_LSEG_SR;
2602 else if (!sci->sc_flush_request)
2603 break;
2604 else
2605 mode = nilfs_segctor_flush_mode(sci);
2606
2607 spin_unlock(&sci->sc_state_lock);
2608 nilfs_segctor_thread_construct(sci, mode);
2609 spin_lock(&sci->sc_state_lock);
2610 timeout = 0;
2611 }
2612
2613
2614 if (freezing(current)) {
2615 spin_unlock(&sci->sc_state_lock);
2616 refrigerator();
2617 spin_lock(&sci->sc_state_lock);
2618 } else {
2619 DEFINE_WAIT(wait);
2620 int should_sleep = 1;
2621
2622 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2623 TASK_INTERRUPTIBLE);
2624
2625 if (sci->sc_seq_request != sci->sc_seq_done)
2626 should_sleep = 0;
2627 else if (sci->sc_flush_request)
2628 should_sleep = 0;
2629 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2630 should_sleep = time_before(jiffies,
Li Hongfdce8952010-04-10 23:25:39 +08002631 sci->sc_timer.expires);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002632
2633 if (should_sleep) {
2634 spin_unlock(&sci->sc_state_lock);
2635 schedule();
2636 spin_lock(&sci->sc_state_lock);
2637 }
2638 finish_wait(&sci->sc_wait_daemon, &wait);
2639 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
Li Hongfdce8952010-04-10 23:25:39 +08002640 time_after_eq(jiffies, sci->sc_timer.expires));
Ryusuke Konishie605f0a2009-12-09 00:57:52 +09002641
2642 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
Jiro SEKIBA1dfa2712009-07-23 01:33:49 +09002643 set_nilfs_discontinued(nilfs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002644 }
2645 goto loop;
2646
2647 end_thread:
2648 spin_unlock(&sci->sc_state_lock);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002649
2650 /* end sync. */
2651 sci->sc_task = NULL;
2652 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2653 return 0;
2654}
2655
2656static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2657{
2658 struct task_struct *t;
2659
2660 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2661 if (IS_ERR(t)) {
2662 int err = PTR_ERR(t);
2663
2664 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2665 err);
2666 return err;
2667 }
2668 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2669 return 0;
2670}
2671
2672static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2673{
2674 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2675
2676 while (sci->sc_task) {
2677 wake_up(&sci->sc_wait_daemon);
2678 spin_unlock(&sci->sc_state_lock);
2679 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2680 spin_lock(&sci->sc_state_lock);
2681 }
2682}
2683
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002684/*
2685 * Setup & clean-up functions
2686 */
2687static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
2688{
2689 struct nilfs_sc_info *sci;
2690
2691 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2692 if (!sci)
2693 return NULL;
2694
2695 sci->sc_sbi = sbi;
2696 sci->sc_super = sbi->s_super;
2697
2698 init_waitqueue_head(&sci->sc_wait_request);
2699 init_waitqueue_head(&sci->sc_wait_daemon);
2700 init_waitqueue_head(&sci->sc_wait_task);
2701 spin_lock_init(&sci->sc_state_lock);
2702 INIT_LIST_HEAD(&sci->sc_dirty_files);
2703 INIT_LIST_HEAD(&sci->sc_segbufs);
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002704 INIT_LIST_HEAD(&sci->sc_write_logs);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002705 INIT_LIST_HEAD(&sci->sc_gc_inodes);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002706 INIT_LIST_HEAD(&sci->sc_copied_buffers);
Li Hongfdce8952010-04-10 23:25:39 +08002707 init_timer(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002708
2709 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2710 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2711 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2712
2713 if (sbi->s_interval)
2714 sci->sc_interval = sbi->s_interval;
2715 if (sbi->s_watermark)
2716 sci->sc_watermark = sbi->s_watermark;
2717 return sci;
2718}
2719
2720static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2721{
2722 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2723
2724 /* The segctord thread was stopped and its timer was removed.
2725 But some tasks remain. */
2726 do {
2727 struct nilfs_sb_info *sbi = sci->sc_sbi;
2728 struct nilfs_transaction_info ti;
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002729
2730 nilfs_transaction_lock(sbi, &ti, 0);
Ryusuke Konishidcd76182010-01-26 15:20:15 +09002731 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002732 nilfs_transaction_unlock(sbi);
2733
2734 } while (ret && retrycount-- > 0);
2735}
2736
2737/**
2738 * nilfs_segctor_destroy - destroy the segment constructor.
2739 * @sci: nilfs_sc_info
2740 *
2741 * nilfs_segctor_destroy() kills the segctord thread and frees
2742 * the nilfs_sc_info struct.
2743 * Caller must hold the segment semaphore.
2744 */
2745static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2746{
2747 struct nilfs_sb_info *sbi = sci->sc_sbi;
2748 int flag;
2749
2750 up_write(&sbi->s_nilfs->ns_segctor_sem);
2751
2752 spin_lock(&sci->sc_state_lock);
2753 nilfs_segctor_kill_thread(sci);
2754 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2755 || sci->sc_seq_request != sci->sc_seq_done);
2756 spin_unlock(&sci->sc_state_lock);
2757
Ryusuke Konishi3256a052010-01-31 12:39:50 +09002758 if (flag || !nilfs_segctor_confirm(sci))
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002759 nilfs_segctor_write_out(sci);
2760
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002761 WARN_ON(!list_empty(&sci->sc_copied_buffers));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002762
2763 if (!list_empty(&sci->sc_dirty_files)) {
2764 nilfs_warning(sbi->s_super, __func__,
2765 "dirty file(s) after the final construction\n");
2766 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
2767 }
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002768
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002769 WARN_ON(!list_empty(&sci->sc_segbufs));
Ryusuke Konishia694291a2009-11-29 23:03:04 +09002770 WARN_ON(!list_empty(&sci->sc_write_logs));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002771
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002772 down_write(&sbi->s_nilfs->ns_segctor_sem);
2773
Li Hongfdce8952010-04-10 23:25:39 +08002774 del_timer_sync(&sci->sc_timer);
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002775 kfree(sci);
2776}
2777
2778/**
2779 * nilfs_attach_segment_constructor - attach a segment constructor
2780 * @sbi: nilfs_sb_info
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002781 *
2782 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
Ryusuke Konishi7a650042010-03-14 03:32:40 +09002783 * initializes it, and starts the segment constructor.
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002784 *
2785 * Return Value: On success, 0 is returned. On error, one of the following
2786 * negative error code is returned.
2787 *
2788 * %-ENOMEM - Insufficient memory available.
2789 */
Ryusuke Konishicece5522009-04-06 19:01:58 -07002790int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002791{
2792 struct the_nilfs *nilfs = sbi->s_nilfs;
2793 int err;
2794
Ryusuke Konishife5f1712010-01-31 19:46:40 +09002795 if (NILFS_SC(sbi)) {
2796 /*
2797 * This happens if the filesystem was remounted
2798 * read/write after nilfs_error degenerated it into a
2799 * read-only mount.
2800 */
2801 nilfs_detach_segment_constructor(sbi);
2802 }
2803
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002804 sbi->s_sc_info = nilfs_segctor_new(sbi);
2805 if (!sbi->s_sc_info)
2806 return -ENOMEM;
2807
2808 nilfs_attach_writer(nilfs, sbi);
Li Hong154ac5a2010-04-10 21:57:11 +08002809 err = nilfs_segctor_start_thread(NILFS_SC(sbi));
Ryusuke Konishi9ff051232009-04-06 19:01:37 -07002810 if (err) {
2811 nilfs_detach_writer(nilfs, sbi);
2812 kfree(sbi->s_sc_info);
2813 sbi->s_sc_info = NULL;
2814 }
2815 return err;
2816}
2817
2818/**
2819 * nilfs_detach_segment_constructor - destroy the segment constructor
2820 * @sbi: nilfs_sb_info
2821 *
2822 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2823 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2824 */
2825void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2826{
2827 struct the_nilfs *nilfs = sbi->s_nilfs;
2828 LIST_HEAD(garbage_list);
2829
2830 down_write(&nilfs->ns_segctor_sem);
2831 if (NILFS_SC(sbi)) {
2832 nilfs_segctor_destroy(NILFS_SC(sbi));
2833 sbi->s_sc_info = NULL;
2834 }
2835
2836 /* Force to free the list of dirty files */
2837 spin_lock(&sbi->s_inode_lock);
2838 if (!list_empty(&sbi->s_dirty_files)) {
2839 list_splice_init(&sbi->s_dirty_files, &garbage_list);
2840 nilfs_warning(sbi->s_super, __func__,
2841 "Non empty dirty list after the last "
2842 "segment construction\n");
2843 }
2844 spin_unlock(&sbi->s_inode_lock);
2845 up_write(&nilfs->ns_segctor_sem);
2846
2847 nilfs_dispose_list(sbi, &garbage_list, 1);
2848 nilfs_detach_writer(nilfs, sbi);
2849}