meta_io.c 12.4 KB
Newer Older
D
David Teigland 已提交
1 2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3
 * Copyright (C) 2004-2006 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>
22
#include <linux/lm_interface.h>
D
David Teigland 已提交
23 24

#include "gfs2.h"
25
#include "incore.h"
D
David Teigland 已提交
26 27 28 29 30 31 32 33
#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"
34
#include "util.h"
S
Steven Whitehouse 已提交
35
#include "ops_address.h"
D
David Teigland 已提交
36 37 38 39

static int aspace_get_block(struct inode *inode, sector_t lblock,
			    struct buffer_head *bh_result, int create)
{
40
	gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
D
David Teigland 已提交
41 42 43 44 45 46 47 48 49
	return -EOPNOTSUPP;
}

static int gfs2_aspace_writepage(struct page *page,
				 struct writeback_control *wbc)
{
	return block_write_full_page(page, aspace_get_block, wbc);
}

50
static const struct address_space_operations aspace_aops = {
D
David Teigland 已提交
51
	.writepage = gfs2_aspace_writepage,
S
Steven Whitehouse 已提交
52
	.releasepage = gfs2_releasepage,
D
David Teigland 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
};

/**
 * gfs2_aspace_get - Create and initialize a struct inode structure
 * @sdp: the filesystem the aspace is in
 *
 * Right now a struct inode is just a struct inode.  Maybe Linux
 * will supply a more lightweight address space construct (that works)
 * in the future.
 *
 * Make sure pages/buffers in this aspace aren't in high memory.
 *
 * Returns: the aspace
 */

struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
{
	struct inode *aspace;

	aspace = new_inode(sdp->sd_vfs);
	if (aspace) {
74
		mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
D
David Teigland 已提交
75 76
		aspace->i_mapping->a_ops = &aspace_aops;
		aspace->i_size = ~0ULL;
77
		aspace->i_private = NULL;
D
David Teigland 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		insert_inode_hash(aspace);
	}
	return aspace;
}

void gfs2_aspace_put(struct inode *aspace)
{
	remove_inode_hash(aspace);
	iput(aspace);
}

/**
 * gfs2_meta_inval - Invalidate all buffers associated with a glock
 * @gl: the glock
 *
 */

void gfs2_meta_inval(struct gfs2_glock *gl)
{
	struct gfs2_sbd *sdp = gl->gl_sbd;
	struct inode *aspace = gl->gl_aspace;
	struct address_space *mapping = gl->gl_aspace->i_mapping;

	gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));

	atomic_inc(&aspace->i_writecount);
	truncate_inode_pages(mapping, 0);
	atomic_dec(&aspace->i_writecount);

	gfs2_assert_withdraw(sdp, !mapping->nrpages);
}

/**
 * gfs2_meta_sync - Sync all buffers associated with a glock
 * @gl: The glock
 *
 */

S
Steven Whitehouse 已提交
116
void gfs2_meta_sync(struct gfs2_glock *gl)
D
David Teigland 已提交
117 118
{
	struct address_space *mapping = gl->gl_aspace->i_mapping;
S
Steven Whitehouse 已提交
119
	int error;
D
David Teigland 已提交
120

S
Steven Whitehouse 已提交
121 122
	filemap_fdatawrite(mapping);
	error = filemap_fdatawait(mapping);
D
David Teigland 已提交
123 124 125 126 127 128 129

	if (error)
		gfs2_io_error(gl->gl_sbd);
}

/**
 * getbuf - Get a buffer with a given address space
130
 * @gl: the glock
D
David Teigland 已提交
131 132 133 134 135 136
 * @blkno: the block number (filesystem scope)
 * @create: 1 if the buffer should be created
 *
 * Returns: the buffer
 */

