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>
19 #include "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);
61 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
63 struct list_head *head = &sdp->sd_ail1_list;
65 struct list_head *first, *tmp;
66 struct gfs2_ail *first_ai, *ai;
69 if (list_empty(head)) {
73 sync_gen = sdp->sd_ail_sync_gen++;
76 first_ai = list_entry(first, struct gfs2_ail, ai_list);
77 first_ai->ai_sync_gen = sync_gen;
78 gfs2_ail1_start_one(sdp, first_ai);
84 if (first && (head->prev != first ||
85 gfs2_ail1_empty_one(sdp, first_ai, 0)))
88 for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
89 ai = list_entry(tmp, struct gfs2_ail, ai_list);
90 if (ai->ai_sync_gen >= sync_gen)
92 ai->ai_sync_gen = sync_gen;
93 gfs2_ail1_start_one(sdp, ai);
101 gfs2_log_unlock(sdp);
104 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
106 struct gfs2_ail *ai, *s;
111 list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
112 if (gfs2_ail1_empty_one(sdp, ai, flags))
113 list_move(&ai->ai_list, &sdp->sd_ail2_list);
114 else if (!(flags & DIO_ALL))
118 ret = list_empty(&sdp->sd_ail1_list);
120 gfs2_log_unlock(sdp);
125 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
127 struct gfs2_ail *ai, *safe;
128 unsigned int old_tail = sdp->sd_log_tail;
129 int wrap = (new_tail < old_tail);
134 list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
135 a = (old_tail <= ai->ai_first);
136 b = (ai->ai_first < new_tail);
137 rm = (wrap) ? (a || b) : (a && b);
141 gfs2_ail2_empty_one(sdp, ai);
142 list_del(&ai->ai_list);
143 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
144 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
148 gfs2_log_unlock(sdp);
152 * gfs2_log_reserve - Make a log reservation
153 * @sdp: The GFS2 superblock
154 * @blks: The number of blocks to reserve
159 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
161 unsigned int try = 0;
163 if (gfs2_assert_warn(sdp, blks) ||
164 gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
167 mutex_lock(&sdp->sd_log_reserve_mutex);
169 while(sdp->sd_log_blks_free <= blks) {
170 gfs2_log_unlock(sdp);
171 gfs2_ail1_empty(sdp, 0);
172 gfs2_log_flush(sdp, NULL);
175 gfs2_ail1_start(sdp, 0);
178 sdp->sd_log_blks_free -= blks;
179 gfs2_log_unlock(sdp);
180 mutex_unlock(&sdp->sd_log_reserve_mutex);
182 down_read(&sdp->sd_log_flush_lock);
188 * gfs2_log_release - Release a given number of log blocks
189 * @sdp: The GFS2 superblock
190 * @blks: The number of blocks
194 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
198 sdp->sd_log_blks_free += blks;
199 gfs2_assert_withdraw(sdp,
200 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
201 gfs2_log_unlock(sdp);
202 up_read(&sdp->sd_log_flush_lock);
205 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
208 struct buffer_head bh_map;
210 error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
211 if (error || !bh_map.b_blocknr)
212 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
213 gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
215 return bh_map.b_blocknr;
219 * log_distance - Compute distance between two journal blocks
220 * @sdp: The GFS2 superblock
221 * @newer: The most recent journal block of the pair
222 * @older: The older journal block of the pair
224 * Compute the distance (in the journal direction) between two
225 * blocks in the journal
227 * Returns: the distance in blocks
230 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
235 dist = newer - older;
237 dist += sdp->sd_jdesc->jd_blocks;
242 static unsigned int current_tail(struct gfs2_sbd *sdp)
249 if (list_empty(&sdp->sd_ail1_list)) {
250 tail = sdp->sd_log_head;
252 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
256 gfs2_log_unlock(sdp);
261 static inline void log_incr_head(struct gfs2_sbd *sdp)
263 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
264 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
266 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
267 sdp->sd_log_flush_head = 0;
268 sdp->sd_log_flush_wrapped = 1;
273 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
274 * @sdp: The GFS2 superblock
276 * Returns: the buffer_head
279 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
281 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
282 struct gfs2_log_buf *lb;
283 struct buffer_head *bh;
285 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
286 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
288 bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
290 memset(bh->b_data, 0, bh->b_size);
291 set_buffer_uptodate(bh);
292 clear_buffer_dirty(bh);
301 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
302 * @sdp: the filesystem
303 * @data: the data the buffer_head should point to
305 * Returns: the log buffer descriptor
308 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
309 struct buffer_head *real)
311 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
312 struct gfs2_log_buf *lb;
313 struct buffer_head *bh;
315 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
316 list_add(&lb->lb_list, &sdp->sd_log_flush_list);
319 bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
320 atomic_set(&bh->b_count, 1);
321 bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
322 set_bh_page(bh, real->b_page, bh_offset(real));
323 bh->b_blocknr = blkno;
324 bh->b_size = sdp->sd_sb.sb_bsize;
325 bh->b_bdev = sdp->sd_vfs->s_bdev;
332 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
334 unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
336 ail2_empty(sdp, new_tail);
339 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
340 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
341 gfs2_log_unlock(sdp);
343 sdp->sd_log_tail = new_tail;
347 * log_write_header - Get and initialize a journal header buffer
348 * @sdp: The GFS2 superblock
350 * Returns: the initialized log buffer descriptor
353 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
355 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
356 struct buffer_head *bh;
357 struct gfs2_log_header *lh;
361 bh = sb_getblk(sdp->sd_vfs, blkno);
363 memset(bh->b_data, 0, bh->b_size);
364 set_buffer_uptodate(bh);
365 clear_buffer_dirty(bh);
368 gfs2_ail1_empty(sdp, 0);
369 tail = current_tail(sdp);
371 lh = (struct gfs2_log_header *)bh->b_data;
372 memset(lh, 0, sizeof(struct gfs2_log_header));
373 lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
374 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
375 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
376 lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
377 lh->lh_flags = cpu_to_be32(flags);
378 lh->lh_tail = cpu_to_be32(tail);
379 lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
380 hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
381 lh->lh_hash = cpu_to_be32(hash);
383 set_buffer_dirty(bh);
384 if (sync_dirty_buffer(bh))
385 gfs2_io_error_bh(sdp, bh);
388 if (sdp->sd_log_tail != tail)
389 log_pull_tail(sdp, tail, pull);
391 gfs2_assert_withdraw(sdp, !pull);
393 sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
397 static void log_flush_commit(struct gfs2_sbd *sdp)
399 struct list_head *head = &sdp->sd_log_flush_list;
400 struct gfs2_log_buf *lb;
401 struct buffer_head *bh;
403 while (!list_empty(head)) {
404 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
405 list_del(&lb->lb_list);
409 if (!buffer_uptodate(bh))
410 gfs2_io_error_bh(sdp, bh);
412 while (atomic_read(&bh->b_count) != 1) /* Grrrr... */
414 free_buffer_head(bh);
420 log_write_header(sdp, 0, 0);
424 * gfs2_log_flush - flush incore transaction(s)
425 * @sdp: the filesystem
426 * @gl: The glock structure to flush. If NULL, flush the whole incore log
430 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
434 down_write(&sdp->sd_log_flush_lock);
438 if (list_empty(&gl->gl_le.le_list)) {
439 gfs2_log_unlock(sdp);
440 up_write(&sdp->sd_log_flush_lock);
443 gfs2_log_unlock(sdp);
446 ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
447 INIT_LIST_HEAD(&ai->ai_ail1_list);
448 INIT_LIST_HEAD(&ai->ai_ail2_list);
450 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
451 gfs2_assert_withdraw(sdp,
452 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
454 sdp->sd_log_flush_head = sdp->sd_log_head;
455 sdp->sd_log_flush_wrapped = 0;
456 ai->ai_first = sdp->sd_log_flush_head;
458 lops_before_commit(sdp);
459 if (!list_empty(&sdp->sd_log_flush_list))
460 log_flush_commit(sdp);
461 else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
462 log_write_header(sdp, 0, PULL);
463 lops_after_commit(sdp, ai);
464 sdp->sd_log_head = sdp->sd_log_flush_head;
466 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
468 sdp->sd_log_blks_reserved = 0;
469 sdp->sd_log_commited_buf = 0;
470 sdp->sd_log_num_hdrs = 0;
471 sdp->sd_log_commited_revoke = 0;
474 if (!list_empty(&ai->ai_ail1_list)) {
475 list_add(&ai->ai_list, &sdp->sd_ail1_list);
478 gfs2_log_unlock(sdp);
480 sdp->sd_vfs->s_dirt = 0;
481 up_write(&sdp->sd_log_flush_lock);
486 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
488 unsigned int reserved = 0;
493 sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
494 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
495 sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
496 gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
498 if (sdp->sd_log_commited_buf)
499 reserved += sdp->sd_log_commited_buf;
500 if (sdp->sd_log_commited_revoke)
501 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
506 old = sdp->sd_log_blks_free;
507 sdp->sd_log_blks_free += tr->tr_reserved -
508 (reserved - sdp->sd_log_blks_reserved);
510 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
511 gfs2_assert_withdraw(sdp,
512 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
513 sdp->sd_log_num_hdrs);
515 sdp->sd_log_blks_reserved = reserved;
517 gfs2_log_unlock(sdp);
521 * gfs2_log_commit - Commit a transaction to the log
522 * @sdp: the filesystem
523 * @tr: the transaction
528 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
531 lops_incore_commit(sdp, tr);
533 sdp->sd_vfs->s_dirt = 1;
534 up_read(&sdp->sd_log_flush_lock);
537 if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
538 gfs2_log_unlock(sdp);
539 gfs2_log_flush(sdp, NULL);
541 gfs2_log_unlock(sdp);
546 * gfs2_log_shutdown - write a shutdown header into a journal
547 * @sdp: the filesystem
551 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
553 down_write(&sdp->sd_log_flush_lock);
555 gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
556 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
557 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
558 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
559 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
560 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
561 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
562 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
563 gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
565 sdp->sd_log_flush_head = sdp->sd_log_head;
566 sdp->sd_log_flush_wrapped = 0;
568 log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
570 gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
571 gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
572 gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
574 sdp->sd_log_head = sdp->sd_log_flush_head;
575 sdp->sd_log_tail = sdp->sd_log_head;
577 up_write(&sdp->sd_log_flush_lock);