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