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