blob: f5cd8d38af7a9ff7af5ac2f4627d73964ccd5814 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -06002 * Copyright (C) International Business Machines Corp., 2000-2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Portions Copyright (C) Christoph Hellwig, 2001-2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
Dave Kleikamp63f83c92006-10-02 09:55:27 -05007 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * (at your option) any later version.
Dave Kleikamp63f83c92006-10-02 09:55:27 -05009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Dave Kleikamp63f83c92006-10-02 09:55:27 -050016 * along with this program; if not, write to the Free Software
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/fs.h>
Dave Kleikamp7fab4792005-05-02 12:25:02 -060021#include <linux/mm.h>
22#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/buffer_head.h>
25#include <linux/mempool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "jfs_incore.h"
27#include "jfs_superblock.h"
28#include "jfs_filsys.h"
29#include "jfs_metapage.h"
30#include "jfs_txnmgr.h"
31#include "jfs_debug.h"
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifdef CONFIG_JFS_STATISTICS
34static struct {
35 uint pagealloc; /* # of page allocations */
36 uint pagefree; /* # of page frees */
37 uint lockwait; /* # of sleeping lock_metapage() calls */
38} mpStat;
39#endif
40
Dave Kleikamp7fab4792005-05-02 12:25:02 -060041#define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
42#define trylock_metapage(mp) test_and_set_bit(META_locked, &(mp)->flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static inline void unlock_metapage(struct metapage *mp)
45{
46 clear_bit(META_locked, &mp->flag);
47 wake_up(&mp->wait);
48}
49
Dave Kleikamp7fab4792005-05-02 12:25:02 -060050static inline void __lock_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 INCREMENT(mpStat.lockwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 add_wait_queue_exclusive(&mp->wait, &wait);
55 do {
56 set_current_state(TASK_UNINTERRUPTIBLE);
57 if (metapage_locked(mp)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -060058 unlock_page(mp->page);
Dave Kleikamp4aa0d232007-01-17 21:18:35 -060059 io_schedule();
Dave Kleikamp7fab4792005-05-02 12:25:02 -060060 lock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 } while (trylock_metapage(mp));
63 __set_current_state(TASK_RUNNING);
64 remove_wait_queue(&mp->wait, &wait);
65}
66
Dave Kleikamp7fab4792005-05-02 12:25:02 -060067/*
68 * Must have mp->page locked
69 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static inline void lock_metapage(struct metapage *mp)
71{
72 if (trylock_metapage(mp))
73 __lock_metapage(mp);
74}
75
76#define METAPOOL_MIN_PAGES 32
Christoph Lametere18b8902006-12-06 20:33:20 -080077static struct kmem_cache *metapage_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static mempool_t *metapage_mempool;
79
Dave Kleikamp7fab4792005-05-02 12:25:02 -060080#define MPS_PER_PAGE (PAGE_CACHE_SIZE >> L2PSIZE)
81
82#if MPS_PER_PAGE > 1
83
84struct meta_anchor {
85 int mp_count;
86 atomic_t io_count;
87 struct metapage *mp[MPS_PER_PAGE];
88};
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070089#define mp_anchor(page) ((struct meta_anchor *)page_private(page))
Dave Kleikamp7fab4792005-05-02 12:25:02 -060090
91static inline struct metapage *page_to_mp(struct page *page, uint offset)
92{
93 if (!PagePrivate(page))
94 return NULL;
95 return mp_anchor(page)->mp[offset >> L2PSIZE];
96}
97
98static inline int insert_metapage(struct page *page, struct metapage *mp)
99{
100 struct meta_anchor *a;
101 int index;
102 int l2mp_blocks; /* log2 blocks per metapage */
103
104 if (PagePrivate(page))
105 a = mp_anchor(page);
106 else {
Eric Sesterhenn5b3030e2006-02-23 09:47:13 -0600107 a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600108 if (!a)
109 return -ENOMEM;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700110 set_page_private(page, (unsigned long)a);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600111 SetPagePrivate(page);
112 kmap(page);
113 }
114
115 if (mp) {
116 l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
117 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
118 a->mp_count++;
119 a->mp[index] = mp;
120 }
121
122 return 0;
123}
124
125static inline void remove_metapage(struct page *page, struct metapage *mp)
126{
127 struct meta_anchor *a = mp_anchor(page);
128 int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
129 int index;
130
131 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
132
133 BUG_ON(a->mp[index] != mp);
134
135 a->mp[index] = NULL;
136 if (--a->mp_count == 0) {
137 kfree(a);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700138 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600139 ClearPagePrivate(page);
140 kunmap(page);
141 }
142}
143
144static inline void inc_io(struct page *page)
145{
146 atomic_inc(&mp_anchor(page)->io_count);
147}
148
149static inline void dec_io(struct page *page, void (*handler) (struct page *))
150{
151 if (atomic_dec_and_test(&mp_anchor(page)->io_count))
152 handler(page);
153}
154
155#else
156static inline struct metapage *page_to_mp(struct page *page, uint offset)
157{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700158 return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600159}
160
161static inline int insert_metapage(struct page *page, struct metapage *mp)
162{
163 if (mp) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700164 set_page_private(page, (unsigned long)mp);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600165 SetPagePrivate(page);
166 kmap(page);
167 }
168 return 0;
169}
170
171static inline void remove_metapage(struct page *page, struct metapage *mp)
172{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700173 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600174 ClearPagePrivate(page);
175 kunmap(page);
176}
177
178#define inc_io(page) do {} while(0)
179#define dec_io(page, handler) handler(page)
180
181#endif
182
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700183static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct metapage *mp = (struct metapage *)foo;
186
Christoph Lametera35afb82007-05-16 22:10:57 -0700187 mp->lid = 0;
188 mp->lsn = 0;
189 mp->flag = 0;
190 mp->data = NULL;
191 mp->clsn = 0;
192 mp->log = NULL;
193 set_bit(META_free, &mp->flag);
194 init_waitqueue_head(&mp->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Al Viro27496a82005-10-21 03:20:48 -0400197static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 return mempool_alloc(metapage_mempool, gfp_mask);
200}
201
202static inline void free_metapage(struct metapage *mp)
203{
204 mp->flag = 0;
205 set_bit(META_free, &mp->flag);
206
207 mempool_free(mp, metapage_mempool);
208}
209
210int __init metapage_init(void)
211{
212 /*
213 * Allocate the metapage structures
214 */
215 metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
Paul Mundt20c2df82007-07-20 10:11:58 +0900216 0, 0, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (metapage_cache == NULL)
218 return -ENOMEM;
219
Matthew Dobson93d23412006-03-26 01:37:50 -0800220 metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES,
221 metapage_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 if (metapage_mempool == NULL) {
224 kmem_cache_destroy(metapage_cache);
225 return -ENOMEM;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return 0;
229}
230
231void metapage_exit(void)
232{
233 mempool_destroy(metapage_mempool);
234 kmem_cache_destroy(metapage_cache);
235}
236
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600237static inline void drop_metapage(struct page *page, struct metapage *mp)
238{
239 if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
240 test_bit(META_io, &mp->flag))
241 return;
242 remove_metapage(page, mp);
243 INCREMENT(mpStat.pagefree);
244 free_metapage(mp);
245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600248 * Metapage address space operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600250
251static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
252 unsigned int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600254 int rc = 0;
255 int xflag;
256 s64 xaddr;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700257 sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600258 inode->i_blkbits;
259
260 if (lblock >= file_blocks)
261 return 0;
262 if (lblock + *len > file_blocks)
263 *len = file_blocks - lblock;
264
265 if (inode->i_ino) {
266 rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
267 if ((rc == 0) && *len)
268 lblock = (sector_t)xaddr;
269 else
270 lblock = 0;
271 } /* else no mapping */
272
273 return lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600276static void last_read_complete(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600278 if (!PageError(page))
279 SetPageUptodate(page);
280 unlock_page(page);
281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
NeilBrown6712ecf2007-09-27 12:47:43 +0200283static void metapage_read_end_io(struct bio *bio, int err)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600284{
285 struct page *page = bio->bi_private;
286
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600287 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
288 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
289 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600292 dec_io(page, last_read_complete);
293 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600296static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600298 struct jfs_log *log = mp->log;
299 unsigned long flags;
300/*
301 * This can race. Recheck that log hasn't been set to null, and after
302 * acquiring logsync lock, recheck lsn
303 */
304 if (!log)
305 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600307 LOGSYNC_LOCK(log, flags);
308 if (mp->lsn) {
309 mp->log = NULL;
310 mp->lsn = 0;
311 mp->clsn = 0;
312 log->count--;
313 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600315 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600318static void last_write_complete(struct page *page)
319{
320 struct metapage *mp;
321 unsigned int offset;
322
323 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
324 mp = page_to_mp(page, offset);
325 if (mp && test_bit(META_io, &mp->flag)) {
326 if (mp->lsn)
327 remove_from_logsync(mp);
328 clear_bit(META_io, &mp->flag);
329 }
330 /*
331 * I'd like to call drop_metapage here, but I don't think it's
332 * safe unless I have the page locked
333 */
334 }
335 end_page_writeback(page);
336}
337
NeilBrown6712ecf2007-09-27 12:47:43 +0200338static void metapage_write_end_io(struct bio *bio, int err)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600339{
340 struct page *page = bio->bi_private;
341
342 BUG_ON(!PagePrivate(page));
343
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600344 if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
345 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
346 SetPageError(page);
347 }
348 dec_io(page, last_write_complete);
349 bio_put(bio);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600350}
351
352static int metapage_writepage(struct page *page, struct writeback_control *wbc)
353{
354 struct bio *bio = NULL;
355 unsigned int block_offset; /* block offset of mp within page */
356 struct inode *inode = page->mapping->host;
357 unsigned int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
358 unsigned int len;
359 unsigned int xlen;
360 struct metapage *mp;
361 int redirty = 0;
362 sector_t lblock;
363 sector_t pblock;
364 sector_t next_block = 0;
365 sector_t page_start;
366 unsigned long bio_bytes = 0;
367 unsigned long bio_offset = 0;
368 unsigned int offset;
369
370 page_start = (sector_t)page->index <<
371 (PAGE_CACHE_SHIFT - inode->i_blkbits);
372 BUG_ON(!PageLocked(page));
373 BUG_ON(PageWriteback(page));
374
375 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
376 mp = page_to_mp(page, offset);
377
378 if (!mp || !test_bit(META_dirty, &mp->flag))
379 continue;
380
381 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
382 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500383 /*
384 * Make sure this page isn't blocked indefinitely.
385 * If the journal isn't undergoing I/O, push it
386 */
387 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
388 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600389 continue;
390 }
391
392 clear_bit(META_dirty, &mp->flag);
393 block_offset = offset >> inode->i_blkbits;
394 lblock = page_start + block_offset;
395 if (bio) {
396 if (xlen && lblock == next_block) {
397 /* Contiguous, in memory & on disk */
398 len = min(xlen, blocks_per_mp);
399 xlen -= len;
400 bio_bytes += len << inode->i_blkbits;
401 set_bit(META_io, &mp->flag);
402 continue;
403 }
404 /* Not contiguous */
405 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
406 bio_bytes)
407 goto add_failed;
408 /*
409 * Increment counter before submitting i/o to keep
410 * count from hitting zero before we're through
411 */
412 inc_io(page);
413 if (!bio->bi_size)
414 goto dump_bio;
415 submit_bio(WRITE, bio);
416 bio = NULL;
417 } else {
418 set_page_writeback(page);
419 inc_io(page);
420 }
421 xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
422 pblock = metapage_get_blocks(inode, lblock, &xlen);
423 if (!pblock) {
424 /* Need better error handling */
425 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
426 dec_io(page, last_write_complete);
427 continue;
428 }
429 set_bit(META_io, &mp->flag);
430 len = min(xlen, (uint) JFS_SBI(inode->i_sb)->nbperpage);
431
432 bio = bio_alloc(GFP_NOFS, 1);
433 bio->bi_bdev = inode->i_sb->s_bdev;
434 bio->bi_sector = pblock << (inode->i_blkbits - 9);
435 bio->bi_end_io = metapage_write_end_io;
436 bio->bi_private = page;
437
438 /* Don't call bio_add_page yet, we may add to this vec */
439 bio_offset = offset;
440 bio_bytes = len << inode->i_blkbits;
441
442 xlen -= len;
443 next_block = lblock + len;
444 }
445 if (bio) {
446 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
447 goto add_failed;
448 if (!bio->bi_size)
449 goto dump_bio;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500450
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600451 submit_bio(WRITE, bio);
452 }
453 if (redirty)
454 redirty_page_for_writepage(wbc, page);
455
456 unlock_page(page);
457
458 return 0;
459add_failed:
460 /* We should never reach here, since we're only adding one vec */
461 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
462 goto skip;
463dump_bio:
Dave Kleikamp288e4d82007-06-13 10:17:50 -0500464 print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16,
465 4, bio, sizeof(*bio), 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600466skip:
467 bio_put(bio);
468 unlock_page(page);
469 dec_io(page, last_write_complete);
470
471 return -EIO;
472}
473
474static int metapage_readpage(struct file *fp, struct page *page)
475{
476 struct inode *inode = page->mapping->host;
477 struct bio *bio = NULL;
478 unsigned int block_offset;
479 unsigned int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
480 sector_t page_start; /* address of page in fs blocks */
481 sector_t pblock;
482 unsigned int xlen;
483 unsigned int len;
484 unsigned int offset;
485
486 BUG_ON(!PageLocked(page));
487 page_start = (sector_t)page->index <<
488 (PAGE_CACHE_SHIFT - inode->i_blkbits);
489
490 block_offset = 0;
491 while (block_offset < blocks_per_page) {
492 xlen = blocks_per_page - block_offset;
493 pblock = metapage_get_blocks(inode, page_start + block_offset,
494 &xlen);
495 if (pblock) {
496 if (!PagePrivate(page))
497 insert_metapage(page, NULL);
498 inc_io(page);
499 if (bio)
500 submit_bio(READ, bio);
501
502 bio = bio_alloc(GFP_NOFS, 1);
503 bio->bi_bdev = inode->i_sb->s_bdev;
504 bio->bi_sector = pblock << (inode->i_blkbits - 9);
505 bio->bi_end_io = metapage_read_end_io;
506 bio->bi_private = page;
507 len = xlen << inode->i_blkbits;
508 offset = block_offset << inode->i_blkbits;
509 if (bio_add_page(bio, page, len, offset) < len)
510 goto add_failed;
511 block_offset += xlen;
512 } else
513 block_offset++;
514 }
515 if (bio)
516 submit_bio(READ, bio);
517 else
518 unlock_page(page);
519
520 return 0;
521
522add_failed:
523 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
524 bio_put(bio);
525 dec_io(page, last_read_complete);
526 return -EIO;
527}
528
Al Viro27496a82005-10-21 03:20:48 -0400529static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600530{
531 struct metapage *mp;
Dave Kleikampb9646382006-05-24 07:43:38 -0500532 int ret = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600533 unsigned int offset;
534
535 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
536 mp = page_to_mp(page, offset);
537
538 if (!mp)
539 continue;
540
541 jfs_info("metapage_releasepage: mp = 0x%p", mp);
Dave Kleikampb9646382006-05-24 07:43:38 -0500542 if (mp->count || mp->nohomeok ||
543 test_bit(META_dirty, &mp->flag)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600544 jfs_info("count = %ld, nohomeok = %d", mp->count,
545 mp->nohomeok);
Dave Kleikampb9646382006-05-24 07:43:38 -0500546 ret = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600547 continue;
548 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600549 if (mp->lsn)
550 remove_from_logsync(mp);
551 remove_metapage(page, mp);
552 INCREMENT(mpStat.pagefree);
553 free_metapage(mp);
554 }
Dave Kleikampb9646382006-05-24 07:43:38 -0500555 return ret;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600556}
557
NeilBrown2ff28e22006-03-26 01:37:18 -0800558static void metapage_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600559{
560 BUG_ON(offset);
561
NeilBrown2ff28e22006-03-26 01:37:18 -0800562 BUG_ON(PageWriteback(page));
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600563
NeilBrown2ff28e22006-03-26 01:37:18 -0800564 metapage_releasepage(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600565}
566
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700567const struct address_space_operations jfs_metapage_aops = {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600568 .readpage = metapage_readpage,
569 .writepage = metapage_writepage,
570 .sync_page = block_sync_page,
571 .releasepage = metapage_releasepage,
572 .invalidatepage = metapage_invalidatepage,
573 .set_page_dirty = __set_page_dirty_nobuffers,
574};
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
577 unsigned int size, int absolute,
578 unsigned long new)
579{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int l2BlocksPerPage;
581 int l2bsize;
582 struct address_space *mapping;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600583 struct metapage *mp = NULL;
584 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 unsigned long page_index;
586 unsigned long page_offset;
587
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600588 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
589 inode->i_ino, lblock, absolute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600591 l2bsize = inode->i_blkbits;
592 l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
593 page_index = lblock >> l2BlocksPerPage;
594 page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
595 if ((page_offset + size) > PAGE_CACHE_SIZE) {
596 jfs_err("MetaData crosses page boundary!!");
597 jfs_err("lblock = %lx, size = %d", lblock, size);
598 dump_stack();
599 return NULL;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (absolute)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600602 mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else {
604 /*
605 * If an nfs client tries to read an inode that is larger
606 * than any existing inodes, we may try to read past the
607 * end of the inode map
608 */
609 if ((lblock << inode->i_blkbits) >= inode->i_size)
610 return NULL;
611 mapping = inode->i_mapping;
612 }
613
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600614 if (new && (PSIZE == PAGE_CACHE_SIZE)) {
615 page = grab_cache_page(mapping, page_index);
616 if (!page) {
617 jfs_err("grab_cache_page failed!");
618 return NULL;
619 }
620 SetPageUptodate(page);
621 } else {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700622 page = read_mapping_page(mapping, page_index, NULL);
Dave Kleikampc40c20242005-07-22 11:08:44 -0500623 if (IS_ERR(page) || !PageUptodate(page)) {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700624 jfs_err("read_mapping_page failed!");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600625 return NULL;
626 }
627 lock_page(page);
628 }
629
630 mp = page_to_mp(page, page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (mp) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600632 if (mp->logical_size != size) {
633 jfs_error(inode->i_sb,
634 "__get_metapage: mp->logical_size != size");
635 jfs_err("logical_size = %d, size = %d",
636 mp->logical_size, size);
637 dump_stack();
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500638 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 mp->count++;
641 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (test_bit(META_discard, &mp->flag)) {
643 if (!new) {
644 jfs_error(inode->i_sb,
645 "__get_metapage: using a "
646 "discarded metapage");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600647 discard_metapage(mp);
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500648 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 clear_bit(META_discard, &mp->flag);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600653 INCREMENT(mpStat.pagealloc);
654 mp = alloc_metapage(GFP_NOFS);
655 mp->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 mp->xflag = COMMIT_PAGE;
658 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600659 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600661 mp->data = page_address(page) + page_offset;
662 mp->index = lblock;
663 if (unlikely(insert_metapage(page, mp))) {
664 free_metapage(mp);
665 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600667 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600670 if (new) {
671 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600675 unlock_page(page);
676 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return mp;
678
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600679unlock:
680 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return NULL;
682}
683
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600684void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600686 jfs_info("grab_metapage: mp = 0x%p", mp);
687 page_cache_get(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600689 mp->count++;
690 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600694void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600697 jfs_info("force_metapage: mp = 0x%p", mp);
698 set_bit(META_forcewrite, &mp->flag);
699 clear_bit(META_sync, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 page_cache_get(page);
701 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600702 set_page_dirty(page);
703 write_one_page(page, 1);
704 clear_bit(META_forcewrite, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 page_cache_release(page);
706}
707
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500708void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600709{
710 lock_page(mp->page);
711}
712
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500713void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600714{
715 if (mp->count || mp->nohomeok) {
716 /* Someone else will release this */
717 unlock_page(mp->page);
718 return;
719 }
720 page_cache_get(mp->page);
721 mp->count++;
722 lock_metapage(mp);
723 unlock_page(mp->page);
724 release_metapage(mp);
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727void release_metapage(struct metapage * mp)
728{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600729 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
731
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600732 BUG_ON(!page);
733
734 lock_page(page);
735 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600738 if (--mp->count || mp->nohomeok) {
739 unlock_page(page);
740 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return;
742 }
743
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600744 if (test_bit(META_dirty, &mp->flag)) {
745 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 clear_bit(META_sync, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600748 write_one_page(page, 1);
749 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600751 } else if (mp->lsn) /* discard_metapage doesn't remove it */
752 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600754 /* Try to keep metapages from using up too much memory */
755 drop_metapage(page, mp);
Dave Kleikampd0e671a2006-12-20 15:55:35 -0600756
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600757 unlock_page(page);
758 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761void __invalidate_metapages(struct inode *ip, s64 addr, int len)
762{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600763 sector_t lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600765 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600767 struct address_space *mapping =
768 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct metapage *mp;
770 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600771 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600774 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * released, but should not be written.
776 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600777 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
778 lblock += BlocksPerPage) {
779 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
780 if (!page)
781 continue;
782 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
783 mp = page_to_mp(page, offset);
784 if (!mp)
785 continue;
786 if (mp->index < addr)
787 continue;
788 if (mp->index >= addr + len)
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 clear_bit(META_dirty, &mp->flag);
792 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600793 if (mp->lsn)
794 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600796 unlock_page(page);
797 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799}
800
801#ifdef CONFIG_JFS_STATISTICS
802int jfs_mpstat_read(char *buffer, char **start, off_t offset, int length,
803 int *eof, void *data)
804{
805 int len = 0;
806 off_t begin;
807
808 len += sprintf(buffer,
809 "JFS Metapage statistics\n"
810 "=======================\n"
811 "page allocations = %d\n"
812 "page frees = %d\n"
813 "lock waits = %d\n",
814 mpStat.pagealloc,
815 mpStat.pagefree,
816 mpStat.lockwait);
817
818 begin = offset;
819 *start = buffer + begin;
820 len -= begin;
821
822 if (len > length)
823 len = length;
824 else
825 *eof = 1;
826
827 if (len < 0)
828 len = 0;
829
830 return len;
831}
832#endif