| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
|---|
| 3 | 4 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
|---|
| 4 | | - * |
|---|
| 5 | | - * This copyrighted material is made available to anyone wishing to use, |
|---|
| 6 | | - * modify, copy, or redistribute it subject to the terms and conditions |
|---|
| 7 | | - * of the GNU General Public License version 2. |
|---|
| 8 | 5 | */ |
|---|
| 9 | 6 | |
|---|
| 10 | 7 | #include <linux/sched.h> |
|---|
| .. | .. |
|---|
| 17 | 14 | #include <linux/bio.h> |
|---|
| 18 | 15 | #include <linux/fs.h> |
|---|
| 19 | 16 | #include <linux/list_sort.h> |
|---|
| 17 | +#include <linux/blkdev.h> |
|---|
| 20 | 18 | |
|---|
| 19 | +#include "bmap.h" |
|---|
| 21 | 20 | #include "dir.h" |
|---|
| 22 | 21 | #include "gfs2.h" |
|---|
| 23 | 22 | #include "incore.h" |
|---|
| 24 | 23 | #include "inode.h" |
|---|
| 25 | 24 | #include "glock.h" |
|---|
| 25 | +#include "glops.h" |
|---|
| 26 | 26 | #include "log.h" |
|---|
| 27 | 27 | #include "lops.h" |
|---|
| 28 | 28 | #include "meta_io.h" |
|---|
| .. | .. |
|---|
| 81 | 81 | if (sdp->sd_args.ar_discard) |
|---|
| 82 | 82 | gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL); |
|---|
| 83 | 83 | memcpy(bi->bi_clone + bi->bi_offset, |
|---|
| 84 | | - bd->bd_bh->b_data + bi->bi_offset, bi->bi_len); |
|---|
| 84 | + bd->bd_bh->b_data + bi->bi_offset, bi->bi_bytes); |
|---|
| 85 | 85 | clear_bit(GBF_FULL, &bi->bi_flags); |
|---|
| 86 | 86 | rgd->rd_free_clone = rgd->rd_free; |
|---|
| 87 | 87 | rgd->rd_extfail_pt = rgd->rd_free; |
|---|
| .. | .. |
|---|
| 130 | 130 | atomic_dec(&sdp->sd_log_pinned); |
|---|
| 131 | 131 | } |
|---|
| 132 | 132 | |
|---|
| 133 | | -static void gfs2_log_incr_head(struct gfs2_sbd *sdp) |
|---|
| 133 | +void gfs2_log_incr_head(struct gfs2_sbd *sdp) |
|---|
| 134 | 134 | { |
|---|
| 135 | 135 | BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) && |
|---|
| 136 | 136 | (sdp->sd_log_flush_head != sdp->sd_log_head)); |
|---|
| .. | .. |
|---|
| 139 | 139 | sdp->sd_log_flush_head = 0; |
|---|
| 140 | 140 | } |
|---|
| 141 | 141 | |
|---|
| 142 | | -u64 gfs2_log_bmap(struct gfs2_sbd *sdp) |
|---|
| 142 | +u64 gfs2_log_bmap(struct gfs2_jdesc *jd, unsigned int lblock) |
|---|
| 143 | 143 | { |
|---|
| 144 | | - unsigned int lbn = sdp->sd_log_flush_head; |
|---|
| 145 | 144 | struct gfs2_journal_extent *je; |
|---|
| 146 | | - u64 block; |
|---|
| 147 | 145 | |
|---|
| 148 | | - list_for_each_entry(je, &sdp->sd_jdesc->extent_list, list) { |
|---|
| 149 | | - if ((lbn >= je->lblock) && (lbn < (je->lblock + je->blocks))) { |
|---|
| 150 | | - block = je->dblock + lbn - je->lblock; |
|---|
| 151 | | - gfs2_log_incr_head(sdp); |
|---|
| 152 | | - return block; |
|---|
| 153 | | - } |
|---|
| 146 | + list_for_each_entry(je, &jd->extent_list, list) { |
|---|
| 147 | + if (lblock >= je->lblock && lblock < je->lblock + je->blocks) |
|---|
| 148 | + return je->dblock + lblock - je->lblock; |
|---|
| 154 | 149 | } |
|---|
| 155 | 150 | |
|---|
| 156 | 151 | return -1; |
|---|
| .. | .. |
|---|
| 168 | 163 | * that is pinned in the pagecache. |
|---|
| 169 | 164 | */ |
|---|
| 170 | 165 | |
|---|
| 171 | | -static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp, struct bio_vec *bvec, |
|---|
| 166 | +static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp, |
|---|
| 167 | + struct bio_vec *bvec, |
|---|
| 172 | 168 | blk_status_t error) |
|---|
| 173 | 169 | { |
|---|
| 174 | 170 | struct buffer_head *bh, *next; |
|---|
| .. | .. |
|---|
| 193 | 189 | /** |
|---|
| 194 | 190 | * gfs2_end_log_write - end of i/o to the log |
|---|
| 195 | 191 | * @bio: The bio |
|---|
| 196 | | - * @error: Status of i/o request |
|---|
| 197 | 192 | * |
|---|
| 198 | 193 | * Each bio_vec contains either data from the pagecache or data |
|---|
| 199 | 194 | * relating to the log itself. Here we iterate over the bio_vec |
|---|
| .. | .. |
|---|
| 206 | 201 | struct gfs2_sbd *sdp = bio->bi_private; |
|---|
| 207 | 202 | struct bio_vec *bvec; |
|---|
| 208 | 203 | struct page *page; |
|---|
| 209 | | - int i; |
|---|
| 204 | + struct bvec_iter_all iter_all; |
|---|
| 210 | 205 | |
|---|
| 211 | 206 | if (bio->bi_status) { |
|---|
| 212 | | - fs_err(sdp, "Error %d writing to journal, jid=%u\n", |
|---|
| 213 | | - bio->bi_status, sdp->sd_jdesc->jd_jid); |
|---|
| 207 | + if (!cmpxchg(&sdp->sd_log_error, 0, (int)bio->bi_status)) |
|---|
| 208 | + fs_err(sdp, "Error %d writing to journal, jid=%u\n", |
|---|
| 209 | + bio->bi_status, sdp->sd_jdesc->jd_jid); |
|---|
| 210 | + gfs2_withdraw_delayed(sdp); |
|---|
| 211 | + /* prevent more writes to the journal */ |
|---|
| 212 | + clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); |
|---|
| 214 | 213 | wake_up(&sdp->sd_logd_waitq); |
|---|
| 215 | 214 | } |
|---|
| 216 | 215 | |
|---|
| 217 | | - bio_for_each_segment_all(bvec, bio, i) { |
|---|
| 216 | + bio_for_each_segment_all(bvec, bio, iter_all) { |
|---|
| 218 | 217 | page = bvec->bv_page; |
|---|
| 219 | 218 | if (page_has_buffers(page)) |
|---|
| 220 | 219 | gfs2_end_log_write_bh(sdp, bvec, bio->bi_status); |
|---|
| .. | .. |
|---|
| 228 | 227 | } |
|---|
| 229 | 228 | |
|---|
| 230 | 229 | /** |
|---|
| 231 | | - * gfs2_log_flush_bio - Submit any pending log bio |
|---|
| 232 | | - * @sdp: The superblock |
|---|
| 233 | | - * @op: REQ_OP |
|---|
| 234 | | - * @op_flags: req_flag_bits |
|---|
| 230 | + * gfs2_log_submit_bio - Submit any pending log bio |
|---|
| 231 | + * @biop: Address of the bio pointer |
|---|
| 232 | + * @opf: REQ_OP | op_flags |
|---|
| 235 | 233 | * |
|---|
| 236 | 234 | * Submit any pending part-built or full bio to the block device. If |
|---|
| 237 | 235 | * there is no pending bio, then this is a no-op. |
|---|
| 238 | 236 | */ |
|---|
| 239 | 237 | |
|---|
| 240 | | -void gfs2_log_flush_bio(struct gfs2_sbd *sdp, int op, int op_flags) |
|---|
| 238 | +void gfs2_log_submit_bio(struct bio **biop, int opf) |
|---|
| 241 | 239 | { |
|---|
| 242 | | - if (sdp->sd_log_bio) { |
|---|
| 240 | + struct bio *bio = *biop; |
|---|
| 241 | + if (bio) { |
|---|
| 242 | + struct gfs2_sbd *sdp = bio->bi_private; |
|---|
| 243 | 243 | atomic_inc(&sdp->sd_log_in_flight); |
|---|
| 244 | | - bio_set_op_attrs(sdp->sd_log_bio, op, op_flags); |
|---|
| 245 | | - submit_bio(sdp->sd_log_bio); |
|---|
| 246 | | - sdp->sd_log_bio = NULL; |
|---|
| 244 | + bio->bi_opf = opf; |
|---|
| 245 | + submit_bio(bio); |
|---|
| 246 | + *biop = NULL; |
|---|
| 247 | 247 | } |
|---|
| 248 | 248 | } |
|---|
| 249 | 249 | |
|---|
| 250 | 250 | /** |
|---|
| 251 | | - * gfs2_log_alloc_bio - Allocate a new bio for log writing |
|---|
| 252 | | - * @sdp: The superblock |
|---|
| 253 | | - * @blkno: The next device block number we want to write to |
|---|
| 251 | + * gfs2_log_alloc_bio - Allocate a bio |
|---|
| 252 | + * @sdp: The super block |
|---|
| 253 | + * @blkno: The device block number we want to write to |
|---|
| 254 | + * @end_io: The bi_end_io callback |
|---|
| 254 | 255 | * |
|---|
| 255 | | - * This should never be called when there is a cached bio in the |
|---|
| 256 | | - * super block. When it returns, there will be a cached bio in the |
|---|
| 257 | | - * super block which will have as many bio_vecs as the device is |
|---|
| 258 | | - * happy to handle. |
|---|
| 256 | + * Allocate a new bio, initialize it with the given parameters and return it. |
|---|
| 259 | 257 | * |
|---|
| 260 | | - * Returns: Newly allocated bio |
|---|
| 258 | + * Returns: The newly allocated bio |
|---|
| 261 | 259 | */ |
|---|
| 262 | 260 | |
|---|
| 263 | | -static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno) |
|---|
| 261 | +static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno, |
|---|
| 262 | + bio_end_io_t *end_io) |
|---|
| 264 | 263 | { |
|---|
| 265 | 264 | struct super_block *sb = sdp->sd_vfs; |
|---|
| 266 | | - struct bio *bio; |
|---|
| 265 | + struct bio *bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); |
|---|
| 267 | 266 | |
|---|
| 268 | | - BUG_ON(sdp->sd_log_bio); |
|---|
| 269 | | - |
|---|
| 270 | | - bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); |
|---|
| 271 | | - bio->bi_iter.bi_sector = blkno * (sb->s_blocksize >> 9); |
|---|
| 267 | + bio->bi_iter.bi_sector = blkno << sdp->sd_fsb2bb_shift; |
|---|
| 272 | 268 | bio_set_dev(bio, sb->s_bdev); |
|---|
| 273 | | - bio->bi_end_io = gfs2_end_log_write; |
|---|
| 269 | + bio->bi_end_io = end_io; |
|---|
| 274 | 270 | bio->bi_private = sdp; |
|---|
| 275 | | - |
|---|
| 276 | | - sdp->sd_log_bio = bio; |
|---|
| 277 | 271 | |
|---|
| 278 | 272 | return bio; |
|---|
| 279 | 273 | } |
|---|
| 280 | 274 | |
|---|
| 281 | 275 | /** |
|---|
| 282 | 276 | * gfs2_log_get_bio - Get cached log bio, or allocate a new one |
|---|
| 283 | | - * @sdp: The superblock |
|---|
| 277 | + * @sdp: The super block |
|---|
| 284 | 278 | * @blkno: The device block number we want to write to |
|---|
| 279 | + * @bio: The bio to get or allocate |
|---|
| 280 | + * @op: REQ_OP |
|---|
| 281 | + * @end_io: The bi_end_io callback |
|---|
| 282 | + * @flush: Always flush the current bio and allocate a new one? |
|---|
| 285 | 283 | * |
|---|
| 286 | 284 | * If there is a cached bio, then if the next block number is sequential |
|---|
| 287 | 285 | * with the previous one, return it, otherwise flush the bio to the |
|---|
| 288 | | - * device. If there is not a cached bio, or we just flushed it, then |
|---|
| 286 | + * device. If there is no cached bio, or we just flushed it, then |
|---|
| 289 | 287 | * allocate a new one. |
|---|
| 290 | 288 | * |
|---|
| 291 | 289 | * Returns: The bio to use for log writes |
|---|
| 292 | 290 | */ |
|---|
| 293 | 291 | |
|---|
| 294 | | -static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno) |
|---|
| 292 | +static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno, |
|---|
| 293 | + struct bio **biop, int op, |
|---|
| 294 | + bio_end_io_t *end_io, bool flush) |
|---|
| 295 | 295 | { |
|---|
| 296 | | - struct bio *bio = sdp->sd_log_bio; |
|---|
| 297 | | - u64 nblk; |
|---|
| 296 | + struct bio *bio = *biop; |
|---|
| 298 | 297 | |
|---|
| 299 | 298 | if (bio) { |
|---|
| 299 | + u64 nblk; |
|---|
| 300 | + |
|---|
| 300 | 301 | nblk = bio_end_sector(bio); |
|---|
| 301 | 302 | nblk >>= sdp->sd_fsb2bb_shift; |
|---|
| 302 | | - if (blkno == nblk) |
|---|
| 303 | + if (blkno == nblk && !flush) |
|---|
| 303 | 304 | return bio; |
|---|
| 304 | | - gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0); |
|---|
| 305 | + gfs2_log_submit_bio(biop, op); |
|---|
| 305 | 306 | } |
|---|
| 306 | 307 | |
|---|
| 307 | | - return gfs2_log_alloc_bio(sdp, blkno); |
|---|
| 308 | + *biop = gfs2_log_alloc_bio(sdp, blkno, end_io); |
|---|
| 309 | + return *biop; |
|---|
| 308 | 310 | } |
|---|
| 309 | 311 | |
|---|
| 310 | 312 | /** |
|---|
| .. | .. |
|---|
| 326 | 328 | struct bio *bio; |
|---|
| 327 | 329 | int ret; |
|---|
| 328 | 330 | |
|---|
| 329 | | - bio = gfs2_log_get_bio(sdp, blkno); |
|---|
| 331 | + bio = gfs2_log_get_bio(sdp, blkno, &sdp->sd_log_bio, REQ_OP_WRITE, |
|---|
| 332 | + gfs2_end_log_write, false); |
|---|
| 330 | 333 | ret = bio_add_page(bio, page, size, offset); |
|---|
| 331 | 334 | if (ret == 0) { |
|---|
| 332 | | - gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0); |
|---|
| 333 | | - bio = gfs2_log_alloc_bio(sdp, blkno); |
|---|
| 335 | + bio = gfs2_log_get_bio(sdp, blkno, &sdp->sd_log_bio, |
|---|
| 336 | + REQ_OP_WRITE, gfs2_end_log_write, true); |
|---|
| 334 | 337 | ret = bio_add_page(bio, page, size, offset); |
|---|
| 335 | 338 | WARN_ON(ret == 0); |
|---|
| 336 | 339 | } |
|---|
| .. | .. |
|---|
| 348 | 351 | |
|---|
| 349 | 352 | static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh) |
|---|
| 350 | 353 | { |
|---|
| 351 | | - gfs2_log_write(sdp, bh->b_page, bh->b_size, bh_offset(bh), |
|---|
| 352 | | - gfs2_log_bmap(sdp)); |
|---|
| 354 | + u64 dblock; |
|---|
| 355 | + |
|---|
| 356 | + dblock = gfs2_log_bmap(sdp->sd_jdesc, sdp->sd_log_flush_head); |
|---|
| 357 | + gfs2_log_incr_head(sdp); |
|---|
| 358 | + gfs2_log_write(sdp, bh->b_page, bh->b_size, bh_offset(bh), dblock); |
|---|
| 353 | 359 | } |
|---|
| 354 | 360 | |
|---|
| 355 | 361 | /** |
|---|
| .. | .. |
|---|
| 366 | 372 | void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page) |
|---|
| 367 | 373 | { |
|---|
| 368 | 374 | struct super_block *sb = sdp->sd_vfs; |
|---|
| 369 | | - gfs2_log_write(sdp, page, sb->s_blocksize, 0, |
|---|
| 370 | | - gfs2_log_bmap(sdp)); |
|---|
| 375 | + u64 dblock; |
|---|
| 376 | + |
|---|
| 377 | + dblock = gfs2_log_bmap(sdp->sd_jdesc, sdp->sd_log_flush_head); |
|---|
| 378 | + gfs2_log_incr_head(sdp); |
|---|
| 379 | + gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock); |
|---|
| 380 | +} |
|---|
| 381 | + |
|---|
| 382 | +/** |
|---|
| 383 | + * gfs2_end_log_read - end I/O callback for reads from the log |
|---|
| 384 | + * @bio: The bio |
|---|
| 385 | + * |
|---|
| 386 | + * Simply unlock the pages in the bio. The main thread will wait on them and |
|---|
| 387 | + * process them in order as necessary. |
|---|
| 388 | + */ |
|---|
| 389 | + |
|---|
| 390 | +static void gfs2_end_log_read(struct bio *bio) |
|---|
| 391 | +{ |
|---|
| 392 | + struct page *page; |
|---|
| 393 | + struct bio_vec *bvec; |
|---|
| 394 | + struct bvec_iter_all iter_all; |
|---|
| 395 | + |
|---|
| 396 | + bio_for_each_segment_all(bvec, bio, iter_all) { |
|---|
| 397 | + page = bvec->bv_page; |
|---|
| 398 | + if (bio->bi_status) { |
|---|
| 399 | + int err = blk_status_to_errno(bio->bi_status); |
|---|
| 400 | + |
|---|
| 401 | + SetPageError(page); |
|---|
| 402 | + mapping_set_error(page->mapping, err); |
|---|
| 403 | + } |
|---|
| 404 | + unlock_page(page); |
|---|
| 405 | + } |
|---|
| 406 | + |
|---|
| 407 | + bio_put(bio); |
|---|
| 408 | +} |
|---|
| 409 | + |
|---|
| 410 | +/** |
|---|
| 411 | + * gfs2_jhead_pg_srch - Look for the journal head in a given page. |
|---|
| 412 | + * @jd: The journal descriptor |
|---|
| 413 | + * @page: The page to look in |
|---|
| 414 | + * |
|---|
| 415 | + * Returns: 1 if found, 0 otherwise. |
|---|
| 416 | + */ |
|---|
| 417 | + |
|---|
| 418 | +static bool gfs2_jhead_pg_srch(struct gfs2_jdesc *jd, |
|---|
| 419 | + struct gfs2_log_header_host *head, |
|---|
| 420 | + struct page *page) |
|---|
| 421 | +{ |
|---|
| 422 | + struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
|---|
| 423 | + struct gfs2_log_header_host lh; |
|---|
| 424 | + void *kaddr = kmap_atomic(page); |
|---|
| 425 | + unsigned int offset; |
|---|
| 426 | + bool ret = false; |
|---|
| 427 | + |
|---|
| 428 | + for (offset = 0; offset < PAGE_SIZE; offset += sdp->sd_sb.sb_bsize) { |
|---|
| 429 | + if (!__get_log_header(sdp, kaddr + offset, 0, &lh)) { |
|---|
| 430 | + if (lh.lh_sequence >= head->lh_sequence) |
|---|
| 431 | + *head = lh; |
|---|
| 432 | + else { |
|---|
| 433 | + ret = true; |
|---|
| 434 | + break; |
|---|
| 435 | + } |
|---|
| 436 | + } |
|---|
| 437 | + } |
|---|
| 438 | + kunmap_atomic(kaddr); |
|---|
| 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 |
|---|
| 446 | + * @done: If set, perform only cleanup, else search and set if found. |
|---|
| 447 | + * |
|---|
| 448 | + * Find the page with 'index' in the journal's mapping. Search the page for |
|---|
| 449 | + * the journal head if requested (cleanup == false). Release refs on the |
|---|
| 450 | + * page so the page cache can reclaim it (put_page() twice). We grabbed a |
|---|
| 451 | + * reference on this page two times, first when we did a find_or_create_page() |
|---|
| 452 | + * to obtain the page to add it to the bio and second when we do a |
|---|
| 453 | + * find_get_page() here to get the page to wait on while I/O on it is being |
|---|
| 454 | + * completed. |
|---|
| 455 | + * This function is also used to free up a page we might've grabbed but not |
|---|
| 456 | + * used. Maybe we added it to a bio, but not submitted it for I/O. Or we |
|---|
| 457 | + * submitted the I/O, but we already found the jhead so we only need to drop |
|---|
| 458 | + * our references to the page. |
|---|
| 459 | + */ |
|---|
| 460 | + |
|---|
| 461 | +static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index, |
|---|
| 462 | + struct gfs2_log_header_host *head, |
|---|
| 463 | + bool *done) |
|---|
| 464 | +{ |
|---|
| 465 | + struct page *page; |
|---|
| 466 | + |
|---|
| 467 | + page = find_get_page(jd->jd_inode->i_mapping, index); |
|---|
| 468 | + wait_on_page_locked(page); |
|---|
| 469 | + |
|---|
| 470 | + if (PageError(page)) |
|---|
| 471 | + *done = true; |
|---|
| 472 | + |
|---|
| 473 | + if (!*done) |
|---|
| 474 | + *done = gfs2_jhead_pg_srch(jd, head, page); |
|---|
| 475 | + |
|---|
| 476 | + put_page(page); /* Once for find_get_page */ |
|---|
| 477 | + put_page(page); /* Once more for find_or_create_page */ |
|---|
| 478 | +} |
|---|
| 479 | + |
|---|
| 480 | +static struct bio *gfs2_chain_bio(struct bio *prev, unsigned int nr_iovecs) |
|---|
| 481 | +{ |
|---|
| 482 | + struct bio *new; |
|---|
| 483 | + |
|---|
| 484 | + new = bio_alloc(GFP_NOIO, nr_iovecs); |
|---|
| 485 | + bio_copy_dev(new, prev); |
|---|
| 486 | + new->bi_iter.bi_sector = bio_end_sector(prev); |
|---|
| 487 | + new->bi_opf = prev->bi_opf; |
|---|
| 488 | + new->bi_write_hint = prev->bi_write_hint; |
|---|
| 489 | + bio_chain(new, prev); |
|---|
| 490 | + submit_bio(prev); |
|---|
| 491 | + return new; |
|---|
| 492 | +} |
|---|
| 493 | + |
|---|
| 494 | +/** |
|---|
| 495 | + * gfs2_find_jhead - find the head of a log |
|---|
| 496 | + * @jd: The journal descriptor |
|---|
| 497 | + * @head: The log descriptor for the head of the log is returned here |
|---|
| 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; |
|---|
| 510 | + unsigned int bsize = sdp->sd_sb.sb_bsize, off; |
|---|
| 511 | + unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift; |
|---|
| 512 | + unsigned int shift = PAGE_SHIFT - bsize_shift; |
|---|
| 513 | + unsigned int max_blocks = 2 * 1024 * 1024 >> bsize_shift; |
|---|
| 514 | + struct gfs2_journal_extent *je; |
|---|
| 515 | + int sz, ret = 0; |
|---|
| 516 | + struct bio *bio = NULL; |
|---|
| 517 | + struct page *page = NULL; |
|---|
| 518 | + bool done = false; |
|---|
| 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) { |
|---|
| 527 | + u64 dblock = je->dblock; |
|---|
| 528 | + |
|---|
| 529 | + for (; block < je->lblock + je->blocks; block++, dblock++) { |
|---|
| 530 | + if (!page) { |
|---|
| 531 | + page = find_or_create_page(mapping, |
|---|
| 532 | + block >> shift, GFP_NOFS); |
|---|
| 533 | + if (!page) { |
|---|
| 534 | + ret = -ENOMEM; |
|---|
| 535 | + done = true; |
|---|
| 536 | + goto out; |
|---|
| 537 | + } |
|---|
| 538 | + off = 0; |
|---|
| 539 | + } |
|---|
| 540 | + |
|---|
| 541 | + if (bio && (off || block < blocks_submitted + max_blocks)) { |
|---|
| 542 | + sector_t sector = dblock << sdp->sd_fsb2bb_shift; |
|---|
| 543 | + |
|---|
| 544 | + if (bio_end_sector(bio) == sector) { |
|---|
| 545 | + sz = bio_add_page(bio, page, bsize, off); |
|---|
| 546 | + if (sz == bsize) |
|---|
| 547 | + goto block_added; |
|---|
| 548 | + } |
|---|
| 549 | + if (off) { |
|---|
| 550 | + unsigned int blocks = |
|---|
| 551 | + (PAGE_SIZE - off) >> bsize_shift; |
|---|
| 552 | + |
|---|
| 553 | + bio = gfs2_chain_bio(bio, blocks); |
|---|
| 554 | + goto add_block_to_new_bio; |
|---|
| 555 | + } |
|---|
| 556 | + } |
|---|
| 557 | + |
|---|
| 558 | + if (bio) { |
|---|
| 559 | + blocks_submitted = block; |
|---|
| 560 | + submit_bio(bio); |
|---|
| 561 | + } |
|---|
| 562 | + |
|---|
| 563 | + bio = gfs2_log_alloc_bio(sdp, dblock, gfs2_end_log_read); |
|---|
| 564 | + bio->bi_opf = REQ_OP_READ; |
|---|
| 565 | +add_block_to_new_bio: |
|---|
| 566 | + sz = bio_add_page(bio, page, bsize, off); |
|---|
| 567 | + BUG_ON(sz != bsize); |
|---|
| 568 | +block_added: |
|---|
| 569 | + off += bsize; |
|---|
| 570 | + if (off == PAGE_SIZE) |
|---|
| 571 | + page = NULL; |
|---|
| 572 | + if (blocks_submitted <= blocks_read + max_blocks) { |
|---|
| 573 | + /* Keep at least one bio in flight */ |
|---|
| 574 | + continue; |
|---|
| 575 | + } |
|---|
| 576 | + |
|---|
| 577 | + gfs2_jhead_process_page(jd, blocks_read >> shift, head, &done); |
|---|
| 578 | + blocks_read += PAGE_SIZE >> bsize_shift; |
|---|
| 579 | + if (done) |
|---|
| 580 | + goto out; /* found */ |
|---|
| 581 | + } |
|---|
| 582 | + } |
|---|
| 583 | + |
|---|
| 584 | +out: |
|---|
| 585 | + if (bio) |
|---|
| 586 | + submit_bio(bio); |
|---|
| 587 | + while (blocks_read < block) { |
|---|
| 588 | + gfs2_jhead_process_page(jd, blocks_read >> shift, head, &done); |
|---|
| 589 | + blocks_read += PAGE_SIZE >> bsize_shift; |
|---|
| 590 | + } |
|---|
| 591 | + |
|---|
| 592 | + if (!ret) |
|---|
| 593 | + ret = filemap_check_wb_err(mapping, since); |
|---|
| 594 | + |
|---|
| 595 | + if (!keep_cache) |
|---|
| 596 | + truncate_inode_pages(mapping, 0); |
|---|
| 597 | + |
|---|
| 598 | + return ret; |
|---|
| 371 | 599 | } |
|---|
| 372 | 600 | |
|---|
| 373 | 601 | static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type, |
|---|
| .. | .. |
|---|
| 507 | 735 | |
|---|
| 508 | 736 | head = &tr->tr_buf; |
|---|
| 509 | 737 | while (!list_empty(head)) { |
|---|
| 510 | | - bd = list_entry(head->next, struct gfs2_bufdata, bd_list); |
|---|
| 738 | + bd = list_first_entry(head, struct gfs2_bufdata, bd_list); |
|---|
| 511 | 739 | list_del_init(&bd->bd_list); |
|---|
| 512 | 740 | gfs2_unpin(sdp, bd->bd_bh, tr); |
|---|
| 513 | 741 | } |
|---|
| .. | .. |
|---|
| 523 | 751 | jd->jd_replayed_blocks = 0; |
|---|
| 524 | 752 | } |
|---|
| 525 | 753 | |
|---|
| 526 | | -static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, |
|---|
| 754 | +static int buf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start, |
|---|
| 527 | 755 | struct gfs2_log_descriptor *ld, __be64 *ptr, |
|---|
| 528 | 756 | int pass) |
|---|
| 529 | 757 | { |
|---|
| .. | .. |
|---|
| 557 | 785 | |
|---|
| 558 | 786 | if (gfs2_meta_check(sdp, bh_ip)) |
|---|
| 559 | 787 | error = -EIO; |
|---|
| 560 | | - else |
|---|
| 561 | | - mark_buffer_dirty(bh_ip); |
|---|
| 788 | + else { |
|---|
| 789 | + struct gfs2_meta_header *mh = |
|---|
| 790 | + (struct gfs2_meta_header *)bh_ip->b_data; |
|---|
| 562 | 791 | |
|---|
| 792 | + if (mh->mh_type == cpu_to_be32(GFS2_METATYPE_RG)) { |
|---|
| 793 | + struct gfs2_rgrpd *rgd; |
|---|
| 794 | + |
|---|
| 795 | + rgd = gfs2_blk2rgrpd(sdp, blkno, false); |
|---|
| 796 | + if (rgd && rgd->rd_addr == blkno && |
|---|
| 797 | + rgd->rd_bits && rgd->rd_bits->bi_bh) { |
|---|
| 798 | + fs_info(sdp, "Replaying 0x%llx but we " |
|---|
| 799 | + "already have a bh!\n", |
|---|
| 800 | + (unsigned long long)blkno); |
|---|
| 801 | + fs_info(sdp, "busy:%d, pinned:%d\n", |
|---|
| 802 | + buffer_busy(rgd->rd_bits->bi_bh) ? 1 : 0, |
|---|
| 803 | + buffer_pinned(rgd->rd_bits->bi_bh)); |
|---|
| 804 | + gfs2_dump_glock(NULL, rgd->rd_gl, true); |
|---|
| 805 | + } |
|---|
| 806 | + } |
|---|
| 807 | + mark_buffer_dirty(bh_ip); |
|---|
| 808 | + } |
|---|
| 563 | 809 | brelse(bh_log); |
|---|
| 564 | 810 | brelse(bh_ip); |
|---|
| 565 | 811 | |
|---|
| .. | .. |
|---|
| 572 | 818 | return error; |
|---|
| 573 | 819 | } |
|---|
| 574 | 820 | |
|---|
| 575 | | -/** |
|---|
| 576 | | - * gfs2_meta_sync - Sync all buffers associated with a glock |
|---|
| 577 | | - * @gl: The glock |
|---|
| 578 | | - * |
|---|
| 579 | | - */ |
|---|
| 580 | | - |
|---|
| 581 | | -static void gfs2_meta_sync(struct gfs2_glock *gl) |
|---|
| 582 | | -{ |
|---|
| 583 | | - struct address_space *mapping = gfs2_glock2aspace(gl); |
|---|
| 584 | | - struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
|---|
| 585 | | - int error; |
|---|
| 586 | | - |
|---|
| 587 | | - if (mapping == NULL) |
|---|
| 588 | | - mapping = &sdp->sd_aspace; |
|---|
| 589 | | - |
|---|
| 590 | | - filemap_fdatawrite(mapping); |
|---|
| 591 | | - error = filemap_fdatawait(mapping); |
|---|
| 592 | | - |
|---|
| 593 | | - if (error) |
|---|
| 594 | | - gfs2_io_error(gl->gl_name.ln_sbd); |
|---|
| 595 | | -} |
|---|
| 596 | | - |
|---|
| 597 | 821 | static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) |
|---|
| 598 | 822 | { |
|---|
| 599 | 823 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
|---|
| 600 | 824 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
|---|
| 601 | 825 | |
|---|
| 602 | 826 | if (error) { |
|---|
| 603 | | - gfs2_meta_sync(ip->i_gl); |
|---|
| 827 | + gfs2_inode_metasync(ip->i_gl); |
|---|
| 604 | 828 | return; |
|---|
| 605 | 829 | } |
|---|
| 606 | 830 | if (pass != 1) |
|---|
| 607 | 831 | return; |
|---|
| 608 | 832 | |
|---|
| 609 | | - gfs2_meta_sync(ip->i_gl); |
|---|
| 833 | + gfs2_inode_metasync(ip->i_gl); |
|---|
| 610 | 834 | |
|---|
| 611 | 835 | fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n", |
|---|
| 612 | 836 | jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks); |
|---|
| .. | .. |
|---|
| 616 | 840 | { |
|---|
| 617 | 841 | struct gfs2_meta_header *mh; |
|---|
| 618 | 842 | unsigned int offset; |
|---|
| 619 | | - struct list_head *head = &sdp->sd_log_le_revoke; |
|---|
| 843 | + struct list_head *head = &sdp->sd_log_revokes; |
|---|
| 620 | 844 | struct gfs2_bufdata *bd; |
|---|
| 621 | 845 | struct page *page; |
|---|
| 622 | 846 | unsigned int length; |
|---|
| .. | .. |
|---|
| 625 | 849 | if (!sdp->sd_log_num_revoke) |
|---|
| 626 | 850 | return; |
|---|
| 627 | 851 | |
|---|
| 628 | | - length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, sizeof(u64)); |
|---|
| 852 | + length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke); |
|---|
| 629 | 853 | page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke); |
|---|
| 630 | 854 | offset = sizeof(struct gfs2_log_descriptor); |
|---|
| 631 | 855 | |
|---|
| .. | .. |
|---|
| 654 | 878 | |
|---|
| 655 | 879 | static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
|---|
| 656 | 880 | { |
|---|
| 657 | | - struct list_head *head = &sdp->sd_log_le_revoke; |
|---|
| 881 | + struct list_head *head = &sdp->sd_log_revokes; |
|---|
| 658 | 882 | struct gfs2_bufdata *bd; |
|---|
| 659 | 883 | struct gfs2_glock *gl; |
|---|
| 660 | 884 | |
|---|
| 661 | 885 | while (!list_empty(head)) { |
|---|
| 662 | | - bd = list_entry(head->next, struct gfs2_bufdata, bd_list); |
|---|
| 886 | + bd = list_first_entry(head, struct gfs2_bufdata, bd_list); |
|---|
| 663 | 887 | list_del_init(&bd->bd_list); |
|---|
| 664 | 888 | gl = bd->bd_gl; |
|---|
| 665 | 889 | gfs2_glock_remove_revoke(gl); |
|---|
| .. | .. |
|---|
| 677 | 901 | jd->jd_replay_tail = head->lh_tail; |
|---|
| 678 | 902 | } |
|---|
| 679 | 903 | |
|---|
| 680 | | -static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, |
|---|
| 904 | +static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, u32 start, |
|---|
| 681 | 905 | struct gfs2_log_descriptor *ld, __be64 *ptr, |
|---|
| 682 | 906 | int pass) |
|---|
| 683 | 907 | { |
|---|
| .. | .. |
|---|
| 759 | 983 | gfs2_before_commit(sdp, limit, nbuf, &tr->tr_databuf, 1); |
|---|
| 760 | 984 | } |
|---|
| 761 | 985 | |
|---|
| 762 | | -static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, |
|---|
| 986 | +static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start, |
|---|
| 763 | 987 | struct gfs2_log_descriptor *ld, |
|---|
| 764 | 988 | __be64 *ptr, int pass) |
|---|
| 765 | 989 | { |
|---|
| .. | .. |
|---|
| 815 | 1039 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
|---|
| 816 | 1040 | |
|---|
| 817 | 1041 | if (error) { |
|---|
| 818 | | - gfs2_meta_sync(ip->i_gl); |
|---|
| 1042 | + gfs2_inode_metasync(ip->i_gl); |
|---|
| 819 | 1043 | return; |
|---|
| 820 | 1044 | } |
|---|
| 821 | 1045 | if (pass != 1) |
|---|
| 822 | 1046 | return; |
|---|
| 823 | 1047 | |
|---|
| 824 | 1048 | /* data sync? */ |
|---|
| 825 | | - gfs2_meta_sync(ip->i_gl); |
|---|
| 1049 | + gfs2_inode_metasync(ip->i_gl); |
|---|
| 826 | 1050 | |
|---|
| 827 | 1051 | fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n", |
|---|
| 828 | 1052 | jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks); |
|---|
| .. | .. |
|---|
| 838 | 1062 | |
|---|
| 839 | 1063 | head = &tr->tr_databuf; |
|---|
| 840 | 1064 | while (!list_empty(head)) { |
|---|
| 841 | | - bd = list_entry(head->next, struct gfs2_bufdata, bd_list); |
|---|
| 1065 | + bd = list_first_entry(head, struct gfs2_bufdata, bd_list); |
|---|
| 842 | 1066 | list_del_init(&bd->bd_list); |
|---|
| 843 | 1067 | gfs2_unpin(sdp, bd->bd_bh, tr); |
|---|
| 844 | 1068 | } |
|---|
| 845 | 1069 | } |
|---|
| 846 | 1070 | |
|---|
| 847 | 1071 | |
|---|
| 848 | | -const struct gfs2_log_operations gfs2_buf_lops = { |
|---|
| 1072 | +static const struct gfs2_log_operations gfs2_buf_lops = { |
|---|
| 849 | 1073 | .lo_before_commit = buf_lo_before_commit, |
|---|
| 850 | 1074 | .lo_after_commit = buf_lo_after_commit, |
|---|
| 851 | 1075 | .lo_before_scan = buf_lo_before_scan, |
|---|
| .. | .. |
|---|
| 854 | 1078 | .lo_name = "buf", |
|---|
| 855 | 1079 | }; |
|---|
| 856 | 1080 | |
|---|
| 857 | | -const struct gfs2_log_operations gfs2_revoke_lops = { |
|---|
| 1081 | +static const struct gfs2_log_operations gfs2_revoke_lops = { |
|---|
| 858 | 1082 | .lo_before_commit = revoke_lo_before_commit, |
|---|
| 859 | 1083 | .lo_after_commit = revoke_lo_after_commit, |
|---|
| 860 | 1084 | .lo_before_scan = revoke_lo_before_scan, |
|---|
| .. | .. |
|---|
| 863 | 1087 | .lo_name = "revoke", |
|---|
| 864 | 1088 | }; |
|---|
| 865 | 1089 | |
|---|
| 866 | | -const struct gfs2_log_operations gfs2_databuf_lops = { |
|---|
| 1090 | +static const struct gfs2_log_operations gfs2_databuf_lops = { |
|---|
| 867 | 1091 | .lo_before_commit = databuf_lo_before_commit, |
|---|
| 868 | 1092 | .lo_after_commit = databuf_lo_after_commit, |
|---|
| 869 | 1093 | .lo_scan_elements = databuf_lo_scan_elements, |
|---|