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