blob: 2daa5848faf2388b06431605fc75c06237112c3b [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
Mark Fashehccd979b2005-12-15 14:31:24 -0800755 status = ocfs2_write_block(osb, bh, journal->j_inode);
756 if (status < 0)
757 mlog_errno(status);
758
Mark Fashehccd979b2005-12-15 14:31:24 -0800759 mlog_exit(status);
760 return status;
761}
762
763/*
764 * If the journal has been kmalloc'd it needs to be freed after this
765 * call.
766 */
767void ocfs2_journal_shutdown(struct ocfs2_super *osb)
768{
769 struct ocfs2_journal *journal = NULL;
770 int status = 0;
771 struct inode *inode = NULL;
772 int num_running_trans = 0;
773
774 mlog_entry_void();
775
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100776 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800777
778 journal = osb->journal;
779 if (!journal)
780 goto done;
781
782 inode = journal->j_inode;
783
784 if (journal->j_state != OCFS2_JOURNAL_LOADED)
785 goto done;
786
Joel Becker2b4e30f2008-09-03 20:03:41 -0700787 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 if (!igrab(inode))
789 BUG();
790
791 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
792 if (num_running_trans > 0)
793 mlog(0, "Shutting down journal: must wait on %d "
794 "running transactions!\n",
795 num_running_trans);
796
797 /* Do a commit_cache here. It will flush our journal, *and*
798 * release any locks that are still held.
799 * set the SHUTDOWN flag and release the trans lock.
800 * the commit thread will take the trans lock for us below. */
801 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
802
803 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
804 * drop the trans_lock (which we want to hold until we
805 * completely destroy the journal. */
806 if (osb->commit_task) {
807 /* Wait for the commit thread */
808 mlog(0, "Waiting for ocfs2commit to exit....\n");
809 kthread_stop(osb->commit_task);
810 osb->commit_task = NULL;
811 }
812
813 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
814
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800815 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700816 jbd2_journal_lock_updates(journal->j_journal);
817 status = jbd2_journal_flush(journal->j_journal);
818 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800819 if (status < 0)
820 mlog_errno(status);
821 }
822
823 if (status == 0) {
824 /*
825 * Do not toggle if flush was unsuccessful otherwise
826 * will leave dirty metadata in a "clean" journal
827 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700828 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800829 if (status < 0)
830 mlog_errno(status);
831 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800832
833 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700834 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700835 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800836
837 OCFS2_I(inode)->ip_open_count--;
838
839 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700840 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800841
842 brelse(journal->j_bh);
843 journal->j_bh = NULL;
844
845 journal->j_state = OCFS2_JOURNAL_FREE;
846
847// up_write(&journal->j_trans_barrier);
848done:
849 if (inode)
850 iput(inode);
851 mlog_exit_void();
852}
853
854static void ocfs2_clear_journal_error(struct super_block *sb,
855 journal_t *journal,
856 int slot)
857{
858 int olderr;
859
Joel Becker2b4e30f2008-09-03 20:03:41 -0700860 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800861 if (olderr) {
862 mlog(ML_ERROR, "File system error %d recorded in "
863 "journal %u.\n", olderr, slot);
864 mlog(ML_ERROR, "File system on device %s needs checking.\n",
865 sb->s_id);
866
Joel Becker2b4e30f2008-09-03 20:03:41 -0700867 jbd2_journal_ack_err(journal);
868 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800869 }
870}
871
Sunil Mushran539d8262008-07-14 17:31:10 -0700872int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800873{
874 int status = 0;
875 struct ocfs2_super *osb;
876
877 mlog_entry_void();
878
Julia Lawallb1f35502008-03-04 15:21:05 -0800879 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800880
881 osb = journal->j_osb;
882
Joel Becker2b4e30f2008-09-03 20:03:41 -0700883 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800884 if (status < 0) {
885 mlog(ML_ERROR, "Failed to load journal!\n");
886 goto done;
887 }
888
889 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
890
Sunil Mushran539d8262008-07-14 17:31:10 -0700891 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800892 if (status < 0) {
893 mlog_errno(status);
894 goto done;
895 }
896
897 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800898 if (!local) {
899 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
900 "ocfs2cmt");
901 if (IS_ERR(osb->commit_task)) {
902 status = PTR_ERR(osb->commit_task);
903 osb->commit_task = NULL;
904 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
905 "error=%d", status);
906 goto done;
907 }
908 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800909 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800910
911done:
912 mlog_exit(status);
913 return status;
914}
915
916
917/* 'full' flag tells us whether we clear out all blocks or if we just
918 * mark the journal clean */
919int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
920{
921 int status;
922
923 mlog_entry_void();
924
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100925 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800926
Joel Becker2b4e30f2008-09-03 20:03:41 -0700927 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800928 if (status < 0) {
929 mlog_errno(status);
930 goto bail;
931 }
932
Sunil Mushran539d8262008-07-14 17:31:10 -0700933 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800934 if (status < 0)
935 mlog_errno(status);
936
937bail:
938 mlog_exit(status);
939 return status;
940}
941
Joel Becker553abd02008-02-01 12:03:57 -0800942static int ocfs2_recovery_completed(struct ocfs2_super *osb)
943{
944 int empty;
945 struct ocfs2_recovery_map *rm = osb->recovery_map;
946
947 spin_lock(&osb->osb_lock);
948 empty = (rm->rm_used == 0);
949 spin_unlock(&osb->osb_lock);
950
951 return empty;
952}
953
954void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
955{
956 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
957}
958
Mark Fashehccd979b2005-12-15 14:31:24 -0800959/*
960 * JBD Might read a cached version of another nodes journal file. We
961 * don't want this as this file changes often and we get no
962 * notification on those changes. The only way to be sure that we've
963 * got the most up to date version of those blocks then is to force
964 * read them off disk. Just searching through the buffer cache won't
965 * work as there may be pages backing this file which are still marked
966 * up to date. We know things can't change on this file underneath us
967 * as we have the lock by now :)
968 */
969static int ocfs2_force_read_journal(struct inode *inode)
970{
971 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800972 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700973 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800974#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800975 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
976
977 mlog_entry_void();
978
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
980
Mark Fasheh8110b072007-03-22 16:53:23 -0700981 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800982 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700983 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800984 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800985 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800986 if (status < 0) {
987 mlog_errno(status);
988 goto bail;
989 }
990
991 if (p_blocks > CONCURRENT_JOURNAL_FILL)
992 p_blocks = CONCURRENT_JOURNAL_FILL;
993
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700994 /* We are reading journal data which should not
995 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -0700996 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
997 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800998 if (status < 0) {
999 mlog_errno(status);
1000 goto bail;
1001 }
1002
1003 for(i = 0; i < p_blocks; i++) {
1004 brelse(bhs[i]);
1005 bhs[i] = NULL;
1006 }
1007
1008 v_blkno += p_blocks;
1009 }
1010
1011bail:
1012 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001013 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001014 mlog_exit(status);
1015 return status;
1016}
1017
1018struct ocfs2_la_recovery_item {
1019 struct list_head lri_list;
1020 int lri_slot;
1021 struct ocfs2_dinode *lri_la_dinode;
1022 struct ocfs2_dinode *lri_tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001023 struct ocfs2_quota_recovery *lri_qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001024};
1025
1026/* Does the second half of the recovery process. By this point, the
1027 * node is marked clean and can actually be considered recovered,
1028 * hence it's no longer in the recovery map, but there's still some
1029 * cleanup we can do which shouldn't happen within the recovery thread
1030 * as locking in that context becomes very difficult if we are to take
1031 * recovering nodes into account.
1032 *
1033 * NOTE: This function can and will sleep on recovery of other nodes
1034 * during cluster locking, just like any other ocfs2 process.
1035 */
David Howellsc4028952006-11-22 14:57:56 +00001036void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08001037{
1038 int ret;
David Howellsc4028952006-11-22 14:57:56 +00001039 struct ocfs2_journal *journal =
1040 container_of(work, struct ocfs2_journal, j_recovery_work);
1041 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001042 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +02001043 struct ocfs2_la_recovery_item *item, *n;
Jan Kara22053632008-10-20 23:50:38 +02001044 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 LIST_HEAD(tmp_la_list);
1046
1047 mlog_entry_void();
1048
1049 mlog(0, "completing recovery from keventd\n");
1050
1051 spin_lock(&journal->j_lock);
1052 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1053 spin_unlock(&journal->j_lock);
1054
Christoph Hellwig800deef2007-05-17 16:03:13 +02001055 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 list_del_init(&item->lri_list);
1057
1058 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1059
Jan Kara19ece542008-08-21 20:13:17 +02001060 ocfs2_wait_on_quotas(osb);
1061
Mark Fashehccd979b2005-12-15 14:31:24 -08001062 la_dinode = item->lri_la_dinode;
1063 if (la_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001064 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001065 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001066
1067 ret = ocfs2_complete_local_alloc_recovery(osb,
1068 la_dinode);
1069 if (ret < 0)
1070 mlog_errno(ret);
1071
1072 kfree(la_dinode);
1073 }
1074
1075 tl_dinode = item->lri_tl_dinode;
1076 if (tl_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001077 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001078 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001079
1080 ret = ocfs2_complete_truncate_log_recovery(osb,
1081 tl_dinode);
1082 if (ret < 0)
1083 mlog_errno(ret);
1084
1085 kfree(tl_dinode);
1086 }
1087
1088 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1089 if (ret < 0)
1090 mlog_errno(ret);
1091
Jan Kara22053632008-10-20 23:50:38 +02001092 qrec = item->lri_qrec;
1093 if (qrec) {
1094 mlog(0, "Recovering quota files");
1095 ret = ocfs2_finish_quota_recovery(osb, qrec,
1096 item->lri_slot);
1097 if (ret < 0)
1098 mlog_errno(ret);
1099 /* Recovery info is already freed now */
1100 }
1101
Mark Fashehccd979b2005-12-15 14:31:24 -08001102 kfree(item);
1103 }
1104
1105 mlog(0, "Recovery completion\n");
1106 mlog_exit_void();
1107}
1108
1109/* NOTE: This function always eats your references to la_dinode and
1110 * tl_dinode, either manually on error, or by passing them to
1111 * ocfs2_complete_recovery */
1112static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1113 int slot_num,
1114 struct ocfs2_dinode *la_dinode,
Jan Kara22053632008-10-20 23:50:38 +02001115 struct ocfs2_dinode *tl_dinode,
1116 struct ocfs2_quota_recovery *qrec)
Mark Fashehccd979b2005-12-15 14:31:24 -08001117{
1118 struct ocfs2_la_recovery_item *item;
1119
Sunil Mushranafae00ab2006-04-12 14:37:00 -07001120 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001121 if (!item) {
1122 /* Though we wish to avoid it, we are in fact safe in
1123 * skipping local alloc cleanup as fsck.ocfs2 is more
1124 * than capable of reclaiming unused space. */
1125 if (la_dinode)
1126 kfree(la_dinode);
1127
1128 if (tl_dinode)
1129 kfree(tl_dinode);
1130
Jan Kara22053632008-10-20 23:50:38 +02001131 if (qrec)
1132 ocfs2_free_quota_recovery(qrec);
1133
Mark Fashehccd979b2005-12-15 14:31:24 -08001134 mlog_errno(-ENOMEM);
1135 return;
1136 }
1137
1138 INIT_LIST_HEAD(&item->lri_list);
1139 item->lri_la_dinode = la_dinode;
1140 item->lri_slot = slot_num;
1141 item->lri_tl_dinode = tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001142 item->lri_qrec = qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001143
1144 spin_lock(&journal->j_lock);
1145 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1146 queue_work(ocfs2_wq, &journal->j_recovery_work);
1147 spin_unlock(&journal->j_lock);
1148}
1149
1150/* Called by the mount code to queue recovery the last part of
1151 * recovery for it's own slot. */
1152void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1153{
1154 struct ocfs2_journal *journal = osb->journal;
1155
1156 if (osb->dirty) {
1157 /* No need to queue up our truncate_log as regular
1158 * cleanup will catch that. */
1159 ocfs2_queue_recovery_completion(journal,
1160 osb->slot_num,
1161 osb->local_alloc_copy,
Jan Kara22053632008-10-20 23:50:38 +02001162 NULL,
Mark Fashehccd979b2005-12-15 14:31:24 -08001163 NULL);
1164 ocfs2_schedule_truncate_log_flush(osb, 0);
1165
1166 osb->local_alloc_copy = NULL;
1167 osb->dirty = 0;
1168 }
1169}
1170
Jan Kara22053632008-10-20 23:50:38 +02001171void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1172{
1173 if (osb->quota_rec) {
1174 ocfs2_queue_recovery_completion(osb->journal,
1175 osb->slot_num,
1176 NULL,
1177 NULL,
1178 osb->quota_rec);
1179 osb->quota_rec = NULL;
1180 }
1181}
1182
Mark Fashehccd979b2005-12-15 14:31:24 -08001183static int __ocfs2_recovery_thread(void *arg)
1184{
Jan Kara22053632008-10-20 23:50:38 +02001185 int status, node_num, slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001187 struct ocfs2_recovery_map *rm = osb->recovery_map;
Jan Kara22053632008-10-20 23:50:38 +02001188 int *rm_quota = NULL;
1189 int rm_quota_used = 0, i;
1190 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001191
1192 mlog_entry_void();
1193
1194 status = ocfs2_wait_on_mount(osb);
1195 if (status < 0) {
1196 goto bail;
1197 }
1198
Jan Kara22053632008-10-20 23:50:38 +02001199 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1200 if (!rm_quota) {
1201 status = -ENOMEM;
1202 goto bail;
1203 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001204restart:
1205 status = ocfs2_super_lock(osb, 1);
1206 if (status < 0) {
1207 mlog_errno(status);
1208 goto bail;
1209 }
1210
Joel Becker553abd02008-02-01 12:03:57 -08001211 spin_lock(&osb->osb_lock);
1212 while (rm->rm_used) {
1213 /* It's always safe to remove entry zero, as we won't
1214 * clear it until ocfs2_recover_node() has succeeded. */
1215 node_num = rm->rm_entries[0];
1216 spin_unlock(&osb->osb_lock);
Jan Kara22053632008-10-20 23:50:38 +02001217 mlog(0, "checking node %d\n", node_num);
1218 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1219 if (slot_num == -ENOENT) {
1220 status = 0;
1221 mlog(0, "no slot for this node, so no recovery"
1222 "required.\n");
1223 goto skip_recovery;
1224 }
1225 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001226
Jan Kara22053632008-10-20 23:50:38 +02001227 /* It is a bit subtle with quota recovery. We cannot do it
1228 * immediately because we have to obtain cluster locks from
1229 * quota files and we also don't want to just skip it because
1230 * then quota usage would be out of sync until some node takes
1231 * the slot. So we remember which nodes need quota recovery
1232 * and when everything else is done, we recover quotas. */
1233 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1234 if (i == rm_quota_used)
1235 rm_quota[rm_quota_used++] = slot_num;
1236
1237 status = ocfs2_recover_node(osb, node_num, slot_num);
1238skip_recovery:
Joel Becker553abd02008-02-01 12:03:57 -08001239 if (!status) {
1240 ocfs2_recovery_map_clear(osb, node_num);
1241 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001242 mlog(ML_ERROR,
1243 "Error %d recovering node %d on device (%u,%u)!\n",
1244 status, node_num,
1245 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1246 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001247 }
1248
Joel Becker553abd02008-02-01 12:03:57 -08001249 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001250 }
Joel Becker553abd02008-02-01 12:03:57 -08001251 spin_unlock(&osb->osb_lock);
1252 mlog(0, "All nodes recovered\n");
1253
Sunil Mushran539d8262008-07-14 17:31:10 -07001254 /* Refresh all journal recovery generations from disk */
1255 status = ocfs2_check_journals_nolocks(osb);
1256 status = (status == -EROFS) ? 0 : status;
1257 if (status < 0)
1258 mlog_errno(status);
1259
Jan Kara22053632008-10-20 23:50:38 +02001260 /* Now it is right time to recover quotas... We have to do this under
1261 * superblock lock so that noone can start using the slot (and crash)
1262 * before we recover it */
1263 for (i = 0; i < rm_quota_used; i++) {
1264 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1265 if (IS_ERR(qrec)) {
1266 status = PTR_ERR(qrec);
1267 mlog_errno(status);
1268 continue;
1269 }
1270 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1271 NULL, NULL, qrec);
1272 }
1273
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 ocfs2_super_unlock(osb, 1);
1275
1276 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001277 * node(s) may have disallowd a previos inode delete. Re-processing
1278 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001279 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
Jan Kara22053632008-10-20 23:50:38 +02001280 NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001281
1282bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001283 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001284 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001285 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001286 goto restart;
1287 }
1288
1289 osb->recovery_thread_task = NULL;
1290 mb(); /* sync with ocfs2_recovery_thread_running */
1291 wake_up(&osb->recovery_event);
1292
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001293 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001294
Jan Kara22053632008-10-20 23:50:38 +02001295 if (rm_quota)
1296 kfree(rm_quota);
1297
Mark Fashehccd979b2005-12-15 14:31:24 -08001298 mlog_exit(status);
1299 /* no one is callint kthread_stop() for us so the kthread() api
1300 * requires that we call do_exit(). And it isn't exported, but
1301 * complete_and_exit() seems to be a minimal wrapper around it. */
1302 complete_and_exit(NULL, status);
1303 return status;
1304}
1305
1306void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1307{
1308 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1309 node_num, osb->node_num);
1310
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001311 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001312 if (osb->disable_recovery)
1313 goto out;
1314
1315 /* People waiting on recovery will wait on
1316 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001317 if (ocfs2_recovery_map_set(osb, node_num))
1318 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001319
1320 mlog(0, "starting recovery thread...\n");
1321
1322 if (osb->recovery_thread_task)
1323 goto out;
1324
1325 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001326 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001327 if (IS_ERR(osb->recovery_thread_task)) {
1328 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1329 osb->recovery_thread_task = NULL;
1330 }
1331
1332out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001333 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001334 wake_up(&osb->recovery_event);
1335
1336 mlog_exit_void();
1337}
1338
Sunil Mushran539d8262008-07-14 17:31:10 -07001339static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1340 int slot_num,
1341 struct buffer_head **bh,
1342 struct inode **ret_inode)
1343{
1344 int status = -EACCES;
1345 struct inode *inode = NULL;
1346
1347 BUG_ON(slot_num >= osb->max_slots);
1348
1349 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1350 slot_num);
1351 if (!inode || is_bad_inode(inode)) {
1352 mlog_errno(status);
1353 goto bail;
1354 }
1355 SET_INODE_JOURNAL(inode);
1356
Joel Beckerb657c952008-11-13 14:49:11 -08001357 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001358 if (status < 0) {
1359 mlog_errno(status);
1360 goto bail;
1361 }
1362
1363 status = 0;
1364
1365bail:
1366 if (inode) {
1367 if (status || !ret_inode)
1368 iput(inode);
1369 else
1370 *ret_inode = inode;
1371 }
1372 return status;
1373}
1374
Mark Fashehccd979b2005-12-15 14:31:24 -08001375/* Does the actual journal replay and marks the journal inode as
1376 * clean. Will only replay if the journal inode is marked dirty. */
1377static int ocfs2_replay_journal(struct ocfs2_super *osb,
1378 int node_num,
1379 int slot_num)
1380{
1381 int status;
1382 int got_lock = 0;
1383 unsigned int flags;
1384 struct inode *inode = NULL;
1385 struct ocfs2_dinode *fe;
1386 journal_t *journal = NULL;
1387 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001388 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001389
Sunil Mushran539d8262008-07-14 17:31:10 -07001390 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1391 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001392 mlog_errno(status);
1393 goto done;
1394 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001395
1396 fe = (struct ocfs2_dinode *)bh->b_data;
1397 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1398 brelse(bh);
1399 bh = NULL;
1400
1401 /*
1402 * As the fs recovery is asynchronous, there is a small chance that
1403 * another node mounted (and recovered) the slot before the recovery
1404 * thread could get the lock. To handle that, we dirty read the journal
1405 * inode for that slot to get the recovery generation. If it is
1406 * different than what we expected, the slot has been recovered.
1407 * If not, it needs recovery.
1408 */
1409 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1410 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1411 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1412 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1413 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001414 goto done;
1415 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001416
1417 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001418
Mark Fashehe63aecb62007-10-18 15:30:42 -07001419 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001420 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001421 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 if (status != -ERESTARTSYS)
1423 mlog(ML_ERROR, "Could not lock journal!\n");
1424 goto done;
1425 }
1426 got_lock = 1;
1427
1428 fe = (struct ocfs2_dinode *) bh->b_data;
1429
1430 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001431 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001432
1433 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1434 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001435 /* Refresh recovery generation for the slot */
1436 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001437 goto done;
1438 }
1439
1440 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1441 node_num, slot_num,
1442 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1443
1444 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1445
1446 status = ocfs2_force_read_journal(inode);
1447 if (status < 0) {
1448 mlog_errno(status);
1449 goto done;
1450 }
1451
1452 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001453 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001454 if (journal == NULL) {
1455 mlog(ML_ERROR, "Linux journal layer error\n");
1456 status = -EIO;
1457 goto done;
1458 }
1459
Joel Becker2b4e30f2008-09-03 20:03:41 -07001460 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001461 if (status < 0) {
1462 mlog_errno(status);
1463 if (!igrab(inode))
1464 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001465 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001466 goto done;
1467 }
1468
1469 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1470
1471 /* wipe the journal */
1472 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001473 jbd2_journal_lock_updates(journal);
1474 status = jbd2_journal_flush(journal);
1475 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001476 if (status < 0)
1477 mlog_errno(status);
1478
1479 /* This will mark the node clean */
1480 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1481 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1482 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1483
Sunil Mushran539d8262008-07-14 17:31:10 -07001484 /* Increment recovery generation to indicate successful recovery */
1485 ocfs2_bump_recovery_generation(fe);
1486 osb->slot_recovery_generations[slot_num] =
1487 ocfs2_get_recovery_generation(fe);
1488
Mark Fashehccd979b2005-12-15 14:31:24 -08001489 status = ocfs2_write_block(osb, bh, inode);
1490 if (status < 0)
1491 mlog_errno(status);
1492
1493 if (!igrab(inode))
1494 BUG();
1495
Joel Becker2b4e30f2008-09-03 20:03:41 -07001496 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001497
1498done:
1499 /* drop the lock on this nodes journal */
1500 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001501 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001502
1503 if (inode)
1504 iput(inode);
1505
Mark Fasheha81cb882008-10-07 14:25:16 -07001506 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001507
1508 mlog_exit(status);
1509 return status;
1510}
1511
1512/*
1513 * Do the most important parts of node recovery:
1514 * - Replay it's journal
1515 * - Stamp a clean local allocator file
1516 * - Stamp a clean truncate log
1517 * - Mark the node clean
1518 *
1519 * If this function completes without error, a node in OCFS2 can be
1520 * said to have been safely recovered. As a result, failure during the
1521 * second part of a nodes recovery process (local alloc recovery) is
1522 * far less concerning.
1523 */
1524static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +02001525 int node_num, int slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -08001526{
1527 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 struct ocfs2_dinode *la_copy = NULL;
1529 struct ocfs2_dinode *tl_copy = NULL;
1530
Jan Kara22053632008-10-20 23:50:38 +02001531 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1532 node_num, slot_num, osb->node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533
1534 /* Should not ever be called to recover ourselves -- in that
1535 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001536 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001537
Mark Fashehccd979b2005-12-15 14:31:24 -08001538 status = ocfs2_replay_journal(osb, node_num, slot_num);
1539 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001540 if (status == -EBUSY) {
1541 mlog(0, "Skipping recovery for slot %u (node %u) "
1542 "as another node has recovered it\n", slot_num,
1543 node_num);
1544 status = 0;
1545 goto done;
1546 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001547 mlog_errno(status);
1548 goto done;
1549 }
1550
1551 /* Stamp a clean local alloc file AFTER recovering the journal... */
1552 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1553 if (status < 0) {
1554 mlog_errno(status);
1555 goto done;
1556 }
1557
1558 /* An error from begin_truncate_log_recovery is not
1559 * serious enough to warrant halting the rest of
1560 * recovery. */
1561 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1562 if (status < 0)
1563 mlog_errno(status);
1564
1565 /* Likewise, this would be a strange but ultimately not so
1566 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001567 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001568 if (status < 0)
1569 mlog_errno(status);
1570
1571 /* This will kfree the memory pointed to by la_copy and tl_copy */
1572 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
Jan Kara22053632008-10-20 23:50:38 +02001573 tl_copy, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001574
1575 status = 0;
1576done:
1577
1578 mlog_exit(status);
1579 return status;
1580}
1581
1582/* Test node liveness by trylocking his journal. If we get the lock,
1583 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1584 * still alive (we couldn't get the lock) and < 0 on error. */
1585static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1586 int slot_num)
1587{
1588 int status, flags;
1589 struct inode *inode = NULL;
1590
1591 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1592 slot_num);
1593 if (inode == NULL) {
1594 mlog(ML_ERROR, "access error\n");
1595 status = -EACCES;
1596 goto bail;
1597 }
1598 if (is_bad_inode(inode)) {
1599 mlog(ML_ERROR, "access error (bad inode)\n");
1600 iput(inode);
1601 inode = NULL;
1602 status = -EACCES;
1603 goto bail;
1604 }
1605 SET_INODE_JOURNAL(inode);
1606
1607 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001608 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001609 if (status < 0) {
1610 if (status != -EAGAIN)
1611 mlog_errno(status);
1612 goto bail;
1613 }
1614
Mark Fashehe63aecb62007-10-18 15:30:42 -07001615 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001616bail:
1617 if (inode)
1618 iput(inode);
1619
1620 return status;
1621}
1622
1623/* Call this underneath ocfs2_super_lock. It also assumes that the
1624 * slot info struct has been updated from disk. */
1625int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1626{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001627 unsigned int node_num;
1628 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001629 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001630 struct buffer_head *bh = NULL;
1631 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001632
1633 /* This is called with the super block cluster lock, so we
1634 * know that the slot map can't change underneath us. */
1635
Joel Beckerd85b20e2008-02-01 12:01:05 -08001636 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001637 /* Read journal inode to get the recovery generation */
1638 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1639 if (status) {
1640 mlog_errno(status);
1641 goto bail;
1642 }
1643 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001644 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001645 brelse(bh);
1646 bh = NULL;
1647
Mark Fasheha1af7d12008-08-19 17:20:28 -07001648 spin_lock(&osb->osb_lock);
1649 osb->slot_recovery_generations[i] = gen;
1650
Sunil Mushran539d8262008-07-14 17:31:10 -07001651 mlog(0, "Slot %u recovery generation is %u\n", i,
1652 osb->slot_recovery_generations[i]);
1653
Mark Fasheha1af7d12008-08-19 17:20:28 -07001654 if (i == osb->slot_num) {
1655 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001656 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001657 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001658
1659 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001660 if (status == -ENOENT) {
1661 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001662 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001663 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001664
Mark Fasheha1af7d12008-08-19 17:20:28 -07001665 if (__ocfs2_recovery_map_test(osb, node_num)) {
1666 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001667 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001668 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001669 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001670
1671 /* Ok, we have a slot occupied by another node which
1672 * is not in the recovery map. We trylock his journal
1673 * file here to test if he's alive. */
1674 status = ocfs2_trylock_journal(osb, i);
1675 if (!status) {
1676 /* Since we're called from mount, we know that
1677 * the recovery thread can't race us on
1678 * setting / checking the recovery bits. */
1679 ocfs2_recovery_thread(osb, node_num);
1680 } else if ((status < 0) && (status != -EAGAIN)) {
1681 mlog_errno(status);
1682 goto bail;
1683 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001684 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001685
1686 status = 0;
1687bail:
1688 mlog_exit(status);
1689 return status;
1690}
1691
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001692struct ocfs2_orphan_filldir_priv {
1693 struct inode *head;
1694 struct ocfs2_super *osb;
1695};
1696
1697static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1698 loff_t pos, u64 ino, unsigned type)
1699{
1700 struct ocfs2_orphan_filldir_priv *p = priv;
1701 struct inode *iter;
1702
1703 if (name_len == 1 && !strncmp(".", name, 1))
1704 return 0;
1705 if (name_len == 2 && !strncmp("..", name, 2))
1706 return 0;
1707
1708 /* Skip bad inodes so that recovery can continue */
1709 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001710 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001711 if (IS_ERR(iter))
1712 return 0;
1713
1714 mlog(0, "queue orphan %llu\n",
1715 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1716 /* No locking is required for the next_orphan queue as there
1717 * is only ever a single process doing orphan recovery. */
1718 OCFS2_I(iter)->ip_next_orphan = p->head;
1719 p->head = iter;
1720
1721 return 0;
1722}
1723
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001724static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1725 int slot,
1726 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001727{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001728 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001729 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001730 struct ocfs2_orphan_filldir_priv priv;
1731 loff_t pos = 0;
1732
1733 priv.osb = osb;
1734 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001735
1736 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1737 ORPHAN_DIR_SYSTEM_INODE,
1738 slot);
1739 if (!orphan_dir_inode) {
1740 status = -ENOENT;
1741 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001742 return status;
1743 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001744
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001745 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001746 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001747 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001748 mlog_errno(status);
1749 goto out;
1750 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001751
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001752 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1753 ocfs2_orphan_filldir);
1754 if (status) {
1755 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001756 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001757 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001758
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001759 *head = priv.head;
1760
Mark Fasheha86370f2007-12-03 14:06:23 -08001761out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001762 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001763out:
1764 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001765 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001766 return status;
1767}
1768
1769static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1770 int slot)
1771{
1772 int ret;
1773
1774 spin_lock(&osb->osb_lock);
1775 ret = !osb->osb_orphan_wipes[slot];
1776 spin_unlock(&osb->osb_lock);
1777 return ret;
1778}
1779
1780static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1781 int slot)
1782{
1783 spin_lock(&osb->osb_lock);
1784 /* Mark ourselves such that new processes in delete_inode()
1785 * know to quit early. */
1786 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1787 while (osb->osb_orphan_wipes[slot]) {
1788 /* If any processes are already in the middle of an
1789 * orphan wipe on this dir, then we need to wait for
1790 * them. */
1791 spin_unlock(&osb->osb_lock);
1792 wait_event_interruptible(osb->osb_wipe_event,
1793 ocfs2_orphan_recovery_can_continue(osb, slot));
1794 spin_lock(&osb->osb_lock);
1795 }
1796 spin_unlock(&osb->osb_lock);
1797}
1798
1799static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1800 int slot)
1801{
1802 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1803}
1804
1805/*
1806 * Orphan recovery. Each mounted node has it's own orphan dir which we
1807 * must run during recovery. Our strategy here is to build a list of
1808 * the inodes in the orphan dir and iget/iput them. The VFS does
1809 * (most) of the rest of the work.
1810 *
1811 * Orphan recovery can happen at any time, not just mount so we have a
1812 * couple of extra considerations.
1813 *
1814 * - We grab as many inodes as we can under the orphan dir lock -
1815 * doing iget() outside the orphan dir risks getting a reference on
1816 * an invalid inode.
1817 * - We must be sure not to deadlock with other processes on the
1818 * system wanting to run delete_inode(). This can happen when they go
1819 * to lock the orphan dir and the orphan recovery process attempts to
1820 * iget() inside the orphan dir lock. This can be avoided by
1821 * advertising our state to ocfs2_delete_inode().
1822 */
1823static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1824 int slot)
1825{
1826 int ret = 0;
1827 struct inode *inode = NULL;
1828 struct inode *iter;
1829 struct ocfs2_inode_info *oi;
1830
1831 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1832
1833 ocfs2_mark_recovering_orphan_dir(osb, slot);
1834 ret = ocfs2_queue_orphans(osb, slot, &inode);
1835 ocfs2_clear_recovering_orphan_dir(osb, slot);
1836
1837 /* Error here should be noted, but we want to continue with as
1838 * many queued inodes as we've got. */
1839 if (ret)
1840 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001841
1842 while (inode) {
1843 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001844 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001845
1846 iter = oi->ip_next_orphan;
1847
1848 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001849 /* The remote delete code may have set these on the
1850 * assumption that the other node would wipe them
1851 * successfully. If they are still in the node's
1852 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001853 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1854
1855 /* Set the proper information to get us going into
1856 * ocfs2_delete_inode. */
1857 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001858 spin_unlock(&oi->ip_lock);
1859
1860 iput(inode);
1861
1862 inode = iter;
1863 }
1864
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001865 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001866}
1867
Jan Kara19ece542008-08-21 20:13:17 +02001868static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
Mark Fashehccd979b2005-12-15 14:31:24 -08001869{
1870 /* This check is good because ocfs2 will wait on our recovery
1871 * thread before changing it to something other than MOUNTED
1872 * or DISABLED. */
1873 wait_event(osb->osb_mount_event,
Jan Kara19ece542008-08-21 20:13:17 +02001874 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
1875 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
Mark Fashehccd979b2005-12-15 14:31:24 -08001876 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1877
1878 /* If there's an error on mount, then we may never get to the
1879 * MOUNTED flag, but this is set right before
1880 * dismount_volume() so we can trust it. */
1881 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1882 mlog(0, "mount error, exiting!\n");
1883 return -EBUSY;
1884 }
1885
1886 return 0;
1887}
1888
1889static int ocfs2_commit_thread(void *arg)
1890{
1891 int status;
1892 struct ocfs2_super *osb = arg;
1893 struct ocfs2_journal *journal = osb->journal;
1894
1895 /* we can trust j_num_trans here because _should_stop() is only set in
1896 * shutdown and nobody other than ourselves should be able to start
1897 * transactions. committing on shutdown might take a few iterations
1898 * as final transactions put deleted inodes on the list */
1899 while (!(kthread_should_stop() &&
1900 atomic_read(&journal->j_num_trans) == 0)) {
1901
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001902 wait_event_interruptible(osb->checkpoint_event,
1903 atomic_read(&journal->j_num_trans)
1904 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001905
1906 status = ocfs2_commit_cache(osb);
1907 if (status < 0)
1908 mlog_errno(status);
1909
1910 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1911 mlog(ML_KTHREAD,
1912 "commit_thread: %u transactions pending on "
1913 "shutdown\n",
1914 atomic_read(&journal->j_num_trans));
1915 }
1916 }
1917
1918 return 0;
1919}
1920
Sunil Mushran539d8262008-07-14 17:31:10 -07001921/* Reads all the journal inodes without taking any cluster locks. Used
1922 * for hard readonly access to determine whether any journal requires
1923 * recovery. Also used to refresh the recovery generation numbers after
1924 * a journal has been recovered by another node.
1925 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001926int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1927{
1928 int ret = 0;
1929 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001930 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001931 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001932 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001933
1934 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001935 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1936 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001937 mlog_errno(ret);
1938 goto out;
1939 }
1940
1941 di = (struct ocfs2_dinode *) di_bh->b_data;
1942
Sunil Mushran539d8262008-07-14 17:31:10 -07001943 osb->slot_recovery_generations[slot] =
1944 ocfs2_get_recovery_generation(di);
1945
Mark Fashehccd979b2005-12-15 14:31:24 -08001946 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1947 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001948 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001949
1950 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001951 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001952 }
1953
1954out:
Sunil Mushran539d8262008-07-14 17:31:10 -07001955 if (journal_dirty)
1956 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08001957 return ret;
1958}