137
static struct buffer_head *getbuf(struct gfs2_glock *gl, u64 blkno, int create)
D
David Teigland 已提交
138
{
139 140
	struct address_space *mapping = gl->gl_aspace->i_mapping;
	struct gfs2_sbd *sdp = gl->gl_sbd;
D
David Teigland 已提交
141 142 143 144 145 146 147 148 149 150 151 152
	struct page *page;
	struct buffer_head *bh;
	unsigned int shift;
	unsigned long index;
	unsigned int bufnum;

	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 (;;) {
153
			page = grab_cache_page(mapping, index);
D
David Teigland 已提交
154 155 156 157 158
			if (page)
				break;
			yield();
		}
	} else {
159
		page = find_lock_page(mapping, index);
D
David Teigland 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
		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);
	mark_page_accessed(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
 */

202
struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
D
David Teigland 已提交
203 204
{
	struct buffer_head *bh;
205
	bh = getbuf(gl, blkno, CREATE);
D
David Teigland 已提交
206 207 208 209 210 211 212 213
	meta_prep_new(bh);
	return bh;
}

/**
 * gfs2_meta_read - Read a block from disk
 * @gl: The glock covering the block
 * @blkno: The block number
S
Steven Whitehouse 已提交
214
 * @flags: flags
D
David Teigland 已提交
215 216 217 218 219
 * @bhp: the place where the buffer is returned (NULL on failure)
 *
 * Returns: errno
 */

220
int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
D
David Teigland 已提交
221 222
		   struct buffer_head **bhp)
{
223
	*bhp = getbuf(gl, blkno, CREATE);
S
Steven Whitehouse 已提交
224
	if (!buffer_uptodate(*bhp))
225
		ll_rw_block(READ_META, 1, bhp);
S
Steven Whitehouse 已提交
226 227 228 229 230 231 232
	if (flags & DIO_WAIT) {
		int error = gfs2_meta_wait(gl->gl_sbd, *bhp);
		if (error) {
			brelse(*bhp);
			return error;
		}
	}
D
David Teigland 已提交
233

S
Steven Whitehouse 已提交
234
	return 0;
D
David Teigland 已提交
235 236 237
}

/**
S
Steven Whitehouse 已提交
238
 * gfs2_meta_wait - Reread a block from disk
D
David Teigland 已提交
239
 * @sdp: the filesystem
S
Steven Whitehouse 已提交
240
 * @bh: The block to wait for
D
David Teigland 已提交
241 242 243 244
 *
 * Returns: errno
 */

S
Steven Whitehouse 已提交
245
int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
D
David Teigland 已提交
246 247 248 249
{
	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
		return -EIO;

S
Steven Whitehouse 已提交
250
	wait_on_buffer(bh);
D
David Teigland 已提交
251

S
Steven Whitehouse 已提交
252 253 254 255 256
	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 已提交
257
	}
S
Steven Whitehouse 已提交
258 259
	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
		return -EIO;
D
David Teigland 已提交
260 261 262 263 264

	return 0;
}

/**
265
 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
D
David Teigland 已提交
266 267
 * @gl: the glock the buffer belongs to
 * @bh: The buffer to be attached to
268
 * @meta: Flag to indicate whether its metadata or not
D
David Teigland 已提交
269 270
 */

271 272
void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
			 int meta)
D
David Teigland 已提交
273 274 275
{
	struct gfs2_bufdata *bd;

276 277
	if (meta)
		lock_page(bh->b_page);
D
David Teigland 已提交
278

279
	if (bh->b_private) {
280 281
		if (meta)
			unlock_page(bh->b_page);
D
David Teigland 已提交
282 283 284
		return;
	}

285
	bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
D
David Teigland 已提交
286 287 288 289 290
	memset(bd, 0, sizeof(struct gfs2_bufdata));
	bd->bd_bh = bh;
	bd->bd_gl = gl;

	INIT_LIST_HEAD(&bd->bd_list_tr);
S
Steven Whitehouse 已提交
291
	if (meta)
292
		lops_init_le(&bd->bd_le, &gfs2_buf_lops);
S
Steven Whitehouse 已提交
293
	else
294
		lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
295
	bh->b_private = bd;
D
David Teigland 已提交
296

297 298
	if (meta)
		unlock_page(bh->b_page);
D
David Teigland 已提交
299 300 301
}

/**
302
 * gfs2_pin - Pin a buffer in memory
D
David Teigland 已提交
303 304 305 306 307
 * @sdp: the filesystem the buffer belongs to
 * @bh: The buffer to be pinned
 *
 */

