jbd.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. #ifndef _LINUX_JBD_H
  16. #define _LINUX_JBD_H
  17. /* Allow this file to be included directly into e2fsprogs */
  18. #ifndef __KERNEL__
  19. #include "jfs_compat.h"
  20. #define JFS_DEBUG
  21. #define jfs_debug jbd_debug
  22. #else
  23. #include <linux/types.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/journal-head.h>
  26. #include <linux/stddef.h>
  27. #include <linux/bit_spinlock.h>
  28. #include <linux/mutex.h>
  29. #include <linux/timer.h>
  30. #include <linux/lockdep.h>
  31. #define journal_oom_retry 1
  32. /*
  33. * Define JBD_PARANOID_IOFAIL to cause a kernel BUG() if ext3 finds
  34. * certain classes of error which can occur due to failed IOs. Under
  35. * normal use we want ext3 to continue after such errors, because
  36. * hardware _can_ fail, but for debugging purposes when running tests on
  37. * known-good hardware we may want to trap these errors.
  38. */
  39. #undef JBD_PARANOID_IOFAIL
  40. /*
  41. * The default maximum commit age, in seconds.
  42. */
  43. #define JBD_DEFAULT_MAX_COMMIT_AGE 5
  44. #ifdef CONFIG_JBD_DEBUG
  45. /*
  46. * Define JBD_EXPENSIVE_CHECKING to enable more expensive internal
  47. * consistency checks. By default we don't do this unless
  48. * CONFIG_JBD_DEBUG is on.
  49. */
  50. #define JBD_EXPENSIVE_CHECKING
  51. extern u8 journal_enable_debug;
  52. #define jbd_debug(n, f, a...) \
  53. do { \
  54. if ((n) <= journal_enable_debug) { \
  55. printk (KERN_DEBUG "(%s, %d): %s: ", \
  56. __FILE__, __LINE__, __func__); \
  57. printk (f, ## a); \
  58. } \
  59. } while (0)
  60. #else
  61. #define jbd_debug(f, a...) /**/
  62. #endif
  63. static inline void *jbd_alloc(size_t size, gfp_t flags)
  64. {
  65. return (void *)__get_free_pages(flags, get_order(size));
  66. }
  67. static inline void jbd_free(void *ptr, size_t size)
  68. {
  69. free_pages((unsigned long)ptr, get_order(size));
  70. };
  71. #define JFS_MIN_JOURNAL_BLOCKS 1024
  72. /**
  73. * typedef handle_t - The handle_t type represents a single atomic update being performed by some process.
  74. *
  75. * All filesystem modifications made by the process go
  76. * through this handle. Recursive operations (such as quota operations)
  77. * are gathered into a single update.
  78. *
  79. * The buffer credits field is used to account for journaled buffers
  80. * being modified by the running process. To ensure that there is
  81. * enough log space for all outstanding operations, we need to limit the
  82. * number of outstanding buffers possible at any time. When the
  83. * operation completes, any buffer credits not used are credited back to
  84. * the transaction, so that at all times we know how many buffers the
  85. * outstanding updates on a transaction might possibly touch.
  86. *
  87. * This is an opaque datatype.
  88. **/
  89. typedef struct handle_s handle_t; /* Atomic operation type */
  90. /**
  91. * typedef journal_t - The journal_t maintains all of the journaling state information for a single filesystem.
  92. *
  93. * journal_t is linked to from the fs superblock structure.
  94. *
  95. * We use the journal_t to keep track of all outstanding transaction
  96. * activity on the filesystem, and to manage the state of the log
  97. * writing process.
  98. *
  99. * This is an opaque datatype.
  100. **/
  101. typedef struct journal_s journal_t; /* Journal control structure */
  102. #endif
  103. /*
  104. * Internal structures used by the logging mechanism:
  105. */
  106. #define JFS_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
  107. /*
  108. * On-disk structures
  109. */
  110. /*
  111. * Descriptor block types:
  112. */
  113. #define JFS_DESCRIPTOR_BLOCK 1
  114. #define JFS_COMMIT_BLOCK 2
  115. #define JFS_SUPERBLOCK_V1 3
  116. #define JFS_SUPERBLOCK_V2 4
  117. #define JFS_REVOKE_BLOCK 5
  118. /*
  119. * Standard header for all descriptor blocks:
  120. */
  121. typedef struct journal_header_s
  122. {
  123. __be32 h_magic;
  124. __be32 h_blocktype;
  125. __be32 h_sequence;
  126. } journal_header_t;
  127. /*
  128. * The block tag: used to describe a single buffer in the journal
  129. */
  130. typedef struct journal_block_tag_s
  131. {
  132. __be32 t_blocknr; /* The on-disk block number */
  133. __be32 t_flags; /* See below */
  134. } journal_block_tag_t;
  135. /*
  136. * The revoke descriptor: used on disk to describe a series of blocks to
  137. * be revoked from the log
  138. */
  139. typedef struct journal_revoke_header_s
  140. {
  141. journal_header_t r_header;
  142. __be32 r_count; /* Count of bytes used in the block */
  143. } journal_revoke_header_t;
  144. /* Definitions for the journal tag flags word: */
  145. #define JFS_FLAG_ESCAPE 1 /* on-disk block is escaped */
  146. #define JFS_FLAG_SAME_UUID 2 /* block has same uuid as previous */
  147. #define JFS_FLAG_DELETED 4 /* block deleted by this transaction */
  148. #define JFS_FLAG_LAST_TAG 8 /* last tag in this descriptor block */
  149. /*
  150. * The journal superblock. All fields are in big-endian byte order.
  151. */
  152. typedef struct journal_superblock_s
  153. {
  154. /* 0x0000 */
  155. journal_header_t s_header;
  156. /* 0x000C */
  157. /* Static information describing the journal */
  158. __be32 s_blocksize; /* journal device blocksize */
  159. __be32 s_maxlen; /* total blocks in journal file */
  160. __be32 s_first; /* first block of log information */
  161. /* 0x0018 */
  162. /* Dynamic information describing the current state of the log */
  163. __be32 s_sequence; /* first commit ID expected in log */
  164. __be32 s_start; /* blocknr of start of log */
  165. /* 0x0020 */
  166. /* Error value, as set by journal_abort(). */
  167. __be32 s_errno;
  168. /* 0x0024 */
  169. /* Remaining fields are only valid in a version-2 superblock */
  170. __be32 s_feature_compat; /* compatible feature set */
  171. __be32 s_feature_incompat; /* incompatible feature set */
  172. __be32 s_feature_ro_compat; /* readonly-compatible feature set */
  173. /* 0x0030 */
  174. __u8 s_uuid[16]; /* 128-bit uuid for journal */
  175. /* 0x0040 */
  176. __be32 s_nr_users; /* Nr of filesystems sharing log */
  177. __be32 s_dynsuper; /* Blocknr of dynamic superblock copy*/
  178. /* 0x0048 */
  179. __be32 s_max_transaction; /* Limit of journal blocks per trans.*/
  180. __be32 s_max_trans_data; /* Limit of data blocks per trans. */
  181. /* 0x0050 */
  182. __u32 s_padding[44];
  183. /* 0x0100 */
  184. __u8 s_users[16*48]; /* ids of all fs'es sharing the log */
  185. /* 0x0400 */
  186. } journal_superblock_t;
  187. #define JFS_HAS_COMPAT_FEATURE(j,mask) \
  188. ((j)->j_format_version >= 2 && \
  189. ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask))))
  190. #define JFS_HAS_RO_COMPAT_FEATURE(j,mask) \
  191. ((j)->j_format_version >= 2 && \
  192. ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask))))
  193. #define JFS_HAS_INCOMPAT_FEATURE(j,mask) \
  194. ((j)->j_format_version >= 2 && \
  195. ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
  196. #define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
  197. /* Features known to this kernel version: */
  198. #define JFS_KNOWN_COMPAT_FEATURES 0
  199. #define JFS_KNOWN_ROCOMPAT_FEATURES 0
  200. #define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE
  201. #ifdef __KERNEL__
  202. #include <linux/fs.h>
  203. #include <linux/sched.h>
  204. #define J_ASSERT(assert) BUG_ON(!(assert))
  205. #if defined(CONFIG_BUFFER_DEBUG)
  206. void buffer_assertion_failure(struct buffer_head *bh);
  207. #define J_ASSERT_BH(bh, expr) \
  208. do { \
  209. if (!(expr)) \
  210. buffer_assertion_failure(bh); \
  211. J_ASSERT(expr); \
  212. } while (0)
  213. #define J_ASSERT_JH(jh, expr) J_ASSERT_BH(jh2bh(jh), expr)
  214. #else
  215. #define J_ASSERT_BH(bh, expr) J_ASSERT(expr)
  216. #define J_ASSERT_JH(jh, expr) J_ASSERT(expr)
  217. #endif
  218. #if defined(JBD_PARANOID_IOFAIL)
  219. #define J_EXPECT(expr, why...) J_ASSERT(expr)
  220. #define J_EXPECT_BH(bh, expr, why...) J_ASSERT_BH(bh, expr)
  221. #define J_EXPECT_JH(jh, expr, why...) J_ASSERT_JH(jh, expr)
  222. #else
  223. #define __journal_expect(expr, why...) \
  224. ({ \
  225. int val = (expr); \
  226. if (!val) { \
  227. printk(KERN_ERR \
  228. "EXT3-fs unexpected failure: %s;\n",# expr); \
  229. printk(KERN_ERR why "\n"); \
  230. } \
  231. val; \
  232. })
  233. #define J_EXPECT(expr, why...) __journal_expect(expr, ## why)
  234. #define J_EXPECT_BH(bh, expr, why...) __journal_expect(expr, ## why)
  235. #define J_EXPECT_JH(jh, expr, why...) __journal_expect(expr, ## why)
  236. #endif
  237. enum jbd_state_bits {
  238. BH_JBD /* Has an attached ext3 journal_head */
  239. = BH_PrivateStart,
  240. BH_JWrite, /* Being written to log (@@@ DEBUGGING) */
  241. BH_Freed, /* Has been freed (truncated) */
  242. BH_Revoked, /* Has been revoked from the log */
  243. BH_RevokeValid, /* Revoked flag is valid */
  244. BH_JBDDirty, /* Is dirty but journaled */
  245. BH_State, /* Pins most journal_head state */
  246. BH_JournalHead, /* Pins bh->b_private and jh->b_bh */
  247. BH_Unshadow, /* Dummy bit, for BJ_Shadow wakeup filtering */
  248. };
  249. BUFFER_FNS(JBD, jbd)
  250. BUFFER_FNS(JWrite, jwrite)
  251. BUFFER_FNS(JBDDirty, jbddirty)
  252. TAS_BUFFER_FNS(JBDDirty, jbddirty)
  253. BUFFER_FNS(Revoked, revoked)
  254. TAS_BUFFER_FNS(Revoked, revoked)
  255. BUFFER_FNS(RevokeValid, revokevalid)
  256. TAS_BUFFER_FNS(RevokeValid, revokevalid)
  257. BUFFER_FNS(Freed, freed)
  258. static inline struct buffer_head *jh2bh(struct journal_head *jh)
  259. {
  260. return jh->b_bh;
  261. }
  262. static inline struct journal_head *bh2jh(struct buffer_head *bh)
  263. {
  264. return bh->b_private;
  265. }
  266. static inline void jbd_lock_bh_state(struct buffer_head *bh)
  267. {
  268. bit_spin_lock(BH_State, &bh->b_state);
  269. }
  270. static inline int jbd_trylock_bh_state(struct buffer_head *bh)
  271. {
  272. return bit_spin_trylock(BH_State, &bh->b_state);
  273. }
  274. static inline int jbd_is_locked_bh_state(struct buffer_head *bh)
  275. {
  276. return bit_spin_is_locked(BH_State, &bh->b_state);
  277. }
  278. static inline void jbd_unlock_bh_state(struct buffer_head *bh)
  279. {
  280. bit_spin_unlock(BH_State, &bh->b_state);
  281. }
  282. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  283. {
  284. bit_spin_lock(BH_JournalHead, &bh->b_state);
  285. }
  286. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  287. {
  288. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  289. }
  290. struct jbd_revoke_table_s;
  291. /**
  292. * struct handle_s - this is the concrete type associated with handle_t.
  293. * @h_transaction: Which compound transaction is this update a part of?
  294. * @h_buffer_credits: Number of remaining buffers we are allowed to dirty.
  295. * @h_ref: Reference count on this handle
  296. * @h_err: Field for caller's use to track errors through large fs operations
  297. * @h_sync: flag for sync-on-close
  298. * @h_jdata: flag to force data journaling
  299. * @h_aborted: flag indicating fatal error on handle
  300. * @h_lockdep_map: lockdep info for debugging lock problems
  301. */
  302. struct handle_s
  303. {
  304. /* Which compound transaction is this update a part of? */
  305. transaction_t *h_transaction;
  306. /* Number of remaining buffers we are allowed to dirty: */
  307. int h_buffer_credits;
  308. /* Reference count on this handle */
  309. int h_ref;
  310. /* Field for caller's use to track errors through large fs */
  311. /* operations */
  312. int h_err;
  313. /* Flags [no locking] */
  314. unsigned int h_sync: 1; /* sync-on-close */
  315. unsigned int h_jdata: 1; /* force data journaling */
  316. unsigned int h_aborted: 1; /* fatal error on handle */
  317. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  318. struct lockdep_map h_lockdep_map;
  319. #endif
  320. };
  321. /* The transaction_t type is the guts of the journaling mechanism. It
  322. * tracks a compound transaction through its various states:
  323. *
  324. * RUNNING: accepting new updates
  325. * LOCKED: Updates still running but we don't accept new ones
  326. * RUNDOWN: Updates are tidying up but have finished requesting
  327. * new buffers to modify (state not used for now)
  328. * FLUSH: All updates complete, but we are still writing to disk
  329. * COMMIT: All data on disk, writing commit record
  330. * FINISHED: We still have to keep the transaction for checkpointing.
  331. *
  332. * The transaction keeps track of all of the buffers modified by a
  333. * running transaction, and all of the buffers committed but not yet
  334. * flushed to home for finished transactions.
  335. */
  336. /*
  337. * Lock ranking:
  338. *
  339. * j_list_lock
  340. * ->jbd_lock_bh_journal_head() (This is "innermost")
  341. *
  342. * j_state_lock
  343. * ->jbd_lock_bh_state()
  344. *
  345. * jbd_lock_bh_state()
  346. * ->j_list_lock
  347. *
  348. * j_state_lock
  349. * ->t_handle_lock
  350. *
  351. * j_state_lock
  352. * ->j_list_lock (journal_unmap_buffer)
  353. *
  354. */
  355. struct transaction_s
  356. {
  357. /* Pointer to the journal for this transaction. [no locking] */
  358. journal_t *t_journal;
  359. /* Sequence number for this transaction [no locking] */
  360. tid_t t_tid;
  361. /*
  362. * Transaction's current state
  363. * [no locking - only kjournald alters this]
  364. * [j_list_lock] guards transition of a transaction into T_FINISHED
  365. * state and subsequent call of __journal_drop_transaction()
  366. * FIXME: needs barriers
  367. * KLUDGE: [use j_state_lock]
  368. */
  369. enum {
  370. T_RUNNING,
  371. T_LOCKED,
  372. T_RUNDOWN,
  373. T_FLUSH,
  374. T_COMMIT,
  375. T_FINISHED
  376. } t_state;
  377. /*
  378. * Where in the log does this transaction's commit start? [no locking]
  379. */
  380. unsigned long t_log_start;
  381. /* Number of buffers on the t_buffers list [j_list_lock] */
  382. int t_nr_buffers;
  383. /*
  384. * Doubly-linked circular list of all buffers reserved but not yet
  385. * modified by this transaction [j_list_lock]
  386. */
  387. struct journal_head *t_reserved_list;
  388. /*
  389. * Doubly-linked circular list of all buffers under writeout during
  390. * commit [j_list_lock]
  391. */
  392. struct journal_head *t_locked_list;
  393. /*
  394. * Doubly-linked circular list of all metadata buffers owned by this
  395. * transaction [j_list_lock]
  396. */
  397. struct journal_head *t_buffers;
  398. /*
  399. * Doubly-linked circular list of all data buffers still to be
  400. * flushed before this transaction can be committed [j_list_lock]
  401. */
  402. struct journal_head *t_sync_datalist;
  403. /*
  404. * Doubly-linked circular list of all forget buffers (superseded
  405. * buffers which we can un-checkpoint once this transaction commits)
  406. * [j_list_lock]
  407. */
  408. struct journal_head *t_forget;
  409. /*
  410. * Doubly-linked circular list of all buffers still to be flushed before
  411. * this transaction can be checkpointed. [j_list_lock]
  412. */
  413. struct journal_head *t_checkpoint_list;
  414. /*
  415. * Doubly-linked circular list of all buffers submitted for IO while
  416. * checkpointing. [j_list_lock]
  417. */
  418. struct journal_head *t_checkpoint_io_list;
  419. /*
  420. * Doubly-linked circular list of temporary buffers currently undergoing
  421. * IO in the log [j_list_lock]
  422. */
  423. struct journal_head *t_iobuf_list;
  424. /*
  425. * Doubly-linked circular list of metadata buffers being shadowed by log
  426. * IO. The IO buffers on the iobuf list and the shadow buffers on this
  427. * list match each other one for one at all times. [j_list_lock]
  428. */
  429. struct journal_head *t_shadow_list;
  430. /*
  431. * Doubly-linked circular list of control buffers being written to the
  432. * log. [j_list_lock]
  433. */
  434. struct journal_head *t_log_list;
  435. /*
  436. * Protects info related to handles
  437. */
  438. spinlock_t t_handle_lock;
  439. /*
  440. * Number of outstanding updates running on this transaction
  441. * [t_handle_lock]
  442. */
  443. int t_updates;
  444. /*
  445. * Number of buffers reserved for use by all handles in this transaction
  446. * handle but not yet modified. [t_handle_lock]
  447. */
  448. int t_outstanding_credits;
  449. /*
  450. * Forward and backward links for the circular list of all transactions
  451. * awaiting checkpoint. [j_list_lock]
  452. */
  453. transaction_t *t_cpnext, *t_cpprev;
  454. /*
  455. * When will the transaction expire (become due for commit), in jiffies?
  456. * [no locking]
  457. */
  458. unsigned long t_expires;
  459. /*
  460. * When this transaction started, in nanoseconds [no locking]
  461. */
  462. ktime_t t_start_time;
  463. /*
  464. * How many handles used this transaction? [t_handle_lock]
  465. */
  466. int t_handle_count;
  467. /*
  468. * This transaction is being forced and some process is
  469. * waiting for it to finish.
  470. */
  471. int t_synchronous_commit:1;
  472. };
  473. /**
  474. * struct journal_s - this is the concrete type associated with journal_t.
  475. * @j_flags: General journaling state flags
  476. * @j_errno: Is there an outstanding uncleared error on the journal (from a
  477. * prior abort)?
  478. * @j_sb_buffer: First part of superblock buffer
  479. * @j_superblock: Second part of superblock buffer
  480. * @j_format_version: Version of the superblock format
  481. * @j_state_lock: Protect the various scalars in the journal
  482. * @j_barrier_count: Number of processes waiting to create a barrier lock
  483. * @j_barrier: The barrier lock itself
  484. * @j_running_transaction: The current running transaction..
  485. * @j_committing_transaction: the transaction we are pushing to disk
  486. * @j_checkpoint_transactions: a linked circular list of all transactions
  487. * waiting for checkpointing
  488. * @j_wait_transaction_locked: Wait queue for waiting for a locked transaction
  489. * to start committing, or for a barrier lock to be released
  490. * @j_wait_logspace: Wait queue for waiting for checkpointing to complete
  491. * @j_wait_done_commit: Wait queue for waiting for commit to complete
  492. * @j_wait_checkpoint: Wait queue to trigger checkpointing
  493. * @j_wait_commit: Wait queue to trigger commit
  494. * @j_wait_updates: Wait queue to wait for updates to complete
  495. * @j_checkpoint_mutex: Mutex for locking against concurrent checkpoints
  496. * @j_head: Journal head - identifies the first unused block in the journal
  497. * @j_tail: Journal tail - identifies the oldest still-used block in the
  498. * journal.
  499. * @j_free: Journal free - how many free blocks are there in the journal?
  500. * @j_first: The block number of the first usable block
  501. * @j_last: The block number one beyond the last usable block
  502. * @j_dev: Device where we store the journal
  503. * @j_blocksize: blocksize for the location where we store the journal.
  504. * @j_blk_offset: starting block offset for into the device where we store the
  505. * journal
  506. * @j_fs_dev: Device which holds the client fs. For internal journal this will
  507. * be equal to j_dev
  508. * @j_maxlen: Total maximum capacity of the journal region on disk.
  509. * @j_list_lock: Protects the buffer lists and internal buffer state.
  510. * @j_inode: Optional inode where we store the journal. If present, all journal
  511. * block numbers are mapped into this inode via bmap().
  512. * @j_tail_sequence: Sequence number of the oldest transaction in the log
  513. * @j_transaction_sequence: Sequence number of the next transaction to grant
  514. * @j_commit_sequence: Sequence number of the most recently committed
  515. * transaction
  516. * @j_commit_request: Sequence number of the most recent transaction wanting
  517. * commit
  518. * @j_uuid: Uuid of client object.
  519. * @j_task: Pointer to the current commit thread for this journal
  520. * @j_max_transaction_buffers: Maximum number of metadata buffers to allow in a
  521. * single compound commit transaction
  522. * @j_commit_interval: What is the maximum transaction lifetime before we begin
  523. * a commit?
  524. * @j_commit_timer: The timer used to wakeup the commit thread
  525. * @j_revoke_lock: Protect the revoke table
  526. * @j_revoke: The revoke table - maintains the list of revoked blocks in the
  527. * current transaction.
  528. * @j_revoke_table: alternate revoke tables for j_revoke
  529. * @j_wbuf: array of buffer_heads for journal_commit_transaction
  530. * @j_wbufsize: maximum number of buffer_heads allowed in j_wbuf, the
  531. * number that will fit in j_blocksize
  532. * @j_last_sync_writer: most recent pid which did a synchronous write
  533. * @j_average_commit_time: the average amount of time in nanoseconds it
  534. * takes to commit a transaction to the disk.
  535. * @j_private: An opaque pointer to fs-private information.
  536. */
  537. struct journal_s
  538. {
  539. /* General journaling state flags [j_state_lock] */
  540. unsigned long j_flags;
  541. /*
  542. * Is there an outstanding uncleared error on the journal (from a prior
  543. * abort)? [j_state_lock]
  544. */
  545. int j_errno;
  546. /* The superblock buffer */
  547. struct buffer_head *j_sb_buffer;
  548. journal_superblock_t *j_superblock;
  549. /* Version of the superblock format */
  550. int j_format_version;
  551. /*
  552. * Protect the various scalars in the journal
  553. */
  554. spinlock_t j_state_lock;
  555. /*
  556. * Number of processes waiting to create a barrier lock [j_state_lock]
  557. */
  558. int j_barrier_count;
  559. /* The barrier lock itself */
  560. struct mutex j_barrier;
  561. /*
  562. * Transactions: The current running transaction...
  563. * [j_state_lock] [caller holding open handle]
  564. */
  565. transaction_t *j_running_transaction;
  566. /*
  567. * the transaction we are pushing to disk
  568. * [j_state_lock] [caller holding open handle]
  569. */
  570. transaction_t *j_committing_transaction;
  571. /*
  572. * ... and a linked circular list of all transactions waiting for
  573. * checkpointing. [j_list_lock]
  574. */
  575. transaction_t *j_checkpoint_transactions;
  576. /*
  577. * Wait queue for waiting for a locked transaction to start committing,
  578. * or for a barrier lock to be released
  579. */
  580. wait_queue_head_t j_wait_transaction_locked;
  581. /* Wait queue for waiting for checkpointing to complete */
  582. wait_queue_head_t j_wait_logspace;
  583. /* Wait queue for waiting for commit to complete */
  584. wait_queue_head_t j_wait_done_commit;
  585. /* Wait queue to trigger checkpointing */
  586. wait_queue_head_t j_wait_checkpoint;
  587. /* Wait queue to trigger commit */
  588. wait_queue_head_t j_wait_commit;
  589. /* Wait queue to wait for updates to complete */
  590. wait_queue_head_t j_wait_updates;
  591. /* Semaphore for locking against concurrent checkpoints */
  592. struct mutex j_checkpoint_mutex;
  593. /*
  594. * Journal head: identifies the first unused block in the journal.
  595. * [j_state_lock]
  596. */
  597. unsigned long j_head;
  598. /*
  599. * Journal tail: identifies the oldest still-used block in the journal.
  600. * [j_state_lock]
  601. */
  602. unsigned long j_tail;
  603. /*
  604. * Journal free: how many free blocks are there in the journal?
  605. * [j_state_lock]
  606. */
  607. unsigned long j_free;
  608. /*
  609. * Journal start and end: the block numbers of the first usable block
  610. * and one beyond the last usable block in the journal. [j_state_lock]
  611. */
  612. unsigned long j_first;
  613. unsigned long j_last;
  614. /*
  615. * Device, blocksize and starting block offset for the location where we
  616. * store the journal.
  617. */
  618. struct block_device *j_dev;
  619. int j_blocksize;
  620. unsigned long j_blk_offset;
  621. /*
  622. * Device which holds the client fs. For internal journal this will be
  623. * equal to j_dev.
  624. */
  625. struct block_device *j_fs_dev;
  626. /* Total maximum capacity of the journal region on disk. */
  627. unsigned int j_maxlen;
  628. /*
  629. * Protects the buffer lists and internal buffer state.
  630. */
  631. spinlock_t j_list_lock;
  632. /* Optional inode where we store the journal. If present, all */
  633. /* journal block numbers are mapped into this inode via */
  634. /* bmap(). */
  635. struct inode *j_inode;
  636. /*
  637. * Sequence number of the oldest transaction in the log [j_state_lock]
  638. */
  639. tid_t j_tail_sequence;
  640. /*
  641. * Sequence number of the next transaction to grant [j_state_lock]
  642. */
  643. tid_t j_transaction_sequence;
  644. /*
  645. * Sequence number of the most recently committed transaction
  646. * [j_state_lock].
  647. */
  648. tid_t j_commit_sequence;
  649. /*
  650. * Sequence number of the most recent transaction wanting commit
  651. * [j_state_lock]
  652. */
  653. tid_t j_commit_request;
  654. /*
  655. * Journal uuid: identifies the object (filesystem, LVM volume etc)
  656. * backed by this journal. This will eventually be replaced by an array
  657. * of uuids, allowing us to index multiple devices within a single
  658. * journal and to perform atomic updates across them.
  659. */
  660. __u8 j_uuid[16];
  661. /* Pointer to the current commit thread for this journal */
  662. struct task_struct *j_task;
  663. /*
  664. * Maximum number of metadata buffers to allow in a single compound
  665. * commit transaction
  666. */
  667. int j_max_transaction_buffers;
  668. /*
  669. * What is the maximum transaction lifetime before we begin a commit?
  670. */
  671. unsigned long j_commit_interval;
  672. /* The timer used to wakeup the commit thread: */
  673. struct timer_list j_commit_timer;
  674. /*
  675. * The revoke table: maintains the list of revoked blocks in the
  676. * current transaction. [j_revoke_lock]
  677. */
  678. spinlock_t j_revoke_lock;
  679. struct jbd_revoke_table_s *j_revoke;
  680. struct jbd_revoke_table_s *j_revoke_table[2];
  681. /*
  682. * array of bhs for journal_commit_transaction
  683. */
  684. struct buffer_head **j_wbuf;
  685. int j_wbufsize;
  686. /*
  687. * this is the pid of the last person to run a synchronous operation
  688. * through the journal.
  689. */
  690. pid_t j_last_sync_writer;
  691. /*
  692. * the average amount of time in nanoseconds it takes to commit a
  693. * transaction to the disk. [j_state_lock]
  694. */
  695. u64 j_average_commit_time;
  696. /*
  697. * An opaque pointer to fs-private information. ext3 puts its
  698. * superblock pointer here
  699. */
  700. void *j_private;
  701. };
  702. /*
  703. * Journal flag definitions
  704. */
  705. #define JFS_UNMOUNT 0x001 /* Journal thread is being destroyed */
  706. #define JFS_ABORT 0x002 /* Journaling has been aborted for errors. */
  707. #define JFS_ACK_ERR 0x004 /* The errno in the sb has been acked */
  708. #define JFS_FLUSHED 0x008 /* The journal superblock has been flushed */
  709. #define JFS_LOADED 0x010 /* The journal superblock has been loaded */
  710. #define JFS_BARRIER 0x020 /* Use IDE barriers */
  711. #define JFS_ABORT_ON_SYNCDATA_ERR 0x040 /* Abort the journal on file
  712. * data write error in ordered
  713. * mode */
  714. /*
  715. * Function declarations for the journaling transaction and buffer
  716. * management
  717. */
  718. /* Filing buffers */
  719. extern void journal_unfile_buffer(journal_t *, struct journal_head *);
  720. extern void __journal_unfile_buffer(struct journal_head *);
  721. extern void __journal_refile_buffer(struct journal_head *);
  722. extern void journal_refile_buffer(journal_t *, struct journal_head *);
  723. extern void __journal_file_buffer(struct journal_head *, transaction_t *, int);
  724. extern void __journal_free_buffer(struct journal_head *bh);
  725. extern void journal_file_buffer(struct journal_head *, transaction_t *, int);
  726. extern void __journal_clean_data_list(transaction_t *transaction);
  727. /* Log buffer allocation */
  728. extern struct journal_head * journal_get_descriptor_buffer(journal_t *);
  729. int journal_next_log_block(journal_t *, unsigned long *);
  730. /* Commit management */
  731. extern void journal_commit_transaction(journal_t *);
  732. /* Checkpoint list management */
  733. int __journal_clean_checkpoint_list(journal_t *journal);
  734. int __journal_remove_checkpoint(struct journal_head *);
  735. void __journal_insert_checkpoint(struct journal_head *, transaction_t *);
  736. /* Buffer IO */
  737. extern int
  738. journal_write_metadata_buffer(transaction_t *transaction,
  739. struct journal_head *jh_in,
  740. struct journal_head **jh_out,
  741. unsigned long blocknr);
  742. /* Transaction locking */
  743. extern void __wait_on_journal (journal_t *);
  744. /*
  745. * Journal locking.
  746. *
  747. * We need to lock the journal during transaction state changes so that nobody
  748. * ever tries to take a handle on the running transaction while we are in the
  749. * middle of moving it to the commit phase. j_state_lock does this.
  750. *
  751. * Note that the locking is completely interrupt unsafe. We never touch
  752. * journal structures from interrupts.
  753. */
  754. static inline handle_t *journal_current_handle(void)
  755. {
  756. return current->journal_info;
  757. }
  758. /* The journaling code user interface:
  759. *
  760. * Create and destroy handles
  761. * Register buffer modifications against the current transaction.
  762. */
  763. extern handle_t *journal_start(journal_t *, int nblocks);
  764. extern int journal_restart (handle_t *, int nblocks);
  765. extern int journal_extend (handle_t *, int nblocks);
  766. extern int journal_get_write_access(handle_t *, struct buffer_head *);
  767. extern int journal_get_create_access (handle_t *, struct buffer_head *);
  768. extern int journal_get_undo_access(handle_t *, struct buffer_head *);
  769. extern int journal_dirty_data (handle_t *, struct buffer_head *);
  770. extern int journal_dirty_metadata (handle_t *, struct buffer_head *);
  771. extern void journal_release_buffer (handle_t *, struct buffer_head *);
  772. extern int journal_forget (handle_t *, struct buffer_head *);
  773. extern void journal_sync_buffer (struct buffer_head *);
  774. extern void journal_invalidatepage(journal_t *,
  775. struct page *, unsigned long);
  776. extern int journal_try_to_free_buffers(journal_t *, struct page *, gfp_t);
  777. extern int journal_stop(handle_t *);
  778. extern int journal_flush (journal_t *);
  779. extern void journal_lock_updates (journal_t *);
  780. extern void journal_unlock_updates (journal_t *);
  781. extern journal_t * journal_init_dev(struct block_device *bdev,
  782. struct block_device *fs_dev,
  783. int start, int len, int bsize);
  784. extern journal_t * journal_init_inode (struct inode *);
  785. extern int journal_update_format (journal_t *);
  786. extern int journal_check_used_features
  787. (journal_t *, unsigned long, unsigned long, unsigned long);
  788. extern int journal_check_available_features
  789. (journal_t *, unsigned long, unsigned long, unsigned long);
  790. extern int journal_set_features
  791. (journal_t *, unsigned long, unsigned long, unsigned long);
  792. extern int journal_create (journal_t *);
  793. extern int journal_load (journal_t *journal);
  794. extern int journal_destroy (journal_t *);
  795. extern int journal_recover (journal_t *journal);
  796. extern int journal_wipe (journal_t *, int);
  797. extern int journal_skip_recovery (journal_t *);
  798. extern void journal_update_superblock (journal_t *, int);
  799. extern void journal_abort (journal_t *, int);
  800. extern int journal_errno (journal_t *);
  801. extern void journal_ack_err (journal_t *);
  802. extern int journal_clear_err (journal_t *);
  803. extern int journal_bmap(journal_t *, unsigned long, unsigned long *);
  804. extern int journal_force_commit(journal_t *);
  805. /*
  806. * journal_head management
  807. */
  808. struct journal_head *journal_add_journal_head(struct buffer_head *bh);
  809. struct journal_head *journal_grab_journal_head(struct buffer_head *bh);
  810. void journal_remove_journal_head(struct buffer_head *bh);
  811. void journal_put_journal_head(struct journal_head *jh);
  812. /*
  813. * handle management
  814. */
  815. extern struct kmem_cache *jbd_handle_cache;
  816. static inline handle_t *jbd_alloc_handle(gfp_t gfp_flags)
  817. {
  818. return kmem_cache_alloc(jbd_handle_cache, gfp_flags);
  819. }
  820. static inline void jbd_free_handle(handle_t *handle)
  821. {
  822. kmem_cache_free(jbd_handle_cache, handle);
  823. }
  824. /* Primary revoke support */
  825. #define JOURNAL_REVOKE_DEFAULT_HASH 256
  826. extern int journal_init_revoke(journal_t *, int);
  827. extern void journal_destroy_revoke_caches(void);
  828. extern int journal_init_revoke_caches(void);
  829. extern void journal_destroy_revoke(journal_t *);
  830. extern int journal_revoke (handle_t *,
  831. unsigned long, struct buffer_head *);
  832. extern int journal_cancel_revoke(handle_t *, struct journal_head *);
  833. extern void journal_write_revoke_records(journal_t *,
  834. transaction_t *, int);
  835. /* Recovery revoke support */
  836. extern int journal_set_revoke(journal_t *, unsigned long, tid_t);
  837. extern int journal_test_revoke(journal_t *, unsigned long, tid_t);
  838. extern void journal_clear_revoke(journal_t *);
  839. extern void journal_switch_revoke_table(journal_t *journal);
  840. /*
  841. * The log thread user interface:
  842. *
  843. * Request space in the current transaction, and force transaction commit
  844. * transitions on demand.
  845. */
  846. int __log_space_left(journal_t *); /* Called with journal locked */
  847. int log_start_commit(journal_t *journal, tid_t tid);
  848. int __log_start_commit(journal_t *journal, tid_t tid);
  849. int journal_start_commit(journal_t *journal, tid_t *tid);
  850. int journal_force_commit_nested(journal_t *journal);
  851. int log_wait_commit(journal_t *journal, tid_t tid);
  852. int log_do_checkpoint(journal_t *journal);
  853. void __log_wait_for_space(journal_t *journal);
  854. extern void __journal_drop_transaction(journal_t *, transaction_t *);
  855. extern int cleanup_journal_tail(journal_t *);
  856. /* Debugging code only: */
  857. #define jbd_ENOSYS() \
  858. do { \
  859. printk (KERN_ERR "JBD unimplemented function %s\n", __func__); \
  860. current->state = TASK_UNINTERRUPTIBLE; \
  861. schedule(); \
  862. } while (1)
  863. /*
  864. * is_journal_abort
  865. *
  866. * Simple test wrapper function to test the JFS_ABORT state flag. This
  867. * bit, when set, indicates that we have had a fatal error somewhere,
  868. * either inside the journaling layer or indicated to us by the client
  869. * (eg. ext3), and that we and should not commit any further
  870. * transactions.
  871. */
  872. static inline int is_journal_aborted(journal_t *journal)
  873. {
  874. return journal->j_flags & JFS_ABORT;
  875. }
  876. static inline int is_handle_aborted(handle_t *handle)
  877. {
  878. if (handle->h_aborted)
  879. return 1;
  880. return is_journal_aborted(handle->h_transaction->t_journal);
  881. }
  882. static inline void journal_abort_handle(handle_t *handle)
  883. {
  884. handle->h_aborted = 1;
  885. }
  886. #endif /* __KERNEL__ */
  887. /* Comparison functions for transaction IDs: perform comparisons using
  888. * modulo arithmetic so that they work over sequence number wraps. */
  889. static inline int tid_gt(tid_t x, tid_t y)
  890. {
  891. int difference = (x - y);
  892. return (difference > 0);
  893. }
  894. static inline int tid_geq(tid_t x, tid_t y)
  895. {
  896. int difference = (x - y);
  897. return (difference >= 0);
  898. }
  899. extern int journal_blocks_per_page(struct inode *inode);
  900. /*
  901. * Return the minimum number of blocks which must be free in the journal
  902. * before a new transaction may be started. Must be called under j_state_lock.
  903. */
  904. static inline int jbd_space_needed(journal_t *journal)
  905. {
  906. int nblocks = journal->j_max_transaction_buffers;
  907. if (journal->j_committing_transaction)
  908. nblocks += journal->j_committing_transaction->
  909. t_outstanding_credits;
  910. return nblocks;
  911. }
  912. /*
  913. * Definitions which augment the buffer_head layer
  914. */
  915. /* journaling buffer types */
  916. #define BJ_None 0 /* Not journaled */
  917. #define BJ_SyncData 1 /* Normal data: flush before commit */
  918. #define BJ_Metadata 2 /* Normal journaled metadata */
  919. #define BJ_Forget 3 /* Buffer superseded by this transaction */
  920. #define BJ_IO 4 /* Buffer is for temporary IO use */
  921. #define BJ_Shadow 5 /* Buffer contents being shadowed to the log */
  922. #define BJ_LogCtl 6 /* Buffer contains log descriptors */
  923. #define BJ_Reserved 7 /* Buffer is reserved for access by journal */
  924. #define BJ_Locked 8 /* Locked for I/O during commit */
  925. #define BJ_Types 9
  926. extern int jbd_blocks_per_page(struct inode *inode);
  927. #ifdef __KERNEL__
  928. #define buffer_trace_init(bh) do {} while (0)
  929. #define print_buffer_fields(bh) do {} while (0)
  930. #define print_buffer_trace(bh) do {} while (0)
  931. #define BUFFER_TRACE(bh, info) do {} while (0)
  932. #define BUFFER_TRACE2(bh, bh2, info) do {} while (0)
  933. #define JBUFFER_TRACE(jh, info) do {} while (0)
  934. #endif /* __KERNEL__ */
  935. #endif /* _LINUX_JBD_H */