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