blob: 562ba652593eefb87aadc5c8c81ed949f139baff [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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 Fasheh316f4b92007-09-07 18:21:26 -070038#include "dir.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#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 Fashehccd979b2005-12-15 14:31:24 -080045#include "slot_map.h"
46#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080047#include "sysfile.h"
48
49#include "buffer_head_io.h"
50
Ingo Molnar34af9462006-06-27 02:53:55 -070051DEFINE_SPINLOCK(trans_inc_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -080052
53static int ocfs2_force_read_journal(struct inode *inode);
54static int ocfs2_recover_node(struct ocfs2_super *osb,
55 int node_num);
56static int __ocfs2_recovery_thread(void *arg);
57static int ocfs2_commit_cache(struct ocfs2_super *osb);
58static int ocfs2_wait_on_mount(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -080059static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -070060 int dirty, int replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -080061static int ocfs2_trylock_journal(struct ocfs2_super *osb,
62 int slot_num);
63static int ocfs2_recover_orphans(struct ocfs2_super *osb,
64 int slot);
65static int ocfs2_commit_thread(void *arg);
66
Joel Becker553abd02008-02-01 12:03:57 -080067
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
73struct ocfs2_recovery_map {
Joel Beckerfc881fa2008-02-01 12:04:48 -080074 unsigned int rm_used;
Joel Becker553abd02008-02-01 12:03:57 -080075 unsigned int *rm_entries;
76};
77
78int 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 */
105static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
106{
107 mb();
108 return osb->recovery_thread_task != NULL;
109}
110
111void 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
137static 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 */
154static 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
175static 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 Fashehccd979b2005-12-15 14:31:24 -0800198static 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 Becker2b4e30f2008-09-03 20:03:41 -0700218 jbd2_journal_lock_updates(journal->j_journal);
219 status = jbd2_journal_flush(journal->j_journal);
220 jbd2_journal_unlock_updates(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800221 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 Fasheh34d024f2007-09-24 15:56:19 -0700236 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800237 wake_up(&journal->j_checkpointed);
238finally:
239 mlog_exit(status);
240 return status;
241}
242
Mark Fashehccd979b2005-12-15 14:31:24 -0800243/* 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 Fasheh1fabe142006-10-09 18:11:45 -0700246handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
Mark Fashehccd979b2005-12-15 14:31:24 -0800247{
Mark Fashehccd979b2005-12-15 14:31:24 -0800248 journal_t *journal = osb->journal->j_journal;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700249 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800250
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100251 BUG_ON(!osb || !osb->journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800252
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700253 if (ocfs2_is_hard_readonly(osb))
254 return ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800255
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 Fashehccd979b2005-12-15 14:31:24 -0800265 down_read(&osb->journal->j_trans_barrier);
266
Joel Becker2b4e30f2008-09-03 20:03:41 -0700267 handle = jbd2_journal_start(journal, max_buffs);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700268 if (IS_ERR(handle)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800269 up_read(&osb->journal->j_trans_barrier);
270
Mark Fasheh1fabe142006-10-09 18:11:45 -0700271 mlog_errno(PTR_ERR(handle));
Mark Fashehccd979b2005-12-15 14:31:24 -0800272
273 if (is_journal_aborted(journal)) {
274 ocfs2_abort(osb->sb, "Detected aborted journal");
Mark Fasheh1fabe142006-10-09 18:11:45 -0700275 handle = ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800276 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800277 } else {
278 if (!ocfs2_mount_local(osb))
279 atomic_inc(&(osb->journal->j_num_trans));
280 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800281
Mark Fashehccd979b2005-12-15 14:31:24 -0800282 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800283}
284
Mark Fasheh1fabe142006-10-09 18:11:45 -0700285int ocfs2_commit_trans(struct ocfs2_super *osb,
286 handle_t *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800287{
Mark Fasheh1fabe142006-10-09 18:11:45 -0700288 int ret;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700289 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800290
291 BUG_ON(!handle);
292
Joel Becker2b4e30f2008-09-03 20:03:41 -0700293 ret = jbd2_journal_stop(handle);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700294 if (ret < 0)
295 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800296
297 up_read(&journal->j_trans_barrier);
298
Mark Fasheh1fabe142006-10-09 18:11:45 -0700299 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800300}
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 Becker2b4e30f2008-09-03 20:03:41 -0700307 * This might call jbd2_journal_restart() which will commit dirty buffers
Mark Fashehe8aed342007-12-03 16:43:01 -0800308 * 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 Fashehccd979b2005-12-15 14:31:24 -0800313 * 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 Fasheh1fc58142006-10-05 14:15:36 -0700321int ocfs2_extend_trans(handle_t *handle, int nblocks)
Mark Fashehccd979b2005-12-15 14:31:24 -0800322{
323 int status;
324
325 BUG_ON(!handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800326 BUG_ON(!nblocks);
327
328 mlog_entry_void();
329
330 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
331
Joel Beckere407e392008-06-12 22:35:39 -0700332#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fasheh0879c582007-12-03 16:42:19 -0800333 status = 1;
334#else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700335 status = jbd2_journal_extend(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800336 if (status < 0) {
337 mlog_errno(status);
338 goto bail;
339 }
Mark Fasheh0879c582007-12-03 16:42:19 -0800340#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800341
342 if (status > 0) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700343 mlog(0,
344 "jbd2_journal_extend failed, trying "
345 "jbd2_journal_restart\n");
346 status = jbd2_journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800347 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 mlog_errno(status);
349 goto bail;
350 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700351 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800352
353 status = 0;
354bail:
355
356 mlog_exit(status);
357 return status;
358}
359
Mark Fasheh1fabe142006-10-09 18:11:45 -0700360int ocfs2_journal_access(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800361 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 Fashehccd979b2005-12-15 14:31:24 -0800370
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800371 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800372 (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 Fasheh251b6ec2006-01-10 15:41:43 -0800394 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800395 switch (type) {
396 case OCFS2_JOURNAL_ACCESS_CREATE:
397 case OCFS2_JOURNAL_ACCESS_WRITE:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700398 status = jbd2_journal_get_write_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800399 break;
400
401 case OCFS2_JOURNAL_ACCESS_UNDO:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700402 status = jbd2_journal_get_undo_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 break;
404
405 default:
406 status = -EINVAL;
407 mlog(ML_ERROR, "Uknown access type!\n");
408 }
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800409 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800410
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 Fasheh1fabe142006-10-09 18:11:45 -0700419int ocfs2_journal_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800420 struct buffer_head *bh)
421{
422 int status;
423
Mark Fashehccd979b2005-12-15 14:31:24 -0800424 mlog_entry("(bh->b_blocknr=%llu)\n",
425 (unsigned long long)bh->b_blocknr);
426
Joel Becker2b4e30f2008-09-03 20:03:41 -0700427 status = jbd2_journal_dirty_metadata(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800428 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 Becker2b4e30f2008-09-03 20:03:41 -0700437#ifdef CONFIG_OCFS2_COMPAT_JBD
Mark Fashehccd979b2005-12-15 14:31:24 -0800438int 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 Becker2b4e30f2008-09-03 20:03:41 -0700449#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800450
Joel Becker2b4e30f2008-09-03 20:03:41 -0700451#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800452
453void ocfs2_set_journal_params(struct ocfs2_super *osb)
454{
455 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800456 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
457
458 if (osb->osb_commit_interval)
459 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800460
461 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800462 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800463 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700464 journal->j_flags |= JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700466 journal->j_flags &= ~JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800467 spin_unlock(&journal->j_state_lock);
468}
469
470int 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 Fashehe63aecb62007-10-18 15:30:42 -0700478 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800479
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 Fasheh6eff5792006-01-18 10:31:47 -0800505 /* 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 Fashehe63aecb62007-10-18 15:30:42 -0700508 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800509 if (status < 0) {
510 if (status != -ERESTARTSYS)
511 mlog(ML_ERROR, "Could not get lock on journal!\n");
512 goto done;
513 }
514
Mark Fashehe63aecb62007-10-18 15:30:42 -0700515 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 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 Morton5515eff2006-03-26 01:37:53 -0800526 mlog(0, "inode->i_blocks = %llu\n",
527 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800528 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
529
530 /* call the kernels journal init function now */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700531 j_journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800532 if (j_journal == NULL) {
533 mlog(ML_ERROR, "Linux journal layer error\n");
534 status = -EINVAL;
535 goto done;
536 }
537
Joel Becker2b4e30f2008-09-03 20:03:41 -0700538 mlog(0, "Returned from jbd2_journal_init_inode\n");
Mark Fashehccd979b2005-12-15 14:31:24 -0800539 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;
553done:
554 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700555 if (inode_lock)
556 ocfs2_inode_unlock(inode, 1);
Mark Fasheha81cb882008-10-07 14:25:16 -0700557 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800558 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 Mushran539d8262008-07-14 17:31:10 -0700568static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
569{
570 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
571}
572
573static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
574{
575 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
576}
577
Mark Fashehccd979b2005-12-15 14:31:24 -0800578static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -0700579 int dirty, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800580{
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 Fashehb06970532006-03-03 10:24:33 -0800594 mlog(ML_ERROR, "Journal dinode %llu has invalid "
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700595 "signature: %.*s",
596 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
Mark Fashehb06970532006-03-03 10:24:33 -0800597 fe->i_signature);
Mark Fashehccd979b2005-12-15 14:31:24 -0800598 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 Mushran539d8262008-07-14 17:31:10 -0700609 if (replayed)
610 ocfs2_bump_recovery_generation(fe);
611
Mark Fashehccd979b2005-12-15 14:31:24 -0800612 status = ocfs2_write_block(osb, bh, journal->j_inode);
613 if (status < 0)
614 mlog_errno(status);
615
616out:
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 */
625void 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 / snakebyteebdec832006-01-27 10:32:52 +0100634 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800635
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 Becker2b4e30f2008-09-03 20:03:41 -0700645 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800646 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 Mushranc271c5c2006-12-05 17:56:35 -0800673 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700674 jbd2_journal_lock_updates(journal->j_journal);
675 status = jbd2_journal_flush(journal->j_journal);
676 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800677 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 Mushran539d8262008-07-14 17:31:10 -0700686 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800687 if (status < 0)
688 mlog_errno(status);
689 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800690
691 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700692 jbd2_journal_destroy(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800693
694 OCFS2_I(inode)->ip_open_count--;
695
696 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700697 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800698
699 brelse(journal->j_bh);
700 journal->j_bh = NULL;
701
702 journal->j_state = OCFS2_JOURNAL_FREE;
703
704// up_write(&journal->j_trans_barrier);
705done:
706 if (inode)
707 iput(inode);
708 mlog_exit_void();
709}
710
711static void ocfs2_clear_journal_error(struct super_block *sb,
712 journal_t *journal,
713 int slot)
714{
715 int olderr;
716
Joel Becker2b4e30f2008-09-03 20:03:41 -0700717 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800718 if (olderr) {
719 mlog(ML_ERROR, "File system error %d recorded in "
720 "journal %u.\n", olderr, slot);
721 mlog(ML_ERROR, "File system on device %s needs checking.\n",
722 sb->s_id);
723
Joel Becker2b4e30f2008-09-03 20:03:41 -0700724 jbd2_journal_ack_err(journal);
725 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 }
727}
728
Sunil Mushran539d8262008-07-14 17:31:10 -0700729int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800730{
731 int status = 0;
732 struct ocfs2_super *osb;
733
734 mlog_entry_void();
735
Julia Lawallb1f35502008-03-04 15:21:05 -0800736 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800737
738 osb = journal->j_osb;
739
Joel Becker2b4e30f2008-09-03 20:03:41 -0700740 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 if (status < 0) {
742 mlog(ML_ERROR, "Failed to load journal!\n");
743 goto done;
744 }
745
746 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
747
Sunil Mushran539d8262008-07-14 17:31:10 -0700748 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 if (status < 0) {
750 mlog_errno(status);
751 goto done;
752 }
753
754 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800755 if (!local) {
756 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
757 "ocfs2cmt");
758 if (IS_ERR(osb->commit_task)) {
759 status = PTR_ERR(osb->commit_task);
760 osb->commit_task = NULL;
761 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
762 "error=%d", status);
763 goto done;
764 }
765 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800766 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800767
768done:
769 mlog_exit(status);
770 return status;
771}
772
773
774/* 'full' flag tells us whether we clear out all blocks or if we just
775 * mark the journal clean */
776int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
777{
778 int status;
779
780 mlog_entry_void();
781
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100782 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800783
Joel Becker2b4e30f2008-09-03 20:03:41 -0700784 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 if (status < 0) {
786 mlog_errno(status);
787 goto bail;
788 }
789
Sunil Mushran539d8262008-07-14 17:31:10 -0700790 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 if (status < 0)
792 mlog_errno(status);
793
794bail:
795 mlog_exit(status);
796 return status;
797}
798
Joel Becker553abd02008-02-01 12:03:57 -0800799static int ocfs2_recovery_completed(struct ocfs2_super *osb)
800{
801 int empty;
802 struct ocfs2_recovery_map *rm = osb->recovery_map;
803
804 spin_lock(&osb->osb_lock);
805 empty = (rm->rm_used == 0);
806 spin_unlock(&osb->osb_lock);
807
808 return empty;
809}
810
811void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
812{
813 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
814}
815
Mark Fashehccd979b2005-12-15 14:31:24 -0800816/*
817 * JBD Might read a cached version of another nodes journal file. We
818 * don't want this as this file changes often and we get no
819 * notification on those changes. The only way to be sure that we've
820 * got the most up to date version of those blocks then is to force
821 * read them off disk. Just searching through the buffer cache won't
822 * work as there may be pages backing this file which are still marked
823 * up to date. We know things can't change on this file underneath us
824 * as we have the lock by now :)
825 */
826static int ocfs2_force_read_journal(struct inode *inode)
827{
828 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800829 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700830 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800831#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800832 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
833
834 mlog_entry_void();
835
Mark Fashehccd979b2005-12-15 14:31:24 -0800836 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
837
Mark Fasheh8110b072007-03-22 16:53:23 -0700838 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800839 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700840 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800841 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800842 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800843 if (status < 0) {
844 mlog_errno(status);
845 goto bail;
846 }
847
848 if (p_blocks > CONCURRENT_JOURNAL_FILL)
849 p_blocks = CONCURRENT_JOURNAL_FILL;
850
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700851 /* We are reading journal data which should not
852 * be put in the uptodate cache */
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
854 p_blkno, p_blocks, bhs, 0,
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700855 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800856 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
869bail:
870 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -0700871 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800872 mlog_exit(status);
873 return status;
874}
875
876struct 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 Howellsc4028952006-11-22 14:57:56 +0000893void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -0800894{
895 int ret;
David Howellsc4028952006-11-22 14:57:56 +0000896 struct ocfs2_journal *journal =
897 container_of(work, struct ocfs2_journal, j_recovery_work);
898 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -0800899 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +0200900 struct ocfs2_la_recovery_item *item, *n;
Mark Fashehccd979b2005-12-15 14:31:24 -0800901 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 Hellwig800deef2007-05-17 16:03:13 +0200911 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800912 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 Fashehb06970532006-03-03 10:24:33 -0800918 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700919 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800920
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 Fashehb06970532006-03-03 10:24:33 -0800931 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700932 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800933
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 */
956static 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 Mushranafae00ab2006-04-12 14:37:00 -0700963 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 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. */
991void 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
1009static int __ocfs2_recovery_thread(void *arg)
1010{
1011 int status, node_num;
1012 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001013 struct ocfs2_recovery_map *rm = osb->recovery_map;
Mark Fashehccd979b2005-12-15 14:31:24 -08001014
1015 mlog_entry_void();
1016
1017 status = ocfs2_wait_on_mount(osb);
1018 if (status < 0) {
1019 goto bail;
1020 }
1021
1022restart:
1023 status = ocfs2_super_lock(osb, 1);
1024 if (status < 0) {
1025 mlog_errno(status);
1026 goto bail;
1027 }
1028
Joel Becker553abd02008-02-01 12:03:57 -08001029 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 Fashehccd979b2005-12-15 14:31:24 -08001035
1036 status = ocfs2_recover_node(osb, node_num);
Joel Becker553abd02008-02-01 12:03:57 -08001037 if (!status) {
1038 ocfs2_recovery_map_clear(osb, node_num);
1039 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 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 Fashehccd979b2005-12-15 14:31:24 -08001045 }
1046
Joel Becker553abd02008-02-01 12:03:57 -08001047 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001048 }
Joel Becker553abd02008-02-01 12:03:57 -08001049 spin_unlock(&osb->osb_lock);
1050 mlog(0, "All nodes recovered\n");
1051
Sunil Mushran539d8262008-07-14 17:31:10 -07001052 /* 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 Fashehccd979b2005-12-15 14:31:24 -08001058 ocfs2_super_unlock(osb, 1);
1059
1060 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001061 * node(s) may have disallowd a previos inode delete. Re-processing
1062 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1064 NULL);
1065
1066bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001067 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001068 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001069 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 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 Venc74ec2f2006-01-13 21:54:23 -08001077 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001078
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
1087void 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 Venc74ec2f2006-01-13 21:54:23 -08001092 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 if (osb->disable_recovery)
1094 goto out;
1095
1096 /* People waiting on recovery will wait on
1097 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001098 if (ocfs2_recovery_map_set(osb, node_num))
1099 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001100
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 Fasheh78427042006-05-04 12:03:26 -07001107 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 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
1113out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001114 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001115 wake_up(&osb->recovery_event);
1116
1117 mlog_exit_void();
1118}
1119
Sunil Mushran539d8262008-07-14 17:31:10 -07001120static 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
1138 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, bh, 0, inode);
1139 if (status < 0) {
1140 mlog_errno(status);
1141 goto bail;
1142 }
1143
1144 status = 0;
1145
1146bail:
1147 if (inode) {
1148 if (status || !ret_inode)
1149 iput(inode);
1150 else
1151 *ret_inode = inode;
1152 }
1153 return status;
1154}
1155
Mark Fashehccd979b2005-12-15 14:31:24 -08001156/* Does the actual journal replay and marks the journal inode as
1157 * clean. Will only replay if the journal inode is marked dirty. */
1158static int ocfs2_replay_journal(struct ocfs2_super *osb,
1159 int node_num,
1160 int slot_num)
1161{
1162 int status;
1163 int got_lock = 0;
1164 unsigned int flags;
1165 struct inode *inode = NULL;
1166 struct ocfs2_dinode *fe;
1167 journal_t *journal = NULL;
1168 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001169 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001170
Sunil Mushran539d8262008-07-14 17:31:10 -07001171 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1172 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001173 mlog_errno(status);
1174 goto done;
1175 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001176
1177 fe = (struct ocfs2_dinode *)bh->b_data;
1178 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1179 brelse(bh);
1180 bh = NULL;
1181
1182 /*
1183 * As the fs recovery is asynchronous, there is a small chance that
1184 * another node mounted (and recovered) the slot before the recovery
1185 * thread could get the lock. To handle that, we dirty read the journal
1186 * inode for that slot to get the recovery generation. If it is
1187 * different than what we expected, the slot has been recovered.
1188 * If not, it needs recovery.
1189 */
1190 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1191 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1192 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1193 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1194 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001195 goto done;
1196 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001197
1198 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001199
Mark Fashehe63aecb62007-10-18 15:30:42 -07001200 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001202 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001203 if (status != -ERESTARTSYS)
1204 mlog(ML_ERROR, "Could not lock journal!\n");
1205 goto done;
1206 }
1207 got_lock = 1;
1208
1209 fe = (struct ocfs2_dinode *) bh->b_data;
1210
1211 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001212 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001213
1214 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1215 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001216 /* Refresh recovery generation for the slot */
1217 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001218 goto done;
1219 }
1220
1221 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1222 node_num, slot_num,
1223 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1224
1225 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1226
1227 status = ocfs2_force_read_journal(inode);
1228 if (status < 0) {
1229 mlog_errno(status);
1230 goto done;
1231 }
1232
1233 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001234 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001235 if (journal == NULL) {
1236 mlog(ML_ERROR, "Linux journal layer error\n");
1237 status = -EIO;
1238 goto done;
1239 }
1240
Joel Becker2b4e30f2008-09-03 20:03:41 -07001241 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001242 if (status < 0) {
1243 mlog_errno(status);
1244 if (!igrab(inode))
1245 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001246 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001247 goto done;
1248 }
1249
1250 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1251
1252 /* wipe the journal */
1253 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001254 jbd2_journal_lock_updates(journal);
1255 status = jbd2_journal_flush(journal);
1256 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001257 if (status < 0)
1258 mlog_errno(status);
1259
1260 /* This will mark the node clean */
1261 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1262 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1263 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1264
Sunil Mushran539d8262008-07-14 17:31:10 -07001265 /* Increment recovery generation to indicate successful recovery */
1266 ocfs2_bump_recovery_generation(fe);
1267 osb->slot_recovery_generations[slot_num] =
1268 ocfs2_get_recovery_generation(fe);
1269
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 status = ocfs2_write_block(osb, bh, inode);
1271 if (status < 0)
1272 mlog_errno(status);
1273
1274 if (!igrab(inode))
1275 BUG();
1276
Joel Becker2b4e30f2008-09-03 20:03:41 -07001277 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001278
1279done:
1280 /* drop the lock on this nodes journal */
1281 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001282 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001283
1284 if (inode)
1285 iput(inode);
1286
Mark Fasheha81cb882008-10-07 14:25:16 -07001287 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001288
1289 mlog_exit(status);
1290 return status;
1291}
1292
1293/*
1294 * Do the most important parts of node recovery:
1295 * - Replay it's journal
1296 * - Stamp a clean local allocator file
1297 * - Stamp a clean truncate log
1298 * - Mark the node clean
1299 *
1300 * If this function completes without error, a node in OCFS2 can be
1301 * said to have been safely recovered. As a result, failure during the
1302 * second part of a nodes recovery process (local alloc recovery) is
1303 * far less concerning.
1304 */
1305static int ocfs2_recover_node(struct ocfs2_super *osb,
1306 int node_num)
1307{
1308 int status = 0;
1309 int slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 struct ocfs2_dinode *la_copy = NULL;
1311 struct ocfs2_dinode *tl_copy = NULL;
1312
1313 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1314 node_num, osb->node_num);
1315
1316 mlog(0, "checking node %d\n", node_num);
1317
1318 /* Should not ever be called to recover ourselves -- in that
1319 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001320 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001321
Joel Beckerd85b20e2008-02-01 12:01:05 -08001322 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1323 if (slot_num == -ENOENT) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001324 status = 0;
1325 mlog(0, "no slot for this node, so no recovery required.\n");
1326 goto done;
1327 }
1328
1329 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1330
1331 status = ocfs2_replay_journal(osb, node_num, slot_num);
1332 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001333 if (status == -EBUSY) {
1334 mlog(0, "Skipping recovery for slot %u (node %u) "
1335 "as another node has recovered it\n", slot_num,
1336 node_num);
1337 status = 0;
1338 goto done;
1339 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 mlog_errno(status);
1341 goto done;
1342 }
1343
1344 /* Stamp a clean local alloc file AFTER recovering the journal... */
1345 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1346 if (status < 0) {
1347 mlog_errno(status);
1348 goto done;
1349 }
1350
1351 /* An error from begin_truncate_log_recovery is not
1352 * serious enough to warrant halting the rest of
1353 * recovery. */
1354 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1355 if (status < 0)
1356 mlog_errno(status);
1357
1358 /* Likewise, this would be a strange but ultimately not so
1359 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001360 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001361 if (status < 0)
1362 mlog_errno(status);
1363
1364 /* This will kfree the memory pointed to by la_copy and tl_copy */
1365 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1366 tl_copy);
1367
1368 status = 0;
1369done:
1370
1371 mlog_exit(status);
1372 return status;
1373}
1374
1375/* Test node liveness by trylocking his journal. If we get the lock,
1376 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1377 * still alive (we couldn't get the lock) and < 0 on error. */
1378static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1379 int slot_num)
1380{
1381 int status, flags;
1382 struct inode *inode = NULL;
1383
1384 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1385 slot_num);
1386 if (inode == NULL) {
1387 mlog(ML_ERROR, "access error\n");
1388 status = -EACCES;
1389 goto bail;
1390 }
1391 if (is_bad_inode(inode)) {
1392 mlog(ML_ERROR, "access error (bad inode)\n");
1393 iput(inode);
1394 inode = NULL;
1395 status = -EACCES;
1396 goto bail;
1397 }
1398 SET_INODE_JOURNAL(inode);
1399
1400 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001401 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001402 if (status < 0) {
1403 if (status != -EAGAIN)
1404 mlog_errno(status);
1405 goto bail;
1406 }
1407
Mark Fashehe63aecb62007-10-18 15:30:42 -07001408 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001409bail:
1410 if (inode)
1411 iput(inode);
1412
1413 return status;
1414}
1415
1416/* Call this underneath ocfs2_super_lock. It also assumes that the
1417 * slot info struct has been updated from disk. */
1418int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1419{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001420 unsigned int node_num;
1421 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001422 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001423 struct buffer_head *bh = NULL;
1424 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001425
1426 /* This is called with the super block cluster lock, so we
1427 * know that the slot map can't change underneath us. */
1428
Joel Beckerd85b20e2008-02-01 12:01:05 -08001429 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001430 /* Read journal inode to get the recovery generation */
1431 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1432 if (status) {
1433 mlog_errno(status);
1434 goto bail;
1435 }
1436 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001437 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001438 brelse(bh);
1439 bh = NULL;
1440
Mark Fasheha1af7d12008-08-19 17:20:28 -07001441 spin_lock(&osb->osb_lock);
1442 osb->slot_recovery_generations[i] = gen;
1443
Sunil Mushran539d8262008-07-14 17:31:10 -07001444 mlog(0, "Slot %u recovery generation is %u\n", i,
1445 osb->slot_recovery_generations[i]);
1446
Mark Fasheha1af7d12008-08-19 17:20:28 -07001447 if (i == osb->slot_num) {
1448 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001449 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001450 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001451
1452 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001453 if (status == -ENOENT) {
1454 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001455 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001456 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001457
Mark Fasheha1af7d12008-08-19 17:20:28 -07001458 if (__ocfs2_recovery_map_test(osb, node_num)) {
1459 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001461 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001462 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001463
1464 /* Ok, we have a slot occupied by another node which
1465 * is not in the recovery map. We trylock his journal
1466 * file here to test if he's alive. */
1467 status = ocfs2_trylock_journal(osb, i);
1468 if (!status) {
1469 /* Since we're called from mount, we know that
1470 * the recovery thread can't race us on
1471 * setting / checking the recovery bits. */
1472 ocfs2_recovery_thread(osb, node_num);
1473 } else if ((status < 0) && (status != -EAGAIN)) {
1474 mlog_errno(status);
1475 goto bail;
1476 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001477 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001478
1479 status = 0;
1480bail:
1481 mlog_exit(status);
1482 return status;
1483}
1484
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001485struct ocfs2_orphan_filldir_priv {
1486 struct inode *head;
1487 struct ocfs2_super *osb;
1488};
1489
1490static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1491 loff_t pos, u64 ino, unsigned type)
1492{
1493 struct ocfs2_orphan_filldir_priv *p = priv;
1494 struct inode *iter;
1495
1496 if (name_len == 1 && !strncmp(".", name, 1))
1497 return 0;
1498 if (name_len == 2 && !strncmp("..", name, 2))
1499 return 0;
1500
1501 /* Skip bad inodes so that recovery can continue */
1502 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001503 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001504 if (IS_ERR(iter))
1505 return 0;
1506
1507 mlog(0, "queue orphan %llu\n",
1508 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1509 /* No locking is required for the next_orphan queue as there
1510 * is only ever a single process doing orphan recovery. */
1511 OCFS2_I(iter)->ip_next_orphan = p->head;
1512 p->head = iter;
1513
1514 return 0;
1515}
1516
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001517static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1518 int slot,
1519 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001520{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001521 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001522 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001523 struct ocfs2_orphan_filldir_priv priv;
1524 loff_t pos = 0;
1525
1526 priv.osb = osb;
1527 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001528
1529 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1530 ORPHAN_DIR_SYSTEM_INODE,
1531 slot);
1532 if (!orphan_dir_inode) {
1533 status = -ENOENT;
1534 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001535 return status;
1536 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001537
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001538 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001539 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001540 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001541 mlog_errno(status);
1542 goto out;
1543 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001544
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001545 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1546 ocfs2_orphan_filldir);
1547 if (status) {
1548 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001549 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001550 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001551
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001552 *head = priv.head;
1553
Mark Fasheha86370f2007-12-03 14:06:23 -08001554out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001555 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001556out:
1557 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001558 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001559 return status;
1560}
1561
1562static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1563 int slot)
1564{
1565 int ret;
1566
1567 spin_lock(&osb->osb_lock);
1568 ret = !osb->osb_orphan_wipes[slot];
1569 spin_unlock(&osb->osb_lock);
1570 return ret;
1571}
1572
1573static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1574 int slot)
1575{
1576 spin_lock(&osb->osb_lock);
1577 /* Mark ourselves such that new processes in delete_inode()
1578 * know to quit early. */
1579 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1580 while (osb->osb_orphan_wipes[slot]) {
1581 /* If any processes are already in the middle of an
1582 * orphan wipe on this dir, then we need to wait for
1583 * them. */
1584 spin_unlock(&osb->osb_lock);
1585 wait_event_interruptible(osb->osb_wipe_event,
1586 ocfs2_orphan_recovery_can_continue(osb, slot));
1587 spin_lock(&osb->osb_lock);
1588 }
1589 spin_unlock(&osb->osb_lock);
1590}
1591
1592static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1593 int slot)
1594{
1595 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1596}
1597
1598/*
1599 * Orphan recovery. Each mounted node has it's own orphan dir which we
1600 * must run during recovery. Our strategy here is to build a list of
1601 * the inodes in the orphan dir and iget/iput them. The VFS does
1602 * (most) of the rest of the work.
1603 *
1604 * Orphan recovery can happen at any time, not just mount so we have a
1605 * couple of extra considerations.
1606 *
1607 * - We grab as many inodes as we can under the orphan dir lock -
1608 * doing iget() outside the orphan dir risks getting a reference on
1609 * an invalid inode.
1610 * - We must be sure not to deadlock with other processes on the
1611 * system wanting to run delete_inode(). This can happen when they go
1612 * to lock the orphan dir and the orphan recovery process attempts to
1613 * iget() inside the orphan dir lock. This can be avoided by
1614 * advertising our state to ocfs2_delete_inode().
1615 */
1616static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1617 int slot)
1618{
1619 int ret = 0;
1620 struct inode *inode = NULL;
1621 struct inode *iter;
1622 struct ocfs2_inode_info *oi;
1623
1624 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1625
1626 ocfs2_mark_recovering_orphan_dir(osb, slot);
1627 ret = ocfs2_queue_orphans(osb, slot, &inode);
1628 ocfs2_clear_recovering_orphan_dir(osb, slot);
1629
1630 /* Error here should be noted, but we want to continue with as
1631 * many queued inodes as we've got. */
1632 if (ret)
1633 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001634
1635 while (inode) {
1636 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001637 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001638
1639 iter = oi->ip_next_orphan;
1640
1641 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001642 /* The remote delete code may have set these on the
1643 * assumption that the other node would wipe them
1644 * successfully. If they are still in the node's
1645 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001646 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1647
1648 /* Set the proper information to get us going into
1649 * ocfs2_delete_inode. */
1650 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001651 spin_unlock(&oi->ip_lock);
1652
1653 iput(inode);
1654
1655 inode = iter;
1656 }
1657
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001658 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001659}
1660
1661static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
1662{
1663 /* This check is good because ocfs2 will wait on our recovery
1664 * thread before changing it to something other than MOUNTED
1665 * or DISABLED. */
1666 wait_event(osb->osb_mount_event,
1667 atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
1668 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1669
1670 /* If there's an error on mount, then we may never get to the
1671 * MOUNTED flag, but this is set right before
1672 * dismount_volume() so we can trust it. */
1673 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1674 mlog(0, "mount error, exiting!\n");
1675 return -EBUSY;
1676 }
1677
1678 return 0;
1679}
1680
1681static int ocfs2_commit_thread(void *arg)
1682{
1683 int status;
1684 struct ocfs2_super *osb = arg;
1685 struct ocfs2_journal *journal = osb->journal;
1686
1687 /* we can trust j_num_trans here because _should_stop() is only set in
1688 * shutdown and nobody other than ourselves should be able to start
1689 * transactions. committing on shutdown might take a few iterations
1690 * as final transactions put deleted inodes on the list */
1691 while (!(kthread_should_stop() &&
1692 atomic_read(&journal->j_num_trans) == 0)) {
1693
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001694 wait_event_interruptible(osb->checkpoint_event,
1695 atomic_read(&journal->j_num_trans)
1696 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001697
1698 status = ocfs2_commit_cache(osb);
1699 if (status < 0)
1700 mlog_errno(status);
1701
1702 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1703 mlog(ML_KTHREAD,
1704 "commit_thread: %u transactions pending on "
1705 "shutdown\n",
1706 atomic_read(&journal->j_num_trans));
1707 }
1708 }
1709
1710 return 0;
1711}
1712
Sunil Mushran539d8262008-07-14 17:31:10 -07001713/* Reads all the journal inodes without taking any cluster locks. Used
1714 * for hard readonly access to determine whether any journal requires
1715 * recovery. Also used to refresh the recovery generation numbers after
1716 * a journal has been recovered by another node.
1717 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001718int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1719{
1720 int ret = 0;
1721 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001722 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001723 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001724 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001725
1726 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001727 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1728 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001729 mlog_errno(ret);
1730 goto out;
1731 }
1732
1733 di = (struct ocfs2_dinode *) di_bh->b_data;
1734
Sunil Mushran539d8262008-07-14 17:31:10 -07001735 osb->slot_recovery_generations[slot] =
1736 ocfs2_get_recovery_generation(di);
1737
Mark Fashehccd979b2005-12-15 14:31:24 -08001738 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1739 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001740 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001741
1742 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001743 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001744 }
1745
1746out:
Sunil Mushran539d8262008-07-14 17:31:10 -07001747 if (journal_dirty)
1748 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08001749 return ret;
1750}