jfs_metapage.c 19.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 *   Copyright (C) International Business Machines Corp., 2000-2005
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5 6
 *   Portions Copyright (C) Christoph Hellwig, 2001-2002
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
Dave Kleikamp's avatar
Dave Kleikamp committed
7
 *   the Free Software Foundation; either version 2 of the License, or
Linus Torvalds's avatar
Linus Torvalds committed
8
 *   (at your option) any later version.
Dave Kleikamp's avatar
Dave Kleikamp committed
9
 *
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14 15
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
Dave Kleikamp's avatar
Dave Kleikamp committed
16
 *   along with this program;  if not, write to the Free Software
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/fs.h>
21
#include <linux/mm.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
22
#include <linux/module.h>
23
#include <linux/bio.h>
24
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
25 26 27
#include <linux/init.h>
#include <linux/buffer_head.h>
#include <linux/mempool.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
28
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "jfs_incore.h"
#include "jfs_superblock.h"
#include "jfs_filsys.h"
#include "jfs_metapage.h"
#include "jfs_txnmgr.h"
#include "jfs_debug.h"

#ifdef CONFIG_JFS_STATISTICS
static struct {
	uint	pagealloc;	/* # of page allocations */
	uint	pagefree;	/* # of page frees */
	uint	lockwait;	/* # of sleeping lock_metapage() calls */
} mpStat;
#endif

44
#define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
45
#define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag)
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48

static inline void unlock_metapage(struct metapage *mp)
{
49
	clear_bit_unlock(META_locked, &mp->flag);
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52
	wake_up(&mp->wait);
}

53
static inline void __lock_metapage(struct metapage *mp)
Linus Torvalds's avatar
Linus Torvalds committed
54 55 56 57 58 59 60
{
	DECLARE_WAITQUEUE(wait, current);
	INCREMENT(mpStat.lockwait);
	add_wait_queue_exclusive(&mp->wait, &wait);
	do {
		set_current_state(TASK_UNINTERRUPTIBLE);
		if (metapage_locked(mp)) {
61
			unlock_page(mp->page);
62
			io_schedule();
63
			lock_page(mp->page);
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66 67 68 69
		}
	} while (trylock_metapage(mp));
	__set_current_state(TASK_RUNNING);
	remove_wait_queue(&mp->wait, &wait);
}

70 71 72
/*
 * Must have mp->page locked
 */
Linus Torvalds's avatar
Linus Torvalds committed
73 74 75 76 77 78 79
static inline void lock_metapage(struct metapage *mp)
{
	if (trylock_metapage(mp))
		__lock_metapage(mp);
}

#define METAPOOL_MIN_PAGES 32
80
static struct kmem_cache *metapage_cache;
Linus Torvalds's avatar
Linus Torvalds committed
81 82
static mempool_t *metapage_mempool;

83 84 85 86 87 88 89 90 91
#define MPS_PER_PAGE (PAGE_CACHE_SIZE >> L2PSIZE)

#if MPS_PER_PAGE > 1

struct meta_anchor {
	int mp_count;
	atomic_t io_count;
	struct metapage *mp[MPS_PER_PAGE];
};
92
#define mp_anchor(page) ((struct meta_anchor *)page_private(page))
93

94
static inline struct metapage *page_to_mp(struct page *page, int offset)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
	if (!PagePrivate(page))
		return NULL;
	return mp_anchor(page)->mp[offset >> L2PSIZE];
}

static inline int insert_metapage(struct page *page, struct metapage *mp)
{
	struct meta_anchor *a;
	int index;
	int l2mp_blocks;	/* log2 blocks per metapage */

	if (PagePrivate(page))
		a = mp_anchor(page);
	else {
Eric Sesterhenn's avatar
Eric Sesterhenn committed
110
		a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS);
111 112
		if (!a)
			return -ENOMEM;
113
		set_page_private(page, (unsigned long)a);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		SetPagePrivate(page);
		kmap(page);
	}

	if (mp) {
		l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
		index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
		a->mp_count++;
		a->mp[index] = mp;
	}

	return 0;
}

