blob: 6b3acb0b5781180a91970ee1c550b7314037a888 [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 Lameter50953fe2007-05-06 14:50:16 -0700187 if (flags & SLAB_CTOR_CONSTRUCTOR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 mp->lid = 0;
189 mp->lsn = 0;
190 mp->flag = 0;
191 mp->data = NULL;
192 mp->clsn = 0;
193 mp->log = NULL;
194 set_bit(META_free, &mp->flag);
195 init_waitqueue_head(&mp->wait);
196 }
197}
198
Al Viro27496a82005-10-21 03:20:48 -0400199static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 return mempool_alloc(metapage_mempool, gfp_mask);
202}
203
204static inline void free_metapage(struct metapage *mp)
205{
206 mp->flag = 0;
207 set_bit(META_free, &mp->flag);
208
209 mempool_free(mp, metapage_mempool);
210}
211
212int __init metapage_init(void)
213{
214 /*
215 * Allocate the metapage structures
216 */
217 metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
218 0, 0, init_once, NULL);
219 if (metapage_cache == NULL)
220 return -ENOMEM;
221
Matthew Dobson93d23412006-03-26 01:37:50 -0800222 metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES,
223 metapage_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (metapage_mempool == NULL) {
226 kmem_cache_destroy(metapage_cache);
227 return -ENOMEM;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 return 0;
231}
232
233void metapage_exit(void)
234{
235 mempool_destroy(metapage_mempool);
236 kmem_cache_destroy(metapage_cache);
237}
238
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600239static inline void drop_metapage(struct page *page, struct metapage *mp)
240{
241 if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
242 test_bit(META_io, &mp->flag))
243 return;
244 remove_metapage(page, mp);
245 INCREMENT(mpStat.pagefree);
246 free_metapage(mp);
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600250 * Metapage address space operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600252
253static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
254 unsigned int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600256 int rc = 0;
257 int xflag;
258 s64 xaddr;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700259 sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600260 inode->i_blkbits;
261
262 if (lblock >= file_blocks)
263 return 0;
264 if (lblock + *len > file_blocks)
265 *len = file_blocks - lblock;
266
267 if (inode->i_ino) {
268 rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
269 if ((rc == 0) && *len)
270 lblock = (sector_t)xaddr;
271 else
272 lblock = 0;
273 } /* else no mapping */
274
275 return lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600278static void last_read_complete(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600280 if (!PageError(page))
281 SetPageUptodate(page);
282 unlock_page(page);
283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600285static int metapage_read_end_io(struct bio *bio, unsigned int bytes_done,
286 int err)
287{
288 struct page *page = bio->bi_private;
289
290 if (bio->bi_size)
291 return 1;
292
293 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
294 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
295 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600298 dec_io(page, last_read_complete);
299 bio_put(bio);
300
301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600304static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600306 struct jfs_log *log = mp->log;
307 unsigned long flags;
308/*
309 * This can race. Recheck that log hasn't been set to null, and after
310 * acquiring logsync lock, recheck lsn
311 */
312 if (!log)
313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600315 LOGSYNC_LOCK(log, flags);
316 if (mp->lsn) {
317 mp->log = NULL;
318 mp->lsn = 0;
319 mp->clsn = 0;
320 log->count--;
321 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600323 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600326static void last_write_complete(struct page *page)
327{
328 struct metapage *mp;
329 unsigned int offset;
330
331 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
332 mp = page_to_mp(page, offset);
333 if (mp && test_bit(META_io, &mp->flag)) {
334 if (mp->lsn)
335 remove_from_logsync(mp);
336 clear_bit(META_io, &mp->flag);
337 }
338 /*
339 * I'd like to call drop_metapage here, but I don't think it's
340 * safe unless I have the page locked
341 */
342 }
343 end_page_writeback(page);
344}
345
346static int metapage_write_end_io(struct bio *bio, unsigned int bytes_done,
347 int err)
348{
349 struct page *page = bio->bi_private;
350
351 BUG_ON(!PagePrivate(page));
352
353 if (bio->bi_size)
354 return 1;
355
356 if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
357 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
358 SetPageError(page);
359 }
360 dec_io(page, last_write_complete);
361 bio_put(bio);
362 return 0;
363}
364
365static int metapage_writepage(struct page *page, struct writeback_control *wbc)
366{
367 struct bio *bio = NULL;
368 unsigned int block_offset; /* block offset of mp within page */
369 struct inode *inode = page->mapping->host;
370 unsigned int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
371 unsigned int len;
372 unsigned int xlen;
373 struct metapage *mp;
374 int redirty = 0;
375 sector_t lblock;
376 sector_t pblock;
377 sector_t next_block = 0;
378 sector_t page_start;
379 unsigned long bio_bytes = 0;
380 unsigned long bio_offset = 0;
381 unsigned int offset;
382
383 page_start = (sector_t)page->index <<
384 (PAGE_CACHE_SHIFT - inode->i_blkbits);
385 BUG_ON(!PageLocked(page));
386 BUG_ON(PageWriteback(page));
387
388 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
389 mp = page_to_mp(page, offset);
390
391 if (!mp || !test_bit(META_dirty, &mp->flag))
392 continue;
393
394 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
395 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500396 /*
397 * Make sure this page isn't blocked indefinitely.
398 * If the journal isn't undergoing I/O, push it
399 */
400 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
401 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600402 continue;
403 }
404
405 clear_bit(META_dirty, &mp->flag);
406 block_offset = offset >> inode->i_blkbits;
407 lblock = page_start + block_offset;
408 if (bio) {
409 if (xlen && lblock == next_block) {
410 /* Contiguous, in memory & on disk */
411 len = min(xlen, blocks_per_mp);
412 xlen -= len;
413 bio_bytes += len << inode->i_blkbits;
414 set_bit(META_io, &mp->flag);
415 continue;
416 }
417 /* Not contiguous */
418 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
419 bio_bytes)
420 goto add_failed;
421 /*
422 * Increment counter before submitting i/o to keep
423 * count from hitting zero before we're through
424 */
425 inc_io(page);
426 if (!bio->bi_size)
427 goto dump_bio;
428 submit_bio(WRITE, bio);
429 bio = NULL;
430 } else {
431 set_page_writeback(page);
432 inc_io(page);
433 }
434 xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
435 pblock = metapage_get_blocks(inode, lblock, &xlen);
436 if (!pblock) {
437 /* Need better error handling */
438 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
439 dec_io(page, last_write_complete);
440 continue;
441 }
442 set_bit(META_io, &mp->flag);
443 len = min(xlen, (uint) JFS_SBI(inode->i_sb)->nbperpage);
444
445 bio = bio_alloc(GFP_NOFS, 1);
446 bio->bi_bdev = inode->i_sb->s_bdev;
447 bio->bi_sector = pblock << (inode->i_blkbits - 9);
448 bio->bi_end_io = metapage_write_end_io;
449 bio->bi_private = page;
450
451 /* Don't call bio_add_page yet, we may add to this vec */
452 bio_offset = offset;
453 bio_bytes = len << inode->i_blkbits;
454
455 xlen -= len;
456 next_block = lblock + len;
457 }
458 if (bio) {
459 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
460 goto add_failed;
461 if (!bio->bi_size)
462 goto dump_bio;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500463
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600464 submit_bio(WRITE, bio);
465 }
466 if (redirty)
467 redirty_page_for_writepage(wbc, page);
468
469 unlock_page(page);
470
471 return 0;
472add_failed:
473 /* We should never reach here, since we're only adding one vec */
474 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
475 goto skip;
476dump_bio:
477 dump_mem("bio", bio, sizeof(*bio));
478skip:
479 bio_put(bio);
480 unlock_page(page);
481 dec_io(page, last_write_complete);
482
483 return -EIO;
484}
485
486static int metapage_readpage(struct file *fp, struct page *page)
487{
488 struct inode *inode = page->mapping->host;
489 struct bio *bio = NULL;
490 unsigned int block_offset;
491 unsigned int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
492 sector_t page_start; /* address of page in fs blocks */
493 sector_t pblock;
494 unsigned int xlen;
495 unsigned int len;
496 unsigned int offset;
497
498 BUG_ON(!PageLocked(page));
499 page_start = (sector_t)page->index <<
500 (PAGE_CACHE_SHIFT - inode->i_blkbits);
501
502 block_offset = 0;
503 while (block_offset < blocks_per_page) {
504 xlen = blocks_per_page - block_offset;
505 pblock = metapage_get_blocks(inode, page_start + block_offset,
506 &xlen);
507 if (pblock) {
508 if (!PagePrivate(page))
509 insert_metapage(page, NULL);
510 inc_io(page);
511 if (bio)
512 submit_bio(READ, bio);
513
514 bio = bio_alloc(GFP_NOFS, 1);
515 bio->bi_bdev = inode->i_sb->s_bdev;
516 bio->bi_sector = pblock << (inode->i_blkbits - 9);
517 bio->bi_end_io = metapage_read_end_io;
518 bio->bi_private = page;
519 len = xlen << inode->i_blkbits;
520 offset = block_offset << inode->i_blkbits;
521 if (bio_add_page(bio, page, len, offset) < len)
522 goto add_failed;
523 block_offset += xlen;
524 } else
525 block_offset++;
526 }
527 if (bio)
528 submit_bio(READ, bio);
529 else
530 unlock_page(page);
531
532 return 0;
533
534add_failed:
535 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
536 bio_put(bio);
537 dec_io(page, last_read_complete);
538 return -EIO;
539}
540
Al Viro27496a82005-10-21 03:20:48 -0400541static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600542{
543 struct metapage *mp;
Dave Kleikampb9646382006-05-24 07:43:38 -0500544 int ret = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600545 unsigned int offset;
546
547 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
548 mp = page_to_mp(page, offset);
549
550 if (!mp)
551 continue;
552
553 jfs_info("metapage_releasepage: mp = 0x%p", mp);
Dave Kleikampb9646382006-05-24 07:43:38 -0500554 if (mp->count || mp->nohomeok ||
555 test_bit(META_dirty, &mp->flag)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600556 jfs_info("count = %ld, nohomeok = %d", mp->count,
557 mp->nohomeok);
Dave Kleikampb9646382006-05-24 07:43:38 -0500558 ret = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600559 continue;
560 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600561 if (mp->lsn)
562 remove_from_logsync(mp);
563 remove_metapage(page, mp);
564 INCREMENT(mpStat.pagefree);
565 free_metapage(mp);
566 }
Dave Kleikampb9646382006-05-24 07:43:38 -0500567 return ret;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600568}
569
NeilBrown2ff28e22006-03-26 01:37:18 -0800570static void metapage_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600571{
572 BUG_ON(offset);
573
NeilBrown2ff28e22006-03-26 01:37:18 -0800574 BUG_ON(PageWriteback(page));
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600575
NeilBrown2ff28e22006-03-26 01:37:18 -0800576 metapage_releasepage(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600577}
578
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700579const struct address_space_operations jfs_metapage_aops = {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600580 .readpage = metapage_readpage,
581 .writepage = metapage_writepage,
582 .sync_page = block_sync_page,
583 .releasepage = metapage_releasepage,
584 .invalidatepage = metapage_invalidatepage,
585 .set_page_dirty = __set_page_dirty_nobuffers,
586};
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
589 unsigned int size, int absolute,
590 unsigned long new)
591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int l2BlocksPerPage;
593 int l2bsize;
594 struct address_space *mapping;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600595 struct metapage *mp = NULL;
596 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned long page_index;
598 unsigned long page_offset;
599
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600600 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
601 inode->i_ino, lblock, absolute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600603 l2bsize = inode->i_blkbits;
604 l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
605 page_index = lblock >> l2BlocksPerPage;
606 page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
607 if ((page_offset + size) > PAGE_CACHE_SIZE) {
608 jfs_err("MetaData crosses page boundary!!");
609 jfs_err("lblock = %lx, size = %d", lblock, size);
610 dump_stack();
611 return NULL;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (absolute)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600614 mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 else {
616 /*
617 * If an nfs client tries to read an inode that is larger
618 * than any existing inodes, we may try to read past the
619 * end of the inode map
620 */
621 if ((lblock << inode->i_blkbits) >= inode->i_size)
622 return NULL;
623 mapping = inode->i_mapping;
624 }
625
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600626 if (new && (PSIZE == PAGE_CACHE_SIZE)) {
627 page = grab_cache_page(mapping, page_index);
628 if (!page) {
629 jfs_err("grab_cache_page failed!");
630 return NULL;
631 }
632 SetPageUptodate(page);
633 } else {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700634 page = read_mapping_page(mapping, page_index, NULL);
Dave Kleikampc40c20242005-07-22 11:08:44 -0500635 if (IS_ERR(page) || !PageUptodate(page)) {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700636 jfs_err("read_mapping_page failed!");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600637 return NULL;
638 }
639 lock_page(page);
640 }
641
642 mp = page_to_mp(page, page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (mp) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600644 if (mp->logical_size != size) {
645 jfs_error(inode->i_sb,
646 "__get_metapage: mp->logical_size != size");
647 jfs_err("logical_size = %d, size = %d",
648 mp->logical_size, size);
649 dump_stack();
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500650 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 mp->count++;
653 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (test_bit(META_discard, &mp->flag)) {
655 if (!new) {
656 jfs_error(inode->i_sb,
657 "__get_metapage: using a "
658 "discarded metapage");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600659 discard_metapage(mp);
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500660 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 clear_bit(META_discard, &mp->flag);
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600665 INCREMENT(mpStat.pagealloc);
666 mp = alloc_metapage(GFP_NOFS);
667 mp->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 mp->xflag = COMMIT_PAGE;
670 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600671 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600673 mp->data = page_address(page) + page_offset;
674 mp->index = lblock;
675 if (unlikely(insert_metapage(page, mp))) {
676 free_metapage(mp);
677 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600679 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600682 if (new) {
683 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600687 unlock_page(page);
688 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return mp;
690
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600691unlock:
692 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return NULL;
694}
695
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600696void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600698 jfs_info("grab_metapage: mp = 0x%p", mp);
699 page_cache_get(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600701 mp->count++;
702 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600706void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600709 jfs_info("force_metapage: mp = 0x%p", mp);
710 set_bit(META_forcewrite, &mp->flag);
711 clear_bit(META_sync, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 page_cache_get(page);
713 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600714 set_page_dirty(page);
715 write_one_page(page, 1);
716 clear_bit(META_forcewrite, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 page_cache_release(page);
718}
719
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500720void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600721{
722 lock_page(mp->page);
723}
724
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500725void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600726{
727 if (mp->count || mp->nohomeok) {
728 /* Someone else will release this */
729 unlock_page(mp->page);
730 return;
731 }
732 page_cache_get(mp->page);
733 mp->count++;
734 lock_metapage(mp);
735 unlock_page(mp->page);
736 release_metapage(mp);
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739void release_metapage(struct metapage * mp)
740{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600741 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
743
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600744 BUG_ON(!page);
745
746 lock_page(page);
747 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600750 if (--mp->count || mp->nohomeok) {
751 unlock_page(page);
752 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
754 }
755
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600756 if (test_bit(META_dirty, &mp->flag)) {
757 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 clear_bit(META_sync, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600760 write_one_page(page, 1);
761 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600763 } else if (mp->lsn) /* discard_metapage doesn't remove it */
764 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600766 /* Try to keep metapages from using up too much memory */
767 drop_metapage(page, mp);
Dave Kleikampd0e671a2006-12-20 15:55:35 -0600768
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600769 unlock_page(page);
770 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773void __invalidate_metapages(struct inode *ip, s64 addr, int len)
774{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600775 sector_t lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600777 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600779 struct address_space *mapping =
780 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct metapage *mp;
782 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600783 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600786 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * released, but should not be written.
788 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600789 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
790 lblock += BlocksPerPage) {
791 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
792 if (!page)
793 continue;
794 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
795 mp = page_to_mp(page, offset);
796 if (!mp)
797 continue;
798 if (mp->index < addr)
799 continue;
800 if (mp->index >= addr + len)
801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 clear_bit(META_dirty, &mp->flag);
804 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600805 if (mp->lsn)
806 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600808 unlock_page(page);
809 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811}
812
813#ifdef CONFIG_JFS_STATISTICS
814int jfs_mpstat_read(char *buffer, char **start, off_t offset, int length,
815 int *eof, void *data)
816{
817 int len = 0;
818 off_t begin;
819
820 len += sprintf(buffer,
821 "JFS Metapage statistics\n"
822 "=======================\n"
823 "page allocations = %d\n"
824 "page frees = %d\n"
825 "lock waits = %d\n",
826 mpStat.pagealloc,
827 mpStat.pagefree,
828 mpStat.lockwait);
829
830 begin = offset;
831 *start = buffer + begin;
832 len -= begin;
833
834 if (len > length)
835 len = length;
836 else
837 *eof = 1;
838
839 if (len < 0)
840 len = 0;
841
842 return len;
843}
844#endif