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
)) /
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
);
62 void gfs2_ail1_start(struct gfs2_sbd
*sdp
, int flags
)
64 struct list_head
*head
= &sdp
->sd_ail1_list
;
66 struct list_head
*first
, *tmp
;
67 struct gfs2_ail
*first_ai
, *ai
;
70 if (list_empty(head
)) {
74 sync_gen
= sdp
->sd_ail_sync_gen
++;
77 first_ai
= list_entry(first
, struct gfs2_ail
, ai_list
);
78 first_ai
->ai_sync_gen
= sync_gen
;
79 gfs2_ail1_start_one(sdp
, first_ai
);
85 if (first
&& (head
->prev
!= first
||
86 gfs2_ail1_empty_one(sdp
, first_ai
, 0)))
89 for (tmp
= head
->prev
; tmp
!= head
; tmp
= tmp
->prev
) {
90 ai
= list_entry(tmp
, struct gfs2_ail
, ai_list
);
91 if (ai
->ai_sync_gen
>= sync_gen
)
93 ai
->ai_sync_gen
= sync_gen
;
94 gfs2_ail1_start_one(sdp
, ai
);
102 gfs2_log_unlock(sdp
);
105 int gfs2_ail1_empty(struct gfs2_sbd
*sdp
, int flags
)
107 struct gfs2_ail
*ai
, *s
;
112 list_for_each_entry_safe_reverse(ai
, s
, &sdp
->sd_ail1_list
, ai_list
) {
113 if (gfs2_ail1_empty_one(sdp
, ai
, flags
))
114 list_move(&ai
->ai_list
, &sdp
->sd_ail2_list
);
115 else if (!(flags
& DIO_ALL
))
119 ret
= list_empty(&sdp
->sd_ail1_list
);
121 gfs2_log_unlock(sdp
);
126 static void ail2_empty(struct gfs2_sbd
*sdp
, unsigned int new_tail
)
128 struct gfs2_ail
*ai
, *safe
;
129 unsigned int old_tail
= sdp
->sd_log_tail
;
130 int wrap
= (new_tail
< old_tail
);
135 list_for_each_entry_safe(ai
, safe
, &sdp
->sd_ail2_list
, ai_list
) {
136 a
= (old_tail
<= ai
->ai_first
);
137 b
= (ai
->ai_first
< new_tail
);
138 rm
= (wrap
) ? (a
|| b
) : (a
&& b
);
142 gfs2_ail2_empty_one(sdp
, ai
);
143 list_del(&ai
->ai_list
);
144 gfs2_assert_warn(sdp
, list_empty(&ai
->ai_ail1_list
));
145 gfs2_assert_warn(sdp
, list_empty(&ai
->ai_ail2_list
));
149 gfs2_log_unlock(sdp
);
153 * gfs2_log_reserve - Make a log reservation
154 * @sdp: The GFS2 superblock
155 * @blks: The number of blocks to reserve
160 int gfs2_log_reserve(struct gfs2_sbd
*sdp
, unsigned int blks
)
162 unsigned int try = 0;
164 if (gfs2_assert_warn(sdp
, blks
) ||
165 gfs2_assert_warn(sdp
, blks
<= sdp
->sd_jdesc
->jd_blocks
))
168 mutex_lock(&sdp
->sd_log_reserve_mutex
);
170 while(sdp
->sd_log_blks_free
<= blks
) {
171 gfs2_log_unlock(sdp
);
172 gfs2_ail1_empty(sdp
, 0);
173 gfs2_log_flush(sdp
, NULL
);
176 gfs2_ail1_start(sdp
, 0);
179 sdp
->sd_log_blks_free
-= blks
;
180 gfs2_log_unlock(sdp
);
181 mutex_unlock(&sdp
->sd_log_reserve_mutex
);
183 down_read(&sdp
->sd_log_flush_lock
);
189 * gfs2_log_release - Release a given number of log blocks
190 * @sdp: The GFS2 superblock
191 * @blks: The number of blocks
195 void gfs2_log_release(struct gfs2_sbd
*sdp
, unsigned int blks
)
199 sdp
->sd_log_blks_free
+= blks
;
200 gfs2_assert_withdraw(sdp
,
201 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
202 gfs2_log_unlock(sdp
);
203 up_read(&sdp
->sd_log_flush_lock
);
206 static u64
log_bmap(struct gfs2_sbd
*sdp
, unsigned int lbn
)
213 error
= gfs2_block_map(sdp
->sd_jdesc
->jd_inode
, lbn
, &new, &dbn
, &bdy
);
215 printk(KERN_INFO
"error=%d, dbn=%llu lbn=%u", error
, (unsigned long long)dbn
, lbn
);
216 gfs2_assert_withdraw(sdp
, !error
&& dbn
);
222 * log_distance - Compute distance between two journal blocks
223 * @sdp: The GFS2 superblock
224 * @newer: The most recent journal block of the pair
225 * @older: The older journal block of the pair
227 * Compute the distance (in the journal direction) between two
228 * blocks in the journal
230 * Returns: the distance in blocks
233 static inline unsigned int log_distance(struct gfs2_sbd
*sdp
,
239 dist
= newer
- older
;
241 dist
+= sdp
->sd_jdesc
->jd_blocks
;
246 static unsigned int current_tail(struct gfs2_sbd
*sdp
)
253 if (list_empty(&sdp
->sd_ail1_list
))
254 tail
= sdp
->sd_log_head
;
256 ai
= list_entry(sdp
->sd_ail1_list
.prev
, struct gfs2_ail
,
261 gfs2_log_unlock(sdp
);
266 static inline void log_incr_head(struct gfs2_sbd
*sdp
)
268 if (sdp
->sd_log_flush_head
== sdp
->sd_log_tail
)
269 gfs2_assert_withdraw(sdp
,
270 sdp
->sd_log_flush_head
== sdp
->sd_log_head
);
272 if (++sdp
->sd_log_flush_head
== sdp
->sd_jdesc
->jd_blocks
) {
273 sdp
->sd_log_flush_head
= 0;
274 sdp
->sd_log_flush_wrapped
= 1;
279 * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
280 * @sdp: The GFS2 superblock
282 * Returns: the buffer_head
285 struct buffer_head
*gfs2_log_get_buf(struct gfs2_sbd
*sdp
)
287 u64 blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
288 struct gfs2_log_buf
*lb
;
289 struct buffer_head
*bh
;
291 lb
= kzalloc(sizeof(struct gfs2_log_buf
), GFP_NOFS
| __GFP_NOFAIL
);
292 list_add(&lb
->lb_list
, &sdp
->sd_log_flush_list
);
294 bh
= lb
->lb_bh
= sb_getblk(sdp
->sd_vfs
, blkno
);
296 memset(bh
->b_data
, 0, bh
->b_size
);
297 set_buffer_uptodate(bh
);
298 clear_buffer_dirty(bh
);
307 * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
308 * @sdp: the filesystem
309 * @data: the data the buffer_head should point to
311 * Returns: the log buffer descriptor
314 struct buffer_head
*gfs2_log_fake_buf(struct gfs2_sbd
*sdp
,
315 struct buffer_head
*real
)
317 u64 blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
318 struct gfs2_log_buf
*lb
;
319 struct buffer_head
*bh
;
321 lb
= kzalloc(sizeof(struct gfs2_log_buf
), GFP_NOFS
| __GFP_NOFAIL
);
322 list_add(&lb
->lb_list
, &sdp
->sd_log_flush_list
);
325 bh
= lb
->lb_bh
= alloc_buffer_head(GFP_NOFS
| __GFP_NOFAIL
);
326 atomic_set(&bh
->b_count
, 1);
327 bh
->b_state
= (1 << BH_Mapped
) | (1 << BH_Uptodate
);
328 set_bh_page(bh
, real
->b_page
, bh_offset(real
));
329 bh
->b_blocknr
= blkno
;
330 bh
->b_size
= sdp
->sd_sb
.sb_bsize
;
331 bh
->b_bdev
= sdp
->sd_vfs
->s_bdev
;
338 static void log_pull_tail(struct gfs2_sbd
*sdp
, unsigned int new_tail
, int pull
)
340 unsigned int dist
= log_distance(sdp
, new_tail
, sdp
->sd_log_tail
);
342 ail2_empty(sdp
, new_tail
);
345 sdp
->sd_log_blks_free
+= dist
- (pull
? 1 : 0);
346 /* printk(KERN_INFO "pull tail refunding %u blocks (%u left) pull=%d\n", dist - (pull ? 1 : 0), sdp->sd_log_blks_free, pull); */
347 gfs2_assert_withdraw(sdp
,
348 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
);
349 gfs2_log_unlock(sdp
);
351 sdp
->sd_log_tail
= new_tail
;
355 * log_write_header - Get and initialize a journal header buffer
356 * @sdp: The GFS2 superblock
358 * Returns: the initialized log buffer descriptor
361 static void log_write_header(struct gfs2_sbd
*sdp
, u32 flags
, int pull
)
363 u64 blkno
= log_bmap(sdp
, sdp
->sd_log_flush_head
);
364 struct buffer_head
*bh
;
365 struct gfs2_log_header
*lh
;
369 /* printk(KERN_INFO "log write header start (flags=%08x, pull=%d)\n", flags, pull); */
371 bh
= sb_getblk(sdp
->sd_vfs
, blkno
);
373 memset(bh
->b_data
, 0, bh
->b_size
);
374 set_buffer_uptodate(bh
);
375 clear_buffer_dirty(bh
);
378 gfs2_ail1_empty(sdp
, 0);
379 tail
= current_tail(sdp
);
381 lh
= (struct gfs2_log_header
*)bh
->b_data
;
382 memset(lh
, 0, sizeof(struct gfs2_log_header
));
383 lh
->lh_header
.mh_magic
= cpu_to_be32(GFS2_MAGIC
);
384 lh
->lh_header
.mh_type
= cpu_to_be32(GFS2_METATYPE_LH
);
385 lh
->lh_header
.mh_format
= cpu_to_be32(GFS2_FORMAT_LH
);
386 lh
->lh_sequence
= cpu_to_be64(sdp
->sd_log_sequence
++);
387 lh
->lh_flags
= cpu_to_be32(flags
);
388 lh
->lh_tail
= cpu_to_be32(tail
);
389 lh
->lh_blkno
= cpu_to_be32(sdp
->sd_log_flush_head
);
390 hash
= gfs2_disk_hash(bh
->b_data
, sizeof(struct gfs2_log_header
));
391 lh
->lh_hash
= cpu_to_be32(hash
);
393 set_buffer_dirty(bh
);
394 if (sync_dirty_buffer(bh
))
395 gfs2_io_error_bh(sdp
, bh
);
398 if (sdp
->sd_log_tail
!= tail
)
399 log_pull_tail(sdp
, tail
, pull
);
401 gfs2_assert_withdraw(sdp
, !pull
);
403 sdp
->sd_log_idle
= (tail
== sdp
->sd_log_flush_head
);
406 /* printk(KERN_INFO "log write header out\n"); */
409 static void log_flush_commit(struct gfs2_sbd
*sdp
)
411 struct list_head
*head
= &sdp
->sd_log_flush_list
;
412 struct gfs2_log_buf
*lb
;
413 struct buffer_head
*bh
;
415 while (!list_empty(head
)) {
416 lb
= list_entry(head
->next
, struct gfs2_log_buf
, lb_list
);
417 list_del(&lb
->lb_list
);
421 if (!buffer_uptodate(bh
))
422 gfs2_io_error_bh(sdp
, bh
);
424 while (atomic_read(&bh
->b_count
) != 1) /* Grrrr... */
426 free_buffer_head(bh
);
432 log_write_header(sdp
, 0, 0);
436 * gfs2_log_flush - flush incore transaction(s)
437 * @sdp: the filesystem
438 * @gl: The glock structure to flush. If NULL, flush the whole incore log
442 void gfs2_log_flush(struct gfs2_sbd
*sdp
, struct gfs2_glock
*gl
)
446 down_write(&sdp
->sd_log_flush_lock
);
450 if (list_empty(&gl
->gl_le
.le_list
)) {
451 gfs2_log_unlock(sdp
);
452 up_write(&sdp
->sd_log_flush_lock
);
455 gfs2_log_unlock(sdp
);
458 ai
= kzalloc(sizeof(struct gfs2_ail
), GFP_NOFS
| __GFP_NOFAIL
);
459 INIT_LIST_HEAD(&ai
->ai_ail1_list
);
460 INIT_LIST_HEAD(&ai
->ai_ail2_list
);
462 gfs2_assert_withdraw(sdp
,
463 sdp
->sd_log_num_buf
== sdp
->sd_log_commited_buf
);
464 gfs2_assert_withdraw(sdp
,
465 sdp
->sd_log_num_revoke
== sdp
->sd_log_commited_revoke
);
467 sdp
->sd_log_flush_head
= sdp
->sd_log_head
;
468 sdp
->sd_log_flush_wrapped
= 0;
469 ai
->ai_first
= sdp
->sd_log_flush_head
;
471 lops_before_commit(sdp
);
472 if (!list_empty(&sdp
->sd_log_flush_list
))
473 log_flush_commit(sdp
);
474 else if (sdp
->sd_log_tail
!= current_tail(sdp
) && !sdp
->sd_log_idle
)
475 log_write_header(sdp
, 0, PULL
);
476 lops_after_commit(sdp
, ai
);
477 sdp
->sd_log_head
= sdp
->sd_log_flush_head
;
479 /* printk(KERN_INFO "sd_log_num_hdrs %u\n", sdp->sd_log_num_hdrs); */
480 sdp
->sd_log_blks_free
-= sdp
->sd_log_num_hdrs
;
482 sdp
->sd_log_blks_reserved
=
483 sdp
->sd_log_commited_buf
=
484 sdp
->sd_log_num_hdrs
=
485 sdp
->sd_log_commited_revoke
= 0;
488 if (!list_empty(&ai
->ai_ail1_list
)) {
489 list_add(&ai
->ai_list
, &sdp
->sd_ail1_list
);
492 gfs2_log_unlock(sdp
);
494 sdp
->sd_vfs
->s_dirt
= 0;
495 up_write(&sdp
->sd_log_flush_lock
);
500 static void log_refund(struct gfs2_sbd
*sdp
, struct gfs2_trans
*tr
)
502 unsigned int reserved
= 0;
507 sdp
->sd_log_commited_buf
+= tr
->tr_num_buf_new
- tr
->tr_num_buf_rm
;
508 gfs2_assert_withdraw(sdp
, ((int)sdp
->sd_log_commited_buf
) >= 0);
509 sdp
->sd_log_commited_revoke
+= tr
->tr_num_revoke
- tr
->tr_num_revoke_rm
;
510 gfs2_assert_withdraw(sdp
, ((int)sdp
->sd_log_commited_revoke
) >= 0);
512 if (sdp
->sd_log_commited_buf
)
513 reserved
+= sdp
->sd_log_commited_buf
;
514 if (sdp
->sd_log_commited_revoke
)
515 reserved
+= gfs2_struct2blk(sdp
, sdp
->sd_log_commited_revoke
,
520 old
= sdp
->sd_log_blks_free
;
521 sdp
->sd_log_blks_free
+= tr
->tr_reserved
-
522 (reserved
- sdp
->sd_log_blks_reserved
);
524 gfs2_assert_withdraw(sdp
, sdp
->sd_log_blks_free
>= old
);
525 gfs2_assert_withdraw(sdp
,
526 sdp
->sd_log_blks_free
<= sdp
->sd_jdesc
->jd_blocks
+
527 sdp
->sd_log_num_hdrs
);
529 sdp
->sd_log_blks_reserved
= reserved
;
531 gfs2_log_unlock(sdp
);
535 * gfs2_log_commit - Commit a transaction to the log
536 * @sdp: the filesystem
537 * @tr: the transaction
542 void gfs2_log_commit(struct gfs2_sbd
*sdp
, struct gfs2_trans
*tr
)
545 lops_incore_commit(sdp
, tr
);
547 sdp
->sd_vfs
->s_dirt
= 1;
548 up_read(&sdp
->sd_log_flush_lock
);
551 if (sdp
->sd_log_num_buf
> gfs2_tune_get(sdp
, gt_incore_log_blocks
)) {
552 gfs2_log_unlock(sdp
);
553 gfs2_log_flush(sdp
, NULL
);
555 gfs2_log_unlock(sdp
);
559 * gfs2_log_shutdown - write a shutdown header into a journal
560 * @sdp: the filesystem
564 void gfs2_log_shutdown(struct gfs2_sbd
*sdp
)
566 down_write(&sdp
->sd_log_flush_lock
);
568 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_blks_reserved
);
569 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_gl
);
570 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_buf
);
571 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_jdata
);
572 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_revoke
);
573 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_rg
);
574 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_databuf
);
575 gfs2_assert_withdraw(sdp
, !sdp
->sd_log_num_hdrs
);
576 gfs2_assert_withdraw(sdp
, list_empty(&sdp
->sd_ail1_list
));
578 sdp
->sd_log_flush_head
= sdp
->sd_log_head
;
579 sdp
->sd_log_flush_wrapped
= 0;
581 log_write_header(sdp
, GFS2_LOG_HEAD_UNMOUNT
, 0);
583 /* printk(KERN_INFO "sd_log_blks_free %u, sd_jdesc->jd_blocks %u\n", sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks); */
584 gfs2_assert_warn(sdp
, sdp
->sd_log_blks_free
== sdp
->sd_jdesc
->jd_blocks
);
585 gfs2_assert_warn(sdp
, sdp
->sd_log_head
== sdp
->sd_log_tail
);
586 gfs2_assert_warn(sdp
, list_empty(&sdp
->sd_ail2_list
));
588 sdp
->sd_log_head
= sdp
->sd_log_flush_head
;
589 sdp
->sd_log_tail
= sdp
->sd_log_head
;
591 up_write(&sdp
->sd_log_flush_lock
);