static inline void remove_metapage(struct page *page, struct metapage *mp)
{
	struct meta_anchor *a = mp_anchor(page);
	int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
	int index;

	index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);

	BUG_ON(a->mp[index] != mp);

	a->mp[index] = NULL;
	if (--a->mp_count == 0) {
		kfree(a);
141
		set_page_private(page, 0);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		ClearPagePrivate(page);
		kunmap(page);
	}
}

static inline void inc_io(struct page *page)
{
	atomic_inc(&mp_anchor(page)->io_count);
}

static inline void dec_io(struct page *page, void (*handler) (struct page *))
{
	if (atomic_dec_and_test(&mp_anchor(page)->io_count))
		handler(page);
}

#else
159
static inline struct metapage *page_to_mp(struct page *page, int offset)
160
{
161
	return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL;
162 163 164 165 166
}

static inline int insert_metapage(struct page *page, struct metapage *mp)
{
	if (mp) {
167
		set_page_private(page, (unsigned long)mp);
168 169 170 171 172 173 174 175
		SetPagePrivate(page);
		kmap(page);
	}
	return 0;
}

static inline void remove_metapage(struct page *page, struct metapage *mp)
{
176
	set_page_private(page, 0);
177 178 179 180 181 182 183 184 185
	ClearPagePrivate(page);
	kunmap(page);
}

#define inc_io(page) do {} while(0)
#define dec_io(page, handler) handler(page)

#endif

Al Viro's avatar
Al Viro committed
186
static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
Linus Torvalds's avatar
Linus Torvalds committed
187
{
188 189 190 191 192 193 194 195 196 197 198
	struct metapage *mp = mempool_alloc(metapage_mempool, gfp_mask);

	if (mp) {
		mp->lid = 0;
		mp->lsn = 0;
		mp->data = NULL;
		mp->clsn = 0;
		mp->log = NULL;
		init_waitqueue_head(&mp->wait);
	}
	return mp;
Linus Torvalds's avatar
Linus Torvalds committed
199 200 201 202 203 204 205 206 207 208 209 210 211
}

static inline void free_metapage(struct metapage *mp)
{
	mempool_free(mp, metapage_mempool);
}

int __init metapage_init(void)
{
	/*
	 * Allocate the metapage structures
	 */
	metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
212
					   0, 0, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
213 214 215
	if (metapage_cache == NULL)
		return -ENOMEM;

216 217
	metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES,
						    metapage_cache);
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

	if (metapage_mempool == NULL) {
		kmem_cache_destroy(metapage_cache);
		return -ENOMEM;
	}

	return 0;
}

void metapage_exit(void)
{
	mempool_destroy(metapage_mempool);
	kmem_cache_destroy(metapage_cache);
}

233 234 235 236 237 238 239 240 241 242
static inline void drop_metapage(struct page *page, struct metapage *mp)
{
	if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
	    test_bit(META_io, &mp->flag))
		return;
	remove_metapage(page, mp);
	INCREMENT(mpStat.pagefree);
	free_metapage(mp);
}

Linus Torvalds's avatar
Linus Torvalds committed
243
/*
244
 * Metapage address space operations
Linus Torvalds's avatar
Linus Torvalds committed
245
 */
246 247

static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
248
				    int *len)
Linus Torvalds's avatar
Linus Torvalds committed
249
{
250 251 252
	int rc = 0;
	int xflag;
	s64 xaddr;
253
	sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
			       inode->i_blkbits;

	if (lblock >= file_blocks)
		return 0;
	if (lblock + *len > file_blocks)
		*len = file_blocks - lblock;

	if (inode->i_ino) {
		rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
		if ((rc == 0) && *len)
			lblock = (sector_t)xaddr;
		else
			lblock = 0;
	} /* else no mapping */

	return lblock;
Linus Torvalds's avatar
Linus Torvalds committed
270 271
}

