meta_io.c 9.9 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"
S
Steven Whitehouse 已提交
34
#include "ops_address.h"
D
David Teigland 已提交
35 36 37 38

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

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

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

/**
 * 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;
71
	struct gfs2_inode *ip;
D
David Teigland 已提交
72 73 74

	aspace = new_inode(sdp->sd_vfs);
	if (aspace) {
75
		mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
D
David Teigland 已提交
76 77
		aspace->i_mapping->a_ops = &aspace_aops;
		aspace->i_size = ~0ULL;
78 79
		ip = GFS2_I(aspace);
		clear_bit(GIF_USER, &ip->i_flags);
D
David Teigland 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		insert_inode_hash(aspace);
	}
	return aspace;
}

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

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

S
Steven Whitehouse 已提交
97
void gfs2_meta_sync(struct gfs2_glock *gl)
D
David Teigland 已提交
98 99
{
	struct address_space *mapping = gl->gl_aspace->i_mapping;
S
Steven Whitehouse 已提交
100
	int error;
D
David Teigland 已提交
101

S
Steven Whitehouse 已提交
102 103
	filemap_fdatawrite(mapping);
	error = filemap_fdatawait(mapping);
D
David Teigland 已提交
104 105 106 107 108 109

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

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

118
struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
D
David Teigland 已提交
119
{
120 121
	struct address_space *mapping = gl->gl_aspace->i_mapping;
	struct gfs2_sbd *sdp = gl->gl_sbd;
D
David Teigland 已提交
122 123 124 125 126 127 128 129 130 131 132 133
	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 (;;) {
134
			page = grab_cache_page(mapping, index);
D
David Teigland 已提交
135 136 137 138 139
			if (page)
				break;
			yield();
		}
	} else {
140
		page = find_lock_page(mapping, index);
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 182
		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
 */

183
struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
D
David Teigland 已提交
184 185
{
	struct buffer_head *bh;
186
	bh = gfs2_getbuf(gl, blkno, CREATE);
D
David Teigland 已提交
187 188 189 190 191 192 193 194
	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 已提交
195
 * @flags: flags
D
David Teigland 已提交
196 197 198 199 200
 * @bhp: the place where the buffer is returned (NULL on failure)
 *
 * Returns: errno
 */

201
int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
D
David Teigland 已提交
202 203
		   struct buffer_head **bhp)
{
S
Steven Whitehouse 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	struct gfs2_sbd *sdp = gl->gl_sbd;
	struct buffer_head *bh;

	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
		return -EIO;

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

	lock_buffer(bh);
	if (buffer_uptodate(bh)) {
		unlock_buffer(bh);
		return 0;
	}
	bh->b_end_io = end_buffer_read_sync;
	get_bh(bh);
	submit_bh(READ_SYNC | (1 << BIO_RW_META), bh);
	if (!(flags & DIO_WAIT))
		return 0;

	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);
		return -EIO;
S
Steven Whitehouse 已提交
230
	}
D
David Teigland 已提交
231

S
Steven Whitehouse 已提交
232
	return 0;
D
David Teigland 已提交
233 234 235
}

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

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

S
Steven Whitehouse 已提交
248
	wait_on_buffer(bh);
D
David Teigland 已提交
249

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

	return 0;
}

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

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

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

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

B
Bob Peterson 已提交
283
	bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
D
David Teigland 已提交
284 285 286 287
	bd->bd_bh = bh;
	bd->bd_gl = gl;

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

294 295
	if (meta)
		unlock_page(bh->b_page);
D
David Teigland 已提交
296 297
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
void gfs2_remove_from_journal(struct buffer_head *bh, struct gfs2_trans *tr, int meta)
{
	struct gfs2_sbd *sdp = GFS2_SB(bh->b_page->mapping->host);
	struct gfs2_bufdata *bd = bh->b_private;
	if (test_clear_buffer_pinned(bh)) {
		list_del_init(&bd->bd_le.le_list);
		if (meta) {
			gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
			sdp->sd_log_num_buf--;
			tr->tr_num_buf_rm++;
		} else {
			gfs2_assert_warn(sdp, sdp->sd_log_num_databuf);
			sdp->sd_log_num_databuf--;
			tr->tr_num_databuf_rm++;
		}
		tr->tr_touched = 1;
		brelse(bh);
	}
	if (bd) {
		if (bd->bd_ail) {
318
			gfs2_remove_from_ail(bd);
319 320 321 322 323 324 325 326 327 328
			bh->b_private = NULL;
			bd->bd_bh = NULL;
			bd->bd_blkno = bh->b_blocknr;
			gfs2_trans_add_revoke(sdp, bd);
		}
	}
	clear_buffer_dirty(bh);
	clear_buffer_uptodate(bh);
}

