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