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 void do_lock_wait(struct gfs2_sbd *sdp, wait_queue_head_t *wq,
29 wait_event(*wq, atomic_read(a) ? 0 : 1);
32 static void lock_for_trans(struct gfs2_sbd *sdp)
34 do_lock_wait(sdp, &sdp->sd_log_trans_wq, &sdp->sd_log_flush_count);
35 atomic_inc(&sdp->sd_log_trans_count);
38 static void unlock_from_trans(struct gfs2_sbd *sdp)
40 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_trans_count));
41 if (atomic_dec_and_test(&sdp->sd_log_trans_count))
42 wake_up(&sdp->sd_log_flush_wq);
45 static void gfs2_lock_for_flush(struct gfs2_sbd *sdp)
47 atomic_inc(&sdp->sd_log_flush_count);
48 do_lock_wait(sdp, &sdp->sd_log_flush_wq, &sdp->sd_log_trans_count);
51 static void gfs2_unlock_from_flush(struct gfs2_sbd *sdp)
53 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_flush_count));
54 if (atomic_dec_and_test(&sdp->sd_log_flush_count))
55 wake_up(&sdp->sd_log_trans_wq);
59 * gfs2_struct2blk - compute stuff
60 * @sdp: the filesystem
61 * @nstruct: the number of structures
62 * @ssize: the size of the structures
64 * Compute the number of log descriptor blocks needed to hold a certain number
65 * of structures of a certain size.
67 * Returns: the number of blocks needed (minimum is always 1)
70 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
74 unsigned int first, second;
77 first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
79 if (nstruct > first) {
80 second = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / ssize;
81 blks += DIV_RU(nstruct - first, second);
87 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
89 struct list_head *head = &sdp->sd_ail1_list;
91 struct list_head *first, *tmp;
92 struct gfs2_ail *first_ai, *ai;
95 if (list_empty(head)) {
99 sync_gen = sdp->sd_ail_sync_gen++;
102 first_ai = list_entry(first, struct gfs2_ail, ai_list);
103 first_ai->ai_sync_gen = sync_gen;
104 gfs2_ail1_start_one(sdp, first_ai);
111 (head->prev != first ||
112 gfs2_ail1_empty_one(sdp, first_ai, 0)))
115 for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
116 ai = list_entry(tmp, struct gfs2_ail, ai_list);
117 if (ai->ai_sync_gen >= sync_gen)
119 ai->ai_sync_gen = sync_gen;
120 gfs2_ail1_start_one(sdp, ai);
128 gfs2_log_unlock(sdp);
131 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
133 struct gfs2_ail *ai, *s;
138 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
139 if (gfs2_ail1_empty_one(sdp, ai, flags))
140 list_move(&ai->ai_list, &sdp->sd_ail2_list);
141 else if (!(flags & DIO_ALL))
145 ret = list_empty(&sdp->sd_ail1_list);
147 gfs2_log_unlock(sdp);
152 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
154 struct gfs2_ail *ai, *safe;
155 unsigned int old_tail = sdp->sd_log_tail;
156 int wrap = (new_tail < old_tail);
161 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
162 a = (old_tail <= ai->ai_first);
163 b = (ai->ai_first < new_tail);
164 rm = (wrap) ? (a || b) : (a && b);
168 gfs2_ail2_empty_one(sdp, ai);
169 list_del(&ai->ai_list);
170 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
171 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
175 gfs2_log_unlock(sdp);
179 * gfs2_log_reserve - Make a log reservation
180 * @sdp: The GFS2 superblock
181 * @blks: The number of blocks to reserve
186 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
189 unsigned int try = 0;
191 if (gfs2_assert_warn(sdp, blks) ||
192 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
197 if (list_empty(&list)) {
198 list_add_tail(&list, &sdp->sd_log_blks_list);
199 while (sdp->sd_log_blks_list.next != &list) {
200 DECLARE_WAITQUEUE(__wait_chan, current);
201 set_current_state(TASK_UNINTERRUPTIBLE);
202 add_wait_queue(&sdp->sd_log_blks_wait,
204 gfs2_log_unlock(sdp);
207 remove_wait_queue(&sdp->sd_log_blks_wait,
209 set_current_state(TASK_RUNNING);
212 /* Never give away the last block so we can
213 always pull the tail if we need to. */
214 if (sdp->sd_log_blks_free > blks) {
215 sdp->sd_log_blks_free -= blks;
217 gfs2_log_unlock(sdp);
218 wake_up(&sdp->sd_log_blks_wait);
222 gfs2_log_unlock(sdp);
223 gfs2_ail1_empty(sdp, 0);
227 gfs2_ail1_start(sdp, 0);
235 * gfs2_log_release - Release a given number of log blocks
236 * @sdp: The GFS2 superblock
237 * @blks: The number of blocks
241 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
243 unlock_from_trans(sdp);
246 sdp->sd_log_blks_free += blks;
247 gfs2_assert_withdraw(sdp,
248 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
249 gfs2_log_unlock(sdp);
252 static uint64_t log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
258 error = gfs2_block_map(get_v2ip(sdp->sd_jdesc->jd_inode), lbn, &new, &dbn, NULL);
259 gfs2_assert_withdraw(sdp, !error && dbn);
265 * log_distance - Compute distance between two journal blocks
266 * @sdp: The GFS2 superblock
267 * @newer: The most recent journal block of the pair
268 * @older: The older journal block of the pair
270 * Compute the distance (in the journal direction) between two
271 * blocks in the journal
273 * Returns: the distance in blocks
276 static inline unsigned int log_distance(struct gfs2_sbd *sdp,
282 dist = newer - older;
284 dist += sdp->sd_jdesc->jd_blocks;
289 static unsigned int current_tail(struct gfs2_sbd *sdp)
296 if (list_empty(&sdp->sd_ail1_list))
297 tail = sdp->sd_log_head;
299 ai = list_entry(sdp->sd_ail1_list.prev,
300 struct gfs2_ail, ai_list);
304 gfs2_log_unlock(sdp);
309 static inline void log_incr_head(struct gfs2_sbd *sdp)
311 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
312 gfs2_assert_withdraw(sdp,
313 sdp->sd_log_flush_head == sdp->sd_log_head);
315 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
316 sdp->sd_log_flush_head = 0;
317 sdp->sd_log_flush_wrapped = 1;
322 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
323 * @sdp: The GFS2 superblock
325 * Returns: the buffer_head
328 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
330 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
331 struct gfs2_log_buf *lb;
332 struct buffer_head *bh;
334 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
335 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
337 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
339 memset(bh->b_data, 0, bh->b_size);
340 set_buffer_uptodate(bh);
341 clear_buffer_dirty(bh);
350 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
351 * @sdp: the filesystem
352 * @data: the data the buffer_head should point to
354 * Returns: the log buffer descriptor
357 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
358 struct buffer_head *real)
360 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
361 struct gfs2_log_buf *lb;
362 struct buffer_head *bh;
364 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
365 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
368 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
369 atomic_set(&bh->b_count, 1);
370 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
371 set_bh_page(bh, real->b_page, bh_offset(real));
372 bh->b_blocknr = blkno;
373 bh->b_size = sdp->sd_sb.sb_bsize;
374 bh->b_bdev = sdp->sd_vfs->s_bdev;
381 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
383 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
385 ail2_empty(sdp, new_tail);
388 sdp->sd_log_blks_free += dist - ((pull) ? 1 : 0);
389 gfs2_assert_withdraw(sdp,
390 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
391 gfs2_log_unlock(sdp);
393 sdp->sd_log_tail = new_tail;
397 * log_write_header - Get and initialize a journal header buffer
398 * @sdp: The GFS2 superblock
400 * Returns: the initialized log buffer descriptor
403 static void log_write_header(struct gfs2_sbd *sdp, uint32_t flags, int pull)
405 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
406 struct buffer_head *bh;
407 struct gfs2_log_header *lh;
411 bh = sb_getblk(sdp->sd_vfs, blkno);
413 memset(bh->b_data, 0, bh->b_size);
414 set_buffer_uptodate(bh);
415 clear_buffer_dirty(bh);
418 gfs2_ail1_empty(sdp, 0);
419 tail = current_tail(sdp);
421 lh = (struct gfs2_log_header *)bh->b_data;
422 memset(lh, 0, sizeof(struct gfs2_log_header));
423 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
424 lh->lh_header.mh_type = cpu_to_be16(GFS2_METATYPE_LH);
425 lh->lh_header.mh_format = cpu_to_be16(GFS2_FORMAT_LH);
426 lh->lh_sequence = be64_to_cpu(sdp->sd_log_sequence++);
427 lh->lh_flags = be32_to_cpu(flags);
428 lh->lh_tail = be32_to_cpu(tail);
429 lh->lh_blkno = be32_to_cpu(sdp->sd_log_flush_head);
430 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
431 lh->lh_hash = cpu_to_be32(hash);
433 set_buffer_dirty(bh);
434 if (sync_dirty_buffer(bh))
435 gfs2_io_error_bh(sdp, bh);
438 if (sdp->sd_log_tail != tail)
439 log_pull_tail(sdp, tail, pull);
441 gfs2_assert_withdraw(sdp, !pull);
443 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
447 static void log_flush_commit(struct gfs2_sbd *sdp)
449 struct list_head *head = &sdp->sd_log_flush_list;
450 struct gfs2_log_buf *lb;
451 struct buffer_head *bh;
454 d = log_distance(sdp, sdp->sd_log_flush_head, sdp->sd_log_head);
456 gfs2_assert_withdraw(sdp, d + 1 == sdp->sd_log_blks_reserved);
458 while (!list_empty(head)) {
459 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
460 list_del(&lb->lb_list);
464 if (!buffer_uptodate(bh))
465 gfs2_io_error_bh(sdp, bh);
467 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
469 free_buffer_head(bh);
475 log_write_header(sdp, 0, 0);
479 * gfs2_log_flush_i - flush incore transaction(s)
480 * @sdp: the filesystem
481 * @gl: The glock structure to flush. If NULL, flush the whole incore log
485 void gfs2_log_flush_i(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
489 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
490 INIT_LIST_HEAD(&ai->ai_ail1_list);
491 INIT_LIST_HEAD(&ai->ai_ail2_list);
492 gfs2_lock_for_flush(sdp);
496 if (list_empty(&gl->gl_le.le_list)) {
497 gfs2_log_unlock(sdp);
498 gfs2_unlock_from_flush(sdp);
502 gfs2_log_unlock(sdp);
505 mutex_lock(&sdp->sd_log_flush_lock);
507 gfs2_assert_withdraw(sdp,
508 sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
509 gfs2_assert_withdraw(sdp,
510 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
512 sdp->sd_log_flush_head = sdp->sd_log_head;
513 sdp->sd_log_flush_wrapped = 0;
514 ai->ai_first = sdp->sd_log_flush_head;
516 lops_before_commit(sdp);
517 if (!list_empty(&sdp->sd_log_flush_list))
518 log_flush_commit(sdp);
519 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
520 log_write_header(sdp, 0, PULL);
521 lops_after_commit(sdp, ai);
522 sdp->sd_log_head = sdp->sd_log_flush_head;
523 if (sdp->sd_log_flush_wrapped)
526 sdp->sd_log_blks_reserved =
527 sdp->sd_log_commited_buf =
528 sdp->sd_log_commited_revoke = 0;
531 if (!list_empty(&ai->ai_ail1_list)) {
532 list_add(&ai->ai_list, &sdp->sd_ail1_list);
535 gfs2_log_unlock(sdp);
537 mutex_unlock(&sdp->sd_log_flush_lock);
538 sdp->sd_vfs->s_dirt = 0;
539 gfs2_unlock_from_flush(sdp);
544 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
546 unsigned int reserved = 1;
551 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
552 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
553 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
554 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
556 if (sdp->sd_log_commited_buf)
557 reserved += 1 + sdp->sd_log_commited_buf + sdp->sd_log_commited_buf/503;
558 if (sdp->sd_log_commited_revoke)
559 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
562 old = sdp->sd_log_blks_free;
563 sdp->sd_log_blks_free += tr->tr_reserved -
564 (reserved - sdp->sd_log_blks_reserved);
566 gfs2_assert_withdraw(sdp,
567 sdp->sd_log_blks_free >= old);
568 gfs2_assert_withdraw(sdp,
569 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
571 sdp->sd_log_blks_reserved = reserved;
573 gfs2_log_unlock(sdp);
577 * gfs2_log_commit - Commit a transaction to the log
578 * @sdp: the filesystem
579 * @tr: the transaction
584 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
587 lops_incore_commit(sdp, tr);
589 sdp->sd_vfs->s_dirt = 1;
590 unlock_from_trans(sdp);
595 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
596 gfs2_log_unlock(sdp);
599 gfs2_log_unlock(sdp);
603 * gfs2_log_shutdown - write a shutdown header into a journal
604 * @sdp: the filesystem
608 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
610 mutex_lock(&sdp->sd_log_flush_lock);
612 gfs2_assert_withdraw(sdp, !atomic_read(&sdp->sd_log_trans_count));
613 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
614 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
615 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
616 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
617 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
618 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
619 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
620 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
622 sdp->sd_log_flush_head = sdp->sd_log_head;
623 sdp->sd_log_flush_wrapped = 0;
625 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
627 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free ==
628 sdp->sd_jdesc->jd_blocks);
629 gfs2_assert_withdraw(sdp, sdp->sd_log_head == sdp->sd_log_tail);
630 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail2_list));
632 sdp->sd_log_head = sdp->sd_log_flush_head;
633 if (sdp->sd_log_flush_wrapped)
635 sdp->sd_log_tail = sdp->sd_log_head;
637 mutex_unlock(&sdp->sd_log_flush_lock);