1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * journal.c 4 * 5 * Defines functions of journalling api 6 * 7 * Copyright (C) 2003, 2004 Oracle. All rights reserved. 8 */ 9 10 #include <linux/fs.h> 11 #include <linux/types.h> 12 #include <linux/slab.h> 13 #include <linux/highmem.h> 14 #include <linux/kthread.h> 15 #include <linux/time.h> 16 #include <linux/random.h> 17 #include <linux/delay.h> 18 #include <linux/writeback.h> 19 20 #include <cluster/masklog.h> 21 22 #include "ocfs2.h" 23 24 #include "alloc.h" 25 #include "blockcheck.h" 26 #include "dir.h" 27 #include "dlmglue.h" 28 #include "extent_map.h" 29 #include "heartbeat.h" 30 #include "inode.h" 31 #include "journal.h" 32 #include "localalloc.h" 33 #include "slot_map.h" 34 #include "super.h" 35 #include "sysfile.h" 36 #include "uptodate.h" 37 #include "quota.h" 38 #include "file.h" 39 #include "namei.h" 40 41 #include "buffer_head_io.h" 42 #include "ocfs2_trace.h" 43 44 DEFINE_SPINLOCK(trans_inc_lock); 45 46 #define ORPHAN_SCAN_SCHEDULE_TIMEOUT 300000 47 48 static int ocfs2_force_read_journal(struct inode *inode); 49 static int ocfs2_recover_node(struct ocfs2_super *osb, 50 int node_num, int slot_num); 51 static int __ocfs2_recovery_thread(void *arg); 52 static int ocfs2_commit_cache(struct ocfs2_super *osb); 53 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota); 54 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb, 55 int dirty, int replayed); 56 static int ocfs2_trylock_journal(struct ocfs2_super *osb, 57 int slot_num); 58 static int ocfs2_recover_orphans(struct ocfs2_super *osb, 59 int slot, 60 enum ocfs2_orphan_reco_type orphan_reco_type); 61 static int ocfs2_commit_thread(void *arg); 62 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal, 63 int slot_num, 64 struct ocfs2_dinode *la_dinode, 65 struct ocfs2_dinode *tl_dinode, 66 struct ocfs2_quota_recovery *qrec, 67 enum ocfs2_orphan_reco_type orphan_reco_type); 68 69 static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb) 70 { 71 return __ocfs2_wait_on_mount(osb, 0); 72 } 73 74 static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb) 75 { 76 return __ocfs2_wait_on_mount(osb, 1); 77 } 78 79 /* 80 * This replay_map is to track online/offline slots, so we could recover 81 * offline slots during recovery and mount 82 */ 83 84 enum ocfs2_replay_state { 85 REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */ 86 REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */ 87 REPLAY_DONE /* Replay was already queued */ 88 }; 89 90 struct ocfs2_replay_map { 91 unsigned int rm_slots; 92 enum ocfs2_replay_state rm_state; 93 unsigned char rm_replay_slots[] __counted_by(rm_slots); 94 }; 95 96 static void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state) 97 { 98 if (!osb->replay_map) 99 return; 100 101 /* If we've already queued the replay, we don't have any more to do */ 102 if (osb->replay_map->rm_state == REPLAY_DONE) 103 return; 104 105 osb->replay_map->rm_state = state; 106 } 107 108 int ocfs2_compute_replay_slots(struct ocfs2_super *osb) 109 { 110 struct ocfs2_replay_map *replay_map; 111 int i, node_num; 112 113 /* If replay map is already set, we don't do it again */ 114 if (osb->replay_map) 115 return 0; 116 117 replay_map = kzalloc_flex(*replay_map, rm_replay_slots, osb->max_slots); 118 if (!replay_map) { 119 mlog_errno(-ENOMEM); 120 return -ENOMEM; 121 } 122 123 spin_lock(&osb->osb_lock); 124 125 replay_map->rm_slots = osb->max_slots; 126 replay_map->rm_state = REPLAY_UNNEEDED; 127 128 /* set rm_replay_slots for offline slot(s) */ 129 for (i = 0; i < replay_map->rm_slots; i++) { 130 if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT) 131 replay_map->rm_replay_slots[i] = 1; 132 } 133 134 osb->replay_map = replay_map; 135 spin_unlock(&osb->osb_lock); 136 return 0; 137 } 138 139 static void ocfs2_queue_replay_slots(struct ocfs2_super *osb, 140 enum ocfs2_orphan_reco_type orphan_reco_type) 141 { 142 struct ocfs2_replay_map *replay_map = osb->replay_map; 143 int i; 144 145 if (!replay_map) 146 return; 147 148 if (replay_map->rm_state != REPLAY_NEEDED) 149 return; 150 151 for (i = 0; i < replay_map->rm_slots; i++) 152 if (replay_map->rm_replay_slots[i]) 153 ocfs2_queue_recovery_completion(osb->journal, i, NULL, 154 NULL, NULL, 155 orphan_reco_type); 156 replay_map->rm_state = REPLAY_DONE; 157 } 158 159 void ocfs2_free_replay_slots(struct ocfs2_super *osb) 160 { 161 struct ocfs2_replay_map *replay_map = osb->replay_map; 162 163 if (!osb->replay_map) 164 return; 165 166 kfree(replay_map); 167 osb->replay_map = NULL; 168 } 169 170 int ocfs2_recovery_init(struct ocfs2_super *osb) 171 { 172 struct ocfs2_recovery_map *rm; 173 174 mutex_init(&osb->recovery_lock); 175 osb->recovery_state = OCFS2_REC_ENABLED; 176 osb->recovery_thread_task = NULL; 177 init_waitqueue_head(&osb->recovery_event); 178 179 rm = kzalloc_flex(*rm, rm_entries, osb->max_slots); 180 if (!rm) { 181 mlog_errno(-ENOMEM); 182 return -ENOMEM; 183 } 184 185 osb->recovery_map = rm; 186 187 return 0; 188 } 189 190 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb) 191 { 192 return osb->recovery_thread_task != NULL; 193 } 194 195 static void ocfs2_recovery_disable(struct ocfs2_super *osb, 196 enum ocfs2_recovery_state state) 197 { 198 mutex_lock(&osb->recovery_lock); 199 /* 200 * If recovery thread is not running, we can directly transition to 201 * final state. 202 */ 203 if (!ocfs2_recovery_thread_running(osb)) { 204 osb->recovery_state = state + 1; 205 goto out_lock; 206 } 207 osb->recovery_state = state; 208 /* Wait for recovery thread to acknowledge state transition */ 209 wait_event_cmd(osb->recovery_event, 210 !ocfs2_recovery_thread_running(osb) || 211 osb->recovery_state >= state + 1, 212 mutex_unlock(&osb->recovery_lock), 213 mutex_lock(&osb->recovery_lock)); 214 out_lock: 215 mutex_unlock(&osb->recovery_lock); 216 217 /* 218 * At this point we know that no more recovery work can be queued so 219 * wait for any recovery completion work to complete. 220 */ 221 if (osb->ocfs2_wq) 222 flush_workqueue(osb->ocfs2_wq); 223 } 224 225 void ocfs2_recovery_disable_quota(struct ocfs2_super *osb) 226 { 227 ocfs2_recovery_disable(osb, OCFS2_REC_QUOTA_WANT_DISABLE); 228 } 229 230 void ocfs2_recovery_exit(struct ocfs2_super *osb) 231 { 232 struct ocfs2_recovery_map *rm; 233 234 /* disable any new recovery threads and wait for any currently 235 * running ones to exit. Do this before setting the vol_state. */ 236 ocfs2_recovery_disable(osb, OCFS2_REC_WANT_DISABLE); 237 238 /* 239 * Now that recovery is shut down, and the osb is about to be 240 * freed, the osb_lock is not taken here. 241 */ 242 rm = osb->recovery_map; 243 /* XXX: Should we bug if there are dirty entries? */ 244 245 kfree(rm); 246 } 247 248 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb, 249 unsigned int node_num) 250 { 251 int i; 252 struct ocfs2_recovery_map *rm = osb->recovery_map; 253 254 assert_spin_locked(&osb->osb_lock); 255 256 for (i = 0; i < rm->rm_used; i++) { 257 if (rm->rm_entries[i] == node_num) 258 return 1; 259 } 260 261 return 0; 262 } 263 264 /* Behaves like test-and-set. Returns the previous value */ 265 static int ocfs2_recovery_map_set(struct ocfs2_super *osb, 266 unsigned int node_num) 267 { 268 struct ocfs2_recovery_map *rm = osb->recovery_map; 269 270 spin_lock(&osb->osb_lock); 271 if (__ocfs2_recovery_map_test(osb, node_num)) { 272 spin_unlock(&osb->osb_lock); 273 return 1; 274 } 275 276 /* XXX: Can this be exploited? Not from o2dlm... */ 277 BUG_ON(rm->rm_used >= osb->max_slots); 278 279 rm->rm_entries[rm->rm_used] = node_num; 280 rm->rm_used++; 281 spin_unlock(&osb->osb_lock); 282 283 return 0; 284 } 285 286 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb, 287 unsigned int node_num) 288 { 289 int i; 290 struct ocfs2_recovery_map *rm = osb->recovery_map; 291 292 spin_lock(&osb->osb_lock); 293 294 for (i = 0; i < rm->rm_used; i++) { 295 if (rm->rm_entries[i] == node_num) 296 break; 297 } 298 299 if (i < rm->rm_used) { 300 /* XXX: be careful with the pointer math */ 301 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]), 302 (rm->rm_used - i - 1) * sizeof(unsigned int)); 303 rm->rm_used--; 304 } 305 306 spin_unlock(&osb->osb_lock); 307 } 308 309 static int ocfs2_commit_cache(struct ocfs2_super *osb) 310 { 311 int status = 0; 312 unsigned int flushed; 313 struct ocfs2_journal *journal = NULL; 314 315 journal = osb->journal; 316 317 /* Flush all pending commits and checkpoint the journal. */ 318 down_write(&journal->j_trans_barrier); 319 320 flushed = atomic_read(&journal->j_num_trans); 321 trace_ocfs2_commit_cache_begin(flushed); 322 if (flushed == 0) { 323 up_write(&journal->j_trans_barrier); 324 goto finally; 325 } 326 327 jbd2_journal_lock_updates(journal->j_journal); 328 status = jbd2_journal_flush(journal->j_journal, 0); 329 jbd2_journal_unlock_updates(journal->j_journal); 330 if (status < 0) { 331 up_write(&journal->j_trans_barrier); 332 mlog_errno(status); 333 goto finally; 334 } 335 336 ocfs2_inc_trans_id(journal); 337 338 flushed = atomic_read(&journal->j_num_trans); 339 atomic_set(&journal->j_num_trans, 0); 340 up_write(&journal->j_trans_barrier); 341 342 trace_ocfs2_commit_cache_end(journal->j_trans_id, flushed); 343 344 ocfs2_wake_downconvert_thread(osb); 345 wake_up(&journal->j_checkpointed); 346 finally: 347 return status; 348 } 349 350 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs) 351 { 352 journal_t *journal = osb->journal->j_journal; 353 handle_t *handle; 354 355 BUG_ON(!osb || !osb->journal->j_journal); 356 357 if (ocfs2_is_hard_readonly(osb)) 358 return ERR_PTR(-EROFS); 359 360 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE); 361 BUG_ON(max_buffs <= 0); 362 363 /* Nested transaction? Just return the handle... */ 364 if (journal_current_handle()) 365 return jbd2_journal_start(journal, max_buffs); 366 367 sb_start_intwrite(osb->sb); 368 369 down_read(&osb->journal->j_trans_barrier); 370 371 handle = jbd2_journal_start(journal, max_buffs); 372 if (IS_ERR(handle)) { 373 up_read(&osb->journal->j_trans_barrier); 374 sb_end_intwrite(osb->sb); 375 376 mlog_errno(PTR_ERR(handle)); 377 378 if (is_journal_aborted(journal)) { 379 ocfs2_abort(osb->sb, "Detected aborted journal\n"); 380 handle = ERR_PTR(-EROFS); 381 } 382 } else { 383 if (!ocfs2_mount_local(osb)) 384 atomic_inc(&(osb->journal->j_num_trans)); 385 } 386 387 return handle; 388 } 389 390 int ocfs2_commit_trans(struct ocfs2_super *osb, 391 handle_t *handle) 392 { 393 int ret, nested; 394 struct ocfs2_journal *journal = osb->journal; 395 396 BUG_ON(!handle); 397 398 nested = handle->h_ref > 1; 399 ret = jbd2_journal_stop(handle); 400 if (ret < 0) 401 mlog_errno(ret); 402 403 if (!nested) { 404 up_read(&journal->j_trans_barrier); 405 sb_end_intwrite(osb->sb); 406 } 407 408 return ret; 409 } 410 411 /* 412 * 'nblocks' is what you want to add to the current transaction. 413 * 414 * This might call jbd2_journal_restart() which will commit dirty buffers 415 * and then restart the transaction. Before calling 416 * ocfs2_extend_trans(), any changed blocks should have been 417 * dirtied. After calling it, all blocks which need to be changed must 418 * go through another set of journal_access/journal_dirty calls. 419 * 420 * WARNING: This will not release any semaphores or disk locks taken 421 * during the transaction, so make sure they were taken *before* 422 * start_trans or we'll have ordering deadlocks. 423 * 424 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is 425 * good because transaction ids haven't yet been recorded on the 426 * cluster locks associated with this handle. 427 */ 428 int ocfs2_extend_trans(handle_t *handle, int nblocks) 429 { 430 int status, old_nblocks; 431 432 BUG_ON(!handle); 433 BUG_ON(nblocks < 0); 434 435 if (!nblocks) 436 return 0; 437 438 old_nblocks = jbd2_handle_buffer_credits(handle); 439 440 trace_ocfs2_extend_trans(old_nblocks, nblocks); 441 442 #ifdef CONFIG_OCFS2_DEBUG_FS 443 status = 1; 444 #else 445 status = jbd2_journal_extend(handle, nblocks, 0); 446 if (status < 0) { 447 mlog_errno(status); 448 goto bail; 449 } 450 #endif 451 452 if (status > 0) { 453 trace_ocfs2_extend_trans_restart(old_nblocks + nblocks); 454 status = jbd2_journal_restart(handle, 455 old_nblocks + nblocks); 456 if (status < 0) { 457 mlog_errno(status); 458 goto bail; 459 } 460 } 461 462 status = 0; 463 bail: 464 return status; 465 } 466 467 /* 468 * Make sure handle has at least 'nblocks' credits available. If it does not 469 * have that many credits available, we will try to extend the handle to have 470 * enough credits. If that fails, we will restart transaction to have enough 471 * credits. Similar notes regarding data consistency and locking implications 472 * as for ocfs2_extend_trans() apply here. 473 */ 474 int ocfs2_assure_trans_credits(handle_t *handle, int nblocks) 475 { 476 int old_nblks = jbd2_handle_buffer_credits(handle); 477 478 trace_ocfs2_assure_trans_credits(old_nblks); 479 if (old_nblks >= nblocks) 480 return 0; 481 return ocfs2_extend_trans(handle, nblocks - old_nblks); 482 } 483 484 /* 485 * If we have fewer than thresh credits, extend by OCFS2_MAX_TRANS_DATA. 486 * If that fails, restart the transaction & regain write access for the 487 * buffer head which is used for metadata modifications. 488 * Taken from Ext4: extend_or_restart_transaction() 489 */ 490 int ocfs2_allocate_extend_trans(handle_t *handle, int thresh) 491 { 492 int status, old_nblks; 493 494 BUG_ON(!handle); 495 496 old_nblks = jbd2_handle_buffer_credits(handle); 497 trace_ocfs2_allocate_extend_trans(old_nblks, thresh); 498 499 if (old_nblks < thresh) 500 return 0; 501 502 status = jbd2_journal_extend(handle, OCFS2_MAX_TRANS_DATA, 0); 503 if (status < 0) { 504 mlog_errno(status); 505 goto bail; 506 } 507 508 if (status > 0) { 509 status = jbd2_journal_restart(handle, OCFS2_MAX_TRANS_DATA); 510 if (status < 0) 511 mlog_errno(status); 512 } 513 514 bail: 515 return status; 516 } 517 518 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers) 519 { 520 return container_of(triggers, struct ocfs2_triggers, ot_triggers); 521 } 522 523 static void ocfs2_frozen_trigger(struct jbd2_buffer_trigger_type *triggers, 524 struct buffer_head *bh, 525 void *data, size_t size) 526 { 527 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers); 528 529 /* 530 * We aren't guaranteed to have the superblock here, so we 531 * must unconditionally compute the ecc data. 532 * __ocfs2_journal_access() will only set the triggers if 533 * metaecc is enabled. 534 */ 535 ocfs2_block_check_compute(data, size, data + ot->ot_offset); 536 } 537 538 /* 539 * Quota blocks have their own trigger because the struct ocfs2_block_check 540 * offset depends on the blocksize. 541 */ 542 static void ocfs2_dq_frozen_trigger(struct jbd2_buffer_trigger_type *triggers, 543 struct buffer_head *bh, 544 void *data, size_t size) 545 { 546 struct ocfs2_disk_dqtrailer *dqt = 547 ocfs2_block_dqtrailer(size, data); 548 549 /* 550 * We aren't guaranteed to have the superblock here, so we 551 * must unconditionally compute the ecc data. 552 * __ocfs2_journal_access() will only set the triggers if 553 * metaecc is enabled. 554 */ 555 ocfs2_block_check_compute(data, size, &dqt->dq_check); 556 } 557 558 /* 559 * Directory blocks also have their own trigger because the 560 * struct ocfs2_block_check offset depends on the blocksize. 561 */ 562 static void ocfs2_db_frozen_trigger(struct jbd2_buffer_trigger_type *triggers, 563 struct buffer_head *bh, 564 void *data, size_t size) 565 { 566 struct ocfs2_dir_block_trailer *trailer = 567 ocfs2_dir_trailer_from_size(size, data); 568 569 /* 570 * We aren't guaranteed to have the superblock here, so we 571 * must unconditionally compute the ecc data. 572 * __ocfs2_journal_access() will only set the triggers if 573 * metaecc is enabled. 574 */ 575 ocfs2_block_check_compute(data, size, &trailer->db_check); 576 } 577 578 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers, 579 struct buffer_head *bh) 580 { 581 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers); 582 583 mlog(ML_ERROR, 584 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, " 585 "bh->b_blocknr = %llu\n", 586 (unsigned long)bh, 587 (unsigned long long)bh->b_blocknr); 588 589 ocfs2_error(ot->sb, 590 "JBD2 has aborted our journal, ocfs2 cannot continue\n"); 591 } 592 593 static void ocfs2_setup_csum_triggers(struct super_block *sb, 594 enum ocfs2_journal_trigger_type type, 595 struct ocfs2_triggers *ot) 596 { 597 BUG_ON(type >= OCFS2_JOURNAL_TRIGGER_COUNT); 598 599 switch (type) { 600 case OCFS2_JTR_DI: 601 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 602 ot->ot_offset = offsetof(struct ocfs2_dinode, i_check); 603 break; 604 case OCFS2_JTR_EB: 605 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 606 ot->ot_offset = offsetof(struct ocfs2_extent_block, h_check); 607 break; 608 case OCFS2_JTR_RB: 609 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 610 ot->ot_offset = offsetof(struct ocfs2_refcount_block, rf_check); 611 break; 612 case OCFS2_JTR_GD: 613 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 614 ot->ot_offset = offsetof(struct ocfs2_group_desc, bg_check); 615 break; 616 case OCFS2_JTR_DB: 617 ot->ot_triggers.t_frozen = ocfs2_db_frozen_trigger; 618 break; 619 case OCFS2_JTR_XB: 620 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 621 ot->ot_offset = offsetof(struct ocfs2_xattr_block, xb_check); 622 break; 623 case OCFS2_JTR_DQ: 624 ot->ot_triggers.t_frozen = ocfs2_dq_frozen_trigger; 625 break; 626 case OCFS2_JTR_DR: 627 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 628 ot->ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check); 629 break; 630 case OCFS2_JTR_DL: 631 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger; 632 ot->ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check); 633 break; 634 case OCFS2_JTR_NONE: 635 /* To make compiler happy... */ 636 return; 637 } 638 639 ot->ot_triggers.t_abort = ocfs2_abort_trigger; 640 ot->sb = sb; 641 } 642 643 void ocfs2_initialize_journal_triggers(struct super_block *sb, 644 struct ocfs2_triggers triggers[]) 645 { 646 enum ocfs2_journal_trigger_type type; 647 648 for (type = OCFS2_JTR_DI; type < OCFS2_JOURNAL_TRIGGER_COUNT; type++) 649 ocfs2_setup_csum_triggers(sb, type, &triggers[type]); 650 } 651 652 static int __ocfs2_journal_access(handle_t *handle, 653 struct ocfs2_caching_info *ci, 654 struct buffer_head *bh, 655 struct ocfs2_triggers *triggers, 656 int type) 657 { 658 int status; 659 struct ocfs2_super *osb = 660 OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 661 662 BUG_ON(!ci || !ci->ci_ops); 663 BUG_ON(!handle); 664 BUG_ON(!bh); 665 666 trace_ocfs2_journal_access( 667 (unsigned long long)ocfs2_metadata_cache_owner(ci), 668 (unsigned long long)bh->b_blocknr, type, bh->b_size); 669 670 /* we can safely remove this assertion after testing. */ 671 if (!buffer_uptodate(bh)) { 672 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n"); 673 mlog(ML_ERROR, "b_blocknr=%llu, b_state=0x%lx\n", 674 (unsigned long long)bh->b_blocknr, bh->b_state); 675 676 lock_buffer(bh); 677 /* 678 * A previous transaction with a couple of buffer heads fail 679 * to checkpoint, so all the bhs are marked as BH_Write_EIO. 680 * For current transaction, the bh is just among those error 681 * bhs which previous transaction handle. We can't just clear 682 * its BH_Write_EIO and reuse directly, since other bhs are 683 * not written to disk yet and that will cause metadata 684 * inconsistency. So we should set fs read-only to avoid 685 * further damage. 686 */ 687 if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) { 688 unlock_buffer(bh); 689 return ocfs2_error(osb->sb, "A previous attempt to " 690 "write this buffer head failed\n"); 691 } 692 unlock_buffer(bh); 693 } 694 695 /* Set the current transaction information on the ci so 696 * that the locking code knows whether it can drop it's locks 697 * on this ci or not. We're protected from the commit 698 * thread updating the current transaction id until 699 * ocfs2_commit_trans() because ocfs2_start_trans() took 700 * j_trans_barrier for us. */ 701 ocfs2_set_ci_lock_trans(osb->journal, ci); 702 703 ocfs2_metadata_cache_io_lock(ci); 704 switch (type) { 705 case OCFS2_JOURNAL_ACCESS_CREATE: 706 case OCFS2_JOURNAL_ACCESS_WRITE: 707 status = jbd2_journal_get_write_access(handle, bh); 708 break; 709 710 case OCFS2_JOURNAL_ACCESS_UNDO: 711 status = jbd2_journal_get_undo_access(handle, bh); 712 break; 713 714 default: 715 status = -EINVAL; 716 mlog(ML_ERROR, "Unknown access type!\n"); 717 } 718 if (!status && ocfs2_meta_ecc(osb) && triggers) 719 jbd2_journal_set_triggers(bh, &triggers->ot_triggers); 720 ocfs2_metadata_cache_io_unlock(ci); 721 722 if (status < 0) 723 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n", 724 status, type); 725 726 return status; 727 } 728 729 int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci, 730 struct buffer_head *bh, int type) 731 { 732 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 733 734 return __ocfs2_journal_access(handle, ci, bh, 735 &osb->s_journal_triggers[OCFS2_JTR_DI], 736 type); 737 } 738 739 int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci, 740 struct buffer_head *bh, int type) 741 { 742 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 743 744 return __ocfs2_journal_access(handle, ci, bh, 745 &osb->s_journal_triggers[OCFS2_JTR_EB], 746 type); 747 } 748 749 int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci, 750 struct buffer_head *bh, int type) 751 { 752 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 753 754 return __ocfs2_journal_access(handle, ci, bh, 755 &osb->s_journal_triggers[OCFS2_JTR_RB], 756 type); 757 } 758 759 int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci, 760 struct buffer_head *bh, int type) 761 { 762 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 763 764 return __ocfs2_journal_access(handle, ci, bh, 765 &osb->s_journal_triggers[OCFS2_JTR_GD], 766 type); 767 } 768 769 int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci, 770 struct buffer_head *bh, int type) 771 { 772 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 773 774 return __ocfs2_journal_access(handle, ci, bh, 775 &osb->s_journal_triggers[OCFS2_JTR_DB], 776 type); 777 } 778 779 int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci, 780 struct buffer_head *bh, int type) 781 { 782 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 783 784 return __ocfs2_journal_access(handle, ci, bh, 785 &osb->s_journal_triggers[OCFS2_JTR_XB], 786 type); 787 } 788 789 int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci, 790 struct buffer_head *bh, int type) 791 { 792 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 793 794 return __ocfs2_journal_access(handle, ci, bh, 795 &osb->s_journal_triggers[OCFS2_JTR_DQ], 796 type); 797 } 798 799 int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci, 800 struct buffer_head *bh, int type) 801 { 802 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 803 804 return __ocfs2_journal_access(handle, ci, bh, 805 &osb->s_journal_triggers[OCFS2_JTR_DR], 806 type); 807 } 808 809 int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci, 810 struct buffer_head *bh, int type) 811 { 812 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci)); 813 814 return __ocfs2_journal_access(handle, ci, bh, 815 &osb->s_journal_triggers[OCFS2_JTR_DL], 816 type); 817 } 818 819 int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci, 820 struct buffer_head *bh, int type) 821 { 822 return __ocfs2_journal_access(handle, ci, bh, NULL, type); 823 } 824 825 void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh) 826 { 827 int status; 828 829 trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr); 830 831 status = jbd2_journal_dirty_metadata(handle, bh); 832 if (status) { 833 mlog_errno(status); 834 if (!is_handle_aborted(handle)) { 835 journal_t *journal = handle->h_transaction->t_journal; 836 837 mlog(ML_ERROR, "jbd2_journal_dirty_metadata failed: " 838 "handle type %u started at line %u, credits %u/%u " 839 "errcode %d. Aborting transaction and journal.\n", 840 handle->h_type, handle->h_line_no, 841 handle->h_requested_credits, 842 jbd2_handle_buffer_credits(handle), status); 843 handle->h_err = status; 844 jbd2_journal_abort_handle(handle); 845 jbd2_journal_abort(journal, status); 846 } 847 } 848 } 849 850 #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE) 851 852 void ocfs2_set_journal_params(struct ocfs2_super *osb) 853 { 854 journal_t *journal = osb->journal->j_journal; 855 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL; 856 857 if (osb->osb_commit_interval) 858 commit_interval = osb->osb_commit_interval; 859 860 write_lock(&journal->j_state_lock); 861 journal->j_commit_interval = commit_interval; 862 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER) 863 journal->j_flags |= JBD2_BARRIER; 864 else 865 journal->j_flags &= ~JBD2_BARRIER; 866 write_unlock(&journal->j_state_lock); 867 } 868 869 /* 870 * alloc & initialize skeleton for journal structure. 871 * ocfs2_journal_init() will make fs have journal ability. 872 */ 873 int ocfs2_journal_alloc(struct ocfs2_super *osb) 874 { 875 int status = 0; 876 struct ocfs2_journal *journal; 877 878 journal = kzalloc_obj(struct ocfs2_journal); 879 if (!journal) { 880 mlog(ML_ERROR, "unable to alloc journal\n"); 881 status = -ENOMEM; 882 goto bail; 883 } 884 osb->journal = journal; 885 journal->j_osb = osb; 886 887 atomic_set(&journal->j_num_trans, 0); 888 init_rwsem(&journal->j_trans_barrier); 889 init_waitqueue_head(&journal->j_checkpointed); 890 spin_lock_init(&journal->j_lock); 891 journal->j_trans_id = 1UL; 892 INIT_LIST_HEAD(&journal->j_la_cleanups); 893 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery); 894 journal->j_state = OCFS2_JOURNAL_FREE; 895 896 bail: 897 return status; 898 } 899 900 static int ocfs2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode) 901 { 902 return filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping, 903 jinode->i_dirty_start, jinode->i_dirty_end); 904 } 905 906 int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty) 907 { 908 int status = -1; 909 struct inode *inode = NULL; /* the journal inode */ 910 journal_t *j_journal = NULL; 911 struct ocfs2_journal *journal = osb->journal; 912 struct ocfs2_dinode *di = NULL; 913 struct buffer_head *bh = NULL; 914 int inode_lock = 0; 915 916 BUG_ON(!journal); 917 /* already have the inode for our journal */ 918 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, 919 osb->slot_num); 920 if (inode == NULL) { 921 status = -EACCES; 922 mlog_errno(status); 923 goto done; 924 } 925 if (is_bad_inode(inode)) { 926 mlog(ML_ERROR, "access error (bad inode)\n"); 927 iput(inode); 928 inode = NULL; 929 status = -EACCES; 930 goto done; 931 } 932 933 SET_INODE_JOURNAL(inode); 934 OCFS2_I(inode)->ip_open_count++; 935 936 /* Skip recovery waits here - journal inode metadata never 937 * changes in a live cluster so it can be considered an 938 * exception to the rule. */ 939 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY); 940 if (status < 0) { 941 if (status != -ERESTARTSYS) 942 mlog(ML_ERROR, "Could not get lock on journal!\n"); 943 goto done; 944 } 945 946 inode_lock = 1; 947 di = (struct ocfs2_dinode *)bh->b_data; 948 949 if (i_size_read(inode) < OCFS2_MIN_JOURNAL_SIZE) { 950 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n", 951 i_size_read(inode)); 952 status = -EINVAL; 953 goto done; 954 } 955 956 trace_ocfs2_journal_init(i_size_read(inode), 957 (unsigned long long)inode->i_blocks, 958 OCFS2_I(inode)->ip_clusters); 959 960 /* call the kernels journal init function now */ 961 j_journal = jbd2_journal_init_inode(inode); 962 if (IS_ERR(j_journal)) { 963 mlog(ML_ERROR, "Linux journal layer error\n"); 964 status = PTR_ERR(j_journal); 965 goto done; 966 } 967 968 trace_ocfs2_journal_init_maxlen(j_journal->j_total_len); 969 970 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) & 971 OCFS2_JOURNAL_DIRTY_FL); 972 973 journal->j_journal = j_journal; 974 journal->j_journal->j_submit_inode_data_buffers = 975 ocfs2_journal_submit_inode_data_buffers; 976 journal->j_journal->j_finish_inode_data_buffers = 977 jbd2_journal_finish_inode_data_buffers; 978 journal->j_inode = inode; 979 journal->j_bh = bh; 980 981 ocfs2_set_journal_params(osb); 982 983 journal->j_state = OCFS2_JOURNAL_LOADED; 984 985 status = 0; 986 done: 987 if (status < 0) { 988 if (inode_lock) 989 ocfs2_inode_unlock(inode, 1); 990 brelse(bh); 991 if (inode) { 992 OCFS2_I(inode)->ip_open_count--; 993 iput(inode); 994 } 995 } 996 997 return status; 998 } 999 1000 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di) 1001 { 1002 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1); 1003 } 1004 1005 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di) 1006 { 1007 return le32_to_cpu(di->id1.journal1.ij_recovery_generation); 1008 } 1009 1010 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb, 1011 int dirty, int replayed) 1012 { 1013 int status; 1014 unsigned int flags; 1015 struct ocfs2_journal *journal = osb->journal; 1016 struct buffer_head *bh = journal->j_bh; 1017 struct ocfs2_dinode *fe; 1018 1019 fe = (struct ocfs2_dinode *)bh->b_data; 1020 1021 /* The journal bh on the osb always comes from ocfs2_journal_init() 1022 * and was validated there inside ocfs2_inode_lock_full(). It's a 1023 * code bug if we mess it up. */ 1024 BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 1025 1026 flags = le32_to_cpu(fe->id1.journal1.ij_flags); 1027 if (dirty) 1028 flags |= OCFS2_JOURNAL_DIRTY_FL; 1029 else 1030 flags &= ~OCFS2_JOURNAL_DIRTY_FL; 1031 fe->id1.journal1.ij_flags = cpu_to_le32(flags); 1032 1033 if (replayed) 1034 ocfs2_bump_recovery_generation(fe); 1035 1036 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check); 1037 status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode)); 1038 if (status < 0) 1039 mlog_errno(status); 1040 1041 return status; 1042 } 1043 1044 /* 1045 * If the journal has been kmalloc'd it needs to be freed after this 1046 * call. 1047 */ 1048 void ocfs2_journal_shutdown(struct ocfs2_super *osb) 1049 { 1050 struct ocfs2_journal *journal = NULL; 1051 int status = 0; 1052 struct inode *inode = NULL; 1053 int num_running_trans = 0; 1054 1055 BUG_ON(!osb); 1056 1057 journal = osb->journal; 1058 if (!journal) 1059 goto done; 1060 1061 inode = journal->j_inode; 1062 1063 if (journal->j_state != OCFS2_JOURNAL_LOADED) 1064 goto done; 1065 1066 /* need to inc inode use count - jbd2_journal_destroy will iput. */ 1067 if (!igrab(inode)) 1068 BUG(); 1069 1070 num_running_trans = atomic_read(&(journal->j_num_trans)); 1071 trace_ocfs2_journal_shutdown(num_running_trans); 1072 1073 /* Do a commit_cache here. It will flush our journal, *and* 1074 * release any locks that are still held. 1075 * set the SHUTDOWN flag and release the trans lock. 1076 * the commit thread will take the trans lock for us below. */ 1077 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN; 1078 1079 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not 1080 * drop the trans_lock (which we want to hold until we 1081 * completely destroy the journal. */ 1082 if (osb->commit_task) { 1083 /* Wait for the commit thread */ 1084 trace_ocfs2_journal_shutdown_wait(osb->commit_task); 1085 kthread_stop(osb->commit_task); 1086 osb->commit_task = NULL; 1087 } 1088 1089 BUG_ON(atomic_read(&(journal->j_num_trans)) != 0); 1090 1091 if (ocfs2_mount_local(osb) && 1092 (journal->j_journal->j_flags & JBD2_LOADED)) { 1093 jbd2_journal_lock_updates(journal->j_journal); 1094 status = jbd2_journal_flush(journal->j_journal, 0); 1095 jbd2_journal_unlock_updates(journal->j_journal); 1096 if (status < 0) 1097 mlog_errno(status); 1098 } 1099 1100 /* Shutdown the kernel journal system */ 1101 if (!jbd2_journal_destroy(journal->j_journal) && !status) { 1102 /* 1103 * Do not toggle if flush was unsuccessful otherwise 1104 * will leave dirty metadata in a "clean" journal 1105 */ 1106 status = ocfs2_journal_toggle_dirty(osb, 0, 0); 1107 if (status < 0) 1108 mlog_errno(status); 1109 } 1110 journal->j_journal = NULL; 1111 1112 OCFS2_I(inode)->ip_open_count--; 1113 1114 /* unlock our journal */ 1115 ocfs2_inode_unlock(inode, 1); 1116 1117 brelse(journal->j_bh); 1118 journal->j_bh = NULL; 1119 1120 journal->j_state = OCFS2_JOURNAL_FREE; 1121 1122 done: 1123 iput(inode); 1124 kfree(journal); 1125 osb->journal = NULL; 1126 } 1127 1128 static void ocfs2_clear_journal_error(struct super_block *sb, 1129 journal_t *journal, 1130 int slot) 1131 { 1132 int olderr; 1133 1134 olderr = jbd2_journal_errno(journal); 1135 if (olderr) { 1136 mlog(ML_ERROR, "File system error %d recorded in " 1137 "journal %u.\n", olderr, slot); 1138 mlog(ML_ERROR, "File system on device %s needs checking.\n", 1139 sb->s_id); 1140 1141 jbd2_journal_ack_err(journal); 1142 jbd2_journal_clear_err(journal); 1143 } 1144 } 1145 1146 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed) 1147 { 1148 int status = 0; 1149 struct ocfs2_super *osb; 1150 1151 BUG_ON(!journal); 1152 1153 osb = journal->j_osb; 1154 1155 status = jbd2_journal_load(journal->j_journal); 1156 if (status < 0) { 1157 mlog(ML_ERROR, "Failed to load journal!\n"); 1158 goto done; 1159 } 1160 1161 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num); 1162 1163 if (replayed) { 1164 jbd2_journal_lock_updates(journal->j_journal); 1165 status = jbd2_journal_flush(journal->j_journal, 0); 1166 jbd2_journal_unlock_updates(journal->j_journal); 1167 if (status < 0) 1168 mlog_errno(status); 1169 } 1170 1171 status = ocfs2_journal_toggle_dirty(osb, 1, replayed); 1172 if (status < 0) { 1173 mlog_errno(status); 1174 goto done; 1175 } 1176 1177 /* Launch the commit thread */ 1178 if (!local) { 1179 osb->commit_task = kthread_run(ocfs2_commit_thread, osb, 1180 "ocfs2cmt-%s", osb->uuid_str); 1181 if (IS_ERR(osb->commit_task)) { 1182 status = PTR_ERR(osb->commit_task); 1183 osb->commit_task = NULL; 1184 mlog(ML_ERROR, "unable to launch ocfs2commit thread, " 1185 "error=%d", status); 1186 goto done; 1187 } 1188 } else 1189 osb->commit_task = NULL; 1190 1191 done: 1192 return status; 1193 } 1194 1195 1196 /* 'full' flag tells us whether we clear out all blocks or if we just 1197 * mark the journal clean */ 1198 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full) 1199 { 1200 int status; 1201 1202 BUG_ON(!journal); 1203 1204 status = jbd2_journal_wipe(journal->j_journal, full); 1205 if (status < 0) { 1206 mlog_errno(status); 1207 goto bail; 1208 } 1209 1210 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0); 1211 if (status < 0) 1212 mlog_errno(status); 1213 1214 bail: 1215 return status; 1216 } 1217 1218 static int ocfs2_recovery_completed(struct ocfs2_super *osb) 1219 { 1220 int empty; 1221 struct ocfs2_recovery_map *rm = osb->recovery_map; 1222 1223 spin_lock(&osb->osb_lock); 1224 empty = (rm->rm_used == 0); 1225 spin_unlock(&osb->osb_lock); 1226 1227 return empty; 1228 } 1229 1230 void ocfs2_wait_for_recovery(struct ocfs2_super *osb) 1231 { 1232 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb)); 1233 } 1234 1235 /* 1236 * JBD Might read a cached version of another nodes journal file. We 1237 * don't want this as this file changes often and we get no 1238 * notification on those changes. The only way to be sure that we've 1239 * got the most up to date version of those blocks then is to force 1240 * read them off disk. Just searching through the buffer cache won't 1241 * work as there may be pages backing this file which are still marked 1242 * up to date. We know things can't change on this file underneath us 1243 * as we have the lock by now :) 1244 */ 1245 static int ocfs2_force_read_journal(struct inode *inode) 1246 { 1247 int status = 0; 1248 int i; 1249 u64 v_blkno, p_blkno, p_blocks, num_blocks; 1250 struct buffer_head *bh = NULL; 1251 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1252 1253 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode)); 1254 v_blkno = 0; 1255 while (v_blkno < num_blocks) { 1256 status = ocfs2_extent_map_get_blocks(inode, v_blkno, 1257 &p_blkno, &p_blocks, NULL); 1258 if (status < 0) { 1259 mlog_errno(status); 1260 goto bail; 1261 } 1262 1263 for (i = 0; i < p_blocks; i++, p_blkno++) { 1264 bh = __find_get_block_nonatomic(osb->sb->s_bdev, p_blkno, 1265 osb->sb->s_blocksize); 1266 /* block not cached. */ 1267 if (!bh) 1268 continue; 1269 1270 brelse(bh); 1271 bh = NULL; 1272 /* We are reading journal data which should not 1273 * be put in the uptodate cache. 1274 */ 1275 status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh); 1276 if (status < 0) { 1277 mlog_errno(status); 1278 goto bail; 1279 } 1280 1281 brelse(bh); 1282 bh = NULL; 1283 } 1284 1285 v_blkno += p_blocks; 1286 } 1287 1288 bail: 1289 return status; 1290 } 1291 1292 struct ocfs2_la_recovery_item { 1293 struct list_head lri_list; 1294 int lri_slot; 1295 struct ocfs2_dinode *lri_la_dinode; 1296 struct ocfs2_dinode *lri_tl_dinode; 1297 struct ocfs2_quota_recovery *lri_qrec; 1298 enum ocfs2_orphan_reco_type lri_orphan_reco_type; 1299 }; 1300 1301 /* Does the second half of the recovery process. By this point, the 1302 * node is marked clean and can actually be considered recovered, 1303 * hence it's no longer in the recovery map, but there's still some 1304 * cleanup we can do which shouldn't happen within the recovery thread 1305 * as locking in that context becomes very difficult if we are to take 1306 * recovering nodes into account. 1307 * 1308 * NOTE: This function can and will sleep on recovery of other nodes 1309 * during cluster locking, just like any other ocfs2 process. 1310 */ 1311 void ocfs2_complete_recovery(struct work_struct *work) 1312 { 1313 int ret = 0; 1314 struct ocfs2_journal *journal = 1315 container_of(work, struct ocfs2_journal, j_recovery_work); 1316 struct ocfs2_super *osb = journal->j_osb; 1317 struct ocfs2_dinode *la_dinode, *tl_dinode; 1318 struct ocfs2_la_recovery_item *item, *n; 1319 struct ocfs2_quota_recovery *qrec; 1320 enum ocfs2_orphan_reco_type orphan_reco_type; 1321 LIST_HEAD(tmp_la_list); 1322 1323 trace_ocfs2_complete_recovery( 1324 (unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno); 1325 1326 spin_lock(&journal->j_lock); 1327 list_splice_init(&journal->j_la_cleanups, &tmp_la_list); 1328 spin_unlock(&journal->j_lock); 1329 1330 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) { 1331 list_del_init(&item->lri_list); 1332 1333 ocfs2_wait_on_quotas(osb); 1334 1335 la_dinode = item->lri_la_dinode; 1336 tl_dinode = item->lri_tl_dinode; 1337 qrec = item->lri_qrec; 1338 orphan_reco_type = item->lri_orphan_reco_type; 1339 1340 trace_ocfs2_complete_recovery_slot(item->lri_slot, 1341 la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0, 1342 tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0, 1343 qrec); 1344 1345 if (la_dinode) { 1346 ret = ocfs2_complete_local_alloc_recovery(osb, 1347 la_dinode); 1348 if (ret < 0) 1349 mlog_errno(ret); 1350 1351 kfree(la_dinode); 1352 } 1353 1354 if (tl_dinode) { 1355 ret = ocfs2_complete_truncate_log_recovery(osb, 1356 tl_dinode); 1357 if (ret < 0) 1358 mlog_errno(ret); 1359 1360 kfree(tl_dinode); 1361 } 1362 1363 ret = ocfs2_recover_orphans(osb, item->lri_slot, 1364 orphan_reco_type); 1365 if (ret < 0) 1366 mlog_errno(ret); 1367 1368 if (qrec) { 1369 ret = ocfs2_finish_quota_recovery(osb, qrec, 1370 item->lri_slot); 1371 if (ret < 0) 1372 mlog_errno(ret); 1373 /* Recovery info is already freed now */ 1374 } 1375 1376 kfree(item); 1377 } 1378 1379 trace_ocfs2_complete_recovery_end(ret); 1380 } 1381 1382 /* NOTE: This function always eats your references to la_dinode and 1383 * tl_dinode, either manually on error, or by passing them to 1384 * ocfs2_complete_recovery */ 1385 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal, 1386 int slot_num, 1387 struct ocfs2_dinode *la_dinode, 1388 struct ocfs2_dinode *tl_dinode, 1389 struct ocfs2_quota_recovery *qrec, 1390 enum ocfs2_orphan_reco_type orphan_reco_type) 1391 { 1392 struct ocfs2_la_recovery_item *item; 1393 1394 item = kmalloc_obj(struct ocfs2_la_recovery_item, GFP_NOFS); 1395 if (!item) { 1396 /* Though we wish to avoid it, we are in fact safe in 1397 * skipping local alloc cleanup as fsck.ocfs2 is more 1398 * than capable of reclaiming unused space. */ 1399 kfree(la_dinode); 1400 kfree(tl_dinode); 1401 1402 if (qrec) 1403 ocfs2_free_quota_recovery(qrec); 1404 1405 mlog_errno(-ENOMEM); 1406 return; 1407 } 1408 1409 INIT_LIST_HEAD(&item->lri_list); 1410 item->lri_la_dinode = la_dinode; 1411 item->lri_slot = slot_num; 1412 item->lri_tl_dinode = tl_dinode; 1413 item->lri_qrec = qrec; 1414 item->lri_orphan_reco_type = orphan_reco_type; 1415 1416 spin_lock(&journal->j_lock); 1417 list_add_tail(&item->lri_list, &journal->j_la_cleanups); 1418 queue_work(journal->j_osb->ocfs2_wq, &journal->j_recovery_work); 1419 spin_unlock(&journal->j_lock); 1420 } 1421 1422 /* Called by the mount code to queue recovery the last part of 1423 * recovery for it's own and offline slot(s). */ 1424 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb) 1425 { 1426 struct ocfs2_journal *journal = osb->journal; 1427 1428 if (ocfs2_is_hard_readonly(osb)) 1429 return; 1430 1431 /* No need to queue up our truncate_log as regular cleanup will catch 1432 * that */ 1433 ocfs2_queue_recovery_completion(journal, osb->slot_num, 1434 osb->local_alloc_copy, NULL, NULL, 1435 ORPHAN_NEED_TRUNCATE); 1436 ocfs2_schedule_truncate_log_flush(osb, 0); 1437 1438 osb->local_alloc_copy = NULL; 1439 1440 /* queue to recover orphan slots for all offline slots */ 1441 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED); 1442 ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE); 1443 ocfs2_free_replay_slots(osb); 1444 } 1445 1446 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb) 1447 { 1448 if (osb->quota_rec) { 1449 ocfs2_queue_recovery_completion(osb->journal, 1450 osb->slot_num, 1451 NULL, 1452 NULL, 1453 osb->quota_rec, 1454 ORPHAN_NEED_TRUNCATE); 1455 osb->quota_rec = NULL; 1456 } 1457 } 1458 1459 static int __ocfs2_recovery_thread(void *arg) 1460 { 1461 int status, node_num, slot_num; 1462 struct ocfs2_super *osb = arg; 1463 struct ocfs2_recovery_map *rm = osb->recovery_map; 1464 int *rm_quota = NULL; 1465 int rm_quota_used = 0, i; 1466 struct ocfs2_quota_recovery *qrec; 1467 1468 /* Whether the quota supported. */ 1469 int quota_enabled = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, 1470 OCFS2_FEATURE_RO_COMPAT_USRQUOTA) 1471 || OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb, 1472 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA); 1473 1474 status = ocfs2_wait_on_mount(osb); 1475 if (status < 0) { 1476 goto bail; 1477 } 1478 1479 if (quota_enabled) { 1480 rm_quota = kzalloc_objs(int, osb->max_slots, GFP_NOFS); 1481 if (!rm_quota) { 1482 status = -ENOMEM; 1483 goto bail; 1484 } 1485 } 1486 restart: 1487 if (quota_enabled) { 1488 mutex_lock(&osb->recovery_lock); 1489 /* Confirm that recovery thread will no longer recover quotas */ 1490 if (osb->recovery_state == OCFS2_REC_QUOTA_WANT_DISABLE) { 1491 osb->recovery_state = OCFS2_REC_QUOTA_DISABLED; 1492 wake_up(&osb->recovery_event); 1493 } 1494 if (osb->recovery_state >= OCFS2_REC_QUOTA_DISABLED) 1495 quota_enabled = 0; 1496 mutex_unlock(&osb->recovery_lock); 1497 } 1498 1499 status = ocfs2_super_lock(osb, 1); 1500 if (status < 0) { 1501 mlog_errno(status); 1502 goto bail; 1503 } 1504 1505 status = ocfs2_compute_replay_slots(osb); 1506 if (status < 0) 1507 mlog_errno(status); 1508 1509 /* queue recovery for our own slot */ 1510 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL, 1511 NULL, NULL, ORPHAN_NO_NEED_TRUNCATE); 1512 1513 spin_lock(&osb->osb_lock); 1514 while (rm->rm_used) { 1515 /* It's always safe to remove entry zero, as we won't 1516 * clear it until ocfs2_recover_node() has succeeded. */ 1517 node_num = rm->rm_entries[0]; 1518 spin_unlock(&osb->osb_lock); 1519 slot_num = ocfs2_node_num_to_slot(osb, node_num); 1520 trace_ocfs2_recovery_thread_node(node_num, slot_num); 1521 if (slot_num == -ENOENT) { 1522 status = 0; 1523 goto skip_recovery; 1524 } 1525 1526 /* It is a bit subtle with quota recovery. We cannot do it 1527 * immediately because we have to obtain cluster locks from 1528 * quota files and we also don't want to just skip it because 1529 * then quota usage would be out of sync until some node takes 1530 * the slot. So we remember which nodes need quota recovery 1531 * and when everything else is done, we recover quotas. */ 1532 if (quota_enabled) { 1533 for (i = 0; i < rm_quota_used 1534 && rm_quota[i] != slot_num; i++) 1535 ; 1536 1537 if (i == rm_quota_used) 1538 rm_quota[rm_quota_used++] = slot_num; 1539 } 1540 1541 status = ocfs2_recover_node(osb, node_num, slot_num); 1542 skip_recovery: 1543 if (!status) { 1544 ocfs2_recovery_map_clear(osb, node_num); 1545 } else { 1546 mlog(ML_ERROR, 1547 "Error %d recovering node %d on device (%u,%u)!\n", 1548 status, node_num, 1549 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev)); 1550 mlog(ML_ERROR, "Volume requires unmount.\n"); 1551 } 1552 1553 spin_lock(&osb->osb_lock); 1554 } 1555 spin_unlock(&osb->osb_lock); 1556 trace_ocfs2_recovery_thread_end(status); 1557 1558 /* Refresh all journal recovery generations from disk */ 1559 status = ocfs2_check_journals_nolocks(osb); 1560 status = (status == -EROFS) ? 0 : status; 1561 if (status < 0) 1562 mlog_errno(status); 1563 1564 /* Now it is right time to recover quotas... We have to do this under 1565 * superblock lock so that no one can start using the slot (and crash) 1566 * before we recover it */ 1567 if (quota_enabled) { 1568 for (i = 0; i < rm_quota_used; i++) { 1569 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]); 1570 if (IS_ERR(qrec)) { 1571 status = PTR_ERR(qrec); 1572 mlog_errno(status); 1573 continue; 1574 } 1575 ocfs2_queue_recovery_completion(osb->journal, 1576 rm_quota[i], 1577 NULL, NULL, qrec, 1578 ORPHAN_NEED_TRUNCATE); 1579 } 1580 } 1581 1582 ocfs2_super_unlock(osb, 1); 1583 1584 /* queue recovery for offline slots */ 1585 ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE); 1586 1587 bail: 1588 mutex_lock(&osb->recovery_lock); 1589 if (!status && !ocfs2_recovery_completed(osb)) { 1590 mutex_unlock(&osb->recovery_lock); 1591 goto restart; 1592 } 1593 1594 ocfs2_free_replay_slots(osb); 1595 osb->recovery_thread_task = NULL; 1596 if (osb->recovery_state == OCFS2_REC_WANT_DISABLE) 1597 osb->recovery_state = OCFS2_REC_DISABLED; 1598 wake_up(&osb->recovery_event); 1599 1600 mutex_unlock(&osb->recovery_lock); 1601 1602 kfree(rm_quota); 1603 1604 return status; 1605 } 1606 1607 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num) 1608 { 1609 int was_set = -1; 1610 1611 mutex_lock(&osb->recovery_lock); 1612 if (osb->recovery_state < OCFS2_REC_WANT_DISABLE) 1613 was_set = ocfs2_recovery_map_set(osb, node_num); 1614 1615 trace_ocfs2_recovery_thread(node_num, osb->node_num, 1616 osb->recovery_state, osb->recovery_thread_task, was_set); 1617 1618 if (osb->recovery_state >= OCFS2_REC_WANT_DISABLE) 1619 goto out; 1620 1621 if (osb->recovery_thread_task) 1622 goto out; 1623 1624 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb, 1625 "ocfs2rec-%s", osb->uuid_str); 1626 if (IS_ERR(osb->recovery_thread_task)) { 1627 mlog_errno((int)PTR_ERR(osb->recovery_thread_task)); 1628 osb->recovery_thread_task = NULL; 1629 } 1630 1631 out: 1632 mutex_unlock(&osb->recovery_lock); 1633 wake_up(&osb->recovery_event); 1634 } 1635 1636 static int ocfs2_read_journal_inode(struct ocfs2_super *osb, 1637 int slot_num, 1638 struct buffer_head **bh, 1639 struct inode **ret_inode) 1640 { 1641 int status = -EACCES; 1642 struct inode *inode = NULL; 1643 1644 BUG_ON(slot_num >= osb->max_slots); 1645 1646 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, 1647 slot_num); 1648 if (!inode || is_bad_inode(inode)) { 1649 mlog_errno(status); 1650 goto bail; 1651 } 1652 SET_INODE_JOURNAL(inode); 1653 1654 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE); 1655 if (status < 0) { 1656 mlog_errno(status); 1657 goto bail; 1658 } 1659 1660 status = 0; 1661 1662 bail: 1663 if (inode) { 1664 if (status || !ret_inode) 1665 iput(inode); 1666 else 1667 *ret_inode = inode; 1668 } 1669 return status; 1670 } 1671 1672 /* Does the actual journal replay and marks the journal inode as 1673 * clean. Will only replay if the journal inode is marked dirty. */ 1674 static int ocfs2_replay_journal(struct ocfs2_super *osb, 1675 int node_num, 1676 int slot_num) 1677 { 1678 int status; 1679 int got_lock = 0; 1680 unsigned int flags; 1681 struct inode *inode = NULL; 1682 struct ocfs2_dinode *fe; 1683 journal_t *journal = NULL; 1684 struct buffer_head *bh = NULL; 1685 u32 slot_reco_gen; 1686 1687 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode); 1688 if (status) { 1689 mlog_errno(status); 1690 goto done; 1691 } 1692 1693 fe = (struct ocfs2_dinode *)bh->b_data; 1694 slot_reco_gen = ocfs2_get_recovery_generation(fe); 1695 brelse(bh); 1696 bh = NULL; 1697 1698 /* 1699 * As the fs recovery is asynchronous, there is a small chance that 1700 * another node mounted (and recovered) the slot before the recovery 1701 * thread could get the lock. To handle that, we dirty read the journal 1702 * inode for that slot to get the recovery generation. If it is 1703 * different than what we expected, the slot has been recovered. 1704 * If not, it needs recovery. 1705 */ 1706 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) { 1707 trace_ocfs2_replay_journal_recovered(slot_num, 1708 osb->slot_recovery_generations[slot_num], slot_reco_gen); 1709 osb->slot_recovery_generations[slot_num] = slot_reco_gen; 1710 status = -EBUSY; 1711 goto done; 1712 } 1713 1714 /* Continue with recovery as the journal has not yet been recovered */ 1715 1716 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY); 1717 if (status < 0) { 1718 trace_ocfs2_replay_journal_lock_err(status); 1719 if (status != -ERESTARTSYS) 1720 mlog(ML_ERROR, "Could not lock journal!\n"); 1721 goto done; 1722 } 1723 got_lock = 1; 1724 1725 fe = (struct ocfs2_dinode *) bh->b_data; 1726 1727 flags = le32_to_cpu(fe->id1.journal1.ij_flags); 1728 slot_reco_gen = ocfs2_get_recovery_generation(fe); 1729 1730 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) { 1731 trace_ocfs2_replay_journal_skip(node_num); 1732 /* Refresh recovery generation for the slot */ 1733 osb->slot_recovery_generations[slot_num] = slot_reco_gen; 1734 goto done; 1735 } 1736 1737 /* we need to run complete recovery for offline orphan slots */ 1738 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED); 1739 1740 printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\ 1741 "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev), 1742 MINOR(osb->sb->s_dev)); 1743 1744 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 1745 1746 status = ocfs2_force_read_journal(inode); 1747 if (status < 0) { 1748 mlog_errno(status); 1749 goto done; 1750 } 1751 1752 journal = jbd2_journal_init_inode(inode); 1753 if (IS_ERR(journal)) { 1754 mlog(ML_ERROR, "Linux journal layer error\n"); 1755 status = PTR_ERR(journal); 1756 goto done; 1757 } 1758 1759 status = jbd2_journal_load(journal); 1760 if (status < 0) { 1761 mlog_errno(status); 1762 BUG_ON(!igrab(inode)); 1763 jbd2_journal_destroy(journal); 1764 goto done; 1765 } 1766 1767 ocfs2_clear_journal_error(osb->sb, journal, slot_num); 1768 1769 /* wipe the journal */ 1770 jbd2_journal_lock_updates(journal); 1771 status = jbd2_journal_flush(journal, 0); 1772 jbd2_journal_unlock_updates(journal); 1773 if (status < 0) 1774 mlog_errno(status); 1775 1776 /* This will mark the node clean */ 1777 flags = le32_to_cpu(fe->id1.journal1.ij_flags); 1778 flags &= ~OCFS2_JOURNAL_DIRTY_FL; 1779 fe->id1.journal1.ij_flags = cpu_to_le32(flags); 1780 1781 /* Increment recovery generation to indicate successful recovery */ 1782 ocfs2_bump_recovery_generation(fe); 1783 osb->slot_recovery_generations[slot_num] = 1784 ocfs2_get_recovery_generation(fe); 1785 1786 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check); 1787 status = ocfs2_write_block(osb, bh, INODE_CACHE(inode)); 1788 if (status < 0) 1789 mlog_errno(status); 1790 1791 BUG_ON(!igrab(inode)); 1792 1793 jbd2_journal_destroy(journal); 1794 1795 printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\ 1796 "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev), 1797 MINOR(osb->sb->s_dev)); 1798 done: 1799 /* drop the lock on this nodes journal */ 1800 if (got_lock) 1801 ocfs2_inode_unlock(inode, 1); 1802 1803 iput(inode); 1804 brelse(bh); 1805 1806 return status; 1807 } 1808 1809 /* 1810 * Do the most important parts of node recovery: 1811 * - Replay it's journal 1812 * - Stamp a clean local allocator file 1813 * - Stamp a clean truncate log 1814 * - Mark the node clean 1815 * 1816 * If this function completes without error, a node in OCFS2 can be 1817 * said to have been safely recovered. As a result, failure during the 1818 * second part of a nodes recovery process (local alloc recovery) is 1819 * far less concerning. 1820 */ 1821 static int ocfs2_recover_node(struct ocfs2_super *osb, 1822 int node_num, int slot_num) 1823 { 1824 int status = 0; 1825 struct ocfs2_dinode *la_copy = NULL; 1826 struct ocfs2_dinode *tl_copy = NULL; 1827 1828 trace_ocfs2_recover_node(node_num, slot_num, osb->node_num); 1829 1830 /* Should not ever be called to recover ourselves -- in that 1831 * case we should've called ocfs2_journal_load instead. */ 1832 BUG_ON(osb->node_num == node_num); 1833 1834 status = ocfs2_replay_journal(osb, node_num, slot_num); 1835 if (status < 0) { 1836 if (status == -EBUSY) { 1837 trace_ocfs2_recover_node_skip(slot_num, node_num); 1838 status = 0; 1839 goto done; 1840 } 1841 mlog_errno(status); 1842 goto done; 1843 } 1844 1845 /* Stamp a clean local alloc file AFTER recovering the journal... */ 1846 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy); 1847 if (status < 0) { 1848 mlog_errno(status); 1849 goto done; 1850 } 1851 1852 /* An error from begin_truncate_log_recovery is not 1853 * serious enough to warrant halting the rest of 1854 * recovery. */ 1855 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy); 1856 if (status < 0) 1857 mlog_errno(status); 1858 1859 /* Likewise, this would be a strange but ultimately not so 1860 * harmful place to get an error... */ 1861 status = ocfs2_clear_slot(osb, slot_num); 1862 if (status < 0) 1863 mlog_errno(status); 1864 1865 /* This will kfree the memory pointed to by la_copy and tl_copy */ 1866 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy, 1867 tl_copy, NULL, ORPHAN_NEED_TRUNCATE); 1868 1869 status = 0; 1870 done: 1871 1872 return status; 1873 } 1874 1875 /* Test node liveness by trylocking his journal. If we get the lock, 1876 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is 1877 * still alive (we couldn't get the lock) and < 0 on error. */ 1878 static int ocfs2_trylock_journal(struct ocfs2_super *osb, 1879 int slot_num) 1880 { 1881 int status, flags; 1882 struct inode *inode = NULL; 1883 1884 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, 1885 slot_num); 1886 if (inode == NULL) { 1887 mlog(ML_ERROR, "access error\n"); 1888 status = -EACCES; 1889 goto bail; 1890 } 1891 if (is_bad_inode(inode)) { 1892 mlog(ML_ERROR, "access error (bad inode)\n"); 1893 iput(inode); 1894 inode = NULL; 1895 status = -EACCES; 1896 goto bail; 1897 } 1898 SET_INODE_JOURNAL(inode); 1899 1900 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE; 1901 status = ocfs2_inode_lock_full(inode, NULL, 1, flags); 1902 if (status < 0) { 1903 if (status != -EAGAIN) 1904 mlog_errno(status); 1905 goto bail; 1906 } 1907 1908 ocfs2_inode_unlock(inode, 1); 1909 bail: 1910 iput(inode); 1911 1912 return status; 1913 } 1914 1915 /* Call this underneath ocfs2_super_lock. It also assumes that the 1916 * slot info struct has been updated from disk. */ 1917 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb) 1918 { 1919 unsigned int node_num; 1920 int status, i; 1921 u32 gen; 1922 struct buffer_head *bh = NULL; 1923 struct ocfs2_dinode *di; 1924 1925 /* This is called with the super block cluster lock, so we 1926 * know that the slot map can't change underneath us. */ 1927 1928 for (i = 0; i < osb->max_slots; i++) { 1929 /* Read journal inode to get the recovery generation */ 1930 status = ocfs2_read_journal_inode(osb, i, &bh, NULL); 1931 if (status) { 1932 mlog_errno(status); 1933 goto bail; 1934 } 1935 di = (struct ocfs2_dinode *)bh->b_data; 1936 gen = ocfs2_get_recovery_generation(di); 1937 brelse(bh); 1938 bh = NULL; 1939 1940 spin_lock(&osb->osb_lock); 1941 osb->slot_recovery_generations[i] = gen; 1942 1943 trace_ocfs2_mark_dead_nodes(i, 1944 osb->slot_recovery_generations[i]); 1945 1946 if (i == osb->slot_num) { 1947 spin_unlock(&osb->osb_lock); 1948 continue; 1949 } 1950 1951 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num); 1952 if (status == -ENOENT) { 1953 spin_unlock(&osb->osb_lock); 1954 continue; 1955 } 1956 1957 if (__ocfs2_recovery_map_test(osb, node_num)) { 1958 spin_unlock(&osb->osb_lock); 1959 continue; 1960 } 1961 spin_unlock(&osb->osb_lock); 1962 1963 /* Ok, we have a slot occupied by another node which 1964 * is not in the recovery map. We trylock his journal 1965 * file here to test if he's alive. */ 1966 status = ocfs2_trylock_journal(osb, i); 1967 if (!status) { 1968 /* Since we're called from mount, we know that 1969 * the recovery thread can't race us on 1970 * setting / checking the recovery bits. */ 1971 ocfs2_recovery_thread(osb, node_num); 1972 } else if ((status < 0) && (status != -EAGAIN)) { 1973 mlog_errno(status); 1974 goto bail; 1975 } 1976 } 1977 1978 status = 0; 1979 bail: 1980 return status; 1981 } 1982 1983 /* 1984 * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some 1985 * randomness to the timeout to minimize multiple nodes firing the timer at the 1986 * same time. 1987 */ 1988 static inline unsigned long ocfs2_orphan_scan_timeout(void) 1989 { 1990 unsigned long time; 1991 1992 get_random_bytes(&time, sizeof(time)); 1993 time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000); 1994 return msecs_to_jiffies(time); 1995 } 1996 1997 /* 1998 * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for 1999 * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This 2000 * is done to catch any orphans that are left over in orphan directories. 2001 * 2002 * It scans all slots, even ones that are in use. It does so to handle the 2003 * case described below: 2004 * 2005 * Node 1 has an inode it was using. The dentry went away due to memory 2006 * pressure. Node 1 closes the inode, but it's on the free list. The node 2007 * has the open lock. 2008 * Node 2 unlinks the inode. It grabs the dentry lock to notify others, 2009 * but node 1 has no dentry and doesn't get the message. It trylocks the 2010 * open lock, sees that another node has a PR, and does nothing. 2011 * Later node 2 runs its orphan dir. It igets the inode, trylocks the 2012 * open lock, sees the PR still, and does nothing. 2013 * Basically, we have to trigger an orphan iput on node 1. The only way 2014 * for this to happen is if node 1 runs node 2's orphan dir. 2015 * 2016 * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT 2017 * seconds. It gets an EX lock on os_lockres and checks sequence number 2018 * stored in LVB. If the sequence number has changed, it means some other 2019 * node has done the scan. This node skips the scan and tracks the 2020 * sequence number. If the sequence number didn't change, it means a scan 2021 * hasn't happened. The node queues a scan and increments the 2022 * sequence number in the LVB. 2023 */ 2024 static void ocfs2_queue_orphan_scan(struct ocfs2_super *osb) 2025 { 2026 struct ocfs2_orphan_scan *os; 2027 int status, i; 2028 u32 seqno = 0; 2029 2030 os = &osb->osb_orphan_scan; 2031 2032 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE) 2033 goto out; 2034 2035 trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno, 2036 atomic_read(&os->os_state)); 2037 2038 status = ocfs2_orphan_scan_lock(osb, &seqno); 2039 if (status < 0) { 2040 if (status != -EAGAIN) 2041 mlog_errno(status); 2042 goto out; 2043 } 2044 2045 /* Do no queue the tasks if the volume is being umounted */ 2046 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE) 2047 goto unlock; 2048 2049 if (os->os_seqno != seqno) { 2050 os->os_seqno = seqno; 2051 goto unlock; 2052 } 2053 2054 for (i = 0; i < osb->max_slots; i++) 2055 ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL, 2056 NULL, ORPHAN_NO_NEED_TRUNCATE); 2057 /* 2058 * We queued a recovery on orphan slots, increment the sequence 2059 * number and update LVB so other node will skip the scan for a while 2060 */ 2061 seqno++; 2062 os->os_count++; 2063 os->os_scantime = ktime_get_seconds(); 2064 unlock: 2065 ocfs2_orphan_scan_unlock(osb, seqno); 2066 out: 2067 trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno, 2068 atomic_read(&os->os_state)); 2069 return; 2070 } 2071 2072 /* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */ 2073 static void ocfs2_orphan_scan_work(struct work_struct *work) 2074 { 2075 struct ocfs2_orphan_scan *os; 2076 struct ocfs2_super *osb; 2077 2078 os = container_of(work, struct ocfs2_orphan_scan, 2079 os_orphan_scan_work.work); 2080 osb = os->os_osb; 2081 2082 mutex_lock(&os->os_lock); 2083 ocfs2_queue_orphan_scan(osb); 2084 if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) 2085 queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work, 2086 ocfs2_orphan_scan_timeout()); 2087 mutex_unlock(&os->os_lock); 2088 } 2089 2090 void ocfs2_orphan_scan_stop(struct ocfs2_super *osb) 2091 { 2092 struct ocfs2_orphan_scan *os; 2093 2094 os = &osb->osb_orphan_scan; 2095 if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) { 2096 atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE); 2097 mutex_lock(&os->os_lock); 2098 cancel_delayed_work(&os->os_orphan_scan_work); 2099 mutex_unlock(&os->os_lock); 2100 } 2101 } 2102 2103 void ocfs2_orphan_scan_init(struct ocfs2_super *osb) 2104 { 2105 struct ocfs2_orphan_scan *os; 2106 2107 os = &osb->osb_orphan_scan; 2108 os->os_osb = osb; 2109 os->os_count = 0; 2110 os->os_seqno = 0; 2111 mutex_init(&os->os_lock); 2112 INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work); 2113 } 2114 2115 void ocfs2_orphan_scan_start(struct ocfs2_super *osb) 2116 { 2117 struct ocfs2_orphan_scan *os; 2118 2119 os = &osb->osb_orphan_scan; 2120 os->os_scantime = ktime_get_seconds(); 2121 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb)) 2122 atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE); 2123 else { 2124 atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE); 2125 queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work, 2126 ocfs2_orphan_scan_timeout()); 2127 } 2128 } 2129 2130 struct ocfs2_orphan_filldir_priv { 2131 struct dir_context ctx; 2132 struct inode *head; 2133 struct ocfs2_super *osb; 2134 enum ocfs2_orphan_reco_type orphan_reco_type; 2135 }; 2136 2137 static bool ocfs2_orphan_filldir(struct dir_context *ctx, const char *name, 2138 int name_len, loff_t pos, u64 ino, 2139 unsigned type) 2140 { 2141 struct ocfs2_orphan_filldir_priv *p = 2142 container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx); 2143 struct inode *iter; 2144 2145 if (name_len == 1 && !strncmp(".", name, 1)) 2146 return true; 2147 if (name_len == 2 && !strncmp("..", name, 2)) 2148 return true; 2149 2150 /* do not include dio entry in case of orphan scan */ 2151 if ((p->orphan_reco_type == ORPHAN_NO_NEED_TRUNCATE) && 2152 (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX, 2153 OCFS2_DIO_ORPHAN_PREFIX_LEN))) 2154 return true; 2155 2156 /* Skip bad inodes so that recovery can continue */ 2157 iter = ocfs2_iget(p->osb, ino, 2158 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0); 2159 if (IS_ERR(iter)) 2160 return true; 2161 2162 if (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX, 2163 OCFS2_DIO_ORPHAN_PREFIX_LEN)) 2164 OCFS2_I(iter)->ip_flags |= OCFS2_INODE_DIO_ORPHAN_ENTRY; 2165 2166 /* Skip inodes which are already added to recover list, since dio may 2167 * happen concurrently with unlink/rename */ 2168 if (OCFS2_I(iter)->ip_next_orphan) { 2169 iput(iter); 2170 return true; 2171 } 2172 2173 trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno); 2174 /* No locking is required for the next_orphan queue as there 2175 * is only ever a single process doing orphan recovery. */ 2176 OCFS2_I(iter)->ip_next_orphan = p->head; 2177 p->head = iter; 2178 2179 return true; 2180 } 2181 2182 static int ocfs2_queue_orphans(struct ocfs2_super *osb, 2183 int slot, 2184 struct inode **head, 2185 enum ocfs2_orphan_reco_type orphan_reco_type) 2186 { 2187 int status; 2188 struct inode *orphan_dir_inode = NULL; 2189 struct ocfs2_orphan_filldir_priv priv = { 2190 .ctx.actor = ocfs2_orphan_filldir, 2191 .osb = osb, 2192 .head = *head, 2193 .orphan_reco_type = orphan_reco_type 2194 }; 2195 2196 orphan_dir_inode = ocfs2_get_system_file_inode(osb, 2197 ORPHAN_DIR_SYSTEM_INODE, 2198 slot); 2199 if (!orphan_dir_inode) { 2200 status = -ENOENT; 2201 mlog_errno(status); 2202 return status; 2203 } 2204 2205 inode_lock(orphan_dir_inode); 2206 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0); 2207 if (status < 0) { 2208 mlog_errno(status); 2209 goto out; 2210 } 2211 2212 status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx); 2213 if (status) { 2214 mlog_errno(status); 2215 goto out_cluster; 2216 } 2217 2218 *head = priv.head; 2219 2220 out_cluster: 2221 ocfs2_inode_unlock(orphan_dir_inode, 0); 2222 out: 2223 inode_unlock(orphan_dir_inode); 2224 iput(orphan_dir_inode); 2225 return status; 2226 } 2227 2228 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb, 2229 int slot) 2230 { 2231 int ret; 2232 2233 spin_lock(&osb->osb_lock); 2234 ret = !osb->osb_orphan_wipes[slot]; 2235 spin_unlock(&osb->osb_lock); 2236 return ret; 2237 } 2238 2239 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb, 2240 int slot) 2241 { 2242 spin_lock(&osb->osb_lock); 2243 /* Mark ourselves such that new processes in delete_inode() 2244 * know to quit early. */ 2245 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot); 2246 while (osb->osb_orphan_wipes[slot]) { 2247 /* If any processes are already in the middle of an 2248 * orphan wipe on this dir, then we need to wait for 2249 * them. */ 2250 spin_unlock(&osb->osb_lock); 2251 wait_event_interruptible(osb->osb_wipe_event, 2252 ocfs2_orphan_recovery_can_continue(osb, slot)); 2253 spin_lock(&osb->osb_lock); 2254 } 2255 spin_unlock(&osb->osb_lock); 2256 } 2257 2258 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb, 2259 int slot) 2260 { 2261 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot); 2262 } 2263 2264 /* 2265 * Orphan recovery. Each mounted node has it's own orphan dir which we 2266 * must run during recovery. Our strategy here is to build a list of 2267 * the inodes in the orphan dir and iget/iput them. The VFS does 2268 * (most) of the rest of the work. 2269 * 2270 * Orphan recovery can happen at any time, not just mount so we have a 2271 * couple of extra considerations. 2272 * 2273 * - We grab as many inodes as we can under the orphan dir lock - 2274 * doing iget() outside the orphan dir risks getting a reference on 2275 * an invalid inode. 2276 * - We must be sure not to deadlock with other processes on the 2277 * system wanting to run delete_inode(). This can happen when they go 2278 * to lock the orphan dir and the orphan recovery process attempts to 2279 * iget() inside the orphan dir lock. This can be avoided by 2280 * advertising our state to ocfs2_delete_inode(). 2281 */ 2282 static int ocfs2_recover_orphans(struct ocfs2_super *osb, 2283 int slot, 2284 enum ocfs2_orphan_reco_type orphan_reco_type) 2285 { 2286 int ret = 0; 2287 struct inode *inode = NULL; 2288 struct inode *iter; 2289 struct ocfs2_inode_info *oi; 2290 struct buffer_head *di_bh = NULL; 2291 struct ocfs2_dinode *di = NULL; 2292 2293 trace_ocfs2_recover_orphans(slot); 2294 2295 ocfs2_mark_recovering_orphan_dir(osb, slot); 2296 ret = ocfs2_queue_orphans(osb, slot, &inode, orphan_reco_type); 2297 ocfs2_clear_recovering_orphan_dir(osb, slot); 2298 2299 /* Error here should be noted, but we want to continue with as 2300 * many queued inodes as we've got. */ 2301 if (ret) 2302 mlog_errno(ret); 2303 2304 while (inode) { 2305 oi = OCFS2_I(inode); 2306 trace_ocfs2_recover_orphans_iput( 2307 (unsigned long long)oi->ip_blkno); 2308 2309 iter = oi->ip_next_orphan; 2310 oi->ip_next_orphan = NULL; 2311 2312 if (oi->ip_flags & OCFS2_INODE_DIO_ORPHAN_ENTRY) { 2313 inode_lock(inode); 2314 ret = ocfs2_rw_lock(inode, 1); 2315 if (ret < 0) { 2316 mlog_errno(ret); 2317 goto unlock_mutex; 2318 } 2319 /* 2320 * We need to take and drop the inode lock to 2321 * force read inode from disk. 2322 */ 2323 ret = ocfs2_inode_lock(inode, &di_bh, 1); 2324 if (ret) { 2325 mlog_errno(ret); 2326 goto unlock_rw; 2327 } 2328 2329 di = (struct ocfs2_dinode *)di_bh->b_data; 2330 2331 if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) { 2332 ret = ocfs2_truncate_file(inode, di_bh, 2333 i_size_read(inode)); 2334 if (ret < 0) { 2335 if (ret != -ENOSPC) 2336 mlog_errno(ret); 2337 goto unlock_inode; 2338 } 2339 2340 ret = ocfs2_del_inode_from_orphan(osb, inode, 2341 di_bh, 0, 0); 2342 if (ret) 2343 mlog_errno(ret); 2344 } 2345 unlock_inode: 2346 ocfs2_inode_unlock(inode, 1); 2347 brelse(di_bh); 2348 di_bh = NULL; 2349 unlock_rw: 2350 ocfs2_rw_unlock(inode, 1); 2351 unlock_mutex: 2352 inode_unlock(inode); 2353 2354 /* clear dio flag in ocfs2_inode_info */ 2355 oi->ip_flags &= ~OCFS2_INODE_DIO_ORPHAN_ENTRY; 2356 } else { 2357 spin_lock(&oi->ip_lock); 2358 /* Set the proper information to get us going into 2359 * ocfs2_delete_inode. */ 2360 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED; 2361 spin_unlock(&oi->ip_lock); 2362 } 2363 2364 iput(inode); 2365 inode = iter; 2366 } 2367 2368 return ret; 2369 } 2370 2371 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota) 2372 { 2373 /* This check is good because ocfs2 will wait on our recovery 2374 * thread before changing it to something other than MOUNTED 2375 * or DISABLED. */ 2376 wait_event(osb->osb_mount_event, 2377 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) || 2378 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS || 2379 atomic_read(&osb->vol_state) == VOLUME_DISABLED); 2380 2381 /* If there's an error on mount, then we may never get to the 2382 * MOUNTED flag, but this is set right before 2383 * dismount_volume() so we can trust it. */ 2384 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) { 2385 trace_ocfs2_wait_on_mount(VOLUME_DISABLED); 2386 mlog(0, "mount error, exiting!\n"); 2387 return -EBUSY; 2388 } 2389 2390 return 0; 2391 } 2392 2393 static int ocfs2_commit_thread(void *arg) 2394 { 2395 int status; 2396 struct ocfs2_super *osb = arg; 2397 struct ocfs2_journal *journal = osb->journal; 2398 2399 /* we can trust j_num_trans here because _should_stop() is only set in 2400 * shutdown and nobody other than ourselves should be able to start 2401 * transactions. committing on shutdown might take a few iterations 2402 * as final transactions put deleted inodes on the list */ 2403 while (!(kthread_should_stop() && 2404 atomic_read(&journal->j_num_trans) == 0)) { 2405 2406 wait_event_interruptible(osb->checkpoint_event, 2407 atomic_read(&journal->j_num_trans) 2408 || kthread_should_stop()); 2409 2410 status = ocfs2_commit_cache(osb); 2411 if (status < 0) { 2412 static unsigned long abort_warn_time; 2413 2414 /* Warn about this once per minute */ 2415 if (printk_timed_ratelimit(&abort_warn_time, 60*HZ)) 2416 mlog(ML_ERROR, "status = %d, journal is " 2417 "already aborted.\n", status); 2418 /* 2419 * After ocfs2_commit_cache() fails, j_num_trans has a 2420 * non-zero value. Sleep here to avoid a busy-wait 2421 * loop. 2422 */ 2423 msleep_interruptible(1000); 2424 } 2425 2426 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){ 2427 mlog(ML_KTHREAD, 2428 "commit_thread: %u transactions pending on " 2429 "shutdown\n", 2430 atomic_read(&journal->j_num_trans)); 2431 } 2432 } 2433 2434 return 0; 2435 } 2436 2437 /* Reads all the journal inodes without taking any cluster locks. Used 2438 * for hard readonly access to determine whether any journal requires 2439 * recovery. Also used to refresh the recovery generation numbers after 2440 * a journal has been recovered by another node. 2441 */ 2442 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb) 2443 { 2444 int ret = 0; 2445 unsigned int slot; 2446 struct buffer_head *di_bh = NULL; 2447 struct ocfs2_dinode *di; 2448 int journal_dirty = 0; 2449 2450 for(slot = 0; slot < osb->max_slots; slot++) { 2451 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL); 2452 if (ret) { 2453 mlog_errno(ret); 2454 goto out; 2455 } 2456 2457 di = (struct ocfs2_dinode *) di_bh->b_data; 2458 2459 osb->slot_recovery_generations[slot] = 2460 ocfs2_get_recovery_generation(di); 2461 2462 if (le32_to_cpu(di->id1.journal1.ij_flags) & 2463 OCFS2_JOURNAL_DIRTY_FL) 2464 journal_dirty = 1; 2465 2466 brelse(di_bh); 2467 di_bh = NULL; 2468 } 2469 2470 out: 2471 if (journal_dirty) 2472 ret = -EROFS; 2473 return ret; 2474 } 2475