]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/ocfs2/journal.c
ocfs2: use smaller counters in ocfs2_remove_xattr_clusters_from_cache
[mirror_ubuntu-hirsute-kernel.git] / fs / ocfs2 / journal.c
CommitLineData
ccd979bd
MF
1/* -*- 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"
316f4b9f 38#include "dir.h"
ccd979bd
MF
39#include "dlmglue.h"
40#include "extent_map.h"
41#include "heartbeat.h"
42#include "inode.h"
43#include "journal.h"
44#include "localalloc.h"
ccd979bd
MF
45#include "slot_map.h"
46#include "super.h"
ccd979bd
MF
47#include "sysfile.h"
48
49#include "buffer_head_io.h"
50
34af946a 51DEFINE_SPINLOCK(trans_inc_lock);
ccd979bd
MF
52
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);
ccd979bd 59static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
539d8264 60 int dirty, int replayed);
ccd979bd
MF
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
553abd04
JB
67
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 {
fc881fa0 74 unsigned int rm_used;
553abd04
JB
75 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
ccd979bd
MF
198static 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
2b4e30fb
JB
218 jbd2_journal_lock_updates(journal->j_journal);
219 status = jbd2_journal_flush(journal->j_journal);
220 jbd2_journal_unlock_updates(journal->j_journal);
ccd979bd
MF
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
34d024f8 236 ocfs2_wake_downconvert_thread(osb);
ccd979bd
MF
237 wake_up(&journal->j_checkpointed);
238finally:
239 mlog_exit(status);
240 return status;
241}
242
ccd979bd
MF
243/* 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. */
1fabe148 246handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
ccd979bd 247{
ccd979bd 248 journal_t *journal = osb->journal->j_journal;
1fabe148 249 handle_t *handle;
ccd979bd 250
ebdec83b 251 BUG_ON(!osb || !osb->journal->j_journal);
ccd979bd 252
65eff9cc
MF
253 if (ocfs2_is_hard_readonly(osb))
254 return ERR_PTR(-EROFS);
ccd979bd
MF
255
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
ccd979bd
MF
265 down_read(&osb->journal->j_trans_barrier);
266
2b4e30fb 267 handle = jbd2_journal_start(journal, max_buffs);
1fabe148 268 if (IS_ERR(handle)) {
ccd979bd
MF
269 up_read(&osb->journal->j_trans_barrier);
270
1fabe148 271 mlog_errno(PTR_ERR(handle));
ccd979bd
MF
272
273 if (is_journal_aborted(journal)) {
274 ocfs2_abort(osb->sb, "Detected aborted journal");
1fabe148 275 handle = ERR_PTR(-EROFS);
ccd979bd 276 }
c271c5c2
SM
277 } else {
278 if (!ocfs2_mount_local(osb))
279 atomic_inc(&(osb->journal->j_num_trans));
280 }
ccd979bd 281
ccd979bd 282 return handle;
ccd979bd
MF
283}
284
1fabe148
MF
285int ocfs2_commit_trans(struct ocfs2_super *osb,
286 handle_t *handle)
ccd979bd 287{
1fabe148 288 int ret;
02dc1af4 289 struct ocfs2_journal *journal = osb->journal;
ccd979bd
MF
290
291 BUG_ON(!handle);
292
2b4e30fb 293 ret = jbd2_journal_stop(handle);
1fabe148
MF
294 if (ret < 0)
295 mlog_errno(ret);
ccd979bd
MF
296
297 up_read(&journal->j_trans_barrier);
298
1fabe148 299 return ret;
ccd979bd
MF
300}
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 *
2b4e30fb 307 * This might call jbd2_journal_restart() which will commit dirty buffers
e8aed345
MF
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 *
ccd979bd
MF
313 * 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 */
1fc58146 321int ocfs2_extend_trans(handle_t *handle, int nblocks)
ccd979bd
MF
322{
323 int status;
324
325 BUG_ON(!handle);
ccd979bd
MF
326 BUG_ON(!nblocks);
327
328 mlog_entry_void();
329
330 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
331
e407e397 332#ifdef CONFIG_OCFS2_DEBUG_FS
0879c584
MF
333 status = 1;
334#else
2b4e30fb 335 status = jbd2_journal_extend(handle, nblocks);
ccd979bd
MF
336 if (status < 0) {
337 mlog_errno(status);
338 goto bail;
339 }
0879c584 340#endif
ccd979bd
MF
341
342 if (status > 0) {
2b4e30fb
JB
343 mlog(0,
344 "jbd2_journal_extend failed, trying "
345 "jbd2_journal_restart\n");
346 status = jbd2_journal_restart(handle, nblocks);
ccd979bd 347 if (status < 0) {
ccd979bd
MF
348 mlog_errno(status);
349 goto bail;
350 }
01ddf1e1 351 }
ccd979bd
MF
352
353 status = 0;
354bail:
355
356 mlog_exit(status);
357 return status;
358}
359
1fabe148 360int ocfs2_journal_access(handle_t *handle,
ccd979bd
MF
361 struct inode *inode,
362 struct buffer_head *bh,
363 int type)
364{
365 int status;
366
367 BUG_ON(!inode);
368 BUG_ON(!handle);
369 BUG_ON(!bh);
ccd979bd 370
205f87f6 371 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
ccd979bd
MF
372 (unsigned long long)bh->b_blocknr, type,
373 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
374 "OCFS2_JOURNAL_ACCESS_CREATE" :
375 "OCFS2_JOURNAL_ACCESS_WRITE",
376 bh->b_size);
377
378 /* we can safely remove this assertion after testing. */
379 if (!buffer_uptodate(bh)) {
380 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
381 mlog(ML_ERROR, "b_blocknr=%llu\n",
382 (unsigned long long)bh->b_blocknr);
383 BUG();
384 }
385
386 /* Set the current transaction information on the inode so
387 * that the locking code knows whether it can drop it's locks
388 * on this inode or not. We're protected from the commit
389 * thread updating the current transaction id until
390 * ocfs2_commit_trans() because ocfs2_start_trans() took
391 * j_trans_barrier for us. */
392 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
393
251b6ecc 394 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
ccd979bd
MF
395 switch (type) {
396 case OCFS2_JOURNAL_ACCESS_CREATE:
397 case OCFS2_JOURNAL_ACCESS_WRITE:
2b4e30fb 398 status = jbd2_journal_get_write_access(handle, bh);
ccd979bd
MF
399 break;
400
401 case OCFS2_JOURNAL_ACCESS_UNDO:
2b4e30fb 402 status = jbd2_journal_get_undo_access(handle, bh);
ccd979bd
MF
403 break;
404
405 default:
406 status = -EINVAL;
407 mlog(ML_ERROR, "Uknown access type!\n");
408 }
251b6ecc 409 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
ccd979bd
MF
410
411 if (status < 0)
412 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
413 status, type);
414
415 mlog_exit(status);
416 return status;
417}
418
1fabe148 419int ocfs2_journal_dirty(handle_t *handle,
ccd979bd
MF
420 struct buffer_head *bh)
421{
422 int status;
423
ccd979bd
MF
424 mlog_entry("(bh->b_blocknr=%llu)\n",
425 (unsigned long long)bh->b_blocknr);
426
2b4e30fb 427 status = jbd2_journal_dirty_metadata(handle, bh);
ccd979bd
MF
428 if (status < 0)
429 mlog(ML_ERROR, "Could not dirty metadata buffer. "
430 "(bh->b_blocknr=%llu)\n",
431 (unsigned long long)bh->b_blocknr);
432
433 mlog_exit(status);
434 return status;
435}
436
2b4e30fb 437#ifdef CONFIG_OCFS2_COMPAT_JBD
ccd979bd
MF
438int ocfs2_journal_dirty_data(handle_t *handle,
439 struct buffer_head *bh)
440{
441 int err = journal_dirty_data(handle, bh);
442 if (err)
443 mlog_errno(err);
444 /* TODO: When we can handle it, abort the handle and go RO on
445 * error here. */
446
447 return err;
448}
2b4e30fb 449#endif
ccd979bd 450
2b4e30fb 451#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
ccd979bd
MF
452
453void ocfs2_set_journal_params(struct ocfs2_super *osb)
454{
455 journal_t *journal = osb->journal->j_journal;
d147b3d6
MF
456 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
457
458 if (osb->osb_commit_interval)
459 commit_interval = osb->osb_commit_interval;
ccd979bd
MF
460
461 spin_lock(&journal->j_state_lock);
d147b3d6 462 journal->j_commit_interval = commit_interval;
ccd979bd 463 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
2b4e30fb 464 journal->j_flags |= JBD2_BARRIER;
ccd979bd 465 else
2b4e30fb 466 journal->j_flags &= ~JBD2_BARRIER;
ccd979bd
MF
467 spin_unlock(&journal->j_state_lock);
468}
469
470int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
471{
472 int status = -1;
473 struct inode *inode = NULL; /* the journal inode */
474 journal_t *j_journal = NULL;
475 struct ocfs2_dinode *di = NULL;
476 struct buffer_head *bh = NULL;
477 struct ocfs2_super *osb;
e63aecb6 478 int inode_lock = 0;
ccd979bd
MF
479
480 mlog_entry_void();
481
482 BUG_ON(!journal);
483
484 osb = journal->j_osb;
485
486 /* already have the inode for our journal */
487 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
488 osb->slot_num);
489 if (inode == NULL) {
490 status = -EACCES;
491 mlog_errno(status);
492 goto done;
493 }
494 if (is_bad_inode(inode)) {
495 mlog(ML_ERROR, "access error (bad inode)\n");
496 iput(inode);
497 inode = NULL;
498 status = -EACCES;
499 goto done;
500 }
501
502 SET_INODE_JOURNAL(inode);
503 OCFS2_I(inode)->ip_open_count++;
504
6eff5790
MF
505 /* Skip recovery waits here - journal inode metadata never
506 * changes in a live cluster so it can be considered an
507 * exception to the rule. */
e63aecb6 508 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
ccd979bd
MF
509 if (status < 0) {
510 if (status != -ERESTARTSYS)
511 mlog(ML_ERROR, "Could not get lock on journal!\n");
512 goto done;
513 }
514
e63aecb6 515 inode_lock = 1;
ccd979bd
MF
516 di = (struct ocfs2_dinode *)bh->b_data;
517
518 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
519 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
520 inode->i_size);
521 status = -EINVAL;
522 goto done;
523 }
524
525 mlog(0, "inode->i_size = %lld\n", inode->i_size);
5515eff8
AM
526 mlog(0, "inode->i_blocks = %llu\n",
527 (unsigned long long)inode->i_blocks);
ccd979bd
MF
528 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
529
530 /* call the kernels journal init function now */
2b4e30fb 531 j_journal = jbd2_journal_init_inode(inode);
ccd979bd
MF
532 if (j_journal == NULL) {
533 mlog(ML_ERROR, "Linux journal layer error\n");
534 status = -EINVAL;
535 goto done;
536 }
537
2b4e30fb 538 mlog(0, "Returned from jbd2_journal_init_inode\n");
ccd979bd
MF
539 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
540
541 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
542 OCFS2_JOURNAL_DIRTY_FL);
543
544 journal->j_journal = j_journal;
545 journal->j_inode = inode;
546 journal->j_bh = bh;
547
548 ocfs2_set_journal_params(osb);
549
550 journal->j_state = OCFS2_JOURNAL_LOADED;
551
552 status = 0;
553done:
554 if (status < 0) {
e63aecb6
MF
555 if (inode_lock)
556 ocfs2_inode_unlock(inode, 1);
ccd979bd
MF
557 if (bh != NULL)
558 brelse(bh);
559 if (inode) {
560 OCFS2_I(inode)->ip_open_count--;
561 iput(inode);
562 }
563 }
564
565 mlog_exit(status);
566 return status;
567}
568
539d8264
SM
569static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
570{
571 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
572}
573
574static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
575{
576 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
577}
578
ccd979bd 579static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
539d8264 580 int dirty, int replayed)
ccd979bd
MF
581{
582 int status;
583 unsigned int flags;
584 struct ocfs2_journal *journal = osb->journal;
585 struct buffer_head *bh = journal->j_bh;
586 struct ocfs2_dinode *fe;
587
588 mlog_entry_void();
589
590 fe = (struct ocfs2_dinode *)bh->b_data;
591 if (!OCFS2_IS_VALID_DINODE(fe)) {
592 /* This is called from startup/shutdown which will
593 * handle the errors in a specific manner, so no need
594 * to call ocfs2_error() here. */
b0697053 595 mlog(ML_ERROR, "Journal dinode %llu has invalid "
1ca1a111
MF
596 "signature: %.*s",
597 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
b0697053 598 fe->i_signature);
ccd979bd
MF
599 status = -EIO;
600 goto out;
601 }
602
603 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
604 if (dirty)
605 flags |= OCFS2_JOURNAL_DIRTY_FL;
606 else
607 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
608 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
609
539d8264
SM
610 if (replayed)
611 ocfs2_bump_recovery_generation(fe);
612
ccd979bd
MF
613 status = ocfs2_write_block(osb, bh, journal->j_inode);
614 if (status < 0)
615 mlog_errno(status);
616
617out:
618 mlog_exit(status);
619 return status;
620}
621
622/*
623 * If the journal has been kmalloc'd it needs to be freed after this
624 * call.
625 */
626void ocfs2_journal_shutdown(struct ocfs2_super *osb)
627{
628 struct ocfs2_journal *journal = NULL;
629 int status = 0;
630 struct inode *inode = NULL;
631 int num_running_trans = 0;
632
633 mlog_entry_void();
634
ebdec83b 635 BUG_ON(!osb);
ccd979bd
MF
636
637 journal = osb->journal;
638 if (!journal)
639 goto done;
640
641 inode = journal->j_inode;
642
643 if (journal->j_state != OCFS2_JOURNAL_LOADED)
644 goto done;
645
2b4e30fb 646 /* need to inc inode use count - jbd2_journal_destroy will iput. */
ccd979bd
MF
647 if (!igrab(inode))
648 BUG();
649
650 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
651 if (num_running_trans > 0)
652 mlog(0, "Shutting down journal: must wait on %d "
653 "running transactions!\n",
654 num_running_trans);
655
656 /* Do a commit_cache here. It will flush our journal, *and*
657 * release any locks that are still held.
658 * set the SHUTDOWN flag and release the trans lock.
659 * the commit thread will take the trans lock for us below. */
660 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
661
662 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
663 * drop the trans_lock (which we want to hold until we
664 * completely destroy the journal. */
665 if (osb->commit_task) {
666 /* Wait for the commit thread */
667 mlog(0, "Waiting for ocfs2commit to exit....\n");
668 kthread_stop(osb->commit_task);
669 osb->commit_task = NULL;
670 }
671
672 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
673
c271c5c2 674 if (ocfs2_mount_local(osb)) {
2b4e30fb
JB
675 jbd2_journal_lock_updates(journal->j_journal);
676 status = jbd2_journal_flush(journal->j_journal);
677 jbd2_journal_unlock_updates(journal->j_journal);
c271c5c2
SM
678 if (status < 0)
679 mlog_errno(status);
680 }
681
682 if (status == 0) {
683 /*
684 * Do not toggle if flush was unsuccessful otherwise
685 * will leave dirty metadata in a "clean" journal
686 */
539d8264 687 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
c271c5c2
SM
688 if (status < 0)
689 mlog_errno(status);
690 }
ccd979bd
MF
691
692 /* Shutdown the kernel journal system */
2b4e30fb 693 jbd2_journal_destroy(journal->j_journal);
ccd979bd
MF
694
695 OCFS2_I(inode)->ip_open_count--;
696
697 /* unlock our journal */
e63aecb6 698 ocfs2_inode_unlock(inode, 1);
ccd979bd
MF
699
700 brelse(journal->j_bh);
701 journal->j_bh = NULL;
702
703 journal->j_state = OCFS2_JOURNAL_FREE;
704
705// up_write(&journal->j_trans_barrier);
706done:
707 if (inode)
708 iput(inode);
709 mlog_exit_void();
710}
711
712static void ocfs2_clear_journal_error(struct super_block *sb,
713 journal_t *journal,
714 int slot)
715{
716 int olderr;
717
2b4e30fb 718 olderr = jbd2_journal_errno(journal);
ccd979bd
MF
719 if (olderr) {
720 mlog(ML_ERROR, "File system error %d recorded in "
721 "journal %u.\n", olderr, slot);
722 mlog(ML_ERROR, "File system on device %s needs checking.\n",
723 sb->s_id);
724
2b4e30fb
JB
725 jbd2_journal_ack_err(journal);
726 jbd2_journal_clear_err(journal);
ccd979bd
MF
727 }
728}
729
539d8264 730int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
ccd979bd
MF
731{
732 int status = 0;
733 struct ocfs2_super *osb;
734
735 mlog_entry_void();
736
b1f3550f 737 BUG_ON(!journal);
ccd979bd
MF
738
739 osb = journal->j_osb;
740
2b4e30fb 741 status = jbd2_journal_load(journal->j_journal);
ccd979bd
MF
742 if (status < 0) {
743 mlog(ML_ERROR, "Failed to load journal!\n");
744 goto done;
745 }
746
747 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
748
539d8264 749 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
ccd979bd
MF
750 if (status < 0) {
751 mlog_errno(status);
752 goto done;
753 }
754
755 /* Launch the commit thread */
c271c5c2
SM
756 if (!local) {
757 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
758 "ocfs2cmt");
759 if (IS_ERR(osb->commit_task)) {
760 status = PTR_ERR(osb->commit_task);
761 osb->commit_task = NULL;
762 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
763 "error=%d", status);
764 goto done;
765 }
766 } else
ccd979bd 767 osb->commit_task = NULL;
ccd979bd
MF
768
769done:
770 mlog_exit(status);
771 return status;
772}
773
774
775/* 'full' flag tells us whether we clear out all blocks or if we just
776 * mark the journal clean */
777int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
778{
779 int status;
780
781 mlog_entry_void();
782
ebdec83b 783 BUG_ON(!journal);
ccd979bd 784
2b4e30fb 785 status = jbd2_journal_wipe(journal->j_journal, full);
ccd979bd
MF
786 if (status < 0) {
787 mlog_errno(status);
788 goto bail;
789 }
790
539d8264 791 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
ccd979bd
MF
792 if (status < 0)
793 mlog_errno(status);
794
795bail:
796 mlog_exit(status);
797 return status;
798}
799
553abd04
JB
800static int ocfs2_recovery_completed(struct ocfs2_super *osb)
801{
802 int empty;
803 struct ocfs2_recovery_map *rm = osb->recovery_map;
804
805 spin_lock(&osb->osb_lock);
806 empty = (rm->rm_used == 0);
807 spin_unlock(&osb->osb_lock);
808
809 return empty;
810}
811
812void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
813{
814 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
815}
816
ccd979bd
MF
817/*
818 * JBD Might read a cached version of another nodes journal file. We
819 * don't want this as this file changes often and we get no
820 * notification on those changes. The only way to be sure that we've
821 * got the most up to date version of those blocks then is to force
822 * read them off disk. Just searching through the buffer cache won't
823 * work as there may be pages backing this file which are still marked
824 * up to date. We know things can't change on this file underneath us
825 * as we have the lock by now :)
826 */
827static int ocfs2_force_read_journal(struct inode *inode)
828{
829 int status = 0;
4f902c37 830 int i;
8110b073 831 u64 v_blkno, p_blkno, p_blocks, num_blocks;
4f902c37 832#define CONCURRENT_JOURNAL_FILL 32ULL
ccd979bd
MF
833 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
834
835 mlog_entry_void();
836
ccd979bd
MF
837 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
838
8110b073 839 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
ccd979bd 840 v_blkno = 0;
8110b073 841 while (v_blkno < num_blocks) {
ccd979bd 842 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
49cb8d2d 843 &p_blkno, &p_blocks, NULL);
ccd979bd
MF
844 if (status < 0) {
845 mlog_errno(status);
846 goto bail;
847 }
848
849 if (p_blocks > CONCURRENT_JOURNAL_FILL)
850 p_blocks = CONCURRENT_JOURNAL_FILL;
851
dd4a2c2b
MF
852 /* We are reading journal data which should not
853 * be put in the uptodate cache */
ccd979bd
MF
854 status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
855 p_blkno, p_blocks, bhs, 0,
dd4a2c2b 856 NULL);
ccd979bd
MF
857 if (status < 0) {
858 mlog_errno(status);
859 goto bail;
860 }
861
862 for(i = 0; i < p_blocks; i++) {
863 brelse(bhs[i]);
864 bhs[i] = NULL;
865 }
866
867 v_blkno += p_blocks;
868 }
869
870bail:
871 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
872 if (bhs[i])
873 brelse(bhs[i]);
874 mlog_exit(status);
875 return status;
876}
877
878struct ocfs2_la_recovery_item {
879 struct list_head lri_list;
880 int lri_slot;
881 struct ocfs2_dinode *lri_la_dinode;
882 struct ocfs2_dinode *lri_tl_dinode;
883};
884
885/* Does the second half of the recovery process. By this point, the
886 * node is marked clean and can actually be considered recovered,
887 * hence it's no longer in the recovery map, but there's still some
888 * cleanup we can do which shouldn't happen within the recovery thread
889 * as locking in that context becomes very difficult if we are to take
890 * recovering nodes into account.
891 *
892 * NOTE: This function can and will sleep on recovery of other nodes
893 * during cluster locking, just like any other ocfs2 process.
894 */
c4028958 895void ocfs2_complete_recovery(struct work_struct *work)
ccd979bd
MF
896{
897 int ret;
c4028958
DH
898 struct ocfs2_journal *journal =
899 container_of(work, struct ocfs2_journal, j_recovery_work);
900 struct ocfs2_super *osb = journal->j_osb;
ccd979bd 901 struct ocfs2_dinode *la_dinode, *tl_dinode;
800deef3 902 struct ocfs2_la_recovery_item *item, *n;
ccd979bd
MF
903 LIST_HEAD(tmp_la_list);
904
905 mlog_entry_void();
906
907 mlog(0, "completing recovery from keventd\n");
908
909 spin_lock(&journal->j_lock);
910 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
911 spin_unlock(&journal->j_lock);
912
800deef3 913 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
ccd979bd
MF
914 list_del_init(&item->lri_list);
915
916 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
917
918 la_dinode = item->lri_la_dinode;
919 if (la_dinode) {
b0697053 920 mlog(0, "Clean up local alloc %llu\n",
1ca1a111 921 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
ccd979bd
MF
922
923 ret = ocfs2_complete_local_alloc_recovery(osb,
924 la_dinode);
925 if (ret < 0)
926 mlog_errno(ret);
927
928 kfree(la_dinode);
929 }
930
931 tl_dinode = item->lri_tl_dinode;
932 if (tl_dinode) {
b0697053 933 mlog(0, "Clean up truncate log %llu\n",
1ca1a111 934 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
ccd979bd
MF
935
936 ret = ocfs2_complete_truncate_log_recovery(osb,
937 tl_dinode);
938 if (ret < 0)
939 mlog_errno(ret);
940
941 kfree(tl_dinode);
942 }
943
944 ret = ocfs2_recover_orphans(osb, item->lri_slot);
945 if (ret < 0)
946 mlog_errno(ret);
947
948 kfree(item);
949 }
950
951 mlog(0, "Recovery completion\n");
952 mlog_exit_void();
953}
954
955/* NOTE: This function always eats your references to la_dinode and
956 * tl_dinode, either manually on error, or by passing them to
957 * ocfs2_complete_recovery */
958static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
959 int slot_num,
960 struct ocfs2_dinode *la_dinode,
961 struct ocfs2_dinode *tl_dinode)
962{
963 struct ocfs2_la_recovery_item *item;
964
afae00ab 965 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
ccd979bd
MF
966 if (!item) {
967 /* Though we wish to avoid it, we are in fact safe in
968 * skipping local alloc cleanup as fsck.ocfs2 is more
969 * than capable of reclaiming unused space. */
970 if (la_dinode)
971 kfree(la_dinode);
972
973 if (tl_dinode)
974 kfree(tl_dinode);
975
976 mlog_errno(-ENOMEM);
977 return;
978 }
979
980 INIT_LIST_HEAD(&item->lri_list);
981 item->lri_la_dinode = la_dinode;
982 item->lri_slot = slot_num;
983 item->lri_tl_dinode = tl_dinode;
984
985 spin_lock(&journal->j_lock);
986 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
987 queue_work(ocfs2_wq, &journal->j_recovery_work);
988 spin_unlock(&journal->j_lock);
989}
990
991/* Called by the mount code to queue recovery the last part of
992 * recovery for it's own slot. */
993void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
994{
995 struct ocfs2_journal *journal = osb->journal;
996
997 if (osb->dirty) {
998 /* No need to queue up our truncate_log as regular
999 * cleanup will catch that. */
1000 ocfs2_queue_recovery_completion(journal,
1001 osb->slot_num,
1002 osb->local_alloc_copy,
1003 NULL);
1004 ocfs2_schedule_truncate_log_flush(osb, 0);
1005
1006 osb->local_alloc_copy = NULL;
1007 osb->dirty = 0;
1008 }
1009}
1010
1011static int __ocfs2_recovery_thread(void *arg)
1012{
1013 int status, node_num;
1014 struct ocfs2_super *osb = arg;
553abd04 1015 struct ocfs2_recovery_map *rm = osb->recovery_map;
ccd979bd
MF
1016
1017 mlog_entry_void();
1018
1019 status = ocfs2_wait_on_mount(osb);
1020 if (status < 0) {
1021 goto bail;
1022 }
1023
1024restart:
1025 status = ocfs2_super_lock(osb, 1);
1026 if (status < 0) {
1027 mlog_errno(status);
1028 goto bail;
1029 }
1030
553abd04
JB
1031 spin_lock(&osb->osb_lock);
1032 while (rm->rm_used) {
1033 /* It's always safe to remove entry zero, as we won't
1034 * clear it until ocfs2_recover_node() has succeeded. */
1035 node_num = rm->rm_entries[0];
1036 spin_unlock(&osb->osb_lock);
ccd979bd
MF
1037
1038 status = ocfs2_recover_node(osb, node_num);
553abd04
JB
1039 if (!status) {
1040 ocfs2_recovery_map_clear(osb, node_num);
1041 } else {
ccd979bd
MF
1042 mlog(ML_ERROR,
1043 "Error %d recovering node %d on device (%u,%u)!\n",
1044 status, node_num,
1045 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1046 mlog(ML_ERROR, "Volume requires unmount.\n");
ccd979bd
MF
1047 }
1048
553abd04 1049 spin_lock(&osb->osb_lock);
ccd979bd 1050 }
553abd04
JB
1051 spin_unlock(&osb->osb_lock);
1052 mlog(0, "All nodes recovered\n");
1053
539d8264
SM
1054 /* Refresh all journal recovery generations from disk */
1055 status = ocfs2_check_journals_nolocks(osb);
1056 status = (status == -EROFS) ? 0 : status;
1057 if (status < 0)
1058 mlog_errno(status);
1059
ccd979bd
MF
1060 ocfs2_super_unlock(osb, 1);
1061
1062 /* We always run recovery on our own orphan dir - the dead
34d024f8
MF
1063 * node(s) may have disallowd a previos inode delete. Re-processing
1064 * is therefore required. */
ccd979bd
MF
1065 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1066 NULL);
1067
1068bail:
c74ec2f7 1069 mutex_lock(&osb->recovery_lock);
553abd04 1070 if (!status && !ocfs2_recovery_completed(osb)) {
c74ec2f7 1071 mutex_unlock(&osb->recovery_lock);
ccd979bd
MF
1072 goto restart;
1073 }
1074
1075 osb->recovery_thread_task = NULL;
1076 mb(); /* sync with ocfs2_recovery_thread_running */
1077 wake_up(&osb->recovery_event);
1078
c74ec2f7 1079 mutex_unlock(&osb->recovery_lock);
ccd979bd
MF
1080
1081 mlog_exit(status);
1082 /* no one is callint kthread_stop() for us so the kthread() api
1083 * requires that we call do_exit(). And it isn't exported, but
1084 * complete_and_exit() seems to be a minimal wrapper around it. */
1085 complete_and_exit(NULL, status);
1086 return status;
1087}
1088
1089void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1090{
1091 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1092 node_num, osb->node_num);
1093
c74ec2f7 1094 mutex_lock(&osb->recovery_lock);
ccd979bd
MF
1095 if (osb->disable_recovery)
1096 goto out;
1097
1098 /* People waiting on recovery will wait on
1099 * the recovery map to empty. */
553abd04
JB
1100 if (ocfs2_recovery_map_set(osb, node_num))
1101 mlog(0, "node %d already in recovery map.\n", node_num);
ccd979bd
MF
1102
1103 mlog(0, "starting recovery thread...\n");
1104
1105 if (osb->recovery_thread_task)
1106 goto out;
1107
1108 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
78427043 1109 "ocfs2rec");
ccd979bd
MF
1110 if (IS_ERR(osb->recovery_thread_task)) {
1111 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1112 osb->recovery_thread_task = NULL;
1113 }
1114
1115out:
c74ec2f7 1116 mutex_unlock(&osb->recovery_lock);
ccd979bd
MF
1117 wake_up(&osb->recovery_event);
1118
1119 mlog_exit_void();
1120}
1121
539d8264
SM
1122static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1123 int slot_num,
1124 struct buffer_head **bh,
1125 struct inode **ret_inode)
1126{
1127 int status = -EACCES;
1128 struct inode *inode = NULL;
1129
1130 BUG_ON(slot_num >= osb->max_slots);
1131
1132 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1133 slot_num);
1134 if (!inode || is_bad_inode(inode)) {
1135 mlog_errno(status);
1136 goto bail;
1137 }
1138 SET_INODE_JOURNAL(inode);
1139
1140 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, bh, 0, inode);
1141 if (status < 0) {
1142 mlog_errno(status);
1143 goto bail;
1144 }
1145
1146 status = 0;
1147
1148bail:
1149 if (inode) {
1150 if (status || !ret_inode)
1151 iput(inode);
1152 else
1153 *ret_inode = inode;
1154 }
1155 return status;
1156}
1157
ccd979bd
MF
1158/* Does the actual journal replay and marks the journal inode as
1159 * clean. Will only replay if the journal inode is marked dirty. */
1160static int ocfs2_replay_journal(struct ocfs2_super *osb,
1161 int node_num,
1162 int slot_num)
1163{
1164 int status;
1165 int got_lock = 0;
1166 unsigned int flags;
1167 struct inode *inode = NULL;
1168 struct ocfs2_dinode *fe;
1169 journal_t *journal = NULL;
1170 struct buffer_head *bh = NULL;
539d8264 1171 u32 slot_reco_gen;
ccd979bd 1172
539d8264
SM
1173 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1174 if (status) {
ccd979bd
MF
1175 mlog_errno(status);
1176 goto done;
1177 }
539d8264
SM
1178
1179 fe = (struct ocfs2_dinode *)bh->b_data;
1180 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1181 brelse(bh);
1182 bh = NULL;
1183
1184 /*
1185 * As the fs recovery is asynchronous, there is a small chance that
1186 * another node mounted (and recovered) the slot before the recovery
1187 * thread could get the lock. To handle that, we dirty read the journal
1188 * inode for that slot to get the recovery generation. If it is
1189 * different than what we expected, the slot has been recovered.
1190 * If not, it needs recovery.
1191 */
1192 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1193 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1194 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1195 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1196 status = -EBUSY;
ccd979bd
MF
1197 goto done;
1198 }
539d8264
SM
1199
1200 /* Continue with recovery as the journal has not yet been recovered */
ccd979bd 1201
e63aecb6 1202 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
ccd979bd 1203 if (status < 0) {
e63aecb6 1204 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
ccd979bd
MF
1205 if (status != -ERESTARTSYS)
1206 mlog(ML_ERROR, "Could not lock journal!\n");
1207 goto done;
1208 }
1209 got_lock = 1;
1210
1211 fe = (struct ocfs2_dinode *) bh->b_data;
1212
1213 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
539d8264 1214 slot_reco_gen = ocfs2_get_recovery_generation(fe);
ccd979bd
MF
1215
1216 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1217 mlog(0, "No recovery required for node %d\n", node_num);
539d8264
SM
1218 /* Refresh recovery generation for the slot */
1219 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
ccd979bd
MF
1220 goto done;
1221 }
1222
1223 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1224 node_num, slot_num,
1225 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1226
1227 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1228
1229 status = ocfs2_force_read_journal(inode);
1230 if (status < 0) {
1231 mlog_errno(status);
1232 goto done;
1233 }
1234
1235 mlog(0, "calling journal_init_inode\n");
2b4e30fb 1236 journal = jbd2_journal_init_inode(inode);
ccd979bd
MF
1237 if (journal == NULL) {
1238 mlog(ML_ERROR, "Linux journal layer error\n");
1239 status = -EIO;
1240 goto done;
1241 }
1242
2b4e30fb 1243 status = jbd2_journal_load(journal);
ccd979bd
MF
1244 if (status < 0) {
1245 mlog_errno(status);
1246 if (!igrab(inode))
1247 BUG();
2b4e30fb 1248 jbd2_journal_destroy(journal);
ccd979bd
MF
1249 goto done;
1250 }
1251
1252 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1253
1254 /* wipe the journal */
1255 mlog(0, "flushing the journal.\n");
2b4e30fb
JB
1256 jbd2_journal_lock_updates(journal);
1257 status = jbd2_journal_flush(journal);
1258 jbd2_journal_unlock_updates(journal);
ccd979bd
MF
1259 if (status < 0)
1260 mlog_errno(status);
1261
1262 /* This will mark the node clean */
1263 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1264 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1265 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1266
539d8264
SM
1267 /* Increment recovery generation to indicate successful recovery */
1268 ocfs2_bump_recovery_generation(fe);
1269 osb->slot_recovery_generations[slot_num] =
1270 ocfs2_get_recovery_generation(fe);
1271
ccd979bd
MF
1272 status = ocfs2_write_block(osb, bh, inode);
1273 if (status < 0)
1274 mlog_errno(status);
1275
1276 if (!igrab(inode))
1277 BUG();
1278
2b4e30fb 1279 jbd2_journal_destroy(journal);
ccd979bd
MF
1280
1281done:
1282 /* drop the lock on this nodes journal */
1283 if (got_lock)
e63aecb6 1284 ocfs2_inode_unlock(inode, 1);
ccd979bd
MF
1285
1286 if (inode)
1287 iput(inode);
1288
1289 if (bh)
1290 brelse(bh);
1291
1292 mlog_exit(status);
1293 return status;
1294}
1295
1296/*
1297 * Do the most important parts of node recovery:
1298 * - Replay it's journal
1299 * - Stamp a clean local allocator file
1300 * - Stamp a clean truncate log
1301 * - Mark the node clean
1302 *
1303 * If this function completes without error, a node in OCFS2 can be
1304 * said to have been safely recovered. As a result, failure during the
1305 * second part of a nodes recovery process (local alloc recovery) is
1306 * far less concerning.
1307 */
1308static int ocfs2_recover_node(struct ocfs2_super *osb,
1309 int node_num)
1310{
1311 int status = 0;
1312 int slot_num;
ccd979bd
MF
1313 struct ocfs2_dinode *la_copy = NULL;
1314 struct ocfs2_dinode *tl_copy = NULL;
1315
1316 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1317 node_num, osb->node_num);
1318
1319 mlog(0, "checking node %d\n", node_num);
1320
1321 /* Should not ever be called to recover ourselves -- in that
1322 * case we should've called ocfs2_journal_load instead. */
ebdec83b 1323 BUG_ON(osb->node_num == node_num);
ccd979bd 1324
d85b20e4
JB
1325 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1326 if (slot_num == -ENOENT) {
ccd979bd
MF
1327 status = 0;
1328 mlog(0, "no slot for this node, so no recovery required.\n");
1329 goto done;
1330 }
1331
1332 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1333
1334 status = ocfs2_replay_journal(osb, node_num, slot_num);
1335 if (status < 0) {
539d8264
SM
1336 if (status == -EBUSY) {
1337 mlog(0, "Skipping recovery for slot %u (node %u) "
1338 "as another node has recovered it\n", slot_num,
1339 node_num);
1340 status = 0;
1341 goto done;
1342 }
ccd979bd
MF
1343 mlog_errno(status);
1344 goto done;
1345 }
1346
1347 /* Stamp a clean local alloc file AFTER recovering the journal... */
1348 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1349 if (status < 0) {
1350 mlog_errno(status);
1351 goto done;
1352 }
1353
1354 /* An error from begin_truncate_log_recovery is not
1355 * serious enough to warrant halting the rest of
1356 * recovery. */
1357 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1358 if (status < 0)
1359 mlog_errno(status);
1360
1361 /* Likewise, this would be a strange but ultimately not so
1362 * harmful place to get an error... */
8e8a4603 1363 status = ocfs2_clear_slot(osb, slot_num);
ccd979bd
MF
1364 if (status < 0)
1365 mlog_errno(status);
1366
1367 /* This will kfree the memory pointed to by la_copy and tl_copy */
1368 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1369 tl_copy);
1370
1371 status = 0;
1372done:
1373
1374 mlog_exit(status);
1375 return status;
1376}
1377
1378/* Test node liveness by trylocking his journal. If we get the lock,
1379 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1380 * still alive (we couldn't get the lock) and < 0 on error. */
1381static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1382 int slot_num)
1383{
1384 int status, flags;
1385 struct inode *inode = NULL;
1386
1387 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1388 slot_num);
1389 if (inode == NULL) {
1390 mlog(ML_ERROR, "access error\n");
1391 status = -EACCES;
1392 goto bail;
1393 }
1394 if (is_bad_inode(inode)) {
1395 mlog(ML_ERROR, "access error (bad inode)\n");
1396 iput(inode);
1397 inode = NULL;
1398 status = -EACCES;
1399 goto bail;
1400 }
1401 SET_INODE_JOURNAL(inode);
1402
1403 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
e63aecb6 1404 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
ccd979bd
MF
1405 if (status < 0) {
1406 if (status != -EAGAIN)
1407 mlog_errno(status);
1408 goto bail;
1409 }
1410
e63aecb6 1411 ocfs2_inode_unlock(inode, 1);
ccd979bd
MF
1412bail:
1413 if (inode)
1414 iput(inode);
1415
1416 return status;
1417}
1418
1419/* Call this underneath ocfs2_super_lock. It also assumes that the
1420 * slot info struct has been updated from disk. */
1421int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1422{
d85b20e4
JB
1423 unsigned int node_num;
1424 int status, i;
a1af7d15 1425 u32 gen;
539d8264
SM
1426 struct buffer_head *bh = NULL;
1427 struct ocfs2_dinode *di;
ccd979bd
MF
1428
1429 /* This is called with the super block cluster lock, so we
1430 * know that the slot map can't change underneath us. */
1431
d85b20e4 1432 for (i = 0; i < osb->max_slots; i++) {
539d8264
SM
1433 /* Read journal inode to get the recovery generation */
1434 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1435 if (status) {
1436 mlog_errno(status);
1437 goto bail;
1438 }
1439 di = (struct ocfs2_dinode *)bh->b_data;
a1af7d15 1440 gen = ocfs2_get_recovery_generation(di);
539d8264
SM
1441 brelse(bh);
1442 bh = NULL;
1443
a1af7d15
MF
1444 spin_lock(&osb->osb_lock);
1445 osb->slot_recovery_generations[i] = gen;
1446
539d8264
SM
1447 mlog(0, "Slot %u recovery generation is %u\n", i,
1448 osb->slot_recovery_generations[i]);
1449
a1af7d15
MF
1450 if (i == osb->slot_num) {
1451 spin_unlock(&osb->osb_lock);
ccd979bd 1452 continue;
a1af7d15 1453 }
d85b20e4
JB
1454
1455 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
a1af7d15
MF
1456 if (status == -ENOENT) {
1457 spin_unlock(&osb->osb_lock);
ccd979bd 1458 continue;
a1af7d15 1459 }
ccd979bd 1460
a1af7d15
MF
1461 if (__ocfs2_recovery_map_test(osb, node_num)) {
1462 spin_unlock(&osb->osb_lock);
ccd979bd 1463 continue;
a1af7d15 1464 }
d85b20e4 1465 spin_unlock(&osb->osb_lock);
ccd979bd
MF
1466
1467 /* Ok, we have a slot occupied by another node which
1468 * is not in the recovery map. We trylock his journal
1469 * file here to test if he's alive. */
1470 status = ocfs2_trylock_journal(osb, i);
1471 if (!status) {
1472 /* Since we're called from mount, we know that
1473 * the recovery thread can't race us on
1474 * setting / checking the recovery bits. */
1475 ocfs2_recovery_thread(osb, node_num);
1476 } else if ((status < 0) && (status != -EAGAIN)) {
1477 mlog_errno(status);
1478 goto bail;
1479 }
ccd979bd 1480 }
ccd979bd
MF
1481
1482 status = 0;
1483bail:
1484 mlog_exit(status);
1485 return status;
1486}
1487
5eae5b96
MF
1488struct ocfs2_orphan_filldir_priv {
1489 struct inode *head;
1490 struct ocfs2_super *osb;
1491};
1492
1493static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1494 loff_t pos, u64 ino, unsigned type)
1495{
1496 struct ocfs2_orphan_filldir_priv *p = priv;
1497 struct inode *iter;
1498
1499 if (name_len == 1 && !strncmp(".", name, 1))
1500 return 0;
1501 if (name_len == 2 && !strncmp("..", name, 2))
1502 return 0;
1503
1504 /* Skip bad inodes so that recovery can continue */
1505 iter = ocfs2_iget(p->osb, ino,
5fa0613e 1506 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
5eae5b96
MF
1507 if (IS_ERR(iter))
1508 return 0;
1509
1510 mlog(0, "queue orphan %llu\n",
1511 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1512 /* No locking is required for the next_orphan queue as there
1513 * is only ever a single process doing orphan recovery. */
1514 OCFS2_I(iter)->ip_next_orphan = p->head;
1515 p->head = iter;
1516
1517 return 0;
1518}
1519
b4df6ed8
MF
1520static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1521 int slot,
1522 struct inode **head)
ccd979bd 1523{
b4df6ed8 1524 int status;
ccd979bd 1525 struct inode *orphan_dir_inode = NULL;
5eae5b96
MF
1526 struct ocfs2_orphan_filldir_priv priv;
1527 loff_t pos = 0;
1528
1529 priv.osb = osb;
1530 priv.head = *head;
ccd979bd
MF
1531
1532 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1533 ORPHAN_DIR_SYSTEM_INODE,
1534 slot);
1535 if (!orphan_dir_inode) {
1536 status = -ENOENT;
1537 mlog_errno(status);
b4df6ed8
MF
1538 return status;
1539 }
ccd979bd 1540
1b1dcc1b 1541 mutex_lock(&orphan_dir_inode->i_mutex);
e63aecb6 1542 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
ccd979bd 1543 if (status < 0) {
ccd979bd
MF
1544 mlog_errno(status);
1545 goto out;
1546 }
ccd979bd 1547
5eae5b96
MF
1548 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1549 ocfs2_orphan_filldir);
1550 if (status) {
1551 mlog_errno(status);
a86370fb 1552 goto out_cluster;
ccd979bd 1553 }
ccd979bd 1554
5eae5b96
MF
1555 *head = priv.head;
1556
a86370fb 1557out_cluster:
e63aecb6 1558 ocfs2_inode_unlock(orphan_dir_inode, 0);
b4df6ed8
MF
1559out:
1560 mutex_unlock(&orphan_dir_inode->i_mutex);
ccd979bd 1561 iput(orphan_dir_inode);
b4df6ed8
MF
1562 return status;
1563}
1564
1565static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1566 int slot)
1567{
1568 int ret;
1569
1570 spin_lock(&osb->osb_lock);
1571 ret = !osb->osb_orphan_wipes[slot];
1572 spin_unlock(&osb->osb_lock);
1573 return ret;
1574}
1575
1576static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1577 int slot)
1578{
1579 spin_lock(&osb->osb_lock);
1580 /* Mark ourselves such that new processes in delete_inode()
1581 * know to quit early. */
1582 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1583 while (osb->osb_orphan_wipes[slot]) {
1584 /* If any processes are already in the middle of an
1585 * orphan wipe on this dir, then we need to wait for
1586 * them. */
1587 spin_unlock(&osb->osb_lock);
1588 wait_event_interruptible(osb->osb_wipe_event,
1589 ocfs2_orphan_recovery_can_continue(osb, slot));
1590 spin_lock(&osb->osb_lock);
1591 }
1592 spin_unlock(&osb->osb_lock);
1593}
1594
1595static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1596 int slot)
1597{
1598 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1599}
1600
1601/*
1602 * Orphan recovery. Each mounted node has it's own orphan dir which we
1603 * must run during recovery. Our strategy here is to build a list of
1604 * the inodes in the orphan dir and iget/iput them. The VFS does
1605 * (most) of the rest of the work.
1606 *
1607 * Orphan recovery can happen at any time, not just mount so we have a
1608 * couple of extra considerations.
1609 *
1610 * - We grab as many inodes as we can under the orphan dir lock -
1611 * doing iget() outside the orphan dir risks getting a reference on
1612 * an invalid inode.
1613 * - We must be sure not to deadlock with other processes on the
1614 * system wanting to run delete_inode(). This can happen when they go
1615 * to lock the orphan dir and the orphan recovery process attempts to
1616 * iget() inside the orphan dir lock. This can be avoided by
1617 * advertising our state to ocfs2_delete_inode().
1618 */
1619static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1620 int slot)
1621{
1622 int ret = 0;
1623 struct inode *inode = NULL;
1624 struct inode *iter;
1625 struct ocfs2_inode_info *oi;
1626
1627 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1628
1629 ocfs2_mark_recovering_orphan_dir(osb, slot);
1630 ret = ocfs2_queue_orphans(osb, slot, &inode);
1631 ocfs2_clear_recovering_orphan_dir(osb, slot);
1632
1633 /* Error here should be noted, but we want to continue with as
1634 * many queued inodes as we've got. */
1635 if (ret)
1636 mlog_errno(ret);
ccd979bd
MF
1637
1638 while (inode) {
1639 oi = OCFS2_I(inode);
b0697053 1640 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
ccd979bd
MF
1641
1642 iter = oi->ip_next_orphan;
1643
1644 spin_lock(&oi->ip_lock);
34d024f8
MF
1645 /* The remote delete code may have set these on the
1646 * assumption that the other node would wipe them
1647 * successfully. If they are still in the node's
1648 * orphan dir, we need to reset that state. */
ccd979bd
MF
1649 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1650
1651 /* Set the proper information to get us going into
1652 * ocfs2_delete_inode. */
1653 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
ccd979bd
MF
1654 spin_unlock(&oi->ip_lock);
1655
1656 iput(inode);
1657
1658 inode = iter;
1659 }
1660
b4df6ed8 1661 return ret;
ccd979bd
MF
1662}
1663
1664static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
1665{
1666 /* This check is good because ocfs2 will wait on our recovery
1667 * thread before changing it to something other than MOUNTED
1668 * or DISABLED. */
1669 wait_event(osb->osb_mount_event,
1670 atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
1671 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1672
1673 /* If there's an error on mount, then we may never get to the
1674 * MOUNTED flag, but this is set right before
1675 * dismount_volume() so we can trust it. */
1676 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1677 mlog(0, "mount error, exiting!\n");
1678 return -EBUSY;
1679 }
1680
1681 return 0;
1682}
1683
1684static int ocfs2_commit_thread(void *arg)
1685{
1686 int status;
1687 struct ocfs2_super *osb = arg;
1688 struct ocfs2_journal *journal = osb->journal;
1689
1690 /* we can trust j_num_trans here because _should_stop() is only set in
1691 * shutdown and nobody other than ourselves should be able to start
1692 * transactions. committing on shutdown might take a few iterations
1693 * as final transactions put deleted inodes on the list */
1694 while (!(kthread_should_stop() &&
1695 atomic_read(&journal->j_num_trans) == 0)) {
1696
745ae8ba
MF
1697 wait_event_interruptible(osb->checkpoint_event,
1698 atomic_read(&journal->j_num_trans)
1699 || kthread_should_stop());
ccd979bd
MF
1700
1701 status = ocfs2_commit_cache(osb);
1702 if (status < 0)
1703 mlog_errno(status);
1704
1705 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1706 mlog(ML_KTHREAD,
1707 "commit_thread: %u transactions pending on "
1708 "shutdown\n",
1709 atomic_read(&journal->j_num_trans));
1710 }
1711 }
1712
1713 return 0;
1714}
1715
539d8264
SM
1716/* Reads all the journal inodes without taking any cluster locks. Used
1717 * for hard readonly access to determine whether any journal requires
1718 * recovery. Also used to refresh the recovery generation numbers after
1719 * a journal has been recovered by another node.
1720 */
ccd979bd
MF
1721int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1722{
1723 int ret = 0;
1724 unsigned int slot;
539d8264 1725 struct buffer_head *di_bh = NULL;
ccd979bd 1726 struct ocfs2_dinode *di;
539d8264 1727 int journal_dirty = 0;
ccd979bd
MF
1728
1729 for(slot = 0; slot < osb->max_slots; slot++) {
539d8264
SM
1730 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1731 if (ret) {
ccd979bd
MF
1732 mlog_errno(ret);
1733 goto out;
1734 }
1735
1736 di = (struct ocfs2_dinode *) di_bh->b_data;
1737
539d8264
SM
1738 osb->slot_recovery_generations[slot] =
1739 ocfs2_get_recovery_generation(di);
1740
ccd979bd
MF
1741 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1742 OCFS2_JOURNAL_DIRTY_FL)
539d8264 1743 journal_dirty = 1;
ccd979bd
MF
1744
1745 brelse(di_bh);
539d8264 1746 di_bh = NULL;
ccd979bd
MF
1747 }
1748
1749out:
539d8264
SM
1750 if (journal_dirty)
1751 ret = -EROFS;
ccd979bd
MF
1752 return ret;
1753}