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