Blame view

fs/gfs2/lops.c 21.3 KB
b3b94faa5   David Teigland   [GFS2] The core o...
1
2
  /*
   * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3a8a9a103   Steven Whitehouse   [GFS2] Update cop...
3
   * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
b3b94faa5   David Teigland   [GFS2] The core o...
4
5
6
   *
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa09   Steven Whitehouse   [GFS2] Update cop...
7
   * of the GNU General Public License version 2.
b3b94faa5   David Teigland   [GFS2] The core o...
8
9
10
11
12
13
14
   */
  
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/completion.h>
  #include <linux/buffer_head.h>
75ca61c10   Steven Whitehouse   GFS2: Remove a __...
15
  #include <linux/mempool.h>
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
16
  #include <linux/gfs2_ondisk.h>
c969f58ca   Steven Whitehouse   GFS2: Update the ...
17
18
  #include <linux/bio.h>
  #include <linux/fs.h>
7f63257da   Benjamin Marzinski   GFS2: Sort buffer...
19
  #include <linux/list_sort.h>
b3b94faa5   David Teigland   [GFS2] The core o...
20
21
  
  #include "gfs2.h"
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
22
  #include "incore.h"
2332c4435   Robert Peterson   [GFS2] assertion ...
23
  #include "inode.h"
b3b94faa5   David Teigland   [GFS2] The core o...
24
25
26
27
28
29
30
  #include "glock.h"
  #include "log.h"
  #include "lops.h"
  #include "meta_io.h"
  #include "recovery.h"
  #include "rgrp.h"
  #include "trans.h"
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
31
  #include "util.h"
63997775b   Steven Whitehouse   GFS2: Add tracepo...
32
  #include "trace_gfs2.h"
b3b94faa5   David Teigland   [GFS2] The core o...
33

9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
34
35
36
37
38
39
40
  /**
   * gfs2_pin - Pin a buffer in memory
   * @sdp: The superblock
   * @bh: The buffer to be pinned
   *
   * The log lock must be held when calling this function
   */
767f433f3   Steven Whitehouse   GFS2: Copy gfs2_t...
41
  void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
42
43
  {
  	struct gfs2_bufdata *bd;
29687a2ac   Steven Whitehouse   GFS2: Alter point...
44
  	BUG_ON(!current->journal_info);
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
45
46
47
48
49
50
51
52
53
54
  
  	clear_buffer_dirty(bh);
  	if (test_set_buffer_pinned(bh))
  		gfs2_assert_withdraw(sdp, 0);
  	if (!buffer_uptodate(bh))
  		gfs2_io_error_bh(sdp, bh);
  	bd = bh->b_private;
  	/* If this buffer is in the AIL and it has already been written
  	 * to in-place disk block, remove it from the AIL.
  	 */
c618e87a5   Steven Whitehouse   GFS2: Update to A...
55
  	spin_lock(&sdp->sd_ail_lock);
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
56
57
  	if (bd->bd_tr)
  		list_move(&bd->bd_ail_st_list, &bd->bd_tr->tr_ail2_list);
c618e87a5   Steven Whitehouse   GFS2: Update to A...
58
  	spin_unlock(&sdp->sd_ail_lock);
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
59
  	get_bh(bh);
5e687eac1   Benjamin Marzinski   GFS2: Various gfs...
60
  	atomic_inc(&sdp->sd_log_pinned);
63997775b   Steven Whitehouse   GFS2: Add tracepo...
61
  	trace_gfs2_pin(bd, 1);
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
62
  }
7c9ca6211   Bob Peterson   GFS2: Use rbtree ...
63
64
65
66
67
68
69
70
  static bool buffer_is_rgrp(const struct gfs2_bufdata *bd)
  {
  	return bd->bd_gl->gl_name.ln_type == LM_TYPE_RGRP;
  }
  
  static void maybe_release_space(struct gfs2_bufdata *bd)
  {
  	struct gfs2_glock *gl = bd->bd_gl;
15562c439   Bob Peterson   GFS2: Move glock ...
71
  	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
6f6597baa   Andreas Gruenbacher   gfs2: Protect gl-...
72
  	struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
7c9ca6211   Bob Peterson   GFS2: Use rbtree ...
73
74
  	unsigned int index = bd->bd_bh->b_blocknr - gl->gl_name.ln_number;
  	struct gfs2_bitmap *bi = rgd->rd_bits + index;
5a7c6690c   Fabian Frederick   GFS2: lops.c: rep...
75
  	if (bi->bi_clone == NULL)
7c9ca6211   Bob Peterson   GFS2: Use rbtree ...
76
77
  		return;
  	if (sdp->sd_args.ar_discard)
66fc061bd   Steven Whitehouse   GFS2: FITRIM ioct...
78
  		gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL);
7c9ca6211   Bob Peterson   GFS2: Use rbtree ...
79
80
81
82
  	memcpy(bi->bi_clone + bi->bi_offset,
  	       bd->bd_bh->b_data + bi->bi_offset, bi->bi_len);
  	clear_bit(GBF_FULL, &bi->bi_flags);
  	rgd->rd_free_clone = rgd->rd_free;
5ea5050ce   Bob Peterson   GFS2: Implement a...
83
  	rgd->rd_extfail_pt = rgd->rd_free;
7c9ca6211   Bob Peterson   GFS2: Use rbtree ...
84
  }
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
85
86
87
88
89
  /**
   * gfs2_unpin - Unpin a buffer
   * @sdp: the filesystem the buffer belongs to
   * @bh: The buffer to unpin
   * @ai:
29687a2ac   Steven Whitehouse   GFS2: Alter point...
90
   * @flags: The inode dirty flags
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
91
92
93
94
   *
   */
  
  static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