272
static void last_read_complete(struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
273
{
274 275 276 277 278
	if (!PageError(page))
		SetPageUptodate(page);
	unlock_page(page);
}

279
static void metapage_read_end_io(struct bio *bio)
280 281 282
{
	struct page *page = bio->bi_private;

283
	if (bio->bi_error) {
284 285
		printk(KERN_ERR "metapage_read_end_io: I/O error\n");
		SetPageError(page);
Linus Torvalds's avatar
Linus Torvalds committed
286 287
	}

288 289
	dec_io(page, last_read_complete);
	bio_put(bio);
Linus Torvalds's avatar
Linus Torvalds committed
290 291
}

292
static void remove_from_logsync(struct metapage *mp)
Linus Torvalds's avatar
Linus Torvalds committed
293
{
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	struct jfs_log *log = mp->log;
	unsigned long flags;
/*
 * This can race.  Recheck that log hasn't been set to null, and after
 * acquiring logsync lock, recheck lsn
 */
	if (!log)
		return;

	LOGSYNC_LOCK(log, flags);
	if (mp->lsn) {
		mp->log = NULL;
		mp->lsn = 0;
		mp->clsn = 0;
		log->count--;
		list_del(&mp->synclist);
	}
	LOGSYNC_UNLOCK(log, flags);
}
Linus Torvalds's avatar
Linus Torvalds committed
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static void last_write_complete(struct page *page)
{
	struct metapage *mp;
	unsigned int offset;

	for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
		mp = page_to_mp(page, offset);
		if (mp && test_bit(META_io, &mp->flag)) {
			if (mp->lsn)
				remove_from_logsync(mp);
			clear_bit(META_io, &mp->flag);
		}
		/*
		 * I'd like to call drop_metapage here, but I don't think it's
		 * safe unless I have the page locked
		 */
	}
	end_page_writeback(page);
Linus Torvalds's avatar
Linus Torvalds committed
332 333
}

334
static void metapage_write_end_io(struct bio *bio)
Linus Torvalds's avatar
Linus Torvalds committed
335
{
336 337 338 339
	struct page *page = bio->bi_private;

	BUG_ON(!PagePrivate(page));

340
	if (bio->bi_error) {
341 342 343 344 345 346 347 348 349 350
		printk(KERN_ERR "metapage_write_end_io: I/O error\n");
		SetPageError(page);
	}
	dec_io(page, last_write_complete);
	bio_put(bio);
}

static int metapage_writepage(struct page *page, struct writeback_control *wbc)
{
	struct bio *bio = NULL;
351
	int block_offset;	/* block offset of mp within page */
352
	struct inode *inode = page->mapping->host;
353 354 355
	int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
	int len;
	int xlen;
356 357 358
	struct metapage *mp;
	int redirty = 0;
	sector_t lblock;
359
	int nr_underway = 0;
360 361 362 363 364
	sector_t pblock;
	sector_t next_block = 0;
	sector_t page_start;
	unsigned long bio_bytes = 0;
	unsigned long bio_offset = 0;
365
	int offset;
366
	int bad_blocks = 0;
367 368 369 370 371

	page_start = (sector_t)page->index <<
		     (PAGE_CACHE_SHIFT - inode->i_blkbits);
	BUG_ON(!PageLocked(page));
	BUG_ON(PageWriteback(page));
372
	set_page_writeback(page);
373 374 375 376 377 378 379 380 381

	for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
		mp = page_to_mp(page, offset);

		if (!mp || !test_bit(META_dirty, &mp->flag))
			continue;

		if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
			redirty = 1;
382 383 384 385 386 387
			/*
			 * Make sure this page isn't blocked indefinitely.
			 * If the journal isn't undergoing I/O, push it
			 */
			if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
				jfs_flush_journal(mp->log, 0);
388 389 390 391
			continue;
		}

		clear_bit(META_dirty, &mp->flag);
