]>
Commit | Line | Data |
---|---|---|
7336d0e6 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
b3b94faa DT |
2 | /* |
3 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 4 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
5 | */ |
6 | ||
7 | #include <linux/sched.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/spinlock.h> | |
10 | #include <linux/completion.h> | |
11 | #include <linux/buffer_head.h> | |
75ca61c1 | 12 | #include <linux/mempool.h> |
5c676f6d | 13 | #include <linux/gfs2_ondisk.h> |
c969f58c SW |
14 | #include <linux/bio.h> |
15 | #include <linux/fs.h> | |
7f63257d | 16 | #include <linux/list_sort.h> |
f4686c26 | 17 | #include <linux/blkdev.h> |
b3b94faa | 18 | |
f4686c26 | 19 | #include "bmap.h" |
c1696fb8 | 20 | #include "dir.h" |
b3b94faa | 21 | #include "gfs2.h" |
5c676f6d | 22 | #include "incore.h" |
2332c443 | 23 | #include "inode.h" |
b3b94faa | 24 | #include "glock.h" |
4a55752a | 25 | #include "glops.h" |
b3b94faa DT |
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 | 42 | void 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 |
66 | static bool buffer_is_rgrp(const struct gfs2_bufdata *bd) |
67 | { | |
68 | return bd->bd_gl->gl_name.ln_type == LM_TYPE_RGRP; | |
69 | } | |
70 | ||
71 | static 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 | ||
9e514605 | 79 | rgrp_lock_local(rgd); |
5a7c6690 | 80 | if (bi->bi_clone == NULL) |
9e514605 | 81 | goto out; |
7c9ca621 | 82 | if (sdp->sd_args.ar_discard) |
66fc061b | 83 | gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL); |
7c9ca621 | 84 | memcpy(bi->bi_clone + bi->bi_offset, |
281b4952 | 85 | bd->bd_bh->b_data + bi->bi_offset, bi->bi_bytes); |
7c9ca621 BP |
86 | clear_bit(GBF_FULL, &bi->bi_flags); |
87 | rgd->rd_free_clone = rgd->rd_free; | |
725d0e9d | 88 | BUG_ON(rgd->rd_free_clone < rgd->rd_reserved); |
5ea5050c | 89 | rgd->rd_extfail_pt = rgd->rd_free; |
9e514605 AG |
90 | |
91 | out: | |
92 | rgrp_unlock_local(rgd); | |
7c9ca621 BP |
93 | } |
94 | ||
9b9107a5 SW |
95 | /** |
96 | * gfs2_unpin - Unpin a buffer | |
97 | * @sdp: the filesystem the buffer belongs to | |
98 | * @bh: The buffer to unpin | |
c551f66c | 99 | * @tr: The system transaction being flushed |
9b9107a5 SW |
100 | */ |
101 | ||
102 | static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, | |
16ca9412 | 103 | struct gfs2_trans *tr) |
9b9107a5 SW |
104 | { |
105 | struct gfs2_bufdata *bd = bh->b_private; | |
106 | ||
29687a2a SW |
107 | BUG_ON(!buffer_uptodate(bh)); |
108 | BUG_ON(!buffer_pinned(bh)); | |
9b9107a5 SW |
109 | |
110 | lock_buffer(bh); | |
111 | mark_buffer_dirty(bh); | |
112 | clear_buffer_pinned(bh); | |
113 | ||
7c9ca621 BP |
114 | if (buffer_is_rgrp(bd)) |
115 | maybe_release_space(bd); | |
116 | ||
d6a079e8 | 117 | spin_lock(&sdp->sd_ail_lock); |
16ca9412 | 118 | if (bd->bd_tr) { |
9b9107a5 SW |
119 | list_del(&bd->bd_ail_st_list); |
120 | brelse(bh); | |
121 | } else { | |
122 | struct gfs2_glock *gl = bd->bd_gl; | |
123 | list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list); | |
124 | atomic_inc(&gl->gl_ail_count); | |
125 | } | |
16ca9412 BM |
126 | bd->bd_tr = tr; |
127 | list_add(&bd->bd_ail_st_list, &tr->tr_ail1_list); | |
d6a079e8 DC |
128 | spin_unlock(&sdp->sd_ail_lock); |
129 | ||
29687a2a | 130 | clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); |
63997775 | 131 | trace_gfs2_pin(bd, 0); |
9b9107a5 | 132 | unlock_buffer(bh); |
5e687eac | 133 | atomic_dec(&sdp->sd_log_pinned); |
9b9107a5 SW |
134 | } |
135 | ||
19ebc050 | 136 | void gfs2_log_incr_head(struct gfs2_sbd *sdp) |
16615be1 | 137 | { |
e8c92ed7 SW |
138 | BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) && |
139 | (sdp->sd_log_flush_head != sdp->sd_log_head)); | |
140 | ||
722f6f62 | 141 | if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) |
e8c92ed7 | 142 | sdp->sd_log_flush_head = 0; |
16615be1 SW |
143 | } |
144 | ||
19ebc050 | 145 | u64 gfs2_log_bmap(struct gfs2_jdesc *jd, unsigned int lblock) |
16615be1 | 146 | { |
e8c92ed7 | 147 | struct gfs2_journal_extent *je; |
e8c92ed7 | 148 | |
19ebc050 AG |
149 | list_for_each_entry(je, &jd->extent_list, list) { |
150 | if (lblock >= je->lblock && lblock < je->lblock + je->blocks) | |
151 | return je->dblock + lblock - je->lblock; | |
e8c92ed7 SW |
152 | } |
153 | ||
154 | return -1; | |
16615be1 SW |
155 | } |
156 | ||
e8c92ed7 SW |
157 | /** |
158 | * gfs2_end_log_write_bh - end log write of pagecache data with buffers | |
159 | * @sdp: The superblock | |
160 | * @bvec: The bio_vec | |
161 | * @error: The i/o status | |
162 | * | |
4519eaad | 163 | * This finds the relevant buffers and unlocks them and sets the |
e8c92ed7 SW |
164 | * error flag according to the status of the i/o request. This is |
165 | * used when the log is writing data which has an in-place version | |
166 | * that is pinned in the pagecache. | |
167 | */ | |
168 | ||
6dc4f100 ML |
169 | static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp, |
170 | struct bio_vec *bvec, | |
4e4cbee9 | 171 | blk_status_t error) |
16615be1 | 172 | { |
e8c92ed7 SW |
173 | struct buffer_head *bh, *next; |
174 | struct page *page = bvec->bv_page; | |
175 | unsigned size; | |
176 | ||
177 | bh = page_buffers(page); | |
178 | size = bvec->bv_len; | |
179 | while (bh_offset(bh) < bvec->bv_offset) | |
180 | bh = bh->b_this_page; | |
181 | do { | |
182 | if (error) | |
87354e5d | 183 | mark_buffer_write_io_error(bh); |
e8c92ed7 SW |
184 | unlock_buffer(bh); |
185 | next = bh->b_this_page; | |
186 | size -= bh->b_size; | |
187 | brelse(bh); | |
188 | bh = next; | |
189 | } while(bh && size); | |
16615be1 SW |
190 | } |
191 | ||
47ac5537 | 192 | /** |
e8c92ed7 SW |
193 | * gfs2_end_log_write - end of i/o to the log |
194 | * @bio: The bio | |
e8c92ed7 SW |
195 | * |
196 | * Each bio_vec contains either data from the pagecache or data | |
197 | * relating to the log itself. Here we iterate over the bio_vec | |
198 | * array, processing both kinds of data. | |
47ac5537 SW |
199 | * |
200 | */ | |
201 | ||
4246a0b6 | 202 | static void gfs2_end_log_write(struct bio *bio) |
47ac5537 | 203 | { |
e8c92ed7 SW |
204 | struct gfs2_sbd *sdp = bio->bi_private; |
205 | struct bio_vec *bvec; | |
206 | struct page *page; | |
6dc4f100 | 207 | struct bvec_iter_all iter_all; |
e8c92ed7 | 208 | |
942b0cdd | 209 | if (bio->bi_status) { |
f34a6135 BP |
210 | if (!cmpxchg(&sdp->sd_log_error, 0, (int)bio->bi_status)) |
211 | fs_err(sdp, "Error %d writing to journal, jid=%u\n", | |
212 | bio->bi_status, sdp->sd_jdesc->jd_jid); | |
f05b86db BP |
213 | gfs2_withdraw_delayed(sdp); |
214 | /* prevent more writes to the journal */ | |
215 | clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); | |
942b0cdd BP |
216 | wake_up(&sdp->sd_logd_waitq); |
217 | } | |
e8c92ed7 | 218 | |
2b070cfe | 219 | bio_for_each_segment_all(bvec, bio, 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 | |
f4686c26 | 235 | * @opf: REQ_OP | op_flags |
47ac5537 | 236 | * |
e8c92ed7 SW |
237 | * Submit any pending part-built or full bio to the block device. If |
238 | * there is no pending bio, then this is a no-op. | |
47ac5537 SW |
239 | */ |
240 | ||
67688c08 | 241 | void gfs2_log_submit_bio(struct bio **biop, blk_opf_t opf) |
47ac5537 | 242 | { |
5b846095 AD |
243 | struct bio *bio = *biop; |
244 | if (bio) { | |
245 | struct gfs2_sbd *sdp = bio->bi_private; | |
e8c92ed7 | 246 | atomic_inc(&sdp->sd_log_in_flight); |
f4686c26 | 247 | bio->bi_opf = opf; |
5b846095 AD |
248 | submit_bio(bio); |
249 | *biop = NULL; | |
e8c92ed7 SW |
250 | } |
251 | } | |
47ac5537 | 252 | |
e8c92ed7 | 253 | /** |
5b846095 AD |
254 | * gfs2_log_alloc_bio - Allocate a bio |
255 | * @sdp: The super block | |
256 | * @blkno: The device block number we want to write to | |
257 | * @end_io: The bi_end_io callback | |
e8c92ed7 | 258 | * |
5b846095 | 259 | * Allocate a new bio, initialize it with the given parameters and return it. |
e8c92ed7 | 260 | * |
5b846095 | 261 | * Returns: The newly allocated bio |
e8c92ed7 SW |
262 | */ |
263 | ||
5b846095 AD |
264 | static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno, |
265 | bio_end_io_t *end_io) | |
e8c92ed7 SW |
266 | { |
267 | struct super_block *sb = sdp->sd_vfs; | |
07888c66 | 268 | struct bio *bio = bio_alloc(sb->s_bdev, BIO_MAX_VECS, 0, GFP_NOIO); |
e8c92ed7 | 269 | |
aa83da7f | 270 | bio->bi_iter.bi_sector = blkno << sdp->sd_fsb2bb_shift; |
5b846095 | 271 | bio->bi_end_io = end_io; |
e8c92ed7 SW |
272 | bio->bi_private = sdp; |
273 | ||
e8c92ed7 | 274 | return bio; |
47ac5537 SW |
275 | } |
276 | ||
277 | /** | |
e8c92ed7 | 278 | * gfs2_log_get_bio - Get cached log bio, or allocate a new one |
5b846095 | 279 | * @sdp: The super block |
e8c92ed7 | 280 | * @blkno: The device block number we want to write to |
c551f66c | 281 | * @biop: The bio to get or allocate |
5b846095 AD |
282 | * @op: REQ_OP |
283 | * @end_io: The bi_end_io callback | |
284 | * @flush: Always flush the current bio and allocate a new one? | |
e8c92ed7 SW |
285 | * |
286 | * If there is a cached bio, then if the next block number is sequential | |
287 | * with the previous one, return it, otherwise flush the bio to the | |
5b846095 | 288 | * device. If there is no cached bio, or we just flushed it, then |
e8c92ed7 | 289 | * allocate a new one. |
47ac5537 | 290 | * |
e8c92ed7 | 291 | * Returns: The bio to use for log writes |
47ac5537 SW |
292 | */ |
293 | ||
5b846095 | 294 | static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno, |
67688c08 | 295 | struct bio **biop, enum req_op op, |
5b846095 | 296 | bio_end_io_t *end_io, bool flush) |
47ac5537 | 297 | { |
5b846095 | 298 | struct bio *bio = *biop; |
e8c92ed7 SW |
299 | |
300 | if (bio) { | |
5b846095 AD |
301 | u64 nblk; |
302 | ||
f73a1c7d | 303 | nblk = bio_end_sector(bio); |
e8c92ed7 | 304 | nblk >>= sdp->sd_fsb2bb_shift; |
5b846095 | 305 | if (blkno == nblk && !flush) |
e8c92ed7 | 306 | return bio; |
f4686c26 | 307 | gfs2_log_submit_bio(biop, op); |
e8c92ed7 SW |
308 | } |
309 | ||
5b846095 AD |
310 | *biop = gfs2_log_alloc_bio(sdp, blkno, end_io); |
311 | return *biop; | |
47ac5537 SW |
312 | } |
313 | ||
314 | /** | |
e8c92ed7 | 315 | * gfs2_log_write - write to log |
47ac5537 | 316 | * @sdp: the filesystem |
c551f66c | 317 | * @jd: The journal descriptor |
e8c92ed7 SW |
318 | * @page: the page to write |
319 | * @size: the size of the data to write | |
320 | * @offset: the offset within the page | |
c1696fb8 | 321 | * @blkno: block number of the log entry |
47ac5537 | 322 | * |
e8c92ed7 SW |
323 | * Try and add the page segment to the current bio. If that fails, |
324 | * submit the current bio to the device and create a new one, and | |
325 | * then add the page segment to that. | |
47ac5537 SW |
326 | */ |
327 | ||
82218943 BP |
328 | void gfs2_log_write(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd, |
329 | struct page *page, unsigned size, unsigned offset, | |
330 | u64 blkno) | |
47ac5537 | 331 | { |
e8c92ed7 SW |
332 | struct bio *bio; |
333 | int ret; | |
334 | ||
82218943 | 335 | bio = gfs2_log_get_bio(sdp, blkno, &jd->jd_log_bio, REQ_OP_WRITE, |
5b846095 | 336 | gfs2_end_log_write, false); |
e8c92ed7 SW |
337 | ret = bio_add_page(bio, page, size, offset); |
338 | if (ret == 0) { | |
82218943 | 339 | bio = gfs2_log_get_bio(sdp, blkno, &jd->jd_log_bio, |
5b846095 | 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 | ||
356 | static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh) | |
357 | { | |
19ebc050 AG |
358 | u64 dblock; |
359 | ||
360 | dblock = gfs2_log_bmap(sdp->sd_jdesc, sdp->sd_log_flush_head); | |
361 | gfs2_log_incr_head(sdp); | |
82218943 BP |
362 | gfs2_log_write(sdp, sdp->sd_jdesc, bh->b_page, bh->b_size, |
363 | bh_offset(bh), dblock); | |
e8c92ed7 | 364 | } |
47ac5537 | 365 | |
e8c92ed7 SW |
366 | /** |
367 | * gfs2_log_write_page - write one block stored in a page, into the log | |
368 | * @sdp: The superblock | |
369 | * @page: The struct page | |
370 | * | |
371 | * This writes the first block-sized part of the page into the log. Note | |
372 | * that the page must have been allocated from the gfs2_page_pool mempool | |
373 | * and that after this has been called, ownership has been transferred and | |
374 | * the page may be freed at any time. | |
375 | */ | |
47ac5537 | 376 | |
2a6fe26c | 377 | static void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page) |
e8c92ed7 SW |
378 | { |
379 | struct super_block *sb = sdp->sd_vfs; | |
19ebc050 AG |
380 | u64 dblock; |
381 | ||
382 | dblock = gfs2_log_bmap(sdp->sd_jdesc, sdp->sd_log_flush_head); | |
383 | gfs2_log_incr_head(sdp); | |
82218943 | 384 | gfs2_log_write(sdp, sdp->sd_jdesc, page, sb->s_blocksize, 0, dblock); |
47ac5537 | 385 | } |
16615be1 | 386 | |
f4686c26 AD |
387 | /** |
388 | * gfs2_end_log_read - end I/O callback for reads from the log | |
389 | * @bio: The bio | |
390 | * | |
391 | * Simply unlock the pages in the bio. The main thread will wait on them and | |
392 | * process them in order as necessary. | |
393 | */ | |
f4686c26 AD |
394 | static void gfs2_end_log_read(struct bio *bio) |
395 | { | |
ff7a85af MWO |
396 | int error = blk_status_to_errno(bio->bi_status); |
397 | struct folio_iter fi; | |
f4686c26 | 398 | |
ff7a85af MWO |
399 | bio_for_each_folio_all(fi, bio) { |
400 | /* We're abusing wb_err to get the error to gfs2_find_jhead */ | |
401 | filemap_set_wb_err(fi.folio->mapping, error); | |
402 | folio_end_read(fi.folio, !error); | |
f4686c26 AD |
403 | } |
404 | ||
405 | bio_put(bio); | |
406 | } | |
407 | ||
408 | /** | |
409 | * gfs2_jhead_pg_srch - Look for the journal head in a given page. | |
410 | * @jd: The journal descriptor | |
c551f66c | 411 | * @head: The journal head to start from |
f4686c26 AD |
412 | * @page: The page to look in |
413 | * | |
414 | * Returns: 1 if found, 0 otherwise. | |
415 | */ | |
416 | ||
417 | static bool gfs2_jhead_pg_srch(struct gfs2_jdesc *jd, | |
418 | struct gfs2_log_header_host *head, | |
419 | struct page *page) | |
420 | { | |
421 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
3f649ab7 | 422 | struct gfs2_log_header_host lh; |
b0c21c6d | 423 | void *kaddr; |
f4686c26 AD |
424 | unsigned int offset; |
425 | bool ret = false; | |
426 | ||
b0c21c6d | 427 | kaddr = kmap_local_page(page); |
f4686c26 AD |
428 | for (offset = 0; offset < PAGE_SIZE; offset += sdp->sd_sb.sb_bsize) { |
429 | if (!__get_log_header(sdp, kaddr + offset, 0, &lh)) { | |
7582026f | 430 | if (lh.lh_sequence >= head->lh_sequence) |
f4686c26 AD |
431 | *head = lh; |
432 | else { | |
433 | ret = true; | |
434 | break; | |
435 | } | |
436 | } | |
437 | } | |
b0c21c6d | 438 | kunmap_local(kaddr); |
f4686c26 AD |
439 | return ret; |
440 | } | |
441 | ||
442 | /** | |
443 | * gfs2_jhead_process_page - Search/cleanup a page | |
444 | * @jd: The journal descriptor | |
445 | * @index: Index of the page to look into | |
c551f66c | 446 | * @head: The journal head to start from |
f4686c26 AD |
447 | * @done: If set, perform only cleanup, else search and set if found. |
448 | * | |
24015907 | 449 | * Find the folio with 'index' in the journal's mapping. Search the folio for |
f4686c26 | 450 | * the journal head if requested (cleanup == false). Release refs on the |
24015907 | 451 | * folio so the page cache can reclaim it. We grabbed a |
111c7d27 | 452 | * reference on this folio twice, first when we did a grab_cache_page() |
24015907 MWO |
453 | * to obtain the folio to add it to the bio and second when we do a |
454 | * filemap_get_folio() here to get the folio to wait on while I/O on it is being | |
f4686c26 | 455 | * completed. |
24015907 | 456 | * This function is also used to free up a folio we might've grabbed but not |
f4686c26 AD |
457 | * used. Maybe we added it to a bio, but not submitted it for I/O. Or we |
458 | * submitted the I/O, but we already found the jhead so we only need to drop | |
24015907 | 459 | * our references to the folio. |
f4686c26 AD |
460 | */ |
461 | ||
462 | static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index, | |
463 | struct gfs2_log_header_host *head, | |
464 | bool *done) | |
465 | { | |
24015907 | 466 | struct folio *folio; |
f4686c26 | 467 | |
24015907 | 468 | folio = filemap_get_folio(jd->jd_inode->i_mapping, index); |
f4686c26 | 469 | |
24015907 | 470 | folio_wait_locked(folio); |
ff7a85af | 471 | if (!folio_test_uptodate(folio)) |
f4686c26 AD |
472 | *done = true; |
473 | ||
474 | if (!*done) | |
24015907 | 475 | *done = gfs2_jhead_pg_srch(jd, head, &folio->page); |
f4686c26 | 476 | |
111c7d27 | 477 | /* filemap_get_folio() and the earlier grab_cache_page() */ |
24015907 | 478 | folio_put_refs(folio, 2); |
f4686c26 AD |
479 | } |
480 | ||
eed0f953 AG |
481 | static struct bio *gfs2_chain_bio(struct bio *prev, unsigned int nr_iovecs) |
482 | { | |
483 | struct bio *new; | |
484 | ||
07888c66 CH |
485 | new = bio_alloc(prev->bi_bdev, nr_iovecs, prev->bi_opf, GFP_NOIO); |
486 | bio_clone_blkg_association(new, prev); | |
eed0f953 | 487 | new->bi_iter.bi_sector = bio_end_sector(prev); |
eed0f953 AG |
488 | bio_chain(new, prev); |
489 | submit_bio(prev); | |
490 | return new; | |
491 | } | |
492 | ||
f4686c26 AD |
493 | /** |
494 | * gfs2_find_jhead - find the head of a log | |
495 | * @jd: The journal descriptor | |
496 | * @head: The log descriptor for the head of the log is returned here | |
c551f66c | 497 | * @keep_cache: If set inode pages will not be truncated |
f4686c26 AD |
498 | * |
499 | * Do a search of a journal by reading it in large chunks using bios and find | |
500 | * the valid log entry with the highest sequence number. (i.e. the log head) | |
501 | * | |
502 | * Returns: 0 on success, errno otherwise | |
503 | */ | |
504 | int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head, | |
505 | bool keep_cache) | |
506 | { | |
507 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
508 | struct address_space *mapping = jd->jd_inode->i_mapping; | |
509 | unsigned int block = 0, blocks_submitted = 0, blocks_read = 0; | |
eed0f953 | 510 | unsigned int bsize = sdp->sd_sb.sb_bsize, off; |
f4686c26 AD |
511 | unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift; |
512 | unsigned int shift = PAGE_SHIFT - bsize_shift; | |
20be493b | 513 | unsigned int max_blocks = 2 * 1024 * 1024 >> bsize_shift; |
f4686c26 AD |
514 | struct gfs2_journal_extent *je; |
515 | int sz, ret = 0; | |
516 | struct bio *bio = NULL; | |
517 | struct page *page = NULL; | |
20be493b | 518 | bool done = false; |
f4686c26 AD |
519 | errseq_t since; |
520 | ||
521 | memset(head, 0, sizeof(*head)); | |
522 | if (list_empty(&jd->extent_list)) | |
523 | gfs2_map_journal_extents(sdp, jd); | |
524 | ||
525 | since = filemap_sample_wb_err(mapping); | |
526 | list_for_each_entry(je, &jd->extent_list, list) { | |
eed0f953 | 527 | u64 dblock = je->dblock; |
f4686c26 | 528 | |
eed0f953 | 529 | for (; block < je->lblock + je->blocks; block++, dblock++) { |
f4686c26 | 530 | if (!page) { |
111c7d27 | 531 | page = grab_cache_page(mapping, block >> shift); |
f4686c26 AD |
532 | if (!page) { |
533 | ret = -ENOMEM; | |
534 | done = true; | |
535 | goto out; | |
536 | } | |
eed0f953 | 537 | off = 0; |
f4686c26 AD |
538 | } |
539 | ||
20be493b | 540 | if (bio && (off || block < blocks_submitted + max_blocks)) { |
aa83da7f AG |
541 | sector_t sector = dblock << sdp->sd_fsb2bb_shift; |
542 | ||
543 | if (bio_end_sector(bio) == sector) { | |
544 | sz = bio_add_page(bio, page, bsize, off); | |
545 | if (sz == bsize) | |
546 | goto block_added; | |
547 | } | |
eed0f953 AG |
548 | if (off) { |
549 | unsigned int blocks = | |
550 | (PAGE_SIZE - off) >> bsize_shift; | |
551 | ||
552 | bio = gfs2_chain_bio(bio, blocks); | |
eed0f953 | 553 | goto add_block_to_new_bio; |
f4686c26 | 554 | } |
eed0f953 AG |
555 | } |
556 | ||
557 | if (bio) { | |
20be493b | 558 | blocks_submitted = block; |
f4686c26 | 559 | submit_bio(bio); |
f4686c26 AD |
560 | } |
561 | ||
f4686c26 AD |
562 | bio = gfs2_log_alloc_bio(sdp, dblock, gfs2_end_log_read); |
563 | bio->bi_opf = REQ_OP_READ; | |
eed0f953 AG |
564 | add_block_to_new_bio: |
565 | sz = bio_add_page(bio, page, bsize, off); | |
566 | BUG_ON(sz != bsize); | |
567 | block_added: | |
568 | off += bsize; | |
569 | if (off == PAGE_SIZE) | |
f4686c26 | 570 | page = NULL; |
20be493b | 571 | if (blocks_submitted <= blocks_read + max_blocks) { |
f4686c26 AD |
572 | /* Keep at least one bio in flight */ |
573 | continue; | |
574 | } | |
575 | ||
576 | gfs2_jhead_process_page(jd, blocks_read >> shift, head, &done); | |
577 | blocks_read += PAGE_SIZE >> bsize_shift; | |
578 | if (done) | |
579 | goto out; /* found */ | |
580 | } | |
581 | } | |
582 | ||
583 | out: | |
584 | if (bio) | |
585 | submit_bio(bio); | |
586 | while (blocks_read < block) { | |
587 | gfs2_jhead_process_page(jd, blocks_read >> shift, head, &done); | |
588 | blocks_read += PAGE_SIZE >> bsize_shift; | |
589 | } | |
590 | ||
591 | if (!ret) | |
592 | ret = filemap_check_wb_err(mapping, since); | |
593 | ||
594 | if (!keep_cache) | |
595 | truncate_inode_pages(mapping, 0); | |
596 | ||
597 | return ret; | |
598 | } | |
599 | ||
dad30e90 SW |
600 | static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type, |
601 | u32 ld_length, u32 ld_data1) | |
16615be1 | 602 | { |
144a4c2f | 603 | struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO); |
e8c92ed7 SW |
604 | struct gfs2_log_descriptor *ld = page_address(page); |
605 | clear_page(ld); | |
16615be1 SW |
606 | ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC); |
607 | ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD); | |
608 | ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD); | |
609 | ld->ld_type = cpu_to_be32(ld_type); | |
dad30e90 SW |
610 | ld->ld_length = cpu_to_be32(ld_length); |
611 | ld->ld_data1 = cpu_to_be32(ld_data1); | |
16615be1 | 612 | ld->ld_data2 = 0; |
e8c92ed7 | 613 | return page; |
16615be1 SW |
614 | } |
615 | ||
dad30e90 SW |
616 | static void gfs2_check_magic(struct buffer_head *bh) |
617 | { | |
618 | void *kaddr; | |
619 | __be32 *ptr; | |
620 | ||
621 | clear_buffer_escaped(bh); | |
b0c21c6d | 622 | kaddr = kmap_local_page(bh->b_page); |
dad30e90 SW |
623 | ptr = kaddr + bh_offset(bh); |
624 | if (*ptr == cpu_to_be32(GFS2_MAGIC)) | |
625 | set_buffer_escaped(bh); | |
b0c21c6d | 626 | kunmap_local(kaddr); |
dad30e90 SW |
627 | } |
628 | ||
4f0f586b ST |
629 | static int blocknr_cmp(void *priv, const struct list_head *a, |
630 | const struct list_head *b) | |
7f63257d BM |
631 | { |
632 | struct gfs2_bufdata *bda, *bdb; | |
633 | ||
634 | bda = list_entry(a, struct gfs2_bufdata, bd_list); | |
635 | bdb = list_entry(b, struct gfs2_bufdata, bd_list); | |
636 | ||
637 | if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr) | |
638 | return -1; | |
639 | if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr) | |
640 | return 1; | |
641 | return 0; | |
642 | } | |
643 | ||
dad30e90 SW |
644 | static void gfs2_before_commit(struct gfs2_sbd *sdp, unsigned int limit, |
645 | unsigned int total, struct list_head *blist, | |
646 | bool is_databuf) | |
b3b94faa | 647 | { |
b3b94faa DT |
648 | struct gfs2_log_descriptor *ld; |
649 | struct gfs2_bufdata *bd1 = NULL, *bd2; | |
e8c92ed7 | 650 | struct page *page; |
b3b94faa DT |
651 | unsigned int num; |
652 | unsigned n; | |
653 | __be64 *ptr; | |
654 | ||
905d2aef | 655 | gfs2_log_lock(sdp); |
7f63257d | 656 | list_sort(NULL, blist, blocknr_cmp); |
c0752aa7 | 657 | bd1 = bd2 = list_prepare_entry(bd1, blist, bd_list); |
b3b94faa DT |
658 | while(total) { |
659 | num = total; | |
660 | if (total > limit) | |
661 | num = limit; | |
905d2aef | 662 | gfs2_log_unlock(sdp); |
4a586812 BP |
663 | page = gfs2_get_log_desc(sdp, |
664 | is_databuf ? GFS2_LOG_DESC_JDATA : | |
665 | GFS2_LOG_DESC_METADATA, num + 1, num); | |
e8c92ed7 | 666 | ld = page_address(page); |
905d2aef | 667 | gfs2_log_lock(sdp); |
e8c92ed7 | 668 | ptr = (__be64 *)(ld + 1); |
b3b94faa DT |
669 | |
670 | n = 0; | |
c0752aa7 | 671 | list_for_each_entry_continue(bd1, blist, bd_list) { |
b3b94faa | 672 | *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr); |
dad30e90 SW |
673 | if (is_databuf) { |
674 | gfs2_check_magic(bd1->bd_bh); | |
675 | *ptr++ = cpu_to_be64(buffer_escaped(bd1->bd_bh) ? 1 : 0); | |
676 | } | |
b3b94faa DT |
677 | if (++n >= num) |
678 | break; | |
679 | } | |
680 | ||
905d2aef | 681 | gfs2_log_unlock(sdp); |
e8c92ed7 | 682 | gfs2_log_write_page(sdp, page); |
905d2aef | 683 | gfs2_log_lock(sdp); |
b3b94faa DT |
684 | |
685 | n = 0; | |
c0752aa7 | 686 | list_for_each_entry_continue(bd2, blist, bd_list) { |
16615be1 | 687 | get_bh(bd2->bd_bh); |
905d2aef | 688 | gfs2_log_unlock(sdp); |
16615be1 | 689 | lock_buffer(bd2->bd_bh); |
dad30e90 SW |
690 | |
691 | if (buffer_escaped(bd2->bd_bh)) { | |
d68d0c6c AG |
692 | void *p; |
693 | ||
dad30e90 | 694 | page = mempool_alloc(gfs2_page_pool, GFP_NOIO); |
d68d0c6c AG |
695 | p = page_address(page); |
696 | memcpy_from_page(p, page, bh_offset(bd2->bd_bh), bd2->bd_bh->b_size); | |
697 | *(__be32 *)p = 0; | |
dad30e90 SW |
698 | clear_buffer_escaped(bd2->bd_bh); |
699 | unlock_buffer(bd2->bd_bh); | |
700 | brelse(bd2->bd_bh); | |
701 | gfs2_log_write_page(sdp, page); | |
702 | } else { | |
703 | gfs2_log_write_bh(sdp, bd2->bd_bh); | |
704 | } | |
905d2aef | 705 | gfs2_log_lock(sdp); |
b3b94faa DT |
706 | if (++n >= num) |
707 | break; | |
708 | } | |
709 | ||
905d2aef | 710 | BUG_ON(total < num); |
b3b94faa DT |
711 | total -= num; |
712 | } | |
905d2aef | 713 | gfs2_log_unlock(sdp); |
b3b94faa DT |
714 | } |
715 | ||
d69a3c65 | 716 | static void buf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
dad30e90 SW |
717 | { |
718 | unsigned int limit = buf_limit(sdp); /* 503 for 4k blocks */ | |
022ef4fe | 719 | unsigned int nbuf; |
d69a3c65 SW |
720 | if (tr == NULL) |
721 | return; | |
022ef4fe SW |
722 | nbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm; |
723 | gfs2_before_commit(sdp, limit, nbuf, &tr->tr_buf, 0); | |
dad30e90 SW |
724 | } |
725 | ||
16ca9412 | 726 | static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
b3b94faa | 727 | { |
d69a3c65 | 728 | struct list_head *head; |
b3b94faa DT |
729 | struct gfs2_bufdata *bd; |
730 | ||
d69a3c65 | 731 | if (tr == NULL) |
16ca9412 | 732 | return; |
16ca9412 | 733 | |
d69a3c65 | 734 | head = &tr->tr_buf; |
b3b94faa | 735 | while (!list_empty(head)) { |
969183bc | 736 | bd = list_first_entry(head, struct gfs2_bufdata, bd_list); |
c0752aa7 | 737 | list_del_init(&bd->bd_list); |
16ca9412 | 738 | gfs2_unpin(sdp, bd->bd_bh, tr); |
b3b94faa | 739 | } |
b3b94faa DT |
740 | } |
741 | ||
742 | static void buf_lo_before_scan(struct gfs2_jdesc *jd, | |
55167622 | 743 | struct gfs2_log_header_host *head, int pass) |
b3b94faa | 744 | { |
b3b94faa DT |
745 | if (pass != 0) |
746 | return; | |
747 | ||
a17d758b BP |
748 | jd->jd_found_blocks = 0; |
749 | jd->jd_replayed_blocks = 0; | |
b3b94faa DT |
750 | } |
751 | ||
c37453cb BP |
752 | #define obsolete_rgrp_replay \ |
753 | "Replaying 0x%llx from jid=%d/0x%llx but we already have a bh!\n" | |
754 | #define obsolete_rgrp_replay2 \ | |
755 | "busy:%d, pinned:%d rg_gen:0x%llx, j_gen:0x%llx\n" | |
756 | ||
757 | static void obsolete_rgrp(struct gfs2_jdesc *jd, struct buffer_head *bh_log, | |
758 | u64 blkno) | |
759 | { | |
760 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
761 | struct gfs2_rgrpd *rgd; | |
762 | struct gfs2_rgrp *jrgd = (struct gfs2_rgrp *)bh_log->b_data; | |
763 | ||
764 | rgd = gfs2_blk2rgrpd(sdp, blkno, false); | |
765 | if (rgd && rgd->rd_addr == blkno && | |
766 | rgd->rd_bits && rgd->rd_bits->bi_bh) { | |
767 | fs_info(sdp, obsolete_rgrp_replay, (unsigned long long)blkno, | |
768 | jd->jd_jid, bh_log->b_blocknr); | |
769 | fs_info(sdp, obsolete_rgrp_replay2, | |
770 | buffer_busy(rgd->rd_bits->bi_bh) ? 1 : 0, | |
771 | buffer_pinned(rgd->rd_bits->bi_bh), | |
772 | rgd->rd_igeneration, | |
773 | be64_to_cpu(jrgd->rg_igeneration)); | |
774 | gfs2_dump_glock(NULL, rgd->rd_gl, true); | |
775 | } | |
776 | } | |
777 | ||
7c70b896 | 778 | static int buf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start, |
b3b94faa DT |
779 | struct gfs2_log_descriptor *ld, __be64 *ptr, |
780 | int pass) | |
781 | { | |
feaa7bba SW |
782 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
783 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
5c676f6d | 784 | struct gfs2_glock *gl = ip->i_gl; |
b3b94faa DT |
785 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
786 | struct buffer_head *bh_log, *bh_ip; | |
cd915493 | 787 | u64 blkno; |
b3b94faa DT |
788 | int error = 0; |
789 | ||
790 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA) | |
791 | return 0; | |
792 | ||
e1cb6be9 | 793 | gfs2_replay_incr_blk(jd, &start); |
b3b94faa | 794 | |
e1cb6be9 | 795 | for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) { |
b3b94faa DT |
796 | blkno = be64_to_cpu(*ptr++); |
797 | ||
a17d758b | 798 | jd->jd_found_blocks++; |
b3b94faa | 799 | |
a17d758b | 800 | if (gfs2_revoke_check(jd, blkno, start)) |
b3b94faa DT |
801 | continue; |
802 | ||
803 | error = gfs2_replay_read_block(jd, start, &bh_log); | |
82ffa516 SW |
804 | if (error) |
805 | return error; | |
b3b94faa DT |
806 | |
807 | bh_ip = gfs2_meta_new(gl, blkno); | |
808 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); | |
809 | ||
810 | if (gfs2_meta_check(sdp, bh_ip)) | |
811 | error = -EIO; | |
d14e1ca3 BP |
812 | else { |
813 | struct gfs2_meta_header *mh = | |
814 | (struct gfs2_meta_header *)bh_ip->b_data; | |
815 | ||
c37453cb BP |
816 | if (mh->mh_type == cpu_to_be32(GFS2_METATYPE_RG)) |
817 | obsolete_rgrp(jd, bh_log, blkno); | |
818 | ||
b3b94faa | 819 | mark_buffer_dirty(bh_ip); |
d14e1ca3 | 820 | } |
b3b94faa DT |
821 | brelse(bh_log); |
822 | brelse(bh_ip); | |
823 | ||
824 | if (error) | |
825 | break; | |
826 | ||
a17d758b | 827 | jd->jd_replayed_blocks++; |
b3b94faa DT |
828 | } |
829 | ||
830 | return error; | |
831 | } | |
832 | ||
833 | static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
834 | { | |
feaa7bba SW |
835 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
836 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
b3b94faa DT |
837 | |
838 | if (error) { | |
4a55752a | 839 | gfs2_inode_metasync(ip->i_gl); |
b3b94faa DT |
840 | return; |
841 | } | |
842 | if (pass != 1) | |
843 | return; | |
844 | ||
4a55752a | 845 | gfs2_inode_metasync(ip->i_gl); |
b3b94faa DT |
846 | |
847 | fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n", | |
a17d758b | 848 | jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks); |
b3b94faa DT |
849 | } |
850 | ||
d69a3c65 | 851 | static void revoke_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
b3b94faa | 852 | { |
b3b94faa | 853 | struct gfs2_meta_header *mh; |
b3b94faa | 854 | unsigned int offset; |
a5b1d3fc | 855 | struct list_head *head = &sdp->sd_log_revokes; |
82e86087 | 856 | struct gfs2_bufdata *bd; |
e8c92ed7 | 857 | struct page *page; |
dad30e90 | 858 | unsigned int length; |
b3b94faa | 859 | |
e7501bf8 | 860 | gfs2_flush_revokes(sdp); |
b3b94faa DT |
861 | if (!sdp->sd_log_num_revoke) |
862 | return; | |
863 | ||
2e9eeaa1 | 864 | length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke); |
dad30e90 | 865 | page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke); |
b3b94faa DT |
866 | offset = sizeof(struct gfs2_log_descriptor); |
867 | ||
c0752aa7 | 868 | list_for_each_entry(bd, head, bd_list) { |
b3b94faa DT |
869 | sdp->sd_log_num_revoke--; |
870 | ||
cd915493 | 871 | if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) { |
e8c92ed7 SW |
872 | gfs2_log_write_page(sdp, page); |
873 | page = mempool_alloc(gfs2_page_pool, GFP_NOIO); | |
874 | mh = page_address(page); | |
875 | clear_page(mh); | |
b3b94faa | 876 | mh->mh_magic = cpu_to_be32(GFS2_MAGIC); |
e3167ded SW |
877 | mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB); |
878 | mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB); | |
b3b94faa DT |
879 | offset = sizeof(struct gfs2_meta_header); |
880 | } | |
881 | ||
e8c92ed7 | 882 | *(__be64 *)(page_address(page) + offset) = cpu_to_be64(bd->bd_blkno); |
cd915493 | 883 | offset += sizeof(u64); |
b3b94faa DT |
884 | } |
885 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); | |
886 | ||
e8c92ed7 | 887 | gfs2_log_write_page(sdp, page); |
b3b94faa DT |
888 | } |
889 | ||
f5456b5d | 890 | void gfs2_drain_revokes(struct gfs2_sbd *sdp) |
f42ab085 | 891 | { |
a5b1d3fc | 892 | struct list_head *head = &sdp->sd_log_revokes; |
638803d4 BP |
893 | struct gfs2_bufdata *bd; |
894 | struct gfs2_glock *gl; | |
73118ca8 | 895 | |
638803d4 | 896 | while (!list_empty(head)) { |
969183bc | 897 | bd = list_first_entry(head, struct gfs2_bufdata, bd_list); |
638803d4 BP |
898 | list_del_init(&bd->bd_list); |
899 | gl = bd->bd_gl; | |
fe5e7ba1 | 900 | gfs2_glock_remove_revoke(gl); |
f42ab085 SW |
901 | kmem_cache_free(gfs2_bufdata_cachep, bd); |
902 | } | |
903 | } | |
904 | ||
f5456b5d BP |
905 | static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
906 | { | |
907 | gfs2_drain_revokes(sdp); | |
908 | } | |
909 | ||
b3b94faa | 910 | static void revoke_lo_before_scan(struct gfs2_jdesc *jd, |
55167622 | 911 | struct gfs2_log_header_host *head, int pass) |
b3b94faa | 912 | { |
b3b94faa DT |
913 | if (pass != 0) |
914 | return; | |
915 | ||
a17d758b BP |
916 | jd->jd_found_revokes = 0; |
917 | jd->jd_replay_tail = head->lh_tail; | |
b3b94faa DT |
918 | } |
919 | ||
7c70b896 | 920 | static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, u32 start, |
b3b94faa DT |
921 | struct gfs2_log_descriptor *ld, __be64 *ptr, |
922 | int pass) | |
923 | { | |
feaa7bba | 924 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
925 | unsigned int blks = be32_to_cpu(ld->ld_length); |
926 | unsigned int revokes = be32_to_cpu(ld->ld_data1); | |
927 | struct buffer_head *bh; | |
928 | unsigned int offset; | |
cd915493 | 929 | u64 blkno; |
b3b94faa DT |
930 | int first = 1; |
931 | int error; | |
932 | ||
933 | if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE) | |
934 | return 0; | |
935 | ||
936 | offset = sizeof(struct gfs2_log_descriptor); | |
937 | ||
e1cb6be9 | 938 | for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) { |
b3b94faa DT |
939 | error = gfs2_replay_read_block(jd, start, &bh); |
940 | if (error) | |
941 | return error; | |
942 | ||
943 | if (!first) | |
944 | gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB); | |
945 | ||
cd915493 | 946 | while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) { |
b3b94faa DT |
947 | blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset)); |
948 | ||
a17d758b | 949 | error = gfs2_revoke_add(jd, blkno, start); |
3ad62e87 BP |
950 | if (error < 0) { |
951 | brelse(bh); | |
b3b94faa | 952 | return error; |
3ad62e87 | 953 | } |
b3b94faa | 954 | else if (error) |
a17d758b | 955 | jd->jd_found_revokes++; |
b3b94faa DT |
956 | |
957 | if (!--revokes) | |
958 | break; | |
cd915493 | 959 | offset += sizeof(u64); |
b3b94faa DT |
960 | } |
961 | ||
962 | brelse(bh); | |
963 | offset = sizeof(struct gfs2_meta_header); | |
964 | first = 0; | |
965 | } | |
966 | ||
967 | return 0; | |
968 | } | |
969 | ||
970 | static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
971 | { | |
feaa7bba | 972 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
b3b94faa DT |
973 | |
974 | if (error) { | |
a17d758b | 975 | gfs2_revoke_clean(jd); |
b3b94faa DT |
976 | return; |
977 | } | |
978 | if (pass != 1) | |
979 | return; | |
980 | ||
981 | fs_info(sdp, "jid=%u: Found %u revoke tags\n", | |
a17d758b | 982 | jd->jd_jid, jd->jd_found_revokes); |
b3b94faa | 983 | |
a17d758b | 984 | gfs2_revoke_clean(jd); |
b3b94faa DT |
985 | } |
986 | ||
16615be1 SW |
987 | /** |
988 | * databuf_lo_before_commit - Scan the data buffers, writing as we go | |
c551f66c LJ |
989 | * @sdp: The filesystem |
990 | * @tr: The system transaction being flushed | |
16615be1 SW |
991 | */ |
992 | ||
d69a3c65 | 993 | static void databuf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
16615be1 | 994 | { |
022ef4fe SW |
995 | unsigned int limit = databuf_limit(sdp); |
996 | unsigned int nbuf; | |
d69a3c65 SW |
997 | if (tr == NULL) |
998 | return; | |
022ef4fe SW |
999 | nbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm; |
1000 | gfs2_before_commit(sdp, limit, nbuf, &tr->tr_databuf, 1); | |
18ec7d5c SW |
1001 | } |
1002 | ||
7c70b896 | 1003 | static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start, |
18ec7d5c SW |
1004 | struct gfs2_log_descriptor *ld, |
1005 | __be64 *ptr, int pass) | |
1006 | { | |
feaa7bba | 1007 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
5c676f6d | 1008 | struct gfs2_glock *gl = ip->i_gl; |
18ec7d5c SW |
1009 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
1010 | struct buffer_head *bh_log, *bh_ip; | |
cd915493 SW |
1011 | u64 blkno; |
1012 | u64 esc; | |
18ec7d5c SW |
1013 | int error = 0; |
1014 | ||
1015 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA) | |
1016 | return 0; | |
1017 | ||
e1cb6be9 BP |
1018 | gfs2_replay_incr_blk(jd, &start); |
1019 | for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) { | |
18ec7d5c SW |
1020 | blkno = be64_to_cpu(*ptr++); |
1021 | esc = be64_to_cpu(*ptr++); | |
1022 | ||
a17d758b | 1023 | jd->jd_found_blocks++; |
18ec7d5c | 1024 | |
a17d758b | 1025 | if (gfs2_revoke_check(jd, blkno, start)) |
18ec7d5c SW |
1026 | continue; |
1027 | ||
1028 | error = gfs2_replay_read_block(jd, start, &bh_log); | |
1029 | if (error) | |
1030 | return error; | |
1031 | ||
1032 | bh_ip = gfs2_meta_new(gl, blkno); | |
1033 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); | |
1034 | ||
1035 | /* Unescape */ | |
1036 | if (esc) { | |
1037 | __be32 *eptr = (__be32 *)bh_ip->b_data; | |
1038 | *eptr = cpu_to_be32(GFS2_MAGIC); | |
1039 | } | |
1040 | mark_buffer_dirty(bh_ip); | |
1041 | ||
1042 | brelse(bh_log); | |
1043 | brelse(bh_ip); | |
18ec7d5c | 1044 | |
a17d758b | 1045 | jd->jd_replayed_blocks++; |
18ec7d5c SW |
1046 | } |
1047 | ||
1048 | return error; | |
1049 | } | |
1050 | ||
1051 | /* FIXME: sort out accounting for log blocks etc. */ | |
1052 | ||
1053 | static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) | |
1054 | { | |
feaa7bba SW |
1055 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
1056 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); | |
18ec7d5c SW |
1057 | |
1058 | if (error) { | |
4a55752a | 1059 | gfs2_inode_metasync(ip->i_gl); |
18ec7d5c SW |
1060 | return; |
1061 | } | |
1062 | if (pass != 1) | |
1063 | return; | |
1064 | ||
1065 | /* data sync? */ | |
4a55752a | 1066 | gfs2_inode_metasync(ip->i_gl); |
18ec7d5c SW |
1067 | |
1068 | fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n", | |
a17d758b | 1069 | jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks); |
18ec7d5c SW |
1070 | } |
1071 | ||
16ca9412 | 1072 | static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
18ec7d5c | 1073 | { |
d69a3c65 | 1074 | struct list_head *head; |
18ec7d5c SW |
1075 | struct gfs2_bufdata *bd; |
1076 | ||
d69a3c65 | 1077 | if (tr == NULL) |
16ca9412 | 1078 | return; |
16ca9412 | 1079 | |
d69a3c65 | 1080 | head = &tr->tr_databuf; |
18ec7d5c | 1081 | while (!list_empty(head)) { |
969183bc | 1082 | bd = list_first_entry(head, struct gfs2_bufdata, bd_list); |
c0752aa7 | 1083 | list_del_init(&bd->bd_list); |
16ca9412 | 1084 | gfs2_unpin(sdp, bd->bd_bh, tr); |
18ec7d5c | 1085 | } |
b3b94faa DT |
1086 | } |
1087 | ||
18ec7d5c | 1088 | |
32ac43f6 | 1089 | static const struct gfs2_log_operations gfs2_buf_lops = { |
b3b94faa DT |
1090 | .lo_before_commit = buf_lo_before_commit, |
1091 | .lo_after_commit = buf_lo_after_commit, | |
1092 | .lo_before_scan = buf_lo_before_scan, | |
1093 | .lo_scan_elements = buf_lo_scan_elements, | |
1094 | .lo_after_scan = buf_lo_after_scan, | |
ea67eedb | 1095 | .lo_name = "buf", |
b3b94faa DT |
1096 | }; |
1097 | ||
32ac43f6 | 1098 | static const struct gfs2_log_operations gfs2_revoke_lops = { |
b3b94faa | 1099 | .lo_before_commit = revoke_lo_before_commit, |
f42ab085 | 1100 | .lo_after_commit = revoke_lo_after_commit, |
b3b94faa DT |
1101 | .lo_before_scan = revoke_lo_before_scan, |
1102 | .lo_scan_elements = revoke_lo_scan_elements, | |
1103 | .lo_after_scan = revoke_lo_after_scan, | |
ea67eedb | 1104 | .lo_name = "revoke", |
b3b94faa DT |
1105 | }; |
1106 | ||
32ac43f6 | 1107 | static const struct gfs2_log_operations gfs2_databuf_lops = { |
b3b94faa | 1108 | .lo_before_commit = databuf_lo_before_commit, |
18ec7d5c SW |
1109 | .lo_after_commit = databuf_lo_after_commit, |
1110 | .lo_scan_elements = databuf_lo_scan_elements, | |
1111 | .lo_after_scan = databuf_lo_after_scan, | |
ea67eedb | 1112 | .lo_name = "databuf", |
b3b94faa DT |
1113 | }; |
1114 | ||
b09e593d | 1115 | const struct gfs2_log_operations *gfs2_log_ops[] = { |
16615be1 | 1116 | &gfs2_databuf_lops, |
b3b94faa | 1117 | &gfs2_buf_lops, |
16615be1 | 1118 | &gfs2_revoke_lops, |
ea67eedb | 1119 | NULL, |
b3b94faa DT |
1120 | }; |
1121 |