blob: 11a1178d5ee8ee3f7d4fc4d647e90528b97e292e [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
Jan Kara90e86a62008-08-27 22:30:28 +0200259 /* Nested transaction? Just return the handle... */
260 if (journal_current_handle())
261 return jbd2_journal_start(journal, max_buffs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800262
Mark Fashehccd979b2005-12-15 14:31:24 -0800263 down_read(&osb->journal->j_trans_barrier);
264
Joel Becker2b4e30f2008-09-03 20:03:41 -0700265 handle = jbd2_journal_start(journal, max_buffs);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700266 if (IS_ERR(handle)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800267 up_read(&osb->journal->j_trans_barrier);
268
Mark Fasheh1fabe142006-10-09 18:11:45 -0700269 mlog_errno(PTR_ERR(handle));
Mark Fashehccd979b2005-12-15 14:31:24 -0800270
271 if (is_journal_aborted(journal)) {
272 ocfs2_abort(osb->sb, "Detected aborted journal");
Mark Fasheh1fabe142006-10-09 18:11:45 -0700273 handle = ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800274 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800275 } else {
276 if (!ocfs2_mount_local(osb))
277 atomic_inc(&(osb->journal->j_num_trans));
278 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800279
Mark Fashehccd979b2005-12-15 14:31:24 -0800280 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800281}
282
Mark Fasheh1fabe142006-10-09 18:11:45 -0700283int ocfs2_commit_trans(struct ocfs2_super *osb,
284 handle_t *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800285{
Jan Kara90e86a62008-08-27 22:30:28 +0200286 int ret, nested;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700287 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800288
289 BUG_ON(!handle);
290
Jan Kara90e86a62008-08-27 22:30:28 +0200291 nested = handle->h_ref > 1;
Joel Becker2b4e30f2008-09-03 20:03:41 -0700292 ret = jbd2_journal_stop(handle);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700293 if (ret < 0)
294 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800295
Jan Kara90e86a62008-08-27 22:30:28 +0200296 if (!nested)
297 up_read(&journal->j_trans_barrier);
Mark Fashehccd979b2005-12-15 14:31:24 -0800298
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#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800438
439void ocfs2_set_journal_params(struct ocfs2_super *osb)
440{
441 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800442 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
443
444 if (osb->osb_commit_interval)
445 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800446
447 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800448 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800449 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700450 journal->j_flags |= JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800451 else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700452 journal->j_flags &= ~JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800453 spin_unlock(&journal->j_state_lock);
454}
455
456int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
457{
458 int status = -1;
459 struct inode *inode = NULL; /* the journal inode */
460 journal_t *j_journal = NULL;
461 struct ocfs2_dinode *di = NULL;
462 struct buffer_head *bh = NULL;
463 struct ocfs2_super *osb;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700464 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800465
466 mlog_entry_void();
467
468 BUG_ON(!journal);
469
470 osb = journal->j_osb;
471
472 /* already have the inode for our journal */
473 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
474 osb->slot_num);
475 if (inode == NULL) {
476 status = -EACCES;
477 mlog_errno(status);
478 goto done;
479 }
480 if (is_bad_inode(inode)) {
481 mlog(ML_ERROR, "access error (bad inode)\n");
482 iput(inode);
483 inode = NULL;
484 status = -EACCES;
485 goto done;
486 }
487
488 SET_INODE_JOURNAL(inode);
489 OCFS2_I(inode)->ip_open_count++;
490
Mark Fasheh6eff5792006-01-18 10:31:47 -0800491 /* Skip recovery waits here - journal inode metadata never
492 * changes in a live cluster so it can be considered an
493 * exception to the rule. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700494 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800495 if (status < 0) {
496 if (status != -ERESTARTSYS)
497 mlog(ML_ERROR, "Could not get lock on journal!\n");
498 goto done;
499 }
500
Mark Fashehe63aecb62007-10-18 15:30:42 -0700501 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800502 di = (struct ocfs2_dinode *)bh->b_data;
503
504 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
505 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
506 inode->i_size);
507 status = -EINVAL;
508 goto done;
509 }
510
511 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800512 mlog(0, "inode->i_blocks = %llu\n",
513 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800514 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
515
516 /* call the kernels journal init function now */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700517 j_journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800518 if (j_journal == NULL) {
519 mlog(ML_ERROR, "Linux journal layer error\n");
520 status = -EINVAL;
521 goto done;
522 }
523
Joel Becker2b4e30f2008-09-03 20:03:41 -0700524 mlog(0, "Returned from jbd2_journal_init_inode\n");
Mark Fashehccd979b2005-12-15 14:31:24 -0800525 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
526
527 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
528 OCFS2_JOURNAL_DIRTY_FL);
529
530 journal->j_journal = j_journal;
531 journal->j_inode = inode;
532 journal->j_bh = bh;
533
534 ocfs2_set_journal_params(osb);
535
536 journal->j_state = OCFS2_JOURNAL_LOADED;
537
538 status = 0;
539done:
540 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700541 if (inode_lock)
542 ocfs2_inode_unlock(inode, 1);
Mark Fasheha81cb882008-10-07 14:25:16 -0700543 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800544 if (inode) {
545 OCFS2_I(inode)->ip_open_count--;
546 iput(inode);
547 }
548 }
549
550 mlog_exit(status);
551 return status;
552}
553
Sunil Mushran539d8262008-07-14 17:31:10 -0700554static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
555{
556 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
557}
558
559static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
560{
561 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
562}
563
Mark Fashehccd979b2005-12-15 14:31:24 -0800564static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -0700565 int dirty, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800566{
567 int status;
568 unsigned int flags;
569 struct ocfs2_journal *journal = osb->journal;
570 struct buffer_head *bh = journal->j_bh;
571 struct ocfs2_dinode *fe;
572
573 mlog_entry_void();
574
575 fe = (struct ocfs2_dinode *)bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800576
577 /* The journal bh on the osb always comes from ocfs2_journal_init()
578 * and was validated there inside ocfs2_inode_lock_full(). It's a
579 * code bug if we mess it up. */
580 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800581
582 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
583 if (dirty)
584 flags |= OCFS2_JOURNAL_DIRTY_FL;
585 else
586 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
587 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
588
Sunil Mushran539d8262008-07-14 17:31:10 -0700589 if (replayed)
590 ocfs2_bump_recovery_generation(fe);
591
Mark Fashehccd979b2005-12-15 14:31:24 -0800592 status = ocfs2_write_block(osb, bh, journal->j_inode);
593 if (status < 0)
594 mlog_errno(status);
595
Mark Fashehccd979b2005-12-15 14:31:24 -0800596 mlog_exit(status);
597 return status;
598}
599
600/*
601 * If the journal has been kmalloc'd it needs to be freed after this
602 * call.
603 */
604void ocfs2_journal_shutdown(struct ocfs2_super *osb)
605{
606 struct ocfs2_journal *journal = NULL;
607 int status = 0;
608 struct inode *inode = NULL;
609 int num_running_trans = 0;
610
611 mlog_entry_void();
612
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100613 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800614
615 journal = osb->journal;
616 if (!journal)
617 goto done;
618
619 inode = journal->j_inode;
620
621 if (journal->j_state != OCFS2_JOURNAL_LOADED)
622 goto done;
623
Joel Becker2b4e30f2008-09-03 20:03:41 -0700624 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800625 if (!igrab(inode))
626 BUG();
627
628 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
629 if (num_running_trans > 0)
630 mlog(0, "Shutting down journal: must wait on %d "
631 "running transactions!\n",
632 num_running_trans);
633
634 /* Do a commit_cache here. It will flush our journal, *and*
635 * release any locks that are still held.
636 * set the SHUTDOWN flag and release the trans lock.
637 * the commit thread will take the trans lock for us below. */
638 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
639
640 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
641 * drop the trans_lock (which we want to hold until we
642 * completely destroy the journal. */
643 if (osb->commit_task) {
644 /* Wait for the commit thread */
645 mlog(0, "Waiting for ocfs2commit to exit....\n");
646 kthread_stop(osb->commit_task);
647 osb->commit_task = NULL;
648 }
649
650 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
651
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800652 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700653 jbd2_journal_lock_updates(journal->j_journal);
654 status = jbd2_journal_flush(journal->j_journal);
655 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800656 if (status < 0)
657 mlog_errno(status);
658 }
659
660 if (status == 0) {
661 /*
662 * Do not toggle if flush was unsuccessful otherwise
663 * will leave dirty metadata in a "clean" journal
664 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700665 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800666 if (status < 0)
667 mlog_errno(status);
668 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800669
670 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700671 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700672 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800673
674 OCFS2_I(inode)->ip_open_count--;
675
676 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700677 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800678
679 brelse(journal->j_bh);
680 journal->j_bh = NULL;
681
682 journal->j_state = OCFS2_JOURNAL_FREE;
683
684// up_write(&journal->j_trans_barrier);
685done:
686 if (inode)
687 iput(inode);
688 mlog_exit_void();
689}
690
691static void ocfs2_clear_journal_error(struct super_block *sb,
692 journal_t *journal,
693 int slot)
694{
695 int olderr;
696
Joel Becker2b4e30f2008-09-03 20:03:41 -0700697 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800698 if (olderr) {
699 mlog(ML_ERROR, "File system error %d recorded in "
700 "journal %u.\n", olderr, slot);
701 mlog(ML_ERROR, "File system on device %s needs checking.\n",
702 sb->s_id);
703
Joel Becker2b4e30f2008-09-03 20:03:41 -0700704 jbd2_journal_ack_err(journal);
705 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 }
707}
708
Sunil Mushran539d8262008-07-14 17:31:10 -0700709int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800710{
711 int status = 0;
712 struct ocfs2_super *osb;
713
714 mlog_entry_void();
715
Julia Lawallb1f35502008-03-04 15:21:05 -0800716 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800717
718 osb = journal->j_osb;
719
Joel Becker2b4e30f2008-09-03 20:03:41 -0700720 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800721 if (status < 0) {
722 mlog(ML_ERROR, "Failed to load journal!\n");
723 goto done;
724 }
725
726 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
727
Sunil Mushran539d8262008-07-14 17:31:10 -0700728 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800729 if (status < 0) {
730 mlog_errno(status);
731 goto done;
732 }
733
734 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800735 if (!local) {
736 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
737 "ocfs2cmt");
738 if (IS_ERR(osb->commit_task)) {
739 status = PTR_ERR(osb->commit_task);
740 osb->commit_task = NULL;
741 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
742 "error=%d", status);
743 goto done;
744 }
745 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800746 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800747
748done:
749 mlog_exit(status);
750 return status;
751}
752
753
754/* 'full' flag tells us whether we clear out all blocks or if we just
755 * mark the journal clean */
756int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
757{
758 int status;
759
760 mlog_entry_void();
761
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100762 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800763
Joel Becker2b4e30f2008-09-03 20:03:41 -0700764 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800765 if (status < 0) {
766 mlog_errno(status);
767 goto bail;
768 }
769
Sunil Mushran539d8262008-07-14 17:31:10 -0700770 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800771 if (status < 0)
772 mlog_errno(status);
773
774bail:
775 mlog_exit(status);
776 return status;
777}
778
Joel Becker553abd02008-02-01 12:03:57 -0800779static int ocfs2_recovery_completed(struct ocfs2_super *osb)
780{
781 int empty;
782 struct ocfs2_recovery_map *rm = osb->recovery_map;
783
784 spin_lock(&osb->osb_lock);
785 empty = (rm->rm_used == 0);
786 spin_unlock(&osb->osb_lock);
787
788 return empty;
789}
790
791void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
792{
793 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
794}
795
Mark Fashehccd979b2005-12-15 14:31:24 -0800796/*
797 * JBD Might read a cached version of another nodes journal file. We
798 * don't want this as this file changes often and we get no
799 * notification on those changes. The only way to be sure that we've
800 * got the most up to date version of those blocks then is to force
801 * read them off disk. Just searching through the buffer cache won't
802 * work as there may be pages backing this file which are still marked
803 * up to date. We know things can't change on this file underneath us
804 * as we have the lock by now :)
805 */
806static int ocfs2_force_read_journal(struct inode *inode)
807{
808 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800809 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700810 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800811#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800812 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
813
814 mlog_entry_void();
815
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
817
Mark Fasheh8110b072007-03-22 16:53:23 -0700818 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800819 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700820 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800821 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800822 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800823 if (status < 0) {
824 mlog_errno(status);
825 goto bail;
826 }
827
828 if (p_blocks > CONCURRENT_JOURNAL_FILL)
829 p_blocks = CONCURRENT_JOURNAL_FILL;
830
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700831 /* We are reading journal data which should not
832 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -0700833 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
834 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 if (status < 0) {
836 mlog_errno(status);
837 goto bail;
838 }
839
840 for(i = 0; i < p_blocks; i++) {
841 brelse(bhs[i]);
842 bhs[i] = NULL;
843 }
844
845 v_blkno += p_blocks;
846 }
847
848bail:
849 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -0700850 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 mlog_exit(status);
852 return status;
853}
854
855struct ocfs2_la_recovery_item {
856 struct list_head lri_list;
857 int lri_slot;
858 struct ocfs2_dinode *lri_la_dinode;
859 struct ocfs2_dinode *lri_tl_dinode;
860};
861
862/* Does the second half of the recovery process. By this point, the
863 * node is marked clean and can actually be considered recovered,
864 * hence it's no longer in the recovery map, but there's still some
865 * cleanup we can do which shouldn't happen within the recovery thread
866 * as locking in that context becomes very difficult if we are to take
867 * recovering nodes into account.
868 *
869 * NOTE: This function can and will sleep on recovery of other nodes
870 * during cluster locking, just like any other ocfs2 process.
871 */
David Howellsc4028952006-11-22 14:57:56 +0000872void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -0800873{
874 int ret;
David Howellsc4028952006-11-22 14:57:56 +0000875 struct ocfs2_journal *journal =
876 container_of(work, struct ocfs2_journal, j_recovery_work);
877 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -0800878 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +0200879 struct ocfs2_la_recovery_item *item, *n;
Mark Fashehccd979b2005-12-15 14:31:24 -0800880 LIST_HEAD(tmp_la_list);
881
882 mlog_entry_void();
883
884 mlog(0, "completing recovery from keventd\n");
885
886 spin_lock(&journal->j_lock);
887 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
888 spin_unlock(&journal->j_lock);
889
Christoph Hellwig800deef2007-05-17 16:03:13 +0200890 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 list_del_init(&item->lri_list);
892
893 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
894
895 la_dinode = item->lri_la_dinode;
896 if (la_dinode) {
Mark Fashehb0697052006-03-03 10:24:33 -0800897 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700898 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800899
900 ret = ocfs2_complete_local_alloc_recovery(osb,
901 la_dinode);
902 if (ret < 0)
903 mlog_errno(ret);
904
905 kfree(la_dinode);
906 }
907
908 tl_dinode = item->lri_tl_dinode;
909 if (tl_dinode) {
Mark Fashehb0697052006-03-03 10:24:33 -0800910 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700911 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800912
913 ret = ocfs2_complete_truncate_log_recovery(osb,
914 tl_dinode);
915 if (ret < 0)
916 mlog_errno(ret);
917
918 kfree(tl_dinode);
919 }
920
921 ret = ocfs2_recover_orphans(osb, item->lri_slot);
922 if (ret < 0)
923 mlog_errno(ret);
924
925 kfree(item);
926 }
927
928 mlog(0, "Recovery completion\n");
929 mlog_exit_void();
930}
931
932/* NOTE: This function always eats your references to la_dinode and
933 * tl_dinode, either manually on error, or by passing them to
934 * ocfs2_complete_recovery */
935static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
936 int slot_num,
937 struct ocfs2_dinode *la_dinode,
938 struct ocfs2_dinode *tl_dinode)
939{
940 struct ocfs2_la_recovery_item *item;
941
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700942 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800943 if (!item) {
944 /* Though we wish to avoid it, we are in fact safe in
945 * skipping local alloc cleanup as fsck.ocfs2 is more
946 * than capable of reclaiming unused space. */
947 if (la_dinode)
948 kfree(la_dinode);
949
950 if (tl_dinode)
951 kfree(tl_dinode);
952
953 mlog_errno(-ENOMEM);
954 return;
955 }
956
957 INIT_LIST_HEAD(&item->lri_list);
958 item->lri_la_dinode = la_dinode;
959 item->lri_slot = slot_num;
960 item->lri_tl_dinode = tl_dinode;
961
962 spin_lock(&journal->j_lock);
963 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
964 queue_work(ocfs2_wq, &journal->j_recovery_work);
965 spin_unlock(&journal->j_lock);
966}
967
968/* Called by the mount code to queue recovery the last part of
969 * recovery for it's own slot. */
970void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
971{
972 struct ocfs2_journal *journal = osb->journal;
973
974 if (osb->dirty) {
975 /* No need to queue up our truncate_log as regular
976 * cleanup will catch that. */
977 ocfs2_queue_recovery_completion(journal,
978 osb->slot_num,
979 osb->local_alloc_copy,
980 NULL);
981 ocfs2_schedule_truncate_log_flush(osb, 0);
982
983 osb->local_alloc_copy = NULL;
984 osb->dirty = 0;
985 }
986}
987
988static int __ocfs2_recovery_thread(void *arg)
989{
990 int status, node_num;
991 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -0800992 struct ocfs2_recovery_map *rm = osb->recovery_map;
Mark Fashehccd979b2005-12-15 14:31:24 -0800993
994 mlog_entry_void();
995
996 status = ocfs2_wait_on_mount(osb);
997 if (status < 0) {
998 goto bail;
999 }
1000
1001restart:
1002 status = ocfs2_super_lock(osb, 1);
1003 if (status < 0) {
1004 mlog_errno(status);
1005 goto bail;
1006 }
1007
Joel Becker553abd02008-02-01 12:03:57 -08001008 spin_lock(&osb->osb_lock);
1009 while (rm->rm_used) {
1010 /* It's always safe to remove entry zero, as we won't
1011 * clear it until ocfs2_recover_node() has succeeded. */
1012 node_num = rm->rm_entries[0];
1013 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001014
1015 status = ocfs2_recover_node(osb, node_num);
Joel Becker553abd02008-02-01 12:03:57 -08001016 if (!status) {
1017 ocfs2_recovery_map_clear(osb, node_num);
1018 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001019 mlog(ML_ERROR,
1020 "Error %d recovering node %d on device (%u,%u)!\n",
1021 status, node_num,
1022 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1023 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001024 }
1025
Joel Becker553abd02008-02-01 12:03:57 -08001026 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001027 }
Joel Becker553abd02008-02-01 12:03:57 -08001028 spin_unlock(&osb->osb_lock);
1029 mlog(0, "All nodes recovered\n");
1030
Sunil Mushran539d8262008-07-14 17:31:10 -07001031 /* Refresh all journal recovery generations from disk */
1032 status = ocfs2_check_journals_nolocks(osb);
1033 status = (status == -EROFS) ? 0 : status;
1034 if (status < 0)
1035 mlog_errno(status);
1036
Mark Fashehccd979b2005-12-15 14:31:24 -08001037 ocfs2_super_unlock(osb, 1);
1038
1039 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001040 * node(s) may have disallowd a previos inode delete. Re-processing
1041 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001042 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1043 NULL);
1044
1045bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001046 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001047 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001048 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001049 goto restart;
1050 }
1051
1052 osb->recovery_thread_task = NULL;
1053 mb(); /* sync with ocfs2_recovery_thread_running */
1054 wake_up(&osb->recovery_event);
1055
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001056 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001057
1058 mlog_exit(status);
1059 /* no one is callint kthread_stop() for us so the kthread() api
1060 * requires that we call do_exit(). And it isn't exported, but
1061 * complete_and_exit() seems to be a minimal wrapper around it. */
1062 complete_and_exit(NULL, status);
1063 return status;
1064}
1065
1066void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1067{
1068 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1069 node_num, osb->node_num);
1070
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001071 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 if (osb->disable_recovery)
1073 goto out;
1074
1075 /* People waiting on recovery will wait on
1076 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001077 if (ocfs2_recovery_map_set(osb, node_num))
1078 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001079
1080 mlog(0, "starting recovery thread...\n");
1081
1082 if (osb->recovery_thread_task)
1083 goto out;
1084
1085 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001086 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001087 if (IS_ERR(osb->recovery_thread_task)) {
1088 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1089 osb->recovery_thread_task = NULL;
1090 }
1091
1092out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001093 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 wake_up(&osb->recovery_event);
1095
1096 mlog_exit_void();
1097}
1098
Sunil Mushran539d8262008-07-14 17:31:10 -07001099static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1100 int slot_num,
1101 struct buffer_head **bh,
1102 struct inode **ret_inode)
1103{
1104 int status = -EACCES;
1105 struct inode *inode = NULL;
1106
1107 BUG_ON(slot_num >= osb->max_slots);
1108
1109 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1110 slot_num);
1111 if (!inode || is_bad_inode(inode)) {
1112 mlog_errno(status);
1113 goto bail;
1114 }
1115 SET_INODE_JOURNAL(inode);
1116
Joel Beckerb657c952008-11-13 14:49:11 -08001117 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001118 if (status < 0) {
1119 mlog_errno(status);
1120 goto bail;
1121 }
1122
1123 status = 0;
1124
1125bail:
1126 if (inode) {
1127 if (status || !ret_inode)
1128 iput(inode);
1129 else
1130 *ret_inode = inode;
1131 }
1132 return status;
1133}
1134
Mark Fashehccd979b2005-12-15 14:31:24 -08001135/* Does the actual journal replay and marks the journal inode as
1136 * clean. Will only replay if the journal inode is marked dirty. */
1137static int ocfs2_replay_journal(struct ocfs2_super *osb,
1138 int node_num,
1139 int slot_num)
1140{
1141 int status;
1142 int got_lock = 0;
1143 unsigned int flags;
1144 struct inode *inode = NULL;
1145 struct ocfs2_dinode *fe;
1146 journal_t *journal = NULL;
1147 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001148 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001149
Sunil Mushran539d8262008-07-14 17:31:10 -07001150 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1151 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 mlog_errno(status);
1153 goto done;
1154 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001155
1156 fe = (struct ocfs2_dinode *)bh->b_data;
1157 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1158 brelse(bh);
1159 bh = NULL;
1160
1161 /*
1162 * As the fs recovery is asynchronous, there is a small chance that
1163 * another node mounted (and recovered) the slot before the recovery
1164 * thread could get the lock. To handle that, we dirty read the journal
1165 * inode for that slot to get the recovery generation. If it is
1166 * different than what we expected, the slot has been recovered.
1167 * If not, it needs recovery.
1168 */
1169 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1170 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1171 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1172 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1173 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 goto done;
1175 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001176
1177 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001178
Mark Fashehe63aecb62007-10-18 15:30:42 -07001179 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001180 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001181 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 if (status != -ERESTARTSYS)
1183 mlog(ML_ERROR, "Could not lock journal!\n");
1184 goto done;
1185 }
1186 got_lock = 1;
1187
1188 fe = (struct ocfs2_dinode *) bh->b_data;
1189
1190 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001191 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
1193 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1194 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001195 /* Refresh recovery generation for the slot */
1196 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 goto done;
1198 }
1199
1200 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1201 node_num, slot_num,
1202 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1203
1204 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1205
1206 status = ocfs2_force_read_journal(inode);
1207 if (status < 0) {
1208 mlog_errno(status);
1209 goto done;
1210 }
1211
1212 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001213 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 if (journal == NULL) {
1215 mlog(ML_ERROR, "Linux journal layer error\n");
1216 status = -EIO;
1217 goto done;
1218 }
1219
Joel Becker2b4e30f2008-09-03 20:03:41 -07001220 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 if (status < 0) {
1222 mlog_errno(status);
1223 if (!igrab(inode))
1224 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001225 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001226 goto done;
1227 }
1228
1229 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1230
1231 /* wipe the journal */
1232 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001233 jbd2_journal_lock_updates(journal);
1234 status = jbd2_journal_flush(journal);
1235 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001236 if (status < 0)
1237 mlog_errno(status);
1238
1239 /* This will mark the node clean */
1240 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1241 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1242 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1243
Sunil Mushran539d8262008-07-14 17:31:10 -07001244 /* Increment recovery generation to indicate successful recovery */
1245 ocfs2_bump_recovery_generation(fe);
1246 osb->slot_recovery_generations[slot_num] =
1247 ocfs2_get_recovery_generation(fe);
1248
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 status = ocfs2_write_block(osb, bh, inode);
1250 if (status < 0)
1251 mlog_errno(status);
1252
1253 if (!igrab(inode))
1254 BUG();
1255
Joel Becker2b4e30f2008-09-03 20:03:41 -07001256 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001257
1258done:
1259 /* drop the lock on this nodes journal */
1260 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001261 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001262
1263 if (inode)
1264 iput(inode);
1265
Mark Fasheha81cb882008-10-07 14:25:16 -07001266 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001267
1268 mlog_exit(status);
1269 return status;
1270}
1271
1272/*
1273 * Do the most important parts of node recovery:
1274 * - Replay it's journal
1275 * - Stamp a clean local allocator file
1276 * - Stamp a clean truncate log
1277 * - Mark the node clean
1278 *
1279 * If this function completes without error, a node in OCFS2 can be
1280 * said to have been safely recovered. As a result, failure during the
1281 * second part of a nodes recovery process (local alloc recovery) is
1282 * far less concerning.
1283 */
1284static int ocfs2_recover_node(struct ocfs2_super *osb,
1285 int node_num)
1286{
1287 int status = 0;
1288 int slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 struct ocfs2_dinode *la_copy = NULL;
1290 struct ocfs2_dinode *tl_copy = NULL;
1291
1292 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1293 node_num, osb->node_num);
1294
1295 mlog(0, "checking node %d\n", node_num);
1296
1297 /* Should not ever be called to recover ourselves -- in that
1298 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001299 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001300
Joel Beckerd85b20e2008-02-01 12:01:05 -08001301 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1302 if (slot_num == -ENOENT) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001303 status = 0;
1304 mlog(0, "no slot for this node, so no recovery required.\n");
1305 goto done;
1306 }
1307
1308 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1309
1310 status = ocfs2_replay_journal(osb, node_num, slot_num);
1311 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001312 if (status == -EBUSY) {
1313 mlog(0, "Skipping recovery for slot %u (node %u) "
1314 "as another node has recovered it\n", slot_num,
1315 node_num);
1316 status = 0;
1317 goto done;
1318 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001319 mlog_errno(status);
1320 goto done;
1321 }
1322
1323 /* Stamp a clean local alloc file AFTER recovering the journal... */
1324 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1325 if (status < 0) {
1326 mlog_errno(status);
1327 goto done;
1328 }
1329
1330 /* An error from begin_truncate_log_recovery is not
1331 * serious enough to warrant halting the rest of
1332 * recovery. */
1333 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1334 if (status < 0)
1335 mlog_errno(status);
1336
1337 /* Likewise, this would be a strange but ultimately not so
1338 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001339 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 if (status < 0)
1341 mlog_errno(status);
1342
1343 /* This will kfree the memory pointed to by la_copy and tl_copy */
1344 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1345 tl_copy);
1346
1347 status = 0;
1348done:
1349
1350 mlog_exit(status);
1351 return status;
1352}
1353
1354/* Test node liveness by trylocking his journal. If we get the lock,
1355 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1356 * still alive (we couldn't get the lock) and < 0 on error. */
1357static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1358 int slot_num)
1359{
1360 int status, flags;
1361 struct inode *inode = NULL;
1362
1363 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1364 slot_num);
1365 if (inode == NULL) {
1366 mlog(ML_ERROR, "access error\n");
1367 status = -EACCES;
1368 goto bail;
1369 }
1370 if (is_bad_inode(inode)) {
1371 mlog(ML_ERROR, "access error (bad inode)\n");
1372 iput(inode);
1373 inode = NULL;
1374 status = -EACCES;
1375 goto bail;
1376 }
1377 SET_INODE_JOURNAL(inode);
1378
1379 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001380 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 if (status < 0) {
1382 if (status != -EAGAIN)
1383 mlog_errno(status);
1384 goto bail;
1385 }
1386
Mark Fashehe63aecb62007-10-18 15:30:42 -07001387 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001388bail:
1389 if (inode)
1390 iput(inode);
1391
1392 return status;
1393}
1394
1395/* Call this underneath ocfs2_super_lock. It also assumes that the
1396 * slot info struct has been updated from disk. */
1397int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1398{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001399 unsigned int node_num;
1400 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001401 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001402 struct buffer_head *bh = NULL;
1403 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001404
1405 /* This is called with the super block cluster lock, so we
1406 * know that the slot map can't change underneath us. */
1407
Joel Beckerd85b20e2008-02-01 12:01:05 -08001408 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001409 /* Read journal inode to get the recovery generation */
1410 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1411 if (status) {
1412 mlog_errno(status);
1413 goto bail;
1414 }
1415 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001416 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001417 brelse(bh);
1418 bh = NULL;
1419
Mark Fasheha1af7d12008-08-19 17:20:28 -07001420 spin_lock(&osb->osb_lock);
1421 osb->slot_recovery_generations[i] = gen;
1422
Sunil Mushran539d8262008-07-14 17:31:10 -07001423 mlog(0, "Slot %u recovery generation is %u\n", i,
1424 osb->slot_recovery_generations[i]);
1425
Mark Fasheha1af7d12008-08-19 17:20:28 -07001426 if (i == osb->slot_num) {
1427 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001428 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001429 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001430
1431 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001432 if (status == -ENOENT) {
1433 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001435 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001436
Mark Fasheha1af7d12008-08-19 17:20:28 -07001437 if (__ocfs2_recovery_map_test(osb, node_num)) {
1438 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001439 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001440 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001441 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442
1443 /* Ok, we have a slot occupied by another node which
1444 * is not in the recovery map. We trylock his journal
1445 * file here to test if he's alive. */
1446 status = ocfs2_trylock_journal(osb, i);
1447 if (!status) {
1448 /* Since we're called from mount, we know that
1449 * the recovery thread can't race us on
1450 * setting / checking the recovery bits. */
1451 ocfs2_recovery_thread(osb, node_num);
1452 } else if ((status < 0) && (status != -EAGAIN)) {
1453 mlog_errno(status);
1454 goto bail;
1455 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001456 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001457
1458 status = 0;
1459bail:
1460 mlog_exit(status);
1461 return status;
1462}
1463
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001464struct ocfs2_orphan_filldir_priv {
1465 struct inode *head;
1466 struct ocfs2_super *osb;
1467};
1468
1469static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1470 loff_t pos, u64 ino, unsigned type)
1471{
1472 struct ocfs2_orphan_filldir_priv *p = priv;
1473 struct inode *iter;
1474
1475 if (name_len == 1 && !strncmp(".", name, 1))
1476 return 0;
1477 if (name_len == 2 && !strncmp("..", name, 2))
1478 return 0;
1479
1480 /* Skip bad inodes so that recovery can continue */
1481 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001482 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001483 if (IS_ERR(iter))
1484 return 0;
1485
1486 mlog(0, "queue orphan %llu\n",
1487 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1488 /* No locking is required for the next_orphan queue as there
1489 * is only ever a single process doing orphan recovery. */
1490 OCFS2_I(iter)->ip_next_orphan = p->head;
1491 p->head = iter;
1492
1493 return 0;
1494}
1495
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001496static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1497 int slot,
1498 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001499{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001500 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001501 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001502 struct ocfs2_orphan_filldir_priv priv;
1503 loff_t pos = 0;
1504
1505 priv.osb = osb;
1506 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001507
1508 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1509 ORPHAN_DIR_SYSTEM_INODE,
1510 slot);
1511 if (!orphan_dir_inode) {
1512 status = -ENOENT;
1513 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001514 return status;
1515 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001516
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001517 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001518 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001519 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001520 mlog_errno(status);
1521 goto out;
1522 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001523
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001524 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1525 ocfs2_orphan_filldir);
1526 if (status) {
1527 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001528 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001529 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001530
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001531 *head = priv.head;
1532
Mark Fasheha86370f2007-12-03 14:06:23 -08001533out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001534 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001535out:
1536 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001537 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001538 return status;
1539}
1540
1541static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1542 int slot)
1543{
1544 int ret;
1545
1546 spin_lock(&osb->osb_lock);
1547 ret = !osb->osb_orphan_wipes[slot];
1548 spin_unlock(&osb->osb_lock);
1549 return ret;
1550}
1551
1552static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1553 int slot)
1554{
1555 spin_lock(&osb->osb_lock);
1556 /* Mark ourselves such that new processes in delete_inode()
1557 * know to quit early. */
1558 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1559 while (osb->osb_orphan_wipes[slot]) {
1560 /* If any processes are already in the middle of an
1561 * orphan wipe on this dir, then we need to wait for
1562 * them. */
1563 spin_unlock(&osb->osb_lock);
1564 wait_event_interruptible(osb->osb_wipe_event,
1565 ocfs2_orphan_recovery_can_continue(osb, slot));
1566 spin_lock(&osb->osb_lock);
1567 }
1568 spin_unlock(&osb->osb_lock);
1569}
1570
1571static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1572 int slot)
1573{
1574 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1575}
1576
1577/*
1578 * Orphan recovery. Each mounted node has it's own orphan dir which we
1579 * must run during recovery. Our strategy here is to build a list of
1580 * the inodes in the orphan dir and iget/iput them. The VFS does
1581 * (most) of the rest of the work.
1582 *
1583 * Orphan recovery can happen at any time, not just mount so we have a
1584 * couple of extra considerations.
1585 *
1586 * - We grab as many inodes as we can under the orphan dir lock -
1587 * doing iget() outside the orphan dir risks getting a reference on
1588 * an invalid inode.
1589 * - We must be sure not to deadlock with other processes on the
1590 * system wanting to run delete_inode(). This can happen when they go
1591 * to lock the orphan dir and the orphan recovery process attempts to
1592 * iget() inside the orphan dir lock. This can be avoided by
1593 * advertising our state to ocfs2_delete_inode().
1594 */
1595static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1596 int slot)
1597{
1598 int ret = 0;
1599 struct inode *inode = NULL;
1600 struct inode *iter;
1601 struct ocfs2_inode_info *oi;
1602
1603 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1604
1605 ocfs2_mark_recovering_orphan_dir(osb, slot);
1606 ret = ocfs2_queue_orphans(osb, slot, &inode);
1607 ocfs2_clear_recovering_orphan_dir(osb, slot);
1608
1609 /* Error here should be noted, but we want to continue with as
1610 * many queued inodes as we've got. */
1611 if (ret)
1612 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001613
1614 while (inode) {
1615 oi = OCFS2_I(inode);
Mark Fashehb0697052006-03-03 10:24:33 -08001616 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001617
1618 iter = oi->ip_next_orphan;
1619
1620 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001621 /* The remote delete code may have set these on the
1622 * assumption that the other node would wipe them
1623 * successfully. If they are still in the node's
1624 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001625 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1626
1627 /* Set the proper information to get us going into
1628 * ocfs2_delete_inode. */
1629 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001630 spin_unlock(&oi->ip_lock);
1631
1632 iput(inode);
1633
1634 inode = iter;
1635 }
1636
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001637 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001638}
1639
1640static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
1641{
1642 /* This check is good because ocfs2 will wait on our recovery
1643 * thread before changing it to something other than MOUNTED
1644 * or DISABLED. */
1645 wait_event(osb->osb_mount_event,
1646 atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
1647 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1648
1649 /* If there's an error on mount, then we may never get to the
1650 * MOUNTED flag, but this is set right before
1651 * dismount_volume() so we can trust it. */
1652 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1653 mlog(0, "mount error, exiting!\n");
1654 return -EBUSY;
1655 }
1656
1657 return 0;
1658}
1659
1660static int ocfs2_commit_thread(void *arg)
1661{
1662 int status;
1663 struct ocfs2_super *osb = arg;
1664 struct ocfs2_journal *journal = osb->journal;
1665
1666 /* we can trust j_num_trans here because _should_stop() is only set in
1667 * shutdown and nobody other than ourselves should be able to start
1668 * transactions. committing on shutdown might take a few iterations
1669 * as final transactions put deleted inodes on the list */
1670 while (!(kthread_should_stop() &&
1671 atomic_read(&journal->j_num_trans) == 0)) {
1672
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001673 wait_event_interruptible(osb->checkpoint_event,
1674 atomic_read(&journal->j_num_trans)
1675 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001676
1677 status = ocfs2_commit_cache(osb);
1678 if (status < 0)
1679 mlog_errno(status);
1680
1681 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1682 mlog(ML_KTHREAD,
1683 "commit_thread: %u transactions pending on "
1684 "shutdown\n",
1685 atomic_read(&journal->j_num_trans));
1686 }
1687 }
1688
1689 return 0;
1690}
1691
Sunil Mushran539d8262008-07-14 17:31:10 -07001692/* Reads all the journal inodes without taking any cluster locks. Used
1693 * for hard readonly access to determine whether any journal requires
1694 * recovery. Also used to refresh the recovery generation numbers after
1695 * a journal has been recovered by another node.
1696 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001697int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1698{
1699 int ret = 0;
1700 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001701 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001702 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001703 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001704
1705 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001706 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1707 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001708 mlog_errno(ret);
1709 goto out;
1710 }
1711
1712 di = (struct ocfs2_dinode *) di_bh->b_data;
1713
Sunil Mushran539d8262008-07-14 17:31:10 -07001714 osb->slot_recovery_generations[slot] =
1715 ocfs2_get_recovery_generation(di);
1716
Mark Fashehccd979b2005-12-15 14:31:24 -08001717 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1718 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001719 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001720
1721 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001722 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001723 }
1724
1725out:
Sunil Mushran539d8262008-07-14 17:31:10 -07001726 if (journal_dirty)
1727 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08001728 return ret;
1729}