392
		set_bit(META_io, &mp->flag);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		block_offset = offset >> inode->i_blkbits;
		lblock = page_start + block_offset;
		if (bio) {
			if (xlen && lblock == next_block) {
				/* Contiguous, in memory & on disk */
				len = min(xlen, blocks_per_mp);
				xlen -= len;
				bio_bytes += len << inode->i_blkbits;
				continue;
			}
			/* Not contiguous */
			if (bio_add_page(bio, page, bio_bytes, bio_offset) <
			    bio_bytes)
				goto add_failed;
			/*
			 * Increment counter before submitting i/o to keep
			 * count from hitting zero before we're through
			 */
			inc_io(page);
412
			if (!bio->bi_iter.bi_size)
413 414
				goto dump_bio;
			submit_bio(WRITE, bio);
415
			nr_underway++;
416
			bio = NULL;
417
		} else
418 419 420 421 422
			inc_io(page);
		xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
		pblock = metapage_get_blocks(inode, lblock, &xlen);
		if (!pblock) {
			printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
423 424 425 426 427
			/*
			 * We already called inc_io(), but can't cancel it
			 * with dec_io() until we're done with the page
			 */
			bad_blocks++;
428 429
			continue;
		}
430
		len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage);
431 432 433

		bio = bio_alloc(GFP_NOFS, 1);
		bio->bi_bdev = inode->i_sb->s_bdev;
434
		bio->bi_iter.bi_sector = pblock << (inode->i_blkbits - 9);
435 436 437 438 439 440 441 442 443 444 445 446 447
		bio->bi_end_io = metapage_write_end_io;
		bio->bi_private = page;

		/* Don't call bio_add_page yet, we may add to this vec */
		bio_offset = offset;
		bio_bytes = len << inode->i_blkbits;

		xlen -= len;
		next_block = lblock + len;
	}
	if (bio) {
		if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
				goto add_failed;
448
		if (!bio->bi_iter.bi_size)
449
			goto dump_bio;
Dave Kleikamp's avatar
Dave Kleikamp committed
450

451
		submit_bio(WRITE, bio);
452
		nr_underway++;
453 454 455 456 457 458
	}
	if (redirty)
		redirty_page_for_writepage(wbc, page);

	unlock_page(page);

459 460 461
	if (bad_blocks)
		goto err_out;

462 463 464
	if (nr_underway == 0)
		end_page_writeback(page);

465 466 467 468 469 470
	return 0;
add_failed:
	/* We should never reach here, since we're only adding one vec */
	printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
	goto skip;
dump_bio:
471 472
	print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16,
		       4, bio, sizeof(*bio), 0);
473 474 475 476
skip:
	bio_put(bio);
	unlock_page(page);
	dec_io(page, last_write_complete);
477 478 479
err_out:
	while (bad_blocks--)
		dec_io(page, last_write_complete);
480 481 482 483 484 485 486
	return -EIO;
}

static int metapage_readpage(struct file *fp, struct page *page)
{
	struct inode *inode = page->mapping->host;
	struct bio *bio = NULL;
487 488
	int block_offset;
	int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
489 490
	sector_t page_start;	/* address of page in fs blocks */
	sector_t pblock;
491
	int xlen;
492
	unsigned int len;
493
	int offset;
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

	BUG_ON(!PageLocked(page));
	page_start = (sector_t)page->index <<
		     (PAGE_CACHE_SHIFT - inode->i_blkbits);

	block_offset = 0;
	while (block_offset < blocks_per_page) {
		xlen = blocks_per_page - block_offset;
		pblock = metapage_get_blocks(inode, page_start + block_offset,
					     &xlen);
		if (pblock) {
			if (!PagePrivate(page))
				insert_metapage(page, NULL);
			inc_io(page);
			if (bio)
				submit_bio(READ, bio);

			bio = bio_alloc(GFP_NOFS, 1);
			bio->bi_bdev = inode->i_sb->s_bdev;
513 514
			bio->bi_iter.bi_sector =
				pblock << (inode->i_blkbits - 9);
515 516 517 518 519 520 521 522 523
			bio->bi_end_io = metapage_read_end_io;
			bio->bi_private = page;
			len = xlen << inode->i_blkbits;
			offset = block_offset << inode->i_blkbits;
			if (bio_add_page(bio, page, len, offset) < len)
				goto add_failed;
			block_offset += xlen;
		} else
			block_offset++;
Linus Torvalds's avatar
Linus Torvalds committed
524
	}
525 526 527 528 529 530
	if (bio)
		submit_bio(READ, bio);
	else
		unlock_page(page);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
531

532 533 534 535 536
add_failed:
	printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
	bio_put(bio);
	dec_io(page, last_read_complete);
	return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
537 538
}

