2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.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 <asm/semaphore.h>
26 static inline int is_done(struct gfs2_sbd *sdp, atomic_t *a)
30 done = atomic_read(a) ? 0 : 1;
35 static void do_lock_wait(struct gfs2_sbd *sdp, wait_queue_head_t *wq,
39 wait_event(*wq, is_done(sdp, a));
43 static void lock_for_trans(struct gfs2_sbd *sdp)
46 do_lock_wait(sdp, &sdp->sd_log_trans_wq, &sdp->sd_log_flush_count);
47 atomic_inc(&sdp->sd_log_trans_count);
51 static void unlock_from_trans(struct gfs2_sbd *sdp)
53 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_trans_count));
54 if (atomic_dec_and_test(&sdp->sd_log_trans_count))
55 wake_up(&sdp->sd_log_flush_wq);
58 void gfs2_lock_for_flush(struct gfs2_sbd *sdp)
61 atomic_inc(&sdp->sd_log_flush_count);
62 do_lock_wait(sdp, &sdp->sd_log_flush_wq, &sdp->sd_log_trans_count);
66 void gfs2_unlock_from_flush(struct gfs2_sbd *sdp)
68 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_flush_count));
69 if (atomic_dec_and_test(&sdp->sd_log_flush_count))
70 wake_up(&sdp->sd_log_trans_wq);
74 * gfs2_struct2blk - compute stuff
75 * @sdp: the filesystem
76 * @nstruct: the number of structures
77 * @ssize: the size of the structures
79 * Compute the number of log descriptor blocks needed to hold a certain number
80 * of structures of a certain size.
82 * Returns: the number of blocks needed (minimum is always 1)
85 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
89 unsigned int first, second;
92 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
94 if (nstruct > first) {
95 second = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / ssize;
96 blks += DIV_RU(nstruct - first, second);
102 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
104 struct list_head *head = &sdp->sd_ail1_list;
106 struct list_head *first, *tmp;
107 struct gfs2_ail *first_ai, *ai;
110 if (list_empty(head)) {
111 gfs2_log_unlock(sdp);
114 sync_gen = sdp->sd_ail_sync_gen++;
117 first_ai = list_entry(first, struct gfs2_ail, ai_list);
118 first_ai->ai_sync_gen = sync_gen;
119 gfs2_ail1_start_one(sdp, first_ai);
126 (head->prev != first ||
127 gfs2_ail1_empty_one(sdp, first_ai, 0)))
130 for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
131 ai = list_entry(tmp, struct gfs2_ail, ai_list);
132 if (ai->ai_sync_gen >= sync_gen)
134 ai->ai_sync_gen = sync_gen;
135 gfs2_ail1_start_one(sdp, ai);
143 gfs2_log_unlock(sdp);
146 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
148 struct gfs2_ail *ai, *s;
153 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
154 if (gfs2_ail1_empty_one(sdp, ai, flags))
155 list_move(&ai->ai_list, &sdp->sd_ail2_list);
156 else if (!(flags & DIO_ALL))
160 ret = list_empty(&sdp->sd_ail1_list);
162 gfs2_log_unlock(sdp);
167 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
169 struct gfs2_ail *ai, *safe;
170 unsigned int old_tail = sdp->sd_log_tail;
171 int wrap = (new_tail < old_tail);
176 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
177 a = (old_tail <= ai->ai_first);
178 b = (ai->ai_first < new_tail);
179 rm = (wrap) ? (a || b) : (a && b);
183 gfs2_ail2_empty_one(sdp, ai);
184 list_del(&ai->ai_list);
185 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
186 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
190 gfs2_log_unlock(sdp);
194 * gfs2_log_reserve - Make a log reservation
195 * @sdp: The GFS2 superblock
196 * @blks: The number of blocks to reserve
201 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
204 unsigned int try = 0;
206 if (gfs2_assert_warn(sdp, blks) ||
207 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
213 if (list_empty(&list)) {
214 list_add_tail(&list, &sdp->sd_log_blks_list);
215 while (sdp->sd_log_blks_list.next != &list) {
216 DECLARE_WAITQUEUE(__wait_chan, current);
217 set_current_state(TASK_UNINTERRUPTIBLE);
218 add_wait_queue(&sdp->sd_log_blks_wait,
220 gfs2_log_unlock(sdp);
223 remove_wait_queue(&sdp->sd_log_blks_wait,
225 set_current_state(TASK_RUNNING);
229 /* Never give away the last block so we can
230 always pull the tail if we need to. */
231 if (sdp->sd_log_blks_free > blks) {
232 sdp->sd_log_blks_free -= blks;
234 gfs2_log_unlock(sdp);
235 wake_up(&sdp->sd_log_blks_wait);
239 gfs2_log_unlock(sdp);
241 gfs2_ail1_empty(sdp, 0);
245 gfs2_ail1_start(sdp, 0);
254 * gfs2_log_release - Release a given number of log blocks
255 * @sdp: The GFS2 superblock
256 * @blks: The number of blocks
260 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
262 unlock_from_trans(sdp);
265 sdp->sd_log_blks_free += blks;
266 gfs2_assert_withdraw(sdp,
267 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
268 gfs2_log_unlock(sdp);
271 static uint64_t log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
277 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, &new, &dbn, NULL);
278 gfs2_assert_withdraw(sdp, !error && dbn);
284 * log_distance - Compute distance between two journal blocks
285 * @sdp: The GFS2 superblock
286 * @newer: The most recent journal block of the pair
287 * @older: The older journal block of the pair
289 * Compute the distance (in the journal direction) between two
290 * blocks in the journal
292 * Returns: the distance in blocks
295 static inline unsigned int log_distance(struct gfs2_sbd *sdp,
301 dist = newer - older;
303 dist += sdp->sd_jdesc->jd_blocks;
308 static unsigned int current_tail(struct gfs2_sbd *sdp)
315 if (list_empty(&sdp->sd_ail1_list))
316 tail = sdp->sd_log_head;
318 ai = list_entry(sdp->sd_ail1_list.prev,
319 struct gfs2_ail, ai_list);
323 gfs2_log_unlock(sdp);
328 static inline void log_incr_head(struct gfs2_sbd *sdp)
330 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
331 gfs2_assert_withdraw(sdp,
332 sdp->sd_log_flush_head == sdp->sd_log_head);
334 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
335 sdp->sd_log_flush_head = 0;
336 sdp->sd_log_flush_wrapped = 1;
341 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
342 * @sdp: The GFS2 superblock
344 * Returns: the buffer_head
347 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
349 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
350 struct gfs2_log_buf *lb;
351 struct buffer_head *bh;
353 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_KERNEL | __GFP_NOFAIL);
354 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
356 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
358 memset(bh->b_data, 0, bh->b_size);
359 set_buffer_uptodate(bh);
360 clear_buffer_dirty(bh);
369 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
370 * @sdp: the filesystem
371 * @data: the data the buffer_head should point to
373 * Returns: the log buffer descriptor
376 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
377 struct buffer_head *real)
379 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
380 struct gfs2_log_buf *lb;
381 struct buffer_head *bh;
383 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_KERNEL | __GFP_NOFAIL);
384 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
387 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
388 atomic_set(&bh->b_count, 1);
389 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
390 set_bh_page(bh, virt_to_page(real->b_data),
391 ((unsigned long)real->b_data) & (PAGE_SIZE - 1));
392 bh->b_blocknr = blkno;
393 bh->b_size = sdp->sd_sb.sb_bsize;
394 bh->b_bdev = sdp->sd_vfs->s_bdev;
401 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
403 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
405 ail2_empty(sdp, new_tail);
408 sdp->sd_log_blks_free += dist - ((pull) ? 1 : 0);
409 gfs2_assert_withdraw(sdp,
410 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
411 gfs2_log_unlock(sdp);
413 sdp->sd_log_tail = new_tail;
417 * log_write_header - Get and initialize a journal header buffer
418 * @sdp: The GFS2 superblock
420 * Returns: the initialized log buffer descriptor
423 static void log_write_header(struct gfs2_sbd *sdp, uint32_t flags, int pull)
425 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
426 struct buffer_head *bh;
427 struct gfs2_log_header *lh;
431 atomic_inc(&sdp->sd_log_flush_ondisk);
433 bh = sb_getblk(sdp->sd_vfs, blkno);
435 memset(bh->b_data, 0, bh->b_size);
436 set_buffer_uptodate(bh);
437 clear_buffer_dirty(bh);
440 gfs2_ail1_empty(sdp, 0);
441 tail = current_tail(sdp);
443 lh = (struct gfs2_log_header *)bh->b_data;
444 memset(lh, 0, sizeof(struct gfs2_log_header));
445 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
446 lh->lh_header.mh_type = cpu_to_be16(GFS2_METATYPE_LH);
447 lh->lh_header.mh_format = cpu_to_be16(GFS2_FORMAT_LH);
448 lh->lh_sequence = be64_to_cpu(sdp->sd_log_sequence++);
449 lh->lh_flags = be32_to_cpu(flags);
450 lh->lh_tail = be32_to_cpu(tail);
451 lh->lh_blkno = be32_to_cpu(sdp->sd_log_flush_head);
452 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
453 lh->lh_hash = cpu_to_be32(hash);
455 set_buffer_dirty(bh);
456 if (sync_dirty_buffer(bh))
457 gfs2_io_error_bh(sdp, bh);
460 if (sdp->sd_log_tail != tail)
461 log_pull_tail(sdp, tail, pull);
463 gfs2_assert_withdraw(sdp, !pull);
465 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
469 static void log_flush_commit(struct gfs2_sbd *sdp)
471 struct list_head *head = &sdp->sd_log_flush_list;
472 struct gfs2_log_buf *lb;
473 struct buffer_head *bh;
476 d = log_distance(sdp, sdp->sd_log_flush_head, sdp->sd_log_head);
478 gfs2_assert_withdraw(sdp, d + 1 == sdp->sd_log_blks_reserved);
480 while (!list_empty(head)) {
481 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
482 list_del(&lb->lb_list);
486 if (!buffer_uptodate(bh))
487 gfs2_io_error_bh(sdp, bh);
489 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
491 free_buffer_head(bh);
497 log_write_header(sdp, 0, 0);
501 * gfs2_log_flush_i - flush incore transaction(s)
502 * @sdp: the filesystem
503 * @gl: The glock structure to flush. If NULL, flush the whole incore log
507 void gfs2_log_flush_i(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
511 atomic_inc(&sdp->sd_log_flush_incore);
513 ai = kzalloc(sizeof(struct gfs2_ail), GFP_KERNEL | __GFP_NOFAIL);
514 INIT_LIST_HEAD(&ai->ai_ail1_list);
515 INIT_LIST_HEAD(&ai->ai_ail2_list);
517 gfs2_lock_for_flush(sdp);
518 down(&sdp->sd_log_flush_lock);
520 gfs2_assert_withdraw(sdp,
521 sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
522 gfs2_assert_withdraw(sdp,
523 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
525 if (gl && list_empty(&gl->gl_le.le_list)) {
526 up(&sdp->sd_log_flush_lock);
527 gfs2_unlock_from_flush(sdp);
532 sdp->sd_log_flush_head = sdp->sd_log_head;
533 sdp->sd_log_flush_wrapped = 0;
534 ai->ai_first = sdp->sd_log_flush_head;
536 lops_before_commit(sdp);
537 if (!list_empty(&sdp->sd_log_flush_list))
538 log_flush_commit(sdp);
539 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
540 log_write_header(sdp, 0, PULL);
541 lops_after_commit(sdp, ai);
543 sdp->sd_log_head = sdp->sd_log_flush_head;
544 if (sdp->sd_log_flush_wrapped)
547 sdp->sd_log_blks_reserved =
548 sdp->sd_log_commited_buf =
549 sdp->sd_log_commited_revoke = 0;
552 if (!list_empty(&ai->ai_ail1_list)) {
553 list_add(&ai->ai_list, &sdp->sd_ail1_list);
556 gfs2_log_unlock(sdp);
558 up(&sdp->sd_log_flush_lock);
559 sdp->sd_vfs->s_dirt = 0;
560 gfs2_unlock_from_flush(sdp);
565 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
567 unsigned int reserved = 1;
572 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
573 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
574 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
575 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
577 if (sdp->sd_log_commited_buf)
578 reserved += 1 + sdp->sd_log_commited_buf + sdp->sd_log_commited_buf/503;
579 if (sdp->sd_log_commited_revoke)
580 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
583 old = sdp->sd_log_blks_free;
584 sdp->sd_log_blks_free += tr->tr_reserved -
585 (reserved - sdp->sd_log_blks_reserved);
587 gfs2_assert_withdraw(sdp,
588 sdp->sd_log_blks_free >= old);
589 gfs2_assert_withdraw(sdp,
590 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
592 sdp->sd_log_blks_reserved = reserved;
594 gfs2_log_unlock(sdp);
598 * gfs2_log_commit - Commit a transaction to the log
599 * @sdp: the filesystem
600 * @tr: the transaction
605 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
608 lops_incore_commit(sdp, tr);
610 sdp->sd_vfs->s_dirt = 1;
611 unlock_from_trans(sdp);
616 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
617 gfs2_log_unlock(sdp);
620 gfs2_log_unlock(sdp);
624 * gfs2_log_shutdown - write a shutdown header into a journal
625 * @sdp: the filesystem
629 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
631 down(&sdp->sd_log_flush_lock);
633 gfs2_assert_withdraw(sdp, !atomic_read(&sdp->sd_log_trans_count));
634 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
635 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
636 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
637 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
638 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
639 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
640 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
642 sdp->sd_log_flush_head = sdp->sd_log_head;
643 sdp->sd_log_flush_wrapped = 0;
645 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
647 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free ==
648 sdp->sd_jdesc->jd_blocks);
649 gfs2_assert_withdraw(sdp, sdp->sd_log_head == sdp->sd_log_tail);
650 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail2_list));
652 sdp->sd_log_head = sdp->sd_log_flush_head;
653 if (sdp->sd_log_flush_wrapped)
655 sdp->sd_log_tail = sdp->sd_log_head;
657 up(&sdp->sd_log_flush_lock);