95
  		       struct gfs2_trans *tr)
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
96
97
  {
  	struct gfs2_bufdata *bd = bh->b_private;
29687a2ac   Steven Whitehouse   GFS2: Alter point...
98
99
  	BUG_ON(!buffer_uptodate(bh));
  	BUG_ON(!buffer_pinned(bh));
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
100
101
102
103
  
  	lock_buffer(bh);
  	mark_buffer_dirty(bh);
  	clear_buffer_pinned(bh);
7c9ca6211   Bob Peterson   GFS2: Use rbtree ...
104
105
  	if (buffer_is_rgrp(bd))
  		maybe_release_space(bd);
d6a079e82   Dave Chinner   GFS2: introduce A...
106
  	spin_lock(&sdp->sd_ail_lock);
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
107
  	if (bd->bd_tr) {
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
108
109
110
111
112
113
114
  		list_del(&bd->bd_ail_st_list);
  		brelse(bh);
  	} else {
  		struct gfs2_glock *gl = bd->bd_gl;
  		list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  		atomic_inc(&gl->gl_ail_count);
  	}
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
115
116
  	bd->bd_tr = tr;
  	list_add(&bd->bd_ail_st_list, &tr->tr_ail1_list);
d6a079e82   Dave Chinner   GFS2: introduce A...
117
  	spin_unlock(&sdp->sd_ail_lock);
29687a2ac   Steven Whitehouse   GFS2: Alter point...
118
  	clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
63997775b   Steven Whitehouse   GFS2: Add tracepo...
119
  	trace_gfs2_pin(bd, 0);
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
120
  	unlock_buffer(bh);
5e687eac1   Benjamin Marzinski   GFS2: Various gfs...
121
  	atomic_dec(&sdp->sd_log_pinned);
9b9107a5a   Steven Whitehouse   [GFS2] Move pin/u...
122
  }
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
123
  static void gfs2_log_incr_head(struct gfs2_sbd *sdp)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
124
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
125
126
  	BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
  	       (sdp->sd_log_flush_head != sdp->sd_log_head));
722f6f62a   Bob Peterson   GFS2: Eliminate v...
127
  	if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks)
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
128
  		sdp->sd_log_flush_head = 0;
16615be18   Steven Whitehouse   [GFS2] Clean up j...
129
  }
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
130
  static u64 gfs2_log_bmap(struct gfs2_sbd *sdp)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
131
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
132
133
134
  	unsigned int lbn = sdp->sd_log_flush_head;
  	struct gfs2_journal_extent *je;
  	u64 block;
b50f227bd   Steven Whitehouse   GFS2: Clean up jo...
135
136
  	list_for_each_entry(je, &sdp->sd_jdesc->extent_list, list) {
  		if ((lbn >= je->lblock) && (lbn < (je->lblock + je->blocks))) {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
137
138
139
140
141
142
143
  			block = je->dblock + lbn - je->lblock;
  			gfs2_log_incr_head(sdp);
  			return block;
  		}
  	}
  
  	return -1;
16615be18   Steven Whitehouse   [GFS2] Clean up j...
144
  }
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
145
146
147
148
149
150
151
152
153
154
155
156
157
  /**
   * gfs2_end_log_write_bh - end log write of pagecache data with buffers
   * @sdp: The superblock
   * @bvec: The bio_vec
   * @error: The i/o status
   *
   * This finds the relavent buffers and unlocks then and sets the
   * error flag according to the status of the i/o request. This is
   * used when the log is writing data which has an in-place version
   * that is pinned in the pagecache.
   */
  
  static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp, struct bio_vec *bvec,
4e4cbee93   Christoph Hellwig   block: switch bio...
158
  				  blk_status_t error)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
159
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
160
161
162
163
164
165
166
167
168
169
  	struct buffer_head *bh, *next;
  	struct page *page = bvec->bv_page;
  	unsigned size;
  
  	bh = page_buffers(page);
  	size = bvec->bv_len;
  	while (bh_offset(bh) < bvec->bv_offset)
  		bh = bh->b_this_page;
  	do {
  		if (error)
87354e5de   Jeff Layton   buffer: set error...
170
  			mark_buffer_write_io_error(bh);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
171
172
173
174
175
176
  		unlock_buffer(bh);
  		next = bh->b_this_page;
  		size -= bh->b_size;
  		brelse(bh);
  		bh = next;
  	} while(bh && size);
16615be18   Steven Whitehouse   [GFS2] Clean up j...
177
  }
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
178
  /**
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
179
180
181
182
183
184
185
   * gfs2_end_log_write - end of i/o to the log
   * @bio: The bio
   * @error: Status of i/o request
   *
   * Each bio_vec contains either data from the pagecache or data
   * relating to the log itself. Here we iterate over the bio_vec
   * array, processing both kinds of data.
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
186
187
   *
   */
4246a0b63   Christoph Hellwig   block: add a bi_e...
188
  static void gfs2_end_log_write(struct bio *bio)
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
189
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
190
191
192
193
  	struct gfs2_sbd *sdp = bio->bi_private;
  	struct bio_vec *bvec;
  	struct page *page;
  	int i;
942b0cddf   Bob Peterson   GFS2: Withdraw fo...
194
195
196
197
198
199
  	if (bio->bi_status) {
  		fs_err(sdp, "Error %d writing to journal, jid=%u
  ",
  		       bio->bi_status, sdp->sd_jdesc->jd_jid);
  		wake_up(&sdp->sd_logd_waitq);
  	}
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
200

