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" |
2332c443 | 20 | #include "inode.h" |
b3b94faa DT |
21 | #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" | |
5c676f6d | 28 | #include "util.h" |
b3b94faa | 29 | |
9b9107a5 SW |
30 | /** |
31 | * gfs2_pin - Pin a buffer in memory | |
32 | * @sdp: The superblock | |
33 | * @bh: The buffer to be pinned | |
34 | * | |
35 | * The log lock must be held when calling this function | |
36 | */ | |
37 | static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) | |
38 | { | |
39 | struct gfs2_bufdata *bd; | |
40 | ||
41 | gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); | |
42 | ||
43 | clear_buffer_dirty(bh); | |
44 | if (test_set_buffer_pinned(bh)) | |
45 | gfs2_assert_withdraw(sdp, 0); | |
46 | if (!buffer_uptodate(bh)) | |
47 | gfs2_io_error_bh(sdp, bh); | |
48 | bd = bh->b_private; | |
49 | /* If this buffer is in the AIL and it has already been written | |
50 | * to in-place disk block, remove it from the AIL. | |
51 | */ | |
52 | if (bd->bd_ail) | |
53 | list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list); | |
54 | get_bh(bh); | |
55 | } | |
56 | ||
57 | /** | |
58 | * gfs2_unpin - Unpin a buffer | |
59 | * @sdp: the filesystem the buffer belongs to | |
60 | * @bh: The buffer to unpin | |
61 | * @ai: | |
62 | * | |
63 | */ | |
64 | ||
65 | static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, | |
66 | struct gfs2_ail *ai) | |
67 | { | |
68 | struct gfs2_bufdata *bd = bh->b_private; | |
69 | ||
70 | gfs2_assert_withdraw(sdp, buffer_uptodate(bh)); | |
71 | ||
72 | if (!buffer_pinned(bh)) | |
73 | gfs2_assert_withdraw(sdp, 0); | |
74 | ||
75 | lock_buffer(bh); | |
76 | mark_buffer_dirty(bh); | |
77 | clear_buffer_pinned(bh); | |
78 | ||
79 | gfs2_log_lock(sdp); | |
80 | if (bd->bd_ail) { | |
81 | list_del(&bd->bd_ail_st_list); | |
82 | brelse(bh); | |
83 | } else { | |
84 | struct gfs2_glock *gl = bd->bd_gl; | |
85 | list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list); | |
86 | atomic_inc(&gl->gl_ail_count); | |
87 | } | |
88 | bd->bd_ail = ai; | |
89 | list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list); | |
90 | gfs2_log_unlock(sdp); | |
91 | unlock_buffer(bh); | |
92 | } | |
93 | ||
16615be1 SW |
94 | |
95 | static inline struct gfs2_log_descriptor *bh_log_desc(struct buffer_head *bh) | |
96 | { | |
97 | return (struct gfs2_log_descriptor *)bh->b_data; | |
98 | } | |
99 | ||
100 | static inline __be64 *bh_log_ptr(struct buffer_head *bh) | |
101 | { | |
102 | struct gfs2_log_descriptor *ld = bh_log_desc(bh); | |
103 | return (__force __be64 *)(ld + 1); | |
104 | } | |
105 | ||
106 | static inline __be64 *bh_ptr_end(struct buffer_head *bh) | |
107 | { | |
108 | return (__force __be64 *)(bh->b_data + bh->b_size); | |
109 | } | |
110 | ||
111 | ||
112 | static struct buffer_head *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type) | |
113 | { | |
114 | struct buffer_head *bh = gfs2_log_get_buf(sdp); | |
115 | struct gfs2_log_descriptor *ld = bh_log_desc(bh); | |
116 | ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC); | |
117 | ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD); | |
118 | ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD); | |
119 | ld->ld_type = cpu_to_be32(ld_type); | |
120 | ld->ld_length = 0; | |
121 | ld->ld_data1 = 0; | |
122 | ld->ld_data2 = 0; | |
123 | memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved)); | |
124 | return bh; | |
125 | } | |
126 | ||
9b9107a5 | 127 | static void __glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) |
b3b94faa DT |
128 | { |
129 | struct gfs2_glock *gl; | |
5c676f6d | 130 | struct gfs2_trans *tr = current->journal_info; |
b3b94faa | 131 | |
5c676f6d | 132 | tr->tr_touched = 1; |
b3b94faa | 133 | |
b3b94faa DT |
134 | gl = container_of(le, struct gfs2_glock, gl_le); |
135 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl))) | |
136 | return; | |
b3b94faa | 137 | |
9b9107a5 | 138 | if (!list_empty(&le->le_list)) |
68835625 | 139 | return; |
9b9107a5 | 140 | |
68835625 BM |
141 | gfs2_glock_hold(gl); |
142 | set_bit(GLF_DIRTY, &gl->gl_flags); | |
b3b94faa DT |
143 | sdp->sd_log_num_gl++; |
144 | list_add(&le->le_list, &sdp->sd_log_le_gl); | |
9b9107a5 SW |
145 | } |
146 | ||
147 | static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
148 | { | |
149 | gfs2_log_lock(sdp); | |
150 | __glock_lo_add(sdp, le); | |
b3b94faa DT |
151 | gfs2_log_unlock(sdp); |
152 | } | |
153 | ||
154 | static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) | |
155 | { | |
156 | struct list_head *head = &sdp->sd_log_le_gl; | |
157 | struct gfs2_glock *gl; | |
158 | ||
159 | while (!list_empty(head)) { | |
160 | gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list); | |
161 | list_del_init(&gl->gl_le.le_list); | |
162 | sdp->sd_log_num_gl--; | |
163 | ||
164 | gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)); | |
165 | gfs2_glock_put(gl); | |
166 | } | |
167 | gfs2_assert_warn(sdp, !sdp->sd_log_num_gl); | |
168 | } | |
169 | ||
170 | static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
171 | { | |
172 | struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le); | |
173 | struct gfs2_trans *tr; | |
174 | ||
9b9107a5 | 175 | lock_buffer(bd->bd_bh); |
8bd95727 | 176 | gfs2_log_lock(sdp); |
9b9107a5 SW |
177 | if (!list_empty(&bd->bd_list_tr)) |
178 | goto out; | |
5c676f6d | 179 | tr = current->journal_info; |
b3b94faa DT |
180 | tr->tr_touched = 1; |
181 | tr->tr_num_buf++; | |
182 | list_add(&bd->bd_list_tr, &tr->tr_list_buf); | |
b3b94faa | 183 | if (!list_empty(&le->le_list)) |
9b9107a5 SW |
184 | goto out; |
185 | __glock_lo_add(sdp, &bd->bd_gl->gl_le); | |
b3b94faa | 186 | gfs2_meta_check(sdp, bd->bd_bh); |
a98ab220 | 187 | gfs2_pin(sdp, bd->bd_bh); |
b3b94faa DT |
188 | sdp->sd_log_num_buf++; |
189 | list_add(&le->le_list, &sdp->sd_log_le_buf); | |
b3b94faa | 190 | tr->tr_num_buf_new++; |
9b9107a5 SW |
191 | out: |
192 | gfs2_log_unlock(sdp); | |
193 | unlock_buffer(bd->bd_bh); | |
b3b94faa DT |
194 | } |
195 | ||
196 | static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) | |
197 | { | |
198 | struct list_head *head = &tr->tr_list_buf; | |
199 | struct gfs2_bufdata *bd; | |
200 | ||
8bd95727 | 201 | gfs2_log_lock(sdp); |
b3b94faa DT |
202 | while (!list_empty(head)) { |
203 | bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr); | |
204 | list_del_init(&bd->bd_list_tr); | |
205 | tr->tr_num_buf--; | |
206 | } | |
8bd95727 | 207 | gfs2_log_unlock(sdp); |
b3b94faa DT |
208 | gfs2_assert_warn(sdp, !tr->tr_num_buf); |
209 | } | |
210 | ||
211 | static void buf_lo_before_commit(struct gfs2_sbd *sdp) | |
212 | { | |
213 | struct buffer_head *bh; | |
214 | struct gfs2_log_descriptor *ld; | |
215 | struct gfs2_bufdata *bd1 = NULL, *bd2; | |
905d2aef | 216 | unsigned int total; |
b3b94faa DT |
217 | unsigned int limit; |
218 | unsigned int num; | |
219 | unsigned n; | |
220 | __be64 *ptr; | |
221 | ||
2332c443 | 222 | limit = buf_limit(sdp); |
b3b94faa DT |
223 | /* for 4k blocks, limit = 503 */ |
224 | ||
905d2aef BP |
225 | gfs2_log_lock(sdp); |
226 | total = sdp->sd_log_num_buf; | |
b3b94faa DT |
227 | bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list); |
228 | while(total) { | |
229 | num = total; | |
230 | if (total > limit) | |
231 | num = limit; | |
905d2aef | 232 | gfs2_log_unlock(sdp); |
16615be1 | 233 | bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_METADATA); |
905d2aef | 234 | gfs2_log_lock(sdp); |
16615be1 SW |
235 | ld = bh_log_desc(bh); |
236 | ptr = bh_log_ptr(bh); | |
b3b94faa DT |
237 | ld->ld_length = cpu_to_be32(num + 1); |
238 | ld->ld_data1 = cpu_to_be32(num); | |
b3b94faa DT |
239 | |
240 | n = 0; | |
568f4c96 SW |
241 | list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf, |
242 | bd_le.le_list) { | |
b3b94faa DT |
243 | *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr); |
244 | if (++n >= num) | |
245 | break; | |
246 | } | |
247 | ||
905d2aef | 248 | gfs2_log_unlock(sdp); |
16615be1 | 249 | submit_bh(WRITE, bh); |
905d2aef | 250 | gfs2_log_lock(sdp); |
b3b94faa DT |
251 | |
252 | n = 0; | |
568f4c96 SW |
253 | list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf, |
254 | bd_le.le_list) { | |
16615be1 | 255 | get_bh(bd2->bd_bh); |
905d2aef | 256 | gfs2_log_unlock(sdp); |
16615be1 | 257 | lock_buffer(bd2->bd_bh); |
b3b94faa | 258 | bh = gfs2_log_fake_buf(sdp, bd2->bd_bh); |
16615be1 | 259 | submit_bh(WRITE, bh); |
905d2aef | 260 | gfs2_log_lock(sdp); |
b3b94faa DT |
261 | if (++n >= num) |
262 | break; | |
263 | } | |
264 | ||
905d2aef | 265 | BUG_ON(total < num); |
b3b94faa DT |
266 | total -= num; |
267 | } | |
905d2aef | 268 | gfs2_log_unlock(sdp); |
b3b94faa DT |
269 | } |
270 | ||
271 | static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) | |
272 | { | |
273 | struct list_head *head = &sdp->sd_log_le_buf; | |
274 | struct gfs2_bufdata *bd; | |
275 | ||
276 | while (!list_empty(head)) { | |
277 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); | |
278 | list_del_init(&bd->bd_le.le_list); | |
279 | sdp->sd_log_num_buf--; | |
280 | ||
a98ab220 | 281 | gfs2_unpin(sdp, bd->bd_bh, ai); |
b3b94faa DT |
282 | } |
283 | gfs2_assert_warn(sdp, !sdp->sd_log_num_buf); | |
284 | } | |
285 | ||
286 | static void buf_lo_before_scan(struct gfs2_jdesc *jd, | |
55167622 | 287 | struct gfs2_log_header_host *head, int pass) |
b3b94faa | 288 | { |
feaa7bba | 289 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
290 | |
291 | if (pass != 0) | |
292 | return; | |
293 | ||
294 | sdp->sd_found_blocks = 0; | |
295 | sdp->sd_replayed_blocks = 0; | |
296 | } | |
297 | ||
298 | static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, | |
299 | struct gfs2_log_descriptor *ld, __be64 *ptr, | |
300 | int pass) | |
301 | { | |
feaa7bba SW |
302 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
303 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
5c676f6d | 304 | struct gfs2_glock *gl = ip->i_gl; |
b3b94faa DT |
305 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
306 | struct buffer_head *bh_log, *bh_ip; | |
cd915493 | 307 | u64 blkno; |
b3b94faa DT |
308 | int error = 0; |
309 | ||
310 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA) | |
311 | return 0; | |
312 | ||
313 | gfs2_replay_incr_blk(sdp, &start); | |
314 | ||
315 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { | |
316 | blkno = be64_to_cpu(*ptr++); | |
317 | ||
318 | sdp->sd_found_blocks++; | |
319 | ||
320 | if (gfs2_revoke_check(sdp, blkno, start)) | |
321 | continue; | |
322 | ||
323 | error = gfs2_replay_read_block(jd, start, &bh_log); | |
82ffa516 SW |
324 | if (error) |
325 | return error; | |
b3b94faa DT |
326 | |
327 | bh_ip = gfs2_meta_new(gl, blkno); | |
328 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); | |
329 | ||
330 | if (gfs2_meta_check(sdp, bh_ip)) | |
331 | error = -EIO; | |
332 | else | |
333 | mark_buffer_dirty(bh_ip); | |
334 | ||
335 | brelse(bh_log); | |
336 | brelse(bh_ip); | |
337 | ||
338 | if (error) | |
339 | break; | |
340 | ||
341 | sdp->sd_replayed_blocks++; | |
342 | } | |
343 | ||
344 | return error; | |
345 | } | |
346 | ||
347 | static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
348 | { | |
feaa7bba SW |
349 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
350 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
b3b94faa DT |
351 | |
352 | if (error) { | |
7276b3b0 | 353 | gfs2_meta_sync(ip->i_gl); |
b3b94faa DT |
354 | return; |
355 | } | |
356 | if (pass != 1) | |
357 | return; | |
358 | ||
7276b3b0 | 359 | gfs2_meta_sync(ip->i_gl); |
b3b94faa DT |
360 | |
361 | fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n", | |
362 | jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks); | |
363 | } | |
364 | ||
365 | static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
366 | { | |
367 | struct gfs2_trans *tr; | |
368 | ||
5c676f6d | 369 | tr = current->journal_info; |
b3b94faa DT |
370 | tr->tr_touched = 1; |
371 | tr->tr_num_revoke++; | |
b3b94faa DT |
372 | sdp->sd_log_num_revoke++; |
373 | list_add(&le->le_list, &sdp->sd_log_le_revoke); | |
b3b94faa DT |
374 | } |
375 | ||
376 | static void revoke_lo_before_commit(struct gfs2_sbd *sdp) | |
377 | { | |
378 | struct gfs2_log_descriptor *ld; | |
379 | struct gfs2_meta_header *mh; | |
380 | struct buffer_head *bh; | |
381 | unsigned int offset; | |
382 | struct list_head *head = &sdp->sd_log_le_revoke; | |
82e86087 | 383 | struct gfs2_bufdata *bd; |
b3b94faa DT |
384 | |
385 | if (!sdp->sd_log_num_revoke) | |
386 | return; | |
387 | ||
16615be1 SW |
388 | bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE); |
389 | ld = bh_log_desc(bh); | |
568f4c96 | 390 | ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, |
cd915493 | 391 | sizeof(u64))); |
b3b94faa | 392 | ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke); |
b3b94faa DT |
393 | offset = sizeof(struct gfs2_log_descriptor); |
394 | ||
395 | while (!list_empty(head)) { | |
82e86087 SW |
396 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); |
397 | list_del_init(&bd->bd_le.le_list); | |
b3b94faa DT |
398 | sdp->sd_log_num_revoke--; |
399 | ||
cd915493 | 400 | if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) { |
16615be1 | 401 | submit_bh(WRITE, bh); |
b3b94faa DT |
402 | |
403 | bh = gfs2_log_get_buf(sdp); | |
404 | mh = (struct gfs2_meta_header *)bh->b_data; | |
405 | mh->mh_magic = cpu_to_be32(GFS2_MAGIC); | |
e3167ded SW |
406 | mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB); |
407 | mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB); | |
b3b94faa DT |
408 | offset = sizeof(struct gfs2_meta_header); |
409 | } | |
410 | ||
82e86087 | 411 | *(__be64 *)(bh->b_data + offset) = cpu_to_be64(bd->bd_blkno); |
0820ab51 | 412 | kmem_cache_free(gfs2_bufdata_cachep, bd); |
b3b94faa | 413 | |
cd915493 | 414 | offset += sizeof(u64); |
b3b94faa DT |
415 | } |
416 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); | |
417 | ||
16615be1 | 418 | submit_bh(WRITE, bh); |
b3b94faa DT |
419 | } |
420 | ||
421 | static void revoke_lo_before_scan(struct gfs2_jdesc *jd, | |
55167622 | 422 | struct gfs2_log_header_host *head, int pass) |
b3b94faa | 423 | { |
feaa7bba | 424 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
425 | |
426 | if (pass != 0) | |
427 | return; | |
428 | ||
429 | sdp->sd_found_revokes = 0; | |
430 | sdp->sd_replay_tail = head->lh_tail; | |
431 | } | |
432 | ||
433 | static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, | |
434 | struct gfs2_log_descriptor *ld, __be64 *ptr, | |
435 | int pass) | |
436 | { | |
feaa7bba | 437 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
438 | unsigned int blks = be32_to_cpu(ld->ld_length); |
439 | unsigned int revokes = be32_to_cpu(ld->ld_data1); | |
440 | struct buffer_head *bh; | |
441 | unsigned int offset; | |
cd915493 | 442 | u64 blkno; |
b3b94faa DT |
443 | int first = 1; |
444 | int error; | |
445 | ||
446 | if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE) | |
447 | return 0; | |
448 | ||
449 | offset = sizeof(struct gfs2_log_descriptor); | |
450 | ||
451 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { | |
452 | error = gfs2_replay_read_block(jd, start, &bh); | |
453 | if (error) | |
454 | return error; | |
455 | ||
456 | if (!first) | |
457 | gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB); | |
458 | ||
cd915493 | 459 | while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) { |
b3b94faa DT |
460 | blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset)); |
461 | ||
462 | error = gfs2_revoke_add(sdp, blkno, start); | |
463 | if (error < 0) | |
464 | return error; | |
465 | else if (error) | |
466 | sdp->sd_found_revokes++; | |
467 | ||
468 | if (!--revokes) | |
469 | break; | |
cd915493 | 470 | offset += sizeof(u64); |
b3b94faa DT |
471 | } |
472 | ||
473 | brelse(bh); | |
474 | offset = sizeof(struct gfs2_meta_header); | |
475 | first = 0; | |
476 | } | |
477 | ||
478 | return 0; | |
479 | } | |
480 | ||
481 | static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
482 | { | |
feaa7bba | 483 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
484 | |
485 | if (error) { | |
486 | gfs2_revoke_clean(sdp); | |
487 | return; | |
488 | } | |
489 | if (pass != 1) | |
490 | return; | |
491 | ||
492 | fs_info(sdp, "jid=%u: Found %u revoke tags\n", | |
493 | jd->jd_jid, sdp->sd_found_revokes); | |
494 | ||
495 | gfs2_revoke_clean(sdp); | |
496 | } | |
497 | ||
498 | static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) | |
499 | { | |
500 | struct gfs2_rgrpd *rgd; | |
5c676f6d | 501 | struct gfs2_trans *tr = current->journal_info; |
b3b94faa | 502 | |
5c676f6d | 503 | tr->tr_touched = 1; |
b3b94faa | 504 | |
b3b94faa | 505 | rgd = container_of(le, struct gfs2_rgrpd, rd_le); |
b3b94faa DT |
506 | |
507 | gfs2_log_lock(sdp); | |
68835625 BM |
508 | if (!list_empty(&le->le_list)){ |
509 | gfs2_log_unlock(sdp); | |
510 | return; | |
511 | } | |
512 | gfs2_rgrp_bh_hold(rgd); | |
b3b94faa DT |
513 | sdp->sd_log_num_rg++; |
514 | list_add(&le->le_list, &sdp->sd_log_le_rg); | |
907b9bce | 515 | gfs2_log_unlock(sdp); |
b3b94faa DT |
516 | } |
517 | ||
518 | static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) | |
519 | { | |
520 | struct list_head *head = &sdp->sd_log_le_rg; | |
521 | struct gfs2_rgrpd *rgd; | |
522 | ||
523 | while (!list_empty(head)) { | |
524 | rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list); | |
525 | list_del_init(&rgd->rd_le.le_list); | |
526 | sdp->sd_log_num_rg--; | |
527 | ||
528 | gfs2_rgrp_repolish_clones(rgd); | |
529 | gfs2_rgrp_bh_put(rgd); | |
530 | } | |
531 | gfs2_assert_warn(sdp, !sdp->sd_log_num_rg); | |
532 | } | |
533 | ||
18ec7d5c SW |
534 | /** |
535 | * databuf_lo_add - Add a databuf to the transaction. | |
536 | * | |
537 | * This is used in two distinct cases: | |
538 | * i) In ordered write mode | |
539 | * We put the data buffer on a list so that we can ensure that its | |
540 | * synced to disk at the right time | |
541 | * ii) In journaled data mode | |
542 | * We need to journal the data block in the same way as metadata in | |
543 | * the functions above. The difference is that here we have a tag | |
544 | * which is two __be64's being the block number (as per meta data) | |
545 | * and a flag which says whether the data block needs escaping or | |
546 | * not. This means we need a new log entry for each 251 or so data | |
547 | * blocks, which isn't an enormous overhead but twice as much as | |
548 | * for normal metadata blocks. | |
549 | */ | |
b3b94faa DT |
550 | static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) |
551 | { | |
18ec7d5c | 552 | struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le); |
5c676f6d | 553 | struct gfs2_trans *tr = current->journal_info; |
18ec7d5c | 554 | struct address_space *mapping = bd->bd_bh->b_page->mapping; |
feaa7bba | 555 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
b3b94faa | 556 | |
9b9107a5 | 557 | lock_buffer(bd->bd_bh); |
8bd95727 | 558 | gfs2_log_lock(sdp); |
9ff8ec32 SW |
559 | if (tr) { |
560 | if (!list_empty(&bd->bd_list_tr)) | |
561 | goto out; | |
562 | tr->tr_touched = 1; | |
563 | if (gfs2_is_jdata(ip)) { | |
564 | tr->tr_num_buf++; | |
565 | list_add(&bd->bd_list_tr, &tr->tr_list_buf); | |
566 | } | |
773ed1a0 | 567 | } |
2332c443 | 568 | if (!list_empty(&le->le_list)) |
9b9107a5 | 569 | goto out; |
2332c443 | 570 | |
9ff8ec32 SW |
571 | if (tr) |
572 | __glock_lo_add(sdp, &bd->bd_gl->gl_le); | |
2332c443 | 573 | if (gfs2_is_jdata(ip)) { |
2332c443 RP |
574 | gfs2_pin(sdp, bd->bd_bh); |
575 | tr->tr_num_databuf_new++; | |
d7b616e2 SW |
576 | sdp->sd_log_num_databuf++; |
577 | list_add(&le->le_list, &sdp->sd_log_le_databuf); | |
578 | } else { | |
579 | list_add(&le->le_list, &sdp->sd_log_le_ordered); | |
9b9107a5 | 580 | } |
9b9107a5 | 581 | out: |
b3b94faa | 582 | gfs2_log_unlock(sdp); |
9b9107a5 | 583 | unlock_buffer(bd->bd_bh); |
b3b94faa DT |
584 | } |
585 | ||
16615be1 | 586 | static void gfs2_check_magic(struct buffer_head *bh) |
18ec7d5c | 587 | { |
18ec7d5c SW |
588 | void *kaddr; |
589 | __be32 *ptr; | |
18ec7d5c | 590 | |
16615be1 SW |
591 | clear_buffer_escaped(bh); |
592 | kaddr = kmap_atomic(bh->b_page, KM_USER0); | |
18ec7d5c SW |
593 | ptr = kaddr + bh_offset(bh); |
594 | if (*ptr == cpu_to_be32(GFS2_MAGIC)) | |
16615be1 | 595 | set_buffer_escaped(bh); |
c312c4fd | 596 | kunmap_atomic(kaddr, KM_USER0); |
18ec7d5c SW |
597 | } |
598 | ||
16615be1 SW |
599 | static void gfs2_write_blocks(struct gfs2_sbd *sdp, struct buffer_head *bh, |
600 | struct list_head *list, struct list_head *done, | |
601 | unsigned int n) | |
b3b94faa | 602 | { |
16615be1 | 603 | struct buffer_head *bh1; |
18ec7d5c | 604 | struct gfs2_log_descriptor *ld; |
16615be1 SW |
605 | struct gfs2_bufdata *bd; |
606 | __be64 *ptr; | |
d7b616e2 | 607 | |
16615be1 SW |
608 | if (!bh) |
609 | return; | |
b3b94faa | 610 | |
16615be1 SW |
611 | ld = bh_log_desc(bh); |
612 | ld->ld_length = cpu_to_be32(n + 1); | |
613 | ld->ld_data1 = cpu_to_be32(n); | |
b3b94faa | 614 | |
16615be1 SW |
615 | ptr = bh_log_ptr(bh); |
616 | ||
617 | get_bh(bh); | |
618 | submit_bh(WRITE, bh); | |
f55ab26a | 619 | gfs2_log_lock(sdp); |
16615be1 SW |
620 | while(!list_empty(list)) { |
621 | bd = list_entry(list->next, struct gfs2_bufdata, bd_le.le_list); | |
622 | list_move_tail(&bd->bd_le.le_list, done); | |
623 | get_bh(bd->bd_bh); | |
624 | while (be64_to_cpu(*ptr) != bd->bd_bh->b_blocknr) { | |
625 | gfs2_log_incr_head(sdp); | |
626 | ptr += 2; | |
18ec7d5c | 627 | } |
f55ab26a | 628 | gfs2_log_unlock(sdp); |
16615be1 SW |
629 | lock_buffer(bd->bd_bh); |
630 | if (buffer_escaped(bd->bd_bh)) { | |
631 | void *kaddr; | |
632 | bh1 = gfs2_log_get_buf(sdp); | |
633 | kaddr = kmap_atomic(bd->bd_bh->b_page, KM_USER0); | |
634 | memcpy(bh1->b_data, kaddr + bh_offset(bd->bd_bh), | |
635 | bh1->b_size); | |
636 | kunmap_atomic(kaddr, KM_USER0); | |
637 | *(__be32 *)bh1->b_data = 0; | |
638 | clear_buffer_escaped(bd->bd_bh); | |
639 | unlock_buffer(bd->bd_bh); | |
640 | brelse(bd->bd_bh); | |
641 | } else { | |
642 | bh1 = gfs2_log_fake_buf(sdp, bd->bd_bh); | |
18ec7d5c | 643 | } |
16615be1 | 644 | submit_bh(WRITE, bh1); |
f55ab26a | 645 | gfs2_log_lock(sdp); |
16615be1 SW |
646 | ptr += 2; |
647 | } | |
648 | gfs2_log_unlock(sdp); | |
649 | brelse(bh); | |
650 | } | |
651 | ||
652 | /** | |
653 | * databuf_lo_before_commit - Scan the data buffers, writing as we go | |
654 | * | |
655 | */ | |
656 | ||
657 | static void databuf_lo_before_commit(struct gfs2_sbd *sdp) | |
658 | { | |
659 | struct gfs2_bufdata *bd = NULL; | |
660 | struct buffer_head *bh = NULL; | |
661 | unsigned int n = 0; | |
662 | __be64 *ptr = NULL, *end = NULL; | |
663 | LIST_HEAD(processed); | |
664 | LIST_HEAD(in_progress); | |
665 | ||
666 | gfs2_log_lock(sdp); | |
667 | while (!list_empty(&sdp->sd_log_le_databuf)) { | |
668 | if (ptr == end) { | |
f55ab26a | 669 | gfs2_log_unlock(sdp); |
16615be1 SW |
670 | gfs2_write_blocks(sdp, bh, &in_progress, &processed, n); |
671 | n = 0; | |
672 | bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_JDATA); | |
673 | ptr = bh_log_ptr(bh); | |
674 | end = bh_ptr_end(bh) - 1; | |
f55ab26a | 675 | gfs2_log_lock(sdp); |
16615be1 | 676 | continue; |
18ec7d5c | 677 | } |
16615be1 SW |
678 | bd = list_entry(sdp->sd_log_le_databuf.next, struct gfs2_bufdata, bd_le.le_list); |
679 | list_move_tail(&bd->bd_le.le_list, &in_progress); | |
680 | gfs2_check_magic(bd->bd_bh); | |
681 | *ptr++ = cpu_to_be64(bd->bd_bh->b_blocknr); | |
682 | *ptr++ = cpu_to_be64(buffer_escaped(bh) ? 1 : 0); | |
683 | n++; | |
b3b94faa | 684 | } |
f55ab26a | 685 | gfs2_log_unlock(sdp); |
16615be1 SW |
686 | gfs2_write_blocks(sdp, bh, &in_progress, &processed, n); |
687 | gfs2_log_lock(sdp); | |
688 | list_splice(&processed, &sdp->sd_log_le_databuf); | |
689 | gfs2_log_unlock(sdp); | |
18ec7d5c SW |
690 | } |
691 | ||
692 | static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, | |
693 | struct gfs2_log_descriptor *ld, | |
694 | __be64 *ptr, int pass) | |
695 | { | |
feaa7bba SW |
696 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
697 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
5c676f6d | 698 | struct gfs2_glock *gl = ip->i_gl; |
18ec7d5c SW |
699 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
700 | struct buffer_head *bh_log, *bh_ip; | |
cd915493 SW |
701 | u64 blkno; |
702 | u64 esc; | |
18ec7d5c SW |
703 | int error = 0; |
704 | ||
705 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA) | |
706 | return 0; | |
707 | ||
708 | gfs2_replay_incr_blk(sdp, &start); | |
709 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { | |
710 | blkno = be64_to_cpu(*ptr++); | |
711 | esc = be64_to_cpu(*ptr++); | |
712 | ||
713 | sdp->sd_found_blocks++; | |
714 | ||
715 | if (gfs2_revoke_check(sdp, blkno, start)) | |
716 | continue; | |
717 | ||
718 | error = gfs2_replay_read_block(jd, start, &bh_log); | |
719 | if (error) | |
720 | return error; | |
721 | ||
722 | bh_ip = gfs2_meta_new(gl, blkno); | |
723 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); | |
724 | ||
725 | /* Unescape */ | |
726 | if (esc) { | |
727 | __be32 *eptr = (__be32 *)bh_ip->b_data; | |
728 | *eptr = cpu_to_be32(GFS2_MAGIC); | |
729 | } | |
730 | mark_buffer_dirty(bh_ip); | |
731 | ||
732 | brelse(bh_log); | |
733 | brelse(bh_ip); | |
734 | if (error) | |
735 | break; | |
736 | ||
737 | sdp->sd_replayed_blocks++; | |
738 | } | |
739 | ||
740 | return error; | |
741 | } | |
742 | ||
743 | /* FIXME: sort out accounting for log blocks etc. */ | |
744 | ||
745 | static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
746 | { | |
feaa7bba SW |
747 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
748 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
18ec7d5c SW |
749 | |
750 | if (error) { | |
7276b3b0 | 751 | gfs2_meta_sync(ip->i_gl); |
18ec7d5c SW |
752 | return; |
753 | } | |
754 | if (pass != 1) | |
755 | return; | |
756 | ||
757 | /* data sync? */ | |
7276b3b0 | 758 | gfs2_meta_sync(ip->i_gl); |
18ec7d5c SW |
759 | |
760 | fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n", | |
761 | jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks); | |
762 | } | |
763 | ||
764 | static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) | |
765 | { | |
766 | struct list_head *head = &sdp->sd_log_le_databuf; | |
767 | struct gfs2_bufdata *bd; | |
768 | ||
769 | while (!list_empty(head)) { | |
770 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); | |
b8e1aabf | 771 | list_del_init(&bd->bd_le.le_list); |
18ec7d5c | 772 | sdp->sd_log_num_databuf--; |
18ec7d5c | 773 | gfs2_unpin(sdp, bd->bd_bh, ai); |
18ec7d5c | 774 | } |
b3b94faa DT |
775 | gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf); |
776 | } | |
777 | ||
18ec7d5c | 778 | |
b09e593d | 779 | const struct gfs2_log_operations gfs2_glock_lops = { |
b3b94faa DT |
780 | .lo_add = glock_lo_add, |
781 | .lo_after_commit = glock_lo_after_commit, | |
ea67eedb | 782 | .lo_name = "glock", |
b3b94faa DT |
783 | }; |
784 | ||
b09e593d | 785 | const struct gfs2_log_operations gfs2_buf_lops = { |
b3b94faa DT |
786 | .lo_add = buf_lo_add, |
787 | .lo_incore_commit = buf_lo_incore_commit, | |
788 | .lo_before_commit = buf_lo_before_commit, | |
789 | .lo_after_commit = buf_lo_after_commit, | |
790 | .lo_before_scan = buf_lo_before_scan, | |
791 | .lo_scan_elements = buf_lo_scan_elements, | |
792 | .lo_after_scan = buf_lo_after_scan, | |
ea67eedb | 793 | .lo_name = "buf", |
b3b94faa DT |
794 | }; |
795 | ||
b09e593d | 796 | const struct gfs2_log_operations gfs2_revoke_lops = { |
b3b94faa DT |
797 | .lo_add = revoke_lo_add, |
798 | .lo_before_commit = revoke_lo_before_commit, | |
799 | .lo_before_scan = revoke_lo_before_scan, | |
800 | .lo_scan_elements = revoke_lo_scan_elements, | |
801 | .lo_after_scan = revoke_lo_after_scan, | |
ea67eedb | 802 | .lo_name = "revoke", |
b3b94faa DT |
803 | }; |
804 | ||
b09e593d | 805 | const struct gfs2_log_operations gfs2_rg_lops = { |
b3b94faa DT |
806 | .lo_add = rg_lo_add, |
807 | .lo_after_commit = rg_lo_after_commit, | |
ea67eedb | 808 | .lo_name = "rg", |
b3b94faa DT |
809 | }; |
810 | ||
b09e593d | 811 | const struct gfs2_log_operations gfs2_databuf_lops = { |
b3b94faa | 812 | .lo_add = databuf_lo_add, |
18ec7d5c | 813 | .lo_incore_commit = buf_lo_incore_commit, |
b3b94faa | 814 | .lo_before_commit = databuf_lo_before_commit, |
18ec7d5c SW |
815 | .lo_after_commit = databuf_lo_after_commit, |
816 | .lo_scan_elements = databuf_lo_scan_elements, | |
817 | .lo_after_scan = databuf_lo_after_scan, | |
ea67eedb | 818 | .lo_name = "databuf", |
b3b94faa DT |
819 | }; |
820 | ||
b09e593d | 821 | const struct gfs2_log_operations *gfs2_log_ops[] = { |
b3b94faa | 822 | &gfs2_glock_lops, |
16615be1 | 823 | &gfs2_databuf_lops, |
b3b94faa | 824 | &gfs2_buf_lops, |
b3b94faa | 825 | &gfs2_rg_lops, |
16615be1 | 826 | &gfs2_revoke_lops, |
ea67eedb | 827 | NULL, |
b3b94faa DT |
828 | }; |
829 |