blob: c81c6438fce5c495fbc2930dcd3a1d302c364683 [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
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
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.
14 *
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
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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
77static kmem_cache_t *metapage_cache;
78static 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};
89#define mp_anchor(page) ((struct meta_anchor *)page->private)
90
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 {
107 a = kmalloc(sizeof(struct meta_anchor), GFP_NOFS);
108 if (!a)
109 return -ENOMEM;
110 memset(a, 0, sizeof(struct meta_anchor));
111 page->private = (unsigned long)a;
112 SetPagePrivate(page);
113 kmap(page);
114 }
115
116 if (mp) {
117 l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
118 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
119 a->mp_count++;
120 a->mp[index] = mp;
121 }
122
123 return 0;
124}
125
126static inline void remove_metapage(struct page *page, struct metapage *mp)
127{
128 struct meta_anchor *a = mp_anchor(page);
129 int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
130 int index;
131
132 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
133
134 BUG_ON(a->mp[index] != mp);
135
136 a->mp[index] = NULL;
137 if (--a->mp_count == 0) {
138 kfree(a);
139 page->private = 0;
140 ClearPagePrivate(page);
141 kunmap(page);
142 }
143}
144
145static inline void inc_io(struct page *page)
146{
147 atomic_inc(&mp_anchor(page)->io_count);
148}
149
150static inline void dec_io(struct page *page, void (*handler) (struct page *))
151{
152 if (atomic_dec_and_test(&mp_anchor(page)->io_count))
153 handler(page);
154}
155
156#else
157static inline struct metapage *page_to_mp(struct page *page, uint offset)
158{
159 return PagePrivate(page) ? (struct metapage *)page->private : NULL;
160}
161
162static inline int insert_metapage(struct page *page, struct metapage *mp)
163{
164 if (mp) {
165 page->private = (unsigned long)mp;
166 SetPagePrivate(page);
167 kmap(page);
168 }
169 return 0;
170}
171
172static inline void remove_metapage(struct page *page, struct metapage *mp)
173{
174 page->private = 0;
175 ClearPagePrivate(page);
176 kunmap(page);
177}
178
179#define inc_io(page) do {} while(0)
180#define dec_io(page, handler) handler(page)
181
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
185{
186 struct metapage *mp = (struct metapage *)foo;
187
188 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
189 SLAB_CTOR_CONSTRUCTOR) {
190 mp->lid = 0;
191 mp->lsn = 0;
192 mp->flag = 0;
193 mp->data = NULL;
194 mp->clsn = 0;
195 mp->log = NULL;
196 set_bit(META_free, &mp->flag);
197 init_waitqueue_head(&mp->wait);
198 }
199}
200
Dave Kleikamp7a694ca2005-05-04 15:31:14 -0500201static inline struct metapage *alloc_metapage(unsigned int gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 return mempool_alloc(metapage_mempool, gfp_mask);
204}
205
206static inline void free_metapage(struct metapage *mp)
207{
208 mp->flag = 0;
209 set_bit(META_free, &mp->flag);
210
211 mempool_free(mp, metapage_mempool);
212}
213
214int __init metapage_init(void)
215{
216 /*
217 * Allocate the metapage structures
218 */
219 metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
220 0, 0, init_once, NULL);
221 if (metapage_cache == NULL)
222 return -ENOMEM;
223
224 metapage_mempool = mempool_create(METAPOOL_MIN_PAGES, mempool_alloc_slab,
225 mempool_free_slab, metapage_cache);
226
227 if (metapage_mempool == NULL) {
228 kmem_cache_destroy(metapage_cache);
229 return -ENOMEM;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return 0;
233}
234
235void metapage_exit(void)
236{
237 mempool_destroy(metapage_mempool);
238 kmem_cache_destroy(metapage_cache);
239}
240
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600241static inline void drop_metapage(struct page *page, struct metapage *mp)
242{
243 if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
244 test_bit(META_io, &mp->flag))
245 return;
246 remove_metapage(page, mp);
247 INCREMENT(mpStat.pagefree);
248 free_metapage(mp);
249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600252 * Metapage address space operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600254
255static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
256 unsigned int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600258 int rc = 0;
259 int xflag;
260 s64 xaddr;
261 sector_t file_blocks = (inode->i_size + inode->i_blksize - 1) >>
262 inode->i_blkbits;
263
264 if (lblock >= file_blocks)
265 return 0;
266 if (lblock + *len > file_blocks)
267 *len = file_blocks - lblock;
268
269 if (inode->i_ino) {
270 rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
271 if ((rc == 0) && *len)
272 lblock = (sector_t)xaddr;
273 else
274 lblock = 0;
275 } /* else no mapping */
276
277 return lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600280static void last_read_complete(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600282 if (!PageError(page))
283 SetPageUptodate(page);
284 unlock_page(page);
285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600287static int metapage_read_end_io(struct bio *bio, unsigned int bytes_done,
288 int err)
289{
290 struct page *page = bio->bi_private;
291
292 if (bio->bi_size)
293 return 1;
294
295 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
296 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
297 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600300 dec_io(page, last_read_complete);
301 bio_put(bio);
302
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600306static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600308 struct jfs_log *log = mp->log;
309 unsigned long flags;
310/*
311 * This can race. Recheck that log hasn't been set to null, and after
312 * acquiring logsync lock, recheck lsn
313 */
314 if (!log)
315 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600317 LOGSYNC_LOCK(log, flags);
318 if (mp->lsn) {
319 mp->log = NULL;
320 mp->lsn = 0;
321 mp->clsn = 0;
322 log->count--;
323 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600325 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600328static void last_write_complete(struct page *page)
329{
330 struct metapage *mp;
331 unsigned int offset;
332
333 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
334 mp = page_to_mp(page, offset);
335 if (mp && test_bit(META_io, &mp->flag)) {
336 if (mp->lsn)
337 remove_from_logsync(mp);
338 clear_bit(META_io, &mp->flag);
339 }
340 /*
341 * I'd like to call drop_metapage here, but I don't think it's
342 * safe unless I have the page locked
343 */
344 }
345 end_page_writeback(page);
346}
347
348static int metapage_write_end_io(struct bio *bio, unsigned int bytes_done,
349 int err)
350{
351 struct page *page = bio->bi_private;
352
353 BUG_ON(!PagePrivate(page));
354
355 if (bio->bi_size)
356 return 1;
357
358 if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
359 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
360 SetPageError(page);
361 }
362 dec_io(page, last_write_complete);
363 bio_put(bio);
364 return 0;
365}
366
367static int metapage_writepage(struct page *page, struct writeback_control *wbc)
368{
369 struct bio *bio = NULL;
370 unsigned int block_offset; /* block offset of mp within page */
371 struct inode *inode = page->mapping->host;
372 unsigned int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
373 unsigned int len;
374 unsigned int xlen;
375 struct metapage *mp;
376 int redirty = 0;
377 sector_t lblock;
378 sector_t pblock;
379 sector_t next_block = 0;
380 sector_t page_start;
381 unsigned long bio_bytes = 0;
382 unsigned long bio_offset = 0;
383 unsigned int offset;
384
385 page_start = (sector_t)page->index <<
386 (PAGE_CACHE_SHIFT - inode->i_blkbits);
387 BUG_ON(!PageLocked(page));
388 BUG_ON(PageWriteback(page));
389
390 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
391 mp = page_to_mp(page, offset);
392
393 if (!mp || !test_bit(META_dirty, &mp->flag))
394 continue;
395
396 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
397 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500398 /*
399 * Make sure this page isn't blocked indefinitely.
400 * If the journal isn't undergoing I/O, push it
401 */
402 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
403 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600404 continue;
405 }
406
407 clear_bit(META_dirty, &mp->flag);
408 block_offset = offset >> inode->i_blkbits;
409 lblock = page_start + block_offset;
410 if (bio) {
411 if (xlen && lblock == next_block) {
412 /* Contiguous, in memory & on disk */
413 len = min(xlen, blocks_per_mp);
414 xlen -= len;
415 bio_bytes += len << inode->i_blkbits;
416 set_bit(META_io, &mp->flag);
417 continue;
418 }
419 /* Not contiguous */
420 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
421 bio_bytes)
422 goto add_failed;
423 /*
424 * Increment counter before submitting i/o to keep
425 * count from hitting zero before we're through
426 */
427 inc_io(page);
428 if (!bio->bi_size)
429 goto dump_bio;
430 submit_bio(WRITE, bio);
431 bio = NULL;
432 } else {
433 set_page_writeback(page);
434 inc_io(page);
435 }
436 xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
437 pblock = metapage_get_blocks(inode, lblock, &xlen);
438 if (!pblock) {
439 /* Need better error handling */
440 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
441 dec_io(page, last_write_complete);
442 continue;
443 }
444 set_bit(META_io, &mp->flag);
445 len = min(xlen, (uint) JFS_SBI(inode->i_sb)->nbperpage);
446
447 bio = bio_alloc(GFP_NOFS, 1);
448 bio->bi_bdev = inode->i_sb->s_bdev;
449 bio->bi_sector = pblock << (inode->i_blkbits - 9);
450 bio->bi_end_io = metapage_write_end_io;
451 bio->bi_private = page;
452
453 /* Don't call bio_add_page yet, we may add to this vec */
454 bio_offset = offset;
455 bio_bytes = len << inode->i_blkbits;
456
457 xlen -= len;
458 next_block = lblock + len;
459 }
460 if (bio) {
461 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
462 goto add_failed;
463 if (!bio->bi_size)
464 goto dump_bio;
465
466 submit_bio(WRITE, bio);
467 }
468 if (redirty)
469 redirty_page_for_writepage(wbc, page);
470
471 unlock_page(page);
472
473 return 0;
474add_failed:
475 /* We should never reach here, since we're only adding one vec */
476 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
477 goto skip;
478dump_bio:
479 dump_mem("bio", bio, sizeof(*bio));
480skip:
481 bio_put(bio);
482 unlock_page(page);
483 dec_io(page, last_write_complete);
484
485 return -EIO;
486}
487
488static int metapage_readpage(struct file *fp, struct page *page)
489{
490 struct inode *inode = page->mapping->host;
491 struct bio *bio = NULL;
492 unsigned int block_offset;
493 unsigned int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
494 sector_t page_start; /* address of page in fs blocks */
495 sector_t pblock;
496 unsigned int xlen;
497 unsigned int len;
498 unsigned int offset;
499
500 BUG_ON(!PageLocked(page));
501 page_start = (sector_t)page->index <<
502 (PAGE_CACHE_SHIFT - inode->i_blkbits);
503
504 block_offset = 0;
505 while (block_offset < blocks_per_page) {
506 xlen = blocks_per_page - block_offset;
507 pblock = metapage_get_blocks(inode, page_start + block_offset,
508 &xlen);
509 if (pblock) {
510 if (!PagePrivate(page))
511 insert_metapage(page, NULL);
512 inc_io(page);
513 if (bio)
514 submit_bio(READ, bio);
515
516 bio = bio_alloc(GFP_NOFS, 1);
517 bio->bi_bdev = inode->i_sb->s_bdev;
518 bio->bi_sector = pblock << (inode->i_blkbits - 9);
519 bio->bi_end_io = metapage_read_end_io;
520 bio->bi_private = page;
521 len = xlen << inode->i_blkbits;
522 offset = block_offset << inode->i_blkbits;
523 if (bio_add_page(bio, page, len, offset) < len)
524 goto add_failed;
525 block_offset += xlen;
526 } else
527 block_offset++;
528 }
529 if (bio)
530 submit_bio(READ, bio);
531 else
532 unlock_page(page);
533
534 return 0;
535
536add_failed:
537 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
538 bio_put(bio);
539 dec_io(page, last_read_complete);
540 return -EIO;
541}
542
543static int metapage_releasepage(struct page *page, int gfp_mask)
544{
545 struct metapage *mp;
546 int busy = 0;
547 unsigned int offset;
548
549 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
550 mp = page_to_mp(page, offset);
551
552 if (!mp)
553 continue;
554
555 jfs_info("metapage_releasepage: mp = 0x%p", mp);
556 if (mp->count || mp->nohomeok) {
557 jfs_info("count = %ld, nohomeok = %d", mp->count,
558 mp->nohomeok);
559 busy = 1;
560 continue;
561 }
562 wait_on_page_writeback(page);
563 //WARN_ON(test_bit(META_dirty, &mp->flag));
564 if (test_bit(META_dirty, &mp->flag)) {
565 dump_mem("dirty mp in metapage_releasepage", mp,
566 sizeof(struct metapage));
567 dump_mem("page", page, sizeof(struct page));
568 dump_stack();
569 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600570 if (mp->lsn)
571 remove_from_logsync(mp);
572 remove_metapage(page, mp);
573 INCREMENT(mpStat.pagefree);
574 free_metapage(mp);
575 }
576 if (busy)
577 return -1;
578
579 return 0;
580}
581
582static int metapage_invalidatepage(struct page *page, unsigned long offset)
583{
584 BUG_ON(offset);
585
586 if (PageWriteback(page))
587 return 0;
588
589 return metapage_releasepage(page, 0);
590}
591
592struct address_space_operations jfs_metapage_aops = {
593 .readpage = metapage_readpage,
594 .writepage = metapage_writepage,
595 .sync_page = block_sync_page,
596 .releasepage = metapage_releasepage,
597 .invalidatepage = metapage_invalidatepage,
598 .set_page_dirty = __set_page_dirty_nobuffers,
599};
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
602 unsigned int size, int absolute,
603 unsigned long new)
604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int l2BlocksPerPage;
606 int l2bsize;
607 struct address_space *mapping;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600608 struct metapage *mp = NULL;
609 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 unsigned long page_index;
611 unsigned long page_offset;
612
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600613 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
614 inode->i_ino, lblock, absolute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600616 l2bsize = inode->i_blkbits;
617 l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
618 page_index = lblock >> l2BlocksPerPage;
619 page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
620 if ((page_offset + size) > PAGE_CACHE_SIZE) {
621 jfs_err("MetaData crosses page boundary!!");
622 jfs_err("lblock = %lx, size = %d", lblock, size);
623 dump_stack();
624 return NULL;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (absolute)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600627 mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 else {
629 /*
630 * If an nfs client tries to read an inode that is larger
631 * than any existing inodes, we may try to read past the
632 * end of the inode map
633 */
634 if ((lblock << inode->i_blkbits) >= inode->i_size)
635 return NULL;
636 mapping = inode->i_mapping;
637 }
638
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600639 if (new && (PSIZE == PAGE_CACHE_SIZE)) {
640 page = grab_cache_page(mapping, page_index);
641 if (!page) {
642 jfs_err("grab_cache_page failed!");
643 return NULL;
644 }
645 SetPageUptodate(page);
646 } else {
647 page = read_cache_page(mapping, page_index,
648 (filler_t *)mapping->a_ops->readpage, NULL);
Dave Kleikampc40c20242005-07-22 11:08:44 -0500649 if (IS_ERR(page) || !PageUptodate(page)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600650 jfs_err("read_cache_page failed!");
651 return NULL;
652 }
653 lock_page(page);
654 }
655
656 mp = page_to_mp(page, page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (mp) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600658 if (mp->logical_size != size) {
659 jfs_error(inode->i_sb,
660 "__get_metapage: mp->logical_size != size");
661 jfs_err("logical_size = %d, size = %d",
662 mp->logical_size, size);
663 dump_stack();
664 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 mp->count++;
667 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (test_bit(META_discard, &mp->flag)) {
669 if (!new) {
670 jfs_error(inode->i_sb,
671 "__get_metapage: using a "
672 "discarded metapage");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600673 discard_metapage(mp);
674 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 clear_bit(META_discard, &mp->flag);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600679 INCREMENT(mpStat.pagealloc);
680 mp = alloc_metapage(GFP_NOFS);
681 mp->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 mp->xflag = COMMIT_PAGE;
684 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600685 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600687 mp->data = page_address(page) + page_offset;
688 mp->index = lblock;
689 if (unlikely(insert_metapage(page, mp))) {
690 free_metapage(mp);
691 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600693 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600696 if (new) {
697 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600701 unlock_page(page);
702 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return mp;
704
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600705unlock:
706 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return NULL;
708}
709
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600710void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600712 jfs_info("grab_metapage: mp = 0x%p", mp);
713 page_cache_get(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600715 mp->count++;
716 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600720void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600723 jfs_info("force_metapage: mp = 0x%p", mp);
724 set_bit(META_forcewrite, &mp->flag);
725 clear_bit(META_sync, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 page_cache_get(page);
727 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600728 set_page_dirty(page);
729 write_one_page(page, 1);
730 clear_bit(META_forcewrite, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 page_cache_release(page);
732}
733
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500734void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600735{
736 lock_page(mp->page);
737}
738
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500739void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600740{
741 if (mp->count || mp->nohomeok) {
742 /* Someone else will release this */
743 unlock_page(mp->page);
744 return;
745 }
746 page_cache_get(mp->page);
747 mp->count++;
748 lock_metapage(mp);
749 unlock_page(mp->page);
750 release_metapage(mp);
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753void release_metapage(struct metapage * mp)
754{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600755 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
757
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600758 BUG_ON(!page);
759
760 lock_page(page);
761 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600764 if (--mp->count || mp->nohomeok) {
765 unlock_page(page);
766 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return;
768 }
769
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600770 if (test_bit(META_dirty, &mp->flag)) {
771 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 clear_bit(META_sync, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600774 write_one_page(page, 1);
775 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600777 } else if (mp->lsn) /* discard_metapage doesn't remove it */
778 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600780#if MPS_PER_PAGE == 1
781 /*
782 * If we know this is the only thing in the page, we can throw
783 * the page out of the page cache. If pages are larger, we
784 * don't want to do this.
785 */
786
787 /* Retest mp->count since we may have released page lock */
788 if (test_bit(META_discard, &mp->flag) && !mp->count) {
789 clear_page_dirty(page);
790 ClearPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600792#else
793 /* Try to keep metapages from using up too much memory */
794 drop_metapage(page, mp);
795#endif
796 unlock_page(page);
797 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800void __invalidate_metapages(struct inode *ip, s64 addr, int len)
801{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600802 sector_t lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600804 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600806 struct address_space *mapping =
807 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct metapage *mp;
809 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600810 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600813 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * released, but should not be written.
815 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600816 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
817 lblock += BlocksPerPage) {
818 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
819 if (!page)
820 continue;
821 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
822 mp = page_to_mp(page, offset);
823 if (!mp)
824 continue;
825 if (mp->index < addr)
826 continue;
827 if (mp->index >= addr + len)
828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 clear_bit(META_dirty, &mp->flag);
831 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600832 if (mp->lsn)
833 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600835 unlock_page(page);
836 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838}
839
840#ifdef CONFIG_JFS_STATISTICS
841int jfs_mpstat_read(char *buffer, char **start, off_t offset, int length,
842 int *eof, void *data)
843{
844 int len = 0;
845 off_t begin;
846
847 len += sprintf(buffer,
848 "JFS Metapage statistics\n"
849 "=======================\n"
850 "page allocations = %d\n"
851 "page frees = %d\n"
852 "lock waits = %d\n",
853 mpStat.pagealloc,
854 mpStat.pagefree,
855 mpStat.lockwait);
856
857 begin = offset;
858 *start = buffer + begin;
859 len -= begin;
860
861 if (len > length)
862 len = length;
863 else
864 *eof = 1;
865
866 if (len < 0)
867 len = 0;
868
869 return len;
870}
871#endif