blob: 302f1144a7089d962fff9a6045b6b1f9addfa6a7 [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"
Mark Fasheh316f4b92007-09-07 18:21:26 -070038#include "dir.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "extent_map.h"
41#include "heartbeat.h"
42#include "inode.h"
43#include "journal.h"
44#include "localalloc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045#include "slot_map.h"
46#include "super.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080047#include "sysfile.h"
Jan Kara22053632008-10-20 23:50:38 +020048#include "quota.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080049
50#include "buffer_head_io.h"
51
Ingo Molnar34af9462006-06-27 02:53:55 -070052DEFINE_SPINLOCK(trans_inc_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -080053
54static int ocfs2_force_read_journal(struct inode *inode);
55static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +020056 int node_num, int slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -080057static int __ocfs2_recovery_thread(void *arg);
58static int ocfs2_commit_cache(struct ocfs2_super *osb);
Jan Kara19ece542008-08-21 20:13:17 +020059static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
Mark Fashehccd979b2005-12-15 14:31:24 -080060static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -070061 int dirty, int replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -080062static int ocfs2_trylock_journal(struct ocfs2_super *osb,
63 int slot_num);
64static int ocfs2_recover_orphans(struct ocfs2_super *osb,
65 int slot);
66static int ocfs2_commit_thread(void *arg);
67
Jan Kara19ece542008-08-21 20:13:17 +020068static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
69{
70 return __ocfs2_wait_on_mount(osb, 0);
71}
72
73static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
74{
75 return __ocfs2_wait_on_mount(osb, 1);
76}
77
78
Joel Becker553abd02008-02-01 12:03:57 -080079
80/*
81 * The recovery_list is a simple linked list of node numbers to recover.
82 * It is protected by the recovery_lock.
83 */
84
85struct ocfs2_recovery_map {
Joel Beckerfc881fa2008-02-01 12:04:48 -080086 unsigned int rm_used;
Joel Becker553abd02008-02-01 12:03:57 -080087 unsigned int *rm_entries;
88};
89
90int ocfs2_recovery_init(struct ocfs2_super *osb)
91{
92 struct ocfs2_recovery_map *rm;
93
94 mutex_init(&osb->recovery_lock);
95 osb->disable_recovery = 0;
96 osb->recovery_thread_task = NULL;
97 init_waitqueue_head(&osb->recovery_event);
98
99 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
100 osb->max_slots * sizeof(unsigned int),
101 GFP_KERNEL);
102 if (!rm) {
103 mlog_errno(-ENOMEM);
104 return -ENOMEM;
105 }
106
107 rm->rm_entries = (unsigned int *)((char *)rm +
108 sizeof(struct ocfs2_recovery_map));
109 osb->recovery_map = rm;
110
111 return 0;
112}
113
114/* we can't grab the goofy sem lock from inside wait_event, so we use
115 * memory barriers to make sure that we'll see the null task before
116 * being woken up */
117static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
118{
119 mb();
120 return osb->recovery_thread_task != NULL;
121}
122
123void ocfs2_recovery_exit(struct ocfs2_super *osb)
124{
125 struct ocfs2_recovery_map *rm;
126
127 /* disable any new recovery threads and wait for any currently
128 * running ones to exit. Do this before setting the vol_state. */
129 mutex_lock(&osb->recovery_lock);
130 osb->disable_recovery = 1;
131 mutex_unlock(&osb->recovery_lock);
132 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
133
134 /* At this point, we know that no more recovery threads can be
135 * launched, so wait for any recovery completion work to
136 * complete. */
137 flush_workqueue(ocfs2_wq);
138
139 /*
140 * Now that recovery is shut down, and the osb is about to be
141 * freed, the osb_lock is not taken here.
142 */
143 rm = osb->recovery_map;
144 /* XXX: Should we bug if there are dirty entries? */
145
146 kfree(rm);
147}
148
149static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
150 unsigned int node_num)
151{
152 int i;
153 struct ocfs2_recovery_map *rm = osb->recovery_map;
154
155 assert_spin_locked(&osb->osb_lock);
156
157 for (i = 0; i < rm->rm_used; i++) {
158 if (rm->rm_entries[i] == node_num)
159 return 1;
160 }
161
162 return 0;
163}
164
165/* Behaves like test-and-set. Returns the previous value */
166static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
167 unsigned int node_num)
168{
169 struct ocfs2_recovery_map *rm = osb->recovery_map;
170
171 spin_lock(&osb->osb_lock);
172 if (__ocfs2_recovery_map_test(osb, node_num)) {
173 spin_unlock(&osb->osb_lock);
174 return 1;
175 }
176
177 /* XXX: Can this be exploited? Not from o2dlm... */
178 BUG_ON(rm->rm_used >= osb->max_slots);
179
180 rm->rm_entries[rm->rm_used] = node_num;
181 rm->rm_used++;
182 spin_unlock(&osb->osb_lock);
183
184 return 0;
185}
186
187static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
188 unsigned int node_num)
189{
190 int i;
191 struct ocfs2_recovery_map *rm = osb->recovery_map;
192
193 spin_lock(&osb->osb_lock);
194
195 for (i = 0; i < rm->rm_used; i++) {
196 if (rm->rm_entries[i] == node_num)
197 break;
198 }
199
200 if (i < rm->rm_used) {
201 /* XXX: be careful with the pointer math */
202 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
203 (rm->rm_used - i - 1) * sizeof(unsigned int));
204 rm->rm_used--;
205 }
206
207 spin_unlock(&osb->osb_lock);
208}
209
Mark Fashehccd979b2005-12-15 14:31:24 -0800210static int ocfs2_commit_cache(struct ocfs2_super *osb)
211{
212 int status = 0;
213 unsigned int flushed;
214 unsigned long old_id;
215 struct ocfs2_journal *journal = NULL;
216
217 mlog_entry_void();
218
219 journal = osb->journal;
220
221 /* Flush all pending commits and checkpoint the journal. */
222 down_write(&journal->j_trans_barrier);
223
224 if (atomic_read(&journal->j_num_trans) == 0) {
225 up_write(&journal->j_trans_barrier);
226 mlog(0, "No transactions for me to flush!\n");
227 goto finally;
228 }
229
Joel Becker2b4e30f2008-09-03 20:03:41 -0700230 jbd2_journal_lock_updates(journal->j_journal);
231 status = jbd2_journal_flush(journal->j_journal);
232 jbd2_journal_unlock_updates(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800233 if (status < 0) {
234 up_write(&journal->j_trans_barrier);
235 mlog_errno(status);
236 goto finally;
237 }
238
239 old_id = ocfs2_inc_trans_id(journal);
240
241 flushed = atomic_read(&journal->j_num_trans);
242 atomic_set(&journal->j_num_trans, 0);
243 up_write(&journal->j_trans_barrier);
244
245 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
246 journal->j_trans_id, flushed);
247
Mark Fasheh34d024f2007-09-24 15:56:19 -0700248 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800249 wake_up(&journal->j_checkpointed);
250finally:
251 mlog_exit(status);
252 return status;
253}
254
Mark Fashehccd979b2005-12-15 14:31:24 -0800255/* pass it NULL and it will allocate a new handle object for you. If
256 * you pass it a handle however, it may still return error, in which
257 * case it has free'd the passed handle for you. */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700258handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
Mark Fashehccd979b2005-12-15 14:31:24 -0800259{
Mark Fashehccd979b2005-12-15 14:31:24 -0800260 journal_t *journal = osb->journal->j_journal;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700261 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800262
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100263 BUG_ON(!osb || !osb->journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800264
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700265 if (ocfs2_is_hard_readonly(osb))
266 return ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800267
268 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
269 BUG_ON(max_buffs <= 0);
270
Jan Kara90e86a62008-08-27 22:30:28 +0200271 /* Nested transaction? Just return the handle... */
272 if (journal_current_handle())
273 return jbd2_journal_start(journal, max_buffs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800274
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 down_read(&osb->journal->j_trans_barrier);
276
Joel Becker2b4e30f2008-09-03 20:03:41 -0700277 handle = jbd2_journal_start(journal, max_buffs);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700278 if (IS_ERR(handle)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800279 up_read(&osb->journal->j_trans_barrier);
280
Mark Fasheh1fabe142006-10-09 18:11:45 -0700281 mlog_errno(PTR_ERR(handle));
Mark Fashehccd979b2005-12-15 14:31:24 -0800282
283 if (is_journal_aborted(journal)) {
284 ocfs2_abort(osb->sb, "Detected aborted journal");
Mark Fasheh1fabe142006-10-09 18:11:45 -0700285 handle = ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800286 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800287 } else {
288 if (!ocfs2_mount_local(osb))
289 atomic_inc(&(osb->journal->j_num_trans));
290 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800291
Mark Fashehccd979b2005-12-15 14:31:24 -0800292 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800293}
294
Mark Fasheh1fabe142006-10-09 18:11:45 -0700295int ocfs2_commit_trans(struct ocfs2_super *osb,
296 handle_t *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800297{
Jan Kara90e86a62008-08-27 22:30:28 +0200298 int ret, nested;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700299 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800300
301 BUG_ON(!handle);
302
Jan Kara90e86a62008-08-27 22:30:28 +0200303 nested = handle->h_ref > 1;
Joel Becker2b4e30f2008-09-03 20:03:41 -0700304 ret = jbd2_journal_stop(handle);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700305 if (ret < 0)
306 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800307
Jan Kara90e86a62008-08-27 22:30:28 +0200308 if (!nested)
309 up_read(&journal->j_trans_barrier);
Mark Fashehccd979b2005-12-15 14:31:24 -0800310
Mark Fasheh1fabe142006-10-09 18:11:45 -0700311 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800312}
313
314/*
315 * 'nblocks' is what you want to add to the current
316 * transaction. extend_trans will either extend the current handle by
317 * nblocks, or commit it and start a new one with nblocks credits.
318 *
Joel Becker2b4e30f2008-09-03 20:03:41 -0700319 * This might call jbd2_journal_restart() which will commit dirty buffers
Mark Fashehe8aed342007-12-03 16:43:01 -0800320 * and then restart the transaction. Before calling
321 * ocfs2_extend_trans(), any changed blocks should have been
322 * dirtied. After calling it, all blocks which need to be changed must
323 * go through another set of journal_access/journal_dirty calls.
324 *
Mark Fashehccd979b2005-12-15 14:31:24 -0800325 * WARNING: This will not release any semaphores or disk locks taken
326 * during the transaction, so make sure they were taken *before*
327 * start_trans or we'll have ordering deadlocks.
328 *
329 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
330 * good because transaction ids haven't yet been recorded on the
331 * cluster locks associated with this handle.
332 */
Mark Fasheh1fc58142006-10-05 14:15:36 -0700333int ocfs2_extend_trans(handle_t *handle, int nblocks)
Mark Fashehccd979b2005-12-15 14:31:24 -0800334{
335 int status;
336
337 BUG_ON(!handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800338 BUG_ON(!nblocks);
339
340 mlog_entry_void();
341
342 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
343
Joel Beckere407e392008-06-12 22:35:39 -0700344#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fasheh0879c582007-12-03 16:42:19 -0800345 status = 1;
346#else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700347 status = jbd2_journal_extend(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800348 if (status < 0) {
349 mlog_errno(status);
350 goto bail;
351 }
Mark Fasheh0879c582007-12-03 16:42:19 -0800352#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800353
354 if (status > 0) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700355 mlog(0,
356 "jbd2_journal_extend failed, trying "
357 "jbd2_journal_restart\n");
358 status = jbd2_journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 mlog_errno(status);
361 goto bail;
362 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700363 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800364
365 status = 0;
366bail:
367
368 mlog_exit(status);
369 return status;
370}
371
Mark Fasheh1fabe142006-10-09 18:11:45 -0700372int ocfs2_journal_access(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800373 struct inode *inode,
374 struct buffer_head *bh,
375 int type)
376{
377 int status;
378
379 BUG_ON(!inode);
380 BUG_ON(!handle);
381 BUG_ON(!bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800382
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800383 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800384 (unsigned long long)bh->b_blocknr, type,
385 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
386 "OCFS2_JOURNAL_ACCESS_CREATE" :
387 "OCFS2_JOURNAL_ACCESS_WRITE",
388 bh->b_size);
389
390 /* we can safely remove this assertion after testing. */
391 if (!buffer_uptodate(bh)) {
392 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
393 mlog(ML_ERROR, "b_blocknr=%llu\n",
394 (unsigned long long)bh->b_blocknr);
395 BUG();
396 }
397
398 /* Set the current transaction information on the inode so
399 * that the locking code knows whether it can drop it's locks
400 * on this inode or not. We're protected from the commit
401 * thread updating the current transaction id until
402 * ocfs2_commit_trans() because ocfs2_start_trans() took
403 * j_trans_barrier for us. */
404 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
405
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800406 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800407 switch (type) {
408 case OCFS2_JOURNAL_ACCESS_CREATE:
409 case OCFS2_JOURNAL_ACCESS_WRITE:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700410 status = jbd2_journal_get_write_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800411 break;
412
413 case OCFS2_JOURNAL_ACCESS_UNDO:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700414 status = jbd2_journal_get_undo_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800415 break;
416
417 default:
418 status = -EINVAL;
419 mlog(ML_ERROR, "Uknown access type!\n");
420 }
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800421 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800422
423 if (status < 0)
424 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
425 status, type);
426
427 mlog_exit(status);
428 return status;
429}
430
Mark Fasheh1fabe142006-10-09 18:11:45 -0700431int ocfs2_journal_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800432 struct buffer_head *bh)
433{
434 int status;
435
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 mlog_entry("(bh->b_blocknr=%llu)\n",
437 (unsigned long long)bh->b_blocknr);
438
Joel Becker2b4e30f2008-09-03 20:03:41 -0700439 status = jbd2_journal_dirty_metadata(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800440 if (status < 0)
441 mlog(ML_ERROR, "Could not dirty metadata buffer. "
442 "(bh->b_blocknr=%llu)\n",
443 (unsigned long long)bh->b_blocknr);
444
445 mlog_exit(status);
446 return status;
447}
448
Joel Becker2b4e30f2008-09-03 20:03:41 -0700449#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800450
451void ocfs2_set_journal_params(struct ocfs2_super *osb)
452{
453 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800454 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
455
456 if (osb->osb_commit_interval)
457 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800458
459 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800460 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800461 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700462 journal->j_flags |= JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800463 else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700464 journal->j_flags &= ~JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800465 spin_unlock(&journal->j_state_lock);
466}
467
468int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
469{
470 int status = -1;
471 struct inode *inode = NULL; /* the journal inode */
472 journal_t *j_journal = NULL;
473 struct ocfs2_dinode *di = NULL;
474 struct buffer_head *bh = NULL;
475 struct ocfs2_super *osb;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700476 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800477
478 mlog_entry_void();
479
480 BUG_ON(!journal);
481
482 osb = journal->j_osb;
483
484 /* already have the inode for our journal */
485 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
486 osb->slot_num);
487 if (inode == NULL) {
488 status = -EACCES;
489 mlog_errno(status);
490 goto done;
491 }
492 if (is_bad_inode(inode)) {
493 mlog(ML_ERROR, "access error (bad inode)\n");
494 iput(inode);
495 inode = NULL;
496 status = -EACCES;
497 goto done;
498 }
499
500 SET_INODE_JOURNAL(inode);
501 OCFS2_I(inode)->ip_open_count++;
502
Mark Fasheh6eff5792006-01-18 10:31:47 -0800503 /* Skip recovery waits here - journal inode metadata never
504 * changes in a live cluster so it can be considered an
505 * exception to the rule. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700506 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 if (status < 0) {
508 if (status != -ERESTARTSYS)
509 mlog(ML_ERROR, "Could not get lock on journal!\n");
510 goto done;
511 }
512
Mark Fashehe63aecb62007-10-18 15:30:42 -0700513 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800514 di = (struct ocfs2_dinode *)bh->b_data;
515
516 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
517 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
518 inode->i_size);
519 status = -EINVAL;
520 goto done;
521 }
522
523 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800524 mlog(0, "inode->i_blocks = %llu\n",
525 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
527
528 /* call the kernels journal init function now */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700529 j_journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800530 if (j_journal == NULL) {
531 mlog(ML_ERROR, "Linux journal layer error\n");
532 status = -EINVAL;
533 goto done;
534 }
535
Joel Becker2b4e30f2008-09-03 20:03:41 -0700536 mlog(0, "Returned from jbd2_journal_init_inode\n");
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
538
539 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
540 OCFS2_JOURNAL_DIRTY_FL);
541
542 journal->j_journal = j_journal;
543 journal->j_inode = inode;
544 journal->j_bh = bh;
545
546 ocfs2_set_journal_params(osb);
547
548 journal->j_state = OCFS2_JOURNAL_LOADED;
549
550 status = 0;
551done:
552 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700553 if (inode_lock)
554 ocfs2_inode_unlock(inode, 1);
Mark Fasheha81cb882008-10-07 14:25:16 -0700555 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800556 if (inode) {
557 OCFS2_I(inode)->ip_open_count--;
558 iput(inode);
559 }
560 }
561
562 mlog_exit(status);
563 return status;
564}
565
Sunil Mushran539d8262008-07-14 17:31:10 -0700566static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
567{
568 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
569}
570
571static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
572{
573 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
574}
575
Mark Fashehccd979b2005-12-15 14:31:24 -0800576static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -0700577 int dirty, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800578{
579 int status;
580 unsigned int flags;
581 struct ocfs2_journal *journal = osb->journal;
582 struct buffer_head *bh = journal->j_bh;
583 struct ocfs2_dinode *fe;
584
585 mlog_entry_void();
586
587 fe = (struct ocfs2_dinode *)bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800588
589 /* The journal bh on the osb always comes from ocfs2_journal_init()
590 * and was validated there inside ocfs2_inode_lock_full(). It's a
591 * code bug if we mess it up. */
592 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800593
594 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
595 if (dirty)
596 flags |= OCFS2_JOURNAL_DIRTY_FL;
597 else
598 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
599 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
600
Sunil Mushran539d8262008-07-14 17:31:10 -0700601 if (replayed)
602 ocfs2_bump_recovery_generation(fe);
603
Mark Fashehccd979b2005-12-15 14:31:24 -0800604 status = ocfs2_write_block(osb, bh, journal->j_inode);
605 if (status < 0)
606 mlog_errno(status);
607
Mark Fashehccd979b2005-12-15 14:31:24 -0800608 mlog_exit(status);
609 return status;
610}
611
612/*
613 * If the journal has been kmalloc'd it needs to be freed after this
614 * call.
615 */
616void ocfs2_journal_shutdown(struct ocfs2_super *osb)
617{
618 struct ocfs2_journal *journal = NULL;
619 int status = 0;
620 struct inode *inode = NULL;
621 int num_running_trans = 0;
622
623 mlog_entry_void();
624
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100625 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800626
627 journal = osb->journal;
628 if (!journal)
629 goto done;
630
631 inode = journal->j_inode;
632
633 if (journal->j_state != OCFS2_JOURNAL_LOADED)
634 goto done;
635
Joel Becker2b4e30f2008-09-03 20:03:41 -0700636 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800637 if (!igrab(inode))
638 BUG();
639
640 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
641 if (num_running_trans > 0)
642 mlog(0, "Shutting down journal: must wait on %d "
643 "running transactions!\n",
644 num_running_trans);
645
646 /* Do a commit_cache here. It will flush our journal, *and*
647 * release any locks that are still held.
648 * set the SHUTDOWN flag and release the trans lock.
649 * the commit thread will take the trans lock for us below. */
650 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
651
652 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
653 * drop the trans_lock (which we want to hold until we
654 * completely destroy the journal. */
655 if (osb->commit_task) {
656 /* Wait for the commit thread */
657 mlog(0, "Waiting for ocfs2commit to exit....\n");
658 kthread_stop(osb->commit_task);
659 osb->commit_task = NULL;
660 }
661
662 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
663
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800664 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700665 jbd2_journal_lock_updates(journal->j_journal);
666 status = jbd2_journal_flush(journal->j_journal);
667 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800668 if (status < 0)
669 mlog_errno(status);
670 }
671
672 if (status == 0) {
673 /*
674 * Do not toggle if flush was unsuccessful otherwise
675 * will leave dirty metadata in a "clean" journal
676 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700677 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800678 if (status < 0)
679 mlog_errno(status);
680 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800681
682 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700683 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700684 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800685
686 OCFS2_I(inode)->ip_open_count--;
687
688 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700689 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800690
691 brelse(journal->j_bh);
692 journal->j_bh = NULL;
693
694 journal->j_state = OCFS2_JOURNAL_FREE;
695
696// up_write(&journal->j_trans_barrier);
697done:
698 if (inode)
699 iput(inode);
700 mlog_exit_void();
701}
702
703static void ocfs2_clear_journal_error(struct super_block *sb,
704 journal_t *journal,
705 int slot)
706{
707 int olderr;
708
Joel Becker2b4e30f2008-09-03 20:03:41 -0700709 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800710 if (olderr) {
711 mlog(ML_ERROR, "File system error %d recorded in "
712 "journal %u.\n", olderr, slot);
713 mlog(ML_ERROR, "File system on device %s needs checking.\n",
714 sb->s_id);
715
Joel Becker2b4e30f2008-09-03 20:03:41 -0700716 jbd2_journal_ack_err(journal);
717 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800718 }
719}
720
Sunil Mushran539d8262008-07-14 17:31:10 -0700721int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800722{
723 int status = 0;
724 struct ocfs2_super *osb;
725
726 mlog_entry_void();
727
Julia Lawallb1f35502008-03-04 15:21:05 -0800728 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800729
730 osb = journal->j_osb;
731
Joel Becker2b4e30f2008-09-03 20:03:41 -0700732 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800733 if (status < 0) {
734 mlog(ML_ERROR, "Failed to load journal!\n");
735 goto done;
736 }
737
738 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
739
Sunil Mushran539d8262008-07-14 17:31:10 -0700740 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 if (status < 0) {
742 mlog_errno(status);
743 goto done;
744 }
745
746 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800747 if (!local) {
748 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
749 "ocfs2cmt");
750 if (IS_ERR(osb->commit_task)) {
751 status = PTR_ERR(osb->commit_task);
752 osb->commit_task = NULL;
753 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
754 "error=%d", status);
755 goto done;
756 }
757 } else
Mark Fashehccd979b2005-12-15 14:31:24 -0800758 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800759
760done:
761 mlog_exit(status);
762 return status;
763}
764
765
766/* 'full' flag tells us whether we clear out all blocks or if we just
767 * mark the journal clean */
768int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
769{
770 int status;
771
772 mlog_entry_void();
773
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100774 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800775
Joel Becker2b4e30f2008-09-03 20:03:41 -0700776 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 if (status < 0) {
778 mlog_errno(status);
779 goto bail;
780 }
781
Sunil Mushran539d8262008-07-14 17:31:10 -0700782 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800783 if (status < 0)
784 mlog_errno(status);
785
786bail:
787 mlog_exit(status);
788 return status;
789}
790
Joel Becker553abd02008-02-01 12:03:57 -0800791static int ocfs2_recovery_completed(struct ocfs2_super *osb)
792{
793 int empty;
794 struct ocfs2_recovery_map *rm = osb->recovery_map;
795
796 spin_lock(&osb->osb_lock);
797 empty = (rm->rm_used == 0);
798 spin_unlock(&osb->osb_lock);
799
800 return empty;
801}
802
803void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
804{
805 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
806}
807
Mark Fashehccd979b2005-12-15 14:31:24 -0800808/*
809 * JBD Might read a cached version of another nodes journal file. We
810 * don't want this as this file changes often and we get no
811 * notification on those changes. The only way to be sure that we've
812 * got the most up to date version of those blocks then is to force
813 * read them off disk. Just searching through the buffer cache won't
814 * work as there may be pages backing this file which are still marked
815 * up to date. We know things can't change on this file underneath us
816 * as we have the lock by now :)
817 */
818static int ocfs2_force_read_journal(struct inode *inode)
819{
820 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800821 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -0700822 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800823#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -0800824 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
825
826 mlog_entry_void();
827
Mark Fashehccd979b2005-12-15 14:31:24 -0800828 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
829
Mark Fasheh8110b072007-03-22 16:53:23 -0700830 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -0800831 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -0700832 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800834 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 if (status < 0) {
836 mlog_errno(status);
837 goto bail;
838 }
839
840 if (p_blocks > CONCURRENT_JOURNAL_FILL)
841 p_blocks = CONCURRENT_JOURNAL_FILL;
842
Mark Fashehdd4a2c22006-04-12 14:24:05 -0700843 /* We are reading journal data which should not
844 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -0700845 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
846 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800847 if (status < 0) {
848 mlog_errno(status);
849 goto bail;
850 }
851
852 for(i = 0; i < p_blocks; i++) {
853 brelse(bhs[i]);
854 bhs[i] = NULL;
855 }
856
857 v_blkno += p_blocks;
858 }
859
860bail:
861 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -0700862 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800863 mlog_exit(status);
864 return status;
865}
866
867struct ocfs2_la_recovery_item {
868 struct list_head lri_list;
869 int lri_slot;
870 struct ocfs2_dinode *lri_la_dinode;
871 struct ocfs2_dinode *lri_tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +0200872 struct ocfs2_quota_recovery *lri_qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800873};
874
875/* Does the second half of the recovery process. By this point, the
876 * node is marked clean and can actually be considered recovered,
877 * hence it's no longer in the recovery map, but there's still some
878 * cleanup we can do which shouldn't happen within the recovery thread
879 * as locking in that context becomes very difficult if we are to take
880 * recovering nodes into account.
881 *
882 * NOTE: This function can and will sleep on recovery of other nodes
883 * during cluster locking, just like any other ocfs2 process.
884 */
David Howellsc4028952006-11-22 14:57:56 +0000885void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -0800886{
887 int ret;
David Howellsc4028952006-11-22 14:57:56 +0000888 struct ocfs2_journal *journal =
889 container_of(work, struct ocfs2_journal, j_recovery_work);
890 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +0200892 struct ocfs2_la_recovery_item *item, *n;
Jan Kara22053632008-10-20 23:50:38 +0200893 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800894 LIST_HEAD(tmp_la_list);
895
896 mlog_entry_void();
897
898 mlog(0, "completing recovery from keventd\n");
899
900 spin_lock(&journal->j_lock);
901 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
902 spin_unlock(&journal->j_lock);
903
Christoph Hellwig800deef2007-05-17 16:03:13 +0200904 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800905 list_del_init(&item->lri_list);
906
907 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
908
Jan Kara19ece542008-08-21 20:13:17 +0200909 ocfs2_wait_on_quotas(osb);
910
Mark Fashehccd979b2005-12-15 14:31:24 -0800911 la_dinode = item->lri_la_dinode;
912 if (la_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -0800913 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700914 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800915
916 ret = ocfs2_complete_local_alloc_recovery(osb,
917 la_dinode);
918 if (ret < 0)
919 mlog_errno(ret);
920
921 kfree(la_dinode);
922 }
923
924 tl_dinode = item->lri_tl_dinode;
925 if (tl_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -0800926 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700927 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800928
929 ret = ocfs2_complete_truncate_log_recovery(osb,
930 tl_dinode);
931 if (ret < 0)
932 mlog_errno(ret);
933
934 kfree(tl_dinode);
935 }
936
937 ret = ocfs2_recover_orphans(osb, item->lri_slot);
938 if (ret < 0)
939 mlog_errno(ret);
940
Jan Kara22053632008-10-20 23:50:38 +0200941 qrec = item->lri_qrec;
942 if (qrec) {
943 mlog(0, "Recovering quota files");
944 ret = ocfs2_finish_quota_recovery(osb, qrec,
945 item->lri_slot);
946 if (ret < 0)
947 mlog_errno(ret);
948 /* Recovery info is already freed now */
949 }
950
Mark Fashehccd979b2005-12-15 14:31:24 -0800951 kfree(item);
952 }
953
954 mlog(0, "Recovery completion\n");
955 mlog_exit_void();
956}
957
958/* NOTE: This function always eats your references to la_dinode and
959 * tl_dinode, either manually on error, or by passing them to
960 * ocfs2_complete_recovery */
961static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
962 int slot_num,
963 struct ocfs2_dinode *la_dinode,
Jan Kara22053632008-10-20 23:50:38 +0200964 struct ocfs2_dinode *tl_dinode,
965 struct ocfs2_quota_recovery *qrec)
Mark Fashehccd979b2005-12-15 14:31:24 -0800966{
967 struct ocfs2_la_recovery_item *item;
968
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700969 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 if (!item) {
971 /* Though we wish to avoid it, we are in fact safe in
972 * skipping local alloc cleanup as fsck.ocfs2 is more
973 * than capable of reclaiming unused space. */
974 if (la_dinode)
975 kfree(la_dinode);
976
977 if (tl_dinode)
978 kfree(tl_dinode);
979
Jan Kara22053632008-10-20 23:50:38 +0200980 if (qrec)
981 ocfs2_free_quota_recovery(qrec);
982
Mark Fashehccd979b2005-12-15 14:31:24 -0800983 mlog_errno(-ENOMEM);
984 return;
985 }
986
987 INIT_LIST_HEAD(&item->lri_list);
988 item->lri_la_dinode = la_dinode;
989 item->lri_slot = slot_num;
990 item->lri_tl_dinode = tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +0200991 item->lri_qrec = qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800992
993 spin_lock(&journal->j_lock);
994 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
995 queue_work(ocfs2_wq, &journal->j_recovery_work);
996 spin_unlock(&journal->j_lock);
997}
998
999/* Called by the mount code to queue recovery the last part of
1000 * recovery for it's own slot. */
1001void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1002{
1003 struct ocfs2_journal *journal = osb->journal;
1004
1005 if (osb->dirty) {
1006 /* No need to queue up our truncate_log as regular
1007 * cleanup will catch that. */
1008 ocfs2_queue_recovery_completion(journal,
1009 osb->slot_num,
1010 osb->local_alloc_copy,
Jan Kara22053632008-10-20 23:50:38 +02001011 NULL,
Mark Fashehccd979b2005-12-15 14:31:24 -08001012 NULL);
1013 ocfs2_schedule_truncate_log_flush(osb, 0);
1014
1015 osb->local_alloc_copy = NULL;
1016 osb->dirty = 0;
1017 }
1018}
1019
Jan Kara22053632008-10-20 23:50:38 +02001020void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1021{
1022 if (osb->quota_rec) {
1023 ocfs2_queue_recovery_completion(osb->journal,
1024 osb->slot_num,
1025 NULL,
1026 NULL,
1027 osb->quota_rec);
1028 osb->quota_rec = NULL;
1029 }
1030}
1031
Mark Fashehccd979b2005-12-15 14:31:24 -08001032static int __ocfs2_recovery_thread(void *arg)
1033{
Jan Kara22053632008-10-20 23:50:38 +02001034 int status, node_num, slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001035 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001036 struct ocfs2_recovery_map *rm = osb->recovery_map;
Jan Kara22053632008-10-20 23:50:38 +02001037 int *rm_quota = NULL;
1038 int rm_quota_used = 0, i;
1039 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001040
1041 mlog_entry_void();
1042
1043 status = ocfs2_wait_on_mount(osb);
1044 if (status < 0) {
1045 goto bail;
1046 }
1047
Jan Kara22053632008-10-20 23:50:38 +02001048 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1049 if (!rm_quota) {
1050 status = -ENOMEM;
1051 goto bail;
1052 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001053restart:
1054 status = ocfs2_super_lock(osb, 1);
1055 if (status < 0) {
1056 mlog_errno(status);
1057 goto bail;
1058 }
1059
Joel Becker553abd02008-02-01 12:03:57 -08001060 spin_lock(&osb->osb_lock);
1061 while (rm->rm_used) {
1062 /* It's always safe to remove entry zero, as we won't
1063 * clear it until ocfs2_recover_node() has succeeded. */
1064 node_num = rm->rm_entries[0];
1065 spin_unlock(&osb->osb_lock);
Jan Kara22053632008-10-20 23:50:38 +02001066 mlog(0, "checking node %d\n", node_num);
1067 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1068 if (slot_num == -ENOENT) {
1069 status = 0;
1070 mlog(0, "no slot for this node, so no recovery"
1071 "required.\n");
1072 goto skip_recovery;
1073 }
1074 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001075
Jan Kara22053632008-10-20 23:50:38 +02001076 /* It is a bit subtle with quota recovery. We cannot do it
1077 * immediately because we have to obtain cluster locks from
1078 * quota files and we also don't want to just skip it because
1079 * then quota usage would be out of sync until some node takes
1080 * the slot. So we remember which nodes need quota recovery
1081 * and when everything else is done, we recover quotas. */
1082 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1083 if (i == rm_quota_used)
1084 rm_quota[rm_quota_used++] = slot_num;
1085
1086 status = ocfs2_recover_node(osb, node_num, slot_num);
1087skip_recovery:
Joel Becker553abd02008-02-01 12:03:57 -08001088 if (!status) {
1089 ocfs2_recovery_map_clear(osb, node_num);
1090 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001091 mlog(ML_ERROR,
1092 "Error %d recovering node %d on device (%u,%u)!\n",
1093 status, node_num,
1094 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1095 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001096 }
1097
Joel Becker553abd02008-02-01 12:03:57 -08001098 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 }
Joel Becker553abd02008-02-01 12:03:57 -08001100 spin_unlock(&osb->osb_lock);
1101 mlog(0, "All nodes recovered\n");
1102
Sunil Mushran539d8262008-07-14 17:31:10 -07001103 /* Refresh all journal recovery generations from disk */
1104 status = ocfs2_check_journals_nolocks(osb);
1105 status = (status == -EROFS) ? 0 : status;
1106 if (status < 0)
1107 mlog_errno(status);
1108
Jan Kara22053632008-10-20 23:50:38 +02001109 /* Now it is right time to recover quotas... We have to do this under
1110 * superblock lock so that noone can start using the slot (and crash)
1111 * before we recover it */
1112 for (i = 0; i < rm_quota_used; i++) {
1113 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1114 if (IS_ERR(qrec)) {
1115 status = PTR_ERR(qrec);
1116 mlog_errno(status);
1117 continue;
1118 }
1119 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1120 NULL, NULL, qrec);
1121 }
1122
Mark Fashehccd979b2005-12-15 14:31:24 -08001123 ocfs2_super_unlock(osb, 1);
1124
1125 /* We always run recovery on our own orphan dir - the dead
Mark Fasheh34d024f2007-09-24 15:56:19 -07001126 * node(s) may have disallowd a previos inode delete. Re-processing
1127 * is therefore required. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001128 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
Jan Kara22053632008-10-20 23:50:38 +02001129 NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001130
1131bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001132 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001133 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001134 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 goto restart;
1136 }
1137
1138 osb->recovery_thread_task = NULL;
1139 mb(); /* sync with ocfs2_recovery_thread_running */
1140 wake_up(&osb->recovery_event);
1141
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001142 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001143
Jan Kara22053632008-10-20 23:50:38 +02001144 if (rm_quota)
1145 kfree(rm_quota);
1146
Mark Fashehccd979b2005-12-15 14:31:24 -08001147 mlog_exit(status);
1148 /* no one is callint kthread_stop() for us so the kthread() api
1149 * requires that we call do_exit(). And it isn't exported, but
1150 * complete_and_exit() seems to be a minimal wrapper around it. */
1151 complete_and_exit(NULL, status);
1152 return status;
1153}
1154
1155void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1156{
1157 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1158 node_num, osb->node_num);
1159
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001160 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001161 if (osb->disable_recovery)
1162 goto out;
1163
1164 /* People waiting on recovery will wait on
1165 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001166 if (ocfs2_recovery_map_set(osb, node_num))
1167 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001168
1169 mlog(0, "starting recovery thread...\n");
1170
1171 if (osb->recovery_thread_task)
1172 goto out;
1173
1174 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001175 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001176 if (IS_ERR(osb->recovery_thread_task)) {
1177 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1178 osb->recovery_thread_task = NULL;
1179 }
1180
1181out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001182 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001183 wake_up(&osb->recovery_event);
1184
1185 mlog_exit_void();
1186}
1187
Sunil Mushran539d8262008-07-14 17:31:10 -07001188static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1189 int slot_num,
1190 struct buffer_head **bh,
1191 struct inode **ret_inode)
1192{
1193 int status = -EACCES;
1194 struct inode *inode = NULL;
1195
1196 BUG_ON(slot_num >= osb->max_slots);
1197
1198 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1199 slot_num);
1200 if (!inode || is_bad_inode(inode)) {
1201 mlog_errno(status);
1202 goto bail;
1203 }
1204 SET_INODE_JOURNAL(inode);
1205
Joel Beckerb657c952008-11-13 14:49:11 -08001206 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001207 if (status < 0) {
1208 mlog_errno(status);
1209 goto bail;
1210 }
1211
1212 status = 0;
1213
1214bail:
1215 if (inode) {
1216 if (status || !ret_inode)
1217 iput(inode);
1218 else
1219 *ret_inode = inode;
1220 }
1221 return status;
1222}
1223
Mark Fashehccd979b2005-12-15 14:31:24 -08001224/* Does the actual journal replay and marks the journal inode as
1225 * clean. Will only replay if the journal inode is marked dirty. */
1226static int ocfs2_replay_journal(struct ocfs2_super *osb,
1227 int node_num,
1228 int slot_num)
1229{
1230 int status;
1231 int got_lock = 0;
1232 unsigned int flags;
1233 struct inode *inode = NULL;
1234 struct ocfs2_dinode *fe;
1235 journal_t *journal = NULL;
1236 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001237 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001238
Sunil Mushran539d8262008-07-14 17:31:10 -07001239 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1240 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 mlog_errno(status);
1242 goto done;
1243 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001244
1245 fe = (struct ocfs2_dinode *)bh->b_data;
1246 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1247 brelse(bh);
1248 bh = NULL;
1249
1250 /*
1251 * As the fs recovery is asynchronous, there is a small chance that
1252 * another node mounted (and recovered) the slot before the recovery
1253 * thread could get the lock. To handle that, we dirty read the journal
1254 * inode for that slot to get the recovery generation. If it is
1255 * different than what we expected, the slot has been recovered.
1256 * If not, it needs recovery.
1257 */
1258 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1259 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1260 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1261 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1262 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 goto done;
1264 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001265
1266 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001267
Mark Fashehe63aecb62007-10-18 15:30:42 -07001268 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001269 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001270 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001271 if (status != -ERESTARTSYS)
1272 mlog(ML_ERROR, "Could not lock journal!\n");
1273 goto done;
1274 }
1275 got_lock = 1;
1276
1277 fe = (struct ocfs2_dinode *) bh->b_data;
1278
1279 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001280 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001281
1282 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1283 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001284 /* Refresh recovery generation for the slot */
1285 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001286 goto done;
1287 }
1288
1289 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1290 node_num, slot_num,
1291 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1292
1293 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1294
1295 status = ocfs2_force_read_journal(inode);
1296 if (status < 0) {
1297 mlog_errno(status);
1298 goto done;
1299 }
1300
1301 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001302 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001303 if (journal == NULL) {
1304 mlog(ML_ERROR, "Linux journal layer error\n");
1305 status = -EIO;
1306 goto done;
1307 }
1308
Joel Becker2b4e30f2008-09-03 20:03:41 -07001309 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 if (status < 0) {
1311 mlog_errno(status);
1312 if (!igrab(inode))
1313 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001314 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 goto done;
1316 }
1317
1318 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1319
1320 /* wipe the journal */
1321 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001322 jbd2_journal_lock_updates(journal);
1323 status = jbd2_journal_flush(journal);
1324 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 if (status < 0)
1326 mlog_errno(status);
1327
1328 /* This will mark the node clean */
1329 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1330 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1331 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1332
Sunil Mushran539d8262008-07-14 17:31:10 -07001333 /* Increment recovery generation to indicate successful recovery */
1334 ocfs2_bump_recovery_generation(fe);
1335 osb->slot_recovery_generations[slot_num] =
1336 ocfs2_get_recovery_generation(fe);
1337
Mark Fashehccd979b2005-12-15 14:31:24 -08001338 status = ocfs2_write_block(osb, bh, inode);
1339 if (status < 0)
1340 mlog_errno(status);
1341
1342 if (!igrab(inode))
1343 BUG();
1344
Joel Becker2b4e30f2008-09-03 20:03:41 -07001345 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001346
1347done:
1348 /* drop the lock on this nodes journal */
1349 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001350 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001351
1352 if (inode)
1353 iput(inode);
1354
Mark Fasheha81cb882008-10-07 14:25:16 -07001355 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001356
1357 mlog_exit(status);
1358 return status;
1359}
1360
1361/*
1362 * Do the most important parts of node recovery:
1363 * - Replay it's journal
1364 * - Stamp a clean local allocator file
1365 * - Stamp a clean truncate log
1366 * - Mark the node clean
1367 *
1368 * If this function completes without error, a node in OCFS2 can be
1369 * said to have been safely recovered. As a result, failure during the
1370 * second part of a nodes recovery process (local alloc recovery) is
1371 * far less concerning.
1372 */
1373static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +02001374 int node_num, int slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -08001375{
1376 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001377 struct ocfs2_dinode *la_copy = NULL;
1378 struct ocfs2_dinode *tl_copy = NULL;
1379
Jan Kara22053632008-10-20 23:50:38 +02001380 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1381 node_num, slot_num, osb->node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001382
1383 /* Should not ever be called to recover ourselves -- in that
1384 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001385 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001386
Mark Fashehccd979b2005-12-15 14:31:24 -08001387 status = ocfs2_replay_journal(osb, node_num, slot_num);
1388 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001389 if (status == -EBUSY) {
1390 mlog(0, "Skipping recovery for slot %u (node %u) "
1391 "as another node has recovered it\n", slot_num,
1392 node_num);
1393 status = 0;
1394 goto done;
1395 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001396 mlog_errno(status);
1397 goto done;
1398 }
1399
1400 /* Stamp a clean local alloc file AFTER recovering the journal... */
1401 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1402 if (status < 0) {
1403 mlog_errno(status);
1404 goto done;
1405 }
1406
1407 /* An error from begin_truncate_log_recovery is not
1408 * serious enough to warrant halting the rest of
1409 * recovery. */
1410 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1411 if (status < 0)
1412 mlog_errno(status);
1413
1414 /* Likewise, this would be a strange but ultimately not so
1415 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001416 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001417 if (status < 0)
1418 mlog_errno(status);
1419
1420 /* This will kfree the memory pointed to by la_copy and tl_copy */
1421 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
Jan Kara22053632008-10-20 23:50:38 +02001422 tl_copy, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001423
1424 status = 0;
1425done:
1426
1427 mlog_exit(status);
1428 return status;
1429}
1430
1431/* Test node liveness by trylocking his journal. If we get the lock,
1432 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1433 * still alive (we couldn't get the lock) and < 0 on error. */
1434static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1435 int slot_num)
1436{
1437 int status, flags;
1438 struct inode *inode = NULL;
1439
1440 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1441 slot_num);
1442 if (inode == NULL) {
1443 mlog(ML_ERROR, "access error\n");
1444 status = -EACCES;
1445 goto bail;
1446 }
1447 if (is_bad_inode(inode)) {
1448 mlog(ML_ERROR, "access error (bad inode)\n");
1449 iput(inode);
1450 inode = NULL;
1451 status = -EACCES;
1452 goto bail;
1453 }
1454 SET_INODE_JOURNAL(inode);
1455
1456 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001457 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 if (status < 0) {
1459 if (status != -EAGAIN)
1460 mlog_errno(status);
1461 goto bail;
1462 }
1463
Mark Fashehe63aecb62007-10-18 15:30:42 -07001464 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001465bail:
1466 if (inode)
1467 iput(inode);
1468
1469 return status;
1470}
1471
1472/* Call this underneath ocfs2_super_lock. It also assumes that the
1473 * slot info struct has been updated from disk. */
1474int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1475{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001476 unsigned int node_num;
1477 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001478 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001479 struct buffer_head *bh = NULL;
1480 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001481
1482 /* This is called with the super block cluster lock, so we
1483 * know that the slot map can't change underneath us. */
1484
Joel Beckerd85b20e2008-02-01 12:01:05 -08001485 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001486 /* Read journal inode to get the recovery generation */
1487 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1488 if (status) {
1489 mlog_errno(status);
1490 goto bail;
1491 }
1492 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001493 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001494 brelse(bh);
1495 bh = NULL;
1496
Mark Fasheha1af7d12008-08-19 17:20:28 -07001497 spin_lock(&osb->osb_lock);
1498 osb->slot_recovery_generations[i] = gen;
1499
Sunil Mushran539d8262008-07-14 17:31:10 -07001500 mlog(0, "Slot %u recovery generation is %u\n", i,
1501 osb->slot_recovery_generations[i]);
1502
Mark Fasheha1af7d12008-08-19 17:20:28 -07001503 if (i == osb->slot_num) {
1504 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001506 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001507
1508 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001509 if (status == -ENOENT) {
1510 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001511 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001512 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001513
Mark Fasheha1af7d12008-08-19 17:20:28 -07001514 if (__ocfs2_recovery_map_test(osb, node_num)) {
1515 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001516 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001517 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001518 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001519
1520 /* Ok, we have a slot occupied by another node which
1521 * is not in the recovery map. We trylock his journal
1522 * file here to test if he's alive. */
1523 status = ocfs2_trylock_journal(osb, i);
1524 if (!status) {
1525 /* Since we're called from mount, we know that
1526 * the recovery thread can't race us on
1527 * setting / checking the recovery bits. */
1528 ocfs2_recovery_thread(osb, node_num);
1529 } else if ((status < 0) && (status != -EAGAIN)) {
1530 mlog_errno(status);
1531 goto bail;
1532 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001534
1535 status = 0;
1536bail:
1537 mlog_exit(status);
1538 return status;
1539}
1540
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001541struct ocfs2_orphan_filldir_priv {
1542 struct inode *head;
1543 struct ocfs2_super *osb;
1544};
1545
1546static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1547 loff_t pos, u64 ino, unsigned type)
1548{
1549 struct ocfs2_orphan_filldir_priv *p = priv;
1550 struct inode *iter;
1551
1552 if (name_len == 1 && !strncmp(".", name, 1))
1553 return 0;
1554 if (name_len == 2 && !strncmp("..", name, 2))
1555 return 0;
1556
1557 /* Skip bad inodes so that recovery can continue */
1558 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001559 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001560 if (IS_ERR(iter))
1561 return 0;
1562
1563 mlog(0, "queue orphan %llu\n",
1564 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1565 /* No locking is required for the next_orphan queue as there
1566 * is only ever a single process doing orphan recovery. */
1567 OCFS2_I(iter)->ip_next_orphan = p->head;
1568 p->head = iter;
1569
1570 return 0;
1571}
1572
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001573static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1574 int slot,
1575 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001576{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001577 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001578 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001579 struct ocfs2_orphan_filldir_priv priv;
1580 loff_t pos = 0;
1581
1582 priv.osb = osb;
1583 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001584
1585 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1586 ORPHAN_DIR_SYSTEM_INODE,
1587 slot);
1588 if (!orphan_dir_inode) {
1589 status = -ENOENT;
1590 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001591 return status;
1592 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001593
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001594 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001595 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001596 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001597 mlog_errno(status);
1598 goto out;
1599 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001600
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001601 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1602 ocfs2_orphan_filldir);
1603 if (status) {
1604 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001605 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001607
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001608 *head = priv.head;
1609
Mark Fasheha86370f2007-12-03 14:06:23 -08001610out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001611 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001612out:
1613 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001614 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001615 return status;
1616}
1617
1618static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1619 int slot)
1620{
1621 int ret;
1622
1623 spin_lock(&osb->osb_lock);
1624 ret = !osb->osb_orphan_wipes[slot];
1625 spin_unlock(&osb->osb_lock);
1626 return ret;
1627}
1628
1629static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1630 int slot)
1631{
1632 spin_lock(&osb->osb_lock);
1633 /* Mark ourselves such that new processes in delete_inode()
1634 * know to quit early. */
1635 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1636 while (osb->osb_orphan_wipes[slot]) {
1637 /* If any processes are already in the middle of an
1638 * orphan wipe on this dir, then we need to wait for
1639 * them. */
1640 spin_unlock(&osb->osb_lock);
1641 wait_event_interruptible(osb->osb_wipe_event,
1642 ocfs2_orphan_recovery_can_continue(osb, slot));
1643 spin_lock(&osb->osb_lock);
1644 }
1645 spin_unlock(&osb->osb_lock);
1646}
1647
1648static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1649 int slot)
1650{
1651 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1652}
1653
1654/*
1655 * Orphan recovery. Each mounted node has it's own orphan dir which we
1656 * must run during recovery. Our strategy here is to build a list of
1657 * the inodes in the orphan dir and iget/iput them. The VFS does
1658 * (most) of the rest of the work.
1659 *
1660 * Orphan recovery can happen at any time, not just mount so we have a
1661 * couple of extra considerations.
1662 *
1663 * - We grab as many inodes as we can under the orphan dir lock -
1664 * doing iget() outside the orphan dir risks getting a reference on
1665 * an invalid inode.
1666 * - We must be sure not to deadlock with other processes on the
1667 * system wanting to run delete_inode(). This can happen when they go
1668 * to lock the orphan dir and the orphan recovery process attempts to
1669 * iget() inside the orphan dir lock. This can be avoided by
1670 * advertising our state to ocfs2_delete_inode().
1671 */
1672static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1673 int slot)
1674{
1675 int ret = 0;
1676 struct inode *inode = NULL;
1677 struct inode *iter;
1678 struct ocfs2_inode_info *oi;
1679
1680 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1681
1682 ocfs2_mark_recovering_orphan_dir(osb, slot);
1683 ret = ocfs2_queue_orphans(osb, slot, &inode);
1684 ocfs2_clear_recovering_orphan_dir(osb, slot);
1685
1686 /* Error here should be noted, but we want to continue with as
1687 * many queued inodes as we've got. */
1688 if (ret)
1689 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001690
1691 while (inode) {
1692 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001693 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001694
1695 iter = oi->ip_next_orphan;
1696
1697 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001698 /* The remote delete code may have set these on the
1699 * assumption that the other node would wipe them
1700 * successfully. If they are still in the node's
1701 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001702 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
1703
1704 /* Set the proper information to get us going into
1705 * ocfs2_delete_inode. */
1706 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08001707 spin_unlock(&oi->ip_lock);
1708
1709 iput(inode);
1710
1711 inode = iter;
1712 }
1713
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001714 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001715}
1716
Jan Kara19ece542008-08-21 20:13:17 +02001717static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
Mark Fashehccd979b2005-12-15 14:31:24 -08001718{
1719 /* This check is good because ocfs2 will wait on our recovery
1720 * thread before changing it to something other than MOUNTED
1721 * or DISABLED. */
1722 wait_event(osb->osb_mount_event,
Jan Kara19ece542008-08-21 20:13:17 +02001723 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
1724 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
Mark Fashehccd979b2005-12-15 14:31:24 -08001725 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
1726
1727 /* If there's an error on mount, then we may never get to the
1728 * MOUNTED flag, but this is set right before
1729 * dismount_volume() so we can trust it. */
1730 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
1731 mlog(0, "mount error, exiting!\n");
1732 return -EBUSY;
1733 }
1734
1735 return 0;
1736}
1737
1738static int ocfs2_commit_thread(void *arg)
1739{
1740 int status;
1741 struct ocfs2_super *osb = arg;
1742 struct ocfs2_journal *journal = osb->journal;
1743
1744 /* we can trust j_num_trans here because _should_stop() is only set in
1745 * shutdown and nobody other than ourselves should be able to start
1746 * transactions. committing on shutdown might take a few iterations
1747 * as final transactions put deleted inodes on the list */
1748 while (!(kthread_should_stop() &&
1749 atomic_read(&journal->j_num_trans) == 0)) {
1750
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08001751 wait_event_interruptible(osb->checkpoint_event,
1752 atomic_read(&journal->j_num_trans)
1753 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08001754
1755 status = ocfs2_commit_cache(osb);
1756 if (status < 0)
1757 mlog_errno(status);
1758
1759 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
1760 mlog(ML_KTHREAD,
1761 "commit_thread: %u transactions pending on "
1762 "shutdown\n",
1763 atomic_read(&journal->j_num_trans));
1764 }
1765 }
1766
1767 return 0;
1768}
1769
Sunil Mushran539d8262008-07-14 17:31:10 -07001770/* Reads all the journal inodes without taking any cluster locks. Used
1771 * for hard readonly access to determine whether any journal requires
1772 * recovery. Also used to refresh the recovery generation numbers after
1773 * a journal has been recovered by another node.
1774 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001775int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
1776{
1777 int ret = 0;
1778 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07001779 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001780 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07001781 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001782
1783 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001784 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
1785 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001786 mlog_errno(ret);
1787 goto out;
1788 }
1789
1790 di = (struct ocfs2_dinode *) di_bh->b_data;
1791
Sunil Mushran539d8262008-07-14 17:31:10 -07001792 osb->slot_recovery_generations[slot] =
1793 ocfs2_get_recovery_generation(di);
1794
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 if (le32_to_cpu(di->id1.journal1.ij_flags) &
1796 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07001797 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001798
1799 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07001800 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001801 }
1802
1803out:
Sunil Mushran539d8262008-07-14 17:31:10 -07001804 if (journal_dirty)
1805 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08001806 return ret;
1807}