2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <linux/lm_interface.h>
32 * gfs2_struct2blk - compute stuff
33 * @sdp: the filesystem
34 * @nstruct: the number of structures
35 * @ssize: the size of the structures
37 * Compute the number of log descriptor blocks needed to hold a certain number
38 * of structures of a certain size.
40 * Returns: the number of blocks needed (minimum is always 1)
43 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
47 unsigned int first, second;
50 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
52 if (nstruct > first) {
53 second = (sdp->sd_sb.sb_bsize -
54 sizeof(struct gfs2_meta_header)) / ssize;
55 blks += DIV_ROUND_UP(nstruct - first, second);
62 * gfs2_ail1_start_one - Start I/O on a part of the AIL
63 * @sdp: the filesystem
64 * @tr: the part of the AIL
68 static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
70 struct gfs2_bufdata *bd, *s;
71 struct buffer_head *bh;
74 BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
79 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
83 gfs2_assert(sdp, bd->bd_ail == ai);
85 if (!buffer_busy(bh)) {
86 if (!buffer_uptodate(bh)) {
88 gfs2_io_error_bh(sdp, bh);
91 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
95 if (!buffer_dirty(bh))
98 list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
100 gfs2_log_unlock(sdp);
102 ll_rw_block(WRITE, 1, &bh);
112 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
113 * @sdp: the filesystem
118 static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
120 struct gfs2_bufdata *bd, *s;
121 struct buffer_head *bh;
123 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
127 gfs2_assert(sdp, bd->bd_ail == ai);
129 if (buffer_busy(bh)) {
136 if (!buffer_uptodate(bh))
137 gfs2_io_error_bh(sdp, bh);
139 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
142 return list_empty(&ai->ai_ail1_list);
145 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
147 struct list_head *head = &sdp->sd_ail1_list;
149 struct list_head *first;
150 struct gfs2_ail *first_ai, *ai, *tmp;
154 if (list_empty(head)) {
155 gfs2_log_unlock(sdp);
158 sync_gen = sdp->sd_ail_sync_gen++;
161 first_ai = list_entry(first, struct gfs2_ail, ai_list);
162 first_ai->ai_sync_gen = sync_gen;
163 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
169 if (first && (head->prev != first ||
170 gfs2_ail1_empty_one(sdp, first_ai, 0)))
174 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
175 if (ai->ai_sync_gen >= sync_gen)
177 ai->ai_sync_gen = sync_gen;
178 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
184 gfs2_log_unlock(sdp);
187 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
189 struct gfs2_ail *ai, *s;
194 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
195 if (gfs2_ail1_empty_one(sdp, ai, flags))
196 list_move(&ai->ai_list, &sdp->sd_ail2_list);
197 else if (!(flags & DIO_ALL))
201 ret = list_empty(&sdp->sd_ail1_list);
203 gfs2_log_unlock(sdp);
210 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
211 * @sdp: the filesystem
216 static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
218 struct list_head *head = &ai->ai_ail2_list;
219 struct gfs2_bufdata *bd;
221 while (!list_empty(head)) {
222 bd = list_entry(head->prev, struct gfs2_bufdata,
224 gfs2_assert(sdp, bd->bd_ail == ai);
226 list_del(&bd->bd_ail_st_list);
227 list_del(&bd->bd_ail_gl_list);
228 atomic_dec(&bd->bd_gl->gl_ail_count);
233 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
235 struct gfs2_ail *ai, *safe;
236 unsigned int old_tail = sdp->sd_log_tail;
237 int wrap = (new_tail < old_tail);
242 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
243 a = (old_tail <= ai->ai_first);
244 b = (ai->ai_first < new_tail);
245 rm = (wrap) ? (a || b) : (a && b);
249 gfs2_ail2_empty_one(sdp, ai);
250 list_del(&ai->ai_list);
251 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
252 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
256 gfs2_log_unlock(sdp);
260 * gfs2_log_reserve - Make a log reservation
261 * @sdp: The GFS2 superblock
262 * @blks: The number of blocks to reserve
267 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
269 unsigned int try = 0;
271 if (gfs2_assert_warn(sdp, blks) ||
272 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
275 mutex_lock(&sdp->sd_log_reserve_mutex);
277 while(sdp->sd_log_blks_free <= blks) {
278 gfs2_log_unlock(sdp);
279 gfs2_ail1_empty(sdp, 0);
280 gfs2_log_flush(sdp, NULL);
283 gfs2_ail1_start(sdp, 0);
286 sdp->sd_log_blks_free -= blks;
287 gfs2_log_unlock(sdp);
288 mutex_unlock(&sdp->sd_log_reserve_mutex);
290 down_read(&sdp->sd_log_flush_lock);
296 * gfs2_log_release - Release a given number of log blocks
297 * @sdp: The GFS2 superblock
298 * @blks: The number of blocks
302 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
306 sdp->sd_log_blks_free += blks;
307 gfs2_assert_withdraw(sdp,
308 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
309 gfs2_log_unlock(sdp);
310 up_read(&sdp->sd_log_flush_lock);
313 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
316 struct buffer_head bh_map;
318 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
319 if (error || !bh_map.b_blocknr)
320 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
321 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
323 return bh_map.b_blocknr;
327 * log_distance - Compute distance between two journal blocks
328 * @sdp: The GFS2 superblock
329 * @newer: The most recent journal block of the pair
330 * @older: The older journal block of the pair
332 * Compute the distance (in the journal direction) between two
333 * blocks in the journal
335 * Returns: the distance in blocks
338 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
343 dist = newer - older;
345 dist += sdp->sd_jdesc->jd_blocks;
350 static unsigned int current_tail(struct gfs2_sbd *sdp)
357 if (list_empty(&sdp->sd_ail1_list)) {
358 tail = sdp->sd_log_head;
360 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
364 gfs2_log_unlock(sdp);
369 static inline void log_incr_head(struct gfs2_sbd *sdp)
371 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
372 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
374 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
375 sdp->sd_log_flush_head = 0;
376 sdp->sd_log_flush_wrapped = 1;
381 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
382 * @sdp: The GFS2 superblock
384 * Returns: the buffer_head
387 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
389 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
390 struct gfs2_log_buf *lb;
391 struct buffer_head *bh;
393 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
394 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
396 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
398 memset(bh->b_data, 0, bh->b_size);
399 set_buffer_uptodate(bh);
400 clear_buffer_dirty(bh);
409 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
410 * @sdp: the filesystem
411 * @data: the data the buffer_head should point to
413 * Returns: the log buffer descriptor
416 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
417 struct buffer_head *real)
419 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
420 struct gfs2_log_buf *lb;
421 struct buffer_head *bh;
423 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
424 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
427 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
428 atomic_set(&bh->b_count, 1);
429 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
430 set_bh_page(bh, real->b_page, bh_offset(real));
431 bh->b_blocknr = blkno;
432 bh->b_size = sdp->sd_sb.sb_bsize;
433 bh->b_bdev = sdp->sd_vfs->s_bdev;
440 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
442 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
444 ail2_empty(sdp, new_tail);
447 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
448 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
449 gfs2_log_unlock(sdp);
451 sdp->sd_log_tail = new_tail;
455 * log_write_header - Get and initialize a journal header buffer
456 * @sdp: The GFS2 superblock
458 * Returns: the initialized log buffer descriptor
461 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
463 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
464 struct buffer_head *bh;
465 struct gfs2_log_header *lh;
469 bh = sb_getblk(sdp->sd_vfs, blkno);
471 memset(bh->b_data, 0, bh->b_size);
472 set_buffer_uptodate(bh);
473 clear_buffer_dirty(bh);
476 gfs2_ail1_empty(sdp, 0);
477 tail = current_tail(sdp);
479 lh = (struct gfs2_log_header *)bh->b_data;
480 memset(lh, 0, sizeof(struct gfs2_log_header));
481 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
482 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
483 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
484 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
485 lh->lh_flags = cpu_to_be32(flags);
486 lh->lh_tail = cpu_to_be32(tail);
487 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
488 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
489 lh->lh_hash = cpu_to_be32(hash);
491 set_buffer_dirty(bh);
492 if (sync_dirty_buffer(bh))
493 gfs2_io_error_bh(sdp, bh);
496 if (sdp->sd_log_tail != tail)
497 log_pull_tail(sdp, tail, pull);
499 gfs2_assert_withdraw(sdp, !pull);
501 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
505 static void log_flush_commit(struct gfs2_sbd *sdp)
507 struct list_head *head = &sdp->sd_log_flush_list;
508 struct gfs2_log_buf *lb;
509 struct buffer_head *bh;
511 while (!list_empty(head)) {
512 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
513 list_del(&lb->lb_list);
517 if (!buffer_uptodate(bh))
518 gfs2_io_error_bh(sdp, bh);
520 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
522 free_buffer_head(bh);
528 log_write_header(sdp, 0, 0);
532 * gfs2_log_flush - flush incore transaction(s)
533 * @sdp: the filesystem
534 * @gl: The glock structure to flush. If NULL, flush the whole incore log
538 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
542 down_write(&sdp->sd_log_flush_lock);
546 if (list_empty(&gl->gl_le.le_list)) {
547 gfs2_log_unlock(sdp);
548 up_write(&sdp->sd_log_flush_lock);
551 gfs2_log_unlock(sdp);
554 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
555 INIT_LIST_HEAD(&ai->ai_ail1_list);
556 INIT_LIST_HEAD(&ai->ai_ail2_list);
558 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
559 gfs2_assert_withdraw(sdp,
560 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
562 sdp->sd_log_flush_head = sdp->sd_log_head;
563 sdp->sd_log_flush_wrapped = 0;
564 ai->ai_first = sdp->sd_log_flush_head;
566 lops_before_commit(sdp);
567 if (!list_empty(&sdp->sd_log_flush_list))
568 log_flush_commit(sdp);
569 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
570 log_write_header(sdp, 0, PULL);
571 lops_after_commit(sdp, ai);
574 sdp->sd_log_head = sdp->sd_log_flush_head;
575 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
576 sdp->sd_log_blks_reserved = 0;
577 sdp->sd_log_commited_buf = 0;
578 sdp->sd_log_num_hdrs = 0;
579 sdp->sd_log_commited_revoke = 0;
581 if (!list_empty(&ai->ai_ail1_list)) {
582 list_add(&ai->ai_list, &sdp->sd_ail1_list);
585 gfs2_log_unlock(sdp);
587 sdp->sd_vfs->s_dirt = 0;
588 up_write(&sdp->sd_log_flush_lock);
593 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
595 unsigned int reserved = 0;
600 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
601 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
602 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
603 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
605 if (sdp->sd_log_commited_buf)
606 reserved += sdp->sd_log_commited_buf;
607 if (sdp->sd_log_commited_revoke)
608 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
613 old = sdp->sd_log_blks_free;
614 sdp->sd_log_blks_free += tr->tr_reserved -
615 (reserved - sdp->sd_log_blks_reserved);
617 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
618 gfs2_assert_withdraw(sdp,
619 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
620 sdp->sd_log_num_hdrs);
622 sdp->sd_log_blks_reserved = reserved;
624 gfs2_log_unlock(sdp);
628 * gfs2_log_commit - Commit a transaction to the log
629 * @sdp: the filesystem
630 * @tr: the transaction
635 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
638 lops_incore_commit(sdp, tr);
640 sdp->sd_vfs->s_dirt = 1;
641 up_read(&sdp->sd_log_flush_lock);
644 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
645 gfs2_log_unlock(sdp);
646 gfs2_log_flush(sdp, NULL);
648 gfs2_log_unlock(sdp);
653 * gfs2_log_shutdown - write a shutdown header into a journal
654 * @sdp: the filesystem
658 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
660 down_write(&sdp->sd_log_flush_lock);
662 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
663 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
664 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
665 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
666 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
667 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
668 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
669 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
670 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
672 sdp->sd_log_flush_head = sdp->sd_log_head;
673 sdp->sd_log_flush_wrapped = 0;
675 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
677 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
678 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
679 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
681 sdp->sd_log_head = sdp->sd_log_flush_head;
682 sdp->sd_log_tail = sdp->sd_log_head;
684 up_write(&sdp->sd_log_flush_lock);