blob: dd810ad68cf04cefd03154ade0595de6630c1f42 [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
30static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
31{
32 struct gfs2_glock *gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -050033 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000034
Steven Whitehouse5c676f62006-02-27 17:23:27 -050035 tr->tr_touched = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
David Teiglandb3b94fa2006-01-16 16:50:04 +000037 gl = container_of(le, struct gfs2_glock, gl_le);
38 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
39 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
41 gfs2_log_lock(sdp);
Benjamin Marzinski68835622007-03-23 09:05:12 +000042 if (!list_empty(&le->le_list)){
43 gfs2_log_unlock(sdp);
44 return;
45 }
46 gfs2_glock_hold(gl);
47 set_bit(GLF_DIRTY, &gl->gl_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000048 sdp->sd_log_num_gl++;
49 list_add(&le->le_list, &sdp->sd_log_le_gl);
50 gfs2_log_unlock(sdp);
51}
52
53static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
54{
55 struct list_head *head = &sdp->sd_log_le_gl;
56 struct gfs2_glock *gl;
57
58 while (!list_empty(head)) {
59 gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
60 list_del_init(&gl->gl_le.le_list);
61 sdp->sd_log_num_gl--;
62
63 gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
64 gfs2_glock_put(gl);
65 }
66 gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
67}
68
69static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
70{
71 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
72 struct gfs2_trans *tr;
73
Steven Whitehouse8bd95722007-01-25 10:04:20 +000074 gfs2_log_lock(sdp);
75 if (!list_empty(&bd->bd_list_tr)) {
76 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +000077 return;
Steven Whitehouse8bd95722007-01-25 10:04:20 +000078 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -050079 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000080 tr->tr_touched = 1;
81 tr->tr_num_buf++;
82 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
Steven Whitehouse8bd95722007-01-25 10:04:20 +000083 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +000084
85 if (!list_empty(&le->le_list))
86 return;
87
88 gfs2_trans_add_gl(bd->bd_gl);
89
90 gfs2_meta_check(sdp, bd->bd_bh);
Steven Whitehousea98ab222006-01-18 13:38:44 +000091 gfs2_pin(sdp, bd->bd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 gfs2_log_lock(sdp);
93 sdp->sd_log_num_buf++;
94 list_add(&le->le_list, &sdp->sd_log_le_buf);
95 gfs2_log_unlock(sdp);
96
97 tr->tr_num_buf_new++;
98}
99
100static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
101{
102 struct list_head *head = &tr->tr_list_buf;
103 struct gfs2_bufdata *bd;
104
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000105 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106 while (!list_empty(head)) {
107 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
108 list_del_init(&bd->bd_list_tr);
109 tr->tr_num_buf--;
110 }
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000111 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 gfs2_assert_warn(sdp, !tr->tr_num_buf);
113}
114
115static void buf_lo_before_commit(struct gfs2_sbd *sdp)
116{
117 struct buffer_head *bh;
118 struct gfs2_log_descriptor *ld;
119 struct gfs2_bufdata *bd1 = NULL, *bd2;
120 unsigned int total = sdp->sd_log_num_buf;
Robert Peterson2332c442007-06-18 14:50:20 -0500121 unsigned int offset = BUF_OFFSET;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 unsigned int limit;
123 unsigned int num;
124 unsigned n;
125 __be64 *ptr;
126
Robert Peterson2332c442007-06-18 14:50:20 -0500127 limit = buf_limit(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 /* for 4k blocks, limit = 503 */
129
130 bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
131 while(total) {
132 num = total;
133 if (total > limit)
134 num = limit;
135 bh = gfs2_log_get_buf(sdp);
136 ld = (struct gfs2_log_descriptor *)bh->b_data;
137 ptr = (__be64 *)(bh->b_data + offset);
138 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500139 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
140 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
142 ld->ld_length = cpu_to_be32(num + 1);
143 ld->ld_data1 = cpu_to_be32(num);
144 ld->ld_data2 = cpu_to_be32(0);
145 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
146
147 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500148 list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
149 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
151 if (++n >= num)
152 break;
153 }
154
155 set_buffer_dirty(bh);
156 ll_rw_block(WRITE, 1, &bh);
157
158 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500159 list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
160 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
162 set_buffer_dirty(bh);
163 ll_rw_block(WRITE, 1, &bh);
164 if (++n >= num)
165 break;
166 }
167
168 total -= num;
169 }
170}
171
172static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
173{
174 struct list_head *head = &sdp->sd_log_le_buf;
175 struct gfs2_bufdata *bd;
176
177 while (!list_empty(head)) {
178 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
179 list_del_init(&bd->bd_le.le_list);
180 sdp->sd_log_num_buf--;
181
Steven Whitehousea98ab222006-01-18 13:38:44 +0000182 gfs2_unpin(sdp, bd->bd_bh, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 }
184 gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
185}
186
187static void buf_lo_before_scan(struct gfs2_jdesc *jd,
Al Viro55167622006-10-13 21:47:13 -0400188 struct gfs2_log_header_host *head, int pass)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400190 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191
192 if (pass != 0)
193 return;
194
195 sdp->sd_found_blocks = 0;
196 sdp->sd_replayed_blocks = 0;
197}
198
199static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
200 struct gfs2_log_descriptor *ld, __be64 *ptr,
201 int pass)
202{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400203 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
204 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500205 struct gfs2_glock *gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 unsigned int blks = be32_to_cpu(ld->ld_data1);
207 struct buffer_head *bh_log, *bh_ip;
Steven Whitehousecd915492006-09-04 12:49:07 -0400208 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 int error = 0;
210
211 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
212 return 0;
213
214 gfs2_replay_incr_blk(sdp, &start);
215
216 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
217 blkno = be64_to_cpu(*ptr++);
218
219 sdp->sd_found_blocks++;
220
221 if (gfs2_revoke_check(sdp, blkno, start))
222 continue;
223
224 error = gfs2_replay_read_block(jd, start, &bh_log);
Steven Whitehouse82ffa512006-09-04 14:47:06 -0400225 if (error)
226 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227
228 bh_ip = gfs2_meta_new(gl, blkno);
229 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
230
231 if (gfs2_meta_check(sdp, bh_ip))
232 error = -EIO;
233 else
234 mark_buffer_dirty(bh_ip);
235
236 brelse(bh_log);
237 brelse(bh_ip);
238
239 if (error)
240 break;
241
242 sdp->sd_replayed_blocks++;
243 }
244
245 return error;
246}
247
248static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
249{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400250 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
251 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252
253 if (error) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400254 gfs2_meta_sync(ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255 return;
256 }
257 if (pass != 1)
258 return;
259
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400260 gfs2_meta_sync(ip->i_gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261
262 fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
263 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
264}
265
266static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
267{
268 struct gfs2_trans *tr;
269
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500270 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 tr->tr_touched = 1;
272 tr->tr_num_revoke++;
273
274 gfs2_log_lock(sdp);
275 sdp->sd_log_num_revoke++;
276 list_add(&le->le_list, &sdp->sd_log_le_revoke);
277 gfs2_log_unlock(sdp);
278}
279
280static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
281{
282 struct gfs2_log_descriptor *ld;
283 struct gfs2_meta_header *mh;
284 struct buffer_head *bh;
285 unsigned int offset;
286 struct list_head *head = &sdp->sd_log_le_revoke;
287 struct gfs2_revoke *rv;
288
289 if (!sdp->sd_log_num_revoke)
290 return;
291
292 bh = gfs2_log_get_buf(sdp);
293 ld = (struct gfs2_log_descriptor *)bh->b_data;
294 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500295 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
296 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500298 ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
Steven Whitehousecd915492006-09-04 12:49:07 -0400299 sizeof(u64)));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000300 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
301 ld->ld_data2 = cpu_to_be32(0);
302 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
303 offset = sizeof(struct gfs2_log_descriptor);
304
305 while (!list_empty(head)) {
306 rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
Steven Whitehouse13538b82006-02-22 11:15:03 +0000307 list_del_init(&rv->rv_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308 sdp->sd_log_num_revoke--;
309
Steven Whitehousecd915492006-09-04 12:49:07 -0400310 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 set_buffer_dirty(bh);
312 ll_rw_block(WRITE, 1, &bh);
313
314 bh = gfs2_log_get_buf(sdp);
315 mh = (struct gfs2_meta_header *)bh->b_data;
316 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500317 mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
318 mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000319 offset = sizeof(struct gfs2_meta_header);
320 }
321
322 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
323 kfree(rv);
324
Steven Whitehousecd915492006-09-04 12:49:07 -0400325 offset += sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326 }
327 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
328
329 set_buffer_dirty(bh);
330 ll_rw_block(WRITE, 1, &bh);
331}
332
333static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
Al Viro55167622006-10-13 21:47:13 -0400334 struct gfs2_log_header_host *head, int pass)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400336 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000337
338 if (pass != 0)
339 return;
340
341 sdp->sd_found_revokes = 0;
342 sdp->sd_replay_tail = head->lh_tail;
343}
344
345static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
346 struct gfs2_log_descriptor *ld, __be64 *ptr,
347 int pass)
348{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400349 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 unsigned int blks = be32_to_cpu(ld->ld_length);
351 unsigned int revokes = be32_to_cpu(ld->ld_data1);
352 struct buffer_head *bh;
353 unsigned int offset;
Steven Whitehousecd915492006-09-04 12:49:07 -0400354 u64 blkno;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355 int first = 1;
356 int error;
357
358 if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
359 return 0;
360
361 offset = sizeof(struct gfs2_log_descriptor);
362
363 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
364 error = gfs2_replay_read_block(jd, start, &bh);
365 if (error)
366 return error;
367
368 if (!first)
369 gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
370
Steven Whitehousecd915492006-09-04 12:49:07 -0400371 while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
373
374 error = gfs2_revoke_add(sdp, blkno, start);
375 if (error < 0)
376 return error;
377 else if (error)
378 sdp->sd_found_revokes++;
379
380 if (!--revokes)
381 break;
Steven Whitehousecd915492006-09-04 12:49:07 -0400382 offset += sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383 }
384
385 brelse(bh);
386 offset = sizeof(struct gfs2_meta_header);
387 first = 0;
388 }
389
390 return 0;
391}
392
393static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
394{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400395 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396
397 if (error) {
398 gfs2_revoke_clean(sdp);
399 return;
400 }
401 if (pass != 1)
402 return;
403
404 fs_info(sdp, "jid=%u: Found %u revoke tags\n",
405 jd->jd_jid, sdp->sd_found_revokes);
406
407 gfs2_revoke_clean(sdp);
408}
409
410static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
411{
412 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500413 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500415 tr->tr_touched = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417 rgd = container_of(le, struct gfs2_rgrpd, rd_le);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000418
419 gfs2_log_lock(sdp);
Benjamin Marzinski68835622007-03-23 09:05:12 +0000420 if (!list_empty(&le->le_list)){
421 gfs2_log_unlock(sdp);
422 return;
423 }
424 gfs2_rgrp_bh_hold(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000425 sdp->sd_log_num_rg++;
426 list_add(&le->le_list, &sdp->sd_log_le_rg);
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400427 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000428}
429
430static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
431{
432 struct list_head *head = &sdp->sd_log_le_rg;
433 struct gfs2_rgrpd *rgd;
434
435 while (!list_empty(head)) {
436 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
437 list_del_init(&rgd->rd_le.le_list);
438 sdp->sd_log_num_rg--;
439
440 gfs2_rgrp_repolish_clones(rgd);
441 gfs2_rgrp_bh_put(rgd);
442 }
443 gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
444}
445
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000446/**
447 * databuf_lo_add - Add a databuf to the transaction.
448 *
449 * This is used in two distinct cases:
450 * i) In ordered write mode
451 * We put the data buffer on a list so that we can ensure that its
452 * synced to disk at the right time
453 * ii) In journaled data mode
454 * We need to journal the data block in the same way as metadata in
455 * the functions above. The difference is that here we have a tag
456 * which is two __be64's being the block number (as per meta data)
457 * and a flag which says whether the data block needs escaping or
458 * not. This means we need a new log entry for each 251 or so data
459 * blocks, which isn't an enormous overhead but twice as much as
460 * for normal metadata blocks.
461 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
463{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000464 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500465 struct gfs2_trans *tr = current->journal_info;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000466 struct address_space *mapping = bd->bd_bh->b_page->mapping;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400467 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000468
Steven Whitehouse8bd95722007-01-25 10:04:20 +0000469 gfs2_log_lock(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500470 if (!list_empty(&bd->bd_list_tr)) {
471 gfs2_log_unlock(sdp);
472 return;
473 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000474 tr->tr_touched = 1;
Robert Peterson2332c442007-06-18 14:50:20 -0500475 tr->tr_num_buf++;
476 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
477 gfs2_log_unlock(sdp);
478 if (!list_empty(&le->le_list))
479 return;
480
Steven Whitehouseb61dde72006-06-19 10:51:11 -0400481 gfs2_trans_add_gl(bd->bd_gl);
Robert Peterson2332c442007-06-18 14:50:20 -0500482 if (gfs2_is_jdata(ip)) {
483 sdp->sd_log_num_jdata++;
484 gfs2_pin(sdp, bd->bd_bh);
485 tr->tr_num_databuf_new++;
Steven Whitehouse13538b82006-02-22 11:15:03 +0000486 }
Robert Peterson2332c442007-06-18 14:50:20 -0500487 sdp->sd_log_num_databuf++;
488 gfs2_log_lock(sdp);
489 list_add(&le->le_list, &sdp->sd_log_le_databuf);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000490 gfs2_log_unlock(sdp);
491}
492
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000493static int gfs2_check_magic(struct buffer_head *bh)
494{
495 struct page *page = bh->b_page;
496 void *kaddr;
497 __be32 *ptr;
498 int rv = 0;
499
500 kaddr = kmap_atomic(page, KM_USER0);
501 ptr = kaddr + bh_offset(bh);
502 if (*ptr == cpu_to_be32(GFS2_MAGIC))
503 rv = 1;
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400504 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000505
506 return rv;
507}
508
509/**
510 * databuf_lo_before_commit - Scan the data buffers, writing as we go
511 *
512 * Here we scan through the lists of buffers and make the assumption
513 * that any buffer thats been pinned is being journaled, and that
514 * any unpinned buffer is an ordered write data buffer and therefore
515 * will be written back rather than journaled.
516 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
518{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 LIST_HEAD(started);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000520 struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
Russell Cattelan70209332006-11-09 11:28:08 -0500521 struct buffer_head *bh = NULL,*bh1 = NULL;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000522 struct gfs2_log_descriptor *ld;
523 unsigned int limit;
524 unsigned int total_dbuf = sdp->sd_log_num_databuf;
525 unsigned int total_jdata = sdp->sd_log_num_jdata;
526 unsigned int num, n;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000527 __be64 *ptr = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528
Robert Peterson2332c442007-06-18 14:50:20 -0500529 limit = databuf_limit(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000531 /*
532 * Start writing ordered buffers, write journaled buffers
533 * into the log along with a header
534 */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000535 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500536 bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
537 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000538 while(total_dbuf) {
539 num = total_jdata;
540 if (num > limit)
541 num = limit;
542 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500543 list_for_each_entry_safe_continue(bd1, bdt,
544 &sdp->sd_log_le_databuf,
545 bd_le.le_list) {
Russell Cattelan70209332006-11-09 11:28:08 -0500546 /* store off the buffer head in a local ptr since
547 * gfs2_bufdata might change when we drop the log lock
548 */
549 bh1 = bd1->bd_bh;
550
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000551 /* An ordered write buffer */
Russell Cattelan70209332006-11-09 11:28:08 -0500552 if (bh1 && !buffer_pinned(bh1)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000553 list_move(&bd1->bd_le.le_list, &started);
554 if (bd1 == bd2) {
555 bd2 = NULL;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500556 bd2 = list_prepare_entry(bd2,
557 &sdp->sd_log_le_databuf,
558 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000559 }
560 total_dbuf--;
Russell Cattelan70209332006-11-09 11:28:08 -0500561 if (bh1) {
562 if (buffer_dirty(bh1)) {
563 get_bh(bh1);
564
Steven Whitehousef55ab262006-02-21 12:51:39 +0000565 gfs2_log_unlock(sdp);
Russell Cattelan70209332006-11-09 11:28:08 -0500566
567 ll_rw_block(SWRITE, 1, &bh1);
568 brelse(bh1);
569
Steven Whitehousef55ab262006-02-21 12:51:39 +0000570 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000571 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000572 continue;
573 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000574 continue;
Russell Cattelan70209332006-11-09 11:28:08 -0500575 } else if (bh1) { /* A journaled buffer */
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000576 int magic;
577 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000578 if (!bh) {
579 bh = gfs2_log_get_buf(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500580 ld = (struct gfs2_log_descriptor *)
581 bh->b_data;
Robert Peterson2332c442007-06-18 14:50:20 -0500582 ptr = (__be64 *)(bh->b_data +
583 DATABUF_OFFSET);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500584 ld->ld_header.mh_magic =
585 cpu_to_be32(GFS2_MAGIC);
586 ld->ld_header.mh_type =
Steven Whitehousee3167de2006-03-30 15:46:23 -0500587 cpu_to_be32(GFS2_METATYPE_LD);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500588 ld->ld_header.mh_format =
Steven Whitehousee3167de2006-03-30 15:46:23 -0500589 cpu_to_be32(GFS2_FORMAT_LD);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500590 ld->ld_type =
591 cpu_to_be32(GFS2_LOG_DESC_JDATA);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000592 ld->ld_length = cpu_to_be32(num + 1);
593 ld->ld_data1 = cpu_to_be32(num);
594 ld->ld_data2 = cpu_to_be32(0);
595 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
596 }
Russell Cattelan70209332006-11-09 11:28:08 -0500597 magic = gfs2_check_magic(bh1);
598 *ptr++ = cpu_to_be64(bh1->b_blocknr);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000599 *ptr++ = cpu_to_be64((__u64)magic);
Russell Cattelan70209332006-11-09 11:28:08 -0500600 clear_buffer_escaped(bh1);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000601 if (unlikely(magic != 0))
Russell Cattelan70209332006-11-09 11:28:08 -0500602 set_buffer_escaped(bh1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000603 gfs2_log_lock(sdp);
Robert Peterson2332c442007-06-18 14:50:20 -0500604 if (++n >= num)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000605 break;
Russell Cattelan70209332006-11-09 11:28:08 -0500606 } else if (!bh1) {
Steven Whitehouse623d9352006-08-31 12:14:44 -0400607 total_dbuf--;
608 sdp->sd_log_num_databuf--;
609 list_del_init(&bd1->bd_le.le_list);
610 if (bd1 == bd2) {
611 bd2 = NULL;
612 bd2 = list_prepare_entry(bd2,
613 &sdp->sd_log_le_databuf,
614 bd_le.le_list);
615 }
616 kmem_cache_free(gfs2_bufdata_cachep, bd1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000618 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000619 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000620 if (bh) {
Robert Peterson8fb68592007-06-12 11:24:36 -0500621 set_buffer_mapped(bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000622 set_buffer_dirty(bh);
623 ll_rw_block(WRITE, 1, &bh);
624 bh = NULL;
625 }
626 n = 0;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000627 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500628 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
629 bd_le.le_list) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000630 if (!bd2->bd_bh)
631 continue;
632 /* copy buffer if it needs escaping */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000633 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000634 if (unlikely(buffer_escaped(bd2->bd_bh))) {
635 void *kaddr;
636 struct page *page = bd2->bd_bh->b_page;
637 bh = gfs2_log_get_buf(sdp);
638 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500639 memcpy(bh->b_data,
640 kaddr + bh_offset(bd2->bd_bh),
641 sdp->sd_sb.sb_bsize);
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400642 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000643 *(__be32 *)bh->b_data = 0;
644 } else {
645 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
646 }
647 set_buffer_dirty(bh);
648 ll_rw_block(WRITE, 1, &bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000649 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000650 if (++n >= num)
651 break;
652 }
653 bh = NULL;
654 total_dbuf -= num;
655 total_jdata -= num;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000657 gfs2_log_unlock(sdp);
658
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000659 /* Wait on all ordered buffers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 while (!list_empty(&started)) {
Steven Whitehouse13538b82006-02-22 11:15:03 +0000661 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500662 bd1 = list_entry(started.next, struct gfs2_bufdata,
663 bd_le.le_list);
Steven Whitehouse08867602006-08-22 11:03:57 -0400664 list_del_init(&bd1->bd_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 sdp->sd_log_num_databuf--;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000666 bh = bd1->bd_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000667 if (bh) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500668 bh->b_private = NULL;
Steven Whitehouse15d00c02006-08-18 15:51:09 -0400669 get_bh(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670 gfs2_log_unlock(sdp);
671 wait_on_buffer(bh);
672 brelse(bh);
673 } else
674 gfs2_log_unlock(sdp);
675
Steven Whitehouse3a8476d2006-06-19 09:10:39 -0400676 kmem_cache_free(gfs2_bufdata_cachep, bd1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 }
678
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000679 /* We've removed all the ordered write bufs here, so only jdata left */
680 gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681}
682
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000683static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
684 struct gfs2_log_descriptor *ld,
685 __be64 *ptr, int pass)
686{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400687 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
688 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500689 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000690 unsigned int blks = be32_to_cpu(ld->ld_data1);
691 struct buffer_head *bh_log, *bh_ip;
Steven Whitehousecd915492006-09-04 12:49:07 -0400692 u64 blkno;
693 u64 esc;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000694 int error = 0;
695
696 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
697 return 0;
698
699 gfs2_replay_incr_blk(sdp, &start);
700 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
701 blkno = be64_to_cpu(*ptr++);
702 esc = be64_to_cpu(*ptr++);
703
704 sdp->sd_found_blocks++;
705
706 if (gfs2_revoke_check(sdp, blkno, start))
707 continue;
708
709 error = gfs2_replay_read_block(jd, start, &bh_log);
710 if (error)
711 return error;
712
713 bh_ip = gfs2_meta_new(gl, blkno);
714 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
715
716 /* Unescape */
717 if (esc) {
718 __be32 *eptr = (__be32 *)bh_ip->b_data;
719 *eptr = cpu_to_be32(GFS2_MAGIC);
720 }
721 mark_buffer_dirty(bh_ip);
722
723 brelse(bh_log);
724 brelse(bh_ip);
725 if (error)
726 break;
727
728 sdp->sd_replayed_blocks++;
729 }
730
731 return error;
732}
733
734/* FIXME: sort out accounting for log blocks etc. */
735
736static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
737{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400738 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
739 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000740
741 if (error) {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400742 gfs2_meta_sync(ip->i_gl);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000743 return;
744 }
745 if (pass != 1)
746 return;
747
748 /* data sync? */
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400749 gfs2_meta_sync(ip->i_gl);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000750
751 fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
752 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
753}
754
755static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
756{
757 struct list_head *head = &sdp->sd_log_le_databuf;
758 struct gfs2_bufdata *bd;
759
760 while (!list_empty(head)) {
761 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
Steven Whitehouseb8e1aab2006-08-22 16:25:50 -0400762 list_del_init(&bd->bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000763 sdp->sd_log_num_databuf--;
764 sdp->sd_log_num_jdata--;
765 gfs2_unpin(sdp, bd->bd_bh, ai);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000766 }
767 gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
768 gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
769}
770
771
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400772const struct gfs2_log_operations gfs2_glock_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 .lo_add = glock_lo_add,
774 .lo_after_commit = glock_lo_after_commit,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400775 .lo_name = "glock",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776};
777
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400778const struct gfs2_log_operations gfs2_buf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 .lo_add = buf_lo_add,
780 .lo_incore_commit = buf_lo_incore_commit,
781 .lo_before_commit = buf_lo_before_commit,
782 .lo_after_commit = buf_lo_after_commit,
783 .lo_before_scan = buf_lo_before_scan,
784 .lo_scan_elements = buf_lo_scan_elements,
785 .lo_after_scan = buf_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400786 .lo_name = "buf",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787};
788
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400789const struct gfs2_log_operations gfs2_revoke_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 .lo_add = revoke_lo_add,
791 .lo_before_commit = revoke_lo_before_commit,
792 .lo_before_scan = revoke_lo_before_scan,
793 .lo_scan_elements = revoke_lo_scan_elements,
794 .lo_after_scan = revoke_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400795 .lo_name = "revoke",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796};
797
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400798const struct gfs2_log_operations gfs2_rg_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 .lo_add = rg_lo_add,
800 .lo_after_commit = rg_lo_after_commit,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400801 .lo_name = "rg",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802};
803
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400804const struct gfs2_log_operations gfs2_databuf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000805 .lo_add = databuf_lo_add,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000806 .lo_incore_commit = buf_lo_incore_commit,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807 .lo_before_commit = databuf_lo_before_commit,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000808 .lo_after_commit = databuf_lo_after_commit,
809 .lo_scan_elements = databuf_lo_scan_elements,
810 .lo_after_scan = databuf_lo_after_scan,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400811 .lo_name = "databuf",
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812};
813
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400814const struct gfs2_log_operations *gfs2_log_ops[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 &gfs2_glock_lops,
816 &gfs2_buf_lops,
817 &gfs2_revoke_lops,
818 &gfs2_rg_lops,
819 &gfs2_databuf_lops,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400820 NULL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821};
822