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>
18 #include <linux/delay.h>
33 * gfs2_struct2blk - compute stuff
34 * @sdp: the filesystem
35 * @nstruct: the number of structures
36 * @ssize: the size of the structures
38 * Compute the number of log descriptor blocks needed to hold a certain number
39 * of structures of a certain size.
41 * Returns: the number of blocks needed (minimum is always 1)
44 unsigned int gfs2_struct2blk(struct gfs2_sbd
*sdp
, unsigned int nstruct
,
48 unsigned int first
, second
;
51 first
= (sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_log_descriptor
)) / ssize
;
53 if (nstruct
> first
) {
54 second
= (sdp
->sd_sb
.sb_bsize
-
55 sizeof(struct gfs2_meta_header
)) / ssize
;
56 blks
+= DIV_ROUND_UP(nstruct
- first
, second
);
63 * gfs2_ail1_start_one - Start I/O on a part of the AIL
64 * @sdp: the filesystem
65 * @tr: the part of the AIL
69 static void gfs2_ail1_start_one(struct gfs2_sbd
*sdp
, struct gfs2_ail
*ai
)
71 struct gfs2_bufdata
*bd
, *s
;
72 struct buffer_head
*bh
;
75 BUG_ON(!spin_is_locked(&sdp
->sd_log_lock
));
80 list_for_each_entry_safe_reverse(bd
, s
, &ai
->ai_ail1_list
,
84 gfs2_assert(sdp
, bd
->bd_ail
== ai
);
87 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail2_list
);
91 if (!buffer_busy(bh
)) {
92 if (!buffer_uptodate(bh
)) {
94 gfs2_io_error_bh(sdp
, bh
);
97 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail2_list
);
101 if (!buffer_dirty(bh
))
104 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail1_list
);
106 gfs2_log_unlock(sdp
);
108 ll_rw_block(WRITE
, 1, &bh
);
118 * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
119 * @sdp: the filesystem
124 static int gfs2_ail1_empty_one(struct gfs2_sbd
*sdp
, struct gfs2_ail
*ai
, int flags
)
126 struct gfs2_bufdata
*bd
, *s
;
127 struct buffer_head
*bh
;
129 list_for_each_entry_safe_reverse(bd
, s
, &ai
->ai_ail1_list
,
134 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail2_list
);
138 gfs2_assert(sdp
, bd
->bd_ail
== ai
);
140 if (buffer_busy(bh
)) {
147 if (!buffer_uptodate(bh
))
148 gfs2_io_error_bh(sdp
, bh
);
150 list_move(&bd
->bd_ail_st_list
, &ai
->ai_ail2_list
);
153 return list_empty(&ai
->ai_ail1_list
);
156 static void gfs2_ail1_start(struct gfs2_sbd
*sdp
, int flags
)
158 struct list_head
*head
= &sdp
->sd_ail1_list
;
160 struct list_head
*first
;
161 struct gfs2_ail
*first_ai
, *ai
, *tmp
;
165 if (list_empty(head
)) {
166 gfs2_log_unlock(sdp
);
169 sync_gen
= sdp
->sd_ail_sync_gen
++;
172 first_ai
= list_entry(first
, struct gfs2_ail
, ai_list
);
173 first_ai
->ai_sync_gen
= sync_gen
;
174 gfs2_ail1_start_one(sdp
, first_ai
); /* This may drop log lock */
180 if (first
&& (head
->prev
!= first
||
181 gfs2_ail1_empty_one(sdp
, first_ai
, 0)))
185 list_for_each_entry_safe_reverse(ai
, tmp
, head
, ai_list
) {
186 if (ai
->ai_sync_gen
>= sync_gen
)
188 ai
->ai_sync_gen
= sync_gen
;
189 gfs2_ail1_start_one(sdp
, ai
); /* This may drop log lock */
195 gfs2_log_unlock(sdp
);
198 int gfs2_ail1_empty(struct gfs2_sbd
*sdp
, int flags
)
200 struct gfs2_ail
*ai
, *s
;
205 list_for_each_entry_safe_reverse(ai
, s
, &sdp
->sd_ail1_list
, ai_list
) {
206 if (gfs2_ail1_empty_one(sdp
, ai
, flags
))
207 list_move(&ai
->ai_list
, &sdp
->sd_ail2_list
);
208 else if (!(flags
& DIO_ALL
))
212 ret
= list_empty(&sdp
->sd_ail1_list
);
214 gfs2_log_unlock(sdp
);
221 * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
222 * @sdp: the filesystem
227 static void gfs2_ail2_empty_one(struct gfs2_sbd
*sdp
, struct gfs2_ail
*ai
)
229 struct list_head
*head
= &ai
->ai_ail2_list
;
230 struct gfs2_bufdata
*bd
;
232 while (!list_empty(head
)) {
233 bd
= list_entry(head
->prev
, struct gfs2_bufdata
,
235 gfs2_assert(sdp
, bd
->bd_ail
== ai
);
237 list_del(&bd
->bd_ail_st_list
);
238 list_del(&bd
->bd_ail_gl_list
);
239 atomic_dec(&bd
->bd_gl
->gl_ail_count
);
243 kmem_cache_free(gfs2_bufdata_cachep
, bd
);
247 static void ail2_empty(struct gfs2_sbd
*sdp
, unsigned int new_tail
)
249 struct gfs2_ail
*ai
, *safe
;
250 unsigned int old_tail
= sdp
->sd_log_tail
;
251 int wrap
= (new_tail
< old_tail
);
256 list_for_each_entry_safe(ai
, safe
, &sdp
->sd_ail2_list
, ai_list
) {
257 a
= (old_tail
<= ai
->ai_first
);
258 b
= (ai
->ai_first
< new_tail
);
259 rm
= (wrap
) ? (a
|| b
) : (a
&& b
);
263 gfs2_ail2_empty_one(sdp
, ai
);
264 list_del(&ai
->ai_list
);
265 gfs2_assert_warn(sdp
, list_empty(&ai
->ai_ail1_list
));
266 gfs2_assert_warn(sdp
, list_empty(&ai
->ai_ail2_list
));
270 gfs2_log_unlock(sdp
);
274 * gfs2_log_reserve - Make a log reservation
275 * @sdp: The GFS2 superblock
276 * @blks: The number of blocks to reserve
278 * Note that we never give out the last few blocks of the journal. Thats
279 * due to the fact that there is are a small number of header blocks
280 * associated with each log flush. The exact number can't be known until
281 * flush time, so we ensure that we have just enough free blocks at all
282 * times to avoid running out during a log flush.
287 int gfs2_log_reserve(struct gfs2_sbd
*sdp
, unsigned int blks
)
289 unsigned int try = 0;
290 unsigned reserved_blks
= 6 * (4096 / sdp
->sd_vfs
->s_blocksize
);
292 if (gfs2_assert_warn(sdp
, blks
) ||
293 gfs2_assert_warn(sdp
, blks
<= sdp
->sd_jdesc
->jd_blocks
))
296 mutex_lock(&sdp
->sd_log_reserve_mutex
);
298 while(sdp
->sd_log_blks_free
<= (blks
+ reserved_blks
)) {
299 gfs2_log_unlock(sdp
);
300 gfs2_ail1_empty(sdp
, 0);
301 gfs2_log_flush(sdp
, NULL
);
304 gfs2_ail1_start(sdp
, 0);
307 sdp
->sd_log_blks_free
-= blks
;
308 gfs2_log_unlock(sdp
);
309 mutex_unlock(&sdp
->sd_log_reserve_mutex
);
311 down_read(&sdp
->sd_log_flush_lock
);
317 * gfs2_log_release - Release a given number of log blocks
318 * @sdp: The GFS2 superblock
319 * @blks: The number of blocks
323 void gfs2_log_release(struct gfs2_sbd
*sdp
, unsigned int blks
)
327 sdp
->sd_log_blks_free
+= blks
;
328 gfs2_assert_withdraw(sdp
,
329 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
330 gfs2_log_unlock(sdp
);
331 up_read(&sdp
->sd_log_flush_lock
);
334 static u64
log_bmap(struct gfs2_sbd
*sdp
, unsigned int lbn
)
336 struct inode
*inode
= sdp
->sd_jdesc
->jd_inode
;
338 struct buffer_head bh_map
= { .b_state
= 0, .b_blocknr
= 0 };
340 bh_map
.b_size
= 1 << inode
->i_blkbits
;
341 error
= gfs2_block_map(inode
, lbn
, 0, &bh_map
);
342 if (error
|| !bh_map
.b_blocknr
)
343 printk(KERN_INFO
"error=%d, dbn=%llu lbn=%u", error
,
344 (unsigned long long)bh_map
.b_blocknr
, lbn
);
345 gfs2_assert_withdraw(sdp
, !error
&& bh_map
.b_blocknr
);
347 return bh_map
.b_blocknr
;
351 * log_distance - Compute distance between two journal blocks
352 * @sdp: The GFS2 superblock
353 * @newer: The most recent journal block of the pair
354 * @older: The older journal block of the pair
356 * Compute the distance (in the journal direction) between two
357 * blocks in the journal
359 * Returns: the distance in blocks
362 static inline unsigned int log_distance(struct gfs2_sbd
*sdp
, unsigned int newer
,
367 dist
= newer
- older
;
369 dist
+= sdp
->sd_jdesc
->jd_blocks
;
374 static unsigned int current_tail(struct gfs2_sbd
*sdp
)
381 if (list_empty(&sdp
->sd_ail1_list
)) {
382 tail
= sdp
->sd_log_head
;
384 ai
= list_entry(sdp
->sd_ail1_list
.prev
, struct gfs2_ail
, ai_list
);
388 gfs2_log_unlock(sdp
);
393 static inline void log_incr_head(struct gfs2_sbd
*sdp
)
395 if (sdp
->sd_log_flush_head
== sdp
->sd_log_tail
)
396 gfs2_assert_withdraw(sdp
, sdp
->sd_log_flush_head
== sdp
->sd_log_head
);
398 if (++sdp
->sd_log_flush_head
== sdp
->sd_jdesc
->jd_blocks
) {
399 sdp
->sd_log_flush_head
= 0;
400 sdp
->sd_log_flush_wrapped
= 1;
405 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
406 * @sdp: The GFS2 superblock
408 * Returns: the buffer_head
411 struct buffer_head
*gfs2_log_get_buf(struct gfs2_sbd
*sdp
)
413 u64 blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
414 struct gfs2_log_buf
*lb
;
415 struct buffer_head
*bh
;
417 lb
= kzalloc(sizeof(struct gfs2_log_buf
), GFP_NOFS
| __GFP_NOFAIL
);
418 list_add(&lb
->lb_list
, &sdp
->sd_log_flush_list
);
420 bh
= lb
->lb_bh
= sb_getblk(sdp
->sd_vfs
, blkno
);
422 memset(bh
->b_data
, 0, bh
->b_size
);
423 set_buffer_uptodate(bh
);
424 clear_buffer_dirty(bh
);
433 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
434 * @sdp: the filesystem
435 * @data: the data the buffer_head should point to
437 * Returns: the log buffer descriptor
440 struct buffer_head
*gfs2_log_fake_buf(struct gfs2_sbd
*sdp
,
441 struct buffer_head
*real
)
443 u64 blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
444 struct gfs2_log_buf
*lb
;
445 struct buffer_head
*bh
;
447 lb
= kzalloc(sizeof(struct gfs2_log_buf
), GFP_NOFS
| __GFP_NOFAIL
);
448 list_add(&lb
->lb_list
, &sdp
->sd_log_flush_list
);
451 bh
= lb
->lb_bh
= alloc_buffer_head(GFP_NOFS
| __GFP_NOFAIL
);
452 atomic_set(&bh
->b_count
, 1);
453 bh
->b_state
= (1 << BH_Mapped
) | (1 << BH_Uptodate
);
454 set_bh_page(bh
, real
->b_page
, bh_offset(real
));
455 bh
->b_blocknr
= blkno
;
456 bh
->b_size
= sdp
->sd_sb
.sb_bsize
;
457 bh
->b_bdev
= sdp
->sd_vfs
->s_bdev
;
464 static void log_pull_tail(struct gfs2_sbd
*sdp
, unsigned int new_tail
, int pull
)
466 unsigned int dist
= log_distance(sdp
, new_tail
, sdp
->sd_log_tail
);
468 ail2_empty(sdp
, new_tail
);
471 sdp
->sd_log_blks_free
+= dist
- (pull
? 1 : 0);
472 gfs2_assert_withdraw(sdp
, sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
473 gfs2_log_unlock(sdp
);
475 sdp
->sd_log_tail
= new_tail
;
479 * log_write_header - Get and initialize a journal header buffer
480 * @sdp: The GFS2 superblock
482 * Returns: the initialized log buffer descriptor
485 static void log_write_header(struct gfs2_sbd
*sdp
, u32 flags
, int pull
)
487 u64 blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
488 struct buffer_head
*bh
;
489 struct gfs2_log_header
*lh
;
493 bh
= sb_getblk(sdp
->sd_vfs
, blkno
);
495 memset(bh
->b_data
, 0, bh
->b_size
);
496 set_buffer_uptodate(bh
);
497 clear_buffer_dirty(bh
);
500 gfs2_ail1_empty(sdp
, 0);
501 tail
= current_tail(sdp
);
503 lh
= (struct gfs2_log_header
*)bh
->b_data
;
504 memset(lh
, 0, sizeof(struct gfs2_log_header
));
505 lh
->lh_header
.mh_magic
= cpu_to_be32(GFS2_MAGIC
);
506 lh
->lh_header
.mh_type
= cpu_to_be32(GFS2_METATYPE_LH
);
507 lh
->lh_header
.mh_format
= cpu_to_be32(GFS2_FORMAT_LH
);
508 lh
->lh_sequence
= cpu_to_be64(sdp
->sd_log_sequence
++);
509 lh
->lh_flags
= cpu_to_be32(flags
);
510 lh
->lh_tail
= cpu_to_be32(tail
);
511 lh
->lh_blkno
= cpu_to_be32(sdp
->sd_log_flush_head
);
512 hash
= gfs2_disk_hash(bh
->b_data
, sizeof(struct gfs2_log_header
));
513 lh
->lh_hash
= cpu_to_be32(hash
);
515 set_buffer_dirty(bh
);
516 if (sync_dirty_buffer(bh
))
517 gfs2_io_error_bh(sdp
, bh
);
520 if (sdp
->sd_log_tail
!= tail
)
521 log_pull_tail(sdp
, tail
, pull
);
523 gfs2_assert_withdraw(sdp
, !pull
);
525 sdp
->sd_log_idle
= (tail
== sdp
->sd_log_flush_head
);
529 static void log_flush_commit(struct gfs2_sbd
*sdp
)
531 struct list_head
*head
= &sdp
->sd_log_flush_list
;
532 struct gfs2_log_buf
*lb
;
533 struct buffer_head
*bh
;
535 while (!list_empty(head
)) {
536 lb
= list_entry(head
->next
, struct gfs2_log_buf
, lb_list
);
537 list_del(&lb
->lb_list
);
541 if (!buffer_uptodate(bh
))
542 gfs2_io_error_bh(sdp
, bh
);
544 while (atomic_read(&bh
->b_count
) != 1) /* Grrrr... */
546 free_buffer_head(bh
);
552 log_write_header(sdp
, 0, 0);
556 * gfs2_log_flush - flush incore transaction(s)
557 * @sdp: the filesystem
558 * @gl: The glock structure to flush. If NULL, flush the whole incore log
562 void gfs2_log_flush(struct gfs2_sbd
*sdp
, struct gfs2_glock
*gl
)
566 down_write(&sdp
->sd_log_flush_lock
);
570 if (list_empty(&gl
->gl_le
.le_list
)) {
571 gfs2_log_unlock(sdp
);
572 up_write(&sdp
->sd_log_flush_lock
);
575 gfs2_log_unlock(sdp
);
578 ai
= kzalloc(sizeof(struct gfs2_ail
), GFP_NOFS
| __GFP_NOFAIL
);
579 INIT_LIST_HEAD(&ai
->ai_ail1_list
);
580 INIT_LIST_HEAD(&ai
->ai_ail2_list
);
582 gfs2_assert_withdraw(sdp
, sdp
->sd_log_num_buf
+ sdp
->sd_log_num_jdata
== sdp
->sd_log_commited_buf
);
583 gfs2_assert_withdraw(sdp
,
584 sdp
->sd_log_num_revoke
== sdp
->sd_log_commited_revoke
);
586 sdp
->sd_log_flush_head
= sdp
->sd_log_head
;
587 sdp
->sd_log_flush_wrapped
= 0;
588 ai
->ai_first
= sdp
->sd_log_flush_head
;
590 lops_before_commit(sdp
);
591 if (!list_empty(&sdp
->sd_log_flush_list
))
592 log_flush_commit(sdp
);
593 else if (sdp
->sd_log_tail
!= current_tail(sdp
) && !sdp
->sd_log_idle
)
594 log_write_header(sdp
, 0, PULL
);
595 lops_after_commit(sdp
, ai
);
598 sdp
->sd_log_head
= sdp
->sd_log_flush_head
;
599 sdp
->sd_log_blks_free
-= sdp
->sd_log_num_hdrs
;
600 sdp
->sd_log_blks_reserved
= 0;
601 sdp
->sd_log_commited_buf
= 0;
602 sdp
->sd_log_num_hdrs
= 0;
603 sdp
->sd_log_commited_revoke
= 0;
605 if (!list_empty(&ai
->ai_ail1_list
)) {
606 list_add(&ai
->ai_list
, &sdp
->sd_ail1_list
);
609 gfs2_log_unlock(sdp
);
611 sdp
->sd_vfs
->s_dirt
= 0;
612 up_write(&sdp
->sd_log_flush_lock
);
617 static void log_refund(struct gfs2_sbd
*sdp
, struct gfs2_trans
*tr
)
619 unsigned int reserved
= 0;
624 sdp
->sd_log_commited_buf
+= tr
->tr_num_buf_new
- tr
->tr_num_buf_rm
;
625 gfs2_assert_withdraw(sdp
, ((int)sdp
->sd_log_commited_buf
) >= 0);
626 sdp
->sd_log_commited_revoke
+= tr
->tr_num_revoke
- tr
->tr_num_revoke_rm
;
627 gfs2_assert_withdraw(sdp
, ((int)sdp
->sd_log_commited_revoke
) >= 0);
629 if (sdp
->sd_log_commited_buf
)
630 reserved
+= sdp
->sd_log_commited_buf
;
631 if (sdp
->sd_log_commited_revoke
)
632 reserved
+= gfs2_struct2blk(sdp
, sdp
->sd_log_commited_revoke
,
637 old
= sdp
->sd_log_blks_free
;
638 sdp
->sd_log_blks_free
+= tr
->tr_reserved
-
639 (reserved
- sdp
->sd_log_blks_reserved
);
641 gfs2_assert_withdraw(sdp
, sdp
->sd_log_blks_free
>= old
);
642 gfs2_assert_withdraw(sdp
,
643 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
+
644 sdp
->sd_log_num_hdrs
);
646 sdp
->sd_log_blks_reserved
= reserved
;
648 gfs2_log_unlock(sdp
);
652 * gfs2_log_commit - Commit a transaction to the log
653 * @sdp: the filesystem
654 * @tr: the transaction
659 void gfs2_log_commit(struct gfs2_sbd
*sdp
, struct gfs2_trans
*tr
)
662 lops_incore_commit(sdp
, tr
);
664 sdp
->sd_vfs
->s_dirt
= 1;
665 up_read(&sdp
->sd_log_flush_lock
);
668 if (sdp
->sd_log_num_buf
> gfs2_tune_get(sdp
, gt_incore_log_blocks
))
669 wake_up_process(sdp
->sd_logd_process
);
670 gfs2_log_unlock(sdp
);
674 * gfs2_log_shutdown - write a shutdown header into a journal
675 * @sdp: the filesystem
679 void gfs2_log_shutdown(struct gfs2_sbd
*sdp
)
681 down_write(&sdp
->sd_log_flush_lock
);
683 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_blks_reserved
);
684 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_gl
);
685 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_buf
);
686 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_jdata
);
687 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_revoke
);
688 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_rg
);
689 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_databuf
);
690 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_hdrs
);
691 gfs2_assert_withdraw(sdp
, list_empty(&sdp
->sd_ail1_list
));
693 sdp
->sd_log_flush_head
= sdp
->sd_log_head
;
694 sdp
->sd_log_flush_wrapped
= 0;
696 log_write_header(sdp
, GFS2_LOG_HEAD_UNMOUNT
, 0);
698 gfs2_assert_warn(sdp
, sdp
->sd_log_blks_free
== sdp
->sd_jdesc
->jd_blocks
);
699 gfs2_assert_warn(sdp
, sdp
->sd_log_head
== sdp
->sd_log_tail
);
700 gfs2_assert_warn(sdp
, list_empty(&sdp
->sd_ail2_list
));
702 sdp
->sd_log_head
= sdp
->sd_log_flush_head
;
703 sdp
->sd_log_tail
= sdp
->sd_log_head
;
705 up_write(&sdp
->sd_log_flush_lock
);
710 * gfs2_meta_syncfs - sync all the buffers in a filesystem
711 * @sdp: the filesystem
715 void gfs2_meta_syncfs(struct gfs2_sbd
*sdp
)
717 gfs2_log_flush(sdp
, NULL
);
719 gfs2_ail1_start(sdp
, DIO_ALL
);
720 if (gfs2_ail1_empty(sdp
, DIO_ALL
))
This page took 0.061727 seconds and 6 git commands to generate.