blob: a70d49da2cebcb7e2864ef518e7931dd161067aa [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * journal.c
5 *
6 * Defines functions of journalling api
7 *
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
30#include <linux/kthread.h>
31
32#define MLOG_MASK_PREFIX ML_JOURNAL
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
Joel Becker50655ae2008-09-11 15:53:07 -070038#include "blockcheck.h"
Mark Fasheh316f4b92007-09-07 18:21:26 -070039#include "dir.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080040#include "dlmglue.h"
41#include "extent_map.h"
42#include "heartbeat.h"
43#include "inode.h"
44#include "journal.h"
45#include "localalloc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080046#include "slot_map.h"
47#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080048#include "sysfile.h"
Jan Kara22053632008-10-20 23:50:38 +020049#include "quota.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080050
51#include "buffer_head_io.h"
52
Ingo Molnar34af9462006-06-27 02:53:55 -070053DEFINE_SPINLOCK(trans_inc_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -080054
55static int ocfs2_force_read_journal(struct inode *inode);
56static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +020057 int node_num, int slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -080058static int __ocfs2_recovery_thread(void *arg);
59static int ocfs2_commit_cache(struct ocfs2_super *osb);
Jan Kara19ece542008-08-21 20:13:17 +020060static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
Mark Fashehccd979b2005-12-15 14:31:24 -080061static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -070062 int dirty, int replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -080063static int ocfs2_trylock_journal(struct ocfs2_super *osb,
64 int slot_num);
65static int ocfs2_recover_orphans(struct ocfs2_super *osb,
66 int slot);
67static int ocfs2_commit_thread(void *arg);
68
Jan Kara19ece542008-08-21 20:13:17 +020069static 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
Joel Becker553abd02008-02-01 12:03:57 -080079int ocfs2_recovery_init(struct ocfs2_super *osb)
80{
81 struct ocfs2_recovery_map *rm;
82
83 mutex_init(&osb->recovery_lock);
84 osb->disable_recovery = 0;
85 osb->recovery_thread_task = NULL;
86 init_waitqueue_head(&osb->recovery_event);
87
88 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
89 osb->max_slots * sizeof(unsigned int),
90 GFP_KERNEL);
91 if (!rm) {
92 mlog_errno(-ENOMEM);
93 return -ENOMEM;
94 }
95
96 rm->rm_entries = (unsigned int *)((char *)rm +
97 sizeof(struct ocfs2_recovery_map));
98 osb->recovery_map = rm;
99
100 return 0;
101}
102
103/* we can't grab the goofy sem lock from inside wait_event, so we use
104 * memory barriers to make sure that we'll see the null task before
105 * being woken up */
106static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
107{
108 mb();
109 return osb->recovery_thread_task != NULL;
110}
111
112void ocfs2_recovery_exit(struct ocfs2_super *osb)
113{
114 struct ocfs2_recovery_map *rm;
115
116 /* disable any new recovery threads and wait for any currently
117 * running ones to exit. Do this before setting the vol_state. */
118 mutex_lock(&osb->recovery_lock);
119 osb->disable_recovery = 1;
120 mutex_unlock(&osb->recovery_lock);
121 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
122
123 /* At this point, we know that no more recovery threads can be
124 * launched, so wait for any recovery completion work to
125 * complete. */
126 flush_workqueue(ocfs2_wq);
127
128 /*
129 * Now that recovery is shut down, and the osb is about to be
130 * freed, the osb_lock is not taken here.
131 */
132 rm = osb->recovery_map;
133 /* XXX: Should we bug if there are dirty entries? */
134
135 kfree(rm);
136}
137
138static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
139 unsigned int node_num)
140{
141 int i;
142 struct ocfs2_recovery_map *rm = osb->recovery_map;
143
144 assert_spin_locked(&osb->osb_lock);
145
146 for (i = 0; i < rm->rm_used; i++) {
147 if (rm->rm_entries[i] == node_num)
148 return 1;
149 }
150
151 return 0;
152}
153
154/* Behaves like test-and-set. Returns the previous value */
155static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
156 unsigned int node_num)
157{
158 struct ocfs2_recovery_map *rm = osb->recovery_map;
159
160 spin_lock(&osb->osb_lock);
161 if (__ocfs2_recovery_map_test(osb, node_num)) {
162 spin_unlock(&osb->osb_lock);
163 return 1;
164 }
165
166 /* XXX: Can this be exploited? Not from o2dlm... */
167 BUG_ON(rm->rm_used >= osb->max_slots);
168
169 rm->rm_entries[rm->rm_used] = node_num;
170 rm->rm_used++;
171 spin_unlock(&osb->osb_lock);
172
173 return 0;
174}
175
176static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
177 unsigned int node_num)
178{
179 int i;
180 struct ocfs2_recovery_map *rm = osb->recovery_map;
181
182 spin_lock(&osb->osb_lock);
183
184 for (i = 0; i < rm->rm_used; i++) {
185 if (rm->rm_entries[i] == node_num)
186 break;
187 }
188
189 if (i < rm->rm_used) {
190 /* XXX: be careful with the pointer math */
191 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
192 (rm->rm_used - i - 1) * sizeof(unsigned int));
193 rm->rm_used--;
194 }
195
196 spin_unlock(&osb->osb_lock);
197}
198
Mark Fashehccd979b2005-12-15 14:31:24 -0800199static int ocfs2_commit_cache(struct ocfs2_super *osb)
200{
201 int status = 0;
202 unsigned int flushed;
203 unsigned long old_id;
204 struct ocfs2_journal *journal = NULL;
205
206 mlog_entry_void();
207
208 journal = osb->journal;
209
210 /* Flush all pending commits and checkpoint the journal. */
211 down_write(&journal->j_trans_barrier);
212
213 if (atomic_read(&journal->j_num_trans) == 0) {
214 up_write(&journal->j_trans_barrier);
215 mlog(0, "No transactions for me to flush!\n");
216 goto finally;
217 }
218
Joel Becker2b4e30f2008-09-03 20:03:41 -0700219 jbd2_journal_lock_updates(journal->j_journal);
220 status = jbd2_journal_flush(journal->j_journal);
221 jbd2_journal_unlock_updates(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800222 if (status < 0) {
223 up_write(&journal->j_trans_barrier);
224 mlog_errno(status);
225 goto finally;
226 }
227
228 old_id = ocfs2_inc_trans_id(journal);
229
230 flushed = atomic_read(&journal->j_num_trans);
231 atomic_set(&journal->j_num_trans, 0);
232 up_write(&journal->j_trans_barrier);
233
234 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
235 journal->j_trans_id, flushed);
236
Mark Fasheh34d024f2007-09-24 15:56:19 -0700237 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800238 wake_up(&journal->j_checkpointed);
239finally:
240 mlog_exit(status);
241 return status;
242}
243
Mark Fashehccd979b2005-12-15 14:31:24 -0800244/* pass it NULL and it will allocate a new handle object for you. If
245 * you pass it a handle however, it may still return error, in which
246 * case it has free'd the passed handle for you. */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700247handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
Mark Fashehccd979b2005-12-15 14:31:24 -0800248{
Mark Fashehccd979b2005-12-15 14:31:24 -0800249 journal_t *journal = osb->journal->j_journal;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700250 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800251
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100252 BUG_ON(!osb || !osb->journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800253
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700254 if (ocfs2_is_hard_readonly(osb))
255 return ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800256
257 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
258 BUG_ON(max_buffs <= 0);
259
Jan Kara90e86a62008-08-27 22:30:28 +0200260 /* Nested transaction? Just return the handle... */
261 if (journal_current_handle())
262 return jbd2_journal_start(journal, max_buffs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800263
Mark Fashehccd979b2005-12-15 14:31:24 -0800264 down_read(&osb->journal->j_trans_barrier);
265
Joel Becker2b4e30f2008-09-03 20:03:41 -0700266 handle = jbd2_journal_start(journal, max_buffs);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700267 if (IS_ERR(handle)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800268 up_read(&osb->journal->j_trans_barrier);
269
Mark Fasheh1fabe142006-10-09 18:11:45 -0700270 mlog_errno(PTR_ERR(handle));
Mark Fashehccd979b2005-12-15 14:31:24 -0800271
272 if (is_journal_aborted(journal)) {
273 ocfs2_abort(osb->sb, "Detected aborted journal");
Mark Fasheh1fabe142006-10-09 18:11:45 -0700274 handle = ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800276 } else {
277 if (!ocfs2_mount_local(osb))
278 atomic_inc(&(osb->journal->j_num_trans));
279 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800280
Mark Fashehccd979b2005-12-15 14:31:24 -0800281 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800282}
283
Mark Fasheh1fabe142006-10-09 18:11:45 -0700284int ocfs2_commit_trans(struct ocfs2_super *osb,
285 handle_t *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800286{
Jan Kara90e86a62008-08-27 22:30:28 +0200287 int ret, nested;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700288 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800289
290 BUG_ON(!handle);
291
Jan Kara90e86a62008-08-27 22:30:28 +0200292 nested = handle->h_ref > 1;
Joel Becker2b4e30f2008-09-03 20:03:41 -0700293 ret = jbd2_journal_stop(handle);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700294 if (ret < 0)
295 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800296
Jan Kara90e86a62008-08-27 22:30:28 +0200297 if (!nested)
298 up_read(&journal->j_trans_barrier);
Mark Fashehccd979b2005-12-15 14:31:24 -0800299
Mark Fasheh1fabe142006-10-09 18:11:45 -0700300 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800301}
302
303/*
304 * 'nblocks' is what you want to add to the current
305 * transaction. extend_trans will either extend the current handle by
306 * nblocks, or commit it and start a new one with nblocks credits.
307 *
Joel Becker2b4e30f2008-09-03 20:03:41 -0700308 * This might call jbd2_journal_restart() which will commit dirty buffers
Mark Fashehe8aed342007-12-03 16:43:01 -0800309 * and then restart the transaction. Before calling
310 * ocfs2_extend_trans(), any changed blocks should have been
311 * dirtied. After calling it, all blocks which need to be changed must
312 * go through another set of journal_access/journal_dirty calls.
313 *
Mark Fashehccd979b2005-12-15 14:31:24 -0800314 * WARNING: This will not release any semaphores or disk locks taken
315 * during the transaction, so make sure they were taken *before*
316 * start_trans or we'll have ordering deadlocks.
317 *
318 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
319 * good because transaction ids haven't yet been recorded on the
320 * cluster locks associated with this handle.
321 */
Mark Fasheh1fc58142006-10-05 14:15:36 -0700322int ocfs2_extend_trans(handle_t *handle, int nblocks)
Mark Fashehccd979b2005-12-15 14:31:24 -0800323{
324 int status;
325
326 BUG_ON(!handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800327 BUG_ON(!nblocks);
328
329 mlog_entry_void();
330
331 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
332
Joel Beckere407e392008-06-12 22:35:39 -0700333#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fasheh0879c582007-12-03 16:42:19 -0800334 status = 1;
335#else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700336 status = jbd2_journal_extend(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800337 if (status < 0) {
338 mlog_errno(status);
339 goto bail;
340 }
Mark Fasheh0879c582007-12-03 16:42:19 -0800341#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800342
343 if (status > 0) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700344 mlog(0,
345 "jbd2_journal_extend failed, trying "
346 "jbd2_journal_restart\n");
347 status = jbd2_journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800349 mlog_errno(status);
350 goto bail;
351 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700352 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800353
354 status = 0;
355bail:
356
357 mlog_exit(status);
358 return status;
359}
360
Joel Becker50655ae2008-09-11 15:53:07 -0700361struct ocfs2_triggers {
362 struct jbd2_buffer_trigger_type ot_triggers;
363 int ot_offset;
364};
365
366static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
367{
368 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
369}
370
371static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
372 struct buffer_head *bh,
373 void *data, size_t size)
374{
375 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
376
377 /*
378 * We aren't guaranteed to have the superblock here, so we
379 * must unconditionally compute the ecc data.
380 * __ocfs2_journal_access() will only set the triggers if
381 * metaecc is enabled.
382 */
383 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
384}
385
386/*
387 * Quota blocks have their own trigger because the struct ocfs2_block_check
388 * offset depends on the blocksize.
389 */
390static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
391 struct buffer_head *bh,
392 void *data, size_t size)
393{
394 struct ocfs2_disk_dqtrailer *dqt =
395 ocfs2_block_dqtrailer(size, data);
396
397 /*
398 * We aren't guaranteed to have the superblock here, so we
399 * must unconditionally compute the ecc data.
400 * __ocfs2_journal_access() will only set the triggers if
401 * metaecc is enabled.
402 */
403 ocfs2_block_check_compute(data, size, &dqt->dq_check);
404}
405
Joel Beckerc175a512008-12-10 17:58:22 -0800406/*
407 * Directory blocks also have their own trigger because the
408 * struct ocfs2_block_check offset depends on the blocksize.
409 */
410static void ocfs2_db_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
411 struct buffer_head *bh,
412 void *data, size_t size)
413{
414 struct ocfs2_dir_block_trailer *trailer =
415 ocfs2_dir_trailer_from_size(size, data);
416
417 /*
418 * We aren't guaranteed to have the superblock here, so we
419 * must unconditionally compute the ecc data.
420 * __ocfs2_journal_access() will only set the triggers if
421 * metaecc is enabled.
422 */
423 ocfs2_block_check_compute(data, size, &trailer->db_check);
424}
425
Joel Becker50655ae2008-09-11 15:53:07 -0700426static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
427 struct buffer_head *bh)
428{
429 mlog(ML_ERROR,
430 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
431 "bh->b_blocknr = %llu\n",
432 (unsigned long)bh,
433 (unsigned long long)bh->b_blocknr);
434
435 /* We aren't guaranteed to have the superblock here - but if we
436 * don't, it'll just crash. */
437 ocfs2_error(bh->b_assoc_map->host->i_sb,
438 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
439}
440
441static struct ocfs2_triggers di_triggers = {
442 .ot_triggers = {
443 .t_commit = ocfs2_commit_trigger,
444 .t_abort = ocfs2_abort_trigger,
445 },
446 .ot_offset = offsetof(struct ocfs2_dinode, i_check),
447};
448
449static struct ocfs2_triggers eb_triggers = {
450 .ot_triggers = {
451 .t_commit = ocfs2_commit_trigger,
452 .t_abort = ocfs2_abort_trigger,
453 },
454 .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
455};
456
457static struct ocfs2_triggers gd_triggers = {
458 .ot_triggers = {
459 .t_commit = ocfs2_commit_trigger,
460 .t_abort = ocfs2_abort_trigger,
461 },
462 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
463};
464
Joel Beckerc175a512008-12-10 17:58:22 -0800465static struct ocfs2_triggers db_triggers = {
466 .ot_triggers = {
467 .t_commit = ocfs2_db_commit_trigger,
468 .t_abort = ocfs2_abort_trigger,
469 },
470};
471
Joel Becker50655ae2008-09-11 15:53:07 -0700472static struct ocfs2_triggers xb_triggers = {
473 .ot_triggers = {
474 .t_commit = ocfs2_commit_trigger,
475 .t_abort = ocfs2_abort_trigger,
476 },
477 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
478};
479
480static struct ocfs2_triggers dq_triggers = {
481 .ot_triggers = {
482 .t_commit = ocfs2_dq_commit_trigger,
483 .t_abort = ocfs2_abort_trigger,
484 },
485};
486
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800487static struct ocfs2_triggers dr_triggers = {
488 .ot_triggers = {
489 .t_commit = ocfs2_commit_trigger,
490 .t_abort = ocfs2_abort_trigger,
491 },
492 .ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check),
493};
494
495static struct ocfs2_triggers dl_triggers = {
496 .ot_triggers = {
497 .t_commit = ocfs2_commit_trigger,
498 .t_abort = ocfs2_abort_trigger,
499 },
500 .ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check),
501};
502
Joel Becker50655ae2008-09-11 15:53:07 -0700503static int __ocfs2_journal_access(handle_t *handle,
504 struct inode *inode,
505 struct buffer_head *bh,
506 struct ocfs2_triggers *triggers,
507 int type)
Mark Fashehccd979b2005-12-15 14:31:24 -0800508{
509 int status;
510
511 BUG_ON(!inode);
512 BUG_ON(!handle);
513 BUG_ON(!bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800514
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800515 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 (unsigned long long)bh->b_blocknr, type,
517 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
518 "OCFS2_JOURNAL_ACCESS_CREATE" :
519 "OCFS2_JOURNAL_ACCESS_WRITE",
520 bh->b_size);
521
522 /* we can safely remove this assertion after testing. */
523 if (!buffer_uptodate(bh)) {
524 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
525 mlog(ML_ERROR, "b_blocknr=%llu\n",
526 (unsigned long long)bh->b_blocknr);
527 BUG();
528 }
529
530 /* Set the current transaction information on the inode so
531 * that the locking code knows whether it can drop it's locks
532 * on this inode or not. We're protected from the commit
533 * thread updating the current transaction id until
534 * ocfs2_commit_trans() because ocfs2_start_trans() took
535 * j_trans_barrier for us. */
536 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
537
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800538 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800539 switch (type) {
540 case OCFS2_JOURNAL_ACCESS_CREATE:
541 case OCFS2_JOURNAL_ACCESS_WRITE:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700542 status = jbd2_journal_get_write_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800543 break;
544
545 case OCFS2_JOURNAL_ACCESS_UNDO:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700546 status = jbd2_journal_get_undo_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800547 break;
548
549 default:
550 status = -EINVAL;
551 mlog(ML_ERROR, "Uknown access type!\n");
552 }
Joel Becker50655ae2008-09-11 15:53:07 -0700553 if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
554 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800555 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800556
557 if (status < 0)
558 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
559 status, type);
560
561 mlog_exit(status);
562 return status;
563}
564
Joel Becker50655ae2008-09-11 15:53:07 -0700565int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
566 struct buffer_head *bh, int type)
567{
568 return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
569 type);
570}
571
572int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
573 struct buffer_head *bh, int type)
574{
575 return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
576 type);
577}
578
579int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
580 struct buffer_head *bh, int type)
581{
582 return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
583 type);
584}
585
586int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
587 struct buffer_head *bh, int type)
588{
Joel Beckerc175a512008-12-10 17:58:22 -0800589 return __ocfs2_journal_access(handle, inode, bh, &db_triggers,
590 type);
Joel Becker50655ae2008-09-11 15:53:07 -0700591}
592
593int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
594 struct buffer_head *bh, int type)
595{
596 return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
597 type);
598}
599
600int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
601 struct buffer_head *bh, int type)
602{
603 return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
604 type);
605}
606
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800607int ocfs2_journal_access_dr(handle_t *handle, struct inode *inode,
608 struct buffer_head *bh, int type)
609{
610 return __ocfs2_journal_access(handle, inode, bh, &dr_triggers,
611 type);
612}
613
614int ocfs2_journal_access_dl(handle_t *handle, struct inode *inode,
615 struct buffer_head *bh, int type)
616{
617 return __ocfs2_journal_access(handle, inode, bh, &dl_triggers,
618 type);
619}
620
Joel Becker50655ae2008-09-11 15:53:07 -0700621int ocfs2_journal_access(handle_t *handle, struct inode *inode,
622 struct buffer_head *bh, int type)
623{
624 return __ocfs2_journal_access(handle, inode, bh, NULL, type);
625}
626
Mark Fasheh1fabe142006-10-09 18:11:45 -0700627int ocfs2_journal_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 struct buffer_head *bh)
629{
630 int status;
631
Mark Fashehccd979b2005-12-15 14:31:24 -0800632 mlog_entry("(bh->b_blocknr=%llu)\n",
633 (unsigned long long)bh->b_blocknr);
634
Joel Becker2b4e30f2008-09-03 20:03:41 -0700635 status = jbd2_journal_dirty_metadata(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800636 if (status < 0)
637 mlog(ML_ERROR, "Could not dirty metadata buffer. "
638 "(bh->b_blocknr=%llu)\n",
639 (unsigned long long)bh->b_blocknr);
640
641 mlog_exit(status);
642 return status;
643}
644
Joel Becker2b4e30f2008-09-03 20:03:41 -0700645#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800646
647void ocfs2_set_journal_params(struct ocfs2_super *osb)
648{
649 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800650 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
651
652 if (osb->osb_commit_interval)
653 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800654
655 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800656 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800657 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700658 journal->j_flags |= JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800659 else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700660 journal->j_flags &= ~JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800661 spin_unlock(&journal->j_state_lock);
662}
663
664int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
665{
666 int status = -1;
667 struct inode *inode = NULL; /* the journal inode */
668 journal_t *j_journal = NULL;
669 struct ocfs2_dinode *di = NULL;
670 struct buffer_head *bh = NULL;
671 struct ocfs2_super *osb;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700672 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800673
674 mlog_entry_void();
675
676 BUG_ON(!journal);
677
678 osb = journal->j_osb;
679
680 /* already have the inode for our journal */
681 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
682 osb->slot_num);
683 if (inode == NULL) {
684 status = -EACCES;
685 mlog_errno(status);
686 goto done;
687 }
688 if (is_bad_inode(inode)) {
689 mlog(ML_ERROR, "access error (bad inode)\n");
690 iput(inode);
691 inode = NULL;
692 status = -EACCES;
693 goto done;
694 }
695
696 SET_INODE_JOURNAL(inode);
697 OCFS2_I(inode)->ip_open_count++;
698
Mark Fasheh6eff5792006-01-18 10:31:47 -0800699 /* Skip recovery waits here - journal inode metadata never
700 * changes in a live cluster so it can be considered an
701 * exception to the rule. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700702 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800703 if (status < 0) {
704 if (status != -ERESTARTSYS)
705 mlog(ML_ERROR, "Could not get lock on journal!\n");
706 goto done;
707 }
708
Mark Fashehe63aecb62007-10-18 15:30:42 -0700709 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800710 di = (struct ocfs2_dinode *)bh->b_data;
711
712 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
713 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
714 inode->i_size);
715 status = -EINVAL;
716 goto done;
717 }
718
719 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800720 mlog(0, "inode->i_blocks = %llu\n",
721 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
723
724 /* call the kernels journal init function now */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700725 j_journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 if (j_journal == NULL) {
727 mlog(ML_ERROR, "Linux journal layer error\n");
728 status = -EINVAL;
729 goto done;
730 }
731
Joel Becker2b4e30f2008-09-03 20:03:41 -0700732 mlog(0, "Returned from jbd2_journal_init_inode\n");
Mark Fashehccd979b2005-12-15 14:31:24 -0800733 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
734
735 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
736 OCFS2_JOURNAL_DIRTY_FL);
737
738 journal->j_journal = j_journal;
739 journal->j_inode = inode;
740 journal->j_bh = bh;
741
742 ocfs2_set_journal_params(osb);
743
744 journal->j_state = OCFS2_JOURNAL_LOADED;
745
746 status = 0;
747done:
748 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700749 if (inode_lock)
750 ocfs2_inode_unlock(inode, 1);
Mark Fasheha81cb882008-10-07 14:25:16 -0700751 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800752 if (inode) {
753 OCFS2_I(inode)->ip_open_count--;
754 iput(inode);
755 }
756 }
757
758 mlog_exit(status);
759 return status;
760}
761
Sunil Mushran539d8262008-07-14 17:31:10 -0700762static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
763{
764 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
765}
766
767static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
768{
769 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
770}
771
Mark Fashehccd979b2005-12-15 14:31:24 -0800772static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -0700773 int dirty, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800774{
775 int status;
776 unsigned int flags;
777 struct ocfs2_journal *journal = osb->journal;
778 struct buffer_head *bh = journal->j_bh;
779 struct ocfs2_dinode *fe;
780
781 mlog_entry_void();
782
783 fe = (struct ocfs2_dinode *)bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800784
785 /* The journal bh on the osb always comes from ocfs2_journal_init()
786 * and was validated there inside ocfs2_inode_lock_full(). It's a
787 * code bug if we mess it up. */
788 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800789
790 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
791 if (dirty)
792 flags |= OCFS2_JOURNAL_DIRTY_FL;
793 else
794 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
795 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
796
Sunil Mushran539d8262008-07-14 17:31:10 -0700797 if (replayed)
798 ocfs2_bump_recovery_generation(fe);
799
Joel Becker13723d02008-10-17 19:25:01 -0700800 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -0800801 status = ocfs2_write_block(osb, bh, journal->j_inode);
802 if (status < 0)
803 mlog_errno(status);
804
Mark Fashehccd979b2005-12-15 14:31:24 -0800805 mlog_exit(status);
806 return status;
807}
808
809/*
810 * If the journal has been kmalloc'd it needs to be freed after this
811 * call.
812 */
813void ocfs2_journal_shutdown(struct ocfs2_super *osb)
814{
815 struct ocfs2_journal *journal = NULL;
816 int status = 0;
817 struct inode *inode = NULL;
818 int num_running_trans = 0;
819
820 mlog_entry_void();
821
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100822 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800823
824 journal = osb->journal;
825 if (!journal)
826 goto done;
827
828 inode = journal->j_inode;
829
830 if (journal->j_state != OCFS2_JOURNAL_LOADED)
831 goto done;
832
Joel Becker2b4e30f2008-09-03 20:03:41 -0700833 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800834 if (!igrab(inode))
835 BUG();
836
837 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
838 if (num_running_trans > 0)
839 mlog(0, "Shutting down journal: must wait on %d "
840 "running transactions!\n",
841 num_running_trans);
842
843 /* Do a commit_cache here. It will flush our journal, *and*
844 * release any locks that are still held.
845 * set the SHUTDOWN flag and release the trans lock.
846 * the commit thread will take the trans lock for us below. */
847 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
848
849 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
850 * drop the trans_lock (which we want to hold until we
851 * completely destroy the journal. */
852 if (osb->commit_task) {
853 /* Wait for the commit thread */
854 mlog(0, "Waiting for ocfs2commit to exit....\n");
855 kthread_stop(osb->commit_task);
856 osb->commit_task = NULL;
857 }
858
859 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
860
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800861 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700862 jbd2_journal_lock_updates(journal->j_journal);
863 status = jbd2_journal_flush(journal->j_journal);
864 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800865 if (status < 0)
866 mlog_errno(status);
867 }
868
869 if (status == 0) {
870 /*
871 * Do not toggle if flush was unsuccessful otherwise
872 * will leave dirty metadata in a "clean" journal
873 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700874 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800875 if (status < 0)
876 mlog_errno(status);
877 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800878
879 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700880 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700881 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882
883 OCFS2_I(inode)->ip_open_count--;
884
885 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700886 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800887
888 brelse(journal->j_bh);
889 journal->j_bh = NULL;
890
891 journal->j_state = OCFS2_JOURNAL_FREE;
892
893// up_write(&journal->j_trans_barrier);
894done:
895 if (inode)
896 iput(inode);
897 mlog_exit_void();
898}
899
900static void ocfs2_clear_journal_error(struct super_block *sb,
901 journal_t *journal,
902 int slot)
903{
904 int olderr;
905
Joel Becker2b4e30f2008-09-03 20:03:41 -0700906 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 if (olderr) {
908 mlog(ML_ERROR, "File system error %d recorded in "
909 "journal %u.\n", olderr, slot);
910 mlog(ML_ERROR, "File system on device %s needs checking.\n",
911 sb->s_id);
912
Joel Becker2b4e30f2008-09-03 20:03:41 -0700913 jbd2_journal_ack_err(journal);
914 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800915 }
916}
917
Sunil Mushran539d8262008-07-14 17:31:10 -0700918int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800919{
920 int status = 0;
921 struct ocfs2_super *osb;
922
923 mlog_entry_void();
924
Julia Lawallb1f35502008-03-04 15:21:05 -0800925 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800926
927 osb = journal->j_osb;
928
Joel Becker2b4e30f2008-09-03 20:03:41 -0700929 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800930 if (status < 0) {
931 mlog(ML_ERROR, "Failed to load journal!\n");
932 goto done;
933 }
934
935 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
936
Sunil Mushran539d8262008-07-14 17:31:10 -0700937 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 if (status < 0) {
939 mlog_errno(status);
940 goto done;
941 }
942
943 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800944 if (!local) {
945 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
946 "ocfs2cmt");
947 if (IS_ERR(osb->commit_task)) {
948 status = PTR_ERR(osb->commit_task);
949 osb->commit_task = NULL;
950 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
951 "error=%d", status);
952 goto done;
953 }
954 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800955 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800956
957done:
958 mlog_exit(status);
959 return status;
960}
961
962
963/* 'full' flag tells us whether we clear out all blocks or if we just
964 * mark the journal clean */
965int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
966{
967 int status;
968
969 mlog_entry_void();
970
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100971 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800972
Joel Becker2b4e30f2008-09-03 20:03:41 -0700973 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 if (status < 0) {
975 mlog_errno(status);
976 goto bail;
977 }
978
Sunil Mushran539d8262008-07-14 17:31:10 -0700979 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 if (status < 0)
981 mlog_errno(status);
982
983bail:
984 mlog_exit(status);
985 return status;
986}
987
Joel Becker553abd02008-02-01 12:03:57 -0800988static int ocfs2_recovery_completed(struct ocfs2_super *osb)
989{
990 int empty;
991 struct ocfs2_recovery_map *rm = osb->recovery_map;
992
993 spin_lock(&osb->osb_lock);
994 empty = (rm->rm_used == 0);
995 spin_unlock(&osb->osb_lock);
996
997 return empty;
998}
999
1000void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1001{
1002 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1003}
1004
Mark Fashehccd979b2005-12-15 14:31:24 -08001005/*
1006 * JBD Might read a cached version of another nodes journal file. We
1007 * don't want this as this file changes often and we get no
1008 * notification on those changes. The only way to be sure that we've
1009 * got the most up to date version of those blocks then is to force
1010 * read them off disk. Just searching through the buffer cache won't
1011 * work as there may be pages backing this file which are still marked
1012 * up to date. We know things can't change on this file underneath us
1013 * as we have the lock by now :)
1014 */
1015static int ocfs2_force_read_journal(struct inode *inode)
1016{
1017 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001018 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -07001019 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001020#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -08001021 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
1022
1023 mlog_entry_void();
1024
Mark Fashehccd979b2005-12-15 14:31:24 -08001025 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
1026
Mark Fasheh8110b072007-03-22 16:53:23 -07001027 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001028 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07001029 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001031 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 if (status < 0) {
1033 mlog_errno(status);
1034 goto bail;
1035 }
1036
1037 if (p_blocks > CONCURRENT_JOURNAL_FILL)
1038 p_blocks = CONCURRENT_JOURNAL_FILL;
1039
Mark Fashehdd4a2c22006-04-12 14:24:05 -07001040 /* We are reading journal data which should not
1041 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -07001042 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
1043 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001044 if (status < 0) {
1045 mlog_errno(status);
1046 goto bail;
1047 }
1048
1049 for(i = 0; i < p_blocks; i++) {
1050 brelse(bhs[i]);
1051 bhs[i] = NULL;
1052 }
1053
1054 v_blkno += p_blocks;
1055 }
1056
1057bail:
1058 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001059 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001060 mlog_exit(status);
1061 return status;
1062}
1063
1064struct ocfs2_la_recovery_item {
1065 struct list_head lri_list;
1066 int lri_slot;
1067 struct ocfs2_dinode *lri_la_dinode;
1068 struct ocfs2_dinode *lri_tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001069 struct ocfs2_quota_recovery *lri_qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001070};
1071
1072/* Does the second half of the recovery process. By this point, the
1073 * node is marked clean and can actually be considered recovered,
1074 * hence it's no longer in the recovery map, but there's still some
1075 * cleanup we can do which shouldn't happen within the recovery thread
1076 * as locking in that context becomes very difficult if we are to take
1077 * recovering nodes into account.
1078 *
1079 * NOTE: This function can and will sleep on recovery of other nodes
1080 * during cluster locking, just like any other ocfs2 process.
1081 */
David Howellsc4028952006-11-22 14:57:56 +00001082void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08001083{
1084 int ret;
David Howellsc4028952006-11-22 14:57:56 +00001085 struct ocfs2_journal *journal =
1086 container_of(work, struct ocfs2_journal, j_recovery_work);
1087 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +02001089 struct ocfs2_la_recovery_item *item, *n;
Jan Kara22053632008-10-20 23:50:38 +02001090 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001091 LIST_HEAD(tmp_la_list);
1092
1093 mlog_entry_void();
1094
1095 mlog(0, "completing recovery from keventd\n");
1096
1097 spin_lock(&journal->j_lock);
1098 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1099 spin_unlock(&journal->j_lock);
1100
Christoph Hellwig800deef2007-05-17 16:03:13 +02001101 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001102 list_del_init(&item->lri_list);
1103
1104 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1105
Jan Kara19ece542008-08-21 20:13:17 +02001106 ocfs2_wait_on_quotas(osb);
1107
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 la_dinode = item->lri_la_dinode;
1109 if (la_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001110 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001111 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001112
1113 ret = ocfs2_complete_local_alloc_recovery(osb,
1114 la_dinode);
1115 if (ret < 0)
1116 mlog_errno(ret);
1117
1118 kfree(la_dinode);
1119 }
1120
1121 tl_dinode = item->lri_tl_dinode;
1122 if (tl_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001123 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001124 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001125
1126 ret = ocfs2_complete_truncate_log_recovery(osb,
1127 tl_dinode);
1128 if (ret < 0)
1129 mlog_errno(ret);
1130
1131 kfree(tl_dinode);
1132 }
1133
1134 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1135 if (ret < 0)
1136 mlog_errno(ret);
1137
Jan Kara22053632008-10-20 23:50:38 +02001138 qrec = item->lri_qrec;
1139 if (qrec) {
1140 mlog(0, "Recovering quota files");
1141 ret = ocfs2_finish_quota_recovery(osb, qrec,
1142 item->lri_slot);
1143 if (ret < 0)
1144 mlog_errno(ret);
1145 /* Recovery info is already freed now */
1146 }
1147
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 kfree(item);
1149 }
1150
1151 mlog(0, "Recovery completion\n");
1152 mlog_exit_void();
1153}
1154
1155/* NOTE: This function always eats your references to la_dinode and
1156 * tl_dinode, either manually on error, or by passing them to
1157 * ocfs2_complete_recovery */
1158static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1159 int slot_num,
1160 struct ocfs2_dinode *la_dinode,
Jan Kara22053632008-10-20 23:50:38 +02001161 struct ocfs2_dinode *tl_dinode,
1162 struct ocfs2_quota_recovery *qrec)
Mark Fashehccd979b2005-12-15 14:31:24 -08001163{
1164 struct ocfs2_la_recovery_item *item;
1165
Sunil Mushranafae00ab2006-04-12 14:37:00 -07001166 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001167 if (!item) {
1168 /* Though we wish to avoid it, we are in fact safe in
1169 * skipping local alloc cleanup as fsck.ocfs2 is more
1170 * than capable of reclaiming unused space. */
1171 if (la_dinode)
1172 kfree(la_dinode);
1173
1174 if (tl_dinode)
1175 kfree(tl_dinode);
1176
Jan Kara22053632008-10-20 23:50:38 +02001177 if (qrec)
1178 ocfs2_free_quota_recovery(qrec);
1179
Mark Fashehccd979b2005-12-15 14:31:24 -08001180 mlog_errno(-ENOMEM);
1181 return;
1182 }
1183
1184 INIT_LIST_HEAD(&item->lri_list);
1185 item->lri_la_dinode = la_dinode;
1186 item->lri_slot = slot_num;
1187 item->lri_tl_dinode = tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001188 item->lri_qrec = qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001189
1190 spin_lock(&journal->j_lock);
1191 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1192 queue_work(ocfs2_wq, &journal->j_recovery_work);
1193 spin_unlock(&journal->j_lock);
1194}
1195
1196/* Called by the mount code to queue recovery the last part of
1197 * recovery for it's own slot. */
1198void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1199{
1200 struct ocfs2_journal *journal = osb->journal;
1201
1202 if (osb->dirty) {
1203 /* No need to queue up our truncate_log as regular
1204 * cleanup will catch that. */
1205 ocfs2_queue_recovery_completion(journal,
1206 osb->slot_num,
1207 osb->local_alloc_copy,
Jan Kara22053632008-10-20 23:50:38 +02001208 NULL,
Mark Fashehccd979b2005-12-15 14:31:24 -08001209 NULL);
1210 ocfs2_schedule_truncate_log_flush(osb, 0);
1211
1212 osb->local_alloc_copy = NULL;
1213 osb->dirty = 0;
1214 }
1215}
1216
Jan Kara22053632008-10-20 23:50:38 +02001217void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1218{
1219 if (osb->quota_rec) {
1220 ocfs2_queue_recovery_completion(osb->journal,
1221 osb->slot_num,
1222 NULL,
1223 NULL,
1224 osb->quota_rec);
1225 osb->quota_rec = NULL;
1226 }
1227}
1228
Mark Fashehccd979b2005-12-15 14:31:24 -08001229static int __ocfs2_recovery_thread(void *arg)
1230{
Jan Kara22053632008-10-20 23:50:38 +02001231 int status, node_num, slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001232 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001233 struct ocfs2_recovery_map *rm = osb->recovery_map;
Jan Kara22053632008-10-20 23:50:38 +02001234 int *rm_quota = NULL;
1235 int rm_quota_used = 0, i;
1236 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001237
1238 mlog_entry_void();
1239
1240 status = ocfs2_wait_on_mount(osb);
1241 if (status < 0) {
1242 goto bail;
1243 }
1244
Jan Kara22053632008-10-20 23:50:38 +02001245 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1246 if (!rm_quota) {
1247 status = -ENOMEM;
1248 goto bail;
1249 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001250restart:
1251 status = ocfs2_super_lock(osb, 1);
1252 if (status < 0) {
1253 mlog_errno(status);
1254 goto bail;
1255 }
1256
Joel Becker553abd02008-02-01 12:03:57 -08001257 spin_lock(&osb->osb_lock);
1258 while (rm->rm_used) {
1259 /* It's always safe to remove entry zero, as we won't
1260 * clear it until ocfs2_recover_node() has succeeded. */
1261 node_num = rm->rm_entries[0];
1262 spin_unlock(&osb->osb_lock);
Jan Kara22053632008-10-20 23:50:38 +02001263 mlog(0, "checking node %d\n", node_num);
1264 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1265 if (slot_num == -ENOENT) {
1266 status = 0;
1267 mlog(0, "no slot for this node, so no recovery"
1268 "required.\n");
1269 goto skip_recovery;
1270 }
1271 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001272
Jan Kara22053632008-10-20 23:50:38 +02001273 /* It is a bit subtle with quota recovery. We cannot do it
1274 * immediately because we have to obtain cluster locks from
1275 * quota files and we also don't want to just skip it because
1276 * then quota usage would be out of sync until some node takes
1277 * the slot. So we remember which nodes need quota recovery
1278 * and when everything else is done, we recover quotas. */
1279 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1280 if (i == rm_quota_used)
1281 rm_quota[rm_quota_used++] = slot_num;
1282
1283 status = ocfs2_recover_node(osb, node_num, slot_num);
1284skip_recovery:
Joel Becker553abd02008-02-01 12:03:57 -08001285 if (!status) {
1286 ocfs2_recovery_map_clear(osb, node_num);
1287 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 mlog(ML_ERROR,
1289 "Error %d recovering node %d on device (%u,%u)!\n",
1290 status, node_num,
1291 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1292 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001293 }
1294
Joel Becker553abd02008-02-01 12:03:57 -08001295 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 }
Joel Becker553abd02008-02-01 12:03:57 -08001297 spin_unlock(&osb->osb_lock);
1298 mlog(0, "All nodes recovered\n");
1299
Sunil Mushran539d8262008-07-14 17:31:10 -07001300 /* Refresh all journal recovery generations from disk */
1301 status = ocfs2_check_journals_nolocks(osb);
1302 status = (status == -EROFS) ? 0 : status;
1303 if (status < 0)
1304 mlog_errno(status);
1305
Jan Kara22053632008-10-20 23:50:38 +02001306 /* Now it is right time to recover quotas... We have to do this under
1307 * superblock lock so that noone can start using the slot (and crash)
1308 * before we recover it */
1309 for (i = 0; i < rm_quota_used; i++) {
1310 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1311 if (IS_ERR(qrec)) {
1312 status = PTR_ERR(qrec);
1313 mlog_errno(status);
1314 continue;
1315 }
1316 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1317 NULL, NULL, qrec);
1318 }
1319
Mark Fashehccd979b2005-12-15 14:31:24 -08001320 ocfs2_super_unlock(osb, 1);
1321
1322 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001323 * node(s) may have disallowd a previos inode delete. Re-processing
1324 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
Jan Kara22053632008-10-20 23:50:38 +02001326 NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001327
1328bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001329 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001330 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001331 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001332 goto restart;
1333 }
1334
1335 osb->recovery_thread_task = NULL;
1336 mb(); /* sync with ocfs2_recovery_thread_running */
1337 wake_up(&osb->recovery_event);
1338
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001339 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001340
Jan Kara22053632008-10-20 23:50:38 +02001341 if (rm_quota)
1342 kfree(rm_quota);
1343
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 mlog_exit(status);
1345 /* no one is callint kthread_stop() for us so the kthread() api
1346 * requires that we call do_exit(). And it isn't exported, but
1347 * complete_and_exit() seems to be a minimal wrapper around it. */
1348 complete_and_exit(NULL, status);
1349 return status;
1350}
1351
1352void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1353{
1354 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1355 node_num, osb->node_num);
1356
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001357 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001358 if (osb->disable_recovery)
1359 goto out;
1360
1361 /* People waiting on recovery will wait on
1362 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001363 if (ocfs2_recovery_map_set(osb, node_num))
1364 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001365
1366 mlog(0, "starting recovery thread...\n");
1367
1368 if (osb->recovery_thread_task)
1369 goto out;
1370
1371 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001372 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001373 if (IS_ERR(osb->recovery_thread_task)) {
1374 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1375 osb->recovery_thread_task = NULL;
1376 }
1377
1378out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001379 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001380 wake_up(&osb->recovery_event);
1381
1382 mlog_exit_void();
1383}
1384
Sunil Mushran539d8262008-07-14 17:31:10 -07001385static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1386 int slot_num,
1387 struct buffer_head **bh,
1388 struct inode **ret_inode)
1389{
1390 int status = -EACCES;
1391 struct inode *inode = NULL;
1392
1393 BUG_ON(slot_num >= osb->max_slots);
1394
1395 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1396 slot_num);
1397 if (!inode || is_bad_inode(inode)) {
1398 mlog_errno(status);
1399 goto bail;
1400 }
1401 SET_INODE_JOURNAL(inode);
1402
Joel Beckerb657c952008-11-13 14:49:11 -08001403 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001404 if (status < 0) {
1405 mlog_errno(status);
1406 goto bail;
1407 }
1408
1409 status = 0;
1410
1411bail:
1412 if (inode) {
1413 if (status || !ret_inode)
1414 iput(inode);
1415 else
1416 *ret_inode = inode;
1417 }
1418 return status;
1419}
1420
Mark Fashehccd979b2005-12-15 14:31:24 -08001421/* Does the actual journal replay and marks the journal inode as
1422 * clean. Will only replay if the journal inode is marked dirty. */
1423static int ocfs2_replay_journal(struct ocfs2_super *osb,
1424 int node_num,
1425 int slot_num)
1426{
1427 int status;
1428 int got_lock = 0;
1429 unsigned int flags;
1430 struct inode *inode = NULL;
1431 struct ocfs2_dinode *fe;
1432 journal_t *journal = NULL;
1433 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001434 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001435
Sunil Mushran539d8262008-07-14 17:31:10 -07001436 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1437 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001438 mlog_errno(status);
1439 goto done;
1440 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001441
1442 fe = (struct ocfs2_dinode *)bh->b_data;
1443 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1444 brelse(bh);
1445 bh = NULL;
1446
1447 /*
1448 * As the fs recovery is asynchronous, there is a small chance that
1449 * another node mounted (and recovered) the slot before the recovery
1450 * thread could get the lock. To handle that, we dirty read the journal
1451 * inode for that slot to get the recovery generation. If it is
1452 * different than what we expected, the slot has been recovered.
1453 * If not, it needs recovery.
1454 */
1455 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1456 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1457 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1458 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1459 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 goto done;
1461 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001462
1463 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001464
Mark Fashehe63aecb62007-10-18 15:30:42 -07001465 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001466 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001467 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 if (status != -ERESTARTSYS)
1469 mlog(ML_ERROR, "Could not lock journal!\n");
1470 goto done;
1471 }
1472 got_lock = 1;
1473
1474 fe = (struct ocfs2_dinode *) bh->b_data;
1475
1476 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001477 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001478
1479 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1480 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001481 /* Refresh recovery generation for the slot */
1482 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001483 goto done;
1484 }
1485
1486 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1487 node_num, slot_num,
1488 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1489
1490 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1491
1492 status = ocfs2_force_read_journal(inode);
1493 if (status < 0) {
1494 mlog_errno(status);
1495 goto done;
1496 }
1497
1498 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001499 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001500 if (journal == NULL) {
1501 mlog(ML_ERROR, "Linux journal layer error\n");
1502 status = -EIO;
1503 goto done;
1504 }
1505
Joel Becker2b4e30f2008-09-03 20:03:41 -07001506 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001507 if (status < 0) {
1508 mlog_errno(status);
1509 if (!igrab(inode))
1510 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001511 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001512 goto done;
1513 }
1514
1515 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1516
1517 /* wipe the journal */
1518 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001519 jbd2_journal_lock_updates(journal);
1520 status = jbd2_journal_flush(journal);
1521 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001522 if (status < 0)
1523 mlog_errno(status);
1524
1525 /* This will mark the node clean */
1526 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1527 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1528 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1529
Sunil Mushran539d8262008-07-14 17:31:10 -07001530 /* Increment recovery generation to indicate successful recovery */
1531 ocfs2_bump_recovery_generation(fe);
1532 osb->slot_recovery_generations[slot_num] =
1533 ocfs2_get_recovery_generation(fe);
1534
Joel Becker13723d02008-10-17 19:25:01 -07001535 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -08001536 status = ocfs2_write_block(osb, bh, inode);
1537 if (status < 0)
1538 mlog_errno(status);
1539
1540 if (!igrab(inode))
1541 BUG();
1542
Joel Becker2b4e30f2008-09-03 20:03:41 -07001543 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001544
1545done:
1546 /* drop the lock on this nodes journal */
1547 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001548 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001549
1550 if (inode)
1551 iput(inode);
1552
Mark Fasheha81cb882008-10-07 14:25:16 -07001553 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001554
1555 mlog_exit(status);
1556 return status;
1557}
1558
1559/*
1560 * Do the most important parts of node recovery:
1561 * - Replay it's journal
1562 * - Stamp a clean local allocator file
1563 * - Stamp a clean truncate log
1564 * - Mark the node clean
1565 *
1566 * If this function completes without error, a node in OCFS2 can be
1567 * said to have been safely recovered. As a result, failure during the
1568 * second part of a nodes recovery process (local alloc recovery) is
1569 * far less concerning.
1570 */
1571static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +02001572 int node_num, int slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -08001573{
1574 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001575 struct ocfs2_dinode *la_copy = NULL;
1576 struct ocfs2_dinode *tl_copy = NULL;
1577
Jan Kara22053632008-10-20 23:50:38 +02001578 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1579 node_num, slot_num, osb->node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001580
1581 /* Should not ever be called to recover ourselves -- in that
1582 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001583 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001584
Mark Fashehccd979b2005-12-15 14:31:24 -08001585 status = ocfs2_replay_journal(osb, node_num, slot_num);
1586 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001587 if (status == -EBUSY) {
1588 mlog(0, "Skipping recovery for slot %u (node %u) "
1589 "as another node has recovered it\n", slot_num,
1590 node_num);
1591 status = 0;
1592 goto done;
1593 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001594 mlog_errno(status);
1595 goto done;
1596 }
1597
1598 /* Stamp a clean local alloc file AFTER recovering the journal... */
1599 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1600 if (status < 0) {
1601 mlog_errno(status);
1602 goto done;
1603 }
1604
1605 /* An error from begin_truncate_log_recovery is not
1606 * serious enough to warrant halting the rest of
1607 * recovery. */
1608 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1609 if (status < 0)
1610 mlog_errno(status);
1611
1612 /* Likewise, this would be a strange but ultimately not so
1613 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001614 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001615 if (status < 0)
1616 mlog_errno(status);
1617
1618 /* This will kfree the memory pointed to by la_copy and tl_copy */
1619 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
Jan Kara22053632008-10-20 23:50:38 +02001620 tl_copy, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001621
1622 status = 0;
1623done:
1624
1625 mlog_exit(status);
1626 return status;
1627}
1628
1629/* Test node liveness by trylocking his journal. If we get the lock,
1630 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1631 * still alive (we couldn't get the lock) and < 0 on error. */
1632static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1633 int slot_num)
1634{
1635 int status, flags;
1636 struct inode *inode = NULL;
1637
1638 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1639 slot_num);
1640 if (inode == NULL) {
1641 mlog(ML_ERROR, "access error\n");
1642 status = -EACCES;
1643 goto bail;
1644 }
1645 if (is_bad_inode(inode)) {
1646 mlog(ML_ERROR, "access error (bad inode)\n");
1647 iput(inode);
1648 inode = NULL;
1649 status = -EACCES;
1650 goto bail;
1651 }
1652 SET_INODE_JOURNAL(inode);
1653
1654 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001655 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001656 if (status < 0) {
1657 if (status != -EAGAIN)
1658 mlog_errno(status);
1659 goto bail;
1660 }
1661
Mark Fashehe63aecb62007-10-18 15:30:42 -07001662 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001663bail:
1664 if (inode)
1665 iput(inode);
1666
1667 return status;
1668}
1669
1670/* Call this underneath ocfs2_super_lock. It also assumes that the
1671 * slot info struct has been updated from disk. */
1672int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1673{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001674 unsigned int node_num;
1675 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001676 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001677 struct buffer_head *bh = NULL;
1678 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001679
1680 /* This is called with the super block cluster lock, so we
1681 * know that the slot map can't change underneath us. */
1682
Joel Beckerd85b20e2008-02-01 12:01:05 -08001683 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001684 /* Read journal inode to get the recovery generation */
1685 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1686 if (status) {
1687 mlog_errno(status);
1688 goto bail;
1689 }
1690 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001691 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001692 brelse(bh);
1693 bh = NULL;
1694
Mark Fasheha1af7d12008-08-19 17:20:28 -07001695 spin_lock(&osb->osb_lock);
1696 osb->slot_recovery_generations[i] = gen;
1697
Sunil Mushran539d8262008-07-14 17:31:10 -07001698 mlog(0, "Slot %u recovery generation is %u\n", i,
1699 osb->slot_recovery_generations[i]);
1700
Mark Fasheha1af7d12008-08-19 17:20:28 -07001701 if (i == osb->slot_num) {
1702 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001703 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001704 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001705
1706 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001707 if (status == -ENOENT) {
1708 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001709 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001710 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001711
Mark Fasheha1af7d12008-08-19 17:20:28 -07001712 if (__ocfs2_recovery_map_test(osb, node_num)) {
1713 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001715 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001716 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001717
1718 /* Ok, we have a slot occupied by another node which
1719 * is not in the recovery map. We trylock his journal
1720 * file here to test if he's alive. */
1721 status = ocfs2_trylock_journal(osb, i);
1722 if (!status) {
1723 /* Since we're called from mount, we know that
1724 * the recovery thread can't race us on
1725 * setting / checking the recovery bits. */
1726 ocfs2_recovery_thread(osb, node_num);
1727 } else if ((status < 0) && (status != -EAGAIN)) {
1728 mlog_errno(status);
1729 goto bail;
1730 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001731 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001732
1733 status = 0;
1734bail:
1735 mlog_exit(status);
1736 return status;
1737}
1738
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001739struct ocfs2_orphan_filldir_priv {
1740 struct inode *head;
1741 struct ocfs2_super *osb;
1742};
1743
1744static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1745 loff_t pos, u64 ino, unsigned type)
1746{
1747 struct ocfs2_orphan_filldir_priv *p = priv;
1748 struct inode *iter;
1749
1750 if (name_len == 1 && !strncmp(".", name, 1))
1751 return 0;
1752 if (name_len == 2 && !strncmp("..", name, 2))
1753 return 0;
1754
1755 /* Skip bad inodes so that recovery can continue */
1756 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001757 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001758 if (IS_ERR(iter))
1759 return 0;
1760
1761 mlog(0, "queue orphan %llu\n",
1762 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1763 /* No locking is required for the next_orphan queue as there
1764 * is only ever a single process doing orphan recovery. */
1765 OCFS2_I(iter)->ip_next_orphan = p->head;
1766 p->head = iter;
1767
1768 return 0;
1769}
1770
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001771static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1772 int slot,
1773 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001774{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001775 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001776 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001777 struct ocfs2_orphan_filldir_priv priv;
1778 loff_t pos = 0;
1779
1780 priv.osb = osb;
1781 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001782
1783 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1784 ORPHAN_DIR_SYSTEM_INODE,
1785 slot);
1786 if (!orphan_dir_inode) {
1787 status = -ENOENT;
1788 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001789 return status;
1790 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001791
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001792 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001793 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001794 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 mlog_errno(status);
1796 goto out;
1797 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001798
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001799 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1800 ocfs2_orphan_filldir);
1801 if (status) {
1802 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001803 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001805
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001806 *head = priv.head;
1807
Mark Fasheha86370f2007-12-03 14:06:23 -08001808out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001809 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001810out:
1811 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001812 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001813 return status;
1814}
1815
1816static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1817 int slot)
1818{
1819 int ret;
1820
1821 spin_lock(&osb->osb_lock);
1822 ret = !osb->osb_orphan_wipes[slot];
1823 spin_unlock(&osb->osb_lock);
1824 return ret;
1825}
1826
1827static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1828 int slot)
1829{
1830 spin_lock(&osb->osb_lock);
1831 /* Mark ourselves such that new processes in delete_inode()
1832 * know to quit early. */
1833 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1834 while (osb->osb_orphan_wipes[slot]) {
1835 /* If any processes are already in the middle of an
1836 * orphan wipe on this dir, then we need to wait for
1837 * them. */
1838 spin_unlock(&osb->osb_lock);
1839 wait_event_interruptible(osb->osb_wipe_event,
1840 ocfs2_orphan_recovery_can_continue(osb, slot));
1841 spin_lock(&osb->osb_lock);
1842 }
1843 spin_unlock(&osb->osb_lock);
1844}
1845
1846static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1847 int slot)
1848{
1849 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1850}
1851
1852/*
1853 * Orphan recovery. Each mounted node has it's own orphan dir which we
1854 * must run during recovery. Our strategy here is to build a list of
1855 * the inodes in the orphan dir and iget/iput them. The VFS does
1856 * (most) of the rest of the work.
1857 *
1858 * Orphan recovery can happen at any time, not just mount so we have a
1859 * couple of extra considerations.
1860 *
1861 * - We grab as many inodes as we can under the orphan dir lock -
1862 * doing iget() outside the orphan dir risks getting a reference on
1863 * an invalid inode.
1864 * - We must be sure not to deadlock with other processes on the
1865 * system wanting to run delete_inode(). This can happen when they go
1866 * to lock the orphan dir and the orphan recovery process attempts to
1867 * iget() inside the orphan dir lock. This can be avoided by
1868 * advertising our state to ocfs2_delete_inode().
1869 */
1870static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1871 int slot)
1872{
1873 int ret = 0;
1874 struct inode *inode = NULL;
1875 struct inode *iter;
1876 struct ocfs2_inode_info *oi;
1877
1878 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1879
1880 ocfs2_mark_recovering_orphan_dir(osb, slot);
1881 ret = ocfs2_queue_orphans(osb, slot, &inode);
1882 ocfs2_clear_recovering_orphan_dir(osb, slot);
1883
1884 /* Error here should be noted, but we want to continue with as
1885 * many queued inodes as we've got. */
1886 if (ret)
1887 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001888
1889 while (inode) {
1890 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001891 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001892
1893 iter = oi->ip_next_orphan;
1894
1895 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001896 /* The remote delete code may have set these on the
1897 * assumption that the other node would wipe them
1898 * successfully. If they are still in the node's
1899 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1901
1902 /* Set the proper information to get us going into
1903 * ocfs2_delete_inode. */
1904 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001905 spin_unlock(&oi->ip_lock);
1906
1907 iput(inode);
1908
1909 inode = iter;
1910 }
1911
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001912 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001913}
1914
Jan Kara19ece542008-08-21 20:13:17 +02001915static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
Mark Fashehccd979b2005-12-15 14:31:24 -08001916{
1917 /* This check is good because ocfs2 will wait on our recovery
1918 * thread before changing it to something other than MOUNTED
1919 * or DISABLED. */
1920 wait_event(osb->osb_mount_event,
Jan Kara19ece542008-08-21 20:13:17 +02001921 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
1922 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
Mark Fashehccd979b2005-12-15 14:31:24 -08001923 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1924
1925 /* If there's an error on mount, then we may never get to the
1926 * MOUNTED flag, but this is set right before
1927 * dismount_volume() so we can trust it. */
1928 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1929 mlog(0, "mount error, exiting!\n");
1930 return -EBUSY;
1931 }
1932
1933 return 0;
1934}
1935
1936static int ocfs2_commit_thread(void *arg)
1937{
1938 int status;
1939 struct ocfs2_super *osb = arg;
1940 struct ocfs2_journal *journal = osb->journal;
1941
1942 /* we can trust j_num_trans here because _should_stop() is only set in
1943 * shutdown and nobody other than ourselves should be able to start
1944 * transactions. committing on shutdown might take a few iterations
1945 * as final transactions put deleted inodes on the list */
1946 while (!(kthread_should_stop() &&
1947 atomic_read(&journal->j_num_trans) == 0)) {
1948
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001949 wait_event_interruptible(osb->checkpoint_event,
1950 atomic_read(&journal->j_num_trans)
1951 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001952
1953 status = ocfs2_commit_cache(osb);
1954 if (status < 0)
1955 mlog_errno(status);
1956
1957 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1958 mlog(ML_KTHREAD,
1959 "commit_thread: %u transactions pending on "
1960 "shutdown\n",
1961 atomic_read(&journal->j_num_trans));
1962 }
1963 }
1964
1965 return 0;
1966}
1967
Sunil Mushran539d8262008-07-14 17:31:10 -07001968/* Reads all the journal inodes without taking any cluster locks. Used
1969 * for hard readonly access to determine whether any journal requires
1970 * recovery. Also used to refresh the recovery generation numbers after
1971 * a journal has been recovered by another node.
1972 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001973int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1974{
1975 int ret = 0;
1976 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001977 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001978 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001979 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001980
1981 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001982 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1983 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001984 mlog_errno(ret);
1985 goto out;
1986 }
1987
1988 di = (struct ocfs2_dinode *) di_bh->b_data;
1989
Sunil Mushran539d8262008-07-14 17:31:10 -07001990 osb->slot_recovery_generations[slot] =
1991 ocfs2_get_recovery_generation(di);
1992
Mark Fashehccd979b2005-12-15 14:31:24 -08001993 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1994 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001995 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001996
1997 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001998 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001999 }
2000
2001out:
Sunil Mushran539d8262008-07-14 17:31:10 -07002002 if (journal_dirty)
2003 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08002004 return ret;
2005}