blob: 9698338adc3915e66a3db0eda77268e33b51f607 [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,
60 int dirty);
61static 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
218 journal_lock_updates(journal->j_journal);
219 status = journal_flush(journal->j_journal);
220 journal_unlock_updates(journal->j_journal);
221 if (status < 0) {
222 up_write(&journal->j_trans_barrier);
223 mlog_errno(status);
224 goto finally;
225 }
226
227 old_id = ocfs2_inc_trans_id(journal);
228
229 flushed = atomic_read(&journal->j_num_trans);
230 atomic_set(&journal->j_num_trans, 0);
231 up_write(&journal->j_trans_barrier);
232
233 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
234 journal->j_trans_id, flushed);
235
Mark 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
Mark Fasheh1fabe142006-10-09 18:11:45 -0700267 handle = journal_start(journal, max_buffs);
268 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
Mark Fasheh1fabe142006-10-09 18:11:45 -0700293 ret = journal_stop(handle);
294 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 *
Mark Fashehe8aed342007-12-03 16:43:01 -0800307 * This might call journal_restart() which will commit dirty buffers
308 * and then restart the transaction. Before calling
309 * ocfs2_extend_trans(), any changed blocks should have been
310 * dirtied. After calling it, all blocks which need to be changed must
311 * go through another set of journal_access/journal_dirty calls.
312 *
Mark 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
Mark Fasheh0879c582007-12-03 16:42:19 -0800332#ifdef OCFS2_DEBUG_FS
333 status = 1;
334#else
Mark Fasheh1fc58142006-10-05 14:15:36 -0700335 status = 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) {
343 mlog(0, "journal_extend failed, trying journal_restart\n");
Mark Fasheh1fc58142006-10-05 14:15:36 -0700344 status = journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800345 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800346 mlog_errno(status);
347 goto bail;
348 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700349 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800350
351 status = 0;
352bail:
353
354 mlog_exit(status);
355 return status;
356}
357
Mark Fasheh1fabe142006-10-09 18:11:45 -0700358int ocfs2_journal_access(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 struct inode *inode,
360 struct buffer_head *bh,
361 int type)
362{
363 int status;
364
365 BUG_ON(!inode);
366 BUG_ON(!handle);
367 BUG_ON(!bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800368
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800369 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 (unsigned long long)bh->b_blocknr, type,
371 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
372 "OCFS2_JOURNAL_ACCESS_CREATE" :
373 "OCFS2_JOURNAL_ACCESS_WRITE",
374 bh->b_size);
375
376 /* we can safely remove this assertion after testing. */
377 if (!buffer_uptodate(bh)) {
378 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
379 mlog(ML_ERROR, "b_blocknr=%llu\n",
380 (unsigned long long)bh->b_blocknr);
381 BUG();
382 }
383
384 /* Set the current transaction information on the inode so
385 * that the locking code knows whether it can drop it's locks
386 * on this inode or not. We're protected from the commit
387 * thread updating the current transaction id until
388 * ocfs2_commit_trans() because ocfs2_start_trans() took
389 * j_trans_barrier for us. */
390 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
391
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800392 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800393 switch (type) {
394 case OCFS2_JOURNAL_ACCESS_CREATE:
395 case OCFS2_JOURNAL_ACCESS_WRITE:
Mark Fasheh1fabe142006-10-09 18:11:45 -0700396 status = journal_get_write_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800397 break;
398
399 case OCFS2_JOURNAL_ACCESS_UNDO:
Mark Fasheh1fabe142006-10-09 18:11:45 -0700400 status = journal_get_undo_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 break;
402
403 default:
404 status = -EINVAL;
405 mlog(ML_ERROR, "Uknown access type!\n");
406 }
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800407 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800408
409 if (status < 0)
410 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
411 status, type);
412
413 mlog_exit(status);
414 return status;
415}
416
Mark Fasheh1fabe142006-10-09 18:11:45 -0700417int ocfs2_journal_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800418 struct buffer_head *bh)
419{
420 int status;
421
Mark Fashehccd979b2005-12-15 14:31:24 -0800422 mlog_entry("(bh->b_blocknr=%llu)\n",
423 (unsigned long long)bh->b_blocknr);
424
Mark Fasheh1fabe142006-10-09 18:11:45 -0700425 status = journal_dirty_metadata(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 if (status < 0)
427 mlog(ML_ERROR, "Could not dirty metadata buffer. "
428 "(bh->b_blocknr=%llu)\n",
429 (unsigned long long)bh->b_blocknr);
430
431 mlog_exit(status);
432 return status;
433}
434
435int ocfs2_journal_dirty_data(handle_t *handle,
436 struct buffer_head *bh)
437{
438 int err = journal_dirty_data(handle, bh);
439 if (err)
440 mlog_errno(err);
441 /* TODO: When we can handle it, abort the handle and go RO on
442 * error here. */
443
444 return err;
445}
446
Mark Fashehd147b3d2007-11-07 14:40:36 -0800447#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800448
449void ocfs2_set_journal_params(struct ocfs2_super *osb)
450{
451 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800452 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
453
454 if (osb->osb_commit_interval)
455 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800456
457 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800458 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800459 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
460 journal->j_flags |= JFS_BARRIER;
461 else
462 journal->j_flags &= ~JFS_BARRIER;
463 spin_unlock(&journal->j_state_lock);
464}
465
466int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
467{
468 int status = -1;
469 struct inode *inode = NULL; /* the journal inode */
470 journal_t *j_journal = NULL;
471 struct ocfs2_dinode *di = NULL;
472 struct buffer_head *bh = NULL;
473 struct ocfs2_super *osb;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700474 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800475
476 mlog_entry_void();
477
478 BUG_ON(!journal);
479
480 osb = journal->j_osb;
481
482 /* already have the inode for our journal */
483 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
484 osb->slot_num);
485 if (inode == NULL) {
486 status = -EACCES;
487 mlog_errno(status);
488 goto done;
489 }
490 if (is_bad_inode(inode)) {
491 mlog(ML_ERROR, "access error (bad inode)\n");
492 iput(inode);
493 inode = NULL;
494 status = -EACCES;
495 goto done;
496 }
497
498 SET_INODE_JOURNAL(inode);
499 OCFS2_I(inode)->ip_open_count++;
500
Mark Fasheh6eff5792006-01-18 10:31:47 -0800501 /* Skip recovery waits here - journal inode metadata never
502 * changes in a live cluster so it can be considered an
503 * exception to the rule. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700504 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800505 if (status < 0) {
506 if (status != -ERESTARTSYS)
507 mlog(ML_ERROR, "Could not get lock on journal!\n");
508 goto done;
509 }
510
Mark Fashehe63aecb62007-10-18 15:30:42 -0700511 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800512 di = (struct ocfs2_dinode *)bh->b_data;
513
514 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
515 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
516 inode->i_size);
517 status = -EINVAL;
518 goto done;
519 }
520
521 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800522 mlog(0, "inode->i_blocks = %llu\n",
523 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800524 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
525
526 /* call the kernels journal init function now */
527 j_journal = journal_init_inode(inode);
528 if (j_journal == NULL) {
529 mlog(ML_ERROR, "Linux journal layer error\n");
530 status = -EINVAL;
531 goto done;
532 }
533
534 mlog(0, "Returned from journal_init_inode\n");
535 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
536
537 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
538 OCFS2_JOURNAL_DIRTY_FL);
539
540 journal->j_journal = j_journal;
541 journal->j_inode = inode;
542 journal->j_bh = bh;
543
544 ocfs2_set_journal_params(osb);
545
546 journal->j_state = OCFS2_JOURNAL_LOADED;
547
548 status = 0;
549done:
550 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700551 if (inode_lock)
552 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800553 if (bh != NULL)
554 brelse(bh);
555 if (inode) {
556 OCFS2_I(inode)->ip_open_count--;
557 iput(inode);
558 }
559 }
560
561 mlog_exit(status);
562 return status;
563}
564
565static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
566 int dirty)
567{
568 int status;
569 unsigned int flags;
570 struct ocfs2_journal *journal = osb->journal;
571 struct buffer_head *bh = journal->j_bh;
572 struct ocfs2_dinode *fe;
573
574 mlog_entry_void();
575
576 fe = (struct ocfs2_dinode *)bh->b_data;
577 if (!OCFS2_IS_VALID_DINODE(fe)) {
578 /* This is called from startup/shutdown which will
579 * handle the errors in a specific manner, so no need
580 * to call ocfs2_error() here. */
Mark Fashehb06970532006-03-03 10:24:33 -0800581 mlog(ML_ERROR, "Journal dinode %llu has invalid "
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700582 "signature: %.*s",
583 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
Mark Fashehb06970532006-03-03 10:24:33 -0800584 fe->i_signature);
Mark Fashehccd979b2005-12-15 14:31:24 -0800585 status = -EIO;
586 goto out;
587 }
588
589 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
590 if (dirty)
591 flags |= OCFS2_JOURNAL_DIRTY_FL;
592 else
593 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
594 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
595
596 status = ocfs2_write_block(osb, bh, journal->j_inode);
597 if (status < 0)
598 mlog_errno(status);
599
600out:
601 mlog_exit(status);
602 return status;
603}
604
605/*
606 * If the journal has been kmalloc'd it needs to be freed after this
607 * call.
608 */
609void ocfs2_journal_shutdown(struct ocfs2_super *osb)
610{
611 struct ocfs2_journal *journal = NULL;
612 int status = 0;
613 struct inode *inode = NULL;
614 int num_running_trans = 0;
615
616 mlog_entry_void();
617
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100618 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800619
620 journal = osb->journal;
621 if (!journal)
622 goto done;
623
624 inode = journal->j_inode;
625
626 if (journal->j_state != OCFS2_JOURNAL_LOADED)
627 goto done;
628
629 /* need to inc inode use count as journal_destroy will iput. */
630 if (!igrab(inode))
631 BUG();
632
633 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
634 if (num_running_trans > 0)
635 mlog(0, "Shutting down journal: must wait on %d "
636 "running transactions!\n",
637 num_running_trans);
638
639 /* Do a commit_cache here. It will flush our journal, *and*
640 * release any locks that are still held.
641 * set the SHUTDOWN flag and release the trans lock.
642 * the commit thread will take the trans lock for us below. */
643 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
644
645 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
646 * drop the trans_lock (which we want to hold until we
647 * completely destroy the journal. */
648 if (osb->commit_task) {
649 /* Wait for the commit thread */
650 mlog(0, "Waiting for ocfs2commit to exit....\n");
651 kthread_stop(osb->commit_task);
652 osb->commit_task = NULL;
653 }
654
655 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
656
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800657 if (ocfs2_mount_local(osb)) {
658 journal_lock_updates(journal->j_journal);
659 status = journal_flush(journal->j_journal);
660 journal_unlock_updates(journal->j_journal);
661 if (status < 0)
662 mlog_errno(status);
663 }
664
665 if (status == 0) {
666 /*
667 * Do not toggle if flush was unsuccessful otherwise
668 * will leave dirty metadata in a "clean" journal
669 */
670 status = ocfs2_journal_toggle_dirty(osb, 0);
671 if (status < 0)
672 mlog_errno(status);
673 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800674
675 /* Shutdown the kernel journal system */
676 journal_destroy(journal->j_journal);
677
678 OCFS2_I(inode)->ip_open_count--;
679
680 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700681 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800682
683 brelse(journal->j_bh);
684 journal->j_bh = NULL;
685
686 journal->j_state = OCFS2_JOURNAL_FREE;
687
688// up_write(&journal->j_trans_barrier);
689done:
690 if (inode)
691 iput(inode);
692 mlog_exit_void();
693}
694
695static void ocfs2_clear_journal_error(struct super_block *sb,
696 journal_t *journal,
697 int slot)
698{
699 int olderr;
700
701 olderr = journal_errno(journal);
702 if (olderr) {
703 mlog(ML_ERROR, "File system error %d recorded in "
704 "journal %u.\n", olderr, slot);
705 mlog(ML_ERROR, "File system on device %s needs checking.\n",
706 sb->s_id);
707
708 journal_ack_err(journal);
709 journal_clear_err(journal);
710 }
711}
712
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800713int ocfs2_journal_load(struct ocfs2_journal *journal, int local)
Mark Fashehccd979b2005-12-15 14:31:24 -0800714{
715 int status = 0;
716 struct ocfs2_super *osb;
717
718 mlog_entry_void();
719
Julia Lawallb1f35502008-03-04 15:21:05 -0800720 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800721
722 osb = journal->j_osb;
723
724 status = journal_load(journal->j_journal);
725 if (status < 0) {
726 mlog(ML_ERROR, "Failed to load journal!\n");
727 goto done;
728 }
729
730 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
731
732 status = ocfs2_journal_toggle_dirty(osb, 1);
733 if (status < 0) {
734 mlog_errno(status);
735 goto done;
736 }
737
738 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800739 if (!local) {
740 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
741 "ocfs2cmt");
742 if (IS_ERR(osb->commit_task)) {
743 status = PTR_ERR(osb->commit_task);
744 osb->commit_task = NULL;
745 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
746 "error=%d", status);
747 goto done;
748 }
749 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800750 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800751
752done:
753 mlog_exit(status);
754 return status;
755}
756
757
758/* 'full' flag tells us whether we clear out all blocks or if we just
759 * mark the journal clean */
760int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
761{
762 int status;
763
764 mlog_entry_void();
765
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100766 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800767
768 status = journal_wipe(journal->j_journal, full);
769 if (status < 0) {
770 mlog_errno(status);
771 goto bail;
772 }
773
774 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0);
775 if (status < 0)
776 mlog_errno(status);
777
778bail:
779 mlog_exit(status);
780 return status;
781}
782
Joel Becker553abd02008-02-01 12:03:57 -0800783static int ocfs2_recovery_completed(struct ocfs2_super *osb)
784{
785 int empty;
786 struct ocfs2_recovery_map *rm = osb->recovery_map;
787
788 spin_lock(&osb->osb_lock);
789 empty = (rm->rm_used == 0);
790 spin_unlock(&osb->osb_lock);
791
792 return empty;
793}
794
795void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
796{
797 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
798}
799
Mark Fashehccd979b2005-12-15 14:31:24 -0800800/*
801 * JBD Might read a cached version of another nodes journal file. We
802 * don't want this as this file changes often and we get no
803 * notification on those changes. The only way to be sure that we've
804 * got the most up to date version of those blocks then is to force
805 * read them off disk. Just searching through the buffer cache won't
806 * work as there may be pages backing this file which are still marked
807 * up to date. We know things can't change on this file underneath us
808 * as we have the lock by now :)
809 */
810static int ocfs2_force_read_journal(struct inode *inode)
811{
812 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800813 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700814 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800815#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
817
818 mlog_entry_void();
819
Mark Fashehccd979b2005-12-15 14:31:24 -0800820 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
821
Mark Fasheh8110b072007-03-22 16:53:23 -0700822 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800823 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700824 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800825 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800826 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800827 if (status < 0) {
828 mlog_errno(status);
829 goto bail;
830 }
831
832 if (p_blocks > CONCURRENT_JOURNAL_FILL)
833 p_blocks = CONCURRENT_JOURNAL_FILL;
834
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700835 /* We are reading journal data which should not
836 * be put in the uptodate cache */
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
838 p_blkno, p_blocks, bhs, 0,
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700839 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800840 if (status < 0) {
841 mlog_errno(status);
842 goto bail;
843 }
844
845 for(i = 0; i < p_blocks; i++) {
846 brelse(bhs[i]);
847 bhs[i] = NULL;
848 }
849
850 v_blkno += p_blocks;
851 }
852
853bail:
854 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
855 if (bhs[i])
856 brelse(bhs[i]);
857 mlog_exit(status);
858 return status;
859}
860
861struct ocfs2_la_recovery_item {
862 struct list_head lri_list;
863 int lri_slot;
864 struct ocfs2_dinode *lri_la_dinode;
865 struct ocfs2_dinode *lri_tl_dinode;
866};
867
868/* Does the second half of the recovery process. By this point, the
869 * node is marked clean and can actually be considered recovered,
870 * hence it's no longer in the recovery map, but there's still some
871 * cleanup we can do which shouldn't happen within the recovery thread
872 * as locking in that context becomes very difficult if we are to take
873 * recovering nodes into account.
874 *
875 * NOTE: This function can and will sleep on recovery of other nodes
876 * during cluster locking, just like any other ocfs2 process.
877 */
David Howellsc4028952006-11-22 14:57:56 +0000878void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -0800879{
880 int ret;
David Howellsc4028952006-11-22 14:57:56 +0000881 struct ocfs2_journal *journal =
882 container_of(work, struct ocfs2_journal, j_recovery_work);
883 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -0800884 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +0200885 struct ocfs2_la_recovery_item *item, *n;
Mark Fashehccd979b2005-12-15 14:31:24 -0800886 LIST_HEAD(tmp_la_list);
887
888 mlog_entry_void();
889
890 mlog(0, "completing recovery from keventd\n");
891
892 spin_lock(&journal->j_lock);
893 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
894 spin_unlock(&journal->j_lock);
895
Christoph Hellwig800deef2007-05-17 16:03:13 +0200896 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800897 list_del_init(&item->lri_list);
898
899 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
900
901 la_dinode = item->lri_la_dinode;
902 if (la_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -0800903 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700904 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800905
906 ret = ocfs2_complete_local_alloc_recovery(osb,
907 la_dinode);
908 if (ret < 0)
909 mlog_errno(ret);
910
911 kfree(la_dinode);
912 }
913
914 tl_dinode = item->lri_tl_dinode;
915 if (tl_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -0800916 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700917 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800918
919 ret = ocfs2_complete_truncate_log_recovery(osb,
920 tl_dinode);
921 if (ret < 0)
922 mlog_errno(ret);
923
924 kfree(tl_dinode);
925 }
926
927 ret = ocfs2_recover_orphans(osb, item->lri_slot);
928 if (ret < 0)
929 mlog_errno(ret);
930
931 kfree(item);
932 }
933
934 mlog(0, "Recovery completion\n");
935 mlog_exit_void();
936}
937
938/* NOTE: This function always eats your references to la_dinode and
939 * tl_dinode, either manually on error, or by passing them to
940 * ocfs2_complete_recovery */
941static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
942 int slot_num,
943 struct ocfs2_dinode *la_dinode,
944 struct ocfs2_dinode *tl_dinode)
945{
946 struct ocfs2_la_recovery_item *item;
947
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700948 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800949 if (!item) {
950 /* Though we wish to avoid it, we are in fact safe in
951 * skipping local alloc cleanup as fsck.ocfs2 is more
952 * than capable of reclaiming unused space. */
953 if (la_dinode)
954 kfree(la_dinode);
955
956 if (tl_dinode)
957 kfree(tl_dinode);
958
959 mlog_errno(-ENOMEM);
960 return;
961 }
962
963 INIT_LIST_HEAD(&item->lri_list);
964 item->lri_la_dinode = la_dinode;
965 item->lri_slot = slot_num;
966 item->lri_tl_dinode = tl_dinode;
967
968 spin_lock(&journal->j_lock);
969 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
970 queue_work(ocfs2_wq, &journal->j_recovery_work);
971 spin_unlock(&journal->j_lock);
972}
973
974/* Called by the mount code to queue recovery the last part of
975 * recovery for it's own slot. */
976void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
977{
978 struct ocfs2_journal *journal = osb->journal;
979
980 if (osb->dirty) {
981 /* No need to queue up our truncate_log as regular
982 * cleanup will catch that. */
983 ocfs2_queue_recovery_completion(journal,
984 osb->slot_num,
985 osb->local_alloc_copy,
986 NULL);
987 ocfs2_schedule_truncate_log_flush(osb, 0);
988
989 osb->local_alloc_copy = NULL;
990 osb->dirty = 0;
991 }
992}
993
994static int __ocfs2_recovery_thread(void *arg)
995{
996 int status, node_num;
997 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -0800998 struct ocfs2_recovery_map *rm = osb->recovery_map;
Mark Fashehccd979b2005-12-15 14:31:24 -0800999
1000 mlog_entry_void();
1001
1002 status = ocfs2_wait_on_mount(osb);
1003 if (status < 0) {
1004 goto bail;
1005 }
1006
1007restart:
1008 status = ocfs2_super_lock(osb, 1);
1009 if (status < 0) {
1010 mlog_errno(status);
1011 goto bail;
1012 }
1013
Joel Becker553abd02008-02-01 12:03:57 -08001014 spin_lock(&osb->osb_lock);
1015 while (rm->rm_used) {
1016 /* It's always safe to remove entry zero, as we won't
1017 * clear it until ocfs2_recover_node() has succeeded. */
1018 node_num = rm->rm_entries[0];
1019 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001020
1021 status = ocfs2_recover_node(osb, node_num);
Joel Becker553abd02008-02-01 12:03:57 -08001022 if (!status) {
1023 ocfs2_recovery_map_clear(osb, node_num);
1024 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001025 mlog(ML_ERROR,
1026 "Error %d recovering node %d on device (%u,%u)!\n",
1027 status, node_num,
1028 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1029 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 }
1031
Joel Becker553abd02008-02-01 12:03:57 -08001032 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001033 }
Joel Becker553abd02008-02-01 12:03:57 -08001034 spin_unlock(&osb->osb_lock);
1035 mlog(0, "All nodes recovered\n");
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
1099/* Does the actual journal replay and marks the journal inode as
1100 * clean. Will only replay if the journal inode is marked dirty. */
1101static int ocfs2_replay_journal(struct ocfs2_super *osb,
1102 int node_num,
1103 int slot_num)
1104{
1105 int status;
1106 int got_lock = 0;
1107 unsigned int flags;
1108 struct inode *inode = NULL;
1109 struct ocfs2_dinode *fe;
1110 journal_t *journal = NULL;
1111 struct buffer_head *bh = NULL;
1112
1113 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1114 slot_num);
1115 if (inode == NULL) {
1116 status = -EACCES;
1117 mlog_errno(status);
1118 goto done;
1119 }
1120 if (is_bad_inode(inode)) {
1121 status = -EACCES;
1122 iput(inode);
1123 inode = NULL;
1124 mlog_errno(status);
1125 goto done;
1126 }
1127 SET_INODE_JOURNAL(inode);
1128
Mark Fashehe63aecb62007-10-18 15:30:42 -07001129 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001130 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001131 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 if (status != -ERESTARTSYS)
1133 mlog(ML_ERROR, "Could not lock journal!\n");
1134 goto done;
1135 }
1136 got_lock = 1;
1137
1138 fe = (struct ocfs2_dinode *) bh->b_data;
1139
1140 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1141
1142 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1143 mlog(0, "No recovery required for node %d\n", node_num);
1144 goto done;
1145 }
1146
1147 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1148 node_num, slot_num,
1149 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1150
1151 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1152
1153 status = ocfs2_force_read_journal(inode);
1154 if (status < 0) {
1155 mlog_errno(status);
1156 goto done;
1157 }
1158
1159 mlog(0, "calling journal_init_inode\n");
1160 journal = journal_init_inode(inode);
1161 if (journal == NULL) {
1162 mlog(ML_ERROR, "Linux journal layer error\n");
1163 status = -EIO;
1164 goto done;
1165 }
1166
1167 status = journal_load(journal);
1168 if (status < 0) {
1169 mlog_errno(status);
1170 if (!igrab(inode))
1171 BUG();
1172 journal_destroy(journal);
1173 goto done;
1174 }
1175
1176 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1177
1178 /* wipe the journal */
1179 mlog(0, "flushing the journal.\n");
1180 journal_lock_updates(journal);
1181 status = journal_flush(journal);
1182 journal_unlock_updates(journal);
1183 if (status < 0)
1184 mlog_errno(status);
1185
1186 /* This will mark the node clean */
1187 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1188 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1189 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1190
1191 status = ocfs2_write_block(osb, bh, inode);
1192 if (status < 0)
1193 mlog_errno(status);
1194
1195 if (!igrab(inode))
1196 BUG();
1197
1198 journal_destroy(journal);
1199
1200done:
1201 /* drop the lock on this nodes journal */
1202 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001203 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001204
1205 if (inode)
1206 iput(inode);
1207
1208 if (bh)
1209 brelse(bh);
1210
1211 mlog_exit(status);
1212 return status;
1213}
1214
1215/*
1216 * Do the most important parts of node recovery:
1217 * - Replay it's journal
1218 * - Stamp a clean local allocator file
1219 * - Stamp a clean truncate log
1220 * - Mark the node clean
1221 *
1222 * If this function completes without error, a node in OCFS2 can be
1223 * said to have been safely recovered. As a result, failure during the
1224 * second part of a nodes recovery process (local alloc recovery) is
1225 * far less concerning.
1226 */
1227static int ocfs2_recover_node(struct ocfs2_super *osb,
1228 int node_num)
1229{
1230 int status = 0;
1231 int slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001232 struct ocfs2_dinode *la_copy = NULL;
1233 struct ocfs2_dinode *tl_copy = NULL;
1234
1235 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1236 node_num, osb->node_num);
1237
1238 mlog(0, "checking node %d\n", node_num);
1239
1240 /* Should not ever be called to recover ourselves -- in that
1241 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001242 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001243
Joel Beckerd85b20e2008-02-01 12:01:05 -08001244 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1245 if (slot_num == -ENOENT) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 status = 0;
1247 mlog(0, "no slot for this node, so no recovery required.\n");
1248 goto done;
1249 }
1250
1251 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1252
1253 status = ocfs2_replay_journal(osb, node_num, slot_num);
1254 if (status < 0) {
1255 mlog_errno(status);
1256 goto done;
1257 }
1258
1259 /* Stamp a clean local alloc file AFTER recovering the journal... */
1260 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1261 if (status < 0) {
1262 mlog_errno(status);
1263 goto done;
1264 }
1265
1266 /* An error from begin_truncate_log_recovery is not
1267 * serious enough to warrant halting the rest of
1268 * recovery. */
1269 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1270 if (status < 0)
1271 mlog_errno(status);
1272
1273 /* Likewise, this would be a strange but ultimately not so
1274 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001275 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001276 if (status < 0)
1277 mlog_errno(status);
1278
1279 /* This will kfree the memory pointed to by la_copy and tl_copy */
1280 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1281 tl_copy);
1282
1283 status = 0;
1284done:
1285
1286 mlog_exit(status);
1287 return status;
1288}
1289
1290/* Test node liveness by trylocking his journal. If we get the lock,
1291 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1292 * still alive (we couldn't get the lock) and < 0 on error. */
1293static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1294 int slot_num)
1295{
1296 int status, flags;
1297 struct inode *inode = NULL;
1298
1299 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1300 slot_num);
1301 if (inode == NULL) {
1302 mlog(ML_ERROR, "access error\n");
1303 status = -EACCES;
1304 goto bail;
1305 }
1306 if (is_bad_inode(inode)) {
1307 mlog(ML_ERROR, "access error (bad inode)\n");
1308 iput(inode);
1309 inode = NULL;
1310 status = -EACCES;
1311 goto bail;
1312 }
1313 SET_INODE_JOURNAL(inode);
1314
1315 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001316 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001317 if (status < 0) {
1318 if (status != -EAGAIN)
1319 mlog_errno(status);
1320 goto bail;
1321 }
1322
Mark Fashehe63aecb62007-10-18 15:30:42 -07001323 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001324bail:
1325 if (inode)
1326 iput(inode);
1327
1328 return status;
1329}
1330
1331/* Call this underneath ocfs2_super_lock. It also assumes that the
1332 * slot info struct has been updated from disk. */
1333int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1334{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001335 unsigned int node_num;
1336 int status, i;
Mark Fashehccd979b2005-12-15 14:31:24 -08001337
1338 /* This is called with the super block cluster lock, so we
1339 * know that the slot map can't change underneath us. */
1340
Joel Beckerd85b20e2008-02-01 12:01:05 -08001341 spin_lock(&osb->osb_lock);
1342 for (i = 0; i < osb->max_slots; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 if (i == osb->slot_num)
1344 continue;
Joel Beckerd85b20e2008-02-01 12:01:05 -08001345
1346 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1347 if (status == -ENOENT)
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 continue;
1349
Joel Becker553abd02008-02-01 12:03:57 -08001350 if (__ocfs2_recovery_map_test(osb, node_num))
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 continue;
Joel Beckerd85b20e2008-02-01 12:01:05 -08001352 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001353
1354 /* Ok, we have a slot occupied by another node which
1355 * is not in the recovery map. We trylock his journal
1356 * file here to test if he's alive. */
1357 status = ocfs2_trylock_journal(osb, i);
1358 if (!status) {
1359 /* Since we're called from mount, we know that
1360 * the recovery thread can't race us on
1361 * setting / checking the recovery bits. */
1362 ocfs2_recovery_thread(osb, node_num);
1363 } else if ((status < 0) && (status != -EAGAIN)) {
1364 mlog_errno(status);
1365 goto bail;
1366 }
1367
Joel Beckerd85b20e2008-02-01 12:01:05 -08001368 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001369 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001370 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001371
1372 status = 0;
1373bail:
1374 mlog_exit(status);
1375 return status;
1376}
1377
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001378struct ocfs2_orphan_filldir_priv {
1379 struct inode *head;
1380 struct ocfs2_super *osb;
1381};
1382
1383static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1384 loff_t pos, u64 ino, unsigned type)
1385{
1386 struct ocfs2_orphan_filldir_priv *p = priv;
1387 struct inode *iter;
1388
1389 if (name_len == 1 && !strncmp(".", name, 1))
1390 return 0;
1391 if (name_len == 2 && !strncmp("..", name, 2))
1392 return 0;
1393
1394 /* Skip bad inodes so that recovery can continue */
1395 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001396 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001397 if (IS_ERR(iter))
1398 return 0;
1399
1400 mlog(0, "queue orphan %llu\n",
1401 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1402 /* No locking is required for the next_orphan queue as there
1403 * is only ever a single process doing orphan recovery. */
1404 OCFS2_I(iter)->ip_next_orphan = p->head;
1405 p->head = iter;
1406
1407 return 0;
1408}
1409
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001410static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1411 int slot,
1412 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001413{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001414 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001416 struct ocfs2_orphan_filldir_priv priv;
1417 loff_t pos = 0;
1418
1419 priv.osb = osb;
1420 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001421
1422 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1423 ORPHAN_DIR_SYSTEM_INODE,
1424 slot);
1425 if (!orphan_dir_inode) {
1426 status = -ENOENT;
1427 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001428 return status;
1429 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001430
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001431 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001432 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 mlog_errno(status);
1435 goto out;
1436 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001437
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001438 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1439 ocfs2_orphan_filldir);
1440 if (status) {
1441 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001442 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001443 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001444
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001445 *head = priv.head;
1446
Mark Fasheha86370f2007-12-03 14:06:23 -08001447out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001448 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001449out:
1450 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001451 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001452 return status;
1453}
1454
1455static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1456 int slot)
1457{
1458 int ret;
1459
1460 spin_lock(&osb->osb_lock);
1461 ret = !osb->osb_orphan_wipes[slot];
1462 spin_unlock(&osb->osb_lock);
1463 return ret;
1464}
1465
1466static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1467 int slot)
1468{
1469 spin_lock(&osb->osb_lock);
1470 /* Mark ourselves such that new processes in delete_inode()
1471 * know to quit early. */
1472 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1473 while (osb->osb_orphan_wipes[slot]) {
1474 /* If any processes are already in the middle of an
1475 * orphan wipe on this dir, then we need to wait for
1476 * them. */
1477 spin_unlock(&osb->osb_lock);
1478 wait_event_interruptible(osb->osb_wipe_event,
1479 ocfs2_orphan_recovery_can_continue(osb, slot));
1480 spin_lock(&osb->osb_lock);
1481 }
1482 spin_unlock(&osb->osb_lock);
1483}
1484
1485static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1486 int slot)
1487{
1488 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1489}
1490
1491/*
1492 * Orphan recovery. Each mounted node has it's own orphan dir which we
1493 * must run during recovery. Our strategy here is to build a list of
1494 * the inodes in the orphan dir and iget/iput them. The VFS does
1495 * (most) of the rest of the work.
1496 *
1497 * Orphan recovery can happen at any time, not just mount so we have a
1498 * couple of extra considerations.
1499 *
1500 * - We grab as many inodes as we can under the orphan dir lock -
1501 * doing iget() outside the orphan dir risks getting a reference on
1502 * an invalid inode.
1503 * - We must be sure not to deadlock with other processes on the
1504 * system wanting to run delete_inode(). This can happen when they go
1505 * to lock the orphan dir and the orphan recovery process attempts to
1506 * iget() inside the orphan dir lock. This can be avoided by
1507 * advertising our state to ocfs2_delete_inode().
1508 */
1509static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1510 int slot)
1511{
1512 int ret = 0;
1513 struct inode *inode = NULL;
1514 struct inode *iter;
1515 struct ocfs2_inode_info *oi;
1516
1517 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1518
1519 ocfs2_mark_recovering_orphan_dir(osb, slot);
1520 ret = ocfs2_queue_orphans(osb, slot, &inode);
1521 ocfs2_clear_recovering_orphan_dir(osb, slot);
1522
1523 /* Error here should be noted, but we want to continue with as
1524 * many queued inodes as we've got. */
1525 if (ret)
1526 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001527
1528 while (inode) {
1529 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001530 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001531
1532 iter = oi->ip_next_orphan;
1533
1534 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001535 /* The remote delete code may have set these on the
1536 * assumption that the other node would wipe them
1537 * successfully. If they are still in the node's
1538 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1540
1541 /* Set the proper information to get us going into
1542 * ocfs2_delete_inode. */
1543 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001544 spin_unlock(&oi->ip_lock);
1545
1546 iput(inode);
1547
1548 inode = iter;
1549 }
1550
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001551 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001552}
1553
1554static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
1555{
1556 /* This check is good because ocfs2 will wait on our recovery
1557 * thread before changing it to something other than MOUNTED
1558 * or DISABLED. */
1559 wait_event(osb->osb_mount_event,
1560 atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
1561 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1562
1563 /* If there's an error on mount, then we may never get to the
1564 * MOUNTED flag, but this is set right before
1565 * dismount_volume() so we can trust it. */
1566 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1567 mlog(0, "mount error, exiting!\n");
1568 return -EBUSY;
1569 }
1570
1571 return 0;
1572}
1573
1574static int ocfs2_commit_thread(void *arg)
1575{
1576 int status;
1577 struct ocfs2_super *osb = arg;
1578 struct ocfs2_journal *journal = osb->journal;
1579
1580 /* we can trust j_num_trans here because _should_stop() is only set in
1581 * shutdown and nobody other than ourselves should be able to start
1582 * transactions. committing on shutdown might take a few iterations
1583 * as final transactions put deleted inodes on the list */
1584 while (!(kthread_should_stop() &&
1585 atomic_read(&journal->j_num_trans) == 0)) {
1586
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001587 wait_event_interruptible(osb->checkpoint_event,
1588 atomic_read(&journal->j_num_trans)
1589 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001590
1591 status = ocfs2_commit_cache(osb);
1592 if (status < 0)
1593 mlog_errno(status);
1594
1595 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1596 mlog(ML_KTHREAD,
1597 "commit_thread: %u transactions pending on "
1598 "shutdown\n",
1599 atomic_read(&journal->j_num_trans));
1600 }
1601 }
1602
1603 return 0;
1604}
1605
1606/* Look for a dirty journal without taking any cluster locks. Used for
1607 * hard readonly access to determine whether the file system journals
1608 * require recovery. */
1609int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1610{
1611 int ret = 0;
1612 unsigned int slot;
1613 struct buffer_head *di_bh;
1614 struct ocfs2_dinode *di;
1615 struct inode *journal = NULL;
1616
1617 for(slot = 0; slot < osb->max_slots; slot++) {
1618 journal = ocfs2_get_system_file_inode(osb,
1619 JOURNAL_SYSTEM_INODE,
1620 slot);
1621 if (!journal || is_bad_inode(journal)) {
1622 ret = -EACCES;
1623 mlog_errno(ret);
1624 goto out;
1625 }
1626
1627 di_bh = NULL;
1628 ret = ocfs2_read_block(osb, OCFS2_I(journal)->ip_blkno, &di_bh,
1629 0, journal);
1630 if (ret < 0) {
1631 mlog_errno(ret);
1632 goto out;
1633 }
1634
1635 di = (struct ocfs2_dinode *) di_bh->b_data;
1636
1637 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1638 OCFS2_JOURNAL_DIRTY_FL)
1639 ret = -EROFS;
1640
1641 brelse(di_bh);
1642 if (ret)
1643 break;
1644 }
1645
1646out:
1647 if (journal)
1648 iput(journal);
1649
1650 return ret;
1651}