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(META_locked, &(mp)->flag)
44 static inline void unlock_metapage(struct metapage
*mp
)
46 clear_bit(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 kmem_cache_t
*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
, uint 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
, uint 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(void *foo
, kmem_cache_t
*cachep
, unsigned long flags
)
185 struct metapage
*mp
= (struct metapage
*)foo
;
187 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
188 SLAB_CTOR_CONSTRUCTOR
) {
195 set_bit(META_free
, &mp
->flag
);
196 init_waitqueue_head(&mp
->wait
);
200 static inline struct metapage
*alloc_metapage(gfp_t gfp_mask
)
202 return mempool_alloc(metapage_mempool
, gfp_mask
);
205 static inline void free_metapage(struct metapage
*mp
)
208 set_bit(META_free
, &mp
->flag
);
210 mempool_free(mp
, metapage_mempool
);
213 int __init
metapage_init(void)
216 * Allocate the metapage structures
218 metapage_cache
= kmem_cache_create("jfs_mp", sizeof(struct metapage
),
219 0, 0, init_once
, NULL
);
220 if (metapage_cache
== NULL
)
223 metapage_mempool
= mempool_create_slab_pool(METAPOOL_MIN_PAGES
,
226 if (metapage_mempool
== NULL
) {
227 kmem_cache_destroy(metapage_cache
);
234 void metapage_exit(void)
236 mempool_destroy(metapage_mempool
);
237 kmem_cache_destroy(metapage_cache
);
240 static inline void drop_metapage(struct page
*page
, struct metapage
*mp
)
242 if (mp
->count
|| mp
->nohomeok
|| test_bit(META_dirty
, &mp
->flag
) ||
243 test_bit(META_io
, &mp
->flag
))
245 remove_metapage(page
, mp
);
246 INCREMENT(mpStat
.pagefree
);
251 * Metapage address space operations
254 static sector_t
metapage_get_blocks(struct inode
*inode
, sector_t lblock
,
260 sector_t file_blocks
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
263 if (lblock
>= file_blocks
)
265 if (lblock
+ *len
> file_blocks
)
266 *len
= file_blocks
- lblock
;
269 rc
= xtLookup(inode
, (s64
)lblock
, *len
, &xflag
, &xaddr
, len
, 0);
270 if ((rc
== 0) && *len
)
271 lblock
= (sector_t
)xaddr
;
274 } /* else no mapping */
279 static void last_read_complete(struct page
*page
)
281 if (!PageError(page
))
282 SetPageUptodate(page
);
286 static int metapage_read_end_io(struct bio
*bio
, unsigned int bytes_done
,
289 struct page
*page
= bio
->bi_private
;
294 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
295 printk(KERN_ERR
"metapage_read_end_io: I/O error\n");
299 dec_io(page
, last_read_complete
);
305 static void remove_from_logsync(struct metapage
*mp
)
307 struct jfs_log
*log
= mp
->log
;
310 * This can race. Recheck that log hasn't been set to null, and after
311 * acquiring logsync lock, recheck lsn
316 LOGSYNC_LOCK(log
, flags
);
322 list_del(&mp
->synclist
);
324 LOGSYNC_UNLOCK(log
, flags
);
327 static void last_write_complete(struct page
*page
)
332 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
333 mp
= page_to_mp(page
, offset
);
334 if (mp
&& test_bit(META_io
, &mp
->flag
)) {
336 remove_from_logsync(mp
);
337 clear_bit(META_io
, &mp
->flag
);
340 * I'd like to call drop_metapage here, but I don't think it's
341 * safe unless I have the page locked
344 end_page_writeback(page
);
347 static int metapage_write_end_io(struct bio
*bio
, unsigned int bytes_done
,
350 struct page
*page
= bio
->bi_private
;
352 BUG_ON(!PagePrivate(page
));
357 if (! test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
358 printk(KERN_ERR
"metapage_write_end_io: I/O error\n");
361 dec_io(page
, last_write_complete
);
366 static int metapage_writepage(struct page
*page
, struct writeback_control
*wbc
)
368 struct bio
*bio
= NULL
;
369 unsigned int block_offset
; /* block offset of mp within page */
370 struct inode
*inode
= page
->mapping
->host
;
371 unsigned int blocks_per_mp
= JFS_SBI(inode
->i_sb
)->nbperpage
;
378 sector_t next_block
= 0;
380 unsigned long bio_bytes
= 0;
381 unsigned long bio_offset
= 0;
384 page_start
= (sector_t
)page
->index
<<
385 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
386 BUG_ON(!PageLocked(page
));
387 BUG_ON(PageWriteback(page
));
389 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
390 mp
= page_to_mp(page
, offset
);
392 if (!mp
|| !test_bit(META_dirty
, &mp
->flag
))
395 if (mp
->nohomeok
&& !test_bit(META_forcewrite
, &mp
->flag
)) {
398 * Make sure this page isn't blocked indefinitely.
399 * If the journal isn't undergoing I/O, push it
401 if (mp
->log
&& !(mp
->log
->cflag
& logGC_PAGEOUT
))
402 jfs_flush_journal(mp
->log
, 0);
406 clear_bit(META_dirty
, &mp
->flag
);
407 block_offset
= offset
>> inode
->i_blkbits
;
408 lblock
= page_start
+ block_offset
;
410 if (xlen
&& lblock
== next_block
) {
411 /* Contiguous, in memory & on disk */
412 len
= min(xlen
, blocks_per_mp
);
414 bio_bytes
+= len
<< inode
->i_blkbits
;
415 set_bit(META_io
, &mp
->flag
);
419 if (bio_add_page(bio
, page
, bio_bytes
, bio_offset
) <
423 * Increment counter before submitting i/o to keep
424 * count from hitting zero before we're through
429 submit_bio(WRITE
, bio
);
432 set_page_writeback(page
);
435 xlen
= (PAGE_CACHE_SIZE
- offset
) >> inode
->i_blkbits
;
436 pblock
= metapage_get_blocks(inode
, lblock
, &xlen
);
438 /* Need better error handling */
439 printk(KERN_ERR
"JFS: metapage_get_blocks failed\n");
440 dec_io(page
, last_write_complete
);
443 set_bit(META_io
, &mp
->flag
);
444 len
= min(xlen
, (uint
) JFS_SBI(inode
->i_sb
)->nbperpage
);
446 bio
= bio_alloc(GFP_NOFS
, 1);
447 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
448 bio
->bi_sector
= pblock
<< (inode
->i_blkbits
- 9);
449 bio
->bi_end_io
= metapage_write_end_io
;
450 bio
->bi_private
= page
;
452 /* Don't call bio_add_page yet, we may add to this vec */
454 bio_bytes
= len
<< inode
->i_blkbits
;
457 next_block
= lblock
+ len
;
460 if (bio_add_page(bio
, page
, bio_bytes
, bio_offset
) < bio_bytes
)
465 submit_bio(WRITE
, bio
);
468 redirty_page_for_writepage(wbc
, page
);
474 /* We should never reach here, since we're only adding one vec */
475 printk(KERN_ERR
"JFS: bio_add_page failed unexpectedly\n");
478 dump_mem("bio", bio
, sizeof(*bio
));
482 dec_io(page
, last_write_complete
);
487 static int metapage_readpage(struct file
*fp
, struct page
*page
)
489 struct inode
*inode
= page
->mapping
->host
;
490 struct bio
*bio
= NULL
;
491 unsigned int block_offset
;
492 unsigned int blocks_per_page
= PAGE_CACHE_SIZE
>> inode
->i_blkbits
;
493 sector_t page_start
; /* address of page in fs blocks */
499 BUG_ON(!PageLocked(page
));
500 page_start
= (sector_t
)page
->index
<<
501 (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
504 while (block_offset
< blocks_per_page
) {
505 xlen
= blocks_per_page
- block_offset
;
506 pblock
= metapage_get_blocks(inode
, page_start
+ block_offset
,
509 if (!PagePrivate(page
))
510 insert_metapage(page
, NULL
);
513 submit_bio(READ
, bio
);
515 bio
= bio_alloc(GFP_NOFS
, 1);
516 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
517 bio
->bi_sector
= pblock
<< (inode
->i_blkbits
- 9);
518 bio
->bi_end_io
= metapage_read_end_io
;
519 bio
->bi_private
= page
;
520 len
= xlen
<< inode
->i_blkbits
;
521 offset
= block_offset
<< inode
->i_blkbits
;
522 if (bio_add_page(bio
, page
, len
, offset
) < len
)
524 block_offset
+= xlen
;
529 submit_bio(READ
, bio
);
536 printk(KERN_ERR
"JFS: bio_add_page failed unexpectedly\n");
538 dec_io(page
, last_read_complete
);
542 static int metapage_releasepage(struct page
*page
, gfp_t gfp_mask
)
548 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
549 mp
= page_to_mp(page
, offset
);
554 jfs_info("metapage_releasepage: mp = 0x%p", mp
);
555 if (mp
->count
|| mp
->nohomeok
||
556 test_bit(META_dirty
, &mp
->flag
)) {
557 jfs_info("count = %ld, nohomeok = %d", mp
->count
,
563 remove_from_logsync(mp
);
564 remove_metapage(page
, mp
);
565 INCREMENT(mpStat
.pagefree
);
571 static void metapage_invalidatepage(struct page
*page
, unsigned long offset
)
575 BUG_ON(PageWriteback(page
));
577 metapage_releasepage(page
, 0);
580 const struct address_space_operations jfs_metapage_aops
= {
581 .readpage
= metapage_readpage
,
582 .writepage
= metapage_writepage
,
583 .sync_page
= block_sync_page
,
584 .releasepage
= metapage_releasepage
,
585 .invalidatepage
= metapage_invalidatepage
,
586 .set_page_dirty
= __set_page_dirty_nobuffers
,
589 struct metapage
*__get_metapage(struct inode
*inode
, unsigned long lblock
,
590 unsigned int size
, int absolute
,
595 struct address_space
*mapping
;
596 struct metapage
*mp
= NULL
;
598 unsigned long page_index
;
599 unsigned long page_offset
;
601 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
602 inode
->i_ino
, lblock
, absolute
);
604 l2bsize
= inode
->i_blkbits
;
605 l2BlocksPerPage
= PAGE_CACHE_SHIFT
- l2bsize
;
606 page_index
= lblock
>> l2BlocksPerPage
;
607 page_offset
= (lblock
- (page_index
<< l2BlocksPerPage
)) << l2bsize
;
608 if ((page_offset
+ size
) > PAGE_CACHE_SIZE
) {
609 jfs_err("MetaData crosses page boundary!!");
610 jfs_err("lblock = %lx, size = %d", lblock
, size
);
615 mapping
= JFS_SBI(inode
->i_sb
)->direct_inode
->i_mapping
;
618 * If an nfs client tries to read an inode that is larger
619 * than any existing inodes, we may try to read past the
620 * end of the inode map
622 if ((lblock
<< inode
->i_blkbits
) >= inode
->i_size
)
624 mapping
= inode
->i_mapping
;
627 if (new && (PSIZE
== PAGE_CACHE_SIZE
)) {
628 page
= grab_cache_page(mapping
, page_index
);
630 jfs_err("grab_cache_page failed!");
633 SetPageUptodate(page
);
635 page
= read_mapping_page(mapping
, page_index
, NULL
);
636 if (IS_ERR(page
) || !PageUptodate(page
)) {
637 jfs_err("read_mapping_page failed!");
643 mp
= page_to_mp(page
, page_offset
);
645 if (mp
->logical_size
!= size
) {
646 jfs_error(inode
->i_sb
,
647 "__get_metapage: mp->logical_size != size");
648 jfs_err("logical_size = %d, size = %d",
649 mp
->logical_size
, size
);
655 if (test_bit(META_discard
, &mp
->flag
)) {
657 jfs_error(inode
->i_sb
,
658 "__get_metapage: using a "
659 "discarded metapage");
660 discard_metapage(mp
);
663 clear_bit(META_discard
, &mp
->flag
);
666 INCREMENT(mpStat
.pagealloc
);
667 mp
= alloc_metapage(GFP_NOFS
);
670 mp
->xflag
= COMMIT_PAGE
;
673 mp
->logical_size
= size
;
674 mp
->data
= page_address(page
) + page_offset
;
676 if (unlikely(insert_metapage(page
, mp
))) {
684 jfs_info("zeroing mp = 0x%p", mp
);
685 memset(mp
->data
, 0, PSIZE
);
689 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp
, mp
->data
);
697 void grab_metapage(struct metapage
* mp
)
699 jfs_info("grab_metapage: mp = 0x%p", mp
);
700 page_cache_get(mp
->page
);
704 unlock_page(mp
->page
);
707 void force_metapage(struct metapage
*mp
)
709 struct page
*page
= mp
->page
;
710 jfs_info("force_metapage: mp = 0x%p", mp
);
711 set_bit(META_forcewrite
, &mp
->flag
);
712 clear_bit(META_sync
, &mp
->flag
);
713 page_cache_get(page
);
715 set_page_dirty(page
);
716 write_one_page(page
, 1);
717 clear_bit(META_forcewrite
, &mp
->flag
);
718 page_cache_release(page
);
721 void hold_metapage(struct metapage
*mp
)
726 void put_metapage(struct metapage
*mp
)
728 if (mp
->count
|| mp
->nohomeok
) {
729 /* Someone else will release this */
730 unlock_page(mp
->page
);
733 page_cache_get(mp
->page
);
736 unlock_page(mp
->page
);
737 release_metapage(mp
);
740 void release_metapage(struct metapage
* mp
)
742 struct page
*page
= mp
->page
;
743 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp
, mp
->flag
);
751 if (--mp
->count
|| mp
->nohomeok
) {
753 page_cache_release(page
);
757 if (test_bit(META_dirty
, &mp
->flag
)) {
758 set_page_dirty(page
);
759 if (test_bit(META_sync
, &mp
->flag
)) {
760 clear_bit(META_sync
, &mp
->flag
);
761 write_one_page(page
, 1);
762 lock_page(page
); /* write_one_page unlocks the page */
764 } else if (mp
->lsn
) /* discard_metapage doesn't remove it */
765 remove_from_logsync(mp
);
767 #if MPS_PER_PAGE == 1
769 * If we know this is the only thing in the page, we can throw
770 * the page out of the page cache. If pages are larger, we
771 * don't want to do this.
774 /* Retest mp->count since we may have released page lock */
775 if (test_bit(META_discard
, &mp
->flag
) && !mp
->count
) {
776 clear_page_dirty(page
);
777 ClearPageUptodate(page
);
780 /* Try to keep metapages from using up too much memory */
781 drop_metapage(page
, mp
);
784 page_cache_release(page
);
787 void __invalidate_metapages(struct inode
*ip
, s64 addr
, int len
)
790 int l2BlocksPerPage
= PAGE_CACHE_SHIFT
- ip
->i_blkbits
;
791 int BlocksPerPage
= 1 << l2BlocksPerPage
;
792 /* All callers are interested in block device's mapping */
793 struct address_space
*mapping
=
794 JFS_SBI(ip
->i_sb
)->direct_inode
->i_mapping
;
800 * Mark metapages to discard. They will eventually be
801 * released, but should not be written.
803 for (lblock
= addr
& ~(BlocksPerPage
- 1); lblock
< addr
+ len
;
804 lblock
+= BlocksPerPage
) {
805 page
= find_lock_page(mapping
, lblock
>> l2BlocksPerPage
);
808 for (offset
= 0; offset
< PAGE_CACHE_SIZE
; offset
+= PSIZE
) {
809 mp
= page_to_mp(page
, offset
);
812 if (mp
->index
< addr
)
814 if (mp
->index
>= addr
+ len
)
817 clear_bit(META_dirty
, &mp
->flag
);
818 set_bit(META_discard
, &mp
->flag
);
820 remove_from_logsync(mp
);
823 page_cache_release(page
);
827 #ifdef CONFIG_JFS_STATISTICS
828 int jfs_mpstat_read(char *buffer
, char **start
, off_t offset
, int length
,
829 int *eof
, void *data
)
834 len
+= sprintf(buffer
,
835 "JFS Metapage statistics\n"
836 "=======================\n"
837 "page allocations = %d\n"
845 *start
= buffer
+ begin
;