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