blob: df6bceea379a737114f77f9b6561b2871f2218e8 [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"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "glock.h"
21#include "log.h"
22#include "lops.h"
23#include "meta_io.h"
24#include "recovery.h"
25#include "rgrp.h"
26#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028
29static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
30{
31 struct gfs2_glock *gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050032 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000033
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034 tr->tr_touched = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000035
David Teiglandb3b94fa2006-01-16 16:50:04 +000036 gl = container_of(le, struct gfs2_glock, gl_le);
37 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
38 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
40 gfs2_log_lock(sdp);
Benjamin Marzinski68835622007-03-23 09:05:12 +000041 if (!list_empty(&le->le_list)){
42 gfs2_log_unlock(sdp);
43 return;
44 }
45 gfs2_glock_hold(gl);
46 set_bit(GLF_DIRTY, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000047 sdp->sd_log_num_gl++;
48 list_add(&le->le_list, &sdp->sd_log_le_gl);
49 gfs2_log_unlock(sdp);
50}
51
52static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
53{
54 struct list_head *head = &sdp->sd_log_le_gl;
55 struct gfs2_glock *gl;
56
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);
60 sdp->sd_log_num_gl--;
61
62 gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
63 gfs2_glock_put(gl);
64 }
65 gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
66}
67
68static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
69{
70 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
71 struct gfs2_trans *tr;
72
Steven Whitehouse8bd95722007-01-25 10:04:20 +000073 gfs2_log_lock(sdp);
74 if (!list_empty(&bd->bd_list_tr)) {
75 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 return;
Steven Whitehouse8bd95722007-01-25 10:04:20 +000077 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -050078 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 tr->tr_touched = 1;
80 tr->tr_num_buf++;
81 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
Steven Whitehouse8bd95722007-01-25 10:04:20 +000082 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +000083
84 if (!list_empty(&le->le_list))
85 return;
86
87 gfs2_trans_add_gl(bd->bd_gl);
88
89 gfs2_meta_check(sdp, bd->bd_bh);
Steven Whitehousea98ab222006-01-18 13:38:44 +000090 gfs2_pin(sdp, bd->bd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 gfs2_log_lock(sdp);
92 sdp->sd_log_num_buf++;
93 list_add(&le->le_list, &sdp->sd_log_le_buf);
94 gfs2_log_unlock(sdp);
95
96 tr->tr_num_buf_new++;
97}
98
99static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
100{
101 struct list_head *head = &tr->tr_list_buf;
102 struct gfs2_bufdata *bd;
103
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000104 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105 while (!list_empty(head)) {
106 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
107 list_del_init(&bd->bd_list_tr);
108 tr->tr_num_buf--;
109 }
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000110 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111 gfs2_assert_warn(sdp, !tr->tr_num_buf);
112}
113
114static void buf_lo_before_commit(struct gfs2_sbd *sdp)
115{
116 struct buffer_head *bh;
117 struct gfs2_log_descriptor *ld;
118 struct gfs2_bufdata *bd1 = NULL, *bd2;
119 unsigned int total = sdp->sd_log_num_buf;
120 unsigned int offset = sizeof(struct gfs2_log_descriptor);
121 unsigned int limit;
122 unsigned int num;
123 unsigned n;
124 __be64 *ptr;
125
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400126 offset += sizeof(__be64) - 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127 offset &= ~(sizeof(__be64) - 1);
128 limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
129 /* for 4k blocks, limit = 503 */
130
131 bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
132 while(total) {
133 num = total;
134 if (total > limit)
135 num = limit;
136 bh = gfs2_log_get_buf(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400137 sdp->sd_log_num_hdrs++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 ld = (struct gfs2_log_descriptor *)bh->b_data;
139 ptr = (__be64 *)(bh->b_data + offset);
140 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500141 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
142 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
144 ld->ld_length = cpu_to_be32(num + 1);
145 ld->ld_data1 = cpu_to_be32(num);
146 ld->ld_data2 = cpu_to_be32(0);
147 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
148
149 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500150 list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
151 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
153 if (++n >= num)
154 break;
155 }
156
157 set_buffer_dirty(bh);
158 ll_rw_block(WRITE, 1, &bh);
159
160 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500161 list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
162 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
164 set_buffer_dirty(bh);
165 ll_rw_block(WRITE, 1, &bh);
166 if (++n >= num)
167 break;
168 }
169
170 total -= num;
171 }
172}
173
174static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
175{
176 struct list_head *head = &sdp->sd_log_le_buf;
177 struct gfs2_bufdata *bd;
178
179 while (!list_empty(head)) {
180 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
181 list_del_init(&bd->bd_le.le_list);
182 sdp->sd_log_num_buf--;
183
Steven Whitehousea98ab222006-01-18 13:38:44 +0000184 gfs2_unpin(sdp, bd->bd_bh, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185 }
186 gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
187}
188
189static void buf_lo_before_scan(struct gfs2_jdesc *jd,
Al Viro55167622006-10-13 21:47:13 -0400190 struct gfs2_log_header_host *head, int pass)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400192 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193
194 if (pass != 0)
195 return;
196
197 sdp->sd_found_blocks = 0;
198 sdp->sd_replayed_blocks = 0;
199}
200
201static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
202 struct gfs2_log_descriptor *ld, __be64 *ptr,
203 int pass)
204{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400205 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
206 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500207 struct gfs2_glock *gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 unsigned int blks = be32_to_cpu(ld->ld_data1);
209 struct buffer_head *bh_log, *bh_ip;
Steven Whitehousecd915492006-09-04 12:49:07 -0400210 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000211 int error = 0;
212
213 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
214 return 0;
215
216 gfs2_replay_incr_blk(sdp, &start);
217
218 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
219 blkno = be64_to_cpu(*ptr++);
220
221 sdp->sd_found_blocks++;
222
223 if (gfs2_revoke_check(sdp, blkno, start))
224 continue;
225
226 error = gfs2_replay_read_block(jd, start, &bh_log);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400227 if (error)
228 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229
230 bh_ip = gfs2_meta_new(gl, blkno);
231 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
232
233 if (gfs2_meta_check(sdp, bh_ip))
234 error = -EIO;
235 else
236 mark_buffer_dirty(bh_ip);
237
238 brelse(bh_log);
239 brelse(bh_ip);
240
241 if (error)
242 break;
243
244 sdp->sd_replayed_blocks++;
245 }
246
247 return error;
248}
249
250static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
251{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400252 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
253 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254
255 if (error) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400256 gfs2_meta_sync(ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257 return;
258 }
259 if (pass != 1)
260 return;
261
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400262 gfs2_meta_sync(ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263
264 fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
265 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
266}
267
268static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
269{
270 struct gfs2_trans *tr;
271
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500272 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 tr->tr_touched = 1;
274 tr->tr_num_revoke++;
275
276 gfs2_log_lock(sdp);
277 sdp->sd_log_num_revoke++;
278 list_add(&le->le_list, &sdp->sd_log_le_revoke);
279 gfs2_log_unlock(sdp);
280}
281
282static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
283{
284 struct gfs2_log_descriptor *ld;
285 struct gfs2_meta_header *mh;
286 struct buffer_head *bh;
287 unsigned int offset;
288 struct list_head *head = &sdp->sd_log_le_revoke;
289 struct gfs2_revoke *rv;
290
291 if (!sdp->sd_log_num_revoke)
292 return;
293
294 bh = gfs2_log_get_buf(sdp);
295 ld = (struct gfs2_log_descriptor *)bh->b_data;
296 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500297 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
298 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500300 ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400301 sizeof(u64)));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000302 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
303 ld->ld_data2 = cpu_to_be32(0);
304 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
305 offset = sizeof(struct gfs2_log_descriptor);
306
307 while (!list_empty(head)) {
308 rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
Steven Whitehouse13538b82006-02-22 11:15:03 +0000309 list_del_init(&rv->rv_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310 sdp->sd_log_num_revoke--;
311
Steven Whitehousecd915492006-09-04 12:49:07 -0400312 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000313 set_buffer_dirty(bh);
314 ll_rw_block(WRITE, 1, &bh);
315
316 bh = gfs2_log_get_buf(sdp);
317 mh = (struct gfs2_meta_header *)bh->b_data;
318 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500319 mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
320 mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000321 offset = sizeof(struct gfs2_meta_header);
322 }
323
324 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
325 kfree(rv);
326
Steven Whitehousecd915492006-09-04 12:49:07 -0400327 offset += sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 }
329 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
330
331 set_buffer_dirty(bh);
332 ll_rw_block(WRITE, 1, &bh);
333}
334
335static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
Al Viro55167622006-10-13 21:47:13 -0400336 struct gfs2_log_header_host *head, int pass)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400338 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339
340 if (pass != 0)
341 return;
342
343 sdp->sd_found_revokes = 0;
344 sdp->sd_replay_tail = head->lh_tail;
345}
346
347static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
348 struct gfs2_log_descriptor *ld, __be64 *ptr,
349 int pass)
350{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400351 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 unsigned int blks = be32_to_cpu(ld->ld_length);
353 unsigned int revokes = be32_to_cpu(ld->ld_data1);
354 struct buffer_head *bh;
355 unsigned int offset;
Steven Whitehousecd915492006-09-04 12:49:07 -0400356 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357 int first = 1;
358 int error;
359
360 if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
361 return 0;
362
363 offset = sizeof(struct gfs2_log_descriptor);
364
365 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
366 error = gfs2_replay_read_block(jd, start, &bh);
367 if (error)
368 return error;
369
370 if (!first)
371 gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
372
Steven Whitehousecd915492006-09-04 12:49:07 -0400373 while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
375
376 error = gfs2_revoke_add(sdp, blkno, start);
377 if (error < 0)
378 return error;
379 else if (error)
380 sdp->sd_found_revokes++;
381
382 if (!--revokes)
383 break;
Steven Whitehousecd915492006-09-04 12:49:07 -0400384 offset += sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 }
386
387 brelse(bh);
388 offset = sizeof(struct gfs2_meta_header);
389 first = 0;
390 }
391
392 return 0;
393}
394
395static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
396{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400397 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398
399 if (error) {
400 gfs2_revoke_clean(sdp);
401 return;
402 }
403 if (pass != 1)
404 return;
405
406 fs_info(sdp, "jid=%u: Found %u revoke tags\n",
407 jd->jd_jid, sdp->sd_found_revokes);
408
409 gfs2_revoke_clean(sdp);
410}
411
412static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
413{
414 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500415 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500417 tr->tr_touched = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419 rgd = container_of(le, struct gfs2_rgrpd, rd_le);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420
421 gfs2_log_lock(sdp);
Benjamin Marzinski68835622007-03-23 09:05:12 +0000422 if (!list_empty(&le->le_list)){
423 gfs2_log_unlock(sdp);
424 return;
425 }
426 gfs2_rgrp_bh_hold(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000427 sdp->sd_log_num_rg++;
428 list_add(&le->le_list, &sdp->sd_log_le_rg);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400429 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000430}
431
432static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
433{
434 struct list_head *head = &sdp->sd_log_le_rg;
435 struct gfs2_rgrpd *rgd;
436
437 while (!list_empty(head)) {
438 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
439 list_del_init(&rgd->rd_le.le_list);
440 sdp->sd_log_num_rg--;
441
442 gfs2_rgrp_repolish_clones(rgd);
443 gfs2_rgrp_bh_put(rgd);
444 }
445 gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
446}
447
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000448/**
449 * databuf_lo_add - Add a databuf to the transaction.
450 *
451 * This is used in two distinct cases:
452 * i) In ordered write mode
453 * We put the data buffer on a list so that we can ensure that its
454 * synced to disk at the right time
455 * ii) In journaled data mode
456 * We need to journal the data block in the same way as metadata in
457 * the functions above. The difference is that here we have a tag
458 * which is two __be64's being the block number (as per meta data)
459 * and a flag which says whether the data block needs escaping or
460 * not. This means we need a new log entry for each 251 or so data
461 * blocks, which isn't an enormous overhead but twice as much as
462 * for normal metadata blocks.
463 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
465{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000466 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500467 struct gfs2_trans *tr = current->journal_info;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000468 struct address_space *mapping = bd->bd_bh->b_page->mapping;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400469 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000470
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000471 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000472 tr->tr_touched = 1;
Steven Whitehouse15d00c02006-08-18 15:51:09 -0400473 if (list_empty(&bd->bd_list_tr) &&
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000474 (ip->i_di.di_flags & GFS2_DIF_JDATA)) {
475 tr->tr_num_buf++;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000476 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000477 gfs2_log_unlock(sdp);
Benjamin Marzinskiddf4b422007-06-01 14:21:38 -0500478 if (!list_empty(&le->le_list))
479 return;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000480 gfs2_pin(sdp, bd->bd_bh);
Steven Whitehouseb4dc7292006-03-01 17:41:58 -0500481 tr->tr_num_buf_new++;
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000482 } else {
483 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000484 }
Steven Whitehouseb61dde72006-06-19 10:51:11 -0400485 gfs2_trans_add_gl(bd->bd_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 gfs2_log_lock(sdp);
Steven Whitehouse15d00c02006-08-18 15:51:09 -0400487 if (list_empty(&le->le_list)) {
Steven Whitehouse13538b82006-02-22 11:15:03 +0000488 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
489 sdp->sd_log_num_jdata++;
490 sdp->sd_log_num_databuf++;
491 list_add(&le->le_list, &sdp->sd_log_le_databuf);
492 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 gfs2_log_unlock(sdp);
494}
495
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000496static int gfs2_check_magic(struct buffer_head *bh)
497{
498 struct page *page = bh->b_page;
499 void *kaddr;
500 __be32 *ptr;
501 int rv = 0;
502
503 kaddr = kmap_atomic(page, KM_USER0);
504 ptr = kaddr + bh_offset(bh);
505 if (*ptr == cpu_to_be32(GFS2_MAGIC))
506 rv = 1;
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400507 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000508
509 return rv;
510}
511
512/**
513 * databuf_lo_before_commit - Scan the data buffers, writing as we go
514 *
515 * Here we scan through the lists of buffers and make the assumption
516 * that any buffer thats been pinned is being journaled, and that
517 * any unpinned buffer is an ordered write data buffer and therefore
518 * will be written back rather than journaled.
519 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
521{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522 LIST_HEAD(started);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000523 struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
Russell Cattelan70209332006-11-09 11:28:08 -0500524 struct buffer_head *bh = NULL,*bh1 = NULL;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000525 unsigned int offset = sizeof(struct gfs2_log_descriptor);
526 struct gfs2_log_descriptor *ld;
527 unsigned int limit;
528 unsigned int total_dbuf = sdp->sd_log_num_databuf;
529 unsigned int total_jdata = sdp->sd_log_num_jdata;
530 unsigned int num, n;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000531 __be64 *ptr = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000532
Steven Whitehousea67cdbd2006-09-05 14:41:30 -0400533 offset += 2*sizeof(__be64) - 1;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000534 offset &= ~(2*sizeof(__be64) - 1);
535 limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000537 /*
538 * Start writing ordered buffers, write journaled buffers
539 * into the log along with a header
540 */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000541 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500542 bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
543 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000544 while(total_dbuf) {
545 num = total_jdata;
546 if (num > limit)
547 num = limit;
548 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500549 list_for_each_entry_safe_continue(bd1, bdt,
550 &sdp->sd_log_le_databuf,
551 bd_le.le_list) {
Russell Cattelan70209332006-11-09 11:28:08 -0500552 /* store off the buffer head in a local ptr since
553 * gfs2_bufdata might change when we drop the log lock
554 */
555 bh1 = bd1->bd_bh;
556
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000557 /* An ordered write buffer */
Russell Cattelan70209332006-11-09 11:28:08 -0500558 if (bh1 && !buffer_pinned(bh1)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000559 list_move(&bd1->bd_le.le_list, &started);
560 if (bd1 == bd2) {
561 bd2 = NULL;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500562 bd2 = list_prepare_entry(bd2,
563 &sdp->sd_log_le_databuf,
564 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000565 }
566 total_dbuf--;
Russell Cattelan70209332006-11-09 11:28:08 -0500567 if (bh1) {
568 if (buffer_dirty(bh1)) {
569 get_bh(bh1);
570
Steven Whitehousef55ab262006-02-21 12:51:39 +0000571 gfs2_log_unlock(sdp);
Russell Cattelan70209332006-11-09 11:28:08 -0500572
573 ll_rw_block(SWRITE, 1, &bh1);
574 brelse(bh1);
575
Steven Whitehousef55ab262006-02-21 12:51:39 +0000576 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000577 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000578 continue;
579 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000580 continue;
Russell Cattelan70209332006-11-09 11:28:08 -0500581 } else if (bh1) { /* A journaled buffer */
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000582 int magic;
583 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000584 if (!bh) {
585 bh = gfs2_log_get_buf(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400586 sdp->sd_log_num_hdrs++;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500587 ld = (struct gfs2_log_descriptor *)
588 bh->b_data;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000589 ptr = (__be64 *)(bh->b_data + offset);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500590 ld->ld_header.mh_magic =
591 cpu_to_be32(GFS2_MAGIC);
592 ld->ld_header.mh_type =
Steven Whitehousee3167de2006-03-30 15:46:23 -0500593 cpu_to_be32(GFS2_METATYPE_LD);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500594 ld->ld_header.mh_format =
Steven Whitehousee3167de2006-03-30 15:46:23 -0500595 cpu_to_be32(GFS2_FORMAT_LD);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500596 ld->ld_type =
597 cpu_to_be32(GFS2_LOG_DESC_JDATA);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000598 ld->ld_length = cpu_to_be32(num + 1);
599 ld->ld_data1 = cpu_to_be32(num);
600 ld->ld_data2 = cpu_to_be32(0);
601 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
602 }
Russell Cattelan70209332006-11-09 11:28:08 -0500603 magic = gfs2_check_magic(bh1);
604 *ptr++ = cpu_to_be64(bh1->b_blocknr);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000605 *ptr++ = cpu_to_be64((__u64)magic);
Russell Cattelan70209332006-11-09 11:28:08 -0500606 clear_buffer_escaped(bh1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000607 if (unlikely(magic != 0))
Russell Cattelan70209332006-11-09 11:28:08 -0500608 set_buffer_escaped(bh1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000609 gfs2_log_lock(sdp);
Robert Peterson8fb68592007-06-12 11:24:36 -0500610 n += 2;
611 if (n >= num)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000612 break;
Russell Cattelan70209332006-11-09 11:28:08 -0500613 } else if (!bh1) {
Steven Whitehouse623d9352006-08-31 12:14:44 -0400614 total_dbuf--;
615 sdp->sd_log_num_databuf--;
616 list_del_init(&bd1->bd_le.le_list);
617 if (bd1 == bd2) {
618 bd2 = NULL;
619 bd2 = list_prepare_entry(bd2,
620 &sdp->sd_log_le_databuf,
621 bd_le.le_list);
622 }
623 kmem_cache_free(gfs2_bufdata_cachep, bd1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000625 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000626 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000627 if (bh) {
Robert Peterson8fb68592007-06-12 11:24:36 -0500628 set_buffer_mapped(bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000629 set_buffer_dirty(bh);
630 ll_rw_block(WRITE, 1, &bh);
631 bh = NULL;
632 }
633 n = 0;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000634 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500635 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
636 bd_le.le_list) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000637 if (!bd2->bd_bh)
638 continue;
639 /* copy buffer if it needs escaping */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000640 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000641 if (unlikely(buffer_escaped(bd2->bd_bh))) {
642 void *kaddr;
643 struct page *page = bd2->bd_bh->b_page;
644 bh = gfs2_log_get_buf(sdp);
645 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500646 memcpy(bh->b_data,
647 kaddr + bh_offset(bd2->bd_bh),
648 sdp->sd_sb.sb_bsize);
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400649 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000650 *(__be32 *)bh->b_data = 0;
651 } else {
652 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
653 }
654 set_buffer_dirty(bh);
655 ll_rw_block(WRITE, 1, &bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000656 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000657 if (++n >= num)
658 break;
659 }
660 bh = NULL;
661 total_dbuf -= num;
662 total_jdata -= num;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000664 gfs2_log_unlock(sdp);
665
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000666 /* Wait on all ordered buffers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 while (!list_empty(&started)) {
Steven Whitehouse13538b82006-02-22 11:15:03 +0000668 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500669 bd1 = list_entry(started.next, struct gfs2_bufdata,
670 bd_le.le_list);
Steven Whitehouse08867602006-08-22 11:03:57 -0400671 list_del_init(&bd1->bd_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 sdp->sd_log_num_databuf--;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000673 bh = bd1->bd_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674 if (bh) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500675 bh->b_private = NULL;
Steven Whitehouse15d00c02006-08-18 15:51:09 -0400676 get_bh(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 gfs2_log_unlock(sdp);
678 wait_on_buffer(bh);
679 brelse(bh);
680 } else
681 gfs2_log_unlock(sdp);
682
Steven Whitehouse3a8476d2006-06-19 09:10:39 -0400683 kmem_cache_free(gfs2_bufdata_cachep, bd1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684 }
685
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000686 /* We've removed all the ordered write bufs here, so only jdata left */
687 gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688}
689
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000690static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
691 struct gfs2_log_descriptor *ld,
692 __be64 *ptr, int pass)
693{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400694 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
695 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500696 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000697 unsigned int blks = be32_to_cpu(ld->ld_data1);
698 struct buffer_head *bh_log, *bh_ip;
Steven Whitehousecd915492006-09-04 12:49:07 -0400699 u64 blkno;
700 u64 esc;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000701 int error = 0;
702
703 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
704 return 0;
705
706 gfs2_replay_incr_blk(sdp, &start);
707 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
708 blkno = be64_to_cpu(*ptr++);
709 esc = be64_to_cpu(*ptr++);
710
711 sdp->sd_found_blocks++;
712
713 if (gfs2_revoke_check(sdp, blkno, start))
714 continue;
715
716 error = gfs2_replay_read_block(jd, start, &bh_log);
717 if (error)
718 return error;
719
720 bh_ip = gfs2_meta_new(gl, blkno);
721 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
722
723 /* Unescape */
724 if (esc) {
725 __be32 *eptr = (__be32 *)bh_ip->b_data;
726 *eptr = cpu_to_be32(GFS2_MAGIC);
727 }
728 mark_buffer_dirty(bh_ip);
729
730 brelse(bh_log);
731 brelse(bh_ip);
732 if (error)
733 break;
734
735 sdp->sd_replayed_blocks++;
736 }
737
738 return error;
739}
740
741/* FIXME: sort out accounting for log blocks etc. */
742
743static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
744{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400745 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
746 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000747
748 if (error) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400749 gfs2_meta_sync(ip->i_gl);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000750 return;
751 }
752 if (pass != 1)
753 return;
754
755 /* data sync? */
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400756 gfs2_meta_sync(ip->i_gl);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000757
758 fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
759 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
760}
761
762static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
763{
764 struct list_head *head = &sdp->sd_log_le_databuf;
765 struct gfs2_bufdata *bd;
766
767 while (!list_empty(head)) {
768 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400769 list_del_init(&bd->bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000770 sdp->sd_log_num_databuf--;
771 sdp->sd_log_num_jdata--;
772 gfs2_unpin(sdp, bd->bd_bh, ai);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000773 }
774 gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
775 gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
776}
777
778
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400779const struct gfs2_log_operations gfs2_glock_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 .lo_add = glock_lo_add,
781 .lo_after_commit = glock_lo_after_commit,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400782 .lo_name = "glock",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783};
784
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400785const struct gfs2_log_operations gfs2_buf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 .lo_add = buf_lo_add,
787 .lo_incore_commit = buf_lo_incore_commit,
788 .lo_before_commit = buf_lo_before_commit,
789 .lo_after_commit = buf_lo_after_commit,
790 .lo_before_scan = buf_lo_before_scan,
791 .lo_scan_elements = buf_lo_scan_elements,
792 .lo_after_scan = buf_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400793 .lo_name = "buf",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794};
795
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400796const struct gfs2_log_operations gfs2_revoke_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 .lo_add = revoke_lo_add,
798 .lo_before_commit = revoke_lo_before_commit,
799 .lo_before_scan = revoke_lo_before_scan,
800 .lo_scan_elements = revoke_lo_scan_elements,
801 .lo_after_scan = revoke_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400802 .lo_name = "revoke",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803};
804
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400805const struct gfs2_log_operations gfs2_rg_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806 .lo_add = rg_lo_add,
807 .lo_after_commit = rg_lo_after_commit,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400808 .lo_name = "rg",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809};
810
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400811const struct gfs2_log_operations gfs2_databuf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 .lo_add = databuf_lo_add,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000813 .lo_incore_commit = buf_lo_incore_commit,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814 .lo_before_commit = databuf_lo_before_commit,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000815 .lo_after_commit = databuf_lo_after_commit,
816 .lo_scan_elements = databuf_lo_scan_elements,
817 .lo_after_scan = databuf_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400818 .lo_name = "databuf",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819};
820
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400821const struct gfs2_log_operations *gfs2_log_ops[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822 &gfs2_glock_lops,
823 &gfs2_buf_lops,
824 &gfs2_revoke_lops,
825 &gfs2_rg_lops,
826 &gfs2_databuf_lops,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400827 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828};
829