blob: 9223bfcca3ba8f3358dbd326ef1b7b90c9349598 [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;
Joel Becker10995aa2008-11-13 14:49:12 -0800590
591 /* The journal bh on the osb always comes from ocfs2_journal_init()
592 * and was validated there inside ocfs2_inode_lock_full(). It's a
593 * code bug if we mess it up. */
594 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800595
596 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
597 if (dirty)
598 flags |= OCFS2_JOURNAL_DIRTY_FL;
599 else
600 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
601 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
602
Sunil Mushran539d8262008-07-14 17:31:10 -0700603 if (replayed)
604 ocfs2_bump_recovery_generation(fe);
605
Mark Fashehccd979b2005-12-15 14:31:24 -0800606 status = ocfs2_write_block(osb, bh, journal->j_inode);
607 if (status < 0)
608 mlog_errno(status);
609
Mark Fashehccd979b2005-12-15 14:31:24 -0800610 mlog_exit(status);
611 return status;
612}
613
614/*
615 * If the journal has been kmalloc'd it needs to be freed after this
616 * call.
617 */
618void ocfs2_journal_shutdown(struct ocfs2_super *osb)
619{
620 struct ocfs2_journal *journal = NULL;
621 int status = 0;
622 struct inode *inode = NULL;
623 int num_running_trans = 0;
624
625 mlog_entry_void();
626
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100627 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800628
629 journal = osb->journal;
630 if (!journal)
631 goto done;
632
633 inode = journal->j_inode;
634
635 if (journal->j_state != OCFS2_JOURNAL_LOADED)
636 goto done;
637
Joel Becker2b4e30f2008-09-03 20:03:41 -0700638 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 if (!igrab(inode))
640 BUG();
641
642 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
643 if (num_running_trans > 0)
644 mlog(0, "Shutting down journal: must wait on %d "
645 "running transactions!\n",
646 num_running_trans);
647
648 /* Do a commit_cache here. It will flush our journal, *and*
649 * release any locks that are still held.
650 * set the SHUTDOWN flag and release the trans lock.
651 * the commit thread will take the trans lock for us below. */
652 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
653
654 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
655 * drop the trans_lock (which we want to hold until we
656 * completely destroy the journal. */
657 if (osb->commit_task) {
658 /* Wait for the commit thread */
659 mlog(0, "Waiting for ocfs2commit to exit....\n");
660 kthread_stop(osb->commit_task);
661 osb->commit_task = NULL;
662 }
663
664 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
665
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800666 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700667 jbd2_journal_lock_updates(journal->j_journal);
668 status = jbd2_journal_flush(journal->j_journal);
669 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800670 if (status < 0)
671 mlog_errno(status);
672 }
673
674 if (status == 0) {
675 /*
676 * Do not toggle if flush was unsuccessful otherwise
677 * will leave dirty metadata in a "clean" journal
678 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700679 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800680 if (status < 0)
681 mlog_errno(status);
682 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800683
684 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700685 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700686 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800687
688 OCFS2_I(inode)->ip_open_count--;
689
690 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700691 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800692
693 brelse(journal->j_bh);
694 journal->j_bh = NULL;
695
696 journal->j_state = OCFS2_JOURNAL_FREE;
697
698// up_write(&journal->j_trans_barrier);
699done:
700 if (inode)
701 iput(inode);
702 mlog_exit_void();
703}
704
705static void ocfs2_clear_journal_error(struct super_block *sb,
706 journal_t *journal,
707 int slot)
708{
709 int olderr;
710
Joel Becker2b4e30f2008-09-03 20:03:41 -0700711 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800712 if (olderr) {
713 mlog(ML_ERROR, "File system error %d recorded in "
714 "journal %u.\n", olderr, slot);
715 mlog(ML_ERROR, "File system on device %s needs checking.\n",
716 sb->s_id);
717
Joel Becker2b4e30f2008-09-03 20:03:41 -0700718 jbd2_journal_ack_err(journal);
719 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800720 }
721}
722
Sunil Mushran539d8262008-07-14 17:31:10 -0700723int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800724{
725 int status = 0;
726 struct ocfs2_super *osb;
727
728 mlog_entry_void();
729
Julia Lawallb1f35502008-03-04 15:21:05 -0800730 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800731
732 osb = journal->j_osb;
733
Joel Becker2b4e30f2008-09-03 20:03:41 -0700734 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800735 if (status < 0) {
736 mlog(ML_ERROR, "Failed to load journal!\n");
737 goto done;
738 }
739
740 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
741
Sunil Mushran539d8262008-07-14 17:31:10 -0700742 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800743 if (status < 0) {
744 mlog_errno(status);
745 goto done;
746 }
747
748 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800749 if (!local) {
750 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
751 "ocfs2cmt");
752 if (IS_ERR(osb->commit_task)) {
753 status = PTR_ERR(osb->commit_task);
754 osb->commit_task = NULL;
755 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
756 "error=%d", status);
757 goto done;
758 }
759 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800760 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800761
762done:
763 mlog_exit(status);
764 return status;
765}
766
767
768/* 'full' flag tells us whether we clear out all blocks or if we just
769 * mark the journal clean */
770int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
771{
772 int status;
773
774 mlog_entry_void();
775
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100776 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800777
Joel Becker2b4e30f2008-09-03 20:03:41 -0700778 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800779 if (status < 0) {
780 mlog_errno(status);
781 goto bail;
782 }
783
Sunil Mushran539d8262008-07-14 17:31:10 -0700784 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 if (status < 0)
786 mlog_errno(status);
787
788bail:
789 mlog_exit(status);
790 return status;
791}
792
Joel Becker553abd02008-02-01 12:03:57 -0800793static int ocfs2_recovery_completed(struct ocfs2_super *osb)
794{
795 int empty;
796 struct ocfs2_recovery_map *rm = osb->recovery_map;
797
798 spin_lock(&osb->osb_lock);
799 empty = (rm->rm_used == 0);
800 spin_unlock(&osb->osb_lock);
801
802 return empty;
803}
804
805void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
806{
807 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
808}
809
Mark Fashehccd979b2005-12-15 14:31:24 -0800810/*
811 * JBD Might read a cached version of another nodes journal file. We
812 * don't want this as this file changes often and we get no
813 * notification on those changes. The only way to be sure that we've
814 * got the most up to date version of those blocks then is to force
815 * read them off disk. Just searching through the buffer cache won't
816 * work as there may be pages backing this file which are still marked
817 * up to date. We know things can't change on this file underneath us
818 * as we have the lock by now :)
819 */
820static int ocfs2_force_read_journal(struct inode *inode)
821{
822 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800823 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700824 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800825#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
827
828 mlog_entry_void();
829
Mark Fashehccd979b2005-12-15 14:31:24 -0800830 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
831
Mark Fasheh8110b072007-03-22 16:53:23 -0700832 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700834 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800836 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 if (status < 0) {
838 mlog_errno(status);
839 goto bail;
840 }
841
842 if (p_blocks > CONCURRENT_JOURNAL_FILL)
843 p_blocks = CONCURRENT_JOURNAL_FILL;
844
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700845 /* We are reading journal data which should not
846 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -0700847 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
848 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800849 if (status < 0) {
850 mlog_errno(status);
851 goto bail;
852 }
853
854 for(i = 0; i < p_blocks; i++) {
855 brelse(bhs[i]);
856 bhs[i] = NULL;
857 }
858
859 v_blkno += p_blocks;
860 }
861
862bail:
863 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -0700864 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800865 mlog_exit(status);
866 return status;
867}
868
869struct ocfs2_la_recovery_item {
870 struct list_head lri_list;
871 int lri_slot;
872 struct ocfs2_dinode *lri_la_dinode;
873 struct ocfs2_dinode *lri_tl_dinode;
874};
875
876/* Does the second half of the recovery process. By this point, the
877 * node is marked clean and can actually be considered recovered,
878 * hence it's no longer in the recovery map, but there's still some
879 * cleanup we can do which shouldn't happen within the recovery thread
880 * as locking in that context becomes very difficult if we are to take
881 * recovering nodes into account.
882 *
883 * NOTE: This function can and will sleep on recovery of other nodes
884 * during cluster locking, just like any other ocfs2 process.
885 */
David Howellsc4028952006-11-22 14:57:56 +0000886void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -0800887{
888 int ret;
David Howellsc4028952006-11-22 14:57:56 +0000889 struct ocfs2_journal *journal =
890 container_of(work, struct ocfs2_journal, j_recovery_work);
891 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -0800892 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +0200893 struct ocfs2_la_recovery_item *item, *n;
Mark Fashehccd979b2005-12-15 14:31:24 -0800894 LIST_HEAD(tmp_la_list);
895
896 mlog_entry_void();
897
898 mlog(0, "completing recovery from keventd\n");
899
900 spin_lock(&journal->j_lock);
901 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
902 spin_unlock(&journal->j_lock);
903
Christoph Hellwig800deef2007-05-17 16:03:13 +0200904 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800905 list_del_init(&item->lri_list);
906
907 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
908
909 la_dinode = item->lri_la_dinode;
910 if (la_dinode) {
Mark Fashehb0697052006-03-03 10:24:33 -0800911 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700912 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800913
914 ret = ocfs2_complete_local_alloc_recovery(osb,
915 la_dinode);
916 if (ret < 0)
917 mlog_errno(ret);
918
919 kfree(la_dinode);
920 }
921
922 tl_dinode = item->lri_tl_dinode;
923 if (tl_dinode) {
Mark Fashehb0697052006-03-03 10:24:33 -0800924 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700925 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800926
927 ret = ocfs2_complete_truncate_log_recovery(osb,
928 tl_dinode);
929 if (ret < 0)
930 mlog_errno(ret);
931
932 kfree(tl_dinode);
933 }
934
935 ret = ocfs2_recover_orphans(osb, item->lri_slot);
936 if (ret < 0)
937 mlog_errno(ret);
938
939 kfree(item);
940 }
941
942 mlog(0, "Recovery completion\n");
943 mlog_exit_void();
944}
945
946/* NOTE: This function always eats your references to la_dinode and
947 * tl_dinode, either manually on error, or by passing them to
948 * ocfs2_complete_recovery */
949static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
950 int slot_num,
951 struct ocfs2_dinode *la_dinode,
952 struct ocfs2_dinode *tl_dinode)
953{
954 struct ocfs2_la_recovery_item *item;
955
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700956 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800957 if (!item) {
958 /* Though we wish to avoid it, we are in fact safe in
959 * skipping local alloc cleanup as fsck.ocfs2 is more
960 * than capable of reclaiming unused space. */
961 if (la_dinode)
962 kfree(la_dinode);
963
964 if (tl_dinode)
965 kfree(tl_dinode);
966
967 mlog_errno(-ENOMEM);
968 return;
969 }
970
971 INIT_LIST_HEAD(&item->lri_list);
972 item->lri_la_dinode = la_dinode;
973 item->lri_slot = slot_num;
974 item->lri_tl_dinode = tl_dinode;
975
976 spin_lock(&journal->j_lock);
977 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
978 queue_work(ocfs2_wq, &journal->j_recovery_work);
979 spin_unlock(&journal->j_lock);
980}
981
982/* Called by the mount code to queue recovery the last part of
983 * recovery for it's own slot. */
984void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
985{
986 struct ocfs2_journal *journal = osb->journal;
987
988 if (osb->dirty) {
989 /* No need to queue up our truncate_log as regular
990 * cleanup will catch that. */
991 ocfs2_queue_recovery_completion(journal,
992 osb->slot_num,
993 osb->local_alloc_copy,
994 NULL);
995 ocfs2_schedule_truncate_log_flush(osb, 0);
996
997 osb->local_alloc_copy = NULL;
998 osb->dirty = 0;
999 }
1000}
1001
1002static int __ocfs2_recovery_thread(void *arg)
1003{
1004 int status, node_num;
1005 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001006 struct ocfs2_recovery_map *rm = osb->recovery_map;
Mark Fashehccd979b2005-12-15 14:31:24 -08001007
1008 mlog_entry_void();
1009
1010 status = ocfs2_wait_on_mount(osb);
1011 if (status < 0) {
1012 goto bail;
1013 }
1014
1015restart:
1016 status = ocfs2_super_lock(osb, 1);
1017 if (status < 0) {
1018 mlog_errno(status);
1019 goto bail;
1020 }
1021
Joel Becker553abd02008-02-01 12:03:57 -08001022 spin_lock(&osb->osb_lock);
1023 while (rm->rm_used) {
1024 /* It's always safe to remove entry zero, as we won't
1025 * clear it until ocfs2_recover_node() has succeeded. */
1026 node_num = rm->rm_entries[0];
1027 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001028
1029 status = ocfs2_recover_node(osb, node_num);
Joel Becker553abd02008-02-01 12:03:57 -08001030 if (!status) {
1031 ocfs2_recovery_map_clear(osb, node_num);
1032 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001033 mlog(ML_ERROR,
1034 "Error %d recovering node %d on device (%u,%u)!\n",
1035 status, node_num,
1036 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1037 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001038 }
1039
Joel Becker553abd02008-02-01 12:03:57 -08001040 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 }
Joel Becker553abd02008-02-01 12:03:57 -08001042 spin_unlock(&osb->osb_lock);
1043 mlog(0, "All nodes recovered\n");
1044
Sunil Mushran539d8262008-07-14 17:31:10 -07001045 /* Refresh all journal recovery generations from disk */
1046 status = ocfs2_check_journals_nolocks(osb);
1047 status = (status == -EROFS) ? 0 : status;
1048 if (status < 0)
1049 mlog_errno(status);
1050
Mark Fashehccd979b2005-12-15 14:31:24 -08001051 ocfs2_super_unlock(osb, 1);
1052
1053 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001054 * node(s) may have disallowd a previos inode delete. Re-processing
1055 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1057 NULL);
1058
1059bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001060 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001061 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001062 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 goto restart;
1064 }
1065
1066 osb->recovery_thread_task = NULL;
1067 mb(); /* sync with ocfs2_recovery_thread_running */
1068 wake_up(&osb->recovery_event);
1069
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001070 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001071
1072 mlog_exit(status);
1073 /* no one is callint kthread_stop() for us so the kthread() api
1074 * requires that we call do_exit(). And it isn't exported, but
1075 * complete_and_exit() seems to be a minimal wrapper around it. */
1076 complete_and_exit(NULL, status);
1077 return status;
1078}
1079
1080void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1081{
1082 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1083 node_num, osb->node_num);
1084
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001085 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 if (osb->disable_recovery)
1087 goto out;
1088
1089 /* People waiting on recovery will wait on
1090 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001091 if (ocfs2_recovery_map_set(osb, node_num))
1092 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001093
1094 mlog(0, "starting recovery thread...\n");
1095
1096 if (osb->recovery_thread_task)
1097 goto out;
1098
1099 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001100 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 if (IS_ERR(osb->recovery_thread_task)) {
1102 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1103 osb->recovery_thread_task = NULL;
1104 }
1105
1106out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001107 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 wake_up(&osb->recovery_event);
1109
1110 mlog_exit_void();
1111}
1112
Sunil Mushran539d8262008-07-14 17:31:10 -07001113static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1114 int slot_num,
1115 struct buffer_head **bh,
1116 struct inode **ret_inode)
1117{
1118 int status = -EACCES;
1119 struct inode *inode = NULL;
1120
1121 BUG_ON(slot_num >= osb->max_slots);
1122
1123 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1124 slot_num);
1125 if (!inode || is_bad_inode(inode)) {
1126 mlog_errno(status);
1127 goto bail;
1128 }
1129 SET_INODE_JOURNAL(inode);
1130
Joel Beckerb657c952008-11-13 14:49:11 -08001131 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001132 if (status < 0) {
1133 mlog_errno(status);
1134 goto bail;
1135 }
1136
1137 status = 0;
1138
1139bail:
1140 if (inode) {
1141 if (status || !ret_inode)
1142 iput(inode);
1143 else
1144 *ret_inode = inode;
1145 }
1146 return status;
1147}
1148
Mark Fashehccd979b2005-12-15 14:31:24 -08001149/* Does the actual journal replay and marks the journal inode as
1150 * clean. Will only replay if the journal inode is marked dirty. */
1151static int ocfs2_replay_journal(struct ocfs2_super *osb,
1152 int node_num,
1153 int slot_num)
1154{
1155 int status;
1156 int got_lock = 0;
1157 unsigned int flags;
1158 struct inode *inode = NULL;
1159 struct ocfs2_dinode *fe;
1160 journal_t *journal = NULL;
1161 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001162 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001163
Sunil Mushran539d8262008-07-14 17:31:10 -07001164 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1165 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 mlog_errno(status);
1167 goto done;
1168 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001169
1170 fe = (struct ocfs2_dinode *)bh->b_data;
1171 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1172 brelse(bh);
1173 bh = NULL;
1174
1175 /*
1176 * As the fs recovery is asynchronous, there is a small chance that
1177 * another node mounted (and recovered) the slot before the recovery
1178 * thread could get the lock. To handle that, we dirty read the journal
1179 * inode for that slot to get the recovery generation. If it is
1180 * different than what we expected, the slot has been recovered.
1181 * If not, it needs recovery.
1182 */
1183 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1184 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1185 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1186 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1187 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001188 goto done;
1189 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001190
1191 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
Mark Fashehe63aecb62007-10-18 15:30:42 -07001193 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001194 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001195 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001196 if (status != -ERESTARTSYS)
1197 mlog(ML_ERROR, "Could not lock journal!\n");
1198 goto done;
1199 }
1200 got_lock = 1;
1201
1202 fe = (struct ocfs2_dinode *) bh->b_data;
1203
1204 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001205 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001206
1207 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1208 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001209 /* Refresh recovery generation for the slot */
1210 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001211 goto done;
1212 }
1213
1214 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1215 node_num, slot_num,
1216 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1217
1218 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1219
1220 status = ocfs2_force_read_journal(inode);
1221 if (status < 0) {
1222 mlog_errno(status);
1223 goto done;
1224 }
1225
1226 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001227 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001228 if (journal == NULL) {
1229 mlog(ML_ERROR, "Linux journal layer error\n");
1230 status = -EIO;
1231 goto done;
1232 }
1233
Joel Becker2b4e30f2008-09-03 20:03:41 -07001234 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001235 if (status < 0) {
1236 mlog_errno(status);
1237 if (!igrab(inode))
1238 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001239 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 goto done;
1241 }
1242
1243 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1244
1245 /* wipe the journal */
1246 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001247 jbd2_journal_lock_updates(journal);
1248 status = jbd2_journal_flush(journal);
1249 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001250 if (status < 0)
1251 mlog_errno(status);
1252
1253 /* This will mark the node clean */
1254 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1255 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1256 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1257
Sunil Mushran539d8262008-07-14 17:31:10 -07001258 /* Increment recovery generation to indicate successful recovery */
1259 ocfs2_bump_recovery_generation(fe);
1260 osb->slot_recovery_generations[slot_num] =
1261 ocfs2_get_recovery_generation(fe);
1262
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 status = ocfs2_write_block(osb, bh, inode);
1264 if (status < 0)
1265 mlog_errno(status);
1266
1267 if (!igrab(inode))
1268 BUG();
1269
Joel Becker2b4e30f2008-09-03 20:03:41 -07001270 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001271
1272done:
1273 /* drop the lock on this nodes journal */
1274 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001275 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001276
1277 if (inode)
1278 iput(inode);
1279
Mark Fasheha81cb882008-10-07 14:25:16 -07001280 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001281
1282 mlog_exit(status);
1283 return status;
1284}
1285
1286/*
1287 * Do the most important parts of node recovery:
1288 * - Replay it's journal
1289 * - Stamp a clean local allocator file
1290 * - Stamp a clean truncate log
1291 * - Mark the node clean
1292 *
1293 * If this function completes without error, a node in OCFS2 can be
1294 * said to have been safely recovered. As a result, failure during the
1295 * second part of a nodes recovery process (local alloc recovery) is
1296 * far less concerning.
1297 */
1298static int ocfs2_recover_node(struct ocfs2_super *osb,
1299 int node_num)
1300{
1301 int status = 0;
1302 int slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001303 struct ocfs2_dinode *la_copy = NULL;
1304 struct ocfs2_dinode *tl_copy = NULL;
1305
1306 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1307 node_num, osb->node_num);
1308
1309 mlog(0, "checking node %d\n", node_num);
1310
1311 /* Should not ever be called to recover ourselves -- in that
1312 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001313 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001314
Joel Beckerd85b20e2008-02-01 12:01:05 -08001315 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1316 if (slot_num == -ENOENT) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001317 status = 0;
1318 mlog(0, "no slot for this node, so no recovery required.\n");
1319 goto done;
1320 }
1321
1322 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1323
1324 status = ocfs2_replay_journal(osb, node_num, slot_num);
1325 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001326 if (status == -EBUSY) {
1327 mlog(0, "Skipping recovery for slot %u (node %u) "
1328 "as another node has recovered it\n", slot_num,
1329 node_num);
1330 status = 0;
1331 goto done;
1332 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001333 mlog_errno(status);
1334 goto done;
1335 }
1336
1337 /* Stamp a clean local alloc file AFTER recovering the journal... */
1338 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1339 if (status < 0) {
1340 mlog_errno(status);
1341 goto done;
1342 }
1343
1344 /* An error from begin_truncate_log_recovery is not
1345 * serious enough to warrant halting the rest of
1346 * recovery. */
1347 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1348 if (status < 0)
1349 mlog_errno(status);
1350
1351 /* Likewise, this would be a strange but ultimately not so
1352 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001353 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001354 if (status < 0)
1355 mlog_errno(status);
1356
1357 /* This will kfree the memory pointed to by la_copy and tl_copy */
1358 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1359 tl_copy);
1360
1361 status = 0;
1362done:
1363
1364 mlog_exit(status);
1365 return status;
1366}
1367
1368/* Test node liveness by trylocking his journal. If we get the lock,
1369 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1370 * still alive (we couldn't get the lock) and < 0 on error. */
1371static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1372 int slot_num)
1373{
1374 int status, flags;
1375 struct inode *inode = NULL;
1376
1377 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1378 slot_num);
1379 if (inode == NULL) {
1380 mlog(ML_ERROR, "access error\n");
1381 status = -EACCES;
1382 goto bail;
1383 }
1384 if (is_bad_inode(inode)) {
1385 mlog(ML_ERROR, "access error (bad inode)\n");
1386 iput(inode);
1387 inode = NULL;
1388 status = -EACCES;
1389 goto bail;
1390 }
1391 SET_INODE_JOURNAL(inode);
1392
1393 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001394 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001395 if (status < 0) {
1396 if (status != -EAGAIN)
1397 mlog_errno(status);
1398 goto bail;
1399 }
1400
Mark Fashehe63aecb62007-10-18 15:30:42 -07001401 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001402bail:
1403 if (inode)
1404 iput(inode);
1405
1406 return status;
1407}
1408
1409/* Call this underneath ocfs2_super_lock. It also assumes that the
1410 * slot info struct has been updated from disk. */
1411int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1412{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001413 unsigned int node_num;
1414 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001415 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001416 struct buffer_head *bh = NULL;
1417 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001418
1419 /* This is called with the super block cluster lock, so we
1420 * know that the slot map can't change underneath us. */
1421
Joel Beckerd85b20e2008-02-01 12:01:05 -08001422 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001423 /* Read journal inode to get the recovery generation */
1424 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1425 if (status) {
1426 mlog_errno(status);
1427 goto bail;
1428 }
1429 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001430 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001431 brelse(bh);
1432 bh = NULL;
1433
Mark Fasheha1af7d12008-08-19 17:20:28 -07001434 spin_lock(&osb->osb_lock);
1435 osb->slot_recovery_generations[i] = gen;
1436
Sunil Mushran539d8262008-07-14 17:31:10 -07001437 mlog(0, "Slot %u recovery generation is %u\n", i,
1438 osb->slot_recovery_generations[i]);
1439
Mark Fasheha1af7d12008-08-19 17:20:28 -07001440 if (i == osb->slot_num) {
1441 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001443 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001444
1445 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001446 if (status == -ENOENT) {
1447 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001448 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001449 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001450
Mark Fasheha1af7d12008-08-19 17:20:28 -07001451 if (__ocfs2_recovery_map_test(osb, node_num)) {
1452 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001453 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001454 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001455 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001456
1457 /* Ok, we have a slot occupied by another node which
1458 * is not in the recovery map. We trylock his journal
1459 * file here to test if he's alive. */
1460 status = ocfs2_trylock_journal(osb, i);
1461 if (!status) {
1462 /* Since we're called from mount, we know that
1463 * the recovery thread can't race us on
1464 * setting / checking the recovery bits. */
1465 ocfs2_recovery_thread(osb, node_num);
1466 } else if ((status < 0) && (status != -EAGAIN)) {
1467 mlog_errno(status);
1468 goto bail;
1469 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001470 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001471
1472 status = 0;
1473bail:
1474 mlog_exit(status);
1475 return status;
1476}
1477
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001478struct ocfs2_orphan_filldir_priv {
1479 struct inode *head;
1480 struct ocfs2_super *osb;
1481};
1482
1483static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1484 loff_t pos, u64 ino, unsigned type)
1485{
1486 struct ocfs2_orphan_filldir_priv *p = priv;
1487 struct inode *iter;
1488
1489 if (name_len == 1 && !strncmp(".", name, 1))
1490 return 0;
1491 if (name_len == 2 && !strncmp("..", name, 2))
1492 return 0;
1493
1494 /* Skip bad inodes so that recovery can continue */
1495 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001496 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001497 if (IS_ERR(iter))
1498 return 0;
1499
1500 mlog(0, "queue orphan %llu\n",
1501 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1502 /* No locking is required for the next_orphan queue as there
1503 * is only ever a single process doing orphan recovery. */
1504 OCFS2_I(iter)->ip_next_orphan = p->head;
1505 p->head = iter;
1506
1507 return 0;
1508}
1509
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001510static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1511 int slot,
1512 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001513{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001514 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001515 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001516 struct ocfs2_orphan_filldir_priv priv;
1517 loff_t pos = 0;
1518
1519 priv.osb = osb;
1520 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001521
1522 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1523 ORPHAN_DIR_SYSTEM_INODE,
1524 slot);
1525 if (!orphan_dir_inode) {
1526 status = -ENOENT;
1527 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001528 return status;
1529 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001530
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001531 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001532 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 mlog_errno(status);
1535 goto out;
1536 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001537
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001538 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1539 ocfs2_orphan_filldir);
1540 if (status) {
1541 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001542 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001543 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001544
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001545 *head = priv.head;
1546
Mark Fasheha86370f2007-12-03 14:06:23 -08001547out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001548 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001549out:
1550 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001551 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001552 return status;
1553}
1554
1555static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1556 int slot)
1557{
1558 int ret;
1559
1560 spin_lock(&osb->osb_lock);
1561 ret = !osb->osb_orphan_wipes[slot];
1562 spin_unlock(&osb->osb_lock);
1563 return ret;
1564}
1565
1566static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1567 int slot)
1568{
1569 spin_lock(&osb->osb_lock);
1570 /* Mark ourselves such that new processes in delete_inode()
1571 * know to quit early. */
1572 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1573 while (osb->osb_orphan_wipes[slot]) {
1574 /* If any processes are already in the middle of an
1575 * orphan wipe on this dir, then we need to wait for
1576 * them. */
1577 spin_unlock(&osb->osb_lock);
1578 wait_event_interruptible(osb->osb_wipe_event,
1579 ocfs2_orphan_recovery_can_continue(osb, slot));
1580 spin_lock(&osb->osb_lock);
1581 }
1582 spin_unlock(&osb->osb_lock);
1583}
1584
1585static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1586 int slot)
1587{
1588 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1589}
1590
1591/*
1592 * Orphan recovery. Each mounted node has it's own orphan dir which we
1593 * must run during recovery. Our strategy here is to build a list of
1594 * the inodes in the orphan dir and iget/iput them. The VFS does
1595 * (most) of the rest of the work.
1596 *
1597 * Orphan recovery can happen at any time, not just mount so we have a
1598 * couple of extra considerations.
1599 *
1600 * - We grab as many inodes as we can under the orphan dir lock -
1601 * doing iget() outside the orphan dir risks getting a reference on
1602 * an invalid inode.
1603 * - We must be sure not to deadlock with other processes on the
1604 * system wanting to run delete_inode(). This can happen when they go
1605 * to lock the orphan dir and the orphan recovery process attempts to
1606 * iget() inside the orphan dir lock. This can be avoided by
1607 * advertising our state to ocfs2_delete_inode().
1608 */
1609static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1610 int slot)
1611{
1612 int ret = 0;
1613 struct inode *inode = NULL;
1614 struct inode *iter;
1615 struct ocfs2_inode_info *oi;
1616
1617 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1618
1619 ocfs2_mark_recovering_orphan_dir(osb, slot);
1620 ret = ocfs2_queue_orphans(osb, slot, &inode);
1621 ocfs2_clear_recovering_orphan_dir(osb, slot);
1622
1623 /* Error here should be noted, but we want to continue with as
1624 * many queued inodes as we've got. */
1625 if (ret)
1626 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001627
1628 while (inode) {
1629 oi = OCFS2_I(inode);
Mark Fashehb0697052006-03-03 10:24:33 -08001630 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001631
1632 iter = oi->ip_next_orphan;
1633
1634 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001635 /* The remote delete code may have set these on the
1636 * assumption that the other node would wipe them
1637 * successfully. If they are still in the node's
1638 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001639 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1640
1641 /* Set the proper information to get us going into
1642 * ocfs2_delete_inode. */
1643 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001644 spin_unlock(&oi->ip_lock);
1645
1646 iput(inode);
1647
1648 inode = iter;
1649 }
1650
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001651 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001652}
1653
1654static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
1655{
1656 /* This check is good because ocfs2 will wait on our recovery
1657 * thread before changing it to something other than MOUNTED
1658 * or DISABLED. */
1659 wait_event(osb->osb_mount_event,
1660 atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
1661 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1662
1663 /* If there's an error on mount, then we may never get to the
1664 * MOUNTED flag, but this is set right before
1665 * dismount_volume() so we can trust it. */
1666 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1667 mlog(0, "mount error, exiting!\n");
1668 return -EBUSY;
1669 }
1670
1671 return 0;
1672}
1673
1674static int ocfs2_commit_thread(void *arg)
1675{
1676 int status;
1677 struct ocfs2_super *osb = arg;
1678 struct ocfs2_journal *journal = osb->journal;
1679
1680 /* we can trust j_num_trans here because _should_stop() is only set in
1681 * shutdown and nobody other than ourselves should be able to start
1682 * transactions. committing on shutdown might take a few iterations
1683 * as final transactions put deleted inodes on the list */
1684 while (!(kthread_should_stop() &&
1685 atomic_read(&journal->j_num_trans) == 0)) {
1686
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001687 wait_event_interruptible(osb->checkpoint_event,
1688 atomic_read(&journal->j_num_trans)
1689 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001690
1691 status = ocfs2_commit_cache(osb);
1692 if (status < 0)
1693 mlog_errno(status);
1694
1695 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1696 mlog(ML_KTHREAD,
1697 "commit_thread: %u transactions pending on "
1698 "shutdown\n",
1699 atomic_read(&journal->j_num_trans));
1700 }
1701 }
1702
1703 return 0;
1704}
1705
Sunil Mushran539d8262008-07-14 17:31:10 -07001706/* Reads all the journal inodes without taking any cluster locks. Used
1707 * for hard readonly access to determine whether any journal requires
1708 * recovery. Also used to refresh the recovery generation numbers after
1709 * a journal has been recovered by another node.
1710 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001711int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1712{
1713 int ret = 0;
1714 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001715 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001716 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001717 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001718
1719 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001720 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1721 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 mlog_errno(ret);
1723 goto out;
1724 }
1725
1726 di = (struct ocfs2_dinode *) di_bh->b_data;
1727
Sunil Mushran539d8262008-07-14 17:31:10 -07001728 osb->slot_recovery_generations[slot] =
1729 ocfs2_get_recovery_generation(di);
1730
Mark Fashehccd979b2005-12-15 14:31:24 -08001731 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1732 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001733 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001734
1735 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001736 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001737 }
1738
1739out:
Sunil Mushran539d8262008-07-14 17:31:10 -07001740 if (journal_dirty)
1741 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08001742 return ret;
1743}