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