Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
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> | |
5c676f6d | 15 | #include <linux/gfs2_ondisk.h> |
7d308590 | 16 | #include <linux/lm_interface.h> |
b3b94faa DT |
17 | |
18 | #include "gfs2.h" | |
5c676f6d | 19 | #include "incore.h" |
b3b94faa DT |
20 | #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" | |
5c676f6d | 27 | #include "util.h" |
b3b94faa DT |
28 | |
29 | static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
30 | { | |
31 | struct gfs2_glock *gl; | |
5c676f6d | 32 | struct gfs2_trans *tr = current->journal_info; |
b3b94faa | 33 | |
5c676f6d | 34 | tr->tr_touched = 1; |
b3b94faa | 35 | |
b3b94faa DT |
36 | gl = container_of(le, struct gfs2_glock, gl_le); |
37 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl))) | |
38 | return; | |
b3b94faa DT |
39 | |
40 | gfs2_log_lock(sdp); | |
68835625 BM |
41 | 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); | |
b3b94faa DT |
47 | sdp->sd_log_num_gl++; |
48 | list_add(&le->le_list, &sdp->sd_log_le_gl); | |
49 | gfs2_log_unlock(sdp); | |
50 | } | |
51 | ||
52 | static 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 | ||
68 | static 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 | ||
8bd95727 SW |
73 | gfs2_log_lock(sdp); |
74 | if (!list_empty(&bd->bd_list_tr)) { | |
75 | gfs2_log_unlock(sdp); | |
b3b94faa | 76 | return; |
8bd95727 | 77 | } |
5c676f6d | 78 | tr = current->journal_info; |
b3b94faa DT |
79 | tr->tr_touched = 1; |
80 | tr->tr_num_buf++; | |
81 | list_add(&bd->bd_list_tr, &tr->tr_list_buf); | |
8bd95727 | 82 | gfs2_log_unlock(sdp); |
b3b94faa DT |
83 | |
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); | |
a98ab220 | 90 | gfs2_pin(sdp, bd->bd_bh); |
b3b94faa DT |
91 | 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 | ||
99 | static 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 | ||
8bd95727 | 104 | gfs2_log_lock(sdp); |
b3b94faa DT |
105 | 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 | } | |
8bd95727 | 110 | gfs2_log_unlock(sdp); |
b3b94faa DT |
111 | gfs2_assert_warn(sdp, !tr->tr_num_buf); |
112 | } | |
113 | ||
114 | static 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 | ||
82ffa516 | 126 | offset += sizeof(__be64) - 1; |
b3b94faa DT |
127 | 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); | |
b09e593d | 137 | sdp->sd_log_num_hdrs++; |
b3b94faa DT |
138 | 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); | |
e3167ded SW |
141 | ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD); |
142 | ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD); | |
b3b94faa DT |
143 | 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; | |
568f4c96 SW |
150 | list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf, |
151 | bd_le.le_list) { | |
b3b94faa DT |
152 | *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; | |
568f4c96 SW |
161 | list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf, |
162 | bd_le.le_list) { | |
b3b94faa DT |
163 | 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 | ||
174 | static 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 | ||
a98ab220 | 184 | gfs2_unpin(sdp, bd->bd_bh, ai); |
b3b94faa DT |
185 | } |
186 | gfs2_assert_warn(sdp, !sdp->sd_log_num_buf); | |
187 | } | |
188 | ||
189 | static void buf_lo_before_scan(struct gfs2_jdesc *jd, | |
55167622 | 190 | struct gfs2_log_header_host *head, int pass) |
b3b94faa | 191 | { |
feaa7bba | 192 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
193 | |
194 | if (pass != 0) | |
195 | return; | |
196 | ||
197 | sdp->sd_found_blocks = 0; | |
198 | sdp->sd_replayed_blocks = 0; | |
199 | } | |
200 | ||
201 | static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, | |
202 | struct gfs2_log_descriptor *ld, __be64 *ptr, | |
203 | int pass) | |
204 | { | |
feaa7bba SW |
205 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
206 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
5c676f6d | 207 | struct gfs2_glock *gl = ip->i_gl; |
b3b94faa DT |
208 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
209 | struct buffer_head *bh_log, *bh_ip; | |
cd915493 | 210 | u64 blkno; |
b3b94faa DT |
211 | 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); | |
82ffa516 SW |
227 | if (error) |
228 | return error; | |
b3b94faa DT |
229 | |
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 | ||
250 | static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
251 | { | |
feaa7bba SW |
252 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
253 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
b3b94faa DT |
254 | |
255 | if (error) { | |
7276b3b0 | 256 | gfs2_meta_sync(ip->i_gl); |
b3b94faa DT |
257 | return; |
258 | } | |
259 | if (pass != 1) | |
260 | return; | |
261 | ||
7276b3b0 | 262 | gfs2_meta_sync(ip->i_gl); |
b3b94faa DT |
263 | |
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 | ||
268 | static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
269 | { | |
270 | struct gfs2_trans *tr; | |
271 | ||
5c676f6d | 272 | tr = current->journal_info; |
b3b94faa DT |
273 | 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 | ||
282 | static 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); | |
e3167ded SW |
297 | ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD); |
298 | ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD); | |
b3b94faa | 299 | ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE); |
568f4c96 | 300 | ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, |
cd915493 | 301 | sizeof(u64))); |
b3b94faa DT |
302 | 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); | |
13538b8e | 309 | list_del_init(&rv->rv_le.le_list); |
b3b94faa DT |
310 | sdp->sd_log_num_revoke--; |
311 | ||
cd915493 | 312 | if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) { |
b3b94faa DT |
313 | 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); | |
e3167ded SW |
319 | mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB); |
320 | mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB); | |
b3b94faa DT |
321 | offset = sizeof(struct gfs2_meta_header); |
322 | } | |
323 | ||
324 | *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno); | |
325 | kfree(rv); | |
326 | ||
cd915493 | 327 | offset += sizeof(u64); |
b3b94faa DT |
328 | } |
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 | ||
335 | static void revoke_lo_before_scan(struct gfs2_jdesc *jd, | |
55167622 | 336 | struct gfs2_log_header_host *head, int pass) |
b3b94faa | 337 | { |
feaa7bba | 338 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
339 | |
340 | if (pass != 0) | |
341 | return; | |
342 | ||
343 | sdp->sd_found_revokes = 0; | |
344 | sdp->sd_replay_tail = head->lh_tail; | |
345 | } | |
346 | ||
347 | static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, | |
348 | struct gfs2_log_descriptor *ld, __be64 *ptr, | |
349 | int pass) | |
350 | { | |
feaa7bba | 351 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
352 | 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; | |
cd915493 | 356 | u64 blkno; |
b3b94faa DT |
357 | 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 | ||
cd915493 | 373 | while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) { |
b3b94faa DT |
374 | 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; | |
cd915493 | 384 | offset += sizeof(u64); |
b3b94faa DT |
385 | } |
386 | ||
387 | brelse(bh); | |
388 | offset = sizeof(struct gfs2_meta_header); | |
389 | first = 0; | |
390 | } | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
396 | { | |
feaa7bba | 397 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
398 | |
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 | ||
412 | static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
413 | { | |
414 | struct gfs2_rgrpd *rgd; | |
5c676f6d | 415 | struct gfs2_trans *tr = current->journal_info; |
b3b94faa | 416 | |
5c676f6d | 417 | tr->tr_touched = 1; |
b3b94faa | 418 | |
b3b94faa | 419 | rgd = container_of(le, struct gfs2_rgrpd, rd_le); |
b3b94faa DT |
420 | |
421 | gfs2_log_lock(sdp); | |
68835625 BM |
422 | if (!list_empty(&le->le_list)){ |
423 | gfs2_log_unlock(sdp); | |
424 | return; | |
425 | } | |
426 | gfs2_rgrp_bh_hold(rgd); | |
b3b94faa DT |
427 | sdp->sd_log_num_rg++; |
428 | list_add(&le->le_list, &sdp->sd_log_le_rg); | |
907b9bce | 429 | gfs2_log_unlock(sdp); |
b3b94faa DT |
430 | } |
431 | ||
432 | static 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 | ||
18ec7d5c SW |
448 | /** |
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 | */ | |
b3b94faa DT |
464 | static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) |
465 | { | |
18ec7d5c | 466 | struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le); |
5c676f6d | 467 | struct gfs2_trans *tr = current->journal_info; |
18ec7d5c | 468 | struct address_space *mapping = bd->bd_bh->b_page->mapping; |
feaa7bba | 469 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
b3b94faa | 470 | |
8bd95727 | 471 | gfs2_log_lock(sdp); |
18ec7d5c | 472 | tr->tr_touched = 1; |
15d00c0b | 473 | if (list_empty(&bd->bd_list_tr) && |
18ec7d5c SW |
474 | (ip->i_di.di_flags & GFS2_DIF_JDATA)) { |
475 | tr->tr_num_buf++; | |
18ec7d5c | 476 | list_add(&bd->bd_list_tr, &tr->tr_list_buf); |
8bd95727 | 477 | gfs2_log_unlock(sdp); |
18ec7d5c | 478 | gfs2_pin(sdp, bd->bd_bh); |
b4dc7291 | 479 | tr->tr_num_buf_new++; |
8bd95727 SW |
480 | } else { |
481 | gfs2_log_unlock(sdp); | |
18ec7d5c | 482 | } |
b61dde79 | 483 | gfs2_trans_add_gl(bd->bd_gl); |
b3b94faa | 484 | gfs2_log_lock(sdp); |
15d00c0b | 485 | if (list_empty(&le->le_list)) { |
13538b8e SW |
486 | if (ip->i_di.di_flags & GFS2_DIF_JDATA) |
487 | sdp->sd_log_num_jdata++; | |
488 | sdp->sd_log_num_databuf++; | |
489 | list_add(&le->le_list, &sdp->sd_log_le_databuf); | |
490 | } | |
b3b94faa DT |
491 | gfs2_log_unlock(sdp); |
492 | } | |
493 | ||
18ec7d5c SW |
494 | static int gfs2_check_magic(struct buffer_head *bh) |
495 | { | |
496 | struct page *page = bh->b_page; | |
497 | void *kaddr; | |
498 | __be32 *ptr; | |
499 | int rv = 0; | |
500 | ||
501 | kaddr = kmap_atomic(page, KM_USER0); | |
502 | ptr = kaddr + bh_offset(bh); | |
503 | if (*ptr == cpu_to_be32(GFS2_MAGIC)) | |
504 | rv = 1; | |
c312c4fd | 505 | kunmap_atomic(kaddr, KM_USER0); |
18ec7d5c SW |
506 | |
507 | return rv; | |
508 | } | |
509 | ||
510 | /** | |
511 | * databuf_lo_before_commit - Scan the data buffers, writing as we go | |
512 | * | |
513 | * Here we scan through the lists of buffers and make the assumption | |
514 | * that any buffer thats been pinned is being journaled, and that | |
515 | * any unpinned buffer is an ordered write data buffer and therefore | |
516 | * will be written back rather than journaled. | |
517 | */ | |
b3b94faa DT |
518 | static void databuf_lo_before_commit(struct gfs2_sbd *sdp) |
519 | { | |
b3b94faa | 520 | LIST_HEAD(started); |
18ec7d5c | 521 | struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt; |
70209331 | 522 | struct buffer_head *bh = NULL,*bh1 = NULL; |
18ec7d5c SW |
523 | unsigned int offset = sizeof(struct gfs2_log_descriptor); |
524 | struct gfs2_log_descriptor *ld; | |
525 | unsigned int limit; | |
526 | unsigned int total_dbuf = sdp->sd_log_num_databuf; | |
527 | unsigned int total_jdata = sdp->sd_log_num_jdata; | |
528 | unsigned int num, n; | |
f55ab26a | 529 | __be64 *ptr = NULL; |
b3b94faa | 530 | |
a67cdbd4 | 531 | offset += 2*sizeof(__be64) - 1; |
18ec7d5c SW |
532 | offset &= ~(2*sizeof(__be64) - 1); |
533 | limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64); | |
b3b94faa | 534 | |
18ec7d5c SW |
535 | /* |
536 | * Start writing ordered buffers, write journaled buffers | |
537 | * into the log along with a header | |
538 | */ | |
f55ab26a | 539 | gfs2_log_lock(sdp); |
568f4c96 SW |
540 | bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf, |
541 | bd_le.le_list); | |
18ec7d5c SW |
542 | while(total_dbuf) { |
543 | num = total_jdata; | |
544 | if (num > limit) | |
545 | num = limit; | |
546 | n = 0; | |
568f4c96 SW |
547 | list_for_each_entry_safe_continue(bd1, bdt, |
548 | &sdp->sd_log_le_databuf, | |
549 | bd_le.le_list) { | |
70209331 RC |
550 | /* store off the buffer head in a local ptr since |
551 | * gfs2_bufdata might change when we drop the log lock | |
552 | */ | |
553 | bh1 = bd1->bd_bh; | |
554 | ||
18ec7d5c | 555 | /* An ordered write buffer */ |
70209331 | 556 | if (bh1 && !buffer_pinned(bh1)) { |
18ec7d5c SW |
557 | list_move(&bd1->bd_le.le_list, &started); |
558 | if (bd1 == bd2) { | |
559 | bd2 = NULL; | |
568f4c96 SW |
560 | bd2 = list_prepare_entry(bd2, |
561 | &sdp->sd_log_le_databuf, | |
562 | bd_le.le_list); | |
18ec7d5c SW |
563 | } |
564 | total_dbuf--; | |
70209331 RC |
565 | if (bh1) { |
566 | if (buffer_dirty(bh1)) { | |
567 | get_bh(bh1); | |
568 | ||
f55ab26a | 569 | gfs2_log_unlock(sdp); |
70209331 RC |
570 | |
571 | ll_rw_block(SWRITE, 1, &bh1); | |
572 | brelse(bh1); | |
573 | ||
f55ab26a | 574 | gfs2_log_lock(sdp); |
18ec7d5c | 575 | } |
18ec7d5c SW |
576 | continue; |
577 | } | |
18ec7d5c | 578 | continue; |
70209331 | 579 | } else if (bh1) { /* A journaled buffer */ |
18ec7d5c SW |
580 | int magic; |
581 | gfs2_log_unlock(sdp); | |
18ec7d5c SW |
582 | if (!bh) { |
583 | bh = gfs2_log_get_buf(sdp); | |
b09e593d | 584 | sdp->sd_log_num_hdrs++; |
568f4c96 SW |
585 | ld = (struct gfs2_log_descriptor *) |
586 | bh->b_data; | |
18ec7d5c | 587 | ptr = (__be64 *)(bh->b_data + offset); |
568f4c96 SW |
588 | ld->ld_header.mh_magic = |
589 | cpu_to_be32(GFS2_MAGIC); | |
590 | ld->ld_header.mh_type = | |
e3167ded | 591 | cpu_to_be32(GFS2_METATYPE_LD); |
568f4c96 | 592 | ld->ld_header.mh_format = |
e3167ded | 593 | cpu_to_be32(GFS2_FORMAT_LD); |
568f4c96 SW |
594 | ld->ld_type = |
595 | cpu_to_be32(GFS2_LOG_DESC_JDATA); | |
18ec7d5c SW |
596 | ld->ld_length = cpu_to_be32(num + 1); |
597 | ld->ld_data1 = cpu_to_be32(num); | |
598 | ld->ld_data2 = cpu_to_be32(0); | |
599 | memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved)); | |
600 | } | |
70209331 RC |
601 | magic = gfs2_check_magic(bh1); |
602 | *ptr++ = cpu_to_be64(bh1->b_blocknr); | |
18ec7d5c | 603 | *ptr++ = cpu_to_be64((__u64)magic); |
70209331 | 604 | clear_buffer_escaped(bh1); |
18ec7d5c | 605 | if (unlikely(magic != 0)) |
70209331 | 606 | set_buffer_escaped(bh1); |
f55ab26a | 607 | gfs2_log_lock(sdp); |
18ec7d5c SW |
608 | if (n++ > num) |
609 | break; | |
70209331 | 610 | } else if (!bh1) { |
623d9355 SW |
611 | total_dbuf--; |
612 | sdp->sd_log_num_databuf--; | |
613 | list_del_init(&bd1->bd_le.le_list); | |
614 | if (bd1 == bd2) { | |
615 | bd2 = NULL; | |
616 | bd2 = list_prepare_entry(bd2, | |
617 | &sdp->sd_log_le_databuf, | |
618 | bd_le.le_list); | |
619 | } | |
620 | kmem_cache_free(gfs2_bufdata_cachep, bd1); | |
18ec7d5c SW |
621 | } |
622 | } | |
f55ab26a | 623 | gfs2_log_unlock(sdp); |
b3b94faa | 624 | if (bh) { |
18ec7d5c SW |
625 | set_buffer_dirty(bh); |
626 | ll_rw_block(WRITE, 1, &bh); | |
627 | bh = NULL; | |
628 | } | |
629 | n = 0; | |
f55ab26a | 630 | gfs2_log_lock(sdp); |
568f4c96 SW |
631 | list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf, |
632 | bd_le.le_list) { | |
18ec7d5c SW |
633 | if (!bd2->bd_bh) |
634 | continue; | |
635 | /* copy buffer if it needs escaping */ | |
f55ab26a | 636 | gfs2_log_unlock(sdp); |
18ec7d5c SW |
637 | if (unlikely(buffer_escaped(bd2->bd_bh))) { |
638 | void *kaddr; | |
639 | struct page *page = bd2->bd_bh->b_page; | |
640 | bh = gfs2_log_get_buf(sdp); | |
641 | kaddr = kmap_atomic(page, KM_USER0); | |
568f4c96 SW |
642 | memcpy(bh->b_data, |
643 | kaddr + bh_offset(bd2->bd_bh), | |
644 | sdp->sd_sb.sb_bsize); | |
c312c4fd | 645 | kunmap_atomic(kaddr, KM_USER0); |
18ec7d5c SW |
646 | *(__be32 *)bh->b_data = 0; |
647 | } else { | |
648 | bh = gfs2_log_fake_buf(sdp, bd2->bd_bh); | |
b3b94faa | 649 | } |
18ec7d5c SW |
650 | set_buffer_dirty(bh); |
651 | ll_rw_block(WRITE, 1, &bh); | |
f55ab26a | 652 | gfs2_log_lock(sdp); |
18ec7d5c SW |
653 | if (++n >= num) |
654 | break; | |
655 | } | |
656 | bh = NULL; | |
657 | total_dbuf -= num; | |
658 | total_jdata -= num; | |
b3b94faa | 659 | } |
f55ab26a SW |
660 | gfs2_log_unlock(sdp); |
661 | ||
18ec7d5c | 662 | /* Wait on all ordered buffers */ |
b3b94faa | 663 | while (!list_empty(&started)) { |
13538b8e | 664 | gfs2_log_lock(sdp); |
568f4c96 SW |
665 | bd1 = list_entry(started.next, struct gfs2_bufdata, |
666 | bd_le.le_list); | |
08867605 | 667 | list_del_init(&bd1->bd_le.le_list); |
b3b94faa | 668 | sdp->sd_log_num_databuf--; |
18ec7d5c | 669 | bh = bd1->bd_bh; |
b3b94faa | 670 | if (bh) { |
5c676f6d | 671 | bh->b_private = NULL; |
15d00c0b | 672 | get_bh(bh); |
b3b94faa DT |
673 | gfs2_log_unlock(sdp); |
674 | wait_on_buffer(bh); | |
675 | brelse(bh); | |
676 | } else | |
677 | gfs2_log_unlock(sdp); | |
678 | ||
3a8476dd | 679 | kmem_cache_free(gfs2_bufdata_cachep, bd1); |
b3b94faa DT |
680 | } |
681 | ||
18ec7d5c SW |
682 | /* We've removed all the ordered write bufs here, so only jdata left */ |
683 | gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata); | |
684 | } | |
685 | ||
686 | static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, | |
687 | struct gfs2_log_descriptor *ld, | |
688 | __be64 *ptr, int pass) | |
689 | { | |
feaa7bba SW |
690 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
691 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
5c676f6d | 692 | struct gfs2_glock *gl = ip->i_gl; |
18ec7d5c SW |
693 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
694 | struct buffer_head *bh_log, *bh_ip; | |
cd915493 SW |
695 | u64 blkno; |
696 | u64 esc; | |
18ec7d5c SW |
697 | int error = 0; |
698 | ||
699 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA) | |
700 | return 0; | |
701 | ||
702 | gfs2_replay_incr_blk(sdp, &start); | |
703 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { | |
704 | blkno = be64_to_cpu(*ptr++); | |
705 | esc = be64_to_cpu(*ptr++); | |
706 | ||
707 | sdp->sd_found_blocks++; | |
708 | ||
709 | if (gfs2_revoke_check(sdp, blkno, start)) | |
710 | continue; | |
711 | ||
712 | error = gfs2_replay_read_block(jd, start, &bh_log); | |
713 | if (error) | |
714 | return error; | |
715 | ||
716 | bh_ip = gfs2_meta_new(gl, blkno); | |
717 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); | |
718 | ||
719 | /* Unescape */ | |
720 | if (esc) { | |
721 | __be32 *eptr = (__be32 *)bh_ip->b_data; | |
722 | *eptr = cpu_to_be32(GFS2_MAGIC); | |
723 | } | |
724 | mark_buffer_dirty(bh_ip); | |
725 | ||
726 | brelse(bh_log); | |
727 | brelse(bh_ip); | |
728 | if (error) | |
729 | break; | |
730 | ||
731 | sdp->sd_replayed_blocks++; | |
732 | } | |
733 | ||
734 | return error; | |
735 | } | |
736 | ||
737 | /* FIXME: sort out accounting for log blocks etc. */ | |
738 | ||
739 | static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
740 | { | |
feaa7bba SW |
741 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
742 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
18ec7d5c SW |
743 | |
744 | if (error) { | |
7276b3b0 | 745 | gfs2_meta_sync(ip->i_gl); |
18ec7d5c SW |
746 | return; |
747 | } | |
748 | if (pass != 1) | |
749 | return; | |
750 | ||
751 | /* data sync? */ | |
7276b3b0 | 752 | gfs2_meta_sync(ip->i_gl); |
18ec7d5c SW |
753 | |
754 | fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n", | |
755 | jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks); | |
756 | } | |
757 | ||
758 | static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) | |
759 | { | |
760 | struct list_head *head = &sdp->sd_log_le_databuf; | |
761 | struct gfs2_bufdata *bd; | |
762 | ||
763 | while (!list_empty(head)) { | |
764 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); | |
b8e1aabf | 765 | list_del_init(&bd->bd_le.le_list); |
18ec7d5c SW |
766 | sdp->sd_log_num_databuf--; |
767 | sdp->sd_log_num_jdata--; | |
768 | gfs2_unpin(sdp, bd->bd_bh, ai); | |
18ec7d5c | 769 | } |
b3b94faa | 770 | gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf); |
18ec7d5c | 771 | gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata); |
b3b94faa DT |
772 | } |
773 | ||
18ec7d5c | 774 | |
b09e593d | 775 | const struct gfs2_log_operations gfs2_glock_lops = { |
b3b94faa DT |
776 | .lo_add = glock_lo_add, |
777 | .lo_after_commit = glock_lo_after_commit, | |
ea67eedb | 778 | .lo_name = "glock", |
b3b94faa DT |
779 | }; |
780 | ||
b09e593d | 781 | const struct gfs2_log_operations gfs2_buf_lops = { |
b3b94faa DT |
782 | .lo_add = buf_lo_add, |
783 | .lo_incore_commit = buf_lo_incore_commit, | |
784 | .lo_before_commit = buf_lo_before_commit, | |
785 | .lo_after_commit = buf_lo_after_commit, | |
786 | .lo_before_scan = buf_lo_before_scan, | |
787 | .lo_scan_elements = buf_lo_scan_elements, | |
788 | .lo_after_scan = buf_lo_after_scan, | |
ea67eedb | 789 | .lo_name = "buf", |
b3b94faa DT |
790 | }; |
791 | ||
b09e593d | 792 | const struct gfs2_log_operations gfs2_revoke_lops = { |
b3b94faa DT |
793 | .lo_add = revoke_lo_add, |
794 | .lo_before_commit = revoke_lo_before_commit, | |
795 | .lo_before_scan = revoke_lo_before_scan, | |
796 | .lo_scan_elements = revoke_lo_scan_elements, | |
797 | .lo_after_scan = revoke_lo_after_scan, | |
ea67eedb | 798 | .lo_name = "revoke", |
b3b94faa DT |
799 | }; |
800 | ||
b09e593d | 801 | const struct gfs2_log_operations gfs2_rg_lops = { |
b3b94faa DT |
802 | .lo_add = rg_lo_add, |
803 | .lo_after_commit = rg_lo_after_commit, | |
ea67eedb | 804 | .lo_name = "rg", |
b3b94faa DT |
805 | }; |
806 | ||
b09e593d | 807 | const struct gfs2_log_operations gfs2_databuf_lops = { |
b3b94faa | 808 | .lo_add = databuf_lo_add, |
18ec7d5c | 809 | .lo_incore_commit = buf_lo_incore_commit, |
b3b94faa | 810 | .lo_before_commit = databuf_lo_before_commit, |
18ec7d5c SW |
811 | .lo_after_commit = databuf_lo_after_commit, |
812 | .lo_scan_elements = databuf_lo_scan_elements, | |
813 | .lo_after_scan = databuf_lo_after_scan, | |
ea67eedb | 814 | .lo_name = "databuf", |
b3b94faa DT |
815 | }; |
816 | ||
b09e593d | 817 | const struct gfs2_log_operations *gfs2_log_ops[] = { |
b3b94faa DT |
818 | &gfs2_glock_lops, |
819 | &gfs2_buf_lops, | |
820 | &gfs2_revoke_lops, | |
821 | &gfs2_rg_lops, | |
822 | &gfs2_databuf_lops, | |
ea67eedb | 823 | NULL, |
b3b94faa DT |
824 | }; |
825 |