Merge tag 'libnvdimm-for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[linux-2.6-block.git] / fs / gfs2 / lops.c
CommitLineData
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>
75ca61c1 15#include <linux/mempool.h>
5c676f6d 16#include <linux/gfs2_ondisk.h>
c969f58c
SW
17#include <linux/bio.h>
18#include <linux/fs.h>
7f63257d 19#include <linux/list_sort.h>
b3b94faa 20
c1696fb8 21#include "dir.h"
b3b94faa 22#include "gfs2.h"
5c676f6d 23#include "incore.h"
2332c443 24#include "inode.h"
b3b94faa
DT
25#include "glock.h"
26#include "log.h"
27#include "lops.h"
28#include "meta_io.h"
29#include "recovery.h"
30#include "rgrp.h"
31#include "trans.h"
5c676f6d 32#include "util.h"
63997775 33#include "trace_gfs2.h"
b3b94faa 34
9b9107a5
SW
35/**
36 * gfs2_pin - Pin a buffer in memory
37 * @sdp: The superblock
38 * @bh: The buffer to be pinned
39 *
40 * The log lock must be held when calling this function
41 */
767f433f 42void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
9b9107a5
SW
43{
44 struct gfs2_bufdata *bd;
45
29687a2a 46 BUG_ON(!current->journal_info);
9b9107a5
SW
47
48 clear_buffer_dirty(bh);
49 if (test_set_buffer_pinned(bh))
50 gfs2_assert_withdraw(sdp, 0);
51 if (!buffer_uptodate(bh))
9e1a9ecd 52 gfs2_io_error_bh_wd(sdp, bh);
9b9107a5
SW
53 bd = bh->b_private;
54 /* If this buffer is in the AIL and it has already been written
55 * to in-place disk block, remove it from the AIL.
56 */
c618e87a 57 spin_lock(&sdp->sd_ail_lock);
16ca9412
BM
58 if (bd->bd_tr)
59 list_move(&bd->bd_ail_st_list, &bd->bd_tr->tr_ail2_list);
c618e87a 60 spin_unlock(&sdp->sd_ail_lock);
9b9107a5 61 get_bh(bh);
5e687eac 62 atomic_inc(&sdp->sd_log_pinned);
63997775 63 trace_gfs2_pin(bd, 1);
9b9107a5
SW
64}
65
7c9ca621
BP
66static bool buffer_is_rgrp(const struct gfs2_bufdata *bd)
67{
68 return bd->bd_gl->gl_name.ln_type == LM_TYPE_RGRP;
69}
70
71static void maybe_release_space(struct gfs2_bufdata *bd)
72{
73 struct gfs2_glock *gl = bd->bd_gl;
15562c43 74 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
6f6597ba 75 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
7c9ca621
BP
76 unsigned int index = bd->bd_bh->b_blocknr - gl->gl_name.ln_number;
77 struct gfs2_bitmap *bi = rgd->rd_bits + index;
78
5a7c6690 79 if (bi->bi_clone == NULL)
7c9ca621
BP
80 return;
81 if (sdp->sd_args.ar_discard)
66fc061b 82 gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL);
7c9ca621 83 memcpy(bi->bi_clone + bi->bi_offset,
281b4952 84 bd->bd_bh->b_data + bi->bi_offset, bi->bi_bytes);
7c9ca621
BP
85 clear_bit(GBF_FULL, &bi->bi_flags);
86 rgd->rd_free_clone = rgd->rd_free;
5ea5050c 87 rgd->rd_extfail_pt = rgd->rd_free;
7c9ca621
BP
88}
89
9b9107a5
SW
90/**
91 * gfs2_unpin - Unpin a buffer
92 * @sdp: the filesystem the buffer belongs to
93 * @bh: The buffer to unpin
94 * @ai:
29687a2a 95 * @flags: The inode dirty flags
9b9107a5
SW
96 *
97 */
98
99static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
16ca9412 100 struct gfs2_trans *tr)
9b9107a5
SW
101{
102 struct gfs2_bufdata *bd = bh->b_private;
103
29687a2a
SW
104 BUG_ON(!buffer_uptodate(bh));
105 BUG_ON(!buffer_pinned(bh));
9b9107a5
SW
106
107 lock_buffer(bh);
108 mark_buffer_dirty(bh);
109 clear_buffer_pinned(bh);
110
7c9ca621
BP
111 if (buffer_is_rgrp(bd))
112 maybe_release_space(bd);
113
d6a079e8 114 spin_lock(&sdp->sd_ail_lock);
16ca9412 115 if (bd->bd_tr) {
9b9107a5
SW
116 list_del(&bd->bd_ail_st_list);
117 brelse(bh);
118 } else {
119 struct gfs2_glock *gl = bd->bd_gl;
120 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
121 atomic_inc(&gl->gl_ail_count);
122 }
16ca9412
BM
123 bd->bd_tr = tr;
124 list_add(&bd->bd_ail_st_list, &tr->tr_ail1_list);
d6a079e8
DC
125 spin_unlock(&sdp->sd_ail_lock);
126
29687a2a 127 clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
63997775 128 trace_gfs2_pin(bd, 0);
9b9107a5 129 unlock_buffer(bh);
5e687eac 130 atomic_dec(&sdp->sd_log_pinned);
9b9107a5
SW
131}
132
e8c92ed7 133static void gfs2_log_incr_head(struct gfs2_sbd *sdp)
16615be1 134{
e8c92ed7
SW
135 BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
136 (sdp->sd_log_flush_head != sdp->sd_log_head));
137
722f6f62 138 if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks)
e8c92ed7 139 sdp->sd_log_flush_head = 0;
16615be1
SW
140}
141
c1696fb8 142u64 gfs2_log_bmap(struct gfs2_sbd *sdp)
16615be1 143{
e8c92ed7
SW
144 unsigned int lbn = sdp->sd_log_flush_head;
145 struct gfs2_journal_extent *je;
146 u64 block;
147
b50f227b
SW
148 list_for_each_entry(je, &sdp->sd_jdesc->extent_list, list) {
149 if ((lbn >= je->lblock) && (lbn < (je->lblock + je->blocks))) {
e8c92ed7
SW
150 block = je->dblock + lbn - je->lblock;
151 gfs2_log_incr_head(sdp);
152 return block;
153 }
154 }
155
156 return -1;
16615be1
SW
157}
158
e8c92ed7
SW
159/**
160 * gfs2_end_log_write_bh - end log write of pagecache data with buffers
161 * @sdp: The superblock
162 * @bvec: The bio_vec
163 * @error: The i/o status
164 *
4519eaad 165 * This finds the relevant buffers and unlocks them and sets the
e8c92ed7
SW
166 * error flag according to the status of the i/o request. This is
167 * used when the log is writing data which has an in-place version
168 * that is pinned in the pagecache.
169 */
170
6dc4f100
ML
171static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp,
172 struct bio_vec *bvec,
4e4cbee9 173 blk_status_t error)
16615be1 174{
e8c92ed7
SW
175 struct buffer_head *bh, *next;
176 struct page *page = bvec->bv_page;
177 unsigned size;
178
179 bh = page_buffers(page);
180 size = bvec->bv_len;
181 while (bh_offset(bh) < bvec->bv_offset)
182 bh = bh->b_this_page;
183 do {
184 if (error)
87354e5d 185 mark_buffer_write_io_error(bh);
e8c92ed7
SW
186 unlock_buffer(bh);
187 next = bh->b_this_page;
188 size -= bh->b_size;
189 brelse(bh);
190 bh = next;
191 } while(bh && size);
16615be1
SW
192}
193
47ac5537 194/**
e8c92ed7
SW
195 * gfs2_end_log_write - end of i/o to the log
196 * @bio: The bio
23e93c9b 197 * @error: Status of i/o request
e8c92ed7
SW
198 *
199 * Each bio_vec contains either data from the pagecache or data
200 * relating to the log itself. Here we iterate over the bio_vec
201 * array, processing both kinds of data.
47ac5537
SW
202 *
203 */
204
4246a0b6 205static void gfs2_end_log_write(struct bio *bio)
47ac5537 206{
e8c92ed7
SW
207 struct gfs2_sbd *sdp = bio->bi_private;
208 struct bio_vec *bvec;
209 struct page *page;
210 int i;
6dc4f100 211 struct bvec_iter_all iter_all;
e8c92ed7 212
942b0cdd
BP
213 if (bio->bi_status) {
214 fs_err(sdp, "Error %d writing to journal, jid=%u\n",
215 bio->bi_status, sdp->sd_jdesc->jd_jid);
216 wake_up(&sdp->sd_logd_waitq);
217 }
e8c92ed7 218
6dc4f100 219 bio_for_each_segment_all(bvec, bio, i, iter_all) {
e8c92ed7
SW
220 page = bvec->bv_page;
221 if (page_has_buffers(page))
4e4cbee9 222 gfs2_end_log_write_bh(sdp, bvec, bio->bi_status);
e8c92ed7
SW
223 else
224 mempool_free(page, gfs2_page_pool);
225 }
47ac5537 226
e8c92ed7 227 bio_put(bio);
47ac5537
SW
228 if (atomic_dec_and_test(&sdp->sd_log_in_flight))
229 wake_up(&sdp->sd_log_flush_wait);
230}
231
232/**
5b846095
AD
233 * gfs2_log_submit_bio - Submit any pending log bio
234 * @biop: Address of the bio pointer
23e93c9b
BP
235 * @op: REQ_OP
236 * @op_flags: req_flag_bits
47ac5537 237 *
e8c92ed7
SW
238 * Submit any pending part-built or full bio to the block device. If
239 * there is no pending bio, then this is a no-op.
47ac5537
SW
240 */
241
23e93c9b 242void gfs2_log_submit_bio(struct bio **biop, int op, int op_flags)
47ac5537 243{
5b846095
AD
244 struct bio *bio = *biop;
245 if (bio) {
246 struct gfs2_sbd *sdp = bio->bi_private;
e8c92ed7 247 atomic_inc(&sdp->sd_log_in_flight);
23e93c9b 248 bio_set_op_attrs(bio, op, op_flags);
5b846095
AD
249 submit_bio(bio);
250 *biop = NULL;
e8c92ed7
SW
251 }
252}
47ac5537 253
e8c92ed7 254/**
5b846095
AD
255 * gfs2_log_alloc_bio - Allocate a bio
256 * @sdp: The super block
257 * @blkno: The device block number we want to write to
258 * @end_io: The bi_end_io callback
e8c92ed7 259 *
5b846095 260 * Allocate a new bio, initialize it with the given parameters and return it.
e8c92ed7 261 *
5b846095 262 * Returns: The newly allocated bio
e8c92ed7
SW
263 */
264
5b846095
AD
265static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno,
266 bio_end_io_t *end_io)
e8c92ed7
SW
267{
268 struct super_block *sb = sdp->sd_vfs;
5b846095 269 struct bio *bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
e8c92ed7 270
4f024f37 271 bio->bi_iter.bi_sector = blkno * (sb->s_blocksize >> 9);
74d46992 272 bio_set_dev(bio, sb->s_bdev);
5b846095 273 bio->bi_end_io = end_io;
e8c92ed7
SW
274 bio->bi_private = sdp;
275
e8c92ed7 276 return bio;
47ac5537
SW
277}
278
279/**
e8c92ed7 280 * gfs2_log_get_bio - Get cached log bio, or allocate a new one
5b846095 281 * @sdp: The super block
e8c92ed7 282 * @blkno: The device block number we want to write to
5b846095
AD
283 * @bio: The bio to get or allocate
284 * @op: REQ_OP
285 * @end_io: The bi_end_io callback
286 * @flush: Always flush the current bio and allocate a new one?
e8c92ed7
SW
287 *
288 * If there is a cached bio, then if the next block number is sequential
289 * with the previous one, return it, otherwise flush the bio to the
5b846095 290 * device. If there is no cached bio, or we just flushed it, then
e8c92ed7 291 * allocate a new one.
47ac5537 292 *
e8c92ed7 293 * Returns: The bio to use for log writes
47ac5537
SW
294 */
295
5b846095
AD
296static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno,
297 struct bio **biop, int op,
298 bio_end_io_t *end_io, bool flush)
47ac5537 299{
5b846095 300 struct bio *bio = *biop;
e8c92ed7
SW
301
302 if (bio) {
5b846095
AD
303 u64 nblk;
304
f73a1c7d 305 nblk = bio_end_sector(bio);
e8c92ed7 306 nblk >>= sdp->sd_fsb2bb_shift;
5b846095 307 if (blkno == nblk && !flush)
e8c92ed7 308 return bio;
23e93c9b 309 gfs2_log_submit_bio(biop, op, 0);
e8c92ed7
SW
310 }
311
5b846095
AD
312 *biop = gfs2_log_alloc_bio(sdp, blkno, end_io);
313 return *biop;
47ac5537
SW
314}
315
316/**
e8c92ed7 317 * gfs2_log_write - write to log
47ac5537 318 * @sdp: the filesystem
e8c92ed7
SW
319 * @page: the page to write
320 * @size: the size of the data to write
321 * @offset: the offset within the page
c1696fb8 322 * @blkno: block number of the log entry
47ac5537 323 *
e8c92ed7
SW
324 * Try and add the page segment to the current bio. If that fails,
325 * submit the current bio to the device and create a new one, and
326 * then add the page segment to that.
47ac5537
SW
327 */
328
c1696fb8
BP
329void gfs2_log_write(struct gfs2_sbd *sdp, struct page *page,
330 unsigned size, unsigned offset, u64 blkno)
47ac5537 331{
e8c92ed7
SW
332 struct bio *bio;
333 int ret;
334
5b846095
AD
335 bio = gfs2_log_get_bio(sdp, blkno, &sdp->sd_log_bio, REQ_OP_WRITE,
336 gfs2_end_log_write, false);
e8c92ed7
SW
337 ret = bio_add_page(bio, page, size, offset);
338 if (ret == 0) {
5b846095
AD
339 bio = gfs2_log_get_bio(sdp, blkno, &sdp->sd_log_bio,
340 REQ_OP_WRITE, gfs2_end_log_write, true);
e8c92ed7
SW
341 ret = bio_add_page(bio, page, size, offset);
342 WARN_ON(ret == 0);
343 }
344}
47ac5537 345
e8c92ed7
SW
346/**
347 * gfs2_log_write_bh - write a buffer's content to the log
348 * @sdp: The super block
349 * @bh: The buffer pointing to the in-place location
350 *
351 * This writes the content of the buffer to the next available location
352 * in the log. The buffer will be unlocked once the i/o to the log has
353 * completed.
354 */
355
356static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh)
357{
c1696fb8
BP
358 gfs2_log_write(sdp, bh->b_page, bh->b_size, bh_offset(bh),
359 gfs2_log_bmap(sdp));
e8c92ed7 360}
47ac5537 361
e8c92ed7
SW
362/**
363 * gfs2_log_write_page - write one block stored in a page, into the log
364 * @sdp: The superblock
365 * @page: The struct page
366 *
367 * This writes the first block-sized part of the page into the log. Note
368 * that the page must have been allocated from the gfs2_page_pool mempool
369 * and that after this has been called, ownership has been transferred and
370 * the page may be freed at any time.
371 */
47ac5537 372
e8c92ed7
SW
373void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page)
374{
375 struct super_block *sb = sdp->sd_vfs;
c1696fb8
BP
376 gfs2_log_write(sdp, page, sb->s_blocksize, 0,
377 gfs2_log_bmap(sdp));
47ac5537 378}
16615be1 379
dad30e90
SW
380static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type,
381 u32 ld_length, u32 ld_data1)
16615be1 382{
144a4c2f 383 struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
e8c92ed7
SW
384 struct gfs2_log_descriptor *ld = page_address(page);
385 clear_page(ld);
16615be1
SW
386 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
387 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
388 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
389 ld->ld_type = cpu_to_be32(ld_type);
dad30e90
SW
390 ld->ld_length = cpu_to_be32(ld_length);
391 ld->ld_data1 = cpu_to_be32(ld_data1);
16615be1 392 ld->ld_data2 = 0;
e8c92ed7 393 return page;
16615be1
SW
394}
395
dad30e90
SW
396static void gfs2_check_magic(struct buffer_head *bh)
397{
398 void *kaddr;
399 __be32 *ptr;
400
401 clear_buffer_escaped(bh);
402 kaddr = kmap_atomic(bh->b_page);
403 ptr = kaddr + bh_offset(bh);
404 if (*ptr == cpu_to_be32(GFS2_MAGIC))
405 set_buffer_escaped(bh);
406 kunmap_atomic(kaddr);
407}
408
7f63257d
BM
409static int blocknr_cmp(void *priv, struct list_head *a, struct list_head *b)
410{
411 struct gfs2_bufdata *bda, *bdb;
412
413 bda = list_entry(a, struct gfs2_bufdata, bd_list);
414 bdb = list_entry(b, struct gfs2_bufdata, bd_list);
415
416 if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
417 return -1;
418 if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
419 return 1;
420 return 0;
421}
422
dad30e90
SW
423static void gfs2_before_commit(struct gfs2_sbd *sdp, unsigned int limit,
424 unsigned int total, struct list_head *blist,
425 bool is_databuf)
b3b94faa 426{
b3b94faa
DT
427 struct gfs2_log_descriptor *ld;
428 struct gfs2_bufdata *bd1 = NULL, *bd2;
e8c92ed7 429 struct page *page;
b3b94faa
DT
430 unsigned int num;
431 unsigned n;
432 __be64 *ptr;
433
905d2aef 434 gfs2_log_lock(sdp);
7f63257d 435 list_sort(NULL, blist, blocknr_cmp);
c0752aa7 436 bd1 = bd2 = list_prepare_entry(bd1, blist, bd_list);
b3b94faa
DT
437 while(total) {
438 num = total;
439 if (total > limit)
440 num = limit;
905d2aef 441 gfs2_log_unlock(sdp);
4a586812
BP
442 page = gfs2_get_log_desc(sdp,
443 is_databuf ? GFS2_LOG_DESC_JDATA :
444 GFS2_LOG_DESC_METADATA, num + 1, num);
e8c92ed7 445 ld = page_address(page);
905d2aef 446 gfs2_log_lock(sdp);
e8c92ed7 447 ptr = (__be64 *)(ld + 1);
b3b94faa
DT
448
449 n = 0;
c0752aa7 450 list_for_each_entry_continue(bd1, blist, bd_list) {
b3b94faa 451 *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
dad30e90
SW
452 if (is_databuf) {
453 gfs2_check_magic(bd1->bd_bh);
454 *ptr++ = cpu_to_be64(buffer_escaped(bd1->bd_bh) ? 1 : 0);
455 }
b3b94faa
DT
456 if (++n >= num)
457 break;
458 }
459
905d2aef 460 gfs2_log_unlock(sdp);
e8c92ed7 461 gfs2_log_write_page(sdp, page);
905d2aef 462 gfs2_log_lock(sdp);
b3b94faa
DT
463
464 n = 0;
c0752aa7 465 list_for_each_entry_continue(bd2, blist, bd_list) {
16615be1 466 get_bh(bd2->bd_bh);
905d2aef 467 gfs2_log_unlock(sdp);
16615be1 468 lock_buffer(bd2->bd_bh);
dad30e90
SW
469
470 if (buffer_escaped(bd2->bd_bh)) {
471 void *kaddr;
472 page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
473 ptr = page_address(page);
474 kaddr = kmap_atomic(bd2->bd_bh->b_page);
475 memcpy(ptr, kaddr + bh_offset(bd2->bd_bh),
476 bd2->bd_bh->b_size);
477 kunmap_atomic(kaddr);
478 *(__be32 *)ptr = 0;
479 clear_buffer_escaped(bd2->bd_bh);
480 unlock_buffer(bd2->bd_bh);
481 brelse(bd2->bd_bh);
482 gfs2_log_write_page(sdp, page);
483 } else {
484 gfs2_log_write_bh(sdp, bd2->bd_bh);
485 }
905d2aef 486 gfs2_log_lock(sdp);
b3b94faa
DT
487 if (++n >= num)
488 break;
489 }
490
905d2aef 491 BUG_ON(total < num);
b3b94faa
DT
492 total -= num;
493 }
905d2aef 494 gfs2_log_unlock(sdp);
b3b94faa
DT
495}
496
d69a3c65 497static void buf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
dad30e90
SW
498{
499 unsigned int limit = buf_limit(sdp); /* 503 for 4k blocks */
022ef4fe 500 unsigned int nbuf;
d69a3c65
SW
501 if (tr == NULL)
502 return;
022ef4fe
SW
503 nbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
504 gfs2_before_commit(sdp, limit, nbuf, &tr->tr_buf, 0);
dad30e90
SW
505}
506
16ca9412 507static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
b3b94faa 508{
d69a3c65 509 struct list_head *head;
b3b94faa
DT
510 struct gfs2_bufdata *bd;
511
d69a3c65 512 if (tr == NULL)
16ca9412 513 return;
16ca9412 514
d69a3c65 515 head = &tr->tr_buf;
b3b94faa 516 while (!list_empty(head)) {
c0752aa7
BP
517 bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
518 list_del_init(&bd->bd_list);
16ca9412 519 gfs2_unpin(sdp, bd->bd_bh, tr);
b3b94faa 520 }
b3b94faa
DT
521}
522
523static void buf_lo_before_scan(struct gfs2_jdesc *jd,
55167622 524 struct gfs2_log_header_host *head, int pass)
b3b94faa 525{
b3b94faa
DT
526 if (pass != 0)
527 return;
528
a17d758b
BP
529 jd->jd_found_blocks = 0;
530 jd->jd_replayed_blocks = 0;
b3b94faa
DT
531}
532
533static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
534 struct gfs2_log_descriptor *ld, __be64 *ptr,
535 int pass)
536{
feaa7bba
SW
537 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
538 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
5c676f6d 539 struct gfs2_glock *gl = ip->i_gl;
b3b94faa
DT
540 unsigned int blks = be32_to_cpu(ld->ld_data1);
541 struct buffer_head *bh_log, *bh_ip;
cd915493 542 u64 blkno;
b3b94faa
DT
543 int error = 0;
544
545 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
546 return 0;
547
e1cb6be9 548 gfs2_replay_incr_blk(jd, &start);
b3b94faa 549
e1cb6be9 550 for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
b3b94faa
DT
551 blkno = be64_to_cpu(*ptr++);
552
a17d758b 553 jd->jd_found_blocks++;
b3b94faa 554
a17d758b 555 if (gfs2_revoke_check(jd, blkno, start))
b3b94faa
DT
556 continue;
557
558 error = gfs2_replay_read_block(jd, start, &bh_log);
82ffa516
SW
559 if (error)
560 return error;
b3b94faa
DT
561
562 bh_ip = gfs2_meta_new(gl, blkno);
563 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
564
565 if (gfs2_meta_check(sdp, bh_ip))
566 error = -EIO;
567 else
568 mark_buffer_dirty(bh_ip);
569
570 brelse(bh_log);
571 brelse(bh_ip);
572
573 if (error)
574 break;
575
a17d758b 576 jd->jd_replayed_blocks++;
b3b94faa
DT
577 }
578
579 return error;
580}
581
7c0ef28a
SW
582/**
583 * gfs2_meta_sync - Sync all buffers associated with a glock
584 * @gl: The glock
585 *
586 */
587
588static void gfs2_meta_sync(struct gfs2_glock *gl)
589{
590 struct address_space *mapping = gfs2_glock2aspace(gl);
15562c43 591 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
7c0ef28a
SW
592 int error;
593
70d4ee94
SW
594 if (mapping == NULL)
595 mapping = &sdp->sd_aspace;
596
7c0ef28a
SW
597 filemap_fdatawrite(mapping);
598 error = filemap_fdatawait(mapping);
599
600 if (error)
15562c43 601 gfs2_io_error(gl->gl_name.ln_sbd);
7c0ef28a
SW
602}
603
b3b94faa
DT
604static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
605{
feaa7bba
SW
606 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
607 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa
DT
608
609 if (error) {
7276b3b0 610 gfs2_meta_sync(ip->i_gl);
b3b94faa
DT
611 return;
612 }
613 if (pass != 1)
614 return;
615
7276b3b0 616 gfs2_meta_sync(ip->i_gl);
b3b94faa
DT
617
618 fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
a17d758b 619 jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
b3b94faa
DT
620}
621
d69a3c65 622static void revoke_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
b3b94faa 623{
b3b94faa 624 struct gfs2_meta_header *mh;
b3b94faa
DT
625 unsigned int offset;
626 struct list_head *head = &sdp->sd_log_le_revoke;
82e86087 627 struct gfs2_bufdata *bd;
e8c92ed7 628 struct page *page;
dad30e90 629 unsigned int length;
b3b94faa 630
5d054964 631 gfs2_write_revokes(sdp);
b3b94faa
DT
632 if (!sdp->sd_log_num_revoke)
633 return;
634
dad30e90
SW
635 length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, sizeof(u64));
636 page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke);
b3b94faa
DT
637 offset = sizeof(struct gfs2_log_descriptor);
638
c0752aa7 639 list_for_each_entry(bd, head, bd_list) {
b3b94faa
DT
640 sdp->sd_log_num_revoke--;
641
cd915493 642 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
b3b94faa 643
e8c92ed7
SW
644 gfs2_log_write_page(sdp, page);
645 page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
646 mh = page_address(page);
647 clear_page(mh);
b3b94faa 648 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
e3167ded
SW
649 mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
650 mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
b3b94faa
DT
651 offset = sizeof(struct gfs2_meta_header);
652 }
653
e8c92ed7 654 *(__be64 *)(page_address(page) + offset) = cpu_to_be64(bd->bd_blkno);
cd915493 655 offset += sizeof(u64);
b3b94faa
DT
656 }
657 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
658
e8c92ed7 659 gfs2_log_write_page(sdp, page);
b3b94faa
DT
660}
661
16ca9412 662static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
f42ab085
SW
663{
664 struct list_head *head = &sdp->sd_log_le_revoke;
665 struct gfs2_bufdata *bd;
666 struct gfs2_glock *gl;
667
668 while (!list_empty(head)) {
c0752aa7
BP
669 bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
670 list_del_init(&bd->bd_list);
f42ab085
SW
671 gl = bd->bd_gl;
672 atomic_dec(&gl->gl_revokes);
673 clear_bit(GLF_LFLUSH, &gl->gl_flags);
674 kmem_cache_free(gfs2_bufdata_cachep, bd);
675 }
676}
677
b3b94faa 678static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
55167622 679 struct gfs2_log_header_host *head, int pass)
b3b94faa 680{
b3b94faa
DT
681 if (pass != 0)
682 return;
683
a17d758b
BP
684 jd->jd_found_revokes = 0;
685 jd->jd_replay_tail = head->lh_tail;
b3b94faa
DT
686}
687
688static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
689 struct gfs2_log_descriptor *ld, __be64 *ptr,
690 int pass)
691{
feaa7bba 692 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa
DT
693 unsigned int blks = be32_to_cpu(ld->ld_length);
694 unsigned int revokes = be32_to_cpu(ld->ld_data1);
695 struct buffer_head *bh;
696 unsigned int offset;
cd915493 697 u64 blkno;
b3b94faa
DT
698 int first = 1;
699 int error;
700
701 if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
702 return 0;
703
704 offset = sizeof(struct gfs2_log_descriptor);
705
e1cb6be9 706 for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
b3b94faa
DT
707 error = gfs2_replay_read_block(jd, start, &bh);
708 if (error)
709 return error;
710
711 if (!first)
712 gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
713
cd915493 714 while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
b3b94faa
DT
715 blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
716
a17d758b 717 error = gfs2_revoke_add(jd, blkno, start);
3ad62e87
BP
718 if (error < 0) {
719 brelse(bh);
b3b94faa 720 return error;
3ad62e87 721 }
b3b94faa 722 else if (error)
a17d758b 723 jd->jd_found_revokes++;
b3b94faa
DT
724
725 if (!--revokes)
726 break;
cd915493 727 offset += sizeof(u64);
b3b94faa
DT
728 }
729
730 brelse(bh);
731 offset = sizeof(struct gfs2_meta_header);
732 first = 0;
733 }
734
735 return 0;
736}
737
738static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
739{
feaa7bba 740 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa
DT
741
742 if (error) {
a17d758b 743 gfs2_revoke_clean(jd);
b3b94faa
DT
744 return;
745 }
746 if (pass != 1)
747 return;
748
749 fs_info(sdp, "jid=%u: Found %u revoke tags\n",
a17d758b 750 jd->jd_jid, jd->jd_found_revokes);
b3b94faa 751
a17d758b 752 gfs2_revoke_clean(jd);
b3b94faa
DT
753}
754
16615be1
SW
755/**
756 * databuf_lo_before_commit - Scan the data buffers, writing as we go
757 *
758 */
759
d69a3c65 760static void databuf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
16615be1 761{
022ef4fe
SW
762 unsigned int limit = databuf_limit(sdp);
763 unsigned int nbuf;
d69a3c65
SW
764 if (tr == NULL)
765 return;
022ef4fe
SW
766 nbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
767 gfs2_before_commit(sdp, limit, nbuf, &tr->tr_databuf, 1);
18ec7d5c
SW
768}
769
770static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
771 struct gfs2_log_descriptor *ld,
772 __be64 *ptr, int pass)
773{
feaa7bba 774 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
5c676f6d 775 struct gfs2_glock *gl = ip->i_gl;
18ec7d5c
SW
776 unsigned int blks = be32_to_cpu(ld->ld_data1);
777 struct buffer_head *bh_log, *bh_ip;
cd915493
SW
778 u64 blkno;
779 u64 esc;
18ec7d5c
SW
780 int error = 0;
781
782 if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
783 return 0;
784
e1cb6be9
BP
785 gfs2_replay_incr_blk(jd, &start);
786 for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
18ec7d5c
SW
787 blkno = be64_to_cpu(*ptr++);
788 esc = be64_to_cpu(*ptr++);
789
a17d758b 790 jd->jd_found_blocks++;
18ec7d5c 791
a17d758b 792 if (gfs2_revoke_check(jd, blkno, start))
18ec7d5c
SW
793 continue;
794
795 error = gfs2_replay_read_block(jd, start, &bh_log);
796 if (error)
797 return error;
798
799 bh_ip = gfs2_meta_new(gl, blkno);
800 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
801
802 /* Unescape */
803 if (esc) {
804 __be32 *eptr = (__be32 *)bh_ip->b_data;
805 *eptr = cpu_to_be32(GFS2_MAGIC);
806 }
807 mark_buffer_dirty(bh_ip);
808
809 brelse(bh_log);
810 brelse(bh_ip);
18ec7d5c 811
a17d758b 812 jd->jd_replayed_blocks++;
18ec7d5c
SW
813 }
814
815 return error;
816}
817
818/* FIXME: sort out accounting for log blocks etc. */
819
820static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
821{
feaa7bba
SW
822 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
823 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
18ec7d5c
SW
824
825 if (error) {
7276b3b0 826 gfs2_meta_sync(ip->i_gl);
18ec7d5c
SW
827 return;
828 }
829 if (pass != 1)
830 return;
831
832 /* data sync? */
7276b3b0 833 gfs2_meta_sync(ip->i_gl);
18ec7d5c
SW
834
835 fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
a17d758b 836 jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
18ec7d5c
SW
837}
838
16ca9412 839static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
18ec7d5c 840{
d69a3c65 841 struct list_head *head;
18ec7d5c
SW
842 struct gfs2_bufdata *bd;
843
d69a3c65 844 if (tr == NULL)
16ca9412 845 return;
16ca9412 846
d69a3c65 847 head = &tr->tr_databuf;
18ec7d5c 848 while (!list_empty(head)) {
c0752aa7
BP
849 bd = list_entry(head->next, struct gfs2_bufdata, bd_list);
850 list_del_init(&bd->bd_list);
16ca9412 851 gfs2_unpin(sdp, bd->bd_bh, tr);
18ec7d5c 852 }
b3b94faa
DT
853}
854
18ec7d5c 855
b09e593d 856const struct gfs2_log_operations gfs2_buf_lops = {
b3b94faa
DT
857 .lo_before_commit = buf_lo_before_commit,
858 .lo_after_commit = buf_lo_after_commit,
859 .lo_before_scan = buf_lo_before_scan,
860 .lo_scan_elements = buf_lo_scan_elements,
861 .lo_after_scan = buf_lo_after_scan,
ea67eedb 862 .lo_name = "buf",
b3b94faa
DT
863};
864
b09e593d 865const struct gfs2_log_operations gfs2_revoke_lops = {
b3b94faa 866 .lo_before_commit = revoke_lo_before_commit,
f42ab085 867 .lo_after_commit = revoke_lo_after_commit,
b3b94faa
DT
868 .lo_before_scan = revoke_lo_before_scan,
869 .lo_scan_elements = revoke_lo_scan_elements,
870 .lo_after_scan = revoke_lo_after_scan,
ea67eedb 871 .lo_name = "revoke",
b3b94faa
DT
872};
873
b09e593d 874const struct gfs2_log_operations gfs2_databuf_lops = {
b3b94faa 875 .lo_before_commit = databuf_lo_before_commit,
18ec7d5c
SW
876 .lo_after_commit = databuf_lo_after_commit,
877 .lo_scan_elements = databuf_lo_scan_elements,
878 .lo_after_scan = databuf_lo_after_scan,
ea67eedb 879 .lo_name = "databuf",
b3b94faa
DT
880};
881
b09e593d 882const struct gfs2_log_operations *gfs2_log_ops[] = {
16615be1 883 &gfs2_databuf_lops,
b3b94faa 884 &gfs2_buf_lops,
16615be1 885 &gfs2_revoke_lops,
ea67eedb 886 NULL,
b3b94faa
DT
887};
888