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