Al Viro's avatar
Al Viro committed
539
static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
540 541
{
	struct metapage *mp;
542
	int ret = 1;
543
	int offset;
544 545 546 547 548 549 550 551

	for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
		mp = page_to_mp(page, offset);

		if (!mp)
			continue;

		jfs_info("metapage_releasepage: mp = 0x%p", mp);
552 553
		if (mp->count || mp->nohomeok ||
		    test_bit(META_dirty, &mp->flag)) {
554 555
			jfs_info("count = %ld, nohomeok = %d", mp->count,
				 mp->nohomeok);
556
			ret = 0;
557 558 559 560 561 562 563 564
			continue;
		}
		if (mp->lsn)
			remove_from_logsync(mp);
		remove_metapage(page, mp);
		INCREMENT(mpStat.pagefree);
		free_metapage(mp);
	}
565
	return ret;
566 567
}

568 569
static void metapage_invalidatepage(struct page *page, unsigned int offset,
				    unsigned int length)
570
{
571
	BUG_ON(offset || length < PAGE_CACHE_SIZE);
572

573
	BUG_ON(PageWriteback(page));
574

575
	metapage_releasepage(page, 0);
576 577
}

578
const struct address_space_operations jfs_metapage_aops = {
579 580 581 582 583 584 585
	.readpage	= metapage_readpage,
	.writepage	= metapage_writepage,
	.releasepage	= metapage_releasepage,
	.invalidatepage	= metapage_invalidatepage,
	.set_page_dirty	= __set_page_dirty_nobuffers,
};

Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590 591 592
struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
				unsigned int size, int absolute,
				unsigned long new)
{
	int l2BlocksPerPage;
	int l2bsize;
	struct address_space *mapping;
593 594
	struct metapage *mp = NULL;
	struct page *page;
Linus Torvalds's avatar
Linus Torvalds committed
595 596 597
	unsigned long page_index;
	unsigned long page_offset;

598 599 600 601 602 603 604 605 606 607 608 609 610
	jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
		 inode->i_ino, lblock, absolute);

	l2bsize = inode->i_blkbits;
	l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
	page_index = lblock >> l2BlocksPerPage;
	page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
	if ((page_offset + size) > PAGE_CACHE_SIZE) {
		jfs_err("MetaData crosses page boundary!!");
		jfs_err("lblock = %lx, size  = %d", lblock, size);
		dump_stack();
		return NULL;
	}
Linus Torvalds's avatar
Linus Torvalds committed
611
	if (absolute)
612
		mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds's avatar
Linus Torvalds committed
613 614 615 616 617 618 619 620 621 622 623
	else {
		/*
		 * If an nfs client tries to read an inode that is larger
		 * than any existing inodes, we may try to read past the
		 * end of the inode map
		 */
		if ((lblock << inode->i_blkbits) >= inode->i_size)
			return NULL;
		mapping = inode->i_mapping;
	}

624 625 626 627 628 629 630 631
	if (new && (PSIZE == PAGE_CACHE_SIZE)) {
		page = grab_cache_page(mapping, page_index);
		if (!page) {
			jfs_err("grab_cache_page failed!");
			return NULL;
		}
		SetPageUptodate(page);
	} else {
632
		page = read_mapping_page(mapping, page_index, NULL);
Dave Kleikamp's avatar
Dave Kleikamp committed
633
		if (IS_ERR(page) || !PageUptodate(page)) {
634
			jfs_err("read_mapping_page failed!");
635 636 637 638 639 640
			return NULL;
		}
		lock_page(page);
	}

	mp = page_to_mp(page, page_offset);
