1/*
2 * linux/include/linux/jbd.h
3 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>
5 *
6 * Copyright 1998-2000 Red Hat, Inc --- 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 * Definitions for transaction data structures for the buffer cache
13 * filesystem journaling support.
14 */
15
16#ifndef _LINUX_HFSPLUS_JBD_H
17#define _LINUX_HFSPLUS_JBD_H
18
19#include <linux/buffer_head.h>
20#include "hfsplus_journal_head.h"
21#include <linux/stddef.h>
22#include <linux/bit_spinlock.h>
23#include <asm/semaphore.h>
24#include "hfsplus_raw.h"
25
26#define hfsplus_jbd_oom_retry 1
27
28#define HFSPLUS_JOURNAL_MAC_COMPATIBLE
29#define HFSPLUS_JBD_MAGIC_NUMBER	0x9876
30
31/*
32 * Define JBD_PARANIOD_IOFAIL to cause a kernel BUG() if ext3 finds
33 * certain classes of error which can occur due to failed IOs.  Under
34 * normal use we want ext3 to continue after such errors, because
35 * hardware _can_ fail, but for debugging purposes when running tests on
36 * known-good hardware we may want to trap these errors.
37 */
38#undef JBD_PARANOID_IOFAIL
39
40/*
41 * The default maximum commit age, in seconds.
42 */
43#define HFSPLUS_JBD_DEFAULT_MAX_COMMIT_AGE 5
44
45#if 0
46#define hfsplus_jbd_debug(n, f, a...)	\
47			printk(KERN_INFO "(%s, %d):", __FUNCTION__, __LINE__); \
48			printk(f, ## a);
49#else
50#define hfsplus_jbd_debug(f, a...)
51#endif
52
53extern void * __hfsplus_jbd_kmalloc (const char *where, size_t size, gfp_t flags, int retry);
54#define hfsplus_jbd_kmalloc(size, flags) \
55	__hfsplus_jbd_kmalloc(__FUNCTION__, (size), (flags), hfsplus_jbd_oom_retry)
56#define hfsplus_jbd_rep_kmalloc(size, flags) \
57	__hfsplus_jbd_kmalloc(__FUNCTION__, (size), (flags), 1)
58
59#define JFS_MIN_JOURNAL_BLOCKS 1024
60
61#ifdef __KERNEL__
62
63typedef struct hfsplus_handle    hfsplus_handle_t;
64/**
65 * typedef hfsplus_jbd_handle_t - The hfsplus_jbd_handle_t type represents a single atomic update being performed by some process.
66 *
67 * All filesystem modifications made by the process go
68 * through this handle.  Recursive operations (such as quota operations)
69 * are gathered into a single update.
70 *
71 * The buffer credits field is used to account for journaled buffers
72 * being modified by the running process.  To ensure that there is
73 * enough log space for all outstanding operations, we need to limit the
74 * number of outstanding buffers possible at any time.  When the
75 * operation completes, any buffer credits not used are credited back to
76 * the transaction, so that at all times we know how many buffers the
77 * outstanding updates on a transaction might possibly touch.
78 *
79 * This is an opaque datatype.
80 **/
81typedef struct hfsplus_jbd_handle_s		hfsplus_jbd_handle_t;	/* Atomic operation type */
82
83
84/**
85 * typedef hfsplus_jbd_t - The hfsplus_jbd_t maintains all of the journaling state information for a single filesystem.
86 *
87 * hfsplus_jbd_t is linked to from the fs superblock structure.
88 *
89 * We use the hfsplus_jbd_t to keep track of all outstanding transaction
90 * activity on the filesystem, and to manage the state of the log
91 * writing process.
92 *
93 * This is an opaque datatype.
94 **/
95typedef struct hfsplus_jbd_s	hfsplus_jbd_t;	/* Journal control structure */
96#endif
97
98/*
99 * Internal structures used by the logging mechanism:
100 */
101
102#define JFS_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
103
104/*
105 * On-disk structures
106 */
107
108/*
109 * Descriptor block types:
110 */
111
112#define JFS_DESCRIPTOR_BLOCK	1
113#define JFS_COMMIT_BLOCK		2
114#define JFS_SUPERBLOCK_V1		3
115#define JFS_SUPERBLOCK_V2		4
116#define JFS_REVOKE_BLOCK		5
117
118/*
119 * Standard header for all descriptor blocks:
120 */
121typedef struct hfsplus_jbd_header_s
122{
123	__be32		h_magic;
124	__be32		h_blocktype;
125	__be32		h_sequence;
126} hfsplus_jbd_header_t;
127
128
129/*
130 * The block tag: used to describe a single buffer in the journal
131 */
132typedef struct hfsplus_jbd_block_tag_s
133{
134	__be32		t_blocknr;	/* The on-disk block number */
135	__be32		t_flags;	/* See below */
136} hfsplus_jbd_block_tag_t;
137
138/*
139 * The revoke descriptor: used on disk to describe a series of blocks to
140 * be revoked from the log
141 */
142typedef struct hfsplus_jbd_revoke_header_s
143{
144	hfsplus_jbd_header_t r_header;
145	__be32		 r_count;	/* Count of bytes used in the block */
146} hfsplus_jbd_revoke_header_t;
147
148
149/* Definitions for the journal tag flags word: */
150#define JFS_FLAG_ESCAPE		1	/* on-disk block is escaped */
151#define JFS_FLAG_SAME_UUID	2	/* block has same uuid as previous */
152#define JFS_FLAG_DELETED	4	/* block deleted by this transaction */
153#define JFS_FLAG_LAST_TAG	8	/* last tag in this descriptor block */
154
155
156/*
157 * The journal superblock.  All fields are in big-endian byte order.
158 */
159typedef struct hfsplus_jbd_superblock_s
160{
161	/* First sector contains Mac's journal header */
162/* 0x0000 */
163	struct hfsplus_journal_header mac_jh;
164
165	/* Insert padding for rest of the first sector */
166	__u8 mac_padding[HFSPLUS_SECTOR_SIZE - sizeof(struct hfsplus_journal_header)];
167
168/* 0x0200 */
169	hfsplus_jbd_header_t s_header;
170
171/* 0x020C */
172	/* Static information describing the journal */
173	__be32	s_blocksize;		/* journal device blocksize */
174	__be32	s_maxlen;		/* total blocks in journal file */
175	__be32	s_first;		/* first block of log information */
176
177/* 0x0218 */
178	/* Dynamic information describing the current state of the log */
179	__be32	s_sequence;		/* first commit ID expected in log */
180	__be32	s_start;		/* blocknr of start of log */
181
182/* 0x0220 */
183	/* Error value, as set by hfsplus_jbd_abort(). */
184	__be32	s_errno;
185
186/* 0x0224 */
187	/* Remaining fields are only valid in a version-2 superblock */
188	__be32	s_feature_compat; 	/* compatible feature set */
189	__be32	s_feature_incompat; 	/* incompatible feature set */
190	__be32	s_feature_ro_compat; 	/* readonly-compatible feature set */
191/* 0x0230 */
192	__u8	s_uuid[16];		/* 128-bit uuid for journal */
193
194/* 0x0240 */
195	__be32	s_nr_users;		/* Nr of filesystems sharing log */
196
197	__be32	s_dynsuper;		/* Blocknr of dynamic superblock copy*/
198
199/* 0x0248 */
200	__be32	s_max_transaction;	/* Limit of journal blocks per trans.*/
201	__be32	s_max_trans_data;	/* Limit of data blocks per trans. */
202
203/* 0x0250 */
204	__u32	s_padding[44];
205
206/* 0x0300 */
207	__u8	s_users[16*16];		/* ids of all fs'es sharing the log */
208/* 0x0400 */
209} hfsplus_jbd_superblock_t;
210
211#define JFS_HAS_COMPAT_FEATURE(j,mask)					\
212	((j)->j_format_version >= 2 &&					\
213	 ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask))))
214#define JFS_HAS_RO_COMPAT_FEATURE(j,mask)				\
215	((j)->j_format_version >= 2 &&					\
216	 ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask))))
217#define JFS_HAS_INCOMPAT_FEATURE(j,mask)				\
218	((j)->j_format_version >= 2 &&					\
219	 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
220
221#define JFS_FEATURE_INCOMPAT_REVOKE	0x00000001
222
223/* Features known to this kernel version: */
224#define JFS_KNOWN_COMPAT_FEATURES	0
225#define JFS_KNOWN_ROCOMPAT_FEATURES	0
226#define JFS_KNOWN_INCOMPAT_FEATURES	JFS_FEATURE_INCOMPAT_REVOKE
227
228#ifdef __KERNEL__
229
230#include <linux/fs.h>
231#include <linux/sched.h>
232#include <asm/bug.h>
233
234#define HFSPLUS_JBD_ASSERTIONS
235#ifdef HFSPLUS_JBD_ASSERTIONS
236#define HFSPLUS_J_ASSERT(assert)						\
237do {									\
238	if (!(assert)) {						\
239		printk (KERN_EMERG					\
240			"Assertion failure in %s() at %s:%d: \"%s\"\n",	\
241			__FUNCTION__, __FILE__, __LINE__, # assert);	\
242		BUG();							\
243	}								\
244} while (0)
245
246#if defined(CONFIG_BUFFER_DEBUG)
247void buffer_assertion_failure(struct buffer_head *bh);
248#define HFSPLUS_J_ASSERT_BH(bh, expr)						\
249	do {								\
250		if (!(expr))						\
251			buffer_assertion_failure(bh);			\
252		HFSPLUS_J_ASSERT(expr);						\
253	} while (0)
254#define HFSPLUS_J_ASSERT_JH(jh, expr)	HFSPLUS_J_ASSERT_BH(hfsplus_jh2bh(jh), expr)
255#else
256#define HFSPLUS_J_ASSERT_BH(bh, expr)	HFSPLUS_J_ASSERT(expr)
257#define HFSPLUS_J_ASSERT_JH(jh, expr)	HFSPLUS_J_ASSERT(expr)
258#endif
259
260#else
261#define HFSPLUS_J_ASSERT(assert)	do { } while (0)
262#endif		/* HFSPLUS_JBD_ASSERTIONS */
263
264#if defined(HFSPLUS_JBD_PARANOID_IOFAIL)
265#define HFSPLUS_J_EXPECT(expr, why...)		HFSPLUS_J_ASSERT(expr)
266#define HFSPLUS_J_EXPECT_BH(bh, expr, why...)	HFSPLUS_J_ASSERT_BH(bh, expr)
267#define HFSPLUS_J_EXPECT_JH(jh, expr, why...)	HFSPLUS_J_ASSERT_JH(jh, expr)
268#else
269#define __hfsplus_jbd_expect(expr, why...)					     \
270	({								     \
271		int val = (expr);					     \
272		if (!val) {						     \
273			printk(KERN_ERR					     \
274				"EXT3-fs unexpected failure: %s;\n",# expr); \
275			printk(KERN_ERR why "\n");			     \
276		}							     \
277		val;							     \
278	})
279#define HFSPLUS_J_EXPECT(expr, why...)		__hfsplus_jbd_expect(expr, ## why)
280#define HFSPLUS_J_EXPECT_BH(bh, expr, why...)	__hfsplus_jbd_expect(expr, ## why)
281#define HFSPLUS_J_EXPECT_JH(jh, expr, why...)	__hfsplus_jbd_expect(expr, ## why)
282#endif
283
284enum hfsplus_jbd_state_bits {
285	BH_HFSPLUS_JBD			/* Has an attached ext3 hfsplus_jbd_head */
286	  = BH_PrivateStart,
287	BH_HFSPLUS_JWrite,		/* Being written to log (@@@ DEBUGGING) */
288	BH_HFSPLUS_Freed,		/* Has been freed (truncated) */
289	BH_HFSPLUS_Revoked,		/* Has been revoked from the log */
290	BH_HFSPLUS_RevokeValid,		/* Revoked flag is valid */
291	BH_HFSPLUS_JBDDirty,		/* Is dirty but journaled */
292	BH_HFSPLUS_State,		/* Pins most hfsplus_jbd_head state */
293	BH_HFSPLUS_JournalHead,		/* Pins bh->b_private and jh->b_bh */
294	BH_HFSPLUS_Unshadow,		/* Dummy bit, for HFSPLUS_BJ_Shadow wakeup filtering */
295};
296
297BUFFER_FNS(HFSPLUS_JBD, hfsplus_jbd)
298BUFFER_FNS(HFSPLUS_JWrite, hfsplus_jbd_jwrite)
299BUFFER_FNS(HFSPLUS_JBDDirty, hfsplus_jbddirty)
300TAS_BUFFER_FNS(HFSPLUS_JBDDirty, hfsplus_jbddirty)
301BUFFER_FNS(HFSPLUS_Revoked, hfsplus_jbd_revoked)
302TAS_BUFFER_FNS(HFSPLUS_Revoked, hfsplus_jbd_revoked)
303BUFFER_FNS(HFSPLUS_RevokeValid, hfsplus_jbd_revokevalid)
304TAS_BUFFER_FNS(HFSPLUS_RevokeValid, hfsplus_jbd_revokevalid)
305BUFFER_FNS(HFSPLUS_Freed, hfsplus_jbd_freed)
306
307static inline struct buffer_head *hfsplus_jh2bh(struct hfsplus_jbd_head *jh)
308{
309	return jh->b_bh;
310}
311
312static inline struct hfsplus_jbd_head *hfsplus_bh2jh(struct buffer_head *bh)
313{
314	return bh->b_private;
315}
316
317static inline void hfsplus_jbd_lock_bh_state(struct buffer_head *bh)
318{
319	bit_spin_lock(BH_HFSPLUS_State, &bh->b_state);
320}
321
322static inline int hfsplus_jbd_trylock_bh_state(struct buffer_head *bh)
323{
324	return bit_spin_trylock(BH_HFSPLUS_State, &bh->b_state);
325}
326
327static inline int hfsplus_jbd_is_locked_bh_state(struct buffer_head *bh)
328{
329	return bit_spin_is_locked(BH_HFSPLUS_State, &bh->b_state);
330}
331
332static inline void hfsplus_jbd_unlock_bh_state(struct buffer_head *bh)
333{
334	bit_spin_unlock(BH_HFSPLUS_State, &bh->b_state);
335}
336
337static inline void hfsplus_jbd_lock_bh_hfsplus_jbd_head(struct buffer_head *bh)
338{
339	bit_spin_lock(BH_HFSPLUS_JournalHead, &bh->b_state);
340}
341
342static inline void hfsplus_jbd_unlock_bh_hfsplus_jbd_head(struct buffer_head *bh)
343{
344	bit_spin_unlock(BH_HFSPLUS_JournalHead, &bh->b_state);
345}
346
347struct hfsplus_jbd_revoke_table_s;
348
349/**
350 * struct hfsplus_jbd_handle_s - The hfsplus_jbd_handle_s type is the concrete type associated with
351 *     hfsplus_jbd_handle_t.
352 * @h_transaction: Which compound transaction is this update a part of?
353 * @h_buffer_credits: Number of remaining buffers we are allowed to dirty.
354 * @h_ref: Reference count on this handle
355 * @h_err: Field for caller's use to track errors through large fs operations
356 * @h_sync: flag for sync-on-close
357 * @h_jdata: flag to force data journaling
358 * @h_aborted: flag indicating fatal error on handle
359 **/
360
361/* Docbook can't yet cope with the bit fields, but will leave the documentation
362 * in so it can be fixed later.
363 */
364
365struct hfsplus_jbd_handle_s
366{
367	/* Which compound transaction is this update a part of? */
368	hfsplus_transaction_t		*h_transaction;
369
370	/* Number of remaining buffers we are allowed to dirty: */
371	int			h_buffer_credits;
372
373	/* Reference count on this handle */
374	int			h_ref;
375
376	/* Field for caller's use to track errors through large fs */
377	/* operations */
378	int			h_err;
379
380	/* Flags [no locking] */
381	unsigned int	h_sync:		1;	/* sync-on-close */
382	unsigned int	h_jdata:	1;	/* force data journaling */
383	unsigned int	h_aborted:	1;	/* fatal error on handle */
384};
385
386
387struct hfsplus_handle {
388	hfsplus_jbd_handle_t *handle;
389	u16 hcnt; /* Count number of call of journal_dirty_metadata() */
390	u16 maxblock;
391	u32 journaled;
392};
393
394/* The hfsplus_transaction_t type is the guts of the journaling mechanism.  It
395 * tracks a compound transaction through its various states:
396 *
397 * RUNNING:	accepting new updates
398 * LOCKED:	Updates still running but we don't accept new ones
399 * RUNDOWN:	Updates are tidying up but have finished requesting
400 *		new buffers to modify (state not used for now)
401 * FLUSH:       All updates complete, but we are still writing to disk
402 * COMMIT:      All data on disk, writing commit record
403 * FINISHED:	We still have to keep the transaction for checkpointing.
404 *
405 * The transaction keeps track of all of the buffers modified by a
406 * running transaction, and all of the buffers committed but not yet
407 * flushed to home for finished transactions.
408 */
409
410/*
411 * Lock ranking:
412 *
413 *    j_list_lock
414 *      ->hfsplus_jbd_lock_bh_hfsplus_jbd_head()	(This is "innermost")
415 *
416 *    j_state_lock
417 *    ->hfsplus_jbd_lock_bh_state()
418 *
419 *    hfsplus_jbd_lock_bh_state()
420 *    ->j_list_lock
421 *
422 *    j_state_lock
423 *    ->t_handle_lock
424 *
425 *    j_state_lock
426 *    ->j_list_lock			(hfsplus_jbd_unmap_buffer)
427 *
428 */
429
430struct hfsplus_transaction_s
431{
432	/* Pointer to the journal for this transaction. [no locking] */
433	hfsplus_jbd_t		*t_journal;
434
435	/* Sequence number for this transaction [no locking] */
436	hfsplus_jbd_tid_t			t_tid;
437
438	/*
439	 * Transaction's current state
440	 * [no locking - only kjournald alters this]
441	 * FIXME: needs barriers
442	 * KLUDGE: [use j_state_lock]
443	 */
444	enum {
445		HFSPLUS_T_RUNNING,
446		HFSPLUS_T_LOCKED,
447		HFSPLUS_T_RUNDOWN,
448		HFSPLUS_T_FLUSH,
449		HFSPLUS_T_COMMIT,
450		HFSPLUS_T_FINISHED
451	}			t_state;
452
453	/*
454	 * Where in the log does this transaction's commit start? [no locking]
455	 */
456	unsigned long		t_log_start;
457
458	/* Number of buffers on the t_buffers list [j_list_lock] */
459	int			t_nr_buffers;
460
461	/*
462	 * Doubly-linked circular list of all buffers reserved but not yet
463	 * modified by this transaction [j_list_lock]
464	 */
465	struct hfsplus_jbd_head	*t_reserved_list;
466
467	/*
468	 * Doubly-linked circular list of all buffers under writeout during
469	 * commit [j_list_lock]
470	 */
471	struct hfsplus_jbd_head	*t_locked_list;
472
473	/*
474	 * Doubly-linked circular list of all metadata buffers owned by this
475	 * transaction [j_list_lock]
476	 */
477	struct hfsplus_jbd_head	*t_buffers;
478
479	/*
480	 * Doubly-linked circular list of all data buffers still to be
481	 * flushed before this transaction can be committed [j_list_lock]
482	 */
483	struct hfsplus_jbd_head	*t_sync_datalist;
484
485	/*
486	 * Doubly-linked circular list of all forget buffers (superseded
487	 * buffers which we can un-checkpoint once this transaction commits)
488	 * [j_list_lock]
489	 */
490	struct hfsplus_jbd_head	*t_forget;
491
492	/*
493	 * Doubly-linked circular list of all buffers still to be flushed before
494	 * this transaction can be checkpointed. [j_list_lock]
495	 */
496	struct hfsplus_jbd_head	*t_checkpoint_list;
497
498	/*
499	 * Doubly-linked circular list of temporary buffers currently undergoing
500	 * IO in the log [j_list_lock]
501	 */
502	struct hfsplus_jbd_head	*t_iobuf_list;
503
504	/*
505	 * Doubly-linked circular list of metadata buffers being shadowed by log
506	 * IO.  The IO buffers on the iobuf list and the shadow buffers on this
507	 * list match each other one for one at all times. [j_list_lock]
508	 */
509	struct hfsplus_jbd_head	*t_shadow_list;
510
511	/*
512	 * Doubly-linked circular list of control buffers being written to the
513	 * log. [j_list_lock]
514	 */
515	struct hfsplus_jbd_head	*t_log_list;
516
517	/*
518	 * Protects info related to handles
519	 */
520	spinlock_t		t_handle_lock;
521
522	/*
523	 * Number of outstanding updates running on this transaction
524	 * [t_handle_lock]
525	 */
526	int			t_updates;
527
528	/*
529	 * Number of buffers reserved for use by all handles in this transaction
530	 * handle but not yet modified. [t_handle_lock]
531	 */
532	int			t_outstanding_credits;
533
534	/*
535	 * Forward and backward links for the circular list of all transactions
536	 * awaiting checkpoint. [j_list_lock]
537	 */
538	hfsplus_transaction_t		*t_cpnext, *t_cpprev;
539
540	/*
541	 * When will the transaction expire (become due for commit), in jiffies?
542	 * [no locking]
543	 */
544	unsigned long		t_expires;
545
546	/*
547	 * How many handles used this transaction? [t_handle_lock]
548	 */
549	int t_handle_count;
550
551};
552
553/**
554 * struct hfsplus_jbd_s - The hfsplus_jbd_s type is the concrete type associated with
555 *     hfsplus_jbd_t.
556 * @j_flags:  General journaling state flags
557 * @j_errno:  Is there an outstanding uncleared error on the journal (from a
558 *     prior abort)?
559 * @j_sb_buffer: First part of superblock buffer
560 * @j_superblock: Second part of superblock buffer
561 * @j_format_version: Version of the superblock format
562 * @j_state_lock: Protect the various scalars in the journal
563 * @j_barrier_count:  Number of processes waiting to create a barrier lock
564 * @j_barrier: The barrier lock itself
565 * @j_running_transaction: The current running transaction..
566 * @j_committing_transaction: the transaction we are pushing to disk
567 * @j_checkpoint_transactions: a linked circular list of all transactions
568 *  waiting for checkpointing
569 * @j_wait_transaction_locked: Wait queue for waiting for a locked transaction
570 *  to start committing, or for a barrier lock to be released
571 * @j_wait_logspace: Wait queue for waiting for checkpointing to complete
572 * @j_wait_done_commit: Wait queue for waiting for commit to complete
573 * @j_wait_checkpoint:  Wait queue to trigger checkpointing
574 * @j_wait_commit: Wait queue to trigger commit
575 * @j_wait_updates: Wait queue to wait for updates to complete
576 * @j_checkpoint_sem: Semaphore for locking against concurrent checkpoints
577 * @j_head: Journal head - identifies the first unused block in the journal
578 * @j_tail: Journal tail - identifies the oldest still-used block in the
579 *  journal.
580 * @j_free: Journal free - how many free blocks are there in the journal?
581 * @j_first: The block number of the first usable block
582 * @j_last: The block number one beyond the last usable block
583 * @j_dev: Device where we store the journal
584 * @j_blocksize: blocksize for the location where we store the journal.
585 * @j_blk_offset: starting block offset for into the device where we store the
586 *     journal
587 * @j_fs_dev: Device which holds the client fs.  For internal journal this will
588 *     be equal to j_dev
589 * @j_maxlen: Total maximum capacity of the journal region on disk.
590 * @j_list_lock: Protects the buffer lists and internal buffer state.
591 * @j_inode: Optional inode where we store the journal.  If present, all journal
592 *     block numbers are mapped into this inode via bmap().
593 * @j_tail_sequence:  Sequence number of the oldest transaction in the log
594 * @j_transaction_sequence: Sequence number of the next transaction to grant
595 * @j_commit_sequence: Sequence number of the most recently committed
596 *  transaction
597 * @j_commit_request: Sequence number of the most recent transaction wanting
598 *     commit
599 * @j_uuid: Uuid of client object.
600 * @j_task: Pointer to the current commit thread for this journal
601 * @j_max_transaction_buffers:  Maximum number of metadata buffers to allow in a
602 *     single compound commit transaction
603 * @j_commit_interval: What is the maximum transaction lifetime before we begin
604 *  a commit?
605 * @j_commit_timer:  The timer used to wakeup the commit thread
606 * @j_revoke_lock: Protect the revoke table
607 * @j_revoke: The revoke table - maintains the list of revoked blocks in the
608 *     current transaction.
609 * @j_revoke_table: alternate revoke tables for j_revoke
610 * @j_wbuf: array of buffer_heads for hfsplus_jbd_commit_transaction
611 * @j_wbufsize: maximum number of buffer_heads allowed in j_wbuf, the
612 *	number that will fit in j_blocksize
613 * @j_private: An opaque pointer to fs-private information.
614 */
615
616struct hfsplus_jbd_s
617{
618	/* General journaling state flags [j_state_lock] */
619	unsigned long		j_flags;
620
621	/*
622	 * Is there an outstanding uncleared error on the journal (from a prior
623	 * abort)? [j_state_lock]
624	 */
625	int			j_errno;
626
627	/* The superblock buffer */
628	struct buffer_head	*j_sb_buffer;
629	hfsplus_jbd_superblock_t	*j_superblock;
630
631	/* Version of the superblock format */
632	int			j_format_version;
633
634	/*
635	 * Protect the various scalars in the journal
636	 */
637	spinlock_t		j_state_lock;
638
639	/*
640	 * Number of processes waiting to create a barrier lock [j_state_lock]
641	 */
642	int			j_barrier_count;
643
644	/* The barrier lock itself */
645	struct semaphore	j_barrier;
646
647	/*
648	 * Transactions: The current running transaction...
649	 * [j_state_lock] [caller holding open handle]
650	 */
651	hfsplus_transaction_t		*j_running_transaction;
652
653	/*
654	 * the transaction we are pushing to disk
655	 * [j_state_lock] [caller holding open handle]
656	 */
657	hfsplus_transaction_t		*j_committing_transaction;
658
659	/*
660	 * ... and a linked circular list of all transactions waiting for
661	 * checkpointing. [j_list_lock]
662	 */
663	hfsplus_transaction_t		*j_checkpoint_transactions;
664
665	/*
666	 * Wait queue for waiting for a locked transaction to start committing,
667	 * or for a barrier lock to be released
668	 */
669	wait_queue_head_t	j_wait_transaction_locked;
670
671	/* Wait queue for waiting for checkpointing to complete */
672	wait_queue_head_t	j_wait_logspace;
673
674	/* Wait queue for waiting for commit to complete */
675	wait_queue_head_t	j_wait_done_commit;
676
677	/* Wait queue to trigger checkpointing */
678	wait_queue_head_t	j_wait_checkpoint;
679
680	/* Wait queue to trigger commit */
681	wait_queue_head_t	j_wait_commit;
682
683	/* Wait queue to wait for updates to complete */
684	wait_queue_head_t	j_wait_updates;
685
686	/* Semaphore for locking against concurrent checkpoints */
687	struct semaphore 	j_checkpoint_sem;
688
689	/*
690	 * Journal head: identifies the first unused block in the journal.
691	 * [j_state_lock]
692	 */
693	unsigned long		j_head;
694
695	/*
696	 * Journal tail: identifies the oldest still-used block in the journal.
697	 * [j_state_lock]
698	 */
699	unsigned long		j_tail;
700
701	/*
702	 * Journal free: how many free blocks are there in the journal?
703	 * [j_state_lock]
704	 */
705	unsigned long		j_free;
706
707	/*
708	 * Journal start and end: the block numbers of the first usable block
709	 * and one beyond the last usable block in the journal. [j_state_lock]
710	 */
711	unsigned long		j_first;
712	unsigned long		j_last;
713
714	/*
715	 * Device, blocksize and starting block offset for the location where we
716	 * store the journal.
717	 */
718	struct block_device	*j_dev;
719	int			j_blocksize;
720	unsigned int		j_blk_offset;
721
722	/*
723	 * Device which holds the client fs.  For internal journal this will be
724	 * equal to j_dev.
725	 */
726	struct block_device	*j_fs_dev;
727
728	/* Total maximum capacity of the journal region on disk. */
729	unsigned int		j_maxlen;
730
731	/*
732	 * Protects the buffer lists and internal buffer state.
733	 */
734	spinlock_t		j_list_lock;
735
736	/* Optional inode where we store the journal.  If present, all */
737	/* journal block numbers are mapped into this inode via */
738	/* bmap(). */
739	struct inode		*j_inode;
740
741	/*
742	 * Sequence number of the oldest transaction in the log [j_state_lock]
743	 */
744	hfsplus_jbd_tid_t			j_tail_sequence;
745
746	/*
747	 * Sequence number of the next transaction to grant [j_state_lock]
748	 */
749	hfsplus_jbd_tid_t			j_transaction_sequence;
750
751	/*
752	 * Sequence number of the most recently committed transaction
753	 * [j_state_lock].
754	 */
755	hfsplus_jbd_tid_t			j_commit_sequence;
756
757	/*
758	 * Sequence number of the most recent transaction wanting commit
759	 * [j_state_lock]
760	 */
761	hfsplus_jbd_tid_t			j_commit_request;
762
763	/*
764	 * Journal uuid: identifies the object (filesystem, LVM volume etc)
765	 * backed by this journal.  This will eventually be replaced by an array
766	 * of uuids, allowing us to index multiple devices within a single
767	 * journal and to perform atomic updates across them.
768	 */
769	__u8			j_uuid[16];
770
771	/* Pointer to the current commit thread for this journal */
772	struct task_struct	*j_task;
773
774	/*
775	 * Maximum number of metadata buffers to allow in a single compound
776	 * commit transaction
777	 */
778	int			j_max_transaction_buffers;
779
780	/*
781	 * What is the maximum transaction lifetime before we begin a commit?
782	 */
783	unsigned long		j_commit_interval;
784
785	/* The timer used to wakeup the commit thread: */
786	struct timer_list	*j_commit_timer;
787
788	/*
789	 * The revoke table: maintains the list of revoked blocks in the
790	 * current transaction.  [j_revoke_lock]
791	 */
792	spinlock_t		j_revoke_lock;
793	struct hfsplus_jbd_revoke_table_s *j_revoke;
794	struct hfsplus_jbd_revoke_table_s *j_revoke_table[2];
795
796	/*
797	 * array of bhs for hfsplus_jbd_commit_transaction
798	 */
799	struct buffer_head	**j_wbuf;
800	int			j_wbufsize;
801
802	/*
803	 * An opaque pointer to fs-private information.  ext3 puts its
804	 * superblock pointer here
805	 */
806	void *j_private;
807};
808
809/*
810 * Journal flag definitions
811 */
812#define JFS_UNMOUNT	0x001	/* Journal thread is being destroyed */
813#define JFS_ABORT	0x002	/* Journaling has been aborted for errors. */
814#define JFS_ACK_ERR	0x004	/* The errno in the sb has been acked */
815#define JFS_FLUSHED	0x008	/* The journal superblock has been flushed */
816#define JFS_LOADED	0x010	/* The journal superblock has been loaded */
817#define JFS_BARRIER	0x020	/* Use IDE barriers */
818
819/*
820 * Function declarations for the journaling transaction and buffer
821 * management
822 */
823
824/* Filing buffers */
825extern void __hfsplus_jbd_temp_unlink_buffer(struct hfsplus_jbd_head *jh);
826extern void hfsplus_jbd_unfile_buffer(hfsplus_jbd_t *, struct hfsplus_jbd_head *);
827extern void __hfsplus_jbd_unfile_buffer(struct hfsplus_jbd_head *);
828extern void __hfsplus_jbd_refile_buffer(struct hfsplus_jbd_head *);
829extern void hfsplus_jbd_refile_buffer(hfsplus_jbd_t *, struct hfsplus_jbd_head *);
830extern void __hfsplus_jbd_file_buffer(struct hfsplus_jbd_head *, hfsplus_transaction_t *, int);
831extern void __hfsplus_jbd_free_buffer(struct hfsplus_jbd_head *bh);
832extern void hfsplus_jbd_file_buffer(struct hfsplus_jbd_head *, hfsplus_transaction_t *, int);
833extern void __hfsplus_jbd_clean_data_list(hfsplus_transaction_t *transaction);
834
835/* Log buffer allocation */
836extern struct hfsplus_jbd_head * hfsplus_jbd_get_descriptor_buffer(hfsplus_jbd_t *);
837int hfsplus_jbd_next_log_block(hfsplus_jbd_t *, unsigned long *);
838
839/* Commit management */
840extern void hfsplus_jbd_commit_transaction(hfsplus_jbd_t *);
841
842/* Checkpoint list management */
843int __hfsplus_jbd_clean_checkpoint_list(hfsplus_jbd_t *journal);
844void __hfsplus_jbd_remove_checkpoint(struct hfsplus_jbd_head *);
845void __hfsplus_jbd_insert_checkpoint(struct hfsplus_jbd_head *, hfsplus_transaction_t *);
846
847/* Buffer IO */
848extern int
849hfsplus_jbd_write_metadata_buffer(hfsplus_transaction_t	  *transaction,
850			      struct hfsplus_jbd_head  *jh_in,
851			      struct hfsplus_jbd_head **jh_out,
852			      int		   blocknr);
853
854/* Transaction locking */
855extern void		__hfsplus__wait_on_journal (hfsplus_jbd_t *);
856
857/*
858 * Journal locking.
859 *
860 * We need to lock the journal during transaction state changes so that nobody
861 * ever tries to take a handle on the running transaction while we are in the
862 * middle of moving it to the commit phase.  j_state_lock does this.
863 *
864 * Note that the locking is completely interrupt unsafe.  We never touch
865 * journal structures from interrupts.
866 */
867
868static inline hfsplus_handle_t *hfsplus_jbd_current_handle(void)
869{
870	return current->journal_info;
871}
872
873/* The journaling code user interface:
874 *
875 * Create and destroy handles
876 * Register buffer modifications against the current transaction.
877 */
878
879extern hfsplus_jbd_handle_t *hfsplus_jbd_start(hfsplus_jbd_t *, int nblocks, hfsplus_handle_t *hfsplus_handle);
880extern int	 hfsplus_jbd_restart (hfsplus_jbd_handle_t *, int nblocks);
881extern int	 hfsplus_jbd_extend (hfsplus_jbd_handle_t *, int nblocks);
882extern int	 hfsplus_jbd_get_write_access(hfsplus_jbd_handle_t *, struct buffer_head *);
883extern int	 hfsplus_jbd_get_create_access (hfsplus_jbd_handle_t *, struct buffer_head *);
884extern int	 hfsplus_jbd_get_undo_access(hfsplus_jbd_handle_t *, struct buffer_head *);
885extern int	 hfsplus_jbd_dirty_data (hfsplus_jbd_handle_t *, struct buffer_head *);
886extern int	 hfsplus_jbd_dirty_metadata (hfsplus_jbd_handle_t *, struct buffer_head *);
887extern void	 hfsplus_jbd_release_buffer (hfsplus_jbd_handle_t *, struct buffer_head *);
888extern int	 hfsplus_jbd_forget (hfsplus_jbd_handle_t *, struct buffer_head *);
889extern void	 hfsplus_jbd_sync_buffer (struct buffer_head *);
890extern int	 hfsplus_jbd_invalidatepage(hfsplus_jbd_t *,
891				struct page *, unsigned long);
892extern int	 hfsplus_jbd_try_to_free_buffers(hfsplus_jbd_t *, struct page *, gfp_t);
893extern int	 hfsplus_jbd_stop(hfsplus_jbd_handle_t *);
894extern int	 hfsplus_jbd_flush (hfsplus_jbd_t *);
895extern void	 hfsplus_jbd_lock_updates (hfsplus_jbd_t *);
896extern void	 hfsplus_jbd_unlock_updates (hfsplus_jbd_t *);
897
898extern hfsplus_jbd_t * hfsplus_jbd_init_dev(struct block_device *bdev,
899				struct block_device *fs_dev,
900				int start, int len, int bsize);
901extern hfsplus_jbd_t * hfsplus_jbd_init_inode (struct inode *);
902extern int	   hfsplus_jbd_update_format (hfsplus_jbd_t *);
903extern int	   hfsplus_jbd_check_used_features
904		   (hfsplus_jbd_t *, unsigned long, unsigned long, unsigned long);
905extern int	   hfsplus_jbd_check_available_features
906		   (hfsplus_jbd_t *, unsigned long, unsigned long, unsigned long);
907extern int	   hfsplus_jbd_set_features
908		   (hfsplus_jbd_t *, unsigned long, unsigned long, unsigned long);
909extern int	   hfsplus_jbd_create     (hfsplus_jbd_t *);
910extern int	   hfsplus_jbd_load       (hfsplus_jbd_t *journal);
911extern void	   hfsplus_jbd_destroy    (hfsplus_jbd_t *);
912extern int	   hfsplus_jbd_recover    (hfsplus_jbd_t *journal);
913extern int	   hfsplus_jbd_wipe       (hfsplus_jbd_t *, int);
914extern int	   hfsplus_jbd_skip_recovery	(hfsplus_jbd_t *);
915extern void	   hfsplus_jbd_update_superblock	(hfsplus_jbd_t *, int);
916extern void	   __hfsplus_jbd_abort_hard	(hfsplus_jbd_t *);
917extern void	   hfsplus_jbd_abort      (hfsplus_jbd_t *, int);
918extern int	   hfsplus_jbd_errno      (hfsplus_jbd_t *);
919extern void	   hfsplus_jbd_ack_err    (hfsplus_jbd_t *);
920extern int	   hfsplus_jbd_clear_err  (hfsplus_jbd_t *);
921extern int	   hfsplus_jbd_bmap(hfsplus_jbd_t *, unsigned long, unsigned long *);
922extern int	   hfsplus_jbd_force_commit(hfsplus_jbd_t *);
923
924/*
925 * hfsplus_jbd_head management
926 */
927struct hfsplus_jbd_head *hfsplus_jbd_add_journal_head(struct buffer_head *bh);
928struct hfsplus_jbd_head *hfsplus_jbd_grab_journal_head(struct buffer_head *bh);
929void hfsplus_jbd_remove_journal_head(struct buffer_head *bh);
930void hfsplus_jbd_put_journal_head(struct hfsplus_jbd_head *jh);
931
932/*
933 * handle management
934 */
935extern kmem_cache_t *hfsplus_jbd_handle_cache;
936
937static inline hfsplus_jbd_handle_t *hfsplus_jbd_alloc_handle(gfp_t gfp_flags)
938{
939	return kmem_cache_alloc(hfsplus_jbd_handle_cache, gfp_flags);
940}
941
942static inline void hfsplus_jbd_free_handle(hfsplus_jbd_handle_t *handle)
943{
944	kmem_cache_free(hfsplus_jbd_handle_cache, handle);
945}
946
947/* Primary revoke support */
948#define HFSPLUS_JBD_REVOKE_DEFAULT_HASH 256
949extern int	   hfsplus_jbd_init_revoke(hfsplus_jbd_t *, int);
950extern void	   hfsplus_jbd_destroy_revoke_caches(void);
951extern int	   hfsplus_jbd_init_revoke_caches(void);
952
953extern void	   hfsplus_jbd_destroy_revoke(hfsplus_jbd_t *);
954extern int	   hfsplus_jbd_revoke (hfsplus_jbd_handle_t *,
955				unsigned long, struct buffer_head *);
956extern int	   hfsplus_jbd_cancel_revoke(hfsplus_jbd_handle_t *, struct hfsplus_jbd_head *);
957extern void	   hfsplus_jbd_write_revoke_records(hfsplus_jbd_t *, hfsplus_transaction_t *);
958
959/* Recovery revoke support */
960extern int	hfsplus_jbd_set_revoke(hfsplus_jbd_t *, unsigned long, hfsplus_jbd_tid_t);
961extern int	hfsplus_jbd_test_revoke(hfsplus_jbd_t *, unsigned long, hfsplus_jbd_tid_t);
962extern void	hfsplus_jbd_clear_revoke(hfsplus_jbd_t *);
963extern void	hfsplus_jbd_brelse_array(struct buffer_head *b[], int n);
964extern void	hfsplus_jbd_switch_revoke_table(hfsplus_jbd_t *journal);
965
966/*
967 * The log thread user interface:
968 *
969 * Request space in the current transaction, and force transaction commit
970 * transitions on demand.
971 */
972
973int __hfsplus__log_space_left(hfsplus_jbd_t *); /* Called with journal locked */
974int hfsplus_log_start_commit(hfsplus_jbd_t *journal, hfsplus_jbd_tid_t tid);
975int __hfsplus__log_start_commit(hfsplus_jbd_t *journal, hfsplus_jbd_tid_t tid);
976int hfsplus_jbd_start_commit(hfsplus_jbd_t *journal, hfsplus_jbd_tid_t *tid);
977int hfsplus_jbd_force_commit_nested(hfsplus_jbd_t *journal);
978int hfsplus_jbd_log_wait_commit(hfsplus_jbd_t *journal, hfsplus_jbd_tid_t tid);
979int hfsplus_jbd_log_do_checkpoint(hfsplus_jbd_t *journal);
980
981void __hfsplus__log_wait_for_space(hfsplus_jbd_t *journal);
982extern void	__hfsplus_jbd_drop_transaction(hfsplus_jbd_t *, hfsplus_transaction_t *);
983extern int	cleanup_hfsplus_jbd_tail(hfsplus_jbd_t *);
984/* Debugging code only: */
985
986void hfsplus_jbd_exit(void);
987int hfsplus_jbd_init(void);
988
989#define hfsplus_jbd_ENOSYS() \
990do {								           \
991	printk (KERN_ERR "HFSPLUS_JBD unimplemented function %s\n", __FUNCTION__); \
992	current->state = TASK_UNINTERRUPTIBLE;			           \
993	schedule();						           \
994} while (1)
995
996/*
997 * is_hfsplus_jbd_abort
998 *
999 * Simple test wrapper function to test the JFS_ABORT state flag.  This
1000 * bit, when set, indicates that we have had a fatal error somewhere,
1001 * either inside the journaling layer or indicated to us by the client
1002 * (eg. ext3), and that we and should not commit any further
1003 * transactions.
1004 */
1005
1006static inline int is_hfsplus_jbd_aborted(hfsplus_jbd_t *journal)
1007{
1008	return journal->j_flags & JFS_ABORT;
1009}
1010
1011static inline int hfsplus_jbd_is_handle_aborted(hfsplus_jbd_handle_t *handle)
1012{
1013	if (handle->h_aborted)
1014		return 1;
1015	return is_hfsplus_jbd_aborted(handle->h_transaction->t_journal);
1016}
1017
1018static inline void hfsplus_jbd_abort_handle(hfsplus_jbd_handle_t *handle)
1019{
1020	handle->h_aborted = 1;
1021}
1022
1023#endif /* __KERNEL__   */
1024
1025/* Comparison functions for transaction IDs: perform comparisons using
1026 * modulo arithmetic so that they work over sequence number wraps. */
1027
1028static inline int hfsplus_tid_gt(hfsplus_jbd_tid_t x, hfsplus_jbd_tid_t y)
1029{
1030	int difference = (x - y);
1031	return (difference > 0);
1032}
1033
1034static inline int hfsplus_tid_geq(hfsplus_jbd_tid_t x, hfsplus_jbd_tid_t y)
1035{
1036	int difference = (x - y);
1037	return (difference >= 0);
1038}
1039
1040extern int hfsplus_jbd_blocks_per_page(struct inode *inode);
1041
1042/*
1043 * Return the minimum number of blocks which must be free in the journal
1044 * before a new transaction may be started.  Must be called under j_state_lock.
1045 */
1046static inline int hfsplus_jbd_space_needed(hfsplus_jbd_t *journal)
1047{
1048	int nblocks = journal->j_max_transaction_buffers;
1049	if (journal->j_committing_transaction)
1050		nblocks += journal->j_committing_transaction->
1051					t_outstanding_credits;
1052	return nblocks;
1053}
1054
1055/*
1056 * Definitions which augment the buffer_head layer
1057 */
1058
1059/* journaling buffer types */
1060#define HFSPLUS_BJ_None		0	/* Not journaled */
1061#define HFSPLUS_BJ_SyncData	1	/* Normal data: flush before commit */
1062#define HFSPLUS_BJ_Metadata	2	/* Normal journaled metadata */
1063#define HFSPLUS_BJ_Forget	3	/* Buffer superseded by this transaction */
1064#define HFSPLUS_BJ_IO		4	/* Buffer is for temporary IO use */
1065#define HFSPLUS_BJ_Shadow	5	/* Buffer contents being shadowed to the log */
1066#define HFSPLUS_BJ_LogCtl	6	/* Buffer contains log descriptors */
1067#define HFSPLUS_BJ_Reserved	7	/* Buffer is reserved for access by journal */
1068#define HFSPLUS_BJ_Locked	8	/* Locked for I/O during commit */
1069#define HFSPLUS_BJ_Types	9
1070
1071extern int hfsplus_jbd_blocks_per_page(struct inode *inode);
1072
1073#ifdef __KERNEL__
1074
1075#define HFSPLUS_buffer_trace_init(bh)	do {} while (0)
1076#define HFSPLUS_print_buffer_fields(bh)	do {} while (0)
1077#define HFSPLUS_print_buffer_trace(bh)	do {} while (0)
1078#define HFSPLUS_BUFFER_TRACE(bh, info)	do {} while (0)
1079#define HFSPLUS_BUFFER_TRACE2(bh, bh2, info)	do {} while (0)
1080#define HFSPLUS_JBUFFER_TRACE(jh, info)	do {} while (0)
1081
1082#endif	/* __KERNEL__ */
1083
1084#endif	/* _LINUX_HFSPLUS_JBD_H */
1085