meta_io.c 10.4 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
D
David Teigland 已提交
4 5 6
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
7
 * of the GNU General Public License version 2.
D
David Teigland 已提交
8 9 10 11 12 13 14 15 16 17 18 19
 */

#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/swap.h>
#include <linux/delay.h>
20
#include <linux/bio.h>
21
#include <linux/gfs2_ondisk.h>
D
David Teigland 已提交
22 23

#include "gfs2.h"
24
#include "incore.h"
D
David Teigland 已提交
25 26 27 28 29 30 31 32
#include "glock.h"
#include "glops.h"
#include "inode.h"
#include "log.h"
#include "lops.h"
#include "meta_io.h"
#include "rgrp.h"
#include "trans.h"
33
#include "util.h"
34
#include "trace_gfs2.h"
D
David Teigland 已提交
35

36
static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
D
David Teigland 已提交
37
{
38 39
	struct buffer_head *bh, *head;
	int nr_underway = 0;
40
	int write_op = REQ_META | REQ_PRIO |
J
Jens Axboe 已提交
41
		(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
D
David Teigland 已提交
42

43 44 45 46 47 48 49 50 51 52 53 54
	BUG_ON(!PageLocked(page));
	BUG_ON(!page_has_buffers(page));

	head = page_buffers(page);
	bh = head;

	do {
		if (!buffer_mapped(bh))
			continue;
		/*
		 * If it's a fully non-blocking write attempt and we cannot
		 * lock the buffer then redirty the page.  Note that this can
55
		 * potentially cause a busy-wait loop from flusher thread and kswapd
56 57 58
		 * activity, but those code paths have their own higher-level
		 * throttling.
		 */
59
		if (wbc->sync_mode != WB_SYNC_NONE) {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
			lock_buffer(bh);
		} else if (!trylock_buffer(bh)) {
			redirty_page_for_writepage(wbc, page);
			continue;
		}
		if (test_clear_buffer_dirty(bh)) {
			mark_buffer_async_write(bh);
		} else {
			unlock_buffer(bh);
		}
	} while ((bh = bh->b_this_page) != head);

	/*
	 * The page and its buffers are protected by PageWriteback(), so we can
	 * drop the bh refcounts early.
	 */
	BUG_ON(PageWriteback(page));
	set_page_writeback(page);

	do {
		struct buffer_head *next = bh->b_this_page;
		if (buffer_async_write(bh)) {
			submit_bh(write_op, bh);
			nr_underway++;
		}
		bh = next;
	} while (bh != head);
	unlock_page(page);

	if (nr_underway == 0)
		end_page_writeback(page);

92
	return 0;
D
David Teigland 已提交
93 94
}

95
const struct address_space_operations gfs2_meta_aops = {
D
David Teigland 已提交
96
	.writepage = gfs2_aspace_writepage,
S
Steven Whitehouse 已提交
97
	.releasepage = gfs2_releasepage,
D
David Teigland 已提交
98 99
};

100 101 102 103 104
const struct address_space_operations gfs2_rgrp_aops = {
	.writepage = gfs2_aspace_writepage,
	.releasepage = gfs2_releasepage,
};

D
David Teigland 已提交
105
/**
106
 * gfs2_getbuf - Get a buffer with a given address space
107
 * @gl: the glock
D
David Teigland 已提交
108 109 110 111 112 113
 * @blkno: the block number (filesystem scope)
 * @create: 1 if the buffer should be created
 *
 * Returns: the buffer
 */

114
struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
D
David Teigland 已提交
115
{
116
	struct address_space *mapping = gfs2_glock2aspace(gl);
117
	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
D
David Teigland 已提交
118 119 120 121 122 123
	struct page *page;
	struct buffer_head *bh;
	unsigned int shift;
	unsigned long index;
	unsigned int bufnum;

124 125 126
	if (mapping == NULL)
		mapping = &sdp->sd_aspace;

D
David Teigland 已提交
127 128 129 130 131 132
	shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
	index = blkno >> shift;             /* convert block to page */
	bufnum = blkno - (index << shift);  /* block buf index within page */

	if (create) {
		for (;;) {
133
			page = grab_cache_page(mapping, index);
D
David Teigland 已提交
134 135 136 137 138
			if (page)
				break;
			yield();
		}
	} else {
139 140
		page = find_get_page_flags(mapping, index,
						FGP_LOCK|FGP_ACCESSED);
D
David Teigland 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		if (!page)
			return NULL;
	}

	if (!page_has_buffers(page))
		create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);

	/* Locate header for our buffer within our page */
	for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
		/* Do nothing */;
	get_bh(bh);

	if (!buffer_mapped(bh))
		map_bh(bh, sdp->sd_vfs, blkno);

	unlock_page(page);
	page_cache_release(page);

	return bh;
}

