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