Linus Torvalds's avatar
Linus Torvalds committed
641
	if (mp) {
642 643
		if (mp->logical_size != size) {
			jfs_error(inode->i_sb,
Joe Perches's avatar
Joe Perches committed
644
				  "get_mp->logical_size != size\n");
645 646 647
			jfs_err("logical_size = %d, size = %d",
				mp->logical_size, size);
			dump_stack();
Dave Kleikamp's avatar
Dave Kleikamp committed
648
			goto unlock;
Linus Torvalds's avatar
Linus Torvalds committed
649 650 651 652 653 654
		}
		mp->count++;
		lock_metapage(mp);
		if (test_bit(META_discard, &mp->flag)) {
			if (!new) {
				jfs_error(inode->i_sb,
Joe Perches's avatar
Joe Perches committed
655
					  "using a discarded metapage\n");
656
				discard_metapage(mp);
Dave Kleikamp's avatar
Dave Kleikamp committed
657
				goto unlock;
Linus Torvalds's avatar
Linus Torvalds committed
658 659 660 661
			}
			clear_bit(META_discard, &mp->flag);
		}
	} else {
662 663 664
		INCREMENT(mpStat.pagealloc);
		mp = alloc_metapage(GFP_NOFS);
		mp->page = page;
Linus Torvalds's avatar
Linus Torvalds committed
665 666 667
		mp->flag = 0;
		mp->xflag = COMMIT_PAGE;
		mp->count = 1;
668
		mp->nohomeok = 0;
Linus Torvalds's avatar
Linus Torvalds committed
669
		mp->logical_size = size;
670 671 672 673 674
		mp->data = page_address(page) + page_offset;
		mp->index = lblock;
		if (unlikely(insert_metapage(page, mp))) {
			free_metapage(mp);
			goto unlock;
Linus Torvalds's avatar
Linus Torvalds committed
675
		}
676
		lock_metapage(mp);
Linus Torvalds's avatar
Linus Torvalds committed
677 678
	}

679 680
	if (new) {
		jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds's avatar
Linus Torvalds committed
681
		memset(mp->data, 0, PSIZE);
682
	}
Linus Torvalds's avatar
Linus Torvalds committed
683

684 685
	unlock_page(page);
	jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds's avatar
Linus Torvalds committed
686 687
	return mp;

688 689
unlock:
	unlock_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
690 691 692
	return NULL;
}

693
void grab_metapage(struct metapage * mp)
Linus Torvalds's avatar
Linus Torvalds committed
694
{
695 696 697
	jfs_info("grab_metapage: mp = 0x%p", mp);
	page_cache_get(mp->page);
	lock_page(mp->page);
Linus Torvalds's avatar
Linus Torvalds committed
698
	mp->count++;
699 700
	lock_metapage(mp);
	unlock_page(mp->page);
Linus Torvalds's avatar
Linus Torvalds committed
701 702
}

703
void force_metapage(struct metapage *mp)
Linus Torvalds's avatar
Linus Torvalds committed
704
{
705 706 707 708 709 710 711 712 713 714 715
	struct page *page = mp->page;
	jfs_info("force_metapage: mp = 0x%p", mp);
	set_bit(META_forcewrite, &mp->flag);
	clear_bit(META_sync, &mp->flag);
	page_cache_get(page);
	lock_page(page);
	set_page_dirty(page);
	write_one_page(page, 1);
	clear_bit(META_forcewrite, &mp->flag);
	page_cache_release(page);
}
Linus Torvalds's avatar
Linus Torvalds committed
716

717
void hold_metapage(struct metapage *mp)
718
{
Linus Torvalds's avatar
Linus Torvalds committed
719
	lock_page(mp->page);
720 721
}

722
void put_metapage(struct metapage *mp)
723 724 725
{
	if (mp->count || mp->nohomeok) {
		/* Someone else will release this */
Linus Torvalds's avatar
Linus Torvalds committed
726 727 728
		unlock_page(mp->page);
		return;
	}
729 730 731
	page_cache_get(mp->page);
	mp->count++;
	lock_metapage(mp);
Linus Torvalds's avatar
Linus Torvalds committed
732
	unlock_page(mp->page);
733
	release_metapage(mp);
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737
}