308
void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
D
David Teigland 已提交
309
{
310
	struct gfs2_bufdata *bd = bh->b_private;
D
David Teigland 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

	gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));

	if (test_set_buffer_pinned(bh))
		gfs2_assert_withdraw(sdp, 0);

	wait_on_buffer(bh);

	/* If this buffer is in the AIL and it has already been written
	   to in-place disk block, remove it from the AIL. */

	gfs2_log_lock(sdp);
	if (bd->bd_ail && !buffer_in_io(bh))
		list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
	gfs2_log_unlock(sdp);

	clear_buffer_dirty(bh);
	wait_on_buffer(bh);

	if (!buffer_uptodate(bh))
		gfs2_io_error_bh(sdp, bh);

	get_bh(bh);
}

/**
337
 * gfs2_unpin - Unpin a buffer
D
David Teigland 已提交
338 339 340 341 342 343
 * @sdp: the filesystem the buffer belongs to
 * @bh: The buffer to unpin
 * @ai:
 *
 */

344 345
void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
	        struct gfs2_ail *ai)
D
David Teigland 已提交
346
{
347
	struct gfs2_bufdata *bd = bh->b_private;
D
David Teigland 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

	gfs2_assert_withdraw(sdp, buffer_uptodate(bh));

	if (!buffer_pinned(bh))
		gfs2_assert_withdraw(sdp, 0);

	mark_buffer_dirty(bh);
	clear_buffer_pinned(bh);

	gfs2_log_lock(sdp);
	if (bd->bd_ail) {
		list_del(&bd->bd_ail_st_list);
		brelse(bh);
	} else {
		struct gfs2_glock *gl = bd->bd_gl;
		list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
		atomic_inc(&gl->gl_ail_count);
	}
	bd->bd_ail = ai;
	list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
	gfs2_log_unlock(sdp);
}

/**
 * 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
 *
 */

379
void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
D
David Teigland 已提交
380
{
381
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
382 383 384
	struct buffer_head *bh;

	while (blen) {
385
		bh = getbuf(ip->i_gl, bstart, NO_CREATE);
D
David Teigland 已提交
386
		if (bh) {
387
			struct gfs2_bufdata *bd = bh->b_private;
D
David Teigland 已提交
388 389

			if (test_clear_buffer_pinned(bh)) {
390
				struct gfs2_trans *tr = current->journal_info;
D
David Teigland 已提交
391 392 393 394 395
				gfs2_log_lock(sdp);
				list_del_init(&bd->bd_le.le_list);
				gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
				sdp->sd_log_num_buf--;
				gfs2_log_unlock(sdp);
396
				tr->tr_num_buf_rm++;
D
David Teigland 已提交
397 398 399 400 401
				brelse(bh);
			}
			if (bd) {
				gfs2_log_lock(sdp);
				if (bd->bd_ail) {
402
					u64 blkno = bh->b_blocknr;
D
David Teigland 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
					bd->bd_ail = NULL;
					list_del(&bd->bd_ail_st_list);
					list_del(&bd->bd_ail_gl_list);
					atomic_dec(&bd->bd_gl->gl_ail_count);
					brelse(bh);
					gfs2_log_unlock(sdp);
					gfs2_trans_add_revoke(sdp, blkno);
				} else
					gfs2_log_unlock(sdp);
			}

			lock_buffer(bh);
			clear_buffer_dirty(bh);
			clear_buffer_uptodate(bh);
			unlock_buffer(bh);

			brelse(bh);
		}

		bstart++;
		blen--;
	}
}

/**
 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
 * @ip: The GFS2 inode
 *
 * This releases buffers that are in the most-recently-used array of
 * blocks used for indirect block addressing for this inode.
 */

void gfs2_meta_cache_flush(struct gfs2_inode *ip)
{
	struct buffer_head **bh_slot;
	unsigned int x;

	spin_lock(&ip->i_spin);

	for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
		bh_slot = &ip->i_cache[x];
		if (!*bh_slot)
			break;
		brelse(*bh_slot);
		*bh_slot = NULL;
	}

	spin_unlock(&ip->i_spin);
}

/**
 * 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
 * @new: Non-zero if we may create a new buffer
 * @bhp: the buffer is returned here
 *
 * Try to use the gfs2_inode's MRU metadata tree cache.
 *
 * Returns: errno
 */

