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