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