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