D
David Teigland 已提交
329 330 331 332 333 334 335 336
/**
 * 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
 *
 */

337
void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
D
David Teigland 已提交
338
{
339
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
D
David Teigland 已提交
340 341 342
	struct buffer_head *bh;

	while (blen) {
343
		bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
D
David Teigland 已提交
344
		if (bh) {
345 346
			lock_buffer(bh);
			gfs2_log_lock(sdp);
347
			gfs2_remove_from_journal(bh, current->journal_info, 1);
348
			gfs2_log_unlock(sdp);
D
David Teigland 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			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
 * @new: Non-zero if we may create a new buffer
 * @bhp: the buffer is returned here
 *
 * Returns: errno
 */

369
int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
D
David Teigland 已提交
370 371
			      int new, struct buffer_head **bhp)
{
S
Steven Whitehouse 已提交
372 373
	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
	struct gfs2_glock *gl = ip->i_gl;
374 375
	struct buffer_head *bh;
	int ret = 0;
D
David Teigland 已提交
376 377

	if (new) {
378 379
		BUG_ON(height == 0);
		bh = gfs2_meta_new(gl, num);
380
		gfs2_trans_add_bh(ip->i_gl, bh, 1);
D
David Teigland 已提交
381 382
		gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
		gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
S
Steven Whitehouse 已提交
383 384
	} else {
		u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
385 386 387 388
		ret = gfs2_meta_read(gl, num, DIO_WAIT, &bh);
		if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
			brelse(bh);
			ret = -EIO;
S
Steven Whitehouse 已提交
389
		}
D
David Teigland 已提交
390 391
	}
	*bhp = bh;
392
	return ret;
D
David Teigland 已提交
393 394 395 396 397 398 399 400
}

/**
 * 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 已提交
401
 * returns: the first buffer in the extent
D
David Teigland 已提交
402 403
 */

S
Steven Whitehouse 已提交
404
struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
D
David Teigland 已提交
405 406 407
{
	struct gfs2_sbd *sdp = gl->gl_sbd;
	struct buffer_head *first_bh, *bh;
408
	u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
409
			  sdp->sd_sb.sb_bsize_shift;
D
David Teigland 已提交
410

S
Steven Whitehouse 已提交
411 412 413 414
	BUG_ON(!extlen);

	if (max_ra < 1)
		max_ra = 1;
D
David Teigland 已提交
415 416 417
	if (extlen > max_ra)
		extlen = max_ra;

418
	first_bh = gfs2_getbuf(gl, dblock, CREATE);
D
David Teigland 已提交
419 420 421

	if (buffer_uptodate(first_bh))
		goto out;
S
Steven Whitehouse 已提交
422
	if (!buffer_locked(first_bh))
S
Steven Whitehouse 已提交
423
		ll_rw_block(READ_SYNC | (1 << BIO_RW_META), 1, &first_bh);
D
David Teigland 已提交
424 425 426 427 428

	dblock++;
	extlen--;

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

S
Steven Whitehouse 已提交
431 432 433
		if (!buffer_uptodate(bh) && !buffer_locked(bh))
			ll_rw_block(READA, 1, &bh);
		brelse(bh);
D
David Teigland 已提交
434 435
		dblock++;
		extlen--;
S
Steven Whitehouse 已提交
436 437
		if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
			goto out;
D
David Teigland 已提交
438 439
	}

S
Steven Whitehouse 已提交
440
	wait_on_buffer(first_bh);
441
out:
S
Steven Whitehouse 已提交
442
	return first_bh;
D
David Teigland 已提交
443 444
}