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