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 <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <asm/semaphore.h>
20 #include "lm_interface.h"
32 static void lock_for_trans(struct gfs2_sbd *sdp)
34 wait_event(sdp->sd_log_trans_wq, atomic_read(&sdp->sd_log_flush_count) ? 0 : 1);
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 wait_event(sdp->sd_log_flush_wq, atomic_read(&sdp->sd_log_trans_count) ? 0 : 1);
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)) /
80 if (nstruct > first) {
81 second = (sdp->sd_sb.sb_bsize -
82 sizeof(struct gfs2_meta_header)) / ssize;
83 blks += DIV_ROUND_UP(nstruct - first, second);
89 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
91 struct list_head *head = &sdp->sd_ail1_list;
93 struct list_head *first, *tmp;
94 struct gfs2_ail *first_ai, *ai;
97 if (list_empty(head)) {
101 sync_gen = sdp->sd_ail_sync_gen++;
104 first_ai = list_entry(first, struct gfs2_ail, ai_list);
105 first_ai->ai_sync_gen = sync_gen;
106 gfs2_ail1_start_one(sdp, first_ai);
113 (head->prev != first ||
114 gfs2_ail1_empty_one(sdp, first_ai, 0)))
117 for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
118 ai = list_entry(tmp, struct gfs2_ail, ai_list);
119 if (ai->ai_sync_gen >= sync_gen)
121 ai->ai_sync_gen = sync_gen;
122 gfs2_ail1_start_one(sdp, ai);
130 gfs2_log_unlock(sdp);
133 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
135 struct gfs2_ail *ai, *s;
140 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
141 if (gfs2_ail1_empty_one(sdp, ai, flags))
142 list_move(&ai->ai_list, &sdp->sd_ail2_list);
143 else if (!(flags & DIO_ALL))
147 ret = list_empty(&sdp->sd_ail1_list);
149 gfs2_log_unlock(sdp);
154 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
156 struct gfs2_ail *ai, *safe;
157 unsigned int old_tail = sdp->sd_log_tail;
158 int wrap = (new_tail < old_tail);
163 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
164 a = (old_tail <= ai->ai_first);
165 b = (ai->ai_first < new_tail);
166 rm = (wrap) ? (a || b) : (a && b);
170 gfs2_ail2_empty_one(sdp, ai);
171 list_del(&ai->ai_list);
172 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
173 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
177 gfs2_log_unlock(sdp);
181 * gfs2_log_reserve - Make a log reservation
182 * @sdp: The GFS2 superblock
183 * @blks: The number of blocks to reserve
188 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
190 unsigned int try = 0;
192 if (gfs2_assert_warn(sdp, blks) ||
193 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
196 mutex_lock(&sdp->sd_log_reserve_mutex);
199 if (sdp->sd_log_blks_free > blks) {
200 sdp->sd_log_blks_free -= blks;
201 gfs2_log_unlock(sdp);
202 mutex_unlock(&sdp->sd_log_reserve_mutex);
206 gfs2_log_unlock(sdp);
207 gfs2_ail1_empty(sdp, 0);
211 gfs2_ail1_start(sdp, 0);
219 * gfs2_log_release - Release a given number of log blocks
220 * @sdp: The GFS2 superblock
221 * @blks: The number of blocks
225 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
227 unlock_from_trans(sdp);
230 sdp->sd_log_blks_free += blks;
231 gfs2_assert_withdraw(sdp,
232 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
233 gfs2_log_unlock(sdp);
236 static uint64_t log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
242 error = gfs2_block_map(sdp->sd_jdesc->jd_inode->u.generic_ip,
243 lbn, &new, &dbn, NULL);
244 gfs2_assert_withdraw(sdp, !error && dbn);
250 * log_distance - Compute distance between two journal blocks
251 * @sdp: The GFS2 superblock
252 * @newer: The most recent journal block of the pair
253 * @older: The older journal block of the pair
255 * Compute the distance (in the journal direction) between two
256 * blocks in the journal
258 * Returns: the distance in blocks
261 static inline unsigned int log_distance(struct gfs2_sbd *sdp,
267 dist = newer - older;
269 dist += sdp->sd_jdesc->jd_blocks;
274 static unsigned int current_tail(struct gfs2_sbd *sdp)
281 if (list_empty(&sdp->sd_ail1_list))
282 tail = sdp->sd_log_head;
284 ai = list_entry(sdp->sd_ail1_list.prev,
285 struct gfs2_ail, ai_list);
289 gfs2_log_unlock(sdp);
294 static inline void log_incr_head(struct gfs2_sbd *sdp)
296 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
297 gfs2_assert_withdraw(sdp,
298 sdp->sd_log_flush_head == sdp->sd_log_head);
300 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
301 sdp->sd_log_flush_head = 0;
302 sdp->sd_log_flush_wrapped = 1;
307 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
308 * @sdp: The GFS2 superblock
310 * Returns: the buffer_head
313 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
315 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
316 struct gfs2_log_buf *lb;
317 struct buffer_head *bh;
319 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
320 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
322 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
324 memset(bh->b_data, 0, bh->b_size);
325 set_buffer_uptodate(bh);
326 clear_buffer_dirty(bh);
335 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
336 * @sdp: the filesystem
337 * @data: the data the buffer_head should point to
339 * Returns: the log buffer descriptor
342 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
343 struct buffer_head *real)
345 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
346 struct gfs2_log_buf *lb;
347 struct buffer_head *bh;
349 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
350 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
353 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
354 atomic_set(&bh->b_count, 1);
355 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
356 set_bh_page(bh, real->b_page, bh_offset(real));
357 bh->b_blocknr = blkno;
358 bh->b_size = sdp->sd_sb.sb_bsize;
359 bh->b_bdev = sdp->sd_vfs->s_bdev;
366 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
368 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
370 ail2_empty(sdp, new_tail);
373 sdp->sd_log_blks_free += dist - ((pull) ? 1 : 0);
374 gfs2_assert_withdraw(sdp,
375 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
376 gfs2_log_unlock(sdp);
378 sdp->sd_log_tail = new_tail;
382 * log_write_header - Get and initialize a journal header buffer
383 * @sdp: The GFS2 superblock
385 * Returns: the initialized log buffer descriptor
388 static void log_write_header(struct gfs2_sbd *sdp, uint32_t flags, int pull)
390 uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
391 struct buffer_head *bh;
392 struct gfs2_log_header *lh;
396 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);
403 gfs2_ail1_empty(sdp, 0);
404 tail = current_tail(sdp);
406 lh = (struct gfs2_log_header *)bh->b_data;
407 memset(lh, 0, sizeof(struct gfs2_log_header));
408 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
409 lh->lh_header.mh_type = cpu_to_be16(GFS2_METATYPE_LH);
410 lh->lh_header.mh_format = cpu_to_be16(GFS2_FORMAT_LH);
411 lh->lh_sequence = be64_to_cpu(sdp->sd_log_sequence++);
412 lh->lh_flags = be32_to_cpu(flags);
413 lh->lh_tail = be32_to_cpu(tail);
414 lh->lh_blkno = be32_to_cpu(sdp->sd_log_flush_head);
415 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
416 lh->lh_hash = cpu_to_be32(hash);
418 set_buffer_dirty(bh);
419 if (sync_dirty_buffer(bh))
420 gfs2_io_error_bh(sdp, bh);
423 if (sdp->sd_log_tail != tail)
424 log_pull_tail(sdp, tail, pull);
426 gfs2_assert_withdraw(sdp, !pull);
428 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
432 static void log_flush_commit(struct gfs2_sbd *sdp)
434 struct list_head *head = &sdp->sd_log_flush_list;
435 struct gfs2_log_buf *lb;
436 struct buffer_head *bh;
439 d = log_distance(sdp, sdp->sd_log_flush_head, sdp->sd_log_head);
441 gfs2_assert_withdraw(sdp, d + 1 == sdp->sd_log_blks_reserved);
443 while (!list_empty(head)) {
444 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
445 list_del(&lb->lb_list);
449 if (!buffer_uptodate(bh))
450 gfs2_io_error_bh(sdp, bh);
452 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
454 free_buffer_head(bh);
460 log_write_header(sdp, 0, 0);
464 * gfs2_log_flush_i - flush incore transaction(s)
465 * @sdp: the filesystem
466 * @gl: The glock structure to flush. If NULL, flush the whole incore log
470 void gfs2_log_flush_i(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
474 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
475 INIT_LIST_HEAD(&ai->ai_ail1_list);
476 INIT_LIST_HEAD(&ai->ai_ail2_list);
477 gfs2_lock_for_flush(sdp);
481 if (list_empty(&gl->gl_le.le_list)) {
482 gfs2_log_unlock(sdp);
483 gfs2_unlock_from_flush(sdp);
487 gfs2_log_unlock(sdp);
490 mutex_lock(&sdp->sd_log_flush_lock);
492 gfs2_assert_withdraw(sdp,
493 sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
494 gfs2_assert_withdraw(sdp,
495 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
497 sdp->sd_log_flush_head = sdp->sd_log_head;
498 sdp->sd_log_flush_wrapped = 0;
499 ai->ai_first = sdp->sd_log_flush_head;
501 lops_before_commit(sdp);
502 if (!list_empty(&sdp->sd_log_flush_list))
503 log_flush_commit(sdp);
504 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
505 log_write_header(sdp, 0, PULL);
506 lops_after_commit(sdp, ai);
507 sdp->sd_log_head = sdp->sd_log_flush_head;
508 if (sdp->sd_log_flush_wrapped)
511 sdp->sd_log_blks_reserved =
512 sdp->sd_log_commited_buf =
513 sdp->sd_log_commited_revoke = 0;
516 if (!list_empty(&ai->ai_ail1_list)) {
517 list_add(&ai->ai_list, &sdp->sd_ail1_list);
520 gfs2_log_unlock(sdp);
522 mutex_unlock(&sdp->sd_log_flush_lock);
523 sdp->sd_vfs->s_dirt = 0;
524 gfs2_unlock_from_flush(sdp);
529 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
531 unsigned int reserved = 1;
536 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
537 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
538 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
539 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
541 if (sdp->sd_log_commited_buf)
542 reserved += 1 + sdp->sd_log_commited_buf +
543 sdp->sd_log_commited_buf/503;
544 if (sdp->sd_log_commited_revoke)
545 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
548 old = sdp->sd_log_blks_free;
549 sdp->sd_log_blks_free += tr->tr_reserved -
550 (reserved - sdp->sd_log_blks_reserved);
552 gfs2_assert_withdraw(sdp,
553 sdp->sd_log_blks_free >= old);
554 gfs2_assert_withdraw(sdp,
555 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
557 sdp->sd_log_blks_reserved = reserved;
559 gfs2_log_unlock(sdp);
563 * gfs2_log_commit - Commit a transaction to the log
564 * @sdp: the filesystem
565 * @tr: the transaction
570 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
573 lops_incore_commit(sdp, tr);
575 sdp->sd_vfs->s_dirt = 1;
576 unlock_from_trans(sdp);
579 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
580 gfs2_log_unlock(sdp);
583 gfs2_log_unlock(sdp);
587 * gfs2_log_shutdown - write a shutdown header into a journal
588 * @sdp: the filesystem
592 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
594 mutex_lock(&sdp->sd_log_flush_lock);
596 gfs2_assert_withdraw(sdp, !atomic_read(&sdp->sd_log_trans_count));
597 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
598 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
599 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
600 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
601 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
602 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
603 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
604 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
606 sdp->sd_log_flush_head = sdp->sd_log_head;
607 sdp->sd_log_flush_wrapped = 0;
609 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
611 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free ==
612 sdp->sd_jdesc->jd_blocks);
613 gfs2_assert_withdraw(sdp, sdp->sd_log_head == sdp->sd_log_tail);
614 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail2_list));
616 sdp->sd_log_head = sdp->sd_log_flush_head;
617 if (sdp->sd_log_flush_wrapped)
619 sdp->sd_log_tail = sdp->sd_log_head;
621 mutex_unlock(&sdp->sd_log_flush_lock);