e97e548ba   Steven Whitehouse   GFS2: Fix typo in...
201
  	bio_for_each_segment_all(bvec, bio, i) {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
202
203
  		page = bvec->bv_page;
  		if (page_has_buffers(page))
4e4cbee93   Christoph Hellwig   block: switch bio...
204
  			gfs2_end_log_write_bh(sdp, bvec, bio->bi_status);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
205
206
207
  		else
  			mempool_free(page, gfs2_page_pool);
  	}
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
208

e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
209
  	bio_put(bio);
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
210
211
212
213
214
  	if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  		wake_up(&sdp->sd_log_flush_wait);
  }
  
  /**
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
215
216
   * gfs2_log_flush_bio - Submit any pending log bio
   * @sdp: The superblock
e1b1afa6f   Mike Christie   gfs2: use bio op ...
217
   * @op: REQ_OP
ef295ecf0   Christoph Hellwig   block: better op ...
218
   * @op_flags: req_flag_bits
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
219
   *
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
220
221
   * Submit any pending part-built or full bio to the block device. If
   * there is no pending bio, then this is a no-op.
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
222
   */
e1b1afa6f   Mike Christie   gfs2: use bio op ...
223
  void gfs2_log_flush_bio(struct gfs2_sbd *sdp, int op, int op_flags)
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
224
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
225
226
  	if (sdp->sd_log_bio) {
  		atomic_inc(&sdp->sd_log_in_flight);
e1b1afa6f   Mike Christie   gfs2: use bio op ...
227
  		bio_set_op_attrs(sdp->sd_log_bio, op, op_flags);
4e49ea4a3   Mike Christie   block/fs/drivers:...
228
  		submit_bio(sdp->sd_log_bio);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
229
230
231
  		sdp->sd_log_bio = NULL;
  	}
  }
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
232

e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  /**
   * gfs2_log_alloc_bio - Allocate a new bio for log writing
   * @sdp: The superblock
   * @blkno: The next device block number we want to write to
   *
   * This should never be called when there is a cached bio in the
   * super block. When it returns, there will be a cached bio in the
   * super block which will have as many bio_vecs as the device is
   * happy to handle.
   *
   * Returns: Newly allocated bio
   */
  
  static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno)
  {
  	struct super_block *sb = sdp->sd_vfs;
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
249
250
251
  	struct bio *bio;
  
  	BUG_ON(sdp->sd_log_bio);
b54ffb73c   Kent Overstreet   block: remove bio...
252
  	bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
4f024f379   Kent Overstreet   block: Abstract o...
253
  	bio->bi_iter.bi_sector = blkno * (sb->s_blocksize >> 9);
74d46992e   Christoph Hellwig   block: replace bi...
254
  	bio_set_dev(bio, sb->s_bdev);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
255
256
257
258
259
260
  	bio->bi_end_io = gfs2_end_log_write;
  	bio->bi_private = sdp;
  
  	sdp->sd_log_bio = bio;
  
  	return bio;
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
261
262
263
  }
  
  /**
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
264
265
266
267
268
269
270
271
   * gfs2_log_get_bio - Get cached log bio, or allocate a new one
   * @sdp: The superblock
   * @blkno: The device block number we want to write to
   *
   * If there is a cached bio, then if the next block number is sequential
   * with the previous one, return it, otherwise flush the bio to the
   * device. If there is not a cached bio, or we just flushed it, then
   * allocate a new one.
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
272
   *
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
273
   * Returns: The bio to use for log writes
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
274
   */
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
275
  static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno)
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
276
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
277
278
279
280
  	struct bio *bio = sdp->sd_log_bio;
  	u64 nblk;
  
  	if (bio) {
f73a1c7d1   Kent Overstreet   block: Add bio_en...
281
  		nblk = bio_end_sector(bio);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
282
283
284
  		nblk >>= sdp->sd_fsb2bb_shift;
  		if (blkno == nblk)
  			return bio;
e1b1afa6f   Mike Christie   gfs2: use bio op ...
285
  		gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
286
287
288
  	}
  
  	return gfs2_log_alloc_bio(sdp, blkno);
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
289
  }
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
290

47ac5537a   Steven Whitehouse   GFS2: Move two fu...
291
  /**
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
292
   * gfs2_log_write - write to log
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
293
   * @sdp: the filesystem
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
294
295
296
   * @page: the page to write
   * @size: the size of the data to write
   * @offset: the offset within the page 
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
297
   *
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
298
299
300
   * Try and add the page segment to the current bio. If that fails,
   * submit the current bio to the device and create a new one, and
   * then add the page segment to that.
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
301
   */
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
302
303
  static void gfs2_log_write(struct gfs2_sbd *sdp, struct page *page,
  			   unsigned size, unsigned offset)
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
304
  {
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
305
306
307
308
309
310
311
  	u64 blkno = gfs2_log_bmap(sdp);
  	struct bio *bio;
  	int ret;
  
  	bio = gfs2_log_get_bio(sdp, blkno);
  	ret = bio_add_page(bio, page, size, offset);
  	if (ret == 0) {
e1b1afa6f   Mike Christie   gfs2: use bio op ...
312
  		gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
313
314
315
316
317
  		bio = gfs2_log_alloc_bio(sdp, blkno);
  		ret = bio_add_page(bio, page, size, offset);
  		WARN_ON(ret == 0);
  	}
  }
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
318