static void meta_prep_new(struct buffer_head *bh)
{
	struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;

	lock_buffer(bh);
	clear_buffer_dirty(bh);
	set_buffer_uptodate(bh);
	unlock_buffer(bh);

	mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
}

/**
 * gfs2_meta_new - Get a block
 * @gl: The glock associated with this block
 * @blkno: The block number
 *
 * Returns: The buffer
 */

182
struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
D
David Teigland 已提交
183 184
{
	struct buffer_head *bh;
185
	bh = gfs2_getbuf(gl, blkno, CREATE);
D
David Teigland 已提交
186 187 188 189
	meta_prep_new(bh);
	return bh;
}

190
static void gfs2_meta_read_endio(struct bio *bio)
191
{
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	struct bio_vec *bvec;
	int i;

	bio_for_each_segment_all(bvec, bio, i) {
		struct page *page = bvec->bv_page;
		struct buffer_head *bh = page_buffers(page);
		unsigned int len = bvec->bv_len;

		while (bh_offset(bh) < bvec->bv_offset)
			bh = bh->b_this_page;
		do {
			struct buffer_head *next = bh->b_this_page;
			len -= bh->b_size;
			bh->b_end_io(bh, !bio->bi_error);
			bh = next;
		} while (bh && len);
	}
	bio_put(bio);
}
211

212 213 214 215 216 217 218 219 220 221 222
/*
 * Submit several consecutive buffer head I/O requests as a single bio I/O
 * request.  (See submit_bh_wbc.)
 */
static void gfs2_submit_bhs(int rw, struct buffer_head *bhs[], int num)
{
	struct buffer_head *bh = bhs[0];
	struct bio *bio;
	int i;

	if (!num)
223
		return;
224 225 226 227 228 229 230

	bio = bio_alloc(GFP_NOIO, num);
	bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
	bio->bi_bdev = bh->b_bdev;
	for (i = 0; i < num; i++) {
		bh = bhs[i];
		bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
231
	}
232 233
	bio->bi_end_io = gfs2_meta_read_endio;
	submit_bio(rw, bio);
234 235
}

D
David Teigland 已提交
236 237 238 239
/**
 * gfs2_meta_read - Read a block from disk
 * @gl: The glock covering the block
 * @blkno: The block number
S
Steven Whitehouse 已提交
240
 * @flags: flags
D
David Teigland 已提交
241 242 243 244 245
 * @bhp: the place where the buffer is returned (NULL on failure)
 *
 * Returns: errno
 */

246
int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
247
		   int rahead, struct buffer_head **bhp)
D
David Teigland 已提交
248
{
249
	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
250 251
	struct buffer_head *bh, *bhs[2];
	int num = 0;
S
Steven Whitehouse 已提交
252

253 254
	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
		*bhp = NULL;
S
Steven Whitehouse 已提交
255
		return -EIO;
256
	}
