blob: c350057087dd39db3809992338432ac07120bd16 [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>
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -050022#include <linux/module.h>
Dave Kleikamp7fab4792005-05-02 12:25:02 -060023#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/buffer_head.h>
26#include <linux/mempool.h>
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -050027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "jfs_incore.h"
29#include "jfs_superblock.h"
30#include "jfs_filsys.h"
31#include "jfs_metapage.h"
32#include "jfs_txnmgr.h"
33#include "jfs_debug.h"
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#ifdef CONFIG_JFS_STATISTICS
36static struct {
37 uint pagealloc; /* # of page allocations */
38 uint pagefree; /* # of page frees */
39 uint lockwait; /* # of sleeping lock_metapage() calls */
40} mpStat;
41#endif
42
Dave Kleikamp7fab4792005-05-02 12:25:02 -060043#define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
Nick Piggin54af6232007-11-26 14:58:10 -060044#define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static inline void unlock_metapage(struct metapage *mp)
47{
Nick Piggin54af6232007-11-26 14:58:10 -060048 clear_bit_unlock(META_locked, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 wake_up(&mp->wait);
50}
51
Dave Kleikamp7fab4792005-05-02 12:25:02 -060052static inline void __lock_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 INCREMENT(mpStat.lockwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 add_wait_queue_exclusive(&mp->wait, &wait);
57 do {
58 set_current_state(TASK_UNINTERRUPTIBLE);
59 if (metapage_locked(mp)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -060060 unlock_page(mp->page);
Dave Kleikamp4aa0d232007-01-17 21:18:35 -060061 io_schedule();
Dave Kleikamp7fab4792005-05-02 12:25:02 -060062 lock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64 } while (trylock_metapage(mp));
65 __set_current_state(TASK_RUNNING);
66 remove_wait_queue(&mp->wait, &wait);
67}
68
Dave Kleikamp7fab4792005-05-02 12:25:02 -060069/*
70 * Must have mp->page locked
71 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static inline void lock_metapage(struct metapage *mp)
73{
74 if (trylock_metapage(mp))
75 __lock_metapage(mp);
76}
77
78#define METAPOOL_MIN_PAGES 32
Christoph Lametere18b8902006-12-06 20:33:20 -080079static struct kmem_cache *metapage_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static mempool_t *metapage_mempool;
81
Dave Kleikamp7fab4792005-05-02 12:25:02 -060082#define MPS_PER_PAGE (PAGE_CACHE_SIZE >> L2PSIZE)
83
84#if MPS_PER_PAGE > 1
85
86struct meta_anchor {
87 int mp_count;
88 atomic_t io_count;
89 struct metapage *mp[MPS_PER_PAGE];
90};
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070091#define mp_anchor(page) ((struct meta_anchor *)page_private(page))
Dave Kleikamp7fab4792005-05-02 12:25:02 -060092
Dave Kleikamp967c9ec2008-01-10 16:04:25 -060093static inline struct metapage *page_to_mp(struct page *page, int offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -060094{
95 if (!PagePrivate(page))
96 return NULL;
97 return mp_anchor(page)->mp[offset >> L2PSIZE];
98}
99
100static inline int insert_metapage(struct page *page, struct metapage *mp)
101{
102 struct meta_anchor *a;
103 int index;
104 int l2mp_blocks; /* log2 blocks per metapage */
105
106 if (PagePrivate(page))
107 a = mp_anchor(page);
108 else {
Eric Sesterhenn5b3030e2006-02-23 09:47:13 -0600109 a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600110 if (!a)
111 return -ENOMEM;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700112 set_page_private(page, (unsigned long)a);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600113 SetPagePrivate(page);
114 kmap(page);
115 }
116
117 if (mp) {
118 l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
119 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
120 a->mp_count++;
121 a->mp[index] = mp;
122 }
123
124 return 0;
125}
126
127static inline void remove_metapage(struct page *page, struct metapage *mp)
128{
129 struct meta_anchor *a = mp_anchor(page);
130 int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
131 int index;
132
133 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
134
135 BUG_ON(a->mp[index] != mp);
136
137 a->mp[index] = NULL;
138 if (--a->mp_count == 0) {
139 kfree(a);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700140 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600141 ClearPagePrivate(page);
142 kunmap(page);
143 }
144}
145
146static inline void inc_io(struct page *page)
147{
148 atomic_inc(&mp_anchor(page)->io_count);
149}
150
151static inline void dec_io(struct page *page, void (*handler) (struct page *))
152{
153 if (atomic_dec_and_test(&mp_anchor(page)->io_count))
154 handler(page);
155}
156
157#else
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600158static inline struct metapage *page_to_mp(struct page *page, int offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600159{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700160 return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600161}
162
163static inline int insert_metapage(struct page *page, struct metapage *mp)
164{
165 if (mp) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700166 set_page_private(page, (unsigned long)mp);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600167 SetPagePrivate(page);
168 kmap(page);
169 }
170 return 0;
171}
172
173static inline void remove_metapage(struct page *page, struct metapage *mp)
174{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700175 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600176 ClearPagePrivate(page);
177 kunmap(page);
178}
179
180#define inc_io(page) do {} while(0)
181#define dec_io(page, handler) handler(page)
182
183#endif
184
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700185static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct metapage *mp = (struct metapage *)foo;
188
Christoph Lametera35afb82007-05-16 22:10:57 -0700189 mp->lid = 0;
190 mp->lsn = 0;
191 mp->flag = 0;
192 mp->data = NULL;
193 mp->clsn = 0;
194 mp->log = NULL;
195 set_bit(META_free, &mp->flag);
196 init_waitqueue_head(&mp->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
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),
Paul Mundt20c2df82007-07-20 10:11:58 +0900218 0, 0, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 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,
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600254 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
NeilBrown6712ecf2007-09-27 12:47:43 +0200285static void metapage_read_end_io(struct bio *bio, int err)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600286{
287 struct page *page = bio->bi_private;
288
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600289 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
290 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
291 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600294 dec_io(page, last_read_complete);
295 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600298static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600300 struct jfs_log *log = mp->log;
301 unsigned long flags;
302/*
303 * This can race. Recheck that log hasn't been set to null, and after
304 * acquiring logsync lock, recheck lsn
305 */
306 if (!log)
307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600309 LOGSYNC_LOCK(log, flags);
310 if (mp->lsn) {
311 mp->log = NULL;
312 mp->lsn = 0;
313 mp->clsn = 0;
314 log->count--;
315 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600317 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600320static void last_write_complete(struct page *page)
321{
322 struct metapage *mp;
323 unsigned int offset;
324
325 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
326 mp = page_to_mp(page, offset);
327 if (mp && test_bit(META_io, &mp->flag)) {
328 if (mp->lsn)
329 remove_from_logsync(mp);
330 clear_bit(META_io, &mp->flag);
331 }
332 /*
333 * I'd like to call drop_metapage here, but I don't think it's
334 * safe unless I have the page locked
335 */
336 }
337 end_page_writeback(page);
338}
339
NeilBrown6712ecf2007-09-27 12:47:43 +0200340static void metapage_write_end_io(struct bio *bio, int err)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600341{
342 struct page *page = bio->bi_private;
343
344 BUG_ON(!PagePrivate(page));
345
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600346 if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
347 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
348 SetPageError(page);
349 }
350 dec_io(page, last_write_complete);
351 bio_put(bio);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600352}
353
354static int metapage_writepage(struct page *page, struct writeback_control *wbc)
355{
356 struct bio *bio = NULL;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600357 int block_offset; /* block offset of mp within page */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600358 struct inode *inode = page->mapping->host;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600359 int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
360 int len;
361 int xlen;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600362 struct metapage *mp;
363 int redirty = 0;
364 sector_t lblock;
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600365 int nr_underway = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600366 sector_t pblock;
367 sector_t next_block = 0;
368 sector_t page_start;
369 unsigned long bio_bytes = 0;
370 unsigned long bio_offset = 0;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600371 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600372
373 page_start = (sector_t)page->index <<
374 (PAGE_CACHE_SHIFT - inode->i_blkbits);
375 BUG_ON(!PageLocked(page));
376 BUG_ON(PageWriteback(page));
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600377 set_page_writeback(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600378
379 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
380 mp = page_to_mp(page, offset);
381
382 if (!mp || !test_bit(META_dirty, &mp->flag))
383 continue;
384
385 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
386 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500387 /*
388 * Make sure this page isn't blocked indefinitely.
389 * If the journal isn't undergoing I/O, push it
390 */
391 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
392 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600393 continue;
394 }
395
396 clear_bit(META_dirty, &mp->flag);
397 block_offset = offset >> inode->i_blkbits;
398 lblock = page_start + block_offset;
399 if (bio) {
400 if (xlen && lblock == next_block) {
401 /* Contiguous, in memory & on disk */
402 len = min(xlen, blocks_per_mp);
403 xlen -= len;
404 bio_bytes += len << inode->i_blkbits;
405 set_bit(META_io, &mp->flag);
406 continue;
407 }
408 /* Not contiguous */
409 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
410 bio_bytes)
411 goto add_failed;
412 /*
413 * Increment counter before submitting i/o to keep
414 * count from hitting zero before we're through
415 */
416 inc_io(page);
417 if (!bio->bi_size)
418 goto dump_bio;
419 submit_bio(WRITE, bio);
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600420 nr_underway++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600421 bio = NULL;
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600422 } else
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600423 inc_io(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600424 xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
425 pblock = metapage_get_blocks(inode, lblock, &xlen);
426 if (!pblock) {
427 /* Need better error handling */
428 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
429 dec_io(page, last_write_complete);
430 continue;
431 }
432 set_bit(META_io, &mp->flag);
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600433 len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600434
435 bio = bio_alloc(GFP_NOFS, 1);
436 bio->bi_bdev = inode->i_sb->s_bdev;
437 bio->bi_sector = pblock << (inode->i_blkbits - 9);
438 bio->bi_end_io = metapage_write_end_io;
439 bio->bi_private = page;
440
441 /* Don't call bio_add_page yet, we may add to this vec */
442 bio_offset = offset;
443 bio_bytes = len << inode->i_blkbits;
444
445 xlen -= len;
446 next_block = lblock + len;
447 }
448 if (bio) {
449 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
450 goto add_failed;
451 if (!bio->bi_size)
452 goto dump_bio;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500453
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600454 submit_bio(WRITE, bio);
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600455 nr_underway++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600456 }
457 if (redirty)
458 redirty_page_for_writepage(wbc, page);
459
460 unlock_page(page);
461
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600462 if (nr_underway == 0)
463 end_page_writeback(page);
464
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600465 return 0;
466add_failed:
467 /* We should never reach here, since we're only adding one vec */
468 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
469 goto skip;
470dump_bio:
Dave Kleikamp288e4d82007-06-13 10:17:50 -0500471 print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16,
472 4, bio, sizeof(*bio), 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600473skip:
474 bio_put(bio);
475 unlock_page(page);
476 dec_io(page, last_write_complete);
477
478 return -EIO;
479}
480
481static int metapage_readpage(struct file *fp, struct page *page)
482{
483 struct inode *inode = page->mapping->host;
484 struct bio *bio = NULL;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600485 int block_offset;
486 int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600487 sector_t page_start; /* address of page in fs blocks */
488 sector_t pblock;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600489 int xlen;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600490 unsigned int len;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600491 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600492
493 BUG_ON(!PageLocked(page));
494 page_start = (sector_t)page->index <<
495 (PAGE_CACHE_SHIFT - inode->i_blkbits);
496
497 block_offset = 0;
498 while (block_offset < blocks_per_page) {
499 xlen = blocks_per_page - block_offset;
500 pblock = metapage_get_blocks(inode, page_start + block_offset,
501 &xlen);
502 if (pblock) {
503 if (!PagePrivate(page))
504 insert_metapage(page, NULL);
505 inc_io(page);
506 if (bio)
507 submit_bio(READ, bio);
508
509 bio = bio_alloc(GFP_NOFS, 1);
510 bio->bi_bdev = inode->i_sb->s_bdev;
511 bio->bi_sector = pblock << (inode->i_blkbits - 9);
512 bio->bi_end_io = metapage_read_end_io;
513 bio->bi_private = page;
514 len = xlen << inode->i_blkbits;
515 offset = block_offset << inode->i_blkbits;
516 if (bio_add_page(bio, page, len, offset) < len)
517 goto add_failed;
518 block_offset += xlen;
519 } else
520 block_offset++;
521 }
522 if (bio)
523 submit_bio(READ, bio);
524 else
525 unlock_page(page);
526
527 return 0;
528
529add_failed:
530 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
531 bio_put(bio);
532 dec_io(page, last_read_complete);
533 return -EIO;
534}
535
Al Viro27496a82005-10-21 03:20:48 -0400536static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600537{
538 struct metapage *mp;
Dave Kleikampb9646382006-05-24 07:43:38 -0500539 int ret = 1;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600540 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600541
542 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
543 mp = page_to_mp(page, offset);
544
545 if (!mp)
546 continue;
547
548 jfs_info("metapage_releasepage: mp = 0x%p", mp);
Dave Kleikampb9646382006-05-24 07:43:38 -0500549 if (mp->count || mp->nohomeok ||
550 test_bit(META_dirty, &mp->flag)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600551 jfs_info("count = %ld, nohomeok = %d", mp->count,
552 mp->nohomeok);
Dave Kleikampb9646382006-05-24 07:43:38 -0500553 ret = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600554 continue;
555 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600556 if (mp->lsn)
557 remove_from_logsync(mp);
558 remove_metapage(page, mp);
559 INCREMENT(mpStat.pagefree);
560 free_metapage(mp);
561 }
Dave Kleikampb9646382006-05-24 07:43:38 -0500562 return ret;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600563}
564
NeilBrown2ff28e22006-03-26 01:37:18 -0800565static void metapage_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600566{
567 BUG_ON(offset);
568
NeilBrown2ff28e22006-03-26 01:37:18 -0800569 BUG_ON(PageWriteback(page));
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600570
NeilBrown2ff28e22006-03-26 01:37:18 -0800571 metapage_releasepage(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600572}
573
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700574const struct address_space_operations jfs_metapage_aops = {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600575 .readpage = metapage_readpage,
576 .writepage = metapage_writepage,
577 .sync_page = block_sync_page,
578 .releasepage = metapage_releasepage,
579 .invalidatepage = metapage_invalidatepage,
580 .set_page_dirty = __set_page_dirty_nobuffers,
581};
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
584 unsigned int size, int absolute,
585 unsigned long new)
586{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int l2BlocksPerPage;
588 int l2bsize;
589 struct address_space *mapping;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600590 struct metapage *mp = NULL;
591 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 unsigned long page_index;
593 unsigned long page_offset;
594
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600595 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
596 inode->i_ino, lblock, absolute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600598 l2bsize = inode->i_blkbits;
599 l2BlocksPerPage = PAGE_CACHE_SHIFT - l2bsize;
600 page_index = lblock >> l2BlocksPerPage;
601 page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
602 if ((page_offset + size) > PAGE_CACHE_SIZE) {
603 jfs_err("MetaData crosses page boundary!!");
604 jfs_err("lblock = %lx, size = %d", lblock, size);
605 dump_stack();
606 return NULL;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (absolute)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600609 mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else {
611 /*
612 * If an nfs client tries to read an inode that is larger
613 * than any existing inodes, we may try to read past the
614 * end of the inode map
615 */
616 if ((lblock << inode->i_blkbits) >= inode->i_size)
617 return NULL;
618 mapping = inode->i_mapping;
619 }
620
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600621 if (new && (PSIZE == PAGE_CACHE_SIZE)) {
622 page = grab_cache_page(mapping, page_index);
623 if (!page) {
624 jfs_err("grab_cache_page failed!");
625 return NULL;
626 }
627 SetPageUptodate(page);
628 } else {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700629 page = read_mapping_page(mapping, page_index, NULL);
Dave Kleikampc40c2022005-07-22 11:08:44 -0500630 if (IS_ERR(page) || !PageUptodate(page)) {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700631 jfs_err("read_mapping_page failed!");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600632 return NULL;
633 }
634 lock_page(page);
635 }
636
637 mp = page_to_mp(page, page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (mp) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600639 if (mp->logical_size != size) {
640 jfs_error(inode->i_sb,
641 "__get_metapage: mp->logical_size != size");
642 jfs_err("logical_size = %d, size = %d",
643 mp->logical_size, size);
644 dump_stack();
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500645 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 mp->count++;
648 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (test_bit(META_discard, &mp->flag)) {
650 if (!new) {
651 jfs_error(inode->i_sb,
652 "__get_metapage: using a "
653 "discarded metapage");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600654 discard_metapage(mp);
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500655 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 clear_bit(META_discard, &mp->flag);
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600660 INCREMENT(mpStat.pagealloc);
661 mp = alloc_metapage(GFP_NOFS);
662 mp->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 mp->xflag = COMMIT_PAGE;
665 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600666 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600668 mp->data = page_address(page) + page_offset;
669 mp->index = lblock;
670 if (unlikely(insert_metapage(page, mp))) {
671 free_metapage(mp);
672 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600674 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600677 if (new) {
678 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600682 unlock_page(page);
683 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return mp;
685
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600686unlock:
687 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return NULL;
689}
690
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600691void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600693 jfs_info("grab_metapage: mp = 0x%p", mp);
694 page_cache_get(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600696 mp->count++;
697 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600701void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600704 jfs_info("force_metapage: mp = 0x%p", mp);
705 set_bit(META_forcewrite, &mp->flag);
706 clear_bit(META_sync, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 page_cache_get(page);
708 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600709 set_page_dirty(page);
710 write_one_page(page, 1);
711 clear_bit(META_forcewrite, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 page_cache_release(page);
713}
714
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500715void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600716{
717 lock_page(mp->page);
718}
719
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500720void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600721{
722 if (mp->count || mp->nohomeok) {
723 /* Someone else will release this */
724 unlock_page(mp->page);
725 return;
726 }
727 page_cache_get(mp->page);
728 mp->count++;
729 lock_metapage(mp);
730 unlock_page(mp->page);
731 release_metapage(mp);
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734void release_metapage(struct metapage * mp)
735{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600736 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
738
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600739 BUG_ON(!page);
740
741 lock_page(page);
742 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600745 if (--mp->count || mp->nohomeok) {
746 unlock_page(page);
747 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return;
749 }
750
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600751 if (test_bit(META_dirty, &mp->flag)) {
752 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 clear_bit(META_sync, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600755 write_one_page(page, 1);
756 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600758 } else if (mp->lsn) /* discard_metapage doesn't remove it */
759 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600761 /* Try to keep metapages from using up too much memory */
762 drop_metapage(page, mp);
Dave Kleikampd0e671a2006-12-20 15:55:35 -0600763
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600764 unlock_page(page);
765 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768void __invalidate_metapages(struct inode *ip, s64 addr, int len)
769{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600770 sector_t lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600772 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600774 struct address_space *mapping =
775 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct metapage *mp;
777 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600778 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600781 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * released, but should not be written.
783 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600784 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
785 lblock += BlocksPerPage) {
786 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
787 if (!page)
788 continue;
789 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
790 mp = page_to_mp(page, offset);
791 if (!mp)
792 continue;
793 if (mp->index < addr)
794 continue;
795 if (mp->index >= addr + len)
796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 clear_bit(META_dirty, &mp->flag);
799 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600800 if (mp->lsn)
801 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600803 unlock_page(page);
804 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806}
807
808#ifdef CONFIG_JFS_STATISTICS
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500809static int jfs_mpstat_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500811 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 "JFS Metapage statistics\n"
813 "=======================\n"
814 "page allocations = %d\n"
815 "page frees = %d\n"
816 "lock waits = %d\n",
817 mpStat.pagealloc,
818 mpStat.pagefree,
819 mpStat.lockwait);
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500822
823static int jfs_mpstat_proc_open(struct inode *inode, struct file *file)
824{
825 return single_open(file, jfs_mpstat_proc_show, NULL);
826}
827
828const struct file_operations jfs_mpstat_proc_fops = {
829 .owner = THIS_MODULE,
830 .open = jfs_mpstat_proc_open,
831 .read = seq_read,
832 .llseek = seq_lseek,
833 .release = single_release,
834};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835#endif