e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  /**
   * gfs2_log_write_bh - write a buffer's content to the log
   * @sdp: The super block
   * @bh: The buffer pointing to the in-place location
   * 
   * This writes the content of the buffer to the next available location
   * in the log. The buffer will be unlocked once the i/o to the log has
   * completed.
   */
  
  static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh)
  {
  	gfs2_log_write(sdp, bh->b_page, bh->b_size, bh_offset(bh));
  }
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
333

e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
334
335
336
337
338
339
340
341
342
343
  /**
   * gfs2_log_write_page - write one block stored in a page, into the log
   * @sdp: The superblock
   * @page: The struct page
   *
   * This writes the first block-sized part of the page into the log. Note
   * that the page must have been allocated from the gfs2_page_pool mempool
   * and that after this has been called, ownership has been transferred and
   * the page may be freed at any time.
   */
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
344

e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
345
346
347
348
  void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page)
  {
  	struct super_block *sb = sdp->sd_vfs;
  	gfs2_log_write(sdp, page, sb->s_blocksize, 0);
47ac5537a   Steven Whitehouse   GFS2: Move two fu...
349
  }
16615be18   Steven Whitehouse   [GFS2] Clean up j...
350

dad30e903   Steven Whitehouse   GFS2: Remove dupl...
351
352
  static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type,
  				      u32 ld_length, u32 ld_data1)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
353
  {
144a4c2ff   Steven Whitehouse   GFS2: Log code fixes
354
  	struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
355
356
  	struct gfs2_log_descriptor *ld = page_address(page);
  	clear_page(ld);
16615be18   Steven Whitehouse   [GFS2] Clean up j...
357
358
359
360
  	ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  	ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  	ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  	ld->ld_type = cpu_to_be32(ld_type);
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
361
362
  	ld->ld_length = cpu_to_be32(ld_length);
  	ld->ld_data1 = cpu_to_be32(ld_data1);
16615be18   Steven Whitehouse   [GFS2] Clean up j...
363
  	ld->ld_data2 = 0;
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
364
  	return page;
16615be18   Steven Whitehouse   [GFS2] Clean up j...
365
  }
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
366
367
368
369
370
371
372
373
374
375
376
377
  static void gfs2_check_magic(struct buffer_head *bh)
  {
  	void *kaddr;
  	__be32 *ptr;
  
  	clear_buffer_escaped(bh);
  	kaddr = kmap_atomic(bh->b_page);
  	ptr = kaddr + bh_offset(bh);
  	if (*ptr == cpu_to_be32(GFS2_MAGIC))
  		set_buffer_escaped(bh);
  	kunmap_atomic(kaddr);
  }
7f63257da   Benjamin Marzinski   GFS2: Sort buffer...
378
379
380
381
382
383
384
385
386
387
388
389
390
  static int blocknr_cmp(void *priv, struct list_head *a, struct list_head *b)
  {
  	struct gfs2_bufdata *bda, *bdb;
  
  	bda = list_entry(a, struct gfs2_bufdata, bd_list);
  	bdb = list_entry(b, struct gfs2_bufdata, bd_list);
  
  	if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
  		return -1;
  	if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
  		return 1;
  	return 0;
  }
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
391
392
393
  static void gfs2_before_commit(struct gfs2_sbd *sdp, unsigned int limit,
  				unsigned int total, struct list_head *blist,
  				bool is_databuf)
b3b94faa5   David Teigland   [GFS2] The core o...
394
  {
b3b94faa5   David Teigland   [GFS2] The core o...
395
396
  	struct gfs2_log_descriptor *ld;
  	struct gfs2_bufdata *bd1 = NULL, *bd2;
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
397
  	struct page *page;
b3b94faa5   David Teigland   [GFS2] The core o...
398
399
400
  	unsigned int num;
  	unsigned n;
  	__be64 *ptr;
905d2aefa   Bob Peterson   [GFS2] Move some ...
401
  	gfs2_log_lock(sdp);
7f63257da   Benjamin Marzinski   GFS2: Sort buffer...
402
  	list_sort(NULL, blist, blocknr_cmp);
c0752aa7e   Bob Peterson   GFS2: eliminate l...
403
  	bd1 = bd2 = list_prepare_entry(bd1, blist, bd_list);
b3b94faa5   David Teigland   [GFS2] The core o...
404
405
406
407
  	while(total) {
  		num = total;
  		if (total > limit)
  			num = limit;
905d2aefa   Bob Peterson   [GFS2] Move some ...
408
  		gfs2_log_unlock(sdp);
4a5868120   Bob Peterson   GFS2: Set log des...
409
410
411
  		page = gfs2_get_log_desc(sdp,
  					 is_databuf ? GFS2_LOG_DESC_JDATA :
  					 GFS2_LOG_DESC_METADATA, num + 1, num);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
412
  		ld = page_address(page);
905d2aefa   Bob Peterson   [GFS2] Move some ...
413
  		gfs2_log_lock(sdp);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
414
  		ptr = (__be64 *)(ld + 1);
b3b94faa5   David Teigland   [GFS2] The core o...
415
416
  
  		n = 0;
c0752aa7e   Bob Peterson   GFS2: eliminate l...
417
  		list_for_each_entry_continue(bd1, blist, bd_list) {
b3b94faa5   David Teigland   [GFS2] The core o...
418
  			*ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
419
420
421
422
  			if (is_databuf) {
  				gfs2_check_magic(bd1->bd_bh);
  				*ptr++ = cpu_to_be64(buffer_escaped(bd1->bd_bh) ? 1 : 0);
  			}
b3b94faa5   David Teigland   [GFS2] The core o...
423
424
425
  			if (++n >= num)
  				break;
  		}
905d2aefa   Bob Peterson   [GFS2] Move some ...
426
  		gfs2_log_unlock(sdp);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
427
  		gfs2_log_write_page(sdp, page);
905d2aefa   Bob Peterson   [GFS2] Move some ...
428
  		gfs2_log_lock(sdp);
b3b94faa5   David Teigland   [GFS2] The core o...
429
430
  
  		n = 0;
c0752aa7e   Bob Peterson   GFS2: eliminate l...
431
  		list_for_each_entry_continue(bd2, blist, bd_list) {
16615be18   Steven Whitehouse   [GFS2] Clean up j...
432
  			get_bh(bd2->bd_bh);
905d2aefa   Bob Peterson   [GFS2] Move some ...
433
  			gfs2_log_unlock(sdp);
16615be18   Steven Whitehouse   [GFS2] Clean up j...
434
  			lock_buffer(bd2->bd_bh);
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  
  			if (buffer_escaped(bd2->bd_bh)) {
  				void *kaddr;
  				page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  				ptr = page_address(page);
  				kaddr = kmap_atomic(bd2->bd_bh->b_page);
  				memcpy(ptr, kaddr + bh_offset(bd2->bd_bh),
  				       bd2->bd_bh->b_size);
  				kunmap_atomic(kaddr);
  				*(__be32 *)ptr = 0;
  				clear_buffer_escaped(bd2->bd_bh);
  				unlock_buffer(bd2->bd_bh);
  				brelse(bd2->bd_bh);
  				gfs2_log_write_page(sdp, page);
  			} else {
  				gfs2_log_write_bh(sdp, bd2->bd_bh);
  			}
905d2aefa   Bob Peterson   [GFS2] Move some ...
452
  			gfs2_log_lock(sdp);
b3b94faa5   David Teigland   [GFS2] The core o...
453
454
455
  			if (++n >= num)
  				break;
  		}
905d2aefa   Bob Peterson   [GFS2] Move some ...
456
  		BUG_ON(total < num);
b3b94faa5   David Teigland   [GFS2] The core o...
457
458
  		total -= num;
  	}
905d2aefa   Bob Peterson   [GFS2] Move some ...
459
  	gfs2_log_unlock(sdp);
b3b94faa5   David Teigland   [GFS2] The core o...
460
  }
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
461
  static void buf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
