Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 3 | * Copyright (C) International Business Machines Corp., 2000-2005 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Portions Copyright (C) Christoph Hellwig, 2001-2002 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/fs.h> |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 8 | #include <linux/mm.h> |
Alexey Dobriyan | b2e03ca | 2008-05-13 08:22:10 -0500 | [diff] [blame] | 9 | #include <linux/module.h> |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 10 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/mempool.h> |
Alexey Dobriyan | b2e03ca | 2008-05-13 08:22:10 -0500 | [diff] [blame] | 15 | #include <linux/seq_file.h> |
Matthew Wilcox (Oracle) | cd78ab1 | 2021-05-02 23:22:52 -0400 | [diff] [blame] | 16 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "jfs_incore.h" |
| 18 | #include "jfs_superblock.h" |
| 19 | #include "jfs_filsys.h" |
| 20 | #include "jfs_metapage.h" |
| 21 | #include "jfs_txnmgr.h" |
| 22 | #include "jfs_debug.h" |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #ifdef CONFIG_JFS_STATISTICS |
| 25 | static struct { |
| 26 | uint pagealloc; /* # of page allocations */ |
| 27 | uint pagefree; /* # of page frees */ |
| 28 | uint lockwait; /* # of sleeping lock_metapage() calls */ |
| 29 | } mpStat; |
| 30 | #endif |
| 31 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 32 | #define metapage_locked(mp) test_bit(META_locked, &(mp)->flag) |
Nick Piggin | 54af623 | 2007-11-26 14:58:10 -0600 | [diff] [blame] | 33 | #define trylock_metapage(mp) test_and_set_bit_lock(META_locked, &(mp)->flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | static inline void unlock_metapage(struct metapage *mp) |
| 36 | { |
Nick Piggin | 54af623 | 2007-11-26 14:58:10 -0600 | [diff] [blame] | 37 | clear_bit_unlock(META_locked, &mp->flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | wake_up(&mp->wait); |
| 39 | } |
| 40 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 41 | static inline void __lock_metapage(struct metapage *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | DECLARE_WAITQUEUE(wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | INCREMENT(mpStat.lockwait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | add_wait_queue_exclusive(&mp->wait, &wait); |
| 46 | do { |
| 47 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 48 | if (metapage_locked(mp)) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 49 | unlock_page(mp->page); |
Dave Kleikamp | 4aa0d23 | 2007-01-17 21:18:35 -0600 | [diff] [blame] | 50 | io_schedule(); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 51 | lock_page(mp->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | } while (trylock_metapage(mp)); |
| 54 | __set_current_state(TASK_RUNNING); |
| 55 | remove_wait_queue(&mp->wait, &wait); |
| 56 | } |
| 57 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 58 | /* |
| 59 | * Must have mp->page locked |
| 60 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static inline void lock_metapage(struct metapage *mp) |
| 62 | { |
| 63 | if (trylock_metapage(mp)) |
| 64 | __lock_metapage(mp); |
| 65 | } |
| 66 | |
| 67 | #define METAPOOL_MIN_PAGES 32 |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 68 | static struct kmem_cache *metapage_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static mempool_t *metapage_mempool; |
| 70 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 71 | #define MPS_PER_PAGE (PAGE_SIZE >> L2PSIZE) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 72 | |
| 73 | #if MPS_PER_PAGE > 1 |
| 74 | |
| 75 | struct meta_anchor { |
| 76 | int mp_count; |
| 77 | atomic_t io_count; |
| 78 | struct metapage *mp[MPS_PER_PAGE]; |
| 79 | }; |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 80 | #define mp_anchor(page) ((struct meta_anchor *)page_private(page)) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 81 | |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 82 | static inline struct metapage *page_to_mp(struct page *page, int offset) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 83 | { |
| 84 | if (!PagePrivate(page)) |
| 85 | return NULL; |
| 86 | return mp_anchor(page)->mp[offset >> L2PSIZE]; |
| 87 | } |
| 88 | |
| 89 | static inline int insert_metapage(struct page *page, struct metapage *mp) |
| 90 | { |
| 91 | struct meta_anchor *a; |
| 92 | int index; |
| 93 | int l2mp_blocks; /* log2 blocks per metapage */ |
| 94 | |
| 95 | if (PagePrivate(page)) |
| 96 | a = mp_anchor(page); |
| 97 | else { |
Eric Sesterhenn | 5b3030e | 2006-02-23 09:47:13 -0600 | [diff] [blame] | 98 | a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 99 | if (!a) |
| 100 | return -ENOMEM; |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 101 | set_page_private(page, (unsigned long)a); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 102 | SetPagePrivate(page); |
| 103 | kmap(page); |
| 104 | } |
| 105 | |
| 106 | if (mp) { |
| 107 | l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits; |
| 108 | index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1); |
| 109 | a->mp_count++; |
| 110 | a->mp[index] = mp; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static inline void remove_metapage(struct page *page, struct metapage *mp) |
| 117 | { |
| 118 | struct meta_anchor *a = mp_anchor(page); |
| 119 | int l2mp_blocks = L2PSIZE - page->mapping->host->i_blkbits; |
| 120 | int index; |
| 121 | |
| 122 | index = (mp->index >> l2mp_blocks) & (MPS_PER_PAGE - 1); |
| 123 | |
| 124 | BUG_ON(a->mp[index] != mp); |
| 125 | |
| 126 | a->mp[index] = NULL; |
| 127 | if (--a->mp_count == 0) { |
| 128 | kfree(a); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 129 | set_page_private(page, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 130 | ClearPagePrivate(page); |
| 131 | kunmap(page); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static inline void inc_io(struct page *page) |
| 136 | { |
| 137 | atomic_inc(&mp_anchor(page)->io_count); |
| 138 | } |
| 139 | |
| 140 | static inline void dec_io(struct page *page, void (*handler) (struct page *)) |
| 141 | { |
| 142 | if (atomic_dec_and_test(&mp_anchor(page)->io_count)) |
| 143 | handler(page); |
| 144 | } |
| 145 | |
| 146 | #else |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 147 | static inline struct metapage *page_to_mp(struct page *page, int offset) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 148 | { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 149 | return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static inline int insert_metapage(struct page *page, struct metapage *mp) |
| 153 | { |
| 154 | if (mp) { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 155 | set_page_private(page, (unsigned long)mp); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 156 | SetPagePrivate(page); |
| 157 | kmap(page); |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static inline void remove_metapage(struct page *page, struct metapage *mp) |
| 163 | { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 164 | set_page_private(page, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 165 | ClearPagePrivate(page); |
| 166 | kunmap(page); |
| 167 | } |
| 168 | |
| 169 | #define inc_io(page) do {} while(0) |
| 170 | #define dec_io(page, handler) handler(page) |
| 171 | |
| 172 | #endif |
| 173 | |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 174 | static inline struct metapage *alloc_metapage(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
David Rientjes | ee14624 | 2015-04-15 16:14:11 -0700 | [diff] [blame] | 176 | struct metapage *mp = mempool_alloc(metapage_mempool, gfp_mask); |
| 177 | |
| 178 | if (mp) { |
| 179 | mp->lid = 0; |
| 180 | mp->lsn = 0; |
| 181 | mp->data = NULL; |
| 182 | mp->clsn = 0; |
| 183 | mp->log = NULL; |
| 184 | init_waitqueue_head(&mp->wait); |
| 185 | } |
| 186 | return mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static inline void free_metapage(struct metapage *mp) |
| 190 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | mempool_free(mp, metapage_mempool); |
| 192 | } |
| 193 | |
| 194 | int __init metapage_init(void) |
| 195 | { |
| 196 | /* |
| 197 | * Allocate the metapage structures |
| 198 | */ |
| 199 | metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage), |
David Rientjes | ee14624 | 2015-04-15 16:14:11 -0700 | [diff] [blame] | 200 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (metapage_cache == NULL) |
| 202 | return -ENOMEM; |
| 203 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 204 | metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES, |
| 205 | metapage_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | if (metapage_mempool == NULL) { |
| 208 | kmem_cache_destroy(metapage_cache); |
| 209 | return -ENOMEM; |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | void metapage_exit(void) |
| 216 | { |
| 217 | mempool_destroy(metapage_mempool); |
| 218 | kmem_cache_destroy(metapage_cache); |
| 219 | } |
| 220 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 221 | static inline void drop_metapage(struct page *page, struct metapage *mp) |
| 222 | { |
| 223 | if (mp->count || mp->nohomeok || test_bit(META_dirty, &mp->flag) || |
| 224 | test_bit(META_io, &mp->flag)) |
| 225 | return; |
| 226 | remove_metapage(page, mp); |
| 227 | INCREMENT(mpStat.pagefree); |
| 228 | free_metapage(mp); |
| 229 | } |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /* |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 232 | * Metapage address space operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 234 | |
| 235 | static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock, |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 236 | int *len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 238 | int rc = 0; |
| 239 | int xflag; |
| 240 | s64 xaddr; |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 241 | sector_t file_blocks = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 242 | inode->i_blkbits; |
| 243 | |
| 244 | if (lblock >= file_blocks) |
| 245 | return 0; |
| 246 | if (lblock + *len > file_blocks) |
| 247 | *len = file_blocks - lblock; |
| 248 | |
| 249 | if (inode->i_ino) { |
| 250 | rc = xtLookup(inode, (s64)lblock, *len, &xflag, &xaddr, len, 0); |
| 251 | if ((rc == 0) && *len) |
| 252 | lblock = (sector_t)xaddr; |
| 253 | else |
| 254 | lblock = 0; |
| 255 | } /* else no mapping */ |
| 256 | |
| 257 | return lblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 260 | static void last_read_complete(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 262 | if (!PageError(page)) |
| 263 | SetPageUptodate(page); |
| 264 | unlock_page(page); |
| 265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 267 | static void metapage_read_end_io(struct bio *bio) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 268 | { |
| 269 | struct page *page = bio->bi_private; |
| 270 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 271 | if (bio->bi_status) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 272 | printk(KERN_ERR "metapage_read_end_io: I/O error\n"); |
| 273 | SetPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 276 | dec_io(page, last_read_complete); |
| 277 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 280 | static void remove_from_logsync(struct metapage *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 282 | struct jfs_log *log = mp->log; |
| 283 | unsigned long flags; |
| 284 | /* |
| 285 | * This can race. Recheck that log hasn't been set to null, and after |
| 286 | * acquiring logsync lock, recheck lsn |
| 287 | */ |
| 288 | if (!log) |
| 289 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 291 | LOGSYNC_LOCK(log, flags); |
| 292 | if (mp->lsn) { |
| 293 | mp->log = NULL; |
| 294 | mp->lsn = 0; |
| 295 | mp->clsn = 0; |
| 296 | log->count--; |
| 297 | list_del(&mp->synclist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 299 | LOGSYNC_UNLOCK(log, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 302 | static void last_write_complete(struct page *page) |
| 303 | { |
| 304 | struct metapage *mp; |
| 305 | unsigned int offset; |
| 306 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 307 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 308 | mp = page_to_mp(page, offset); |
| 309 | if (mp && test_bit(META_io, &mp->flag)) { |
| 310 | if (mp->lsn) |
| 311 | remove_from_logsync(mp); |
| 312 | clear_bit(META_io, &mp->flag); |
| 313 | } |
| 314 | /* |
| 315 | * I'd like to call drop_metapage here, but I don't think it's |
| 316 | * safe unless I have the page locked |
| 317 | */ |
| 318 | } |
| 319 | end_page_writeback(page); |
| 320 | } |
| 321 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 322 | static void metapage_write_end_io(struct bio *bio) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 323 | { |
| 324 | struct page *page = bio->bi_private; |
| 325 | |
| 326 | BUG_ON(!PagePrivate(page)); |
| 327 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 328 | if (bio->bi_status) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 329 | printk(KERN_ERR "metapage_write_end_io: I/O error\n"); |
| 330 | SetPageError(page); |
| 331 | } |
| 332 | dec_io(page, last_write_complete); |
| 333 | bio_put(bio); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int metapage_writepage(struct page *page, struct writeback_control *wbc) |
| 337 | { |
| 338 | struct bio *bio = NULL; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 339 | int block_offset; /* block offset of mp within page */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 340 | struct inode *inode = page->mapping->host; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 341 | int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage; |
| 342 | int len; |
| 343 | int xlen; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 344 | struct metapage *mp; |
| 345 | int redirty = 0; |
| 346 | sector_t lblock; |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 347 | int nr_underway = 0; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 348 | sector_t pblock; |
| 349 | sector_t next_block = 0; |
| 350 | sector_t page_start; |
| 351 | unsigned long bio_bytes = 0; |
| 352 | unsigned long bio_offset = 0; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 353 | int offset; |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 354 | int bad_blocks = 0; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 355 | |
| 356 | page_start = (sector_t)page->index << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 357 | (PAGE_SHIFT - inode->i_blkbits); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 358 | BUG_ON(!PageLocked(page)); |
| 359 | BUG_ON(PageWriteback(page)); |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 360 | set_page_writeback(page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 361 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 362 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 363 | mp = page_to_mp(page, offset); |
| 364 | |
| 365 | if (!mp || !test_bit(META_dirty, &mp->flag)) |
| 366 | continue; |
| 367 | |
| 368 | if (mp->nohomeok && !test_bit(META_forcewrite, &mp->flag)) { |
| 369 | redirty = 1; |
Dave Kleikamp | ac17b8b | 2005-10-03 15:32:11 -0500 | [diff] [blame] | 370 | /* |
| 371 | * Make sure this page isn't blocked indefinitely. |
| 372 | * If the journal isn't undergoing I/O, push it |
| 373 | */ |
| 374 | if (mp->log && !(mp->log->cflag & logGC_PAGEOUT)) |
| 375 | jfs_flush_journal(mp->log, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 376 | continue; |
| 377 | } |
| 378 | |
| 379 | clear_bit(META_dirty, &mp->flag); |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 380 | set_bit(META_io, &mp->flag); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 381 | block_offset = offset >> inode->i_blkbits; |
| 382 | lblock = page_start + block_offset; |
| 383 | if (bio) { |
| 384 | if (xlen && lblock == next_block) { |
| 385 | /* Contiguous, in memory & on disk */ |
| 386 | len = min(xlen, blocks_per_mp); |
| 387 | xlen -= len; |
| 388 | bio_bytes += len << inode->i_blkbits; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 389 | continue; |
| 390 | } |
| 391 | /* Not contiguous */ |
| 392 | if (bio_add_page(bio, page, bio_bytes, bio_offset) < |
| 393 | bio_bytes) |
| 394 | goto add_failed; |
| 395 | /* |
| 396 | * Increment counter before submitting i/o to keep |
| 397 | * count from hitting zero before we're through |
| 398 | */ |
| 399 | inc_io(page); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 400 | if (!bio->bi_iter.bi_size) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 401 | goto dump_bio; |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 402 | submit_bio(bio); |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 403 | nr_underway++; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 404 | bio = NULL; |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 405 | } else |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 406 | inc_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 407 | xlen = (PAGE_SIZE - offset) >> inode->i_blkbits; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 408 | pblock = metapage_get_blocks(inode, lblock, &xlen); |
| 409 | if (!pblock) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 410 | printk(KERN_ERR "JFS: metapage_get_blocks failed\n"); |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 411 | /* |
| 412 | * We already called inc_io(), but can't cancel it |
| 413 | * with dec_io() until we're done with the page |
| 414 | */ |
| 415 | bad_blocks++; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 416 | continue; |
| 417 | } |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 418 | len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 419 | |
| 420 | bio = bio_alloc(GFP_NOFS, 1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 421 | bio_set_dev(bio, inode->i_sb->s_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 422 | bio->bi_iter.bi_sector = pblock << (inode->i_blkbits - 9); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 423 | bio->bi_end_io = metapage_write_end_io; |
| 424 | bio->bi_private = page; |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 425 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 426 | |
| 427 | /* Don't call bio_add_page yet, we may add to this vec */ |
| 428 | bio_offset = offset; |
| 429 | bio_bytes = len << inode->i_blkbits; |
| 430 | |
| 431 | xlen -= len; |
| 432 | next_block = lblock + len; |
| 433 | } |
| 434 | if (bio) { |
| 435 | if (bio_add_page(bio, page, bio_bytes, bio_offset) < bio_bytes) |
| 436 | goto add_failed; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 437 | if (!bio->bi_iter.bi_size) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 438 | goto dump_bio; |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 439 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 440 | submit_bio(bio); |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 441 | nr_underway++; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 442 | } |
| 443 | if (redirty) |
| 444 | redirty_page_for_writepage(wbc, page); |
| 445 | |
| 446 | unlock_page(page); |
| 447 | |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 448 | if (bad_blocks) |
| 449 | goto err_out; |
| 450 | |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 451 | if (nr_underway == 0) |
| 452 | end_page_writeback(page); |
| 453 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 454 | return 0; |
| 455 | add_failed: |
| 456 | /* We should never reach here, since we're only adding one vec */ |
| 457 | printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n"); |
| 458 | goto skip; |
| 459 | dump_bio: |
Dave Kleikamp | 288e4d8 | 2007-06-13 10:17:50 -0500 | [diff] [blame] | 460 | print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16, |
| 461 | 4, bio, sizeof(*bio), 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 462 | skip: |
| 463 | bio_put(bio); |
| 464 | unlock_page(page); |
| 465 | dec_io(page, last_write_complete); |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 466 | err_out: |
| 467 | while (bad_blocks--) |
| 468 | dec_io(page, last_write_complete); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 469 | return -EIO; |
| 470 | } |
| 471 | |
| 472 | static int metapage_readpage(struct file *fp, struct page *page) |
| 473 | { |
| 474 | struct inode *inode = page->mapping->host; |
| 475 | struct bio *bio = NULL; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 476 | int block_offset; |
Matthew Wilcox (Oracle) | 24addd8 | 2020-09-21 08:58:39 -0700 | [diff] [blame] | 477 | int blocks_per_page = i_blocks_per_page(inode, page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 478 | sector_t page_start; /* address of page in fs blocks */ |
| 479 | sector_t pblock; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 480 | int xlen; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 481 | unsigned int len; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 482 | int offset; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 483 | |
| 484 | BUG_ON(!PageLocked(page)); |
| 485 | page_start = (sector_t)page->index << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 486 | (PAGE_SHIFT - inode->i_blkbits); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 487 | |
| 488 | block_offset = 0; |
| 489 | while (block_offset < blocks_per_page) { |
| 490 | xlen = blocks_per_page - block_offset; |
| 491 | pblock = metapage_get_blocks(inode, page_start + block_offset, |
| 492 | &xlen); |
| 493 | if (pblock) { |
| 494 | if (!PagePrivate(page)) |
| 495 | insert_metapage(page, NULL); |
| 496 | inc_io(page); |
| 497 | if (bio) |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 498 | submit_bio(bio); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 499 | |
| 500 | bio = bio_alloc(GFP_NOFS, 1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 501 | bio_set_dev(bio, inode->i_sb->s_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 502 | bio->bi_iter.bi_sector = |
| 503 | pblock << (inode->i_blkbits - 9); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 504 | bio->bi_end_io = metapage_read_end_io; |
| 505 | bio->bi_private = page; |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 506 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 507 | len = xlen << inode->i_blkbits; |
| 508 | offset = block_offset << inode->i_blkbits; |
| 509 | if (bio_add_page(bio, page, len, offset) < len) |
| 510 | goto add_failed; |
| 511 | block_offset += xlen; |
| 512 | } else |
| 513 | block_offset++; |
| 514 | } |
| 515 | if (bio) |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 516 | submit_bio(bio); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 517 | else |
| 518 | unlock_page(page); |
| 519 | |
| 520 | return 0; |
| 521 | |
| 522 | add_failed: |
| 523 | printk(KERN_ERR "JFS: bio_add_page failed unexpectedly\n"); |
| 524 | bio_put(bio); |
| 525 | dec_io(page, last_read_complete); |
| 526 | return -EIO; |
| 527 | } |
| 528 | |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 529 | static int metapage_releasepage(struct page *page, gfp_t gfp_mask) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 530 | { |
| 531 | struct metapage *mp; |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 532 | int ret = 1; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 533 | int offset; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 534 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 535 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 536 | mp = page_to_mp(page, offset); |
| 537 | |
| 538 | if (!mp) |
| 539 | continue; |
| 540 | |
| 541 | jfs_info("metapage_releasepage: mp = 0x%p", mp); |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 542 | if (mp->count || mp->nohomeok || |
| 543 | test_bit(META_dirty, &mp->flag)) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 544 | jfs_info("count = %ld, nohomeok = %d", mp->count, |
| 545 | mp->nohomeok); |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 546 | ret = 0; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 547 | continue; |
| 548 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 549 | if (mp->lsn) |
| 550 | remove_from_logsync(mp); |
| 551 | remove_metapage(page, mp); |
| 552 | INCREMENT(mpStat.pagefree); |
| 553 | free_metapage(mp); |
| 554 | } |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 555 | return ret; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 556 | } |
| 557 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 558 | static void metapage_invalidatepage(struct page *page, unsigned int offset, |
| 559 | unsigned int length) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 560 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 561 | BUG_ON(offset || length < PAGE_SIZE); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 562 | |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 563 | BUG_ON(PageWriteback(page)); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 564 | |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 565 | metapage_releasepage(page, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 566 | } |
| 567 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 568 | const struct address_space_operations jfs_metapage_aops = { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 569 | .readpage = metapage_readpage, |
| 570 | .writepage = metapage_writepage, |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 571 | .releasepage = metapage_releasepage, |
| 572 | .invalidatepage = metapage_invalidatepage, |
| 573 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 574 | }; |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | struct metapage *__get_metapage(struct inode *inode, unsigned long lblock, |
| 577 | unsigned int size, int absolute, |
| 578 | unsigned long new) |
| 579 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | int l2BlocksPerPage; |
| 581 | int l2bsize; |
| 582 | struct address_space *mapping; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 583 | struct metapage *mp = NULL; |
| 584 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | unsigned long page_index; |
| 586 | unsigned long page_offset; |
| 587 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 588 | jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d", |
| 589 | inode->i_ino, lblock, absolute); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 591 | l2bsize = inode->i_blkbits; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 592 | l2BlocksPerPage = PAGE_SHIFT - l2bsize; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 593 | page_index = lblock >> l2BlocksPerPage; |
| 594 | page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 595 | if ((page_offset + size) > PAGE_SIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 596 | jfs_err("MetaData crosses page boundary!!"); |
| 597 | jfs_err("lblock = %lx, size = %d", lblock, size); |
| 598 | dump_stack(); |
| 599 | return NULL; |
| 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (absolute) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 602 | mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | else { |
| 604 | /* |
| 605 | * If an nfs client tries to read an inode that is larger |
| 606 | * than any existing inodes, we may try to read past the |
| 607 | * end of the inode map |
| 608 | */ |
| 609 | if ((lblock << inode->i_blkbits) >= inode->i_size) |
| 610 | return NULL; |
| 611 | mapping = inode->i_mapping; |
| 612 | } |
| 613 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 614 | if (new && (PSIZE == PAGE_SIZE)) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 615 | page = grab_cache_page(mapping, page_index); |
| 616 | if (!page) { |
| 617 | jfs_err("grab_cache_page failed!"); |
| 618 | return NULL; |
| 619 | } |
| 620 | SetPageUptodate(page); |
| 621 | } else { |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 622 | page = read_mapping_page(mapping, page_index, NULL); |
Dave Kleikamp | c40c2024 | 2005-07-22 11:08:44 -0500 | [diff] [blame] | 623 | if (IS_ERR(page) || !PageUptodate(page)) { |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 624 | jfs_err("read_mapping_page failed!"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 625 | return NULL; |
| 626 | } |
| 627 | lock_page(page); |
| 628 | } |
| 629 | |
| 630 | mp = page_to_mp(page, page_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (mp) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 632 | if (mp->logical_size != size) { |
| 633 | jfs_error(inode->i_sb, |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 634 | "get_mp->logical_size != size\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 635 | jfs_err("logical_size = %d, size = %d", |
| 636 | mp->logical_size, size); |
| 637 | dump_stack(); |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 638 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | mp->count++; |
| 641 | lock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | if (test_bit(META_discard, &mp->flag)) { |
| 643 | if (!new) { |
| 644 | jfs_error(inode->i_sb, |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 645 | "using a discarded metapage\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 646 | discard_metapage(mp); |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 647 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | clear_bit(META_discard, &mp->flag); |
| 650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } else { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 652 | INCREMENT(mpStat.pagealloc); |
| 653 | mp = alloc_metapage(GFP_NOFS); |
Juerg Haefliger | 88a96fa | 2017-10-04 04:24:41 -0400 | [diff] [blame] | 654 | if (!mp) |
| 655 | goto unlock; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 656 | mp->page = page; |
Dave Kleikamp | 11ab831 | 2017-07-05 15:26:49 -0400 | [diff] [blame] | 657 | mp->sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | mp->flag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | mp->xflag = COMMIT_PAGE; |
| 660 | mp->count = 1; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 661 | mp->nohomeok = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | mp->logical_size = size; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 663 | mp->data = page_address(page) + page_offset; |
| 664 | mp->index = lblock; |
| 665 | if (unlikely(insert_metapage(page, mp))) { |
| 666 | free_metapage(mp); |
| 667 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 669 | lock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 672 | if (new) { |
| 673 | jfs_info("zeroing mp = 0x%p", mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | memset(mp->data, 0, PSIZE); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 677 | unlock_page(page); |
| 678 | jfs_info("__get_metapage: returning = 0x%p data = 0x%p", mp, mp->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return mp; |
| 680 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 681 | unlock: |
| 682 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | return NULL; |
| 684 | } |
| 685 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 686 | void grab_metapage(struct metapage * mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 688 | jfs_info("grab_metapage: mp = 0x%p", mp); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 689 | get_page(mp->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | lock_page(mp->page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 691 | mp->count++; |
| 692 | lock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | unlock_page(mp->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 696 | void force_metapage(struct metapage *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
| 698 | struct page *page = mp->page; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 699 | jfs_info("force_metapage: mp = 0x%p", mp); |
| 700 | set_bit(META_forcewrite, &mp->flag); |
| 701 | clear_bit(META_sync, &mp->flag); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 702 | get_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | lock_page(page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 704 | set_page_dirty(page); |
Dave Kleikamp | 11ab831 | 2017-07-05 15:26:49 -0400 | [diff] [blame] | 705 | if (write_one_page(page)) |
| 706 | jfs_error(mp->sb, "write_one_page() failed\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 707 | clear_bit(META_forcewrite, &mp->flag); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 708 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 711 | void hold_metapage(struct metapage *mp) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 712 | { |
| 713 | lock_page(mp->page); |
| 714 | } |
| 715 | |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 716 | void put_metapage(struct metapage *mp) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 717 | { |
| 718 | if (mp->count || mp->nohomeok) { |
| 719 | /* Someone else will release this */ |
| 720 | unlock_page(mp->page); |
| 721 | return; |
| 722 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 723 | get_page(mp->page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 724 | mp->count++; |
| 725 | lock_metapage(mp); |
| 726 | unlock_page(mp->page); |
| 727 | release_metapage(mp); |
| 728 | } |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | void release_metapage(struct metapage * mp) |
| 731 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 732 | struct page *page = mp->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag); |
| 734 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 735 | BUG_ON(!page); |
| 736 | |
| 737 | lock_page(page); |
| 738 | unlock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | assert(mp->count); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 741 | if (--mp->count || mp->nohomeok) { |
| 742 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 743 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 747 | if (test_bit(META_dirty, &mp->flag)) { |
| 748 | set_page_dirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if (test_bit(META_sync, &mp->flag)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | clear_bit(META_sync, &mp->flag); |
Dave Kleikamp | 11ab831 | 2017-07-05 15:26:49 -0400 | [diff] [blame] | 751 | if (write_one_page(page)) |
| 752 | jfs_error(mp->sb, "write_one_page() failed\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 753 | lock_page(page); /* write_one_page unlocks the page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 755 | } else if (mp->lsn) /* discard_metapage doesn't remove it */ |
| 756 | remove_from_logsync(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 758 | /* Try to keep metapages from using up too much memory */ |
| 759 | drop_metapage(page, mp); |
Dave Kleikamp | d0e671a | 2006-12-20 15:55:35 -0600 | [diff] [blame] | 760 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 761 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 762 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void __invalidate_metapages(struct inode *ip, s64 addr, int len) |
| 766 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 767 | sector_t lblock; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 768 | int l2BlocksPerPage = PAGE_SHIFT - ip->i_blkbits; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 769 | int BlocksPerPage = 1 << l2BlocksPerPage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | /* All callers are interested in block device's mapping */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 771 | struct address_space *mapping = |
| 772 | JFS_SBI(ip->i_sb)->direct_inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | struct metapage *mp; |
| 774 | struct page *page; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 775 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | /* |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 778 | * Mark metapages to discard. They will eventually be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | * released, but should not be written. |
| 780 | */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 781 | for (lblock = addr & ~(BlocksPerPage - 1); lblock < addr + len; |
| 782 | lblock += BlocksPerPage) { |
| 783 | page = find_lock_page(mapping, lblock >> l2BlocksPerPage); |
| 784 | if (!page) |
| 785 | continue; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 786 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 787 | mp = page_to_mp(page, offset); |
| 788 | if (!mp) |
| 789 | continue; |
| 790 | if (mp->index < addr) |
| 791 | continue; |
| 792 | if (mp->index >= addr + len) |
| 793 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | clear_bit(META_dirty, &mp->flag); |
| 796 | set_bit(META_discard, &mp->flag); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 797 | if (mp->lsn) |
| 798 | remove_from_logsync(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 800 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 801 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| 805 | #ifdef CONFIG_JFS_STATISTICS |
Christoph Hellwig | 07a3b8e | 2018-04-11 16:51:18 +0200 | [diff] [blame] | 806 | int jfs_mpstat_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
Alexey Dobriyan | b2e03ca | 2008-05-13 08:22:10 -0500 | [diff] [blame] | 808 | seq_printf(m, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | "JFS Metapage statistics\n" |
| 810 | "=======================\n" |
| 811 | "page allocations = %d\n" |
| 812 | "page frees = %d\n" |
| 813 | "lock waits = %d\n", |
| 814 | mpStat.pagealloc, |
| 815 | mpStat.pagefree, |
| 816 | mpStat.lockwait); |
Alexey Dobriyan | b2e03ca | 2008-05-13 08:22:10 -0500 | [diff] [blame] | 817 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | #endif |