Commit | Line | Data |
---|---|---|
470decc6 | 1 | /* |
f7f4bccb | 2 | * linux/fs/jbd2/journal.c |
470decc6 DK |
3 | * |
4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 | |
5 | * | |
6 | * Copyright 1998 Red Hat corp --- All Rights Reserved | |
7 | * | |
8 | * This file is part of the Linux kernel and is made available under | |
9 | * the terms of the GNU General Public License, version 2, or at your | |
10 | * option, any later version, incorporated herein by reference. | |
11 | * | |
12 | * Generic filesystem journal-writing code; part of the ext2fs | |
13 | * journaling system. | |
14 | * | |
15 | * This file manages journals: areas of disk reserved for logging | |
16 | * transactional updates. This includes the kernel journaling thread | |
17 | * which is responsible for scheduling updates to the log. | |
18 | * | |
19 | * We do not actually manage the physical storage of the journal in this | |
20 | * file: that is left to a per-journal policy function, which allows us | |
21 | * to store the journal within a filesystem-specified area for ext2 | |
22 | * journaling (ext2 can use a reserved inode for storing the log). | |
23 | */ | |
24 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/fs.h> | |
f7f4bccb | 28 | #include <linux/jbd2.h> |
470decc6 DK |
29 | #include <linux/errno.h> |
30 | #include <linux/slab.h> | |
470decc6 DK |
31 | #include <linux/init.h> |
32 | #include <linux/mm.h> | |
7dfb7103 | 33 | #include <linux/freezer.h> |
470decc6 DK |
34 | #include <linux/pagemap.h> |
35 | #include <linux/kthread.h> | |
36 | #include <linux/poison.h> | |
37 | #include <linux/proc_fs.h> | |
0f49d5d0 | 38 | #include <linux/debugfs.h> |
8e85fb3f | 39 | #include <linux/seq_file.h> |
c225aa57 | 40 | #include <linux/math64.h> |
879c5e6b | 41 | #include <linux/hash.h> |
d2eecb03 TT |
42 | #include <linux/log2.h> |
43 | #include <linux/vmalloc.h> | |
47def826 | 44 | #include <linux/backing-dev.h> |
879c5e6b TT |
45 | |
46 | #define CREATE_TRACE_POINTS | |
47 | #include <trace/events/jbd2.h> | |
470decc6 DK |
48 | |
49 | #include <asm/uaccess.h> | |
50 | #include <asm/page.h> | |
51 | ||
f7f4bccb MC |
52 | EXPORT_SYMBOL(jbd2_journal_extend); |
53 | EXPORT_SYMBOL(jbd2_journal_stop); | |
54 | EXPORT_SYMBOL(jbd2_journal_lock_updates); | |
55 | EXPORT_SYMBOL(jbd2_journal_unlock_updates); | |
56 | EXPORT_SYMBOL(jbd2_journal_get_write_access); | |
57 | EXPORT_SYMBOL(jbd2_journal_get_create_access); | |
58 | EXPORT_SYMBOL(jbd2_journal_get_undo_access); | |
e06c8227 | 59 | EXPORT_SYMBOL(jbd2_journal_set_triggers); |
f7f4bccb MC |
60 | EXPORT_SYMBOL(jbd2_journal_dirty_metadata); |
61 | EXPORT_SYMBOL(jbd2_journal_release_buffer); | |
62 | EXPORT_SYMBOL(jbd2_journal_forget); | |
470decc6 DK |
63 | #if 0 |
64 | EXPORT_SYMBOL(journal_sync_buffer); | |
65 | #endif | |
f7f4bccb MC |
66 | EXPORT_SYMBOL(jbd2_journal_flush); |
67 | EXPORT_SYMBOL(jbd2_journal_revoke); | |
68 | ||
69 | EXPORT_SYMBOL(jbd2_journal_init_dev); | |
70 | EXPORT_SYMBOL(jbd2_journal_init_inode); | |
71 | EXPORT_SYMBOL(jbd2_journal_update_format); | |
72 | EXPORT_SYMBOL(jbd2_journal_check_used_features); | |
73 | EXPORT_SYMBOL(jbd2_journal_check_available_features); | |
74 | EXPORT_SYMBOL(jbd2_journal_set_features); | |
f7f4bccb MC |
75 | EXPORT_SYMBOL(jbd2_journal_load); |
76 | EXPORT_SYMBOL(jbd2_journal_destroy); | |
f7f4bccb MC |
77 | EXPORT_SYMBOL(jbd2_journal_abort); |
78 | EXPORT_SYMBOL(jbd2_journal_errno); | |
79 | EXPORT_SYMBOL(jbd2_journal_ack_err); | |
80 | EXPORT_SYMBOL(jbd2_journal_clear_err); | |
81 | EXPORT_SYMBOL(jbd2_log_wait_commit); | |
3b799d15 | 82 | EXPORT_SYMBOL(jbd2_log_start_commit); |
f7f4bccb MC |
83 | EXPORT_SYMBOL(jbd2_journal_start_commit); |
84 | EXPORT_SYMBOL(jbd2_journal_force_commit_nested); | |
85 | EXPORT_SYMBOL(jbd2_journal_wipe); | |
86 | EXPORT_SYMBOL(jbd2_journal_blocks_per_page); | |
87 | EXPORT_SYMBOL(jbd2_journal_invalidatepage); | |
88 | EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); | |
89 | EXPORT_SYMBOL(jbd2_journal_force_commit); | |
c851ed54 JK |
90 | EXPORT_SYMBOL(jbd2_journal_file_inode); |
91 | EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); | |
92 | EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); | |
93 | EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); | |
470decc6 DK |
94 | |
95 | static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); | |
96 | static void __journal_abort_soft (journal_t *journal, int errno); | |
d2eecb03 | 97 | static int jbd2_journal_create_slab(size_t slab_size); |
470decc6 DK |
98 | |
99 | /* | |
100 | * Helper function used to manage commit timeouts | |
101 | */ | |
102 | ||
103 | static void commit_timeout(unsigned long __data) | |
104 | { | |
105 | struct task_struct * p = (struct task_struct *) __data; | |
106 | ||
107 | wake_up_process(p); | |
108 | } | |
109 | ||
110 | /* | |
f7f4bccb | 111 | * kjournald2: The main thread function used to manage a logging device |
470decc6 DK |
112 | * journal. |
113 | * | |
114 | * This kernel thread is responsible for two things: | |
115 | * | |
116 | * 1) COMMIT: Every so often we need to commit the current state of the | |
117 | * filesystem to disk. The journal thread is responsible for writing | |
118 | * all of the metadata buffers to disk. | |
119 | * | |
120 | * 2) CHECKPOINT: We cannot reuse a used section of the log file until all | |
121 | * of the data in that part of the log has been rewritten elsewhere on | |
122 | * the disk. Flushing these old buffers to reclaim space in the log is | |
123 | * known as checkpointing, and this thread is responsible for that job. | |
124 | */ | |
125 | ||
f7f4bccb | 126 | static int kjournald2(void *arg) |
470decc6 DK |
127 | { |
128 | journal_t *journal = arg; | |
129 | transaction_t *transaction; | |
130 | ||
131 | /* | |
132 | * Set up an interval timer which can be used to trigger a commit wakeup | |
133 | * after the commit interval expires | |
134 | */ | |
135 | setup_timer(&journal->j_commit_timer, commit_timeout, | |
136 | (unsigned long)current); | |
137 | ||
138 | /* Record that the journal thread is running */ | |
139 | journal->j_task = current; | |
140 | wake_up(&journal->j_wait_done_commit); | |
141 | ||
470decc6 DK |
142 | /* |
143 | * And now, wait forever for commit wakeup events. | |
144 | */ | |
a931da6a | 145 | write_lock(&journal->j_state_lock); |
470decc6 DK |
146 | |
147 | loop: | |
f7f4bccb | 148 | if (journal->j_flags & JBD2_UNMOUNT) |
470decc6 DK |
149 | goto end_loop; |
150 | ||
151 | jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", | |
152 | journal->j_commit_sequence, journal->j_commit_request); | |
153 | ||
154 | if (journal->j_commit_sequence != journal->j_commit_request) { | |
155 | jbd_debug(1, "OK, requests differ\n"); | |
a931da6a | 156 | write_unlock(&journal->j_state_lock); |
470decc6 | 157 | del_timer_sync(&journal->j_commit_timer); |
f7f4bccb | 158 | jbd2_journal_commit_transaction(journal); |
a931da6a | 159 | write_lock(&journal->j_state_lock); |
470decc6 DK |
160 | goto loop; |
161 | } | |
162 | ||
163 | wake_up(&journal->j_wait_done_commit); | |
164 | if (freezing(current)) { | |
165 | /* | |
166 | * The simpler the better. Flushing journal isn't a | |
167 | * good idea, because that depends on threads that may | |
168 | * be already stopped. | |
169 | */ | |
f7f4bccb | 170 | jbd_debug(1, "Now suspending kjournald2\n"); |
a931da6a | 171 | write_unlock(&journal->j_state_lock); |
470decc6 | 172 | refrigerator(); |
a931da6a | 173 | write_lock(&journal->j_state_lock); |
470decc6 DK |
174 | } else { |
175 | /* | |
176 | * We assume on resume that commits are already there, | |
177 | * so we don't sleep | |
178 | */ | |
179 | DEFINE_WAIT(wait); | |
180 | int should_sleep = 1; | |
181 | ||
182 | prepare_to_wait(&journal->j_wait_commit, &wait, | |
183 | TASK_INTERRUPTIBLE); | |
184 | if (journal->j_commit_sequence != journal->j_commit_request) | |
185 | should_sleep = 0; | |
186 | transaction = journal->j_running_transaction; | |
187 | if (transaction && time_after_eq(jiffies, | |
188 | transaction->t_expires)) | |
189 | should_sleep = 0; | |
f7f4bccb | 190 | if (journal->j_flags & JBD2_UNMOUNT) |
470decc6 DK |
191 | should_sleep = 0; |
192 | if (should_sleep) { | |
a931da6a | 193 | write_unlock(&journal->j_state_lock); |
470decc6 | 194 | schedule(); |
a931da6a | 195 | write_lock(&journal->j_state_lock); |
470decc6 DK |
196 | } |
197 | finish_wait(&journal->j_wait_commit, &wait); | |
198 | } | |
199 | ||
f7f4bccb | 200 | jbd_debug(1, "kjournald2 wakes\n"); |
470decc6 DK |
201 | |
202 | /* | |
203 | * Were we woken up by a commit wakeup event? | |
204 | */ | |
205 | transaction = journal->j_running_transaction; | |
206 | if (transaction && time_after_eq(jiffies, transaction->t_expires)) { | |
207 | journal->j_commit_request = transaction->t_tid; | |
208 | jbd_debug(1, "woke because of timeout\n"); | |
209 | } | |
210 | goto loop; | |
211 | ||
212 | end_loop: | |
a931da6a | 213 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
214 | del_timer_sync(&journal->j_commit_timer); |
215 | journal->j_task = NULL; | |
216 | wake_up(&journal->j_wait_done_commit); | |
217 | jbd_debug(1, "Journal thread exiting.\n"); | |
218 | return 0; | |
219 | } | |
220 | ||
97f06784 | 221 | static int jbd2_journal_start_thread(journal_t *journal) |
470decc6 | 222 | { |
97f06784 PE |
223 | struct task_struct *t; |
224 | ||
90576c0b TT |
225 | t = kthread_run(kjournald2, journal, "jbd2/%s", |
226 | journal->j_devname); | |
97f06784 PE |
227 | if (IS_ERR(t)) |
228 | return PTR_ERR(t); | |
229 | ||
1076d17a | 230 | wait_event(journal->j_wait_done_commit, journal->j_task != NULL); |
97f06784 | 231 | return 0; |
470decc6 DK |
232 | } |
233 | ||
234 | static void journal_kill_thread(journal_t *journal) | |
235 | { | |
a931da6a | 236 | write_lock(&journal->j_state_lock); |
f7f4bccb | 237 | journal->j_flags |= JBD2_UNMOUNT; |
470decc6 DK |
238 | |
239 | while (journal->j_task) { | |
240 | wake_up(&journal->j_wait_commit); | |
a931da6a | 241 | write_unlock(&journal->j_state_lock); |
1076d17a | 242 | wait_event(journal->j_wait_done_commit, journal->j_task == NULL); |
a931da6a | 243 | write_lock(&journal->j_state_lock); |
470decc6 | 244 | } |
a931da6a | 245 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
246 | } |
247 | ||
248 | /* | |
f7f4bccb | 249 | * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. |
470decc6 DK |
250 | * |
251 | * Writes a metadata buffer to a given disk block. The actual IO is not | |
252 | * performed but a new buffer_head is constructed which labels the data | |
253 | * to be written with the correct destination disk block. | |
254 | * | |
255 | * Any magic-number escaping which needs to be done will cause a | |
256 | * copy-out here. If the buffer happens to start with the | |
f7f4bccb | 257 | * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the |
470decc6 DK |
258 | * magic number is only written to the log for descripter blocks. In |
259 | * this case, we copy the data and replace the first word with 0, and we | |
260 | * return a result code which indicates that this buffer needs to be | |
261 | * marked as an escaped buffer in the corresponding log descriptor | |
262 | * block. The missing word can then be restored when the block is read | |
263 | * during recovery. | |
264 | * | |
265 | * If the source buffer has already been modified by a new transaction | |
266 | * since we took the last commit snapshot, we use the frozen copy of | |
267 | * that data for IO. If we end up using the existing buffer_head's data | |
268 | * for the write, then we *have* to lock the buffer to prevent anyone | |
269 | * else from using and possibly modifying it while the IO is in | |
270 | * progress. | |
271 | * | |
272 | * The function returns a pointer to the buffer_heads to be used for IO. | |
273 | * | |
274 | * We assume that the journal has already been locked in this function. | |
275 | * | |
276 | * Return value: | |
277 | * <0: Error | |
278 | * >=0: Finished OK | |
279 | * | |
280 | * On success: | |
281 | * Bit 0 set == escape performed on the data | |
282 | * Bit 1 set == buffer copy-out performed (kfree the data after IO) | |
283 | */ | |
284 | ||
f7f4bccb | 285 | int jbd2_journal_write_metadata_buffer(transaction_t *transaction, |
470decc6 DK |
286 | struct journal_head *jh_in, |
287 | struct journal_head **jh_out, | |
18eba7aa | 288 | unsigned long long blocknr) |
470decc6 DK |
289 | { |
290 | int need_copy_out = 0; | |
291 | int done_copy_out = 0; | |
292 | int do_escape = 0; | |
293 | char *mapped_data; | |
294 | struct buffer_head *new_bh; | |
295 | struct journal_head *new_jh; | |
296 | struct page *new_page; | |
297 | unsigned int new_offset; | |
298 | struct buffer_head *bh_in = jh2bh(jh_in); | |
96577c43 | 299 | journal_t *journal = transaction->t_journal; |
470decc6 DK |
300 | |
301 | /* | |
302 | * The buffer really shouldn't be locked: only the current committing | |
303 | * transaction is allowed to write it, so nobody else is allowed | |
304 | * to do any IO. | |
305 | * | |
306 | * akpm: except if we're journalling data, and write() output is | |
307 | * also part of a shared mapping, and another thread has | |
308 | * decided to launch a writepage() against this buffer. | |
309 | */ | |
310 | J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); | |
311 | ||
47def826 TT |
312 | retry_alloc: |
313 | new_bh = alloc_buffer_head(GFP_NOFS); | |
314 | if (!new_bh) { | |
315 | /* | |
316 | * Failure is not an option, but __GFP_NOFAIL is going | |
317 | * away; so we retry ourselves here. | |
318 | */ | |
319 | congestion_wait(BLK_RW_ASYNC, HZ/50); | |
320 | goto retry_alloc; | |
321 | } | |
322 | ||
96577c43 | 323 | /* keep subsequent assertions sane */ |
324 | new_bh->b_state = 0; | |
325 | init_buffer(new_bh, NULL, NULL); | |
326 | atomic_set(&new_bh->b_count, 1); | |
327 | new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ | |
470decc6 DK |
328 | |
329 | /* | |
330 | * If a new transaction has already done a buffer copy-out, then | |
331 | * we use that version of the data for the commit. | |
332 | */ | |
333 | jbd_lock_bh_state(bh_in); | |
334 | repeat: | |
335 | if (jh_in->b_frozen_data) { | |
336 | done_copy_out = 1; | |
337 | new_page = virt_to_page(jh_in->b_frozen_data); | |
338 | new_offset = offset_in_page(jh_in->b_frozen_data); | |
339 | } else { | |
340 | new_page = jh2bh(jh_in)->b_page; | |
341 | new_offset = offset_in_page(jh2bh(jh_in)->b_data); | |
342 | } | |
343 | ||
344 | mapped_data = kmap_atomic(new_page, KM_USER0); | |
e06c8227 | 345 | /* |
13ceef09 JK |
346 | * Fire data frozen trigger if data already wasn't frozen. Do this |
347 | * before checking for escaping, as the trigger may modify the magic | |
348 | * offset. If a copy-out happens afterwards, it will have the correct | |
349 | * data in the buffer. | |
e06c8227 | 350 | */ |
13ceef09 JK |
351 | if (!done_copy_out) |
352 | jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, | |
353 | jh_in->b_triggers); | |
e06c8227 | 354 | |
470decc6 DK |
355 | /* |
356 | * Check for escaping | |
357 | */ | |
358 | if (*((__be32 *)(mapped_data + new_offset)) == | |
f7f4bccb | 359 | cpu_to_be32(JBD2_MAGIC_NUMBER)) { |
470decc6 DK |
360 | need_copy_out = 1; |
361 | do_escape = 1; | |
362 | } | |
363 | kunmap_atomic(mapped_data, KM_USER0); | |
364 | ||
365 | /* | |
366 | * Do we need to do a data copy? | |
367 | */ | |
368 | if (need_copy_out && !done_copy_out) { | |
369 | char *tmp; | |
370 | ||
371 | jbd_unlock_bh_state(bh_in); | |
af1e76d6 | 372 | tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); |
e6ec116b TT |
373 | if (!tmp) { |
374 | jbd2_journal_put_journal_head(new_jh); | |
375 | return -ENOMEM; | |
376 | } | |
470decc6 DK |
377 | jbd_lock_bh_state(bh_in); |
378 | if (jh_in->b_frozen_data) { | |
af1e76d6 | 379 | jbd2_free(tmp, bh_in->b_size); |
470decc6 DK |
380 | goto repeat; |
381 | } | |
382 | ||
383 | jh_in->b_frozen_data = tmp; | |
384 | mapped_data = kmap_atomic(new_page, KM_USER0); | |
385 | memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); | |
386 | kunmap_atomic(mapped_data, KM_USER0); | |
387 | ||
388 | new_page = virt_to_page(tmp); | |
389 | new_offset = offset_in_page(tmp); | |
390 | done_copy_out = 1; | |
e06c8227 JB |
391 | |
392 | /* | |
393 | * This isn't strictly necessary, as we're using frozen | |
394 | * data for the escaping, but it keeps consistency with | |
395 | * b_frozen_data usage. | |
396 | */ | |
397 | jh_in->b_frozen_triggers = jh_in->b_triggers; | |
470decc6 DK |
398 | } |
399 | ||
400 | /* | |
401 | * Did we need to do an escaping? Now we've done all the | |
402 | * copying, we can finally do so. | |
403 | */ | |
404 | if (do_escape) { | |
405 | mapped_data = kmap_atomic(new_page, KM_USER0); | |
406 | *((unsigned int *)(mapped_data + new_offset)) = 0; | |
407 | kunmap_atomic(mapped_data, KM_USER0); | |
408 | } | |
409 | ||
470decc6 DK |
410 | set_bh_page(new_bh, new_page, new_offset); |
411 | new_jh->b_transaction = NULL; | |
412 | new_bh->b_size = jh2bh(jh_in)->b_size; | |
413 | new_bh->b_bdev = transaction->t_journal->j_dev; | |
414 | new_bh->b_blocknr = blocknr; | |
415 | set_buffer_mapped(new_bh); | |
416 | set_buffer_dirty(new_bh); | |
417 | ||
418 | *jh_out = new_jh; | |
419 | ||
420 | /* | |
421 | * The to-be-written buffer needs to get moved to the io queue, | |
422 | * and the original buffer whose contents we are shadowing or | |
423 | * copying is moved to the transaction's shadow queue. | |
424 | */ | |
425 | JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); | |
96577c43 | 426 | spin_lock(&journal->j_list_lock); |
427 | __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); | |
428 | spin_unlock(&journal->j_list_lock); | |
429 | jbd_unlock_bh_state(bh_in); | |
430 | ||
470decc6 | 431 | JBUFFER_TRACE(new_jh, "file as BJ_IO"); |
f7f4bccb | 432 | jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); |
470decc6 DK |
433 | |
434 | return do_escape | (done_copy_out << 1); | |
435 | } | |
436 | ||
437 | /* | |
438 | * Allocation code for the journal file. Manage the space left in the | |
439 | * journal, so that we can begin checkpointing when appropriate. | |
440 | */ | |
441 | ||
442 | /* | |
f7f4bccb | 443 | * __jbd2_log_space_left: Return the number of free blocks left in the journal. |
470decc6 DK |
444 | * |
445 | * Called with the journal already locked. | |
446 | * | |
447 | * Called under j_state_lock | |
448 | */ | |
449 | ||
f7f4bccb | 450 | int __jbd2_log_space_left(journal_t *journal) |
470decc6 DK |
451 | { |
452 | int left = journal->j_free; | |
453 | ||
a931da6a | 454 | /* assert_spin_locked(&journal->j_state_lock); */ |
470decc6 DK |
455 | |
456 | /* | |
457 | * Be pessimistic here about the number of those free blocks which | |
458 | * might be required for log descriptor control blocks. | |
459 | */ | |
460 | ||
461 | #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ | |
462 | ||
463 | left -= MIN_LOG_RESERVED_BLOCKS; | |
464 | ||
465 | if (left <= 0) | |
466 | return 0; | |
467 | left -= (left >> 3); | |
468 | return left; | |
469 | } | |
470 | ||
471 | /* | |
c88ccea3 | 472 | * Called under j_state_lock. Returns true if a transaction commit was started. |
470decc6 | 473 | */ |
f7f4bccb | 474 | int __jbd2_log_start_commit(journal_t *journal, tid_t target) |
470decc6 DK |
475 | { |
476 | /* | |
477 | * Are we already doing a recent enough commit? | |
478 | */ | |
479 | if (!tid_geq(journal->j_commit_request, target)) { | |
480 | /* | |
481 | * We want a new commit: OK, mark the request and wakup the | |
482 | * commit thread. We do _not_ do the commit ourselves. | |
483 | */ | |
484 | ||
485 | journal->j_commit_request = target; | |
486 | jbd_debug(1, "JBD: requesting commit %d/%d\n", | |
487 | journal->j_commit_request, | |
488 | journal->j_commit_sequence); | |
489 | wake_up(&journal->j_wait_commit); | |
490 | return 1; | |
491 | } | |
492 | return 0; | |
493 | } | |
494 | ||
f7f4bccb | 495 | int jbd2_log_start_commit(journal_t *journal, tid_t tid) |
470decc6 DK |
496 | { |
497 | int ret; | |
498 | ||
a931da6a | 499 | write_lock(&journal->j_state_lock); |
f7f4bccb | 500 | ret = __jbd2_log_start_commit(journal, tid); |
a931da6a | 501 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
502 | return ret; |
503 | } | |
504 | ||
505 | /* | |
506 | * Force and wait upon a commit if the calling process is not within | |
507 | * transaction. This is used for forcing out undo-protected data which contains | |
508 | * bitmaps, when the fs is running out of space. | |
509 | * | |
510 | * We can only force the running transaction if we don't have an active handle; | |
511 | * otherwise, we will deadlock. | |
512 | * | |
513 | * Returns true if a transaction was started. | |
514 | */ | |
f7f4bccb | 515 | int jbd2_journal_force_commit_nested(journal_t *journal) |
470decc6 DK |
516 | { |
517 | transaction_t *transaction = NULL; | |
518 | tid_t tid; | |
519 | ||
a931da6a | 520 | read_lock(&journal->j_state_lock); |
470decc6 DK |
521 | if (journal->j_running_transaction && !current->journal_info) { |
522 | transaction = journal->j_running_transaction; | |
f7f4bccb | 523 | __jbd2_log_start_commit(journal, transaction->t_tid); |
470decc6 DK |
524 | } else if (journal->j_committing_transaction) |
525 | transaction = journal->j_committing_transaction; | |
526 | ||
527 | if (!transaction) { | |
a931da6a | 528 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
529 | return 0; /* Nothing to retry */ |
530 | } | |
531 | ||
532 | tid = transaction->t_tid; | |
a931da6a | 533 | read_unlock(&journal->j_state_lock); |
f7f4bccb | 534 | jbd2_log_wait_commit(journal, tid); |
470decc6 DK |
535 | return 1; |
536 | } | |
537 | ||
538 | /* | |
539 | * Start a commit of the current running transaction (if any). Returns true | |
c88ccea3 JK |
540 | * if a transaction is going to be committed (or is currently already |
541 | * committing), and fills its tid in at *ptid | |
470decc6 | 542 | */ |
f7f4bccb | 543 | int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) |
470decc6 DK |
544 | { |
545 | int ret = 0; | |
546 | ||
a931da6a | 547 | write_lock(&journal->j_state_lock); |
470decc6 DK |
548 | if (journal->j_running_transaction) { |
549 | tid_t tid = journal->j_running_transaction->t_tid; | |
550 | ||
c88ccea3 JK |
551 | __jbd2_log_start_commit(journal, tid); |
552 | /* There's a running transaction and we've just made sure | |
553 | * it's commit has been scheduled. */ | |
554 | if (ptid) | |
470decc6 | 555 | *ptid = tid; |
c88ccea3 JK |
556 | ret = 1; |
557 | } else if (journal->j_committing_transaction) { | |
470decc6 DK |
558 | /* |
559 | * If ext3_write_super() recently started a commit, then we | |
560 | * have to wait for completion of that transaction | |
561 | */ | |
c88ccea3 JK |
562 | if (ptid) |
563 | *ptid = journal->j_committing_transaction->t_tid; | |
470decc6 DK |
564 | ret = 1; |
565 | } | |
a931da6a | 566 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
567 | return ret; |
568 | } | |
569 | ||
570 | /* | |
571 | * Wait for a specified commit to complete. | |
572 | * The caller may not hold the journal lock. | |
573 | */ | |
f7f4bccb | 574 | int jbd2_log_wait_commit(journal_t *journal, tid_t tid) |
470decc6 DK |
575 | { |
576 | int err = 0; | |
577 | ||
a931da6a | 578 | read_lock(&journal->j_state_lock); |
e23291b9 | 579 | #ifdef CONFIG_JBD2_DEBUG |
470decc6 DK |
580 | if (!tid_geq(journal->j_commit_request, tid)) { |
581 | printk(KERN_EMERG | |
582 | "%s: error: j_commit_request=%d, tid=%d\n", | |
329d291f | 583 | __func__, journal->j_commit_request, tid); |
470decc6 | 584 | } |
470decc6 | 585 | #endif |
470decc6 DK |
586 | while (tid_gt(tid, journal->j_commit_sequence)) { |
587 | jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", | |
588 | tid, journal->j_commit_sequence); | |
589 | wake_up(&journal->j_wait_commit); | |
a931da6a | 590 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
591 | wait_event(journal->j_wait_done_commit, |
592 | !tid_gt(tid, journal->j_commit_sequence)); | |
a931da6a | 593 | read_lock(&journal->j_state_lock); |
470decc6 | 594 | } |
a931da6a | 595 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
596 | |
597 | if (unlikely(is_journal_aborted(journal))) { | |
598 | printk(KERN_EMERG "journal commit I/O error\n"); | |
599 | err = -EIO; | |
600 | } | |
601 | return err; | |
602 | } | |
603 | ||
604 | /* | |
605 | * Log buffer allocation routines: | |
606 | */ | |
607 | ||
18eba7aa | 608 | int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) |
470decc6 DK |
609 | { |
610 | unsigned long blocknr; | |
611 | ||
a931da6a | 612 | write_lock(&journal->j_state_lock); |
470decc6 DK |
613 | J_ASSERT(journal->j_free > 1); |
614 | ||
615 | blocknr = journal->j_head; | |
616 | journal->j_head++; | |
617 | journal->j_free--; | |
618 | if (journal->j_head == journal->j_last) | |
619 | journal->j_head = journal->j_first; | |
a931da6a | 620 | write_unlock(&journal->j_state_lock); |
f7f4bccb | 621 | return jbd2_journal_bmap(journal, blocknr, retp); |
470decc6 DK |
622 | } |
623 | ||
624 | /* | |
625 | * Conversion of logical to physical block numbers for the journal | |
626 | * | |
627 | * On external journals the journal blocks are identity-mapped, so | |
628 | * this is a no-op. If needed, we can use j_blk_offset - everything is | |
629 | * ready. | |
630 | */ | |
f7f4bccb | 631 | int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, |
18eba7aa | 632 | unsigned long long *retp) |
470decc6 DK |
633 | { |
634 | int err = 0; | |
18eba7aa | 635 | unsigned long long ret; |
470decc6 DK |
636 | |
637 | if (journal->j_inode) { | |
638 | ret = bmap(journal->j_inode, blocknr); | |
639 | if (ret) | |
640 | *retp = ret; | |
641 | else { | |
470decc6 DK |
642 | printk(KERN_ALERT "%s: journal block not found " |
643 | "at offset %lu on %s\n", | |
05496769 | 644 | __func__, blocknr, journal->j_devname); |
470decc6 DK |
645 | err = -EIO; |
646 | __journal_abort_soft(journal, err); | |
647 | } | |
648 | } else { | |
649 | *retp = blocknr; /* +journal->j_blk_offset */ | |
650 | } | |
651 | return err; | |
652 | } | |
653 | ||
654 | /* | |
655 | * We play buffer_head aliasing tricks to write data/metadata blocks to | |
656 | * the journal without copying their contents, but for journal | |
657 | * descriptor blocks we do need to generate bona fide buffers. | |
658 | * | |
f7f4bccb | 659 | * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying |
470decc6 DK |
660 | * the buffer's contents they really should run flush_dcache_page(bh->b_page). |
661 | * But we don't bother doing that, so there will be coherency problems with | |
662 | * mmaps of blockdevs which hold live JBD-controlled filesystems. | |
663 | */ | |
f7f4bccb | 664 | struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) |
470decc6 DK |
665 | { |
666 | struct buffer_head *bh; | |
18eba7aa | 667 | unsigned long long blocknr; |
470decc6 DK |
668 | int err; |
669 | ||
f7f4bccb | 670 | err = jbd2_journal_next_log_block(journal, &blocknr); |
470decc6 DK |
671 | |
672 | if (err) | |
673 | return NULL; | |
674 | ||
675 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | |
4b905671 JK |
676 | if (!bh) |
677 | return NULL; | |
470decc6 DK |
678 | lock_buffer(bh); |
679 | memset(bh->b_data, 0, journal->j_blocksize); | |
680 | set_buffer_uptodate(bh); | |
681 | unlock_buffer(bh); | |
682 | BUFFER_TRACE(bh, "return this buffer"); | |
f7f4bccb | 683 | return jbd2_journal_add_journal_head(bh); |
470decc6 DK |
684 | } |
685 | ||
8e85fb3f JL |
686 | struct jbd2_stats_proc_session { |
687 | journal_t *journal; | |
688 | struct transaction_stats_s *stats; | |
689 | int start; | |
690 | int max; | |
691 | }; | |
692 | ||
8e85fb3f JL |
693 | static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) |
694 | { | |
695 | return *pos ? NULL : SEQ_START_TOKEN; | |
696 | } | |
697 | ||
698 | static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) | |
699 | { | |
700 | return NULL; | |
701 | } | |
702 | ||
703 | static int jbd2_seq_info_show(struct seq_file *seq, void *v) | |
704 | { | |
705 | struct jbd2_stats_proc_session *s = seq->private; | |
706 | ||
707 | if (v != SEQ_START_TOKEN) | |
708 | return 0; | |
bf699327 | 709 | seq_printf(seq, "%lu transaction, each up to %u blocks\n", |
8e85fb3f JL |
710 | s->stats->ts_tid, |
711 | s->journal->j_max_transaction_buffers); | |
712 | if (s->stats->ts_tid == 0) | |
713 | return 0; | |
714 | seq_printf(seq, "average: \n %ums waiting for transaction\n", | |
bf699327 | 715 | jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); |
8e85fb3f | 716 | seq_printf(seq, " %ums running transaction\n", |
bf699327 | 717 | jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); |
8e85fb3f | 718 | seq_printf(seq, " %ums transaction was being locked\n", |
bf699327 | 719 | jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); |
8e85fb3f | 720 | seq_printf(seq, " %ums flushing data (in ordered mode)\n", |
bf699327 | 721 | jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); |
8e85fb3f | 722 | seq_printf(seq, " %ums logging transaction\n", |
bf699327 | 723 | jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); |
c225aa57 SHT |
724 | seq_printf(seq, " %lluus average transaction commit time\n", |
725 | div_u64(s->journal->j_average_commit_time, 1000)); | |
8e85fb3f | 726 | seq_printf(seq, " %lu handles per transaction\n", |
bf699327 | 727 | s->stats->run.rs_handle_count / s->stats->ts_tid); |
8e85fb3f | 728 | seq_printf(seq, " %lu blocks per transaction\n", |
bf699327 | 729 | s->stats->run.rs_blocks / s->stats->ts_tid); |
8e85fb3f | 730 | seq_printf(seq, " %lu logged blocks per transaction\n", |
bf699327 | 731 | s->stats->run.rs_blocks_logged / s->stats->ts_tid); |
8e85fb3f JL |
732 | return 0; |
733 | } | |
734 | ||
735 | static void jbd2_seq_info_stop(struct seq_file *seq, void *v) | |
736 | { | |
737 | } | |
738 | ||
88e9d34c | 739 | static const struct seq_operations jbd2_seq_info_ops = { |
8e85fb3f JL |
740 | .start = jbd2_seq_info_start, |
741 | .next = jbd2_seq_info_next, | |
742 | .stop = jbd2_seq_info_stop, | |
743 | .show = jbd2_seq_info_show, | |
744 | }; | |
745 | ||
746 | static int jbd2_seq_info_open(struct inode *inode, struct file *file) | |
747 | { | |
748 | journal_t *journal = PDE(inode)->data; | |
749 | struct jbd2_stats_proc_session *s; | |
750 | int rc, size; | |
751 | ||
752 | s = kmalloc(sizeof(*s), GFP_KERNEL); | |
753 | if (s == NULL) | |
754 | return -ENOMEM; | |
755 | size = sizeof(struct transaction_stats_s); | |
756 | s->stats = kmalloc(size, GFP_KERNEL); | |
757 | if (s->stats == NULL) { | |
758 | kfree(s); | |
759 | return -ENOMEM; | |
760 | } | |
761 | spin_lock(&journal->j_history_lock); | |
762 | memcpy(s->stats, &journal->j_stats, size); | |
763 | s->journal = journal; | |
764 | spin_unlock(&journal->j_history_lock); | |
765 | ||
766 | rc = seq_open(file, &jbd2_seq_info_ops); | |
767 | if (rc == 0) { | |
768 | struct seq_file *m = file->private_data; | |
769 | m->private = s; | |
770 | } else { | |
771 | kfree(s->stats); | |
772 | kfree(s); | |
773 | } | |
774 | return rc; | |
775 | ||
776 | } | |
777 | ||
778 | static int jbd2_seq_info_release(struct inode *inode, struct file *file) | |
779 | { | |
780 | struct seq_file *seq = file->private_data; | |
781 | struct jbd2_stats_proc_session *s = seq->private; | |
782 | kfree(s->stats); | |
783 | kfree(s); | |
784 | return seq_release(inode, file); | |
785 | } | |
786 | ||
828c0950 | 787 | static const struct file_operations jbd2_seq_info_fops = { |
8e85fb3f JL |
788 | .owner = THIS_MODULE, |
789 | .open = jbd2_seq_info_open, | |
790 | .read = seq_read, | |
791 | .llseek = seq_lseek, | |
792 | .release = jbd2_seq_info_release, | |
793 | }; | |
794 | ||
795 | static struct proc_dir_entry *proc_jbd2_stats; | |
796 | ||
797 | static void jbd2_stats_proc_init(journal_t *journal) | |
798 | { | |
05496769 | 799 | journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); |
8e85fb3f | 800 | if (journal->j_proc_entry) { |
79da3664 DL |
801 | proc_create_data("info", S_IRUGO, journal->j_proc_entry, |
802 | &jbd2_seq_info_fops, journal); | |
8e85fb3f JL |
803 | } |
804 | } | |
805 | ||
806 | static void jbd2_stats_proc_exit(journal_t *journal) | |
807 | { | |
8e85fb3f | 808 | remove_proc_entry("info", journal->j_proc_entry); |
05496769 | 809 | remove_proc_entry(journal->j_devname, proc_jbd2_stats); |
8e85fb3f JL |
810 | } |
811 | ||
470decc6 DK |
812 | /* |
813 | * Management for journal control blocks: functions to create and | |
814 | * destroy journal_t structures, and to initialise and read existing | |
815 | * journal blocks from disk. */ | |
816 | ||
817 | /* First: create and setup a journal_t object in memory. We initialise | |
818 | * very few fields yet: that has to wait until we have created the | |
819 | * journal structures from from scratch, or loaded them from disk. */ | |
820 | ||
821 | static journal_t * journal_init_common (void) | |
822 | { | |
823 | journal_t *journal; | |
824 | int err; | |
825 | ||
3ebfdf88 | 826 | journal = kzalloc(sizeof(*journal), GFP_KERNEL); |
470decc6 DK |
827 | if (!journal) |
828 | goto fail; | |
470decc6 DK |
829 | |
830 | init_waitqueue_head(&journal->j_wait_transaction_locked); | |
831 | init_waitqueue_head(&journal->j_wait_logspace); | |
832 | init_waitqueue_head(&journal->j_wait_done_commit); | |
833 | init_waitqueue_head(&journal->j_wait_checkpoint); | |
834 | init_waitqueue_head(&journal->j_wait_commit); | |
835 | init_waitqueue_head(&journal->j_wait_updates); | |
836 | mutex_init(&journal->j_barrier); | |
837 | mutex_init(&journal->j_checkpoint_mutex); | |
838 | spin_lock_init(&journal->j_revoke_lock); | |
839 | spin_lock_init(&journal->j_list_lock); | |
a931da6a | 840 | rwlock_init(&journal->j_state_lock); |
470decc6 | 841 | |
cd02ff0b | 842 | journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); |
30773840 TT |
843 | journal->j_min_batch_time = 0; |
844 | journal->j_max_batch_time = 15000; /* 15ms */ | |
470decc6 DK |
845 | |
846 | /* The journal is marked for error until we succeed with recovery! */ | |
f7f4bccb | 847 | journal->j_flags = JBD2_ABORT; |
470decc6 DK |
848 | |
849 | /* Set up a default-sized revoke table for the new mount. */ | |
f7f4bccb | 850 | err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); |
470decc6 DK |
851 | if (err) { |
852 | kfree(journal); | |
853 | goto fail; | |
854 | } | |
8e85fb3f | 855 | |
bf699327 | 856 | spin_lock_init(&journal->j_history_lock); |
8e85fb3f | 857 | |
470decc6 DK |
858 | return journal; |
859 | fail: | |
860 | return NULL; | |
861 | } | |
862 | ||
f7f4bccb | 863 | /* jbd2_journal_init_dev and jbd2_journal_init_inode: |
470decc6 DK |
864 | * |
865 | * Create a journal structure assigned some fixed set of disk blocks to | |
866 | * the journal. We don't actually touch those disk blocks yet, but we | |
867 | * need to set up all of the mapping information to tell the journaling | |
868 | * system where the journal blocks are. | |
869 | * | |
870 | */ | |
871 | ||
872 | /** | |
5648ba5b | 873 | * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure |
470decc6 DK |
874 | * @bdev: Block device on which to create the journal |
875 | * @fs_dev: Device which hold journalled filesystem for this journal. | |
876 | * @start: Block nr Start of journal. | |
877 | * @len: Length of the journal in blocks. | |
878 | * @blocksize: blocksize of journalling device | |
5648ba5b RD |
879 | * |
880 | * Returns: a newly created journal_t * | |
470decc6 | 881 | * |
f7f4bccb | 882 | * jbd2_journal_init_dev creates a journal which maps a fixed contiguous |
470decc6 DK |
883 | * range of blocks on an arbitrary block device. |
884 | * | |
885 | */ | |
f7f4bccb | 886 | journal_t * jbd2_journal_init_dev(struct block_device *bdev, |
470decc6 | 887 | struct block_device *fs_dev, |
18eba7aa | 888 | unsigned long long start, int len, int blocksize) |
470decc6 DK |
889 | { |
890 | journal_t *journal = journal_init_common(); | |
891 | struct buffer_head *bh; | |
05496769 | 892 | char *p; |
470decc6 DK |
893 | int n; |
894 | ||
895 | if (!journal) | |
896 | return NULL; | |
897 | ||
898 | /* journal descriptor can store up to n blocks -bzzz */ | |
899 | journal->j_blocksize = blocksize; | |
4b905671 | 900 | jbd2_stats_proc_init(journal); |
470decc6 DK |
901 | n = journal->j_blocksize / sizeof(journal_block_tag_t); |
902 | journal->j_wbufsize = n; | |
903 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); | |
904 | if (!journal->j_wbuf) { | |
905 | printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", | |
329d291f | 906 | __func__); |
4b905671 | 907 | goto out_err; |
470decc6 DK |
908 | } |
909 | journal->j_dev = bdev; | |
910 | journal->j_fs_dev = fs_dev; | |
911 | journal->j_blk_offset = start; | |
912 | journal->j_maxlen = len; | |
05496769 TT |
913 | bdevname(journal->j_dev, journal->j_devname); |
914 | p = journal->j_devname; | |
915 | while ((p = strchr(p, '/'))) | |
916 | *p = '!'; | |
470decc6 DK |
917 | |
918 | bh = __getblk(journal->j_dev, start, journal->j_blocksize); | |
4b905671 JK |
919 | if (!bh) { |
920 | printk(KERN_ERR | |
921 | "%s: Cannot get buffer for journal superblock\n", | |
922 | __func__); | |
923 | goto out_err; | |
924 | } | |
470decc6 DK |
925 | journal->j_sb_buffer = bh; |
926 | journal->j_superblock = (journal_superblock_t *)bh->b_data; | |
4b905671 | 927 | |
470decc6 | 928 | return journal; |
4b905671 | 929 | out_err: |
7b02bec0 | 930 | kfree(journal->j_wbuf); |
4b905671 JK |
931 | jbd2_stats_proc_exit(journal); |
932 | kfree(journal); | |
933 | return NULL; | |
470decc6 DK |
934 | } |
935 | ||
936 | /** | |
f7f4bccb | 937 | * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. |
470decc6 DK |
938 | * @inode: An inode to create the journal in |
939 | * | |
f7f4bccb | 940 | * jbd2_journal_init_inode creates a journal which maps an on-disk inode as |
470decc6 DK |
941 | * the journal. The inode must exist already, must support bmap() and |
942 | * must have all data blocks preallocated. | |
943 | */ | |
f7f4bccb | 944 | journal_t * jbd2_journal_init_inode (struct inode *inode) |
470decc6 DK |
945 | { |
946 | struct buffer_head *bh; | |
947 | journal_t *journal = journal_init_common(); | |
05496769 | 948 | char *p; |
470decc6 DK |
949 | int err; |
950 | int n; | |
18eba7aa | 951 | unsigned long long blocknr; |
470decc6 DK |
952 | |
953 | if (!journal) | |
954 | return NULL; | |
955 | ||
956 | journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; | |
957 | journal->j_inode = inode; | |
05496769 TT |
958 | bdevname(journal->j_dev, journal->j_devname); |
959 | p = journal->j_devname; | |
960 | while ((p = strchr(p, '/'))) | |
961 | *p = '!'; | |
962 | p = journal->j_devname + strlen(journal->j_devname); | |
90576c0b | 963 | sprintf(p, "-%lu", journal->j_inode->i_ino); |
470decc6 DK |
964 | jbd_debug(1, |
965 | "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", | |
966 | journal, inode->i_sb->s_id, inode->i_ino, | |
967 | (long long) inode->i_size, | |
968 | inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); | |
969 | ||
970 | journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; | |
971 | journal->j_blocksize = inode->i_sb->s_blocksize; | |
8e85fb3f | 972 | jbd2_stats_proc_init(journal); |
470decc6 DK |
973 | |
974 | /* journal descriptor can store up to n blocks -bzzz */ | |
975 | n = journal->j_blocksize / sizeof(journal_block_tag_t); | |
976 | journal->j_wbufsize = n; | |
977 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); | |
978 | if (!journal->j_wbuf) { | |
979 | printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", | |
329d291f | 980 | __func__); |
4b905671 | 981 | goto out_err; |
470decc6 DK |
982 | } |
983 | ||
f7f4bccb | 984 | err = jbd2_journal_bmap(journal, 0, &blocknr); |
470decc6 DK |
985 | /* If that failed, give up */ |
986 | if (err) { | |
987 | printk(KERN_ERR "%s: Cannnot locate journal superblock\n", | |
329d291f | 988 | __func__); |
4b905671 | 989 | goto out_err; |
470decc6 DK |
990 | } |
991 | ||
992 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | |
4b905671 JK |
993 | if (!bh) { |
994 | printk(KERN_ERR | |
995 | "%s: Cannot get buffer for journal superblock\n", | |
996 | __func__); | |
997 | goto out_err; | |
998 | } | |
470decc6 DK |
999 | journal->j_sb_buffer = bh; |
1000 | journal->j_superblock = (journal_superblock_t *)bh->b_data; | |
1001 | ||
1002 | return journal; | |
4b905671 | 1003 | out_err: |
7b02bec0 | 1004 | kfree(journal->j_wbuf); |
4b905671 JK |
1005 | jbd2_stats_proc_exit(journal); |
1006 | kfree(journal); | |
1007 | return NULL; | |
470decc6 DK |
1008 | } |
1009 | ||
1010 | /* | |
1011 | * If the journal init or create aborts, we need to mark the journal | |
1012 | * superblock as being NULL to prevent the journal destroy from writing | |
1013 | * back a bogus superblock. | |
1014 | */ | |
1015 | static void journal_fail_superblock (journal_t *journal) | |
1016 | { | |
1017 | struct buffer_head *bh = journal->j_sb_buffer; | |
1018 | brelse(bh); | |
1019 | journal->j_sb_buffer = NULL; | |
1020 | } | |
1021 | ||
1022 | /* | |
1023 | * Given a journal_t structure, initialise the various fields for | |
1024 | * startup of a new journaling session. We use this both when creating | |
1025 | * a journal, and after recovering an old journal to reset it for | |
1026 | * subsequent use. | |
1027 | */ | |
1028 | ||
1029 | static int journal_reset(journal_t *journal) | |
1030 | { | |
1031 | journal_superblock_t *sb = journal->j_superblock; | |
18eba7aa | 1032 | unsigned long long first, last; |
470decc6 DK |
1033 | |
1034 | first = be32_to_cpu(sb->s_first); | |
1035 | last = be32_to_cpu(sb->s_maxlen); | |
f6f50e28 JK |
1036 | if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { |
1037 | printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n", | |
1038 | first, last); | |
1039 | journal_fail_superblock(journal); | |
1040 | return -EINVAL; | |
1041 | } | |
470decc6 DK |
1042 | |
1043 | journal->j_first = first; | |
1044 | journal->j_last = last; | |
1045 | ||
1046 | journal->j_head = first; | |
1047 | journal->j_tail = first; | |
1048 | journal->j_free = last - first; | |
1049 | ||
1050 | journal->j_tail_sequence = journal->j_transaction_sequence; | |
1051 | journal->j_commit_sequence = journal->j_transaction_sequence - 1; | |
1052 | journal->j_commit_request = journal->j_commit_sequence; | |
1053 | ||
1054 | journal->j_max_transaction_buffers = journal->j_maxlen / 4; | |
1055 | ||
1056 | /* Add the dynamic fields and write it to disk. */ | |
f7f4bccb | 1057 | jbd2_journal_update_superblock(journal, 1); |
97f06784 | 1058 | return jbd2_journal_start_thread(journal); |
470decc6 DK |
1059 | } |
1060 | ||
470decc6 | 1061 | /** |
f7f4bccb | 1062 | * void jbd2_journal_update_superblock() - Update journal sb on disk. |
470decc6 DK |
1063 | * @journal: The journal to update. |
1064 | * @wait: Set to '0' if you don't want to wait for IO completion. | |
1065 | * | |
1066 | * Update a journal's dynamic superblock fields and write it to disk, | |
1067 | * optionally waiting for the IO to complete. | |
1068 | */ | |
f7f4bccb | 1069 | void jbd2_journal_update_superblock(journal_t *journal, int wait) |
470decc6 DK |
1070 | { |
1071 | journal_superblock_t *sb = journal->j_superblock; | |
1072 | struct buffer_head *bh = journal->j_sb_buffer; | |
1073 | ||
1074 | /* | |
1075 | * As a special case, if the on-disk copy is already marked as needing | |
1076 | * no recovery (s_start == 0) and there are no outstanding transactions | |
1077 | * in the filesystem, then we can safely defer the superblock update | |
f7f4bccb | 1078 | * until the next commit by setting JBD2_FLUSHED. This avoids |
470decc6 DK |
1079 | * attempting a write to a potential-readonly device. |
1080 | */ | |
1081 | if (sb->s_start == 0 && journal->j_tail_sequence == | |
1082 | journal->j_transaction_sequence) { | |
1083 | jbd_debug(1,"JBD: Skipping superblock update on recovered sb " | |
1084 | "(start %ld, seq %d, errno %d)\n", | |
1085 | journal->j_tail, journal->j_tail_sequence, | |
1086 | journal->j_errno); | |
1087 | goto out; | |
1088 | } | |
1089 | ||
914258bf TT |
1090 | if (buffer_write_io_error(bh)) { |
1091 | /* | |
1092 | * Oh, dear. A previous attempt to write the journal | |
1093 | * superblock failed. This could happen because the | |
1094 | * USB device was yanked out. Or it could happen to | |
1095 | * be a transient write error and maybe the block will | |
1096 | * be remapped. Nothing we can do but to retry the | |
1097 | * write and hope for the best. | |
1098 | */ | |
1099 | printk(KERN_ERR "JBD2: previous I/O error detected " | |
1100 | "for journal superblock update for %s.\n", | |
1101 | journal->j_devname); | |
1102 | clear_buffer_write_io_error(bh); | |
1103 | set_buffer_uptodate(bh); | |
1104 | } | |
1105 | ||
a931da6a | 1106 | read_lock(&journal->j_state_lock); |
470decc6 DK |
1107 | jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", |
1108 | journal->j_tail, journal->j_tail_sequence, journal->j_errno); | |
1109 | ||
1110 | sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); | |
1111 | sb->s_start = cpu_to_be32(journal->j_tail); | |
1112 | sb->s_errno = cpu_to_be32(journal->j_errno); | |
a931da6a | 1113 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
1114 | |
1115 | BUFFER_TRACE(bh, "marking dirty"); | |
1116 | mark_buffer_dirty(bh); | |
914258bf | 1117 | if (wait) { |
470decc6 | 1118 | sync_dirty_buffer(bh); |
914258bf TT |
1119 | if (buffer_write_io_error(bh)) { |
1120 | printk(KERN_ERR "JBD2: I/O error detected " | |
1121 | "when updating journal superblock for %s.\n", | |
1122 | journal->j_devname); | |
1123 | clear_buffer_write_io_error(bh); | |
1124 | set_buffer_uptodate(bh); | |
1125 | } | |
1126 | } else | |
470decc6 DK |
1127 | ll_rw_block(SWRITE, 1, &bh); |
1128 | ||
1129 | out: | |
1130 | /* If we have just flushed the log (by marking s_start==0), then | |
1131 | * any future commit will have to be careful to update the | |
1132 | * superblock again to re-record the true start of the log. */ | |
1133 | ||
a931da6a | 1134 | write_lock(&journal->j_state_lock); |
470decc6 | 1135 | if (sb->s_start) |
f7f4bccb | 1136 | journal->j_flags &= ~JBD2_FLUSHED; |
470decc6 | 1137 | else |
f7f4bccb | 1138 | journal->j_flags |= JBD2_FLUSHED; |
a931da6a | 1139 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
1140 | } |
1141 | ||
1142 | /* | |
1143 | * Read the superblock for a given journal, performing initial | |
1144 | * validation of the format. | |
1145 | */ | |
1146 | ||
1147 | static int journal_get_superblock(journal_t *journal) | |
1148 | { | |
1149 | struct buffer_head *bh; | |
1150 | journal_superblock_t *sb; | |
1151 | int err = -EIO; | |
1152 | ||
1153 | bh = journal->j_sb_buffer; | |
1154 | ||
1155 | J_ASSERT(bh != NULL); | |
1156 | if (!buffer_uptodate(bh)) { | |
1157 | ll_rw_block(READ, 1, &bh); | |
1158 | wait_on_buffer(bh); | |
1159 | if (!buffer_uptodate(bh)) { | |
1160 | printk (KERN_ERR | |
1161 | "JBD: IO error reading journal superblock\n"); | |
1162 | goto out; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | sb = journal->j_superblock; | |
1167 | ||
1168 | err = -EINVAL; | |
1169 | ||
f7f4bccb | 1170 | if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || |
470decc6 DK |
1171 | sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { |
1172 | printk(KERN_WARNING "JBD: no valid journal superblock found\n"); | |
1173 | goto out; | |
1174 | } | |
1175 | ||
1176 | switch(be32_to_cpu(sb->s_header.h_blocktype)) { | |
f7f4bccb | 1177 | case JBD2_SUPERBLOCK_V1: |
470decc6 DK |
1178 | journal->j_format_version = 1; |
1179 | break; | |
f7f4bccb | 1180 | case JBD2_SUPERBLOCK_V2: |
470decc6 DK |
1181 | journal->j_format_version = 2; |
1182 | break; | |
1183 | default: | |
1184 | printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); | |
1185 | goto out; | |
1186 | } | |
1187 | ||
1188 | if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) | |
1189 | journal->j_maxlen = be32_to_cpu(sb->s_maxlen); | |
1190 | else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { | |
1191 | printk (KERN_WARNING "JBD: journal file too short\n"); | |
1192 | goto out; | |
1193 | } | |
1194 | ||
1195 | return 0; | |
1196 | ||
1197 | out: | |
1198 | journal_fail_superblock(journal); | |
1199 | return err; | |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * Load the on-disk journal superblock and read the key fields into the | |
1204 | * journal_t. | |
1205 | */ | |
1206 | ||
1207 | static int load_superblock(journal_t *journal) | |
1208 | { | |
1209 | int err; | |
1210 | journal_superblock_t *sb; | |
1211 | ||
1212 | err = journal_get_superblock(journal); | |
1213 | if (err) | |
1214 | return err; | |
1215 | ||
1216 | sb = journal->j_superblock; | |
1217 | ||
1218 | journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); | |
1219 | journal->j_tail = be32_to_cpu(sb->s_start); | |
1220 | journal->j_first = be32_to_cpu(sb->s_first); | |
1221 | journal->j_last = be32_to_cpu(sb->s_maxlen); | |
1222 | journal->j_errno = be32_to_cpu(sb->s_errno); | |
1223 | ||
1224 | return 0; | |
1225 | } | |
1226 | ||
1227 | ||
1228 | /** | |
f7f4bccb | 1229 | * int jbd2_journal_load() - Read journal from disk. |
470decc6 DK |
1230 | * @journal: Journal to act on. |
1231 | * | |
1232 | * Given a journal_t structure which tells us which disk blocks contain | |
1233 | * a journal, read the journal from disk to initialise the in-memory | |
1234 | * structures. | |
1235 | */ | |
f7f4bccb | 1236 | int jbd2_journal_load(journal_t *journal) |
470decc6 DK |
1237 | { |
1238 | int err; | |
1239 | journal_superblock_t *sb; | |
1240 | ||
1241 | err = load_superblock(journal); | |
1242 | if (err) | |
1243 | return err; | |
1244 | ||
1245 | sb = journal->j_superblock; | |
1246 | /* If this is a V2 superblock, then we have to check the | |
1247 | * features flags on it. */ | |
1248 | ||
1249 | if (journal->j_format_version >= 2) { | |
1250 | if ((sb->s_feature_ro_compat & | |
f7f4bccb | 1251 | ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || |
470decc6 | 1252 | (sb->s_feature_incompat & |
f7f4bccb | 1253 | ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { |
470decc6 DK |
1254 | printk (KERN_WARNING |
1255 | "JBD: Unrecognised features on journal\n"); | |
1256 | return -EINVAL; | |
1257 | } | |
1258 | } | |
1259 | ||
d2eecb03 TT |
1260 | /* |
1261 | * Create a slab for this blocksize | |
1262 | */ | |
1263 | err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); | |
1264 | if (err) | |
1265 | return err; | |
1266 | ||
470decc6 DK |
1267 | /* Let the recovery code check whether it needs to recover any |
1268 | * data from the journal. */ | |
f7f4bccb | 1269 | if (jbd2_journal_recover(journal)) |
470decc6 DK |
1270 | goto recovery_error; |
1271 | ||
e6a47428 TT |
1272 | if (journal->j_failed_commit) { |
1273 | printk(KERN_ERR "JBD2: journal transaction %u on %s " | |
1274 | "is corrupt.\n", journal->j_failed_commit, | |
1275 | journal->j_devname); | |
1276 | return -EIO; | |
1277 | } | |
1278 | ||
470decc6 DK |
1279 | /* OK, we've finished with the dynamic journal bits: |
1280 | * reinitialise the dynamic contents of the superblock in memory | |
1281 | * and reset them on disk. */ | |
1282 | if (journal_reset(journal)) | |
1283 | goto recovery_error; | |
1284 | ||
f7f4bccb MC |
1285 | journal->j_flags &= ~JBD2_ABORT; |
1286 | journal->j_flags |= JBD2_LOADED; | |
470decc6 DK |
1287 | return 0; |
1288 | ||
1289 | recovery_error: | |
1290 | printk (KERN_WARNING "JBD: recovery failed\n"); | |
1291 | return -EIO; | |
1292 | } | |
1293 | ||
1294 | /** | |
f7f4bccb | 1295 | * void jbd2_journal_destroy() - Release a journal_t structure. |
470decc6 DK |
1296 | * @journal: Journal to act on. |
1297 | * | |
1298 | * Release a journal_t structure once it is no longer in use by the | |
1299 | * journaled object. | |
44519faf | 1300 | * Return <0 if we couldn't clean up the journal. |
470decc6 | 1301 | */ |
44519faf | 1302 | int jbd2_journal_destroy(journal_t *journal) |
470decc6 | 1303 | { |
44519faf HK |
1304 | int err = 0; |
1305 | ||
470decc6 DK |
1306 | /* Wait for the commit thread to wake up and die. */ |
1307 | journal_kill_thread(journal); | |
1308 | ||
1309 | /* Force a final log commit */ | |
1310 | if (journal->j_running_transaction) | |
f7f4bccb | 1311 | jbd2_journal_commit_transaction(journal); |
470decc6 DK |
1312 | |
1313 | /* Force any old transactions to disk */ | |
1314 | ||
1315 | /* Totally anal locking here... */ | |
1316 | spin_lock(&journal->j_list_lock); | |
1317 | while (journal->j_checkpoint_transactions != NULL) { | |
1318 | spin_unlock(&journal->j_list_lock); | |
1a0d3786 | 1319 | mutex_lock(&journal->j_checkpoint_mutex); |
f7f4bccb | 1320 | jbd2_log_do_checkpoint(journal); |
1a0d3786 | 1321 | mutex_unlock(&journal->j_checkpoint_mutex); |
470decc6 DK |
1322 | spin_lock(&journal->j_list_lock); |
1323 | } | |
1324 | ||
1325 | J_ASSERT(journal->j_running_transaction == NULL); | |
1326 | J_ASSERT(journal->j_committing_transaction == NULL); | |
1327 | J_ASSERT(journal->j_checkpoint_transactions == NULL); | |
1328 | spin_unlock(&journal->j_list_lock); | |
1329 | ||
470decc6 | 1330 | if (journal->j_sb_buffer) { |
44519faf HK |
1331 | if (!is_journal_aborted(journal)) { |
1332 | /* We can now mark the journal as empty. */ | |
1333 | journal->j_tail = 0; | |
1334 | journal->j_tail_sequence = | |
1335 | ++journal->j_transaction_sequence; | |
1336 | jbd2_journal_update_superblock(journal, 1); | |
1337 | } else { | |
1338 | err = -EIO; | |
1339 | } | |
470decc6 DK |
1340 | brelse(journal->j_sb_buffer); |
1341 | } | |
1342 | ||
8e85fb3f JL |
1343 | if (journal->j_proc_entry) |
1344 | jbd2_stats_proc_exit(journal); | |
470decc6 DK |
1345 | if (journal->j_inode) |
1346 | iput(journal->j_inode); | |
1347 | if (journal->j_revoke) | |
f7f4bccb | 1348 | jbd2_journal_destroy_revoke(journal); |
470decc6 DK |
1349 | kfree(journal->j_wbuf); |
1350 | kfree(journal); | |
44519faf HK |
1351 | |
1352 | return err; | |
470decc6 DK |
1353 | } |
1354 | ||
1355 | ||
1356 | /** | |
f7f4bccb | 1357 | *int jbd2_journal_check_used_features () - Check if features specified are used. |
470decc6 DK |
1358 | * @journal: Journal to check. |
1359 | * @compat: bitmask of compatible features | |
1360 | * @ro: bitmask of features that force read-only mount | |
1361 | * @incompat: bitmask of incompatible features | |
1362 | * | |
1363 | * Check whether the journal uses all of a given set of | |
1364 | * features. Return true (non-zero) if it does. | |
1365 | **/ | |
1366 | ||
f7f4bccb | 1367 | int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, |
470decc6 DK |
1368 | unsigned long ro, unsigned long incompat) |
1369 | { | |
1370 | journal_superblock_t *sb; | |
1371 | ||
1372 | if (!compat && !ro && !incompat) | |
1373 | return 1; | |
1374 | if (journal->j_format_version == 1) | |
1375 | return 0; | |
1376 | ||
1377 | sb = journal->j_superblock; | |
1378 | ||
1379 | if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && | |
1380 | ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && | |
1381 | ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) | |
1382 | return 1; | |
1383 | ||
1384 | return 0; | |
1385 | } | |
1386 | ||
1387 | /** | |
f7f4bccb | 1388 | * int jbd2_journal_check_available_features() - Check feature set in journalling layer |
470decc6 DK |
1389 | * @journal: Journal to check. |
1390 | * @compat: bitmask of compatible features | |
1391 | * @ro: bitmask of features that force read-only mount | |
1392 | * @incompat: bitmask of incompatible features | |
1393 | * | |
1394 | * Check whether the journaling code supports the use of | |
1395 | * all of a given set of features on this journal. Return true | |
1396 | * (non-zero) if it can. */ | |
1397 | ||
f7f4bccb | 1398 | int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, |
470decc6 DK |
1399 | unsigned long ro, unsigned long incompat) |
1400 | { | |
470decc6 DK |
1401 | if (!compat && !ro && !incompat) |
1402 | return 1; | |
1403 | ||
470decc6 DK |
1404 | /* We can support any known requested features iff the |
1405 | * superblock is in version 2. Otherwise we fail to support any | |
1406 | * extended sb features. */ | |
1407 | ||
1408 | if (journal->j_format_version != 2) | |
1409 | return 0; | |
1410 | ||
f7f4bccb MC |
1411 | if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && |
1412 | (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && | |
1413 | (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) | |
470decc6 DK |
1414 | return 1; |
1415 | ||
1416 | return 0; | |
1417 | } | |
1418 | ||
1419 | /** | |
f7f4bccb | 1420 | * int jbd2_journal_set_features () - Mark a given journal feature in the superblock |
470decc6 DK |
1421 | * @journal: Journal to act on. |
1422 | * @compat: bitmask of compatible features | |
1423 | * @ro: bitmask of features that force read-only mount | |
1424 | * @incompat: bitmask of incompatible features | |
1425 | * | |
1426 | * Mark a given journal feature as present on the | |
1427 | * superblock. Returns true if the requested features could be set. | |
1428 | * | |
1429 | */ | |
1430 | ||
f7f4bccb | 1431 | int jbd2_journal_set_features (journal_t *journal, unsigned long compat, |
470decc6 DK |
1432 | unsigned long ro, unsigned long incompat) |
1433 | { | |
1434 | journal_superblock_t *sb; | |
1435 | ||
f7f4bccb | 1436 | if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) |
470decc6 DK |
1437 | return 1; |
1438 | ||
f7f4bccb | 1439 | if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) |
470decc6 DK |
1440 | return 0; |
1441 | ||
1442 | jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", | |
1443 | compat, ro, incompat); | |
1444 | ||
1445 | sb = journal->j_superblock; | |
1446 | ||
1447 | sb->s_feature_compat |= cpu_to_be32(compat); | |
1448 | sb->s_feature_ro_compat |= cpu_to_be32(ro); | |
1449 | sb->s_feature_incompat |= cpu_to_be32(incompat); | |
1450 | ||
1451 | return 1; | |
1452 | } | |
1453 | ||
818d276c GS |
1454 | /* |
1455 | * jbd2_journal_clear_features () - Clear a given journal feature in the | |
1456 | * superblock | |
1457 | * @journal: Journal to act on. | |
1458 | * @compat: bitmask of compatible features | |
1459 | * @ro: bitmask of features that force read-only mount | |
1460 | * @incompat: bitmask of incompatible features | |
1461 | * | |
1462 | * Clear a given journal feature as present on the | |
1463 | * superblock. | |
1464 | */ | |
1465 | void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, | |
1466 | unsigned long ro, unsigned long incompat) | |
1467 | { | |
1468 | journal_superblock_t *sb; | |
1469 | ||
1470 | jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", | |
1471 | compat, ro, incompat); | |
1472 | ||
1473 | sb = journal->j_superblock; | |
1474 | ||
1475 | sb->s_feature_compat &= ~cpu_to_be32(compat); | |
1476 | sb->s_feature_ro_compat &= ~cpu_to_be32(ro); | |
1477 | sb->s_feature_incompat &= ~cpu_to_be32(incompat); | |
1478 | } | |
1479 | EXPORT_SYMBOL(jbd2_journal_clear_features); | |
470decc6 DK |
1480 | |
1481 | /** | |
f7f4bccb | 1482 | * int jbd2_journal_update_format () - Update on-disk journal structure. |
470decc6 DK |
1483 | * @journal: Journal to act on. |
1484 | * | |
1485 | * Given an initialised but unloaded journal struct, poke about in the | |
1486 | * on-disk structure to update it to the most recent supported version. | |
1487 | */ | |
f7f4bccb | 1488 | int jbd2_journal_update_format (journal_t *journal) |
470decc6 DK |
1489 | { |
1490 | journal_superblock_t *sb; | |
1491 | int err; | |
1492 | ||
1493 | err = journal_get_superblock(journal); | |
1494 | if (err) | |
1495 | return err; | |
1496 | ||
1497 | sb = journal->j_superblock; | |
1498 | ||
1499 | switch (be32_to_cpu(sb->s_header.h_blocktype)) { | |
f7f4bccb | 1500 | case JBD2_SUPERBLOCK_V2: |
470decc6 | 1501 | return 0; |
f7f4bccb | 1502 | case JBD2_SUPERBLOCK_V1: |
470decc6 DK |
1503 | return journal_convert_superblock_v1(journal, sb); |
1504 | default: | |
1505 | break; | |
1506 | } | |
1507 | return -EINVAL; | |
1508 | } | |
1509 | ||
1510 | static int journal_convert_superblock_v1(journal_t *journal, | |
1511 | journal_superblock_t *sb) | |
1512 | { | |
1513 | int offset, blocksize; | |
1514 | struct buffer_head *bh; | |
1515 | ||
1516 | printk(KERN_WARNING | |
1517 | "JBD: Converting superblock from version 1 to 2.\n"); | |
1518 | ||
1519 | /* Pre-initialise new fields to zero */ | |
1520 | offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); | |
1521 | blocksize = be32_to_cpu(sb->s_blocksize); | |
1522 | memset(&sb->s_feature_compat, 0, blocksize-offset); | |
1523 | ||
1524 | sb->s_nr_users = cpu_to_be32(1); | |
f7f4bccb | 1525 | sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); |
470decc6 DK |
1526 | journal->j_format_version = 2; |
1527 | ||
1528 | bh = journal->j_sb_buffer; | |
1529 | BUFFER_TRACE(bh, "marking dirty"); | |
1530 | mark_buffer_dirty(bh); | |
1531 | sync_dirty_buffer(bh); | |
1532 | return 0; | |
1533 | } | |
1534 | ||
1535 | ||
1536 | /** | |
f7f4bccb | 1537 | * int jbd2_journal_flush () - Flush journal |
470decc6 DK |
1538 | * @journal: Journal to act on. |
1539 | * | |
1540 | * Flush all data for a given journal to disk and empty the journal. | |
1541 | * Filesystems can use this when remounting readonly to ensure that | |
1542 | * recovery does not need to happen on remount. | |
1543 | */ | |
1544 | ||
f7f4bccb | 1545 | int jbd2_journal_flush(journal_t *journal) |
470decc6 DK |
1546 | { |
1547 | int err = 0; | |
1548 | transaction_t *transaction = NULL; | |
1549 | unsigned long old_tail; | |
1550 | ||
a931da6a | 1551 | write_lock(&journal->j_state_lock); |
470decc6 DK |
1552 | |
1553 | /* Force everything buffered to the log... */ | |
1554 | if (journal->j_running_transaction) { | |
1555 | transaction = journal->j_running_transaction; | |
f7f4bccb | 1556 | __jbd2_log_start_commit(journal, transaction->t_tid); |
470decc6 DK |
1557 | } else if (journal->j_committing_transaction) |
1558 | transaction = journal->j_committing_transaction; | |
1559 | ||
1560 | /* Wait for the log commit to complete... */ | |
1561 | if (transaction) { | |
1562 | tid_t tid = transaction->t_tid; | |
1563 | ||
a931da6a | 1564 | write_unlock(&journal->j_state_lock); |
f7f4bccb | 1565 | jbd2_log_wait_commit(journal, tid); |
470decc6 | 1566 | } else { |
a931da6a | 1567 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
1568 | } |
1569 | ||
1570 | /* ...and flush everything in the log out to disk. */ | |
1571 | spin_lock(&journal->j_list_lock); | |
1572 | while (!err && journal->j_checkpoint_transactions != NULL) { | |
1573 | spin_unlock(&journal->j_list_lock); | |
44519faf | 1574 | mutex_lock(&journal->j_checkpoint_mutex); |
f7f4bccb | 1575 | err = jbd2_log_do_checkpoint(journal); |
44519faf | 1576 | mutex_unlock(&journal->j_checkpoint_mutex); |
470decc6 DK |
1577 | spin_lock(&journal->j_list_lock); |
1578 | } | |
1579 | spin_unlock(&journal->j_list_lock); | |
44519faf HK |
1580 | |
1581 | if (is_journal_aborted(journal)) | |
1582 | return -EIO; | |
1583 | ||
f7f4bccb | 1584 | jbd2_cleanup_journal_tail(journal); |
470decc6 DK |
1585 | |
1586 | /* Finally, mark the journal as really needing no recovery. | |
1587 | * This sets s_start==0 in the underlying superblock, which is | |
1588 | * the magic code for a fully-recovered superblock. Any future | |
1589 | * commits of data to the journal will restore the current | |
1590 | * s_start value. */ | |
a931da6a | 1591 | write_lock(&journal->j_state_lock); |
470decc6 DK |
1592 | old_tail = journal->j_tail; |
1593 | journal->j_tail = 0; | |
a931da6a | 1594 | write_unlock(&journal->j_state_lock); |
f7f4bccb | 1595 | jbd2_journal_update_superblock(journal, 1); |
a931da6a | 1596 | write_lock(&journal->j_state_lock); |
470decc6 DK |
1597 | journal->j_tail = old_tail; |
1598 | ||
1599 | J_ASSERT(!journal->j_running_transaction); | |
1600 | J_ASSERT(!journal->j_committing_transaction); | |
1601 | J_ASSERT(!journal->j_checkpoint_transactions); | |
1602 | J_ASSERT(journal->j_head == journal->j_tail); | |
1603 | J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); | |
a931da6a | 1604 | write_unlock(&journal->j_state_lock); |
44519faf | 1605 | return 0; |
470decc6 DK |
1606 | } |
1607 | ||
1608 | /** | |
f7f4bccb | 1609 | * int jbd2_journal_wipe() - Wipe journal contents |
470decc6 DK |
1610 | * @journal: Journal to act on. |
1611 | * @write: flag (see below) | |
1612 | * | |
1613 | * Wipe out all of the contents of a journal, safely. This will produce | |
1614 | * a warning if the journal contains any valid recovery information. | |
f7f4bccb | 1615 | * Must be called between journal_init_*() and jbd2_journal_load(). |
470decc6 DK |
1616 | * |
1617 | * If 'write' is non-zero, then we wipe out the journal on disk; otherwise | |
1618 | * we merely suppress recovery. | |
1619 | */ | |
1620 | ||
f7f4bccb | 1621 | int jbd2_journal_wipe(journal_t *journal, int write) |
470decc6 | 1622 | { |
470decc6 DK |
1623 | int err = 0; |
1624 | ||
f7f4bccb | 1625 | J_ASSERT (!(journal->j_flags & JBD2_LOADED)); |
470decc6 DK |
1626 | |
1627 | err = load_superblock(journal); | |
1628 | if (err) | |
1629 | return err; | |
1630 | ||
470decc6 DK |
1631 | if (!journal->j_tail) |
1632 | goto no_recovery; | |
1633 | ||
1634 | printk (KERN_WARNING "JBD: %s recovery information on journal\n", | |
1635 | write ? "Clearing" : "Ignoring"); | |
1636 | ||
f7f4bccb | 1637 | err = jbd2_journal_skip_recovery(journal); |
470decc6 | 1638 | if (write) |
f7f4bccb | 1639 | jbd2_journal_update_superblock(journal, 1); |
470decc6 DK |
1640 | |
1641 | no_recovery: | |
1642 | return err; | |
1643 | } | |
1644 | ||
470decc6 DK |
1645 | /* |
1646 | * Journal abort has very specific semantics, which we describe | |
1647 | * for journal abort. | |
1648 | * | |
bfcd3555 | 1649 | * Two internal functions, which provide abort to the jbd layer |
470decc6 DK |
1650 | * itself are here. |
1651 | */ | |
1652 | ||
1653 | /* | |
1654 | * Quick version for internal journal use (doesn't lock the journal). | |
1655 | * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, | |
1656 | * and don't attempt to make any other journal updates. | |
1657 | */ | |
f7f4bccb | 1658 | void __jbd2_journal_abort_hard(journal_t *journal) |
470decc6 DK |
1659 | { |
1660 | transaction_t *transaction; | |
470decc6 | 1661 | |
f7f4bccb | 1662 | if (journal->j_flags & JBD2_ABORT) |
470decc6 DK |
1663 | return; |
1664 | ||
1665 | printk(KERN_ERR "Aborting journal on device %s.\n", | |
05496769 | 1666 | journal->j_devname); |
470decc6 | 1667 | |
a931da6a | 1668 | write_lock(&journal->j_state_lock); |
f7f4bccb | 1669 | journal->j_flags |= JBD2_ABORT; |
470decc6 DK |
1670 | transaction = journal->j_running_transaction; |
1671 | if (transaction) | |
f7f4bccb | 1672 | __jbd2_log_start_commit(journal, transaction->t_tid); |
a931da6a | 1673 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
1674 | } |
1675 | ||
1676 | /* Soft abort: record the abort error status in the journal superblock, | |
1677 | * but don't do any other IO. */ | |
1678 | static void __journal_abort_soft (journal_t *journal, int errno) | |
1679 | { | |
f7f4bccb | 1680 | if (journal->j_flags & JBD2_ABORT) |
470decc6 DK |
1681 | return; |
1682 | ||
1683 | if (!journal->j_errno) | |
1684 | journal->j_errno = errno; | |
1685 | ||
f7f4bccb | 1686 | __jbd2_journal_abort_hard(journal); |
470decc6 DK |
1687 | |
1688 | if (errno) | |
f7f4bccb | 1689 | jbd2_journal_update_superblock(journal, 1); |
470decc6 DK |
1690 | } |
1691 | ||
1692 | /** | |
f7f4bccb | 1693 | * void jbd2_journal_abort () - Shutdown the journal immediately. |
470decc6 DK |
1694 | * @journal: the journal to shutdown. |
1695 | * @errno: an error number to record in the journal indicating | |
1696 | * the reason for the shutdown. | |
1697 | * | |
1698 | * Perform a complete, immediate shutdown of the ENTIRE | |
1699 | * journal (not of a single transaction). This operation cannot be | |
1700 | * undone without closing and reopening the journal. | |
1701 | * | |
f7f4bccb | 1702 | * The jbd2_journal_abort function is intended to support higher level error |
470decc6 DK |
1703 | * recovery mechanisms such as the ext2/ext3 remount-readonly error |
1704 | * mode. | |
1705 | * | |
1706 | * Journal abort has very specific semantics. Any existing dirty, | |
1707 | * unjournaled buffers in the main filesystem will still be written to | |
1708 | * disk by bdflush, but the journaling mechanism will be suspended | |
1709 | * immediately and no further transaction commits will be honoured. | |
1710 | * | |
1711 | * Any dirty, journaled buffers will be written back to disk without | |
1712 | * hitting the journal. Atomicity cannot be guaranteed on an aborted | |
1713 | * filesystem, but we _do_ attempt to leave as much data as possible | |
1714 | * behind for fsck to use for cleanup. | |
1715 | * | |
1716 | * Any attempt to get a new transaction handle on a journal which is in | |
1717 | * ABORT state will just result in an -EROFS error return. A | |
f7f4bccb | 1718 | * jbd2_journal_stop on an existing handle will return -EIO if we have |
470decc6 DK |
1719 | * entered abort state during the update. |
1720 | * | |
1721 | * Recursive transactions are not disturbed by journal abort until the | |
f7f4bccb | 1722 | * final jbd2_journal_stop, which will receive the -EIO error. |
470decc6 | 1723 | * |
f7f4bccb | 1724 | * Finally, the jbd2_journal_abort call allows the caller to supply an errno |
470decc6 DK |
1725 | * which will be recorded (if possible) in the journal superblock. This |
1726 | * allows a client to record failure conditions in the middle of a | |
1727 | * transaction without having to complete the transaction to record the | |
1728 | * failure to disk. ext3_error, for example, now uses this | |
1729 | * functionality. | |
1730 | * | |
1731 | * Errors which originate from within the journaling layer will NOT | |
1732 | * supply an errno; a null errno implies that absolutely no further | |
1733 | * writes are done to the journal (unless there are any already in | |
1734 | * progress). | |
1735 | * | |
1736 | */ | |
1737 | ||
f7f4bccb | 1738 | void jbd2_journal_abort(journal_t *journal, int errno) |
470decc6 DK |
1739 | { |
1740 | __journal_abort_soft(journal, errno); | |
1741 | } | |
1742 | ||
1743 | /** | |
f7f4bccb | 1744 | * int jbd2_journal_errno () - returns the journal's error state. |
470decc6 DK |
1745 | * @journal: journal to examine. |
1746 | * | |
bfcd3555 | 1747 | * This is the errno number set with jbd2_journal_abort(), the last |
470decc6 DK |
1748 | * time the journal was mounted - if the journal was stopped |
1749 | * without calling abort this will be 0. | |
1750 | * | |
1751 | * If the journal has been aborted on this mount time -EROFS will | |
1752 | * be returned. | |
1753 | */ | |
f7f4bccb | 1754 | int jbd2_journal_errno(journal_t *journal) |
470decc6 DK |
1755 | { |
1756 | int err; | |
1757 | ||
a931da6a | 1758 | read_lock(&journal->j_state_lock); |
f7f4bccb | 1759 | if (journal->j_flags & JBD2_ABORT) |
470decc6 DK |
1760 | err = -EROFS; |
1761 | else | |
1762 | err = journal->j_errno; | |
a931da6a | 1763 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
1764 | return err; |
1765 | } | |
1766 | ||
1767 | /** | |
f7f4bccb | 1768 | * int jbd2_journal_clear_err () - clears the journal's error state |
470decc6 DK |
1769 | * @journal: journal to act on. |
1770 | * | |
bfcd3555 | 1771 | * An error must be cleared or acked to take a FS out of readonly |
470decc6 DK |
1772 | * mode. |
1773 | */ | |
f7f4bccb | 1774 | int jbd2_journal_clear_err(journal_t *journal) |
470decc6 DK |
1775 | { |
1776 | int err = 0; | |
1777 | ||
a931da6a | 1778 | write_lock(&journal->j_state_lock); |
f7f4bccb | 1779 | if (journal->j_flags & JBD2_ABORT) |
470decc6 DK |
1780 | err = -EROFS; |
1781 | else | |
1782 | journal->j_errno = 0; | |
a931da6a | 1783 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
1784 | return err; |
1785 | } | |
1786 | ||
1787 | /** | |
f7f4bccb | 1788 | * void jbd2_journal_ack_err() - Ack journal err. |
470decc6 DK |
1789 | * @journal: journal to act on. |
1790 | * | |
bfcd3555 | 1791 | * An error must be cleared or acked to take a FS out of readonly |
470decc6 DK |
1792 | * mode. |
1793 | */ | |
f7f4bccb | 1794 | void jbd2_journal_ack_err(journal_t *journal) |
470decc6 | 1795 | { |
a931da6a | 1796 | write_lock(&journal->j_state_lock); |
470decc6 | 1797 | if (journal->j_errno) |
f7f4bccb | 1798 | journal->j_flags |= JBD2_ACK_ERR; |
a931da6a | 1799 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
1800 | } |
1801 | ||
f7f4bccb | 1802 | int jbd2_journal_blocks_per_page(struct inode *inode) |
470decc6 DK |
1803 | { |
1804 | return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | |
1805 | } | |
1806 | ||
b517bea1 ZB |
1807 | /* |
1808 | * helper functions to deal with 32 or 64bit block numbers. | |
1809 | */ | |
1810 | size_t journal_tag_bytes(journal_t *journal) | |
1811 | { | |
1812 | if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) | |
cd02ff0b | 1813 | return JBD2_TAG_SIZE64; |
b517bea1 | 1814 | else |
cd02ff0b | 1815 | return JBD2_TAG_SIZE32; |
b517bea1 ZB |
1816 | } |
1817 | ||
d2eecb03 TT |
1818 | /* |
1819 | * JBD memory management | |
1820 | * | |
1821 | * These functions are used to allocate block-sized chunks of memory | |
1822 | * used for making copies of buffer_head data. Very often it will be | |
1823 | * page-sized chunks of data, but sometimes it will be in | |
1824 | * sub-page-size chunks. (For example, 16k pages on Power systems | |
1825 | * with a 4k block file system.) For blocks smaller than a page, we | |
1826 | * use a SLAB allocator. There are slab caches for each block size, | |
1827 | * which are allocated at mount time, if necessary, and we only free | |
1828 | * (all of) the slab caches when/if the jbd2 module is unloaded. For | |
1829 | * this reason we don't need to a mutex to protect access to | |
1830 | * jbd2_slab[] allocating or releasing memory; only in | |
1831 | * jbd2_journal_create_slab(). | |
1832 | */ | |
1833 | #define JBD2_MAX_SLABS 8 | |
1834 | static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; | |
1835 | static DECLARE_MUTEX(jbd2_slab_create_sem); | |
1836 | ||
1837 | static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { | |
1838 | "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", | |
1839 | "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" | |
1840 | }; | |
1841 | ||
1842 | ||
1843 | static void jbd2_journal_destroy_slabs(void) | |
1844 | { | |
1845 | int i; | |
1846 | ||
1847 | for (i = 0; i < JBD2_MAX_SLABS; i++) { | |
1848 | if (jbd2_slab[i]) | |
1849 | kmem_cache_destroy(jbd2_slab[i]); | |
1850 | jbd2_slab[i] = NULL; | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | static int jbd2_journal_create_slab(size_t size) | |
1855 | { | |
1856 | int i = order_base_2(size) - 10; | |
1857 | size_t slab_size; | |
1858 | ||
1859 | if (size == PAGE_SIZE) | |
1860 | return 0; | |
1861 | ||
1862 | if (i >= JBD2_MAX_SLABS) | |
1863 | return -EINVAL; | |
1864 | ||
1865 | if (unlikely(i < 0)) | |
1866 | i = 0; | |
1867 | down(&jbd2_slab_create_sem); | |
1868 | if (jbd2_slab[i]) { | |
1869 | up(&jbd2_slab_create_sem); | |
1870 | return 0; /* Already created */ | |
1871 | } | |
1872 | ||
1873 | slab_size = 1 << (i+10); | |
1874 | jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, | |
1875 | slab_size, 0, NULL); | |
1876 | up(&jbd2_slab_create_sem); | |
1877 | if (!jbd2_slab[i]) { | |
1878 | printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); | |
1879 | return -ENOMEM; | |
1880 | } | |
1881 | return 0; | |
1882 | } | |
1883 | ||
1884 | static struct kmem_cache *get_slab(size_t size) | |
1885 | { | |
1886 | int i = order_base_2(size) - 10; | |
1887 | ||
1888 | BUG_ON(i >= JBD2_MAX_SLABS); | |
1889 | if (unlikely(i < 0)) | |
1890 | i = 0; | |
8ac97b74 | 1891 | BUG_ON(jbd2_slab[i] == NULL); |
d2eecb03 TT |
1892 | return jbd2_slab[i]; |
1893 | } | |
1894 | ||
1895 | void *jbd2_alloc(size_t size, gfp_t flags) | |
1896 | { | |
1897 | void *ptr; | |
1898 | ||
1899 | BUG_ON(size & (size-1)); /* Must be a power of 2 */ | |
1900 | ||
1901 | flags |= __GFP_REPEAT; | |
1902 | if (size == PAGE_SIZE) | |
1903 | ptr = (void *)__get_free_pages(flags, 0); | |
1904 | else if (size > PAGE_SIZE) { | |
1905 | int order = get_order(size); | |
1906 | ||
1907 | if (order < 3) | |
1908 | ptr = (void *)__get_free_pages(flags, order); | |
1909 | else | |
1910 | ptr = vmalloc(size); | |
1911 | } else | |
1912 | ptr = kmem_cache_alloc(get_slab(size), flags); | |
1913 | ||
1914 | /* Check alignment; SLUB has gotten this wrong in the past, | |
1915 | * and this can lead to user data corruption! */ | |
1916 | BUG_ON(((unsigned long) ptr) & (size-1)); | |
1917 | ||
1918 | return ptr; | |
1919 | } | |
1920 | ||
1921 | void jbd2_free(void *ptr, size_t size) | |
1922 | { | |
1923 | if (size == PAGE_SIZE) { | |
1924 | free_pages((unsigned long)ptr, 0); | |
1925 | return; | |
1926 | } | |
1927 | if (size > PAGE_SIZE) { | |
1928 | int order = get_order(size); | |
1929 | ||
1930 | if (order < 3) | |
1931 | free_pages((unsigned long)ptr, order); | |
1932 | else | |
1933 | vfree(ptr); | |
1934 | return; | |
1935 | } | |
1936 | kmem_cache_free(get_slab(size), ptr); | |
1937 | }; | |
1938 | ||
470decc6 DK |
1939 | /* |
1940 | * Journal_head storage management | |
1941 | */ | |
e18b890b | 1942 | static struct kmem_cache *jbd2_journal_head_cache; |
e23291b9 | 1943 | #ifdef CONFIG_JBD2_DEBUG |
470decc6 DK |
1944 | static atomic_t nr_journal_heads = ATOMIC_INIT(0); |
1945 | #endif | |
1946 | ||
f7f4bccb | 1947 | static int journal_init_jbd2_journal_head_cache(void) |
470decc6 DK |
1948 | { |
1949 | int retval; | |
1950 | ||
1076d17a | 1951 | J_ASSERT(jbd2_journal_head_cache == NULL); |
a920e941 | 1952 | jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", |
470decc6 DK |
1953 | sizeof(struct journal_head), |
1954 | 0, /* offset */ | |
77160957 | 1955 | SLAB_TEMPORARY, /* flags */ |
20c2df83 | 1956 | NULL); /* ctor */ |
470decc6 | 1957 | retval = 0; |
1076d17a | 1958 | if (!jbd2_journal_head_cache) { |
470decc6 DK |
1959 | retval = -ENOMEM; |
1960 | printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); | |
1961 | } | |
1962 | return retval; | |
1963 | } | |
1964 | ||
f7f4bccb | 1965 | static void jbd2_journal_destroy_jbd2_journal_head_cache(void) |
470decc6 | 1966 | { |
8a9362eb DG |
1967 | if (jbd2_journal_head_cache) { |
1968 | kmem_cache_destroy(jbd2_journal_head_cache); | |
1969 | jbd2_journal_head_cache = NULL; | |
1970 | } | |
470decc6 DK |
1971 | } |
1972 | ||
1973 | /* | |
1974 | * journal_head splicing and dicing | |
1975 | */ | |
1976 | static struct journal_head *journal_alloc_journal_head(void) | |
1977 | { | |
1978 | struct journal_head *ret; | |
1979 | static unsigned long last_warning; | |
1980 | ||
e23291b9 | 1981 | #ifdef CONFIG_JBD2_DEBUG |
470decc6 DK |
1982 | atomic_inc(&nr_journal_heads); |
1983 | #endif | |
f7f4bccb | 1984 | ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); |
1076d17a | 1985 | if (!ret) { |
470decc6 DK |
1986 | jbd_debug(1, "out of memory for journal_head\n"); |
1987 | if (time_after(jiffies, last_warning + 5*HZ)) { | |
1988 | printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", | |
329d291f | 1989 | __func__); |
470decc6 DK |
1990 | last_warning = jiffies; |
1991 | } | |
1076d17a | 1992 | while (!ret) { |
470decc6 | 1993 | yield(); |
f7f4bccb | 1994 | ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); |
470decc6 DK |
1995 | } |
1996 | } | |
1997 | return ret; | |
1998 | } | |
1999 | ||
2000 | static void journal_free_journal_head(struct journal_head *jh) | |
2001 | { | |
e23291b9 | 2002 | #ifdef CONFIG_JBD2_DEBUG |
470decc6 | 2003 | atomic_dec(&nr_journal_heads); |
cd02ff0b | 2004 | memset(jh, JBD2_POISON_FREE, sizeof(*jh)); |
470decc6 | 2005 | #endif |
f7f4bccb | 2006 | kmem_cache_free(jbd2_journal_head_cache, jh); |
470decc6 DK |
2007 | } |
2008 | ||
2009 | /* | |
2010 | * A journal_head is attached to a buffer_head whenever JBD has an | |
2011 | * interest in the buffer. | |
2012 | * | |
2013 | * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit | |
2014 | * is set. This bit is tested in core kernel code where we need to take | |
2015 | * JBD-specific actions. Testing the zeroness of ->b_private is not reliable | |
2016 | * there. | |
2017 | * | |
2018 | * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. | |
2019 | * | |
2020 | * When a buffer has its BH_JBD bit set it is immune from being released by | |
2021 | * core kernel code, mainly via ->b_count. | |
2022 | * | |
2023 | * A journal_head may be detached from its buffer_head when the journal_head's | |
2024 | * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. | |
f7f4bccb | 2025 | * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the |
470decc6 DK |
2026 | * journal_head can be dropped if needed. |
2027 | * | |
2028 | * Various places in the kernel want to attach a journal_head to a buffer_head | |
2029 | * _before_ attaching the journal_head to a transaction. To protect the | |
f7f4bccb | 2030 | * journal_head in this situation, jbd2_journal_add_journal_head elevates the |
470decc6 | 2031 | * journal_head's b_jcount refcount by one. The caller must call |
f7f4bccb | 2032 | * jbd2_journal_put_journal_head() to undo this. |
470decc6 DK |
2033 | * |
2034 | * So the typical usage would be: | |
2035 | * | |
2036 | * (Attach a journal_head if needed. Increments b_jcount) | |
f7f4bccb | 2037 | * struct journal_head *jh = jbd2_journal_add_journal_head(bh); |
470decc6 DK |
2038 | * ... |
2039 | * jh->b_transaction = xxx; | |
f7f4bccb | 2040 | * jbd2_journal_put_journal_head(jh); |
470decc6 DK |
2041 | * |
2042 | * Now, the journal_head's b_jcount is zero, but it is safe from being released | |
2043 | * because it has a non-zero b_transaction. | |
2044 | */ | |
2045 | ||
2046 | /* | |
2047 | * Give a buffer_head a journal_head. | |
2048 | * | |
2049 | * Doesn't need the journal lock. | |
2050 | * May sleep. | |
2051 | */ | |
f7f4bccb | 2052 | struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) |
470decc6 DK |
2053 | { |
2054 | struct journal_head *jh; | |
2055 | struct journal_head *new_jh = NULL; | |
2056 | ||
2057 | repeat: | |
2058 | if (!buffer_jbd(bh)) { | |
2059 | new_jh = journal_alloc_journal_head(); | |
2060 | memset(new_jh, 0, sizeof(*new_jh)); | |
2061 | } | |
2062 | ||
2063 | jbd_lock_bh_journal_head(bh); | |
2064 | if (buffer_jbd(bh)) { | |
2065 | jh = bh2jh(bh); | |
2066 | } else { | |
2067 | J_ASSERT_BH(bh, | |
2068 | (atomic_read(&bh->b_count) > 0) || | |
2069 | (bh->b_page && bh->b_page->mapping)); | |
2070 | ||
2071 | if (!new_jh) { | |
2072 | jbd_unlock_bh_journal_head(bh); | |
2073 | goto repeat; | |
2074 | } | |
2075 | ||
2076 | jh = new_jh; | |
2077 | new_jh = NULL; /* We consumed it */ | |
2078 | set_buffer_jbd(bh); | |
2079 | bh->b_private = jh; | |
2080 | jh->b_bh = bh; | |
2081 | get_bh(bh); | |
2082 | BUFFER_TRACE(bh, "added journal_head"); | |
2083 | } | |
2084 | jh->b_jcount++; | |
2085 | jbd_unlock_bh_journal_head(bh); | |
2086 | if (new_jh) | |
2087 | journal_free_journal_head(new_jh); | |
2088 | return bh->b_private; | |
2089 | } | |
2090 | ||
2091 | /* | |
2092 | * Grab a ref against this buffer_head's journal_head. If it ended up not | |
2093 | * having a journal_head, return NULL | |
2094 | */ | |
f7f4bccb | 2095 | struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) |
470decc6 DK |
2096 | { |
2097 | struct journal_head *jh = NULL; | |
2098 | ||
2099 | jbd_lock_bh_journal_head(bh); | |
2100 | if (buffer_jbd(bh)) { | |
2101 | jh = bh2jh(bh); | |
2102 | jh->b_jcount++; | |
2103 | } | |
2104 | jbd_unlock_bh_journal_head(bh); | |
2105 | return jh; | |
2106 | } | |
2107 | ||
2108 | static void __journal_remove_journal_head(struct buffer_head *bh) | |
2109 | { | |
2110 | struct journal_head *jh = bh2jh(bh); | |
2111 | ||
2112 | J_ASSERT_JH(jh, jh->b_jcount >= 0); | |
2113 | ||
2114 | get_bh(bh); | |
2115 | if (jh->b_jcount == 0) { | |
2116 | if (jh->b_transaction == NULL && | |
2117 | jh->b_next_transaction == NULL && | |
2118 | jh->b_cp_transaction == NULL) { | |
2119 | J_ASSERT_JH(jh, jh->b_jlist == BJ_None); | |
2120 | J_ASSERT_BH(bh, buffer_jbd(bh)); | |
2121 | J_ASSERT_BH(bh, jh2bh(jh) == bh); | |
2122 | BUFFER_TRACE(bh, "remove journal_head"); | |
2123 | if (jh->b_frozen_data) { | |
2124 | printk(KERN_WARNING "%s: freeing " | |
2125 | "b_frozen_data\n", | |
329d291f | 2126 | __func__); |
af1e76d6 | 2127 | jbd2_free(jh->b_frozen_data, bh->b_size); |
470decc6 DK |
2128 | } |
2129 | if (jh->b_committed_data) { | |
2130 | printk(KERN_WARNING "%s: freeing " | |
2131 | "b_committed_data\n", | |
329d291f | 2132 | __func__); |
af1e76d6 | 2133 | jbd2_free(jh->b_committed_data, bh->b_size); |
470decc6 DK |
2134 | } |
2135 | bh->b_private = NULL; | |
2136 | jh->b_bh = NULL; /* debug, really */ | |
2137 | clear_buffer_jbd(bh); | |
2138 | __brelse(bh); | |
2139 | journal_free_journal_head(jh); | |
2140 | } else { | |
2141 | BUFFER_TRACE(bh, "journal_head was locked"); | |
2142 | } | |
2143 | } | |
2144 | } | |
2145 | ||
2146 | /* | |
f7f4bccb | 2147 | * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction |
470decc6 DK |
2148 | * and has a zero b_jcount then remove and release its journal_head. If we did |
2149 | * see that the buffer is not used by any transaction we also "logically" | |
2150 | * decrement ->b_count. | |
2151 | * | |
2152 | * We in fact take an additional increment on ->b_count as a convenience, | |
2153 | * because the caller usually wants to do additional things with the bh | |
2154 | * after calling here. | |
f7f4bccb | 2155 | * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some |
470decc6 DK |
2156 | * time. Once the caller has run __brelse(), the buffer is eligible for |
2157 | * reaping by try_to_free_buffers(). | |
2158 | */ | |
f7f4bccb | 2159 | void jbd2_journal_remove_journal_head(struct buffer_head *bh) |
470decc6 DK |
2160 | { |
2161 | jbd_lock_bh_journal_head(bh); | |
2162 | __journal_remove_journal_head(bh); | |
2163 | jbd_unlock_bh_journal_head(bh); | |
2164 | } | |
2165 | ||
2166 | /* | |
2167 | * Drop a reference on the passed journal_head. If it fell to zero then try to | |
2168 | * release the journal_head from the buffer_head. | |
2169 | */ | |
f7f4bccb | 2170 | void jbd2_journal_put_journal_head(struct journal_head *jh) |
470decc6 DK |
2171 | { |
2172 | struct buffer_head *bh = jh2bh(jh); | |
2173 | ||
2174 | jbd_lock_bh_journal_head(bh); | |
2175 | J_ASSERT_JH(jh, jh->b_jcount > 0); | |
2176 | --jh->b_jcount; | |
2177 | if (!jh->b_jcount && !jh->b_transaction) { | |
2178 | __journal_remove_journal_head(bh); | |
2179 | __brelse(bh); | |
2180 | } | |
2181 | jbd_unlock_bh_journal_head(bh); | |
2182 | } | |
2183 | ||
c851ed54 JK |
2184 | /* |
2185 | * Initialize jbd inode head | |
2186 | */ | |
2187 | void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) | |
2188 | { | |
2189 | jinode->i_transaction = NULL; | |
2190 | jinode->i_next_transaction = NULL; | |
2191 | jinode->i_vfs_inode = inode; | |
2192 | jinode->i_flags = 0; | |
2193 | INIT_LIST_HEAD(&jinode->i_list); | |
2194 | } | |
2195 | ||
2196 | /* | |
2197 | * Function to be called before we start removing inode from memory (i.e., | |
2198 | * clear_inode() is a fine place to be called from). It removes inode from | |
2199 | * transaction's lists. | |
2200 | */ | |
2201 | void jbd2_journal_release_jbd_inode(journal_t *journal, | |
2202 | struct jbd2_inode *jinode) | |
2203 | { | |
c851ed54 JK |
2204 | if (!journal) |
2205 | return; | |
2206 | restart: | |
2207 | spin_lock(&journal->j_list_lock); | |
2208 | /* Is commit writing out inode - we have to wait */ | |
2209 | if (jinode->i_flags & JI_COMMIT_RUNNING) { | |
2210 | wait_queue_head_t *wq; | |
2211 | DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); | |
2212 | wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); | |
2213 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); | |
2214 | spin_unlock(&journal->j_list_lock); | |
2215 | schedule(); | |
2216 | finish_wait(wq, &wait.wait); | |
2217 | goto restart; | |
2218 | } | |
2219 | ||
c851ed54 JK |
2220 | if (jinode->i_transaction) { |
2221 | list_del(&jinode->i_list); | |
2222 | jinode->i_transaction = NULL; | |
2223 | } | |
2224 | spin_unlock(&journal->j_list_lock); | |
2225 | } | |
2226 | ||
470decc6 | 2227 | /* |
0f49d5d0 | 2228 | * debugfs tunables |
470decc6 | 2229 | */ |
6f38c74f JS |
2230 | #ifdef CONFIG_JBD2_DEBUG |
2231 | u8 jbd2_journal_enable_debug __read_mostly; | |
f7f4bccb | 2232 | EXPORT_SYMBOL(jbd2_journal_enable_debug); |
470decc6 | 2233 | |
0f49d5d0 | 2234 | #define JBD2_DEBUG_NAME "jbd2-debug" |
470decc6 | 2235 | |
6f38c74f JS |
2236 | static struct dentry *jbd2_debugfs_dir; |
2237 | static struct dentry *jbd2_debug; | |
470decc6 | 2238 | |
0f49d5d0 JS |
2239 | static void __init jbd2_create_debugfs_entry(void) |
2240 | { | |
2241 | jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); | |
2242 | if (jbd2_debugfs_dir) | |
765f8361 YK |
2243 | jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, |
2244 | S_IRUGO | S_IWUSR, | |
0f49d5d0 JS |
2245 | jbd2_debugfs_dir, |
2246 | &jbd2_journal_enable_debug); | |
470decc6 DK |
2247 | } |
2248 | ||
0f49d5d0 | 2249 | static void __exit jbd2_remove_debugfs_entry(void) |
470decc6 | 2250 | { |
6f38c74f JS |
2251 | debugfs_remove(jbd2_debug); |
2252 | debugfs_remove(jbd2_debugfs_dir); | |
470decc6 DK |
2253 | } |
2254 | ||
0f49d5d0 | 2255 | #else |
470decc6 | 2256 | |
0f49d5d0 | 2257 | static void __init jbd2_create_debugfs_entry(void) |
470decc6 | 2258 | { |
470decc6 DK |
2259 | } |
2260 | ||
0f49d5d0 | 2261 | static void __exit jbd2_remove_debugfs_entry(void) |
470decc6 | 2262 | { |
470decc6 DK |
2263 | } |
2264 | ||
470decc6 DK |
2265 | #endif |
2266 | ||
8e85fb3f JL |
2267 | #ifdef CONFIG_PROC_FS |
2268 | ||
2269 | #define JBD2_STATS_PROC_NAME "fs/jbd2" | |
2270 | ||
2271 | static void __init jbd2_create_jbd_stats_proc_entry(void) | |
2272 | { | |
2273 | proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); | |
2274 | } | |
2275 | ||
2276 | static void __exit jbd2_remove_jbd_stats_proc_entry(void) | |
2277 | { | |
2278 | if (proc_jbd2_stats) | |
2279 | remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); | |
2280 | } | |
2281 | ||
2282 | #else | |
2283 | ||
2284 | #define jbd2_create_jbd_stats_proc_entry() do {} while (0) | |
2285 | #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) | |
2286 | ||
2287 | #endif | |
2288 | ||
e18b890b | 2289 | struct kmem_cache *jbd2_handle_cache; |
470decc6 DK |
2290 | |
2291 | static int __init journal_init_handle_cache(void) | |
2292 | { | |
a920e941 | 2293 | jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle", |
470decc6 DK |
2294 | sizeof(handle_t), |
2295 | 0, /* offset */ | |
77160957 | 2296 | SLAB_TEMPORARY, /* flags */ |
20c2df83 | 2297 | NULL); /* ctor */ |
f7f4bccb | 2298 | if (jbd2_handle_cache == NULL) { |
470decc6 DK |
2299 | printk(KERN_EMERG "JBD: failed to create handle cache\n"); |
2300 | return -ENOMEM; | |
2301 | } | |
2302 | return 0; | |
2303 | } | |
2304 | ||
f7f4bccb | 2305 | static void jbd2_journal_destroy_handle_cache(void) |
470decc6 | 2306 | { |
f7f4bccb MC |
2307 | if (jbd2_handle_cache) |
2308 | kmem_cache_destroy(jbd2_handle_cache); | |
470decc6 DK |
2309 | } |
2310 | ||
2311 | /* | |
2312 | * Module startup and shutdown | |
2313 | */ | |
2314 | ||
2315 | static int __init journal_init_caches(void) | |
2316 | { | |
2317 | int ret; | |
2318 | ||
f7f4bccb | 2319 | ret = jbd2_journal_init_revoke_caches(); |
470decc6 | 2320 | if (ret == 0) |
f7f4bccb | 2321 | ret = journal_init_jbd2_journal_head_cache(); |
470decc6 DK |
2322 | if (ret == 0) |
2323 | ret = journal_init_handle_cache(); | |
2324 | return ret; | |
2325 | } | |
2326 | ||
f7f4bccb | 2327 | static void jbd2_journal_destroy_caches(void) |
470decc6 | 2328 | { |
f7f4bccb MC |
2329 | jbd2_journal_destroy_revoke_caches(); |
2330 | jbd2_journal_destroy_jbd2_journal_head_cache(); | |
2331 | jbd2_journal_destroy_handle_cache(); | |
d2eecb03 | 2332 | jbd2_journal_destroy_slabs(); |
470decc6 DK |
2333 | } |
2334 | ||
2335 | static int __init journal_init(void) | |
2336 | { | |
2337 | int ret; | |
2338 | ||
2339 | BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); | |
2340 | ||
2341 | ret = journal_init_caches(); | |
620de4e1 DG |
2342 | if (ret == 0) { |
2343 | jbd2_create_debugfs_entry(); | |
2344 | jbd2_create_jbd_stats_proc_entry(); | |
2345 | } else { | |
f7f4bccb | 2346 | jbd2_journal_destroy_caches(); |
620de4e1 | 2347 | } |
470decc6 DK |
2348 | return ret; |
2349 | } | |
2350 | ||
2351 | static void __exit journal_exit(void) | |
2352 | { | |
e23291b9 | 2353 | #ifdef CONFIG_JBD2_DEBUG |
470decc6 DK |
2354 | int n = atomic_read(&nr_journal_heads); |
2355 | if (n) | |
2356 | printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); | |
2357 | #endif | |
0f49d5d0 | 2358 | jbd2_remove_debugfs_entry(); |
8e85fb3f | 2359 | jbd2_remove_jbd_stats_proc_entry(); |
f7f4bccb | 2360 | jbd2_journal_destroy_caches(); |
470decc6 DK |
2361 | } |
2362 | ||
879c5e6b TT |
2363 | /* |
2364 | * jbd2_dev_to_name is a utility function used by the jbd2 and ext4 | |
2365 | * tracing infrastructure to map a dev_t to a device name. | |
2366 | * | |
2367 | * The caller should use rcu_read_lock() in order to make sure the | |
2368 | * device name stays valid until its done with it. We use | |
2369 | * rcu_read_lock() as well to make sure we're safe in case the caller | |
2370 | * gets sloppy, and because rcu_read_lock() is cheap and can be safely | |
2371 | * nested. | |
2372 | */ | |
2373 | struct devname_cache { | |
2374 | struct rcu_head rcu; | |
2375 | dev_t device; | |
2376 | char devname[BDEVNAME_SIZE]; | |
2377 | }; | |
2378 | #define CACHE_SIZE_BITS 6 | |
2379 | static struct devname_cache *devcache[1 << CACHE_SIZE_BITS]; | |
2380 | static DEFINE_SPINLOCK(devname_cache_lock); | |
2381 | ||
2382 | static void free_devcache(struct rcu_head *rcu) | |
2383 | { | |
2384 | kfree(rcu); | |
2385 | } | |
2386 | ||
2387 | const char *jbd2_dev_to_name(dev_t device) | |
2388 | { | |
2389 | int i = hash_32(device, CACHE_SIZE_BITS); | |
2390 | char *ret; | |
2391 | struct block_device *bd; | |
b5744805 | 2392 | static struct devname_cache *new_dev; |
879c5e6b TT |
2393 | |
2394 | rcu_read_lock(); | |
2395 | if (devcache[i] && devcache[i]->device == device) { | |
2396 | ret = devcache[i]->devname; | |
2397 | rcu_read_unlock(); | |
2398 | return ret; | |
2399 | } | |
2400 | rcu_read_unlock(); | |
2401 | ||
b5744805 TT |
2402 | new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL); |
2403 | if (!new_dev) | |
2404 | return "NODEV-ALLOCFAILURE"; /* Something non-NULL */ | |
879c5e6b TT |
2405 | spin_lock(&devname_cache_lock); |
2406 | if (devcache[i]) { | |
2407 | if (devcache[i]->device == device) { | |
b5744805 | 2408 | kfree(new_dev); |
879c5e6b TT |
2409 | ret = devcache[i]->devname; |
2410 | spin_unlock(&devname_cache_lock); | |
2411 | return ret; | |
2412 | } | |
2413 | call_rcu(&devcache[i]->rcu, free_devcache); | |
2414 | } | |
b5744805 | 2415 | devcache[i] = new_dev; |
879c5e6b TT |
2416 | devcache[i]->device = device; |
2417 | bd = bdget(device); | |
2418 | if (bd) { | |
2419 | bdevname(bd, devcache[i]->devname); | |
2420 | bdput(bd); | |
2421 | } else | |
2422 | __bdevname(device, devcache[i]->devname); | |
2423 | ret = devcache[i]->devname; | |
2424 | spin_unlock(&devname_cache_lock); | |
2425 | return ret; | |
2426 | } | |
2427 | EXPORT_SYMBOL(jbd2_dev_to_name); | |
2428 | ||
470decc6 DK |
2429 | MODULE_LICENSE("GPL"); |
2430 | module_init(journal_init); | |
2431 | module_exit(journal_exit); | |
2432 |