462
463
  {
  	unsigned int limit = buf_limit(sdp); /* 503 for 4k blocks */
022ef4fee   Steven Whitehouse   GFS2: Move log bu...
464
  	unsigned int nbuf;
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
465
466
  	if (tr == NULL)
  		return;
022ef4fee   Steven Whitehouse   GFS2: Move log bu...
467
468
  	nbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  	gfs2_before_commit(sdp, limit, nbuf, &tr->tr_buf, 0);
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
469
  }
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
470
  static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
b3b94faa5   David Teigland   [GFS2] The core o...
471
  {
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
472
  	struct list_head *head;
b3b94faa5   David Teigland   [GFS2] The core o...
473
  	struct gfs2_bufdata *bd;
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
474
  	if (tr == NULL)
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
475
  		return;
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
476

d69a3c656   Steven Whitehouse   GFS2: Move log bu...
477
  	head = &tr->tr_buf;
b3b94faa5   David Teigland   [GFS2] The core o...
478
  	while (!list_empty(head)) {
c0752aa7e   Bob Peterson   GFS2: eliminate l...
479
480
  		bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  		list_del_init(&bd->bd_list);
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
481
  		gfs2_unpin(sdp, bd->bd_bh, tr);
b3b94faa5   David Teigland   [GFS2] The core o...
482
  	}
b3b94faa5   David Teigland   [GFS2] The core o...
483
484
485
  }
  
  static void buf_lo_before_scan(struct gfs2_jdesc *jd,
551676226   Al Viro   [GFS2] split and ...
486
  			       struct gfs2_log_header_host *head, int pass)
b3b94faa5   David Teigland   [GFS2] The core o...
487
  {
b3b94faa5   David Teigland   [GFS2] The core o...
488
489
  	if (pass != 0)
  		return;
a17d758b6   Bob Peterson   GFS2: Move recove...
490
491
  	jd->jd_found_blocks = 0;
  	jd->jd_replayed_blocks = 0;
b3b94faa5   David Teigland   [GFS2] The core o...
492
493
494
495
496
497
  }
  
  static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  				struct gfs2_log_descriptor *ld, __be64 *ptr,
  				int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
498
499
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
500
  	struct gfs2_glock *gl = ip->i_gl;
b3b94faa5   David Teigland   [GFS2] The core o...
501
502
  	unsigned int blks = be32_to_cpu(ld->ld_data1);
  	struct buffer_head *bh_log, *bh_ip;
cd915493f   Steven Whitehouse   [GFS2] Change all...
503
  	u64 blkno;
b3b94faa5   David Teigland   [GFS2] The core o...
504
505
506
507
  	int error = 0;
  
  	if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
  		return 0;
e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
508
  	gfs2_replay_incr_blk(jd, &start);
b3b94faa5   David Teigland   [GFS2] The core o...
509

e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
510
  	for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
b3b94faa5   David Teigland   [GFS2] The core o...
511
  		blkno = be64_to_cpu(*ptr++);
a17d758b6   Bob Peterson   GFS2: Move recove...
512
  		jd->jd_found_blocks++;
b3b94faa5   David Teigland   [GFS2] The core o...
513

a17d758b6   Bob Peterson   GFS2: Move recove...
514
  		if (gfs2_revoke_check(jd, blkno, start))
b3b94faa5   David Teigland   [GFS2] The core o...
515
516
517
  			continue;
  
  		error = gfs2_replay_read_block(jd, start, &bh_log);
