blob: 43d4f69afbeca923878a45d77fa68d4eb53a55f9 [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 Lametere18b8902006-12-06 20:33:20 -0800183static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
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),
216 0, 0, init_once, NULL);
217 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
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600283static int metapage_read_end_io(struct bio *bio, unsigned int bytes_done,
284 int err)
285{
286 struct page *page = bio->bi_private;
287
288 if (bio->bi_size)
289 return 1;
290
291 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
292 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
293 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600296 dec_io(page, last_read_complete);
297 bio_put(bio);
298
299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600302static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600304 struct jfs_log *log = mp->log;
305 unsigned long flags;
306/*
307 * This can race. Recheck that log hasn't been set to null, and after
308 * acquiring logsync lock, recheck lsn
309 */
310 if (!log)
311 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600313 LOGSYNC_LOCK(log, flags);
314 if (mp->lsn) {
315 mp->log = NULL;
316 mp->lsn = 0;
317 mp->clsn = 0;
318 log->count--;
319 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600321 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600324static void last_write_complete(struct page *page)
325{
326 struct metapage *mp;
327 unsigned int offset;
328
329 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
330 mp = page_to_mp(page, offset);
331 if (mp && test_bit(META_io, &mp->flag)) {
332 if (mp->lsn)
333 remove_from_logsync(mp);
334 clear_bit(META_io, &mp->flag);
335 }
336 /*
337 * I'd like to call drop_metapage here, but I don't think it's
338 * safe unless I have the page locked
339 */
340 }
341 end_page_writeback(page);
342}
343
344static int metapage_write_end_io(struct bio *bio, unsigned int bytes_done,
345 int err)
346{
347 struct page *page = bio->bi_private;
348
349 BUG_ON(!PagePrivate(page));
350
351 if (bio->bi_size)
352 return 1;
353
354 if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
355 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
356 SetPageError(page);
357 }
358 dec_io(page, last_write_complete);
359 bio_put(bio);
360 return 0;
361}
362
363static int metapage_writepage(struct page *page, struct writeback_control *wbc)
364{
365 struct bio *bio = NULL;
366 unsigned int block_offset; /* block offset of mp within page */
367 struct inode *inode = page->mapping->host;
368 unsigned int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
369 unsigned int len;
370 unsigned int xlen;
371 struct metapage *mp;
372 int redirty = 0;
373 sector_t lblock;
374 sector_t pblock;
375 sector_t next_block = 0;
376 sector_t page_start;
377 unsigned long bio_bytes = 0;
378 unsigned long bio_offset = 0;
379 unsigned int offset;
380
381 page_start = (sector_t)page->index <<
382 (PAGE_CACHE_SHIFT - inode->i_blkbits);
383 BUG_ON(!PageLocked(page));
384 BUG_ON(PageWriteback(page));
385
386 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
387 mp = page_to_mp(page, offset);
388
389 if (!mp || !test_bit(META_dirty, &mp->flag))
390 continue;
391
392 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
393 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500394 /*
395 * Make sure this page isn't blocked indefinitely.
396 * If the journal isn't undergoing I/O, push it
397 */
398 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
399 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600400 continue;
401 }
402
403 clear_bit(META_dirty, &mp->flag);
404 block_offset = offset >> inode->i_blkbits;
405 lblock = page_start + block_offset;
406 if (bio) {
407 if (xlen && lblock == next_block) {
408 /* Contiguous, in memory & on disk */
409 len = min(xlen, blocks_per_mp);
410 xlen -= len;
411 bio_bytes += len << inode->i_blkbits;
412 set_bit(META_io, &mp->flag);
413 continue;
414 }
415 /* Not contiguous */
416 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
417 bio_bytes)
418 goto add_failed;
419 /*
420 * Increment counter before submitting i/o to keep
421 * count from hitting zero before we're through
422 */
423 inc_io(page);
424 if (!bio->bi_size)
425 goto dump_bio;
426 submit_bio(WRITE, bio);
427 bio = NULL;
428 } else {
429 set_page_writeback(page);
430 inc_io(page);
431 }
432 xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
433 pblock = metapage_get_blocks(inode, lblock, &xlen);
434 if (!pblock) {
435 /* Need better error handling */
436 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
437 dec_io(page, last_write_complete);
438 continue;
439 }
440 set_bit(META_io, &mp->flag);
441 len = min(xlen, (uint) JFS_SBI(inode->i_sb)->nbperpage);
442
443 bio = bio_alloc(GFP_NOFS, 1);
444 bio->bi_bdev = inode->i_sb->s_bdev;
445 bio->bi_sector = pblock << (inode->i_blkbits - 9);
446 bio->bi_end_io = metapage_write_end_io;
447 bio->bi_private = page;
448
449 /* Don't call bio_add_page yet, we may add to this vec */
450 bio_offset = offset;
451 bio_bytes = len << inode->i_blkbits;
452
453 xlen -= len;
454 next_block = lblock + len;
455 }
456 if (bio) {
457 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
458 goto add_failed;
459 if (!bio->bi_size)
460 goto dump_bio;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500461
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600462 submit_bio(WRITE, bio);
463 }
464 if (redirty)
465 redirty_page_for_writepage(wbc, page);
466
467 unlock_page(page);
468
469 return 0;
470add_failed:
471 /* We should never reach here, since we're only adding one vec */
472 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
473 goto skip;
474dump_bio:
475 dump_mem("bio", bio, sizeof(*bio));
476skip:
477 bio_put(bio);
478 unlock_page(page);
479 dec_io(page, last_write_complete);
480
481 return -EIO;
482}
483
484static int metapage_readpage(struct file *fp, struct page *page)
485{
486 struct inode *inode = page->mapping->host;
487 struct bio *bio = NULL;
488 unsigned int block_offset;
489 unsigned int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
490 sector_t page_start; /* address of page in fs blocks */
491 sector_t pblock;
492 unsigned int xlen;
493 unsigned int len;
494 unsigned int offset;
495
496 BUG_ON(!PageLocked(page));
497 page_start = (sector_t)page->index <<
498 (PAGE_CACHE_SHIFT - inode->i_blkbits);
499
500 block_offset = 0;
501 while (block_offset < blocks_per_page) {
502 xlen = blocks_per_page - block_offset;
503 pblock = metapage_get_blocks(inode, page_start + block_offset,
504 &xlen);
505 if (pblock) {
506 if (!PagePrivate(page))
507 insert_metapage(page, NULL);
508 inc_io(page);
509 if (bio)
510 submit_bio(READ, bio);
511
512 bio = bio_alloc(GFP_NOFS, 1);
513 bio->bi_bdev = inode->i_sb->s_bdev;
514 bio->bi_sector = pblock << (inode->i_blkbits - 9);
515 bio->bi_end_io = metapage_read_end_io;
516 bio->bi_private = page;
517 len = xlen << inode->i_blkbits;
518 offset = block_offset << inode->i_blkbits;
519 if (bio_add_page(bio, page, len, offset) < len)
520 goto add_failed;
521 block_offset += xlen;
522 } else
523 block_offset++;
524 }
525 if (bio)
526 submit_bio(READ, bio);
527 else
528 unlock_page(page);
529
530 return 0;
531
532add_failed:
533 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
534 bio_put(bio);
535 dec_io(page, last_read_complete);
536 return -EIO;
537}
538
Al Viro27496a82005-10-21 03:20:48 -0400539static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600540{
541 struct metapage *mp;
Dave Kleikampb9646382006-05-24 07:43:38 -0500542 int ret = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600543 unsigned int offset;
544
545 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
546 mp = page_to_mp(page, offset);
547
548 if (!mp)
549 continue;
550
551 jfs_info("metapage_releasepage: mp = 0x%p", mp);
Dave Kleikampb9646382006-05-24 07:43:38 -0500552 if (mp->count || mp->nohomeok ||
553 test_bit(META_dirty, &mp->flag)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600554 jfs_info("count = %ld, nohomeok = %d", mp->count,
555 mp->nohomeok);
Dave Kleikampb9646382006-05-24 07:43:38 -0500556 ret = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600557 continue;
558 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600559 if (mp->lsn)
560 remove_from_logsync(mp);
561 remove_metapage(page, mp);
562 INCREMENT(mpStat.pagefree);
563 free_metapage(mp);
564 }
Dave Kleikampb9646382006-05-24 07:43:38 -0500565 return ret;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600566}
567
NeilBrown2ff28e22006-03-26 01:37:18 -0800568static void metapage_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600569{
570 BUG_ON(offset);
571
NeilBrown2ff28e22006-03-26 01:37:18 -0800572 BUG_ON(PageWriteback(page));
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600573
NeilBrown2ff28e22006-03-26 01:37:18 -0800574 metapage_releasepage(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600575}
576
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700577const struct address_space_operations jfs_metapage_aops = {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600578 .readpage = metapage_readpage,
579 .writepage = metapage_writepage,
580 .sync_page = block_sync_page,
581 .releasepage = metapage_releasepage,
582 .invalidatepage = metapage_invalidatepage,
583 .set_page_dirty = __set_page_dirty_nobuffers,
584};
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
587 unsigned int size, int absolute,
588 unsigned long new)
589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int l2BlocksPerPage;
591 int l2bsize;
592 struct address_space *mapping;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600593 struct metapage *mp = NULL;
594 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned long page_index;
596 unsigned long page_offset;
597
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600598 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
599 inode->i_ino, lblock, absolute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600601 l2bsize = inode->i_blkbits;
602 l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
603 page_index = lblock >> l2BlocksPerPage;
604 page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
605 if ((page_offset + size) > PAGE_CACHE_SIZE) {
606 jfs_err("MetaData crosses page boundary!!");
607 jfs_err("lblock = %lx, size = %d", lblock, size);
608 dump_stack();
609 return NULL;
610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (absolute)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600612 mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else {
614 /*
615 * If an nfs client tries to read an inode that is larger
616 * than any existing inodes, we may try to read past the
617 * end of the inode map
618 */
619 if ((lblock << inode->i_blkbits) >= inode->i_size)
620 return NULL;
621 mapping = inode->i_mapping;
622 }
623
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600624 if (new && (PSIZE == PAGE_CACHE_SIZE)) {
625 page = grab_cache_page(mapping, page_index);
626 if (!page) {
627 jfs_err("grab_cache_page failed!");
628 return NULL;
629 }
630 SetPageUptodate(page);
631 } else {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700632 page = read_mapping_page(mapping, page_index, NULL);
Dave Kleikampc40c20242005-07-22 11:08:44 -0500633 if (IS_ERR(page) || !PageUptodate(page)) {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700634 jfs_err("read_mapping_page failed!");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600635 return NULL;
636 }
637 lock_page(page);
638 }
639
640 mp = page_to_mp(page, page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (mp) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600642 if (mp->logical_size != size) {
643 jfs_error(inode->i_sb,
644 "__get_metapage: mp->logical_size != size");
645 jfs_err("logical_size = %d, size = %d",
646 mp->logical_size, size);
647 dump_stack();
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500648 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 mp->count++;
651 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (test_bit(META_discard, &mp->flag)) {
653 if (!new) {
654 jfs_error(inode->i_sb,
655 "__get_metapage: using a "
656 "discarded metapage");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600657 discard_metapage(mp);
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500658 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 clear_bit(META_discard, &mp->flag);
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600663 INCREMENT(mpStat.pagealloc);
664 mp = alloc_metapage(GFP_NOFS);
665 mp->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 mp->xflag = COMMIT_PAGE;
668 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600669 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600671 mp->data = page_address(page) + page_offset;
672 mp->index = lblock;
673 if (unlikely(insert_metapage(page, mp))) {
674 free_metapage(mp);
675 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600677 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600680 if (new) {
681 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600685 unlock_page(page);
686 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return mp;
688
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600689unlock:
690 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return NULL;
692}
693
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600694void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600696 jfs_info("grab_metapage: mp = 0x%p", mp);
697 page_cache_get(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600699 mp->count++;
700 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600704void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600707 jfs_info("force_metapage: mp = 0x%p", mp);
708 set_bit(META_forcewrite, &mp->flag);
709 clear_bit(META_sync, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 page_cache_get(page);
711 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600712 set_page_dirty(page);
713 write_one_page(page, 1);
714 clear_bit(META_forcewrite, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 page_cache_release(page);
716}
717
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500718void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600719{
720 lock_page(mp->page);
721}
722
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500723void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600724{
725 if (mp->count || mp->nohomeok) {
726 /* Someone else will release this */
727 unlock_page(mp->page);
728 return;
729 }
730 page_cache_get(mp->page);
731 mp->count++;
732 lock_metapage(mp);
733 unlock_page(mp->page);
734 release_metapage(mp);
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737void release_metapage(struct metapage * mp)
738{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600739 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
741
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600742 BUG_ON(!page);
743
744 lock_page(page);
745 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600748 if (--mp->count || mp->nohomeok) {
749 unlock_page(page);
750 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752 }
753
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600754 if (test_bit(META_dirty, &mp->flag)) {
755 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 clear_bit(META_sync, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600758 write_one_page(page, 1);
759 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600761 } else if (mp->lsn) /* discard_metapage doesn't remove it */
762 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600764 /* Try to keep metapages from using up too much memory */
765 drop_metapage(page, mp);
Dave Kleikampd0e671a2006-12-20 15:55:35 -0600766
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600767 unlock_page(page);
768 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771void __invalidate_metapages(struct inode *ip, s64 addr, int len)
772{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600773 sector_t lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600775 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600777 struct address_space *mapping =
778 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct metapage *mp;
780 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600781 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600784 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * released, but should not be written.
786 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600787 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
788 lblock += BlocksPerPage) {
789 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
790 if (!page)
791 continue;
792 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
793 mp = page_to_mp(page, offset);
794 if (!mp)
795 continue;
796 if (mp->index < addr)
797 continue;
798 if (mp->index >= addr + len)
799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 clear_bit(META_dirty, &mp->flag);
802 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600803 if (mp->lsn)
804 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600806 unlock_page(page);
807 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809}
810
811#ifdef CONFIG_JFS_STATISTICS
812int jfs_mpstat_read(char *buffer, char **start, off_t offset, int length,
813 int *eof, void *data)
814{
815 int len = 0;
816 off_t begin;
817
818 len += sprintf(buffer,
819 "JFS Metapage statistics\n"
820 "=======================\n"
821 "page allocations = %d\n"
822 "page frees = %d\n"
823 "lock waits = %d\n",
824 mpStat.pagealloc,
825 mpStat.pagefree,
826 mpStat.lockwait);
827
828 begin = offset;
829 *start = buffer + begin;
830 len -= begin;
831
832 if (len > length)
833 len = length;
834 else
835 *eof = 1;
836
837 if (len < 0)
838 len = 0;
839
840 return len;
841}
842#endif