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