blob: 6d9658b0c5db272a4c7b407dc5c635f72d79a71a [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"
38#include "dlmglue.h"
39#include "extent_map.h"
40#include "heartbeat.h"
41#include "inode.h"
42#include "journal.h"
43#include "localalloc.h"
44#include "namei.h"
45#include "slot_map.h"
46#include "super.h"
47#include "vote.h"
48#include "sysfile.h"
49
50#include "buffer_head_io.h"
51
Ingo Molnar34af9462006-06-27 02:53:55 -070052DEFINE_SPINLOCK(trans_inc_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -080053
54static int ocfs2_force_read_journal(struct inode *inode);
55static int ocfs2_recover_node(struct ocfs2_super *osb,
56 int node_num);
57static int __ocfs2_recovery_thread(void *arg);
58static int ocfs2_commit_cache(struct ocfs2_super *osb);
59static int ocfs2_wait_on_mount(struct ocfs2_super *osb);
Mark Fashehccd979b2005-12-15 14:31:24 -080060static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
61 int dirty);
62static int ocfs2_trylock_journal(struct ocfs2_super *osb,
63 int slot_num);
64static int ocfs2_recover_orphans(struct ocfs2_super *osb,
65 int slot);
66static int ocfs2_commit_thread(void *arg);
67
68static int ocfs2_commit_cache(struct ocfs2_super *osb)
69{
70 int status = 0;
71 unsigned int flushed;
72 unsigned long old_id;
73 struct ocfs2_journal *journal = NULL;
74
75 mlog_entry_void();
76
77 journal = osb->journal;
78
79 /* Flush all pending commits and checkpoint the journal. */
80 down_write(&journal->j_trans_barrier);
81
82 if (atomic_read(&journal->j_num_trans) == 0) {
83 up_write(&journal->j_trans_barrier);
84 mlog(0, "No transactions for me to flush!\n");
85 goto finally;
86 }
87
88 journal_lock_updates(journal->j_journal);
89 status = journal_flush(journal->j_journal);
90 journal_unlock_updates(journal->j_journal);
91 if (status < 0) {
92 up_write(&journal->j_trans_barrier);
93 mlog_errno(status);
94 goto finally;
95 }
96
97 old_id = ocfs2_inc_trans_id(journal);
98
99 flushed = atomic_read(&journal->j_num_trans);
100 atomic_set(&journal->j_num_trans, 0);
101 up_write(&journal->j_trans_barrier);
102
103 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
104 journal->j_trans_id, flushed);
105
106 ocfs2_kick_vote_thread(osb);
107 wake_up(&journal->j_checkpointed);
108finally:
109 mlog_exit(status);
110 return status;
111}
112
Mark Fasheha301a272006-10-06 19:07:43 -0700113static struct ocfs2_journal_handle *ocfs2_alloc_handle(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800114{
115 struct ocfs2_journal_handle *retval = NULL;
116
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700117 retval = kcalloc(1, sizeof(*retval), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800118 if (!retval) {
119 mlog(ML_ERROR, "Failed to allocate memory for journal "
120 "handle!\n");
121 return NULL;
122 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800123 retval->k_handle = NULL;
124
Mark Fashehccd979b2005-12-15 14:31:24 -0800125 return retval;
126}
127
128/* pass it NULL and it will allocate a new handle object for you. If
129 * you pass it a handle however, it may still return error, in which
130 * case it has free'd the passed handle for you. */
131struct ocfs2_journal_handle *ocfs2_start_trans(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800132 int max_buffs)
133{
134 int ret;
135 journal_t *journal = osb->journal->j_journal;
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700136 struct ocfs2_journal_handle *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800137
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100138 BUG_ON(!osb || !osb->journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800139
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700140 if (ocfs2_is_hard_readonly(osb))
141 return ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800142
143 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
144 BUG_ON(max_buffs <= 0);
145
146 /* JBD might support this, but our journalling code doesn't yet. */
147 if (journal_current_handle()) {
148 mlog(ML_ERROR, "Recursive transaction attempted!\n");
149 BUG();
150 }
151
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700152 handle = ocfs2_alloc_handle(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800153 if (!handle) {
154 ret = -ENOMEM;
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700155 mlog_errno(ret);
156 return ERR_PTR(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800157 }
158
Mark Fashehccd979b2005-12-15 14:31:24 -0800159 down_read(&osb->journal->j_trans_barrier);
160
161 /* actually start the transaction now */
162 handle->k_handle = journal_start(journal, max_buffs);
163 if (IS_ERR(handle->k_handle)) {
164 up_read(&osb->journal->j_trans_barrier);
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700165 kfree(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800166
167 ret = PTR_ERR(handle->k_handle);
168 handle->k_handle = NULL;
169 mlog_errno(ret);
170
171 if (is_journal_aborted(journal)) {
172 ocfs2_abort(osb->sb, "Detected aborted journal");
173 ret = -EROFS;
174 }
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700175 return ERR_PTR(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800176 }
177
178 atomic_inc(&(osb->journal->j_num_trans));
Mark Fashehccd979b2005-12-15 14:31:24 -0800179
Mark Fashehccd979b2005-12-15 14:31:24 -0800180 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800181}
182
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700183void ocfs2_commit_trans(struct ocfs2_super *osb,
184 struct ocfs2_journal_handle *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800185{
186 handle_t *jbd_handle;
187 int retval;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700188 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800189
190 mlog_entry_void();
191
192 BUG_ON(!handle);
193
Mark Fashehc161f892006-10-05 15:11:36 -0700194 if (!handle->k_handle) {
Mark Fashehdaf29e92006-10-06 19:05:31 -0700195 kfree(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800196 mlog_exit_void();
197 return;
198 }
199
Mark Fashehccd979b2005-12-15 14:31:24 -0800200 /* ocfs2_extend_trans may have had to call journal_restart
201 * which will always commit the transaction, but may return
202 * error for any number of reasons. If this is the case, we
203 * clear k_handle as it's not valid any more. */
204 if (handle->k_handle) {
205 jbd_handle = handle->k_handle;
206
Mark Fashehccd979b2005-12-15 14:31:24 -0800207 /* actually stop the transaction. if we've set h_sync,
208 * it'll have been committed when we return */
209 retval = journal_stop(jbd_handle);
210 if (retval < 0) {
211 mlog_errno(retval);
212 mlog(ML_ERROR, "Could not commit transaction\n");
213 BUG();
214 }
215
216 handle->k_handle = NULL; /* it's been free'd in journal_stop */
217 }
218
Mark Fashehccd979b2005-12-15 14:31:24 -0800219 up_read(&journal->j_trans_barrier);
220
221 kfree(handle);
222 mlog_exit_void();
223}
224
225/*
226 * 'nblocks' is what you want to add to the current
227 * transaction. extend_trans will either extend the current handle by
228 * nblocks, or commit it and start a new one with nblocks credits.
229 *
230 * WARNING: This will not release any semaphores or disk locks taken
231 * during the transaction, so make sure they were taken *before*
232 * start_trans or we'll have ordering deadlocks.
233 *
234 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
235 * good because transaction ids haven't yet been recorded on the
236 * cluster locks associated with this handle.
237 */
Mark Fasheh1fc58142006-10-05 14:15:36 -0700238int ocfs2_extend_trans(handle_t *handle, int nblocks)
Mark Fashehccd979b2005-12-15 14:31:24 -0800239{
240 int status;
241
242 BUG_ON(!handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800243 BUG_ON(!nblocks);
244
245 mlog_entry_void();
246
247 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
248
Mark Fasheh1fc58142006-10-05 14:15:36 -0700249 status = journal_extend(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800250 if (status < 0) {
251 mlog_errno(status);
252 goto bail;
253 }
254
255 if (status > 0) {
256 mlog(0, "journal_extend failed, trying journal_restart\n");
Mark Fasheh1fc58142006-10-05 14:15:36 -0700257 status = journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800258 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800259 mlog_errno(status);
260 goto bail;
261 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700262 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800263
264 status = 0;
265bail:
266
267 mlog_exit(status);
268 return status;
269}
270
271int ocfs2_journal_access(struct ocfs2_journal_handle *handle,
272 struct inode *inode,
273 struct buffer_head *bh,
274 int type)
275{
276 int status;
277
278 BUG_ON(!inode);
279 BUG_ON(!handle);
280 BUG_ON(!bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800281
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800282 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800283 (unsigned long long)bh->b_blocknr, type,
284 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
285 "OCFS2_JOURNAL_ACCESS_CREATE" :
286 "OCFS2_JOURNAL_ACCESS_WRITE",
287 bh->b_size);
288
289 /* we can safely remove this assertion after testing. */
290 if (!buffer_uptodate(bh)) {
291 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
292 mlog(ML_ERROR, "b_blocknr=%llu\n",
293 (unsigned long long)bh->b_blocknr);
294 BUG();
295 }
296
297 /* Set the current transaction information on the inode so
298 * that the locking code knows whether it can drop it's locks
299 * on this inode or not. We're protected from the commit
300 * thread updating the current transaction id until
301 * ocfs2_commit_trans() because ocfs2_start_trans() took
302 * j_trans_barrier for us. */
303 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
304
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800305 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800306 switch (type) {
307 case OCFS2_JOURNAL_ACCESS_CREATE:
308 case OCFS2_JOURNAL_ACCESS_WRITE:
309 status = journal_get_write_access(handle->k_handle, bh);
310 break;
311
312 case OCFS2_JOURNAL_ACCESS_UNDO:
313 status = journal_get_undo_access(handle->k_handle, bh);
314 break;
315
316 default:
317 status = -EINVAL;
318 mlog(ML_ERROR, "Uknown access type!\n");
319 }
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800320 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800321
322 if (status < 0)
323 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
324 status, type);
325
326 mlog_exit(status);
327 return status;
328}
329
330int ocfs2_journal_dirty(struct ocfs2_journal_handle *handle,
331 struct buffer_head *bh)
332{
333 int status;
334
Mark Fashehccd979b2005-12-15 14:31:24 -0800335 mlog_entry("(bh->b_blocknr=%llu)\n",
336 (unsigned long long)bh->b_blocknr);
337
338 status = journal_dirty_metadata(handle->k_handle, bh);
339 if (status < 0)
340 mlog(ML_ERROR, "Could not dirty metadata buffer. "
341 "(bh->b_blocknr=%llu)\n",
342 (unsigned long long)bh->b_blocknr);
343
344 mlog_exit(status);
345 return status;
346}
347
348int ocfs2_journal_dirty_data(handle_t *handle,
349 struct buffer_head *bh)
350{
351 int err = journal_dirty_data(handle, bh);
352 if (err)
353 mlog_errno(err);
354 /* TODO: When we can handle it, abort the handle and go RO on
355 * error here. */
356
357 return err;
358}
359
Mark Fashehccd979b2005-12-15 14:31:24 -0800360#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * 5)
361
362void ocfs2_set_journal_params(struct ocfs2_super *osb)
363{
364 journal_t *journal = osb->journal->j_journal;
365
366 spin_lock(&journal->j_state_lock);
367 journal->j_commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
368 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
369 journal->j_flags |= JFS_BARRIER;
370 else
371 journal->j_flags &= ~JFS_BARRIER;
372 spin_unlock(&journal->j_state_lock);
373}
374
375int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
376{
377 int status = -1;
378 struct inode *inode = NULL; /* the journal inode */
379 journal_t *j_journal = NULL;
380 struct ocfs2_dinode *di = NULL;
381 struct buffer_head *bh = NULL;
382 struct ocfs2_super *osb;
383 int meta_lock = 0;
384
385 mlog_entry_void();
386
387 BUG_ON(!journal);
388
389 osb = journal->j_osb;
390
391 /* already have the inode for our journal */
392 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
393 osb->slot_num);
394 if (inode == NULL) {
395 status = -EACCES;
396 mlog_errno(status);
397 goto done;
398 }
399 if (is_bad_inode(inode)) {
400 mlog(ML_ERROR, "access error (bad inode)\n");
401 iput(inode);
402 inode = NULL;
403 status = -EACCES;
404 goto done;
405 }
406
407 SET_INODE_JOURNAL(inode);
408 OCFS2_I(inode)->ip_open_count++;
409
Mark Fasheh6eff5792006-01-18 10:31:47 -0800410 /* Skip recovery waits here - journal inode metadata never
411 * changes in a live cluster so it can be considered an
412 * exception to the rule. */
Mark Fasheh4bcec182006-10-09 16:02:40 -0700413 status = ocfs2_meta_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800414 if (status < 0) {
415 if (status != -ERESTARTSYS)
416 mlog(ML_ERROR, "Could not get lock on journal!\n");
417 goto done;
418 }
419
420 meta_lock = 1;
421 di = (struct ocfs2_dinode *)bh->b_data;
422
423 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
424 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
425 inode->i_size);
426 status = -EINVAL;
427 goto done;
428 }
429
430 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800431 mlog(0, "inode->i_blocks = %llu\n",
432 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800433 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
434
435 /* call the kernels journal init function now */
436 j_journal = journal_init_inode(inode);
437 if (j_journal == NULL) {
438 mlog(ML_ERROR, "Linux journal layer error\n");
439 status = -EINVAL;
440 goto done;
441 }
442
443 mlog(0, "Returned from journal_init_inode\n");
444 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
445
446 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
447 OCFS2_JOURNAL_DIRTY_FL);
448
449 journal->j_journal = j_journal;
450 journal->j_inode = inode;
451 journal->j_bh = bh;
452
453 ocfs2_set_journal_params(osb);
454
455 journal->j_state = OCFS2_JOURNAL_LOADED;
456
457 status = 0;
458done:
459 if (status < 0) {
460 if (meta_lock)
461 ocfs2_meta_unlock(inode, 1);
462 if (bh != NULL)
463 brelse(bh);
464 if (inode) {
465 OCFS2_I(inode)->ip_open_count--;
466 iput(inode);
467 }
468 }
469
470 mlog_exit(status);
471 return status;
472}
473
474static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
475 int dirty)
476{
477 int status;
478 unsigned int flags;
479 struct ocfs2_journal *journal = osb->journal;
480 struct buffer_head *bh = journal->j_bh;
481 struct ocfs2_dinode *fe;
482
483 mlog_entry_void();
484
485 fe = (struct ocfs2_dinode *)bh->b_data;
486 if (!OCFS2_IS_VALID_DINODE(fe)) {
487 /* This is called from startup/shutdown which will
488 * handle the errors in a specific manner, so no need
489 * to call ocfs2_error() here. */
Mark Fashehb0697052006-03-03 10:24:33 -0800490 mlog(ML_ERROR, "Journal dinode %llu has invalid "
491 "signature: %.*s", (unsigned long long)fe->i_blkno, 7,
492 fe->i_signature);
Mark Fashehccd979b2005-12-15 14:31:24 -0800493 status = -EIO;
494 goto out;
495 }
496
497 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
498 if (dirty)
499 flags |= OCFS2_JOURNAL_DIRTY_FL;
500 else
501 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
502 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
503
504 status = ocfs2_write_block(osb, bh, journal->j_inode);
505 if (status < 0)
506 mlog_errno(status);
507
508out:
509 mlog_exit(status);
510 return status;
511}
512
513/*
514 * If the journal has been kmalloc'd it needs to be freed after this
515 * call.
516 */
517void ocfs2_journal_shutdown(struct ocfs2_super *osb)
518{
519 struct ocfs2_journal *journal = NULL;
520 int status = 0;
521 struct inode *inode = NULL;
522 int num_running_trans = 0;
523
524 mlog_entry_void();
525
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100526 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800527
528 journal = osb->journal;
529 if (!journal)
530 goto done;
531
532 inode = journal->j_inode;
533
534 if (journal->j_state != OCFS2_JOURNAL_LOADED)
535 goto done;
536
537 /* need to inc inode use count as journal_destroy will iput. */
538 if (!igrab(inode))
539 BUG();
540
541 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
542 if (num_running_trans > 0)
543 mlog(0, "Shutting down journal: must wait on %d "
544 "running transactions!\n",
545 num_running_trans);
546
547 /* Do a commit_cache here. It will flush our journal, *and*
548 * release any locks that are still held.
549 * set the SHUTDOWN flag and release the trans lock.
550 * the commit thread will take the trans lock for us below. */
551 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
552
553 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
554 * drop the trans_lock (which we want to hold until we
555 * completely destroy the journal. */
556 if (osb->commit_task) {
557 /* Wait for the commit thread */
558 mlog(0, "Waiting for ocfs2commit to exit....\n");
559 kthread_stop(osb->commit_task);
560 osb->commit_task = NULL;
561 }
562
563 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
564
565 status = ocfs2_journal_toggle_dirty(osb, 0);
566 if (status < 0)
567 mlog_errno(status);
568
569 /* Shutdown the kernel journal system */
570 journal_destroy(journal->j_journal);
571
572 OCFS2_I(inode)->ip_open_count--;
573
574 /* unlock our journal */
575 ocfs2_meta_unlock(inode, 1);
576
577 brelse(journal->j_bh);
578 journal->j_bh = NULL;
579
580 journal->j_state = OCFS2_JOURNAL_FREE;
581
582// up_write(&journal->j_trans_barrier);
583done:
584 if (inode)
585 iput(inode);
586 mlog_exit_void();
587}
588
589static void ocfs2_clear_journal_error(struct super_block *sb,
590 journal_t *journal,
591 int slot)
592{
593 int olderr;
594
595 olderr = journal_errno(journal);
596 if (olderr) {
597 mlog(ML_ERROR, "File system error %d recorded in "
598 "journal %u.\n", olderr, slot);
599 mlog(ML_ERROR, "File system on device %s needs checking.\n",
600 sb->s_id);
601
602 journal_ack_err(journal);
603 journal_clear_err(journal);
604 }
605}
606
607int ocfs2_journal_load(struct ocfs2_journal *journal)
608{
609 int status = 0;
610 struct ocfs2_super *osb;
611
612 mlog_entry_void();
613
614 if (!journal)
615 BUG();
616
617 osb = journal->j_osb;
618
619 status = journal_load(journal->j_journal);
620 if (status < 0) {
621 mlog(ML_ERROR, "Failed to load journal!\n");
622 goto done;
623 }
624
625 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
626
627 status = ocfs2_journal_toggle_dirty(osb, 1);
628 if (status < 0) {
629 mlog_errno(status);
630 goto done;
631 }
632
633 /* Launch the commit thread */
Mark Fasheh78427042006-05-04 12:03:26 -0700634 osb->commit_task = kthread_run(ocfs2_commit_thread, osb, "ocfs2cmt");
Mark Fashehccd979b2005-12-15 14:31:24 -0800635 if (IS_ERR(osb->commit_task)) {
636 status = PTR_ERR(osb->commit_task);
637 osb->commit_task = NULL;
638 mlog(ML_ERROR, "unable to launch ocfs2commit thread, error=%d",
639 status);
640 goto done;
641 }
642
643done:
644 mlog_exit(status);
645 return status;
646}
647
648
649/* 'full' flag tells us whether we clear out all blocks or if we just
650 * mark the journal clean */
651int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
652{
653 int status;
654
655 mlog_entry_void();
656
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100657 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800658
659 status = journal_wipe(journal->j_journal, full);
660 if (status < 0) {
661 mlog_errno(status);
662 goto bail;
663 }
664
665 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0);
666 if (status < 0)
667 mlog_errno(status);
668
669bail:
670 mlog_exit(status);
671 return status;
672}
673
674/*
675 * JBD Might read a cached version of another nodes journal file. We
676 * don't want this as this file changes often and we get no
677 * notification on those changes. The only way to be sure that we've
678 * got the most up to date version of those blocks then is to force
679 * read them off disk. Just searching through the buffer cache won't
680 * work as there may be pages backing this file which are still marked
681 * up to date. We know things can't change on this file underneath us
682 * as we have the lock by now :)
683 */
684static int ocfs2_force_read_journal(struct inode *inode)
685{
686 int status = 0;
687 int i, p_blocks;
688 u64 v_blkno, p_blkno;
689#define CONCURRENT_JOURNAL_FILL 32
690 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
691
692 mlog_entry_void();
693
694 BUG_ON(inode->i_blocks !=
695 ocfs2_align_bytes_to_sectors(i_size_read(inode)));
696
697 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
698
Andrew Morton5515eff2006-03-26 01:37:53 -0800699 mlog(0, "Force reading %llu blocks\n",
700 (unsigned long long)(inode->i_blocks >>
701 (inode->i_sb->s_blocksize_bits - 9)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800702
703 v_blkno = 0;
704 while (v_blkno <
705 (inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9))) {
706
707 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
708 1, &p_blkno,
709 &p_blocks);
710 if (status < 0) {
711 mlog_errno(status);
712 goto bail;
713 }
714
715 if (p_blocks > CONCURRENT_JOURNAL_FILL)
716 p_blocks = CONCURRENT_JOURNAL_FILL;
717
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700718 /* We are reading journal data which should not
719 * be put in the uptodate cache */
Mark Fashehccd979b2005-12-15 14:31:24 -0800720 status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
721 p_blkno, p_blocks, bhs, 0,
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700722 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800723 if (status < 0) {
724 mlog_errno(status);
725 goto bail;
726 }
727
728 for(i = 0; i < p_blocks; i++) {
729 brelse(bhs[i]);
730 bhs[i] = NULL;
731 }
732
733 v_blkno += p_blocks;
734 }
735
736bail:
737 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
738 if (bhs[i])
739 brelse(bhs[i]);
740 mlog_exit(status);
741 return status;
742}
743
744struct ocfs2_la_recovery_item {
745 struct list_head lri_list;
746 int lri_slot;
747 struct ocfs2_dinode *lri_la_dinode;
748 struct ocfs2_dinode *lri_tl_dinode;
749};
750
751/* Does the second half of the recovery process. By this point, the
752 * node is marked clean and can actually be considered recovered,
753 * hence it's no longer in the recovery map, but there's still some
754 * cleanup we can do which shouldn't happen within the recovery thread
755 * as locking in that context becomes very difficult if we are to take
756 * recovering nodes into account.
757 *
758 * NOTE: This function can and will sleep on recovery of other nodes
759 * during cluster locking, just like any other ocfs2 process.
760 */
761void ocfs2_complete_recovery(void *data)
762{
763 int ret;
764 struct ocfs2_super *osb = data;
765 struct ocfs2_journal *journal = osb->journal;
766 struct ocfs2_dinode *la_dinode, *tl_dinode;
767 struct ocfs2_la_recovery_item *item;
768 struct list_head *p, *n;
769 LIST_HEAD(tmp_la_list);
770
771 mlog_entry_void();
772
773 mlog(0, "completing recovery from keventd\n");
774
775 spin_lock(&journal->j_lock);
776 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
777 spin_unlock(&journal->j_lock);
778
779 list_for_each_safe(p, n, &tmp_la_list) {
780 item = list_entry(p, struct ocfs2_la_recovery_item, lri_list);
781 list_del_init(&item->lri_list);
782
783 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
784
785 la_dinode = item->lri_la_dinode;
786 if (la_dinode) {
Mark Fashehb0697052006-03-03 10:24:33 -0800787 mlog(0, "Clean up local alloc %llu\n",
788 (unsigned long long)la_dinode->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800789
790 ret = ocfs2_complete_local_alloc_recovery(osb,
791 la_dinode);
792 if (ret < 0)
793 mlog_errno(ret);
794
795 kfree(la_dinode);
796 }
797
798 tl_dinode = item->lri_tl_dinode;
799 if (tl_dinode) {
Mark Fashehb0697052006-03-03 10:24:33 -0800800 mlog(0, "Clean up truncate log %llu\n",
801 (unsigned long long)tl_dinode->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800802
803 ret = ocfs2_complete_truncate_log_recovery(osb,
804 tl_dinode);
805 if (ret < 0)
806 mlog_errno(ret);
807
808 kfree(tl_dinode);
809 }
810
811 ret = ocfs2_recover_orphans(osb, item->lri_slot);
812 if (ret < 0)
813 mlog_errno(ret);
814
815 kfree(item);
816 }
817
818 mlog(0, "Recovery completion\n");
819 mlog_exit_void();
820}
821
822/* NOTE: This function always eats your references to la_dinode and
823 * tl_dinode, either manually on error, or by passing them to
824 * ocfs2_complete_recovery */
825static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
826 int slot_num,
827 struct ocfs2_dinode *la_dinode,
828 struct ocfs2_dinode *tl_dinode)
829{
830 struct ocfs2_la_recovery_item *item;
831
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700832 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 if (!item) {
834 /* Though we wish to avoid it, we are in fact safe in
835 * skipping local alloc cleanup as fsck.ocfs2 is more
836 * than capable of reclaiming unused space. */
837 if (la_dinode)
838 kfree(la_dinode);
839
840 if (tl_dinode)
841 kfree(tl_dinode);
842
843 mlog_errno(-ENOMEM);
844 return;
845 }
846
847 INIT_LIST_HEAD(&item->lri_list);
848 item->lri_la_dinode = la_dinode;
849 item->lri_slot = slot_num;
850 item->lri_tl_dinode = tl_dinode;
851
852 spin_lock(&journal->j_lock);
853 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
854 queue_work(ocfs2_wq, &journal->j_recovery_work);
855 spin_unlock(&journal->j_lock);
856}
857
858/* Called by the mount code to queue recovery the last part of
859 * recovery for it's own slot. */
860void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
861{
862 struct ocfs2_journal *journal = osb->journal;
863
864 if (osb->dirty) {
865 /* No need to queue up our truncate_log as regular
866 * cleanup will catch that. */
867 ocfs2_queue_recovery_completion(journal,
868 osb->slot_num,
869 osb->local_alloc_copy,
870 NULL);
871 ocfs2_schedule_truncate_log_flush(osb, 0);
872
873 osb->local_alloc_copy = NULL;
874 osb->dirty = 0;
875 }
876}
877
878static int __ocfs2_recovery_thread(void *arg)
879{
880 int status, node_num;
881 struct ocfs2_super *osb = arg;
882
883 mlog_entry_void();
884
885 status = ocfs2_wait_on_mount(osb);
886 if (status < 0) {
887 goto bail;
888 }
889
890restart:
891 status = ocfs2_super_lock(osb, 1);
892 if (status < 0) {
893 mlog_errno(status);
894 goto bail;
895 }
896
897 while(!ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
898 node_num = ocfs2_node_map_first_set_bit(osb,
899 &osb->recovery_map);
900 if (node_num == O2NM_INVALID_NODE_NUM) {
901 mlog(0, "Out of nodes to recover.\n");
902 break;
903 }
904
905 status = ocfs2_recover_node(osb, node_num);
906 if (status < 0) {
907 mlog(ML_ERROR,
908 "Error %d recovering node %d on device (%u,%u)!\n",
909 status, node_num,
910 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
911 mlog(ML_ERROR, "Volume requires unmount.\n");
912 continue;
913 }
914
915 ocfs2_recovery_map_clear(osb, node_num);
916 }
917 ocfs2_super_unlock(osb, 1);
918
919 /* We always run recovery on our own orphan dir - the dead
920 * node(s) may have voted "no" on an inode delete earlier. A
921 * revote is therefore required. */
922 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
923 NULL);
924
925bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -0800926 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800927 if (!status &&
928 !ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -0800929 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800930 goto restart;
931 }
932
933 osb->recovery_thread_task = NULL;
934 mb(); /* sync with ocfs2_recovery_thread_running */
935 wake_up(&osb->recovery_event);
936
Arjan van de Venc74ec2f2006-01-13 21:54:23 -0800937 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800938
939 mlog_exit(status);
940 /* no one is callint kthread_stop() for us so the kthread() api
941 * requires that we call do_exit(). And it isn't exported, but
942 * complete_and_exit() seems to be a minimal wrapper around it. */
943 complete_and_exit(NULL, status);
944 return status;
945}
946
947void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
948{
949 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
950 node_num, osb->node_num);
951
Arjan van de Venc74ec2f2006-01-13 21:54:23 -0800952 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800953 if (osb->disable_recovery)
954 goto out;
955
956 /* People waiting on recovery will wait on
957 * the recovery map to empty. */
958 if (!ocfs2_recovery_map_set(osb, node_num))
959 mlog(0, "node %d already be in recovery.\n", node_num);
960
961 mlog(0, "starting recovery thread...\n");
962
963 if (osb->recovery_thread_task)
964 goto out;
965
966 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -0700967 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -0800968 if (IS_ERR(osb->recovery_thread_task)) {
969 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
970 osb->recovery_thread_task = NULL;
971 }
972
973out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -0800974 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800975 wake_up(&osb->recovery_event);
976
977 mlog_exit_void();
978}
979
980/* Does the actual journal replay and marks the journal inode as
981 * clean. Will only replay if the journal inode is marked dirty. */
982static int ocfs2_replay_journal(struct ocfs2_super *osb,
983 int node_num,
984 int slot_num)
985{
986 int status;
987 int got_lock = 0;
988 unsigned int flags;
989 struct inode *inode = NULL;
990 struct ocfs2_dinode *fe;
991 journal_t *journal = NULL;
992 struct buffer_head *bh = NULL;
993
994 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
995 slot_num);
996 if (inode == NULL) {
997 status = -EACCES;
998 mlog_errno(status);
999 goto done;
1000 }
1001 if (is_bad_inode(inode)) {
1002 status = -EACCES;
1003 iput(inode);
1004 inode = NULL;
1005 mlog_errno(status);
1006 goto done;
1007 }
1008 SET_INODE_JOURNAL(inode);
1009
Mark Fasheh4bcec182006-10-09 16:02:40 -07001010 status = ocfs2_meta_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 if (status < 0) {
1012 mlog(0, "status returned from ocfs2_meta_lock=%d\n", status);
1013 if (status != -ERESTARTSYS)
1014 mlog(ML_ERROR, "Could not lock journal!\n");
1015 goto done;
1016 }
1017 got_lock = 1;
1018
1019 fe = (struct ocfs2_dinode *) bh->b_data;
1020
1021 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1022
1023 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1024 mlog(0, "No recovery required for node %d\n", node_num);
1025 goto done;
1026 }
1027
1028 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1029 node_num, slot_num,
1030 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1031
1032 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1033
1034 status = ocfs2_force_read_journal(inode);
1035 if (status < 0) {
1036 mlog_errno(status);
1037 goto done;
1038 }
1039
1040 mlog(0, "calling journal_init_inode\n");
1041 journal = journal_init_inode(inode);
1042 if (journal == NULL) {
1043 mlog(ML_ERROR, "Linux journal layer error\n");
1044 status = -EIO;
1045 goto done;
1046 }
1047
1048 status = journal_load(journal);
1049 if (status < 0) {
1050 mlog_errno(status);
1051 if (!igrab(inode))
1052 BUG();
1053 journal_destroy(journal);
1054 goto done;
1055 }
1056
1057 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1058
1059 /* wipe the journal */
1060 mlog(0, "flushing the journal.\n");
1061 journal_lock_updates(journal);
1062 status = journal_flush(journal);
1063 journal_unlock_updates(journal);
1064 if (status < 0)
1065 mlog_errno(status);
1066
1067 /* This will mark the node clean */
1068 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1069 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1070 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1071
1072 status = ocfs2_write_block(osb, bh, inode);
1073 if (status < 0)
1074 mlog_errno(status);
1075
1076 if (!igrab(inode))
1077 BUG();
1078
1079 journal_destroy(journal);
1080
1081done:
1082 /* drop the lock on this nodes journal */
1083 if (got_lock)
1084 ocfs2_meta_unlock(inode, 1);
1085
1086 if (inode)
1087 iput(inode);
1088
1089 if (bh)
1090 brelse(bh);
1091
1092 mlog_exit(status);
1093 return status;
1094}
1095
1096/*
1097 * Do the most important parts of node recovery:
1098 * - Replay it's journal
1099 * - Stamp a clean local allocator file
1100 * - Stamp a clean truncate log
1101 * - Mark the node clean
1102 *
1103 * If this function completes without error, a node in OCFS2 can be
1104 * said to have been safely recovered. As a result, failure during the
1105 * second part of a nodes recovery process (local alloc recovery) is
1106 * far less concerning.
1107 */
1108static int ocfs2_recover_node(struct ocfs2_super *osb,
1109 int node_num)
1110{
1111 int status = 0;
1112 int slot_num;
1113 struct ocfs2_slot_info *si = osb->slot_info;
1114 struct ocfs2_dinode *la_copy = NULL;
1115 struct ocfs2_dinode *tl_copy = NULL;
1116
1117 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1118 node_num, osb->node_num);
1119
1120 mlog(0, "checking node %d\n", node_num);
1121
1122 /* Should not ever be called to recover ourselves -- in that
1123 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001124 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001125
1126 slot_num = ocfs2_node_num_to_slot(si, node_num);
1127 if (slot_num == OCFS2_INVALID_SLOT) {
1128 status = 0;
1129 mlog(0, "no slot for this node, so no recovery required.\n");
1130 goto done;
1131 }
1132
1133 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1134
1135 status = ocfs2_replay_journal(osb, node_num, slot_num);
1136 if (status < 0) {
1137 mlog_errno(status);
1138 goto done;
1139 }
1140
1141 /* Stamp a clean local alloc file AFTER recovering the journal... */
1142 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1143 if (status < 0) {
1144 mlog_errno(status);
1145 goto done;
1146 }
1147
1148 /* An error from begin_truncate_log_recovery is not
1149 * serious enough to warrant halting the rest of
1150 * recovery. */
1151 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1152 if (status < 0)
1153 mlog_errno(status);
1154
1155 /* Likewise, this would be a strange but ultimately not so
1156 * harmful place to get an error... */
1157 ocfs2_clear_slot(si, slot_num);
1158 status = ocfs2_update_disk_slots(osb, si);
1159 if (status < 0)
1160 mlog_errno(status);
1161
1162 /* This will kfree the memory pointed to by la_copy and tl_copy */
1163 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1164 tl_copy);
1165
1166 status = 0;
1167done:
1168
1169 mlog_exit(status);
1170 return status;
1171}
1172
1173/* Test node liveness by trylocking his journal. If we get the lock,
1174 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1175 * still alive (we couldn't get the lock) and < 0 on error. */
1176static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1177 int slot_num)
1178{
1179 int status, flags;
1180 struct inode *inode = NULL;
1181
1182 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1183 slot_num);
1184 if (inode == NULL) {
1185 mlog(ML_ERROR, "access error\n");
1186 status = -EACCES;
1187 goto bail;
1188 }
1189 if (is_bad_inode(inode)) {
1190 mlog(ML_ERROR, "access error (bad inode)\n");
1191 iput(inode);
1192 inode = NULL;
1193 status = -EACCES;
1194 goto bail;
1195 }
1196 SET_INODE_JOURNAL(inode);
1197
1198 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fasheh4bcec182006-10-09 16:02:40 -07001199 status = ocfs2_meta_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001200 if (status < 0) {
1201 if (status != -EAGAIN)
1202 mlog_errno(status);
1203 goto bail;
1204 }
1205
1206 ocfs2_meta_unlock(inode, 1);
1207bail:
1208 if (inode)
1209 iput(inode);
1210
1211 return status;
1212}
1213
1214/* Call this underneath ocfs2_super_lock. It also assumes that the
1215 * slot info struct has been updated from disk. */
1216int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1217{
1218 int status, i, node_num;
1219 struct ocfs2_slot_info *si = osb->slot_info;
1220
1221 /* This is called with the super block cluster lock, so we
1222 * know that the slot map can't change underneath us. */
1223
1224 spin_lock(&si->si_lock);
1225 for(i = 0; i < si->si_num_slots; i++) {
1226 if (i == osb->slot_num)
1227 continue;
1228 if (ocfs2_is_empty_slot(si, i))
1229 continue;
1230
1231 node_num = si->si_global_node_nums[i];
1232 if (ocfs2_node_map_test_bit(osb, &osb->recovery_map, node_num))
1233 continue;
1234 spin_unlock(&si->si_lock);
1235
1236 /* Ok, we have a slot occupied by another node which
1237 * is not in the recovery map. We trylock his journal
1238 * file here to test if he's alive. */
1239 status = ocfs2_trylock_journal(osb, i);
1240 if (!status) {
1241 /* Since we're called from mount, we know that
1242 * the recovery thread can't race us on
1243 * setting / checking the recovery bits. */
1244 ocfs2_recovery_thread(osb, node_num);
1245 } else if ((status < 0) && (status != -EAGAIN)) {
1246 mlog_errno(status);
1247 goto bail;
1248 }
1249
1250 spin_lock(&si->si_lock);
1251 }
1252 spin_unlock(&si->si_lock);
1253
1254 status = 0;
1255bail:
1256 mlog_exit(status);
1257 return status;
1258}
1259
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001260static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1261 int slot,
1262 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001263{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001264 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001265 struct inode *orphan_dir_inode = NULL;
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001266 struct inode *iter;
Mark Fashehccd979b2005-12-15 14:31:24 -08001267 unsigned long offset, blk, local;
1268 struct buffer_head *bh = NULL;
1269 struct ocfs2_dir_entry *de;
1270 struct super_block *sb = osb->sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001271
1272 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1273 ORPHAN_DIR_SYSTEM_INODE,
1274 slot);
1275 if (!orphan_dir_inode) {
1276 status = -ENOENT;
1277 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001278 return status;
1279 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001280
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001281 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fasheh4bcec182006-10-09 16:02:40 -07001282 status = ocfs2_meta_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001283 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001284 mlog_errno(status);
1285 goto out;
1286 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001287
1288 offset = 0;
1289 iter = NULL;
1290 while(offset < i_size_read(orphan_dir_inode)) {
1291 blk = offset >> sb->s_blocksize_bits;
1292
1293 bh = ocfs2_bread(orphan_dir_inode, blk, &status, 0);
1294 if (!bh)
1295 status = -EINVAL;
1296 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001297 if (bh)
1298 brelse(bh);
1299 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001300 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001301 }
1302
1303 local = 0;
1304 while(offset < i_size_read(orphan_dir_inode)
1305 && local < sb->s_blocksize) {
1306 de = (struct ocfs2_dir_entry *) (bh->b_data + local);
1307
1308 if (!ocfs2_check_dir_entry(orphan_dir_inode,
1309 de, bh, local)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 status = -EINVAL;
1311 mlog_errno(status);
1312 brelse(bh);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001313 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 }
1315
1316 local += le16_to_cpu(de->rec_len);
1317 offset += le16_to_cpu(de->rec_len);
1318
1319 /* I guess we silently fail on no inode? */
1320 if (!le64_to_cpu(de->inode))
1321 continue;
1322 if (de->file_type > OCFS2_FT_MAX) {
1323 mlog(ML_ERROR,
1324 "block %llu contains invalid de: "
Mark Fashehb0697052006-03-03 10:24:33 -08001325 "inode = %llu, rec_len = %u, "
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 "name_len = %u, file_type = %u, "
1327 "name='%.*s'\n",
1328 (unsigned long long)bh->b_blocknr,
Mark Fashehb0697052006-03-03 10:24:33 -08001329 (unsigned long long)le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -08001330 le16_to_cpu(de->rec_len),
1331 de->name_len,
1332 de->file_type,
1333 de->name_len,
1334 de->name);
1335 continue;
1336 }
1337 if (de->name_len == 1 && !strncmp(".", de->name, 1))
1338 continue;
1339 if (de->name_len == 2 && !strncmp("..", de->name, 2))
1340 continue;
1341
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001342 iter = ocfs2_iget(osb, le64_to_cpu(de->inode),
1343 OCFS2_FI_FLAG_NOLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 if (IS_ERR(iter))
1345 continue;
1346
Mark Fashehb0697052006-03-03 10:24:33 -08001347 mlog(0, "queue orphan %llu\n",
1348 (unsigned long long)OCFS2_I(iter)->ip_blkno);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001349 /* No locking is required for the next_orphan
1350 * queue as there is only ever a single
1351 * process doing orphan recovery. */
1352 OCFS2_I(iter)->ip_next_orphan = *head;
1353 *head = iter;
Mark Fashehccd979b2005-12-15 14:31:24 -08001354 }
1355 brelse(bh);
1356 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001357
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001358out_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -08001359 ocfs2_meta_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001360out:
1361 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001362 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001363 return status;
1364}
1365
1366static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1367 int slot)
1368{
1369 int ret;
1370
1371 spin_lock(&osb->osb_lock);
1372 ret = !osb->osb_orphan_wipes[slot];
1373 spin_unlock(&osb->osb_lock);
1374 return ret;
1375}
1376
1377static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1378 int slot)
1379{
1380 spin_lock(&osb->osb_lock);
1381 /* Mark ourselves such that new processes in delete_inode()
1382 * know to quit early. */
1383 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1384 while (osb->osb_orphan_wipes[slot]) {
1385 /* If any processes are already in the middle of an
1386 * orphan wipe on this dir, then we need to wait for
1387 * them. */
1388 spin_unlock(&osb->osb_lock);
1389 wait_event_interruptible(osb->osb_wipe_event,
1390 ocfs2_orphan_recovery_can_continue(osb, slot));
1391 spin_lock(&osb->osb_lock);
1392 }
1393 spin_unlock(&osb->osb_lock);
1394}
1395
1396static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1397 int slot)
1398{
1399 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1400}
1401
1402/*
1403 * Orphan recovery. Each mounted node has it's own orphan dir which we
1404 * must run during recovery. Our strategy here is to build a list of
1405 * the inodes in the orphan dir and iget/iput them. The VFS does
1406 * (most) of the rest of the work.
1407 *
1408 * Orphan recovery can happen at any time, not just mount so we have a
1409 * couple of extra considerations.
1410 *
1411 * - We grab as many inodes as we can under the orphan dir lock -
1412 * doing iget() outside the orphan dir risks getting a reference on
1413 * an invalid inode.
1414 * - We must be sure not to deadlock with other processes on the
1415 * system wanting to run delete_inode(). This can happen when they go
1416 * to lock the orphan dir and the orphan recovery process attempts to
1417 * iget() inside the orphan dir lock. This can be avoided by
1418 * advertising our state to ocfs2_delete_inode().
1419 */
1420static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1421 int slot)
1422{
1423 int ret = 0;
1424 struct inode *inode = NULL;
1425 struct inode *iter;
1426 struct ocfs2_inode_info *oi;
1427
1428 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1429
1430 ocfs2_mark_recovering_orphan_dir(osb, slot);
1431 ret = ocfs2_queue_orphans(osb, slot, &inode);
1432 ocfs2_clear_recovering_orphan_dir(osb, slot);
1433
1434 /* Error here should be noted, but we want to continue with as
1435 * many queued inodes as we've got. */
1436 if (ret)
1437 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001438
1439 while (inode) {
1440 oi = OCFS2_I(inode);
Mark Fashehb0697052006-03-03 10:24:33 -08001441 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442
1443 iter = oi->ip_next_orphan;
1444
1445 spin_lock(&oi->ip_lock);
1446 /* Delete voting may have set these on the assumption
1447 * that the other node would wipe them successfully.
1448 * If they are still in the node's orphan dir, we need
1449 * to reset that state. */
1450 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1451
1452 /* Set the proper information to get us going into
1453 * ocfs2_delete_inode. */
1454 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
1455 oi->ip_orphaned_slot = slot;
1456 spin_unlock(&oi->ip_lock);
1457
1458 iput(inode);
1459
1460 inode = iter;
1461 }
1462
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001463 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001464}
1465
1466static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
1467{
1468 /* This check is good because ocfs2 will wait on our recovery
1469 * thread before changing it to something other than MOUNTED
1470 * or DISABLED. */
1471 wait_event(osb->osb_mount_event,
1472 atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
1473 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1474
1475 /* If there's an error on mount, then we may never get to the
1476 * MOUNTED flag, but this is set right before
1477 * dismount_volume() so we can trust it. */
1478 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1479 mlog(0, "mount error, exiting!\n");
1480 return -EBUSY;
1481 }
1482
1483 return 0;
1484}
1485
1486static int ocfs2_commit_thread(void *arg)
1487{
1488 int status;
1489 struct ocfs2_super *osb = arg;
1490 struct ocfs2_journal *journal = osb->journal;
1491
1492 /* we can trust j_num_trans here because _should_stop() is only set in
1493 * shutdown and nobody other than ourselves should be able to start
1494 * transactions. committing on shutdown might take a few iterations
1495 * as final transactions put deleted inodes on the list */
1496 while (!(kthread_should_stop() &&
1497 atomic_read(&journal->j_num_trans) == 0)) {
1498
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001499 wait_event_interruptible(osb->checkpoint_event,
1500 atomic_read(&journal->j_num_trans)
1501 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001502
1503 status = ocfs2_commit_cache(osb);
1504 if (status < 0)
1505 mlog_errno(status);
1506
1507 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1508 mlog(ML_KTHREAD,
1509 "commit_thread: %u transactions pending on "
1510 "shutdown\n",
1511 atomic_read(&journal->j_num_trans));
1512 }
1513 }
1514
1515 return 0;
1516}
1517
1518/* Look for a dirty journal without taking any cluster locks. Used for
1519 * hard readonly access to determine whether the file system journals
1520 * require recovery. */
1521int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1522{
1523 int ret = 0;
1524 unsigned int slot;
1525 struct buffer_head *di_bh;
1526 struct ocfs2_dinode *di;
1527 struct inode *journal = NULL;
1528
1529 for(slot = 0; slot < osb->max_slots; slot++) {
1530 journal = ocfs2_get_system_file_inode(osb,
1531 JOURNAL_SYSTEM_INODE,
1532 slot);
1533 if (!journal || is_bad_inode(journal)) {
1534 ret = -EACCES;
1535 mlog_errno(ret);
1536 goto out;
1537 }
1538
1539 di_bh = NULL;
1540 ret = ocfs2_read_block(osb, OCFS2_I(journal)->ip_blkno, &di_bh,
1541 0, journal);
1542 if (ret < 0) {
1543 mlog_errno(ret);
1544 goto out;
1545 }
1546
1547 di = (struct ocfs2_dinode *) di_bh->b_data;
1548
1549 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1550 OCFS2_JOURNAL_DIRTY_FL)
1551 ret = -EROFS;
1552
1553 brelse(di_bh);
1554 if (ret)
1555 break;
1556 }
1557
1558out:
1559 if (journal)
1560 iput(journal);
1561
1562 return ret;
1563}