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 glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
32 struct gfs2_glock *gl;
33 struct gfs2_trans *tr = current->journal_info;
37 if (!list_empty(&le->le_list))
40 gl = container_of(le, struct gfs2_glock, gl_le);
41 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
44 set_bit(GLF_DIRTY, &gl->gl_flags);
48 list_add(&le->le_list, &sdp->sd_log_le_gl);
52 static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
54 struct list_head *head = &sdp->sd_log_le_gl;
55 struct gfs2_glock *gl;
57 while (!list_empty(head)) {
58 gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
59 list_del_init(&gl->gl_le.le_list);
62 gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
65 gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
68 static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
70 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
71 struct gfs2_trans *tr;
73 if (!list_empty(&bd->bd_list_tr))
76 tr = current->journal_info;
79 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
81 if (!list_empty(&le->le_list))
84 gfs2_trans_add_gl(bd->bd_gl);
86 gfs2_meta_check(sdp, bd->bd_bh);
87 gfs2_pin(sdp, bd->bd_bh);
90 sdp->sd_log_num_buf++;
91 list_add(&le->le_list, &sdp->sd_log_le_buf);
97 static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
99 struct list_head *head = &tr->tr_list_buf;
100 struct gfs2_bufdata *bd;
102 while (!list_empty(head)) {
103 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
104 list_del_init(&bd->bd_list_tr);
107 gfs2_assert_warn(sdp, !tr->tr_num_buf);
110 static void buf_lo_before_commit(struct gfs2_sbd *sdp)
112 struct buffer_head *bh;
113 struct gfs2_log_descriptor *ld;
114 struct gfs2_bufdata *bd1 = NULL, *bd2;
115 unsigned int total = sdp->sd_log_num_buf;
116 unsigned int offset = sizeof(struct gfs2_log_descriptor);
122 offset += (sizeof(__be64) - 1);
123 offset &= ~(sizeof(__be64) - 1);
124 limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
125 /* for 4k blocks, limit = 503 */
127 bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
132 bh = gfs2_log_get_buf(sdp);
133 ld = (struct gfs2_log_descriptor *)bh->b_data;
134 ptr = (__be64 *)(bh->b_data + offset);
135 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
136 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
137 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
138 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
139 ld->ld_length = cpu_to_be32(num + 1);
140 ld->ld_data1 = cpu_to_be32(num);
141 ld->ld_data2 = cpu_to_be32(0);
142 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
145 list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
147 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
152 set_buffer_dirty(bh);
153 ll_rw_block(WRITE, 1, &bh);
156 list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
158 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
159 set_buffer_dirty(bh);
160 ll_rw_block(WRITE, 1, &bh);
169 static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
171 struct list_head *head = &sdp->sd_log_le_buf;
172 struct gfs2_bufdata *bd;
174 while (!list_empty(head)) {
175 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
176 list_del_init(&bd->bd_le.le_list);
177 sdp->sd_log_num_buf--;
179 gfs2_unpin(sdp, bd->bd_bh, ai);
181 gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
184 static void buf_lo_before_scan(struct gfs2_jdesc *jd,
185 struct gfs2_log_header *head, int pass)
187 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
188 struct gfs2_sbd *sdp = ip->i_sbd;
193 sdp->sd_found_blocks = 0;
194 sdp->sd_replayed_blocks = 0;
197 static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
198 struct gfs2_log_descriptor *ld, __be64 *ptr,
201 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
202 struct gfs2_sbd *sdp = ip->i_sbd;
203 struct gfs2_glock *gl = ip->i_gl;
204 unsigned int blks = be32_to_cpu(ld->ld_data1);
205 struct buffer_head *bh_log, *bh_ip;
209 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
212 gfs2_replay_incr_blk(sdp, &start);
214 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
215 blkno = be64_to_cpu(*ptr++);
217 sdp->sd_found_blocks++;
219 if (gfs2_revoke_check(sdp, blkno, start))
222 error = gfs2_replay_read_block(jd, start, &bh_log);
226 bh_ip = gfs2_meta_new(gl, blkno);
227 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
229 if (gfs2_meta_check(sdp, bh_ip))
232 mark_buffer_dirty(bh_ip);
240 sdp->sd_replayed_blocks++;
246 static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
248 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
249 struct gfs2_sbd *sdp = ip->i_sbd;
252 gfs2_meta_sync(ip->i_gl,
253 DIO_START | DIO_WAIT);
259 gfs2_meta_sync(ip->i_gl, DIO_START | DIO_WAIT);
261 fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
262 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
265 static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
267 struct gfs2_trans *tr;
269 tr = current->journal_info;
274 sdp->sd_log_num_revoke++;
275 list_add(&le->le_list, &sdp->sd_log_le_revoke);
276 gfs2_log_unlock(sdp);
279 static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
281 struct gfs2_log_descriptor *ld;
282 struct gfs2_meta_header *mh;
283 struct buffer_head *bh;
285 struct list_head *head = &sdp->sd_log_le_revoke;
286 struct gfs2_revoke *rv;
288 if (!sdp->sd_log_num_revoke)
291 bh = gfs2_log_get_buf(sdp);
292 ld = (struct gfs2_log_descriptor *)bh->b_data;
293 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
294 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
295 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
296 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
297 ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
299 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
300 ld->ld_data2 = cpu_to_be32(0);
301 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
302 offset = sizeof(struct gfs2_log_descriptor);
304 while (!list_empty(head)) {
305 rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
306 list_del_init(&rv->rv_le.le_list);
307 sdp->sd_log_num_revoke--;
309 if (offset + sizeof(uint64_t) > sdp->sd_sb.sb_bsize) {
310 set_buffer_dirty(bh);
311 ll_rw_block(WRITE, 1, &bh);
313 bh = gfs2_log_get_buf(sdp);
314 mh = (struct gfs2_meta_header *)bh->b_data;
315 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
316 mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
317 mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
318 offset = sizeof(struct gfs2_meta_header);
321 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
324 offset += sizeof(uint64_t);
326 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
328 set_buffer_dirty(bh);
329 ll_rw_block(WRITE, 1, &bh);
332 static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
333 struct gfs2_log_header *head, int pass)
335 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
336 struct gfs2_sbd *sdp = ip->i_sbd;
341 sdp->sd_found_revokes = 0;
342 sdp->sd_replay_tail = head->lh_tail;
345 static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
346 struct gfs2_log_descriptor *ld, __be64 *ptr,
349 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
350 struct gfs2_sbd *sdp = ip->i_sbd;
351 unsigned int blks = be32_to_cpu(ld->ld_length);
352 unsigned int revokes = be32_to_cpu(ld->ld_data1);
353 struct buffer_head *bh;
359 if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
362 offset = sizeof(struct gfs2_log_descriptor);
364 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
365 error = gfs2_replay_read_block(jd, start, &bh);
370 gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
372 while (offset + sizeof(uint64_t) <= sdp->sd_sb.sb_bsize) {
373 blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
375 error = gfs2_revoke_add(sdp, blkno, start);
379 sdp->sd_found_revokes++;
383 offset += sizeof(uint64_t);
387 offset = sizeof(struct gfs2_meta_header);
394 static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
396 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
397 struct gfs2_sbd *sdp = ip->i_sbd;
400 gfs2_revoke_clean(sdp);
406 fs_info(sdp, "jid=%u: Found %u revoke tags\n",
407 jd->jd_jid, sdp->sd_found_revokes);
409 gfs2_revoke_clean(sdp);
412 static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
414 struct gfs2_rgrpd *rgd;
415 struct gfs2_trans *tr = current->journal_info;
419 if (!list_empty(&le->le_list))
422 rgd = container_of(le, struct gfs2_rgrpd, rd_le);
423 gfs2_rgrp_bh_hold(rgd);
426 sdp->sd_log_num_rg++;
427 list_add(&le->le_list, &sdp->sd_log_le_rg);
428 gfs2_log_unlock(sdp);
431 static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
433 struct list_head *head = &sdp->sd_log_le_rg;
434 struct gfs2_rgrpd *rgd;
436 while (!list_empty(head)) {
437 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
438 list_del_init(&rgd->rd_le.le_list);
439 sdp->sd_log_num_rg--;
441 gfs2_rgrp_repolish_clones(rgd);
442 gfs2_rgrp_bh_put(rgd);
444 gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
448 * databuf_lo_add - Add a databuf to the transaction.
450 * This is used in two distinct cases:
451 * i) In ordered write mode
452 * We put the data buffer on a list so that we can ensure that its
453 * synced to disk at the right time
454 * ii) In journaled data mode
455 * We need to journal the data block in the same way as metadata in
456 * the functions above. The difference is that here we have a tag
457 * which is two __be64's being the block number (as per meta data)
458 * and a flag which says whether the data block needs escaping or
459 * not. This means we need a new log entry for each 251 or so data
460 * blocks, which isn't an enormous overhead but twice as much as
461 * for normal metadata blocks.
463 static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
465 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
466 struct gfs2_trans *tr = current->journal_info;
467 struct address_space *mapping = bd->bd_bh->b_page->mapping;
468 struct gfs2_inode *ip = mapping->host->u.generic_ip;
471 if (!list_empty(&bd->bd_list_tr) &&
472 (ip->i_di.di_flags & GFS2_DIF_JDATA)) {
474 gfs2_trans_add_gl(bd->bd_gl);
475 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
476 gfs2_pin(sdp, bd->bd_bh);
477 tr->tr_num_buf_new++;
480 if (!list_empty(&le->le_list)) {
481 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
482 sdp->sd_log_num_jdata++;
483 sdp->sd_log_num_databuf++;
484 list_add(&le->le_list, &sdp->sd_log_le_databuf);
486 gfs2_log_unlock(sdp);
489 static int gfs2_check_magic(struct buffer_head *bh)
491 struct page *page = bh->b_page;
496 kaddr = kmap_atomic(page, KM_USER0);
497 ptr = kaddr + bh_offset(bh);
498 if (*ptr == cpu_to_be32(GFS2_MAGIC))
500 kunmap_atomic(page, KM_USER0);
506 * databuf_lo_before_commit - Scan the data buffers, writing as we go
508 * Here we scan through the lists of buffers and make the assumption
509 * that any buffer thats been pinned is being journaled, and that
510 * any unpinned buffer is an ordered write data buffer and therefore
511 * will be written back rather than journaled.
513 static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
516 struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
517 struct buffer_head *bh = NULL;
518 unsigned int offset = sizeof(struct gfs2_log_descriptor);
519 struct gfs2_log_descriptor *ld;
521 unsigned int total_dbuf = sdp->sd_log_num_databuf;
522 unsigned int total_jdata = sdp->sd_log_num_jdata;
526 offset += (2*sizeof(__be64) - 1);
527 offset &= ~(2*sizeof(__be64) - 1);
528 limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
531 * Start writing ordered buffers, write journaled buffers
532 * into the log along with a header
535 bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
542 list_for_each_entry_safe_continue(bd1, bdt,
543 &sdp->sd_log_le_databuf,
545 /* An ordered write buffer */
546 if (bd1->bd_bh && !buffer_pinned(bd1->bd_bh)) {
547 list_move(&bd1->bd_le.le_list, &started);
550 bd2 = list_prepare_entry(bd2,
551 &sdp->sd_log_le_databuf,
557 if (buffer_dirty(bd1->bd_bh)) {
558 gfs2_log_unlock(sdp);
559 wait_on_buffer(bd1->bd_bh);
560 ll_rw_block(WRITE, 1,
568 } else if (bd1->bd_bh) { /* A journaled buffer */
570 gfs2_log_unlock(sdp);
572 bh = gfs2_log_get_buf(sdp);
573 ld = (struct gfs2_log_descriptor *)
575 ptr = (__be64 *)(bh->b_data + offset);
576 ld->ld_header.mh_magic =
577 cpu_to_be32(GFS2_MAGIC);
578 ld->ld_header.mh_type =
579 cpu_to_be32(GFS2_METATYPE_LD);
580 ld->ld_header.mh_format =
581 cpu_to_be32(GFS2_FORMAT_LD);
583 cpu_to_be32(GFS2_LOG_DESC_JDATA);
584 ld->ld_length = cpu_to_be32(num + 1);
585 ld->ld_data1 = cpu_to_be32(num);
586 ld->ld_data2 = cpu_to_be32(0);
587 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
589 magic = gfs2_check_magic(bd1->bd_bh);
590 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
591 *ptr++ = cpu_to_be64((__u64)magic);
592 clear_buffer_escaped(bd1->bd_bh);
593 if (unlikely(magic != 0))
594 set_buffer_escaped(bd1->bd_bh);
600 gfs2_log_unlock(sdp);
602 set_buffer_dirty(bh);
603 ll_rw_block(WRITE, 1, &bh);
608 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
612 /* copy buffer if it needs escaping */
613 gfs2_log_unlock(sdp);
614 if (unlikely(buffer_escaped(bd2->bd_bh))) {
616 struct page *page = bd2->bd_bh->b_page;
617 bh = gfs2_log_get_buf(sdp);
618 kaddr = kmap_atomic(page, KM_USER0);
620 kaddr + bh_offset(bd2->bd_bh),
621 sdp->sd_sb.sb_bsize);
622 kunmap_atomic(page, KM_USER0);
623 *(__be32 *)bh->b_data = 0;
625 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
627 set_buffer_dirty(bh);
628 ll_rw_block(WRITE, 1, &bh);
637 gfs2_log_unlock(sdp);
639 /* Wait on all ordered buffers */
640 while (!list_empty(&started)) {
642 bd1 = list_entry(started.next, struct gfs2_bufdata,
644 list_del(&bd1->bd_le.le_list);
645 sdp->sd_log_num_databuf--;
649 bh->b_private = NULL;
650 gfs2_log_unlock(sdp);
654 gfs2_log_unlock(sdp);
659 /* We've removed all the ordered write bufs here, so only jdata left */
660 gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
663 static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
664 struct gfs2_log_descriptor *ld,
665 __be64 *ptr, int pass)
667 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
668 struct gfs2_sbd *sdp = ip->i_sbd;
669 struct gfs2_glock *gl = ip->i_gl;
670 unsigned int blks = be32_to_cpu(ld->ld_data1);
671 struct buffer_head *bh_log, *bh_ip;
676 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
679 gfs2_replay_incr_blk(sdp, &start);
680 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
681 blkno = be64_to_cpu(*ptr++);
682 esc = be64_to_cpu(*ptr++);
684 sdp->sd_found_blocks++;
686 if (gfs2_revoke_check(sdp, blkno, start))
689 error = gfs2_replay_read_block(jd, start, &bh_log);
693 bh_ip = gfs2_meta_new(gl, blkno);
694 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
698 __be32 *eptr = (__be32 *)bh_ip->b_data;
699 *eptr = cpu_to_be32(GFS2_MAGIC);
701 mark_buffer_dirty(bh_ip);
708 sdp->sd_replayed_blocks++;
714 /* FIXME: sort out accounting for log blocks etc. */
716 static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
718 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
719 struct gfs2_sbd *sdp = ip->i_sbd;
722 gfs2_meta_sync(ip->i_gl,
723 DIO_START | DIO_WAIT);
730 gfs2_meta_sync(ip->i_gl, DIO_START | DIO_WAIT);
732 fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
733 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
736 static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
738 struct list_head *head = &sdp->sd_log_le_databuf;
739 struct gfs2_bufdata *bd;
741 while (!list_empty(head)) {
742 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
743 list_del(&bd->bd_le.le_list);
744 sdp->sd_log_num_databuf--;
745 sdp->sd_log_num_jdata--;
746 gfs2_unpin(sdp, bd->bd_bh, ai);
748 gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
749 gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
753 struct gfs2_log_operations gfs2_glock_lops = {
754 .lo_add = glock_lo_add,
755 .lo_after_commit = glock_lo_after_commit,
759 struct gfs2_log_operations gfs2_buf_lops = {
760 .lo_add = buf_lo_add,
761 .lo_incore_commit = buf_lo_incore_commit,
762 .lo_before_commit = buf_lo_before_commit,
763 .lo_after_commit = buf_lo_after_commit,
764 .lo_before_scan = buf_lo_before_scan,
765 .lo_scan_elements = buf_lo_scan_elements,
766 .lo_after_scan = buf_lo_after_scan,
770 struct gfs2_log_operations gfs2_revoke_lops = {
771 .lo_add = revoke_lo_add,
772 .lo_before_commit = revoke_lo_before_commit,
773 .lo_before_scan = revoke_lo_before_scan,
774 .lo_scan_elements = revoke_lo_scan_elements,
775 .lo_after_scan = revoke_lo_after_scan,
779 struct gfs2_log_operations gfs2_rg_lops = {
781 .lo_after_commit = rg_lo_after_commit,
785 struct gfs2_log_operations gfs2_databuf_lops = {
786 .lo_add = databuf_lo_add,
787 .lo_incore_commit = buf_lo_incore_commit,
788 .lo_before_commit = databuf_lo_before_commit,
789 .lo_after_commit = databuf_lo_after_commit,
790 .lo_scan_elements = databuf_lo_scan_elements,
791 .lo_after_scan = databuf_lo_after_scan,
795 struct gfs2_log_operations *gfs2_log_ops[] = {