2 * Copyright (C) International Business Machines Corp., 2000-2005
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
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
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * 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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/bio.h>
23 #include <linux/init.h>
24 #include <linux/buffer_head.h>
25 #include <linux/mempool.h>
26 #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"
33 #ifdef CONFIG_JFS_STATISTICS
35 uint pagealloc
; /* # of page allocations */
36 uint pagefree
; /* # of page frees */
37 uint lockwait
; /* # of sleeping lock_metapage() calls */
41 #define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
42 #define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag)
44 static inline void unlock_metapage(struct metapage
*mp
)
46 clear_bit_unlock(META_locked
, &mp
->flag
);
50 static inline void __lock_metapage(struct metapage
*mp
)
52 DECLARE_WAITQUEUE(wait
, current
);
53 INCREMENT(mpStat
.lockwait
);
54 add_wait_queue_exclusive(&mp
->wait
, &wait
);
56 set_current_state(TASK_UNINTERRUPTIBLE
);
57 if (metapage_locked(mp
)) {
58 unlock_page(mp
->page
);
62 } while (trylock_metapage(mp
));
63 __set_current_state(TASK_RUNNING
);
64 remove_wait_queue(&mp
->wait
, &wait
);
68 * Must have mp->page locked
70 static inline void lock_metapage(struct metapage
*mp
)
72 if (trylock_metapage(mp
))
76 #define METAPOOL_MIN_PAGES 32
77 static struct kmem_cache
*metapage_cache
;
78 static mempool_t
*metapage_mempool
;
80 #define MPS_PER_PAGE (PAGE_CACHE_SIZE >> L2PSIZE)
87 struct metapage
*mp
[MPS_PER_PAGE
];
89 #define mp_anchor(page) ((struct meta_anchor *)page_private(page))
91 static inline struct metapage
*page_to_mp(struct page
*page
, int offset
)
93 if (!PagePrivate(page
))
95 return mp_anchor(page
)->mp
[offset
>> L2PSIZE
];
98 static inline int insert_metapage(struct page
*page
, struct metapage
*mp
)
100 struct meta_anchor
*a
;
102 int l2mp_blocks
; /* log2 blocks per metapage */
104 if (PagePrivate(page
))
107 a
= kzalloc(sizeof(struct meta_anchor
), GFP_NOFS
);
110 set_page_private(page
, (unsigned long)a
);
111 SetPagePrivate(page
);
116 l2mp_blocks
= L2PSIZE
- page
->mapping
->host
->i_blkbits
;
117 index
= (mp
->index
>> l2mp_blocks
) & (MPS_PER_PAGE
- 1);
125 static inline void remove_metapage(struct page
*page
, struct metapage
*mp
)
127 struct meta_anchor
*a
= mp_anchor(page
);
128 int l2mp_blocks
= L2PSIZE
- page
->mapping
->host
->i_blkbits
;
131 index
= (mp
->index
>> l2mp_blocks
) & (MPS_PER_PAGE
- 1);
133 BUG_ON(a
->mp
[index
] != mp
);
136 if (--a
->mp_count
== 0) {
138 set_page_private(page
, 0);
139 ClearPagePrivate(page
);
144 static inline void inc_io(struct page
*page
)
146 atomic_inc(&mp_anchor(page
)->io_count
);
149 static inline void dec_io(struct page
*page
, void (*handler
) (struct page
*))
151 if (atomic_dec_and_test(&mp_anchor(page
)->io_count
))
156 static inline struct metapage
*page_to_mp(struct page
*page
, int offset
)
158 return PagePrivate(page
) ? (struct metapage
*)page_private(page
) : NULL
;
161 static inline int insert_metapage(struct page
*page
, struct metapage
*mp
)
164 set_page_private(page
, (unsigned long)mp
);
165 SetPagePrivate(page
);
171 static inline void remove_metapage(struct page
*page
, struct metapage
*mp
)
173 set_page_private(page
, 0);
174 ClearPagePrivate(page
);
178 #define inc_io(page) do {} while(0)
179 #define dec_io(page, handler) handler(page)
183 static void init_once(struct kmem_cache
*cachep
, void *foo
)
185 struct metapage
*mp
= (struct metapage
*)foo
;
193 set_bit(META_free
, &mp
->flag
);
194 init_waitqueue_head(&mp
->wait
);
197 static inline struct metapage
*alloc_metapage(gfp_t gfp_mask
)
199 return mempool_alloc(metapage_mempool
, gfp_mask
);
202 static inline void free_metapage(struct metapage
*mp
)
205 set_bit(META_free
, &mp
->flag
);
207 mempool_free(mp
, metapage_mempool
);
210 int __init
metapage_init(void)
213 * Allocate the metapage structures
215 metapage_cache
= kmem_cache_create("jfs_mp", sizeof(struct metapage
),
217 if (metapage_cache
== NULL
)
220 metapage_mempool
= mempool_create_slab_pool(METAPOOL_MIN_PAGES
,
223 if (metapage_mempool
== NULL
) {
224 kmem_cache_destroy(metapage_cache
);
231 void metapage_exit(void)
233 mempool_destroy(metapage_mempool
);
234 kmem_cache_destroy(metapage_cache
);
237 static inline void drop_metapage(struct page
*page
, struct metapage
*mp
)
239 if (mp
->count
|| mp
->nohomeok
|| test_bit(META_dirty
, &mp
->flag
) ||
240 test_bit(META_io
, &mp
->flag
))
242 remove_metapage(page
, mp
);
243 INCREMENT(mpStat
.pagefree
);
248 * Metapage address space operations
251 static sector_t
metapage_get_blocks(struct inode
*inode
, sector_t lblock
,
257 sector_t file_blocks
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
260 if (lblock
>= file_blocks
)
262 if (lblock
+ *len
> file_blocks
)
263 *len
= file_blocks
- lblock
;
266 rc
= xtLookup(inode
, (s64
)lblock
, *len
, &xflag
, &xaddr
, len
, 0);
267 if ((rc
== 0) && *len
)
268 lblock
= (sector_t
)xaddr
;
271 } /* else no mapping */
276 static void last_read_complete(struct page
*page
)
278 if (!PageError(page
))
279 SetPageUptodate(page
);
283 static void metapage_read_end_io(struct bio
*bio
, int err
)
285 struct page
*page
= bio
->bi_private
;
287 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
288 printk(KERN_ERR
"metapage_read_end_io: I/O error\n");
292 dec_io(page
, last_read_complete
);
296 static void remove_from_logsync(struct metapage
*mp
)
298 struct jfs_log
*log
= mp
->log
;
301 * This can race. Recheck that log hasn't been set to null, and after
302 * acquiring logsync lock, recheck lsn
307 LOGSYNC_LOCK(log
, flags
);
313 list_del(&mp
->synclist
);
315 LOGSYNC_UNLOCK(log
, flags
);
318 static void last_write_complete(struct page
*page
)
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
)) {
327 remove_from_logsync(mp
);
328 clear_bit(META_io
, &mp
->flag
);
331 * I'd like to call drop_metapage here, but I don't think it's
332 * safe unless I have the page locked
335 end_page_writeback(page
);
338 static void metapage_write_end_io(struct bio
*bio
, int err
)
340 struct page
*page
= bio
->bi_private
;
342 BUG_ON(!PagePrivate(page
));
344 if (! test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
345 printk(KERN_ERR
"metapage_write_end_io: I/O error\n");
348 dec_io(page
, last_write_complete
);
352 static int metapage_writepage(struct page
*page
, struct writeback_control
*wbc
)
354 struct bio
*bio
= NULL
;
355 int block_offset
; /* block offset of mp within page */
356 struct inode
*inode
= page
->mapping
->host
;
357 int blocks_per_mp
= JFS_SBI(inode
->i_sb
)->nbperpage
;
365 sector_t next_block
= 0;
367 unsigned long bio_bytes
= 0;
368 unsigned long bio_offset
= 0;
371 page_start
= (sector_t
)page
->index
<<
372 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
373 BUG_ON(!PageLocked(page
));
374 BUG_ON(PageWriteback(page
));
375 set_page_writeback(page
);
377 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
378 mp
= page_to_mp(page
, offset
);
380 if (!mp
|| !test_bit(META_dirty
, &mp
->flag
))
383 if (mp
->nohomeok
&& !test_bit(META_forcewrite
, &mp
->flag
)) {
386 * Make sure this page isn't blocked indefinitely.
387 * If the journal isn't undergoing I/O, push it
389 if (mp
->log
&& !(mp
->log
->cflag
& logGC_PAGEOUT
))
390 jfs_flush_journal(mp
->log
, 0);
394 clear_bit(META_dirty
, &mp
->flag
);
395 block_offset
= offset
>> inode
->i_blkbits
;
396 lblock
= page_start
+ block_offset
;
398 if (xlen
&& lblock
== next_block
) {
399 /* Contiguous, in memory & on disk */
400 len
= min(xlen
, blocks_per_mp
);
402 bio_bytes
+= len
<< inode
->i_blkbits
;
403 set_bit(META_io
, &mp
->flag
);
407 if (bio_add_page(bio
, page
, bio_bytes
, bio_offset
) <
411 * Increment counter before submitting i/o to keep
412 * count from hitting zero before we're through
417 submit_bio(WRITE
, bio
);
422 xlen
= (PAGE_CACHE_SIZE
- offset
) >> inode
->i_blkbits
;
423 pblock
= metapage_get_blocks(inode
, lblock
, &xlen
);
425 /* Need better error handling */
426 printk(KERN_ERR
"JFS: metapage_get_blocks failed\n");
427 dec_io(page
, last_write_complete
);
430 set_bit(META_io
, &mp
->flag
);
431 len
= min(xlen
, (int)JFS_SBI(inode
->i_sb
)->nbperpage
);
433 bio
= bio_alloc(GFP_NOFS
, 1);
434 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
435 bio
->bi_sector
= pblock
<< (inode
->i_blkbits
- 9);
436 bio
->bi_end_io
= metapage_write_end_io
;
437 bio
->bi_private
= page
;
439 /* Don't call bio_add_page yet, we may add to this vec */
441 bio_bytes
= len
<< inode
->i_blkbits
;
444 next_block
= lblock
+ len
;
447 if (bio_add_page(bio
, page
, bio_bytes
, bio_offset
) < bio_bytes
)
452 submit_bio(WRITE
, bio
);
456 redirty_page_for_writepage(wbc
, page
);
460 if (nr_underway
== 0)
461 end_page_writeback(page
);
465 /* We should never reach here, since we're only adding one vec */
466 printk(KERN_ERR
"JFS: bio_add_page failed unexpectedly\n");
469 print_hex_dump(KERN_ERR
, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS
, 16,
470 4, bio
, sizeof(*bio
), 0);
474 dec_io(page
, last_write_complete
);
479 static int metapage_readpage(struct file
*fp
, struct page
*page
)
481 struct inode
*inode
= page
->mapping
->host
;
482 struct bio
*bio
= NULL
;
484 int blocks_per_page
= PAGE_CACHE_SIZE
>> inode
->i_blkbits
;
485 sector_t page_start
; /* address of page in fs blocks */
491 BUG_ON(!PageLocked(page
));
492 page_start
= (sector_t
)page
->index
<<
493 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
496 while (block_offset
< blocks_per_page
) {
497 xlen
= blocks_per_page
- block_offset
;
498 pblock
= metapage_get_blocks(inode
, page_start
+ block_offset
,
501 if (!PagePrivate(page
))
502 insert_metapage(page
, NULL
);
505 submit_bio(READ
, bio
);
507 bio
= bio_alloc(GFP_NOFS
, 1);
508 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
509 bio
->bi_sector
= pblock
<< (inode
->i_blkbits
- 9);
510 bio
->bi_end_io
= metapage_read_end_io
;
511 bio
->bi_private
= page
;
512 len
= xlen
<< inode
->i_blkbits
;
513 offset
= block_offset
<< inode
->i_blkbits
;
514 if (bio_add_page(bio
, page
, len
, offset
) < len
)
516 block_offset
+= xlen
;
521 submit_bio(READ
, bio
);
528 printk(KERN_ERR
"JFS: bio_add_page failed unexpectedly\n");
530 dec_io(page
, last_read_complete
);
534 static int metapage_releasepage(struct page
*page
, gfp_t gfp_mask
)
540 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
541 mp
= page_to_mp(page
, offset
);
546 jfs_info("metapage_releasepage: mp = 0x%p", mp
);
547 if (mp
->count
|| mp
->nohomeok
||
548 test_bit(META_dirty
, &mp
->flag
)) {
549 jfs_info("count = %ld, nohomeok = %d", mp
->count
,
555 remove_from_logsync(mp
);
556 remove_metapage(page
, mp
);
557 INCREMENT(mpStat
.pagefree
);
563 static void metapage_invalidatepage(struct page
*page
, unsigned long offset
)
567 BUG_ON(PageWriteback(page
));
569 metapage_releasepage(page
, 0);
572 const struct address_space_operations jfs_metapage_aops
= {
573 .readpage
= metapage_readpage
,
574 .writepage
= metapage_writepage
,
575 .sync_page
= block_sync_page
,
576 .releasepage
= metapage_releasepage
,
577 .invalidatepage
= metapage_invalidatepage
,
578 .set_page_dirty
= __set_page_dirty_nobuffers
,
581 struct metapage
*__get_metapage(struct inode
*inode
, unsigned long lblock
,
582 unsigned int size
, int absolute
,
587 struct address_space
*mapping
;
588 struct metapage
*mp
= NULL
;
590 unsigned long page_index
;
591 unsigned long page_offset
;
593 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
594 inode
->i_ino
, lblock
, absolute
);
596 l2bsize
= inode
->i_blkbits
;
597 l2BlocksPerPage
= PAGE_CACHE_SHIFT
- l2bsize
;
598 page_index
= lblock
>> l2BlocksPerPage
;
599 page_offset
= (lblock
- (page_index
<< l2BlocksPerPage
)) << l2bsize
;
600 if ((page_offset
+ size
) > PAGE_CACHE_SIZE
) {
601 jfs_err("MetaData crosses page boundary!!");
602 jfs_err("lblock = %lx, size = %d", lblock
, size
);
607 mapping
= JFS_SBI(inode
->i_sb
)->direct_inode
->i_mapping
;
610 * If an nfs client tries to read an inode that is larger
611 * than any existing inodes, we may try to read past the
612 * end of the inode map
614 if ((lblock
<< inode
->i_blkbits
) >= inode
->i_size
)
616 mapping
= inode
->i_mapping
;
619 if (new && (PSIZE
== PAGE_CACHE_SIZE
)) {
620 page
= grab_cache_page(mapping
, page_index
);
622 jfs_err("grab_cache_page failed!");
625 SetPageUptodate(page
);
627 page
= read_mapping_page(mapping
, page_index
, NULL
);
628 if (IS_ERR(page
) || !PageUptodate(page
)) {
629 jfs_err("read_mapping_page failed!");
635 mp
= page_to_mp(page
, page_offset
);
637 if (mp
->logical_size
!= size
) {
638 jfs_error(inode
->i_sb
,
639 "__get_metapage: mp->logical_size != size");
640 jfs_err("logical_size = %d, size = %d",
641 mp
->logical_size
, size
);
647 if (test_bit(META_discard
, &mp
->flag
)) {
649 jfs_error(inode
->i_sb
,
650 "__get_metapage: using a "
651 "discarded metapage");
652 discard_metapage(mp
);
655 clear_bit(META_discard
, &mp
->flag
);
658 INCREMENT(mpStat
.pagealloc
);
659 mp
= alloc_metapage(GFP_NOFS
);
662 mp
->xflag
= COMMIT_PAGE
;
665 mp
->logical_size
= size
;
666 mp
->data
= page_address(page
) + page_offset
;
668 if (unlikely(insert_metapage(page
, mp
))) {
676 jfs_info("zeroing mp = 0x%p", mp
);
677 memset(mp
->data
, 0, PSIZE
);
681 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp
, mp
->data
);
689 void grab_metapage(struct metapage
* mp
)
691 jfs_info("grab_metapage: mp = 0x%p", mp
);
692 page_cache_get(mp
->page
);
696 unlock_page(mp
->page
);
699 void force_metapage(struct metapage
*mp
)
701 struct page
*page
= mp
->page
;
702 jfs_info("force_metapage: mp = 0x%p", mp
);
703 set_bit(META_forcewrite
, &mp
->flag
);
704 clear_bit(META_sync
, &mp
->flag
);
705 page_cache_get(page
);
707 set_page_dirty(page
);
708 write_one_page(page
, 1);
709 clear_bit(META_forcewrite
, &mp
->flag
);
710 page_cache_release(page
);
713 void hold_metapage(struct metapage
*mp
)
718 void put_metapage(struct metapage
*mp
)
720 if (mp
->count
|| mp
->nohomeok
) {
721 /* Someone else will release this */
722 unlock_page(mp
->page
);
725 page_cache_get(mp
->page
);
728 unlock_page(mp
->page
);
729 release_metapage(mp
);
732 void release_metapage(struct metapage
* mp
)
734 struct page
*page
= mp
->page
;
735 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp
, mp
->flag
);
743 if (--mp
->count
|| mp
->nohomeok
) {
745 page_cache_release(page
);
749 if (test_bit(META_dirty
, &mp
->flag
)) {
750 set_page_dirty(page
);
751 if (test_bit(META_sync
, &mp
->flag
)) {
752 clear_bit(META_sync
, &mp
->flag
);
753 write_one_page(page
, 1);
754 lock_page(page
); /* write_one_page unlocks the page */
756 } else if (mp
->lsn
) /* discard_metapage doesn't remove it */
757 remove_from_logsync(mp
);
759 /* Try to keep metapages from using up too much memory */
760 drop_metapage(page
, mp
);
763 page_cache_release(page
);
766 void __invalidate_metapages(struct inode
*ip
, s64 addr
, int len
)
769 int l2BlocksPerPage
= PAGE_CACHE_SHIFT
- ip
->i_blkbits
;
770 int BlocksPerPage
= 1 << l2BlocksPerPage
;
771 /* All callers are interested in block device's mapping */
772 struct address_space
*mapping
=
773 JFS_SBI(ip
->i_sb
)->direct_inode
->i_mapping
;
779 * Mark metapages to discard. They will eventually be
780 * released, but should not be written.
782 for (lblock
= addr
& ~(BlocksPerPage
- 1); lblock
< addr
+ len
;
783 lblock
+= BlocksPerPage
) {
784 page
= find_lock_page(mapping
, lblock
>> l2BlocksPerPage
);
787 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
788 mp
= page_to_mp(page
, offset
);
791 if (mp
->index
< addr
)
793 if (mp
->index
>= addr
+ len
)
796 clear_bit(META_dirty
, &mp
->flag
);
797 set_bit(META_discard
, &mp
->flag
);
799 remove_from_logsync(mp
);
802 page_cache_release(page
);
806 #ifdef CONFIG_JFS_STATISTICS
807 int jfs_mpstat_read(char *buffer
, char **start
, off_t offset
, int length
,
808 int *eof
, void *data
)
813 len
+= sprintf(buffer
,
814 "JFS Metapage statistics\n"
815 "=======================\n"
816 "page allocations = %d\n"
824 *start
= buffer
+ begin
;