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