void release_metapage(struct metapage * mp)
{
738
	struct page *page = mp->page;
Linus Torvalds's avatar
Linus Torvalds committed
739 740
	jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);

741 742 743 744
	BUG_ON(!page);

	lock_page(page);
	unlock_metapage(mp);
Linus Torvalds's avatar
Linus Torvalds committed
745 746

	assert(mp->count);
747 748 749
	if (--mp->count || mp->nohomeok) {
		unlock_page(page);
		page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
750 751 752
		return;
	}

753 754
	if (test_bit(META_dirty, &mp->flag)) {
		set_page_dirty(page);
Linus Torvalds's avatar
Linus Torvalds committed
755 756
		if (test_bit(META_sync, &mp->flag)) {
			clear_bit(META_sync, &mp->flag);
757 758
			write_one_page(page, 1);
			lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds's avatar
Linus Torvalds committed
759
		}
760 761
	} else if (mp->lsn)	/* discard_metapage doesn't remove it */
		remove_from_logsync(mp);
Linus Torvalds's avatar
Linus Torvalds committed
762

763 764
	/* Try to keep metapages from using up too much memory */
	drop_metapage(page, mp);
765

766 767
	unlock_page(page);
	page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770 771
}

void __invalidate_metapages(struct inode *ip, s64 addr, int len)
{
772
	sector_t lblock;
Linus Torvalds's avatar
Linus Torvalds committed
773
	int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
774
	int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds's avatar
Linus Torvalds committed
775
	/* All callers are interested in block device's mapping */
776 777
	struct address_space *mapping =
		JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds's avatar
Linus Torvalds committed
778 779
	struct metapage *mp;
	struct page *page;
780
	unsigned int offset;
Linus Torvalds's avatar
Linus Torvalds committed
781 782

	/*
783
	 * Mark metapages to discard.  They will eventually be
Linus Torvalds's avatar
Linus Torvalds committed
784 785
	 * released, but should not be written.
	 */
786 787 788 789 790 791 792 793 794 795 796 797 798
	for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
	     lblock += BlocksPerPage) {
		page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
		if (!page)
			continue;
		for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
			mp = page_to_mp(page, offset);
			if (!mp)
				continue;
			if (mp->index < addr)
				continue;
			if (mp->index >= addr + len)
				break;
Linus Torvalds's avatar
Linus Torvalds committed
799 800 801

			clear_bit(META_dirty, &mp->flag);
			set_bit(META_discard, &mp->flag);
802 803
			if (mp->lsn)
				remove_from_logsync(mp);
Linus Torvalds's avatar
Linus Torvalds committed
804
		}
805 806
		unlock_page(page);
		page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
807 808 809 810
	}
}

#ifdef CONFIG_JFS_STATISTICS
Alexey Dobriyan's avatar
Alexey Dobriyan committed
811
static int jfs_mpstat_proc_show(struct seq_file *m, void *v)
Linus Torvalds's avatar
Linus Torvalds committed
812
{
Alexey Dobriyan's avatar
Alexey Dobriyan committed
813
	seq_printf(m,
Linus Torvalds's avatar
Linus Torvalds committed
814 815 816 817 818 819 820 821
		       "JFS Metapage statistics\n"
		       "=======================\n"
		       "page allocations = %d\n"
		       "page frees = %d\n"
		       "lock waits = %d\n",
		       mpStat.pagealloc,
		       mpStat.pagefree,
		       mpStat.lockwait);
Alexey Dobriyan's avatar
Alexey Dobriyan committed
822 823
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
824

Alexey Dobriyan's avatar
Alexey Dobriyan committed
825 826 827
static int jfs_mpstat_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, jfs_mpstat_proc_show, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
828
}
Alexey Dobriyan's avatar
Alexey Dobriyan committed
829 830 831 832 833 834 835 836

const struct file_operations jfs_mpstat_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= jfs_mpstat_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
Linus Torvalds's avatar
Linus Torvalds committed
837
#endif