blob: a2f5338a5ea188808c1dd6635f95b1c155ffbffd [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -06003 * Copyright (C) International Business Machines Corp., 2000-2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Portions Copyright (C) Christoph Hellwig, 2001-2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
7#include <linux/fs.h>
Dave Kleikamp7fab4792005-05-02 12:25:02 -06008#include <linux/mm.h>
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -05009#include <linux/module.h>
Dave Kleikamp7fab4792005-05-02 12:25:02 -060010#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/buffer_head.h>
14#include <linux/mempool.h>
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -050015#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "jfs_incore.h"
17#include "jfs_superblock.h"
18#include "jfs_filsys.h"
19#include "jfs_metapage.h"
20#include "jfs_txnmgr.h"
21#include "jfs_debug.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifdef CONFIG_JFS_STATISTICS
24static struct {
25 uint pagealloc; /* # of page allocations */
26 uint pagefree; /* # of page frees */
27 uint lockwait; /* # of sleeping lock_metapage() calls */
28} mpStat;
29#endif
30
Dave Kleikamp7fab4792005-05-02 12:25:02 -060031#define metapage_locked(mp) test_bit(META_locked, &(mp)->flag)
Nick Piggin54af6232007-11-26 14:58:10 -060032#define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static inline void unlock_metapage(struct metapage *mp)
35{
Nick Piggin54af6232007-11-26 14:58:10 -060036 clear_bit_unlock(META_locked, &mp->flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 wake_up(&mp->wait);
38}
39
Dave Kleikamp7fab4792005-05-02 12:25:02 -060040static inline void __lock_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 INCREMENT(mpStat.lockwait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 add_wait_queue_exclusive(&mp->wait, &wait);
45 do {
46 set_current_state(TASK_UNINTERRUPTIBLE);
47 if (metapage_locked(mp)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -060048 unlock_page(mp->page);
Dave Kleikamp4aa0d232007-01-17 21:18:35 -060049 io_schedule();
Dave Kleikamp7fab4792005-05-02 12:25:02 -060050 lock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52 } while (trylock_metapage(mp));
53 __set_current_state(TASK_RUNNING);
54 remove_wait_queue(&mp->wait, &wait);
55}
56
Dave Kleikamp7fab4792005-05-02 12:25:02 -060057/*
58 * Must have mp->page locked
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static inline void lock_metapage(struct metapage *mp)
61{
62 if (trylock_metapage(mp))
63 __lock_metapage(mp);
64}
65
66#define METAPOOL_MIN_PAGES 32
Christoph Lametere18b8902006-12-06 20:33:20 -080067static struct kmem_cache *metapage_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static mempool_t *metapage_mempool;
69
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030070#define MPS_PER_PAGE (PAGE_SIZE >> L2PSIZE)
Dave Kleikamp7fab4792005-05-02 12:25:02 -060071
72#if MPS_PER_PAGE > 1
73
74struct meta_anchor {
75 int mp_count;
76 atomic_t io_count;
77 struct metapage *mp[MPS_PER_PAGE];
78};
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070079#define mp_anchor(page) ((struct meta_anchor *)page_private(page))
Dave Kleikamp7fab4792005-05-02 12:25:02 -060080
Dave Kleikamp967c9ec2008-01-10 16:04:25 -060081static inline struct metapage *page_to_mp(struct page *page, int offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -060082{
83 if (!PagePrivate(page))
84 return NULL;
85 return mp_anchor(page)->mp[offset >> L2PSIZE];
86}
87
88static inline int insert_metapage(struct page *page, struct metapage *mp)
89{
90 struct meta_anchor *a;
91 int index;
92 int l2mp_blocks; /* log2 blocks per metapage */
93
94 if (PagePrivate(page))
95 a = mp_anchor(page);
96 else {
Eric Sesterhenn5b3030e2006-02-23 09:47:13 -060097 a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS);
Dave Kleikamp7fab4792005-05-02 12:25:02 -060098 if (!a)
99 return -ENOMEM;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700100 set_page_private(page, (unsigned long)a);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600101 SetPagePrivate(page);
102 kmap(page);
103 }
104
105 if (mp) {
106 l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
107 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
108 a->mp_count++;
109 a->mp[index] = mp;
110 }
111
112 return 0;
113}
114
115static inline void remove_metapage(struct page *page, struct metapage *mp)
116{
117 struct meta_anchor *a = mp_anchor(page);
118 int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits;
119 int index;
120
121 index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1);
122
123 BUG_ON(a->mp[index] != mp);
124
125 a->mp[index] = NULL;
126 if (--a->mp_count == 0) {
127 kfree(a);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700128 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600129 ClearPagePrivate(page);
130 kunmap(page);
131 }
132}
133
134static inline void inc_io(struct page *page)
135{
136 atomic_inc(&mp_anchor(page)->io_count);
137}
138
139static inline void dec_io(struct page *page, void (*handler) (struct page *))
140{
141 if (atomic_dec_and_test(&mp_anchor(page)->io_count))
142 handler(page);
143}
144
145#else
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600146static inline struct metapage *page_to_mp(struct page *page, int offset)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600147{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700148 return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600149}
150
151static inline int insert_metapage(struct page *page, struct metapage *mp)
152{
153 if (mp) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700154 set_page_private(page, (unsigned long)mp);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600155 SetPagePrivate(page);
156 kmap(page);
157 }
158 return 0;
159}
160
161static inline void remove_metapage(struct page *page, struct metapage *mp)
162{
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700163 set_page_private(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600164 ClearPagePrivate(page);
165 kunmap(page);
166}
167
168#define inc_io(page) do {} while(0)
169#define dec_io(page, handler) handler(page)
170
171#endif
172
Al Viro27496a82005-10-21 03:20:48 -0400173static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
David Rientjesee146242015-04-15 16:14:11 -0700175 struct metapage *mp = mempool_alloc(metapage_mempool, gfp_mask);
176
177 if (mp) {
178 mp->lid = 0;
179 mp->lsn = 0;
180 mp->data = NULL;
181 mp->clsn = 0;
182 mp->log = NULL;
183 init_waitqueue_head(&mp->wait);
184 }
185 return mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188static inline void free_metapage(struct metapage *mp)
189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 mempool_free(mp, metapage_mempool);
191}
192
193int __init metapage_init(void)
194{
195 /*
196 * Allocate the metapage structures
197 */
198 metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage),
David Rientjesee146242015-04-15 16:14:11 -0700199 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (metapage_cache == NULL)
201 return -ENOMEM;
202
Matthew Dobson93d23412006-03-26 01:37:50 -0800203 metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES,
204 metapage_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (metapage_mempool == NULL) {
207 kmem_cache_destroy(metapage_cache);
208 return -ENOMEM;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return 0;
212}
213
214void metapage_exit(void)
215{
216 mempool_destroy(metapage_mempool);
217 kmem_cache_destroy(metapage_cache);
218}
219
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600220static inline void drop_metapage(struct page *page, struct metapage *mp)
221{
222 if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) ||
223 test_bit(META_io, &mp->flag))
224 return;
225 remove_metapage(page, mp);
226 INCREMENT(mpStat.pagefree);
227 free_metapage(mp);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600231 * Metapage address space operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600233
234static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock,
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600235 int *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600237 int rc = 0;
238 int xflag;
239 s64 xaddr;
Theodore Ts'oba52de12006-09-27 01:50:49 -0700240 sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600241 inode->i_blkbits;
242
243 if (lblock >= file_blocks)
244 return 0;
245 if (lblock + *len > file_blocks)
246 *len = file_blocks - lblock;
247
248 if (inode->i_ino) {
249 rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0);
250 if ((rc == 0) && *len)
251 lblock = (sector_t)xaddr;
252 else
253 lblock = 0;
254 } /* else no mapping */
255
256 return lblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600259static void last_read_complete(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600261 if (!PageError(page))
262 SetPageUptodate(page);
263 unlock_page(page);
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200266static void metapage_read_end_io(struct bio *bio)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600267{
268 struct page *page = bio->bi_private;
269
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200270 if (bio->bi_status) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600271 printk(KERN_ERR "metapage_read_end_io: I/O error\n");
272 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600275 dec_io(page, last_read_complete);
276 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600279static void remove_from_logsync(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600281 struct jfs_log *log = mp->log;
282 unsigned long flags;
283/*
284 * This can race. Recheck that log hasn't been set to null, and after
285 * acquiring logsync lock, recheck lsn
286 */
287 if (!log)
288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600290 LOGSYNC_LOCK(log, flags);
291 if (mp->lsn) {
292 mp->log = NULL;
293 mp->lsn = 0;
294 mp->clsn = 0;
295 log->count--;
296 list_del(&mp->synclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600298 LOGSYNC_UNLOCK(log, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600301static void last_write_complete(struct page *page)
302{
303 struct metapage *mp;
304 unsigned int offset;
305
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300306 for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600307 mp = page_to_mp(page, offset);
308 if (mp && test_bit(META_io, &mp->flag)) {
309 if (mp->lsn)
310 remove_from_logsync(mp);
311 clear_bit(META_io, &mp->flag);
312 }
313 /*
314 * I'd like to call drop_metapage here, but I don't think it's
315 * safe unless I have the page locked
316 */
317 }
318 end_page_writeback(page);
319}
320
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200321static void metapage_write_end_io(struct bio *bio)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600322{
323 struct page *page = bio->bi_private;
324
325 BUG_ON(!PagePrivate(page));
326
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200327 if (bio->bi_status) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600328 printk(KERN_ERR "metapage_write_end_io: I/O error\n");
329 SetPageError(page);
330 }
331 dec_io(page, last_write_complete);
332 bio_put(bio);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600333}
334
335static int metapage_writepage(struct page *page, struct writeback_control *wbc)
336{
337 struct bio *bio = NULL;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600338 int block_offset; /* block offset of mp within page */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600339 struct inode *inode = page->mapping->host;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600340 int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage;
341 int len;
342 int xlen;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600343 struct metapage *mp;
344 int redirty = 0;
345 sector_t lblock;
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600346 int nr_underway = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600347 sector_t pblock;
348 sector_t next_block = 0;
349 sector_t page_start;
350 unsigned long bio_bytes = 0;
351 unsigned long bio_offset = 0;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600352 int offset;
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600353 int bad_blocks = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600354
355 page_start = (sector_t)page->index <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300356 (PAGE_SHIFT - inode->i_blkbits);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600357 BUG_ON(!PageLocked(page));
358 BUG_ON(PageWriteback(page));
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600359 set_page_writeback(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600360
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300361 for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600362 mp = page_to_mp(page, offset);
363
364 if (!mp || !test_bit(META_dirty, &mp->flag))
365 continue;
366
367 if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) {
368 redirty = 1;
Dave Kleikampac17b8b2005-10-03 15:32:11 -0500369 /*
370 * Make sure this page isn't blocked indefinitely.
371 * If the journal isn't undergoing I/O, push it
372 */
373 if (mp->log && !(mp->log->cflag & logGC_PAGEOUT))
374 jfs_flush_journal(mp->log, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600375 continue;
376 }
377
378 clear_bit(META_dirty, &mp->flag);
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600379 set_bit(META_io, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600380 block_offset = offset >> inode->i_blkbits;
381 lblock = page_start + block_offset;
382 if (bio) {
383 if (xlen && lblock == next_block) {
384 /* Contiguous, in memory & on disk */
385 len = min(xlen, blocks_per_mp);
386 xlen -= len;
387 bio_bytes += len << inode->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600388 continue;
389 }
390 /* Not contiguous */
391 if (bio_add_page(bio, page, bio_bytes, bio_offset) <
392 bio_bytes)
393 goto add_failed;
394 /*
395 * Increment counter before submitting i/o to keep
396 * count from hitting zero before we're through
397 */
398 inc_io(page);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700399 if (!bio->bi_iter.bi_size)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600400 goto dump_bio;
Mike Christie4e49ea42016-06-05 14:31:41 -0500401 submit_bio(bio);
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600402 nr_underway++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600403 bio = NULL;
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600404 } else
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600405 inc_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300406 xlen = (PAGE_SIZE - offset) >> inode->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600407 pblock = metapage_get_blocks(inode, lblock, &xlen);
408 if (!pblock) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600409 printk(KERN_ERR "JFS: metapage_get_blocks failed\n");
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600410 /*
411 * We already called inc_io(), but can't cancel it
412 * with dec_io() until we're done with the page
413 */
414 bad_blocks++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600415 continue;
416 }
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600417 len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600418
419 bio = bio_alloc(GFP_NOFS, 1);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200420 bio_set_dev(bio, inode->i_sb->s_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700421 bio->bi_iter.bi_sector = pblock << (inode->i_blkbits - 9);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600422 bio->bi_end_io = metapage_write_end_io;
423 bio->bi_private = page;
Mike Christie95fe6c12016-06-05 14:31:48 -0500424 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600425
426 /* Don't call bio_add_page yet, we may add to this vec */
427 bio_offset = offset;
428 bio_bytes = len << inode->i_blkbits;
429
430 xlen -= len;
431 next_block = lblock + len;
432 }
433 if (bio) {
434 if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes)
435 goto add_failed;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700436 if (!bio->bi_iter.bi_size)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600437 goto dump_bio;
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500438
Mike Christie4e49ea42016-06-05 14:31:41 -0500439 submit_bio(bio);
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600440 nr_underway++;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600441 }
442 if (redirty)
443 redirty_page_for_writepage(wbc, page);
444
445 unlock_page(page);
446
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600447 if (bad_blocks)
448 goto err_out;
449
Dave Kleikamp29a424f2008-01-03 13:09:33 -0600450 if (nr_underway == 0)
451 end_page_writeback(page);
452
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600453 return 0;
454add_failed:
455 /* We should never reach here, since we're only adding one vec */
456 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
457 goto skip;
458dump_bio:
Dave Kleikamp288e4d82007-06-13 10:17:50 -0500459 print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16,
460 4, bio, sizeof(*bio), 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600461skip:
462 bio_put(bio);
463 unlock_page(page);
464 dec_io(page, last_write_complete);
Dave Kleikamp1ad53a92009-01-30 14:09:06 -0600465err_out:
466 while (bad_blocks--)
467 dec_io(page, last_write_complete);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600468 return -EIO;
469}
470
471static int metapage_readpage(struct file *fp, struct page *page)
472{
473 struct inode *inode = page->mapping->host;
474 struct bio *bio = NULL;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600475 int block_offset;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300476 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600477 sector_t page_start; /* address of page in fs blocks */
478 sector_t pblock;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600479 int xlen;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600480 unsigned int len;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600481 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600482
483 BUG_ON(!PageLocked(page));
484 page_start = (sector_t)page->index <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300485 (PAGE_SHIFT - inode->i_blkbits);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600486
487 block_offset = 0;
488 while (block_offset < blocks_per_page) {
489 xlen = blocks_per_page - block_offset;
490 pblock = metapage_get_blocks(inode, page_start + block_offset,
491 &xlen);
492 if (pblock) {
493 if (!PagePrivate(page))
494 insert_metapage(page, NULL);
495 inc_io(page);
496 if (bio)
Mike Christie4e49ea42016-06-05 14:31:41 -0500497 submit_bio(bio);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600498
499 bio = bio_alloc(GFP_NOFS, 1);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200500 bio_set_dev(bio, inode->i_sb->s_bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700501 bio->bi_iter.bi_sector =
502 pblock << (inode->i_blkbits - 9);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600503 bio->bi_end_io = metapage_read_end_io;
504 bio->bi_private = page;
Mike Christie95fe6c12016-06-05 14:31:48 -0500505 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600506 len = xlen << inode->i_blkbits;
507 offset = block_offset << inode->i_blkbits;
508 if (bio_add_page(bio, page, len, offset) < len)
509 goto add_failed;
510 block_offset += xlen;
511 } else
512 block_offset++;
513 }
514 if (bio)
Mike Christie4e49ea42016-06-05 14:31:41 -0500515 submit_bio(bio);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600516 else
517 unlock_page(page);
518
519 return 0;
520
521add_failed:
522 printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n");
523 bio_put(bio);
524 dec_io(page, last_read_complete);
525 return -EIO;
526}
527
Al Viro27496a82005-10-21 03:20:48 -0400528static int metapage_releasepage(struct page *page, gfp_t gfp_mask)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600529{
530 struct metapage *mp;
Dave Kleikampb9646382006-05-24 07:43:38 -0500531 int ret = 1;
Dave Kleikamp967c9ec2008-01-10 16:04:25 -0600532 int offset;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600533
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300534 for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600535 mp = page_to_mp(page, offset);
536
537 if (!mp)
538 continue;
539
540 jfs_info("metapage_releasepage: mp = 0x%p", mp);
Dave Kleikampb9646382006-05-24 07:43:38 -0500541 if (mp->count || mp->nohomeok ||
542 test_bit(META_dirty, &mp->flag)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600543 jfs_info("count = %ld, nohomeok = %d", mp->count,
544 mp->nohomeok);
Dave Kleikampb9646382006-05-24 07:43:38 -0500545 ret = 0;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600546 continue;
547 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600548 if (mp->lsn)
549 remove_from_logsync(mp);
550 remove_metapage(page, mp);
551 INCREMENT(mpStat.pagefree);
552 free_metapage(mp);
553 }
Dave Kleikampb9646382006-05-24 07:43:38 -0500554 return ret;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600555}
556
Lukas Czernerd47992f2013-05-21 23:17:23 -0400557static void metapage_invalidatepage(struct page *page, unsigned int offset,
558 unsigned int length)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600559{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300560 BUG_ON(offset || length < PAGE_SIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600561
NeilBrown2ff28e22006-03-26 01:37:18 -0800562 BUG_ON(PageWriteback(page));
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600563
NeilBrown2ff28e22006-03-26 01:37:18 -0800564 metapage_releasepage(page, 0);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600565}
566
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700567const struct address_space_operations jfs_metapage_aops = {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600568 .readpage = metapage_readpage,
569 .writepage = metapage_writepage,
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600570 .releasepage = metapage_releasepage,
571 .invalidatepage = metapage_invalidatepage,
572 .set_page_dirty = __set_page_dirty_nobuffers,
573};
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
576 unsigned int size, int absolute,
577 unsigned long new)
578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int l2BlocksPerPage;
580 int l2bsize;
581 struct address_space *mapping;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600582 struct metapage *mp = NULL;
583 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned long page_index;
585 unsigned long page_offset;
586
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600587 jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d",
588 inode->i_ino, lblock, absolute);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600590 l2bsize = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300591 l2BlocksPerPage = PAGE_SHIFT - l2bsize;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600592 page_index = lblock >> l2BlocksPerPage;
593 page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300594 if ((page_offset + size) > PAGE_SIZE) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600595 jfs_err("MetaData crosses page boundary!!");
596 jfs_err("lblock = %lx, size = %d", lblock, size);
597 dump_stack();
598 return NULL;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (absolute)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600601 mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 else {
603 /*
604 * If an nfs client tries to read an inode that is larger
605 * than any existing inodes, we may try to read past the
606 * end of the inode map
607 */
608 if ((lblock << inode->i_blkbits) >= inode->i_size)
609 return NULL;
610 mapping = inode->i_mapping;
611 }
612
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300613 if (new && (PSIZE == PAGE_SIZE)) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600614 page = grab_cache_page(mapping, page_index);
615 if (!page) {
616 jfs_err("grab_cache_page failed!");
617 return NULL;
618 }
619 SetPageUptodate(page);
620 } else {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700621 page = read_mapping_page(mapping, page_index, NULL);
Dave Kleikampc40c20242005-07-22 11:08:44 -0500622 if (IS_ERR(page) || !PageUptodate(page)) {
Pekka Enberg090d2b12006-06-23 02:05:08 -0700623 jfs_err("read_mapping_page failed!");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600624 return NULL;
625 }
626 lock_page(page);
627 }
628
629 mp = page_to_mp(page, page_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (mp) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600631 if (mp->logical_size != size) {
632 jfs_error(inode->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -0700633 "get_mp->logical_size != size\n");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600634 jfs_err("logical_size = %d, size = %d",
635 mp->logical_size, size);
636 dump_stack();
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500637 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 mp->count++;
640 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (test_bit(META_discard, &mp->flag)) {
642 if (!new) {
643 jfs_error(inode->i_sb,
Joe Percheseb8630d2013-06-04 16:39:15 -0700644 "using a discarded metapage\n");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600645 discard_metapage(mp);
Dave Kleikamp63f83c92006-10-02 09:55:27 -0500646 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 clear_bit(META_discard, &mp->flag);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 } else {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600651 INCREMENT(mpStat.pagealloc);
652 mp = alloc_metapage(GFP_NOFS);
Juerg Haefliger88a96fa2017-10-04 04:24:41 -0400653 if (!mp)
654 goto unlock;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600655 mp->page = page;
Dave Kleikamp11ab8312017-07-05 15:26:49 -0400656 mp->sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 mp->flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 mp->xflag = COMMIT_PAGE;
659 mp->count = 1;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600660 mp->nohomeok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 mp->logical_size = size;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600662 mp->data = page_address(page) + page_offset;
663 mp->index = lblock;
664 if (unlikely(insert_metapage(page, mp))) {
665 free_metapage(mp);
666 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600668 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600671 if (new) {
672 jfs_info("zeroing mp = 0x%p", mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 memset(mp->data, 0, PSIZE);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600676 unlock_page(page);
677 jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return mp;
679
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600680unlock:
681 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return NULL;
683}
684
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600685void grab_metapage(struct metapage * mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600687 jfs_info("grab_metapage: mp = 0x%p", mp);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300688 get_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 lock_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600690 mp->count++;
691 lock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 unlock_page(mp->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600695void force_metapage(struct metapage *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 struct page *page = mp->page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600698 jfs_info("force_metapage: mp = 0x%p", mp);
699 set_bit(META_forcewrite, &mp->flag);
700 clear_bit(META_sync, &mp->flag);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300701 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 lock_page(page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600703 set_page_dirty(page);
Dave Kleikamp11ab8312017-07-05 15:26:49 -0400704 if (write_one_page(page))
705 jfs_error(mp->sb, "write_one_page() failed\n");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600706 clear_bit(META_forcewrite, &mp->flag);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500710void hold_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600711{
712 lock_page(mp->page);
713}
714
Dave Kleikamp1868f4a2005-05-04 15:29:35 -0500715void put_metapage(struct metapage *mp)
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600716{
717 if (mp->count || mp->nohomeok) {
718 /* Someone else will release this */
719 unlock_page(mp->page);
720 return;
721 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300722 get_page(mp->page);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600723 mp->count++;
724 lock_metapage(mp);
725 unlock_page(mp->page);
726 release_metapage(mp);
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729void release_metapage(struct metapage * mp)
730{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600731 struct page *page = mp->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag);
733
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600734 BUG_ON(!page);
735
736 lock_page(page);
737 unlock_metapage(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 assert(mp->count);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600740 if (--mp->count || mp->nohomeok) {
741 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300742 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744 }
745
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600746 if (test_bit(META_dirty, &mp->flag)) {
747 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (test_bit(META_sync, &mp->flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 clear_bit(META_sync, &mp->flag);
Dave Kleikamp11ab8312017-07-05 15:26:49 -0400750 if (write_one_page(page))
751 jfs_error(mp->sb, "write_one_page() failed\n");
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600752 lock_page(page); /* write_one_page unlocks the page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600754 } else if (mp->lsn) /* discard_metapage doesn't remove it */
755 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600757 /* Try to keep metapages from using up too much memory */
758 drop_metapage(page, mp);
Dave Kleikampd0e671a2006-12-20 15:55:35 -0600759
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600760 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300761 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764void __invalidate_metapages(struct inode *ip, s64 addr, int len)
765{
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600766 sector_t lblock;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300767 int l2BlocksPerPage = PAGE_SHIFT - ip->i_blkbits;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600768 int BlocksPerPage = 1 << l2BlocksPerPage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* All callers are interested in block device's mapping */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600770 struct address_space *mapping =
771 JFS_SBI(ip->i_sb)->direct_inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct metapage *mp;
773 struct page *page;
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600774 unsigned int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /*
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600777 * Mark metapages to discard. They will eventually be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * released, but should not be written.
779 */
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600780 for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len;
781 lblock += BlocksPerPage) {
782 page = find_lock_page(mapping, lblock >> l2BlocksPerPage);
783 if (!page)
784 continue;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300785 for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) {
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600786 mp = page_to_mp(page, offset);
787 if (!mp)
788 continue;
789 if (mp->index < addr)
790 continue;
791 if (mp->index >= addr + len)
792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 clear_bit(META_dirty, &mp->flag);
795 set_bit(META_discard, &mp->flag);
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600796 if (mp->lsn)
797 remove_from_logsync(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Dave Kleikamp7fab4792005-05-02 12:25:02 -0600799 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300800 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802}
803
804#ifdef CONFIG_JFS_STATISTICS
Christoph Hellwig07a3b8e2018-04-11 16:51:18 +0200805int jfs_mpstat_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500807 seq_printf(m,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 "JFS Metapage statistics\n"
809 "=======================\n"
810 "page allocations = %d\n"
811 "page frees = %d\n"
812 "lock waits = %d\n",
813 mpStat.pagealloc,
814 mpStat.pagefree,
815 mpStat.lockwait);
Alexey Dobriyanb2e03ca2008-05-13 08:22:10 -0500816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#endif