S
Steven Whitehouse 已提交
257 258 259 260 261 262

	*bhp = bh = gfs2_getbuf(gl, blkno, CREATE);

	lock_buffer(bh);
	if (buffer_uptodate(bh)) {
		unlock_buffer(bh);
263 264 265 266 267
		flags &= ~DIO_WAIT;
	} else {
		bh->b_end_io = end_buffer_read_sync;
		get_bh(bh);
		bhs[num++] = bh;
S
Steven Whitehouse 已提交
268
	}
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

	if (rahead) {
		bh = gfs2_getbuf(gl, blkno + 1, CREATE);

		lock_buffer(bh);
		if (buffer_uptodate(bh)) {
			unlock_buffer(bh);
			brelse(bh);
		} else {
			bh->b_end_io = end_buffer_read_sync;
			bhs[num++] = bh;
		}
	}

	gfs2_submit_bhs(READ_SYNC | REQ_META | REQ_PRIO, bhs, num);
S
Steven Whitehouse 已提交
284 285 286
	if (!(flags & DIO_WAIT))
		return 0;

287
	bh = *bhp;
S
Steven Whitehouse 已提交
288 289 290 291 292 293
	wait_on_buffer(bh);
	if (unlikely(!buffer_uptodate(bh))) {
		struct gfs2_trans *tr = current->journal_info;
		if (tr && tr->tr_touched)
			gfs2_io_error_bh(sdp, bh);
		brelse(bh);
294
		*bhp = NULL;
S
Steven Whitehouse 已提交
295
		return -EIO;
S
Steven Whitehouse 已提交
296
	}
D
David Teigland 已提交
297

S
Steven Whitehouse 已提交
298
	return 0;
D
David Teigland 已提交
299 300 301
}

/**
S
Steven Whitehouse 已提交
302
 * gfs2_meta_wait - Reread a block from disk
D
David Teigland 已提交
303
 * @sdp: the filesystem
S
Steven Whitehouse 已提交
304
 * @bh: The block to wait for
D
David Teigland 已提交
305 306 307 308
 *
 * Returns: errno
 */

S
Steven Whitehouse 已提交
309
int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
D
David Teigland 已提交
310 311 312 313
{
	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
		return -EIO;

S
Steven Whitehouse 已提交
314
	wait_on_buffer(bh);
D
David Teigland 已提交
315

S
Steven Whitehouse 已提交
316 317 318 319 320
	if (!buffer_uptodate(bh)) {
		struct gfs2_trans *tr = current->journal_info;
		if (tr && tr->tr_touched)
			gfs2_io_error_bh(sdp, bh);
		return -EIO;
D
David Teigland 已提交
321
	}
S
Steven Whitehouse 已提交
322 323
	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
		return -EIO;
D
David Teigland 已提交
324 325 326 327

	return 0;
}

328 329
void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int meta)
{
330 331
	struct address_space *mapping = bh->b_page->mapping;
	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
332
	struct gfs2_bufdata *bd = bh->b_private;
333
	int was_pinned = 0;
334

335
	if (test_clear_buffer_pinned(bh)) {
336
		trace_gfs2_pin(bd, 0);
337
		atomic_dec(&sdp->sd_log_pinned);
338
		list_del_init(&bd->bd_list);
339
		if (meta)
340
			tr->tr_num_buf_rm++;
341
		else
342 343
			tr->tr_num_databuf_rm++;
		tr->tr_touched = 1;
344
		was_pinned = 1;
345 346 347
		brelse(bh);
	}
	if (bd) {
348
		spin_lock(&sdp->sd_ail_lock);
349
		if (bd->bd_tr) {
350
			gfs2_trans_add_revoke(sdp, bd);
351 352 353
		} else if (was_pinned) {
			bh->b_private = NULL;
			kmem_cache_free(gfs2_bufdata_cachep, bd);
354
		}
355
		spin_unlock(&sdp->sd_ail_lock);
356 357 358 359 360
	}
	clear_buffer_dirty(bh);
	clear_buffer_uptodate(bh);
}

D
David Teigland 已提交
361 362 363 364 365 366 367 368
/**
 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
 * @ip: the inode who owns the buffers
 * @bstart: the first buffer in the run
 * @blen: the number of buffers in the run
 *
 */

