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