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