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