82ffa5163   Steven Whitehouse   [GFS2] More style...
518
519
  		if (error)
  			return error;
b3b94faa5   David Teigland   [GFS2] The core o...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  
  		bh_ip = gfs2_meta_new(gl, blkno);
  		memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  
  		if (gfs2_meta_check(sdp, bh_ip))
  			error = -EIO;
  		else
  			mark_buffer_dirty(bh_ip);
  
  		brelse(bh_log);
  		brelse(bh_ip);
  
  		if (error)
  			break;
a17d758b6   Bob Peterson   GFS2: Move recove...
534
  		jd->jd_replayed_blocks++;
b3b94faa5   David Teigland   [GFS2] The core o...
535
536
537
538
  	}
  
  	return error;
  }
7c0ef28a2   Steven Whitehouse   GFS2: Move gfs2_s...
539
540
541
542
543
544
545
546
547
  /**
   * gfs2_meta_sync - Sync all buffers associated with a glock
   * @gl: The glock
   *
   */
  
  static void gfs2_meta_sync(struct gfs2_glock *gl)
  {
  	struct address_space *mapping = gfs2_glock2aspace(gl);
15562c439   Bob Peterson   GFS2: Move glock ...
548
  	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
7c0ef28a2   Steven Whitehouse   GFS2: Move gfs2_s...
549
  	int error;
70d4ee94b   Steven Whitehouse   GFS2: Use only a ...
550
551
  	if (mapping == NULL)
  		mapping = &sdp->sd_aspace;
7c0ef28a2   Steven Whitehouse   GFS2: Move gfs2_s...
552
553
554
555
  	filemap_fdatawrite(mapping);
  	error = filemap_fdatawait(mapping);
  
  	if (error)
15562c439   Bob Peterson   GFS2: Move glock ...
556
  		gfs2_io_error(gl->gl_name.ln_sbd);
7c0ef28a2   Steven Whitehouse   GFS2: Move gfs2_s...
557
  }
b3b94faa5   David Teigland   [GFS2] The core o...
558
559
  static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
560
561
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa5   David Teigland   [GFS2] The core o...
562
563
  
  	if (error) {
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
564
  		gfs2_meta_sync(ip->i_gl);
b3b94faa5   David Teigland   [GFS2] The core o...
565
566
567
568
  		return;
  	}
  	if (pass != 1)
  		return;
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
569
  	gfs2_meta_sync(ip->i_gl);
b3b94faa5   David Teigland   [GFS2] The core o...
570
571
572
  
  	fs_info(sdp, "jid=%u: Replayed %u of %u blocks
  ",
a17d758b6   Bob Peterson   GFS2: Move recove...
573
  	        jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
b3b94faa5   David Teigland   [GFS2] The core o...
574
  }
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
575
  static void revoke_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
b3b94faa5   David Teigland   [GFS2] The core o...
576
  {
b3b94faa5   David Teigland   [GFS2] The core o...
577
  	struct gfs2_meta_header *mh;
b3b94faa5   David Teigland   [GFS2] The core o...
578
579
  	unsigned int offset;
  	struct list_head *head = &sdp->sd_log_le_revoke;
82e86087b   Steven Whitehouse   [GFS2] Replace re...
580
  	struct gfs2_bufdata *bd;
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
581
  	struct page *page;
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
582
  	unsigned int length;
b3b94faa5   David Teigland   [GFS2] The core o...
583

5d054964f   Benjamin Marzinski   GFS2: aggressivel...
584
  	gfs2_write_revokes(sdp);
b3b94faa5   David Teigland   [GFS2] The core o...
585
586
  	if (!sdp->sd_log_num_revoke)
  		return;
dad30e903   Steven Whitehouse   GFS2: Remove dupl...
587
588
  	length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, sizeof(u64));
  	page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke);
b3b94faa5   David Teigland   [GFS2] The core o...
589
  	offset = sizeof(struct gfs2_log_descriptor);
c0752aa7e   Bob Peterson   GFS2: eliminate l...
590
  	list_for_each_entry(bd, head, bd_list) {
b3b94faa5   David Teigland   [GFS2] The core o...
591
  		sdp->sd_log_num_revoke--;
cd915493f   Steven Whitehouse   [GFS2] Change all...
592
  		if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
b3b94faa5   David Teigland   [GFS2] The core o...
593

e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
594
595
596
597
  			gfs2_log_write_page(sdp, page);
  			page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  			mh = page_address(page);
  			clear_page(mh);
b3b94faa5   David Teigland   [GFS2] The core o...
598
  			mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
e3167ded1   Steven Whitehouse   [GFS] Fix bug in ...
599
600
  			mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  			mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
b3b94faa5   David Teigland   [GFS2] The core o...
601
602
  			offset = sizeof(struct gfs2_meta_header);
  		}
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
603
  		*(__be64 *)(page_address(page) + offset) = cpu_to_be64(bd->bd_blkno);
cd915493f   Steven Whitehouse   [GFS2] Change all...
604
  		offset += sizeof(u64);
b3b94faa5   David Teigland   [GFS2] The core o...
605
606
  	}
  	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
e8c92ed76   Steven Whitehouse   GFS2: Clean up lo...
607
  	gfs2_log_write_page(sdp, page);
b3b94faa5   David Teigland   [GFS2] The core o...
608
  }
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
609
  static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
