journal.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * journal.h
  6. *
  7. * Defines journalling api and structures.
  8. *
  9. * Copyright (C) 2003, 2005 Oracle. All rights reserved.
  10. */
  11. #ifndef OCFS2_JOURNAL_H
  12. #define OCFS2_JOURNAL_H
  13. #include <linux/fs.h>
  14. #include <linux/jbd2.h>
  15. enum ocfs2_journal_state {
  16. OCFS2_JOURNAL_FREE = 0,
  17. OCFS2_JOURNAL_LOADED,
  18. OCFS2_JOURNAL_IN_SHUTDOWN,
  19. };
  20. struct ocfs2_super;
  21. struct ocfs2_dinode;
  22. /*
  23. * The recovery_list is a simple linked list of node numbers to recover.
  24. * It is protected by the recovery_lock.
  25. */
  26. struct ocfs2_recovery_map {
  27. unsigned int rm_used;
  28. unsigned int *rm_entries;
  29. };
  30. struct ocfs2_journal {
  31. enum ocfs2_journal_state j_state; /* Journals current state */
  32. journal_t *j_journal; /* The kernels journal type */
  33. struct inode *j_inode; /* Kernel inode pointing to
  34. * this journal */
  35. struct ocfs2_super *j_osb; /* pointer to the super
  36. * block for the node
  37. * we're currently
  38. * running on -- not
  39. * necessarily the super
  40. * block from the node
  41. * which we usually run
  42. * from (recovery,
  43. * etc) */
  44. struct buffer_head *j_bh; /* Journal disk inode block */
  45. atomic_t j_num_trans; /* Number of transactions
  46. * currently in the system. */
  47. spinlock_t j_lock;
  48. unsigned long j_trans_id;
  49. struct rw_semaphore j_trans_barrier;
  50. wait_queue_head_t j_checkpointed;
  51. /* both fields protected by j_lock*/
  52. struct list_head j_la_cleanups;
  53. struct work_struct j_recovery_work;
  54. };
  55. extern spinlock_t trans_inc_lock;
  56. /* wrap j_trans_id so we never have it equal to zero. */
  57. static inline unsigned long ocfs2_inc_trans_id(struct ocfs2_journal *j)
  58. {
  59. unsigned long old_id;
  60. spin_lock(&trans_inc_lock);
  61. old_id = j->j_trans_id++;
  62. if (unlikely(!j->j_trans_id))
  63. j->j_trans_id = 1;
  64. spin_unlock(&trans_inc_lock);
  65. return old_id;
  66. }
  67. static inline void ocfs2_set_ci_lock_trans(struct ocfs2_journal *journal,
  68. struct ocfs2_caching_info *ci)
  69. {
  70. spin_lock(&trans_inc_lock);
  71. ci->ci_last_trans = journal->j_trans_id;
  72. spin_unlock(&trans_inc_lock);
  73. }
  74. /* Used to figure out whether it's safe to drop a metadata lock on an
  75. * cached object. Returns true if all the object's changes have been
  76. * checkpointed to disk. You should be holding the spinlock on the
  77. * metadata lock while calling this to be sure that nobody can take
  78. * the lock and put it on another transaction. */
  79. static inline int ocfs2_ci_fully_checkpointed(struct ocfs2_caching_info *ci)
  80. {
  81. int ret;
  82. struct ocfs2_journal *journal =
  83. OCFS2_SB(ocfs2_metadata_cache_get_super(ci))->journal;
  84. spin_lock(&trans_inc_lock);
  85. ret = time_after(journal->j_trans_id, ci->ci_last_trans);
  86. spin_unlock(&trans_inc_lock);
  87. return ret;
  88. }
  89. /* convenience function to check if an object backed by struct
  90. * ocfs2_caching_info is still new (has never hit disk) Will do you a
  91. * favor and set created_trans = 0 when you've
  92. * been checkpointed. returns '1' if the ci is still new. */
  93. static inline int ocfs2_ci_is_new(struct ocfs2_caching_info *ci)
  94. {
  95. int ret;
  96. struct ocfs2_journal *journal =
  97. OCFS2_SB(ocfs2_metadata_cache_get_super(ci))->journal;
  98. spin_lock(&trans_inc_lock);
  99. ret = !(time_after(journal->j_trans_id, ci->ci_created_trans));
  100. if (!ret)
  101. ci->ci_created_trans = 0;
  102. spin_unlock(&trans_inc_lock);
  103. return ret;
  104. }
  105. /* Wrapper for inodes so we can check system files */
  106. static inline int ocfs2_inode_is_new(struct inode *inode)
  107. {
  108. /* System files are never "new" as they're written out by
  109. * mkfs. This helps us early during mount, before we have the
  110. * journal open and j_trans_id could be junk. */
  111. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
  112. return 0;
  113. return ocfs2_ci_is_new(INODE_CACHE(inode));
  114. }
  115. static inline void ocfs2_ci_set_new(struct ocfs2_super *osb,
  116. struct ocfs2_caching_info *ci)
  117. {
  118. spin_lock(&trans_inc_lock);
  119. ci->ci_created_trans = osb->journal->j_trans_id;
  120. spin_unlock(&trans_inc_lock);
  121. }
  122. /* Exported only for the journal struct init code in super.c. Do not call. */
  123. void ocfs2_orphan_scan_init(struct ocfs2_super *osb);
  124. void ocfs2_orphan_scan_start(struct ocfs2_super *osb);
  125. void ocfs2_orphan_scan_stop(struct ocfs2_super *osb);
  126. void ocfs2_complete_recovery(struct work_struct *work);
  127. void ocfs2_wait_for_recovery(struct ocfs2_super *osb);
  128. int ocfs2_recovery_init(struct ocfs2_super *osb);
  129. void ocfs2_recovery_exit(struct ocfs2_super *osb);
  130. int ocfs2_compute_replay_slots(struct ocfs2_super *osb);
  131. /*
  132. * Journal Control:
  133. * Initialize, Load, Shutdown, Wipe a journal.
  134. *
  135. * ocfs2_journal_init - Initialize journal structures in the OSB.
  136. * ocfs2_journal_load - Load the given journal off disk. Replay it if
  137. * there's transactions still in there.
  138. * ocfs2_journal_shutdown - Shutdown a journal, this will flush all
  139. * uncommitted, uncheckpointed transactions.
  140. * ocfs2_journal_wipe - Wipe transactions from a journal. Optionally
  141. * zero out each block.
  142. * ocfs2_recovery_thread - Perform recovery on a node. osb is our own osb.
  143. * ocfs2_mark_dead_nodes - Start recovery on nodes we won't get a heartbeat
  144. * event on.
  145. * ocfs2_start_checkpoint - Kick the commit thread to do a checkpoint.
  146. */
  147. void ocfs2_set_journal_params(struct ocfs2_super *osb);
  148. int ocfs2_journal_init(struct ocfs2_journal *journal,
  149. int *dirty);
  150. void ocfs2_journal_shutdown(struct ocfs2_super *osb);
  151. int ocfs2_journal_wipe(struct ocfs2_journal *journal,
  152. int full);
  153. int ocfs2_journal_load(struct ocfs2_journal *journal, int local,
  154. int replayed);
  155. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb);
  156. void ocfs2_recovery_thread(struct ocfs2_super *osb,
  157. int node_num);
  158. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb);
  159. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb);
  160. void ocfs2_complete_quota_recovery(struct ocfs2_super *osb);
  161. static inline void ocfs2_start_checkpoint(struct ocfs2_super *osb)
  162. {
  163. wake_up(&osb->checkpoint_event);
  164. }
  165. static inline void ocfs2_checkpoint_inode(struct inode *inode)
  166. {
  167. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  168. if (ocfs2_mount_local(osb))
  169. return;
  170. if (!ocfs2_ci_fully_checkpointed(INODE_CACHE(inode))) {
  171. /* WARNING: This only kicks off a single
  172. * checkpoint. If someone races you and adds more
  173. * metadata to the journal, you won't know, and will
  174. * wind up waiting *a lot* longer than necessary. Right
  175. * now we only use this in clear_inode so that's
  176. * OK. */
  177. ocfs2_start_checkpoint(osb);
  178. wait_event(osb->journal->j_checkpointed,
  179. ocfs2_ci_fully_checkpointed(INODE_CACHE(inode)));
  180. }
  181. }
  182. /*
  183. * Transaction Handling:
  184. * Manage the lifetime of a transaction handle.
  185. *
  186. * ocfs2_start_trans - Begin a transaction. Give it an upper estimate of
  187. * the number of blocks that will be changed during
  188. * this handle.
  189. * ocfs2_commit_trans - Complete a handle. It might return -EIO if
  190. * the journal was aborted. The majority of paths don't
  191. * check the return value as an error there comes too
  192. * late to do anything (and will be picked up in a
  193. * later transaction).
  194. * ocfs2_extend_trans - Extend a handle by nblocks credits. This may
  195. * commit the handle to disk in the process, but will
  196. * not release any locks taken during the transaction.
  197. * ocfs2_journal_access* - Notify the handle that we want to journal this
  198. * buffer. Will have to call ocfs2_journal_dirty once
  199. * we've actually dirtied it. Type is one of . or .
  200. * Always call the specific flavor of
  201. * ocfs2_journal_access_*() unless you intend to
  202. * manage the checksum by hand.
  203. * ocfs2_journal_dirty - Mark a journalled buffer as having dirty data.
  204. * ocfs2_jbd2_inode_add_write - Mark an inode with range so that its data goes
  205. * out before the current handle commits.
  206. */
  207. /* You must always start_trans with a number of buffs > 0, but it's
  208. * perfectly legal to go through an entire transaction without having
  209. * dirtied any buffers. */
  210. handle_t *ocfs2_start_trans(struct ocfs2_super *osb,
  211. int max_buffs);
  212. int ocfs2_commit_trans(struct ocfs2_super *osb,
  213. handle_t *handle);
  214. int ocfs2_extend_trans(handle_t *handle, int nblocks);
  215. int ocfs2_allocate_extend_trans(handle_t *handle,
  216. int thresh);
  217. /*
  218. * Define an arbitrary limit for the amount of data we will anticipate
  219. * writing to any given transaction. For unbounded transactions such as
  220. * fallocate(2) we can write more than this, but we always
  221. * start off at the maximum transaction size and grow the transaction
  222. * optimistically as we go.
  223. */
  224. #define OCFS2_MAX_TRANS_DATA 64U
  225. /*
  226. * Create access is for when we get a newly created buffer and we're
  227. * not gonna read it off disk, but rather fill it ourselves. Right
  228. * now, we don't do anything special with this (it turns into a write
  229. * request), but this is a good placeholder in case we do...
  230. *
  231. * Write access is for when we read a block off disk and are going to
  232. * modify it. This way the journalling layer knows it may need to make
  233. * a copy of that block (if it's part of another, uncommitted
  234. * transaction) before we do so.
  235. */
  236. #define OCFS2_JOURNAL_ACCESS_CREATE 0
  237. #define OCFS2_JOURNAL_ACCESS_WRITE 1
  238. #define OCFS2_JOURNAL_ACCESS_UNDO 2
  239. /* ocfs2_inode */
  240. int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
  241. struct buffer_head *bh, int type);
  242. /* ocfs2_extent_block */
  243. int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
  244. struct buffer_head *bh, int type);
  245. /* ocfs2_refcount_block */
  246. int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
  247. struct buffer_head *bh, int type);
  248. /* ocfs2_group_desc */
  249. int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
  250. struct buffer_head *bh, int type);
  251. /* ocfs2_xattr_block */
  252. int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
  253. struct buffer_head *bh, int type);
  254. /* quota blocks */
  255. int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
  256. struct buffer_head *bh, int type);
  257. /* dirblock */
  258. int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
  259. struct buffer_head *bh, int type);
  260. /* ocfs2_dx_root_block */
  261. int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
  262. struct buffer_head *bh, int type);
  263. /* ocfs2_dx_leaf */
  264. int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
  265. struct buffer_head *bh, int type);
  266. /* Anything that has no ecc */
  267. int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
  268. struct buffer_head *bh, int type);
  269. /*
  270. * A word about the journal_access/journal_dirty "dance". It is
  271. * entirely legal to journal_access a buffer more than once (as long
  272. * as the access type is the same -- I'm not sure what will happen if
  273. * access type is different but this should never happen anyway) It is
  274. * also legal to journal_dirty a buffer more than once. In fact, you
  275. * can even journal_access a buffer after you've done a
  276. * journal_access/journal_dirty pair. The only thing you cannot do
  277. * however, is journal_dirty a buffer which you haven't yet passed to
  278. * journal_access at least once.
  279. *
  280. * That said, 99% of the time this doesn't matter and this is what the
  281. * path looks like:
  282. *
  283. * <read a bh>
  284. * ocfs2_journal_access(handle, bh, OCFS2_JOURNAL_ACCESS_WRITE);
  285. * <modify the bh>
  286. * ocfs2_journal_dirty(handle, bh);
  287. */
  288. void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh);
  289. /*
  290. * Credit Macros:
  291. * Convenience macros to calculate number of credits needed.
  292. *
  293. * For convenience sake, I have a set of macros here which calculate
  294. * the *maximum* number of sectors which will be changed for various
  295. * metadata updates.
  296. */
  297. /* simple file updates like chmod, etc. */
  298. #define OCFS2_INODE_UPDATE_CREDITS 1
  299. /* extended attribute block update */
  300. #define OCFS2_XATTR_BLOCK_UPDATE_CREDITS 1
  301. /* Update of a single quota block */
  302. #define OCFS2_QUOTA_BLOCK_UPDATE_CREDITS 1
  303. /* global quotafile inode update, data block */
  304. #define OCFS2_QINFO_WRITE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + \
  305. OCFS2_QUOTA_BLOCK_UPDATE_CREDITS)
  306. #define OCFS2_LOCAL_QINFO_WRITE_CREDITS OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
  307. /*
  308. * The two writes below can accidentally see global info dirty due
  309. * to set_info() quotactl so make them prepared for the writes.
  310. */
  311. /* quota data block, global info */
  312. /* Write to local quota file */
  313. #define OCFS2_QWRITE_CREDITS (OCFS2_QINFO_WRITE_CREDITS + \
  314. OCFS2_QUOTA_BLOCK_UPDATE_CREDITS)
  315. /* global quota data block, local quota data block, global quota inode,
  316. * global quota info */
  317. #define OCFS2_QSYNC_CREDITS (OCFS2_QINFO_WRITE_CREDITS + \
  318. 2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS)
  319. static inline int ocfs2_quota_trans_credits(struct super_block *sb)
  320. {
  321. int credits = 0;
  322. if (OCFS2_HAS_RO_COMPAT_FEATURE(sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA))
  323. credits += OCFS2_QWRITE_CREDITS;
  324. if (OCFS2_HAS_RO_COMPAT_FEATURE(sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA))
  325. credits += OCFS2_QWRITE_CREDITS;
  326. return credits;
  327. }
  328. /* group extend. inode update and last group update. */
  329. #define OCFS2_GROUP_EXTEND_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
  330. /* group add. inode update and the new group update. */
  331. #define OCFS2_GROUP_ADD_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
  332. /* get one bit out of a suballocator: dinode + group descriptor +
  333. * prev. group desc. if we relink. */
  334. #define OCFS2_SUBALLOC_ALLOC (3)
  335. static inline int ocfs2_inline_to_extents_credits(struct super_block *sb)
  336. {
  337. return OCFS2_SUBALLOC_ALLOC + OCFS2_INODE_UPDATE_CREDITS +
  338. ocfs2_quota_trans_credits(sb);
  339. }
  340. /* dinode + group descriptor update. We don't relink on free yet. */
  341. #define OCFS2_SUBALLOC_FREE (2)
  342. #define OCFS2_TRUNCATE_LOG_UPDATE OCFS2_INODE_UPDATE_CREDITS
  343. #define OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC (OCFS2_SUBALLOC_FREE \
  344. + OCFS2_TRUNCATE_LOG_UPDATE)
  345. static inline int ocfs2_remove_extent_credits(struct super_block *sb)
  346. {
  347. return OCFS2_TRUNCATE_LOG_UPDATE + OCFS2_INODE_UPDATE_CREDITS +
  348. ocfs2_quota_trans_credits(sb);
  349. }
  350. /* data block for new dir/symlink, allocation of directory block, dx_root
  351. * update for free list */
  352. #define OCFS2_DIR_LINK_ADDITIONAL_CREDITS (1 + OCFS2_SUBALLOC_ALLOC + 1)
  353. static inline int ocfs2_add_dir_index_credits(struct super_block *sb)
  354. {
  355. /* 1 block for index, 2 allocs (data, metadata), 1 clusters
  356. * worth of blocks for initial extent. */
  357. return 1 + 2 * OCFS2_SUBALLOC_ALLOC +
  358. ocfs2_clusters_to_blocks(sb, 1);
  359. }
  360. /* parent fe, parent block, new file entry, index leaf, inode alloc fe, inode
  361. * alloc group descriptor + mkdir/symlink blocks + dir blocks + xattr
  362. * blocks + quota update */
  363. static inline int ocfs2_mknod_credits(struct super_block *sb, int is_dir,
  364. int xattr_credits)
  365. {
  366. int dir_credits = OCFS2_DIR_LINK_ADDITIONAL_CREDITS;
  367. if (is_dir)
  368. dir_credits += ocfs2_add_dir_index_credits(sb);
  369. return 4 + OCFS2_SUBALLOC_ALLOC + dir_credits + xattr_credits +
  370. ocfs2_quota_trans_credits(sb);
  371. }
  372. /* local alloc metadata change + main bitmap updates */
  373. #define OCFS2_WINDOW_MOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS \
  374. + OCFS2_SUBALLOC_ALLOC + OCFS2_SUBALLOC_FREE)
  375. /* used when we don't need an allocation change for a dir extend. One
  376. * for the dinode, one for the new block. */
  377. #define OCFS2_SIMPLE_DIR_EXTEND_CREDITS (2)
  378. /* file update (nlink, etc) + directory mtime/ctime + dir entry block + quota
  379. * update on dir + index leaf + dx root update for free list +
  380. * previous dirblock update in the free list */
  381. static inline int ocfs2_link_credits(struct super_block *sb)
  382. {
  383. return 2 * OCFS2_INODE_UPDATE_CREDITS + 4 +
  384. ocfs2_quota_trans_credits(sb);
  385. }
  386. /* inode + dir inode (if we unlink a dir), + dir entry block + orphan
  387. * dir inode link + dir inode index leaf + dir index root */
  388. static inline int ocfs2_unlink_credits(struct super_block *sb)
  389. {
  390. /* The quota update from ocfs2_link_credits is unused here... */
  391. return 2 * OCFS2_INODE_UPDATE_CREDITS + 3 + ocfs2_link_credits(sb);
  392. }
  393. /* dinode + orphan dir dinode + inode alloc dinode + orphan dir entry +
  394. * inode alloc group descriptor + orphan dir index root +
  395. * orphan dir index leaf */
  396. #define OCFS2_DELETE_INODE_CREDITS (3 * OCFS2_INODE_UPDATE_CREDITS + 4)
  397. /* dinode + orphan dir dinode + extent tree leaf block + orphan dir entry +
  398. * orphan dir index root + orphan dir index leaf */
  399. #define OCFS2_INODE_ADD_TO_ORPHAN_CREDITS (2 * OCFS2_INODE_UPDATE_CREDITS + 4)
  400. #define OCFS2_INODE_DEL_FROM_ORPHAN_CREDITS OCFS2_INODE_ADD_TO_ORPHAN_CREDITS
  401. /* dinode update, old dir dinode update, new dir dinode update, old
  402. * dir dir entry, new dir dir entry, dir entry update for renaming
  403. * directory + target unlink + 3 x dir index leaves */
  404. static inline int ocfs2_rename_credits(struct super_block *sb)
  405. {
  406. return 3 * OCFS2_INODE_UPDATE_CREDITS + 6 + ocfs2_unlink_credits(sb);
  407. }
  408. /* global bitmap dinode, group desc., relinked group,
  409. * suballocator dinode, group desc., relinked group,
  410. * dinode, xattr block */
  411. #define OCFS2_XATTR_BLOCK_CREATE_CREDITS (OCFS2_SUBALLOC_ALLOC * 2 + \
  412. + OCFS2_INODE_UPDATE_CREDITS \
  413. + OCFS2_XATTR_BLOCK_UPDATE_CREDITS)
  414. /* inode update, removal of dx root block from allocator */
  415. #define OCFS2_DX_ROOT_REMOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + \
  416. OCFS2_SUBALLOC_FREE)
  417. static inline int ocfs2_calc_dxi_expand_credits(struct super_block *sb)
  418. {
  419. int credits = 1 + OCFS2_SUBALLOC_ALLOC;
  420. credits += ocfs2_clusters_to_blocks(sb, 1);
  421. credits += ocfs2_quota_trans_credits(sb);
  422. return credits;
  423. }
  424. /* inode update, new refcount block and its allocation credits. */
  425. #define OCFS2_REFCOUNT_TREE_CREATE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1 \
  426. + OCFS2_SUBALLOC_ALLOC)
  427. /* inode and the refcount block update. */
  428. #define OCFS2_REFCOUNT_TREE_SET_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
  429. /*
  430. * inode and the refcount block update.
  431. * It doesn't include the credits for sub alloc change.
  432. * So if we need to free the bit, OCFS2_SUBALLOC_FREE needs to be added.
  433. */
  434. #define OCFS2_REFCOUNT_TREE_REMOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
  435. /* 2 metadata alloc, 2 new blocks and root refcount block */
  436. #define OCFS2_EXPAND_REFCOUNT_TREE_CREDITS (OCFS2_SUBALLOC_ALLOC * 2 + 3)
  437. /*
  438. * Please note that the caller must make sure that root_el is the root
  439. * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
  440. * the result may be wrong.
  441. */
  442. static inline int ocfs2_calc_extend_credits(struct super_block *sb,
  443. struct ocfs2_extent_list *root_el)
  444. {
  445. int bitmap_blocks, sysfile_bitmap_blocks, extent_blocks;
  446. /* bitmap dinode, group desc. + relinked group. */
  447. bitmap_blocks = OCFS2_SUBALLOC_ALLOC;
  448. /* we might need to shift tree depth so lets assume an
  449. * absolute worst case of complete fragmentation. Even with
  450. * that, we only need one update for the dinode, and then
  451. * however many metadata chunks needed * a remaining suballoc
  452. * alloc. */
  453. sysfile_bitmap_blocks = 1 +
  454. (OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(root_el);
  455. /* this does not include *new* metadata blocks, which are
  456. * accounted for in sysfile_bitmap_blocks. root_el +
  457. * prev. last_eb_blk + blocks along edge of tree.
  458. * calc_symlink_credits passes because we just need 1
  459. * credit for the dinode there. */
  460. extent_blocks = 1 + 1 + le16_to_cpu(root_el->l_tree_depth);
  461. return bitmap_blocks + sysfile_bitmap_blocks + extent_blocks +
  462. ocfs2_quota_trans_credits(sb);
  463. }
  464. static inline int ocfs2_calc_symlink_credits(struct super_block *sb)
  465. {
  466. int blocks = ocfs2_mknod_credits(sb, 0, 0);
  467. /* links can be longer than one block so we may update many
  468. * within our single allocated extent. */
  469. blocks += ocfs2_clusters_to_blocks(sb, 1);
  470. return blocks + ocfs2_quota_trans_credits(sb);
  471. }
  472. static inline int ocfs2_calc_group_alloc_credits(struct super_block *sb,
  473. unsigned int cpg)
  474. {
  475. int blocks;
  476. int bitmap_blocks = OCFS2_SUBALLOC_ALLOC + 1;
  477. /* parent inode update + new block group header + bitmap inode update
  478. + bitmap blocks affected */
  479. blocks = 1 + 1 + 1 + bitmap_blocks;
  480. return blocks;
  481. }
  482. /*
  483. * Allocating a discontiguous block group requires the credits from
  484. * ocfs2_calc_group_alloc_credits() as well as enough credits to fill
  485. * the group descriptor's extent list. The caller already has started
  486. * the transaction with ocfs2_calc_group_alloc_credits(). They extend
  487. * it with these credits.
  488. */
  489. static inline int ocfs2_calc_bg_discontig_credits(struct super_block *sb)
  490. {
  491. return ocfs2_extent_recs_per_gd(sb);
  492. }
  493. static inline int ocfs2_jbd2_inode_add_write(handle_t *handle, struct inode *inode,
  494. loff_t start_byte, loff_t length)
  495. {
  496. return jbd2_journal_inode_ranged_write(handle,
  497. &OCFS2_I(inode)->ip_jinode,
  498. start_byte, length);
  499. }
  500. static inline int ocfs2_begin_ordered_truncate(struct inode *inode,
  501. loff_t new_size)
  502. {
  503. return jbd2_journal_begin_ordered_truncate(
  504. OCFS2_SB(inode->i_sb)->journal->j_journal,
  505. &OCFS2_I(inode)->ip_jinode,
  506. new_size);
  507. }
  508. static inline void ocfs2_update_inode_fsync_trans(handle_t *handle,
  509. struct inode *inode,
  510. int datasync)
  511. {
  512. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  513. if (!is_handle_aborted(handle)) {
  514. oi->i_sync_tid = handle->h_transaction->t_tid;
  515. if (datasync)
  516. oi->i_datasync_tid = handle->h_transaction->t_tid;
  517. }
  518. }
  519. #endif /* OCFS2_JOURNAL_H */