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