f42ab0852   Steven Whitehouse   GFS2: Optimise gl...
610
611
612
613
614
615
  {
  	struct list_head *head = &sdp->sd_log_le_revoke;
  	struct gfs2_bufdata *bd;
  	struct gfs2_glock *gl;
  
  	while (!list_empty(head)) {
c0752aa7e   Bob Peterson   GFS2: eliminate l...
616
617
  		bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  		list_del_init(&bd->bd_list);
f42ab0852   Steven Whitehouse   GFS2: Optimise gl...
618
619
620
621
622
623
  		gl = bd->bd_gl;
  		atomic_dec(&gl->gl_revokes);
  		clear_bit(GLF_LFLUSH, &gl->gl_flags);
  		kmem_cache_free(gfs2_bufdata_cachep, bd);
  	}
  }
b3b94faa5   David Teigland   [GFS2] The core o...
624
  static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
551676226   Al Viro   [GFS2] split and ...
625
  				  struct gfs2_log_header_host *head, int pass)
b3b94faa5   David Teigland   [GFS2] The core o...
626
  {
b3b94faa5   David Teigland   [GFS2] The core o...
627
628
  	if (pass != 0)
  		return;
a17d758b6   Bob Peterson   GFS2: Move recove...
629
630
  	jd->jd_found_revokes = 0;
  	jd->jd_replay_tail = head->lh_tail;
b3b94faa5   David Teigland   [GFS2] The core o...
631
632
633
634
635
636
  }
  
  static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  				   struct gfs2_log_descriptor *ld, __be64 *ptr,
  				   int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
637
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa5   David Teigland   [GFS2] The core o...
638
639
640
641
  	unsigned int blks = be32_to_cpu(ld->ld_length);
  	unsigned int revokes = be32_to_cpu(ld->ld_data1);
  	struct buffer_head *bh;
  	unsigned int offset;
cd915493f   Steven Whitehouse   [GFS2] Change all...
642
  	u64 blkno;
b3b94faa5   David Teigland   [GFS2] The core o...
643
644
645
646
647
648
649
  	int first = 1;
  	int error;
  
  	if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  		return 0;
  
  	offset = sizeof(struct gfs2_log_descriptor);
e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
650
  	for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
b3b94faa5   David Teigland   [GFS2] The core o...
651
652
653
654
655
656
  		error = gfs2_replay_read_block(jd, start, &bh);
  		if (error)
  			return error;
  
  		if (!first)
  			gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
cd915493f   Steven Whitehouse   [GFS2] Change all...
657
  		while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
b3b94faa5   David Teigland   [GFS2] The core o...
658
  			blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
a17d758b6   Bob Peterson   GFS2: Move recove...
659
  			error = gfs2_revoke_add(jd, blkno, start);
3ad62e87c   Bob Peterson   [GFS2] Plug an un...
660
661
  			if (error < 0) {
  				brelse(bh);
b3b94faa5   David Teigland   [GFS2] The core o...
662
  				return error;
3ad62e87c   Bob Peterson   [GFS2] Plug an un...
663
  			}
b3b94faa5   David Teigland   [GFS2] The core o...
664
  			else if (error)
a17d758b6   Bob Peterson   GFS2: Move recove...
665
  				jd->jd_found_revokes++;
b3b94faa5   David Teigland   [GFS2] The core o...
666
667
668
  
  			if (!--revokes)
  				break;
cd915493f   Steven Whitehouse   [GFS2] Change all...
669
  			offset += sizeof(u64);
b3b94faa5   David Teigland   [GFS2] The core o...
670
671
672
673
674
675
676
677
678
679
680
681
  		}
  
  		brelse(bh);
  		offset = sizeof(struct gfs2_meta_header);
  		first = 0;
  	}
  
  	return 0;
  }
  
  static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
682
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa5   David Teigland   [GFS2] The core o...
683
684
  
  	if (error) {
a17d758b6   Bob Peterson   GFS2: Move recove...
685
  		gfs2_revoke_clean(jd);
b3b94faa5   David Teigland   [GFS2] The core o...
686
687
688
689
690
691
692
  		return;
  	}
  	if (pass != 1)
  		return;
  
  	fs_info(sdp, "jid=%u: Found %u revoke tags
  ",
a17d758b6   Bob Peterson   GFS2: Move recove...
693
  	        jd->jd_jid, jd->jd_found_revokes);
b3b94faa5   David Teigland   [GFS2] The core o...
694

a17d758b6   Bob Peterson   GFS2: Move recove...
695
  	gfs2_revoke_clean(jd);
b3b94faa5   David Teigland   [GFS2] The core o...
696
  }
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
697
  /**
16615be18   Steven Whitehouse   [GFS2] Clean up j...
698
699
700
   * databuf_lo_before_commit - Scan the data buffers, writing as we go
   *
   */
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
701
  static void databuf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
702
  {
022ef4fee   Steven Whitehouse   GFS2: Move log bu...
703
704
  	unsigned int limit = databuf_limit(sdp);
  	unsigned int nbuf;
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
705
706
  	if (tr == NULL)
  		return;
022ef4fee   Steven Whitehouse   GFS2: Move log bu...
707
708
  	nbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  	gfs2_before_commit(sdp, limit, nbuf, &tr->tr_databuf, 1);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
709
710
711
712
713
714
  }
  
  static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
  				    struct gfs2_log_descriptor *ld,
  				    __be64 *ptr, int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
715
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
716
  	struct gfs2_glock *gl = ip->i_gl;
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
717
718
  	unsigned int blks = be32_to_cpu(ld->ld_data1);
  	struct buffer_head *bh_log, *bh_ip;
cd915493f   Steven Whitehouse   [GFS2] Change all...
719
720
  	u64 blkno;
  	u64 esc;
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
721
722
723
724
  	int error = 0;
  
  	if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  		return 0;
