blob: a20a0f1e37fd18e898ade8803d19ff7489df7e85 [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);
Srinivas Eeda9140db02009-03-06 14:21:46 -080068static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
69 int slot_num,
70 struct ocfs2_dinode *la_dinode,
71 struct ocfs2_dinode *tl_dinode,
72 struct ocfs2_quota_recovery *qrec);
Mark Fashehccd979b2005-12-15 14:31:24 -080073
Jan Kara19ece542008-08-21 20:13:17 +020074static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
75{
76 return __ocfs2_wait_on_mount(osb, 0);
77}
78
79static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
80{
81 return __ocfs2_wait_on_mount(osb, 1);
82}
83
Srinivas Eeda9140db02009-03-06 14:21:46 -080084/*
85 * This replay_map is to track online/offline slots, so we could recover
86 * offline slots during recovery and mount
87 */
88
89enum ocfs2_replay_state {
90 REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */
91 REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */
92 REPLAY_DONE /* Replay was already queued */
93};
94
95struct ocfs2_replay_map {
96 unsigned int rm_slots;
97 enum ocfs2_replay_state rm_state;
98 unsigned char rm_replay_slots[0];
99};
100
101void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
102{
103 if (!osb->replay_map)
104 return;
105
106 /* If we've already queued the replay, we don't have any more to do */
107 if (osb->replay_map->rm_state == REPLAY_DONE)
108 return;
109
110 osb->replay_map->rm_state = state;
111}
112
113int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
114{
115 struct ocfs2_replay_map *replay_map;
116 int i, node_num;
117
118 /* If replay map is already set, we don't do it again */
119 if (osb->replay_map)
120 return 0;
121
122 replay_map = kzalloc(sizeof(struct ocfs2_replay_map) +
123 (osb->max_slots * sizeof(char)), GFP_KERNEL);
124
125 if (!replay_map) {
126 mlog_errno(-ENOMEM);
127 return -ENOMEM;
128 }
129
130 spin_lock(&osb->osb_lock);
131
132 replay_map->rm_slots = osb->max_slots;
133 replay_map->rm_state = REPLAY_UNNEEDED;
134
135 /* set rm_replay_slots for offline slot(s) */
136 for (i = 0; i < replay_map->rm_slots; i++) {
137 if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
138 replay_map->rm_replay_slots[i] = 1;
139 }
140
141 osb->replay_map = replay_map;
142 spin_unlock(&osb->osb_lock);
143 return 0;
144}
145
146void ocfs2_queue_replay_slots(struct ocfs2_super *osb)
147{
148 struct ocfs2_replay_map *replay_map = osb->replay_map;
149 int i;
150
151 if (!replay_map)
152 return;
153
154 if (replay_map->rm_state != REPLAY_NEEDED)
155 return;
156
157 for (i = 0; i < replay_map->rm_slots; i++)
158 if (replay_map->rm_replay_slots[i])
159 ocfs2_queue_recovery_completion(osb->journal, i, NULL,
160 NULL, NULL);
161 replay_map->rm_state = REPLAY_DONE;
162}
163
164void ocfs2_free_replay_slots(struct ocfs2_super *osb)
165{
166 struct ocfs2_replay_map *replay_map = osb->replay_map;
167
168 if (!osb->replay_map)
169 return;
170
171 kfree(replay_map);
172 osb->replay_map = NULL;
173}
174
Joel Becker553abd02008-02-01 12:03:57 -0800175int ocfs2_recovery_init(struct ocfs2_super *osb)
176{
177 struct ocfs2_recovery_map *rm;
178
179 mutex_init(&osb->recovery_lock);
180 osb->disable_recovery = 0;
181 osb->recovery_thread_task = NULL;
182 init_waitqueue_head(&osb->recovery_event);
183
184 rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
185 osb->max_slots * sizeof(unsigned int),
186 GFP_KERNEL);
187 if (!rm) {
188 mlog_errno(-ENOMEM);
189 return -ENOMEM;
190 }
191
192 rm->rm_entries = (unsigned int *)((char *)rm +
193 sizeof(struct ocfs2_recovery_map));
194 osb->recovery_map = rm;
195
196 return 0;
197}
198
199/* we can't grab the goofy sem lock from inside wait_event, so we use
200 * memory barriers to make sure that we'll see the null task before
201 * being woken up */
202static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
203{
204 mb();
205 return osb->recovery_thread_task != NULL;
206}
207
208void ocfs2_recovery_exit(struct ocfs2_super *osb)
209{
210 struct ocfs2_recovery_map *rm;
211
212 /* disable any new recovery threads and wait for any currently
213 * running ones to exit. Do this before setting the vol_state. */
214 mutex_lock(&osb->recovery_lock);
215 osb->disable_recovery = 1;
216 mutex_unlock(&osb->recovery_lock);
217 wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
218
219 /* At this point, we know that no more recovery threads can be
220 * launched, so wait for any recovery completion work to
221 * complete. */
222 flush_workqueue(ocfs2_wq);
223
224 /*
225 * Now that recovery is shut down, and the osb is about to be
226 * freed, the osb_lock is not taken here.
227 */
228 rm = osb->recovery_map;
229 /* XXX: Should we bug if there are dirty entries? */
230
231 kfree(rm);
232}
233
234static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
235 unsigned int node_num)
236{
237 int i;
238 struct ocfs2_recovery_map *rm = osb->recovery_map;
239
240 assert_spin_locked(&osb->osb_lock);
241
242 for (i = 0; i < rm->rm_used; i++) {
243 if (rm->rm_entries[i] == node_num)
244 return 1;
245 }
246
247 return 0;
248}
249
250/* Behaves like test-and-set. Returns the previous value */
251static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
252 unsigned int node_num)
253{
254 struct ocfs2_recovery_map *rm = osb->recovery_map;
255
256 spin_lock(&osb->osb_lock);
257 if (__ocfs2_recovery_map_test(osb, node_num)) {
258 spin_unlock(&osb->osb_lock);
259 return 1;
260 }
261
262 /* XXX: Can this be exploited? Not from o2dlm... */
263 BUG_ON(rm->rm_used >= osb->max_slots);
264
265 rm->rm_entries[rm->rm_used] = node_num;
266 rm->rm_used++;
267 spin_unlock(&osb->osb_lock);
268
269 return 0;
270}
271
272static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
273 unsigned int node_num)
274{
275 int i;
276 struct ocfs2_recovery_map *rm = osb->recovery_map;
277
278 spin_lock(&osb->osb_lock);
279
280 for (i = 0; i < rm->rm_used; i++) {
281 if (rm->rm_entries[i] == node_num)
282 break;
283 }
284
285 if (i < rm->rm_used) {
286 /* XXX: be careful with the pointer math */
287 memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
288 (rm->rm_used - i - 1) * sizeof(unsigned int));
289 rm->rm_used--;
290 }
291
292 spin_unlock(&osb->osb_lock);
293}
294
Mark Fashehccd979b2005-12-15 14:31:24 -0800295static int ocfs2_commit_cache(struct ocfs2_super *osb)
296{
297 int status = 0;
298 unsigned int flushed;
299 unsigned long old_id;
300 struct ocfs2_journal *journal = NULL;
301
302 mlog_entry_void();
303
304 journal = osb->journal;
305
306 /* Flush all pending commits and checkpoint the journal. */
307 down_write(&journal->j_trans_barrier);
308
309 if (atomic_read(&journal->j_num_trans) == 0) {
310 up_write(&journal->j_trans_barrier);
311 mlog(0, "No transactions for me to flush!\n");
312 goto finally;
313 }
314
Joel Becker2b4e30f2008-09-03 20:03:41 -0700315 jbd2_journal_lock_updates(journal->j_journal);
316 status = jbd2_journal_flush(journal->j_journal);
317 jbd2_journal_unlock_updates(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800318 if (status < 0) {
319 up_write(&journal->j_trans_barrier);
320 mlog_errno(status);
321 goto finally;
322 }
323
324 old_id = ocfs2_inc_trans_id(journal);
325
326 flushed = atomic_read(&journal->j_num_trans);
327 atomic_set(&journal->j_num_trans, 0);
328 up_write(&journal->j_trans_barrier);
329
330 mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
331 journal->j_trans_id, flushed);
332
Mark Fasheh34d024f2007-09-24 15:56:19 -0700333 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800334 wake_up(&journal->j_checkpointed);
335finally:
336 mlog_exit(status);
337 return status;
338}
339
Mark Fashehccd979b2005-12-15 14:31:24 -0800340/* pass it NULL and it will allocate a new handle object for you. If
341 * you pass it a handle however, it may still return error, in which
342 * case it has free'd the passed handle for you. */
Mark Fasheh1fabe142006-10-09 18:11:45 -0700343handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
Mark Fashehccd979b2005-12-15 14:31:24 -0800344{
Mark Fashehccd979b2005-12-15 14:31:24 -0800345 journal_t *journal = osb->journal->j_journal;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700346 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800347
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100348 BUG_ON(!osb || !osb->journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -0800349
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700350 if (ocfs2_is_hard_readonly(osb))
351 return ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800352
353 BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
354 BUG_ON(max_buffs <= 0);
355
Jan Kara90e86a62008-08-27 22:30:28 +0200356 /* Nested transaction? Just return the handle... */
357 if (journal_current_handle())
358 return jbd2_journal_start(journal, max_buffs);
Mark Fashehccd979b2005-12-15 14:31:24 -0800359
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 down_read(&osb->journal->j_trans_barrier);
361
Joel Becker2b4e30f2008-09-03 20:03:41 -0700362 handle = jbd2_journal_start(journal, max_buffs);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700363 if (IS_ERR(handle)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800364 up_read(&osb->journal->j_trans_barrier);
365
Mark Fasheh1fabe142006-10-09 18:11:45 -0700366 mlog_errno(PTR_ERR(handle));
Mark Fashehccd979b2005-12-15 14:31:24 -0800367
368 if (is_journal_aborted(journal)) {
369 ocfs2_abort(osb->sb, "Detected aborted journal");
Mark Fasheh1fabe142006-10-09 18:11:45 -0700370 handle = ERR_PTR(-EROFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800372 } else {
373 if (!ocfs2_mount_local(osb))
374 atomic_inc(&(osb->journal->j_num_trans));
375 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800376
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 return handle;
Mark Fashehccd979b2005-12-15 14:31:24 -0800378}
379
Mark Fasheh1fabe142006-10-09 18:11:45 -0700380int ocfs2_commit_trans(struct ocfs2_super *osb,
381 handle_t *handle)
Mark Fashehccd979b2005-12-15 14:31:24 -0800382{
Jan Kara90e86a62008-08-27 22:30:28 +0200383 int ret, nested;
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700384 struct ocfs2_journal *journal = osb->journal;
Mark Fashehccd979b2005-12-15 14:31:24 -0800385
386 BUG_ON(!handle);
387
Jan Kara90e86a62008-08-27 22:30:28 +0200388 nested = handle->h_ref > 1;
Joel Becker2b4e30f2008-09-03 20:03:41 -0700389 ret = jbd2_journal_stop(handle);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700390 if (ret < 0)
391 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -0800392
Jan Kara90e86a62008-08-27 22:30:28 +0200393 if (!nested)
394 up_read(&journal->j_trans_barrier);
Mark Fashehccd979b2005-12-15 14:31:24 -0800395
Mark Fasheh1fabe142006-10-09 18:11:45 -0700396 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -0800397}
398
399/*
400 * 'nblocks' is what you want to add to the current
401 * transaction. extend_trans will either extend the current handle by
402 * nblocks, or commit it and start a new one with nblocks credits.
403 *
Joel Becker2b4e30f2008-09-03 20:03:41 -0700404 * This might call jbd2_journal_restart() which will commit dirty buffers
Mark Fashehe8aed342007-12-03 16:43:01 -0800405 * and then restart the transaction. Before calling
406 * ocfs2_extend_trans(), any changed blocks should have been
407 * dirtied. After calling it, all blocks which need to be changed must
408 * go through another set of journal_access/journal_dirty calls.
409 *
Mark Fashehccd979b2005-12-15 14:31:24 -0800410 * WARNING: This will not release any semaphores or disk locks taken
411 * during the transaction, so make sure they were taken *before*
412 * start_trans or we'll have ordering deadlocks.
413 *
414 * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
415 * good because transaction ids haven't yet been recorded on the
416 * cluster locks associated with this handle.
417 */
Mark Fasheh1fc58142006-10-05 14:15:36 -0700418int ocfs2_extend_trans(handle_t *handle, int nblocks)
Mark Fashehccd979b2005-12-15 14:31:24 -0800419{
420 int status;
421
422 BUG_ON(!handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 BUG_ON(!nblocks);
424
425 mlog_entry_void();
426
427 mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
428
Joel Beckere407e392008-06-12 22:35:39 -0700429#ifdef CONFIG_OCFS2_DEBUG_FS
Mark Fasheh0879c582007-12-03 16:42:19 -0800430 status = 1;
431#else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700432 status = jbd2_journal_extend(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800433 if (status < 0) {
434 mlog_errno(status);
435 goto bail;
436 }
Mark Fasheh0879c582007-12-03 16:42:19 -0800437#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800438
439 if (status > 0) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700440 mlog(0,
441 "jbd2_journal_extend failed, trying "
442 "jbd2_journal_restart\n");
443 status = jbd2_journal_restart(handle, nblocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800444 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800445 mlog_errno(status);
446 goto bail;
447 }
Mark Fasheh01ddf1e2006-10-05 13:54:39 -0700448 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800449
450 status = 0;
451bail:
452
453 mlog_exit(status);
454 return status;
455}
456
Joel Becker50655ae2008-09-11 15:53:07 -0700457struct ocfs2_triggers {
458 struct jbd2_buffer_trigger_type ot_triggers;
459 int ot_offset;
460};
461
462static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
463{
464 return container_of(triggers, struct ocfs2_triggers, ot_triggers);
465}
466
467static void ocfs2_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
468 struct buffer_head *bh,
469 void *data, size_t size)
470{
471 struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
472
473 /*
474 * We aren't guaranteed to have the superblock here, so we
475 * must unconditionally compute the ecc data.
476 * __ocfs2_journal_access() will only set the triggers if
477 * metaecc is enabled.
478 */
479 ocfs2_block_check_compute(data, size, data + ot->ot_offset);
480}
481
482/*
483 * Quota blocks have their own trigger because the struct ocfs2_block_check
484 * offset depends on the blocksize.
485 */
486static void ocfs2_dq_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
487 struct buffer_head *bh,
488 void *data, size_t size)
489{
490 struct ocfs2_disk_dqtrailer *dqt =
491 ocfs2_block_dqtrailer(size, data);
492
493 /*
494 * We aren't guaranteed to have the superblock here, so we
495 * must unconditionally compute the ecc data.
496 * __ocfs2_journal_access() will only set the triggers if
497 * metaecc is enabled.
498 */
499 ocfs2_block_check_compute(data, size, &dqt->dq_check);
500}
501
Joel Beckerc175a512008-12-10 17:58:22 -0800502/*
503 * Directory blocks also have their own trigger because the
504 * struct ocfs2_block_check offset depends on the blocksize.
505 */
506static void ocfs2_db_commit_trigger(struct jbd2_buffer_trigger_type *triggers,
507 struct buffer_head *bh,
508 void *data, size_t size)
509{
510 struct ocfs2_dir_block_trailer *trailer =
511 ocfs2_dir_trailer_from_size(size, data);
512
513 /*
514 * We aren't guaranteed to have the superblock here, so we
515 * must unconditionally compute the ecc data.
516 * __ocfs2_journal_access() will only set the triggers if
517 * metaecc is enabled.
518 */
519 ocfs2_block_check_compute(data, size, &trailer->db_check);
520}
521
Joel Becker50655ae2008-09-11 15:53:07 -0700522static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
523 struct buffer_head *bh)
524{
525 mlog(ML_ERROR,
526 "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
527 "bh->b_blocknr = %llu\n",
528 (unsigned long)bh,
529 (unsigned long long)bh->b_blocknr);
530
531 /* We aren't guaranteed to have the superblock here - but if we
532 * don't, it'll just crash. */
533 ocfs2_error(bh->b_assoc_map->host->i_sb,
534 "JBD2 has aborted our journal, ocfs2 cannot continue\n");
535}
536
537static struct ocfs2_triggers di_triggers = {
538 .ot_triggers = {
539 .t_commit = ocfs2_commit_trigger,
540 .t_abort = ocfs2_abort_trigger,
541 },
542 .ot_offset = offsetof(struct ocfs2_dinode, i_check),
543};
544
545static struct ocfs2_triggers eb_triggers = {
546 .ot_triggers = {
547 .t_commit = ocfs2_commit_trigger,
548 .t_abort = ocfs2_abort_trigger,
549 },
550 .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
551};
552
553static struct ocfs2_triggers gd_triggers = {
554 .ot_triggers = {
555 .t_commit = ocfs2_commit_trigger,
556 .t_abort = ocfs2_abort_trigger,
557 },
558 .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
559};
560
Joel Beckerc175a512008-12-10 17:58:22 -0800561static struct ocfs2_triggers db_triggers = {
562 .ot_triggers = {
563 .t_commit = ocfs2_db_commit_trigger,
564 .t_abort = ocfs2_abort_trigger,
565 },
566};
567
Joel Becker50655ae2008-09-11 15:53:07 -0700568static struct ocfs2_triggers xb_triggers = {
569 .ot_triggers = {
570 .t_commit = ocfs2_commit_trigger,
571 .t_abort = ocfs2_abort_trigger,
572 },
573 .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
574};
575
576static struct ocfs2_triggers dq_triggers = {
577 .ot_triggers = {
578 .t_commit = ocfs2_dq_commit_trigger,
579 .t_abort = ocfs2_abort_trigger,
580 },
581};
582
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800583static struct ocfs2_triggers dr_triggers = {
584 .ot_triggers = {
585 .t_commit = ocfs2_commit_trigger,
586 .t_abort = ocfs2_abort_trigger,
587 },
588 .ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check),
589};
590
591static struct ocfs2_triggers dl_triggers = {
592 .ot_triggers = {
593 .t_commit = ocfs2_commit_trigger,
594 .t_abort = ocfs2_abort_trigger,
595 },
596 .ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check),
597};
598
Joel Becker50655ae2008-09-11 15:53:07 -0700599static int __ocfs2_journal_access(handle_t *handle,
600 struct inode *inode,
601 struct buffer_head *bh,
602 struct ocfs2_triggers *triggers,
603 int type)
Mark Fashehccd979b2005-12-15 14:31:24 -0800604{
605 int status;
606
607 BUG_ON(!inode);
608 BUG_ON(!handle);
609 BUG_ON(!bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800610
Badari Pulavarty205f87f2006-03-26 01:38:00 -0800611 mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
Mark Fashehccd979b2005-12-15 14:31:24 -0800612 (unsigned long long)bh->b_blocknr, type,
613 (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
614 "OCFS2_JOURNAL_ACCESS_CREATE" :
615 "OCFS2_JOURNAL_ACCESS_WRITE",
616 bh->b_size);
617
618 /* we can safely remove this assertion after testing. */
619 if (!buffer_uptodate(bh)) {
620 mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
621 mlog(ML_ERROR, "b_blocknr=%llu\n",
622 (unsigned long long)bh->b_blocknr);
623 BUG();
624 }
625
626 /* Set the current transaction information on the inode so
627 * that the locking code knows whether it can drop it's locks
628 * on this inode or not. We're protected from the commit
629 * thread updating the current transaction id until
630 * ocfs2_commit_trans() because ocfs2_start_trans() took
631 * j_trans_barrier for us. */
632 ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
633
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800634 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800635 switch (type) {
636 case OCFS2_JOURNAL_ACCESS_CREATE:
637 case OCFS2_JOURNAL_ACCESS_WRITE:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700638 status = jbd2_journal_get_write_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 break;
640
641 case OCFS2_JOURNAL_ACCESS_UNDO:
Joel Becker2b4e30f2008-09-03 20:03:41 -0700642 status = jbd2_journal_get_undo_access(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800643 break;
644
645 default:
646 status = -EINVAL;
647 mlog(ML_ERROR, "Uknown access type!\n");
648 }
Joel Becker50655ae2008-09-11 15:53:07 -0700649 if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
650 jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
Mark Fasheh251b6ec2006-01-10 15:41:43 -0800651 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -0800652
653 if (status < 0)
654 mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
655 status, type);
656
657 mlog_exit(status);
658 return status;
659}
660
Joel Becker50655ae2008-09-11 15:53:07 -0700661int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
662 struct buffer_head *bh, int type)
663{
664 return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
665 type);
666}
667
668int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
669 struct buffer_head *bh, int type)
670{
671 return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
672 type);
673}
674
675int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
676 struct buffer_head *bh, int type)
677{
678 return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
679 type);
680}
681
682int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
683 struct buffer_head *bh, int type)
684{
Joel Beckerc175a512008-12-10 17:58:22 -0800685 return __ocfs2_journal_access(handle, inode, bh, &db_triggers,
686 type);
Joel Becker50655ae2008-09-11 15:53:07 -0700687}
688
689int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
690 struct buffer_head *bh, int type)
691{
692 return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
693 type);
694}
695
696int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
697 struct buffer_head *bh, int type)
698{
699 return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
700 type);
701}
702
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800703int ocfs2_journal_access_dr(handle_t *handle, struct inode *inode,
704 struct buffer_head *bh, int type)
705{
706 return __ocfs2_journal_access(handle, inode, bh, &dr_triggers,
707 type);
708}
709
710int ocfs2_journal_access_dl(handle_t *handle, struct inode *inode,
711 struct buffer_head *bh, int type)
712{
713 return __ocfs2_journal_access(handle, inode, bh, &dl_triggers,
714 type);
715}
716
Joel Becker50655ae2008-09-11 15:53:07 -0700717int ocfs2_journal_access(handle_t *handle, struct inode *inode,
718 struct buffer_head *bh, int type)
719{
720 return __ocfs2_journal_access(handle, inode, bh, NULL, type);
721}
722
Mark Fasheh1fabe142006-10-09 18:11:45 -0700723int ocfs2_journal_dirty(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800724 struct buffer_head *bh)
725{
726 int status;
727
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 mlog_entry("(bh->b_blocknr=%llu)\n",
729 (unsigned long long)bh->b_blocknr);
730
Joel Becker2b4e30f2008-09-03 20:03:41 -0700731 status = jbd2_journal_dirty_metadata(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800732 if (status < 0)
733 mlog(ML_ERROR, "Could not dirty metadata buffer. "
734 "(bh->b_blocknr=%llu)\n",
735 (unsigned long long)bh->b_blocknr);
736
737 mlog_exit(status);
738 return status;
739}
740
Joel Becker2b4e30f2008-09-03 20:03:41 -0700741#define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800742
743void ocfs2_set_journal_params(struct ocfs2_super *osb)
744{
745 journal_t *journal = osb->journal->j_journal;
Mark Fashehd147b3d2007-11-07 14:40:36 -0800746 unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
747
748 if (osb->osb_commit_interval)
749 commit_interval = osb->osb_commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800750
751 spin_lock(&journal->j_state_lock);
Mark Fashehd147b3d2007-11-07 14:40:36 -0800752 journal->j_commit_interval = commit_interval;
Mark Fashehccd979b2005-12-15 14:31:24 -0800753 if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
Joel Becker2b4e30f2008-09-03 20:03:41 -0700754 journal->j_flags |= JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800755 else
Joel Becker2b4e30f2008-09-03 20:03:41 -0700756 journal->j_flags &= ~JBD2_BARRIER;
Mark Fashehccd979b2005-12-15 14:31:24 -0800757 spin_unlock(&journal->j_state_lock);
758}
759
760int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
761{
762 int status = -1;
763 struct inode *inode = NULL; /* the journal inode */
764 journal_t *j_journal = NULL;
765 struct ocfs2_dinode *di = NULL;
766 struct buffer_head *bh = NULL;
767 struct ocfs2_super *osb;
Mark Fashehe63aecb62007-10-18 15:30:42 -0700768 int inode_lock = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800769
770 mlog_entry_void();
771
772 BUG_ON(!journal);
773
774 osb = journal->j_osb;
775
776 /* already have the inode for our journal */
777 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
778 osb->slot_num);
779 if (inode == NULL) {
780 status = -EACCES;
781 mlog_errno(status);
782 goto done;
783 }
784 if (is_bad_inode(inode)) {
785 mlog(ML_ERROR, "access error (bad inode)\n");
786 iput(inode);
787 inode = NULL;
788 status = -EACCES;
789 goto done;
790 }
791
792 SET_INODE_JOURNAL(inode);
793 OCFS2_I(inode)->ip_open_count++;
794
Mark Fasheh6eff5792006-01-18 10:31:47 -0800795 /* Skip recovery waits here - journal inode metadata never
796 * changes in a live cluster so it can be considered an
797 * exception to the rule. */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700798 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800799 if (status < 0) {
800 if (status != -ERESTARTSYS)
801 mlog(ML_ERROR, "Could not get lock on journal!\n");
802 goto done;
803 }
804
Mark Fashehe63aecb62007-10-18 15:30:42 -0700805 inode_lock = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 di = (struct ocfs2_dinode *)bh->b_data;
807
808 if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
809 mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
810 inode->i_size);
811 status = -EINVAL;
812 goto done;
813 }
814
815 mlog(0, "inode->i_size = %lld\n", inode->i_size);
Andrew Morton5515eff2006-03-26 01:37:53 -0800816 mlog(0, "inode->i_blocks = %llu\n",
817 (unsigned long long)inode->i_blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -0800818 mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
819
820 /* call the kernels journal init function now */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700821 j_journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800822 if (j_journal == NULL) {
823 mlog(ML_ERROR, "Linux journal layer error\n");
824 status = -EINVAL;
825 goto done;
826 }
827
Joel Becker2b4e30f2008-09-03 20:03:41 -0700828 mlog(0, "Returned from jbd2_journal_init_inode\n");
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
830
831 *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
832 OCFS2_JOURNAL_DIRTY_FL);
833
834 journal->j_journal = j_journal;
835 journal->j_inode = inode;
836 journal->j_bh = bh;
837
838 ocfs2_set_journal_params(osb);
839
840 journal->j_state = OCFS2_JOURNAL_LOADED;
841
842 status = 0;
843done:
844 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -0700845 if (inode_lock)
846 ocfs2_inode_unlock(inode, 1);
Mark Fasheha81cb882008-10-07 14:25:16 -0700847 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800848 if (inode) {
849 OCFS2_I(inode)->ip_open_count--;
850 iput(inode);
851 }
852 }
853
854 mlog_exit(status);
855 return status;
856}
857
Sunil Mushran539d8262008-07-14 17:31:10 -0700858static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
859{
860 le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
861}
862
863static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
864{
865 return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
866}
867
Mark Fashehccd979b2005-12-15 14:31:24 -0800868static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
Sunil Mushran539d8262008-07-14 17:31:10 -0700869 int dirty, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -0800870{
871 int status;
872 unsigned int flags;
873 struct ocfs2_journal *journal = osb->journal;
874 struct buffer_head *bh = journal->j_bh;
875 struct ocfs2_dinode *fe;
876
877 mlog_entry_void();
878
879 fe = (struct ocfs2_dinode *)bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800880
881 /* The journal bh on the osb always comes from ocfs2_journal_init()
882 * and was validated there inside ocfs2_inode_lock_full(). It's a
883 * code bug if we mess it up. */
884 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -0800885
886 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
887 if (dirty)
888 flags |= OCFS2_JOURNAL_DIRTY_FL;
889 else
890 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
891 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
892
Sunil Mushran539d8262008-07-14 17:31:10 -0700893 if (replayed)
894 ocfs2_bump_recovery_generation(fe);
895
Joel Becker13723d02008-10-17 19:25:01 -0700896 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -0800897 status = ocfs2_write_block(osb, bh, journal->j_inode);
898 if (status < 0)
899 mlog_errno(status);
900
Mark Fashehccd979b2005-12-15 14:31:24 -0800901 mlog_exit(status);
902 return status;
903}
904
905/*
906 * If the journal has been kmalloc'd it needs to be freed after this
907 * call.
908 */
909void ocfs2_journal_shutdown(struct ocfs2_super *osb)
910{
911 struct ocfs2_journal *journal = NULL;
912 int status = 0;
913 struct inode *inode = NULL;
914 int num_running_trans = 0;
915
916 mlog_entry_void();
917
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +0100918 BUG_ON(!osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800919
920 journal = osb->journal;
921 if (!journal)
922 goto done;
923
924 inode = journal->j_inode;
925
926 if (journal->j_state != OCFS2_JOURNAL_LOADED)
927 goto done;
928
Joel Becker2b4e30f2008-09-03 20:03:41 -0700929 /* need to inc inode use count - jbd2_journal_destroy will iput. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800930 if (!igrab(inode))
931 BUG();
932
933 num_running_trans = atomic_read(&(osb->journal->j_num_trans));
934 if (num_running_trans > 0)
935 mlog(0, "Shutting down journal: must wait on %d "
936 "running transactions!\n",
937 num_running_trans);
938
939 /* Do a commit_cache here. It will flush our journal, *and*
940 * release any locks that are still held.
941 * set the SHUTDOWN flag and release the trans lock.
942 * the commit thread will take the trans lock for us below. */
943 journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
944
945 /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
946 * drop the trans_lock (which we want to hold until we
947 * completely destroy the journal. */
948 if (osb->commit_task) {
949 /* Wait for the commit thread */
950 mlog(0, "Waiting for ocfs2commit to exit....\n");
951 kthread_stop(osb->commit_task);
952 osb->commit_task = NULL;
953 }
954
955 BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
956
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800957 if (ocfs2_mount_local(osb)) {
Joel Becker2b4e30f2008-09-03 20:03:41 -0700958 jbd2_journal_lock_updates(journal->j_journal);
959 status = jbd2_journal_flush(journal->j_journal);
960 jbd2_journal_unlock_updates(journal->j_journal);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800961 if (status < 0)
962 mlog_errno(status);
963 }
964
965 if (status == 0) {
966 /*
967 * Do not toggle if flush was unsuccessful otherwise
968 * will leave dirty metadata in a "clean" journal
969 */
Sunil Mushran539d8262008-07-14 17:31:10 -0700970 status = ocfs2_journal_toggle_dirty(osb, 0, 0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800971 if (status < 0)
972 mlog_errno(status);
973 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800974
975 /* Shutdown the kernel journal system */
Joel Becker2b4e30f2008-09-03 20:03:41 -0700976 jbd2_journal_destroy(journal->j_journal);
Sunil Mushranae0dff62008-10-22 13:24:29 -0700977 journal->j_journal = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800978
979 OCFS2_I(inode)->ip_open_count--;
980
981 /* unlock our journal */
Mark Fashehe63aecb62007-10-18 15:30:42 -0700982 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800983
984 brelse(journal->j_bh);
985 journal->j_bh = NULL;
986
987 journal->j_state = OCFS2_JOURNAL_FREE;
988
989// up_write(&journal->j_trans_barrier);
990done:
991 if (inode)
992 iput(inode);
993 mlog_exit_void();
994}
995
996static void ocfs2_clear_journal_error(struct super_block *sb,
997 journal_t *journal,
998 int slot)
999{
1000 int olderr;
1001
Joel Becker2b4e30f2008-09-03 20:03:41 -07001002 olderr = jbd2_journal_errno(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001003 if (olderr) {
1004 mlog(ML_ERROR, "File system error %d recorded in "
1005 "journal %u.\n", olderr, slot);
1006 mlog(ML_ERROR, "File system on device %s needs checking.\n",
1007 sb->s_id);
1008
Joel Becker2b4e30f2008-09-03 20:03:41 -07001009 jbd2_journal_ack_err(journal);
1010 jbd2_journal_clear_err(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 }
1012}
1013
Sunil Mushran539d8262008-07-14 17:31:10 -07001014int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
Mark Fashehccd979b2005-12-15 14:31:24 -08001015{
1016 int status = 0;
1017 struct ocfs2_super *osb;
1018
1019 mlog_entry_void();
1020
Julia Lawallb1f35502008-03-04 15:21:05 -08001021 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001022
1023 osb = journal->j_osb;
1024
Joel Becker2b4e30f2008-09-03 20:03:41 -07001025 status = jbd2_journal_load(journal->j_journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 if (status < 0) {
1027 mlog(ML_ERROR, "Failed to load journal!\n");
1028 goto done;
1029 }
1030
1031 ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
1032
Sunil Mushran539d8262008-07-14 17:31:10 -07001033 status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
Mark Fashehccd979b2005-12-15 14:31:24 -08001034 if (status < 0) {
1035 mlog_errno(status);
1036 goto done;
1037 }
1038
1039 /* Launch the commit thread */
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001040 if (!local) {
1041 osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
1042 "ocfs2cmt");
1043 if (IS_ERR(osb->commit_task)) {
1044 status = PTR_ERR(osb->commit_task);
1045 osb->commit_task = NULL;
1046 mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
1047 "error=%d", status);
1048 goto done;
1049 }
1050 } else
Mark Fashehccd979b2005-12-15 14:31:24 -08001051 osb->commit_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001052
1053done:
1054 mlog_exit(status);
1055 return status;
1056}
1057
1058
1059/* 'full' flag tells us whether we clear out all blocks or if we just
1060 * mark the journal clean */
1061int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
1062{
1063 int status;
1064
1065 mlog_entry_void();
1066
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001067 BUG_ON(!journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001068
Joel Becker2b4e30f2008-09-03 20:03:41 -07001069 status = jbd2_journal_wipe(journal->j_journal, full);
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 if (status < 0) {
1071 mlog_errno(status);
1072 goto bail;
1073 }
1074
Sunil Mushran539d8262008-07-14 17:31:10 -07001075 status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001076 if (status < 0)
1077 mlog_errno(status);
1078
1079bail:
1080 mlog_exit(status);
1081 return status;
1082}
1083
Joel Becker553abd02008-02-01 12:03:57 -08001084static int ocfs2_recovery_completed(struct ocfs2_super *osb)
1085{
1086 int empty;
1087 struct ocfs2_recovery_map *rm = osb->recovery_map;
1088
1089 spin_lock(&osb->osb_lock);
1090 empty = (rm->rm_used == 0);
1091 spin_unlock(&osb->osb_lock);
1092
1093 return empty;
1094}
1095
1096void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
1097{
1098 wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
1099}
1100
Mark Fashehccd979b2005-12-15 14:31:24 -08001101/*
1102 * JBD Might read a cached version of another nodes journal file. We
1103 * don't want this as this file changes often and we get no
1104 * notification on those changes. The only way to be sure that we've
1105 * got the most up to date version of those blocks then is to force
1106 * read them off disk. Just searching through the buffer cache won't
1107 * work as there may be pages backing this file which are still marked
1108 * up to date. We know things can't change on this file underneath us
1109 * as we have the lock by now :)
1110 */
1111static int ocfs2_force_read_journal(struct inode *inode)
1112{
1113 int status = 0;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001114 int i;
Mark Fasheh8110b072007-03-22 16:53:23 -07001115 u64 v_blkno, p_blkno, p_blocks, num_blocks;
Mark Fasheh4f902c32007-03-09 16:26:50 -08001116#define CONCURRENT_JOURNAL_FILL 32ULL
Mark Fashehccd979b2005-12-15 14:31:24 -08001117 struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
1118
1119 mlog_entry_void();
1120
Mark Fashehccd979b2005-12-15 14:31:24 -08001121 memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
1122
Mark Fasheh8110b072007-03-22 16:53:23 -07001123 num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001124 v_blkno = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07001125 while (v_blkno < num_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001126 status = ocfs2_extent_map_get_blocks(inode, v_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001127 &p_blkno, &p_blocks, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001128 if (status < 0) {
1129 mlog_errno(status);
1130 goto bail;
1131 }
1132
1133 if (p_blocks > CONCURRENT_JOURNAL_FILL)
1134 p_blocks = CONCURRENT_JOURNAL_FILL;
1135
Mark Fashehdd4a2c22006-04-12 14:24:05 -07001136 /* We are reading journal data which should not
1137 * be put in the uptodate cache */
Joel Beckerda1e9092008-10-09 17:20:29 -07001138 status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
1139 p_blkno, p_blocks, bhs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001140 if (status < 0) {
1141 mlog_errno(status);
1142 goto bail;
1143 }
1144
1145 for(i = 0; i < p_blocks; i++) {
1146 brelse(bhs[i]);
1147 bhs[i] = NULL;
1148 }
1149
1150 v_blkno += p_blocks;
1151 }
1152
1153bail:
1154 for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001155 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 mlog_exit(status);
1157 return status;
1158}
1159
1160struct ocfs2_la_recovery_item {
1161 struct list_head lri_list;
1162 int lri_slot;
1163 struct ocfs2_dinode *lri_la_dinode;
1164 struct ocfs2_dinode *lri_tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001165 struct ocfs2_quota_recovery *lri_qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001166};
1167
1168/* Does the second half of the recovery process. By this point, the
1169 * node is marked clean and can actually be considered recovered,
1170 * hence it's no longer in the recovery map, but there's still some
1171 * cleanup we can do which shouldn't happen within the recovery thread
1172 * as locking in that context becomes very difficult if we are to take
1173 * recovering nodes into account.
1174 *
1175 * NOTE: This function can and will sleep on recovery of other nodes
1176 * during cluster locking, just like any other ocfs2 process.
1177 */
David Howellsc4028952006-11-22 14:57:56 +00001178void ocfs2_complete_recovery(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08001179{
1180 int ret;
David Howellsc4028952006-11-22 14:57:56 +00001181 struct ocfs2_journal *journal =
1182 container_of(work, struct ocfs2_journal, j_recovery_work);
1183 struct ocfs2_super *osb = journal->j_osb;
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 struct ocfs2_dinode *la_dinode, *tl_dinode;
Christoph Hellwig800deef2007-05-17 16:03:13 +02001185 struct ocfs2_la_recovery_item *item, *n;
Jan Kara22053632008-10-20 23:50:38 +02001186 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 LIST_HEAD(tmp_la_list);
1188
1189 mlog_entry_void();
1190
1191 mlog(0, "completing recovery from keventd\n");
1192
1193 spin_lock(&journal->j_lock);
1194 list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
1195 spin_unlock(&journal->j_lock);
1196
Christoph Hellwig800deef2007-05-17 16:03:13 +02001197 list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 list_del_init(&item->lri_list);
1199
1200 mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
1201
Jan Kara19ece542008-08-21 20:13:17 +02001202 ocfs2_wait_on_quotas(osb);
1203
Mark Fashehccd979b2005-12-15 14:31:24 -08001204 la_dinode = item->lri_la_dinode;
1205 if (la_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001206 mlog(0, "Clean up local alloc %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001207 (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001208
1209 ret = ocfs2_complete_local_alloc_recovery(osb,
1210 la_dinode);
1211 if (ret < 0)
1212 mlog_errno(ret);
1213
1214 kfree(la_dinode);
1215 }
1216
1217 tl_dinode = item->lri_tl_dinode;
1218 if (tl_dinode) {
Mark Fashehb06970532006-03-03 10:24:33 -08001219 mlog(0, "Clean up truncate log %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001220 (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001221
1222 ret = ocfs2_complete_truncate_log_recovery(osb,
1223 tl_dinode);
1224 if (ret < 0)
1225 mlog_errno(ret);
1226
1227 kfree(tl_dinode);
1228 }
1229
1230 ret = ocfs2_recover_orphans(osb, item->lri_slot);
1231 if (ret < 0)
1232 mlog_errno(ret);
1233
Jan Kara22053632008-10-20 23:50:38 +02001234 qrec = item->lri_qrec;
1235 if (qrec) {
1236 mlog(0, "Recovering quota files");
1237 ret = ocfs2_finish_quota_recovery(osb, qrec,
1238 item->lri_slot);
1239 if (ret < 0)
1240 mlog_errno(ret);
1241 /* Recovery info is already freed now */
1242 }
1243
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 kfree(item);
1245 }
1246
1247 mlog(0, "Recovery completion\n");
1248 mlog_exit_void();
1249}
1250
1251/* NOTE: This function always eats your references to la_dinode and
1252 * tl_dinode, either manually on error, or by passing them to
1253 * ocfs2_complete_recovery */
1254static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
1255 int slot_num,
1256 struct ocfs2_dinode *la_dinode,
Jan Kara22053632008-10-20 23:50:38 +02001257 struct ocfs2_dinode *tl_dinode,
1258 struct ocfs2_quota_recovery *qrec)
Mark Fashehccd979b2005-12-15 14:31:24 -08001259{
1260 struct ocfs2_la_recovery_item *item;
1261
Sunil Mushranafae00ab2006-04-12 14:37:00 -07001262 item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 if (!item) {
1264 /* Though we wish to avoid it, we are in fact safe in
1265 * skipping local alloc cleanup as fsck.ocfs2 is more
1266 * than capable of reclaiming unused space. */
1267 if (la_dinode)
1268 kfree(la_dinode);
1269
1270 if (tl_dinode)
1271 kfree(tl_dinode);
1272
Jan Kara22053632008-10-20 23:50:38 +02001273 if (qrec)
1274 ocfs2_free_quota_recovery(qrec);
1275
Mark Fashehccd979b2005-12-15 14:31:24 -08001276 mlog_errno(-ENOMEM);
1277 return;
1278 }
1279
1280 INIT_LIST_HEAD(&item->lri_list);
1281 item->lri_la_dinode = la_dinode;
1282 item->lri_slot = slot_num;
1283 item->lri_tl_dinode = tl_dinode;
Jan Kara22053632008-10-20 23:50:38 +02001284 item->lri_qrec = qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001285
1286 spin_lock(&journal->j_lock);
1287 list_add_tail(&item->lri_list, &journal->j_la_cleanups);
1288 queue_work(ocfs2_wq, &journal->j_recovery_work);
1289 spin_unlock(&journal->j_lock);
1290}
1291
1292/* Called by the mount code to queue recovery the last part of
Srinivas Eeda9140db02009-03-06 14:21:46 -08001293 * recovery for it's own and offline slot(s). */
Mark Fashehccd979b2005-12-15 14:31:24 -08001294void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
1295{
1296 struct ocfs2_journal *journal = osb->journal;
1297
Srinivas Eeda9140db02009-03-06 14:21:46 -08001298 /* No need to queue up our truncate_log as regular cleanup will catch
1299 * that */
1300 ocfs2_queue_recovery_completion(journal, osb->slot_num,
1301 osb->local_alloc_copy, NULL, NULL);
1302 ocfs2_schedule_truncate_log_flush(osb, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001303
Srinivas Eeda9140db02009-03-06 14:21:46 -08001304 osb->local_alloc_copy = NULL;
1305 osb->dirty = 0;
1306
1307 /* queue to recover orphan slots for all offline slots */
1308 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1309 ocfs2_queue_replay_slots(osb);
1310 ocfs2_free_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001311}
1312
Jan Kara22053632008-10-20 23:50:38 +02001313void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
1314{
1315 if (osb->quota_rec) {
1316 ocfs2_queue_recovery_completion(osb->journal,
1317 osb->slot_num,
1318 NULL,
1319 NULL,
1320 osb->quota_rec);
1321 osb->quota_rec = NULL;
1322 }
1323}
1324
Mark Fashehccd979b2005-12-15 14:31:24 -08001325static int __ocfs2_recovery_thread(void *arg)
1326{
Jan Kara22053632008-10-20 23:50:38 +02001327 int status, node_num, slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 struct ocfs2_super *osb = arg;
Joel Becker553abd02008-02-01 12:03:57 -08001329 struct ocfs2_recovery_map *rm = osb->recovery_map;
Jan Kara22053632008-10-20 23:50:38 +02001330 int *rm_quota = NULL;
1331 int rm_quota_used = 0, i;
1332 struct ocfs2_quota_recovery *qrec;
Mark Fashehccd979b2005-12-15 14:31:24 -08001333
1334 mlog_entry_void();
1335
1336 status = ocfs2_wait_on_mount(osb);
1337 if (status < 0) {
1338 goto bail;
1339 }
1340
Jan Kara22053632008-10-20 23:50:38 +02001341 rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
1342 if (!rm_quota) {
1343 status = -ENOMEM;
1344 goto bail;
1345 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001346restart:
1347 status = ocfs2_super_lock(osb, 1);
1348 if (status < 0) {
1349 mlog_errno(status);
1350 goto bail;
1351 }
1352
Srinivas Eeda9140db02009-03-06 14:21:46 -08001353 status = ocfs2_compute_replay_slots(osb);
1354 if (status < 0)
1355 mlog_errno(status);
1356
1357 /* queue recovery for our own slot */
1358 ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
1359 NULL, NULL);
1360
Joel Becker553abd02008-02-01 12:03:57 -08001361 spin_lock(&osb->osb_lock);
1362 while (rm->rm_used) {
1363 /* It's always safe to remove entry zero, as we won't
1364 * clear it until ocfs2_recover_node() has succeeded. */
1365 node_num = rm->rm_entries[0];
1366 spin_unlock(&osb->osb_lock);
Jan Kara22053632008-10-20 23:50:38 +02001367 mlog(0, "checking node %d\n", node_num);
1368 slot_num = ocfs2_node_num_to_slot(osb, node_num);
1369 if (slot_num == -ENOENT) {
1370 status = 0;
1371 mlog(0, "no slot for this node, so no recovery"
1372 "required.\n");
1373 goto skip_recovery;
1374 }
1375 mlog(0, "node %d was using slot %d\n", node_num, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001376
Jan Kara22053632008-10-20 23:50:38 +02001377 /* It is a bit subtle with quota recovery. We cannot do it
1378 * immediately because we have to obtain cluster locks from
1379 * quota files and we also don't want to just skip it because
1380 * then quota usage would be out of sync until some node takes
1381 * the slot. So we remember which nodes need quota recovery
1382 * and when everything else is done, we recover quotas. */
1383 for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
1384 if (i == rm_quota_used)
1385 rm_quota[rm_quota_used++] = slot_num;
1386
1387 status = ocfs2_recover_node(osb, node_num, slot_num);
1388skip_recovery:
Joel Becker553abd02008-02-01 12:03:57 -08001389 if (!status) {
1390 ocfs2_recovery_map_clear(osb, node_num);
1391 } else {
Mark Fashehccd979b2005-12-15 14:31:24 -08001392 mlog(ML_ERROR,
1393 "Error %d recovering node %d on device (%u,%u)!\n",
1394 status, node_num,
1395 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1396 mlog(ML_ERROR, "Volume requires unmount.\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08001397 }
1398
Joel Becker553abd02008-02-01 12:03:57 -08001399 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001400 }
Joel Becker553abd02008-02-01 12:03:57 -08001401 spin_unlock(&osb->osb_lock);
1402 mlog(0, "All nodes recovered\n");
1403
Sunil Mushran539d8262008-07-14 17:31:10 -07001404 /* Refresh all journal recovery generations from disk */
1405 status = ocfs2_check_journals_nolocks(osb);
1406 status = (status == -EROFS) ? 0 : status;
1407 if (status < 0)
1408 mlog_errno(status);
1409
Jan Kara22053632008-10-20 23:50:38 +02001410 /* Now it is right time to recover quotas... We have to do this under
1411 * superblock lock so that noone can start using the slot (and crash)
1412 * before we recover it */
1413 for (i = 0; i < rm_quota_used; i++) {
1414 qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
1415 if (IS_ERR(qrec)) {
1416 status = PTR_ERR(qrec);
1417 mlog_errno(status);
1418 continue;
1419 }
1420 ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
1421 NULL, NULL, qrec);
1422 }
1423
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 ocfs2_super_unlock(osb, 1);
1425
Srinivas Eeda9140db02009-03-06 14:21:46 -08001426 /* queue recovery for offline slots */
1427 ocfs2_queue_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001428
1429bail:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001430 mutex_lock(&osb->recovery_lock);
Joel Becker553abd02008-02-01 12:03:57 -08001431 if (!status && !ocfs2_recovery_completed(osb)) {
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001432 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 goto restart;
1434 }
1435
Srinivas Eeda9140db02009-03-06 14:21:46 -08001436 ocfs2_free_replay_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001437 osb->recovery_thread_task = NULL;
1438 mb(); /* sync with ocfs2_recovery_thread_running */
1439 wake_up(&osb->recovery_event);
1440
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001441 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442
Jan Kara22053632008-10-20 23:50:38 +02001443 if (rm_quota)
1444 kfree(rm_quota);
1445
Mark Fashehccd979b2005-12-15 14:31:24 -08001446 mlog_exit(status);
1447 /* no one is callint kthread_stop() for us so the kthread() api
1448 * requires that we call do_exit(). And it isn't exported, but
1449 * complete_and_exit() seems to be a minimal wrapper around it. */
1450 complete_and_exit(NULL, status);
1451 return status;
1452}
1453
1454void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
1455{
1456 mlog_entry("(node_num=%d, osb->node_num = %d)\n",
1457 node_num, osb->node_num);
1458
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001459 mutex_lock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 if (osb->disable_recovery)
1461 goto out;
1462
1463 /* People waiting on recovery will wait on
1464 * the recovery map to empty. */
Joel Becker553abd02008-02-01 12:03:57 -08001465 if (ocfs2_recovery_map_set(osb, node_num))
1466 mlog(0, "node %d already in recovery map.\n", node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001467
1468 mlog(0, "starting recovery thread...\n");
1469
1470 if (osb->recovery_thread_task)
1471 goto out;
1472
1473 osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
Mark Fasheh78427042006-05-04 12:03:26 -07001474 "ocfs2rec");
Mark Fashehccd979b2005-12-15 14:31:24 -08001475 if (IS_ERR(osb->recovery_thread_task)) {
1476 mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
1477 osb->recovery_thread_task = NULL;
1478 }
1479
1480out:
Arjan van de Venc74ec2f2006-01-13 21:54:23 -08001481 mutex_unlock(&osb->recovery_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001482 wake_up(&osb->recovery_event);
1483
1484 mlog_exit_void();
1485}
1486
Sunil Mushran539d8262008-07-14 17:31:10 -07001487static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
1488 int slot_num,
1489 struct buffer_head **bh,
1490 struct inode **ret_inode)
1491{
1492 int status = -EACCES;
1493 struct inode *inode = NULL;
1494
1495 BUG_ON(slot_num >= osb->max_slots);
1496
1497 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1498 slot_num);
1499 if (!inode || is_bad_inode(inode)) {
1500 mlog_errno(status);
1501 goto bail;
1502 }
1503 SET_INODE_JOURNAL(inode);
1504
Joel Beckerb657c952008-11-13 14:49:11 -08001505 status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
Sunil Mushran539d8262008-07-14 17:31:10 -07001506 if (status < 0) {
1507 mlog_errno(status);
1508 goto bail;
1509 }
1510
1511 status = 0;
1512
1513bail:
1514 if (inode) {
1515 if (status || !ret_inode)
1516 iput(inode);
1517 else
1518 *ret_inode = inode;
1519 }
1520 return status;
1521}
1522
Mark Fashehccd979b2005-12-15 14:31:24 -08001523/* Does the actual journal replay and marks the journal inode as
1524 * clean. Will only replay if the journal inode is marked dirty. */
1525static int ocfs2_replay_journal(struct ocfs2_super *osb,
1526 int node_num,
1527 int slot_num)
1528{
1529 int status;
1530 int got_lock = 0;
1531 unsigned int flags;
1532 struct inode *inode = NULL;
1533 struct ocfs2_dinode *fe;
1534 journal_t *journal = NULL;
1535 struct buffer_head *bh = NULL;
Sunil Mushran539d8262008-07-14 17:31:10 -07001536 u32 slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001537
Sunil Mushran539d8262008-07-14 17:31:10 -07001538 status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
1539 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001540 mlog_errno(status);
1541 goto done;
1542 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001543
1544 fe = (struct ocfs2_dinode *)bh->b_data;
1545 slot_reco_gen = ocfs2_get_recovery_generation(fe);
1546 brelse(bh);
1547 bh = NULL;
1548
1549 /*
1550 * As the fs recovery is asynchronous, there is a small chance that
1551 * another node mounted (and recovered) the slot before the recovery
1552 * thread could get the lock. To handle that, we dirty read the journal
1553 * inode for that slot to get the recovery generation. If it is
1554 * different than what we expected, the slot has been recovered.
1555 * If not, it needs recovery.
1556 */
1557 if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
1558 mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
1559 osb->slot_recovery_generations[slot_num], slot_reco_gen);
1560 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
1561 status = -EBUSY;
Mark Fashehccd979b2005-12-15 14:31:24 -08001562 goto done;
1563 }
Sunil Mushran539d8262008-07-14 17:31:10 -07001564
1565 /* Continue with recovery as the journal has not yet been recovered */
Mark Fashehccd979b2005-12-15 14:31:24 -08001566
Mark Fashehe63aecb62007-10-18 15:30:42 -07001567 status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
Mark Fashehccd979b2005-12-15 14:31:24 -08001568 if (status < 0) {
Mark Fashehe63aecb62007-10-18 15:30:42 -07001569 mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 if (status != -ERESTARTSYS)
1571 mlog(ML_ERROR, "Could not lock journal!\n");
1572 goto done;
1573 }
1574 got_lock = 1;
1575
1576 fe = (struct ocfs2_dinode *) bh->b_data;
1577
1578 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
Sunil Mushran539d8262008-07-14 17:31:10 -07001579 slot_reco_gen = ocfs2_get_recovery_generation(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -08001580
1581 if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
1582 mlog(0, "No recovery required for node %d\n", node_num);
Sunil Mushran539d8262008-07-14 17:31:10 -07001583 /* Refresh recovery generation for the slot */
1584 osb->slot_recovery_generations[slot_num] = slot_reco_gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001585 goto done;
1586 }
1587
Srinivas Eeda9140db02009-03-06 14:21:46 -08001588 /* we need to run complete recovery for offline orphan slots */
1589 ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
1590
Mark Fashehccd979b2005-12-15 14:31:24 -08001591 mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
1592 node_num, slot_num,
1593 MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
1594
1595 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
1596
1597 status = ocfs2_force_read_journal(inode);
1598 if (status < 0) {
1599 mlog_errno(status);
1600 goto done;
1601 }
1602
1603 mlog(0, "calling journal_init_inode\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001604 journal = jbd2_journal_init_inode(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001605 if (journal == NULL) {
1606 mlog(ML_ERROR, "Linux journal layer error\n");
1607 status = -EIO;
1608 goto done;
1609 }
1610
Joel Becker2b4e30f2008-09-03 20:03:41 -07001611 status = jbd2_journal_load(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001612 if (status < 0) {
1613 mlog_errno(status);
1614 if (!igrab(inode))
1615 BUG();
Joel Becker2b4e30f2008-09-03 20:03:41 -07001616 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001617 goto done;
1618 }
1619
1620 ocfs2_clear_journal_error(osb->sb, journal, slot_num);
1621
1622 /* wipe the journal */
1623 mlog(0, "flushing the journal.\n");
Joel Becker2b4e30f2008-09-03 20:03:41 -07001624 jbd2_journal_lock_updates(journal);
1625 status = jbd2_journal_flush(journal);
1626 jbd2_journal_unlock_updates(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001627 if (status < 0)
1628 mlog_errno(status);
1629
1630 /* This will mark the node clean */
1631 flags = le32_to_cpu(fe->id1.journal1.ij_flags);
1632 flags &= ~OCFS2_JOURNAL_DIRTY_FL;
1633 fe->id1.journal1.ij_flags = cpu_to_le32(flags);
1634
Sunil Mushran539d8262008-07-14 17:31:10 -07001635 /* Increment recovery generation to indicate successful recovery */
1636 ocfs2_bump_recovery_generation(fe);
1637 osb->slot_recovery_generations[slot_num] =
1638 ocfs2_get_recovery_generation(fe);
1639
Joel Becker13723d02008-10-17 19:25:01 -07001640 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -08001641 status = ocfs2_write_block(osb, bh, inode);
1642 if (status < 0)
1643 mlog_errno(status);
1644
1645 if (!igrab(inode))
1646 BUG();
1647
Joel Becker2b4e30f2008-09-03 20:03:41 -07001648 jbd2_journal_destroy(journal);
Mark Fashehccd979b2005-12-15 14:31:24 -08001649
1650done:
1651 /* drop the lock on this nodes journal */
1652 if (got_lock)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001653 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001654
1655 if (inode)
1656 iput(inode);
1657
Mark Fasheha81cb882008-10-07 14:25:16 -07001658 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001659
1660 mlog_exit(status);
1661 return status;
1662}
1663
1664/*
1665 * Do the most important parts of node recovery:
1666 * - Replay it's journal
1667 * - Stamp a clean local allocator file
1668 * - Stamp a clean truncate log
1669 * - Mark the node clean
1670 *
1671 * If this function completes without error, a node in OCFS2 can be
1672 * said to have been safely recovered. As a result, failure during the
1673 * second part of a nodes recovery process (local alloc recovery) is
1674 * far less concerning.
1675 */
1676static int ocfs2_recover_node(struct ocfs2_super *osb,
Jan Kara22053632008-10-20 23:50:38 +02001677 int node_num, int slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -08001678{
1679 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001680 struct ocfs2_dinode *la_copy = NULL;
1681 struct ocfs2_dinode *tl_copy = NULL;
1682
Jan Kara22053632008-10-20 23:50:38 +02001683 mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
1684 node_num, slot_num, osb->node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001685
1686 /* Should not ever be called to recover ourselves -- in that
1687 * case we should've called ocfs2_journal_load instead. */
Eric Sesterhenn / snakebyteebdec832006-01-27 10:32:52 +01001688 BUG_ON(osb->node_num == node_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001689
Mark Fashehccd979b2005-12-15 14:31:24 -08001690 status = ocfs2_replay_journal(osb, node_num, slot_num);
1691 if (status < 0) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001692 if (status == -EBUSY) {
1693 mlog(0, "Skipping recovery for slot %u (node %u) "
1694 "as another node has recovered it\n", slot_num,
1695 node_num);
1696 status = 0;
1697 goto done;
1698 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001699 mlog_errno(status);
1700 goto done;
1701 }
1702
1703 /* Stamp a clean local alloc file AFTER recovering the journal... */
1704 status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
1705 if (status < 0) {
1706 mlog_errno(status);
1707 goto done;
1708 }
1709
1710 /* An error from begin_truncate_log_recovery is not
1711 * serious enough to warrant halting the rest of
1712 * recovery. */
1713 status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
1714 if (status < 0)
1715 mlog_errno(status);
1716
1717 /* Likewise, this would be a strange but ultimately not so
1718 * harmful place to get an error... */
Mark Fasheh8e8a4602008-02-01 11:59:09 -08001719 status = ocfs2_clear_slot(osb, slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001720 if (status < 0)
1721 mlog_errno(status);
1722
1723 /* This will kfree the memory pointed to by la_copy and tl_copy */
1724 ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
Jan Kara22053632008-10-20 23:50:38 +02001725 tl_copy, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001726
1727 status = 0;
1728done:
1729
1730 mlog_exit(status);
1731 return status;
1732}
1733
1734/* Test node liveness by trylocking his journal. If we get the lock,
1735 * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
1736 * still alive (we couldn't get the lock) and < 0 on error. */
1737static int ocfs2_trylock_journal(struct ocfs2_super *osb,
1738 int slot_num)
1739{
1740 int status, flags;
1741 struct inode *inode = NULL;
1742
1743 inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
1744 slot_num);
1745 if (inode == NULL) {
1746 mlog(ML_ERROR, "access error\n");
1747 status = -EACCES;
1748 goto bail;
1749 }
1750 if (is_bad_inode(inode)) {
1751 mlog(ML_ERROR, "access error (bad inode)\n");
1752 iput(inode);
1753 inode = NULL;
1754 status = -EACCES;
1755 goto bail;
1756 }
1757 SET_INODE_JOURNAL(inode);
1758
1759 flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001760 status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001761 if (status < 0) {
1762 if (status != -EAGAIN)
1763 mlog_errno(status);
1764 goto bail;
1765 }
1766
Mark Fashehe63aecb62007-10-18 15:30:42 -07001767 ocfs2_inode_unlock(inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001768bail:
1769 if (inode)
1770 iput(inode);
1771
1772 return status;
1773}
1774
1775/* Call this underneath ocfs2_super_lock. It also assumes that the
1776 * slot info struct has been updated from disk. */
1777int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
1778{
Joel Beckerd85b20e2008-02-01 12:01:05 -08001779 unsigned int node_num;
1780 int status, i;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001781 u32 gen;
Sunil Mushran539d8262008-07-14 17:31:10 -07001782 struct buffer_head *bh = NULL;
1783 struct ocfs2_dinode *di;
Mark Fashehccd979b2005-12-15 14:31:24 -08001784
1785 /* This is called with the super block cluster lock, so we
1786 * know that the slot map can't change underneath us. */
1787
Joel Beckerd85b20e2008-02-01 12:01:05 -08001788 for (i = 0; i < osb->max_slots; i++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07001789 /* Read journal inode to get the recovery generation */
1790 status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
1791 if (status) {
1792 mlog_errno(status);
1793 goto bail;
1794 }
1795 di = (struct ocfs2_dinode *)bh->b_data;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001796 gen = ocfs2_get_recovery_generation(di);
Sunil Mushran539d8262008-07-14 17:31:10 -07001797 brelse(bh);
1798 bh = NULL;
1799
Mark Fasheha1af7d12008-08-19 17:20:28 -07001800 spin_lock(&osb->osb_lock);
1801 osb->slot_recovery_generations[i] = gen;
1802
Sunil Mushran539d8262008-07-14 17:31:10 -07001803 mlog(0, "Slot %u recovery generation is %u\n", i,
1804 osb->slot_recovery_generations[i]);
1805
Mark Fasheha1af7d12008-08-19 17:20:28 -07001806 if (i == osb->slot_num) {
1807 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001808 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001809 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001810
1811 status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
Mark Fasheha1af7d12008-08-19 17:20:28 -07001812 if (status == -ENOENT) {
1813 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001814 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001815 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001816
Mark Fasheha1af7d12008-08-19 17:20:28 -07001817 if (__ocfs2_recovery_map_test(osb, node_num)) {
1818 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001819 continue;
Mark Fasheha1af7d12008-08-19 17:20:28 -07001820 }
Joel Beckerd85b20e2008-02-01 12:01:05 -08001821 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08001822
1823 /* Ok, we have a slot occupied by another node which
1824 * is not in the recovery map. We trylock his journal
1825 * file here to test if he's alive. */
1826 status = ocfs2_trylock_journal(osb, i);
1827 if (!status) {
1828 /* Since we're called from mount, we know that
1829 * the recovery thread can't race us on
1830 * setting / checking the recovery bits. */
1831 ocfs2_recovery_thread(osb, node_num);
1832 } else if ((status < 0) && (status != -EAGAIN)) {
1833 mlog_errno(status);
1834 goto bail;
1835 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001836 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001837
1838 status = 0;
1839bail:
1840 mlog_exit(status);
1841 return status;
1842}
1843
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001844struct ocfs2_orphan_filldir_priv {
1845 struct inode *head;
1846 struct ocfs2_super *osb;
1847};
1848
1849static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
1850 loff_t pos, u64 ino, unsigned type)
1851{
1852 struct ocfs2_orphan_filldir_priv *p = priv;
1853 struct inode *iter;
1854
1855 if (name_len == 1 && !strncmp(".", name, 1))
1856 return 0;
1857 if (name_len == 2 && !strncmp("..", name, 2))
1858 return 0;
1859
1860 /* Skip bad inodes so that recovery can continue */
1861 iter = ocfs2_iget(p->osb, ino,
Jan Kara5fa06132008-01-11 00:11:45 +01001862 OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001863 if (IS_ERR(iter))
1864 return 0;
1865
1866 mlog(0, "queue orphan %llu\n",
1867 (unsigned long long)OCFS2_I(iter)->ip_blkno);
1868 /* No locking is required for the next_orphan queue as there
1869 * is only ever a single process doing orphan recovery. */
1870 OCFS2_I(iter)->ip_next_orphan = p->head;
1871 p->head = iter;
1872
1873 return 0;
1874}
1875
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001876static int ocfs2_queue_orphans(struct ocfs2_super *osb,
1877 int slot,
1878 struct inode **head)
Mark Fashehccd979b2005-12-15 14:31:24 -08001879{
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001880 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001881 struct inode *orphan_dir_inode = NULL;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001882 struct ocfs2_orphan_filldir_priv priv;
1883 loff_t pos = 0;
1884
1885 priv.osb = osb;
1886 priv.head = *head;
Mark Fashehccd979b2005-12-15 14:31:24 -08001887
1888 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
1889 ORPHAN_DIR_SYSTEM_INODE,
1890 slot);
1891 if (!orphan_dir_inode) {
1892 status = -ENOENT;
1893 mlog_errno(status);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001894 return status;
1895 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001896
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001897 mutex_lock(&orphan_dir_inode->i_mutex);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001898 status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001899 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 mlog_errno(status);
1901 goto out;
1902 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001903
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001904 status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
1905 ocfs2_orphan_filldir);
1906 if (status) {
1907 mlog_errno(status);
Mark Fasheha86370f2007-12-03 14:06:23 -08001908 goto out_cluster;
Mark Fashehccd979b2005-12-15 14:31:24 -08001909 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001910
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001911 *head = priv.head;
1912
Mark Fasheha86370f2007-12-03 14:06:23 -08001913out_cluster:
Mark Fashehe63aecb62007-10-18 15:30:42 -07001914 ocfs2_inode_unlock(orphan_dir_inode, 0);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001915out:
1916 mutex_unlock(&orphan_dir_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001917 iput(orphan_dir_inode);
Mark Fashehb4df6ed2006-02-22 17:35:08 -08001918 return status;
1919}
1920
1921static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
1922 int slot)
1923{
1924 int ret;
1925
1926 spin_lock(&osb->osb_lock);
1927 ret = !osb->osb_orphan_wipes[slot];
1928 spin_unlock(&osb->osb_lock);
1929 return ret;
1930}
1931
1932static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
1933 int slot)
1934{
1935 spin_lock(&osb->osb_lock);
1936 /* Mark ourselves such that new processes in delete_inode()
1937 * know to quit early. */
1938 ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1939 while (osb->osb_orphan_wipes[slot]) {
1940 /* If any processes are already in the middle of an
1941 * orphan wipe on this dir, then we need to wait for
1942 * them. */
1943 spin_unlock(&osb->osb_lock);
1944 wait_event_interruptible(osb->osb_wipe_event,
1945 ocfs2_orphan_recovery_can_continue(osb, slot));
1946 spin_lock(&osb->osb_lock);
1947 }
1948 spin_unlock(&osb->osb_lock);
1949}
1950
1951static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
1952 int slot)
1953{
1954 ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
1955}
1956
1957/*
1958 * Orphan recovery. Each mounted node has it's own orphan dir which we
1959 * must run during recovery. Our strategy here is to build a list of
1960 * the inodes in the orphan dir and iget/iput them. The VFS does
1961 * (most) of the rest of the work.
1962 *
1963 * Orphan recovery can happen at any time, not just mount so we have a
1964 * couple of extra considerations.
1965 *
1966 * - We grab as many inodes as we can under the orphan dir lock -
1967 * doing iget() outside the orphan dir risks getting a reference on
1968 * an invalid inode.
1969 * - We must be sure not to deadlock with other processes on the
1970 * system wanting to run delete_inode(). This can happen when they go
1971 * to lock the orphan dir and the orphan recovery process attempts to
1972 * iget() inside the orphan dir lock. This can be avoided by
1973 * advertising our state to ocfs2_delete_inode().
1974 */
1975static int ocfs2_recover_orphans(struct ocfs2_super *osb,
1976 int slot)
1977{
1978 int ret = 0;
1979 struct inode *inode = NULL;
1980 struct inode *iter;
1981 struct ocfs2_inode_info *oi;
1982
1983 mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
1984
1985 ocfs2_mark_recovering_orphan_dir(osb, slot);
1986 ret = ocfs2_queue_orphans(osb, slot, &inode);
1987 ocfs2_clear_recovering_orphan_dir(osb, slot);
1988
1989 /* Error here should be noted, but we want to continue with as
1990 * many queued inodes as we've got. */
1991 if (ret)
1992 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001993
1994 while (inode) {
1995 oi = OCFS2_I(inode);
Mark Fashehb06970532006-03-03 10:24:33 -08001996 mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001997
1998 iter = oi->ip_next_orphan;
1999
2000 spin_lock(&oi->ip_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002001 /* The remote delete code may have set these on the
2002 * assumption that the other node would wipe them
2003 * successfully. If they are still in the node's
2004 * orphan dir, we need to reset that state. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002005 oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
2006
2007 /* Set the proper information to get us going into
2008 * ocfs2_delete_inode. */
2009 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
Mark Fashehccd979b2005-12-15 14:31:24 -08002010 spin_unlock(&oi->ip_lock);
2011
2012 iput(inode);
2013
2014 inode = iter;
2015 }
2016
Mark Fashehb4df6ed2006-02-22 17:35:08 -08002017 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002018}
2019
Jan Kara19ece542008-08-21 20:13:17 +02002020static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
Mark Fashehccd979b2005-12-15 14:31:24 -08002021{
2022 /* This check is good because ocfs2 will wait on our recovery
2023 * thread before changing it to something other than MOUNTED
2024 * or DISABLED. */
2025 wait_event(osb->osb_mount_event,
Jan Kara19ece542008-08-21 20:13:17 +02002026 (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
2027 atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
Mark Fashehccd979b2005-12-15 14:31:24 -08002028 atomic_read(&osb->vol_state) == VOLUME_DISABLED);
2029
2030 /* If there's an error on mount, then we may never get to the
2031 * MOUNTED flag, but this is set right before
2032 * dismount_volume() so we can trust it. */
2033 if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
2034 mlog(0, "mount error, exiting!\n");
2035 return -EBUSY;
2036 }
2037
2038 return 0;
2039}
2040
2041static int ocfs2_commit_thread(void *arg)
2042{
2043 int status;
2044 struct ocfs2_super *osb = arg;
2045 struct ocfs2_journal *journal = osb->journal;
2046
2047 /* we can trust j_num_trans here because _should_stop() is only set in
2048 * shutdown and nobody other than ourselves should be able to start
2049 * transactions. committing on shutdown might take a few iterations
2050 * as final transactions put deleted inodes on the list */
2051 while (!(kthread_should_stop() &&
2052 atomic_read(&journal->j_num_trans) == 0)) {
2053
Mark Fasheh745ae8ba2006-02-09 13:23:39 -08002054 wait_event_interruptible(osb->checkpoint_event,
2055 atomic_read(&journal->j_num_trans)
2056 || kthread_should_stop());
Mark Fashehccd979b2005-12-15 14:31:24 -08002057
2058 status = ocfs2_commit_cache(osb);
2059 if (status < 0)
2060 mlog_errno(status);
2061
2062 if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
2063 mlog(ML_KTHREAD,
2064 "commit_thread: %u transactions pending on "
2065 "shutdown\n",
2066 atomic_read(&journal->j_num_trans));
2067 }
2068 }
2069
2070 return 0;
2071}
2072
Sunil Mushran539d8262008-07-14 17:31:10 -07002073/* Reads all the journal inodes without taking any cluster locks. Used
2074 * for hard readonly access to determine whether any journal requires
2075 * recovery. Also used to refresh the recovery generation numbers after
2076 * a journal has been recovered by another node.
2077 */
Mark Fashehccd979b2005-12-15 14:31:24 -08002078int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
2079{
2080 int ret = 0;
2081 unsigned int slot;
Sunil Mushran539d8262008-07-14 17:31:10 -07002082 struct buffer_head *di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002083 struct ocfs2_dinode *di;
Sunil Mushran539d8262008-07-14 17:31:10 -07002084 int journal_dirty = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002085
2086 for(slot = 0; slot < osb->max_slots; slot++) {
Sunil Mushran539d8262008-07-14 17:31:10 -07002087 ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
2088 if (ret) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002089 mlog_errno(ret);
2090 goto out;
2091 }
2092
2093 di = (struct ocfs2_dinode *) di_bh->b_data;
2094
Sunil Mushran539d8262008-07-14 17:31:10 -07002095 osb->slot_recovery_generations[slot] =
2096 ocfs2_get_recovery_generation(di);
2097
Mark Fashehccd979b2005-12-15 14:31:24 -08002098 if (le32_to_cpu(di->id1.journal1.ij_flags) &
2099 OCFS2_JOURNAL_DIRTY_FL)
Sunil Mushran539d8262008-07-14 17:31:10 -07002100 journal_dirty = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002101
2102 brelse(di_bh);
Sunil Mushran539d8262008-07-14 17:31:10 -07002103 di_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002104 }
2105
2106out:
Sunil Mushran539d8262008-07-14 17:31:10 -07002107 if (journal_dirty)
2108 ret = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08002109 return ret;
2110}