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