e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
725
726
  	gfs2_replay_incr_blk(jd, &start);
  	for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
727
728
  		blkno = be64_to_cpu(*ptr++);
  		esc = be64_to_cpu(*ptr++);
a17d758b6   Bob Peterson   GFS2: Move recove...
729
  		jd->jd_found_blocks++;
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
730

a17d758b6   Bob Peterson   GFS2: Move recove...
731
  		if (gfs2_revoke_check(jd, blkno, start))
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
  			continue;
  
  		error = gfs2_replay_read_block(jd, start, &bh_log);
  		if (error)
  			return error;
  
  		bh_ip = gfs2_meta_new(gl, blkno);
  		memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  
  		/* Unescape */
  		if (esc) {
  			__be32 *eptr = (__be32 *)bh_ip->b_data;
  			*eptr = cpu_to_be32(GFS2_MAGIC);
  		}
  		mark_buffer_dirty(bh_ip);
  
  		brelse(bh_log);
  		brelse(bh_ip);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
750

a17d758b6   Bob Peterson   GFS2: Move recove...
751
  		jd->jd_replayed_blocks++;
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
752
753
754
755
756
757
758
759
760
  	}
  
  	return error;
  }
  
  /* FIXME: sort out accounting for log blocks etc. */
  
  static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
761
762
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
763
764
  
  	if (error) {
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
765
  		gfs2_meta_sync(ip->i_gl);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
766
767
768
769
770
771
  		return;
  	}
  	if (pass != 1)
  		return;
  
  	/* data sync? */
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
772
  	gfs2_meta_sync(ip->i_gl);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
773
774
775
  
  	fs_info(sdp, "jid=%u: Replayed %u of %u data blocks
  ",
a17d758b6   Bob Peterson   GFS2: Move recove...
776
  		jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
777
  }
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
778
  static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
779
  {
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
780
  	struct list_head *head;
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
781
  	struct gfs2_bufdata *bd;
d69a3c656   Steven Whitehouse   GFS2: Move log bu...
782
  	if (tr == NULL)
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
783
  		return;
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
784

d69a3c656   Steven Whitehouse   GFS2: Move log bu...
785
  	head = &tr->tr_databuf;
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
786
  	while (!list_empty(head)) {
c0752aa7e   Bob Peterson   GFS2: eliminate l...
787
788
  		bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
  		list_del_init(&bd->bd_list);
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
789
  		gfs2_unpin(sdp, bd->bd_bh, tr);
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
790
  	}
b3b94faa5   David Teigland   [GFS2] The core o...
791
  }
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
792

b09e593d7   Steven Whitehouse   [GFS2] Fix a ref ...
793
  const struct gfs2_log_operations gfs2_buf_lops = {
b3b94faa5   David Teigland   [GFS2] The core o...
794
795
796
797
798
  	.lo_before_commit = buf_lo_before_commit,
  	.lo_after_commit = buf_lo_after_commit,
  	.lo_before_scan = buf_lo_before_scan,
  	.lo_scan_elements = buf_lo_scan_elements,
  	.lo_after_scan = buf_lo_after_scan,
ea67eedb2   Steven Whitehouse   [GFS2] Fix end of...
799
  	.lo_name = "buf",
b3b94faa5   David Teigland   [GFS2] The core o...
800
  };
b09e593d7   Steven Whitehouse   [GFS2] Fix a ref ...
801
  const struct gfs2_log_operations gfs2_revoke_lops = {
b3b94faa5   David Teigland   [GFS2] The core o...
802
  	.lo_before_commit = revoke_lo_before_commit,
f42ab0852   Steven Whitehouse   GFS2: Optimise gl...
803
  	.lo_after_commit = revoke_lo_after_commit,
b3b94faa5   David Teigland   [GFS2] The core o...
804
805
806
  	.lo_before_scan = revoke_lo_before_scan,
  	.lo_scan_elements = revoke_lo_scan_elements,
  	.lo_after_scan = revoke_lo_after_scan,
ea67eedb2   Steven Whitehouse   [GFS2] Fix end of...
807
  	.lo_name = "revoke",
b3b94faa5   David Teigland   [GFS2] The core o...
808
  };
b09e593d7   Steven Whitehouse   [GFS2] Fix a ref ...
809
  const struct gfs2_log_operations gfs2_databuf_lops = {
b3b94faa5   David Teigland   [GFS2] The core o...
810
  	.lo_before_commit = databuf_lo_before_commit,
18ec7d5c3   Steven Whitehouse   [GFS2] Make journ...
811
812
813
  	.lo_after_commit = databuf_lo_after_commit,
  	.lo_scan_elements = databuf_lo_scan_elements,
  	.lo_after_scan = databuf_lo_after_scan,
ea67eedb2   Steven Whitehouse   [GFS2] Fix end of...
814
  	.lo_name = "databuf",
b3b94faa5   David Teigland   [GFS2] The core o...
815
  };
b09e593d7   Steven Whitehouse   [GFS2] Fix a ref ...
816
  const struct gfs2_log_operations *gfs2_log_ops[] = {
16615be18   Steven Whitehouse   [GFS2] Clean up j...
817
  	&gfs2_databuf_lops,
b3b94faa5   David Teigland   [GFS2] The core o...
818
  	&gfs2_buf_lops,
16615be18   Steven Whitehouse   [GFS2] Clean up j...
819
  	&gfs2_revoke_lops,
ea67eedb2   Steven Whitehouse   [GFS2] Fix end of...
820
  	NULL,
b3b94faa5   David Teigland   [GFS2] The core o...
821
  };