Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * journal.h
0004  *
0005  * Defines journalling api and structures.
0006  *
0007  * Copyright (C) 2003, 2005 Oracle.  All rights reserved.
0008  */
0009 
0010 #ifndef OCFS2_JOURNAL_H
0011 #define OCFS2_JOURNAL_H
0012 
0013 #include <linux/fs.h>
0014 #include <linux/jbd2.h>
0015 
0016 enum ocfs2_journal_state {
0017     OCFS2_JOURNAL_FREE = 0,
0018     OCFS2_JOURNAL_LOADED,
0019     OCFS2_JOURNAL_IN_SHUTDOWN,
0020 };
0021 
0022 struct ocfs2_super;
0023 struct ocfs2_dinode;
0024 
0025 /*
0026  * The recovery_list is a simple linked list of node numbers to recover.
0027  * It is protected by the recovery_lock.
0028  */
0029 
0030 struct ocfs2_recovery_map {
0031     unsigned int rm_used;
0032     unsigned int *rm_entries;
0033 };
0034 
0035 
0036 struct ocfs2_journal {
0037     enum ocfs2_journal_state   j_state;    /* Journals current state   */
0038 
0039     journal_t                 *j_journal; /* The kernels journal type */
0040     struct inode              *j_inode;   /* Kernel inode pointing to
0041                            * this journal             */
0042     struct ocfs2_super        *j_osb;     /* pointer to the super
0043                            * block for the node
0044                            * we're currently
0045                            * running on -- not
0046                            * necessarily the super
0047                            * block from the node
0048                            * which we usually run
0049                            * from (recovery,
0050                            * etc)                     */
0051     struct buffer_head        *j_bh;      /* Journal disk inode block */
0052     atomic_t                  j_num_trans; /* Number of transactions
0053                             * currently in the system. */
0054     spinlock_t                j_lock;
0055     unsigned long             j_trans_id;
0056     struct rw_semaphore       j_trans_barrier;
0057     wait_queue_head_t         j_checkpointed;
0058 
0059     /* both fields protected by j_lock*/
0060     struct list_head          j_la_cleanups;
0061     struct work_struct        j_recovery_work;
0062 };
0063 
0064 extern spinlock_t trans_inc_lock;
0065 
0066 /* wrap j_trans_id so we never have it equal to zero. */
0067 static inline unsigned long ocfs2_inc_trans_id(struct ocfs2_journal *j)
0068 {
0069     unsigned long old_id;
0070     spin_lock(&trans_inc_lock);
0071     old_id = j->j_trans_id++;
0072     if (unlikely(!j->j_trans_id))
0073         j->j_trans_id = 1;
0074     spin_unlock(&trans_inc_lock);
0075     return old_id;
0076 }
0077 
0078 static inline void ocfs2_set_ci_lock_trans(struct ocfs2_journal *journal,
0079                        struct ocfs2_caching_info *ci)
0080 {
0081     spin_lock(&trans_inc_lock);
0082     ci->ci_last_trans = journal->j_trans_id;
0083     spin_unlock(&trans_inc_lock);
0084 }
0085 
0086 /* Used to figure out whether it's safe to drop a metadata lock on an
0087  * cached object. Returns true if all the object's changes have been
0088  * checkpointed to disk. You should be holding the spinlock on the
0089  * metadata lock while calling this to be sure that nobody can take
0090  * the lock and put it on another transaction. */
0091 static inline int ocfs2_ci_fully_checkpointed(struct ocfs2_caching_info *ci)
0092 {
0093     int ret;
0094     struct ocfs2_journal *journal =
0095         OCFS2_SB(ocfs2_metadata_cache_get_super(ci))->journal;
0096 
0097     spin_lock(&trans_inc_lock);
0098     ret = time_after(journal->j_trans_id, ci->ci_last_trans);
0099     spin_unlock(&trans_inc_lock);
0100     return ret;
0101 }
0102 
0103 /* convenience function to check if an object backed by struct
0104  * ocfs2_caching_info  is still new (has never hit disk) Will do you a
0105  * favor and set created_trans = 0 when you've
0106  * been checkpointed.  returns '1' if the ci is still new. */
0107 static inline int ocfs2_ci_is_new(struct ocfs2_caching_info *ci)
0108 {
0109     int ret;
0110     struct ocfs2_journal *journal =
0111         OCFS2_SB(ocfs2_metadata_cache_get_super(ci))->journal;
0112 
0113     spin_lock(&trans_inc_lock);
0114     ret = !(time_after(journal->j_trans_id, ci->ci_created_trans));
0115     if (!ret)
0116         ci->ci_created_trans = 0;
0117     spin_unlock(&trans_inc_lock);
0118     return ret;
0119 }
0120 
0121 /* Wrapper for inodes so we can check system files */
0122 static inline int ocfs2_inode_is_new(struct inode *inode)
0123 {
0124     /* System files are never "new" as they're written out by
0125      * mkfs. This helps us early during mount, before we have the
0126      * journal open and j_trans_id could be junk. */
0127     if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
0128         return 0;
0129 
0130     return ocfs2_ci_is_new(INODE_CACHE(inode));
0131 }
0132 
0133 static inline void ocfs2_ci_set_new(struct ocfs2_super *osb,
0134                     struct ocfs2_caching_info *ci)
0135 {
0136     spin_lock(&trans_inc_lock);
0137     ci->ci_created_trans = osb->journal->j_trans_id;
0138     spin_unlock(&trans_inc_lock);
0139 }
0140 
0141 /* Exported only for the journal struct init code in super.c. Do not call. */
0142 void ocfs2_orphan_scan_init(struct ocfs2_super *osb);
0143 void ocfs2_orphan_scan_start(struct ocfs2_super *osb);
0144 void ocfs2_orphan_scan_stop(struct ocfs2_super *osb);
0145 
0146 void ocfs2_complete_recovery(struct work_struct *work);
0147 void ocfs2_wait_for_recovery(struct ocfs2_super *osb);
0148 
0149 int ocfs2_recovery_init(struct ocfs2_super *osb);
0150 void ocfs2_recovery_exit(struct ocfs2_super *osb);
0151 
0152 int ocfs2_compute_replay_slots(struct ocfs2_super *osb);
0153 /*
0154  *  Journal Control:
0155  *  Initialize, Load, Shutdown, Wipe a journal.
0156  *
0157  *  ocfs2_journal_alloc    - Initialize skeleton for journal structure.
0158  *  ocfs2_journal_init     - Initialize journal structures in the OSB.
0159  *  ocfs2_journal_load     - Load the given journal off disk. Replay it if
0160  *                          there's transactions still in there.
0161  *  ocfs2_journal_shutdown - Shutdown a journal, this will flush all
0162  *                          uncommitted, uncheckpointed transactions.
0163  *  ocfs2_journal_wipe     - Wipe transactions from a journal. Optionally
0164  *                          zero out each block.
0165  *  ocfs2_recovery_thread  - Perform recovery on a node. osb is our own osb.
0166  *  ocfs2_mark_dead_nodes - Start recovery on nodes we won't get a heartbeat
0167  *                          event on.
0168  *  ocfs2_start_checkpoint - Kick the commit thread to do a checkpoint.
0169  */
0170 void   ocfs2_set_journal_params(struct ocfs2_super *osb);
0171 int    ocfs2_journal_alloc(struct ocfs2_super *osb);
0172 int    ocfs2_journal_init(struct ocfs2_super *osb, int *dirty);
0173 void   ocfs2_journal_shutdown(struct ocfs2_super *osb);
0174 int    ocfs2_journal_wipe(struct ocfs2_journal *journal,
0175               int full);
0176 int    ocfs2_journal_load(struct ocfs2_journal *journal, int local,
0177               int replayed);
0178 int    ocfs2_check_journals_nolocks(struct ocfs2_super *osb);
0179 void   ocfs2_recovery_thread(struct ocfs2_super *osb,
0180                  int node_num);
0181 int    ocfs2_mark_dead_nodes(struct ocfs2_super *osb);
0182 void   ocfs2_complete_mount_recovery(struct ocfs2_super *osb);
0183 void ocfs2_complete_quota_recovery(struct ocfs2_super *osb);
0184 
0185 static inline void ocfs2_start_checkpoint(struct ocfs2_super *osb)
0186 {
0187     wake_up(&osb->checkpoint_event);
0188 }
0189 
0190 static inline void ocfs2_checkpoint_inode(struct inode *inode)
0191 {
0192     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0193 
0194     if (ocfs2_mount_local(osb))
0195         return;
0196 
0197     if (!ocfs2_ci_fully_checkpointed(INODE_CACHE(inode))) {
0198         /* WARNING: This only kicks off a single
0199          * checkpoint. If someone races you and adds more
0200          * metadata to the journal, you won't know, and will
0201          * wind up waiting *a lot* longer than necessary. Right
0202          * now we only use this in clear_inode so that's
0203          * OK. */
0204         ocfs2_start_checkpoint(osb);
0205 
0206         wait_event(osb->journal->j_checkpointed,
0207                ocfs2_ci_fully_checkpointed(INODE_CACHE(inode)));
0208     }
0209 }
0210 
0211 /*
0212  *  Transaction Handling:
0213  *  Manage the lifetime of a transaction handle.
0214  *
0215  *  ocfs2_start_trans      - Begin a transaction. Give it an upper estimate of
0216  *                          the number of blocks that will be changed during
0217  *                          this handle.
0218  *  ocfs2_commit_trans - Complete a handle. It might return -EIO if
0219  *                       the journal was aborted. The majority of paths don't
0220  *                       check the return value as an error there comes too
0221  *                       late to do anything (and will be picked up in a
0222  *                       later transaction).
0223  *  ocfs2_extend_trans     - Extend a handle by nblocks credits. This may
0224  *                          commit the handle to disk in the process, but will
0225  *                          not release any locks taken during the transaction.
0226  *  ocfs2_journal_access* - Notify the handle that we want to journal this
0227  *                          buffer. Will have to call ocfs2_journal_dirty once
0228  *                          we've actually dirtied it. Type is one of . or .
0229  *                          Always call the specific flavor of
0230  *                          ocfs2_journal_access_*() unless you intend to
0231  *                          manage the checksum by hand.
0232  *  ocfs2_journal_dirty    - Mark a journalled buffer as having dirty data.
0233  *  ocfs2_jbd2_inode_add_write  - Mark an inode with range so that its data goes
0234  *                                out before the current handle commits.
0235  */
0236 
0237 /* You must always start_trans with a number of buffs > 0, but it's
0238  * perfectly legal to go through an entire transaction without having
0239  * dirtied any buffers. */
0240 handle_t            *ocfs2_start_trans(struct ocfs2_super *osb,
0241                            int max_buffs);
0242 int              ocfs2_commit_trans(struct ocfs2_super *osb,
0243                         handle_t *handle);
0244 int              ocfs2_extend_trans(handle_t *handle, int nblocks);
0245 int              ocfs2_allocate_extend_trans(handle_t *handle,
0246                         int thresh);
0247 
0248 /*
0249  * Define an arbitrary limit for the amount of data we will anticipate
0250  * writing to any given transaction.  For unbounded transactions such as
0251  * fallocate(2) we can write more than this, but we always
0252  * start off at the maximum transaction size and grow the transaction
0253  * optimistically as we go.
0254  */
0255 #define OCFS2_MAX_TRANS_DATA    64U
0256 
0257 /*
0258  * Create access is for when we get a newly created buffer and we're
0259  * not gonna read it off disk, but rather fill it ourselves.  Right
0260  * now, we don't do anything special with this (it turns into a write
0261  * request), but this is a good placeholder in case we do...
0262  *
0263  * Write access is for when we read a block off disk and are going to
0264  * modify it. This way the journalling layer knows it may need to make
0265  * a copy of that block (if it's part of another, uncommitted
0266  * transaction) before we do so.
0267  */
0268 #define OCFS2_JOURNAL_ACCESS_CREATE 0
0269 #define OCFS2_JOURNAL_ACCESS_WRITE  1
0270 #define OCFS2_JOURNAL_ACCESS_UNDO   2
0271 
0272 
0273 /* ocfs2_inode */
0274 int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
0275                 struct buffer_head *bh, int type);
0276 /* ocfs2_extent_block */
0277 int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
0278                 struct buffer_head *bh, int type);
0279 /* ocfs2_refcount_block */
0280 int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
0281                 struct buffer_head *bh, int type);
0282 /* ocfs2_group_desc */
0283 int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
0284                 struct buffer_head *bh, int type);
0285 /* ocfs2_xattr_block */
0286 int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
0287                 struct buffer_head *bh, int type);
0288 /* quota blocks */
0289 int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
0290                 struct buffer_head *bh, int type);
0291 /* dirblock */
0292 int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
0293                 struct buffer_head *bh, int type);
0294 /* ocfs2_dx_root_block */
0295 int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
0296                 struct buffer_head *bh, int type);
0297 /* ocfs2_dx_leaf */
0298 int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
0299                 struct buffer_head *bh, int type);
0300 /* Anything that has no ecc */
0301 int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
0302              struct buffer_head *bh, int type);
0303 
0304 /*
0305  * A word about the journal_access/journal_dirty "dance". It is
0306  * entirely legal to journal_access a buffer more than once (as long
0307  * as the access type is the same -- I'm not sure what will happen if
0308  * access type is different but this should never happen anyway) It is
0309  * also legal to journal_dirty a buffer more than once. In fact, you
0310  * can even journal_access a buffer after you've done a
0311  * journal_access/journal_dirty pair. The only thing you cannot do
0312  * however, is journal_dirty a buffer which you haven't yet passed to
0313  * journal_access at least once.
0314  *
0315  * That said, 99% of the time this doesn't matter and this is what the
0316  * path looks like:
0317  *
0318  *  <read a bh>
0319  *  ocfs2_journal_access(handle, bh,    OCFS2_JOURNAL_ACCESS_WRITE);
0320  *  <modify the bh>
0321  *  ocfs2_journal_dirty(handle, bh);
0322  */
0323 void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh);
0324 
0325 /*
0326  *  Credit Macros:
0327  *  Convenience macros to calculate number of credits needed.
0328  *
0329  *  For convenience sake, I have a set of macros here which calculate
0330  *  the *maximum* number of sectors which will be changed for various
0331  *  metadata updates.
0332  */
0333 
0334 /* simple file updates like chmod, etc. */
0335 #define OCFS2_INODE_UPDATE_CREDITS 1
0336 
0337 /* extended attribute block update */
0338 #define OCFS2_XATTR_BLOCK_UPDATE_CREDITS 1
0339 
0340 /* Update of a single quota block */
0341 #define OCFS2_QUOTA_BLOCK_UPDATE_CREDITS 1
0342 
0343 /* global quotafile inode update, data block */
0344 #define OCFS2_QINFO_WRITE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + \
0345                    OCFS2_QUOTA_BLOCK_UPDATE_CREDITS)
0346 
0347 #define OCFS2_LOCAL_QINFO_WRITE_CREDITS OCFS2_QUOTA_BLOCK_UPDATE_CREDITS
0348 /*
0349  * The two writes below can accidentally see global info dirty due
0350  * to set_info() quotactl so make them prepared for the writes.
0351  */
0352 /* quota data block, global info */
0353 /* Write to local quota file */
0354 #define OCFS2_QWRITE_CREDITS (OCFS2_QINFO_WRITE_CREDITS + \
0355                   OCFS2_QUOTA_BLOCK_UPDATE_CREDITS)
0356 
0357 /* global quota data block, local quota data block, global quota inode,
0358  * global quota info */
0359 #define OCFS2_QSYNC_CREDITS (OCFS2_QINFO_WRITE_CREDITS + \
0360                  2 * OCFS2_QUOTA_BLOCK_UPDATE_CREDITS)
0361 
0362 static inline int ocfs2_quota_trans_credits(struct super_block *sb)
0363 {
0364     int credits = 0;
0365 
0366     if (OCFS2_HAS_RO_COMPAT_FEATURE(sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA))
0367         credits += OCFS2_QWRITE_CREDITS;
0368     if (OCFS2_HAS_RO_COMPAT_FEATURE(sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA))
0369         credits += OCFS2_QWRITE_CREDITS;
0370     return credits;
0371 }
0372 
0373 /* group extend. inode update and last group update. */
0374 #define OCFS2_GROUP_EXTEND_CREDITS  (OCFS2_INODE_UPDATE_CREDITS + 1)
0375 
0376 /* group add. inode update and the new group update. */
0377 #define OCFS2_GROUP_ADD_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
0378 
0379 /* get one bit out of a suballocator: dinode + group descriptor +
0380  * prev. group desc. if we relink. */
0381 #define OCFS2_SUBALLOC_ALLOC (3)
0382 
0383 static inline int ocfs2_inline_to_extents_credits(struct super_block *sb)
0384 {
0385     return OCFS2_SUBALLOC_ALLOC + OCFS2_INODE_UPDATE_CREDITS +
0386            ocfs2_quota_trans_credits(sb);
0387 }
0388 
0389 /* dinode + group descriptor update. We don't relink on free yet. */
0390 #define OCFS2_SUBALLOC_FREE  (2)
0391 
0392 #define OCFS2_TRUNCATE_LOG_UPDATE OCFS2_INODE_UPDATE_CREDITS
0393 #define OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC (OCFS2_SUBALLOC_FREE             \
0394                      + OCFS2_TRUNCATE_LOG_UPDATE)
0395 
0396 static inline int ocfs2_remove_extent_credits(struct super_block *sb)
0397 {
0398     return OCFS2_TRUNCATE_LOG_UPDATE + OCFS2_INODE_UPDATE_CREDITS +
0399            ocfs2_quota_trans_credits(sb);
0400 }
0401 
0402 /* data block for new dir/symlink, allocation of directory block, dx_root
0403  * update for free list */
0404 #define OCFS2_DIR_LINK_ADDITIONAL_CREDITS (1 + OCFS2_SUBALLOC_ALLOC + 1)
0405 
0406 static inline int ocfs2_add_dir_index_credits(struct super_block *sb)
0407 {
0408     /* 1 block for index, 2 allocs (data, metadata), 1 clusters
0409      * worth of blocks for initial extent. */
0410     return 1 + 2 * OCFS2_SUBALLOC_ALLOC +
0411         ocfs2_clusters_to_blocks(sb, 1);
0412 }
0413 
0414 /* parent fe, parent block, new file entry, index leaf, inode alloc fe, inode
0415  * alloc group descriptor + mkdir/symlink blocks + dir blocks + xattr
0416  * blocks + quota update */
0417 static inline int ocfs2_mknod_credits(struct super_block *sb, int is_dir,
0418                       int xattr_credits)
0419 {
0420     int dir_credits = OCFS2_DIR_LINK_ADDITIONAL_CREDITS;
0421 
0422     if (is_dir)
0423         dir_credits += ocfs2_add_dir_index_credits(sb);
0424 
0425     return 4 + OCFS2_SUBALLOC_ALLOC + dir_credits + xattr_credits +
0426            ocfs2_quota_trans_credits(sb);
0427 }
0428 
0429 /* local alloc metadata change + main bitmap updates */
0430 #define OCFS2_WINDOW_MOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS                 \
0431                   + OCFS2_SUBALLOC_ALLOC + OCFS2_SUBALLOC_FREE)
0432 
0433 /* used when we don't need an allocation change for a dir extend. One
0434  * for the dinode, one for the new block. */
0435 #define OCFS2_SIMPLE_DIR_EXTEND_CREDITS (2)
0436 
0437 /* file update (nlink, etc) + directory mtime/ctime + dir entry block + quota
0438  * update on dir + index leaf + dx root update for free list +
0439  * previous dirblock update in the free list */
0440 static inline int ocfs2_link_credits(struct super_block *sb)
0441 {
0442     return 2 * OCFS2_INODE_UPDATE_CREDITS + 4 +
0443            ocfs2_quota_trans_credits(sb);
0444 }
0445 
0446 /* inode + dir inode (if we unlink a dir), + dir entry block + orphan
0447  * dir inode link + dir inode index leaf + dir index root */
0448 static inline int ocfs2_unlink_credits(struct super_block *sb)
0449 {
0450     /* The quota update from ocfs2_link_credits is unused here... */
0451     return 2 * OCFS2_INODE_UPDATE_CREDITS + 3 + ocfs2_link_credits(sb);
0452 }
0453 
0454 /* dinode + orphan dir dinode + inode alloc dinode + orphan dir entry +
0455  * inode alloc group descriptor + orphan dir index root +
0456  * orphan dir index leaf */
0457 #define OCFS2_DELETE_INODE_CREDITS (3 * OCFS2_INODE_UPDATE_CREDITS + 4)
0458 
0459 /* dinode + orphan dir dinode + extent tree leaf block + orphan dir entry +
0460  * orphan dir index root + orphan dir index leaf */
0461 #define OCFS2_INODE_ADD_TO_ORPHAN_CREDITS  (2 * OCFS2_INODE_UPDATE_CREDITS + 4)
0462 #define OCFS2_INODE_DEL_FROM_ORPHAN_CREDITS  OCFS2_INODE_ADD_TO_ORPHAN_CREDITS
0463 
0464 /* dinode update, old dir dinode update, new dir dinode update, old
0465  * dir dir entry, new dir dir entry, dir entry update for renaming
0466  * directory + target unlink + 3 x dir index leaves */
0467 static inline int ocfs2_rename_credits(struct super_block *sb)
0468 {
0469     return 3 * OCFS2_INODE_UPDATE_CREDITS + 6 + ocfs2_unlink_credits(sb);
0470 }
0471 
0472 /* global bitmap dinode, group desc., relinked group,
0473  * suballocator dinode, group desc., relinked group,
0474  * dinode, xattr block */
0475 #define OCFS2_XATTR_BLOCK_CREATE_CREDITS (OCFS2_SUBALLOC_ALLOC * 2 + \
0476                       + OCFS2_INODE_UPDATE_CREDITS \
0477                       + OCFS2_XATTR_BLOCK_UPDATE_CREDITS)
0478 
0479 /* inode update, removal of dx root block from allocator */
0480 #define OCFS2_DX_ROOT_REMOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS +  \
0481                       OCFS2_SUBALLOC_FREE)
0482 
0483 static inline int ocfs2_calc_dxi_expand_credits(struct super_block *sb)
0484 {
0485     int credits = 1 + OCFS2_SUBALLOC_ALLOC;
0486 
0487     credits += ocfs2_clusters_to_blocks(sb, 1);
0488     credits += ocfs2_quota_trans_credits(sb);
0489 
0490     return credits;
0491 }
0492 
0493 /* inode update, new refcount block and its allocation credits. */
0494 #define OCFS2_REFCOUNT_TREE_CREATE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1 \
0495                         + OCFS2_SUBALLOC_ALLOC)
0496 
0497 /* inode and the refcount block update. */
0498 #define OCFS2_REFCOUNT_TREE_SET_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
0499 
0500 /*
0501  * inode and the refcount block update.
0502  * It doesn't include the credits for sub alloc change.
0503  * So if we need to free the bit, OCFS2_SUBALLOC_FREE needs to be added.
0504  */
0505 #define OCFS2_REFCOUNT_TREE_REMOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
0506 
0507 /* 2 metadata alloc, 2 new blocks and root refcount block */
0508 #define OCFS2_EXPAND_REFCOUNT_TREE_CREDITS (OCFS2_SUBALLOC_ALLOC * 2 + 3)
0509 
0510 /*
0511  * Please note that the caller must make sure that root_el is the root
0512  * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
0513  * the result may be wrong.
0514  */
0515 static inline int ocfs2_calc_extend_credits(struct super_block *sb,
0516                         struct ocfs2_extent_list *root_el)
0517 {
0518     int bitmap_blocks, sysfile_bitmap_blocks, extent_blocks;
0519 
0520     /* bitmap dinode, group desc. + relinked group. */
0521     bitmap_blocks = OCFS2_SUBALLOC_ALLOC;
0522 
0523     /* we might need to shift tree depth so lets assume an
0524      * absolute worst case of complete fragmentation.  Even with
0525      * that, we only need one update for the dinode, and then
0526      * however many metadata chunks needed * a remaining suballoc
0527      * alloc. */
0528     sysfile_bitmap_blocks = 1 +
0529         (OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(root_el);
0530 
0531     /* this does not include *new* metadata blocks, which are
0532      * accounted for in sysfile_bitmap_blocks. root_el +
0533      * prev. last_eb_blk + blocks along edge of tree.
0534      * calc_symlink_credits passes because we just need 1
0535      * credit for the dinode there. */
0536     extent_blocks = 1 + 1 + le16_to_cpu(root_el->l_tree_depth);
0537 
0538     return bitmap_blocks + sysfile_bitmap_blocks + extent_blocks +
0539            ocfs2_quota_trans_credits(sb);
0540 }
0541 
0542 static inline int ocfs2_calc_symlink_credits(struct super_block *sb)
0543 {
0544     int blocks = ocfs2_mknod_credits(sb, 0, 0);
0545 
0546     /* links can be longer than one block so we may update many
0547      * within our single allocated extent. */
0548     blocks += ocfs2_clusters_to_blocks(sb, 1);
0549 
0550     return blocks + ocfs2_quota_trans_credits(sb);
0551 }
0552 
0553 static inline int ocfs2_calc_group_alloc_credits(struct super_block *sb,
0554                          unsigned int cpg)
0555 {
0556     int blocks;
0557     int bitmap_blocks = OCFS2_SUBALLOC_ALLOC + 1;
0558     /* parent inode update + new block group header + bitmap inode update
0559        + bitmap blocks affected */
0560     blocks = 1 + 1 + 1 + bitmap_blocks;
0561     return blocks;
0562 }
0563 
0564 /*
0565  * Allocating a discontiguous block group requires the credits from
0566  * ocfs2_calc_group_alloc_credits() as well as enough credits to fill
0567  * the group descriptor's extent list.  The caller already has started
0568  * the transaction with ocfs2_calc_group_alloc_credits().  They extend
0569  * it with these credits.
0570  */
0571 static inline int ocfs2_calc_bg_discontig_credits(struct super_block *sb)
0572 {
0573     return ocfs2_extent_recs_per_gd(sb);
0574 }
0575 
0576 static inline int ocfs2_jbd2_inode_add_write(handle_t *handle, struct inode *inode,
0577                          loff_t start_byte, loff_t length)
0578 {
0579     return jbd2_journal_inode_ranged_write(handle,
0580                            &OCFS2_I(inode)->ip_jinode,
0581                            start_byte, length);
0582 }
0583 
0584 static inline int ocfs2_begin_ordered_truncate(struct inode *inode,
0585                            loff_t new_size)
0586 {
0587     return jbd2_journal_begin_ordered_truncate(
0588                 OCFS2_SB(inode->i_sb)->journal->j_journal,
0589                 &OCFS2_I(inode)->ip_jinode,
0590                 new_size);
0591 }
0592 
0593 static inline void ocfs2_update_inode_fsync_trans(handle_t *handle,
0594                           struct inode *inode,
0595                           int datasync)
0596 {
0597     struct ocfs2_inode_info *oi = OCFS2_I(inode);
0598 
0599     if (!is_handle_aborted(handle)) {
0600         oi->i_sync_tid = handle->h_transaction->t_tid;
0601         if (datasync)
0602             oi->i_datasync_tid = handle->h_transaction->t_tid;
0603     }
0604 }
0605 
0606 #endif /* OCFS2_JOURNAL_H */