369
void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
D
David Teigland 已提交
370
{
371
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
372 373 374
	struct buffer_head *bh;

	while (blen) {
375
		bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
D
David Teigland 已提交
376
		if (bh) {
377 378
			lock_buffer(bh);
			gfs2_log_lock(sdp);
379
			gfs2_remove_from_journal(bh, current->journal_info, 1);
380
			gfs2_log_unlock(sdp);
D
David Teigland 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
			unlock_buffer(bh);
			brelse(bh);
		}

		bstart++;
		blen--;
	}
}

/**
 * gfs2_meta_indirect_buffer - Get a metadata buffer
 * @ip: The GFS2 inode
 * @height: The level of this buf in the metadata (indir addr) tree (if any)
 * @num: The block number (device relative) of the buffer
 * @bhp: the buffer is returned here
 *
 * Returns: errno
 */

400
int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
401
			      struct buffer_head **bhp)
D
David Teigland 已提交
402
{
S
Steven Whitehouse 已提交
403 404
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
	struct gfs2_glock *gl = ip->i_gl;
405 406
	struct buffer_head *bh;
	int ret = 0;
407
	u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
408 409 410 411
	int rahead = 0;

	if (num == ip->i_no_addr)
		rahead = ip->i_rahead;
D
David Teigland 已提交
412

413
	ret = gfs2_meta_read(gl, num, DIO_WAIT, rahead, &bh);
414 415 416
	if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
		brelse(bh);
		ret = -EIO;
D
David Teigland 已提交
417 418
	}
	*bhp = bh;
419
	return ret;
D
David Teigland 已提交
420 421 422 423 424 425 426 427
}

/**
 * gfs2_meta_ra - start readahead on an extent of a file
 * @gl: the glock the blocks belong to
 * @dblock: the starting disk block
 * @extlen: the number of blocks in the extent
 *
S
Steven Whitehouse 已提交
428
 * returns: the first buffer in the extent
D
David Teigland 已提交
429 430
 */

S
Steven Whitehouse 已提交
431
struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
D
David Teigland 已提交
432
{
433
	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
D
David Teigland 已提交
434
	struct buffer_head *first_bh, *bh;
435
	u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
436
			  sdp->sd_sb.sb_bsize_shift;
D
David Teigland 已提交
437

S
Steven Whitehouse 已提交
438 439 440 441
	BUG_ON(!extlen);

	if (max_ra < 1)
		max_ra = 1;
D
David Teigland 已提交
442 443 444
	if (extlen > max_ra)
		extlen = max_ra;

445
	first_bh = gfs2_getbuf(gl, dblock, CREATE);
D
David Teigland 已提交
446 447 448

	if (buffer_uptodate(first_bh))
		goto out;
S
Steven Whitehouse 已提交
449
	if (!buffer_locked(first_bh))
S
Steven Whitehouse 已提交
450
		ll_rw_block(READ_SYNC | REQ_META, 1, &first_bh);
D
David Teigland 已提交
451 452 453 454 455

	dblock++;
	extlen--;

	while (extlen) {
456
		bh = gfs2_getbuf(gl, dblock, CREATE);
D
David Teigland 已提交
457

S
Steven Whitehouse 已提交
458
		if (!buffer_uptodate(bh) && !buffer_locked(bh))
S
Steven Whitehouse 已提交
459
			ll_rw_block(READA | REQ_META, 1, &bh);
S
Steven Whitehouse 已提交
460
		brelse(bh);
D
David Teigland 已提交
461 462
		dblock++;
		extlen--;
S
Steven Whitehouse 已提交
463 464
		if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
			goto out;
D
David Teigland 已提交
465 466
	}

S
Steven Whitehouse 已提交
467
	wait_on_buffer(first_bh);
468
out:
S
Steven Whitehouse 已提交
469
	return first_bh;
D
David Teigland 已提交
470 471
}