blob: e4c75a74df5bc55989705e6281d8e999b373b5b0 [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
7 * of the GNU General Public License v.2.
8 */
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>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016
17#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include "lm_interface.h"
19#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
36 if (!list_empty(&le->le_list))
37 return;
38
39 gl = container_of(le, struct gfs2_glock, gl_le);
40 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
41 return;
42 gfs2_glock_hold(gl);
43 set_bit(GLF_DIRTY, &gl->gl_flags);
44
45 gfs2_log_lock(sdp);
46 sdp->sd_log_num_gl++;
47 list_add(&le->le_list, &sdp->sd_log_le_gl);
48 gfs2_log_unlock(sdp);
49}
50
51static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
52{
53 struct list_head *head = &sdp->sd_log_le_gl;
54 struct gfs2_glock *gl;
55
56 while (!list_empty(head)) {
57 gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
58 list_del_init(&gl->gl_le.le_list);
59 sdp->sd_log_num_gl--;
60
61 gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
62 gfs2_glock_put(gl);
63 }
64 gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
65}
66
67static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
68{
69 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
70 struct gfs2_trans *tr;
71
72 if (!list_empty(&bd->bd_list_tr))
73 return;
74
Steven Whitehouse5c676f62006-02-27 17:23:27 -050075 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 tr->tr_touched = 1;
77 tr->tr_num_buf++;
78 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
79
80 if (!list_empty(&le->le_list))
81 return;
82
83 gfs2_trans_add_gl(bd->bd_gl);
84
85 gfs2_meta_check(sdp, bd->bd_bh);
Steven Whitehousea98ab222006-01-18 13:38:44 +000086 gfs2_pin(sdp, bd->bd_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
88 gfs2_log_lock(sdp);
89 sdp->sd_log_num_buf++;
90 list_add(&le->le_list, &sdp->sd_log_le_buf);
91 gfs2_log_unlock(sdp);
92
93 tr->tr_num_buf_new++;
94}
95
96static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
97{
98 struct list_head *head = &tr->tr_list_buf;
99 struct gfs2_bufdata *bd;
100
101 while (!list_empty(head)) {
102 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
103 list_del_init(&bd->bd_list_tr);
104 tr->tr_num_buf--;
105 }
106 gfs2_assert_warn(sdp, !tr->tr_num_buf);
107}
108
109static void buf_lo_before_commit(struct gfs2_sbd *sdp)
110{
111 struct buffer_head *bh;
112 struct gfs2_log_descriptor *ld;
113 struct gfs2_bufdata *bd1 = NULL, *bd2;
114 unsigned int total = sdp->sd_log_num_buf;
115 unsigned int offset = sizeof(struct gfs2_log_descriptor);
116 unsigned int limit;
117 unsigned int num;
118 unsigned n;
119 __be64 *ptr;
120
121 offset += (sizeof(__be64) - 1);
122 offset &= ~(sizeof(__be64) - 1);
123 limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
124 /* for 4k blocks, limit = 503 */
125
126 bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
127 while(total) {
128 num = total;
129 if (total > limit)
130 num = limit;
131 bh = gfs2_log_get_buf(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400132 sdp->sd_log_num_hdrs++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 ld = (struct gfs2_log_descriptor *)bh->b_data;
134 ptr = (__be64 *)(bh->b_data + offset);
135 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500136 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
137 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
139 ld->ld_length = cpu_to_be32(num + 1);
140 ld->ld_data1 = cpu_to_be32(num);
141 ld->ld_data2 = cpu_to_be32(0);
142 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
143
144 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500145 list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
146 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
148 if (++n >= num)
149 break;
150 }
151
152 set_buffer_dirty(bh);
153 ll_rw_block(WRITE, 1, &bh);
154
155 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500156 list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
157 bd_le.le_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
159 set_buffer_dirty(bh);
160 ll_rw_block(WRITE, 1, &bh);
161 if (++n >= num)
162 break;
163 }
164
165 total -= num;
166 }
167}
168
169static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
170{
171 struct list_head *head = &sdp->sd_log_le_buf;
172 struct gfs2_bufdata *bd;
173
174 while (!list_empty(head)) {
175 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
176 list_del_init(&bd->bd_le.le_list);
177 sdp->sd_log_num_buf--;
178
Steven Whitehousea98ab222006-01-18 13:38:44 +0000179 gfs2_unpin(sdp, bd->bd_bh, ai);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 }
181 gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
182}
183
184static void buf_lo_before_scan(struct gfs2_jdesc *jd,
185 struct gfs2_log_header *head, int pass)
186{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500187 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
188 struct gfs2_sbd *sdp = ip->i_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189
190 if (pass != 0)
191 return;
192
193 sdp->sd_found_blocks = 0;
194 sdp->sd_replayed_blocks = 0;
195}
196
197static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
198 struct gfs2_log_descriptor *ld, __be64 *ptr,
199 int pass)
200{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500201 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
202 struct gfs2_sbd *sdp = ip->i_sbd;
203 struct gfs2_glock *gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 unsigned int blks = be32_to_cpu(ld->ld_data1);
205 struct buffer_head *bh_log, *bh_ip;
206 uint64_t blkno;
207 int error = 0;
208
209 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
210 return 0;
211
212 gfs2_replay_incr_blk(sdp, &start);
213
214 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
215 blkno = be64_to_cpu(*ptr++);
216
217 sdp->sd_found_blocks++;
218
219 if (gfs2_revoke_check(sdp, blkno, start))
220 continue;
221
222 error = gfs2_replay_read_block(jd, start, &bh_log);
223 if (error)
224 return error;
225
226 bh_ip = gfs2_meta_new(gl, blkno);
227 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
228
229 if (gfs2_meta_check(sdp, bh_ip))
230 error = -EIO;
231 else
232 mark_buffer_dirty(bh_ip);
233
234 brelse(bh_log);
235 brelse(bh_ip);
236
237 if (error)
238 break;
239
240 sdp->sd_replayed_blocks++;
241 }
242
243 return error;
244}
245
246static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
247{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500248 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
249 struct gfs2_sbd *sdp = ip->i_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250
251 if (error) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500252 gfs2_meta_sync(ip->i_gl,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500253 DIO_START | DIO_WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254 return;
255 }
256 if (pass != 1)
257 return;
258
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500259 gfs2_meta_sync(ip->i_gl, DIO_START | DIO_WAIT);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260
261 fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
262 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
263}
264
265static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
266{
267 struct gfs2_trans *tr;
268
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500269 tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000270 tr->tr_touched = 1;
271 tr->tr_num_revoke++;
272
273 gfs2_log_lock(sdp);
274 sdp->sd_log_num_revoke++;
275 list_add(&le->le_list, &sdp->sd_log_le_revoke);
276 gfs2_log_unlock(sdp);
277}
278
279static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
280{
281 struct gfs2_log_descriptor *ld;
282 struct gfs2_meta_header *mh;
283 struct buffer_head *bh;
284 unsigned int offset;
285 struct list_head *head = &sdp->sd_log_le_revoke;
286 struct gfs2_revoke *rv;
287
288 if (!sdp->sd_log_num_revoke)
289 return;
290
291 bh = gfs2_log_get_buf(sdp);
292 ld = (struct gfs2_log_descriptor *)bh->b_data;
293 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500294 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
295 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500297 ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
298 sizeof(uint64_t)));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
300 ld->ld_data2 = cpu_to_be32(0);
301 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
302 offset = sizeof(struct gfs2_log_descriptor);
303
304 while (!list_empty(head)) {
305 rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
Steven Whitehouse13538b82006-02-22 11:15:03 +0000306 list_del_init(&rv->rv_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307 sdp->sd_log_num_revoke--;
308
309 if (offset + sizeof(uint64_t) > sdp->sd_sb.sb_bsize) {
310 set_buffer_dirty(bh);
311 ll_rw_block(WRITE, 1, &bh);
312
313 bh = gfs2_log_get_buf(sdp);
314 mh = (struct gfs2_meta_header *)bh->b_data;
315 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
Steven Whitehousee3167de2006-03-30 15:46:23 -0500316 mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
317 mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 offset = sizeof(struct gfs2_meta_header);
319 }
320
321 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
322 kfree(rv);
323
324 offset += sizeof(uint64_t);
325 }
326 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
327
328 set_buffer_dirty(bh);
329 ll_rw_block(WRITE, 1, &bh);
330}
331
332static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
333 struct gfs2_log_header *head, int pass)
334{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500335 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
336 struct gfs2_sbd *sdp = ip->i_sbd;
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 Whitehouse5c676f62006-02-27 17:23:27 -0500349 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
350 struct gfs2_sbd *sdp = ip->i_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351 unsigned int blks = be32_to_cpu(ld->ld_length);
352 unsigned int revokes = be32_to_cpu(ld->ld_data1);
353 struct buffer_head *bh;
354 unsigned int offset;
355 uint64_t blkno;
356 int first = 1;
357 int error;
358
359 if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
360 return 0;
361
362 offset = sizeof(struct gfs2_log_descriptor);
363
364 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
365 error = gfs2_replay_read_block(jd, start, &bh);
366 if (error)
367 return error;
368
369 if (!first)
370 gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
371
372 while (offset + sizeof(uint64_t) <= sdp->sd_sb.sb_bsize) {
373 blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
374
375 error = gfs2_revoke_add(sdp, blkno, start);
376 if (error < 0)
377 return error;
378 else if (error)
379 sdp->sd_found_revokes++;
380
381 if (!--revokes)
382 break;
383 offset += sizeof(uint64_t);
384 }
385
386 brelse(bh);
387 offset = sizeof(struct gfs2_meta_header);
388 first = 0;
389 }
390
391 return 0;
392}
393
394static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
395{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500396 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
397 struct gfs2_sbd *sdp = ip->i_sbd;
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
419 if (!list_empty(&le->le_list))
420 return;
421
422 rgd = container_of(le, struct gfs2_rgrpd, rd_le);
423 gfs2_rgrp_bh_hold(rgd);
424
425 gfs2_log_lock(sdp);
426 sdp->sd_log_num_rg++;
427 list_add(&le->le_list, &sdp->sd_log_le_rg);
428 gfs2_log_unlock(sdp);
429}
430
431static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
432{
433 struct list_head *head = &sdp->sd_log_le_rg;
434 struct gfs2_rgrpd *rgd;
435
436 while (!list_empty(head)) {
437 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
438 list_del_init(&rgd->rd_le.le_list);
439 sdp->sd_log_num_rg--;
440
441 gfs2_rgrp_repolish_clones(rgd);
442 gfs2_rgrp_bh_put(rgd);
443 }
444 gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
445}
446
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000447/**
448 * databuf_lo_add - Add a databuf to the transaction.
449 *
450 * This is used in two distinct cases:
451 * i) In ordered write mode
452 * We put the data buffer on a list so that we can ensure that its
453 * synced to disk at the right time
454 * ii) In journaled data mode
455 * We need to journal the data block in the same way as metadata in
456 * the functions above. The difference is that here we have a tag
457 * which is two __be64's being the block number (as per meta data)
458 * and a flag which says whether the data block needs escaping or
459 * not. This means we need a new log entry for each 251 or so data
460 * blocks, which isn't an enormous overhead but twice as much as
461 * for normal metadata blocks.
462 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000463static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
464{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000465 struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500466 struct gfs2_trans *tr = current->journal_info;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000467 struct address_space *mapping = bd->bd_bh->b_page->mapping;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500468 struct gfs2_inode *ip = mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000470 tr->tr_touched = 1;
471 if (!list_empty(&bd->bd_list_tr) &&
472 (ip->i_di.di_flags & GFS2_DIF_JDATA)) {
473 tr->tr_num_buf++;
474 gfs2_trans_add_gl(bd->bd_gl);
475 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
476 gfs2_pin(sdp, bd->bd_bh);
Steven Whitehouseb4dc7292006-03-01 17:41:58 -0500477 tr->tr_num_buf_new++;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000478 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 gfs2_log_lock(sdp);
Steven Whitehouse13538b82006-02-22 11:15:03 +0000480 if (!list_empty(&le->le_list)) {
481 if (ip->i_di.di_flags & GFS2_DIF_JDATA)
482 sdp->sd_log_num_jdata++;
483 sdp->sd_log_num_databuf++;
484 list_add(&le->le_list, &sdp->sd_log_le_databuf);
485 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486 gfs2_log_unlock(sdp);
487}
488
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000489static int gfs2_check_magic(struct buffer_head *bh)
490{
491 struct page *page = bh->b_page;
492 void *kaddr;
493 __be32 *ptr;
494 int rv = 0;
495
496 kaddr = kmap_atomic(page, KM_USER0);
497 ptr = kaddr + bh_offset(bh);
498 if (*ptr == cpu_to_be32(GFS2_MAGIC))
499 rv = 1;
500 kunmap_atomic(page, KM_USER0);
501
502 return rv;
503}
504
505/**
506 * databuf_lo_before_commit - Scan the data buffers, writing as we go
507 *
508 * Here we scan through the lists of buffers and make the assumption
509 * that any buffer thats been pinned is being journaled, and that
510 * any unpinned buffer is an ordered write data buffer and therefore
511 * will be written back rather than journaled.
512 */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
514{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000515 LIST_HEAD(started);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000516 struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
517 struct buffer_head *bh = NULL;
518 unsigned int offset = sizeof(struct gfs2_log_descriptor);
519 struct gfs2_log_descriptor *ld;
520 unsigned int limit;
521 unsigned int total_dbuf = sdp->sd_log_num_databuf;
522 unsigned int total_jdata = sdp->sd_log_num_jdata;
523 unsigned int num, n;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000524 __be64 *ptr = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000526 offset += (2*sizeof(__be64) - 1);
527 offset &= ~(2*sizeof(__be64) - 1);
528 limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000530 /*
531 * Start writing ordered buffers, write journaled buffers
532 * into the log along with a header
533 */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000534 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500535 bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
536 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000537 while(total_dbuf) {
538 num = total_jdata;
539 if (num > limit)
540 num = limit;
541 n = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500542 list_for_each_entry_safe_continue(bd1, bdt,
543 &sdp->sd_log_le_databuf,
544 bd_le.le_list) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000545 /* An ordered write buffer */
546 if (bd1->bd_bh && !buffer_pinned(bd1->bd_bh)) {
547 list_move(&bd1->bd_le.le_list, &started);
548 if (bd1 == bd2) {
549 bd2 = NULL;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500550 bd2 = list_prepare_entry(bd2,
551 &sdp->sd_log_le_databuf,
552 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000553 }
554 total_dbuf--;
555 if (bd1->bd_bh) {
556 get_bh(bd1->bd_bh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000557 if (buffer_dirty(bd1->bd_bh)) {
Steven Whitehousef55ab262006-02-21 12:51:39 +0000558 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000559 wait_on_buffer(bd1->bd_bh);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500560 ll_rw_block(WRITE, 1,
561 &bd1->bd_bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000562 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000563 }
564 brelse(bd1->bd_bh);
565 continue;
566 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000567 continue;
568 } else if (bd1->bd_bh) { /* A journaled buffer */
569 int magic;
570 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000571 if (!bh) {
572 bh = gfs2_log_get_buf(sdp);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400573 sdp->sd_log_num_hdrs++;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500574 ld = (struct gfs2_log_descriptor *)
575 bh->b_data;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000576 ptr = (__be64 *)(bh->b_data + offset);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500577 ld->ld_header.mh_magic =
578 cpu_to_be32(GFS2_MAGIC);
579 ld->ld_header.mh_type =
Steven Whitehousee3167de2006-03-30 15:46:23 -0500580 cpu_to_be32(GFS2_METATYPE_LD);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500581 ld->ld_header.mh_format =
Steven Whitehousee3167de2006-03-30 15:46:23 -0500582 cpu_to_be32(GFS2_FORMAT_LD);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500583 ld->ld_type =
584 cpu_to_be32(GFS2_LOG_DESC_JDATA);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000585 ld->ld_length = cpu_to_be32(num + 1);
586 ld->ld_data1 = cpu_to_be32(num);
587 ld->ld_data2 = cpu_to_be32(0);
588 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
589 }
590 magic = gfs2_check_magic(bd1->bd_bh);
591 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
592 *ptr++ = cpu_to_be64((__u64)magic);
593 clear_buffer_escaped(bd1->bd_bh);
594 if (unlikely(magic != 0))
595 set_buffer_escaped(bd1->bd_bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000596 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000597 if (n++ > num)
598 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000600 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000601 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000602 if (bh) {
603 set_buffer_dirty(bh);
604 ll_rw_block(WRITE, 1, &bh);
605 bh = NULL;
606 }
607 n = 0;
Steven Whitehousef55ab262006-02-21 12:51:39 +0000608 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500609 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
610 bd_le.le_list) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000611 if (!bd2->bd_bh)
612 continue;
613 /* copy buffer if it needs escaping */
Steven Whitehousef55ab262006-02-21 12:51:39 +0000614 gfs2_log_unlock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000615 if (unlikely(buffer_escaped(bd2->bd_bh))) {
616 void *kaddr;
617 struct page *page = bd2->bd_bh->b_page;
618 bh = gfs2_log_get_buf(sdp);
619 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500620 memcpy(bh->b_data,
621 kaddr + bh_offset(bd2->bd_bh),
622 sdp->sd_sb.sb_bsize);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000623 kunmap_atomic(page, KM_USER0);
624 *(__be32 *)bh->b_data = 0;
625 } else {
626 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
627 }
628 set_buffer_dirty(bh);
629 ll_rw_block(WRITE, 1, &bh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000630 gfs2_log_lock(sdp);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000631 if (++n >= num)
632 break;
633 }
634 bh = NULL;
635 total_dbuf -= num;
636 total_jdata -= num;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 }
Steven Whitehousef55ab262006-02-21 12:51:39 +0000638 gfs2_log_unlock(sdp);
639
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000640 /* Wait on all ordered buffers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 while (!list_empty(&started)) {
Steven Whitehouse13538b82006-02-22 11:15:03 +0000642 gfs2_log_lock(sdp);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500643 bd1 = list_entry(started.next, struct gfs2_bufdata,
644 bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000645 list_del(&bd1->bd_le.le_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 sdp->sd_log_num_databuf--;
647
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000648 bh = bd1->bd_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649 if (bh) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500650 bh->b_private = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651 gfs2_log_unlock(sdp);
652 wait_on_buffer(bh);
653 brelse(bh);
654 } else
655 gfs2_log_unlock(sdp);
656
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000657 kfree(bd1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 }
659
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000660 /* We've removed all the ordered write bufs here, so only jdata left */
661 gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662}
663
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000664static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
665 struct gfs2_log_descriptor *ld,
666 __be64 *ptr, int pass)
667{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500668 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
669 struct gfs2_sbd *sdp = ip->i_sbd;
670 struct gfs2_glock *gl = ip->i_gl;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000671 unsigned int blks = be32_to_cpu(ld->ld_data1);
672 struct buffer_head *bh_log, *bh_ip;
673 uint64_t blkno;
674 uint64_t esc;
675 int error = 0;
676
677 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
678 return 0;
679
680 gfs2_replay_incr_blk(sdp, &start);
681 for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
682 blkno = be64_to_cpu(*ptr++);
683 esc = be64_to_cpu(*ptr++);
684
685 sdp->sd_found_blocks++;
686
687 if (gfs2_revoke_check(sdp, blkno, start))
688 continue;
689
690 error = gfs2_replay_read_block(jd, start, &bh_log);
691 if (error)
692 return error;
693
694 bh_ip = gfs2_meta_new(gl, blkno);
695 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
696
697 /* Unescape */
698 if (esc) {
699 __be32 *eptr = (__be32 *)bh_ip->b_data;
700 *eptr = cpu_to_be32(GFS2_MAGIC);
701 }
702 mark_buffer_dirty(bh_ip);
703
704 brelse(bh_log);
705 brelse(bh_ip);
706 if (error)
707 break;
708
709 sdp->sd_replayed_blocks++;
710 }
711
712 return error;
713}
714
715/* FIXME: sort out accounting for log blocks etc. */
716
717static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
718{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500719 struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
720 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000721
722 if (error) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500723 gfs2_meta_sync(ip->i_gl,
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500724 DIO_START | DIO_WAIT);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000725 return;
726 }
727 if (pass != 1)
728 return;
729
730 /* data sync? */
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500731 gfs2_meta_sync(ip->i_gl, DIO_START | DIO_WAIT);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000732
733 fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
734 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
735}
736
737static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
738{
739 struct list_head *head = &sdp->sd_log_le_databuf;
740 struct gfs2_bufdata *bd;
741
742 while (!list_empty(head)) {
743 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000744 list_del(&bd->bd_le.le_list);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000745 sdp->sd_log_num_databuf--;
746 sdp->sd_log_num_jdata--;
747 gfs2_unpin(sdp, bd->bd_bh, ai);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000748 }
749 gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
750 gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
751}
752
753
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400754const struct gfs2_log_operations gfs2_glock_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 .lo_add = glock_lo_add,
756 .lo_after_commit = glock_lo_after_commit,
757 .lo_name = "glock"
758};
759
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400760const struct gfs2_log_operations gfs2_buf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 .lo_add = buf_lo_add,
762 .lo_incore_commit = buf_lo_incore_commit,
763 .lo_before_commit = buf_lo_before_commit,
764 .lo_after_commit = buf_lo_after_commit,
765 .lo_before_scan = buf_lo_before_scan,
766 .lo_scan_elements = buf_lo_scan_elements,
767 .lo_after_scan = buf_lo_after_scan,
768 .lo_name = "buf"
769};
770
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400771const struct gfs2_log_operations gfs2_revoke_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000772 .lo_add = revoke_lo_add,
773 .lo_before_commit = revoke_lo_before_commit,
774 .lo_before_scan = revoke_lo_before_scan,
775 .lo_scan_elements = revoke_lo_scan_elements,
776 .lo_after_scan = revoke_lo_after_scan,
777 .lo_name = "revoke"
778};
779
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400780const struct gfs2_log_operations gfs2_rg_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 .lo_add = rg_lo_add,
782 .lo_after_commit = rg_lo_after_commit,
783 .lo_name = "rg"
784};
785
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400786const struct gfs2_log_operations gfs2_databuf_lops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 .lo_add = databuf_lo_add,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000788 .lo_incore_commit = buf_lo_incore_commit,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789 .lo_before_commit = databuf_lo_before_commit,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000790 .lo_after_commit = databuf_lo_after_commit,
791 .lo_scan_elements = databuf_lo_scan_elements,
792 .lo_after_scan = databuf_lo_after_scan,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 .lo_name = "databuf"
794};
795
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400796const struct gfs2_log_operations *gfs2_log_ops[] = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 &gfs2_glock_lops,
798 &gfs2_buf_lops,
799 &gfs2_revoke_lops,
800 &gfs2_rg_lops,
801 &gfs2_databuf_lops,
802 NULL
803};
804