jbd2.h 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * linux/include/linux/jbd2.h
  4. *
  5. * Written by Stephen C. Tweedie <[email protected]>
  6. *
  7. * Copyright 1998-2000 Red Hat, Inc --- All Rights Reserved
  8. *
  9. * Definitions for transaction data structures for the buffer cache
  10. * filesystem journaling support.
  11. */
  12. #ifndef _LINUX_JBD2_H
  13. #define _LINUX_JBD2_H
  14. /* Allow this file to be included directly into e2fsprogs */
  15. #ifndef __KERNEL__
  16. #include "jfs_compat.h"
  17. #define JBD2_DEBUG
  18. #else
  19. #include <linux/types.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/journal-head.h>
  22. #include <linux/stddef.h>
  23. #include <linux/mutex.h>
  24. #include <linux/timer.h>
  25. #include <linux/slab.h>
  26. #include <linux/bit_spinlock.h>
  27. #include <linux/blkdev.h>
  28. #include <crypto/hash.h>
  29. #endif
  30. #define journal_oom_retry 1
  31. /*
  32. * Define JBD2_PARANIOD_IOFAIL to cause a kernel BUG() if ext4 finds
  33. * certain classes of error which can occur due to failed IOs. Under
  34. * normal use we want ext4 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 JBD2_PARANOID_IOFAIL
  39. /*
  40. * The default maximum commit age, in seconds.
  41. */
  42. #define JBD2_DEFAULT_MAX_COMMIT_AGE 5
  43. #ifdef CONFIG_JBD2_DEBUG
  44. /*
  45. * Define JBD2_EXPENSIVE_CHECKING to enable more expensive internal
  46. * consistency checks. By default we don't do this unless
  47. * CONFIG_JBD2_DEBUG is on.
  48. */
  49. #define JBD2_EXPENSIVE_CHECKING
  50. void __jbd2_debug(int level, const char *file, const char *func,
  51. unsigned int line, const char *fmt, ...);
  52. #define jbd2_debug(n, fmt, a...) \
  53. __jbd2_debug((n), __FILE__, __func__, __LINE__, (fmt), ##a)
  54. #else
  55. #define jbd2_debug(n, fmt, a...) no_printk(fmt, ##a)
  56. #endif
  57. extern void *jbd2_alloc(size_t size, gfp_t flags);
  58. extern void jbd2_free(void *ptr, size_t size);
  59. #define JBD2_MIN_JOURNAL_BLOCKS 1024
  60. #define JBD2_DEFAULT_FAST_COMMIT_BLOCKS 256
  61. #ifdef __KERNEL__
  62. /**
  63. * typedef handle_t - The handle_t type represents a single atomic update being performed by some process.
  64. *
  65. * All filesystem modifications made by the process go
  66. * through this handle. Recursive operations (such as quota operations)
  67. * are gathered into a single update.
  68. *
  69. * The buffer credits field is used to account for journaled buffers
  70. * being modified by the running process. To ensure that there is
  71. * enough log space for all outstanding operations, we need to limit the
  72. * number of outstanding buffers possible at any time. When the
  73. * operation completes, any buffer credits not used are credited back to
  74. * the transaction, so that at all times we know how many buffers the
  75. * outstanding updates on a transaction might possibly touch.
  76. *
  77. * This is an opaque datatype.
  78. **/
  79. typedef struct jbd2_journal_handle handle_t; /* Atomic operation type */
  80. /**
  81. * typedef journal_t - The journal_t maintains all of the journaling state information for a single filesystem.
  82. *
  83. * journal_t is linked to from the fs superblock structure.
  84. *
  85. * We use the journal_t to keep track of all outstanding transaction
  86. * activity on the filesystem, and to manage the state of the log
  87. * writing process.
  88. *
  89. * This is an opaque datatype.
  90. **/
  91. typedef struct journal_s journal_t; /* Journal control structure */
  92. #endif
  93. /*
  94. * Internal structures used by the logging mechanism:
  95. */
  96. #define JBD2_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
  97. /*
  98. * On-disk structures
  99. */
  100. /*
  101. * Descriptor block types:
  102. */
  103. #define JBD2_DESCRIPTOR_BLOCK 1
  104. #define JBD2_COMMIT_BLOCK 2
  105. #define JBD2_SUPERBLOCK_V1 3
  106. #define JBD2_SUPERBLOCK_V2 4
  107. #define JBD2_REVOKE_BLOCK 5
  108. /*
  109. * Standard header for all descriptor blocks:
  110. */
  111. typedef struct journal_header_s
  112. {
  113. __be32 h_magic;
  114. __be32 h_blocktype;
  115. __be32 h_sequence;
  116. } journal_header_t;
  117. /*
  118. * Checksum types.
  119. */
  120. #define JBD2_CRC32_CHKSUM 1
  121. #define JBD2_MD5_CHKSUM 2
  122. #define JBD2_SHA1_CHKSUM 3
  123. #define JBD2_CRC32C_CHKSUM 4
  124. #define JBD2_CRC32_CHKSUM_SIZE 4
  125. #define JBD2_CHECKSUM_BYTES (32 / sizeof(u32))
  126. /*
  127. * Commit block header for storing transactional checksums:
  128. *
  129. * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum*
  130. * fields are used to store a checksum of the descriptor and data blocks.
  131. *
  132. * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum
  133. * field is used to store crc32c(uuid+commit_block). Each journal metadata
  134. * block gets its own checksum, and data block checksums are stored in
  135. * journal_block_tag (in the descriptor). The other h_chksum* fields are
  136. * not used.
  137. *
  138. * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses
  139. * journal_block_tag3_t to store a full 32-bit checksum. Everything else
  140. * is the same as v2.
  141. *
  142. * Checksum v1, v2, and v3 are mutually exclusive features.
  143. */
  144. struct commit_header {
  145. __be32 h_magic;
  146. __be32 h_blocktype;
  147. __be32 h_sequence;
  148. unsigned char h_chksum_type;
  149. unsigned char h_chksum_size;
  150. unsigned char h_padding[2];
  151. __be32 h_chksum[JBD2_CHECKSUM_BYTES];
  152. __be64 h_commit_sec;
  153. __be32 h_commit_nsec;
  154. };
  155. /*
  156. * The block tag: used to describe a single buffer in the journal.
  157. * t_blocknr_high is only used if INCOMPAT_64BIT is set, so this
  158. * raw struct shouldn't be used for pointer math or sizeof() - use
  159. * journal_tag_bytes(journal) instead to compute this.
  160. */
  161. typedef struct journal_block_tag3_s
  162. {
  163. __be32 t_blocknr; /* The on-disk block number */
  164. __be32 t_flags; /* See below */
  165. __be32 t_blocknr_high; /* most-significant high 32bits. */
  166. __be32 t_checksum; /* crc32c(uuid+seq+block) */
  167. } journal_block_tag3_t;
  168. typedef struct journal_block_tag_s
  169. {
  170. __be32 t_blocknr; /* The on-disk block number */
  171. __be16 t_checksum; /* truncated crc32c(uuid+seq+block) */
  172. __be16 t_flags; /* See below */
  173. __be32 t_blocknr_high; /* most-significant high 32bits. */
  174. } journal_block_tag_t;
  175. /* Tail of descriptor or revoke block, for checksumming */
  176. struct jbd2_journal_block_tail {
  177. __be32 t_checksum; /* crc32c(uuid+descr_block) */
  178. };
  179. /*
  180. * The revoke descriptor: used on disk to describe a series of blocks to
  181. * be revoked from the log
  182. */
  183. typedef struct jbd2_journal_revoke_header_s
  184. {
  185. journal_header_t r_header;
  186. __be32 r_count; /* Count of bytes used in the block */
  187. } jbd2_journal_revoke_header_t;
  188. /* Definitions for the journal tag flags word: */
  189. #define JBD2_FLAG_ESCAPE 1 /* on-disk block is escaped */
  190. #define JBD2_FLAG_SAME_UUID 2 /* block has same uuid as previous */
  191. #define JBD2_FLAG_DELETED 4 /* block deleted by this transaction */
  192. #define JBD2_FLAG_LAST_TAG 8 /* last tag in this descriptor block */
  193. /*
  194. * The journal superblock. All fields are in big-endian byte order.
  195. */
  196. typedef struct journal_superblock_s
  197. {
  198. /* 0x0000 */
  199. journal_header_t s_header;
  200. /* 0x000C */
  201. /* Static information describing the journal */
  202. __be32 s_blocksize; /* journal device blocksize */
  203. __be32 s_maxlen; /* total blocks in journal file */
  204. __be32 s_first; /* first block of log information */
  205. /* 0x0018 */
  206. /* Dynamic information describing the current state of the log */
  207. __be32 s_sequence; /* first commit ID expected in log */
  208. __be32 s_start; /* blocknr of start of log */
  209. /* 0x0020 */
  210. /* Error value, as set by jbd2_journal_abort(). */
  211. __be32 s_errno;
  212. /* 0x0024 */
  213. /* Remaining fields are only valid in a version-2 superblock */
  214. __be32 s_feature_compat; /* compatible feature set */
  215. __be32 s_feature_incompat; /* incompatible feature set */
  216. __be32 s_feature_ro_compat; /* readonly-compatible feature set */
  217. /* 0x0030 */
  218. __u8 s_uuid[16]; /* 128-bit uuid for journal */
  219. /* 0x0040 */
  220. __be32 s_nr_users; /* Nr of filesystems sharing log */
  221. __be32 s_dynsuper; /* Blocknr of dynamic superblock copy*/
  222. /* 0x0048 */
  223. __be32 s_max_transaction; /* Limit of journal blocks per trans.*/
  224. __be32 s_max_trans_data; /* Limit of data blocks per trans. */
  225. /* 0x0050 */
  226. __u8 s_checksum_type; /* checksum type */
  227. __u8 s_padding2[3];
  228. /* 0x0054 */
  229. __be32 s_num_fc_blks; /* Number of fast commit blocks */
  230. /* 0x0058 */
  231. __u32 s_padding[41];
  232. __be32 s_checksum; /* crc32c(superblock) */
  233. /* 0x0100 */
  234. __u8 s_users[16*48]; /* ids of all fs'es sharing the log */
  235. /* 0x0400 */
  236. } journal_superblock_t;
  237. /* Use the jbd2_{has,set,clear}_feature_* helpers; these will be removed */
  238. #define JBD2_HAS_COMPAT_FEATURE(j,mask) \
  239. ((j)->j_format_version >= 2 && \
  240. ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask))))
  241. #define JBD2_HAS_RO_COMPAT_FEATURE(j,mask) \
  242. ((j)->j_format_version >= 2 && \
  243. ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask))))
  244. #define JBD2_HAS_INCOMPAT_FEATURE(j,mask) \
  245. ((j)->j_format_version >= 2 && \
  246. ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
  247. #define JBD2_FEATURE_COMPAT_CHECKSUM 0x00000001
  248. #define JBD2_FEATURE_INCOMPAT_REVOKE 0x00000001
  249. #define JBD2_FEATURE_INCOMPAT_64BIT 0x00000002
  250. #define JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004
  251. #define JBD2_FEATURE_INCOMPAT_CSUM_V2 0x00000008
  252. #define JBD2_FEATURE_INCOMPAT_CSUM_V3 0x00000010
  253. #define JBD2_FEATURE_INCOMPAT_FAST_COMMIT 0x00000020
  254. /* See "journal feature predicate functions" below */
  255. /* Features known to this kernel version: */
  256. #define JBD2_KNOWN_COMPAT_FEATURES JBD2_FEATURE_COMPAT_CHECKSUM
  257. #define JBD2_KNOWN_ROCOMPAT_FEATURES 0
  258. #define JBD2_KNOWN_INCOMPAT_FEATURES (JBD2_FEATURE_INCOMPAT_REVOKE | \
  259. JBD2_FEATURE_INCOMPAT_64BIT | \
  260. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT | \
  261. JBD2_FEATURE_INCOMPAT_CSUM_V2 | \
  262. JBD2_FEATURE_INCOMPAT_CSUM_V3 | \
  263. JBD2_FEATURE_INCOMPAT_FAST_COMMIT)
  264. #ifdef __KERNEL__
  265. #include <linux/fs.h>
  266. #include <linux/sched.h>
  267. enum jbd_state_bits {
  268. BH_JBD /* Has an attached ext3 journal_head */
  269. = BH_PrivateStart,
  270. BH_JWrite, /* Being written to log (@@@ DEBUGGING) */
  271. BH_Freed, /* Has been freed (truncated) */
  272. BH_Revoked, /* Has been revoked from the log */
  273. BH_RevokeValid, /* Revoked flag is valid */
  274. BH_JBDDirty, /* Is dirty but journaled */
  275. BH_JournalHead, /* Pins bh->b_private and jh->b_bh */
  276. BH_Shadow, /* IO on shadow buffer is running */
  277. BH_Verified, /* Metadata block has been verified ok */
  278. BH_JBDPrivateStart, /* First bit available for private use by FS */
  279. };
  280. BUFFER_FNS(JBD, jbd)
  281. BUFFER_FNS(JWrite, jwrite)
  282. BUFFER_FNS(JBDDirty, jbddirty)
  283. TAS_BUFFER_FNS(JBDDirty, jbddirty)
  284. BUFFER_FNS(Revoked, revoked)
  285. TAS_BUFFER_FNS(Revoked, revoked)
  286. BUFFER_FNS(RevokeValid, revokevalid)
  287. TAS_BUFFER_FNS(RevokeValid, revokevalid)
  288. BUFFER_FNS(Freed, freed)
  289. BUFFER_FNS(Shadow, shadow)
  290. BUFFER_FNS(Verified, verified)
  291. static inline struct buffer_head *jh2bh(struct journal_head *jh)
  292. {
  293. return jh->b_bh;
  294. }
  295. static inline struct journal_head *bh2jh(struct buffer_head *bh)
  296. {
  297. return bh->b_private;
  298. }
  299. static inline void jbd_lock_bh_journal_head(struct buffer_head *bh)
  300. {
  301. bit_spin_lock(BH_JournalHead, &bh->b_state);
  302. }
  303. static inline void jbd_unlock_bh_journal_head(struct buffer_head *bh)
  304. {
  305. bit_spin_unlock(BH_JournalHead, &bh->b_state);
  306. }
  307. #define J_ASSERT(assert) BUG_ON(!(assert))
  308. #define J_ASSERT_BH(bh, expr) J_ASSERT(expr)
  309. #define J_ASSERT_JH(jh, expr) J_ASSERT(expr)
  310. #if defined(JBD2_PARANOID_IOFAIL)
  311. #define J_EXPECT(expr, why...) J_ASSERT(expr)
  312. #define J_EXPECT_BH(bh, expr, why...) J_ASSERT_BH(bh, expr)
  313. #define J_EXPECT_JH(jh, expr, why...) J_ASSERT_JH(jh, expr)
  314. #else
  315. #define __journal_expect(expr, why...) \
  316. ({ \
  317. int val = (expr); \
  318. if (!val) { \
  319. printk(KERN_ERR \
  320. "JBD2 unexpected failure: %s: %s;\n", \
  321. __func__, #expr); \
  322. printk(KERN_ERR why "\n"); \
  323. } \
  324. val; \
  325. })
  326. #define J_EXPECT(expr, why...) __journal_expect(expr, ## why)
  327. #define J_EXPECT_BH(bh, expr, why...) __journal_expect(expr, ## why)
  328. #define J_EXPECT_JH(jh, expr, why...) __journal_expect(expr, ## why)
  329. #endif
  330. /* Flags in jbd_inode->i_flags */
  331. #define __JI_COMMIT_RUNNING 0
  332. #define __JI_WRITE_DATA 1
  333. #define __JI_WAIT_DATA 2
  334. /*
  335. * Commit of the inode data in progress. We use this flag to protect us from
  336. * concurrent deletion of inode. We cannot use reference to inode for this
  337. * since we cannot afford doing last iput() on behalf of kjournald
  338. */
  339. #define JI_COMMIT_RUNNING (1 << __JI_COMMIT_RUNNING)
  340. /* Write allocated dirty buffers in this inode before commit */
  341. #define JI_WRITE_DATA (1 << __JI_WRITE_DATA)
  342. /* Wait for outstanding data writes for this inode before commit */
  343. #define JI_WAIT_DATA (1 << __JI_WAIT_DATA)
  344. /**
  345. * struct jbd2_inode - The jbd_inode type is the structure linking inodes in
  346. * ordered mode present in a transaction so that we can sync them during commit.
  347. */
  348. struct jbd2_inode {
  349. /**
  350. * @i_transaction:
  351. *
  352. * Which transaction does this inode belong to? Either the running
  353. * transaction or the committing one. [j_list_lock]
  354. */
  355. transaction_t *i_transaction;
  356. /**
  357. * @i_next_transaction:
  358. *
  359. * Pointer to the running transaction modifying inode's data in case
  360. * there is already a committing transaction touching it. [j_list_lock]
  361. */
  362. transaction_t *i_next_transaction;
  363. /**
  364. * @i_list: List of inodes in the i_transaction [j_list_lock]
  365. */
  366. struct list_head i_list;
  367. /**
  368. * @i_vfs_inode:
  369. *
  370. * VFS inode this inode belongs to [constant for lifetime of structure]
  371. */
  372. struct inode *i_vfs_inode;
  373. /**
  374. * @i_flags: Flags of inode [j_list_lock]
  375. */
  376. unsigned long i_flags;
  377. /**
  378. * @i_dirty_start:
  379. *
  380. * Offset in bytes where the dirty range for this inode starts.
  381. * [j_list_lock]
  382. */
  383. loff_t i_dirty_start;
  384. /**
  385. * @i_dirty_end:
  386. *
  387. * Inclusive offset in bytes where the dirty range for this inode
  388. * ends. [j_list_lock]
  389. */
  390. loff_t i_dirty_end;
  391. };
  392. struct jbd2_revoke_table_s;
  393. /**
  394. * struct jbd2_journal_handle - The jbd2_journal_handle type is the concrete
  395. * type associated with handle_t.
  396. * @h_transaction: Which compound transaction is this update a part of?
  397. * @h_journal: Which journal handle belongs to - used iff h_reserved set.
  398. * @h_rsv_handle: Handle reserved for finishing the logical operation.
  399. * @h_total_credits: Number of remaining buffers we are allowed to add to
  400. * journal. These are dirty buffers and revoke descriptor blocks.
  401. * @h_revoke_credits: Number of remaining revoke records available for handle
  402. * @h_ref: Reference count on this handle.
  403. * @h_err: Field for caller's use to track errors through large fs operations.
  404. * @h_sync: Flag for sync-on-close.
  405. * @h_jdata: Flag to force data journaling.
  406. * @h_reserved: Flag for handle for reserved credits.
  407. * @h_aborted: Flag indicating fatal error on handle.
  408. * @h_type: For handle statistics.
  409. * @h_line_no: For handle statistics.
  410. * @h_start_jiffies: Handle Start time.
  411. * @h_requested_credits: Holds @h_total_credits after handle is started.
  412. * @h_revoke_credits_requested: Holds @h_revoke_credits after handle is started.
  413. * @saved_alloc_context: Saved context while transaction is open.
  414. **/
  415. /* Docbook can't yet cope with the bit fields, but will leave the documentation
  416. * in so it can be fixed later.
  417. */
  418. struct jbd2_journal_handle
  419. {
  420. union {
  421. transaction_t *h_transaction;
  422. /* Which journal handle belongs to - used iff h_reserved set */
  423. journal_t *h_journal;
  424. };
  425. handle_t *h_rsv_handle;
  426. int h_total_credits;
  427. int h_revoke_credits;
  428. int h_revoke_credits_requested;
  429. int h_ref;
  430. int h_err;
  431. /* Flags [no locking] */
  432. unsigned int h_sync: 1;
  433. unsigned int h_jdata: 1;
  434. unsigned int h_reserved: 1;
  435. unsigned int h_aborted: 1;
  436. unsigned int h_type: 8;
  437. unsigned int h_line_no: 16;
  438. unsigned long h_start_jiffies;
  439. unsigned int h_requested_credits;
  440. unsigned int saved_alloc_context;
  441. };
  442. /*
  443. * Some stats for checkpoint phase
  444. */
  445. struct transaction_chp_stats_s {
  446. unsigned long cs_chp_time;
  447. __u32 cs_forced_to_close;
  448. __u32 cs_written;
  449. __u32 cs_dropped;
  450. };
  451. /* The transaction_t type is the guts of the journaling mechanism. It
  452. * tracks a compound transaction through its various states:
  453. *
  454. * RUNNING: accepting new updates
  455. * LOCKED: Updates still running but we don't accept new ones
  456. * RUNDOWN: Updates are tidying up but have finished requesting
  457. * new buffers to modify (state not used for now)
  458. * FLUSH: All updates complete, but we are still writing to disk
  459. * COMMIT: All data on disk, writing commit record
  460. * FINISHED: We still have to keep the transaction for checkpointing.
  461. *
  462. * The transaction keeps track of all of the buffers modified by a
  463. * running transaction, and all of the buffers committed but not yet
  464. * flushed to home for finished transactions.
  465. * (Locking Documentation improved by LockDoc)
  466. */
  467. /*
  468. * Lock ranking:
  469. *
  470. * j_list_lock
  471. * ->jbd_lock_bh_journal_head() (This is "innermost")
  472. *
  473. * j_state_lock
  474. * ->b_state_lock
  475. *
  476. * b_state_lock
  477. * ->j_list_lock
  478. *
  479. * j_state_lock
  480. * ->j_list_lock (journal_unmap_buffer)
  481. *
  482. */
  483. struct transaction_s
  484. {
  485. /* Pointer to the journal for this transaction. [no locking] */
  486. journal_t *t_journal;
  487. /* Sequence number for this transaction [no locking] */
  488. tid_t t_tid;
  489. /*
  490. * Transaction's current state
  491. * [no locking - only kjournald2 alters this]
  492. * [j_list_lock] guards transition of a transaction into T_FINISHED
  493. * state and subsequent call of __jbd2_journal_drop_transaction()
  494. * FIXME: needs barriers
  495. * KLUDGE: [use j_state_lock]
  496. */
  497. enum {
  498. T_RUNNING,
  499. T_LOCKED,
  500. T_SWITCH,
  501. T_FLUSH,
  502. T_COMMIT,
  503. T_COMMIT_DFLUSH,
  504. T_COMMIT_JFLUSH,
  505. T_COMMIT_CALLBACK,
  506. T_FINISHED
  507. } t_state;
  508. /*
  509. * Where in the log does this transaction's commit start? [no locking]
  510. */
  511. unsigned long t_log_start;
  512. /*
  513. * Number of buffers on the t_buffers list [j_list_lock, no locks
  514. * needed for jbd2 thread]
  515. */
  516. int t_nr_buffers;
  517. /*
  518. * Doubly-linked circular list of all buffers reserved but not yet
  519. * modified by this transaction [j_list_lock, no locks needed fo
  520. * jbd2 thread]
  521. */
  522. struct journal_head *t_reserved_list;
  523. /*
  524. * Doubly-linked circular list of all metadata buffers owned by this
  525. * transaction [j_list_lock, no locks needed for jbd2 thread]
  526. */
  527. struct journal_head *t_buffers;
  528. /*
  529. * Doubly-linked circular list of all forget buffers (superseded
  530. * buffers which we can un-checkpoint once this transaction commits)
  531. * [j_list_lock]
  532. */
  533. struct journal_head *t_forget;
  534. /*
  535. * Doubly-linked circular list of all buffers still to be flushed before
  536. * this transaction can be checkpointed. [j_list_lock]
  537. */
  538. struct journal_head *t_checkpoint_list;
  539. /*
  540. * Doubly-linked circular list of metadata buffers being
  541. * shadowed by log IO. The IO buffers on the iobuf list and
  542. * the shadow buffers on this list match each other one for
  543. * one at all times. [j_list_lock, no locks needed for jbd2
  544. * thread]
  545. */
  546. struct journal_head *t_shadow_list;
  547. /*
  548. * List of inodes associated with the transaction; e.g., ext4 uses
  549. * this to track inodes in data=ordered and data=journal mode that
  550. * need special handling on transaction commit; also used by ocfs2.
  551. * [j_list_lock]
  552. */
  553. struct list_head t_inode_list;
  554. /*
  555. * Protects info related to handles
  556. */
  557. spinlock_t t_handle_lock;
  558. /*
  559. * Longest time some handle had to wait for running transaction
  560. */
  561. unsigned long t_max_wait;
  562. /*
  563. * When transaction started
  564. */
  565. unsigned long t_start;
  566. /*
  567. * When commit was requested [j_state_lock]
  568. */
  569. unsigned long t_requested;
  570. /*
  571. * Checkpointing stats [j_list_lock]
  572. */
  573. struct transaction_chp_stats_s t_chp_stats;
  574. /*
  575. * Number of outstanding updates running on this transaction
  576. * [none]
  577. */
  578. atomic_t t_updates;
  579. /*
  580. * Number of blocks reserved for this transaction in the journal.
  581. * This is including all credits reserved when starting transaction
  582. * handles as well as all journal descriptor blocks needed for this
  583. * transaction. [none]
  584. */
  585. atomic_t t_outstanding_credits;
  586. /*
  587. * Number of revoke records for this transaction added by already
  588. * stopped handles. [none]
  589. */
  590. atomic_t t_outstanding_revokes;
  591. /*
  592. * How many handles used this transaction? [none]
  593. */
  594. atomic_t t_handle_count;
  595. /*
  596. * Forward and backward links for the circular list of all transactions
  597. * awaiting checkpoint. [j_list_lock]
  598. */
  599. transaction_t *t_cpnext, *t_cpprev;
  600. /*
  601. * When will the transaction expire (become due for commit), in jiffies?
  602. * [no locking]
  603. */
  604. unsigned long t_expires;
  605. /*
  606. * When this transaction started, in nanoseconds [no locking]
  607. */
  608. ktime_t t_start_time;
  609. /*
  610. * This transaction is being forced and some process is
  611. * waiting for it to finish.
  612. */
  613. unsigned int t_synchronous_commit:1;
  614. /* Disk flush needs to be sent to fs partition [no locking] */
  615. int t_need_data_flush;
  616. /*
  617. * For use by the filesystem to store fs-specific data
  618. * structures associated with the transaction
  619. */
  620. struct list_head t_private_list;
  621. };
  622. struct transaction_run_stats_s {
  623. unsigned long rs_wait;
  624. unsigned long rs_request_delay;
  625. unsigned long rs_running;
  626. unsigned long rs_locked;
  627. unsigned long rs_flushing;
  628. unsigned long rs_logging;
  629. __u32 rs_handle_count;
  630. __u32 rs_blocks;
  631. __u32 rs_blocks_logged;
  632. };
  633. struct transaction_stats_s {
  634. unsigned long ts_tid;
  635. unsigned long ts_requested;
  636. struct transaction_run_stats_s run;
  637. };
  638. static inline unsigned long
  639. jbd2_time_diff(unsigned long start, unsigned long end)
  640. {
  641. if (end >= start)
  642. return end - start;
  643. return end + (MAX_JIFFY_OFFSET - start);
  644. }
  645. #define JBD2_NR_BATCH 64
  646. enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
  647. #define JBD2_FC_REPLAY_STOP 0
  648. #define JBD2_FC_REPLAY_CONTINUE 1
  649. /**
  650. * struct journal_s - The journal_s type is the concrete type associated with
  651. * journal_t.
  652. */
  653. struct journal_s
  654. {
  655. /**
  656. * @j_flags: General journaling state flags [j_state_lock,
  657. * no lock for quick racy checks]
  658. */
  659. unsigned long j_flags;
  660. /**
  661. * @j_atomic_flags: Atomic journaling state flags.
  662. */
  663. unsigned long j_atomic_flags;
  664. /**
  665. * @j_errno:
  666. *
  667. * Is there an outstanding uncleared error on the journal (from a prior
  668. * abort)? [j_state_lock]
  669. */
  670. int j_errno;
  671. /**
  672. * @j_abort_mutex: Lock the whole aborting procedure.
  673. */
  674. struct mutex j_abort_mutex;
  675. /**
  676. * @j_sb_buffer: The first part of the superblock buffer.
  677. */
  678. struct buffer_head *j_sb_buffer;
  679. /**
  680. * @j_superblock: The second part of the superblock buffer.
  681. */
  682. journal_superblock_t *j_superblock;
  683. /**
  684. * @j_format_version: Version of the superblock format.
  685. */
  686. int j_format_version;
  687. /**
  688. * @j_state_lock: Protect the various scalars in the journal.
  689. */
  690. rwlock_t j_state_lock;
  691. /**
  692. * @j_barrier_count:
  693. *
  694. * Number of processes waiting to create a barrier lock [j_state_lock,
  695. * no lock for quick racy checks]
  696. */
  697. int j_barrier_count;
  698. /**
  699. * @j_barrier: The barrier lock itself.
  700. */
  701. struct mutex j_barrier;
  702. /**
  703. * @j_running_transaction:
  704. *
  705. * Transactions: The current running transaction...
  706. * [j_state_lock, no lock for quick racy checks] [caller holding
  707. * open handle]
  708. */
  709. transaction_t *j_running_transaction;
  710. /**
  711. * @j_committing_transaction:
  712. *
  713. * the transaction we are pushing to disk
  714. * [j_state_lock] [caller holding open handle]
  715. */
  716. transaction_t *j_committing_transaction;
  717. /**
  718. * @j_checkpoint_transactions:
  719. *
  720. * ... and a linked circular list of all transactions waiting for
  721. * checkpointing. [j_list_lock]
  722. */
  723. transaction_t *j_checkpoint_transactions;
  724. /**
  725. * @j_wait_transaction_locked:
  726. *
  727. * Wait queue for waiting for a locked transaction to start committing,
  728. * or for a barrier lock to be released.
  729. */
  730. wait_queue_head_t j_wait_transaction_locked;
  731. /**
  732. * @j_wait_done_commit: Wait queue for waiting for commit to complete.
  733. */
  734. wait_queue_head_t j_wait_done_commit;
  735. /**
  736. * @j_wait_commit: Wait queue to trigger commit.
  737. */
  738. wait_queue_head_t j_wait_commit;
  739. /**
  740. * @j_wait_updates: Wait queue to wait for updates to complete.
  741. */
  742. wait_queue_head_t j_wait_updates;
  743. /**
  744. * @j_wait_reserved:
  745. *
  746. * Wait queue to wait for reserved buffer credits to drop.
  747. */
  748. wait_queue_head_t j_wait_reserved;
  749. /**
  750. * @j_fc_wait:
  751. *
  752. * Wait queue to wait for completion of async fast commits.
  753. */
  754. wait_queue_head_t j_fc_wait;
  755. /**
  756. * @j_checkpoint_mutex:
  757. *
  758. * Semaphore for locking against concurrent checkpoints.
  759. */
  760. struct mutex j_checkpoint_mutex;
  761. /**
  762. * @j_chkpt_bhs:
  763. *
  764. * List of buffer heads used by the checkpoint routine. This
  765. * was moved from jbd2_log_do_checkpoint() to reduce stack
  766. * usage. Access to this array is controlled by the
  767. * @j_checkpoint_mutex. [j_checkpoint_mutex]
  768. */
  769. struct buffer_head *j_chkpt_bhs[JBD2_NR_BATCH];
  770. /**
  771. * @j_shrinker:
  772. *
  773. * Journal head shrinker, reclaim buffer's journal head which
  774. * has been written back.
  775. */
  776. struct shrinker j_shrinker;
  777. /**
  778. * @j_checkpoint_jh_count:
  779. *
  780. * Number of journal buffers on the checkpoint list. [j_list_lock]
  781. */
  782. struct percpu_counter j_checkpoint_jh_count;
  783. /**
  784. * @j_shrink_transaction:
  785. *
  786. * Record next transaction will shrink on the checkpoint list.
  787. * [j_list_lock]
  788. */
  789. transaction_t *j_shrink_transaction;
  790. /**
  791. * @j_head:
  792. *
  793. * Journal head: identifies the first unused block in the journal.
  794. * [j_state_lock]
  795. */
  796. unsigned long j_head;
  797. /**
  798. * @j_tail:
  799. *
  800. * Journal tail: identifies the oldest still-used block in the journal.
  801. * [j_state_lock]
  802. */
  803. unsigned long j_tail;
  804. /**
  805. * @j_free:
  806. *
  807. * Journal free: how many free blocks are there in the journal?
  808. * [j_state_lock]
  809. */
  810. unsigned long j_free;
  811. /**
  812. * @j_first:
  813. *
  814. * The block number of the first usable block in the journal
  815. * [j_state_lock].
  816. */
  817. unsigned long j_first;
  818. /**
  819. * @j_last:
  820. *
  821. * The block number one beyond the last usable block in the journal
  822. * [j_state_lock].
  823. */
  824. unsigned long j_last;
  825. /**
  826. * @j_fc_first:
  827. *
  828. * The block number of the first fast commit block in the journal
  829. * [j_state_lock].
  830. */
  831. unsigned long j_fc_first;
  832. /**
  833. * @j_fc_off:
  834. *
  835. * Number of fast commit blocks currently allocated. Accessed only
  836. * during fast commit. Currently only process can do fast commit, so
  837. * this field is not protected by any lock.
  838. */
  839. unsigned long j_fc_off;
  840. /**
  841. * @j_fc_last:
  842. *
  843. * The block number one beyond the last fast commit block in the journal
  844. * [j_state_lock].
  845. */
  846. unsigned long j_fc_last;
  847. /**
  848. * @j_dev: Device where we store the journal.
  849. */
  850. struct block_device *j_dev;
  851. /**
  852. * @j_blocksize: Block size for the location where we store the journal.
  853. */
  854. int j_blocksize;
  855. /**
  856. * @j_blk_offset:
  857. *
  858. * Starting block offset into the device where we store the journal.
  859. */
  860. unsigned long long j_blk_offset;
  861. /**
  862. * @j_devname: Journal device name.
  863. */
  864. char j_devname[BDEVNAME_SIZE+24];
  865. /**
  866. * @j_fs_dev:
  867. *
  868. * Device which holds the client fs. For internal journal this will be
  869. * equal to j_dev.
  870. */
  871. struct block_device *j_fs_dev;
  872. /**
  873. * @j_total_len: Total maximum capacity of the journal region on disk.
  874. */
  875. unsigned int j_total_len;
  876. /**
  877. * @j_reserved_credits:
  878. *
  879. * Number of buffers reserved from the running transaction.
  880. */
  881. atomic_t j_reserved_credits;
  882. /**
  883. * @j_list_lock: Protects the buffer lists and internal buffer state.
  884. */
  885. spinlock_t j_list_lock;
  886. /**
  887. * @j_inode:
  888. *
  889. * Optional inode where we store the journal. If present, all
  890. * journal block numbers are mapped into this inode via bmap().
  891. */
  892. struct inode *j_inode;
  893. /**
  894. * @j_tail_sequence:
  895. *
  896. * Sequence number of the oldest transaction in the log [j_state_lock]
  897. */
  898. tid_t j_tail_sequence;
  899. /**
  900. * @j_transaction_sequence:
  901. *
  902. * Sequence number of the next transaction to grant [j_state_lock]
  903. */
  904. tid_t j_transaction_sequence;
  905. /**
  906. * @j_commit_sequence:
  907. *
  908. * Sequence number of the most recently committed transaction
  909. * [j_state_lock, no lock for quick racy checks]
  910. */
  911. tid_t j_commit_sequence;
  912. /**
  913. * @j_commit_request:
  914. *
  915. * Sequence number of the most recent transaction wanting commit
  916. * [j_state_lock, no lock for quick racy checks]
  917. */
  918. tid_t j_commit_request;
  919. /**
  920. * @j_uuid:
  921. *
  922. * Journal uuid: identifies the object (filesystem, LVM volume etc)
  923. * backed by this journal. This will eventually be replaced by an array
  924. * of uuids, allowing us to index multiple devices within a single
  925. * journal and to perform atomic updates across them.
  926. */
  927. __u8 j_uuid[16];
  928. /**
  929. * @j_task: Pointer to the current commit thread for this journal.
  930. */
  931. struct task_struct *j_task;
  932. /**
  933. * @j_max_transaction_buffers:
  934. *
  935. * Maximum number of metadata buffers to allow in a single compound
  936. * commit transaction.
  937. */
  938. int j_max_transaction_buffers;
  939. /**
  940. * @j_revoke_records_per_block:
  941. *
  942. * Number of revoke records that fit in one descriptor block.
  943. */
  944. int j_revoke_records_per_block;
  945. /**
  946. * @j_commit_interval:
  947. *
  948. * What is the maximum transaction lifetime before we begin a commit?
  949. */
  950. unsigned long j_commit_interval;
  951. /**
  952. * @j_commit_timer: The timer used to wakeup the commit thread.
  953. */
  954. struct timer_list j_commit_timer;
  955. /**
  956. * @j_revoke_lock: Protect the revoke table.
  957. */
  958. spinlock_t j_revoke_lock;
  959. /**
  960. * @j_revoke:
  961. *
  962. * The revoke table - maintains the list of revoked blocks in the
  963. * current transaction.
  964. */
  965. struct jbd2_revoke_table_s *j_revoke;
  966. /**
  967. * @j_revoke_table: Alternate revoke tables for j_revoke.
  968. */
  969. struct jbd2_revoke_table_s *j_revoke_table[2];
  970. /**
  971. * @j_wbuf: Array of bhs for jbd2_journal_commit_transaction.
  972. */
  973. struct buffer_head **j_wbuf;
  974. /**
  975. * @j_fc_wbuf: Array of fast commit bhs for fast commit. Accessed only
  976. * during a fast commit. Currently only process can do fast commit, so
  977. * this field is not protected by any lock.
  978. */
  979. struct buffer_head **j_fc_wbuf;
  980. /**
  981. * @j_wbufsize:
  982. *
  983. * Size of @j_wbuf array.
  984. */
  985. int j_wbufsize;
  986. /**
  987. * @j_fc_wbufsize:
  988. *
  989. * Size of @j_fc_wbuf array.
  990. */
  991. int j_fc_wbufsize;
  992. /**
  993. * @j_last_sync_writer:
  994. *
  995. * The pid of the last person to run a synchronous operation
  996. * through the journal.
  997. */
  998. pid_t j_last_sync_writer;
  999. /**
  1000. * @j_average_commit_time:
  1001. *
  1002. * The average amount of time in nanoseconds it takes to commit a
  1003. * transaction to disk. [j_state_lock]
  1004. */
  1005. u64 j_average_commit_time;
  1006. /**
  1007. * @j_min_batch_time:
  1008. *
  1009. * Minimum time that we should wait for additional filesystem operations
  1010. * to get batched into a synchronous handle in microseconds.
  1011. */
  1012. u32 j_min_batch_time;
  1013. /**
  1014. * @j_max_batch_time:
  1015. *
  1016. * Maximum time that we should wait for additional filesystem operations
  1017. * to get batched into a synchronous handle in microseconds.
  1018. */
  1019. u32 j_max_batch_time;
  1020. /**
  1021. * @j_commit_callback:
  1022. *
  1023. * This function is called when a transaction is closed.
  1024. */
  1025. void (*j_commit_callback)(journal_t *,
  1026. transaction_t *);
  1027. /**
  1028. * @j_submit_inode_data_buffers:
  1029. *
  1030. * This function is called for all inodes associated with the
  1031. * committing transaction marked with JI_WRITE_DATA flag
  1032. * before we start to write out the transaction to the journal.
  1033. */
  1034. int (*j_submit_inode_data_buffers)
  1035. (struct jbd2_inode *);
  1036. /**
  1037. * @j_finish_inode_data_buffers:
  1038. *
  1039. * This function is called for all inodes associated with the
  1040. * committing transaction marked with JI_WAIT_DATA flag
  1041. * after we have written the transaction to the journal
  1042. * but before we write out the commit block.
  1043. */
  1044. int (*j_finish_inode_data_buffers)
  1045. (struct jbd2_inode *);
  1046. /*
  1047. * Journal statistics
  1048. */
  1049. /**
  1050. * @j_history_lock: Protect the transactions statistics history.
  1051. */
  1052. spinlock_t j_history_lock;
  1053. /**
  1054. * @j_proc_entry: procfs entry for the jbd statistics directory.
  1055. */
  1056. struct proc_dir_entry *j_proc_entry;
  1057. /**
  1058. * @j_stats: Overall statistics.
  1059. */
  1060. struct transaction_stats_s j_stats;
  1061. /**
  1062. * @j_failed_commit: Failed journal commit ID.
  1063. */
  1064. unsigned int j_failed_commit;
  1065. /**
  1066. * @j_private:
  1067. *
  1068. * An opaque pointer to fs-private information. ext3 puts its
  1069. * superblock pointer here.
  1070. */
  1071. void *j_private;
  1072. /**
  1073. * @j_chksum_driver:
  1074. *
  1075. * Reference to checksum algorithm driver via cryptoapi.
  1076. */
  1077. struct crypto_shash *j_chksum_driver;
  1078. /**
  1079. * @j_csum_seed:
  1080. *
  1081. * Precomputed journal UUID checksum for seeding other checksums.
  1082. */
  1083. __u32 j_csum_seed;
  1084. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1085. /**
  1086. * @j_trans_commit_map:
  1087. *
  1088. * Lockdep entity to track transaction commit dependencies. Handles
  1089. * hold this "lock" for read, when we wait for commit, we acquire the
  1090. * "lock" for writing. This matches the properties of jbd2 journalling
  1091. * where the running transaction has to wait for all handles to be
  1092. * dropped to commit that transaction and also acquiring a handle may
  1093. * require transaction commit to finish.
  1094. */
  1095. struct lockdep_map j_trans_commit_map;
  1096. #endif
  1097. /**
  1098. * @j_fc_cleanup_callback:
  1099. *
  1100. * Clean-up after fast commit or full commit. JBD2 calls this function
  1101. * after every commit operation.
  1102. */
  1103. void (*j_fc_cleanup_callback)(struct journal_s *journal, int full, tid_t tid);
  1104. /**
  1105. * @j_fc_replay_callback:
  1106. *
  1107. * File-system specific function that performs replay of a fast
  1108. * commit. JBD2 calls this function for each fast commit block found in
  1109. * the journal. This function should return JBD2_FC_REPLAY_CONTINUE
  1110. * to indicate that the block was processed correctly and more fast
  1111. * commit replay should continue. Return value of JBD2_FC_REPLAY_STOP
  1112. * indicates the end of replay (no more blocks remaining). A negative
  1113. * return value indicates error.
  1114. */
  1115. int (*j_fc_replay_callback)(struct journal_s *journal,
  1116. struct buffer_head *bh,
  1117. enum passtype pass, int off,
  1118. tid_t expected_commit_id);
  1119. };
  1120. #define jbd2_might_wait_for_commit(j) \
  1121. do { \
  1122. rwsem_acquire(&j->j_trans_commit_map, 0, 0, _THIS_IP_); \
  1123. rwsem_release(&j->j_trans_commit_map, _THIS_IP_); \
  1124. } while (0)
  1125. /* journal feature predicate functions */
  1126. #define JBD2_FEATURE_COMPAT_FUNCS(name, flagname) \
  1127. static inline bool jbd2_has_feature_##name(journal_t *j) \
  1128. { \
  1129. return ((j)->j_format_version >= 2 && \
  1130. ((j)->j_superblock->s_feature_compat & \
  1131. cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname)) != 0); \
  1132. } \
  1133. static inline void jbd2_set_feature_##name(journal_t *j) \
  1134. { \
  1135. (j)->j_superblock->s_feature_compat |= \
  1136. cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname); \
  1137. } \
  1138. static inline void jbd2_clear_feature_##name(journal_t *j) \
  1139. { \
  1140. (j)->j_superblock->s_feature_compat &= \
  1141. ~cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname); \
  1142. }
  1143. #define JBD2_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
  1144. static inline bool jbd2_has_feature_##name(journal_t *j) \
  1145. { \
  1146. return ((j)->j_format_version >= 2 && \
  1147. ((j)->j_superblock->s_feature_ro_compat & \
  1148. cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname)) != 0); \
  1149. } \
  1150. static inline void jbd2_set_feature_##name(journal_t *j) \
  1151. { \
  1152. (j)->j_superblock->s_feature_ro_compat |= \
  1153. cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname); \
  1154. } \
  1155. static inline void jbd2_clear_feature_##name(journal_t *j) \
  1156. { \
  1157. (j)->j_superblock->s_feature_ro_compat &= \
  1158. ~cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname); \
  1159. }
  1160. #define JBD2_FEATURE_INCOMPAT_FUNCS(name, flagname) \
  1161. static inline bool jbd2_has_feature_##name(journal_t *j) \
  1162. { \
  1163. return ((j)->j_format_version >= 2 && \
  1164. ((j)->j_superblock->s_feature_incompat & \
  1165. cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname)) != 0); \
  1166. } \
  1167. static inline void jbd2_set_feature_##name(journal_t *j) \
  1168. { \
  1169. (j)->j_superblock->s_feature_incompat |= \
  1170. cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname); \
  1171. } \
  1172. static inline void jbd2_clear_feature_##name(journal_t *j) \
  1173. { \
  1174. (j)->j_superblock->s_feature_incompat &= \
  1175. ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname); \
  1176. }
  1177. JBD2_FEATURE_COMPAT_FUNCS(checksum, CHECKSUM)
  1178. JBD2_FEATURE_INCOMPAT_FUNCS(revoke, REVOKE)
  1179. JBD2_FEATURE_INCOMPAT_FUNCS(64bit, 64BIT)
  1180. JBD2_FEATURE_INCOMPAT_FUNCS(async_commit, ASYNC_COMMIT)
  1181. JBD2_FEATURE_INCOMPAT_FUNCS(csum2, CSUM_V2)
  1182. JBD2_FEATURE_INCOMPAT_FUNCS(csum3, CSUM_V3)
  1183. JBD2_FEATURE_INCOMPAT_FUNCS(fast_commit, FAST_COMMIT)
  1184. /*
  1185. * Journal flag definitions
  1186. */
  1187. #define JBD2_UNMOUNT 0x001 /* Journal thread is being destroyed */
  1188. #define JBD2_ABORT 0x002 /* Journaling has been aborted for errors. */
  1189. #define JBD2_ACK_ERR 0x004 /* The errno in the sb has been acked */
  1190. #define JBD2_FLUSHED 0x008 /* The journal superblock has been flushed */
  1191. #define JBD2_LOADED 0x010 /* The journal superblock has been loaded */
  1192. #define JBD2_BARRIER 0x020 /* Use IDE barriers */
  1193. #define JBD2_ABORT_ON_SYNCDATA_ERR 0x040 /* Abort the journal on file
  1194. * data write error in ordered
  1195. * mode */
  1196. #define JBD2_FAST_COMMIT_ONGOING 0x100 /* Fast commit is ongoing */
  1197. #define JBD2_FULL_COMMIT_ONGOING 0x200 /* Full commit is ongoing */
  1198. #define JBD2_JOURNAL_FLUSH_DISCARD 0x0001
  1199. #define JBD2_JOURNAL_FLUSH_ZEROOUT 0x0002
  1200. #define JBD2_JOURNAL_FLUSH_VALID (JBD2_JOURNAL_FLUSH_DISCARD | \
  1201. JBD2_JOURNAL_FLUSH_ZEROOUT)
  1202. /*
  1203. * Journal atomic flag definitions
  1204. */
  1205. #define JBD2_CHECKPOINT_IO_ERROR 0x001 /* Detect io error while writing
  1206. * buffer back to disk */
  1207. /*
  1208. * Function declarations for the journaling transaction and buffer
  1209. * management
  1210. */
  1211. /* Filing buffers */
  1212. extern void jbd2_journal_unfile_buffer(journal_t *, struct journal_head *);
  1213. extern bool __jbd2_journal_refile_buffer(struct journal_head *);
  1214. extern void jbd2_journal_refile_buffer(journal_t *, struct journal_head *);
  1215. extern void __jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
  1216. extern void jbd2_journal_file_buffer(struct journal_head *, transaction_t *, int);
  1217. static inline void jbd2_file_log_bh(struct list_head *head, struct buffer_head *bh)
  1218. {
  1219. list_add_tail(&bh->b_assoc_buffers, head);
  1220. }
  1221. static inline void jbd2_unfile_log_bh(struct buffer_head *bh)
  1222. {
  1223. list_del_init(&bh->b_assoc_buffers);
  1224. }
  1225. /* Log buffer allocation */
  1226. struct buffer_head *jbd2_journal_get_descriptor_buffer(transaction_t *, int);
  1227. void jbd2_descriptor_block_csum_set(journal_t *, struct buffer_head *);
  1228. int jbd2_journal_next_log_block(journal_t *, unsigned long long *);
  1229. int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
  1230. unsigned long *block);
  1231. int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block);
  1232. void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block);
  1233. /* Commit management */
  1234. extern void jbd2_journal_commit_transaction(journal_t *);
  1235. /* Checkpoint list management */
  1236. void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy);
  1237. unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal, unsigned long *nr_to_scan);
  1238. int __jbd2_journal_remove_checkpoint(struct journal_head *);
  1239. int jbd2_journal_try_remove_checkpoint(struct journal_head *jh);
  1240. void jbd2_journal_destroy_checkpoint(journal_t *journal);
  1241. void __jbd2_journal_insert_checkpoint(struct journal_head *, transaction_t *);
  1242. /*
  1243. * Triggers
  1244. */
  1245. struct jbd2_buffer_trigger_type {
  1246. /*
  1247. * Fired a the moment data to write to the journal are known to be
  1248. * stable - so either at the moment b_frozen_data is created or just
  1249. * before a buffer is written to the journal. mapped_data is a mapped
  1250. * buffer that is the frozen data for commit.
  1251. */
  1252. void (*t_frozen)(struct jbd2_buffer_trigger_type *type,
  1253. struct buffer_head *bh, void *mapped_data,
  1254. size_t size);
  1255. /*
  1256. * Fired during journal abort for dirty buffers that will not be
  1257. * committed.
  1258. */
  1259. void (*t_abort)(struct jbd2_buffer_trigger_type *type,
  1260. struct buffer_head *bh);
  1261. };
  1262. extern void jbd2_buffer_frozen_trigger(struct journal_head *jh,
  1263. void *mapped_data,
  1264. struct jbd2_buffer_trigger_type *triggers);
  1265. extern void jbd2_buffer_abort_trigger(struct journal_head *jh,
  1266. struct jbd2_buffer_trigger_type *triggers);
  1267. /* Buffer IO */
  1268. extern int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
  1269. struct journal_head *jh_in,
  1270. struct buffer_head **bh_out,
  1271. sector_t blocknr);
  1272. /* Transaction cache support */
  1273. extern void jbd2_journal_destroy_transaction_cache(void);
  1274. extern int __init jbd2_journal_init_transaction_cache(void);
  1275. extern void jbd2_journal_free_transaction(transaction_t *);
  1276. /*
  1277. * Journal locking.
  1278. *
  1279. * We need to lock the journal during transaction state changes so that nobody
  1280. * ever tries to take a handle on the running transaction while we are in the
  1281. * middle of moving it to the commit phase. j_state_lock does this.
  1282. *
  1283. * Note that the locking is completely interrupt unsafe. We never touch
  1284. * journal structures from interrupts.
  1285. */
  1286. static inline handle_t *journal_current_handle(void)
  1287. {
  1288. return current->journal_info;
  1289. }
  1290. /* The journaling code user interface:
  1291. *
  1292. * Create and destroy handles
  1293. * Register buffer modifications against the current transaction.
  1294. */
  1295. extern handle_t *jbd2_journal_start(journal_t *, int nblocks);
  1296. extern handle_t *jbd2__journal_start(journal_t *, int blocks, int rsv_blocks,
  1297. int revoke_records, gfp_t gfp_mask,
  1298. unsigned int type, unsigned int line_no);
  1299. extern int jbd2_journal_restart(handle_t *, int nblocks);
  1300. extern int jbd2__journal_restart(handle_t *, int nblocks,
  1301. int revoke_records, gfp_t gfp_mask);
  1302. extern int jbd2_journal_start_reserved(handle_t *handle,
  1303. unsigned int type, unsigned int line_no);
  1304. extern void jbd2_journal_free_reserved(handle_t *handle);
  1305. extern int jbd2_journal_extend(handle_t *handle, int nblocks,
  1306. int revoke_records);
  1307. extern int jbd2_journal_get_write_access(handle_t *, struct buffer_head *);
  1308. extern int jbd2_journal_get_create_access (handle_t *, struct buffer_head *);
  1309. extern int jbd2_journal_get_undo_access(handle_t *, struct buffer_head *);
  1310. void jbd2_journal_set_triggers(struct buffer_head *,
  1311. struct jbd2_buffer_trigger_type *type);
  1312. extern int jbd2_journal_dirty_metadata (handle_t *, struct buffer_head *);
  1313. extern int jbd2_journal_forget (handle_t *, struct buffer_head *);
  1314. int jbd2_journal_invalidate_folio(journal_t *, struct folio *,
  1315. size_t offset, size_t length);
  1316. bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio);
  1317. extern int jbd2_journal_stop(handle_t *);
  1318. extern int jbd2_journal_flush(journal_t *journal, unsigned int flags);
  1319. extern void jbd2_journal_lock_updates (journal_t *);
  1320. extern void jbd2_journal_unlock_updates (journal_t *);
  1321. void jbd2_journal_wait_updates(journal_t *);
  1322. extern journal_t * jbd2_journal_init_dev(struct block_device *bdev,
  1323. struct block_device *fs_dev,
  1324. unsigned long long start, int len, int bsize);
  1325. extern journal_t * jbd2_journal_init_inode (struct inode *);
  1326. extern int jbd2_journal_update_format (journal_t *);
  1327. extern int jbd2_journal_check_used_features
  1328. (journal_t *, unsigned long, unsigned long, unsigned long);
  1329. extern int jbd2_journal_check_available_features
  1330. (journal_t *, unsigned long, unsigned long, unsigned long);
  1331. extern int jbd2_journal_set_features
  1332. (journal_t *, unsigned long, unsigned long, unsigned long);
  1333. extern void jbd2_journal_clear_features
  1334. (journal_t *, unsigned long, unsigned long, unsigned long);
  1335. extern int jbd2_journal_load (journal_t *journal);
  1336. extern int jbd2_journal_destroy (journal_t *);
  1337. extern int jbd2_journal_recover (journal_t *journal);
  1338. extern int jbd2_journal_wipe (journal_t *, int);
  1339. extern int jbd2_journal_skip_recovery (journal_t *);
  1340. extern void jbd2_journal_update_sb_errno(journal_t *);
  1341. extern int jbd2_journal_update_sb_log_tail (journal_t *, tid_t,
  1342. unsigned long, blk_opf_t);
  1343. extern void jbd2_journal_abort (journal_t *, int);
  1344. extern int jbd2_journal_errno (journal_t *);
  1345. extern void jbd2_journal_ack_err (journal_t *);
  1346. extern int jbd2_journal_clear_err (journal_t *);
  1347. extern int jbd2_journal_bmap(journal_t *, unsigned long, unsigned long long *);
  1348. extern int jbd2_journal_force_commit(journal_t *);
  1349. extern int jbd2_journal_force_commit_nested(journal_t *);
  1350. extern int jbd2_journal_inode_ranged_write(handle_t *handle,
  1351. struct jbd2_inode *inode, loff_t start_byte,
  1352. loff_t length);
  1353. extern int jbd2_journal_inode_ranged_wait(handle_t *handle,
  1354. struct jbd2_inode *inode, loff_t start_byte,
  1355. loff_t length);
  1356. extern int jbd2_journal_submit_inode_data_buffers(
  1357. struct jbd2_inode *jinode);
  1358. extern int jbd2_journal_finish_inode_data_buffers(
  1359. struct jbd2_inode *jinode);
  1360. extern int jbd2_journal_begin_ordered_truncate(journal_t *journal,
  1361. struct jbd2_inode *inode, loff_t new_size);
  1362. extern void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode);
  1363. extern void jbd2_journal_release_jbd_inode(journal_t *journal, struct jbd2_inode *jinode);
  1364. /*
  1365. * journal_head management
  1366. */
  1367. struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh);
  1368. struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh);
  1369. void jbd2_journal_put_journal_head(struct journal_head *jh);
  1370. /*
  1371. * handle management
  1372. */
  1373. extern struct kmem_cache *jbd2_handle_cache;
  1374. static inline handle_t *jbd2_alloc_handle(gfp_t gfp_flags)
  1375. {
  1376. return kmem_cache_zalloc(jbd2_handle_cache, gfp_flags);
  1377. }
  1378. static inline void jbd2_free_handle(handle_t *handle)
  1379. {
  1380. kmem_cache_free(jbd2_handle_cache, handle);
  1381. }
  1382. /*
  1383. * jbd2_inode management (optional, for those file systems that want to use
  1384. * dynamically allocated jbd2_inode structures)
  1385. */
  1386. extern struct kmem_cache *jbd2_inode_cache;
  1387. static inline struct jbd2_inode *jbd2_alloc_inode(gfp_t gfp_flags)
  1388. {
  1389. return kmem_cache_alloc(jbd2_inode_cache, gfp_flags);
  1390. }
  1391. static inline void jbd2_free_inode(struct jbd2_inode *jinode)
  1392. {
  1393. kmem_cache_free(jbd2_inode_cache, jinode);
  1394. }
  1395. /* Primary revoke support */
  1396. #define JOURNAL_REVOKE_DEFAULT_HASH 256
  1397. extern int jbd2_journal_init_revoke(journal_t *, int);
  1398. extern void jbd2_journal_destroy_revoke_record_cache(void);
  1399. extern void jbd2_journal_destroy_revoke_table_cache(void);
  1400. extern int __init jbd2_journal_init_revoke_record_cache(void);
  1401. extern int __init jbd2_journal_init_revoke_table_cache(void);
  1402. extern void jbd2_journal_destroy_revoke(journal_t *);
  1403. extern int jbd2_journal_revoke (handle_t *, unsigned long long, struct buffer_head *);
  1404. extern int jbd2_journal_cancel_revoke(handle_t *, struct journal_head *);
  1405. extern void jbd2_journal_write_revoke_records(transaction_t *transaction,
  1406. struct list_head *log_bufs);
  1407. /* Recovery revoke support */
  1408. extern int jbd2_journal_set_revoke(journal_t *, unsigned long long, tid_t);
  1409. extern int jbd2_journal_test_revoke(journal_t *, unsigned long long, tid_t);
  1410. extern void jbd2_journal_clear_revoke(journal_t *);
  1411. extern void jbd2_journal_switch_revoke_table(journal_t *journal);
  1412. extern void jbd2_clear_buffer_revoked_flags(journal_t *journal);
  1413. /*
  1414. * The log thread user interface:
  1415. *
  1416. * Request space in the current transaction, and force transaction commit
  1417. * transitions on demand.
  1418. */
  1419. int jbd2_log_start_commit(journal_t *journal, tid_t tid);
  1420. int jbd2_journal_start_commit(journal_t *journal, tid_t *tid);
  1421. int jbd2_log_wait_commit(journal_t *journal, tid_t tid);
  1422. int jbd2_transaction_committed(journal_t *journal, tid_t tid);
  1423. int jbd2_complete_transaction(journal_t *journal, tid_t tid);
  1424. int jbd2_log_do_checkpoint(journal_t *journal);
  1425. int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid);
  1426. void __jbd2_log_wait_for_space(journal_t *journal);
  1427. extern void __jbd2_journal_drop_transaction(journal_t *, transaction_t *);
  1428. extern int jbd2_cleanup_journal_tail(journal_t *);
  1429. /* Fast commit related APIs */
  1430. int jbd2_fc_begin_commit(journal_t *journal, tid_t tid);
  1431. int jbd2_fc_end_commit(journal_t *journal);
  1432. int jbd2_fc_end_commit_fallback(journal_t *journal);
  1433. int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out);
  1434. int jbd2_submit_inode_data(struct jbd2_inode *jinode);
  1435. int jbd2_wait_inode_data(journal_t *journal, struct jbd2_inode *jinode);
  1436. int jbd2_fc_wait_bufs(journal_t *journal, int num_blks);
  1437. int jbd2_fc_release_bufs(journal_t *journal);
  1438. static inline int jbd2_journal_get_max_txn_bufs(journal_t *journal)
  1439. {
  1440. return (journal->j_total_len - journal->j_fc_wbufsize) / 4;
  1441. }
  1442. /*
  1443. * is_journal_abort
  1444. *
  1445. * Simple test wrapper function to test the JBD2_ABORT state flag. This
  1446. * bit, when set, indicates that we have had a fatal error somewhere,
  1447. * either inside the journaling layer or indicated to us by the client
  1448. * (eg. ext3), and that we and should not commit any further
  1449. * transactions.
  1450. */
  1451. static inline int is_journal_aborted(journal_t *journal)
  1452. {
  1453. return journal->j_flags & JBD2_ABORT;
  1454. }
  1455. static inline int is_handle_aborted(handle_t *handle)
  1456. {
  1457. if (handle->h_aborted || !handle->h_transaction)
  1458. return 1;
  1459. return is_journal_aborted(handle->h_transaction->t_journal);
  1460. }
  1461. static inline void jbd2_journal_abort_handle(handle_t *handle)
  1462. {
  1463. handle->h_aborted = 1;
  1464. }
  1465. #endif /* __KERNEL__ */
  1466. /* Comparison functions for transaction IDs: perform comparisons using
  1467. * modulo arithmetic so that they work over sequence number wraps. */
  1468. static inline int tid_gt(tid_t x, tid_t y)
  1469. {
  1470. int difference = (x - y);
  1471. return (difference > 0);
  1472. }
  1473. static inline int tid_geq(tid_t x, tid_t y)
  1474. {
  1475. int difference = (x - y);
  1476. return (difference >= 0);
  1477. }
  1478. extern int jbd2_journal_blocks_per_page(struct inode *inode);
  1479. extern size_t journal_tag_bytes(journal_t *journal);
  1480. static inline bool jbd2_journal_has_csum_v2or3_feature(journal_t *j)
  1481. {
  1482. return jbd2_has_feature_csum2(j) || jbd2_has_feature_csum3(j);
  1483. }
  1484. static inline int jbd2_journal_has_csum_v2or3(journal_t *journal)
  1485. {
  1486. WARN_ON_ONCE(jbd2_journal_has_csum_v2or3_feature(journal) &&
  1487. journal->j_chksum_driver == NULL);
  1488. return journal->j_chksum_driver != NULL;
  1489. }
  1490. static inline int jbd2_journal_get_num_fc_blks(journal_superblock_t *jsb)
  1491. {
  1492. int num_fc_blocks = be32_to_cpu(jsb->s_num_fc_blks);
  1493. return num_fc_blocks ? num_fc_blocks : JBD2_DEFAULT_FAST_COMMIT_BLOCKS;
  1494. }
  1495. /*
  1496. * Return number of free blocks in the log. Must be called under j_state_lock.
  1497. */
  1498. static inline unsigned long jbd2_log_space_left(journal_t *journal)
  1499. {
  1500. /* Allow for rounding errors */
  1501. long free = journal->j_free - 32;
  1502. if (journal->j_committing_transaction) {
  1503. free -= atomic_read(&journal->
  1504. j_committing_transaction->t_outstanding_credits);
  1505. }
  1506. return max_t(long, free, 0);
  1507. }
  1508. /*
  1509. * Definitions which augment the buffer_head layer
  1510. */
  1511. /* journaling buffer types */
  1512. #define BJ_None 0 /* Not journaled */
  1513. #define BJ_Metadata 1 /* Normal journaled metadata */
  1514. #define BJ_Forget 2 /* Buffer superseded by this transaction */
  1515. #define BJ_Shadow 3 /* Buffer contents being shadowed to the log */
  1516. #define BJ_Reserved 4 /* Buffer is reserved for access by journal */
  1517. #define BJ_Types 5
  1518. /* JBD uses a CRC32 checksum */
  1519. #define JBD_MAX_CHECKSUM_SIZE 4
  1520. static inline u32 jbd2_chksum(journal_t *journal, u32 crc,
  1521. const void *address, unsigned int length)
  1522. {
  1523. struct {
  1524. struct shash_desc shash;
  1525. char ctx[JBD_MAX_CHECKSUM_SIZE];
  1526. } desc;
  1527. int err;
  1528. BUG_ON(crypto_shash_descsize(journal->j_chksum_driver) >
  1529. JBD_MAX_CHECKSUM_SIZE);
  1530. desc.shash.tfm = journal->j_chksum_driver;
  1531. *(u32 *)desc.ctx = crc;
  1532. err = crypto_shash_update(&desc.shash, address, length);
  1533. BUG_ON(err);
  1534. return *(u32 *)desc.ctx;
  1535. }
  1536. /* Return most recent uncommitted transaction */
  1537. static inline tid_t jbd2_get_latest_transaction(journal_t *journal)
  1538. {
  1539. tid_t tid;
  1540. read_lock(&journal->j_state_lock);
  1541. tid = journal->j_commit_request;
  1542. if (journal->j_running_transaction)
  1543. tid = journal->j_running_transaction->t_tid;
  1544. read_unlock(&journal->j_state_lock);
  1545. return tid;
  1546. }
  1547. static inline int jbd2_handle_buffer_credits(handle_t *handle)
  1548. {
  1549. journal_t *journal;
  1550. if (!handle->h_reserved)
  1551. journal = handle->h_transaction->t_journal;
  1552. else
  1553. journal = handle->h_journal;
  1554. return handle->h_total_credits -
  1555. DIV_ROUND_UP(handle->h_revoke_credits_requested,
  1556. journal->j_revoke_records_per_block);
  1557. }
  1558. #ifdef __KERNEL__
  1559. #define buffer_trace_init(bh) do {} while (0)
  1560. #define print_buffer_fields(bh) do {} while (0)
  1561. #define print_buffer_trace(bh) do {} while (0)
  1562. #define BUFFER_TRACE(bh, info) do {} while (0)
  1563. #define BUFFER_TRACE2(bh, bh2, info) do {} while (0)
  1564. #define JBUFFER_TRACE(jh, info) do {} while (0)
  1565. #endif /* __KERNEL__ */
  1566. #define EFSBADCRC EBADMSG /* Bad CRC detected */
  1567. #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
  1568. #endif /* _LINUX_JBD2_H */