466
int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
D
David Teigland 已提交
467 468
			      int new, struct buffer_head **bhp)
{
S
Steven Whitehouse 已提交
469 470 471 472
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
	struct gfs2_glock *gl = ip->i_gl;
	struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height;
	int in_cache = 0;
D
David Teigland 已提交
473

474 475 476
	BUG_ON(!gl);
	BUG_ON(!sdp);

D
David Teigland 已提交
477
	spin_lock(&ip->i_spin);
S
Steven Whitehouse 已提交
478 479 480 481
	if (*bh_slot && (*bh_slot)->b_blocknr == num) {
		bh = *bh_slot;
		get_bh(bh);
		in_cache = 1;
D
David Teigland 已提交
482 483 484
	}
	spin_unlock(&ip->i_spin);

S
Steven Whitehouse 已提交
485
	if (!bh)
486
		bh = getbuf(gl, num, CREATE);
D
David Teigland 已提交
487

S
Steven Whitehouse 已提交
488 489
	if (!bh)
		return -ENOBUFS;
D
David Teigland 已提交
490 491

	if (new) {
S
Steven Whitehouse 已提交
492 493 494
		if (gfs2_assert_warn(sdp, height))
			goto err;
		meta_prep_new(bh);
495
		gfs2_trans_add_bh(ip->i_gl, bh, 1);
D
David Teigland 已提交
496 497
		gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
		gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
S
Steven Whitehouse 已提交
498 499 500
	} else {
		u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
		if (!buffer_uptodate(bh)) {
501
			ll_rw_block(READ_META, 1, &bh);
S
Steven Whitehouse 已提交
502 503 504 505 506 507
			if (gfs2_meta_wait(sdp, bh))
				goto err;
		}
		if (gfs2_metatype_check(sdp, bh, mtype))
			goto err;
	}
D
David Teigland 已提交
508

S
Steven Whitehouse 已提交
509 510 511 512 513 514 515
	if (!in_cache) {
		spin_lock(&ip->i_spin);
		if (*bh_slot)
			brelse(*bh_slot);
		*bh_slot = bh;
		get_bh(bh);
		spin_unlock(&ip->i_spin);
D
David Teigland 已提交
516 517 518 519
	}

	*bhp = bh;
	return 0;
S
Steven Whitehouse 已提交
520 521 522
err:
	brelse(bh);
	return -EIO;
D
David Teigland 已提交
523 524 525 526 527 528 529 530
}

/**
 * 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 已提交
531
 * returns: the first buffer in the extent
D
David Teigland 已提交
532 533
 */

S
Steven Whitehouse 已提交
534
struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
D
David Teigland 已提交
535 536 537
{
	struct gfs2_sbd *sdp = gl->gl_sbd;
	struct buffer_head *first_bh, *bh;
538
	u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
539
			  sdp->sd_sb.sb_bsize_shift;
D
David Teigland 已提交
540

S
Steven Whitehouse 已提交
541 542 543 544
	BUG_ON(!extlen);

	if (max_ra < 1)
		max_ra = 1;
D
David Teigland 已提交
545 546 547
	if (extlen > max_ra)
		extlen = max_ra;

548
	first_bh = getbuf(gl, dblock, CREATE);
D
David Teigland 已提交
549 550 551

	if (buffer_uptodate(first_bh))
		goto out;
S
Steven Whitehouse 已提交
552
	if (!buffer_locked(first_bh))
553
		ll_rw_block(READ_META, 1, &first_bh);
D
David Teigland 已提交
554 555 556 557 558

	dblock++;
	extlen--;

	while (extlen) {
559
		bh = getbuf(gl, dblock, CREATE);
D
David Teigland 已提交
560

S
Steven Whitehouse 已提交
561 562 563
		if (!buffer_uptodate(bh) && !buffer_locked(bh))
			ll_rw_block(READA, 1, &bh);
		brelse(bh);
D
David Teigland 已提交
564 565
		dblock++;
		extlen--;
S
Steven Whitehouse 已提交
566 567
		if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
			goto out;
D
David Teigland 已提交
568 569
	}

S
Steven Whitehouse 已提交
570
	wait_on_buffer(first_bh);
571
out:
S
Steven Whitehouse 已提交
572
	return first_bh;
D
David Teigland 已提交
573 574
}