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