blob: 16a0922beb59cf8c4157e404f9ffd376f1a1c621 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/buffer_head.h>
27#include <linux/mempool.h>
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -050028#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "jfs_incore.h"
30#include "jfs_superblock.h"
31#include "jfs_filsys.h"
32#include "jfs_metapage.h"
33#include "jfs_txnmgr.h"
34#include "jfs_debug.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#ifdef CONFIG_JFS_STATISTICS
37static struct {
38 uint pagealloc; /* # of page allocations */
39 uint pagefree; /* # of page frees */
40 uint lockwait; /* # of sleeping lock_metapage() calls */
41} mpStat;
42#endif
43
Dave Kleikamp7fab4792005-05-02 12:25:02 -060044#define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
Nick Piggin54af6232007-11-26 14:58:10 -060045#define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static inline void unlock_metapage(struct metapage *mp)
48{
Nick Piggin54af6232007-11-26 14:58:10 -060049 clear_bit_unlock(META_locked, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 wake_up(&mp->wait);
51}
52
Dave Kleikamp7fab4792005-05-02 12:25:02 -060053static inline void __lock_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 INCREMENT(mpStat.lockwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 add_wait_queue_exclusive(&mp->wait, &wait);
58 do {
59 set_current_state(TASK_UNINTERRUPTIBLE);
60 if (metapage_locked(mp)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -060061 unlock_page(mp->page);
Dave Kleikamp4aa0d232007-01-17 21:18:35 -060062 io_schedule();
Dave Kleikamp7fab4792005-05-02 12:25:02 -060063 lock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65 } while (trylock_metapage(mp));
66 __set_current_state(TASK_RUNNING);
67 remove_wait_queue(&mp->wait, &wait);
68}
69
Dave Kleikamp7fab4792005-05-02 12:25:02 -060070/*
71 * Must have mp->page locked
72 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static inline void lock_metapage(struct metapage *mp)
74{
75 if (trylock_metapage(mp))
76 __lock_metapage(mp);
77}
78
79#define METAPOOL_MIN_PAGES 32
Christoph Lametere18b8902006-12-06 20:33:20 -080080static struct kmem_cache *metapage_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static mempool_t *metapage_mempool;
82
Dave Kleikamp7fab4792005-05-02 12:25:02 -060083#define MPS_PER_PAGE (PAGE_CACHE_SIZE >> L2PSIZE)
84
85#if MPS_PER_PAGE > 1
86
87struct meta_anchor {
88 int mp_count;
89 atomic_t io_count;
90 struct metapage *mp[MPS_PER_PAGE];
91};
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070092#define mp_anchor(page) ((struct meta_anchor *)page_private(page))
Dave Kleikamp7fab4792005-05-02 12:25:02 -060093
Dave Kleikamp967c9ec2008-01-10 16:04:25 -060094static inline struct metapage *page_to_mp(struct page *page, int offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -060095{
96 if (!PagePrivate(page))
97 return NULL;
98 return mp_anchor(page)->mp[offset >> L2PSIZE];
99}
100
101static inline int insert_metapage(struct page *page, struct metapage *mp)
102{
103 struct meta_anchor *a;
104 int index;
105 int l2mp_blocks; /* log2 blocks per metapage */
106
107 if (PagePrivate(page))
108 a = mp_anchor(page);
109 else {
Eric Sesterhenn5b3030e2006-02-23 09:47:13 -0600110 a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600111 if (!a)
112 return -ENOMEM;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700113 set_page_private(page, (unsigned long)a);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600114 SetPagePrivate(page);
115 kmap(page);
116 }
117
118 if (mp) {
119 l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
120 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
121 a->mp_count++;
122 a->mp[index] = mp;
123 }
124
125 return 0;
126}
127
128static inline void remove_metapage(struct page *page, struct metapage *mp)
129{
130 struct meta_anchor *a = mp_anchor(page);
131 int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
132 int index;
133
134 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
135
136 BUG_ON(a->mp[index] != mp);
137
138 a->mp[index] = NULL;
139 if (--a->mp_count == 0) {
140 kfree(a);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700141 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600142 ClearPagePrivate(page);
143 kunmap(page);
144 }
145}
146
147static inline void inc_io(struct page *page)
148{
149 atomic_inc(&mp_anchor(page)->io_count);
150}
151
152static inline void dec_io(struct page *page, void (*handler) (struct page *))
153{
154 if (atomic_dec_and_test(&mp_anchor(page)->io_count))
155 handler(page);
156}
157
158#else
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600159static inline struct metapage *page_to_mp(struct page *page, int offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600160{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700161 return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600162}
163
164static inline int insert_metapage(struct page *page, struct metapage *mp)
165{
166 if (mp) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700167 set_page_private(page, (unsigned long)mp);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600168 SetPagePrivate(page);
169 kmap(page);
170 }
171 return 0;
172}
173
174static inline void remove_metapage(struct page *page, struct metapage *mp)
175{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700176 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600177 ClearPagePrivate(page);
178 kunmap(page);
179}
180
181#define inc_io(page) do {} while(0)
182#define dec_io(page, handler) handler(page)
183
184#endif
185
Al Viro27496a82005-10-21 03:20:48 -0400186static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
David Rientjesee146242015-04-15 16:14:11 -0700188 struct metapage *mp = mempool_alloc(metapage_mempool, gfp_mask);
189
190 if (mp) {
191 mp->lid = 0;
192 mp->lsn = 0;
193 mp->data = NULL;
194 mp->clsn = 0;
195 mp->log = NULL;
196 init_waitqueue_head(&mp->wait);
197 }
198 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static inline void free_metapage(struct metapage *mp)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 mempool_free(mp, metapage_mempool);
204}
205
206int __init metapage_init(void)
207{
208 /*
209 * Allocate the metapage structures
210 */
211 metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
David Rientjesee146242015-04-15 16:14:11 -0700212 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (metapage_cache == NULL)
214 return -ENOMEM;
215
Matthew Dobson93d23412006-03-26 01:37:50 -0800216 metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES,
217 metapage_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (metapage_mempool == NULL) {
220 kmem_cache_destroy(metapage_cache);
221 return -ENOMEM;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 return 0;
225}
226
227void metapage_exit(void)
228{
229 mempool_destroy(metapage_mempool);
230 kmem_cache_destroy(metapage_cache);
231}
232
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600233static inline void drop_metapage(struct page *page, struct metapage *mp)
234{
235 if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
236 test_bit(META_io, &mp->flag))
237 return;
238 remove_metapage(page, mp);
239 INCREMENT(mpStat.pagefree);
240 free_metapage(mp);
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600244 * Metapage address space operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600246
247static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600248 int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600250 int rc = 0;
251 int xflag;
252 s64 xaddr;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700253 sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600254 inode->i_blkbits;
255
256 if (lblock >= file_blocks)
257 return 0;
258 if (lblock + *len > file_blocks)
259 *len = file_blocks - lblock;
260
261 if (inode->i_ino) {
262 rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
263 if ((rc == 0) && *len)
264 lblock = (sector_t)xaddr;
265 else
266 lblock = 0;
267 } /* else no mapping */
268
269 return lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600272static void last_read_complete(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600274 if (!PageError(page))
275 SetPageUptodate(page);
276 unlock_page(page);
277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
NeilBrown6712ecf2007-09-27 12:47:43 +0200279static void metapage_read_end_io(struct bio *bio, int err)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600280{
281 struct page *page = bio->bi_private;
282
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600283 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
284 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
285 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600288 dec_io(page, last_read_complete);
289 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600292static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600294 struct jfs_log *log = mp->log;
295 unsigned long flags;
296/*
297 * This can race. Recheck that log hasn't been set to null, and after
298 * acquiring logsync lock, recheck lsn
299 */
300 if (!log)
301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600303 LOGSYNC_LOCK(log, flags);
304 if (mp->lsn) {
305 mp->log = NULL;
306 mp->lsn = 0;
307 mp->clsn = 0;
308 log->count--;
309 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600311 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600314static void last_write_complete(struct page *page)
315{
316 struct metapage *mp;
317 unsigned int offset;
318
319 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
320 mp = page_to_mp(page, offset);
321 if (mp && test_bit(META_io, &mp->flag)) {
322 if (mp->lsn)
323 remove_from_logsync(mp);
324 clear_bit(META_io, &mp->flag);
325 }
326 /*
327 * I'd like to call drop_metapage here, but I don't think it's
328 * safe unless I have the page locked
329 */
330 }
331 end_page_writeback(page);
332}
333
NeilBrown6712ecf2007-09-27 12:47:43 +0200334static void metapage_write_end_io(struct bio *bio, int err)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600335{
336 struct page *page = bio->bi_private;
337
338 BUG_ON(!PagePrivate(page));
339
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600340 if (! test_bit(BIO_UPTODATE, &bio->bi_flags)) {
341 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
342 SetPageError(page);
343 }
344 dec_io(page, last_write_complete);
345 bio_put(bio);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600346}
347
348static int metapage_writepage(struct page *page, struct writeback_control *wbc)
349{
350 struct bio *bio = NULL;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600351 int block_offset; /* block offset of mp within page */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600352 struct inode *inode = page->mapping->host;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600353 int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
354 int len;
355 int xlen;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600356 struct metapage *mp;
357 int redirty = 0;
358 sector_t lblock;
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600359 int nr_underway = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600360 sector_t pblock;
361 sector_t next_block = 0;
362 sector_t page_start;
363 unsigned long bio_bytes = 0;
364 unsigned long bio_offset = 0;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600365 int offset;
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600366 int bad_blocks = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600367
368 page_start = (sector_t)page->index <<
369 (PAGE_CACHE_SHIFT - inode->i_blkbits);
370 BUG_ON(!PageLocked(page));
371 BUG_ON(PageWriteback(page));
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600372 set_page_writeback(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600373
374 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
375 mp = page_to_mp(page, offset);
376
377 if (!mp || !test_bit(META_dirty, &mp->flag))
378 continue;
379
380 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
381 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500382 /*
383 * Make sure this page isn't blocked indefinitely.
384 * If the journal isn't undergoing I/O, push it
385 */
386 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
387 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600388 continue;
389 }
390
391 clear_bit(META_dirty, &mp->flag);
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600392 set_bit(META_io, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600393 block_offset = offset >> inode->i_blkbits;
394 lblock = page_start + block_offset;
395 if (bio) {
396 if (xlen && lblock == next_block) {
397 /* Contiguous, in memory & on disk */
398 len = min(xlen, blocks_per_mp);
399 xlen -= len;
400 bio_bytes += len << inode->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600401 continue;
402 }
403 /* Not contiguous */
404 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
405 bio_bytes)
406 goto add_failed;
407 /*
408 * Increment counter before submitting i/o to keep
409 * count from hitting zero before we're through
410 */
411 inc_io(page);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700412 if (!bio->bi_iter.bi_size)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600413 goto dump_bio;
414 submit_bio(WRITE, bio);
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600415 nr_underway++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600416 bio = NULL;
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600417 } else
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600418 inc_io(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600419 xlen = (PAGE_CACHE_SIZE - offset) >> inode->i_blkbits;
420 pblock = metapage_get_blocks(inode, lblock, &xlen);
421 if (!pblock) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600422 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600423 /*
424 * We already called inc_io(), but can't cancel it
425 * with dec_io() until we're done with the page
426 */
427 bad_blocks++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600428 continue;
429 }
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600430 len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600431
432 bio = bio_alloc(GFP_NOFS, 1);
433 bio->bi_bdev = inode->i_sb->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700434 bio->bi_iter.bi_sector = pblock << (inode->i_blkbits - 9);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600435 bio->bi_end_io = metapage_write_end_io;
436 bio->bi_private = page;
437
438 /* Don't call bio_add_page yet, we may add to this vec */
439 bio_offset = offset;
440 bio_bytes = len << inode->i_blkbits;
441
442 xlen -= len;
443 next_block = lblock + len;
444 }
445 if (bio) {
446 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
447 goto add_failed;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700448 if (!bio->bi_iter.bi_size)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600449 goto dump_bio;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500450
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600451 submit_bio(WRITE, bio);
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600452 nr_underway++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600453 }
454 if (redirty)
455 redirty_page_for_writepage(wbc, page);
456
457 unlock_page(page);
458
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600459 if (bad_blocks)
460 goto err_out;
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);
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600477err_out:
478 while (bad_blocks--)
479 dec_io(page, last_write_complete);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600480 return -EIO;
481}
482
483static int metapage_readpage(struct file *fp, struct page *page)
484{
485 struct inode *inode = page->mapping->host;
486 struct bio *bio = NULL;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600487 int block_offset;
488 int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600489 sector_t page_start; /* address of page in fs blocks */
490 sector_t pblock;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600491 int xlen;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600492 unsigned int len;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600493 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600494
495 BUG_ON(!PageLocked(page));
496 page_start = (sector_t)page->index <<
497 (PAGE_CACHE_SHIFT - inode->i_blkbits);
498
499 block_offset = 0;
500 while (block_offset < blocks_per_page) {
501 xlen = blocks_per_page - block_offset;
502 pblock = metapage_get_blocks(inode, page_start + block_offset,
503 &xlen);
504 if (pblock) {
505 if (!PagePrivate(page))
506 insert_metapage(page, NULL);
507 inc_io(page);
508 if (bio)
509 submit_bio(READ, bio);
510
511 bio = bio_alloc(GFP_NOFS, 1);
512 bio->bi_bdev = inode->i_sb->s_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700513 bio->bi_iter.bi_sector =
514 pblock << (inode->i_blkbits - 9);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600515 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 Kleikamp967c9ec2008-01-10 16:04:25 -0600543 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600544
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
Lukas Czernerd47992f2013-05-21 23:17:23 -0400568static void metapage_invalidatepage(struct page *page, unsigned int offset,
569 unsigned int length)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600570{
Lukas Czernerd47992f2013-05-21 23:17:23 -0400571 BUG_ON(offset || length < PAGE_CACHE_SIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600572
NeilBrown2ff28e22006-03-26 01:37:18 -0800573 BUG_ON(PageWriteback(page));
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600574
NeilBrown2ff28e22006-03-26 01:37:18 -0800575 metapage_releasepage(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600576}
577
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700578const struct address_space_operations jfs_metapage_aops = {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600579 .readpage = metapage_readpage,
580 .writepage = metapage_writepage,
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600581 .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,
Joe Percheseb8630d2013-06-04 16:39:15 -0700644 "get_mp->logical_size != size\n");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600645 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,
Joe Percheseb8630d2013-06-04 16:39:15 -0700655 "using a discarded metapage\n");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600656 discard_metapage(mp);
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500657 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 clear_bit(META_discard, &mp->flag);
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600662 INCREMENT(mpStat.pagealloc);
663 mp = alloc_metapage(GFP_NOFS);
664 mp->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 mp->xflag = COMMIT_PAGE;
667 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600668 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600670 mp->data = page_address(page) + page_offset;
671 mp->index = lblock;
672 if (unlikely(insert_metapage(page, mp))) {
673 free_metapage(mp);
674 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600676 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600679 if (new) {
680 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600684 unlock_page(page);
685 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return mp;
687
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600688unlock:
689 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return NULL;
691}
692
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600693void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600695 jfs_info("grab_metapage: mp = 0x%p", mp);
696 page_cache_get(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600698 mp->count++;
699 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600703void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600706 jfs_info("force_metapage: mp = 0x%p", mp);
707 set_bit(META_forcewrite, &mp->flag);
708 clear_bit(META_sync, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 page_cache_get(page);
710 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600711 set_page_dirty(page);
712 write_one_page(page, 1);
713 clear_bit(META_forcewrite, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 page_cache_release(page);
715}
716
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500717void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600718{
719 lock_page(mp->page);
720}
721
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500722void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600723{
724 if (mp->count || mp->nohomeok) {
725 /* Someone else will release this */
726 unlock_page(mp->page);
727 return;
728 }
729 page_cache_get(mp->page);
730 mp->count++;
731 lock_metapage(mp);
732 unlock_page(mp->page);
733 release_metapage(mp);
734}
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736void release_metapage(struct metapage * mp)
737{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600738 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
740
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600741 BUG_ON(!page);
742
743 lock_page(page);
744 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600747 if (--mp->count || mp->nohomeok) {
748 unlock_page(page);
749 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751 }
752
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600753 if (test_bit(META_dirty, &mp->flag)) {
754 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 clear_bit(META_sync, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600757 write_one_page(page, 1);
758 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600760 } else if (mp->lsn) /* discard_metapage doesn't remove it */
761 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600763 /* Try to keep metapages from using up too much memory */
764 drop_metapage(page, mp);
Dave Kleikampd0e671a2006-12-20 15:55:35 -0600765
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600766 unlock_page(page);
767 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770void __invalidate_metapages(struct inode *ip, s64 addr, int len)
771{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600772 sector_t lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int l2BlocksPerPage = PAGE_CACHE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600774 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600776 struct address_space *mapping =
777 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct metapage *mp;
779 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600780 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600783 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * released, but should not be written.
785 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600786 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
787 lblock += BlocksPerPage) {
788 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
789 if (!page)
790 continue;
791 for (offset = 0; offset < PAGE_CACHE_SIZE; offset += PSIZE) {
792 mp = page_to_mp(page, offset);
793 if (!mp)
794 continue;
795 if (mp->index < addr)
796 continue;
797 if (mp->index >= addr + len)
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 clear_bit(META_dirty, &mp->flag);
801 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600802 if (mp->lsn)
803 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600805 unlock_page(page);
806 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808}
809
810#ifdef CONFIG_JFS_STATISTICS
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500811static int jfs_mpstat_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500813 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 "JFS Metapage statistics\n"
815 "=======================\n"
816 "page allocations = %d\n"
817 "page frees = %d\n"
818 "lock waits = %d\n",
819 mpStat.pagealloc,
820 mpStat.pagefree,
821 mpStat.lockwait);
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500824
825static int jfs_mpstat_proc_open(struct inode *inode, struct file *file)
826{
827 return single_open(file, jfs_mpstat_proc_show, NULL);
828}
829
830const struct file_operations jfs_mpstat_proc_fops = {
831 .owner = THIS_MODULE,
832 .open = jfs_mpstat_proc_open,
833 .read = seq_read,
834 .llseek = seq_lseek,
835 .release = single_release,
836};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837#endif