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