jffs2: Fix corruption when flash erase/write failure
[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 "blockcheck.h"
39 #include "dir.h"
40 #include "dlmglue.h"
41 #include "extent_map.h"
42 #include "heartbeat.h"
43 #include "inode.h"
44 #include "journal.h"
45 #include "localalloc.h"
46 #include "slot_map.h"
47 #include "super.h"
48 #include "sysfile.h"
49 #include "quota.h"
50
51 #include "buffer_head_io.h"
52
53 DEFINE_SPINLOCK(trans_inc_lock);
54
55 static int ocfs2_force_read_journal(struct inode *inode);
56 static int ocfs2_recover_node(struct ocfs2_super *osb,
57                               int node_num, int slot_num);
58 static int __ocfs2_recovery_thread(void *arg);
59 static int ocfs2_commit_cache(struct ocfs2_super *osb);
60 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
61 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
62                                       int dirty, int replayed);
63 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
64                                  int slot_num);
65 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
66                                  int slot);
67 static int ocfs2_commit_thread(void *arg);
68 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
69                                             int slot_num,
70                                             struct ocfs2_dinode *la_dinode,
71                                             struct ocfs2_dinode *tl_dinode,
72                                             struct ocfs2_quota_recovery *qrec);
73
74 static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
75 {
76         return __ocfs2_wait_on_mount(osb, 0);
77 }
78
79 static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
80 {
81         return __ocfs2_wait_on_mount(osb, 1);
82 }
83
84 /*
85  * This replay_map is to track online/offline slots, so we could recover
86  * offline slots during recovery and mount
87  */
88
89 enum ocfs2_replay_state {
90         REPLAY_UNNEEDED = 0,    /* Replay is not needed, so ignore this map */
91         REPLAY_NEEDED,          /* Replay slots marked in rm_replay_slots */
92         REPLAY_DONE             /* Replay was already queued */
93 };
94
95 struct ocfs2_replay_map {
96         unsigned int rm_slots;
97         enum ocfs2_replay_state rm_state;
98         unsigned char rm_replay_slots[0];
99 };
100
101 void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
102 {
103         if (!osb->replay_map)
104                 return;
105
106         /* If we've already queued the replay, we don't have any more to do */
107         if (osb->replay_map->rm_state == REPLAY_DONE)
108                 return;
109
110         osb->replay_map->rm_state = state;
111 }
112
113 int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
114 {
115         struct ocfs2_replay_map *replay_map;
116         int i, node_num;
117
118         /* If replay map is already set, we don't do it again */
119         if (osb->replay_map)
120                 return 0;
121
122         replay_map = kzalloc(sizeof(struct ocfs2_replay_map) +
123                              (osb->max_slots * sizeof(char)), GFP_KERNEL);
124
125         if (!replay_map) {
126                 mlog_errno(-ENOMEM);
127                 return -ENOMEM;
128         }
129
130         spin_lock(&osb->osb_lock);
131
132         replay_map->rm_slots = osb->max_slots;
133         replay_map->rm_state = REPLAY_UNNEEDED;
134
135         /* set rm_replay_slots for offline slot(s) */
136         for (i = 0; i < replay_map->rm_slots; i++) {
137                 if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
138                         replay_map->rm_replay_slots[i] = 1;
139         }
140
141         osb->replay_map = replay_map;
142         spin_unlock(&osb->osb_lock);
143         return 0;
144 }
145
146 void ocfs2_queue_replay_slots(struct ocfs2_super *osb)
147 {
148         struct ocfs2_replay_map *replay_map = osb->replay_map;
149         int i;
150
151         if (!replay_map)
152                 return;
153
154         if (replay_map->rm_state != REPLAY_NEEDED)
155                 return;
156
157         for (i = 0; i < replay_map->rm_slots; i++)
158                 if (replay_map->rm_replay_slots[i])
159                         ocfs2_queue_recovery_completion(osb->journal, i, NULL,
160                                                         NULL, NULL);
161         replay_map->rm_state = REPLAY_DONE;
162 }
163
164 void ocfs2_free_replay_slots(struct ocfs2_super *osb)
165 {
166         struct ocfs2_replay_map *replay_map = osb->replay_map;
167
168         if (!osb->replay_map)
169                 return;
170
171         kfree(replay_map);
172         osb->replay_map = NULL;
173 }
174
175 int ocfs2_recovery_init(struct ocfs2_super *osb)
176 {
177         struct ocfs2_recovery_map *rm;
178
179         mutex_init(&osb->recovery_lock);
180         osb->disable_recovery = 0;
181         osb->recovery_thread_task = NULL;
182         init_waitqueue_head(&osb->recovery_event);
183
184         rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
185                      osb->max_slots * sizeof(unsigned int),
186                      GFP_KERNEL);
187         if (!rm) {
188                 mlog_errno(-ENOMEM);
189                 return -ENOMEM;
190         }
191
192         rm->rm_entries = (unsigned int *)((char *)rm +
193                                           sizeof(struct ocfs2_recovery_map));
194         osb->recovery_map = rm;
195
196         return 0;
197 }
198
199 /* we can't grab the goofy sem lock from inside wait_event, so we use
200  * memory barriers to make sure that we'll see the null task before
201  * being woken up */
202 static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
203 {
204         mb();
205         return osb->recovery_thread_task != NULL;
206 }
207
208 void ocfs2_recovery_exit(struct ocfs2_super *osb)
209 {
210         struct ocfs2_recovery_map *rm;
211
212         /* disable any new recovery threads and wait for any currently
213          * running ones to exit. Do this before setting the vol_state. */
214         mutex_lock(&osb->recovery_lock);
215         osb->disable_recovery = 1;
216         mutex_unlock(&osb->recovery_lock);
217         wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
218
219         /* At this point, we know that no more recovery threads can be
220          * launched, so wait for any recovery completion work to
221          * complete. */
222         flush_workqueue(ocfs2_wq);
223
224         /*
225          * Now that recovery is shut down, and the osb is about to be
226          * freed,  the osb_lock is not taken here.
227          */
228         rm = osb->recovery_map;
229         /* XXX: Should we bug if there are dirty entries? */
230
231         kfree(rm);
232 }
233
234 static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
235                                      unsigned int node_num)
236 {
237         int i;
238         struct ocfs2_recovery_map *rm = osb->recovery_map;
239
240         assert_spin_locked(&osb->osb_lock);
241
242         for (i = 0; i < rm->rm_used; i++) {
243                 if (rm->rm_entries[i] == node_num)
244                         return 1;
245         }
246
247         return 0;
248 }
249
250 /* Behaves like test-and-set.  Returns the previous value */
251 static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
252                                   unsigned int node_num)
253 {
254         struct ocfs2_recovery_map *rm = osb->recovery_map;
255
256         spin_lock(&osb->osb_lock);
257         if (__ocfs2_recovery_map_test(osb, node_num)) {
258                 spin_unlock(&osb->osb_lock);
259                 return 1;
260         }
261
262         /* XXX: Can this be exploited? Not from o2dlm... */
263         BUG_ON(rm->rm_used >= osb->max_slots);
264
265         rm->rm_entries[rm->rm_used] = node_num;
266         rm->rm_used++;
267         spin_unlock(&osb->osb_lock);
268
269         return 0;
270 }
271
272 static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
273                                      unsigned int node_num)
274 {
275         int i;
276         struct ocfs2_recovery_map *rm = osb->recovery_map;
277
278         spin_lock(&osb->osb_lock);
279
280         for (i = 0; i < rm->rm_used; i++) {
281                 if (rm->rm_entries[i] == node_num)
282                         break;
283         }
284
285         if (i < rm->rm_used) {
286                 /* XXX: be careful with the pointer math */
287                 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
288                         (rm->rm_used - i - 1) * sizeof(unsigned int));
289                 rm->rm_used--;
290         }
291
292         spin_unlock(&osb->osb_lock);
293 }
294
295 static int ocfs2_commit_cache(struct ocfs2_super *osb)
296 {
297         int status = 0;
298         unsigned int flushed;
299         unsigned long old_id;
300         struct ocfs2_journal *journal = NULL;
301
302         mlog_entry_void();
303
304         journal = osb->journal;
305
306         /* Flush all pending commits and checkpoint the journal. */
307         down_write(&journal->j_trans_barrier);
308
309         if (atomic_read(&journal->j_num_trans) == 0) {
310                 up_write(&journal->j_trans_barrier);
311                 mlog(0, "No transactions for me to flush!\n");
312                 goto finally;
313         }
314
315         jbd2_journal_lock_updates(journal->j_journal);
316         status = jbd2_journal_flush(journal->j_journal);
317         jbd2_journal_unlock_updates(journal->j_journal);
318         if (status < 0) {
319                 up_write(&journal->j_trans_barrier);
320                 mlog_errno(status);
321                 goto finally;
322         }
323
324         old_id = ocfs2_inc_trans_id(journal);
325
326         flushed = atomic_read(&journal->j_num_trans);
327         atomic_set(&journal->j_num_trans, 0);
328         up_write(&journal->j_trans_barrier);
329
330         mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
331              journal->j_trans_id, flushed);
332
333         ocfs2_wake_downconvert_thread(osb);
334         wake_up(&journal->j_checkpointed);
335 finally:
336         mlog_exit(status);
337         return status;
338 }
339
340 /* pass it NULL and it will allocate a new handle object for you.  If
341  * you pass it a handle however, it may still return error, in which
342  * case it has free'd the passed handle for you. */
343 handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
344 {
345         journal_t *journal = osb->journal->j_journal;
346         handle_t *handle;
347
348         BUG_ON(!osb || !osb->journal->j_journal);
349
350         if (ocfs2_is_hard_readonly(osb))
351                 return ERR_PTR(-EROFS);
352
353         BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
354         BUG_ON(max_buffs <= 0);
355
356         /* Nested transaction? Just return the handle... */
357         if (journal_current_handle())
358                 return jbd2_journal_start(journal, max_buffs);
359
360         down_read(&osb->journal->j_trans_barrier);
361
362         handle = jbd2_journal_start(journal, max_buffs);
363         if (IS_ERR(handle)) {
364                 up_read(&osb->journal->j_trans_barrier);
365
366                 mlog_errno(PTR_ERR(handle));
367
368                 if (is_journal_aborted(journal)) {
369                         ocfs2_abort(osb->sb, "Detected aborted journal");
370                         handle = ERR_PTR(-EROFS);
371                 }
372         } else {
373                 if (!ocfs2_mount_local(osb))
374                         atomic_inc(&(osb->journal->j_num_trans));
375         }
376
377         return handle;
378 }
379
380 int ocfs2_commit_trans(struct ocfs2_super *osb,
381                        handle_t *handle)
382 {
383         int ret, nested;
384         struct ocfs2_journal *journal = osb->journal;
385
386         BUG_ON(!handle);
387
388         nested = handle->h_ref > 1;
389         ret = jbd2_journal_stop(handle);
390         if (ret < 0)
391                 mlog_errno(ret);
392
393         if (!nested)
394                 up_read(&journal->j_trans_barrier);
395
396         return ret;
397 }
398
399 /*
400  * 'nblocks' is what you want to add to the current
401  * transaction. extend_trans will either extend the current handle by
402  * nblocks, or commit it and start a new one with nblocks credits.
403  *
404  * This might call jbd2_journal_restart() which will commit dirty buffers
405  * and then restart the transaction. Before calling
406  * ocfs2_extend_trans(), any changed blocks should have been
407  * dirtied. After calling it, all blocks which need to be changed must
408  * go through another set of journal_access/journal_dirty calls.
409  *
410  * WARNING: This will not release any semaphores or disk locks taken
411  * during the transaction, so make sure they were taken *before*
412  * start_trans or we'll have ordering deadlocks.
413  *
414  * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
415  * good because transaction ids haven't yet been recorded on the
416  * cluster locks associated with this handle.
417  */
418 int ocfs2_extend_trans(handle_t *handle, int nblocks)
419 {
420         int status;
421
422         BUG_ON(!handle);
423         BUG_ON(!nblocks);
424
425         mlog_entry_void();
426
427         mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
428
429 #ifdef CONFIG_OCFS2_DEBUG_FS
430         status = 1;
431 #else
432         status = jbd2_journal_extend(handle, nblocks);
433         if (status < 0) {
434                 mlog_errno(status);
435                 goto bail;
436         }
437 #endif
438
439         if (status > 0) {
440                 mlog(0,
441                      "jbd2_journal_extend failed, trying "
442                      "jbd2_journal_restart\n");
443                 status = jbd2_journal_restart(handle, nblocks);
444                 if (status < 0) {
445                         mlog_errno(status);
446                         goto bail;
447                 }
448         }
449
450         status = 0;
451 bail:
452
453         mlog_exit(status);
454         return status;
455 }
456
457 struct ocfs2_triggers {
458         struct jbd2_buffer_trigger_type ot_triggers;
459         int                             ot_offset;
460 };
461
462 static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
463 {
464         return container_of(triggers, struct ocfs2_triggers, ot_triggers);
465 }
466
467 static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
468                                  struct buffer_head *bh,
469                                  void *data, size_t size)
470 {
471         struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
472
473         /*
474          * We aren't guaranteed to have the superblock here, so we
475          * must unconditionally compute the ecc data.
476          * __ocfs2_journal_access() will only set the triggers if
477          * metaecc is enabled.
478          */
479         ocfs2_block_check_compute(data, size, data + ot->ot_offset);
480 }
481
482 /*
483  * Quota blocks have their own trigger because the struct ocfs2_block_check
484  * offset depends on the blocksize.
485  */
486 static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
487                                  struct buffer_head *bh,
488                                  void *data, size_t size)
489 {
490         struct ocfs2_disk_dqtrailer *dqt =
491                 ocfs2_block_dqtrailer(size, data);
492
493         /*
494          * We aren't guaranteed to have the superblock here, so we
495          * must unconditionally compute the ecc data.
496          * __ocfs2_journal_access() will only set the triggers if
497          * metaecc is enabled.
498          */
499         ocfs2_block_check_compute(data, size, &dqt->dq_check);
500 }
501
502 /*
503  * Directory blocks also have their own trigger because the
504  * struct ocfs2_block_check offset depends on the blocksize.
505  */
506 static void ocfs2_db_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
507                                  struct buffer_head *bh,
508                                  void *data, size_t size)
509 {
510         struct ocfs2_dir_block_trailer *trailer =
511                 ocfs2_dir_trailer_from_size(size, data);
512
513         /*
514          * We aren't guaranteed to have the superblock here, so we
515          * must unconditionally compute the ecc data.
516          * __ocfs2_journal_access() will only set the triggers if
517          * metaecc is enabled.
518          */
519         ocfs2_block_check_compute(data, size, &trailer->db_check);
520 }
521
522 static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
523                                 struct buffer_head *bh)
524 {
525         mlog(ML_ERROR,
526              "ocfs2_abort_trigger called by JBD2.  bh = 0x%lx, "
527              "bh->b_blocknr = %llu\n",
528              (unsigned long)bh,
529              (unsigned long long)bh->b_blocknr);
530
531         /* We aren't guaranteed to have the superblock here - but if we
532          * don't, it'll just crash. */
533         ocfs2_error(bh->b_assoc_map->host->i_sb,
534                     "JBD2 has aborted our journal, ocfs2 cannot continue\n");
535 }
536
537 static struct ocfs2_triggers di_triggers = {
538         .ot_triggers = {
539                 .t_commit = ocfs2_commit_trigger,
540                 .t_abort = ocfs2_abort_trigger,
541         },
542         .ot_offset      = offsetof(struct ocfs2_dinode, i_check),
543 };
544
545 static struct ocfs2_triggers eb_triggers = {
546         .ot_triggers = {
547                 .t_commit = ocfs2_commit_trigger,
548                 .t_abort = ocfs2_abort_trigger,
549         },
550         .ot_offset      = offsetof(struct ocfs2_extent_block, h_check),
551 };
552
553 static struct ocfs2_triggers gd_triggers = {
554         .ot_triggers = {
555                 .t_commit = ocfs2_commit_trigger,
556                 .t_abort = ocfs2_abort_trigger,
557         },
558         .ot_offset      = offsetof(struct ocfs2_group_desc, bg_check),
559 };
560
561 static struct ocfs2_triggers db_triggers = {
562         .ot_triggers = {
563                 .t_commit = ocfs2_db_commit_trigger,
564                 .t_abort = ocfs2_abort_trigger,
565         },
566 };
567
568 static struct ocfs2_triggers xb_triggers = {
569         .ot_triggers = {
570                 .t_commit = ocfs2_commit_trigger,
571                 .t_abort = ocfs2_abort_trigger,
572         },
573         .ot_offset      = offsetof(struct ocfs2_xattr_block, xb_check),
574 };
575
576 static struct ocfs2_triggers dq_triggers = {
577         .ot_triggers = {
578                 .t_commit = ocfs2_dq_commit_trigger,
579                 .t_abort = ocfs2_abort_trigger,
580         },
581 };
582
583 static struct ocfs2_triggers dr_triggers = {
584         .ot_triggers = {
585                 .t_commit = ocfs2_commit_trigger,
586                 .t_abort = ocfs2_abort_trigger,
587         },
588         .ot_offset      = offsetof(struct ocfs2_dx_root_block, dr_check),
589 };
590
591 static struct ocfs2_triggers dl_triggers = {
592         .ot_triggers = {
593                 .t_commit = ocfs2_commit_trigger,
594                 .t_abort = ocfs2_abort_trigger,
595         },
596         .ot_offset      = offsetof(struct ocfs2_dx_leaf, dl_check),
597 };
598
599 static int __ocfs2_journal_access(handle_t *handle,
600                                   struct inode *inode,
601                                   struct buffer_head *bh,
602                                   struct ocfs2_triggers *triggers,
603                                   int type)
604 {
605         int status;
606
607         BUG_ON(!inode);
608         BUG_ON(!handle);
609         BUG_ON(!bh);
610
611         mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
612                    (unsigned long long)bh->b_blocknr, type,
613                    (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
614                    "OCFS2_JOURNAL_ACCESS_CREATE" :
615                    "OCFS2_JOURNAL_ACCESS_WRITE",
616                    bh->b_size);
617
618         /* we can safely remove this assertion after testing. */
619         if (!buffer_uptodate(bh)) {
620                 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
621                 mlog(ML_ERROR, "b_blocknr=%llu\n",
622                      (unsigned long long)bh->b_blocknr);
623                 BUG();
624         }
625
626         /* Set the current transaction information on the inode so
627          * that the locking code knows whether it can drop it's locks
628          * on this inode or not. We're protected from the commit
629          * thread updating the current transaction id until
630          * ocfs2_commit_trans() because ocfs2_start_trans() took
631          * j_trans_barrier for us. */
632         ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
633
634         mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
635         switch (type) {
636         case OCFS2_JOURNAL_ACCESS_CREATE:
637         case OCFS2_JOURNAL_ACCESS_WRITE:
638                 status = jbd2_journal_get_write_access(handle, bh);
639                 break;
640
641         case OCFS2_JOURNAL_ACCESS_UNDO:
642                 status = jbd2_journal_get_undo_access(handle, bh);
643                 break;
644
645         default:
646                 status = -EINVAL;
647                 mlog(ML_ERROR, "Uknown access type!\n");
648         }
649         if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
650                 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
651         mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
652
653         if (status < 0)
654                 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
655                      status, type);
656
657         mlog_exit(status);
658         return status;
659 }
660
661 int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
662                                struct buffer_head *bh, int type)
663 {
664         return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
665                                       type);
666 }
667
668 int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
669                             struct buffer_head *bh, int type)
670 {
671         return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
672                                       type);
673 }
674
675 int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
676                             struct buffer_head *bh, int type)
677 {
678         return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
679                                       type);
680 }
681
682 int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
683                             struct buffer_head *bh, int type)
684 {
685         return __ocfs2_journal_access(handle, inode, bh, &db_triggers,
686                                       type);
687 }
688
689 int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
690                             struct buffer_head *bh, int type)
691 {
692         return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
693                                       type);
694 }
695
696 int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
697                             struct buffer_head *bh, int type)
698 {
699         return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
700                                       type);
701 }
702
703 int ocfs2_journal_access_dr(handle_t *handle, struct inode *inode,
704                             struct buffer_head *bh, int type)
705 {
706         return __ocfs2_journal_access(handle, inode, bh, &dr_triggers,
707                                       type);
708 }
709
710 int ocfs2_journal_access_dl(handle_t *handle, struct inode *inode,
711                             struct buffer_head *bh, int type)
712 {
713         return __ocfs2_journal_access(handle, inode, bh, &dl_triggers,
714                                       type);
715 }
716
717 int ocfs2_journal_access(handle_t *handle, struct inode *inode,
718                          struct buffer_head *bh, int type)
719 {
720         return __ocfs2_journal_access(handle, inode, bh, NULL, type);
721 }
722
723 int ocfs2_journal_dirty(handle_t *handle,
724                         struct buffer_head *bh)
725 {
726         int status;
727
728         mlog_entry("(bh->b_blocknr=%llu)\n",
729                    (unsigned long long)bh->b_blocknr);
730
731         status = jbd2_journal_dirty_metadata(handle, bh);
732         if (status < 0)
733                 mlog(ML_ERROR, "Could not dirty metadata buffer. "
734                      "(bh->b_blocknr=%llu)\n",
735                      (unsigned long long)bh->b_blocknr);
736
737         mlog_exit(status);
738         return status;
739 }
740
741 #define OCFS2_DEFAULT_COMMIT_INTERVAL   (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
742
743 void ocfs2_set_journal_params(struct ocfs2_super *osb)
744 {
745         journal_t *journal = osb->journal->j_journal;
746         unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
747
748         if (osb->osb_commit_interval)
749                 commit_interval = osb->osb_commit_interval;
750
751         spin_lock(&journal->j_state_lock);
752         journal->j_commit_interval = commit_interval;
753         if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
754                 journal->j_flags |= JBD2_BARRIER;
755         else
756                 journal->j_flags &= ~JBD2_BARRIER;
757         spin_unlock(&journal->j_state_lock);
758 }
759
760 int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
761 {
762         int status = -1;
763         struct inode *inode = NULL; /* the journal inode */
764         journal_t *j_journal = NULL;
765         struct ocfs2_dinode *di = NULL;
766         struct buffer_head *bh = NULL;
767         struct ocfs2_super *osb;
768         int inode_lock = 0;
769
770         mlog_entry_void();
771
772         BUG_ON(!journal);
773
774         osb = journal->j_osb;
775
776         /* already have the inode for our journal */
777         inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
778                                             osb->slot_num);
779         if (inode == NULL) {
780                 status = -EACCES;
781                 mlog_errno(status);
782                 goto done;
783         }
784         if (is_bad_inode(inode)) {
785                 mlog(ML_ERROR, "access error (bad inode)\n");
786                 iput(inode);
787                 inode = NULL;
788                 status = -EACCES;
789                 goto done;
790         }
791
792         SET_INODE_JOURNAL(inode);
793         OCFS2_I(inode)->ip_open_count++;
794
795         /* Skip recovery waits here - journal inode metadata never
796          * changes in a live cluster so it can be considered an
797          * exception to the rule. */
798         status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
799         if (status < 0) {
800                 if (status != -ERESTARTSYS)
801                         mlog(ML_ERROR, "Could not get lock on journal!\n");
802                 goto done;
803         }
804
805         inode_lock = 1;
806         di = (struct ocfs2_dinode *)bh->b_data;
807
808         if (inode->i_size <  OCFS2_MIN_JOURNAL_SIZE) {
809                 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
810                      inode->i_size);
811                 status = -EINVAL;
812                 goto done;
813         }
814
815         mlog(0, "inode->i_size = %lld\n", inode->i_size);
816         mlog(0, "inode->i_blocks = %llu\n",
817                         (unsigned long long)inode->i_blocks);
818         mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
819
820         /* call the kernels journal init function now */
821         j_journal = jbd2_journal_init_inode(inode);
822         if (j_journal == NULL) {
823                 mlog(ML_ERROR, "Linux journal layer error\n");
824                 status = -EINVAL;
825                 goto done;
826         }
827
828         mlog(0, "Returned from jbd2_journal_init_inode\n");
829         mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
830
831         *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
832                   OCFS2_JOURNAL_DIRTY_FL);
833
834         journal->j_journal = j_journal;
835         journal->j_inode = inode;
836         journal->j_bh = bh;
837
838         ocfs2_set_journal_params(osb);
839
840         journal->j_state = OCFS2_JOURNAL_LOADED;
841
842         status = 0;
843 done:
844         if (status < 0) {
845                 if (inode_lock)
846                         ocfs2_inode_unlock(inode, 1);
847                 brelse(bh);
848                 if (inode) {
849                         OCFS2_I(inode)->ip_open_count--;
850                         iput(inode);
851                 }
852         }
853
854         mlog_exit(status);
855         return status;
856 }
857
858 static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
859 {
860         le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
861 }
862
863 static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
864 {
865         return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
866 }
867
868 static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
869                                       int dirty, int replayed)
870 {
871         int status;
872         unsigned int flags;
873         struct ocfs2_journal *journal = osb->journal;
874         struct buffer_head *bh = journal->j_bh;
875         struct ocfs2_dinode *fe;
876
877         mlog_entry_void();
878
879         fe = (struct ocfs2_dinode *)bh->b_data;
880
881         /* The journal bh on the osb always comes from ocfs2_journal_init()
882          * and was validated there inside ocfs2_inode_lock_full().  It's a
883          * code bug if we mess it up. */
884         BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
885
886         flags = le32_to_cpu(fe->id1.journal1.ij_flags);
887         if (dirty)
888                 flags |= OCFS2_JOURNAL_DIRTY_FL;
889         else
890                 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
891         fe->id1.journal1.ij_flags = cpu_to_le32(flags);
892
893         if (replayed)
894                 ocfs2_bump_recovery_generation(fe);
895
896         ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
897         status = ocfs2_write_block(osb, bh, journal->j_inode);
898         if (status < 0)
899                 mlog_errno(status);
900
901         mlog_exit(status);
902         return status;
903 }
904
905 /*
906  * If the journal has been kmalloc'd it needs to be freed after this
907  * call.
908  */
909 void ocfs2_journal_shutdown(struct ocfs2_super *osb)
910 {
911         struct ocfs2_journal *journal = NULL;
912         int status = 0;
913         struct inode *inode = NULL;
914         int num_running_trans = 0;
915
916         mlog_entry_void();
917
918         BUG_ON(!osb);
919
920         journal = osb->journal;
921         if (!journal)
922                 goto done;
923
924         inode = journal->j_inode;
925
926         if (journal->j_state != OCFS2_JOURNAL_LOADED)
927                 goto done;
928
929         /* need to inc inode use count - jbd2_journal_destroy will iput. */
930         if (!igrab(inode))
931                 BUG();
932
933         num_running_trans = atomic_read(&(osb->journal->j_num_trans));
934         if (num_running_trans > 0)
935                 mlog(0, "Shutting down journal: must wait on %d "
936                      "running transactions!\n",
937                      num_running_trans);
938
939         /* Do a commit_cache here. It will flush our journal, *and*
940          * release any locks that are still held.
941          * set the SHUTDOWN flag and release the trans lock.
942          * the commit thread will take the trans lock for us below. */
943         journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
944
945         /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
946          * drop the trans_lock (which we want to hold until we
947          * completely destroy the journal. */
948         if (osb->commit_task) {
949                 /* Wait for the commit thread */
950                 mlog(0, "Waiting for ocfs2commit to exit....\n");
951                 kthread_stop(osb->commit_task);
952                 osb->commit_task = NULL;
953         }
954
955         BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
956
957         if (ocfs2_mount_local(osb)) {
958                 jbd2_journal_lock_updates(journal->j_journal);
959                 status = jbd2_journal_flush(journal->j_journal);
960                 jbd2_journal_unlock_updates(journal->j_journal);
961                 if (status < 0)
962                         mlog_errno(status);
963         }
964
965         if (status == 0) {
966                 /*
967                  * Do not toggle if flush was unsuccessful otherwise
968                  * will leave dirty metadata in a "clean" journal
969                  */
970                 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
971                 if (status < 0)
972                         mlog_errno(status);
973         }
974
975         /* Shutdown the kernel journal system */
976         jbd2_journal_destroy(journal->j_journal);
977         journal->j_journal = NULL;
978
979         OCFS2_I(inode)->ip_open_count--;
980
981         /* unlock our journal */
982         ocfs2_inode_unlock(inode, 1);
983
984         brelse(journal->j_bh);
985         journal->j_bh = NULL;
986
987         journal->j_state = OCFS2_JOURNAL_FREE;
988
989 //      up_write(&journal->j_trans_barrier);
990 done:
991         if (inode)
992                 iput(inode);
993         mlog_exit_void();
994 }
995
996 static void ocfs2_clear_journal_error(struct super_block *sb,
997                                       journal_t *journal,
998                                       int slot)
999 {
1000         int olderr;
1001
1002         olderr = jbd2_journal_errno(journal);
1003         if (olderr) {
1004                 mlog(ML_ERROR, "File system error %d recorded in "
1005                      "journal %u.\n", olderr, slot);
1006                 mlog(ML_ERROR, "File system on device %s needs checking.\n",
1007                      sb->s_id);
1008
1009                 jbd2_journal_ack_err(journal);
1010                 jbd2_journal_clear_err(journal);
1011         }
1012 }
1013
1014 int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
1015 {
1016         int status = 0;
1017         struct ocfs2_super *osb;
1018
1019         mlog_entry_void();
1020
1021         BUG_ON(!journal);
1022
1023         osb = journal->j_osb;
1024
1025         status = jbd2_journal_load(journal->j_journal);
1026         if (status < 0) {
1027                 mlog(ML_ERROR, "Failed to load journal!\n");
1028                 goto done;
1029         }
1030
1031         ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
1032
1033         status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
1034         if (status < 0) {
1035                 mlog_errno(status);
1036                 goto done;
1037         }
1038
1039         /* Launch the commit thread */
1040         if (!local) {
1041                 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
1042                                                "ocfs2cmt");
1043                 if (IS_ERR(osb->commit_task)) {
1044                         status = PTR_ERR(osb->commit_task);
1045                         osb->commit_task = NULL;
1046                         mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
1047                              "error=%d", status);
1048                         goto done;
1049                 }
1050         } else
1051                 osb->commit_task = NULL;
1052
1053 done:
1054         mlog_exit(status);
1055         return status;
1056 }
1057
1058
1059 /* 'full' flag tells us whether we clear out all blocks or if we just
1060  * mark the journal clean */
1061 int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
1062 {
1063         int status;
1064
1065         mlog_entry_void();
1066
1067         BUG_ON(!journal);
1068
1069         status = jbd2_journal_wipe(journal->j_journal, full);
1070         if (status < 0) {
1071                 mlog_errno(status);
1072                 goto bail;
1073         }
1074
1075         status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
1076         if (status < 0)
1077                 mlog_errno(status);
1078
1079 bail:
1080         mlog_exit(status);
1081         return status;
1082 }
1083
1084 static int ocfs2_recovery_completed(struct ocfs2_super *osb)
1085 {
1086         int empty;
1087         struct ocfs2_recovery_map *rm = osb->recovery_map;
1088
1089         spin_lock(&osb->osb_lock);
1090         empty = (rm->rm_used == 0);
1091         spin_unlock(&osb->osb_lock);
1092
1093         return empty;
1094 }
1095
1096 void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1097 {
1098         wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1099 }
1100
1101 /*
1102  * JBD Might read a cached version of another nodes journal file. We
1103  * don't want this as this file changes often and we get no
1104  * notification on those changes. The only way to be sure that we've
1105  * got the most up to date version of those blocks then is to force
1106  * read them off disk. Just searching through the buffer cache won't
1107  * work as there may be pages backing this file which are still marked
1108  * up to date. We know things can't change on this file underneath us
1109  * as we have the lock by now :)
1110  */
1111 static int ocfs2_force_read_journal(struct inode *inode)
1112 {
1113         int status = 0;
1114         int i;
1115         u64 v_blkno, p_blkno, p_blocks, num_blocks;
1116 #define CONCURRENT_JOURNAL_FILL 32ULL
1117         struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
1118
1119         mlog_entry_void();
1120
1121         memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
1122
1123         num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
1124         v_blkno = 0;
1125         while (v_blkno < num_blocks) {
1126                 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
1127                                                      &p_blkno, &p_blocks, NULL);
1128                 if (status < 0) {
1129                         mlog_errno(status);
1130                         goto bail;
1131                 }
1132
1133                 if (p_blocks > CONCURRENT_JOURNAL_FILL)
1134                         p_blocks = CONCURRENT_JOURNAL_FILL;
1135
1136                 /* We are reading journal data which should not
1137                  * be put in the uptodate cache */
1138                 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
1139                                                 p_blkno, p_blocks, bhs);
1140                 if (status < 0) {
1141                         mlog_errno(status);
1142                         goto bail;
1143                 }
1144
1145                 for(i = 0; i < p_blocks; i++) {
1146                         brelse(bhs[i]);
1147                         bhs[i] = NULL;
1148                 }
1149
1150                 v_blkno += p_blocks;
1151         }
1152
1153 bail:
1154         for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
1155                 brelse(bhs[i]);
1156         mlog_exit(status);
1157         return status;
1158 }
1159
1160 struct ocfs2_la_recovery_item {
1161         struct list_head        lri_list;
1162         int                     lri_slot;
1163         struct ocfs2_dinode     *lri_la_dinode;
1164         struct ocfs2_dinode     *lri_tl_dinode;
1165         struct ocfs2_quota_recovery *lri_qrec;
1166 };
1167
1168 /* Does the second half of the recovery process. By this point, the
1169  * node is marked clean and can actually be considered recovered,
1170  * hence it's no longer in the recovery map, but there's still some
1171  * cleanup we can do which shouldn't happen within the recovery thread
1172  * as locking in that context becomes very difficult if we are to take
1173  * recovering nodes into account.
1174  *
1175  * NOTE: This function can and will sleep on recovery of other nodes
1176  * during cluster locking, just like any other ocfs2 process.
1177  */
1178 void ocfs2_complete_recovery(struct work_struct *work)
1179 {
1180         int ret;
1181         struct ocfs2_journal *journal =
1182                 container_of(work, struct ocfs2_journal, j_recovery_work);
1183         struct ocfs2_super *osb = journal->j_osb;
1184         struct ocfs2_dinode *la_dinode, *tl_dinode;
1185         struct ocfs2_la_recovery_item *item, *n;
1186         struct ocfs2_quota_recovery *qrec;
1187         LIST_HEAD(tmp_la_list);
1188
1189         mlog_entry_void();
1190
1191         mlog(0, "completing recovery from keventd\n");
1192
1193         spin_lock(&journal->j_lock);
1194         list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1195         spin_unlock(&journal->j_lock);
1196
1197         list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
1198                 list_del_init(&item->lri_list);
1199
1200                 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1201
1202                 ocfs2_wait_on_quotas(osb);
1203
1204                 la_dinode = item->lri_la_dinode;
1205                 if (la_dinode) {
1206                         mlog(0, "Clean up local alloc %llu\n",
1207                              (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
1208
1209                         ret = ocfs2_complete_local_alloc_recovery(osb,
1210                                                                   la_dinode);
1211                         if (ret < 0)
1212                                 mlog_errno(ret);
1213
1214                         kfree(la_dinode);
1215                 }
1216
1217                 tl_dinode = item->lri_tl_dinode;
1218                 if (tl_dinode) {
1219                         mlog(0, "Clean up truncate log %llu\n",
1220                              (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
1221
1222                         ret = ocfs2_complete_truncate_log_recovery(osb,
1223                                                                    tl_dinode);
1224                         if (ret < 0)
1225                                 mlog_errno(ret);
1226
1227                         kfree(tl_dinode);
1228                 }
1229
1230                 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1231                 if (ret < 0)
1232                         mlog_errno(ret);
1233
1234                 qrec = item->lri_qrec;
1235                 if (qrec) {
1236                         mlog(0, "Recovering quota files");
1237                         ret = ocfs2_finish_quota_recovery(osb, qrec,
1238                                                           item->lri_slot);
1239                         if (ret < 0)
1240                                 mlog_errno(ret);
1241                         /* Recovery info is already freed now */
1242                 }
1243
1244                 kfree(item);
1245         }
1246
1247         mlog(0, "Recovery completion\n");
1248         mlog_exit_void();
1249 }
1250
1251 /* NOTE: This function always eats your references to la_dinode and
1252  * tl_dinode, either manually on error, or by passing them to
1253  * ocfs2_complete_recovery */
1254 static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1255                                             int slot_num,
1256                                             struct ocfs2_dinode *la_dinode,
1257                                             struct ocfs2_dinode *tl_dinode,
1258                                             struct ocfs2_quota_recovery *qrec)
1259 {
1260         struct ocfs2_la_recovery_item *item;
1261
1262         item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
1263         if (!item) {
1264                 /* Though we wish to avoid it, we are in fact safe in
1265                  * skipping local alloc cleanup as fsck.ocfs2 is more
1266                  * than capable of reclaiming unused space. */
1267                 if (la_dinode)
1268                         kfree(la_dinode);
1269
1270                 if (tl_dinode)
1271                         kfree(tl_dinode);
1272
1273                 if (qrec)
1274                         ocfs2_free_quota_recovery(qrec);
1275
1276                 mlog_errno(-ENOMEM);
1277                 return;
1278         }
1279
1280         INIT_LIST_HEAD(&item->lri_list);
1281         item->lri_la_dinode = la_dinode;
1282         item->lri_slot = slot_num;
1283         item->lri_tl_dinode = tl_dinode;
1284         item->lri_qrec = qrec;
1285
1286         spin_lock(&journal->j_lock);
1287         list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1288         queue_work(ocfs2_wq, &journal->j_recovery_work);
1289         spin_unlock(&journal->j_lock);
1290 }
1291
1292 /* Called by the mount code to queue recovery the last part of
1293  * recovery for it's own and offline slot(s). */
1294 void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1295 {
1296         struct ocfs2_journal *journal = osb->journal;
1297
1298         /* No need to queue up our truncate_log as regular cleanup will catch
1299          * that */
1300         ocfs2_queue_recovery_completion(journal, osb->slot_num,
1301                                         osb->local_alloc_copy, NULL, NULL);
1302         ocfs2_schedule_truncate_log_flush(osb, 0);
1303
1304         osb->local_alloc_copy = NULL;
1305         osb->dirty = 0;
1306
1307         /* queue to recover orphan slots for all offline slots */
1308         ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1309         ocfs2_queue_replay_slots(osb);
1310         ocfs2_free_replay_slots(osb);
1311 }
1312
1313 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1314 {
1315         if (osb->quota_rec) {
1316                 ocfs2_queue_recovery_completion(osb->journal,
1317                                                 osb->slot_num,
1318                                                 NULL,
1319                                                 NULL,
1320                                                 osb->quota_rec);
1321                 osb->quota_rec = NULL;
1322         }
1323 }
1324
1325 static int __ocfs2_recovery_thread(void *arg)
1326 {
1327         int status, node_num, slot_num;
1328         struct ocfs2_super *osb = arg;
1329         struct ocfs2_recovery_map *rm = osb->recovery_map;
1330         int *rm_quota = NULL;
1331         int rm_quota_used = 0, i;
1332         struct ocfs2_quota_recovery *qrec;
1333
1334         mlog_entry_void();
1335
1336         status = ocfs2_wait_on_mount(osb);
1337         if (status < 0) {
1338                 goto bail;
1339         }
1340
1341         rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1342         if (!rm_quota) {
1343                 status = -ENOMEM;
1344                 goto bail;
1345         }
1346 restart:
1347         status = ocfs2_super_lock(osb, 1);
1348         if (status < 0) {
1349                 mlog_errno(status);
1350                 goto bail;
1351         }
1352
1353         status = ocfs2_compute_replay_slots(osb);
1354         if (status < 0)
1355                 mlog_errno(status);
1356
1357         /* queue recovery for our own slot */
1358         ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1359                                         NULL, NULL);
1360
1361         spin_lock(&osb->osb_lock);
1362         while (rm->rm_used) {
1363                 /* It's always safe to remove entry zero, as we won't
1364                  * clear it until ocfs2_recover_node() has succeeded. */
1365                 node_num = rm->rm_entries[0];
1366                 spin_unlock(&osb->osb_lock);
1367                 mlog(0, "checking node %d\n", node_num);
1368                 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1369                 if (slot_num == -ENOENT) {
1370                         status = 0;
1371                         mlog(0, "no slot for this node, so no recovery"
1372                              "required.\n");
1373                         goto skip_recovery;
1374                 }
1375                 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
1376
1377                 /* It is a bit subtle with quota recovery. We cannot do it
1378                  * immediately because we have to obtain cluster locks from
1379                  * quota files and we also don't want to just skip it because
1380                  * then quota usage would be out of sync until some node takes
1381                  * the slot. So we remember which nodes need quota recovery
1382                  * and when everything else is done, we recover quotas. */
1383                 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1384                 if (i == rm_quota_used)
1385                         rm_quota[rm_quota_used++] = slot_num;
1386
1387                 status = ocfs2_recover_node(osb, node_num, slot_num);
1388 skip_recovery:
1389                 if (!status) {
1390                         ocfs2_recovery_map_clear(osb, node_num);
1391                 } else {
1392                         mlog(ML_ERROR,
1393                              "Error %d recovering node %d on device (%u,%u)!\n",
1394                              status, node_num,
1395                              MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1396                         mlog(ML_ERROR, "Volume requires unmount.\n");
1397                 }
1398
1399                 spin_lock(&osb->osb_lock);
1400         }
1401         spin_unlock(&osb->osb_lock);
1402         mlog(0, "All nodes recovered\n");
1403
1404         /* Refresh all journal recovery generations from disk */
1405         status = ocfs2_check_journals_nolocks(osb);
1406         status = (status == -EROFS) ? 0 : status;
1407         if (status < 0)
1408                 mlog_errno(status);
1409
1410         /* Now it is right time to recover quotas... We have to do this under
1411          * superblock lock so that noone can start using the slot (and crash)
1412          * before we recover it */
1413         for (i = 0; i < rm_quota_used; i++) {
1414                 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1415                 if (IS_ERR(qrec)) {
1416                         status = PTR_ERR(qrec);
1417                         mlog_errno(status);
1418                         continue;
1419                 }
1420                 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1421                                                 NULL, NULL, qrec);
1422         }
1423
1424         ocfs2_super_unlock(osb, 1);
1425
1426         /* queue recovery for offline slots */
1427         ocfs2_queue_replay_slots(osb);
1428
1429 bail:
1430         mutex_lock(&osb->recovery_lock);
1431         if (!status && !ocfs2_recovery_completed(osb)) {
1432                 mutex_unlock(&osb->recovery_lock);
1433                 goto restart;
1434         }
1435
1436         ocfs2_free_replay_slots(osb);
1437         osb->recovery_thread_task = NULL;
1438         mb(); /* sync with ocfs2_recovery_thread_running */
1439         wake_up(&osb->recovery_event);
1440
1441         mutex_unlock(&osb->recovery_lock);
1442
1443         if (rm_quota)
1444                 kfree(rm_quota);
1445
1446         mlog_exit(status);
1447         /* no one is callint kthread_stop() for us so the kthread() api
1448          * requires that we call do_exit().  And it isn't exported, but
1449          * complete_and_exit() seems to be a minimal wrapper around it. */
1450         complete_and_exit(NULL, status);
1451         return status;
1452 }
1453
1454 void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1455 {
1456         mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1457                    node_num, osb->node_num);
1458
1459         mutex_lock(&osb->recovery_lock);
1460         if (osb->disable_recovery)
1461                 goto out;
1462
1463         /* People waiting on recovery will wait on
1464          * the recovery map to empty. */
1465         if (ocfs2_recovery_map_set(osb, node_num))
1466                 mlog(0, "node %d already in recovery map.\n", node_num);
1467
1468         mlog(0, "starting recovery thread...\n");
1469
1470         if (osb->recovery_thread_task)
1471                 goto out;
1472
1473         osb->recovery_thread_task =  kthread_run(__ocfs2_recovery_thread, osb,
1474                                                  "ocfs2rec");
1475         if (IS_ERR(osb->recovery_thread_task)) {
1476                 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1477                 osb->recovery_thread_task = NULL;
1478         }
1479
1480 out:
1481         mutex_unlock(&osb->recovery_lock);
1482         wake_up(&osb->recovery_event);
1483
1484         mlog_exit_void();
1485 }
1486
1487 static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1488                                     int slot_num,
1489                                     struct buffer_head **bh,
1490                                     struct inode **ret_inode)
1491 {
1492         int status = -EACCES;
1493         struct inode *inode = NULL;
1494
1495         BUG_ON(slot_num >= osb->max_slots);
1496
1497         inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1498                                             slot_num);
1499         if (!inode || is_bad_inode(inode)) {
1500                 mlog_errno(status);
1501                 goto bail;
1502         }
1503         SET_INODE_JOURNAL(inode);
1504
1505         status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
1506         if (status < 0) {
1507                 mlog_errno(status);
1508                 goto bail;
1509         }
1510
1511         status = 0;
1512
1513 bail:
1514         if (inode) {
1515                 if (status || !ret_inode)
1516                         iput(inode);
1517                 else
1518                         *ret_inode = inode;
1519         }
1520         return status;
1521 }
1522
1523 /* Does the actual journal replay and marks the journal inode as
1524  * clean. Will only replay if the journal inode is marked dirty. */
1525 static int ocfs2_replay_journal(struct ocfs2_super *osb,
1526                                 int node_num,
1527                                 int slot_num)
1528 {
1529         int status;
1530         int got_lock = 0;
1531         unsigned int flags;
1532         struct inode *inode = NULL;
1533         struct ocfs2_dinode *fe;
1534         journal_t *journal = NULL;
1535         struct buffer_head *bh = NULL;
1536         u32 slot_reco_gen;
1537
1538         status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1539         if (status) {
1540                 mlog_errno(status);
1541                 goto done;
1542         }
1543
1544         fe = (struct ocfs2_dinode *)bh->b_data;
1545         slot_reco_gen = ocfs2_get_recovery_generation(fe);
1546         brelse(bh);
1547         bh = NULL;
1548
1549         /*
1550          * As the fs recovery is asynchronous, there is a small chance that
1551          * another node mounted (and recovered) the slot before the recovery
1552          * thread could get the lock. To handle that, we dirty read the journal
1553          * inode for that slot to get the recovery generation. If it is
1554          * different than what we expected, the slot has been recovered.
1555          * If not, it needs recovery.
1556          */
1557         if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1558                 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1559                      osb->slot_recovery_generations[slot_num], slot_reco_gen);
1560                 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1561                 status = -EBUSY;
1562                 goto done;
1563         }
1564
1565         /* Continue with recovery as the journal has not yet been recovered */
1566
1567         status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
1568         if (status < 0) {
1569                 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
1570                 if (status != -ERESTARTSYS)
1571                         mlog(ML_ERROR, "Could not lock journal!\n");
1572                 goto done;
1573         }
1574         got_lock = 1;
1575
1576         fe = (struct ocfs2_dinode *) bh->b_data;
1577
1578         flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1579         slot_reco_gen = ocfs2_get_recovery_generation(fe);
1580
1581         if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1582                 mlog(0, "No recovery required for node %d\n", node_num);
1583                 /* Refresh recovery generation for the slot */
1584                 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1585                 goto done;
1586         }
1587
1588         /* we need to run complete recovery for offline orphan slots */
1589         ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1590
1591         mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1592              node_num, slot_num,
1593              MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1594
1595         OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1596
1597         status = ocfs2_force_read_journal(inode);
1598         if (status < 0) {
1599                 mlog_errno(status);
1600                 goto done;
1601         }
1602
1603         mlog(0, "calling journal_init_inode\n");
1604         journal = jbd2_journal_init_inode(inode);
1605         if (journal == NULL) {
1606                 mlog(ML_ERROR, "Linux journal layer error\n");
1607                 status = -EIO;
1608                 goto done;
1609         }
1610
1611         status = jbd2_journal_load(journal);
1612         if (status < 0) {
1613                 mlog_errno(status);
1614                 if (!igrab(inode))
1615                         BUG();
1616                 jbd2_journal_destroy(journal);
1617                 goto done;
1618         }
1619
1620         ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1621
1622         /* wipe the journal */
1623         mlog(0, "flushing the journal.\n");
1624         jbd2_journal_lock_updates(journal);
1625         status = jbd2_journal_flush(journal);
1626         jbd2_journal_unlock_updates(journal);
1627         if (status < 0)
1628                 mlog_errno(status);
1629
1630         /* This will mark the node clean */
1631         flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1632         flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1633         fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1634
1635         /* Increment recovery generation to indicate successful recovery */
1636         ocfs2_bump_recovery_generation(fe);
1637         osb->slot_recovery_generations[slot_num] =
1638                                         ocfs2_get_recovery_generation(fe);
1639
1640         ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
1641         status = ocfs2_write_block(osb, bh, inode);
1642         if (status < 0)
1643                 mlog_errno(status);
1644
1645         if (!igrab(inode))
1646                 BUG();
1647
1648         jbd2_journal_destroy(journal);
1649
1650 done:
1651         /* drop the lock on this nodes journal */
1652         if (got_lock)
1653                 ocfs2_inode_unlock(inode, 1);
1654
1655         if (inode)
1656                 iput(inode);
1657
1658         brelse(bh);
1659
1660         mlog_exit(status);
1661         return status;
1662 }
1663
1664 /*
1665  * Do the most important parts of node recovery:
1666  *  - Replay it's journal
1667  *  - Stamp a clean local allocator file
1668  *  - Stamp a clean truncate log
1669  *  - Mark the node clean
1670  *
1671  * If this function completes without error, a node in OCFS2 can be
1672  * said to have been safely recovered. As a result, failure during the
1673  * second part of a nodes recovery process (local alloc recovery) is
1674  * far less concerning.
1675  */
1676 static int ocfs2_recover_node(struct ocfs2_super *osb,
1677                               int node_num, int slot_num)
1678 {
1679         int status = 0;
1680         struct ocfs2_dinode *la_copy = NULL;
1681         struct ocfs2_dinode *tl_copy = NULL;
1682
1683         mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1684                    node_num, slot_num, osb->node_num);
1685
1686         /* Should not ever be called to recover ourselves -- in that
1687          * case we should've called ocfs2_journal_load instead. */
1688         BUG_ON(osb->node_num == node_num);
1689
1690         status = ocfs2_replay_journal(osb, node_num, slot_num);
1691         if (status < 0) {
1692                 if (status == -EBUSY) {
1693                         mlog(0, "Skipping recovery for slot %u (node %u) "
1694                              "as another node has recovered it\n", slot_num,
1695                              node_num);
1696                         status = 0;
1697                         goto done;
1698                 }
1699                 mlog_errno(status);
1700                 goto done;
1701         }
1702
1703         /* Stamp a clean local alloc file AFTER recovering the journal... */
1704         status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1705         if (status < 0) {
1706                 mlog_errno(status);
1707                 goto done;
1708         }
1709
1710         /* An error from begin_truncate_log_recovery is not
1711          * serious enough to warrant halting the rest of
1712          * recovery. */
1713         status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1714         if (status < 0)
1715                 mlog_errno(status);
1716
1717         /* Likewise, this would be a strange but ultimately not so
1718          * harmful place to get an error... */
1719         status = ocfs2_clear_slot(osb, slot_num);
1720         if (status < 0)
1721                 mlog_errno(status);
1722
1723         /* This will kfree the memory pointed to by la_copy and tl_copy */
1724         ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
1725                                         tl_copy, NULL);
1726
1727         status = 0;
1728 done:
1729
1730         mlog_exit(status);
1731         return status;
1732 }
1733
1734 /* Test node liveness by trylocking his journal. If we get the lock,
1735  * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1736  * still alive (we couldn't get the lock) and < 0 on error. */
1737 static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1738                                  int slot_num)
1739 {
1740         int status, flags;
1741         struct inode *inode = NULL;
1742
1743         inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1744                                             slot_num);
1745         if (inode == NULL) {
1746                 mlog(ML_ERROR, "access error\n");
1747                 status = -EACCES;
1748                 goto bail;
1749         }
1750         if (is_bad_inode(inode)) {
1751                 mlog(ML_ERROR, "access error (bad inode)\n");
1752                 iput(inode);
1753                 inode = NULL;
1754                 status = -EACCES;
1755                 goto bail;
1756         }
1757         SET_INODE_JOURNAL(inode);
1758
1759         flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
1760         status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
1761         if (status < 0) {
1762                 if (status != -EAGAIN)
1763                         mlog_errno(status);
1764                 goto bail;
1765         }
1766
1767         ocfs2_inode_unlock(inode, 1);
1768 bail:
1769         if (inode)
1770                 iput(inode);
1771
1772         return status;
1773 }
1774
1775 /* Call this underneath ocfs2_super_lock. It also assumes that the
1776  * slot info struct has been updated from disk. */
1777 int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1778 {
1779         unsigned int node_num;
1780         int status, i;
1781         u32 gen;
1782         struct buffer_head *bh = NULL;
1783         struct ocfs2_dinode *di;
1784
1785         /* This is called with the super block cluster lock, so we
1786          * know that the slot map can't change underneath us. */
1787
1788         for (i = 0; i < osb->max_slots; i++) {
1789                 /* Read journal inode to get the recovery generation */
1790                 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1791                 if (status) {
1792                         mlog_errno(status);
1793                         goto bail;
1794                 }
1795                 di = (struct ocfs2_dinode *)bh->b_data;
1796                 gen = ocfs2_get_recovery_generation(di);
1797                 brelse(bh);
1798                 bh = NULL;
1799
1800                 spin_lock(&osb->osb_lock);
1801                 osb->slot_recovery_generations[i] = gen;
1802
1803                 mlog(0, "Slot %u recovery generation is %u\n", i,
1804                      osb->slot_recovery_generations[i]);
1805
1806                 if (i == osb->slot_num) {
1807                         spin_unlock(&osb->osb_lock);
1808                         continue;
1809                 }
1810
1811                 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
1812                 if (status == -ENOENT) {
1813                         spin_unlock(&osb->osb_lock);
1814                         continue;
1815                 }
1816
1817                 if (__ocfs2_recovery_map_test(osb, node_num)) {
1818                         spin_unlock(&osb->osb_lock);
1819                         continue;
1820                 }
1821                 spin_unlock(&osb->osb_lock);
1822
1823                 /* Ok, we have a slot occupied by another node which
1824                  * is not in the recovery map. We trylock his journal
1825                  * file here to test if he's alive. */
1826                 status = ocfs2_trylock_journal(osb, i);
1827                 if (!status) {
1828                         /* Since we're called from mount, we know that
1829                          * the recovery thread can't race us on
1830                          * setting / checking the recovery bits. */
1831                         ocfs2_recovery_thread(osb, node_num);
1832                 } else if ((status < 0) && (status != -EAGAIN)) {
1833                         mlog_errno(status);
1834                         goto bail;
1835                 }
1836         }
1837
1838         status = 0;
1839 bail:
1840         mlog_exit(status);
1841         return status;
1842 }
1843
1844 struct ocfs2_orphan_filldir_priv {
1845         struct inode            *head;
1846         struct ocfs2_super      *osb;
1847 };
1848
1849 static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1850                                 loff_t pos, u64 ino, unsigned type)
1851 {
1852         struct ocfs2_orphan_filldir_priv *p = priv;
1853         struct inode *iter;
1854
1855         if (name_len == 1 && !strncmp(".", name, 1))
1856                 return 0;
1857         if (name_len == 2 && !strncmp("..", name, 2))
1858                 return 0;
1859
1860         /* Skip bad inodes so that recovery can continue */
1861         iter = ocfs2_iget(p->osb, ino,
1862                           OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
1863         if (IS_ERR(iter))
1864                 return 0;
1865
1866         mlog(0, "queue orphan %llu\n",
1867              (unsigned long long)OCFS2_I(iter)->ip_blkno);
1868         /* No locking is required for the next_orphan queue as there
1869          * is only ever a single process doing orphan recovery. */
1870         OCFS2_I(iter)->ip_next_orphan = p->head;
1871         p->head = iter;
1872
1873         return 0;
1874 }
1875
1876 static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1877                                int slot,
1878                                struct inode **head)
1879 {
1880         int status;
1881         struct inode *orphan_dir_inode = NULL;
1882         struct ocfs2_orphan_filldir_priv priv;
1883         loff_t pos = 0;
1884
1885         priv.osb = osb;
1886         priv.head = *head;
1887
1888         orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1889                                                        ORPHAN_DIR_SYSTEM_INODE,
1890                                                        slot);
1891         if  (!orphan_dir_inode) {
1892                 status = -ENOENT;
1893                 mlog_errno(status);
1894                 return status;
1895         }       
1896
1897         mutex_lock(&orphan_dir_inode->i_mutex);
1898         status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
1899         if (status < 0) {
1900                 mlog_errno(status);
1901                 goto out;
1902         }
1903
1904         status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1905                                    ocfs2_orphan_filldir);
1906         if (status) {
1907                 mlog_errno(status);
1908                 goto out_cluster;
1909         }
1910
1911         *head = priv.head;
1912
1913 out_cluster:
1914         ocfs2_inode_unlock(orphan_dir_inode, 0);
1915 out:
1916         mutex_unlock(&orphan_dir_inode->i_mutex);
1917         iput(orphan_dir_inode);
1918         return status;
1919 }
1920
1921 static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1922                                               int slot)
1923 {
1924         int ret;
1925
1926         spin_lock(&osb->osb_lock);
1927         ret = !osb->osb_orphan_wipes[slot];
1928         spin_unlock(&osb->osb_lock);
1929         return ret;
1930 }
1931
1932 static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1933                                              int slot)
1934 {
1935         spin_lock(&osb->osb_lock);
1936         /* Mark ourselves such that new processes in delete_inode()
1937          * know to quit early. */
1938         ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1939         while (osb->osb_orphan_wipes[slot]) {
1940                 /* If any processes are already in the middle of an
1941                  * orphan wipe on this dir, then we need to wait for
1942                  * them. */
1943                 spin_unlock(&osb->osb_lock);
1944                 wait_event_interruptible(osb->osb_wipe_event,
1945                                          ocfs2_orphan_recovery_can_continue(osb, slot));
1946                 spin_lock(&osb->osb_lock);
1947         }
1948         spin_unlock(&osb->osb_lock);
1949 }
1950
1951 static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1952                                               int slot)
1953 {
1954         ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1955 }
1956
1957 /*
1958  * Orphan recovery. Each mounted node has it's own orphan dir which we
1959  * must run during recovery. Our strategy here is to build a list of
1960  * the inodes in the orphan dir and iget/iput them. The VFS does
1961  * (most) of the rest of the work.
1962  *
1963  * Orphan recovery can happen at any time, not just mount so we have a
1964  * couple of extra considerations.
1965  *
1966  * - We grab as many inodes as we can under the orphan dir lock -
1967  *   doing iget() outside the orphan dir risks getting a reference on
1968  *   an invalid inode.
1969  * - We must be sure not to deadlock with other processes on the
1970  *   system wanting to run delete_inode(). This can happen when they go
1971  *   to lock the orphan dir and the orphan recovery process attempts to
1972  *   iget() inside the orphan dir lock. This can be avoided by
1973  *   advertising our state to ocfs2_delete_inode().
1974  */
1975 static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1976                                  int slot)
1977 {
1978         int ret = 0;
1979         struct inode *inode = NULL;
1980         struct inode *iter;
1981         struct ocfs2_inode_info *oi;
1982
1983         mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1984
1985         ocfs2_mark_recovering_orphan_dir(osb, slot);
1986         ret = ocfs2_queue_orphans(osb, slot, &inode);
1987         ocfs2_clear_recovering_orphan_dir(osb, slot);
1988
1989         /* Error here should be noted, but we want to continue with as
1990          * many queued inodes as we've got. */
1991         if (ret)
1992                 mlog_errno(ret);
1993
1994         while (inode) {
1995                 oi = OCFS2_I(inode);
1996                 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
1997
1998                 iter = oi->ip_next_orphan;
1999
2000                 spin_lock(&oi->ip_lock);
2001                 /* The remote delete code may have set these on the
2002                  * assumption that the other node would wipe them
2003                  * successfully.  If they are still in the node's
2004                  * orphan dir, we need to reset that state. */
2005                 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
2006
2007                 /* Set the proper information to get us going into
2008                  * ocfs2_delete_inode. */
2009                 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2010                 spin_unlock(&oi->ip_lock);
2011
2012                 iput(inode);
2013
2014                 inode = iter;
2015         }
2016
2017         return ret;
2018 }
2019
2020 static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
2021 {
2022         /* This check is good because ocfs2 will wait on our recovery
2023          * thread before changing it to something other than MOUNTED
2024          * or DISABLED. */
2025         wait_event(osb->osb_mount_event,
2026                   (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
2027                    atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
2028                    atomic_read(&osb->vol_state) == VOLUME_DISABLED);
2029
2030         /* If there's an error on mount, then we may never get to the
2031          * MOUNTED flag, but this is set right before
2032          * dismount_volume() so we can trust it. */
2033         if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
2034                 mlog(0, "mount error, exiting!\n");
2035                 return -EBUSY;
2036         }
2037
2038         return 0;
2039 }
2040
2041 static int ocfs2_commit_thread(void *arg)
2042 {
2043         int status;
2044         struct ocfs2_super *osb = arg;
2045         struct ocfs2_journal *journal = osb->journal;
2046
2047         /* we can trust j_num_trans here because _should_stop() is only set in
2048          * shutdown and nobody other than ourselves should be able to start
2049          * transactions.  committing on shutdown might take a few iterations
2050          * as final transactions put deleted inodes on the list */
2051         while (!(kthread_should_stop() &&
2052                  atomic_read(&journal->j_num_trans) == 0)) {
2053
2054                 wait_event_interruptible(osb->checkpoint_event,
2055                                          atomic_read(&journal->j_num_trans)
2056                                          || kthread_should_stop());
2057
2058                 status = ocfs2_commit_cache(osb);
2059                 if (status < 0)
2060                         mlog_errno(status);
2061
2062                 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
2063                         mlog(ML_KTHREAD,
2064                              "commit_thread: %u transactions pending on "
2065                              "shutdown\n",
2066                              atomic_read(&journal->j_num_trans));
2067                 }
2068         }
2069
2070         return 0;
2071 }
2072
2073 /* Reads all the journal inodes without taking any cluster locks. Used
2074  * for hard readonly access to determine whether any journal requires
2075  * recovery. Also used to refresh the recovery generation numbers after
2076  * a journal has been recovered by another node.
2077  */
2078 int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
2079 {
2080         int ret = 0;
2081         unsigned int slot;
2082         struct buffer_head *di_bh = NULL;
2083         struct ocfs2_dinode *di;
2084         int journal_dirty = 0;
2085
2086         for(slot = 0; slot < osb->max_slots; slot++) {
2087                 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
2088                 if (ret) {
2089                         mlog_errno(ret);
2090                         goto out;
2091                 }
2092
2093                 di = (struct ocfs2_dinode *) di_bh->b_data;
2094
2095                 osb->slot_recovery_generations[slot] =
2096                                         ocfs2_get_recovery_generation(di);
2097
2098                 if (le32_to_cpu(di->id1.journal1.ij_flags) &
2099                     OCFS2_JOURNAL_DIRTY_FL)
2100                         journal_dirty = 1;
2101
2102                 brelse(di_bh);
2103                 di_bh = NULL;
2104         }
2105
2106 out:
2107         if (journal_dirty)
2108                 ret = -EROFS;
2109         return ret;
2110 }