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