blob: 3b54dba0f74b46f35c20fd81c2074dd038fb32de [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
79
Joel Becker553abd02008-02-01 12:03:57 -080080
81/*
82 * The recovery_list is a simple linked list of node numbers to recover.
83 * It is protected by the recovery_lock.
84 */
85
86struct ocfs2_recovery_map {
Joel Beckerfc881fa2008-02-01 12:04:48 -080087 unsigned int rm_used;
Joel Becker553abd02008-02-01 12:03:57 -080088 unsigned int *rm_entries;
89};
90
91int ocfs2_recovery_init(struct ocfs2_super *osb)
92{
93 struct ocfs2_recovery_map *rm;
94
95 mutex_init(&osb->recovery_lock);
96 osb->disable_recovery = 0;
97 osb->recovery_thread_task = NULL;
98 init_waitqueue_head(&osb->recovery_event);
99
100 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
101 osb->max_slots * sizeof(unsigned int),
102 GFP_KERNEL);
103 if (!rm) {
104 mlog_errno(-ENOMEM);
105 return -ENOMEM;
106 }
107
108 rm->rm_entries = (unsigned int *)((char *)rm +
109 sizeof(struct ocfs2_recovery_map));
110 osb->recovery_map = rm;
111
112 return 0;
113}
114
115/* we can't grab the goofy sem lock from inside wait_event, so we use
116 * memory barriers to make sure that we'll see the null task before
117 * being woken up */
118static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
119{
120 mb();
121 return osb->recovery_thread_task != NULL;
122}
123
124void ocfs2_recovery_exit(struct ocfs2_super *osb)
125{
126 struct ocfs2_recovery_map *rm;
127
128 /* disable any new recovery threads and wait for any currently
129 * running ones to exit. Do this before setting the vol_state. */
130 mutex_lock(&osb->recovery_lock);
131 osb->disable_recovery = 1;
132 mutex_unlock(&osb->recovery_lock);
133 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
134
135 /* At this point, we know that no more recovery threads can be
136 * launched, so wait for any recovery completion work to
137 * complete. */
138 flush_workqueue(ocfs2_wq);
139
140 /*
141 * Now that recovery is shut down, and the osb is about to be
142 * freed, the osb_lock is not taken here.
143 */
144 rm = osb->recovery_map;
145 /* XXX: Should we bug if there are dirty entries? */
146
147 kfree(rm);
148}
149
150static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
151 unsigned int node_num)
152{
153 int i;
154 struct ocfs2_recovery_map *rm = osb->recovery_map;
155
156 assert_spin_locked(&osb->osb_lock);
157
158 for (i = 0; i < rm->rm_used; i++) {
159 if (rm->rm_entries[i] == node_num)
160 return 1;
161 }
162
163 return 0;
164}
165
166/* Behaves like test-and-set. Returns the previous value */
167static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
168 unsigned int node_num)
169{
170 struct ocfs2_recovery_map *rm = osb->recovery_map;
171
172 spin_lock(&osb->osb_lock);
173 if (__ocfs2_recovery_map_test(osb, node_num)) {
174 spin_unlock(&osb->osb_lock);
175 return 1;
176 }
177
178 /* XXX: Can this be exploited? Not from o2dlm... */
179 BUG_ON(rm->rm_used >= osb->max_slots);
180
181 rm->rm_entries[rm->rm_used] = node_num;
182 rm->rm_used++;
183 spin_unlock(&osb->osb_lock);
184
185 return 0;
186}
187
188static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
189 unsigned int node_num)
190{
191 int i;
192 struct ocfs2_recovery_map *rm = osb->recovery_map;
193
194 spin_lock(&osb->osb_lock);
195
196 for (i = 0; i < rm->rm_used; i++) {
197 if (rm->rm_entries[i] == node_num)
198 break;
199 }
200
201 if (i < rm->rm_used) {
202 /* XXX: be careful with the pointer math */
203 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
204 (rm->rm_used - i - 1) * sizeof(unsigned int));
205 rm->rm_used--;
206 }
207
208 spin_unlock(&osb->osb_lock);
209}
210
Mark Fashehccd979b2005-12-15 14:31:24 -0800211static int ocfs2_commit_cache(struct ocfs2_super *osb)
212{
213 int status = 0;
214 unsigned int flushed;
215 unsigned long old_id;
216 struct ocfs2_journal *journal = NULL;
217
218 mlog_entry_void();
219
220 journal = osb->journal;
221
222 /* Flush all pending commits and checkpoint the journal. */
223 down_write(&journal->j_trans_barrier);
224
225 if (atomic_read(&journal->j_num_trans) == 0) {
226 up_write(&journal->j_trans_barrier);
227 mlog(0, "No transactions for me to flush!\n");
228 goto finally;
229 }
230
Joel Becker2b4e30f2008-09-03 20:03:41 -0700231 jbd2_journal_lock_updates(journal->j_journal);
232 status = jbd2_journal_flush(journal->j_journal);
233 jbd2_journal_unlock_updates(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800234 if (status < 0) {
235 up_write(&journal->j_trans_barrier);
236 mlog_errno(status);
237 goto finally;
238 }
239
240 old_id = ocfs2_inc_trans_id(journal);
241
242 flushed = atomic_read(&journal->j_num_trans);
243 atomic_set(&journal->j_num_trans, 0);
244 up_write(&journal->j_trans_barrier);
245
246 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
247 journal->j_trans_id, flushed);
248
Mark Fasheh34d024f2007-09-24 15:56:19 -0700249 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800250 wake_up(&journal->j_checkpointed);
251finally:
252 mlog_exit(status);
253 return status;
254}
255
Mark Fashehccd979b2005-12-15 14:31:24 -0800256/* pass it NULL and it will allocate a new handle object for you. If
257 * you pass it a handle however, it may still return error, in which
258 * case it has free'd the passed handle for you. */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700259handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
Mark Fashehccd979b2005-12-15 14:31:24 -0800260{
Mark Fashehccd979b2005-12-15 14:31:24 -0800261 journal_t *journal = osb->journal->j_journal;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700262 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800263
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100264 BUG_ON(!osb || !osb->journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800265
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700266 if (ocfs2_is_hard_readonly(osb))
267 return ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800268
269 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
270 BUG_ON(max_buffs <= 0);
271
Jan Kara90e86a62008-08-27 22:30:28 +0200272 /* Nested transaction? Just return the handle... */
273 if (journal_current_handle())
274 return jbd2_journal_start(journal, max_buffs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800275
Mark Fashehccd979b2005-12-15 14:31:24 -0800276 down_read(&osb->journal->j_trans_barrier);
277
Joel Becker2b4e30f2008-09-03 20:03:41 -0700278 handle = jbd2_journal_start(journal, max_buffs);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700279 if (IS_ERR(handle)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800280 up_read(&osb->journal->j_trans_barrier);
281
Mark Fasheh1fabe142006-10-09 18:11:45 -0700282 mlog_errno(PTR_ERR(handle));
Mark Fashehccd979b2005-12-15 14:31:24 -0800283
284 if (is_journal_aborted(journal)) {
285 ocfs2_abort(osb->sb, "Detected aborted journal");
Mark Fasheh1fabe142006-10-09 18:11:45 -0700286 handle = ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800287 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800288 } else {
289 if (!ocfs2_mount_local(osb))
290 atomic_inc(&(osb->journal->j_num_trans));
291 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800292
Mark Fashehccd979b2005-12-15 14:31:24 -0800293 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800294}
295
Mark Fasheh1fabe142006-10-09 18:11:45 -0700296int ocfs2_commit_trans(struct ocfs2_super *osb,
297 handle_t *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800298{
Jan Kara90e86a62008-08-27 22:30:28 +0200299 int ret, nested;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700300 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800301
302 BUG_ON(!handle);
303
Jan Kara90e86a62008-08-27 22:30:28 +0200304 nested = handle->h_ref > 1;
Joel Becker2b4e30f2008-09-03 20:03:41 -0700305 ret = jbd2_journal_stop(handle);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700306 if (ret < 0)
307 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800308
Jan Kara90e86a62008-08-27 22:30:28 +0200309 if (!nested)
310 up_read(&journal->j_trans_barrier);
Mark Fashehccd979b2005-12-15 14:31:24 -0800311
Mark Fasheh1fabe142006-10-09 18:11:45 -0700312 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800313}
314
315/*
316 * 'nblocks' is what you want to add to the current
317 * transaction. extend_trans will either extend the current handle by
318 * nblocks, or commit it and start a new one with nblocks credits.
319 *
Joel Becker2b4e30f2008-09-03 20:03:41 -0700320 * This might call jbd2_journal_restart() which will commit dirty buffers
Mark Fashehe8aed342007-12-03 16:43:01 -0800321 * and then restart the transaction. Before calling
322 * ocfs2_extend_trans(), any changed blocks should have been
323 * dirtied. After calling it, all blocks which need to be changed must
324 * go through another set of journal_access/journal_dirty calls.
325 *
Mark Fashehccd979b2005-12-15 14:31:24 -0800326 * WARNING: This will not release any semaphores or disk locks taken
327 * during the transaction, so make sure they were taken *before*
328 * start_trans or we'll have ordering deadlocks.
329 *
330 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
331 * good because transaction ids haven't yet been recorded on the
332 * cluster locks associated with this handle.
333 */
Mark Fasheh1fc58142006-10-05 14:15:36 -0700334int ocfs2_extend_trans(handle_t *handle, int nblocks)
Mark Fashehccd979b2005-12-15 14:31:24 -0800335{
336 int status;
337
338 BUG_ON(!handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800339 BUG_ON(!nblocks);
340
341 mlog_entry_void();
342
343 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
344
Joel Beckere407e392008-06-12 22:35:39 -0700345#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fasheh0879c582007-12-03 16:42:19 -0800346 status = 1;
347#else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700348 status = jbd2_journal_extend(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800349 if (status < 0) {
350 mlog_errno(status);
351 goto bail;
352 }
Mark Fasheh0879c582007-12-03 16:42:19 -0800353#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800354
355 if (status > 0) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700356 mlog(0,
357 "jbd2_journal_extend failed, trying "
358 "jbd2_journal_restart\n");
359 status = jbd2_journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800361 mlog_errno(status);
362 goto bail;
363 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700364 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800365
366 status = 0;
367bail:
368
369 mlog_exit(status);
370 return status;
371}
372
Joel Becker50655ae2008-09-11 15:53:07 -0700373struct ocfs2_triggers {
374 struct jbd2_buffer_trigger_type ot_triggers;
375 int ot_offset;
376};
377
378static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
379{
380 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
381}
382
383static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
384 struct buffer_head *bh,
385 void *data, size_t size)
386{
387 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
388
389 /*
390 * We aren't guaranteed to have the superblock here, so we
391 * must unconditionally compute the ecc data.
392 * __ocfs2_journal_access() will only set the triggers if
393 * metaecc is enabled.
394 */
395 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
396}
397
398/*
399 * Quota blocks have their own trigger because the struct ocfs2_block_check
400 * offset depends on the blocksize.
401 */
402static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
403 struct buffer_head *bh,
404 void *data, size_t size)
405{
406 struct ocfs2_disk_dqtrailer *dqt =
407 ocfs2_block_dqtrailer(size, data);
408
409 /*
410 * We aren't guaranteed to have the superblock here, so we
411 * must unconditionally compute the ecc data.
412 * __ocfs2_journal_access() will only set the triggers if
413 * metaecc is enabled.
414 */
415 ocfs2_block_check_compute(data, size, &dqt->dq_check);
416}
417
418static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
419 struct buffer_head *bh)
420{
421 mlog(ML_ERROR,
422 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
423 "bh->b_blocknr = %llu\n",
424 (unsigned long)bh,
425 (unsigned long long)bh->b_blocknr);
426
427 /* We aren't guaranteed to have the superblock here - but if we
428 * don't, it'll just crash. */
429 ocfs2_error(bh->b_assoc_map->host->i_sb,
430 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
431}
432
433static struct ocfs2_triggers di_triggers = {
434 .ot_triggers = {
435 .t_commit = ocfs2_commit_trigger,
436 .t_abort = ocfs2_abort_trigger,
437 },
438 .ot_offset = offsetof(struct ocfs2_dinode, i_check),
439};
440
441static struct ocfs2_triggers eb_triggers = {
442 .ot_triggers = {
443 .t_commit = ocfs2_commit_trigger,
444 .t_abort = ocfs2_abort_trigger,
445 },
446 .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
447};
448
449static struct ocfs2_triggers gd_triggers = {
450 .ot_triggers = {
451 .t_commit = ocfs2_commit_trigger,
452 .t_abort = ocfs2_abort_trigger,
453 },
454 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
455};
456
457static struct ocfs2_triggers xb_triggers = {
458 .ot_triggers = {
459 .t_commit = ocfs2_commit_trigger,
460 .t_abort = ocfs2_abort_trigger,
461 },
462 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
463};
464
465static struct ocfs2_triggers dq_triggers = {
466 .ot_triggers = {
467 .t_commit = ocfs2_dq_commit_trigger,
468 .t_abort = ocfs2_abort_trigger,
469 },
470};
471
472static int __ocfs2_journal_access(handle_t *handle,
473 struct inode *inode,
474 struct buffer_head *bh,
475 struct ocfs2_triggers *triggers,
476 int type)
Mark Fashehccd979b2005-12-15 14:31:24 -0800477{
478 int status;
479
480 BUG_ON(!inode);
481 BUG_ON(!handle);
482 BUG_ON(!bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800483
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800484 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800485 (unsigned long long)bh->b_blocknr, type,
486 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
487 "OCFS2_JOURNAL_ACCESS_CREATE" :
488 "OCFS2_JOURNAL_ACCESS_WRITE",
489 bh->b_size);
490
491 /* we can safely remove this assertion after testing. */
492 if (!buffer_uptodate(bh)) {
493 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
494 mlog(ML_ERROR, "b_blocknr=%llu\n",
495 (unsigned long long)bh->b_blocknr);
496 BUG();
497 }
498
499 /* Set the current transaction information on the inode so
500 * that the locking code knows whether it can drop it's locks
501 * on this inode or not. We're protected from the commit
502 * thread updating the current transaction id until
503 * ocfs2_commit_trans() because ocfs2_start_trans() took
504 * j_trans_barrier for us. */
505 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
506
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800507 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800508 switch (type) {
509 case OCFS2_JOURNAL_ACCESS_CREATE:
510 case OCFS2_JOURNAL_ACCESS_WRITE:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700511 status = jbd2_journal_get_write_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800512 break;
513
514 case OCFS2_JOURNAL_ACCESS_UNDO:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700515 status = jbd2_journal_get_undo_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800516 break;
517
518 default:
519 status = -EINVAL;
520 mlog(ML_ERROR, "Uknown access type!\n");
521 }
Joel Becker50655ae2008-09-11 15:53:07 -0700522 if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
523 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800524 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800525
526 if (status < 0)
527 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
528 status, type);
529
530 mlog_exit(status);
531 return status;
532}
533
Joel Becker50655ae2008-09-11 15:53:07 -0700534int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
535 struct buffer_head *bh, int type)
536{
537 return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
538 type);
539}
540
541int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
542 struct buffer_head *bh, int type)
543{
544 return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
545 type);
546}
547
548int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
549 struct buffer_head *bh, int type)
550{
551 return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
552 type);
553}
554
555int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
556 struct buffer_head *bh, int type)
557{
558 /* Right now, nothing for dirblocks */
559 return __ocfs2_journal_access(handle, inode, bh, NULL, type);
560}
561
562int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
563 struct buffer_head *bh, int type)
564{
565 return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
566 type);
567}
568
569int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
570 struct buffer_head *bh, int type)
571{
572 return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
573 type);
574}
575
576int ocfs2_journal_access(handle_t *handle, struct inode *inode,
577 struct buffer_head *bh, int type)
578{
579 return __ocfs2_journal_access(handle, inode, bh, NULL, type);
580}
581
Mark Fasheh1fabe142006-10-09 18:11:45 -0700582int ocfs2_journal_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800583 struct buffer_head *bh)
584{
585 int status;
586
Mark Fashehccd979b2005-12-15 14:31:24 -0800587 mlog_entry("(bh->b_blocknr=%llu)\n",
588 (unsigned long long)bh->b_blocknr);
589
Joel Becker2b4e30f2008-09-03 20:03:41 -0700590 status = jbd2_journal_dirty_metadata(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800591 if (status < 0)
592 mlog(ML_ERROR, "Could not dirty metadata buffer. "
593 "(bh->b_blocknr=%llu)\n",
594 (unsigned long long)bh->b_blocknr);
595
596 mlog_exit(status);
597 return status;
598}
599
Joel Becker2b4e30f2008-09-03 20:03:41 -0700600#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800601
602void ocfs2_set_journal_params(struct ocfs2_super *osb)
603{
604 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800605 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
606
607 if (osb->osb_commit_interval)
608 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800609
610 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800611 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800612 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700613 journal->j_flags |= JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800614 else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700615 journal->j_flags &= ~JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800616 spin_unlock(&journal->j_state_lock);
617}
618
619int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
620{
621 int status = -1;
622 struct inode *inode = NULL; /* the journal inode */
623 journal_t *j_journal = NULL;
624 struct ocfs2_dinode *di = NULL;
625 struct buffer_head *bh = NULL;
626 struct ocfs2_super *osb;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700627 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800628
629 mlog_entry_void();
630
631 BUG_ON(!journal);
632
633 osb = journal->j_osb;
634
635 /* already have the inode for our journal */
636 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
637 osb->slot_num);
638 if (inode == NULL) {
639 status = -EACCES;
640 mlog_errno(status);
641 goto done;
642 }
643 if (is_bad_inode(inode)) {
644 mlog(ML_ERROR, "access error (bad inode)\n");
645 iput(inode);
646 inode = NULL;
647 status = -EACCES;
648 goto done;
649 }
650
651 SET_INODE_JOURNAL(inode);
652 OCFS2_I(inode)->ip_open_count++;
653
Mark Fasheh6eff5792006-01-18 10:31:47 -0800654 /* Skip recovery waits here - journal inode metadata never
655 * changes in a live cluster so it can be considered an
656 * exception to the rule. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700657 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800658 if (status < 0) {
659 if (status != -ERESTARTSYS)
660 mlog(ML_ERROR, "Could not get lock on journal!\n");
661 goto done;
662 }
663
Mark Fashehe63aecb62007-10-18 15:30:42 -0700664 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 di = (struct ocfs2_dinode *)bh->b_data;
666
667 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
668 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
669 inode->i_size);
670 status = -EINVAL;
671 goto done;
672 }
673
674 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800675 mlog(0, "inode->i_blocks = %llu\n",
676 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800677 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
678
679 /* call the kernels journal init function now */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700680 j_journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800681 if (j_journal == NULL) {
682 mlog(ML_ERROR, "Linux journal layer error\n");
683 status = -EINVAL;
684 goto done;
685 }
686
Joel Becker2b4e30f2008-09-03 20:03:41 -0700687 mlog(0, "Returned from jbd2_journal_init_inode\n");
Mark Fashehccd979b2005-12-15 14:31:24 -0800688 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
689
690 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
691 OCFS2_JOURNAL_DIRTY_FL);
692
693 journal->j_journal = j_journal;
694 journal->j_inode = inode;
695 journal->j_bh = bh;
696
697 ocfs2_set_journal_params(osb);
698
699 journal->j_state = OCFS2_JOURNAL_LOADED;
700
701 status = 0;
702done:
703 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700704 if (inode_lock)
705 ocfs2_inode_unlock(inode, 1);
Mark Fasheha81cb882008-10-07 14:25:16 -0700706 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800707 if (inode) {
708 OCFS2_I(inode)->ip_open_count--;
709 iput(inode);
710 }
711 }
712
713 mlog_exit(status);
714 return status;
715}
716
Sunil Mushran539d8262008-07-14 17:31:10 -0700717static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
718{
719 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
720}
721
722static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
723{
724 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
725}
726
Mark Fashehccd979b2005-12-15 14:31:24 -0800727static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -0700728 int dirty, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800729{
730 int status;
731 unsigned int flags;
732 struct ocfs2_journal *journal = osb->journal;
733 struct buffer_head *bh = journal->j_bh;
734 struct ocfs2_dinode *fe;
735
736 mlog_entry_void();
737
738 fe = (struct ocfs2_dinode *)bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800739
740 /* The journal bh on the osb always comes from ocfs2_journal_init()
741 * and was validated there inside ocfs2_inode_lock_full(). It's a
742 * code bug if we mess it up. */
743 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800744
745 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
746 if (dirty)
747 flags |= OCFS2_JOURNAL_DIRTY_FL;
748 else
749 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
750 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
751
Sunil Mushran539d8262008-07-14 17:31:10 -0700752 if (replayed)
753 ocfs2_bump_recovery_generation(fe);
754
Joel Becker13723d02008-10-17 19:25:01 -0700755 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -0800756 status = ocfs2_write_block(osb, bh, journal->j_inode);
757 if (status < 0)
758 mlog_errno(status);
759
Mark Fashehccd979b2005-12-15 14:31:24 -0800760 mlog_exit(status);
761 return status;
762}
763
764/*
765 * If the journal has been kmalloc'd it needs to be freed after this
766 * call.
767 */
768void ocfs2_journal_shutdown(struct ocfs2_super *osb)
769{
770 struct ocfs2_journal *journal = NULL;
771 int status = 0;
772 struct inode *inode = NULL;
773 int num_running_trans = 0;
774
775 mlog_entry_void();
776
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100777 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800778
779 journal = osb->journal;
780 if (!journal)
781 goto done;
782
783 inode = journal->j_inode;
784
785 if (journal->j_state != OCFS2_JOURNAL_LOADED)
786 goto done;
787
Joel Becker2b4e30f2008-09-03 20:03:41 -0700788 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800789 if (!igrab(inode))
790 BUG();
791
792 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
793 if (num_running_trans > 0)
794 mlog(0, "Shutting down journal: must wait on %d "
795 "running transactions!\n",
796 num_running_trans);
797
798 /* Do a commit_cache here. It will flush our journal, *and*
799 * release any locks that are still held.
800 * set the SHUTDOWN flag and release the trans lock.
801 * the commit thread will take the trans lock for us below. */
802 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
803
804 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
805 * drop the trans_lock (which we want to hold until we
806 * completely destroy the journal. */
807 if (osb->commit_task) {
808 /* Wait for the commit thread */
809 mlog(0, "Waiting for ocfs2commit to exit....\n");
810 kthread_stop(osb->commit_task);
811 osb->commit_task = NULL;
812 }
813
814 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
815
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800816 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700817 jbd2_journal_lock_updates(journal->j_journal);
818 status = jbd2_journal_flush(journal->j_journal);
819 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800820 if (status < 0)
821 mlog_errno(status);
822 }
823
824 if (status == 0) {
825 /*
826 * Do not toggle if flush was unsuccessful otherwise
827 * will leave dirty metadata in a "clean" journal
828 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700829 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800830 if (status < 0)
831 mlog_errno(status);
832 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800833
834 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700835 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700836 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800837
838 OCFS2_I(inode)->ip_open_count--;
839
840 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700841 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800842
843 brelse(journal->j_bh);
844 journal->j_bh = NULL;
845
846 journal->j_state = OCFS2_JOURNAL_FREE;
847
848// up_write(&journal->j_trans_barrier);
849done:
850 if (inode)
851 iput(inode);
852 mlog_exit_void();
853}
854
855static void ocfs2_clear_journal_error(struct super_block *sb,
856 journal_t *journal,
857 int slot)
858{
859 int olderr;
860
Joel Becker2b4e30f2008-09-03 20:03:41 -0700861 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800862 if (olderr) {
863 mlog(ML_ERROR, "File system error %d recorded in "
864 "journal %u.\n", olderr, slot);
865 mlog(ML_ERROR, "File system on device %s needs checking.\n",
866 sb->s_id);
867
Joel Becker2b4e30f2008-09-03 20:03:41 -0700868 jbd2_journal_ack_err(journal);
869 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800870 }
871}
872
Sunil Mushran539d8262008-07-14 17:31:10 -0700873int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800874{
875 int status = 0;
876 struct ocfs2_super *osb;
877
878 mlog_entry_void();
879
Julia Lawallb1f35502008-03-04 15:21:05 -0800880 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800881
882 osb = journal->j_osb;
883
Joel Becker2b4e30f2008-09-03 20:03:41 -0700884 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800885 if (status < 0) {
886 mlog(ML_ERROR, "Failed to load journal!\n");
887 goto done;
888 }
889
890 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
891
Sunil Mushran539d8262008-07-14 17:31:10 -0700892 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 if (status < 0) {
894 mlog_errno(status);
895 goto done;
896 }
897
898 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800899 if (!local) {
900 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
901 "ocfs2cmt");
902 if (IS_ERR(osb->commit_task)) {
903 status = PTR_ERR(osb->commit_task);
904 osb->commit_task = NULL;
905 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
906 "error=%d", status);
907 goto done;
908 }
909 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800911
912done:
913 mlog_exit(status);
914 return status;
915}
916
917
918/* 'full' flag tells us whether we clear out all blocks or if we just
919 * mark the journal clean */
920int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
921{
922 int status;
923
924 mlog_entry_void();
925
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100926 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800927
Joel Becker2b4e30f2008-09-03 20:03:41 -0700928 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 if (status < 0) {
930 mlog_errno(status);
931 goto bail;
932 }
933
Sunil Mushran539d8262008-07-14 17:31:10 -0700934 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800935 if (status < 0)
936 mlog_errno(status);
937
938bail:
939 mlog_exit(status);
940 return status;
941}
942
Joel Becker553abd02008-02-01 12:03:57 -0800943static int ocfs2_recovery_completed(struct ocfs2_super *osb)
944{
945 int empty;
946 struct ocfs2_recovery_map *rm = osb->recovery_map;
947
948 spin_lock(&osb->osb_lock);
949 empty = (rm->rm_used == 0);
950 spin_unlock(&osb->osb_lock);
951
952 return empty;
953}
954
955void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
956{
957 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
958}
959
Mark Fashehccd979b2005-12-15 14:31:24 -0800960/*
961 * JBD Might read a cached version of another nodes journal file. We
962 * don't want this as this file changes often and we get no
963 * notification on those changes. The only way to be sure that we've
964 * got the most up to date version of those blocks then is to force
965 * read them off disk. Just searching through the buffer cache won't
966 * work as there may be pages backing this file which are still marked
967 * up to date. We know things can't change on this file underneath us
968 * as we have the lock by now :)
969 */
970static int ocfs2_force_read_journal(struct inode *inode)
971{
972 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800973 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700974 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800975#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800976 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
977
978 mlog_entry_void();
979
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
981
Mark Fasheh8110b072007-03-22 16:53:23 -0700982 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800983 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700984 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800985 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800986 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 if (status < 0) {
988 mlog_errno(status);
989 goto bail;
990 }
991
992 if (p_blocks > CONCURRENT_JOURNAL_FILL)
993 p_blocks = CONCURRENT_JOURNAL_FILL;
994
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700995 /* We are reading journal data which should not
996 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -0700997 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
998 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800999 if (status < 0) {
1000 mlog_errno(status);
1001 goto bail;
1002 }
1003
1004 for(i = 0; i < p_blocks; i++) {
1005 brelse(bhs[i]);
1006 bhs[i] = NULL;
1007 }
1008
1009 v_blkno += p_blocks;
1010 }
1011
1012bail:
1013 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001014 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001015 mlog_exit(status);
1016 return status;
1017}
1018
1019struct ocfs2_la_recovery_item {
1020 struct list_head lri_list;
1021 int lri_slot;
1022 struct ocfs2_dinode *lri_la_dinode;
1023 struct ocfs2_dinode *lri_tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001024 struct ocfs2_quota_recovery *lri_qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001025};
1026
1027/* Does the second half of the recovery process. By this point, the
1028 * node is marked clean and can actually be considered recovered,
1029 * hence it's no longer in the recovery map, but there's still some
1030 * cleanup we can do which shouldn't happen within the recovery thread
1031 * as locking in that context becomes very difficult if we are to take
1032 * recovering nodes into account.
1033 *
1034 * NOTE: This function can and will sleep on recovery of other nodes
1035 * during cluster locking, just like any other ocfs2 process.
1036 */
David Howellsc4028952006-11-22 14:57:56 +00001037void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08001038{
1039 int ret;
David Howellsc4028952006-11-22 14:57:56 +00001040 struct ocfs2_journal *journal =
1041 container_of(work, struct ocfs2_journal, j_recovery_work);
1042 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +02001044 struct ocfs2_la_recovery_item *item, *n;
Jan Kara22053632008-10-20 23:50:38 +02001045 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 LIST_HEAD(tmp_la_list);
1047
1048 mlog_entry_void();
1049
1050 mlog(0, "completing recovery from keventd\n");
1051
1052 spin_lock(&journal->j_lock);
1053 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1054 spin_unlock(&journal->j_lock);
1055
Christoph Hellwig800deef2007-05-17 16:03:13 +02001056 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001057 list_del_init(&item->lri_list);
1058
1059 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1060
Jan Kara19ece542008-08-21 20:13:17 +02001061 ocfs2_wait_on_quotas(osb);
1062
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 la_dinode = item->lri_la_dinode;
1064 if (la_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001065 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001066 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001067
1068 ret = ocfs2_complete_local_alloc_recovery(osb,
1069 la_dinode);
1070 if (ret < 0)
1071 mlog_errno(ret);
1072
1073 kfree(la_dinode);
1074 }
1075
1076 tl_dinode = item->lri_tl_dinode;
1077 if (tl_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001078 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001079 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001080
1081 ret = ocfs2_complete_truncate_log_recovery(osb,
1082 tl_dinode);
1083 if (ret < 0)
1084 mlog_errno(ret);
1085
1086 kfree(tl_dinode);
1087 }
1088
1089 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1090 if (ret < 0)
1091 mlog_errno(ret);
1092
Jan Kara22053632008-10-20 23:50:38 +02001093 qrec = item->lri_qrec;
1094 if (qrec) {
1095 mlog(0, "Recovering quota files");
1096 ret = ocfs2_finish_quota_recovery(osb, qrec,
1097 item->lri_slot);
1098 if (ret < 0)
1099 mlog_errno(ret);
1100 /* Recovery info is already freed now */
1101 }
1102
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 kfree(item);
1104 }
1105
1106 mlog(0, "Recovery completion\n");
1107 mlog_exit_void();
1108}
1109
1110/* NOTE: This function always eats your references to la_dinode and
1111 * tl_dinode, either manually on error, or by passing them to
1112 * ocfs2_complete_recovery */
1113static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1114 int slot_num,
1115 struct ocfs2_dinode *la_dinode,
Jan Kara22053632008-10-20 23:50:38 +02001116 struct ocfs2_dinode *tl_dinode,
1117 struct ocfs2_quota_recovery *qrec)
Mark Fashehccd979b2005-12-15 14:31:24 -08001118{
1119 struct ocfs2_la_recovery_item *item;
1120
Sunil Mushranafae00ab2006-04-12 14:37:00 -07001121 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001122 if (!item) {
1123 /* Though we wish to avoid it, we are in fact safe in
1124 * skipping local alloc cleanup as fsck.ocfs2 is more
1125 * than capable of reclaiming unused space. */
1126 if (la_dinode)
1127 kfree(la_dinode);
1128
1129 if (tl_dinode)
1130 kfree(tl_dinode);
1131
Jan Kara22053632008-10-20 23:50:38 +02001132 if (qrec)
1133 ocfs2_free_quota_recovery(qrec);
1134
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 mlog_errno(-ENOMEM);
1136 return;
1137 }
1138
1139 INIT_LIST_HEAD(&item->lri_list);
1140 item->lri_la_dinode = la_dinode;
1141 item->lri_slot = slot_num;
1142 item->lri_tl_dinode = tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001143 item->lri_qrec = qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001144
1145 spin_lock(&journal->j_lock);
1146 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1147 queue_work(ocfs2_wq, &journal->j_recovery_work);
1148 spin_unlock(&journal->j_lock);
1149}
1150
1151/* Called by the mount code to queue recovery the last part of
1152 * recovery for it's own slot. */
1153void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1154{
1155 struct ocfs2_journal *journal = osb->journal;
1156
1157 if (osb->dirty) {
1158 /* No need to queue up our truncate_log as regular
1159 * cleanup will catch that. */
1160 ocfs2_queue_recovery_completion(journal,
1161 osb->slot_num,
1162 osb->local_alloc_copy,
Jan Kara22053632008-10-20 23:50:38 +02001163 NULL,
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 NULL);
1165 ocfs2_schedule_truncate_log_flush(osb, 0);
1166
1167 osb->local_alloc_copy = NULL;
1168 osb->dirty = 0;
1169 }
1170}
1171
Jan Kara22053632008-10-20 23:50:38 +02001172void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1173{
1174 if (osb->quota_rec) {
1175 ocfs2_queue_recovery_completion(osb->journal,
1176 osb->slot_num,
1177 NULL,
1178 NULL,
1179 osb->quota_rec);
1180 osb->quota_rec = NULL;
1181 }
1182}
1183
Mark Fashehccd979b2005-12-15 14:31:24 -08001184static int __ocfs2_recovery_thread(void *arg)
1185{
Jan Kara22053632008-10-20 23:50:38 +02001186 int status, node_num, slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001188 struct ocfs2_recovery_map *rm = osb->recovery_map;
Jan Kara22053632008-10-20 23:50:38 +02001189 int *rm_quota = NULL;
1190 int rm_quota_used = 0, i;
1191 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
1193 mlog_entry_void();
1194
1195 status = ocfs2_wait_on_mount(osb);
1196 if (status < 0) {
1197 goto bail;
1198 }
1199
Jan Kara22053632008-10-20 23:50:38 +02001200 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1201 if (!rm_quota) {
1202 status = -ENOMEM;
1203 goto bail;
1204 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001205restart:
1206 status = ocfs2_super_lock(osb, 1);
1207 if (status < 0) {
1208 mlog_errno(status);
1209 goto bail;
1210 }
1211
Joel Becker553abd02008-02-01 12:03:57 -08001212 spin_lock(&osb->osb_lock);
1213 while (rm->rm_used) {
1214 /* It's always safe to remove entry zero, as we won't
1215 * clear it until ocfs2_recover_node() has succeeded. */
1216 node_num = rm->rm_entries[0];
1217 spin_unlock(&osb->osb_lock);
Jan Kara22053632008-10-20 23:50:38 +02001218 mlog(0, "checking node %d\n", node_num);
1219 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1220 if (slot_num == -ENOENT) {
1221 status = 0;
1222 mlog(0, "no slot for this node, so no recovery"
1223 "required.\n");
1224 goto skip_recovery;
1225 }
1226 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001227
Jan Kara22053632008-10-20 23:50:38 +02001228 /* It is a bit subtle with quota recovery. We cannot do it
1229 * immediately because we have to obtain cluster locks from
1230 * quota files and we also don't want to just skip it because
1231 * then quota usage would be out of sync until some node takes
1232 * the slot. So we remember which nodes need quota recovery
1233 * and when everything else is done, we recover quotas. */
1234 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1235 if (i == rm_quota_used)
1236 rm_quota[rm_quota_used++] = slot_num;
1237
1238 status = ocfs2_recover_node(osb, node_num, slot_num);
1239skip_recovery:
Joel Becker553abd02008-02-01 12:03:57 -08001240 if (!status) {
1241 ocfs2_recovery_map_clear(osb, node_num);
1242 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001243 mlog(ML_ERROR,
1244 "Error %d recovering node %d on device (%u,%u)!\n",
1245 status, node_num,
1246 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1247 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 }
1249
Joel Becker553abd02008-02-01 12:03:57 -08001250 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001251 }
Joel Becker553abd02008-02-01 12:03:57 -08001252 spin_unlock(&osb->osb_lock);
1253 mlog(0, "All nodes recovered\n");
1254
Sunil Mushran539d8262008-07-14 17:31:10 -07001255 /* Refresh all journal recovery generations from disk */
1256 status = ocfs2_check_journals_nolocks(osb);
1257 status = (status == -EROFS) ? 0 : status;
1258 if (status < 0)
1259 mlog_errno(status);
1260
Jan Kara22053632008-10-20 23:50:38 +02001261 /* Now it is right time to recover quotas... We have to do this under
1262 * superblock lock so that noone can start using the slot (and crash)
1263 * before we recover it */
1264 for (i = 0; i < rm_quota_used; i++) {
1265 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1266 if (IS_ERR(qrec)) {
1267 status = PTR_ERR(qrec);
1268 mlog_errno(status);
1269 continue;
1270 }
1271 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1272 NULL, NULL, qrec);
1273 }
1274
Mark Fashehccd979b2005-12-15 14:31:24 -08001275 ocfs2_super_unlock(osb, 1);
1276
1277 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001278 * node(s) may have disallowd a previos inode delete. Re-processing
1279 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001280 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
Jan Kara22053632008-10-20 23:50:38 +02001281 NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001282
1283bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001284 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001285 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001286 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 goto restart;
1288 }
1289
1290 osb->recovery_thread_task = NULL;
1291 mb(); /* sync with ocfs2_recovery_thread_running */
1292 wake_up(&osb->recovery_event);
1293
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001294 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001295
Jan Kara22053632008-10-20 23:50:38 +02001296 if (rm_quota)
1297 kfree(rm_quota);
1298
Mark Fashehccd979b2005-12-15 14:31:24 -08001299 mlog_exit(status);
1300 /* no one is callint kthread_stop() for us so the kthread() api
1301 * requires that we call do_exit(). And it isn't exported, but
1302 * complete_and_exit() seems to be a minimal wrapper around it. */
1303 complete_and_exit(NULL, status);
1304 return status;
1305}
1306
1307void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1308{
1309 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1310 node_num, osb->node_num);
1311
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001312 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001313 if (osb->disable_recovery)
1314 goto out;
1315
1316 /* People waiting on recovery will wait on
1317 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001318 if (ocfs2_recovery_map_set(osb, node_num))
1319 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001320
1321 mlog(0, "starting recovery thread...\n");
1322
1323 if (osb->recovery_thread_task)
1324 goto out;
1325
1326 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001327 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 if (IS_ERR(osb->recovery_thread_task)) {
1329 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1330 osb->recovery_thread_task = NULL;
1331 }
1332
1333out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001334 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001335 wake_up(&osb->recovery_event);
1336
1337 mlog_exit_void();
1338}
1339
Sunil Mushran539d8262008-07-14 17:31:10 -07001340static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1341 int slot_num,
1342 struct buffer_head **bh,
1343 struct inode **ret_inode)
1344{
1345 int status = -EACCES;
1346 struct inode *inode = NULL;
1347
1348 BUG_ON(slot_num >= osb->max_slots);
1349
1350 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1351 slot_num);
1352 if (!inode || is_bad_inode(inode)) {
1353 mlog_errno(status);
1354 goto bail;
1355 }
1356 SET_INODE_JOURNAL(inode);
1357
Joel Beckerb657c952008-11-13 14:49:11 -08001358 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001359 if (status < 0) {
1360 mlog_errno(status);
1361 goto bail;
1362 }
1363
1364 status = 0;
1365
1366bail:
1367 if (inode) {
1368 if (status || !ret_inode)
1369 iput(inode);
1370 else
1371 *ret_inode = inode;
1372 }
1373 return status;
1374}
1375
Mark Fashehccd979b2005-12-15 14:31:24 -08001376/* Does the actual journal replay and marks the journal inode as
1377 * clean. Will only replay if the journal inode is marked dirty. */
1378static int ocfs2_replay_journal(struct ocfs2_super *osb,
1379 int node_num,
1380 int slot_num)
1381{
1382 int status;
1383 int got_lock = 0;
1384 unsigned int flags;
1385 struct inode *inode = NULL;
1386 struct ocfs2_dinode *fe;
1387 journal_t *journal = NULL;
1388 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001389 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001390
Sunil Mushran539d8262008-07-14 17:31:10 -07001391 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1392 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001393 mlog_errno(status);
1394 goto done;
1395 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001396
1397 fe = (struct ocfs2_dinode *)bh->b_data;
1398 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1399 brelse(bh);
1400 bh = NULL;
1401
1402 /*
1403 * As the fs recovery is asynchronous, there is a small chance that
1404 * another node mounted (and recovered) the slot before the recovery
1405 * thread could get the lock. To handle that, we dirty read the journal
1406 * inode for that slot to get the recovery generation. If it is
1407 * different than what we expected, the slot has been recovered.
1408 * If not, it needs recovery.
1409 */
1410 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1411 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1412 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1413 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1414 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 goto done;
1416 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001417
1418 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001419
Mark Fashehe63aecb62007-10-18 15:30:42 -07001420 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001421 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001422 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001423 if (status != -ERESTARTSYS)
1424 mlog(ML_ERROR, "Could not lock journal!\n");
1425 goto done;
1426 }
1427 got_lock = 1;
1428
1429 fe = (struct ocfs2_dinode *) bh->b_data;
1430
1431 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001432 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001433
1434 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1435 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001436 /* Refresh recovery generation for the slot */
1437 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001438 goto done;
1439 }
1440
1441 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1442 node_num, slot_num,
1443 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1444
1445 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1446
1447 status = ocfs2_force_read_journal(inode);
1448 if (status < 0) {
1449 mlog_errno(status);
1450 goto done;
1451 }
1452
1453 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001454 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001455 if (journal == NULL) {
1456 mlog(ML_ERROR, "Linux journal layer error\n");
1457 status = -EIO;
1458 goto done;
1459 }
1460
Joel Becker2b4e30f2008-09-03 20:03:41 -07001461 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001462 if (status < 0) {
1463 mlog_errno(status);
1464 if (!igrab(inode))
1465 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001466 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001467 goto done;
1468 }
1469
1470 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1471
1472 /* wipe the journal */
1473 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001474 jbd2_journal_lock_updates(journal);
1475 status = jbd2_journal_flush(journal);
1476 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001477 if (status < 0)
1478 mlog_errno(status);
1479
1480 /* This will mark the node clean */
1481 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1482 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1483 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1484
Sunil Mushran539d8262008-07-14 17:31:10 -07001485 /* Increment recovery generation to indicate successful recovery */
1486 ocfs2_bump_recovery_generation(fe);
1487 osb->slot_recovery_generations[slot_num] =
1488 ocfs2_get_recovery_generation(fe);
1489
Joel Becker13723d02008-10-17 19:25:01 -07001490 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -08001491 status = ocfs2_write_block(osb, bh, inode);
1492 if (status < 0)
1493 mlog_errno(status);
1494
1495 if (!igrab(inode))
1496 BUG();
1497
Joel Becker2b4e30f2008-09-03 20:03:41 -07001498 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001499
1500done:
1501 /* drop the lock on this nodes journal */
1502 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001503 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001504
1505 if (inode)
1506 iput(inode);
1507
Mark Fasheha81cb882008-10-07 14:25:16 -07001508 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001509
1510 mlog_exit(status);
1511 return status;
1512}
1513
1514/*
1515 * Do the most important parts of node recovery:
1516 * - Replay it's journal
1517 * - Stamp a clean local allocator file
1518 * - Stamp a clean truncate log
1519 * - Mark the node clean
1520 *
1521 * If this function completes without error, a node in OCFS2 can be
1522 * said to have been safely recovered. As a result, failure during the
1523 * second part of a nodes recovery process (local alloc recovery) is
1524 * far less concerning.
1525 */
1526static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +02001527 int node_num, int slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -08001528{
1529 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001530 struct ocfs2_dinode *la_copy = NULL;
1531 struct ocfs2_dinode *tl_copy = NULL;
1532
Jan Kara22053632008-10-20 23:50:38 +02001533 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1534 node_num, slot_num, osb->node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001535
1536 /* Should not ever be called to recover ourselves -- in that
1537 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001538 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001539
Mark Fashehccd979b2005-12-15 14:31:24 -08001540 status = ocfs2_replay_journal(osb, node_num, slot_num);
1541 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001542 if (status == -EBUSY) {
1543 mlog(0, "Skipping recovery for slot %u (node %u) "
1544 "as another node has recovered it\n", slot_num,
1545 node_num);
1546 status = 0;
1547 goto done;
1548 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001549 mlog_errno(status);
1550 goto done;
1551 }
1552
1553 /* Stamp a clean local alloc file AFTER recovering the journal... */
1554 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1555 if (status < 0) {
1556 mlog_errno(status);
1557 goto done;
1558 }
1559
1560 /* An error from begin_truncate_log_recovery is not
1561 * serious enough to warrant halting the rest of
1562 * recovery. */
1563 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1564 if (status < 0)
1565 mlog_errno(status);
1566
1567 /* Likewise, this would be a strange but ultimately not so
1568 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001569 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 if (status < 0)
1571 mlog_errno(status);
1572
1573 /* This will kfree the memory pointed to by la_copy and tl_copy */
1574 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
Jan Kara22053632008-10-20 23:50:38 +02001575 tl_copy, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001576
1577 status = 0;
1578done:
1579
1580 mlog_exit(status);
1581 return status;
1582}
1583
1584/* Test node liveness by trylocking his journal. If we get the lock,
1585 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1586 * still alive (we couldn't get the lock) and < 0 on error. */
1587static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1588 int slot_num)
1589{
1590 int status, flags;
1591 struct inode *inode = NULL;
1592
1593 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1594 slot_num);
1595 if (inode == NULL) {
1596 mlog(ML_ERROR, "access error\n");
1597 status = -EACCES;
1598 goto bail;
1599 }
1600 if (is_bad_inode(inode)) {
1601 mlog(ML_ERROR, "access error (bad inode)\n");
1602 iput(inode);
1603 inode = NULL;
1604 status = -EACCES;
1605 goto bail;
1606 }
1607 SET_INODE_JOURNAL(inode);
1608
1609 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001610 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001611 if (status < 0) {
1612 if (status != -EAGAIN)
1613 mlog_errno(status);
1614 goto bail;
1615 }
1616
Mark Fashehe63aecb62007-10-18 15:30:42 -07001617 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001618bail:
1619 if (inode)
1620 iput(inode);
1621
1622 return status;
1623}
1624
1625/* Call this underneath ocfs2_super_lock. It also assumes that the
1626 * slot info struct has been updated from disk. */
1627int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1628{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001629 unsigned int node_num;
1630 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001631 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001632 struct buffer_head *bh = NULL;
1633 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001634
1635 /* This is called with the super block cluster lock, so we
1636 * know that the slot map can't change underneath us. */
1637
Joel Beckerd85b20e2008-02-01 12:01:05 -08001638 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001639 /* Read journal inode to get the recovery generation */
1640 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1641 if (status) {
1642 mlog_errno(status);
1643 goto bail;
1644 }
1645 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001646 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001647 brelse(bh);
1648 bh = NULL;
1649
Mark Fasheha1af7d12008-08-19 17:20:28 -07001650 spin_lock(&osb->osb_lock);
1651 osb->slot_recovery_generations[i] = gen;
1652
Sunil Mushran539d8262008-07-14 17:31:10 -07001653 mlog(0, "Slot %u recovery generation is %u\n", i,
1654 osb->slot_recovery_generations[i]);
1655
Mark Fasheha1af7d12008-08-19 17:20:28 -07001656 if (i == osb->slot_num) {
1657 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001658 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001659 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001660
1661 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001662 if (status == -ENOENT) {
1663 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001664 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001665 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001666
Mark Fasheha1af7d12008-08-19 17:20:28 -07001667 if (__ocfs2_recovery_map_test(osb, node_num)) {
1668 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001669 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001670 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001671 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001672
1673 /* Ok, we have a slot occupied by another node which
1674 * is not in the recovery map. We trylock his journal
1675 * file here to test if he's alive. */
1676 status = ocfs2_trylock_journal(osb, i);
1677 if (!status) {
1678 /* Since we're called from mount, we know that
1679 * the recovery thread can't race us on
1680 * setting / checking the recovery bits. */
1681 ocfs2_recovery_thread(osb, node_num);
1682 } else if ((status < 0) && (status != -EAGAIN)) {
1683 mlog_errno(status);
1684 goto bail;
1685 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001686 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001687
1688 status = 0;
1689bail:
1690 mlog_exit(status);
1691 return status;
1692}
1693
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001694struct ocfs2_orphan_filldir_priv {
1695 struct inode *head;
1696 struct ocfs2_super *osb;
1697};
1698
1699static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1700 loff_t pos, u64 ino, unsigned type)
1701{
1702 struct ocfs2_orphan_filldir_priv *p = priv;
1703 struct inode *iter;
1704
1705 if (name_len == 1 && !strncmp(".", name, 1))
1706 return 0;
1707 if (name_len == 2 && !strncmp("..", name, 2))
1708 return 0;
1709
1710 /* Skip bad inodes so that recovery can continue */
1711 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001712 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001713 if (IS_ERR(iter))
1714 return 0;
1715
1716 mlog(0, "queue orphan %llu\n",
1717 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1718 /* No locking is required for the next_orphan queue as there
1719 * is only ever a single process doing orphan recovery. */
1720 OCFS2_I(iter)->ip_next_orphan = p->head;
1721 p->head = iter;
1722
1723 return 0;
1724}
1725
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001726static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1727 int slot,
1728 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001729{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001730 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001731 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001732 struct ocfs2_orphan_filldir_priv priv;
1733 loff_t pos = 0;
1734
1735 priv.osb = osb;
1736 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001737
1738 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1739 ORPHAN_DIR_SYSTEM_INODE,
1740 slot);
1741 if (!orphan_dir_inode) {
1742 status = -ENOENT;
1743 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001744 return status;
1745 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001746
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001747 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001748 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001749 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001750 mlog_errno(status);
1751 goto out;
1752 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001753
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001754 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1755 ocfs2_orphan_filldir);
1756 if (status) {
1757 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001758 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001759 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001760
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001761 *head = priv.head;
1762
Mark Fasheha86370f2007-12-03 14:06:23 -08001763out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001764 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001765out:
1766 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001767 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001768 return status;
1769}
1770
1771static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1772 int slot)
1773{
1774 int ret;
1775
1776 spin_lock(&osb->osb_lock);
1777 ret = !osb->osb_orphan_wipes[slot];
1778 spin_unlock(&osb->osb_lock);
1779 return ret;
1780}
1781
1782static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1783 int slot)
1784{
1785 spin_lock(&osb->osb_lock);
1786 /* Mark ourselves such that new processes in delete_inode()
1787 * know to quit early. */
1788 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1789 while (osb->osb_orphan_wipes[slot]) {
1790 /* If any processes are already in the middle of an
1791 * orphan wipe on this dir, then we need to wait for
1792 * them. */
1793 spin_unlock(&osb->osb_lock);
1794 wait_event_interruptible(osb->osb_wipe_event,
1795 ocfs2_orphan_recovery_can_continue(osb, slot));
1796 spin_lock(&osb->osb_lock);
1797 }
1798 spin_unlock(&osb->osb_lock);
1799}
1800
1801static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1802 int slot)
1803{
1804 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1805}
1806
1807/*
1808 * Orphan recovery. Each mounted node has it's own orphan dir which we
1809 * must run during recovery. Our strategy here is to build a list of
1810 * the inodes in the orphan dir and iget/iput them. The VFS does
1811 * (most) of the rest of the work.
1812 *
1813 * Orphan recovery can happen at any time, not just mount so we have a
1814 * couple of extra considerations.
1815 *
1816 * - We grab as many inodes as we can under the orphan dir lock -
1817 * doing iget() outside the orphan dir risks getting a reference on
1818 * an invalid inode.
1819 * - We must be sure not to deadlock with other processes on the
1820 * system wanting to run delete_inode(). This can happen when they go
1821 * to lock the orphan dir and the orphan recovery process attempts to
1822 * iget() inside the orphan dir lock. This can be avoided by
1823 * advertising our state to ocfs2_delete_inode().
1824 */
1825static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1826 int slot)
1827{
1828 int ret = 0;
1829 struct inode *inode = NULL;
1830 struct inode *iter;
1831 struct ocfs2_inode_info *oi;
1832
1833 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1834
1835 ocfs2_mark_recovering_orphan_dir(osb, slot);
1836 ret = ocfs2_queue_orphans(osb, slot, &inode);
1837 ocfs2_clear_recovering_orphan_dir(osb, slot);
1838
1839 /* Error here should be noted, but we want to continue with as
1840 * many queued inodes as we've got. */
1841 if (ret)
1842 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001843
1844 while (inode) {
1845 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001846 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001847
1848 iter = oi->ip_next_orphan;
1849
1850 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001851 /* The remote delete code may have set these on the
1852 * assumption that the other node would wipe them
1853 * successfully. If they are still in the node's
1854 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001855 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1856
1857 /* Set the proper information to get us going into
1858 * ocfs2_delete_inode. */
1859 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001860 spin_unlock(&oi->ip_lock);
1861
1862 iput(inode);
1863
1864 inode = iter;
1865 }
1866
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001867 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001868}
1869
Jan Kara19ece542008-08-21 20:13:17 +02001870static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
Mark Fashehccd979b2005-12-15 14:31:24 -08001871{
1872 /* This check is good because ocfs2 will wait on our recovery
1873 * thread before changing it to something other than MOUNTED
1874 * or DISABLED. */
1875 wait_event(osb->osb_mount_event,
Jan Kara19ece542008-08-21 20:13:17 +02001876 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
1877 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
Mark Fashehccd979b2005-12-15 14:31:24 -08001878 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1879
1880 /* If there's an error on mount, then we may never get to the
1881 * MOUNTED flag, but this is set right before
1882 * dismount_volume() so we can trust it. */
1883 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1884 mlog(0, "mount error, exiting!\n");
1885 return -EBUSY;
1886 }
1887
1888 return 0;
1889}
1890
1891static int ocfs2_commit_thread(void *arg)
1892{
1893 int status;
1894 struct ocfs2_super *osb = arg;
1895 struct ocfs2_journal *journal = osb->journal;
1896
1897 /* we can trust j_num_trans here because _should_stop() is only set in
1898 * shutdown and nobody other than ourselves should be able to start
1899 * transactions. committing on shutdown might take a few iterations
1900 * as final transactions put deleted inodes on the list */
1901 while (!(kthread_should_stop() &&
1902 atomic_read(&journal->j_num_trans) == 0)) {
1903
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001904 wait_event_interruptible(osb->checkpoint_event,
1905 atomic_read(&journal->j_num_trans)
1906 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001907
1908 status = ocfs2_commit_cache(osb);
1909 if (status < 0)
1910 mlog_errno(status);
1911
1912 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1913 mlog(ML_KTHREAD,
1914 "commit_thread: %u transactions pending on "
1915 "shutdown\n",
1916 atomic_read(&journal->j_num_trans));
1917 }
1918 }
1919
1920 return 0;
1921}
1922
Sunil Mushran539d8262008-07-14 17:31:10 -07001923/* Reads all the journal inodes without taking any cluster locks. Used
1924 * for hard readonly access to determine whether any journal requires
1925 * recovery. Also used to refresh the recovery generation numbers after
1926 * a journal has been recovered by another node.
1927 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001928int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1929{
1930 int ret = 0;
1931 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001932 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001933 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001934 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001935
1936 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001937 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1938 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001939 mlog_errno(ret);
1940 goto out;
1941 }
1942
1943 di = (struct ocfs2_dinode *) di_bh->b_data;
1944
Sunil Mushran539d8262008-07-14 17:31:10 -07001945 osb->slot_recovery_generations[slot] =
1946 ocfs2_get_recovery_generation(di);
1947
Mark Fashehccd979b2005-12-15 14:31:24 -08001948 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1949 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001950 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001951
1952 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001953 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001954 }
1955
1956out:
Sunil Mushran539d8262008-07-14 17:31:10 -07001957 if (journal_dirty)
1958 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08001959 return ret;
1960}