Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-block.git] / fs / ocfs2 / journal.c
CommitLineData
328970de 1// SPDX-License-Identifier: GPL-2.0-or-later
fa60ce2c 2/*
ccd979bd
MF
3 * journal.c
4 *
5 * Defines functions of journalling api
6 *
7 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
ccd979bd
MF
8 */
9
10#include <linux/fs.h>
11#include <linux/types.h>
12#include <linux/slab.h>
13#include <linux/highmem.h>
14#include <linux/kthread.h>
83273932
SE
15#include <linux/time.h>
16#include <linux/random.h>
55b465b6 17#include <linux/delay.h>
cff61bbc 18#include <linux/writeback.h>
ccd979bd 19
ccd979bd
MF
20#include <cluster/masklog.h>
21
22#include "ocfs2.h"
23
24#include "alloc.h"
50655ae9 25#include "blockcheck.h"
316f4b9f 26#include "dir.h"
ccd979bd
MF
27#include "dlmglue.h"
28#include "extent_map.h"
29#include "heartbeat.h"
30#include "inode.h"
31#include "journal.h"
32#include "localalloc.h"
ccd979bd
MF
33#include "slot_map.h"
34#include "super.h"
ccd979bd 35#include "sysfile.h"
0cf2f763 36#include "uptodate.h"
2205363d 37#include "quota.h"
ed460cff
JQ
38#include "file.h"
39#include "namei.h"
ccd979bd
MF
40
41#include "buffer_head_io.h"
b4107950 42#include "ocfs2_trace.h"
ccd979bd 43
34af946a 44DEFINE_SPINLOCK(trans_inc_lock);
ccd979bd 45
83273932
SE
46#define ORPHAN_SCAN_SCHEDULE_TIMEOUT 300000
47
ccd979bd
MF
48static int ocfs2_force_read_journal(struct inode *inode);
49static int ocfs2_recover_node(struct ocfs2_super *osb,
2205363d 50 int node_num, int slot_num);
ccd979bd
MF
51static int __ocfs2_recovery_thread(void *arg);
52static int ocfs2_commit_cache(struct ocfs2_super *osb);
19ece546 53static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
ccd979bd 54static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
539d8264 55 int dirty, int replayed);
ccd979bd
MF
56static int ocfs2_trylock_journal(struct ocfs2_super *osb,
57 int slot_num);
58static int ocfs2_recover_orphans(struct ocfs2_super *osb,
ed460cff
JQ
59 int slot,
60 enum ocfs2_orphan_reco_type orphan_reco_type);
ccd979bd 61static int ocfs2_commit_thread(void *arg);
9140db04
SE
62static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
63 int slot_num,
64 struct ocfs2_dinode *la_dinode,
65 struct ocfs2_dinode *tl_dinode,
ed460cff
JQ
66 struct ocfs2_quota_recovery *qrec,
67 enum ocfs2_orphan_reco_type orphan_reco_type);
ccd979bd 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
9140db04
SE
79/*
80 * This replay_map is to track online/offline slots, so we could recover
81 * offline slots during recovery and mount
82 */
83
84enum ocfs2_replay_state {
85 REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */
86 REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */
87 REPLAY_DONE /* Replay was already queued */
88};
89
90struct ocfs2_replay_map {
91 unsigned int rm_slots;
92 enum ocfs2_replay_state rm_state;
a1cfa251 93 unsigned char rm_replay_slots[] __counted_by(rm_slots);
9140db04
SE
94};
95
b519ea6d 96static void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
9140db04
SE
97{
98 if (!osb->replay_map)
99 return;
100
101 /* If we've already queued the replay, we don't have any more to do */
102 if (osb->replay_map->rm_state == REPLAY_DONE)
103 return;
104
105 osb->replay_map->rm_state = state;
106}
107
108int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
109{
110 struct ocfs2_replay_map *replay_map;
111 int i, node_num;
112
113 /* If replay map is already set, we don't do it again */
114 if (osb->replay_map)
115 return 0;
116
d70fa34f
CJ
117 replay_map = kzalloc(struct_size(replay_map, rm_replay_slots,
118 osb->max_slots),
119 GFP_KERNEL);
9140db04
SE
120 if (!replay_map) {
121 mlog_errno(-ENOMEM);
122 return -ENOMEM;
123 }
124
125 spin_lock(&osb->osb_lock);
126
127 replay_map->rm_slots = osb->max_slots;
128 replay_map->rm_state = REPLAY_UNNEEDED;
129
130 /* set rm_replay_slots for offline slot(s) */
131 for (i = 0; i < replay_map->rm_slots; i++) {
132 if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
133 replay_map->rm_replay_slots[i] = 1;
134 }
135
136 osb->replay_map = replay_map;
137 spin_unlock(&osb->osb_lock);
138 return 0;
139}
140
b519ea6d 141static void ocfs2_queue_replay_slots(struct ocfs2_super *osb,
ed460cff 142 enum ocfs2_orphan_reco_type orphan_reco_type)
9140db04
SE
143{
144 struct ocfs2_replay_map *replay_map = osb->replay_map;
145 int i;
146
147 if (!replay_map)
148 return;
149
150 if (replay_map->rm_state != REPLAY_NEEDED)
151 return;
152
153 for (i = 0; i < replay_map->rm_slots; i++)
154 if (replay_map->rm_replay_slots[i])
155 ocfs2_queue_recovery_completion(osb->journal, i, NULL,
ed460cff
JQ
156 NULL, NULL,
157 orphan_reco_type);
9140db04
SE
158 replay_map->rm_state = REPLAY_DONE;
159}
160
ce2fcf15 161void ocfs2_free_replay_slots(struct ocfs2_super *osb)
9140db04
SE
162{
163 struct ocfs2_replay_map *replay_map = osb->replay_map;
164
165 if (!osb->replay_map)
166 return;
167
168 kfree(replay_map);
169 osb->replay_map = NULL;
170}
171
553abd04
JB
172int ocfs2_recovery_init(struct ocfs2_super *osb)
173{
174 struct ocfs2_recovery_map *rm;
175
176 mutex_init(&osb->recovery_lock);
c0fb8308 177 osb->recovery_state = OCFS2_REC_ENABLED;
553abd04
JB
178 osb->recovery_thread_task = NULL;
179 init_waitqueue_head(&osb->recovery_event);
180
cb2273a4 181 rm = kzalloc(struct_size(rm, rm_entries, osb->max_slots),
553abd04
JB
182 GFP_KERNEL);
183 if (!rm) {
184 mlog_errno(-ENOMEM);
185 return -ENOMEM;
186 }
187
553abd04
JB
188 osb->recovery_map = rm;
189
190 return 0;
191}
192
553abd04
JB
193static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
194{
553abd04
JB
195 return osb->recovery_thread_task != NULL;
196}
197
8f947e0f
JK
198static void ocfs2_recovery_disable(struct ocfs2_super *osb,
199 enum ocfs2_recovery_state state)
553abd04 200{
553abd04 201 mutex_lock(&osb->recovery_lock);
8f947e0f
JK
202 /*
203 * If recovery thread is not running, we can directly transition to
204 * final state.
205 */
206 if (!ocfs2_recovery_thread_running(osb)) {
207 osb->recovery_state = state + 1;
208 goto out_lock;
209 }
210 osb->recovery_state = state;
211 /* Wait for recovery thread to acknowledge state transition */
212 wait_event_cmd(osb->recovery_event,
213 !ocfs2_recovery_thread_running(osb) ||
214 osb->recovery_state >= state + 1,
215 mutex_unlock(&osb->recovery_lock),
216 mutex_lock(&osb->recovery_lock));
217out_lock:
553abd04 218 mutex_unlock(&osb->recovery_lock);
553abd04 219
8f947e0f
JK
220 /*
221 * At this point we know that no more recovery work can be queued so
222 * wait for any recovery completion work to complete.
223 */
b918c430
YL
224 if (osb->ocfs2_wq)
225 flush_workqueue(osb->ocfs2_wq);
8f947e0f
JK
226}
227
fcaf3b26
JK
228void ocfs2_recovery_disable_quota(struct ocfs2_super *osb)
229{
230 ocfs2_recovery_disable(osb, OCFS2_REC_QUOTA_WANT_DISABLE);
231}
232
8f947e0f
JK
233void ocfs2_recovery_exit(struct ocfs2_super *osb)
234{
235 struct ocfs2_recovery_map *rm;
236
237 /* disable any new recovery threads and wait for any currently
238 * running ones to exit. Do this before setting the vol_state. */
239 ocfs2_recovery_disable(osb, OCFS2_REC_WANT_DISABLE);
553abd04
JB
240
241 /*
242 * Now that recovery is shut down, and the osb is about to be
243 * freed, the osb_lock is not taken here.
244 */
245 rm = osb->recovery_map;
246 /* XXX: Should we bug if there are dirty entries? */
247
248 kfree(rm);
249}
250
251static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
252 unsigned int node_num)
253{
254 int i;
255 struct ocfs2_recovery_map *rm = osb->recovery_map;
256
257 assert_spin_locked(&osb->osb_lock);
258
259 for (i = 0; i < rm->rm_used; i++) {
260 if (rm->rm_entries[i] == node_num)
261 return 1;
262 }
263
264 return 0;
265}
266
267/* Behaves like test-and-set. Returns the previous value */
268static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
269 unsigned int node_num)
270{
271 struct ocfs2_recovery_map *rm = osb->recovery_map;
272
273 spin_lock(&osb->osb_lock);
274 if (__ocfs2_recovery_map_test(osb, node_num)) {
275 spin_unlock(&osb->osb_lock);
276 return 1;
277 }
278
279 /* XXX: Can this be exploited? Not from o2dlm... */
280 BUG_ON(rm->rm_used >= osb->max_slots);
281
282 rm->rm_entries[rm->rm_used] = node_num;
283 rm->rm_used++;
284 spin_unlock(&osb->osb_lock);
285
286 return 0;
287}
288
289static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
290 unsigned int node_num)
291{
292 int i;
293 struct ocfs2_recovery_map *rm = osb->recovery_map;
294
295 spin_lock(&osb->osb_lock);
296
297 for (i = 0; i < rm->rm_used; i++) {
298 if (rm->rm_entries[i] == node_num)
299 break;
300 }
301
302 if (i < rm->rm_used) {
303 /* XXX: be careful with the pointer math */
304 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
305 (rm->rm_used - i - 1) * sizeof(unsigned int));
306 rm->rm_used--;
307 }
308
309 spin_unlock(&osb->osb_lock);
310}
311
ccd979bd
MF
312static int ocfs2_commit_cache(struct ocfs2_super *osb)
313{
314 int status = 0;
315 unsigned int flushed;
ccd979bd
MF
316 struct ocfs2_journal *journal = NULL;
317
ccd979bd
MF
318 journal = osb->journal;
319
320 /* Flush all pending commits and checkpoint the journal. */
321 down_write(&journal->j_trans_barrier);
322
b4107950
TM
323 flushed = atomic_read(&journal->j_num_trans);
324 trace_ocfs2_commit_cache_begin(flushed);
325 if (flushed == 0) {
ccd979bd 326 up_write(&journal->j_trans_barrier);
ccd979bd
MF
327 goto finally;
328 }
329
2b4e30fb 330 jbd2_journal_lock_updates(journal->j_journal);
01d5d965 331 status = jbd2_journal_flush(journal->j_journal, 0);
2b4e30fb 332 jbd2_journal_unlock_updates(journal->j_journal);
ccd979bd
MF
333 if (status < 0) {
334 up_write(&journal->j_trans_barrier);
335 mlog_errno(status);
336 goto finally;
337 }
338
f9c57ada 339 ocfs2_inc_trans_id(journal);
ccd979bd
MF
340
341 flushed = atomic_read(&journal->j_num_trans);
342 atomic_set(&journal->j_num_trans, 0);
343 up_write(&journal->j_trans_barrier);
344
b4107950 345 trace_ocfs2_commit_cache_end(journal->j_trans_id, flushed);
ccd979bd 346
34d024f8 347 ocfs2_wake_downconvert_thread(osb);
ccd979bd
MF
348 wake_up(&journal->j_checkpointed);
349finally:
ccd979bd
MF
350 return status;
351}
352
1fabe148 353handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
ccd979bd 354{
ccd979bd 355 journal_t *journal = osb->journal->j_journal;
1fabe148 356 handle_t *handle;
ccd979bd 357
ebdec83b 358 BUG_ON(!osb || !osb->journal->j_journal);
ccd979bd 359
65eff9cc
MF
360 if (ocfs2_is_hard_readonly(osb))
361 return ERR_PTR(-EROFS);
ccd979bd
MF
362
363 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
364 BUG_ON(max_buffs <= 0);
365
90e86a63
JK
366 /* Nested transaction? Just return the handle... */
367 if (journal_current_handle())
368 return jbd2_journal_start(journal, max_buffs);
ccd979bd 369
fef6925c
JK
370 sb_start_intwrite(osb->sb);
371
ccd979bd
MF
372 down_read(&osb->journal->j_trans_barrier);
373
2b4e30fb 374 handle = jbd2_journal_start(journal, max_buffs);
1fabe148 375 if (IS_ERR(handle)) {
ccd979bd 376 up_read(&osb->journal->j_trans_barrier);
fef6925c 377 sb_end_intwrite(osb->sb);
ccd979bd 378
1fabe148 379 mlog_errno(PTR_ERR(handle));
ccd979bd
MF
380
381 if (is_journal_aborted(journal)) {
7ecef14a 382 ocfs2_abort(osb->sb, "Detected aborted journal\n");
1fabe148 383 handle = ERR_PTR(-EROFS);
ccd979bd 384 }
c271c5c2
SM
385 } else {
386 if (!ocfs2_mount_local(osb))
387 atomic_inc(&(osb->journal->j_num_trans));
388 }
ccd979bd 389
ccd979bd 390 return handle;
ccd979bd
MF
391}
392
1fabe148
MF
393int ocfs2_commit_trans(struct ocfs2_super *osb,
394 handle_t *handle)
ccd979bd 395{
90e86a63 396 int ret, nested;
02dc1af4 397 struct ocfs2_journal *journal = osb->journal;
ccd979bd
MF
398
399 BUG_ON(!handle);
400
90e86a63 401 nested = handle->h_ref > 1;
2b4e30fb 402 ret = jbd2_journal_stop(handle);
1fabe148
MF
403 if (ret < 0)
404 mlog_errno(ret);
ccd979bd 405
fef6925c 406 if (!nested) {
90e86a63 407 up_read(&journal->j_trans_barrier);
fef6925c
JK
408 sb_end_intwrite(osb->sb);
409 }
ccd979bd 410
1fabe148 411 return ret;
ccd979bd
MF
412}
413
414/*
c901fb00 415 * 'nblocks' is what you want to add to the current transaction.
ccd979bd 416 *
2b4e30fb 417 * This might call jbd2_journal_restart() which will commit dirty buffers
e8aed345
MF
418 * and then restart the transaction. Before calling
419 * ocfs2_extend_trans(), any changed blocks should have been
420 * dirtied. After calling it, all blocks which need to be changed must
421 * go through another set of journal_access/journal_dirty calls.
422 *
ccd979bd
MF
423 * WARNING: This will not release any semaphores or disk locks taken
424 * during the transaction, so make sure they were taken *before*
425 * start_trans or we'll have ordering deadlocks.
426 *
427 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
428 * good because transaction ids haven't yet been recorded on the
429 * cluster locks associated with this handle.
430 */
1fc58146 431int ocfs2_extend_trans(handle_t *handle, int nblocks)
ccd979bd 432{
c901fb00 433 int status, old_nblocks;
ccd979bd
MF
434
435 BUG_ON(!handle);
c901fb00 436 BUG_ON(nblocks < 0);
ccd979bd 437
c901fb00
TM
438 if (!nblocks)
439 return 0;
440
9797a902 441 old_nblocks = jbd2_handle_buffer_credits(handle);
ccd979bd 442
b4107950 443 trace_ocfs2_extend_trans(old_nblocks, nblocks);
ccd979bd 444
e407e397 445#ifdef CONFIG_OCFS2_DEBUG_FS
0879c584
MF
446 status = 1;
447#else
fdc3ef88 448 status = jbd2_journal_extend(handle, nblocks, 0);
ccd979bd
MF
449 if (status < 0) {
450 mlog_errno(status);
451 goto bail;
452 }
0879c584 453#endif
ccd979bd
MF
454
455 if (status > 0) {
b4107950 456 trace_ocfs2_extend_trans_restart(old_nblocks + nblocks);
c901fb00
TM
457 status = jbd2_journal_restart(handle,
458 old_nblocks + nblocks);
ccd979bd 459 if (status < 0) {
ccd979bd
MF
460 mlog_errno(status);
461 goto bail;
462 }
01ddf1e1 463 }
ccd979bd
MF
464
465 status = 0;
466bail:
ccd979bd
MF
467 return status;
468}
469
be346c1a
JK
470/*
471 * Make sure handle has at least 'nblocks' credits available. If it does not
472 * have that many credits available, we will try to extend the handle to have
473 * enough credits. If that fails, we will restart transaction to have enough
474 * credits. Similar notes regarding data consistency and locking implications
475 * as for ocfs2_extend_trans() apply here.
476 */
477int ocfs2_assure_trans_credits(handle_t *handle, int nblocks)
478{
479 int old_nblks = jbd2_handle_buffer_credits(handle);
480
481 trace_ocfs2_assure_trans_credits(old_nblks);
482 if (old_nblks >= nblocks)
483 return 0;
484 return ocfs2_extend_trans(handle, nblocks - old_nblks);
485}
486
2b1e55c3
YL
487/*
488 * If we have fewer than thresh credits, extend by OCFS2_MAX_TRANS_DATA.
489 * If that fails, restart the transaction & regain write access for the
490 * buffer head which is used for metadata modifications.
491 * Taken from Ext4: extend_or_restart_transaction()
492 */
493int ocfs2_allocate_extend_trans(handle_t *handle, int thresh)
494{
495 int status, old_nblks;
496
497 BUG_ON(!handle);
498
9797a902 499 old_nblks = jbd2_handle_buffer_credits(handle);
2b1e55c3
YL
500 trace_ocfs2_allocate_extend_trans(old_nblks, thresh);
501
502 if (old_nblks < thresh)
503 return 0;
504
fdc3ef88 505 status = jbd2_journal_extend(handle, OCFS2_MAX_TRANS_DATA, 0);
2b1e55c3
YL
506 if (status < 0) {
507 mlog_errno(status);
508 goto bail;
509 }
510
511 if (status > 0) {
512 status = jbd2_journal_restart(handle, OCFS2_MAX_TRANS_DATA);
513 if (status < 0)
514 mlog_errno(status);
515 }
516
517bail:
518 return status;
519}
520
50655ae9
JB
521static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
522{
523 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
524}
525
13ceef09 526static void ocfs2_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
50655ae9
JB
527 struct buffer_head *bh,
528 void *data, size_t size)
529{
530 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
531
532 /*
533 * We aren't guaranteed to have the superblock here, so we
534 * must unconditionally compute the ecc data.
535 * __ocfs2_journal_access() will only set the triggers if
536 * metaecc is enabled.
537 */
538 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
539}
540
541/*
542 * Quota blocks have their own trigger because the struct ocfs2_block_check
543 * offset depends on the blocksize.
544 */
13ceef09 545static void ocfs2_dq_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
50655ae9
JB
546 struct buffer_head *bh,
547 void *data, size_t size)
548{
549 struct ocfs2_disk_dqtrailer *dqt =
550 ocfs2_block_dqtrailer(size, data);
551
552 /*
553 * We aren't guaranteed to have the superblock here, so we
554 * must unconditionally compute the ecc data.
555 * __ocfs2_journal_access() will only set the triggers if
556 * metaecc is enabled.
557 */
558 ocfs2_block_check_compute(data, size, &dqt->dq_check);
559}
560
c175a518
JB
561/*
562 * Directory blocks also have their own trigger because the
563 * struct ocfs2_block_check offset depends on the blocksize.
564 */
13ceef09 565static void ocfs2_db_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
c175a518
JB
566 struct buffer_head *bh,
567 void *data, size_t size)
568{
569 struct ocfs2_dir_block_trailer *trailer =
570 ocfs2_dir_trailer_from_size(size, data);
571
572 /*
573 * We aren't guaranteed to have the superblock here, so we
574 * must unconditionally compute the ecc data.
575 * __ocfs2_journal_access() will only set the triggers if
576 * metaecc is enabled.
577 */
578 ocfs2_block_check_compute(data, size, &trailer->db_check);
579}
580
50655ae9
JB
581static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
582 struct buffer_head *bh)
583{
685d03c3
JQ
584 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
585
50655ae9
JB
586 mlog(ML_ERROR,
587 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
588 "bh->b_blocknr = %llu\n",
589 (unsigned long)bh,
590 (unsigned long long)bh->b_blocknr);
591
685d03c3 592 ocfs2_error(ot->sb,
50655ae9
JB
593 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
594}
595
685d03c3
JQ
596static void ocfs2_setup_csum_triggers(struct super_block *sb,
597 enum ocfs2_journal_trigger_type type,
598 struct ocfs2_triggers *ot)
599{
600 BUG_ON(type >= OCFS2_JOURNAL_TRIGGER_COUNT);
c175a518 601
685d03c3
JQ
602 switch (type) {
603 case OCFS2_JTR_DI:
604 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
605 ot->ot_offset = offsetof(struct ocfs2_dinode, i_check);
606 break;
607 case OCFS2_JTR_EB:
608 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
609 ot->ot_offset = offsetof(struct ocfs2_extent_block, h_check);
610 break;
611 case OCFS2_JTR_RB:
612 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
613 ot->ot_offset = offsetof(struct ocfs2_refcount_block, rf_check);
614 break;
615 case OCFS2_JTR_GD:
616 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
617 ot->ot_offset = offsetof(struct ocfs2_group_desc, bg_check);
618 break;
619 case OCFS2_JTR_DB:
620 ot->ot_triggers.t_frozen = ocfs2_db_frozen_trigger;
621 break;
622 case OCFS2_JTR_XB:
623 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
624 ot->ot_offset = offsetof(struct ocfs2_xattr_block, xb_check);
625 break;
626 case OCFS2_JTR_DQ:
627 ot->ot_triggers.t_frozen = ocfs2_dq_frozen_trigger;
628 break;
629 case OCFS2_JTR_DR:
630 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
631 ot->ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check);
632 break;
633 case OCFS2_JTR_DL:
634 ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
635 ot->ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check);
636 break;
637 case OCFS2_JTR_NONE:
638 /* To make compiler happy... */
639 return;
640 }
50655ae9 641
685d03c3
JQ
642 ot->ot_triggers.t_abort = ocfs2_abort_trigger;
643 ot->sb = sb;
644}
50655ae9 645
685d03c3
JQ
646void ocfs2_initialize_journal_triggers(struct super_block *sb,
647 struct ocfs2_triggers triggers[])
648{
649 enum ocfs2_journal_trigger_type type;
9b7895ef 650
685d03c3
JQ
651 for (type = OCFS2_JTR_DI; type < OCFS2_JOURNAL_TRIGGER_COUNT; type++)
652 ocfs2_setup_csum_triggers(sb, type, &triggers[type]);
653}
9b7895ef 654
50655ae9 655static int __ocfs2_journal_access(handle_t *handle,
0cf2f763 656 struct ocfs2_caching_info *ci,
50655ae9
JB
657 struct buffer_head *bh,
658 struct ocfs2_triggers *triggers,
659 int type)
ccd979bd
MF
660{
661 int status;
0cf2f763
JB
662 struct ocfs2_super *osb =
663 OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
ccd979bd 664
0cf2f763 665 BUG_ON(!ci || !ci->ci_ops);
ccd979bd
MF
666 BUG_ON(!handle);
667 BUG_ON(!bh);
ccd979bd 668
b4107950
TM
669 trace_ocfs2_journal_access(
670 (unsigned long long)ocfs2_metadata_cache_owner(ci),
671 (unsigned long long)bh->b_blocknr, type, bh->b_size);
ccd979bd
MF
672
673 /* we can safely remove this assertion after testing. */
674 if (!buffer_uptodate(bh)) {
675 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
d984187e 676 mlog(ML_ERROR, "b_blocknr=%llu, b_state=0x%lx\n",
677 (unsigned long long)bh->b_blocknr, bh->b_state);
acf8fdbe
JQ
678
679 lock_buffer(bh);
680 /*
d984187e 681 * A previous transaction with a couple of buffer heads fail
682 * to checkpoint, so all the bhs are marked as BH_Write_EIO.
683 * For current transaction, the bh is just among those error
684 * bhs which previous transaction handle. We can't just clear
685 * its BH_Write_EIO and reuse directly, since other bhs are
686 * not written to disk yet and that will cause metadata
687 * inconsistency. So we should set fs read-only to avoid
688 * further damage.
acf8fdbe
JQ
689 */
690 if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) {
acf8fdbe 691 unlock_buffer(bh);
d984187e 692 return ocfs2_error(osb->sb, "A previous attempt to "
693 "write this buffer head failed\n");
acf8fdbe
JQ
694 }
695 unlock_buffer(bh);
ccd979bd
MF
696 }
697
0cf2f763 698 /* Set the current transaction information on the ci so
ccd979bd 699 * that the locking code knows whether it can drop it's locks
0cf2f763 700 * on this ci or not. We're protected from the commit
ccd979bd
MF
701 * thread updating the current transaction id until
702 * ocfs2_commit_trans() because ocfs2_start_trans() took
703 * j_trans_barrier for us. */
0cf2f763 704 ocfs2_set_ci_lock_trans(osb->journal, ci);
ccd979bd 705
0cf2f763 706 ocfs2_metadata_cache_io_lock(ci);
ccd979bd
MF
707 switch (type) {
708 case OCFS2_JOURNAL_ACCESS_CREATE:
709 case OCFS2_JOURNAL_ACCESS_WRITE:
2b4e30fb 710 status = jbd2_journal_get_write_access(handle, bh);
ccd979bd
MF
711 break;
712
713 case OCFS2_JOURNAL_ACCESS_UNDO:
2b4e30fb 714 status = jbd2_journal_get_undo_access(handle, bh);
ccd979bd
MF
715 break;
716
717 default:
718 status = -EINVAL;
af901ca1 719 mlog(ML_ERROR, "Unknown access type!\n");
ccd979bd 720 }
0cf2f763 721 if (!status && ocfs2_meta_ecc(osb) && triggers)
50655ae9 722 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
0cf2f763 723 ocfs2_metadata_cache_io_unlock(ci);
ccd979bd
MF
724
725 if (status < 0)
726 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
727 status, type);
728
ccd979bd
MF
729 return status;
730}
731
0cf2f763
JB
732int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
733 struct buffer_head *bh, int type)
50655ae9 734{
685d03c3
JQ
735 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
736
737 return __ocfs2_journal_access(handle, ci, bh,
738 &osb->s_journal_triggers[OCFS2_JTR_DI],
739 type);
50655ae9
JB
740}
741
0cf2f763 742int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
50655ae9
JB
743 struct buffer_head *bh, int type)
744{
685d03c3
JQ
745 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
746
747 return __ocfs2_journal_access(handle, ci, bh,
748 &osb->s_journal_triggers[OCFS2_JTR_EB],
749 type);
50655ae9
JB
750}
751
93c97087
TM
752int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
753 struct buffer_head *bh, int type)
754{
685d03c3
JQ
755 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
756
757 return __ocfs2_journal_access(handle, ci, bh,
758 &osb->s_journal_triggers[OCFS2_JTR_RB],
93c97087
TM
759 type);
760}
761
0cf2f763 762int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
50655ae9
JB
763 struct buffer_head *bh, int type)
764{
685d03c3
JQ
765 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
766
767 return __ocfs2_journal_access(handle, ci, bh,
768 &osb->s_journal_triggers[OCFS2_JTR_GD],
769 type);
50655ae9
JB
770}
771
0cf2f763 772int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
50655ae9
JB
773 struct buffer_head *bh, int type)
774{
685d03c3
JQ
775 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
776
777 return __ocfs2_journal_access(handle, ci, bh,
778 &osb->s_journal_triggers[OCFS2_JTR_DB],
779 type);
50655ae9
JB
780}
781
0cf2f763 782int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
50655ae9
JB
783 struct buffer_head *bh, int type)
784{
685d03c3
JQ
785 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
786
787 return __ocfs2_journal_access(handle, ci, bh,
788 &osb->s_journal_triggers[OCFS2_JTR_XB],
789 type);
50655ae9
JB
790}
791
0cf2f763 792int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
50655ae9
JB
793 struct buffer_head *bh, int type)
794{
685d03c3
JQ
795 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
796
797 return __ocfs2_journal_access(handle, ci, bh,
798 &osb->s_journal_triggers[OCFS2_JTR_DQ],
799 type);
50655ae9
JB
800}
801
0cf2f763 802int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
9b7895ef
MF
803 struct buffer_head *bh, int type)
804{
685d03c3
JQ
805 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
806
807 return __ocfs2_journal_access(handle, ci, bh,
808 &osb->s_journal_triggers[OCFS2_JTR_DR],
809 type);
9b7895ef
MF
810}
811
0cf2f763 812int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
9b7895ef
MF
813 struct buffer_head *bh, int type)
814{
685d03c3
JQ
815 struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
816
817 return __ocfs2_journal_access(handle, ci, bh,
818 &osb->s_journal_triggers[OCFS2_JTR_DL],
819 type);
9b7895ef
MF
820}
821
0cf2f763 822int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
50655ae9
JB
823 struct buffer_head *bh, int type)
824{
0cf2f763 825 return __ocfs2_journal_access(handle, ci, bh, NULL, type);
50655ae9
JB
826}
827
ec20cec7 828void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh)
ccd979bd
MF
829{
830 int status;
831
b4107950 832 trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr);
ccd979bd 833
2b4e30fb 834 status = jbd2_journal_dirty_metadata(handle, bh);
e272e7f0
JQ
835 if (status) {
836 mlog_errno(status);
837 if (!is_handle_aborted(handle)) {
838 journal_t *journal = handle->h_transaction->t_journal;
e272e7f0 839
58f7e1e2
JQ
840 mlog(ML_ERROR, "jbd2_journal_dirty_metadata failed: "
841 "handle type %u started at line %u, credits %u/%u "
842 "errcode %d. Aborting transaction and journal.\n",
843 handle->h_type, handle->h_line_no,
844 handle->h_requested_credits,
845 jbd2_handle_buffer_credits(handle), status);
e272e7f0
JQ
846 handle->h_err = status;
847 jbd2_journal_abort_handle(handle);
848 jbd2_journal_abort(journal, status);
e272e7f0
JQ
849 }
850 }
ccd979bd
MF
851}
852
2b4e30fb 853#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
ccd979bd
MF
854
855void ocfs2_set_journal_params(struct ocfs2_super *osb)
856{
857 journal_t *journal = osb->journal->j_journal;
d147b3d6
MF
858 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
859
860 if (osb->osb_commit_interval)
861 commit_interval = osb->osb_commit_interval;
ccd979bd 862
a931da6a 863 write_lock(&journal->j_state_lock);
d147b3d6 864 journal->j_commit_interval = commit_interval;
ccd979bd 865 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
2b4e30fb 866 journal->j_flags |= JBD2_BARRIER;
ccd979bd 867 else
2b4e30fb 868 journal->j_flags &= ~JBD2_BARRIER;
a931da6a 869 write_unlock(&journal->j_state_lock);
ccd979bd
MF
870}
871
bb20b31d
HZO
872/*
873 * alloc & initialize skeleton for journal structure.
874 * ocfs2_journal_init() will make fs have journal ability.
875 */
876int ocfs2_journal_alloc(struct ocfs2_super *osb)
ccd979bd 877{
bb20b31d
HZO
878 int status = 0;
879 struct ocfs2_journal *journal;
ccd979bd 880
da5e7c87
VV
881 journal = kzalloc(sizeof(struct ocfs2_journal), GFP_KERNEL);
882 if (!journal) {
883 mlog(ML_ERROR, "unable to alloc journal\n");
884 status = -ENOMEM;
bb20b31d 885 goto bail;
da5e7c87
VV
886 }
887 osb->journal = journal;
888 journal->j_osb = osb;
ccd979bd 889
da5e7c87
VV
890 atomic_set(&journal->j_num_trans, 0);
891 init_rwsem(&journal->j_trans_barrier);
892 init_waitqueue_head(&journal->j_checkpointed);
893 spin_lock_init(&journal->j_lock);
894 journal->j_trans_id = 1UL;
895 INIT_LIST_HEAD(&journal->j_la_cleanups);
896 INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
897 journal->j_state = OCFS2_JOURNAL_FREE;
ccd979bd 898
bb20b31d
HZO
899bail:
900 return status;
901}
902
cff61bbc
CH
903static int ocfs2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
904{
905 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
906 struct writeback_control wbc = {
907 .sync_mode = WB_SYNC_ALL,
908 .nr_to_write = mapping->nrpages * 2,
909 .range_start = jinode->i_dirty_start,
910 .range_end = jinode->i_dirty_end,
911 };
912
17c30ee6 913 return filemap_fdatawrite_wbc(mapping, &wbc);
cff61bbc
CH
914}
915
bb20b31d
HZO
916int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty)
917{
918 int status = -1;
919 struct inode *inode = NULL; /* the journal inode */
920 journal_t *j_journal = NULL;
921 struct ocfs2_journal *journal = osb->journal;
922 struct ocfs2_dinode *di = NULL;
923 struct buffer_head *bh = NULL;
924 int inode_lock = 0;
925
926 BUG_ON(!journal);
ccd979bd
MF
927 /* already have the inode for our journal */
928 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
929 osb->slot_num);
930 if (inode == NULL) {
931 status = -EACCES;
932 mlog_errno(status);
933 goto done;
934 }
935 if (is_bad_inode(inode)) {
936 mlog(ML_ERROR, "access error (bad inode)\n");
937 iput(inode);
938 inode = NULL;
939 status = -EACCES;
940 goto done;
941 }
942
943 SET_INODE_JOURNAL(inode);
944 OCFS2_I(inode)->ip_open_count++;
945
6eff5790
MF
946 /* Skip recovery waits here - journal inode metadata never
947 * changes in a live cluster so it can be considered an
948 * exception to the rule. */
e63aecb6 949 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
ccd979bd
MF
950 if (status < 0) {
951 if (status != -ERESTARTSYS)
952 mlog(ML_ERROR, "Could not get lock on journal!\n");
953 goto done;
954 }
955
e63aecb6 956 inode_lock = 1;
ccd979bd
MF
957 di = (struct ocfs2_dinode *)bh->b_data;
958
f17c20dd 959 if (i_size_read(inode) < OCFS2_MIN_JOURNAL_SIZE) {
ccd979bd 960 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
f17c20dd 961 i_size_read(inode));
ccd979bd
MF
962 status = -EINVAL;
963 goto done;
964 }
965
f17c20dd 966 trace_ocfs2_journal_init(i_size_read(inode),
b4107950
TM
967 (unsigned long long)inode->i_blocks,
968 OCFS2_I(inode)->ip_clusters);
ccd979bd
MF
969
970 /* call the kernels journal init function now */
2b4e30fb 971 j_journal = jbd2_journal_init_inode(inode);
8e6cf5fb 972 if (IS_ERR(j_journal)) {
ccd979bd 973 mlog(ML_ERROR, "Linux journal layer error\n");
8e6cf5fb 974 status = PTR_ERR(j_journal);
ccd979bd
MF
975 goto done;
976 }
977
ede7dc7f 978 trace_ocfs2_journal_init_maxlen(j_journal->j_total_len);
ccd979bd
MF
979
980 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
981 OCFS2_JOURNAL_DIRTY_FL);
982
983 journal->j_journal = j_journal;
342af94e 984 journal->j_journal->j_submit_inode_data_buffers =
cff61bbc 985 ocfs2_journal_submit_inode_data_buffers;
342af94e
MFO
986 journal->j_journal->j_finish_inode_data_buffers =
987 jbd2_journal_finish_inode_data_buffers;
ccd979bd
MF
988 journal->j_inode = inode;
989 journal->j_bh = bh;
990
991 ocfs2_set_journal_params(osb);
992
993 journal->j_state = OCFS2_JOURNAL_LOADED;
994
995 status = 0;
996done:
997 if (status < 0) {
e63aecb6
MF
998 if (inode_lock)
999 ocfs2_inode_unlock(inode, 1);
a81cb88b 1000 brelse(bh);
ccd979bd
MF
1001 if (inode) {
1002 OCFS2_I(inode)->ip_open_count--;
1003 iput(inode);
1004 }
1005 }
1006
ccd979bd
MF
1007 return status;
1008}
1009
539d8264
SM
1010static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
1011{
1012 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
1013}
1014
1015static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
1016{
1017 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
1018}
1019
ccd979bd 1020static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
539d8264 1021 int dirty, int replayed)
ccd979bd
MF
1022{
1023 int status;
1024 unsigned int flags;
1025 struct ocfs2_journal *journal = osb->journal;
1026 struct buffer_head *bh = journal->j_bh;
1027 struct ocfs2_dinode *fe;
1028
ccd979bd 1029 fe = (struct ocfs2_dinode *)bh->b_data;
10995aa2
JB
1030
1031 /* The journal bh on the osb always comes from ocfs2_journal_init()
1032 * and was validated there inside ocfs2_inode_lock_full(). It's a
1033 * code bug if we mess it up. */
1034 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bd
MF
1035
1036 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1037 if (dirty)
1038 flags |= OCFS2_JOURNAL_DIRTY_FL;
1039 else
1040 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1041 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1042
539d8264
SM
1043 if (replayed)
1044 ocfs2_bump_recovery_generation(fe);
1045
13723d00 1046 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
8cb471e8 1047 status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode));
ccd979bd
MF
1048 if (status < 0)
1049 mlog_errno(status);
1050
ccd979bd
MF
1051 return status;
1052}
1053
1054/*
1055 * If the journal has been kmalloc'd it needs to be freed after this
1056 * call.
1057 */
1058void ocfs2_journal_shutdown(struct ocfs2_super *osb)
1059{
1060 struct ocfs2_journal *journal = NULL;
1061 int status = 0;
1062 struct inode *inode = NULL;
1063 int num_running_trans = 0;
1064
ebdec83b 1065 BUG_ON(!osb);
ccd979bd
MF
1066
1067 journal = osb->journal;
1068 if (!journal)
1069 goto done;
1070
1071 inode = journal->j_inode;
1072
1073 if (journal->j_state != OCFS2_JOURNAL_LOADED)
1074 goto done;
1075
2b4e30fb 1076 /* need to inc inode use count - jbd2_journal_destroy will iput. */
ccd979bd
MF
1077 if (!igrab(inode))
1078 BUG();
1079
5784d9fc 1080 num_running_trans = atomic_read(&(journal->j_num_trans));
b4107950 1081 trace_ocfs2_journal_shutdown(num_running_trans);
ccd979bd
MF
1082
1083 /* Do a commit_cache here. It will flush our journal, *and*
1084 * release any locks that are still held.
1085 * set the SHUTDOWN flag and release the trans lock.
1086 * the commit thread will take the trans lock for us below. */
1087 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
1088
1089 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
1090 * drop the trans_lock (which we want to hold until we
1091 * completely destroy the journal. */
1092 if (osb->commit_task) {
1093 /* Wait for the commit thread */
b4107950 1094 trace_ocfs2_journal_shutdown_wait(osb->commit_task);
ccd979bd
MF
1095 kthread_stop(osb->commit_task);
1096 osb->commit_task = NULL;
1097 }
1098
5784d9fc 1099 BUG_ON(atomic_read(&(journal->j_num_trans)) != 0);
ccd979bd 1100
5784d9fc
JS
1101 if (ocfs2_mount_local(osb) &&
1102 (journal->j_journal->j_flags & JBD2_LOADED)) {
2b4e30fb 1103 jbd2_journal_lock_updates(journal->j_journal);
01d5d965 1104 status = jbd2_journal_flush(journal->j_journal, 0);
2b4e30fb 1105 jbd2_journal_unlock_updates(journal->j_journal);
c271c5c2
SM
1106 if (status < 0)
1107 mlog_errno(status);
1108 }
1109
d85400af
JB
1110 /* Shutdown the kernel journal system */
1111 if (!jbd2_journal_destroy(journal->j_journal) && !status) {
c271c5c2
SM
1112 /*
1113 * Do not toggle if flush was unsuccessful otherwise
1114 * will leave dirty metadata in a "clean" journal
1115 */
539d8264 1116 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
c271c5c2
SM
1117 if (status < 0)
1118 mlog_errno(status);
1119 }
ae0dff68 1120 journal->j_journal = NULL;
ccd979bd
MF
1121
1122 OCFS2_I(inode)->ip_open_count--;
1123
1124 /* unlock our journal */
e63aecb6 1125 ocfs2_inode_unlock(inode, 1);
ccd979bd
MF
1126
1127 brelse(journal->j_bh);
1128 journal->j_bh = NULL;
1129
1130 journal->j_state = OCFS2_JOURNAL_FREE;
1131
ccd979bd 1132done:
72865d92 1133 iput(inode);
da5e7c87
VV
1134 kfree(journal);
1135 osb->journal = NULL;
ccd979bd
MF
1136}
1137
1138static void ocfs2_clear_journal_error(struct super_block *sb,
1139 journal_t *journal,
1140 int slot)
1141{
1142 int olderr;
1143
2b4e30fb 1144 olderr = jbd2_journal_errno(journal);
ccd979bd
MF
1145 if (olderr) {
1146 mlog(ML_ERROR, "File system error %d recorded in "
1147 "journal %u.\n", olderr, slot);
1148 mlog(ML_ERROR, "File system on device %s needs checking.\n",
1149 sb->s_id);
1150
2b4e30fb
JB
1151 jbd2_journal_ack_err(journal);
1152 jbd2_journal_clear_err(journal);
ccd979bd
MF
1153 }
1154}
1155
539d8264 1156int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
ccd979bd
MF
1157{
1158 int status = 0;
1159 struct ocfs2_super *osb;
1160
b1f3550f 1161 BUG_ON(!journal);
ccd979bd
MF
1162
1163 osb = journal->j_osb;
1164
2b4e30fb 1165 status = jbd2_journal_load(journal->j_journal);
ccd979bd
MF
1166 if (status < 0) {
1167 mlog(ML_ERROR, "Failed to load journal!\n");
1168 goto done;
1169 }
1170
1171 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
1172
397eac17
KL
1173 if (replayed) {
1174 jbd2_journal_lock_updates(journal->j_journal);
01d5d965 1175 status = jbd2_journal_flush(journal->j_journal, 0);
397eac17
KL
1176 jbd2_journal_unlock_updates(journal->j_journal);
1177 if (status < 0)
1178 mlog_errno(status);
1179 }
1180
539d8264 1181 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
ccd979bd
MF
1182 if (status < 0) {
1183 mlog_errno(status);
1184 goto done;
1185 }
1186
1187 /* Launch the commit thread */
c271c5c2
SM
1188 if (!local) {
1189 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
5afc44e2 1190 "ocfs2cmt-%s", osb->uuid_str);
c271c5c2
SM
1191 if (IS_ERR(osb->commit_task)) {
1192 status = PTR_ERR(osb->commit_task);
1193 osb->commit_task = NULL;
1194 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
1195 "error=%d", status);
1196 goto done;
1197 }
1198 } else
ccd979bd 1199 osb->commit_task = NULL;
ccd979bd
MF
1200
1201done:
ccd979bd
MF
1202 return status;
1203}
1204
1205
1206/* 'full' flag tells us whether we clear out all blocks or if we just
1207 * mark the journal clean */
1208int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
1209{
1210 int status;
1211
ebdec83b 1212 BUG_ON(!journal);
ccd979bd 1213
2b4e30fb 1214 status = jbd2_journal_wipe(journal->j_journal, full);
ccd979bd
MF
1215 if (status < 0) {
1216 mlog_errno(status);
1217 goto bail;
1218 }
1219
539d8264 1220 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
ccd979bd
MF
1221 if (status < 0)
1222 mlog_errno(status);
1223
1224bail:
ccd979bd
MF
1225 return status;
1226}
1227
553abd04
JB
1228static int ocfs2_recovery_completed(struct ocfs2_super *osb)
1229{
1230 int empty;
1231 struct ocfs2_recovery_map *rm = osb->recovery_map;
1232
1233 spin_lock(&osb->osb_lock);
1234 empty = (rm->rm_used == 0);
1235 spin_unlock(&osb->osb_lock);
1236
1237 return empty;
1238}
1239
1240void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1241{
1242 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1243}
1244
ccd979bd
MF
1245/*
1246 * JBD Might read a cached version of another nodes journal file. We
1247 * don't want this as this file changes often and we get no
1248 * notification on those changes. The only way to be sure that we've
1249 * got the most up to date version of those blocks then is to force
1250 * read them off disk. Just searching through the buffer cache won't
1251 * work as there may be pages backing this file which are still marked
1252 * up to date. We know things can't change on this file underneath us
1253 * as we have the lock by now :)
1254 */
1255static int ocfs2_force_read_journal(struct inode *inode)
1256{
1257 int status = 0;
4f902c37 1258 int i;
8110b073 1259 u64 v_blkno, p_blkno, p_blocks, num_blocks;
0b492f68
JB
1260 struct buffer_head *bh = NULL;
1261 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
ccd979bd 1262
f17c20dd 1263 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
ccd979bd 1264 v_blkno = 0;
8110b073 1265 while (v_blkno < num_blocks) {
ccd979bd 1266 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
49cb8d2d 1267 &p_blkno, &p_blocks, NULL);
ccd979bd
MF
1268 if (status < 0) {
1269 mlog_errno(status);
1270 goto bail;
1271 }
1272
0b492f68 1273 for (i = 0; i < p_blocks; i++, p_blkno++) {
a0b5ff07 1274 bh = __find_get_block_nonatomic(osb->sb->s_bdev, p_blkno,
0b492f68
JB
1275 osb->sb->s_blocksize);
1276 /* block not cached. */
1277 if (!bh)
1278 continue;
1279
1280 brelse(bh);
1281 bh = NULL;
1282 /* We are reading journal data which should not
1283 * be put in the uptodate cache.
1284 */
1285 status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh);
1286 if (status < 0) {
1287 mlog_errno(status);
1288 goto bail;
1289 }
1290
1291 brelse(bh);
1292 bh = NULL;
ccd979bd
MF
1293 }
1294
1295 v_blkno += p_blocks;
1296 }
1297
1298bail:
ccd979bd
MF
1299 return status;
1300}
1301
1302struct ocfs2_la_recovery_item {
1303 struct list_head lri_list;
1304 int lri_slot;
1305 struct ocfs2_dinode *lri_la_dinode;
1306 struct ocfs2_dinode *lri_tl_dinode;
2205363d 1307 struct ocfs2_quota_recovery *lri_qrec;
ed460cff 1308 enum ocfs2_orphan_reco_type lri_orphan_reco_type;
ccd979bd
MF
1309};
1310
1311/* Does the second half of the recovery process. By this point, the
1312 * node is marked clean and can actually be considered recovered,
1313 * hence it's no longer in the recovery map, but there's still some
1314 * cleanup we can do which shouldn't happen within the recovery thread
1315 * as locking in that context becomes very difficult if we are to take
1316 * recovering nodes into account.
1317 *
1318 * NOTE: This function can and will sleep on recovery of other nodes
1319 * during cluster locking, just like any other ocfs2 process.
1320 */
c4028958 1321void ocfs2_complete_recovery(struct work_struct *work)
ccd979bd 1322{
b4107950 1323 int ret = 0;
c4028958
DH
1324 struct ocfs2_journal *journal =
1325 container_of(work, struct ocfs2_journal, j_recovery_work);
1326 struct ocfs2_super *osb = journal->j_osb;
ccd979bd 1327 struct ocfs2_dinode *la_dinode, *tl_dinode;
800deef3 1328 struct ocfs2_la_recovery_item *item, *n;
2205363d 1329 struct ocfs2_quota_recovery *qrec;
ed460cff 1330 enum ocfs2_orphan_reco_type orphan_reco_type;
ccd979bd
MF
1331 LIST_HEAD(tmp_la_list);
1332
b4107950
TM
1333 trace_ocfs2_complete_recovery(
1334 (unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno);
ccd979bd
MF
1335
1336 spin_lock(&journal->j_lock);
1337 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1338 spin_unlock(&journal->j_lock);
1339
800deef3 1340 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
ccd979bd
MF
1341 list_del_init(&item->lri_list);
1342
19ece546
JK
1343 ocfs2_wait_on_quotas(osb);
1344
ccd979bd 1345 la_dinode = item->lri_la_dinode;
b4107950
TM
1346 tl_dinode = item->lri_tl_dinode;
1347 qrec = item->lri_qrec;
ed460cff 1348 orphan_reco_type = item->lri_orphan_reco_type;
ccd979bd 1349
b4107950
TM
1350 trace_ocfs2_complete_recovery_slot(item->lri_slot,
1351 la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0,
1352 tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0,
1353 qrec);
1354
1355 if (la_dinode) {
ccd979bd
MF
1356 ret = ocfs2_complete_local_alloc_recovery(osb,
1357 la_dinode);
1358 if (ret < 0)
1359 mlog_errno(ret);
1360
1361 kfree(la_dinode);
1362 }
1363
ccd979bd 1364 if (tl_dinode) {
ccd979bd
MF
1365 ret = ocfs2_complete_truncate_log_recovery(osb,
1366 tl_dinode);
1367 if (ret < 0)
1368 mlog_errno(ret);
1369
1370 kfree(tl_dinode);
1371 }
1372
ed460cff
JQ
1373 ret = ocfs2_recover_orphans(osb, item->lri_slot,
1374 orphan_reco_type);
ccd979bd
MF
1375 if (ret < 0)
1376 mlog_errno(ret);
1377
2205363d 1378 if (qrec) {
2205363d
JK
1379 ret = ocfs2_finish_quota_recovery(osb, qrec,
1380 item->lri_slot);
1381 if (ret < 0)
1382 mlog_errno(ret);
1383 /* Recovery info is already freed now */
1384 }
1385
ccd979bd
MF
1386 kfree(item);
1387 }
1388
b4107950 1389 trace_ocfs2_complete_recovery_end(ret);
ccd979bd
MF
1390}
1391
1392/* NOTE: This function always eats your references to la_dinode and
1393 * tl_dinode, either manually on error, or by passing them to
1394 * ocfs2_complete_recovery */
1395static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1396 int slot_num,
1397 struct ocfs2_dinode *la_dinode,
2205363d 1398 struct ocfs2_dinode *tl_dinode,
ed460cff
JQ
1399 struct ocfs2_quota_recovery *qrec,
1400 enum ocfs2_orphan_reco_type orphan_reco_type)
ccd979bd
MF
1401{
1402 struct ocfs2_la_recovery_item *item;
1403
afae00ab 1404 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
ccd979bd
MF
1405 if (!item) {
1406 /* Though we wish to avoid it, we are in fact safe in
1407 * skipping local alloc cleanup as fsck.ocfs2 is more
1408 * than capable of reclaiming unused space. */
d787ab09
TG
1409 kfree(la_dinode);
1410 kfree(tl_dinode);
ccd979bd 1411
2205363d
JK
1412 if (qrec)
1413 ocfs2_free_quota_recovery(qrec);
1414
ccd979bd
MF
1415 mlog_errno(-ENOMEM);
1416 return;
1417 }
1418
1419 INIT_LIST_HEAD(&item->lri_list);
1420 item->lri_la_dinode = la_dinode;
1421 item->lri_slot = slot_num;
1422 item->lri_tl_dinode = tl_dinode;
2205363d 1423 item->lri_qrec = qrec;
ed460cff 1424 item->lri_orphan_reco_type = orphan_reco_type;
ccd979bd
MF
1425
1426 spin_lock(&journal->j_lock);
1427 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
35ddf78e 1428 queue_work(journal->j_osb->ocfs2_wq, &journal->j_recovery_work);
ccd979bd
MF
1429 spin_unlock(&journal->j_lock);
1430}
1431
1432/* Called by the mount code to queue recovery the last part of
9140db04 1433 * recovery for it's own and offline slot(s). */
ccd979bd
MF
1434void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1435{
1436 struct ocfs2_journal *journal = osb->journal;
1437
10b3dd76
SM
1438 if (ocfs2_is_hard_readonly(osb))
1439 return;
1440
9140db04
SE
1441 /* No need to queue up our truncate_log as regular cleanup will catch
1442 * that */
1443 ocfs2_queue_recovery_completion(journal, osb->slot_num,
ed460cff
JQ
1444 osb->local_alloc_copy, NULL, NULL,
1445 ORPHAN_NEED_TRUNCATE);
9140db04 1446 ocfs2_schedule_truncate_log_flush(osb, 0);
ccd979bd 1447
9140db04 1448 osb->local_alloc_copy = NULL;
9140db04
SE
1449
1450 /* queue to recover orphan slots for all offline slots */
1451 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
ed460cff 1452 ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
9140db04 1453 ocfs2_free_replay_slots(osb);
ccd979bd
MF
1454}
1455
2205363d
JK
1456void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1457{
1458 if (osb->quota_rec) {
1459 ocfs2_queue_recovery_completion(osb->journal,
1460 osb->slot_num,
1461 NULL,
1462 NULL,
ed460cff
JQ
1463 osb->quota_rec,
1464 ORPHAN_NEED_TRUNCATE);
2205363d
JK
1465 osb->quota_rec = NULL;
1466 }
1467}
1468
ccd979bd
MF
1469static int __ocfs2_recovery_thread(void *arg)
1470{
2205363d 1471 int status, node_num, slot_num;
ccd979bd 1472 struct ocfs2_super *osb = arg;
553abd04 1473 struct ocfs2_recovery_map *rm = osb->recovery_map;
2205363d
JK
1474 int *rm_quota = NULL;
1475 int rm_quota_used = 0, i;
1476 struct ocfs2_quota_recovery *qrec;
ccd979bd 1477
21158ca8
G
1478 /* Whether the quota supported. */
1479 int quota_enabled = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
1480 OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
1481 || OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
1482 OCFS2_FEATURE_RO_COMPAT_GRPQUOTA);
1483
ccd979bd
MF
1484 status = ocfs2_wait_on_mount(osb);
1485 if (status < 0) {
1486 goto bail;
1487 }
1488
21158ca8
G
1489 if (quota_enabled) {
1490 rm_quota = kcalloc(osb->max_slots, sizeof(int), GFP_NOFS);
1491 if (!rm_quota) {
1492 status = -ENOMEM;
1493 goto bail;
1494 }
2205363d 1495 }
ccd979bd 1496restart:
fcaf3b26
JK
1497 if (quota_enabled) {
1498 mutex_lock(&osb->recovery_lock);
1499 /* Confirm that recovery thread will no longer recover quotas */
1500 if (osb->recovery_state == OCFS2_REC_QUOTA_WANT_DISABLE) {
1501 osb->recovery_state = OCFS2_REC_QUOTA_DISABLED;
1502 wake_up(&osb->recovery_event);
1503 }
1504 if (osb->recovery_state >= OCFS2_REC_QUOTA_DISABLED)
1505 quota_enabled = 0;
1506 mutex_unlock(&osb->recovery_lock);
1507 }
1508
ccd979bd
MF
1509 status = ocfs2_super_lock(osb, 1);
1510 if (status < 0) {
1511 mlog_errno(status);
1512 goto bail;
1513 }
1514
9140db04
SE
1515 status = ocfs2_compute_replay_slots(osb);
1516 if (status < 0)
1517 mlog_errno(status);
1518
1519 /* queue recovery for our own slot */
1520 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
ed460cff 1521 NULL, NULL, ORPHAN_NO_NEED_TRUNCATE);
9140db04 1522
553abd04
JB
1523 spin_lock(&osb->osb_lock);
1524 while (rm->rm_used) {
1525 /* It's always safe to remove entry zero, as we won't
1526 * clear it until ocfs2_recover_node() has succeeded. */
1527 node_num = rm->rm_entries[0];
1528 spin_unlock(&osb->osb_lock);
2205363d 1529 slot_num = ocfs2_node_num_to_slot(osb, node_num);
b4107950 1530 trace_ocfs2_recovery_thread_node(node_num, slot_num);
2205363d
JK
1531 if (slot_num == -ENOENT) {
1532 status = 0;
2205363d
JK
1533 goto skip_recovery;
1534 }
2205363d
JK
1535
1536 /* It is a bit subtle with quota recovery. We cannot do it
1537 * immediately because we have to obtain cluster locks from
1538 * quota files and we also don't want to just skip it because
1539 * then quota usage would be out of sync until some node takes
1540 * the slot. So we remember which nodes need quota recovery
1541 * and when everything else is done, we recover quotas. */
21158ca8
G
1542 if (quota_enabled) {
1543 for (i = 0; i < rm_quota_used
1544 && rm_quota[i] != slot_num; i++)
1545 ;
1546
1547 if (i == rm_quota_used)
1548 rm_quota[rm_quota_used++] = slot_num;
1549 }
2205363d
JK
1550
1551 status = ocfs2_recover_node(osb, node_num, slot_num);
1552skip_recovery:
553abd04
JB
1553 if (!status) {
1554 ocfs2_recovery_map_clear(osb, node_num);
1555 } else {
ccd979bd
MF
1556 mlog(ML_ERROR,
1557 "Error %d recovering node %d on device (%u,%u)!\n",
1558 status, node_num,
1559 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1560 mlog(ML_ERROR, "Volume requires unmount.\n");
ccd979bd
MF
1561 }
1562
553abd04 1563 spin_lock(&osb->osb_lock);
ccd979bd 1564 }
553abd04 1565 spin_unlock(&osb->osb_lock);
b4107950 1566 trace_ocfs2_recovery_thread_end(status);
553abd04 1567
539d8264
SM
1568 /* Refresh all journal recovery generations from disk */
1569 status = ocfs2_check_journals_nolocks(osb);
1570 status = (status == -EROFS) ? 0 : status;
1571 if (status < 0)
1572 mlog_errno(status);
1573
2205363d 1574 /* Now it is right time to recover quotas... We have to do this under
25985edc 1575 * superblock lock so that no one can start using the slot (and crash)
2205363d 1576 * before we recover it */
21158ca8
G
1577 if (quota_enabled) {
1578 for (i = 0; i < rm_quota_used; i++) {
1579 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1580 if (IS_ERR(qrec)) {
1581 status = PTR_ERR(qrec);
1582 mlog_errno(status);
1583 continue;
1584 }
1585 ocfs2_queue_recovery_completion(osb->journal,
1586 rm_quota[i],
1587 NULL, NULL, qrec,
1588 ORPHAN_NEED_TRUNCATE);
2205363d 1589 }
2205363d
JK
1590 }
1591
ccd979bd
MF
1592 ocfs2_super_unlock(osb, 1);
1593
9140db04 1594 /* queue recovery for offline slots */
ed460cff 1595 ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
ccd979bd
MF
1596
1597bail:
c74ec2f7 1598 mutex_lock(&osb->recovery_lock);
553abd04 1599 if (!status && !ocfs2_recovery_completed(osb)) {
c74ec2f7 1600 mutex_unlock(&osb->recovery_lock);
ccd979bd
MF
1601 goto restart;
1602 }
1603
9140db04 1604 ocfs2_free_replay_slots(osb);
ccd979bd 1605 osb->recovery_thread_task = NULL;
8f947e0f
JK
1606 if (osb->recovery_state == OCFS2_REC_WANT_DISABLE)
1607 osb->recovery_state = OCFS2_REC_DISABLED;
ccd979bd
MF
1608 wake_up(&osb->recovery_event);
1609
c74ec2f7 1610 mutex_unlock(&osb->recovery_lock);
ccd979bd 1611
fcaf3b26 1612 kfree(rm_quota);
2205363d 1613
111e7049 1614 return status;
ccd979bd
MF
1615}
1616
1617void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1618{
c0fb8308
JK
1619 int was_set = -1;
1620
c74ec2f7 1621 mutex_lock(&osb->recovery_lock);
8f947e0f 1622 if (osb->recovery_state < OCFS2_REC_WANT_DISABLE)
c0fb8308 1623 was_set = ocfs2_recovery_map_set(osb, node_num);
ccd979bd 1624
b4107950 1625 trace_ocfs2_recovery_thread(node_num, osb->node_num,
c0fb8308 1626 osb->recovery_state, osb->recovery_thread_task, was_set);
ccd979bd 1627
8f947e0f 1628 if (osb->recovery_state >= OCFS2_REC_WANT_DISABLE)
b4107950 1629 goto out;
ccd979bd
MF
1630
1631 if (osb->recovery_thread_task)
1632 goto out;
1633
1634 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
5afc44e2 1635 "ocfs2rec-%s", osb->uuid_str);
ccd979bd
MF
1636 if (IS_ERR(osb->recovery_thread_task)) {
1637 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1638 osb->recovery_thread_task = NULL;
1639 }
1640
1641out:
c74ec2f7 1642 mutex_unlock(&osb->recovery_lock);
ccd979bd 1643 wake_up(&osb->recovery_event);
ccd979bd
MF
1644}
1645
539d8264
SM
1646static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1647 int slot_num,
1648 struct buffer_head **bh,
1649 struct inode **ret_inode)
1650{
1651 int status = -EACCES;
1652 struct inode *inode = NULL;
1653
1654 BUG_ON(slot_num >= osb->max_slots);
1655
1656 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1657 slot_num);
1658 if (!inode || is_bad_inode(inode)) {
1659 mlog_errno(status);
1660 goto bail;
1661 }
1662 SET_INODE_JOURNAL(inode);
1663
b657c95c 1664 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
539d8264
SM
1665 if (status < 0) {
1666 mlog_errno(status);
1667 goto bail;
1668 }
1669
1670 status = 0;
1671
1672bail:
1673 if (inode) {
1674 if (status || !ret_inode)
1675 iput(inode);
1676 else
1677 *ret_inode = inode;
1678 }
1679 return status;
1680}
1681
ccd979bd
MF
1682/* Does the actual journal replay and marks the journal inode as
1683 * clean. Will only replay if the journal inode is marked dirty. */
1684static int ocfs2_replay_journal(struct ocfs2_super *osb,
1685 int node_num,
1686 int slot_num)
1687{
1688 int status;
1689 int got_lock = 0;
1690 unsigned int flags;
1691 struct inode *inode = NULL;
1692 struct ocfs2_dinode *fe;
1693 journal_t *journal = NULL;
1694 struct buffer_head *bh = NULL;
539d8264 1695 u32 slot_reco_gen;
ccd979bd 1696
539d8264
SM
1697 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1698 if (status) {
ccd979bd
MF
1699 mlog_errno(status);
1700 goto done;
1701 }
539d8264
SM
1702
1703 fe = (struct ocfs2_dinode *)bh->b_data;
1704 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1705 brelse(bh);
1706 bh = NULL;
1707
1708 /*
1709 * As the fs recovery is asynchronous, there is a small chance that
1710 * another node mounted (and recovered) the slot before the recovery
1711 * thread could get the lock. To handle that, we dirty read the journal
1712 * inode for that slot to get the recovery generation. If it is
1713 * different than what we expected, the slot has been recovered.
1714 * If not, it needs recovery.
1715 */
1716 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
b4107950 1717 trace_ocfs2_replay_journal_recovered(slot_num,
539d8264
SM
1718 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1719 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1720 status = -EBUSY;
ccd979bd
MF
1721 goto done;
1722 }
539d8264
SM
1723
1724 /* Continue with recovery as the journal has not yet been recovered */
ccd979bd 1725
e63aecb6 1726 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
ccd979bd 1727 if (status < 0) {
b4107950 1728 trace_ocfs2_replay_journal_lock_err(status);
ccd979bd
MF
1729 if (status != -ERESTARTSYS)
1730 mlog(ML_ERROR, "Could not lock journal!\n");
1731 goto done;
1732 }
1733 got_lock = 1;
1734
1735 fe = (struct ocfs2_dinode *) bh->b_data;
1736
1737 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
539d8264 1738 slot_reco_gen = ocfs2_get_recovery_generation(fe);
ccd979bd
MF
1739
1740 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
b4107950 1741 trace_ocfs2_replay_journal_skip(node_num);
539d8264
SM
1742 /* Refresh recovery generation for the slot */
1743 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
ccd979bd
MF
1744 goto done;
1745 }
1746
9140db04
SE
1747 /* we need to run complete recovery for offline orphan slots */
1748 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1749
619c200d
SM
1750 printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\
1751 "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
1752 MINOR(osb->sb->s_dev));
ccd979bd
MF
1753
1754 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1755
1756 status = ocfs2_force_read_journal(inode);
1757 if (status < 0) {
1758 mlog_errno(status);
1759 goto done;
1760 }
1761
2b4e30fb 1762 journal = jbd2_journal_init_inode(inode);
8e6cf5fb 1763 if (IS_ERR(journal)) {
ccd979bd 1764 mlog(ML_ERROR, "Linux journal layer error\n");
8e6cf5fb 1765 status = PTR_ERR(journal);
ccd979bd
MF
1766 goto done;
1767 }
1768
2b4e30fb 1769 status = jbd2_journal_load(journal);
ccd979bd
MF
1770 if (status < 0) {
1771 mlog_errno(status);
783cc68d 1772 BUG_ON(!igrab(inode));
2b4e30fb 1773 jbd2_journal_destroy(journal);
ccd979bd
MF
1774 goto done;
1775 }
1776
1777 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1778
1779 /* wipe the journal */
2b4e30fb 1780 jbd2_journal_lock_updates(journal);
01d5d965 1781 status = jbd2_journal_flush(journal, 0);
2b4e30fb 1782 jbd2_journal_unlock_updates(journal);
ccd979bd
MF
1783 if (status < 0)
1784 mlog_errno(status);
1785
1786 /* This will mark the node clean */
1787 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1788 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1789 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1790
539d8264
SM
1791 /* Increment recovery generation to indicate successful recovery */
1792 ocfs2_bump_recovery_generation(fe);
1793 osb->slot_recovery_generations[slot_num] =
1794 ocfs2_get_recovery_generation(fe);
1795
13723d00 1796 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
8cb471e8 1797 status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
ccd979bd
MF
1798 if (status < 0)
1799 mlog_errno(status);
1800
783cc68d 1801 BUG_ON(!igrab(inode));
ccd979bd 1802
2b4e30fb 1803 jbd2_journal_destroy(journal);
ccd979bd 1804
619c200d
SM
1805 printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\
1806 "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
1807 MINOR(osb->sb->s_dev));
ccd979bd
MF
1808done:
1809 /* drop the lock on this nodes journal */
1810 if (got_lock)
e63aecb6 1811 ocfs2_inode_unlock(inode, 1);
ccd979bd 1812
72865d92 1813 iput(inode);
a81cb88b 1814 brelse(bh);
ccd979bd 1815
ccd979bd
MF
1816 return status;
1817}
1818
1819/*
1820 * Do the most important parts of node recovery:
1821 * - Replay it's journal
1822 * - Stamp a clean local allocator file
1823 * - Stamp a clean truncate log
1824 * - Mark the node clean
1825 *
1826 * If this function completes without error, a node in OCFS2 can be
1827 * said to have been safely recovered. As a result, failure during the
1828 * second part of a nodes recovery process (local alloc recovery) is
1829 * far less concerning.
1830 */
1831static int ocfs2_recover_node(struct ocfs2_super *osb,
2205363d 1832 int node_num, int slot_num)
ccd979bd
MF
1833{
1834 int status = 0;
ccd979bd
MF
1835 struct ocfs2_dinode *la_copy = NULL;
1836 struct ocfs2_dinode *tl_copy = NULL;
1837
b4107950 1838 trace_ocfs2_recover_node(node_num, slot_num, osb->node_num);
ccd979bd
MF
1839
1840 /* Should not ever be called to recover ourselves -- in that
1841 * case we should've called ocfs2_journal_load instead. */
ebdec83b 1842 BUG_ON(osb->node_num == node_num);
ccd979bd 1843
ccd979bd
MF
1844 status = ocfs2_replay_journal(osb, node_num, slot_num);
1845 if (status < 0) {
539d8264 1846 if (status == -EBUSY) {
b4107950 1847 trace_ocfs2_recover_node_skip(slot_num, node_num);
539d8264
SM
1848 status = 0;
1849 goto done;
1850 }
ccd979bd
MF
1851 mlog_errno(status);
1852 goto done;
1853 }
1854
1855 /* Stamp a clean local alloc file AFTER recovering the journal... */
1856 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1857 if (status < 0) {
1858 mlog_errno(status);
1859 goto done;
1860 }
1861
1862 /* An error from begin_truncate_log_recovery is not
1863 * serious enough to warrant halting the rest of
1864 * recovery. */
1865 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1866 if (status < 0)
1867 mlog_errno(status);
1868
1869 /* Likewise, this would be a strange but ultimately not so
1870 * harmful place to get an error... */
8e8a4603 1871 status = ocfs2_clear_slot(osb, slot_num);
ccd979bd
MF
1872 if (status < 0)
1873 mlog_errno(status);
1874
1875 /* This will kfree the memory pointed to by la_copy and tl_copy */
1876 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
ed460cff 1877 tl_copy, NULL, ORPHAN_NEED_TRUNCATE);
ccd979bd
MF
1878
1879 status = 0;
1880done:
1881
ccd979bd
MF
1882 return status;
1883}
1884
1885/* Test node liveness by trylocking his journal. If we get the lock,
1886 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1887 * still alive (we couldn't get the lock) and < 0 on error. */
1888static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1889 int slot_num)
1890{
1891 int status, flags;
1892 struct inode *inode = NULL;
1893
1894 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1895 slot_num);
1896 if (inode == NULL) {
1897 mlog(ML_ERROR, "access error\n");
1898 status = -EACCES;
1899 goto bail;
1900 }
1901 if (is_bad_inode(inode)) {
1902 mlog(ML_ERROR, "access error (bad inode)\n");
1903 iput(inode);
1904 inode = NULL;
1905 status = -EACCES;
1906 goto bail;
1907 }
1908 SET_INODE_JOURNAL(inode);
1909
1910 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
e63aecb6 1911 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
ccd979bd
MF
1912 if (status < 0) {
1913 if (status != -EAGAIN)
1914 mlog_errno(status);
1915 goto bail;
1916 }
1917
e63aecb6 1918 ocfs2_inode_unlock(inode, 1);
ccd979bd 1919bail:
72865d92 1920 iput(inode);
ccd979bd
MF
1921
1922 return status;
1923}
1924
1925/* Call this underneath ocfs2_super_lock. It also assumes that the
1926 * slot info struct has been updated from disk. */
1927int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1928{
d85b20e4
JB
1929 unsigned int node_num;
1930 int status, i;
a1af7d15 1931 u32 gen;
539d8264
SM
1932 struct buffer_head *bh = NULL;
1933 struct ocfs2_dinode *di;
ccd979bd
MF
1934
1935 /* This is called with the super block cluster lock, so we
1936 * know that the slot map can't change underneath us. */
1937
d85b20e4 1938 for (i = 0; i < osb->max_slots; i++) {
539d8264
SM
1939 /* Read journal inode to get the recovery generation */
1940 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1941 if (status) {
1942 mlog_errno(status);
1943 goto bail;
1944 }
1945 di = (struct ocfs2_dinode *)bh->b_data;
a1af7d15 1946 gen = ocfs2_get_recovery_generation(di);
539d8264
SM
1947 brelse(bh);
1948 bh = NULL;
1949
a1af7d15
MF
1950 spin_lock(&osb->osb_lock);
1951 osb->slot_recovery_generations[i] = gen;
1952
b4107950
TM
1953 trace_ocfs2_mark_dead_nodes(i,
1954 osb->slot_recovery_generations[i]);
539d8264 1955
a1af7d15
MF
1956 if (i == osb->slot_num) {
1957 spin_unlock(&osb->osb_lock);
ccd979bd 1958 continue;
a1af7d15 1959 }
d85b20e4
JB
1960
1961 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
a1af7d15
MF
1962 if (status == -ENOENT) {
1963 spin_unlock(&osb->osb_lock);
ccd979bd 1964 continue;
a1af7d15 1965 }
ccd979bd 1966
a1af7d15
MF
1967 if (__ocfs2_recovery_map_test(osb, node_num)) {
1968 spin_unlock(&osb->osb_lock);
ccd979bd 1969 continue;
a1af7d15 1970 }
d85b20e4 1971 spin_unlock(&osb->osb_lock);
ccd979bd
MF
1972
1973 /* Ok, we have a slot occupied by another node which
1974 * is not in the recovery map. We trylock his journal
1975 * file here to test if he's alive. */
1976 status = ocfs2_trylock_journal(osb, i);
1977 if (!status) {
1978 /* Since we're called from mount, we know that
1979 * the recovery thread can't race us on
1980 * setting / checking the recovery bits. */
1981 ocfs2_recovery_thread(osb, node_num);
1982 } else if ((status < 0) && (status != -EAGAIN)) {
1983 mlog_errno(status);
1984 goto bail;
1985 }
ccd979bd 1986 }
ccd979bd
MF
1987
1988 status = 0;
1989bail:
ccd979bd
MF
1990 return status;
1991}
1992
83273932
SE
1993/*
1994 * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some
a0f8a9a9 1995 * randomness to the timeout to minimize multiple nodes firing the timer at the
83273932
SE
1996 * same time.
1997 */
1998static inline unsigned long ocfs2_orphan_scan_timeout(void)
1999{
2000 unsigned long time;
2001
2002 get_random_bytes(&time, sizeof(time));
2003 time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000);
2004 return msecs_to_jiffies(time);
2005}
2006
2007/*
2008 * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for
2009 * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This
2010 * is done to catch any orphans that are left over in orphan directories.
2011 *
a035bff6
SM
2012 * It scans all slots, even ones that are in use. It does so to handle the
2013 * case described below:
2014 *
2015 * Node 1 has an inode it was using. The dentry went away due to memory
2016 * pressure. Node 1 closes the inode, but it's on the free list. The node
2017 * has the open lock.
2018 * Node 2 unlinks the inode. It grabs the dentry lock to notify others,
2019 * but node 1 has no dentry and doesn't get the message. It trylocks the
2020 * open lock, sees that another node has a PR, and does nothing.
2021 * Later node 2 runs its orphan dir. It igets the inode, trylocks the
2022 * open lock, sees the PR still, and does nothing.
2023 * Basically, we have to trigger an orphan iput on node 1. The only way
2024 * for this to happen is if node 1 runs node 2's orphan dir.
2025 *
83273932
SE
2026 * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT
2027 * seconds. It gets an EX lock on os_lockres and checks sequence number
2028 * stored in LVB. If the sequence number has changed, it means some other
2029 * node has done the scan. This node skips the scan and tracks the
2030 * sequence number. If the sequence number didn't change, it means a scan
2031 * hasn't happened. The node queues a scan and increments the
2032 * sequence number in the LVB.
2033 */
b519ea6d 2034static void ocfs2_queue_orphan_scan(struct ocfs2_super *osb)
83273932
SE
2035{
2036 struct ocfs2_orphan_scan *os;
2037 int status, i;
2038 u32 seqno = 0;
2039
2040 os = &osb->osb_orphan_scan;
2041
692684e1
SM
2042 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
2043 goto out;
2044
b4107950
TM
2045 trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno,
2046 atomic_read(&os->os_state));
2047
df152c24 2048 status = ocfs2_orphan_scan_lock(osb, &seqno);
83273932
SE
2049 if (status < 0) {
2050 if (status != -EAGAIN)
2051 mlog_errno(status);
2052 goto out;
2053 }
2054
692684e1
SM
2055 /* Do no queue the tasks if the volume is being umounted */
2056 if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
2057 goto unlock;
2058
83273932
SE
2059 if (os->os_seqno != seqno) {
2060 os->os_seqno = seqno;
2061 goto unlock;
2062 }
2063
2064 for (i = 0; i < osb->max_slots; i++)
2065 ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL,
ed460cff 2066 NULL, ORPHAN_NO_NEED_TRUNCATE);
83273932
SE
2067 /*
2068 * We queued a recovery on orphan slots, increment the sequence
2069 * number and update LVB so other node will skip the scan for a while
2070 */
2071 seqno++;
15633a22 2072 os->os_count++;
395627b0 2073 os->os_scantime = ktime_get_seconds();
83273932 2074unlock:
df152c24 2075 ocfs2_orphan_scan_unlock(osb, seqno);
83273932 2076out:
b4107950
TM
2077 trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno,
2078 atomic_read(&os->os_state));
83273932
SE
2079 return;
2080}
2081
2082/* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */
b519ea6d 2083static void ocfs2_orphan_scan_work(struct work_struct *work)
83273932
SE
2084{
2085 struct ocfs2_orphan_scan *os;
2086 struct ocfs2_super *osb;
2087
2088 os = container_of(work, struct ocfs2_orphan_scan,
2089 os_orphan_scan_work.work);
2090 osb = os->os_osb;
2091
2092 mutex_lock(&os->os_lock);
2093 ocfs2_queue_orphan_scan(osb);
692684e1 2094 if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE)
35ddf78e 2095 queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
692684e1 2096 ocfs2_orphan_scan_timeout());
83273932
SE
2097 mutex_unlock(&os->os_lock);
2098}
2099
2100void ocfs2_orphan_scan_stop(struct ocfs2_super *osb)
2101{
2102 struct ocfs2_orphan_scan *os;
2103
2104 os = &osb->osb_orphan_scan;
df152c24
SM
2105 if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) {
2106 atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
2107 mutex_lock(&os->os_lock);
2108 cancel_delayed_work(&os->os_orphan_scan_work);
2109 mutex_unlock(&os->os_lock);
2110 }
83273932
SE
2111}
2112
df152c24 2113void ocfs2_orphan_scan_init(struct ocfs2_super *osb)
83273932
SE
2114{
2115 struct ocfs2_orphan_scan *os;
2116
2117 os = &osb->osb_orphan_scan;
2118 os->os_osb = osb;
15633a22 2119 os->os_count = 0;
3211949f 2120 os->os_seqno = 0;
83273932 2121 mutex_init(&os->os_lock);
df152c24 2122 INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work);
8b712cd5 2123}
83273932 2124
8b712cd5
JM
2125void ocfs2_orphan_scan_start(struct ocfs2_super *osb)
2126{
2127 struct ocfs2_orphan_scan *os;
2128
2129 os = &osb->osb_orphan_scan;
395627b0 2130 os->os_scantime = ktime_get_seconds();
df152c24
SM
2131 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
2132 atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
2133 else {
2134 atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE);
35ddf78e 2135 queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
40f165f4 2136 ocfs2_orphan_scan_timeout());
df152c24 2137 }
83273932
SE
2138}
2139
5eae5b96 2140struct ocfs2_orphan_filldir_priv {
3704412b 2141 struct dir_context ctx;
5eae5b96
MF
2142 struct inode *head;
2143 struct ocfs2_super *osb;
30edc43c 2144 enum ocfs2_orphan_reco_type orphan_reco_type;
5eae5b96
MF
2145};
2146
25885a35 2147static bool ocfs2_orphan_filldir(struct dir_context *ctx, const char *name,
ac7576f4
MS
2148 int name_len, loff_t pos, u64 ino,
2149 unsigned type)
5eae5b96 2150{
ac7576f4
MS
2151 struct ocfs2_orphan_filldir_priv *p =
2152 container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx);
5eae5b96
MF
2153 struct inode *iter;
2154
2155 if (name_len == 1 && !strncmp(".", name, 1))
25885a35 2156 return true;
5eae5b96 2157 if (name_len == 2 && !strncmp("..", name, 2))
25885a35 2158 return true;
5eae5b96 2159
30edc43c
JQ
2160 /* do not include dio entry in case of orphan scan */
2161 if ((p->orphan_reco_type == ORPHAN_NO_NEED_TRUNCATE) &&
2162 (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
2163 OCFS2_DIO_ORPHAN_PREFIX_LEN)))
25885a35 2164 return true;
30edc43c 2165
5eae5b96
MF
2166 /* Skip bad inodes so that recovery can continue */
2167 iter = ocfs2_iget(p->osb, ino,
5fa0613e 2168 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
5eae5b96 2169 if (IS_ERR(iter))
25885a35 2170 return true;
5eae5b96 2171
93d911fc
JQ
2172 if (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
2173 OCFS2_DIO_ORPHAN_PREFIX_LEN))
2174 OCFS2_I(iter)->ip_flags |= OCFS2_INODE_DIO_ORPHAN_ENTRY;
2175
ed460cff
JQ
2176 /* Skip inodes which are already added to recover list, since dio may
2177 * happen concurrently with unlink/rename */
2178 if (OCFS2_I(iter)->ip_next_orphan) {
2179 iput(iter);
25885a35 2180 return true;
ed460cff
JQ
2181 }
2182
b4107950 2183 trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno);
5eae5b96
MF
2184 /* No locking is required for the next_orphan queue as there
2185 * is only ever a single process doing orphan recovery. */
2186 OCFS2_I(iter)->ip_next_orphan = p->head;
2187 p->head = iter;
2188
25885a35 2189 return true;
5eae5b96
MF
2190}
2191
b4df6ed8
MF
2192static int ocfs2_queue_orphans(struct ocfs2_super *osb,
2193 int slot,
30edc43c
JQ
2194 struct inode **head,
2195 enum ocfs2_orphan_reco_type orphan_reco_type)
ccd979bd 2196{
b4df6ed8 2197 int status;
ccd979bd 2198 struct inode *orphan_dir_inode = NULL;
3704412b
AV
2199 struct ocfs2_orphan_filldir_priv priv = {
2200 .ctx.actor = ocfs2_orphan_filldir,
2201 .osb = osb,
30edc43c
JQ
2202 .head = *head,
2203 .orphan_reco_type = orphan_reco_type
3704412b 2204 };
ccd979bd
MF
2205
2206 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2207 ORPHAN_DIR_SYSTEM_INODE,
2208 slot);
2209 if (!orphan_dir_inode) {
2210 status = -ENOENT;
2211 mlog_errno(status);
b4df6ed8 2212 return status;
2bd63216 2213 }
ccd979bd 2214
5955102c 2215 inode_lock(orphan_dir_inode);
e63aecb6 2216 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
ccd979bd 2217 if (status < 0) {
ccd979bd
MF
2218 mlog_errno(status);
2219 goto out;
2220 }
ccd979bd 2221
3704412b 2222 status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx);
5eae5b96
MF
2223 if (status) {
2224 mlog_errno(status);
a86370fb 2225 goto out_cluster;
ccd979bd 2226 }
ccd979bd 2227
5eae5b96
MF
2228 *head = priv.head;
2229
a86370fb 2230out_cluster:
e63aecb6 2231 ocfs2_inode_unlock(orphan_dir_inode, 0);
b4df6ed8 2232out:
5955102c 2233 inode_unlock(orphan_dir_inode);
ccd979bd 2234 iput(orphan_dir_inode);
b4df6ed8
MF
2235 return status;
2236}
2237
2238static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
2239 int slot)
2240{
2241 int ret;
2242
2243 spin_lock(&osb->osb_lock);
2244 ret = !osb->osb_orphan_wipes[slot];
2245 spin_unlock(&osb->osb_lock);
2246 return ret;
2247}
2248
2249static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
2250 int slot)
2251{
2252 spin_lock(&osb->osb_lock);
2253 /* Mark ourselves such that new processes in delete_inode()
2254 * know to quit early. */
2255 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
2256 while (osb->osb_orphan_wipes[slot]) {
2257 /* If any processes are already in the middle of an
2258 * orphan wipe on this dir, then we need to wait for
2259 * them. */
2260 spin_unlock(&osb->osb_lock);
2261 wait_event_interruptible(osb->osb_wipe_event,
2262 ocfs2_orphan_recovery_can_continue(osb, slot));
2263 spin_lock(&osb->osb_lock);
2264 }
2265 spin_unlock(&osb->osb_lock);
2266}
2267
2268static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
2269 int slot)
2270{
2271 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
2272}
2273
2274/*
2275 * Orphan recovery. Each mounted node has it's own orphan dir which we
2276 * must run during recovery. Our strategy here is to build a list of
2277 * the inodes in the orphan dir and iget/iput them. The VFS does
2278 * (most) of the rest of the work.
2279 *
2280 * Orphan recovery can happen at any time, not just mount so we have a
2281 * couple of extra considerations.
2282 *
2283 * - We grab as many inodes as we can under the orphan dir lock -
2284 * doing iget() outside the orphan dir risks getting a reference on
2285 * an invalid inode.
2286 * - We must be sure not to deadlock with other processes on the
2287 * system wanting to run delete_inode(). This can happen when they go
2288 * to lock the orphan dir and the orphan recovery process attempts to
2289 * iget() inside the orphan dir lock. This can be avoided by
2290 * advertising our state to ocfs2_delete_inode().
2291 */
2292static int ocfs2_recover_orphans(struct ocfs2_super *osb,
ed460cff
JQ
2293 int slot,
2294 enum ocfs2_orphan_reco_type orphan_reco_type)
b4df6ed8
MF
2295{
2296 int ret = 0;
2297 struct inode *inode = NULL;
2298 struct inode *iter;
2299 struct ocfs2_inode_info *oi;
cf1776a9
JQ
2300 struct buffer_head *di_bh = NULL;
2301 struct ocfs2_dinode *di = NULL;
b4df6ed8 2302
b4107950 2303 trace_ocfs2_recover_orphans(slot);
b4df6ed8
MF
2304
2305 ocfs2_mark_recovering_orphan_dir(osb, slot);
30edc43c 2306 ret = ocfs2_queue_orphans(osb, slot, &inode, orphan_reco_type);
b4df6ed8
MF
2307 ocfs2_clear_recovering_orphan_dir(osb, slot);
2308
2309 /* Error here should be noted, but we want to continue with as
2310 * many queued inodes as we've got. */
2311 if (ret)
2312 mlog_errno(ret);
ccd979bd
MF
2313
2314 while (inode) {
2315 oi = OCFS2_I(inode);
b4107950
TM
2316 trace_ocfs2_recover_orphans_iput(
2317 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
2318
2319 iter = oi->ip_next_orphan;
ed460cff 2320 oi->ip_next_orphan = NULL;
ccd979bd 2321
93d911fc 2322 if (oi->ip_flags & OCFS2_INODE_DIO_ORPHAN_ENTRY) {
5955102c 2323 inode_lock(inode);
93d911fc
JQ
2324 ret = ocfs2_rw_lock(inode, 1);
2325 if (ret < 0) {
2326 mlog_errno(ret);
2327 goto unlock_mutex;
2328 }
2329 /*
2330 * We need to take and drop the inode lock to
2331 * force read inode from disk.
2332 */
2333 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2334 if (ret) {
2335 mlog_errno(ret);
2336 goto unlock_rw;
2337 }
2338
2339 di = (struct ocfs2_dinode *)di_bh->b_data;
2340
2341 if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) {
2342 ret = ocfs2_truncate_file(inode, di_bh,
2343 i_size_read(inode));
2344 if (ret < 0) {
2345 if (ret != -ENOSPC)
2346 mlog_errno(ret);
2347 goto unlock_inode;
2348 }
cf1776a9 2349
93d911fc
JQ
2350 ret = ocfs2_del_inode_from_orphan(osb, inode,
2351 di_bh, 0, 0);
2352 if (ret)
2353 mlog_errno(ret);
2354 }
2355unlock_inode:
2356 ocfs2_inode_unlock(inode, 1);
2357 brelse(di_bh);
2358 di_bh = NULL;
2359unlock_rw:
2360 ocfs2_rw_unlock(inode, 1);
2361unlock_mutex:
5955102c 2362 inode_unlock(inode);
ed460cff 2363
93d911fc
JQ
2364 /* clear dio flag in ocfs2_inode_info */
2365 oi->ip_flags &= ~OCFS2_INODE_DIO_ORPHAN_ENTRY;
2366 } else {
ed460cff
JQ
2367 spin_lock(&oi->ip_lock);
2368 /* Set the proper information to get us going into
2369 * ocfs2_delete_inode. */
2370 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2371 spin_unlock(&oi->ip_lock);
ad694821
JQ
2372 }
2373
ccd979bd 2374 iput(inode);
ccd979bd
MF
2375 inode = iter;
2376 }
2377
b4df6ed8 2378 return ret;
ccd979bd
MF
2379}
2380
19ece546 2381static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
ccd979bd
MF
2382{
2383 /* This check is good because ocfs2 will wait on our recovery
2384 * thread before changing it to something other than MOUNTED
2385 * or DISABLED. */
2386 wait_event(osb->osb_mount_event,
19ece546
JK
2387 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
2388 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
ccd979bd
MF
2389 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
2390
2391 /* If there's an error on mount, then we may never get to the
2392 * MOUNTED flag, but this is set right before
2393 * dismount_volume() so we can trust it. */
2394 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
b4107950 2395 trace_ocfs2_wait_on_mount(VOLUME_DISABLED);
ccd979bd
MF
2396 mlog(0, "mount error, exiting!\n");
2397 return -EBUSY;
2398 }
2399
2400 return 0;
2401}
2402
2403static int ocfs2_commit_thread(void *arg)
2404{
2405 int status;
2406 struct ocfs2_super *osb = arg;
2407 struct ocfs2_journal *journal = osb->journal;
2408
2409 /* we can trust j_num_trans here because _should_stop() is only set in
2410 * shutdown and nobody other than ourselves should be able to start
2411 * transactions. committing on shutdown might take a few iterations
2412 * as final transactions put deleted inodes on the list */
2413 while (!(kthread_should_stop() &&
2414 atomic_read(&journal->j_num_trans) == 0)) {
2415
745ae8ba
MF
2416 wait_event_interruptible(osb->checkpoint_event,
2417 atomic_read(&journal->j_num_trans)
2418 || kthread_should_stop());
ccd979bd
MF
2419
2420 status = ocfs2_commit_cache(osb);
55b465b6
JQ
2421 if (status < 0) {
2422 static unsigned long abort_warn_time;
2423
2424 /* Warn about this once per minute */
2425 if (printk_timed_ratelimit(&abort_warn_time, 60*HZ))
2426 mlog(ML_ERROR, "status = %d, journal is "
2427 "already aborted.\n", status);
2428 /*
2429 * After ocfs2_commit_cache() fails, j_num_trans has a
2430 * non-zero value. Sleep here to avoid a busy-wait
2431 * loop.
2432 */
2433 msleep_interruptible(1000);
2434 }
ccd979bd
MF
2435
2436 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
2437 mlog(ML_KTHREAD,
2438 "commit_thread: %u transactions pending on "
2439 "shutdown\n",
2440 atomic_read(&journal->j_num_trans));
2441 }
2442 }
2443
2444 return 0;
2445}
2446
539d8264
SM
2447/* Reads all the journal inodes without taking any cluster locks. Used
2448 * for hard readonly access to determine whether any journal requires
2449 * recovery. Also used to refresh the recovery generation numbers after
2450 * a journal has been recovered by another node.
2451 */
ccd979bd
MF
2452int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
2453{
2454 int ret = 0;
2455 unsigned int slot;
539d8264 2456 struct buffer_head *di_bh = NULL;
ccd979bd 2457 struct ocfs2_dinode *di;
539d8264 2458 int journal_dirty = 0;
ccd979bd
MF
2459
2460 for(slot = 0; slot < osb->max_slots; slot++) {
539d8264
SM
2461 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
2462 if (ret) {
ccd979bd
MF
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466
2467 di = (struct ocfs2_dinode *) di_bh->b_data;
2468
539d8264
SM
2469 osb->slot_recovery_generations[slot] =
2470 ocfs2_get_recovery_generation(di);
2471
ccd979bd
MF
2472 if (le32_to_cpu(di->id1.journal1.ij_flags) &
2473 OCFS2_JOURNAL_DIRTY_FL)
539d8264 2474 journal_dirty = 1;
ccd979bd
MF
2475
2476 brelse(di_bh);
539d8264 2477 di_bh = NULL;
ccd979bd
MF
2478 }
2479
2480out:
539d8264
SM
2481 if (journal_dirty)
2482 ret = -EROFS;
ccd979bd
MF
2483 return ret;
2484}