1b3b94faaSDavid Teigland /* 2b3b94faaSDavid Teigland * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 3b3b94faaSDavid Teigland * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 4b3b94faaSDavid Teigland * 5b3b94faaSDavid Teigland * This copyrighted material is made available to anyone wishing to use, 6b3b94faaSDavid Teigland * modify, copy, or redistribute it subject to the terms and conditions 7b3b94faaSDavid Teigland * of the GNU General Public License v.2. 8b3b94faaSDavid Teigland */ 9b3b94faaSDavid Teigland 10b3b94faaSDavid Teigland #include <linux/sched.h> 11b3b94faaSDavid Teigland #include <linux/slab.h> 12b3b94faaSDavid Teigland #include <linux/spinlock.h> 13b3b94faaSDavid Teigland #include <linux/completion.h> 14b3b94faaSDavid Teigland #include <linux/buffer_head.h> 155c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h> 1671b86f56SSteven Whitehouse #include <linux/crc32.h> 17b3b94faaSDavid Teigland #include <asm/semaphore.h> 18b3b94faaSDavid Teigland 19b3b94faaSDavid Teigland #include "gfs2.h" 205c676f6dSSteven Whitehouse #include "lm_interface.h" 215c676f6dSSteven Whitehouse #include "incore.h" 22b3b94faaSDavid Teigland #include "bmap.h" 23b3b94faaSDavid Teigland #include "glock.h" 24b3b94faaSDavid Teigland #include "log.h" 25b3b94faaSDavid Teigland #include "lops.h" 26b3b94faaSDavid Teigland #include "meta_io.h" 275c676f6dSSteven Whitehouse #include "util.h" 2871b86f56SSteven Whitehouse #include "dir.h" 29b3b94faaSDavid Teigland 30b3b94faaSDavid Teigland #define PULL 1 31b3b94faaSDavid Teigland 32b3b94faaSDavid Teigland /** 33b3b94faaSDavid Teigland * gfs2_struct2blk - compute stuff 34b3b94faaSDavid Teigland * @sdp: the filesystem 35b3b94faaSDavid Teigland * @nstruct: the number of structures 36b3b94faaSDavid Teigland * @ssize: the size of the structures 37b3b94faaSDavid Teigland * 38b3b94faaSDavid Teigland * Compute the number of log descriptor blocks needed to hold a certain number 39b3b94faaSDavid Teigland * of structures of a certain size. 40b3b94faaSDavid Teigland * 41b3b94faaSDavid Teigland * Returns: the number of blocks needed (minimum is always 1) 42b3b94faaSDavid Teigland */ 43b3b94faaSDavid Teigland 44b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct, 45b3b94faaSDavid Teigland unsigned int ssize) 46b3b94faaSDavid Teigland { 47b3b94faaSDavid Teigland unsigned int blks; 48b3b94faaSDavid Teigland unsigned int first, second; 49b3b94faaSDavid Teigland 50b3b94faaSDavid Teigland blks = 1; 51568f4c96SSteven Whitehouse first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / 52568f4c96SSteven Whitehouse ssize; 53b3b94faaSDavid Teigland 54b3b94faaSDavid Teigland if (nstruct > first) { 55568f4c96SSteven Whitehouse second = (sdp->sd_sb.sb_bsize - 56568f4c96SSteven Whitehouse sizeof(struct gfs2_meta_header)) / ssize; 575c676f6dSSteven Whitehouse blks += DIV_ROUND_UP(nstruct - first, second); 58b3b94faaSDavid Teigland } 59b3b94faaSDavid Teigland 60b3b94faaSDavid Teigland return blks; 61b3b94faaSDavid Teigland } 62b3b94faaSDavid Teigland 63b3b94faaSDavid Teigland void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags) 64b3b94faaSDavid Teigland { 65b3b94faaSDavid Teigland struct list_head *head = &sdp->sd_ail1_list; 66b3b94faaSDavid Teigland uint64_t sync_gen; 67b3b94faaSDavid Teigland struct list_head *first, *tmp; 68b3b94faaSDavid Teigland struct gfs2_ail *first_ai, *ai; 69b3b94faaSDavid Teigland 70b3b94faaSDavid Teigland gfs2_log_lock(sdp); 71b3b94faaSDavid Teigland if (list_empty(head)) { 72b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 73b3b94faaSDavid Teigland return; 74b3b94faaSDavid Teigland } 75b3b94faaSDavid Teigland sync_gen = sdp->sd_ail_sync_gen++; 76b3b94faaSDavid Teigland 77b3b94faaSDavid Teigland first = head->prev; 78b3b94faaSDavid Teigland first_ai = list_entry(first, struct gfs2_ail, ai_list); 79b3b94faaSDavid Teigland first_ai->ai_sync_gen = sync_gen; 80b3b94faaSDavid Teigland gfs2_ail1_start_one(sdp, first_ai); 81b3b94faaSDavid Teigland 82b3b94faaSDavid Teigland if (flags & DIO_ALL) 83b3b94faaSDavid Teigland first = NULL; 84b3b94faaSDavid Teigland 85b3b94faaSDavid Teigland for (;;) { 86484adff8SSteven Whitehouse if (first && (head->prev != first || 87b3b94faaSDavid Teigland gfs2_ail1_empty_one(sdp, first_ai, 0))) 88b3b94faaSDavid Teigland break; 89b3b94faaSDavid Teigland 90b3b94faaSDavid Teigland for (tmp = head->prev; tmp != head; tmp = tmp->prev) { 91b3b94faaSDavid Teigland ai = list_entry(tmp, struct gfs2_ail, ai_list); 92b3b94faaSDavid Teigland if (ai->ai_sync_gen >= sync_gen) 93b3b94faaSDavid Teigland continue; 94b3b94faaSDavid Teigland ai->ai_sync_gen = sync_gen; 95b3b94faaSDavid Teigland gfs2_ail1_start_one(sdp, ai); 96b3b94faaSDavid Teigland break; 97b3b94faaSDavid Teigland } 98b3b94faaSDavid Teigland 99b3b94faaSDavid Teigland if (tmp == head) 100b3b94faaSDavid Teigland break; 101b3b94faaSDavid Teigland } 102b3b94faaSDavid Teigland 103b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 104b3b94faaSDavid Teigland } 105b3b94faaSDavid Teigland 106b3b94faaSDavid Teigland int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags) 107b3b94faaSDavid Teigland { 108b3b94faaSDavid Teigland struct gfs2_ail *ai, *s; 109b3b94faaSDavid Teigland int ret; 110b3b94faaSDavid Teigland 111b3b94faaSDavid Teigland gfs2_log_lock(sdp); 112b3b94faaSDavid Teigland 113b3b94faaSDavid Teigland list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) { 114b3b94faaSDavid Teigland if (gfs2_ail1_empty_one(sdp, ai, flags)) 115b3b94faaSDavid Teigland list_move(&ai->ai_list, &sdp->sd_ail2_list); 116b3b94faaSDavid Teigland else if (!(flags & DIO_ALL)) 117b3b94faaSDavid Teigland break; 118b3b94faaSDavid Teigland } 119b3b94faaSDavid Teigland 120b3b94faaSDavid Teigland ret = list_empty(&sdp->sd_ail1_list); 121b3b94faaSDavid Teigland 122b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 123b3b94faaSDavid Teigland 124b3b94faaSDavid Teigland return ret; 125b3b94faaSDavid Teigland } 126b3b94faaSDavid Teigland 127b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) 128b3b94faaSDavid Teigland { 129b3b94faaSDavid Teigland struct gfs2_ail *ai, *safe; 130b3b94faaSDavid Teigland unsigned int old_tail = sdp->sd_log_tail; 131b3b94faaSDavid Teigland int wrap = (new_tail < old_tail); 132b3b94faaSDavid Teigland int a, b, rm; 133b3b94faaSDavid Teigland 134b3b94faaSDavid Teigland gfs2_log_lock(sdp); 135b3b94faaSDavid Teigland 136b3b94faaSDavid Teigland list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) { 137b3b94faaSDavid Teigland a = (old_tail <= ai->ai_first); 138b3b94faaSDavid Teigland b = (ai->ai_first < new_tail); 139b3b94faaSDavid Teigland rm = (wrap) ? (a || b) : (a && b); 140b3b94faaSDavid Teigland if (!rm) 141b3b94faaSDavid Teigland continue; 142b3b94faaSDavid Teigland 143b3b94faaSDavid Teigland gfs2_ail2_empty_one(sdp, ai); 144b3b94faaSDavid Teigland list_del(&ai->ai_list); 145b3b94faaSDavid Teigland gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list)); 146b3b94faaSDavid Teigland gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list)); 147b3b94faaSDavid Teigland kfree(ai); 148b3b94faaSDavid Teigland } 149b3b94faaSDavid Teigland 150b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 151b3b94faaSDavid Teigland } 152b3b94faaSDavid Teigland 153b3b94faaSDavid Teigland /** 154b3b94faaSDavid Teigland * gfs2_log_reserve - Make a log reservation 155b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 156b3b94faaSDavid Teigland * @blks: The number of blocks to reserve 157b3b94faaSDavid Teigland * 158b3b94faaSDavid Teigland * Returns: errno 159b3b94faaSDavid Teigland */ 160b3b94faaSDavid Teigland 161b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) 162b3b94faaSDavid Teigland { 163b3b94faaSDavid Teigland unsigned int try = 0; 164b3b94faaSDavid Teigland 165b3b94faaSDavid Teigland if (gfs2_assert_warn(sdp, blks) || 166b3b94faaSDavid Teigland gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) 167b3b94faaSDavid Teigland return -EINVAL; 168b3b94faaSDavid Teigland 16971b86f56SSteven Whitehouse mutex_lock(&sdp->sd_log_reserve_mutex); 170b3b94faaSDavid Teigland gfs2_log_lock(sdp); 171484adff8SSteven Whitehouse while(sdp->sd_log_blks_free <= blks) { 172b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 173b3b94faaSDavid Teigland gfs2_ail1_empty(sdp, 0); 174b3b94faaSDavid Teigland gfs2_log_flush(sdp); 175b3b94faaSDavid Teigland 176b3b94faaSDavid Teigland if (try++) 177b3b94faaSDavid Teigland gfs2_ail1_start(sdp, 0); 178484adff8SSteven Whitehouse gfs2_log_lock(sdp); 179b3b94faaSDavid Teigland } 180484adff8SSteven Whitehouse sdp->sd_log_blks_free -= blks; 181484adff8SSteven Whitehouse gfs2_log_unlock(sdp); 182484adff8SSteven Whitehouse mutex_unlock(&sdp->sd_log_reserve_mutex); 183484adff8SSteven Whitehouse 184484adff8SSteven Whitehouse down_read(&sdp->sd_log_flush_lock); 185b3b94faaSDavid Teigland 186b3b94faaSDavid Teigland return 0; 187b3b94faaSDavid Teigland } 188b3b94faaSDavid Teigland 189b3b94faaSDavid Teigland /** 190b3b94faaSDavid Teigland * gfs2_log_release - Release a given number of log blocks 191b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 192b3b94faaSDavid Teigland * @blks: The number of blocks 193b3b94faaSDavid Teigland * 194b3b94faaSDavid Teigland */ 195b3b94faaSDavid Teigland 196b3b94faaSDavid Teigland void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks) 197b3b94faaSDavid Teigland { 198484adff8SSteven Whitehouse up_read(&sdp->sd_log_flush_lock); 199b3b94faaSDavid Teigland 200b3b94faaSDavid Teigland gfs2_log_lock(sdp); 201b3b94faaSDavid Teigland sdp->sd_log_blks_free += blks; 202b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 203b3b94faaSDavid Teigland sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks); 204b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 205b3b94faaSDavid Teigland } 206b3b94faaSDavid Teigland 207b3b94faaSDavid Teigland static uint64_t log_bmap(struct gfs2_sbd *sdp, unsigned int lbn) 208b3b94faaSDavid Teigland { 209b3b94faaSDavid Teigland int new = 0; 210b3b94faaSDavid Teigland uint64_t dbn; 211b3b94faaSDavid Teigland int error; 212b3b94faaSDavid Teigland 2135c676f6dSSteven Whitehouse error = gfs2_block_map(sdp->sd_jdesc->jd_inode->u.generic_ip, 214568f4c96SSteven Whitehouse lbn, &new, &dbn, NULL); 215b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !error && dbn); 216b3b94faaSDavid Teigland 217b3b94faaSDavid Teigland return dbn; 218b3b94faaSDavid Teigland } 219b3b94faaSDavid Teigland 220b3b94faaSDavid Teigland /** 221b3b94faaSDavid Teigland * log_distance - Compute distance between two journal blocks 222b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 223b3b94faaSDavid Teigland * @newer: The most recent journal block of the pair 224b3b94faaSDavid Teigland * @older: The older journal block of the pair 225b3b94faaSDavid Teigland * 226b3b94faaSDavid Teigland * Compute the distance (in the journal direction) between two 227b3b94faaSDavid Teigland * blocks in the journal 228b3b94faaSDavid Teigland * 229b3b94faaSDavid Teigland * Returns: the distance in blocks 230b3b94faaSDavid Teigland */ 231b3b94faaSDavid Teigland 232b3b94faaSDavid Teigland static inline unsigned int log_distance(struct gfs2_sbd *sdp, 233b3b94faaSDavid Teigland unsigned int newer, 234b3b94faaSDavid Teigland unsigned int older) 235b3b94faaSDavid Teigland { 236b3b94faaSDavid Teigland int dist; 237b3b94faaSDavid Teigland 238b3b94faaSDavid Teigland dist = newer - older; 239b3b94faaSDavid Teigland if (dist < 0) 240b3b94faaSDavid Teigland dist += sdp->sd_jdesc->jd_blocks; 241b3b94faaSDavid Teigland 242b3b94faaSDavid Teigland return dist; 243b3b94faaSDavid Teigland } 244b3b94faaSDavid Teigland 245b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp) 246b3b94faaSDavid Teigland { 247b3b94faaSDavid Teigland struct gfs2_ail *ai; 248b3b94faaSDavid Teigland unsigned int tail; 249b3b94faaSDavid Teigland 250b3b94faaSDavid Teigland gfs2_log_lock(sdp); 251b3b94faaSDavid Teigland 252b3b94faaSDavid Teigland if (list_empty(&sdp->sd_ail1_list)) 253b3b94faaSDavid Teigland tail = sdp->sd_log_head; 254b3b94faaSDavid Teigland else { 255b3b94faaSDavid Teigland ai = list_entry(sdp->sd_ail1_list.prev, 256b3b94faaSDavid Teigland struct gfs2_ail, ai_list); 257b3b94faaSDavid Teigland tail = ai->ai_first; 258b3b94faaSDavid Teigland } 259b3b94faaSDavid Teigland 260b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 261b3b94faaSDavid Teigland 262b3b94faaSDavid Teigland return tail; 263b3b94faaSDavid Teigland } 264b3b94faaSDavid Teigland 265b3b94faaSDavid Teigland static inline void log_incr_head(struct gfs2_sbd *sdp) 266b3b94faaSDavid Teigland { 267b3b94faaSDavid Teigland if (sdp->sd_log_flush_head == sdp->sd_log_tail) 268b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 269b3b94faaSDavid Teigland sdp->sd_log_flush_head == sdp->sd_log_head); 270b3b94faaSDavid Teigland 271b3b94faaSDavid Teigland if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) { 272b3b94faaSDavid Teigland sdp->sd_log_flush_head = 0; 273b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 1; 274b3b94faaSDavid Teigland } 275b3b94faaSDavid Teigland } 276b3b94faaSDavid Teigland 277b3b94faaSDavid Teigland /** 278b3b94faaSDavid Teigland * gfs2_log_get_buf - Get and initialize a buffer to use for log control data 279b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 280b3b94faaSDavid Teigland * 281b3b94faaSDavid Teigland * Returns: the buffer_head 282b3b94faaSDavid Teigland */ 283b3b94faaSDavid Teigland 284b3b94faaSDavid Teigland struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp) 285b3b94faaSDavid Teigland { 286b3b94faaSDavid Teigland uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head); 287b3b94faaSDavid Teigland struct gfs2_log_buf *lb; 288b3b94faaSDavid Teigland struct buffer_head *bh; 289b3b94faaSDavid Teigland 2904f3df041SSteven Whitehouse lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL); 291b3b94faaSDavid Teigland list_add(&lb->lb_list, &sdp->sd_log_flush_list); 292b3b94faaSDavid Teigland 293b3b94faaSDavid Teigland bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno); 294b3b94faaSDavid Teigland lock_buffer(bh); 295b3b94faaSDavid Teigland memset(bh->b_data, 0, bh->b_size); 296b3b94faaSDavid Teigland set_buffer_uptodate(bh); 297b3b94faaSDavid Teigland clear_buffer_dirty(bh); 298b3b94faaSDavid Teigland unlock_buffer(bh); 299b3b94faaSDavid Teigland 300b3b94faaSDavid Teigland log_incr_head(sdp); 301b3b94faaSDavid Teigland 302b3b94faaSDavid Teigland return bh; 303b3b94faaSDavid Teigland } 304b3b94faaSDavid Teigland 305b3b94faaSDavid Teigland /** 306b3b94faaSDavid Teigland * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log 307b3b94faaSDavid Teigland * @sdp: the filesystem 308b3b94faaSDavid Teigland * @data: the data the buffer_head should point to 309b3b94faaSDavid Teigland * 310b3b94faaSDavid Teigland * Returns: the log buffer descriptor 311b3b94faaSDavid Teigland */ 312b3b94faaSDavid Teigland 313b3b94faaSDavid Teigland struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp, 314b3b94faaSDavid Teigland struct buffer_head *real) 315b3b94faaSDavid Teigland { 316b3b94faaSDavid Teigland uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head); 317b3b94faaSDavid Teigland struct gfs2_log_buf *lb; 318b3b94faaSDavid Teigland struct buffer_head *bh; 319b3b94faaSDavid Teigland 3204f3df041SSteven Whitehouse lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL); 321b3b94faaSDavid Teigland list_add(&lb->lb_list, &sdp->sd_log_flush_list); 322b3b94faaSDavid Teigland lb->lb_real = real; 323b3b94faaSDavid Teigland 324b3b94faaSDavid Teigland bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL); 325b3b94faaSDavid Teigland atomic_set(&bh->b_count, 1); 326b3b94faaSDavid Teigland bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate); 32718ec7d5cSSteven Whitehouse set_bh_page(bh, real->b_page, bh_offset(real)); 328b3b94faaSDavid Teigland bh->b_blocknr = blkno; 329b3b94faaSDavid Teigland bh->b_size = sdp->sd_sb.sb_bsize; 330b3b94faaSDavid Teigland bh->b_bdev = sdp->sd_vfs->s_bdev; 331b3b94faaSDavid Teigland 332b3b94faaSDavid Teigland log_incr_head(sdp); 333b3b94faaSDavid Teigland 334b3b94faaSDavid Teigland return bh; 335b3b94faaSDavid Teigland } 336b3b94faaSDavid Teigland 337b3b94faaSDavid Teigland static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull) 338b3b94faaSDavid Teigland { 339b3b94faaSDavid Teigland unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); 340b3b94faaSDavid Teigland 341b3b94faaSDavid Teigland ail2_empty(sdp, new_tail); 342b3b94faaSDavid Teigland 343b3b94faaSDavid Teigland gfs2_log_lock(sdp); 344b3b94faaSDavid Teigland sdp->sd_log_blks_free += dist - ((pull) ? 1 : 0); 345b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 346b3b94faaSDavid Teigland sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks); 347b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 348b3b94faaSDavid Teigland 349b3b94faaSDavid Teigland sdp->sd_log_tail = new_tail; 350b3b94faaSDavid Teigland } 351b3b94faaSDavid Teigland 352b3b94faaSDavid Teigland /** 353b3b94faaSDavid Teigland * log_write_header - Get and initialize a journal header buffer 354b3b94faaSDavid Teigland * @sdp: The GFS2 superblock 355b3b94faaSDavid Teigland * 356b3b94faaSDavid Teigland * Returns: the initialized log buffer descriptor 357b3b94faaSDavid Teigland */ 358b3b94faaSDavid Teigland 359b3b94faaSDavid Teigland static void log_write_header(struct gfs2_sbd *sdp, uint32_t flags, int pull) 360b3b94faaSDavid Teigland { 361b3b94faaSDavid Teigland uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head); 362b3b94faaSDavid Teigland struct buffer_head *bh; 363b3b94faaSDavid Teigland struct gfs2_log_header *lh; 364b3b94faaSDavid Teigland unsigned int tail; 365b3b94faaSDavid Teigland uint32_t hash; 366b3b94faaSDavid Teigland 367b3b94faaSDavid Teigland bh = sb_getblk(sdp->sd_vfs, blkno); 368b3b94faaSDavid Teigland lock_buffer(bh); 369b3b94faaSDavid Teigland memset(bh->b_data, 0, bh->b_size); 370b3b94faaSDavid Teigland set_buffer_uptodate(bh); 371b3b94faaSDavid Teigland clear_buffer_dirty(bh); 372b3b94faaSDavid Teigland unlock_buffer(bh); 373b3b94faaSDavid Teigland 374b3b94faaSDavid Teigland gfs2_ail1_empty(sdp, 0); 375b3b94faaSDavid Teigland tail = current_tail(sdp); 376b3b94faaSDavid Teigland 377b3b94faaSDavid Teigland lh = (struct gfs2_log_header *)bh->b_data; 378b3b94faaSDavid Teigland memset(lh, 0, sizeof(struct gfs2_log_header)); 379b3b94faaSDavid Teigland lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); 380*e3167dedSSteven Whitehouse lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); 381*e3167dedSSteven Whitehouse lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); 382b3b94faaSDavid Teigland lh->lh_sequence = be64_to_cpu(sdp->sd_log_sequence++); 383b3b94faaSDavid Teigland lh->lh_flags = be32_to_cpu(flags); 384b3b94faaSDavid Teigland lh->lh_tail = be32_to_cpu(tail); 385b3b94faaSDavid Teigland lh->lh_blkno = be32_to_cpu(sdp->sd_log_flush_head); 386b3b94faaSDavid Teigland hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header)); 387b3b94faaSDavid Teigland lh->lh_hash = cpu_to_be32(hash); 388b3b94faaSDavid Teigland 389b3b94faaSDavid Teigland set_buffer_dirty(bh); 390b3b94faaSDavid Teigland if (sync_dirty_buffer(bh)) 391b3b94faaSDavid Teigland gfs2_io_error_bh(sdp, bh); 392b3b94faaSDavid Teigland brelse(bh); 393b3b94faaSDavid Teigland 394b3b94faaSDavid Teigland if (sdp->sd_log_tail != tail) 395b3b94faaSDavid Teigland log_pull_tail(sdp, tail, pull); 396b3b94faaSDavid Teigland else 397b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !pull); 398b3b94faaSDavid Teigland 399b3b94faaSDavid Teigland sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); 400b3b94faaSDavid Teigland log_incr_head(sdp); 401b3b94faaSDavid Teigland } 402b3b94faaSDavid Teigland 403b3b94faaSDavid Teigland static void log_flush_commit(struct gfs2_sbd *sdp) 404b3b94faaSDavid Teigland { 405b3b94faaSDavid Teigland struct list_head *head = &sdp->sd_log_flush_list; 406b3b94faaSDavid Teigland struct gfs2_log_buf *lb; 407b3b94faaSDavid Teigland struct buffer_head *bh; 408b3b94faaSDavid Teigland unsigned int d; 409b3b94faaSDavid Teigland 410b3b94faaSDavid Teigland d = log_distance(sdp, sdp->sd_log_flush_head, sdp->sd_log_head); 411b3b94faaSDavid Teigland 412b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, d + 1 == sdp->sd_log_blks_reserved); 413b3b94faaSDavid Teigland 414b3b94faaSDavid Teigland while (!list_empty(head)) { 415b3b94faaSDavid Teigland lb = list_entry(head->next, struct gfs2_log_buf, lb_list); 416b3b94faaSDavid Teigland list_del(&lb->lb_list); 417b3b94faaSDavid Teigland bh = lb->lb_bh; 418b3b94faaSDavid Teigland 419b3b94faaSDavid Teigland wait_on_buffer(bh); 420b3b94faaSDavid Teigland if (!buffer_uptodate(bh)) 421b3b94faaSDavid Teigland gfs2_io_error_bh(sdp, bh); 422b3b94faaSDavid Teigland if (lb->lb_real) { 423b3b94faaSDavid Teigland while (atomic_read(&bh->b_count) != 1) /* Grrrr... */ 424b3b94faaSDavid Teigland schedule(); 425b3b94faaSDavid Teigland free_buffer_head(bh); 426b3b94faaSDavid Teigland } else 427b3b94faaSDavid Teigland brelse(bh); 428b3b94faaSDavid Teigland kfree(lb); 429b3b94faaSDavid Teigland } 430b3b94faaSDavid Teigland 431b3b94faaSDavid Teigland log_write_header(sdp, 0, 0); 432b3b94faaSDavid Teigland } 433b3b94faaSDavid Teigland 434b3b94faaSDavid Teigland /** 435b3b94faaSDavid Teigland * gfs2_log_flush_i - flush incore transaction(s) 436b3b94faaSDavid Teigland * @sdp: the filesystem 437b3b94faaSDavid Teigland * @gl: The glock structure to flush. If NULL, flush the whole incore log 438b3b94faaSDavid Teigland * 439b3b94faaSDavid Teigland */ 440b3b94faaSDavid Teigland 441b3b94faaSDavid Teigland void gfs2_log_flush_i(struct gfs2_sbd *sdp, struct gfs2_glock *gl) 442b3b94faaSDavid Teigland { 443b3b94faaSDavid Teigland struct gfs2_ail *ai; 444b3b94faaSDavid Teigland 4454f3df041SSteven Whitehouse ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL); 446b3b94faaSDavid Teigland INIT_LIST_HEAD(&ai->ai_ail1_list); 447b3b94faaSDavid Teigland INIT_LIST_HEAD(&ai->ai_ail2_list); 448484adff8SSteven Whitehouse 449484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 450f55ab26aSSteven Whitehouse 451f55ab26aSSteven Whitehouse if (gl) { 452f55ab26aSSteven Whitehouse gfs2_log_lock(sdp); 453f55ab26aSSteven Whitehouse if (list_empty(&gl->gl_le.le_list)) { 454f55ab26aSSteven Whitehouse gfs2_log_unlock(sdp); 455484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 456f55ab26aSSteven Whitehouse kfree(ai); 457f55ab26aSSteven Whitehouse return; 458f55ab26aSSteven Whitehouse } 459f55ab26aSSteven Whitehouse gfs2_log_unlock(sdp); 460f55ab26aSSteven Whitehouse } 461f55ab26aSSteven Whitehouse 462b3b94faaSDavid Teigland 463b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 464b3b94faaSDavid Teigland sdp->sd_log_num_buf == sdp->sd_log_commited_buf); 465b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 466b3b94faaSDavid Teigland sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke); 467b3b94faaSDavid Teigland 468b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 469b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 0; 470b3b94faaSDavid Teigland ai->ai_first = sdp->sd_log_flush_head; 471b3b94faaSDavid Teigland 472b3b94faaSDavid Teigland lops_before_commit(sdp); 473b3b94faaSDavid Teigland if (!list_empty(&sdp->sd_log_flush_list)) 474b3b94faaSDavid Teigland log_flush_commit(sdp); 475b3b94faaSDavid Teigland else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle) 476b3b94faaSDavid Teigland log_write_header(sdp, 0, PULL); 477b3b94faaSDavid Teigland lops_after_commit(sdp, ai); 478b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 479b3b94faaSDavid Teigland if (sdp->sd_log_flush_wrapped) 480b3b94faaSDavid Teigland sdp->sd_log_wraps++; 481b3b94faaSDavid Teigland 482b3b94faaSDavid Teigland sdp->sd_log_blks_reserved = 483b3b94faaSDavid Teigland sdp->sd_log_commited_buf = 484b3b94faaSDavid Teigland sdp->sd_log_commited_revoke = 0; 485b3b94faaSDavid Teigland 486b3b94faaSDavid Teigland gfs2_log_lock(sdp); 487b3b94faaSDavid Teigland if (!list_empty(&ai->ai_ail1_list)) { 488b3b94faaSDavid Teigland list_add(&ai->ai_list, &sdp->sd_ail1_list); 489b3b94faaSDavid Teigland ai = NULL; 490b3b94faaSDavid Teigland } 491b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 492b3b94faaSDavid Teigland 493b3b94faaSDavid Teigland sdp->sd_vfs->s_dirt = 0; 494484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 495b3b94faaSDavid Teigland 496b3b94faaSDavid Teigland kfree(ai); 497b3b94faaSDavid Teigland } 498b3b94faaSDavid Teigland 499b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 500b3b94faaSDavid Teigland { 501b3b94faaSDavid Teigland unsigned int reserved = 1; 502b3b94faaSDavid Teigland unsigned int old; 503b3b94faaSDavid Teigland 504b3b94faaSDavid Teigland gfs2_log_lock(sdp); 505b3b94faaSDavid Teigland 506b3b94faaSDavid Teigland sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm; 507b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0); 508b3b94faaSDavid Teigland sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; 509b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0); 510b3b94faaSDavid Teigland 511b3b94faaSDavid Teigland if (sdp->sd_log_commited_buf) 512568f4c96SSteven Whitehouse reserved += 1 + sdp->sd_log_commited_buf + 513568f4c96SSteven Whitehouse sdp->sd_log_commited_buf/503; 514b3b94faaSDavid Teigland if (sdp->sd_log_commited_revoke) 515b3b94faaSDavid Teigland reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke, 516b3b94faaSDavid Teigland sizeof(uint64_t)); 517b3b94faaSDavid Teigland 518b3b94faaSDavid Teigland old = sdp->sd_log_blks_free; 519b3b94faaSDavid Teigland sdp->sd_log_blks_free += tr->tr_reserved - 520b3b94faaSDavid Teigland (reserved - sdp->sd_log_blks_reserved); 521b3b94faaSDavid Teigland 522b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 523b3b94faaSDavid Teigland sdp->sd_log_blks_free >= old); 524b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, 525b3b94faaSDavid Teigland sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks); 526b3b94faaSDavid Teigland 527b3b94faaSDavid Teigland sdp->sd_log_blks_reserved = reserved; 528b3b94faaSDavid Teigland 529b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 530b3b94faaSDavid Teigland } 531b3b94faaSDavid Teigland 532b3b94faaSDavid Teigland /** 533b3b94faaSDavid Teigland * gfs2_log_commit - Commit a transaction to the log 534b3b94faaSDavid Teigland * @sdp: the filesystem 535b3b94faaSDavid Teigland * @tr: the transaction 536b3b94faaSDavid Teigland * 537b3b94faaSDavid Teigland * Returns: errno 538b3b94faaSDavid Teigland */ 539b3b94faaSDavid Teigland 540b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 541b3b94faaSDavid Teigland { 542b3b94faaSDavid Teigland log_refund(sdp, tr); 543b3b94faaSDavid Teigland lops_incore_commit(sdp, tr); 544b3b94faaSDavid Teigland 545b3b94faaSDavid Teigland sdp->sd_vfs->s_dirt = 1; 546484adff8SSteven Whitehouse up_read(&sdp->sd_log_flush_lock); 547b3b94faaSDavid Teigland 548b3b94faaSDavid Teigland gfs2_log_lock(sdp); 549b3b94faaSDavid Teigland if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) { 550b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 551b3b94faaSDavid Teigland gfs2_log_flush(sdp); 552b3b94faaSDavid Teigland } else 553b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 554b3b94faaSDavid Teigland } 555b3b94faaSDavid Teigland 556b3b94faaSDavid Teigland /** 557b3b94faaSDavid Teigland * gfs2_log_shutdown - write a shutdown header into a journal 558b3b94faaSDavid Teigland * @sdp: the filesystem 559b3b94faaSDavid Teigland * 560b3b94faaSDavid Teigland */ 561b3b94faaSDavid Teigland 562b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp) 563b3b94faaSDavid Teigland { 564484adff8SSteven Whitehouse down_write(&sdp->sd_log_flush_lock); 565b3b94faaSDavid Teigland 566b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); 567b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl); 568b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf); 56918ec7d5cSSteven Whitehouse gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata); 570b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); 571b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg); 572b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf); 573b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); 574b3b94faaSDavid Teigland 575b3b94faaSDavid Teigland sdp->sd_log_flush_head = sdp->sd_log_head; 576b3b94faaSDavid Teigland sdp->sd_log_flush_wrapped = 0; 577b3b94faaSDavid Teigland 578b3b94faaSDavid Teigland log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0); 579b3b94faaSDavid Teigland 580b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free == 581b3b94faaSDavid Teigland sdp->sd_jdesc->jd_blocks); 582b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, sdp->sd_log_head == sdp->sd_log_tail); 583b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail2_list)); 584b3b94faaSDavid Teigland 585b3b94faaSDavid Teigland sdp->sd_log_head = sdp->sd_log_flush_head; 586b3b94faaSDavid Teigland if (sdp->sd_log_flush_wrapped) 587b3b94faaSDavid Teigland sdp->sd_log_wraps++; 588b3b94faaSDavid Teigland sdp->sd_log_tail = sdp->sd_log_head; 589b3b94faaSDavid Teigland 590484adff8SSteven Whitehouse up_write(&sdp->sd_log_flush_lock); 591b3b94faaSDavid Teigland } 592b3b94faaSDavid Teigland 593