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