blob: 342c10e12af2f994e0f073e330de43d7efb53b1e [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020016#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
Robert Peterson2332c442007-06-18 14:50:20 -050020#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "glock.h"
22#include "log.h"
23#include "lops.h"
24#include "meta_io.h"
25#include "recovery.h"
26#include "rgrp.h"
27#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
Steven Whitehouse9b9107a2007-08-27 13:54:05 +010030/**
31 * gfs2_pin - Pin a buffer in memory
32 * @sdp: The superblock
33 * @bh: The buffer to be pinned
34 *
35 * The log lock must be held when calling this function
36 */
37static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
38{
39 struct gfs2_bufdata *bd;
40
41 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
42
43 clear_buffer_dirty(bh);
44 if (test_set_buffer_pinned(bh))
45 gfs2_assert_withdraw(sdp, 0);
46 if (!buffer_uptodate(bh))
47 gfs2_io_error_bh(sdp, bh);
48 bd = bh->b_private;
49 /* If this buffer is in the AIL and it has already been written
50 * to in-place disk block, remove it from the AIL.
51 */
52 if (bd->bd_ail)
53 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
54 get_bh(bh);
55}
56
57/**
58 * gfs2_unpin - Unpin a buffer
59 * @sdp: the filesystem the buffer belongs to
60 * @bh: The buffer to unpin
61 * @ai:
62 *
63 */
64
65static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
66 struct gfs2_ail *ai)
67{
68 struct gfs2_bufdata *bd = bh->b_private;
69
70 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
71
72 if (!buffer_pinned(bh))
73 gfs2_assert_withdraw(sdp, 0);
74
75 lock_buffer(bh);
76 mark_buffer_dirty(bh);
77 clear_buffer_pinned(bh);
78
79 gfs2_log_lock(sdp);
80 if (bd->bd_ail) {
81 list_del(&bd->bd_ail_st_list);
82 brelse(bh);
83 } else {
84 struct gfs2_glock *gl = bd->bd_gl;
85 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
86 atomic_inc(&gl->gl_ail_count);
87 }
88 bd->bd_ail = ai;
89 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
90 gfs2_log_unlock(sdp);
91 unlock_buffer(bh);
92}
93
94static void __glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
David Teiglandb3b94fa2006-01-16 16:50:04 +000095{
96 struct gfs2_glock *gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050097 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000098
Steven Whitehouse5c676f62006-02-27 17:23:27 -050099 tr->tr_touched = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101 gl = container_of(le, struct gfs2_glock, gl_le);
102 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
103 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100105 if (!list_empty(&le->le_list))
Benjamin Marzinski68835622007-03-23 09:05:12 +0000106 return;
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100107
Benjamin Marzinski68835622007-03-23 09:05:12 +0000108 gfs2_glock_hold(gl);
109 set_bit(GLF_DIRTY, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 sdp->sd_log_num_gl++;
111 list_add(&le->le_list, &sdp->sd_log_le_gl);
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100112}
113
114static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
115{
116 gfs2_log_lock(sdp);
117 __glock_lo_add(sdp, le);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 gfs2_log_unlock(sdp);
119}
120
121static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
122{
123 struct list_head *head = &sdp->sd_log_le_gl;
124 struct gfs2_glock *gl;
125
126 while (!list_empty(head)) {
127 gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
128 list_del_init(&gl->gl_le.le_list);
129 sdp->sd_log_num_gl--;
130
131 gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
132 gfs2_glock_put(gl);
133 }
134 gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
135}
136
137static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
138{
139 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
140 struct gfs2_trans *tr;
141
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100142 lock_buffer(bd->bd_bh);
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000143 gfs2_log_lock(sdp);
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100144 if (!list_empty(&bd->bd_list_tr))
145 goto out;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500146 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 tr->tr_touched = 1;
148 tr->tr_num_buf++;
149 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 if (!list_empty(&le->le_list))
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100151 goto out;
152 __glock_lo_add(sdp, &bd->bd_gl->gl_le);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 gfs2_meta_check(sdp, bd->bd_bh);
Steven Whitehousea98ab222006-01-18 13:38:44 +0000154 gfs2_pin(sdp, bd->bd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155 sdp->sd_log_num_buf++;
156 list_add(&le->le_list, &sdp->sd_log_le_buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 tr->tr_num_buf_new++;
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100158out:
159 gfs2_log_unlock(sdp);
160 unlock_buffer(bd->bd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161}
162
163static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
164{
165 struct list_head *head = &tr->tr_list_buf;
166 struct gfs2_bufdata *bd;
167
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000168 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 while (!list_empty(head)) {
170 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
171 list_del_init(&bd->bd_list_tr);
172 tr->tr_num_buf--;
173 }
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000174 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 gfs2_assert_warn(sdp, !tr->tr_num_buf);
176}
177
178static void buf_lo_before_commit(struct gfs2_sbd *sdp)
179{
180 struct buffer_head *bh;
181 struct gfs2_log_descriptor *ld;
182 struct gfs2_bufdata *bd1 = NULL, *bd2;
Bob Peterson905d2ae2007-07-24 14:05:31 -0500183 unsigned int total;
Robert Peterson2332c442007-06-18 14:50:20 -0500184 unsigned int offset = BUF_OFFSET;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 unsigned int limit;
186 unsigned int num;
187 unsigned n;
188 __be64 *ptr;
189
Robert Peterson2332c442007-06-18 14:50:20 -0500190 limit = buf_limit(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 /* for 4k blocks, limit = 503 */
192
Bob Peterson905d2ae2007-07-24 14:05:31 -0500193 gfs2_log_lock(sdp);
194 total = sdp->sd_log_num_buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
196 while(total) {
197 num = total;
198 if (total > limit)
199 num = limit;
Bob Peterson905d2ae2007-07-24 14:05:31 -0500200 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000201 bh = gfs2_log_get_buf(sdp);
Bob Peterson905d2ae2007-07-24 14:05:31 -0500202 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 ld = (struct gfs2_log_descriptor *)bh->b_data;
204 ptr = (__be64 *)(bh->b_data + offset);
205 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500206 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
207 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
209 ld->ld_length = cpu_to_be32(num + 1);
210 ld->ld_data1 = cpu_to_be32(num);
211 ld->ld_data2 = cpu_to_be32(0);
212 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
213
214 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500215 list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
216 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000217 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
218 if (++n >= num)
219 break;
220 }
221
Bob Peterson905d2ae2007-07-24 14:05:31 -0500222 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 set_buffer_dirty(bh);
224 ll_rw_block(WRITE, 1, &bh);
Bob Peterson905d2ae2007-07-24 14:05:31 -0500225 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000226
227 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500228 list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
229 bd_le.le_list) {
Bob Peterson905d2ae2007-07-24 14:05:31 -0500230 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
232 set_buffer_dirty(bh);
233 ll_rw_block(WRITE, 1, &bh);
Bob Peterson905d2ae2007-07-24 14:05:31 -0500234 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 if (++n >= num)
236 break;
237 }
238
Bob Peterson905d2ae2007-07-24 14:05:31 -0500239 BUG_ON(total < num);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000240 total -= num;
241 }
Bob Peterson905d2ae2007-07-24 14:05:31 -0500242 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000243}
244
245static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
246{
247 struct list_head *head = &sdp->sd_log_le_buf;
248 struct gfs2_bufdata *bd;
249
250 while (!list_empty(head)) {
251 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
252 list_del_init(&bd->bd_le.le_list);
253 sdp->sd_log_num_buf--;
254
Steven Whitehousea98ab222006-01-18 13:38:44 +0000255 gfs2_unpin(sdp, bd->bd_bh, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 }
257 gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
258}
259
260static void buf_lo_before_scan(struct gfs2_jdesc *jd,
Al Viro55167622006-10-13 21:47:13 -0400261 struct gfs2_log_header_host *head, int pass)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400263 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264
265 if (pass != 0)
266 return;
267
268 sdp->sd_found_blocks = 0;
269 sdp->sd_replayed_blocks = 0;
270}
271
272static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
273 struct gfs2_log_descriptor *ld, __be64 *ptr,
274 int pass)
275{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400276 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
277 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500278 struct gfs2_glock *gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 unsigned int blks = be32_to_cpu(ld->ld_data1);
280 struct buffer_head *bh_log, *bh_ip;
Steven Whitehousecd915492006-09-04 12:49:07 -0400281 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 int error = 0;
283
284 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
285 return 0;
286
287 gfs2_replay_incr_blk(sdp, &start);
288
289 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
290 blkno = be64_to_cpu(*ptr++);
291
292 sdp->sd_found_blocks++;
293
294 if (gfs2_revoke_check(sdp, blkno, start))
295 continue;
296
297 error = gfs2_replay_read_block(jd, start, &bh_log);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400298 if (error)
299 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300
301 bh_ip = gfs2_meta_new(gl, blkno);
302 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
303
304 if (gfs2_meta_check(sdp, bh_ip))
305 error = -EIO;
306 else
307 mark_buffer_dirty(bh_ip);
308
309 brelse(bh_log);
310 brelse(bh_ip);
311
312 if (error)
313 break;
314
315 sdp->sd_replayed_blocks++;
316 }
317
318 return error;
319}
320
321static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
322{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400323 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
324 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000325
326 if (error) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400327 gfs2_meta_sync(ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 return;
329 }
330 if (pass != 1)
331 return;
332
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400333 gfs2_meta_sync(ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334
335 fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
336 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
337}
338
339static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
340{
341 struct gfs2_trans *tr;
342
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500343 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 tr->tr_touched = 1;
345 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346 sdp->sd_log_num_revoke++;
347 list_add(&le->le_list, &sdp->sd_log_le_revoke);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348}
349
350static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
351{
352 struct gfs2_log_descriptor *ld;
353 struct gfs2_meta_header *mh;
354 struct buffer_head *bh;
355 unsigned int offset;
356 struct list_head *head = &sdp->sd_log_le_revoke;
Steven Whitehouse82e86082007-09-02 15:39:43 +0100357 struct gfs2_bufdata *bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358
359 if (!sdp->sd_log_num_revoke)
360 return;
361
362 bh = gfs2_log_get_buf(sdp);
363 ld = (struct gfs2_log_descriptor *)bh->b_data;
364 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500365 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
366 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500368 ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400369 sizeof(u64)));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
371 ld->ld_data2 = cpu_to_be32(0);
372 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
373 offset = sizeof(struct gfs2_log_descriptor);
374
375 while (!list_empty(head)) {
Steven Whitehouse82e86082007-09-02 15:39:43 +0100376 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
377 list_del_init(&bd->bd_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378 sdp->sd_log_num_revoke--;
379
Steven Whitehousecd915492006-09-04 12:49:07 -0400380 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 set_buffer_dirty(bh);
382 ll_rw_block(WRITE, 1, &bh);
383
384 bh = gfs2_log_get_buf(sdp);
385 mh = (struct gfs2_meta_header *)bh->b_data;
386 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500387 mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
388 mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 offset = sizeof(struct gfs2_meta_header);
390 }
391
Steven Whitehouse82e86082007-09-02 15:39:43 +0100392 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(bd->bd_blkno);
Steven Whitehouse0820ab52007-09-02 16:47:38 +0100393 kmem_cache_free(gfs2_bufdata_cachep, bd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394
Steven Whitehousecd915492006-09-04 12:49:07 -0400395 offset += sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396 }
397 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
398
399 set_buffer_dirty(bh);
400 ll_rw_block(WRITE, 1, &bh);
401}
402
403static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
Al Viro55167622006-10-13 21:47:13 -0400404 struct gfs2_log_header_host *head, int pass)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400406 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407
408 if (pass != 0)
409 return;
410
411 sdp->sd_found_revokes = 0;
412 sdp->sd_replay_tail = head->lh_tail;
413}
414
415static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
416 struct gfs2_log_descriptor *ld, __be64 *ptr,
417 int pass)
418{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400419 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 unsigned int blks = be32_to_cpu(ld->ld_length);
421 unsigned int revokes = be32_to_cpu(ld->ld_data1);
422 struct buffer_head *bh;
423 unsigned int offset;
Steven Whitehousecd915492006-09-04 12:49:07 -0400424 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 int first = 1;
426 int error;
427
428 if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
429 return 0;
430
431 offset = sizeof(struct gfs2_log_descriptor);
432
433 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
434 error = gfs2_replay_read_block(jd, start, &bh);
435 if (error)
436 return error;
437
438 if (!first)
439 gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
440
Steven Whitehousecd915492006-09-04 12:49:07 -0400441 while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
443
444 error = gfs2_revoke_add(sdp, blkno, start);
445 if (error < 0)
446 return error;
447 else if (error)
448 sdp->sd_found_revokes++;
449
450 if (!--revokes)
451 break;
Steven Whitehousecd915492006-09-04 12:49:07 -0400452 offset += sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 }
454
455 brelse(bh);
456 offset = sizeof(struct gfs2_meta_header);
457 first = 0;
458 }
459
460 return 0;
461}
462
463static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
464{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400465 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466
467 if (error) {
468 gfs2_revoke_clean(sdp);
469 return;
470 }
471 if (pass != 1)
472 return;
473
474 fs_info(sdp, "jid=%u: Found %u revoke tags\n",
475 jd->jd_jid, sdp->sd_found_revokes);
476
477 gfs2_revoke_clean(sdp);
478}
479
480static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
481{
482 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500483 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500485 tr->tr_touched = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486
David Teiglandb3b94fa2006-01-16 16:50:04 +0000487 rgd = container_of(le, struct gfs2_rgrpd, rd_le);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000488
489 gfs2_log_lock(sdp);
Benjamin Marzinski68835622007-03-23 09:05:12 +0000490 if (!list_empty(&le->le_list)){
491 gfs2_log_unlock(sdp);
492 return;
493 }
494 gfs2_rgrp_bh_hold(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000495 sdp->sd_log_num_rg++;
496 list_add(&le->le_list, &sdp->sd_log_le_rg);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400497 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000498}
499
500static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
501{
502 struct list_head *head = &sdp->sd_log_le_rg;
503 struct gfs2_rgrpd *rgd;
504
505 while (!list_empty(head)) {
506 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
507 list_del_init(&rgd->rd_le.le_list);
508 sdp->sd_log_num_rg--;
509
510 gfs2_rgrp_repolish_clones(rgd);
511 gfs2_rgrp_bh_put(rgd);
512 }
513 gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
514}
515
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000516/**
517 * databuf_lo_add - Add a databuf to the transaction.
518 *
519 * This is used in two distinct cases:
520 * i) In ordered write mode
521 * We put the data buffer on a list so that we can ensure that its
522 * synced to disk at the right time
523 * ii) In journaled data mode
524 * We need to journal the data block in the same way as metadata in
525 * the functions above. The difference is that here we have a tag
526 * which is two __be64's being the block number (as per meta data)
527 * and a flag which says whether the data block needs escaping or
528 * not. This means we need a new log entry for each 251 or so data
529 * blocks, which isn't an enormous overhead but twice as much as
530 * for normal metadata blocks.
531 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
533{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000534 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500535 struct gfs2_trans *tr = current->journal_info;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000536 struct address_space *mapping = bd->bd_bh->b_page->mapping;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400537 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000538
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100539 lock_buffer(bd->bd_bh);
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000540 gfs2_log_lock(sdp);
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100541 if (!list_empty(&bd->bd_list_tr))
542 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000543 tr->tr_touched = 1;
Robert Peterson773ed1a2007-06-20 08:34:06 -0500544 if (gfs2_is_jdata(ip)) {
545 tr->tr_num_buf++;
546 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
547 }
Robert Peterson2332c442007-06-18 14:50:20 -0500548 if (!list_empty(&le->le_list))
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100549 goto out;
Robert Peterson2332c442007-06-18 14:50:20 -0500550
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100551 __glock_lo_add(sdp, &bd->bd_gl->gl_le);
Robert Peterson2332c442007-06-18 14:50:20 -0500552 if (gfs2_is_jdata(ip)) {
Robert Peterson2332c442007-06-18 14:50:20 -0500553 gfs2_pin(sdp, bd->bd_bh);
554 tr->tr_num_databuf_new++;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100555 sdp->sd_log_num_databuf++;
556 list_add(&le->le_list, &sdp->sd_log_le_databuf);
557 } else {
558 list_add(&le->le_list, &sdp->sd_log_le_ordered);
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100559 }
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100560out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561 gfs2_log_unlock(sdp);
Steven Whitehouse9b9107a2007-08-27 13:54:05 +0100562 unlock_buffer(bd->bd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563}
564
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000565static int gfs2_check_magic(struct buffer_head *bh)
566{
567 struct page *page = bh->b_page;
568 void *kaddr;
569 __be32 *ptr;
570 int rv = 0;
571
572 kaddr = kmap_atomic(page, KM_USER0);
573 ptr = kaddr + bh_offset(bh);
574 if (*ptr == cpu_to_be32(GFS2_MAGIC))
575 rv = 1;
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400576 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000577
578 return rv;
579}
580
581/**
582 * databuf_lo_before_commit - Scan the data buffers, writing as we go
583 *
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000584 */
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100585
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
587{
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100588 struct gfs2_bufdata *bd1 = NULL, *bd2;
Russell Cattelan70209332006-11-09 11:28:08 -0500589 struct buffer_head *bh = NULL,*bh1 = NULL;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000590 struct gfs2_log_descriptor *ld;
591 unsigned int limit;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100592 unsigned int total;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000593 unsigned int num, n;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000594 __be64 *ptr = NULL;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100595 int magic;
596
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597
Robert Peterson2332c442007-06-18 14:50:20 -0500598 limit = databuf_limit(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599
Steven Whitehousef55ab262006-02-21 12:51:39 +0000600 gfs2_log_lock(sdp);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100601 total = sdp->sd_log_num_databuf;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500602 bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
603 bd_le.le_list);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100604 while(total) {
605 num = total;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000606 if (num > limit)
607 num = limit;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100608
609 gfs2_log_unlock(sdp);
610 bh = gfs2_log_get_buf(sdp);
611 gfs2_log_lock(sdp);
612
613 ld = (struct gfs2_log_descriptor *)bh->b_data;
614 ptr = (__be64 *)(bh->b_data + DATABUF_OFFSET);
615 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
616 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
617 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
618 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_JDATA);
619 ld->ld_length = cpu_to_be32(num + 1);
620 ld->ld_data1 = cpu_to_be32(num);
621 ld->ld_data2 = cpu_to_be32(0);
622 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
623
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000624 n = 0;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100625 list_for_each_entry_continue(bd1, &sdp->sd_log_le_databuf,
626 bd_le.le_list) {
Russell Cattelan70209332006-11-09 11:28:08 -0500627 bh1 = bd1->bd_bh;
628
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100629 magic = gfs2_check_magic(bh1);
630 *ptr++ = cpu_to_be64(bh1->b_blocknr);
631 *ptr++ = cpu_to_be64((__u64)magic);
632 clear_buffer_escaped(bh1);
633 if (unlikely(magic != 0))
634 set_buffer_escaped(bh1);
635 if (++n >= num)
636 break;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000637 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000638 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000639 if (bh) {
640 set_buffer_dirty(bh);
641 ll_rw_block(WRITE, 1, &bh);
642 bh = NULL;
Bob Peterson905d2ae2007-07-24 14:05:31 -0500643 ptr = NULL;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000644 }
645 n = 0;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000646 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500647 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
648 bd_le.le_list) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000649 if (!bd2->bd_bh)
650 continue;
651 /* copy buffer if it needs escaping */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000652 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000653 if (unlikely(buffer_escaped(bd2->bd_bh))) {
654 void *kaddr;
655 struct page *page = bd2->bd_bh->b_page;
656 bh = gfs2_log_get_buf(sdp);
657 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500658 memcpy(bh->b_data,
659 kaddr + bh_offset(bd2->bd_bh),
660 sdp->sd_sb.sb_bsize);
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400661 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000662 *(__be32 *)bh->b_data = 0;
663 } else {
664 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
665 }
666 set_buffer_dirty(bh);
667 ll_rw_block(WRITE, 1, &bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000668 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000669 if (++n >= num)
670 break;
671 }
672 bh = NULL;
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100673 BUG_ON(total < num);
674 total -= num;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000676 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677}
678
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000679static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
680 struct gfs2_log_descriptor *ld,
681 __be64 *ptr, int pass)
682{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400683 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
684 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500685 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000686 unsigned int blks = be32_to_cpu(ld->ld_data1);
687 struct buffer_head *bh_log, *bh_ip;
Steven Whitehousecd915492006-09-04 12:49:07 -0400688 u64 blkno;
689 u64 esc;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000690 int error = 0;
691
692 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
693 return 0;
694
695 gfs2_replay_incr_blk(sdp, &start);
696 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
697 blkno = be64_to_cpu(*ptr++);
698 esc = be64_to_cpu(*ptr++);
699
700 sdp->sd_found_blocks++;
701
702 if (gfs2_revoke_check(sdp, blkno, start))
703 continue;
704
705 error = gfs2_replay_read_block(jd, start, &bh_log);
706 if (error)
707 return error;
708
709 bh_ip = gfs2_meta_new(gl, blkno);
710 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
711
712 /* Unescape */
713 if (esc) {
714 __be32 *eptr = (__be32 *)bh_ip->b_data;
715 *eptr = cpu_to_be32(GFS2_MAGIC);
716 }
717 mark_buffer_dirty(bh_ip);
718
719 brelse(bh_log);
720 brelse(bh_ip);
721 if (error)
722 break;
723
724 sdp->sd_replayed_blocks++;
725 }
726
727 return error;
728}
729
730/* FIXME: sort out accounting for log blocks etc. */
731
732static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
733{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400734 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
735 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000736
737 if (error) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400738 gfs2_meta_sync(ip->i_gl);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000739 return;
740 }
741 if (pass != 1)
742 return;
743
744 /* data sync? */
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400745 gfs2_meta_sync(ip->i_gl);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000746
747 fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
748 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
749}
750
751static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
752{
753 struct list_head *head = &sdp->sd_log_le_databuf;
754 struct gfs2_bufdata *bd;
755
756 while (!list_empty(head)) {
757 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400758 list_del_init(&bd->bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000759 sdp->sd_log_num_databuf--;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000760 gfs2_unpin(sdp, bd->bd_bh, ai);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000761 }
762 gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000763}
764
765
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400766const struct gfs2_log_operations gfs2_glock_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 .lo_add = glock_lo_add,
768 .lo_after_commit = glock_lo_after_commit,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400769 .lo_name = "glock",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770};
771
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400772const struct gfs2_log_operations gfs2_buf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 .lo_add = buf_lo_add,
774 .lo_incore_commit = buf_lo_incore_commit,
775 .lo_before_commit = buf_lo_before_commit,
776 .lo_after_commit = buf_lo_after_commit,
777 .lo_before_scan = buf_lo_before_scan,
778 .lo_scan_elements = buf_lo_scan_elements,
779 .lo_after_scan = buf_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400780 .lo_name = "buf",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781};
782
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400783const struct gfs2_log_operations gfs2_revoke_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 .lo_add = revoke_lo_add,
785 .lo_before_commit = revoke_lo_before_commit,
786 .lo_before_scan = revoke_lo_before_scan,
787 .lo_scan_elements = revoke_lo_scan_elements,
788 .lo_after_scan = revoke_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400789 .lo_name = "revoke",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790};
791
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400792const struct gfs2_log_operations gfs2_rg_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 .lo_add = rg_lo_add,
794 .lo_after_commit = rg_lo_after_commit,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400795 .lo_name = "rg",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796};
797
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400798const struct gfs2_log_operations gfs2_databuf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 .lo_add = databuf_lo_add,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000800 .lo_incore_commit = buf_lo_incore_commit,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801 .lo_before_commit = databuf_lo_before_commit,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000802 .lo_after_commit = databuf_lo_after_commit,
803 .lo_scan_elements = databuf_lo_scan_elements,
804 .lo_after_scan = databuf_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400805 .lo_name = "databuf",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806};
807
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400808const struct gfs2_log_operations *gfs2_log_ops[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 &gfs2_glock_lops,
810 &gfs2_buf_lops,
811 &gfs2_revoke_lops,
812 &gfs2_rg_lops,
813 &gfs2_databuf_lops,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400814 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815};
816