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