1 /* -*- mode: c; c-basic-offset: 8; -*- 2 * vim: noexpandtab sw=8 ts=8 sts=0: 3 * 4 * journal.c 5 * 6 * Defines functions of journalling api 7 * 8 * Copyright (C) 2003, 2004 Oracle. All rights reserved. 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public 12 * License as published by the Free Software Foundation; either 13 * version 2 of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public 21 * License along with this program; if not, write to the 22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23 * Boston, MA 021110-1307, USA. 24 */ 25 26 #include <linux/fs.h> 27 #include <linux/types.h> 28 #include <linux/slab.h> 29 #include <linux/highmem.h> 30 #include <linux/kthread.h> 31 32 #define MLOG_MASK_PREFIX ML_JOURNAL 33 #include <cluster/masklog.h> 34 35 #include "ocfs2.h" 36 37 #include "alloc.h" 38 #include "blockcheck.h" 39 #include "dir.h" 40 #include "dlmglue.h" 41 #include "extent_map.h" 42 #include "heartbeat.h" 43 #include "inode.h" 44 #include "journal.h" 45 #include "localalloc.h" 46 #include "slot_map.h" 47 #include "super.h" 48 #include "sysfile.h" 49 #include "quota.h" 50 51 #include "buffer_head_io.h" 52 53 DEFINE_SPINLOCK(trans_inc_lock); 54 55 static int ocfs2_force_read_journal(struct inode *inode); 56 static int ocfs2_recover_node(struct ocfs2_super *osb, 57 int node_num, int slot_num); 58 static int __ocfs2_recovery_thread(void *arg); 59 static int ocfs2_commit_cache(struct ocfs2_super *osb); 60 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota); 61 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb, 62 int dirty, int replayed); 63 static int ocfs2_trylock_journal(struct ocfs2_super *osb, 64 int slot_num); 65 static int ocfs2_recover_orphans(struct ocfs2_super *osb, 66 int slot); 67 static int ocfs2_commit_thread(void *arg); 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 81 /* 82 * The recovery_list is a simple linked list of node numbers to recover. 83 * It is protected by the recovery_lock. 84 */ 85 86 struct ocfs2_recovery_map { 87 unsigned int rm_used; 88 unsigned int *rm_entries; 89 }; 90 91 int ocfs2_recovery_init(struct ocfs2_super *osb) 92 { 93 struct ocfs2_recovery_map *rm; 94 95 mutex_init(&osb->recovery_lock); 96 osb->disable_recovery = 0; 97 osb->recovery_thread_task = NULL; 98 init_waitqueue_head(&osb->recovery_event); 99 100 rm = kzalloc(sizeof(struct ocfs2_recovery_map) + 101 osb->max_slots * sizeof(unsigned int), 102 GFP_KERNEL); 103 if (!rm) { 104 mlog_errno(-ENOMEM); 105 return -ENOMEM; 106 } 107 108 rm->rm_entries = (unsigned int *)((char *)rm + 109 sizeof(struct ocfs2_recovery_map)); 110 osb->recovery_map = rm; 111 112 return 0; 113 } 114 115 /* we can't grab the goofy sem lock from inside wait_event, so we use 116 * memory barriers to make sure that we'll see the null task before 117 * being woken up */ 118 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb) 119 { 120 mb(); 121 return osb->recovery_thread_task != NULL; 122 } 123 124 void ocfs2_recovery_exit(struct ocfs2_super *osb) 125 { 126 struct ocfs2_recovery_map *rm; 127 128 /* disable any new recovery threads and wait for any currently 129 * running ones to exit. Do this before setting the vol_state. */ 130 mutex_lock(&osb->recovery_lock); 131 osb->disable_recovery = 1; 132 mutex_unlock(&osb->recovery_lock); 133 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb)); 134 135 /* At this point, we know that no more recovery threads can be 136 * launched, so wait for any recovery completion work to 137 * complete. */ 138 flush_workqueue(ocfs2_wq); 139 140 /* 141 * Now that recovery is shut down, and the osb is about to be 142 * freed, the osb_lock is not taken here. 143 */ 144 rm = osb->recovery_map; 145 /* XXX: Should we bug if there are dirty entries? */ 146 147 kfree(rm); 148 } 149 150 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb, 151 unsigned int node_num) 152 { 153 int i; 154 struct ocfs2_recovery_map *rm = osb->recovery_map; 155 156 assert_spin_locked(&osb->osb_lock); 157 158 for (i = 0; i < rm->rm_used; i++) { 159 if (rm->rm_entries[i] == node_num) 160 return 1; 161 } 162 163 return 0; 164 } 165 166 /* Behaves like test-and-set. Returns the previous value */ 167 static int ocfs2_recovery_map_set(struct ocfs2_super *osb, 168 unsigned int node_num) 169 { 170 struct ocfs2_recovery_map *rm = osb->recovery_map; 171 172 spin_lock(&osb->osb_lock); 173 if (__ocfs2_recovery_map_test(osb, node_num)) { 174 spin_unlock(&osb->osb_lock); 175 return 1; 176 } 177 178 /* XXX: Can this be exploited? Not from o2dlm... */ 179 BUG_ON(rm->rm_used >= osb->max_slots); 180 181 rm->rm_entries[rm->rm_used] = node_num; 182 rm->rm_used++; 183 spin_unlock(&osb->osb_lock); 184 185 return 0; 186 } 187 188 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb, 189 unsigned int node_num) 190 { 191 int i; 192 struct ocfs2_recovery_map *rm = osb->recovery_map; 193 194 spin_lock(&osb->osb_lock); 195 196 for (i = 0; i < rm->rm_used; i++) { 197 if (rm->rm_entries[i] == node_num) 198 break; 199 } 200 201 if (i < rm->rm_used) { 202 /* XXX: be careful with the pointer math */ 203 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]), 204 (rm->rm_used - i - 1) * sizeof(unsigned int)); 205 rm->rm_used--; 206 } 207 208 spin_unlock(&osb->osb_lock); 209 } 210 211 static int ocfs2_commit_cache(struct ocfs2_super *osb) 212 { 213 int status = 0; 214 unsigned int flushed; 215 unsigned long old_id; 216 struct ocfs2_journal *journal = NULL; 217 218 mlog_entry_void(); 219 220 journal = osb->journal; 221 222 /* Flush all pending commits and checkpoint the journal. */ 223 down_write(&journal->j_trans_barrier); 224 225 if (atomic_read(&journal->j_num_trans) == 0) { 226 up_write(&journal->j_trans_barrier); 227 mlog(0, "No transactions for me to flush!\n"); 228 goto finally; 229 } 230 231 jbd2_journal_lock_updates(journal->j_journal); 232 status = jbd2_journal_flush(journal->j_journal); 233 jbd2_journal_unlock_updates(journal->j_journal); 234 if (status < 0) { 235 up_write(&journal->j_trans_barrier); 236 mlog_errno(status); 237 goto finally; 238 } 239 240 old_id = ocfs2_inc_trans_id(journal); 241 242 flushed = atomic_read(&journal->j_num_trans); 243 atomic_set(&journal->j_num_trans, 0); 244 up_write(&journal->j_trans_barrier); 245 246 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n", 247 journal->j_trans_id, flushed); 248 249 ocfs2_wake_downconvert_thread(osb); 250 wake_up(&journal->j_checkpointed); 251 finally: 252 mlog_exit(status); 253 return status; 254 } 255 256 /* pass it NULL and it will allocate a new handle object for you. If 257 * you pass it a handle however, it may still return error, in which 258 * case it has free'd the passed handle for you. */ 259 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs) 260 { 261 journal_t *journal = osb->journal->j_journal; 262 handle_t *handle; 263 264 BUG_ON(!osb || !osb->journal->j_journal); 265 266 if (ocfs2_is_hard_readonly(osb)) 267 return ERR_PTR(-EROFS); 268 269 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE); 270 BUG_ON(max_buffs <= 0); 271 272 /* Nested transaction? Just return the handle... */ 273 if (journal_current_handle()) 274 return jbd2_journal_start(journal, max_buffs); 275 276 down_read(&osb->journal->j_trans_barrier); 277 278 handle = jbd2_journal_start(journal, max_buffs); 279 if (IS_ERR(handle)) { 280 up_read(&osb->journal->j_trans_barrier); 281 282 mlog_errno(PTR_ERR(handle)); 283 284 if (is_journal_aborted(journal)) { 285 ocfs2_abort(osb->sb, "Detected aborted journal"); 286 handle = ERR_PTR(-EROFS); 287 } 288 } else { 289 if (!ocfs2_mount_local(osb)) 290 atomic_inc(&(osb->journal->j_num_trans)); 291 } 292 293 return handle; 294 } 295 296 int ocfs2_commit_trans(struct ocfs2_super *osb, 297 handle_t *handle) 298 { 299 int ret, nested; 300 struct ocfs2_journal *journal = osb->journal; 301 302 BUG_ON(!handle); 303 304 nested = handle->h_ref > 1; 305 ret = jbd2_journal_stop(handle); 306 if (ret < 0) 307 mlog_errno(ret); 308 309 if (!nested) 310 up_read(&journal->j_trans_barrier); 311 312 return ret; 313 } 314 315 /* 316 * 'nblocks' is what you want to add to the current 317 * transaction. extend_trans will either extend the current handle by 318 * nblocks, or commit it and start a new one with nblocks credits. 319 * 320 * This might call jbd2_journal_restart() which will commit dirty buffers 321 * and then restart the transaction. Before calling 322 * ocfs2_extend_trans(), any changed blocks should have been 323 * dirtied. After calling it, all blocks which need to be changed must 324 * go through another set of journal_access/journal_dirty calls. 325 * 326 * WARNING: This will not release any semaphores or disk locks taken 327 * during the transaction, so make sure they were taken *before* 328 * start_trans or we'll have ordering deadlocks. 329 * 330 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is 331 * good because transaction ids haven't yet been recorded on the 332 * cluster locks associated with this handle. 333 */ 334 int ocfs2_extend_trans(handle_t *handle, int nblocks) 335 { 336 int status; 337 338 BUG_ON(!handle); 339 BUG_ON(!nblocks); 340 341 mlog_entry_void(); 342 343 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks); 344 345 #ifdef CONFIG_OCFS2_DEBUG_FS 346 status = 1; 347 #else 348 status = jbd2_journal_extend(handle, nblocks); 349 if (status < 0) { 350 mlog_errno(status); 351 goto bail; 352 } 353 #endif 354 355 if (status > 0) { 356 mlog(0, 357 "jbd2_journal_extend failed, trying " 358 "jbd2_journal_restart\n"); 359 status = jbd2_journal_restart(handle, nblocks); 360 if (status < 0) { 361 mlog_errno(status); 362 goto bail; 363 } 364 } 365 366 status = 0; 367 bail: 368 369 mlog_exit(status); 370 return status; 371 } 372 373 struct ocfs2_triggers { 374 struct jbd2_buffer_trigger_type ot_triggers; 375 int ot_offset; 376 }; 377 378 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers) 379 { 380 return container_of(triggers, struct ocfs2_triggers, ot_triggers); 381 } 382 383 static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers, 384 struct buffer_head *bh, 385 void *data, size_t size) 386 { 387 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers); 388 389 /* 390 * We aren't guaranteed to have the superblock here, so we 391 * must unconditionally compute the ecc data. 392 * __ocfs2_journal_access() will only set the triggers if 393 * metaecc is enabled. 394 */ 395 ocfs2_block_check_compute(data, size, data + ot->ot_offset); 396 } 397 398 /* 399 * Quota blocks have their own trigger because the struct ocfs2_block_check 400 * offset depends on the blocksize. 401 */ 402 static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers, 403 struct buffer_head *bh, 404 void *data, size_t size) 405 { 406 struct ocfs2_disk_dqtrailer *dqt = 407 ocfs2_block_dqtrailer(size, data); 408 409 /* 410 * We aren't guaranteed to have the superblock here, so we 411 * must unconditionally compute the ecc data. 412 * __ocfs2_journal_access() will only set the triggers if 413 * metaecc is enabled. 414 */ 415 ocfs2_block_check_compute(data, size, &dqt->dq_check); 416 } 417 418 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers, 419 struct buffer_head *bh) 420 { 421 mlog(ML_ERROR, 422 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, " 423 "bh->b_blocknr = %llu\n", 424 (unsigned long)bh, 425 (unsigned long long)bh->b_blocknr); 426 427 /* We aren't guaranteed to have the superblock here - but if we 428 * don't, it'll just crash. */ 429 ocfs2_error(bh->b_assoc_map->host->i_sb, 430 "JBD2 has aborted our journal, ocfs2 cannot continue\n"); 431 } 432 433 static struct ocfs2_triggers di_triggers = { 434 .ot_triggers = { 435 .t_commit = ocfs2_commit_trigger, 436 .t_abort = ocfs2_abort_trigger, 437 }, 438 .ot_offset = offsetof(struct ocfs2_dinode, i_check), 439 }; 440 441 static struct ocfs2_triggers eb_triggers = { 442 .ot_triggers = { 443 .t_commit = ocfs2_commit_trigger, 444 .t_abort = ocfs2_abort_trigger, 445 }, 446 .ot_offset = offsetof(struct ocfs2_extent_block, h_check), 447 }; 448 449 static struct ocfs2_triggers gd_triggers = { 450 .ot_triggers = { 451 .t_commit = ocfs2_commit_trigger, 452 .t_abort = ocfs2_abort_trigger, 453 }, 454 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check), 455 }; 456 457 static struct ocfs2_triggers xb_triggers = { 458 .ot_triggers = { 459 .t_commit = ocfs2_commit_trigger, 460 .t_abort = ocfs2_abort_trigger, 461 }, 462 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check), 463 }; 464 465 static struct ocfs2_triggers dq_triggers = { 466 .ot_triggers = { 467 .t_commit = ocfs2_dq_commit_trigger, 468 .t_abort = ocfs2_abort_trigger, 469 }, 470 }; 471 472 static int __ocfs2_journal_access(handle_t *handle, 473 struct inode *inode, 474 struct buffer_head *bh, 475 struct ocfs2_triggers *triggers, 476 int type) 477 { 478 int status; 479 480 BUG_ON(!inode); 481 BUG_ON(!handle); 482 BUG_ON(!bh); 483 484 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n", 485 (unsigned long long)bh->b_blocknr, type, 486 (type == OCFS2_JOURNAL_ACCESS_CREATE) ? 487 "OCFS2_JOURNAL_ACCESS_CREATE" : 488 "OCFS2_JOURNAL_ACCESS_WRITE", 489 bh->b_size); 490 491 /* we can safely remove this assertion after testing. */ 492 if (!buffer_uptodate(bh)) { 493 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n"); 494 mlog(ML_ERROR, "b_blocknr=%llu\n", 495 (unsigned long long)bh->b_blocknr); 496 BUG(); 497 } 498 499 /* Set the current transaction information on the inode so 500 * that the locking code knows whether it can drop it's locks 501 * on this inode or not. We're protected from the commit 502 * thread updating the current transaction id until 503 * ocfs2_commit_trans() because ocfs2_start_trans() took 504 * j_trans_barrier for us. */ 505 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode); 506 507 mutex_lock(&OCFS2_I(inode)->ip_io_mutex); 508 switch (type) { 509 case OCFS2_JOURNAL_ACCESS_CREATE: 510 case OCFS2_JOURNAL_ACCESS_WRITE: 511 status = jbd2_journal_get_write_access(handle, bh); 512 break; 513 514 case OCFS2_JOURNAL_ACCESS_UNDO: 515 status = jbd2_journal_get_undo_access(handle, bh); 516 break; 517 518 default: 519 status = -EINVAL; 520 mlog(ML_ERROR, "Uknown access type!\n"); 521 } 522 if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers) 523 jbd2_journal_set_triggers(bh, &triggers->ot_triggers); 524 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex); 525 526 if (status < 0) 527 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n", 528 status, type); 529 530 mlog_exit(status); 531 return status; 532 } 533 534 int ocfs2_journal_access_di(handle_t *handle, struct inode *inode, 535 struct buffer_head *bh, int type) 536 { 537 return __ocfs2_journal_access(handle, inode, bh, &di_triggers, 538 type); 539 } 540 541 int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode, 542 struct buffer_head *bh, int type) 543 { 544 return __ocfs2_journal_access(handle, inode, bh, &eb_triggers, 545 type); 546 } 547 548 int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode, 549 struct buffer_head *bh, int type) 550 { 551 return __ocfs2_journal_access(handle, inode, bh, &gd_triggers, 552 type); 553 } 554 555 int ocfs2_journal_access_db(handle_t *handle, struct inode *inode, 556 struct buffer_head *bh, int type) 557 { 558 /* Right now, nothing for dirblocks */ 559 return __ocfs2_journal_access(handle, inode, bh, NULL, type); 560 } 561 562 int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode, 563 struct buffer_head *bh, int type) 564 { 565 return __ocfs2_journal_access(handle, inode, bh, &xb_triggers, 566 type); 567 } 568 569 int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode, 570 struct buffer_head *bh, int type) 571 { 572 return __ocfs2_journal_access(handle, inode, bh, &dq_triggers, 573 type); 574 } 575 576 int ocfs2_journal_access(handle_t *handle, struct inode *inode, 577 struct buffer_head *bh, int type) 578 { 579 return __ocfs2_journal_access(handle, inode, bh, NULL, type); 580 } 581 582 int ocfs2_journal_dirty(handle_t *handle, 583 struct buffer_head *bh) 584 { 585 int status; 586 587 mlog_entry("(bh->b_blocknr=%llu)\n", 588 (unsigned long long)bh->b_blocknr); 589 590 status = jbd2_journal_dirty_metadata(handle, bh); 591 if (status < 0) 592 mlog(ML_ERROR, "Could not dirty metadata buffer. " 593 "(bh->b_blocknr=%llu)\n", 594 (unsigned long long)bh->b_blocknr); 595 596 mlog_exit(status); 597 return status; 598 } 599 600 #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE) 601 602 void ocfs2_set_journal_params(struct ocfs2_super *osb) 603 { 604 journal_t *journal = osb->journal->j_journal; 605 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL; 606 607 if (osb->osb_commit_interval) 608 commit_interval = osb->osb_commit_interval; 609 610 spin_lock(&journal->j_state_lock); 611 journal->j_commit_interval = commit_interval; 612 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER) 613 journal->j_flags |= JBD2_BARRIER; 614 else 615 journal->j_flags &= ~JBD2_BARRIER; 616 spin_unlock(&journal->j_state_lock); 617 } 618 619 int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty) 620 { 621 int status = -1; 622 struct inode *inode = NULL; /* the journal inode */ 623 journal_t *j_journal = NULL; 624 struct ocfs2_dinode *di = NULL; 625 struct buffer_head *bh = NULL; 626 struct ocfs2_super *osb; 627 int inode_lock = 0; 628 629 mlog_entry_void(); 630 631 BUG_ON(!journal); 632 633 osb = journal->j_osb; 634 635 /* already have the inode for our journal */ 636 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, 637 osb->slot_num); 638 if (inode == NULL) { 639 status = -EACCES; 640 mlog_errno(status); 641 goto done; 642 } 643 if (is_bad_inode(inode)) { 644 mlog(ML_ERROR, "access error (bad inode)\n"); 645 iput(inode); 646 inode = NULL; 647 status = -EACCES; 648 goto done; 649 } 650 651 SET_INODE_JOURNAL(inode); 652 OCFS2_I(inode)->ip_open_count++; 653 654 /* Skip recovery waits here - journal inode metadata never 655 * changes in a live cluster so it can be considered an 656 * exception to the rule. */ 657 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY); 658 if (status < 0) { 659 if (status != -ERESTARTSYS) 660 mlog(ML_ERROR, "Could not get lock on journal!\n"); 661 goto done; 662 } 663 664 inode_lock = 1; 665 di = (struct ocfs2_dinode *)bh->b_data; 666 667 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) { 668 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n", 669 inode->i_size); 670 status = -EINVAL; 671 goto done; 672 } 673 674 mlog(0, "inode->i_size = %lld\n", inode->i_size); 675 mlog(0, "inode->i_blocks = %llu\n", 676 (unsigned long long)inode->i_blocks); 677 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters); 678 679 /* call the kernels journal init function now */ 680 j_journal = jbd2_journal_init_inode(inode); 681 if (j_journal == NULL) { 682 mlog(ML_ERROR, "Linux journal layer error\n"); 683 status = -EINVAL; 684 goto done; 685 } 686 687 mlog(0, "Returned from jbd2_journal_init_inode\n"); 688 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen); 689 690 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) & 691 OCFS2_JOURNAL_DIRTY_FL); 692 693 journal->j_journal = j_journal; 694 journal->j_inode = inode; 695 journal->j_bh = bh; 696 697 ocfs2_set_journal_params(osb); 698 699 journal->j_state = OCFS2_JOURNAL_LOADED; 700 701 status = 0; 702 done: 703 if (status < 0) { 704 if (inode_lock) 705 ocfs2_inode_unlock(inode, 1); 706 brelse(bh); 707 if (inode) { 708 OCFS2_I(inode)->ip_open_count--; 709 iput(inode); 710 } 711 } 712 713 mlog_exit(status); 714 return status; 715 } 716 717 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di) 718 { 719 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1); 720 } 721 722 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di) 723 { 724 return le32_to_cpu(di->id1.journal1.ij_recovery_generation); 725 } 726 727 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb, 728 int dirty, int replayed) 729 { 730 int status; 731 unsigned int flags; 732 struct ocfs2_journal *journal = osb->journal; 733 struct buffer_head *bh = journal->j_bh; 734 struct ocfs2_dinode *fe; 735 736 mlog_entry_void(); 737 738 fe = (struct ocfs2_dinode *)bh->b_data; 739 740 /* The journal bh on the osb always comes from ocfs2_journal_init() 741 * and was validated there inside ocfs2_inode_lock_full(). It's a 742 * code bug if we mess it up. */ 743 BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); 744 745 flags = le32_to_cpu(fe->id1.journal1.ij_flags); 746 if (dirty) 747 flags |= OCFS2_JOURNAL_DIRTY_FL; 748 else 749 flags &= ~OCFS2_JOURNAL_DIRTY_FL; 750 fe->id1.journal1.ij_flags = cpu_to_le32(flags); 751 752 if (replayed) 753 ocfs2_bump_recovery_generation(fe); 754 755 status = ocfs2_write_block(osb, bh, journal->j_inode); 756 if (status < 0) 757 mlog_errno(status); 758 759 mlog_exit(status); 760 return status; 761 } 762 763 /* 764 * If the journal has been kmalloc'd it needs to be freed after this 765 * call. 766 */ 767 void ocfs2_journal_shutdown(struct ocfs2_super *osb) 768 { 769 struct ocfs2_journal *journal = NULL; 770 int status = 0; 771 struct inode *inode = NULL; 772 int num_running_trans = 0; 773 774 mlog_entry_void(); 775 776 BUG_ON(!osb); 777 778 journal = osb->journal; 779 if (!journal) 780 goto done; 781 782 inode = journal->j_inode; 783 784 if (journal->j_state != OCFS2_JOURNAL_LOADED) 785 goto done; 786 787 /* need to inc inode use count - jbd2_journal_destroy will iput. */ 788 if (!igrab(inode)) 789 BUG(); 790 791 num_running_trans = atomic_read(&(osb->journal->j_num_trans)); 792 if (num_running_trans > 0) 793 mlog(0, "Shutting down journal: must wait on %d " 794 "running transactions!\n", 795 num_running_trans); 796 797 /* Do a commit_cache here. It will flush our journal, *and* 798 * release any locks that are still held. 799 * set the SHUTDOWN flag and release the trans lock. 800 * the commit thread will take the trans lock for us below. */ 801 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN; 802 803 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not 804 * drop the trans_lock (which we want to hold until we 805 * completely destroy the journal. */ 806 if (osb->commit_task) { 807 /* Wait for the commit thread */ 808 mlog(0, "Waiting for ocfs2commit to exit....\n"); 809 kthread_stop(osb->commit_task); 810 osb->commit_task = NULL; 811 } 812 813 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0); 814 815 if (ocfs2_mount_local(osb)) { 816 jbd2_journal_lock_updates(journal->j_journal); 817 status = jbd2_journal_flush(journal->j_journal); 818 jbd2_journal_unlock_updates(journal->j_journal); 819 if (status < 0) 820 mlog_errno(status); 821 } 822 823 if (status == 0) { 824 /* 825 * Do not toggle if flush was unsuccessful otherwise 826 * will leave dirty metadata in a "clean" journal 827 */ 828 status = ocfs2_journal_toggle_dirty(osb, 0, 0); 829 if (status < 0) 830 mlog_errno(status); 831 } 832 833 /* Shutdown the kernel journal system */ 834 jbd2_journal_destroy(journal->j_journal); 835 journal->j_journal = NULL; 836 837 OCFS2_I(inode)->ip_open_count--; 838 839 /* unlock our journal */ 840 ocfs2_inode_unlock(inode, 1); 841 842 brelse(journal->j_bh); 843 journal->j_bh = NULL; 844 845 journal->j_state = OCFS2_JOURNAL_FREE; 846 847 // up_write(&journal->j_trans_barrier); 848 done: 849 if (inode) 850 iput(inode); 851 mlog_exit_void(); 852 } 853 854 static void ocfs2_clear_journal_error(struct super_block *sb, 855 journal_t *journal, 856 int slot) 857 { 858 int olderr; 859 860 olderr = jbd2_journal_errno(journal); 861 if (olderr) { 862 mlog(ML_ERROR, "File system error %d recorded in " 863 "journal %u.\n", olderr, slot); 864 mlog(ML_ERROR, "File system on device %s needs checking.\n", 865 sb->s_id); 866 867 jbd2_journal_ack_err(journal); 868 jbd2_journal_clear_err(journal); 869 } 870 } 871 872 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed) 873 { 874 int status = 0; 875 struct ocfs2_super *osb; 876 877 mlog_entry_void(); 878 879 BUG_ON(!journal); 880 881 osb = journal->j_osb; 882 883 status = jbd2_journal_load(journal->j_journal); 884 if (status < 0) { 885 mlog(ML_ERROR, "Failed to load journal!\n"); 886 goto done; 887 } 888 889 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num); 890 891 status = ocfs2_journal_toggle_dirty(osb, 1, replayed); 892 if (status < 0) { 893 mlog_errno(status); 894 goto done; 895 } 896 897 /* Launch the commit thread */ 898 if (!local) { 899 osb->commit_task = kthread_run(ocfs2_commit_thread, osb, 900 "ocfs2cmt"); 901 if (IS_ERR(osb->commit_task)) { 902 status = PTR_ERR(osb->commit_task); 903 osb->commit_task = NULL; 904 mlog(ML_ERROR, "unable to launch ocfs2commit thread, " 905 "error=%d", status); 906 goto done; 907 } 908 } else 909 osb->commit_task = NULL; 910 911 done: 912 mlog_exit(status); 913 return status; 914 } 915 916 917 /* 'full' flag tells us whether we clear out all blocks or if we just 918 * mark the journal clean */ 919 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full) 920 { 921 int status; 922 923 mlog_entry_void(); 924 925 BUG_ON(!journal); 926 927 status = jbd2_journal_wipe(journal->j_journal, full); 928 if (status < 0) { 929 mlog_errno(status); 930 goto bail; 931 } 932 933 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0); 934 if (status < 0) 935 mlog_errno(status); 936 937 bail: 938 mlog_exit(status); 939 return status; 940 } 941 942 static int ocfs2_recovery_completed(struct ocfs2_super *osb) 943 { 944 int empty; 945 struct ocfs2_recovery_map *rm = osb->recovery_map; 946 947 spin_lock(&osb->osb_lock); 948 empty = (rm->rm_used == 0); 949 spin_unlock(&osb->osb_lock); 950 951 return empty; 952 } 953 954 void ocfs2_wait_for_recovery(struct ocfs2_super *osb) 955 { 956 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb)); 957 } 958 959 /* 960 * JBD Might read a cached version of another nodes journal file. We 961 * don't want this as this file changes often and we get no 962 * notification on those changes. The only way to be sure that we've 963 * got the most up to date version of those blocks then is to force 964 * read them off disk. Just searching through the buffer cache won't 965 * work as there may be pages backing this file which are still marked 966 * up to date. We know things can't change on this file underneath us 967 * as we have the lock by now :) 968 */ 969 static int ocfs2_force_read_journal(struct inode *inode) 970 { 971 int status = 0; 972 int i; 973 u64 v_blkno, p_blkno, p_blocks, num_blocks; 974 #define CONCURRENT_JOURNAL_FILL 32ULL 975 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL]; 976 977 mlog_entry_void(); 978 979 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL); 980 981 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size); 982 v_blkno = 0; 983 while (v_blkno < num_blocks) { 984 status = ocfs2_extent_map_get_blocks(inode, v_blkno, 985 &p_blkno, &p_blocks, NULL); 986 if (status < 0) { 987 mlog_errno(status); 988 goto bail; 989 } 990 991 if (p_blocks > CONCURRENT_JOURNAL_FILL) 992 p_blocks = CONCURRENT_JOURNAL_FILL; 993 994 /* We are reading journal data which should not 995 * be put in the uptodate cache */ 996 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb), 997 p_blkno, p_blocks, bhs); 998 if (status < 0) { 999 mlog_errno(status); 1000 goto bail; 1001 } 1002 1003 for(i = 0; i < p_blocks; i++) { 1004 brelse(bhs[i]); 1005 bhs[i] = NULL; 1006 } 1007 1008 v_blkno += p_blocks; 1009 } 1010 1011 bail: 1012 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++) 1013 brelse(bhs[i]); 1014 mlog_exit(status); 1015 return status; 1016 } 1017 1018 struct ocfs2_la_recovery_item { 1019 struct list_head lri_list; 1020 int lri_slot; 1021 struct ocfs2_dinode *lri_la_dinode; 1022 struct ocfs2_dinode *lri_tl_dinode; 1023 struct ocfs2_quota_recovery *lri_qrec; 1024 }; 1025 1026 /* Does the second half of the recovery process. By this point, the 1027 * node is marked clean and can actually be considered recovered, 1028 * hence it's no longer in the recovery map, but there's still some 1029 * cleanup we can do which shouldn't happen within the recovery thread 1030 * as locking in that context becomes very difficult if we are to take 1031 * recovering nodes into account. 1032 * 1033 * NOTE: This function can and will sleep on recovery of other nodes 1034 * during cluster locking, just like any other ocfs2 process. 1035 */ 1036 void ocfs2_complete_recovery(struct work_struct *work) 1037 { 1038 int ret; 1039 struct ocfs2_journal *journal = 1040 container_of(work, struct ocfs2_journal, j_recovery_work); 1041 struct ocfs2_super *osb = journal->j_osb; 1042 struct ocfs2_dinode *la_dinode, *tl_dinode; 1043 struct ocfs2_la_recovery_item *item, *n; 1044 struct ocfs2_quota_recovery *qrec; 1045 LIST_HEAD(tmp_la_list); 1046 1047 mlog_entry_void(); 1048 1049 mlog(0, "completing recovery from keventd\n"); 1050 1051 spin_lock(&journal->j_lock); 1052 list_splice_init(&journal->j_la_cleanups, &tmp_la_list); 1053 spin_unlock(&journal->j_lock); 1054 1055 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) { 1056 list_del_init(&item->lri_list); 1057 1058 mlog(0, "Complete recovery for slot %d\n", item->lri_slot); 1059 1060 ocfs2_wait_on_quotas(osb); 1061 1062 la_dinode = item->lri_la_dinode; 1063 if (la_dinode) { 1064 mlog(0, "Clean up local alloc %llu\n", 1065 (unsigned long long)le64_to_cpu(la_dinode->i_blkno)); 1066 1067 ret = ocfs2_complete_local_alloc_recovery(osb, 1068 la_dinode); 1069 if (ret < 0) 1070 mlog_errno(ret); 1071 1072 kfree(la_dinode); 1073 } 1074 1075 tl_dinode = item->lri_tl_dinode; 1076 if (tl_dinode) { 1077 mlog(0, "Clean up truncate log %llu\n", 1078 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno)); 1079 1080 ret = ocfs2_complete_truncate_log_recovery(osb, 1081 tl_dinode); 1082 if (ret < 0) 1083 mlog_errno(ret); 1084 1085 kfree(tl_dinode); 1086 } 1087 1088 ret = ocfs2_recover_orphans(osb, item->lri_slot); 1089 if (ret < 0) 1090 mlog_errno(ret); 1091 1092 qrec = item->lri_qrec; 1093 if (qrec) { 1094 mlog(0, "Recovering quota files"); 1095 ret = ocfs2_finish_quota_recovery(osb, qrec, 1096 item->lri_slot); 1097 if (ret < 0) 1098 mlog_errno(ret); 1099 /* Recovery info is already freed now */ 1100 } 1101 1102 kfree(item); 1103 } 1104 1105 mlog(0, "Recovery completion\n"); 1106 mlog_exit_void(); 1107 } 1108 1109 /* NOTE: This function always eats your references to la_dinode and 1110 * tl_dinode, either manually on error, or by passing them to 1111 * ocfs2_complete_recovery */ 1112 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal, 1113 int slot_num, 1114 struct ocfs2_dinode *la_dinode, 1115 struct ocfs2_dinode *tl_dinode, 1116 struct ocfs2_quota_recovery *qrec) 1117 { 1118 struct ocfs2_la_recovery_item *item; 1119 1120 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS); 1121 if (!item) { 1122 /* Though we wish to avoid it, we are in fact safe in 1123 * skipping local alloc cleanup as fsck.ocfs2 is more 1124 * than capable of reclaiming unused space. */ 1125 if (la_dinode) 1126 kfree(la_dinode); 1127 1128 if (tl_dinode) 1129 kfree(tl_dinode); 1130 1131 if (qrec) 1132 ocfs2_free_quota_recovery(qrec); 1133 1134 mlog_errno(-ENOMEM); 1135 return; 1136 } 1137 1138 INIT_LIST_HEAD(&item->lri_list); 1139 item->lri_la_dinode = la_dinode; 1140 item->lri_slot = slot_num; 1141 item->lri_tl_dinode = tl_dinode; 1142 item->lri_qrec = qrec; 1143 1144 spin_lock(&journal->j_lock); 1145 list_add_tail(&item->lri_list, &journal->j_la_cleanups); 1146 queue_work(ocfs2_wq, &journal->j_recovery_work); 1147 spin_unlock(&journal->j_lock); 1148 } 1149 1150 /* Called by the mount code to queue recovery the last part of 1151 * recovery for it's own slot. */ 1152 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb) 1153 { 1154 struct ocfs2_journal *journal = osb->journal; 1155 1156 if (osb->dirty) { 1157 /* No need to queue up our truncate_log as regular 1158 * cleanup will catch that. */ 1159 ocfs2_queue_recovery_completion(journal, 1160 osb->slot_num, 1161 osb->local_alloc_copy, 1162 NULL, 1163 NULL); 1164 ocfs2_schedule_truncate_log_flush(osb, 0); 1165 1166 osb->local_alloc_copy = NULL; 1167 osb->dirty = 0; 1168 } 1169 } 1170 1171 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb) 1172 { 1173 if (osb->quota_rec) { 1174 ocfs2_queue_recovery_completion(osb->journal, 1175 osb->slot_num, 1176 NULL, 1177 NULL, 1178 osb->quota_rec); 1179 osb->quota_rec = NULL; 1180 } 1181 } 1182 1183 static int __ocfs2_recovery_thread(void *arg) 1184 { 1185 int status, node_num, slot_num; 1186 struct ocfs2_super *osb = arg; 1187 struct ocfs2_recovery_map *rm = osb->recovery_map; 1188 int *rm_quota = NULL; 1189 int rm_quota_used = 0, i; 1190 struct ocfs2_quota_recovery *qrec; 1191 1192 mlog_entry_void(); 1193 1194 status = ocfs2_wait_on_mount(osb); 1195 if (status < 0) { 1196 goto bail; 1197 } 1198 1199 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS); 1200 if (!rm_quota) { 1201 status = -ENOMEM; 1202 goto bail; 1203 } 1204 restart: 1205 status = ocfs2_super_lock(osb, 1); 1206 if (status < 0) { 1207 mlog_errno(status); 1208 goto bail; 1209 } 1210 1211 spin_lock(&osb->osb_lock); 1212 while (rm->rm_used) { 1213 /* It's always safe to remove entry zero, as we won't 1214 * clear it until ocfs2_recover_node() has succeeded. */ 1215 node_num = rm->rm_entries[0]; 1216 spin_unlock(&osb->osb_lock); 1217 mlog(0, "checking node %d\n", node_num); 1218 slot_num = ocfs2_node_num_to_slot(osb, node_num); 1219 if (slot_num == -ENOENT) { 1220 status = 0; 1221 mlog(0, "no slot for this node, so no recovery" 1222 "required.\n"); 1223 goto skip_recovery; 1224 } 1225 mlog(0, "node %d was using slot %d\n", node_num, slot_num); 1226 1227 /* It is a bit subtle with quota recovery. We cannot do it 1228 * immediately because we have to obtain cluster locks from 1229 * quota files and we also don't want to just skip it because 1230 * then quota usage would be out of sync until some node takes 1231 * the slot. So we remember which nodes need quota recovery 1232 * and when everything else is done, we recover quotas. */ 1233 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++); 1234 if (i == rm_quota_used) 1235 rm_quota[rm_quota_used++] = slot_num; 1236 1237 status = ocfs2_recover_node(osb, node_num, slot_num); 1238 skip_recovery: 1239 if (!status) { 1240 ocfs2_recovery_map_clear(osb, node_num); 1241 } else { 1242 mlog(ML_ERROR, 1243 "Error %d recovering node %d on device (%u,%u)!\n", 1244 status, node_num, 1245 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev)); 1246 mlog(ML_ERROR, "Volume requires unmount.\n"); 1247 } 1248 1249 spin_lock(&osb->osb_lock); 1250 } 1251 spin_unlock(&osb->osb_lock); 1252 mlog(0, "All nodes recovered\n"); 1253 1254 /* Refresh all journal recovery generations from disk */ 1255 status = ocfs2_check_journals_nolocks(osb); 1256 status = (status == -EROFS) ? 0 : status; 1257 if (status < 0) 1258 mlog_errno(status); 1259 1260 /* Now it is right time to recover quotas... We have to do this under 1261 * superblock lock so that noone can start using the slot (and crash) 1262 * before we recover it */ 1263 for (i = 0; i < rm_quota_used; i++) { 1264 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]); 1265 if (IS_ERR(qrec)) { 1266 status = PTR_ERR(qrec); 1267 mlog_errno(status); 1268 continue; 1269 } 1270 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i], 1271 NULL, NULL, qrec); 1272 } 1273 1274 ocfs2_super_unlock(osb, 1); 1275 1276 /* We always run recovery on our own orphan dir - the dead 1277 * node(s) may have disallowd a previos inode delete. Re-processing 1278 * is therefore required. */ 1279 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL, 1280 NULL, NULL); 1281 1282 bail: 1283 mutex_lock(&osb->recovery_lock); 1284 if (!status && !ocfs2_recovery_completed(osb)) { 1285 mutex_unlock(&osb->recovery_lock); 1286 goto restart; 1287 } 1288 1289 osb->recovery_thread_task = NULL; 1290 mb(); /* sync with ocfs2_recovery_thread_running */ 1291 wake_up(&osb->recovery_event); 1292 1293 mutex_unlock(&osb->recovery_lock); 1294 1295 if (rm_quota) 1296 kfree(rm_quota); 1297 1298 mlog_exit(status); 1299 /* no one is callint kthread_stop() for us so the kthread() api 1300 * requires that we call do_exit(). And it isn't exported, but 1301 * complete_and_exit() seems to be a minimal wrapper around it. */ 1302 complete_and_exit(NULL, status); 1303 return status; 1304 } 1305 1306 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num) 1307 { 1308 mlog_entry("(node_num=%d, osb->node_num = %d)\n", 1309 node_num, osb->node_num); 1310 1311 mutex_lock(&osb->recovery_lock); 1312 if (osb->disable_recovery) 1313 goto out; 1314 1315 /* People waiting on recovery will wait on 1316 * the recovery map to empty. */ 1317 if (ocfs2_recovery_map_set(osb, node_num)) 1318 mlog(0, "node %d already in recovery map.\n", node_num); 1319 1320 mlog(0, "starting recovery thread...\n"); 1321 1322 if (osb->recovery_thread_task) 1323 goto out; 1324 1325 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb, 1326 "ocfs2rec"); 1327 if (IS_ERR(osb->recovery_thread_task)) { 1328 mlog_errno((int)PTR_ERR(osb->recovery_thread_task)); 1329 osb->recovery_thread_task = NULL; 1330 } 1331 1332 out: 1333 mutex_unlock(&osb->recovery_lock); 1334 wake_up(&osb->recovery_event); 1335 1336 mlog_exit_void(); 1337 } 1338 1339 static int ocfs2_read_journal_inode(struct ocfs2_super *osb, 1340 int slot_num, 1341 struct buffer_head **bh, 1342 struct inode **ret_inode) 1343 { 1344 int status = -EACCES; 1345 struct inode *inode = NULL; 1346 1347 BUG_ON(slot_num >= osb->max_slots); 1348 1349 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, 1350 slot_num); 1351 if (!inode || is_bad_inode(inode)) { 1352 mlog_errno(status); 1353 goto bail; 1354 } 1355 SET_INODE_JOURNAL(inode); 1356 1357 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE); 1358 if (status < 0) { 1359 mlog_errno(status); 1360 goto bail; 1361 } 1362 1363 status = 0; 1364 1365 bail: 1366 if (inode) { 1367 if (status || !ret_inode) 1368 iput(inode); 1369 else 1370 *ret_inode = inode; 1371 } 1372 return status; 1373 } 1374 1375 /* Does the actual journal replay and marks the journal inode as 1376 * clean. Will only replay if the journal inode is marked dirty. */ 1377 static int ocfs2_replay_journal(struct ocfs2_super *osb, 1378 int node_num, 1379 int slot_num) 1380 { 1381 int status; 1382 int got_lock = 0; 1383 unsigned int flags; 1384 struct inode *inode = NULL; 1385 struct ocfs2_dinode *fe; 1386 journal_t *journal = NULL; 1387 struct buffer_head *bh = NULL; 1388 u32 slot_reco_gen; 1389 1390 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode); 1391 if (status) { 1392 mlog_errno(status); 1393 goto done; 1394 } 1395 1396 fe = (struct ocfs2_dinode *)bh->b_data; 1397 slot_reco_gen = ocfs2_get_recovery_generation(fe); 1398 brelse(bh); 1399 bh = NULL; 1400 1401 /* 1402 * As the fs recovery is asynchronous, there is a small chance that 1403 * another node mounted (and recovered) the slot before the recovery 1404 * thread could get the lock. To handle that, we dirty read the journal 1405 * inode for that slot to get the recovery generation. If it is 1406 * different than what we expected, the slot has been recovered. 1407 * If not, it needs recovery. 1408 */ 1409 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) { 1410 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num, 1411 osb->slot_recovery_generations[slot_num], slot_reco_gen); 1412 osb->slot_recovery_generations[slot_num] = slot_reco_gen; 1413 status = -EBUSY; 1414 goto done; 1415 } 1416 1417 /* Continue with recovery as the journal has not yet been recovered */ 1418 1419 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY); 1420 if (status < 0) { 1421 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status); 1422 if (status != -ERESTARTSYS) 1423 mlog(ML_ERROR, "Could not lock journal!\n"); 1424 goto done; 1425 } 1426 got_lock = 1; 1427 1428 fe = (struct ocfs2_dinode *) bh->b_data; 1429 1430 flags = le32_to_cpu(fe->id1.journal1.ij_flags); 1431 slot_reco_gen = ocfs2_get_recovery_generation(fe); 1432 1433 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) { 1434 mlog(0, "No recovery required for node %d\n", node_num); 1435 /* Refresh recovery generation for the slot */ 1436 osb->slot_recovery_generations[slot_num] = slot_reco_gen; 1437 goto done; 1438 } 1439 1440 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n", 1441 node_num, slot_num, 1442 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev)); 1443 1444 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 1445 1446 status = ocfs2_force_read_journal(inode); 1447 if (status < 0) { 1448 mlog_errno(status); 1449 goto done; 1450 } 1451 1452 mlog(0, "calling journal_init_inode\n"); 1453 journal = jbd2_journal_init_inode(inode); 1454 if (journal == NULL) { 1455 mlog(ML_ERROR, "Linux journal layer error\n"); 1456 status = -EIO; 1457 goto done; 1458 } 1459 1460 status = jbd2_journal_load(journal); 1461 if (status < 0) { 1462 mlog_errno(status); 1463 if (!igrab(inode)) 1464 BUG(); 1465 jbd2_journal_destroy(journal); 1466 goto done; 1467 } 1468 1469 ocfs2_clear_journal_error(osb->sb, journal, slot_num); 1470 1471 /* wipe the journal */ 1472 mlog(0, "flushing the journal.\n"); 1473 jbd2_journal_lock_updates(journal); 1474 status = jbd2_journal_flush(journal); 1475 jbd2_journal_unlock_updates(journal); 1476 if (status < 0) 1477 mlog_errno(status); 1478 1479 /* This will mark the node clean */ 1480 flags = le32_to_cpu(fe->id1.journal1.ij_flags); 1481 flags &= ~OCFS2_JOURNAL_DIRTY_FL; 1482 fe->id1.journal1.ij_flags = cpu_to_le32(flags); 1483 1484 /* Increment recovery generation to indicate successful recovery */ 1485 ocfs2_bump_recovery_generation(fe); 1486 osb->slot_recovery_generations[slot_num] = 1487 ocfs2_get_recovery_generation(fe); 1488 1489 status = ocfs2_write_block(osb, bh, inode); 1490 if (status < 0) 1491 mlog_errno(status); 1492 1493 if (!igrab(inode)) 1494 BUG(); 1495 1496 jbd2_journal_destroy(journal); 1497 1498 done: 1499 /* drop the lock on this nodes journal */ 1500 if (got_lock) 1501 ocfs2_inode_unlock(inode, 1); 1502 1503 if (inode) 1504 iput(inode); 1505 1506 brelse(bh); 1507 1508 mlog_exit(status); 1509 return status; 1510 } 1511 1512 /* 1513 * Do the most important parts of node recovery: 1514 * - Replay it's journal 1515 * - Stamp a clean local allocator file 1516 * - Stamp a clean truncate log 1517 * - Mark the node clean 1518 * 1519 * If this function completes without error, a node in OCFS2 can be 1520 * said to have been safely recovered. As a result, failure during the 1521 * second part of a nodes recovery process (local alloc recovery) is 1522 * far less concerning. 1523 */ 1524 static int ocfs2_recover_node(struct ocfs2_super *osb, 1525 int node_num, int slot_num) 1526 { 1527 int status = 0; 1528 struct ocfs2_dinode *la_copy = NULL; 1529 struct ocfs2_dinode *tl_copy = NULL; 1530 1531 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n", 1532 node_num, slot_num, osb->node_num); 1533 1534 /* Should not ever be called to recover ourselves -- in that 1535 * case we should've called ocfs2_journal_load instead. */ 1536 BUG_ON(osb->node_num == node_num); 1537 1538 status = ocfs2_replay_journal(osb, node_num, slot_num); 1539 if (status < 0) { 1540 if (status == -EBUSY) { 1541 mlog(0, "Skipping recovery for slot %u (node %u) " 1542 "as another node has recovered it\n", slot_num, 1543 node_num); 1544 status = 0; 1545 goto done; 1546 } 1547 mlog_errno(status); 1548 goto done; 1549 } 1550 1551 /* Stamp a clean local alloc file AFTER recovering the journal... */ 1552 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy); 1553 if (status < 0) { 1554 mlog_errno(status); 1555 goto done; 1556 } 1557 1558 /* An error from begin_truncate_log_recovery is not 1559 * serious enough to warrant halting the rest of 1560 * recovery. */ 1561 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy); 1562 if (status < 0) 1563 mlog_errno(status); 1564 1565 /* Likewise, this would be a strange but ultimately not so 1566 * harmful place to get an error... */ 1567 status = ocfs2_clear_slot(osb, slot_num); 1568 if (status < 0) 1569 mlog_errno(status); 1570 1571 /* This will kfree the memory pointed to by la_copy and tl_copy */ 1572 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy, 1573 tl_copy, NULL); 1574 1575 status = 0; 1576 done: 1577 1578 mlog_exit(status); 1579 return status; 1580 } 1581 1582 /* Test node liveness by trylocking his journal. If we get the lock, 1583 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is 1584 * still alive (we couldn't get the lock) and < 0 on error. */ 1585 static int ocfs2_trylock_journal(struct ocfs2_super *osb, 1586 int slot_num) 1587 { 1588 int status, flags; 1589 struct inode *inode = NULL; 1590 1591 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE, 1592 slot_num); 1593 if (inode == NULL) { 1594 mlog(ML_ERROR, "access error\n"); 1595 status = -EACCES; 1596 goto bail; 1597 } 1598 if (is_bad_inode(inode)) { 1599 mlog(ML_ERROR, "access error (bad inode)\n"); 1600 iput(inode); 1601 inode = NULL; 1602 status = -EACCES; 1603 goto bail; 1604 } 1605 SET_INODE_JOURNAL(inode); 1606 1607 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE; 1608 status = ocfs2_inode_lock_full(inode, NULL, 1, flags); 1609 if (status < 0) { 1610 if (status != -EAGAIN) 1611 mlog_errno(status); 1612 goto bail; 1613 } 1614 1615 ocfs2_inode_unlock(inode, 1); 1616 bail: 1617 if (inode) 1618 iput(inode); 1619 1620 return status; 1621 } 1622 1623 /* Call this underneath ocfs2_super_lock. It also assumes that the 1624 * slot info struct has been updated from disk. */ 1625 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb) 1626 { 1627 unsigned int node_num; 1628 int status, i; 1629 u32 gen; 1630 struct buffer_head *bh = NULL; 1631 struct ocfs2_dinode *di; 1632 1633 /* This is called with the super block cluster lock, so we 1634 * know that the slot map can't change underneath us. */ 1635 1636 for (i = 0; i < osb->max_slots; i++) { 1637 /* Read journal inode to get the recovery generation */ 1638 status = ocfs2_read_journal_inode(osb, i, &bh, NULL); 1639 if (status) { 1640 mlog_errno(status); 1641 goto bail; 1642 } 1643 di = (struct ocfs2_dinode *)bh->b_data; 1644 gen = ocfs2_get_recovery_generation(di); 1645 brelse(bh); 1646 bh = NULL; 1647 1648 spin_lock(&osb->osb_lock); 1649 osb->slot_recovery_generations[i] = gen; 1650 1651 mlog(0, "Slot %u recovery generation is %u\n", i, 1652 osb->slot_recovery_generations[i]); 1653 1654 if (i == osb->slot_num) { 1655 spin_unlock(&osb->osb_lock); 1656 continue; 1657 } 1658 1659 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num); 1660 if (status == -ENOENT) { 1661 spin_unlock(&osb->osb_lock); 1662 continue; 1663 } 1664 1665 if (__ocfs2_recovery_map_test(osb, node_num)) { 1666 spin_unlock(&osb->osb_lock); 1667 continue; 1668 } 1669 spin_unlock(&osb->osb_lock); 1670 1671 /* Ok, we have a slot occupied by another node which 1672 * is not in the recovery map. We trylock his journal 1673 * file here to test if he's alive. */ 1674 status = ocfs2_trylock_journal(osb, i); 1675 if (!status) { 1676 /* Since we're called from mount, we know that 1677 * the recovery thread can't race us on 1678 * setting / checking the recovery bits. */ 1679 ocfs2_recovery_thread(osb, node_num); 1680 } else if ((status < 0) && (status != -EAGAIN)) { 1681 mlog_errno(status); 1682 goto bail; 1683 } 1684 } 1685 1686 status = 0; 1687 bail: 1688 mlog_exit(status); 1689 return status; 1690 } 1691 1692 struct ocfs2_orphan_filldir_priv { 1693 struct inode *head; 1694 struct ocfs2_super *osb; 1695 }; 1696 1697 static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len, 1698 loff_t pos, u64 ino, unsigned type) 1699 { 1700 struct ocfs2_orphan_filldir_priv *p = priv; 1701 struct inode *iter; 1702 1703 if (name_len == 1 && !strncmp(".", name, 1)) 1704 return 0; 1705 if (name_len == 2 && !strncmp("..", name, 2)) 1706 return 0; 1707 1708 /* Skip bad inodes so that recovery can continue */ 1709 iter = ocfs2_iget(p->osb, ino, 1710 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0); 1711 if (IS_ERR(iter)) 1712 return 0; 1713 1714 mlog(0, "queue orphan %llu\n", 1715 (unsigned long long)OCFS2_I(iter)->ip_blkno); 1716 /* No locking is required for the next_orphan queue as there 1717 * is only ever a single process doing orphan recovery. */ 1718 OCFS2_I(iter)->ip_next_orphan = p->head; 1719 p->head = iter; 1720 1721 return 0; 1722 } 1723 1724 static int ocfs2_queue_orphans(struct ocfs2_super *osb, 1725 int slot, 1726 struct inode **head) 1727 { 1728 int status; 1729 struct inode *orphan_dir_inode = NULL; 1730 struct ocfs2_orphan_filldir_priv priv; 1731 loff_t pos = 0; 1732 1733 priv.osb = osb; 1734 priv.head = *head; 1735 1736 orphan_dir_inode = ocfs2_get_system_file_inode(osb, 1737 ORPHAN_DIR_SYSTEM_INODE, 1738 slot); 1739 if (!orphan_dir_inode) { 1740 status = -ENOENT; 1741 mlog_errno(status); 1742 return status; 1743 } 1744 1745 mutex_lock(&orphan_dir_inode->i_mutex); 1746 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0); 1747 if (status < 0) { 1748 mlog_errno(status); 1749 goto out; 1750 } 1751 1752 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv, 1753 ocfs2_orphan_filldir); 1754 if (status) { 1755 mlog_errno(status); 1756 goto out_cluster; 1757 } 1758 1759 *head = priv.head; 1760 1761 out_cluster: 1762 ocfs2_inode_unlock(orphan_dir_inode, 0); 1763 out: 1764 mutex_unlock(&orphan_dir_inode->i_mutex); 1765 iput(orphan_dir_inode); 1766 return status; 1767 } 1768 1769 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb, 1770 int slot) 1771 { 1772 int ret; 1773 1774 spin_lock(&osb->osb_lock); 1775 ret = !osb->osb_orphan_wipes[slot]; 1776 spin_unlock(&osb->osb_lock); 1777 return ret; 1778 } 1779 1780 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb, 1781 int slot) 1782 { 1783 spin_lock(&osb->osb_lock); 1784 /* Mark ourselves such that new processes in delete_inode() 1785 * know to quit early. */ 1786 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot); 1787 while (osb->osb_orphan_wipes[slot]) { 1788 /* If any processes are already in the middle of an 1789 * orphan wipe on this dir, then we need to wait for 1790 * them. */ 1791 spin_unlock(&osb->osb_lock); 1792 wait_event_interruptible(osb->osb_wipe_event, 1793 ocfs2_orphan_recovery_can_continue(osb, slot)); 1794 spin_lock(&osb->osb_lock); 1795 } 1796 spin_unlock(&osb->osb_lock); 1797 } 1798 1799 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb, 1800 int slot) 1801 { 1802 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot); 1803 } 1804 1805 /* 1806 * Orphan recovery. Each mounted node has it's own orphan dir which we 1807 * must run during recovery. Our strategy here is to build a list of 1808 * the inodes in the orphan dir and iget/iput them. The VFS does 1809 * (most) of the rest of the work. 1810 * 1811 * Orphan recovery can happen at any time, not just mount so we have a 1812 * couple of extra considerations. 1813 * 1814 * - We grab as many inodes as we can under the orphan dir lock - 1815 * doing iget() outside the orphan dir risks getting a reference on 1816 * an invalid inode. 1817 * - We must be sure not to deadlock with other processes on the 1818 * system wanting to run delete_inode(). This can happen when they go 1819 * to lock the orphan dir and the orphan recovery process attempts to 1820 * iget() inside the orphan dir lock. This can be avoided by 1821 * advertising our state to ocfs2_delete_inode(). 1822 */ 1823 static int ocfs2_recover_orphans(struct ocfs2_super *osb, 1824 int slot) 1825 { 1826 int ret = 0; 1827 struct inode *inode = NULL; 1828 struct inode *iter; 1829 struct ocfs2_inode_info *oi; 1830 1831 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot); 1832 1833 ocfs2_mark_recovering_orphan_dir(osb, slot); 1834 ret = ocfs2_queue_orphans(osb, slot, &inode); 1835 ocfs2_clear_recovering_orphan_dir(osb, slot); 1836 1837 /* Error here should be noted, but we want to continue with as 1838 * many queued inodes as we've got. */ 1839 if (ret) 1840 mlog_errno(ret); 1841 1842 while (inode) { 1843 oi = OCFS2_I(inode); 1844 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno); 1845 1846 iter = oi->ip_next_orphan; 1847 1848 spin_lock(&oi->ip_lock); 1849 /* The remote delete code may have set these on the 1850 * assumption that the other node would wipe them 1851 * successfully. If they are still in the node's 1852 * orphan dir, we need to reset that state. */ 1853 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE); 1854 1855 /* Set the proper information to get us going into 1856 * ocfs2_delete_inode. */ 1857 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED; 1858 spin_unlock(&oi->ip_lock); 1859 1860 iput(inode); 1861 1862 inode = iter; 1863 } 1864 1865 return ret; 1866 } 1867 1868 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota) 1869 { 1870 /* This check is good because ocfs2 will wait on our recovery 1871 * thread before changing it to something other than MOUNTED 1872 * or DISABLED. */ 1873 wait_event(osb->osb_mount_event, 1874 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) || 1875 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS || 1876 atomic_read(&osb->vol_state) == VOLUME_DISABLED); 1877 1878 /* If there's an error on mount, then we may never get to the 1879 * MOUNTED flag, but this is set right before 1880 * dismount_volume() so we can trust it. */ 1881 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) { 1882 mlog(0, "mount error, exiting!\n"); 1883 return -EBUSY; 1884 } 1885 1886 return 0; 1887 } 1888 1889 static int ocfs2_commit_thread(void *arg) 1890 { 1891 int status; 1892 struct ocfs2_super *osb = arg; 1893 struct ocfs2_journal *journal = osb->journal; 1894 1895 /* we can trust j_num_trans here because _should_stop() is only set in 1896 * shutdown and nobody other than ourselves should be able to start 1897 * transactions. committing on shutdown might take a few iterations 1898 * as final transactions put deleted inodes on the list */ 1899 while (!(kthread_should_stop() && 1900 atomic_read(&journal->j_num_trans) == 0)) { 1901 1902 wait_event_interruptible(osb->checkpoint_event, 1903 atomic_read(&journal->j_num_trans) 1904 || kthread_should_stop()); 1905 1906 status = ocfs2_commit_cache(osb); 1907 if (status < 0) 1908 mlog_errno(status); 1909 1910 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){ 1911 mlog(ML_KTHREAD, 1912 "commit_thread: %u transactions pending on " 1913 "shutdown\n", 1914 atomic_read(&journal->j_num_trans)); 1915 } 1916 } 1917 1918 return 0; 1919 } 1920 1921 /* Reads all the journal inodes without taking any cluster locks. Used 1922 * for hard readonly access to determine whether any journal requires 1923 * recovery. Also used to refresh the recovery generation numbers after 1924 * a journal has been recovered by another node. 1925 */ 1926 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb) 1927 { 1928 int ret = 0; 1929 unsigned int slot; 1930 struct buffer_head *di_bh = NULL; 1931 struct ocfs2_dinode *di; 1932 int journal_dirty = 0; 1933 1934 for(slot = 0; slot < osb->max_slots; slot++) { 1935 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL); 1936 if (ret) { 1937 mlog_errno(ret); 1938 goto out; 1939 } 1940 1941 di = (struct ocfs2_dinode *) di_bh->b_data; 1942 1943 osb->slot_recovery_generations[slot] = 1944 ocfs2_get_recovery_generation(di); 1945 1946 if (le32_to_cpu(di->id1.journal1.ij_flags) & 1947 OCFS2_JOURNAL_DIRTY_FL) 1948 journal_dirty = 1; 1949 1950 brelse(di_bh); 1951 di_bh = NULL; 1952 } 1953 1954 out: 1955 if (journal_dirty) 1956 ret = -EROFS; 1957 return ret; 1958 } 1959