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