Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * segment.c - NILFS segment constructor. |
| 3 | * |
| 4 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | * |
| 20 | * Written by Ryusuke Konishi <ryusuke@osrg.net> |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/buffer_head.h> |
| 26 | #include <linux/writeback.h> |
| 27 | #include <linux/bio.h> |
| 28 | #include <linux/completion.h> |
| 29 | #include <linux/blkdev.h> |
| 30 | #include <linux/backing-dev.h> |
| 31 | #include <linux/freezer.h> |
| 32 | #include <linux/kthread.h> |
| 33 | #include <linux/crc32.h> |
| 34 | #include <linux/pagevec.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 36 | #include "nilfs.h" |
| 37 | #include "btnode.h" |
| 38 | #include "page.h" |
| 39 | #include "segment.h" |
| 40 | #include "sufile.h" |
| 41 | #include "cpfile.h" |
| 42 | #include "ifile.h" |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 43 | #include "segbuf.h" |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * Segment constructor |
| 48 | */ |
| 49 | #define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */ |
| 50 | |
| 51 | #define SC_MAX_SEGDELTA 64 /* Upper limit of the number of segments |
| 52 | appended in collection retry loop */ |
| 53 | |
| 54 | /* Construction mode */ |
| 55 | enum { |
| 56 | SC_LSEG_SR = 1, /* Make a logical segment having a super root */ |
| 57 | SC_LSEG_DSYNC, /* Flush data blocks of a given file and make |
| 58 | a logical segment without a super root */ |
| 59 | SC_FLUSH_FILE, /* Flush data files, leads to segment writes without |
| 60 | creating a checkpoint */ |
| 61 | SC_FLUSH_DAT, /* Flush DAT file. This also creates segments without |
| 62 | a checkpoint */ |
| 63 | }; |
| 64 | |
| 65 | /* Stage numbers of dirty block collection */ |
| 66 | enum { |
| 67 | NILFS_ST_INIT = 0, |
| 68 | NILFS_ST_GC, /* Collecting dirty blocks for GC */ |
| 69 | NILFS_ST_FILE, |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 70 | NILFS_ST_IFILE, |
| 71 | NILFS_ST_CPFILE, |
| 72 | NILFS_ST_SUFILE, |
| 73 | NILFS_ST_DAT, |
| 74 | NILFS_ST_SR, /* Super root */ |
| 75 | NILFS_ST_DSYNC, /* Data sync blocks */ |
| 76 | NILFS_ST_DONE, |
| 77 | }; |
| 78 | |
| 79 | /* State flags of collection */ |
| 80 | #define NILFS_CF_NODE 0x0001 /* Collecting node blocks */ |
| 81 | #define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */ |
Ryusuke Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 82 | #define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */ |
| 83 | #define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 84 | |
| 85 | /* Operations depending on the construction mode and file type */ |
| 86 | struct nilfs_sc_operations { |
| 87 | int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *, |
| 88 | struct inode *); |
| 89 | int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *, |
| 90 | struct inode *); |
| 91 | int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *, |
| 92 | struct inode *); |
| 93 | void (*write_data_binfo)(struct nilfs_sc_info *, |
| 94 | struct nilfs_segsum_pointer *, |
| 95 | union nilfs_binfo *); |
| 96 | void (*write_node_binfo)(struct nilfs_sc_info *, |
| 97 | struct nilfs_segsum_pointer *, |
| 98 | union nilfs_binfo *); |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * Other definitions |
| 103 | */ |
| 104 | static void nilfs_segctor_start_timer(struct nilfs_sc_info *); |
| 105 | static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int); |
| 106 | static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *); |
| 107 | static void nilfs_dispose_list(struct nilfs_sb_info *, struct list_head *, |
| 108 | int); |
| 109 | |
| 110 | #define nilfs_cnt32_gt(a, b) \ |
| 111 | (typecheck(__u32, a) && typecheck(__u32, b) && \ |
| 112 | ((__s32)(b) - (__s32)(a) < 0)) |
| 113 | #define nilfs_cnt32_ge(a, b) \ |
| 114 | (typecheck(__u32, a) && typecheck(__u32, b) && \ |
| 115 | ((__s32)(a) - (__s32)(b) >= 0)) |
| 116 | #define nilfs_cnt32_lt(a, b) nilfs_cnt32_gt(b, a) |
| 117 | #define nilfs_cnt32_le(a, b) nilfs_cnt32_ge(b, a) |
| 118 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 119 | static 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 Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 130 | * it is saved and will be restored on |
| 131 | * nilfs_transaction_commit(). |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 132 | */ |
| 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 Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 162 | * exclusive. The function is used with nilfs_transaction_commit() in pairs. |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 163 | * 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 Konishi | 7a65004 | 2010-03-14 03:32:40 +0900 | [diff] [blame] | 169 | * instead; otherwise a new struct is assigned from a slab. |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 170 | * |
| 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 179 | * %-ENOSPC - No space left on device |
| 180 | */ |
| 181 | int 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 Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 213 | * nilfs_transaction_commit - commit indivisible file operations. |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 214 | * @sb: super block |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 215 | * |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 216 | * 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 222 | */ |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 223 | int nilfs_transaction_commit(struct super_block *sb) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 224 | { |
| 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 Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 231 | ti->ti_flags |= NILFS_TI_COMMIT; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 232 | 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 Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 255 | void 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 271 | void 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 | |
| 295 | static 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 Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 301 | WARN_ON(cur_ti); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 302 | 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 | |
| 323 | static 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 | |
| 336 | static 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 | */ |
| 359 | static 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 Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 369 | err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 370 | 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 | |
| 381 | static 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 | |
| 391 | static 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 Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 402 | err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 403 | 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 | */ |
| 411 | static 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 | |
| 423 | static 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 Konishi | c96fa46 | 2009-04-06 19:01:57 -0700 | [diff] [blame] | 430 | |
| 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 433 | /* skip finfo */ |
| 434 | } |
| 435 | |
| 436 | static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci, |
| 437 | struct inode *inode) |
| 438 | { |
| 439 | struct nilfs_finfo *finfo; |
| 440 | struct nilfs_inode_info *ii; |
| 441 | struct nilfs_segment_buffer *segbuf; |
Ryusuke Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 442 | __u64 cno; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 443 | |
| 444 | if (sci->sc_blk_cnt == 0) |
| 445 | return; |
| 446 | |
| 447 | ii = NILFS_I(inode); |
Ryusuke Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 448 | |
| 449 | if (test_bit(NILFS_I_GCINODE, &ii->i_state)) |
| 450 | cno = ii->i_cno; |
| 451 | else if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) |
| 452 | cno = 0; |
| 453 | else |
| 454 | cno = sci->sc_cno; |
| 455 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 456 | finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr, |
| 457 | sizeof(*finfo)); |
| 458 | finfo->fi_ino = cpu_to_le64(inode->i_ino); |
| 459 | finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt); |
| 460 | finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt); |
Ryusuke Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 461 | finfo->fi_cno = cpu_to_le64(cno); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 462 | |
| 463 | segbuf = sci->sc_curseg; |
| 464 | segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset + |
| 465 | sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1); |
| 466 | sci->sc_finfo_ptr = sci->sc_binfo_ptr; |
| 467 | sci->sc_blk_cnt = sci->sc_datablk_cnt = 0; |
| 468 | } |
| 469 | |
| 470 | static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci, |
| 471 | struct buffer_head *bh, |
| 472 | struct inode *inode, |
| 473 | unsigned binfo_size) |
| 474 | { |
| 475 | struct nilfs_segment_buffer *segbuf; |
| 476 | int required, err = 0; |
| 477 | |
| 478 | retry: |
| 479 | segbuf = sci->sc_curseg; |
| 480 | required = nilfs_segctor_segsum_block_required( |
| 481 | sci, &sci->sc_binfo_ptr, binfo_size); |
| 482 | if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) { |
| 483 | nilfs_segctor_end_finfo(sci, inode); |
| 484 | err = nilfs_segctor_feed_segment(sci); |
| 485 | if (err) |
| 486 | return err; |
| 487 | goto retry; |
| 488 | } |
| 489 | if (unlikely(required)) { |
| 490 | err = nilfs_segbuf_extend_segsum(segbuf); |
| 491 | if (unlikely(err)) |
| 492 | goto failed; |
| 493 | } |
| 494 | if (sci->sc_blk_cnt == 0) |
| 495 | nilfs_segctor_begin_finfo(sci, inode); |
| 496 | |
| 497 | nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size); |
| 498 | /* Substitution to vblocknr is delayed until update_blocknr() */ |
| 499 | nilfs_segbuf_add_file_buffer(segbuf, bh); |
| 500 | sci->sc_blk_cnt++; |
| 501 | failed: |
| 502 | return err; |
| 503 | } |
| 504 | |
| 505 | static 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 | */ |
| 519 | static int nilfs_collect_file_data(struct nilfs_sc_info *sci, |
| 520 | struct buffer_head *bh, struct inode *inode) |
| 521 | { |
| 522 | int err; |
| 523 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 524 | 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 | |
| 536 | static 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 542 | 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 | |
| 549 | static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci, |
| 550 | struct buffer_head *bh, |
| 551 | struct inode *inode) |
| 552 | { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 553 | WARN_ON(!buffer_dirty(bh)); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 554 | return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64)); |
| 555 | } |
| 556 | |
| 557 | static 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 | |
| 566 | static 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 Konishi | 4e81950 | 2010-04-23 17:35:23 +0900 | [diff] [blame] | 575 | static struct nilfs_sc_operations nilfs_sc_file_ops = { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 576 | .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 | |
| 583 | static 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 | |
| 599 | static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci, |
| 600 | struct buffer_head *bh, struct inode *inode) |
| 601 | { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 602 | WARN_ON(!buffer_dirty(bh)); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 603 | return nilfs_segctor_add_file_block(sci, bh, inode, |
| 604 | sizeof(struct nilfs_binfo_dat)); |
| 605 | } |
| 606 | |
| 607 | static 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 | |
| 616 | static 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 Konishi | 4e81950 | 2010-04-23 17:35:23 +0900 | [diff] [blame] | 625 | static struct nilfs_sc_operations nilfs_sc_dat_ops = { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 626 | .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 Konishi | 4e81950 | 2010-04-23 17:35:23 +0900 | [diff] [blame] | 633 | static struct nilfs_sc_operations nilfs_sc_dsync_ops = { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 634 | .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 Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 641 | static 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 645 | { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 646 | struct address_space *mapping = inode->i_mapping; |
| 647 | struct pagevec pvec; |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 648 | pgoff_t index = 0, last = ULONG_MAX; |
| 649 | size_t ndirties = 0; |
| 650 | int i; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 651 | |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 652 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 661 | pagevec_init(&pvec, 0); |
| 662 | repeat: |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 663 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 668 | |
| 669 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 670 | struct buffer_head *bh, *head; |
| 671 | struct page *page = pvec.pages[i]; |
| 672 | |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 673 | if (unlikely(page->index > last)) |
| 674 | break; |
| 675 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 676 | 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 Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 686 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 695 | } |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 696 | } while (bh = bh->b_this_page, bh != head); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 697 | } |
| 698 | pagevec_release(&pvec); |
| 699 | cond_resched(); |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 700 | goto repeat; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static 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 | |
| 733 | static 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 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 766 | static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs, |
| 767 | struct nilfs_root *root) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 768 | { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 769 | int ret = 0; |
| 770 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 771 | if (nilfs_mdt_fetch_dirty(root->ifile)) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 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 | |
| 783 | static 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 Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 787 | sci->sc_nfreesegs == 0 && |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 788 | (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes)); |
| 789 | } |
| 790 | |
| 791 | static int nilfs_segctor_confirm(struct nilfs_sc_info *sci) |
| 792 | { |
| 793 | struct nilfs_sb_info *sbi = sci->sc_sbi; |
| 794 | int ret = 0; |
| 795 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 796 | if (nilfs_test_metadata_dirty(sbi->s_nilfs, sci->sc_root)) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 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 | |
| 807 | static 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 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 812 | nilfs_mdt_clear_dirty(sci->sc_root->ifile); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 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 | |
| 818 | static 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 Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 836 | } else |
| 837 | WARN_ON(err == -EINVAL || err == -ENOENT); |
| 838 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 839 | return err; |
| 840 | } |
| 841 | |
| 842 | static 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 Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 853 | WARN_ON(err == -EINVAL || err == -ENOENT); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 854 | 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 Konishi | 458c5b0 | 2009-04-06 19:01:56 -0700 | [diff] [blame] | 866 | |
Ryusuke Konishi | c96fa46 | 2009-04-06 19:01:57 -0700 | [diff] [blame] | 867 | 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 Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 872 | nilfs_write_inode_common(sci->sc_root->ifile, |
| 873 | &raw_cp->cp_ifile_inode, 1); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 874 | nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp); |
| 875 | return 0; |
| 876 | |
| 877 | failed_ibh: |
| 878 | return err; |
| 879 | } |
| 880 | |
| 881 | static void nilfs_fill_in_file_bmap(struct inode *ifile, |
| 882 | struct nilfs_inode_info *ii) |
| 883 | |
| 884 | { |
| 885 | struct buffer_head *ibh; |
| 886 | struct nilfs_inode *raw_inode; |
| 887 | |
| 888 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) { |
| 889 | ibh = ii->i_bh; |
| 890 | BUG_ON(!ibh); |
| 891 | raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino, |
| 892 | ibh); |
| 893 | nilfs_bmap_write(ii->i_bmap, raw_inode); |
| 894 | nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh); |
| 895 | } |
| 896 | } |
| 897 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 898 | static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 899 | { |
| 900 | struct nilfs_inode_info *ii; |
| 901 | |
| 902 | list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) { |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 903 | nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 904 | set_bit(NILFS_I_COLLECTED, &ii->i_state); |
| 905 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 908 | static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci, |
| 909 | struct the_nilfs *nilfs) |
| 910 | { |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 911 | struct buffer_head *bh_sr; |
| 912 | struct nilfs_super_root *raw_sr; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 913 | unsigned isz = nilfs->ns_inode_size; |
| 914 | |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 915 | bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root; |
| 916 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; |
| 917 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 918 | 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 Konishi | 3961f0e | 2009-11-13 01:55:02 +0900 | [diff] [blame] | 924 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static 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 | |
| 942 | static 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 955 | static 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 Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 986 | static 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 993 | static 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 Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 999 | int err; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (!(sci->sc_stage.flags & NILFS_CF_NODE)) { |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 1002 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1008 | sci, inode, &data_buffers, |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 1009 | sc_ops->collect_data); |
| 1010 | BUG_ON(!err); /* always receive -E2BIG or true error */ |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1011 | 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 | |
| 1046 | static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci, |
| 1047 | struct inode *inode) |
| 1048 | { |
| 1049 | LIST_HEAD(data_buffers); |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 1050 | size_t n, rest = nilfs_segctor_buffer_rest(sci); |
| 1051 | int err; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1052 | |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 1053 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1060 | nilfs_segctor_end_finfo(sci, inode); |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 1061 | BUG_ON(n > rest); |
| 1062 | /* always receive -E2BIG or true error if n > rest */ |
| 1063 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1064 | return err; |
| 1065 | } |
| 1066 | |
| 1067 | static 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 Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 1073 | size_t ndone; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1074 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1142 | sci->sc_stage.scnt++; |
| 1143 | sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED; |
| 1144 | /* Fall through */ |
| 1145 | case NILFS_ST_IFILE: |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 1146 | err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile, |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 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 Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 1163 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1169 | break; |
Ryusuke Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 1170 | } |
| 1171 | sci->sc_stage.flags |= NILFS_CF_SUFREED; |
| 1172 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1173 | 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 Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 1203 | ii = sci->sc_dsync_inode; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1204 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1210 | 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 Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1223 | /** |
| 1224 | * nilfs_segctor_begin_construction - setup segment buffer to make a new log |
| 1225 | * @sci: nilfs_sc_info |
| 1226 | * @nilfs: nilfs object |
| 1227 | */ |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1228 | static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci, |
| 1229 | struct the_nilfs *nilfs) |
| 1230 | { |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1231 | struct nilfs_segment_buffer *segbuf, *prev; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1232 | __u64 nextnum; |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1233 | int err, alloc = 0; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1234 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1235 | segbuf = nilfs_segbuf_new(sci->sc_super); |
| 1236 | if (unlikely(!segbuf)) |
| 1237 | return -ENOMEM; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1238 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1239 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1246 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1247 | segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1248 | nextnum = nilfs->ns_nextnum; |
| 1249 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1250 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1276 | nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs); |
| 1277 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1278 | 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 Konishi | cece552 | 2009-04-06 19:01:58 -0700 | [diff] [blame] | 1281 | return 0; |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1282 | |
| 1283 | failed: |
| 1284 | nilfs_segbuf_free(segbuf); |
| 1285 | return err; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci, |
| 1289 | struct the_nilfs *nilfs, int nadd) |
| 1290 | { |
Ryusuke Konishi | e29df39 | 2009-11-29 16:51:16 +0900 | [diff] [blame] | 1291 | struct nilfs_segment_buffer *segbuf, *prev; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1292 | 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 Konishi | 61a189e | 2009-11-18 17:25:12 +0900 | [diff] [blame] | 1304 | err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1305 | 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 Konishi | cece552 | 2009-04-06 19:01:58 -0700 | [diff] [blame] | 1316 | nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1317 | 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 Konishi | 0935db7 | 2009-11-29 02:39:11 +0900 | [diff] [blame] | 1330 | list_splice_tail(&list, &sci->sc_segbufs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1331 | return 0; |
| 1332 | |
| 1333 | failed_segbuf: |
| 1334 | nilfs_segbuf_free(segbuf); |
| 1335 | failed: |
Ryusuke Konishi | e29df39 | 2009-11-29 16:51:16 +0900 | [diff] [blame] | 1336 | list_for_each_entry(segbuf, &list, sb_list) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1337 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1338 | WARN_ON(ret); /* never fails */ |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1339 | } |
Ryusuke Konishi | e29df39 | 2009-11-29 16:51:16 +0900 | [diff] [blame] | 1340 | nilfs_destroy_logs(&list); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1341 | return err; |
| 1342 | } |
| 1343 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1344 | static void nilfs_free_incomplete_logs(struct list_head *logs, |
| 1345 | struct the_nilfs *nilfs) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1346 | { |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1347 | struct nilfs_segment_buffer *segbuf, *prev; |
| 1348 | struct inode *sufile = nilfs->ns_sufile; |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame] | 1349 | int ret; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1350 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1351 | segbuf = NILFS_FIRST_SEGBUF(logs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1352 | if (nilfs->ns_nextnum != segbuf->sb_nextnum) { |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1353 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1354 | WARN_ON(ret); /* never fails */ |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1355 | } |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame] | 1356 | if (atomic_read(&segbuf->sb_err)) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1357 | /* 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1367 | 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 Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame] | 1373 | if (atomic_read(&segbuf->sb_err) && |
| 1374 | segbuf->sb_segnum != nilfs->ns_nextnum) |
| 1375 | /* Case 2: extended segment (!= next) failed */ |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1376 | nilfs_sufile_set_error(sufile, segbuf->sb_segnum); |
| 1377 | prev = segbuf; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1381 | static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci, |
| 1382 | struct inode *sufile) |
| 1383 | { |
| 1384 | struct nilfs_segment_buffer *segbuf; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1385 | unsigned long live_blocks; |
| 1386 | int ret; |
| 1387 | |
| 1388 | list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1389 | live_blocks = segbuf->sb_sum.nblocks + |
| 1390 | (segbuf->sb_pseg_start - segbuf->sb_fseg_start); |
Ryusuke Konishi | 071ec54 | 2009-11-18 18:23:34 +0900 | [diff] [blame] | 1391 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1398 | static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1399 | { |
| 1400 | struct nilfs_segment_buffer *segbuf; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1401 | int ret; |
| 1402 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1403 | segbuf = NILFS_FIRST_SEGBUF(logs); |
Ryusuke Konishi | 071ec54 | 2009-11-18 18:23:34 +0900 | [diff] [blame] | 1404 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1408 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1409 | list_for_each_entry_continue(segbuf, logs, sb_list) { |
Ryusuke Konishi | 071ec54 | 2009-11-18 18:23:34 +0900 | [diff] [blame] | 1410 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, |
| 1411 | 0, 0); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1412 | WARN_ON(ret); /* always succeed */ |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci, |
| 1417 | struct nilfs_segment_buffer *last, |
| 1418 | struct inode *sufile) |
| 1419 | { |
Ryusuke Konishi | e29df39 | 2009-11-29 16:51:16 +0900 | [diff] [blame] | 1420 | struct nilfs_segment_buffer *segbuf = last; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1421 | int ret; |
| 1422 | |
Ryusuke Konishi | e29df39 | 2009-11-29 16:51:16 +0900 | [diff] [blame] | 1423 | list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1424 | sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks; |
| 1425 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1426 | WARN_ON(ret); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1427 | } |
Ryusuke Konishi | e29df39 | 2009-11-29 16:51:16 +0900 | [diff] [blame] | 1428 | nilfs_truncate_logs(&sci->sc_segbufs, last); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | static 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1440 | 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 Konishi | 2d8428a | 2010-03-22 14:01:24 +0900 | [diff] [blame] | 1459 | 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 Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 1465 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1472 | 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 | |
| 1482 | static 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 | |
| 1491 | static int |
| 1492 | nilfs_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 Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1516 | if (bh == segbuf->sb_super_root) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1517 | 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 | |
| 1568 | static 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 | |
| 1582 | static int |
| 1583 | nilfs_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 | |
| 1616 | static 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 | |
| 1630 | static 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 | |
| 1650 | static 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 Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1677 | if (bh == segbuf->sb_super_root) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1678 | 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 | |
| 1710 | static int nilfs_segctor_write(struct nilfs_sc_info *sci, |
Ryusuke Konishi | 9c965ba | 2009-11-29 01:17:31 +0900 | [diff] [blame] | 1711 | struct the_nilfs *nilfs) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1712 | { |
Ryusuke Konishi | d1c6b72 | 2009-12-17 00:55:40 +0900 | [diff] [blame] | 1713 | int ret; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1714 | |
Ryusuke Konishi | d1c6b72 | 2009-12-17 00:55:40 +0900 | [diff] [blame] | 1715 | ret = nilfs_write_logs(&sci->sc_segbufs, nilfs); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1716 | list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs); |
| 1717 | return ret; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1720 | static void __nilfs_end_page_io(struct page *page, int err) |
| 1721 | { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1722 | 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 | |
| 1738 | static void nilfs_end_page_io(struct page *page, int err) |
| 1739 | { |
| 1740 | if (!page) |
| 1741 | return; |
| 1742 | |
Ryusuke Konishi | a977784 | 2009-07-28 17:55:29 +0900 | [diff] [blame] | 1743 | if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) { |
Ryusuke Konishi | 8227b29 | 2009-06-18 23:52:23 +0900 | [diff] [blame] | 1744 | /* |
| 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 Konishi | a977784 | 2009-07-28 17:55:29 +0900 | [diff] [blame] | 1748 | 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1761 | return; |
Ryusuke Konishi | a977784 | 2009-07-28 17:55:29 +0900 | [diff] [blame] | 1762 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1763 | |
| 1764 | __nilfs_end_page_io(page, err); |
| 1765 | } |
| 1766 | |
| 1767 | static 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 Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1795 | static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page, |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1796 | int err) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1797 | { |
| 1798 | struct nilfs_segment_buffer *segbuf; |
| 1799 | struct page *bd_page = NULL, *fs_page = NULL; |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1800 | struct buffer_head *bh; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1801 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1802 | if (list_empty(logs)) |
| 1803 | return; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1804 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1805 | list_for_each_entry(segbuf, logs, sb_list) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1806 | 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 Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1817 | if (bh == segbuf->sb_super_root) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1818 | 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 Konishi | 8227b29 | 2009-06-18 23:52:23 +0900 | [diff] [blame] | 1826 | if (fs_page && fs_page == failed_page) |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1827 | return; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1828 | 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 Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | static 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 Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1846 | nilfs_abort_logs(&logs, NULL, ret ? : err); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1847 | |
| 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1851 | nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1852 | |
| 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | static 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 | |
| 1875 | static 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 Konishi | e605f0a | 2009-12-09 00:57:52 +0900 | [diff] [blame] | 1879 | struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs; |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1880 | int update_sr = false; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1881 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1882 | list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1883 | 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 Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1911 | if (bh == segbuf->sb_super_root) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1912 | if (bh->b_page != bd_page) { |
| 1913 | end_page_writeback(bd_page); |
| 1914 | bd_page = bh->b_page; |
| 1915 | } |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 1916 | update_sr = true; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1917 | 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 Konishi | 4762077 | 2010-05-23 21:48:36 +0900 | [diff] [blame] | 1925 | if (!nilfs_segbuf_simplex(segbuf)) { |
| 1926 | if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1927 | set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags); |
| 1928 | sci->sc_lseg_stime = jiffies; |
| 1929 | } |
Ryusuke Konishi | 4762077 | 2010-05-23 21:48:36 +0900 | [diff] [blame] | 1930 | if (segbuf->sb_sum.flags & NILFS_SS_LOGEND) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1931 | 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 Konishi | 1088dcf | 2009-04-06 19:01:51 -0700 | [diff] [blame] | 1951 | } else |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1952 | nilfs->ns_nongc_ctime = sci->sc_seg_ctime; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1953 | |
| 1954 | sci->sc_nblk_inc += sci->sc_nblk_this_inc; |
| 1955 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1956 | segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1957 | nilfs_set_next_segment(nilfs, segbuf); |
| 1958 | |
| 1959 | if (update_sr) { |
| 1960 | nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start, |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 1961 | segbuf->sb_sum.seg_seq, nilfs->ns_cno++); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1962 | |
Ryusuke Konishi | c96fa46 | 2009-04-06 19:01:57 -0700 | [diff] [blame] | 1963 | clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1964 | clear_bit(NILFS_SC_DIRTY, &sci->sc_flags); |
| 1965 | set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1966 | nilfs_segctor_clear_metadata_dirty(sci); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1967 | } else |
| 1968 | clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags); |
| 1969 | } |
| 1970 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 1971 | static 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 Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1983 | static 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 Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 1987 | struct inode *ifile = sci->sc_root->ifile; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1988 | |
| 1989 | spin_lock(&sbi->s_inode_lock); |
| 1990 | retry: |
| 1991 | list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) { |
| 1992 | if (!ii->i_bh) { |
| 1993 | struct buffer_head *ibh; |
| 1994 | int err; |
| 1995 | |
| 1996 | spin_unlock(&sbi->s_inode_lock); |
| 1997 | err = nilfs_ifile_get_inode_block( |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 1998 | ifile, ii->vfs_inode.i_ino, &ibh); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 1999 | if (unlikely(err)) { |
| 2000 | nilfs_warning(sbi->s_super, __func__, |
| 2001 | "failed to get inode block.\n"); |
| 2002 | return err; |
| 2003 | } |
| 2004 | nilfs_mdt_mark_buffer_dirty(ibh); |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2005 | nilfs_mdt_mark_dirty(ifile); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2006 | spin_lock(&sbi->s_inode_lock); |
| 2007 | if (likely(!ii->i_bh)) |
| 2008 | ii->i_bh = ibh; |
| 2009 | else |
| 2010 | brelse(ibh); |
| 2011 | goto retry; |
| 2012 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2013 | |
| 2014 | clear_bit(NILFS_I_QUEUED, &ii->i_state); |
| 2015 | set_bit(NILFS_I_BUSY, &ii->i_state); |
| 2016 | list_del(&ii->i_dirty); |
| 2017 | list_add_tail(&ii->i_dirty, &sci->sc_dirty_files); |
| 2018 | } |
| 2019 | spin_unlock(&sbi->s_inode_lock); |
| 2020 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci, |
| 2025 | struct nilfs_sb_info *sbi) |
| 2026 | { |
| 2027 | struct nilfs_transaction_info *ti = current->journal_info; |
| 2028 | struct nilfs_inode_info *ii, *n; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2029 | |
| 2030 | spin_lock(&sbi->s_inode_lock); |
| 2031 | list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) { |
| 2032 | if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) || |
Ryusuke Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 2033 | test_bit(NILFS_I_DIRTY, &ii->i_state)) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2034 | continue; |
Ryusuke Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 2035 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2036 | clear_bit(NILFS_I_BUSY, &ii->i_state); |
| 2037 | brelse(ii->i_bh); |
| 2038 | ii->i_bh = NULL; |
| 2039 | list_del(&ii->i_dirty); |
| 2040 | list_add_tail(&ii->i_dirty, &ti->ti_garbage); |
| 2041 | } |
| 2042 | spin_unlock(&sbi->s_inode_lock); |
| 2043 | } |
| 2044 | |
| 2045 | /* |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2046 | * Main procedure of segment constructor |
| 2047 | */ |
| 2048 | static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode) |
| 2049 | { |
| 2050 | struct nilfs_sb_info *sbi = sci->sc_sbi; |
| 2051 | struct the_nilfs *nilfs = sbi->s_nilfs; |
| 2052 | struct page *failed_page; |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 2053 | int err; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2054 | |
| 2055 | sci->sc_stage.scnt = NILFS_ST_INIT; |
Ryusuke Konishi | 6c43f41 | 2010-08-20 20:10:38 +0900 | [diff] [blame] | 2056 | sci->sc_cno = nilfs->ns_cno; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2057 | |
| 2058 | err = nilfs_segctor_check_in_files(sci, sbi); |
| 2059 | if (unlikely(err)) |
| 2060 | goto out; |
| 2061 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2062 | if (nilfs_test_metadata_dirty(nilfs, sci->sc_root)) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2063 | set_bit(NILFS_SC_DIRTY, &sci->sc_flags); |
| 2064 | |
| 2065 | if (nilfs_segctor_clean(sci)) |
| 2066 | goto out; |
| 2067 | |
| 2068 | do { |
| 2069 | sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK; |
| 2070 | |
| 2071 | err = nilfs_segctor_begin_construction(sci, nilfs); |
| 2072 | if (unlikely(err)) |
| 2073 | goto out; |
| 2074 | |
| 2075 | /* Update time stamp */ |
| 2076 | sci->sc_seg_ctime = get_seconds(); |
| 2077 | |
| 2078 | err = nilfs_segctor_collect(sci, nilfs, mode); |
| 2079 | if (unlikely(err)) |
| 2080 | goto failed; |
| 2081 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2082 | /* Avoid empty segment */ |
| 2083 | if (sci->sc_stage.scnt == NILFS_ST_DONE && |
Ryusuke Konishi | 4762077 | 2010-05-23 21:48:36 +0900 | [diff] [blame] | 2084 | nilfs_segbuf_empty(sci->sc_curseg)) { |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2085 | nilfs_segctor_abort_construction(sci, nilfs, 1); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2086 | goto out; |
| 2087 | } |
| 2088 | |
| 2089 | err = nilfs_segctor_assign(sci, mode); |
| 2090 | if (unlikely(err)) |
| 2091 | goto failed; |
| 2092 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2093 | if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED) |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2094 | nilfs_segctor_fill_in_file_bmap(sci); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2095 | |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 2096 | if (mode == SC_LSEG_SR && |
| 2097 | sci->sc_stage.scnt >= NILFS_ST_CPFILE) { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2098 | err = nilfs_segctor_fill_in_checkpoint(sci); |
| 2099 | if (unlikely(err)) |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2100 | goto failed_to_write; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2101 | |
| 2102 | nilfs_segctor_fill_in_super_root(sci, nilfs); |
| 2103 | } |
| 2104 | nilfs_segctor_update_segusage(sci, nilfs->ns_sufile); |
| 2105 | |
| 2106 | /* Write partial segments */ |
| 2107 | err = nilfs_segctor_prepare_write(sci, &failed_page); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2108 | if (err) { |
Ryusuke Konishi | 1e2b68b | 2010-03-23 01:15:31 +0900 | [diff] [blame] | 2109 | nilfs_abort_logs(&sci->sc_segbufs, failed_page, err); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2110 | goto failed_to_write; |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2111 | } |
Ryusuke Konishi | aaed1d5 | 2010-03-23 01:50:38 +0900 | [diff] [blame] | 2112 | |
| 2113 | nilfs_add_checksums_on_logs(&sci->sc_segbufs, |
| 2114 | nilfs->ns_crc_seed); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2115 | |
Ryusuke Konishi | 9c965ba | 2009-11-29 01:17:31 +0900 | [diff] [blame] | 2116 | err = nilfs_segctor_write(sci, nilfs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2117 | if (unlikely(err)) |
| 2118 | goto failed_to_write; |
| 2119 | |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2120 | if (sci->sc_stage.scnt == NILFS_ST_DONE || |
| 2121 | nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) { |
| 2122 | /* |
| 2123 | * At this point, we avoid double buffering |
| 2124 | * for blocksize < pagesize because page dirty |
| 2125 | * flag is turned off during write and dirty |
| 2126 | * buffers are not properly collected for |
| 2127 | * pages crossing over segments. |
| 2128 | */ |
| 2129 | err = nilfs_segctor_wait(sci); |
| 2130 | if (err) |
| 2131 | goto failed_to_write; |
| 2132 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2133 | } while (sci->sc_stage.scnt != NILFS_ST_DONE); |
| 2134 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2135 | out: |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2136 | nilfs_segctor_check_out_files(sci, sbi); |
| 2137 | return err; |
| 2138 | |
| 2139 | failed_to_write: |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2140 | if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED) |
| 2141 | nilfs_redirty_inodes(&sci->sc_dirty_files); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2142 | |
| 2143 | failed: |
| 2144 | if (nilfs_doing_gc()) |
| 2145 | nilfs_redirty_inodes(&sci->sc_gc_inodes); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2146 | nilfs_segctor_abort_construction(sci, nilfs, err); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2147 | goto out; |
| 2148 | } |
| 2149 | |
| 2150 | /** |
Ryusuke Konishi | 9ccf56c | 2010-03-14 03:01:03 +0900 | [diff] [blame] | 2151 | * nilfs_segctor_start_timer - set timer of background write |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2152 | * @sci: nilfs_sc_info |
| 2153 | * |
| 2154 | * If the timer has already been set, it ignores the new request. |
| 2155 | * This function MUST be called within a section locking the segment |
| 2156 | * semaphore. |
| 2157 | */ |
| 2158 | static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci) |
| 2159 | { |
| 2160 | spin_lock(&sci->sc_state_lock); |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2161 | if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) { |
| 2162 | sci->sc_timer.expires = jiffies + sci->sc_interval; |
| 2163 | add_timer(&sci->sc_timer); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2164 | sci->sc_state |= NILFS_SEGCTOR_COMMIT; |
| 2165 | } |
| 2166 | spin_unlock(&sci->sc_state_lock); |
| 2167 | } |
| 2168 | |
| 2169 | static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn) |
| 2170 | { |
| 2171 | spin_lock(&sci->sc_state_lock); |
| 2172 | if (!(sci->sc_flush_request & (1 << bn))) { |
| 2173 | unsigned long prev_req = sci->sc_flush_request; |
| 2174 | |
| 2175 | sci->sc_flush_request |= (1 << bn); |
| 2176 | if (!prev_req) |
| 2177 | wake_up(&sci->sc_wait_daemon); |
| 2178 | } |
| 2179 | spin_unlock(&sci->sc_state_lock); |
| 2180 | } |
| 2181 | |
| 2182 | /** |
| 2183 | * nilfs_flush_segment - trigger a segment construction for resource control |
| 2184 | * @sb: super block |
| 2185 | * @ino: inode number of the file to be flushed out. |
| 2186 | */ |
| 2187 | void nilfs_flush_segment(struct super_block *sb, ino_t ino) |
| 2188 | { |
| 2189 | struct nilfs_sb_info *sbi = NILFS_SB(sb); |
| 2190 | struct nilfs_sc_info *sci = NILFS_SC(sbi); |
| 2191 | |
| 2192 | if (!sci || nilfs_doing_construction()) |
| 2193 | return; |
| 2194 | nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0); |
| 2195 | /* assign bit 0 to data files */ |
| 2196 | } |
| 2197 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2198 | struct nilfs_segctor_wait_request { |
| 2199 | wait_queue_t wq; |
| 2200 | __u32 seq; |
| 2201 | int err; |
| 2202 | atomic_t done; |
| 2203 | }; |
| 2204 | |
| 2205 | static int nilfs_segctor_sync(struct nilfs_sc_info *sci) |
| 2206 | { |
| 2207 | struct nilfs_segctor_wait_request wait_req; |
| 2208 | int err = 0; |
| 2209 | |
| 2210 | spin_lock(&sci->sc_state_lock); |
| 2211 | init_wait(&wait_req.wq); |
| 2212 | wait_req.err = 0; |
| 2213 | atomic_set(&wait_req.done, 0); |
| 2214 | wait_req.seq = ++sci->sc_seq_request; |
| 2215 | spin_unlock(&sci->sc_state_lock); |
| 2216 | |
| 2217 | init_waitqueue_entry(&wait_req.wq, current); |
| 2218 | add_wait_queue(&sci->sc_wait_request, &wait_req.wq); |
| 2219 | set_current_state(TASK_INTERRUPTIBLE); |
| 2220 | wake_up(&sci->sc_wait_daemon); |
| 2221 | |
| 2222 | for (;;) { |
| 2223 | if (atomic_read(&wait_req.done)) { |
| 2224 | err = wait_req.err; |
| 2225 | break; |
| 2226 | } |
| 2227 | if (!signal_pending(current)) { |
| 2228 | schedule(); |
| 2229 | continue; |
| 2230 | } |
| 2231 | err = -ERESTARTSYS; |
| 2232 | break; |
| 2233 | } |
| 2234 | finish_wait(&sci->sc_wait_request, &wait_req.wq); |
| 2235 | return err; |
| 2236 | } |
| 2237 | |
| 2238 | static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err) |
| 2239 | { |
| 2240 | struct nilfs_segctor_wait_request *wrq, *n; |
| 2241 | unsigned long flags; |
| 2242 | |
| 2243 | spin_lock_irqsave(&sci->sc_wait_request.lock, flags); |
| 2244 | list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list, |
| 2245 | wq.task_list) { |
| 2246 | if (!atomic_read(&wrq->done) && |
| 2247 | nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) { |
| 2248 | wrq->err = err; |
| 2249 | atomic_set(&wrq->done, 1); |
| 2250 | } |
| 2251 | if (atomic_read(&wrq->done)) { |
| 2252 | wrq->wq.func(&wrq->wq, |
| 2253 | TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 2254 | 0, NULL); |
| 2255 | } |
| 2256 | } |
| 2257 | spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags); |
| 2258 | } |
| 2259 | |
| 2260 | /** |
| 2261 | * nilfs_construct_segment - construct a logical segment |
| 2262 | * @sb: super block |
| 2263 | * |
| 2264 | * Return Value: On success, 0 is retured. On errors, one of the following |
| 2265 | * negative error code is returned. |
| 2266 | * |
| 2267 | * %-EROFS - Read only filesystem. |
| 2268 | * |
| 2269 | * %-EIO - I/O error |
| 2270 | * |
| 2271 | * %-ENOSPC - No space left on device (only in a panic state). |
| 2272 | * |
| 2273 | * %-ERESTARTSYS - Interrupted. |
| 2274 | * |
| 2275 | * %-ENOMEM - Insufficient memory available. |
| 2276 | */ |
| 2277 | int nilfs_construct_segment(struct super_block *sb) |
| 2278 | { |
| 2279 | struct nilfs_sb_info *sbi = NILFS_SB(sb); |
| 2280 | struct nilfs_sc_info *sci = NILFS_SC(sbi); |
| 2281 | struct nilfs_transaction_info *ti; |
| 2282 | int err; |
| 2283 | |
| 2284 | if (!sci) |
| 2285 | return -EROFS; |
| 2286 | |
| 2287 | /* A call inside transactions causes a deadlock. */ |
| 2288 | BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC); |
| 2289 | |
| 2290 | err = nilfs_segctor_sync(sci); |
| 2291 | return err; |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * nilfs_construct_dsync_segment - construct a data-only logical segment |
| 2296 | * @sb: super block |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 2297 | * @inode: inode whose data blocks should be written out |
| 2298 | * @start: start byte offset |
| 2299 | * @end: end byte offset (inclusive) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2300 | * |
| 2301 | * Return Value: On success, 0 is retured. On errors, one of the following |
| 2302 | * negative error code is returned. |
| 2303 | * |
| 2304 | * %-EROFS - Read only filesystem. |
| 2305 | * |
| 2306 | * %-EIO - I/O error |
| 2307 | * |
| 2308 | * %-ENOSPC - No space left on device (only in a panic state). |
| 2309 | * |
| 2310 | * %-ERESTARTSYS - Interrupted. |
| 2311 | * |
| 2312 | * %-ENOMEM - Insufficient memory available. |
| 2313 | */ |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 2314 | int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode, |
| 2315 | loff_t start, loff_t end) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2316 | { |
| 2317 | struct nilfs_sb_info *sbi = NILFS_SB(sb); |
| 2318 | struct nilfs_sc_info *sci = NILFS_SC(sbi); |
| 2319 | struct nilfs_inode_info *ii; |
| 2320 | struct nilfs_transaction_info ti; |
| 2321 | int err = 0; |
| 2322 | |
| 2323 | if (!sci) |
| 2324 | return -EROFS; |
| 2325 | |
| 2326 | nilfs_transaction_lock(sbi, &ti, 0); |
| 2327 | |
| 2328 | ii = NILFS_I(inode); |
| 2329 | if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) || |
| 2330 | nilfs_test_opt(sbi, STRICT_ORDER) || |
| 2331 | test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) || |
| 2332 | nilfs_discontinued(sbi->s_nilfs)) { |
| 2333 | nilfs_transaction_unlock(sbi); |
| 2334 | err = nilfs_segctor_sync(sci); |
| 2335 | return err; |
| 2336 | } |
| 2337 | |
| 2338 | spin_lock(&sbi->s_inode_lock); |
| 2339 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && |
| 2340 | !test_bit(NILFS_I_BUSY, &ii->i_state)) { |
| 2341 | spin_unlock(&sbi->s_inode_lock); |
| 2342 | nilfs_transaction_unlock(sbi); |
| 2343 | return 0; |
| 2344 | } |
| 2345 | spin_unlock(&sbi->s_inode_lock); |
Ryusuke Konishi | f30bf3e | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 2346 | sci->sc_dsync_inode = ii; |
| 2347 | sci->sc_dsync_start = start; |
| 2348 | sci->sc_dsync_end = end; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2349 | |
| 2350 | err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC); |
| 2351 | |
| 2352 | nilfs_transaction_unlock(sbi); |
| 2353 | return err; |
| 2354 | } |
| 2355 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2356 | #define FLUSH_FILE_BIT (0x1) /* data file only */ |
| 2357 | #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */ |
| 2358 | |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2359 | /** |
| 2360 | * nilfs_segctor_accept - record accepted sequence count of log-write requests |
| 2361 | * @sci: segment constructor object |
| 2362 | */ |
| 2363 | static void nilfs_segctor_accept(struct nilfs_sc_info *sci) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2364 | { |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2365 | spin_lock(&sci->sc_state_lock); |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2366 | sci->sc_seq_accepted = sci->sc_seq_request; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2367 | spin_unlock(&sci->sc_state_lock); |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2368 | del_timer_sync(&sci->sc_timer); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2371 | /** |
| 2372 | * nilfs_segctor_notify - notify the result of request to caller threads |
| 2373 | * @sci: segment constructor object |
| 2374 | * @mode: mode of log forming |
| 2375 | * @err: error code to be notified |
| 2376 | */ |
| 2377 | static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2378 | { |
| 2379 | /* Clear requests (even when the construction failed) */ |
| 2380 | spin_lock(&sci->sc_state_lock); |
| 2381 | |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2382 | if (mode == SC_LSEG_SR) { |
Ryusuke Konishi | aeda7f6 | 2009-11-02 15:08:13 +0900 | [diff] [blame] | 2383 | sci->sc_state &= ~NILFS_SEGCTOR_COMMIT; |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2384 | sci->sc_seq_done = sci->sc_seq_accepted; |
| 2385 | nilfs_segctor_wakeup(sci, err); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2386 | sci->sc_flush_request = 0; |
Ryusuke Konishi | aeda7f6 | 2009-11-02 15:08:13 +0900 | [diff] [blame] | 2387 | } else { |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2388 | if (mode == SC_FLUSH_FILE) |
Ryusuke Konishi | aeda7f6 | 2009-11-02 15:08:13 +0900 | [diff] [blame] | 2389 | sci->sc_flush_request &= ~FLUSH_FILE_BIT; |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2390 | else if (mode == SC_FLUSH_DAT) |
Ryusuke Konishi | aeda7f6 | 2009-11-02 15:08:13 +0900 | [diff] [blame] | 2391 | sci->sc_flush_request &= ~FLUSH_DAT_BIT; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2392 | |
Ryusuke Konishi | aeda7f6 | 2009-11-02 15:08:13 +0900 | [diff] [blame] | 2393 | /* re-enable timer if checkpoint creation was not done */ |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2394 | if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && |
| 2395 | time_before(jiffies, sci->sc_timer.expires)) |
| 2396 | add_timer(&sci->sc_timer); |
Ryusuke Konishi | aeda7f6 | 2009-11-02 15:08:13 +0900 | [diff] [blame] | 2397 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2398 | spin_unlock(&sci->sc_state_lock); |
| 2399 | } |
| 2400 | |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2401 | /** |
| 2402 | * nilfs_segctor_construct - form logs and write them to disk |
| 2403 | * @sci: segment constructor object |
| 2404 | * @mode: mode of log forming |
| 2405 | */ |
| 2406 | static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2407 | { |
| 2408 | struct nilfs_sb_info *sbi = sci->sc_sbi; |
| 2409 | struct the_nilfs *nilfs = sbi->s_nilfs; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 2410 | struct nilfs_super_block **sbp; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2411 | int err = 0; |
| 2412 | |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2413 | nilfs_segctor_accept(sci); |
| 2414 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2415 | if (nilfs_discontinued(nilfs)) |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2416 | mode = SC_LSEG_SR; |
| 2417 | if (!nilfs_segctor_confirm(sci)) |
| 2418 | err = nilfs_segctor_do_construct(sci, mode); |
| 2419 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2420 | if (likely(!err)) { |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2421 | if (mode != SC_FLUSH_DAT) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2422 | atomic_set(&nilfs->ns_ndirtyblks, 0); |
| 2423 | if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) && |
| 2424 | nilfs_discontinued(nilfs)) { |
| 2425 | down_write(&nilfs->ns_sem); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 2426 | err = -EIO; |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 2427 | sbp = nilfs_prepare_super(sbi, |
| 2428 | nilfs_sb_will_flip(nilfs)); |
| 2429 | if (likely(sbp)) { |
| 2430 | nilfs_set_log_cursor(sbp[0], nilfs); |
| 2431 | err = nilfs_commit_super(sbi, NILFS_SB_COMMIT); |
| 2432 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2433 | up_write(&nilfs->ns_sem); |
| 2434 | } |
| 2435 | } |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2436 | |
| 2437 | nilfs_segctor_notify(sci, mode, err); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2438 | return err; |
| 2439 | } |
| 2440 | |
| 2441 | static void nilfs_construction_timeout(unsigned long data) |
| 2442 | { |
| 2443 | struct task_struct *p = (struct task_struct *)data; |
| 2444 | wake_up_process(p); |
| 2445 | } |
| 2446 | |
| 2447 | static void |
| 2448 | nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head) |
| 2449 | { |
| 2450 | struct nilfs_inode_info *ii, *n; |
| 2451 | |
| 2452 | list_for_each_entry_safe(ii, n, head, i_dirty) { |
| 2453 | if (!test_bit(NILFS_I_UPDATED, &ii->i_state)) |
| 2454 | continue; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2455 | list_del_init(&ii->i_dirty); |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 2456 | iput(&ii->vfs_inode); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame] | 2460 | int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv, |
| 2461 | void **kbufs) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2462 | { |
| 2463 | struct nilfs_sb_info *sbi = NILFS_SB(sb); |
| 2464 | struct nilfs_sc_info *sci = NILFS_SC(sbi); |
| 2465 | struct the_nilfs *nilfs = sbi->s_nilfs; |
| 2466 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2467 | int err; |
| 2468 | |
| 2469 | if (unlikely(!sci)) |
| 2470 | return -EROFS; |
| 2471 | |
| 2472 | nilfs_transaction_lock(sbi, &ti, 1); |
| 2473 | |
| 2474 | err = nilfs_init_gcdat_inode(nilfs); |
| 2475 | if (unlikely(err)) |
| 2476 | goto out_unlock; |
Ryusuke Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 2477 | |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame] | 2478 | err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2479 | if (unlikely(err)) |
| 2480 | goto out_unlock; |
| 2481 | |
Ryusuke Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 2482 | sci->sc_freesegs = kbufs[4]; |
| 2483 | sci->sc_nfreesegs = argv[4].v_nmembs; |
Ryusuke Konishi | 0935db7 | 2009-11-29 02:39:11 +0900 | [diff] [blame] | 2484 | list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2485 | |
| 2486 | for (;;) { |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2487 | err = nilfs_segctor_construct(sci, SC_LSEG_SR); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2488 | nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2489 | |
| 2490 | if (likely(!err)) |
| 2491 | break; |
| 2492 | |
| 2493 | nilfs_warning(sb, __func__, |
| 2494 | "segment construction failed. (err=%d)", err); |
| 2495 | set_current_state(TASK_INTERRUPTIBLE); |
| 2496 | schedule_timeout(sci->sc_interval); |
| 2497 | } |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 2498 | if (nilfs_test_opt(sbi, DISCARD)) { |
| 2499 | int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs, |
| 2500 | sci->sc_nfreesegs); |
| 2501 | if (ret) { |
| 2502 | printk(KERN_WARNING |
| 2503 | "NILFS warning: error %d on discard request, " |
| 2504 | "turning discards off for the device\n", ret); |
| 2505 | nilfs_clear_opt(sbi, DISCARD); |
| 2506 | } |
| 2507 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2508 | |
| 2509 | out_unlock: |
Ryusuke Konishi | 071cb4b | 2009-05-16 23:44:55 +0900 | [diff] [blame] | 2510 | sci->sc_freesegs = NULL; |
| 2511 | sci->sc_nfreesegs = 0; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2512 | nilfs_clear_gcdat_inode(nilfs); |
| 2513 | nilfs_transaction_unlock(sbi); |
| 2514 | return err; |
| 2515 | } |
| 2516 | |
| 2517 | static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode) |
| 2518 | { |
| 2519 | struct nilfs_sb_info *sbi = sci->sc_sbi; |
| 2520 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2521 | |
| 2522 | nilfs_transaction_lock(sbi, &ti, 0); |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2523 | nilfs_segctor_construct(sci, mode); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2524 | |
| 2525 | /* |
| 2526 | * Unclosed segment should be retried. We do this using sc_timer. |
| 2527 | * Timeout of sc_timer will invoke complete construction which leads |
| 2528 | * to close the current logical segment. |
| 2529 | */ |
| 2530 | if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) |
| 2531 | nilfs_segctor_start_timer(sci); |
| 2532 | |
| 2533 | nilfs_transaction_unlock(sbi); |
| 2534 | } |
| 2535 | |
| 2536 | static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci) |
| 2537 | { |
| 2538 | int mode = 0; |
| 2539 | int err; |
| 2540 | |
| 2541 | spin_lock(&sci->sc_state_lock); |
| 2542 | mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ? |
| 2543 | SC_FLUSH_DAT : SC_FLUSH_FILE; |
| 2544 | spin_unlock(&sci->sc_state_lock); |
| 2545 | |
| 2546 | if (mode) { |
| 2547 | err = nilfs_segctor_do_construct(sci, mode); |
| 2548 | |
| 2549 | spin_lock(&sci->sc_state_lock); |
| 2550 | sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ? |
| 2551 | ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT; |
| 2552 | spin_unlock(&sci->sc_state_lock); |
| 2553 | } |
| 2554 | clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags); |
| 2555 | } |
| 2556 | |
| 2557 | static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci) |
| 2558 | { |
| 2559 | if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) || |
| 2560 | time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) { |
| 2561 | if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT)) |
| 2562 | return SC_FLUSH_FILE; |
| 2563 | else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT)) |
| 2564 | return SC_FLUSH_DAT; |
| 2565 | } |
| 2566 | return SC_LSEG_SR; |
| 2567 | } |
| 2568 | |
| 2569 | /** |
| 2570 | * nilfs_segctor_thread - main loop of the segment constructor thread. |
| 2571 | * @arg: pointer to a struct nilfs_sc_info. |
| 2572 | * |
| 2573 | * nilfs_segctor_thread() initializes a timer and serves as a daemon |
| 2574 | * to execute segment constructions. |
| 2575 | */ |
| 2576 | static int nilfs_segctor_thread(void *arg) |
| 2577 | { |
| 2578 | struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg; |
Ryusuke Konishi | e605f0a | 2009-12-09 00:57:52 +0900 | [diff] [blame] | 2579 | struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2580 | int timeout = 0; |
| 2581 | |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2582 | sci->sc_timer.data = (unsigned long)current; |
| 2583 | sci->sc_timer.function = nilfs_construction_timeout; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2584 | |
| 2585 | /* start sync. */ |
| 2586 | sci->sc_task = current; |
| 2587 | wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */ |
| 2588 | printk(KERN_INFO |
| 2589 | "segctord starting. Construction interval = %lu seconds, " |
| 2590 | "CP frequency < %lu seconds\n", |
| 2591 | sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ); |
| 2592 | |
| 2593 | spin_lock(&sci->sc_state_lock); |
| 2594 | loop: |
| 2595 | for (;;) { |
| 2596 | int mode; |
| 2597 | |
| 2598 | if (sci->sc_state & NILFS_SEGCTOR_QUIT) |
| 2599 | goto end_thread; |
| 2600 | |
| 2601 | if (timeout || sci->sc_seq_request != sci->sc_seq_done) |
| 2602 | mode = SC_LSEG_SR; |
| 2603 | else if (!sci->sc_flush_request) |
| 2604 | break; |
| 2605 | else |
| 2606 | mode = nilfs_segctor_flush_mode(sci); |
| 2607 | |
| 2608 | spin_unlock(&sci->sc_state_lock); |
| 2609 | nilfs_segctor_thread_construct(sci, mode); |
| 2610 | spin_lock(&sci->sc_state_lock); |
| 2611 | timeout = 0; |
| 2612 | } |
| 2613 | |
| 2614 | |
| 2615 | if (freezing(current)) { |
| 2616 | spin_unlock(&sci->sc_state_lock); |
| 2617 | refrigerator(); |
| 2618 | spin_lock(&sci->sc_state_lock); |
| 2619 | } else { |
| 2620 | DEFINE_WAIT(wait); |
| 2621 | int should_sleep = 1; |
| 2622 | |
| 2623 | prepare_to_wait(&sci->sc_wait_daemon, &wait, |
| 2624 | TASK_INTERRUPTIBLE); |
| 2625 | |
| 2626 | if (sci->sc_seq_request != sci->sc_seq_done) |
| 2627 | should_sleep = 0; |
| 2628 | else if (sci->sc_flush_request) |
| 2629 | should_sleep = 0; |
| 2630 | else if (sci->sc_state & NILFS_SEGCTOR_COMMIT) |
| 2631 | should_sleep = time_before(jiffies, |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2632 | sci->sc_timer.expires); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2633 | |
| 2634 | if (should_sleep) { |
| 2635 | spin_unlock(&sci->sc_state_lock); |
| 2636 | schedule(); |
| 2637 | spin_lock(&sci->sc_state_lock); |
| 2638 | } |
| 2639 | finish_wait(&sci->sc_wait_daemon, &wait); |
| 2640 | timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2641 | time_after_eq(jiffies, sci->sc_timer.expires)); |
Ryusuke Konishi | e605f0a | 2009-12-09 00:57:52 +0900 | [diff] [blame] | 2642 | |
| 2643 | if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs)) |
Jiro SEKIBA | 1dfa271 | 2009-07-23 01:33:49 +0900 | [diff] [blame] | 2644 | set_nilfs_discontinued(nilfs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2645 | } |
| 2646 | goto loop; |
| 2647 | |
| 2648 | end_thread: |
| 2649 | spin_unlock(&sci->sc_state_lock); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2650 | |
| 2651 | /* end sync. */ |
| 2652 | sci->sc_task = NULL; |
| 2653 | wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */ |
| 2654 | return 0; |
| 2655 | } |
| 2656 | |
| 2657 | static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci) |
| 2658 | { |
| 2659 | struct task_struct *t; |
| 2660 | |
| 2661 | t = kthread_run(nilfs_segctor_thread, sci, "segctord"); |
| 2662 | if (IS_ERR(t)) { |
| 2663 | int err = PTR_ERR(t); |
| 2664 | |
| 2665 | printk(KERN_ERR "NILFS: error %d creating segctord thread\n", |
| 2666 | err); |
| 2667 | return err; |
| 2668 | } |
| 2669 | wait_event(sci->sc_wait_task, sci->sc_task != NULL); |
| 2670 | return 0; |
| 2671 | } |
| 2672 | |
| 2673 | static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci) |
| 2674 | { |
| 2675 | sci->sc_state |= NILFS_SEGCTOR_QUIT; |
| 2676 | |
| 2677 | while (sci->sc_task) { |
| 2678 | wake_up(&sci->sc_wait_daemon); |
| 2679 | spin_unlock(&sci->sc_state_lock); |
| 2680 | wait_event(sci->sc_wait_task, sci->sc_task == NULL); |
| 2681 | spin_lock(&sci->sc_state_lock); |
| 2682 | } |
| 2683 | } |
| 2684 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2685 | /* |
| 2686 | * Setup & clean-up functions |
| 2687 | */ |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2688 | static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi, |
| 2689 | struct nilfs_root *root) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2690 | { |
| 2691 | struct nilfs_sc_info *sci; |
| 2692 | |
| 2693 | sci = kzalloc(sizeof(*sci), GFP_KERNEL); |
| 2694 | if (!sci) |
| 2695 | return NULL; |
| 2696 | |
| 2697 | sci->sc_sbi = sbi; |
| 2698 | sci->sc_super = sbi->s_super; |
| 2699 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2700 | nilfs_get_root(root); |
| 2701 | sci->sc_root = root; |
| 2702 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2703 | init_waitqueue_head(&sci->sc_wait_request); |
| 2704 | init_waitqueue_head(&sci->sc_wait_daemon); |
| 2705 | init_waitqueue_head(&sci->sc_wait_task); |
| 2706 | spin_lock_init(&sci->sc_state_lock); |
| 2707 | INIT_LIST_HEAD(&sci->sc_dirty_files); |
| 2708 | INIT_LIST_HEAD(&sci->sc_segbufs); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2709 | INIT_LIST_HEAD(&sci->sc_write_logs); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2710 | INIT_LIST_HEAD(&sci->sc_gc_inodes); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2711 | INIT_LIST_HEAD(&sci->sc_copied_buffers); |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2712 | init_timer(&sci->sc_timer); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2713 | |
| 2714 | sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT; |
| 2715 | sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ; |
| 2716 | sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK; |
| 2717 | |
| 2718 | if (sbi->s_interval) |
| 2719 | sci->sc_interval = sbi->s_interval; |
| 2720 | if (sbi->s_watermark) |
| 2721 | sci->sc_watermark = sbi->s_watermark; |
| 2722 | return sci; |
| 2723 | } |
| 2724 | |
| 2725 | static void nilfs_segctor_write_out(struct nilfs_sc_info *sci) |
| 2726 | { |
| 2727 | int ret, retrycount = NILFS_SC_CLEANUP_RETRY; |
| 2728 | |
| 2729 | /* The segctord thread was stopped and its timer was removed. |
| 2730 | But some tasks remain. */ |
| 2731 | do { |
| 2732 | struct nilfs_sb_info *sbi = sci->sc_sbi; |
| 2733 | struct nilfs_transaction_info ti; |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2734 | |
| 2735 | nilfs_transaction_lock(sbi, &ti, 0); |
Ryusuke Konishi | dcd7618 | 2010-01-26 15:20:15 +0900 | [diff] [blame] | 2736 | ret = nilfs_segctor_construct(sci, SC_LSEG_SR); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2737 | nilfs_transaction_unlock(sbi); |
| 2738 | |
| 2739 | } while (ret && retrycount-- > 0); |
| 2740 | } |
| 2741 | |
| 2742 | /** |
| 2743 | * nilfs_segctor_destroy - destroy the segment constructor. |
| 2744 | * @sci: nilfs_sc_info |
| 2745 | * |
| 2746 | * nilfs_segctor_destroy() kills the segctord thread and frees |
| 2747 | * the nilfs_sc_info struct. |
| 2748 | * Caller must hold the segment semaphore. |
| 2749 | */ |
| 2750 | static void nilfs_segctor_destroy(struct nilfs_sc_info *sci) |
| 2751 | { |
| 2752 | struct nilfs_sb_info *sbi = sci->sc_sbi; |
| 2753 | int flag; |
| 2754 | |
| 2755 | up_write(&sbi->s_nilfs->ns_segctor_sem); |
| 2756 | |
| 2757 | spin_lock(&sci->sc_state_lock); |
| 2758 | nilfs_segctor_kill_thread(sci); |
| 2759 | flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request |
| 2760 | || sci->sc_seq_request != sci->sc_seq_done); |
| 2761 | spin_unlock(&sci->sc_state_lock); |
| 2762 | |
Ryusuke Konishi | 3256a05 | 2010-01-31 12:39:50 +0900 | [diff] [blame] | 2763 | if (flag || !nilfs_segctor_confirm(sci)) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2764 | nilfs_segctor_write_out(sci); |
| 2765 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2766 | WARN_ON(!list_empty(&sci->sc_copied_buffers)); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2767 | |
| 2768 | if (!list_empty(&sci->sc_dirty_files)) { |
| 2769 | nilfs_warning(sbi->s_super, __func__, |
| 2770 | "dirty file(s) after the final construction\n"); |
| 2771 | nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1); |
| 2772 | } |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2773 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2774 | WARN_ON(!list_empty(&sci->sc_segbufs)); |
Ryusuke Konishi | a694291a | 2009-11-29 23:03:04 +0900 | [diff] [blame] | 2775 | WARN_ON(!list_empty(&sci->sc_write_logs)); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2776 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2777 | nilfs_put_root(sci->sc_root); |
| 2778 | |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2779 | down_write(&sbi->s_nilfs->ns_segctor_sem); |
| 2780 | |
Li Hong | fdce895 | 2010-04-10 23:25:39 +0800 | [diff] [blame] | 2781 | del_timer_sync(&sci->sc_timer); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2782 | kfree(sci); |
| 2783 | } |
| 2784 | |
| 2785 | /** |
| 2786 | * nilfs_attach_segment_constructor - attach a segment constructor |
| 2787 | * @sbi: nilfs_sb_info |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2788 | * @root: root object of the current filesystem tree |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2789 | * |
| 2790 | * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info, |
Ryusuke Konishi | 7a65004 | 2010-03-14 03:32:40 +0900 | [diff] [blame] | 2791 | * initializes it, and starts the segment constructor. |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2792 | * |
| 2793 | * Return Value: On success, 0 is returned. On error, one of the following |
| 2794 | * negative error code is returned. |
| 2795 | * |
| 2796 | * %-ENOMEM - Insufficient memory available. |
| 2797 | */ |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2798 | int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi, |
| 2799 | struct nilfs_root *root) |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2800 | { |
| 2801 | struct the_nilfs *nilfs = sbi->s_nilfs; |
| 2802 | int err; |
| 2803 | |
Ryusuke Konishi | fe5f171 | 2010-01-31 19:46:40 +0900 | [diff] [blame] | 2804 | if (NILFS_SC(sbi)) { |
| 2805 | /* |
| 2806 | * This happens if the filesystem was remounted |
| 2807 | * read/write after nilfs_error degenerated it into a |
| 2808 | * read-only mount. |
| 2809 | */ |
| 2810 | nilfs_detach_segment_constructor(sbi); |
| 2811 | } |
| 2812 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame^] | 2813 | sbi->s_sc_info = nilfs_segctor_new(sbi, root); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2814 | if (!sbi->s_sc_info) |
| 2815 | return -ENOMEM; |
| 2816 | |
| 2817 | nilfs_attach_writer(nilfs, sbi); |
Li Hong | 154ac5a | 2010-04-10 21:57:11 +0800 | [diff] [blame] | 2818 | err = nilfs_segctor_start_thread(NILFS_SC(sbi)); |
Ryusuke Konishi | 9ff05123 | 2009-04-06 19:01:37 -0700 | [diff] [blame] | 2819 | if (err) { |
| 2820 | nilfs_detach_writer(nilfs, sbi); |
| 2821 | kfree(sbi->s_sc_info); |
| 2822 | sbi->s_sc_info = NULL; |
| 2823 | } |
| 2824 | return err; |
| 2825 | } |
| 2826 | |
| 2827 | /** |
| 2828 | * nilfs_detach_segment_constructor - destroy the segment constructor |
| 2829 | * @sbi: nilfs_sb_info |
| 2830 | * |
| 2831 | * nilfs_detach_segment_constructor() kills the segment constructor daemon, |
| 2832 | * frees the struct nilfs_sc_info, and destroy the dirty file list. |
| 2833 | */ |
| 2834 | void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi) |
| 2835 | { |
| 2836 | struct the_nilfs *nilfs = sbi->s_nilfs; |
| 2837 | LIST_HEAD(garbage_list); |
| 2838 | |
| 2839 | down_write(&nilfs->ns_segctor_sem); |
| 2840 | if (NILFS_SC(sbi)) { |
| 2841 | nilfs_segctor_destroy(NILFS_SC(sbi)); |
| 2842 | sbi->s_sc_info = NULL; |
| 2843 | } |
| 2844 | |
| 2845 | /* Force to free the list of dirty files */ |
| 2846 | spin_lock(&sbi->s_inode_lock); |
| 2847 | if (!list_empty(&sbi->s_dirty_files)) { |
| 2848 | list_splice_init(&sbi->s_dirty_files, &garbage_list); |
| 2849 | nilfs_warning(sbi->s_super, __func__, |
| 2850 | "Non empty dirty list after the last " |
| 2851 | "segment construction\n"); |
| 2852 | } |
| 2853 | spin_unlock(&sbi->s_inode_lock); |
| 2854 | up_write(&nilfs->ns_segctor_sem); |
| 2855 | |
| 2856 | nilfs_dispose_list(sbi, &garbage_list, 1); |
| 2857 | nilfs_detach_writer(nilfs, sbi); |
| 2858 | } |