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 <asm/semaphore.h>
19 #include "lm_interface.h"
30 static void do_lock_wait(struct gfs2_sbd
*sdp
, wait_queue_head_t
*wq
,
33 wait_event(*wq
, atomic_read(a
) ? 0 : 1);
36 static void lock_for_trans(struct gfs2_sbd
*sdp
)
38 do_lock_wait(sdp
, &sdp
->sd_log_trans_wq
, &sdp
->sd_log_flush_count
);
39 atomic_inc(&sdp
->sd_log_trans_count
);
42 static void unlock_from_trans(struct gfs2_sbd
*sdp
)
44 gfs2_assert_warn(sdp
, atomic_read(&sdp
->sd_log_trans_count
));
45 if (atomic_dec_and_test(&sdp
->sd_log_trans_count
))
46 wake_up(&sdp
->sd_log_flush_wq
);
49 static void gfs2_lock_for_flush(struct gfs2_sbd
*sdp
)
51 atomic_inc(&sdp
->sd_log_flush_count
);
52 do_lock_wait(sdp
, &sdp
->sd_log_flush_wq
, &sdp
->sd_log_trans_count
);
55 static void gfs2_unlock_from_flush(struct gfs2_sbd
*sdp
)
57 gfs2_assert_warn(sdp
, atomic_read(&sdp
->sd_log_flush_count
));
58 if (atomic_dec_and_test(&sdp
->sd_log_flush_count
))
59 wake_up(&sdp
->sd_log_trans_wq
);
63 * gfs2_struct2blk - compute stuff
64 * @sdp: the filesystem
65 * @nstruct: the number of structures
66 * @ssize: the size of the structures
68 * Compute the number of log descriptor blocks needed to hold a certain number
69 * of structures of a certain size.
71 * Returns: the number of blocks needed (minimum is always 1)
74 unsigned int gfs2_struct2blk(struct gfs2_sbd
*sdp
, unsigned int nstruct
,
78 unsigned int first
, second
;
81 first
= (sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_log_descriptor
)) /
84 if (nstruct
> first
) {
85 second
= (sdp
->sd_sb
.sb_bsize
-
86 sizeof(struct gfs2_meta_header
)) / ssize
;
87 blks
+= DIV_ROUND_UP(nstruct
- first
, second
);
93 void gfs2_ail1_start(struct gfs2_sbd
*sdp
, int flags
)
95 struct list_head
*head
= &sdp
->sd_ail1_list
;
97 struct list_head
*first
, *tmp
;
98 struct gfs2_ail
*first_ai
, *ai
;
101 if (list_empty(head
)) {
102 gfs2_log_unlock(sdp
);
105 sync_gen
= sdp
->sd_ail_sync_gen
++;
108 first_ai
= list_entry(first
, struct gfs2_ail
, ai_list
);
109 first_ai
->ai_sync_gen
= sync_gen
;
110 gfs2_ail1_start_one(sdp
, first_ai
);
117 (head
->prev
!= first
||
118 gfs2_ail1_empty_one(sdp
, first_ai
, 0)))
121 for (tmp
= head
->prev
; tmp
!= head
; tmp
= tmp
->prev
) {
122 ai
= list_entry(tmp
, struct gfs2_ail
, ai_list
);
123 if (ai
->ai_sync_gen
>= sync_gen
)
125 ai
->ai_sync_gen
= sync_gen
;
126 gfs2_ail1_start_one(sdp
, ai
);
134 gfs2_log_unlock(sdp
);
137 int gfs2_ail1_empty(struct gfs2_sbd
*sdp
, int flags
)
139 struct gfs2_ail
*ai
, *s
;
144 list_for_each_entry_safe_reverse(ai
, s
, &sdp
->sd_ail1_list
, ai_list
) {
145 if (gfs2_ail1_empty_one(sdp
, ai
, flags
))
146 list_move(&ai
->ai_list
, &sdp
->sd_ail2_list
);
147 else if (!(flags
& DIO_ALL
))
151 ret
= list_empty(&sdp
->sd_ail1_list
);
153 gfs2_log_unlock(sdp
);
158 static void ail2_empty(struct gfs2_sbd
*sdp
, unsigned int new_tail
)
160 struct gfs2_ail
*ai
, *safe
;
161 unsigned int old_tail
= sdp
->sd_log_tail
;
162 int wrap
= (new_tail
< old_tail
);
167 list_for_each_entry_safe(ai
, safe
, &sdp
->sd_ail2_list
, ai_list
) {
168 a
= (old_tail
<= ai
->ai_first
);
169 b
= (ai
->ai_first
< new_tail
);
170 rm
= (wrap
) ? (a
|| b
) : (a
&& b
);
174 gfs2_ail2_empty_one(sdp
, ai
);
175 list_del(&ai
->ai_list
);
176 gfs2_assert_warn(sdp
, list_empty(&ai
->ai_ail1_list
));
177 gfs2_assert_warn(sdp
, list_empty(&ai
->ai_ail2_list
));
181 gfs2_log_unlock(sdp
);
185 * gfs2_log_reserve - Make a log reservation
186 * @sdp: The GFS2 superblock
187 * @blks: The number of blocks to reserve
192 int gfs2_log_reserve(struct gfs2_sbd
*sdp
, unsigned int blks
)
195 unsigned int try = 0;
197 if (gfs2_assert_warn(sdp
, blks
) ||
198 gfs2_assert_warn(sdp
, blks
<= sdp
->sd_jdesc
->jd_blocks
))
203 if (list_empty(&list
)) {
204 list_add_tail(&list
, &sdp
->sd_log_blks_list
);
205 while (sdp
->sd_log_blks_list
.next
!= &list
) {
206 DECLARE_WAITQUEUE(__wait_chan
, current
);
207 set_current_state(TASK_UNINTERRUPTIBLE
);
208 add_wait_queue(&sdp
->sd_log_blks_wait
,
210 gfs2_log_unlock(sdp
);
213 remove_wait_queue(&sdp
->sd_log_blks_wait
,
215 set_current_state(TASK_RUNNING
);
218 /* Never give away the last block so we can
219 always pull the tail if we need to. */
220 if (sdp
->sd_log_blks_free
> blks
) {
221 sdp
->sd_log_blks_free
-= blks
;
223 gfs2_log_unlock(sdp
);
224 wake_up(&sdp
->sd_log_blks_wait
);
228 gfs2_log_unlock(sdp
);
229 gfs2_ail1_empty(sdp
, 0);
233 gfs2_ail1_start(sdp
, 0);
241 * gfs2_log_release - Release a given number of log blocks
242 * @sdp: The GFS2 superblock
243 * @blks: The number of blocks
247 void gfs2_log_release(struct gfs2_sbd
*sdp
, unsigned int blks
)
249 unlock_from_trans(sdp
);
252 sdp
->sd_log_blks_free
+= blks
;
253 gfs2_assert_withdraw(sdp
,
254 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
255 gfs2_log_unlock(sdp
);
258 static uint64_t log_bmap(struct gfs2_sbd
*sdp
, unsigned int lbn
)
264 error
= gfs2_block_map(sdp
->sd_jdesc
->jd_inode
->u
.generic_ip
,
265 lbn
, &new, &dbn
, NULL
);
266 gfs2_assert_withdraw(sdp
, !error
&& dbn
);
272 * log_distance - Compute distance between two journal blocks
273 * @sdp: The GFS2 superblock
274 * @newer: The most recent journal block of the pair
275 * @older: The older journal block of the pair
277 * Compute the distance (in the journal direction) between two
278 * blocks in the journal
280 * Returns: the distance in blocks
283 static inline unsigned int log_distance(struct gfs2_sbd
*sdp
,
289 dist
= newer
- older
;
291 dist
+= sdp
->sd_jdesc
->jd_blocks
;
296 static unsigned int current_tail(struct gfs2_sbd
*sdp
)
303 if (list_empty(&sdp
->sd_ail1_list
))
304 tail
= sdp
->sd_log_head
;
306 ai
= list_entry(sdp
->sd_ail1_list
.prev
,
307 struct gfs2_ail
, ai_list
);
311 gfs2_log_unlock(sdp
);
316 static inline void log_incr_head(struct gfs2_sbd
*sdp
)
318 if (sdp
->sd_log_flush_head
== sdp
->sd_log_tail
)
319 gfs2_assert_withdraw(sdp
,
320 sdp
->sd_log_flush_head
== sdp
->sd_log_head
);
322 if (++sdp
->sd_log_flush_head
== sdp
->sd_jdesc
->jd_blocks
) {
323 sdp
->sd_log_flush_head
= 0;
324 sdp
->sd_log_flush_wrapped
= 1;
329 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
330 * @sdp: The GFS2 superblock
332 * Returns: the buffer_head
335 struct buffer_head
*gfs2_log_get_buf(struct gfs2_sbd
*sdp
)
337 uint64_t blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
338 struct gfs2_log_buf
*lb
;
339 struct buffer_head
*bh
;
341 lb
= kzalloc(sizeof(struct gfs2_log_buf
), GFP_NOFS
| __GFP_NOFAIL
);
342 list_add(&lb
->lb_list
, &sdp
->sd_log_flush_list
);
344 bh
= lb
->lb_bh
= sb_getblk(sdp
->sd_vfs
, blkno
);
346 memset(bh
->b_data
, 0, bh
->b_size
);
347 set_buffer_uptodate(bh
);
348 clear_buffer_dirty(bh
);
357 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
358 * @sdp: the filesystem
359 * @data: the data the buffer_head should point to
361 * Returns: the log buffer descriptor
364 struct buffer_head
*gfs2_log_fake_buf(struct gfs2_sbd
*sdp
,
365 struct buffer_head
*real
)
367 uint64_t blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
368 struct gfs2_log_buf
*lb
;
369 struct buffer_head
*bh
;
371 lb
= kzalloc(sizeof(struct gfs2_log_buf
), GFP_NOFS
| __GFP_NOFAIL
);
372 list_add(&lb
->lb_list
, &sdp
->sd_log_flush_list
);
375 bh
= lb
->lb_bh
= alloc_buffer_head(GFP_NOFS
| __GFP_NOFAIL
);
376 atomic_set(&bh
->b_count
, 1);
377 bh
->b_state
= (1 << BH_Mapped
) | (1 << BH_Uptodate
);
378 set_bh_page(bh
, real
->b_page
, bh_offset(real
));
379 bh
->b_blocknr
= blkno
;
380 bh
->b_size
= sdp
->sd_sb
.sb_bsize
;
381 bh
->b_bdev
= sdp
->sd_vfs
->s_bdev
;
388 static void log_pull_tail(struct gfs2_sbd
*sdp
, unsigned int new_tail
, int pull
)
390 unsigned int dist
= log_distance(sdp
, new_tail
, sdp
->sd_log_tail
);
392 ail2_empty(sdp
, new_tail
);
395 sdp
->sd_log_blks_free
+= dist
- ((pull
) ? 1 : 0);
396 gfs2_assert_withdraw(sdp
,
397 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
398 gfs2_log_unlock(sdp
);
400 sdp
->sd_log_tail
= new_tail
;
404 * log_write_header - Get and initialize a journal header buffer
405 * @sdp: The GFS2 superblock
407 * Returns: the initialized log buffer descriptor
410 static void log_write_header(struct gfs2_sbd
*sdp
, uint32_t flags
, int pull
)
412 uint64_t blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
413 struct buffer_head
*bh
;
414 struct gfs2_log_header
*lh
;
418 bh
= sb_getblk(sdp
->sd_vfs
, blkno
);
420 memset(bh
->b_data
, 0, bh
->b_size
);
421 set_buffer_uptodate(bh
);
422 clear_buffer_dirty(bh
);
425 gfs2_ail1_empty(sdp
, 0);
426 tail
= current_tail(sdp
);
428 lh
= (struct gfs2_log_header
*)bh
->b_data
;
429 memset(lh
, 0, sizeof(struct gfs2_log_header
));
430 lh
->lh_header
.mh_magic
= cpu_to_be32(GFS2_MAGIC
);
431 lh
->lh_header
.mh_type
= cpu_to_be16(GFS2_METATYPE_LH
);
432 lh
->lh_header
.mh_format
= cpu_to_be16(GFS2_FORMAT_LH
);
433 lh
->lh_sequence
= be64_to_cpu(sdp
->sd_log_sequence
++);
434 lh
->lh_flags
= be32_to_cpu(flags
);
435 lh
->lh_tail
= be32_to_cpu(tail
);
436 lh
->lh_blkno
= be32_to_cpu(sdp
->sd_log_flush_head
);
437 hash
= gfs2_disk_hash(bh
->b_data
, sizeof(struct gfs2_log_header
));
438 lh
->lh_hash
= cpu_to_be32(hash
);
440 set_buffer_dirty(bh
);
441 if (sync_dirty_buffer(bh
))
442 gfs2_io_error_bh(sdp
, bh
);
445 if (sdp
->sd_log_tail
!= tail
)
446 log_pull_tail(sdp
, tail
, pull
);
448 gfs2_assert_withdraw(sdp
, !pull
);
450 sdp
->sd_log_idle
= (tail
== sdp
->sd_log_flush_head
);
454 static void log_flush_commit(struct gfs2_sbd
*sdp
)
456 struct list_head
*head
= &sdp
->sd_log_flush_list
;
457 struct gfs2_log_buf
*lb
;
458 struct buffer_head
*bh
;
461 d
= log_distance(sdp
, sdp
->sd_log_flush_head
, sdp
->sd_log_head
);
463 gfs2_assert_withdraw(sdp
, d
+ 1 == sdp
->sd_log_blks_reserved
);
465 while (!list_empty(head
)) {
466 lb
= list_entry(head
->next
, struct gfs2_log_buf
, lb_list
);
467 list_del(&lb
->lb_list
);
471 if (!buffer_uptodate(bh
))
472 gfs2_io_error_bh(sdp
, bh
);
474 while (atomic_read(&bh
->b_count
) != 1) /* Grrrr... */
476 free_buffer_head(bh
);
482 log_write_header(sdp
, 0, 0);
486 * gfs2_log_flush_i - flush incore transaction(s)
487 * @sdp: the filesystem
488 * @gl: The glock structure to flush. If NULL, flush the whole incore log
492 void gfs2_log_flush_i(struct gfs2_sbd
*sdp
, struct gfs2_glock
*gl
)
496 ai
= kzalloc(sizeof(struct gfs2_ail
), GFP_NOFS
| __GFP_NOFAIL
);
497 INIT_LIST_HEAD(&ai
->ai_ail1_list
);
498 INIT_LIST_HEAD(&ai
->ai_ail2_list
);
499 gfs2_lock_for_flush(sdp
);
503 if (list_empty(&gl
->gl_le
.le_list
)) {
504 gfs2_log_unlock(sdp
);
505 gfs2_unlock_from_flush(sdp
);
509 gfs2_log_unlock(sdp
);
512 mutex_lock(&sdp
->sd_log_flush_lock
);
514 gfs2_assert_withdraw(sdp
,
515 sdp
->sd_log_num_buf
== sdp
->sd_log_commited_buf
);
516 gfs2_assert_withdraw(sdp
,
517 sdp
->sd_log_num_revoke
== sdp
->sd_log_commited_revoke
);
519 sdp
->sd_log_flush_head
= sdp
->sd_log_head
;
520 sdp
->sd_log_flush_wrapped
= 0;
521 ai
->ai_first
= sdp
->sd_log_flush_head
;
523 lops_before_commit(sdp
);
524 if (!list_empty(&sdp
->sd_log_flush_list
))
525 log_flush_commit(sdp
);
526 else if (sdp
->sd_log_tail
!= current_tail(sdp
) && !sdp
->sd_log_idle
)
527 log_write_header(sdp
, 0, PULL
);
528 lops_after_commit(sdp
, ai
);
529 sdp
->sd_log_head
= sdp
->sd_log_flush_head
;
530 if (sdp
->sd_log_flush_wrapped
)
533 sdp
->sd_log_blks_reserved
=
534 sdp
->sd_log_commited_buf
=
535 sdp
->sd_log_commited_revoke
= 0;
538 if (!list_empty(&ai
->ai_ail1_list
)) {
539 list_add(&ai
->ai_list
, &sdp
->sd_ail1_list
);
542 gfs2_log_unlock(sdp
);
544 mutex_unlock(&sdp
->sd_log_flush_lock
);
545 sdp
->sd_vfs
->s_dirt
= 0;
546 gfs2_unlock_from_flush(sdp
);
551 static void log_refund(struct gfs2_sbd
*sdp
, struct gfs2_trans
*tr
)
553 unsigned int reserved
= 1;
558 sdp
->sd_log_commited_buf
+= tr
->tr_num_buf_new
- tr
->tr_num_buf_rm
;
559 gfs2_assert_withdraw(sdp
, ((int)sdp
->sd_log_commited_buf
) >= 0);
560 sdp
->sd_log_commited_revoke
+= tr
->tr_num_revoke
- tr
->tr_num_revoke_rm
;
561 gfs2_assert_withdraw(sdp
, ((int)sdp
->sd_log_commited_revoke
) >= 0);
563 if (sdp
->sd_log_commited_buf
)
564 reserved
+= 1 + sdp
->sd_log_commited_buf
+
565 sdp
->sd_log_commited_buf
/503;
566 if (sdp
->sd_log_commited_revoke
)
567 reserved
+= gfs2_struct2blk(sdp
, sdp
->sd_log_commited_revoke
,
570 old
= sdp
->sd_log_blks_free
;
571 sdp
->sd_log_blks_free
+= tr
->tr_reserved
-
572 (reserved
- sdp
->sd_log_blks_reserved
);
574 gfs2_assert_withdraw(sdp
,
575 sdp
->sd_log_blks_free
>= old
);
576 gfs2_assert_withdraw(sdp
,
577 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
579 sdp
->sd_log_blks_reserved
= reserved
;
581 gfs2_log_unlock(sdp
);
585 * gfs2_log_commit - Commit a transaction to the log
586 * @sdp: the filesystem
587 * @tr: the transaction
592 void gfs2_log_commit(struct gfs2_sbd
*sdp
, struct gfs2_trans
*tr
)
595 lops_incore_commit(sdp
, tr
);
597 sdp
->sd_vfs
->s_dirt
= 1;
598 unlock_from_trans(sdp
);
603 if (sdp
->sd_log_num_buf
> gfs2_tune_get(sdp
, gt_incore_log_blocks
)) {
604 gfs2_log_unlock(sdp
);
607 gfs2_log_unlock(sdp
);
611 * gfs2_log_shutdown - write a shutdown header into a journal
612 * @sdp: the filesystem
616 void gfs2_log_shutdown(struct gfs2_sbd
*sdp
)
618 mutex_lock(&sdp
->sd_log_flush_lock
);
620 gfs2_assert_withdraw(sdp
, !atomic_read(&sdp
->sd_log_trans_count
));
621 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_blks_reserved
);
622 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_gl
);
623 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_buf
);
624 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_jdata
);
625 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_revoke
);
626 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_rg
);
627 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_databuf
);
628 gfs2_assert_withdraw(sdp
, list_empty(&sdp
->sd_ail1_list
));
630 sdp
->sd_log_flush_head
= sdp
->sd_log_head
;
631 sdp
->sd_log_flush_wrapped
= 0;
633 log_write_header(sdp
, GFS2_LOG_HEAD_UNMOUNT
, 0);
635 gfs2_assert_withdraw(sdp
, sdp
->sd_log_blks_free
==
636 sdp
->sd_jdesc
->jd_blocks
);
637 gfs2_assert_withdraw(sdp
, sdp
->sd_log_head
== sdp
->sd_log_tail
);
638 gfs2_assert_withdraw(sdp
, list_empty(&sdp
->sd_ail2_list
));
640 sdp
->sd_log_head
= sdp
->sd_log_flush_head
;
641 if (sdp
->sd_log_flush_wrapped
)
643 sdp
->sd_log_tail
= sdp
->sd_log_head
;
645 mutex_unlock(&sdp
->sd_log_flush_lock
);