]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
f30c2269 | 2 | * linux/fs/jbd/journal.c |
1da177e4 LT |
3 | * |
4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 | |
5 | * | |
6 | * Copyright 1998 Red Hat corp --- All Rights Reserved | |
7 | * | |
8 | * This file is part of the Linux kernel and is made available under | |
9 | * the terms of the GNU General Public License, version 2, or at your | |
10 | * option, any later version, incorporated herein by reference. | |
11 | * | |
12 | * Generic filesystem journal-writing code; part of the ext2fs | |
13 | * journaling system. | |
14 | * | |
15 | * This file manages journals: areas of disk reserved for logging | |
16 | * transactional updates. This includes the kernel journaling thread | |
17 | * which is responsible for scheduling updates to the log. | |
18 | * | |
19 | * We do not actually manage the physical storage of the journal in this | |
20 | * file: that is left to a per-journal policy function, which allows us | |
21 | * to store the journal within a filesystem-specified area for ext2 | |
22 | * journaling (ext2 can use a reserved inode for storing the log). | |
23 | */ | |
24 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/fs.h> | |
28 | #include <linux/jbd.h> | |
29 | #include <linux/errno.h> | |
30 | #include <linux/slab.h> | |
1da177e4 LT |
31 | #include <linux/init.h> |
32 | #include <linux/mm.h> | |
7dfb7103 | 33 | #include <linux/freezer.h> |
1da177e4 | 34 | #include <linux/pagemap.h> |
8d8c8511 | 35 | #include <linux/kthread.h> |
c9cf5528 | 36 | #include <linux/poison.h> |
8d8c8511 | 37 | #include <linux/proc_fs.h> |
c2a9159c | 38 | #include <linux/debugfs.h> |
8d8c8511 | 39 | |
1da177e4 LT |
40 | #include <asm/uaccess.h> |
41 | #include <asm/page.h> | |
1da177e4 LT |
42 | |
43 | EXPORT_SYMBOL(journal_start); | |
44 | EXPORT_SYMBOL(journal_restart); | |
45 | EXPORT_SYMBOL(journal_extend); | |
46 | EXPORT_SYMBOL(journal_stop); | |
47 | EXPORT_SYMBOL(journal_lock_updates); | |
48 | EXPORT_SYMBOL(journal_unlock_updates); | |
49 | EXPORT_SYMBOL(journal_get_write_access); | |
50 | EXPORT_SYMBOL(journal_get_create_access); | |
51 | EXPORT_SYMBOL(journal_get_undo_access); | |
52 | EXPORT_SYMBOL(journal_dirty_data); | |
53 | EXPORT_SYMBOL(journal_dirty_metadata); | |
54 | EXPORT_SYMBOL(journal_release_buffer); | |
55 | EXPORT_SYMBOL(journal_forget); | |
56 | #if 0 | |
57 | EXPORT_SYMBOL(journal_sync_buffer); | |
58 | #endif | |
59 | EXPORT_SYMBOL(journal_flush); | |
60 | EXPORT_SYMBOL(journal_revoke); | |
61 | ||
62 | EXPORT_SYMBOL(journal_init_dev); | |
63 | EXPORT_SYMBOL(journal_init_inode); | |
64 | EXPORT_SYMBOL(journal_update_format); | |
65 | EXPORT_SYMBOL(journal_check_used_features); | |
66 | EXPORT_SYMBOL(journal_check_available_features); | |
67 | EXPORT_SYMBOL(journal_set_features); | |
68 | EXPORT_SYMBOL(journal_create); | |
69 | EXPORT_SYMBOL(journal_load); | |
70 | EXPORT_SYMBOL(journal_destroy); | |
1da177e4 LT |
71 | EXPORT_SYMBOL(journal_abort); |
72 | EXPORT_SYMBOL(journal_errno); | |
73 | EXPORT_SYMBOL(journal_ack_err); | |
74 | EXPORT_SYMBOL(journal_clear_err); | |
75 | EXPORT_SYMBOL(log_wait_commit); | |
ff5e4b51 | 76 | EXPORT_SYMBOL(log_start_commit); |
1da177e4 LT |
77 | EXPORT_SYMBOL(journal_start_commit); |
78 | EXPORT_SYMBOL(journal_force_commit_nested); | |
79 | EXPORT_SYMBOL(journal_wipe); | |
80 | EXPORT_SYMBOL(journal_blocks_per_page); | |
81 | EXPORT_SYMBOL(journal_invalidatepage); | |
82 | EXPORT_SYMBOL(journal_try_to_free_buffers); | |
83 | EXPORT_SYMBOL(journal_force_commit); | |
84 | ||
85 | static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); | |
022a4a7b | 86 | static void __journal_abort_soft (journal_t *journal, int errno); |
1da177e4 LT |
87 | |
88 | /* | |
89 | * Helper function used to manage commit timeouts | |
90 | */ | |
91 | ||
92 | static void commit_timeout(unsigned long __data) | |
93 | { | |
94 | struct task_struct * p = (struct task_struct *) __data; | |
95 | ||
96 | wake_up_process(p); | |
97 | } | |
98 | ||
1da177e4 LT |
99 | /* |
100 | * kjournald: The main thread function used to manage a logging device | |
101 | * journal. | |
102 | * | |
103 | * This kernel thread is responsible for two things: | |
104 | * | |
105 | * 1) COMMIT: Every so often we need to commit the current state of the | |
106 | * filesystem to disk. The journal thread is responsible for writing | |
107 | * all of the metadata buffers to disk. | |
108 | * | |
109 | * 2) CHECKPOINT: We cannot reuse a used section of the log file until all | |
110 | * of the data in that part of the log has been rewritten elsewhere on | |
111 | * the disk. Flushing these old buffers to reclaim space in the log is | |
112 | * known as checkpointing, and this thread is responsible for that job. | |
113 | */ | |
114 | ||
022a4a7b | 115 | static int kjournald(void *arg) |
1da177e4 | 116 | { |
e3df1898 | 117 | journal_t *journal = arg; |
1da177e4 | 118 | transaction_t *transaction; |
1da177e4 | 119 | |
e3df1898 AM |
120 | /* |
121 | * Set up an interval timer which can be used to trigger a commit wakeup | |
122 | * after the commit interval expires | |
123 | */ | |
124 | setup_timer(&journal->j_commit_timer, commit_timeout, | |
125 | (unsigned long)current); | |
1da177e4 LT |
126 | |
127 | /* Record that the journal thread is running */ | |
128 | journal->j_task = current; | |
129 | wake_up(&journal->j_wait_done_commit); | |
130 | ||
131 | printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n", | |
132 | journal->j_commit_interval / HZ); | |
133 | ||
134 | /* | |
135 | * And now, wait forever for commit wakeup events. | |
136 | */ | |
137 | spin_lock(&journal->j_state_lock); | |
138 | ||
139 | loop: | |
140 | if (journal->j_flags & JFS_UNMOUNT) | |
141 | goto end_loop; | |
142 | ||
143 | jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", | |
144 | journal->j_commit_sequence, journal->j_commit_request); | |
145 | ||
146 | if (journal->j_commit_sequence != journal->j_commit_request) { | |
147 | jbd_debug(1, "OK, requests differ\n"); | |
148 | spin_unlock(&journal->j_state_lock); | |
e3df1898 | 149 | del_timer_sync(&journal->j_commit_timer); |
1da177e4 LT |
150 | journal_commit_transaction(journal); |
151 | spin_lock(&journal->j_state_lock); | |
152 | goto loop; | |
153 | } | |
154 | ||
155 | wake_up(&journal->j_wait_done_commit); | |
3e1d1d28 | 156 | if (freezing(current)) { |
1da177e4 LT |
157 | /* |
158 | * The simpler the better. Flushing journal isn't a | |
159 | * good idea, because that depends on threads that may | |
160 | * be already stopped. | |
161 | */ | |
162 | jbd_debug(1, "Now suspending kjournald\n"); | |
163 | spin_unlock(&journal->j_state_lock); | |
3e1d1d28 | 164 | refrigerator(); |
1da177e4 LT |
165 | spin_lock(&journal->j_state_lock); |
166 | } else { | |
167 | /* | |
168 | * We assume on resume that commits are already there, | |
169 | * so we don't sleep | |
170 | */ | |
171 | DEFINE_WAIT(wait); | |
172 | int should_sleep = 1; | |
173 | ||
174 | prepare_to_wait(&journal->j_wait_commit, &wait, | |
175 | TASK_INTERRUPTIBLE); | |
176 | if (journal->j_commit_sequence != journal->j_commit_request) | |
177 | should_sleep = 0; | |
178 | transaction = journal->j_running_transaction; | |
179 | if (transaction && time_after_eq(jiffies, | |
180 | transaction->t_expires)) | |
181 | should_sleep = 0; | |
cbf0d27a | 182 | if (journal->j_flags & JFS_UNMOUNT) |
e9ad5620 | 183 | should_sleep = 0; |
1da177e4 LT |
184 | if (should_sleep) { |
185 | spin_unlock(&journal->j_state_lock); | |
186 | schedule(); | |
187 | spin_lock(&journal->j_state_lock); | |
188 | } | |
189 | finish_wait(&journal->j_wait_commit, &wait); | |
190 | } | |
191 | ||
192 | jbd_debug(1, "kjournald wakes\n"); | |
193 | ||
194 | /* | |
195 | * Were we woken up by a commit wakeup event? | |
196 | */ | |
197 | transaction = journal->j_running_transaction; | |
198 | if (transaction && time_after_eq(jiffies, transaction->t_expires)) { | |
199 | journal->j_commit_request = transaction->t_tid; | |
200 | jbd_debug(1, "woke because of timeout\n"); | |
201 | } | |
202 | goto loop; | |
203 | ||
204 | end_loop: | |
205 | spin_unlock(&journal->j_state_lock); | |
e3df1898 | 206 | del_timer_sync(&journal->j_commit_timer); |
1da177e4 LT |
207 | journal->j_task = NULL; |
208 | wake_up(&journal->j_wait_done_commit); | |
209 | jbd_debug(1, "Journal thread exiting.\n"); | |
210 | return 0; | |
211 | } | |
212 | ||
97f06784 | 213 | static int journal_start_thread(journal_t *journal) |
1da177e4 | 214 | { |
97f06784 PE |
215 | struct task_struct *t; |
216 | ||
217 | t = kthread_run(kjournald, journal, "kjournald"); | |
218 | if (IS_ERR(t)) | |
219 | return PTR_ERR(t); | |
220 | ||
c80544dc | 221 | wait_event(journal->j_wait_done_commit, journal->j_task != NULL); |
97f06784 | 222 | return 0; |
1da177e4 LT |
223 | } |
224 | ||
225 | static void journal_kill_thread(journal_t *journal) | |
226 | { | |
227 | spin_lock(&journal->j_state_lock); | |
228 | journal->j_flags |= JFS_UNMOUNT; | |
229 | ||
230 | while (journal->j_task) { | |
231 | wake_up(&journal->j_wait_commit); | |
232 | spin_unlock(&journal->j_state_lock); | |
c80544dc SH |
233 | wait_event(journal->j_wait_done_commit, |
234 | journal->j_task == NULL); | |
1da177e4 LT |
235 | spin_lock(&journal->j_state_lock); |
236 | } | |
237 | spin_unlock(&journal->j_state_lock); | |
238 | } | |
239 | ||
240 | /* | |
241 | * journal_write_metadata_buffer: write a metadata buffer to the journal. | |
242 | * | |
243 | * Writes a metadata buffer to a given disk block. The actual IO is not | |
244 | * performed but a new buffer_head is constructed which labels the data | |
245 | * to be written with the correct destination disk block. | |
246 | * | |
247 | * Any magic-number escaping which needs to be done will cause a | |
248 | * copy-out here. If the buffer happens to start with the | |
249 | * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the | |
250 | * magic number is only written to the log for descripter blocks. In | |
251 | * this case, we copy the data and replace the first word with 0, and we | |
252 | * return a result code which indicates that this buffer needs to be | |
253 | * marked as an escaped buffer in the corresponding log descriptor | |
254 | * block. The missing word can then be restored when the block is read | |
255 | * during recovery. | |
256 | * | |
257 | * If the source buffer has already been modified by a new transaction | |
258 | * since we took the last commit snapshot, we use the frozen copy of | |
259 | * that data for IO. If we end up using the existing buffer_head's data | |
260 | * for the write, then we *have* to lock the buffer to prevent anyone | |
261 | * else from using and possibly modifying it while the IO is in | |
262 | * progress. | |
263 | * | |
264 | * The function returns a pointer to the buffer_heads to be used for IO. | |
265 | * | |
266 | * We assume that the journal has already been locked in this function. | |
267 | * | |
268 | * Return value: | |
269 | * <0: Error | |
270 | * >=0: Finished OK | |
271 | * | |
272 | * On success: | |
273 | * Bit 0 set == escape performed on the data | |
274 | * Bit 1 set == buffer copy-out performed (kfree the data after IO) | |
275 | */ | |
276 | ||
277 | int journal_write_metadata_buffer(transaction_t *transaction, | |
278 | struct journal_head *jh_in, | |
279 | struct journal_head **jh_out, | |
9c28cbcc | 280 | unsigned int blocknr) |
1da177e4 LT |
281 | { |
282 | int need_copy_out = 0; | |
283 | int done_copy_out = 0; | |
284 | int do_escape = 0; | |
285 | char *mapped_data; | |
286 | struct buffer_head *new_bh; | |
287 | struct journal_head *new_jh; | |
288 | struct page *new_page; | |
289 | unsigned int new_offset; | |
290 | struct buffer_head *bh_in = jh2bh(jh_in); | |
f1015c44 | 291 | journal_t *journal = transaction->t_journal; |
1da177e4 LT |
292 | |
293 | /* | |
294 | * The buffer really shouldn't be locked: only the current committing | |
295 | * transaction is allowed to write it, so nobody else is allowed | |
296 | * to do any IO. | |
297 | * | |
298 | * akpm: except if we're journalling data, and write() output is | |
299 | * also part of a shared mapping, and another thread has | |
300 | * decided to launch a writepage() against this buffer. | |
301 | */ | |
302 | J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); | |
303 | ||
304 | new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); | |
f1015c44 | 305 | /* keep subsequent assertions sane */ |
306 | new_bh->b_state = 0; | |
307 | init_buffer(new_bh, NULL, NULL); | |
308 | atomic_set(&new_bh->b_count, 1); | |
309 | new_jh = journal_add_journal_head(new_bh); /* This sleeps */ | |
1da177e4 LT |
310 | |
311 | /* | |
312 | * If a new transaction has already done a buffer copy-out, then | |
313 | * we use that version of the data for the commit. | |
314 | */ | |
315 | jbd_lock_bh_state(bh_in); | |
316 | repeat: | |
317 | if (jh_in->b_frozen_data) { | |
318 | done_copy_out = 1; | |
319 | new_page = virt_to_page(jh_in->b_frozen_data); | |
320 | new_offset = offset_in_page(jh_in->b_frozen_data); | |
321 | } else { | |
322 | new_page = jh2bh(jh_in)->b_page; | |
323 | new_offset = offset_in_page(jh2bh(jh_in)->b_data); | |
324 | } | |
325 | ||
326 | mapped_data = kmap_atomic(new_page, KM_USER0); | |
327 | /* | |
328 | * Check for escaping | |
329 | */ | |
330 | if (*((__be32 *)(mapped_data + new_offset)) == | |
331 | cpu_to_be32(JFS_MAGIC_NUMBER)) { | |
332 | need_copy_out = 1; | |
333 | do_escape = 1; | |
334 | } | |
335 | kunmap_atomic(mapped_data, KM_USER0); | |
336 | ||
337 | /* | |
338 | * Do we need to do a data copy? | |
339 | */ | |
340 | if (need_copy_out && !done_copy_out) { | |
341 | char *tmp; | |
342 | ||
343 | jbd_unlock_bh_state(bh_in); | |
c089d490 | 344 | tmp = jbd_alloc(bh_in->b_size, GFP_NOFS); |
1da177e4 LT |
345 | jbd_lock_bh_state(bh_in); |
346 | if (jh_in->b_frozen_data) { | |
c089d490 | 347 | jbd_free(tmp, bh_in->b_size); |
1da177e4 LT |
348 | goto repeat; |
349 | } | |
350 | ||
351 | jh_in->b_frozen_data = tmp; | |
352 | mapped_data = kmap_atomic(new_page, KM_USER0); | |
353 | memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); | |
354 | kunmap_atomic(mapped_data, KM_USER0); | |
355 | ||
356 | new_page = virt_to_page(tmp); | |
357 | new_offset = offset_in_page(tmp); | |
358 | done_copy_out = 1; | |
359 | } | |
360 | ||
361 | /* | |
362 | * Did we need to do an escaping? Now we've done all the | |
363 | * copying, we can finally do so. | |
364 | */ | |
365 | if (do_escape) { | |
366 | mapped_data = kmap_atomic(new_page, KM_USER0); | |
367 | *((unsigned int *)(mapped_data + new_offset)) = 0; | |
368 | kunmap_atomic(mapped_data, KM_USER0); | |
369 | } | |
370 | ||
1da177e4 LT |
371 | set_bh_page(new_bh, new_page, new_offset); |
372 | new_jh->b_transaction = NULL; | |
373 | new_bh->b_size = jh2bh(jh_in)->b_size; | |
374 | new_bh->b_bdev = transaction->t_journal->j_dev; | |
375 | new_bh->b_blocknr = blocknr; | |
376 | set_buffer_mapped(new_bh); | |
377 | set_buffer_dirty(new_bh); | |
378 | ||
379 | *jh_out = new_jh; | |
380 | ||
381 | /* | |
382 | * The to-be-written buffer needs to get moved to the io queue, | |
383 | * and the original buffer whose contents we are shadowing or | |
384 | * copying is moved to the transaction's shadow queue. | |
385 | */ | |
386 | JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); | |
f1015c44 | 387 | spin_lock(&journal->j_list_lock); |
388 | __journal_file_buffer(jh_in, transaction, BJ_Shadow); | |
389 | spin_unlock(&journal->j_list_lock); | |
390 | jbd_unlock_bh_state(bh_in); | |
391 | ||
1da177e4 LT |
392 | JBUFFER_TRACE(new_jh, "file as BJ_IO"); |
393 | journal_file_buffer(new_jh, transaction, BJ_IO); | |
394 | ||
395 | return do_escape | (done_copy_out << 1); | |
396 | } | |
397 | ||
398 | /* | |
399 | * Allocation code for the journal file. Manage the space left in the | |
400 | * journal, so that we can begin checkpointing when appropriate. | |
401 | */ | |
402 | ||
403 | /* | |
404 | * __log_space_left: Return the number of free blocks left in the journal. | |
405 | * | |
406 | * Called with the journal already locked. | |
407 | * | |
408 | * Called under j_state_lock | |
409 | */ | |
410 | ||
411 | int __log_space_left(journal_t *journal) | |
412 | { | |
413 | int left = journal->j_free; | |
414 | ||
415 | assert_spin_locked(&journal->j_state_lock); | |
416 | ||
417 | /* | |
418 | * Be pessimistic here about the number of those free blocks which | |
419 | * might be required for log descriptor control blocks. | |
420 | */ | |
421 | ||
422 | #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ | |
423 | ||
424 | left -= MIN_LOG_RESERVED_BLOCKS; | |
425 | ||
426 | if (left <= 0) | |
427 | return 0; | |
428 | left -= (left >> 3); | |
429 | return left; | |
430 | } | |
431 | ||
432 | /* | |
8fe4cd0d | 433 | * Called under j_state_lock. Returns true if a transaction commit was started. |
1da177e4 LT |
434 | */ |
435 | int __log_start_commit(journal_t *journal, tid_t target) | |
436 | { | |
437 | /* | |
438 | * Are we already doing a recent enough commit? | |
439 | */ | |
440 | if (!tid_geq(journal->j_commit_request, target)) { | |
441 | /* | |
442 | * We want a new commit: OK, mark the request and wakup the | |
443 | * commit thread. We do _not_ do the commit ourselves. | |
444 | */ | |
445 | ||
446 | journal->j_commit_request = target; | |
447 | jbd_debug(1, "JBD: requesting commit %d/%d\n", | |
448 | journal->j_commit_request, | |
449 | journal->j_commit_sequence); | |
450 | wake_up(&journal->j_wait_commit); | |
451 | return 1; | |
452 | } | |
453 | return 0; | |
454 | } | |
455 | ||
456 | int log_start_commit(journal_t *journal, tid_t tid) | |
457 | { | |
458 | int ret; | |
459 | ||
460 | spin_lock(&journal->j_state_lock); | |
461 | ret = __log_start_commit(journal, tid); | |
462 | spin_unlock(&journal->j_state_lock); | |
463 | return ret; | |
464 | } | |
465 | ||
466 | /* | |
467 | * Force and wait upon a commit if the calling process is not within | |
468 | * transaction. This is used for forcing out undo-protected data which contains | |
469 | * bitmaps, when the fs is running out of space. | |
470 | * | |
471 | * We can only force the running transaction if we don't have an active handle; | |
472 | * otherwise, we will deadlock. | |
473 | * | |
474 | * Returns true if a transaction was started. | |
475 | */ | |
476 | int journal_force_commit_nested(journal_t *journal) | |
477 | { | |
478 | transaction_t *transaction = NULL; | |
479 | tid_t tid; | |
480 | ||
481 | spin_lock(&journal->j_state_lock); | |
482 | if (journal->j_running_transaction && !current->journal_info) { | |
483 | transaction = journal->j_running_transaction; | |
484 | __log_start_commit(journal, transaction->t_tid); | |
485 | } else if (journal->j_committing_transaction) | |
486 | transaction = journal->j_committing_transaction; | |
487 | ||
488 | if (!transaction) { | |
489 | spin_unlock(&journal->j_state_lock); | |
490 | return 0; /* Nothing to retry */ | |
491 | } | |
492 | ||
493 | tid = transaction->t_tid; | |
494 | spin_unlock(&journal->j_state_lock); | |
495 | log_wait_commit(journal, tid); | |
496 | return 1; | |
497 | } | |
498 | ||
499 | /* | |
500 | * Start a commit of the current running transaction (if any). Returns true | |
8fe4cd0d JK |
501 | * if a transaction is going to be committed (or is currently already |
502 | * committing), and fills its tid in at *ptid | |
1da177e4 LT |
503 | */ |
504 | int journal_start_commit(journal_t *journal, tid_t *ptid) | |
505 | { | |
506 | int ret = 0; | |
507 | ||
508 | spin_lock(&journal->j_state_lock); | |
509 | if (journal->j_running_transaction) { | |
510 | tid_t tid = journal->j_running_transaction->t_tid; | |
511 | ||
8fe4cd0d JK |
512 | __log_start_commit(journal, tid); |
513 | /* There's a running transaction and we've just made sure | |
514 | * it's commit has been scheduled. */ | |
515 | if (ptid) | |
1da177e4 | 516 | *ptid = tid; |
8fe4cd0d JK |
517 | ret = 1; |
518 | } else if (journal->j_committing_transaction) { | |
1da177e4 LT |
519 | /* |
520 | * If ext3_write_super() recently started a commit, then we | |
521 | * have to wait for completion of that transaction | |
522 | */ | |
8fe4cd0d JK |
523 | if (ptid) |
524 | *ptid = journal->j_committing_transaction->t_tid; | |
1da177e4 LT |
525 | ret = 1; |
526 | } | |
527 | spin_unlock(&journal->j_state_lock); | |
528 | return ret; | |
529 | } | |
530 | ||
531 | /* | |
532 | * Wait for a specified commit to complete. | |
533 | * The caller may not hold the journal lock. | |
534 | */ | |
535 | int log_wait_commit(journal_t *journal, tid_t tid) | |
536 | { | |
537 | int err = 0; | |
538 | ||
539 | #ifdef CONFIG_JBD_DEBUG | |
540 | spin_lock(&journal->j_state_lock); | |
541 | if (!tid_geq(journal->j_commit_request, tid)) { | |
542 | printk(KERN_EMERG | |
543 | "%s: error: j_commit_request=%d, tid=%d\n", | |
08fc99bf | 544 | __func__, journal->j_commit_request, tid); |
1da177e4 LT |
545 | } |
546 | spin_unlock(&journal->j_state_lock); | |
547 | #endif | |
548 | spin_lock(&journal->j_state_lock); | |
549 | while (tid_gt(tid, journal->j_commit_sequence)) { | |
550 | jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", | |
551 | tid, journal->j_commit_sequence); | |
552 | wake_up(&journal->j_wait_commit); | |
553 | spin_unlock(&journal->j_state_lock); | |
554 | wait_event(journal->j_wait_done_commit, | |
555 | !tid_gt(tid, journal->j_commit_sequence)); | |
556 | spin_lock(&journal->j_state_lock); | |
557 | } | |
558 | spin_unlock(&journal->j_state_lock); | |
559 | ||
560 | if (unlikely(is_journal_aborted(journal))) { | |
561 | printk(KERN_EMERG "journal commit I/O error\n"); | |
562 | err = -EIO; | |
563 | } | |
564 | return err; | |
565 | } | |
566 | ||
03f4d804 JK |
567 | /* |
568 | * Return 1 if a given transaction has not yet sent barrier request | |
569 | * connected with a transaction commit. If 0 is returned, transaction | |
570 | * may or may not have sent the barrier. Used to avoid sending barrier | |
571 | * twice in common cases. | |
572 | */ | |
573 | int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid) | |
574 | { | |
575 | int ret = 0; | |
576 | transaction_t *commit_trans; | |
577 | ||
578 | if (!(journal->j_flags & JFS_BARRIER)) | |
579 | return 0; | |
580 | spin_lock(&journal->j_state_lock); | |
581 | /* Transaction already committed? */ | |
582 | if (tid_geq(journal->j_commit_sequence, tid)) | |
583 | goto out; | |
584 | /* | |
585 | * Transaction is being committed and we already proceeded to | |
586 | * writing commit record? | |
587 | */ | |
588 | commit_trans = journal->j_committing_transaction; | |
589 | if (commit_trans && commit_trans->t_tid == tid && | |
590 | commit_trans->t_state >= T_COMMIT_RECORD) | |
591 | goto out; | |
592 | ret = 1; | |
593 | out: | |
594 | spin_unlock(&journal->j_state_lock); | |
595 | return ret; | |
596 | } | |
52779708 | 597 | EXPORT_SYMBOL(journal_trans_will_send_data_barrier); |
03f4d804 | 598 | |
1da177e4 LT |
599 | /* |
600 | * Log buffer allocation routines: | |
601 | */ | |
602 | ||
9c28cbcc | 603 | int journal_next_log_block(journal_t *journal, unsigned int *retp) |
1da177e4 | 604 | { |
9c28cbcc | 605 | unsigned int blocknr; |
1da177e4 LT |
606 | |
607 | spin_lock(&journal->j_state_lock); | |
608 | J_ASSERT(journal->j_free > 1); | |
609 | ||
610 | blocknr = journal->j_head; | |
611 | journal->j_head++; | |
612 | journal->j_free--; | |
613 | if (journal->j_head == journal->j_last) | |
614 | journal->j_head = journal->j_first; | |
615 | spin_unlock(&journal->j_state_lock); | |
616 | return journal_bmap(journal, blocknr, retp); | |
617 | } | |
618 | ||
619 | /* | |
620 | * Conversion of logical to physical block numbers for the journal | |
621 | * | |
622 | * On external journals the journal blocks are identity-mapped, so | |
623 | * this is a no-op. If needed, we can use j_blk_offset - everything is | |
624 | * ready. | |
625 | */ | |
9c28cbcc JK |
626 | int journal_bmap(journal_t *journal, unsigned int blocknr, |
627 | unsigned int *retp) | |
1da177e4 LT |
628 | { |
629 | int err = 0; | |
9c28cbcc | 630 | unsigned int ret; |
1da177e4 LT |
631 | |
632 | if (journal->j_inode) { | |
633 | ret = bmap(journal->j_inode, blocknr); | |
634 | if (ret) | |
635 | *retp = ret; | |
636 | else { | |
637 | char b[BDEVNAME_SIZE]; | |
638 | ||
639 | printk(KERN_ALERT "%s: journal block not found " | |
9c28cbcc | 640 | "at offset %u on %s\n", |
08fc99bf | 641 | __func__, |
1da177e4 LT |
642 | blocknr, |
643 | bdevname(journal->j_dev, b)); | |
644 | err = -EIO; | |
645 | __journal_abort_soft(journal, err); | |
646 | } | |
647 | } else { | |
648 | *retp = blocknr; /* +journal->j_blk_offset */ | |
649 | } | |
650 | return err; | |
651 | } | |
652 | ||
653 | /* | |
654 | * We play buffer_head aliasing tricks to write data/metadata blocks to | |
655 | * the journal without copying their contents, but for journal | |
656 | * descriptor blocks we do need to generate bona fide buffers. | |
657 | * | |
658 | * After the caller of journal_get_descriptor_buffer() has finished modifying | |
659 | * the buffer's contents they really should run flush_dcache_page(bh->b_page). | |
660 | * But we don't bother doing that, so there will be coherency problems with | |
661 | * mmaps of blockdevs which hold live JBD-controlled filesystems. | |
662 | */ | |
663 | struct journal_head *journal_get_descriptor_buffer(journal_t *journal) | |
664 | { | |
665 | struct buffer_head *bh; | |
9c28cbcc | 666 | unsigned int blocknr; |
1da177e4 LT |
667 | int err; |
668 | ||
669 | err = journal_next_log_block(journal, &blocknr); | |
670 | ||
671 | if (err) | |
672 | return NULL; | |
673 | ||
674 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | |
ecca9af0 JK |
675 | if (!bh) |
676 | return NULL; | |
1da177e4 LT |
677 | lock_buffer(bh); |
678 | memset(bh->b_data, 0, journal->j_blocksize); | |
679 | set_buffer_uptodate(bh); | |
680 | unlock_buffer(bh); | |
681 | BUFFER_TRACE(bh, "return this buffer"); | |
682 | return journal_add_journal_head(bh); | |
683 | } | |
684 | ||
685 | /* | |
686 | * Management for journal control blocks: functions to create and | |
687 | * destroy journal_t structures, and to initialise and read existing | |
688 | * journal blocks from disk. */ | |
689 | ||
690 | /* First: create and setup a journal_t object in memory. We initialise | |
691 | * very few fields yet: that has to wait until we have created the | |
692 | * journal structures from from scratch, or loaded them from disk. */ | |
693 | ||
694 | static journal_t * journal_init_common (void) | |
695 | { | |
696 | journal_t *journal; | |
697 | int err; | |
698 | ||
8c3478a5 | 699 | journal = kzalloc(sizeof(*journal), GFP_KERNEL); |
1da177e4 LT |
700 | if (!journal) |
701 | goto fail; | |
1da177e4 LT |
702 | |
703 | init_waitqueue_head(&journal->j_wait_transaction_locked); | |
704 | init_waitqueue_head(&journal->j_wait_logspace); | |
705 | init_waitqueue_head(&journal->j_wait_done_commit); | |
706 | init_waitqueue_head(&journal->j_wait_checkpoint); | |
707 | init_waitqueue_head(&journal->j_wait_commit); | |
708 | init_waitqueue_head(&journal->j_wait_updates); | |
2c68ee75 AV |
709 | mutex_init(&journal->j_barrier); |
710 | mutex_init(&journal->j_checkpoint_mutex); | |
1da177e4 LT |
711 | spin_lock_init(&journal->j_revoke_lock); |
712 | spin_lock_init(&journal->j_list_lock); | |
713 | spin_lock_init(&journal->j_state_lock); | |
714 | ||
715 | journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE); | |
716 | ||
717 | /* The journal is marked for error until we succeed with recovery! */ | |
718 | journal->j_flags = JFS_ABORT; | |
719 | ||
720 | /* Set up a default-sized revoke table for the new mount. */ | |
721 | err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); | |
722 | if (err) { | |
723 | kfree(journal); | |
724 | goto fail; | |
725 | } | |
726 | return journal; | |
727 | fail: | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | /* journal_init_dev and journal_init_inode: | |
732 | * | |
733 | * Create a journal structure assigned some fixed set of disk blocks to | |
734 | * the journal. We don't actually touch those disk blocks yet, but we | |
735 | * need to set up all of the mapping information to tell the journaling | |
736 | * system where the journal blocks are. | |
737 | * | |
738 | */ | |
739 | ||
740 | /** | |
0cf01f66 | 741 | * journal_t * journal_init_dev() - creates and initialises a journal structure |
1da177e4 LT |
742 | * @bdev: Block device on which to create the journal |
743 | * @fs_dev: Device which hold journalled filesystem for this journal. | |
744 | * @start: Block nr Start of journal. | |
37ed3222 | 745 | * @len: Length of the journal in blocks. |
1da177e4 | 746 | * @blocksize: blocksize of journalling device |
0cf01f66 RD |
747 | * |
748 | * Returns: a newly created journal_t * | |
ae6ddcc5 | 749 | * |
1da177e4 LT |
750 | * journal_init_dev creates a journal which maps a fixed contiguous |
751 | * range of blocks on an arbitrary block device. | |
ae6ddcc5 | 752 | * |
1da177e4 LT |
753 | */ |
754 | journal_t * journal_init_dev(struct block_device *bdev, | |
755 | struct block_device *fs_dev, | |
756 | int start, int len, int blocksize) | |
757 | { | |
758 | journal_t *journal = journal_init_common(); | |
759 | struct buffer_head *bh; | |
760 | int n; | |
761 | ||
762 | if (!journal) | |
763 | return NULL; | |
764 | ||
1da177e4 | 765 | /* journal descriptor can store up to n blocks -bzzz */ |
d1807793 | 766 | journal->j_blocksize = blocksize; |
1da177e4 LT |
767 | n = journal->j_blocksize / sizeof(journal_block_tag_t); |
768 | journal->j_wbufsize = n; | |
769 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); | |
770 | if (!journal->j_wbuf) { | |
771 | printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", | |
08fc99bf | 772 | __func__); |
ecca9af0 | 773 | goto out_err; |
1da177e4 | 774 | } |
d1807793 ZM |
775 | journal->j_dev = bdev; |
776 | journal->j_fs_dev = fs_dev; | |
777 | journal->j_blk_offset = start; | |
778 | journal->j_maxlen = len; | |
779 | ||
780 | bh = __getblk(journal->j_dev, start, journal->j_blocksize); | |
ecca9af0 JK |
781 | if (!bh) { |
782 | printk(KERN_ERR | |
783 | "%s: Cannot get buffer for journal superblock\n", | |
784 | __func__); | |
785 | goto out_err; | |
786 | } | |
d1807793 ZM |
787 | journal->j_sb_buffer = bh; |
788 | journal->j_superblock = (journal_superblock_t *)bh->b_data; | |
ecca9af0 | 789 | |
1da177e4 | 790 | return journal; |
ecca9af0 | 791 | out_err: |
7b02bec0 | 792 | kfree(journal->j_wbuf); |
ecca9af0 JK |
793 | kfree(journal); |
794 | return NULL; | |
1da177e4 | 795 | } |
ae6ddcc5 MC |
796 | |
797 | /** | |
1da177e4 LT |
798 | * journal_t * journal_init_inode () - creates a journal which maps to a inode. |
799 | * @inode: An inode to create the journal in | |
ae6ddcc5 | 800 | * |
1da177e4 LT |
801 | * journal_init_inode creates a journal which maps an on-disk inode as |
802 | * the journal. The inode must exist already, must support bmap() and | |
803 | * must have all data blocks preallocated. | |
804 | */ | |
805 | journal_t * journal_init_inode (struct inode *inode) | |
806 | { | |
807 | struct buffer_head *bh; | |
808 | journal_t *journal = journal_init_common(); | |
809 | int err; | |
810 | int n; | |
9c28cbcc | 811 | unsigned int blocknr; |
1da177e4 LT |
812 | |
813 | if (!journal) | |
814 | return NULL; | |
815 | ||
816 | journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; | |
817 | journal->j_inode = inode; | |
818 | jbd_debug(1, | |
819 | "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", | |
ae6ddcc5 | 820 | journal, inode->i_sb->s_id, inode->i_ino, |
1da177e4 LT |
821 | (long long) inode->i_size, |
822 | inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); | |
823 | ||
824 | journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; | |
825 | journal->j_blocksize = inode->i_sb->s_blocksize; | |
826 | ||
827 | /* journal descriptor can store up to n blocks -bzzz */ | |
828 | n = journal->j_blocksize / sizeof(journal_block_tag_t); | |
829 | journal->j_wbufsize = n; | |
830 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); | |
831 | if (!journal->j_wbuf) { | |
832 | printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", | |
08fc99bf | 833 | __func__); |
ecca9af0 | 834 | goto out_err; |
1da177e4 LT |
835 | } |
836 | ||
837 | err = journal_bmap(journal, 0, &blocknr); | |
838 | /* If that failed, give up */ | |
839 | if (err) { | |
840 | printk(KERN_ERR "%s: Cannnot locate journal superblock\n", | |
08fc99bf | 841 | __func__); |
ecca9af0 | 842 | goto out_err; |
1da177e4 LT |
843 | } |
844 | ||
845 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | |
ecca9af0 JK |
846 | if (!bh) { |
847 | printk(KERN_ERR | |
848 | "%s: Cannot get buffer for journal superblock\n", | |
849 | __func__); | |
850 | goto out_err; | |
851 | } | |
1da177e4 LT |
852 | journal->j_sb_buffer = bh; |
853 | journal->j_superblock = (journal_superblock_t *)bh->b_data; | |
854 | ||
855 | return journal; | |
ecca9af0 | 856 | out_err: |
7b02bec0 | 857 | kfree(journal->j_wbuf); |
ecca9af0 JK |
858 | kfree(journal); |
859 | return NULL; | |
1da177e4 LT |
860 | } |
861 | ||
ae6ddcc5 | 862 | /* |
1da177e4 LT |
863 | * If the journal init or create aborts, we need to mark the journal |
864 | * superblock as being NULL to prevent the journal destroy from writing | |
ae6ddcc5 | 865 | * back a bogus superblock. |
1da177e4 LT |
866 | */ |
867 | static void journal_fail_superblock (journal_t *journal) | |
868 | { | |
869 | struct buffer_head *bh = journal->j_sb_buffer; | |
870 | brelse(bh); | |
871 | journal->j_sb_buffer = NULL; | |
872 | } | |
873 | ||
874 | /* | |
875 | * Given a journal_t structure, initialise the various fields for | |
876 | * startup of a new journaling session. We use this both when creating | |
877 | * a journal, and after recovering an old journal to reset it for | |
878 | * subsequent use. | |
879 | */ | |
880 | ||
881 | static int journal_reset(journal_t *journal) | |
882 | { | |
883 | journal_superblock_t *sb = journal->j_superblock; | |
9c28cbcc | 884 | unsigned int first, last; |
1da177e4 LT |
885 | |
886 | first = be32_to_cpu(sb->s_first); | |
887 | last = be32_to_cpu(sb->s_maxlen); | |
7447a668 | 888 | if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) { |
9c28cbcc | 889 | printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n", |
7447a668 JK |
890 | first, last); |
891 | journal_fail_superblock(journal); | |
892 | return -EINVAL; | |
893 | } | |
1da177e4 LT |
894 | |
895 | journal->j_first = first; | |
896 | journal->j_last = last; | |
897 | ||
898 | journal->j_head = first; | |
899 | journal->j_tail = first; | |
900 | journal->j_free = last - first; | |
901 | ||
902 | journal->j_tail_sequence = journal->j_transaction_sequence; | |
903 | journal->j_commit_sequence = journal->j_transaction_sequence - 1; | |
904 | journal->j_commit_request = journal->j_commit_sequence; | |
905 | ||
906 | journal->j_max_transaction_buffers = journal->j_maxlen / 4; | |
907 | ||
908 | /* Add the dynamic fields and write it to disk. */ | |
909 | journal_update_superblock(journal, 1); | |
97f06784 | 910 | return journal_start_thread(journal); |
1da177e4 LT |
911 | } |
912 | ||
ae6ddcc5 | 913 | /** |
1da177e4 LT |
914 | * int journal_create() - Initialise the new journal file |
915 | * @journal: Journal to create. This structure must have been initialised | |
ae6ddcc5 | 916 | * |
1da177e4 LT |
917 | * Given a journal_t structure which tells us which disk blocks we can |
918 | * use, create a new journal superblock and initialise all of the | |
ae6ddcc5 | 919 | * journal fields from scratch. |
1da177e4 LT |
920 | **/ |
921 | int journal_create(journal_t *journal) | |
922 | { | |
9c28cbcc | 923 | unsigned int blocknr; |
1da177e4 LT |
924 | struct buffer_head *bh; |
925 | journal_superblock_t *sb; | |
926 | int i, err; | |
927 | ||
928 | if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) { | |
929 | printk (KERN_ERR "Journal length (%d blocks) too short.\n", | |
930 | journal->j_maxlen); | |
931 | journal_fail_superblock(journal); | |
932 | return -EINVAL; | |
933 | } | |
934 | ||
935 | if (journal->j_inode == NULL) { | |
936 | /* | |
937 | * We don't know what block to start at! | |
938 | */ | |
939 | printk(KERN_EMERG | |
940 | "%s: creation of journal on external device!\n", | |
08fc99bf | 941 | __func__); |
1da177e4 LT |
942 | BUG(); |
943 | } | |
944 | ||
945 | /* Zero out the entire journal on disk. We cannot afford to | |
946 | have any blocks on disk beginning with JFS_MAGIC_NUMBER. */ | |
947 | jbd_debug(1, "JBD: Zeroing out journal blocks...\n"); | |
948 | for (i = 0; i < journal->j_maxlen; i++) { | |
949 | err = journal_bmap(journal, i, &blocknr); | |
950 | if (err) | |
951 | return err; | |
952 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | |
953 | lock_buffer(bh); | |
954 | memset (bh->b_data, 0, journal->j_blocksize); | |
955 | BUFFER_TRACE(bh, "marking dirty"); | |
956 | mark_buffer_dirty(bh); | |
957 | BUFFER_TRACE(bh, "marking uptodate"); | |
958 | set_buffer_uptodate(bh); | |
959 | unlock_buffer(bh); | |
960 | __brelse(bh); | |
961 | } | |
962 | ||
963 | sync_blockdev(journal->j_dev); | |
964 | jbd_debug(1, "JBD: journal cleared.\n"); | |
965 | ||
966 | /* OK, fill in the initial static fields in the new superblock */ | |
967 | sb = journal->j_superblock; | |
968 | ||
969 | sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER); | |
970 | sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2); | |
971 | ||
972 | sb->s_blocksize = cpu_to_be32(journal->j_blocksize); | |
973 | sb->s_maxlen = cpu_to_be32(journal->j_maxlen); | |
974 | sb->s_first = cpu_to_be32(1); | |
975 | ||
976 | journal->j_transaction_sequence = 1; | |
977 | ||
978 | journal->j_flags &= ~JFS_ABORT; | |
979 | journal->j_format_version = 2; | |
980 | ||
981 | return journal_reset(journal); | |
982 | } | |
983 | ||
ae6ddcc5 | 984 | /** |
1da177e4 LT |
985 | * void journal_update_superblock() - Update journal sb on disk. |
986 | * @journal: The journal to update. | |
987 | * @wait: Set to '0' if you don't want to wait for IO completion. | |
988 | * | |
989 | * Update a journal's dynamic superblock fields and write it to disk, | |
990 | * optionally waiting for the IO to complete. | |
991 | */ | |
992 | void journal_update_superblock(journal_t *journal, int wait) | |
993 | { | |
994 | journal_superblock_t *sb = journal->j_superblock; | |
995 | struct buffer_head *bh = journal->j_sb_buffer; | |
996 | ||
997 | /* | |
998 | * As a special case, if the on-disk copy is already marked as needing | |
999 | * no recovery (s_start == 0) and there are no outstanding transactions | |
1000 | * in the filesystem, then we can safely defer the superblock update | |
1001 | * until the next commit by setting JFS_FLUSHED. This avoids | |
1002 | * attempting a write to a potential-readonly device. | |
1003 | */ | |
1004 | if (sb->s_start == 0 && journal->j_tail_sequence == | |
1005 | journal->j_transaction_sequence) { | |
1006 | jbd_debug(1,"JBD: Skipping superblock update on recovered sb " | |
9c28cbcc | 1007 | "(start %u, seq %d, errno %d)\n", |
ae6ddcc5 | 1008 | journal->j_tail, journal->j_tail_sequence, |
1da177e4 LT |
1009 | journal->j_errno); |
1010 | goto out; | |
1011 | } | |
1012 | ||
1013 | spin_lock(&journal->j_state_lock); | |
9c28cbcc | 1014 | jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n", |
1da177e4 LT |
1015 | journal->j_tail, journal->j_tail_sequence, journal->j_errno); |
1016 | ||
1017 | sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); | |
1018 | sb->s_start = cpu_to_be32(journal->j_tail); | |
1019 | sb->s_errno = cpu_to_be32(journal->j_errno); | |
1020 | spin_unlock(&journal->j_state_lock); | |
1021 | ||
1022 | BUFFER_TRACE(bh, "marking dirty"); | |
1023 | mark_buffer_dirty(bh); | |
1024 | if (wait) | |
1025 | sync_dirty_buffer(bh); | |
1026 | else | |
9cb569d6 | 1027 | write_dirty_buffer(bh, WRITE); |
1da177e4 LT |
1028 | |
1029 | out: | |
1030 | /* If we have just flushed the log (by marking s_start==0), then | |
1031 | * any future commit will have to be careful to update the | |
1032 | * superblock again to re-record the true start of the log. */ | |
1033 | ||
1034 | spin_lock(&journal->j_state_lock); | |
1035 | if (sb->s_start) | |
1036 | journal->j_flags &= ~JFS_FLUSHED; | |
1037 | else | |
1038 | journal->j_flags |= JFS_FLUSHED; | |
1039 | spin_unlock(&journal->j_state_lock); | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * Read the superblock for a given journal, performing initial | |
1044 | * validation of the format. | |
1045 | */ | |
1046 | ||
1047 | static int journal_get_superblock(journal_t *journal) | |
1048 | { | |
1049 | struct buffer_head *bh; | |
1050 | journal_superblock_t *sb; | |
1051 | int err = -EIO; | |
1052 | ||
1053 | bh = journal->j_sb_buffer; | |
1054 | ||
1055 | J_ASSERT(bh != NULL); | |
1056 | if (!buffer_uptodate(bh)) { | |
1057 | ll_rw_block(READ, 1, &bh); | |
1058 | wait_on_buffer(bh); | |
1059 | if (!buffer_uptodate(bh)) { | |
1060 | printk (KERN_ERR | |
1061 | "JBD: IO error reading journal superblock\n"); | |
1062 | goto out; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | sb = journal->j_superblock; | |
1067 | ||
1068 | err = -EINVAL; | |
1069 | ||
1070 | if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) || | |
1071 | sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { | |
1072 | printk(KERN_WARNING "JBD: no valid journal superblock found\n"); | |
1073 | goto out; | |
1074 | } | |
1075 | ||
1076 | switch(be32_to_cpu(sb->s_header.h_blocktype)) { | |
1077 | case JFS_SUPERBLOCK_V1: | |
1078 | journal->j_format_version = 1; | |
1079 | break; | |
1080 | case JFS_SUPERBLOCK_V2: | |
1081 | journal->j_format_version = 2; | |
1082 | break; | |
1083 | default: | |
1084 | printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); | |
1085 | goto out; | |
1086 | } | |
1087 | ||
1088 | if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) | |
1089 | journal->j_maxlen = be32_to_cpu(sb->s_maxlen); | |
1090 | else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { | |
1091 | printk (KERN_WARNING "JBD: journal file too short\n"); | |
1092 | goto out; | |
1093 | } | |
1094 | ||
1095 | return 0; | |
1096 | ||
1097 | out: | |
1098 | journal_fail_superblock(journal); | |
1099 | return err; | |
1100 | } | |
1101 | ||
1102 | /* | |
1103 | * Load the on-disk journal superblock and read the key fields into the | |
1104 | * journal_t. | |
1105 | */ | |
1106 | ||
1107 | static int load_superblock(journal_t *journal) | |
1108 | { | |
1109 | int err; | |
1110 | journal_superblock_t *sb; | |
1111 | ||
1112 | err = journal_get_superblock(journal); | |
1113 | if (err) | |
1114 | return err; | |
1115 | ||
1116 | sb = journal->j_superblock; | |
1117 | ||
1118 | journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); | |
1119 | journal->j_tail = be32_to_cpu(sb->s_start); | |
1120 | journal->j_first = be32_to_cpu(sb->s_first); | |
1121 | journal->j_last = be32_to_cpu(sb->s_maxlen); | |
1122 | journal->j_errno = be32_to_cpu(sb->s_errno); | |
1123 | ||
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | ||
1128 | /** | |
1129 | * int journal_load() - Read journal from disk. | |
1130 | * @journal: Journal to act on. | |
ae6ddcc5 | 1131 | * |
1da177e4 LT |
1132 | * Given a journal_t structure which tells us which disk blocks contain |
1133 | * a journal, read the journal from disk to initialise the in-memory | |
1134 | * structures. | |
1135 | */ | |
1136 | int journal_load(journal_t *journal) | |
1137 | { | |
1138 | int err; | |
ea817398 | 1139 | journal_superblock_t *sb; |
1da177e4 LT |
1140 | |
1141 | err = load_superblock(journal); | |
1142 | if (err) | |
1143 | return err; | |
1144 | ||
ea817398 | 1145 | sb = journal->j_superblock; |
1da177e4 LT |
1146 | /* If this is a V2 superblock, then we have to check the |
1147 | * features flags on it. */ | |
1148 | ||
1149 | if (journal->j_format_version >= 2) { | |
1da177e4 LT |
1150 | if ((sb->s_feature_ro_compat & |
1151 | ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) || | |
1152 | (sb->s_feature_incompat & | |
1153 | ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) { | |
1154 | printk (KERN_WARNING | |
1155 | "JBD: Unrecognised features on journal\n"); | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | } | |
1159 | ||
1160 | /* Let the recovery code check whether it needs to recover any | |
1161 | * data from the journal. */ | |
1162 | if (journal_recover(journal)) | |
1163 | goto recovery_error; | |
1164 | ||
1165 | /* OK, we've finished with the dynamic journal bits: | |
1166 | * reinitialise the dynamic contents of the superblock in memory | |
1167 | * and reset them on disk. */ | |
1168 | if (journal_reset(journal)) | |
1169 | goto recovery_error; | |
1170 | ||
1171 | journal->j_flags &= ~JFS_ABORT; | |
1172 | journal->j_flags |= JFS_LOADED; | |
1173 | return 0; | |
1174 | ||
1175 | recovery_error: | |
1176 | printk (KERN_WARNING "JBD: recovery failed\n"); | |
1177 | return -EIO; | |
1178 | } | |
1179 | ||
1180 | /** | |
1181 | * void journal_destroy() - Release a journal_t structure. | |
1182 | * @journal: Journal to act on. | |
1183 | * | |
1184 | * Release a journal_t structure once it is no longer in use by the | |
1185 | * journaled object. | |
4afe9785 | 1186 | * Return <0 if we couldn't clean up the journal. |
1da177e4 | 1187 | */ |
4afe9785 | 1188 | int journal_destroy(journal_t *journal) |
1da177e4 | 1189 | { |
4afe9785 HK |
1190 | int err = 0; |
1191 | ||
03f4d804 | 1192 | |
1da177e4 LT |
1193 | /* Wait for the commit thread to wake up and die. */ |
1194 | journal_kill_thread(journal); | |
1195 | ||
1196 | /* Force a final log commit */ | |
1197 | if (journal->j_running_transaction) | |
1198 | journal_commit_transaction(journal); | |
1199 | ||
1200 | /* Force any old transactions to disk */ | |
1201 | ||
1202 | /* Totally anal locking here... */ | |
1203 | spin_lock(&journal->j_list_lock); | |
1204 | while (journal->j_checkpoint_transactions != NULL) { | |
1205 | spin_unlock(&journal->j_list_lock); | |
1206 | log_do_checkpoint(journal); | |
1207 | spin_lock(&journal->j_list_lock); | |
1208 | } | |
1209 | ||
1210 | J_ASSERT(journal->j_running_transaction == NULL); | |
1211 | J_ASSERT(journal->j_committing_transaction == NULL); | |
1212 | J_ASSERT(journal->j_checkpoint_transactions == NULL); | |
1213 | spin_unlock(&journal->j_list_lock); | |
1214 | ||
1da177e4 | 1215 | if (journal->j_sb_buffer) { |
4afe9785 HK |
1216 | if (!is_journal_aborted(journal)) { |
1217 | /* We can now mark the journal as empty. */ | |
1218 | journal->j_tail = 0; | |
1219 | journal->j_tail_sequence = | |
1220 | ++journal->j_transaction_sequence; | |
1221 | journal_update_superblock(journal, 1); | |
1222 | } else { | |
1223 | err = -EIO; | |
1224 | } | |
1da177e4 LT |
1225 | brelse(journal->j_sb_buffer); |
1226 | } | |
1227 | ||
1228 | if (journal->j_inode) | |
1229 | iput(journal->j_inode); | |
1230 | if (journal->j_revoke) | |
1231 | journal_destroy_revoke(journal); | |
1232 | kfree(journal->j_wbuf); | |
1233 | kfree(journal); | |
4afe9785 HK |
1234 | |
1235 | return err; | |
1da177e4 LT |
1236 | } |
1237 | ||
1238 | ||
1239 | /** | |
1240 | *int journal_check_used_features () - Check if features specified are used. | |
1241 | * @journal: Journal to check. | |
1242 | * @compat: bitmask of compatible features | |
1243 | * @ro: bitmask of features that force read-only mount | |
1244 | * @incompat: bitmask of incompatible features | |
ae6ddcc5 | 1245 | * |
1da177e4 | 1246 | * Check whether the journal uses all of a given set of |
ae6ddcc5 | 1247 | * features. Return true (non-zero) if it does. |
1da177e4 LT |
1248 | **/ |
1249 | ||
1250 | int journal_check_used_features (journal_t *journal, unsigned long compat, | |
1251 | unsigned long ro, unsigned long incompat) | |
1252 | { | |
1253 | journal_superblock_t *sb; | |
1254 | ||
1255 | if (!compat && !ro && !incompat) | |
1256 | return 1; | |
1257 | if (journal->j_format_version == 1) | |
1258 | return 0; | |
1259 | ||
1260 | sb = journal->j_superblock; | |
1261 | ||
1262 | if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && | |
1263 | ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && | |
1264 | ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) | |
1265 | return 1; | |
1266 | ||
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | /** | |
1271 | * int journal_check_available_features() - Check feature set in journalling layer | |
1272 | * @journal: Journal to check. | |
1273 | * @compat: bitmask of compatible features | |
1274 | * @ro: bitmask of features that force read-only mount | |
1275 | * @incompat: bitmask of incompatible features | |
ae6ddcc5 | 1276 | * |
1da177e4 LT |
1277 | * Check whether the journaling code supports the use of |
1278 | * all of a given set of features on this journal. Return true | |
1279 | * (non-zero) if it can. */ | |
1280 | ||
1281 | int journal_check_available_features (journal_t *journal, unsigned long compat, | |
1282 | unsigned long ro, unsigned long incompat) | |
1283 | { | |
1da177e4 LT |
1284 | if (!compat && !ro && !incompat) |
1285 | return 1; | |
1286 | ||
1da177e4 LT |
1287 | /* We can support any known requested features iff the |
1288 | * superblock is in version 2. Otherwise we fail to support any | |
1289 | * extended sb features. */ | |
1290 | ||
1291 | if (journal->j_format_version != 2) | |
1292 | return 0; | |
1293 | ||
1294 | if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat && | |
1295 | (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro && | |
1296 | (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat) | |
1297 | return 1; | |
1298 | ||
1299 | return 0; | |
1300 | } | |
1301 | ||
1302 | /** | |
1303 | * int journal_set_features () - Mark a given journal feature in the superblock | |
1304 | * @journal: Journal to act on. | |
1305 | * @compat: bitmask of compatible features | |
1306 | * @ro: bitmask of features that force read-only mount | |
1307 | * @incompat: bitmask of incompatible features | |
1308 | * | |
1309 | * Mark a given journal feature as present on the | |
ae6ddcc5 | 1310 | * superblock. Returns true if the requested features could be set. |
1da177e4 LT |
1311 | * |
1312 | */ | |
1313 | ||
1314 | int journal_set_features (journal_t *journal, unsigned long compat, | |
1315 | unsigned long ro, unsigned long incompat) | |
1316 | { | |
1317 | journal_superblock_t *sb; | |
1318 | ||
1319 | if (journal_check_used_features(journal, compat, ro, incompat)) | |
1320 | return 1; | |
1321 | ||
1322 | if (!journal_check_available_features(journal, compat, ro, incompat)) | |
1323 | return 0; | |
1324 | ||
1325 | jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", | |
1326 | compat, ro, incompat); | |
1327 | ||
1328 | sb = journal->j_superblock; | |
1329 | ||
1330 | sb->s_feature_compat |= cpu_to_be32(compat); | |
1331 | sb->s_feature_ro_compat |= cpu_to_be32(ro); | |
1332 | sb->s_feature_incompat |= cpu_to_be32(incompat); | |
1333 | ||
1334 | return 1; | |
1335 | } | |
1336 | ||
1337 | ||
1338 | /** | |
1339 | * int journal_update_format () - Update on-disk journal structure. | |
1340 | * @journal: Journal to act on. | |
1341 | * | |
1342 | * Given an initialised but unloaded journal struct, poke about in the | |
1343 | * on-disk structure to update it to the most recent supported version. | |
1344 | */ | |
1345 | int journal_update_format (journal_t *journal) | |
1346 | { | |
1347 | journal_superblock_t *sb; | |
1348 | int err; | |
1349 | ||
1350 | err = journal_get_superblock(journal); | |
1351 | if (err) | |
1352 | return err; | |
1353 | ||
1354 | sb = journal->j_superblock; | |
1355 | ||
1356 | switch (be32_to_cpu(sb->s_header.h_blocktype)) { | |
1357 | case JFS_SUPERBLOCK_V2: | |
1358 | return 0; | |
1359 | case JFS_SUPERBLOCK_V1: | |
1360 | return journal_convert_superblock_v1(journal, sb); | |
1361 | default: | |
1362 | break; | |
1363 | } | |
1364 | return -EINVAL; | |
1365 | } | |
1366 | ||
1367 | static int journal_convert_superblock_v1(journal_t *journal, | |
1368 | journal_superblock_t *sb) | |
1369 | { | |
1370 | int offset, blocksize; | |
1371 | struct buffer_head *bh; | |
1372 | ||
1373 | printk(KERN_WARNING | |
1374 | "JBD: Converting superblock from version 1 to 2.\n"); | |
1375 | ||
1376 | /* Pre-initialise new fields to zero */ | |
1377 | offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); | |
1378 | blocksize = be32_to_cpu(sb->s_blocksize); | |
1379 | memset(&sb->s_feature_compat, 0, blocksize-offset); | |
1380 | ||
1381 | sb->s_nr_users = cpu_to_be32(1); | |
1382 | sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2); | |
1383 | journal->j_format_version = 2; | |
1384 | ||
1385 | bh = journal->j_sb_buffer; | |
1386 | BUFFER_TRACE(bh, "marking dirty"); | |
1387 | mark_buffer_dirty(bh); | |
1388 | sync_dirty_buffer(bh); | |
1389 | return 0; | |
1390 | } | |
1391 | ||
1392 | ||
1393 | /** | |
1394 | * int journal_flush () - Flush journal | |
1395 | * @journal: Journal to act on. | |
ae6ddcc5 | 1396 | * |
1da177e4 LT |
1397 | * Flush all data for a given journal to disk and empty the journal. |
1398 | * Filesystems can use this when remounting readonly to ensure that | |
1399 | * recovery does not need to happen on remount. | |
1400 | */ | |
1401 | ||
1402 | int journal_flush(journal_t *journal) | |
1403 | { | |
1404 | int err = 0; | |
1405 | transaction_t *transaction = NULL; | |
9c28cbcc | 1406 | unsigned int old_tail; |
1da177e4 LT |
1407 | |
1408 | spin_lock(&journal->j_state_lock); | |
1409 | ||
1410 | /* Force everything buffered to the log... */ | |
1411 | if (journal->j_running_transaction) { | |
1412 | transaction = journal->j_running_transaction; | |
1413 | __log_start_commit(journal, transaction->t_tid); | |
1414 | } else if (journal->j_committing_transaction) | |
1415 | transaction = journal->j_committing_transaction; | |
1416 | ||
1417 | /* Wait for the log commit to complete... */ | |
1418 | if (transaction) { | |
1419 | tid_t tid = transaction->t_tid; | |
1420 | ||
1421 | spin_unlock(&journal->j_state_lock); | |
1422 | log_wait_commit(journal, tid); | |
1423 | } else { | |
1424 | spin_unlock(&journal->j_state_lock); | |
1425 | } | |
1426 | ||
1427 | /* ...and flush everything in the log out to disk. */ | |
1428 | spin_lock(&journal->j_list_lock); | |
1429 | while (!err && journal->j_checkpoint_transactions != NULL) { | |
1430 | spin_unlock(&journal->j_list_lock); | |
4afe9785 | 1431 | mutex_lock(&journal->j_checkpoint_mutex); |
1da177e4 | 1432 | err = log_do_checkpoint(journal); |
4afe9785 | 1433 | mutex_unlock(&journal->j_checkpoint_mutex); |
1da177e4 LT |
1434 | spin_lock(&journal->j_list_lock); |
1435 | } | |
1436 | spin_unlock(&journal->j_list_lock); | |
4afe9785 HK |
1437 | |
1438 | if (is_journal_aborted(journal)) | |
1439 | return -EIO; | |
1440 | ||
1da177e4 LT |
1441 | cleanup_journal_tail(journal); |
1442 | ||
1443 | /* Finally, mark the journal as really needing no recovery. | |
1444 | * This sets s_start==0 in the underlying superblock, which is | |
1445 | * the magic code for a fully-recovered superblock. Any future | |
1446 | * commits of data to the journal will restore the current | |
1447 | * s_start value. */ | |
1448 | spin_lock(&journal->j_state_lock); | |
1449 | old_tail = journal->j_tail; | |
1450 | journal->j_tail = 0; | |
1451 | spin_unlock(&journal->j_state_lock); | |
1452 | journal_update_superblock(journal, 1); | |
1453 | spin_lock(&journal->j_state_lock); | |
1454 | journal->j_tail = old_tail; | |
1455 | ||
1456 | J_ASSERT(!journal->j_running_transaction); | |
1457 | J_ASSERT(!journal->j_committing_transaction); | |
1458 | J_ASSERT(!journal->j_checkpoint_transactions); | |
1459 | J_ASSERT(journal->j_head == journal->j_tail); | |
1460 | J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); | |
1461 | spin_unlock(&journal->j_state_lock); | |
4afe9785 | 1462 | return 0; |
1da177e4 LT |
1463 | } |
1464 | ||
1465 | /** | |
1466 | * int journal_wipe() - Wipe journal contents | |
1467 | * @journal: Journal to act on. | |
1468 | * @write: flag (see below) | |
ae6ddcc5 | 1469 | * |
1da177e4 LT |
1470 | * Wipe out all of the contents of a journal, safely. This will produce |
1471 | * a warning if the journal contains any valid recovery information. | |
1472 | * Must be called between journal_init_*() and journal_load(). | |
1473 | * | |
1474 | * If 'write' is non-zero, then we wipe out the journal on disk; otherwise | |
1475 | * we merely suppress recovery. | |
1476 | */ | |
1477 | ||
1478 | int journal_wipe(journal_t *journal, int write) | |
1479 | { | |
1da177e4 LT |
1480 | int err = 0; |
1481 | ||
1482 | J_ASSERT (!(journal->j_flags & JFS_LOADED)); | |
1483 | ||
1484 | err = load_superblock(journal); | |
1485 | if (err) | |
1486 | return err; | |
1487 | ||
1da177e4 LT |
1488 | if (!journal->j_tail) |
1489 | goto no_recovery; | |
1490 | ||
1491 | printk (KERN_WARNING "JBD: %s recovery information on journal\n", | |
1492 | write ? "Clearing" : "Ignoring"); | |
1493 | ||
1494 | err = journal_skip_recovery(journal); | |
1495 | if (write) | |
1496 | journal_update_superblock(journal, 1); | |
1497 | ||
1498 | no_recovery: | |
1499 | return err; | |
1500 | } | |
1501 | ||
1502 | /* | |
1503 | * journal_dev_name: format a character string to describe on what | |
1504 | * device this journal is present. | |
1505 | */ | |
1506 | ||
022a4a7b | 1507 | static const char *journal_dev_name(journal_t *journal, char *buffer) |
1da177e4 LT |
1508 | { |
1509 | struct block_device *bdev; | |
1510 | ||
1511 | if (journal->j_inode) | |
1512 | bdev = journal->j_inode->i_sb->s_bdev; | |
1513 | else | |
1514 | bdev = journal->j_dev; | |
1515 | ||
1516 | return bdevname(bdev, buffer); | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * Journal abort has very specific semantics, which we describe | |
ae6ddcc5 | 1521 | * for journal abort. |
1da177e4 LT |
1522 | * |
1523 | * Two internal function, which provide abort to te jbd layer | |
1524 | * itself are here. | |
1525 | */ | |
1526 | ||
1527 | /* | |
1528 | * Quick version for internal journal use (doesn't lock the journal). | |
1529 | * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, | |
1530 | * and don't attempt to make any other journal updates. | |
1531 | */ | |
53308383 | 1532 | static void __journal_abort_hard(journal_t *journal) |
1da177e4 LT |
1533 | { |
1534 | transaction_t *transaction; | |
1535 | char b[BDEVNAME_SIZE]; | |
1536 | ||
1537 | if (journal->j_flags & JFS_ABORT) | |
1538 | return; | |
1539 | ||
1540 | printk(KERN_ERR "Aborting journal on device %s.\n", | |
1541 | journal_dev_name(journal, b)); | |
1542 | ||
1543 | spin_lock(&journal->j_state_lock); | |
1544 | journal->j_flags |= JFS_ABORT; | |
1545 | transaction = journal->j_running_transaction; | |
1546 | if (transaction) | |
1547 | __log_start_commit(journal, transaction->t_tid); | |
1548 | spin_unlock(&journal->j_state_lock); | |
1549 | } | |
1550 | ||
1551 | /* Soft abort: record the abort error status in the journal superblock, | |
1552 | * but don't do any other IO. */ | |
022a4a7b | 1553 | static void __journal_abort_soft (journal_t *journal, int errno) |
1da177e4 LT |
1554 | { |
1555 | if (journal->j_flags & JFS_ABORT) | |
1556 | return; | |
1557 | ||
1558 | if (!journal->j_errno) | |
1559 | journal->j_errno = errno; | |
1560 | ||
1561 | __journal_abort_hard(journal); | |
1562 | ||
1563 | if (errno) | |
1564 | journal_update_superblock(journal, 1); | |
1565 | } | |
1566 | ||
1567 | /** | |
1568 | * void journal_abort () - Shutdown the journal immediately. | |
1569 | * @journal: the journal to shutdown. | |
1570 | * @errno: an error number to record in the journal indicating | |
1571 | * the reason for the shutdown. | |
1572 | * | |
1573 | * Perform a complete, immediate shutdown of the ENTIRE | |
1574 | * journal (not of a single transaction). This operation cannot be | |
1575 | * undone without closing and reopening the journal. | |
ae6ddcc5 | 1576 | * |
1da177e4 LT |
1577 | * The journal_abort function is intended to support higher level error |
1578 | * recovery mechanisms such as the ext2/ext3 remount-readonly error | |
1579 | * mode. | |
1580 | * | |
1581 | * Journal abort has very specific semantics. Any existing dirty, | |
1582 | * unjournaled buffers in the main filesystem will still be written to | |
1583 | * disk by bdflush, but the journaling mechanism will be suspended | |
1584 | * immediately and no further transaction commits will be honoured. | |
1585 | * | |
1586 | * Any dirty, journaled buffers will be written back to disk without | |
1587 | * hitting the journal. Atomicity cannot be guaranteed on an aborted | |
1588 | * filesystem, but we _do_ attempt to leave as much data as possible | |
1589 | * behind for fsck to use for cleanup. | |
1590 | * | |
1591 | * Any attempt to get a new transaction handle on a journal which is in | |
1592 | * ABORT state will just result in an -EROFS error return. A | |
1593 | * journal_stop on an existing handle will return -EIO if we have | |
1594 | * entered abort state during the update. | |
1595 | * | |
1596 | * Recursive transactions are not disturbed by journal abort until the | |
1597 | * final journal_stop, which will receive the -EIO error. | |
1598 | * | |
1599 | * Finally, the journal_abort call allows the caller to supply an errno | |
1600 | * which will be recorded (if possible) in the journal superblock. This | |
1601 | * allows a client to record failure conditions in the middle of a | |
1602 | * transaction without having to complete the transaction to record the | |
1603 | * failure to disk. ext3_error, for example, now uses this | |
1604 | * functionality. | |
1605 | * | |
1606 | * Errors which originate from within the journaling layer will NOT | |
1607 | * supply an errno; a null errno implies that absolutely no further | |
1608 | * writes are done to the journal (unless there are any already in | |
1609 | * progress). | |
ae6ddcc5 | 1610 | * |
1da177e4 LT |
1611 | */ |
1612 | ||
1613 | void journal_abort(journal_t *journal, int errno) | |
1614 | { | |
1615 | __journal_abort_soft(journal, errno); | |
1616 | } | |
1617 | ||
ae6ddcc5 | 1618 | /** |
1da177e4 LT |
1619 | * int journal_errno () - returns the journal's error state. |
1620 | * @journal: journal to examine. | |
1621 | * | |
1622 | * This is the errno numbet set with journal_abort(), the last | |
1623 | * time the journal was mounted - if the journal was stopped | |
1624 | * without calling abort this will be 0. | |
1625 | * | |
1626 | * If the journal has been aborted on this mount time -EROFS will | |
1627 | * be returned. | |
1628 | */ | |
1629 | int journal_errno(journal_t *journal) | |
1630 | { | |
1631 | int err; | |
1632 | ||
1633 | spin_lock(&journal->j_state_lock); | |
1634 | if (journal->j_flags & JFS_ABORT) | |
1635 | err = -EROFS; | |
1636 | else | |
1637 | err = journal->j_errno; | |
1638 | spin_unlock(&journal->j_state_lock); | |
1639 | return err; | |
1640 | } | |
1641 | ||
ae6ddcc5 | 1642 | /** |
1da177e4 LT |
1643 | * int journal_clear_err () - clears the journal's error state |
1644 | * @journal: journal to act on. | |
1645 | * | |
1646 | * An error must be cleared or Acked to take a FS out of readonly | |
1647 | * mode. | |
1648 | */ | |
1649 | int journal_clear_err(journal_t *journal) | |
1650 | { | |
1651 | int err = 0; | |
1652 | ||
1653 | spin_lock(&journal->j_state_lock); | |
1654 | if (journal->j_flags & JFS_ABORT) | |
1655 | err = -EROFS; | |
1656 | else | |
1657 | journal->j_errno = 0; | |
1658 | spin_unlock(&journal->j_state_lock); | |
1659 | return err; | |
1660 | } | |
1661 | ||
ae6ddcc5 | 1662 | /** |
1da177e4 LT |
1663 | * void journal_ack_err() - Ack journal err. |
1664 | * @journal: journal to act on. | |
1665 | * | |
1666 | * An error must be cleared or Acked to take a FS out of readonly | |
1667 | * mode. | |
1668 | */ | |
1669 | void journal_ack_err(journal_t *journal) | |
1670 | { | |
1671 | spin_lock(&journal->j_state_lock); | |
1672 | if (journal->j_errno) | |
1673 | journal->j_flags |= JFS_ACK_ERR; | |
1674 | spin_unlock(&journal->j_state_lock); | |
1675 | } | |
1676 | ||
1677 | int journal_blocks_per_page(struct inode *inode) | |
1678 | { | |
1679 | return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | |
1680 | } | |
1681 | ||
1da177e4 LT |
1682 | /* |
1683 | * Journal_head storage management | |
1684 | */ | |
e18b890b | 1685 | static struct kmem_cache *journal_head_cache; |
1da177e4 LT |
1686 | #ifdef CONFIG_JBD_DEBUG |
1687 | static atomic_t nr_journal_heads = ATOMIC_INIT(0); | |
1688 | #endif | |
1689 | ||
1690 | static int journal_init_journal_head_cache(void) | |
1691 | { | |
1692 | int retval; | |
1693 | ||
1076d17a | 1694 | J_ASSERT(journal_head_cache == NULL); |
1da177e4 LT |
1695 | journal_head_cache = kmem_cache_create("journal_head", |
1696 | sizeof(struct journal_head), | |
1697 | 0, /* offset */ | |
e12ba74d | 1698 | SLAB_TEMPORARY, /* flags */ |
20c2df83 | 1699 | NULL); /* ctor */ |
1da177e4 | 1700 | retval = 0; |
1076d17a | 1701 | if (!journal_head_cache) { |
1da177e4 LT |
1702 | retval = -ENOMEM; |
1703 | printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); | |
1704 | } | |
1705 | return retval; | |
1706 | } | |
1707 | ||
1708 | static void journal_destroy_journal_head_cache(void) | |
1709 | { | |
3850f7a5 DG |
1710 | if (journal_head_cache) { |
1711 | kmem_cache_destroy(journal_head_cache); | |
1712 | journal_head_cache = NULL; | |
1713 | } | |
1da177e4 LT |
1714 | } |
1715 | ||
1716 | /* | |
1717 | * journal_head splicing and dicing | |
1718 | */ | |
1719 | static struct journal_head *journal_alloc_journal_head(void) | |
1720 | { | |
1721 | struct journal_head *ret; | |
1722 | static unsigned long last_warning; | |
1723 | ||
1724 | #ifdef CONFIG_JBD_DEBUG | |
1725 | atomic_inc(&nr_journal_heads); | |
1726 | #endif | |
1727 | ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS); | |
c80544dc | 1728 | if (ret == NULL) { |
1da177e4 LT |
1729 | jbd_debug(1, "out of memory for journal_head\n"); |
1730 | if (time_after(jiffies, last_warning + 5*HZ)) { | |
1731 | printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", | |
08fc99bf | 1732 | __func__); |
1da177e4 LT |
1733 | last_warning = jiffies; |
1734 | } | |
c80544dc | 1735 | while (ret == NULL) { |
1da177e4 LT |
1736 | yield(); |
1737 | ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS); | |
1738 | } | |
1739 | } | |
1740 | return ret; | |
1741 | } | |
1742 | ||
1743 | static void journal_free_journal_head(struct journal_head *jh) | |
1744 | { | |
1745 | #ifdef CONFIG_JBD_DEBUG | |
1746 | atomic_dec(&nr_journal_heads); | |
c9cf5528 | 1747 | memset(jh, JBD_POISON_FREE, sizeof(*jh)); |
1da177e4 LT |
1748 | #endif |
1749 | kmem_cache_free(journal_head_cache, jh); | |
1750 | } | |
1751 | ||
1752 | /* | |
1753 | * A journal_head is attached to a buffer_head whenever JBD has an | |
1754 | * interest in the buffer. | |
1755 | * | |
1756 | * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit | |
1757 | * is set. This bit is tested in core kernel code where we need to take | |
1758 | * JBD-specific actions. Testing the zeroness of ->b_private is not reliable | |
1759 | * there. | |
1760 | * | |
1761 | * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. | |
1762 | * | |
1763 | * When a buffer has its BH_JBD bit set it is immune from being released by | |
1764 | * core kernel code, mainly via ->b_count. | |
1765 | * | |
1766 | * A journal_head may be detached from its buffer_head when the journal_head's | |
1767 | * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. | |
1768 | * Various places in JBD call journal_remove_journal_head() to indicate that the | |
1769 | * journal_head can be dropped if needed. | |
1770 | * | |
1771 | * Various places in the kernel want to attach a journal_head to a buffer_head | |
1772 | * _before_ attaching the journal_head to a transaction. To protect the | |
1773 | * journal_head in this situation, journal_add_journal_head elevates the | |
1774 | * journal_head's b_jcount refcount by one. The caller must call | |
1775 | * journal_put_journal_head() to undo this. | |
1776 | * | |
1777 | * So the typical usage would be: | |
1778 | * | |
1779 | * (Attach a journal_head if needed. Increments b_jcount) | |
1780 | * struct journal_head *jh = journal_add_journal_head(bh); | |
1781 | * ... | |
1782 | * jh->b_transaction = xxx; | |
1783 | * journal_put_journal_head(jh); | |
1784 | * | |
1785 | * Now, the journal_head's b_jcount is zero, but it is safe from being released | |
1786 | * because it has a non-zero b_transaction. | |
1787 | */ | |
1788 | ||
1789 | /* | |
1790 | * Give a buffer_head a journal_head. | |
1791 | * | |
1792 | * Doesn't need the journal lock. | |
1793 | * May sleep. | |
1794 | */ | |
1795 | struct journal_head *journal_add_journal_head(struct buffer_head *bh) | |
1796 | { | |
1797 | struct journal_head *jh; | |
1798 | struct journal_head *new_jh = NULL; | |
1799 | ||
1800 | repeat: | |
1801 | if (!buffer_jbd(bh)) { | |
1802 | new_jh = journal_alloc_journal_head(); | |
1803 | memset(new_jh, 0, sizeof(*new_jh)); | |
1804 | } | |
1805 | ||
1806 | jbd_lock_bh_journal_head(bh); | |
1807 | if (buffer_jbd(bh)) { | |
1808 | jh = bh2jh(bh); | |
1809 | } else { | |
1810 | J_ASSERT_BH(bh, | |
1811 | (atomic_read(&bh->b_count) > 0) || | |
1812 | (bh->b_page && bh->b_page->mapping)); | |
1813 | ||
1814 | if (!new_jh) { | |
1815 | jbd_unlock_bh_journal_head(bh); | |
1816 | goto repeat; | |
1817 | } | |
1818 | ||
1819 | jh = new_jh; | |
1820 | new_jh = NULL; /* We consumed it */ | |
1821 | set_buffer_jbd(bh); | |
1822 | bh->b_private = jh; | |
1823 | jh->b_bh = bh; | |
1824 | get_bh(bh); | |
1825 | BUFFER_TRACE(bh, "added journal_head"); | |
1826 | } | |
1827 | jh->b_jcount++; | |
1828 | jbd_unlock_bh_journal_head(bh); | |
1829 | if (new_jh) | |
1830 | journal_free_journal_head(new_jh); | |
1831 | return bh->b_private; | |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * Grab a ref against this buffer_head's journal_head. If it ended up not | |
1836 | * having a journal_head, return NULL | |
1837 | */ | |
1838 | struct journal_head *journal_grab_journal_head(struct buffer_head *bh) | |
1839 | { | |
1840 | struct journal_head *jh = NULL; | |
1841 | ||
1842 | jbd_lock_bh_journal_head(bh); | |
1843 | if (buffer_jbd(bh)) { | |
1844 | jh = bh2jh(bh); | |
1845 | jh->b_jcount++; | |
1846 | } | |
1847 | jbd_unlock_bh_journal_head(bh); | |
1848 | return jh; | |
1849 | } | |
1850 | ||
1851 | static void __journal_remove_journal_head(struct buffer_head *bh) | |
1852 | { | |
1853 | struct journal_head *jh = bh2jh(bh); | |
1854 | ||
1855 | J_ASSERT_JH(jh, jh->b_jcount >= 0); | |
1856 | ||
1857 | get_bh(bh); | |
1858 | if (jh->b_jcount == 0) { | |
1859 | if (jh->b_transaction == NULL && | |
1860 | jh->b_next_transaction == NULL && | |
1861 | jh->b_cp_transaction == NULL) { | |
1862 | J_ASSERT_JH(jh, jh->b_jlist == BJ_None); | |
1863 | J_ASSERT_BH(bh, buffer_jbd(bh)); | |
1864 | J_ASSERT_BH(bh, jh2bh(jh) == bh); | |
1865 | BUFFER_TRACE(bh, "remove journal_head"); | |
1866 | if (jh->b_frozen_data) { | |
1867 | printk(KERN_WARNING "%s: freeing " | |
1868 | "b_frozen_data\n", | |
08fc99bf | 1869 | __func__); |
c089d490 | 1870 | jbd_free(jh->b_frozen_data, bh->b_size); |
1da177e4 LT |
1871 | } |
1872 | if (jh->b_committed_data) { | |
1873 | printk(KERN_WARNING "%s: freeing " | |
1874 | "b_committed_data\n", | |
08fc99bf | 1875 | __func__); |
c089d490 | 1876 | jbd_free(jh->b_committed_data, bh->b_size); |
1da177e4 LT |
1877 | } |
1878 | bh->b_private = NULL; | |
1879 | jh->b_bh = NULL; /* debug, really */ | |
1880 | clear_buffer_jbd(bh); | |
1881 | __brelse(bh); | |
1882 | journal_free_journal_head(jh); | |
1883 | } else { | |
1884 | BUFFER_TRACE(bh, "journal_head was locked"); | |
1885 | } | |
1886 | } | |
1887 | } | |
1888 | ||
1889 | /* | |
1890 | * journal_remove_journal_head(): if the buffer isn't attached to a transaction | |
1891 | * and has a zero b_jcount then remove and release its journal_head. If we did | |
1892 | * see that the buffer is not used by any transaction we also "logically" | |
1893 | * decrement ->b_count. | |
1894 | * | |
1895 | * We in fact take an additional increment on ->b_count as a convenience, | |
1896 | * because the caller usually wants to do additional things with the bh | |
1897 | * after calling here. | |
1898 | * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some | |
1899 | * time. Once the caller has run __brelse(), the buffer is eligible for | |
1900 | * reaping by try_to_free_buffers(). | |
1901 | */ | |
1902 | void journal_remove_journal_head(struct buffer_head *bh) | |
1903 | { | |
1904 | jbd_lock_bh_journal_head(bh); | |
1905 | __journal_remove_journal_head(bh); | |
1906 | jbd_unlock_bh_journal_head(bh); | |
1907 | } | |
1908 | ||
1909 | /* | |
1910 | * Drop a reference on the passed journal_head. If it fell to zero then try to | |
1911 | * release the journal_head from the buffer_head. | |
1912 | */ | |
1913 | void journal_put_journal_head(struct journal_head *jh) | |
1914 | { | |
1915 | struct buffer_head *bh = jh2bh(jh); | |
1916 | ||
1917 | jbd_lock_bh_journal_head(bh); | |
1918 | J_ASSERT_JH(jh, jh->b_jcount > 0); | |
1919 | --jh->b_jcount; | |
1920 | if (!jh->b_jcount && !jh->b_transaction) { | |
1921 | __journal_remove_journal_head(bh); | |
1922 | __brelse(bh); | |
1923 | } | |
1924 | jbd_unlock_bh_journal_head(bh); | |
1925 | } | |
1926 | ||
1927 | /* | |
c2a9159c | 1928 | * debugfs tunables |
1da177e4 | 1929 | */ |
c2a9159c | 1930 | #ifdef CONFIG_JBD_DEBUG |
1da177e4 | 1931 | |
c2a9159c JS |
1932 | u8 journal_enable_debug __read_mostly; |
1933 | EXPORT_SYMBOL(journal_enable_debug); | |
1da177e4 | 1934 | |
c2a9159c JS |
1935 | static struct dentry *jbd_debugfs_dir; |
1936 | static struct dentry *jbd_debug; | |
1da177e4 | 1937 | |
c2a9159c | 1938 | static void __init jbd_create_debugfs_entry(void) |
1da177e4 | 1939 | { |
c2a9159c JS |
1940 | jbd_debugfs_dir = debugfs_create_dir("jbd", NULL); |
1941 | if (jbd_debugfs_dir) | |
765f8361 | 1942 | jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR, |
c2a9159c JS |
1943 | jbd_debugfs_dir, |
1944 | &journal_enable_debug); | |
1da177e4 LT |
1945 | } |
1946 | ||
c2a9159c | 1947 | static void __exit jbd_remove_debugfs_entry(void) |
1da177e4 | 1948 | { |
c2a9159c JS |
1949 | debugfs_remove(jbd_debug); |
1950 | debugfs_remove(jbd_debugfs_dir); | |
1da177e4 LT |
1951 | } |
1952 | ||
c2a9159c | 1953 | #else |
1da177e4 | 1954 | |
c2a9159c | 1955 | static inline void jbd_create_debugfs_entry(void) |
1da177e4 | 1956 | { |
1da177e4 LT |
1957 | } |
1958 | ||
c2a9159c | 1959 | static inline void jbd_remove_debugfs_entry(void) |
1da177e4 | 1960 | { |
1da177e4 LT |
1961 | } |
1962 | ||
1da177e4 LT |
1963 | #endif |
1964 | ||
e18b890b | 1965 | struct kmem_cache *jbd_handle_cache; |
1da177e4 LT |
1966 | |
1967 | static int __init journal_init_handle_cache(void) | |
1968 | { | |
1969 | jbd_handle_cache = kmem_cache_create("journal_handle", | |
1970 | sizeof(handle_t), | |
1971 | 0, /* offset */ | |
e12ba74d | 1972 | SLAB_TEMPORARY, /* flags */ |
20c2df83 | 1973 | NULL); /* ctor */ |
1da177e4 LT |
1974 | if (jbd_handle_cache == NULL) { |
1975 | printk(KERN_EMERG "JBD: failed to create handle cache\n"); | |
1976 | return -ENOMEM; | |
1977 | } | |
1978 | return 0; | |
1979 | } | |
1980 | ||
1981 | static void journal_destroy_handle_cache(void) | |
1982 | { | |
1983 | if (jbd_handle_cache) | |
1984 | kmem_cache_destroy(jbd_handle_cache); | |
1985 | } | |
1986 | ||
1987 | /* | |
1988 | * Module startup and shutdown | |
1989 | */ | |
1990 | ||
1991 | static int __init journal_init_caches(void) | |
1992 | { | |
1993 | int ret; | |
1994 | ||
1995 | ret = journal_init_revoke_caches(); | |
1996 | if (ret == 0) | |
1997 | ret = journal_init_journal_head_cache(); | |
1998 | if (ret == 0) | |
1999 | ret = journal_init_handle_cache(); | |
2000 | return ret; | |
2001 | } | |
2002 | ||
2003 | static void journal_destroy_caches(void) | |
2004 | { | |
2005 | journal_destroy_revoke_caches(); | |
2006 | journal_destroy_journal_head_cache(); | |
2007 | journal_destroy_handle_cache(); | |
2008 | } | |
2009 | ||
2010 | static int __init journal_init(void) | |
2011 | { | |
2012 | int ret; | |
2013 | ||
2aed3484 | 2014 | BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); |
022a4a7b | 2015 | |
1da177e4 LT |
2016 | ret = journal_init_caches(); |
2017 | if (ret != 0) | |
2018 | journal_destroy_caches(); | |
c2a9159c | 2019 | jbd_create_debugfs_entry(); |
1da177e4 LT |
2020 | return ret; |
2021 | } | |
2022 | ||
2023 | static void __exit journal_exit(void) | |
2024 | { | |
2025 | #ifdef CONFIG_JBD_DEBUG | |
2026 | int n = atomic_read(&nr_journal_heads); | |
2027 | if (n) | |
2028 | printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); | |
2029 | #endif | |
c2a9159c | 2030 | jbd_remove_debugfs_entry(); |
1da177e4 LT |
2031 | journal_destroy_caches(); |
2032 | } | |
2033 | ||
2034 | MODULE_LICENSE("GPL"); | |
2035 | module_init(journal_init); | |
2036 | module_exit(journal_exit); | |
2037 |