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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_JFS_STATISTICS |
| 24 | static 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 Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 31 | #define metapage_locked(mp) test_bit(META_locked, &(mp)->flag) |
Nick Piggin | 54af623 | 2007-11-26 14:58:10 -0600 | [diff] [blame] | 32 | #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] | 33 | |
| 34 | static inline void unlock_metapage(struct metapage *mp) |
| 35 | { |
Nick Piggin | 54af623 | 2007-11-26 14:58:10 -0600 | [diff] [blame] | 36 | clear_bit_unlock(META_locked, &mp->flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | wake_up(&mp->wait); |
| 38 | } |
| 39 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 40 | static inline void __lock_metapage(struct metapage *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | DECLARE_WAITQUEUE(wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | INCREMENT(mpStat.lockwait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | add_wait_queue_exclusive(&mp->wait, &wait); |
| 45 | do { |
| 46 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 47 | if (metapage_locked(mp)) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 48 | unlock_page(mp->page); |
Dave Kleikamp | 4aa0d23 | 2007-01-17 21:18:35 -0600 | [diff] [blame] | 49 | io_schedule(); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 50 | lock_page(mp->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | } while (trylock_metapage(mp)); |
| 53 | __set_current_state(TASK_RUNNING); |
| 54 | remove_wait_queue(&mp->wait, &wait); |
| 55 | } |
| 56 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 57 | /* |
| 58 | * Must have mp->page locked |
| 59 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static 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 Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 67 | static struct kmem_cache *metapage_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static mempool_t *metapage_mempool; |
| 69 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 70 | #define MPS_PER_PAGE (PAGE_SIZE >> L2PSIZE) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 71 | |
| 72 | #if MPS_PER_PAGE > 1 |
| 73 | |
| 74 | struct meta_anchor { |
| 75 | int mp_count; |
| 76 | atomic_t io_count; |
| 77 | struct metapage *mp[MPS_PER_PAGE]; |
| 78 | }; |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 79 | #define mp_anchor(page) ((struct meta_anchor *)page_private(page)) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 80 | |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 81 | static inline struct metapage *page_to_mp(struct page *page, int offset) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 82 | { |
| 83 | if (!PagePrivate(page)) |
| 84 | return NULL; |
| 85 | return mp_anchor(page)->mp[offset >> L2PSIZE]; |
| 86 | } |
| 87 | |
| 88 | static 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 Sesterhenn | 5b3030e | 2006-02-23 09:47:13 -0600 | [diff] [blame] | 97 | a = kzalloc(sizeof(struct meta_anchor), GFP_NOFS); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 98 | if (!a) |
| 99 | return -ENOMEM; |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 100 | set_page_private(page, (unsigned long)a); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 101 | 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 | |
| 115 | static 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 Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 128 | set_page_private(page, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 129 | ClearPagePrivate(page); |
| 130 | kunmap(page); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static inline void inc_io(struct page *page) |
| 135 | { |
| 136 | atomic_inc(&mp_anchor(page)->io_count); |
| 137 | } |
| 138 | |
| 139 | static 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 Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 146 | static inline struct metapage *page_to_mp(struct page *page, int offset) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 147 | { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 148 | return PagePrivate(page) ? (struct metapage *)page_private(page) : NULL; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline int insert_metapage(struct page *page, struct metapage *mp) |
| 152 | { |
| 153 | if (mp) { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 154 | set_page_private(page, (unsigned long)mp); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 155 | SetPagePrivate(page); |
| 156 | kmap(page); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static inline void remove_metapage(struct page *page, struct metapage *mp) |
| 162 | { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 163 | set_page_private(page, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 164 | 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 Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 173 | static inline struct metapage *alloc_metapage(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
David Rientjes | ee14624 | 2015-04-15 16:14:11 -0700 | [diff] [blame] | 175 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static inline void free_metapage(struct metapage *mp) |
| 189 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | mempool_free(mp, metapage_mempool); |
| 191 | } |
| 192 | |
| 193 | int __init metapage_init(void) |
| 194 | { |
| 195 | /* |
| 196 | * Allocate the metapage structures |
| 197 | */ |
| 198 | metapage_cache = kmem_cache_create("jfs_mp", sizeof(struct metapage), |
David Rientjes | ee14624 | 2015-04-15 16:14:11 -0700 | [diff] [blame] | 199 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | if (metapage_cache == NULL) |
| 201 | return -ENOMEM; |
| 202 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 203 | metapage_mempool = mempool_create_slab_pool(METAPOOL_MIN_PAGES, |
| 204 | metapage_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | if (metapage_mempool == NULL) { |
| 207 | kmem_cache_destroy(metapage_cache); |
| 208 | return -ENOMEM; |
| 209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | void metapage_exit(void) |
| 215 | { |
| 216 | mempool_destroy(metapage_mempool); |
| 217 | kmem_cache_destroy(metapage_cache); |
| 218 | } |
| 219 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 220 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | /* |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 231 | * Metapage address space operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 233 | |
| 234 | static sector_t metapage_get_blocks(struct inode *inode, sector_t lblock, |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 235 | int *len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 237 | int rc = 0; |
| 238 | int xflag; |
| 239 | s64 xaddr; |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 240 | 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] | 241 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 259 | static void last_read_complete(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 261 | if (!PageError(page)) |
| 262 | SetPageUptodate(page); |
| 263 | unlock_page(page); |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 266 | static void metapage_read_end_io(struct bio *bio) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 267 | { |
| 268 | struct page *page = bio->bi_private; |
| 269 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 270 | if (bio->bi_status) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 271 | printk(KERN_ERR "metapage_read_end_io: I/O error\n"); |
| 272 | SetPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 275 | dec_io(page, last_read_complete); |
| 276 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 279 | static void remove_from_logsync(struct metapage *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 281 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 290 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 298 | LOGSYNC_UNLOCK(log, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 301 | static void last_write_complete(struct page *page) |
| 302 | { |
| 303 | struct metapage *mp; |
| 304 | unsigned int offset; |
| 305 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 306 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 307 | 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 Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 321 | static void metapage_write_end_io(struct bio *bio) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 322 | { |
| 323 | struct page *page = bio->bi_private; |
| 324 | |
| 325 | BUG_ON(!PagePrivate(page)); |
| 326 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 327 | if (bio->bi_status) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 328 | 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 Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int metapage_writepage(struct page *page, struct writeback_control *wbc) |
| 336 | { |
| 337 | struct bio *bio = NULL; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 338 | int block_offset; /* block offset of mp within page */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 339 | struct inode *inode = page->mapping->host; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 340 | int blocks_per_mp = JFS_SBI(inode->i_sb)->nbperpage; |
| 341 | int len; |
| 342 | int xlen; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 343 | struct metapage *mp; |
| 344 | int redirty = 0; |
| 345 | sector_t lblock; |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 346 | int nr_underway = 0; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 347 | 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 Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 352 | int offset; |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 353 | int bad_blocks = 0; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 354 | |
| 355 | page_start = (sector_t)page->index << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 356 | (PAGE_SHIFT - inode->i_blkbits); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 357 | BUG_ON(!PageLocked(page)); |
| 358 | BUG_ON(PageWriteback(page)); |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 359 | set_page_writeback(page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 360 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 361 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 362 | 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 Kleikamp | ac17b8b | 2005-10-03 15:32:11 -0500 | [diff] [blame] | 369 | /* |
| 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 Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 375 | continue; |
| 376 | } |
| 377 | |
| 378 | clear_bit(META_dirty, &mp->flag); |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 379 | set_bit(META_io, &mp->flag); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 380 | 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 Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 388 | 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 Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 399 | if (!bio->bi_iter.bi_size) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 400 | goto dump_bio; |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 401 | submit_bio(bio); |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 402 | nr_underway++; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 403 | bio = NULL; |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 404 | } else |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 405 | inc_io(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 406 | xlen = (PAGE_SIZE - offset) >> inode->i_blkbits; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 407 | pblock = metapage_get_blocks(inode, lblock, &xlen); |
| 408 | if (!pblock) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 409 | printk(KERN_ERR "JFS: metapage_get_blocks failed\n"); |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 410 | /* |
| 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 Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 415 | continue; |
| 416 | } |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 417 | len = min(xlen, (int)JFS_SBI(inode->i_sb)->nbperpage); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 418 | |
| 419 | bio = bio_alloc(GFP_NOFS, 1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 420 | bio_set_dev(bio, inode->i_sb->s_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 421 | bio->bi_iter.bi_sector = pblock << (inode->i_blkbits - 9); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 422 | bio->bi_end_io = metapage_write_end_io; |
| 423 | bio->bi_private = page; |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 424 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 425 | |
| 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 Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 436 | if (!bio->bi_iter.bi_size) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 437 | goto dump_bio; |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 438 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 439 | submit_bio(bio); |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 440 | nr_underway++; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 441 | } |
| 442 | if (redirty) |
| 443 | redirty_page_for_writepage(wbc, page); |
| 444 | |
| 445 | unlock_page(page); |
| 446 | |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 447 | if (bad_blocks) |
| 448 | goto err_out; |
| 449 | |
Dave Kleikamp | 29a424f | 2008-01-03 13:09:33 -0600 | [diff] [blame] | 450 | if (nr_underway == 0) |
| 451 | end_page_writeback(page); |
| 452 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 453 | return 0; |
| 454 | add_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; |
| 458 | dump_bio: |
Dave Kleikamp | 288e4d8 | 2007-06-13 10:17:50 -0500 | [diff] [blame] | 459 | print_hex_dump(KERN_ERR, "JFS: dump of bio: ", DUMP_PREFIX_ADDRESS, 16, |
| 460 | 4, bio, sizeof(*bio), 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 461 | skip: |
| 462 | bio_put(bio); |
| 463 | unlock_page(page); |
| 464 | dec_io(page, last_write_complete); |
Dave Kleikamp | 1ad53a9 | 2009-01-30 14:09:06 -0600 | [diff] [blame] | 465 | err_out: |
| 466 | while (bad_blocks--) |
| 467 | dec_io(page, last_write_complete); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 468 | return -EIO; |
| 469 | } |
| 470 | |
| 471 | static int metapage_readpage(struct file *fp, struct page *page) |
| 472 | { |
| 473 | struct inode *inode = page->mapping->host; |
| 474 | struct bio *bio = NULL; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 475 | int block_offset; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 476 | int blocks_per_page = PAGE_SIZE >> inode->i_blkbits; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 477 | sector_t page_start; /* address of page in fs blocks */ |
| 478 | sector_t pblock; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 479 | int xlen; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 480 | unsigned int len; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 481 | int offset; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 482 | |
| 483 | BUG_ON(!PageLocked(page)); |
| 484 | page_start = (sector_t)page->index << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 485 | (PAGE_SHIFT - inode->i_blkbits); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 486 | |
| 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 Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 497 | submit_bio(bio); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 498 | |
| 499 | bio = bio_alloc(GFP_NOFS, 1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 500 | bio_set_dev(bio, inode->i_sb->s_bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 501 | bio->bi_iter.bi_sector = |
| 502 | pblock << (inode->i_blkbits - 9); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 503 | bio->bi_end_io = metapage_read_end_io; |
| 504 | bio->bi_private = page; |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 505 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 506 | 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 Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 515 | submit_bio(bio); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 516 | else |
| 517 | unlock_page(page); |
| 518 | |
| 519 | return 0; |
| 520 | |
| 521 | add_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 Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 528 | static int metapage_releasepage(struct page *page, gfp_t gfp_mask) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 529 | { |
| 530 | struct metapage *mp; |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 531 | int ret = 1; |
Dave Kleikamp | 967c9ec | 2008-01-10 16:04:25 -0600 | [diff] [blame] | 532 | int offset; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 533 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 534 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 535 | mp = page_to_mp(page, offset); |
| 536 | |
| 537 | if (!mp) |
| 538 | continue; |
| 539 | |
| 540 | jfs_info("metapage_releasepage: mp = 0x%p", mp); |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 541 | if (mp->count || mp->nohomeok || |
| 542 | test_bit(META_dirty, &mp->flag)) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 543 | jfs_info("count = %ld, nohomeok = %d", mp->count, |
| 544 | mp->nohomeok); |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 545 | ret = 0; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 546 | continue; |
| 547 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 548 | if (mp->lsn) |
| 549 | remove_from_logsync(mp); |
| 550 | remove_metapage(page, mp); |
| 551 | INCREMENT(mpStat.pagefree); |
| 552 | free_metapage(mp); |
| 553 | } |
Dave Kleikamp | b964638 | 2006-05-24 07:43:38 -0500 | [diff] [blame] | 554 | return ret; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 555 | } |
| 556 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 557 | static void metapage_invalidatepage(struct page *page, unsigned int offset, |
| 558 | unsigned int length) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 559 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 560 | BUG_ON(offset || length < PAGE_SIZE); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 561 | |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 562 | BUG_ON(PageWriteback(page)); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 563 | |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 564 | metapage_releasepage(page, 0); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 565 | } |
| 566 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 567 | const struct address_space_operations jfs_metapage_aops = { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 568 | .readpage = metapage_readpage, |
| 569 | .writepage = metapage_writepage, |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 570 | .releasepage = metapage_releasepage, |
| 571 | .invalidatepage = metapage_invalidatepage, |
| 572 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 573 | }; |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | struct metapage *__get_metapage(struct inode *inode, unsigned long lblock, |
| 576 | unsigned int size, int absolute, |
| 577 | unsigned long new) |
| 578 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | int l2BlocksPerPage; |
| 580 | int l2bsize; |
| 581 | struct address_space *mapping; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 582 | struct metapage *mp = NULL; |
| 583 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | unsigned long page_index; |
| 585 | unsigned long page_offset; |
| 586 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 587 | jfs_info("__get_metapage: ino = %ld, lblock = 0x%lx, abs=%d", |
| 588 | inode->i_ino, lblock, absolute); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 590 | l2bsize = inode->i_blkbits; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 591 | l2BlocksPerPage = PAGE_SHIFT - l2bsize; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 592 | page_index = lblock >> l2BlocksPerPage; |
| 593 | page_offset = (lblock - (page_index << l2BlocksPerPage)) << l2bsize; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 594 | if ((page_offset + size) > PAGE_SIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 595 | jfs_err("MetaData crosses page boundary!!"); |
| 596 | jfs_err("lblock = %lx, size = %d", lblock, size); |
| 597 | dump_stack(); |
| 598 | return NULL; |
| 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (absolute) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 601 | mapping = JFS_SBI(inode->i_sb)->direct_inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | 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. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 613 | if (new && (PSIZE == PAGE_SIZE)) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 614 | 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 Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 621 | page = read_mapping_page(mapping, page_index, NULL); |
Dave Kleikamp | c40c2024 | 2005-07-22 11:08:44 -0500 | [diff] [blame] | 622 | if (IS_ERR(page) || !PageUptodate(page)) { |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 623 | jfs_err("read_mapping_page failed!"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 624 | return NULL; |
| 625 | } |
| 626 | lock_page(page); |
| 627 | } |
| 628 | |
| 629 | mp = page_to_mp(page, page_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | if (mp) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 631 | if (mp->logical_size != size) { |
| 632 | jfs_error(inode->i_sb, |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 633 | "get_mp->logical_size != size\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 634 | jfs_err("logical_size = %d, size = %d", |
| 635 | mp->logical_size, size); |
| 636 | dump_stack(); |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 637 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | mp->count++; |
| 640 | lock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | if (test_bit(META_discard, &mp->flag)) { |
| 642 | if (!new) { |
| 643 | jfs_error(inode->i_sb, |
Joe Perches | eb8630d | 2013-06-04 16:39:15 -0700 | [diff] [blame] | 644 | "using a discarded metapage\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 645 | discard_metapage(mp); |
Dave Kleikamp | 63f83c9 | 2006-10-02 09:55:27 -0500 | [diff] [blame] | 646 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | clear_bit(META_discard, &mp->flag); |
| 649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } else { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 651 | INCREMENT(mpStat.pagealloc); |
| 652 | mp = alloc_metapage(GFP_NOFS); |
Juerg Haefliger | 88a96fa | 2017-10-04 04:24:41 -0400 | [diff] [blame] | 653 | if (!mp) |
| 654 | goto unlock; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 655 | mp->page = page; |
Dave Kleikamp | 11ab831 | 2017-07-05 15:26:49 -0400 | [diff] [blame] | 656 | mp->sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | mp->flag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | mp->xflag = COMMIT_PAGE; |
| 659 | mp->count = 1; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 660 | mp->nohomeok = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | mp->logical_size = size; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 662 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 668 | lock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 671 | if (new) { |
| 672 | jfs_info("zeroing mp = 0x%p", mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | memset(mp->data, 0, PSIZE); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 674 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 676 | unlock_page(page); |
| 677 | 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] | 678 | return mp; |
| 679 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 680 | unlock: |
| 681 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return NULL; |
| 683 | } |
| 684 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 685 | void grab_metapage(struct metapage * mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 687 | jfs_info("grab_metapage: mp = 0x%p", mp); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 688 | get_page(mp->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | lock_page(mp->page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 690 | mp->count++; |
| 691 | lock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | unlock_page(mp->page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 695 | void force_metapage(struct metapage *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
| 697 | struct page *page = mp->page; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 698 | jfs_info("force_metapage: mp = 0x%p", mp); |
| 699 | set_bit(META_forcewrite, &mp->flag); |
| 700 | clear_bit(META_sync, &mp->flag); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 701 | get_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | lock_page(page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 703 | set_page_dirty(page); |
Dave Kleikamp | 11ab831 | 2017-07-05 15:26:49 -0400 | [diff] [blame] | 704 | if (write_one_page(page)) |
| 705 | jfs_error(mp->sb, "write_one_page() failed\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 706 | clear_bit(META_forcewrite, &mp->flag); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 707 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 710 | void hold_metapage(struct metapage *mp) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 711 | { |
| 712 | lock_page(mp->page); |
| 713 | } |
| 714 | |
Dave Kleikamp | 1868f4a | 2005-05-04 15:29:35 -0500 | [diff] [blame] | 715 | void put_metapage(struct metapage *mp) |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 716 | { |
| 717 | if (mp->count || mp->nohomeok) { |
| 718 | /* Someone else will release this */ |
| 719 | unlock_page(mp->page); |
| 720 | return; |
| 721 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 722 | get_page(mp->page); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 723 | mp->count++; |
| 724 | lock_metapage(mp); |
| 725 | unlock_page(mp->page); |
| 726 | release_metapage(mp); |
| 727 | } |
| 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | void release_metapage(struct metapage * mp) |
| 730 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 731 | struct page *page = mp->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | jfs_info("release_metapage: mp = 0x%p, flag = 0x%lx", mp, mp->flag); |
| 733 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 734 | BUG_ON(!page); |
| 735 | |
| 736 | lock_page(page); |
| 737 | unlock_metapage(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
| 739 | assert(mp->count); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 740 | if (--mp->count || mp->nohomeok) { |
| 741 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 742 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 746 | if (test_bit(META_dirty, &mp->flag)) { |
| 747 | set_page_dirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (test_bit(META_sync, &mp->flag)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | clear_bit(META_sync, &mp->flag); |
Dave Kleikamp | 11ab831 | 2017-07-05 15:26:49 -0400 | [diff] [blame] | 750 | if (write_one_page(page)) |
| 751 | jfs_error(mp->sb, "write_one_page() failed\n"); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 752 | lock_page(page); /* write_one_page unlocks the page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 754 | } else if (mp->lsn) /* discard_metapage doesn't remove it */ |
| 755 | remove_from_logsync(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 757 | /* Try to keep metapages from using up too much memory */ |
| 758 | drop_metapage(page, mp); |
Dave Kleikamp | d0e671a | 2006-12-20 15:55:35 -0600 | [diff] [blame] | 759 | |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 760 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 761 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | void __invalidate_metapages(struct inode *ip, s64 addr, int len) |
| 765 | { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 766 | sector_t lblock; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 767 | int l2BlocksPerPage = PAGE_SHIFT - ip->i_blkbits; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 768 | int BlocksPerPage = 1 << l2BlocksPerPage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* All callers are interested in block device's mapping */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 770 | struct address_space *mapping = |
| 771 | JFS_SBI(ip->i_sb)->direct_inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | struct metapage *mp; |
| 773 | struct page *page; |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 774 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | /* |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 777 | * Mark metapages to discard. They will eventually be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | * released, but should not be written. |
| 779 | */ |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 780 | 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. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 785 | for (offset = 0; offset < PAGE_SIZE; offset += PSIZE) { |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 786 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | clear_bit(META_dirty, &mp->flag); |
| 795 | set_bit(META_discard, &mp->flag); |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 796 | if (mp->lsn) |
| 797 | remove_from_logsync(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
Dave Kleikamp | 7fab479 | 2005-05-02 12:25:02 -0600 | [diff] [blame] | 799 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 800 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
| 804 | #ifdef CONFIG_JFS_STATISTICS |
Christoph Hellwig | 07a3b8e | 2018-04-11 16:51:18 +0200 | [diff] [blame] | 805 | int jfs_mpstat_proc_show(struct seq_file *m, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Alexey Dobriyan | b2e03ca | 2008-05-13 08:22:10 -0500 | [diff] [blame] | 807 | seq_printf(m, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | "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 Dobriyan | b2e03ca | 2008-05-13 08:22:10 -0500 | [diff] [blame] | 816 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | #endif |