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